pi-subagents 0.45.2 → 0.46.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +2 -0
  3. package/docs/agents.md +342 -0
  4. package/docs/configuration.md +320 -0
  5. package/docs/extension-api.md +308 -0
  6. package/docs/missions.md +117 -0
  7. package/docs/models.md +190 -0
  8. package/docs/observability.md +174 -0
  9. package/docs/tool-reference.md +343 -0
  10. package/docs/watchdog.md +176 -0
  11. package/docs/workflows.md +163 -0
  12. package/package.json +4 -2
  13. package/skills/pi-subagents/references/execution-controls.md +2 -2
  14. package/src/agents/agents.ts +17 -8
  15. package/src/agents/frontmatter.ts +7 -3
  16. package/src/agents/skills.ts +2 -9
  17. package/src/api/project-panes.ts +30 -0
  18. package/src/extension/config.ts +15 -1
  19. package/src/extension/index.ts +36 -16
  20. package/src/extension/schemas.ts +3 -2
  21. package/src/extension/subagent-guide.ts +39 -0
  22. package/src/extension/tool-description.ts +4 -4
  23. package/src/inspectors/herdr/project-panes.ts +457 -62
  24. package/src/missions/actions.ts +25 -2
  25. package/src/missions/lifecycle.ts +21 -2
  26. package/src/missions/store.ts +77 -1
  27. package/src/missions/types.ts +33 -0
  28. package/src/runs/background/async-execution.ts +7 -1
  29. package/src/runs/background/completion-replay.ts +267 -0
  30. package/src/runs/background/result-watcher.ts +12 -4
  31. package/src/runs/background/wait-completions.ts +39 -5
  32. package/src/runs/background/wait-subscriptions.ts +18 -3
  33. package/src/runs/foreground/execution.ts +4 -0
  34. package/src/runs/foreground/foreground-history.ts +137 -0
  35. package/src/runs/foreground/subagent-executor.ts +310 -44
  36. package/src/shared/fork-context.ts +13 -0
  37. package/src/shared/prompt-resources.ts +51 -0
  38. package/src/shared/types.ts +30 -1
  39. package/src/shared/utf8.ts +11 -0
  40. package/src/slash/prompt-workflows.ts +2 -15
  41. package/src/slash/slash-commands.ts +19 -1
  42. package/src/tui/fleet-status.ts +8 -2
  43. package/src/tui/fleet.ts +135 -25
  44. package/src/tui/render.ts +120 -7
  45. package/src/workflows/scripted-workflow.ts +167 -10
@@ -4,6 +4,7 @@ import { randomUUID } from "node:crypto";
4
4
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
5
5
  import { listAsyncRuns, type AsyncRunSummary } from "./async-status.ts";
6
6
  import { formatResumeFirstFailedRunDetail } from "./resume-guidance.ts";
7
+ import { readCompletionReplay } from "./completion-replay.ts";
7
8
  import { writeAtomicJson } from "../../shared/atomic-json.ts";
