@hmharness/evolution 0.14.19 → 0.14.20

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/index.d.ts CHANGED
@@ -19,3 +19,4 @@ export * from './knowledge.ts';
19
19
  export * from './radar.ts';
20
20
  export * from './external.ts';
21
21
  export * from './knowledge-graph.ts';
22
+ export * from './router-evolution.ts';
package/dist/index.js CHANGED
@@ -19,3 +19,4 @@ export * from "./knowledge.js";
19
19
  export * from "./radar.js";
20
20
  export * from "./external.js";
21
21
  export * from "./knowledge-graph.js";
22
+ export * from "./router-evolution.js";
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @hmharness/evolution - Router Evolution (P2-05)
3
+ *
4
+ * The audit called for: "让 model routing 本身成为可实验、可回滚策略"
5
+ *
6
+ * Makes routing decisions experimentable: a routing policy can be registered
7
+ * as a candidate, A/B tested against the current policy, and promoted or
8
+ * rolled back based on SPRT results.
9
+ */
10
+ import type { RouteHistoryEntry } from '@hmharness/kernel';
11
+ export interface RoutingPolicy {
12
+ id: string;
13
+ name: string;
14
+ description: string;
15
+ /** the routing function */
16
+ decide: (history: RouteHistoryEntry[], taskType: string, candidates: string[]) => string;
17
+ }
18
+ export interface RoutingExperiment {
19
+ id: string;
20
+ /** the challenger policy */
21
+ challenger: RoutingPolicy;
22
+ /** incumbent policy (or 'default' for current behavior) */
23
+ incumbent: string;
24
+ /** control arm results */
25
+ controlSuccesses: number;
26
+ controlTotal: number;
27
+ /** treatment arm results */
28
+ treatmentSuccesses: number;
29
+ treatmentTotal: number;
30
+ status: 'running' | 'promoted' | 'rolled-back' | 'inconclusive';
31
+ startedAt: string;
32
+ }
33
+ /**
34
+ * Evaluate a routing experiment using simple SPRT logic.
35
+ * Pure - testable.
36
+ */
37
+ export declare function evaluateExperiment(exp: RoutingExperiment): {
38
+ decision: 'continue' | 'promote' | 'rollback';
39
+ reason: string;
40
+ };
41
+ /**
42
+ * The routing policy registry - manages candidate and active policies.
43
+ */
44
+ export declare class RoutingPolicyRegistry {
45
+ private policies;
46
+ private activePolicyId;
47
+ register(policy: RoutingPolicy): {
48
+ ok: boolean;
49
+ reason?: string;
50
+ };
51
+ get(id: string): RoutingPolicy | undefined;
52
+ list(): RoutingPolicy[];
53
+ /** Get the currently active policy */
54
+ getActive(): RoutingPolicy | undefined;
55
+ /** Promote a policy to active (rollback = set back to 'default') */
56
+ promote(policyId: string): boolean;
57
+ /** Rollback to default policy */
58
+ rollback(): void;
59
+ get activeId(): string;
60
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * @hmharness/evolution - Router Evolution (P2-05)
3
+ *
4
+ * The audit called for: "让 model routing 本身成为可实验、可回滚策略"
5
+ *
6
+ * Makes routing decisions experimentable: a routing policy can be registered
7
+ * as a candidate, A/B tested against the current policy, and promoted or
8
+ * rolled back based on SPRT results.
9
+ */
10
+ /**
11
+ * Evaluate a routing experiment using simple SPRT logic.
12
+ * Pure - testable.
13
+ */
14
+ export function evaluateExperiment(exp) {
15
+ const minN = 20; // minimum samples per arm
16
+ if (exp.controlTotal < minN || exp.treatmentTotal < minN) {
17
+ return { decision: 'continue', reason: `insufficient samples (control ${exp.controlTotal}/${minN}, treatment ${exp.treatmentTotal}/${minN})` };
18
+ }
19
+ const controlRate = exp.controlSuccesses / exp.controlTotal;
20
+ const treatmentRate = exp.treatmentSuccesses / exp.treatmentTotal;
21
+ const diff = treatmentRate - controlRate;
22
+ // promote if treatment is 5pp better, rollback if 5pp worse
23
+ if (diff >= 0.05) {
24
+ return { decision: 'promote', reason: `treatment ${treatmentRate.toFixed(2)} vs control ${controlRate.toFixed(2)} (+${(diff * 100).toFixed(1)}pp)` };
25
+ }
26
+ if (diff <= -0.05) {
27
+ return { decision: 'rollback', reason: `treatment ${treatmentRate.toFixed(2)} vs control ${controlRate.toFixed(2)} (${(diff * 100).toFixed(1)}pp)` };
28
+ }
29
+ return { decision: 'continue', reason: `diff ${(diff * 100).toFixed(1)}pp within noise band` };
30
+ }
31
+ /**
32
+ * The routing policy registry - manages candidate and active policies.
33
+ */
34
+ export class RoutingPolicyRegistry {
35
+ policies = new Map();
36
+ activePolicyId = 'default';
37
+ register(policy) {
38
+ if (this.policies.has(policy.id))
39
+ return { ok: false, reason: `policy ${policy.id} already registered` };
40
+ this.policies.set(policy.id, policy);
41
+ return { ok: true };
42
+ }
43
+ get(id) {
44
+ return this.policies.get(id);
45
+ }
46
+ list() {
47
+ return [...this.policies.values()];
48
+ }
49
+ /** Get the currently active policy */
50
+ getActive() {
51
+ return this.policies.get(this.activePolicyId);
52
+ }
53
+ /** Promote a policy to active (rollback = set back to 'default') */
54
+ promote(policyId) {
55
+ if (!this.policies.has(policyId))
56
+ return false;
57
+ this.activePolicyId = policyId;
58
+ return true;
59
+ }
60
+ /** Rollback to default policy */
61
+ rollback() {
62
+ this.activePolicyId = 'default';
63
+ }
64
+ get activeId() {
65
+ return this.activePolicyId;
66
+ }
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/evolution",
3
- "version": "0.14.19",
3
+ "version": "0.14.20",
4
4
  "description": "hmharness evolution subsystem: persistent memory, insight capture, skill library, and the bench that gives evolution its fitness signal. First-class kernel citizen, not a plugin.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",