@restranger/repi-subagents 0.0.0-stage → 0.0.1-ca9b82c
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/README.md +277 -2
- package/package.json +40 -5
- package/src/agent-file.ts +297 -0
- package/src/agent-schema.ts +90 -0
- package/src/child-guard.ts +151 -0
- package/src/command.ts +256 -0
- package/src/default-agents.ts +409 -0
- package/src/discovery.ts +354 -0
- package/src/extensions.ts +119 -0
- package/src/fleet-ui.ts +472 -0
- package/src/format.ts +974 -0
- package/src/frontmatter.ts +302 -0
- package/src/index.ts +112 -0
- package/src/manager.ts +1192 -0
- package/src/model-resolver.ts +58 -0
- package/src/overrides.ts +693 -0
- package/src/plugin.ts +568 -0
- package/src/prompt.ts +184 -0
- package/src/registry.ts +71 -0
- package/src/session.ts +304 -0
- package/src/status-widget.ts +65 -0
- package/src/tool-policy.ts +77 -0
- package/src/tools.ts +554 -0
- package/src/types.ts +163 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One schema for agent fields.
|
|
3
|
+
*
|
|
4
|
+
* Markdown frontmatter and `.repi/subagents.json` overrides used to keep separate key lists, so a
|
|
5
|
+
* field could be accepted by one layer and silently ignored by the other (`allowed_tools`,
|
|
6
|
+
* `max-turns`, `run_in_background` were all in that state). Every accepted spelling now lives here:
|
|
7
|
+
* `parseAgentFile` reads through it and the override layer validates against it.
|
|
8
|
+
*
|
|
9
|
+
* The order inside each list is the resolution priority: the first spelling present in the file
|
|
10
|
+
* wins, matching the explicit `a ?? b ?? c` chains this replaced.
|
|
11
|
+
*/
|
|
12
|
+
export const AGENT_FIELDS = {
|
|
13
|
+
description: ["description"],
|
|
14
|
+
tools: ["tools", "allowed-tools", "allowed_tools", "allowedTools"],
|
|
15
|
+
disallowedTools: ["disallowedTools", "disallowed-tools", "disallowed_tools"],
|
|
16
|
+
model: ["model"],
|
|
17
|
+
fallbackModels: [
|
|
18
|
+
"fallback_models",
|
|
19
|
+
"fallbackModels",
|
|
20
|
+
"fallbacks",
|
|
21
|
+
"fallback_model",
|
|
22
|
+
"fallback-model",
|
|
23
|
+
"fallback-models",
|
|
24
|
+
"fallbackModel",
|
|
25
|
+
],
|
|
26
|
+
thinking: ["thinking", "effort"],
|
|
27
|
+
maxTurns: ["max_turns", "maxTurns", "max-turns"],
|
|
28
|
+
background: ["background", "run_in_background", "runInBackground", "run-in-background"],
|
|
29
|
+
// `systemPromptMode` stays first: it is the documented Claude Code spelling.
|
|
30
|
+
promptMode: [
|
|
31
|
+
"systemPromptMode",
|
|
32
|
+
"prompt_mode",
|
|
33
|
+
"promptMode",
|
|
34
|
+
"system_prompt_mode",
|
|
35
|
+
"prompt-mode",
|
|
36
|
+
"system-prompt-mode",
|
|
37
|
+
],
|
|
38
|
+
color: ["color"],
|
|
39
|
+
enabled: ["enabled"],
|
|
40
|
+
} as const;
|
|
41
|
+
|
|
42
|
+
export type AgentFieldName = keyof typeof AGENT_FIELDS;
|
|
43
|
+
|
|
44
|
+
/** Frontmatter-only spellings: the agent name (the file name is the fallback). */
|
|
45
|
+
export const FRONTMATTER_ONLY_FIELDS = {
|
|
46
|
+
name: ["name", "display_name"],
|
|
47
|
+
} as const;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Fields that exist only in the JSON override layer.
|
|
51
|
+
*
|
|
52
|
+
* In Markdown the body *is* the prompt, so `systemPrompt` there is deliberately rejected instead
|
|
53
|
+
* of silently ignored; a JSON override may replace the body.
|
|
54
|
+
*/
|
|
55
|
+
export const OVERRIDE_ONLY_ALIASES: Readonly<Record<string, string>> = {
|
|
56
|
+
systemPrompt: "systemPrompt",
|
|
57
|
+
prompt: "systemPrompt",
|
|
58
|
+
system_prompt: "systemPrompt",
|
|
59
|
+
"system-prompt": "systemPrompt",
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const CANONICAL_BY_ALIAS = new Map<string, AgentFieldName>();
|
|
63
|
+
for (const [canonical, aliases] of Object.entries(AGENT_FIELDS)) {
|
|
64
|
+
for (const alias of aliases) {
|
|
65
|
+
CANONICAL_BY_ALIAS.set(alias, canonical as AgentFieldName);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Canonical field for any accepted override spelling, or `undefined` for an unknown key. */
|
|
70
|
+
export function canonicalAgentField(key: string): AgentFieldName | undefined {
|
|
71
|
+
return CANONICAL_BY_ALIAS.get(key);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Every frontmatter key this parser consumes, including aliases and the agent name. */
|
|
75
|
+
export const FRONTMATTER_CONSUMED_KEYS: ReadonlySet<string> = new Set([
|
|
76
|
+
...CANONICAL_BY_ALIAS.keys(),
|
|
77
|
+
...FRONTMATTER_ONLY_FIELDS.name,
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
/** First present spelling of a field, in schema priority order. */
|
|
81
|
+
export function agentFieldValue<T>(data: Readonly<Record<string, T>>, field: AgentFieldName | "name"): T | undefined {
|
|
82
|
+
const aliases: readonly string[] = field === "name" ? FRONTMATTER_ONLY_FIELDS.name : AGENT_FIELDS[field];
|
|
83
|
+
for (const alias of aliases) {
|
|
84
|
+
const value = data[alias];
|
|
85
|
+
if (value !== undefined) {
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { PermissionService } from "@restranger/repi-lib";
|
|
2
|
+
|
|
3
|
+
export interface SubagentConfirmationRequest {
|
|
4
|
+
readonly tool: string;
|
|
5
|
+
readonly input: Record<string, unknown>;
|
|
6
|
+
readonly prompt: string;
|
|
7
|
+
readonly reason?: string;
|
|
8
|
+
readonly signal?: AbortSignal;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SubagentGuardOptions {
|
|
12
|
+
/** `undefined` allows every tool the child session exposes. */
|
|
13
|
+
readonly allowedTools?: readonly string[];
|
|
14
|
+
readonly permission?: PermissionService;
|
|
15
|
+
/**
|
|
16
|
+
* Routes an `ask` decision to the parent's interactive UI. Without it, `ask` fails closed: a
|
|
17
|
+
* child must never silently approve what the parent policy wanted a human to confirm.
|
|
18
|
+
*/
|
|
19
|
+
readonly requestConfirmation?: (request: SubagentConfirmationRequest) => Promise<boolean>;
|
|
20
|
+
readonly parentSessionId: string;
|
|
21
|
+
readonly cwd: string;
|
|
22
|
+
readonly signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface GuardResult {
|
|
26
|
+
readonly block: boolean;
|
|
27
|
+
readonly reason?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The child-side permission seam.
|
|
32
|
+
*
|
|
33
|
+
* It never constructs a second permissions engine: every call is delegated to the parent session's
|
|
34
|
+
* `re:permissions@1` service, tagged `source: "subagent"`, so hard denies and the parent's active
|
|
35
|
+
* mode keep applying. An `ask` decision is routed to the parent's UI through
|
|
36
|
+
* `requestConfirmation`; when the host cannot ask, it fails closed instead of blocking every child
|
|
37
|
+
* tool by default.
|
|
38
|
+
*/
|
|
39
|
+
export function createSubagentGuardExtension(options: SubagentGuardOptions): (pi: unknown) => void {
|
|
40
|
+
return (pi) => {
|
|
41
|
+
const api = pi as { on?: (event: string, handler: (event: unknown) => unknown) => void };
|
|
42
|
+
api.on?.("tool_call", async (event) => guardToolCall(event, options));
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Exported separately so the policy is testable without a Pi extension runtime. */
|
|
47
|
+
export async function guardToolCall(event: unknown, options: SubagentGuardOptions): Promise<GuardResult> {
|
|
48
|
+
const record = isRecord(event) ? event : undefined;
|
|
49
|
+
const tool = stringField(record, "tool") ?? stringField(record, "toolName") ?? stringField(record, "name");
|
|
50
|
+
if (!tool) {
|
|
51
|
+
return { block: true, reason: "Subagent tool_call has no tool name (fail-closed)." };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (options.allowedTools && !options.allowedTools.includes(tool)) {
|
|
55
|
+
return {
|
|
56
|
+
block: true,
|
|
57
|
+
reason: `Tool "${tool}" is not in the agent's declared tool allowlist.`,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!options.permission) {
|
|
62
|
+
return { block: false };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// The host's event shape varies (`input`, Claude-style `arguments`, `args`). Resolve it once so
|
|
66
|
+
// the rewrite branch can mutate the same object the check read.
|
|
67
|
+
const input = isRecord(record?.input)
|
|
68
|
+
? record.input
|
|
69
|
+
: isRecord(record?.arguments)
|
|
70
|
+
? record.arguments
|
|
71
|
+
: isRecord(record?.args)
|
|
72
|
+
? record.args
|
|
73
|
+
: undefined;
|
|
74
|
+
|
|
75
|
+
let decision: Awaited<ReturnType<PermissionService["check"]>>;
|
|
76
|
+
try {
|
|
77
|
+
decision = await options.permission.check({
|
|
78
|
+
tool,
|
|
79
|
+
input: input ?? {},
|
|
80
|
+
cwd: options.cwd,
|
|
81
|
+
source: "subagent",
|
|
82
|
+
sessionId: options.parentSessionId,
|
|
83
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
84
|
+
});
|
|
85
|
+
} catch (err) {
|
|
86
|
+
return {
|
|
87
|
+
block: true,
|
|
88
|
+
reason: `Subagent permission check failed (fail-closed): ${errorMessage(err)}`,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
switch (decision.action) {
|
|
93
|
+
case "allow":
|
|
94
|
+
return { block: false };
|
|
95
|
+
case "deny":
|
|
96
|
+
return { block: true, reason: decision.reason };
|
|
97
|
+
case "ask": {
|
|
98
|
+
const promptText = decision.prompt ?? `Allow tool ${tool}?`;
|
|
99
|
+
if (!options.requestConfirmation) {
|
|
100
|
+
return {
|
|
101
|
+
block: true,
|
|
102
|
+
reason: `Subagent permission requires confirmation, but this host cannot ask (fail-closed): ${promptText}`,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
let approved: boolean;
|
|
106
|
+
try {
|
|
107
|
+
approved = await options.requestConfirmation({
|
|
108
|
+
tool,
|
|
109
|
+
input: input ?? {},
|
|
110
|
+
prompt: promptText,
|
|
111
|
+
...(decision.reason ? { reason: decision.reason } : {}),
|
|
112
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
113
|
+
});
|
|
114
|
+
} catch (err) {
|
|
115
|
+
return {
|
|
116
|
+
block: true,
|
|
117
|
+
reason: `Subagent permission prompt failed (fail-closed): ${errorMessage(err)}`,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return approved ? { block: false } : { block: true, reason: `Subagent permission was denied: ${promptText}` };
|
|
121
|
+
}
|
|
122
|
+
case "rewrite": {
|
|
123
|
+
if (!input) {
|
|
124
|
+
return {
|
|
125
|
+
block: true,
|
|
126
|
+
reason: "Subagent permission rewrite could not be applied to a non-object input (fail-closed).",
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
for (const key of Object.keys(input)) {
|
|
130
|
+
delete input[key];
|
|
131
|
+
}
|
|
132
|
+
Object.assign(input, decision.modifiedInput);
|
|
133
|
+
return { block: false };
|
|
134
|
+
}
|
|
135
|
+
default:
|
|
136
|
+
return { block: true, reason: "Unknown permission decision for subagent (fail-closed)." };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
141
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function stringField(record: Record<string, unknown> | undefined, key: string): string | undefined {
|
|
145
|
+
const value = record?.[key];
|
|
146
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function errorMessage(error: unknown): string {
|
|
150
|
+
return error instanceof Error ? error.message : String(error);
|
|
151
|
+
}
|
package/src/command.ts
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { type ArgumentChoice, type PiContext, type PiExtensionAPI, completeArguments } from "@restranger/repi-lib";
|
|
2
|
+
import { FleetView } from "./fleet-ui.js";
|
|
3
|
+
import { formatAgentCatalog, formatDiagnostics, formatFleetDashboard, formatRunList } from "./format.js";
|
|
4
|
+
import type { SubagentManager } from "./manager.js";
|
|
5
|
+
import { resolveMaxConcurrent } from "./plugin.js";
|
|
6
|
+
import type { AgentRegistry } from "./registry.js";
|
|
7
|
+
import type { AgentScanResult, SubagentRunSnapshot } from "./types.js";
|
|
8
|
+
|
|
9
|
+
function errorMessage(error: unknown): string {
|
|
10
|
+
return error instanceof Error ? error.message : String(error);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SubagentsCommandSessionState {
|
|
14
|
+
ctx: PiContext;
|
|
15
|
+
readonly registry: AgentRegistry;
|
|
16
|
+
readonly manager: SubagentManager;
|
|
17
|
+
readonly notified: Set<string>;
|
|
18
|
+
fleet?: FleetView;
|
|
19
|
+
widgetMounted: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SubagentsCommandSession {
|
|
23
|
+
readonly sessionId: string;
|
|
24
|
+
readonly state: SubagentsCommandSessionState;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface SubagentsCommandOptions {
|
|
28
|
+
sessionFor(ctx: PiContext | undefined): SubagentsCommandSession | undefined;
|
|
29
|
+
getActiveRuns(): readonly SubagentRunSnapshot[];
|
|
30
|
+
refreshSession(sessionId: string): void;
|
|
31
|
+
notifyDiagnostics(state: SubagentsCommandSessionState): void;
|
|
32
|
+
onReload?(registry: AgentRegistry): void;
|
|
33
|
+
maxConcurrent?: number;
|
|
34
|
+
now?: () => number;
|
|
35
|
+
widgetKey?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function openFleet(
|
|
39
|
+
ctx: PiContext,
|
|
40
|
+
sessionId: string,
|
|
41
|
+
state: SubagentsCommandSessionState,
|
|
42
|
+
options: {
|
|
43
|
+
now?: () => number;
|
|
44
|
+
refreshSession(sessionId: string): void;
|
|
45
|
+
widgetKey?: string;
|
|
46
|
+
},
|
|
47
|
+
initialId?: string,
|
|
48
|
+
): Promise<void> {
|
|
49
|
+
const now = options.now ?? (() => Date.now());
|
|
50
|
+
const runs = state.manager.list(sessionId);
|
|
51
|
+
if (typeof ctx.ui.custom !== "function") {
|
|
52
|
+
ctx.ui.notify(formatFleetDashboard(runs, now(), "⠋", { width: 100 }).join("\n"), "info");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (state.fleet) {
|
|
56
|
+
state.fleet.refresh();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const view = new FleetView({
|
|
60
|
+
runs: () => state.manager.list(sessionId),
|
|
61
|
+
now,
|
|
62
|
+
...(initialId ? { initialId } : {}),
|
|
63
|
+
onCancel: (id) => {
|
|
64
|
+
state.manager.abort(id, sessionId);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
state.fleet = view;
|
|
68
|
+
try {
|
|
69
|
+
ctx.ui.setWidget?.(options.widgetKey ?? "repi-subagents", undefined);
|
|
70
|
+
state.widgetMounted = false;
|
|
71
|
+
} catch {
|
|
72
|
+
// ignore
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
await ctx.ui.custom<undefined>(
|
|
76
|
+
(tui, theme, keybindings, done) => {
|
|
77
|
+
view.attach(tui, theme, keybindings, done);
|
|
78
|
+
return view;
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
overlay: true,
|
|
82
|
+
overlayOptions: {
|
|
83
|
+
anchor: "center",
|
|
84
|
+
width: "95%",
|
|
85
|
+
minWidth: 60,
|
|
86
|
+
maxHeight: "85%",
|
|
87
|
+
margin: 1,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
ctx.ui.notify(`Fleet dashboard failed: ${errorMessage(err)}`, "error");
|
|
93
|
+
} finally {
|
|
94
|
+
if (state.fleet === view) {
|
|
95
|
+
state.fleet = undefined;
|
|
96
|
+
}
|
|
97
|
+
options.refreshSession(sessionId);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* `/subagents` arguments, with the run ids of the active session spliced in.
|
|
103
|
+
*
|
|
104
|
+
* Ids are generated (`r-…`), so nobody can type one from memory; suggesting the runs that are
|
|
105
|
+
* actually in flight is what makes `stop` and `fleet` usable without opening the dashboard.
|
|
106
|
+
*/
|
|
107
|
+
export function subagentArguments(runs: readonly SubagentRunSnapshot[]): readonly ArgumentChoice[] {
|
|
108
|
+
const describe = (run: SubagentRunSnapshot): ArgumentChoice => ({
|
|
109
|
+
name: run.id,
|
|
110
|
+
description: `${run.agent} · ${run.status}`,
|
|
111
|
+
});
|
|
112
|
+
const active = runs.filter((run) => run.status === "running" || run.status === "queued");
|
|
113
|
+
return [
|
|
114
|
+
{
|
|
115
|
+
name: "fleet",
|
|
116
|
+
description: "Open the fleet dashboard",
|
|
117
|
+
...(runs.length > 0 ? { options: runs.map(describe) } : {}),
|
|
118
|
+
},
|
|
119
|
+
{ name: "list", description: "List agents, runs, override configs and diagnostics" },
|
|
120
|
+
{ name: "reload", description: "Re-scan agent directories" },
|
|
121
|
+
{
|
|
122
|
+
name: "stop",
|
|
123
|
+
description: "Stop one run by id, or every active run",
|
|
124
|
+
...(active.length > 0 ? { options: active.map(describe) } : {}),
|
|
125
|
+
},
|
|
126
|
+
];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function createSubagentsCommandHandler(
|
|
130
|
+
options: SubagentsCommandOptions,
|
|
131
|
+
): (args: string, ctx: PiContext) => Promise<void> {
|
|
132
|
+
return async (args: string, ctx: PiContext): Promise<void> => {
|
|
133
|
+
const resolved = options.sessionFor(ctx);
|
|
134
|
+
if (!resolved) {
|
|
135
|
+
ctx?.ui?.notify?.("Subagents requires a Pi session context.", "error");
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const trimmed = args.trim();
|
|
139
|
+
const firstSpace = trimmed.search(/\s/);
|
|
140
|
+
const subcommand = (firstSpace === -1 ? trimmed : trimmed.slice(0, firstSpace)).toLowerCase();
|
|
141
|
+
const subArgs = firstSpace === -1 ? "" : trimmed.slice(firstSpace).trim();
|
|
142
|
+
|
|
143
|
+
if (subcommand === "reload") {
|
|
144
|
+
let scan: AgentScanResult;
|
|
145
|
+
try {
|
|
146
|
+
scan = resolved.state.registry.reload({
|
|
147
|
+
cwd: ctx.cwd,
|
|
148
|
+
// Same rule as the initial scan: only an explicit `true` may read project files.
|
|
149
|
+
workspaceTrusted: ctx.isProjectTrusted?.() === true,
|
|
150
|
+
});
|
|
151
|
+
const newMax = resolveMaxConcurrent({
|
|
152
|
+
explicit: options.maxConcurrent,
|
|
153
|
+
config: scan.maxConcurrent,
|
|
154
|
+
});
|
|
155
|
+
resolved.state.manager.setMaxConcurrent(newMax);
|
|
156
|
+
} catch (err) {
|
|
157
|
+
ctx.ui.notify(`Subagents reload failed: ${errorMessage(err)}`, "error");
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
options.onReload?.(resolved.state.registry);
|
|
161
|
+
resolved.state.notified.clear();
|
|
162
|
+
options.notifyDiagnostics(resolved.state);
|
|
163
|
+
ctx.ui.notify(
|
|
164
|
+
`Subagents reloaded: ${scan.agents.length} agent${scan.agents.length === 1 ? "" : "s"}, ${scan.diagnostics.length} diagnostic${scan.diagnostics.length === 1 ? "" : "s"}.`,
|
|
165
|
+
"info",
|
|
166
|
+
);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (subcommand === "fleet" || subcommand === "inspect") {
|
|
171
|
+
const initialId = subArgs || undefined;
|
|
172
|
+
await openFleet(ctx, resolved.sessionId, resolved.state, options, initialId);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (subcommand === "stop") {
|
|
177
|
+
if (subArgs) {
|
|
178
|
+
const aborted = resolved.state.manager.abort(subArgs, resolved.sessionId);
|
|
179
|
+
options.refreshSession(resolved.sessionId);
|
|
180
|
+
if (aborted) {
|
|
181
|
+
ctx.ui.notify(`Subagent run "${subArgs}" stopped.`, "info");
|
|
182
|
+
} else {
|
|
183
|
+
ctx.ui.notify(`No active subagent run "${subArgs}" in this session.`, "warning");
|
|
184
|
+
}
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const stopped = resolved.state.manager.abortSession(resolved.sessionId);
|
|
188
|
+
options.refreshSession(resolved.sessionId);
|
|
189
|
+
if (stopped.length === 0) {
|
|
190
|
+
ctx.ui.notify("No active subagent runs to stop.", "info");
|
|
191
|
+
} else {
|
|
192
|
+
ctx.ui.notify(`Stopped ${stopped.length} subagent run${stopped.length === 1 ? "" : "s"}.`, "info");
|
|
193
|
+
}
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (subcommand === "wait") {
|
|
198
|
+
if (!subArgs) {
|
|
199
|
+
ctx.ui.notify("Usage: /subagents wait <run-id>", "warning");
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const existing = resolved.state.manager.get(subArgs, resolved.sessionId);
|
|
203
|
+
if (!existing) {
|
|
204
|
+
ctx.ui.notify(`No active subagent run "${subArgs}" in this session.`, "warning");
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const [settled] = await resolved.state.manager.wait([subArgs]);
|
|
208
|
+
const updated = settled ?? resolved.state.manager.get(subArgs, resolved.sessionId) ?? existing;
|
|
209
|
+
ctx.ui.notify(
|
|
210
|
+
`Subagent run "${subArgs}" settled: ${updated.status}${updated.error ? ` (${updated.error})` : ""}.`,
|
|
211
|
+
"info",
|
|
212
|
+
);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (subcommand !== "" && subcommand !== "list") {
|
|
217
|
+
ctx.ui.notify(
|
|
218
|
+
`Unknown subagents subcommand "${subcommand}". Usage: /subagents [list|reload|fleet|stop]`,
|
|
219
|
+
"warning",
|
|
220
|
+
);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const agents = resolved.state.registry.list();
|
|
225
|
+
const runs = resolved.state.manager.list(resolved.sessionId);
|
|
226
|
+
const diagnostics = resolved.state.registry.diagnostics();
|
|
227
|
+
ctx.ui.notify(
|
|
228
|
+
[
|
|
229
|
+
`Agents (${agents.length}):\n${formatAgentCatalog(agents, 100)}`,
|
|
230
|
+
`Runs:\n${formatRunList(runs)}`,
|
|
231
|
+
`Directories:\n${
|
|
232
|
+
resolved.state.registry
|
|
233
|
+
.directories()
|
|
234
|
+
.map((dir) => `- ${dir}`)
|
|
235
|
+
.join("\n") || "- none"
|
|
236
|
+
}`,
|
|
237
|
+
`Override configs:\n${
|
|
238
|
+
resolved.state.registry
|
|
239
|
+
.configFiles()
|
|
240
|
+
.map((file) => `- ${file}`)
|
|
241
|
+
.join("\n") || "- none"
|
|
242
|
+
}`,
|
|
243
|
+
`Diagnostics:\n${formatDiagnostics(diagnostics)}`,
|
|
244
|
+
].join("\n\n"),
|
|
245
|
+
"info",
|
|
246
|
+
);
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function registerSubagentsCommand(pi: PiExtensionAPI, options: SubagentsCommandOptions): void {
|
|
251
|
+
pi.registerCommand("subagents", {
|
|
252
|
+
description: "Manage subagents: /subagents [fleet|list|reload|stop] — Tab completes, including running ids",
|
|
253
|
+
getArgumentCompletions: (prefix) => completeArguments(prefix, subagentArguments(options.getActiveRuns())),
|
|
254
|
+
handler: createSubagentsCommandHandler(options),
|
|
255
|
+
});
|
|
256
|
+
}
|