@xzzpig/pi-subagents 0.1.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 +1621 -0
- package/LICENSE +21 -0
- package/README.md +118 -0
- package/agents/delegate.md +14 -0
- package/agents/oracle.md +74 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/worker.md +58 -0
- package/index.ts +1 -0
- package/inspector-runner.mjs +11 -0
- package/install.mjs +93 -0
- package/package.json +105 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +43 -0
- package/skills/pi-subagents/SKILL.md +41 -0
- package/skills/pi-subagents/references/constraints-and-recipes.md +251 -0
- package/skills/pi-subagents/references/execution-controls.md +428 -0
- package/skills/pi-subagents/references/management-authoring-rpc.md +161 -0
- package/skills/pi-subagents/references/prompting-and-roles.md +242 -0
- package/src/agents/agent-management.ts +1256 -0
- package/src/agents/agent-memory.ts +254 -0
- package/src/agents/agent-refinements.ts +624 -0
- package/src/agents/agent-scope.ts +6 -0
- package/src/agents/agent-selection.ts +25 -0
- package/src/agents/agent-serializer.ts +163 -0
- package/src/agents/agents.ts +1889 -0
- package/src/agents/chain-serializer.ts +280 -0
- package/src/agents/frontmatter.ts +149 -0
- package/src/agents/identity.ts +30 -0
- package/src/agents/proactive-skills.ts +194 -0
- package/src/agents/skills.ts +761 -0
- package/src/api/background-work.ts +197 -0
- package/src/api/capability-ceiling.ts +17 -0
- package/src/api/control-channel.ts +4 -0
- package/src/api/delegation.ts +119 -0
- package/src/api/external-runs.ts +129 -0
- package/src/api/intercom-bridge.ts +3 -0
- package/src/api/pi-args.ts +5 -0
- package/src/api/preflight.ts +403 -0
- package/src/api/shared-types.ts +19 -0
- package/src/extension/config.ts +76 -0
- package/src/extension/control-notices.ts +58 -0
- package/src/extension/doctor.ts +230 -0
- package/src/extension/fanout-child.ts +201 -0
- package/src/extension/index.ts +805 -0
- package/src/extension/public-execution.ts +71 -0
- package/src/extension/rpc.ts +653 -0
- package/src/extension/schemas.ts +375 -0
- package/src/extension/steering-notices.ts +35 -0
- package/src/extension/tool-description.ts +164 -0
- package/src/inspectors/herdr/actions.ts +229 -0
- package/src/inspectors/herdr/client.ts +130 -0
- package/src/inspectors/herdr/inspector-runner.ts +141 -0
- package/src/inspectors/herdr/project-panes.ts +154 -0
- package/src/integrations/herdr-status.ts +330 -0
- package/src/intercom/intercom-bridge.ts +187 -0
- package/src/intercom/native-supervisor-channel.ts +715 -0
- package/src/intercom/result-intercom.ts +421 -0
- package/src/missions/actions.ts +410 -0
- package/src/missions/goal-driver.ts +162 -0
- package/src/missions/lifecycle.ts +346 -0
- package/src/missions/store.ts +507 -0
- package/src/missions/types.ts +157 -0
- package/src/missions/workflow-state.ts +250 -0
- package/src/policy/authority.ts +46 -0
- package/src/profiles/profiles.ts +661 -0
- package/src/runs/background/async-execution.ts +1574 -0
- package/src/runs/background/async-job-tracker.ts +511 -0
- package/src/runs/background/async-resume.ts +539 -0
- package/src/runs/background/async-status.ts +542 -0
- package/src/runs/background/auto-drain.ts +67 -0
- package/src/runs/background/chain-append.ts +328 -0
- package/src/runs/background/chain-root-attachment.ts +199 -0
- package/src/runs/background/completion-batcher.ts +168 -0
- package/src/runs/background/completion-dedupe.ts +54 -0
- package/src/runs/background/control-channel.ts +692 -0
- package/src/runs/background/fleet-view.ts +540 -0
- package/src/runs/background/notify.ts +332 -0
- package/src/runs/background/parallel-groups.ts +45 -0
- package/src/runs/background/process-terminal.ts +280 -0
- package/src/runs/background/result-watcher.ts +414 -0
- package/src/runs/background/retained-children.ts +68 -0
- package/src/runs/background/run-id-resolver.ts +167 -0
- package/src/runs/background/run-status.ts +504 -0
- package/src/runs/background/scheduled-runs.ts +753 -0
- package/src/runs/background/stale-run-reconciler.ts +396 -0
- package/src/runs/background/steering.ts +239 -0
- package/src/runs/background/subagent-runner.ts +4718 -0
- package/src/runs/background/subagent-wait.ts +661 -0
- package/src/runs/background/top-level-async.ts +14 -0
- package/src/runs/background/wait-completions.ts +112 -0
- package/src/runs/background/wait-config.ts +36 -0
- package/src/runs/background/wait-subscriptions.ts +253 -0
- package/src/runs/background/wait-tool.ts +34 -0
- package/src/runs/foreground/async-steering-action.ts +256 -0
- package/src/runs/foreground/async-stop-action.ts +65 -0
- package/src/runs/foreground/chain-clarify.ts +1354 -0
- package/src/runs/foreground/chain-execution.ts +1527 -0
- package/src/runs/foreground/execution.ts +1868 -0
- package/src/runs/foreground/foreground-control.ts +132 -0
- package/src/runs/foreground/subagent-executor.ts +5532 -0
- package/src/runs/shared/acceptance.ts +1365 -0
- package/src/runs/shared/agent-contract.ts +38 -0
- package/src/runs/shared/capability-ceiling.ts +209 -0
- package/src/runs/shared/chain-outputs.ts +118 -0
- package/src/runs/shared/child-protocol.ts +401 -0
- package/src/runs/shared/completion-guard.ts +95 -0
- package/src/runs/shared/context-mode.ts +44 -0
- package/src/runs/shared/dynamic-fanout.ts +297 -0
- package/src/runs/shared/external-cli-runner.ts +130 -0
- package/src/runs/shared/long-running-guard.ts +220 -0
- package/src/runs/shared/mcp-direct-tool-allowlist.ts +406 -0
- package/src/runs/shared/model-fallback.ts +336 -0
- package/src/runs/shared/model-scope.ts +128 -0
- package/src/runs/shared/nested-events.ts +1076 -0
- package/src/runs/shared/nested-path.ts +52 -0
- package/src/runs/shared/nested-render.ts +123 -0
- package/src/runs/shared/parallel-handoff.ts +238 -0
- package/src/runs/shared/parallel-utils.ts +256 -0
- package/src/runs/shared/permissions.ts +95 -0
- package/src/runs/shared/pi-args.ts +808 -0
- package/src/runs/shared/pi-spawn.ts +163 -0
- package/src/runs/shared/process-signal.ts +19 -0
- package/src/runs/shared/run-history.ts +181 -0
- package/src/runs/shared/runtime-acknowledged-extensions.ts +71 -0
- package/src/runs/shared/session-lease.ts +299 -0
- package/src/runs/shared/single-output.ts +235 -0
- package/src/runs/shared/spawn-budget.ts +128 -0
- package/src/runs/shared/structured-output.ts +182 -0
- package/src/runs/shared/subagent-control.ts +231 -0
- package/src/runs/shared/subagent-prompt-runtime.ts +588 -0
- package/src/runs/shared/subagent-startup-retry.ts +104 -0
- package/src/runs/shared/task-intent.ts +181 -0
- package/src/runs/shared/tool-availability.ts +83 -0
- package/src/runs/shared/tool-budget.ts +80 -0
- package/src/runs/shared/turn-budget.ts +98 -0
- package/src/runs/shared/usage-budget.ts +65 -0
- package/src/runs/shared/workflow-graph.ts +231 -0
- package/src/runs/shared/worktree.ts +759 -0
- package/src/shared/accessible-dir.ts +47 -0
- package/src/shared/artifacts.ts +285 -0
- package/src/shared/atomic-json.ts +61 -0
- package/src/shared/child-transcript.ts +264 -0
- package/src/shared/file-coalescer.ts +40 -0
- package/src/shared/file-system-retry.ts +47 -0
- package/src/shared/fork-context.ts +213 -0
- package/src/shared/formatters.ts +133 -0
- package/src/shared/jsonl-writer.ts +81 -0
- package/src/shared/launch-contract.ts +124 -0
- package/src/shared/model-info.ts +81 -0
- package/src/shared/post-exit-stdio-guard.ts +85 -0
- package/src/shared/session-identity.ts +10 -0
- package/src/shared/session-tokens.ts +44 -0
- package/src/shared/settings.ts +501 -0
- package/src/shared/status-format.ts +55 -0
- package/src/shared/types.ts +2069 -0
- package/src/shared/utils.ts +613 -0
- package/src/slash/delegation-adapters.ts +434 -0
- package/src/slash/delegation-json.ts +108 -0
- package/src/slash/delegation-request.ts +166 -0
- package/src/slash/prompt-template-bridge.ts +383 -0
- package/src/slash/prompt-workflows.ts +300 -0
- package/src/slash/selector.ts +147 -0
- package/src/slash/slash-bridge.ts +192 -0
- package/src/slash/slash-commands.ts +994 -0
- package/src/slash/slash-live-state.ts +296 -0
- package/src/slash/subagents-admin.ts +432 -0
- package/src/tui/fleet-status.ts +564 -0
- package/src/tui/fleet-transcript.ts +577 -0
- package/src/tui/fleet.ts +879 -0
- package/src/tui/render-helpers.ts +80 -0
- package/src/tui/render.ts +2029 -0
- package/src/types/pi-runtime-compat.d.ts +8 -0
- package/src/watchdog/change-signature.ts +220 -0
- package/src/watchdog/child-status.ts +205 -0
- package/src/watchdog/emission-guard.ts +123 -0
- package/src/watchdog/lsp-diagnostics.ts +537 -0
- package/src/watchdog/model-selection.ts +167 -0
- package/src/watchdog/permission-arbiter.ts +145 -0
- package/src/watchdog/register-child.ts +117 -0
- package/src/watchdog/register-main.ts +440 -0
- package/src/watchdog/render.ts +54 -0
- package/src/watchdog/review.ts +302 -0
- package/src/watchdog/runtime.ts +868 -0
- package/src/watchdog/scope.ts +62 -0
- package/src/watchdog/settings.ts +568 -0
- package/src/watchdog/tool-actions.ts +155 -0
- package/src/watchdog/turn-delta.ts +161 -0
- package/src/watchdog/types.ts +198 -0
- package/src/watchdog/warning-format.ts +73 -0
- package/src/workflows/chat-progress.ts +140 -0
- package/src/workflows/scripted-workflow.ts +506 -0
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type EditorComponent, isKeyRelease, Key, matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
import { formatModelThinking } from "../shared/formatters.ts";
|
|
4
|
+
import type { AsyncJobStep, FleetViewPlacement, NestedRunSummary, NestedStepSummary, SubagentState } from "../shared/types.ts";
|
|
5
|
+
import { formatWorkflowJsonPreview } from "../workflows/scripted-workflow.ts";
|
|
6
|
+
|
|
7
|
+
export const FLEET_STATUS_WIDGET_KEY = "subagent-fleet-status";
|
|
8
|
+
|
|
9
|
+
// Six rows fit the accepted collapsed hierarchy: one owner, four direct leaves, and overflow.
|
|
10
|
+
const MAX_AGENT_ROWS = 6;
|
|
11
|
+
const REFRESH_MS = 500;
|
|
12
|
+
|
|
13
|
+
type Theme = ExtensionContext["ui"]["theme"];
|
|
14
|
+
type FleetStatusTui = {
|
|
15
|
+
requestRender(): void;
|
|
16
|
+
};
|
|
17
|
+
type FleetStatusEntry = {
|
|
18
|
+
key: string;
|
|
19
|
+
agent: string;
|
|
20
|
+
modelThinking?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
startedAt: number;
|
|
23
|
+
tokens: number;
|
|
24
|
+
state: string;
|
|
25
|
+
nestedChildren?: NestedRunSummary[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type FleetNestedRow = {
|
|
29
|
+
name: string;
|
|
30
|
+
state: NestedRunSummary["state"] | NestedStepSummary["status"];
|
|
31
|
+
modelThinking?: string;
|
|
32
|
+
activity?: string;
|
|
33
|
+
startedAt?: number;
|
|
34
|
+
overflow?: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
type FleetTreeRow =
|
|
38
|
+
| { kind: "owner"; entry: FleetStatusEntry }
|
|
39
|
+
| { kind: "nested"; ownerKey: string; row: FleetNestedRow; last: boolean };
|
|
40
|
+
|
|
41
|
+
export interface FleetStatusOptions {
|
|
42
|
+
refreshMs?: number;
|
|
43
|
+
maxAgentRows?: number;
|
|
44
|
+
placement?: FleetViewPlacement;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function resolveFleetViewPlacement(value: unknown): FleetViewPlacement {
|
|
48
|
+
return value === "aboveEditor" ? "aboveEditor" : "belowEditor";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function formatFleetElapsed(ms: number): string {
|
|
52
|
+
return `${Math.max(0, Math.round(ms / 1000))}s`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function formatFleetTokens(count: number): string {
|
|
56
|
+
let compact: string;
|
|
57
|
+
if (count >= 1_000_000) compact = `${(count / 1_000_000).toFixed(1)}M`;
|
|
58
|
+
else if (count >= 1_000) compact = `${(count / 1_000).toFixed(1)}k`;
|
|
59
|
+
else compact = `${Math.max(0, Math.round(count))}`;
|
|
60
|
+
return `↓ ${compact} tokens`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function rightAlign(left: string, right: string, width: number): string {
|
|
64
|
+
const rightWidth = visibleWidth(right);
|
|
65
|
+
const maxLeftWidth = Math.max(0, width - rightWidth - 1);
|
|
66
|
+
const leftClamped = truncateToWidth(left, maxLeftWidth);
|
|
67
|
+
const gap = Math.max(1, width - visibleWidth(leftClamped) - rightWidth);
|
|
68
|
+
return truncateToWidth(`${leftClamped}${" ".repeat(gap)}${right}`, width);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isActiveState(value: string): boolean {
|
|
72
|
+
return value === "running" || value === "queued" || value === "pending";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function nestedRunLabel(run: NestedRunSummary): string {
|
|
76
|
+
if (run.agent) return run.agent;
|
|
77
|
+
if (run.agents?.length) return run.agents.length === 1 ? run.agents[0]! : `${run.agents.slice(0, 2).join(", ")}${run.agents.length > 2 ? ` +${run.agents.length - 2}` : ""}`;
|
|
78
|
+
return run.id;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function nestedActivity(node: NestedRunSummary | NestedStepSummary): string | undefined {
|
|
82
|
+
if (node.currentTool) return `tool ${node.currentTool}`;
|
|
83
|
+
if (node.currentPath) return node.currentPath.split(/[\\/]/).at(-1);
|
|
84
|
+
if (node.activityState === "needs_attention") return "needs attention";
|
|
85
|
+
if (node.activityState === "active_long_running") return "long-running";
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function nestedStatusGlyph(state: FleetNestedRow["state"], theme: Theme): string {
|
|
90
|
+
if (state === "running") return theme.fg("accent", "●");
|
|
91
|
+
if (state === "queued" || state === "pending") return theme.fg("muted", "◦");
|
|
92
|
+
if (state === "complete" || state === "completed") return theme.fg("success", "✓");
|
|
93
|
+
if (state === "failed" || state === "rejected") return theme.fg("error", "✗");
|
|
94
|
+
return theme.fg("warning", "■");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function nestedFleetRows(children: NestedRunSummary[] | undefined): FleetNestedRow[] {
|
|
98
|
+
const rows: FleetNestedRow[] = [];
|
|
99
|
+
for (const child of children ?? []) {
|
|
100
|
+
const steps = (child.mode === "parallel" || child.mode === "chain") ? child.steps ?? [] : [];
|
|
101
|
+
if (steps.length > 0) {
|
|
102
|
+
for (const step of steps) {
|
|
103
|
+
const modelThinking = formatModelThinking(step.model, step.thinking) || undefined;
|
|
104
|
+
const activity = nestedActivity(step);
|
|
105
|
+
rows.push({
|
|
106
|
+
name: step.agent,
|
|
107
|
+
state: step.status,
|
|
108
|
+
...(modelThinking ? { modelThinking } : {}),
|
|
109
|
+
...(activity ? { activity } : {}),
|
|
110
|
+
...(step.startedAt !== undefined ? { startedAt: step.startedAt } : {}),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const modelThinking = formatModelThinking(child.model, child.thinking) || undefined;
|
|
116
|
+
const activity = nestedActivity(child);
|
|
117
|
+
rows.push({
|
|
118
|
+
name: nestedRunLabel(child),
|
|
119
|
+
state: child.state,
|
|
120
|
+
...(modelThinking ? { modelThinking } : {}),
|
|
121
|
+
...(activity ? { activity } : {}),
|
|
122
|
+
...(child.startedAt !== undefined ? { startedAt: child.startedAt } : {}),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const visible = rows.slice(0, 4);
|
|
126
|
+
if (rows.length > visible.length) visible.push({ name: `… +${rows.length - visible.length} more nested leaves`, state: "complete", overflow: rows.length - visible.length });
|
|
127
|
+
return visible;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function fleetTreeRows(entries: FleetStatusEntry[]): FleetTreeRow[] {
|
|
131
|
+
const rows: FleetTreeRow[] = [];
|
|
132
|
+
for (const entry of entries) {
|
|
133
|
+
rows.push({ kind: "owner", entry });
|
|
134
|
+
const nested = nestedFleetRows(entry.nestedChildren);
|
|
135
|
+
for (const [index, row] of nested.entries()) rows.push({ kind: "nested", ownerKey: entry.key, row, last: index === nested.length - 1 });
|
|
136
|
+
}
|
|
137
|
+
return rows;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function isStaleExtensionContextError(error: unknown): boolean {
|
|
141
|
+
// Pi currently exposes stale contexts as plain Errors without a stable code or subtype.
|
|
142
|
+
return error instanceof Error
|
|
143
|
+
&& (error.message.includes("This extension ctx is stale")
|
|
144
|
+
|| error.message.includes("Extension context no longer active"));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function collectFleetStatusEntries(state: SubagentState): FleetStatusEntry[] {
|
|
148
|
+
const entries: FleetStatusEntry[] = [];
|
|
149
|
+
for (const control of state.foregroundControls.values()) {
|
|
150
|
+
if (control.activeChildren) {
|
|
151
|
+
for (const child of [...control.activeChildren.values()].sort((left, right) => left.index - right.index)) {
|
|
152
|
+
const modelThinking = formatModelThinking(child.model, child.thinking) || undefined;
|
|
153
|
+
entries.push({
|
|
154
|
+
key: `foreground-active:${control.runId}:${child.index}`,
|
|
155
|
+
agent: child.agent,
|
|
156
|
+
...(modelThinking ? { modelThinking } : {}),
|
|
157
|
+
description: child.description,
|
|
158
|
+
startedAt: child.startedAt,
|
|
159
|
+
tokens: child.tokens ?? 0,
|
|
160
|
+
state: "running",
|
|
161
|
+
...((control.nestedChildren?.filter((nested) => nested.parentStepIndex === child.index).length ?? 0) > 0
|
|
162
|
+
? { nestedChildren: control.nestedChildren!.filter((nested) => nested.parentStepIndex === child.index) }
|
|
163
|
+
: control.activeChildren.size === 1 && control.nestedChildren?.length ? { nestedChildren: control.nestedChildren } : {}),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const modelThinking = formatModelThinking(control.model, control.thinking) || undefined;
|
|
169
|
+
entries.push({
|
|
170
|
+
key: `foreground-active:${control.runId}:${control.currentIndex ?? 0}`,
|
|
171
|
+
agent: control.currentAgent ?? control.mode,
|
|
172
|
+
...(modelThinking ? { modelThinking } : {}),
|
|
173
|
+
description: control.description,
|
|
174
|
+
startedAt: control.startedAt,
|
|
175
|
+
tokens: control.tokens ?? 0,
|
|
176
|
+
state: "running",
|
|
177
|
+
...(control.nestedChildren?.length ? { nestedChildren: control.nestedChildren } : {}),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
for (const job of state.asyncJobs.values()) {
|
|
182
|
+
if (!isActiveState(job.status)) continue;
|
|
183
|
+
const startedAt = job.startedAt ?? job.updatedAt ?? Date.now();
|
|
184
|
+
if (job.mode === "workflow") {
|
|
185
|
+
const latestEmit = job.workflow?.emits?.length ? formatWorkflowJsonPreview(job.workflow.emits.at(-1), 120) : undefined;
|
|
186
|
+
entries.push({
|
|
187
|
+
key: `async:${job.asyncId}`,
|
|
188
|
+
agent: "workflow",
|
|
189
|
+
description: latestEmit !== undefined ? `latest emit: ${latestEmit}` : job.description,
|
|
190
|
+
startedAt,
|
|
191
|
+
tokens: job.totalTokens?.total ?? 0,
|
|
192
|
+
state: job.status,
|
|
193
|
+
...(job.nestedChildren?.length ? { nestedChildren: job.nestedChildren } : {}),
|
|
194
|
+
});
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const steps: AsyncJobStep[] | undefined = job.steps?.length
|
|
198
|
+
? job.steps
|
|
199
|
+
: job.agents?.map((agent, index) => {
|
|
200
|
+
const pending = job.status === "queued"
|
|
201
|
+
|| (job.mode === "chain" && !job.activeParallelGroup && index !== (job.currentStep ?? 0));
|
|
202
|
+
return { agent, index, status: pending ? "pending" : "running" };
|
|
203
|
+
});
|
|
204
|
+
if (!steps?.length) {
|
|
205
|
+
entries.push({
|
|
206
|
+
key: `async:${job.asyncId}`,
|
|
207
|
+
agent: job.mode ?? "subagent",
|
|
208
|
+
description: job.description,
|
|
209
|
+
startedAt,
|
|
210
|
+
tokens: job.totalTokens?.total ?? 0,
|
|
211
|
+
state: job.status,
|
|
212
|
+
...(job.nestedChildren?.length ? { nestedChildren: job.nestedChildren } : {}),
|
|
213
|
+
});
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
for (const [offset, step] of steps.entries()) {
|
|
217
|
+
if (!isActiveState(step.status)) continue;
|
|
218
|
+
const index = step.index ?? offset;
|
|
219
|
+
if (step.status === "pending" && job.mode === "chain" && !job.activeParallelGroup && index !== (job.currentStep ?? 0)) continue;
|
|
220
|
+
const modelThinking = formatModelThinking(step.model, step.thinking) || undefined;
|
|
221
|
+
entries.push({
|
|
222
|
+
key: `async:${job.asyncId}:${index}`,
|
|
223
|
+
agent: step.label ? `${step.label} (${step.agent})` : step.agent,
|
|
224
|
+
...(modelThinking ? { modelThinking } : {}),
|
|
225
|
+
description: step.description ?? job.description,
|
|
226
|
+
startedAt: step.startedAt ?? startedAt,
|
|
227
|
+
tokens: step.tokens?.total ?? (steps.length === 1 ? job.totalTokens?.total ?? 0 : 0),
|
|
228
|
+
state: step.status,
|
|
229
|
+
...((step.children?.length ?? 0) > 0
|
|
230
|
+
? { nestedChildren: step.children }
|
|
231
|
+
: job.nestedChildren?.filter((nested) => nested.parentStepIndex === index).length
|
|
232
|
+
? { nestedChildren: job.nestedChildren.filter((nested) => nested.parentStepIndex === index) }
|
|
233
|
+
: {}),
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return entries.sort((left, right) => left.startedAt - right.startedAt || left.key.localeCompare(right.key));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export class SubagentFleetStatus {
|
|
242
|
+
private ctx: ExtensionContext | undefined;
|
|
243
|
+
private ui: ExtensionContext["ui"] | undefined;
|
|
244
|
+
private tui: FleetStatusTui | undefined;
|
|
245
|
+
private inputUnsubscribe: (() => void) | undefined;
|
|
246
|
+
private timer: ReturnType<typeof setInterval> | undefined;
|
|
247
|
+
private widgetRegistered = false;
|
|
248
|
+
private active = false;
|
|
249
|
+
private selectedKey = "main";
|
|
250
|
+
private inspectorOpen = false;
|
|
251
|
+
private lastRenderKey = "";
|
|
252
|
+
private entries: FleetStatusEntry[] = [];
|
|
253
|
+
private readonly state: SubagentState;
|
|
254
|
+
private readonly openInspector: (itemKey: string) => Promise<void> | void;
|
|
255
|
+
private readonly refreshMs: number;
|
|
256
|
+
private readonly maxAgentRows: number;
|
|
257
|
+
private readonly placement: FleetViewPlacement;
|
|
258
|
+
|
|
259
|
+
constructor(
|
|
260
|
+
state: SubagentState,
|
|
261
|
+
openInspector: (itemKey: string) => Promise<void> | void,
|
|
262
|
+
options: FleetStatusOptions = {},
|
|
263
|
+
) {
|
|
264
|
+
this.state = state;
|
|
265
|
+
this.openInspector = openInspector;
|
|
266
|
+
this.refreshMs = options.refreshMs ?? REFRESH_MS;
|
|
267
|
+
this.maxAgentRows = options.maxAgentRows ?? MAX_AGENT_ROWS;
|
|
268
|
+
this.placement = options.placement ?? "belowEditor";
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
setContext(ctx: ExtensionContext): void {
|
|
272
|
+
if (!ctx.hasUI) return;
|
|
273
|
+
const ui = ctx.ui;
|
|
274
|
+
if (this.ui === ui) {
|
|
275
|
+
this.ctx = ctx;
|
|
276
|
+
this.refresh();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
this.clearUiRegistration();
|
|
280
|
+
this.ctx = ctx;
|
|
281
|
+
this.ui = ui;
|
|
282
|
+
if (typeof ui.onTerminalInput === "function") {
|
|
283
|
+
this.inputUnsubscribe = ui.onTerminalInput((data) => this.handleKey(data));
|
|
284
|
+
}
|
|
285
|
+
this.timer = setInterval(() => this.refresh(), this.refreshMs);
|
|
286
|
+
this.timer.unref?.();
|
|
287
|
+
this.refresh();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
dispose(): void {
|
|
291
|
+
this.clearUiRegistration();
|
|
292
|
+
this.ctx = undefined;
|
|
293
|
+
this.ui = undefined;
|
|
294
|
+
this.entries = [];
|
|
295
|
+
this.active = false;
|
|
296
|
+
this.selectedKey = "main";
|
|
297
|
+
this.inspectorOpen = false;
|
|
298
|
+
this.lastRenderKey = "";
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
refresh(): void {
|
|
302
|
+
const ctx = this.getActiveUiContext();
|
|
303
|
+
if (!ctx) return;
|
|
304
|
+
this.entries = collectFleetStatusEntries(this.state);
|
|
305
|
+
this.clampSelection();
|
|
306
|
+
if (this.inspectorOpen || this.state.fleetInspectorOpen) {
|
|
307
|
+
this.lastRenderKey = "";
|
|
308
|
+
if (this.widgetRegistered) {
|
|
309
|
+
ctx.ui.setWidget(FLEET_STATUS_WIDGET_KEY, undefined);
|
|
310
|
+
this.widgetRegistered = false;
|
|
311
|
+
this.tui = undefined;
|
|
312
|
+
}
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
if (this.entries.length === 0) {
|
|
316
|
+
this.active = false;
|
|
317
|
+
this.selectedKey = "main";
|
|
318
|
+
this.lastRenderKey = "";
|
|
319
|
+
if (this.widgetRegistered) {
|
|
320
|
+
ctx.ui.setWidget(FLEET_STATUS_WIDGET_KEY, undefined);
|
|
321
|
+
this.widgetRegistered = false;
|
|
322
|
+
this.tui = undefined;
|
|
323
|
+
}
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const renderKey = this.getRenderKey();
|
|
328
|
+
if (!this.widgetRegistered) {
|
|
329
|
+
ctx.ui.setWidget(FLEET_STATUS_WIDGET_KEY, (tui, theme) => {
|
|
330
|
+
this.tui = tui;
|
|
331
|
+
return {
|
|
332
|
+
render: (width: number) => this.render(width, theme),
|
|
333
|
+
invalidate: () => {
|
|
334
|
+
this.lastRenderKey = "";
|
|
335
|
+
},
|
|
336
|
+
dispose: () => {
|
|
337
|
+
if (this.tui !== tui) return;
|
|
338
|
+
this.widgetRegistered = false;
|
|
339
|
+
this.tui = undefined;
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
}, { placement: this.placement });
|
|
343
|
+
this.widgetRegistered = true;
|
|
344
|
+
this.lastRenderKey = renderKey;
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (renderKey === this.lastRenderKey) return;
|
|
348
|
+
this.lastRenderKey = renderKey;
|
|
349
|
+
this.tui?.requestRender();
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
handleKey(data: string): { consume?: boolean; data?: string } | undefined {
|
|
353
|
+
const ctx = this.getActiveUiContext();
|
|
354
|
+
if (!ctx || this.entries.length === 0 || isKeyRelease(data)) return undefined;
|
|
355
|
+
if (this.inspectorOpen) return undefined;
|
|
356
|
+
if (!this.editorHasFocus()) {
|
|
357
|
+
if (this.active) this.deactivate();
|
|
358
|
+
return undefined;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (!this.active) {
|
|
362
|
+
const activates = matchesKey(data, "down") || matchesKey(data, "left");
|
|
363
|
+
if (!activates || ctx.ui.getEditorText() !== "") return undefined;
|
|
364
|
+
this.active = true;
|
|
365
|
+
this.selectedKey = "main";
|
|
366
|
+
this.refresh();
|
|
367
|
+
return { consume: true };
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const roster = this.rosterKeys();
|
|
371
|
+
const selectedIndex = Math.max(0, roster.indexOf(this.selectedKey));
|
|
372
|
+
if (matchesKey(data, "down") || matchesKey(data, "j")) {
|
|
373
|
+
this.selectedKey = roster[Math.min(roster.length - 1, selectedIndex + 1)] ?? "main";
|
|
374
|
+
this.refresh();
|
|
375
|
+
return { consume: true };
|
|
376
|
+
}
|
|
377
|
+
if (matchesKey(data, "up") || matchesKey(data, "k")) {
|
|
378
|
+
if (selectedIndex === 0) {
|
|
379
|
+
this.deactivate();
|
|
380
|
+
return { consume: true };
|
|
381
|
+
}
|
|
382
|
+
this.selectedKey = roster[selectedIndex - 1] ?? "main";
|
|
383
|
+
this.refresh();
|
|
384
|
+
return { consume: true };
|
|
385
|
+
}
|
|
386
|
+
if (matchesKey(data, "escape")) {
|
|
387
|
+
this.deactivate();
|
|
388
|
+
return { consume: true };
|
|
389
|
+
}
|
|
390
|
+
if (matchesKey(data, Key.enter)) {
|
|
391
|
+
if (this.selectedKey === "main") {
|
|
392
|
+
this.deactivate();
|
|
393
|
+
return { consume: true };
|
|
394
|
+
}
|
|
395
|
+
this.inspectorOpen = true;
|
|
396
|
+
this.refresh();
|
|
397
|
+
const selectedKey = this.selectedKey;
|
|
398
|
+
void Promise.resolve()
|
|
399
|
+
.then(() => this.openInspector(selectedKey))
|
|
400
|
+
.catch((error) => ctx.ui.notify(error instanceof Error ? error.message : String(error), "error"))
|
|
401
|
+
.finally(() => {
|
|
402
|
+
this.inspectorOpen = false;
|
|
403
|
+
this.refresh();
|
|
404
|
+
});
|
|
405
|
+
return { consume: true };
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
this.deactivate();
|
|
409
|
+
return undefined;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
render(width: number, theme: Theme): string[] {
|
|
413
|
+
if (this.entries.length === 0) return [];
|
|
414
|
+
if (!this.active) {
|
|
415
|
+
const tokens = this.entries.reduce((total, entry) => total + entry.tokens, 0);
|
|
416
|
+
const label = `${this.entries.length} active ${this.entries.length === 1 ? "agent" : "agents"}`;
|
|
417
|
+
return [truncateToWidth(` ${theme.fg("muted", label)} · ${theme.fg("dim", `${formatFleetTokens(tokens)} · ↓/← to inspect`)}`, width)];
|
|
418
|
+
}
|
|
419
|
+
const roster = this.rosterKeys();
|
|
420
|
+
const selectedIndex = Math.max(0, roster.indexOf(this.selectedKey));
|
|
421
|
+
const lines = [truncateToWidth(` ${theme.fg("dim", "↑↓/jk select · enter inspect · esc back")}`, width), ""];
|
|
422
|
+
lines.push(truncateToWidth(` ${this.bullet(0, selectedIndex, theme)} main`, width));
|
|
423
|
+
|
|
424
|
+
const tree = fleetTreeRows(this.entries);
|
|
425
|
+
const selectedTreeIndex = Math.max(0, tree.findIndex((row) => row.kind === "owner" && row.entry.key === this.selectedKey));
|
|
426
|
+
const visibleCount = Math.min(this.maxAgentRows, tree.length);
|
|
427
|
+
const start = selectedTreeIndex < visibleCount ? 0 : selectedTreeIndex - visibleCount + 1;
|
|
428
|
+
const hiddenBelow = tree.length - (start + visibleCount);
|
|
429
|
+
if (start > 0) lines.push(rightAlign("", theme.fg("dim", `↑ ${start} more`), width));
|
|
430
|
+
for (let index = start; index < start + visibleCount; index++) {
|
|
431
|
+
const row = tree[index]!;
|
|
432
|
+
if (row.kind === "owner") {
|
|
433
|
+
const ownerIndex = this.entries.findIndex((entry) => entry.key === row.entry.key);
|
|
434
|
+
lines.push(this.renderEntry(ownerIndex + 1, selectedIndex, row.entry, width, theme));
|
|
435
|
+
} else {
|
|
436
|
+
lines.push(this.renderNestedRow(row.row, row.last, width, theme));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (hiddenBelow > 0) lines.push(rightAlign("", theme.fg("dim", `↓ ${hiddenBelow} more`), width));
|
|
440
|
+
return lines;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private renderEntry(rosterIndex: number, selectedIndex: number, entry: FleetStatusEntry, width: number, theme: Theme): string {
|
|
444
|
+
const description = entry.description?.replace(/\s+/g, " ").trim();
|
|
445
|
+
const agent = entry.modelThinking ? `${entry.agent} (${entry.modelThinking})` : entry.agent;
|
|
446
|
+
const left = ` ${this.bullet(rosterIndex, selectedIndex, theme)} ${theme.fg("muted", agent)} · ${entry.state}${description ? ` ${description}` : ""}`;
|
|
447
|
+
const elapsed = Date.now() - entry.startedAt;
|
|
448
|
+
const right = theme.fg("dim", `${formatFleetElapsed(elapsed)} · ${formatFleetTokens(entry.tokens)}`);
|
|
449
|
+
return rightAlign(left, right, width);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private renderNestedRow(row: FleetNestedRow, last: boolean, width: number, theme: Theme): string {
|
|
453
|
+
const marker = last ? "└─" : "├─";
|
|
454
|
+
if (row.overflow !== undefined) return truncateToWidth(` ${marker} ${theme.fg("dim", `+${row.overflow} nested leaves`)}`, width);
|
|
455
|
+
const modelThinking = row.modelThinking ? ` (${row.modelThinking})` : "";
|
|
456
|
+
const activity = row.activity ? ` · ${row.activity}` : "";
|
|
457
|
+
const left = ` ${marker} ${nestedStatusGlyph(row.state, theme)} ${theme.fg("muted", `${row.name}${modelThinking}`)} · ${row.state}${activity}`;
|
|
458
|
+
const elapsed = row.startedAt !== undefined ? ` · ${formatFleetElapsed(Date.now() - row.startedAt)}` : "";
|
|
459
|
+
return truncateToWidth(`${left}${theme.fg("dim", elapsed)}`, width);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private bullet(rosterIndex: number, selectedIndex: number, theme: Theme): string {
|
|
463
|
+
return rosterIndex === selectedIndex ? theme.fg("accent", ">") : " ";
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
private rosterKeys(): string[] {
|
|
467
|
+
return ["main", ...this.entries.map((entry) => entry.key)];
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
private clampSelection(): void {
|
|
471
|
+
if (!this.rosterKeys().includes(this.selectedKey)) this.selectedKey = "main";
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
private deactivate(): void {
|
|
475
|
+
this.active = false;
|
|
476
|
+
this.selectedKey = "main";
|
|
477
|
+
this.refresh();
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
private editorHasFocus(): boolean {
|
|
481
|
+
// pi-tui exposes focus mutation but no focus getter, so inspect the focused
|
|
482
|
+
// component structurally. instanceof is unreliable across jiti module boundaries.
|
|
483
|
+
const focused = (this.tui as unknown as { focusedComponent?: unknown } | undefined)?.focusedComponent;
|
|
484
|
+
if (!focused || typeof focused !== "object") return false;
|
|
485
|
+
const candidate = focused as Partial<EditorComponent>;
|
|
486
|
+
return typeof candidate.render === "function"
|
|
487
|
+
&& typeof candidate.invalidate === "function"
|
|
488
|
+
&& typeof candidate.handleInput === "function"
|
|
489
|
+
&& typeof candidate.getText === "function"
|
|
490
|
+
&& typeof candidate.setText === "function";
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
private getRenderKey(): string {
|
|
494
|
+
const now = Date.now();
|
|
495
|
+
return JSON.stringify({
|
|
496
|
+
active: this.active,
|
|
497
|
+
selected: this.selectedKey,
|
|
498
|
+
inspectorOpen: this.inspectorOpen,
|
|
499
|
+
entries: this.entries.map((entry) => this.active
|
|
500
|
+
? [
|
|
501
|
+
entry.key,
|
|
502
|
+
entry.agent,
|
|
503
|
+
entry.state,
|
|
504
|
+
entry.modelThinking,
|
|
505
|
+
entry.description,
|
|
506
|
+
Math.round((now - entry.startedAt) / 1000),
|
|
507
|
+
entry.tokens,
|
|
508
|
+
entry.nestedChildren?.map((child) => [
|
|
509
|
+
child.id,
|
|
510
|
+
child.state,
|
|
511
|
+
child.model,
|
|
512
|
+
child.thinking,
|
|
513
|
+
child.lastUpdate,
|
|
514
|
+
child.steps?.map((step) => [step.agent, step.status, step.model, step.thinking, step.lastActivityAt]),
|
|
515
|
+
]),
|
|
516
|
+
]
|
|
517
|
+
: [entry.key, entry.state, entry.tokens]),
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
private getActiveUiContext(): ExtensionContext | undefined {
|
|
522
|
+
const ctx = this.ctx;
|
|
523
|
+
if (!ctx) return undefined;
|
|
524
|
+
try {
|
|
525
|
+
return ctx.hasUI ? ctx : undefined;
|
|
526
|
+
} catch (error) {
|
|
527
|
+
if (!isStaleExtensionContextError(error)) throw error;
|
|
528
|
+
this.clearUiRegistration();
|
|
529
|
+
return undefined;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
private clearUiRegistration(): void {
|
|
534
|
+
if (this.timer) clearInterval(this.timer);
|
|
535
|
+
this.timer = undefined;
|
|
536
|
+
|
|
537
|
+
const inputUnsubscribe = this.inputUnsubscribe;
|
|
538
|
+
const ui = this.ui;
|
|
539
|
+
const widgetRegistered = this.widgetRegistered;
|
|
540
|
+
this.inputUnsubscribe = undefined;
|
|
541
|
+
this.ctx = undefined;
|
|
542
|
+
this.ui = undefined;
|
|
543
|
+
this.widgetRegistered = false;
|
|
544
|
+
this.tui = undefined;
|
|
545
|
+
|
|
546
|
+
const cleanupErrors: unknown[] = [];
|
|
547
|
+
try {
|
|
548
|
+
inputUnsubscribe?.();
|
|
549
|
+
} catch (error) {
|
|
550
|
+
if (!isStaleExtensionContextError(error)) cleanupErrors.push(error);
|
|
551
|
+
}
|
|
552
|
+
if (ui && widgetRegistered) {
|
|
553
|
+
try {
|
|
554
|
+
ui.setWidget(FLEET_STATUS_WIDGET_KEY, undefined);
|
|
555
|
+
} catch (error) {
|
|
556
|
+
if (!isStaleExtensionContextError(error)) cleanupErrors.push(error);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
if (cleanupErrors.length === 1) throw cleanupErrors[0];
|
|
560
|
+
if (cleanupErrors.length > 1) {
|
|
561
|
+
throw new AggregateError(cleanupErrors, "Failed to clean up FleetView UI registration");
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|