pi-subagents 0.29.0 → 0.31.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 +43 -0
- package/README.md +125 -19
- package/agents/context-builder.md +3 -3
- package/agents/planner.md +1 -1
- package/agents/researcher.md +1 -1
- package/agents/scout.md +1 -1
- package/package.json +7 -7
- package/skills/pi-subagents/SKILL.md +30 -0
- package/src/agents/agent-management.ts +189 -8
- package/src/agents/agent-serializer.ts +35 -12
- package/src/agents/agents.ts +243 -24
- package/src/agents/frontmatter.ts +66 -2
- package/src/agents/proactive-skills.ts +191 -0
- package/src/agents/skills.ts +117 -20
- package/src/extension/doctor.ts +20 -0
- package/src/extension/fanout-child.ts +2 -1
- package/src/extension/index.ts +50 -5
- package/src/extension/schemas.ts +40 -79
- package/src/intercom/intercom-bridge.ts +2 -3
- package/src/runs/background/async-execution.ts +180 -67
- package/src/runs/background/async-job-tracker.ts +56 -11
- package/src/runs/background/async-resume.ts +53 -5
- package/src/runs/background/async-status.ts +4 -1
- package/src/runs/background/chain-append.ts +282 -0
- package/src/runs/background/chain-root-attachment.ts +161 -0
- package/src/runs/background/result-watcher.ts +11 -2
- package/src/runs/background/run-status.ts +1 -0
- package/src/runs/background/stale-run-reconciler.ts +9 -4
- package/src/runs/background/subagent-runner.ts +158 -11
- package/src/runs/foreground/chain-execution.ts +26 -2
- package/src/runs/foreground/execution.ts +114 -8
- package/src/runs/foreground/subagent-executor.ts +611 -87
- package/src/runs/shared/acceptance.ts +285 -34
- package/src/runs/shared/chain-outputs.ts +23 -8
- package/src/runs/shared/completion-guard.ts +1 -1
- package/src/runs/shared/dynamic-fanout.ts +5 -3
- package/src/runs/shared/mcp-direct-tool-allowlist.ts +2 -2
- package/src/runs/shared/parallel-utils.ts +13 -1
- package/src/runs/shared/pi-args.ts +12 -3
- package/src/runs/shared/single-output.ts +15 -1
- package/src/runs/shared/subagent-control.ts +8 -11
- package/src/shared/settings.ts +1 -0
- package/src/shared/types.ts +17 -2
- package/src/shared/utils.ts +19 -1
- package/src/slash/prompt-template-bridge.ts +26 -3
- package/src/slash/slash-bridge.ts +3 -1
- package/src/slash/slash-commands.ts +34 -4
- package/src/tui/render.ts +265 -13
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { AgentConfig, ChainConfig, ChainStepConfig } from "./agents.ts";
|
|
2
|
+
import type { ProactiveSkillSubagentsConfig } from "../shared/types.ts";
|
|
3
|
+
|
|
4
|
+
const SUBAGENT_ORCHESTRATION_SKILL = "pi-subagents";
|
|
5
|
+
const DEFAULT_MIN_REFERENCES = 2;
|
|
6
|
+
const DEFAULT_MAX_RECOMMENDATIONS = 3;
|
|
7
|
+
const DEFAULT_PREFERRED_AGENT = "reviewer";
|
|
8
|
+
const FALLBACK_AGENT_ORDER = ["reviewer", "context-builder", "delegate"];
|
|
9
|
+
const MAX_RECOMMENDATION_CAP = 5;
|
|
10
|
+
|
|
11
|
+
export interface ResolvedProactiveSkillSubagentsConfig {
|
|
12
|
+
enabled: boolean;
|
|
13
|
+
minReferences: number;
|
|
14
|
+
maxRecommendations: number;
|
|
15
|
+
preferredAgent: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ProactiveSkillSubagentRecommendation {
|
|
19
|
+
skill: string;
|
|
20
|
+
agent: string;
|
|
21
|
+
references: number;
|
|
22
|
+
sources: string[];
|
|
23
|
+
description?: string;
|
|
24
|
+
reason: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AvailableSkill {
|
|
28
|
+
name: string;
|
|
29
|
+
description?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function positiveInteger(value: unknown): number | undefined {
|
|
33
|
+
if (typeof value !== "number") return undefined;
|
|
34
|
+
if (!Number.isInteger(value) || !Number.isFinite(value) || value < 1) return undefined;
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function resolveProactiveSkillSubagentsConfig(
|
|
39
|
+
config?: ProactiveSkillSubagentsConfig | false,
|
|
40
|
+
): ResolvedProactiveSkillSubagentsConfig {
|
|
41
|
+
if (config === false) {
|
|
42
|
+
return {
|
|
43
|
+
enabled: false,
|
|
44
|
+
minReferences: DEFAULT_MIN_REFERENCES,
|
|
45
|
+
maxRecommendations: DEFAULT_MAX_RECOMMENDATIONS,
|
|
46
|
+
preferredAgent: DEFAULT_PREFERRED_AGENT,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const maxRecommendations = positiveInteger(config?.maxRecommendations) ?? DEFAULT_MAX_RECOMMENDATIONS;
|
|
51
|
+
return {
|
|
52
|
+
enabled: config?.enabled ?? true,
|
|
53
|
+
minReferences: positiveInteger(config?.minReferences) ?? DEFAULT_MIN_REFERENCES,
|
|
54
|
+
maxRecommendations: Math.min(maxRecommendations, MAX_RECOMMENDATION_CAP),
|
|
55
|
+
preferredAgent: typeof config?.preferredAgent === "string" && config.preferredAgent.trim()
|
|
56
|
+
? config.preferredAgent.trim()
|
|
57
|
+
: DEFAULT_PREFERRED_AGENT,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function normalizeSkillNames(value: unknown): string[] {
|
|
62
|
+
if (value === false || value === true || value === undefined || value === null) return [];
|
|
63
|
+
if (Array.isArray(value)) {
|
|
64
|
+
return [...new Set(value.filter((entry): entry is string => typeof entry === "string").map((entry) => entry.trim()).filter(Boolean))];
|
|
65
|
+
}
|
|
66
|
+
if (typeof value === "string") {
|
|
67
|
+
return [...new Set(value.split(",").map((entry) => entry.trim()).filter(Boolean))];
|
|
68
|
+
}
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function collectStepSkills(step: ChainStepConfig, out: Set<string>): void {
|
|
73
|
+
for (const skill of normalizeSkillNames(step.skills ?? (step as { skill?: unknown }).skill)) {
|
|
74
|
+
out.add(skill);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const parallel = step.parallel;
|
|
78
|
+
if (!parallel) return;
|
|
79
|
+
if (Array.isArray(parallel)) {
|
|
80
|
+
for (const child of parallel) {
|
|
81
|
+
if (child && typeof child === "object" && !Array.isArray(child)) {
|
|
82
|
+
collectStepSkills(child as ChainStepConfig, out);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (typeof parallel === "object") {
|
|
88
|
+
collectStepSkills(parallel as ChainStepConfig, out);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function chooseRecommendationAgent(agents: AgentConfig[], preferredAgent: string): string | undefined {
|
|
93
|
+
const enabled = agents.filter((agent) => !agent.disabled);
|
|
94
|
+
if (enabled.some((agent) => agent.name === preferredAgent)) return preferredAgent;
|
|
95
|
+
for (const name of FALLBACK_AGENT_ORDER) {
|
|
96
|
+
if (enabled.some((agent) => agent.name === name)) return name;
|
|
97
|
+
}
|
|
98
|
+
return enabled[0]?.name;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function addSource(counts: Map<string, Set<string>>, skill: string, source: string): void {
|
|
102
|
+
if (skill === SUBAGENT_ORCHESTRATION_SKILL) return;
|
|
103
|
+
const sources = counts.get(skill) ?? new Set<string>();
|
|
104
|
+
sources.add(source);
|
|
105
|
+
counts.set(skill, sources);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function recommendProactiveSkillSubagents(input: {
|
|
109
|
+
agents: AgentConfig[];
|
|
110
|
+
chains?: ChainConfig[];
|
|
111
|
+
availableSkills?: AvailableSkill[];
|
|
112
|
+
config?: ProactiveSkillSubagentsConfig | false;
|
|
113
|
+
}): ProactiveSkillSubagentRecommendation[] {
|
|
114
|
+
const config = resolveProactiveSkillSubagentsConfig(input.config);
|
|
115
|
+
if (!config.enabled) return [];
|
|
116
|
+
|
|
117
|
+
const agent = chooseRecommendationAgent(input.agents, config.preferredAgent);
|
|
118
|
+
if (!agent) return [];
|
|
119
|
+
|
|
120
|
+
const availableByName = input.availableSkills
|
|
121
|
+
? new Map(input.availableSkills.map((skill) => [skill.name, skill]))
|
|
122
|
+
: undefined;
|
|
123
|
+
const counts = new Map<string, Set<string>>();
|
|
124
|
+
|
|
125
|
+
for (const candidate of input.agents) {
|
|
126
|
+
if (candidate.disabled) continue;
|
|
127
|
+
for (const skill of candidate.skills ?? []) {
|
|
128
|
+
addSource(counts, skill, `agent:${candidate.name}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
for (const chain of input.chains ?? []) {
|
|
133
|
+
const chainSkills = new Set<string>();
|
|
134
|
+
for (const step of chain.steps) {
|
|
135
|
+
collectStepSkills(step, chainSkills);
|
|
136
|
+
}
|
|
137
|
+
for (const skill of chainSkills) {
|
|
138
|
+
addSource(counts, skill, `chain:${chain.name}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return [...counts.entries()]
|
|
143
|
+
.filter(([skill, sources]) => sources.size >= config.minReferences && (!availableByName || availableByName.has(skill)))
|
|
144
|
+
.map(([skill, sources]) => ({
|
|
145
|
+
skill,
|
|
146
|
+
agent,
|
|
147
|
+
references: sources.size,
|
|
148
|
+
sources: [...sources].sort((a, b) => a.localeCompare(b)),
|
|
149
|
+
description: availableByName?.get(skill)?.description,
|
|
150
|
+
reason: `referenced by ${sources.size} configured agents/chains`,
|
|
151
|
+
}))
|
|
152
|
+
.sort((a, b) => b.references - a.references || a.skill.localeCompare(b.skill))
|
|
153
|
+
.slice(0, config.maxRecommendations);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function formatProactiveSkillSubagentRecommendations(
|
|
157
|
+
recommendations: ProactiveSkillSubagentRecommendation[],
|
|
158
|
+
): string[] {
|
|
159
|
+
if (recommendations.length === 0) return [];
|
|
160
|
+
return [
|
|
161
|
+
"Proactive skill subagent suggestions:",
|
|
162
|
+
...recommendations.map((recommendation) => {
|
|
163
|
+
const sampleSources = recommendation.sources.slice(0, 3).join(", ");
|
|
164
|
+
const extra = recommendation.sources.length > 3 ? `, +${recommendation.sources.length - 3} more` : "";
|
|
165
|
+
const description = recommendation.description ? ` - ${recommendation.description}` : "";
|
|
166
|
+
return `- ${recommendation.skill} via ${recommendation.agent} (${recommendation.reason}; ${sampleSources}${extra})${description}`;
|
|
167
|
+
}),
|
|
168
|
+
"Guardrails: use these for broad tasks where a skill-specialist pass is useful; keep fanout small, use fresh context unless private/session context is explicitly needed, and skip when the user asks for a direct answer.",
|
|
169
|
+
];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function buildProactiveSkillSubagentRecommendationLines(input: {
|
|
173
|
+
agents: AgentConfig[];
|
|
174
|
+
chains?: ChainConfig[];
|
|
175
|
+
config?: ProactiveSkillSubagentsConfig | false;
|
|
176
|
+
discoverAvailableSkills: () => AvailableSkill[];
|
|
177
|
+
}): string[] {
|
|
178
|
+
if (!resolveProactiveSkillSubagentsConfig(input.config).enabled) return [];
|
|
179
|
+
let availableSkills: AvailableSkill[];
|
|
180
|
+
try {
|
|
181
|
+
availableSkills = input.discoverAvailableSkills();
|
|
182
|
+
} catch {
|
|
183
|
+
availableSkills = [];
|
|
184
|
+
}
|
|
185
|
+
return formatProactiveSkillSubagentRecommendations(recommendProactiveSkillSubagents({
|
|
186
|
+
agents: input.agents,
|
|
187
|
+
chains: input.chains,
|
|
188
|
+
availableSkills,
|
|
189
|
+
config: input.config,
|
|
190
|
+
}));
|
|
191
|
+
}
|
package/src/agents/skills.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { execSync } from "node:child_process";
|
|
|
6
6
|
import * as fs from "node:fs";
|
|
7
7
|
import * as os from "node:os";
|
|
8
8
|
import * as path from "node:path";
|
|
9
|
-
import { getAgentDir } from "../shared/utils.ts";
|
|
9
|
+
import { getAgentDir, getProjectConfigDir } from "../shared/utils.ts";
|
|
10
10
|
|
|
11
11
|
export type SkillSource =
|
|
12
12
|
| "project"
|
|
@@ -23,6 +23,7 @@ interface ResolvedSkill {
|
|
|
23
23
|
name: string;
|
|
24
24
|
path: string;
|
|
25
25
|
content: string;
|
|
26
|
+
description?: string;
|
|
26
27
|
source: SkillSource;
|
|
27
28
|
}
|
|
28
29
|
|
|
@@ -50,7 +51,6 @@ const MAX_CACHE_SIZE = 50;
|
|
|
50
51
|
let loadSkillsCache: { cwd: string; agentDir: string; skills: CachedSkillEntry[]; timestamp: number } | null = null;
|
|
51
52
|
const LOAD_SKILLS_CACHE_TTL_MS = 5000;
|
|
52
53
|
|
|
53
|
-
const CONFIG_DIR = ".pi";
|
|
54
54
|
const SUBAGENT_ORCHESTRATION_SKILL = "pi-subagents";
|
|
55
55
|
|
|
56
56
|
const SOURCE_PRIORITY: Record<SkillSource, number> = {
|
|
@@ -134,8 +134,9 @@ function getGlobalNpmRoot(): string | null {
|
|
|
134
134
|
}
|
|
135
135
|
|
|
136
136
|
function collectInstalledPackageSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
137
|
+
const projectConfigDir = getProjectConfigDir(cwd);
|
|
137
138
|
const dirs: SkillSearchPath[] = [
|
|
138
|
-
{ path: path.join(
|
|
139
|
+
{ path: path.join(projectConfigDir, "npm", "node_modules"), source: "project-package" },
|
|
139
140
|
{ path: path.join(agentDir, "npm", "node_modules"), source: "user-package" },
|
|
140
141
|
];
|
|
141
142
|
|
|
@@ -186,8 +187,9 @@ function collectInstalledPackageSkillPaths(cwd: string, agentDir: string): Skill
|
|
|
186
187
|
|
|
187
188
|
function collectSettingsSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
188
189
|
const results: SkillSearchPath[] = [];
|
|
190
|
+
const projectConfigDir = getProjectConfigDir(cwd);
|
|
189
191
|
const settingsFiles = [
|
|
190
|
-
{ file: path.join(
|
|
192
|
+
{ file: path.join(projectConfigDir, "settings.json"), base: projectConfigDir, source: "project-settings" as const },
|
|
191
193
|
{ file: path.join(agentDir, "settings.json"), base: agentDir, source: "user-settings" as const },
|
|
192
194
|
];
|
|
193
195
|
|
|
@@ -286,8 +288,9 @@ function resolveSettingsPackageRoot(source: string, baseDir: string): string | u
|
|
|
286
288
|
}
|
|
287
289
|
|
|
288
290
|
function collectSettingsPackageSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
291
|
+
const projectConfigDir = getProjectConfigDir(cwd);
|
|
289
292
|
const settingsFiles = [
|
|
290
|
-
{ file: path.join(
|
|
293
|
+
{ file: path.join(projectConfigDir, "settings.json"), base: projectConfigDir, source: "project-package" as const },
|
|
291
294
|
{ file: path.join(agentDir, "settings.json"), base: agentDir, source: "user-package" as const },
|
|
292
295
|
];
|
|
293
296
|
const results: SkillSearchPath[] = [];
|
|
@@ -316,8 +319,9 @@ function collectSettingsPackageSkillPaths(cwd: string, agentDir: string): SkillS
|
|
|
316
319
|
}
|
|
317
320
|
|
|
318
321
|
function buildSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
322
|
+
const projectConfigDir = getProjectConfigDir(cwd);
|
|
319
323
|
const skillPaths: SkillSearchPath[] = [
|
|
320
|
-
{ path: path.join(
|
|
324
|
+
{ path: path.join(projectConfigDir, "skills"), source: "project" },
|
|
321
325
|
{ path: path.join(cwd, ".agents", "skills"), source: "project" },
|
|
322
326
|
{ path: path.join(agentDir, "skills"), source: "user" },
|
|
323
327
|
{ path: path.join(os.homedir(), ".agents", "skills"), source: "user" },
|
|
@@ -330,7 +334,8 @@ function buildSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
|
330
334
|
const deduped = new Map<string, SkillSearchPath>();
|
|
331
335
|
for (const entry of skillPaths) {
|
|
332
336
|
const resolvedPath = path.resolve(entry.path);
|
|
333
|
-
|
|
337
|
+
const existing = deduped.get(resolvedPath);
|
|
338
|
+
if (!existing || (SOURCE_PRIORITY[entry.source] ?? 0) > (SOURCE_PRIORITY[existing.source] ?? 0)) {
|
|
334
339
|
deduped.set(resolvedPath, { path: resolvedPath, source: entry.source });
|
|
335
340
|
}
|
|
336
341
|
}
|
|
@@ -340,9 +345,9 @@ function buildSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
|
340
345
|
function inferSkillSource(filePath: string, cwd: string, agentDir: string, sourceHint?: SkillSource): SkillSource {
|
|
341
346
|
if (sourceHint) return sourceHint;
|
|
342
347
|
|
|
343
|
-
const projectConfigRoot = path.resolve(cwd
|
|
344
|
-
const projectSkillsRoot = path.resolve(
|
|
345
|
-
const projectPackagesRoot = path.resolve(
|
|
348
|
+
const projectConfigRoot = path.resolve(getProjectConfigDir(cwd));
|
|
349
|
+
const projectSkillsRoot = path.resolve(projectConfigRoot, "skills");
|
|
350
|
+
const projectPackagesRoot = path.resolve(projectConfigRoot, "npm", "node_modules");
|
|
346
351
|
const projectAgentsRoot = path.resolve(cwd, ".agents");
|
|
347
352
|
const userSkillsRoot = path.resolve(agentDir, "skills");
|
|
348
353
|
const userPackagesRoot = path.resolve(agentDir, "npm", "node_modules");
|
|
@@ -393,23 +398,86 @@ function maybeReadSkillDescription(filePath: string): string | undefined {
|
|
|
393
398
|
|
|
394
399
|
function collectFilesystemSkills(cwd: string, agentDir: string, skillPaths: SkillSearchPath[]): CachedSkillEntry[] {
|
|
395
400
|
const entries: CachedSkillEntry[] = [];
|
|
396
|
-
const seen = new
|
|
401
|
+
const seen = new Map<string, number>();
|
|
402
|
+
const visitedDirectories = new Map<string, number>();
|
|
397
403
|
let order = 0;
|
|
398
404
|
|
|
399
405
|
const pushEntry = (name: string, filePath: string, sourceHint?: SkillSource) => {
|
|
400
406
|
const resolvedFile = path.resolve(filePath);
|
|
401
|
-
if (seen.has(resolvedFile)) return;
|
|
402
407
|
if (!fs.existsSync(resolvedFile)) return;
|
|
403
|
-
|
|
408
|
+
const source = inferSkillSource(resolvedFile, cwd, agentDir, sourceHint);
|
|
409
|
+
const existingIndex = seen.get(resolvedFile);
|
|
410
|
+
if (existingIndex !== undefined) {
|
|
411
|
+
const existing = entries[existingIndex];
|
|
412
|
+
if (existing && (SOURCE_PRIORITY[source] ?? 0) > (SOURCE_PRIORITY[existing.source] ?? 0)) {
|
|
413
|
+
entries[existingIndex] = {
|
|
414
|
+
...existing,
|
|
415
|
+
name,
|
|
416
|
+
source,
|
|
417
|
+
description: maybeReadSkillDescription(resolvedFile),
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
seen.set(resolvedFile, entries.length);
|
|
404
423
|
entries.push({
|
|
405
424
|
name,
|
|
406
425
|
filePath: resolvedFile,
|
|
407
|
-
source
|
|
426
|
+
source,
|
|
408
427
|
description: maybeReadSkillDescription(resolvedFile),
|
|
409
428
|
order: order++,
|
|
410
429
|
});
|
|
411
430
|
};
|
|
412
431
|
|
|
432
|
+
const shouldSkipDirectory = (name: string) => name.startsWith(".") || name === "node_modules";
|
|
433
|
+
|
|
434
|
+
const markDirectoryVisited = (dirPath: string, sourceHint?: SkillSource): boolean => {
|
|
435
|
+
let resolvedDir: string;
|
|
436
|
+
try {
|
|
437
|
+
resolvedDir = fs.realpathSync(dirPath);
|
|
438
|
+
} catch {
|
|
439
|
+
resolvedDir = path.resolve(dirPath);
|
|
440
|
+
}
|
|
441
|
+
const priority = sourceHint ? SOURCE_PRIORITY[sourceHint] ?? 0 : SOURCE_PRIORITY.unknown;
|
|
442
|
+
const previousPriority = visitedDirectories.get(resolvedDir);
|
|
443
|
+
if (previousPriority !== undefined && previousPriority >= priority) return false;
|
|
444
|
+
visitedDirectories.set(resolvedDir, priority);
|
|
445
|
+
return true;
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
const walkSkillDirectories = (dirPath: string, sourceHint?: SkillSource) => {
|
|
449
|
+
if (!markDirectoryVisited(dirPath, sourceHint)) return;
|
|
450
|
+
|
|
451
|
+
const skillFile = path.join(dirPath, "SKILL.md");
|
|
452
|
+
if (fs.existsSync(skillFile)) {
|
|
453
|
+
pushEntry(path.basename(dirPath), skillFile, sourceHint);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
let entriesInDir: fs.Dirent[];
|
|
458
|
+
try {
|
|
459
|
+
entriesInDir = fs.readdirSync(dirPath, { withFileTypes: true });
|
|
460
|
+
} catch {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
for (const entry of entriesInDir) {
|
|
465
|
+
if (shouldSkipDirectory(entry.name)) continue;
|
|
466
|
+
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
|
|
467
|
+
|
|
468
|
+
const entryPath = path.join(dirPath, entry.name);
|
|
469
|
+
let stat: fs.Stats;
|
|
470
|
+
try {
|
|
471
|
+
stat = fs.statSync(entryPath);
|
|
472
|
+
} catch {
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
if (stat.isDirectory()) {
|
|
476
|
+
walkSkillDirectories(entryPath, sourceHint);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
|
|
413
481
|
for (const skillPath of skillPaths) {
|
|
414
482
|
if (!fs.existsSync(skillPath.path)) continue;
|
|
415
483
|
|
|
@@ -435,8 +503,11 @@ function collectFilesystemSkills(cwd: string, agentDir: string, skillPaths: Skil
|
|
|
435
503
|
const rootSkillFile = path.join(skillPath.path, "SKILL.md");
|
|
436
504
|
if (fs.existsSync(rootSkillFile)) {
|
|
437
505
|
pushEntry(path.basename(skillPath.path), rootSkillFile, skillPath.source);
|
|
506
|
+
continue;
|
|
438
507
|
}
|
|
439
508
|
|
|
509
|
+
markDirectoryVisited(skillPath.path, skillPath.source);
|
|
510
|
+
|
|
440
511
|
let childEntries: fs.Dirent[];
|
|
441
512
|
try {
|
|
442
513
|
childEntries = fs.readdirSync(skillPath.path, { withFileTypes: true });
|
|
@@ -448,10 +519,14 @@ function collectFilesystemSkills(cwd: string, agentDir: string, skillPaths: Skil
|
|
|
448
519
|
if (child.name.startsWith(".")) continue;
|
|
449
520
|
const childPath = path.join(skillPath.path, child.name);
|
|
450
521
|
if (child.isDirectory() || child.isSymbolicLink()) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
522
|
+
if (shouldSkipDirectory(child.name)) continue;
|
|
523
|
+
let childStat: fs.Stats;
|
|
524
|
+
try {
|
|
525
|
+
childStat = fs.statSync(childPath);
|
|
526
|
+
} catch {
|
|
527
|
+
continue;
|
|
454
528
|
}
|
|
529
|
+
if (childStat.isDirectory()) walkSkillDirectories(childPath, skillPath.source);
|
|
455
530
|
continue;
|
|
456
531
|
}
|
|
457
532
|
if (child.isFile() && child.name.toLowerCase().endsWith(".md")) {
|
|
@@ -508,10 +583,12 @@ function readSkill(
|
|
|
508
583
|
|
|
509
584
|
const raw = fs.readFileSync(skillPath, "utf-8");
|
|
510
585
|
const content = stripSkillFrontmatter(raw);
|
|
586
|
+
const description = maybeReadSkillDescription(skillPath);
|
|
511
587
|
const skill: ResolvedSkill = {
|
|
512
588
|
name: skillName,
|
|
513
589
|
path: skillPath,
|
|
514
590
|
content,
|
|
591
|
+
description,
|
|
515
592
|
source,
|
|
516
593
|
};
|
|
517
594
|
|
|
@@ -579,9 +656,29 @@ export function resolveSkillsWithFallback(
|
|
|
579
656
|
export function buildSkillInjection(skills: ResolvedSkill[]): string {
|
|
580
657
|
if (skills.length === 0) return "";
|
|
581
658
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
.
|
|
659
|
+
const lines = [
|
|
660
|
+
"The following configured skills are available to this subagent.",
|
|
661
|
+
"Use the read tool to load a skill's file when the task matches its description.",
|
|
662
|
+
"When a skill file references a relative path, resolve it against the skill directory (parent of SKILL.md / dirname of the path) and use that absolute path in tool commands.",
|
|
663
|
+
"",
|
|
664
|
+
"<available_skills>",
|
|
665
|
+
];
|
|
666
|
+
for (const skill of skills) {
|
|
667
|
+
lines.push(" <skill>");
|
|
668
|
+
lines.push(` <name>${escapeXmlText(skill.name)}</name>`);
|
|
669
|
+
lines.push(` <description>${escapeXmlText(skill.description ?? "")}</description>`);
|
|
670
|
+
lines.push(` <location>${escapeXmlText(skill.path)}</location>`);
|
|
671
|
+
lines.push(" </skill>");
|
|
672
|
+
}
|
|
673
|
+
lines.push("</available_skills>");
|
|
674
|
+
return lines.join("\n");
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function escapeXmlText(value: string): string {
|
|
678
|
+
return value
|
|
679
|
+
.replace(/&/g, "&")
|
|
680
|
+
.replace(/</g, "<")
|
|
681
|
+
.replace(/>/g, ">");
|
|
585
682
|
}
|
|
586
683
|
|
|
587
684
|
export function normalizeSkillInput(
|
package/src/extension/doctor.ts
CHANGED
|
@@ -168,6 +168,23 @@ function formatIntercomDiagnostic(diagnostic: IntercomBridgeDiagnostic, context:
|
|
|
168
168
|
return lines;
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
+
function formatPermissionSystemSection(): string[] {
|
|
172
|
+
const lines: string[] = [];
|
|
173
|
+
const parentSession = process.env["PI_SUBAGENT_PARENT_SESSION"] ?? "";
|
|
174
|
+
const trimmed = parentSession.trim();
|
|
175
|
+
if (trimmed) {
|
|
176
|
+
lines.push(`- parent session: set (${trimmed})`);
|
|
177
|
+
} else {
|
|
178
|
+
lines.push("- parent session: not set — ask forwarding from subprocess children will not reach a parent UI");
|
|
179
|
+
}
|
|
180
|
+
const isChild = process.env["PI_SUBAGENT_CHILD"] === "1";
|
|
181
|
+
lines.push(`- subagent process: ${isChild ? "yes (PI_SUBAGENT_CHILD=1)" : "no"}`);
|
|
182
|
+
// Whether pi-permission-system is installed and where it stores config is
|
|
183
|
+
// outside pi-subagents' control, so we only report the forwarding signal we
|
|
184
|
+
// own. Run `pi list` to confirm the permission extension is installed.
|
|
185
|
+
return lines;
|
|
186
|
+
}
|
|
187
|
+
|
|
171
188
|
export function buildDoctorReport(input: DoctorReportInput): string {
|
|
172
189
|
const paths = input.paths ?? DEFAULT_PATHS;
|
|
173
190
|
const deps = { ...DEFAULT_DEPS, ...input.deps };
|
|
@@ -188,6 +205,9 @@ export function buildDoctorReport(input: DoctorReportInput): string {
|
|
|
188
205
|
"Discovery",
|
|
189
206
|
...formatDiscovery(input, deps),
|
|
190
207
|
"",
|
|
208
|
+
"Permission system",
|
|
209
|
+
...formatPermissionSystemSection(),
|
|
210
|
+
"",
|
|
191
211
|
"Intercom bridge",
|
|
192
212
|
...lineFromCheck("intercom bridge", () => formatIntercomDiagnostic(deps.diagnoseIntercomBridge({
|
|
193
213
|
config: input.config.intercomBridge,
|
|
@@ -30,6 +30,7 @@ function createChildSafeState(): SubagentState {
|
|
|
30
30
|
return {
|
|
31
31
|
baseCwd: "",
|
|
32
32
|
currentSessionId: null,
|
|
33
|
+
subagentInProgress: false,
|
|
33
34
|
asyncJobs: new Map(),
|
|
34
35
|
foregroundRuns: new Map(),
|
|
35
36
|
foregroundControls: new Map(),
|
|
@@ -156,7 +157,7 @@ export default function registerFanoutChildSubagentExtension(pi: ExtensionAPI):
|
|
|
156
157
|
label: "Subagent",
|
|
157
158
|
description: [
|
|
158
159
|
"Delegate to subagents from child-safe fanout mode.",
|
|
159
|
-
"Allowed management/control actions: list, get, status, interrupt, resume, doctor.",
|
|
160
|
+
"Allowed management/control actions: list, get, status, interrupt, resume, append-step, doctor.",
|
|
160
161
|
"Agent config mutation actions create, update, and delete are blocked in this mode.",
|
|
161
162
|
].join("\n"),
|
|
162
163
|
parameters: SubagentParams,
|
package/src/extension/index.ts
CHANGED
|
@@ -33,7 +33,7 @@ import { registerSlashSubagentBridge } from "../slash/slash-bridge.ts";
|
|
|
33
33
|
import { clearSlashSnapshots, getSlashRenderableSnapshot, resolveSlashMessageDetails, restoreSlashFinalSnapshots, type SlashMessageDetails } from "../slash/slash-live-state.ts";
|
|
34
34
|
import { inspectSubagentStatus } from "../runs/background/run-status.ts";
|
|
35
35
|
import registerSubagentNotify, { type SubagentNotifyDetails } from "../runs/background/notify.ts";
|
|
36
|
-
import { SUBAGENT_CHILD_ENV } from "../runs/shared/pi-args.ts";
|
|
36
|
+
import { SUBAGENT_CHILD_ENV, SUBAGENT_PARENT_SESSION_ENV } from "../runs/shared/pi-args.ts";
|
|
37
37
|
import { formatDuration, shortenPath } from "../shared/formatters.ts";
|
|
38
38
|
import { loadConfig } from "./config.ts";
|
|
39
39
|
import {
|
|
@@ -106,6 +106,30 @@ function isSlashResultRunning(result: { details?: Details }): boolean {
|
|
|
106
106
|
|| false;
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
// Drives the inline running-indicator braille animation for foreground subagent
|
|
110
|
+
// results. Foreground runs receive progress only on child events, so the glyph
|
|
111
|
+
// (derived from progress fields) would freeze between events. While a result is
|
|
112
|
+
// running we tick a frame counter + invalidate() every 80ms so renderSubagentResult
|
|
113
|
+
// can blend the frame into runningGlyph and produce a smooth spinner.
|
|
114
|
+
function subagentResultIsRunning(result: { details?: Details }): boolean {
|
|
115
|
+
return result.details?.progress?.some((entry) => entry.status === "running")
|
|
116
|
+
|| result.details?.results.some((entry) => entry.progress?.status === "running")
|
|
117
|
+
|| false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function ensureSubagentResultAnimation(context: { state: Record<string, unknown>; invalidate?: () => void }): void {
|
|
121
|
+
const state = context.state as { subagentResultAnimationTimer?: ReturnType<typeof setInterval>; frame?: number };
|
|
122
|
+
if (state.subagentResultAnimationTimer) return;
|
|
123
|
+
if (typeof context.invalidate !== "function") return;
|
|
124
|
+
if (state.frame === undefined) state.frame = 0;
|
|
125
|
+
state.subagentResultAnimationTimer = setInterval(() => {
|
|
126
|
+
state.frame = ((state.frame ?? 0) + 1) % 10;
|
|
127
|
+
try {
|
|
128
|
+
context.invalidate();
|
|
129
|
+
} catch {}
|
|
130
|
+
}, 80);
|
|
131
|
+
}
|
|
132
|
+
|
|
109
133
|
function isSlashResultError(result: { details?: Details }): boolean {
|
|
110
134
|
return result.details?.results.some((entry) => entry.exitCode !== 0 && entry.progress?.status !== "running") || false;
|
|
111
135
|
}
|
|
@@ -233,6 +257,7 @@ export default function registerSubagentExtension(pi: ExtensionAPI): void {
|
|
|
233
257
|
const state: SubagentState = {
|
|
234
258
|
baseCwd: "",
|
|
235
259
|
currentSessionId: null,
|
|
260
|
+
subagentInProgress: false,
|
|
236
261
|
asyncJobs: new Map(),
|
|
237
262
|
foregroundRuns: new Map(),
|
|
238
263
|
foregroundControls: new Map(),
|
|
@@ -392,7 +417,8 @@ EXECUTION (use exactly ONE mode):
|
|
|
392
417
|
• SINGLE: { agent, task? } - one task; omit task for self-contained agents
|
|
393
418
|
• CHAIN: { chain: [{agent:"agent-a"}, {parallel:[{agent:"agent-b",count:3}]}] } - sequential pipeline with optional parallel fan-out
|
|
394
419
|
• PARALLEL: { tasks: [{agent,task,count?,output?,reads?,progress?}, ...], concurrency?: number, worktree?: true } - concurrent execution (worktree: isolate each task in a git worktree)
|
|
395
|
-
• Optional context: { context: "fresh" | "fork" } (
|
|
420
|
+
• Optional context: { context: "fresh" | "fork" } (explicit value overrides every child; when omitted, each requested agent uses its own defaultContext, otherwise "fresh"; inspect agent defaults via { action: "list" })
|
|
421
|
+
• If { action: "list" } shows proactive skill subagent suggestions, consider a small fresh-context fanout for broad tasks where one of those skills would materially help
|
|
396
422
|
|
|
397
423
|
CHAIN TEMPLATE VARIABLES (use in task strings):
|
|
398
424
|
• {task} - The original task/request from the user
|
|
@@ -404,6 +430,7 @@ Example: { chain: [{agent:"agent-a", task:"Analyze {task}"}, {agent:"agent-b", t
|
|
|
404
430
|
MANAGEMENT (use action field, omit agent/task/chain/tasks):
|
|
405
431
|
• { action: "list" } - discover executable agents/chains
|
|
406
432
|
• { action: "get", agent: "name" } - full detail; packaged agents use dotted runtime names like "package.agent"
|
|
433
|
+
• { action: "models", agent?: "name" } - show the runtime-loaded builtin subagent model mapping, optionally filtered to one builtin
|
|
407
434
|
• { action: "create", config: { name: "custom-agent", package: "code-analysis", systemPrompt, systemPromptMode, inheritProjectContext, inheritSkills, defaultContext, ... } }
|
|
408
435
|
• { action: "update", agent: "code-analysis.custom-agent", config: { package: "analysis", ... } } - merge
|
|
409
436
|
• { action: "delete", agent: "code-analysis.custom-agent" }
|
|
@@ -412,7 +439,8 @@ MANAGEMENT (use action field, omit agent/task/chain/tasks):
|
|
|
412
439
|
CONTROL:
|
|
413
440
|
• { action: "status", id: "..." } - inspect an async/background run by id or prefix
|
|
414
441
|
• { action: "interrupt", id?: "..." } - soft-interrupt the current child turn and leave the run paused
|
|
415
|
-
• { action: "resume", id: "...", message: "...", index?: 0 } - follow up with a live async child or revive a completed async/foreground child from its session
|
|
442
|
+
• { action: "resume", id: "...", message: "...", index?: 0 } - interrupt then follow up with a live async child, or revive a completed async/foreground child from its session
|
|
443
|
+
• { action: "append-step", id: "...", chain: [{agent:"agent-c", task:"Use {previous}"}] } - append one step to the tail of a running async chain
|
|
416
444
|
|
|
417
445
|
DIAGNOSTICS:
|
|
418
446
|
• { action: "doctor" } - read-only report for runtime paths, discovery, sessions, and intercom`,
|
|
@@ -453,8 +481,13 @@ DIAGNOSTICS:
|
|
|
453
481
|
},
|
|
454
482
|
|
|
455
483
|
renderResult(result, options, theme, context) {
|
|
456
|
-
|
|
457
|
-
|
|
484
|
+
if (subagentResultIsRunning(result)) {
|
|
485
|
+
ensureSubagentResultAnimation(context);
|
|
486
|
+
} else {
|
|
487
|
+
clearLegacyResultAnimationTimer(context);
|
|
488
|
+
}
|
|
489
|
+
const frame = (context.state as { frame?: number } | undefined)?.frame ?? 0;
|
|
490
|
+
return renderSubagentResult(result, options, theme, frame);
|
|
458
491
|
},
|
|
459
492
|
|
|
460
493
|
};
|
|
@@ -520,6 +553,17 @@ DIAGNOSTICS:
|
|
|
520
553
|
const resetSessionState = (ctx: ExtensionContext) => {
|
|
521
554
|
state.baseCwd = ctx.cwd;
|
|
522
555
|
state.currentSessionId = resolveCurrentSessionId(ctx.sessionManager);
|
|
556
|
+
// Set PI_SUBAGENT_PARENT_SESSION for permission-system forwarding.
|
|
557
|
+
// Only set in the root session (the interactive UI session), not in
|
|
558
|
+
// child subagent processes — children inherit the parent's value
|
|
559
|
+
// through the process environment at spawn time and must not overwrite
|
|
560
|
+
// it with their own session identity.
|
|
561
|
+
if (!process.env[SUBAGENT_CHILD_ENV]) {
|
|
562
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
563
|
+
if (sessionId) {
|
|
564
|
+
process.env[SUBAGENT_PARENT_SESSION_ENV] = sessionId;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
523
567
|
state.lastUiContext = ctx;
|
|
524
568
|
cleanupSessionArtifacts(ctx);
|
|
525
569
|
clearPendingForegroundControlNotices(state);
|
|
@@ -533,6 +577,7 @@ DIAGNOSTICS:
|
|
|
533
577
|
});
|
|
534
578
|
|
|
535
579
|
pi.on("session_shutdown", () => {
|
|
580
|
+
delete process.env[SUBAGENT_PARENT_SESSION_ENV];
|
|
536
581
|
for (const unsubscribe of eventUnsubscribes) {
|
|
537
582
|
try {
|
|
538
583
|
unsubscribe();
|