Skip to content

Handoff: the command source writes part 2 (HO3) - #775

Merged
scuttletoby merged 2 commits into
mainfrom
fork/handoff-ho3-command
Nov 21, 2026
Merged

scuttletoby merged 2 commits into
mainfrom
fork/handoff-ho3-command

Conversation

@scuttletoby

@scuttletoby scuttletoby commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

What / why

Handoff design row HO3: the command source now writes part 2 of the handoff message.

With handoff.source: "command", the handoff service runs the program in handoff.command (from the person's settings) in the thread's project folder, right after the settle and before the send. It reuses the wait checks' runner: the process part of makeCheckJudge is now makeSettingsProgramRunner in waits/WaitJudges.ts, and the check judge and the handoff both call it. The command runs with no shell, a 60 s timeout (the process is killed), a 32 KB cap, and T3CODE_THREAD_ID / T3CODE_PROJECT_ID in its environment. Its trimmed output becomes part 2 ("Handoff from the handoff command:").

Failure rules (design 4.3): a non-zero exit, a timeout (partial output unused), empty output, output over 32 KB, a program that cannot start, or a project that cannot be read all count as failure.

  • At the idle trigger the handoff is skipped, and the next turn end decides again.
  • At the size backstop it hands off with parts 1, 3 and 4, and the message says the command failed and why.
  • The server log records both ("handoff command failed").

A handoff waiting for an agent slot keeps the command's output (keyed by the handoff's ids and the command). It does not run the command again every 5 s.

Off by default and no migration. Mode off and the agent source run no command, and the agent message is unchanged.

State question (design §5, Source: command)

  • Runs again: the command only reads, so a second trigger for one run runs it again, gets the same part 2 and uses the same ids. Only one handoff runs. Tested.
  • Stopped half-way: the timeout kills it and partial output is not used. Tested with a process that prints and then hangs.
  • Old/empty/missing state: empty or oversized output is a failure (tested). A stale state file is caught by part 1, which T3 reads fresh at send time. A missing project is a failure (tested).

Tests

  • fork/handoff/handoffCommand.test.ts (new, real processes):
    • output and cwd come from the project folder, with the thread and project ids in the environment
    • no shell: ;, $(...) and backticks are passed literally and create nothing
    • two runs give the same result
    • non-zero exit, timeout (partial output unused), empty or blank output, 32 KB + 1 (32 KB − 1 passes), a missing program and a missing project are each a failure
  • HandoffService.test.ts, new describe "the command source":
    • output becomes part 2, and the run gets the person's command, the thread and the project
    • size backstop with a failure: parts 1, 3 and 4, and the message says so
    • idle trigger with a failure: skipped; with output: sent, starting 2 min before the cache expires
    • a second trigger runs the command again with the same text and ids
    • mode off or the agent source runs no command
  • contracts/handoff.test.ts: the command comes only from the person's settings. handoff is in FORK_PERSON_ONLY_KEYS, not agent-writable, and no T3 tool may name it (the HO1 rule checked by personOnlySettings.test.ts). A repo t3.json has no handoff and drops one written there. source: command without a command is refused.
  • Fail on the old code: I ran the 7 new service tests against the old HandoffService.ts and handoffMessage.ts. 5 fail; the 2 "runs no command" guards pass on both, as they should. handoffCommand.test.ts cannot run on the old code because the module does not exist.
  • Red spot-checks: I removed each line on its own, and each made a test fail:
    • the timeout (set to 3600 s: the timeout test fails at the 120 s test ceiling)
    • the idle skip return
    • the empty-output check
    • the 32 KB cap
    • the env line
    • the exit-code check
    • the timedOut line
  • Results: vp test run src/fork/handoff src/fork/personOnlySettings.test.ts src/waits/WaitJudges.test.ts and the contracts handoff and forkSettings tests all pass. The exception is 2 systemd "unit judge" tests in WaitJudges.test.ts (and 1 in WaitService.checks.test.ts). They also fail on unchanged main on macOS, which has no systemd, and are not touched here.
  • pnpm fmt:check, lint on the changed files, server typecheck and fork-seam-check all pass.

