Skip to content

A Stop just after the agent asked records how Pine ended the turn: endings follow Pine's order (A10, P0) - #255

Merged
SitgesRun merged 1 commit into
stagingfrom
fix/a10-stop-ending-order
Nov 15, 2026
Merged

SitgesRun merged 1 commit into
stagingfrom
fix/a10-stop-ending-order

Conversation

@SitgesRun

Copy link
Copy Markdown
Contributor

Monkey test 2026-11-15, finding A10 (P0): Stop pressed just after the agent asked hides the question, and the Chat waits for nothing.

Root cause (pine-dev gateway log + Studio log; the two clocks agree within 15 ms)

UTC what
10:13:51.420 GET agent/turns/turn_…_173 returns after 9375 ms: Pine ended the turn needs_input
10:13:52.354 the person's Stop, 0.93 s later. The Studio still records the turn running (grace, event drain, collection), so cancelTurn sends a cancel
10:13:53.294 POST agent/cancel 202: Pine's CancelTurn closes the waiting question (broker.go cancelQuestion)
10:13:53.887 the follower records needs_input from the read it took before the cancel

Two writers recorded one turn's ending, and the stale one won. The status line said "1 waiting for you" for a question Pine no longer had.

Fix: endings follow Pine's order (compare-and-set); Stop only sends the cancel

Endings are ordered by Pine.

  • TurnView.lastEventId is the turn's place in its Task's ordered log (SDK AgentTurn.lastEventId). A cancel that closes a question moves it there (PineSandbox turns.go closesFoldedQuestion).
  • Store.recordEnding writes in one statement, only when no later ending of the current turn is recorded (the shape of HTTP If-Match or a Kubernetes resourceVersion).
  • Schema step 21 adds chats.ending_event_id, appended to MIGRATIONS.

Stop only sends the cancel (codex-rs: the turn's own path records TurnAborted).

  • PinePort.cancel returns Pine's answer, an AgentTurn from the SDK.
  • The Stop's pass records that answer in the Chat's line, then attaches as before. Nothing in the line waits on another item of the line.

One writer path. recordTurnEnding runs the compare-and-set, then the idle timeout for the state just recorded. Every writer goes through it in the Chat's line, so a Chat's idle-timeout changes never overlap.

A Stop after the recorded end is a no-op, as codex-rs app-server refuses "no active turn to interrupt" and Code-OSS's cancel of a completed request does nothing. cancelTurn cancels only a Chat recorded running.

Unchanged

  • Code-OSS's message queue and its Keep/Remove prompt.
  • doc/protocol.md 4.4, "Stop", says the new rule.

Tests

Failing-first, on the old code with only the tests (4f9fd97c), on pine-dev: npm test: tests 339, pass 335, fail 3.

  • A10's schedule (gated: the question read held, the Stop's cancel held) → "the Chat records the turn Pine ended".
  • The idle timeout after the Stop → "the stopped turn and the working idle timeout".
  • A Stop after a recorded question → "no cancel" (actual 1).

The 3 tests that failed there, at this head:

  1. The A10 schedule: Pine canceled; the Chat idle with no question waiting; ending_event_id is Pine's; the question row is kept, then "You stopped this turn".
  2. The idle timeout ends on the working value.
  3. A Stop once the question is recorded asks Pine nothing.

New tests that did not run on the old code:
4. The same ending read twice is recorded once and said by both readers.
5. Store unit tests: equal or smaller ids are refused, a larger id is accepted, and only for the current turn; a schema-20 store upgrades with the column null.

Existing fixtures corrected:

  • chats.test.ts (2 places) and restartFollower.test.ts forge a running Chat over a turn whose ending was already recorded. They now also clear ending_event_id, so the state they simulate is whole: nothing of the turn recorded yet. Without that, the compare-and-set correctly refused their re-read of the same ending.
  • projectEntry.test.ts and the migration probe in protocolFlow.test.ts no longer hard-code schema 20/21.
  • No assertion was weakened.

Gate, pine-dev on this exact head 6497b7e8a9f075869c1db4c872543fca6551f28a: npm test: tests 342, pass 341, fail 0, skipped 1; render smoke 30 of 30; release safety tests passed. npx tsc -p . is clean.

Codex (read-only):

  • The plan was reviewed in 9 rounds; the last answer was "OK to code".
  • The code was reviewed in 3 rounds. The first two found 3 minor issues (a test that missed its regression, the Stop pass not logging its words, one more forged fixture), all fixed. The last answer was "OK to merge".

Left as is (stated)

…dings follow Pine's order, and a Stop after the recorded end is a no-op

Monkey test A10 (2026-11-15, turn _173): Pine ended the turn needs_input at 10:13:51.420 (pine-dev gateway log). The person pressed Stop 0.93 s later, while the Studio still recorded the turn running (grace, event drain, collection). The Stop's cancel reached Pine at 10:13:53.294 and closed the question. The follower then recorded needs_input from the read it had taken before the cancel. The Chat said "1 waiting for you" for a question Pine no longer had.

- A turn's ending is recorded by one compare-and-set on Pine's order: TurnView.lastEventId, the turn's place in its Task's ordered log. A cancel that closes a question moves it there (PineSandbox turns.go closesFoldedQuestion, broker.go cancelQuestion). Store.recordEnding writes only when no later ending of the current turn is recorded, so an earlier read never replaces a later ending. Schema step 21 adds chats.ending_event_id (store.ts MIGRATIONS).
- The Stop only sends the cancel. PinePort.cancel returns the turn Pine answers with (SDK agent.cancel returns an AgentTurn). Its pass records that ending in the Chat's line, then follows as before.
- Every ending goes through recordTurnEnding, in the Chat's line: the compare-and-set, then the idle timeout for the state just recorded. Two of a Chat's idle-timeout changes never overlap.
- cancelTurn cancels only a turn the Chat records as running. A Stop after the recorded end does nothing on Pine, as codex-rs app-server refuses "no active turn to interrupt" and Code-OSS cancels nothing once a request is complete.

Left as it was:
- A cancel whose answer is lost records nothing from the Stop (the lost-response family of #193).
- The stopped response still cannot show a question recorded before the Stop (Code-OSS drops progress after a cancel).
- Code-OSS keeps queued messages after a Stop and drops them on Reload (#253).

Signed-off-by: SitgesRun <169425442+SitgesRun@users.noreply.github.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.

1 participant