|
|
@@ -1390,7 +1390,7 @@ describe('task-session-manager hook', () => {
|
|
|
{ output: ['task_id: child-1', 'state: running'].join('\n') },
|
|
|
);
|
|
|
|
|
|
- // setup sibling child-2 (will be terminal via updateStatus, no injected completion)
|
|
|
+ // setup sibling child-2 (terminal via updateStatus after board payload, no injected)
|
|
|
await hook['tool.execute.before'](
|
|
|
{ tool: 'task', sessionID: 'parent-1', callID: 'call-2' },
|
|
|
{
|
|
|
@@ -1402,6 +1402,38 @@ describe('task-session-manager hook', () => {
|
|
|
{ output: ['task_id: child-2', 'state: running'].join('\n') },
|
|
|
);
|
|
|
|
|
|
+ // Full production sequence: transformMessages ... Only child-1 synthetic.
|
|
|
+ // child-2 still running so not in terminalUnreconciled IDs of this payload.
|
|
|
+ const messages = {
|
|
|
+ messages: [
|
|
|
+ {
|
|
|
+ info: { role: 'user', agent: 'orchestrator', sessionID: 'parent-1' },
|
|
|
+ parts: [
|
|
|
+ {
|
|
|
+ type: 'text',
|
|
|
+ id: 'part-1',
|
|
|
+ synthetic: true,
|
|
|
+ text: [
|
|
|
+ '<task id="child-1" state="completed">',
|
|
|
+ '<summary>Background task completed: first</summary>',
|
|
|
+ '<task_result>done1</task_result>',
|
|
|
+ '</task>',
|
|
|
+ ].join('\n'),
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ };
|
|
|
+ await transformMessages(hook, messages);
|
|
|
+
|
|
|
+ expect(board.get('child-1')?.terminalUnreconciled).toBe(true);
|
|
|
+ expect(board.get('child-2')?.terminalUnreconciled).toBe(false);
|
|
|
+
|
|
|
+ // duplicate stays idempotent
|
|
|
+ await transformMessages(hook, messages);
|
|
|
+ expect(board.get('child-1')?.terminalUnreconciled).toBe(true);
|
|
|
+
|
|
|
+ // now make child-2 terminal (after the board payload was emitted)
|
|
|
board.updateStatus({
|
|
|
taskID: 'child-2',
|
|
|
state: 'completed',
|
|
|
@@ -1409,10 +1441,40 @@ describe('task-session-manager hook', () => {
|
|
|
});
|
|
|
expect(board.get('child-2')?.terminalUnreconciled).toBe(true);
|
|
|
|
|
|
- // Full production sequence: transformMessages performs the message
|
|
|
- // transform and injectBackgroundJobBoard. Only child-1 has a synthetic
|
|
|
- // terminal result; child-2 is terminal through updateStatus only.
|
|
|
- const messages = {
|
|
|
+ await hook.event({
|
|
|
+ event: {
|
|
|
+ type: 'session.status',
|
|
|
+ properties: { sessionID: 'parent-1', status: { type: 'idle' } },
|
|
|
+ },
|
|
|
+ });
|
|
|
+ await flushIdleReconcileDelay();
|
|
|
+
|
|
|
+ expect(board.get('child-1')).toMatchObject({
|
|
|
+ state: 'reconciled',
|
|
|
+ terminalUnreconciled: false,
|
|
|
+ });
|
|
|
+ // sibling terminal but never appeared in board payload nor had synthetic injected
|
|
|
+ expect(board.get('child-2')).toMatchObject({
|
|
|
+ state: 'completed',
|
|
|
+ terminalUnreconciled: true,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ test('no-starvation latest pipeline: child-1 synthetic remembered; child-2 becomes terminal before idle; next full transform emits child-2 in board; idle reconciles both', async () => {
|
|
|
+ const board = new BackgroundJobBoard();
|
|
|
+ const { hook } = createHook({ backgroundJobBoard: board });
|
|
|
+
|
|
|
+ // child-1 via tool + synthetic injected (narrow + metadata will remember it)
|
|
|
+ await hook['tool.execute.before'](
|
|
|
+ { tool: 'task', sessionID: 'parent-1', callID: 'call-1' },
|
|
|
+ { args: { subagent_type: 'explorer', description: 'first' } },
|
|
|
+ );
|
|
|
+ await hook['tool.execute.after'](
|
|
|
+ { tool: 'task', sessionID: 'parent-1', callID: 'call-1' },
|
|
|
+ { output: ['task_id: child-1', 'state: running'].join('\n') },
|
|
|
+ );
|
|
|
+
|
|
|
+ const msg1 = {
|
|
|
messages: [
|
|
|
{
|
|
|
info: { role: 'user', agent: 'orchestrator', sessionID: 'parent-1' },
|
|
|
@@ -1432,17 +1494,99 @@ describe('task-session-manager hook', () => {
|
|
|
},
|
|
|
],
|
|
|
};
|
|
|
- await transformMessages(hook, messages);
|
|
|
-
|
|
|
- expect(boardText(messages)).toContain('child-2');
|
|
|
- expect(boardText(messages)).toContain('completed, unreconciled');
|
|
|
+ await transformMessages(hook, msg1);
|
|
|
expect(board.get('child-1')?.terminalUnreconciled).toBe(true);
|
|
|
+
|
|
|
+ // before idle, child-2 becomes terminal (no synthetic for it)
|
|
|
+ await hook['tool.execute.before'](
|
|
|
+ { tool: 'task', sessionID: 'parent-1', callID: 'call-2' },
|
|
|
+ { args: { subagent_type: 'oracle', description: 'second' } },
|
|
|
+ );
|
|
|
+ await hook['tool.execute.after'](
|
|
|
+ { tool: 'task', sessionID: 'parent-1', callID: 'call-2' },
|
|
|
+ { output: ['task_id: child-2', 'state: running'].join('\n') },
|
|
|
+ );
|
|
|
+ board.updateStatus({
|
|
|
+ taskID: 'child-2',
|
|
|
+ state: 'completed',
|
|
|
+ resultSummary: 'done2',
|
|
|
+ });
|
|
|
expect(board.get('child-2')?.terminalUnreconciled).toBe(true);
|
|
|
|
|
|
- // duplicate full production injection stays idempotent (narrow + gated broad)
|
|
|
+ // next full transform: emits board payload that now includes child-2 terminal
|
|
|
+ const msg2 = createMessages('parent-1', 'next turn');
|
|
|
+ await transformMessages(hook, msg2);
|
|
|
+ expect(boardText(msg2)).toContain('child-2');
|
|
|
+ expect(boardText(msg2)).toContain('completed, unreconciled');
|
|
|
+
|
|
|
+ await hook.event({
|
|
|
+ event: {
|
|
|
+ type: 'session.status',
|
|
|
+ properties: { sessionID: 'parent-1', status: { type: 'idle' } },
|
|
|
+ },
|
|
|
+ });
|
|
|
+ await flushIdleReconcileDelay();
|
|
|
+
|
|
|
+ expect(board.get('child-1')).toMatchObject({
|
|
|
+ state: 'reconciled',
|
|
|
+ terminalUnreconciled: false,
|
|
|
+ });
|
|
|
+ expect(board.get('child-2')).toMatchObject({
|
|
|
+ state: 'reconciled',
|
|
|
+ terminalUnreconciled: false,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ test('metadata/renderer selection omits child-2 from both board text and IDs; child-2 absent from emitted text and remains unreconciled', async () => {
|
|
|
+ const board = new BackgroundJobBoard();
|
|
|
+ // renderer-selection stub/fake: omits child-2 row from BOTH text and IDs (test-only shaping)
|
|
|
+ const orig = board.formatForPromptWithMetadata.bind(board);
|
|
|
+ board.formatForPromptWithMetadata = (p: string) => {
|
|
|
+ const m = orig(p);
|
|
|
+ if (!m) return m;
|
|
|
+ const shapedText = m.text
|
|
|
+ ? m.text
|
|
|
+ .split('\n')
|
|
|
+ .filter((line: string) => !line.includes('child-2'))
|
|
|
+ .join('\n')
|
|
|
+ : m.text;
|
|
|
+ return {
|
|
|
+ text: shapedText,
|
|
|
+ terminalUnreconciledTaskIDs: m.terminalUnreconciledTaskIDs.filter(
|
|
|
+ (id: string) => id === 'child-1',
|
|
|
+ ),
|
|
|
+ };
|
|
|
+ };
|
|
|
+ const { hook } = createHook({ backgroundJobBoard: board });
|
|
|
+
|
|
|
+ board.registerLaunch({
|
|
|
+ taskID: 'child-1',
|
|
|
+ parentSessionID: 'parent-1',
|
|
|
+ agent: 'explorer',
|
|
|
+ description: 'c1',
|
|
|
+ });
|
|
|
+ board.updateStatus({
|
|
|
+ taskID: 'child-1',
|
|
|
+ state: 'completed',
|
|
|
+ resultSummary: 'd1',
|
|
|
+ });
|
|
|
+ board.registerLaunch({
|
|
|
+ taskID: 'child-2',
|
|
|
+ parentSessionID: 'parent-1',
|
|
|
+ agent: 'oracle',
|
|
|
+ description: 'c2',
|
|
|
+ });
|
|
|
+ board.updateStatus({
|
|
|
+ taskID: 'child-2',
|
|
|
+ state: 'completed',
|
|
|
+ resultSummary: 'd2',
|
|
|
+ });
|
|
|
+
|
|
|
+ const messages = createMessages('parent-1');
|
|
|
await transformMessages(hook, messages);
|
|
|
- expect(board.get('child-1')?.terminalUnreconciled).toBe(true);
|
|
|
- expect(board.get('child-2')?.terminalUnreconciled).toBe(true);
|
|
|
+
|
|
|
+ const emitted = boardText(messages);
|
|
|
+ expect(emitted).not.toContain('child-2');
|
|
|
|
|
|
await hook.event({
|
|
|
event: {
|
|
|
@@ -1456,13 +1600,94 @@ describe('task-session-manager hook', () => {
|
|
|
state: 'reconciled',
|
|
|
terminalUnreconciled: false,
|
|
|
});
|
|
|
- // sibling not remembered via narrow; broad must not widen to it
|
|
|
expect(board.get('child-2')).toMatchObject({
|
|
|
state: 'completed',
|
|
|
terminalUnreconciled: true,
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+ test('checkpoint-compatible no-starvation via snapshot replay: child-1 synthetic; child-2 terminal no synthetic; second transform replays snapshot with child-2; board text has it; idle reconciles both', async () => {
|
|
|
+ const board = new BackgroundJobBoard();
|
|
|
+ const { hook } = createHook({
|
|
|
+ backgroundJobBoard: board,
|
|
|
+ strategy: 'checkpoint-compatible',
|
|
|
+ idleReconcileDelayMs: 0,
|
|
|
+ });
|
|
|
+
|
|
|
+ // first: synthetic child-1 only
|
|
|
+ await hook['tool.execute.before'](
|
|
|
+ { tool: 'task', sessionID: 'parent-1', callID: 'call-1' },
|
|
|
+ { args: { subagent_type: 'explorer', description: 'first' } },
|
|
|
+ );
|
|
|
+ await hook['tool.execute.after'](
|
|
|
+ { tool: 'task', sessionID: 'parent-1', callID: 'call-1' },
|
|
|
+ { output: ['task_id: child-1', 'state: running'].join('\n') },
|
|
|
+ );
|
|
|
+
|
|
|
+ const msg1 = {
|
|
|
+ messages: [
|
|
|
+ {
|
|
|
+ info: { role: 'user', agent: 'orchestrator', sessionID: 'parent-1' },
|
|
|
+ parts: [
|
|
|
+ {
|
|
|
+ type: 'text',
|
|
|
+ id: 'part-1',
|
|
|
+ synthetic: true,
|
|
|
+ text: [
|
|
|
+ '<task id="child-1" state="completed">',
|
|
|
+ '<summary>Background task completed: first</summary>',
|
|
|
+ '<task_result>done1</task_result>',
|
|
|
+ '</task>',
|
|
|
+ ].join('\n'),
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ };
|
|
|
+ await transformMessages(hook, msg1);
|
|
|
+ expect(board.get('child-1')?.terminalUnreconciled).toBe(true);
|
|
|
+
|
|
|
+ // child-2 becomes terminal without synthetic
|
|
|
+ await hook['tool.execute.before'](
|
|
|
+ { tool: 'task', sessionID: 'parent-1', callID: 'call-2' },
|
|
|
+ { args: { subagent_type: 'oracle', description: 'second' } },
|
|
|
+ );
|
|
|
+ await hook['tool.execute.after'](
|
|
|
+ { tool: 'task', sessionID: 'parent-1', callID: 'call-2' },
|
|
|
+ { output: ['task_id: child-2', 'state: running'].join('\n') },
|
|
|
+ );
|
|
|
+ board.updateStatus({
|
|
|
+ taskID: 'child-2',
|
|
|
+ state: 'completed',
|
|
|
+ resultSummary: 'done2',
|
|
|
+ });
|
|
|
+ expect(board.get('child-2')?.terminalUnreconciled).toBe(true);
|
|
|
+
|
|
|
+ // second full transform (checkpoint): emits/replays snapshot containing child-2 (no narrow for child-2)
|
|
|
+ const msg2 = createMessages('parent-1', 'next');
|
|
|
+ await transformMessages(hook, msg2);
|
|
|
+ const replayedText = boardText(msg2);
|
|
|
+ expect(replayedText).toContain('child-2');
|
|
|
+ expect(replayedText).toContain('completed, unreconciled');
|
|
|
+
|
|
|
+ await hook.event({
|
|
|
+ event: {
|
|
|
+ type: 'session.status',
|
|
|
+ properties: { sessionID: 'parent-1', status: { type: 'idle' } },
|
|
|
+ },
|
|
|
+ });
|
|
|
+ await flushContinuation();
|
|
|
+
|
|
|
+ expect(board.get('child-1')).toMatchObject({
|
|
|
+ state: 'reconciled',
|
|
|
+ terminalUnreconciled: false,
|
|
|
+ });
|
|
|
+ expect(board.get('child-2')).toMatchObject({
|
|
|
+ state: 'reconciled',
|
|
|
+ terminalUnreconciled: false,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
test('ignores non-synthetic user text that resembles task status', async () => {
|
|
|
const board = new BackgroundJobBoard();
|
|
|
const { hook } = createHook({ backgroundJobBoard: board });
|