@metaharness/flywheel 0.1.7 → 0.1.8

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/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatAnalysis = exports.analyzeBundle = exports.verifyReplayBundle = exports.liftPoint = exports.computeLiftCurve = exports.InMemoryLineageStore = exports.canon = exports.verifyReceipt = exports.makeSigner = exports.gateFingerprint = exports.meetsPromotionRule = exports.runFlywheelGenerations = void 0;
3
+ exports.formatAnalysis = exports.analyzeBundle = exports.verifyReplayBundle = exports.liftPoint = exports.computeLiftCurve = exports.InMemoryLineageStore = exports.canon = exports.verifyReceipt = exports.makeSigner = exports.withSequentialEvidence = exports.sequentialEvidence = exports.gateFingerprint = exports.meetsPromotionRule = exports.runFlywheelGenerations = void 0;
4
4
  // @metaharness/flywheel — a verifiable self-improvement loop for agent harnesses.
5
5
  // Freeze the model. Evolve the harness. Promote only what proves lift.
6
6
  //
@@ -12,6 +12,9 @@ Object.defineProperty(exports, "runFlywheelGenerations", { enumerable: true, get
12
12
  var gate_js_1 = require("./gate.js");
13
13
  Object.defineProperty(exports, "meetsPromotionRule", { enumerable: true, get: function () { return gate_js_1.meetsPromotionRule; } });
14
14
  Object.defineProperty(exports, "gateFingerprint", { enumerable: true, get: function () { return gate_js_1.gateFingerprint; } });
15
+ var sequential_js_1 = require("./sequential.js");
16
+ Object.defineProperty(exports, "sequentialEvidence", { enumerable: true, get: function () { return sequential_js_1.sequentialEvidence; } });
17
+ Object.defineProperty(exports, "withSequentialEvidence", { enumerable: true, get: function () { return sequential_js_1.withSequentialEvidence; } });
15
18
  var receipts_js_1 = require("./receipts.js");
16
19
  Object.defineProperty(exports, "makeSigner", { enumerable: true, get: function () { return receipts_js_1.makeSigner; } });
17
20
  Object.defineProperty(exports, "verifyReceipt", { enumerable: true, get: function () { return receipts_js_1.verifyReceipt; } });
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,kFAAkF;AAClF,uEAAuE;AACvE,EAAE;AACF,uGAAuG;AACvG,yGAAyG;AACzG,2GAA2G;AAC3G,mCAAkD;AAAzC,gHAAA,sBAAsB,OAAA;AAG/B,qCAAgE;AAAvD,6GAAA,kBAAkB,OAAA;AAAE,0GAAA,eAAe,OAAA;AAC5C,6CAAiE;AAAxD,yGAAA,UAAU,OAAA;AAAE,4GAAA,aAAa,OAAA;AAAE,oGAAA,KAAK,OAAA;AACzC,2CAAiF;AAAxE,kHAAA,oBAAoB,OAAA;AAAE,8GAAA,gBAAgB,OAAA;AAAE,uGAAA,SAAS,OAAA;AAC1D,yCAAiD;AAAxC,+GAAA,kBAAkB,OAAA;AAE3B,2CAA6D;AAApD,2GAAA,aAAa,OAAA;AAAE,4GAAA,cAAc,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,kFAAkF;AAClF,uEAAuE;AACvE,EAAE;AACF,uGAAuG;AACvG,yGAAyG;AACzG,2GAA2G;AAC3G,mCAAkD;AAAzC,gHAAA,sBAAsB,OAAA;AAG/B,qCAAgE;AAAvD,6GAAA,kBAAkB,OAAA;AAAE,0GAAA,eAAe,OAAA;AAC5C,iDAA6E;AAApE,mHAAA,kBAAkB,OAAA;AAAE,uHAAA,sBAAsB,OAAA;AAEnD,6CAAiE;AAAxD,yGAAA,UAAU,OAAA;AAAE,4GAAA,aAAa,OAAA;AAAE,oGAAA,KAAK,OAAA;AACzC,2CAAiF;AAAxE,kHAAA,oBAAoB,OAAA;AAAE,8GAAA,gBAAgB,OAAA;AAAE,uGAAA,SAAS,OAAA;AAC1D,yCAAiD;AAAxC,+GAAA,kBAAkB,OAAA;AAE3B,2CAA6D;AAApD,2GAAA,aAAa,OAAA;AAAE,4GAAA,cAAc,OAAA"}
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ // @metaharness/flywheel — anytime-valid sequential testing for promotion gates.
3
+ //
4
+ // WHY THIS EXISTS
5
+ //
6
+ // `meetsPromotionRule` is a single-shot conjunctive comparison, and it is the
7
+ // right shape: frozen, fingerprintable, every clause load-bearing. But a
8
+ // flywheel run evaluates MANY generations against the SAME holdout, and that is
9
+ // uncontrolled multiple testing. Published measurements of greedy
10
+ // "accept-if-the-score-improved" acceptance put the false-commit rate at
11
+ // 30-42%, and found 13-21 spurious modifications made even when NO true gains
12
+ // existed — degrading one agent by 4.9 points while every individual decision
13
+ // looked locally justified.
14
+ //
15
+ // The conjunctive gate plus a frozen anchor already mitigates this with
16
+ // multiple hurdles, which is real. It is not, however, anytime-valid: nothing
17
+ // in it accounts for how many times you have looked.
18
+ //
19
+ // This module adds that missing property WITHOUT touching the default gate,
20
+ // whose stability is itself the product. It composes: wrap any PromotionRule,
21
+ // and a candidate must clear both the frozen clauses AND accumulated evidence.
22
+ //
23
+ // METHOD
24
+ //
25
+ // Testing-by-betting / e-processes. An e-value is a non-negative random
26
+ // variable with expectation <= 1 under the null hypothesis ("this candidate is
27
+ // no better than baseline"). By Ville's inequality, P(sup_t E_t >= 1/alpha)
28
+ // <= alpha, so you may stop and reject at ANY time — no pre-registered sample
29
+ // size, no alpha spending schedule, and no penalty for peeking. That is exactly
30
+ // the property a flywheel needs, because it peeks by construction.
31
+ //
32
+ // The bet here is deliberately simple and assumption-light: per paired item,
33
+ // a candidate win against a baseline loss multiplies the e-value up, the
34
+ // reverse multiplies it down, and ties leave it unchanged.
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.sequentialEvidence = sequentialEvidence;
37
+ exports.withSequentialEvidence = withSequentialEvidence;
38
+ const DEFAULT_ALPHA = 0.05;
39
+ const DEFAULT_LAMBDA = 0.5;
40
+ /**
41
+ * Accumulate paired outcomes into an anytime-valid e-value.
42
+ *
43
+ * Only discordant pairs move the e-value: if both arms win or both lose, that
44
+ * item tells you nothing about which is better (this is the McNemar insight,
45
+ * carried over to the sequential setting).
46
+ */
47
+ function sequentialEvidence(outcomes, config = {}) {
48
+ const alpha = config.alpha ?? DEFAULT_ALPHA;
49
+ const lambda = config.lambda ?? DEFAULT_LAMBDA;
50
+ if (!(alpha > 0 && alpha < 1))
51
+ throw new RangeError('alpha must be in (0, 1)');
52
+ if (!(lambda > 0 && lambda < 1))
53
+ throw new RangeError('lambda must be in (0, 1)');
54
+ const threshold = 1 / alpha;
55
+ let eValue = 1;
56
+ let informativePairs = 0;
57
+ for (const o of outcomes) {
58
+ if (o.candidateWon === o.baselineWon)
59
+ continue; // concordant: no information
60
+ informativePairs++;
61
+ // Under the null, a discordant pair favors either arm with probability 1/2,
62
+ // so E[multiplier] = 1 and the process is a non-negative martingale.
63
+ eValue *= o.candidateWon ? 1 + lambda : 1 - lambda;
64
+ }
65
+ return {
66
+ significant: eValue >= threshold,
67
+ eValue,
68
+ threshold,
69
+ informativePairs,
70
+ totalPairs: outcomes.length,
71
+ };
72
+ }
73
+ /**
74
+ * Compose a frozen gate with a sequential-evidence requirement.
75
+ *
76
+ * The returned rule is still a plain `PromotionRule`, so it fingerprints and
77
+ * freezes exactly like the default one. A candidate must satisfy BOTH: every
78
+ * clause of `baseRule`, and evidence strong enough to survive having been
79
+ * looked at repeatedly.
80
+ *
81
+ * Paired outcomes are read from `evidence.pairedOutcomes` when present. When
82
+ * absent the rule degrades to `baseRule` alone rather than silently blocking
83
+ * every promotion — a caller that has not wired up per-item outcomes yet should
84
+ * get the old behavior, not a permanently closed gate.
85
+ */
86
+ function withSequentialEvidence(baseRule, config = {}) {
87
+ return function sequentialPromotionRule(evidence) {
88
+ const base = baseRule(evidence);
89
+ const outcomes = evidence
90
+ .pairedOutcomes;
91
+ if (!outcomes)
92
+ return base;
93
+ const verdict = sequentialEvidence(outcomes, config);
94
+ if (verdict.significant)
95
+ return base;
96
+ return {
97
+ promote: false,
98
+ reasons: [
99
+ ...base.reasons,
100
+ `insufficient_sequential_evidence(e=${verdict.eValue.toFixed(2)}<${verdict.threshold})`,
101
+ ],
102
+ };
103
+ };
104
+ }
105
+ //# sourceMappingURL=sequential.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sequential.js","sourceRoot":"","sources":["../../src/sequential.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,EAAE;AACF,kBAAkB;AAClB,EAAE;AACF,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,kEAAkE;AAClE,yEAAyE;AACzE,8EAA8E;AAC9E,8EAA8E;AAC9E,4BAA4B;AAC5B,EAAE;AACF,wEAAwE;AACxE,8EAA8E;AAC9E,qDAAqD;AACrD,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,+EAA+E;AAC/E,EAAE;AACF,SAAS;AACT,EAAE;AACF,wEAAwE;AACxE,+EAA+E;AAC/E,4EAA4E;AAC5E,8EAA8E;AAC9E,gFAAgF;AAChF,mEAAmE;AACnE,EAAE;AACF,6EAA6E;AAC7E,yEAAyE;AACzE,2DAA2D;;AA8C3D,gDA6BC;AAeD,wDAsBC;AA5ED,MAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAChC,QAAyB,EACzB,SAA2B,EAAE;IAE7B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,aAAa,CAAC;IAC5C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,cAAc,CAAC;IAE/C,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC;QAAE,MAAM,IAAI,UAAU,CAAC,yBAAyB,CAAC,CAAC;IAC/E,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,GAAG,CAAC,CAAC;QAAE,MAAM,IAAI,UAAU,CAAC,0BAA0B,CAAC,CAAC;IAElF,MAAM,SAAS,GAAG,CAAC,G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erations } from './run.js';
2
2
  export type { FlywheelConfig, FlywheelResult } from './run.js';
3
3
  export { meetsPromotionRule, gateFingerprint } from './gate.js';
4
+ export { sequentialEvidence, withSequentialEvidence } from './sequential.js';
5
+ export type { PairedOutcome, SequentialConfig, SequentialVerdict } from './sequential.js';
4
6
  export { makeSigner, verifyReceipt, canon } from './receipts.js';
5
7
  export { InMemoryLineageStore, computeLiftCurve, liftPoint } from './lineage.js';
6
8
  export { verifyReplayBundle } from './replay.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAClD,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/D,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC7D,YAAY,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/D,YAAY,EACV,MAAM,EACN,YAAY,EACZ,iBAAiB,EACjB,KAAK,EACL,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,KAAK,EACL,YAAY,EACZ,WAAW,EACX,QAAQ,EACR,SAAS,EACT,gBAAgB,EAChB,MAAM,EACN,aAAa,EACb,YAAY,EACZ,SAAS,EACT,SAAS,EACT,YAAY,EACZ,oBAAoB,EACpB,WAAW,GACZ,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAClD,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/D,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAC7E,YAAY,EAAE,aAAa,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAC1F,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC7D,YAAY,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/D,YAAY,EACV,MAAM,EACN,YAAY,EACZ,iBAAiB,EACjB,KAAK,EACL,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,KAAK,EACL,YAAY,EACZ,WAAW,EACX,QAAQ,EACR,SAAS,EACT,gBAAgB,EAChB,MAAM,EACN,aAAa,EACb,YAAY,EACZ,SAAS,EACT,SAAS,EACT,YAAY,EACZ,oBAAoB,EACpB,WAAW,GACZ,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@
6
6
  // evaluator, gate, holdouts, and cost/security rules; get the same auditable, replayable improvement loop.
7
7
  export { runFlywheelGenerations } from './run.js';
8
8
  export { meetsPromotionRule, gateFingerprint } from './gate.js';
9
+ export { sequentialEvidence, withSequentialEvidence } from './sequential.js';
9
10
  export { makeSigner, verifyReceipt, canon } from './receipts.js';
10
11
  export { InMemoryLineageStore, computeLiftCurve, liftPoint } from './lineage.js';
11
12
  export { verifyReplayBundle } from './replay.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,uEAAuE;AACvE,EAAE;AACF,uGAAuG;AACvG,yGAAyG;AACzG,2GAA2G;AAC3G,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAGlD,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,uEAAuE;AACvE,EAAE;AACF,uGAAuG;AACvG,yGAAyG;AACzG,2GAA2G;AAC3G,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAGlD,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAE7E,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC"}
@@ -0,0 +1,53 @@
1
+ import type { PromotionRule } from './types.js';
2
+ /** Per-item paired outcome: did candidate and baseline each succeed? */
3
+ export interface PairedOutcome {
4
+ /** Suite item id. Pairing is by item — comparing unpaired sets is a different, weaker test. */
5
+ itemId: string;
6
+ candidateWon: boolean;
7
+ baselineWon: boolean;
8
+ }
9
+ export interface SequentialConfig {
10
+ /**
11
+ * Type-I error bound. Reject the null only when the e-value reaches 1/alpha.
12
+ * Default 0.05 => threshold 20.
13
+ */
14
+ alpha?: number;
15
+ /**
16
+ * Betting fraction in (0, 1). Higher detects large effects sooner but is
17
+ * slower on small ones. 0.5 is the Kelly-ish default and needs no tuning.
18
+ */
19
+ lambda?: number;
20
+ }
21
+ export interface SequentialVerdict {
22
+ /** True when accumulated evidence crosses 1/alpha. */
23
+ significant: boolean;
24
+ /** The e-value. Interpretable directly: 20 means "20:1 against the null". */
25
+ eValue: number;
26
+ threshold: number;
27
+ /** Items where the two arms disagreed — the only ones carrying information. */
28
+ informativePairs: number;
29
+ totalPairs: number;
30
+ }
31
+ /**
32
+ * Accumulate paired outcomes into an anytime-valid e-value.
33
+ *
34
+ * Only discordant pairs move the e-value: if both arms win or both lose, that
35
+ * item tells you nothing about which is better (this is the McNemar insight,
36
+ * carried over to the sequential setting).
37
+ */
38
+ export declare function sequentialEvidence(outcomes: PairedOutcome[], config?: SequentialConfig): SequentialVerdict;
39
+ /**
40
+ * Compose a frozen gate with a sequential-evidence requirement.
41
+ *
42
+ * The returned rule is still a plain `PromotionRule`, so it fingerprints and
43
+ * freezes exactly like the default one. A candidate must satisfy BOTH: every
44
+ * clause of `baseRule`, and evidence strong enough to survive having been
45
+ * looked at repeatedly.
46
+ *
47
+ * Paired outcomes are read from `evidence.pairedOutcomes` when present. When
48
+ * absent the rule degrades to `baseRule` alone rather than silently blocking
49
+ * every promotion — a caller that has not wired up per-item outcomes yet should
50
+ * get the old behavior, not a permanently closed gate.
51
+ */
52
+ export declare function withSequentialEvidence(baseRule: PromotionRule, config?: SequentialConfig): PromotionRule;
53
+ //# sourceMappingURL=sequential.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sequential.d.ts","sourceRoot":"","sources":["../src/sequential.ts"],"names":[],"mappings":"AAkCA,OAAO,KAAK,EAAwC,aAAa,EAAE,MAAM,YAAY,CAAC;AAEtF,wEAAwE;AACxE,MAAM,WAAW,aAAa;IAC5B,+FAA+F;IAC/F,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,OAAO,CAAC;IACtB,WAAW,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,sDAAsD;IACtD,WAAW,EAAE,OAAO,CAAC;IACrB,6EAA6E;IAC7E,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,+EAA+E;IAC/E,gBAAgB,EAAE,MAAM,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;CACpB;AAKD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,aAAa,EAAE,EACzB,MAAM,GAAE,gBAAqB,GAC5B,iBAAiB,CA0BnB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,aAAa,EACvB,MAAM,GAAE,gBAAqB,GAC5B,aAAa,CAmBf"}
@@ -0,0 +1,101 @@
1
+ // @metaharness/flywheel — anytime-valid sequential testing for promotion gates.
2
+ //
3
+ // WHY THIS EXISTS
4
+ //
5
+ // `meetsPromotionRule` is a single-shot conjunctive comparison, and it is the
6
+ // right shape: frozen, fingerprintable, every clause load-bearing. But a
7
+ // flywheel run evaluates MANY generations against the SAME holdout, and that is
8
+ // uncontrolled multiple testing. Published measurements of greedy
9
+ // "accept-if-the-score-improved" acceptance put the false-commit rate at
10
+ // 30-42%, and found 13-21 spurious modifications made even when NO true gains
11
+ // existed — degrading one agent by 4.9 points while every individual decision
12
+ // looked locally justified.
13
+ //
14
+ // The conjunctive gate plus a frozen anchor already mitigates this with
15
+ // multiple hurdles, which is real. It is not, however, anytime-valid: nothing
16
+ // in it accounts for how many times you have looked.
17
+ //
18
+ // This module adds that missing property WITHOUT touching the default gate,
19
+ // whose stability is itself the product. It composes: wrap any PromotionRule,
20
+ // and a candidate must clear both the frozen clauses AND accumulated evidence.
21
+ //
22
+ // METHOD
23
+ //
24
+ // Testing-by-betting / e-processes. An e-value is a non-negative random
25
+ // variable with expectation <= 1 under the null hypothesis ("this candidate is
26
+ // no better than baseline"). By Ville's inequality, P(sup_t E_t >= 1/alpha)
27
+ // <= alpha, so you may stop and reject at ANY time — no pre-registered sample
28
+ // size, no alpha spending schedule, and no penalty for peeking. That is exactly
29
+ // the property a flywheel needs, because it peeks by construction.
30
+ //
31
+ // The bet here is deliberately simple and assumption-light: per paired item,
32
+ // a candidate win against a baseline loss multiplies the e-value up, the
33
+ // reverse multiplies it down, and ties leave it unchanged.
34
+ const DEFAULT_ALPHA = 0.05;
35
+ const DEFAULT_LAMBDA = 0.5;
36
+ /**
37
+ * Accumulate paired outcomes into an anytime-valid e-value.
38
+ *
39
+ * Only discordant pairs move the e-value: if both arms win or both lose, that
40
+ * item tells you nothing about which is better (this is the McNemar insight,
41
+ * carried over to the sequential setting).
42
+ */
43
+ export function sequentialEvidence(outcomes, config = {}) {
44
+ const alpha = config.alpha ?? DEFAULT_ALPHA;
45
+ const lambda = config.lambda ?? DEFAULT_LAMBDA;
46
+ if (!(alpha > 0 && alpha < 1))
47
+ throw new RangeError('alpha must be in (0, 1)');
48
+ if (!(lambda > 0 && lambda < 1))
49
+ throw new RangeError('lambda must be in (0, 1)');
50
+ const threshold = 1 / alpha;
51
+ let eValue = 1;
52
+ let informativePairs = 0;
53
+ for (const o of outcomes) {
54
+ if (o.candidateWon === o.baselineWon)
55
+ continue; // concordant: no information
56
+ informativePairs++;
57
+ // Under the null, a discordant pair favors either arm with probability 1/2,
58
+ // so E[multiplier] = 1 and the process is a non-negative martingale.
59
+ eValue *= o.candidateWon ? 1 + lambda : 1 - lambda;
60
+ }
61
+ return {
62
+ significant: eValue >= threshold,
63
+ eValue,
64
+ threshold,
65
+ informativePairs,
66
+ totalPairs: outcomes.length,
67
+ };
68
+ }
69
+ /**
70
+ * Compose a frozen gate with a sequential-evidence requirement.
71
+ *
72
+ * The returned rule is still a plain `PromotionRule`, so it fingerprints and
73
+ * freezes exactly like the default one. A candidate must satisfy BOTH: every
74
+ * clause of `baseRule`, and evidence strong enough to survive having been
75
+ * looked at repeatedly.
76
+ *
77
+ * Paired outcomes are read from `evidence.pairedOutcomes` when present. When
78
+ * absent the rule degrades to `baseRule` alone rather than silently blocking
79
+ * every promotion — a caller that has not wired up per-item outcomes yet should
80
+ * get the old behavior, not a permanently closed gate.
81
+ */
82
+ export function withSequentialEvidence(baseRule, config = {}) {
83
+ return function sequentialPromotionRule(evidence) {
84
+ const base = baseRule(evidence);
85
+ const outcomes = evidence
86
+ .pairedOutcomes;
87
+ if (!outcomes)
88
+ return base;
89
+ const verdict = sequentialEvidence(outcomes, config);
90
+ if (verdict.significant)
91
+ return base;
92
+ return {
93
+ promote: false,
94
+ reasons: [
95
+ ...base.reasons,
96
+ `insufficient_sequential_evidence(e=${verdict.eValue.toFixed(2)}<${verdict.threshold})`,
97
+ ],
98
+ };
99
+ };
100
+ }
101
+ //# sourceMappingURL=sequential.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sequential.js","sourceRoot":"","sources":["../src/sequential.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,EAAE;AACF,kBAAkB;AAClB,EAAE;AACF,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,kEAAkE;AAClE,yEAAyE;AACzE,8EAA8E;AAC9E,8EAA8E;AAC9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C,GAAG,MAAM,CAAC;IACrD,CAAC;IAED,OAAO;QACL,WAAW,EAAE,MAAM,IAAI,SAAS;QAChC,MAAM;QACN,SAAS;QACT,gBAAgB;QAChB,UAAU,EAAE,QAAQ,CAAC,MAAM;KAC5B,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,sBAAsB,CACpC,QAAuB,EACvB,SAA2B,EAAE;IAE7B,OAAO,SAAS,uBAAuB,CAAC,QAA2B;QACjE,MAAM,IAAI,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAChC,MAAM,QAAQ,GAAI,QAAqE;aACpF,cAAc,CAAC;QAElB,IAAI,CAAC,QAAQ;YAAE,OAAO,IAAI,CAAC;QAE3B,MAAM,OAAO,GAAG,kBAAkB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QACrD,IAAI,OAAO,CAAC,WAAW;YAAE,OAAO,IAAI,CAAC;QAErC,OAAO;YACL,OAAO,EAAE,KAAK;YACd,OAAO,EAAE;gBACP,GAAG,IAAI,CAAC,OAAO;gBACf,sCAAsC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,SAAS,GAAG;aACxF;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metaharness/flywheel",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "A verifiable self-improvement loop for agent harnesses. Freeze the model. Evolve the harness. Promote only what proves lift.",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",