@pi-agents/orchid 0.1.0-beta.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 (163) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/LICENSE +21 -0
  3. package/README.md +246 -0
  4. package/agents/AGENTS-MANIFEST.md +42 -0
  5. package/agents/brain.md +42 -0
  6. package/agents/context-builder.md +46 -0
  7. package/agents/delegate.md +12 -0
  8. package/agents/dev-1.md +42 -0
  9. package/agents/oracle.md +73 -0
  10. package/agents/planner.md +55 -0
  11. package/agents/researcher.md +52 -0
  12. package/agents/reviewer.md +79 -0
  13. package/agents/scout.md +50 -0
  14. package/agents/tester.md +45 -0
  15. package/agents/worker.md +55 -0
  16. package/extensions/ralph.ts +1 -0
  17. package/extensions/reviewer-extension.ts +125 -0
  18. package/extensions/task-orchestrator.ts +28 -0
  19. package/package.json +63 -0
  20. package/prompts/gather-context-and-clarify.md +13 -0
  21. package/prompts/parallel-cleanup.md +59 -0
  22. package/prompts/parallel-context-build.md +53 -0
  23. package/prompts/parallel-handoff-plan.md +59 -0
  24. package/prompts/parallel-research.md +50 -0
  25. package/prompts/parallel-review.md +54 -0
  26. package/prompts/review-loop.md +41 -0
  27. package/skills/orchid/SKILL.md +214 -0
  28. package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
  29. package/skills/orchid/orchid-converge/SKILL.md +124 -0
  30. package/skills/orchid/orchid-decompose/SKILL.md +201 -0
  31. package/skills/orchid/orchid-doctor/SKILL.md +162 -0
  32. package/skills/orchid/orchid-investigate/SKILL.md +102 -0
  33. package/skills/orchid/orchid-launch/SKILL.md +147 -0
  34. package/skills/ralph/SKILL.md +73 -0
  35. package/skills/subagents/pi-subagents/SKILL.md +813 -0
  36. package/src/index.ts +7 -0
  37. package/src/orchestrator/abort.ts +534 -0
  38. package/src/orchestrator/agent-bridge-extension.ts +1020 -0
  39. package/src/orchestrator/agent-host.ts +954 -0
  40. package/src/orchestrator/cleanup.ts +776 -0
  41. package/src/orchestrator/config-loader.ts +1412 -0
  42. package/src/orchestrator/config-schema.ts +690 -0
  43. package/src/orchestrator/config.ts +81 -0
  44. package/src/orchestrator/context-window.ts +66 -0
  45. package/src/orchestrator/diagnostic-reports.ts +475 -0
  46. package/src/orchestrator/diagnostics.ts +394 -0
  47. package/src/orchestrator/discovery.ts +1833 -0
  48. package/src/orchestrator/engine-worker.ts +415 -0
  49. package/src/orchestrator/engine.ts +5940 -0
  50. package/src/orchestrator/execution.ts +3104 -0
  51. package/src/orchestrator/extension.ts +5934 -0
  52. package/src/orchestrator/formatting.ts +785 -0
  53. package/src/orchestrator/git.ts +88 -0
  54. package/src/orchestrator/index.ts +28 -0
  55. package/src/orchestrator/lane-runner.ts +1787 -0
  56. package/src/orchestrator/mailbox.ts +780 -0
  57. package/src/orchestrator/merge.ts +3414 -0
  58. package/src/orchestrator/messages.ts +1062 -0
  59. package/src/orchestrator/migrations.ts +278 -0
  60. package/src/orchestrator/naming.ts +117 -0
  61. package/src/orchestrator/path-resolver.ts +275 -0
  62. package/src/orchestrator/persistence.ts +2625 -0
  63. package/src/orchestrator/process-registry.ts +452 -0
  64. package/src/orchestrator/quality-gate.ts +1085 -0
  65. package/src/orchestrator/resume.ts +3488 -0
  66. package/src/orchestrator/sessions.ts +57 -0
  67. package/src/orchestrator/settings-loader.ts +136 -0
  68. package/src/orchestrator/settings-tui.ts +2208 -0
  69. package/src/orchestrator/sidecar-telemetry.ts +267 -0
  70. package/src/orchestrator/supervisor.ts +4548 -0
  71. package/src/orchestrator/task-executor-core.ts +675 -0
  72. package/src/orchestrator/tmux-compat.ts +37 -0
  73. package/src/orchestrator/tool-allowlist-constants.ts +37 -0
  74. package/src/orchestrator/types.ts +4465 -0
  75. package/src/orchestrator/verification.ts +547 -0
  76. package/src/orchestrator/waves.ts +1564 -0
  77. package/src/orchestrator/workspace.ts +707 -0
  78. package/src/orchestrator/worktree.ts +2725 -0
  79. package/src/ralph/index.ts +825 -0
  80. package/src/subagents/agents/agent-management.ts +648 -0
  81. package/src/subagents/agents/agent-scope.ts +6 -0
  82. package/src/subagents/agents/agent-selection.ts +23 -0
  83. package/src/subagents/agents/agent-serializer.ts +86 -0
  84. package/src/subagents/agents/agents.ts +832 -0
  85. package/src/subagents/agents/chain-serializer.ts +137 -0
  86. package/src/subagents/agents/frontmatter.ts +29 -0
  87. package/src/subagents/agents/identity.ts +30 -0
  88. package/src/subagents/agents/skills.ts +632 -0
  89. package/src/subagents/extension/config.ts +16 -0
  90. package/src/subagents/extension/control-notices.ts +92 -0
  91. package/src/subagents/extension/doctor.ts +199 -0
  92. package/src/subagents/extension/fanout-child.ts +170 -0
  93. package/src/subagents/extension/index.ts +573 -0
  94. package/src/subagents/extension/schemas.ts +168 -0
  95. package/src/subagents/intercom/intercom-bridge.ts +379 -0
  96. package/src/subagents/intercom/result-intercom.ts +377 -0
  97. package/src/subagents/runs/background/async-execution.ts +712 -0
  98. package/src/subagents/runs/background/async-job-tracker.ts +310 -0
  99. package/src/subagents/runs/background/async-resume.ts +345 -0
  100. package/src/subagents/runs/background/async-status.ts +325 -0
  101. package/src/subagents/runs/background/completion-dedupe.ts +63 -0
  102. package/src/subagents/runs/background/notify.ts +108 -0
  103. package/src/subagents/runs/background/parallel-groups.ts +45 -0
  104. package/src/subagents/runs/background/result-watcher.ts +307 -0
  105. package/src/subagents/runs/background/run-id-resolver.ts +83 -0
  106. package/src/subagents/runs/background/run-status.ts +269 -0
  107. package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
  108. package/src/subagents/runs/background/subagent-runner.ts +1808 -0
  109. package/src/subagents/runs/background/top-level-async.ts +13 -0
  110. package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
  111. package/src/subagents/runs/foreground/chain-execution.ts +938 -0
  112. package/src/subagents/runs/foreground/execution.ts +918 -0
  113. package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
  114. package/src/subagents/runs/shared/completion-guard.ts +147 -0
  115. package/src/subagents/runs/shared/long-running-guard.ts +175 -0
  116. package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
  117. package/src/subagents/runs/shared/model-fallback.ts +103 -0
  118. package/src/subagents/runs/shared/nested-events.ts +819 -0
  119. package/src/subagents/runs/shared/nested-path.ts +52 -0
  120. package/src/subagents/runs/shared/nested-render.ts +115 -0
  121. package/src/subagents/runs/shared/parallel-utils.ts +109 -0
  122. package/src/subagents/runs/shared/pi-args.ts +220 -0
  123. package/src/subagents/runs/shared/pi-spawn.ts +115 -0
  124. package/src/subagents/runs/shared/run-history.ts +60 -0
  125. package/src/subagents/runs/shared/single-output.ts +164 -0
  126. package/src/subagents/runs/shared/subagent-control.ts +226 -0
  127. package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
  128. package/src/subagents/runs/shared/worktree.ts +577 -0
  129. package/src/subagents/shared/artifacts.ts +98 -0
  130. package/src/subagents/shared/atomic-json.ts +16 -0
  131. package/src/subagents/shared/file-coalescer.ts +40 -0
  132. package/src/subagents/shared/fork-context.ts +76 -0
  133. package/src/subagents/shared/formatters.ts +133 -0
  134. package/src/subagents/shared/jsonl-writer.ts +81 -0
  135. package/src/subagents/shared/model-info.ts +78 -0
  136. package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
  137. package/src/subagents/shared/session-identity.ts +10 -0
  138. package/src/subagents/shared/session-tokens.ts +44 -0
  139. package/src/subagents/shared/settings.ts +397 -0
  140. package/src/subagents/shared/status-format.ts +49 -0
  141. package/src/subagents/shared/types.ts +822 -0
  142. package/src/subagents/shared/utils.ts +450 -0
  143. package/src/subagents/slash/prompt-template-bridge.ts +397 -0
  144. package/src/subagents/slash/slash-bridge.ts +174 -0
  145. package/src/subagents/slash/slash-commands.ts +528 -0
  146. package/src/subagents/slash/slash-live-state.ts +292 -0
  147. package/src/subagents/tui/render-helpers.ts +80 -0
  148. package/src/subagents/tui/render.ts +1358 -0
  149. package/templates/agents/local/supervisor.md +33 -0
  150. package/templates/agents/local/task-merger.md +27 -0
  151. package/templates/agents/local/task-reviewer.md +30 -0
  152. package/templates/agents/local/task-worker.md +34 -0
  153. package/templates/agents/supervisor-routing.md +92 -0
  154. package/templates/agents/supervisor.md +229 -0
  155. package/templates/agents/task-merger.md +214 -0
  156. package/templates/agents/task-reviewer.md +260 -0
  157. package/templates/agents/task-worker-segment.md +44 -0
  158. package/templates/agents/task-worker.md +557 -0
  159. package/templates/tasks/CONTEXT.md +30 -0
  160. package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
  161. package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
  162. package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
  163. package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
