@mmerterden/multi-agent-pipeline 24.2.0 → 24.3.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.
@@ -42,11 +42,14 @@
42
42
  *
43
43
  * Usage:
44
44
  * disk-cleaner.mjs [--dry-run] [--apply] [--only=a,b] [--skip=a,b]
45
- * [--trash] [--transcripts] [--unattended] [--include-recent]
45
+ * [--trash] [--transcripts] [--test-artifacts] [--unattended]
46
+ * [--include-recent]
46
47
  * [--log-days=N] [--archive-days=N] [--transcript-days=N]
48
+ * [--artifact-days=N]
47
49
  * [--scratch-hours=N] [--build-hours=N] [--orphan-minutes=N]
48
50
  * [--runtime-days=N] [--depth=N] [--exclude=PATH]...
49
- * [--root=DIR]... [--tmp-root=DIR]... [--home=DIR]
51
+ * [--root=DIR]... [--tmp-root=DIR]... [--artifact-root=DIR]...
52
+ * [--home=DIR]
50
53
  * [--report=FILE] [--json] [--list] [--top=N]
51
54
  *
52
55
  * --root and --depth (defaults: the git repos under the usual dev folders,
@@ -54,6 +57,11 @@
54
57
  * DISK_CLEANER_DEPTH; --exclude from DISK_CLEANER_EXCLUDE (colon-separated).
55
58
  * An excluded path, and any candidate that holds it, is kept.
56
59
  *
60
+ * --artifact-root, also DISK_CLEANER_ARTIFACT_ROOTS (colon-separated), adds a
61
+ * tree to search for test run output beside the dev roots. It is where a
62
+ * harness that keeps its results outside a repository is named, so no product's
63
+ * paths live in this file.
64
+ *
57
65
  * Every category in the JSON report carries `top` (its largest items, real
58
66
  * paths and sizes, 5 unless --top says otherwise), `more` (how many items
59
67
  * `top` leaves out) and `kept` (how many items were kept, per reason). The
@@ -70,12 +78,15 @@
70
78
 
71
79
  import { spawnSync } from "node:child_process";
72
80
  import {
81
+ closeSync,
73
82
  existsSync,
74
83
  lstatSync,
75
84
  mkdirSync,
85
+ openSync,
76
86
  readdirSync,
77
87
  realpathSync,
78
88
  readFileSync,
89
+ readSync,
79
90
  rmSync,
80
91
  statfsSync,
81
92
  writeFileSync,
@@ -312,12 +323,14 @@ export function makeContext(o = {}) {
312
323
  now: o.now ?? Date.now(),
313
324
  tmpRoots,
314
325
  devRoots: o.devRoots || defaultDevRoots(home),
326
+ artifactRoots: (o.artifactRoots || []).filter(Boolean).map((r) => nativeReal(r) || r),
315
327
  scriptsDir: o.scriptsDir || defaultScriptsDir(home),
316
328
  sessionId: o.sessionId ?? process.env.CLAUDE_CODE_SESSION_ID ?? "",
317
329
  days: {
318
330
  logs: o.logDays ?? 30,
319
331
  archives: o.archiveDays ?? 90,
320
332
  transcripts: o.transcriptDays ?? 90,
333
+ artifacts: o.artifactDays ?? 7,
321
334
  },
322
335
  includeRecent: Boolean(o.includeRecent),
323
336
  scratchHours: o.scratchHours ?? 48,
@@ -328,6 +341,7 @@ export function makeContext(o = {}) {
328
341
  maxDepth: o.maxDepth ?? MAX_WALK_DEPTH,
329
342
  excludes: (o.excludes || []).filter(Boolean).map((p) => nativeReal(p) || p),
330
343
  denied: new Set(),
344
+ trackedCache: new Map(),
331
345
  run: o.run || realRun,
332
346
  du: o.du || duBytes,
333
347
  ps: () => (psCache ??= (o.ps || realPs)()),
@@ -669,6 +683,54 @@ const BUILD_SHAPE = {
669
683
  };
670
684
  BUILD_SHAPE[".DerivedData"] = BUILD_SHAPE.DerivedData;
671
685
 
686
+ // Build output under a name nobody can enumerate.
687
+ //
688
+ // Matching on the folder's NAME finds the default layouts and nothing else. A
689
+ // tool that points `-derivedDataPath` somewhere of its own choosing - a test
690
+ // harness giving each lane its own copy, a CI script, a wrapper that keeps one
691
+ // per scheme - writes the same tree under a name this file has never heard of.
692
+ // Measured on one machine: four such copies beside a repository's worktrees
693
+ // held 104 GB, the walk descended through all of it looking for a folder called
694
+ // DerivedData, and the report came back with 11 GB.
695
+ //
696
+ // So the shape decides, not the name. Both tools write a signature no source
697
+ // tree carries: Xcode writes `Build` beside its `.noindex` caches, SwiftPM a
698
+ // `build.db` beside its manifest and checkouts. `Build` or `build.db` is tested
699
+ // first, one stat, and the rest only when that hits - so an ordinary directory
700
+ // costs a single extra stat.
701
+ const SHAPE_SIGNATURES = Object.freeze([
702
+ {
703
+ kind: "DerivedData",
704
+ lead: "Build",
705
+ others: ["Index.noindex", "ModuleCache.noindex", "SourcePackages", "Logs", "info.plist"],
706
+ least: 2,
707
+ },
708
+ {
709
+ kind: ".build",
710
+ lead: "build.db",
711
+ others: ["manifest.db", "checkouts", "artifacts", "repositories", "index-build"],
712
+ least: 1,
713
+ },
714
+ ]);
715
+
716
+ /**
717
+ * What a directory is, read from what it holds rather than what it is called.
718
+ *
719
+ * @param {string} dir
720
+ * @returns {"DerivedData"|".build"|null}
721
+ */
722
+ function shapeOf(dir) {
723
+ for (const sig of SHAPE_SIGNATURES) {
724
+ if (!existsSync(join(dir, sig.lead))) continue;
725
+ let found = 0;
726
+ for (const n of sig.others) {
727
+ if (existsSync(join(dir, n))) found++;
728
+ if (found >= sig.least) return sig.kind;
729
+ }
730
+ }
731
+ return null;
732
+ }
733
+
672
734
  function repoBuildCandidates(ctx, out) {
673
735
  const maxAge = ctx.repoBuildHours * HOUR_MS;
674
736
  const visit = (root, dir, depth) => {
@@ -684,9 +746,14 @@ function repoBuildCandidates(ctx, out) {
684
746
  if (!e.isDirectory() || e.isSymbolicLink()) continue;
685
747
  const p = join(dir, e.name);
686
748
  if (devPruned(ctx, p, e.name)) continue;
687
- const shape = BUILD_SHAPE[e.name];
749
+ // A folder with a build tool's NAME is a candidate when it also holds what
750
+ // that tool writes; one that does not is somebody's own directory, and the
751
+ // report says it was kept. A folder with any OTHER name is a candidate only
752
+ // when its shape says so, and then there is nothing to report about it.
753
+ const named = Boolean(BUILD_SHAPE[e.name]);
754
+ const shape = named ? BUILD_SHAPE[e.name] : BUILD_SHAPE[shapeOf(p) ?? ""];
688
755
  if (shape) {
689
- if (!shape.some((n) => existsSync(join(p, n)))) {
756
+ if (named && !shape.some((n) => existsSync(join(p, n)))) {
690
757
  out.skipped.push({ path: p, reason: "does not look like build output" });
691
758
  } else if (inUse(ctx, realOrNull(p) || p)) {
692
759
  hold(out, p, "in use by a running process", "live");
@@ -784,6 +851,146 @@ function claudeScratchCandidates(ctx, out) {
784
851
  }
785
852
  }
786
853
 
854
+ // What a test run leaves behind, found by shape.
855
+ //
856
+ // A run writes three kinds of output and all three are regenerable by running
857
+ // it again: the result bundle, the recordings and screenshots taken while it
858
+ // ran, and the report files. They are also the biggest thing on a machine that
859
+ // runs a suite every night, and nothing deleted them.
860
+ //
861
+ // The shape is the only safe way to find them. Naming the directories one
862
+ // harness happens to use would tie this file to that harness, and this
863
+ // repository carries no product's paths - an extra root belongs in preferences
864
+ // (DISK_CLEANER_ARTIFACT_ROOTS, --artifact-root), not here. So a run is
865
+ // recognised by what every harness writes: a result bundle, or a report file
866
+ // shaped like a test report. Media and report files count only inside such a
867
+ // directory, so a recording somebody saved among their own files is never a
868
+ // candidate.
869
+ //
870
+ // Opt-in, never unattended: a recording can be the only evidence of a failure
871
+ // anybody still has.
872
+ const RESULT_BUNDLE = /\.xcresult$/i;
873
+ const RUN_MEDIA = /\.(?:mp4|mov|m4v|png|jpe?g|heic)$/i;
874
+ const RUN_REPORT = /\.(?:html?|json|xml|junit|txt|log)$/i;
875
+ const JUNIT_HEAD = /<testsuites?[\s>]/i;
876
+
877
+ /**
878
+ * The names git tracks in one directory, cached per directory.
879
+ *
880
+ * A run's output is not in version control; a test fixture is. Without this the
881
+ * shape rules read a committed `TEST-*.xml` as a run's report and offer to
882
+ * delete the repository's own fixtures - found on the first real run, against
883
+ * this repository's test tree.
884
+ *
885
+ * `ls-files` is asked once per directory and only for that directory, so a
886
+ * directory of run output, which is normally ignored, costs one call that
887
+ * returns nothing.
888
+ *
889
+ * @returns {Set<string>} basenames; empty when the directory is not in a repository
890
+ */
891
+ function trackedNames(dir, ctx) {
892
+ const cached = ctx.trackedCache.get(dir);
893
+ if (cached) return cached;
894
+ const names = new Set();
895
+ const r = ctx.run("git", ["-C", dir, "ls-files", "-z", "--", "."], { timeout: 20_000 });
896
+ if (r.status === 0 && r.stdout) {
897
+ for (const rel of r.stdout.split("\0")) {
898
+ if (!rel) continue;
899
+ // `.` is this directory, so a path with a separator names something below
900
+ // it - and the first segment is what sits here.
901
+ names.add(rel.split("/")[0]);
902
+ }
903
+ }
904
+ ctx.trackedCache.set(dir, names);
905
+ return names;
906
+ }
907
+
908
+ /** Does this directory hold a test run's output? */
909
+ function holdsRunOutput(dir, ctx) {
910
+ let names;
911
+ try {
912
+ names = listDir(dir, ctx).map((p) => basename(p));
913
+ } catch {
914
+ return false;
915
+ }
916
+ if (names.some((n) => RESULT_BUNDLE.test(n))) return true;
917
+ for (const n of names) {
918
+ if (!/\.xml$/i.test(n)) continue;
919
+ try {
920
+ // The head of the file, not the whole of it: a junit file can be large and
921
+ // the element that identifies it is the first one.
922
+ const fd = openSync(join(dir, n), "r");
923
+ try {
924
+ const buf = Buffer.alloc(512);
925
+ const read = readSync(fd, buf, 0, 512, 0);
926
+ if (JUNIT_HEAD.test(buf.subarray(0, read).toString("utf8"))) return true;
927
+ } finally {
928
+ closeSync(fd);
929
+ }
930
+ } catch {
931
+ /* unreadable; it decides nothing */
932
+ }
933
+ }
934
+ return false;
935
+ }
936
+
937
+ function testArtifactCandidates(ctx, out) {
938
+ const maxAge = ctx.days.artifacts * DAY_MS;
939
+ const roots = [...ctx.devRoots, ...ctx.artifactRoots];
940
+ const seen = new Set();
941
+ for (const root of roots) {
942
+ if (seen.has(root) || !rootUsable(root, ctx, out)) continue;
943
+ seen.add(root);
944
+ const visit = (dir, depth) => {
945
+ if (depth > ctx.maxDepth) return;
946
+ let entries;
947
+ try {
948
+ entries = readdirSync(dir, { withFileTypes: true });
949
+ } catch (e) {
950
+ noteDenied(ctx, dir, e);
951
+ return;
952
+ }
953
+ // A result bundle is a directory. Take it whole and do not walk into it.
954
+ for (const e of entries) {
955
+ if (e.isSymbolicLink()) continue;
956
+ const path = join(dir, e.name);
957
+ if (e.isDirectory() && RESULT_BUNDLE.test(e.name)) {
958
+ take(path);
959
+ continue;
960
+ }
961
+ if (e.isDirectory()) {
962
+ if (WALK_SKIP.has(e.name) || devPruned(ctx, path, e.name)) continue;
963
+ if (e.name === ".git" || e.name === "node_modules" || e.name === "Pods") continue;
964
+ visit(path, depth + 1);
965
+ }
966
+ }
967
+ // Media and reports, only where a run's output is.
968
+ if (!holdsRunOutput(dir, ctx)) return;
969
+ for (const e of entries) {
970
+ if (!e.isFile() || e.isSymbolicLink()) continue;
971
+ if (!RUN_MEDIA.test(e.name) && !RUN_REPORT.test(e.name)) continue;
972
+ take(join(dir, e.name));
973
+ }
974
+ };
975
+ const take = (path) => {
976
+ if (trackedNames(dirname(path), ctx).has(basename(path))) {
977
+ out.skipped.push({ path, reason: "tracked by git" });
978
+ return;
979
+ }
980
+ if (!olderThan(path, maxAge, ctx)) {
981
+ hold(out, path, "touched recently", "age");
982
+ return;
983
+ }
984
+ if (inUse(ctx, realOrNull(path) || path)) {
985
+ hold(out, path, "in use by a running process", "live");
986
+ return;
987
+ }
988
+ addPath(out, ctx, path, [root]);
989
+ };
990
+ visit(root, 0);
991
+ }
992
+ }
993
+
787
994
  function transcriptCandidates(ctx, out) {
788
995
  const maxAge = ctx.days.transcripts * DAY_MS;
789
996
  const roots = [H(ctx, ".claude", "projects"), H(ctx, ".codex", "sessions")];
@@ -1240,6 +1447,14 @@ export const CATEGORIES = [
1240
1447
  optIn: "trash",
1241
1448
  discover: (ctx, out) => childrenOf(ctx, out, [H(ctx, ".Trash")]),
1242
1449
  },
1450
+ {
1451
+ id: "test-artifacts",
1452
+ label:
1453
+ "test run output under the dev and artifact roots, older than the artifact age: result bundles, recordings, screenshots and report files",
1454
+ unattended: false,
1455
+ optIn: "test-artifacts",
1456
+ discover: testArtifactCandidates,
1457
+ },
1243
1458
  {
1244
1459
  id: "transcripts",
1245
1460
  label: "Claude Code and Codex session transcripts older than the transcript age",
@@ -1427,7 +1642,7 @@ export function applyPlan(ctx, planned) {
1427
1642
  // ---------------------------------------------------------------- CLI
1428
1643
 
1429
1644
  function parseCli(argv) {
1430
- const o = { only: [], skip: [], tmpRoots: [], devRoots: [], excludes: [] };
1645
+ const o = { only: [], skip: [], tmpRoots: [], devRoots: [], artifactRoots: [], excludes: [] };
1431
1646
  const num = (k, v) => {
1432
1647
  if (!/^\d+$/.test(v)) throw new Error(`${k} needs a whole number, got "${v}"`);
1433
1648
  return Number(v);
@@ -1445,6 +1660,7 @@ function parseCli(argv) {
1445
1660
  break;
1446
1661
  case "--trash":
1447
1662
  case "--transcripts":
1663
+ case "--test-artifacts":
1448
1664
  case "--unattended":
1449
1665
  case "--json":
1450
1666
  case "--list":
@@ -1457,6 +1673,13 @@ function parseCli(argv) {
1457
1673
  if (!v) throw new Error("--exclude needs a path");
1458
1674
  o.excludes.push(v);
1459
1675
  break;
1676
+ case "--artifact-root":
1677
+ if (!v) throw new Error("--artifact-root needs a path");
1678
+ o.artifactRoots.push(v);
1679
+ break;
1680
+ case "--artifact-days":
1681
+ o.artifactDays = num(k, v);
1682
+ break;
1460
1683
  case "--depth":
1461
1684
  o.depth = num(k, v);
1462
1685
  break;
@@ -1670,6 +1893,9 @@ export function main(argv, env = process.env) {
1670
1893
  home: o.home,
1671
1894
  tmpRoots: o.tmpRoots.length ? o.tmpRoots : undefined,
1672
1895
  devRoots: o.devRoots.length ? o.devRoots : env.DISK_CLEANER_ROOTS?.split(":").filter(Boolean),
1896
+ artifactRoots: o.artifactRoots.length
1897
+ ? o.artifactRoots
1898
+ : (env.DISK_CLEANER_ARTIFACT_ROOTS?.split(":").filter(Boolean) ?? []),
1673
1899
  logDays: o.logDays,
1674
1900
  archiveDays: o.archiveDays,
1675
1901
  transcriptDays: o.transcriptDays,
@@ -1690,6 +1916,7 @@ export function main(argv, env = process.env) {
1690
1916
  unattended,
1691
1917
  trash: unattended ? false : o.trash,
1692
1918
  transcripts: unattended ? false : o.transcripts,
1919
+ "test-artifacts": unattended ? false : o["test-artifacts"],
1693
1920
  };
1694
1921
  const before = diskStatus(ctx.home);
1695
1922
  const planned = plan(ctx, sel);