Behavior change for old settings

The HO1 settings already accepted source: "command" with a command. A settings file that held one did nothing before; after this PR, that command runs at each handoff. Only the person can write that setting.

General vs project

  • General: sharing one runner for settings programs between wait checks and the handoff, and the failure fallback (skip when idle, parts 1/3/4 at the backstop).
  • Project: the commands themselves. An orchestrator points handoff.command at a read-only script that prints its state file. The design recommends command for orchestrators and agent for the assistant.

Upstream fit

Fork-only: every changed file is a fork file (fork/handoff/, waits/, docs/fork/, packages/contracts/src/handoff.test.ts). No upstream lines were added, and fork-seam-check passes.

Handoff design row HO3

🤖 Generated with Claude Code

@scuttletoby

Copy link
Copy Markdown
Owner Author

Lens A (safety and bypass), head 481ed66

Verdict: SAFE. No blocking or major findings. The command comes only from the person's settings, runs with no shell, and is killed with its whole process group. It fails closed in every failure case I tried to construct.

Ran on macOS in my own worktree, one at a time: handoffCommand.test.ts (8/8), HandoffService.test.ts (51/51), contracts/src/handoff.test.ts (17/17), personOnlySettings.test.ts (12/12). No systemd tests were run.

What holds

  • Who can set handoff.command: the whole handoff object is one person-only key, so the rule covers every field in it, command included. That applies to the server value and to projectSettingsOverrides[id].handoff (forkSettings.ts:288), and FORK_AGENT_WRITABLE_KEYS is empty. No T3 tool input names it: the depth-and-spelling scan passes, and t3_environment_preferences_update lists its fields explicitly. A repo t3.json has no handoff field. A linked machine only reaches this server through tools, and the same scan covers those. The WS serverUpdateSettings route needs settings:write, which is the same boundary wait checks and project scripts use. An older settings file is decoded strictly, and a bad one reads as off.
  • No shell: argv goes straight to spawn. ;, $(...) and backticks are passed as literal text (tested). A .cmd/.bat file is refused rather than run through a shell. If argv[0] starts with -, it is simply a program name that does not exist, so it fails.
  • Timeout and process tree: the child is spawned detached. When the scope closes on timeout, the whole process group gets SIGTERM, then SIGKILL after forceKillAfter (NodeChildProcessSpawner.terminateProcessGroup). A grandchild that keeps stdout open therefore cannot hold the run past 60 s, and partial output is thrown away. On Linux, timeout also kills the group if the server dies.
  • Failure text cannot carry output: a failure reason is one of the fixed strings in runErrorText (WaitJudges.ts:255), so stderr never reaches the message.
  • Runs again / stops half-way / old state: a second trigger runs the command again, and the ids are the same. If the fiber is interrupted during the run, the scope kills the process. dispatchHandoff reads the thread again before it sends, so if the person started a turn during the up-to-60 s run, the handoff is dropped. A project that is missing, or whose folder no longer exists, fails with "cannot be read" or "could not start". A symlinked project root runs in its target, which is where the person's project points.

