@ferris1225/pi-subagents 5.0.1 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +9 -4
- package/package.json +1 -1
- package/src/isolation/cleanup.ts +158 -0
- package/src/lifecycle/dispatch-core.ts +4 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 5.0.2
|
|
4
|
+
|
|
5
|
+
### Changes
|
|
6
|
+
|
|
7
|
+
- Reclaim disk automatically on settlement: one paced sweep per five minutes
|
|
8
|
+
drops dead-owner artifacts, expired results and settled session copies older
|
|
9
|
+
than 30 minutes, then keeps each project inside a 1.5 GB budget. Live runs,
|
|
10
|
+
parked recovery work, recent sessions and every manifest-referenced path stay.
|
|
11
|
+
|
|
3
12
|
## 5.0.1
|
|
4
13
|
|
|
5
14
|
### Changes
|
package/README.md
CHANGED
|
@@ -211,10 +211,15 @@ hygiene already drops temp leftovers, artifacts whose Pi process is gone, result
|
|
|
211
211
|
files past their retention window, and project roots idle for three days. Parked
|
|
212
212
|
recovery work and anything a manifest still references always survive.
|
|
213
213
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
214
|
+
Reclamation is automatic: a settlement may trigger one paced sweep (at most every
|
|
215
|
+
five minutes) that drops dead-owner artifacts, expired result files, and settled
|
|
216
|
+
session copies older than 30 minutes, then trims the oldest settled sessions
|
|
217
|
+
until a project fits its 1.5 GB budget. Live runs, parked recovery work, recent
|
|
218
|
+
sessions, and anything a manifest references are never touched, so rework within
|
|
219
|
+
the last half hour can still continue its session.
|
|
220
|
+
|
|
221
|
+
`/subagents-cleanup` runs the same pass immediately and reports per-project usage
|
|
222
|
+
and freed bytes; `/subagents-cleanup dry-run` measures without deleting anything.
|
|
218
223
|
|
|
219
224
|
## Development
|
|
220
225
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ferris1225/pi-subagents",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.2",
|
|
4
4
|
"description": "A managed sub-agent team for pi: research, implementation, independent review, interactive follow-ups, and Git worktree isolation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/isolation/cleanup.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { existsSync, type Dirent, readdirSync, rmSync, statSync } from "node:fs"
|
|
|
13
13
|
import { join } from "node:path";
|
|
14
14
|
import { getSubagentsRoot, sweepProjectResultArtifacts } from "../execution/spawn.ts";
|
|
15
15
|
import { pruneStaleProjectRoots, readReferencedDurablePaths, readThreadRecords } from "../lifecycle/durable.ts";
|
|
16
|
+
import type { SubagentRuntime } from "../lifecycle/runtime.ts";
|
|
16
17
|
import { isProcessAlive, sweepProjectDurableDirs, sweepProjectTempDirs } from "./temp-hygiene.ts";
|
|
17
18
|
|
|
18
19
|
/** Bytes a directory tree occupies, without following symlinks. */
|
|
@@ -172,3 +173,160 @@ export function formatBytes(bytes: number): string {
|
|
|
172
173
|
}
|
|
173
174
|
return value.toFixed(value >= 10 ? 0 : 1) + " " + units[unit];
|
|
174
175
|
}
|
|
176
|
+
|
|
177
|
+
/** Sessions of settled runs are kept only so a follow-up can resume them. After
|
|
178
|
+
* this window they are pure disk cost: `subagent_message` with
|
|
179
|
+
* `context: "summary"` still works, and rework usually starts long before it. */
|
|
180
|
+
export const SETTLED_SESSION_MAX_AGE_MS = 30 * 60 * 1_000;
|
|
181
|
+
|
|
182
|
+
/** Recent sessions are never reclaimed regardless of budget: a follow-up can
|
|
183
|
+
* legitimately arrive seconds after settlement. */
|
|
184
|
+
const RECENT_SESSION_FLOOR_MS = 5 * 60 * 1_000;
|
|
185
|
+
|
|
186
|
+
/** Automatic passes are paced by settlement, so a burst of finished runs costs
|
|
187
|
+
* one sweep instead of one per run. */
|
|
188
|
+
const AUTO_CLEANUP_INTERVAL_MS = 5 * 60 * 1_000;
|
|
189
|
+
|
|
190
|
+
/** Upper bound for one project's artifacts; the oldest settled sessions go
|
|
191
|
+
* first until the project fits. */
|
|
192
|
+
export const PROJECT_BUDGET_BYTES = 1.5 * 1024 * 1024 * 1024;
|
|
193
|
+
|
|
194
|
+
let lastAutomaticCleanupAt = 0;
|
|
195
|
+
|
|
196
|
+
export interface AutomaticCleanupOptions {
|
|
197
|
+
now?: number;
|
|
198
|
+
/** Retained-session age before automatic reclamation. */
|
|
199
|
+
maxSessionAgeMs?: number;
|
|
200
|
+
/** Per-project artifact budget. */
|
|
201
|
+
budgetBytes?: number;
|
|
202
|
+
/** Ignore the pacing clock (tests, or an explicit pass). */
|
|
203
|
+
force?: boolean;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
interface SettledSession {
|
|
207
|
+
path: string;
|
|
208
|
+
mtimeMs: number;
|
|
209
|
+
bytes: number;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function settledSessions(projectRoot: string): SettledSession[] {
|
|
213
|
+
const dir = join(projectRoot, "sessions");
|
|
214
|
+
let entries: Dirent[];
|
|
215
|
+
try {
|
|
216
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
217
|
+
} catch {
|
|
218
|
+
return [];
|
|
219
|
+
}
|
|
220
|
+
return entries
|
|
221
|
+
.filter((entry) => entry.isDirectory() && !entry.isSymbolicLink() && entry.name.startsWith("pi-subagent-session-"))
|
|
222
|
+
.flatMap((entry) => {
|
|
223
|
+
const path = join(dir, entry.name);
|
|
224
|
+
try {
|
|
225
|
+
return [{ path, mtimeMs: statSync(path).mtimeMs, bytes: directoryBytes(path) }];
|
|
226
|
+
} catch {
|
|
227
|
+
return [];
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Delete settled-session copies that nothing references: not a parked record,
|
|
233
|
+
* not a live or resumed thread, and older than the retention window. */
|
|
234
|
+
function reclaimSettledSessions(
|
|
235
|
+
projectRoot: string,
|
|
236
|
+
protectedPaths: readonly string[],
|
|
237
|
+
now: number,
|
|
238
|
+
maxAgeMs: number,
|
|
239
|
+
): number {
|
|
240
|
+
let freed = 0;
|
|
241
|
+
for (const session of settledSessions(projectRoot)) {
|
|
242
|
+
if (now - session.mtimeMs < Math.max(maxAgeMs, RECENT_SESSION_FLOOR_MS)) continue;
|
|
243
|
+
if (protectedPaths.some((kept) => kept.startsWith(session.path) || session.path.startsWith(kept))) continue;
|
|
244
|
+
try {
|
|
245
|
+
rmSync(session.path, { recursive: true, force: true });
|
|
246
|
+
freed += session.bytes;
|
|
247
|
+
} catch {
|
|
248
|
+
/* a locked directory waits for the next pass */
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return freed;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Delete the oldest settled sessions until the project fits its budget. */
|
|
255
|
+
function enforceProjectBudget(
|
|
256
|
+
projectRoot: string,
|
|
257
|
+
protectedPaths: readonly string[],
|
|
258
|
+
now: number,
|
|
259
|
+
budgetBytes: number,
|
|
260
|
+
): number {
|
|
261
|
+
let used = directoryBytes(projectRoot);
|
|
262
|
+
if (used <= budgetBytes) return 0;
|
|
263
|
+
let freed = 0;
|
|
264
|
+
for (const session of settledSessions(projectRoot).sort((left, right) => left.mtimeMs - right.mtimeMs)) {
|
|
265
|
+
if (used - freed <= budgetBytes) break;
|
|
266
|
+
if (now - session.mtimeMs < RECENT_SESSION_FLOOR_MS) continue;
|
|
267
|
+
if (protectedPaths.some((kept) => kept.startsWith(session.path) || session.path.startsWith(kept))) continue;
|
|
268
|
+
try {
|
|
269
|
+
rmSync(session.path, { recursive: true, force: true });
|
|
270
|
+
freed += session.bytes;
|
|
271
|
+
} catch {
|
|
272
|
+
/* best-effort */
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return freed;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function protectedSessionPaths(runtime: Pick<SubagentRuntime, "threads">, claimed: ReadonlySet<string>): string[] {
|
|
279
|
+
const protectedPaths = [...claimed];
|
|
280
|
+
for (const thread of runtime.threads.values()) {
|
|
281
|
+
// A parked or unfinished thread keeps its session for recovery; a live
|
|
282
|
+
// thread may be resumed by a follow-up that is already running.
|
|
283
|
+
if (thread.state === "queued" || thread.state === "running" || thread.state === "interrupting" || thread.state === "parked") {
|
|
284
|
+
if (thread.sessionDir) protectedPaths.push(thread.sessionDir);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (thread.lifecycleOperation !== undefined && thread.sessionDir) protectedPaths.push(thread.sessionDir);
|
|
288
|
+
}
|
|
289
|
+
return protectedPaths;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Reclaim what nothing references, then keep each project inside its budget.
|
|
293
|
+
* Safe to call at any time: live runs, parked recovery work and recently
|
|
294
|
+
* settled sessions are all protected. */
|
|
295
|
+
export async function runAutomaticCleanup(
|
|
296
|
+
runtime: Pick<SubagentRuntime, "configPath" | "threads">,
|
|
297
|
+
options: AutomaticCleanupOptions = {},
|
|
298
|
+
): Promise<{ freedBytes: number; skipped: boolean }> {
|
|
299
|
+
const now = options.now ?? Date.now();
|
|
300
|
+
if (!options.force && now - lastAutomaticCleanupAt < AUTO_CLEANUP_INTERVAL_MS) return { freedBytes: 0, skipped: true };
|
|
301
|
+
lastAutomaticCleanupAt = now;
|
|
302
|
+
const root = getSubagentsRoot(runtime.configPath);
|
|
303
|
+
const claimed = await readReferencedDurablePaths(runtime.configPath).catch(() => new Set<string>());
|
|
304
|
+
const records = await readThreadRecords(runtime.configPath).catch(() => []);
|
|
305
|
+
for (const record of records) if (record.sessionDir) claimed.add(record.sessionDir);
|
|
306
|
+
const protectedPaths = protectedSessionPaths(runtime, claimed);
|
|
307
|
+
const keep = (path: string) => protectedPaths.some((kept) => path.startsWith(kept) || kept.startsWith(path));
|
|
308
|
+
const before = measureDurableUsage(root).totalBytes;
|
|
309
|
+
|
|
310
|
+
sweepProjectTempDirs(root);
|
|
311
|
+
sweepProjectDurableDirs(root, { keep, isAlive: isProcessAlive });
|
|
312
|
+
sweepProjectResultArtifacts(root);
|
|
313
|
+
let projects: Dirent[] = [];
|
|
314
|
+
try {
|
|
315
|
+
projects = readdirSync(root, { withFileTypes: true });
|
|
316
|
+
} catch {
|
|
317
|
+
projects = [];
|
|
318
|
+
}
|
|
319
|
+
for (const project of projects) {
|
|
320
|
+
if (!project.isDirectory() || project.isSymbolicLink()) continue;
|
|
321
|
+
const path = join(root, project.name);
|
|
322
|
+
reclaimSettledSessions(path, protectedPaths, now, options.maxSessionAgeMs ?? SETTLED_SESSION_MAX_AGE_MS);
|
|
323
|
+
enforceProjectBudget(path, protectedPaths, now, options.budgetBytes ?? PROJECT_BUDGET_BYTES);
|
|
324
|
+
}
|
|
325
|
+
const after = measureDurableUsage(root).totalBytes;
|
|
326
|
+
return { freedBytes: Math.max(0, before - after), skipped: false };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Reset the pacing clock so the next settlement performs a real pass. */
|
|
330
|
+
export function resetAutomaticCleanupClock(): void {
|
|
331
|
+
lastAutomaticCleanupAt = 0;
|
|
332
|
+
}
|
|
@@ -14,6 +14,7 @@ import { formatTaskSummary, formatToolActivity, monitor } from "../presentation/
|
|
|
14
14
|
import type { StartBackgroundInternal, DispatchEnvironment } from "./thread-shared.ts";
|
|
15
15
|
import { persistThreadCheckpoint } from "./thread-shared.ts";
|
|
16
16
|
import type { SubagentRuntime } from "./runtime.ts";
|
|
17
|
+
import { runAutomaticCleanup } from "../isolation/cleanup.ts";
|
|
17
18
|
|
|
18
19
|
export interface DispatchCore {
|
|
19
20
|
/** Publish the context/config/catalog a dispatch needs; every fresh call refreshes it. */
|
|
@@ -43,6 +44,9 @@ export function createDispatchCore(runtime: SubagentRuntime): DispatchCore {
|
|
|
43
44
|
const endedAt = monitor.findRun(runId)?.endedAt;
|
|
44
45
|
if (endedAt !== undefined && publishedEndedAt.get(runId) === endedAt) return;
|
|
45
46
|
if (endedAt !== undefined) publishedEndedAt.set(runId, endedAt);
|
|
47
|
+
// Settlement is the natural moment to reclaim disk: a burst of runs costs
|
|
48
|
+
// one paced sweep, and every live or parked thread stays protected.
|
|
49
|
+
void runAutomaticCleanup(runtime).catch(() => undefined);
|
|
46
50
|
if (opts?.silent || !runtime.sessionActive) return;
|
|
47
51
|
const icon = status === "done" ? "✓" : "✗";
|
|
48
52
|
const result = runtime.threads.get(runId)?.lastResult;
|