@saadnvd1/agent-os 0.3.662 → 0.3.671
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/app/api/audits/[kind]/[id]/route.test.ts +2 -2
- package/app/api/mcp-auth/callback/route.test.ts +42 -0
- package/app/api/mcp-auth/callback/route.ts +56 -0
- package/app/api/projects/init/route.test.ts +2 -4
- package/app/api/sessions/[id]/mcp-auth/route.test.ts +86 -0
- package/app/api/sessions/[id]/mcp-auth/route.ts +58 -0
- package/app/api/sessions/[id]/route.ts +3 -3
- package/app/api/shares/route.test.ts +3 -4
- package/app/api/workspaces/[id]/route.ts +9 -1
- package/app/api/worktree-sweep/route.test.ts +40 -1
- package/app/api/worktree-sweep/route.ts +45 -6
- package/components/Chat/ChatPanel.tsx +6 -3
- package/components/Chat/McpServers.tsx +61 -24
- package/components/Chat/McpSignIn.tsx +122 -0
- package/components/Chat/session.ts +8 -0
- package/components/Health/SweepRuns.tsx +77 -1
- package/components/Workspaces/WorkspaceSettings.tsx +6 -0
- package/data/chat/mcpAuth.ts +42 -0
- package/data/workspaces/index.ts +6 -0
- package/data/worktree-sweep/index.ts +15 -0
- package/lib/agentos-home.ts +11 -0
- package/lib/agents/launch.ts +3 -7
- package/lib/artifacts/store.ts +2 -5
- package/lib/chat/driver.ts +24 -2
- package/lib/chat/drivers/claude-approvals.ts +7 -0
- package/lib/chat/drivers/claude-mcp-auth.ts +82 -0
- package/lib/chat/drivers/claude.ts +7 -0
- package/lib/chat/drivers/pi-approvals.ts +2 -2
- package/lib/chat/events.ts +12 -0
- package/lib/chat/mcp-auth.ts +193 -0
- package/lib/chat/registry.ts +2 -0
- package/lib/chat/runner.ts +18 -0
- package/lib/chat/worker/client.ts +35 -1
- package/lib/chat/worker/host.ts +9 -0
- package/lib/chat/worker/main.ts +1 -1
- package/lib/chat/worker/mcp-auth.ts +119 -0
- package/lib/chat/worker/protocol.ts +21 -2
- package/lib/connect/config.ts +2 -3
- package/lib/db/backup.ts +2 -2
- package/lib/db/migrations/20261018T1600_health_boot_deploys.ts +13 -0
- package/lib/db/migrations/20261018T1730_mcp_sign_ins.ts +22 -0
- package/lib/db/migrations/20261018T2100_orch_full_access.ts +16 -0
- package/lib/db/types.ts +3 -0
- package/lib/dev-instance/processInfo.ts +48 -27
- package/lib/dev-instance/startDevServer.ts +1 -1
- package/lib/dev-instance/stopDevServer.ts +20 -17
- package/lib/dev-servers.ts +2 -1
- package/lib/done/index.ts +2 -2
- package/lib/done/testing.ts +2 -2
- package/lib/env-setup.ts +8 -1
- package/lib/health/boots.ts +31 -0
- package/lib/health/ci.ts +23 -2
- package/lib/health/detectors.ts +2 -1
- package/lib/health/evidence.ts +1 -1
- package/lib/health/facts.ts +3 -3
- package/lib/health/fixtures.ts +1 -0
- package/lib/health/tick.ts +6 -1
- package/lib/health/types.ts +2 -0
- package/lib/orchestrator/armed.test-helpers.ts +3 -2
- package/lib/orchestrator/brief.ts +14 -9
- package/lib/orchestrator/external-gates.ts +62 -1
- package/lib/orchestrator/full-access.ts +21 -0
- package/lib/orchestrator/home.ts +2 -5
- package/lib/orchestrator/quick.ts +47 -4
- package/lib/orchestrator/serve.ts +11 -2
- package/lib/orchestrator/shell-guard.ts +24 -0
- package/lib/orchestrator/signoff.test-helpers.ts +3 -2
- package/lib/orchestrator/sweep-arm.ts +180 -0
- package/lib/orchestrator/tool-names.ts +4 -1
- package/lib/orchestrator/tool-schemas.ts +33 -1
- package/lib/orchestrator/tools.ts +6 -2
- package/lib/orchestrator/write-guard.ts +37 -9
- package/lib/plans/features.ts +2 -2
- package/lib/program-status/claude-hooks.ts +2 -2
- package/lib/program-status/tap.ts +2 -2
- package/lib/project-config/brief.ts +3 -1
- package/lib/project-config/database.ts +77 -16
- package/lib/project-config/index.ts +108 -21
- package/lib/project-config/schema.ts +10 -4
- package/lib/project-config/session.ts +27 -6
- package/lib/security/net.ts +7 -4
- package/lib/security/tailnet-https.ts +2 -2
- package/lib/session-artifacts/collect.ts +18 -1
- package/lib/session-artifacts/keep.ts +2 -4
- package/lib/session-audit/store.ts +2 -5
- package/lib/session-logs/folder.ts +2 -3
- package/lib/sessions/launch.ts +2 -2
- package/lib/sessions/leftovers.ts +61 -0
- package/lib/simulator/agent-device.ts +2 -2
- package/lib/tasks/finish.ts +2 -2
- package/lib/tasks/import.ts +3 -5
- package/lib/tasks/move-testing.ts +2 -2
- package/lib/tasks/move.ts +2 -2
- package/lib/tasks/moved-away.ts +2 -0
- package/lib/tasks/state.ts +7 -3
- package/lib/tasks/ui-brief.ts +2 -2
- package/lib/test-temp.ts +30 -0
- package/lib/workspaces.ts +6 -2
- package/lib/worktree-deps/fastcopy.ts +2 -2
- package/lib/worktree-deps/spare.ts +2 -2
- package/lib/worktree-sweep/caches.ts +129 -0
- package/lib/worktree-sweep/databases.ts +222 -0
- package/lib/worktree-sweep/du.ts +13 -0
- package/lib/worktree-sweep/extras.ts +196 -0
- package/lib/worktree-sweep/findOrphans.ts +19 -8
- package/lib/worktree-sweep/report.ts +86 -0
- package/lib/worktree-sweep/runSweep.ts +46 -13
- package/lib/worktree-sweep/settings.ts +24 -0
- package/lib/worktree-sweep/state.ts +5 -0
- package/lib/worktree-sweep/store.ts +11 -0
- package/lib/worktree-sweep/testTemp.ts +113 -0
- package/lib/worktree-sweep/types.ts +46 -1
- package/lib/worktrees.ts +3 -0
- package/package.json +2 -1
- package/scripts/autodeploy.test.ts +2 -2
- package/scripts/check-bundle.test.ts +2 -2
- package/scripts/disk-dry-run.ts +57 -0
- package/scripts/hooks/banned-terms.test.ts +3 -2
- package/scripts/hooks/staged-secrets.test.ts +2 -2
- package/scripts/lib/build-swap.test.ts +2 -3
- package/scripts/lib/wsl.test.ts +2 -3
- package/scripts/update-run.test.ts +2 -2
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Task databases left behind: copies and what projects derived from them
|
|
3
|
+
* (`<copy>_test`), found by the `_aos_<12 hex>` token every session's name
|
|
4
|
+
* carries. A database goes only if its token belongs to no live session,
|
|
5
|
+
* nobody is connected, it isn't a project's declared source, and it is more
|
|
6
|
+
* than a day old. Anything without the token is never looked at.
|
|
7
|
+
*
|
|
8
|
+
* A session is over only when it is merged, dropped, done or moved away, or
|
|
9
|
+
* its row is gone: archiving and settling can be undone, and an interactive
|
|
10
|
+
* session has no end of its own, so neither keeps a database off the list.
|
|
11
|
+
*
|
|
12
|
+
* Proof of who made it: a copy must carry the `agentos session <id>` comment
|
|
13
|
+
* AgentOS gave it, and the id must hash to the token. Something a project
|
|
14
|
+
* derived from a copy (`<copy>_test`) has no such comment, so it also needs
|
|
15
|
+
* its token to belong to no session this machine has ever known, and the
|
|
16
|
+
* server to be local (a host that isn't localhost or a socket is never
|
|
17
|
+
* reaped on a name alone).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { db } from "../db";
|
|
21
|
+
import { loadProjectConfig } from "../project-config";
|
|
22
|
+
import { expandHome } from "../project-config/session";
|
|
23
|
+
import {
|
|
24
|
+
LOCAL_HOSTS,
|
|
25
|
+
MARK,
|
|
26
|
+
SOCKET_DIR,
|
|
27
|
+
pgEnv,
|
|
28
|
+
pgRun,
|
|
29
|
+
sessionHash,
|
|
30
|
+
taskHashOf,
|
|
31
|
+
type PgRun,
|
|
32
|
+
} from "../project-config/database";
|
|
33
|
+
import type { Candidate, SweepSkipped } from "./types";
|
|
34
|
+
|
|
35
|
+
const DAY_MS = 24 * 3600_000;
|
|
36
|
+
|
|
37
|
+
export interface PgServer {
|
|
38
|
+
host?: string;
|
|
39
|
+
port?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface DatabaseDeps {
|
|
43
|
+
now: number;
|
|
44
|
+
run: PgRun;
|
|
45
|
+
servers: PgServer[];
|
|
46
|
+
// Hashes of the sessions that still need their database.
|
|
47
|
+
live: Set<string>;
|
|
48
|
+
// Hashes of every session this machine has a row for, over or not.
|
|
49
|
+
known: Set<string>;
|
|
50
|
+
// Databases a project declares as its source, never reaped.
|
|
51
|
+
froms: Set<string>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const where = (s: PgServer) =>
|
|
55
|
+
`${s.host ?? "local server"}${s.port ? `:${s.port}` : ""}`;
|
|
56
|
+
|
|
57
|
+
// Sessions whose work isn't over: everything with a row except a task that
|
|
58
|
+
// merged, was dropped, finished or moved away.
|
|
59
|
+
export function liveSessionIds(): Set<string> {
|
|
60
|
+
const rows = db
|
|
61
|
+
.prepare(
|
|
62
|
+
`SELECT id FROM session_rows
|
|
63
|
+
WHERE task_status IS NULL
|
|
64
|
+
OR task_status NOT IN ('merged', 'dropped', 'done', 'moved')`
|
|
65
|
+
)
|
|
66
|
+
.all() as Array<{ id: string }>;
|
|
67
|
+
return new Set(rows.map((r) => r.id));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const knownSessionIds = (): Set<string> =>
|
|
71
|
+
new Set(
|
|
72
|
+
(
|
|
73
|
+
db.prepare(`SELECT id FROM session_rows`).all() as Array<{ id: string }>
|
|
74
|
+
).map((r) => r.id)
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
export const hashesOf = (ids: Iterable<string>): Set<string> =>
|
|
78
|
+
new Set([...ids].map(sessionHash));
|
|
79
|
+
|
|
80
|
+
export const liveHashes = (ids = liveSessionIds()): Set<string> =>
|
|
81
|
+
hashesOf(ids);
|
|
82
|
+
|
|
83
|
+
const isLocal = (s: PgServer) =>
|
|
84
|
+
!s.host || LOCAL_HOSTS.has(s.host) || SOCKET_DIR.test(s.host);
|
|
85
|
+
|
|
86
|
+
// Every project's declared database source and server.
|
|
87
|
+
export function declaredDatabases(): {
|
|
88
|
+
servers: PgServer[];
|
|
89
|
+
froms: Set<string>;
|
|
90
|
+
} {
|
|
91
|
+
const rows = db
|
|
92
|
+
.prepare(
|
|
93
|
+
`SELECT working_directory FROM projects WHERE working_directory IS NOT NULL`
|
|
94
|
+
)
|
|
95
|
+
.all() as Array<{ working_directory: string }>;
|
|
96
|
+
const servers = new Map<string, PgServer>([[":", {}]]);
|
|
97
|
+
const froms = new Set<string>();
|
|
98
|
+
for (const r of rows) {
|
|
99
|
+
try {
|
|
100
|
+
const d = loadProjectConfig(expandHome(r.working_directory)).config
|
|
101
|
+
.database;
|
|
102
|
+
if (!d) continue;
|
|
103
|
+
froms.add(d.from);
|
|
104
|
+
servers.set(`${d.host ?? ""}:${d.port ?? ""}`, {
|
|
105
|
+
host: d.host,
|
|
106
|
+
port: d.port,
|
|
107
|
+
});
|
|
108
|
+
} catch {
|
|
109
|
+
// A project whose config can't be read declares nothing.
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return { servers: [...servers.values()], froms };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const LIST = (age: boolean) =>
|
|
116
|
+
`select d.datname, pg_database_size(d.oid), coalesce(shobj_description(d.oid, 'pg_database'), ''),
|
|
117
|
+
(select count(*) from pg_stat_activity a where a.datname = d.datname),
|
|
118
|
+
${age ? `extract(epoch from (pg_stat_file('base/' || d.oid || '/PG_VERSION')).modification)::bigint` : "0"}
|
|
119
|
+
from pg_database d where d.datname ~ '_aos_[0-9a-f]{12}'`;
|
|
120
|
+
|
|
121
|
+
// Why this database can't be shown to be a session's, or null when it can.
|
|
122
|
+
function whoMade(
|
|
123
|
+
hash: string,
|
|
124
|
+
note: string,
|
|
125
|
+
known: Set<string>,
|
|
126
|
+
server: PgServer
|
|
127
|
+
): string | null {
|
|
128
|
+
const m = /^agentos session (\S+?)(?: \(copying\))?$/.exec(note);
|
|
129
|
+
if (m)
|
|
130
|
+
return sessionHash(m[1]) === hash
|
|
131
|
+
? null
|
|
132
|
+
: "its comment names another session";
|
|
133
|
+
if (note) return "it has a comment that isn't AgentOS's";
|
|
134
|
+
if (known.has(hash))
|
|
135
|
+
return "no AgentOS comment on it, and its session is known here";
|
|
136
|
+
return isLocal(server) ? null : "no AgentOS comment, and not a local server";
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function list(deps: DatabaseDeps, server: PgServer) {
|
|
140
|
+
const env = pgEnv(server);
|
|
141
|
+
for (const age of [true, false]) {
|
|
142
|
+
const { code, out } = await deps.run(
|
|
143
|
+
"psql",
|
|
144
|
+
["-X", "-d", "postgres", "-tAq", "-F", "\t", "-c", LIST(age)],
|
|
145
|
+
env
|
|
146
|
+
);
|
|
147
|
+
if (code === 0)
|
|
148
|
+
return out
|
|
149
|
+
.split("\n")
|
|
150
|
+
.filter(Boolean)
|
|
151
|
+
.map((line) => {
|
|
152
|
+
const [name, size, note, conns, at] = line.split("\t");
|
|
153
|
+
return {
|
|
154
|
+
name,
|
|
155
|
+
bytes: Number(size) || 0,
|
|
156
|
+
note: note ?? "",
|
|
157
|
+
conns: Number(conns) || 0,
|
|
158
|
+
madeAt: Number(at) * 1000 || 0,
|
|
159
|
+
};
|
|
160
|
+
});
|
|
161
|
+
if (code === 127) return "psql isn't installed";
|
|
162
|
+
if (!age) return `Postgres isn't answering (${out.slice(0, 120)})`;
|
|
163
|
+
}
|
|
164
|
+
return "Postgres isn't answering";
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export async function findTaskDatabases(deps: DatabaseDeps): Promise<{
|
|
168
|
+
candidates: Candidate[];
|
|
169
|
+
skipped: SweepSkipped[];
|
|
170
|
+
}> {
|
|
171
|
+
const candidates: Candidate[] = [];
|
|
172
|
+
const skipped: SweepSkipped[] = [];
|
|
173
|
+
for (const server of deps.servers) {
|
|
174
|
+
const found = await list(deps, server);
|
|
175
|
+
if (typeof found === "string") {
|
|
176
|
+
skipped.push({
|
|
177
|
+
path: where(server),
|
|
178
|
+
kind: "database",
|
|
179
|
+
reason: found,
|
|
180
|
+
});
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
for (const d of found) {
|
|
184
|
+
const hash = taskHashOf(d.name);
|
|
185
|
+
const skip = (reason: string) =>
|
|
186
|
+
skipped.push({ path: d.name, kind: "database", reason });
|
|
187
|
+
if (!hash) continue;
|
|
188
|
+
const proof = whoMade(hash, d.note, deps.known, server);
|
|
189
|
+
if (deps.froms.has(d.name)) skip("a project's declared source");
|
|
190
|
+
else if (proof) skip(proof);
|
|
191
|
+
else if (deps.live.has(hash)) skip("its session is still running");
|
|
192
|
+
else if (d.conns > 0) skip(`${d.conns} connection(s) open`);
|
|
193
|
+
else if (d.madeAt && deps.now - d.madeAt < DAY_MS)
|
|
194
|
+
skip("made less than a day ago");
|
|
195
|
+
else
|
|
196
|
+
candidates.push({
|
|
197
|
+
path: d.name,
|
|
198
|
+
kind: "database",
|
|
199
|
+
server,
|
|
200
|
+
bytes: d.bytes,
|
|
201
|
+
why: `no session needs it${d.madeAt ? "" : " (age unknown)"}`,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return { candidates, skipped };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Re-checked here as well: only a name carrying the token is ever dropped,
|
|
209
|
+
// and a plain drop, which Postgres refuses while anyone is connected.
|
|
210
|
+
export async function dropTaskDatabase(
|
|
211
|
+
c: Candidate,
|
|
212
|
+
froms: Set<string>,
|
|
213
|
+
run: PgRun = pgRun
|
|
214
|
+
): Promise<boolean> {
|
|
215
|
+
if (!taskHashOf(c.path) || froms.has(c.path)) return false;
|
|
216
|
+
const { code } = await run(
|
|
217
|
+
"dropdb",
|
|
218
|
+
["--if-exists", "--", c.path],
|
|
219
|
+
pgEnv(c.server ?? {})
|
|
220
|
+
);
|
|
221
|
+
return code === 0;
|
|
222
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { execFile } from "child_process";
|
|
2
|
+
|
|
3
|
+
// Disk use of a folder in bytes; 0 when it can't be measured in time.
|
|
4
|
+
export default function duBytes(
|
|
5
|
+
dir: string,
|
|
6
|
+
timeout = 300_000
|
|
7
|
+
): Promise<number> {
|
|
8
|
+
return new Promise((resolve) =>
|
|
9
|
+
execFile("du", ["-sk", dir], { timeout }, (error, stdout) =>
|
|
10
|
+
resolve(error ? 0 : (parseInt(String(stdout), 10) || 0) * 1024)
|
|
11
|
+
)
|
|
12
|
+
);
|
|
13
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The second half of a run: what tasks leave beside their worktrees. Found
|
|
3
|
+
* by databases.ts and caches.ts, removed here only when the full clean-up is
|
|
4
|
+
* on; otherwise listed as pending, with sizes, for a dry run's report.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from "fs";
|
|
8
|
+
import os from "os";
|
|
9
|
+
import path from "path";
|
|
10
|
+
import { SPARE_ROOT } from "../worktree-deps/spare";
|
|
11
|
+
import duBytes from "./du";
|
|
12
|
+
import { pgRun } from "../project-config/database";
|
|
13
|
+
import {
|
|
14
|
+
declaredDatabases,
|
|
15
|
+
dropTaskDatabase,
|
|
16
|
+
findTaskDatabases,
|
|
17
|
+
hashesOf,
|
|
18
|
+
knownSessionIds,
|
|
19
|
+
liveHashes,
|
|
20
|
+
liveSessionIds,
|
|
21
|
+
} from "./databases";
|
|
22
|
+
import { findCacheLeftovers, workspaceFromPlist } from "./caches";
|
|
23
|
+
import type { SweepDeps } from "./runSweep";
|
|
24
|
+
import type {
|
|
25
|
+
Candidate,
|
|
26
|
+
SweepRemoved,
|
|
27
|
+
SweepSkipped,
|
|
28
|
+
SweepSummary,
|
|
29
|
+
SweepTotals,
|
|
30
|
+
} from "./types";
|
|
31
|
+
import { WORKTREES_DIR } from "../worktrees";
|
|
32
|
+
|
|
33
|
+
export interface Extras {
|
|
34
|
+
candidates: Candidate[];
|
|
35
|
+
skipped: SweepSkipped[];
|
|
36
|
+
drop: (c: Candidate) => Promise<boolean>;
|
|
37
|
+
spares: { count: number; bytes: number };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const dirCount = (root: string) => {
|
|
41
|
+
try {
|
|
42
|
+
return fs
|
|
43
|
+
.readdirSync(root, { withFileTypes: true })
|
|
44
|
+
.filter((e) => e.isDirectory() && !e.name.startsWith(".")).length;
|
|
45
|
+
} catch {
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// The size of a whole root (every worktree, every spare) takes minutes of
|
|
51
|
+
// disk reading, so it is measured at most every six hours, or again on a
|
|
52
|
+
// dry run, which is what asks for fresh numbers.
|
|
53
|
+
const ROOT_SIZE_TTL_MS = 6 * 3600_000;
|
|
54
|
+
const rootSizes = new Map<string, { at: number; bytes: number }>();
|
|
55
|
+
|
|
56
|
+
export async function cachedRootSize(
|
|
57
|
+
dir: string,
|
|
58
|
+
size: (dir: string) => Promise<number>,
|
|
59
|
+
fresh: boolean,
|
|
60
|
+
now = Date.now()
|
|
61
|
+
): Promise<number> {
|
|
62
|
+
const hit = rootSizes.get(dir);
|
|
63
|
+
if (hit && !fresh && now - hit.at < ROOT_SIZE_TTL_MS) return hit.bytes;
|
|
64
|
+
const bytes = await size(dir);
|
|
65
|
+
// 0 is what a failed or timed-out du reports: try again next time.
|
|
66
|
+
if (bytes > 0) rootSizes.set(dir, { at: now, bytes });
|
|
67
|
+
return bytes;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const forgetRootSizes = () => rootSizes.clear();
|
|
71
|
+
|
|
72
|
+
// Removes a folder directly inside one of the roots, and nothing else.
|
|
73
|
+
export async function dropFolderIn(
|
|
74
|
+
roots: string[],
|
|
75
|
+
folder: string
|
|
76
|
+
): Promise<boolean> {
|
|
77
|
+
const target = path.resolve(folder);
|
|
78
|
+
if (!roots.some((r) => r && path.dirname(target) === path.resolve(r)))
|
|
79
|
+
return false;
|
|
80
|
+
await fs.promises.rm(target, { recursive: true, force: true });
|
|
81
|
+
return !fs.existsSync(target);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function defaultExtras(
|
|
85
|
+
opts: { fresh?: boolean } = {}
|
|
86
|
+
): Promise<Extras> {
|
|
87
|
+
const { servers, froms } = declaredDatabases();
|
|
88
|
+
const ids = liveSessionIds();
|
|
89
|
+
const size = (dir: string) => duBytes(dir);
|
|
90
|
+
const dbs = await findTaskDatabases({
|
|
91
|
+
now: Date.now(),
|
|
92
|
+
run: pgRun,
|
|
93
|
+
servers,
|
|
94
|
+
live: liveHashes(ids),
|
|
95
|
+
known: hashesOf(knownSessionIds()),
|
|
96
|
+
froms,
|
|
97
|
+
});
|
|
98
|
+
const derivedRoot = path.join(
|
|
99
|
+
os.homedir(),
|
|
100
|
+
"Library",
|
|
101
|
+
"Developer",
|
|
102
|
+
"Xcode",
|
|
103
|
+
"DerivedData"
|
|
104
|
+
);
|
|
105
|
+
const caches = await findCacheLeftovers({
|
|
106
|
+
now: Date.now(),
|
|
107
|
+
spareRoot: SPARE_ROOT,
|
|
108
|
+
tmpRoot: os.tmpdir(),
|
|
109
|
+
derivedRoot: process.platform === "darwin" ? derivedRoot : "",
|
|
110
|
+
worktreesRoot: WORKTREES_DIR,
|
|
111
|
+
liveIds: ids,
|
|
112
|
+
workspaceOf: workspaceFromPlist,
|
|
113
|
+
size,
|
|
114
|
+
});
|
|
115
|
+
const roots = [SPARE_ROOT, os.tmpdir(), derivedRoot];
|
|
116
|
+
return {
|
|
117
|
+
candidates: [...dbs.candidates, ...caches.candidates],
|
|
118
|
+
skipped: [...dbs.skipped, ...caches.skipped],
|
|
119
|
+
spares: {
|
|
120
|
+
count: dirCount(SPARE_ROOT),
|
|
121
|
+
bytes: await cachedRootSize(SPARE_ROOT, size, !!opts.fresh),
|
|
122
|
+
},
|
|
123
|
+
drop: async (c) =>
|
|
124
|
+
c.kind === "database"
|
|
125
|
+
? dropTaskDatabase(c, froms)
|
|
126
|
+
: dropFolderIn(roots, c.path),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Removes (or, off or dry, only lists) what the extras found.
|
|
131
|
+
export async function findExtras(
|
|
132
|
+
deps: SweepDeps,
|
|
133
|
+
summary: SweepSummary,
|
|
134
|
+
opts: { armed: boolean; dryRun: boolean }
|
|
135
|
+
): Promise<{ totals: SweepTotals }> {
|
|
136
|
+
const x = await deps.extras({ fresh: opts.dryRun });
|
|
137
|
+
summary.skipped.push(...x.skipped);
|
|
138
|
+
for (const c of x.candidates) {
|
|
139
|
+
const item: SweepRemoved = {
|
|
140
|
+
path: c.path,
|
|
141
|
+
kind: c.kind,
|
|
142
|
+
why: c.why ?? "",
|
|
143
|
+
bytes: c.bytes ?? (await deps.size(c.path)),
|
|
144
|
+
};
|
|
145
|
+
if (!opts.armed) {
|
|
146
|
+
summary.pending!.push(item);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (deps.busy()) {
|
|
150
|
+
summary.skipped.push({
|
|
151
|
+
path: c.path,
|
|
152
|
+
kind: c.kind,
|
|
153
|
+
reason: "the machine is busy; left for the next run",
|
|
154
|
+
});
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (!opts.dryRun && !(await x.drop(c).catch(() => false))) {
|
|
158
|
+
summary.skipped.push({
|
|
159
|
+
path: c.path,
|
|
160
|
+
kind: c.kind,
|
|
161
|
+
reason: "removing it failed",
|
|
162
|
+
});
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
summary.removed.push(item);
|
|
166
|
+
summary.freedBytes += item.bytes;
|
|
167
|
+
}
|
|
168
|
+
const found = (kind: Candidate["kind"]) => {
|
|
169
|
+
const items = [...summary.removed, ...(summary.pending ?? [])].filter(
|
|
170
|
+
(i) => i.kind === kind
|
|
171
|
+
);
|
|
172
|
+
return {
|
|
173
|
+
count: items.length,
|
|
174
|
+
bytes: items.reduce((n, i) => n + i.bytes, 0),
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
return {
|
|
178
|
+
totals: {
|
|
179
|
+
worktrees: {
|
|
180
|
+
count: dirCount(deps.root),
|
|
181
|
+
bytes: await cachedRootSize(deps.root, deps.size, opts.dryRun),
|
|
182
|
+
},
|
|
183
|
+
databases: found("database"),
|
|
184
|
+
spares: x.spares,
|
|
185
|
+
temp: found("temp"),
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// For tests, and for a machine without the extras: nothing found.
|
|
191
|
+
export const noExtras = async (): Promise<Extras> => ({
|
|
192
|
+
candidates: [],
|
|
193
|
+
skipped: [],
|
|
194
|
+
drop: async () => false,
|
|
195
|
+
spares: { count: 0, bytes: 0 },
|
|
196
|
+
});
|
|
@@ -61,8 +61,16 @@ function gitdirOf(dir: string): string | null {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
// The server's own temp folder differs from a test run's (launchd, systemd),
|
|
65
|
+
// so the usual system ones count too.
|
|
66
|
+
const TEMP_ROOTS = [
|
|
67
|
+
/^\/(?:private\/)?tmp(?:\/|$)/,
|
|
68
|
+
/^\/(?:private\/)?var\/folders\/[^/]+\/[^/]+\/T(?:\/|$)/,
|
|
69
|
+
/^\/var\/tmp(?:\/|$)/,
|
|
70
|
+
];
|
|
64
71
|
const inTemp = (p: string) =>
|
|
65
|
-
[os.tmpdir(), real(os.tmpdir())].some((t) => inFolder(p, t))
|
|
72
|
+
[os.tmpdir(), real(os.tmpdir())].some((t) => inFolder(p, t)) ||
|
|
73
|
+
TEMP_ROOTS.some((r) => r.test(p));
|
|
66
74
|
|
|
67
75
|
// Nothing in it but what AgentOS placed or rebuilds: no work to lose.
|
|
68
76
|
export const holdsNothing = (dir: string) =>
|
|
@@ -103,7 +111,16 @@ export async function findOrphans(deps: OrphanDeps): Promise<{
|
|
|
103
111
|
if (mine.has(real(dir))) continue;
|
|
104
112
|
const skip = (reason: string) =>
|
|
105
113
|
skipped.push({ path: dir, kind: "orphan", reason });
|
|
106
|
-
|
|
114
|
+
const gitdir = gitdirOf(dir);
|
|
115
|
+
const registered = !!gitdir && fs.existsSync(gitdir);
|
|
116
|
+
const leak =
|
|
117
|
+
TEST_LEAK.test(entry.name) &&
|
|
118
|
+
!!gitdir &&
|
|
119
|
+
inTemp(real(path.dirname(gitdir)));
|
|
120
|
+
// A test's folder whose repository is gone is nobody's, however young:
|
|
121
|
+
// new ones arrive with every run of an old checkout's tests.
|
|
122
|
+
const abandoned = leak && !registered;
|
|
123
|
+
if (!abandoned && deps.now - newestMs(dir) < days * DAY_MS) {
|
|
107
124
|
skip(`touched less than ${days} day${days === 1 ? "" : "s"} ago`);
|
|
108
125
|
continue;
|
|
109
126
|
}
|
|
@@ -111,12 +128,6 @@ export async function findOrphans(deps: OrphanDeps): Promise<{
|
|
|
111
128
|
skip("a terminal is open in it");
|
|
112
129
|
continue;
|
|
113
130
|
}
|
|
114
|
-
const gitdir = gitdirOf(dir);
|
|
115
|
-
const registered = !!gitdir && fs.existsSync(gitdir);
|
|
116
|
-
const leak =
|
|
117
|
-
TEST_LEAK.test(entry.name) &&
|
|
118
|
-
!!gitdir &&
|
|
119
|
-
inTemp(real(path.dirname(gitdir)));
|
|
120
131
|
if (leak) {
|
|
121
132
|
candidates.push({
|
|
122
133
|
path: dir,
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The list a dry run writes before anything is removed: what would go, what
|
|
3
|
+
* stays and why, with sizes, for a person to read before turning the full
|
|
4
|
+
* clean-up on. One file per day under ~/.agent-os/audits/disk-cleanup.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from "fs";
|
|
8
|
+
import path from "path";
|
|
9
|
+
import { agentosHome } from "../agentos-home";
|
|
10
|
+
import type { SweepKind, SweepRemoved, SweepSummary } from "./types";
|
|
11
|
+
|
|
12
|
+
export const mb = (b: number) =>
|
|
13
|
+
b >= 1024 ** 3
|
|
14
|
+
? `${(b / 1024 ** 3).toFixed(2)} GB`
|
|
15
|
+
: `${(b / 1024 ** 2).toFixed(1)} MB`;
|
|
16
|
+
|
|
17
|
+
const TITLES: Record<SweepKind, string> = {
|
|
18
|
+
settled: "Worktrees of finished tasks",
|
|
19
|
+
orphan: "Worktree folders no session owns",
|
|
20
|
+
"test-leak": "Throwaway repositories left by tests",
|
|
21
|
+
database: "Task databases",
|
|
22
|
+
spare: "Dependency spares",
|
|
23
|
+
temp: "Temp folders",
|
|
24
|
+
"test-temp": "Test temp folders",
|
|
25
|
+
"derived-data": "Xcode build folders of removed worktrees",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function section(kind: SweepKind, items: SweepRemoved[]): string {
|
|
29
|
+
const bytes = items.reduce((n, i) => n + i.bytes, 0);
|
|
30
|
+
const rows = items
|
|
31
|
+
.sort((a, b) => b.bytes - a.bytes)
|
|
32
|
+
.map((i) => `- \`${i.path}\` · ${mb(i.bytes)} · ${i.why}`);
|
|
33
|
+
return `## ${TITLES[kind]}: ${items.length} (${mb(bytes)})\n\n${rows.join("\n")}\n`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function renderDiskReport(summary: SweepSummary, at: Date): string {
|
|
37
|
+
const all = [...summary.removed, ...(summary.pending ?? [])];
|
|
38
|
+
const kinds = Object.keys(TITLES) as SweepKind[];
|
|
39
|
+
const total = all.reduce((n, i) => n + i.bytes, 0);
|
|
40
|
+
const why = new Map<string, number>();
|
|
41
|
+
for (const k of summary.skipped) {
|
|
42
|
+
const r = k.reason.replace(/\(.*\)/, "").trim();
|
|
43
|
+
why.set(r, (why.get(r) ?? 0) + 1);
|
|
44
|
+
}
|
|
45
|
+
return [
|
|
46
|
+
`# Disk clean-up dry run, ${at.toISOString()}`,
|
|
47
|
+
"",
|
|
48
|
+
`Would remove ${all.length} items, ${mb(total)}. Nothing was removed.`,
|
|
49
|
+
`Full clean-up is ${summary.armed ? "on" : "off"} on this machine.`,
|
|
50
|
+
"",
|
|
51
|
+
...kinds
|
|
52
|
+
.map((k) => all.filter((i) => i.kind === k))
|
|
53
|
+
.filter((items) => items.length)
|
|
54
|
+
.map((items) => section(items[0].kind, items)),
|
|
55
|
+
`## Kept: ${summary.skipped.length}`,
|
|
56
|
+
"",
|
|
57
|
+
...[...why].map(([r, n]) => `- ${n} × ${r}`),
|
|
58
|
+
"",
|
|
59
|
+
"### Kept, in detail",
|
|
60
|
+
"",
|
|
61
|
+
...summary.skipped
|
|
62
|
+
.filter((s) => !/less than \d+ day/.test(s.reason))
|
|
63
|
+
.slice(0, 300)
|
|
64
|
+
.map((s) => `- \`${s.path}\` · ${s.reason}`),
|
|
65
|
+
"",
|
|
66
|
+
].join("\n");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// A second dry run the same day gets -2, -3, never overwrites the first.
|
|
70
|
+
export function writeDiskReport(
|
|
71
|
+
summary: SweepSummary,
|
|
72
|
+
at = new Date(),
|
|
73
|
+
dir = agentosHome("audits", "disk-cleanup")
|
|
74
|
+
): string {
|
|
75
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
76
|
+
const day = [
|
|
77
|
+
at.getFullYear(),
|
|
78
|
+
String(at.getMonth() + 1).padStart(2, "0"),
|
|
79
|
+
String(at.getDate()).padStart(2, "0"),
|
|
80
|
+
].join("-");
|
|
81
|
+
let file = path.join(dir, `${day}.md`);
|
|
82
|
+
for (let n = 2; fs.existsSync(file); n++)
|
|
83
|
+
file = path.join(dir, `${day}-${n}.md`);
|
|
84
|
+
fs.writeFileSync(file, renderDiskReport(summary, at), { mode: 0o600 });
|
|
85
|
+
return file;
|
|
86
|
+
}
|
|
@@ -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
|
|
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
|
|
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
|
-
|
|
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
|
}
|