Repository navigation
fix(agent-core): drop empty assistant tail left by an interrupted turn on resume (#1404) #1882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
whyihaveyou
wants to merge
2
commits into
MoonshotAI:main
Choose a base branch
from
whyihaveyou:fix/empty-assistant-tail-resume
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // finishResume — replay-time repair for turns killed between `step.begin` and | ||
| // the first content/tool record (issue #1404: empty assistant at the tail of | ||
| // history bricks every subsequent resume with provider 400s). | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { testAgent } from './harness/agent'; | ||
|
|
||
| const PROVIDER = { type: 'kimi', apiKey: 'test-key', model: 'kimi-code' } as const; | ||
| const CAPS = { | ||
| image_in: true, | ||
| video_in: true, | ||
| audio_in: false, | ||
| thinking: true, | ||
| tool_use: true, | ||
| max_context_tokens: 256_000, | ||
| } as const; | ||
|
|
||
| function seedAgent() { | ||
| const ctx = testAgent(); | ||
| ctx.configure({ provider: PROVIDER, modelCapabilities: CAPS }); | ||
| ctx.agent.context.appendUserMessage([{ type: 'text', text: 'user one' }]); | ||
| ctx.agent.context.appendLoopEvent({ type: 'step.begin', uuid: 's1', turnId: 't1', step: 1 }); | ||
| ctx.agent.context.appendLoopEvent({ | ||
| type: 'content.part', | ||
| uuid: 'p1', | ||
| turnId: 't1', | ||
| step: 1, | ||
| stepUuid: 's1', | ||
| part: { type: 'text', text: 'assistant one' }, | ||
| }); | ||
| ctx.agent.context.appendLoopEvent({ type: 'step.end', uuid: 's1', turnId: 't1', step: 1 }); | ||
| return ctx; | ||
| } | ||
|
|
||
| describe('finishResume — empty assistant tail repair (#1404)', () => { | ||
| it('drops a trailing assistant message left empty by a killed turn', () => { | ||
| const ctx = seedAgent(); | ||
| const before = ctx.agent.context.history.length; | ||
| // Turn dies right after step.begin: an empty assistant is appended and | ||
| // never receives any content or tool calls. | ||
| ctx.agent.context.appendLoopEvent({ type: 'step.begin', uuid: 'dead', turnId: 't2', step: 2 }); | ||
| expect(ctx.agent.context.history.at(-1)?.role).toBe('assistant'); | ||
| expect(ctx.agent.context.history.at(-1)?.content).toHaveLength(0); | ||
| expect(ctx.agent.context.history.at(-1)?.toolCalls).toHaveLength(0); | ||
|
|
||
| ctx.agent.context.finishResume(); | ||
|
|
||
| const history = ctx.agent.context.history; | ||
| expect(history.length).toBe(before); | ||
| const last = history.at(-1); | ||
| expect(last?.role).toBe('assistant'); | ||
| expect(last?.content.map((p) => (p.type === 'text' ? p.text : '')).join('')).toBe( | ||
| 'assistant one', | ||
| ); | ||
| }); | ||
|
|
||
| it('drops several consecutive empty assistant tails', () => { | ||
| const ctx = seedAgent(); | ||
| ctx.agent.context.appendLoopEvent({ type: 'step.begin', uuid: 'dead1', turnId: 't2', step: 2 }); | ||
| ctx.agent.context.appendLoopEvent({ type: 'step.begin', uuid: 'dead2', turnId: 't2', step: 3 }); | ||
|
|
||
| ctx.agent.context.finishResume(); | ||
|
|
||
| const last = ctx.agent.context.history.at(-1); | ||
| expect(last?.content.map((p) => (p.type === 'text' ? p.text : '')).join('')).toBe( | ||
| 'assistant one', | ||
| ); | ||
| }); | ||
|
|
||
| it('keeps a trailing assistant that has content', () => { | ||
| const ctx = seedAgent(); | ||
| ctx.agent.context.appendLoopEvent({ type: 'step.begin', uuid: 's2', turnId: 't2', step: 2 }); | ||
| ctx.agent.context.appendLoopEvent({ | ||
| type: 'content.part', | ||
| uuid: 'p2', | ||
| turnId: 't2', | ||
| step: 2, | ||
| stepUuid: 's2', | ||
| part: { type: 'text', text: 'partial but real content' }, | ||
| }); | ||
|
|
||
| ctx.agent.context.finishResume(); | ||
|
|
||
| const last = ctx.agent.context.history.at(-1); | ||
| expect(last?.role).toBe('assistant'); | ||
| expect(last?.content.map((p) => (p.type === 'text' ? p.text : '')).join('')).toBe( | ||
| 'partial but real content', | ||
| ); | ||
| }); | ||
|
|
||
| it('keeps a trailing assistant that has tool calls (exchange closes with an error result)', () => { | ||
| const ctx = seedAgent(); | ||
| ctx.agent.context.appendLoopEvent({ type: 'step.begin', uuid: 's2', turnId: 't2', step: 2 }); | ||
| ctx.agent.context.appendLoopEvent({ | ||
| type: 'tool.call', | ||
| uuid: 'c1', | ||
| turnId: 't2', | ||
| step: 2, | ||
| stepUuid: 's2', | ||
| toolCallId: 'call-1', | ||
| name: 'bash', | ||
| args: { command: 'sleep 1' }, | ||
| }); | ||
|
|
||
| ctx.agent.context.finishResume(); | ||
|
|
||
| const history = ctx.agent.context.history; | ||
| const assistant = history.findLast((m) => m.role === 'assistant'); | ||
| expect(assistant?.toolCalls.map((c) => c.id)).toContain('call-1'); | ||
| // The dangling call is closed in place by finishResume with an interrupted | ||
| // tool result, so the exchange stays well-formed for the provider. | ||
| expect(history.at(-1)?.role).toBe('tool'); | ||
| expect(history.at(-1)?.isError).toBe(true); | ||
| }); | ||
| }); |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This repair only pops the blank assistant from the in-memory
_history; the tornstep.beginrecord remains inwire.jsonl. If a user resumes once, sends another prompt, and then resumes the same session again, replay rebuilds the old empty assistant before the newly appended records, so it is no longer at the tail and this loop stops before removing it. That makes the resumed raw context/transcript contain the blank assistant again instead of being self-healing; the repair needs to be persisted/tombstoned or applied during replay reduction wherever these empty interrupted-step messages appear.Useful? React with 👍 / 👎.