forked from ZeenSong/claude-code
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAgentTool.tsx
More file actions
1398 lines (1320 loc) · 228 KB
/
AgentTool.tsx
File metadata and controls
1398 lines (1320 loc) · 228 KB
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { feature } from 'bun:bundle';
import * as React from 'react';
import { buildTool, type ToolDef, toolMatchesName } from 'src/Tool.js';
import type { Message as MessageType, NormalizedUserMessage } from 'src/types/message.js';
import { getQuerySourceForAgent } from 'src/utils/promptCategory.js';
import { z } from 'zod/v4';
import { clearInvokedSkillsForAgent, getSdkAgentProgressSummariesEnabled } from '../../bootstrap/state.js';
import { enhanceSystemPromptWithEnvDetails, getSystemPrompt } from '../../constants/prompts.js';
import { isCoordinatorMode } from '../../coordinator/coordinatorMode.js';
import { startAgentSummarization } from '../../services/AgentSummary/agentSummary.js';
import { getFeatureValue_CACHED_MAY_BE_STALE } from '../../services/analytics/growthbook.js';
import { type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, logEvent } from '../../services/analytics/index.js';
import { clearDumpState } from '../../services/api/dumpPrompts.js';
import { completeAgentTask as completeAsyncAgent, createActivityDescriptionResolver, createProgressTracker, enqueueAgentNotification, failAgentTask as failAsyncAgent, getProgressUpdate, getTokenCountFromTracker, isLocalAgentTask, killAsyncAgent, registerAgentForeground, registerAsyncAgent, unregisterAgentForeground, updateAgentProgress as updateAsyncAgentProgress, updateProgressFromMessage } from '../../tasks/LocalAgentTask/LocalAgentTask.js';
import { checkRemoteAgentEligibility, formatPreconditionError, getRemoteTaskSessionUrl, registerRemoteAgentTask } from '../../tasks/RemoteAgentTask/RemoteAgentTask.js';
import { assembleToolPool } from '../../tools.js';
import { asAgentId } from '../../types/ids.js';
import { runWithAgentContext } from '../../utils/agentContext.js';
import { isAgentSwarmsEnabled } from '../../utils/agentSwarmsEnabled.js';
import { getCwd, runWithCwdOverride } from '../../utils/cwd.js';
import { logForDebugging } from '../../utils/debug.js';
import { isEnvTruthy } from '../../utils/envUtils.js';
import { AbortError, errorMessage, toError } from '../../utils/errors.js';
import type { CacheSafeParams } from '../../utils/forkedAgent.js';
import { lazySchema } from '../../utils/lazySchema.js';
import { createUserMessage, extractTextContent, isSyntheticMessage, normalizeMessages } from '../../utils/messages.js';
import { getAgentModel } from '../../utils/model/agent.js';
import { permissionModeSchema } from '../../utils/permissions/PermissionMode.js';
import type { PermissionResult } from '../../utils/permissions/PermissionResult.js';
import { filterDeniedAgents, getDenyRuleForAgent } from '../../utils/permissions/permissions.js';
import { enqueueSdkEvent } from '../../utils/sdkEventQueue.js';
import { writeAgentMetadata } from '../../utils/sessionStorage.js';
import { sleep } from '../../utils/sleep.js';
import { buildEffectiveSystemPrompt } from '../../utils/systemPrompt.js';
import { asSystemPrompt } from '../../utils/systemPromptType.js';
import { getTaskOutputPath } from '../../utils/task/diskOutput.js';
import { getParentSessionId, isTeammate } from '../../utils/teammate.js';
import { isInProcessTeammate } from '../../utils/teammateContext.js';
import { teleportToRemote } from '../../utils/teleport.js';
import { getAssistantMessageContentLength } from '../../utils/tokens.js';
import { createAgentId } from '../../utils/uuid.js';
import { createAgentWorktree, hasWorktreeChanges, removeAgentWorktree } from '../../utils/worktree.js';
import { BASH_TOOL_NAME } from '../BashTool/toolName.js';
import { BackgroundHint } from '../BashTool/UI.js';
import { FILE_READ_TOOL_NAME } from '../FileReadTool/prompt.js';
import { spawnTeammate } from '../shared/spawnMultiAgent.js';
import { setAgentColor } from './agentColorManager.js';
import { agentToolResultSchema, classifyHandoffIfNeeded, emitTaskProgress, extractPartialResult, finalizeAgentTool, getLastToolUseName, runAsyncAgentLifecycle } from './agentToolUtils.js';
import { GENERAL_PURPOSE_AGENT } from './built-in/generalPurposeAgent.js';
import { AGENT_TOOL_NAME, LEGACY_AGENT_TOOL_NAME, ONE_SHOT_BUILTIN_AGENT_TYPES } from './constants.js';
import { buildForkedMessages, buildWorktreeNotice, FORK_AGENT, isForkSubagentEnabled, isInForkChild } from './forkSubagent.js';
import type { AgentDefinition } from './loadAgentsDir.js';
import { filterAgentsByMcpRequirements, hasRequiredMcpServers, isBuiltInAgent } from './loadAgentsDir.js';
import { getPrompt } from './prompt.js';
import { runAgent } from './runAgent.js';
import { renderGroupedAgentToolUse, renderToolResultMessage, renderToolUseErrorMessage, renderToolUseMessage, renderToolUseProgressMessage, renderToolUseRejectedMessage, renderToolUseTag, userFacingName, userFacingNameBackgroundColor } from './UI.js';
/* eslint-disable @typescript-eslint/no-require-imports */
const proactiveModule = feature('PROACTIVE') || feature('KAIROS') ? require('../../proactive/index.js') as typeof import('../../proactive/index.js') : null;
/* eslint-enable @typescript-eslint/no-require-imports */
// Progress display constants (for showing background hint)
const PROGRESS_THRESHOLD_MS = 2000; // Show background hint after 2 seconds
// Check if background tasks are disabled at module load time
const isBackgroundTasksDisabled =
// eslint-disable-next-line custom-rules/no-process-env-top-level -- Intentional: schema must be defined at module load
isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_BACKGROUND_TASKS);
// Auto-background agent tasks after this many ms (0 = disabled)
// Enabled by env var OR GrowthBook gate (checked lazily since GB may not be ready at module load)
function getAutoBackgroundMs(): number {
if (isEnvTruthy(process.env.CLAUDE_AUTO_BACKGROUND_TASKS) || getFeatureValue_CACHED_MAY_BE_STALE('tengu_auto_background_agents', false)) {
return 120_000;
}
return 0;
}
// Multi-agent type constants are defined inline inside gated blocks to enable dead code elimination
// Base input schema without multi-agent parameters
const baseInputSchema = lazySchema(() => z.object({
description: z.string().describe('A short (3-5 word) description of the task'),
prompt: z.string().describe('The task for the agent to perform'),
subagent_type: z.string().optional().describe('The type of specialized agent to use for this task'),
model: z.enum(['sonnet', 'opus', 'haiku']).optional().describe("Optional model override for this agent. Takes precedence over the agent definition's model frontmatter. If omitted, uses the agent definition's model, or inherits from the parent."),
run_in_background: z.boolean().optional().describe('Set to true to run this agent in the background. You will be notified when it completes.')
}));
// Full schema combining base + multi-agent params + isolation
const fullInputSchema = lazySchema(() => {
// Multi-agent parameters
const multiAgentInputSchema = z.object({
name: z.string().optional().describe('Name for the spawned agent. Makes it addressable via SendMessage({to: name}) while running.'),
team_name: z.string().optional().describe('Team name for spawning. Uses current team context if omitted.'),
mode: permissionModeSchema().optional().describe('Permission mode for spawned teammate (e.g., "plan" to require plan approval).')
});
return baseInputSchema().merge(multiAgentInputSchema).extend({
isolation: ("external" === 'ant' ? z.enum(['worktree', 'remote']) : z.enum(['worktree'])).optional().describe("external" === 'ant' ? 'Isolation mode. "worktree" creates a temporary git worktree so the agent works on an isolated copy of the repo. "remote" launches the agent in a remote CCR environment (always runs in background).' : 'Isolation mode. "worktree" creates a temporary git worktree so the agent works on an isolated copy of the repo.'),
cwd: z.string().optional().describe('Absolute path to run the agent in. Overrides the working directory for all filesystem and shell operations within this agent. Mutually exclusive with isolation: "worktree".')
});
});
// Strip optional fields from the schema when the backing feature is off so
// the model never sees them. Done via .omit() rather than conditional spread
// inside .extend() because the spread-ternary breaks Zod's type inference
// (field type collapses to `unknown`). The ternary return produces a union
// type, but call() destructures via the explicit AgentToolInput type below
// which always includes all optional fields.
export const inputSchema = lazySchema(() => {
const schema = feature('KAIROS') ? fullInputSchema() : fullInputSchema().omit({
cwd: true
});
// GrowthBook-in-lazySchema is acceptable here (unlike subagent_type, which
// was removed in 906da6c723): the divergence window is one-session-per-
// gate-flip via _CACHED_MAY_BE_STALE disk read, and worst case is either
// "schema shows a no-op param" (gate flips on mid-session: param ignored
// by forceAsync) or "schema hides a param that would've worked" (gate
// flips off mid-session: everything still runs async via memoized
// forceAsync). No Zod rejection, no crash — unlike required→optional.
return isBackgroundTasksDisabled || isForkSubagentEnabled() ? schema.omit({
run_in_background: true
}) : schema;
});
type InputSchema = ReturnType<typeof inputSchema>;
// Explicit type widens the schema inference to always include all optional
// fields even when .omit() strips them for gating (cwd, run_in_background).
// subagent_type is optional; call() defaults it to general-purpose when the
// fork gate is off, or routes to the fork path when the gate is on.
type AgentToolInput = z.infer<ReturnType<typeof baseInputSchema>> & {
name?: string;
team_name?: string;
mode?: z.infer<ReturnType<typeof permissionModeSchema>>;
isolation?: 'worktree' | 'remote';
cwd?: string;
};
// Output schema - multi-agent spawned schema added dynamically at runtime when enabled
export const outputSchema = lazySchema(() => {
const syncOutputSchema = agentToolResultSchema().extend({
status: z.literal('completed'),
prompt: z.string()
});
const asyncOutputSchema = z.object({
status: z.literal('async_launched'),
agentId: z.string().describe('The ID of the async agent'),
description: z.string().describe('The description of the task'),
prompt: z.string().describe('The prompt for the agent'),
outputFile: z.string().describe('Path to the output file for checking agent progress'),
canReadOutputFile: z.boolean().optional().describe('Whether the calling agent has Read/Bash tools to check progress')
});
return z.union([syncOutputSchema, asyncOutputSchema]);
});
type OutputSchema = ReturnType<typeof outputSchema>;
type Output = z.input<OutputSchema>;
// Private type for teammate spawn results - excluded from exported schema for dead code elimination
// The 'teammate_spawned' status string is only included when ENABLE_AGENT_SWARMS is true
type TeammateSpawnedOutput = {
status: 'teammate_spawned';
prompt: string;
teammate_id: string;
agent_id: string;
agent_type?: string;
model?: string;
name: string;
color?: string;
tmux_session_name: string;
tmux_window_name: string;
tmux_pane_id: string;
team_name?: string;
is_splitpane?: boolean;
plan_mode_required?: boolean;
};
// Combined output type including both public and internal types
// Note: TeammateSpawnedOutput type is fine - TypeScript types are erased at compile time
// Private type for remote-launched results — excluded from exported schema
// like TeammateSpawnedOutput for dead code elimination purposes. Exported
// for UI.tsx to do proper discriminated-union narrowing instead of ad-hoc casts.
export type RemoteLaunchedOutput = {
status: 'remote_launched';
taskId: string;
sessionUrl: string;
description: string;
prompt: string;
outputFile: string;
};
type InternalOutput = Output | TeammateSpawnedOutput | RemoteLaunchedOutput;
import type { AgentToolProgress, ShellProgress } from '../../types/tools.js';
// AgentTool forwards both its own progress events and shell progress
// events from the sub-agent so the SDK receives tool_progress updates during bash/powershell runs.
export type Progress = AgentToolProgress | ShellProgress;
export const AgentTool = buildTool({
async prompt({
agents,
tools,
getToolPermissionContext,
allowedAgentTypes
}) {
const toolPermissionContext = await getToolPermissionContext();
// Get MCP servers that have tools available
const mcpServersWithTools: string[] = [];
for (const tool of tools) {
if (tool.name?.startsWith('mcp__')) {
const parts = tool.name.split('__');
const serverName = parts[1];
if (serverName && !mcpServersWithTools.includes(serverName)) {
mcpServersWithTools.push(serverName);
}
}
}
// Filter agents: first by MCP requirements, then by permission rules
const agentsWithMcpRequirementsMet = filterAgentsByMcpRequirements(agents, mcpServersWithTools);
const filteredAgents = filterDeniedAgents(agentsWithMcpRequirementsMet, toolPermissionContext, AGENT_TOOL_NAME);
// Use inline env check instead of coordinatorModule to avoid circular
// dependency issues during test module loading.
const isCoordinator = feature('COORDINATOR_MODE') ? isEnvTruthy(process.env.CLAUDE_CODE_COORDINATOR_MODE) : false;
return await getPrompt(filteredAgents, isCoordinator, allowedAgentTypes);
},
name: AGENT_TOOL_NAME,
searchHint: 'delegate work to a subagent',
aliases: [LEGACY_AGENT_TOOL_NAME],
maxResultSizeChars: 100_000,
async description() {
return 'Launch a new agent';
},
get inputSchema(): InputSchema {
return inputSchema();
},
get outputSchema(): OutputSchema {
return outputSchema();
},
async call({
prompt,
subagent_type,
description,
model: modelParam,
run_in_background,
name,
team_name,
mode: spawnMode,
isolation,
cwd
}: AgentToolInput, toolUseContext, canUseTool, assistantMessage, onProgress?) {
const startTime = Date.now();
const model = isCoordinatorMode() ? undefined : modelParam;
// Get app state for permission mode and agent filtering
const appState = toolUseContext.getAppState();
const permissionMode = appState.toolPermissionContext.mode;
// In-process teammates get a no-op setAppState; setAppStateForTasks
// reaches the root store so task registration/progress/kill stay visible.
const rootSetAppState = toolUseContext.setAppStateForTasks ?? toolUseContext.setAppState;
// Check if user is trying to use agent teams without access
if (team_name && !isAgentSwarmsEnabled()) {
throw new Error('Agent Teams is not yet available on your plan.');
}
// Teammates (in-process or tmux) passing `name` would trigger spawnTeammate()
// below, but TeamFile.members is a flat array with one leadAgentId — nested
// teammates land in the roster with no provenance and confuse the lead.
const teamName = resolveTeamName({
team_name
}, appState);
if (isTeammate() && teamName && name) {
throw new Error('Teammates cannot spawn other teammates — the team roster is flat. To spawn a subagent instead, omit the `name` parameter.');
}
// In-process teammates cannot spawn background agents (their lifecycle is
// tied to the leader's process). Tmux teammates are separate processes and
// can manage their own background agents.
if (isInProcessTeammate() && teamName && run_in_background === true) {
throw new Error('In-process teammates cannot spawn background agents. Use run_in_background=false for synchronous subagents.');
}
// Check if this is a multi-agent spawn request
// Spawn is triggered when team_name is set (from param or context) and name is provided
if (teamName && name) {
// Set agent definition color for grouped UI display before spawning
const agentDef = subagent_type ? toolUseContext.options.agentDefinitions.activeAgents.find(a => a.agentType === subagent_type) : undefined;
if (agentDef?.color) {
setAgentColor(subagent_type!, agentDef.color);
}
const result = await spawnTeammate({
name,
prompt,
description,
team_name: teamName,
use_splitpane: true,
plan_mode_required: spawnMode === 'plan',
model: model ?? agentDef?.model,
agent_type: subagent_type,
invokingRequestId: assistantMessage?.requestId
}, toolUseContext);
// Type assertion uses TeammateSpawnedOutput (defined above) instead of any.
// This type is excluded from the exported outputSchema for dead code elimination.
// Cast through unknown because TeammateSpawnedOutput is intentionally
// not part of the exported Output union (for dead code elimination purposes).
const spawnResult: TeammateSpawnedOutput = {
status: 'teammate_spawned' as const,
prompt,
...result.data
};
return {
data: spawnResult
} as unknown as {
data: Output;
};
}
// Fork subagent experiment routing:
// - subagent_type set: use it (explicit wins)
// - subagent_type omitted, gate on: fork path (undefined)
// - subagent_type omitted, gate off: default general-purpose
const effectiveType = subagent_type ?? (isForkSubagentEnabled() ? undefined : GENERAL_PURPOSE_AGENT.agentType);
const isForkPath = effectiveType === undefined;
let selectedAgent: AgentDefinition;
if (isForkPath) {
// Recursive fork guard: fork children keep the Agent tool in their
// pool for cache-identical tool defs, so reject fork attempts at call
// time. Primary check is querySource (compaction-resistant — set on
// context.options at spawn time, survives autocompact's message
// rewrite). Message-scan fallback catches any path where querySource
// wasn't threaded.
if (toolUseContext.options.querySource === `agent:builtin:${FORK_AGENT.agentType}` || isInForkChild(toolUseContext.messages)) {
throw new Error('Fork is not available inside a forked worker. Complete your task directly using your tools.');
}
selectedAgent = FORK_AGENT;
} else {
// Filter agents to exclude those denied via Agent(AgentName) syntax
const allAgents = toolUseContext.options.agentDefinitions.activeAgents;
const {
allowedAgentTypes
} = toolUseContext.options.agentDefinitions;
const agents = filterDeniedAgents(
// When allowedAgentTypes is set (from Agent(x,y) tool spec), restrict to those types
allowedAgentTypes ? allAgents.filter(a => allowedAgentTypes.includes(a.agentType)) : allAgents, appState.toolPermissionContext, AGENT_TOOL_NAME);
const found = agents.find(agent => agent.agentType === effectiveType);
if (!found) {
// Check if the agent exists but is denied by permission rules
const agentExistsButDenied = allAgents.find(agent => agent.agentType === effectiveType);
if (agentExistsButDenied) {
const denyRule = getDenyRuleForAgent(appState.toolPermissionContext, AGENT_TOOL_NAME, effectiveType);
throw new Error(`Agent type '${effectiveType}' has been denied by permission rule '${AGENT_TOOL_NAME}(${effectiveType})' from ${denyRule?.source ?? 'settings'}.`);
}
throw new Error(`Agent type '${effectiveType}' not found. Available agents: ${agents.map(a => a.agentType).join(', ')}`);
}
selectedAgent = found;
}
// Same lifecycle constraint as the run_in_background guard above, but for
// agent definitions that force background via `background: true`. Checked
// here because selectedAgent is only now resolved.
if (isInProcessTeammate() && teamName && selectedAgent.background === true) {
throw new Error(`In-process teammates cannot spawn background agents. Agent '${selectedAgent.agentType}' has background: true in its definition.`);
}
// Capture for type narrowing — `let selectedAgent` prevents TS from
// narrowing property types across the if-else assignment above.
const requiredMcpServers = selectedAgent.requiredMcpServers;
// Check if required MCP servers have tools available
// A server that's connected but not authenticated won't have any tools
if (requiredMcpServers?.length) {
// If any required servers are still pending (connecting), wait for them
// before checking tool availability. This avoids a race condition where
// the agent is invoked before MCP servers finish connecting.
const hasPendingRequiredServers = appState.mcp.clients.some(c => c.type === 'pending' && requiredMcpServers.some(pattern => c.name.toLowerCase().includes(pattern.toLowerCase())));
let currentAppState = appState;
if (hasPendingRequiredServers) {
const MAX_WAIT_MS = 30_000;
const POLL_INTERVAL_MS = 500;
const deadline = Date.now() + MAX_WAIT_MS;
while (Date.now() < deadline) {
await sleep(POLL_INTERVAL_MS);
currentAppState = toolUseContext.getAppState();
// Early exit: if any required server has already failed, no point
// waiting for other pending servers — the check will fail regardless.
const hasFailedRequiredServer = currentAppState.mcp.clients.some(c => c.type === 'failed' && requiredMcpServers.some(pattern => c.name.toLowerCase().includes(pattern.toLowerCase())));
if (hasFailedRequiredServer) break;
const stillPending = currentAppState.mcp.clients.some(c => c.type === 'pending' && requiredMcpServers.some(pattern => c.name.toLowerCase().includes(pattern.toLowerCase())));
if (!stillPending) break;
}
}
// Get servers that actually have tools (meaning they're connected AND authenticated)
const serversWithTools: string[] = [];
for (const tool of currentAppState.mcp.tools) {
if (tool.name?.startsWith('mcp__')) {
// Extract server name from tool name (format: mcp__serverName__toolName)
const parts = tool.name.split('__');
const serverName = parts[1];
if (serverName && !serversWithTools.includes(serverName)) {
serversWithTools.push(serverName);
}
}
}
if (!hasRequiredMcpServers(selectedAgent, serversWithTools)) {
const missing = requiredMcpServers.filter(pattern => !serversWithTools.some(server => server.toLowerCase().includes(pattern.toLowerCase())));
throw new Error(`Agent '${selectedAgent.agentType}' requires MCP servers matching: ${missing.join(', ')}. ` + `MCP servers with tools: ${serversWithTools.length > 0 ? serversWithTools.join(', ') : 'none'}. ` + `Use /mcp to configure and authenticate the required MCP servers.`);
}
}
// Initialize the color for this agent if it has a predefined one
if (selectedAgent.color) {
setAgentColor(selectedAgent.agentType, selectedAgent.color);
}
// Resolve agent params for logging (these are already resolved in runAgent)
const resolvedAgentModel = getAgentModel(selectedAgent.model, toolUseContext.options.mainLoopModel, isForkPath ? undefined : model, permissionMode);
logEvent('tengu_agent_tool_selected', {
agent_type: selectedAgent.agentType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
model: resolvedAgentModel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
source: selectedAgent.source as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
color: selectedAgent.color as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
is_built_in_agent: isBuiltInAgent(selectedAgent),
is_resume: false,
is_async: (run_in_background === true || selectedAgent.background === true) && !isBackgroundTasksDisabled,
is_fork: isForkPath
});
// Resolve effective isolation mode (explicit param overrides agent def)
const effectiveIsolation = isolation ?? selectedAgent.isolation;
// Remote isolation: delegate to CCR. Gated ant-only — the guard enables
// dead code elimination of the entire block for external builds.
if ("external" === 'ant' && effectiveIsolation === 'remote') {
const eligibility = await checkRemoteAgentEligibility();
if (!eligibility.eligible) {
const reasons = eligibility.errors.map(formatPreconditionError).join('\n');
throw new Error(`Cannot launch remote agent:\n${reasons}`);
}
let bundleFailHint: string | undefined;
const session = await teleportToRemote({
initialMessage: prompt,
description,
signal: toolUseContext.abortController.signal,
onBundleFail: msg => {
bundleFailHint = msg;
}
});
if (!session) {
throw new Error(bundleFailHint ?? 'Failed to create remote session');
}
const {
taskId,
sessionId
} = registerRemoteAgentTask({
remoteTaskType: 'remote-agent',
session: {
id: session.id,
title: session.title || description
},
command: prompt,
context: toolUseContext,
toolUseId: toolUseContext.toolUseId
});
logEvent('tengu_agent_tool_remote_launched', {
agent_type: selectedAgent.agentType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
});
const remoteResult: RemoteLaunchedOutput = {
status: 'remote_launched',
taskId,
sessionUrl: getRemoteTaskSessionUrl(sessionId),
description,
prompt,
outputFile: getTaskOutputPath(taskId)
};
return {
data: remoteResult
} as unknown as {
data: Output;
};
}
// System prompt + prompt messages: branch on fork path.
//
// Fork path: child inherits the PARENT's system prompt (not FORK_AGENT's)
// for cache-identical API request prefixes. Prompt messages are built via
// buildForkedMessages() which clones the parent's full assistant message
// (all tool_use blocks) + placeholder tool_results + per-child directive.
//
// Normal path: build the selected agent's own system prompt with env
// details, and use a simple user message for the prompt.
let enhancedSystemPrompt: string[] | undefined;
let forkParentSystemPrompt: ReturnType<typeof buildEffectiveSystemPrompt> | undefined;
let promptMessages: MessageType[];
if (isForkPath) {
if (toolUseContext.renderedSystemPrompt) {
forkParentSystemPrompt = toolUseContext.renderedSystemPrompt;
} else {
// Fallback: recompute. May diverge from parent's cached bytes if
// GrowthBook state changed between parent turn-start and fork spawn.
const mainThreadAgentDefinition = appState.agent ? appState.agentDefinitions.activeAgents.find(a => a.agentType === appState.agent) : undefined;
const additionalWorkingDirectories = Array.from(appState.toolPermissionContext.additionalWorkingDirectories.keys());
const defaultSystemPrompt = await getSystemPrompt(toolUseContext.options.tools, toolUseContext.options.mainLoopModel, additionalWorkingDirectories, toolUseContext.options.mcpClients);
forkParentSystemPrompt = buildEffectiveSystemPrompt({
mainThreadAgentDefinition,
toolUseContext,
customSystemPrompt: toolUseContext.options.customSystemPrompt,
defaultSystemPrompt,
appendSystemPrompt: toolUseContext.options.appendSystemPrompt
});
}
promptMessages = buildForkedMessages(prompt, assistantMessage);
} else {
try {
const additionalWorkingDirectories = Array.from(appState.toolPermissionContext.additionalWorkingDirectories.keys());
// All agents have getSystemPrompt - pass toolUseContext to all
const agentPrompt = selectedAgent.getSystemPrompt({
toolUseContext
});
// Log agent memory loaded event for subagents
if (selectedAgent.memory) {
logEvent('tengu_agent_memory_loaded', {
...("external" === 'ant' && {
agent_type: selectedAgent.agentType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
}),
scope: selectedAgent.memory as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
source: 'subagent' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
});
}
// Apply environment details enhancement
enhancedSystemPrompt = await enhanceSystemPromptWithEnvDetails([agentPrompt], resolvedAgentModel, additionalWorkingDirectories);
} catch (error) {
logForDebugging(`Failed to get system prompt for agent ${selectedAgent.agentType}: ${errorMessage(error)}`);
}
promptMessages = [createUserMessage({
content: prompt
})];
}
const metadata = {
prompt,
resolvedAgentModel,
isBuiltInAgent: isBuiltInAgent(selectedAgent),
startTime,
agentType: selectedAgent.agentType,
isAsync: (run_in_background === true || selectedAgent.background === true) && !isBackgroundTasksDisabled
};
// Use inline env check instead of coordinatorModule to avoid circular
// dependency issues during test module loading.
const isCoordinator = feature('COORDINATOR_MODE') ? isEnvTruthy(process.env.CLAUDE_CODE_COORDINATOR_MODE) : false;
// Fork subagent experiment: force ALL spawns async for a unified
// <task-notification> interaction model (not just fork spawns — all of them).
const forceAsync = isForkSubagentEnabled();
// Assistant mode: force all agents async. Synchronous subagents hold the
// main loop's turn open until they complete — the daemon's inputQueue
// backs up, and the first overdue cron catch-up on spawn becomes N
// serial subagent turns blocking all user input. Same gate as
// executeForkedSlashCommand's fire-and-forget path; the
// <task-notification> re-entry there is handled by the else branch
// below (registerAsyncAgentTask + notifyOnCompletion).
const assistantForceAsync = feature('KAIROS') ? appState.kairosEnabled : false;
const shouldRunAsync = (run_in_background === true || selectedAgent.background === true || isCoordinator || forceAsync || assistantForceAsync || (proactiveModule?.isProactiveActive() ?? false)) && !isBackgroundTasksDisabled;
// Assemble the worker's tool pool independently of the parent's.
// Workers always get their tools from assembleToolPool with their own
// permission mode, so they aren't affected by the parent's tool
// restrictions. This is computed here so that runAgent doesn't need to
// import from tools.ts (which would create a circular dependency).
const workerPermissionContext = {
...appState.toolPermissionContext,
mode: selectedAgent.permissionMode ?? 'acceptEdits'
};
const workerTools = assembleToolPool(workerPermissionContext, appState.mcp.tools);
// Create a stable agent ID early so it can be used for worktree slug
const earlyAgentId = createAgentId();
// Set up worktree isolation if requested
let worktreeInfo: {
worktreePath: string;
worktreeBranch?: string;
headCommit?: string;
gitRoot?: string;
hookBased?: boolean;
} | null = null;
if (effectiveIsolation === 'worktree') {
const slug = `agent-${earlyAgentId.slice(0, 8)}`;
worktreeInfo = await createAgentWorktree(slug);
}
// Fork + worktree: inject a notice telling the child to translate paths
// and re-read potentially stale files. Appended after the fork directive
// so it appears as the most recent guidance the child sees.
if (isForkPath && worktreeInfo) {
promptMessages.push(createUserMessage({
content: buildWorktreeNotice(getCwd(), worktreeInfo.worktreePath)
}));
}
const runAgentParams: Parameters<typeof runAgent>[0] = {
agentDefinition: selectedAgent,
promptMessages,
toolUseContext,
canUseTool,
isAsync: shouldRunAsync,
querySource: toolUseContext.options.querySource ?? getQuerySourceForAgent(selectedAgent.agentType, isBuiltInAgent(selectedAgent)),
model: isForkPath ? undefined : model,
// Fork path: pass parent's system prompt AND parent's exact tool
// array (cache-identical prefix). workerTools is rebuilt under
// permissionMode 'bubble' which differs from the parent's mode, so
// its tool-def serialization diverges and breaks cache at the first
// differing tool. useExactTools also inherits the parent's
// thinkingConfig and isNonInteractiveSession (see runAgent.ts).
//
// Normal path: when a cwd override is in effect (worktree isolation
// or explicit cwd), skip the pre-built system prompt so runAgent's
// buildAgentSystemPrompt() runs inside wrapWithCwd where getCwd()
// returns the override path.
override: isForkPath ? {
systemPrompt: forkParentSystemPrompt
} : enhancedSystemPrompt && !worktreeInfo && !cwd ? {
systemPrompt: asSystemPrompt(enhancedSystemPrompt)
} : undefined,
availableTools: isForkPath ? toolUseContext.options.tools : workerTools,
// Pass parent conversation when the fork-subagent path needs full
// context. useExactTools inherits thinkingConfig (runAgent.ts:624).
forkContextMessages: isForkPath ? toolUseContext.messages : undefined,
...(isForkPath && {
useExactTools: true
}),
worktreePath: worktreeInfo?.worktreePath,
description
};
// Helper to wrap execution with a cwd override: explicit cwd arg (KAIROS)
// takes precedence over worktree isolation path.
const cwdOverridePath = cwd ?? worktreeInfo?.worktreePath;
const wrapWithCwd = <T,>(fn: () => T): T => cwdOverridePath ? runWithCwdOverride(cwdOverridePath, fn) : fn();
// Helper to clean up worktree after agent completes
const cleanupWorktreeIfNeeded = async (): Promise<{
worktreePath?: string;
worktreeBranch?: string;
}> => {
if (!worktreeInfo) return {};
const {
worktreePath,
worktreeBranch,
headCommit,
gitRoot,
hookBased
} = worktreeInfo;
// Null out to make idempotent — guards against double-call if code
// between cleanup and end of try throws into catch
worktreeInfo = null;
if (hookBased) {
// Hook-based worktrees are always kept since we can't detect VCS changes
logForDebugging(`Hook-based agent worktree kept at: ${worktreePath}`);
return {
worktreePath
};
}
if (headCommit) {
const changed = await hasWorktreeChanges(worktreePath, headCommit);
if (!changed) {
await removeAgentWorktree(worktreePath, worktreeBranch, gitRoot);
// Clear worktreePath from metadata so resume doesn't try to use
// a deleted directory. Fire-and-forget to match runAgent's
// writeAgentMetadata handling.
void writeAgentMetadata(asAgentId(earlyAgentId), {
agentType: selectedAgent.agentType,
description
}).catch(_err => logForDebugging(`Failed to clear worktree metadata: ${_err}`));
return {};
}
}
logForDebugging(`Agent worktree has changes, keeping: ${worktreePath}`);
return {
worktreePath,
worktreeBranch
};
};
if (shouldRunAsync) {
const asyncAgentId = earlyAgentId;
const agentBackgroundTask = registerAsyncAgent({
agentId: asyncAgentId,
description,
prompt,
selectedAgent,
setAppState: rootSetAppState,
// Don't link to parent's abort controller -- background agents should
// survive when the user presses ESC to cancel the main thread.
// They are killed explicitly via chat:killAgents.
toolUseId: toolUseContext.toolUseId
});
// Register name → agentId for SendMessage routing. Post-registerAsyncAgent
// so we don't leave a stale entry if spawn fails. Sync agents skipped —
// coordinator is blocked, so SendMessage routing doesn't apply.
if (name) {
rootSetAppState(prev => {
const next = new Map(prev.agentNameRegistry);
next.set(name, asAgentId(asyncAgentId));
return {
...prev,
agentNameRegistry: next
};
});
}
// Wrap async agent execution in agent context for analytics attribution
const asyncAgentContext = {
agentId: asyncAgentId,
// For subagents from teammates: use team lead's session
// For subagents from main REPL: undefined (no parent session)
parentSessionId: getParentSessionId(),
agentType: 'subagent' as const,
subagentName: selectedAgent.agentType,
isBuiltIn: isBuiltInAgent(selectedAgent),
invokingRequestId: assistantMessage?.requestId,
invocationKind: 'spawn' as const,
invocationEmitted: false
};
// Workload propagation: handlePromptSubmit wraps the entire turn in
// runWithWorkload (AsyncLocalStorage). ALS context is captured at
// invocation time — when this `void` fires — and survives every await
// inside. No capture/restore needed; the detached closure sees the
// parent turn's workload automatically, isolated from its finally.
void runWithAgentContext(asyncAgentContext, () => wrapWithCwd(() => runAsyncAgentLifecycle({
taskId: agentBackgroundTask.agentId,
abortController: agentBackgroundTask.abortController!,
makeStream: onCacheSafeParams => runAgent({
...runAgentParams,
override: {
...runAgentParams.override,
agentId: asAgentId(agentBackgroundTask.agentId),
abortController: agentBackgroundTask.abortController!
},
onCacheSafeParams
}),
metadata,
description,
toolUseContext,
rootSetAppState,
agentIdForCleanup: asyncAgentId,
enableSummarization: isCoordinator || isForkSubagentEnabled() || getSdkAgentProgressSummariesEnabled(),
getWorktreeResult: cleanupWorktreeIfNeeded
})));
const canReadOutputFile = toolUseContext.options.tools.some(t => toolMatchesName(t, FILE_READ_TOOL_NAME) || toolMatchesName(t, BASH_TOOL_NAME));
return {
data: {
isAsync: true as const,
status: 'async_launched' as const,
agentId: agentBackgroundTask.agentId,
description: description,
prompt: prompt,
outputFile: getTaskOutputPath(agentBackgroundTask.agentId),
canReadOutputFile
}
};
} else {
// Create an explicit agentId for sync agents
const syncAgentId = asAgentId(earlyAgentId);
// Set up agent context for sync execution (for analytics attribution)
const syncAgentContext = {
agentId: syncAgentId,
// For subagents from teammates: use team lead's session
// For subagents from main REPL: undefined (no parent session)
parentSessionId: getParentSessionId(),
agentType: 'subagent' as const,
subagentName: selectedAgent.agentType,
isBuiltIn: isBuiltInAgent(selectedAgent),
invokingRequestId: assistantMessage?.requestId,
invocationKind: 'spawn' as const,
invocationEmitted: false
};
// Wrap entire sync agent execution in context for analytics attribution
// and optionally in a worktree cwd override for filesystem isolation
return runWithAgentContext(syncAgentContext, () => wrapWithCwd(async () => {
const agentMessages: MessageType[] = [];
const agentStartTime = Date.now();
const syncTracker = createProgressTracker();
const syncResolveActivity = createActivityDescriptionResolver(toolUseContext.options.tools);
// Yield initial progress message to carry metadata (prompt)
if (promptMessages.length > 0) {
const normalizedPromptMessages = normalizeMessages(promptMessages);
const normalizedFirstMessage = normalizedPromptMessages.find((m): m is NormalizedUserMessage => m.type === 'user');
if (normalizedFirstMessage && normalizedFirstMessage.type === 'user' && onProgress) {
onProgress({
toolUseID: `agent_${assistantMessage.message.id}`,
data: {
message: normalizedFirstMessage,
type: 'agent_progress',
prompt,
agentId: syncAgentId
}
});
}
}
// Register as foreground task immediately so it can be backgrounded at any time
// Skip registration if background tasks are disabled
let foregroundTaskId: string | undefined;
// Create the background race promise once outside the loop — otherwise
// each iteration adds a new .then() reaction to the same pending
// promise, accumulating callbacks for the lifetime of the agent.
let backgroundPromise: Promise<{
type: 'background';
}> | undefined;
let cancelAutoBackground: (() => void) | undefined;
if (!isBackgroundTasksDisabled) {
const registration = registerAgentForeground({
agentId: syncAgentId,
description,
prompt,
selectedAgent,
setAppState: rootSetAppState,
toolUseId: toolUseContext.toolUseId,
autoBackgroundMs: getAutoBackgroundMs() || undefined
});
foregroundTaskId = registration.taskId;
backgroundPromise = registration.backgroundSignal.then(() => ({
type: 'background' as const
}));
cancelAutoBackground = registration.cancelAutoBackground;
}
// Track if we've shown the background hint UI
let backgroundHintShown = false;
// Track if the agent was backgrounded (cleanup handled by backgrounded finally)
let wasBackgrounded = false;
// Per-scope stop function — NOT shared with the backgrounded closure.
// idempotent: startAgentSummarization's stop() checks `stopped` flag.
let stopForegroundSummarization: (() => void) | undefined;
// const capture for sound type narrowing inside the callback below
const summaryTaskId = foregroundTaskId;
// Get async iterator for the agent
const agentIterator = runAgent({
...runAgentParams,
override: {
...runAgentParams.override,
agentId: syncAgentId
},
onCacheSafeParams: summaryTaskId && getSdkAgentProgressSummariesEnabled() ? (params: CacheSafeParams) => {
const {
stop
} = startAgentSummarization(summaryTaskId, syncAgentId, params, rootSetAppState);
stopForegroundSummarization = stop;
} : undefined
})[Symbol.asyncIterator]();
// Track if an error occurred during iteration
let syncAgentError: Error | undefined;
let wasAborted = false;
let worktreeResult: {
worktreePath?: string;
worktreeBranch?: string;
} = {};
try {
while (true) {
const elapsed = Date.now() - agentStartTime;
// Show background hint after threshold (but task is already registered)
// Skip if background tasks are disabled
if (!isBackgroundTasksDisabled && !backgroundHintShown && elapsed >= PROGRESS_THRESHOLD_MS && toolUseContext.setToolJSX) {
backgroundHintShown = true;
toolUseContext.setToolJSX({
jsx: <BackgroundHint />,
shouldHidePromptInput: false,
shouldContinueAnimation: true,
showSpinner: true
});
}
// Race between next message and background signal
// If background tasks are disabled, just await the next message directly
const nextMessagePromise = agentIterator.next();
const raceResult = backgroundPromise ? await Promise.race([nextMessagePromise.then(r => ({
type: 'message' as const,
result: r
})), backgroundPromise]) : {
type: 'message' as const,
result: await nextMessagePromise
};
// Check if we were backgrounded via backgroundAll()
// foregroundTaskId is guaranteed to be defined if raceResult.type is 'background'
// because backgroundPromise is only defined when foregroundTaskId is defined
if (raceResult.type === 'background' && foregroundTaskId) {
const appState = toolUseContext.getAppState();
const task = appState.tasks[foregroundTaskId];
if (isLocalAgentTask(task) && task.isBackgrounded) {
// Capture the taskId for use in the async callback
const backgroundedTaskId = foregroundTaskId;
wasBackgrounded = true;
// Stop foreground summarization; the backgrounded closure
// below owns its own independent stop function.
stopForegroundSummarization?.();
// Workload: inherited via ALS at `void` invocation time,
// same as the async-from-start path above.
// Continue agent in background and return async result
void runWithAgentContext(syncAgentContext, async () => {
let stopBackgroundedSummarization: (() => void) | undefined;
try {
// Clean up the foreground iterator so its finally block runs
// (releases MCP connections, session hooks, prompt cache tracking, etc.)
// Timeout prevents blocking if MCP server cleanup hangs.
// .catch() prevents unhandled rejection if timeout wins the race.
await Promise.race([agentIterator.return(undefined).catch(() => {}), sleep(1000)]);
// Initialize progress tracking from existing messages
const tracker = createProgressTracker();
const resolveActivity2 = createActivityDescriptionResolver(toolUseContext.options.tools);
for (const existingMsg of agentMessages) {
updateProgressFromMessage(tracker, existingMsg, resolveActivity2, toolUseContext.options.tools);
}
for await (const msg of runAgent({
...runAgentParams,
isAsync: true,
// Agent is now running in background
override: {
...runAgentParams.override,
agentId: asAgentId(backgroundedTaskId),
abortController: task.abortController
},
onCacheSafeParams: getSdkAgentProgressSummariesEnabled() ? (params: CacheSafeParams) => {
const {
stop
} = startAgentSummarization(backgroundedTaskId, asAgentId(backgroundedTaskId), params, rootSetAppState);
stopBackgroundedSummarization = stop;
} : undefined
})) {
agentMessages.push(msg);
// Track progress for backgrounded agents
updateProgressFromMessage(tracker, msg, resolveActivity2, toolUseContext.options.tools);
updateAsyncAgentProgress(backgroundedTaskId, getProgressUpdate(tracker), rootSetAppState);
const lastToolName = getLastToolUseName(msg);
if (lastToolName) {
emitTaskProgress(tracker, backgroundedTaskId, toolUseContext.toolUseId, description, startTime, lastToolName);
}
}
const agentResult = finalizeAgentTool(agentMessages, backgroundedTaskId, metadata);
// Mark task completed FIRST so TaskOutput(block=true)
// unblocks immediately. classifyHandoffIfNeeded and
// cleanupWorktreeIfNeeded can hang — they must not gate
// the status transition (gh-20236).
completeAsyncAgent(agentResult, rootSetAppState);
// Extract text from agent result content for the notification
let finalMessage = extractTextContent(agentResult.content, '\n');
if (feature('TRANSCRIPT_CLASSIFIER')) {
const backgroundedAppState = toolUseContext.getAppState();
const handoffWarning = await classifyHandoffIfNeeded({
agentMessages,
tools: toolUseContext.options.tools,
toolPermissionContext: backgroundedAppState.toolPermissionContext,
abortSignal: task.abortController!.signal,
subagentType: selectedAgent.agentType,
totalToolUseCount: agentResult.totalToolUseCount
});
if (handoffWarning) {
finalMessage = `${handoffWarning}\n\n${finalMessage}`;
}
}
// Clean up worktree before notification so we can include it
const worktreeResult = await cleanupWorktreeIfNeeded();
enqueueAgentNotification({
taskId: backgroundedTaskId,
description,
status: 'completed',
setAppState: rootSetAppState,
finalMessage,
usage: {
totalTokens: getTokenCountFromTracker(tracker),
toolUses: agentResult.totalToolUseCount,
durationMs: agentResult.totalDurationMs
},
toolUseId: toolUseContext.toolUseId,
...worktreeResult
});
} catch (error) {
if (error instanceof AbortError) {
// Transition status BEFORE worktree cleanup so
// TaskOutput unblocks even if git hangs (gh-20236).
killAsyncAgent(backgroundedTaskId, rootSetAppState);
logEvent('tengu_agent_tool_terminated', {
agent_type: metadata.agentType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
model: metadata.resolvedAgentModel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
duration_ms: Date.now() - metadata.startTime,