Skip to content

fix(acp,cursor): a turn's cached provider thread is settled, so late updates cannot mark it busy - #909

Merged
scuttletoby merged 3 commits into
mainfrom
fix/846-settled-provider-thread
Nov 22, 2026
Merged

scuttletoby merged 3 commits into
mainfrom
fix/846-settled-provider-thread

Conversation

@scuttletoby

@scuttletoby scuttletoby commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Fixes #846

Cause (a product race, not a test race)

When a turn ends, the ACP adapter caches the provider thread snapshot with status active (packages/provider-acp/src/server/adapter.ts:8654, in finalizeTurn) and clears the active turn. Kiro sends two session_info_update notifications after the cancelled session/prompt response (entries 26 and 27 of fixtures/turn_interrupt/kiro_transcript.ndjson). The usage/session-info handler re-sends the cached snapshot, still active, and marks it sessionScoped because no turn is running (adapter.ts:5813). The server writes a session-scoped update straight to the projection, with no run guard (apps/server/src/orchestration-v2/ProviderSessionManager.ts:3026).

The run's own final write sets the thread to idle (apps/server/src/orchestration-v2/RunExecutionService.ts:378, finalProviderThreadStatus). Which of the two writes lands last depends on fiber timing. When the late active write lands last, the ended thread stays active for good, and the test's status === "idle" check (fixtures/turn_interrupt/codex_output.ts:47) fails now and then.

The Cursor adapter has the same pattern. It caches active at a turn's end (packages/provider-cursor/src/server/adapter.ts:2614), and background-shell updates after the turn re-send that snapshot session-scoped.

Fix

The cached snapshot is settled at the turn's end, like the server's final write: idle, while a broken thread keeps error in ACP. The fix is one override line, plus a comment, after upstream's status line in each adapter. No upstream line changes. The other adapters (Claude, Codex, OpenCode, Pi, Muse) set active only when a turn starts, so they do not have this pattern. The ACP adapter also serves Kimi, Grok, fx and the ACP registry agents, so they get the fix too. The test was right; it is unchanged.

Each adapter's existing test now also checks the status of the snapshot that is sent again:

  • AcpAdapterV2.test.ts, the Kimi late usage_update test
  • provider-cursor adapter.test.ts, the background-shell test

The four files are upstream files, so the lines are seams. They are announced in #907 (step 1) and moved to added here (step 2).

Verification

Forced race (proof of the cause). The race is rare on its own, so I forced the order seen in CI, as a local experiment that is not committed: a 100 ms delay on Kiro's session_info_update so it arrives after the turn has closed, and a 200 ms pause before the scenario reads its final projections. With a trace on both writes:

  • Unfixed: 4 of 4 runs fail with expected 'active' to equal 'idle', the same error as CI. The trace shows the run's final idle write, then the late session-scoped active write about 90 ms later.
  • Fixed: 4 of 4 runs pass. The same late write now carries idle.

Without the forced delay, Kiro's late update arrives while the turn is still open, so it goes through the run and lands before the final idle. That is why the test passes almost always.

Unit tests (deterministic). Without the two adapter lines, the new checks fail every time with expected 'active' to equal 'idle': the AcpAdapterV2 late-usage test and the Cursor background-shell test. With them:

  • provider-cursor adapter.test.ts: 33/33
  • AcpAdapterV2.test.ts: 152/152
  • Replay suites for Kiro, Cursor, Grok, fx and the ACP registry: 5 files, 69/69
  • Typecheck is clean for provider-acp, provider-cursor and apps/server.

Loops (the Kiro replay file, 30 completed runs each, on a loaded machine).

Runs Race failures (active) Other
Before (unfixed) 30 0 1 extra run: timed out at load ~100 before any test ran
After (fix) 30 0 1 extra run: timed out at load ~110 before any test ran

The natural rate is too low to show in 30 runs: 0/30 before, 0/30 after. CI hit it rarely. The forced-race runs above are the before/after evidence: 4/4 failures before and 0/4 after.

