File size: 5,402 Bytes
963e554
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
 * Typed fetch wrappers + contracts for the anytime-valid sequential
 * regression-detection engine (/api/seqeval/*).
 *
 * Decision semantics (from GET /policy):
 *   REGRESSION            → block (evidence of a drop beyond the margin)
 *   PASS                  → allow (equivalence proven within the margin)
 *   INSUFFICIENT_EVIDENCE → not proven equivalent; never treated as a pass
 */

async function handle<T>(res: Response): Promise<T> {
  if (!res.ok) {
    const body = await res.text().catch(() => '');
    throw new Error(`API ${res.status}: ${body || res.statusText}`);
  }
  return res.json() as Promise<T>;
}

const JSON_HEADERS = { 'Content-Type': 'application/json' };

function get<T>(url: string): Promise<T> {
  return fetch(url).then((res) => handle<T>(res));
}

function post<T>(url: string, body?: unknown): Promise<T> {
  return fetch(url, {
    method: 'POST',
    headers: JSON_HEADERS,
    body: body === undefined ? undefined : JSON.stringify(body),
  }).then((res) => handle<T>(res));
}

// ---------------------------------------------------------------- contracts

export type SeqDecision = 'REGRESSION' | 'PASS' | 'INSUFFICIENT_EVIDENCE';

export interface SeqModelSpec {
  model_version: string;
  effects: Record<string, number>;
  fingerprint?: string;
}

export interface SeqNode {
  node: string; // "overall" | "class:vehicle" | "stratum:pedestrian|night" | ...
  level: number;
  n: number;
  n_clusters: number;
  delta_estimate: number;
  delta_ci: [number, number];
  e_regression: number;
  e_pass: number;
  p_analogue: number | null;
  bayes_p_regression: number | null;
  decision: SeqDecision;
  safety_primary: boolean;
  suspect: boolean;
  alpha_allocated: number;
  e_threshold_regression: number;
  e_threshold_pass: number;
}

export interface TrajectoryPoint {
  n: number;
  n_clusters: number;
  delta_estimate: number;
  delta_lower: number;
  delta_upper: number;
  log_e_regression: number;
  log_e_pass: number;
  decision: SeqDecision;
}

export interface NodeTrajectory {
  points: TrajectoryPoint[];
  boundaries: { log_e_regression: number; log_e_pass: number; delta_margin: number };
}

export interface SeqBudget {
  planned_total: number;
  samples_used: number;
  escalation_used: number;
  full_population: number;
  fraction_of_population: number;
}

export interface SeqStratumPlan {
  key: string;
  class: string;
  condition: string;
  N: number;
  allocated: number;
  clusters: number;
  reserve: number;
  weight: number;
  safety_primary: boolean;
}

export interface SeqPlan {
  plan_id: string;
  plan_hash: string;
  population_id: string;
  dataset_fingerprint: string;
  seed: number;
  created_at: string;
  config: Record<string, unknown>;
  population_objects: number;
  total_allocated: number;
  strata: Record<string, SeqStratumPlan>;
  frozen_before_candidate_outcomes: boolean;
}

export interface SeqRunState {
  run_id: string;
  population_id: string;
  baseline: SeqModelSpec;
  candidate: SeqModelSpec;
  status: 'running' | 'done' | 'failed' | string;
  stage: string;
  decision: SeqDecision | null;
  gate: 'block' | 'allow' | 'expand_budget_or_report' | string | null;
  message: string | null;
  stopping_reason: string | null;
  error: string | null;
  seed: number;
  policy: {
    metric: string;
    delta_margin: number;
    alpha: number;
    alpha_pass: number;
    alpha_shares: Record<string, number>;
    condition_dim: string;
    safety_primaries: string[];
    target_n: number;
    [k: string]: unknown;
  };
  sanity: Record<string, unknown> | null;
  budget: SeqBudget | null;
  plan: SeqPlan | null;
  nodes: SeqNode[];
  attribution?: SeqAttribution | null;
  trajectories?: Record<string, NodeTrajectory>;
  created_at: string;
  finished_at: string | null;
}

export interface SeqAttribution {
  run_id?: string;
  decision?: SeqDecision | null;
  gate?: string | null;
  classes?: string[];
  conditions?: string[];
  cells?: Array<{
    class: string;
    condition: string;
    delta: number | null;
    n?: number;
    flagged?: boolean;
    [k: string]: unknown;
  }>;
  [k: string]: unknown;
}

export interface SeqPolicyInfo {
  default_policy: Record<string, unknown>;
  decision_semantics: Record<string, string>;
  test_method: string;
  multiple_testing_method: string;
  notes: string[];
}

// ---------------------------------------------------------------- endpoints

export const listSeqRuns = () => get<{ runs: SeqRunState[] }>('/api/seqeval/runs');

export const getSeqRun = (runId: string) => get<SeqRunState>(`/api/seqeval/runs/${encodeURIComponent(runId)}`);

export const getSeqAttribution = (runId: string) =>
  get<SeqAttribution>(`/api/seqeval/runs/${encodeURIComponent(runId)}/attribution`);

export const getSeqEvidence = (runId: string) =>
  get<{ run_id: string; records: Array<Record<string, unknown>>; lineage: Record<string, unknown>; required_fields: string[] }>(
    `/api/seqeval/runs/${encodeURIComponent(runId)}/evidence`
  );

export const getSeqPolicy = () => get<SeqPolicyInfo>('/api/seqeval/policy');

export const startSeqRun = (body: {
  population_id: string;
  baseline: { model_version: string; effects: Record<string, number> };
  candidate: { model_version: string; effects: Record<string, number> };
  policy?: Record<string, unknown>;
  seed?: number;
  sync?: boolean;
}) => post<SeqRunState>('/api/seqeval/runs', body);