@arnilo/prism-hosts 0.0.0-stage → 0.13.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/CHANGELOG.md +394 -0
- package/LICENSE +21 -0
- package/README.md +34 -2
- package/acp/README.md +87 -0
- package/acp/src/capabilities.ts +184 -0
- package/acp/src/errors.ts +24 -0
- package/acp/src/host.ts +36 -0
- package/acp/src/index.ts +25 -0
- package/acp/src/limits.ts +49 -0
- package/acp/src/mapper.ts +419 -0
- package/acp/src/permission.ts +37 -0
- package/acp/src/projection.ts +39 -0
- package/acp/src/prompt.ts +170 -0
- package/acp/src/serve.ts +345 -0
- package/acp/src/session-map.ts +49 -0
- package/ag-ui/README.md +56 -0
- package/ag-ui/src/a2ui.ts +570 -0
- package/ag-ui/src/ag-ui-mapper.ts +507 -0
- package/ag-ui/src/encrypted-value.ts +40 -0
- package/ag-ui/src/errors.ts +29 -0
- package/ag-ui/src/handler.ts +642 -0
- package/ag-ui/src/index.ts +177 -0
- package/ag-ui/src/input.ts +190 -0
- package/ag-ui/src/limits.ts +279 -0
- package/ag-ui/src/mcp-apps.ts +256 -0
- package/ag-ui/src/mcp.ts +94 -0
- package/ag-ui/src/projection.ts +201 -0
- package/ag-ui/src/projectors.ts +272 -0
- package/ag-ui/src/renderer/bind.ts +152 -0
- package/ag-ui/src/renderer/core.ts +270 -0
- package/ag-ui/src/renderer/index.ts +165 -0
- package/ag-ui/src/replay.ts +189 -0
- package/ag-ui/src/types.ts +66 -0
- package/channels/README.md +139 -0
- package/channels/src/admission.ts +8 -0
- package/channels/src/approvals.ts +262 -0
- package/channels/src/checkpoint-contract.ts +97 -0
- package/channels/src/delivery.ts +116 -0
- package/channels/src/host-ui.ts +356 -0
- package/channels/src/identity.ts +46 -0
- package/channels/src/index.ts +82 -0
- package/channels/src/limits.ts +49 -0
- package/channels/src/memory-stores.ts +112 -0
- package/channels/src/pairing.ts +178 -0
- package/channels/src/runtime-admit.ts +389 -0
- package/channels/src/runtime-core.ts +621 -0
- package/channels/src/runtime-reconcile.ts +299 -0
- package/channels/src/runtime-turn.ts +664 -0
- package/channels/src/runtime-types.ts +95 -0
- package/channels/src/runtime.ts +86 -0
- package/channels/src/secret-ref.ts +42 -0
- package/channels/src/signal.ts +790 -0
- package/channels/src/state.ts +355 -0
- package/channels/src/telegram.ts +1317 -0
- package/channels/src/types.ts +442 -0
- package/devtools/README.md +92 -0
- package/devtools/bin/prism-devtools.ts +63 -0
- package/devtools/src/args.ts +71 -0
- package/devtools/src/bun.d.ts +27 -0
- package/devtools/src/compare.ts +190 -0
- package/devtools/src/errors.ts +15 -0
- package/devtools/src/index.ts +25 -0
- package/devtools/src/replay.ts +36 -0
- package/devtools/src/security.ts +80 -0
- package/devtools/src/serve.ts +259 -0
- package/devtools/src/ui/assets.ts +108 -0
- package/devtools/src/ui/inspector.ts +182 -0
- package/driver-process/README.md +197 -0
- package/driver-process/src/abort.ts +74 -0
- package/driver-process/src/adapter.ts +211 -0
- package/driver-process/src/driver.ts +303 -0
- package/driver-process/src/errors.ts +17 -0
- package/driver-process/src/extend.ts +72 -0
- package/driver-process/src/index.ts +5 -0
- package/driver-process/src/inspect.ts +33 -0
- package/driver-process/src/session.ts +72 -0
- package/driver-process/src/testing.ts +394 -0
- package/driver-process/src/transports/json-rpc.ts +147 -0
- package/driver-process/src/transports/jsonl-stream.ts +12 -0
- package/driver-process/src/transports/types.ts +11 -0
- package/driver-process/src/turn.ts +106 -0
- package/evals/README.md +843 -0
- package/evals/src/calibration.ts +179 -0
- package/evals/src/citation-integrity.ts +98 -0
- package/evals/src/citations.ts +45 -0
- package/evals/src/compare-experiments.ts +153 -0
- package/evals/src/comparison.ts +230 -0
- package/evals/src/curate.ts +257 -0
- package/evals/src/dataset.ts +81 -0
- package/evals/src/erp-invariants.ts +163 -0
- package/evals/src/errors.ts +23 -0
- package/evals/src/experiment.ts +537 -0
- package/evals/src/failure-injection.ts +77 -0
- package/evals/src/feedback.ts +53 -0
- package/evals/src/index.ts +198 -0
- package/evals/src/injection.ts +247 -0
- package/evals/src/judge-panel.ts +109 -0
- package/evals/src/judge.ts +89 -0
- package/evals/src/limits.ts +50 -0
- package/evals/src/report-record.ts +47 -0
- package/evals/src/rubric-judge.ts +334 -0
- package/evals/src/scenario-runner.ts +317 -0
- package/evals/src/scenarios.ts +81 -0
- package/evals/src/score.ts +328 -0
- package/evals/src/scorer.ts +21 -0
- package/evals/src/state-match.ts +130 -0
- package/evals/src/stats.ts +635 -0
- package/evals/src/threshold.ts +117 -0
- package/evals/src/timeline.ts +38 -0
- package/evals/src/toolless-session.ts +33 -0
- package/evals/src/trace.ts +80 -0
- package/evals/src/trajectory.ts +623 -0
- package/evals/src/types.ts +672 -0
- package/evals/src/user-simulator.ts +101 -0
- package/evals/src/util.ts +207 -0
- package/headless/README.md +172 -0
- package/headless/src/answers.ts +91 -0
- package/headless/src/authority.ts +32 -0
- package/headless/src/errors.ts +36 -0
- package/headless/src/index.ts +17 -0
- package/headless/src/limits.ts +11 -0
- package/headless/src/pending.ts +16 -0
- package/headless/src/policy.ts +36 -0
- package/headless/src/resume.ts +103 -0
- package/headless/src/route.ts +153 -0
- package/headless/src/scheduler/clock.ts +22 -0
- package/headless/src/scheduler/cron.ts +19 -0
- package/headless/src/scheduler/firing.ts +58 -0
- package/headless/src/scheduler/index.ts +399 -0
- package/headless/src/scheduler/lease.ts +38 -0
- package/headless/src/scheduler/missed.ts +12 -0
- package/headless/src/scheduler/state.ts +132 -0
- package/host-builder/README.md +251 -0
- package/host-builder/bin/prism-host-builder.ts +169 -0
- package/host-builder/data/api.json +6167 -0
- package/host-builder/data/errors.json +546 -0
- package/host-builder/data/examples/acp.ts +63 -0
- package/host-builder/data/examples/adapters/conformance.ts +34 -0
- package/host-builder/data/examples/adapters/fixtures/rpc-server.json +26 -0
- package/host-builder/data/examples/adapters/fixtures/stream-cli.json +27 -0
- package/host-builder/data/examples/adapters/fixtures/workspace-config.json +28 -0
- package/host-builder/data/examples/adapters/rpc-server.dialect.ts +218 -0
- package/host-builder/data/examples/adapters/rpc-server.ts +309 -0
- package/host-builder/data/examples/adapters/stream-cli.dialect.ts +165 -0
- package/host-builder/data/examples/adapters/stream-cli.ts +256 -0
- package/host-builder/data/examples/adapters/workspace-config.dialect.ts +172 -0
- package/host-builder/data/examples/adapters/workspace-config.ts +251 -0
- package/host-builder/data/examples/advisor.ts +94 -0
- package/host-builder/data/examples/ag-ui-a2ui.ts +80 -0
- package/host-builder/data/examples/ag-ui-mcp-apps.ts +44 -0
- package/host-builder/data/examples/ag-ui-server.ts +118 -0
- package/host-builder/data/examples/agent-durable-approval.ts +43 -0
- package/host-builder/data/examples/api-key-auth.ts +23 -0
- package/host-builder/data/examples/approval.ts +92 -0
- package/host-builder/data/examples/attention-compiler.ts +81 -0
- package/host-builder/data/examples/autonomous-coding-loop.ts +343 -0
- package/host-builder/data/examples/behavior-evaluation.ts +303 -0
- package/host-builder/data/examples/build-workflow.ts +334 -0
- package/host-builder/data/examples/cache-aware-prompt-assembly.ts +139 -0
- package/host-builder/data/examples/caveman-ponytail.ts +197 -0
- package/host-builder/data/examples/cli.ts +31 -0
- package/host-builder/data/examples/coding-approval.ts +115 -0
- package/host-builder/data/examples/coding-browser-evaluation.ts +208 -0
- package/host-builder/data/examples/coding-compaction.ts +30 -0
- package/host-builder/data/examples/coding-goal-verify.ts +121 -0
- package/host-builder/data/examples/coding-tools-capability-gaps.ts +75 -0
- package/host-builder/data/examples/compaction.ts +36 -0
- package/host-builder/data/examples/connected-slack-mcp.ts +67 -0
- package/host-builder/data/examples/context.ts +29 -0
- package/host-builder/data/examples/custom-adapter.ts +256 -0
- package/host-builder/data/examples/custom-builders.ts +66 -0
- package/host-builder/data/examples/custom-loop.ts +89 -0
- package/host-builder/data/examples/custom-provider.ts +70 -0
- package/host-builder/data/examples/custom-session-store.ts +72 -0
- package/host-builder/data/examples/custom-tools-skills-context.ts +97 -0
- package/host-builder/data/examples/decision-models.ts +98 -0
- package/host-builder/data/examples/define-package.ts +188 -0
- package/host-builder/data/examples/delegate.ts +200 -0
- package/host-builder/data/examples/discover-skills.ts +65 -0
- package/host-builder/data/examples/drive-rag-sync.ts +44 -0
- package/host-builder/data/examples/durable-approval.ts +139 -0
- package/host-builder/data/examples/durable-investigation.ts +93 -0
- package/host-builder/data/examples/durable-loops-and-approvals.ts +94 -0
- package/host-builder/data/examples/enterprise-identity.ts +21 -0
- package/host-builder/data/examples/enterprise-policy-audit.ts +50 -0
- package/host-builder/data/examples/enterprise-work-connectors.ts +103 -0
- package/host-builder/data/examples/eval-injection.ts +73 -0
- package/host-builder/data/examples/eval-judges.ts +145 -0
- package/host-builder/data/examples/evals.ts +86 -0
- package/host-builder/data/examples/evaluation-gate.ts +40 -0
- package/host-builder/data/examples/execution-timeline.ts +173 -0
- package/host-builder/data/examples/external-agent.ts +541 -0
- package/host-builder/data/examples/external-app-db-backed.ts +344 -0
- package/host-builder/data/examples/governance-deny.ts +122 -0
- package/host-builder/data/examples/guardrail-packs.ts +240 -0
- package/host-builder/data/examples/headless-host.ts +245 -0
- package/host-builder/data/examples/hooks-audit-command.mjs +31 -0
- package/host-builder/data/examples/hooks-json.ts +87 -0
- package/host-builder/data/examples/host-builder-agent.ts +150 -0
- package/host-builder/data/examples/host-hooks.ts +71 -0
- package/host-builder/data/examples/host-step-loop-timeline.ts +212 -0
- package/host-builder/data/examples/hosted-sandbox.ts +48 -0
- package/host-builder/data/examples/instruction-injection.ts +127 -0
- package/host-builder/data/examples/jsonl-stores-branching.ts +49 -0
- package/host-builder/data/examples/long-term-memory.ts +158 -0
- package/host-builder/data/examples/manifest.ts +112 -0
- package/host-builder/data/examples/mcp-server.ts +48 -0
- package/host-builder/data/examples/mcp-trust.ts +173 -0
- package/host-builder/data/examples/memory-and-wiki.ts +315 -0
- package/host-builder/data/examples/memory-lifecycle.ts +302 -0
- package/host-builder/data/examples/memory-system.ts +252 -0
- package/host-builder/data/examples/messaging-agent.ts +70 -0
- package/host-builder/data/examples/minimal-host-app.ts +41 -0
- package/host-builder/data/examples/model-router-aggregate-budgets.ts +364 -0
- package/host-builder/data/examples/model-router.ts +106 -0
- package/host-builder/data/examples/obscura.ts +48 -0
- package/host-builder/data/examples/observability.ts +116 -0
- package/host-builder/data/examples/open-connector-sidecar.ts +105 -0
- package/host-builder/data/examples/openai-compatible.ts +70 -0
- package/host-builder/data/examples/package-skills.ts +85 -0
- package/host-builder/data/examples/phase9-coding-intelligence.ts +221 -0
- package/host-builder/data/examples/provider-registration.ts +29 -0
- package/host-builder/data/examples/provider-resolver.ts +39 -0
- package/host-builder/data/examples/rag.ts +40 -0
- package/host-builder/data/examples/remote-agent-server.ts +45 -0
- package/host-builder/data/examples/remote-agent.ts +124 -0
- package/host-builder/data/examples/research-ledger.ts +79 -0
- package/host-builder/data/examples/revocation-propagation.ts +287 -0
- package/host-builder/data/examples/run-feedback.ts +93 -0
- package/host-builder/data/examples/scanned-document-rag.ts +48 -0
- package/host-builder/data/examples/sdk-basics.ts +21 -0
- package/host-builder/data/examples/secure-agent.ts +55 -0
- package/host-builder/data/examples/semantic-memory.ts +52 -0
- package/host-builder/data/examples/serve-tools.ts +495 -0
- package/host-builder/data/examples/session-memory.ts +317 -0
- package/host-builder/data/examples/session-search.ts +131 -0
- package/host-builder/data/examples/session.ts +52 -0
- package/host-builder/data/examples/side-call.ts +64 -0
- package/host-builder/data/examples/signal-agent.ts +9 -0
- package/host-builder/data/examples/skills-progressive-disclosure.ts +88 -0
- package/host-builder/data/examples/skills.ts +19 -0
- package/host-builder/data/examples/sqlite-resume.ts +147 -0
- package/host-builder/data/examples/system-project-prompts.ts +74 -0
- package/host-builder/data/examples/system-prompts.ts +19 -0
- package/host-builder/data/examples/telegram-agent.ts +30 -0
- package/host-builder/data/examples/tool-narrowing-planes.ts +99 -0
- package/host-builder/data/examples/tools.ts +29 -0
- package/host-builder/data/examples/validator.ts +67 -0
- package/host-builder/data/examples/web-no-key.ts +85 -0
- package/host-builder/data/examples/web-research.ts +35 -0
- package/host-builder/data/examples/workflow-event-sink.ts +114 -0
- package/host-builder/data/examples/workflow-loop.ts +93 -0
- package/host-builder/data/examples/workflow-multimodal-document.ts +94 -0
- package/host-builder/data/examples/workflow-parallel-research.ts +111 -0
- package/host-builder/data/examples/workflow-postgres-resume.ts +75 -0
- package/host-builder/data/examples/workflow-research-and-review.ts +98 -0
- package/host-builder/data/examples/workflow-sqlite-resume.ts +163 -0
- package/host-builder/data/examples.json +130 -0
- package/host-builder/skills/prism-host-builder/SKILL.md +85 -0
- package/host-builder/skills/prism-host-builder/references/choosing.md +101 -0
- package/host-builder/skills/prism-host-builder/references/layout.md +52 -0
- package/host-builder/skills/prism-host-builder/references/pitfalls.md +50 -0
- package/host-builder/skills/prism-host-builder/references/recipes.md +129 -0
- package/host-builder/skills/prism-host-builder/references/security.md +52 -0
- package/host-builder/skills/prism-host-builder/references/upgrading.md +67 -0
- package/host-builder/skills/prism-host-builder/references/verification.md +108 -0
- package/host-builder/src/check/child.ts +108 -0
- package/host-builder/src/check/conformance.ts +13 -0
- package/host-builder/src/check/inspect.ts +20 -0
- package/host-builder/src/check/manifest.ts +23 -0
- package/host-builder/src/check/runner.ts +181 -0
- package/host-builder/src/check/targets.ts +65 -0
- package/host-builder/src/errors.ts +35 -0
- package/host-builder/src/index.ts +25 -0
- package/host-builder/src/indexes.ts +145 -0
- package/host-builder/src/install/install.ts +400 -0
- package/host-builder/src/install/targets.ts +50 -0
- package/host-builder/src/lookup/api.ts +127 -0
- package/host-builder/src/lookup/bound.ts +38 -0
- package/host-builder/src/lookup/docs.ts +238 -0
- package/host-builder/src/lookup/errors.ts +92 -0
- package/host-builder/src/lookup/examples.ts +117 -0
- package/host-builder/src/lookup/text.ts +60 -0
- package/host-builder/src/server.ts +300 -0
- package/host-builder/src/version.ts +38 -0
- package/mcp-server/README.md +219 -0
- package/mcp-server/src/bounds.ts +23 -0
- package/mcp-server/src/host.ts +31 -0
- package/mcp-server/src/index.ts +27 -0
- package/mcp-server/src/serve-tools.ts +408 -0
- package/mcp-server/src/serve.ts +329 -0
- package/mcp-server/src/server.ts +331 -0
- package/mcp-server/src/session-map.ts +47 -0
- package/mcp-server/src/skill-resources.ts +85 -0
- package/mcp-server/src/types.ts +215 -0
- package/package.json +130 -4
- package/remote/README.md +244 -0
- package/remote/src/attribution.ts +22 -0
- package/remote/src/auth.ts +30 -0
- package/remote/src/bun-module.d.ts +34 -0
- package/remote/src/channel.ts +86 -0
- package/remote/src/connect.ts +212 -0
- package/remote/src/dispatcher.ts +282 -0
- package/remote/src/errors.ts +59 -0
- package/remote/src/host-ui.ts +152 -0
- package/remote/src/http-gate.ts +43 -0
- package/remote/src/index.ts +53 -0
- package/remote/src/jsonl.ts +69 -0
- package/remote/src/limits.ts +43 -0
- package/remote/src/mirror.ts +90 -0
- package/remote/src/pending.ts +22 -0
- package/remote/src/protocol.ts +107 -0
- package/remote/src/serve.ts +560 -0
- package/remote/src/session-proxy.ts +219 -0
- package/remote/src/sse.ts +38 -0
- package/remote/src/stdio-client.ts +41 -0
- package/remote/src/testing-fixtures/serve-stdio.ts +32 -0
- package/remote/src/testing.ts +230 -0
- package/remote/src/transport.ts +139 -0
- package/remote/src/transports/client.ts +126 -0
- package/remote/src/transports/network.ts +268 -0
- package/remote/src/wire.ts +244 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import type { EvaluationRecord } from "@arnilo/prism";
|
|
2
|
+
import { type Message, type RunResult, resolveRedactor } from "@arnilo/prism";
|
|
3
|
+
import { EvalError, EvalScoreError } from "./errors.js";
|
|
4
|
+
import { DEFAULT_MAX_STEP_SCORES, DEFAULT_SAMPLE_RATE, HARD_MAX_STEP_SCORES } from "./limits.js";
|
|
5
|
+
import { mulberry32 } from "./scenarios.js";
|
|
6
|
+
import { loadTimeline } from "./timeline.js";
|
|
7
|
+
import type {
|
|
8
|
+
DatasetItem,
|
|
9
|
+
EvaluationTarget,
|
|
10
|
+
EvaluationTelemetrySink,
|
|
11
|
+
ExecutionStep,
|
|
12
|
+
ExecutionTimeline,
|
|
13
|
+
LiveScoreOptions,
|
|
14
|
+
ScoreRunOptions,
|
|
15
|
+
Scorer,
|
|
16
|
+
ScorerRequirement,
|
|
17
|
+
} from "./types.js";
|
|
18
|
+
import { hash32, randomId, redactEvaluationRecord, resolveSampling, shouldSample, toErrorInfo, validateScoreResult } from "./util.js";
|
|
19
|
+
|
|
20
|
+
function emitEvaluation(record: EvaluationRecord, telemetry?: EvaluationTelemetrySink): void {
|
|
21
|
+
if (!telemetry) return;
|
|
22
|
+
try {
|
|
23
|
+
telemetry.handleEvaluation({
|
|
24
|
+
// Simulator failures can precede a run; correlate the standalone event with its record.
|
|
25
|
+
runId: record.runId ?? record.id,
|
|
26
|
+
name: record.scorerId,
|
|
27
|
+
status: record.status,
|
|
28
|
+
...(record.score === undefined ? {} : { score: record.score }),
|
|
29
|
+
hasReason: record.reason !== undefined,
|
|
30
|
+
});
|
|
31
|
+
} catch {
|
|
32
|
+
// Observers cannot change scoring or persistence outcomes.
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function stripInvariant(metadata: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>> {
|
|
37
|
+
if (!("invariant" in metadata) && !("sampling" in metadata)) return metadata;
|
|
38
|
+
const { invariant: _ignored, sampling: _ignoredSampling, ...rest } = metadata;
|
|
39
|
+
return rest;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const REQUIREMENT_ERRORS: Readonly<Record<ScorerRequirement, { readonly code: string; readonly hint: string }>> = {
|
|
43
|
+
timeline: {
|
|
44
|
+
code: "ERR_PRISM_EVAL_TIMELINE_REQUIRED",
|
|
45
|
+
hint: 'set timeline: "metadata" (scoreRun needs a traceResolver or an injectedTimeline; runExperiment and runScenario project while running)',
|
|
46
|
+
},
|
|
47
|
+
trace: { code: "ERR_PRISM_EVAL_TRACE_REQUIRED", hint: "pass a traceResolver" },
|
|
48
|
+
environment: { code: "ERR_PRISM_EVAL_ENVIRONMENT_REQUIRED", hint: "pass environment (runExperiment: toEnvironment)" },
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Throws when a scorer's declared input cannot be supplied. Every entry point runs this once, before
|
|
53
|
+
* any agent run, so a misconfiguration is an error and never a zero score.
|
|
54
|
+
*/
|
|
55
|
+
export function checkRequirements<TInput, TExpected>(
|
|
56
|
+
scorers: readonly Scorer<TInput, TExpected>[],
|
|
57
|
+
available: Readonly<Record<ScorerRequirement, boolean>>,
|
|
58
|
+
): void {
|
|
59
|
+
for (const scorer of scorers) {
|
|
60
|
+
for (const requirement of scorer.requires ?? []) {
|
|
61
|
+
if (available[requirement]) continue;
|
|
62
|
+
const { code, hint } = REQUIREMENT_ERRORS[requirement];
|
|
63
|
+
throw new EvalError(`scorer "${scorer.id}" requires ${requirement}; ${hint}`, code);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function scoreOne<TInput, TExpected>(
|
|
69
|
+
scorer: Scorer<TInput, TExpected>,
|
|
70
|
+
options: ScoreRunOptions<TInput, TExpected>,
|
|
71
|
+
sampled: boolean,
|
|
72
|
+
item?: DatasetItem<TInput, TExpected>,
|
|
73
|
+
target?: EvaluationTarget,
|
|
74
|
+
timeline?: ExecutionTimeline,
|
|
75
|
+
step?: ExecutionStep,
|
|
76
|
+
skipReason?: string,
|
|
77
|
+
): Promise<EvaluationRecord> {
|
|
78
|
+
const invariant = scorer.invariant === true;
|
|
79
|
+
const base = {
|
|
80
|
+
id: randomId("eval"),
|
|
81
|
+
scorerId: scorer.id,
|
|
82
|
+
sampled,
|
|
83
|
+
sessionId: options.result.sessionId,
|
|
84
|
+
runId: options.result.runId,
|
|
85
|
+
traceId: options.traceId,
|
|
86
|
+
datasetId: options.datasetId,
|
|
87
|
+
itemId: options.itemId ?? item?.id,
|
|
88
|
+
experimentId: options.experimentId,
|
|
89
|
+
stepId: step?.id,
|
|
90
|
+
nodeId: step?.kind === "workflow_node" ? step.name : undefined,
|
|
91
|
+
createdAt: new Date().toISOString(),
|
|
92
|
+
metadata: invariant ? { ...options.metadata, invariant: true } : options.metadata,
|
|
93
|
+
...options.ownership,
|
|
94
|
+
} satisfies Omit<EvaluationRecord, "status" | "score" | "reason" | "error">;
|
|
95
|
+
|
|
96
|
+
if (!sampled) {
|
|
97
|
+
return redactEvaluationRecord(
|
|
98
|
+
{ ...base, status: "skipped", ...(skipReason === undefined ? {} : { reason: skipReason }) },
|
|
99
|
+
options.redactor,
|
|
100
|
+
options.secrets,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
options.signal?.throwIfAborted();
|
|
106
|
+
const raw = await scorer.score({
|
|
107
|
+
result: options.result,
|
|
108
|
+
item,
|
|
109
|
+
expected: item?.expected,
|
|
110
|
+
signal: options.signal,
|
|
111
|
+
target,
|
|
112
|
+
timeline,
|
|
113
|
+
...(step === undefined ? {} : { step }),
|
|
114
|
+
environment: options.environment,
|
|
115
|
+
...(options.redactor === undefined ? {} : { redactor: options.redactor }),
|
|
116
|
+
});
|
|
117
|
+
const scored = validateScoreResult(raw);
|
|
118
|
+
return redactEvaluationRecord(
|
|
119
|
+
{
|
|
120
|
+
...base,
|
|
121
|
+
status: "scored",
|
|
122
|
+
score: scored.score,
|
|
123
|
+
reason: scored.reason,
|
|
124
|
+
// The declaration decides invariance; a returned `invariant` key must not.
|
|
125
|
+
metadata: scored.metadata ? { ...(base.metadata ?? {}), ...stripInvariant(scored.metadata) } : base.metadata,
|
|
126
|
+
},
|
|
127
|
+
options.redactor,
|
|
128
|
+
options.secrets,
|
|
129
|
+
);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
return redactEvaluationRecord(
|
|
132
|
+
{
|
|
133
|
+
...base,
|
|
134
|
+
status: "failed",
|
|
135
|
+
error: toErrorInfo(
|
|
136
|
+
error instanceof EvalScoreError ||
|
|
137
|
+
(error instanceof EvalError && (error.code.startsWith("ERR_PRISM_EVAL_JUDGE") || error.code === "ERR_PRISM_EVAL_STATE_BOUNDS"))
|
|
138
|
+
? error
|
|
139
|
+
: new EvalScoreError(error instanceof Error ? error.message : String(error), { cause: error }),
|
|
140
|
+
),
|
|
141
|
+
},
|
|
142
|
+
options.redactor,
|
|
143
|
+
options.secrets,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Score one `RunResult` with the provided scorers. Failures become `failed` records. */
|
|
149
|
+
export async function scoreRun<TInput = unknown, TExpected = unknown>(
|
|
150
|
+
options: ScoreRunOptions<TInput, TExpected>,
|
|
151
|
+
): Promise<readonly EvaluationRecord[]> {
|
|
152
|
+
if (!options.scorers.length) return [];
|
|
153
|
+
const seed = options.seed ?? options.sampling?.seed;
|
|
154
|
+
if (seed !== undefined && !Number.isSafeInteger(seed)) {
|
|
155
|
+
throw new EvalError("seed must be a safe integer", "ERR_PRISM_EVAL_BOUNDS");
|
|
156
|
+
}
|
|
157
|
+
const random =
|
|
158
|
+
options.random ??
|
|
159
|
+
(seed !== undefined ? mulberry32(hash32(seed, options.itemId ?? options.item?.id ?? options.result.runId ?? "", 0)) : undefined);
|
|
160
|
+
const { probability, metadata: samplingMetadata } = resolveSampling(options.result, options.sampling);
|
|
161
|
+
if (probability <= 0) return [];
|
|
162
|
+
const sampled = shouldSample(probability, random);
|
|
163
|
+
const records: EvaluationRecord[] = [];
|
|
164
|
+
const trace =
|
|
165
|
+
sampled && options.traceResolver
|
|
166
|
+
? await options.traceResolver({
|
|
167
|
+
...options.ownership,
|
|
168
|
+
sessionId: options.result.sessionId,
|
|
169
|
+
runId: options.result.runId,
|
|
170
|
+
limits: options.traceLimits,
|
|
171
|
+
redactor: options.redactor,
|
|
172
|
+
secrets: options.secrets,
|
|
173
|
+
signal: options.signal,
|
|
174
|
+
})
|
|
175
|
+
: undefined;
|
|
176
|
+
let timeline = options.injectedTimeline;
|
|
177
|
+
if (!timeline && sampled && options.timeline && options.timeline !== "off" && trace) {
|
|
178
|
+
const { projectTraceTimeline } = await loadTimeline();
|
|
179
|
+
timeline = projectTraceTimeline(trace, {
|
|
180
|
+
content: options.timeline,
|
|
181
|
+
redactor: options.redactor,
|
|
182
|
+
traceId: options.traceId,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
// Resolve the seam once per run: rebuilding a secret redactor per scored step is pure cost.
|
|
186
|
+
const seam = resolveRedactor(options.redactor, options.secrets);
|
|
187
|
+
const metadata = samplingMetadata ? { ...options.metadata, sampling: samplingMetadata } : options.metadata;
|
|
188
|
+
const scoring =
|
|
189
|
+
(seam === undefined || seam === options.redactor) && metadata === options.metadata
|
|
190
|
+
? options
|
|
191
|
+
: { ...options, ...(seam === undefined ? {} : { redactor: seam }), metadata };
|
|
192
|
+
const target: EvaluationTarget = {
|
|
193
|
+
result: options.result,
|
|
194
|
+
...(trace ? { trace } : {}),
|
|
195
|
+
...(timeline ? { timeline } : {}),
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
if (sampled) {
|
|
199
|
+
checkRequirements(options.scorers, {
|
|
200
|
+
timeline: timeline !== undefined,
|
|
201
|
+
trace: trace !== undefined,
|
|
202
|
+
environment: options.environment !== undefined,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (options.forEach) {
|
|
207
|
+
const matchingSteps = (timeline?.steps ?? []).filter((s) => {
|
|
208
|
+
if (options.forEach === "tool") return s.kind === "tool";
|
|
209
|
+
if (options.forEach === "workflow_node") return s.kind === "workflow_node";
|
|
210
|
+
return false;
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
const cap = Math.min(
|
|
214
|
+
options.maxStepScores !== undefined && Number.isInteger(options.maxStepScores) && options.maxStepScores > 0
|
|
215
|
+
? options.maxStepScores
|
|
216
|
+
: DEFAULT_MAX_STEP_SCORES,
|
|
217
|
+
HARD_MAX_STEP_SCORES,
|
|
218
|
+
);
|
|
219
|
+
const stepsToScore = matchingSteps.slice(0, cap);
|
|
220
|
+
|
|
221
|
+
if (!stepsToScore.length) {
|
|
222
|
+
// Nothing matched: one explicit skip per scorer, so "no step ran" never reads as "no failures".
|
|
223
|
+
for (const scorer of options.scorers) {
|
|
224
|
+
const record = await scoreOne(scorer, scoring, false, options.item, target, timeline, undefined, "no matching steps");
|
|
225
|
+
if (options.store) await options.store.append(record);
|
|
226
|
+
emitEvaluation(record, options.telemetry);
|
|
227
|
+
records.push(record);
|
|
228
|
+
}
|
|
229
|
+
return records;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
for (const step of stepsToScore) {
|
|
233
|
+
for (const scorer of options.scorers) {
|
|
234
|
+
const record = await scoreOne(scorer, scoring, sampled, options.item, target, timeline, step);
|
|
235
|
+
if (options.store) await options.store.append(record);
|
|
236
|
+
emitEvaluation(record, options.telemetry);
|
|
237
|
+
records.push(record);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
for (const scorer of options.scorers) {
|
|
242
|
+
const record = await scoreOne(scorer, scoring, sampled, options.item, target, timeline);
|
|
243
|
+
if (options.store) await options.store.append(record);
|
|
244
|
+
emitEvaluation(record, options.telemetry);
|
|
245
|
+
records.push(record);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return records;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Live post-run scoring helper. Returns a promise the host may ignore.
|
|
254
|
+
* Scoring failure never mutates `result` and is attributable via `failed` records / `onError`.
|
|
255
|
+
*/
|
|
256
|
+
export function scoreRunLive<TInput = unknown, TExpected = unknown>(
|
|
257
|
+
result: RunResult,
|
|
258
|
+
options: LiveScoreOptions<TInput, TExpected>,
|
|
259
|
+
): Promise<readonly EvaluationRecord[]> {
|
|
260
|
+
const telemetry = options.telemetry;
|
|
261
|
+
const sampling = options.sampling ?? { rate: DEFAULT_SAMPLE_RATE };
|
|
262
|
+
return scoreRun({
|
|
263
|
+
...options,
|
|
264
|
+
result,
|
|
265
|
+
sampling,
|
|
266
|
+
...(telemetry
|
|
267
|
+
? {
|
|
268
|
+
telemetry: {
|
|
269
|
+
handleEvaluation(evaluation) {
|
|
270
|
+
try {
|
|
271
|
+
telemetry.handleEvaluation(evaluation);
|
|
272
|
+
} catch (error) {
|
|
273
|
+
options.onError?.(error);
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
}
|
|
278
|
+
: {}),
|
|
279
|
+
}).catch((error) => {
|
|
280
|
+
options.onError?.(error);
|
|
281
|
+
return [];
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function defaultToAgentInput(input: unknown): string | Message | readonly Message[] {
|
|
286
|
+
if (typeof input === "string") return input;
|
|
287
|
+
if (isMessage(input)) return input;
|
|
288
|
+
if (Array.isArray(input) && input.every(isMessage)) return input;
|
|
289
|
+
return JSON.stringify(input ?? null);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function isMessage(value: unknown): value is Message {
|
|
293
|
+
return Boolean(value && typeof value === "object" && "role" in value && "content" in value && Array.isArray((value as Message).content));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** Infrastructure failure skips all scorers without inventing an agent result or invoking scorers. */
|
|
297
|
+
export async function skipScorers<TInput, TExpected>(
|
|
298
|
+
options: Omit<ScoreRunOptions<TInput, TExpected>, "result"> & { result?: RunResult },
|
|
299
|
+
reason: string,
|
|
300
|
+
): Promise<EvaluationRecord[]> {
|
|
301
|
+
const records: EvaluationRecord[] = [];
|
|
302
|
+
for (const scorer of options.scorers) {
|
|
303
|
+
const record = redactEvaluationRecord(
|
|
304
|
+
{
|
|
305
|
+
id: randomId("eval"),
|
|
306
|
+
scorerId: scorer.id,
|
|
307
|
+
sampled: false,
|
|
308
|
+
status: "skipped",
|
|
309
|
+
reason,
|
|
310
|
+
sessionId: options.result?.sessionId,
|
|
311
|
+
runId: options.result?.runId,
|
|
312
|
+
datasetId: options.datasetId,
|
|
313
|
+
itemId: options.itemId,
|
|
314
|
+
experimentId: options.experimentId,
|
|
315
|
+
traceId: options.traceId,
|
|
316
|
+
createdAt: new Date().toISOString(),
|
|
317
|
+
...options.ownership,
|
|
318
|
+
metadata: scorer.invariant ? { ...options.metadata, invariant: true } : options.metadata,
|
|
319
|
+
},
|
|
320
|
+
options.redactor,
|
|
321
|
+
options.secrets,
|
|
322
|
+
);
|
|
323
|
+
await options.store?.append(record);
|
|
324
|
+
emitEvaluation(record, options.telemetry);
|
|
325
|
+
records.push(record);
|
|
326
|
+
}
|
|
327
|
+
return records;
|
|
328
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { assertFiniteUnitInterval } from "./util.js";
|
|
2
|
+
import { EvalError } from "./errors.js";
|
|
3
|
+
import type { DefineScorerInput, Scorer } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/** Create a deterministic function scorer. */
|
|
6
|
+
export function defineScorer<TInput = unknown, TExpected = unknown>(
|
|
7
|
+
input: DefineScorerInput<TInput, TExpected>,
|
|
8
|
+
): Scorer<TInput, TExpected> {
|
|
9
|
+
const id = input.id.trim();
|
|
10
|
+
if (!id) throw new EvalError("scorer id is required", "ERR_PRISM_EVAL_SCORER");
|
|
11
|
+
if (input.passThreshold !== undefined) assertFiniteUnitInterval(input.passThreshold, "passThreshold");
|
|
12
|
+
return {
|
|
13
|
+
id,
|
|
14
|
+
judgeModel: input.judgeModel,
|
|
15
|
+
description: input.description,
|
|
16
|
+
requires: input.requires,
|
|
17
|
+
invariant: input.invariant,
|
|
18
|
+
passThreshold: input.passThreshold,
|
|
19
|
+
score: input.score,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { EvalError } from "./errors.js";
|
|
2
|
+
import { defineScorer } from "./scorer.js";
|
|
3
|
+
import type { DatasetItem, Scorer } from "./types.js";
|
|
4
|
+
|
|
5
|
+
export interface StateMatchScorerOptions<TInput = unknown, TExpected = unknown> {
|
|
6
|
+
readonly id?: string;
|
|
7
|
+
readonly expected: unknown | ((item: DatasetItem<TInput, TExpected> | undefined) => unknown);
|
|
8
|
+
readonly select?: (environment: unknown) => unknown;
|
|
9
|
+
readonly mode: "exact" | "subset";
|
|
10
|
+
/** JSON pointers relative to the selected state; array positions are removed without reindexing. */
|
|
11
|
+
readonly ignore?: readonly string[];
|
|
12
|
+
readonly invariant?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const MAX_STATE_BYTES = 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
function snapshot(value: unknown): unknown {
|
|
18
|
+
if (value === undefined) return undefined;
|
|
19
|
+
const json = JSON.stringify(value, (_key, entry: unknown) => {
|
|
20
|
+
if (
|
|
21
|
+
typeof entry === "function" ||
|
|
22
|
+
typeof entry === "symbol" ||
|
|
23
|
+
typeof entry === "bigint" ||
|
|
24
|
+
(typeof entry === "number" && !Number.isFinite(entry))
|
|
25
|
+
) {
|
|
26
|
+
throw new EvalError("state must be JSON serializable", "ERR_PRISM_EVAL_STATE_INVALID");
|
|
27
|
+
}
|
|
28
|
+
return entry;
|
|
29
|
+
});
|
|
30
|
+
if (json === undefined) throw new EvalError("state must be JSON serializable", "ERR_PRISM_EVAL_STATE_INVALID");
|
|
31
|
+
if (Buffer.byteLength(json) > MAX_STATE_BYTES) {
|
|
32
|
+
throw new EvalError("state exceeds 1 MiB", "ERR_PRISM_EVAL_STATE_BOUNDS");
|
|
33
|
+
}
|
|
34
|
+
return JSON.parse(json);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function preview(value: unknown): unknown {
|
|
38
|
+
if (value === undefined) return undefined;
|
|
39
|
+
const json = JSON.stringify(value);
|
|
40
|
+
if (Buffer.byteLength(json) <= 256) return value;
|
|
41
|
+
let text = "";
|
|
42
|
+
for (const char of json) {
|
|
43
|
+
if (Buffer.byteLength(JSON.stringify(`${text + char}…`)) > 256) break;
|
|
44
|
+
text += char;
|
|
45
|
+
}
|
|
46
|
+
return `${text}…`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function removePointer(state: unknown, tokens: readonly string[]): unknown {
|
|
50
|
+
if (!tokens.length) return undefined;
|
|
51
|
+
let parent = state;
|
|
52
|
+
for (const token of tokens.slice(0, -1)) {
|
|
53
|
+
if (!parent || typeof parent !== "object" || !Object.hasOwn(parent, token)) return state;
|
|
54
|
+
parent = (parent as Record<string, unknown>)[token];
|
|
55
|
+
}
|
|
56
|
+
if (parent && typeof parent === "object") delete (parent as Record<string, unknown>)[tokens[tokens.length - 1]!];
|
|
57
|
+
return state;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function createStateMatchScorer<TInput = unknown, TExpected = unknown>(
|
|
61
|
+
options: StateMatchScorerOptions<TInput, TExpected>,
|
|
62
|
+
): Scorer<TInput, TExpected> {
|
|
63
|
+
if (options.mode !== "exact" && options.mode !== "subset") {
|
|
64
|
+
throw new EvalError("state match mode must be exact or subset", "ERR_PRISM_EVAL_SCORER");
|
|
65
|
+
}
|
|
66
|
+
const pointers = (options.ignore ?? []).map((pointer) => {
|
|
67
|
+
if (typeof pointer !== "string" || (pointer !== "" && !pointer.startsWith("/")) || /~(?![01])/u.test(pointer)) {
|
|
68
|
+
throw new EvalError("invalid state ignore JSON pointer", "ERR_PRISM_EVAL_SCORER");
|
|
69
|
+
}
|
|
70
|
+
return pointer === ""
|
|
71
|
+
? []
|
|
72
|
+
: pointer
|
|
73
|
+
.slice(1)
|
|
74
|
+
.split("/")
|
|
75
|
+
.map((token) => token.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
76
|
+
});
|
|
77
|
+
return defineScorer({
|
|
78
|
+
id: options.id ?? "state_match",
|
|
79
|
+
description: `state match (${options.mode})`,
|
|
80
|
+
requires: ["environment"],
|
|
81
|
+
invariant: options.invariant,
|
|
82
|
+
score(input) {
|
|
83
|
+
if (input.environment === undefined) return { score: 0, reason: "no environment available for state scoring" };
|
|
84
|
+
const goal = typeof options.expected === "function" ? options.expected(input.item) : options.expected;
|
|
85
|
+
let expected = snapshot(goal);
|
|
86
|
+
let actual = snapshot(options.select ? options.select(input.environment) : input.environment);
|
|
87
|
+
if (input.redactor) {
|
|
88
|
+
expected = snapshot(input.redactor.redact(expected));
|
|
89
|
+
actual = snapshot(input.redactor.redact(actual));
|
|
90
|
+
}
|
|
91
|
+
for (const tokens of pointers) {
|
|
92
|
+
expected = removePointer(expected, tokens);
|
|
93
|
+
actual = removePointer(actual, tokens);
|
|
94
|
+
}
|
|
95
|
+
const diff: { path: string; expected: unknown; actual: unknown }[] = [];
|
|
96
|
+
const mismatch = (path: string, expected: unknown, actual: unknown) => {
|
|
97
|
+
diff.push({ path, expected: preview(expected), actual: preview(actual) });
|
|
98
|
+
};
|
|
99
|
+
const compare = (expected: unknown, actual: unknown, path: string): void => {
|
|
100
|
+
if (diff.length >= 32 || expected === actual) return;
|
|
101
|
+
if (
|
|
102
|
+
!expected ||
|
|
103
|
+
!actual ||
|
|
104
|
+
typeof expected !== "object" ||
|
|
105
|
+
typeof actual !== "object" ||
|
|
106
|
+
Array.isArray(expected) !== Array.isArray(actual)
|
|
107
|
+
) {
|
|
108
|
+
mismatch(path, expected, actual);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const goal = expected as Record<string, unknown>;
|
|
112
|
+
const state = actual as Record<string, unknown>;
|
|
113
|
+
for (const key of Object.keys(goal)) {
|
|
114
|
+
if (diff.length >= 32) return;
|
|
115
|
+
const child = `${path}/${key.replace(/~/g, "~0").replace(/\//g, "~1")}`;
|
|
116
|
+
if (!Object.hasOwn(state, key)) mismatch(child, goal[key], undefined);
|
|
117
|
+
else compare(goal[key], state[key], child);
|
|
118
|
+
}
|
|
119
|
+
if (options.mode === "exact") {
|
|
120
|
+
for (const key of Object.keys(state)) {
|
|
121
|
+
if (diff.length >= 32) return;
|
|
122
|
+
if (!Object.hasOwn(goal, key)) mismatch(`${path}/${key.replace(/~/g, "~0").replace(/\//g, "~1")}`, undefined, state[key]);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
compare(expected, actual, "");
|
|
127
|
+
return diff.length ? { score: 0, reason: "state mismatch", metadata: { diff } } : { score: 1 };
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}
|