@esso0428/pi-subagents 0.17.15 → 0.17.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/ROADMAP.md +87 -0
- package/docs/post-0.17.6-agents-roster-viewer-spec.md +42 -0
- package/docs/post-0.17.6-durable-history-spec.md +41 -0
- package/docs/post-0.17.6-feature-specs.md +50 -0
- package/docs/post-0.17.6-nested-agents-spec.md +38 -0
- package/docs/post-0.17.6-recovery-shutdown-spec.md +46 -0
- package/docs/post-0.17.6-ui-latency-baseline-spec.md +46 -0
- package/docs/post-0.17.6-workflow-rpc-lifecycle-spec.md +54 -0
- package/package.json +2 -3
- package/src/agent-history.ts +2 -54
- package/src/agent-manager.ts +406 -1288
- package/src/agent-runner.ts +27 -251
- package/src/agent-types.ts +32 -188
- package/src/cross-extension-rpc.ts +20 -96
- package/src/custom-agents.ts +13 -170
- package/src/index.ts +480 -1920
- package/src/invocation-config.ts +3 -118
- package/src/model-resolver.ts +0 -18
- package/src/output-file.ts +6 -61
- package/src/prompts.ts +2 -45
- package/src/schedule.ts +14 -35
- package/src/settings.ts +2 -301
- package/src/status-note.ts +1 -66
- package/src/types.ts +10 -177
- package/src/ui/agent-widget.ts +57 -278
- package/src/ui/conversation-blocks.ts +0 -6
- package/src/ui/conversation-timeline.ts +25 -139
- package/src/ui/conversation-viewer.ts +48 -212
- package/src/ui/schedule-menu.ts +8 -9
- package/src/usage.ts +2 -109
- package/src/worktree.ts +55 -69
- package/dist/abortable.d.ts +0 -13
- package/dist/abortable.d.ts.map +0 -1
- package/dist/abortable.js +0 -43
- package/dist/abortable.js.map +0 -1
- package/dist/agent-color.d.ts +0 -36
- package/dist/agent-color.d.ts.map +0 -1
- package/dist/agent-color.js +0 -124
- package/dist/agent-color.js.map +0 -1
- package/dist/agent-file-toggle.d.ts +0 -126
- package/dist/agent-file-toggle.d.ts.map +0 -1
- package/dist/agent-file-toggle.js +0 -259
- package/dist/agent-file-toggle.js.map +0 -1
- package/dist/agent-history-list.d.ts +0 -19
- package/dist/agent-history-list.d.ts.map +0 -1
- package/dist/agent-history-list.js +0 -69
- package/dist/agent-history-list.js.map +0 -1
- package/dist/agent-history.d.ts +0 -35
- package/dist/agent-history.d.ts.map +0 -1
- package/dist/agent-history.js +0 -188
- package/dist/agent-history.js.map +0 -1
- package/dist/agent-manager.d.ts +0 -503
- package/dist/agent-manager.d.ts.map +0 -1
- package/dist/agent-manager.js +0 -1572
- package/dist/agent-manager.js.map +0 -1
- package/dist/agent-recovery.d.ts +0 -37
- package/dist/agent-recovery.d.ts.map +0 -1
- package/dist/agent-recovery.js +0 -171
- package/dist/agent-recovery.js.map +0 -1
- package/dist/agent-runner.d.ts +0 -303
- package/dist/agent-runner.d.ts.map +0 -1
- package/dist/agent-runner.js +0 -1003
- package/dist/agent-runner.js.map +0 -1
- package/dist/agent-types.d.ts +0 -120
- package/dist/agent-types.d.ts.map +0 -1
- package/dist/agent-types.js +0 -301
- package/dist/agent-types.js.map +0 -1
- package/dist/child-context.d.ts +0 -3
- package/dist/child-context.d.ts.map +0 -1
- package/dist/child-context.js +0 -13
- package/dist/child-context.js.map +0 -1
- package/dist/context.d.ts +0 -13
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js +0 -57
- package/dist/context.js.map +0 -1
- package/dist/cross-extension-rpc.d.ts +0 -67
- package/dist/cross-extension-rpc.d.ts.map +0 -1
- package/dist/cross-extension-rpc.js +0 -139
- package/dist/cross-extension-rpc.js.map +0 -1
- package/dist/custom-agents.d.ts +0 -55
- package/dist/custom-agents.d.ts.map +0 -1
- package/dist/custom-agents.js +0 -309
- package/dist/custom-agents.js.map +0 -1
- package/dist/default-agents.d.ts +0 -8
- package/dist/default-agents.d.ts.map +0 -1
- package/dist/default-agents.js +0 -123
- package/dist/default-agents.js.map +0 -1
- package/dist/enabled-models.d.ts +0 -50
- package/dist/enabled-models.d.ts.map +0 -1
- package/dist/enabled-models.js +0 -146
- package/dist/enabled-models.js.map +0 -1
- package/dist/env.d.ts +0 -7
- package/dist/env.d.ts.map +0 -1
- package/dist/env.js +0 -29
- package/dist/env.js.map +0 -1
- package/dist/group-join.d.ts +0 -33
- package/dist/group-join.d.ts.map +0 -1
- package/dist/group-join.js +0 -117
- package/dist/group-join.js.map +0 -1
- package/dist/index.d.ts +0 -51
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -3687
- package/dist/index.js.map +0 -1
- package/dist/invocation-config.d.ts +0 -108
- package/dist/invocation-config.d.ts.map +0 -1
- package/dist/invocation-config.js +0 -84
- package/dist/invocation-config.js.map +0 -1
- package/dist/memory.d.ts +0 -54
- package/dist/memory.d.ts.map +0 -1
- package/dist/memory.js +0 -166
- package/dist/memory.js.map +0 -1
- package/dist/mention-clone.d.ts +0 -88
- package/dist/mention-clone.d.ts.map +0 -1
- package/dist/mention-clone.js +0 -154
- package/dist/mention-clone.js.map +0 -1
- package/dist/mention.d.ts +0 -82
- package/dist/mention.d.ts.map +0 -1
- package/dist/mention.js +0 -132
- package/dist/mention.js.map +0 -1
- package/dist/model-resolver.d.ts +0 -37
- package/dist/model-resolver.d.ts.map +0 -1
- package/dist/model-resolver.js +0 -96
- package/dist/model-resolver.js.map +0 -1
- package/dist/model-scope.d.ts +0 -50
- package/dist/model-scope.d.ts.map +0 -1
- package/dist/model-scope.js +0 -49
- package/dist/model-scope.js.map +0 -1
- package/dist/nested-tools.d.ts +0 -57
- package/dist/nested-tools.d.ts.map +0 -1
- package/dist/nested-tools.js +0 -301
- package/dist/nested-tools.js.map +0 -1
- package/dist/nico-overrides.d.ts +0 -54
- package/dist/nico-overrides.d.ts.map +0 -1
- package/dist/nico-overrides.js +0 -170
- package/dist/nico-overrides.js.map +0 -1
- package/dist/output-file.d.ts +0 -44
- package/dist/output-file.d.ts.map +0 -1
- package/dist/output-file.js +0 -156
- package/dist/output-file.js.map +0 -1
- package/dist/prompts.d.ts +0 -56
- package/dist/prompts.d.ts.map +0 -1
- package/dist/prompts.js +0 -92
- package/dist/prompts.js.map +0 -1
- package/dist/schedule-store.d.ts +0 -39
- package/dist/schedule-store.d.ts.map +0 -1
- package/dist/schedule-store.js +0 -156
- package/dist/schedule-store.js.map +0 -1
- package/dist/schedule.d.ts +0 -110
- package/dist/schedule.d.ts.map +0 -1
- package/dist/schedule.js +0 -360
- package/dist/schedule.js.map +0 -1
- package/dist/settings.d.ts +0 -354
- package/dist/settings.d.ts.map +0 -1
- package/dist/settings.js +0 -247
- package/dist/settings.js.map +0 -1
- package/dist/skill-loader.d.ts +0 -25
- package/dist/skill-loader.d.ts.map +0 -1
- package/dist/skill-loader.js +0 -94
- package/dist/skill-loader.js.map +0 -1
- package/dist/status-note.d.ts +0 -62
- package/dist/status-note.d.ts.map +0 -1
- package/dist/status-note.js +0 -86
- package/dist/status-note.js.map +0 -1
- package/dist/structured-output.d.ts +0 -62
- package/dist/structured-output.d.ts.map +0 -1
- package/dist/structured-output.js +0 -113
- package/dist/structured-output.js.map +0 -1
- package/dist/types.d.ts +0 -372
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -6
- package/dist/types.js.map +0 -1
- package/dist/ui/agent-mention.d.ts +0 -83
- package/dist/ui/agent-mention.d.ts.map +0 -1
- package/dist/ui/agent-mention.js +0 -188
- package/dist/ui/agent-mention.js.map +0 -1
- package/dist/ui/agent-widget.d.ts +0 -241
- package/dist/ui/agent-widget.d.ts.map +0 -1
- package/dist/ui/agent-widget.js +0 -992
- package/dist/ui/agent-widget.js.map +0 -1
- package/dist/ui/ccstyle/diff/ansi-utils.d.ts +0 -11
- package/dist/ui/ccstyle/diff/ansi-utils.d.ts.map +0 -1
- package/dist/ui/ccstyle/diff/ansi-utils.js +0 -145
- package/dist/ui/ccstyle/diff/ansi-utils.js.map +0 -1
- package/dist/ui/ccstyle/diff/diff-presentation.d.ts +0 -12
- package/dist/ui/ccstyle/diff/diff-presentation.d.ts.map +0 -1
- package/dist/ui/ccstyle/diff/diff-presentation.js +0 -48
- package/dist/ui/ccstyle/diff/diff-presentation.js.map +0 -1
- package/dist/ui/ccstyle/diff/diff-renderer.d.ts +0 -46
- package/dist/ui/ccstyle/diff/diff-renderer.d.ts.map +0 -1
- package/dist/ui/ccstyle/diff/diff-renderer.js +0 -2049
- package/dist/ui/ccstyle/diff/diff-renderer.js.map +0 -1
- package/dist/ui/ccstyle/diff/line-width-safety.d.ts +0 -12
- package/dist/ui/ccstyle/diff/line-width-safety.d.ts.map +0 -1
- package/dist/ui/ccstyle/diff/line-width-safety.js +0 -58
- package/dist/ui/ccstyle/diff/line-width-safety.js.map +0 -1
- package/dist/ui/ccstyle/diff/render-utils.d.ts +0 -6
- package/dist/ui/ccstyle/diff/render-utils.d.ts.map +0 -1
- package/dist/ui/ccstyle/diff/render-utils.js +0 -25
- package/dist/ui/ccstyle/diff/render-utils.js.map +0 -1
- package/dist/ui/ccstyle/diff/shiki-highlight.d.ts +0 -19
- package/dist/ui/ccstyle/diff/shiki-highlight.d.ts.map +0 -1
- package/dist/ui/ccstyle/diff/shiki-highlight.js +0 -85
- package/dist/ui/ccstyle/diff/shiki-highlight.js.map +0 -1
- package/dist/ui/ccstyle/diff/types.d.ts +0 -22
- package/dist/ui/ccstyle/diff/types.d.ts.map +0 -1
- package/dist/ui/ccstyle/diff/types.js +0 -10
- package/dist/ui/ccstyle/diff/types.js.map +0 -1
- package/dist/ui/ccstyle/diff/write-display-utils.d.ts +0 -2
- package/dist/ui/ccstyle/diff/write-display-utils.d.ts.map +0 -1
- package/dist/ui/ccstyle/diff/write-display-utils.js +0 -12
- package/dist/ui/ccstyle/diff/write-display-utils.js.map +0 -1
- package/dist/ui/ccstyle/tool-renderer.d.ts +0 -17
- package/dist/ui/ccstyle/tool-renderer.d.ts.map +0 -1
- package/dist/ui/ccstyle/tool-renderer.js +0 -74
- package/dist/ui/ccstyle/tool-renderer.js.map +0 -1
- package/dist/ui/ccstyle/tool-result.d.ts +0 -44
- package/dist/ui/ccstyle/tool-result.d.ts.map +0 -1
- package/dist/ui/ccstyle/tool-result.js +0 -423
- package/dist/ui/ccstyle/tool-result.js.map +0 -1
- package/dist/ui/conversation-blocks.d.ts +0 -46
- package/dist/ui/conversation-blocks.d.ts.map +0 -1
- package/dist/ui/conversation-blocks.js +0 -313
- package/dist/ui/conversation-blocks.js.map +0 -1
- package/dist/ui/conversation-nvim.d.ts +0 -8
- package/dist/ui/conversation-nvim.d.ts.map +0 -1
- package/dist/ui/conversation-nvim.js +0 -117
- package/dist/ui/conversation-nvim.js.map +0 -1
- package/dist/ui/conversation-role.d.ts +0 -13
- package/dist/ui/conversation-role.d.ts.map +0 -1
- package/dist/ui/conversation-role.js +0 -39
- package/dist/ui/conversation-role.js.map +0 -1
- package/dist/ui/conversation-search.d.ts +0 -39
- package/dist/ui/conversation-search.d.ts.map +0 -1
- package/dist/ui/conversation-search.js +0 -124
- package/dist/ui/conversation-search.js.map +0 -1
- package/dist/ui/conversation-timeline.d.ts +0 -102
- package/dist/ui/conversation-timeline.d.ts.map +0 -1
- package/dist/ui/conversation-timeline.js +0 -555
- package/dist/ui/conversation-timeline.js.map +0 -1
- package/dist/ui/conversation-viewer.d.ts +0 -138
- package/dist/ui/conversation-viewer.d.ts.map +0 -1
- package/dist/ui/conversation-viewer.js +0 -1175
- package/dist/ui/conversation-viewer.js.map +0 -1
- package/dist/ui/schedule-menu.d.ts +0 -17
- package/dist/ui/schedule-menu.d.ts.map +0 -1
- package/dist/ui/schedule-menu.js +0 -95
- package/dist/ui/schedule-menu.js.map +0 -1
- package/dist/ui/select-item.d.ts +0 -28
- package/dist/ui/select-item.d.ts.map +0 -1
- package/dist/ui/select-item.js +0 -35
- package/dist/ui/select-item.js.map +0 -1
- package/dist/ui/viewer-keys.d.ts +0 -21
- package/dist/ui/viewer-keys.d.ts.map +0 -1
- package/dist/ui/viewer-keys.js +0 -18
- package/dist/ui/viewer-keys.js.map +0 -1
- package/dist/ui/workflow-card.d.ts +0 -176
- package/dist/ui/workflow-card.d.ts.map +0 -1
- package/dist/ui/workflow-card.js +0 -333
- package/dist/ui/workflow-card.js.map +0 -1
- package/dist/ui/workflow-dialog.d.ts +0 -306
- package/dist/ui/workflow-dialog.d.ts.map +0 -1
- package/dist/ui/workflow-dialog.js +0 -844
- package/dist/ui/workflow-dialog.js.map +0 -1
- package/dist/ui/workflow-menu.d.ts +0 -42
- package/dist/ui/workflow-menu.d.ts.map +0 -1
- package/dist/ui/workflow-menu.js +0 -127
- package/dist/ui/workflow-menu.js.map +0 -1
- package/dist/usage.d.ts +0 -136
- package/dist/usage.d.ts.map +0 -1
- package/dist/usage.js +0 -121
- package/dist/usage.js.map +0 -1
- package/dist/workflow/collisions.d.ts +0 -96
- package/dist/workflow/collisions.d.ts.map +0 -1
- package/dist/workflow/collisions.js +0 -89
- package/dist/workflow/collisions.js.map +0 -1
- package/dist/workflow/entry.d.ts +0 -33
- package/dist/workflow/entry.d.ts.map +0 -1
- package/dist/workflow/entry.js +0 -30
- package/dist/workflow/entry.js.map +0 -1
- package/dist/workflow/host.d.ts +0 -63
- package/dist/workflow/host.d.ts.map +0 -1
- package/dist/workflow/host.js +0 -363
- package/dist/workflow/host.js.map +0 -1
- package/dist/workflow/journal.d.ts +0 -98
- package/dist/workflow/journal.d.ts.map +0 -1
- package/dist/workflow/journal.js +0 -121
- package/dist/workflow/journal.js.map +0 -1
- package/dist/workflow/json-schema.d.ts +0 -52
- package/dist/workflow/json-schema.d.ts.map +0 -1
- package/dist/workflow/json-schema.js +0 -112
- package/dist/workflow/json-schema.js.map +0 -1
- package/dist/workflow/meta.d.ts +0 -68
- package/dist/workflow/meta.d.ts.map +0 -1
- package/dist/workflow/meta.js +0 -318
- package/dist/workflow/meta.js.map +0 -1
- package/dist/workflow/progress.d.ts +0 -225
- package/dist/workflow/progress.d.ts.map +0 -1
- package/dist/workflow/progress.js +0 -362
- package/dist/workflow/progress.js.map +0 -1
- package/dist/workflow/runtime.d.ts +0 -335
- package/dist/workflow/runtime.d.ts.map +0 -1
- package/dist/workflow/runtime.js +0 -831
- package/dist/workflow/runtime.js.map +0 -1
- package/dist/workflow/saved.d.ts +0 -91
- package/dist/workflow/saved.d.ts.map +0 -1
- package/dist/workflow/saved.js +0 -204
- package/dist/workflow/saved.js.map +0 -1
- package/dist/workflow/task.d.ts +0 -137
- package/dist/workflow/task.d.ts.map +0 -1
- package/dist/workflow/task.js +0 -208
- package/dist/workflow/task.js.map +0 -1
- package/dist/workflow/tool-description.d.ts +0 -39
- package/dist/workflow/tool-description.d.ts.map +0 -1
- package/dist/workflow/tool-description.js +0 -200
- package/dist/workflow/tool-description.js.map +0 -1
- package/dist/workflow/worker-source.d.ts +0 -48
- package/dist/workflow/worker-source.d.ts.map +0 -1
- package/dist/workflow/worker-source.js +0 -779
- package/dist/workflow/worker-source.js.map +0 -1
- package/dist/worktree.d.ts +0 -53
- package/dist/worktree.d.ts.map +0 -1
- package/dist/worktree.js +0 -165
- package/dist/worktree.js.map +0 -1
- package/dist/write-execution.d.ts +0 -42
- package/dist/write-execution.d.ts.map +0 -1
- package/dist/write-execution.js +0 -138
- package/dist/write-execution.js.map +0 -1
- package/dist/xml.d.ts +0 -11
- package/dist/xml.d.ts.map +0 -1
- package/dist/xml.js +0 -13
- package/dist/xml.js.map +0 -1
- package/src/abortable.ts +0 -43
- package/src/agent-color.ts +0 -161
- package/src/agent-file-toggle.ts +0 -269
- package/src/child-context.ts +0 -15
- package/src/mention-clone.ts +0 -196
- package/src/mention.ts +0 -141
- package/src/model-scope.ts +0 -70
- package/src/nested-tools.ts +0 -424
- package/src/structured-output.ts +0 -130
- package/src/ui/agent-mention.ts +0 -216
- package/src/ui/select-item.ts +0 -45
- package/src/ui/workflow-card.ts +0 -470
- package/src/ui/workflow-dialog.ts +0 -1115
- package/src/ui/workflow-menu.ts +0 -166
- package/src/workflow/collisions.ts +0 -123
- package/src/workflow/entry.ts +0 -47
- package/src/workflow/host.ts +0 -403
- package/src/workflow/journal.ts +0 -164
- package/src/workflow/json-schema.ts +0 -128
- package/src/workflow/meta.ts +0 -325
- package/src/workflow/progress.ts +0 -550
- package/src/workflow/runtime.ts +0 -1219
- package/src/workflow/saved.ts +0 -217
- package/src/workflow/task.ts +0 -302
- package/src/workflow/tool-description.ts +0 -200
- package/src/workflow/worker-source.ts +0 -781
- package/src/xml.ts +0 -13
package/src/mention.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mention.ts — the `@handle` grammar for messaging a subagent from the prompt.
|
|
3
|
-
*
|
|
4
|
-
* Claude Code lets you type `@code-review take another look` at the prompt and
|
|
5
|
-
* routes the message to that agent instead of the main model. Its grammar is
|
|
6
|
-
* reproduced here so the two behave identically:
|
|
7
|
-
*
|
|
8
|
-
* - suggestions fire on `@` at the start of the input or after whitespace,
|
|
9
|
-
* followed by `[\w-]*` (so `@src/foo.ts` is a file, never an agent);
|
|
10
|
-
* - a send is recognized only at the START of the input, and only with a
|
|
11
|
-
* non-empty message after the handle. That is why a bare `@code-review`
|
|
12
|
-
* goes to the main model rather than anywhere near the agent.
|
|
13
|
-
*
|
|
14
|
-
* A record's own identity is a UUID plus a deliberately non-unique description,
|
|
15
|
-
* neither of which is typeable, so the handle is derived from the agent type.
|
|
16
|
-
* Colliding handles are numbered (`explore`, `explore-2`), which is also what
|
|
17
|
-
* Claude Code's `allocateName` does — it recycles a name only once the task
|
|
18
|
-
* behind it is gone. Its SendMessage prompt describes the *registry* as
|
|
19
|
-
* latest-wins, which is a different thing and not how names are allocated.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Suggestion trigger: `@` at a token boundary plus the partial handle typed so
|
|
24
|
-
* far. Ported from Claude Code, including the CJK sentence-ending punctuation
|
|
25
|
-
* it accepts as a boundary.
|
|
26
|
-
*/
|
|
27
|
-
export const MENTION_TRIGGER = /(^|[\s。、?!])@([\w-]*)$/;
|
|
28
|
-
|
|
29
|
-
/** Send grammar: leading `@handle`, then a non-empty message. */
|
|
30
|
-
const MENTION_SEND = /^@([\w-]+)\s+([\s\S]+)$/;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Upper bound on a handle, matching Claude Code's `dSS`. Nothing here generates
|
|
34
|
-
* a name this long, but an agent type or a model-supplied name can be arbitrary
|
|
35
|
-
* text, and an unbounded handle would wrap the suggestion popup.
|
|
36
|
-
*/
|
|
37
|
-
const MAX_HANDLE_LENGTH = 64;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Handles that address something other than a subagent, and so can never be
|
|
41
|
-
* allocated to one. Claude Code reserves exactly this name (`Vq = "main"`),
|
|
42
|
-
* refusing it at spawn and routing it to the main conversation instead.
|
|
43
|
-
*/
|
|
44
|
-
const RESERVED_HANDLES: ReadonlySet<string> = new Set(["main"]);
|
|
45
|
-
|
|
46
|
-
/** Whether `@handle` names the main conversation rather than any subagent. */
|
|
47
|
-
export function isReservedHandle(handle: string): boolean {
|
|
48
|
-
return RESERVED_HANDLES.has(handle.toLowerCase());
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** Slug of an agent type or name, restricted to the `[\w-]` the grammar allows. */
|
|
52
|
-
export function handleBase(type: string): string {
|
|
53
|
-
const slug = type.toLowerCase()
|
|
54
|
-
.replace(/[^a-z0-9_-]+/g, "-")
|
|
55
|
-
.replace(/^-+|-+$/g, "")
|
|
56
|
-
.slice(0, MAX_HANDLE_LENGTH)
|
|
57
|
-
// The slice can land mid-run and leave the trailing hyphen back.
|
|
58
|
-
.replace(/-+$/, "");
|
|
59
|
-
return slug || "agent";
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* `base`, else `base-2`, `base-3`, … — the first form that is neither `taken`
|
|
64
|
-
* nor reserved. Callers pass one shared `taken` set covering type-derived
|
|
65
|
-
* handles and model-supplied aliases alike, so the two can never collide.
|
|
66
|
-
*/
|
|
67
|
-
export function assignHandle(base: string, taken: ReadonlySet<string>): string {
|
|
68
|
-
let candidate = base;
|
|
69
|
-
let n = 1;
|
|
70
|
-
while (taken.has(candidate) || RESERVED_HANDLES.has(candidate)) {
|
|
71
|
-
n++;
|
|
72
|
-
candidate = `${base}-${n}`;
|
|
73
|
-
}
|
|
74
|
-
return candidate;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Map a typed handle back to a registered agent type, so `@explore fix it`
|
|
79
|
-
* reaches the Explore agent even when no instance has ever run. `handleBase` is
|
|
80
|
-
* the single source of truth in both directions, so a type is addressable by
|
|
81
|
-
* exactly the handle its instances would be given.
|
|
82
|
-
*/
|
|
83
|
-
export function resolveHandleToType(handle: string, types: readonly string[]): string | undefined {
|
|
84
|
-
const wanted = handle.toLowerCase();
|
|
85
|
-
// A type slugging to a reserved name is unaddressable rather than shadowing
|
|
86
|
-
// it — `assignHandle` refuses that name too, so its instances never hold one.
|
|
87
|
-
if (RESERVED_HANDLES.has(wanted)) return undefined;
|
|
88
|
-
return types.find(type => handleBase(type) === wanted);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Claude Code documents `@agent-<name>` as the form you type by hand when the
|
|
93
|
-
* picker isn't involved. Accepted here as an exact synonym: the caller tries the
|
|
94
|
-
* handle as written first, so an agent genuinely called `agent-foo` still wins
|
|
95
|
-
* over `@agent-` + `foo`, and only falls back to this when that finds nothing.
|
|
96
|
-
* Returns undefined when the prefix is absent or is the whole handle.
|
|
97
|
-
*/
|
|
98
|
-
export function stripAgentPrefix(handle: string): string | undefined {
|
|
99
|
-
const rest = /^agent-(.+)$/i.exec(handle)?.[1];
|
|
100
|
-
return rest || undefined;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* A spawn needs the short description every agent surface renders. A mention
|
|
105
|
-
* carries no separate label, so the message itself becomes one: first line,
|
|
106
|
-
* whitespace collapsed, clipped to roughly the 3-5 words the Agent tool asks of
|
|
107
|
-
* the model.
|
|
108
|
-
*/
|
|
109
|
-
export function describeMention(message: string): string {
|
|
110
|
-
const oneLine = message.split("\n", 1)[0].replace(/\s+/g, " ").trim();
|
|
111
|
-
return oneLine.length > 40 ? `${oneLine.slice(0, 39).trimEnd()}…` : oneLine;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* What Claude Code sends the main model when a mention names an agent it could
|
|
116
|
-
* start. Its `@agent-<type>` mention is not a spawn at all: it becomes an
|
|
117
|
-
* `agent_mention` attachment, which renders to a synthetic `isMeta` user
|
|
118
|
-
* message placed after the user's own untouched text — no tool forcing, no
|
|
119
|
-
* allowed-tools narrowing, and the Task tool is not even named. The model reads
|
|
120
|
-
* this and calls the tool itself.
|
|
121
|
-
*
|
|
122
|
-
* Ported verbatim from the 2.1.233 bundle's attachment renderer, trailing space
|
|
123
|
-
* before the closing newline included, so the wording the model was trained
|
|
124
|
-
* against is the wording it gets. The one substitution is ours: pi's equivalent
|
|
125
|
-
* of Task is the `Agent` tool, and the agent listing that teaches valid
|
|
126
|
-
* `subagent_type` values is the tool spec rather than a separate attachment.
|
|
127
|
-
*/
|
|
128
|
-
export function agentMentionReminder(type: string): string {
|
|
129
|
-
return `<system-reminder>\nThe user has expressed a desire to invoke the agent "${type}". Please invoke the agent appropriately, passing in the required context to it. \n</system-reminder>`;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Split `@handle message` into its parts, or null when the text isn't a send —
|
|
134
|
-
* a bare handle, a leading file path, or a mention that isn't at the start.
|
|
135
|
-
*/
|
|
136
|
-
export function parseMention(text: string): { handle: string; message: string } | null {
|
|
137
|
-
const match = MENTION_SEND.exec(text);
|
|
138
|
-
if (!match) return null;
|
|
139
|
-
const message = match[2].trim();
|
|
140
|
-
return message ? { handle: match[1], message } : null;
|
|
141
|
-
}
|
package/src/model-scope.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* model-scope.ts — `scopeModels` policy, shared by the top-level Agent tool and
|
|
3
|
-
* the nested delegation tools so a nested spawn can't escape the allowlist the
|
|
4
|
-
* top-level path enforces.
|
|
5
|
-
*
|
|
6
|
-
* State lives here (rather than in an index.ts closure) for the same reason
|
|
7
|
-
* `disableDefaults` lives in agent-types.ts: both entry points need it.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { isModelInScope, type ModelRegistryRef, readEnabledModels, resolveEnabledModels } from "./enabled-models.js";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* When enabled, subagent model choices are validated against `enabledModels`
|
|
14
|
-
* from pi's settings — both global `<agentDir>/settings.json` and project-local
|
|
15
|
-
* `<cwd>/.pi/settings.json` (project overrides global). Off by default; opt-in
|
|
16
|
-
* via `/agents → Settings`. See the SubagentsSettings.scopeModels docstring for
|
|
17
|
-
* the hard-error vs warn-and-proceed policy and its rationale.
|
|
18
|
-
*/
|
|
19
|
-
let scopeModelsEnabled = false;
|
|
20
|
-
|
|
21
|
-
export function isScopeModelsEnabled(): boolean { return scopeModelsEnabled; }
|
|
22
|
-
export function setScopeModelsEnabled(enabled: boolean): void { scopeModelsEnabled = enabled; }
|
|
23
|
-
|
|
24
|
-
export type ModelScopeVerdict =
|
|
25
|
-
/** In scope, or nothing to validate against (feature off / no allowlist). */
|
|
26
|
-
| { kind: "ok" }
|
|
27
|
-
/** Caller-supplied out-of-scope choice — refuse the spawn with this message. */
|
|
28
|
-
| { kind: "error"; message: string }
|
|
29
|
-
/** Frontmatter-pinned or parent-inherited — proceed, but tell the user. */
|
|
30
|
-
| { kind: "warn"; message: string };
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Check the effective resolved model against the user's enabledModels list.
|
|
34
|
-
*
|
|
35
|
-
* scopeModels guards against *runtime* LLM choices, not user-level config:
|
|
36
|
-
* - Caller-supplied out-of-scope → hard error (the orchestrator made an explicit
|
|
37
|
-
* out-of-scope choice; surface it so it picks differently).
|
|
38
|
-
* - Frontmatter-pinned or parent-inherited out-of-scope → warn but proceed (the
|
|
39
|
-
* user authored/installed this agent or chose the parent's model; trust it).
|
|
40
|
-
*/
|
|
41
|
-
export function checkModelScope(args: {
|
|
42
|
-
model: { provider: string; id: string } | undefined;
|
|
43
|
-
cwd: string;
|
|
44
|
-
modelRegistry: ModelRegistryRef;
|
|
45
|
-
/** True when the model came from the tool call rather than frontmatter. */
|
|
46
|
-
callerSupplied: boolean;
|
|
47
|
-
/** Display name used in the warning toast. */
|
|
48
|
-
agentLabel: string;
|
|
49
|
-
/** The raw `model:` input, when there was one. */
|
|
50
|
-
modelInput?: string;
|
|
51
|
-
}): ModelScopeVerdict {
|
|
52
|
-
const { model, cwd, modelRegistry, callerSupplied, agentLabel, modelInput } = args;
|
|
53
|
-
if (!scopeModelsEnabled || !model) return { kind: "ok" };
|
|
54
|
-
|
|
55
|
-
const allowed = resolveEnabledModels(readEnabledModels(cwd), modelRegistry, cwd);
|
|
56
|
-
if (!allowed || isModelInScope(model, allowed)) return { kind: "ok" };
|
|
57
|
-
|
|
58
|
-
if (callerSupplied) {
|
|
59
|
-
const list = [...allowed].sort().map(m => ` ${m}`).join("\n");
|
|
60
|
-
return {
|
|
61
|
-
kind: "error",
|
|
62
|
-
message: `Model not in scope: "${modelInput}".\n\nAllowed models (from enabledModels):\n${list}`,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
const modelLabel = modelInput ?? `${model.provider}/${model.id}`;
|
|
66
|
-
return {
|
|
67
|
-
kind: "warn",
|
|
68
|
-
message: `Agent "${agentLabel}" using out-of-scope model "${modelLabel}"`,
|
|
69
|
-
};
|
|
70
|
-
}
|
package/src/nested-tools.ts
DELETED
|
@@ -1,424 +0,0 @@
|
|
|
1
|
-
import type { Model } from "@earendil-works/pi-ai";
|
|
2
|
-
import {
|
|
3
|
-
type AgentSession,
|
|
4
|
-
defineTool,
|
|
5
|
-
type ExtensionAPI,
|
|
6
|
-
type ExtensionContext,
|
|
7
|
-
type ToolDefinition,
|
|
8
|
-
} from "@earendil-works/pi-coding-agent";
|
|
9
|
-
import { Type } from "@sinclair/typebox";
|
|
10
|
-
import { abortable } from "./abortable.js";
|
|
11
|
-
import {
|
|
12
|
-
buildAgentRegistry,
|
|
13
|
-
getAgentConfigIn,
|
|
14
|
-
getAvailableTypesIn,
|
|
15
|
-
resolveEnabledTypeIn,
|
|
16
|
-
resolveTypeIn,
|
|
17
|
-
} from "./agent-types.js";
|
|
18
|
-
import { loadCustomAgents } from "./custom-agents.js";
|
|
19
|
-
import { isolationParam, resolveAgentInvocationConfig } from "./invocation-config.js";
|
|
20
|
-
import { resolveModel } from "./model-resolver.js";
|
|
21
|
-
import { checkModelScope } from "./model-scope.js";
|
|
22
|
-
import {
|
|
23
|
-
createOutputFilePath,
|
|
24
|
-
getOutputTranscriptDefault,
|
|
25
|
-
streamToOutputFile,
|
|
26
|
-
writeInitialEntry,
|
|
27
|
-
} from "./output-file.js";
|
|
28
|
-
import { getForegroundOutcomeNote, getStatusNote, partialOutputSuffix } from "./status-note.js";
|
|
29
|
-
import type {
|
|
30
|
-
AgentConfig,
|
|
31
|
-
AgentInvocation,
|
|
32
|
-
AgentRecord,
|
|
33
|
-
IsolationMode,
|
|
34
|
-
ThinkingLevel,
|
|
35
|
-
} from "./types.js";
|
|
36
|
-
import { addUsage } from "./usage.js";
|
|
37
|
-
import { isWorktreeIsolationEnabled } from "./worktree.js";
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Hard ceiling on nesting for every branch: main session = 0, its subagents = 1,
|
|
41
|
-
* their children = 2. `0`/`1` disables nesting entirely. Set from
|
|
42
|
-
* `subagents.json` (`maxSubagentDepth`). Read when a subagent session is built,
|
|
43
|
-
* so a change applies to sessions started after it.
|
|
44
|
-
*/
|
|
45
|
-
let maxSubagentDepth = 2;
|
|
46
|
-
|
|
47
|
-
export function getMaxSubagentDepth(): number { return maxSubagentDepth; }
|
|
48
|
-
export function setMaxSubagentDepth(n: number): void { maxSubagentDepth = Math.max(0, Math.floor(n)); }
|
|
49
|
-
|
|
50
|
-
const NESTED_TOOL_NAMES = ["Agent", "get_subagent_result", "steer_subagent"] as const;
|
|
51
|
-
|
|
52
|
-
interface NestedSpawnOptions {
|
|
53
|
-
description: string;
|
|
54
|
-
model?: Model<any>;
|
|
55
|
-
maxTurns?: number;
|
|
56
|
-
isolated?: boolean;
|
|
57
|
-
inheritContext?: boolean;
|
|
58
|
-
thinkingLevel?: ThinkingLevel;
|
|
59
|
-
isBackground?: boolean;
|
|
60
|
-
isolation?: IsolationMode;
|
|
61
|
-
outputTranscript?: boolean;
|
|
62
|
-
invocation?: AgentInvocation;
|
|
63
|
-
signal?: AbortSignal;
|
|
64
|
-
onAssistantUsage?: (usage: { input: number; output: number; cacheWrite: number }) => void;
|
|
65
|
-
onSessionCreated?: (session: AgentSession) => void;
|
|
66
|
-
depth: number;
|
|
67
|
-
parentAgentId: string;
|
|
68
|
-
maxSubagentDepth: number;
|
|
69
|
-
configCwd?: string;
|
|
70
|
-
rootSessionId?: string;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export interface NestedAgentManager {
|
|
74
|
-
spawn(
|
|
75
|
-
pi: ExtensionAPI,
|
|
76
|
-
ctx: ExtensionContext,
|
|
77
|
-
type: string,
|
|
78
|
-
prompt: string,
|
|
79
|
-
options: NestedSpawnOptions,
|
|
80
|
-
): string;
|
|
81
|
-
/** Resolves once the spawned agent is running; rejects on a startup failure. */
|
|
82
|
-
awaitStartup(id: string): Promise<void>;
|
|
83
|
-
spawnAndWait(
|
|
84
|
-
pi: ExtensionAPI,
|
|
85
|
-
ctx: ExtensionContext,
|
|
86
|
-
type: string,
|
|
87
|
-
prompt: string,
|
|
88
|
-
options: Omit<NestedSpawnOptions, "isBackground">,
|
|
89
|
-
/** Fires synchronously after spawn, before the session exists — where the transcript is attached. */
|
|
90
|
-
onSpawned?: (id: string) => void,
|
|
91
|
-
): Promise<{ id: string; record: AgentRecord }>;
|
|
92
|
-
getRecord(id: string): AgentRecord | undefined;
|
|
93
|
-
resume(id: string, prompt: string, signal?: AbortSignal): Promise<AgentRecord | undefined>;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export interface NestedToolContext {
|
|
97
|
-
manager: NestedAgentManager;
|
|
98
|
-
pi: ExtensionAPI;
|
|
99
|
-
parentAgentId: string;
|
|
100
|
-
depth: number;
|
|
101
|
-
maxSubagentDepth: number;
|
|
102
|
-
/** "all" = any enabled agent; string[] = only those types. Never empty. */
|
|
103
|
-
allowedSubagents: "all" | string[];
|
|
104
|
-
/** Root used for agent/config discovery; may differ from the agent's working directory. */
|
|
105
|
-
configCwd: string;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function textResult(text: string, isError = false) {
|
|
109
|
-
return { content: [{ type: "text" as const, text }], isError, details: {} };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function ownsRecord(record: AgentRecord | undefined, parentAgentId: string): record is AgentRecord {
|
|
113
|
-
return record?.parentAgentId === parentAgentId;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* How the caller received this record, which decides the outcome wording.
|
|
118
|
-
*
|
|
119
|
-
* - "inline": a foreground spawn or a resume. The full output is in this very
|
|
120
|
-
* result and no agent id was handed back, so the note must say there is
|
|
121
|
-
* nothing left to fetch — otherwise the parent invents an id, calls
|
|
122
|
-
* `get_subagent_result`, and hits "not owned by this parent" (#174, the same
|
|
123
|
-
* trap the top-level foreground path fell into).
|
|
124
|
-
* - "fetched": `get_subagent_result` on a background child. The parent holds a
|
|
125
|
-
* valid id and can poll again, so the background wording applies.
|
|
126
|
-
*/
|
|
127
|
-
type ResultPosition = "inline" | "fetched";
|
|
128
|
-
|
|
129
|
-
function formatRecord(record: AgentRecord, position: ResultPosition): string {
|
|
130
|
-
if (record.status === "error") {
|
|
131
|
-
return `Agent failed: ${record.error ?? "unknown error"}${partialOutputSuffix(record)}`;
|
|
132
|
-
}
|
|
133
|
-
if (record.status === "queued" || record.status === "running") {
|
|
134
|
-
return `Agent ${record.id} is ${record.status}.`;
|
|
135
|
-
}
|
|
136
|
-
// A truncated run must not read as a finished one. The top-level path carries
|
|
137
|
-
// this in its result headline; a nested result has no headline, so the note
|
|
138
|
-
// leads — appended, it would look like part of the child's own output.
|
|
139
|
-
const text = record.result?.trim() || record.error?.trim() || "No output.";
|
|
140
|
-
const note = position === "inline"
|
|
141
|
-
? getForegroundOutcomeNote(record.status)
|
|
142
|
-
: getStatusNote(record.status);
|
|
143
|
-
return note ? `Nested agent${note}.\n\n${text}` : text;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** Build child-safe orchestration tools scoped to one parent agent instance. */
|
|
147
|
-
export function createNestedSubagentTools(context: NestedToolContext): ToolDefinition[] {
|
|
148
|
-
// Agents resolve from a registry built for THIS branch's config root (under
|
|
149
|
-
// worktree isolation, the copy). Never via registerAgents — that is
|
|
150
|
-
// process-global state shared with the main session and every other agent.
|
|
151
|
-
const loadRegistry = () => buildAgentRegistry(loadCustomAgents(context.configCwd));
|
|
152
|
-
const allowedTypesIn = (registry: Map<string, AgentConfig>): Set<string> | undefined =>
|
|
153
|
-
context.allowedSubagents === "all"
|
|
154
|
-
? undefined
|
|
155
|
-
: new Set(context.allowedSubagents.map(name => resolveTypeIn(registry, name) ?? name));
|
|
156
|
-
const availableIn = (registry: Map<string, AgentConfig>): string[] => {
|
|
157
|
-
const allowed = allowedTypesIn(registry);
|
|
158
|
-
return getAvailableTypesIn(registry).filter(name => allowed === undefined || allowed.has(name));
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
const agentTool = defineTool({
|
|
162
|
-
name: NESTED_TOOL_NAMES[0],
|
|
163
|
-
label: "Agent",
|
|
164
|
-
description:
|
|
165
|
-
"Launch a child-safe nested subagent for bounded delegated work. " +
|
|
166
|
-
"Only use agent types allowed by this parent agent; nesting is depth-limited.",
|
|
167
|
-
parameters: Type.Object({
|
|
168
|
-
prompt: Type.String({ description: "Self-contained task for the nested agent." }),
|
|
169
|
-
description: Type.String({ description: "Short 3-5 word task description." }),
|
|
170
|
-
subagent_type: Type.String({ description: `Allowed nested agent type. Available: ${availableIn(loadRegistry()).join(", ") || "none"}.` }),
|
|
171
|
-
model: Type.Optional(Type.String({ description: "Optional provider/model override." })),
|
|
172
|
-
thinking: Type.Optional(Type.String({ description: "Optional thinking level." })),
|
|
173
|
-
max_turns: Type.Optional(Type.Number({ minimum: 1 })),
|
|
174
|
-
run_in_background: Type.Optional(
|
|
175
|
-
Type.Boolean({
|
|
176
|
-
description: "Defaults to false for nested spawns — the call blocks and returns the child's result inline. Set true only for work you will collect later with get_subagent_result; a detached child is stopped when you finish.",
|
|
177
|
-
}),
|
|
178
|
-
),
|
|
179
|
-
resume: Type.Optional(Type.String({ description: "Resume a nested agent owned by this parent." })),
|
|
180
|
-
isolated: Type.Optional(Type.Boolean()),
|
|
181
|
-
inherit_context: Type.Optional(Type.Boolean()),
|
|
182
|
-
...isolationParam(isWorktreeIsolationEnabled()),
|
|
183
|
-
}),
|
|
184
|
-
execute: async (_toolCallId, params, signal, _onUpdate, ctx) => {
|
|
185
|
-
if (params.resume) {
|
|
186
|
-
const existing = context.manager.getRecord(params.resume);
|
|
187
|
-
if (!ownsRecord(existing, context.parentAgentId)) {
|
|
188
|
-
return textResult(`Nested agent not found or not owned by this parent: "${params.resume}".`, true);
|
|
189
|
-
}
|
|
190
|
-
const resumed = await context.manager.resume(params.resume, params.prompt, signal);
|
|
191
|
-
return resumed
|
|
192
|
-
? textResult(formatRecord(resumed, "inline"), resumed.status === "error")
|
|
193
|
-
: textResult(`Failed to resume nested agent "${params.resume}".`, true);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (context.depth >= context.maxSubagentDepth) {
|
|
197
|
-
return textResult(
|
|
198
|
-
`Nested subagent call blocked (depth=${context.depth}, max=${context.maxSubagentDepth}). Complete the task directly.`,
|
|
199
|
-
true,
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// Reloaded per call so new agent files are picked up without a restart.
|
|
204
|
-
const registry = loadRegistry();
|
|
205
|
-
const rawType = params.subagent_type;
|
|
206
|
-
// Strict resolve, never the fallback policy: a project-level
|
|
207
|
-
// `fallbackSubagent` must not hand a nested caller an agent its allowlist
|
|
208
|
-
// never named. The list stays allowlist-filtered so a typo can't enumerate
|
|
209
|
-
// agents this parent may not reach.
|
|
210
|
-
const resolvedType = resolveEnabledTypeIn(registry, rawType);
|
|
211
|
-
if (resolvedType === undefined) {
|
|
212
|
-
return textResult(
|
|
213
|
-
`Unknown or disabled nested agent type: "${rawType}". Allowed: ${availableIn(registry).join(", ") || "none"}.`,
|
|
214
|
-
true,
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
const allowed = allowedTypesIn(registry);
|
|
218
|
-
if (allowed !== undefined && !allowed.has(resolvedType)) {
|
|
219
|
-
return textResult(
|
|
220
|
-
`Nested agent type "${resolvedType}" is not allowed for this parent. Allowed: ${[...allowed].join(", ")}.`,
|
|
221
|
-
true,
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const config = getAgentConfigIn(registry, resolvedType);
|
|
226
|
-
// Foreground regardless of `backgroundByDefault` — see the reasoning on
|
|
227
|
-
// ResolveOptions. An explicit `true` here still opts in.
|
|
228
|
-
const invocation = resolveAgentInvocationConfig(config, params, {
|
|
229
|
-
worktreeAllowed: isWorktreeIsolationEnabled(),
|
|
230
|
-
defaultRunInBackground: false,
|
|
231
|
-
});
|
|
232
|
-
let model = ctx.model;
|
|
233
|
-
if (invocation.modelInput) {
|
|
234
|
-
const resolvedModel = resolveModel(invocation.modelInput, ctx.modelRegistry);
|
|
235
|
-
if (typeof resolvedModel === "string") {
|
|
236
|
-
if (invocation.modelFromParams) return textResult(resolvedModel, true);
|
|
237
|
-
} else {
|
|
238
|
-
model = resolvedModel;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Same scopeModels policy as the top-level Agent tool — a nested spawn
|
|
243
|
-
// must not escape the allowlist. A "warn" verdict proceeds silently:
|
|
244
|
-
// child sessions have no UI surface to toast to.
|
|
245
|
-
const scopeVerdict = checkModelScope({
|
|
246
|
-
model,
|
|
247
|
-
cwd: context.configCwd,
|
|
248
|
-
modelRegistry: ctx.modelRegistry,
|
|
249
|
-
callerSupplied: invocation.modelFromParams,
|
|
250
|
-
agentLabel: config?.displayName ?? resolvedType,
|
|
251
|
-
modelInput: invocation.modelInput,
|
|
252
|
-
});
|
|
253
|
-
if (scopeVerdict.kind === "error") return textResult(scopeVerdict.message, true);
|
|
254
|
-
|
|
255
|
-
// The whole branch shares the root session's transcript directory; read it
|
|
256
|
-
// off the owning parent rather than this child session's own id.
|
|
257
|
-
const rootSessionId = context.manager.getRecord(context.parentAgentId)?.rootSessionId;
|
|
258
|
-
const childDepth = context.depth + 1;
|
|
259
|
-
const options: NestedSpawnOptions = {
|
|
260
|
-
description: params.description,
|
|
261
|
-
model,
|
|
262
|
-
maxTurns: invocation.maxTurns,
|
|
263
|
-
isolated: invocation.isolated,
|
|
264
|
-
inheritContext: invocation.inheritContext,
|
|
265
|
-
thinkingLevel: invocation.thinking,
|
|
266
|
-
isolation: invocation.isolation,
|
|
267
|
-
outputTranscript: config?.outputTranscript ?? getOutputTranscriptDefault(),
|
|
268
|
-
invocation: {
|
|
269
|
-
thinking: invocation.thinking,
|
|
270
|
-
maxTurns: invocation.maxTurns,
|
|
271
|
-
isolated: invocation.isolated,
|
|
272
|
-
inheritContext: invocation.inheritContext,
|
|
273
|
-
runInBackground: invocation.runInBackground,
|
|
274
|
-
isolation: invocation.isolation,
|
|
275
|
-
},
|
|
276
|
-
// Nested children are hidden from every reporting surface, so their spend
|
|
277
|
-
// would otherwise be unattributable. Fold it into every ancestor's record:
|
|
278
|
-
// the top-level one appears in lifecycle events, completion notifications,
|
|
279
|
-
// and `/agents`, and those all read `lifetimeUsage`. The whole chain is
|
|
280
|
-
// walked, not just the immediate parent — a spawn callback only fires for
|
|
281
|
-
// that child's OWN turns, so stopping at one level would hide a
|
|
282
|
-
// great-grandchild from the only record anyone can see. (The live
|
|
283
|
-
// widget/fleet counters read their own per-agent activity tracker, which
|
|
284
|
-
// still sees only the top-level agent's own turns.)
|
|
285
|
-
onAssistantUsage: (usage) => {
|
|
286
|
-
for (let id: string | undefined = context.parentAgentId; id !== undefined; ) {
|
|
287
|
-
const ancestor = context.manager.getRecord(id);
|
|
288
|
-
if (!ancestor) break;
|
|
289
|
-
addUsage(ancestor.lifetimeUsage, usage);
|
|
290
|
-
id = ancestor.parentAgentId;
|
|
291
|
-
}
|
|
292
|
-
},
|
|
293
|
-
depth: childDepth,
|
|
294
|
-
parentAgentId: context.parentAgentId,
|
|
295
|
-
maxSubagentDepth: context.maxSubagentDepth,
|
|
296
|
-
configCwd: context.configCwd,
|
|
297
|
-
rootSessionId,
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
// Transcript wiring, same gate as the top-level path: the child's
|
|
301
|
-
// `output_transcript` frontmatter wins, else the project default. Without
|
|
302
|
-
// it a nested run leaves no artifact but the string it returned — the
|
|
303
|
-
// parent's own transcript records the call and the answer, never the tool
|
|
304
|
-
// calls in between, which is exactly what a misbehaving child needs to
|
|
305
|
-
// explain itself. Filed under the ROOT session and this branch's config
|
|
306
|
-
// root, so a nested transcript lands in the same `tasks/` directory as its
|
|
307
|
-
// ancestors' rather than in a directory of its own.
|
|
308
|
-
const transcriptSessionId =
|
|
309
|
-
rootSessionId !== undefined && (config?.outputTranscript ?? getOutputTranscriptDefault())
|
|
310
|
-
? rootSessionId
|
|
311
|
-
: undefined;
|
|
312
|
-
let childId: string | undefined;
|
|
313
|
-
const attachTranscript = (id: string): void => {
|
|
314
|
-
childId = id;
|
|
315
|
-
if (transcriptSessionId === undefined) return;
|
|
316
|
-
const rec = context.manager.getRecord(id);
|
|
317
|
-
if (!rec) return;
|
|
318
|
-
rec.outputFile = createOutputFilePath(context.configCwd, id, transcriptSessionId);
|
|
319
|
-
writeInitialEntry(rec.outputFile, id, params.prompt, ctx.cwd);
|
|
320
|
-
};
|
|
321
|
-
options.onSessionCreated = (session) => {
|
|
322
|
-
const rec = childId === undefined ? undefined : context.manager.getRecord(childId);
|
|
323
|
-
if (rec?.outputFile && childId !== undefined) {
|
|
324
|
-
rec.outputCleanup = streamToOutputFile(session, rec.outputFile, childId, ctx.cwd, undefined);
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
// `ctx` is forwarded to the manager unmodified, never captured at tool-build
|
|
329
|
-
// time: each AgentSession builds its own ExtensionRunner from that session's
|
|
330
|
-
// cwd/sessionManager/modelRegistry, so this is the CHILD's context. Capturing
|
|
331
|
-
// one earlier would silently give a grandchild the wrong worktree base, the
|
|
332
|
-
// wrong conversation under inherit_context, and the wrong inherited model.
|
|
333
|
-
//
|
|
334
|
-
// spawn() throws on strict worktree-isolation failure and cwd validation —
|
|
335
|
-
// report it as a tool error, like the top-level Agent tool does, instead of
|
|
336
|
-
// letting it escape into the child's turn.
|
|
337
|
-
try {
|
|
338
|
-
if (invocation.runInBackground) {
|
|
339
|
-
const id = context.manager.spawn(context.pi, ctx, resolvedType, params.prompt, {
|
|
340
|
-
...options,
|
|
341
|
-
isBackground: true,
|
|
342
|
-
});
|
|
343
|
-
// Synchronous, before the event loop yields — onSessionCreated fires
|
|
344
|
-
// asynchronously inside runAgent, so the file is attached in time.
|
|
345
|
-
attachTranscript(id);
|
|
346
|
-
// Worktree isolation starts the agent asynchronously; surface its
|
|
347
|
-
// failure as a tool error, like the synchronous throw used to.
|
|
348
|
-
await context.manager.awaitStartup(id);
|
|
349
|
-
return textResult(`Nested agent started in background. Agent ID: ${id}`);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
const { record } = await context.manager.spawnAndWait(
|
|
353
|
-
context.pi,
|
|
354
|
-
ctx,
|
|
355
|
-
resolvedType,
|
|
356
|
-
params.prompt,
|
|
357
|
-
{ ...options, signal },
|
|
358
|
-
attachTranscript,
|
|
359
|
-
);
|
|
360
|
-
return textResult(formatRecord(record, "inline"), record.status === "error");
|
|
361
|
-
} catch (err) {
|
|
362
|
-
return textResult(err instanceof Error ? err.message : String(err), true);
|
|
363
|
-
}
|
|
364
|
-
},
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
const resultTool = defineTool({
|
|
368
|
-
name: NESTED_TOOL_NAMES[1],
|
|
369
|
-
label: "Get Nested Agent Result",
|
|
370
|
-
description: "Check or wait for a background nested agent owned by this parent.",
|
|
371
|
-
parameters: Type.Object({
|
|
372
|
-
agent_id: Type.String(),
|
|
373
|
-
wait: Type.Optional(Type.Boolean()),
|
|
374
|
-
}),
|
|
375
|
-
execute: async (_toolCallId, params, signal) => {
|
|
376
|
-
const record = context.manager.getRecord(params.agent_id);
|
|
377
|
-
if (!ownsRecord(record, context.parentAgentId)) {
|
|
378
|
-
return textResult(`Nested agent not found or not owned by this parent: "${params.agent_id}".`, true);
|
|
379
|
-
}
|
|
380
|
-
// Wait for completion if requested. Cancellation (e.g. the parent's tool
|
|
381
|
-
// call is aborted) stops only this wait; the nested child keeps running and
|
|
382
|
-
// stays unconsumed. Queued records have no promise until the manager starts
|
|
383
|
-
// them, so poll — abortably — until they leave the queue, then await.
|
|
384
|
-
if (params.wait && (record.status === "queued" || record.status === "running")) {
|
|
385
|
-
while (record.status === "queued") {
|
|
386
|
-
await abortable(new Promise<void>(resolve => setTimeout(resolve, 250)), signal);
|
|
387
|
-
}
|
|
388
|
-
if (record.promise) await abortable(record.promise, signal);
|
|
389
|
-
}
|
|
390
|
-
return textResult(formatRecord(record, "fetched"), record.status === "error");
|
|
391
|
-
},
|
|
392
|
-
});
|
|
393
|
-
|
|
394
|
-
const steerTool = defineTool({
|
|
395
|
-
name: NESTED_TOOL_NAMES[2],
|
|
396
|
-
label: "Steer Nested Agent",
|
|
397
|
-
description: "Send guidance to a running nested agent owned by this parent.",
|
|
398
|
-
parameters: Type.Object({
|
|
399
|
-
agent_id: Type.String(),
|
|
400
|
-
message: Type.String(),
|
|
401
|
-
}),
|
|
402
|
-
execute: async (_toolCallId, params) => {
|
|
403
|
-
const record = context.manager.getRecord(params.agent_id);
|
|
404
|
-
if (!ownsRecord(record, context.parentAgentId) || record.status !== "running") {
|
|
405
|
-
return textResult(`Running nested agent not found or not owned by this parent: "${params.agent_id}".`, true);
|
|
406
|
-
}
|
|
407
|
-
// Session not ready yet — queue the steer. The manager flushes pending
|
|
408
|
-
// steers when the session is created (same contract as the top-level tool).
|
|
409
|
-
if (!record.session) {
|
|
410
|
-
if (!record.pendingSteers) record.pendingSteers = [];
|
|
411
|
-
record.pendingSteers.push(params.message);
|
|
412
|
-
return textResult(`Steering message queued for nested agent ${params.agent_id}.`);
|
|
413
|
-
}
|
|
414
|
-
try {
|
|
415
|
-
await record.session.steer(params.message);
|
|
416
|
-
} catch (err) {
|
|
417
|
-
return textResult(`Failed to steer nested agent: ${err instanceof Error ? err.message : String(err)}`, true);
|
|
418
|
-
}
|
|
419
|
-
return textResult(`Steering message sent to nested agent ${params.agent_id}.`);
|
|
420
|
-
},
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
return [agentTool, resultTool, steerTool];
|
|
424
|
-
}
|