Skip to content

The agent that grows with you

fix(tui): the tool trail survives an idle snapshot that lands a frame before the final

teknium119fd84167569
diff --git a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
index 6d8a1b64f06..ace32bf123b 100644
--- a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
+++ b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts
@@ -107,6 +107,28 @@ describe('createGatewayEventHandler', () => {
expect(getUiState().busy).toBe(false)
})
+ it('an idle snapshot a frame ahead of the final leaves the tool trail for the final to archive', () => {
+ const appended: Msg[] = []
+ const onEvent = createGatewayEventHandler(buildCtx(appended))
+ patchUiState({ sid: 'focused', info: { model: 'test', skills: {}, tools: {} } as any })
+ const emit = (type: string, payload: any) => onEvent({ type, payload, session_id: 'focused' } as any)
+
+ emit('message.start', {})
+ emit('tool.start', { tool_id: 'c1', name: 'terminal', context: 'echo TOOLMARK' })
+ emit('tool.complete', { tool_id: 'c1', name: 'terminal', duration_s: 0.1 })
+ // The authority settles the row (running: false) before it publishes message.complete.
+ emit('session.info', { running: false })
+ expect(getUiState().busy).toBe(false)
+ emit('message.complete', { text: 'BETA after the tool' })
+
+ const trail = appended.find(m => m.kind === 'trail')
+ expect(trail?.tools?.join('\n')).toContain('TOOLMARK')
+ expect(appended.at(-1)).toMatchObject({ role: 'assistant', text: 'BETA after the tool' })
+ // Nothing is left parked for the next turn.
+ emit('message.start', {})
+ expect(getTurnState().streamSegments).toEqual([])
+ })
+
it('fences restarted owner lifecycle events until resume establishes the new epoch', () => {
const appended: Msg[] = []
const onEvent = createGatewayEventHandler(buildCtx(appended))
diff --git a/ui-tui/src/app/createGatewayEventHandler.ts b/ui-tui/src/app/createGatewayEventHandler.ts
index 036ec915603..f0668a67151 100644
--- a/ui-tui/src/app/createGatewayEventHandler.ts
+++ b/ui-tui/src/app/createGatewayEventHandler.ts
@@ -901,7 +901,10 @@ export function createGatewayEventHandler(ctx: GatewayEventHandlerContext): (ev:
if (incoming.running === false) {
turnController.clearStatusTimer()
- turnController.idle()
+ // The authority settles the row before it publishes the final, so this snapshot
+ // can land a frame ahead of `message.complete`; the trail (tool rows, reasoning)
+ // stays parked for that final to archive instead of being dropped here.
+ turnController.idle({ keepTurnArchive: true })
setStatus('ready')
}
diff --git a/ui-tui/src/app/turnController.ts b/ui-tui/src/app/turnController.ts
index 4e753ff588b..f75262dc5c0 100644
--- a/ui-tui/src/app/turnController.ts
+++ b/ui-tui/src/app/turnController.ts
@@ -308,17 +308,21 @@ class TurnController {
patchTurnState({ reasoningActive: false, reasoningStreaming: false })
}
- idle() {
+ idle(opts: { keepTurnArchive?: boolean } = {}) {
this.endReasoningPhase()
this.activeTools = []
this.streamTimer = clear(this.streamTimer)
this.bufRef = ''
- this.pendingSegmentTools = []
- this.segmentMessages = []
+ // `keepTurnArchive`: the turn-over signal arrived before the final; the sealed segments
+ // and finished tool rows wait for `recordMessageComplete` to archive them.
+ if (!opts.keepTurnArchive) {
+ this.pendingSegmentTools = []
+ this.segmentMessages = []
+ }
patchTurnState({
- streamPendingTools: [],
- streamSegments: [],
+ streamPendingTools: opts.keepTurnArchive ? this.pendingSegmentTools : [],
+ streamSegments: opts.keepTurnArchive ? this.segmentMessages : [],
streaming: '',
subagents: [],
tools: [],
@@ -1040,7 +1044,11 @@ class TurnController {
}
patchUiState({ busy: true })
- patchTurnState({ activity: [], outcome: '', subagents: [], toolTokens: 0, tools: [], turnTrail: [] })
+ // A turn whose final never came (owner died between settle and publish) must not leak its
+ // parked archive into this one.
+ this.segmentMessages = []
+ this.pendingSegmentTools = []
+ patchTurnState({ activity: [], outcome: '', streamPendingTools: [], streamSegments: [], subagents: [], toolTokens: 0, tools: [], turnTrail: [] })
}
upsertSubagent(