Repository navigation
ref(core): Reconcile a Turn in one place and store its failure - #4020
Merged
Merged
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ 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.
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
approved these changes
Nov 23, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

stepCommittedandturnEndedwere called as a pair on four paths inadvanceTurn, 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 inadvanceTurn, including the claim miss, calls onlyreconcileTurn.A failed Turn now stores why it failed on its
junior_turnsrow, in main's failure labels:failure_code(where it stopped),failure_reason(the provider error kind of a failed model request) andfailure_event_id(the Sentry event of the failure). A failed model step builds main'sProviderErrorfrom the failed reply and reports it to Sentry asagent.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/andsrc/turn-steps/, onlycore/turn/state.tsmay insert, update or deletejuniorTurnsrows.via @thomasjiangcy.
--
View Session [Sentry]