Skip to content

fix(sdk): run a turn again when its activity lock was lost - #382

Open
ChrisKrawczyk wants to merge 5 commits into
mainfrom
fix/turn-cut-lost-lock
Open

ChrisKrawczyk wants to merge 5 commits into
mainfrom
fix/turn-cut-lost-lock

Conversation

@ChrisKrawczyk

@ChrisKrawczyk ChrisKrawczyk commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

A turn whose activity lock is lost outside a worker drain could drop the user's prompt. 0.8.16 (#367) stopped saving a turn that the worker's own drain cuts. A lock lost any other way still saved the aborted attempt's cancelled result under the turn's key. For example, the lock expires while the worker is busy, and duroxide hands the activity to another attempt (microsoft/duroxide#56). The retry runs with the same turnKey, so it adopted that result through already-committed recovery, and the orchestration ended the turn without running it.

The hazard has been present since v0.6.0, the initial versioned-snapshot release, for snapshot-enabled turns. The fix does not recover prompts that were already dropped. An affected session shows a runTurn activity that completed with result cancelled (turn.ts 1609-1611).

No orchestration version, no Workflow controller version and no CMS migration: the change is inside the runTurn activity and the worker's local cleanup. Upgrade: stop every worker before you deploy (CHANGELOG). An older worker would still commit a cancelled attempt that a new worker then has to work around.

Fix

  • A cancelled attempt is never committed. runTurnCommit leaves the turn sentinel dirty, and the worker records session.snapshot_unpublished { reason: "cancelled" }. The message no longer promises a retry.
  • A stored cancelled result is never adopted, whether through already-committed recovery or through the commit's same-turnKey adoption. Any release from v0.6.0 to 0.8.17 can leave one behind; the retry then runs the turn on the saved state.
    • If an older build's cancelled commit wins the CAS, the attempt returns its own result as unpublished (foreign_cancelled_winner). It gets no control-cursor credit and no carried-steer credit, so its steers are kept.
  • Re-read the cancellation before each step. The activity checks it again before every send, steer, corrective prompt and recovery. A cancelled activity starts no run, sends nothing and never resets the stored session state.
  • A cancellation marks the session cut on this worker (markTurnCut). A cancel latched after the attempt's commit marks nothing.
    • While the worker drains, neither the idle sweep nor the shutdown release reclaims a cut session.
    • On a live worker, the eviction sweep reclaims it: it releases the workspace (cancelling its background tasks), destroys the handle, deletes the dir without publishing it, then clears the mark.
    • A retry on this worker clears the mark only after its preamble has resolved the session from the store; a failed probe keeps the mark. destroySession clears it.
  • The legacy publish guard. A legacy dehydrate, the safety checkpoint and the eviction sweep never publish a dir that holds an uncommitted turn (a dirty sentinel) or a cut turn. _legacyPublishBlocker runs under the session lock before any archive, and the store keeps its last commit. fix(sdk): never reuse a dead Copilot runtime (#329) #376 adds the same guard for a dirty sentinel. Whichever PR merges second takes the union ("dirty_turn" | "turn_cut"), and fix(sdk): never reuse a dead Copilot runtime (#329) #376's destroyDeadlineMs on invalidateWarmSession.

Formal model

TurnCut in docs/formal/session-orchestration/, TLC 2.19:

  • tc1, bound to f9b958c1 (CORRESPONDENCE-f9b958c1-tc1.md): 15 of 15 as expected.

  • tc2, bound to 63024c6c (CORRESPONDENCE-63024c6c-tc2.md), models what the review found. It covers:

    • the legacy publish guard;
    • the sweep's skip only while the worker drains;
    • when the mark is cleared;
    • a terminal cancel;
    • a shutdown that begins after an ordinary lost-lock cut;
    • a retry beaten by an older build's cancelled commit.

    It has 26 cases: 11 faults, 6 comparisons, 4 holds and 5 witnesses. All 26 are as expected, with 0 PARTIAL and 0 VOID.

    • Mode "head" reproduces the pre-review branch.
    • Mode "old" is v0.6.0 to 0.8.15.
    • The tc1 witness wit.tc-lingering-mark is retired.
  • The whole session-orchestration folder re-ran with 0 mismatches.

  • After the rebase onto 0.8.18 (1f6af957, which changes no modelled code), tc1 and tc2 re-ran with the same results; tc2's PASS and liveness cases found the same distinct-state counts (tc1's first run recorded none).

Bounds and what is not covered are in CORRESPONDENCE-63024c6c-tc2.md. #376 also adds cases to this folder. Whichever merges second re-tables it and re-runs RuntimeLoss and TurnCut on the combined code, which neither PR's TLC run covers (review C-3).

Tests

Each test fails without its fix:

  • turn-control-run-turn.test.js, runTurnV4: a turn whose activity lock is lost. These drive session-proxy.ts with a real snapshot store:
    • the cut attempt commits nothing, and its retry on another worker runs the turn from the last commit;
    • a lock lost before the send starts no run;
    • an older build's cancelled CAS winner leaves the turn unpublished and uncredited, and keeps its carried steers;
    • a cancellation latched after the commit marks nothing;
    • a retry whose store read fails keeps the cut mark.
  • turn-lifecycle.test.js (M4 cases):
    • a cancelled attempt is not committed;
    • a stored cancelled result is never adopted;
    • a commit that finds an older build's cancelled attempt returns its own result, unpublished.
  • turn-control-live.test.js: a cancelled activity sends nothing (no initial send, no steer, no corrective prompt).
  • session-proxy-events.test.js: a refused dehydrate of a cut turn records snapshot_unpublished, not dehydrated.
  • session-workspace-release.test.mjs:
    • during a drain, a cut session is neither released nor swept;
    • on a live worker, the sweep reclaims a cut session without publishing it;
    • dehydrate, checkpoint and the sweep never publish a dirty or cut dir;
    • a session that ends clears its mark.

Multi-model review

Three models (Claude Opus 5.5, GPT-6.2 Sol and Gemini 4 Argon) reviewed this PR as five specialists in a threaded debate, and a synthesis kept only findings grounded in the diff. The consensus was 2 must-fix and 6 should-fix findings, plus 2 trade-offs and 7 to consider.

The must-fix and should-fix findings are fixed in 63024c6c and 102c701d:

  • MF-1: the cut mark disabled the sweep's workspace backstop on a live worker.
  • MF-2: a retry cleared the mark before its probe.
  • SF-1: a terminal cancel leaked its mark.
  • SF-2: a foreign cancelled CAS winner was reported as published.
  • SF-3: there was no re-read of the cancellation before a send.
  • SF-5: there was no activity-level test.
  • SF-6: the model could not reach a shutdown after a lost-lock cut.

SF-4 is fixed for the live-session-lost recovery and the transcript reset. The other recovery branches and the tests of those two guards are follow-ups (#393).

The considered items C-2, C-4 and C-5 are done, as is the part of C-1 that fits this PR.

Both trade-offs keep this PR's conservative choice, with the alternative recorded in #394:

  • T-1: a lock-lost attempt's finished body is always discarded, not committed.
  • T-2: a foreign cancelled winner is reported truthfully as unpublished, not recommitted as v3.

Other follow-ups:

Validation

All runs were on Linux (Node 24.22.1, PostgreSQL 16), one suite at a time, at 102c701d (this head's code; the last commit changes only docs/formal/), rebased on 0.8.18 (1f6af957).

  • CI steps: lint, privacy (0 findings), deploy-scripts (402 passed, 43 skipped of 445), ci-scripts 152/152, sdk-api 63/63, SDK unit 3083/3083, campaign 57/57 and 3/3, app 99/99, 312 of 315 (3 skipped), 2134/2134 and 3/3.
  • Full run-tests.sh: 441 of 441 files; 4540 passed, 0 failed and 26 skipped of 4566.
  • TLC: see "Formal model" above. check-packet.sh and tlc_cases.py check --all pass.

chkraw and others added 5 commits November 24, 2026 18:00
0.8.16 (#367) stopped saving a turn the worker's drain cuts. A lock lost
outside a drain still saved the aborted attempt's `cancelled` result under
the turn's key; the retry (same turnKey) adopted it through
already-committed recovery and the orchestration ended the turn without
running it, dropping the prompt.

- runTurnCommit skips the commit of a cancelled attempt and leaves the
  sentinel dirty; the worker records
  `session.snapshot_unpublished { reason: "cancelled" }`.
- Already-committed recovery and the commit's same-turnKey adoption never
  adopt a stored cancelled result (left by an earlier release): the retry
  runs the turn on the saved state.
- The attempt marks the session cut (markTurnCut) when it latches the
  cancellation, so neither the idle sweep nor the shutdown release reclaims
  its unsaved dir or workspace before the retry; the next attempt of the
  session on this worker clears the mark (clearTurnCut). If the retry runs
  on another worker, the mark and the local dir stay until this worker
  restarts.

No CMS migration and no session orchestration or Workflow controller
version change.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
Model a runTurn attempt cut by a drain or a lost activity lock and its
retry under the same turnKey: the cut attempt is never saved, the retry
never adopts a stored cancelled result (from 0.8.15/0.8.17 too) and runs
from the last committed snapshot, and a graceful shutdown neither
releases the cut session's workspace nor reclaims its dir.

Batch tc1, bound to 2078582: 15 of 15 as expected (5 PASS, 10 expected
violations, 0 unexpected, 0 PARTIAL, 0 VOID), TLC 2.19.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
Review of #382 (MF-1, MF-2, SF-1, SF-2, SF-3, SF-5, C-2):

- A legacy dehydrate, the safety checkpoint and the eviction sweep never
  publish a session whose local files hold an uncommitted turn (a dirty
  turn sentinel) or a cut turn (the cut mark): _legacyPublishBlocker,
  checked under the session lock before any archive. The store keeps its
  last commit; dehydrate and checkpoint return { unpublished } and the
  dehydrate activity records session.snapshot_unpublished.
- The sweeps skip a cut session only while the worker drains. On a live
  worker the eviction sweep reclaims it: it releases the workspace
  (cancelling its background tasks), destroys the handle, deletes the dir
  unpublished and then clears the mark.
- The retry on this worker clears the mark once its preamble resolved the
  session from the store; a failed probe keeps it. destroySession clears
  it, and a cancel latched after the attempt's commit marks nothing.
- When an older build's cancelled commit wins the CAS, the attempt
  returns its own result unpublished (foreign_cancelled_winner), with no
  control-cursor or carried-steer credit.
- The activity re-reads its cancellation before every send, steer,
  corrective prompt and recovery: a cancelled activity starts no run,
  sends nothing and never resets the stored session state.
- The cancelled unpublished message no longer promises a retry.
- CHANGELOG: the hazard dates from v0.6.0; dropped prompts are not
  recovered; side effects may repeat; stop every worker before an upgrade.
  snapshot-store-wins.md gets back its lost-lock caveat.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
Bind TurnCut to 2ab2a85e, the fixes from the review of #382, and model
what the review found: the legacy publish guard (a dirty turn sentinel
or the cut mark) on the dehydrate, the checkpoint and the sweep; the
sweep's skip only while the worker drains, with a live worker's sweep
releasing a cut session and deleting its dir; the mark cleared only after
the retry's preamble and when the session is destroyed; a terminal
cancel; a shutdown that begins after an ordinary lost-lock cut; and a
retry beaten by an older build's cancelled commit (unpublished). A new
mode "head" models the branch before its review (2078582); mode "old"
is v0.6.0 to 0.8.15. A cut and a shutdown are no longer fair (C-5).

26 cases (11 faults, 6 comparisons, 5 witnesses); the tc1 witness
wit.tc-lingering-mark is retired to cases-retired.json. TLC 2.19: 26 of
26 as expected. CORRESPONDENCE-2ab2a85e-tc2.md maps the model to the
code; both READMEs and SHA256SUMS are updated.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
The rebase onto 0.8.18 (1f6af95) moved the bound session-manager.ts lines
by four after main's custom-agents change; the modelled paths are
unchanged, and so are the other bound source files and the cited tests.
Re-read every line, bind tc1 to f9b958c and tc2 to 63024c6 (mode
"main" stays at ac86a39), point the model and case notes at 35a6865,
and re-run both batches: tc2 26 of 26 and tc1 15 of 15 as expected,
with the same PASS and liveness state counts as the first tc2 run.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants