Skip to content

ref(core): Reconcile a Turn in one place and store its failure - #4020

Merged
thomasjiangcy merged 4 commits into
core-nextfrom
core/turn-lifecycle
Nov 23, 2026
Merged

thomasjiangcy merged 4 commits into
core-nextfrom
core/turn-lifecycle

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

stepCommitted and turnEnded were called as a pair on four paths in advanceTurn, and forgetting one half of the pair is how replies got lost before. They are now one function, reconcileTurn. It reads the stored Turn and brings delivery and the queue in line with it: it publishes the delivery message, moves a failed Turn's late inputs, and publishes the Conversation's open Turn if it is queued. That last rule also covers the next step after a "continue" commit. What it does depends only on the stored state, so it is safe to repeat. Every path in advanceTurn, including the claim miss, calls only reconcileTurn.

A failed Turn now stores why it failed on its junior_turns row, in main's failure labels: failure_code (where it stopped), failure_reason (the provider error kind of a failed model request) and failure_event_id (the Sentry event of the failure). A failed model step builds main's ProviderError from the failed reply and reports it to Sentry as agent.turn.provider_error, the way main reports a provider error. A Turn that runs out of attempts stores only its code.

A new ast-grep rule keeps every Turn state write in one file: in src/core/ and src/turn-steps/, only core/turn/state.ts may insert, update or delete juniorTurns rows.

via @thomasjiangcy.

--

View Session [Sentry]

The core Turn engine writes main's turn_started, turn_completed and
turn_failed events in core/turn/state.ts, in the same transaction as
the state change they record, so main's readers work unchanged.

stepCommitted and turnEnded become one reconcileTurn. It reads the
stored Turn, then rings delivery, moves a failed Turn's late inputs
and publishes the Conversation's queued Turn. Every path in
advanceTurn calls only it, so no path can forget one half of a pair.

An ast-grep rule keeps juniorTurns writes and lifecycle events in
state.ts for core and turn-steps code.

Co-Authored-By: Thomas Jiang <20501235+thomasjiangcy@users.noreply.github.com>
@vercel

vercel Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 22, 2026 11:56pm UTC

Request Review

@thomasjiangcy
thomasjiangcy marked this pull request as ready for review November 22, 2026 15:00
@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 22, 2026
A failed model step builds main's ProviderError from the failed reply,
captures it to Sentry as agent.turn.provider_error, and writes its kind
as failureReason and the Sentry event id as eventId on turn_failed, the
way main does for a provider error. attempts_exhausted keeps
agent_run_failed with no reason.

Co-Authored-By: Thomas Jiang <20501235+thomasjiangcy@users.noreply.github.com>
Core no longer writes main's turn_started, turn_completed and
turn_failed events. Core never reads them, and nothing reads them
until an ingress feeds core.

failTurn stores the failure code, the provider error kind and the
Sentry event id on junior_turns, in the new failure_code,
failure_reason and failure_event_id columns of 0137. failTurn and
cancelTurn go back to a single update with no Conversation lock,
because they no longer append events.

The ast-grep rule now only keeps juniorTurns writes in state.ts.

Co-Authored-By: Thomas Jiang <20501235+thomasjiangcy@users.noreply.github.com>
@sentry-junior sentry-junior Bot changed the title ref(core): Keep the Turn lifecycle in one place ref(core): Reconcile a Turn in one place and store its failure Nov 22, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 27b887a. Configure here.

Comment thread packages/junior/src/core/turn/state.ts Resolved
scheduleInput reads the open Turn under the Conversation lock. Without
the lock, cancelTurn could end that Turn after the read, and the input
then joined a cancelled Turn that never runs. Under the lock, an input
placed after the cancel goes to a new Turn.
@thomasjiangcy
thomasjiangcy merged commit 2a9be2f into core-next Nov 23, 2026
29 checks passed
@thomasjiangcy
thomasjiangcy deleted the core/turn-lifecycle branch November 23, 2026 00:03

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 80901dd6 Deployed Nov 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant