benignswarm 0.12.46 → 0.12.47

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.
@@ -1,10 +1,10 @@
1
1
  import { spawn, spawnSync } from "node:child_process";
2
2
  import { bindSession, sessionOfExec } from "../sessions.js";
3
3
  import { reloadLaunchAgent, requireInstall } from "../install.js";
4
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
4
+ import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
5
5
  import { createHash } from "node:crypto";
6
6
  import { homedir } from "node:os";
7
- import { dirname, join } from "node:path";
7
+ import { basename, dirname, join } from "node:path";
8
8
  import { bool, str } from "../args.js";
9
9
  import { configRoot, loadAgent, resolveAgentName } from "../config.js";
10
10
  import { Client } from "../client.js";
@@ -90,12 +90,12 @@ export async function watch(p, out) {
90
90
  // One scan per repository per machine per hour, whichever watcher gets there first: five
91
91
  // fabricbios watchers rescanning the same 136 worktrees cost ~1,400 database queries an hour,
92
92
  // and D1 runs one query at a time for every tenant.
93
- const recent = repoScannedRecently(SCAN_EVERY_MS);
94
- if (recent)
95
- log(`worktree scan: skipped, ${recent}`);
93
+ const marker = scanMarker();
94
+ const taken = marker ? claimRepoScan(marker, scanCreds.agent, SCAN_EVERY_MS) : null;
95
+ if (taken)
96
+ log(`worktree scan: skipped, ${taken}`);
96
97
  else {
97
98
  const r = await scanProject(scanCreds, scanCreds.project);
98
- markRepoScanned(scanCreds.agent);
99
99
  log(`worktree scan: ${r.recorded} in ${r.repo}${r.missing ? `, ${r.missing} gone` : ""}`);
100
100
  }
101
101
  if (bool(p.flags, "prune")) {
@@ -363,24 +363,42 @@ function scanMarker() {
363
363
  const key = createHash("sha256").update(r.stdout.trim()).digest("hex").slice(0, 16);
364
364
  return join(configRoot(), "state", `scan-${key}.json`);
365
365
  }
366
- /** "by <agent> 12 min ago" when another watcher scanned this repository within `ms`, else null. */
367
- function repoScannedRecently(ms) {
368
- const f = scanMarker();
369
- if (!f || !existsSync(f))
370
- return null;
366
+ /**
367
+ * Claim this repository's scan for `by`, or say why another watcher has it. Watchers launched
368
+ * together reach this in the same second, so reading the marker and writing it after the scan let
369
+ * all of them scan (seven StickJoy watchers at once). The claim is an exclusive create of a file
370
+ * named after the marker version read, so exactly one watcher moves the marker on; it writes the
371
+ * marker before scanning, and older claim files are removed then.
372
+ */
373
+ export function claimRepoScan(f, by, everyMs, nowMs = Date.now()) {
374
+ let prev = { at: 0, by: "" };
375
+ if (existsSync(f)) {
376
+ const raw = readFileSync(f, "utf8");
377
+ try {
378
+ prev = JSON.parse(raw);
379
+ }
380
+ catch {
381
+ throw new Error(`scan marker ${f} is not JSON (${raw.slice(0, 40)}); remove it to let scans resume`);
382
+ }
383
+ }
384
+ const age = nowMs - prev.at;
385
+ if (prev.by && age < everyMs)
386
+ return `scanned by ${prev.by} ${Math.round(age / 60_000)} min ago`;
387
+ const claim = `${f}.${prev.at}.claim`;
388
+ mkdirSync(dirname(f), { recursive: true });
371
389
  try {
372
- const m = JSON.parse(readFileSync(f, "utf8"));
373
- const age = Date.now() - m.at;
374
- return age < ms ? `scanned by ${m.by} ${Math.round(age / 60_000)} min ago` : null;
390
+ writeFileSync(claim, by, { flag: "wx" });
375
391
  }
376
- catch {
377
- return null;
392
+ catch (e) {
393
+ if (e.code === "EEXIST")
394
+ return `another watcher (${readFileSync(claim, "utf8")}) claimed this scan`;
395
+ throw e;
378
396
  }
379
- }
380
- function markRepoScanned(by) {
381
- const f = scanMarker();
382
- if (!f)
383
- return;
384
- mkdirSync(dirname(f), { recursive: true });
385
- writeFileSync(f, JSON.stringify({ at: Date.now(), by }));
397
+ writeFileSync(f, JSON.stringify({ at: nowMs, by }));
398
+ const base = basename(f);
399
+ for (const old of readdirSync(dirname(f))) {
400
+ if (old.startsWith(`${base}.`) && old.endsWith(".claim") && old !== basename(claim))
401
+ rmSync(join(dirname(f), old), { force: true });
402
+ }
403
+ return null;
386
404
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = "0.12.46";
1
+ export const CLI_VERSION = "0.12.47";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "benignswarm",
3
- "version": "0.12.46",
3
+ "version": "0.12.47",
4
4
  "description": "bschat: chat, status board and evidence store for coding agents (benignswarm.org)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {