Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions Claude/templates/linux/cron-launcher.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
# ============================================================
# cron-launcher.sh - Linux 側で ClaudeCode を cron から起動するラッパ
# ClaudeOS v3.2.16
# ClaudeOS v3.2.31
#
# Usage: cron-launcher.sh <project> <duration-minutes>
#
Expand Down Expand Up @@ -102,7 +102,7 @@ finalize() {
if command -v tmux >/dev/null 2>&1; then
tmux kill-session -t "claudeos-${SAFE_PROJECT}" 2>/dev/null || true
fi
rm -f "$CLAUDE_WRAPPER" "$CLAUDE_EXIT_FILE"
rm -f "$CLAUDE_WRAPPER" "$CLAUDE_EXIT_FILE" "${CLAUDE_WRAPPER%.sh}.prompt"

# --- v3.2.0: HTML レポートメール送信 ---
# 明示的トグル CLAUDEOS_EMAIL_ENABLED=1 が必要。誤送信防止のため既定 off。
Expand Down Expand Up @@ -141,13 +141,19 @@ if [[ -f "$PROJECT_DIR/.claude/START_PROMPT.md" ]]; then
PROMPT_ARG="$(cat "$PROJECT_DIR/.claude/START_PROMPT.md")"
fi

# wrapper script: env var 経由で引数を安全に渡す(tmux new-session での引用符崩れ対策)
# PROMPT_ARG をサイドカーファイルへ書き出す(tmux env var 継承バグ / 長大引数問題を回避)
PROMPT_FILE="${CLAUDE_WRAPPER%.sh}.prompt"
printf '%s' "$PROMPT_ARG" > "$PROMPT_FILE"

# wrapper script: -e フラグ経由で env var を渡す(tmux サーバーのグローバル環境に依存しない)
# set -e を使わず claude_exit に明示的に格納する(非0終了でも wait-for -S を必ず実行するため)
cat > "$CLAUDE_WRAPPER" <<'WRAPPER_EOF'
#!/usr/bin/env bash
claude_exit=0
if [[ -n "${_CLAUDEOS_PROMPT_ARG:-}" ]]; then
timeout --foreground "${_CLAUDEOS_DURATION_SEC}s" claude --dangerously-skip-permissions "${_CLAUDEOS_PROMPT_ARG}" || claude_exit=$?
_prompt_file="${_CLAUDEOS_PROMPT_FILE:-}"
if [[ -f "$_prompt_file" ]] && [[ -s "$_prompt_file" ]]; then
_prompt_content="$(cat "$_prompt_file")"
timeout --foreground "${_CLAUDEOS_DURATION_SEC}s" claude --dangerously-skip-permissions "$_prompt_content" || claude_exit=$?
else
Comment on lines +153 to 157

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

Claude Code CLI の最新公式ドキュメントで、claude --dangerously-skip-permissionsに対してプロンプトを argv ではなく「標準入力」または「ファイル」から渡す方法(例: --prompt-file,@file, stdin)がサポートされているか確認してください。

💡 Result:

Claude Code CLIの最新公式ドキュメント(https://code.claude.com/docs/en/cli-referenceなど)では、claude --dangerously-skip-permissionsに対して、プロンプトをargv(コマンドライン引数)ではなく標準入力(stdin)やファイル(--prompt-file, @fileなど)から渡す専用のオプション(例: --prompt-fileや@file)は明示的にリストされていません。 ただし、-p(printモード、非インタラクティブ)を使用する場合、stdinからの入力がサポートされており、例としてcat file | claude -p "query"が公式ドキュメントで示されています。この場合、--dangerously-skip-permissionsを組み合わせることが可能で、cat prompt.txt | claude -p --dangerously-skip-permissions "追加のクエリ"のようにstdinでプロンプト部分を渡せます。argvの"query"がstdinの内容に置き換わるかは文脈からstdinがコンテキストとして追加される形です。 @fileのようなファイル指定オプションや--prompt-fileはflagsリストに存在せず、サポートされていないと判断されます。システムプロンプト用には--system-prompt-fileがありますが、ユーザープロンプト用ではありません。

Citations:


大容量プロンプト対策が未完了です(argv へ直接渡すことによる引数長上限の制約が残ります)。

Line 155-157 で .prompt ファイルの内容全体を _prompt_content に読み込み、その後 claude --dangerously-skip-permissions "$_prompt_content" として単一の引数に渡しているため、システムの引数長上限に依存する制約が残ります。

公式ドキュメント確認により、--prompt-file や @file のような専用オプションは現在サポートされていませんが、標準入力(stdin)を経由する方法が利用可能です。以下の形式でプロンプトをファイルから読み込むことで、引数長の制限を回避できます:

cat "$_prompt_file" | timeout --foreground "${_CLAUDEOS_DURATION_SEC}s" claude -p --dangerously-skip-permissions

現在の実装を stdin ベースに変更することで、Issue #186 の「大容量引数対策」に対応してください。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Claude/templates/linux/cron-launcher.sh` around lines 153 - 157, The current
code reads the whole prompt into _prompt_content and passes it as a single argv
to claude (variables: _prompt_file, _prompt_content, the timeout --foreground
... claude invocation), which still hits system ARG_MAX; change the invocation
to stream the prompt via stdin instead of building _prompt_content: read/verify
_prompt_file exists and is non-empty, then pipe its contents into timeout
--foreground "${_CLAUDEOS_DURATION_SEC}s" claude -p
--dangerously-skip-permissions (capture exit status into claude_exit as before)
and remove the direct use of _prompt_content so large prompt files bypass argv
length limits.

timeout --foreground "${_CLAUDEOS_DURATION_SEC}s" claude --dangerously-skip-permissions || claude_exit=$?
fi
Expand All @@ -157,18 +163,21 @@ tmux wait-for -S "${_CLAUDEOS_TMUX_DONE}"
WRAPPER_EOF
chmod +x "$CLAUDE_WRAPPER"

export _CLAUDEOS_PROMPT_ARG="$PROMPT_ARG"
export _CLAUDEOS_DURATION_SEC="$DURATION_SEC"
export _CLAUDEOS_EXIT_FILE="$CLAUDE_EXIT_FILE"
export _CLAUDEOS_TMUX_DONE="done-${SAFE_PROJECT}"
_TMUX_DONE="done-${SAFE_PROJECT}"

if command -v tmux >/dev/null 2>&1 && [[ "${CLAUDEOS_TMUX:-1}" == "1" ]]; then
# Claude を tmux セッション内で起動(TTY あり → attach で UI 閲覧可能)
# -e で env var を明示渡し(tmux サーバーのグローバル環境に依存しない)
tmux kill-session -t "$TMUX_SESSION" 2>/dev/null || true
tmux new-session -d -s "$TMUX_SESSION" -x 220 -y 50 "$CLAUDE_WRAPPER"
tmux new-session -d -s "$TMUX_SESSION" -x 220 -y 50 \
-e "_CLAUDEOS_DURATION_SEC=$DURATION_SEC" \
-e "_CLAUDEOS_EXIT_FILE=$CLAUDE_EXIT_FILE" \
-e "_CLAUDEOS_TMUX_DONE=$_TMUX_DONE" \
-e "_CLAUDEOS_PROMPT_FILE=$PROMPT_FILE" \
"$CLAUDE_WRAPPER"
Comment on lines +172 to +177
echo "[cron-launcher] tmux attach -t $TMUX_SESSION (UI閲覧用)" >> "$LOG_FILE"
# tmux セッション終了まで待機
tmux wait-for "${_CLAUDEOS_TMUX_DONE}"
tmux wait-for "$_TMUX_DONE"
else
# tmux 無効時は従来通り TTY なし実行
timeout --foreground "${DURATION_SEC}s" claude --dangerously-skip-permissions ${PROMPT_ARG:+"$PROMPT_ARG"} >> "$LOG_FILE" 2>&1
Expand Down
7 changes: 7 additions & 0 deletions TASKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
43. [DONE] [Priority:P2][Owner:Developer][Source:GitHub#182] v3.2.28 CronManager / LogManager ユニットテスト追加 — CronManager.Tests.ps1 27件 / LogManager.Tests.ps1 11件 / 515 PASS
44. [DONE] [Priority:P2][Owner:Developer][Source:GitHub#183] v3.2.29 Phase 2 ユニットテスト追加 — MenuCommon.Tests.ps1 19件 / SSHHelper.Tests.ps1 7件 / SessionTabManager.Tests.ps1 15件 / 556 PASS
45. [DONE] [Priority:P2][Owner:Developer][Source:GitHub#184] v3.2.30 Phase 3 ユニットテスト追加 — StatuslineManager.Tests.ps1 7件 / McpHealthCheck.Tests.ps1 6件 (InModuleScope) / 569 PASS
46. [DONE] [Priority:P1][Owner:Developer][Source:GitHub#185+#186] v3.2.31 Watch-ClaudeLog 起動時セッション検出修正 + cron-launcher.sh tmux -e 明示 env var 渡し / PROMPT_ARG サイドカーファイル化 / サーバー反映済み

## Auto Extracted From Agent Teams Matrix

Expand Down Expand Up @@ -101,6 +102,12 @@









Comment on lines +105 to +110



Expand Down
11 changes: 10 additions & 1 deletion scripts/tools/Watch-ClaudeLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Linux 側の cron-launcher.sh が出力するログファイルを自動検出し
Windows Terminal の新規タブで tail -f を開始する。
cron 実行前に起動しておくと、発火を検知して自動でログ表示を開始する。
ClaudeOS v3.2.16
ClaudeOS v3.2.31
.PARAMETER NewTab
Windows Terminal の新規タブで開く(既定: 現在のウィンドウで実行)。
.PARAMETER PollIntervalSeconds
Expand Down Expand Up @@ -182,6 +182,15 @@ function Open-SessionInfoTab {
Write-WaitHeader

$knownLog = Get-LatestLog
# 起動時に15分以内のログがある場合は実行中と見なして即監視
if ($knownLog -match 'cron-(\d{8}-\d{6})\.log$') {
$logTime = [datetime]::MinValue
$parsed = [datetime]::TryParseExact($Matches[1], 'yyyyMMdd-HHmmss', $null,
[System.Globalization.DateTimeStyles]::None, [ref]$logTime)
if ($parsed -and ((Get-Date) - $logTime -lt [timespan]::FromMinutes(15))) {
$knownLog = '' # 新規扱いにして直後のループで検出させる
}
}
Comment on lines +185 to +193

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# First, let's examine the PowerShell script to see the actual code at lines 185-193
cat -n scripts/tools/Watch-ClaudeLog.ps1 | sed -n '180,200p'

Repository: Kensan196948G/ClaudeCode-StartUpTools-New

Length of output: 873


🏁 Script executed:

# Let's also check the Linux side to see how timestamps are generated
cat -n Claude/templates/linux/cron-launcher.sh | head -60

Repository: Kensan196948G/ClaudeCode-StartUpTools-New

Length of output: 2299


🏁 Script executed:

# Look for SSH-related variables and usage patterns in the PowerShell script
rg -n 'SshTarget|SshUser|ssh ' scripts/tools/Watch-ClaudeLog.ps1 | head -40

Repository: Kensan196948G/ClaudeCode-StartUpTools-New

Length of output: 454


🏁 Script executed:

# Check the beginning of the file to understand SSH setup
head -100 scripts/tools/Watch-ClaudeLog.ps1 | tail -80

Repository: Kensan196948G/ClaudeCode-StartUpTools-New

Length of output: 3218


タイムゾーン差で起動時の「15分以内」判定が誤る可能性があります。

Line 188-190 は監視端末側時刻 (Get-Date) で比較していますが、ログ名タイムスタンプは Linux 側で生成されています(cron-launcher.sh 44行目の date +'%Y%m%d-%H%M%S')。端末とサーバーのタイムゾーンが異なると、起動直後検出の取りこぼし/誤検出が起きます。比較はログのファイルmtimeをepoch秒で取得して行う方が安全です。

🔧 例: タイムゾーン非依存の比較に寄せる差分案
-$knownLog = Get-LatestLog
-# 起動時に15分以内のログがある場合は実行中と見なして即監視
-if ($knownLog -match 'cron-(\d{8}-\d{6})\.log$') {
-    $logTime = [datetime]::MinValue
-    $parsed  = [datetime]::TryParseExact($Matches[1], 'yyyyMMdd-HHmmss', $null,
-        [System.Globalization.DateTimeStyles]::None, [ref]$logTime)
-    if ($parsed -and ((Get-Date) - $logTime -lt [timespan]::FromMinutes(15))) {
-        $knownLog = ''  # 新規扱いにして直後のループで検出させる
-    }
-}
+$knownLog = Get-LatestLog
+# 起動時に15分以内のログがある場合は実行中と見なして即監視(タイムゾーン非依存)
+if ($knownLog) {
+    $latestEpochRaw = ssh $SshTarget "stat -c %Y '$knownLog' 2>/dev/null" 2>$null
+    if ($latestEpochRaw -match '^\d+$') {
+        $ageSec = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() - [int64]$latestEpochRaw
+        if ($ageSec -ge 0 -and $ageSec -lt 900) {
+            $knownLog = ''  # 新規扱いにして直後のループで検出させる
+        }
+    }
+}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/tools/Watch-ClaudeLog.ps1` around lines 185 - 193, The current
15-minute check compares the parsed timestamp from the log filename (via
$Matches) against the local Get-Date which can be wrong across timezones;
instead retrieve the file's modification time (e.g., using
Get-Item/Get-ChildItem and LastWriteTimeUtc or LastWriteTime converted to UTC)
and compare epoch/UTC times to Get-Date.ToUniversalTime() so the logic in the
block that inspects $knownLog (the regex match 'cron-(\d{8}-\d{6})\.log$' and
variables $Matches, $logTime, $parsed) becomes timezone-independent: locate the
code that sets $logTime/$parsed and replace the filename-based local-time
comparison with a file-based UTC mtime comparison and keep the original behavior
of clearing $knownLog when the file mtime is within 15 minutes.

$dotCount = 0

while ($true) {
Expand Down
Loading