@saadnvd1/agent-os 0.3.660 → 0.3.664

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 (76) hide show
  1. package/app/api/sessions/[id]/route.ts +3 -3
  2. package/app/api/sessions/[id]/summarize/route.ts +3 -2
  3. package/app/api/worktree-sweep/route.test.ts +19 -1
  4. package/app/api/worktree-sweep/route.ts +25 -6
  5. package/components/Health/SweepRuns.tsx +76 -1
  6. package/data/worktree-sweep/index.ts +15 -0
  7. package/lib/agentos-home.ts +11 -0
  8. package/lib/agents/launch.ts +6 -9
  9. package/lib/agents/server-env.ts +24 -0
  10. package/lib/artifacts/store.ts +2 -5
  11. package/lib/chat/drivers/claude.ts +3 -2
  12. package/lib/chat/drivers/codex.ts +3 -2
  13. package/lib/chat/drivers/copilot.ts +2 -1
  14. package/lib/chat/drivers/opencode.ts +3 -2
  15. package/lib/chat/drivers/pi-approvals.ts +2 -2
  16. package/lib/chat/drivers/pi.ts +3 -2
  17. package/lib/chat/worker/client.ts +18 -13
  18. package/lib/chat/worker/protocol.ts +2 -2
  19. package/lib/connect/config.ts +2 -3
  20. package/lib/db/backup.ts +2 -2
  21. package/lib/db/migrations/20261018T1600_health_boot_deploys.ts +13 -0
  22. package/lib/dev-instance/processInfo.ts +48 -27
  23. package/lib/dev-instance/startDevServer.ts +1 -1
  24. package/lib/dev-instance/stopDevServer.ts +20 -17
  25. package/lib/dev-servers.ts +2 -1
  26. package/lib/done/index.ts +2 -2
  27. package/lib/health/boots.ts +37 -0
  28. package/lib/health/ci.ts +23 -2
  29. package/lib/health/detectors.ts +2 -1
  30. package/lib/health/evidence.ts +1 -1
  31. package/lib/health/facts.ts +3 -3
  32. package/lib/health/fixtures.ts +1 -0
  33. package/lib/health/tick.ts +6 -1
  34. package/lib/health/types.ts +2 -0
  35. package/lib/hosts/attach.ts +8 -1
  36. package/lib/orchestration.ts +3 -1
  37. package/lib/orchestrator/home.ts +2 -5
  38. package/lib/orchestrator/shell-guard.ts +2 -0
  39. package/lib/orchestrator/write-guard.ts +3 -2
  40. package/lib/plans/features.ts +2 -2
  41. package/lib/pr-generation.ts +2 -1
  42. package/lib/program-status/claude-hooks.ts +2 -2
  43. package/lib/program-status/tap.ts +2 -2
  44. package/lib/project-config/database.ts +71 -16
  45. package/lib/project-config/session.ts +18 -0
  46. package/lib/security/tailnet-https.ts +2 -2
  47. package/lib/session-artifacts/collect.ts +18 -1
  48. package/lib/session-artifacts/keep.ts +2 -4
  49. package/lib/session-audit/store.ts +2 -5
  50. package/lib/session-logs/folder.ts +2 -3
  51. package/lib/sessions/launch.ts +2 -2
  52. package/lib/sessions/leftovers.ts +61 -0
  53. package/lib/simulator/agent-device.ts +2 -2
  54. package/lib/tasks/finish.ts +2 -2
  55. package/lib/tasks/import.ts +3 -5
  56. package/lib/tasks/move.ts +2 -2
  57. package/lib/tasks/moved-away.ts +2 -0
  58. package/lib/tasks/state.ts +7 -3
  59. package/lib/tasks/ui-brief.ts +2 -2
  60. package/lib/tmux/exec.ts +3 -1
  61. package/lib/worktree-deps/fastcopy.ts +2 -2
  62. package/lib/worktree-deps/spare.ts +2 -2
  63. package/lib/worktree-sweep/caches.ts +123 -0
  64. package/lib/worktree-sweep/databases.ts +222 -0
  65. package/lib/worktree-sweep/du.ts +13 -0
  66. package/lib/worktree-sweep/extras.ts +188 -0
  67. package/lib/worktree-sweep/findOrphans.ts +19 -8
  68. package/lib/worktree-sweep/report.ts +85 -0
  69. package/lib/worktree-sweep/runSweep.ts +46 -13
  70. package/lib/worktree-sweep/settings.ts +24 -0
  71. package/lib/worktree-sweep/state.ts +5 -0
  72. package/lib/worktree-sweep/store.ts +11 -0
  73. package/lib/worktree-sweep/types.ts +44 -1
  74. package/lib/worktrees.ts +3 -0
  75. package/package.json +2 -1
  76. package/scripts/disk-dry-run.ts +57 -0
