@seanhogg/builderforce-memory 2026.10.79 → 2026.10.81
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 +45 -5
- package/dist/knowledge/build.js.map +1 -1
- package/dist/knowledge/cooperative.d.ts +51 -0
- package/dist/knowledge/cooperative.d.ts.map +1 -0
- package/dist/knowledge/cooperative.js +58 -0
- package/dist/knowledge/cooperative.js.map +1 -0
- package/dist/knowledge/findings.d.ts +7 -1
- package/dist/knowledge/findings.d.ts.map +1 -1
- package/dist/knowledge/findings.js +39 -14
- package/dist/knowledge/findings.js.map +1 -1
- package/dist/knowledge/ignoreMatcher.d.ts +25 -0
- package/dist/knowledge/ignoreMatcher.d.ts.map +1 -0
- package/dist/knowledge/ignoreMatcher.js +121 -0
- package/dist/knowledge/ignoreMatcher.js.map +1 -0
- package/dist/knowledge/index.d.ts +6 -2
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +4 -1
- package/dist/knowledge/index.js.map +1 -1
- package/dist/knowledge/model.d.ts +26 -0
- package/dist/knowledge/model.d.ts.map +1 -1
- package/dist/knowledge/plan.d.ts +6 -0
- package/dist/knowledge/plan.d.ts.map +1 -1
- package/dist/knowledge/plan.js +17 -1
- package/dist/knowledge/plan.js.map +1 -1
- package/dist/knowledge/reviewAnalyse.d.ts.map +1 -1
- package/dist/knowledge/reviewAnalyse.js +6 -1
- package/dist/knowledge/reviewAnalyse.js.map +1 -1
- package/dist/knowledge/reviewFinish.d.ts.map +1 -1
- package/dist/knowledge/reviewFinish.js +7 -1
- package/dist/knowledge/reviewFinish.js.map +1 -1
- package/dist/knowledge/reviewModel.d.ts +10 -0
- package/dist/knowledge/reviewModel.d.ts.map +1 -1
- package/dist/knowledge/rules/factStore.d.ts +11 -4
- package/dist/knowledge/rules/factStore.d.ts.map +1 -1
- package/dist/knowledge/rules/factStore.js +57 -25
- package/dist/knowledge/rules/factStore.js.map +1 -1
- package/dist/knowledge/rules/fixpointRules.d.ts +68 -0
- package/dist/knowledge/rules/fixpointRules.d.ts.map +1 -0
- package/dist/knowledge/rules/fixpointRules.js +0 -0
- package/dist/knowledge/rules/fixpointRules.js.map +1 -0
- package/dist/knowledge/sources.d.ts +9 -9
- package/dist/knowledge/sources.d.ts.map +1 -1
- package/dist/knowledge/sources.js +11 -50
- package/dist/knowledge/sources.js.map +1 -1
- package/dist/knowledge/statManifest.d.ts +89 -0
- package/dist/knowledge/statManifest.d.ts.map +1 -0
- package/dist/knowledge/statManifest.js +125 -0
- package/dist/knowledge/statManifest.js.map +1 -0
- package/dist/stats/estimators.d.ts +8 -1
- package/dist/stats/estimators.d.ts.map +1 -1
- package/dist/stats/estimators.js +18 -7
- package/dist/stats/estimators.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/exports.snapshot.json +12 -0
- package/src/contract/regression.assets.test.ts +2 -2
- package/src/contract/regression.factStoreBatchedLoad.test.ts +62 -0
- package/src/contract/regression.factStoreFastPath.test.ts +116 -0
- package/src/contract/regression.findingsProvider.test.ts +80 -0
- package/src/contract/regression.ignoreMatcher.test.ts +116 -0
- package/src/contract/types.contract.test.ts +9 -1
- package/src/knowledge/build.ts +37 -5
- package/src/knowledge/cooperative.test.ts +57 -0
- package/src/knowledge/cooperative.ts +76 -0
- package/src/knowledge/docs.test.ts +30 -0
- package/src/knowledge/findings.ts +55 -14
- package/src/knowledge/ignoreMatcher.ts +143 -0
- package/src/knowledge/index.ts +9 -2
- package/src/knowledge/model.ts +24 -0
- package/src/knowledge/plan.ts +20 -1
- package/src/knowledge/reviewAnalyse.ts +6 -1
- package/src/knowledge/reviewFinish.ts +7 -1
- package/src/knowledge/reviewModel.ts +6 -0
- package/src/knowledge/rules/factStore.ts +59 -21
- package/src/knowledge/rules/fixpointRules.test.ts +94 -0
- package/src/knowledge/rules/fixpointRules.ts +0 -0
- package/src/knowledge/sources.ts +12 -46
- package/src/knowledge/statManifest.test.ts +122 -0
- package/src/knowledge/statManifest.ts +176 -0
- package/src/stats/estimators.ts +18 -7
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/statManifest.ts — the STAT PREFILTER (PRD 41 §B2 steps 1–2): a per-root manifest of
|
|
3
|
+
* `(path, size, mtimeNs, fileId, hash, recordedAtMs)` so a warm plan reuses a stored content hash
|
|
4
|
+
* without reading the file, and only stats — never content — decide it.
|
|
5
|
+
*
|
|
6
|
+
* Rules implemented here, straight from the PRD:
|
|
7
|
+
* 0. Content identity is unchanged by this module: the stored `hash` is the host's content hash
|
|
8
|
+
* (EOL-normalized for text, raw for binary — see contentHash.ts); this manifest only caches it.
|
|
9
|
+
* 1. Stat tuple match → reuse the stored hash. Stats are BigInt (`bigint: true` from fs.stat):
|
|
10
|
+
* Windows file ids and ns mtimes lose precision as JS numbers, so {@link StatRecord} carries
|
|
11
|
+
* them as bigint and the manifest persists them as decimal strings.
|
|
12
|
+
* 2. Racy-file rule (git's index): a file whose mtime is within {@link RACY_WINDOW_MS} of the
|
|
13
|
+
* manifest's write time, or of `now` at plan time, is re-hashed whatever its stat says.
|
|
14
|
+
* 4. Directory Merkle hashes stay the identity of a subtree (merkle.ts) — this manifest never
|
|
15
|
+
* enters a digest, a cache key or the plan output (PRD 41 §B8.3: mtime is only a hint).
|
|
16
|
+
*
|
|
17
|
+
* Determinism (§B8): every output here is sorted by path; the only clock use is the injected
|
|
18
|
+
* `now` (default `Date.now`) stamping `recordedAtMs`, which is persisted metadata, never part of
|
|
19
|
+
* any digest. Hosts that need a deterministic plan inject `now`.
|
|
20
|
+
*
|
|
21
|
+
* The plan itself consumes hashes from `DirectorySource.list()`; this module is the helper a
|
|
22
|
+
* host's listing calls per file ({@link createStatPrefilter}) plus the manifest persistence the
|
|
23
|
+
* plan updates after a successful run ({@link saveStatManifest}).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import type { IngestManifest } from '../ingest/types.js';
|
|
27
|
+
import { canonicalJson, compareStr, normalizePath } from './refs.js';
|
|
28
|
+
import { manifestMap, saveManifestMap } from './sources.js';
|
|
29
|
+
|
|
30
|
+
/** git's racy-window: an mtime within this many ms of the index write is re-checked next plan. */
|
|
31
|
+
export const RACY_WINDOW_MS = 2000;
|
|
32
|
+
|
|
33
|
+
/** One file's stat tuple, read with `bigint: true`. `fileId` is `ino` on POSIX, the file id on Windows; null when the host cannot say. */
|
|
34
|
+
export interface StatRecord {
|
|
35
|
+
size: number;
|
|
36
|
+
mtimeNs: bigint;
|
|
37
|
+
fileId: bigint | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A stat-manifest row: the stat tuple the hash was cached under, the hash, and when the manifest was written (ms). */
|
|
41
|
+
export interface StatManifestEntry extends StatRecord {
|
|
42
|
+
hash: string;
|
|
43
|
+
recordedAtMs: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The manifest key under the ingest manifest: one stat manifest per root. */
|
|
47
|
+
export const statManifestKey = (rootId: string): string => `stats:${rootId}`;
|
|
48
|
+
|
|
49
|
+
/** Stat-tuple equality: same size, same mtime to the nanosecond, same file id (a null fileId matches only a null fileId). */
|
|
50
|
+
export function statMatches(a: StatRecord, b: StatRecord): boolean {
|
|
51
|
+
return a.size === b.size && a.mtimeNs === b.mtimeNs && a.fileId === b.fileId;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The racy-file rule (§B2 step 2): true when the file may have been written in the same tick as
|
|
56
|
+
* the manifest — its mtime is within {@link RACY_WINDOW_MS} of the manifest's `recordedAtMs`, or
|
|
57
|
+
* of `nowMs` (a file touched just now whose mtime we are about to record). A racy file is
|
|
58
|
+
* re-hashed on the next plan whatever its stat says.
|
|
59
|
+
*/
|
|
60
|
+
export function isRacy(stat: StatRecord, recordedAtMs: number, nowMs: number): boolean {
|
|
61
|
+
const mtimeMs = Number(stat.mtimeNs / 1_000_000n);
|
|
62
|
+
return Math.abs(mtimeMs - recordedAtMs) < RACY_WINDOW_MS || Math.abs(mtimeMs - nowMs) < RACY_WINDOW_MS;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface PrefilterDecision {
|
|
66
|
+
/** Paths whose stored hash is reused (stat matched, not racy), sorted. */
|
|
67
|
+
reused: readonly string[];
|
|
68
|
+
/** Paths that must be hashed (new, stat moved, or racy), sorted. */
|
|
69
|
+
hash: readonly string[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The ONE prefilter decision, pure and order-independent: a path reuses its stored hash only when
|
|
74
|
+
* its prior entry exists, the stat tuple matches, and the racy rule does not fire. Everything else
|
|
75
|
+
* is hashed. `nowMs` is the plan's clock.
|
|
76
|
+
*/
|
|
77
|
+
export function prefilterStats(
|
|
78
|
+
listed: ReadonlyArray<{ path: string; stat: StatRecord }>,
|
|
79
|
+
prior: ReadonlyMap<string, StatManifestEntry>,
|
|
80
|
+
nowMs: number,
|
|
81
|
+
): PrefilterDecision {
|
|
82
|
+
const reused: string[] = [];
|
|
83
|
+
const hash: string[] = [];
|
|
84
|
+
for (const { path, stat } of listed) {
|
|
85
|
+
const p = normalizePath(path);
|
|
86
|
+
const entry = prior.get(p);
|
|
87
|
+
if (entry && statMatches(entry, stat) && !isRacy(stat, entry.recordedAtMs, nowMs)) reused.push(p);
|
|
88
|
+
else hash.push(p);
|
|
89
|
+
}
|
|
90
|
+
reused.sort(compareStr);
|
|
91
|
+
hash.sort(compareStr);
|
|
92
|
+
return { reused, hash };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* A per-file hash provider for a host's `DirectorySource.list()`: returns the stored hash when
|
|
97
|
+
* {@link prefilterStats} reuses the path, else `hashPath(path)`. `hashPath` reads the file and
|
|
98
|
+
* hashes it (EOL-normalized, per the host's content-hash rule) — the only path that reads bytes.
|
|
99
|
+
* `now` defaults to `Date.now`; inject it for deterministic plans.
|
|
100
|
+
*/
|
|
101
|
+
export function createStatPrefilter(
|
|
102
|
+
prior: ReadonlyMap<string, StatManifestEntry>,
|
|
103
|
+
hashPath: (listedPath: string) => Promise<string>,
|
|
104
|
+
now: () => number = () => Date.now(),
|
|
105
|
+
): (entry: { path: string; stat: StatRecord }) => Promise<{ path: string; hash: string }> {
|
|
106
|
+
return async ({ path, stat }) => {
|
|
107
|
+
const normalized = normalizePath(path);
|
|
108
|
+
const entry = prior.get(normalized);
|
|
109
|
+
if (entry && statMatches(entry, stat) && !isRacy(stat, entry.recordedAtMs, now())) {
|
|
110
|
+
return { path: normalized, hash: entry.hash };
|
|
111
|
+
}
|
|
112
|
+
return { path: normalized, hash: await hashPath(path) };
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* ── persistence (the ingest manifest, one canonical JSON blob per root) ─────────────────────────── */
|
|
117
|
+
|
|
118
|
+
interface StatRow {
|
|
119
|
+
size: number;
|
|
120
|
+
mtimeNs: string;
|
|
121
|
+
fileId: string | null;
|
|
122
|
+
hash: string;
|
|
123
|
+
recordedAtMs: number;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const encode = (entries: ReadonlyMap<string, StatManifestEntry>): string => canonicalJson(
|
|
127
|
+
[...entries.entries()].sort((a, b) => compareStr(a[0], b[0])).map(([path, e]): [string, StatRow] => [path, {
|
|
128
|
+
size: e.size,
|
|
129
|
+
mtimeNs: e.mtimeNs.toString(),
|
|
130
|
+
fileId: e.fileId === null ? null : e.fileId.toString(),
|
|
131
|
+
hash: e.hash,
|
|
132
|
+
recordedAtMs: e.recordedAtMs,
|
|
133
|
+
}]),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const decode = (json: string): Map<string, StatManifestEntry> => {
|
|
137
|
+
const rows = JSON.parse(json) as Array<[string, StatRow]>;
|
|
138
|
+
const out = new Map<string, StatManifestEntry>();
|
|
139
|
+
for (const [path, r] of rows ?? []) {
|
|
140
|
+
out.set(path, {
|
|
141
|
+
size: r.size,
|
|
142
|
+
mtimeNs: BigInt(r.mtimeNs),
|
|
143
|
+
fileId: r.fileId === null ? null : BigInt(r.fileId),
|
|
144
|
+
hash: r.hash,
|
|
145
|
+
recordedAtMs: r.recordedAtMs,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return out;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/** The stored stat manifest for `rootId` (empty when none — the first run hashes everything, the existing fallback). */
|
|
152
|
+
export async function loadStatManifest(manifest: IngestManifest, rootId: string): Promise<Map<string, StatManifestEntry>> {
|
|
153
|
+
const map = await manifestMap(manifest, statManifestKey(rootId));
|
|
154
|
+
const raw = map.get('entries');
|
|
155
|
+
if (raw === undefined) return new Map();
|
|
156
|
+
try {
|
|
157
|
+
return decode(raw);
|
|
158
|
+
} catch {
|
|
159
|
+
return new Map(); // a corrupt stat cache is a perf fallback only: re-hash, never fail the plan
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Persists the stat manifest for `rootId`: every listed file's hash and the stat tuple it was
|
|
165
|
+
* cached under, stamped with `recordedAtMs` (the manifest write time the racy rule compares to).
|
|
166
|
+
*/
|
|
167
|
+
export async function saveStatManifest(
|
|
168
|
+
manifest: IngestManifest,
|
|
169
|
+
rootId: string,
|
|
170
|
+
files: ReadonlyArray<{ path: string; hash: string; stat: StatRecord }>,
|
|
171
|
+
recordedAtMs: number,
|
|
172
|
+
): Promise<void> {
|
|
173
|
+
const entries = new Map<string, StatManifestEntry>();
|
|
174
|
+
for (const f of files) entries.set(normalizePath(f.path), { ...f.stat, hash: f.hash, recordedAtMs });
|
|
175
|
+
await saveManifestMap(manifest, statManifestKey(rootId), new Map([['entries', encode(entries)]]));
|
|
176
|
+
}
|
package/src/stats/estimators.ts
CHANGED
|
@@ -57,7 +57,14 @@ export function kaplanMeier(rows: readonly { time: number; event: 0 | 1 }[]): Ka
|
|
|
57
57
|
/**
|
|
58
58
|
* Goel–Okumoto NHPP MLE: mean failures μ(t) = a(1 − e^{−bt}). a is solved in
|
|
59
59
|
* closed form given b; b is refined by bisection on the score equation
|
|
60
|
-
* Σ tᵢ = n/b − n·
|
|
60
|
+
* Σ tᵢ = n/b − n·tMax·e^{−b·tMax}/(1 − e^{−b·tMax}). Needs n ≥ 2 distinct times.
|
|
61
|
+
*
|
|
62
|
+
* The score uses tMax (the censoring horizon), not tMean: with likelihood
|
|
63
|
+
* L ∝ aⁿ e^{−a(1−e^{−bT})} · bⁿ e^{−bΣtᵢ} · Πe^{−btᵢ}, the b score after profiling out a
|
|
64
|
+
* substitutes the END time T = tMax in the per-observation exposure term, giving the
|
|
65
|
+
* root-finding target below. An earlier version used tMean, which has no stationary point
|
|
66
|
+
* on the data and drove b to the search bound (a saturated, meaningless fit) for every
|
|
67
|
+
* input.
|
|
61
68
|
*/
|
|
62
69
|
export function goelOkumotoMLE(
|
|
63
70
|
times: readonly number[],
|
|
@@ -66,19 +73,23 @@ export function goelOkumotoMLE(
|
|
|
66
73
|
const n = sorted.length;
|
|
67
74
|
if (n < 2) return { status: "insufficient-data" };
|
|
68
75
|
const tMax = sorted[n - 1]!;
|
|
69
|
-
const
|
|
76
|
+
const tSum = sorted.reduce((a, b) => a + b, 0);
|
|
77
|
+
// g(b) = n/b − n·tMax·e^{−b·tMax}/(1 − e^{−b·tMax}); the MLE b is the root of tSum = g(b).
|
|
78
|
+
// g is strictly decreasing in b (n/b falls, and the subtracted term grows toward n·tMax),
|
|
79
|
+
// so tSum − g(b) is strictly INCREASING: negative below the root, positive above it.
|
|
70
80
|
const score = (b: number) => {
|
|
71
|
-
if (b <= 0) return Infinity;
|
|
81
|
+
if (b <= 0) return -Infinity;
|
|
72
82
|
const denom = 1 - Math.exp(-b * tMax);
|
|
73
|
-
return n / b - (n *
|
|
83
|
+
return tSum - (n / b - (n * tMax * Math.exp(-b * tMax)) / denom);
|
|
74
84
|
};
|
|
75
85
|
let lo = 1e-9;
|
|
76
86
|
let hi = 1;
|
|
77
|
-
|
|
87
|
+
// Expand until the score is positive (past the increasing root); cap the search.
|
|
88
|
+
while (score(hi) < 0 && hi < 1e6) hi *= 2;
|
|
78
89
|
for (let i = 0; i < 100; i++) {
|
|
79
90
|
const mid = (lo + hi) / 2;
|
|
80
|
-
if (score(mid) > 0)
|
|
81
|
-
else
|
|
91
|
+
if (score(mid) > 0) hi = mid;
|
|
92
|
+
else lo = mid;
|
|
82
93
|
}
|
|
83
94
|
const b = (lo + hi) / 2;
|
|
84
95
|
const a = n / (1 - Math.exp(-b * tMax));
|