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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {
  describe,
  it,
  expect,
  vi,
  beforeEach,
  afterEach,
  type Mocked,
} from 'vitest';
import {
  AgentTerminateMode,
  type LocalAgentDefinition,
  type SubagentActivityEvent,
  type AgentInputs,
  type SubagentProgress,
  SubagentActivityErrorType,
  SUBAGENT_REJECTED_ERROR_PREFIX,
  SubagentState,
} from './types.js';
import { LocalSubagentInvocation } from './local-invocation.js';
import { LocalAgentExecutor } from './local-executor.js';
import { makeFakeConfig } from '../test-utils/config.js';
import type { Config } from '../config/config.js';
import type { MessageBus } from '../confirmation-bus/message-bus.js';
import { type z } from 'zod';
import { createMockMessageBus } from '../test-utils/mock-message-bus.js';

vi.mock('./local-executor.js');

const MockLocalAgentExecutor = vi.mocked(LocalAgentExecutor);

let mockConfig: Config;

const testDefinition: LocalAgentDefinition<z.ZodUnknown> = {
  kind: 'local',
  name: 'MockAgent',
  displayName: 'Mock Agent',
  description: 'A mock agent.',
  inputConfig: {
    inputSchema: {
      type: 'object',
      properties: {
        task: { type: 'string', description: 'task' },
        priority: { type: 'number', description: 'prio' },
      },
      required: ['task'],
    },
  },
  modelConfig: {
    model: 'test',
    generateContentConfig: {
      temperature: 0,
      topP: 1,
    },
  },
  runConfig: { maxTimeMinutes: 1 },
  promptConfig: { systemPrompt: 'test' },
};

