@superbased/sdk 0.0.1-placeholder.0 → 0.1.0
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/NOTICE +4 -0
- package/README.md +434 -5
- package/dist/cjs/admit.d.ts +139 -0
- package/dist/cjs/admit.js +442 -0
- package/dist/cjs/admit.js.map +1 -0
- package/dist/cjs/attrs.d.ts +113 -0
- package/dist/cjs/attrs.js +183 -0
- package/dist/cjs/attrs.js.map +1 -0
- package/dist/cjs/config.d.ts +202 -0
- package/dist/cjs/config.js +386 -0
- package/dist/cjs/config.js.map +1 -0
- package/dist/cjs/context.d.ts +102 -0
- package/dist/cjs/context.js +234 -0
- package/dist/cjs/context.js.map +1 -0
- package/dist/cjs/control.d.ts +99 -0
- package/dist/cjs/control.js +243 -0
- package/dist/cjs/control.js.map +1 -0
- package/dist/cjs/cost.d.ts +108 -0
- package/dist/cjs/cost.js +211 -0
- package/dist/cjs/cost.js.map +1 -0
- package/dist/cjs/destination.d.ts +125 -0
- package/dist/cjs/destination.js +357 -0
- package/dist/cjs/destination.js.map +1 -0
- package/dist/cjs/experiments.d.ts +237 -0
- package/dist/cjs/experiments.js +678 -0
- package/dist/cjs/experiments.js.map +1 -0
- package/dist/cjs/export.d.ts +84 -0
- package/dist/cjs/export.js +328 -0
- package/dist/cjs/export.js.map +1 -0
- package/dist/cjs/index.d.ts +168 -0
- package/dist/cjs/index.js +581 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/instrument.d.ts +88 -0
- package/dist/cjs/instrument.js +397 -0
- package/dist/cjs/instrument.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/policy.d.ts +77 -0
- package/dist/cjs/policy.js +252 -0
- package/dist/cjs/policy.js.map +1 -0
- package/dist/cjs/prompts.d.ts +175 -0
- package/dist/cjs/prompts.js +476 -0
- package/dist/cjs/prompts.js.map +1 -0
- package/dist/cjs/propagation.d.ts +34 -0
- package/dist/cjs/propagation.js +151 -0
- package/dist/cjs/propagation.js.map +1 -0
- package/dist/cjs/req.d.ts +10 -0
- package/dist/cjs/req.js +3 -0
- package/dist/cjs/req.js.map +1 -0
- package/dist/cjs/runtime.d.ts +44 -0
- package/dist/cjs/runtime.js +67 -0
- package/dist/cjs/runtime.js.map +1 -0
- package/dist/cjs/scores.d.ts +102 -0
- package/dist/cjs/scores.js +226 -0
- package/dist/cjs/scores.js.map +1 -0
- package/dist/cjs/spans.d.ts +193 -0
- package/dist/cjs/spans.js +398 -0
- package/dist/cjs/spans.js.map +1 -0
- package/dist/cjs/vendor.d.ts +33 -0
- package/dist/cjs/vendor.js +67 -0
- package/dist/cjs/vendor.js.map +1 -0
- package/dist/esm/admit.d.ts +139 -0
- package/dist/esm/admit.js +432 -0
- package/dist/esm/admit.js.map +1 -0
- package/dist/esm/attrs.d.ts +113 -0
- package/dist/esm/attrs.js +179 -0
- package/dist/esm/attrs.js.map +1 -0
- package/dist/esm/config.d.ts +202 -0
- package/dist/esm/config.js +364 -0
- package/dist/esm/config.js.map +1 -0
- package/dist/esm/context.d.ts +102 -0
- package/dist/esm/context.js +221 -0
- package/dist/esm/context.js.map +1 -0
- package/dist/esm/control.d.ts +99 -0
- package/dist/esm/control.js +230 -0
- package/dist/esm/control.js.map +1 -0
- package/dist/esm/cost.d.ts +108 -0
- package/dist/esm/cost.js +202 -0
- package/dist/esm/cost.js.map +1 -0
- package/dist/esm/destination.d.ts +125 -0
- package/dist/esm/destination.js +346 -0
- package/dist/esm/destination.js.map +1 -0
- package/dist/esm/experiments.d.ts +237 -0
- package/dist/esm/experiments.js +666 -0
- package/dist/esm/experiments.js.map +1 -0
- package/dist/esm/export.d.ts +84 -0
- package/dist/esm/export.js +314 -0
- package/dist/esm/export.js.map +1 -0
- package/dist/esm/index.d.ts +168 -0
- package/dist/esm/index.js +500 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/instrument.d.ts +88 -0
- package/dist/esm/instrument.js +391 -0
- package/dist/esm/instrument.js.map +1 -0
- package/dist/esm/policy.d.ts +77 -0
- package/dist/esm/policy.js +240 -0
- package/dist/esm/policy.js.map +1 -0
- package/dist/esm/prompts.d.ts +175 -0
- package/dist/esm/prompts.js +460 -0
- package/dist/esm/prompts.js.map +1 -0
- package/dist/esm/propagation.d.ts +34 -0
- package/dist/esm/propagation.js +145 -0
- package/dist/esm/propagation.js.map +1 -0
- package/dist/esm/req.d.ts +10 -0
- package/dist/esm/req.js +2 -0
- package/dist/esm/req.js.map +1 -0
- package/dist/esm/runtime.d.ts +44 -0
- package/dist/esm/runtime.js +59 -0
- package/dist/esm/runtime.js.map +1 -0
- package/dist/esm/scores.d.ts +102 -0
- package/dist/esm/scores.js +219 -0
- package/dist/esm/scores.js.map +1 -0
- package/dist/esm/spans.d.ts +193 -0
- package/dist/esm/spans.js +378 -0
- package/dist/esm/spans.js.map +1 -0
- package/dist/esm/vendor.d.ts +33 -0
- package/dist/esm/vendor.js +61 -0
- package/dist/esm/vendor.js.map +1 -0
- package/package.json +121 -4
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Datasets and experiments (W3-2; contract `contracts/experiments.md`, lane
|
|
4
|
+
* B7; the same flow as the Python SDK's `run_experiment`).
|
|
5
|
+
*
|
|
6
|
+
* `runExperiment({ dataset, task, evaluators })`:
|
|
7
|
+
* 1. finds the dataset by name or id on each destination side (online
|
|
8
|
+
* first, then this machine), or uploads inline items as a literal
|
|
9
|
+
* snapshot of `datasetName` (created on the machine; on the org it must
|
|
10
|
+
* exist). Inline items without `datasetName` run in-process only and
|
|
11
|
+
* nothing is posted;
|
|
12
|
+
* 2. creates a run on each side (`POST /v1/sdk/experiments`, machine
|
|
13
|
+
* `/api/obs/eval/experiments`), which freezes a snapshot unless
|
|
14
|
+
* `snapshotVersion` pins one, and reads that snapshot's items. Under `both`
|
|
15
|
+
* the task runs once: the other side's items are matched by client id,
|
|
16
|
+
* then by input;
|
|
17
|
+
* 3. runs `task` on each item in its own trace (a CHAIN root span stamped
|
|
18
|
+
* `sbo.experiment.run_id` / `sbo.experiment.item_id`), then the
|
|
19
|
+
* evaluators in-process;
|
|
20
|
+
* 4. posts each item's result (output under captureContent, scores, error)
|
|
21
|
+
* to each run as it finishes; the server also stores each score on the
|
|
22
|
+
* item's trace;
|
|
23
|
+
* 5. finishes the runs and reads their totals.
|
|
24
|
+
*
|
|
25
|
+
* An experiment is an explicit batch job, so unlike the tracing calls a
|
|
26
|
+
* dataset that cannot be found or a run that cannot be created rejects with
|
|
27
|
+
* {@link ExperimentError}, and `failUnder` rejects with
|
|
28
|
+
* {@link ExperimentGateFailed} (and sets `process.exitCode = 1`) when the
|
|
29
|
+
* pass rate is below it. A task or evaluator that throws is recorded on its
|
|
30
|
+
* item; the run goes on. CI credentials need the `sdk:eval` scope.
|
|
31
|
+
*/
|
|
32
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
|
+
exports.ExperimentGateFailed = exports.ExperimentError = void 0;
|
|
34
|
+
exports.experimentGate = experimentGate;
|
|
35
|
+
exports.literalItemId = literalItemId;
|
|
36
|
+
exports.literalRows = literalRows;
|
|
37
|
+
exports.getDataset = getDataset;
|
|
38
|
+
exports.runExperiment = runExperiment;
|
|
39
|
+
exports.scorePasses = scorePasses;
|
|
40
|
+
exports.experimentTotals = experimentTotals;
|
|
41
|
+
const node_crypto_1 = require("node:crypto");
|
|
42
|
+
const api_1 = require("@opentelemetry/api");
|
|
43
|
+
const attrs_js_1 = require("./attrs.js");
|
|
44
|
+
const control_js_1 = require("./control.js");
|
|
45
|
+
const runtime_js_1 = require("./runtime.js");
|
|
46
|
+
const spans_js_1 = require("./spans.js");
|
|
47
|
+
/** The experiment could not be set up (dataset, run creation). */
|
|
48
|
+
class ExperimentError extends Error {
|
|
49
|
+
constructor(message) {
|
|
50
|
+
super(message);
|
|
51
|
+
this.name = "ExperimentError";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.ExperimentError = ExperimentError;
|
|
55
|
+
/**
|
|
56
|
+
* A destination refused a dataset or experiment call for want of a scope
|
|
57
|
+
* (403 `insufficient_scope`): a member SDK key from `observer sdk sign-in`
|
|
58
|
+
* deliberately lacks `sdk:eval`, which every org dataset and experiment route
|
|
59
|
+
* needs. runExperiment skips that destination with an entry in `errors` and
|
|
60
|
+
* carries on with the other one; it never rejects for this.
|
|
61
|
+
*/
|
|
62
|
+
class SideSkipped extends ExperimentError {
|
|
63
|
+
}
|
|
64
|
+
function scopeRefused(r) {
|
|
65
|
+
return r.status === 403 && r.error === "insufficient_scope";
|
|
66
|
+
}
|
|
67
|
+
function skipReason(side, what, r) {
|
|
68
|
+
return `${side}: skipped, ${what} refused: ${describe(r)} (an SDK key from sign-in has no sdk:eval scope; run experiments on the org with a CI credential that has it)`;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The `failUnder` gate failed (a CI gate): the pass rate is below it, or,
|
|
72
|
+
* strict (the default), `reasons` says why the gate could not pass at all.
|
|
73
|
+
*/
|
|
74
|
+
class ExperimentGateFailed extends Error {
|
|
75
|
+
result;
|
|
76
|
+
failUnder;
|
|
77
|
+
reasons;
|
|
78
|
+
constructor(result, failUnder, reasons = []) {
|
|
79
|
+
super(reasons.length
|
|
80
|
+
? `experiment ${JSON.stringify(result.name)}: the failUnder gate cannot pass: ${reasons.join("; ")} (strict; pass strict: false to gate on the pass rate alone)`
|
|
81
|
+
: `experiment ${JSON.stringify(result.name)}: pass rate ${result.passRate.toFixed(3)} is below ${failUnder.toFixed(3)}`);
|
|
82
|
+
this.result = result;
|
|
83
|
+
this.failUnder = failUnder;
|
|
84
|
+
this.reasons = reasons;
|
|
85
|
+
this.name = "ExperimentGateFailed";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.ExperimentGateFailed = ExperimentGateFailed;
|
|
89
|
+
/**
|
|
90
|
+
* The `failUnder` gate (contract experiments.md 4.2, review E-12): `reasons`
|
|
91
|
+
* are why a strict gate fails before the rate is compared (nothing scored, a
|
|
92
|
+
* skipped destination, results not accepted); `failed` applies them when
|
|
93
|
+
* `strict`, then the rate. A run that scored nothing is not failed when not
|
|
94
|
+
* strict. Python: gate_problems / gate_fails.
|
|
95
|
+
*/
|
|
96
|
+
function experimentGate(r, failUnder, strict = true) {
|
|
97
|
+
const reasons = [];
|
|
98
|
+
if (r.scored === 0)
|
|
99
|
+
reasons.push("no item was scored (no evaluator returned a score)");
|
|
100
|
+
for (const side of r.skippedSides)
|
|
101
|
+
reasons.push(`the ${side} destination was skipped`);
|
|
102
|
+
if (r.failedPosts)
|
|
103
|
+
reasons.push(`${r.failedPosts} result(s) were not accepted, so the recorded pass rate covers only part of the run`);
|
|
104
|
+
if (failUnder === undefined || failUnder === null)
|
|
105
|
+
return { failed: false, reasons };
|
|
106
|
+
if (strict && reasons.length)
|
|
107
|
+
return { failed: true, reasons };
|
|
108
|
+
return { failed: r.scored > 0 && !(r.passRate >= failUnder), reasons };
|
|
109
|
+
}
|
|
110
|
+
function obj(v) {
|
|
111
|
+
return v !== null && typeof v === "object" && !Array.isArray(v) ? v : {};
|
|
112
|
+
}
|
|
113
|
+
const idOf = (v) => (typeof v === "string" || typeof v === "number" ? String(v) : "");
|
|
114
|
+
function describe(r) {
|
|
115
|
+
return `${r.target}: ${r.status ? `HTTP ${r.status} ` : ""}${r.error}${r.message ? `: ${r.message}` : ""}`;
|
|
116
|
+
}
|
|
117
|
+
const enc = encodeURIComponent;
|
|
118
|
+
/** Find a dataset by name or id on one target. */
|
|
119
|
+
async function findDataset(t, nameOrId, opts) {
|
|
120
|
+
const timeoutMs = (0, control_js_1.apiTimeout)(opts);
|
|
121
|
+
const list = await (0, control_js_1.call)(t, "GET", { online: `/datasets?name=${enc(nameOrId)}`, local: "/api/obs/eval/datasets" }, { timeoutMs, quiet404: true });
|
|
122
|
+
if (scopeRefused(list))
|
|
123
|
+
return { id: "", name: "", error: skipReason(t.name, "dataset list", list), skipped: true };
|
|
124
|
+
if (list.ok) {
|
|
125
|
+
const rows = (Array.isArray(obj(list.body).datasets) ? obj(list.body).datasets : Array.isArray(list.body) ? list.body : []);
|
|
126
|
+
const hit = rows.map(obj).find((d) => d.name === nameOrId || idOf(d.id) === nameOrId);
|
|
127
|
+
if (hit)
|
|
128
|
+
return { id: idOf(hit.id), name: String(hit.name ?? nameOrId), error: "" };
|
|
129
|
+
}
|
|
130
|
+
const one = await (0, control_js_1.call)(t, "GET", { online: `/datasets/${enc(nameOrId)}`, local: `/api/obs/eval/datasets/${enc(nameOrId)}` }, { timeoutMs, quiet404: true });
|
|
131
|
+
if (one.ok) {
|
|
132
|
+
const d = obj(obj(one.body).dataset ?? one.body);
|
|
133
|
+
if (idOf(d.id))
|
|
134
|
+
return { id: idOf(d.id), name: String(d.name ?? nameOrId), error: "" };
|
|
135
|
+
}
|
|
136
|
+
return { id: "", name: "", error: list.ok ? `${t.name}: no dataset named ${JSON.stringify(nameOrId)}` : describe(list) };
|
|
137
|
+
}
|
|
138
|
+
function parseItem(x) {
|
|
139
|
+
return {
|
|
140
|
+
item: {
|
|
141
|
+
id: idOf(x.item_id ?? x.id) || undefined,
|
|
142
|
+
input: x.input,
|
|
143
|
+
expectedOutput: x.expected ?? x.expected_output,
|
|
144
|
+
metadata: Object.keys(obj(x.metadata)).length ? obj(x.metadata) : undefined,
|
|
145
|
+
},
|
|
146
|
+
hasContent: x.has_content !== false,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Compact JSON with object keys sorted at every level (`undefined` as null):
|
|
151
|
+
* the same text Python's canonical form gives, so both SDKs derive the same
|
|
152
|
+
* item id.
|
|
153
|
+
*/
|
|
154
|
+
function canonical(v) {
|
|
155
|
+
if (v === undefined || v === null)
|
|
156
|
+
return "null";
|
|
157
|
+
if (Array.isArray(v))
|
|
158
|
+
return `[${v.map(canonical).join(",")}]`;
|
|
159
|
+
if (typeof v === "object") {
|
|
160
|
+
const o = v;
|
|
161
|
+
return `{${Object.keys(o)
|
|
162
|
+
.sort()
|
|
163
|
+
.map((k) => `${JSON.stringify(k)}:${canonical(o[k])}`)
|
|
164
|
+
.join(",")}}`;
|
|
165
|
+
}
|
|
166
|
+
return JSON.stringify(v) ?? "null";
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The default id of a literal item: derived from its content (review E-75),
|
|
170
|
+
* so a re-run with changed inputs makes new items on the machine instead of
|
|
171
|
+
* matching the previous run's `item-00001`. Python gives the same id
|
|
172
|
+
* (sdk-behaviour-cases.json `literal_item_ids`).
|
|
173
|
+
*/
|
|
174
|
+
function literalItemId(input, expected, metadata) {
|
|
175
|
+
return "item-" + (0, node_crypto_1.createHash)("sha256").update(canonical([input, expected, metadata]), "utf8").digest("hex").slice(0, 16);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Inline items as the literal upload rows `{id, input, expected, metadata?}`.
|
|
179
|
+
* A missing id is {@link literalItemId} of the content; the same content
|
|
180
|
+
* twice gets `-2`, `-3`, ... (E-75; Python's `_literal`).
|
|
181
|
+
*/
|
|
182
|
+
function literalRows(ds) {
|
|
183
|
+
const seen = new Map();
|
|
184
|
+
return ds.map((it) => {
|
|
185
|
+
const expected = it.expectedOutput ?? it.expected;
|
|
186
|
+
let id = it.id === undefined || it.id === null || it.id === "" ? "" : String(it.id);
|
|
187
|
+
if (!id) {
|
|
188
|
+
id = literalItemId(it.input, expected, it.metadata);
|
|
189
|
+
const n = (seen.get(id) ?? 0) + 1;
|
|
190
|
+
seen.set(id, n);
|
|
191
|
+
if (n > 1)
|
|
192
|
+
id = `${id}-${n}`;
|
|
193
|
+
}
|
|
194
|
+
const row = { id, input: it.input, expected };
|
|
195
|
+
if (it.metadata)
|
|
196
|
+
row.metadata = it.metadata;
|
|
197
|
+
return row;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
/** Read a snapshot's items on one target (all pages); `runId`: the run's own items on the machine. */
|
|
201
|
+
async function readItems(t, datasetId, version, opts, runId = "") {
|
|
202
|
+
const items = [];
|
|
203
|
+
const rawItems = [];
|
|
204
|
+
let skipped = 0;
|
|
205
|
+
let cursor = "";
|
|
206
|
+
for (let page = 0; page < 1000; page++) {
|
|
207
|
+
const q = `limit=500${cursor ? `&cursor=${enc(cursor)}` : ""}`;
|
|
208
|
+
const r = await (0, control_js_1.call)(t, "GET", {
|
|
209
|
+
online: `/datasets/${enc(datasetId)}/snapshots/${version}/items?${q}`,
|
|
210
|
+
// run=: the run's pinned set (a literal upload), else every item up to its version (R-B22, E-75).
|
|
211
|
+
local: `/api/obs/eval/datasets/${enc(datasetId)}/items?${q}${version ? `&version=${version}` : ""}${runId ? `&run=${enc(runId)}` : ""}`,
|
|
212
|
+
}, { timeoutMs: (0, control_js_1.apiTimeout)(opts) });
|
|
213
|
+
if (!r.ok)
|
|
214
|
+
return { items, raw: rawItems, skipped, error: describe(r), scopeRefused: scopeRefused(r) ? r : undefined };
|
|
215
|
+
const body = obj(r.body);
|
|
216
|
+
for (const raw of (Array.isArray(body.items) ? body.items : [])) {
|
|
217
|
+
const { item, hasContent } = parseItem(obj(raw));
|
|
218
|
+
if (hasContent) {
|
|
219
|
+
items.push(item);
|
|
220
|
+
rawItems.push(obj(raw));
|
|
221
|
+
}
|
|
222
|
+
else
|
|
223
|
+
skipped += 1;
|
|
224
|
+
}
|
|
225
|
+
cursor = typeof body.next_cursor === "string" ? body.next_cursor : "";
|
|
226
|
+
if (!cursor)
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
return { items, raw: rawItems, skipped, error: "" };
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Fetch a dataset's items: by name or id, from a snapshot `version` (default
|
|
233
|
+
* the latest), from the first destination that has it (the org first under
|
|
234
|
+
* `both`). Rejects with {@link ExperimentError} when no destination has it
|
|
235
|
+
* (or none is usable), as Python's `get_dataset` raises (review R-C34);
|
|
236
|
+
* `errors` lists what an earlier destination answered when a later one had
|
|
237
|
+
* it.
|
|
238
|
+
*/
|
|
239
|
+
async function getDataset(nameOrId, opts = {}) {
|
|
240
|
+
const errors = [];
|
|
241
|
+
await (0, runtime_js_1.destinationKnown)();
|
|
242
|
+
const ts = (0, control_js_1.targets)("read", opts);
|
|
243
|
+
if (!ts.length)
|
|
244
|
+
errors.push((0, control_js_1.noTargetReason)());
|
|
245
|
+
for (const t of ts) {
|
|
246
|
+
const ds = await findDataset(t, nameOrId, opts);
|
|
247
|
+
if (!ds.id) {
|
|
248
|
+
errors.push(ds.error);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
let version = opts.version ?? 0;
|
|
252
|
+
if (!version && t.name === "online") {
|
|
253
|
+
const d = await (0, control_js_1.call)(t, "GET", `/datasets/${enc(ds.id)}`, { timeoutMs: (0, control_js_1.apiTimeout)(opts) });
|
|
254
|
+
const snaps = (Array.isArray(obj(d.body).snapshots) ? obj(d.body).snapshots : []);
|
|
255
|
+
version = Math.max(0, ...snaps.map((x) => Number(obj(x).version) || 0));
|
|
256
|
+
if (!version)
|
|
257
|
+
version = Number(obj(obj(d.body).dataset).latest_snapshot) || 0;
|
|
258
|
+
}
|
|
259
|
+
const r = await readItems(t, ds.id, version, opts);
|
|
260
|
+
if (r.error) {
|
|
261
|
+
errors.push(r.error);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
return { id: ds.id, name: ds.name, version, items: r.items, skippedNoContent: r.skipped, target: t.name, errors };
|
|
265
|
+
}
|
|
266
|
+
const why = errors.filter((e) => e);
|
|
267
|
+
throw new ExperimentError(`dataset ${JSON.stringify(nameOrId)} not found${why.length ? `: ${why.join("; ")}` : ""}`);
|
|
268
|
+
}
|
|
269
|
+
function evaluatorMeta(e, i) {
|
|
270
|
+
if (typeof e === "function")
|
|
271
|
+
return { name: e.name || `evaluator_${i + 1}`, version: "1", run: e };
|
|
272
|
+
return { name: e.name || `evaluator_${i + 1}`, version: e.version || "1", run: (a) => e.evaluate(a) };
|
|
273
|
+
}
|
|
274
|
+
function normalize(name, r) {
|
|
275
|
+
if (r === null || r === undefined)
|
|
276
|
+
return null;
|
|
277
|
+
if (typeof r === "object") {
|
|
278
|
+
if (r.value === undefined || r.value === null)
|
|
279
|
+
return null;
|
|
280
|
+
return { name: r.name || name, value: r.value, comment: r.comment };
|
|
281
|
+
}
|
|
282
|
+
return { name, value: r };
|
|
283
|
+
}
|
|
284
|
+
async function mapLimit(items, limit, fn) {
|
|
285
|
+
const out = new Array(items.length);
|
|
286
|
+
let next = 0;
|
|
287
|
+
const workers = Array.from({ length: Math.max(1, Math.min(limit, items.length)) }, async () => {
|
|
288
|
+
while (next < items.length) {
|
|
289
|
+
const i = next++;
|
|
290
|
+
out[i] = await fn(items[i], i);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
await Promise.all(workers);
|
|
294
|
+
return out;
|
|
295
|
+
}
|
|
296
|
+
const keyOf = (input) => {
|
|
297
|
+
try {
|
|
298
|
+
return JSON.stringify(input, Object.keys(obj(input)).sort()) ?? String(input);
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
return String(input);
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
function routes(id = "", version = 0) {
|
|
305
|
+
const i = enc(id);
|
|
306
|
+
return {
|
|
307
|
+
datasets: { online: "/datasets", local: "/api/obs/eval/datasets" },
|
|
308
|
+
snapshot: { online: `/datasets/${i}/snapshots`, local: `/api/obs/eval/datasets/${i}/items` },
|
|
309
|
+
experiments: { online: "/experiments", local: "/api/obs/eval/experiments" },
|
|
310
|
+
results: { online: `/experiments/${i}/results`, local: `/api/obs/eval/experiments/${i}/results` },
|
|
311
|
+
finish: { online: `/experiments/${i}/finish`, local: `/api/obs/eval/experiments/${i}/finish` },
|
|
312
|
+
run: { online: `/experiments/${i}`, local: `/api/obs/eval/experiments/${i}` },
|
|
313
|
+
version,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
/** Put literal items where a run can see them: [dataset id, snapshot version, the machine's ids of exactly these items (empty on the org)]. */
|
|
317
|
+
async function uploadLiteral(t, datasetName, items, timeoutMs) {
|
|
318
|
+
let id = "";
|
|
319
|
+
if (t.name === "local") {
|
|
320
|
+
const made = await (0, control_js_1.call)(t, "POST", routes().datasets, { body: { name: datasetName }, timeoutMs });
|
|
321
|
+
if (scopeRefused(made))
|
|
322
|
+
throw new SideSkipped(skipReason(t.name, "dataset create", made));
|
|
323
|
+
if (!made.ok)
|
|
324
|
+
throw new ExperimentError(`local: dataset create: ${describe(made)}`);
|
|
325
|
+
id = idOf(obj(obj(made.body).dataset ?? made.body).id);
|
|
326
|
+
if (!id)
|
|
327
|
+
throw new ExperimentError("local: dataset create answered no id");
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
const found = await findDataset(t, datasetName, { timeoutMs });
|
|
331
|
+
if (found.skipped)
|
|
332
|
+
throw new SideSkipped(found.error);
|
|
333
|
+
if (!found.id)
|
|
334
|
+
throw new ExperimentError(`online: dataset ${JSON.stringify(datasetName)} does not exist on the org (create it on the dashboard first)`);
|
|
335
|
+
id = found.id;
|
|
336
|
+
}
|
|
337
|
+
const r = await (0, control_js_1.call)(t, "POST", routes(id).snapshot, { body: { items }, timeoutMs });
|
|
338
|
+
if (scopeRefused(r))
|
|
339
|
+
throw new SideSkipped(skipReason(t.name, "dataset snapshot", r));
|
|
340
|
+
if (!r.ok)
|
|
341
|
+
throw new ExperimentError(`${t.name}: dataset snapshot: ${describe(r)}`);
|
|
342
|
+
const raw = obj(r.body).item_ids;
|
|
343
|
+
const pinned = t.name === "local" && Array.isArray(raw) ? raw.filter((x) => typeof x === "string" || typeof x === "number").map(String) : [];
|
|
344
|
+
return [id, Number(obj(r.body).version ?? obj(r.body).snapshot_version) || 0, pinned];
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Run `task` over a dataset and score each output.
|
|
348
|
+
*
|
|
349
|
+
* ```ts
|
|
350
|
+
* const res = await runExperiment({
|
|
351
|
+
* name: "prompt-v7",
|
|
352
|
+
* dataset: "golden-qa",
|
|
353
|
+
* task: async (input) => answer(input),
|
|
354
|
+
* evaluators: [function exact_match({ output, expectedOutput }) { return output === expectedOutput; }],
|
|
355
|
+
* failUnder: 0.9,
|
|
356
|
+
* });
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
async function runExperiment(opts) {
|
|
360
|
+
const name = opts.name || opts.task.name || "experiment";
|
|
361
|
+
const result = {
|
|
362
|
+
runId: "",
|
|
363
|
+
runs: {},
|
|
364
|
+
name,
|
|
365
|
+
datasetId: "",
|
|
366
|
+
snapshotVersion: 0,
|
|
367
|
+
items: [],
|
|
368
|
+
summary: {},
|
|
369
|
+
passRate: NaN,
|
|
370
|
+
scored: 0,
|
|
371
|
+
recorded: false,
|
|
372
|
+
skippedNoContent: 0,
|
|
373
|
+
errors: [],
|
|
374
|
+
skippedSides: [],
|
|
375
|
+
failedPosts: 0,
|
|
376
|
+
server: {},
|
|
377
|
+
};
|
|
378
|
+
const evaluators = (opts.evaluators ?? []).map(evaluatorMeta);
|
|
379
|
+
const scorers = evaluators.map((e) => ({ name: e.name, version: e.version }));
|
|
380
|
+
const timeoutMs = (0, control_js_1.apiTimeout)(opts);
|
|
381
|
+
await (0, runtime_js_1.destinationKnown)();
|
|
382
|
+
const sides = (0, control_js_1.targets)("read", opts); // online first, then this machine
|
|
383
|
+
const runs = [];
|
|
384
|
+
let items = [];
|
|
385
|
+
// 1. The dataset on each side.
|
|
386
|
+
const ds = opts.dataset;
|
|
387
|
+
if (Array.isArray(ds)) {
|
|
388
|
+
const literal = literalRows(ds);
|
|
389
|
+
items = literal.map((r) => ({ id: String(r.id), input: r.input, expectedOutput: r.expected, metadata: r.metadata }));
|
|
390
|
+
if (opts.datasetName) {
|
|
391
|
+
// A degraded online uploads nothing: the run stays in-process and says
|
|
392
|
+
// why (contract section 6; as Python).
|
|
393
|
+
if (!sides.length)
|
|
394
|
+
result.errors.push(`dataset ${JSON.stringify(opts.datasetName)} not uploaded, the run is in-process only: ${(0, control_js_1.noTargetReason)()}`);
|
|
395
|
+
for (const t of sides) {
|
|
396
|
+
try {
|
|
397
|
+
const [id, version, pinned] = await uploadLiteral(t, opts.datasetName, literal, timeoutMs);
|
|
398
|
+
runs.push({ target: t, datasetId: id, version, id: "", itemIds: new Map(), pinned });
|
|
399
|
+
}
|
|
400
|
+
catch (err) {
|
|
401
|
+
if (!(err instanceof SideSkipped))
|
|
402
|
+
throw err;
|
|
403
|
+
result.errors.push(err.message);
|
|
404
|
+
result.skippedSides.push(t.name);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
const ref = typeof ds === "string" ? ds : ds.id;
|
|
411
|
+
if (!sides.length)
|
|
412
|
+
throw new ExperimentError(`dataset ${JSON.stringify(ref)}: ${(0, control_js_1.noTargetReason)()}`);
|
|
413
|
+
let skipped = 0;
|
|
414
|
+
for (const t of sides) {
|
|
415
|
+
const found = typeof ds !== "string" && ds.target && ds.target !== t.name && ds.name ? await findDataset(t, ds.name, opts) : await findDataset(t, ref, opts);
|
|
416
|
+
if (found.skipped) {
|
|
417
|
+
result.errors.push(found.error);
|
|
418
|
+
result.skippedSides.push(t.name);
|
|
419
|
+
skipped += 1;
|
|
420
|
+
}
|
|
421
|
+
else if (found.id)
|
|
422
|
+
runs.push({ target: t, datasetId: found.id, version: opts.snapshotVersion ?? opts.datasetVersion ?? 0, id: "", itemIds: new Map(), pinned: [] });
|
|
423
|
+
}
|
|
424
|
+
if (!runs.length && !skipped)
|
|
425
|
+
throw new ExperimentError(`dataset ${JSON.stringify(ref)} not found on any destination`);
|
|
426
|
+
}
|
|
427
|
+
// 2. A run on each side, and its snapshot's items. A side that refuses
|
|
428
|
+
// for want of a scope is skipped (SideSkipped), the others go on.
|
|
429
|
+
const started = [];
|
|
430
|
+
// One idempotency id per runExperiment call (review E-79): every side's
|
|
431
|
+
// create carries it, and a create retried after no answer, a 429 or a 5xx
|
|
432
|
+
// carries it again, so the side answers the run the first attempt opened
|
|
433
|
+
// instead of opening a second one (contract experiments.md 4.1).
|
|
434
|
+
const createId = (0, control_js_1.newId)();
|
|
435
|
+
for (const r of runs) {
|
|
436
|
+
const create = { id: createId, name, dataset_id: r.datasetId, snapshot_version: r.version, metadata: opts.metadata ?? {}, scorers };
|
|
437
|
+
if (r.pinned.length)
|
|
438
|
+
create.item_ids = r.pinned;
|
|
439
|
+
let created = await (0, control_js_1.call)(r.target, "POST", routes().experiments, { body: create, timeoutMs });
|
|
440
|
+
if ((0, control_js_1.retryable)(created))
|
|
441
|
+
created = await (0, control_js_1.call)(r.target, "POST", routes().experiments, { body: create, timeoutMs });
|
|
442
|
+
if (scopeRefused(created)) {
|
|
443
|
+
result.errors.push(skipReason(r.target.name, "experiment create", created));
|
|
444
|
+
result.skippedSides.push(r.target.name);
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
if (!created.ok)
|
|
448
|
+
throw new ExperimentError(`${r.target.name}: experiment create: ${describe(created)}`);
|
|
449
|
+
const body = obj(created.body);
|
|
450
|
+
r.id = idOf(body.id);
|
|
451
|
+
if (!r.id)
|
|
452
|
+
throw new ExperimentError(`${r.target.name}: experiment create answered no id`);
|
|
453
|
+
r.version = Number(body.snapshot_version) || r.version;
|
|
454
|
+
const read = await readItems(r.target, r.datasetId, r.version, opts, r.target.name === "local" ? r.id : "");
|
|
455
|
+
if (read.scopeRefused) {
|
|
456
|
+
result.errors.push(skipReason(r.target.name, "dataset items", read.scopeRefused));
|
|
457
|
+
result.skippedSides.push(r.target.name);
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (read.error)
|
|
461
|
+
throw new ExperimentError(`${r.target.name}: dataset items: ${read.error}`);
|
|
462
|
+
result.runs[r.target.name] = r.id;
|
|
463
|
+
const n = started.length;
|
|
464
|
+
started.push(r);
|
|
465
|
+
if (n === 0 && !Array.isArray(ds)) {
|
|
466
|
+
items = read.items;
|
|
467
|
+
result.skippedNoContent = read.skipped;
|
|
468
|
+
items.forEach((it, i) => r.itemIds.set(i, it.id ?? ""));
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
// Match this side's items to ours: by client id, then by input.
|
|
472
|
+
const byClient = new Map();
|
|
473
|
+
const byInput = new Map();
|
|
474
|
+
for (const it of read.raw) {
|
|
475
|
+
const id = idOf(it.item_id ?? it.id);
|
|
476
|
+
if (it.client_id)
|
|
477
|
+
byClient.set(idOf(it.client_id), id);
|
|
478
|
+
const k = keyOf(it.input);
|
|
479
|
+
byInput.set(k, [...(byInput.get(k) ?? []), id]);
|
|
480
|
+
}
|
|
481
|
+
let unmatched = 0;
|
|
482
|
+
items.forEach((it, i) => {
|
|
483
|
+
const hit = (it.id && byClient.get(it.id)) || byInput.get(keyOf(it.input))?.shift();
|
|
484
|
+
if (hit)
|
|
485
|
+
r.itemIds.set(i, hit);
|
|
486
|
+
else
|
|
487
|
+
unmatched += 1;
|
|
488
|
+
});
|
|
489
|
+
if (unmatched)
|
|
490
|
+
result.errors.push(`${r.target.name}: ${unmatched} item(s) have no match there; their results go to ${started[0].target.name} only`);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
runs.splice(0, runs.length, ...started);
|
|
494
|
+
if (runs.length) {
|
|
495
|
+
result.runId = runs[0].id;
|
|
496
|
+
result.datasetId = runs[0].datasetId;
|
|
497
|
+
result.snapshotVersion = runs[0].version;
|
|
498
|
+
}
|
|
499
|
+
// 3-4. Run, evaluate, post each result.
|
|
500
|
+
const firstRunId = runs[0]?.id ?? "";
|
|
501
|
+
result.items = await mapLimit(items, opts.maxConcurrency ?? 4, async (item, idx) => {
|
|
502
|
+
const ir = { item, traceId: "", output: undefined, error: "", scores: [], postError: "" };
|
|
503
|
+
const itemId = runs[0]?.itemIds.get(idx) || item.id || "";
|
|
504
|
+
const attrs = {};
|
|
505
|
+
if (firstRunId)
|
|
506
|
+
attrs[attrs_js_1.K.experimentRunId] = firstRunId;
|
|
507
|
+
if (itemId)
|
|
508
|
+
attrs[attrs_js_1.K.experimentItemId] = itemId;
|
|
509
|
+
// Each item is its own trace, even when runExperiment runs inside a span.
|
|
510
|
+
await api_1.context
|
|
511
|
+
.with(api_1.trace.deleteSpan(api_1.context.active()), () => (0, spans_js_1.withSpan)(`experiment ${name}`, { kind: spans_js_1.Kind.CHAIN, attributes: attrs }, async (span) => {
|
|
512
|
+
ir.traceId = (0, spans_js_1.getCurrentTraceId)();
|
|
513
|
+
(0, spans_js_1.setInput)(span, item.input);
|
|
514
|
+
try {
|
|
515
|
+
ir.output = await opts.task(item.input, item);
|
|
516
|
+
(0, spans_js_1.setOutput)(span, ir.output);
|
|
517
|
+
}
|
|
518
|
+
catch (err) {
|
|
519
|
+
ir.error = (err instanceof Error ? `${err.name}: ${err.message}` : String(err)).slice(0, 500);
|
|
520
|
+
throw err; // the span records it; the item keeps the error
|
|
521
|
+
}
|
|
522
|
+
}))
|
|
523
|
+
.catch(() => undefined);
|
|
524
|
+
const wire = [];
|
|
525
|
+
if (!ir.error) {
|
|
526
|
+
const args = {
|
|
527
|
+
input: item.input,
|
|
528
|
+
output: ir.output,
|
|
529
|
+
expectedOutput: item.expectedOutput,
|
|
530
|
+
expected: item.expectedOutput,
|
|
531
|
+
metadata: item.metadata ?? {},
|
|
532
|
+
item,
|
|
533
|
+
};
|
|
534
|
+
for (const ev of evaluators) {
|
|
535
|
+
try {
|
|
536
|
+
const raw = await ev.run(args);
|
|
537
|
+
for (const one of Array.isArray(raw) ? raw : [raw]) {
|
|
538
|
+
const sc = normalize(ev.name, one);
|
|
539
|
+
if (!sc)
|
|
540
|
+
continue;
|
|
541
|
+
ir.scores.push(sc);
|
|
542
|
+
wire.push({ name: sc.name, value: sc.value, ...(sc.comment ? { comment: sc.comment } : {}), scorer: { name: ev.name, version: ev.version } });
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
catch (err) {
|
|
546
|
+
ir.scores.push({ name: ev.name, value: "", error: (err instanceof Error ? err.message : String(err)).slice(0, 300) });
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
for (const r of runs) {
|
|
551
|
+
const sideItem = r.itemIds.get(idx);
|
|
552
|
+
if (!sideItem)
|
|
553
|
+
continue;
|
|
554
|
+
const row = { item_id: sideItem, scores: wire };
|
|
555
|
+
if (ir.traceId)
|
|
556
|
+
row.trace_id = ir.traceId;
|
|
557
|
+
if (ir.error)
|
|
558
|
+
row.error = ir.error;
|
|
559
|
+
if (runtime_js_1.runtime.captureContent && ir.output !== undefined)
|
|
560
|
+
row.output = (0, runtime_js_1.truncateContent)((0, runtime_js_1.contentText)(ir.output));
|
|
561
|
+
const resp = await (0, control_js_1.call)(r.target, "POST", routes(r.id).results, { body: { results: [row] }, timeoutMs });
|
|
562
|
+
const rejected = (Array.isArray(obj(resp.body).rejected) ? obj(resp.body).rejected : []);
|
|
563
|
+
const why = !resp.ok ? describe(resp) : rejected.length ? `${String(obj(rejected[0]).error)}: ${String(obj(rejected[0]).message ?? "")}` : "";
|
|
564
|
+
if (why) {
|
|
565
|
+
ir.postError = ir.postError ? `${ir.postError}; ${r.target.name}: ${why}` : `${r.target.name}: ${why}`;
|
|
566
|
+
result.failedPosts += 1;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
return ir;
|
|
570
|
+
});
|
|
571
|
+
// Local summary.
|
|
572
|
+
const sums = {};
|
|
573
|
+
for (const it of result.items) {
|
|
574
|
+
for (const sc of it.scores) {
|
|
575
|
+
if (sc.error)
|
|
576
|
+
continue;
|
|
577
|
+
const v = typeof sc.value === "boolean" ? (sc.value ? 1 : 0) : typeof sc.value === "number" ? sc.value : NaN;
|
|
578
|
+
if (!Number.isFinite(v))
|
|
579
|
+
continue;
|
|
580
|
+
sums[sc.name] ??= { sum: 0, count: 0 };
|
|
581
|
+
sums[sc.name].sum += v;
|
|
582
|
+
sums[sc.name].count += 1;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
for (const [k, v] of Object.entries(sums))
|
|
586
|
+
result.summary[k] = { mean: v.sum / v.count, count: v.count };
|
|
587
|
+
const refused = result.items.filter((i) => i.postError);
|
|
588
|
+
if (refused.length)
|
|
589
|
+
result.errors.push(`${refused.length} result(s) not accepted; first: ${refused[0].postError}`);
|
|
590
|
+
// 5. Finish and read the totals.
|
|
591
|
+
let finished = 0;
|
|
592
|
+
for (const r of runs) {
|
|
593
|
+
const fin = await (0, control_js_1.call)(r.target, "POST", routes(r.id).finish, { body: {}, timeoutMs });
|
|
594
|
+
if (!fin.ok && fin.status !== 409)
|
|
595
|
+
result.errors.push(`${r.target.name}: finish: ${describe(fin)}`);
|
|
596
|
+
else
|
|
597
|
+
finished += 1;
|
|
598
|
+
const got = await (0, control_js_1.call)(r.target, "GET", routes(r.id).run, { timeoutMs });
|
|
599
|
+
if (got.ok)
|
|
600
|
+
result.server[r.target.name] = obj(got.body);
|
|
601
|
+
}
|
|
602
|
+
result.recorded = runs.length > 0 && finished === runs.length;
|
|
603
|
+
const totals = experimentTotals(result.items, runs.map((r) => result.server[r.target.name]).filter((b) => !!b), opts.failUnder);
|
|
604
|
+
result.passRate = totals.passRate;
|
|
605
|
+
result.scored = totals.scored;
|
|
606
|
+
const strict = opts.strict ?? true;
|
|
607
|
+
const gate = experimentGate(result, opts.failUnder, strict);
|
|
608
|
+
if (opts.failUnder !== undefined && strict && gate.reasons.length) {
|
|
609
|
+
for (const r of gate.reasons)
|
|
610
|
+
result.errors.push(`gate: ${r}`);
|
|
611
|
+
if (typeof process !== "undefined")
|
|
612
|
+
process.exitCode = 1;
|
|
613
|
+
throw new ExperimentGateFailed(result, opts.failUnder, gate.reasons);
|
|
614
|
+
}
|
|
615
|
+
if (opts.failUnder !== undefined && result.scored === 0) {
|
|
616
|
+
const msg = "no scores: no item was scored (no evaluator ran), so failUnder was not applied (strict: false)";
|
|
617
|
+
result.errors.push(msg);
|
|
618
|
+
try {
|
|
619
|
+
console.warn(`@superbased/sdk: experiment ${JSON.stringify(name)}: ${msg}`);
|
|
620
|
+
}
|
|
621
|
+
catch {
|
|
622
|
+
// ignore
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
if (opts.failUnder !== undefined && gate.failed) {
|
|
626
|
+
if (typeof process !== "undefined")
|
|
627
|
+
process.exitCode = 1;
|
|
628
|
+
throw new ExperimentGateFailed(result, opts.failUnder);
|
|
629
|
+
}
|
|
630
|
+
return result;
|
|
631
|
+
}
|
|
632
|
+
const FAIL_LABELS = new Set(["fail", "false", "no", "bad"]);
|
|
633
|
+
/** B7's pass rule (contract 4.2): boolean true, a number >= 0.5, a label not fail/false/no/bad. */
|
|
634
|
+
function scorePasses(v) {
|
|
635
|
+
if (typeof v === "boolean")
|
|
636
|
+
return v;
|
|
637
|
+
if (typeof v === "number")
|
|
638
|
+
return v >= 0.5;
|
|
639
|
+
return !FAIL_LABELS.has(v.trim().toLowerCase());
|
|
640
|
+
}
|
|
641
|
+
/** Items that count toward a pass rate: a task error, or a score an evaluator returned (one that threw is no score). */
|
|
642
|
+
function scoredItems(items) {
|
|
643
|
+
return items.filter((i) => i.error || i.scores.some((sc) => !sc.error));
|
|
644
|
+
}
|
|
645
|
+
const isCount = (v) => typeof v === "number" && Number.isInteger(v) && v >= 0;
|
|
646
|
+
/**
|
|
647
|
+
* A run's pass rate and scored count (contract experiments.md 4.2), as
|
|
648
|
+
* Python's ExperimentResult computes them: the first side whose totals say
|
|
649
|
+
* something was scored, else the local tally over scored items (an item
|
|
650
|
+
* passes with no task error and every returned score passing). A side's
|
|
651
|
+
* `pass_rate` with `total` 0 (an older server's 0 for "nothing scored") is
|
|
652
|
+
* not a rate. NaN when nothing was scored. `gateFailed`: the rate is below
|
|
653
|
+
* `failUnder`; never for a run that scored nothing.
|
|
654
|
+
*/
|
|
655
|
+
function experimentTotals(items, sides = [], failUnder) {
|
|
656
|
+
const local = scoredItems(items);
|
|
657
|
+
let scored = local.length;
|
|
658
|
+
for (const b of sides) {
|
|
659
|
+
if (isCount(b.total) && (b.total > 0 || !local.length)) {
|
|
660
|
+
scored = b.total;
|
|
661
|
+
break;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
for (const b of sides) {
|
|
665
|
+
if (typeof b.pass_rate !== "number")
|
|
666
|
+
continue;
|
|
667
|
+
if (isCount(b.total) && b.total === 0)
|
|
668
|
+
continue;
|
|
669
|
+
return { scored, passRate: b.pass_rate, gateFailed: gate(scored, b.pass_rate, failUnder) };
|
|
670
|
+
}
|
|
671
|
+
const passed = local.filter((i) => !i.error && i.scores.every((sc) => sc.error || scorePasses(sc.value))).length;
|
|
672
|
+
const passRate = local.length ? passed / local.length : NaN;
|
|
673
|
+
return { scored, passRate, gateFailed: gate(scored, passRate, failUnder) };
|
|
674
|
+
}
|
|
675
|
+
function gate(scored, passRate, failUnder) {
|
|
676
|
+
return failUnder !== undefined && scored > 0 && !(passRate >= failUnder);
|
|
677
|
+
}
|
|
678
|
+
//# sourceMappingURL=experiments.js.map
|