pi-crew 0.1.30 → 0.1.32

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 (159) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +80 -27
  3. package/agents/analyst.md +11 -11
  4. package/agents/critic.md +11 -11
  5. package/agents/executor.md +11 -11
  6. package/agents/explorer.md +11 -11
  7. package/agents/planner.md +11 -11
  8. package/agents/reviewer.md +11 -11
  9. package/agents/security-reviewer.md +11 -11
  10. package/agents/test-engineer.md +11 -11
  11. package/agents/verifier.md +11 -11
  12. package/agents/writer.md +11 -11
  13. package/docs/architecture.md +173 -164
  14. package/docs/refactor-tasks-phase3.md +394 -394
  15. package/docs/refactor-tasks-phase4.md +564 -564
  16. package/docs/refactor-tasks-phase5.md +402 -402
  17. package/docs/refactor-tasks-phase6.md +662 -662
  18. package/docs/research-extension-examples.md +297 -0
  19. package/docs/research-extension-system.md +324 -0
  20. package/docs/research-optimization-plan.md +548 -0
  21. package/docs/research-pi-coding-agent.md +357 -0
  22. package/docs/resource-formats.md +4 -3
  23. package/docs/runtime-flow.md +148 -148
  24. package/docs/source-runtime-refactor-map.md +83 -83
  25. package/docs/usage.md +3 -3
  26. package/index.ts +6 -6
  27. package/package.json +1 -1
  28. package/schema.json +53 -1
  29. package/skills/git-master/SKILL.md +24 -19
  30. package/skills/read-only-explorer/SKILL.md +26 -21
  31. package/skills/safe-bash/SKILL.md +21 -16
  32. package/skills/task-packet/SKILL.md +28 -23
  33. package/skills/verify-evidence/SKILL.md +27 -22
  34. package/src/agents/agent-serializer.ts +34 -34
  35. package/src/agents/discover-agents.ts +102 -102
  36. package/src/config/config.ts +14 -1
  37. package/src/config/defaults.ts +3 -2
  38. package/src/extension/cross-extension-rpc.ts +82 -82
  39. package/src/extension/import-index.ts +4 -3
  40. package/src/extension/management.ts +2 -2
  41. package/src/extension/project-init.ts +9 -7
  42. package/src/extension/register.ts +63 -0
  43. package/src/extension/registration/artifact-cleanup.ts +15 -14
  44. package/src/extension/registration/commands.ts +208 -208
  45. package/src/extension/registration/compaction-guard.ts +125 -0
  46. package/src/extension/registration/subagent-tools.ts +27 -8
  47. package/src/extension/registration/team-tool.ts +61 -44
  48. package/src/extension/result-watcher.ts +98 -98
  49. package/src/extension/run-import.ts +4 -4
  50. package/src/extension/run-index.ts +14 -14
  51. package/src/extension/run-maintenance.ts +24 -24
  52. package/src/extension/team-tool/api.ts +3 -0
  53. package/src/extension/team-tool/cancel.ts +31 -31
  54. package/src/extension/team-tool/doctor.ts +179 -178
  55. package/src/extension/team-tool/inspect.ts +41 -41
  56. package/src/extension/team-tool/lifecycle-actions.ts +79 -79
  57. package/src/extension/team-tool/plan.ts +19 -19
  58. package/src/extension/team-tool/status.ts +73 -73
  59. package/src/prompt/prompt-runtime.ts +68 -68
  60. package/src/runtime/agent-control.ts +64 -64
  61. package/src/runtime/agent-memory.ts +72 -72
  62. package/src/runtime/agent-observability.ts +113 -113
  63. package/src/runtime/async-marker.ts +26 -26
  64. package/src/runtime/background-runner.ts +53 -53
  65. package/src/runtime/crew-agent-runtime.ts +58 -58
  66. package/src/runtime/direct-run.ts +35 -35
  67. package/src/runtime/foreground-control.ts +82 -82
  68. package/src/runtime/green-contract.ts +46 -46
  69. package/src/runtime/group-join.ts +88 -88
  70. package/src/runtime/live-agent-control.ts +78 -78
  71. package/src/runtime/live-agent-manager.ts +85 -85
  72. package/src/runtime/live-control-realtime.ts +36 -36
  73. package/src/runtime/live-session-runtime.ts +299 -299
  74. package/src/runtime/manifest-cache.ts +212 -214
  75. package/src/runtime/model-fallback.ts +261 -261
  76. package/src/runtime/parallel-research.ts +44 -44
  77. package/src/runtime/parallel-utils.ts +99 -99
  78. package/src/runtime/pi-json-output.ts +111 -111
  79. package/src/runtime/pi-spawn.ts +96 -96
  80. package/src/runtime/policy-engine.ts +78 -78
  81. package/src/runtime/post-exit-stdio-guard.ts +86 -86
  82. package/src/runtime/process-status.ts +56 -56
  83. package/src/runtime/progress-event-coalescer.ts +43 -43
  84. package/src/runtime/recovery-recipes.ts +74 -74
  85. package/src/runtime/role-permission.ts +39 -39
  86. package/src/runtime/session-usage.ts +79 -79
  87. package/src/runtime/sidechain-output.ts +28 -28
  88. package/src/runtime/subagent-manager.ts +29 -2
  89. package/src/runtime/task-display.ts +38 -38
  90. package/src/runtime/task-output-context.ts +106 -106
  91. package/src/runtime/task-packet.ts +84 -84
  92. package/src/runtime/task-runner/live-executor.ts +98 -98
  93. package/src/runtime/task-runner/progress.ts +111 -111
  94. package/src/runtime/task-runner/prompt-builder.ts +72 -72
  95. package/src/runtime/task-runner/result-utils.ts +14 -14
  96. package/src/runtime/task-runner/state-helpers.ts +22 -22
  97. package/src/runtime/worker-heartbeat.ts +21 -21
  98. package/src/runtime/worker-startup.ts +57 -57
  99. package/src/schema/config-schema.ts +12 -0
  100. package/src/schema/team-tool-schema.ts +100 -100
  101. package/src/state/artifact-store.ts +108 -108
  102. package/src/state/contracts.ts +105 -105
  103. package/src/state/jsonl-writer.ts +77 -77
  104. package/src/state/state-store.ts +8 -9
  105. package/src/state/task-claims.ts +42 -42
  106. package/src/state/types.ts +180 -180
  107. package/src/state/usage.ts +29 -29
  108. package/src/subagents/async-entry.ts +1 -1
  109. package/src/subagents/index.ts +3 -3
  110. package/src/subagents/live/control.ts +1 -1
  111. package/src/subagents/live/manager.ts +1 -1
  112. package/src/subagents/live/realtime.ts +1 -1
  113. package/src/subagents/live/session-runtime.ts +1 -1
  114. package/src/subagents/manager.ts +1 -1
  115. package/src/subagents/spawn.ts +1 -1
  116. package/src/teams/discover-teams.ts +2 -2
  117. package/src/teams/team-serializer.ts +36 -36
  118. package/src/types/diff.d.ts +18 -0
  119. package/src/ui/crew-footer.ts +101 -101
  120. package/src/ui/crew-select-list.ts +111 -111
  121. package/src/ui/crew-widget.ts +285 -285
  122. package/src/ui/dynamic-border.ts +25 -25
  123. package/src/ui/layout-primitives.ts +106 -106
  124. package/src/ui/loaders.ts +158 -158
  125. package/src/ui/mascot.ts +441 -441
  126. package/src/ui/powerbar-publisher.ts +94 -94
  127. package/src/ui/render-diff.ts +119 -119
  128. package/src/ui/run-dashboard.ts +372 -372
  129. package/src/ui/status-colors.ts +54 -54
  130. package/src/ui/syntax-highlight.ts +116 -116
  131. package/src/ui/transcript-viewer.ts +302 -302
  132. package/src/utils/completion-dedupe.ts +63 -63
  133. package/src/utils/file-coalescer.ts +84 -84
  134. package/src/utils/frontmatter.ts +36 -36
  135. package/src/utils/fs-watch.ts +31 -31
  136. package/src/utils/git.ts +262 -262
  137. package/src/utils/ids.ts +12 -12
  138. package/src/utils/names.ts +26 -26
  139. package/src/utils/paths.ts +34 -7
  140. package/src/utils/sleep.ts +32 -32
  141. package/src/utils/timings.ts +31 -31
  142. package/src/utils/visual.ts +159 -159
  143. package/src/workflows/discover-workflows.ts +2 -2
  144. package/src/workflows/validate-workflow.ts +40 -40
  145. package/src/worktree/branch-freshness.ts +45 -45
  146. package/src/worktree/cleanup.ts +71 -69
  147. package/src/worktree/worktree-manager.ts +3 -1
  148. package/teams/default.team.md +12 -12
  149. package/teams/fast-fix.team.md +11 -11
  150. package/teams/implementation.team.md +18 -18
  151. package/teams/parallel-research.team.md +14 -14
  152. package/teams/research.team.md +11 -11
  153. package/teams/review.team.md +12 -12
  154. package/workflows/default.workflow.md +29 -29
  155. package/workflows/fast-fix.workflow.md +22 -22
  156. package/workflows/implementation.workflow.md +38 -38
  157. package/workflows/parallel-research.workflow.md +46 -46
  158. package/workflows/research.workflow.md +22 -22
  159. package/workflows/review.workflow.md +30 -30