describe('LocalSubagentInvocation', () => {
  let mockExecutorInstance: Mocked<LocalAgentExecutor<z.ZodUnknown>>;
  let mockMessageBus: MessageBus;

  beforeEach(() => {
    vi.clearAllMocks();
    mockConfig = makeFakeConfig();
    // .config is already set correctly by the getter on the instance.
    Object.defineProperty(mockConfig, 'promptId', {
      get: () => 'test-prompt-id',
      configurable: true,
    });
    mockMessageBus = createMockMessageBus();

    mockExecutorInstance = {
      run: vi.fn(),
      definition: testDefinition,
      agentId: 'test-agent-id',
    } as unknown as Mocked<LocalAgentExecutor<z.ZodUnknown>>;

    MockLocalAgentExecutor.create.mockResolvedValue(
      mockExecutorInstance as unknown as LocalAgentExecutor<z.ZodTypeAny>,
    );
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('should pass the messageBus to the parent constructor', () => {
    const params = { task: 'Analyze data' };
    const invocation = new LocalSubagentInvocation(
      testDefinition,
      mockConfig,
      params,
      mockMessageBus,
    );

    // Access the protected messageBus property by casting to any
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    expect((invocation as any).messageBus).toBe(mockMessageBus);
  });

  describe('getDescription', () => {
    it('should format the description with inputs', () => {
      const params = { task: 'Analyze data', priority: 5 };
      const invocation = new LocalSubagentInvocation(
        testDefinition,
        mockConfig,
        params,
        mockMessageBus,
      );
      const description = invocation.getDescription();
      expect(description).toBe(
        "Running subagent 'MockAgent' with inputs: { task: Analyze data, priority: 5 }",
      );
    });

    it('should not truncate long input values', () => {
      const longTask = 'A'.repeat(100);
      const params = { task: longTask };
      const invocation = new LocalSubagentInvocation(
        testDefinition,
        mockConfig,
        params,
        mockMessageBus,
      );
      const description = invocation.getDescription();
      expect(description).toBe(
        `Running subagent 'MockAgent' with inputs: { task: ${'A'.repeat(100)} }`,
      );
    });

    it('should not truncate the overall description', () => {
      // Create a definition and inputs that result in a very long description
      const longNameDef: LocalAgentDefinition = {
        ...testDefinition,
        name: 'VeryLongAgentNameThatTakesUpSpace',
      };
      const params: AgentInputs = {};
      for (let i = 0; i < 20; i++) {
        params[`input${i}`] = `value${i}`;
      }
      const invocation = new LocalSubagentInvocation(
        longNameDef,
        mockConfig,
        params,
        mockMessageBus,
      );
      const description = invocation.getDescription();
      expect(description.length).toBeGreaterThan(300);
      expect(
        description.startsWith(
          "Running subagent 'VeryLongAgentNameThatTakesUpSpace'",
        ),
      ).toBe(true);
    });
  });

  describe('execute', () => {
    let signal: AbortSignal;
    let updateOutput: ReturnType<typeof vi.fn>;
    const params = { task: 'Execute task' };
    let invocation: LocalSubagentInvocation;

    beforeEach(() => {
      signal = new AbortController().signal;
      updateOutput = vi.fn();
      invocation = new LocalSubagentInvocation(
        testDefinition,
        mockConfig,
        params,
        mockMessageBus,
      );
    });

    it('should initialize and run the executor successfully', async () => {
      const mockOutput = {
        result: 'Analysis complete.',
        terminate_reason: AgentTerminateMode.GOAL,
      };
      mockExecutorInstance.run.mockResolvedValue(mockOutput);

      const result = await invocation.execute({
        abortSignal: signal,
        updateOutput,
      });

      expect(MockLocalAgentExecutor.create).toHaveBeenCalledWith(
        testDefinition,
        mockConfig,
        expect.any(Function),
      );
      expect(updateOutput).toHaveBeenCalledWith(
        expect.objectContaining({
          isSubagentProgress: true,
          agentName: 'MockAgent',
        }),
      );

      expect(mockExecutorInstance.run).toHaveBeenCalledWith(params, signal);

      expect(result.llmContent).toEqual([
        {
          text: expect.stringContaining(
            "Subagent 'MockAgent' finished.\nTermination Reason: GOAL\nResult:\nAnalysis complete.",
          ),
        },
      ]);
      const display = result.returnDisplay as SubagentProgress;
      expect(display.isSubagentProgress).toBe(true);
      expect(display.state).toBe(SubagentState.COMPLETED);
      expect(display.result).toBe('Analysis complete.');
      expect(display.terminateReason).toBe(AgentTerminateMode.GOAL);
    });

    it('should show detailed UI for non-goal terminations (e.g., TIMEOUT)', async () => {
      const mockOutput = {
        result: 'Partial progress...',
        terminate_reason: AgentTerminateMode.TIMEOUT,
      };
      mockExecutorInstance.run.mockResolvedValue(mockOutput);

      const result = await invocation.execute({
        abortSignal: signal,
        updateOutput,
      });

      const display = result.returnDisplay as SubagentProgress;
      expect(display.isSubagentProgress).toBe(true);
      expect(display.state).toBe(SubagentState.COMPLETED);
      expect(display.result).toBe('Partial progress...');
      expect(display.terminateReason).toBe(AgentTerminateMode.TIMEOUT);
    });

    it('should stream THOUGHT_CHUNK activities from the executor, replacing the last running thought', async () => {
      mockExecutorInstance.run.mockImplementation(async () => {
        const onActivity = MockLocalAgentExecutor.create.mock.calls[0][2];

        if (onActivity) {
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'THOUGHT_CHUNK',
            data: { text: 'Analyzing...' },
          } as SubagentActivityEvent);
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'THOUGHT_CHUNK',
            data: { text: 'Thinking about next steps.' },
          } as SubagentActivityEvent);
        }
        return { result: 'Done', terminate_reason: AgentTerminateMode.GOAL };
      });

      await invocation.execute({ abortSignal: signal, updateOutput });

      expect(updateOutput).toHaveBeenCalledTimes(4); // Initial + 2 updates + Final completion
      const lastCall = updateOutput.mock.calls[3][0] as SubagentProgress;
      expect(lastCall.recentActivity).toContainEqual(
        expect.objectContaining({
          type: 'thought',
          content: 'Thinking about next steps.',
        }),
      );
      expect(lastCall.recentActivity).not.toContainEqual(
        expect.objectContaining({
          type: 'thought',
          content: 'Analyzing...',
        }),
      );
    });

    it('should overwrite the thought content with new THOUGHT_CHUNK activity', async () => {
      mockExecutorInstance.run.mockImplementation(async () => {
        const onActivity = MockLocalAgentExecutor.create.mock.calls[0][2];

        if (onActivity) {
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'THOUGHT_CHUNK',
            data: { text: 'I am thinking.' },
          } as SubagentActivityEvent);
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'THOUGHT_CHUNK',
            data: { text: 'Now I will act.' },
          } as SubagentActivityEvent);
        }
        return { result: 'Done', terminate_reason: AgentTerminateMode.GOAL };
      });

      await invocation.execute({ abortSignal: signal, updateOutput });

      const calls = updateOutput.mock.calls;
      const lastCall = calls[calls.length - 1][0] as SubagentProgress;
      expect(lastCall.recentActivity).toContainEqual(
        expect.objectContaining({
          type: 'thought',
          content: 'Now I will act.',
        }),
      );
    });

    it('should stream other activities (e.g., TOOL_CALL_START, ERROR)', async () => {
      mockExecutorInstance.run.mockImplementation(async () => {
        const onActivity = MockLocalAgentExecutor.create.mock.calls[0][2];

        if (onActivity) {
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'TOOL_CALL_START',
            data: { name: 'ls', args: {} },
          } as SubagentActivityEvent);
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'ERROR',
            data: { error: 'Failed' },
          } as SubagentActivityEvent);
        }
        return { result: 'Done', terminate_reason: AgentTerminateMode.GOAL };
      });

      await invocation.execute({ abortSignal: signal, updateOutput });

      expect(updateOutput).toHaveBeenCalledTimes(4); // Initial + 2 updates + Final completion
      const lastCall = updateOutput.mock.calls[3][0] as SubagentProgress;
      expect(lastCall.recentActivity).toContainEqual(
        expect.objectContaining({
          type: 'thought',
          content: 'Error: Failed',
          status: SubagentState.ERROR,
        }),
      );
    });

    it('should mark tool call as error when TOOL_CALL_END contains isError: true', async () => {
      mockExecutorInstance.run.mockImplementation(async () => {
        const onActivity = MockLocalAgentExecutor.create.mock.calls[0][2];

        if (onActivity) {
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'TOOL_CALL_START',
            data: { name: 'ls', args: {}, callId: 'call1' },
          } as SubagentActivityEvent);
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'TOOL_CALL_END',
            data: { name: 'ls', id: 'call1', data: { isError: true } },
          } as SubagentActivityEvent);
        }
        return { result: 'Done', terminate_reason: AgentTerminateMode.GOAL };
      });

      await invocation.execute({ abortSignal: signal, updateOutput });

      expect(updateOutput).toHaveBeenCalled();
      const lastCall = updateOutput.mock.calls[
        updateOutput.mock.calls.length - 1
      ][0] as SubagentProgress;
      expect(lastCall.recentActivity).toContainEqual(
        expect.objectContaining({
          type: 'tool_call',
          content: 'ls',
          status: SubagentState.ERROR,
        }),
      );
    });

    it('should reflect tool rejections in the activity stream as cancelled but not abort the agent', async () => {
      mockExecutorInstance.run.mockImplementation(async () => {
        const onActivity = MockLocalAgentExecutor.create.mock.calls[0][2];

        if (onActivity) {
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'TOOL_CALL_START',
            data: { name: 'ls', args: {}, callId: 'call1' },
          } as SubagentActivityEvent);
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'ERROR',
            data: {
              name: 'ls',
              callId: 'call1',
              error: `${SUBAGENT_REJECTED_ERROR_PREFIX} Please acknowledge this, rethink your strategy, and try a different approach. If you cannot proceed without the rejected operation, summarize the issue and use \`complete_task\` to report your findings and the blocker.`,
              errorType: SubagentActivityErrorType.REJECTED,
            },
          } as SubagentActivityEvent);
        }
        return {
          result: 'Rethinking...',
          terminate_reason: AgentTerminateMode.GOAL,
        };
      });

      await invocation.execute({ abortSignal: signal, updateOutput });

      expect(updateOutput).toHaveBeenCalledTimes(4);
      const lastCall = updateOutput.mock.calls[3][0] as SubagentProgress;
      expect(lastCall.recentActivity).toContainEqual(
        expect.objectContaining({
          type: 'tool_call',
          content: 'ls',
          status: SubagentState.CANCELLED,
        }),
      );
    });

    it('should run successfully without an updateOutput callback', async () => {
      mockExecutorInstance.run.mockImplementation(async () => {
        const onActivity = MockLocalAgentExecutor.create.mock.calls[0][2];
        if (onActivity) {
          // Ensure calling activity doesn't crash when updateOutput is undefined
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'testAgent',
            type: 'THOUGHT_CHUNK',
            data: { text: 'Thinking silently.' },
          } as SubagentActivityEvent);
        }
        return { result: 'Done', terminate_reason: AgentTerminateMode.GOAL };
      });

      // Execute without the optional callback
      const result = await invocation.execute({ abortSignal: signal });
      expect(result.error).toBeUndefined();
      const display = result.returnDisplay as SubagentProgress;
      expect(display.isSubagentProgress).toBe(true);
      expect(display.state).toBe(SubagentState.COMPLETED);
      expect(display.result).toBe('Done');
    });

    it('should handle executor run failure', async () => {
      const error = new Error('Model failed during execution.');
      mockExecutorInstance.run.mockRejectedValue(error);

      const result = await invocation.execute({
        abortSignal: signal,
        updateOutput,
      });

      expect(result.error).toBeUndefined();
      expect(result.llmContent).toBe(
        `Subagent 'MockAgent' failed. Error: ${error.message}`,
      );
      const display = result.returnDisplay as SubagentProgress;
      expect(display.isSubagentProgress).toBe(true);
      expect(display.recentActivity).toContainEqual(
        expect.objectContaining({
          type: 'thought',
          content: `Error: ${error.message}`,
          status: SubagentState.ERROR,
        }),
      );
    });

    it('should handle executor creation failure', async () => {
      const creationError = new Error('Failed to initialize tools.');
      MockLocalAgentExecutor.create.mockRejectedValue(creationError);

      const result = await invocation.execute({
        abortSignal: signal,
        updateOutput,
      });

      expect(mockExecutorInstance.run).not.toHaveBeenCalled();
      expect(result.error).toBeUndefined();
      expect(result.llmContent).toContain(creationError.message);

      const display = result.returnDisplay as SubagentProgress;
      expect(display.recentActivity).toContainEqual(
        expect.objectContaining({
          content: `Error: ${creationError.message}`,
          status: SubagentState.ERROR,
        }),
      );
    });

    it('should handle abortion signal during execution', async () => {
      const abortError = new Error('Aborted');
      abortError.name = 'AbortError';
      mockExecutorInstance.run.mockRejectedValue(abortError);

      const controller = new AbortController();
      const executePromise = invocation.execute({
        abortSignal: controller.signal,
        updateOutput,
      });
      controller.abort();
      await expect(executePromise).rejects.toThrow('Aborted');

      expect(mockExecutorInstance.run).toHaveBeenCalledWith(
        params,
        controller.signal,
      );
    });

    it('should throw an error and bubble cancellation when execution returns ABORTED', async () => {
      const mockOutput = {
        result: 'Cancelled by user',
        terminate_reason: AgentTerminateMode.ABORTED,
      };
      mockExecutorInstance.run.mockResolvedValue(mockOutput);

      await expect(
        invocation.execute({ abortSignal: signal, updateOutput }),
      ).rejects.toThrow('Operation cancelled by user');
    });

    it('should publish SUBAGENT_ACTIVITY events to the MessageBus', async () => {
      const { MessageBusType } = await import('../confirmation-bus/types.js');

      mockExecutorInstance.run.mockImplementation(async () => {
        const onActivity = MockLocalAgentExecutor.create.mock.calls[0][2];

        if (onActivity) {
          onActivity({
            isSubagentActivityEvent: true,
            agentName: 'MockAgent',
            type: 'THOUGHT_CHUNK',
            data: { text: 'Thinking...' },
          } as SubagentActivityEvent);
        }
        return { result: 'Done', terminate_reason: AgentTerminateMode.GOAL };
      });

      await invocation.execute({ abortSignal: signal, updateOutput });

      expect(mockMessageBus.publish).toHaveBeenCalledWith(
        expect.objectContaining({
          type: MessageBusType.SUBAGENT_ACTIVITY,
          subagentName: 'Mock Agent',
          activity: expect.objectContaining({
            type: 'thought',
            content: 'Thinking...',
          }),
        }),
      );
    });
  });
});