@seanhogg/builderforce-memory 2026.10.45 → 2026.10.46
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/knowledge/build.d.ts.map +1 -1
- package/dist/knowledge/build.js +13 -7
- package/dist/knowledge/build.js.map +1 -1
- package/dist/knowledge/extractFile.d.ts +9 -1
- package/dist/knowledge/extractFile.d.ts.map +1 -1
- package/dist/knowledge/extractFile.js +22 -5
- package/dist/knowledge/extractFile.js.map +1 -1
- package/dist/knowledge/extractJob.d.ts +30 -5
- package/dist/knowledge/extractJob.d.ts.map +1 -1
- package/dist/knowledge/extractJob.js +9 -1
- package/dist/knowledge/extractJob.js.map +1 -1
- package/dist/knowledge/index.d.ts +5 -3
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +2 -1
- package/dist/knowledge/index.js.map +1 -1
- package/dist/knowledge/jobDeadline.d.ts +17 -0
- package/dist/knowledge/jobDeadline.d.ts.map +1 -0
- package/dist/knowledge/jobDeadline.js +46 -0
- package/dist/knowledge/jobDeadline.js.map +1 -0
- package/dist/knowledge/judgeCalls.d.ts +16 -0
- package/dist/knowledge/judgeCalls.d.ts.map +1 -1
- package/dist/knowledge/judgeCalls.js +20 -1
- package/dist/knowledge/judgeCalls.js.map +1 -1
- package/dist/knowledge/judgePriority.d.ts +22 -0
- package/dist/knowledge/judgePriority.d.ts.map +1 -0
- package/dist/knowledge/judgePriority.js +28 -0
- package/dist/knowledge/judgePriority.js.map +1 -0
- package/dist/knowledge/pack/crc32.d.ts +3 -0
- package/dist/knowledge/pack/crc32.d.ts.map +1 -0
- package/dist/knowledge/pack/crc32.js +18 -0
- package/dist/knowledge/pack/crc32.js.map +1 -0
- package/dist/knowledge/pack/decode.d.ts +14 -0
- package/dist/knowledge/pack/decode.d.ts.map +1 -0
- package/dist/knowledge/pack/decode.js +64 -0
- package/dist/knowledge/pack/decode.js.map +1 -0
- package/dist/knowledge/pack/deflate.d.ts +9 -0
- package/dist/knowledge/pack/deflate.d.ts.map +1 -0
- package/dist/knowledge/pack/deflate.js +45 -0
- package/dist/knowledge/pack/deflate.js.map +1 -0
- package/dist/knowledge/pack/encode.d.ts +17 -0
- package/dist/knowledge/pack/encode.d.ts.map +1 -0
- package/dist/knowledge/pack/encode.js +79 -0
- package/dist/knowledge/pack/encode.js.map +1 -0
- package/dist/knowledge/pack/extrDiff.d.ts +23 -0
- package/dist/knowledge/pack/extrDiff.d.ts.map +1 -0
- package/dist/knowledge/pack/extrDiff.js +0 -0
- package/dist/knowledge/pack/extrDiff.js.map +1 -0
- package/dist/knowledge/pack/frame.d.ts +37 -0
- package/dist/knowledge/pack/frame.d.ts.map +1 -0
- package/dist/knowledge/pack/frame.js +94 -0
- package/dist/knowledge/pack/frame.js.map +1 -0
- package/dist/knowledge/pack/index.d.ts +19 -0
- package/dist/knowledge/pack/index.d.ts.map +1 -0
- package/dist/knowledge/pack/index.js +17 -0
- package/dist/knowledge/pack/index.js.map +1 -0
- package/dist/knowledge/pack/jsonSize.d.ts +5 -0
- package/dist/knowledge/pack/jsonSize.d.ts.map +1 -0
- package/dist/knowledge/pack/jsonSize.js +8 -0
- package/dist/knowledge/pack/jsonSize.js.map +1 -0
- package/dist/knowledge/pack/manifest.d.ts +66 -0
- package/dist/knowledge/pack/manifest.d.ts.map +1 -0
- package/dist/knowledge/pack/manifest.js +104 -0
- package/dist/knowledge/pack/manifest.js.map +1 -0
- package/dist/knowledge/pack/sections/columns.d.ts +32 -0
- package/dist/knowledge/pack/sections/columns.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/columns.js +53 -0
- package/dist/knowledge/pack/sections/columns.js.map +1 -0
- package/dist/knowledge/pack/sections/extr.d.ts +8 -0
- package/dist/knowledge/pack/sections/extr.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/extr.js +99 -0
- package/dist/knowledge/pack/sections/extr.js.map +1 -0
- package/dist/knowledge/pack/sections/extrLists.d.ts +23 -0
- package/dist/knowledge/pack/sections/extrLists.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/extrLists.js +194 -0
- package/dist/knowledge/pack/sections/extrLists.js.map +1 -0
- package/dist/knowledge/pack/sections/facts.d.ts +8 -0
- package/dist/knowledge/pack/sections/facts.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/facts.js +167 -0
- package/dist/knowledge/pack/sections/facts.js.map +1 -0
- package/dist/knowledge/pack/sections/index.d.ts +6 -0
- package/dist/knowledge/pack/sections/index.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/index.js +9 -0
- package/dist/knowledge/pack/sections/index.js.map +1 -0
- package/dist/knowledge/pack/sections/rows.d.ts +31 -0
- package/dist/knowledge/pack/sections/rows.d.ts.map +1 -0
- package/dist/knowledge/pack/sections/rows.js +226 -0
- package/dist/knowledge/pack/sections/rows.js.map +1 -0
- package/dist/knowledge/pack/strings.d.ts +39 -0
- package/dist/knowledge/pack/strings.d.ts.map +1 -0
- package/dist/knowledge/pack/strings.js +115 -0
- package/dist/knowledge/pack/strings.js.map +1 -0
- package/dist/knowledge/pack/types.d.ts +109 -0
- package/dist/knowledge/pack/types.d.ts.map +1 -0
- package/dist/knowledge/pack/types.js +17 -0
- package/dist/knowledge/pack/types.js.map +1 -0
- package/dist/knowledge/pack/varint.d.ts +50 -0
- package/dist/knowledge/pack/varint.d.ts.map +1 -0
- package/dist/knowledge/pack/varint.js +141 -0
- package/dist/knowledge/pack/varint.js.map +1 -0
- package/dist/knowledge/partitions.d.ts +1 -1
- package/dist/knowledge/partitions.d.ts.map +1 -1
- package/dist/knowledge/partitions.js +6 -1
- package/dist/knowledge/partitions.js.map +1 -1
- package/dist/knowledge/review.d.ts.map +1 -1
- package/dist/knowledge/review.js +58 -16
- package/dist/knowledge/review.js.map +1 -1
- package/dist/knowledge/reviewJudge.d.ts +8 -1
- package/dist/knowledge/reviewJudge.d.ts.map +1 -1
- package/dist/knowledge/reviewJudge.js +14 -8
- package/dist/knowledge/reviewJudge.js.map +1 -1
- package/dist/knowledge/reviewModel.d.ts +35 -1
- package/dist/knowledge/reviewModel.d.ts.map +1 -1
- package/dist/knowledge/reviewPrefetch.d.ts +8 -2
- package/dist/knowledge/reviewPrefetch.d.ts.map +1 -1
- package/dist/knowledge/reviewPrefetch.js +50 -25
- package/dist/knowledge/reviewPrefetch.js.map +1 -1
- package/dist/knowledge/reviewRun.d.ts +15 -3
- package/dist/knowledge/reviewRun.d.ts.map +1 -1
- package/dist/knowledge/reviewRun.js +19 -2
- package/dist/knowledge/reviewRun.js.map +1 -1
- package/dist/knowledge/reviewRunModel.d.ts +8 -1
- package/dist/knowledge/reviewRunModel.d.ts.map +1 -1
- package/dist/knowledge/reviewRunModel.js.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.d.ts +72 -6
- package/dist/knowledge/stores/localSnapshotStore.d.ts.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.js +279 -75
- package/dist/knowledge/stores/localSnapshotStore.js.map +1 -1
- package/package.json +7 -3
- package/src/knowledge/build.ts +11 -5
- package/src/knowledge/extractFile.ts +26 -6
- package/src/knowledge/extractJob.ts +26 -7
- package/src/knowledge/index.ts +5 -3
- package/src/knowledge/jobDeadline.ts +43 -0
- package/src/knowledge/judgeCalls.ts +19 -2
- package/src/knowledge/judgePriority.ts +36 -0
- package/src/knowledge/pack/crc32.ts +17 -0
- package/src/knowledge/pack/decode.ts +58 -0
- package/src/knowledge/pack/deflate.ts +43 -0
- package/src/knowledge/pack/encode.ts +81 -0
- package/src/knowledge/pack/extrDiff.ts +0 -0
- package/src/knowledge/pack/frame.ts +86 -0
- package/src/knowledge/pack/index.ts +26 -0
- package/src/knowledge/pack/jsonSize.ts +11 -0
- package/src/knowledge/pack/manifest.ts +128 -0
- package/src/knowledge/pack/pack.guards.test.ts +77 -0
- package/src/knowledge/pack/pack.reconcile.test.ts +96 -0
- package/src/knowledge/pack/pack.roundtrip.test.ts +50 -0
- package/src/knowledge/pack/pack.size.test.ts +22 -0
- package/src/knowledge/pack/pack.testkit.test-util.ts +90 -0
- package/src/knowledge/pack/sections/columns.ts +61 -0
- package/src/knowledge/pack/sections/extr.ts +90 -0
- package/src/knowledge/pack/sections/extrLists.ts +147 -0
- package/src/knowledge/pack/sections/facts.ts +123 -0
- package/src/knowledge/pack/sections/index.ts +13 -0
- package/src/knowledge/pack/sections/rows.ts +184 -0
- package/src/knowledge/pack/strings.ts +123 -0
- package/src/knowledge/pack/types.ts +89 -0
- package/src/knowledge/pack/varint.ts +138 -0
- package/src/knowledge/partitions.test.ts +4 -0
- package/src/knowledge/partitions.ts +3 -1
- package/src/knowledge/review.throughput.test.ts +122 -0
- package/src/knowledge/review.ts +54 -16
- package/src/knowledge/review.twoPass.test.ts +224 -0
- package/src/knowledge/reviewJudge.ts +19 -8
- package/src/knowledge/reviewModel.ts +32 -1
- package/src/knowledge/reviewPrefetch.ts +51 -24
- package/src/knowledge/reviewRun.test.ts +20 -3
- package/src/knowledge/reviewRun.ts +33 -6
- package/src/knowledge/reviewRunModel.ts +4 -1
- package/src/knowledge/stores/localSnapshotStore.batched.test.ts +25 -3
- package/src/knowledge/stores/localSnapshotStore.extractedLog.test.ts +280 -0
- package/src/knowledge/stores/localSnapshotStore.ts +283 -76
- package/src/knowledge/systems.test.ts +12 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/knowledge/review.twoPass.test.ts — PRD 41 A10: the map is complete without the model (pass 1,
|
|
3
|
+
* `mechanical`), judging follows in priority order within a budget (pass 2, `only`), the next run judges only what
|
|
4
|
+
* is still owed, a hanging job is a `timeout` failure that never stalls the run, and consecutive provider failures
|
|
5
|
+
* stop judging with the sections left owed.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { hashText } from '../ingest/hash.js';
|
|
9
|
+
import { InMemoryIngestManifest } from '../ingest/IngestionPipeline.js';
|
|
10
|
+
import { DOCS_PACK } from './packs/docs.js';
|
|
11
|
+
import { extractJob } from './extractJob.js';
|
|
12
|
+
import type { ExtractPool } from './extractJob.js';
|
|
13
|
+
import { orderForJudging } from './judgePriority.js';
|
|
14
|
+
import { reviewDirectory } from './review.js';
|
|
15
|
+
import { applyRunEvent, slowestSteps, startRun } from './reviewRun.js';
|
|
16
|
+
import { LocalSnapshotStore } from './stores/localSnapshotStore.js';
|
|
17
|
+
import { args, fakeJudge, memPersistence, memSource, memStore, quietPack, ROOT, stateOf } from './testkit.test-util.js';
|
|
18
|
+
import type { ReviewStep } from './reviewRunModel.js';
|
|
19
|
+
|
|
20
|
+
const N = 6;
|
|
21
|
+
const files = () => Object.fromEntries(Array.from({ length: N }, (_, i) => [`d${i}.md`, `# T${i}\nbody ${i}\n`]));
|
|
22
|
+
const byKey = (a: { key: string }, b: { key: string }) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
23
|
+
const factsOf = async (store: ReturnType<typeof memStore>) => [...(await store.facts({ rootId: ROOT }))].sort(byKey);
|
|
24
|
+
|
|
25
|
+
describe('two-pass review', () => {
|
|
26
|
+
it('pass 1 makes no model call, completes the extract cache and lists every judged file as awaiting judgement', async () => {
|
|
27
|
+
const judge = fakeJudge();
|
|
28
|
+
const store = memStore();
|
|
29
|
+
const manifest = new InMemoryIngestManifest();
|
|
30
|
+
const report = await reviewDirectory(args(memSource(files()), store, manifest, { judgement: judge, mechanical: true, packs: [quietPack] }));
|
|
31
|
+
expect(judge.requests).toHaveLength(0);
|
|
32
|
+
expect(report.awaitingJudge).toEqual(Object.keys(files()));
|
|
33
|
+
expect(report.extracted).toBe(N);
|
|
34
|
+
expect(await factsOf(store)).toEqual([]);
|
|
35
|
+
expect(await store.getExtracted(hashText(files()['d0.md']!))).toBeDefined(); // the map has the file
|
|
36
|
+
expect((await stateOf(manifest)).owed).toEqual(Object.keys(files()));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('pass 2 judges only the files named, and the next run judges only what is still owed', async () => {
|
|
40
|
+
const judge = fakeJudge();
|
|
41
|
+
const store = memStore();
|
|
42
|
+
const manifest = new InMemoryIngestManifest();
|
|
43
|
+
const source = memSource(files());
|
|
44
|
+
const base = { judgement: judge, packs: [quietPack] };
|
|
45
|
+
await reviewDirectory(args(source, store, manifest, { ...base, mechanical: true }));
|
|
46
|
+
const first = await reviewDirectory(args(source, store, manifest, { ...base, only: ['d3.md', 'd1.md'] }));
|
|
47
|
+
const judgedFirst = new Set(judge.requests.flatMap((r) => r.items.map((it) => it.ref as string)));
|
|
48
|
+
expect(judgedFirst.size).toBe(2);
|
|
49
|
+
expect([...judgedFirst].every((r) => r.includes('d1.md') || r.includes('d3.md'))).toBe(true);
|
|
50
|
+
expect((await stateOf(manifest)).owed).toEqual(['d0.md', 'd2.md', 'd4.md', 'd5.md']);
|
|
51
|
+
expect(first.judged).toBe(2);
|
|
52
|
+
|
|
53
|
+
judge.requests.length = 0;
|
|
54
|
+
const next = await reviewDirectory(args(source, store, manifest, base));
|
|
55
|
+
const judgedNext = judge.requests.flatMap((r) => r.items.map((it) => it.ref as string));
|
|
56
|
+
expect(judgedNext).toHaveLength(4);
|
|
57
|
+
expect(judgedNext.some((r) => r.includes('d1.md') || r.includes('d3.md'))).toBe(false);
|
|
58
|
+
expect(next.judged).toBe(4);
|
|
59
|
+
expect((await stateOf(manifest)).owed).toEqual([]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('mechanical pass then pass 2 over every file stores the same facts as one sequential pass', async () => {
|
|
63
|
+
const single = memStore();
|
|
64
|
+
await reviewDirectory(args(memSource(files()), single, new InMemoryIngestManifest(), { judgement: fakeJudge(), packs: [quietPack], concurrency: 1, judgeConcurrency: 1 }));
|
|
65
|
+
|
|
66
|
+
const two = memStore();
|
|
67
|
+
const manifest = new InMemoryIngestManifest();
|
|
68
|
+
const source = memSource(files());
|
|
69
|
+
await reviewDirectory(args(source, two, manifest, { judgement: fakeJudge(), packs: [quietPack], mechanical: true, concurrency: 4 }));
|
|
70
|
+
await reviewDirectory(args(source, two, manifest, { judgement: fakeJudge(), packs: [quietPack], only: Object.keys(files()), concurrency: 4 }));
|
|
71
|
+
expect(await factsOf(two)).toEqual(await factsOf(single));
|
|
72
|
+
const relKey = (r: { from: string; to: string; kind: string }) => `${r.from}|${r.to}|${r.kind}`;
|
|
73
|
+
const sorted = async (st: ReturnType<typeof memStore>) => [...(await st.relations({ rootId: ROOT }))].sort((a, b) => (relKey(a) < relKey(b) ? -1 : 1));
|
|
74
|
+
expect(await sorted(two)).toEqual(await sorted(single));
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('pass 1 owes only what costs a model call, and a partial budget stores what a sequential run stores', () => {
|
|
79
|
+
const K = 3;
|
|
80
|
+
const bytesOf = (p: { value?: string }) => {
|
|
81
|
+
const snap = JSON.parse(p.value!) as { scopes: Array<Record<string, unknown>> };
|
|
82
|
+
for (const sc of snap.scopes) delete sc.lastReviewedAt; // the wall clock of the run
|
|
83
|
+
return JSON.stringify(snap);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
it('mechanical + pass 2 over the first K files equals one sequential run with a budget of K calls, byte for byte', async () => {
|
|
87
|
+
const seq = memPersistence();
|
|
88
|
+
await reviewDirectory(args(memSource(files()), new LocalSnapshotStore(seq, { now: () => 'T' }), new InMemoryIngestManifest(), {
|
|
89
|
+
judgement: fakeJudge(), packs: [quietPack], concurrency: 1, judgeConcurrency: 1, budget: { modelCalls: K, ms: 60_000 },
|
|
90
|
+
}));
|
|
91
|
+
|
|
92
|
+
const two = memPersistence();
|
|
93
|
+
const store = new LocalSnapshotStore(two, { now: () => 'T' });
|
|
94
|
+
const manifest = new InMemoryIngestManifest();
|
|
95
|
+
const source = memSource(files());
|
|
96
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), packs: [quietPack], mechanical: true }));
|
|
97
|
+
await reviewDirectory(args(source, store, manifest, {
|
|
98
|
+
judgement: fakeJudge(), packs: [quietPack], only: Object.keys(files()).slice(0, K), budget: { modelCalls: K, ms: 60_000 },
|
|
99
|
+
}));
|
|
100
|
+
expect(bytesOf(two)).toEqual(bytesOf(seq));
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('a re-review of files whose sections are all still judged owes nothing and calls no model', async () => {
|
|
104
|
+
const store = memStore();
|
|
105
|
+
const manifest = new InMemoryIngestManifest();
|
|
106
|
+
const source = memSource(files());
|
|
107
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), packs: [quietPack] }));
|
|
108
|
+
const judge = fakeJudge();
|
|
109
|
+
const report = await reviewDirectory(args(source, store, manifest, { judgement: judge, packs: [quietPack], mechanical: true, force: true }));
|
|
110
|
+
expect(judge.requests).toHaveLength(0);
|
|
111
|
+
expect(report.awaitingJudge).toEqual([]);
|
|
112
|
+
expect((await stateOf(manifest)).owed).toEqual([]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('pass 2 follows the order it is given and reports what it left owed, unreached files included', async () => {
|
|
116
|
+
const judge = fakeJudge();
|
|
117
|
+
const store = memStore();
|
|
118
|
+
const manifest = new InMemoryIngestManifest();
|
|
119
|
+
const source = memSource(files());
|
|
120
|
+
const base = { judgement: judge, packs: [quietPack] };
|
|
121
|
+
await reviewDirectory(args(source, store, manifest, { ...base, mechanical: true }));
|
|
122
|
+
const order = ['d4.md', 'd0.md', 'd2.md'];
|
|
123
|
+
const report = await reviewDirectory(args(source, store, manifest, { ...base, only: order, concurrency: 1, judgeConcurrency: 1, budget: { modelCalls: 1, ms: 60_000 } }));
|
|
124
|
+
expect(judge.requests.map((r) => r.items[0]!.ref)).toEqual(['src:r::d4.md#'].map((p) => expect.stringContaining(p)));
|
|
125
|
+
expect(report.judgedFiles).toBe(1);
|
|
126
|
+
expect(report.awaitingJudge).toEqual(['d0.md', 'd2.md']);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('per-job timeouts', () => {
|
|
131
|
+
const hang = (path: string): ExtractPool => (job, signal) =>
|
|
132
|
+
job.path === path
|
|
133
|
+
? new Promise((_, reject) => signal?.addEventListener('abort', () => reject(new Error('aborted')), { once: true }))
|
|
134
|
+
: Promise.resolve(extractJob(job, [DOCS_PACK]));
|
|
135
|
+
|
|
136
|
+
it('a hanging extractor is one timeout failure (its worker told to stop) and the run continues', async () => {
|
|
137
|
+
const steps: ReviewStep[] = [];
|
|
138
|
+
const started = Date.now();
|
|
139
|
+
const report = await reviewDirectory(args(memSource(files()), memStore(), new InMemoryIngestManifest(), {
|
|
140
|
+
extractPool: hang('d2.md'), concurrency: 2, timeouts: { extractMs: 60 }, onStep: (s) => steps.push(s),
|
|
141
|
+
}));
|
|
142
|
+
expect(Date.now() - started).toBeLessThan(5_000);
|
|
143
|
+
expect(report.aborted).toBe(false);
|
|
144
|
+
expect(report.extractErrors).toBe(1);
|
|
145
|
+
expect(report.failures).toEqual([{ path: 'd2.md', phase: 'extract', reason: 'timeout' }]);
|
|
146
|
+
expect(report.extracted).toBe(N - 1);
|
|
147
|
+
expect(steps.filter((s) => s.phase === 'extract' && s.outcome === 'failed').map((s) => s.reason)).toEqual(['timeout']);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('a read that never returns is a failed read with a timeout and the file keeps its prior state', async () => {
|
|
151
|
+
const source = memSource(files());
|
|
152
|
+
const read = source.read.bind(source);
|
|
153
|
+
source.read = (path) => (path === 'd1.md' ? new Promise(() => undefined) : read(path));
|
|
154
|
+
const report = await reviewDirectory(args(source, memStore(), new InMemoryIngestManifest(), { timeouts: { readMs: 40 } }));
|
|
155
|
+
expect(report.readErrors).toBe(1);
|
|
156
|
+
expect(report.failures).toEqual([{ path: 'd1.md', phase: 'extract', reason: 'read: timeout' }]);
|
|
157
|
+
expect(report.extracted).toBe(N - 1);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('queued jobs are not timed out while they wait for a pool slot', async () => {
|
|
161
|
+
let busy = 0, peak = 0;
|
|
162
|
+
const slow: ExtractPool = async (job) => {
|
|
163
|
+
busy += 1; peak = Math.max(peak, busy);
|
|
164
|
+
await new Promise((r) => setTimeout(r, 30));
|
|
165
|
+
busy -= 1;
|
|
166
|
+
return extractJob(job, [DOCS_PACK]);
|
|
167
|
+
};
|
|
168
|
+
const report = await reviewDirectory(args(memSource(files()), memStore(), new InMemoryIngestManifest(), {
|
|
169
|
+
extractPool: slow, concurrency: 1, readAhead: 6, timeouts: { extractMs: 200 },
|
|
170
|
+
}));
|
|
171
|
+
expect(peak).toBe(1);
|
|
172
|
+
expect(report.failures).toEqual([]);
|
|
173
|
+
expect(report.extracted).toBe(N);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe('judge breaker', () => {
|
|
178
|
+
it('stops judging after consecutive failures; the unjudged sections stay owed and the next run judges them', async () => {
|
|
179
|
+
let fail = true;
|
|
180
|
+
const calls: string[] = [];
|
|
181
|
+
const judge = {
|
|
182
|
+
async judge(req: Parameters<ReturnType<typeof fakeJudge>['judge']>[0]) {
|
|
183
|
+
calls.push(req.items[0]!.ref as string);
|
|
184
|
+
if (fail) throw new Error('judge: timeout after 60s');
|
|
185
|
+
return fakeJudge().judge(req);
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
const store = memStore();
|
|
189
|
+
const manifest = new InMemoryIngestManifest();
|
|
190
|
+
const source = memSource(files());
|
|
191
|
+
const report = await reviewDirectory(args(source, store, manifest, { judgement: judge, packs: [quietPack], concurrency: 1, judgeConcurrency: 1 }));
|
|
192
|
+
expect(calls.length).toBe(5);
|
|
193
|
+
expect(report.judgeStopped).toMatch(/5 model calls failed in a row/);
|
|
194
|
+
expect(report.judged).toBe(0);
|
|
195
|
+
expect((await stateOf(manifest)).owed).toEqual(Object.keys(files()));
|
|
196
|
+
|
|
197
|
+
fail = false;
|
|
198
|
+
calls.length = 0;
|
|
199
|
+
const next = await reviewDirectory(args(source, store, manifest, { judgement: judge, packs: [quietPack] }));
|
|
200
|
+
expect(next.judged).toBe(N);
|
|
201
|
+
expect((await stateOf(manifest)).owed).toEqual([]);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe('judge priority and the slowest steps', () => {
|
|
206
|
+
it('orders entry points, then inbound references (most first), then recent, then the rest, ties by path', () => {
|
|
207
|
+
const paths = ['z.md', 'b.md', 'a.md', 'recent.md', 'hub.md', 'README.md', 'few.md'];
|
|
208
|
+
const order = orderForJudging(paths, {
|
|
209
|
+
entry: (p) => p === 'README.md',
|
|
210
|
+
inbound: new Map([['hub.md', 9], ['few.md', 2]]),
|
|
211
|
+
recent: new Set(['recent.md']),
|
|
212
|
+
});
|
|
213
|
+
expect(order).toEqual(['README.md', 'hub.md', 'few.md', 'recent.md', 'a.md', 'b.md', 'z.md']);
|
|
214
|
+
expect(paths[0]).toBe('z.md');
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('slowestSteps lists the longest steps first with their reasons', () => {
|
|
218
|
+
let run = startRun({ id: 'r', root: ROOT, kind: 'review', at: 0 });
|
|
219
|
+
for (const [path, ms, reason] of [['a', 5, undefined], ['b', 9000, 'timeout'], ['c', 300, undefined]] as const) {
|
|
220
|
+
run = applyRunEvent(run, { type: 'step', step: { path, phase: 'extract', ms, outcome: reason ? 'failed' : 'ok', ...(reason ? { reason } : {}) } }, 1);
|
|
221
|
+
}
|
|
222
|
+
expect(slowestSteps(run, 2).map((s) => [s.path, s.reason])).toEqual([['b', 'timeout'], ['c', undefined]]);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
import { hashText } from '../ingest/hash.js';
|
|
20
20
|
import { SECTION_INDEX_BADGE } from '../ingest/docsExtract.js';
|
|
21
21
|
import { budgetLeft, type JudgeBudget, type JudgedBatch, type SectionToJudge } from './judge.js';
|
|
22
|
-
import { applyAnswers, callSections, type SectionAnswer } from './judgeCalls.js';
|
|
22
|
+
import { applyAnswers, callSections, type JudgeBreaker, type SectionAnswer } from './judgeCalls.js';
|
|
23
23
|
import type { FileExtraction } from './extractFile.js';
|
|
24
24
|
import type { KnowledgePack, NodeRef, ReviewArgs, ReviewReport, StoredFact } from './model.js';
|
|
25
25
|
import type { ModelLimiter } from './modelLimiter.js';
|
|
@@ -55,23 +55,34 @@ export interface JudgeDispatch {
|
|
|
55
55
|
judgedPrior: ReadonlyMap<string, string>;
|
|
56
56
|
lanes: Lanes;
|
|
57
57
|
limiter: ModelLimiter;
|
|
58
|
+
/** Stops judging for the run after consecutive provider failures. */
|
|
59
|
+
breaker?: JudgeBreaker;
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
/**
|
|
61
|
-
export function
|
|
62
|
+
/** The sections of a file the model has to judge (changed text) and those whose judgement is still current. */
|
|
63
|
+
export function splitSections(
|
|
64
|
+
judgedPrior: ReadonlyMap<string, string>, extraction: FileExtraction,
|
|
65
|
+
): { reuse: NodeRef[]; toJudge: SectionToJudge[] } {
|
|
62
66
|
const { extracted: ex, sections, pack } = extraction;
|
|
63
|
-
const
|
|
64
|
-
if (!port || !judgesPack(port, pack)) return undefined;
|
|
65
|
-
const kinds = new Set(pack.judgement!.nodeKinds);
|
|
67
|
+
const kinds = new Set(pack.judgement?.nodeKinds ?? []);
|
|
66
68
|
const reuse: NodeRef[] = [];
|
|
67
69
|
const toJudge: SectionToJudge[] = [];
|
|
68
70
|
for (const node of ex.nodes) {
|
|
69
71
|
const index = node.badges?.[SECTION_INDEX_BADGE];
|
|
70
72
|
const text = typeof index === 'number' ? sections[index]?.text : undefined;
|
|
71
73
|
if (!kinds.has(node.kind) || !text) continue;
|
|
72
|
-
if (
|
|
74
|
+
if (judgedPrior.get(node.ref) === hashText(text)) reuse.push(node.ref);
|
|
73
75
|
else toJudge.push({ ref: node.ref, text });
|
|
74
76
|
}
|
|
77
|
+
return { reuse, toJudge };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Starts judging a file's changed sections; undefined when its pack asks for no judgement (or there is no port). */
|
|
81
|
+
export function startJudge(d: JudgeDispatch, f: { path: string; hash: string }, extraction: FileExtraction): JudgeJob | undefined {
|
|
82
|
+
const { pack } = extraction;
|
|
83
|
+
const port = d.port;
|
|
84
|
+
if (!port || !judgesPack(port, pack)) return undefined;
|
|
85
|
+
const { reuse, toJudge } = splitSections(d.judgedPrior, extraction);
|
|
75
86
|
// Reserved now, in plan order: this file's calls come out of the shared budget before any later file's.
|
|
76
87
|
const allot = budgetLeft(d.budget) ? Math.min(toJudge.length, Math.max(0, d.budget.modelCalls - d.budget.used)) : 0;
|
|
77
88
|
d.budget.used += allot;
|
|
@@ -81,7 +92,7 @@ export function startJudge(d: JudgeDispatch, f: { path: string; hash: string },
|
|
|
81
92
|
const lane = d.lanes.take();
|
|
82
93
|
const t0 = Date.now();
|
|
83
94
|
try {
|
|
84
|
-
const r = await callSections({ port, fileHash: f.hash, sections: toJudge, budget: slice, limiter: d.limiter, signal: d.signal });
|
|
95
|
+
const r = await callSections({ port, fileHash: f.hash, sections: toJudge, budget: slice, limiter: d.limiter, signal: d.signal, ...(d.breaker ? { breaker: d.breaker } : {}) });
|
|
85
96
|
// Every call that was made failed: the provider is refusing us (key, quota, network), not the file.
|
|
86
97
|
if (r.answers.length === 0 && r.errors > 0) {
|
|
87
98
|
return { status: 'error', reason: `judge: ${r.firstError ?? 'model call failed'}`, ms: Date.now() - t0, lane };
|
|
@@ -15,7 +15,8 @@ export interface DirectorySource {
|
|
|
15
15
|
rootId: string;
|
|
16
16
|
title: string;
|
|
17
17
|
list(): AsyncIterable<{ path: string; hash: string; size: number }>;
|
|
18
|
-
read(
|
|
18
|
+
/** `signal` aborts the read when the review gives up on it (a per-job timeout, a stop); a source that cannot cancel may ignore it. */
|
|
19
|
+
read(path: string, signal?: AbortSignal): Promise<SourceDocument>;
|
|
19
20
|
watch?(cb: (paths: string[]) => void): () => void;
|
|
20
21
|
}
|
|
21
22
|
|
|
@@ -29,6 +30,8 @@ export interface ReviewPlan {
|
|
|
29
30
|
skipped: Array<{ path: string; reason: 'ignored' | 'binary' | 'too-large' | 'no-pack' }>;
|
|
30
31
|
/** ≈ judgement calls for changed files (sections of judged kinds). */
|
|
31
32
|
estimatedModelCalls: number;
|
|
33
|
+
/** The file budget of the judge pass this run will run after the map is complete (PRD 41 A10); set by the host, so the confirm chip states it. */
|
|
34
|
+
judgeFiles?: number;
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
export interface ReviewProgress {
|
|
@@ -59,6 +62,17 @@ export interface ReviewReport {
|
|
|
59
62
|
extractErrors: number;
|
|
60
63
|
/** Every file this run could not finish (read, extract, judge), with why. */
|
|
61
64
|
failures: ReviewFailure[];
|
|
65
|
+
/**
|
|
66
|
+
* Files still owed model judgement after this run (PRD 41 A10): in a `mechanical` run every file of a judged pack
|
|
67
|
+
* with a section to judge; otherwise the files a budget cut or a failed call left. Plan order.
|
|
68
|
+
*/
|
|
69
|
+
awaitingJudge: string[];
|
|
70
|
+
/** How many files are awaiting judgement: the ONLY form that leaves memory — hosts persist and send this, never the path list. */
|
|
71
|
+
awaitingJudgeCount?: number;
|
|
72
|
+
/** Pass 2 (`only`): files of the slice judged to completion. */
|
|
73
|
+
judgedFiles?: number;
|
|
74
|
+
/** Judging was stopped for this run after consecutive provider failures (A10 rule 9); the files stay owed. */
|
|
75
|
+
judgeStopped?: string;
|
|
62
76
|
aborted: boolean;
|
|
63
77
|
durationMs: number;
|
|
64
78
|
}
|
|
@@ -105,6 +119,23 @@ export interface ReviewArgs {
|
|
|
105
119
|
* A result without the file's exact `sourceRef` fails that file's extraction (prior knowledge kept).
|
|
106
120
|
*/
|
|
107
121
|
extractPool?: ExtractPool;
|
|
122
|
+
/**
|
|
123
|
+
* Pass 1 of a two-pass review (PRD 41 A10): read, extract, write — no model call. A file whose pack asks for
|
|
124
|
+
* judgement and has sections to judge stays OWED (manifest hash unchanged) and is listed in `report.awaitingJudge`;
|
|
125
|
+
* the map is complete without it. A later run without `mechanical` (pass 2) judges it.
|
|
126
|
+
*/
|
|
127
|
+
mechanical?: boolean;
|
|
128
|
+
/** Restrict the run to these changed paths (pass 2: the prioritized, budgeted slice). Other changed files are untouched and stay owed. */
|
|
129
|
+
only?: readonly string[];
|
|
130
|
+
/**
|
|
131
|
+
* With an `extractPool`: the pool reads each file itself (PRD 41 A10 rule 8) — the job carries the path and the planned hash,
|
|
132
|
+
* the main thread neither reads nor copies the text, and a binary file comes back as `binary`. The pool must support `readByPool` jobs.
|
|
133
|
+
*/
|
|
134
|
+
deferRead?: boolean;
|
|
135
|
+
/** Files read and extracted ahead of the writer (default: the pool's width times 4, 32..64, with a pool; else `concurrency`). Wider than the pool, so workers never wait for the writer. */
|
|
136
|
+
readAhead?: number;
|
|
137
|
+
/** Per-job ceilings in ms for the extract path (PRD 41 A10): reads 5000, extract (pool dispatch included) 10000. A job over its ceiling is a failed step with reason `timeout`. */
|
|
138
|
+
timeouts?: { readMs?: number; extractMs?: number };
|
|
108
139
|
/** One event per file per phase, as it happens (PRD 41 A8). */
|
|
109
140
|
onStep?(step: ReviewStep): void;
|
|
110
141
|
onProgress?(p: ReviewProgress): void;
|
|
@@ -12,7 +12,10 @@
|
|
|
12
12
|
import type { SourceDocument } from '../ingest/types.js';
|
|
13
13
|
import type { KnowledgePack, KnowledgeStore, NodeRef, ReviewArgs } from './model.js';
|
|
14
14
|
import { extractFileDetailed, type FileExtraction } from './extractFile.js';
|
|
15
|
+
import { BinaryContent } from './extractJob.js';
|
|
15
16
|
import { judgesPack } from './reviewJudge.js';
|
|
17
|
+
import { createDeadlines } from './jobDeadline.js';
|
|
18
|
+
import { createModelLimiter } from './modelLimiter.js';
|
|
16
19
|
import { Lanes, reasonOf, type ReviewStep } from './reviewRunModel.js';
|
|
17
20
|
import { isBinaryText, type ComposedPacks } from './sources.js';
|
|
18
21
|
|
|
@@ -21,9 +24,9 @@ export type Prefetch = (
|
|
|
21
24
|
| { kind: 'read-error'; reason: string }
|
|
22
25
|
| { kind: 'binary' }
|
|
23
26
|
| { kind: 'none' }
|
|
24
|
-
| { kind: 'extracted'; doc
|
|
27
|
+
| { kind: 'extracted'; doc?: SourceDocument; extraction: FileExtraction }
|
|
25
28
|
| { kind: 'failed'; reason: string }
|
|
26
|
-
) & { ms: number; lane: number };
|
|
29
|
+
) & { ms: number; lane: number; parts?: NonNullable<ReviewStep['parts']> };
|
|
27
30
|
|
|
28
31
|
export interface PrefetchContext {
|
|
29
32
|
args: ReviewArgs;
|
|
@@ -34,8 +37,10 @@ export interface PrefetchContext {
|
|
|
34
37
|
store: KnowledgeStore;
|
|
35
38
|
composed: ComposedPacks;
|
|
36
39
|
knownKeys: ReadonlyMap<string, NodeRef>;
|
|
37
|
-
/** Lanes = the read-ahead width. */
|
|
40
|
+
/** Lanes = the read-ahead width: files read and extracted ahead of the writer. */
|
|
38
41
|
width: number;
|
|
42
|
+
/** Files in the extract stage at once (the pool's width); default `width`. */
|
|
43
|
+
extractWidth?: number;
|
|
39
44
|
}
|
|
40
45
|
|
|
41
46
|
/** A {@link Prefetch} before its timing is stamped. */
|
|
@@ -43,7 +48,7 @@ type Outcome = Prefetch extends infer P ? (P extends unknown ? Omit<P, 'ms' | 'l
|
|
|
43
48
|
|
|
44
49
|
/** The extract step event of a settled job. */
|
|
45
50
|
export function extractStepOf(path: string, job: Prefetch): ReviewStep {
|
|
46
|
-
const base = { path, phase: 'extract' as const, ms: job.ms, lane: job.lane };
|
|
51
|
+
const base = { path, phase: 'extract' as const, ms: job.ms, lane: job.lane, ...(job.parts && Object.keys(job.parts).length > 0 ? { parts: job.parts } : {}) };
|
|
47
52
|
switch (job.kind) {
|
|
48
53
|
case 'read-error': return { ...base, outcome: 'failed', reason: `read: ${job.reason}` };
|
|
49
54
|
case 'failed': return { ...base, outcome: 'failed', reason: job.reason };
|
|
@@ -53,34 +58,56 @@ export function extractStepOf(path: string, job: Prefetch): ReviewStep {
|
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
|
|
61
|
+
/** Per-job ceilings of the extract path (PRD 41 A10 rule 2). */
|
|
62
|
+
export const DEFAULT_READ_TIMEOUT_MS = 5_000;
|
|
63
|
+
export const DEFAULT_EXTRACT_TIMEOUT_MS = 10_000;
|
|
64
|
+
|
|
56
65
|
export function createPrefetch(c: PrefetchContext): (f: { path: string; hash: string }) => Promise<Prefetch> {
|
|
57
|
-
|
|
58
|
-
|
|
66
|
+
// A lane is a worker: taken while the file holds an extract slot. The extract stage holds at most `extractWidth` at once, so the
|
|
67
|
+
// extract deadline counts from the moment a job holds a slot, never from its wait in line (the limiter is used as a plain semaphore).
|
|
68
|
+
const lanes = new Lanes(c.extractWidth ?? c.width);
|
|
69
|
+
const extractSlots = createModelLimiter(c.extractWidth ?? c.width);
|
|
70
|
+
const withDeadline = createDeadlines(c.signal);
|
|
71
|
+
const readMs = c.args.timeouts?.readMs ?? DEFAULT_READ_TIMEOUT_MS;
|
|
72
|
+
const extractMs = c.args.timeouts?.extractMs ?? DEFAULT_EXTRACT_TIMEOUT_MS;
|
|
73
|
+
// Judging is a later pass in a mechanical review: no sections are wanted from the pool.
|
|
74
|
+
const wantsSections = (pack: KnowledgePack) => !c.args.mechanical && judgesPack(c.args.judgement, pack);
|
|
75
|
+
const byPool = !!c.args.extractPool && !!c.args.deferRead;
|
|
59
76
|
|
|
60
|
-
const run = async (f: { path: string; hash: string }): Promise<Outcome> => {
|
|
61
|
-
let doc: SourceDocument;
|
|
62
|
-
|
|
77
|
+
const run = async (f: { path: string; hash: string }, parts: NonNullable<ReviewStep['parts']>, held: { lane: number }): Promise<Outcome> => {
|
|
78
|
+
let doc: SourceDocument | undefined;
|
|
79
|
+
const t0 = Date.now();
|
|
80
|
+
if (!byPool) {
|
|
81
|
+
try { doc = await withDeadline(readMs, (signal) => c.args.source.read(f.path, signal)); } catch (error) {
|
|
82
|
+
return { kind: 'read-error', reason: reasonOf(error) };
|
|
83
|
+
}
|
|
84
|
+
parts.readMs = Date.now() - t0;
|
|
85
|
+
}
|
|
63
86
|
try {
|
|
64
|
-
if (doc.content.kind === 'text' && isBinaryText(doc.content.text)) return { kind: 'binary' };
|
|
65
|
-
const extraction = await
|
|
66
|
-
|
|
87
|
+
if (doc && doc.content.kind === 'text' && isBinaryText(doc.content.text)) return { kind: 'binary' };
|
|
88
|
+
const extraction = await extractSlots.run(async () => {
|
|
89
|
+
held.lane = lanes.take();
|
|
90
|
+
try { return await withDeadline(extractMs, (signal) => extractFileDetailed({
|
|
91
|
+
rootId: c.rootId, path: f.path, hash: f.hash, ...(doc ? { doc } : {}), packs: c.packs, store: c.store, composed: c.composed,
|
|
67
92
|
knownKeys: c.knownKeys, writeCache: false,
|
|
68
|
-
|
|
93
|
+
onTiming: (t) => { parts.lookupMs = t.lookupMs; parts.poolMs = t.poolMs; },
|
|
94
|
+
...(c.args.extractPool ? { pool: c.args.extractPool, signal, wantsSections, ...(byPool ? { readByPool: true } : {}) } : {}),
|
|
95
|
+
})); } finally { lanes.free(held.lane); }
|
|
69
96
|
});
|
|
70
|
-
return extraction ? { kind: 'extracted', doc, extraction } : { kind: 'none' };
|
|
71
|
-
} catch (error) {
|
|
97
|
+
return extraction ? { kind: 'extracted', ...(doc ? { doc } : {}), extraction } : { kind: 'none' };
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (error instanceof BinaryContent) return { kind: 'binary' };
|
|
100
|
+
return { kind: 'failed', reason: reasonOf(error) };
|
|
101
|
+
}
|
|
72
102
|
};
|
|
73
103
|
|
|
74
104
|
return async (f) => {
|
|
75
|
-
const lane = lanes.take();
|
|
76
105
|
const t0 = Date.now();
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
lanes.free(lane);
|
|
84
|
-
}
|
|
106
|
+
const parts: NonNullable<ReviewStep['parts']> = {};
|
|
107
|
+
const held = { lane: 0 };
|
|
108
|
+
const out = await run(f, parts, held);
|
|
109
|
+
const job = { ...out, ms: Date.now() - t0, lane: held.lane, parts } as Prefetch;
|
|
110
|
+
c.args.onStep?.(extractStepOf(f.path, job));
|
|
111
|
+
return job;
|
|
85
112
|
};
|
|
86
113
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* The pure run recorder (PRD 41 A8): phases, lanes, the step ring, failures, rate and ETA.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { applyRunEvent, REVIEW_RUN_STEP_RING, runEta, runRate, startRun, type ReviewRunEvent } from './reviewRun.js';
|
|
6
|
+
import { applyRunEvent, failuresOf, failureTotal, REVIEW_RUN_FAILURE_CHUNK, REVIEW_RUN_STEP_RING, runEta, runRate, startRun, type ReviewRunEvent } from './reviewRun.js';
|
|
7
7
|
import type { ReviewStep } from './reviewRunModel.js';
|
|
8
8
|
|
|
9
9
|
const step = (i: number, extra: Partial<ReviewStep> = {}): ReviewStep => ({ path: `f${i}.md`, phase: 'extract', ms: 10, outcome: 'ok', lane: i % 2, ...extra });
|
|
@@ -40,8 +40,25 @@ describe('reviewRun reducer', () => {
|
|
|
40
40
|
expect(run.steps).toHaveLength(REVIEW_RUN_STEP_RING);
|
|
41
41
|
expect(run.steps[0]!.path).toBe('f50.md');
|
|
42
42
|
expect(run.steps.at(-1)!.path).toBe('f249.md');
|
|
43
|
-
expect(run.
|
|
44
|
-
expect(run
|
|
43
|
+
expect(failuresOf(run).map((f) => f.reason)).toEqual(['boom 0', 'boom 50', 'boom 100', 'boom 150', 'boom 200']);
|
|
44
|
+
expect(failuresOf(run)[0]).toEqual({ path: 'f0.md', phase: 'extract', reason: 'boom 0' });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('keeps failures in chunks: an append copies the last chunk and the chunk list, never every failure, and the input run is untouched', () => {
|
|
48
|
+
let run = run0();
|
|
49
|
+
const N = 20_000;
|
|
50
|
+
const t0 = Date.now();
|
|
51
|
+
for (let i = 0; i < N; i++) {
|
|
52
|
+
const before = run.failures;
|
|
53
|
+
const sizes = before.map((c) => c.length);
|
|
54
|
+
run = applyRunEvent(run, { type: 'step', step: step(i, { outcome: 'failed', reason: 'timeout' }) }, 2000 + i);
|
|
55
|
+
if (i % 5000 === 0) expect(before.map((c) => c.length)).toEqual(sizes); // the previous run's chunks did not grow
|
|
56
|
+
}
|
|
57
|
+
expect(failureTotal(run)).toBe(N);
|
|
58
|
+
expect(run.failures.every((c) => c.length <= REVIEW_RUN_FAILURE_CHUNK)).toBe(true);
|
|
59
|
+
expect(run.failures).toHaveLength(Math.ceil(N / REVIEW_RUN_FAILURE_CHUNK));
|
|
60
|
+
expect(failuresOf(run).map((f) => f.path)[N - 1]).toBe(`f${N - 1}.md`);
|
|
61
|
+
expect(Date.now() - t0).toBeLessThan(5_000); // linear: 20,000 whole-array copies would take far longer
|
|
45
62
|
});
|
|
46
63
|
|
|
47
64
|
it('keeps an ended phase ended on a stale event, and reopens it only when the total grows', () => {
|
|
@@ -16,6 +16,11 @@ export type ReviewRunOutcome = 'done' | 'stopped' | 'failed';
|
|
|
16
16
|
/** How many of the latest steps a run keeps (failures are kept whole). */
|
|
17
17
|
export const REVIEW_RUN_STEP_RING = 200;
|
|
18
18
|
|
|
19
|
+
/** Failures are kept in chunks of at most this many, so appending copies one chunk and the outer list, never every failure. */
|
|
20
|
+
export const REVIEW_RUN_FAILURE_CHUNK = 256;
|
|
21
|
+
|
|
22
|
+
export type ReviewFailureChunks = readonly (readonly ReviewFailure[])[];
|
|
23
|
+
|
|
19
24
|
export interface ReviewRunPhase {
|
|
20
25
|
phase: ReviewPhase;
|
|
21
26
|
startedAt: number;
|
|
@@ -46,7 +51,8 @@ export interface ReviewRun {
|
|
|
46
51
|
lanes: ReviewRunLane[];
|
|
47
52
|
/** The last {@link REVIEW_RUN_STEP_RING} steps, oldest first. */
|
|
48
53
|
steps: ReviewStep[];
|
|
49
|
-
|
|
54
|
+
/** Every failure, in chunks (see {@link REVIEW_RUN_FAILURE_CHUNK}); read it through {@link failuresOf}. */
|
|
55
|
+
failures: ReviewFailureChunks;
|
|
50
56
|
outcome?: ReviewRunOutcome;
|
|
51
57
|
report?: ReviewReport;
|
|
52
58
|
}
|
|
@@ -76,7 +82,7 @@ function withProgress(phases: ReviewRunPhase[], p: ReviewProgress, at: number):
|
|
|
76
82
|
return phases.map((x, j) => (j === i ? next : x));
|
|
77
83
|
}
|
|
78
84
|
|
|
79
|
-
const PHASE_ORDER: readonly ReviewPhase[] = ['plan', 'extract', 'resolve', 'judge', 'compare', 'write', 'index', 'done'];
|
|
85
|
+
const PHASE_ORDER: readonly ReviewPhase[] = ['plan', 'extract', 'resolve', 'judge', 'compare', 'write', 'index', 'mapped', 'done'];
|
|
80
86
|
|
|
81
87
|
/** Lanes are keyed by phase and id: extract workers and judge lanes are different workers. */
|
|
82
88
|
function withLane(lanes: ReviewRunLane[], step: ReviewStep): ReviewRunLane[] {
|
|
@@ -90,6 +96,19 @@ function withLane(lanes: ReviewRunLane[], step: ReviewStep): ReviewRunLane[] {
|
|
|
90
96
|
.sort((a, b) => PHASE_ORDER.indexOf(a.phase) - PHASE_ORDER.indexOf(b.phase) || a.id - b.id);
|
|
91
97
|
}
|
|
92
98
|
|
|
99
|
+
/** The chunks with `f` appended: copies the last chunk and the outer list only (linear over a run of thousands of failures). */
|
|
100
|
+
function withFailure(chunks: ReviewFailureChunks, f: ReviewFailure): ReviewFailureChunks {
|
|
101
|
+
const last = chunks[chunks.length - 1];
|
|
102
|
+
if (last && last.length < REVIEW_RUN_FAILURE_CHUNK) return [...chunks.slice(0, -1), [...last, f]];
|
|
103
|
+
return [...chunks, [f]];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Every failure of the run, oldest first (for readers; the run keeps them in chunks). */
|
|
107
|
+
export const failuresOf = (run: Pick<ReviewRun, 'failures'>): ReviewFailure[] => run.failures.flat();
|
|
108
|
+
|
|
109
|
+
/** How many failures the run holds. */
|
|
110
|
+
export const failureTotal = (run: Pick<ReviewRun, 'failures'>): number => run.failures.reduce((n, c) => n + c.length, 0);
|
|
111
|
+
|
|
93
112
|
/** Folds one event into the run; the input run is never mutated. */
|
|
94
113
|
export function applyRunEvent(run: ReviewRun, event: ReviewRunEvent, at: number): ReviewRun {
|
|
95
114
|
const base = { ...run, updatedAt: at };
|
|
@@ -105,7 +124,7 @@ export function applyRunEvent(run: ReviewRun, event: ReviewRunEvent, at: number)
|
|
|
105
124
|
...base,
|
|
106
125
|
lanes: withLane(run.lanes, s),
|
|
107
126
|
steps: [...run.steps, s].slice(-REVIEW_RUN_STEP_RING),
|
|
108
|
-
failures: s.outcome === 'failed' ?
|
|
127
|
+
failures: s.outcome === 'failed' ? withFailure(run.failures, { path: s.path, phase: s.phase, reason: s.reason ?? 'failed' }) : run.failures,
|
|
109
128
|
};
|
|
110
129
|
}
|
|
111
130
|
case 'end':
|
|
@@ -116,10 +135,13 @@ export function applyRunEvent(run: ReviewRun, event: ReviewRunEvent, at: number)
|
|
|
116
135
|
}
|
|
117
136
|
}
|
|
118
137
|
|
|
119
|
-
|
|
138
|
+
/** What the rate and the ETA read of a run (a restated run record qualifies too). */
|
|
139
|
+
export type RunClock = Pick<ReviewRun, 'phases' | 'endedAt' | 'updatedAt'>;
|
|
140
|
+
|
|
141
|
+
const phaseOf = (run: RunClock, phase: ReviewPhase): ReviewRunPhase | undefined => run.phases.find((p) => p.phase === phase);
|
|
120
142
|
|
|
121
143
|
/** Items per second a phase has sustained so far; 0 when it has not started or no time has passed. */
|
|
122
|
-
export function runRate(run:
|
|
144
|
+
export function runRate(run: RunClock, phase: ReviewPhase): number {
|
|
123
145
|
const p = phaseOf(run, phase);
|
|
124
146
|
if (!p) return 0;
|
|
125
147
|
const ms = (p.endedAt ?? run.endedAt ?? run.updatedAt) - p.startedAt;
|
|
@@ -127,7 +149,7 @@ export function runRate(run: ReviewRun, phase: ReviewPhase): number {
|
|
|
127
149
|
}
|
|
128
150
|
|
|
129
151
|
/** Milliseconds until a phase finishes at its measured rate; 0 when it is complete, undefined when it cannot be told. */
|
|
130
|
-
export function runEta(run:
|
|
152
|
+
export function runEta(run: RunClock, phase: ReviewPhase): number | undefined {
|
|
131
153
|
const p = phaseOf(run, phase);
|
|
132
154
|
if (!p) return undefined;
|
|
133
155
|
const remaining = p.total - p.done;
|
|
@@ -135,3 +157,8 @@ export function runEta(run: ReviewRun, phase: ReviewPhase): number | undefined {
|
|
|
135
157
|
const rate = runRate(run, phase);
|
|
136
158
|
return rate > 0 ? Math.round((remaining / rate) * 1000) : undefined;
|
|
137
159
|
}
|
|
160
|
+
|
|
161
|
+
/** The `n` slowest steps of the run's ring, longest first (ties by path), each with its reason: a stall names itself. */
|
|
162
|
+
export function slowestSteps(run: Pick<ReviewRun, 'steps'>, n: number): ReviewStep[] {
|
|
163
|
+
return [...run.steps].sort((a, b) => b.ms - a.ms || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0)).slice(0, Math.max(0, n));
|
|
164
|
+
}
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* per-file step events a review emits, and the failures it records instead of throwing.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/** `mapped` (PRD 41 A10): the mechanical pass is over and the map is complete; judging, if any is owed, follows. */
|
|
7
|
+
export type ReviewPhase = 'plan' | 'extract' | 'resolve' | 'judge' | 'compare' | 'write' | 'index' | 'mapped' | 'done';
|
|
7
8
|
|
|
8
9
|
export type ReviewStepOutcome = 'ok' | 'cached' | 'skipped' | 'failed';
|
|
9
10
|
|
|
@@ -18,6 +19,8 @@ export interface ReviewStep {
|
|
|
18
19
|
outcome: ReviewStepOutcome;
|
|
19
20
|
reason?: string;
|
|
20
21
|
lane?: number;
|
|
22
|
+
/** Where an extract step's time went (PRD 41 A10), so a stall names itself: the file read, the cache lookup, the pool job. */
|
|
23
|
+
parts?: { readMs?: number; lookupMs?: number; poolMs?: number };
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
/** A file a review could not finish: its prior knowledge and manifest hash are kept, so the next review retries it. */
|