@@ -6,7 +6,6 @@
6
6
 
7
7
  import fs from "fs";
8
8
  import path from "path";
9
- import { execFile } from "child_process";
10
9
  import { db, type Session } from "../db";
11
10
  import { git as runGit } from "../git-exec";
12
11
  import { run as runCmd } from "../tasks/gh";
@@ -19,10 +18,14 @@ import {
19
18
  type SettledDeps,
20
19
  } from "./findSettled";
21
20
  import { findOrphans, holdsNothing } from "./findOrphans";
21
+ import duBytes from "./du";
22
+ import { findExtras, defaultExtras, type Extras } from "./extras";
23
+ import { reaperArmed } from "./settings";
24
+ import { writeDiskReport } from "./report";
22
25
  import { judgeWorktree } from "./judge";
23
26
  import { openPaneFolders } from "./panes";
24
27
  import { finishRun, recordSwept, startRun } from "./store";
25
- import type { Candidate, SweepSummary } from "./types";
28
+ import { ARMED_KINDS, type Candidate, type SweepSummary } from "./types";
26
29
 
27
30
  export interface SweepDeps {
28
31
  now: number;
@@ -33,19 +36,15 @@ export interface SweepDeps {
33
36
  prHead: (repo: string, number: number) => Promise<string | null>;
34
37
  // Stop early when the machine is busy.
35
38
  busy: () => boolean;
39
+ // Task databases, spares, temp folders and build caches.
40
+ extras: (opts?: { fresh?: boolean }) => Promise<Extras>;
41
+ // Written by a dry run: where its list goes.
42
+ report: (summary: SweepSummary) => string | null;
36
43
  }
37
44
 
38
45
  const git = (cwd: string, ...args: string[]) =>
39
46
  runGit(cwd, args, { timeout: 30_000 }).then((o) => o.trim());
40
47
 
41
- function duBytes(dir: string): Promise<number> {
42
- return new Promise((resolve) =>
43
- execFile("du", ["-sk", dir], { timeout: 120_000 }, (error, stdout) =>
44
- resolve(error ? 0 : (parseInt(String(stdout), 10) || 0) * 1024)
45
- )
46
- );
47
- }
48
-
49
48
  async function prHeadOf(repo: string, number: number): Promise<string | null> {
50
49
  try {
51
50
  const out = await runCmd(
@@ -73,9 +72,18 @@ export const defaultSweepDeps = (): SweepDeps => ({
73
72
  root: WORKTREES_DIR,
74
73
  panes: openPaneFolders,
75
74
  settled: defaultSettledDeps(),
76
- size: duBytes,
75
+ size: (dir) => duBytes(dir, 120_000),
77
76
  prHead: prHeadOf,
78
77
  busy: () => currentLevel() === "red",
78
+ extras: defaultExtras,
79
+ report: (summary) => {
80
+ try {
81
+ return writeDiskReport(summary);
82
+ } catch (error) {
83
+ console.error("[worktree-sweep] report not written:", error);
84
+ return null;
85
+ }
86
+ },
79
87
  });
80
88
 
81
89
  // A path under the root, never the root itself or something outside it.
@@ -123,7 +131,14 @@ export async function sweep(
123
131
  ): Promise<{ runId: number; summary: SweepSummary }> {
124
132
  const dryRun = !!opts.dryRun;
125
133
  const runId = startRun(dryRun);
126
- const summary: SweepSummary = { removed: [], skipped: [], freedBytes: 0 };
134
+ const armed = reaperArmed();
135
+ const summary: SweepSummary = {
136
+ removed: [],
137
+ skipped: [],
138
+ freedBytes: 0,
139
+ pending: [],
140
+ armed,
141
+ };
127
142
  let error: string | null = null;
128
143
  try {
129
144
  const panes = await deps.panes();
@@ -155,6 +170,18 @@ export async function sweep(
155
170
  reason,
156
171
  sessionId: c.sessionId,
157
172
  });
173
+ if (ARMED_KINDS.has(c.kind) && !armed) {
174
+ summary.pending!.push({
175
+ path: c.path,
176
+ kind: c.kind,
177
+ why:
178
+ c.kind === "test-leak"
179
+ ? "a test's throwaway repository"
180
+ : "an orphan worktree",
181
+ bytes: await deps.size(c.path),
182
+ });
183
+ continue;
184
+ }
158
185
  const repo = await repoOfSession(c);
159
186
  const why =
160
187
  c.kind === "test-leak"
@@ -203,9 +230,12 @@ export async function sweep(
203
230
  summary.freedBytes += bytes;
204
231
  }
205
232
  if (!dryRun) await pruneProjects([...pruned]);
233
+ const extras = await findExtras(deps, summary, { armed, dryRun });
234
+ summary.totals = extras.totals;
206
235
  } catch (e) {
207
236
  error = e instanceof Error ? e.message : String(e);
208
237
  }
238
+ if (dryRun) summary.report = deps.report(summary) ?? undefined;
209
239
  finishRun(runId, summary, error);
210
240
  console.log(describeRun(summary, dryRun, error));
211
241
  return { runId, summary };
@@ -239,5 +269,8 @@ export function describeRun(
239
269
  }
240
270
  const why = [...reasons].map(([r, n]) => `${n} ${r}`).join("; ");
241
271
  const leaks = s.removed.filter((r) => r.kind === "test-leak").length;
242
- return `[worktree-sweep] ${dryRun ? "would remove" : "removed"} ${s.removed.length} (${gb(s.freedBytes)}${leaks ? `, ${leaks} test leftovers` : ""}), skipped ${s.skipped.length}${why ? `: ${why}` : ""}${error ? `; failed: ${error}` : ""}`;
272
+ const pending = s.pending?.length
273
+ ? `, ${s.pending.length} more (${gb(s.pending.reduce((n, p) => n + p.bytes, 0))}) wait for the full clean-up to be turned on`
274
+ : "";
275
+ return `[worktree-sweep] ${dryRun ? "would remove" : "removed"} ${s.removed.length} (${gb(s.freedBytes)}${leaks ? `, ${leaks} test leftovers` : ""})${pending}, skipped ${s.skipped.length}${why ? `: ${why}` : ""}${error ? `; failed: ${error}` : ""}`;
243
276
  }
@@ -81,3 +81,27 @@ export function projectSweepOverrides(): Array<{
81
81
  return days === null ? [] : [{ projectId, days }];
82
82
  });
83
83
  }
84
+
85
+ // The full clean-up (test leftovers, task databases, spares, temp folders,
86
+ // build caches) removes nothing until a person turns it on for this machine,
87
+ // after reading what a dry run found.
88
+ const ARMED_KEY = "worktree_sweep.reaper";
89
+
90
+ export function reaperArmed(): boolean {
91
+ const row = db
92
+ .prepare(`SELECT value FROM settings WHERE key = ?`)
93
+ .get(ARMED_KEY) as { value: string } | undefined;
94
+ if (!row) return false;
95
+ try {
96
+ return JSON.parse(row.value)?.armed === true;
97
+ } catch {
98
+ return false;
99
+ }
100
+ }
101
+
102
+ export function setReaperArmed(armed: boolean): void {
103
+ db.prepare(
104
+ `INSERT INTO settings (key, value) VALUES (?, ?)
105
+ ON CONFLICT(key) DO UPDATE SET value = excluded.value`
106
+ ).run(ARMED_KEY, JSON.stringify({ armed, at: new Date().toISOString() }));
107
+ }
@@ -3,6 +3,7 @@ import {
3
3
  DEFAULT_SWEEP_DAYS,
4
4
  globalSweepDays,
5
5
  projectSweepOverrides,
6
+ reaperArmed,
6
7
  } from "./settings";
7
8
  import { recentRuns } from "./store";
8
9
  import type { SweepRun } from "./types";
@@ -13,6 +14,9 @@ export interface SweepState {
13
14
  defaultDays: number;
14
15
  overrides: Array<{ projectId: string; name: string; days: number }>;
15
16
  runs: SweepRun[];
17
+ // Whether the full clean-up (test leftovers, task databases, spares, temp
18
+ // folders, build caches) removes anything on this machine.
19
+ armed: boolean;
16
20
  }
17
21
 
18
22
  export function sweepState(): SweepState {
@@ -32,5 +36,6 @@ export function sweepState(): SweepState {
32
36
  : []
33
37
  ),
34
38
  runs: recentRuns(),
39
+ armed: reaperArmed(),
35
40
  };
36
41
  }
