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.
- package/dist/commands/watch.js +41 -23
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/commands/watch.js
CHANGED
|
@@ -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
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
/**
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
381
|
-
const
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
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.
|
|
1
|
+
export const CLI_VERSION = "0.12.47";
|