@seanhogg/builderforce-memory 2026.10.32 → 2026.10.34
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/README.md +144 -0
- package/dist/knowledge/index.d.ts +2 -1
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +1 -1
- package/dist/knowledge/index.js.map +1 -1
- package/dist/knowledge/judge.d.ts +20 -1
- package/dist/knowledge/judge.d.ts.map +1 -1
- package/dist/knowledge/judge.js +32 -3
- package/dist/knowledge/judge.js.map +1 -1
- package/dist/knowledge/owedFiles.d.ts +68 -0
- package/dist/knowledge/owedFiles.d.ts.map +1 -0
- package/dist/knowledge/owedFiles.js +86 -0
- package/dist/knowledge/owedFiles.js.map +1 -0
- package/dist/knowledge/plan.d.ts +22 -3
- package/dist/knowledge/plan.d.ts.map +1 -1
- package/dist/knowledge/plan.js +43 -18
- package/dist/knowledge/plan.js.map +1 -1
- package/dist/knowledge/review.d.ts +3 -1
- package/dist/knowledge/review.d.ts.map +1 -1
- package/dist/knowledge/review.js +43 -13
- package/dist/knowledge/review.js.map +1 -1
- package/dist/knowledge/reviewCompare.d.ts +6 -5
- package/dist/knowledge/reviewCompare.d.ts.map +1 -1
- package/dist/knowledge/reviewCompare.js +27 -16
- package/dist/knowledge/reviewCompare.js.map +1 -1
- package/dist/knowledge/reviewModel.d.ts +8 -2
- package/dist/knowledge/reviewModel.d.ts.map +1 -1
- package/dist/knowledge/sources.d.ts +5 -7
- package/dist/knowledge/sources.d.ts.map +1 -1
- package/dist/knowledge/sources.js +5 -12
- package/dist/knowledge/sources.js.map +1 -1
- package/package.json +3 -3
- package/src/knowledge/index.ts +3 -1
- package/src/knowledge/judge.clamp.test.ts +82 -0
- package/src/knowledge/judge.ts +33 -3
- package/src/knowledge/owedFiles.ts +108 -0
- package/src/knowledge/plan.ts +61 -19
- package/src/knowledge/review.estimate.test.ts +112 -0
- package/src/knowledge/review.owed.test.ts +379 -0
- package/src/knowledge/review.ts +39 -14
- package/src/knowledge/review.upgrade.test.ts +86 -21
- package/src/knowledge/reviewCompare.ts +26 -14
- package/src/knowledge/reviewModel.ts +8 -2
- package/src/knowledge/sources.ts +5 -14
- package/src/knowledge/testkit.test-util.ts +24 -1
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/knowledge/review.owed.test.ts
|
|
3
|
+
* The extractor entry carries `{ version, owed }`: an upgrade run cut short, a file that
|
|
4
|
+
* cannot be read, an abort, a removed or ignored path — whatever a run leaves unfinished is
|
|
5
|
+
* owed to the next review / *Continue*, whatever its content does meanwhile.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { InMemoryIngestManifest } from '../ingest/IngestionPipeline.js';
|
|
9
|
+
import type { SourceDocument } from '../ingest/types.js';
|
|
10
|
+
import type { ReviewArgs } from './model.js';
|
|
11
|
+
import { loadExtractorState } from './owedFiles.js';
|
|
12
|
+
import { DOCS_PACK } from './packs/docs.js';
|
|
13
|
+
import { planDetail, planReview } from './plan.js';
|
|
14
|
+
import { reviewDirectory } from './review.js';
|
|
15
|
+
import {
|
|
16
|
+
compareManifestKey, EXTRACTOR_VERSION, extractorManifestKey, filesManifestKey, judgedManifestKey, manifestMap, saveManifestMap,
|
|
17
|
+
} from './sources.js';
|
|
18
|
+
import { ALL, args, fakeJudge, FILES, memSource, memStore, ref, ROOT, similar, stateOf, type SpySource } from './testkit.test-util.js';
|
|
19
|
+
|
|
20
|
+
const CURRENT = EXTRACTOR_VERSION;
|
|
21
|
+
const Q_TEXT = '# Q\nquux\n';
|
|
22
|
+
const TEXT = 'Intro text.\n# Body\nunder the heading\n';
|
|
23
|
+
const PREAMBLE = `${ref('p.md')}#~preamble`;
|
|
24
|
+
/** p.md has a preamble section, q.md one section. */
|
|
25
|
+
const PQ = { 'p.md': TEXT, 'q.md': Q_TEXT };
|
|
26
|
+
|
|
27
|
+
/** A reviewed root, the extractor entry deleted: the next review is the upgrade run. */
|
|
28
|
+
async function reviewed(files: Record<string, string> = FILES, extra: Partial<ReviewArgs> = {}) {
|
|
29
|
+
const source = memSource({ ...files });
|
|
30
|
+
const store = memStore();
|
|
31
|
+
const manifest = new InMemoryIngestManifest();
|
|
32
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), ...extra }));
|
|
33
|
+
await manifest.delete(extractorManifestKey(ROOT));
|
|
34
|
+
return { source, store, manifest };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** `path` throws on read, as a permission error would; returns the original `read`. */
|
|
38
|
+
function unreadable(source: SpySource, path: string) {
|
|
39
|
+
const read = source.read.bind(source);
|
|
40
|
+
source.read = async (p) => {
|
|
41
|
+
if (p !== path) return read(p);
|
|
42
|
+
source.reads.push(p);
|
|
43
|
+
throw Object.assign(new Error('permission denied'), { code: 'EACCES' });
|
|
44
|
+
};
|
|
45
|
+
return read;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** What the upgraded extractor adds to p.md: a preamble section nobody has judged. */
|
|
49
|
+
async function forgetPreamble(manifest: InMemoryIngestManifest) {
|
|
50
|
+
const judged = await manifestMap(manifest, judgedManifestKey(ROOT));
|
|
51
|
+
expect(judged.delete(PREAMBLE)).toBe(true);
|
|
52
|
+
await saveManifestMap(manifest, judgedManifestKey(ROOT), judged);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The upgrade has run with the budget spent: p.md's preamble section, new to the extractor, was not
|
|
57
|
+
* judged. The first review had a `similar` port, so no claims are left awaiting a compare.
|
|
58
|
+
*/
|
|
59
|
+
async function upgraded() {
|
|
60
|
+
const { source, store, manifest } = await reviewed(PQ, { similar });
|
|
61
|
+
await forgetPreamble(manifest);
|
|
62
|
+
const detail = await planDetail(source, manifest, [DOCS_PACK]);
|
|
63
|
+
expect(detail.files.map((f) => [f.path, f.changed, f.owed])).toEqual([['p.md', true, true], ['q.md', true, true]]);
|
|
64
|
+
const cut = await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), similar, budget: { modelCalls: 0, ms: 60_000 } }));
|
|
65
|
+
expect((await manifestMap(manifest, compareManifestKey(ROOT))).size).toBe(0);
|
|
66
|
+
expect(cut.unjudged).toBe(1);
|
|
67
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['p.md'] });
|
|
68
|
+
source.reads.length = 0;
|
|
69
|
+
return { source, store, manifest, detail };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
describe('an upgrade run cut by the budget on an unchanged file', () => {
|
|
73
|
+
it('records the file with the calls it still costs', async () => {
|
|
74
|
+
const { manifest } = await upgraded();
|
|
75
|
+
expect(await manifest.get(extractorManifestKey(ROOT))).toEqual([
|
|
76
|
+
{ id: 'version', hash: String(CURRENT) }, { id: 'owed:p.md', hash: '1' },
|
|
77
|
+
]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('the next review judges the new section, once', async () => {
|
|
81
|
+
const { source, store, manifest } = await upgraded();
|
|
82
|
+
const judge = fakeJudge();
|
|
83
|
+
await reviewDirectory(args(source, store, manifest, { judgement: judge }));
|
|
84
|
+
expect(judge.requests.map((r) => r.items.map((i) => i.ref))).toEqual([[PREAMBLE]]);
|
|
85
|
+
expect(source.reads).toEqual(['p.md']);
|
|
86
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
87
|
+
|
|
88
|
+
const third = fakeJudge();
|
|
89
|
+
source.reads.length = 0;
|
|
90
|
+
await reviewDirectory(args(source, store, manifest, { judgement: third }));
|
|
91
|
+
expect(third.requests).toHaveLength(0);
|
|
92
|
+
expect(source.reads).toEqual([]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('Continue with the same plan picks the owed file up without a re-walk, once', async () => {
|
|
96
|
+
const { source, store, manifest, detail } = await upgraded();
|
|
97
|
+
const judge = fakeJudge();
|
|
98
|
+
await reviewDirectory(args(source, store, manifest, { judgement: judge, plan: detail }));
|
|
99
|
+
expect(judge.requests.map((r) => r.items.map((i) => i.ref))).toEqual([[PREAMBLE]]);
|
|
100
|
+
expect(source.reads).toEqual(['p.md']);
|
|
101
|
+
|
|
102
|
+
const third = fakeJudge();
|
|
103
|
+
source.reads.length = 0;
|
|
104
|
+
await reviewDirectory(args(source, store, manifest, { judgement: third, plan: detail }));
|
|
105
|
+
expect(third.requests).toHaveLength(0);
|
|
106
|
+
expect(source.reads).toEqual([]);
|
|
107
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('the estimate covers the section and the compare its claim may reach, so it is never below what is spent', async () => {
|
|
111
|
+
const { source, store, manifest } = await upgraded();
|
|
112
|
+
expect((await manifestMap(manifest, compareManifestKey(ROOT))).size).toBe(0); // nothing awaiting: the term below is the owed section's
|
|
113
|
+
const sections = (await planReview(source, manifest, [DOCS_PACK], { compare: false })).estimatedModelCalls;
|
|
114
|
+
const estimate = (await planReview(source, manifest, [DOCS_PACK])).estimatedModelCalls;
|
|
115
|
+
expect(sections).toBe(1);
|
|
116
|
+
expect(estimate - sections).toBe(1); // ceil(1 owed section / 10 per compare call)
|
|
117
|
+
|
|
118
|
+
const judge = fakeJudge();
|
|
119
|
+
await reviewDirectory(args(source, store, manifest, { judgement: judge, similar }));
|
|
120
|
+
expect(judge.requests.map((r) => r.kind)).toEqual(['extract']); // a lone new claim has nothing to be compared with
|
|
121
|
+
expect(estimate).toBeGreaterThanOrEqual(judge.requests.length);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('stays owed through a forced run that is cut short, which does not widen it', async () => {
|
|
125
|
+
const { source, store, manifest } = await upgraded();
|
|
126
|
+
const controller = new AbortController();
|
|
127
|
+
controller.abort();
|
|
128
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), force: true, signal: controller.signal }));
|
|
129
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['p.md'] });
|
|
130
|
+
expect((await loadExtractorState(manifest, ROOT)).owed.get('p.md')).toBe(1);
|
|
131
|
+
const forced = await planDetail(source, manifest, [DOCS_PACK], { force: true });
|
|
132
|
+
expect(forced.files.map((f) => [f.path, f.changed, f.owed])).toEqual([['p.md', true, true], ['q.md', true, false]]);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('a file that stays unreadable', () => {
|
|
137
|
+
it('is the only file the next review reads, and the only one owed — at the one call an upgrade owes it', async () => {
|
|
138
|
+
const { source, store, manifest } = await reviewed(PQ, { similar });
|
|
139
|
+
await forgetPreamble(manifest); // the upgrade adds a section to p.md
|
|
140
|
+
const read = unreadable(source, 'p.md');
|
|
141
|
+
source.reads.length = 0;
|
|
142
|
+
const upgrade = await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
143
|
+
expect(upgrade).toMatchObject({ readErrors: 1, aborted: false });
|
|
144
|
+
expect(source.reads).toEqual(['p.md', 'q.md']);
|
|
145
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['p.md'] });
|
|
146
|
+
expect((await loadExtractorState(manifest, ROOT)).owed.get('p.md')).toBe(1);
|
|
147
|
+
const estimate = (await planReview(source, manifest, [DOCS_PACK], { compare: false })).estimatedModelCalls;
|
|
148
|
+
expect(estimate).toBe(1);
|
|
149
|
+
|
|
150
|
+
source.reads.length = 0;
|
|
151
|
+
const next = await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
152
|
+
expect(next.readErrors).toBe(1);
|
|
153
|
+
expect(source.reads).toEqual(['p.md']);
|
|
154
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['p.md'] });
|
|
155
|
+
|
|
156
|
+
source.read = read; // readable again: the new section is judged, within what the estimate promised
|
|
157
|
+
source.reads.length = 0;
|
|
158
|
+
const judge = fakeJudge();
|
|
159
|
+
await reviewDirectory(args(source, store, manifest, { judgement: judge }));
|
|
160
|
+
expect(source.reads).toEqual(['p.md']);
|
|
161
|
+
expect(judge.requests.map((r) => r.items.map((i) => i.ref))).toEqual([[PREAMBLE]]);
|
|
162
|
+
expect(estimate).toBeGreaterThanOrEqual(judge.requests.length);
|
|
163
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('keeps the count it was already owed at: a read error learns nothing about the file', async () => {
|
|
167
|
+
const { source, store, manifest } = await reviewed(PQ, { similar });
|
|
168
|
+
// p.md is owed at three calls, the version is today's (q.md owes nothing).
|
|
169
|
+
await manifest.set(extractorManifestKey(ROOT), [{ id: 'version', hash: String(CURRENT) }, { id: 'owed:p.md', hash: '3' }]);
|
|
170
|
+
unreadable(source, 'p.md');
|
|
171
|
+
source.reads.length = 0;
|
|
172
|
+
const report = await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
173
|
+
expect(report.readErrors).toBe(1);
|
|
174
|
+
expect(source.reads).toEqual(['p.md']);
|
|
175
|
+
expect((await loadExtractorState(manifest, ROOT)).owed.get('p.md')).toBe(3);
|
|
176
|
+
expect((await planReview(source, manifest, [DOCS_PACK], { compare: false })).estimatedModelCalls).toBe(3);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe('what an upgrade run leaves owed', () => {
|
|
181
|
+
it('an abort partway through the files: the finished ones are clear, the unreached ones owed', async () => {
|
|
182
|
+
const { source, store, manifest } = await reviewed();
|
|
183
|
+
const controller = new AbortController();
|
|
184
|
+
const read = source.read.bind(source);
|
|
185
|
+
source.read = async (p) => {
|
|
186
|
+
const doc = await read(p);
|
|
187
|
+
if (p === 'b.md') controller.abort(); // b.md is still handled; the loop stops before c.md
|
|
188
|
+
return doc;
|
|
189
|
+
};
|
|
190
|
+
const report = await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), signal: controller.signal }));
|
|
191
|
+
expect(report.aborted).toBe(true);
|
|
192
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['c.md'] });
|
|
193
|
+
|
|
194
|
+
source.read = read;
|
|
195
|
+
source.reads.length = 0;
|
|
196
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
197
|
+
expect(source.reads).toEqual(['c.md']);
|
|
198
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('a file found binary on read, or taken by no pack, is cleared', async () => {
|
|
202
|
+
const { source, store, manifest } = await reviewed();
|
|
203
|
+
const read = source.read.bind(source);
|
|
204
|
+
source.read = async (p) => {
|
|
205
|
+
const doc = await read(p);
|
|
206
|
+
if (p === 'a.md') return { ...doc, content: { kind: 'text', text: 'bin\u0000ary' } };
|
|
207
|
+
if (p === 'b.md') return { ...doc, content: { kind: 'text', text: 'x', mediaType: 'application/pdf' } };
|
|
208
|
+
return doc;
|
|
209
|
+
};
|
|
210
|
+
const report = await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
211
|
+
expect(report.aborted).toBe(false);
|
|
212
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('a file removed while owed drops out of a clean run', async () => {
|
|
216
|
+
const { source, store, manifest } = await reviewed();
|
|
217
|
+
delete source.files['b.md'];
|
|
218
|
+
const report = await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
219
|
+
expect(report.aborted).toBe(false);
|
|
220
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
221
|
+
expect([...(await manifestMap(manifest, filesManifestKey(ROOT))).keys()]).toEqual(['a.md', 'c.md']);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('a file removed while owed stays owed through an aborted run, and drops out of the next clean one', async () => {
|
|
225
|
+
const { source, store, manifest } = await reviewed();
|
|
226
|
+
delete source.files['b.md'];
|
|
227
|
+
const controller = new AbortController();
|
|
228
|
+
controller.abort();
|
|
229
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), signal: controller.signal }));
|
|
230
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ALL });
|
|
231
|
+
|
|
232
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
233
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('a path claimed by no pack stays owed while the manifest holds it', async () => {
|
|
237
|
+
const { source, store, manifest } = await reviewed({ ...FILES, 'x.md': '# X\nxx\n' });
|
|
238
|
+
const narrow = {
|
|
239
|
+
...DOCS_PACK, parsers: DOCS_PACK.parsers.map((p) => ({ ...p, accepts: (s: SourceDocument) => s.id !== 'x.md' && p.accepts(s) })),
|
|
240
|
+
};
|
|
241
|
+
const plan = await planReview(source, manifest, [narrow]);
|
|
242
|
+
expect(plan.skipped).toEqual([{ path: 'x.md', reason: 'no-pack' }]);
|
|
243
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), packs: [narrow] }));
|
|
244
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['x.md'] });
|
|
245
|
+
|
|
246
|
+
source.reads.length = 0;
|
|
247
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
248
|
+
expect(source.reads).toEqual(['x.md']);
|
|
249
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('Continue with a plan made before a path existed carries that owed path, unread', async () => {
|
|
253
|
+
const source = memSource({ 'a.md': FILES['a.md'], 'b.md': FILES['b.md'] });
|
|
254
|
+
const store = memStore();
|
|
255
|
+
const manifest = new InMemoryIngestManifest();
|
|
256
|
+
const stale = await planDetail(source, manifest, [DOCS_PACK]); // lists a.md and b.md only
|
|
257
|
+
source.files['c.md'] = FILES['c.md'];
|
|
258
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
259
|
+
await manifest.delete(extractorManifestKey(ROOT)); // the upgrade: c.md is owed, though the plan never heard of it
|
|
260
|
+
|
|
261
|
+
source.reads.length = 0;
|
|
262
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), plan: stale }));
|
|
263
|
+
expect(source.reads).toEqual(['a.md', 'b.md']);
|
|
264
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['c.md'] });
|
|
265
|
+
|
|
266
|
+
source.reads.length = 0;
|
|
267
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
268
|
+
expect(source.reads).toEqual(['c.md']);
|
|
269
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('a file now ignored stays owed while the manifest holds it, and is reviewed when it is listed again', async () => {
|
|
273
|
+
const { source, store, manifest } = await reviewed();
|
|
274
|
+
source.files['.gitignore'] = 'b.md\n';
|
|
275
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
276
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['b.md'] });
|
|
277
|
+
|
|
278
|
+
delete source.files['.gitignore'];
|
|
279
|
+
source.reads.length = 0;
|
|
280
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
281
|
+
expect(source.reads).toEqual(['b.md']);
|
|
282
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('a content-changed file left unfinished stays owed when its bytes revert to the manifest hash', async () => {
|
|
286
|
+
const { source, store, manifest } = await reviewed();
|
|
287
|
+
source.files['c.md'] = '# C\ngamma\nmore\n';
|
|
288
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), budget: { modelCalls: 0, ms: 60_000 } }));
|
|
289
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['c.md'] });
|
|
290
|
+
|
|
291
|
+
source.files['c.md'] = FILES['c.md']; // the manifest holds this hash again
|
|
292
|
+
expect((await planReview(source, manifest, [DOCS_PACK])).changed).toEqual(['c.md']);
|
|
293
|
+
const report = await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
294
|
+
expect(report.extracted).toBe(1);
|
|
295
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('the same with the version current: a content-changed file that was never owed is recorded, then re-reviewed after the revert', async () => {
|
|
299
|
+
const { source, store, manifest } = await reviewed();
|
|
300
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() })); // settles the upgrade
|
|
301
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
302
|
+
|
|
303
|
+
source.files['c.md'] = '# C\ngamma\nmore\n';
|
|
304
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), budget: { modelCalls: 0, ms: 60_000 } }));
|
|
305
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['c.md'] });
|
|
306
|
+
|
|
307
|
+
source.files['c.md'] = FILES['c.md'];
|
|
308
|
+
expect((await planReview(source, manifest, [DOCS_PACK])).changed).toEqual(['c.md']);
|
|
309
|
+
const report = await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() }));
|
|
310
|
+
expect(report.extracted).toBe(1);
|
|
311
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('records every section a content-changed file had to judge, not only those the budget cut', async () => {
|
|
315
|
+
const { source, store, manifest } = await reviewed();
|
|
316
|
+
source.files['c.md'] = '# C\ngamma two\n## D\ndelta\n'; // two sections to judge
|
|
317
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), budget: { modelCalls: 1, ms: 60_000 } }));
|
|
318
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['c.md'] });
|
|
319
|
+
expect((await loadExtractorState(manifest, ROOT)).owed.get('c.md')).toBe(2); // one was judged, but the revert re-judges it
|
|
320
|
+
|
|
321
|
+
source.files['c.md'] = FILES['c.md'];
|
|
322
|
+
const estimate = (await planReview(source, manifest, [DOCS_PACK], { compare: false })).estimatedModelCalls;
|
|
323
|
+
const judge = fakeJudge();
|
|
324
|
+
await reviewDirectory(args(source, store, manifest, { judgement: judge }));
|
|
325
|
+
expect(estimate).toBeGreaterThanOrEqual(judge.requests.length);
|
|
326
|
+
expect(judge.requests.length).toBeGreaterThan(0);
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
describe('a debt carried through several cuts', () => {
|
|
331
|
+
it('a content-changed file cut three times, then reverted, is estimated at no less than the revert spends', async () => {
|
|
332
|
+
const original = '# C\ngamma\n## D\ndelta\n## E\nepsilon\n';
|
|
333
|
+
const { source, store, manifest } = await reviewed({ ...FILES, 'c.md': original });
|
|
334
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge() })); // settles the upgrade
|
|
335
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
336
|
+
|
|
337
|
+
source.files['c.md'] = '# C\ngamma 2\n## D\ndelta 2\n## E\nepsilon 2\n'; // three sections to judge
|
|
338
|
+
for (const modelCalls of [1, 1, 0]) {
|
|
339
|
+
await reviewDirectory(args(source, store, manifest, { judgement: fakeJudge(), budget: { modelCalls, ms: 60_000 } }));
|
|
340
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: ['c.md'] });
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
source.files['c.md'] = original; // the manifest holds this hash again
|
|
344
|
+
const estimate = (await planReview(source, manifest, [DOCS_PACK], { compare: false })).estimatedModelCalls;
|
|
345
|
+
const judge = fakeJudge();
|
|
346
|
+
await reviewDirectory(args(source, store, manifest, { judgement: judge }));
|
|
347
|
+
expect(judge.requests.length).toBeGreaterThan(1); // the sections judged under the edit are re-judged
|
|
348
|
+
expect(estimate).toBeGreaterThanOrEqual(judge.requests.length);
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
describe('the stored extractor value', () => {
|
|
353
|
+
it('an upgrade adds its one call to the debt a file already carries', async () => {
|
|
354
|
+
const { source, manifest } = await reviewed({ 'p.md': TEXT }, { similar });
|
|
355
|
+
await manifest.set(extractorManifestKey(ROOT), [{ id: 'version', hash: String(CURRENT - 1) }, { id: 'owed:p.md', hash: '5' }]);
|
|
356
|
+
expect((await planReview(source, manifest, [DOCS_PACK], { compare: false })).estimatedModelCalls).toBe(5 + 1);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it('a missing, empty or older value is an upgrade; a legacy value of today is not', async () => {
|
|
360
|
+
const source = memSource({ ...FILES });
|
|
361
|
+
const manifest = new InMemoryIngestManifest();
|
|
362
|
+
await reviewDirectory(args(source, memStore(), manifest, { judgement: fakeJudge() }));
|
|
363
|
+
const key = extractorManifestKey(ROOT);
|
|
364
|
+
const owedPaths = async () => (await planDetail(source, manifest, [DOCS_PACK])).files.filter((f) => f.owed).map((f) => f.path);
|
|
365
|
+
expect(await owedPaths()).toEqual([]);
|
|
366
|
+
|
|
367
|
+
await manifest.delete(key);
|
|
368
|
+
expect(await owedPaths()).toEqual(ALL);
|
|
369
|
+
await manifest.set(key, []);
|
|
370
|
+
expect(await owedPaths()).toEqual(ALL);
|
|
371
|
+
await manifest.set(key, [{ id: 'version', hash: String(CURRENT - 1) }]);
|
|
372
|
+
expect(await owedPaths()).toEqual(ALL);
|
|
373
|
+
|
|
374
|
+
// The shape an older build wrote parses as `{ version, owed: none }`.
|
|
375
|
+
await manifest.set(key, [{ id: 'version', hash: String(CURRENT) }]);
|
|
376
|
+
expect(await stateOf(manifest)).toEqual({ version: CURRENT, owed: [] });
|
|
377
|
+
expect(await owedPaths()).toEqual([]);
|
|
378
|
+
});
|
|
379
|
+
});
|
package/src/knowledge/review.ts
CHANGED
|
@@ -15,7 +15,9 @@
|
|
|
15
15
|
* Abort is honoured between files and inside judgement; a file is written whole or not at
|
|
16
16
|
* all. A file whose sections were cut by the budget stays "changed" in the manifest, so
|
|
17
17
|
* *Continue* (the confirmed plan passed back as `args.plan`, no re-walk) judges only the
|
|
18
|
-
* sections not judged yet.
|
|
18
|
+
* sections not judged yet. A file owed under an extractor upgrade that this run left
|
|
19
|
+
* unfinished (a failed read, an abort, the budget) stays owed — see owedFiles.ts — so the
|
|
20
|
+
* next review or *Continue* picks up exactly those files, whatever their content does.
|
|
19
21
|
*/
|
|
20
22
|
|
|
21
23
|
import { hashText } from '../ingest/hash.js';
|
|
@@ -23,10 +25,9 @@ import type { SourceDocument } from '../ingest/types.js';
|
|
|
23
25
|
import { sha256Hex } from '../retrieval/knowledge-index/sha256.js';
|
|
24
26
|
import type { NodeRef, ReviewArgs, ReviewReport, ScopeRecord, StoredFact, StoredRelation } from './model.js';
|
|
25
27
|
import { SECTION_INDEX_BADGE } from '../ingest/docsExtract.js';
|
|
28
|
+
import { owedAfterRun, owedCalls, saveExtractorState } from './owedFiles.js';
|
|
26
29
|
import { planDetail, refreshPlan } from './plan.js';
|
|
27
|
-
import {
|
|
28
|
-
composePacks, filesManifestKey, isBinaryText, judgedManifestKey, manifestMap, saveExtractorVersion, saveManifestMap,
|
|
29
|
-
} from './sources.js';
|
|
30
|
+
import { composePacks, filesManifestKey, isBinaryText, judgedManifestKey, manifestMap, saveManifestMap } from './sources.js';
|
|
30
31
|
import { extractFileDetailed, knownKeysOfEntities } from './extractFile.js';
|
|
31
32
|
import { DEFAULT_REVIEW_MODEL_CALLS, JudgedBatch, judgeSections, type CompareOutcome, type JudgeBudget } from './judge.js';
|
|
32
33
|
import { compareNew, pendingClaims, saveAwaiting } from './reviewCompare.js';
|
|
@@ -53,7 +54,7 @@ export async function reviewDirectory(args: ReviewArgs): Promise<ReviewReport> {
|
|
|
53
54
|
|
|
54
55
|
const detail = args.plan
|
|
55
56
|
? await refreshPlan(args.plan, manifest)
|
|
56
|
-
: await planDetail(source, manifest, packs, args.force ? { force: true } : {});
|
|
57
|
+
: await planDetail(source, manifest, packs, { ...(args.force ? { force: true } : {}), compare: !!args.similar && !!args.judgement });
|
|
57
58
|
progress({ phase: 'plan', done: detail.files.length, total: detail.files.length });
|
|
58
59
|
const composed = composePacks(packs);
|
|
59
60
|
const packIds = packs.map((p) => p.id).sort(compareStr);
|
|
@@ -106,16 +107,25 @@ export async function reviewDirectory(args: ReviewArgs): Promise<ReviewReport> {
|
|
|
106
107
|
};
|
|
107
108
|
|
|
108
109
|
const changed = detail.files.filter((f) => f.changed);
|
|
110
|
+
// Changed files this run finished (or had nothing to do for); an owed file not in it stays owed,
|
|
111
|
+
// at the model calls it still costs where this run could measure that (see owedFiles.ts).
|
|
112
|
+
const finished = new Set<string>();
|
|
113
|
+
const measured = new Map<string, number>();
|
|
109
114
|
for (const [i, f] of changed.entries()) {
|
|
110
115
|
if (signal?.aborted) { report.aborted = true; break; }
|
|
111
116
|
progress({ phase: 'extract', done: i, total: changed.length, path: f.path });
|
|
112
117
|
// A read that throws (permissions, a lock, a race with a delete) says nothing about the file:
|
|
113
118
|
// skipped with its knowledge and manifest hash intact, tried again next review.
|
|
114
119
|
let doc: SourceDocument;
|
|
115
|
-
try { doc = await source.read(f.path); } catch {
|
|
120
|
+
try { doc = await source.read(f.path); } catch {
|
|
121
|
+
report.readErrors += 1;
|
|
122
|
+
measured.set(f.path, owedCalls(detail.extractor, f.path)); // a read error learns nothing: keep what it was owed
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
116
125
|
// Recorded, so a file found binary on read is not re-read by every later review.
|
|
117
126
|
if (doc.content.kind === 'text' && isBinaryText(doc.content.text)) {
|
|
118
127
|
filesNow.set(f.path, f.hash);
|
|
128
|
+
finished.add(f.path);
|
|
119
129
|
await store.markStale(scope, srcRef(rootId, f.path), f.hash);
|
|
120
130
|
report.staleMarked += 1;
|
|
121
131
|
continue;
|
|
@@ -123,7 +133,7 @@ export async function reviewDirectory(args: ReviewArgs): Promise<ReviewReport> {
|
|
|
123
133
|
const extraction = await extractFileDetailed({
|
|
124
134
|
rootId, path: f.path, hash: f.hash, doc, packs, store, composed, knownKeys,
|
|
125
135
|
});
|
|
126
|
-
if (!extraction) continue;
|
|
136
|
+
if (!extraction) { finished.add(f.path); continue; }
|
|
127
137
|
const { extracted: ex, sections, pack } = extraction;
|
|
128
138
|
report.extracted += 1;
|
|
129
139
|
|
|
@@ -150,7 +160,11 @@ export async function reviewDirectory(args: ReviewArgs): Promise<ReviewReport> {
|
|
|
150
160
|
budget, batch, entities, ...(signal ? { signal } : {}),
|
|
151
161
|
onJudged: (ref) => judgedHere.set(ref, hashText(toJudge.find((s) => s.ref === ref)!.text)),
|
|
152
162
|
});
|
|
153
|
-
for
|
|
163
|
+
// Only a pack that asks for contradictions has claims to compare (the estimate counts the same).
|
|
164
|
+
// Deleted first so a re-judged claim moves to the newest position (the awaiting cap drops the oldest).
|
|
165
|
+
if (spec.contradictions) {
|
|
166
|
+
for (const c of claims) { newClaims.delete(c.ref); newClaims.set(c.ref, { text: c.text, hash: f.hash }); }
|
|
167
|
+
}
|
|
154
168
|
} catch (err) {
|
|
155
169
|
if (!signal?.aborted) throw err;
|
|
156
170
|
batch.take();
|
|
@@ -159,12 +173,18 @@ export async function reviewDirectory(args: ReviewArgs): Promise<ReviewReport> {
|
|
|
159
173
|
}
|
|
160
174
|
for (const [ref, h] of judgedHere) judgedNow.set(ref, h);
|
|
161
175
|
complete = batch.unjudged === unjudgedBefore;
|
|
176
|
+
// A content-changed file may revert to the manifest bytes: then all it had to judge — and what it was
|
|
177
|
+
// already owed from earlier cuts — is still owed.
|
|
178
|
+
if (!complete) {
|
|
179
|
+
const contentChanged = detail.prior.get(f.path) !== f.hash;
|
|
180
|
+
measured.set(f.path, contentChanged ? toJudge.length + (detail.extractor.owed.get(f.path) ?? 0) : batch.unjudged - unjudgedBefore);
|
|
181
|
+
}
|
|
162
182
|
}
|
|
163
183
|
progress({ phase: 'write', done: i, total: changed.length, path: f.path });
|
|
164
184
|
await write();
|
|
165
185
|
await store.markStale(scope, srcRef(rootId, f.path), f.hash);
|
|
166
186
|
report.staleMarked += 1;
|
|
167
|
-
if (complete) filesNow.set(f.path, f.hash);
|
|
187
|
+
if (complete) { filesNow.set(f.path, f.hash); finished.add(f.path); }
|
|
168
188
|
}
|
|
169
189
|
|
|
170
190
|
if (!report.aborted) {
|
|
@@ -179,8 +199,9 @@ export async function reviewDirectory(args: ReviewArgs): Promise<ReviewReport> {
|
|
|
179
199
|
progress({ phase: 'resolve', done: 0, total: newClaims.size });
|
|
180
200
|
}
|
|
181
201
|
|
|
182
|
-
// The compare phase needs a pack asking for it AND both ports
|
|
183
|
-
const
|
|
202
|
+
// The compare phase needs a pack asking for it AND both ports.
|
|
203
|
+
const wantsCompare = packs.some((p) => p.judgement?.contradictions);
|
|
204
|
+
const canCompare = wantsCompare && !!args.judgement && !!args.similar;
|
|
184
205
|
let outcome: CompareOutcome | undefined;
|
|
185
206
|
if (!report.aborted && canCompare) {
|
|
186
207
|
outcome = await compareNew({
|
|
@@ -190,7 +211,8 @@ export async function reviewDirectory(args: ReviewArgs): Promise<ReviewReport> {
|
|
|
190
211
|
// Aborted only if the abort landed before the last pair was compared.
|
|
191
212
|
if (outcome?.aborted) report.aborted = true;
|
|
192
213
|
}
|
|
193
|
-
|
|
214
|
+
// No pack asks for contradictions: nothing waits, and an earlier run's awaiting set is left as it was.
|
|
215
|
+
if (wantsCompare) await saveAwaiting(manifest, rootId, newClaims, outcome);
|
|
194
216
|
|
|
195
217
|
report.judged = batch.judged;
|
|
196
218
|
report.unjudged = batch.unjudged;
|
|
@@ -198,8 +220,11 @@ export async function reviewDirectory(args: ReviewArgs): Promise<ReviewReport> {
|
|
|
198
220
|
report.digest = sha256Hex(canonicalJson({ rootId, packs: packIds, files: [...filesNow].sort((a, b) => compareStr(a[0], b[0])) }));
|
|
199
221
|
await saveManifestMap(manifest, filesManifestKey(rootId), filesNow);
|
|
200
222
|
await saveManifestMap(manifest, judgedManifestKey(rootId), judgedNow);
|
|
201
|
-
//
|
|
202
|
-
|
|
223
|
+
// After every run: today's version and every file still owed a re-extraction — the unfinished ones,
|
|
224
|
+
// content-changed or not, and the owed paths the plan did not list that the manifest still holds.
|
|
225
|
+
await saveExtractorState(manifest, rootId, owedAfterRun({
|
|
226
|
+
state: detail.extractor, changed, prior: detail.prior, finished, carried: detail.carried, live: filesNow, measured,
|
|
227
|
+
}));
|
|
203
228
|
await write(report.aborted ? scopeRecord : { ...scopeRecord, lastDigest: report.digest, lastReviewedAt: new Date(now()).toISOString() });
|
|
204
229
|
report.durationMs = now() - started;
|
|
205
230
|
progress({ phase: 'done', done: changed.length, total: changed.length });
|