@@ -51,6 +51,17 @@ export function finishRun(
51
51
  summary.skipped.length,
52
52
  JSON.stringify({
53
53
  ...summary,
54
+ // The biggest first: they are what a person looks for.
55
+ removed: [...summary.removed]
56
+ .sort((a, b) => b.bytes - a.bytes)
57
+ .slice(0, DETAIL_CAP),
58
+ pending: [...(summary.pending ?? [])]
59
+ .sort((a, b) => b.bytes - a.bytes)
60
+ .slice(0, DETAIL_CAP),
61
+ pendingTotal: {
62
+ count: summary.pending?.length ?? 0,
63
+ bytes: (summary.pending ?? []).reduce((n, p) => n + p.bytes, 0),
64
+ },
54
65
  skipped: summary.skipped.slice(0, DETAIL_CAP),
55
66
  }),
56
67
  error,
@@ -1,4 +1,21 @@
1
- export type SweepKind = "settled" | "orphan" | "test-leak";
1
+ export type SweepKind =
2
+ | "settled"
3
+ | "orphan"
4
+ | "test-leak"
5
+ // What a task left beside its checkout, found by the full clean-up.
6
+ | "database"
7
+ | "spare"
8
+ | "temp"
9
+ | "derived-data";
10
+
11
+ // Kinds that go only once the full clean-up is turned on.
12
+ export const ARMED_KINDS: ReadonlySet<SweepKind> = new Set([
13
+ "test-leak",
14
+ "database",
15
+ "spare",
16
+ "temp",
17
+ "derived-data",
18
+ ]);
2
19
 
3
20
  export interface SweepRemoved {
4
21
  path: string;
@@ -15,10 +32,31 @@ export interface SweepSkipped {
15
32
  sessionId?: string;
16
33
  }
17
34
 
35
+ export interface CategoryTotal {
36
+ count: number;
37
+ bytes: number;
38
+ }
39
+
40
+ // What is on disk after the run, for the one-line summary.
41
+ export interface SweepTotals {
42
+ worktrees: CategoryTotal;
43
+ databases: CategoryTotal;
44
+ spares: CategoryTotal;
45
+ temp: CategoryTotal;
46
+ }
47
+
18
48
  export interface SweepSummary {
19
49
  removed: SweepRemoved[];
20
50
  skipped: SweepSkipped[];
21
51
  freedBytes: number;
52
+ // Found but left, because the full clean-up isn't turned on.
53
+ pending?: SweepRemoved[];
54
+ // The whole of pending, once a stored run keeps only its biggest.
55
+ pendingTotal?: CategoryTotal;
56
+ totals?: SweepTotals;
57
+ armed?: boolean;
58
+ // Where a dry run wrote its list.
59
+ report?: string;
22
60
  }
23
61
 
24
62
  export interface SweepRun {
@@ -50,4 +88,9 @@ export interface Candidate {
50
88
  // The PR head a merge took, when the session has one.
51
89
  prNumber?: number | null;
52
90
  branch?: string | null;
91
+ // A database: the server it lives on, and why it's reapable.
92
+ server?: { host?: string; port?: number };
93
+ why?: string;
94
+ // Known without measuring (a database's size).
95
+ bytes?: number;
53
96
  }
package/lib/worktrees.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  * Git Worktree management for isolated feature development
3
3
  */
4
4
 
5
+ import { stopDevServer } from "./dev-instance/stopDevServer";
5
6
  import * as path from "path";
6
7
  import * as fs from "fs";
7
8
  import * as os from "os";
@@ -301,6 +302,8 @@ export function deleteWorktree(
301
302
  let marker: string | null = null;
302
303
  const done = (async () => {
303
304
  await branch;
305
+ // A dev server left running would outlive the folder it runs in.
306
+ await stopDevServer(key).catch(() => {});
304
307
  // Before anything is removed, so no reader catches it half-deleted.
305
308
  marker = markRemoving(key);
306
309
  await removeWorktree(key, repo);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saadnvd1/agent-os",
3
- "version": "0.3.660",
3
+ "version": "0.3.664",
4
4
  "description": "Self-hosted web UI for managing Claude Code sessions",
5
5
  "bin": {
6
6
  "agent-os": "scripts/agent-os"
@@ -33,6 +33,7 @@
33
33
  "dev": "AGENTOS_DEV=1 tsx server.ts",
34
34
  "dev:task": "tsx scripts/dev-task.ts",
35
35
  "dev:stop": "tsx scripts/dev-stop.ts",
36
+ "disk:dry-run": "tsx scripts/disk-dry-run.ts",
36
37
  "prebuild": "node scripts/gen-migrations-index.mjs",
37
38
  "build": "next build",
38
39
  "prestart": "node scripts/ensure-native.mjs && node scripts/gen-migrations-index.mjs",
@@ -0,0 +1,57 @@
1
+ /**
2
+ * What the disk clean-up would remove, without removing anything: runs a dry
3
+ * sweep against a copy of the live database and writes the list to
4
+ * ~/.agent-os/audits/disk-cleanup/<date>.md.
5
+ *
6
+ * npm run disk:dry-run -- --db <path to the live agent-os.db>
7
+ *
8
+ * The live database is opened read-only for the copy and never written; the
9
+ * copy is deleted afterwards. Nothing on disk or in Postgres is touched.
10
+ */
11
+
12
+ import fs from "fs";
13
+ import os from "os";
14
+ import path from "path";
15
+ import Database from "better-sqlite3";
16
+
17
+ const arg = process.argv.indexOf("--db");
18
+ const live: string = (arg > 0 && process.argv[arg + 1]) || "";
19
+ if (!live || !fs.existsSync(live)) {
20
+ console.error("usage: npm run disk:dry-run -- --db <path to agent-os.db>");
21
+ process.exit(1);
22
+ }
23
+
24
+ async function main() {
25
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "aos-disk-dry-"));
26
+ const copy = path.join(dir, "agent-os.db");
27
+ try {
28
+ const src = new Database(live, { readonly: true });
29
+ await src.backup(copy);
30
+ src.close();
31
+ process.env.DB_PATH = copy;
32
+
33
+ const { sweep, defaultSweepDeps } =
34
+ await import("../lib/worktree-sweep/runSweep");
35
+ const { summary } = await sweep(
36
+ { dryRun: true },
37
+ { ...defaultSweepDeps(), busy: () => false }
38
+ );
39
+ const all = [...summary.removed, ...(summary.pending ?? [])];
40
+ const gb = (b: number) => `${(b / 1024 ** 3).toFixed(2)} GB`;
41
+ for (const kind of new Set(all.map((i) => i.kind))) {
42
+ const items = all.filter((i) => i.kind === kind);
43
+ console.log(
44
+ `${kind}: ${items.length} (${gb(items.reduce((n, i) => n + i.bytes, 0))})`
45
+ );
46
+ }
47
+ console.log(`kept: ${summary.skipped.length}`);
48
+ console.log(`list: ${summary.report ?? "(not written)"}`);
49
+ } finally {
50
+ fs.rmSync(dir, { recursive: true, force: true });
51
+ }
52
+ }
53
+
54
+ main().catch((error) => {
55
+ console.error(error);
56
+ process.exit(1);
57
+ });