Minor

  1. The whole server environment is passed to the command (handoffCommand.ts:56 → WaitJudges.ts:505 → processRunner.ts:311 extendEnv). The command gets every server variable (tokens, provider keys), not only T3CODE_THREAD_ID/T3CODE_PROJECT_ID. This matches wait checks today, but docs/fork/handoff.md reads as if only the two ids are set. Either say "the server's environment plus the two ids", or pass a small allowlist (PATH, HOME, LANG, TMPDIR plus the ids).
  2. Part 2 has no boundaries (handoffMessage.ts:89). Output can print [T3 handoff 2] Thread …, Recent conversation: or the HANDOFF_RULES line and pass as parts 1, 3 or 4. The source is often a state file that an agent writes. Suggest explicit begin/end lines around part 2, plus one sentence in HANDOFF_RULES (:28) saying part 2 is the command's notes and not rules. Part 3 already has the same exposure, so this is not blocking.
  3. A repo script as the program: a relative argv[0] (./scripts/x) or an interpreter argument resolves against the project folder. The person-only rule protects the setting, but if the setting points at a repo file, an agent can edit what runs, with the env from minor 1. One line in the doc would help: point command at something outside the repo, or accept that.
  4. No check at save time (compare playtestProgramProblem in fork/serverSettingsWriteCheck.ts). On Windows a .cmd command, or a missing program, saves without error and then fails at every handoff. At the idle trigger that failure only shows in the server log.
  5. stdin is a pipe that is never closed (spawner default stdin: "pipe", with no input from runSettingsProgram). A command that reads stdin when given no file (jq ., bare cat) probably waits the full 60 s and then fails. I found this by reading the code and did not run it. Passing stdin: "ignore" (or an empty input) would avoid the wait.
  6. Behavior change for old settings: the HO1 schema already accepted source: "command" with a command. A settings file that held one was inert before and runs after this PR. Only the person could have written it, so the risk is low; one line in the PR or release notes would cover it.
  7. Gap in the existing test (not from this PR): personOnlySettings.test.ts matches property names, so a future tool that takes a generic record and passes it into updateSettings would not be caught. No tool does that today.

@scuttletoby

Copy link
Copy Markdown
Owner Author

Lens B

