-
Notifications
You must be signed in to change notification settings - Fork 511
fix(task-session): fill false-completed task parts with child session real output (#863) #912
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
Closed
Jiajun0413
wants to merge
2
commits into
alvinunreal:master
from
Jiajun0413:fix/false-complete-fallback-reconcile
+547
−0
Closed
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
260 changes: 260 additions & 0 deletions
260
src/hooks/task-session-manager/false-complete-fallback.test.ts
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,260 @@ | ||
| import { describe, expect, test, mock } from 'bun:test'; | ||
| import { BackgroundJobBoard } from '../../utils'; | ||
| import { createTaskSessionManagerHook } from './index'; | ||
|
|
||
| const PARENT = 'parent-1'; | ||
| const CHILD = 'child-1'; | ||
|
|
||
| function taskCompletedPart( | ||
| callID: string, | ||
| childSessionId: string, | ||
| resultText = '', | ||
| ) { | ||
| const tag = 'task_result'; | ||
| return { | ||
| info: { | ||
| role: 'assistant', | ||
| agent: 'orchestrator', | ||
| sessionID: PARENT, | ||
| id: callID, | ||
| }, | ||
| parts: [ | ||
| { type: 'text', text: ' ' }, | ||
| { | ||
| type: 'tool', | ||
| tool: 'task', | ||
| callID, | ||
| state: { | ||
| status: 'completed', | ||
| output: [ | ||
| `<task id="${childSessionId}" state="completed">`, | ||
| `<${tag}>`, | ||
| resultText, | ||
| `</${tag}>`, | ||
| '</task>', | ||
| ].join('\n'), | ||
| metadata: { sessionId: childSessionId }, | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| function userMessage(id: string, text: string) { | ||
| return { | ||
| info: { role: 'user', agent: 'orchestrator', sessionID: PARENT, id }, | ||
| parts: [{ type: 'text', text }], | ||
| }; | ||
| } | ||
|
|
||
| function findTaskPart(messages: unknown[], callID: string) { | ||
| for (const message of messages as { parts?: any[] }[]) { | ||
| for (const part of message?.parts ?? []) { | ||
| if (part?.type === 'tool' && part?.tool === 'task' && part?.callID === callID) { | ||
| return part; | ||
| } | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| async function transform( | ||
| hook: ReturnType<typeof createTaskSessionManagerHook>, | ||
| history: unknown[], | ||
| ) { | ||
| const request = { messages: structuredClone(history) }; | ||
| await hook['experimental.chat.messages.transform']({}, request as never); | ||
| return request.messages; | ||
| } | ||
|
|
||
| function setupCompletedBoard(board: BackgroundJobBoard, description = 'test task') { | ||
| board.registerLaunch({ | ||
| taskID: CHILD, | ||
| parentSessionID: PARENT, | ||
| agent: 'fixer', | ||
| description, | ||
| }); | ||
| } | ||
|
|
||
| function mockClient(childMessages: Array<{ info?: { role: string }; parts?: Array<{ type: string; text?: string }> }>) { | ||
| return { | ||
| session: { | ||
| messages: mock(async () => ({ data: childMessages })), | ||
| status: mock(async () => ({ data: {} })), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe('reconcileFalseCompleteFallback', () => { | ||
| test('fills empty completed output with child session real assistant text', async () => { | ||
| const board = new BackgroundJobBoard(); | ||
| setupCompletedBoard(board, 'audit DATA.md'); | ||
| // Child session has produced a real 9346-char report | ||
| const childMessages = [ | ||
| { info: { role: 'user' }, parts: [{ type: 'text', text: 'audit' }] }, | ||
| { | ||
| info: { role: 'assistant' }, | ||
| parts: [{ type: 'text', text: '# Audit Report\nAll claims verified.' }], | ||
| }, | ||
| ]; | ||
| const hook = createTaskSessionManagerHook( | ||
| { client: mockClient(childMessages), directory: '/tmp' } as never, | ||
| { | ||
| maxSessionsPerAgent: 2, | ||
| maxRetainedSnapshots: 2, | ||
| backgroundJobBoard: board, | ||
| shouldManageSession: () => true, | ||
| }, | ||
| ); | ||
|
|
||
| const history = [ | ||
| userMessage('u1', 'run audit'), | ||
| taskCompletedPart('call-1', CHILD, ''), // empty result = false-complete | ||
| ]; | ||
|
|
||
| const result = await transform(hook, history); | ||
| const part = findTaskPart(result, 'call-1') as any; | ||
|
|
||
| expect(part.state.status).toBe('completed'); | ||
| expect(part.state.output).toContain('state="completed"'); | ||
| expect(part.state.output).toContain('Background task completed: audit DATA.md'); | ||
| expect(part.state.output).toContain('# Audit Report'); | ||
| expect(part.state.output).toContain('All claims verified.'); | ||
| }); | ||
|
|
||
| test('does NOT rewrite when completed output already has real text', async () => { | ||
| const board = new BackgroundJobBoard(); | ||
| setupCompletedBoard(board, 'test task'); | ||
| const childMessages = [ | ||
| { info: { role: 'assistant' }, parts: [{ type: 'text', text: 'real output' }] }, | ||
| ]; | ||
| const hook = createTaskSessionManagerHook( | ||
| { client: mockClient(childMessages), directory: '/tmp' } as never, | ||
| { | ||
| maxSessionsPerAgent: 2, | ||
| maxRetainedSnapshots: 2, | ||
| backgroundJobBoard: board, | ||
| shouldManageSession: () => true, | ||
| }, | ||
| ); | ||
|
|
||
| const realText = 'Already have real result here'; | ||
| const history = [ | ||
| userMessage('u1', 'run task'), | ||
| taskCompletedPart('call-1', CHILD, realText), | ||
| ]; | ||
|
|
||
| const result = await transform(hook, history); | ||
| const part = findTaskPart(result, 'call-1') as any; | ||
|
|
||
| // Real completion is preserved unchanged. | ||
| expect(part.state.output).toContain(realText); | ||
| expect(part.state.output).not.toContain('Background task completed:'); | ||
| }); | ||
|
|
||
| test('does NOT rewrite when child session has no assistant text yet (still running)', async () => { | ||
| const board = new BackgroundJobBoard(); | ||
| setupCompletedBoard(board, 'test task'); | ||
| // Child session has no assistant text — fallback model still running | ||
| const childMessages = [ | ||
| { info: { role: 'user' }, parts: [{ type: 'text', text: 'prompt' }] }, | ||
| ]; | ||
| const hook = createTaskSessionManagerHook( | ||
| { client: mockClient(childMessages), directory: '/tmp' } as never, | ||
| { | ||
| maxSessionsPerAgent: 2, | ||
| maxRetainedSnapshots: 2, | ||
| backgroundJobBoard: board, | ||
| shouldManageSession: () => true, | ||
| }, | ||
| ); | ||
|
|
||
| const history = [ | ||
| userMessage('u1', 'run task'), | ||
| taskCompletedPart('call-1', CHILD, ''), | ||
| ]; | ||
|
|
||
| const result = await transform(hook, history); | ||
| const part = findTaskPart(result, 'call-1') as any; | ||
|
|
||
| // No real text available yet — part stays empty (next turn re-evaluates). | ||
| expect(part.state.status).toBe('completed'); | ||
| const tagMatch = /<task_result>\s*([\s\S]*?)\s*<\/task_result>/m.exec( | ||
| part.state.output, | ||
| ); | ||
| expect(tagMatch?.[1]?.trim()).toBe(''); | ||
| }); | ||
|
|
||
| test('is idempotent across consecutive transforms', async () => { | ||
| const board = new BackgroundJobBoard(); | ||
| setupCompletedBoard(board, 'test task'); | ||
| const childMessages = [ | ||
| { info: { role: 'assistant' }, parts: [{ type: 'text', text: 'real output' }] }, | ||
| ]; | ||
| const hook = createTaskSessionManagerHook( | ||
| { client: mockClient(childMessages), directory: '/tmp' } as never, | ||
| { | ||
| maxSessionsPerAgent: 2, | ||
| maxRetainedSnapshots: 2, | ||
| backgroundJobBoard: board, | ||
| shouldManageSession: () => true, | ||
| }, | ||
| ); | ||
|
|
||
| const history = [ | ||
| userMessage('u1', 'run task'), | ||
| taskCompletedPart('call-1', CHILD, ''), | ||
| ]; | ||
|
|
||
| const first = await transform(hook, history); | ||
| const firstOutput = findTaskPart(first, 'call-1').state.output; | ||
|
|
||
| const second = await transform(hook, history); | ||
| const secondOutput = findTaskPart(second, 'call-1').state.output; | ||
|
|
||
| expect(secondOutput).toBe(firstOutput); | ||
| }); | ||
|
|
||
| test('does not touch non-task tool parts', async () => { | ||
| const board = new BackgroundJobBoard(); | ||
| setupCompletedBoard(board, 'test task'); | ||
| const childMessages = [ | ||
| { info: { role: 'assistant' }, parts: [{ type: 'text', text: 'real' }] }, | ||
| ]; | ||
| const hook = createTaskSessionManagerHook( | ||
| { client: mockClient(childMessages), directory: '/tmp' } as never, | ||
| { | ||
| maxSessionsPerAgent: 2, | ||
| maxRetainedSnapshots: 2, | ||
| backgroundJobBoard: board, | ||
| shouldManageSession: () => true, | ||
| }, | ||
| ); | ||
|
|
||
| const history = [ | ||
| userMessage('u1', 'run task'), | ||
| { | ||
| info: { role: 'assistant', agent: 'orchestrator', sessionID: PARENT, id: 'call-2' }, | ||
| parts: [ | ||
| { type: 'text', text: ' ' }, | ||
| { | ||
| type: 'tool', | ||
| tool: 'read', | ||
| callID: 'call-2', | ||
| state: { | ||
| status: 'completed', | ||
| output: '<task id="child-1" state="completed"><task_result></task_result></task>', | ||
| metadata: { sessionId: CHILD }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = await transform(hook, history); | ||
| const part = result[1].parts[1] as any; | ||
|
|
||
| // read tool parts are not reconciled. | ||
| expect(part.state.output).toContain('<task_result></task_result>'); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
When the child-session messages request fails because of a transient client error, unavailable service, or missing session, the uncaught exception propagates through the new reconciliation pass and aborts the parent message transform instead of preserving the empty result for a later retry.