benignswarm 0.12.27 → 0.12.28

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.
@@ -6,8 +6,8 @@ import { bool, str } from "../args.js";
6
6
  import { loadAgent, resolveAgentName } from "../config.js";
7
7
  import { Client } from "../client.js";
8
8
  import { heartbeatIfActive, resolveActivitySource } from "../heartbeat.js";
9
- import { scanProject } from "./worktrees.js";
10
- import { prunableWorktrees, removeWorktreeIfClean } from "../git.js";
9
+ import { scanProject, unitWorktrees } from "./worktrees.js";
10
+ import { pruneReport, removeWorktreeIfClean } from "../git.js";
11
11
  import { CliError } from "../errors.js";
12
12
  import { acquireWatchLock } from "../lock.js";
13
13
  import { gatherUpdates } from "./hook.js";
@@ -85,8 +85,10 @@ export async function watch(p, out) {
85
85
  const r = await scanProject(scanCreds, scanCreds.project);
86
86
  log(`worktree scan: ${r.recorded} in ${r.repo}${r.missing ? `, ${r.missing} gone` : ""}`);
87
87
  if (bool(p.flags, "prune")) {
88
- // Space back on a schedule: only clean, merged worktrees; never the one a unit is being worked in (dirty or ahead).
89
- for (const c of prunableWorktrees()) {
88
+ // Space back on a schedule: only clean worktrees whose work has landed or whose unit is
89
+ // closed; never one a unit is still being worked in (it may have no commits yet).
90
+ const units = await unitWorktrees(scanCreds, scanCreds.project);
91
+ for (const c of pruneReport(process.cwd(), units.closed, units.active).prunable) {
90
92
  const res = removeWorktreeIfClean(c.path);
91
93
  log(res.removed ? `pruned merged worktree ${c.path}${c.branch ? ` (${c.branch})` : ""}` : `kept ${c.path}: ${res.reason}`);
92
94
  }
@@ -1,10 +1,37 @@
1
- import { realpathSync } from "node:fs";
1
+ import { existsSync, realpathSync } from "node:fs";
2
2
  import { str, bool } from "../args.js";
3
3
  import { Client } from "../client.js";
4
4
  import { loadAgent, resolveAgentName } from "../config.js";
5
5
  import { CliError } from "../errors.js";
6
6
  import { defaultMachine, scanWorktrees, pruneReport, removeWorktreeIfClean } from "../git.js";
7
7
  import { table } from "../output.js";
8
+ const CLOSED = ["done", "abandoned", "superseded"];
9
+ /**
10
+ * What the project's units say about worktrees on this machine: worktrees of units not yet
11
+ * closed (by path, and by branch for a path recorded elsewhere) are never pruned; clean
12
+ * worktrees of done or abandoned units may be. Most recorded paths of old units are gone from
13
+ * disk and are skipped one by one; a failure to read the units is an error, never a quieter
14
+ * prune (before, one missing path threw and silently discarded every closed unit).
15
+ */
16
+ export async function unitWorktrees(creds, project) {
17
+ const all = await Client.forAgent(creds, undefined, { presence: false }).call("GET", `/projects/${project}/work-units?all=1`);
18
+ const closed = new Map();
19
+ const active = { paths: new Map(), branches: new Map() };
20
+ for (const u of all.units) {
21
+ const label = `WU #${u.number} (${u.status})`;
22
+ if (!CLOSED.includes(u.status)) {
23
+ if (u.branch && u.branch !== "main")
24
+ active.branches.set(u.branch, label);
25
+ if (u.worktree && existsSync(u.worktree))
26
+ active.paths.set(realpathSync(u.worktree), label);
27
+ continue;
28
+ }
29
+ if (u.status === "superseded" || !u.worktree || !existsSync(u.worktree))
30
+ continue;
31
+ closed.set(realpathSync(u.worktree), `WU #${u.number} ${u.status}`);
32
+ }
33
+ return { closed, active };
34
+ }
8
35
  /**
9
36
  * The project's worktree registry.
10
37
  * bschat worktrees every worktree the project knows about: machine, path, branch, dirty, merged, who, suggestion
@@ -21,15 +48,8 @@ export async function worktrees(p, out) {
21
48
  // Space back: delete linked worktrees that are clean and merged into the base branch. Dry run
22
49
  // unless --yes. Branches are kept (deleting a worktree never deletes its branch).
23
50
  // Worktrees recorded on units that are done or abandoned: clean ones go even if their branch never merged.
24
- const closedUnitPaths = new Map();
25
- try {
26
- const all = await Client.forAgent(creds, undefined, { presence: false }).call("GET", `/projects/${project}/work-units?all=1`);
27
- for (const u of all.units)
28
- if (u.worktree && (u.status === "done" || u.status === "abandoned"))
29
- closedUnitPaths.set(realpathSync(u.worktree), `WU #${u.number} ${u.status}`);
30
- }
31
- catch { /* the worktree is gone or the server is unreachable: fall back to merged-only */ }
32
- const rep = pruneReport(process.cwd(), closedUnitPaths);
51
+ const units = await unitWorktrees(creds, project);
52
+ const rep = pruneReport(process.cwd(), units.closed, units.active);
33
53
  const candidates = rep.prunable;
34
54
  const keptText = rep.kept.length ? `\nKept ${rep.kept.length} (measured against ${rep.base ?? "the base branch"}):\n${rep.kept.map((k) => ` ${k.path}${k.branch ? ` (${k.branch})` : ""}: ${k.reason}`).join("\n")}` : "";
35
55
  if (!candidates.length) {
package/dist/git.js CHANGED
@@ -196,12 +196,30 @@ export function ensureWorktree(path, branch, base, cwd = process.cwd(), log = ()
196
196
  export function prunableWorktrees(cwd = process.cwd()) {
197
197
  return pruneReport(cwd).prunable;
198
198
  }
199
+ /**
200
+ * Whether everything on `head` is already in `base` by content: merging it would leave base's
201
+ * tree unchanged. True for a branch that landed by merge, rebase, cherry-pick or squash, which
202
+ * ancestry alone misses (ester: WU 68, 98, 99, 101, 102 all landed rebased or squashed, and their
203
+ * 20 GB worktrees were kept as "N commits not on origin/main"). Null when git cannot tell.
204
+ */
205
+ export function landedByContent(head, base, cwd) {
206
+ const baseTree = git(["rev-parse", `${base}^{tree}`], cwd);
207
+ if (!baseTree)
208
+ return null;
209
+ const r = spawnSync("git", ["merge-tree", "--write-tree", "--no-messages", base, head], { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 120_000 });
210
+ if (r.status === 1)
211
+ return false; // conflicts: the branch still differs from base
212
+ if (r.status !== 0)
213
+ return null;
214
+ return r.stdout.split("\n")[0].trim() === baseTree;
215
+ }
199
216
  /**
200
217
  * Every linked worktree sorted into prunable or kept, with the reason. "Merged" means the
201
218
  * worktree's head is on the base branch here or on origin's copy of it (fetched first), so a
202
- * machine whose local main is behind does not keep work that already landed.
219
+ * machine whose local main is behind does not keep work that already landed, or that its
220
+ * changes are already there by content (rebased or squashed landings).
203
221
  */
204
- export function pruneReport(cwd = process.cwd(), closedUnitPaths = new Map()) {
222
+ export function pruneReport(cwd = process.cwd(), closedUnitPaths = new Map(), activeUnits = { paths: new Map(), branches: new Map() }) {
205
223
  const scan = scanWorktrees(cwd);
206
224
  if (!scan)
207
225
  return { prunable: [], kept: [], base: null };
@@ -217,8 +235,16 @@ export function pruneReport(cwd = process.cwd(), closedUnitPaths = new Map()) {
217
235
  kept.push({ path: w.path, branch: w.branch, reason: "you are standing in it" });
218
236
  continue;
219
237
  }
238
+ // A unit still being worked keeps its worktree whatever git says: one just started has no
239
+ // commits yet, so its head is on the base and it looks merged (ester WU 110 and 111).
240
+ const active = activeUnits.paths.get(realpathOrSelf(w.path)) ?? (w.branch ? activeUnits.branches.get(w.branch) : undefined);
241
+ if (active) {
242
+ kept.push({ path: w.path, branch: w.branch, reason: `${active} is not closed` });
243
+ continue;
244
+ }
245
+ const closedNote = closedUnitPaths.get(realpathOrSelf(w.path));
220
246
  if (w.dirty !== false) {
221
- kept.push({ path: w.path, branch: w.branch, reason: w.dirty === null ? "cannot read its status" : "uncommitted changes" });
247
+ kept.push({ path: w.path, branch: w.branch, reason: `${w.dirty === null ? "cannot read its status" : "uncommitted changes"}${closedNote ? ` (${closedNote}: look before discarding)` : ""}` });
222
248
  continue;
223
249
  }
224
250
  const onRemote = remoteBase && w.head ? spawnSync("git", ["merge-base", "--is-ancestor", w.head, remoteBase], { cwd: scan.repo, stdio: "ignore" }).status === 0 : false;
@@ -226,10 +252,14 @@ export function pruneReport(cwd = process.cwd(), closedUnitPaths = new Map()) {
226
252
  prunable.push({ path: w.path, branch: w.branch });
227
253
  continue;
228
254
  }
255
+ const landedOn = remoteBase ?? base;
256
+ if (landedOn && w.head && landedByContent(w.head, landedOn, scan.repo) === true) {
257
+ prunable.push({ path: w.path, branch: w.branch, why: `its changes are on ${landedOn} (rebased or squashed)` });
258
+ continue;
259
+ }
229
260
  // A clean worktree of a closed unit holds nothing the branch does not: nobody is working there.
230
- const closed = closedUnitPaths.get(realpathOrSelf(w.path));
231
- if (closed) {
232
- prunable.push({ path: w.path, branch: w.branch, why: closed });
261
+ if (closedNote) {
262
+ prunable.push({ path: w.path, branch: w.branch, why: closedNote });
233
263
  continue;
234
264
  }
235
265
  kept.push({ path: w.path, branch: w.branch, reason: `${w.ahead ?? "?"} commit${w.ahead === 1 ? "" : "s"} not on ${remoteBase ?? base ?? "the base branch"}` });
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = "0.12.27";
1
+ export const CLI_VERSION = "0.12.28";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "benignswarm",
3
- "version": "0.12.27",
3
+ "version": "0.12.28",
4
4
  "description": "bschat: chat, status board and evidence store for coding agents (benignswarm.org)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {