@smthrs/evals 0.0.0-stage → 1.0.0-rc.4
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/CHANGELOG.md +142 -0
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/dist/cjs/Baseline.d.ts +118 -0
- package/dist/cjs/Baseline.d.ts.map +1 -0
- package/dist/cjs/Baseline.js +158 -0
- package/dist/cjs/Baseline.js.map +7 -0
- package/dist/cjs/CaseExecutor.d.ts +85 -0
- package/dist/cjs/CaseExecutor.d.ts.map +1 -0
- package/dist/cjs/CaseExecutor.js +59 -0
- package/dist/cjs/CaseExecutor.js.map +7 -0
- package/dist/cjs/EvalError.d.ts +64 -0
- package/dist/cjs/EvalError.d.ts.map +1 -0
- package/dist/cjs/EvalError.js +54 -0
- package/dist/cjs/EvalError.js.map +7 -0
- package/dist/cjs/Gate.d.ts +55 -0
- package/dist/cjs/Gate.d.ts.map +1 -0
- package/dist/cjs/Gate.js +88 -0
- package/dist/cjs/Gate.js.map +7 -0
- package/dist/cjs/Regression.d.ts +116 -0
- package/dist/cjs/Regression.d.ts.map +1 -0
- package/dist/cjs/Regression.js +191 -0
- package/dist/cjs/Regression.js.map +7 -0
- package/dist/cjs/Report.d.ts +114 -0
- package/dist/cjs/Report.d.ts.map +1 -0
- package/dist/cjs/Report.js +161 -0
- package/dist/cjs/Report.js.map +7 -0
- package/dist/cjs/Runner.d.ts +229 -0
- package/dist/cjs/Runner.d.ts.map +1 -0
- package/dist/cjs/Runner.js +430 -0
- package/dist/cjs/Runner.js.map +7 -0
- package/dist/cjs/Suite.d.ts +139 -0
- package/dist/cjs/Suite.d.ts.map +1 -0
- package/dist/cjs/Suite.js +221 -0
- package/dist/cjs/Suite.js.map +7 -0
- package/dist/cjs/Trials.d.ts +93 -0
- package/dist/cjs/Trials.d.ts.map +1 -0
- package/dist/cjs/Trials.js +82 -0
- package/dist/cjs/Trials.js.map +7 -0
- package/dist/cjs/index.d.ts +76 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +51 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/internal/CanonicalJson.d.ts +44 -0
- package/dist/cjs/internal/CanonicalJson.d.ts.map +1 -0
- package/dist/cjs/internal/CanonicalJson.js +114 -0
- package/dist/cjs/internal/CanonicalJson.js.map +7 -0
- package/dist/cjs/internal/controlCharacters.d.ts +44 -0
- package/dist/cjs/internal/controlCharacters.d.ts.map +1 -0
- package/dist/cjs/internal/controlCharacters.js +42 -0
- package/dist/cjs/internal/controlCharacters.js.map +7 -0
- package/dist/cjs/internal/scorerLabel.d.ts +33 -0
- package/dist/cjs/internal/scorerLabel.d.ts.map +1 -0
- package/dist/cjs/internal/scorerLabel.js +28 -0
- package/dist/cjs/internal/scorerLabel.js.map +7 -0
- package/dist/cjs/internal/tupleKey.d.ts +20 -0
- package/dist/cjs/internal/tupleKey.d.ts.map +1 -0
- package/dist/cjs/internal/tupleKey.js +25 -0
- package/dist/cjs/internal/tupleKey.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/Baseline.d.ts +118 -0
- package/dist/esm/Baseline.d.ts.map +1 -0
- package/dist/esm/Baseline.js +203 -0
- package/dist/esm/Baseline.js.map +1 -0
- package/dist/esm/CaseExecutor.d.ts +85 -0
- package/dist/esm/CaseExecutor.d.ts.map +1 -0
- package/dist/esm/CaseExecutor.js +60 -0
- package/dist/esm/CaseExecutor.js.map +1 -0
- package/dist/esm/EvalError.d.ts +64 -0
- package/dist/esm/EvalError.d.ts.map +1 -0
- package/dist/esm/EvalError.js +65 -0
- package/dist/esm/EvalError.js.map +1 -0
- package/dist/esm/Gate.d.ts +55 -0
- package/dist/esm/Gate.d.ts.map +1 -0
- package/dist/esm/Gate.js +90 -0
- package/dist/esm/Gate.js.map +1 -0
- package/dist/esm/Regression.d.ts +116 -0
- package/dist/esm/Regression.d.ts.map +1 -0
- package/dist/esm/Regression.js +195 -0
- package/dist/esm/Regression.js.map +1 -0
- package/dist/esm/Report.d.ts +114 -0
- package/dist/esm/Report.d.ts.map +1 -0
- package/dist/esm/Report.js +188 -0
- package/dist/esm/Report.js.map +1 -0
- package/dist/esm/Runner.d.ts +229 -0
- package/dist/esm/Runner.d.ts.map +1 -0
- package/dist/esm/Runner.js +430 -0
- package/dist/esm/Runner.js.map +1 -0
- package/dist/esm/Suite.d.ts +139 -0
- package/dist/esm/Suite.d.ts.map +1 -0
- package/dist/esm/Suite.js +255 -0
- package/dist/esm/Suite.js.map +1 -0
- package/dist/esm/Trials.d.ts +93 -0
- package/dist/esm/Trials.d.ts.map +1 -0
- package/dist/esm/Trials.js +116 -0
- package/dist/esm/Trials.js.map +1 -0
- package/dist/esm/index.d.ts +76 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +76 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal/CanonicalJson.d.ts +44 -0
- package/dist/esm/internal/CanonicalJson.d.ts.map +1 -0
- package/dist/esm/internal/CanonicalJson.js +202 -0
- package/dist/esm/internal/CanonicalJson.js.map +1 -0
- package/dist/esm/internal/controlCharacters.d.ts +44 -0
- package/dist/esm/internal/controlCharacters.d.ts.map +1 -0
- package/dist/esm/internal/controlCharacters.js +65 -0
- package/dist/esm/internal/controlCharacters.js.map +1 -0
- package/dist/esm/internal/scorerLabel.d.ts +33 -0
- package/dist/esm/internal/scorerLabel.d.ts.map +1 -0
- package/dist/esm/internal/scorerLabel.js +33 -0
- package/dist/esm/internal/scorerLabel.js.map +1 -0
- package/dist/esm/internal/tupleKey.d.ts +20 -0
- package/dist/esm/internal/tupleKey.d.ts.map +1 -0
- package/dist/esm/internal/tupleKey.js +20 -0
- package/dist/esm/internal/tupleKey.js.map +1 -0
- package/docs/README.md +130 -0
- package/docs/api.md +626 -0
- package/docs/concepts/determinism.md +64 -0
- package/docs/concepts/fixed-suites.md +71 -0
- package/docs/concepts/step-keys.md +60 -0
- package/docs/guides/author-a-suite.md +115 -0
- package/docs/guides/gate-a-run-in-ci.md +129 -0
- package/docs/guides/maintain-a-baseline.md +91 -0
- package/docs/guides/run-a-suite.md +150 -0
- package/docs/installation.md +46 -0
- package/docs/quickstart.md +238 -0
- package/docs/troubleshooting.md +148 -0
- package/package.json +198 -3
- package/src/Baseline.ts +259 -0
- package/src/CaseExecutor.ts +104 -0
- package/src/EvalError.ts +74 -0
- package/src/Gate.ts +128 -0
- package/src/Regression.ts +314 -0
- package/src/Report.ts +268 -0
- package/src/Runner.ts +759 -0
- package/src/Suite.ts +358 -0
- package/src/Trials.ts +154 -0
- package/src/index.ts +76 -0
- package/src/internal/CanonicalJson.ts +234 -0
- package/src/internal/controlCharacters.ts +66 -0
- package/src/internal/scorerLabel.ts +35 -0
- package/src/internal/tupleKey.ts +20 -0
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic fixed-suite execution and bound scorer evaluation.
|
|
3
|
+
*
|
|
4
|
+
* A run executes every case through the injected {@link CaseExecutor}, then
|
|
5
|
+
* grades each execution with the scorers bound to the flow it executed. The
|
|
6
|
+
* timestamp and the run identity come from the caller, so two runs of the same
|
|
7
|
+
* suite over the same inputs produce byte-identical observations.
|
|
8
|
+
*
|
|
9
|
+
* @since 0.1.0
|
|
10
|
+
*/
|
|
11
|
+
import * as ScorerRunner from "@smthrs/scorers/Runner";
|
|
12
|
+
import * as Sampling from "@smthrs/scorers/Sampling";
|
|
13
|
+
import * as Scorer from "@smthrs/scorers/Scorer";
|
|
14
|
+
import * as Cause from "effect/Cause";
|
|
15
|
+
import * as Context from "effect/Context";
|
|
16
|
+
import * as Effect from "effect/Effect";
|
|
17
|
+
import * as Layer from "effect/Layer";
|
|
18
|
+
import { CaseExecutor } from "./CaseExecutor.js";
|
|
19
|
+
import { EvalError } from "./EvalError.js";
|
|
20
|
+
import { flattenControlCharacters } from "./internal/controlCharacters.js";
|
|
21
|
+
import { scorerLabel } from "./internal/scorerLabel.js";
|
|
22
|
+
import { tupleKey } from "./internal/tupleKey.js";
|
|
23
|
+
import * as Trials from "./Trials.js";
|
|
24
|
+
/**
|
|
25
|
+
* Injectable batch-runner service used when a caller wants a reusable adapter.
|
|
26
|
+
*
|
|
27
|
+
* {@link run} does not require it: a run scores with `options.scorer` when one
|
|
28
|
+
* is passed, otherwise with this service when one is provided, otherwise
|
|
29
|
+
* in process through {@link makeInline}.
|
|
30
|
+
*
|
|
31
|
+
* @category services
|
|
32
|
+
* @since 0.1.0
|
|
33
|
+
*/
|
|
34
|
+
export class Runner extends Context.Service()("flows/evals/Runner") {
|
|
35
|
+
}
|
|
36
|
+
/** The longest cause summary a public reason carries. */
|
|
37
|
+
const maxReasonLength = 2048;
|
|
38
|
+
const boundedReason = (reason) => reason.length <= maxReasonLength ? reason : `${reason.slice(0, maxReasonLength)}[truncated]`;
|
|
39
|
+
const scorerKeyOf = (binding) => binding.scorer.scorerKey;
|
|
40
|
+
// A scorer is a flow and every flow carries a name, so a run always has one to
|
|
41
|
+
// report. A stored observation may still lack it, which is why `scorerLabel`
|
|
42
|
+
// and the `Observation` field both keep the absent case.
|
|
43
|
+
const scorerNameOf = (binding) => binding.scorer.name;
|
|
44
|
+
const label = (binding) => scorerLabel({ scorer: scorerKeyOf(binding), scorerName: scorerNameOf(binding) });
|
|
45
|
+
const inconclusive = (request, reason, at) => {
|
|
46
|
+
return {
|
|
47
|
+
case: request.case,
|
|
48
|
+
scorer: scorerKeyOf(request.binding),
|
|
49
|
+
scorerName: scorerNameOf(request.binding),
|
|
50
|
+
stepKey: request.stepKey,
|
|
51
|
+
kind: "inconclusive",
|
|
52
|
+
reason,
|
|
53
|
+
at
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
// A case failure always locates itself. An executor that named the offending
|
|
57
|
+
// value keeps its own path; one that named nothing still gets the case, because
|
|
58
|
+
// a failure with no path leaves a caller nothing to branch on but prose.
|
|
59
|
+
const casePath = (name) => `cases['${name}']`;
|
|
60
|
+
// The executor boundary handles every non-interruption cause, not only typed
|
|
61
|
+
// failures: an executor that wraps a parser in `Effect.sync` dies with a
|
|
62
|
+
// defect, and one case's defect must not abort its siblings or the report.
|
|
63
|
+
// The wrapped message is flattened because a gate prints it to a CI log, where
|
|
64
|
+
// a newline from the target would let it forge a workflow command; the
|
|
65
|
+
// original message survives untouched on `cause`.
|
|
66
|
+
const caseError = (suiteCase, cause) => {
|
|
67
|
+
const reason = Cause.squash(cause);
|
|
68
|
+
return reason instanceof EvalError
|
|
69
|
+
? new EvalError({
|
|
70
|
+
code: reason.code,
|
|
71
|
+
message: flattenControlCharacters(`Target failed for case '${suiteCase.name}': ${reason.message}`),
|
|
72
|
+
path: reason.path ?? casePath(suiteCase.name),
|
|
73
|
+
cause: reason
|
|
74
|
+
})
|
|
75
|
+
: new EvalError({
|
|
76
|
+
code: "executor",
|
|
77
|
+
message: flattenControlCharacters(`Target failed for case '${suiteCase.name}': ${String(reason)}`),
|
|
78
|
+
path: casePath(suiteCase.name),
|
|
79
|
+
cause: reason
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
// A step key is a runtime value the target returns, so unlike a suite name it
|
|
83
|
+
// is flattened rather than rejected: it reaches baselines, reports and the
|
|
84
|
+
// gate summary, and a control character in any of them corrupts a CI log line.
|
|
85
|
+
const runCase = (executor, suiteCase) => Effect.suspend(() => executor.run(structuredClone(suiteCase))).pipe(Effect.map((execution) => ({
|
|
86
|
+
case: suiteCase.name,
|
|
87
|
+
execution: { ...execution, stepKey: flattenControlCharacters(execution.stepKey) },
|
|
88
|
+
observations: []
|
|
89
|
+
})), Effect.catchCause((cause) => Cause.hasInterrupts(cause)
|
|
90
|
+
? Effect.interrupt
|
|
91
|
+
: Effect.succeed({ case: suiteCase.name, error: caseError(suiteCase, cause), observations: [] })), Effect.withSpan("Eval.case", { attributes: { case: suiteCase.name } }));
|
|
92
|
+
// A declared case value is ground truth even when it is `null`, `false`, `0`
|
|
93
|
+
// or the empty string; only an absent `expected` defers to the binding.
|
|
94
|
+
const groundTruthFor = (suiteCase, binding) => {
|
|
95
|
+
const groundTruth = suiteCase.expected !== undefined ? suiteCase.expected : binding.groundTruth;
|
|
96
|
+
return groundTruth === undefined ? {} : { groundTruth: structuredClone(groundTruth) };
|
|
97
|
+
};
|
|
98
|
+
const requestFor = (suiteCase, execution, binding) => ({
|
|
99
|
+
case: suiteCase.name,
|
|
100
|
+
stepKey: execution.stepKey,
|
|
101
|
+
binding: {
|
|
102
|
+
...structuredClone({
|
|
103
|
+
sampling: binding.sampling,
|
|
104
|
+
...(binding.groundTruth === undefined ? {} : { groundTruth: binding.groundTruth }),
|
|
105
|
+
...(binding.context === undefined ? {} : { context: binding.context })
|
|
106
|
+
}),
|
|
107
|
+
scorer: binding.scorer,
|
|
108
|
+
appliesTo: binding.appliesTo
|
|
109
|
+
},
|
|
110
|
+
input: {
|
|
111
|
+
input: structuredClone(suiteCase.input),
|
|
112
|
+
output: execution.output,
|
|
113
|
+
...(groundTruthFor(suiteCase, binding)),
|
|
114
|
+
...(binding.context === undefined ? {} : { context: structuredClone(binding.context) }),
|
|
115
|
+
latencyMs: execution.latencyMs
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
const observationFor = (request, result, at) => {
|
|
119
|
+
const source = result;
|
|
120
|
+
const kind = source.kind;
|
|
121
|
+
if (kind !== "score" && kind !== "inconclusive") {
|
|
122
|
+
return inconclusive(request, boundedReason(`Scorer ${label(request.binding)} returned an unusable observation kind '${String(kind)}'`), at);
|
|
123
|
+
}
|
|
124
|
+
const reason = source.reason;
|
|
125
|
+
if (kind === "inconclusive") {
|
|
126
|
+
return inconclusive(request, typeof reason === "string" && reason.length > 0
|
|
127
|
+
? boundedReason(reason)
|
|
128
|
+
: boundedReason(`Scorer ${label(request.binding)} returned an inconclusive observation with no reason`), at);
|
|
129
|
+
}
|
|
130
|
+
const score = source.score;
|
|
131
|
+
if (typeof score !== "number" || !Number.isFinite(score) || score < 0 || score > 1) {
|
|
132
|
+
return inconclusive(request, boundedReason(`Scorer ${label(request.binding)} returned a score outside [0, 1]: ${String(score)}`), at);
|
|
133
|
+
}
|
|
134
|
+
const meta = source.meta;
|
|
135
|
+
return {
|
|
136
|
+
case: request.case,
|
|
137
|
+
scorer: scorerKeyOf(request.binding),
|
|
138
|
+
scorerName: scorerNameOf(request.binding),
|
|
139
|
+
stepKey: request.stepKey,
|
|
140
|
+
kind: "score",
|
|
141
|
+
score,
|
|
142
|
+
...(typeof reason === "string" ? { reason: boundedReason(reason) } : {}),
|
|
143
|
+
...(meta === undefined ? {} : { meta }),
|
|
144
|
+
at
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
const executeInline = (job) => job.score.pipe(Effect.flatMap(Scorer.validate), Effect.map((result) => ({
|
|
148
|
+
...job.observation,
|
|
149
|
+
kind: "score",
|
|
150
|
+
score: result.score,
|
|
151
|
+
...(result.reason === undefined ? {} : { reason: result.reason }),
|
|
152
|
+
...(result.meta === undefined ? {} : { meta: result.meta }),
|
|
153
|
+
at: job.at
|
|
154
|
+
})), Effect.catchCause((cause) => Cause.hasInterrupts(cause)
|
|
155
|
+
? Effect.interrupt
|
|
156
|
+
: Effect.succeed(ScorerRunner.inconclusive(job, cause))));
|
|
157
|
+
/**
|
|
158
|
+
* Builds the in-process batch runner a run scores with by default.
|
|
159
|
+
*
|
|
160
|
+
* Each job's scorer runs in the current process, its result is checked by
|
|
161
|
+
* `@smthrs/scorers`' own `Scorer.validate`, and a scorer that fails becomes an
|
|
162
|
+
* inconclusive observation naming its cause rather than failing the run. It
|
|
163
|
+
* honours the {@link ScoreBatchRunner} protocol, so it is also the reference
|
|
164
|
+
* a custom adapter can be compared against.
|
|
165
|
+
*
|
|
166
|
+
* @category constructors
|
|
167
|
+
* @since 0.1.0
|
|
168
|
+
*/
|
|
169
|
+
export const makeInline = () => {
|
|
170
|
+
const runBatchCorrelated = (jobs, options) => Effect.forEach(jobs, (job) => executeInline(job).pipe(Effect.map((observation) => ({ identity: job.identity, observation }))), { concurrency: options?.concurrency ?? 1 });
|
|
171
|
+
return {
|
|
172
|
+
runBatchCorrelated,
|
|
173
|
+
runBatch: (jobs, options) => runBatchCorrelated(jobs, options).pipe(Effect.map((results) => results.map((result) => result.observation)))
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* Provides the in-process batch runner built by {@link makeInline}.
|
|
178
|
+
*
|
|
179
|
+
* @category layers
|
|
180
|
+
* @since 0.1.0
|
|
181
|
+
*/
|
|
182
|
+
export const layerInline = Layer.succeed(Runner)(makeInline());
|
|
183
|
+
const buildJobs = (cases, suite, { at, runId, sampleId, concurrency }) => Effect.gen(function* () {
|
|
184
|
+
// `cases` is the result of one `Effect.forEach` over `suite.cases`, so the
|
|
185
|
+
// two arrays are index-aligned by construction; nothing here has to look a
|
|
186
|
+
// case back up by name.
|
|
187
|
+
const candidates = cases.flatMap((caseResult, index) => {
|
|
188
|
+
const execution = caseResult.execution;
|
|
189
|
+
if (execution === undefined)
|
|
190
|
+
return [];
|
|
191
|
+
const suiteCase = suite.cases[index];
|
|
192
|
+
return suite.bindings.flatMap((binding, bindingIndex) => binding.appliesTo === execution.target
|
|
193
|
+
? [{ request: requestFor(suiteCase, execution, binding), bindingIndex }]
|
|
194
|
+
: []);
|
|
195
|
+
});
|
|
196
|
+
const sampled = yield* Effect.forEach(candidates, ({ request, bindingIndex }) => Sampling.decide(request.binding.sampling, request.stepKey, scorerKeyOf(request.binding)).pipe(Effect.map((selected) => selected ? request : undefined), Effect.mapError((cause) => new EvalError({
|
|
197
|
+
code: "invalid_suite",
|
|
198
|
+
message: `Invalid sampling policy for scorer ${label(request.binding)}`,
|
|
199
|
+
path: `bindings[${bindingIndex}].sampling`,
|
|
200
|
+
cause
|
|
201
|
+
}))), { concurrency });
|
|
202
|
+
const requests = sampled.filter((request) => request !== undefined);
|
|
203
|
+
const jobs = requests.map((request, index) => ({
|
|
204
|
+
identity: tupleKey(suite.name, runId, sampleId, request.case, request.stepKey, scorerKeyOf(request.binding), `${index}`),
|
|
205
|
+
observation: { targetStepKey: request.stepKey, scorerKey: scorerKeyOf(request.binding) },
|
|
206
|
+
score: Effect.suspend(() => request.binding.scorer.score(request.input)),
|
|
207
|
+
at: Date.parse(at)
|
|
208
|
+
}));
|
|
209
|
+
return { requests, jobs };
|
|
210
|
+
});
|
|
211
|
+
const batchFallback = (jobs, cause) => Cause.hasInterrupts(cause)
|
|
212
|
+
? Effect.interrupt
|
|
213
|
+
: Effect.succeed(jobs.map((job) => ScorerRunner.inconclusive(job, cause)));
|
|
214
|
+
const scoreCorrelated = (jobs, scorer, runBatchCorrelated, { concurrency }) => Effect.gen(function* () {
|
|
215
|
+
const batchResults = yield* Effect.suspend(() => runBatchCorrelated.call(scorer, jobs, { concurrency })).pipe(Effect.catchCause((cause) => batchFallback(jobs, cause).pipe(Effect.map((observations) => observations.map((observation, index) => ({
|
|
216
|
+
identity: jobs[index].identity,
|
|
217
|
+
observation
|
|
218
|
+
}))))));
|
|
219
|
+
if (batchResults.length !== jobs.length) {
|
|
220
|
+
return yield* Effect.fail(new EvalError({
|
|
221
|
+
code: "scorer_protocol",
|
|
222
|
+
message: `Correlated scorer batch returned ${batchResults.length} results for ${jobs.length} jobs; a batch runner must return exactly one result per job identity`,
|
|
223
|
+
path: "runBatchCorrelated"
|
|
224
|
+
}));
|
|
225
|
+
}
|
|
226
|
+
const jobIndexByIdentity = new Map(jobs.map((job, index) => [job.identity, index]));
|
|
227
|
+
const byIdentity = new Map();
|
|
228
|
+
let unknown;
|
|
229
|
+
for (const [index, result] of batchResults.entries()) {
|
|
230
|
+
const jobIndex = jobIndexByIdentity.get(result.identity);
|
|
231
|
+
if (jobIndex !== undefined && byIdentity.has(result.identity)) {
|
|
232
|
+
return yield* Effect.fail(new EvalError({
|
|
233
|
+
code: "scorer_protocol",
|
|
234
|
+
message: `Correlated scorer batch returned duplicate identity '${result.identity}' for job ${jobIndex} at result index ${index}`,
|
|
235
|
+
path: `runBatchCorrelated[${index}]`
|
|
236
|
+
}));
|
|
237
|
+
}
|
|
238
|
+
if (jobIndex === undefined)
|
|
239
|
+
unknown = { identity: result.identity, index };
|
|
240
|
+
byIdentity.set(result.identity, result.observation);
|
|
241
|
+
}
|
|
242
|
+
if (unknown !== undefined) {
|
|
243
|
+
const missingJobIndex = jobs.findIndex((job) => !byIdentity.has(job.identity));
|
|
244
|
+
const missingIdentity = jobs[missingJobIndex].identity;
|
|
245
|
+
return yield* Effect.fail(new EvalError({
|
|
246
|
+
code: "scorer_protocol",
|
|
247
|
+
message: `Correlated scorer batch returned unknown identity '${unknown.identity}' at result index ${unknown.index}; job ${missingJobIndex} identity '${missingIdentity}' is absent`,
|
|
248
|
+
path: `runBatchCorrelated[${unknown.index}]`
|
|
249
|
+
}));
|
|
250
|
+
}
|
|
251
|
+
return jobs.map((job) => byIdentity.get(job.identity));
|
|
252
|
+
});
|
|
253
|
+
const scoreOrderOnly = (jobs, requests, scorer, { concurrency }) => Effect.gen(function* () {
|
|
254
|
+
const pairIndexes = new Map();
|
|
255
|
+
for (const [index, job] of jobs.entries()) {
|
|
256
|
+
const pair = tupleKey(job.observation.targetStepKey, job.observation.scorerKey);
|
|
257
|
+
const previous = pairIndexes.get(pair);
|
|
258
|
+
if (previous !== undefined) {
|
|
259
|
+
const first = requests[previous];
|
|
260
|
+
const second = requests[index];
|
|
261
|
+
return yield* Effect.fail(new EvalError({
|
|
262
|
+
code: "ambiguous_score_job",
|
|
263
|
+
message: `Cannot attribute score jobs for cases '${first.case}' and '${second.case}': both use step key '${job.observation.targetStepKey}' and scorer ${label(second.binding)}. Give each case its own step key, or provide a batch runner that implements runBatchCorrelated`,
|
|
264
|
+
path: "runBatch"
|
|
265
|
+
}));
|
|
266
|
+
}
|
|
267
|
+
pairIndexes.set(pair, index);
|
|
268
|
+
}
|
|
269
|
+
const results = yield* Effect.suspend(() => scorer.runBatch(jobs, { concurrency })).pipe(Effect.catchCause((cause) => batchFallback(jobs, cause)));
|
|
270
|
+
if (results.length !== jobs.length) {
|
|
271
|
+
return yield* Effect.fail(new EvalError({
|
|
272
|
+
code: "scorer_protocol",
|
|
273
|
+
message: `Scorer batch returned ${results.length} observations for ${jobs.length} jobs; a batch runner must return exactly one observation per job, in order`,
|
|
274
|
+
path: "runBatch"
|
|
275
|
+
}));
|
|
276
|
+
}
|
|
277
|
+
return results;
|
|
278
|
+
});
|
|
279
|
+
const score = (cases, suite, scorer, context) => Effect.gen(function* () {
|
|
280
|
+
const { requests, jobs } = yield* buildJobs(cases, suite, context);
|
|
281
|
+
if (requests.length === 0)
|
|
282
|
+
return cases;
|
|
283
|
+
const runBatchCorrelated = scorer.runBatchCorrelated;
|
|
284
|
+
const protocol = runBatchCorrelated === undefined ? "runBatch" : "runBatchCorrelated";
|
|
285
|
+
const results = yield* runBatchCorrelated === undefined
|
|
286
|
+
? scoreOrderOnly(jobs, requests, scorer, context)
|
|
287
|
+
: scoreCorrelated(jobs, scorer, runBatchCorrelated, context);
|
|
288
|
+
const { at } = context;
|
|
289
|
+
const observations = [];
|
|
290
|
+
for (const [index, request] of requests.entries()) {
|
|
291
|
+
const result = results[index];
|
|
292
|
+
if (result.targetStepKey !== request.stepKey || result.scorerKey !== scorerKeyOf(request.binding)) {
|
|
293
|
+
return yield* Effect.fail(new EvalError({
|
|
294
|
+
code: "scorer_protocol",
|
|
295
|
+
message: protocol === "runBatchCorrelated"
|
|
296
|
+
? `Correlated scorer batch identity '${jobs[index].identity}' returned step '${result.targetStepKey}' and scorer '${result.scorerKey}' where job ${index} asked for scorer '${scorerKeyOf(request.binding)}' at step '${request.stepKey}'`
|
|
297
|
+
: `Scorer batch returned an observation for '${result.scorerKey}' at step '${result.targetStepKey}' where job ${index} asked for '${scorerKeyOf(request.binding)}' at step '${request.stepKey}'; results must stay aligned with their jobs`,
|
|
298
|
+
path: `${protocol}[${index}]`
|
|
299
|
+
}));
|
|
300
|
+
}
|
|
301
|
+
observations.push(observationFor(request, result, at));
|
|
302
|
+
}
|
|
303
|
+
const byCase = new Map();
|
|
304
|
+
for (const observation of observations) {
|
|
305
|
+
const bucket = byCase.get(observation.case);
|
|
306
|
+
if (bucket === undefined)
|
|
307
|
+
byCase.set(observation.case, [observation]);
|
|
308
|
+
else
|
|
309
|
+
bucket.push(observation);
|
|
310
|
+
}
|
|
311
|
+
return cases.map((caseResult) => ({
|
|
312
|
+
...caseResult,
|
|
313
|
+
observations: byCase.get(caseResult.case) ?? []
|
|
314
|
+
}));
|
|
315
|
+
});
|
|
316
|
+
/**
|
|
317
|
+
* Runs a fixed suite with bounded execution and declaration-order results.
|
|
318
|
+
*
|
|
319
|
+
* Cases run through the provided {@link CaseExecutor} at the suite's
|
|
320
|
+
* concurrency and are returned in declaration order. Each executor receives
|
|
321
|
+
* its own mutable case copy. Each scorer receives independent copies of the
|
|
322
|
+
* original case input, expected value, and binding data. Every execution is then
|
|
323
|
+
* graded by the bindings whose `appliesTo` is the flow the execution reports as
|
|
324
|
+
* its target, matched by reference identity, so a binding attached to another
|
|
325
|
+
* flow contributes no observations.
|
|
326
|
+
*
|
|
327
|
+
* Scoring goes through `options.scorer` when the caller passes one, the
|
|
328
|
+
* {@link Runner} service when one is provided, and {@link makeInline}
|
|
329
|
+
* otherwise. A case whose target failed keeps its typed error, locates itself
|
|
330
|
+
* with the executor's own `path` or with `cases['<name>']`, and produces no
|
|
331
|
+
* observations; a scorer that failed produces an inconclusive observation. The
|
|
332
|
+
* run itself fails only with `invalid_run_options` for a non-canonical run
|
|
333
|
+
* identity or timestamp, `invalid_suite` for an unusable sampling policy,
|
|
334
|
+
* `ambiguous_score_job` when an order-only runner cannot distinguish two jobs,
|
|
335
|
+
* or `scorer_protocol` for a batch runner that broke the
|
|
336
|
+
* {@link ScoreBatchRunner} contract.
|
|
337
|
+
*
|
|
338
|
+
* @category constructors
|
|
339
|
+
* @since 0.1.0
|
|
340
|
+
*/
|
|
341
|
+
export const run = (suite, options) => Effect.gen(function* () {
|
|
342
|
+
const executor = yield* CaseExecutor;
|
|
343
|
+
const injected = yield* Effect.serviceOption(Runner);
|
|
344
|
+
const scorer = options.scorer ?? (injected._tag === "Some" ? injected.value : makeInline());
|
|
345
|
+
const atMillis = Date.parse(options.at);
|
|
346
|
+
if (options.runId.trim().length === 0) {
|
|
347
|
+
return yield* Effect.fail(new EvalError({
|
|
348
|
+
code: "invalid_run_options",
|
|
349
|
+
message: "Deterministic runs require a non-empty runId",
|
|
350
|
+
path: "options.runId"
|
|
351
|
+
}));
|
|
352
|
+
}
|
|
353
|
+
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u.test(options.at) ||
|
|
354
|
+
!Number.isFinite(atMillis) ||
|
|
355
|
+
new Date(atMillis).toISOString() !== options.at) {
|
|
356
|
+
return yield* Effect.fail(new EvalError({
|
|
357
|
+
code: "invalid_run_options",
|
|
358
|
+
message: `Deterministic runs require a canonical UTC timestamp such as 2026-01-01T00:00:00.000Z, got '${options.at}'`,
|
|
359
|
+
path: "options.at"
|
|
360
|
+
}));
|
|
361
|
+
}
|
|
362
|
+
const n = options.trials ?? suite.trials ?? 1;
|
|
363
|
+
const k = options.k ?? 1;
|
|
364
|
+
if (!Number.isSafeInteger(n) || n < 1 || n > 1000 || !Number.isSafeInteger(k) || k < 1 || k > n) {
|
|
365
|
+
return yield* Effect.fail(new EvalError({
|
|
366
|
+
code: "invalid_trials",
|
|
367
|
+
message: "trials must be an integer from 1 to 1000 and k must be an integer from 1 to trials",
|
|
368
|
+
path: "options.trials"
|
|
369
|
+
}));
|
|
370
|
+
}
|
|
371
|
+
const at = options.at;
|
|
372
|
+
const runId = options.runId;
|
|
373
|
+
const sampleId = options.sampleId ?? "default";
|
|
374
|
+
// Await each batch before starting the next: concurrency within a trial is
|
|
375
|
+
// bounded by the suite, and neither scheduling nor scorer latency reorders trials.
|
|
376
|
+
const batches = yield* Effect.forEach(Array.from({ length: n }, (_, index) => index), (index) => Effect.gen(function* () {
|
|
377
|
+
const cases = yield* Effect.forEach(suite.cases, (suiteCase) => runCase(executor, suiteCase), {
|
|
378
|
+
concurrency: suite.concurrency,
|
|
379
|
+
discard: false
|
|
380
|
+
});
|
|
381
|
+
return yield* score(cases, suite, scorer, {
|
|
382
|
+
concurrency: suite.concurrency,
|
|
383
|
+
at,
|
|
384
|
+
runId,
|
|
385
|
+
sampleId: n === 1 ? sampleId : tupleKey(sampleId, String(index))
|
|
386
|
+
});
|
|
387
|
+
}), { concurrency: 1 });
|
|
388
|
+
const outcomes = {};
|
|
389
|
+
const cases = suite.cases.map((suiteCase, index) => {
|
|
390
|
+
const attempts = batches.map((batch) => batch[index]);
|
|
391
|
+
const passed = attempts.map((attempt) => attempt.error === undefined && attempt.observations.length > 0 &&
|
|
392
|
+
attempt.observations.every((observation) => observation.kind === "score" && observation.score === 1));
|
|
393
|
+
outcomes[suiteCase.name] = passed;
|
|
394
|
+
return {
|
|
395
|
+
...attempts[0],
|
|
396
|
+
error: attempts.find((attempt) => attempt.error !== undefined)?.error,
|
|
397
|
+
observations: attempts.flatMap((attempt) => attempt.observations),
|
|
398
|
+
trials: Trials.summarize(passed, k)
|
|
399
|
+
};
|
|
400
|
+
});
|
|
401
|
+
return {
|
|
402
|
+
runId,
|
|
403
|
+
suite: suite.name,
|
|
404
|
+
cases,
|
|
405
|
+
observations: cases.flatMap((caseResult) => caseResult.observations),
|
|
406
|
+
trials: Trials.aggregate(outcomes, k),
|
|
407
|
+
k
|
|
408
|
+
};
|
|
409
|
+
}).pipe(Effect.withSpan("Eval.run", { attributes: { suite: suite.name, runId: options.runId } }));
|
|
410
|
+
/**
|
|
411
|
+
* Provides a batch runner that is never available.
|
|
412
|
+
*
|
|
413
|
+
* Every bound score under this layer becomes an inconclusive observation, which
|
|
414
|
+
* a gate grades as an undecidable run rather than a red. Provide it to state
|
|
415
|
+
* that a suite must not score, and {@link layerInline} to score in process.
|
|
416
|
+
*
|
|
417
|
+
* @category layers
|
|
418
|
+
* @since 0.1.0
|
|
419
|
+
*/
|
|
420
|
+
export const layerNoop = Layer.sync(Runner)(() => {
|
|
421
|
+
const unavailable = new EvalError({ code: "scorer_unavailable", message: "No scorer batch runner is available" });
|
|
422
|
+
return {
|
|
423
|
+
runBatch: () => Effect.fail(unavailable),
|
|
424
|
+
runBatchCorrelated: (jobs) => Effect.succeed(jobs.map((job) => ({
|
|
425
|
+
identity: job.identity,
|
|
426
|
+
observation: ScorerRunner.inconclusive(job, unavailable)
|
|
427
|
+
})))
|
|
428
|
+
};
|
|
429
|
+
});
|
|
430
|
+
//# sourceMappingURL=Runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Runner.js","sourceRoot":"","sources":["../../src/Runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,YAAY,MAAM,wBAAwB,CAAA;AACtD,OAAO,KAAK,QAAQ,MAAM,0BAA0B,CAAA;AACpD,OAAO,KAAK,MAAM,MAAM,wBAAwB,CAAA;AAEhD,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,YAAY,EAAuD,MAAM,mBAAmB,CAAA;AACrG,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAA;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AAEjD,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AA0JrC;;;;;;;;;GASG;AACH,MAAM,OAAO,MAAO,SAAQ,OAAO,CAAC,OAAO,EAA4B,CAAC,oBAAoB,CAAC;CAAG;AAEhG,yDAAyD;AACzD,MAAM,eAAe,GAAG,IAAI,CAAA;AAE5B,M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AE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAA;AAEnG;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,SAAS,GAAwB,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE;IACpE,MAAM,WAAW,GAAG,IAAI,SAAS,CAAC,EAAE,IAAI,EAAE,oBAAoB,EAAE,OAAO,EAAE,qCAAqC,EAAE,CAAC,CAAA;IACjH,OAAO;QACL,QAAQ,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;QACxC,kBAAkB,EAAE,CAAC,IAAI,EAAE,EAAE,CAC3B,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;YAChC,QAAQ,EAAE,GAAG,CAAC,QAAQ;YACtB,WAAW,EAAE,YAAY,CAAC,YAAY,CAAC,GAAG,EAAE,WAAW,CAAC;SACzD,CAAC,CAAC,CAAC;KACP,CAAA;AACH,CAAC,CAAC,CAAA","sourcesContent":["/**\n * Deterministic fixed-suite execution and bound scorer evaluation.\n *\n * A run executes every case through the injected {@link CaseExecutor}, then\n * grades each execution with the scorers bound to the flow it executed. The\n * timestamp and the run identity come from the caller, so two runs of the same\n * suite over the same inputs produce byte-identical observations.\n *\n * @since 0.1.0\n */\n\nimport * as ScorerRunner from \"@smthrs/scorers/Runner\"\nimport * as Sampling from \"@smthrs/scorers/Sampling\"\nimport * as Scorer from \"@smthrs/scorers/Scorer\"\nimport type * as ScoreStore from \"@smthrs/scorers/ScoreStore\"\nimport * as Cause from \"effect/Cause\"\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport { CaseExecutor, type Execution, type Service as CaseExecutorService } from \"./CaseExecutor.ts\"\nimport { EvalError } from \"./EvalError.ts\"\nimport { flattenControlCharacters } from \"./internal/controlCharacters.ts\"\nimport { scorerLabel } from \"./internal/scorerLabel.ts\"\nimport { tupleKey } from \"./internal/tupleKey.ts\"\nimport type { Binding, Case, Suite } from \"./Suite.ts\"\nimport * as Trials from \"./Trials.ts\"\n\n/**\n * One score observation emitted by a suite run.\n *\n * `scorer` is the scorer key, a digest of the scorer's own declaration, which\n * is what a baseline matches on. `scorerName` is the human name from the same\n * declaration, carried alongside so a report can be read without grepping for\n * the digest. `at` is the run's timestamp, not the scorer's: a run's\n * observations all carry one instant so a baseline stays reproducible.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Observation =\n & {\n readonly case: string\n readonly scorer: string\n readonly scorerName?: string | undefined\n readonly stepKey: string\n readonly at: string\n }\n & (\n | {\n readonly kind: \"score\"\n readonly score: number\n readonly reason?: string | undefined\n readonly meta?: unknown\n }\n | { readonly kind: \"inconclusive\"; readonly reason: string; readonly meta?: unknown }\n )\n\n/**\n * A request sent to the scorers batch runner.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ScoreRequest {\n readonly case: string\n readonly stepKey: string\n readonly binding: Binding\n readonly input: {\n readonly input: unknown\n readonly output: unknown\n readonly groundTruth?: unknown\n readonly context?: unknown\n readonly latencyMs?: number\n }\n}\n\n/**\n * A blocking scorer job, matching the `Runner` module of `@smthrs/scorers`.\n *\n * @category models\n * @since 0.1.0\n */\nexport type ScoreJob = ScorerRunner.Job\n\n/**\n * Structural adapter for `@smthrs/scorers`' blocking batch runner.\n *\n * A runner that implements `runBatchCorrelated` tags every observation with\n * its job identity and may return results in any order. A runner that implements\n * only `runBatch` is correlated positionally: it must return exactly one\n * observation per job, in order, and each observation must echo its job's\n * `targetStepKey` and `scorerKey`. A run refuses the order-only protocol before\n * scoring when two jobs share that pair, because their results could not be\n * attributed to a case.\n *\n * A run fails with `scorer_protocol` when either protocol is broken.\n * `@smthrs/scorers`' `Runner.Service` implements `runBatchCorrelated`, so its\n * service value can be used directly and `ambiguous_score_job` never applies\n * to it.\n *\n * @category services\n * @since 0.1.0\n */\nexport interface ScoreBatchRunner {\n readonly runBatch: (\n jobs: ReadonlyArray<ScoreJob>,\n options?: { readonly concurrency?: number | undefined }\n ) => Effect.Effect<ReadonlyArray<ScoreObservation>, unknown>\n readonly runBatchCorrelated?:\n | ((\n jobs: ReadonlyArray<ScoreJob>,\n options?: { readonly concurrency?: number | undefined }\n ) => Effect.Effect<ReadonlyArray<BatchResult>, unknown>)\n | undefined\n}\n\n/**\n * A score result aligned with a {@link ScoreRequest}.\n *\n * @category models\n * @since 0.1.0\n */\nexport type ScoreObservation = ScoreStore.Observation\n\n/**\n * A batch result tagged with the identity of the job that produced it.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface BatchResult {\n readonly identity: string\n readonly observation: ScoreObservation\n}\n\n/**\n * Per-case result retained by the deterministic runner.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface CaseResult {\n readonly case: string\n readonly execution?: Execution | undefined\n readonly error?: EvalError | undefined\n readonly observations: ReadonlyArray<Observation>\n readonly trials?: Trials.Summary | undefined\n}\n\n/**\n * Stable result of a suite run.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface RunResult {\n readonly runId: string\n readonly suite: string\n readonly cases: ReadonlyArray<CaseResult>\n readonly observations: ReadonlyArray<Observation>\n readonly trials?: Trials.Aggregate | undefined\n readonly k?: number | undefined\n}\n\n/**\n * Options for a deterministic suite run.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface RunOptions {\n readonly scorer?: ScoreBatchRunner | undefined\n readonly runId: string\n readonly sampleId?: string | undefined\n readonly trials?: number | undefined\n readonly k?: number | undefined\n readonly at: string\n}\n\n/**\n * Injectable batch-runner service used when a caller wants a reusable adapter.\n *\n * {@link run} does not require it: a run scores with `options.scorer` when one\n * is passed, otherwise with this service when one is provided, otherwise\n * in process through {@link makeInline}.\n *\n * @category services\n * @since 0.1.0\n */\nexport class Runner extends Context.Service<Runner, ScoreBatchRunner>()(\"flows/evals/Runner\") {}\n\n/** The longest cause summary a public reason carries. */\nconst maxReasonLength = 2048\n\nconst boundedReason = (reason: string): string =>\n reason.length <= maxReasonLength ? reason : `${reason.slice(0, maxReasonLength)}[truncated]`\n\nconst scorerKeyOf = (binding: Binding): string => binding.scorer.scorerKey\n\n// A scorer is a flow and every flow carries a name, so a run always has one to\n// report. A stored observation may still lack it, which is why `scorerLabel`\n// and the `Observation` field both keep the absent case.\nconst scorerNameOf = (binding: Binding): string => binding.scorer.name\n\nconst label = (binding: Binding): string =>\n scorerLabel({ scorer: scorerKeyOf(binding), scorerName: scorerNameOf(binding) })\n\nconst inconclusive = (request: ScoreRequest, reason: string, at: string): Observation => {\n return {\n case: request.case,\n scorer: scorerKeyOf(request.binding),\n scorerName: scorerNameOf(request.binding),\n stepKey: request.stepKey,\n kind: \"inconclusive\",\n reason,\n at\n }\n}\n\n// A case failure always locates itself. An executor that named the offending\n// value keeps its own path; one that named nothing still gets the case, because\n// a failure with no path leaves a caller nothing to branch on but prose.\nconst casePath = (name: string): string => `cases['${name}']`\n\n// The executor boundary handles every non-interruption cause, not only typed\n// failures: an executor that wraps a parser in `Effect.sync` dies with a\n// defect, and one case's defect must not abort its siblings or the report.\n// The wrapped message is flattened because a gate prints it to a CI log, where\n// a newline from the target would let it forge a workflow command; the\n// original message survives untouched on `cause`.\nconst caseError = (suiteCase: Case, cause: Cause.Cause<unknown>): EvalError => {\n const reason = Cause.squash(cause)\n return reason instanceof EvalError\n ? new EvalError({\n code: reason.code,\n message: flattenControlCharacters(`Target failed for case '${suiteCase.name}': ${reason.message}`),\n path: reason.path ?? casePath(suiteCase.name),\n cause: reason\n })\n : new EvalError({\n code: \"executor\",\n message: flattenControlCharacters(`Target failed for case '${suiteCase.name}': ${String(reason)}`),\n path: casePath(suiteCase.name),\n cause: reason\n })\n}\n\n// A step key is a runtime value the target returns, so unlike a suite name it\n// is flattened rather than rejected: it reaches baselines, reports and the\n// gate summary, and a control character in any of them corrupts a CI log line.\nconst runCase = (executor: CaseExecutorService, suiteCase: Case): Effect.Effect<CaseResult> =>\n Effect.suspend(() => executor.run(structuredClone(suiteCase))).pipe(\n Effect.map((execution): CaseResult => ({\n case: suiteCase.name,\n execution: { ...execution, stepKey: flattenControlCharacters(execution.stepKey) },\n observations: []\n })),\n Effect.catchCause((cause) =>\n Cause.hasInterrupts(cause)\n ? Effect.interrupt\n : Effect.succeed<CaseResult>({ case: suiteCase.name, error: caseError(suiteCase, cause), observations: [] })\n ),\n Effect.withSpan(\"Eval.case\", { attributes: { case: suiteCase.name } })\n )\n\n// A declared case value is ground truth even when it is `null`, `false`, `0`\n// or the empty string; only an absent `expected` defers to the binding.\nconst groundTruthFor = (suiteCase: Case, binding: Binding): { readonly groundTruth?: unknown } => {\n const groundTruth = suiteCase.expected !== undefined ? suiteCase.expected : binding.groundTruth\n return groundTruth === undefined ? {} : { groundTruth: structuredClone(groundTruth) }\n}\n\nconst requestFor = (suiteCase: Case, execution: Execution, binding: Binding): ScoreRequest => ({\n case: suiteCase.name,\n stepKey: execution.stepKey,\n binding: {\n ...structuredClone({\n sampling: binding.sampling,\n ...(binding.groundTruth === undefined ? {} : { groundTruth: binding.groundTruth }),\n ...(binding.context === undefined ? {} : { context: binding.context })\n }),\n scorer: binding.scorer,\n appliesTo: binding.appliesTo\n },\n input: {\n input: structuredClone(suiteCase.input),\n output: execution.output,\n ...(groundTruthFor(suiteCase, binding)),\n ...(binding.context === undefined ? {} : { context: structuredClone(binding.context) }),\n latencyMs: execution.latencyMs\n }\n})\n\nconst observationFor = (request: ScoreRequest, result: ScoreObservation, at: string): Observation => {\n const source = result as {\n readonly kind: unknown\n readonly score?: unknown\n readonly reason?: unknown\n readonly meta?: unknown\n }\n const kind = source.kind\n if (kind !== \"score\" && kind !== \"inconclusive\") {\n return inconclusive(\n request,\n boundedReason(`Scorer ${label(request.binding)} returned an unusable observation kind '${String(kind)}'`),\n at\n )\n }\n const reason = source.reason\n if (kind === \"inconclusive\") {\n return inconclusive(\n request,\n typeof reason === \"string\" && reason.length > 0\n ? boundedReason(reason)\n : boundedReason(`Scorer ${label(request.binding)} returned an inconclusive observation with no reason`),\n at\n )\n }\n const score = source.score\n if (typeof score !== \"number\" || !Number.isFinite(score) || score < 0 || score > 1) {\n return inconclusive(\n request,\n boundedReason(`Scorer ${label(request.binding)} returned a score outside [0, 1]: ${String(score)}`),\n at\n )\n }\n const meta = source.meta\n return {\n case: request.case,\n scorer: scorerKeyOf(request.binding),\n scorerName: scorerNameOf(request.binding),\n stepKey: request.stepKey,\n kind: \"score\",\n score,\n ...(typeof reason === \"string\" ? { reason: boundedReason(reason) } : {}),\n ...(meta === undefined ? {} : { meta }),\n at\n }\n}\n\nconst executeInline = (job: ScoreJob): Effect.Effect<ScoreObservation> =>\n job.score.pipe(\n Effect.flatMap(Scorer.validate),\n Effect.map((result): ScoreStore.ScoreObservation => ({\n ...job.observation,\n kind: \"score\",\n score: result.score,\n ...(result.reason === undefined ? {} : { reason: result.reason }),\n ...(result.meta === undefined ? {} : { meta: result.meta }),\n at: job.at\n })),\n Effect.catchCause((cause) =>\n Cause.hasInterrupts(cause)\n ? Effect.interrupt\n : Effect.succeed(ScorerRunner.inconclusive(job, cause))\n )\n )\n\n/**\n * Builds the in-process batch runner a run scores with by default.\n *\n * Each job's scorer runs in the current process, its result is checked by\n * `@smthrs/scorers`' own `Scorer.validate`, and a scorer that fails becomes an\n * inconclusive observation naming its cause rather than failing the run. It\n * honours the {@link ScoreBatchRunner} protocol, so it is also the reference\n * a custom adapter can be compared against.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const makeInline = (): ScoreBatchRunner => {\n const runBatchCorrelated = (\n jobs: ReadonlyArray<ScoreJob>,\n options?: { readonly concurrency?: number | undefined }\n ): Effect.Effect<ReadonlyArray<BatchResult>> =>\n Effect.forEach(\n jobs,\n (job) => executeInline(job).pipe(Effect.map((observation) => ({ identity: job.identity, observation }))),\n { concurrency: options?.concurrency ?? 1 }\n )\n return {\n runBatchCorrelated,\n runBatch: (jobs, options) =>\n runBatchCorrelated(jobs, options).pipe(Effect.map((results) => results.map((result) => result.observation)))\n }\n}\n\n/**\n * Provides the in-process batch runner built by {@link makeInline}.\n *\n * @category layers\n * @since 0.1.0\n */\nexport const layerInline: Layer.Layer<Runner> = Layer.succeed(Runner)(makeInline())\n\ninterface ScoreContext {\n readonly at: string\n readonly runId: string\n readonly sampleId: string\n readonly concurrency: number\n}\n\nconst buildJobs = (\n cases: ReadonlyArray<CaseResult>,\n suite: Suite,\n { at, runId, sampleId, concurrency }: ScoreContext\n): Effect.Effect<\n { readonly requests: ReadonlyArray<ScoreRequest>; readonly jobs: ReadonlyArray<ScoreJob> },\n EvalError\n> =>\n Effect.gen(function*() {\n // `cases` is the result of one `Effect.forEach` over `suite.cases`, so the\n // two arrays are index-aligned by construction; nothing here has to look a\n // case back up by name.\n const candidates = cases.flatMap((caseResult, index) => {\n const execution = caseResult.execution\n if (execution === undefined) return []\n const suiteCase = suite.cases[index]!\n return suite.bindings.flatMap((binding, bindingIndex) =>\n binding.appliesTo === execution.target\n ? [{ request: requestFor(suiteCase, execution, binding), bindingIndex }]\n : []\n )\n })\n const sampled = yield* Effect.forEach(\n candidates,\n ({ request, bindingIndex }) =>\n Sampling.decide(\n request.binding.sampling,\n request.stepKey,\n scorerKeyOf(request.binding)\n ).pipe(\n Effect.map((selected) => selected ? request : undefined),\n Effect.mapError((cause) =>\n new EvalError({\n code: \"invalid_suite\",\n message: `Invalid sampling policy for scorer ${label(request.binding)}`,\n path: `bindings[${bindingIndex}].sampling`,\n cause\n })\n )\n ),\n { concurrency }\n )\n const requests = sampled.filter((request): request is ScoreRequest => request !== undefined)\n const jobs: Array<ScoreJob> = requests.map((request, index) => ({\n identity: tupleKey(\n suite.name,\n runId,\n sampleId,\n request.case,\n request.stepKey,\n scorerKeyOf(request.binding),\n `${index}`\n ),\n observation: { targetStepKey: request.stepKey, scorerKey: scorerKeyOf(request.binding) },\n score: Effect.suspend(() => request.binding.scorer.score(request.input)),\n at: Date.parse(at)\n }))\n return { requests, jobs }\n })\n\nconst batchFallback = (\n jobs: ReadonlyArray<ScoreJob>,\n cause: Cause.Cause<unknown>\n): Effect.Effect<ReadonlyArray<ScoreObservation>> =>\n Cause.hasInterrupts(cause)\n ? Effect.interrupt\n : Effect.succeed(jobs.map((job) => ScorerRunner.inconclusive(job, cause)))\n\nconst scoreCorrelated = (\n jobs: ReadonlyArray<ScoreJob>,\n scorer: ScoreBatchRunner,\n runBatchCorrelated: NonNullable<ScoreBatchRunner[\"runBatchCorrelated\"]>,\n { concurrency }: ScoreContext\n): Effect.Effect<ReadonlyArray<ScoreObservation>, EvalError> =>\n Effect.gen(function*() {\n const batchResults = yield* Effect.suspend(() => runBatchCorrelated.call(scorer, jobs, { concurrency })).pipe(\n Effect.catchCause((cause) =>\n batchFallback(jobs, cause).pipe(\n Effect.map((observations) =>\n observations.map((observation, index) => ({\n identity: jobs[index]!.identity,\n observation\n }))\n )\n )\n )\n )\n if (batchResults.length !== jobs.length) {\n return yield* Effect.fail(\n new EvalError({\n code: \"scorer_protocol\",\n message:\n `Correlated scorer batch returned ${batchResults.length} results for ${jobs.length} jobs; a batch runner must return exactly one result per job identity`,\n path: \"runBatchCorrelated\"\n })\n )\n }\n const jobIndexByIdentity = new Map(jobs.map((job, index) => [job.identity, index] as const))\n const byIdentity = new Map<string, ScoreObservation>()\n let unknown: { readonly identity: string; readonly index: number } | undefined\n for (const [index, result] of batchResults.entries()) {\n const jobIndex = jobIndexByIdentity.get(result.identity)\n if (jobIndex !== undefined && byIdentity.has(result.identity)) {\n return yield* Effect.fail(\n new EvalError({\n code: \"scorer_protocol\",\n message:\n `Correlated scorer batch returned duplicate identity '${result.identity}' for job ${jobIndex} at result index ${index}`,\n path: `runBatchCorrelated[${index}]`\n })\n )\n }\n if (jobIndex === undefined) unknown = { identity: result.identity, index }\n byIdentity.set(result.identity, result.observation)\n }\n if (unknown !== undefined) {\n const missingJobIndex = jobs.findIndex((job) => !byIdentity.has(job.identity))\n const missingIdentity = jobs[missingJobIndex]!.identity\n return yield* Effect.fail(\n new EvalError({\n code: \"scorer_protocol\",\n message:\n `Correlated scorer batch returned unknown identity '${unknown.identity}' at result index ${unknown.index}; job ${missingJobIndex} identity '${missingIdentity}' is absent`,\n path: `runBatchCorrelated[${unknown.index}]`\n })\n )\n }\n return jobs.map((job) => byIdentity.get(job.identity)!)\n })\n\nconst scoreOrderOnly = (\n jobs: ReadonlyArray<ScoreJob>,\n requests: ReadonlyArray<ScoreRequest>,\n scorer: ScoreBatchRunner,\n { concurrency }: ScoreContext\n): Effect.Effect<ReadonlyArray<ScoreObservation>, EvalError> =>\n Effect.gen(function*() {\n const pairIndexes = new Map<string, number>()\n for (const [index, job] of jobs.entries()) {\n const pair = tupleKey(job.observation.targetStepKey, job.observation.scorerKey)\n const previous = pairIndexes.get(pair)\n if (previous !== undefined) {\n const first = requests[previous]!\n const second = requests[index]!\n return yield* Effect.fail(\n new EvalError({\n code: \"ambiguous_score_job\",\n message:\n `Cannot attribute score jobs for cases '${first.case}' and '${second.case}': both use step key '${job.observation.targetStepKey}' and scorer ${\n label(second.binding)\n }. Give each case its own step key, or provide a batch runner that implements runBatchCorrelated`,\n path: \"runBatch\"\n })\n )\n }\n pairIndexes.set(pair, index)\n }\n const results = yield* Effect.suspend(() => scorer.runBatch(jobs, { concurrency })).pipe(\n Effect.catchCause((cause) => batchFallback(jobs, cause))\n )\n if (results.length !== jobs.length) {\n return yield* Effect.fail(\n new EvalError({\n code: \"scorer_protocol\",\n message:\n `Scorer batch returned ${results.length} observations for ${jobs.length} jobs; a batch runner must return exactly one observation per job, in order`,\n path: \"runBatch\"\n })\n )\n }\n return results\n })\n\nconst score = (\n cases: ReadonlyArray<CaseResult>,\n suite: Suite,\n scorer: ScoreBatchRunner,\n context: ScoreContext\n): Effect.Effect<ReadonlyArray<CaseResult>, EvalError> =>\n Effect.gen(function*() {\n const { requests, jobs } = yield* buildJobs(cases, suite, context)\n if (requests.length === 0) return cases\n const runBatchCorrelated = scorer.runBatchCorrelated\n const protocol = runBatchCorrelated === undefined ? \"runBatch\" : \"runBatchCorrelated\"\n const results = yield* runBatchCorrelated === undefined\n ? scoreOrderOnly(jobs, requests, scorer, context)\n : scoreCorrelated(jobs, scorer, runBatchCorrelated, context)\n const { at } = context\n const observations: Array<Observation> = []\n for (const [index, request] of requests.entries()) {\n const result = results[index]!\n if (result.targetStepKey !== request.stepKey || result.scorerKey !== scorerKeyOf(request.binding)) {\n return yield* Effect.fail(\n new EvalError({\n code: \"scorer_protocol\",\n message: protocol === \"runBatchCorrelated\"\n ? `Correlated scorer batch identity '${\n jobs[index]!.identity\n }' returned step '${result.targetStepKey}' and scorer '${result.scorerKey}' where job ${index} asked for scorer '${\n scorerKeyOf(request.binding)\n }' at step '${request.stepKey}'`\n : `Scorer batch returned an observation for '${result.scorerKey}' at step '${result.targetStepKey}' where job ${index} asked for '${\n scorerKeyOf(request.binding)\n }' at step '${request.stepKey}'; results must stay aligned with their jobs`,\n path: `${protocol}[${index}]`\n })\n )\n }\n observations.push(observationFor(request, result, at))\n }\n const byCase = new Map<string, Array<Observation>>()\n for (const observation of observations) {\n const bucket = byCase.get(observation.case)\n if (bucket === undefined) byCase.set(observation.case, [observation])\n else bucket.push(observation)\n }\n return cases.map((caseResult) => ({\n ...caseResult,\n observations: byCase.get(caseResult.case) ?? []\n }))\n })\n\n/**\n * Runs a fixed suite with bounded execution and declaration-order results.\n *\n * Cases run through the provided {@link CaseExecutor} at the suite's\n * concurrency and are returned in declaration order. Each executor receives\n * its own mutable case copy. Each scorer receives independent copies of the\n * original case input, expected value, and binding data. Every execution is then\n * graded by the bindings whose `appliesTo` is the flow the execution reports as\n * its target, matched by reference identity, so a binding attached to another\n * flow contributes no observations.\n *\n * Scoring goes through `options.scorer` when the caller passes one, the\n * {@link Runner} service when one is provided, and {@link makeInline}\n * otherwise. A case whose target failed keeps its typed error, locates itself\n * with the executor's own `path` or with `cases['<name>']`, and produces no\n * observations; a scorer that failed produces an inconclusive observation. The\n * run itself fails only with `invalid_run_options` for a non-canonical run\n * identity or timestamp, `invalid_suite` for an unusable sampling policy,\n * `ambiguous_score_job` when an order-only runner cannot distinguish two jobs,\n * or `scorer_protocol` for a batch runner that broke the\n * {@link ScoreBatchRunner} contract.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const run = (\n suite: Suite,\n options: RunOptions\n): Effect.Effect<RunResult, EvalError, CaseExecutor> =>\n Effect.gen(function*() {\n const executor = yield* CaseExecutor\n const injected = yield* Effect.serviceOption(Runner)\n const scorer = options.scorer ?? (injected._tag === \"Some\" ? injected.value : makeInline())\n const atMillis = Date.parse(options.at)\n if (options.runId.trim().length === 0) {\n return yield* Effect.fail(\n new EvalError({\n code: \"invalid_run_options\",\n message: \"Deterministic runs require a non-empty runId\",\n path: \"options.runId\"\n })\n )\n }\n if (\n !/^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$/u.test(options.at) ||\n !Number.isFinite(atMillis) ||\n new Date(atMillis).toISOString() !== options.at\n ) {\n return yield* Effect.fail(\n new EvalError({\n code: \"invalid_run_options\",\n message:\n `Deterministic runs require a canonical UTC timestamp such as 2026-01-01T00:00:00.000Z, got '${options.at}'`,\n path: \"options.at\"\n })\n )\n }\n const n = options.trials ?? suite.trials ?? 1\n const k = options.k ?? 1\n if (!Number.isSafeInteger(n) || n < 1 || n > 1000 || !Number.isSafeInteger(k) || k < 1 || k > n) {\n return yield* Effect.fail(\n new EvalError({\n code: \"invalid_trials\",\n message: \"trials must be an integer from 1 to 1000 and k must be an integer from 1 to trials\",\n path: \"options.trials\"\n })\n )\n }\n const at = options.at\n const runId = options.runId\n const sampleId = options.sampleId ?? \"default\"\n // Await each batch before starting the next: concurrency within a trial is\n // bounded by the suite, and neither scheduling nor scorer latency reorders trials.\n const batches = yield* Effect.forEach(Array.from({ length: n }, (_, index) => index), (index) =>\n Effect.gen(function*() {\n const cases = yield* Effect.forEach(suite.cases, (suiteCase) =>\n runCase(executor, suiteCase), {\n concurrency: suite.concurrency,\n discard: false\n })\n return yield* score(cases, suite, scorer, {\n concurrency: suite.concurrency,\n at,\n runId,\n sampleId: n === 1 ? sampleId : tupleKey(sampleId, String(index))\n })\n }), { concurrency: 1 })\n const outcomes: Record<string, Array<boolean>> = {}\n const cases = suite.cases.map((suiteCase, index) => {\n const attempts = batches.map((batch) =>\n batch[index]!\n )\n const passed = attempts.map((attempt) =>\n attempt.error === undefined && attempt.observations.length > 0 &&\n attempt.observations.every((observation) => observation.kind === \"score\" && observation.score === 1)\n )\n outcomes[suiteCase.name] = passed\n return {\n ...attempts[0]!,\n error: attempts.find((attempt) => attempt.error !== undefined)?.error,\n observations: attempts.flatMap((attempt) => attempt.observations),\n trials: Trials.summarize(passed, k)\n }\n })\n return {\n runId,\n suite: suite.name,\n cases,\n observations: cases.flatMap((caseResult) => caseResult.observations),\n trials: Trials.aggregate(outcomes, k),\n k\n }\n }).pipe(Effect.withSpan(\"Eval.run\", { attributes: { suite: suite.name, runId: options.runId } }))\n\n/**\n * Provides a batch runner that is never available.\n *\n * Every bound score under this layer becomes an inconclusive observation, which\n * a gate grades as an undecidable run rather than a red. Provide it to state\n * that a suite must not score, and {@link layerInline} to score in process.\n *\n * @category layers\n * @since 0.1.0\n */\nexport const layerNoop: Layer.Layer<Runner> = Layer.sync(Runner)(() => {\n const unavailable = new EvalError({ code: \"scorer_unavailable\", message: \"No scorer batch runner is available\" })\n return {\n runBatch: () => Effect.fail(unavailable),\n runBatchCorrelated: (jobs) =>\n Effect.succeed(jobs.map((job) => ({\n identity: job.identity,\n observation: ScorerRunner.inconclusive(job, unavailable)\n })))\n }\n})\n"]}
|