8
9
  import {
9
10
  DIRS,
@@ -14,6 +15,7 @@ import {
14
15
  SUBAGENT_FOREGROUND_COMPLETE_EVENT,
15
16
  SUBAGENT_RESULT_INTERCOM_EVENT,
16
17
  type SubagentState,
18
+ type WaitCompletion,
17
19
  type WaitSubscriptionRecord,
18
20
  } from "../../shared/types.ts";
19
21
 
@@ -104,7 +106,7 @@ export function createWaitSubscriptionManager(
104
106
  unresolvedRestoredForegroundTokens.delete(record.token);
105
107
  };
106
108
 
107
- const settle = (record: WaitSubscriptionRecord, outcome: string, detail: string) => {
109
+ const settle = (record: WaitSubscriptionRecord, outcome: string, detail: string, completion?: WaitCompletion) => {
108
110
  if (disposed || state.currentSessionId !== record.sessionId) return;
109
111
  try {
110
112
  remove(record);
@@ -117,7 +119,12 @@ export function createWaitSubscriptionManager(
117
119
  customType: "subagent-wait-subscription",
118
120
  content: `Wait subscription ${record.token} fired for run ${record.runId}: ${outcome}. ${detail}`,
119
121
  display: true,
120
- details: { token: record.token, runId: record.runId, outcome },
122
+ details: {
123
+ token: record.token,
124
+ runId: record.runId,
125
+ outcome,
126
+ ...(completion ? { completions: [completion] } : {}),
127
+ },
121
128
  }, { triggerTurn: true });
122
129
  } catch (error) {
123
130
  console.error(`Failed to deliver wait subscription '${record.token}' after clearing it:`, error);
@@ -167,7 +174,15 @@ export function createWaitSubscriptionManager(
167
174
  return;
168
175
  }
169
176
  if (run.state !== "queued" && run.state !== "running") {
170
- settle(record, run.state === "complete" ? "completed" : run.state, formatResumeFirstFailedRunDetail(run) ?? "Inspect the run status for its final output.");
177
+ let completion: WaitCompletion | undefined;
178
+ try {
179
+ completion = readCompletionReplay(resultsDir, record.runId, { sessionId: record.sessionId, now: now() })?.completion;
180
+ } catch (error) {
181
+ console.error(`Failed to read completion replay for wait subscription '${record.token}':`, error);
182
+ }
183
+ const detail = formatResumeFirstFailedRunDetail(run) ?? "Inspect the run status for its final output.";
184
+ const archiveDetail = completion?.archivePath ? ` Completion archive: ${completion.archivePath}.` : "";
185
+ settle(record, run.state === "complete" ? "completed" : run.state, `${detail}${archiveDetail}`, completion);
171
186
  }
172
187
  };
173
188
 
@@ -8,6 +8,7 @@ import * as path from "node:path";
8
8
  import type { Message } from "@earendil-works/pi-ai";
9
9
  import type { AgentConfig } from "../../agents/agents.ts";
10
10
  import { appendAgentRefinementOverlay } from "../../agents/agent-refinements.ts";
11
+ import { alignForkedSessionCwd } from "../../shared/fork-context.ts";
11
12
  import {
12
13
  ensureArtifactsDir,
13
14
  formatOutputArtifactContent,
@@ -1416,6 +1417,9 @@ async function runSyncCompletion(
1416
1417
  const acceptancePrompt = formatAcceptancePrompt(effectiveAcceptance, { reportOptional: isAgentContractV1(options.agentContract) });
1417
1418
  const taskWithAcceptance = acceptancePrompt ? `${task}\n${acceptancePrompt}` : task;
1418
1419
  const sessionEnabled = Boolean(options.sessionFile || options.sessionDir) || shareEnabled;
1420
+ if (options.context === "fork" && options.sessionFile && existsSync(options.sessionFile)) {
1421
+ alignForkedSessionCwd(options.sessionFile, options.cwd ?? runtimeCwd);
1422
+ }
1419
1423
  const skillNames = options.skills ?? agent.skills ?? [];
1420
1424
  const skillCwd = options.cwd ?? runtimeCwd;
1421
1425
  const { resolved: resolvedSkills, missing: missingSkills } = resolveSkillsWithFallback(
@@ -0,0 +1,137 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { ForegroundResumeChild, ForegroundResumeRun, SubagentState } from "../../shared/types.ts";
4
+ import { DIRS } from "../../shared/types.ts";
5
+ import { writeAtomicJson } from "../../shared/atomic-json.ts";
6
+ import { utf8Tail } from "../../shared/utf8.ts";
7
+
8
+ export const MAX_REMEMBERED_FOREGROUND_RUNS = 50;
9
+ const HISTORY_VERSION = 1;
10
+ const MAX_INLINE_OUTPUT_BYTES = 64 * 1024;
11
+
12
+ interface ForegroundHistoryIndex {
13
+ version: 1;
14
+ runs: ForegroundResumeRun[];
15
+ }
16
+
17
+ function historyPath(resultsDir: string): string {
18
+ return path.join(resultsDir, "foreground-history.json");
19
+ }
20
+
21
+ function boundedTail(value: string): string {
22
+ return utf8Tail(value, MAX_INLINE_OUTPUT_BYTES).text;
23
+ }
24
+
25
+ function compactChild(child: ForegroundResumeChild): ForegroundResumeChild {
26
+ const outputPath = child.artifactPaths?.outputPath ?? child.savedOutputPath;
27
+ return {
28
+ agent: child.agent,
29
+ index: child.index,
30
+ ...(child.context ? { context: child.context } : {}),
31
+ ...(child.sessionFile ? { sessionFile: child.sessionFile } : {}),
32
+ ...(child.model ? { model: child.model } : {}),
33
+ ...(child.thinking ? { thinking: child.thinking } : {}),
34
+ status: child.status,
35
+ ...(child.activityState ? { activityState: child.activityState } : {}),
36
+ ...(child.lastActivityAt !== undefined ? { lastActivityAt: child.lastActivityAt } : {}),
37
+ ...(child.currentTool ? { currentTool: child.currentTool } : {}),
38
+ ...(child.currentToolStartedAt !== undefined ? { currentToolStartedAt: child.currentToolStartedAt } : {}),
39
+ ...(child.currentPath ? { currentPath: child.currentPath } : {}),
40
+ ...(child.turnCount !== undefined ? { turnCount: child.turnCount } : {}),
41
+ ...(child.tokens !== undefined ? { tokens: child.tokens } : {}),
42
+ ...(child.toolCount !== undefined ? { toolCount: child.toolCount } : {}),
43
+ ...(child.exitCode !== undefined ? { exitCode: child.exitCode } : {}),
44
+ ...(child.error ? { error: child.error } : {}),
45
+ ...(!outputPath && child.finalOutput ? { finalOutput: boundedTail(child.finalOutput) } : {}),
46
+ ...(child.outputState ? { outputState: child.outputState } : {}),
47
+ ...(child.outputMode ? { outputMode: child.outputMode } : {}),
48
+ ...(child.savedOutputPath ? { savedOutputPath: child.savedOutputPath } : {}),
49
+ ...(child.outputSaveError ? { outputSaveError: child.outputSaveError } : {}),
50
+ ...(child.artifactPaths ? { artifactPaths: child.artifactPaths } : {}),
51
+ ...(child.transcriptPath ? { transcriptPath: child.transcriptPath } : {}),
52
+ ...(child.transcriptError ? { transcriptError: child.transcriptError } : {}),
53
+ ...(child.acceptance ? { acceptance: child.acceptance } : {}),
54
+ ...(child.launchContractDigest ? { launchContractDigest: child.launchContractDigest } : {}),
55
+ ...(child.capabilityCeiling ? { capabilityCeiling: child.capabilityCeiling } : {}),
56
+ ...(child.updatedAt !== undefined ? { updatedAt: child.updatedAt } : {}),
57
+ };
58
+ }
59
+
60
+ function isRestorableForegroundStatus(status: unknown): status is ForegroundResumeChild["status"] {
61
+ return status === "completed" || status === "failed" || status === "paused" || status === "stopped";
62
+ }
63
+
64
+ function compactRun(run: ForegroundResumeRun): ForegroundResumeRun | undefined {
65
+ if (!run.sessionId) return undefined;
66
+ if (run.children.length === 0 || !run.children.every((child) => isRestorableForegroundStatus(child.status))) return undefined;
67
+ return {
68
+ runId: run.runId,
69
+ mode: run.mode,
70
+ cwd: run.cwd,
71
+ sessionId: run.sessionId,
72
+ updatedAt: run.updatedAt,
73
+ children: run.children.map(compactChild),
74
+ };
75
+ }
76
+
77
+ function readIndex(resultsDir: string): ForegroundHistoryIndex {
78
+ const filePath = historyPath(resultsDir);
79
+ if (!fs.existsSync(filePath)) return { version: HISTORY_VERSION, runs: [] };
80
+ try {
81
+ const parsed = JSON.parse(fs.readFileSync(filePath, "utf-8")) as unknown;
82
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return { version: HISTORY_VERSION, runs: [] };
83
+ const record = parsed as Partial<ForegroundHistoryIndex>;
84
+ if (record.version !== HISTORY_VERSION || !Array.isArray(record.runs)) return { version: HISTORY_VERSION, runs: [] };
85
+ return { version: HISTORY_VERSION, runs: record.runs.filter(isRestorableRun) };
86
+ } catch {
87
+ return { version: HISTORY_VERSION, runs: [] };
88
+ }
89
+ }
90
+
91
+ function isRestorableRun(value: unknown): value is ForegroundResumeRun {
92
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
93
+ const run = value as Partial<ForegroundResumeRun>;
94
+ return typeof run.runId === "string" && Boolean(run.runId)
95
+ && (run.mode === "single" || run.mode === "parallel" || run.mode === "chain")
96
+ && typeof run.cwd === "string" && Boolean(run.cwd)
97
+ && typeof run.sessionId === "string" && Boolean(run.sessionId)
98
+ && typeof run.updatedAt === "number" && Number.isFinite(run.updatedAt)
99
+ && Array.isArray(run.children)
100
+ && run.children.length > 0
101
+ && run.children.every((child) => Boolean(child && typeof child === "object" && !Array.isArray(child)
102
+ && typeof (child as Partial<ForegroundResumeChild>).agent === "string"
103
+ && typeof (child as Partial<ForegroundResumeChild>).index === "number"
104
+ && isRestorableForegroundStatus((child as Partial<ForegroundResumeChild>).status)));
105
+ }
106
+
107
+ function sortAndBound(runs: ForegroundResumeRun[], limit: number): ForegroundResumeRun[] {
108
+ return [...runs].sort((left, right) => right.updatedAt - left.updatedAt).slice(0, limit);
109
+ }
110
+
111
+ export function persistForegroundRunHistory(state: SubagentState, options: { resultsDir?: string; limit?: number } = {}): void {
112
+ const resultsDir = options.resultsDir ?? DIRS.results;
113
+ const limit = options.limit ?? MAX_REMEMBERED_FOREGROUND_RUNS;
114
+ const existing = readIndex(resultsDir);
115
+ const merged = new Map(existing.runs.map((run) => [run.runId, run]));
116
+ for (const run of state.foregroundRuns?.values() ?? []) {
117
+ const compact = compactRun(run);
118
+ if (compact) merged.set(compact.runId, compact);
119
+ }
120
+ const runs = sortAndBound([...merged.values()], limit);
121
+ writeAtomicJson(historyPath(resultsDir), { version: HISTORY_VERSION, runs });
122
+ }
123
+
124
+ export function restoreForegroundRunHistory(state: SubagentState, options: { resultsDir?: string; sessionId?: string | null; limit?: number } = {}): number {
125
+ const sessionId = options.sessionId ?? state.currentSessionId;
126
+ if (!sessionId) return 0;
127
+ const index = readIndex(options.resultsDir ?? DIRS.results);
128
+ const runs = sortAndBound(index.runs.filter((run) => run.sessionId === sessionId), options.limit ?? MAX_REMEMBERED_FOREGROUND_RUNS);
129
+ state.foregroundRuns ??= new Map();
130
+ let restored = 0;
131
+ for (const run of runs) {
132
+ if (state.foregroundRuns.has(run.runId)) continue;
133
+ state.foregroundRuns.set(run.runId, run);
134
+ restored += 1;
135
+ }
136
+ return restored;
137
+ }