@seanhogg/builderforce-memory 2026.10.62 → 2026.10.63
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/stats/distributions.d.ts +34 -0
- package/dist/stats/distributions.d.ts.map +1 -0
- package/dist/stats/distributions.js +180 -0
- package/dist/stats/distributions.js.map +1 -0
- package/dist/stats/estimators.d.ts +53 -0
- package/dist/stats/estimators.d.ts.map +1 -0
- package/dist/stats/estimators.js +79 -0
- package/dist/stats/estimators.js.map +1 -0
- package/dist/stats/fitting.d.ts +48 -0
- package/dist/stats/fitting.d.ts.map +1 -0
- package/dist/stats/fitting.js +209 -0
- package/dist/stats/fitting.js.map +1 -0
- package/dist/stats/index.d.ts +26 -0
- package/dist/stats/index.d.ts.map +1 -0
- package/dist/stats/index.js +18 -0
- package/dist/stats/index.js.map +1 -0
- package/dist/stats/information.d.ts +9 -0
- package/dist/stats/information.d.ts.map +1 -0
- package/dist/stats/information.js +21 -0
- package/dist/stats/information.js.map +1 -0
- package/dist/stats/multivariate.d.ts +25 -0
- package/dist/stats/multivariate.d.ts.map +1 -0
- package/dist/stats/multivariate.js +89 -0
- package/dist/stats/multivariate.js.map +1 -0
- package/dist/stats/policies.d.ts +19 -0
- package/dist/stats/policies.d.ts.map +1 -0
- package/dist/stats/policies.js +75 -0
- package/dist/stats/policies.js.map +1 -0
- package/dist/stats/prng.d.ts +11 -0
- package/dist/stats/prng.d.ts.map +1 -0
- package/dist/stats/prng.js +41 -0
- package/dist/stats/prng.js.map +1 -0
- package/dist/stats/scores.d.ts +33 -0
- package/dist/stats/scores.d.ts.map +1 -0
- package/dist/stats/scores.js +134 -0
- package/dist/stats/scores.js.map +1 -0
- package/dist/stats/tests.d.ts +48 -0
- package/dist/stats/tests.d.ts.map +1 -0
- package/dist/stats/tests.js +143 -0
- package/dist/stats/tests.js.map +1 -0
- package/dist/stats/timeSeries.d.ts +34 -0
- package/dist/stats/timeSeries.d.ts.map +1 -0
- package/dist/stats/timeSeries.js +71 -0
- package/dist/stats/timeSeries.js.map +1 -0
- package/dist/working/fold.d.ts +51 -0
- package/dist/working/fold.d.ts.map +1 -0
- package/dist/working/fold.js +218 -0
- package/dist/working/fold.js.map +1 -0
- package/dist/working/index.d.ts +1 -0
- package/dist/working/index.d.ts.map +1 -1
- package/dist/working/index.js +1 -0
- package/dist/working/index.js.map +1 -1
- package/package.json +5 -1
- package/src/contract/exports.snapshot.json +41 -0
- package/src/stats/distributions.ts +180 -0
- package/src/stats/estimators.ts +101 -0
- package/src/stats/fitting.ts +252 -0
- package/src/stats/index.ts +59 -0
- package/src/stats/information.ts +20 -0
- package/src/stats/multivariate.ts +93 -0
- package/src/stats/policies.ts +84 -0
- package/src/stats/prng.ts +45 -0
- package/src/stats/scores.ts +135 -0
- package/src/stats/stats.test.ts +294 -0
- package/src/stats/tests.ts +164 -0
- package/src/stats/timeSeries.ts +92 -0
- package/src/working/fold.test.ts +196 -0
- package/src/working/fold.ts +241 -0
- package/src/working/index.ts +1 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Statistical tests for the stats module (PRD 41 Part D / D13): Wilcoxon signed-rank
|
|
3
|
+
* with the paired Hodges–Lehmann estimator, a seeded paired bootstrap CI, and the
|
|
4
|
+
* sign-test SPRT. All deterministic under their seeds.
|
|
5
|
+
*/
|
|
6
|
+
import { createRng } from "./prng.js";
|
|
7
|
+
const WILCOXON_P = (() => {
|
|
8
|
+
// Exact null distribution of W+ for n <= 60 via DP over subset sums.
|
|
9
|
+
const n = 60;
|
|
10
|
+
const total = (n * (n + 1)) / 2;
|
|
11
|
+
const dp = new Array(total + 1).fill(0);
|
|
12
|
+
dp[0] = 1;
|
|
13
|
+
for (let rank = 1; rank <= n; rank++) {
|
|
14
|
+
for (let s = total; s >= rank; s--)
|
|
15
|
+
dp[s] = (dp[s] ?? 0) + (dp[s - rank] ?? 0);
|
|
16
|
+
}
|
|
17
|
+
const cum = new Array(total + 1).fill(0);
|
|
18
|
+
let running = 0;
|
|
19
|
+
for (let s = 0; s <= total; s++) {
|
|
20
|
+
running += dp[s];
|
|
21
|
+
cum[s] = running / 2 ** n;
|
|
22
|
+
}
|
|
23
|
+
return cum;
|
|
24
|
+
})();
|
|
25
|
+
function wilcoxonPValue(wPlus, n) {
|
|
26
|
+
if (n > 60) {
|
|
27
|
+
// Normal approximation with continuity correction.
|
|
28
|
+
const mean = (n * (n + 1)) / 4;
|
|
29
|
+
const sd = Math.sqrt((n * (n + 1) * (2 * n + 1)) / 24);
|
|
30
|
+
const z = (wPlus - mean + 0.5) / sd;
|
|
31
|
+
return Math.min(1, Math.exp(-0.5 * z * z) * 0.5 * 2); // two-sided via |z|
|
|
32
|
+
}
|
|
33
|
+
const total = (n * (n + 1)) / 2;
|
|
34
|
+
const lo = Math.max(0, Math.floor(wPlus));
|
|
35
|
+
const hi = Math.min(total, Math.ceil(wPlus));
|
|
36
|
+
const pLo = WILCOXON_P[lo] ?? 0;
|
|
37
|
+
const pHi = 1 - (WILCOXON_P[hi - 1] ?? 0);
|
|
38
|
+
return Math.min(1, 2 * Math.min(pLo, pHi));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Wilcoxon signed-rank test on the nonzero paired differences of `before` and
|
|
42
|
+
* `after`, plus the paired Hodges–Lehmann location estimator.
|
|
43
|
+
*/
|
|
44
|
+
export function wilcoxonSignedRank(before, after) {
|
|
45
|
+
const diffs = [];
|
|
46
|
+
const n0 = Math.min(before.length, after.length);
|
|
47
|
+
for (let i = 0; i < n0; i++) {
|
|
48
|
+
const d = after[i] - before[i];
|
|
49
|
+
if (d !== 0)
|
|
50
|
+
diffs.push(d);
|
|
51
|
+
}
|
|
52
|
+
const n = diffs.length;
|
|
53
|
+
if (n === 0)
|
|
54
|
+
return { n: 0, wPlus: 0, pValue: 1, hodgesLehmann: 0 };
|
|
55
|
+
const abs = diffs.map((d) => Math.abs(d));
|
|
56
|
+
const order = abs.map((_, i) => i).sort((p, q) => abs[p] - abs[q]);
|
|
57
|
+
const ranks = new Array(n).fill(0);
|
|
58
|
+
let i = 0;
|
|
59
|
+
while (i < n) {
|
|
60
|
+
let j = i;
|
|
61
|
+
while (j + 1 < n && abs[order[j + 1]] === abs[order[i]])
|
|
62
|
+
j++;
|
|
63
|
+
const avg = (i + j) / 2 + 1;
|
|
64
|
+
for (let k = i; k <= j; k++)
|
|
65
|
+
ranks[order[k]] = avg;
|
|
66
|
+
i = j + 1;
|
|
67
|
+
}
|
|
68
|
+
let wPlus = 0;
|
|
69
|
+
for (let k = 0; k < n; k++)
|
|
70
|
+
if (diffs[k] > 0)
|
|
71
|
+
wPlus += ranks[k];
|
|
72
|
+
// Hodges–Lehmann: median of Walsh averages over positive-magnitude pairs.
|
|
73
|
+
const walsh = [];
|
|
74
|
+
for (let p = 0; p < n; p++) {
|
|
75
|
+
for (let q = p; q < n; q++)
|
|
76
|
+
walsh.push((diffs[p] + diffs[q]) / 2);
|
|
77
|
+
}
|
|
78
|
+
walsh.sort((a, b) => a - b);
|
|
79
|
+
const hl = walsh.length % 2 === 1
|
|
80
|
+
? walsh[(walsh.length - 1) / 2]
|
|
81
|
+
: (walsh[walsh.length / 2 - 1] + walsh[walsh.length / 2]) / 2;
|
|
82
|
+
return { n, wPlus, pValue: wilcoxonPValue(wPlus, n), hodgesLehmann: hl };
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Paired bootstrap confidence interval for the mean difference `after − before`,
|
|
86
|
+
* resampling PAIRS so the pairing is preserved. Deterministic under `seed`.
|
|
87
|
+
*/
|
|
88
|
+
export function pairedBootstrapCi(before, after, options = {}) {
|
|
89
|
+
const iterations = options.iterations ?? 2000;
|
|
90
|
+
const confidence = options.confidence ?? 0.95;
|
|
91
|
+
const rng = createRng(options.seed ?? 1);
|
|
92
|
+
const n = Math.min(before.length, after.length);
|
|
93
|
+
const diffs = [];
|
|
94
|
+
for (let i = 0; i < n; i++)
|
|
95
|
+
diffs.push(after[i] - before[i]);
|
|
96
|
+
const estimate = diffs.reduce((a, b) => a + b, 0) / Math.max(diffs.length, 1);
|
|
97
|
+
if (n === 0)
|
|
98
|
+
return { low: 0, high: 0, estimate: 0 };
|
|
99
|
+
const means = [];
|
|
100
|
+
for (let b = 0; b < iterations; b++) {
|
|
101
|
+
let s = 0;
|
|
102
|
+
for (let i = 0; i < n; i++)
|
|
103
|
+
s += diffs[Math.floor(rng() * n)];
|
|
104
|
+
means.push(s / n);
|
|
105
|
+
}
|
|
106
|
+
means.sort((a, b) => a - b);
|
|
107
|
+
const alpha = 1 - confidence;
|
|
108
|
+
return {
|
|
109
|
+
low: means[Math.floor((alpha / 2) * iterations)] ?? estimate,
|
|
110
|
+
high: means[Math.floor((1 - alpha / 2) * iterations) - 1] ?? estimate,
|
|
111
|
+
estimate,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Sequential probability ratio test on paired signs. H0: P(after > before) = p0,
|
|
116
|
+
* H1: = p1. Decides as soon as the likelihood ratio crosses a boundary, else reports
|
|
117
|
+
* `continue` at `maxObservations`. Deterministic — the observations are supplied.
|
|
118
|
+
*/
|
|
119
|
+
export function signTestSprt(pairs, options = {}) {
|
|
120
|
+
const p0 = options.p0 ?? 0.5;
|
|
121
|
+
const p1 = options.p1 ?? 0.7;
|
|
122
|
+
const alpha = options.alpha ?? 0.05;
|
|
123
|
+
const beta = options.beta ?? 0.1;
|
|
124
|
+
const aBound = Math.log((1 - beta) / alpha);
|
|
125
|
+
const bBound = Math.log(beta / (1 - alpha));
|
|
126
|
+
let llr = 0;
|
|
127
|
+
let observations = 0;
|
|
128
|
+
for (const [beforeV, afterV] of pairs) {
|
|
129
|
+
const plus = afterV > beforeV;
|
|
130
|
+
const inc = Math.log((plus ? p1 : 1 - p1) / (plus ? p0 : 1 - p0));
|
|
131
|
+
const incAlt = options.alternative === "less"
|
|
132
|
+
? Math.log((plus ? 1 - p1 : p1) / (plus ? 1 - p0 : p0))
|
|
133
|
+
: inc;
|
|
134
|
+
llr += incAlt;
|
|
135
|
+
observations++;
|
|
136
|
+
if (llr >= aBound)
|
|
137
|
+
return { decision: "reject-null", likelihoodRatio: llr, observations };
|
|
138
|
+
if (llr <= bBound)
|
|
139
|
+
return { decision: "accept-null", likelihoodRatio: llr, observations };
|
|
140
|
+
}
|
|
141
|
+
return { decision: "continue", likelihoodRatio: llr, observations };
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=tests.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tests.js","sourceRoot":"","sources":["../../src/stats/tests.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAY,MAAM,WAAW,CAAC;AAEhD,MAAM,UAAU,GAAa,CAAC,GAAG,EAAE;IAC/B,qEAAqE;IACrE,MAAM,CAAC,GAAG,EAAE,CAAC;IACb,MAAM,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAChC,MAAM,EAAE,GAAa,IAAI,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClD,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACV,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC;QACnC,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,EAAE;YAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACnF,CAAC;IACD,MAAM,GAAG,GAAa,IAAI,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,C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E,CAAC,CAAC;YACvD,CAAC,CAAC,GAAG,CAAC;QACd,GAAG,IAAI,MAAM,CAAC;QACd,YAAY,EAAE,CAAC;QACf,IAAI,GAAG,IAAI,MAAM;YAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,eAAe,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC;QAC1F,IAAI,GAAG,IAAI,MAAM;YAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,eAAe,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC;IAC9F,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC;AACxE,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Time-series change detection for the stats module (PRD 41 Part D / D13): CUSUM
|
|
3
|
+
* and Bayesian online change-point detection. Both are deterministic (fixed hazard,
|
|
4
|
+
* bounded run length); EWMA itself is reused from ml/classical, not copied here.
|
|
5
|
+
*/
|
|
6
|
+
export interface CusumResult {
|
|
7
|
+
/** First index where |cumulative sum| exceeds `threshold`, or -1. */
|
|
8
|
+
alarmIndex: number;
|
|
9
|
+
cumulative: number[];
|
|
10
|
+
}
|
|
11
|
+
/** Cumulative-sum detector on standardized deviations from `target`. */
|
|
12
|
+
export declare function cusum(values: readonly number[], target: number, options?: {
|
|
13
|
+
threshold?: number;
|
|
14
|
+
sigma?: number;
|
|
15
|
+
drift?: number;
|
|
16
|
+
}): CusumResult;
|
|
17
|
+
export interface ChangePointResult {
|
|
18
|
+
/** MAP run length at the last observation; -1 when the series is empty. */
|
|
19
|
+
currentRunLength: number;
|
|
20
|
+
/** Posterior probability of a change point at each index (run length 0 mass). */
|
|
21
|
+
changePointProbabilities: number[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Bayesian online change-point detection (Adams & MacKay, 2007), Gaussian with
|
|
25
|
+
* unknown mean and known variance 1, conjugate N(0, 1) prior on the mean per run.
|
|
26
|
+
* Each run length carries its own sufficient statistics (sum of observations), so a
|
|
27
|
+
* level shift accumulates evidence for a young run. Run-length mass is truncated at
|
|
28
|
+
* `maxRunLength` and renormalized; identical inputs give identical outputs.
|
|
29
|
+
*/
|
|
30
|
+
export declare function bayesianOnlineChangePoint(values: readonly number[], options?: {
|
|
31
|
+
hazard?: number;
|
|
32
|
+
maxRunLength?: number;
|
|
33
|
+
}): ChangePointResult;
|
|
34
|
+
//# sourceMappingURL=timeSeries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeSeries.d.ts","sourceRoot":"","sources":["../../src/stats/timeSeries.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,WAAW,WAAW;IACxB,qEAAqE;IACrE,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;CACxB;AAED,wEAAwE;AACxE,wBAAgB,KAAK,CACjB,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,MAAM,EAAE,MAAM,EACd,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAO,GACrE,WAAW,CAgBb;AAED,MAAM,WAAW,iBAAiB;IAC9B,2EAA2E;IAC3E,gBAAgB,EAAE,MAAM,CAAC;IACzB,iFAAiF;IACjF,wBAAwB,EAAE,MAAM,EAAE,CAAC;CACtC;AAED;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CACrC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,OAAO,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAO,GACzD,iBAAiB,CA2BnB"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Time-series change detection for the stats module (PRD 41 Part D / D13): CUSUM
|
|
3
|
+
* and Bayesian online change-point detection. Both are deterministic (fixed hazard,
|
|
4
|
+
* bounded run length); EWMA itself is reused from ml/classical, not copied here.
|
|
5
|
+
*/
|
|
6
|
+
/** Cumulative-sum detector on standardized deviations from `target`. */
|
|
7
|
+
export function cusum(values, target, options = {}) {
|
|
8
|
+
const sigma = options.sigma ?? 1;
|
|
9
|
+
const h = options.threshold ?? 5;
|
|
10
|
+
const k = options.drift ?? 0.5;
|
|
11
|
+
const cumulative = [];
|
|
12
|
+
let cPlus = 0;
|
|
13
|
+
let cMinus = 0;
|
|
14
|
+
let alarmIndex = -1;
|
|
15
|
+
for (let i = 0; i < values.length; i++) {
|
|
16
|
+
const z = (values[i] - target) / sigma;
|
|
17
|
+
cPlus = Math.max(0, cPlus + z - k);
|
|
18
|
+
cMinus = Math.min(0, cMinus + z + k);
|
|
19
|
+
cumulative.push(cPlus + cMinus);
|
|
20
|
+
if (alarmIndex === -1 && (cPlus > h || -cMinus > h))
|
|
21
|
+
alarmIndex = i;
|
|
22
|
+
}
|
|
23
|
+
return { alarmIndex, cumulative };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Bayesian online change-point detection (Adams & MacKay, 2007), Gaussian with
|
|
27
|
+
* unknown mean and known variance 1, conjugate N(0, 1) prior on the mean per run.
|
|
28
|
+
* Each run length carries its own sufficient statistics (sum of observations), so a
|
|
29
|
+
* level shift accumulates evidence for a young run. Run-length mass is truncated at
|
|
30
|
+
* `maxRunLength` and renormalized; identical inputs give identical outputs.
|
|
31
|
+
*/
|
|
32
|
+
export function bayesianOnlineChangePoint(values, options = {}) {
|
|
33
|
+
const hazard = options.hazard ?? 1 / 250;
|
|
34
|
+
const maxRunLength = options.maxRunLength ?? 500;
|
|
35
|
+
let growth = [1];
|
|
36
|
+
let sums = [0]; // sums[r] = Σ of observations inside a run of length r
|
|
37
|
+
const changePointProbabilities = [];
|
|
38
|
+
let currentRunLength = -1;
|
|
39
|
+
for (let t = 0; t < values.length; t++) {
|
|
40
|
+
const x = values[t];
|
|
41
|
+
const predProbs = growth.map((g, r) => g * gaussianPredictive(x, sums[r], r));
|
|
42
|
+
const evidence = predProbs.reduce((a, b) => a + b, 0) || 1e-300;
|
|
43
|
+
const next = new Array(maxRunLength).fill(0);
|
|
44
|
+
const nextSums = new Array(maxRunLength).fill(0);
|
|
45
|
+
next[0] = hazard;
|
|
46
|
+
nextSums[0] = 0;
|
|
47
|
+
for (let r = 0; r < growth.length && r + 1 < maxRunLength; r++) {
|
|
48
|
+
next[r + 1] = ((1 - hazard) * predProbs[r]) / evidence;
|
|
49
|
+
nextSums[r + 1] = sums[r] + x;
|
|
50
|
+
}
|
|
51
|
+
growth = next;
|
|
52
|
+
sums = nextSums;
|
|
53
|
+
const norm = growth.reduce((a, b) => a + b, 0) || 1e-300;
|
|
54
|
+
growth = growth.map((g) => g / norm);
|
|
55
|
+
changePointProbabilities.push(growth[0]);
|
|
56
|
+
currentRunLength = growth.indexOf(Math.max(...growth));
|
|
57
|
+
}
|
|
58
|
+
return { currentRunLength, changePointProbabilities };
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Predictive N(x; sum/(r+1), 1 + 1/(r+1)) for a run of length r: the posterior mean
|
|
62
|
+
* of the run's mean under the N(0, 1) prior is sum/(r+1), and the posterior variance
|
|
63
|
+
* adds 1 to the observation variance 1.
|
|
64
|
+
*/
|
|
65
|
+
function gaussianPredictive(x, sum, runLength) {
|
|
66
|
+
const mean = sum / (runLength + 1);
|
|
67
|
+
const variance = 1 + 1 / (runLength + 1);
|
|
68
|
+
const z = (x - mean) / Math.sqrt(variance);
|
|
69
|
+
return Math.exp(-0.5 * z * z) / Math.sqrt(2 * Math.PI * variance);
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=timeSeries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeSeries.js","sourceRoot":"","sources":["../../src/stats/timeSeries.ts"],"names":[],"mappings":"AAAA;;;;GA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
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advisory folds of the working transcript (PRD 45 §4.7 B6/B7) — pure post-processing
|
|
3
|
+
* over the messages {@link buildWorkingTranscript} returns, before a host sends them to
|
|
4
|
+
* the model. The folds are COMPRESSION ONLY: the run's own transcript (`state.transcript`)
|
|
5
|
+
* and its trace keep every message at full fidelity; only what the MODEL sees is folded.
|
|
6
|
+
*
|
|
7
|
+
* B6 — fold superseded reads: when an edit/write to path P succeeds at step N, every
|
|
8
|
+
* earlier `read_file` result for P in the model transcript becomes a one-line marker.
|
|
9
|
+
* The stale content leaves the model's window (the edit changed those bytes — anything
|
|
10
|
+
* the model still needs it re-reads), and stays in the trace at full fidelity.
|
|
11
|
+
*
|
|
12
|
+
* B7 — fold repeated narration: assistant text PRECEDING tool calls whose word-set
|
|
13
|
+
* overlaps the previous such text by ≥ {@link NARRATION_OVERLAP} becomes
|
|
14
|
+
* `"(progress note)"`. A final answer and a turn without tool calls are NEVER folded —
|
|
15
|
+
* those carry the run's actual output.
|
|
16
|
+
*
|
|
17
|
+
* A "step" is the index of the assistant message carrying the tool call, in the message
|
|
18
|
+
* list the fold runs over (the model transcript, system prompt included).
|
|
19
|
+
*
|
|
20
|
+
* Pure: no `node:*`, no I/O — runs wherever the `./working` subpath runs.
|
|
21
|
+
*/
|
|
22
|
+
import type { TranscriptMessage } from './transcript.js';
|
|
23
|
+
/** B7's fold threshold: |intersection| / |union| of the two texts' word sets (Jaccard). */
|
|
24
|
+
export declare const NARRATION_OVERLAP = 0.8;
|
|
25
|
+
/** B6's replacement for a read result whose file a later successful edit superseded. */
|
|
26
|
+
export declare function supersededReadMarker(path: string, step: number): string;
|
|
27
|
+
/** B7's replacement for a tool-call turn's narration that merely repeats the previous one. */
|
|
28
|
+
export declare const PROGRESS_NOTE = "(progress note)";
|
|
29
|
+
/**
|
|
30
|
+
* B6 — fold reads a later successful edit/write superseded. Returns the input unchanged
|
|
31
|
+
* (same reference, no copy) when nothing is superseded; otherwise a new list whose
|
|
32
|
+
* replaced messages are shallow copies with only `content` changed. The caller's
|
|
33
|
+
* transcript is never touched.
|
|
34
|
+
*/
|
|
35
|
+
export declare function foldSupersededReads<M extends TranscriptMessage>(msgs: readonly M[]): M[];
|
|
36
|
+
/**
|
|
37
|
+
* B7 — fold a tool-call turn's narration when it merely repeats the previous one.
|
|
38
|
+
* Only assistant messages WITH tool calls are candidates: a final answer and a turn
|
|
39
|
+
* without tool calls are never folded. The FIRST narration is kept verbatim (it has no
|
|
40
|
+
* predecessor to repeat); each later one folds when its word-set overlap with the most
|
|
41
|
+
* recent kept narration is ≥ {@link NARRATION_OVERLAP}. Once folded, it does not become
|
|
42
|
+
* the comparison text — the model keeps the last thing actually said.
|
|
43
|
+
*/
|
|
44
|
+
export declare function foldRepeatedNarration<M extends TranscriptMessage>(msgs: readonly M[], overlap?: number): M[];
|
|
45
|
+
/**
|
|
46
|
+
* Both folds, B6 then B7, over the working transcript a host is about to send to the
|
|
47
|
+
* model. Pure and advisory: `msgs` is not mutated, and the run's own transcript/trace
|
|
48
|
+
* keep every message at full fidelity.
|
|
49
|
+
*/
|
|
50
|
+
export declare function foldWorkingTranscript<M extends TranscriptMessage>(msgs: readonly M[]): M[];
|
|
51
|
+
//# sourceMappingURL=fold.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fold.d.ts","sourceRoot":"","sources":["../../src/working/fold.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEzD,2FAA2F;AAC3F,eAAO,MAAM,iBAAiB,MAAM,CAAC;AAErC,wFAAwF;AACxF,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAEvE;AAED,8FAA8F;AAC9F,eAAO,MAAM,aAAa,oBAAoB,CAAC;AAuH/C;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,iBAAiB,EAAE,IAAI,EAAE,SAAS,CAAC,EAAE,GAAG,CAAC,EAAE,CA0CxF;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,iBAAiB,EAAE,IAAI,EAAE,SAAS,CAAC,EAAE,EAAE,OAAO,SAAoB,GAAG,CAAC,EAAE,CAqBvH;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,iBAAiB,EAAE,IAAI,EAAE,SAAS,CAAC,EAAE,GAAG,CAAC,EAAE,CAE1F"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advisory folds of the working transcript (PRD 45 §4.7 B6/B7) — pure post-processing
|
|
3
|
+
* over the messages {@link buildWorkingTranscript} returns, before a host sends them to
|
|
4
|
+
* the model. The folds are COMPRESSION ONLY: the run's own transcript (`state.transcript`)
|
|
5
|
+
* and its trace keep every message at full fidelity; only what the MODEL sees is folded.
|
|
6
|
+
*
|
|
7
|
+
* B6 — fold superseded reads: when an edit/write to path P succeeds at step N, every
|
|
8
|
+
* earlier `read_file` result for P in the model transcript becomes a one-line marker.
|
|
9
|
+
* The stale content leaves the model's window (the edit changed those bytes — anything
|
|
10
|
+
* the model still needs it re-reads), and stays in the trace at full fidelity.
|
|
11
|
+
*
|
|
12
|
+
* B7 — fold repeated narration: assistant text PRECEDING tool calls whose word-set
|
|
13
|
+
* overlaps the previous such text by ≥ {@link NARRATION_OVERLAP} becomes
|
|
14
|
+
* `"(progress note)"`. A final answer and a turn without tool calls are NEVER folded —
|
|
15
|
+
* those carry the run's actual output.
|
|
16
|
+
*
|
|
17
|
+
* A "step" is the index of the assistant message carrying the tool call, in the message
|
|
18
|
+
* list the fold runs over (the model transcript, system prompt included).
|
|
19
|
+
*
|
|
20
|
+
* Pure: no `node:*`, no I/O — runs wherever the `./working` subpath runs.
|
|
21
|
+
*/
|
|
22
|
+
/** B7's fold threshold: |intersection| / |union| of the two texts' word sets (Jaccard). */
|
|
23
|
+
export const NARRATION_OVERLAP = 0.8;
|
|
24
|
+
/** B6's replacement for a read result whose file a later successful edit superseded. */
|
|
25
|
+
export function supersededReadMarker(path, step) {
|
|
26
|
+
return `(read of ${path} superseded by the edit at step ${step} — re-read if needed)`;
|
|
27
|
+
}
|
|
28
|
+
/** B7's replacement for a tool-call turn's narration that merely repeats the previous one. */
|
|
29
|
+
export const PROGRESS_NOTE = '(progress note)';
|
|
30
|
+
function callsOf(m) {
|
|
31
|
+
return Array.isArray(m.tool_calls) ? m.tool_calls : [];
|
|
32
|
+
}
|
|
33
|
+
function nameOf(call) {
|
|
34
|
+
const name = call.function?.name;
|
|
35
|
+
return typeof name === 'string' && name.length > 0 ? name : null;
|
|
36
|
+
}
|
|
37
|
+
/** The `path` argument of a tool call — from the object form or parsed out of a JSON string. */
|
|
38
|
+
function pathOf(call) {
|
|
39
|
+
const raw = call.function?.arguments;
|
|
40
|
+
let parsed = raw;
|
|
41
|
+
if (typeof raw === 'string') {
|
|
42
|
+
try {
|
|
43
|
+
parsed = JSON.parse(raw);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (!parsed || typeof parsed !== 'object')
|
|
50
|
+
return null;
|
|
51
|
+
const path = parsed.path;
|
|
52
|
+
return typeof path === 'string' && path.length > 0 ? path : null;
|
|
53
|
+
}
|
|
54
|
+
/** The tool result answering `call`: by `tool_call_id` when present, else positionally. */
|
|
55
|
+
function resultFor(convo, call, callIndex) {
|
|
56
|
+
const id = typeof call.id === 'string' && call.id.length > 0 ? call.id : null;
|
|
57
|
+
if (id) {
|
|
58
|
+
const byId = convo.find((m) => m.role === 'tool' && m.tool_call_id === id);
|
|
59
|
+
if (byId)
|
|
60
|
+
return byId;
|
|
61
|
+
}
|
|
62
|
+
// No ids (or an unmatched one): the nth tool result answers the nth call.
|
|
63
|
+
const results = convo.filter((m) => m.role === 'tool');
|
|
64
|
+
return results[callIndex];
|
|
65
|
+
}
|
|
66
|
+
/** Did the tool call SUCCEED? An explicit `{ok:false}` / `error` fails; anything else —
|
|
67
|
+
* including a textual error, which is still informative — passes. Absent result fails
|
|
68
|
+
* (a write nobody confirmed changed nothing). */
|
|
69
|
+
function succeeded(m) {
|
|
70
|
+
if (!m)
|
|
71
|
+
return false;
|
|
72
|
+
const content = m.content;
|
|
73
|
+
if (typeof content === 'string')
|
|
74
|
+
return content.trim().length > 0;
|
|
75
|
+
if (!content || typeof content !== 'object')
|
|
76
|
+
return false;
|
|
77
|
+
const r = content;
|
|
78
|
+
if (typeof r.ok === 'boolean')
|
|
79
|
+
return r.ok;
|
|
80
|
+
return r.error == null;
|
|
81
|
+
}
|
|
82
|
+
const EDIT_OR_WRITE = /^(edit|write)/i;
|
|
83
|
+
const READ = /^read/i;
|
|
84
|
+
function wordSet(text) {
|
|
85
|
+
return new Set(text
|
|
86
|
+
.toLowerCase()
|
|
87
|
+
.split(/[^a-z0-9_]+/)
|
|
88
|
+
.filter((w) => w.length > 0));
|
|
89
|
+
}
|
|
90
|
+
/** Jaccard similarity of two texts' word sets. */
|
|
91
|
+
function wordOverlap(a, b) {
|
|
92
|
+
const sa = wordSet(a);
|
|
93
|
+
const sb = wordSet(b);
|
|
94
|
+
if (sa.size === 0 || sb.size === 0)
|
|
95
|
+
return 0;
|
|
96
|
+
let intersection = 0;
|
|
97
|
+
for (const w of sa)
|
|
98
|
+
if (sb.has(w))
|
|
99
|
+
intersection += 1;
|
|
100
|
+
return intersection / (sa.size + sb.size - intersection);
|
|
101
|
+
}
|
|
102
|
+
/** Containment: is every word of the smaller set present in the larger? True when one
|
|
103
|
+
* narration is the other plus a trailing qualifier ("…once more", "…again", "…now") —
|
|
104
|
+
* a repeat Jaccard under-counts because the union grows with the qualifier's words. */
|
|
105
|
+
function wordContained(a, b) {
|
|
106
|
+
const sa = wordSet(a);
|
|
107
|
+
const sb = wordSet(b);
|
|
108
|
+
if (sa.size === 0 || sb.size === 0)
|
|
109
|
+
return false;
|
|
110
|
+
const [small, large] = sa.size <= sb.size ? [sa, sb] : [sb, sa];
|
|
111
|
+
for (const w of small)
|
|
112
|
+
if (!large.has(w))
|
|
113
|
+
return false;
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
/** The narration an assistant turn carries before its tool calls — null when it carries
|
|
117
|
+
* none (an empty-content tool-call turn, or any turn without tool calls). */
|
|
118
|
+
function narrationOf(m) {
|
|
119
|
+
const content = m.content;
|
|
120
|
+
const text = typeof content === 'string' ? content : content == null ? '' : JSON.stringify(content);
|
|
121
|
+
const t = text.trim();
|
|
122
|
+
return callsOf(m).length > 0 && t.length > 0 ? t : null;
|
|
123
|
+
}
|
|
124
|
+
/** Tracks which read CALLS were superseded, so `foldWorkingTranscript`'s B7 pass can
|
|
125
|
+
* skip the narration that introduced a read whose content B6 just replaced. */
|
|
126
|
+
const SUPERSEDED_CALL = new WeakSet();
|
|
127
|
+
/**
|
|
128
|
+
* B6 — fold reads a later successful edit/write superseded. Returns the input unchanged
|
|
129
|
+
* (same reference, no copy) when nothing is superseded; otherwise a new list whose
|
|
130
|
+
* replaced messages are shallow copies with only `content` changed. The caller's
|
|
131
|
+
* transcript is never touched.
|
|
132
|
+
*/
|
|
133
|
+
export function foldSupersededReads(msgs) {
|
|
134
|
+
const edits = [];
|
|
135
|
+
const reads = [];
|
|
136
|
+
let callIndex = 0;
|
|
137
|
+
for (let i = 0; i < msgs.length; i++) {
|
|
138
|
+
const m = msgs[i];
|
|
139
|
+
for (const call of callsOf(m)) {
|
|
140
|
+
const name = nameOf(call);
|
|
141
|
+
if (!name)
|
|
142
|
+
continue;
|
|
143
|
+
const path = pathOf(call);
|
|
144
|
+
const result = resultFor(msgs, call, callIndex);
|
|
145
|
+
if (path && EDIT_OR_WRITE.test(name)) {
|
|
146
|
+
if (succeeded(result))
|
|
147
|
+
edits.push({ path, step: i });
|
|
148
|
+
}
|
|
149
|
+
else if (path && READ.test(name) && result) {
|
|
150
|
+
reads.push({ step: i, path, result, call });
|
|
151
|
+
}
|
|
152
|
+
callIndex += 1;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (edits.length === 0 || reads.length === 0)
|
|
156
|
+
return msgs;
|
|
157
|
+
const superseded = new Map();
|
|
158
|
+
for (const read of reads) {
|
|
159
|
+
// The LATEST successful edit of the same path after the read wins — an earlier read
|
|
160
|
+
// re-made after an edit is not superseded by that edit.
|
|
161
|
+
let step = -1;
|
|
162
|
+
for (const e of edits) {
|
|
163
|
+
if (e.path === read.path && e.step > read.step && e.step > step)
|
|
164
|
+
step = e.step;
|
|
165
|
+
}
|
|
166
|
+
if (step >= 0) {
|
|
167
|
+
superseded.set(read.result, supersededReadMarker(read.path, step));
|
|
168
|
+
SUPERSEDED_CALL.add(read.call);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (superseded.size === 0)
|
|
172
|
+
return msgs;
|
|
173
|
+
return msgs.map((m) => {
|
|
174
|
+
const marker = superseded.get(m);
|
|
175
|
+
return marker == null ? m : { ...m, content: marker };
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* B7 — fold a tool-call turn's narration when it merely repeats the previous one.
|
|
180
|
+
* Only assistant messages WITH tool calls are candidates: a final answer and a turn
|
|
181
|
+
* without tool calls are never folded. The FIRST narration is kept verbatim (it has no
|
|
182
|
+
* predecessor to repeat); each later one folds when its word-set overlap with the most
|
|
183
|
+
* recent kept narration is ≥ {@link NARRATION_OVERLAP}. Once folded, it does not become
|
|
184
|
+
* the comparison text — the model keeps the last thing actually said.
|
|
185
|
+
*/
|
|
186
|
+
export function foldRepeatedNarration(msgs, overlap = NARRATION_OVERLAP) {
|
|
187
|
+
let lastKept = null;
|
|
188
|
+
let changed = false;
|
|
189
|
+
const out = msgs.map((m) => {
|
|
190
|
+
if (m.role !== 'assistant')
|
|
191
|
+
return m;
|
|
192
|
+
const narration = narrationOf(m);
|
|
193
|
+
if (narration == null)
|
|
194
|
+
return m; // no tool calls, or no narration: never folded
|
|
195
|
+
// A narration that introduced a read B6 folded is the model's only record of why it
|
|
196
|
+
// read — it never folds itself, but it IS what a later repeat is compared against.
|
|
197
|
+
if (callsOf(m).some((call) => SUPERSEDED_CALL.has(call))) {
|
|
198
|
+
lastKept = narration;
|
|
199
|
+
return m;
|
|
200
|
+
}
|
|
201
|
+
if (lastKept != null && (wordOverlap(narration, lastKept) >= overlap || wordContained(narration, lastKept))) {
|
|
202
|
+
changed = true;
|
|
203
|
+
return { ...m, content: PROGRESS_NOTE };
|
|
204
|
+
}
|
|
205
|
+
lastKept = narration;
|
|
206
|
+
return m;
|
|
207
|
+
});
|
|
208
|
+
return changed ? out : msgs;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Both folds, B6 then B7, over the working transcript a host is about to send to the
|
|
212
|
+
* model. Pure and advisory: `msgs` is not mutated, and the run's own transcript/trace
|
|
213
|
+
* keep every message at full fidelity.
|
|
214
|
+
*/
|
|
215
|
+
export function foldWorkingTranscript(msgs) {
|
|
216
|
+
return foldRepeatedNarration(foldSupersededReads(msgs));
|
|
217
|
+
}
|
|
218
|
+
//# sourceMappingURL=fold.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fold.js","sourceRoot":"","sources":["../../src/working/fold.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAIH,2FAA2F;AAC3F,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAErC,wFAAwF;AACxF,MAAM,UAAU,oBAAoB,CAAC,IAAY,EAAE,IAAY;IAC7D,OAAO,YAAY,IAAI,mCAAmC,IAAI,uBAAuB,CAAC;AACxF,CAAC;AAED,8FAA8F;AAC9F,MAAM,CAAC,MAAM,aAAa,GAAG,iBAAiB,CAAC;AAY/C,SAAS,OAAO,CAAC,CAAoB;IACnC,OAAO,KAAK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CAAC,CAAE,IAAY,CAAC;AACvC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAA8B,IAAkB;IACnF,OAAO,qBAAqB,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAC;AAC1D,CAAC"}
|
package/dist/working/index.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export { rankByCount } from './rank.js';
|
|
|
29
29
|
export type { Observation, WorkingMemorySnapshot, ConsolidatedMemory, ConsolidateOptions } from './types.js';
|
|
30
30
|
export { HISTORY_WINDOW, HISTORY_TOKEN_BUDGET, COMPACT_TAIL_TOKEN_BUDGET, windowed, stillInWorkingContext, transcriptTokens, buildWorkingTranscript, } from './transcript.js';
|
|
31
31
|
export type { TranscriptMessage, CompactMemo, WorkingTranscriptState, TranscriptFold } from './transcript.js';
|
|
32
|
+
export { NARRATION_OVERLAP, PROGRESS_NOTE, supersededReadMarker, foldSupersededReads, foldRepeatedNarration, foldWorkingTranscript } from './fold.js';
|
|
32
33
|
export { TRANSCRIPT_SUMMARY_PROMPT, TRANSCRIPT_SUMMARY_MAX_TOKENS, TRANSCRIPT_SUMMARY_INPUT_TOKENS, renderForSummary, summaryChunks, summarizeTranscript } from './summary.js';
|
|
33
34
|
export type { SummaryCompletion } from './summary.js';
|
|
34
35
|
export { CONSOLIDATION_META, CONSOLIDATION_MARKER_PREFIX, consolidationMetadata, consolidationMarkerOf, consolidationMarkerContent, isConsolidationMarker, lastConsolidationIndex, scopeToConsolidation, foldKeepFromId, } from './consolidationMarker.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/working/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EACL,WAAW,EACX,iBAAiB,EACjB,eAAe,EACf,iBAAiB,EACjB,wBAAwB,EACxB,sBAAsB,GACvB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uB,EACvB,sBAAsB,EACtB,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,cAAc,EACd,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,YAAY,EAAE,WAAW,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAC7G,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,yBAAyB,EACzB,QAAQ,EACR,qBAAqB,EACrB,gBAAgB,EAChB,sBAAsB,GACvB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAE,iBAAiB,EAAE,WAAW,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAC9G,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AACtJ,OAAO,EAAE,yBAAyB,EAAE,6BAA6B,EAAE,+BAA+B,EAAE,gBAAgB,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAC/K,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EACL,kBAAkB,EAClB,2BAA2B,EAC3B,qBAAqB,EACrB,qBAAqB,EACrB,0BAA0B,EAC1B,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,EACpB,cAAc,GACf,MAAM,0BAA0B,CAAC;AAClC,YAAY,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC"}
|
package/dist/working/index.js
CHANGED
|
@@ -27,6 +27,7 @@ export { consolidate, mergeConsolidated, consolidatedKey, consolidatedFiles, wit
|
|
|
27
27
|
export { CONSOLIDATED_KEY_PREFIX, CONSOLIDATED_ROOT_AREA, MAX_RECALLED_MAPS, areaOfPath, areasNamedIn, areasOfPathsIn, consolidatedArea, consolidatedFactsFor, } from './codemapAreas.js';
|
|
28
28
|
export { rankByCount } from './rank.js';
|
|
29
29
|
export { HISTORY_WINDOW, HISTORY_TOKEN_BUDGET, COMPACT_TAIL_TOKEN_BUDGET, windowed, stillInWorkingContext, transcriptTokens, buildWorkingTranscript, } from './transcript.js';
|
|
30
|
+
export { NARRATION_OVERLAP, PROGRESS_NOTE, supersededReadMarker, foldSupersededReads, foldRepeatedNarration, foldWorkingTranscript } from './fold.js';
|
|
30
31
|
export { TRANSCRIPT_SUMMARY_PROMPT, TRANSCRIPT_SUMMARY_MAX_TOKENS, TRANSCRIPT_SUMMARY_INPUT_TOKENS, renderForSummary, summaryChunks, summarizeTranscript } from './summary.js';
|
|
31
32
|
export { CONSOLIDATION_META, CONSOLIDATION_MARKER_PREFIX, consolidationMetadata, consolidationMarkerOf, consolidationMarkerContent, isConsolidationMarker, lastConsolidationIndex, scopeToConsolidation, foldKeepFromId, } from './consolidationMarker.js';
|
|
32
33
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/working/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EACL,WAAW,EACX,iBAAiB,EACjB,eAAe,EACf,iBAAiB,EACjB,wBAAwB,EACxB,sBAAsB,GACvB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACtB,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,cAAc,EACd,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,yBAAyB,EACzB,QAAQ,EACR,qBAAqB,EACrB,gBAAgB,EAChB,sBAAsB,GACvB,MAAM,iBAAiB,CAA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B,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,EACpB,cAAc,GACf,MAAM,0BAA0B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seanhogg/builderforce-memory",
|
|
3
|
-
"version": "2026.10.
|
|
3
|
+
"version": "2026.10.63",
|
|
4
4
|
"description": "BuilderForce Agent Memory — runtime layer. SSM execution, Transformer orchestration, online distillation, enterprise RAG (ingestion pipelines, vector-store adapters, tenant/ACL-scoped hybrid retrieval), multi-agent graph orchestration, LLM telemetry and cost accounting, persistent agent memory, and token-saving agent-run memory (code maps, a prompt-cache-safe working memory, chunked transcript windowing) for BuilderForce.ai agents.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -34,6 +34,10 @@
|
|
|
34
34
|
"types": "./dist/working/index.d.ts",
|
|
35
35
|
"import": "./dist/working/index.js"
|
|
36
36
|
},
|
|
37
|
+
"./stats": {
|
|
38
|
+
"types": "./dist/stats/index.d.ts",
|
|
39
|
+
"import": "./dist/stats/index.js"
|
|
40
|
+
},
|
|
37
41
|
"./distillation": {
|
|
38
42
|
"types": "./dist/distillation/index.d.ts",
|
|
39
43
|
"import": "./dist/distillation/index.js"
|