@@ -0,0 +1,776 @@
1
+ /**
2
+ * Artifact cleanup and log rotation for orchestrator runtime files.
3
+ *
4
+ * Five cleanup layers prevent unbounded disk growth:
5
+ *
6
+ * 1. **Post-Integrate Cleanup** โ€” Deletes batch-specific telemetry and merge
7
+ * result files after successful /orch-integrate. Scoped by batchId.
8
+ *
9
+ * 2. **Age-Based Preflight Sweep** โ€” On /orch start, removes telemetry,
10
+ * verification, conversation, lane-state, and merge artifacts older than
11
+ * 3 days. Catches files missed by Layer 1 (e.g., aborted batches,
12
+ * manual branch deletions).
13
+ *
14
+ * 3. **Size-Capped Log Rotation** โ€” Rotates append-only supervisor logs
15
+ * (events.jsonl, actions.jsonl) at a 5MB threshold during preflight.
16
+ * Keeps one .old generation.
17
+ *
18
+ * 4. **Telemetry Size Cap** โ€” Enforces a 500MB cap on `.pi/telemetry/`
19
+ * by evicting oldest files first when the directory exceeds the cap.
20
+ *
21
+ * 5. **Batch-Start Cleanup** โ€” Removes artifacts from prior completed
22
+ * batches when a new batch starts, protecting the current batch.
23
+ *
24
+ * All cleanup is **non-fatal** โ€” failures warn but never block execution.
25
+ *
26
+ * @module orch/cleanup
27
+ * @since TP-065
28
+ */
29
+ import { existsSync, readdirSync, statSync, unlinkSync, renameSync, mkdirSync, rmSync } from "fs";
30
+ import { join } from "path";
31
+ import { MAILBOX_DIR_NAME } from "./types.ts";
32
+
33
+ // โ”€โ”€ Layer 1: Post-Integrate Cleanup โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
34
+
35
+ /**
36
+ * Result of post-integrate artifact cleanup.
37
+ */
38
+ export interface PostIntegrateCleanupResult {
39
+ /** Number of telemetry files deleted */
40
+ telemetryFilesDeleted: number;
41
+ /** Number of merge result/request files deleted */
42
+ mergeFilesDeleted: number;
43
+ /** Number of lane prompt files deleted */
44
+ promptFilesDeleted: number;
45
+ /** Number of mailbox batch directories deleted (0 or 1) */
46
+ mailboxDirsDeleted: number;
47
+ /** Number of context-snapshot batch directories deleted (0 or 1) */
48
+ snapshotDirsDeleted: number;
49
+ /** Warnings from non-fatal cleanup failures */
50
+ warnings: string[];
51
+ }
52
+
53
+ /**
54
+ * Clean up batch-specific telemetry and merge result files after integrate.
55
+ *
56
+ * Targets files whose names contain the batchId:
57
+ * - `.pi/telemetry/*-{batchId}-*.jsonl` โ€” worker/merger sidecar files
58
+ * - `.pi/telemetry/*-{batchId}-*-exit.json` โ€” exit summaries
59
+ * - `.pi/telemetry/lane-prompt-*.txt` โ€” temporary prompt files (all, not scoped)
60
+ * - `.pi/merge-result-*-{batchId}.json` โ€” merge result files
61
+ * - `.pi/merge-request-*-{batchId}.txt` โ€” merge request files
62
+ *
63
+ * @param stateRoot - Root directory containing .pi/ (workspace root or repo root)
64
+ * @param batchId - Batch ID to scope deletion
65
+ * @returns Cleanup result with counts and warnings
66
+ */
67
+ export function cleanupPostIntegrate(
68
+ stateRoot: string,
69
+ batchId: string,
70
+ ): PostIntegrateCleanupResult {
71
+ const result: PostIntegrateCleanupResult = {
72
+ telemetryFilesDeleted: 0,
73
+ mergeFilesDeleted: 0,
74
+ promptFilesDeleted: 0,
75
+ mailboxDirsDeleted: 0,
76
+ snapshotDirsDeleted: 0,
77
+ warnings: [],
78
+ };
79
+
80
+ if (!batchId) {
81
+ result.warnings.push("No batchId provided โ€” skipping post-integrate cleanup");
82
+ return result;
83
+ }
84
+
85
+ // โ”€โ”€ Telemetry files (.pi/telemetry/) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
86
+ const telemetryDir = join(stateRoot, ".pi", "telemetry");
87
+ if (existsSync(telemetryDir)) {
88
+ try {
89
+ const entries = readdirSync(telemetryDir);
90
+ for (const entry of entries) {
91
+ // Delete batch-scoped sidecar/exit files containing the batchId
92
+ if (entry.includes(batchId) && (entry.endsWith(".jsonl") || entry.endsWith("-exit.json"))) {
93
+ try {
94
+ unlinkSync(join(telemetryDir, entry));
95
+ result.telemetryFilesDeleted++;
96
+ } catch (err: unknown) {
97
+ result.warnings.push(`Failed to delete telemetry file ${entry}: ${(err as Error).message}`);
98
+ }
99
+ }
100
+ // Delete all lane-prompt-*.txt files (not batch-scoped โ€” they're
101
+ // temporary and should be cleaned up with any batch)
102
+ if (entry.startsWith("lane-prompt-") && entry.endsWith(".txt")) {
103
+ try {
104
+ unlinkSync(join(telemetryDir, entry));
105
+ result.promptFilesDeleted++;
106
+ } catch (err: unknown) {
107
+ result.warnings.push(`Failed to delete prompt file ${entry}: ${(err as Error).message}`);
108
+ }
109
+ }
110
+ }
111
+ } catch (err: unknown) {
112
+ result.warnings.push(`Failed to read telemetry directory: ${(err as Error).message}`);
113
+ }
114
+ }
115
+
116
+ // โ”€โ”€ Merge result/request files (.pi/) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
117
+ const piDir = join(stateRoot, ".pi");
118
+ if (existsSync(piDir)) {
119
+ try {
120
+ const entries = readdirSync(piDir);
121
+ for (const entry of entries) {
122
+ if (
123
+ entry.includes(batchId) &&
124
+ ((entry.startsWith("merge-result-") && entry.endsWith(".json")) ||
125
+ (entry.startsWith("merge-request-") && entry.endsWith(".txt")))
126
+ ) {
127
+ try {
128
+ unlinkSync(join(piDir, entry));
129
+ result.mergeFilesDeleted++;
130
+ } catch (err: unknown) {
131
+ result.warnings.push(`Failed to delete merge file ${entry}: ${(err as Error).message}`);
132
+ }
133
+ }
134
+ }
135
+ } catch (err: unknown) {
136
+ result.warnings.push(`Failed to read .pi directory: ${(err as Error).message}`);
137
+ }
138
+ }
139
+
140
+ // โ”€โ”€ Mailbox directory (.pi/mailbox/{batchId}/) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
141
+ const mailboxBatchDir = join(stateRoot, ".pi", MAILBOX_DIR_NAME, batchId);
142
+ if (existsSync(mailboxBatchDir)) {
143
+ try {
144
+ rmSync(mailboxBatchDir, { recursive: true, force: true });
145
+ result.mailboxDirsDeleted = 1;
146
+ } catch (err: unknown) {
147
+ result.warnings.push(
148
+ `Failed to delete mailbox directory ${mailboxBatchDir}: ${(err as Error).message}`,
149
+ );
150
+ }
151
+ }
152
+
153
+ // โ”€โ”€ Context snapshots directory (.pi/context-snapshots/{batchId}/) โ”€โ”€โ”€โ”€โ”€โ”€
154
+ const snapshotBatchDir = join(stateRoot, ".pi", "context-snapshots", batchId);
155
+ if (existsSync(snapshotBatchDir)) {
156
+ try {
157
+ rmSync(snapshotBatchDir, { recursive: true, force: true });
158
+ result.snapshotDirsDeleted = 1;
159
+ } catch (err: unknown) {
160
+ result.warnings.push(
161
+ `Failed to delete context-snapshots directory ${snapshotBatchDir}: ${(err as Error).message}`,
162
+ );
163
+ }
164
+ }
165
+
166
+ return result;
167
+ }
168
+
169
+ /**
170
+ * Format post-integrate cleanup result for user-facing notification.
171
+ */
172
+ export function formatPostIntegrateCleanup(result: PostIntegrateCleanupResult): string {
173
+ const parts: string[] = [];
174
+ const totalDeleted =
175
+ result.telemetryFilesDeleted +
176
+ result.mergeFilesDeleted +
177
+ result.promptFilesDeleted +
178
+ result.mailboxDirsDeleted +
179
+ result.snapshotDirsDeleted;
180
+
181
+ if (totalDeleted > 0) {
182
+ const segments: string[] = [];
183
+ if (result.telemetryFilesDeleted > 0) segments.push(`${result.telemetryFilesDeleted} telemetry`);
184
+ if (result.mergeFilesDeleted > 0) segments.push(`${result.mergeFilesDeleted} merge`);
185
+ if (result.promptFilesDeleted > 0) segments.push(`${result.promptFilesDeleted} prompt`);
186
+ if (result.mailboxDirsDeleted > 0) segments.push(`${result.mailboxDirsDeleted} mailbox`);
187
+ if (result.snapshotDirsDeleted > 0) segments.push(`${result.snapshotDirsDeleted} snapshots`);
188
+ parts.push(`๐Ÿงน Cleaned up ${totalDeleted} artifact file(s): ${segments.join(", ")}`);
189
+ }
190
+
191
+ for (const warning of result.warnings) {
192
+ parts.push(` โš ๏ธ ${warning}`);
193
+ }
194
+
195
+ return parts.join("\n");
196
+ }
197
+
198
+ // โ”€โ”€ Layer 2: Age-Based Preflight Sweep โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
199
+
200
+ /** Default max age for stale artifacts (3 days in milliseconds). */
201
+ export const STALE_ARTIFACT_MAX_AGE_MS = 3 * 24 * 60 * 60 * 1000;
202
+
203
+ /**
204
+ * Result of a preflight age-based sweep.
205
+ */
206
+ export interface PreflightSweepResult {
207
+ /** Number of stale files deleted */
208
+ staleFilesDeleted: number;
209
+ /** Number of stale mailbox batch directories deleted */
210
+ staleDirsDeleted: number;
211
+ /** Whether the sweep was skipped (e.g., active batch) */
212
+ skipped: boolean;
213
+ /** Reason for skipping (if skipped) */
214
+ skipReason?: string;
215
+ /** Warnings from non-fatal cleanup failures */
216
+ warnings: string[];
217
+ }
218
+
219
+ /**
220
+ * Dependencies injected into sweepStaleArtifacts for testability.
221
+ */
222
+ export interface SweepDeps {
223
+ /** Check if a batch is currently active (phase is not terminal). */
224
+ isBatchActive: () => boolean;
225
+ /** Get the current timestamp (for deterministic testing). */
226
+ now: () => number;
227
+ }
228
+
229
+ /**
230
+ * Sweep stale artifacts older than maxAgeMs during preflight.
231
+ *
232
+ * Targets:
233
+ * - `.pi/telemetry/*.jsonl` โ€” sidecar files
234
+ * - `.pi/telemetry/*-exit.json` โ€” exit summaries
235
+ * - `.pi/telemetry/lane-prompt-*.txt` โ€” temporary prompt files
236
+ * - `.pi/merge-result-*.json` โ€” merge result files
237
+ * - `.pi/merge-request-*.txt` โ€” merge request files
238
+ * - `.pi/verification/*` โ€” verification snapshots
239
+ * - `.pi/worker-conversation-*.jsonl` โ€” worker conversation logs
240
+ * - `.pi/lane-state-*.json` โ€” lane state files
241
+ *
242
+ * Uses file mtime for age detection. Skips files modified within maxAgeMs.
243
+ * If a batch is currently active (executing/merging), skips ALL cleanup.
244
+ *
245
+ * @param stateRoot - Root directory containing .pi/
246
+ * @param deps - Injectable dependencies for testability
247
+ * @param maxAgeMs - Maximum file age in milliseconds (default: 3 days)
248
+ * @returns Sweep result with count and warnings
249
+ */
250
+ export function sweepStaleArtifacts(
251
+ stateRoot: string,
252
+ deps: SweepDeps,
253
+ maxAgeMs: number = STALE_ARTIFACT_MAX_AGE_MS,
254
+ ): PreflightSweepResult {
255
+ const result: PreflightSweepResult = {
256
+ staleFilesDeleted: 0,
257
+ staleDirsDeleted: 0,
258
+ skipped: false,
259
+ warnings: [],
260
+ };
261
+
262
+ // Guard: skip if batch is actively executing
263
+ try {
264
+ if (deps.isBatchActive()) {
265
+ result.skipped = true;
266
+ result.skipReason = "Active batch detected โ€” skipping stale artifact sweep";
267
+ return result;
268
+ }
269
+ } catch {
270
+ // If we can't determine batch state, proceed cautiously
271
+ }
272
+
273
+ const now = deps.now();
274
+ const cutoff = now - maxAgeMs;
275
+
276
+ /**
277
+ * Delete files older than cutoff from a directory, matching a filter.
278
+ */
279
+ const sweepDir = (dir: string, filter: (name: string) => boolean): void => {
280
+ if (!existsSync(dir)) return;
281
+ try {
282
+ const entries = readdirSync(dir);
283
+ for (const entry of entries) {
284
+ if (!filter(entry)) continue;
285
+ const filePath = join(dir, entry);
286
+ try {
287
+ const stat = statSync(filePath);
288
+ if (!stat.isFile()) continue;
289
+ if (stat.mtimeMs < cutoff) {
290
+ unlinkSync(filePath);
291
+ result.staleFilesDeleted++;
292
+ }
293
+ } catch (err: unknown) {
294
+ result.warnings.push(`Failed to process ${entry}: ${(err as Error).message}`);
295
+ }
296
+ }
297
+ } catch (err: unknown) {
298
+ result.warnings.push(`Failed to read directory ${dir}: ${(err as Error).message}`);
299
+ }
300
+ };
301
+
302
+ // Sweep telemetry files
303
+ sweepDir(
304
+ join(stateRoot, ".pi", "telemetry"),
305
+ (name) =>
306
+ name.endsWith(".jsonl") ||
307
+ name.endsWith("-exit.json") ||
308
+ (name.startsWith("lane-prompt-") && name.endsWith(".txt")),
309
+ );
310
+
311
+ // Sweep merge result/request files
312
+ sweepDir(
313
+ join(stateRoot, ".pi"),
314
+ (name) =>
315
+ (name.startsWith("merge-result-") && name.endsWith(".json")) ||
316
+ (name.startsWith("merge-request-") && name.endsWith(".txt")),
317
+ );
318
+
319
+ // Sweep stale worker conversation logs (.pi/worker-conversation-*.jsonl)
320
+ sweepDir(
321
+ join(stateRoot, ".pi"),
322
+ (name) => name.startsWith("worker-conversation-") && name.endsWith(".jsonl"),
323
+ );
324
+
325
+ // Sweep stale lane state files (.pi/lane-state-*.json)
326
+ sweepDir(
327
+ join(stateRoot, ".pi"),
328
+ (name) => name.startsWith("lane-state-") && name.endsWith(".json"),
329
+ );
330
+
331
+ // Sweep stale batch directories under a parent (mailbox, context-snapshots, verification)
332
+ const sweepBatchDirs = (parentDir: string, label: string): void => {
333
+ if (!existsSync(parentDir)) return;
334
+ try {
335
+ const entries = readdirSync(parentDir);
336
+ for (const entry of entries) {
337
+ const entryPath = join(parentDir, entry);
338
+ try {
339
+ const stat = statSync(entryPath);
340
+ if (!stat.isDirectory()) continue;
341
+ if (stat.mtimeMs < cutoff) {
342
+ rmSync(entryPath, { recursive: true, force: true });
343
+ result.staleDirsDeleted++;
344
+ }
345
+ } catch (err: unknown) {
346
+ result.warnings.push(`Failed to process ${label} dir ${entry}: ${(err as Error).message}`);
347
+ }
348
+ }
349
+ } catch (err: unknown) {
350
+ result.warnings.push(
351
+ `Failed to read ${label} directory ${parentDir}: ${(err as Error).message}`,
352
+ );
353
+ }
354
+ };
355
+
356
+ // Sweep stale mailbox batch directories (.pi/mailbox/{batchId}/)
357
+ sweepBatchDirs(join(stateRoot, ".pi", MAILBOX_DIR_NAME), "mailbox");
358
+
359
+ // Sweep stale context-snapshot batch directories (.pi/context-snapshots/{batchId}/)
360
+ sweepBatchDirs(join(stateRoot, ".pi", "context-snapshots"), "context-snapshots");
361
+
362
+ // Sweep stale verification snapshot directories (.pi/verification/{opId}/)
363
+ sweepBatchDirs(join(stateRoot, ".pi", "verification"), "verification");
364
+
365
+ return result;
366
+ }
367
+
368
+ /**
369
+ * Format preflight sweep result for logging.
370
+ */
371
+ export function formatPreflightSweep(result: PreflightSweepResult): string {
372
+ if (result.skipped) {
373
+ return `โ„น๏ธ Preflight sweep skipped: ${result.skipReason}`;
374
+ }
375
+ if (
376
+ result.staleFilesDeleted === 0 &&
377
+ result.staleDirsDeleted === 0 &&
378
+ result.warnings.length === 0
379
+ ) {
380
+ return ""; // Nothing to report
381
+ }
382
+ const parts: string[] = [];
383
+ if (result.staleFilesDeleted > 0 || result.staleDirsDeleted > 0) {
384
+ const segments: string[] = [];
385
+ if (result.staleFilesDeleted > 0) segments.push(`${result.staleFilesDeleted} stale artifact(s)`);
386
+ if (result.staleDirsDeleted > 0) segments.push(`${result.staleDirsDeleted} stale mailbox dir(s)`);
387
+ parts.push(`๐Ÿงน Preflight cleanup: removed ${segments.join(" and ")} (>3 days old)`);
388
+ }
389
+ for (const warning of result.warnings) {
390
+ parts.push(` โš ๏ธ ${warning}`);
391
+ }
392
+ return parts.join("\n");
393
+ }
394
+
395
+ // โ”€โ”€ Layer 3: Size-Capped Log Rotation โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
396
+
397
+ /** Default rotation threshold: 5MB. */
398
+ export const LOG_ROTATION_THRESHOLD_BYTES = 5 * 1024 * 1024;
399
+
400
+ /**
401
+ * Result of log rotation.
402
+ */
403
+ export interface LogRotationResult {
404
+ /** Files that were rotated */
405
+ rotated: string[];
406
+ /** Warnings from non-fatal rotation failures */
407
+ warnings: string[];
408
+ }
409
+
410
+ /**
411
+ * Rotate supervisor append-only logs at a size threshold.
412
+ *
413
+ * Checks `events.jsonl` and `actions.jsonl` in `.pi/supervisor/`.
414
+ * If a file exceeds the threshold, renames it to `.old` (overwriting
415
+ * any existing `.old`), allowing a fresh file to be created on next write.
416
+ *
417
+ * Only call during preflight (not mid-batch).
418
+ *
419
+ * @param stateRoot - Root directory containing .pi/
420
+ * @param thresholdBytes - Maximum file size before rotation (default: 5MB)
421
+ * @returns Rotation result
422
+ */
423
+ export function rotateSupervisorLogs(
424
+ stateRoot: string,
425
+ thresholdBytes: number = LOG_ROTATION_THRESHOLD_BYTES,
426
+ ): LogRotationResult {
427
+ const result: LogRotationResult = {
428
+ rotated: [],
429
+ warnings: [],
430
+ };
431
+
432
+ const supervisorDir = join(stateRoot, ".pi", "supervisor");
433
+ if (!existsSync(supervisorDir)) {
434
+ return result; // Nothing to rotate
435
+ }
436
+
437
+ const filesToRotate = ["events.jsonl", "actions.jsonl"];
438
+
439
+ for (const fileName of filesToRotate) {
440
+ const filePath = join(supervisorDir, fileName);
441
+ if (!existsSync(filePath)) continue;
442
+
443
+ try {
444
+ const stat = statSync(filePath);
445
+ if (!stat.isFile() || stat.size <= thresholdBytes) continue;
446
+
447
+ const oldPath = `${filePath}.old`;
448
+ renameSync(filePath, oldPath);
449
+ result.rotated.push(fileName);
450
+ } catch (err: unknown) {
451
+ result.warnings.push(`Failed to rotate ${fileName}: ${(err as Error).message}`);
452
+ }
453
+ }
454
+
455
+ return result;
456
+ }
457
+
458
+ /**
459
+ * Format log rotation result for logging.
460
+ */
461
+ export function formatLogRotation(result: LogRotationResult): string {
462
+ if (result.rotated.length === 0 && result.warnings.length === 0) {
463
+ return ""; // Nothing to report
464
+ }
465
+ const parts: string[] = [];
466
+ if (result.rotated.length > 0) {
467
+ parts.push(`๐Ÿ”„ Rotated ${result.rotated.length} supervisor log(s): ${result.rotated.join(", ")}`);
468
+ }
469
+ for (const warning of result.warnings) {
470
+ parts.push(` โš ๏ธ ${warning}`);
471
+ }
472
+ return parts.join("\n");
473
+ }
474
+
475
+ // โ”€โ”€ Layer 4: Telemetry Directory Size Cap โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
476
+
477
+ /** Default telemetry directory size cap: 500 MB. */
478
+ export const TELEMETRY_SIZE_CAP_BYTES = 500 * 1024 * 1024;
479
+
480
+ /**
481
+ * Result of telemetry size cap enforcement.
482
+ */
483
+ export interface SizeCapResult {
484
+ /** Number of files deleted to bring directory under cap */
485
+ filesDeleted: number;
486
+ /** Total bytes freed */
487
+ bytesFreed: number;
488
+ /** Warnings from non-fatal failures */
489
+ warnings: string[];
490
+ }
491
+
492
+ /**
493
+ * Enforce a size cap on the telemetry directory by evicting oldest files first.
494
+ *
495
+ * Scans `.pi/telemetry/` and sums file sizes. If the total exceeds `capBytes`,
496
+ * deletes the oldest files (by mtime) until the total is under the cap.
497
+ *
498
+ * @param stateRoot - Root directory containing .pi/
499
+ * @param capBytes - Maximum allowed total size in bytes (default: 500MB)
500
+ * @returns Size cap enforcement result
501
+ */
502
+ export function enforceTelemetrySizeCap(
503
+ stateRoot: string,
504
+ capBytes: number = TELEMETRY_SIZE_CAP_BYTES,
505
+ ): SizeCapResult {
506
+ const result: SizeCapResult = {
507
+ filesDeleted: 0,
508
+ bytesFreed: 0,
509
+ warnings: [],
510
+ };
511
+
512
+ const telemetryDir = join(stateRoot, ".pi", "telemetry");
513
+ if (!existsSync(telemetryDir)) return result;
514
+
515
+ // Collect all files with size and mtime
516
+ interface FileEntry {
517
+ name: string;
518
+ path: string;
519
+ size: number;
520
+ mtimeMs: number;
521
+ }
522
+
523
+ const files: FileEntry[] = [];
524
+ let totalSize = 0;
525
+
526
+ try {
527
+ const entries = readdirSync(telemetryDir);
528
+ for (const entry of entries) {
529
+ const filePath = join(telemetryDir, entry);
530
+ try {
531
+ const stat = statSync(filePath);
532
+ if (!stat.isFile()) continue;
533
+ files.push({ name: entry, path: filePath, size: stat.size, mtimeMs: stat.mtimeMs });
534
+ totalSize += stat.size;
535
+ } catch (err: unknown) {
536
+ result.warnings.push(`Failed to stat ${entry}: ${(err as Error).message}`);
537
+ }
538
+ }
539
+ } catch (err: unknown) {
540
+ result.warnings.push(`Failed to read telemetry directory: ${(err as Error).message}`);
541
+ return result;
542
+ }
543
+
544
+ if (totalSize <= capBytes) return result;
545
+
546
+ // Sort oldest first (lowest mtime first)
547
+ files.sort((a, b) => a.mtimeMs - b.mtimeMs);
548
+
549
+ // Delete oldest files until under cap
550
+ for (const file of files) {
551
+ if (totalSize <= capBytes) break;
552
+ try {
553
+ unlinkSync(file.path);
554
+ totalSize -= file.size;
555
+ result.filesDeleted++;
556
+ result.bytesFreed += file.size;
557
+ } catch (err: unknown) {
558
+ result.warnings.push(`Failed to delete ${file.name}: ${(err as Error).message}`);
559
+ }
560
+ }
561
+
562
+ return result;
563
+ }
564
+
565
+ /**
566
+ * Format size cap result for logging.
567
+ */
568
+ export function formatSizeCap(result: SizeCapResult): string {
569
+ if (result.filesDeleted === 0 && result.warnings.length === 0) return "";
570
+ const parts: string[] = [];
571
+ if (result.filesDeleted > 0) {
572
+ const mbFreed = (result.bytesFreed / (1024 * 1024)).toFixed(1);
573
+ parts.push(`๐Ÿงน Telemetry size cap: deleted ${result.filesDeleted} file(s), freed ${mbFreed} MB`);
574
+ }
575
+ for (const warning of result.warnings) {
576
+ parts.push(` โš ๏ธ ${warning}`);
577
+ }
578
+ return parts.join("\n");
579
+ }
580
+
581
+ // โ”€โ”€ Layer 5: Batch-Start Cleanup of Prior Batch Artifacts โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
582
+
583
+ /**
584
+ * Result of prior-batch artifact cleanup.
585
+ */
586
+ export interface PriorBatchCleanupResult {
587
+ /** Number of files/dirs deleted */
588
+ itemsDeleted: number;
589
+ /** Warnings from non-fatal failures */
590
+ warnings: string[];
591
+ }
592
+
593
+ /**
594
+ * Clean up artifacts from prior completed batches when a new batch starts.
595
+ *
596
+ * Removes batch-scoped files that may have been left behind by prior runs
597
+ * that were not integrated (e.g., aborted, crashed). Only cleans artifacts
598
+ * from batches that are NOT the currently active batch.
599
+ *
600
+ * Targets the same file patterns as `cleanupPostIntegrate` plus stale
601
+ * batch-state files.
602
+ *
603
+ * @param stateRoot - Root directory containing .pi/
604
+ * @param currentBatchId - The batch ID that is currently starting (will NOT be deleted)
605
+ * @returns Cleanup result
606
+ */
607
+ export function cleanupPriorBatchArtifacts(
608
+ stateRoot: string,
609
+ currentBatchId: string,
610
+ ): PriorBatchCleanupResult {
611
+ const result: PriorBatchCleanupResult = {
612
+ itemsDeleted: 0,
613
+ warnings: [],
614
+ };
615
+
616
+ if (!currentBatchId) {
617
+ result.warnings.push("No currentBatchId provided โ€” skipping prior batch cleanup");
618
+ return result;
619
+ }
620
+
621
+ const piDir = join(stateRoot, ".pi");
622
+ if (!existsSync(piDir)) return result;
623
+
624
+ // Helper: delete files in a directory matching a filter, skipping current batch
625
+ const cleanDir = (dir: string, filter: (name: string) => boolean): void => {
626
+ if (!existsSync(dir)) return;
627
+ try {
628
+ const entries = readdirSync(dir);
629
+ for (const entry of entries) {
630
+ if (!filter(entry)) continue;
631
+ if (entry.includes(currentBatchId)) continue; // Protect current batch
632
+ const filePath = join(dir, entry);
633
+ try {
634
+ const stat = statSync(filePath);
635
+ if (stat.isFile()) {
636
+ unlinkSync(filePath);
637
+ result.itemsDeleted++;
638
+ }
639
+ } catch (err: unknown) {
640
+ result.warnings.push(`Failed to delete ${entry}: ${(err as Error).message}`);
641
+ }
642
+ }
643
+ } catch (err: unknown) {
644
+ result.warnings.push(`Failed to read directory ${dir}: ${(err as Error).message}`);
645
+ }
646
+ };
647
+
648
+ // Clean telemetry files from prior batches
649
+ cleanDir(
650
+ join(piDir, "telemetry"),
651
+ (name) =>
652
+ name.endsWith(".jsonl") ||
653
+ name.endsWith("-exit.json") ||
654
+ (name.startsWith("lane-prompt-") && name.endsWith(".txt")),
655
+ );
656
+
657
+ // Clean merge result/request files from prior batches
658
+ cleanDir(
659
+ piDir,
660
+ (name) =>
661
+ (name.startsWith("merge-result-") && name.endsWith(".json")) ||
662
+ (name.startsWith("merge-request-") && name.endsWith(".txt")),
663
+ );
664
+
665
+ // Clean worker conversation logs from prior batches
666
+ cleanDir(piDir, (name) => name.startsWith("worker-conversation-") && name.endsWith(".jsonl"));
667
+
668
+ // Clean lane state files from prior batches
669
+ cleanDir(piDir, (name) => name.startsWith("lane-state-") && name.endsWith(".json"));
670
+
671
+ // Clean batch-scoped directories (mailbox, context-snapshots)
672
+ const cleanBatchDirs = (parentDir: string): void => {
673
+ if (!existsSync(parentDir)) return;
674
+ try {
675
+ const entries = readdirSync(parentDir);
676
+ for (const entry of entries) {
677
+ if (entry === currentBatchId) continue; // Protect current batch
678
+ const entryPath = join(parentDir, entry);
679
+ try {
680
+ const stat = statSync(entryPath);
681
+ if (!stat.isDirectory()) continue;
682
+ rmSync(entryPath, { recursive: true, force: true });
683
+ result.itemsDeleted++;
684
+ } catch (err: unknown) {
685
+ result.warnings.push(`Failed to delete batch dir ${entry}: ${(err as Error).message}`);
686
+ }
687
+ }
688
+ } catch (err: unknown) {
689
+ result.warnings.push(`Failed to read directory ${parentDir}: ${(err as Error).message}`);
690
+ }
691
+ };
692
+
693
+ cleanBatchDirs(join(piDir, MAILBOX_DIR_NAME));
694
+ cleanBatchDirs(join(piDir, "context-snapshots"));
695
+
696
+ return result;
697
+ }
698
+
699
+ /**
700
+ * Format prior batch cleanup result for logging.
701
+ */
702
+ export function formatPriorBatchCleanup(result: PriorBatchCleanupResult): string {
703
+ if (result.itemsDeleted === 0 && result.warnings.length === 0) return "";
704
+ const parts: string[] = [];
705
+ if (result.itemsDeleted > 0) {
706
+ parts.push(
707
+ `๐Ÿงน Prior batch cleanup: removed ${result.itemsDeleted} artifact(s) from previous batch(es)`,
708
+ );
709
+ }
710
+ for (const warning of result.warnings) {
711
+ parts.push(` โš ๏ธ ${warning}`);
712
+ }
713
+ return parts.join("\n");
714
+ }
715
+
716
+ // โ”€โ”€ Combined Preflight Cleanup โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
717
+
718
+ /**
719
+ * Combined result of preflight cleanup (Layer 2 + Layer 3).
720
+ */
721
+ export interface PreflightCleanupResult {
722
+ sweep: PreflightSweepResult;
723
+ rotation: LogRotationResult;
724
+ }
725
+
726
+ /**
727
+ * Run all preflight cleanup operations (Layer 2 + Layer 3).
728
+ *
729
+ * Called from the engine's preflight phase before batch starts.
730
+ * Always non-fatal.
731
+ *
732
+ * @param stateRoot - Root directory containing .pi/
733
+ * @param deps - Sweep dependencies (active batch check)
734
+ * @returns Combined cleanup result
735
+ */
736
+ export function runPreflightCleanup(stateRoot: string, deps: SweepDeps): PreflightCleanupResult {
737
+ const sweep = sweepStaleArtifacts(stateRoot, deps);
738
+ const rotation = rotateSupervisorLogs(stateRoot);
739
+ return { sweep, rotation };
740
+ }
741
+
742
+ /**
743
+ * Format combined preflight cleanup result for user notification.
744
+ *
745
+ * Returns an empty string if nothing happened (no files cleaned/rotated).
746
+ */
747
+ export function formatPreflightCleanup(result: PreflightCleanupResult): string {
748
+ const parts: string[] = [];
749
+
750
+ // Layer 2: age-based sweep
751
+ if (
752
+ !result.sweep.skipped &&
753
+ (result.sweep.staleFilesDeleted > 0 || result.sweep.staleDirsDeleted > 0)
754
+ ) {
755
+ const segments: string[] = [];
756
+ if (result.sweep.staleFilesDeleted > 0)
757
+ segments.push(`${result.sweep.staleFilesDeleted} stale artifact(s)`);
758
+ if (result.sweep.staleDirsDeleted > 0)
759
+ segments.push(`${result.sweep.staleDirsDeleted} stale mailbox dir(s)`);
760
+ parts.push(`removed ${segments.join(" and ")} (>3 days old)`);
761
+ }
762
+
763
+ // Layer 3: log rotation
764
+ if (result.rotation.rotated.length > 0) {
765
+ parts.push(`rotated ${result.rotation.rotated.join(", ")} (>5 MB)`);
766
+ }
767
+
768
+ // Collect warnings from both layers
769
+ const warnings = [...result.sweep.warnings, ...result.rotation.warnings];
770
+ if (warnings.length > 0) {
771
+ parts.push(`โš ๏ธ ${warnings.length} cleanup warning(s)`);
772
+ }
773
+
774
+ if (parts.length === 0) return "";
775
+ return `๐Ÿงน Preflight cleanup: ${parts.join("; ")}`;
776
+ }