Skip to content

feat(sente): move timed-out shell commands to the background instead of killing them - #73

Open
yukihamada wants to merge 2 commits into
headless-model-fallbackfrom
sente-shell-bg
Open

yukihamada wants to merge 2 commits into
headless-model-fallbackfrom
sente-shell-bg

Conversation

@yukihamada

@yukihamada yukihamada commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

概要

シェル(bash)ツールで時間切れになったコマンドを、殺さずバックグラウンドジョブに移します(本家 anomalyco#63024 / anomalyco#63699 と同じ考え方)。Sente 既存の BackgroundJob レジストリに乗せ、結果は wait ツールか完了通知で受け取ります。

動き

  • 時間切れ → 同じプロセスを BackgroundJob(type bash、metadata sessionId/pid/outputPath/mustWait)に引き渡す。締め切り直後に終わっていたものは普通の完了として扱う。
  • 対話セッション: 「完了時に自動で通知される。通知を待ち、他にやることがなければ返答を終える」とだけ案内。完了時に <background_shell> を1回だけ入れる。wait で結果を受け取った(または受け取り中の)ジョブは通知しない。
  • sente run とサブエージェント: 「返答を終えると実行が終わりコマンドも止まる。終える前に wait(job)、pending なら終わるまで再度呼ぶ」と案内。この種のジョブに限り、wait の pending 応答は「再試行しない」ではなく「もう一度呼ぶ」。sente run はセッションの metadata(sente.oneShot)で印を付けるので --attach でも効く。終了時に印を外し、--attach ではセッションを abort して遠隔サーバーに残さない。
  • wait ツール: job 専用(GitHub の説明なし。引数は job/timeout/stop)。そのセッションに背景シェルジョブができてから初めてツール一覧に出す(それまではトークン0)。stop: true でジョブIDから停止。PID での kill 案内は出さない。
  • --unattended: 組み込み許可リストで wait を job_* に限って許可(他セッションのジョブはツール側で拒否)。
  • 上限: 背景で動ける時間 SENTE_SHELL_BACKGROUND_MAX_MS(既定60分)、セッションごとの同時数 SENTE_SHELL_BACKGROUND_MAX_JOBS(既定4。超えたら従来どおり止めて理由を返す)。
  • 無効化: SENTE_SHELL_TIMEOUT_BACKGROUND=0 で時間切れ時に止める従来の動き。時間切れメッセージにも1行、cli.mdx(英・日)にも記載。
  • 表示: sente run と TUI で「running in background (job …)」と表示し、completed と区別。
  • 停止: 時間内の中断、セッション中断(背景ジョブを cancel)、wait stop、sente run 終了(インスタンス破棄)のどれでもプロセスグループ(子孫含む)を止め、SIGTERM を無視しても3秒で SIGKILL。

テスト

  • bun run typecheck: packages/sente・packages/tui ともエラー0
  • test/tool/shell-background.test.ts 21件 pass(3回連続)。子孫まで停止、背景移行後の上限超え出力→ファイル退避、本物の wait ツール経由、wait 後の通知抑止、pending 後は通知される、失敗時通知、非対話・サブエージェントは待つ案内で通知なし、one-shot の pending は「再度呼ぶ」、stop、他セッション拒否、寿命上限、同時数上限、締め切り付近の競合、無効化、中断、インスタンス破棄
  • test/cli/run/tool-bash-background.test.ts(表示)、registry.test.ts(wait の出し分け)、unattended.test.ts(wait 許可)追加
  • ローカル test/tool/ test/background/ test/cli/run/ test/permission/ runtime-flags session/prompt: 868 pass / 15 fail。15件はすべて base でも失敗(webfetch 204/205、security-boundary-audit の redirect ×5、session/prompt の loop ×7、run-process の不安定1件)
  • CI unit(linux): PR の失敗一覧は base(headless-model-fallback を workflow_dispatch した run 45288524310)の失敗一覧に含まれる(新規失敗なし)
  • CI e2e(linux): base でも同じ Playwright does not support chromium on ubuntu26.04-x64 で失敗(ランナー側の問題)

実機(teai/cheap)

  • 非対話: sleep 150; echo LONG_DONE を timeout 2000ms → 背景 → モデルが wait → Exit code: 0 / LONG_DONE を返して終了(158秒)。終了後 sleep 残存なし、セッションの印も解除済み
  • 対話(headless server): 「結果を報告して」型 → bash→wait→回答の3回呼び出し、通知の重複なし。「後で教えて」型 → bash→即返答、完了時に通知1回→回答(計3回呼び出し・2ターン)

🤖 Generated with Claude Code

…of killing them

When a bash command is still running at its timeout, hand the process to a
BackgroundJob (same registry as background tasks) instead of killing it. The
model gets the job ID, PID, the output file it keeps streaming to, and is told
to collect the result with the wait tool (kind "job").

- On by default; SENTE_SHELL_TIMEOUT_BACKGROUND=0 restores kill-on-timeout.
- Register the existing wait tool (it was never exposed) and clarify its job param.
- Interactive sessions get a synthetic <background_shell> message when the job
  finishes; one-shot `sente run` (SENTE_NONINTERACTIVE=1) does not inject a turn.
- The process lives in its own scope: abort before the timeout, job cancel
  (session interrupt) and instance dispose (sente run exit) all kill the process
  group, escalating to SIGKILL after 3s, so nothing is orphaned or waited on forever.
- Job output goes through SecretRedact; sandbox note and truncation as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

Follow-up review fixes for timed-out shell commands moving to the background.

- One-shot runs and subagents must wait in-turn: the message says the command
  stops when the reply ends, and wait answers pending with "call wait again"
  (no "do not retry"). `sente run` marks the session (metadata, so it also
  works over --attach) and, under --attach, aborts the session at the end so
  nothing is left running on the remote server.
- No double delivery: a job whose result a wait call delivered (or is
  delivering) is not announced again. Interactive guidance is only "wait for
  the notification; end your reply if nothing else to do".
- --unattended: the builtin policy allows wait on job_* (the tool still
  rejects other sessions' jobs).
- Limits: background lifetime (SENTE_SHELL_BACKGROUND_MAX_MS, default 60 min)
  and per-session concurrency (SENTE_SHELL_BACKGROUND_MAX_JOBS, default 4).
- wait is a job-only tool (no GitHub text) offered only once the session has a
  background shell job; it can stop a job by ID (stop: true). No PID kill hint.
- A command that exits right at the deadline is treated as finished.
- sente run and the TUI show "running in background (job …)" instead of completed.
- Docs for the new environment variables; tests for grandchildren, spill after
  handover, real wait tool, notification suppression, failure notice, one-shot
  and subagent sessions, limits, stop, deadline race and the run renderer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant