kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

[Bug] Resume after an interrupted tool call can drop the newest user message when compaction runs before the next step

Open vastStage opened this issue 2 weeks ago • 1 comments

What version of Kimi Code is running?

2.6.3 (source main snapshot, 2026-11-18). Confirmed by reading the restore and compaction folds.

Which open platform/subscription were you using?

None involved (code-level audit).

Which model were you using?

None involved (code-level audit).

What platform is your computer?

Windows 11 x64.

What issue are you seeing?

If a session is interrupted while a tool call is still open, then resumed, and the user sends a message that is followed by an automatic compaction before the next step, resuming the session again loses that user message.

The replay/restore fold defers messages while a tool call is awaiting its result:

  • packages/agent-core-v2/src/agent/contextMemory/loopEventFold.ts:156-161: appendMessage() pushes messages into deferred while pending (open tool calls) is non-empty; they are only folded later by closePending() / flushDeferred().
  • A resumed session adds the synthetic interrupted tool result through the normal context.append_message path (packages/agent-core-v2/src/agent/loop/journal.ts -> appendContextMessage), so that record is deferred as well.
  • The user's next prompt is recorded as another context.append_message and is also deferred.
  • Before the first step of the new turn, onWillBeginStep can trigger automatic compaction, which writes context.apply_compaction before the next step.begin.
  • On the following restore, applyContextCompactionRecord (packages/agent-core-v2/src/agent/contextMemory/contextOps.ts:18-24) rebuilds the context from the fold state only. The deferred messages are not in state, so the compaction shape (compactionHandoff.ts) cannot keep them; resetFold([...result.messages]) then discards the old fold's deferred array.
  • The user message is still in the wire (it was recorded before the compaction record), but no later fold re-applies it. If the summary mentions it at all, it is only a paraphrase.

The live session keeps the message verbatim (historySafeToCompact explicitly allows appended real user input and buildContextCompactionShape keeps it in the tail), so live and resumed states diverge.

What steps can reproduce the bug?

  1. Start a turn that calls a blocking tool (for example TaskOutput or a long Bash command).
  2. Force-interrupt the process while the tool call is still open (close the terminal or kill it).
  3. Resume the session.
  4. Send a new user message before any step runs, with a context large enough that automatic compaction triggers in onWillBeginStep.
  5. Let compaction finish, then close and resume the session again.
  6. Inspect the restored context / replay: the message from step 4 is missing.

The existing resume tests only cover an interrupted tool exchange without an intervening compaction; the deferral tests only cover the live fold. There is no "resume + compaction + resume" case.

What is the expected behavior?

Messages that were appended while a tool call was open must be part of the input to compaction (or flushed before the compaction record is applied), so the newest user intent survives a resume the same way it survives the live session.

Additional information

Suggested fix: before applyContextCompactionRecord builds the shape during restore, flush deferred messages into state (or otherwise pass them into the compaction input), for example by settling pending tool calls the same way foldSettle does when the restore ends. A regression test can replay a wire with: tool.call with no result -> context.append_message (synthetic result) -> context.append_message (user prompt) -> context.apply_compaction, then assert that the prompt is present in the rebuilt messages.

Contribution

  • [x] I am willing to submit a PR for this bug fix myself (please wait for maintainer approval in this issue first)

vastStage avatar Nov 18 '26 18:11 vastStage

I reproduced the loss directly through restoreLoopJournal with the record sequence described above (temporary test, reverted afterwards):

const records = [
  { type: 'context.append_loop_event', event: { type: 'step.begin', uuid: 's1' } },
  { type: 'context.append_loop_event', event: { type: 'content.part', stepUuid: 's1', part: { type: 'text', text: 'calling tool' } } },
  { type: 'context.append_loop_event', event: { type: 'tool.call', stepUuid: 's1', toolCallId: 'call_1', name: 'Bash', args: {} } },
  { type: 'context.append_message', message: { role: 'tool', content: [{ type: 'text', text: 'interrupted' }], toolCallId: 'call_1', isError: true } },
  { type: 'context.append_message', message: { role: 'user', content: [{ type: 'text', text: 'NEW USER MESSAGE' }], origin: { kind: 'user' } } },
  { type: 'context.apply_compaction', summary: 'summary', contextSummary: 'summary', compactedCount: 4, tokensBefore: 100, tokensAfter: 10, keptUserMessageCount: 0, appendedInjectionCount: 0 },
];
const { input } = restoreLoopJournal(records);

Observed restored messages:

[{"role":"user","text":"summary"},{"role":"user","text":"<system-reminder>\nContext compaction is complete — continue the work that was in progress when it began.\n</system-reminder>"}]

NEW USER MESSAGE is absent. Both context.append_message records before the compaction were deferred because the tool call was still pending, and applyContextCompactionRecord built the shape from the fold state without them. The same wire replayed without the context.apply_compaction record keeps the message in the deferred queue and flushes it at foldSettle, which is why the loss only appears when compaction is recorded before the next step.begin.

I will add this wire sequence as a regression test in the PR.

vastStage avatar Nov 18 '26 19:11 vastStage