@seanhogg/builderforce-memory 2026.10.43 → 2026.10.44
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/extractFile.d.ts +15 -0
- package/dist/knowledge/extractFile.d.ts.map +1 -1
- package/dist/knowledge/extractFile.js +40 -17
- package/dist/knowledge/extractFile.js.map +1 -1
- package/dist/knowledge/extractJob.d.ts +64 -0
- package/dist/knowledge/extractJob.d.ts.map +1 -0
- package/dist/knowledge/extractJob.js +47 -0
- package/dist/knowledge/extractJob.js.map +1 -0
- package/dist/knowledge/index.d.ts +10 -1
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +6 -1
- package/dist/knowledge/index.js.map +1 -1
- package/dist/knowledge/judge.d.ts +23 -5
- package/dist/knowledge/judge.d.ts.map +1 -1
- package/dist/knowledge/judge.js +14 -76
- package/dist/knowledge/judge.js.map +1 -1
- package/dist/knowledge/judgeCalls.d.ts +52 -0
- package/dist/knowledge/judgeCalls.d.ts.map +1 -0
- package/dist/knowledge/judgeCalls.js +107 -0
- package/dist/knowledge/judgeCalls.js.map +1 -0
- package/dist/knowledge/modelLimiter.d.ts +20 -0
- package/dist/knowledge/modelLimiter.d.ts.map +1 -0
- package/dist/knowledge/modelLimiter.js +63 -0
- package/dist/knowledge/modelLimiter.js.map +1 -0
- package/dist/knowledge/review.d.ts +6 -2
- package/dist/knowledge/review.d.ts.map +1 -1
- package/dist/knowledge/review.js +108 -112
- package/dist/knowledge/review.js.map +1 -1
- package/dist/knowledge/reviewJudge.d.ts +95 -0
- package/dist/knowledge/reviewJudge.d.ts.map +1 -0
- package/dist/knowledge/reviewJudge.js +119 -0
- package/dist/knowledge/reviewJudge.js.map +1 -0
- package/dist/knowledge/reviewModel.d.ts +30 -2
- package/dist/knowledge/reviewModel.d.ts.map +1 -1
- package/dist/knowledge/reviewPrefetch.d.ts +53 -0
- package/dist/knowledge/reviewPrefetch.d.ts.map +1 -0
- package/dist/knowledge/reviewPrefetch.js +65 -0
- package/dist/knowledge/reviewPrefetch.js.map +1 -0
- package/dist/knowledge/reviewRun.d.ts +70 -0
- package/dist/knowledge/reviewRun.d.ts.map +1 -0
- package/dist/knowledge/reviewRun.js +88 -0
- package/dist/knowledge/reviewRun.js.map +1 -0
- package/dist/knowledge/reviewRunModel.d.ts +35 -0
- package/dist/knowledge/reviewRunModel.d.ts.map +1 -0
- package/dist/knowledge/reviewRunModel.js +27 -0
- package/dist/knowledge/reviewRunModel.js.map +1 -0
- package/package.json +3 -3
- package/src/knowledge/extractFile.ts +49 -17
- package/src/knowledge/extractJob.ts +91 -0
- package/src/knowledge/index.ts +10 -1
- package/src/knowledge/judge.ts +24 -70
- package/src/knowledge/judgeCalls.ts +136 -0
- package/src/knowledge/modelLimiter.test.ts +68 -0
- package/src/knowledge/modelLimiter.ts +68 -0
- package/src/knowledge/review.failures.test.ts +277 -0
- package/src/knowledge/review.ordered.test.ts +114 -0
- package/src/knowledge/review.ts +102 -100
- package/src/knowledge/reviewJudge.ts +155 -0
- package/src/knowledge/reviewModel.ts +30 -2
- package/src/knowledge/reviewPrefetch.ts +86 -0
- package/src/knowledge/reviewRun.test.ts +96 -0
- package/src/knowledge/reviewRun.ts +137 -0
- package/src/knowledge/reviewRunModel.ts +53 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/judgeCalls.ts — judging one file's sections, in two stages (PRD 41 A8).
|
|
3
|
+
*
|
|
4
|
+
* • {@link callSections} — the CALL stage: one model call per section within a budget slice, touching
|
|
5
|
+
* no shared state, so several files can be in it at once (optionally under ONE shared
|
|
6
|
+
* {@link ModelLimiter}).
|
|
7
|
+
* • {@link applyAnswers} — the APPLY stage: merges answers into the batch and the shared entity map.
|
|
8
|
+
* Synchronous; the review's one ordered writer calls it in plan order, which keeps the stored
|
|
9
|
+
* bytes independent of how many files were judged at once.
|
|
10
|
+
*
|
|
11
|
+
* {@link judgeSections} is both stages for one file.
|
|
12
|
+
*/
|
|
13
|
+
import { validateExtract, type ExtractArgs, type JudgeBudget, type SectionToJudge } from './judge.js';
|
|
14
|
+
import type { ModelLimiter } from './modelLimiter.js';
|
|
15
|
+
import type { JudgementPort, NodeRef } from './model.js';
|
|
16
|
+
/** A section's validated answer: the output of the call stage, applied later by {@link applyAnswers}. */
|
|
17
|
+
export interface SectionAnswer {
|
|
18
|
+
section: SectionToJudge;
|
|
19
|
+
v: ReturnType<typeof validateExtract>;
|
|
20
|
+
}
|
|
21
|
+
export interface CallSectionsArgs {
|
|
22
|
+
port: JudgementPort;
|
|
23
|
+
fileHash: string;
|
|
24
|
+
sections: readonly SectionToJudge[];
|
|
25
|
+
/** This file's slice of the review budget (its own `used`). */
|
|
26
|
+
budget: JudgeBudget;
|
|
27
|
+
/** Shared across the files of a review: caps the calls in flight and adapts to rate limits. */
|
|
28
|
+
limiter?: ModelLimiter;
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
}
|
|
31
|
+
export interface CallSectionsResult {
|
|
32
|
+
answers: SectionAnswer[];
|
|
33
|
+
/** Sections cut by the budget or whose call failed. */
|
|
34
|
+
unjudged: number;
|
|
35
|
+
/** Calls the port (or the provider behind it) failed. */
|
|
36
|
+
errors: number;
|
|
37
|
+
/** The first failure's message, single line. */
|
|
38
|
+
firstError?: string;
|
|
39
|
+
}
|
|
40
|
+
/** Answers come back in section order. Throws on abort. */
|
|
41
|
+
export declare function callSections(a: CallSectionsArgs): Promise<CallSectionsResult>;
|
|
42
|
+
/** Judges each section within the budget; the rest — and any whose call failed — are counted `unjudged`. */
|
|
43
|
+
export declare function judgeSections(a: ExtractArgs): Promise<Array<{
|
|
44
|
+
ref: NodeRef;
|
|
45
|
+
text: string;
|
|
46
|
+
}>>;
|
|
47
|
+
/** Merges answers into the batch and the shared entities map, in the order given. */
|
|
48
|
+
export declare function applyAnswers(a: Pick<ExtractArgs, 'pack' | 'path' | 'fileHash' | 'batch' | 'entities' | 'onJudged'>, answers: readonly SectionAnswer[]): Array<{
|
|
49
|
+
ref: NodeRef;
|
|
50
|
+
text: string;
|
|
51
|
+
}>;
|
|
52
|
+
//# sourceMappingURL=judgeCalls.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"judgeCalls.d.ts","sourceRoot":"","sources":["../../src/knowledge/judgeCalls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAC6E,eAAe,EAC/F,KAAK,WAAW,EAAE,KAAK,WAAW,EAAE,KAAK,cAAc,EAC1D,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAc,MAAM,YAAY,CAAC;AAKrE,yGAAyG;AACzG,MAAM,WAAW,aAAa;IAAG,OAAO,EAAE,cAAc,CAAC;IAAC,CAAC,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,CAAA;CAAE;AAEjG,MAAM,WAAW,gBAAgB;IAC7B,IAAI,EAAE,aAAa,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,SAAS,cAAc,EAAE,CAAC;IACpC,+DAA+D;IAC/D,MAAM,EAAE,WAAW,CAAC;IACpB,+FAA+F;IAC/F,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,MAAM,CAAC,EAAE,WAAW,CAAC;CACxB;AAED,MAAM,WAAW,kBAAkB;IAC/B,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB,uDAAuD;IACvD,QAAQ,EAAE,MAAM,CAAC;IACjB,yDAAyD;IACzD,MAAM,EAAE,MAAM,CAAC;IACf,gDAAgD;IAChD,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB;AAGD,2DAA2D;AAC3D,wBAAsB,YAAY,CAAC,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CA4BnF;AAED,4GAA4G;AAC5G,wBAAsB,aAAa,CAAC,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,KAAK,CAAC;IAAE,GAAG,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC,CAMlG;AAED,qFAAqF;AACrF,wBAAgB,YAAY,CACxB,CAAC,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,OAAO,GAAG,UAAU,GAAG,UAAU,CAAC,EACtF,OAAO,EAAE,SAAS,aAAa,EAAE,GAClC,KAAK,CAAC;IAAE,GAAG,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CA0CvC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/judgeCalls.ts — judging one file's sections, in two stages (PRD 41 A8).
|
|
3
|
+
*
|
|
4
|
+
* • {@link callSections} — the CALL stage: one model call per section within a budget slice, touching
|
|
5
|
+
* no shared state, so several files can be in it at once (optionally under ONE shared
|
|
6
|
+
* {@link ModelLimiter}).
|
|
7
|
+
* • {@link applyAnswers} — the APPLY stage: merges answers into the batch and the shared entity map.
|
|
8
|
+
* Synchronous; the review's one ordered writer calls it in plan order, which keeps the stored
|
|
9
|
+
* bytes independent of how many files were judged at once.
|
|
10
|
+
*
|
|
11
|
+
* {@link judgeSections} is both stages for one file.
|
|
12
|
+
*/
|
|
13
|
+
import { hashText } from '../ingest/hash.js';
|
|
14
|
+
import { budgetLeft, call, claimContent, JUDGE_CONCURRENCY, REVIEW_FACT_SOURCE, runPool, validateExtract, } from './judge.js';
|
|
15
|
+
import { compareStr, factRef } from './refs.js';
|
|
16
|
+
import { entityKey, normalizeKey } from './resolve.js';
|
|
17
|
+
import { reasonOf } from './reviewRunModel.js';
|
|
18
|
+
/** Answers come back in section order. Throws on abort. */
|
|
19
|
+
export async function callSections(a) {
|
|
20
|
+
const slots = new Array(a.sections.length);
|
|
21
|
+
let unjudged = 0;
|
|
22
|
+
let errors = 0;
|
|
23
|
+
let firstError;
|
|
24
|
+
const hooks = {
|
|
25
|
+
...(a.limiter ? { limiter: a.limiter } : {}),
|
|
26
|
+
onError: (err) => { errors += 1; firstError ??= reasonOf(err); },
|
|
27
|
+
};
|
|
28
|
+
// The budget check and `call`'s synchronous `used` increment happen on the same tick (no await
|
|
29
|
+
// between), so concurrency cannot overshoot `modelCalls`.
|
|
30
|
+
const abortError = () => a.signal?.reason ?? new Error('aborted');
|
|
31
|
+
await runPool(a.sections.map((section, idx) => ({ section, idx })), JUDGE_CONCURRENCY, async ({ section, idx }) => {
|
|
32
|
+
// Honoured per section, not only when the port throws: the caller drops this file whole.
|
|
33
|
+
if (a.signal?.aborted)
|
|
34
|
+
throw abortError();
|
|
35
|
+
if (!budgetLeft(a.budget)) {
|
|
36
|
+
unjudged += 1;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const answer = await call(a.port, {
|
|
40
|
+
kind: 'extract', items: [{ ref: section.ref, text: section.text, sourceHash: a.fileHash }],
|
|
41
|
+
...(a.signal ? { signal: a.signal } : {}),
|
|
42
|
+
}, a.budget, hooks);
|
|
43
|
+
// A failed call judged nothing: not counted judged, so the file stays changed and is retried.
|
|
44
|
+
if (!answer) {
|
|
45
|
+
unjudged += 1;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// The abort can land while a call is in flight (the port sees the signal): a section that
|
|
49
|
+
// finished after the abort must not count as judged, or the caller would not re-judge it.
|
|
50
|
+
if (a.signal?.aborted)
|
|
51
|
+
throw abortError();
|
|
52
|
+
slots[idx] = { section, v: validateExtract(answer.result, section.ref) };
|
|
53
|
+
});
|
|
54
|
+
return { answers: slots.filter((s) => !!s), unjudged, errors, ...(firstError !== undefined ? { firstError } : {}) };
|
|
55
|
+
}
|
|
56
|
+
/** Judges each section within the budget; the rest — and any whose call failed — are counted `unjudged`. */
|
|
57
|
+
export async function judgeSections(a) {
|
|
58
|
+
const { answers, unjudged } = await callSections({
|
|
59
|
+
port: a.port, fileHash: a.fileHash, sections: a.sections, budget: a.budget, ...(a.signal ? { signal: a.signal } : {}),
|
|
60
|
+
});
|
|
61
|
+
a.batch.unjudged += unjudged;
|
|
62
|
+
return applyAnswers(a, answers);
|
|
63
|
+
}
|
|
64
|
+
/** Merges answers into the batch and the shared entities map, in the order given. */
|
|
65
|
+
export function applyAnswers(a, answers) {
|
|
66
|
+
const claims = [];
|
|
67
|
+
const anchor = { path: a.path, contentHash: a.fileHash };
|
|
68
|
+
for (const { section, v } of answers) {
|
|
69
|
+
a.batch.invalid += v.invalid;
|
|
70
|
+
a.batch.judged += 1;
|
|
71
|
+
a.onJudged(section.ref);
|
|
72
|
+
const rel = (from, to, kind) => a.batch.relation({ from, to, kind, weight: 1, origin: 'model', sourceHash: a.fileHash });
|
|
73
|
+
const entityRefs = new Map();
|
|
74
|
+
for (const e of v.entities) {
|
|
75
|
+
const key = entityKey(a.pack, e.name);
|
|
76
|
+
const prior = a.entities.get(key);
|
|
77
|
+
const anchors = [...(prior?.fileAnchors ?? []).filter((x) => x.path !== a.path), anchor]
|
|
78
|
+
.sort((x, y) => compareStr(x.path, y.path));
|
|
79
|
+
const fact = {
|
|
80
|
+
key, content: prior?.content ?? `${e.name} (${e.type})`, source: REVIEW_FACT_SOURCE,
|
|
81
|
+
importance: prior?.importance ?? 0.5, origin: prior?.origin ?? 'model', fileAnchors: anchors,
|
|
82
|
+
};
|
|
83
|
+
a.entities.set(key, fact);
|
|
84
|
+
a.batch.fact(fact);
|
|
85
|
+
entityRefs.set(normalizeKey(e.name), key);
|
|
86
|
+
rel(section.ref, key, 'mentions');
|
|
87
|
+
}
|
|
88
|
+
for (const c of v.claims) {
|
|
89
|
+
const key = `claim:${hashText([section.ref, c.subject, c.predicate, c.object].join('\u0000'))}`;
|
|
90
|
+
const text = `${c.subject} ${c.predicate} ${c.object}`;
|
|
91
|
+
a.batch.fact({
|
|
92
|
+
key, content: claimContent(text, c.quote), source: REVIEW_FACT_SOURCE,
|
|
93
|
+
importance: 0.5, origin: 'model', fileAnchors: [anchor],
|
|
94
|
+
});
|
|
95
|
+
const ref = factRef(key);
|
|
96
|
+
rel(ref, section.ref, 'about');
|
|
97
|
+
for (const term of [c.subject, c.object]) {
|
|
98
|
+
const ent = entityRefs.get(normalizeKey(term));
|
|
99
|
+
if (ent)
|
|
100
|
+
rel(ref, ent, 'mentions');
|
|
101
|
+
}
|
|
102
|
+
claims.push({ ref, text });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return claims;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=judgeCalls.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"judgeCalls.js","sourceRoot":"","sources":["../../src/knowledge/judgeCalls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EACH,UAAU,EAAE,IAAI,EAAE,YAAY,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,OAAO,EAAE,eAAe,GAElG,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AA2B/C,2DAA2D;AAC3D,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,CAAmB;IAClD,MAAM,KAAK,GAAqC,IAAI,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC7E,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,UAA8B,CAAC;IACnC,MAAM,KAAK,GAAG;QACV,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CA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CAAC;YACH,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;YACzB,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YAC/B,KAAK,MAAM,IAAI,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC;gBACvC,MAAM,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC;gBAC/C,IAAI,GAAG;oBAAE,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;YACvC,CAAC;YACD,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/B,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/modelLimiter.ts — ONE cap on the model calls in flight across every file of a review
|
|
3
|
+
* (PRD 41 A8), so judging N files at once does not multiply the provider's load.
|
|
4
|
+
*
|
|
5
|
+
* AIMD: a rate-limit answer halves the width (minimum 1); every `growEvery` successful calls grow it
|
|
6
|
+
* by 1, back up to the cap. Waiting calls start in order; one whose signal has aborted is rejected
|
|
7
|
+
* instead of started, so nothing starts after an abort.
|
|
8
|
+
*/
|
|
9
|
+
export interface ModelLimiter {
|
|
10
|
+
/** Calls allowed in flight right now (1..max). */
|
|
11
|
+
readonly width: number;
|
|
12
|
+
readonly max: number;
|
|
13
|
+
run<T>(fn: () => Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
14
|
+
}
|
|
15
|
+
/** An error that says the provider is rate limiting us (HTTP 429 or the usual wording). */
|
|
16
|
+
export declare function isRateLimit(err: unknown): boolean;
|
|
17
|
+
export declare function createModelLimiter(max: number, opts?: {
|
|
18
|
+
growEvery?: number;
|
|
19
|
+
}): ModelLimiter;
|
|
20
|
+
//# sourceMappingURL=modelLimiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modelLimiter.d.ts","sourceRoot":"","sources":["../../src/knowledge/modelLimiter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,WAAW,YAAY;IACzB,kDAAkD;IAClD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAClE;AAED,2FAA2F;AAC3F,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAIjD;AAID,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,YAAY,CA0C/F"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/modelLimiter.ts — ONE cap on the model calls in flight across every file of a review
|
|
3
|
+
* (PRD 41 A8), so judging N files at once does not multiply the provider's load.
|
|
4
|
+
*
|
|
5
|
+
* AIMD: a rate-limit answer halves the width (minimum 1); every `growEvery` successful calls grow it
|
|
6
|
+
* by 1, back up to the cap. Waiting calls start in order; one whose signal has aborted is rejected
|
|
7
|
+
* instead of started, so nothing starts after an abort.
|
|
8
|
+
*/
|
|
9
|
+
/** An error that says the provider is rate limiting us (HTTP 429 or the usual wording). */
|
|
10
|
+
export function isRateLimit(err) {
|
|
11
|
+
const e = err;
|
|
12
|
+
if (e?.status === 429 || e?.statusCode === 429)
|
|
13
|
+
return true;
|
|
14
|
+
return typeof e?.message === 'string' && /\b429\b|rate.?limit|too many requests/i.test(e.message);
|
|
15
|
+
}
|
|
16
|
+
export function createModelLimiter(max, opts = {}) {
|
|
17
|
+
const cap = Math.max(1, Math.floor(max));
|
|
18
|
+
const growEvery = Math.max(1, Math.floor(opts.growEvery ?? 20));
|
|
19
|
+
let width = cap;
|
|
20
|
+
let active = 0;
|
|
21
|
+
let successes = 0;
|
|
22
|
+
const waiting = [];
|
|
23
|
+
const pump = () => {
|
|
24
|
+
while (active < width && waiting.length > 0) {
|
|
25
|
+
const w = waiting.shift();
|
|
26
|
+
if (w.signal?.aborted) {
|
|
27
|
+
w.reject(w.signal.reason ?? new Error('aborted'));
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
active += 1;
|
|
31
|
+
w.start();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
get width() { return width; },
|
|
36
|
+
max: cap,
|
|
37
|
+
run(fn, signal) {
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
waiting.push({
|
|
40
|
+
signal, reject,
|
|
41
|
+
start: () => {
|
|
42
|
+
(async () => fn())().then((v) => {
|
|
43
|
+
successes += 1;
|
|
44
|
+
if (successes >= growEvery && width < cap) {
|
|
45
|
+
width += 1;
|
|
46
|
+
successes = 0;
|
|
47
|
+
}
|
|
48
|
+
resolve(v);
|
|
49
|
+
}, (err) => {
|
|
50
|
+
if (isRateLimit(err)) {
|
|
51
|
+
width = Math.max(1, Math.floor(width / 2));
|
|
52
|
+
successes = 0;
|
|
53
|
+
}
|
|
54
|
+
reject(err);
|
|
55
|
+
}).finally(() => { active -= 1; pump(); });
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
pump();
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=modelLimiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modelLimiter.js","sourceRoot":"","sources":["../../src/knowledge/modelLimiter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AASH,2FAA2F;AAC3F,MAAM,UA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K,CAAC,CAAC,CAAC;QAC7B,GAAG,EAAE,GAAG;QACR,GAAG,CAAI,EAAoB,EAAE,MAAoB;YAC7C,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBACtC,OAAO,CAAC,IAAI,CAAC;oBACT,MAAM,EAAE,MAAM;oBACd,KAAK,EAAE,GAAG,EAAE;wBACR,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CACrB,CAAC,CAAC,EAAE,EAAE;4BACF,SAAS,IAAI,CAAC,CAAC;4BACf,IAAI,SAAS,IAAI,SAAS,IAAI,KAAK,GAAG,GAAG,EAAE,CAAC;gCAAC,KAAK,IAAI,CAAC,CAAC;gCAAC,SAAS,GAAG,CAAC,CAAC;4BAAC,CAAC;4BACzE,OAAO,CAAC,CAAC,CAAC,CAAC;wBACf,CAAC,EACD,CAAC,GAAG,EAAE,EAAE;4BACJ,IAAI,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC;gCAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;gCAAC,SAAS,GAAG,CAAC,CAAC;4BAAC,CAAC;4BACpF,MAAM,CAAC,GAAG,CAAC,CAAC;wBAChB,CAAC,CACJ,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,MAAM,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;oBAC9C,CAAC;iBACJ,CAAC,CAAC;gBACH,IAAI,EAAE,CAAC;YACX,CAAC,CAAC,CAAC;QACP,CAAC;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -7,8 +7,12 @@
|
|
|
7
7
|
* of judged kinds, within budget) · write · markStale → analyzers → compare near-duplicate
|
|
8
8
|
* claims → manifest + scope digest. Claims whose compare pairs were skipped wait in the
|
|
9
9
|
* compare manifest for *Continue*.
|
|
10
|
-
* Read + extract are prefetched with bounded concurrency (`args.concurrency
|
|
11
|
-
*
|
|
10
|
+
* Read + extract are prefetched with bounded concurrency (`args.concurrency`; through `args.extractPool`
|
|
11
|
+
* when a host supplies one) and up to `args.judgeConcurrency` files are judged at once, but ONE writer
|
|
12
|
+
* applies judge · write · markStale strictly in file order, and each file's model calls are reserved from
|
|
13
|
+
* the shared budget in file order — so the stored bytes and the budget spend do not depend on the widths
|
|
14
|
+
* (PRD 41 A8). A file whose read, extraction or judging fails is recorded in `report.failures` and skipped;
|
|
15
|
+
* only an abort or a local store write error ends the run.
|
|
12
16
|
*
|
|
13
17
|
* Store judgement, compute perception: only `model` facts and relations are written —
|
|
14
18
|
* `extracted` output goes to the extracted cache, never to memory ({@link assertStoredBatch}).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"review.d.ts","sourceRoot":"","sources":["../../src/knowledge/review.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"review.d.ts","sourceRoot":"","sources":["../../src/knowledge/review.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAe,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AA0BpG,gFAAgF;AAChF,wBAAgB,iBAAiB,CAAC,KAAK,EAAE;IAAE,KAAK,EAAE,SAAS,UAAU,EAAE,CAAC;IAAC,SAAS,EAAE,SAAS,cAAc,EAAE,CAAA;CAAE,GAAG,IAAI,CAGrH;AAED;;;GAGG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC,CAE7E"}
|
package/dist/knowledge/review.js
CHANGED
|
@@ -7,8 +7,12 @@
|
|
|
7
7
|
* of judged kinds, within budget) · write · markStale → analyzers → compare near-duplicate
|
|
8
8
|
* claims → manifest + scope digest. Claims whose compare pairs were skipped wait in the
|
|
9
9
|
* compare manifest for *Continue*.
|
|
10
|
-
* Read + extract are prefetched with bounded concurrency (`args.concurrency
|
|
11
|
-
*
|
|
10
|
+
* Read + extract are prefetched with bounded concurrency (`args.concurrency`; through `args.extractPool`
|
|
11
|
+
* when a host supplies one) and up to `args.judgeConcurrency` files are judged at once, but ONE writer
|
|
12
|
+
* applies judge · write · markStale strictly in file order, and each file's model calls are reserved from
|
|
13
|
+
* the shared budget in file order — so the stored bytes and the budget spend do not depend on the widths
|
|
14
|
+
* (PRD 41 A8). A file whose read, extraction or judging fails is recorded in `report.failures` and skipped;
|
|
15
|
+
* only an abort or a local store write error ends the run.
|
|
12
16
|
*
|
|
13
17
|
* Store judgement, compute perception: only `model` facts and relations are written —
|
|
14
18
|
* `extracted` output goes to the extracted cache, never to memory ({@link assertStoredBatch}).
|
|
@@ -21,15 +25,17 @@
|
|
|
21
25
|
* unfinished (a failed read, an abort, the budget) stays owed — see owedFiles.ts — so the
|
|
22
26
|
* next review or *Continue* picks up exactly those files, whatever their content does.
|
|
23
27
|
*/
|
|
24
|
-
import { hashText } from '../ingest/hash.js';
|
|
25
28
|
import { sha256Hex } from '../retrieval/knowledge-index/sha256.js';
|
|
26
|
-
import { SECTION_INDEX_BADGE } from '../ingest/docsExtract.js';
|
|
27
29
|
import { owedAfterRun, owedCalls, saveExtractorState } from './owedFiles.js';
|
|
28
30
|
import { planDetail, refreshPlan } from './plan.js';
|
|
29
|
-
import { composePacks, filesManifestKey,
|
|
30
|
-
import {
|
|
31
|
+
import { composePacks, filesManifestKey, judgedManifestKey, manifestMap, saveManifestMap } from './sources.js';
|
|
32
|
+
import { knownKeysOfEntities, putExtract } from './extractFile.js';
|
|
31
33
|
import { prefetchOrdered } from './orderedPool.js';
|
|
32
|
-
import { DEFAULT_REVIEW_CONCURRENCY, DEFAULT_REVIEW_MODEL_CALLS, JudgedBatch,
|
|
34
|
+
import { DEFAULT_REVIEW_CONCURRENCY, DEFAULT_REVIEW_JUDGE_CONCURRENCY, DEFAULT_REVIEW_MODEL_CALLS, JUDGE_CONCURRENCY, JudgedBatch, } from './judge.js';
|
|
35
|
+
import { createModelLimiter } from './modelLimiter.js';
|
|
36
|
+
import { applyJudge, startJudge } from './reviewJudge.js';
|
|
37
|
+
import { createPrefetch } from './reviewPrefetch.js';
|
|
38
|
+
import { Lanes } from './reviewRunModel.js';
|
|
33
39
|
import { compareNew, pendingClaims, saveAwaiting } from './reviewCompare.js';
|
|
34
40
|
import { factsWithoutAnchor, StoredIndex } from './reviewStored.js';
|
|
35
41
|
import { canonicalJson, compareStr, srcRef } from './refs.js';
|
|
@@ -55,7 +61,7 @@ async function reviewRun(args, flush) {
|
|
|
55
61
|
const progress = (p) => args.onProgress?.(p);
|
|
56
62
|
const report = {
|
|
57
63
|
rootId, digest: '', extracted: 0, judged: 0, unjudged: 0, invalidJudgements: 0,
|
|
58
|
-
factsWritten: 0, relationsWritten: 0, staleMarked: 0, readErrors: 0, aborted: false, durationMs: 0,
|
|
64
|
+
factsWritten: 0, relationsWritten: 0, staleMarked: 0, readErrors: 0, extractErrors: 0, failures: [], aborted: false, durationMs: 0,
|
|
59
65
|
};
|
|
60
66
|
const detail = args.plan
|
|
61
67
|
? await refreshPlan(args.plan, manifest)
|
|
@@ -122,58 +128,48 @@ async function reviewRun(args, flush) {
|
|
|
122
128
|
if (hashes.length > 0)
|
|
123
129
|
await store.preloadExtracted?.(hashes);
|
|
124
130
|
// Read + binary check + extract run ahead with bounded concurrency (extraction only reads `knownKeys`,
|
|
125
|
-
// fixed above, and the content-hash extract cache);
|
|
131
|
+
// fixed above, and the content-hash extract cache); with `args.extractPool` the width is the pool's.
|
|
132
|
+
// Judging runs `judgeWidth` files at once; ONE writer (`applyFile`) consumes them strictly in plan order.
|
|
126
133
|
const concurrency = Math.min(32, Math.max(1, Math.floor(args.concurrency ?? DEFAULT_REVIEW_CONCURRENCY) || 1));
|
|
127
|
-
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (doc.content.kind === 'text' && isBinaryText(doc.content.text))
|
|
147
|
-
return { kind: 'binary' };
|
|
148
|
-
await before;
|
|
149
|
-
const extraction = await extractFileDetailed({
|
|
150
|
-
rootId, path: f.path, hash: f.hash, doc, packs, store, composed, knownKeys,
|
|
151
|
-
});
|
|
152
|
-
return extraction ? { kind: 'extracted', doc, extraction } : { kind: 'none' };
|
|
153
|
-
}
|
|
154
|
-
catch (error) {
|
|
155
|
-
return { kind: 'failed', error };
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
finally {
|
|
159
|
-
release();
|
|
160
|
-
void tail.then(() => { if (hashTail.get(f.hash) === tail)
|
|
161
|
-
hashTail.delete(f.hash); });
|
|
162
|
-
}
|
|
134
|
+
const judgeWidth = Math.min(concurrency, Math.min(16, Math.max(1, Math.floor(args.judgeConcurrency ?? DEFAULT_REVIEW_JUDGE_CONCURRENCY) || 1)));
|
|
135
|
+
// The run's own stop: the caller's abort relays into it, and a thrown error (a local store failure) aborts it,
|
|
136
|
+
// so queued and in-flight judge calls and pool jobs stop and nothing outlives the run.
|
|
137
|
+
const stopper = new AbortController();
|
|
138
|
+
// An abort that lands while the writer is mid-file ends the run at that file's boundary, as a sequential run
|
|
139
|
+
// does (`cut`); one that lands anywhere else (a read, a model call) keeps every file whose work completed.
|
|
140
|
+
let inWriter = false;
|
|
141
|
+
let cut = false;
|
|
142
|
+
const relay = () => { if (inWriter)
|
|
143
|
+
cut = true; stopper.abort(signal?.reason); };
|
|
144
|
+
if (signal?.aborted)
|
|
145
|
+
relay();
|
|
146
|
+
else
|
|
147
|
+
signal?.addEventListener('abort', relay, { once: true });
|
|
148
|
+
const runSignal = stopper.signal;
|
|
149
|
+
const prefetch = createPrefetch({ args, signal: runSignal, rootId, packs, store, composed, knownKeys, width: concurrency });
|
|
150
|
+
const dispatch = {
|
|
151
|
+
port: args.judgement, signal: runSignal, budget, judgedPrior, lanes: new Lanes(judgeWidth),
|
|
152
|
+
limiter: createModelLimiter(args.maxModelCalls ?? JUDGE_CONCURRENCY),
|
|
163
153
|
};
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
154
|
+
const judgeCtx = { batch, entities, stored, judgedNow, newClaims, measured, detail, report, ...(args.onStep ? { onStep: args.onStep } : {}) };
|
|
155
|
+
const fail = (path, phase, reason) => report.failures.push({ path, phase, reason });
|
|
156
|
+
/**
|
|
157
|
+
* Applies one file, in plan order. False when its work was cut short by an abort (extracted, judging
|
|
158
|
+
* unfinished): nothing of it is applied and it stays as it was, owed. A file whose work is complete is
|
|
159
|
+
* applied even after an abort — the work is done, so it is kept.
|
|
160
|
+
*/
|
|
161
|
+
const applyInner = async ({ f, i, job, judge }) => {
|
|
167
162
|
progress({ phase: 'extract', done: i, total: changed.length, path: f.path });
|
|
168
|
-
// A read that throws (permissions, a lock, a race with a delete)
|
|
169
|
-
// skipped with its knowledge and manifest hash intact, tried again next review.
|
|
170
|
-
if (
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
163
|
+
// A read that throws (permissions, a lock, a race with a delete) or an extraction that throws says nothing
|
|
164
|
+
// about the file: skipped with its knowledge and manifest hash intact, tried again next review.
|
|
165
|
+
if (job.kind === 'read-error' || job.kind === 'failed') {
|
|
166
|
+
if (job.kind === 'read-error')
|
|
167
|
+
report.readErrors += 1;
|
|
168
|
+
else
|
|
169
|
+
report.extractErrors += 1;
|
|
170
|
+
fail(f.path, 'extract', job.kind === 'read-error' ? `read: ${job.reason}` : job.reason);
|
|
171
|
+
measured.set(f.path, owedCalls(detail.extractor, f.path)); // learns nothing: keep what it was owed
|
|
172
|
+
return true;
|
|
177
173
|
}
|
|
178
174
|
// Recorded, so a file found binary on read is not re-read by every later review.
|
|
179
175
|
if (job.kind === 'binary') {
|
|
@@ -181,78 +177,78 @@ async function reviewRun(args, flush) {
|
|
|
181
177
|
finished.add(f.path);
|
|
182
178
|
await store.markStale(scope, srcRef(rootId, f.path), f.hash);
|
|
183
179
|
report.staleMarked += 1;
|
|
184
|
-
|
|
180
|
+
return true;
|
|
185
181
|
}
|
|
186
|
-
if (job.kind === 'failed')
|
|
187
|
-
throw job.error;
|
|
188
182
|
if (job.kind === 'none') {
|
|
189
183
|
finished.add(f.path);
|
|
190
|
-
|
|
184
|
+
return true;
|
|
191
185
|
}
|
|
192
|
-
const { extracted: ex, sections, pack } = job.extraction;
|
|
193
186
|
report.extracted += 1;
|
|
194
|
-
|
|
187
|
+
// The extract cache is written HERE, in plan order, so the cache, the counts and the order match a sequential run.
|
|
188
|
+
if (!job.extraction.cached)
|
|
189
|
+
await putExtract(store, f.hash, job.extraction.extracted);
|
|
195
190
|
let complete = true;
|
|
196
|
-
if (
|
|
197
|
-
const kinds = new Set(spec.nodeKinds);
|
|
198
|
-
const toJudge = [];
|
|
199
|
-
for (const node of ex.nodes) {
|
|
200
|
-
const index = node.badges?.[SECTION_INDEX_BADGE];
|
|
201
|
-
const text = typeof index === 'number' ? sections[index]?.text : undefined;
|
|
202
|
-
if (!kinds.has(node.kind) || !text)
|
|
203
|
-
continue;
|
|
204
|
-
if (judgedPrior.get(node.ref) === hashText(text))
|
|
205
|
-
stored.refresh(node.ref, f.path, f.hash, batch, entities);
|
|
206
|
-
else
|
|
207
|
-
toJudge.push({ ref: node.ref, text });
|
|
208
|
-
}
|
|
209
|
-
const unjudgedBefore = batch.unjudged;
|
|
210
|
-
// Staged per file: a section counts as judged only once its facts are written,
|
|
211
|
-
// so an abort mid-file leaves it to be judged again (written whole or not at all).
|
|
212
|
-
const judgedHere = new Map();
|
|
191
|
+
if (judge) {
|
|
213
192
|
progress({ phase: 'judge', done: i, total: changed.length, path: f.path });
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
//
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
for (const c of claims) {
|
|
224
|
-
newClaims.delete(c.ref);
|
|
225
|
-
newClaims.set(c.ref, { text: c.text, hash: f.hash });
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
catch (err) {
|
|
230
|
-
if (!signal?.aborted)
|
|
231
|
-
throw err;
|
|
232
|
-
batch.take();
|
|
233
|
-
report.aborted = true;
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
for (const [ref, h] of judgedHere)
|
|
237
|
-
judgedNow.set(ref, h);
|
|
238
|
-
complete = batch.unjudged === unjudgedBefore;
|
|
239
|
-
// A content-changed file may revert to the manifest bytes: then all it had to judge — and what it was
|
|
240
|
-
// already owed from earlier cuts — is still owed.
|
|
241
|
-
if (!complete) {
|
|
242
|
-
const contentChanged = detail.prior.get(f.path) !== f.hash;
|
|
243
|
-
measured.set(f.path, contentChanged ? toJudge.length + (detail.extractor.owed.get(f.path) ?? 0) : batch.unjudged - unjudgedBefore);
|
|
193
|
+
inWriter = false; // waiting for the model calls is not the writer working
|
|
194
|
+
const outcome = await applyJudge(judgeCtx, f, judge);
|
|
195
|
+
inWriter = true;
|
|
196
|
+
if (outcome === 'aborted')
|
|
197
|
+
return false;
|
|
198
|
+
if (outcome === 'failed') {
|
|
199
|
+
// Judging failed as a whole: leave the file exactly as it was (facts, manifest hash, owed) so the next review retries it.
|
|
200
|
+
measured.set(f.path, owedCalls(detail.extractor, f.path));
|
|
201
|
+
return true;
|
|
244
202
|
}
|
|
203
|
+
complete = outcome.complete;
|
|
245
204
|
}
|
|
246
205
|
progress({ phase: 'write', done: i, total: changed.length, path: f.path });
|
|
206
|
+
const t0 = now();
|
|
247
207
|
await write();
|
|
248
208
|
await store.markStale(scope, srcRef(rootId, f.path), f.hash);
|
|
249
209
|
report.staleMarked += 1;
|
|
210
|
+
args.onStep?.({ path: f.path, phase: 'write', ms: now() - t0, outcome: 'ok' });
|
|
250
211
|
if (complete) {
|
|
251
212
|
filesNow.set(f.path, f.hash);
|
|
252
213
|
finished.add(f.path);
|
|
253
214
|
}
|
|
215
|
+
return true;
|
|
216
|
+
};
|
|
217
|
+
const applyFile = async (slot) => {
|
|
218
|
+
inWriter = true;
|
|
219
|
+
try {
|
|
220
|
+
return await applyInner(slot);
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
inWriter = false;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
const queue = [];
|
|
227
|
+
let applied = 0;
|
|
228
|
+
const drain = async (keep) => {
|
|
229
|
+
while (!cut && queue.length > keep)
|
|
230
|
+
if (await applyFile(queue.shift()))
|
|
231
|
+
applied += 1;
|
|
232
|
+
};
|
|
233
|
+
let completed = false;
|
|
234
|
+
try {
|
|
235
|
+
for await (const { item: f, i, result } of prefetchOrdered(changed, concurrency, prefetch, runSignal)) {
|
|
236
|
+
if (result.status === 'rejected')
|
|
237
|
+
throw result.reason; // a prefetch job settles to a Prefetch; this is a broken invariant
|
|
238
|
+
const job = result.value;
|
|
239
|
+
queue.push({ f, i, job, judge: job.kind === 'extracted' ? startJudge(dispatch, f, job.extraction) : undefined });
|
|
240
|
+
await drain(judgeWidth - 1);
|
|
241
|
+
}
|
|
242
|
+
await drain(0);
|
|
243
|
+
completed = true;
|
|
244
|
+
}
|
|
245
|
+
finally {
|
|
246
|
+
if (!completed)
|
|
247
|
+
stopper.abort(); // a thrown error stops every queued and in-flight call
|
|
248
|
+
signal?.removeEventListener('abort', relay);
|
|
249
|
+
await Promise.allSettled(queue.map((s) => s.judge?.result)); // nothing outlives the run
|
|
254
250
|
}
|
|
255
|
-
if (
|
|
251
|
+
if (applied < changed.length)
|
|
256
252
|
report.aborted = true;
|
|
257
253
|
if (!report.aborted) {
|
|
258
254
|
await retire(detail.plan.removed);
|
|
@@ -282,7 +278,7 @@ async function reviewRun(args, flush) {
|
|
|
282
278
|
if (!report.aborted && canCompare) {
|
|
283
279
|
outcome = await compareNew({
|
|
284
280
|
args, pending: newClaims, known: stored.facts, budget, batch,
|
|
285
|
-
onPairs: (total) => progress({ phase: '
|
|
281
|
+
onPairs: (total) => progress({ phase: 'compare', done: 0, total }),
|
|
286
282
|
});
|
|
287
283
|
// Aborted only if the abort landed before the last pair was compared.
|
|
288
284
|
if (outcome?.aborted)
|