@@ -1,178 +1,179 @@
1
- import { execFileSync, spawnSync } from "node:child_process";
2
- import * as fs from "node:fs";
3
- import * as path from "node:path";
4
- import { allAgents, discoverAgents } from "../../agents/discover-agents.ts";
5
- import { allTeams, discoverTeams } from "../../teams/discover-teams.ts";
6
- import { allWorkflows, discoverWorkflows } from "../../workflows/discover-workflows.ts";
7
- import { loadConfig } from "../../config/config.ts";
8
- import { projectPiRoot, userPiRoot } from "../../utils/paths.ts";
9
- import type { TeamToolParamsValue } from "../../schema/team-tool-schema.ts";
10
- import { getPiSpawnCommand } from "../../runtime/pi-spawn.ts";
11
- import { validateResources } from "../validate-resources.ts";
12
- import type { PiTeamsToolResult } from "../tool-result.ts";
13
- import { configRecord, result, type TeamContext } from "./context.ts";
14
-
15
- interface DoctorCheck {
16
- label: string;
17
- ok: boolean;
18
- detail: string;
19
- }
20
-
21
- function firstOutputLine(stdout: string | null | undefined, stderr: string | null | undefined): string {
22
- const output = `${stdout ?? ""}\n${stderr ?? ""}`.trim();
23
- return output.split(/\r?\n/).find((line) => line.trim().length > 0)?.trim() ?? "available";
24
- }
25
-
26
- function commandExists(command: string, args: string[]): { ok: boolean; detail: string } {
27
- try {
28
- const output = spawnSync(command, args, { encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] });
29
- if (output.error) {
30
- return { ok: false, detail: output.error.message };
31
- }
32
- if (output.status !== 0) {
33
- return { ok: false, detail: firstOutputLine(output.stdout, output.stderr) || `status ${output.status}` };
34
- }
35
- return { ok: true, detail: firstOutputLine(output.stdout, output.stderr) };
36
- } catch (error) {
37
- return { ok: false, detail: error instanceof Error ? error.message : String(error) };
38
- }
39
- }
40
-
41
- function piCommandExists(): { ok: boolean; detail: string } {
42
- const spec = getPiSpawnCommand(["--version"]);
43
- const output = commandExists(spec.command, spec.args);
44
- if (!output.ok) return output;
45
- const executable = spec.command === "pi" ? "pi" : `${spec.command} ${spec.args[0] ?? ""}`.trim();
46
- return { ok: true, detail: `${output.detail} (${executable})` };
47
- }
48
-
49
- function checkWritableDir(dir: string): { ok: boolean; detail: string } {
50
- try {
51
- if (!fs.existsSync(dir)) return { ok: false, detail: `${dir}: missing` };
52
- if (!fs.statSync(dir).isDirectory()) return { ok: false, detail: `${dir}: not a directory` };
53
- fs.accessSync(dir, fs.constants.W_OK);
54
- return { ok: true, detail: dir };
55
- } catch (error) {
56
- const message = error instanceof Error ? error.message : String(error);
57
- return { ok: false, detail: `${dir}: ${message}` };
58
- }
59
- }
60
-
61
- function makeLine(check: DoctorCheck): string {
62
- return `- ${check.ok ? "OK" : "FAIL"} ${check.label}: ${check.detail}`;
63
- }
64
-
65
- function section(title: string, checks: () => DoctorCheck[]): string[] {
66
- try {
67
- return [title, ...checks().map(makeLine)];
68
- } catch (error) {
69
- const detail = error instanceof Error ? error.message : String(error);
70
- return [title, `- FAIL ${title}: ${detail}`];
71
- }
72
- }
73
-
74
- export interface TeamDoctorReportInput {
75
- cwd: string;
76
- configPath: string;
77
- configErrors: string[];
78
- configWarnings: string[];
79
- model?: { provider: string; id: string };
80
- validationErrors: number;
81
- validationWarnings: number;
82
- smokeChildPi?: { ok: boolean; detail: string };
83
- }
84
-
85
- export interface TeamDoctorReport {
86
- text: string;
87
- hasErrors: boolean;
88
- }
89
-
90
- export function buildTeamDoctorReport(input: TeamDoctorReportInput): TeamDoctorReport {
91
- const sections = [
92
- section("Runtime", () => {
93
- const git = commandExists("git", ["--version"]);
94
- const pi = piCommandExists();
95
- return [
96
- { label: "cwd", ok: true, detail: input.cwd },
97
- { label: "platform", ok: true, detail: `${process.platform}/${process.arch} node=${process.version}` },
98
- { label: "pi command", ok: pi.ok, detail: pi.detail },
99
- { label: "git command", ok: git.ok, detail: git.detail },
100
- { label: "config", ok: input.configErrors.length === 0, detail: `${input.configPath} (${input.configErrors.length} errors)` },
101
- { label: "model", ok: true, detail: input.model ? `${input.model.provider}/${input.model.id}` : "not available in this context" },
102
- { label: "config warnings", ok: true, detail: `${input.configWarnings.length} warnings` },
103
- ];
104
- }),
105
- section("Filesystem", () => {
106
- const userWritable = checkWritableDir(path.join(userPiRoot(), "extensions", "pi-crew"));
107
- const projectWritable = checkWritableDir(path.join(projectPiRoot(input.cwd), "teams"));
108
- return [
109
- { label: "user state", ok: userWritable.ok, detail: userWritable.detail },
110
- { label: "project state", ok: projectWritable.ok, detail: projectWritable.detail },
111
- { label: "project state root", ok: true, detail: path.join(projectPiRoot(input.cwd), "teams") },
112
- { label: "artifacts root", ok: true, detail: path.join(projectPiRoot(input.cwd), "artifacts") },
113
- ];
114
- }),
115
- section("Discovery", () => {
116
- const discoveredAgents = allAgents(discoverAgents(input.cwd));
117
- const discoveredTeams = allTeams(discoverTeams(input.cwd));
118
- const discoveredWorkflows = allWorkflows(discoverWorkflows(input.cwd));
119
- const agentModelHints = discoveredAgents.filter((agent) => agent.model || agent.fallbackModels?.length).length;
120
- return [
121
- { label: "agents", ok: true, detail: `${discoveredAgents.length} discovered` },
122
- { label: "teams", ok: true, detail: `${discoveredTeams.length} discovered` },
123
- { label: "workflows", ok: true, detail: `${discoveredWorkflows.length} discovered` },
124
- { label: "resource model hints", ok: true, detail: `${agentModelHints} agents declare model/fallback preferences` },
125
- ];
126
- }),
127
- section("Resource validation", () => [{
128
- label: "resource validation",
129
- ok: input.validationErrors === 0,
130
- detail: `${input.validationErrors} errors, ${input.validationWarnings} warnings`,
131
- }]),
132
- ];
133
- if (input.smokeChildPi) {
134
- sections.push([`Child check`, `- ${input.smokeChildPi.ok ? "OK" : "FAIL"} child Pi smoke: ${input.smokeChildPi.detail}`]);
135
- }
136
- const lines = ["pi-crew doctor report"];
137
- for (const block of sections) {
138
- if (block.length > 0) {
139
- lines.push(...block);
140
- lines.push("");
141
- }
142
- }
143
- if (lines.at(-1) === "") lines.pop();
144
- const text = lines.join("\n");
145
- return { text, hasErrors: sections.some((sectionLines) => sectionLines.some((line) => line.includes("FAIL"))) };
146
- }
147
-
148
- export function handleDoctor(ctx: TeamContext, params: TeamToolParamsValue = {}): PiTeamsToolResult {
149
- const loadedConfig = loadConfig(ctx.cwd);
150
- let smokeChildPi: { ok: boolean; detail: string } | undefined;
151
- if (configRecord(params.config).smokeChildPi === true) {
152
- try {
153
- const spec = getPiSpawnCommand(["--mode", "json", "-p", "Reply with exactly PI-TEAMS-SMOKE-OK"]);
154
- const output = execFileSync(spec.command, spec.args, {
155
- cwd: ctx.cwd,
156
- encoding: "utf-8",
157
- stdio: ["ignore", "pipe", "pipe"],
158
- timeout: 15_000,
159
- }).trim();
160
- smokeChildPi = { ok: output.includes("PI-TEAMS-SMOKE-OK"), detail: output.split("\n").slice(-1)[0] ?? "completed" };
161
- } catch (error) {
162
- const message = error instanceof Error ? error.message : String(error);
163
- smokeChildPi = { ok: false, detail: message };
164
- }
165
- }
166
- const validation = validateResources(ctx.cwd);
167
- const { text, hasErrors } = buildTeamDoctorReport({
168
- cwd: ctx.cwd,
169
- configPath: loadedConfig.path,
170
- configErrors: loadedConfig.error ? [loadedConfig.error] : [],
171
- configWarnings: loadedConfig.warnings ?? [],
172
- model: ctx.model,
173
- validationErrors: validation.issues.filter((issue) => issue.level === "error").length,
174
- validationWarnings: validation.issues.filter((issue) => issue.level === "warning").length,
175
- smokeChildPi,
176
- });
177
- return result(text, { action: "doctor", status: hasErrors ? "error" : "ok" }, hasErrors);
178
- }
1
+ import { execFileSync, spawnSync } from "node:child_process";
2
+ import * as fs from "node:fs";
3
+ import * as path from "node:path";
4
+ import { allAgents, discoverAgents } from "../../agents/discover-agents.ts";
5
+ import { allTeams, discoverTeams } from "../../teams/discover-teams.ts";
6
+ import { allWorkflows, discoverWorkflows } from "../../workflows/discover-workflows.ts";
7
+ import { loadConfig } from "../../config/config.ts";
8
+ import { projectCrewRoot, userCrewRoot } from "../../utils/paths.ts";
9
+ import { DEFAULT_PATHS } from "../../config/defaults.ts";
10
+ import type { TeamToolParamsValue } from "../../schema/team-tool-schema.ts";
11
+ import { getPiSpawnCommand } from "../../runtime/pi-spawn.ts";
12
+ import { validateResources } from "../validate-resources.ts";
13
+ import type { PiTeamsToolResult } from "../tool-result.ts";
14
+ import { configRecord, result, type TeamContext } from "./context.ts";
15
+
16
+ interface DoctorCheck {
17
+ label: string;
18
+ ok: boolean;
19
+ detail: string;
20
+ }
21
+
22
+ function firstOutputLine(stdout: string | null | undefined, stderr: string | null | undefined): string {
23
+ const output = `${stdout ?? ""}\n${stderr ?? ""}`.trim();
24
+ return output.split(/\r?\n/).find((line) => line.trim().length > 0)?.trim() ?? "available";
25
+ }
26
+
27
+ function commandExists(command: string, args: string[]): { ok: boolean; detail: string } {
28
+ try {
29
+ const output = spawnSync(command, args, { encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] });
30
+ if (output.error) {
31
+ return { ok: false, detail: output.error.message };
32
+ }
33
+ if (output.status !== 0) {
34
+ return { ok: false, detail: firstOutputLine(output.stdout, output.stderr) || `status ${output.status}` };
35
+ }
36
+ return { ok: true, detail: firstOutputLine(output.stdout, output.stderr) };
37
+ } catch (error) {
38
+ return { ok: false, detail: error instanceof Error ? error.message : String(error) };
39
+ }
40
+ }
41
+
42
+ function piCommandExists(): { ok: boolean; detail: string } {
43
+ const spec = getPiSpawnCommand(["--version"]);
44
+ const output = commandExists(spec.command, spec.args);
45
+ if (!output.ok) return output;
46
+ const executable = spec.command === "pi" ? "pi" : `${spec.command} ${spec.args[0] ?? ""}`.trim();
47
+ return { ok: true, detail: `${output.detail} (${executable})` };
48
+ }
49
+
50
+ function checkWritableDir(dir: string): { ok: boolean; detail: string } {
51
+ try {
52
+ if (!fs.existsSync(dir)) return { ok: false, detail: `${dir}: missing` };
53
+ if (!fs.statSync(dir).isDirectory()) return { ok: false, detail: `${dir}: not a directory` };
54
+ fs.accessSync(dir, fs.constants.W_OK);
55
+ return { ok: true, detail: dir };
56
+ } catch (error) {
57
+ const message = error instanceof Error ? error.message : String(error);
58
+ return { ok: false, detail: `${dir}: ${message}` };
59
+ }
60
+ }
61
+
62
+ function makeLine(check: DoctorCheck): string {
63
+ return `- ${check.ok ? "OK" : "FAIL"} ${check.label}: ${check.detail}`;
64
+ }
65
+
66
+ function section(title: string, checks: () => DoctorCheck[]): string[] {
67
+ try {
68
+ return [title, ...checks().map(makeLine)];
69
+ } catch (error) {
70
+ const detail = error instanceof Error ? error.message : String(error);
71
+ return [title, `- FAIL ${title}: ${detail}`];
72
+ }
73
+ }
74
+
75
+ export interface TeamDoctorReportInput {
76
+ cwd: string;
77
+ configPath: string;
78
+ configErrors: string[];
79
+ configWarnings: string[];
80
+ model?: { provider: string; id: string };
81
+ validationErrors: number;
82
+ validationWarnings: number;
83
+ smokeChildPi?: { ok: boolean; detail: string };
84
+ }
85
+
86
+ export interface TeamDoctorReport {
87
+ text: string;
88
+ hasErrors: boolean;
89
+ }
90
+
91
+ export function buildTeamDoctorReport(input: TeamDoctorReportInput): TeamDoctorReport {
92
+ const sections = [
93
+ section("Runtime", () => {
94
+ const git = commandExists("git", ["--version"]);
95
+ const pi = piCommandExists();
96
+ return [
97
+ { label: "cwd", ok: true, detail: input.cwd },
98
+ { label: "platform", ok: true, detail: `${process.platform}/${process.arch} node=${process.version}` },
99
+ { label: "pi command", ok: pi.ok, detail: pi.detail },
100
+ { label: "git command", ok: git.ok, detail: git.detail },
101
+ { label: "config", ok: input.configErrors.length === 0, detail: `${input.configPath} (${input.configErrors.length} errors)` },
102
+ { label: "model", ok: true, detail: input.model ? `${input.model.provider}/${input.model.id}` : "not available in this context" },
103
+ { label: "config warnings", ok: true, detail: `${input.configWarnings.length} warnings` },
104
+ ];
105
+ }),
106
+ section("Filesystem", () => {
107
+ const userWritable = checkWritableDir(userCrewRoot());
108
+ const projectWritable = checkWritableDir(projectCrewRoot(input.cwd));
109
+ return [
110
+ { label: "user state", ok: userWritable.ok, detail: userWritable.detail },
111
+ { label: "project state", ok: projectWritable.ok, detail: projectWritable.detail },
112
+ { label: "project state root", ok: true, detail: path.join(projectCrewRoot(input.cwd), DEFAULT_PATHS.state.runsSubdir) },
113
+ { label: "artifacts root", ok: true, detail: path.join(projectCrewRoot(input.cwd), DEFAULT_PATHS.state.artifactsSubdir) },
114
+ ];
115
+ }),
116
+ section("Discovery", () => {
117
+ const discoveredAgents = allAgents(discoverAgents(input.cwd));
118
+ const discoveredTeams = allTeams(discoverTeams(input.cwd));
119
+ const discoveredWorkflows = allWorkflows(discoverWorkflows(input.cwd));
120
+ const agentModelHints = discoveredAgents.filter((agent) => agent.model || agent.fallbackModels?.length).length;
121
+ return [
122
+ { label: "agents", ok: true, detail: `${discoveredAgents.length} discovered` },
123
+ { label: "teams", ok: true, detail: `${discoveredTeams.length} discovered` },
124
+ { label: "workflows", ok: true, detail: `${discoveredWorkflows.length} discovered` },
125
+ { label: "resource model hints", ok: true, detail: `${agentModelHints} agents declare model/fallback preferences` },
126
+ ];
127
+ }),
128
+ section("Resource validation", () => [{
129
+ label: "resource validation",
130
+ ok: input.validationErrors === 0,
131
+ detail: `${input.validationErrors} errors, ${input.validationWarnings} warnings`,
132
+ }]),
133
+ ];
134
+ if (input.smokeChildPi) {
135
+ sections.push([`Child check`, `- ${input.smokeChildPi.ok ? "OK" : "FAIL"} child Pi smoke: ${input.smokeChildPi.detail}`]);
136
+ }
137
+ const lines = ["pi-crew doctor report"];
138
+ for (const block of sections) {
139
+ if (block.length > 0) {
140
+ lines.push(...block);
141
+ lines.push("");
142
+ }
143
+ }
144
+ if (lines.at(-1) === "") lines.pop();
145
+ const text = lines.join("\n");
146
+ return { text, hasErrors: sections.some((sectionLines) => sectionLines.some((line) => line.includes("FAIL"))) };
147
+ }
148
+
149
+ export function handleDoctor(ctx: TeamContext, params: TeamToolParamsValue = {}): PiTeamsToolResult {
150
+ const loadedConfig = loadConfig(ctx.cwd);
151
+ let smokeChildPi: { ok: boolean; detail: string } | undefined;
152
+ if (configRecord(params.config).smokeChildPi === true) {
153
+ try {
154
+ const spec = getPiSpawnCommand(["--mode", "json", "-p", "Reply with exactly PI-TEAMS-SMOKE-OK"]);
155
+ const output = execFileSync(spec.command, spec.args, {
156
+ cwd: ctx.cwd,
157
+ encoding: "utf-8",
158
+ stdio: ["ignore", "pipe", "pipe"],
159
+ timeout: 15_000,
160
+ }).trim();
161
+ smokeChildPi = { ok: output.includes("PI-TEAMS-SMOKE-OK"), detail: output.split("\n").slice(-1)[0] ?? "completed" };
162
+ } catch (error) {
163
+ const message = error instanceof Error ? error.message : String(error);
164
+ smokeChildPi = { ok: false, detail: message };
165
+ }
166
+ }
167
+ const validation = validateResources(ctx.cwd);
168
+ const { text, hasErrors } = buildTeamDoctorReport({
169
+ cwd: ctx.cwd,
170
+ configPath: loadedConfig.path,
171
+ configErrors: loadedConfig.error ? [loadedConfig.error] : [],
172
+ configWarnings: loadedConfig.warnings ?? [],
173
+ model: ctx.model,
174
+ validationErrors: validation.issues.filter((issue) => issue.level === "error").length,
175
+ validationWarnings: validation.issues.filter((issue) => issue.level === "warning").length,
176
+ smokeChildPi,
177
+ });
178
+ return result(text, { action: "doctor", status: hasErrors ? "error" : "ok" }, hasErrors);
179
+ }
@@ -1,41 +1,41 @@
1
- import type { TeamToolParamsValue } from "../../schema/team-tool-schema.ts";
2
- import { readEvents } from "../../state/event-log.ts";
3
- import { loadRunManifestById } from "../../state/state-store.ts";
4
- import { aggregateUsage, formatUsage } from "../../state/usage.ts";
5
- import type { PiTeamsToolResult } from "../tool-result.ts";
6
- import { result, type TeamContext } from "./context.ts";
7
-
8
- export function handleEvents(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
9
- if (!params.runId) return result("Events requires runId.", { action: "events", status: "error" }, true);
10
- const loaded = loadRunManifestById(ctx.cwd, params.runId);
11
- if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "events", status: "error" }, true);
12
- const events = readEvents(loaded.manifest.eventsPath);
13
- const lines = [`Events for ${loaded.manifest.runId}:`, ...(events.length ? events.map((event) => `${event.time} ${event.type}${event.taskId ? ` ${event.taskId}` : ""}${event.message ? `: ${event.message}` : ""}${event.data ? ` ${JSON.stringify(event.data)}` : ""}`) : ["(none)"])];
14
- return result(lines.join("\n"), { action: "events", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
15
- }
16
-
17
- export function handleArtifacts(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
18
- if (!params.runId) return result("Artifacts requires runId.", { action: "artifacts", status: "error" }, true);
19
- const loaded = loadRunManifestById(ctx.cwd, params.runId);
20
- if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "artifacts", status: "error" }, true);
21
- const lines = [`Artifacts for ${loaded.manifest.runId}:`, ...(loaded.manifest.artifacts.length ? loaded.manifest.artifacts.map((artifact) => `- ${artifact.kind}: ${artifact.path}${artifact.sizeBytes !== undefined ? ` (${artifact.sizeBytes} bytes)` : ""}${artifact.contentHash ? ` sha256=${artifact.contentHash.slice(0, 12)}` : ""}`) : ["- (none)"])];
22
- return result(lines.join("\n"), { action: "artifacts", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
23
- }
24
-
25
- export function handleSummary(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
26
- if (!params.runId) return result("Summary requires runId.", { action: "summary", status: "error" }, true);
27
- const loaded = loadRunManifestById(ctx.cwd, params.runId);
28
- if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "summary", status: "error" }, true);
29
- const usage = aggregateUsage(loaded.tasks);
30
- const lines = [
31
- `Summary for ${loaded.manifest.runId}`,
32
- `Status: ${loaded.manifest.status}`,
33
- `Team: ${loaded.manifest.team}`,
34
- `Workflow: ${loaded.manifest.workflow ?? "(none)"}`,
35
- `Goal: ${loaded.manifest.goal}`,
36
- `Usage: ${formatUsage(usage)}`,
37
- "Tasks:",
38
- ...loaded.tasks.map((task) => `- ${task.id}: ${task.status} (${task.role} -> ${task.agent})${task.error ? ` - ${task.error}` : ""}`),
39
- ];
40
- return result(lines.join("\n"), { action: "summary", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
41
- }
1
+ import type { TeamToolParamsValue } from "../../schema/team-tool-schema.ts";
2
+ import { readEvents } from "../../state/event-log.ts";
3
+ import { loadRunManifestById } from "../../state/state-store.ts";
4
+ import { aggregateUsage, formatUsage } from "../../state/usage.ts";
5
+ import type { PiTeamsToolResult } from "../tool-result.ts";
6
+ import { result, type TeamContext } from "./context.ts";
7
+
8
+ export function handleEvents(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
9
+ if (!params.runId) return result("Events requires runId.", { action: "events", status: "error" }, true);
10
+ const loaded = loadRunManifestById(ctx.cwd, params.runId);
11
+ if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "events", status: "error" }, true);
12
+ const events = readEvents(loaded.manifest.eventsPath);
13
+ const lines = [`Events for ${loaded.manifest.runId}:`, ...(events.length ? events.map((event) => `${event.time} ${event.type}${event.taskId ? ` ${event.taskId}` : ""}${event.message ? `: ${event.message}` : ""}${event.data ? ` ${JSON.stringify(event.data)}` : ""}`) : ["(none)"])];
14
+ return result(lines.join("\n"), { action: "events", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
15
+ }
16
+
17
+ export function handleArtifacts(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
18
+ if (!params.runId) return result("Artifacts requires runId.", { action: "artifacts", status: "error" }, true);
19
+ const loaded = loadRunManifestById(ctx.cwd, params.runId);
20
+ if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "artifacts", status: "error" }, true);
21
+ const lines = [`Artifacts for ${loaded.manifest.runId}:`, ...(loaded.manifest.artifacts.length ? loaded.manifest.artifacts.map((artifact) => `- ${artifact.kind}: ${artifact.path}${artifact.sizeBytes !== undefined ? ` (${artifact.sizeBytes} bytes)` : ""}${artifact.contentHash ? ` sha256=${artifact.contentHash.slice(0, 12)}` : ""}`) : ["- (none)"])];
22
+ return result(lines.join("\n"), { action: "artifacts", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
23
+ }
24
+
25
+ export function handleSummary(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
26
+ if (!params.runId) return result("Summary requires runId.", { action: "summary", status: "error" }, true);
27
+ const loaded = loadRunManifestById(ctx.cwd, params.runId);
28
+ if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "summary", status: "error" }, true);
29
+ const usage = aggregateUsage(loaded.tasks);
30
+ const lines = [
31
+ `Summary for ${loaded.manifest.runId}`,
32
+ `Status: ${loaded.manifest.status}`,
33
+ `Team: ${loaded.manifest.team}`,
34
+ `Workflow: ${loaded.manifest.workflow ?? "(none)"}`,
35
+ `Goal: ${loaded.manifest.goal}`,
36
+ `Usage: ${formatUsage(usage)}`,
37
+ "Tasks:",
38
+ ...loaded.tasks.map((task) => `- ${task.id}: ${task.status} (${task.role} -> ${task.agent})${task.error ? ` - ${task.error}` : ""}`),
39
+ ];
40
+ return result(lines.join("\n"), { action: "summary", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
41
+ }
@@ -1,79 +1,79 @@
1
- import * as fs from "node:fs";
2
- import type { TeamToolParamsValue } from "../../schema/team-tool-schema.ts";
3
- import { appendEvent } from "../../state/event-log.ts";
4
- import { loadRunManifestById } from "../../state/state-store.ts";
5
- import { cleanupRunWorktrees } from "../../worktree/cleanup.ts";
6
- import { listImportedRuns } from "../import-index.ts";
7
- import { exportRunBundle } from "../run-export.ts";
8
- import { importRunBundle } from "../run-import.ts";
9
- import { pruneFinishedRuns } from "../run-maintenance.ts";
10
- import type { PiTeamsToolResult } from "../tool-result.ts";
11
- import { configRecord, result, type TeamContext } from "./context.ts";
12
-
13
- export function handleWorktrees(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
14
- if (!params.runId) return result("Worktrees requires runId.", { action: "worktrees", status: "error" }, true);
15
- const loaded = loadRunManifestById(ctx.cwd, params.runId);
16
- if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "worktrees", status: "error" }, true);
17
- const withWorktrees = loaded.tasks.filter((task) => task.worktree);
18
- const lines = [`Worktrees for ${loaded.manifest.runId}:`, ...(withWorktrees.length ? withWorktrees.map((task) => `- ${task.id}: ${task.worktree!.path} branch=${task.worktree!.branch} reused=${task.worktree!.reused ? "true" : "false"}`) : ["- (none)"])];
19
- return result(lines.join("\n"), { action: "worktrees", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
20
- }
21
-
22
- export function handleImports(_params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
23
- const imports = listImportedRuns(ctx.cwd);
24
- const lines = ["Imported pi-crew runs:", ...(imports.length ? imports.map((entry) => `- ${entry.runId} (${entry.scope})${entry.status ? ` [${entry.status}]` : ""} ${entry.team ?? "unknown"}/${entry.workflow ?? "none"}: ${entry.goal ?? ""}\n Bundle: ${entry.bundlePath}\n Summary: ${entry.summaryPath}`) : ["- (none)"])];
25
- return result(lines.join("\n"), { action: "imports", status: "ok" });
26
- }
27
-
28
- export function handleImport(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
29
- const cfg = configRecord(params.config);
30
- const bundlePath = typeof cfg.path === "string" ? cfg.path : typeof cfg.bundlePath === "string" ? cfg.bundlePath : undefined;
31
- if (!bundlePath) return result("Import requires config.path pointing at run-export.json.", { action: "import", status: "error" }, true);
32
- const scope = cfg.scope === "user" ? "user" : "project";
33
- try {
34
- const imported = importRunBundle(ctx.cwd, bundlePath, scope);
35
- return result([`Imported run bundle ${imported.runId}.`, `Bundle: ${imported.bundlePath}`, `Summary: ${imported.summaryPath}`].join("\n"), { action: "import", status: "ok" });
36
- } catch (error) {
37
- const message = error instanceof Error ? error.message : String(error);
38
- return result(`Import failed: ${message}`, { action: "import", status: "error" }, true);
39
- }
40
- }
41
-
42
- export function handleExport(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
43
- if (!params.runId) return result("Export requires runId.", { action: "export", status: "error" }, true);
44
- const loaded = loadRunManifestById(ctx.cwd, params.runId);
45
- if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "export", status: "error" }, true);
46
- const exported = exportRunBundle(loaded.manifest, loaded.tasks);
47
- appendEvent(loaded.manifest.eventsPath, { type: "run.exported", runId: loaded.manifest.runId, data: exported });
48
- return result([`Exported run ${loaded.manifest.runId}.`, `JSON: ${exported.jsonPath}`, `Markdown: ${exported.markdownPath}`].join("\n"), { action: "export", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
49
- }
50
-
51
- export function handlePrune(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
52
- const keep = params.keep ?? 20;
53
- if (!params.confirm) return result("prune requires confirm: true.", { action: "prune", status: "error" }, true);
54
- if (keep < 0 || !Number.isInteger(keep)) return result("keep must be an integer >= 0.", { action: "prune", status: "error" }, true);
55
- const pruned = pruneFinishedRuns(ctx.cwd, keep);
56
- return result([`Pruned finished pi-crew runs.`, `Kept: ${pruned.kept.length}`, `Removed: ${pruned.removed.length}`, ...(pruned.removed.length ? ["Removed runs:", ...pruned.removed.map((runId) => `- ${runId}`)] : [])].join("\n"), { action: "prune", status: "ok" });
57
- }
58
-
59
- export function handleForget(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
60
- if (!params.runId) return result("Forget requires runId.", { action: "forget", status: "error" }, true);
61
- if (!params.confirm) return result("forget requires confirm: true.", { action: "forget", status: "error" }, true);
62
- const loaded = loadRunManifestById(ctx.cwd, params.runId);
63
- if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "forget", status: "error" }, true);
64
- const cleanup = cleanupRunWorktrees(loaded.manifest, { force: params.force });
65
- if (cleanup.preserved.length > 0 && !params.force) return result([`Run '${params.runId}' has preserved worktrees. Use force: true to forget anyway.`, ...cleanup.preserved.map((item) => `- ${item.path}: ${item.reason}`)].join("\n"), { action: "forget", status: "error", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot }, true);
66
- fs.rmSync(loaded.manifest.stateRoot, { recursive: true, force: true });
67
- fs.rmSync(loaded.manifest.artifactsRoot, { recursive: true, force: true });
68
- return result([`Forgot run ${loaded.manifest.runId}.`, `Removed state: ${loaded.manifest.stateRoot}`, `Removed artifacts: ${loaded.manifest.artifactsRoot}`, ...(cleanup.removed.length ? ["Removed worktrees:", ...cleanup.removed.map((item) => `- ${item}`)] : [])].join("\n"), { action: "forget", status: "ok", runId: loaded.manifest.runId });
69
- }
70
-
71
- export function handleCleanup(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
72
- if (!params.runId) return result("Cleanup requires runId.", { action: "cleanup", status: "error" }, true);
73
- const loaded = loadRunManifestById(ctx.cwd, params.runId);
74
- if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "cleanup", status: "error" }, true);
75
- const cleanup = cleanupRunWorktrees(loaded.manifest, { force: params.force });
76
- appendEvent(loaded.manifest.eventsPath, { type: "worktree.cleanup", runId: loaded.manifest.runId, data: { removed: cleanup.removed, preserved: cleanup.preserved, artifacts: cleanup.artifactPaths } });
77
- const lines = [`Worktree cleanup for ${loaded.manifest.runId}:`, "Removed:", ...(cleanup.removed.length ? cleanup.removed.map((item) => `- ${item}`) : ["- (none)"]), "Preserved:", ...(cleanup.preserved.length ? cleanup.preserved.map((item) => `- ${item.path}: ${item.reason}`) : ["- (none)"]), "Artifacts:", ...(cleanup.artifactPaths.length ? cleanup.artifactPaths.map((item) => `- ${item}`) : ["- (none)"])];
78
- return result(lines.join("\n"), { action: "cleanup", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
79
- }
1
+ import * as fs from "node:fs";
2
+ import type { TeamToolParamsValue } from "../../schema/team-tool-schema.ts";
3
+ import { appendEvent } from "../../state/event-log.ts";
4
+ import { loadRunManifestById } from "../../state/state-store.ts";
5
+ import { cleanupRunWorktrees } from "../../worktree/cleanup.ts";
6
+ import { listImportedRuns } from "../import-index.ts";
7
+ import { exportRunBundle } from "../run-export.ts";
8
+ import { importRunBundle } from "../run-import.ts";
9
+ import { pruneFinishedRuns } from "../run-maintenance.ts";
10
+ import type { PiTeamsToolResult } from "../tool-result.ts";
11
+ import { configRecord, result, type TeamContext } from "./context.ts";
12
+
13
+ export function handleWorktrees(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
14
+ if (!params.runId) return result("Worktrees requires runId.", { action: "worktrees", status: "error" }, true);
15
+ const loaded = loadRunManifestById(ctx.cwd, params.runId);
16
+ if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "worktrees", status: "error" }, true);
17
+ const withWorktrees = loaded.tasks.filter((task) => task.worktree);
18
+ const lines = [`Worktrees for ${loaded.manifest.runId}:`, ...(withWorktrees.length ? withWorktrees.map((task) => `- ${task.id}: ${task.worktree!.path} branch=${task.worktree!.branch} reused=${task.worktree!.reused ? "true" : "false"}`) : ["- (none)"])];
19
+ return result(lines.join("\n"), { action: "worktrees", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
20
+ }
21
+
22
+ export function handleImports(_params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
23
+ const imports = listImportedRuns(ctx.cwd);
24
+ const lines = ["Imported pi-crew runs:", ...(imports.length ? imports.map((entry) => `- ${entry.runId} (${entry.scope})${entry.status ? ` [${entry.status}]` : ""} ${entry.team ?? "unknown"}/${entry.workflow ?? "none"}: ${entry.goal ?? ""}\n Bundle: ${entry.bundlePath}\n Summary: ${entry.summaryPath}`) : ["- (none)"])];
25
+ return result(lines.join("\n"), { action: "imports", status: "ok" });
26
+ }
27
+
28
+ export function handleImport(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
29
+ const cfg = configRecord(params.config);
30
+ const bundlePath = typeof cfg.path === "string" ? cfg.path : typeof cfg.bundlePath === "string" ? cfg.bundlePath : undefined;
31
+ if (!bundlePath) return result("Import requires config.path pointing at run-export.json.", { action: "import", status: "error" }, true);
32
+ const scope = cfg.scope === "user" ? "user" : "project";
33
+ try {
34
+ const imported = importRunBundle(ctx.cwd, bundlePath, scope);
35
+ return result([`Imported run bundle ${imported.runId}.`, `Bundle: ${imported.bundlePath}`, `Summary: ${imported.summaryPath}`].join("\n"), { action: "import", status: "ok" });
36
+ } catch (error) {
37
+ const message = error instanceof Error ? error.message : String(error);
38
+ return result(`Import failed: ${message}`, { action: "import", status: "error" }, true);
39
+ }
40
+ }
41
+
42
+ export function handleExport(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
43
+ if (!params.runId) return result("Export requires runId.", { action: "export", status: "error" }, true);
44
+ const loaded = loadRunManifestById(ctx.cwd, params.runId);
45
+ if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "export", status: "error" }, true);
46
+ const exported = exportRunBundle(loaded.manifest, loaded.tasks);
47
+ appendEvent(loaded.manifest.eventsPath, { type: "run.exported", runId: loaded.manifest.runId, data: exported });
48
+ return result([`Exported run ${loaded.manifest.runId}.`, `JSON: ${exported.jsonPath}`, `Markdown: ${exported.markdownPath}`].join("\n"), { action: "export", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
49
+ }
50
+
51
+ export function handlePrune(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
52
+ const keep = params.keep ?? 20;
53
+ if (!params.confirm) return result("prune requires confirm: true.", { action: "prune", status: "error" }, true);
54
+ if (keep < 0 || !Number.isInteger(keep)) return result("keep must be an integer >= 0.", { action: "prune", status: "error" }, true);
55
+ const pruned = pruneFinishedRuns(ctx.cwd, keep);
56
+ return result([`Pruned finished pi-crew runs.`, `Kept: ${pruned.kept.length}`, `Removed: ${pruned.removed.length}`, ...(pruned.removed.length ? ["Removed runs:", ...pruned.removed.map((runId) => `- ${runId}`)] : [])].join("\n"), { action: "prune", status: "ok" });
57
+ }
58
+
59
+ export function handleForget(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
60
+ if (!params.runId) return result("Forget requires runId.", { action: "forget", status: "error" }, true);
61
+ if (!params.confirm) return result("forget requires confirm: true.", { action: "forget", status: "error" }, true);
62
+ const loaded = loadRunManifestById(ctx.cwd, params.runId);
63
+ if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "forget", status: "error" }, true);
64
+ const cleanup = cleanupRunWorktrees(loaded.manifest, { force: params.force });
65
+ if (cleanup.preserved.length > 0 && !params.force) return result([`Run '${params.runId}' has preserved worktrees. Use force: true to forget anyway.`, ...cleanup.preserved.map((item) => `- ${item.path}: ${item.reason}`)].join("\n"), { action: "forget", status: "error", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot }, true);
66
+ fs.rmSync(loaded.manifest.stateRoot, { recursive: true, force: true });
67
+ fs.rmSync(loaded.manifest.artifactsRoot, { recursive: true, force: true });
68
+ return result([`Forgot run ${loaded.manifest.runId}.`, `Removed state: ${loaded.manifest.stateRoot}`, `Removed artifacts: ${loaded.manifest.artifactsRoot}`, ...(cleanup.removed.length ? ["Removed worktrees:", ...cleanup.removed.map((item) => `- ${item}`)] : [])].join("\n"), { action: "forget", status: "ok", runId: loaded.manifest.runId });
69
+ }
70
+
71
+ export function handleCleanup(params: TeamToolParamsValue, ctx: TeamContext): PiTeamsToolResult {
72
+ if (!params.runId) return result("Cleanup requires runId.", { action: "cleanup", status: "error" }, true);
73
+ const loaded = loadRunManifestById(ctx.cwd, params.runId);
74
+ if (!loaded) return result(`Run '${params.runId}' not found.`, { action: "cleanup", status: "error" }, true);
75
+ const cleanup = cleanupRunWorktrees(loaded.manifest, { force: params.force });
76
+ appendEvent(loaded.manifest.eventsPath, { type: "worktree.cleanup", runId: loaded.manifest.runId, data: { removed: cleanup.removed, preserved: cleanup.preserved, artifacts: cleanup.artifactPaths } });
77
+ const lines = [`Worktree cleanup for ${loaded.manifest.runId}:`, "Removed:", ...(cleanup.removed.length ? cleanup.removed.map((item) => `- ${item}`) : ["- (none)"]), "Preserved:", ...(cleanup.preserved.length ? cleanup.preserved.map((item) => `- ${item.path}: ${item.reason}`) : ["- (none)"]), "Artifacts:", ...(cleanup.artifactPaths.length ? cleanup.artifactPaths.map((item) => `- ${item}`) : ["- (none)"])];
78
+ return result(lines.join("\n"), { action: "cleanup", status: "ok", runId: loaded.manifest.runId, artifactsRoot: loaded.manifest.artifactsRoot });
79
+ }