@agentsignalnet/claude-plugin 0.2.121 → 0.2.122
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/.claude-plugin/plugin.json +1 -1
- package/package.json +1 -1
- package/scripts/inbox-daemon.mjs +18 -19
- package/scripts/inbox-hook.mjs +3 -1
- package/scripts/responder.mjs +197 -6
package/package.json
CHANGED
package/scripts/inbox-daemon.mjs
CHANGED
|
@@ -48,7 +48,7 @@ import { incumbent, livingWatchers, lockDir as sharedLockDir, apiBase } from './
|
|
|
48
48
|
import {
|
|
49
49
|
settings as responderSettings, mayRespond, mayTryAgain, waitBeforeRetrying,
|
|
50
50
|
takeLock, releaseLock, heldBy,
|
|
51
|
-
record as recordResponder, readLedger, spawnResponder, hadItSwitchedOff,
|
|
51
|
+
record as recordResponder, readLedger, spawnResponder, hadItSwitchedOff, compactLedger, refusalsSaid, noteRefusal,
|
|
52
52
|
} from './responder.mjs';
|
|
53
53
|
|
|
54
54
|
const args = process.argv.slice(2);
|
|
@@ -445,7 +445,14 @@ async function considerResponding(watched) {
|
|
|
445
445
|
);
|
|
446
446
|
unavailable = { ...unavailable, at: now };
|
|
447
447
|
}
|
|
448
|
+
try {
|
|
449
|
+
const c = compactLedger(undefined, { now });
|
|
450
|
+
if (c.compacted) say(`responder ledger compacted: ${c.lines} lines -> ${c.kept} (${c.spawns} spawns); old file kept as ${c.archive}`);
|
|
451
|
+
} catch (e) {
|
|
452
|
+
say(`responder ledger could not be compacted: ${String(e?.message ?? e).slice(0, 160)}`);
|
|
453
|
+
}
|
|
448
454
|
const ledger = readLedger();
|
|
455
|
+
const said = refusalsSaid(ledger);
|
|
449
456
|
const lockHeldBy = heldBy();
|
|
450
457
|
|
|
451
458
|
for (const identity of watched) {
|
|
@@ -481,30 +488,22 @@ async function considerResponding(watched) {
|
|
|
481
488
|
reason, and repeats are dropped. A reason that CHANGES still gets its
|
|
482
489
|
own line, because that is a different fact about the world.
|
|
483
490
|
*/
|
|
484
|
-
|
|
485
|
-
!['disabled', 'within_grace', 'already_handled'].includes(verdict.why) &&
|
|
486
|
-
ledger.some(
|
|
487
|
-
(e) =>
|
|
488
|
-
e.event === 'refused' &&
|
|
489
|
-
e.delivery_id === message.delivery_id &&
|
|
490
|
-
e.why === verdict.why,
|
|
491
|
-
);
|
|
492
|
-
|
|
493
|
-
if (!['disabled', 'within_grace', 'already_handled'].includes(verdict.why) && !alreadySaid) {
|
|
491
|
+
if (!['disabled', 'within_grace', 'already_handled'].includes(verdict.why)) {
|
|
494
492
|
const entry = { event: 'refused', at: now, why: verdict.why,
|
|
495
493
|
delivery_id: message.delivery_id, peer: verdict.peer ?? null };
|
|
496
|
-
|
|
497
|
-
//
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
494
|
+
// `said` is updated too, so a second message in this same pass
|
|
495
|
+
// cannot write the line this one just wrote.
|
|
496
|
+
if (noteRefusal(entry, said, recordResponder)) {
|
|
497
|
+
ledger.push(entry);
|
|
498
|
+
say(`responder declined ${message.delivery_id}: ${verdict.why}`);
|
|
499
|
+
}
|
|
501
500
|
}
|
|
502
501
|
continue;
|
|
503
502
|
}
|
|
504
503
|
|
|
505
|
-
if (!takeLock()) {
|
|
506
|
-
|
|
507
|
-
|
|
504
|
+
if (!takeLock(undefined, now, { staleMs: config.staleLockMs })) {
|
|
505
|
+
noteRefusal({ event: 'refused', at: now, why: 'lock_taken',
|
|
506
|
+
delivery_id: message.delivery_id }, said, recordResponder);
|
|
508
507
|
return;
|
|
509
508
|
}
|
|
510
509
|
|
package/scripts/inbox-hook.mjs
CHANGED
|
@@ -297,7 +297,9 @@ if (mode === 'watch') {
|
|
|
297
297
|
is no safe key, and it falls through to the old behaviour. Wasteful beats
|
|
298
298
|
silent.
|
|
299
299
|
*/
|
|
300
|
-
|
|
300
|
+
// 2026-11-11: CLAUDE_CODE_HOST_SESSION_ID is no longer set in hook environments (0 of 40 live watchers had it), so the lease below
|
|
301
|
+
// never applied and every tool call left a ten-minute node process (105 alive, 5.2 GB). CLAUDE_CODE_SESSION_ID is what is set now.
|
|
302
|
+
const sessionKey = (process.env.CLAUDE_CODE_HOST_SESSION_ID ?? process.env.CLAUDE_CODE_SESSION_ID ?? '').replace(/[^A-Za-z0-9_-]/g, '_');
|
|
301
303
|
/*
|
|
302
304
|
`let`, because losing the lease must DEGRADE rather than throw.
|
|
303
305
|
|
package/scripts/responder.mjs
CHANGED
|
@@ -80,10 +80,11 @@ import { spawn } from 'node:child_process';
|
|
|
80
80
|
|
|
81
81
|
const require = createRequire(import.meta.url);
|
|
82
82
|
import {
|
|
83
|
-
appendFileSync, existsSync, mkdirSync, readFileSync,
|
|
83
|
+
appendFileSync, closeSync, existsSync, linkSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, statSync,
|
|
84
|
+
unlinkSync, writeFileSync,
|
|
84
85
|
} from 'node:fs';
|
|
85
86
|
import { homedir } from 'node:os';
|
|
86
|
-
import { join } from 'node:path';
|
|
87
|
+
import { basename, dirname, join } from 'node:path';
|
|
87
88
|
|
|
88
89
|
const HOME = process.env.HOME ?? homedir();
|
|
89
90
|
const ROOT = join(HOME, '.agentsignal');
|
|
@@ -230,10 +231,120 @@ export function readLedger(path = LEDGER) {
|
|
|
230
231
|
}
|
|
231
232
|
|
|
232
233
|
export function record(entry, path = LEDGER) {
|
|
233
|
-
mkdirSync(
|
|
234
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
234
235
|
appendFileSync(path, `${JSON.stringify(entry)}\n`);
|
|
235
236
|
}
|
|
236
237
|
|
|
238
|
+
/*
|
|
239
|
+
THE LEDGER HAS TO STAY SMALL ENOUGH TO RE-READ. The daemon reads all of it on
|
|
240
|
+
every cycle, because the brakes need every spawn (one message, one session is
|
|
241
|
+
forever) and the refusal dedupe needs the refusals. Nothing ever trimmed it.
|
|
242
|
+
Measured 2026-11-11: 262 MB and 2.36 million lines, 2,362,512 of them the same
|
|
243
|
+
`lock_taken` refusal written every two minutes behind a dead lock, and a
|
|
244
|
+
daemon at 1.6 GB of memory from parsing that file once per cycle.
|
|
245
|
+
|
|
246
|
+
Past `maxBytes` the file is rewritten with every spawn (the brakes' memory) and
|
|
247
|
+
the newest other lines from the last `keepMs`, up to half the cap. The old
|
|
248
|
+
file is KEPT, renamed beside it with a time stamp -- a thing that runs
|
|
249
|
+
unattended has to stay legible afterwards -- and only the oldest of those
|
|
250
|
+
archives beyond `keepArchives` are removed. Read in chunks, so a ledger that
|
|
251
|
+
already grew huge is compacted without loading it whole.
|
|
252
|
+
*/
|
|
253
|
+
export const LEDGER_LIMITS = { maxBytes: 8 * 1024 * 1024, keepMs: 7 * 86_400_000, keepArchives: 2 };
|
|
254
|
+
|
|
255
|
+
function eachLine(path, fn) {
|
|
256
|
+
const fd = openSync(path, 'r');
|
|
257
|
+
try {
|
|
258
|
+
const buf = Buffer.alloc(1 << 20);
|
|
259
|
+
let rest = '';
|
|
260
|
+
for (;;) {
|
|
261
|
+
const n = readSync(fd, buf, 0, buf.length, null);
|
|
262
|
+
if (n === 0) break;
|
|
263
|
+
const parts = (rest + buf.toString('utf8', 0, n)).split('\n');
|
|
264
|
+
rest = parts.pop();
|
|
265
|
+
for (const line of parts) if (line) fn(line);
|
|
266
|
+
}
|
|
267
|
+
if (rest) fn(rest);
|
|
268
|
+
} finally {
|
|
269
|
+
closeSync(fd);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function compactLedger(path = LEDGER, { now = Date.now(), ...opts } = {}) {
|
|
274
|
+
const { maxBytes, keepMs, keepArchives } = { ...LEDGER_LIMITS, ...opts };
|
|
275
|
+
let size;
|
|
276
|
+
try {
|
|
277
|
+
size = statSync(path).size;
|
|
278
|
+
} catch {
|
|
279
|
+
return { compacted: false, why: 'no_ledger' };
|
|
280
|
+
}
|
|
281
|
+
if (size <= maxBytes) return { compacted: false, why: 'under_cap', bytes: size };
|
|
282
|
+
|
|
283
|
+
const spawns = [];
|
|
284
|
+
const recent = [];
|
|
285
|
+
let recentBytes = 0;
|
|
286
|
+
let lines = 0;
|
|
287
|
+
eachLine(path, (line) => {
|
|
288
|
+
lines += 1;
|
|
289
|
+
let e;
|
|
290
|
+
try {
|
|
291
|
+
e = JSON.parse(line);
|
|
292
|
+
} catch {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (e && e.event === 'spawn') {
|
|
296
|
+
spawns.push(line);
|
|
297
|
+
} else if (e && Number.isFinite(e.at) && now - e.at < keepMs) {
|
|
298
|
+
recent.push(line);
|
|
299
|
+
recentBytes += line.length + 1;
|
|
300
|
+
while (recentBytes > maxBytes / 2 && recent.length) recentBytes -= recent.shift().length + 1;
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
const stampText = new Date(now).toISOString().replace(/[-:]/g, '').replace(/\.\d+Z$/, 'Z');
|
|
305
|
+
const archive = `${path}.${stampText}.jsonl`;
|
|
306
|
+
const tmp = `${path}.compacting-${process.pid}`;
|
|
307
|
+
writeFileSync(tmp, [...spawns, ...recent].map((l) => `${l}\n`).join(''));
|
|
308
|
+
renameSync(path, archive);
|
|
309
|
+
renameSync(tmp, path);
|
|
310
|
+
|
|
311
|
+
const dir = dirname(path);
|
|
312
|
+
const mine = new RegExp(`^${basename(path).replace(/[.]/g, '\\.')}\\.\\d{8}T\\d{6}Z\\.jsonl$`);
|
|
313
|
+
const archives = readdirSync(dir).filter((f) => mine.test(f)).sort();
|
|
314
|
+
const removed = archives.slice(0, Math.max(0, archives.length - keepArchives));
|
|
315
|
+
for (const f of removed) {
|
|
316
|
+
try {
|
|
317
|
+
unlinkSync(join(dir, f));
|
|
318
|
+
} catch {
|
|
319
|
+
/* already gone */
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return { compacted: true, lines, kept: spawns.length + recent.length, spawns: spawns.length, archive, removed };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/*
|
|
326
|
+
ONCE PER DELIVERY AND REASON, for every refusal -- `lock_taken` included. It
|
|
327
|
+
used to be written outside this dedupe, and behind a lock that could never be
|
|
328
|
+
taken it was written every cycle, forever. A Set, because the old
|
|
329
|
+
`ledger.some(...)` scanned the whole ledger for every message in the spool.
|
|
330
|
+
*/
|
|
331
|
+
export function refusalKey(e) {
|
|
332
|
+
return `${e.delivery_id}|${e.why}`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function refusalsSaid(ledger) {
|
|
336
|
+
return new Set(ledger.filter((e) => e.event === 'refused').map(refusalKey));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Writes `entry` unless that delivery was already refused for that reason. True when it wrote. */
|
|
340
|
+
export function noteRefusal(entry, said, write = record) {
|
|
341
|
+
const key = refusalKey(entry);
|
|
342
|
+
if (said.has(key)) return false;
|
|
343
|
+
write(entry);
|
|
344
|
+
said.add(key);
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
|
|
237
348
|
/**
|
|
238
349
|
* May this message spawn a responder?
|
|
239
350
|
*
|
|
@@ -356,9 +467,18 @@ export function mayTryAgain(state, now, config) {
|
|
|
356
467
|
return { ok: true, why: 'retrying_after_unavailable', attempts: state.attempts ?? 1 };
|
|
357
468
|
}
|
|
358
469
|
|
|
359
|
-
/**
|
|
360
|
-
export function
|
|
361
|
-
|
|
470
|
+
/** True while a process with this pid exists (EPERM: it exists, it is somebody else's). */
|
|
471
|
+
export function pidAlive(pid) {
|
|
472
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
473
|
+
try {
|
|
474
|
+
process.kill(pid, 0);
|
|
475
|
+
return true;
|
|
476
|
+
} catch (e) {
|
|
477
|
+
return e && e.code === 'EPERM';
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function create(path, now) {
|
|
362
482
|
try {
|
|
363
483
|
writeFileSync(path, JSON.stringify({ pid: process.pid, at: now }), { flag: 'wx' });
|
|
364
484
|
return true;
|
|
@@ -367,6 +487,77 @@ export function takeLock(path = LOCK, now = Date.now()) {
|
|
|
367
487
|
}
|
|
368
488
|
}
|
|
369
489
|
|
|
490
|
+
/*
|
|
491
|
+
Exclusive-create, the same brake the watcher uses. `wx` fails if it exists.
|
|
492
|
+
|
|
493
|
+
AND A DEAD LOCK IS TAKEN OVER. mayRespond already calls a lock older than
|
|
494
|
+
staleLockMs debris and lets the message through, but this never broke one, so
|
|
495
|
+
the create failed forever. Measured 2026-11-11: a lock left on Sep 18 by a pid
|
|
496
|
+
long gone, and from then on no responder ever ran on that machine while the
|
|
497
|
+
ledger gained a `lock_taken` line every cycle. A lock is debris when its pid
|
|
498
|
+
is not running or it is older than staleMs.
|
|
499
|
+
|
|
500
|
+
The takeover moves the old file aside and checks that what it moved is the
|
|
501
|
+
very lock it judged dead; if another process replaced it in between, that
|
|
502
|
+
lock is put back (link fails rather than overwrite) and this one does not run.
|
|
503
|
+
*/
|
|
504
|
+
export function takeLock(path = LOCK, now = Date.now(), { staleMs = DEFAULTS.staleLockMs, alive = pidAlive } = {}) {
|
|
505
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
506
|
+
if (create(path, now)) return true;
|
|
507
|
+
|
|
508
|
+
let raw;
|
|
509
|
+
let holder = null;
|
|
510
|
+
try {
|
|
511
|
+
raw = readFileSync(path, 'utf8');
|
|
512
|
+
holder = JSON.parse(raw);
|
|
513
|
+
} catch {
|
|
514
|
+
if (raw === undefined) return create(path, now); // released between the two calls
|
|
515
|
+
}
|
|
516
|
+
let dead;
|
|
517
|
+
if (holder && Number.isFinite(holder.at)) {
|
|
518
|
+
dead = !alive(holder.pid) || now - holder.at >= staleMs;
|
|
519
|
+
} else {
|
|
520
|
+
try {
|
|
521
|
+
dead = now - statSync(path).mtimeMs >= staleMs; // unreadable: a lock half written is not debris
|
|
522
|
+
} catch {
|
|
523
|
+
return create(path, now);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (!dead) return false;
|
|
527
|
+
|
|
528
|
+
const moved = `${path}.taken-over-${process.pid}-${now}`;
|
|
529
|
+
try {
|
|
530
|
+
renameSync(path, moved);
|
|
531
|
+
} catch {
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
534
|
+
let same = false;
|
|
535
|
+
try {
|
|
536
|
+
same = readFileSync(moved, 'utf8') === raw;
|
|
537
|
+
} catch {
|
|
538
|
+
same = false;
|
|
539
|
+
}
|
|
540
|
+
if (!same) {
|
|
541
|
+
try {
|
|
542
|
+
linkSync(moved, path);
|
|
543
|
+
} catch {
|
|
544
|
+
/* somebody holds it again; theirs stands */
|
|
545
|
+
}
|
|
546
|
+
try {
|
|
547
|
+
unlinkSync(moved);
|
|
548
|
+
} catch {
|
|
549
|
+
/* already gone */
|
|
550
|
+
}
|
|
551
|
+
return false;
|
|
552
|
+
}
|
|
553
|
+
try {
|
|
554
|
+
unlinkSync(moved);
|
|
555
|
+
} catch {
|
|
556
|
+
/* already gone */
|
|
557
|
+
}
|
|
558
|
+
return create(path, now);
|
|
559
|
+
}
|
|
560
|
+
|
|
370
561
|
export function releaseLock(path = LOCK) {
|
|
371
562
|
try {
|
|
372
563
|
unlinkSync(path);
|