@timmyjl12/pi-subagents 0.0.0-stage → 0.21.0
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 +862 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +21 -0
- package/README.md +1125 -3
- package/SECURITY.md +97 -0
- package/dist/abortable.d.ts +12 -0
- package/dist/abortable.js +42 -0
- package/dist/active-agents-prompt.d.ts +40 -0
- package/dist/active-agents-prompt.js +98 -0
- package/dist/advisor-status.d.ts +16 -0
- package/dist/advisor-status.js +74 -0
- package/dist/agent-color.d.ts +35 -0
- package/dist/agent-color.js +131 -0
- package/dist/agent-file-toggle.d.ts +125 -0
- package/dist/agent-file-toggle.js +264 -0
- package/dist/agent-manager.d.ts +872 -0
- package/dist/agent-manager.js +2910 -0
- package/dist/agent-runner.d.ts +358 -0
- package/dist/agent-runner.js +1219 -0
- package/dist/agent-types.d.ts +142 -0
- package/dist/agent-types.js +315 -0
- package/dist/awaited-result.d.ts +37 -0
- package/dist/awaited-result.js +150 -0
- package/dist/child-context.d.ts +15 -0
- package/dist/child-context.js +181 -0
- package/dist/child-session-shutdown.d.ts +21 -0
- package/dist/child-session-shutdown.js +97 -0
- package/dist/cleanup-plans.d.ts +10 -0
- package/dist/cleanup-plans.js +112 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +73 -0
- package/dist/cross-extension-rpc.js +192 -0
- package/dist/custom-agents.d.ts +79 -0
- package/dist/custom-agents.js +583 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +125 -0
- package/dist/enabled-models.d.ts +49 -0
- package/dist/enabled-models.js +145 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/error-log.d.ts +64 -0
- package/dist/error-log.js +176 -0
- package/dist/force-main-model.d.ts +21 -0
- package/dist/force-main-model.js +28 -0
- package/dist/goal-bridge.d.ts +155 -0
- package/dist/goal-bridge.js +305 -0
- package/dist/group-join.d.ts +36 -0
- package/dist/group-join.js +161 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +6390 -0
- package/dist/invocation-config.d.ts +164 -0
- package/dist/invocation-config.js +158 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/mention-clone.d.ts +87 -0
- package/dist/mention-clone.js +178 -0
- package/dist/mention.d.ts +81 -0
- package/dist/mention.js +131 -0
- package/dist/model-resolver.d.ts +36 -0
- package/dist/model-resolver.js +95 -0
- package/dist/model-scope.d.ts +49 -0
- package/dist/model-scope.js +48 -0
- package/dist/nested-tools.d.ts +76 -0
- package/dist/nested-tools.js +393 -0
- package/dist/output-diagnostics.d.ts +11 -0
- package/dist/output-diagnostics.js +64 -0
- package/dist/output-file.d.ts +64 -0
- package/dist/output-file.js +351 -0
- package/dist/prompts.d.ts +61 -0
- package/dist/prompts.js +94 -0
- package/dist/recovery.d.ts +56 -0
- package/dist/recovery.js +341 -0
- package/dist/report-cleanup.d.ts +204 -0
- package/dist/report-cleanup.js +1632 -0
- package/dist/report-store.d.ts +738 -0
- package/dist/report-store.js +3612 -0
- package/dist/schedule-store.d.ts +52 -0
- package/dist/schedule-store.js +240 -0
- package/dist/schedule.d.ts +121 -0
- package/dist/schedule.js +420 -0
- package/dist/settings.d.ts +388 -0
- package/dist/settings.js +268 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/standalone-fallback.d.ts +30 -0
- package/dist/standalone-fallback.js +239 -0
- package/dist/status-note.d.ts +61 -0
- package/dist/status-note.js +85 -0
- package/dist/structured-output.d.ts +61 -0
- package/dist/structured-output.js +112 -0
- package/dist/terminal-progress.d.ts +115 -0
- package/dist/terminal-progress.js +206 -0
- package/dist/types.d.ts +411 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-mention.d.ts +82 -0
- package/dist/ui/agent-mention.js +187 -0
- package/dist/ui/agent-widget.d.ts +304 -0
- package/dist/ui/agent-widget.js +770 -0
- package/dist/ui/cleanup-preview.d.ts +59 -0
- package/dist/ui/cleanup-preview.js +466 -0
- package/dist/ui/conversation-viewer.d.ts +105 -0
- package/dist/ui/conversation-viewer.js +544 -0
- package/dist/ui/fleet-list.d.ts +183 -0
- package/dist/ui/fleet-list.js +494 -0
- package/dist/ui/fleet-overlay.d.ts +203 -0
- package/dist/ui/fleet-overlay.js +887 -0
- package/dist/ui/hub-feed.d.ts +52 -0
- package/dist/ui/hub-feed.js +148 -0
- package/dist/ui/native-transcript.d.ts +33 -0
- package/dist/ui/native-transcript.js +234 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +94 -0
- package/dist/ui/select-item.d.ts +27 -0
- package/dist/ui/select-item.js +34 -0
- package/dist/ui/tool-progress.d.ts +22 -0
- package/dist/ui/tool-progress.js +56 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/ui/workflow-card.d.ts +175 -0
- package/dist/ui/workflow-card.js +332 -0
- package/dist/ui/workflow-dialog.d.ts +305 -0
- package/dist/ui/workflow-dialog.js +848 -0
- package/dist/ui/workflow-menu.d.ts +60 -0
- package/dist/ui/workflow-menu.js +147 -0
- package/dist/usage.d.ts +147 -0
- package/dist/usage.js +147 -0
- package/dist/workflow/collisions.d.ts +95 -0
- package/dist/workflow/collisions.js +88 -0
- package/dist/workflow/entry.d.ts +32 -0
- package/dist/workflow/entry.js +29 -0
- package/dist/workflow/host.d.ts +65 -0
- package/dist/workflow/host.js +477 -0
- package/dist/workflow/journal.d.ts +113 -0
- package/dist/workflow/journal.js +177 -0
- package/dist/workflow/json-schema.d.ts +51 -0
- package/dist/workflow/json-schema.js +111 -0
- package/dist/workflow/meta.d.ts +67 -0
- package/dist/workflow/meta.js +317 -0
- package/dist/workflow/progress.d.ts +224 -0
- package/dist/workflow/progress.js +361 -0
- package/dist/workflow/runtime.d.ts +373 -0
- package/dist/workflow/runtime.js +919 -0
- package/dist/workflow/saved.d.ts +90 -0
- package/dist/workflow/saved.js +203 -0
- package/dist/workflow/task.d.ts +145 -0
- package/dist/workflow/task.js +213 -0
- package/dist/workflow/tool-description.d.ts +43 -0
- package/dist/workflow/tool-description.js +204 -0
- package/dist/workflow/worker-source.d.ts +47 -0
- package/dist/workflow/worker-source.js +778 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +164 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +12 -0
- package/docs/DEVELOPMENT.md +40 -0
- package/docs/artifacts.md +93 -0
- package/docs/recovery.md +62 -0
- package/docs/rpc.md +252 -0
- package/docs/runtime-performance.md +53 -0
- package/docs/workflows.md +467 -0
- package/examples/agent-tool-description.md +43 -0
- package/examples/workflows/compose.js +51 -0
- package/examples/workflows/fan-out-audit.js +47 -0
- package/examples/workflows/gated-fix.js +60 -0
- package/examples/workflows/lib/count-child.js +27 -0
- package/examples/workflows/review-panel.js +66 -0
- package/examples/workflows/structured-findings.js +78 -0
- package/package.json +75 -6
- package/src/abortable.ts +43 -0
- package/src/active-agents-prompt.ts +115 -0
- package/src/advisor-status.ts +74 -0
- package/src/agent-color.ts +169 -0
- package/src/agent-file-toggle.ts +273 -0
- package/src/agent-manager.ts +3312 -0
- package/src/agent-runner.ts +1500 -0
- package/src/agent-types.ts +390 -0
- package/src/awaited-result.ts +187 -0
- package/src/child-context.ts +173 -0
- package/src/child-session-shutdown.ts +114 -0
- package/src/cleanup-plans.ts +112 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +257 -0
- package/src/custom-agents.ts +689 -0
- package/src/default-agents.ts +129 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/error-log.ts +202 -0
- package/src/force-main-model.ts +29 -0
- package/src/goal-bridge.ts +468 -0
- package/src/group-join.ts +188 -0
- package/src/index.ts +6856 -0
- package/src/invocation-config.ts +276 -0
- package/src/memory.ts +179 -0
- package/src/mention-clone.ts +221 -0
- package/src/mention.ts +141 -0
- package/src/model-resolver.ts +118 -0
- package/src/model-scope.ts +70 -0
- package/src/nested-tools.ts +553 -0
- package/src/output-diagnostics.ts +68 -0
- package/src/output-file.ts +380 -0
- package/src/prompts.ts +151 -0
- package/src/recovery.ts +320 -0
- package/src/report-cleanup.ts +2044 -0
- package/src/report-store.ts +4130 -0
- package/src/schedule-store.ts +244 -0
- package/src/schedule.ts +450 -0
- package/src/settings.ts +632 -0
- package/src/skill-loader.ts +102 -0
- package/src/standalone-fallback.ts +195 -0
- package/src/status-note.ts +90 -0
- package/src/structured-output.ts +130 -0
- package/src/terminal-progress.ts +215 -0
- package/src/types.ts +424 -0
- package/src/ui/agent-mention.ts +216 -0
- package/src/ui/agent-widget.ts +866 -0
- package/src/ui/cleanup-preview.ts +518 -0
- package/src/ui/conversation-viewer.ts +527 -0
- package/src/ui/fleet-list.ts +563 -0
- package/src/ui/fleet-overlay.ts +1048 -0
- package/src/ui/hub-feed.ts +179 -0
- package/src/ui/native-transcript.ts +224 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/select-item.ts +45 -0
- package/src/ui/tool-progress.ts +67 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/ui/workflow-card.ts +470 -0
- package/src/ui/workflow-dialog.ts +1119 -0
- package/src/ui/workflow-menu.ts +193 -0
- package/src/usage.ts +217 -0
- package/src/workflow/collisions.ts +123 -0
- package/src/workflow/entry.ts +47 -0
- package/src/workflow/host.ts +524 -0
- package/src/workflow/journal.ts +221 -0
- package/src/workflow/json-schema.ts +128 -0
- package/src/workflow/meta.ts +325 -0
- package/src/workflow/progress.ts +550 -0
- package/src/workflow/runtime.ts +1342 -0
- package/src/workflow/saved.ts +217 -0
- package/src/workflow/task.ts +313 -0
- package/src/workflow/tool-description.ts +205 -0
- package/src/workflow/worker-source.ts +781 -0
- package/src/worktree.ts +205 -0
- package/src/xml.ts +13 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-types.ts — Unified agent type registry.
|
|
3
|
+
*
|
|
4
|
+
* Merges embedded default agents with user-defined agents from .pi/agents/*.md, .agents/agents/*.md, and global agents.
|
|
5
|
+
* User agents override defaults with the same name. Disabled agents are kept but excluded from spawning.
|
|
6
|
+
*/
|
|
7
|
+
import type { AgentConfig } from "./types.ts";
|
|
8
|
+
/**
|
|
9
|
+
* Every built-in tool name pi's registry knows, on every platform — the set
|
|
10
|
+
* `tools:` frontmatter is validated against. Derived from pi's own tool
|
|
11
|
+
* factories rather than hardcoded so the set tracks pi-mono if it
|
|
12
|
+
* adds/renames a built-in: `createCodingTools` → read/bash/edit/write;
|
|
13
|
+
* `createReadOnlyTools` → read/grep/find/ls; `powershell` from its own
|
|
14
|
+
* factory (pi 0.84.3 added it — inside the ≥0.86.1 peer floor — and pi's
|
|
15
|
+
* `allToolNames` registry lists it unconditionally). The `cwd` only binds
|
|
16
|
+
* tool operations we never invoke here — we read each tool's `.name` and
|
|
17
|
+
* discard it. Construction is safe everywhere: pi builds these definitions
|
|
18
|
+
* in every session on every platform.
|
|
19
|
+
*/
|
|
20
|
+
export declare const KNOWN_BUILTIN_TOOL_NAMES: string[];
|
|
21
|
+
/**
|
|
22
|
+
* The built-ins `tools: *`/`all` and an omitted `tools:` expand to on THIS
|
|
23
|
+
* platform. `powershell` validates as known everywhere (explicit
|
|
24
|
+
* `tools: powershell` never warns, and pi registers it in every session), but
|
|
25
|
+
* its exec throws off-Windows (pi's `getPowerShellConfig`), so wildcard and
|
|
26
|
+
* full-set agents only advertise it where it can actually run.
|
|
27
|
+
*/
|
|
28
|
+
export declare const BUILTIN_TOOL_NAMES: string[];
|
|
29
|
+
/** Check whether default agents are disabled. */
|
|
30
|
+
export declare function isDefaultsDisabled(): boolean;
|
|
31
|
+
/** Set whether default agents are disabled. */
|
|
32
|
+
export declare function setDefaultsDisabled(b: boolean): void;
|
|
33
|
+
/** `fallbackSubagent` value that disables the fallback entirely (strict dispatch). */
|
|
34
|
+
export declare const NO_FALLBACK = "none";
|
|
35
|
+
/** Get the configured fallback agent type. undefined = strict dispatch. */
|
|
36
|
+
export declare function getFallbackSubagent(): string | undefined;
|
|
37
|
+
/** Set the configured fallback agent type. undefined = strict dispatch. */
|
|
38
|
+
export declare function setFallbackSubagent(v: string | undefined): void;
|
|
39
|
+
/**
|
|
40
|
+
* Build a registry map: DEFAULT_AGENTS first (unless disabled via settings),
|
|
41
|
+
* then user agents overlaid on top (same name overrides the default).
|
|
42
|
+
* Pure — callers that must not disturb the process-wide registry (nested
|
|
43
|
+
* delegation resolving agents from its own config root) build their own map.
|
|
44
|
+
*/
|
|
45
|
+
export declare function buildAgentRegistry(userAgents: Map<string, AgentConfig>): Map<string, AgentConfig>;
|
|
46
|
+
/**
|
|
47
|
+
* Register agents into the unified registry.
|
|
48
|
+
* Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
|
|
49
|
+
* Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
|
|
50
|
+
*/
|
|
51
|
+
export declare function registerAgents(userAgents: Map<string, AgentConfig>): void;
|
|
52
|
+
/**
|
|
53
|
+
* Detach an agent definition from the process-global registry for admission.
|
|
54
|
+
*
|
|
55
|
+
* A spawn can be queued, or paused on an async acquire-side guard, long enough
|
|
56
|
+
* for a settings reload or a mention-driven reload to re-register the same type
|
|
57
|
+
* name. The run must use the definition that identified it when it was admitted,
|
|
58
|
+
* not whatever the registry says minutes later. Strings (including `systemPrompt`)
|
|
59
|
+
* are immutable and shared by reference; only the mutable arrays are copied.
|
|
60
|
+
*/
|
|
61
|
+
export declare function cloneAgentDefinition(config: AgentConfig): AgentConfig;
|
|
62
|
+
export declare function cloneAgentDefinition(config: AgentConfig | undefined): AgentConfig | undefined;
|
|
63
|
+
/** Resolve a type name case-insensitively in a registry. Returns the canonical key or undefined. */
|
|
64
|
+
export declare function resolveTypeIn(registry: Map<string, AgentConfig>, name: string): string | undefined;
|
|
65
|
+
/** Get the agent config for a type (case-insensitive) from a registry. */
|
|
66
|
+
export declare function getAgentConfigIn(registry: Map<string, AgentConfig>, name: string): AgentConfig | undefined;
|
|
67
|
+
/** Check if a type is valid and enabled (case-insensitive) in a registry. */
|
|
68
|
+
export declare function isValidTypeIn(registry: Map<string, AgentConfig>, type: string): boolean;
|
|
69
|
+
/** Get all enabled type names in a registry (for spawning and tool descriptions). */
|
|
70
|
+
export declare function getAvailableTypesIn(registry: Map<string, AgentConfig>): string[];
|
|
71
|
+
/**
|
|
72
|
+
* The canonical key for a caller-supplied name that identifies exactly one
|
|
73
|
+
* ENABLED agent, or undefined. Strict by construction: no fallback, no guessing
|
|
74
|
+
* between case-variants. Nested delegation resolves with this directly, since
|
|
75
|
+
* "unknown types are rejected rather than falling back" is its own contract.
|
|
76
|
+
*/
|
|
77
|
+
export declare function resolveEnabledTypeIn(registry: Map<string, AgentConfig>, requested: unknown): string | undefined;
|
|
78
|
+
/** Outcome of resolving a caller-supplied `subagent_type` into a spawnable type. */
|
|
79
|
+
export type SpawnTypeResolution =
|
|
80
|
+
/** Spawn this type. `fellBackFrom` is set when it isn't what the caller asked for. */
|
|
81
|
+
{
|
|
82
|
+
ok: true;
|
|
83
|
+
type: string;
|
|
84
|
+
fellBackFrom?: string;
|
|
85
|
+
}
|
|
86
|
+
/** Refuse the spawn and return this message to the caller. */
|
|
87
|
+
| {
|
|
88
|
+
ok: false;
|
|
89
|
+
message: string;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Resolve a caller-supplied agent type against a registry, applying the
|
|
93
|
+
* `fallbackSubagent` policy. The single decision point for every caller-supplied
|
|
94
|
+
* spawn — the Agent tool, the scheduler, cross-extension RPC, and the nested
|
|
95
|
+
* tools — so a type that fails here never reaches `runAgent`, where `getConfig`
|
|
96
|
+
* would silently substitute general-purpose.
|
|
97
|
+
*
|
|
98
|
+
* Unknown, disabled, and case-ambiguous names are all treated the same way:
|
|
99
|
+
* the caller named something that doesn't identify exactly one enabled agent.
|
|
100
|
+
*
|
|
101
|
+
* Pure over `registry` — callers that need fresh agent files reload before
|
|
102
|
+
* calling (the Agent tool already does, per spawn). Reloading here would mean
|
|
103
|
+
* importing custom-agents.ts, which imports this module.
|
|
104
|
+
*/
|
|
105
|
+
export declare function resolveSpawnTypeIn(registry: Map<string, AgentConfig>, requested: unknown): SpawnTypeResolution;
|
|
106
|
+
/** Resolve a caller-supplied agent type against the process-wide registry. */
|
|
107
|
+
export declare function resolveSpawnType(requested: unknown): SpawnTypeResolution;
|
|
108
|
+
/** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
|
|
109
|
+
export declare function resolveType(name: string): string | undefined;
|
|
110
|
+
/** Get the agent config for a type (case-insensitive). */
|
|
111
|
+
export declare function getAgentConfig(name: string): AgentConfig | undefined;
|
|
112
|
+
/** Get all enabled type names (for spawning and tool descriptions). */
|
|
113
|
+
export declare function getAvailableTypes(): string[];
|
|
114
|
+
/** Get all type names including disabled (for UI listing). */
|
|
115
|
+
export declare function getAllTypes(): string[];
|
|
116
|
+
/** Get names of default agents currently in the registry. */
|
|
117
|
+
export declare function getDefaultAgentNames(): string[];
|
|
118
|
+
/** Get names of user-defined agents (non-defaults) currently in the registry. */
|
|
119
|
+
export declare function getUserAgentNames(): string[];
|
|
120
|
+
/** Check if a type is valid and enabled (case-insensitive). */
|
|
121
|
+
export declare function isValidType(type: string): boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Get memory tool names (read/write/edit) not already in the provided set.
|
|
124
|
+
*/
|
|
125
|
+
export declare function getMemoryToolNames(existingToolNames: Set<string>): string[];
|
|
126
|
+
/**
|
|
127
|
+
* Get read-only memory tool names not already in the provided set.
|
|
128
|
+
*/
|
|
129
|
+
export declare function getReadOnlyMemoryToolNames(existingToolNames: Set<string>): string[];
|
|
130
|
+
/** Get built-in tool names for a type (case-insensitive). */
|
|
131
|
+
export declare function getToolNamesForType(type: string): string[];
|
|
132
|
+
/** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
|
|
133
|
+
export declare function getConfig(type: string): {
|
|
134
|
+
displayName: string;
|
|
135
|
+
color?: string;
|
|
136
|
+
description: string;
|
|
137
|
+
builtinToolNames: string[];
|
|
138
|
+
extensions: true | string[] | false;
|
|
139
|
+
excludeExtensions?: string[];
|
|
140
|
+
skills: true | string[] | false;
|
|
141
|
+
promptMode: "replace" | "append";
|
|
142
|
+
};
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-types.ts — Unified agent type registry.
|
|
3
|
+
*
|
|
4
|
+
* Merges embedded default agents with user-defined agents from .pi/agents/*.md, .agents/agents/*.md, and global agents.
|
|
5
|
+
* User agents override defaults with the same name. Disabled agents are kept but excluded from spawning.
|
|
6
|
+
*/
|
|
7
|
+
import { createCodingTools, createPowerShellTool, createReadOnlyTools } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { DEFAULT_AGENTS } from "./default-agents.js";
|
|
9
|
+
/**
|
|
10
|
+
* Every built-in tool name pi's registry knows, on every platform — the set
|
|
11
|
+
* `tools:` frontmatter is validated against. Derived from pi's own tool
|
|
12
|
+
* factories rather than hardcoded so the set tracks pi-mono if it
|
|
13
|
+
* adds/renames a built-in: `createCodingTools` → read/bash/edit/write;
|
|
14
|
+
* `createReadOnlyTools` → read/grep/find/ls; `powershell` from its own
|
|
15
|
+
* factory (pi 0.84.3 added it — inside the ≥0.86.1 peer floor — and pi's
|
|
16
|
+
* `allToolNames` registry lists it unconditionally). The `cwd` only binds
|
|
17
|
+
* tool operations we never invoke here — we read each tool's `.name` and
|
|
18
|
+
* discard it. Construction is safe everywhere: pi builds these definitions
|
|
19
|
+
* in every session on every platform.
|
|
20
|
+
*/
|
|
21
|
+
export const KNOWN_BUILTIN_TOOL_NAMES = [
|
|
22
|
+
...new Set([...createCodingTools("."), ...createReadOnlyTools("."), createPowerShellTool(".")].map((t) => t.name)),
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* The built-ins `tools: *`/`all` and an omitted `tools:` expand to on THIS
|
|
26
|
+
* platform. `powershell` validates as known everywhere (explicit
|
|
27
|
+
* `tools: powershell` never warns, and pi registers it in every session), but
|
|
28
|
+
* its exec throws off-Windows (pi's `getPowerShellConfig`), so wildcard and
|
|
29
|
+
* full-set agents only advertise it where it can actually run.
|
|
30
|
+
*/
|
|
31
|
+
export const BUILTIN_TOOL_NAMES = KNOWN_BUILTIN_TOOL_NAMES.filter((name) => name !== "powershell" || process.platform === "win32");
|
|
32
|
+
/** Unified runtime registry of all agents (defaults + user-defined). */
|
|
33
|
+
const agents = new Map();
|
|
34
|
+
/** When true, DEFAULT_AGENTS are skipped during registration. */
|
|
35
|
+
let disableDefaults = false;
|
|
36
|
+
/** Check whether default agents are disabled. */
|
|
37
|
+
export function isDefaultsDisabled() { return disableDefaults; }
|
|
38
|
+
/** Set whether default agents are disabled. */
|
|
39
|
+
export function setDefaultsDisabled(b) { disableDefaults = b; }
|
|
40
|
+
/** `fallbackSubagent` value that disables the fallback entirely (strict dispatch). */
|
|
41
|
+
export const NO_FALLBACK = "none";
|
|
42
|
+
/**
|
|
43
|
+
* Agent type substituted when a caller-supplied `subagent_type` doesn't resolve
|
|
44
|
+
* to exactly one enabled agent. `undefined` and `NO_FALLBACK` both fail closed;
|
|
45
|
+
* a named fallback must be explicitly configured. Set from
|
|
46
|
+
* `subagents.json` (`fallbackSubagent`).
|
|
47
|
+
*
|
|
48
|
+
* Module state rather than an index.ts closure because every caller-supplied
|
|
49
|
+
* spawn path needs it — the Agent tool, the scheduler, and cross-extension RPC.
|
|
50
|
+
*/
|
|
51
|
+
let fallbackSubagent;
|
|
52
|
+
/** Get the configured fallback agent type. undefined = strict dispatch. */
|
|
53
|
+
export function getFallbackSubagent() { return fallbackSubagent; }
|
|
54
|
+
/** Set the configured fallback agent type. undefined = strict dispatch. */
|
|
55
|
+
export function setFallbackSubagent(v) { fallbackSubagent = v; }
|
|
56
|
+
/**
|
|
57
|
+
* Build a registry map: DEFAULT_AGENTS first (unless disabled via settings),
|
|
58
|
+
* then user agents overlaid on top (same name overrides the default).
|
|
59
|
+
* Pure — callers that must not disturb the process-wide registry (nested
|
|
60
|
+
* delegation resolving agents from its own config root) build their own map.
|
|
61
|
+
*/
|
|
62
|
+
export function buildAgentRegistry(userAgents) {
|
|
63
|
+
const registry = new Map();
|
|
64
|
+
if (!disableDefaults) {
|
|
65
|
+
for (const [name, config] of DEFAULT_AGENTS)
|
|
66
|
+
registry.set(name, config);
|
|
67
|
+
}
|
|
68
|
+
for (const [name, config] of userAgents) {
|
|
69
|
+
// A user file overrides a default of the same name regardless of case:
|
|
70
|
+
// `explore.md` (name: explore) must replace built-in `Explore`, not sit
|
|
71
|
+
// beside it while `subagent_type: "Explore"` still hits the default.
|
|
72
|
+
const lower = name.toLowerCase();
|
|
73
|
+
for (const [key, existing] of registry) {
|
|
74
|
+
if (existing.isDefault === true && key !== name && key.toLowerCase() === lower)
|
|
75
|
+
registry.delete(key);
|
|
76
|
+
}
|
|
77
|
+
registry.set(name, config);
|
|
78
|
+
}
|
|
79
|
+
return registry;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Register agents into the unified registry.
|
|
83
|
+
* Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
|
|
84
|
+
* Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
|
|
85
|
+
*/
|
|
86
|
+
export function registerAgents(userAgents) {
|
|
87
|
+
agents.clear();
|
|
88
|
+
for (const [name, config] of buildAgentRegistry(userAgents)) {
|
|
89
|
+
agents.set(name, config);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export function cloneAgentDefinition(config) {
|
|
93
|
+
if (config === undefined)
|
|
94
|
+
return undefined;
|
|
95
|
+
const allowedSubagents = config.allowedSubagents;
|
|
96
|
+
return {
|
|
97
|
+
...config,
|
|
98
|
+
...(config.builtinToolNames === undefined ? {} : { builtinToolNames: [...config.builtinToolNames] }),
|
|
99
|
+
...(config.extSelectors === undefined ? {} : { extSelectors: [...config.extSelectors] }),
|
|
100
|
+
...(config.disallowedTools === undefined ? {} : { disallowedTools: [...config.disallowedTools] }),
|
|
101
|
+
extensions: Array.isArray(config.extensions) ? [...config.extensions] : config.extensions,
|
|
102
|
+
...(config.excludeExtensions === undefined ? {} : { excludeExtensions: [...config.excludeExtensions] }),
|
|
103
|
+
skills: Array.isArray(config.skills) ? [...config.skills] : config.skills,
|
|
104
|
+
...(allowedSubagents === undefined
|
|
105
|
+
? {}
|
|
106
|
+
: { allowedSubagents: Array.isArray(allowedSubagents) ? [...allowedSubagents] : allowedSubagents }),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/** Case-insensitive key resolution within a registry. */
|
|
110
|
+
function resolveKeyIn(registry, name) {
|
|
111
|
+
if (registry.has(name))
|
|
112
|
+
return name;
|
|
113
|
+
const lower = name.toLowerCase();
|
|
114
|
+
for (const key of registry.keys()) {
|
|
115
|
+
if (key.toLowerCase() === lower)
|
|
116
|
+
return key;
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
/** Case-insensitive key resolution. */
|
|
121
|
+
function resolveKey(name) {
|
|
122
|
+
return resolveKeyIn(agents, name);
|
|
123
|
+
}
|
|
124
|
+
/** Resolve a type name case-insensitively in a registry. Returns the canonical key or undefined. */
|
|
125
|
+
export function resolveTypeIn(registry, name) {
|
|
126
|
+
return resolveKeyIn(registry, name);
|
|
127
|
+
}
|
|
128
|
+
/** Get the agent config for a type (case-insensitive) from a registry. */
|
|
129
|
+
export function getAgentConfigIn(registry, name) {
|
|
130
|
+
const key = resolveKeyIn(registry, name);
|
|
131
|
+
return key ? registry.get(key) : undefined;
|
|
132
|
+
}
|
|
133
|
+
/** Check if a type is valid and enabled (case-insensitive) in a registry. */
|
|
134
|
+
export function isValidTypeIn(registry, type) {
|
|
135
|
+
const key = resolveKeyIn(registry, type);
|
|
136
|
+
if (!key)
|
|
137
|
+
return false;
|
|
138
|
+
return registry.get(key)?.enabled !== false;
|
|
139
|
+
}
|
|
140
|
+
/** Get all enabled type names in a registry (for spawning and tool descriptions). */
|
|
141
|
+
export function getAvailableTypesIn(registry) {
|
|
142
|
+
return [...registry.entries()]
|
|
143
|
+
.filter(([_, config]) => config.enabled !== false)
|
|
144
|
+
.map(([name]) => name);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Case-insensitive resolution that refuses to guess. An exact match always wins;
|
|
148
|
+
* otherwise the name must match exactly one key. Two agents differing only in
|
|
149
|
+
* case are reachable (`loadCustomAgents` keys by filename across three
|
|
150
|
+
* directories), and picking whichever came first would silently dispatch a
|
|
151
|
+
* different agent, model and tool policy than the caller meant.
|
|
152
|
+
*/
|
|
153
|
+
function resolveUnambiguousKeyIn(registry, name) {
|
|
154
|
+
if (registry.has(name))
|
|
155
|
+
return name;
|
|
156
|
+
const lower = name.toLowerCase();
|
|
157
|
+
const matches = [...registry.keys()].filter(key => key.toLowerCase() === lower);
|
|
158
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The canonical key for a caller-supplied name that identifies exactly one
|
|
162
|
+
* ENABLED agent, or undefined. Strict by construction: no fallback, no guessing
|
|
163
|
+
* between case-variants. Nested delegation resolves with this directly, since
|
|
164
|
+
* "unknown types are rejected rather than falling back" is its own contract.
|
|
165
|
+
*/
|
|
166
|
+
export function resolveEnabledTypeIn(registry, requested) {
|
|
167
|
+
const raw = typeof requested === "string" ? requested.trim() : "";
|
|
168
|
+
if (!raw)
|
|
169
|
+
return undefined;
|
|
170
|
+
const key = resolveUnambiguousKeyIn(registry, raw);
|
|
171
|
+
return key !== undefined && registry.get(key)?.enabled !== false ? key : undefined;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Resolve a caller-supplied agent type against a registry, applying the
|
|
175
|
+
* `fallbackSubagent` policy. The single decision point for every caller-supplied
|
|
176
|
+
* spawn — the Agent tool, the scheduler, cross-extension RPC, and the nested
|
|
177
|
+
* tools — so a type that fails here never reaches `runAgent`, where `getConfig`
|
|
178
|
+
* would silently substitute general-purpose.
|
|
179
|
+
*
|
|
180
|
+
* Unknown, disabled, and case-ambiguous names are all treated the same way:
|
|
181
|
+
* the caller named something that doesn't identify exactly one enabled agent.
|
|
182
|
+
*
|
|
183
|
+
* Pure over `registry` — callers that need fresh agent files reload before
|
|
184
|
+
* calling (the Agent tool already does, per spawn). Reloading here would mean
|
|
185
|
+
* importing custom-agents.ts, which imports this module.
|
|
186
|
+
*/
|
|
187
|
+
export function resolveSpawnTypeIn(registry, requested) {
|
|
188
|
+
const raw = typeof requested === "string" ? requested.trim() : "";
|
|
189
|
+
const available = () => getAvailableTypesIn(registry).join(", ") || "(none)";
|
|
190
|
+
const key = resolveEnabledTypeIn(registry, raw);
|
|
191
|
+
if (key !== undefined)
|
|
192
|
+
return { ok: true, type: key };
|
|
193
|
+
const ambiguous = raw && !registry.has(raw)
|
|
194
|
+
&& [...registry.keys()].filter(name => name.toLowerCase() === raw.toLowerCase()).length > 1;
|
|
195
|
+
const reason = !raw ? "No agent type given."
|
|
196
|
+
: ambiguous ? `Ambiguous agent type: "${raw}". Use an exact name.`
|
|
197
|
+
: `Unknown or disabled agent type: "${raw}".`;
|
|
198
|
+
// Trimmed like `requested`: a padded value set programmatically would
|
|
199
|
+
// otherwise be reported as a missing agent.
|
|
200
|
+
const configured = typeof fallbackSubagent === "string" ? fallbackSubagent.trim() : undefined;
|
|
201
|
+
if (configured === undefined || configured.toLowerCase() === NO_FALLBACK) {
|
|
202
|
+
return { ok: false, message: `${reason} Choose an enabled subagent_type. Available: ${available()}.` };
|
|
203
|
+
}
|
|
204
|
+
// An unusable explicit fallback is a configuration error, not permission to guess.
|
|
205
|
+
const fallbackKey = resolveUnambiguousKeyIn(registry, configured);
|
|
206
|
+
if (fallbackKey === undefined || registry.get(fallbackKey)?.enabled === false) {
|
|
207
|
+
return {
|
|
208
|
+
ok: false,
|
|
209
|
+
message: `${reason} The configured fallbackSubagent "${configured}" is itself ` +
|
|
210
|
+
`unknown, disabled or ambiguous. Available: ${available()}.`,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
return { ok: true, type: fallbackKey, fellBackFrom: raw };
|
|
214
|
+
}
|
|
215
|
+
/** Resolve a caller-supplied agent type against the process-wide registry. */
|
|
216
|
+
export function resolveSpawnType(requested) {
|
|
217
|
+
return resolveSpawnTypeIn(agents, requested);
|
|
218
|
+
}
|
|
219
|
+
/** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
|
|
220
|
+
export function resolveType(name) {
|
|
221
|
+
return resolveKey(name);
|
|
222
|
+
}
|
|
223
|
+
/** Get the agent config for a type (case-insensitive). */
|
|
224
|
+
export function getAgentConfig(name) {
|
|
225
|
+
return getAgentConfigIn(agents, name);
|
|
226
|
+
}
|
|
227
|
+
/** Get all enabled type names (for spawning and tool descriptions). */
|
|
228
|
+
export function getAvailableTypes() {
|
|
229
|
+
return getAvailableTypesIn(agents);
|
|
230
|
+
}
|
|
231
|
+
/** Get all type names including disabled (for UI listing). */
|
|
232
|
+
export function getAllTypes() {
|
|
233
|
+
return [...agents.keys()];
|
|
234
|
+
}
|
|
235
|
+
/** Get names of default agents currently in the registry. */
|
|
236
|
+
export function getDefaultAgentNames() {
|
|
237
|
+
return [...agents.entries()]
|
|
238
|
+
.filter(([_, config]) => config.isDefault === true)
|
|
239
|
+
.map(([name]) => name);
|
|
240
|
+
}
|
|
241
|
+
/** Get names of user-defined agents (non-defaults) currently in the registry. */
|
|
242
|
+
export function getUserAgentNames() {
|
|
243
|
+
return [...agents.entries()]
|
|
244
|
+
.filter(([_, config]) => config.isDefault !== true)
|
|
245
|
+
.map(([name]) => name);
|
|
246
|
+
}
|
|
247
|
+
/** Check if a type is valid and enabled (case-insensitive). */
|
|
248
|
+
export function isValidType(type) {
|
|
249
|
+
return isValidTypeIn(agents, type);
|
|
250
|
+
}
|
|
251
|
+
/** Tool names required for memory management. */
|
|
252
|
+
const MEMORY_TOOL_NAMES = ["read", "write", "edit"];
|
|
253
|
+
/**
|
|
254
|
+
* Get memory tool names (read/write/edit) not already in the provided set.
|
|
255
|
+
*/
|
|
256
|
+
export function getMemoryToolNames(existingToolNames) {
|
|
257
|
+
return MEMORY_TOOL_NAMES.filter(n => !existingToolNames.has(n));
|
|
258
|
+
}
|
|
259
|
+
/** Tool names needed for read-only memory access. */
|
|
260
|
+
const READONLY_MEMORY_TOOL_NAMES = ["read"];
|
|
261
|
+
/**
|
|
262
|
+
* Get read-only memory tool names not already in the provided set.
|
|
263
|
+
*/
|
|
264
|
+
export function getReadOnlyMemoryToolNames(existingToolNames) {
|
|
265
|
+
return READONLY_MEMORY_TOOL_NAMES.filter(n => !existingToolNames.has(n));
|
|
266
|
+
}
|
|
267
|
+
/** Get built-in tool names for a type (case-insensitive). */
|
|
268
|
+
export function getToolNamesForType(type) {
|
|
269
|
+
const key = resolveKey(type);
|
|
270
|
+
const raw = key ? agents.get(key) : undefined;
|
|
271
|
+
const config = raw?.enabled !== false ? raw : undefined;
|
|
272
|
+
// `undefined` (definition omitted the field) → all built-ins; an explicit `[]`
|
|
273
|
+
// (`tools: none` or a `tools:` with only `ext:` entries) → zero built-ins.
|
|
274
|
+
return config?.builtinToolNames ?? [...BUILTIN_TOOL_NAMES];
|
|
275
|
+
}
|
|
276
|
+
/** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
|
|
277
|
+
export function getConfig(type) {
|
|
278
|
+
const key = resolveKey(type);
|
|
279
|
+
const config = key ? agents.get(key) : undefined;
|
|
280
|
+
if (config && config.enabled !== false) {
|
|
281
|
+
return {
|
|
282
|
+
displayName: config.displayName ?? config.name,
|
|
283
|
+
color: config.color,
|
|
284
|
+
description: config.description,
|
|
285
|
+
builtinToolNames: config.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
286
|
+
extensions: config.extensions,
|
|
287
|
+
excludeExtensions: config.excludeExtensions,
|
|
288
|
+
skills: config.skills,
|
|
289
|
+
promptMode: config.promptMode,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
// Fallback for unknown/disabled types — general-purpose config
|
|
293
|
+
const gp = agents.get("general-purpose");
|
|
294
|
+
if (gp && gp.enabled !== false) {
|
|
295
|
+
return {
|
|
296
|
+
displayName: gp.displayName ?? gp.name,
|
|
297
|
+
color: gp.color,
|
|
298
|
+
description: gp.description,
|
|
299
|
+
builtinToolNames: gp.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
300
|
+
extensions: gp.extensions,
|
|
301
|
+
excludeExtensions: gp.excludeExtensions,
|
|
302
|
+
skills: gp.skills,
|
|
303
|
+
promptMode: gp.promptMode,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
// Absolute fallback (should never happen)
|
|
307
|
+
return {
|
|
308
|
+
displayName: "Agent",
|
|
309
|
+
description: "General-purpose agent for complex, multi-step tasks",
|
|
310
|
+
builtinToolNames: BUILTIN_TOOL_NAMES,
|
|
311
|
+
extensions: true,
|
|
312
|
+
skills: true,
|
|
313
|
+
promptMode: "append",
|
|
314
|
+
};
|
|
315
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ReportLookup } from "./report-store.ts";
|
|
2
|
+
import type { AgentRecord } from "./types.ts";
|
|
3
|
+
/** Bound on the opt-in conversation excerpt appended after the report. */
|
|
4
|
+
export declare const TRANSCRIPT_EXCERPT_MAX_CHARS = 16000;
|
|
5
|
+
export interface FinalReportRequest {
|
|
6
|
+
/** Append the raw conversation. Opt-in per wait; never implied. */
|
|
7
|
+
verbose: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The worker's final answer, framed honestly: a failure keeps its error and the
|
|
11
|
+
* partial output that preceded it, a non-normal terminal status keeps its note,
|
|
12
|
+
* and discarded steering messages are disclosed. This text is the report — it is
|
|
13
|
+
* never truncated, and only `verbose` adds anything after it.
|
|
14
|
+
*/
|
|
15
|
+
export declare function formatFinalReport(record: AgentRecord, request: FinalReportRequest): string;
|
|
16
|
+
/**
|
|
17
|
+
* Format a durable-report lookup for the root `get_subagent_result` surface.
|
|
18
|
+
*
|
|
19
|
+
* This is the post-eviction/reload twin of the live output above, so it keeps
|
|
20
|
+
* the same facts and the same honesty rules:
|
|
21
|
+
*
|
|
22
|
+
* - the stored report is returned in full, never previewed or capped;
|
|
23
|
+
* - absent legacy metadata reads `unknown`, never a fabricated `0`;
|
|
24
|
+
* - provenance names where the report came from;
|
|
25
|
+
* - expiry, deliberate deletion, ambiguity and absence each say what actually
|
|
26
|
+
* happened instead of promoting an older value as current.
|
|
27
|
+
*
|
|
28
|
+
* The caller passes `conversation` only for an opt-in verbose read; this
|
|
29
|
+
* function never reads a session, never notifies, and never bills usage.
|
|
30
|
+
*/
|
|
31
|
+
export interface ArchivedReportFormatOptions {
|
|
32
|
+
/** Show cost in the stats line, matching the live path's setting. */
|
|
33
|
+
showCost?: boolean;
|
|
34
|
+
/** Opt-in raw conversation, already read from verified existing files. */
|
|
35
|
+
conversation?: string;
|
|
36
|
+
}
|
|
37
|
+
export declare function formatArchivedReportLookup(lookup: ReportLookup, options?: ArchivedReportFormatOptions): string;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The complete final report carried by an agent's ordinary completion wakeup.
|
|
3
|
+
*
|
|
4
|
+
* There is deliberately NO report-character cap. The parent needs the worker's
|
|
5
|
+
* whole technical conclusion — finding, evidence, changes, tests, blockers — and
|
|
6
|
+
* a silent truncation hides exactly the part that decides what to do next. The
|
|
7
|
+
* compact 500/300-character previews in `buildNotificationDetails` are a
|
|
8
|
+
* separate thing: they are what a HUMAN sees in the rendered notification, not
|
|
9
|
+
* what the model reads.
|
|
10
|
+
*
|
|
11
|
+
* Only the OPT-IN raw transcript is bounded. It grows with tool output rather
|
|
12
|
+
* than with the answer, so a very long conversation stays a bounded excerpt and
|
|
13
|
+
* points at the settled `get_subagent_result(verbose: true)` call for the rest.
|
|
14
|
+
* The report above it is never cut to fit that excerpt.
|
|
15
|
+
*/
|
|
16
|
+
import { getAgentConversation } from "./agent-runner.js";
|
|
17
|
+
import { getStatusNote, partialOutputSuffix } from "./status-note.js";
|
|
18
|
+
import { getLifetimeCost, getLifetimeTotal } from "./usage.js";
|
|
19
|
+
/** Bound on the opt-in conversation excerpt appended after the report. */
|
|
20
|
+
export const TRANSCRIPT_EXCERPT_MAX_CHARS = 16_000;
|
|
21
|
+
/** Bound the opt-in transcript excerpt and say where the full one lives. */
|
|
22
|
+
function transcriptExcerpt(conversation, agentId) {
|
|
23
|
+
if (conversation.length <= TRANSCRIPT_EXCERPT_MAX_CHARS)
|
|
24
|
+
return conversation;
|
|
25
|
+
const pointer = `\n\n...(transcript excerpt truncated at ${TRANSCRIPT_EXCERPT_MAX_CHARS} characters — ` +
|
|
26
|
+
`get_subagent_result with agent_id "${agentId}" and verbose: true returns the full conversation. ` +
|
|
27
|
+
`The final report above is complete.)`;
|
|
28
|
+
return conversation.slice(0, TRANSCRIPT_EXCERPT_MAX_CHARS).trimEnd() + pointer;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The worker's final answer, framed honestly: a failure keeps its error and the
|
|
32
|
+
* partial output that preceded it, a non-normal terminal status keeps its note,
|
|
33
|
+
* and discarded steering messages are disclosed. This text is the report — it is
|
|
34
|
+
* never truncated, and only `verbose` adds anything after it.
|
|
35
|
+
*/
|
|
36
|
+
export function formatFinalReport(record, request) {
|
|
37
|
+
let text = record.status === "error"
|
|
38
|
+
? `Error: ${record.error ?? "unknown"}${partialOutputSuffix(record)}`
|
|
39
|
+
: (record.result?.trim() || "No output.");
|
|
40
|
+
const statusNote = getStatusNote(record.status);
|
|
41
|
+
if (statusNote)
|
|
42
|
+
text += `\n${statusNote.trim()}`;
|
|
43
|
+
if (record.droppedSteers) {
|
|
44
|
+
const n = record.droppedSteers;
|
|
45
|
+
text +=
|
|
46
|
+
`\n\nNote: ${n} steering message${n === 1 ? "" : "s"} arrived after the agent finished and ` +
|
|
47
|
+
`${n === 1 ? "was" : "were"} discarded.`;
|
|
48
|
+
}
|
|
49
|
+
if (request.verbose && record.session) {
|
|
50
|
+
const conversation = getAgentConversation(record.session);
|
|
51
|
+
if (conversation) {
|
|
52
|
+
text += `\n\n--- Agent Conversation ---\n${transcriptExcerpt(conversation, record.id)}`;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return text;
|
|
56
|
+
}
|
|
57
|
+
function archivedTokens(count) {
|
|
58
|
+
if (count >= 1_000_000)
|
|
59
|
+
return `${(count / 1_000_000).toFixed(1)}M token`;
|
|
60
|
+
if (count >= 1_000)
|
|
61
|
+
return `${(count / 1_000).toFixed(1)}k token`;
|
|
62
|
+
return `${count} token`;
|
|
63
|
+
}
|
|
64
|
+
function archivedCost(cost) {
|
|
65
|
+
if (!(cost > 0))
|
|
66
|
+
return "";
|
|
67
|
+
if (cost < 0.0001)
|
|
68
|
+
return "<$0.0001";
|
|
69
|
+
if (cost >= 1)
|
|
70
|
+
return `~$${cost.toFixed(2)}`;
|
|
71
|
+
return `~$${cost.toFixed(4).replace(/0+$/, "").replace(/\.$/, "")}`;
|
|
72
|
+
}
|
|
73
|
+
export function formatArchivedReportLookup(lookup, options = {}) {
|
|
74
|
+
switch (lookup.status) {
|
|
75
|
+
case "found": {
|
|
76
|
+
const report = lookup.report;
|
|
77
|
+
const stats = [
|
|
78
|
+
`Tool uses: ${report.toolUses === undefined ? "unknown" : report.toolUses}`,
|
|
79
|
+
];
|
|
80
|
+
if (report.startedAt !== undefined && report.completedAt !== undefined) {
|
|
81
|
+
stats.push(`Duration: ${((report.completedAt - report.startedAt) / 1000).toFixed(1)}s`);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
stats.push("Duration: unknown");
|
|
85
|
+
}
|
|
86
|
+
if (report.lifetimeUsage === undefined) {
|
|
87
|
+
stats.push("Tokens: unknown");
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const total = getLifetimeTotal(report.lifetimeUsage);
|
|
91
|
+
if (total > 0)
|
|
92
|
+
stats.push(archivedTokens(total));
|
|
93
|
+
if (options.showCost) {
|
|
94
|
+
const cost = getLifetimeCost(report.lifetimeUsage);
|
|
95
|
+
const text = archivedCost(cost);
|
|
96
|
+
if (text)
|
|
97
|
+
stats.push(`Cost: ${text}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (report.compactionCount)
|
|
101
|
+
stats.push(`Compactions: ${report.compactionCount}`);
|
|
102
|
+
const provenance = lookup.provenance === "archive"
|
|
103
|
+
? "saved report"
|
|
104
|
+
: lookup.provenance === "parent-record"
|
|
105
|
+
? "parent-session record (pre-archive)"
|
|
106
|
+
: "validated legacy transcript metadata";
|
|
107
|
+
let body;
|
|
108
|
+
if (report.status === "error") {
|
|
109
|
+
const partial = report.result?.trim();
|
|
110
|
+
body = `Error: ${report.error ?? "unknown"}${partial ? `\n\nPartial output before the failure:\n${partial}` : ""}`;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
body = report.result?.trim() || report.error?.trim() || "No output.";
|
|
114
|
+
const note = getStatusNote(report.status);
|
|
115
|
+
if (note)
|
|
116
|
+
body += `\n${note.trim()}`;
|
|
117
|
+
}
|
|
118
|
+
if (report.droppedSteers) {
|
|
119
|
+
const n = report.droppedSteers;
|
|
120
|
+
body +=
|
|
121
|
+
`\n\nNote: ${n} steering message${n === 1 ? "" : "s"} arrived after the agent finished and ` +
|
|
122
|
+
`${n === 1 ? "was" : "were"} discarded.`;
|
|
123
|
+
}
|
|
124
|
+
let output = `Agent: ${report.id}\n` +
|
|
125
|
+
`Type: ${report.type} | Status: ${report.status}${getStatusNote(report.status)} | ${stats.join(" | ")}\n` +
|
|
126
|
+
`Description: ${report.description}\n` +
|
|
127
|
+
`Recovered from: ${provenance} (root session ${lookup.rootSessionId})\n\n` +
|
|
128
|
+
body;
|
|
129
|
+
if (options.conversation !== undefined) {
|
|
130
|
+
output += `\n\n--- Agent Conversation ---\n${options.conversation}`;
|
|
131
|
+
}
|
|
132
|
+
return output;
|
|
133
|
+
}
|
|
134
|
+
case "expired":
|
|
135
|
+
return (`Agent ${lookup.entry.id} is no longer available: its saved report expired at ` +
|
|
136
|
+
`${new Date(lookup.expiredAt).toISOString()} (30-day retention). ` +
|
|
137
|
+
`The complete result was removed; no older value is being presented as current.`);
|
|
138
|
+
case "deleted":
|
|
139
|
+
return (`Agent ${lookup.entry.id}'s saved report was deliberately deleted${lookup.reason === undefined ? "" : ` (${lookup.reason})`} at ${new Date(lookup.deletedAt).toISOString()}. ` +
|
|
140
|
+
`Deliberate removal is final: the report will not be resurrected from history or a transcript.`);
|
|
141
|
+
case "ambiguous":
|
|
142
|
+
return (`Agent reference "${lookup.ref}" is ambiguous in the saved archive: ` +
|
|
143
|
+
`${lookup.candidates.map(candidate => candidate.id).join(", ")}. ` +
|
|
144
|
+
`No result was chosen — use the exact 17-character agent id.`);
|
|
145
|
+
case "missing": {
|
|
146
|
+
const reason = lookup.reason.replace(/[.]+$/, "");
|
|
147
|
+
return `Agent not found: "${lookup.ref}". ${reason}. It may have been cleaned up.`;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function inChildSessionContext(): boolean;
|
|
2
|
+
export declare function runInChildSessionContext<T>(fn: () => Promise<T>): Promise<T>;
|
|
3
|
+
/**
|
|
4
|
+
* Run `fn` with `process.env.PI_SUBAGENT_CHILD === "1"`, the way a genuinely
|
|
5
|
+
* forked child process would inherit it. Restores the previous state when the
|
|
6
|
+
* outermost overlapping region exits: the variable is deleted again when it
|
|
7
|
+
* was unset, otherwise its prior value is put back.
|
|
8
|
+
*/
|
|
9
|
+
export declare function runAsSubagentProcess<T>(fn: () => Promise<T>): Promise<T>;
|
|
10
|
+
/**
|
|
11
|
+
* Bind with pi-advisor's explicit child override, held exclusively only while
|
|
12
|
+
* fn runs. Queued binds may abort or time out without poisoning the gate.
|
|
13
|
+
* An operator's concurrent subagent toggle outlives our temporary "1".
|
|
14
|
+
*/
|
|
15
|
+
export declare function runWithAdvisorBind<T>(override: boolean | undefined, fn: () => Promise<T>, signal?: AbortSignal): Promise<T>;
|