Skip to content

fix(handoff): #973 minors: random-token fence, late continuation row, bun/deno refused - #1021

Closed
scuttletoby wants to merge 2 commits into
mainfrom
fix/973-handoff-minors
Closed

scuttletoby wants to merge 2 commits into
mainfrom
fix/973-handoff-minors

Conversation

@scuttletoby

@scuttletoby scuttletoby commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

All five follow-ups from the #958 round-2 lenses, each fixed at the root. Every change is in fork files (apps/server/src/fork/handoff/, docs/fork/handoff.md), so no seam lines are added or announced.

What changed, per item

  1. A fence with a random token instead of a look-alike list (handoffMessage.ts). Part 2 (a command's output or the old session's notes) now goes in exactly as written, between ----- begin … <token> ----- and ----- end … <token> -----. The token is 128 random bits drawn for that message (randomFenceToken, Web Crypto), and the line before the fence says the notes end only at the end line with that token. The notes are written before the token exists, so no line in them can end the fence early, however it is disguised: Greek capitals, small capitals, Armenian, ǝ, Cherokee, an HTML tag, U+001C..1E, or a guessed token. If the notes happen to contain the token, a new one is drawn. CONFUSABLES, LOOKS_LIKE_OURS and neutraliseNotes are removed.
  2. Continuation row id checked against upstream's real writer. New handoffContinuationRow.test.ts runs upstream's ProviderRuntimeRecovery.reconcile("startup") on a write turn that a restart cut short. It checks that the only provider-runtime.continue row written has id continuationEffectId(writeRun) (now exported from HandoffService.ts). The harness tests now use that helper instead of a copied string. Mutation check: renaming the id fails this test while every HandoffService test stays green.
  3. No time limit while a continuation row is pending (HandoffService.finishWrite). While upstream's continuation row is pending/running, the handoff waits for it to settle, however late a busy effect worker gets to it. The one-minute-past-due limit now applies only when no row is written yet (a retry row upstream writes right after the failure). If waiting on the row fails, the row is read again after 5 s. A row that cannot be read counts as owed, never as "no row": it is read again every 5 s (round 1).
  4. Test for parsed.fallback. New test: after a restart, a failed write turn on a thread below the size backstop sends the fallback only when it is marked :fallback. Mutation check: removing the parsed.fallback || term turns it red.
  5. bun, bunx and deno are refused by the handoff command save check (PROJECT_CONFIG_RUNTIME), whatever script they are given. They read bunfig.toml (preload) or deno.json (import map) from the project folder before the script runs. They are removed from the interpreter list, and deno run/bun run no longer need special handling. Round 1 adds R: R is refused whatever it is given, and Rscript unless --vanilla comes among its options (before its script or -e), as R sources .Rprofile and reads .Renviron from its working folder first.

Failing first

Every new test was run against the unfixed code first:

  • handoffMessage.test.ts: red (fenceLine is not a function: the fence did not exist).
  • HandoffService.test.ts: 4 red. The busy-worker test fails at the 10-minute check (expected 1 to be +0: the chain was judged after one minute). The fence tests are the other 3.
  • handoffCommandSaveCheck.test.ts: the bun/deno test is red (the command was accepted).
  • handoffContinuationRow.test.ts: red until the export.

Items 2 and 4 add tests only, with no behaviour change; their mutation checks are above. After the fixes, each changed file passes on its own (17, 1, 10, 15 and 84 tests), and vp run check:changed passes (format, lint, knip, typecheck, tests). fork-seam-check.cjs origin/main HEAD also passes.

State question

A handoff that runs again, stops half-way, or meets an old outbox row or an empty thread is still sent at most once, and never without its notes when they exist:

  • Runs again: the command and message ids are still fixed per source run, so dispatchHandoff drops a second send. A rebuilt text draws a new token; only the token differs, and the existing "second trigger" test now compares the texts with the token taken out. A failed handoff's retry resends the stored text, token included.
  • Stops half-way: a restart during the wait loses the in-memory loop. Upstream's continuation run then reports itself (restartContinuationOfRunId), and a new loop picks the chain up (continuedWrite → carriesOn), with the same ids. A pending row no longer runs into a time limit, so the continuation's notes are not dropped in favour of the fallback.
  • Old outbox row: a settled row (succeeded, failed or cancelled) with no continuation run means upstream owes nothing, so the chain is judged at once. A run that already exists wins over its row, because the row is read before the runs. A row pruned after 7 days counts as "no row": only the retry-delay check applies. A row that cannot be read is owed, never "no row": it is read again every 5 s, so the fallback never goes out without the notes; a failed wait on a pending row reads it again after 5 s too.
  • Empty thread / empty notes: an empty or marker-only reply and empty command output already fail the source, so the fence never wraps empty notes. A thread with no runs sends nothing, as before.
  • Trade-off: a row that upstream left pending forever, or an outbox that can never be read, would keep this one handoff waiting, with nothing sent. That fits at most once. A newer turn end replaces the wait, as before.

General vs project

  • General (every server with handoff on): the fenced part 2 (in every handoff message from now on), the wait on a pending or unreadable continuation row, and bun/bunx/deno, R, and Rscript without --vanilla refused in a new handoff command. Such a command saved earlier is not checked again.
  • Project (this fork's upkeep): the upstream-writer test, which guards against upstream renaming a string it does not export; the :fallback coverage; and the docs/fork/handoff.md changes.

Round 1 (code lens)

Finding Fix Test (red first)
Major: Secret scan red, gitleaks generic-api-key on the test constant at handoffMessage.test.ts:12 Renamed to FENCE_ID, value built at run time (Array.from over 0..f twice); no ignore or allow-list entry handoffMessage.test.ts 17 pass. Local gitleaks 8.30.1 (checksum checked) with main's .gitleaks.toml/.gitleaksignore: the new commit and the files as they stand have no leaks. The repo's scan script itself cannot run on this Mac (it needs bash 4). The PR's Secret scan stays red: it reads every commit of main..head, and 4bb4067a33 still adds the line. Clearing it needs the owner's call (rewrite that commit, an exact-value allow on main, or a new branch); none was done here.
Minor 1: the busy-worker test did not pin the removed one-minute limit (HandoffService.ts:422-423) Test only: one thread event (runUpdated(write)) after the first 10 minutes, then the toBe(0) and sent checks With giveUpMs = after.dueMs + stopWaitMs back: red (expected 1 to be +0); green with the fix
Minor 2: an unreadable outbox row counted as "no row" (HandoffService.ts:333-343, :451-456) continuationOf returns owed (wait: "unread") for a failed get; the loop reads it again after CONTINUATION_RECHECK (5 s) with no limit. The owed kinds are now row, retry, unread New "an outbox row that cannot be read is owed": red on the old code (judged at once), and red when the unread wait has no 5 s re-read (times out). New "a failed wait on the outbox row reads it again after 5 s": red (times out) when that path waits forever
Minor 3: Rscript reads .Rprofile/.Renviron from the project folder (handoffCommandSaveCheck.ts:19, docs/fork/handoff.md:166) R refused outright; Rscript refused unless --vanilla comes before its script or -e (after them it is the script's argument); with --vanilla its script is checked as before. Docs updated New "refuses R, and Rscript without --vanilla": red on the old code (Rscript accepted)

Runs after round 1, one file at a time: HandoffService.test.ts 86 pass, handoffCommandSaveCheck.test.ts 11 pass, handoffMessage.test.ts 17 pass; vp run check:changed passes (format, lint, knip, graphql, typecheck, 6 test files). The macOS-only __CF_USER_TEXT_ENCODING failure in handoffCommand.test.ts did not come up (that file is unchanged and was not run on its own).

Fixes #973

🤖 Generated with Claude Code

…nuation row; refuse bun and deno (#973)

- Part 2 (a command's output or the old session's notes) goes as written between begin and end
  lines carrying a 128-bit token drawn for the message, replacing the look-alike list and quoting.
- The restart continuation row's id is checked against upstream's real restart writer.
- A written continuation row is waited for until upstream settles it, however late a busy effect
  worker runs it; only an unwritten retry row is given up a minute past its due time.
- A test covers the `:fallback` mark deciding the fallback for a write turn found after a restart.
- The handoff command save check refuses bun, bunx and deno: they read bunfig.toml / deno.json
  from the project folder before any script.

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

Copy link
Copy Markdown
Owner Author

lens: code · reviewer: code lens agent · read at: 4bb4067
covered: #973 (body; no comments), #958 for context, the PR body and the full diff (handoffMessage.ts, HandoffService.ts, handoffCommandSaveCheck.ts, the 5 test files, docs/fork/handoff.md), plus upstream's EffectOutbox.ts (claim, retry, awaitSettled, process-loss reconcile), the 4 upstream writers of the continuation row id, ProviderRuntimeRecoveryService.ts and the event handler that starts and replaces the loops.
checks: ran vp test run src/fork/handoff/ on a detached worktree of this head: 266 pass, 1 fails (handoffCommand.test.ts, macOS only, see below). Then one undo per fix, each run on its own: removing the token redraw turns its test red; removing parsed.fallback || turns the :fallback test red; letting bun/deno through turns the save-check test red; renaming the row id turns handoffContinuationRow.test.ts red while all 84 HandoffService tests stay green. Putting back the one-minute limit for a written row (giveUpMs = after.dueMs + stopWaitMs) leaves every handoff test green (minor 1). Making a failed awaitSettled wait forever instead of rechecking after 5 s also leaves all 84 green (minor 2). Two local probe tests, not pushed: (a) the busy-worker test with one thread event after 10 minutes is green on this head and red with the one-minute limit back; (b) the same test with one failing outbox.get plus a thread event ends finishWrite while the row is still pending. Also checked: node 26 ignores a node.config.json in its working folder; how programName handles a full path, .exe/.cmd, upper case, env (with options, by name and by full path) and npx.
state: Runs again: the ids are fixed per source run and upstream drops a repeated commandId (command receipts), so a rebuilt text with a new token is still sent at most once. In probe (b) the notes were dispatched twice with the same ids, and the receipt dropped the second. Stops half-way: a restart loses the in-memory wait. Upstream requeues a provider-runtime.continue row on process loss (REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS), and the continuation run then reports itself (continuedWrite), so a new loop carries the chain on with the same ids. Old row: a settled row means nothing is owed and the chain is judged at once; a row pruned after 7 days counts as no row. An unreadable row also counts as no row, which is minor 2. Empty thread or empty notes: unchanged, the fence never wraps empty notes. Waiting with no limit: retry() puts the row back to pending without signalling it as settled, so the wait goes on through upstream's retries. A newer turn end does free it: FiberMap.run without onlyIfMissing interrupts the waiting fiber, and the chain ids stay in carrying as before this PR. In practice, though, upstream's claim keeps later effects of the thread behind a pending row (EffectOutbox.ts:354-366), so a person's new turn usually starts only once the row settles. The handoff then waits exactly as long as the thread does.
blocker: none
major: CI is red: Secret scan fails on this head (all 3 runs). gitleaks generic-api-key flags const TOKEN = "0123456789abcdef0123456789abcdef" at apps/server/src/fork/handoff/handoffMessage.test.ts:12, a false positive on a test constant. Small and in scope: rename the constant so its name does not read as a key (for example FENCE_ID) and build the value (for example "0123456789abcdef".repeat(2)), then check that the scan passes. Do not add an ignore entry.
minor: 1. The busy-worker test does not pin the giveUpMs change (apps/server/src/fork/handoff/HandoffService.ts:422-423). With the one-minute limit back for a written row, every test stays green. The test passes only because no thread event arrives during its 20 minutes, and the restructured wait has no timer. Any event after the minute (a request, a run update) would judge the chain without the continuation's notes. Small and in scope: after the first TestClock.adjust(10 * MINUTE_MS) in HandoffService.test.ts (the "busy effect worker" test), offer one thread event (runUpdated(write)) and keep the toBe(0) check. Probe (a) shows it is red without the fix.
minor: 2. An unreadable row counts as no row (HandoffService.ts:333-343, continuationOf maps a failed outbox.get to Option.none). The new "a failed wait reads the row again after 5 s" (HandoffService.ts:451-456) therefore judges the chain at once when the reread also fails. Take a write turn that failed before its prompt, with its retry row pending, at the size backstop: the fallback goes out without the notes, and the continuation's later send is dropped as already sent. By reading the code; probe (b) shows the early judgment for a cut-short write turn. The 5 s recheck path has no test (see checks). Small and in scope: have continuationOf return owed for an unread row, so the loop rereads after CONTINUATION_RECHECK, and add a test with one failing get.
minor: 3. Rscript belongs with bun and deno. R sources .Rprofile and reads .Renviron from its working folder (the project folder) before the script, unless it gets --vanilla or --no-init-file. It is still a followed interpreter (apps/server/src/fork/handoff/handoffCommandSaveCheck.ts:19, docs/fork/handoff.md:166-167). Small and in scope: add rscript to PROJECT_CONFIG_RUNTIME (handoffCommandSaveCheck.ts:42) with its test case and docs line, or accept it only with --vanilla.
notes (no finding, dropped with reason):

  • The fence is sound as structure. The token comes from Web Crypto (crypto.getRandomValues, 16 bytes, so 128 bits) after the notes are fixed, and it is drawn again if the notes contain it. No line of the notes, whatever its spelling, line breaks (lone CR, U+2028, U+001C..1E) or script, can carry it, and part 2 ends only at the real end line. What remains depends on the model: a fake end line with a wrong token now goes in unquoted, where the exact fixed line used to get > . Dropped, because the fence is what Handoff: follow-up minors from #958 round-2 lenses #973 asked for, the intro line names the token, and matching more text would bring the look-alike list back.
  • Item 2: the new test drives upstream's real writer (ProviderRuntimeRecovery.make and reconcile("startup"), with only the stores mocked) and goes red when the id is renamed. It covers one of upstream's 4 writers of the id. It does not cover Orchestrator.retryContinuationStart (Orchestrator.ts:3401) or ProviderSessionManager.ts:1279. Dropped, because upstream's own readers use the same literal (Orchestrator.ts:14692, :16973; ProviderRuntimeRecoveryService.ts:1023), so a rename in one writer would break upstream itself.
  • Item 5 bypasses: a full path, .exe/.cmd/.bat, any case, env with options (by full path, tested) and env bun by name are all refused, and npx/pnpx are refused as runners. A renamed or hyphen-versioned copy (bun-canary), sh -c 'bun …' and wrappers like nice or timeout still pass, which handoff.md already lists as not caught.
  • A bun or deno command saved before this PR is not checked again. That is safe as far as this PR goes: nothing crashes and nothing is sent twice, and its exposure is the same as before. Refusing a stored command at run time would turn a saved setting into a failed source at handoff time, and the guard is documented as against mistakes, with the setting person-only. Dropped.
  • Outside this PR: handoffCommand.test.ts "gets a short list of the server's environment" fails on macOS, because the OS adds __CF_USER_TEXT_ENCODING after env -i. The file is untouched here and Linux CI passes. Dropped for this PR.
    CI at read: Secret scan red (3 runs). Test Server 3, 4 and 7 green; Test Server 1, 2, 5, 6 and 8 still running; fork-ci-result not posted yet.

🤖 Generated with Claude Code

…out --vanilla; pin the busy-worker wait (#973)

Round 1 of review on the #973 follow-ups:
- handoffMessage.test.ts: the fence id stand-in is named FENCE_ID and built at run time, so the
  secret scan no longer reads it as a key. No ignore entry.
- HandoffService: an outbox row that cannot be read is owed, not "no row": the loop reads it again
  after 5 s, with no limit, so the fallback never goes out without the continuation's notes.
- The busy-worker test offers a thread event during its wait, so the one-minute limit coming back
  turns it red. New tests: an unreadable row, then the 5 s re-read; a failed wait on the row read
  again after 5 s with no thread event.
- The handoff command save check refuses R, and Rscript unless --vanilla comes among its options:
  R sources .Rprofile and reads .Renviron from the project folder before the script.

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

Copy link
Copy Markdown
Owner Author

Replaced by #1025 (the same change as one commit; an earlier commit here trips the per-commit secret scan, and fixing that would need a force-push).

🤖 Generated with Claude Code

scuttletoby added a commit that referenced this pull request Nov 22, 2026
… bun/deno refused (#1025)

* fix(handoff): #973 minors: random-token fence, late continuation row, bun/deno refused

One commit with the full change from #1021 (rounds 0 and 1). The earlier
branch had a test value in one commit that the per-commit secret scan
flags; this branch carries only the final files, which scan clean.

Fixes #973

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

* docs(handoff): say an unreadable continuation row is read again, with its trade-off

From the narrow check on #1025.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

Handoff: follow-up minors from #958 round-2 lenses

1 participant