@remnic/core 10.0.125 → 10.0.127
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/access-admin-ops-surface.js +38 -38
- package/dist/access-cli.js +38 -38
- package/dist/access-extraction-force-flush.js +38 -38
- package/dist/access-http-briefing.js +38 -38
- package/dist/access-http-query.js +38 -38
- package/dist/access-http.js +38 -38
- package/dist/access-identity-continuity-surface.js +38 -38
- package/dist/access-lcm-surface.js +38 -38
- package/dist/access-mcp.js +38 -38
- package/dist/access-observe-write-surface.js +38 -38
- package/dist/access-operations-batch.js +38 -38
- package/dist/access-operations.js +38 -38
- package/dist/access-recall-concurrency.js +38 -38
- package/dist/access-recall-response.js +38 -38
- package/dist/access-recall-surface.js +38 -38
- package/dist/access-service.d.ts.map +1 -1
- package/dist/access-service.js +38 -38
- package/dist/active-recall.js +6 -6
- package/dist/briefing.js +4 -4
- package/dist/buffer-turn-helpers.d.ts +7 -0
- package/dist/buffer-turn-helpers.d.ts.map +1 -1
- package/dist/buffer-turn-helpers.js +3 -1
- package/dist/buffer-turn.d.ts +10 -0
- package/dist/buffer-turn.d.ts.map +1 -1
- package/dist/buffer.js +3 -3
- package/dist/causal-consolidation.js +5 -5
- package/dist/{chunk-BNRKSJMO.js → chunk-2BBCM2PX.js} +2 -2
- package/dist/{chunk-2GSPNTOU.js → chunk-2G72JBQB.js} +2 -2
- package/dist/{chunk-IDCZEKK6.js → chunk-34YQ7CVE.js} +3 -3
- package/dist/{chunk-AYWW6JEH.js → chunk-3OLHDECL.js} +115 -41
- package/dist/chunk-3OLHDECL.js.map +1 -0
- package/dist/{chunk-4VWZ4HPL.js → chunk-4DJSDH2M.js} +6 -6
- package/dist/{chunk-YJEOQ72B.js → chunk-6FPJPSQK.js} +2 -2
- package/dist/{chunk-YKUW2EZF.js → chunk-7T7I5D3F.js} +2 -2
- package/dist/{chunk-ERR7ZFPJ.js → chunk-7TC2PWKH.js} +2 -2
- package/dist/{chunk-DH2CDGAE.js → chunk-ALLI23GW.js} +2 -2
- package/dist/{chunk-RTIHO7AJ.js → chunk-AYBRQJXP.js} +2 -2
- package/dist/{chunk-UXXK523X.js → chunk-BR7CZVMO.js} +20 -16
- package/dist/chunk-BR7CZVMO.js.map +1 -0
- package/dist/{chunk-AWD36R4D.js → chunk-DURQZFVJ.js} +36 -6
- package/dist/chunk-DURQZFVJ.js.map +1 -0
- package/dist/{chunk-C7BTGSCY.js → chunk-FPBFDQZZ.js} +9 -9
- package/dist/{chunk-5DCCVEKT.js → chunk-GWFL6LUU.js} +2 -2
- package/dist/{chunk-JTY3NA3I.js → chunk-HJ7M322C.js} +2 -2
- package/dist/{chunk-HURBXNNB.js → chunk-JCPEXUNP.js} +2 -2
- package/dist/{chunk-LKGTTX74.js → chunk-JK2IVISS.js} +2 -2
- package/dist/{chunk-7XG7KMIW.js → chunk-LNQQEFKH.js} +2 -2
- package/dist/{chunk-IB4TDPCC.js → chunk-LR2X3FJM.js} +2 -2
- package/dist/{chunk-6IRJWKR5.js → chunk-LU2GQ4Q4.js} +2 -2
- package/dist/{chunk-4VCASS3Q.js → chunk-MCTGYIYN.js} +2 -2
- package/dist/{chunk-XN43HAIV.js → chunk-MGCV3VGC.js} +2 -2
- package/dist/{chunk-KWVGRNKL.js → chunk-NYZ6ZNFO.js} +2 -2
- package/dist/{chunk-6VAN7VZR.js → chunk-ORMJT23N.js} +7 -7
- package/dist/{chunk-DL7K5PMQ.js → chunk-PPPY2PCU.js} +2 -2
- package/dist/{chunk-5DP4GVKR.js → chunk-PXZLVFNC.js} +2 -2
- package/dist/{chunk-GSCIRMPR.js → chunk-QKYOMKRG.js} +2 -2
- package/dist/{chunk-QF64W4YM.js → chunk-QRZNVNNS.js} +2 -2
- package/dist/{chunk-CQMH65IP.js → chunk-TJJPT2OV.js} +2 -2
- package/dist/{chunk-2AVPRUML.js → chunk-TJY6AKWV.js} +2 -2
- package/dist/{chunk-RRHLI6VU.js → chunk-TQ5NOVMT.js} +2 -2
- package/dist/{chunk-KNT3BZZH.js → chunk-UWU6KEZU.js} +185 -59
- package/dist/chunk-UWU6KEZU.js.map +1 -0
- package/dist/{chunk-PUKLCFCU.js → chunk-VY3DHUL7.js} +2 -2
- package/dist/chunk-VY3DHUL7.js.map +1 -0
- package/dist/{chunk-47W3ALIS.js → chunk-XMKUABIG.js} +3 -3
- package/dist/{chunk-UGCLNASV.js → chunk-XUWDB53W.js} +3 -3
- package/dist/{chunk-XLHLZALR.js → chunk-YCJRYM3Q.js} +3 -3
- package/dist/{chunk-DB4THCGN.js → chunk-YFUHFPV6.js} +2 -2
- package/dist/{chunk-42JE2CF4.js → chunk-YPE3O5VH.js} +2 -2
- package/dist/{chunk-7TJMXZPO.js → chunk-ZO377APE.js} +2 -2
- package/dist/cli.js +38 -38
- package/dist/coding/export-okf-codegraph.js +3 -3
- package/dist/compounding/engine.js +5 -5
- package/dist/config.js +6 -6
- package/dist/connectors/codex-materialize-runner.js +4 -4
- package/dist/connectors/index.js +4 -4
- package/dist/convergence-refresh.js +6 -6
- package/dist/corpus-watermark.js +6 -6
- package/dist/entity-retrieval.js +4 -4
- package/dist/explicit-capture.js +38 -38
- package/dist/extraction-liveness.d.ts +54 -5
- package/dist/extraction-liveness.d.ts.map +1 -1
- package/dist/extraction-liveness.js +1 -1
- package/dist/extraction-span-config.js +2 -2
- package/dist/extraction.js +7 -7
- package/dist/harmonic-retrieval.js +4 -4
- package/dist/index.js +38 -38
- package/dist/location/index.js +5 -5
- package/dist/maintenance/memory-governance.js +4 -4
- package/dist/maintenance/rebuild-memory-lifecycle-ledger.js +4 -4
- package/dist/maintenance/rebuild-memory-projection-runner.js +6 -6
- package/dist/maintenance/rebuild-memory-projection.js +5 -5
- package/dist/namespaces/migrate.js +5 -5
- package/dist/namespaces/storage.js +4 -4
- package/dist/operator-doctor-corpus.js +7 -7
- package/dist/operator-toolkit.js +13 -13
- package/dist/orchestration/backlog-scheduler.js +39 -39
- package/dist/orchestration/buffer-drain-runner.js +39 -39
- package/dist/orchestration/extraction-dedupe.d.ts +46 -0
- package/dist/orchestration/extraction-dedupe.d.ts.map +1 -0
- package/dist/orchestration/extraction-drain.d.ts +12 -1
- package/dist/orchestration/extraction-drain.d.ts.map +1 -1
- package/dist/orchestration/extraction-run.d.ts.map +1 -1
- package/dist/orchestration/maintenance.js +12 -12
- package/dist/orchestrator.js +38 -38
- package/dist/recall-navigation.js +38 -38
- package/dist/resume-bundles.js +7 -7
- package/dist/semantic-consolidation.js +5 -5
- package/dist/semantic-rule-promotion.js +4 -4
- package/dist/semantic-rule-verifier.js +4 -4
- package/dist/source-agent-qualifier.js +6 -6
- package/dist/standup/build.js +4 -4
- package/dist/storage/buffer-drain-claims.d.ts +19 -4
- package/dist/storage/buffer-drain-claims.d.ts.map +1 -1
- package/dist/storage/buffer-drain-claims.js +2 -2
- package/dist/storage/buffer-session-files.js +2 -2
- package/dist/storage/drain-claim-totals.d.ts +54 -0
- package/dist/storage/drain-claim-totals.d.ts.map +1 -0
- package/dist/storage.js +3 -3
- package/dist/transfer/export-okf.js +4 -4
- package/dist/verified-recall.js +4 -4
- package/dist/who-knows-cli.js +5 -5
- package/dist/who-knows.js +4 -4
- package/package.json +2 -2
- package/src/access-service-health.test.ts +102 -1
- package/src/access-service.ts +10 -1
- package/src/buffer-turn-helpers.ts +25 -15
- package/src/buffer-turn.ts +10 -0
- package/src/extraction-liveness.test.ts +229 -0
- package/src/extraction-liveness.ts +89 -6
- package/src/orchestration/extraction-dedupe.test.ts +101 -0
- package/src/orchestration/extraction-dedupe.ts +80 -0
- package/src/orchestration/extraction-drain-dedupe.test.ts +477 -0
- package/src/orchestration/extraction-drain.ts +69 -9
- package/src/orchestration/extraction-run.ts +2 -0
- package/src/storage/buffer-drain-claims.test.ts +66 -0
- package/src/storage/buffer-drain-claims.ts +49 -4
- package/src/storage/drain-claim-totals.test.ts +356 -0
- package/src/storage/drain-claim-totals.ts +175 -0
- package/dist/chunk-AWD36R4D.js.map +0 -1
- package/dist/chunk-AYWW6JEH.js.map +0 -1
- package/dist/chunk-KNT3BZZH.js.map +0 -1
- package/dist/chunk-PUKLCFCU.js.map +0 -1
- package/dist/chunk-UXXK523X.js.map +0 -1
- /package/dist/{chunk-BNRKSJMO.js.map → chunk-2BBCM2PX.js.map} +0 -0
- /package/dist/{chunk-2GSPNTOU.js.map → chunk-2G72JBQB.js.map} +0 -0
- /package/dist/{chunk-IDCZEKK6.js.map → chunk-34YQ7CVE.js.map} +0 -0
- /package/dist/{chunk-4VWZ4HPL.js.map → chunk-4DJSDH2M.js.map} +0 -0
- /package/dist/{chunk-YJEOQ72B.js.map → chunk-6FPJPSQK.js.map} +0 -0
- /package/dist/{chunk-YKUW2EZF.js.map → chunk-7T7I5D3F.js.map} +0 -0
- /package/dist/{chunk-ERR7ZFPJ.js.map → chunk-7TC2PWKH.js.map} +0 -0
- /package/dist/{chunk-DH2CDGAE.js.map → chunk-ALLI23GW.js.map} +0 -0
- /package/dist/{chunk-RTIHO7AJ.js.map → chunk-AYBRQJXP.js.map} +0 -0
- /package/dist/{chunk-C7BTGSCY.js.map → chunk-FPBFDQZZ.js.map} +0 -0
- /package/dist/{chunk-5DCCVEKT.js.map → chunk-GWFL6LUU.js.map} +0 -0
- /package/dist/{chunk-JTY3NA3I.js.map → chunk-HJ7M322C.js.map} +0 -0
- /package/dist/{chunk-HURBXNNB.js.map → chunk-JCPEXUNP.js.map} +0 -0
- /package/dist/{chunk-LKGTTX74.js.map → chunk-JK2IVISS.js.map} +0 -0
- /package/dist/{chunk-7XG7KMIW.js.map → chunk-LNQQEFKH.js.map} +0 -0
- /package/dist/{chunk-IB4TDPCC.js.map → chunk-LR2X3FJM.js.map} +0 -0
- /package/dist/{chunk-6IRJWKR5.js.map → chunk-LU2GQ4Q4.js.map} +0 -0
- /package/dist/{chunk-4VCASS3Q.js.map → chunk-MCTGYIYN.js.map} +0 -0
- /package/dist/{chunk-XN43HAIV.js.map → chunk-MGCV3VGC.js.map} +0 -0
- /package/dist/{chunk-KWVGRNKL.js.map → chunk-NYZ6ZNFO.js.map} +0 -0
- /package/dist/{chunk-6VAN7VZR.js.map → chunk-ORMJT23N.js.map} +0 -0
- /package/dist/{chunk-DL7K5PMQ.js.map → chunk-PPPY2PCU.js.map} +0 -0
- /package/dist/{chunk-5DP4GVKR.js.map → chunk-PXZLVFNC.js.map} +0 -0
- /package/dist/{chunk-GSCIRMPR.js.map → chunk-QKYOMKRG.js.map} +0 -0
- /package/dist/{chunk-QF64W4YM.js.map → chunk-QRZNVNNS.js.map} +0 -0
- /package/dist/{chunk-CQMH65IP.js.map → chunk-TJJPT2OV.js.map} +0 -0
- /package/dist/{chunk-2AVPRUML.js.map → chunk-TJY6AKWV.js.map} +0 -0
- /package/dist/{chunk-RRHLI6VU.js.map → chunk-TQ5NOVMT.js.map} +0 -0
- /package/dist/{chunk-47W3ALIS.js.map → chunk-XMKUABIG.js.map} +0 -0
- /package/dist/{chunk-UGCLNASV.js.map → chunk-XUWDB53W.js.map} +0 -0
- /package/dist/{chunk-XLHLZALR.js.map → chunk-YCJRYM3Q.js.map} +0 -0
- /package/dist/{chunk-DB4THCGN.js.map → chunk-YFUHFPV6.js.map} +0 -0
- /package/dist/{chunk-42JE2CF4.js.map → chunk-YPE3O5VH.js.map} +0 -0
- /package/dist/{chunk-7TJMXZPO.js.map → chunk-ZO377APE.js.map} +0 -0
|
@@ -27,10 +27,11 @@
|
|
|
27
27
|
* inbox, so a kill between the two replays
|
|
28
28
|
* nothing. A torn last line is skipped: its
|
|
29
29
|
* owner never saw the append succeed.
|
|
30
|
-
* s-<sha256(key)>.json.count cached { pending, drained }
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
30
|
+
* s-<sha256(key)>.json.count cached { pending, drained, oldestTurnAt? }
|
|
31
|
+
* turn counts of the body (oldestTurnAt added by
|
|
32
|
+
* #3442; absent in older sidecars), valid while
|
|
33
|
+
* the body's size and mtime match, so listings
|
|
34
|
+
* and the hand-over gate never parse a body.
|
|
34
35
|
*
|
|
35
36
|
* Rolling back to a build that predates the inbox: run `foldAllInboxes()` (CLI:
|
|
36
37
|
* `remnic buffer drain --fold-inboxes`) first; that build never reads an inbox.
|
|
@@ -62,6 +63,7 @@ import path from "node:path";
|
|
|
62
63
|
|
|
63
64
|
import { bufferSessionFileName, type BufferFileIo } from "./buffer-session-files.js";
|
|
64
65
|
import { HandInMemo } from "./buffer-drain-handin-memo.js";
|
|
66
|
+
import { readDrainClaimTotals, type DrainClaimTotals } from "./drain-claim-totals.js";
|
|
65
67
|
import { bufferTurnIdentityKey, subtractBufferTurns, type DrainedTurnSets } from "../buffer-turn-helpers.js";
|
|
66
68
|
import { log } from "../logger.js";
|
|
67
69
|
import { isErrnoCode } from "../utils/errno.js";
|
|
@@ -129,6 +131,13 @@ interface ClaimSummary {
|
|
|
129
131
|
mtimeMs: number;
|
|
130
132
|
pending: number;
|
|
131
133
|
drained: number;
|
|
134
|
+
/**
|
|
135
|
+
* Oldest pending turn timestamp (ISO) across active + retained turns.
|
|
136
|
+
* Absent in pre-#3442 sidecars; readers must treat that as "unknown", not
|
|
137
|
+
* "fresh" (issue #3442: an invisible backlog must not hide behind a
|
|
138
|
+
* missing field).
|
|
139
|
+
*/
|
|
140
|
+
oldestTurnAt?: string;
|
|
132
141
|
}
|
|
133
142
|
|
|
134
143
|
export type ClaimFromSessionOutcome = "claimed" | "empty" | "already_claimed";
|
|
@@ -385,12 +394,26 @@ function pendingTurns(entry: BufferEntryState): number {
|
|
|
385
394
|
return entry.turns.length + (entry.retainedTurns?.length ?? 0);
|
|
386
395
|
}
|
|
387
396
|
|
|
397
|
+
/** Oldest ISO timestamp among `turns`; undefined when none parses (counts stay authoritative regardless). */
|
|
398
|
+
function oldestTurnTimestamp(turns: readonly BufferTurn[]): string | undefined {
|
|
399
|
+
let oldest: string | undefined;
|
|
400
|
+
let oldestMs = Number.POSITIVE_INFINITY;
|
|
401
|
+
for (const turn of turns) {
|
|
402
|
+
const parsed = Date.parse(turn.timestamp);
|
|
403
|
+
if (!Number.isFinite(parsed) || parsed >= oldestMs) continue;
|
|
404
|
+
oldestMs = parsed;
|
|
405
|
+
oldest = turn.timestamp;
|
|
406
|
+
}
|
|
407
|
+
return oldest;
|
|
408
|
+
}
|
|
409
|
+
|
|
388
410
|
const summarySchema = z.object({
|
|
389
411
|
key: z.string().min(1),
|
|
390
412
|
size: z.number(),
|
|
391
413
|
mtimeMs: z.number(),
|
|
392
414
|
pending: z.number(),
|
|
393
415
|
drained: z.number(),
|
|
416
|
+
oldestTurnAt: z.string().optional(),
|
|
394
417
|
});
|
|
395
418
|
|
|
396
419
|
const leaseSchema = z.object({
|
|
@@ -437,6 +460,9 @@ const INBOX_UNREADABLE_BYTES = 0x1fffffe8;
|
|
|
437
460
|
* I/O even when the directory is mostly empty tombstones. */
|
|
438
461
|
const LIST_PAGE_SCAN_FACTOR = 8;
|
|
439
462
|
|
|
463
|
+
/** How long `claimTotals` may serve a cached scan to the health path. */
|
|
464
|
+
const CLAIM_TOTALS_TTL_MS = 30_000;
|
|
465
|
+
|
|
440
466
|
export class BufferDrainClaims {
|
|
441
467
|
constructor(
|
|
442
468
|
private readonly stateDir: string,
|
|
@@ -445,6 +471,24 @@ export class BufferDrainClaims {
|
|
|
445
471
|
|
|
446
472
|
private readonly handIns = new HandInMemo();
|
|
447
473
|
|
|
474
|
+
/** Last `claimTotals` scan; the /health path polls per request (#3442). */
|
|
475
|
+
private totalsCache: { atMs: number; totals: DrainClaimTotals } | null = null;
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Claimed-backlog totals for liveness surfaces (#3442): sidecars only, no
|
|
479
|
+
* claim-body parse, bounded scan, cached for `ttlMs` (default 30s) so a
|
|
480
|
+
* health poll per request cannot turn into a directory storm. `ttlMs: 0`
|
|
481
|
+
* forces a rescan; `nowMs`/`maxClaims` are test/bounds seams.
|
|
482
|
+
*/
|
|
483
|
+
async claimTotals(opts: { ttlMs?: number; maxClaims?: number; nowMs?: number } = {}): Promise<DrainClaimTotals> {
|
|
484
|
+
const nowMs = opts.nowMs ?? Date.now();
|
|
485
|
+
const cached = this.totalsCache;
|
|
486
|
+
if (cached !== null && nowMs - cached.atMs < (opts.ttlMs ?? CLAIM_TOTALS_TTL_MS)) return cached.totals;
|
|
487
|
+
const totals = await readDrainClaimTotals(this.dir, { maxClaims: opts.maxClaims });
|
|
488
|
+
this.totalsCache = { atMs: nowMs, totals };
|
|
489
|
+
return totals;
|
|
490
|
+
}
|
|
491
|
+
|
|
448
492
|
private get dir(): string {
|
|
449
493
|
return path.join(this.stateDir, "buffer", "drain");
|
|
450
494
|
}
|
|
@@ -546,6 +590,7 @@ export class BufferDrainClaims {
|
|
|
546
590
|
mtimeMs: identity.mtimeMs,
|
|
547
591
|
pending: pendingTurns(claim.entry),
|
|
548
592
|
drained: (claim.drained?.length ?? 0) + (claim.drainedRetained?.length ?? 0),
|
|
593
|
+
oldestTurnAt: oldestTurnTimestamp([...claim.entry.turns, ...(claim.entry.retainedTurns ?? [])]),
|
|
549
594
|
};
|
|
550
595
|
try {
|
|
551
596
|
await writeFile(`${bodyPath}${COUNT_SUFFIX}`, JSON.stringify(summary));
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdtempSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import test from "node:test";
|
|
6
|
+
|
|
7
|
+
import { readDrainClaimTotals } from "./drain-claim-totals.js";
|
|
8
|
+
|
|
9
|
+
/** Claim body names look like `s-<64 lowercase hex>.json`; zeros are valid hex. */
|
|
10
|
+
function bodyName(n: number): string {
|
|
11
|
+
return `s-${String(n).padStart(64, "0")}.json`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function setup() {
|
|
15
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), "remnic-drain-totals-"));
|
|
16
|
+
return { dir, cleanup: () => rmSync(dir, { recursive: true, force: true }) };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Write a claim body plus a sidecar whose identity matches the body. */
|
|
20
|
+
function writeClaim(
|
|
21
|
+
dir: string,
|
|
22
|
+
n: number,
|
|
23
|
+
pending: number,
|
|
24
|
+
oldestTurnAt?: string,
|
|
25
|
+
opts: { sidecar?: boolean } = {},
|
|
26
|
+
): void {
|
|
27
|
+
const body = path.join(dir, bodyName(n));
|
|
28
|
+
writeFileSync(body, JSON.stringify({ key: `k${n}`, entry: { turns: [] } }));
|
|
29
|
+
if (opts.sidecar === false) return;
|
|
30
|
+
const identity = statSync(body);
|
|
31
|
+
// JSON.stringify drops an undefined `oldestTurnAt`, which is exactly the
|
|
32
|
+
// legacy (pre-#3442) sidecar shape the back-compat case needs.
|
|
33
|
+
const sidecar = {
|
|
34
|
+
key: `k${n}`,
|
|
35
|
+
size: identity.size,
|
|
36
|
+
mtimeMs: identity.mtimeMs,
|
|
37
|
+
pending,
|
|
38
|
+
drained: 0,
|
|
39
|
+
oldestTurnAt,
|
|
40
|
+
};
|
|
41
|
+
writeFileSync(`${body}.count`, JSON.stringify(sidecar));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Body mtime pinned to `mtimeMs` BEFORE the sidecar identity is captured: simulates a claim whose last write is old (all legacy sidecars on live hosts). */
|
|
45
|
+
function writeClaimWithMtime(dir: string, n: number, pending: number, oldestTurnAt: string | undefined, mtimeMs: number): void {
|
|
46
|
+
const body = path.join(dir, bodyName(n));
|
|
47
|
+
writeFileSync(body, JSON.stringify({ key: `k${n}`, entry: { turns: [] } }));
|
|
48
|
+
utimesSync(body, mtimeMs / 1000, mtimeMs / 1000);
|
|
49
|
+
const identity = statSync(body);
|
|
50
|
+
const sidecar = {
|
|
51
|
+
key: `k${n}`,
|
|
52
|
+
size: identity.size,
|
|
53
|
+
mtimeMs: identity.mtimeMs,
|
|
54
|
+
pending,
|
|
55
|
+
drained: 0,
|
|
56
|
+
oldestTurnAt,
|
|
57
|
+
};
|
|
58
|
+
writeFileSync(`${body}.count`, JSON.stringify(sidecar));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
test("readDrainClaimTotals: sums pending turns and the oldest turn across verified sidecars", async () => {
|
|
62
|
+
const { dir, cleanup } = setup();
|
|
63
|
+
try {
|
|
64
|
+
writeClaim(dir, 1, 5, "2026-11-10T00:00:00.000Z");
|
|
65
|
+
writeClaim(dir, 2, 7, "2026-11-01T00:00:00.000Z");
|
|
66
|
+
const totals = await readDrainClaimTotals(dir);
|
|
67
|
+
assert.deepEqual(totals, {
|
|
68
|
+
claimedSessions: 2,
|
|
69
|
+
claimedPendingTurns: 12,
|
|
70
|
+
oldestClaimedTurnAt: "2026-11-01T00:00:00.000Z",
|
|
71
|
+
oldestClaimedAgeBasis: "turn",
|
|
72
|
+
verifiedSidecars: 2,
|
|
73
|
+
unverifiedClaims: 0,
|
|
74
|
+
claimsPendingInbox: 0,
|
|
75
|
+
truncated: false,
|
|
76
|
+
});
|
|
77
|
+
} finally {
|
|
78
|
+
cleanup();
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("readDrainClaimTotals: a legacy sidecar falls back to the body mtime as a LOWER BOUND on the oldest turn", async () => {
|
|
83
|
+
const { dir, cleanup } = setup();
|
|
84
|
+
try {
|
|
85
|
+
// Body last written 2026-11-01, no sidecar oldestTurnAt: its oldest turn
|
|
86
|
+
// is AT LEAST as old as the body's last write (every body mutation is a
|
|
87
|
+
// whole-file rewrite, so mtime >= newest turn >= oldest turn).
|
|
88
|
+
writeClaimWithMtime(dir, 1, 9, undefined, Date.parse("2026-11-01T00:00:00.000Z"));
|
|
89
|
+
const totals = await readDrainClaimTotals(dir);
|
|
90
|
+
assert.equal(totals.oldestClaimedTurnAt, "2026-11-01T00:00:00.000Z");
|
|
91
|
+
assert.equal(totals.oldestClaimedAgeBasis, "claim-mtime-lower-bound");
|
|
92
|
+
assert.equal(totals.claimedPendingTurns, 9);
|
|
93
|
+
} finally {
|
|
94
|
+
cleanup();
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("readDrainClaimTotals: an explicit oldestTurnAt beats a fresher body mtime (basis turn)", async () => {
|
|
99
|
+
const { dir, cleanup } = setup();
|
|
100
|
+
try {
|
|
101
|
+
// Body freshly written (mtime ≈ now) but the sidecar knows the oldest
|
|
102
|
+
// turn: the turn time wins and the basis says so.
|
|
103
|
+
writeClaim(dir, 1, 4, "2026-11-01T00:00:00.000Z");
|
|
104
|
+
const totals = await readDrainClaimTotals(dir);
|
|
105
|
+
assert.equal(totals.oldestClaimedTurnAt, "2026-11-01T00:00:00.000Z");
|
|
106
|
+
assert.equal(totals.oldestClaimedAgeBasis, "turn");
|
|
107
|
+
} finally {
|
|
108
|
+
cleanup();
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("readDrainClaimTotals: the minimum spans mtime and turn candidates, basis from the winner", async () => {
|
|
113
|
+
const { dir, cleanup } = setup();
|
|
114
|
+
try {
|
|
115
|
+
// Legacy mtime (11-03) is older than the other claim's turn (11-05).
|
|
116
|
+
writeClaimWithMtime(dir, 1, 2, undefined, Date.parse("2026-11-03T00:00:00.000Z"));
|
|
117
|
+
writeClaim(dir, 2, 3, "2026-11-05T00:00:00.000Z");
|
|
118
|
+
let totals = await readDrainClaimTotals(dir);
|
|
119
|
+
assert.equal(totals.oldestClaimedTurnAt, "2026-11-03T00:00:00.000Z");
|
|
120
|
+
assert.equal(totals.oldestClaimedAgeBasis, "claim-mtime-lower-bound");
|
|
121
|
+
|
|
122
|
+
// Reversed: a turn (11-02) older than the other claim's mtime (11-03).
|
|
123
|
+
cleanup();
|
|
124
|
+
const { dir: dir2, cleanup: cleanup2 } = setup();
|
|
125
|
+
writeClaim(dir2, 1, 2, "2026-11-02T00:00:00.000Z");
|
|
126
|
+
writeClaimWithMtime(dir2, 2, 3, undefined, Date.parse("2026-11-03T00:00:00.000Z"));
|
|
127
|
+
totals = await readDrainClaimTotals(dir2);
|
|
128
|
+
assert.equal(totals.oldestClaimedTurnAt, "2026-11-02T00:00:00.000Z");
|
|
129
|
+
assert.equal(totals.oldestClaimedAgeBasis, "turn");
|
|
130
|
+
cleanup2();
|
|
131
|
+
} finally {
|
|
132
|
+
cleanup();
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("readDrainClaimTotals: claims with pending 0 contribute no oldest candidate", async () => {
|
|
137
|
+
const { dir, cleanup } = setup();
|
|
138
|
+
try {
|
|
139
|
+
// Fully drained tombstone with an ancient mtime must not skew the age.
|
|
140
|
+
writeClaimWithMtime(dir, 1, 0, undefined, Date.parse("2020-01-01T00:00:00.000Z"));
|
|
141
|
+
writeClaim(dir, 2, 1, "2026-11-10T00:00:00.000Z");
|
|
142
|
+
const totals = await readDrainClaimTotals(dir);
|
|
143
|
+
assert.equal(totals.oldestClaimedTurnAt, "2026-11-10T00:00:00.000Z");
|
|
144
|
+
assert.equal(totals.oldestClaimedAgeBasis, "turn");
|
|
145
|
+
assert.equal(totals.claimedPendingTurns, 1);
|
|
146
|
+
} finally {
|
|
147
|
+
cleanup();
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("readDrainClaimTotals: a pending hand-in inbox counts the claim and bounds its age even when the body sidecar says pending 0", async () => {
|
|
152
|
+
const { dir, cleanup } = setup();
|
|
153
|
+
try {
|
|
154
|
+
// Body fully drained (sidecar pending 0), but a live owner handed in new
|
|
155
|
+
// turns via the inbox afterwards: those are pending and invisible to the
|
|
156
|
+
// sidecar counts (CodeRabbit round-1 major on drain-claim-totals.ts).
|
|
157
|
+
const body = path.join(dir, bodyName(1));
|
|
158
|
+
writeFileSync(body, JSON.stringify({ key: "k1", entry: { turns: [] } }));
|
|
159
|
+
const identity = statSync(body);
|
|
160
|
+
writeFileSync(`${body}.count`, JSON.stringify({ key: "k1", size: identity.size, mtimeMs: identity.mtimeMs, pending: 0, drained: 2, oldestTurnAt: "2026-01-01T00:00:00.000Z" }));
|
|
161
|
+
// Inbox appended 2026-11-01: every inbox turn predates its last append,
|
|
162
|
+
// so the inbox mtime is a lower bound on the oldest inbox turn's age.
|
|
163
|
+
const inbox = `${body}.inbox.jsonl`;
|
|
164
|
+
writeFileSync(inbox, JSON.stringify({ id: "h1", entry: { turns: [{ role: "user", content: "x", timestamp: "2026-10-30T00:00:00.000Z" }] } }) + "\n");
|
|
165
|
+
utimesSync(inbox, Date.parse("2026-11-01T00:00:00.000Z") / 1000, Date.parse("2026-11-01T00:00:00.000Z") / 1000);
|
|
166
|
+
const totals = await readDrainClaimTotals(dir);
|
|
167
|
+
assert.equal(totals.claimsPendingInbox, 1);
|
|
168
|
+
assert.equal(totals.oldestClaimedTurnAt, "2026-11-01T00:00:00.000Z");
|
|
169
|
+
assert.equal(totals.oldestClaimedAgeBasis, "claim-mtime-lower-bound");
|
|
170
|
+
assert.equal(totals.claimedPendingTurns, 0, "sidecar pending stays authoritative; inbox magnitudes are unknown");
|
|
171
|
+
} finally {
|
|
172
|
+
cleanup();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("readDrainClaimTotals: an empty inbox does not count", async () => {
|
|
177
|
+
const { dir, cleanup } = setup();
|
|
178
|
+
try {
|
|
179
|
+
writeClaim(dir, 1, 4, "2026-11-01T00:00:00.000Z");
|
|
180
|
+
writeFileSync(path.join(dir, `${bodyName(1)}.inbox.jsonl`), "");
|
|
181
|
+
const totals = await readDrainClaimTotals(dir);
|
|
182
|
+
assert.equal(totals.claimsPendingInbox, 0);
|
|
183
|
+
assert.equal(totals.oldestClaimedAgeBasis, "turn");
|
|
184
|
+
} finally {
|
|
185
|
+
cleanup();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("readDrainClaimTotals: the inbox mtime competes with the turn candidate and the true minimum wins", async () => {
|
|
190
|
+
const { dir, cleanup } = setup();
|
|
191
|
+
try {
|
|
192
|
+
// Turn candidate 11-01 is older than the inbox mtime 11-05: turn wins.
|
|
193
|
+
writeClaim(dir, 1, 2, "2026-11-01T00:00:00.000Z");
|
|
194
|
+
const inbox = path.join(dir, `${bodyName(1)}.inbox.jsonl`);
|
|
195
|
+
writeFileSync(inbox, '{"id":"h1"}\n');
|
|
196
|
+
utimesSync(inbox, Date.parse("2026-11-05T00:00:00.000Z") / 1000, Date.parse("2026-11-05T00:00:00.000Z") / 1000);
|
|
197
|
+
let totals = await readDrainClaimTotals(dir);
|
|
198
|
+
assert.equal(totals.oldestClaimedTurnAt, "2026-11-01T00:00:00.000Z");
|
|
199
|
+
assert.equal(totals.oldestClaimedAgeBasis, "turn");
|
|
200
|
+
assert.equal(totals.claimsPendingInbox, 1);
|
|
201
|
+
|
|
202
|
+
// Reversed: an ancient inbox (10-01) beats the turn candidate (11-01).
|
|
203
|
+
cleanup();
|
|
204
|
+
const { dir: dir2, cleanup: cleanup2 } = setup();
|
|
205
|
+
writeClaim(dir2, 1, 2, "2026-11-01T00:00:00.000Z");
|
|
206
|
+
const inbox2 = path.join(dir2, `${bodyName(1)}.inbox.jsonl`);
|
|
207
|
+
writeFileSync(inbox2, '{"id":"h1"}\n');
|
|
208
|
+
utimesSync(inbox2, Date.parse("2026-10-01T00:00:00.000Z") / 1000, Date.parse("2026-10-01T00:00:00.000Z") / 1000);
|
|
209
|
+
totals = await readDrainClaimTotals(dir2);
|
|
210
|
+
assert.equal(totals.oldestClaimedTurnAt, "2026-10-01T00:00:00.000Z");
|
|
211
|
+
assert.equal(totals.oldestClaimedAgeBasis, "claim-mtime-lower-bound");
|
|
212
|
+
cleanup2();
|
|
213
|
+
} finally {
|
|
214
|
+
cleanup();
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("readDrainClaimTotals: an unparsable sidecar oldestTurnAt falls back to the body mtime", async () => {
|
|
219
|
+
const { dir, cleanup } = setup();
|
|
220
|
+
try {
|
|
221
|
+
const body = path.join(dir, bodyName(1));
|
|
222
|
+
writeFileSync(body, JSON.stringify({ key: "k1", entry: { turns: [] } }));
|
|
223
|
+
utimesSync(body, Date.parse("2026-11-02T00:00:00.000Z") / 1000, Date.parse("2026-11-02T00:00:00.000Z") / 1000);
|
|
224
|
+
const identity = statSync(body);
|
|
225
|
+
writeFileSync(`${body}.count`, JSON.stringify({ key: "k1", size: identity.size, mtimeMs: identity.mtimeMs, pending: 3, drained: 0, oldestTurnAt: "not-a-timestamp" }));
|
|
226
|
+
const totals = await readDrainClaimTotals(dir);
|
|
227
|
+
assert.equal(totals.claimedPendingTurns, 3);
|
|
228
|
+
assert.equal(totals.oldestClaimedTurnAt, new Date(identity.mtimeMs).toISOString());
|
|
229
|
+
assert.equal(totals.oldestClaimedAgeBasis, "claim-mtime-lower-bound");
|
|
230
|
+
assert.equal(totals.claimsPendingInbox, 0);
|
|
231
|
+
} finally {
|
|
232
|
+
cleanup();
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("readDrainClaimTotals: a legacy sidecar without oldestTurnAt still counts its pending turns", async () => {
|
|
237
|
+
const { dir, cleanup } = setup();
|
|
238
|
+
try {
|
|
239
|
+
// Pre-#3442 sidecar shape: counts only, no oldest-turn timestamp.
|
|
240
|
+
writeClaim(dir, 1, 9, undefined);
|
|
241
|
+
writeClaim(dir, 2, 1, "2026-11-05T00:00:00.000Z");
|
|
242
|
+
const totals = await readDrainClaimTotals(dir);
|
|
243
|
+
assert.equal(totals.claimedPendingTurns, 10);
|
|
244
|
+
assert.equal(totals.oldestClaimedTurnAt, "2026-11-05T00:00:00.000Z");
|
|
245
|
+
// Claim 1's mtime candidate is ≈ now, so claim 2's turn wins.
|
|
246
|
+
assert.equal(totals.oldestClaimedAgeBasis, "turn");
|
|
247
|
+
assert.equal(totals.verifiedSidecars, 2);
|
|
248
|
+
assert.equal(totals.unverifiedClaims, 0);
|
|
249
|
+
} finally {
|
|
250
|
+
cleanup();
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test("readDrainClaimTotals: a stale sidecar is skipped WITHOUT parsing the body or rewriting the sidecar", async () => {
|
|
255
|
+
const { dir, cleanup } = setup();
|
|
256
|
+
try {
|
|
257
|
+
// Body holds 999 pending turns; the sidecar predates a body rewrite, so
|
|
258
|
+
// its identity no longer matches. The health path must trust NEITHER: it
|
|
259
|
+
// must not parse the body (999 must not be counted) and must not refresh
|
|
260
|
+
// the sidecar (drain owns summary writes).
|
|
261
|
+
const body = path.join(dir, bodyName(1));
|
|
262
|
+
writeFileSync(body, JSON.stringify({ key: "k1", entry: { turns: Array.from({ length: 999 }, (_, i) => ({ role: "user", content: `t${i}`, timestamp: "2026-01-01T00:00:00.000Z" })) } }));
|
|
263
|
+
writeFileSync(`${body}.count`, JSON.stringify({ key: "k1", size: 1, mtimeMs: 1, pending: 3, drained: 0, oldestTurnAt: "2026-02-01T00:00:00.000Z" }));
|
|
264
|
+
const before = readFileSync(`${body}.count`, "utf8");
|
|
265
|
+
const totals = await readDrainClaimTotals(dir);
|
|
266
|
+
assert.deepEqual(
|
|
267
|
+
{ ...totals, oldestClaimedTurnAt: totals.oldestClaimedTurnAt },
|
|
268
|
+
{ claimedSessions: 1, claimedPendingTurns: 0, oldestClaimedTurnAt: null, oldestClaimedAgeBasis: null, verifiedSidecars: 0, unverifiedClaims: 1, claimsPendingInbox: 0, truncated: false },
|
|
269
|
+
);
|
|
270
|
+
assert.equal(readFileSync(`${body}.count`, "utf8"), before, "sidecar must not be rewritten by the health-path reader");
|
|
271
|
+
} finally {
|
|
272
|
+
cleanup();
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("readDrainClaimTotals: a claim body without any sidecar is unverified", async () => {
|
|
277
|
+
const { dir, cleanup } = setup();
|
|
278
|
+
try {
|
|
279
|
+
writeClaim(dir, 1, 4, "2026-11-08T00:00:00.000Z");
|
|
280
|
+
writeClaim(dir, 2, 100, "2026-11-09T00:00:00.000Z", { sidecar: false });
|
|
281
|
+
const totals = await readDrainClaimTotals(dir);
|
|
282
|
+
assert.equal(totals.claimedSessions, 2);
|
|
283
|
+
assert.equal(totals.claimedPendingTurns, 4);
|
|
284
|
+
assert.equal(totals.unverifiedClaims, 1);
|
|
285
|
+
assert.equal(totals.truncated, false);
|
|
286
|
+
} finally {
|
|
287
|
+
cleanup();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("readDrainClaimTotals: maxClaims bounds the sidecar scan but not the session count", async () => {
|
|
292
|
+
const { dir, cleanup } = setup();
|
|
293
|
+
try {
|
|
294
|
+
writeClaim(dir, 1, 3, "2026-11-10T00:00:00.000Z");
|
|
295
|
+
writeClaim(dir, 2, 4, "2026-11-11T00:00:00.000Z");
|
|
296
|
+
writeClaim(dir, 3, 5, "2026-11-12T00:00:00.000Z");
|
|
297
|
+
const totals = await readDrainClaimTotals(dir, { maxClaims: 2 });
|
|
298
|
+
assert.equal(totals.claimedSessions, 3);
|
|
299
|
+
assert.equal(totals.claimedPendingTurns, 7, "only the bounded prefix is summed");
|
|
300
|
+
assert.equal(totals.verifiedSidecars, 2);
|
|
301
|
+
assert.equal(totals.truncated, true);
|
|
302
|
+
} finally {
|
|
303
|
+
cleanup();
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test("readDrainClaimTotals: non-claim files (lease, lock, count, inbox) are not sessions", async () => {
|
|
308
|
+
const { dir, cleanup } = setup();
|
|
309
|
+
try {
|
|
310
|
+
writeClaim(dir, 1, 2, "2026-11-10T00:00:00.000Z");
|
|
311
|
+
// Suffix files around a name that has no body: none of them is a session.
|
|
312
|
+
for (const suffix of [".lease.json", ".lock", ".count", ".inbox.jsonl"]) {
|
|
313
|
+
writeFileSync(path.join(dir, `${bodyName(9)}${suffix}`), "{}");
|
|
314
|
+
}
|
|
315
|
+
writeFileSync(path.join(dir, "not-a-claim.json"), "{}");
|
|
316
|
+
const totals = await readDrainClaimTotals(dir);
|
|
317
|
+
assert.equal(totals.claimedSessions, 1);
|
|
318
|
+
assert.equal(totals.claimedPendingTurns, 2);
|
|
319
|
+
} finally {
|
|
320
|
+
cleanup();
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
test("readDrainClaimTotals: a missing claims directory reads as an empty backlog", async () => {
|
|
325
|
+
const { dir, cleanup } = setup();
|
|
326
|
+
try {
|
|
327
|
+
const totals = await readDrainClaimTotals(path.join(dir, "buffer", "drain"));
|
|
328
|
+
assert.deepEqual(totals, {
|
|
329
|
+
claimedSessions: 0,
|
|
330
|
+
claimedPendingTurns: 0,
|
|
331
|
+
oldestClaimedTurnAt: null,
|
|
332
|
+
oldestClaimedAgeBasis: null,
|
|
333
|
+
verifiedSidecars: 0,
|
|
334
|
+
unverifiedClaims: 0,
|
|
335
|
+
claimsPendingInbox: 0,
|
|
336
|
+
truncated: false,
|
|
337
|
+
});
|
|
338
|
+
} finally {
|
|
339
|
+
cleanup();
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test("readDrainClaimTotals: an unparsable sidecar counts as unverified, not a crash", async () => {
|
|
344
|
+
const { dir, cleanup } = setup();
|
|
345
|
+
try {
|
|
346
|
+
writeClaim(dir, 1, 2, "2026-11-10T00:00:00.000Z");
|
|
347
|
+
writeFileSync(path.join(dir, `${bodyName(2)}.count`), "{not json");
|
|
348
|
+
writeFileSync(path.join(dir, bodyName(2)), "{}");
|
|
349
|
+
const totals = await readDrainClaimTotals(dir);
|
|
350
|
+
assert.equal(totals.claimedSessions, 2);
|
|
351
|
+
assert.equal(totals.unverifiedClaims, 1);
|
|
352
|
+
assert.equal(totals.claimedPendingTurns, 2);
|
|
353
|
+
} finally {
|
|
354
|
+
cleanup();
|
|
355
|
+
}
|
|
356
|
+
});
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cheap drain-claim totals for liveness surfaces (`/health.extraction`, #3442).
|
|
3
|
+
*
|
|
4
|
+
* The in-memory buffer snapshot misses every turn handed to a drain claim, so
|
|
5
|
+
* health can report a tiny backlog while hundreds of thousands of turns sit in
|
|
6
|
+
* `<stateDir>/buffer/drain/`. This reader answers "how much is claimed?" from
|
|
7
|
+
* the `*.json.count` sidecars ALONE: it stats each claim body to validate the
|
|
8
|
+
* sidecar's cached identity (size + mtime, the same rule `readSummary` uses)
|
|
9
|
+
* and never parses a claim body — on the health path a body parse is exactly
|
|
10
|
+
* the multi-GB cost we are avoiding, and stale/missing sidecars count as
|
|
11
|
+
* `unverifiedClaims` instead of triggering one. It also never WRITES: summary
|
|
12
|
+
* refreshes stay owned by the drain path (`readSummary`).
|
|
13
|
+
*
|
|
14
|
+
* Bounded: the sidecar scan stops after `maxClaims` bodies (`truncated: true`),
|
|
15
|
+
* while `claimedSessions` still counts every body from the single readdir, so
|
|
16
|
+
* the session count stays exact even when the scan is capped. Callers cache the
|
|
17
|
+
* result (`BufferDrainClaims.claimTotals`) rather than re-scanning per request.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
|
|
24
|
+
/** How the reported oldest claimed age was derived. */
|
|
25
|
+
export type DrainClaimAgeBasis = "turn" | "claim-mtime-lower-bound";
|
|
26
|
+
|
|
27
|
+
export interface DrainClaimTotals {
|
|
28
|
+
/** Claim bodies in the drain directory: every session handed to a drainer. */
|
|
29
|
+
claimedSessions: number;
|
|
30
|
+
/** Sum of `pending` over sidecars that still match their body's identity. */
|
|
31
|
+
claimedPendingTurns: number;
|
|
32
|
+
/**
|
|
33
|
+
* Oldest oldest-turn candidate over claims with pending > 0; null when none
|
|
34
|
+
* report one. Legacy sidecars (pre-#3442) contribute their BODY MTIME, which
|
|
35
|
+
* is a true LOWER BOUND on the oldest turn's age: every claim-body mutation
|
|
36
|
+
* is a whole-file rewrite, so mtime >= newest turn >= oldest turn.
|
|
37
|
+
*/
|
|
38
|
+
oldestClaimedTurnAt: string | null;
|
|
39
|
+
/** How `oldestClaimedTurnAt` was derived (see the field doc above). */
|
|
40
|
+
oldestClaimedAgeBasis: DrainClaimAgeBasis | null;
|
|
41
|
+
/** Sidecars read and identity-verified this scan. */
|
|
42
|
+
verifiedSidecars: number;
|
|
43
|
+
/** Bodies whose sidecar is missing, unparsable, or stale — excluded from the sums, never parsed to recover. */
|
|
44
|
+
unverifiedClaims: number;
|
|
45
|
+
/**
|
|
46
|
+
* Claims with a non-empty pending-hand-in inbox (`.inbox.jsonl`): their
|
|
47
|
+
* turns are pending but NOT in `claimedPendingTurns` (the sidecar counts
|
|
48
|
+
* only the body), so the pending sum UNDERCOUNTS by this many claims'
|
|
49
|
+
* worth. Each contributes its inbox mtime as a lower-bound age candidate:
|
|
50
|
+
* appends only ever add content, so every inbox turn predates the last
|
|
51
|
+
* append (mtime >= newest inbox turn >= oldest inbox turn).
|
|
52
|
+
*/
|
|
53
|
+
claimsPendingInbox: number;
|
|
54
|
+
/** The sidecar scan stopped at `maxClaims`; bodies past the cap are not summed. */
|
|
55
|
+
truncated: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Default bound on bodies scanned per refresh (jarvis-ml evidence: ~2.4k claims). */
|
|
59
|
+
const DEFAULT_MAX_CLAIMS = 4_096;
|
|
60
|
+
|
|
61
|
+
/** Claim bodies are exactly `s-<64 hex>.json`; leases/locks/counts/inboxes carry suffixes. */
|
|
62
|
+
const CLAIM_BODY_PATTERN = /^s-[0-9a-f]{64}\.json$/;
|
|
63
|
+
|
|
64
|
+
/** The sidecar body contract; `oldestTurnAt` is absent in pre-#3442 sidecars. */
|
|
65
|
+
const sidecarSchema = z.object({
|
|
66
|
+
size: z.number(),
|
|
67
|
+
mtimeMs: z.number(),
|
|
68
|
+
pending: z.number().nonnegative(),
|
|
69
|
+
oldestTurnAt: z.string().optional(),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A sidecar is trusted only while its cached body identity still matches;
|
|
74
|
+
* any deviation (schema, identity, parse) reads as "unverified", never a crash.
|
|
75
|
+
*/
|
|
76
|
+
function verifiedSidecar(
|
|
77
|
+
raw: string,
|
|
78
|
+
identity: { size: number; mtimeMs: number },
|
|
79
|
+
): { pending: number; oldestTurnAt?: string } | null {
|
|
80
|
+
let parsed: z.infer<typeof sidecarSchema>;
|
|
81
|
+
try {
|
|
82
|
+
parsed = sidecarSchema.parse(JSON.parse(raw));
|
|
83
|
+
} catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
if (parsed.size !== identity.size || parsed.mtimeMs !== identity.mtimeMs) return null;
|
|
87
|
+
return { pending: parsed.pending, oldestTurnAt: parsed.oldestTurnAt };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function readDrainClaimTotals(
|
|
91
|
+
claimsDir: string,
|
|
92
|
+
opts: { maxClaims?: number } = {},
|
|
93
|
+
): Promise<DrainClaimTotals> {
|
|
94
|
+
const totals: DrainClaimTotals = {
|
|
95
|
+
claimedSessions: 0,
|
|
96
|
+
claimedPendingTurns: 0,
|
|
97
|
+
oldestClaimedTurnAt: null,
|
|
98
|
+
oldestClaimedAgeBasis: null,
|
|
99
|
+
verifiedSidecars: 0,
|
|
100
|
+
unverifiedClaims: 0,
|
|
101
|
+
claimsPendingInbox: 0,
|
|
102
|
+
truncated: false,
|
|
103
|
+
};
|
|
104
|
+
let oldest: { ms: number; iso: string; basis: DrainClaimAgeBasis } | null = null;
|
|
105
|
+
let bodies: string[];
|
|
106
|
+
try {
|
|
107
|
+
bodies = (await readdir(claimsDir)).filter((name) => CLAIM_BODY_PATTERN.test(name)).sort();
|
|
108
|
+
} catch {
|
|
109
|
+
// No claims directory yet: nothing has ever been claimed. Empty backlog.
|
|
110
|
+
return totals;
|
|
111
|
+
}
|
|
112
|
+
totals.claimedSessions = bodies.length;
|
|
113
|
+
const maxClaims = opts.maxClaims ?? DEFAULT_MAX_CLAIMS;
|
|
114
|
+
if (bodies.length > maxClaims) {
|
|
115
|
+
totals.truncated = true;
|
|
116
|
+
bodies = bodies.slice(0, maxClaims);
|
|
117
|
+
}
|
|
118
|
+
for (const name of bodies) {
|
|
119
|
+
const bodyPath = path.join(claimsDir, name);
|
|
120
|
+
let identity;
|
|
121
|
+
try {
|
|
122
|
+
identity = await stat(bodyPath);
|
|
123
|
+
} catch {
|
|
124
|
+
totals.unverifiedClaims += 1;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
let raw: string;
|
|
128
|
+
try {
|
|
129
|
+
raw = await readFile(`${bodyPath}.count`, "utf8");
|
|
130
|
+
} catch {
|
|
131
|
+
totals.unverifiedClaims += 1;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const sidecar = verifiedSidecar(raw, { size: identity.size, mtimeMs: identity.mtimeMs });
|
|
135
|
+
if (sidecar === null) {
|
|
136
|
+
totals.unverifiedClaims += 1;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
totals.verifiedSidecars += 1;
|
|
140
|
+
totals.claimedPendingTurns += sidecar.pending;
|
|
141
|
+
// Hand-in inbox: appended after the body counts were cached, so a non-empty
|
|
142
|
+
// inbox holds pending turns the sidecar does not know about. One extra
|
|
143
|
+
// stat per claim, no parse — the file only ever grows by appends, so its
|
|
144
|
+
// mtime is a lower bound on the oldest turn inside it.
|
|
145
|
+
const inboxStat = await stat(`${bodyPath}.inbox.jsonl`).catch(() => null);
|
|
146
|
+
const inboxPending = inboxStat !== null && inboxStat.size > 0;
|
|
147
|
+
if (inboxPending) totals.claimsPendingInbox += 1;
|
|
148
|
+
if (sidecar.pending > 0 || inboxPending) {
|
|
149
|
+
// Oldest-age candidates. The sidecar's turn timestamp (when it parses,
|
|
150
|
+
// else the body mtime as a lower bound — see DrainClaimTotals doc)
|
|
151
|
+
// describes the body's turns only while pending > 0; the inbox mtime
|
|
152
|
+
// covers the hand-ins. The per-claim minimum competes in the global
|
|
153
|
+
// minimum with its own basis.
|
|
154
|
+
const candidates: { ms: number; iso: string; basis: DrainClaimAgeBasis }[] = [];
|
|
155
|
+
if (sidecar.pending > 0) {
|
|
156
|
+
const turnIso = sidecar.oldestTurnAt;
|
|
157
|
+
if (turnIso !== undefined) {
|
|
158
|
+
const turnMs = Date.parse(turnIso);
|
|
159
|
+
if (Number.isFinite(turnMs)) candidates.push({ ms: turnMs, iso: turnIso, basis: "turn" });
|
|
160
|
+
else candidates.push({ ms: identity.mtimeMs, iso: new Date(identity.mtimeMs).toISOString(), basis: "claim-mtime-lower-bound" });
|
|
161
|
+
} else {
|
|
162
|
+
candidates.push({ ms: identity.mtimeMs, iso: new Date(identity.mtimeMs).toISOString(), basis: "claim-mtime-lower-bound" });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (inboxPending && inboxStat !== null) {
|
|
166
|
+
candidates.push({ ms: inboxStat.mtimeMs, iso: new Date(inboxStat.mtimeMs).toISOString(), basis: "claim-mtime-lower-bound" });
|
|
167
|
+
}
|
|
168
|
+
const claimOldest = candidates.sort((a, b) => a.ms - b.ms)[0];
|
|
169
|
+
if (claimOldest !== undefined && (oldest === null || claimOldest.ms < oldest.ms)) oldest = claimOldest;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
totals.oldestClaimedTurnAt = oldest?.iso ?? null;
|
|
173
|
+
totals.oldestClaimedAgeBasis = oldest?.basis ?? null;
|
|
174
|
+
return totals;
|
|
175
|
+
}
|