fix(agent): preserve unfinished turn context for follow-ups#1739
fix(agent): preserve unfinished turn context for follow-ups#1739RealKai42 wants to merge 4 commits into
Conversation
🦋 Changeset detectedLatest commit: f4913cd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cb6e8ea72c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| this.agent.context.appendSystemReminder(content, { | ||
| kind: 'injection', | ||
| variant: TURN_OUTCOME_REMINDER_VARIANT, | ||
| }); |
There was a problem hiding this comment.
Do not leave turn reminders behind after undo
When a user runs /undo after a failed or cancelled turn, this records the turn-specific reminder as origin.kind === 'injection'; the undo code skips injections (packages/agent-core/src/agent/context/index.ts:195, and v2 does the same in contextOps.ts), so undo removes the actual user prompt while leaving this stale <system-reminder> in model context. The next request is then told to continue an undone request, which can make the model repeat or reason about work the user explicitly removed; make this turn_outcome reminder undoable with its prompt or purge it during undo.
Useful? React with 👍 / 👎.
cb6e8ea to
f7c9200
Compare
f7c9200 to
d132588
Compare
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d132588072
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // recorded call gets a result" invariant), close the exchange now so the | ||
| // context state machine cannot strand later messages in deferredMessages. | ||
| this.closeAbandonedToolExchange(ended); | ||
| this.appendTurnOutcomeReminder(ended, signal.reason); |
There was a problem hiding this comment.
Keep cancellation reminders before follow-up prompts
In the v1 cancel path, abortTurn() clears activeTurn as soon as /cancel is received, so prompt() can start and append the next user message before the aborted runOneTurn reaches this new append. If the user hits stop and immediately submits “Continue” (the cancel RPC returns before turn.ended), this reminder can land after the follow-up or during that next request, so the model does not see the failed-turn boundary before the follow-up and the reminder refers to the wrong “next user message”. Append/insert the reminder before accepting a new turn, or anchor it to the cancelled turn rather than late cleanup.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 995863ac93
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| this.reminders.appendSystemReminder(content, { | ||
| kind: 'injection', | ||
| variant: TURN_OUTCOME_REMINDER_VARIANT, |
There was a problem hiding this comment.
Handle v2 turn outcome reminders in transcript undo
Fresh evidence in this revision is that v2 now persists the new turn_outcome variant here. In v2 sessions where a failed or cancelled turn is later undone, the transcript reducer used by /messages still skips every origin.kind === 'injection' during context.undo (packages/agent-core-v2/src/agent/contextMemory/contextTranscript.ts:178), so log replay leaves this hidden reminder in the transcript after its user prompt is removed; mirror the v1 transcript exception for this variant when adding the persisted reminder.
Useful? React with 👍 / 👎.
Related Issue
No linked issue. This addresses maintainer feedback about follow-up prompts losing the boundary of a failed or interrupted turn.
Problem
When a turn ended with a terminal provider error or a user interruption, the event stream recorded the failure but the model conversation did not. A short follow-up such as “Continue” therefore appeared adjacent to the original request without any explanation that work had stopped, making it easy for the model to misread the user's intent or repeat operations that had already taken effect.
What changed
injectionso live TUI events, resumed transcripts, exports, and undo continue to hide it like existing system reminders.No documentation update is needed because this does not add or change a command, configuration option, or visible TUI contract.
Validation
turn.endedor stall queued turns; multi-tool dangling exchanges close before the reminder; strict projection and wire replay remain valid.git diff --checkand the public-identifier audit passed.Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.