Skip to content

fix(agent): preserve unfinished turn context for follow-ups#1739

Open
RealKai42 wants to merge 4 commits into
mainfrom
kaiyi/analyze-continue-context-harness
Open

fix(agent): preserve unfinished turn context for follow-ups#1739
RealKai42 wants to merge 4 commits into
mainfrom
kaiyi/analyze-continue-context-harness

Conversation

@RealKai42

@RealKai42 RealKai42 commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

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

  • Append a compact model-facing system reminder after terminal failures and cancellations in both agent engines.
  • Preserve the original request, reminder, and follow-up as distinct content blocks, while exposing public provider categories and status codes for known provider failures.
  • Mark the reminder as an internal injection so live TUI events, resumed transcripts, exports, and undo continue to hide it like existing system reminders.
  • Keep terminal cleanup non-throwing even when an error object cannot be rendered.
  • Close dangling v2 tool exchanges with persisted error results and a step boundary before appending the reminder, so strict providers, compaction, resume, and later prompts retain a valid sequence.
  • Do not add a reminder when an in-turn retry succeeds.

No documentation update is needed because this does not add or change a command, configuration option, or visible TUI contract.

Validation

  • Agent engine v1 full suite: 3,809 passed, 3 expected failures, 30 skipped, 1 todo.
  • Agent engine v2 full suite: 3,479 passed.
  • CLI/TUI full suite: 2,298 passed, 2 skipped.
  • Adversarial coverage verifies hostile cancellation diagnostics cannot suppress turn.ended or stall queued turns; multi-tool dangling exchanges close before the reminder; strict projection and wire replay remain valid.
  • TUI replay coverage verifies a synthetic tool failure renders once while the outcome reminder remains hidden.
  • Real Anthropic and Gemini adapter captures preserve strict role alternation and separate original-request, reminder, and follow-up blocks.
  • Agent v1, agent v2, and CLI typechecks passed.
  • Agent v2 domain-layer lint passed; full-repository lint completed with zero errors.
  • Package builds, production CLI/Web build, and bundle smoke passed.
  • git diff --check and the public-identifier audit passed.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f4913cd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

@RealKai42
RealKai42 marked this pull request as ready for review July 15, 2026 07:53

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 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".

Comment on lines +687 to +690
this.agent.context.appendSystemReminder(content, {
kind: 'injection',
variant: TURN_OUTCOME_REMINDER_VARIANT,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@RealKai42
RealKai42 force-pushed the kaiyi/analyze-continue-context-harness branch from cb6e8ea to f7c9200 Compare July 15, 2026 08:13
@RealKai42
RealKai42 force-pushed the kaiyi/analyze-continue-context-harness branch from f7c9200 to d132588 Compare July 15, 2026 08:16
@pkg-pr-new

pkg-pr-new Bot commented Jul 15, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@995863a
npx https://pkg.pr.new/@moonshot-ai/kimi-code@995863a

commit: 995863a

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 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".

Comment on lines +429 to +431
this.reminders.appendSystemReminder(content, {
kind: 'injection',
variant: TURN_OUTCOME_REMINDER_VARIANT,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

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