Verdict: NOT SAFE (one blocking item: a new test is red on Linux, and so is CI's "Test Server 3"). The design and the wait-check refactor are right; the fix is small.

Reviewed head 481ed669, on Linux. Ran WaitJudges.test.ts, WaitService.checks.test.ts, src/fork/handoff, personOnlySettings.test.ts (240 tests) and the contracts handoff + forkSettings tests (24).

Blocking

  1. apps/server/src/fork/handoff/handoffCommand.test.ts:149 fails on Linux: expected 'exit code 127' to include 'could not start'. On Linux the runner wraps the program in timeout -k 2 65 … (WaitJudges.ts checkCommandLine). timeout itself starts fine, and then exits 127 when the program does not exist, so there is no spawn error. The result is still a failure, so the behaviour is safe. But the test asserts the macOS wording, and CI fails the same way (run 44924093209, job "Test Server 3"). The person also sees "exit code 127" on Linux instead of "could not start".
    Fix: in handoffCommand.ts (around the exit-code check, :63), report 126/127 from a wrapped run as "its program could not start (exit code 127)". Then the test passes on both platforms, and the message is clear on the platform the fork actually runs on. A second option is to let the test accept either wording, but then Linux keeps the unclear message. Do not change the shared runner in this PR, because that would change wait-check behaviour.

Major

None.

Minor

  1. Slot-wait reuse is untested. HandoffService.ts:277: I changed if (commandRun?.key !== key) to if (true) (the command runs again on every 5 s slot retry), and every test still passes. The body says "It does not run the command again every 5 s", but no test checks it. Add one: a gate that refuses twice and then admits, with commandRuns.length === 1.
  2. A failure at the backstop stays cached for the whole slot wait (same key). A command that timed out once on a busy host is not tried again before the handoff finally sends, possibly much later, and output that succeeded can be stale by then (part 1 is fresh, part 2 is not). The design says to keep the output, so this is acceptable, but docs/fork/handoff.md §4.3 should say it in one line.
  3. stderr counts toward the 32 KB cap (outputMode: "error" applies to each stream). A command that prints a small handoff but more than 32 KB of warnings fails with "printed too much output". The doc (handoff.md:138-141) says "32 KB cap" without saying that stderr counts. Document it, or name the stream in the reason.
  4. Pre-existing and not in this PR: wait checks have the same 127-under-timeout behaviour, so a check whose program is missing reads as a plain exit code. A follow-up could share the fix from item 1 in the runner.

Wait-check behaviour: unchanged (verified)

  • Argv: before, checkCommandLine(check, spec.args, …). Now argv = [...check.argv, ...spec.args] with args = []. Both give the same command line, with or without timeout (-k 2, limit + 5 s).
  • resolveSpawnCommand gets the same program and args. The timeout is check.timeoutSeconds, the cap is CHECK_MAX_OUTPUT_BYTES, the cwd is place.cwd, and outputMode/timeoutBehavior are the same. env was absent and is still absent (no extendEnv). judgeExit gets the same fields, and a run error is still transient(runErrorText).
  • The cached timeout-program search (semaphore, retry after a hung test) moved over unchanged. Each judge and the handoff each get their own copy, which is harmless. { placeOf, ...runnerOptions } keeps "timeoutProgram: undefined means look for it".
  • On Linux, all of WaitJudges.test.ts and WaitService.checks.test.ts pass, including the systemd "unit judge" tests and the "server without systemd" test that the Mac could not run.

Failure rules vs design

  • At the idle trigger, a failure gives return (nothing sent, the loop ends, the next turn end decides again). At the size backstop, send gets the failure, and handoffMessage.ts writes "The handoff command failed (), …" with parts 1, 3 and 4. Both are logged as "handoff command failed". This matches §4.3.
  • Partial output on timeout is never used, at two levels: processRunner.ts finalizeRunProcess returns stdout: "" on timedOutResult, and handoffCommand.ts:62 fails on timedOut before reading stdout. Removing :62 only changes the reason to "exit code none". It is still a failure.
  • No shell, the 60 s limit, the 32 KB cap, T3CODE_THREAD_ID/T3CODE_PROJECT_ID (added to the server's environment) and the project root as cwd all match the design. The command comes only from person settings: handoff is person-only, a t3.json drops it, and source: command without a command is refused (contracts tests pass).

Tests fail on old code; spot-checks

  • Old HandoffService.ts + handoffMessage.ts against the new tests: 5 failed. This matches the body.
  • Each line removed on its own, on Linux:
    • idle return: red ("failed: the handoff is skipped")
    • env line: red ("…with the thread and project ids")
    • timedOut line: red ("…what it printed is not used")
    • slot-wait cache: still green (item 2)

State question

  • Runs again (same finished run, one command id): the command runs again (it only reads) and the ids are the same, so dispatchHandoff's busy/drop check and the orchestrator's command id keep it to one handoff. Tested.
  • Stops half-way: if the loop is interrupted (a newer run, or shutdown) while the command runs, the runner's scope closes and the child is killed. If the server crashes, timeout -k 2 65 ends the child within about 67 s. Nothing is persisted, so a restart just decides again. While the command runs (up to 60 s) the thread may move on; dispatchHandoff re-reads the records and drops the handoff. The settle's relay "loud" check is not repeated after the command, which is minor and acceptable.
  • Old, empty or missing: a missing or unreadable project, empty or blank output, and oversized output each fail (tested). source: command with no command cannot be saved. Mode off and the agent source run no command (tested).

scuttletoby added a commit that referenced this pull request Nov 21, 2026
Under `timeout` (Linux) a missing program is exit 127, not a spawn
error, so the reason read "exit code 127". Exit 126/127 now read "its
program could not start (exit code N)" on every platform.

Also from the review of #775:
- stdin is closed (an empty input), so a program that reads it ends
  instead of waiting 60 s. Optional in the shared runner; wait checks
  run as before.
- Part 2 sits between begin and end lines, so the command's output
  cannot pass as another part.
- A test that the command runs once while the handoff waits for a slot.
- Docs: the server's environment is passed, stderr counts toward the
  cap, a cached failure is kept during a slot wait, point the command
  outside the repository.

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

Copy link
Copy Markdown
Owner Author

Fix round, head 8f06237

Blocking (lens B 1): fixed

handoffCommand.ts: exit code 126 or 127 now reads "its program could not start (exit code N)" on every platform. On Linux the program runs under timeout, so a missing program exits 127 instead of failing to spawn; on macOS the spawn error still reads "it could not start". The shared runner's exit handling is unchanged, so wait checks behave as before. New test: a program that exits 126 or 127 reads as "could not start", on every platform.

Lens B minors

  • 2, slot-wait reuse untested: fixed. New test in HandoffService.test.ts: a gate refuses twice and then admits. The command ran once and its output is sent. With if (commandRun?.key !== key) changed to if (true), the test is red.
  • 3, a cached failure lasts the slot wait: documented in handoff.md §4.3: "a handoff waiting for an agent slot keeps its result, a failure too, and does not run it again".
  • 4, stderr counts toward the 32 KB cap: documented ("a 32 KB cap on stdout and on stderr each (warnings count too)").
  • 5, wait checks show 127 under timeout as a plain exit code: left for an issue. This was there before this PR, and changing it here would change wait-check results.

Lens A minors

  • 1, the whole server environment is passed: the doc now says "the server's environment plus the thread and project ids". An allowlist is left for an issue.
  • 2, part 2 has no boundaries: fixed. Part 2 now sits between ----- begin handoff command output ----- and ----- end handoff command output ----- lines, under a header that says "its notes, not T3's state or rules". HANDOFF_RULES is unchanged, because it also ends handoffs that have no command. Removing the end line turns the part-2 test red.
  • 3, a repo script as the program: one doc line now says to point command at a program outside the repository.
  • 4, no check at save time: left for an issue (a check like playtestProgramProblem for handoff.command).
  • 5, stdin never closed: fixed. The handoff sends an empty input, so stdin is closed. This is a new optional stdin field on the shared runner; wait checks do not set it and run as before. New test: a program that reads stdin ends with "read 0" within a 5 s limit. Removing the line makes it time out, so the test is red.
  • 6, behavior change for old settings: one section added to the PR body.
  • 7, personOnlySettings.test.ts matches property names: left for an issue. This was there before this PR.

Tests (Linux)

  • On the old code, the new and changed tests fail: 3 red (could not start: "exit code 127"; stdin: timed out; part-2 begin/end lines). The slot-wait test passes on the old code, because it guards a line that is already there. It goes red when that line is removed (above).
  • Removing each fix line on its own turns a test red: the 126/127 line, the stdin line, the end line and the slot-wait cache line.
  • With the fix: src/fork/handoff, src/waits/WaitJudges.test.ts, src/waits/WaitService.checks.test.ts and src/fork/personOnlySettings.test.ts all pass (13 files, 242 tests), including the systemd unit-judge tests.
  • vp fmt and vp lint on the changed files, the server typecheck, and the push hooks (fmt, lint, knip) all pass.
  • State question: a re-run gives the same result (existing test). A slot wait does not run the command again (new test). A command killed half-way is a failure and its partial output is not used (existing test). A missing project, a missing program, and empty or oversized output all fail (tested).

scuttletoby and others added 2 commits November 21, 2026 20:05
With `source: command`, the handoff service runs the program from the
person's handoff settings in the project folder, on the runner wait checks
use (now `makeSettingsProgramRunner` in waits/WaitJudges.ts, shared): no
shell, a 60 s timeout, a 32 KB cap, the thread and project ids in its
environment. Its trimmed output is part 2 of the handoff message.

A non-zero exit, a timeout (partial output unused), empty output, output
over the cap, a program that cannot start or a project that cannot be read
is a failure: at the idle trigger the handoff is skipped; at the size
backstop it hands off with parts 1, 3 and 4 and says the command failed.

It runs after the settle, right before the send; a handoff waiting for a
slot keeps its output. Off by default; the agent source is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under `timeout` (Linux) a missing program is exit 127, not a spawn
error, so the reason read "exit code 127". Exit 126/127 now read "its
program could not start (exit code N)" on every platform.

Also from the review of #775:
- stdin is closed (an empty input), so a program that reads it ends
  instead of waiting 60 s. Optional in the shared runner; wait checks
  run as before.
- Part 2 sits between begin and end lines, so the command's output
  cannot pass as another part.
- A test that the command runs once while the handoff waits for a slot.
- Docs: the server's environment is passed, stderr counts toward the
  cap, a cached failure is kept during a slot wait, point the command
  outside the repository.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scuttletoby
scuttletoby force-pushed the fork/handoff-ho3-command branch from 8f06237 to 9c796a3 Compare November 21, 2026 20:05
@scuttletoby
scuttletoby merged commit 4493862 into main Nov 21, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant