Skip to content

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

Merged
scuttletoby merged 3 commits into
mainfrom
fix/973-handoff-minors-v2
Nov 22, 2026
Merged

scuttletoby merged 3 commits into
mainfrom
fix/973-handoff-minors-v2

Conversation

@scuttletoby

@scuttletoby scuttletoby commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Replaces #1021: the same change as one commit. The old branch had a test value in one commit that the per-commit secret scan flags (the value is a test placeholder, not a secret). Rewriting that branch would need a force-push, so the final files moved here; gitleaks git over origin/main..HEAD finds no leaks.

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

After the narrow check (SAFE TO MERGE, comment 7085795419)

  • Fixed (39ed9fb): docs/fork/handoff.md now says an unreadable continuation row is read again every 5 s, with its trade-off.
  • Left: the 5-second warning while the outbox stays unreadable (log noise only; it shows the fault). Rterm, Rcmd, Rscript-4.3 and Rscript --args --vanilla: other names are covered by the doc's existing "not named above, or under another name" rule (the check refuses known names; it is a safety net, not the wall). No test pins the -e check: the code is right and the narrow check probed it by hand.

🤖 Generated with Claude Code

… 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>
@scuttletoby

Copy link
Copy Markdown
Owner Author

not a lens: narrow check after fix round 1
Verdict: SAFE TO MERGE

Read at 3ca7da66ad821cbc6d403ae51483f858fbe8dd1b, on a detached temp worktree (removed after). Scope: the move and the four round-1 fixes only, against the code lens on #1021 (read at 4bb4067a33) and the round-1 table in this PR's body. Each undo below was run on its own file, one at a time, then reverted.

0. The move: OK. git diff 6aa9cac461 3ca7da66 touches the same 15 files that main changed between the two merge bases (4b131b7404 → c0de9c5018), and nothing under apps/server/src/fork/handoff/ or docs/fork/handoff.md. A stronger check: git merge-tree --write-tree origin/main 6aa9cac461 gives tree fed1960249, the same tree as 3ca7da66. origin/main..3ca7da66 is 1 commit. gitleaks is not installed on this machine, so I did not run it locally. The PR's own Secret scan ran gitleaks on that range with main's .gitleaks.toml/.gitleaksignore: "1 commits scanned … no leaks found", and the commit-object scan found no leaks either.

1. Secret scan fix: OK. handoffMessage.test.ts:13 builds FENCE_ID at run time, and it equals the old constant (0123456789abcdef twice). The tests still use it only as an injected stand-in. The token is still drawn per message by randomFenceToken (handoffMessage.ts:36, 16 bytes from Web Crypto). Undo checks: with a fixed token, "draws 128 random bits each time" (handoffMessage.test.ts:74) goes red (expected 1 to be 50). With the token left out of fenceLine (handoffMessage.ts:32-33), 2 tests go red ("draws the token again", "names its source").

2. Busy-worker test: OK. HandoffService.test.ts:2066 sends one runUpdated(write) after 10 minutes, then checks toBe(0) and sent empty. With the one-minute limit put back for a written row (HandoffService.ts:424, giveUpMs = dueMs + stopWaitMs for row and retry), exactly this test goes red (expected 1 to be +0), 85 pass. It is green on head.

3. Unreadable outbox row is owed: OK.

  • continuationOf (HandoffService.ts:330-348) maps a failed get to "unread" → { kind: "owed", wait: "unread" }. A continuation run that already exists still wins, because the runs are checked first (:347).
  • giveUpMs is infinite for row and unread, and only retry keeps the minute (:424). The unread wait is Deferred.await(wake) with a 5 s timeoutOrElse (:453-459), so there is no time limit and the row is read again every 5 s. A failed awaitSettled sleeps 5 s and then reads again (:451).
  • Never sent without the notes: the chain is judged only after none, or after retry passes its minute. Neither path is reached while the row is unread.
  • Never sent twice: the fixed ids and dispatchHandoff are unchanged. The new test checks exactly one handoffMessageId, and the notes are in part 2.
  • Runs again, stops half-way, old row, empty thread: each lands in the same states as before (re-entry by continuedWrite/carriesOn; a settled row → none). Nothing in this round touches them.
  • No block and no leak: the wait runs in the per-thread FiberMap fiber (:996-1007), not in the event stream, so other threads are not held up. raceFirst and timeoutOrElse interrupt the losing side, and each turn of the loop replaces the thread's single wakes entry. A newer turn end interrupts the fiber as before.
  • The trade-off is stated honestly: the PR body says it ("an outbox that can never be read, would keep this one handoff waiting, with nothing sent"), and so does the code comment at :420-421.
  • Undo checks (each red, 85 pass): with the old Option.none mapping, "an outbox row that cannot be read is owed" (HandoffService.test.ts:2089) goes red (expected 1 to be +0, judged at once). With no 5 s re-read for unread, the same test times out. With a failed awaitSettled waiting forever (Effect.never), "a failed wait on the outbox row reads it again after 5 s" (:2124) times out.

4. R and Rscript: OK. readsRProfile (handoffCommandSaveCheck.ts:50-58, used at :241) runs after programName (:61-65: last path part, lower case, .exe/.cmd/.bat and a trailing version dropped). A local probe test (not pushed) refused Rscript.exe, RSCRIPT, Rscript4.3, R64 --vanilla, R.exe -f, Rscript --verbose <script> --vanilla, Rscript -e x --vanilla, Rscript --vanilla=1 <script>, env R_PROFILE_USER=x Rscript …, env -u X Rscript …, env -- Rscript …, and env -a Rscript R --vanilla …. It accepted Rscript --vanilla <script>, env -i A=1 Rscript --vanilla <script>, and env -a R Rscript --vanilla <script> (argv0 does not change the program file). docs/fork/handoff.md:167 and :182-184 say exactly this: R whatever it is given, and Rscript only with --vanilla before its script or -e. Undo check: letting R through turns "refuses R, and Rscript without --vanilla" (handoffCommandSaveCheck.test.ts:227) red.

Runs on head, one file at a time: HandoffService.test.ts 86 pass, handoffCommandSaveCheck.test.ts 11 pass, handoffMessage.test.ts 17 pass, handoffContinuationRow.test.ts 1 pass.

Non-blocking (none needs a new round):

  • docs/fork/handoff.md:237-246 describes the wait for a written row and for a retry, but not the unread row (owed, read again every 5 s, no limit) or its trade-off. Only the PR body and the code comment carry it. A sentence there would keep the doc complete.
  • HandoffService.ts:332: while the outbox stays unreadable, this warning is logged every 5 s for as long as the wait lasts. That is noise, not a leak.
  • Not refused, all within the doc's "interpreters … not named above, or under another name" (docs/fork/handoff.md:187-188):
    • the Windows front ends Rterm/Rterm.exe (which also source .Rprofile) and Rcmd;
    • Rscript-4.3;
    • Rscript --args --vanilla <script>. By R's argument rules, --args would make R ignore the --vanilla that follows it. This one is unverified, because R is not installed here.
  • The arg === "-e" term in readsRProfile (:55) is not pinned: removing it leaves all 11 tests green, because -e cat(1) --vanilla is refused anyway at cat(1). The code is right.

CI at read (3ca7da66): fork-ci-result SUCCESS. All Fork tests jobs (Test 1-2, Test Server 1-8, Lint, Build and Typecheck, kit tests), Secret scan (all runs), and No database schema changes are green.

🤖 Generated with Claude Code

scuttletoby and others added 2 commits November 22, 2026 21:08
… its trade-off

From the narrow check on #1025.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scuttletoby
scuttletoby merged commit 6853e86 into main Nov 22, 2026
38 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.

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

1 participant