@seanhogg/builderforce-memory 2026.10.75 → 2026.10.76
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/prng.d.ts +5 -1
- package/dist/stats/prng.d.ts.map +1 -1
- package/dist/stats/prng.js +11 -3
- package/dist/stats/prng.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/regression.prng.test.ts +126 -0
- package/src/stats/prng.ts +11 -3
package/dist/stats/prng.d.ts
CHANGED
|
@@ -4,7 +4,11 @@
|
|
|
4
4
|
* same inputs give byte-identical outputs on any machine. Algorithm: xoshiro128**.
|
|
5
5
|
*/
|
|
6
6
|
export type Rng = () => number;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* A deterministic RNG in [0, 1), seeded once. `seed = 0` is a valid seed. State (a, b, c, d) = (s0, s1, s2, s3)
|
|
9
|
+
* of the reference xoshiro128** (Blackman & Vigna): result = rotl(s1·5, 7)·9; t = s1 << 9 (a SHIFT, not a
|
|
10
|
+
* rotate — rotating collapsed every stream to 0 within a few hundred draws).
|
|
11
|
+
*/
|
|
8
12
|
export declare function createRng(seed: number): Rng;
|
|
9
13
|
/** Box–Muller standard-normal draw from a uniform RNG (deterministic under seed). */
|
|
10
14
|
export declare function normalDraw(rng: Rng): number;
|
package/dist/stats/prng.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prng.d.ts","sourceRoot":"","sources":["../../src/stats/prng.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"prng.d.ts","sourceRoot":"","sources":["../../src/stats/prng.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC;AAoB/B;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAc3C;AAED,qFAAqF;AACrF,wBAAgB,UAAU,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAI3C"}
|
package/dist/stats/prng.js
CHANGED
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
* stochastic routine here draws from THIS generator, seeded by the caller, so the
|
|
4
4
|
* same inputs give byte-identical outputs on any machine. Algorithm: xoshiro128**.
|
|
5
5
|
*/
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* SplitMix32-style seed expansion to four uint32 words. Never the all-zero state (the one fixed point of
|
|
8
|
+
* xoshiro): the finaliser is a bijection on uint32 (xor-shifts and odd multipliers), and its four inputs
|
|
9
|
+
* s + k·0x9e3779b9 (k = 1..4) are distinct mod 2^32, so at most one of the four words can be 0.
|
|
10
|
+
*/
|
|
7
11
|
function seedWords(seed) {
|
|
8
12
|
let s = seed >>> 0;
|
|
9
13
|
const next = () => {
|
|
@@ -16,13 +20,17 @@ function seedWords(seed) {
|
|
|
16
20
|
};
|
|
17
21
|
return [next(), next(), next(), next()];
|
|
18
22
|
}
|
|
19
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* A deterministic RNG in [0, 1), seeded once. `seed = 0` is a valid seed. State (a, b, c, d) = (s0, s1, s2, s3)
|
|
25
|
+
* of the reference xoshiro128** (Blackman & Vigna): result = rotl(s1·5, 7)·9; t = s1 << 9 (a SHIFT, not a
|
|
26
|
+
* rotate — rotating collapsed every stream to 0 within a few hundred draws).
|
|
27
|
+
*/
|
|
20
28
|
export function createRng(seed) {
|
|
21
29
|
let [a, b, c, d] = seedWords(seed);
|
|
22
30
|
return () => {
|
|
23
31
|
const result = (Math.imul(b, 5) << 7 | Math.imul(b, 5) >>> 25) >>> 0;
|
|
24
32
|
const out = ((result * 9) >>> 0) / 4294967296;
|
|
25
|
-
const t = (b << 9
|
|
33
|
+
const t = (b << 9) >>> 0;
|
|
26
34
|
c = (c ^ a) >>> 0;
|
|
27
35
|
d = (d ^ b) >>> 0;
|
|
28
36
|
b = (b ^ c) >>> 0;
|
package/dist/stats/prng.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prng.js","sourceRoot":"","sources":["../../src/stats/prng.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH
|
|
1
|
+
{"version":3,"file":"prng.js","sourceRoot":"","sources":["../../src/stats/prng.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;;;GAIG;AACH,SAAS,SAAS,CAAC,IAAY;IAC3B,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC;IACnB,MAAM,IAAI,GAAG,GAAG,EAAE;QACd,CAAC,GAAG,CAAC,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;QAC3B,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;QAChD,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;QAChD,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;QAC3B,OAAO,CAAC,CAAC;IACb,CAAC,CAAC;IACF,OAAO,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;AAC5C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,IAAY;IAClC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IACnC,OAAO,GAAG,EAAE;QACR,MAAM,MAAM,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CA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-memory",
|
|
3
|
-
"version": "2026.10.
|
|
3
|
+
"version": "2026.10.76",
|
|
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",
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/contract/regression.prng.test.ts — the stats module's xoshiro128** (stats/prng.ts `createRng`, shipped wrong in
|
|
3
|
+
* 2026.10.72) computed `t = rotl(s1, 9)` where the algorithm shifts (`t = s1 << 9`). The state degenerated: every
|
|
4
|
+
* stream collapsed to 0 within ~76–200 draws, so the paired bootstrap (2,000 × n draws from one stream) returned
|
|
5
|
+
* collapsed CIs and Thompson draws were biased over long streams.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, it } from '@jest/globals';
|
|
9
|
+
import { createRng, normalDraw } from '../stats/prng.js';
|
|
10
|
+
import { pairedBootstrapCi } from '../stats/index.js';
|
|
11
|
+
|
|
12
|
+
const MASK = 0xffffffffn;
|
|
13
|
+
|
|
14
|
+
/** The seed expansion createRng documents (SplitMix32-style), in BigInt so it shares no code path with prng.ts. */
|
|
15
|
+
function referenceSeedWords(seed: number): bigint[] {
|
|
16
|
+
let s = BigInt(seed >>> 0);
|
|
17
|
+
const words: bigint[] = [];
|
|
18
|
+
for (let k = 0; k < 4; k++) {
|
|
19
|
+
s = (s + 0x9e3779b9n) & MASK;
|
|
20
|
+
let z = s;
|
|
21
|
+
z = ((z ^ (z >> 16n)) * 0x21f0aaadn) & MASK;
|
|
22
|
+
z = ((z ^ (z >> 15n)) * 0x735a2d97n) & MASK;
|
|
23
|
+
z = (z ^ (z >> 15n)) & MASK;
|
|
24
|
+
words.push(z);
|
|
25
|
+
}
|
|
26
|
+
return words;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** xoshiro128** transcribed from Blackman & Vigna's reference C (xoshiro128starstar.c), uint32 arithmetic via BigInt. */
|
|
30
|
+
function referenceXoshiro128ss(state: readonly bigint[]): () => bigint {
|
|
31
|
+
const s = [...state];
|
|
32
|
+
const rotl = (x: bigint, k: bigint) => ((x << k) | (x >> (32n - k))) & MASK;
|
|
33
|
+
return () => {
|
|
34
|
+
const result = (rotl((s[1]! * 5n) & MASK, 7n) * 9n) & MASK;
|
|
35
|
+
const t = (s[1]! << 9n) & MASK;
|
|
36
|
+
s[2] = s[2]! ^ s[0]!;
|
|
37
|
+
s[3] = s[3]! ^ s[1]!;
|
|
38
|
+
s[1] = s[1]! ^ s[2]!;
|
|
39
|
+
s[0] = s[0]! ^ s[3]!;
|
|
40
|
+
s[2] = s[2]! ^ t;
|
|
41
|
+
s[3] = rotl(s[3]!, 11n);
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe('regression: createRng is the reference xoshiro128**', () => {
|
|
47
|
+
it('regression: matches an inline reference implementation over 10,000 draws for 50 seeds', () => {
|
|
48
|
+
for (let seed = 0; seed < 50; seed++) {
|
|
49
|
+
const rng = createRng(seed);
|
|
50
|
+
const ref = referenceXoshiro128ss(referenceSeedWords(seed));
|
|
51
|
+
for (let i = 0; i < 10_000; i++) {
|
|
52
|
+
const expected = Number(ref()) / 4294967296;
|
|
53
|
+
const got = rng();
|
|
54
|
+
if (got !== expected) expect({ seed, i, got }).toEqual({ seed, i, got: expected });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('regression: known answer for the fixed state (1, 2, 3, 4)', () => {
|
|
60
|
+
// Hand-derived from the reference C. Step 1: s1 = 2 → rotl(10, 7)·9 = 1280·9 = 11520; t = 1024;
|
|
61
|
+
// s = (1^6, 2^2, (3^1)^1024, rotl(4^2, 11)) = (7, 0, 1026, 12288).
|
|
62
|
+
const ref = referenceXoshiro128ss([1n, 2n, 3n, 4n]);
|
|
63
|
+
expect(ref()).toBe(11520n);
|
|
64
|
+
// Step 2: s1 = 0 → result 0; t = 0; s = (7^12288, 0^1029, 1029, rotl(12288, 11)) = (12295, 1029, 1029, 25165824).
|
|
65
|
+
expect(ref()).toBe(0n);
|
|
66
|
+
// Step 3: s1 = 1029 → rotl(5145, 7)·9 = 658560·9 = 5927040.
|
|
67
|
+
expect(ref()).toBe(5927040n);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('regression: seedWords never yields the all-zero state', () => {
|
|
71
|
+
for (const seed of [0, 1, -1, 0x61c88647, 0xffffffff, 2 ** 31, -(2 ** 31)]) {
|
|
72
|
+
expect({ seed, zero: referenceSeedWords(seed).every((w) => w === 0n) }).toEqual({ seed, zero: false });
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('regression: no stream collapses to 0', () => {
|
|
78
|
+
it('regression: 200 seeds × 2,000 draws (the rotate collapsed within ~200) — no run of ≥ 8 exact zeros, zero fraction < 1e-6', () => {
|
|
79
|
+
let zeros = 0;
|
|
80
|
+
let total = 0;
|
|
81
|
+
let longestRun = 0;
|
|
82
|
+
for (let seed = 0; seed < 200; seed++) {
|
|
83
|
+
const rng = createRng(seed);
|
|
84
|
+
let run = 0;
|
|
85
|
+
for (let i = 0; i < 2_000; i++) {
|
|
86
|
+
total++;
|
|
87
|
+
if (rng() === 0) {
|
|
88
|
+
zeros++;
|
|
89
|
+
run++;
|
|
90
|
+
if (run > longestRun) longestRun = run;
|
|
91
|
+
} else run = 0;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
expect(longestRun).toBeLessThan(8);
|
|
95
|
+
expect(zeros / total).toBeLessThan(1e-6);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('regression: 1M draws from one seed have mean ≈ 1/2 and variance ≈ 1/12', () => {
|
|
99
|
+
const rng = createRng(12345);
|
|
100
|
+
const N = 1_000_000;
|
|
101
|
+
let sum = 0;
|
|
102
|
+
let sumSq = 0;
|
|
103
|
+
for (let i = 0; i < N; i++) {
|
|
104
|
+
const u = rng();
|
|
105
|
+
sum += u;
|
|
106
|
+
sumSq += u * u;
|
|
107
|
+
}
|
|
108
|
+
const mean = sum / N;
|
|
109
|
+
const variance = sumSq / N - mean * mean;
|
|
110
|
+
expect(Math.abs(mean - 0.5)).toBeLessThan(0.001);
|
|
111
|
+
expect(Math.abs(variance - 1 / 12)).toBeLessThan(0.001);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('regression: the paired bootstrap CI does not collapse', () => {
|
|
116
|
+
it('regression: a noisy no-shift series (seeded normal noise, n = 30) gives a CI that contains 0', () => {
|
|
117
|
+
const rng = createRng(2026);
|
|
118
|
+
const before = Array.from({ length: 30 }, () => 10 + normalDraw(rng));
|
|
119
|
+
const after = Array.from({ length: 30 }, () => 10 + normalDraw(rng));
|
|
120
|
+
const ci = pairedBootstrapCi(before, after, { seed: 7 });
|
|
121
|
+
expect(ci.low).toBeLessThan(0);
|
|
122
|
+
expect(ci.high).toBeGreaterThan(0);
|
|
123
|
+
// diff sd ≈ √2, se ≈ 0.26: a 95% interval is ≈ 1 wide, never a collapsed point.
|
|
124
|
+
expect(ci.high - ci.low).toBeGreaterThan(0.5);
|
|
125
|
+
});
|
|
126
|
+
});
|
package/src/stats/prng.ts
CHANGED
|
@@ -6,7 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
export type Rng = () => number;
|
|
8
8
|
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* SplitMix32-style seed expansion to four uint32 words. Never the all-zero state (the one fixed point of
|
|
11
|
+
* xoshiro): the finaliser is a bijection on uint32 (xor-shifts and odd multipliers), and its four inputs
|
|
12
|
+
* s + k·0x9e3779b9 (k = 1..4) are distinct mod 2^32, so at most one of the four words can be 0.
|
|
13
|
+
*/
|
|
10
14
|
function seedWords(seed: number): [number, number, number, number] {
|
|
11
15
|
let s = seed >>> 0;
|
|
12
16
|
const next = () => {
|
|
@@ -20,13 +24,17 @@ function seedWords(seed: number): [number, number, number, number] {
|
|
|
20
24
|
return [next(), next(), next(), next()];
|
|
21
25
|
}
|
|
22
26
|
|
|
23
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* A deterministic RNG in [0, 1), seeded once. `seed = 0` is a valid seed. State (a, b, c, d) = (s0, s1, s2, s3)
|
|
29
|
+
* of the reference xoshiro128** (Blackman & Vigna): result = rotl(s1·5, 7)·9; t = s1 << 9 (a SHIFT, not a
|
|
30
|
+
* rotate — rotating collapsed every stream to 0 within a few hundred draws).
|
|
31
|
+
*/
|
|
24
32
|
export function createRng(seed: number): Rng {
|
|
25
33
|
let [a, b, c, d] = seedWords(seed);
|
|
26
34
|
return () => {
|
|
27
35
|
const result = (Math.imul(b, 5) << 7 | Math.imul(b, 5) >>> 25) >>> 0;
|
|
28
36
|
const out = ((result * 9) >>> 0) / 4294967296;
|
|
29
|
-
const t = (b << 9
|
|
37
|
+
const t = (b << 9) >>> 0;
|
|
30
38
|
c = (c ^ a) >>> 0;
|
|
31
39
|
d = (d ^ b) >>> 0;
|
|
32
40
|
b = (b ^ c) >>> 0;
|