CI. Every test job passes. The only red check is the seam check (Fork lines in upstream files), and the two aggregate checks that follow from it. CI judges seams by main's list (--config-ref is main), and main does not list these lines until #907 merges. Locally, the seam check passes against #907's list.

Merge order

This is stacked on #907 (base fork-seams/846-announce) so its seam check is judged by the list that announces these lines. Merge #907 first, then retarget this PR to main before the base branch is deleted. If #907 is squash-merged, updating this branch from main conflicts in .github/fork-seams.json: keep this branch's side, with the lines under added and no announced.

General vs project

  • General: the fix is in the shared ACP and Cursor adapters, and serves every agent that uses them.
  • Project: none.

🤖 Generated with Claude Code

scuttletoby and others added 2 commits November 22, 2026 01:01
List only (step 1 of "Adding a seam line"). Fixes the race behind the
flaky Kiro turn_interrupt replay test (#846): at a turn's end the ACP and
Cursor adapters cache the provider thread as active, and usage or shell
updates that arrive after the turn re-send that snapshot session-scoped,
which the server writes without a run guard. When that write lands after
the run's final idle write, the ended thread stays active.

The fix adds one override line (and a comment) after upstream's status
line in each adapter, so the cached snapshot is idle like the server's
final write, and one check in each adapter's existing test. No upstream
line changes, so no lost-line count is needed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…updates cannot mark it busy

Fixes #846. At a turn's end the ACP adapter (Kiro, Kimi, Grok, fx and the
ACP registry agents) and the Cursor adapter cache the provider thread as
active. Usage or session info that Kiro sends after the cancelled prompt
(and Cursor's background-shell updates) re-send that cached snapshot as a
session-scoped update, which the server writes without a run guard. When
that write lands after the run's final idle write, the ended thread stays
active for good; the turn_interrupt Kiro replay test caught it now and
then.

The cached snapshot is now settled (idle, or error for a broken thread)
like the server's final write. One override line after upstream's status
line in each adapter, announced in .github/fork-seams.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@scuttletoby
scuttletoby force-pushed the fork-seams/846-announce branch from a0cd901 to 6e58254 Compare November 22, 2026 01:01
@scuttletoby
scuttletoby force-pushed the fix/846-settled-provider-thread branch from 11d0ddd to 6b4c249 Compare November 22, 2026 01:02
@scuttletoby

Copy link
Copy Markdown
Owner Author

lens: code · reviewer: claude-opus-5-5 · read at: 6b4c249

blocker: none
major: none
minor: Pre-existing lost update, now visible in status. The ACP usage handler reads the cached snapshot and writes it back (packages/provider-acp/src/server/adapter.ts:5788-5807); Cursor's publishBackgroundShells does the same (packages/provider-cursor/src/server/adapter.ts:1170-1176). If that runs interleaved with the next turn's start (ACP sets activeTurn at 9489 and the active cache at 9514; Cursor 3082 and 3093), it can write the old idle copy over the new active one. Before this change the same race only lost usage fields. All the steps are synchronous Ref reads and writes, so this needs a scheduler yield at exactly that point: very rare, and the run's own final write corrects it when that turn ends. No change needed here.
minor: Crash window. The in-run turn-end snapshot is now idle one event before turn.terminal. If the server stops between those two writes, RestartContinuation.ts:207 sees a running run with a non-active provider thread and does not continue it. Before, it saw active and would continue a turn the agent had already ended. The new outcome is the more truthful one, and the window is a single event. No change needed.
minor: In Cursor, the input.threadDisposition === "broken" guard is dead today: no caller passes broken (adapter.ts:2554). It is harmless and keeps the line the same shape as ACP's. If upstream starts passing broken, Cursor's cache would keep active instead of error, but the server's final write still sets error.
minor: Test gap (optional). No test pins that an update during the next turn still carries active, which is the path a regression of the turn-start refresh would break. That refresh is at ACP adapter.ts:9514 and Cursor adapter.ts:3093.

Cause analysis: correct.

  • ACP finalizeTurn cached and emitted active at turn end, then cleared the turn (adapter.ts:8651-8668).
  • The usage/session-info handler re-sends that cache, marked sessionScoped when no turn runs (adapter.ts:5788-5813).
  • ProviderSessionManager.ts:3011-3044 ingests a session-scoped provider_thread.updated straight into the projection, with no run ordering.
  • RunExecutionService.ts:378 and :839 write the final idle/error. Whichever write lands last wins.
  • Cursor is the same: the cache in publishedProviderThread is re-sent session-scoped between turns (adapter.ts:1168-1178).

Fix:

  • The override gives exactly the server's finalProviderThreadStatus: error for broken and idle otherwise in ACP, idle in Cursor.
  • It matches what upstream's own OpenCode v2 adapter already does at turn end (packages/provider-opencode/src/server/v2/adapter.ts:2445).

Paths that should mark a thread active:

  • A new turn: both adapters set the cache to active and emit it at turn start (ACP :9503-9518, Cursor :3093-3098). Later usage or shell updates then carry active through the new run. Not affected.
  • Background shells: busy-ness between turns is carried by pendingBackgroundTasks (Cursor's withBackgroundShells), not by status. The server already settles a stale active to idle (Orchestrator.ts:12917, :13025), and CheckpointRestoreSafety.ts:148 also checks the task list. Not affected.
  • Resumed sessions and restarts: the caches start empty, and ensure/resume build fresh snapshots (:9941-9957, :10717). Not affected.

Other adapters:

  • Only ACP, Cursor and OpenCode v2 emit session-scoped thread updates. OpenCode v2 already settles at turn end.
  • Claude, Codex, Muse, Pi and OpenCode v1 never emit them, so the unguarded write path does not apply to them.
  • Kiro, Grok, fx and the ACP registry run on the ACP adapter, so they get this fix.

Tests:

  • With the fix: provider-cursor adapter.test.ts 33/33, and the AcpAdapterV2.test.ts usage tests 5/5.
  • With only the two adapter lines removed, both new checks fail with expected 'active' to equal 'idle'.
  • Seam check against the step 1 list passes.

State question:

  • Update during the next turn: carries active, which is correct; see the first minor for the one rare interleave.
  • After a crash: see the second minor.
  • After a restart: caches are rebuilt, nothing stale is re-sent.
  • Runs again: the override is idempotent.

@scuttletoby
scuttletoby changed the base branch from fork-seams/846-announce to main November 22, 2026 02:51
# Conflicts:
#	.github/fork-seams.json
@scuttletoby
scuttletoby merged commit 0170f62 into main Nov 22, 2026
40 checks passed
scuttletoby added a commit that referenced this pull request Nov 28, 2026
…updates cannot mark it busy (#909)

* fork seams: announce the settled provider thread lines (#846, step 1)

List only (step 1 of "Adding a seam line"). Fixes the race behind the
flaky Kiro turn_interrupt replay test (#846): at a turn's end the ACP and
Cursor adapters cache the provider thread as active, and usage or shell
updates that arrive after the turn re-send that snapshot session-scoped,
which the server writes without a run guard. When that write lands after
the run's final idle write, the ended thread stays active.

The fix adds one override line (and a comment) after upstream's status
line in each adapter, so the cached snapshot is idle like the server's
final write, and one check in each adapter's existing test. No upstream
line changes, so no lost-line count is needed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(acp,cursor): a turn's cached provider thread is settled, so late updates cannot mark it busy

Fixes #846. At a turn's end the ACP adapter (Kiro, Kimi, Grok, fx and the
ACP registry agents) and the Cursor adapter cache the provider thread as
active. Usage or session info that Kiro sends after the cancelled prompt
(and Cursor's background-shell updates) re-send that cached snapshot as a
session-scoped update, which the server writes without a run guard. When
that write lands after the run's final idle write, the ended thread stays
active for good; the turn_interrupt Kiro replay test caught it now and
then.

The cached snapshot is now settled (idle, or error for a broken thread)
like the server's final write. One override line after upstream's status
line in each adapter, announced in .github/fork-seams.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
scuttletoby added a commit that referenced this pull request Nov 28, 2026
…updates cannot mark it busy (#909)

* fork seams: announce the settled provider thread lines (#846, step 1)

List only (step 1 of "Adding a seam line"). Fixes the race behind the
flaky Kiro turn_interrupt replay test (#846): at a turn's end the ACP and
Cursor adapters cache the provider thread as active, and usage or shell
updates that arrive after the turn re-send that snapshot session-scoped,
which the server writes without a run guard. When that write lands after
the run's final idle write, the ended thread stays active.

The fix adds one override line (and a comment) after upstream's status
line in each adapter, so the cached snapshot is idle like the server's
final write, and one check in each adapter's existing test. No upstream
line changes, so no lost-line count is needed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(acp,cursor): a turn's cached provider thread is settled, so late updates cannot mark it busy

Fixes #846. At a turn's end the ACP adapter (Kiro, Kimi, Grok, fx and the
ACP registry agents) and the Cursor adapter cache the provider thread as
active. Usage or session info that Kiro sends after the cancelled prompt
(and Cursor's background-shell updates) re-send that cached snapshot as a
session-scoped update, which the server writes without a run guard. When
that write lands after the run's final idle write, the ended thread stays
active for good; the turn_interrupt Kiro replay test caught it now and
then.

The cached snapshot is now settled (idle, or error for a broken thread)
like the server's final write. One override line after upstream's status
line in each adapter, announced in .github/fork-seams.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
scuttletoby added a commit that referenced this pull request Nov 29, 2026
… file (#1187)

* fork-ci: enforce review findings, rounds and critical gate files (#740)

* fork-ci: enforce review findings, rounds and critical gate files

The head check now enforces the owner's review rules from the base's
`review` block:

- findings (#99): in enforce, a lens's newest report with a `blocker:`
  or `major:` line fails the head until a later report of that lens has
  none, or a person's override comment for the head is newer.
- rounds (#100): the round is the number of different full shas the lens
  comments read (or the highest rN marker). From rounds.approach_at the
  approach lens is required; past rounds.max it stops with "a person
  decides" until an override covers every lens.
- critical files (#82): the fork's own queue config makes workflow,
  queue and lens files critical with two lenses.

It fails closed: rounds and findings that cannot be judged fail the
head in either mode. Adds the fork's own config with a review block in
enforce mode and four general lens prompts, and documents how a project
opts in through its own config.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fork ci: read lens findings both ways, count fix rounds, person-only overrides (#740 fix)

Findings (#99): headings, bold, tables, dash and emoji labels count, after
NFKC, zero-width removal and o/0/M look-alikes; clean lines (none found,
none!, none (only minors)) and fenced code do not. A report on the head
with no findings section fails that lens instead of passing as clean.
NOT SAFE on the title or a verdict line is a blocker.

Rounds (#100): a round is a different change of the pull request's own
diff (from its merge base), so a queue update that only merges the base
in adds none. Edited, hidden and short-sha reads still count; a round
marker in a lens name is a floor. Past the cap the message says a
person's override alone is enough, also for a missing approach lens.

Overrides (#78): review.override_authors lists the people whose override
counts; empty or left out means none is accepted. The fork config stays
in shadow and leaves the list out, and the lens prompts use the exact
header and findings section the gate reads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fork ci: a findings section needs both labels, read finding: lines, key rounds on content (#740 final fix)

- A report has a findings section only with a blocker label and a major
  label (or findings: none, or finding: lines): blocker: none plus a major
  in a form not read (F1 (major): x, Severity: major, One major: x ...)
  now fails closed instead of passing as clean.
- finding: <blocker|major|minor> lines are read: a minor-only report
  passes, a finding: major line fails whatever none lines are there.
- Clean: major: none — two minors below, Major: (none), none plus an emoji.
  A labelled recap (Major 1: fixed) stays an open finding; the lens
  prompts say to write recaps without a label.
- HTML entities, backslash escapes and the ratio sign are folded.
- Linear time on long runs of spaces (squeezed in _plain; the table rule
  pattern matched on the stripped line): a 200k-character line under 2 s.
- Round key: index lines and the whole hunk header dropped, --binary
  kept, so a base edit to another line of a file the PR changes is no
  new round.
- A listed override login whose comment has another author association
  is warned about in the check output; a read-commit fetch has a timeout.
- Tests for the hunk-header strip, the fence that ends a table and the
  override_authors cap.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>

* fork seams: announce the model policy lines on t3_thread_launch and t3_thread_configure (#731) (#746)

* fork seams: announce the model policy lines on t3_thread_launch and t3_thread_configure (#731)

Part of #731. List only (step 1 of "Adding a seam line"): the #731 code change
adds two upstream lines to each of two MCP handler files, and changes the
fork's own model policy contract file, which the list must name as a fork file.

- project/handlers.ts: an agent's t3_thread_launch is checked against the model
  policy once its model is known, before the slot gate.
- thread/handlers.ts: an agent's t3_thread_configure that changes a thread's
  model is checked before it is dispatched.
- forkFiles: packages/contracts/src/modelPolicy* (added by #268, a fork file
  upstream does not have; like mcpProfile*, playtest* and reviewRules*).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fork seams: announce the model policy lines for a settings file that fails to load (#731)

A settings.json that does not parse makes upstream run on its defaults, whose model
policy is off. The fork reads the policy from the file on its own and fails closed, which
needs two lines in apps/server/src/serverSettings.ts: the import of fork/modelPolicyFile.ts
and the modelPolicy value in the fallback settings. Announced here so #747 may add them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>

* Scheduled tasks: webhook wait counts unreadable retries, logs the skip's cause (#763) (#765)

- A waiting webhook delivery whose task stays unreadable is skipped after
  60 failed retries in a row (5 minutes), not 5 minutes of wall-clock time,
  so a host that sleeps between two retries cannot end it early. One good
  read still resets the count.
- The skip logs a warning with the cause of the last failed read.
- savedSince's SELECT and row mapping are built from one column map, typed
  against RunCopy's fields, so a field cannot be compared but not read.
- A row found deleted by the compare's read ends the delivery at once as
  deleted, not as "could not be read". The reason text takes its retry
  count and minutes from the constants; the long doc comment is wrapped.

Co-authored-by: Claude Opus 5.5 <[email protected]>

* heavy: minors from the #760 reviews (#761) (#764)

- The fallback note folder (~/.local/state/heavy) is used only in a home the
  effective user owns ($HOME, else the passwd home). As root with a user's
  HOME (sudo -E) it never makes a root-owned folder there. With no usable home
  it refuses instead of writing into the working dir.
- stop_scope waits the grace time only after a step that sent a signal.
- The leftover sweep removes old empty docker-label notes of scopes that are
  gone (not listed when it began, minAgeSeconds old). A live run's note stays.
- Tests: the hygiene check finds every untimed read form; the real tests wait
  for the guard's rename and use a temp HOME. README/docstring wording fixed.

Co-authored-by: Claude Opus 5.5 <[email protected]>

* Model policy: check agents' model picks on the server, mark picks outside it in Settings (#731) (#747)

* fork seams: announce the model policy lines on t3_thread_launch and t3_thread_configure (#731)

Part of #731. List only (step 1 of "Adding a seam line"): the #731 code change
adds two upstream lines to each of two MCP handler files, and changes the
fork's own model policy contract file, which the list must name as a fork file.

- project/handlers.ts: an agent's t3_thread_launch is checked against the model
  policy once its model is known, before the slot gate.
- thread/handl