@reinsjs/eval 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/README.md +55 -0
- package/dist/index.d.ts +518 -0
- package/dist/index.js +888 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# @reinsjs/eval
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- First public release.
|
|
8
|
+
|
|
9
|
+
- **Timeline first.** Append-only event log with `schemaVersion` on every event and fail-closed upcast on read; projection decides what the model sees each turn; replay and fork from the log alone.
|
|
10
|
+
- **A loop you can copy.** `runLoop` as an exported async generator: pause/resume is a return value with a small signed state, bounded retry on transient failures only while nothing has been written, tool pipeline `beforeTool → validate → approval → execute → afterTool`, sub-agent pause bubbling (`Interruption { kind: "subagent" }`).
|
|
11
|
+
- **Brain modules** (`@reinsjs/brain`): perception, compact (with folded-result manifest and `recall`; recommended on after measuring 100% recall at lower token cost on two model families), pins, spill, handoff, memory (`memory_20250818` shape, namespace isolation), approval (deny → ask → allow, fail-closed), budget (five limits, sub-agent usage included).
|
|
12
|
+
- **Lowering** (`@reinsjs/lowering-pi`): Anthropic Messages and OpenAI Responses via pi-ai with a declared loss matrix, trust markers on untrusted content, cache-breakpoint handling.
|
|
13
|
+
- **Serving** (`@reinsjs/server`, `@reinsjs/ui-agui`): Web-standard handler with SSE replay from `lastSeq`, per-session run lock, `authorizeSession` (only `true` allows), AG-UI as the first-class UI protocol.
|
|
14
|
+
- **Stores**: SQLite (`node:sqlite` / `bun:sqlite`) and Postgres (`pg` / PGlite), conformance-tested; configurable memory table for per-role isolation.
|
|
15
|
+
- **Tools**: MCP servers as one socket (`@reinsjs/tools-mcp`, tools bound per run, annotations as defaults not permissions); `asTool(agent)` in `reins`.
|
|
16
|
+
- **TanStack AI** middleware (`@reinsjs/adapter-tanstack-ai`): the brain on TanStack's loop with the log as the single source of truth; idempotent client-message import; runtime check for the approval interrupt.
|
|
17
|
+
- **Eval harness** (`@reinsjs/eval`): fixtures from real recordings, recorded tools, arm-vs-arm runner, metrics, the four-rule gate.
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Updated dependencies
|
|
22
|
+
- @reinsjs/core@0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NewRate Limited
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @reinsjs/eval
|
|
2
|
+
|
|
3
|
+
The eval harness [reins](../../README.md) uses to decide its own defaults with numbers instead of taste. Real sessions become fixtures; recorded tools replay deterministically so every arm faces the same world; arms (a baseline, a threshold-only fallback, or a full brain configuration) run side by side on real models; metrics come straight from the timelines; a gate says pass or fail.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add -D @reinsjs/eval
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { compact, perception } from "@reinsjs/brain"
|
|
11
|
+
import { checkGate, noneArm, renderReport, runEval, thresholdArm, withContextWindow } from "@reinsjs/eval"
|
|
12
|
+
import { anthropic } from "@reinsjs/lowering-pi"
|
|
13
|
+
|
|
14
|
+
const bound = anthropic("claude-sonnet-5", { apiKey: process.env.ANTHROPIC_API_KEY ?? "" })
|
|
15
|
+
|
|
16
|
+
const report = await runEval({
|
|
17
|
+
fixtures, // EvalFixture[]: seed log + task + graders (+ `maxResumes`, default 0: a budget pause counts as "did not finish"); see `recording.ts` for turning a real JSONL into one
|
|
18
|
+
arms: [noneArm(), thresholdArm(), { name: "compact", sockets: [compact(), perception()] }],
|
|
19
|
+
lowering: withContextWindow(bound.lowering, 32_000), // shrink what the mechanisms *believe* the window is
|
|
20
|
+
model: bound.model,
|
|
21
|
+
repeats: 3,
|
|
22
|
+
})
|
|
23
|
+
console.log(renderReport(report))
|
|
24
|
+
const gate = checkGate(report, { candidate: "compact", reference: "threshold" })
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The reference CLI is `examples/eval/run.ts` in the repository; copy from it rather than from memory.
|
|
28
|
+
|
|
29
|
+
## Pieces
|
|
30
|
+
|
|
31
|
+
| module | what |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `jsonl` | read/write event JSONL; reading goes through the schema registry (unknown or unupgradable events fail loudly) |
|
|
34
|
+
| `recording` | turn a real recording into fixture material: re-attach spilled blobs, redact deterministically |
|
|
35
|
+
| `recorded-tools` | tools that answer from the recording so arms compare like with like |
|
|
36
|
+
| `metrics` | pure functions over a timeline: tokens and cache hits, turns / tool calls / duplicate calls, compaction count and streaks, governance decay windows |
|
|
37
|
+
| `arms` | `noneArm`, `thresholdArm`, `withCapabilities` / `withContextWindow` lowering wrappers; brain-driven arms are assembled by the caller from `@reinsjs/brain` |
|
|
38
|
+
| `runner` | fixture × arm × repeat runner with approval auto-answering, budget resumes, handoff following and a forked probe question at the end |
|
|
39
|
+
| `gate` | the four hard rules from the PRD (§7 threshold 2 / P8): recall, token cost, governance, no regressions |
|
|
40
|
+
| `report` | Markdown tables |
|
|
41
|
+
|
|
42
|
+
Zero `node:*`: file I/O and the CLI live in the caller (`examples/eval` in the repository is the reference CLI and holds the fixtures reins itself is measured on).
|
|
43
|
+
|
|
44
|
+
## Read the numbers correctly
|
|
45
|
+
|
|
46
|
+
- `withContextWindow` changes only what the mechanisms are told; the real window is untouched. It measures *how the mechanism behaves in a narrow window*, not *what overflow does*.
|
|
47
|
+
- The probe question keeps brain tools (an arm with `compact` can `recall`, one with `spill` can `fetch_blob`) and drops host tools — so the recall cell is intentionally not the same exam for a baseline arm.
|
|
48
|
+
- The governance rule compares the candidate against itself before and after its first compaction; two of the four gate rules can pass on "no information".
|
|
49
|
+
- Behavioural defects only show on real models. Scripted lowerings prove mechanics, not that a model will use them.
|
|
50
|
+
|
|
51
|
+
## Documentation
|
|
52
|
+
|
|
53
|
+
`docs/技术方案.md` §13, `docs/模块盘点/eval.md`, the E1–E4 reports under `docs/归档/` — in Chinese.
|
|
54
|
+
|
|
55
|
+
MIT © 2026 NewRate Limited.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import { Interruption, Socket, LoopConfig, ContentPart, Event, Tool, CoreEventOf, RunResult, ModelRef, Lowering, LoweringCapabilities, EventSchemaRegistry, ToolContext, ToolResult, Stores } from '@reinsjs/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* eval 的数据形状(技术方案 §13)。
|
|
5
|
+
*
|
|
6
|
+
* fixture = 一段事件日志(种子历史 + 录下的工具结果)+ 任务目标 + 评分器。
|
|
7
|
+
* 三组对照(无脑子 / 纯阈值 / 模型自决)是三个"臂"(arm):同一个 fixture、同一个模型,只换循环配置。
|
|
8
|
+
* 指标全部从跑完的时间线算出来(纯函数),judge 与执行分离:模型下的题由另一个模型或确定性规则来判。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
interface EvalTask {
|
|
12
|
+
/** 任务陈述:作为主会话的第一条 user_message */
|
|
13
|
+
input: string | ContentPart[];
|
|
14
|
+
/** 宿主系统提示(角色、契约);臂可以在它后面追加 */
|
|
15
|
+
systemPrompt?: string;
|
|
16
|
+
/**
|
|
17
|
+
* 任务开始前已有的历史(如脱敏录像的前缀),seq 必须从 1 起连续。
|
|
18
|
+
* runner 只换 sessionId 后原样 append,事件 id 保留(parentId / pinsKept 等会话内引用不断)。
|
|
19
|
+
*/
|
|
20
|
+
seed?: readonly Event[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 预埋事实:任务里出现过(在种子历史、工具结果或用户消息里)的一个具体信息。
|
|
24
|
+
* 跑完后在**分叉出的会话**里向模型提问,看它在整理之后还记得多少 —— 这就是"关键信息召回"。
|
|
25
|
+
*/
|
|
26
|
+
interface PlantedFact {
|
|
27
|
+
id: string;
|
|
28
|
+
question: string;
|
|
29
|
+
/**
|
|
30
|
+
* 确定性判定:字符串(不分大小写的包含)、正则、或函数(返回 0~1 的分数或布尔)。
|
|
31
|
+
* 缺省则交给 runner 的 judge(LLM judge);两者都没有即报错,不静默给 0 分。
|
|
32
|
+
*/
|
|
33
|
+
expect?: string | RegExp | ((answer: string) => boolean | number);
|
|
34
|
+
}
|
|
35
|
+
/** 模型的一个"动作":一次工具调用或一段正文,治理衰减按动作数算违规率 */
|
|
36
|
+
type ModelAction = CoreEventOf<"core.tool_call"> | CoreEventOf<"core.model_text">;
|
|
37
|
+
/**
|
|
38
|
+
* 预埋约束:任务或 pin 里写明的一条规矩("只操作测试广告主 X"、"不要调 deploy")。
|
|
39
|
+
* violates 对每个模型动作判一次;违规率按"第一次整理之前 / 之后"分开算(§9.3 治理衰减)。
|
|
40
|
+
*/
|
|
41
|
+
interface PlantedConstraint {
|
|
42
|
+
id: string;
|
|
43
|
+
/** 约束原文,只进报告 */
|
|
44
|
+
text?: string;
|
|
45
|
+
violates(action: ModelAction, ctx: {
|
|
46
|
+
timeline: readonly Event[];
|
|
47
|
+
}): boolean;
|
|
48
|
+
}
|
|
49
|
+
type ApprovalInterruption = Extract<Interruption, {
|
|
50
|
+
kind: "approval";
|
|
51
|
+
}>;
|
|
52
|
+
interface EvalFixture {
|
|
53
|
+
id: string;
|
|
54
|
+
description?: string;
|
|
55
|
+
task: EvalTask;
|
|
56
|
+
/** 任务可用的工具;长任务通常用 recordedTools() 从录像回放,保证各臂看到同样的世界 */
|
|
57
|
+
tools: readonly Tool[];
|
|
58
|
+
facts?: readonly PlantedFact[];
|
|
59
|
+
constraints?: readonly PlantedConstraint[];
|
|
60
|
+
/**
|
|
61
|
+
* 完成判定:看整条时间线(最后一段正文、调过哪些工具、工具结果里的状态……)给 0~1 或布尔。
|
|
62
|
+
* 这是确定性评分器;要 LLM judge 判完成度就自己在这里调 judge。
|
|
63
|
+
*/
|
|
64
|
+
completion(outcome: EvalOutcomeDraft): boolean | number | Promise<boolean | number>;
|
|
65
|
+
/** 审批由 runner 代人回答;缺省全批。返回 false 即拒绝(模型会看到被拒的 tool_result) */
|
|
66
|
+
approve?(interruption: ApprovalInterruption, outcome: EvalOutcomeDraft): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* 覆盖模型的上下文窗口(token)。把窗口缩小,中等长度的任务也会触发整理 ——
|
|
69
|
+
* 比真的烧到 200k 便宜得多,而且三个臂在同样的窄窗口下比较才公平。
|
|
70
|
+
*/
|
|
71
|
+
contextWindow?: number;
|
|
72
|
+
/** 单次 run 的轮数上限(LoopConfig.maxTurns),缺省 100 */
|
|
73
|
+
maxTurns?: number;
|
|
74
|
+
/** 预算暂停(budget 模块 / compact 连续上限 / maxTurns)后最多续跑几次;缺省 0:暂停即视为没跑完 */
|
|
75
|
+
maxResumes?: number;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 对照组的一臂:只描述"循环怎么配",与 fixture、模型无关。
|
|
79
|
+
* 内置 noneArm(无脑子、无阈值兜底)与 thresholdArm(只有 core 的阈值裁剪);
|
|
80
|
+
* "模型自决"臂由调用方用 @reinsjs/brain 组(本包只依赖 core)。
|
|
81
|
+
*/
|
|
82
|
+
interface EvalArm {
|
|
83
|
+
name: string;
|
|
84
|
+
sockets?: readonly Socket[];
|
|
85
|
+
projection?: LoopConfig["projection"];
|
|
86
|
+
/** 给这一臂追加的系统提示片段(如给基线一段与脑子等价的文字说明),排在 fixture 的系统提示之后 */
|
|
87
|
+
systemPrompt?: string;
|
|
88
|
+
}
|
|
89
|
+
/** LLM judge:与被测模型分离,只回答"这个答案算不算对",返回 0~1 */
|
|
90
|
+
type Judge = (input: {
|
|
91
|
+
fixtureId: string;
|
|
92
|
+
fact: PlantedFact;
|
|
93
|
+
answer: string;
|
|
94
|
+
}) => Promise<number | boolean> | number | boolean;
|
|
95
|
+
interface TokenTotals {
|
|
96
|
+
input: number;
|
|
97
|
+
output: number;
|
|
98
|
+
cacheRead: number;
|
|
99
|
+
cacheWrite: number;
|
|
100
|
+
/** input + output + cacheRead + cacheWrite */
|
|
101
|
+
total: number;
|
|
102
|
+
}
|
|
103
|
+
interface CompactionCounts {
|
|
104
|
+
model: number;
|
|
105
|
+
threshold: number;
|
|
106
|
+
/** 最长的一段"连续含整理的模型轮"里的整理次数(§9.2 连续上限看的就是它) */
|
|
107
|
+
maxConsecutive: number;
|
|
108
|
+
}
|
|
109
|
+
interface ViolationWindow {
|
|
110
|
+
/** 该窗口内的模型动作数(tool_call + model_text) */
|
|
111
|
+
actions: number;
|
|
112
|
+
violations: number;
|
|
113
|
+
/** violations / actions;没有动作时为 0 */
|
|
114
|
+
rate: number;
|
|
115
|
+
}
|
|
116
|
+
interface EvalMetrics {
|
|
117
|
+
/** 最终状态:done 才算跑完;paused / error / handoff 都如实记 */
|
|
118
|
+
status: RunResult["status"];
|
|
119
|
+
/** 完成度 0~1(fixture.completion 的结果) */
|
|
120
|
+
completed: number;
|
|
121
|
+
tokens: TokenTotals;
|
|
122
|
+
/** cacheRead / (input + cacheRead + cacheWrite);一次请求都没记用量时为 undefined */
|
|
123
|
+
cacheHitRate?: number;
|
|
124
|
+
/** 模型请求次数(从日志按轮切) */
|
|
125
|
+
turns: number;
|
|
126
|
+
toolCalls: number;
|
|
127
|
+
/** isError 的工具结果数 */
|
|
128
|
+
toolErrors: number;
|
|
129
|
+
/** 同名同参的工具调用重复出现的次数(死循环计数:第二次起每次记 1) */
|
|
130
|
+
repeatedToolCalls: number;
|
|
131
|
+
compactions: CompactionCounts;
|
|
132
|
+
/** 治理衰减:第一次整理(任何 decidedBy)之前 / 之后的违规率。没整理过则 after 全零 */
|
|
133
|
+
violations: {
|
|
134
|
+
before: ViolationWindow;
|
|
135
|
+
after: ViolationWindow;
|
|
136
|
+
};
|
|
137
|
+
/** 关键信息召回:各预埋事实得分的均值;没有预埋事实时为 undefined */
|
|
138
|
+
recall?: number;
|
|
139
|
+
/** runner 量的墙钟(含审批续跑之间的开销,不含探针问答) */
|
|
140
|
+
wallMs: number;
|
|
141
|
+
/** 探针问答自己花的 token,不计入 tokens */
|
|
142
|
+
probeTokens: TokenTotals;
|
|
143
|
+
}
|
|
144
|
+
interface FactResult {
|
|
145
|
+
id: string;
|
|
146
|
+
question: string;
|
|
147
|
+
/** 模型在分叉会话里的回答(正文拼接;没有正文时退回最后一段 thinking) */
|
|
148
|
+
answer: string;
|
|
149
|
+
/** 回答取自哪里:text 正文、thinking(正文为空)、none(什么都没输出) */
|
|
150
|
+
answerFrom: "text" | "thinking" | "none";
|
|
151
|
+
/** 0~1 */
|
|
152
|
+
score: number;
|
|
153
|
+
/** 判定来源 */
|
|
154
|
+
gradedBy: "expect" | "judge";
|
|
155
|
+
}
|
|
156
|
+
/** completion / approve 回调看到的中间态:跑完了但还没算指标 */
|
|
157
|
+
interface EvalOutcomeDraft {
|
|
158
|
+
fixtureId: string;
|
|
159
|
+
arm: string;
|
|
160
|
+
repeat: number;
|
|
161
|
+
/** 主会话链:handoff 会接到新会话,按先后排列 */
|
|
162
|
+
sessionIds: string[];
|
|
163
|
+
/** 各会话的完整时间线,与 sessionIds 一一对应 */
|
|
164
|
+
timelines: Event[][];
|
|
165
|
+
/** 全链拼起来的时间线(按会话先后再按 seq),评分器最常用它 */
|
|
166
|
+
timeline: Event[];
|
|
167
|
+
/** 本次真正跑出来的事件:去掉种子历史后的 timeline。指标(token、轮、动作、违规)只看它 */
|
|
168
|
+
fresh: Event[];
|
|
169
|
+
/** 最后一次 run 的返回 */
|
|
170
|
+
result: RunResult;
|
|
171
|
+
/** 最后一段模型正文(通常是给用户的汇报) */
|
|
172
|
+
finalText: string;
|
|
173
|
+
}
|
|
174
|
+
interface EvalOutcome extends EvalOutcomeDraft {
|
|
175
|
+
metrics: EvalMetrics;
|
|
176
|
+
facts: FactResult[];
|
|
177
|
+
}
|
|
178
|
+
/** 一臂在全部 fixture × repeat 上的均值 */
|
|
179
|
+
interface ArmSummary {
|
|
180
|
+
arm: string;
|
|
181
|
+
runs: number;
|
|
182
|
+
/** done 的比例 */
|
|
183
|
+
finishedRate: number;
|
|
184
|
+
completion: number;
|
|
185
|
+
tokens: TokenTotals;
|
|
186
|
+
cacheHitRate?: number;
|
|
187
|
+
recall?: number;
|
|
188
|
+
violations: {
|
|
189
|
+
before: number;
|
|
190
|
+
after: number;
|
|
191
|
+
};
|
|
192
|
+
compactions: CompactionCounts;
|
|
193
|
+
turns: number;
|
|
194
|
+
toolCalls: number;
|
|
195
|
+
repeatedToolCalls: number;
|
|
196
|
+
wallMs: number;
|
|
197
|
+
}
|
|
198
|
+
interface EvalReport {
|
|
199
|
+
model: ModelRef;
|
|
200
|
+
startedAt: number;
|
|
201
|
+
finishedAt: number;
|
|
202
|
+
outcomes: EvalOutcome[];
|
|
203
|
+
/** 按臂名索引 */
|
|
204
|
+
summary: Record<string, ArmSummary>;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 内置的两个对照臂与"缩窗口"包装(§13 对照组:无脑子、纯阈值压缩、模型自决)。
|
|
209
|
+
*
|
|
210
|
+
* - noneArm:什么都不装,连 core 的阈值裁剪也拆掉。窗口装不下就让降级层报错 —— 这是"不管"的真实代价。
|
|
211
|
+
* - thresholdArm:不装脑子,只留 core 投影链缺省的 budgetTruncate(机械折叠 + 兜底摘要)。PRD §7 门槛 2 的对照基线。
|
|
212
|
+
* - 模型自决臂请用 @reinsjs/brain 自己组(本包只依赖 core):
|
|
213
|
+
* { name: "brain", sockets: [perception(), compact(), pins(), spill(), budget({...})] }
|
|
214
|
+
*
|
|
215
|
+
* withContextWindow:eval 里把窗口缩到几千 token,让中等长度任务也触发整理。它只改 capabilities,
|
|
216
|
+
* 请求本身不变 —— 模型实际还是那个大窗口,只是循环、投影、感知都按小窗口行事。
|
|
217
|
+
*/
|
|
218
|
+
|
|
219
|
+
declare function noneArm(name?: string): EvalArm;
|
|
220
|
+
declare function thresholdArm(name?: string): EvalArm;
|
|
221
|
+
/** 包一层 Lowering,只覆盖 capabilities 的若干字段(常用 contextWindow);其余调用原样透传 */
|
|
222
|
+
declare function withCapabilities<P>(lowering: Lowering<P>, override: Partial<LoweringCapabilities> | ((caps: LoweringCapabilities, model: ModelRef) => Partial<LoweringCapabilities>)): Lowering<P>;
|
|
223
|
+
declare function withContextWindow<P>(lowering: Lowering<P>, contextWindow: number): Lowering<P>;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 门禁(P8"无 eval 不默认开"、PRD §7 门槛 2):候选臂(模型自决)对照基线臂(纯阈值)的四条硬规则。
|
|
227
|
+
*
|
|
228
|
+
* 1. token 不多于基线的 100%(可配比例)
|
|
229
|
+
* 2. 任务完成率不低于基线
|
|
230
|
+
* 3. 关键信息召回不低于基线
|
|
231
|
+
* 4. 压缩前后约束违规率不上升(候选臂自己整理前 vs 整理后)
|
|
232
|
+
*
|
|
233
|
+
* 只给结论与数字,怎么用(CI 红绿、默认开关)由调用方定。
|
|
234
|
+
*/
|
|
235
|
+
|
|
236
|
+
interface GateOptions {
|
|
237
|
+
/** 基线臂名,通常 "threshold" */
|
|
238
|
+
reference: string;
|
|
239
|
+
/** 候选臂名,通常 "brain" */
|
|
240
|
+
candidate: string;
|
|
241
|
+
/** 候选 token / 基线 token 的上限,缺省 1(不多于 100%) */
|
|
242
|
+
tokenRatioMax?: number;
|
|
243
|
+
}
|
|
244
|
+
interface GateCheck {
|
|
245
|
+
name: "tokens" | "completion" | "recall" | "governance";
|
|
246
|
+
rule: string;
|
|
247
|
+
reference?: number;
|
|
248
|
+
candidate?: number;
|
|
249
|
+
pass: boolean;
|
|
250
|
+
/** 无法判定时的说明(如没有预埋事实) */
|
|
251
|
+
note?: string;
|
|
252
|
+
}
|
|
253
|
+
interface GateResult {
|
|
254
|
+
pass: boolean;
|
|
255
|
+
checks: GateCheck[];
|
|
256
|
+
reference: ArmSummary;
|
|
257
|
+
candidate: ArmSummary;
|
|
258
|
+
}
|
|
259
|
+
declare function checkGate(report: EvalReport, opts: GateOptions): GateResult;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* 事件的 JSONL 读写:一行一个事件。这是 fixture 文件与录像(examples 下各 recordings 目录)共用的格式。
|
|
263
|
+
*
|
|
264
|
+
* 读是 fail-closed 的(P9):每一行都过注册表 `read`,旧版本升级到当前形状,未登记的类型或未来版本直接抛,
|
|
265
|
+
* 报错带行号。只做字符串 ↔ 事件,不碰文件系统(本包零 node:*),读文件由调用方负责。
|
|
266
|
+
*/
|
|
267
|
+
|
|
268
|
+
interface ParseJsonlOptions {
|
|
269
|
+
/** 有 ext.* 事件时传宿主注册表;缺省内置 core 注册表 */
|
|
270
|
+
registry?: EventSchemaRegistry;
|
|
271
|
+
}
|
|
272
|
+
declare class JsonlParseError extends Error {
|
|
273
|
+
readonly line: number;
|
|
274
|
+
constructor(line: number, message: string, options?: {
|
|
275
|
+
cause?: unknown;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/** 解析 JSONL 文本为事件数组(按文件顺序)。空行与只有空白的行跳过 */
|
|
279
|
+
declare function parseEventsJsonl(text: string, opts?: ParseJsonlOptions): Event[];
|
|
280
|
+
/** 事件数组 → JSONL 文本(末尾带换行,便于追加) */
|
|
281
|
+
declare function toEventsJsonl(events: readonly Event[]): string;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* 指标(§13):全部从跑完的时间线算出来,纯函数、不看进程内状态。
|
|
285
|
+
* 这样同一条日志今天算和三个月后算结果一样;录像也能直接拿来算(不必重跑)。
|
|
286
|
+
*
|
|
287
|
+
* "轮"的口径:一个模型轮 = 一段连续的模型输出(thinking / text / tool_call,见 core isModelOutput)
|
|
288
|
+
* + 到下一段模型输出之前发生的一切(工具结果、用量、整理、暂停……)。第一轮之前的事件归入第一轮。
|
|
289
|
+
* 连续整理数按这个口径数:连续若干轮都含 compaction,这一串里 compaction 的总数取最大值。
|
|
290
|
+
*/
|
|
291
|
+
|
|
292
|
+
declare const ZERO_TOKENS: Readonly<TokenTotals>;
|
|
293
|
+
/** 把时间线里全部 budget_usage 的用量加起来 */
|
|
294
|
+
declare function sumTokens(timeline: readonly Event[]): TokenTotals;
|
|
295
|
+
declare function addTokens(a: TokenTotals, b: TokenTotals): TokenTotals;
|
|
296
|
+
/** cacheRead / 送进模型的上下文总量;没有任何输入 token 时为 undefined(没请求过) */
|
|
297
|
+
/**
|
|
298
|
+
* 计费等价 token:缓存读按 0.1× 折算(Anthropic 与 DeepSeek 的缓存命中价都是标准输入价的一成),其余按 1×。
|
|
299
|
+
* `total` 把缓存读按 1× 计,反映的是"模型每轮读了多少";这个数反映的是账单。门禁用 total(PRD §7 原文是 token 不多于),报告两个都给。
|
|
300
|
+
*/
|
|
301
|
+
declare function billableTokens(t: TokenTotals): number;
|
|
302
|
+
declare function cacheHitRateOf(t: TokenTotals): number | undefined;
|
|
303
|
+
interface ModelTurn {
|
|
304
|
+
output: Event[];
|
|
305
|
+
/** 输出之后、下一轮输出之前的一切(第一轮还包含轮前的 preamble) */
|
|
306
|
+
aftermath: Event[];
|
|
307
|
+
}
|
|
308
|
+
/** 按上文口径切轮。没有任何模型输出的时间线得到空数组 */
|
|
309
|
+
declare function splitModelTurns(timeline: readonly Event[]): ModelTurn[];
|
|
310
|
+
declare function countCompactions(timeline: readonly Event[]): CompactionCounts;
|
|
311
|
+
/** 同名同参的 tool_call 第二次起每次记 1(死循环计数) */
|
|
312
|
+
declare function countRepeatedToolCalls(timeline: readonly Event[]): number;
|
|
313
|
+
declare function isModelAction(e: Event): e is ModelAction;
|
|
314
|
+
/**
|
|
315
|
+
* 治理衰减:按第一次 compaction 的位置把模型动作分成"整理前 / 整理后"两窗,各算违规率。
|
|
316
|
+
* 位置按数组顺序而不是 seq(跨会话拼接的时间线 seq 会重新从 1 起)。
|
|
317
|
+
*/
|
|
318
|
+
declare function measureViolations(timeline: readonly Event[], constraints: readonly PlantedConstraint[]): {
|
|
319
|
+
before: ViolationWindow;
|
|
320
|
+
after: ViolationWindow;
|
|
321
|
+
};
|
|
322
|
+
/** 时间线本身能算出的那部分指标;完成度、召回、墙钟、探针用量由 runner 补 */
|
|
323
|
+
type TimelineMetrics = Pick<EvalMetrics, "tokens" | "cacheHitRate" | "turns" | "toolCalls" | "toolErrors" | "repeatedToolCalls" | "compactions" | "violations">;
|
|
324
|
+
declare function measureTimeline(timeline: readonly Event[], constraints?: readonly PlantedConstraint[]): TimelineMetrics;
|
|
325
|
+
/** 一段时间线里最后一段连续的模型正文(给用户的收尾汇报) */
|
|
326
|
+
declare function finalTextOf(timeline: readonly Event[]): string;
|
|
327
|
+
/** 一组数的均值;空数组为 0 */
|
|
328
|
+
declare function mean(values: readonly number[]): number;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 从录像回放的确定性工具(§13"fixture = 一段事件日志"的落地)。
|
|
332
|
+
*
|
|
333
|
+
* 真实长任务录下来之后,工具那一侧的世界(报表、列表、命令终态)就冻结在日志里了:
|
|
334
|
+
* 模型再问同一个问题,就给它当时的答案。这样三个臂面对的是同一个世界,差异只来自脑子;
|
|
335
|
+
* 也不用为了跑 eval 再去碰真服务(AdRate 写操作有副作用、还要审批)。
|
|
336
|
+
*
|
|
337
|
+
* 匹配规则(按顺序):
|
|
338
|
+
* 1. 同名 + 入参逐字相同(键排序后的 JSON)→ 录下的那次结果;同参多次录过的按顺序轮着给,用完了回到第一次
|
|
339
|
+
* 2. 没匹配上 → `fallback(name, args)`(fixture 作者补的合成答案),有就用
|
|
340
|
+
* 3. 还没有 → `sequence: true` 时给该工具下一条没用过的录像结果(模型换了写法但意图相近的场景)
|
|
341
|
+
* 4. 都没有 → isError 结果,告诉模型这个入参没有可用的数据
|
|
342
|
+
*
|
|
343
|
+
* 录像里已外溢(`spilled`)的结果,日志里只剩预览与 blob id,全文在原会话的 BlobStore 里;
|
|
344
|
+
* 回放时原样给预览(blob 不在),`stats.spilled` 记数,fixture 作者要么录制时关外溢、要么用 fallback 补全文。
|
|
345
|
+
*/
|
|
346
|
+
|
|
347
|
+
interface RecordedToolSpec {
|
|
348
|
+
description?: string;
|
|
349
|
+
inputSchema?: Record<string, unknown>;
|
|
350
|
+
risk?: Tool["risk"];
|
|
351
|
+
needsApproval?: Tool["needsApproval"];
|
|
352
|
+
/** 结果处置(如 spill):只有装了 spill 模块的臂会消费,无脑子臂照样拿全文 */
|
|
353
|
+
resultPolicy?: Tool["resultPolicy"];
|
|
354
|
+
}
|
|
355
|
+
interface RecordedToolsOptions {
|
|
356
|
+
/** 各工具的模型可见声明;录像里没有 schema,缺省给一个宽松的 object 与一句通用说明 */
|
|
357
|
+
specs?: Record<string, RecordedToolSpec>;
|
|
358
|
+
/** 没有逐字匹配时的合成答案;返回 undefined 表示不补 */
|
|
359
|
+
fallback?: (name: string, args: unknown, ctx: ToolContext) => Promise<ToolResult | undefined> | ToolResult | undefined;
|
|
360
|
+
/** 允许按顺序给同名的下一条没用过的录像结果(缺省 false:宁可告诉模型没数据,也不给错误的答案) */
|
|
361
|
+
sequence?: boolean;
|
|
362
|
+
/** 只为这些工具建回放(缺省录像里出现过的全部) */
|
|
363
|
+
only?: readonly string[];
|
|
364
|
+
}
|
|
365
|
+
interface RecordedToolset {
|
|
366
|
+
tools: Tool[];
|
|
367
|
+
stats: {
|
|
368
|
+
/** 录像里配对成功的 tool_call → tool_result 次数 */
|
|
369
|
+
pairs: number;
|
|
370
|
+
/** 每个工具录了几次 */
|
|
371
|
+
byName: Record<string, number>;
|
|
372
|
+
/** 录像里外溢过的结果数(回放只剩预览) */
|
|
373
|
+
spilled: number;
|
|
374
|
+
/** 没有结果的 tool_call(录制时被拒 / 暂停未续跑),不进回放 */
|
|
375
|
+
unanswered: number;
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
/** 键排序后的稳定 JSON,让 {a:1,b:2} 与 {b:2,a:1} 视为同一入参 */
|
|
379
|
+
declare function canonicalArgs(args: unknown): string;
|
|
380
|
+
declare function recordedTools(recording: readonly Event[], opts?: RecordedToolsOptions): RecordedToolset;
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* 把一段真实录像整理成能公开、能回放的 fixture 素材(E2)。两件事:
|
|
384
|
+
*
|
|
385
|
+
* 1. **去外溢(unspill)**:录制时 spill 模块把大结果外溢进 BlobStore,日志里只剩预览与 blob id;
|
|
386
|
+
* 但模型随后用 fetch_blob 分段取回的全文也在日志里。把这些分片按字符偏移拼回去,就能在不碰原 BlobStore 的前提下
|
|
387
|
+
* 还原完整的工具结果 —— fixture 里的"世界"必须是完整的,不然无脑子臂拿到的是一段预览加一个取不回的 id。
|
|
388
|
+
* 拼不齐(模型当时没读完)的原样保留并报告,不硬凑。
|
|
389
|
+
* 2. **脱敏(scrub)**:按"原文 → 别名"表对每条事件做逐字替换,长的先换,替换在 JSON 文本层进行,
|
|
390
|
+
* 所以入参、结果、模型正文、思考里出现的同一个值一起变,回放时模型看到的 id 与它要传给工具的 id 仍然一致。
|
|
391
|
+
* 每条规则命中几次会报告出来(不含原文),零命中的规则说明表写错了。
|
|
392
|
+
*
|
|
393
|
+
* 本文件是纯函数、零 node:*;读写文件与决定"哪些字段算敏感"由调用方(examples/eval 的 build 脚本)负责。
|
|
394
|
+
*/
|
|
395
|
+
|
|
396
|
+
interface UnspillOptions {
|
|
397
|
+
/** 还原后是否把 fetch_blob 的调用 / 结果对从录像里删掉(它们是脑子的动作,不是世界的一部分);缺省 true */
|
|
398
|
+
dropFetchBlob?: boolean;
|
|
399
|
+
/** fetch_blob 工具名;缺省 "fetch_blob" */
|
|
400
|
+
fetchToolName?: string;
|
|
401
|
+
}
|
|
402
|
+
interface UnspillResult {
|
|
403
|
+
events: Event[];
|
|
404
|
+
/** 成功还原全文的 blob id */
|
|
405
|
+
restored: string[];
|
|
406
|
+
/** 分片拼不齐(有缺口或没读到末尾)的 blob id,其 tool_result 原样保留 */
|
|
407
|
+
incomplete: string[];
|
|
408
|
+
/** 删掉的 fetch_blob 调用 / 结果对数 */
|
|
409
|
+
droppedFetches: number;
|
|
410
|
+
}
|
|
411
|
+
interface BlobChunk {
|
|
412
|
+
start: number;
|
|
413
|
+
end: number;
|
|
414
|
+
total: number;
|
|
415
|
+
text: string;
|
|
416
|
+
}
|
|
417
|
+
declare function parseFetchChunk(text: string): (BlobChunk & {
|
|
418
|
+
id: string;
|
|
419
|
+
}) | undefined;
|
|
420
|
+
/** 按偏移把分片拼成全文;重叠部分取先到者,有缺口或没到末尾返回 undefined */
|
|
421
|
+
declare function assembleChunks(chunks: readonly BlobChunk[]): string | undefined;
|
|
422
|
+
declare function unspillRecording(recording: readonly Event[], opts?: UnspillOptions): UnspillResult;
|
|
423
|
+
type Replacement = readonly [from: string, to: string];
|
|
424
|
+
interface ScrubResult {
|
|
425
|
+
events: Event[];
|
|
426
|
+
/** 每条规则命中的次数,与传入顺序一致(不含原文,可以放进报告) */
|
|
427
|
+
hits: number[];
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* 对每条事件的 JSON 文本做逐字替换(长的 from 先换,避免短串把长串拆坏),再解析回事件。
|
|
431
|
+
* 事件的 id / sessionId / seq 等结构字段同样会被替换 —— 调用方若不想动它们,别把那些值放进表里。
|
|
432
|
+
*/
|
|
433
|
+
declare function scrubEvents(events: readonly Event[], replacements: readonly Replacement[]): ScrubResult;
|
|
434
|
+
/** 去重保序,给每个不同的值起一个别名;alias 收到的 index 从 1 起 */
|
|
435
|
+
declare function aliasTable(values: Iterable<string>, alias: (index: number, value: string) => string): Replacement[];
|
|
436
|
+
/** 在事件的 JSON 文本里按正则找值(如 19 位广告主 id),按出现顺序去重返回 */
|
|
437
|
+
declare function matchStrings(events: readonly Event[], pattern: RegExp): string[];
|
|
438
|
+
/**
|
|
439
|
+
* 收集事件里指定键名的字符串值:遍历 payload,工具结果的文本部分若是 JSON 也解析后一起遍历
|
|
440
|
+
* (AdRate 这类信封工具把整个响应放在一段文本里)。按出现顺序去重。
|
|
441
|
+
*/
|
|
442
|
+
declare function jsonValuesAt(events: readonly Event[], keys: readonly string[]): string[];
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* 把报告排成 Markdown 表:一臂一行,Boss 看这张表就够。要机器读请直接用 EvalReport(JSON)。
|
|
446
|
+
*/
|
|
447
|
+
|
|
448
|
+
interface RenderOptions {
|
|
449
|
+
/** 附上门禁结论 */
|
|
450
|
+
gate?: GateResult;
|
|
451
|
+
/** 每个 fixture × 臂 × 重复各一行的明细 */
|
|
452
|
+
detail?: boolean;
|
|
453
|
+
}
|
|
454
|
+
declare function renderReport(report: EvalReport, opts?: RenderOptions): string;
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* 对照运行器:fixture × 臂 × 重复,每格独立的存储与会话,跑完整任务,再在分叉会话里问预埋事实,最后算指标。
|
|
458
|
+
*
|
|
459
|
+
* 一格的流程:
|
|
460
|
+
* 1. 新存储(缺省内存)、新 sessionId;有种子历史就换 sessionId 后原样 append
|
|
461
|
+
* 2. runLoop 跑任务:审批由 fixture.approve 代答后带 decisions 续跑;预算暂停按 maxResumes 续;handoff 跟到新会话;
|
|
462
|
+
* error / host / 客户端工具暂停就停(如实记 status)
|
|
463
|
+
* 3. 探针:对每条预埋事实,从主会话末尾 fork 一条新会话,去掉宿主工具(脑子工具如 fetch_blob 保留),
|
|
464
|
+
* 只问一个问题,取模型正文按 expect / judge 打分。fork 保证探针之间互不污染、也不污染主会话
|
|
465
|
+
* 4. 指标 = 时间线纯函数 + 完成度 + 召回 + 墙钟
|
|
466
|
+
*
|
|
467
|
+
* 循环用的就是 core 的 runLoop,不做第二套;臂只是 LoopConfig 的一部分。
|
|
468
|
+
*/
|
|
469
|
+
|
|
470
|
+
interface RunEvalOptions {
|
|
471
|
+
fixtures: readonly EvalFixture[];
|
|
472
|
+
arms: readonly EvalArm[];
|
|
473
|
+
/** 被测模型 */
|
|
474
|
+
lowering: Lowering;
|
|
475
|
+
model: ModelRef;
|
|
476
|
+
/** 每格重复次数,缺省 1;真模型有随机性,门禁前建议 ≥ 3 */
|
|
477
|
+
repeats?: number;
|
|
478
|
+
/** 重复编号从几开始,缺省 1。补跑单格(如第 3 次因网络中断失败)时给 3,结果编号不会盖掉已有的 */
|
|
479
|
+
repeatStart?: number;
|
|
480
|
+
/** 每格一套新存储;缺省内存实现 */
|
|
481
|
+
stores?: () => Stores;
|
|
482
|
+
/** LLM judge,给没有 expect 的预埋事实打分 */
|
|
483
|
+
judge?: Judge;
|
|
484
|
+
registry?: EventSchemaRegistry;
|
|
485
|
+
/** 探针问答的轮数上限,缺省 4 */
|
|
486
|
+
probeMaxTurns?: number;
|
|
487
|
+
/** 续跑(审批 / 预算 / 交接)的总次数上限,防止无限循环;缺省 200 */
|
|
488
|
+
maxSteps?: number;
|
|
489
|
+
signal?: AbortSignal;
|
|
490
|
+
/** 每条刚入日志的事件(含探针会话) */
|
|
491
|
+
onEvent?: (event: Event, cell: {
|
|
492
|
+
fixtureId: string;
|
|
493
|
+
arm: string;
|
|
494
|
+
repeat: number;
|
|
495
|
+
probe?: string;
|
|
496
|
+
}) => void;
|
|
497
|
+
/** 每格跑完 */
|
|
498
|
+
onOutcome?: (outcome: EvalOutcome) => void;
|
|
499
|
+
/** 测试注入 */
|
|
500
|
+
now?: () => number;
|
|
501
|
+
newId?: (at: number) => string;
|
|
502
|
+
}
|
|
503
|
+
declare function runEval(opts: RunEvalOptions): Promise<EvalReport>;
|
|
504
|
+
/**
|
|
505
|
+
* 探针的回答:优先取模型正文;一句正文都没有时退回最后一段 thinking。
|
|
506
|
+
* 实测(E3,DeepSeek v4 flash 经 Anthropic 端口)短答案("14")有 9% 的概率整个落在 thinking 块里、正文为空、输出 1 个 token ——
|
|
507
|
+
* 那是模型唯一的输出,不该判成"不记得"。
|
|
508
|
+
*/
|
|
509
|
+
declare function probeAnswerOf(events: readonly Event[]): {
|
|
510
|
+
answer: string;
|
|
511
|
+
answerFrom: FactResult["answerFrom"];
|
|
512
|
+
};
|
|
513
|
+
/** 确定性判分:字符串不分大小写包含、正则、或函数(0~1 / 布尔) */
|
|
514
|
+
declare function gradeExpect(expect: NonNullable<PlantedFact["expect"]>, answer: string): number;
|
|
515
|
+
/** 按臂聚合均值 */
|
|
516
|
+
declare function summarize(outcomes: readonly EvalOutcome[]): Record<string, ArmSummary>;
|
|
517
|
+
|
|
518
|
+
export { type ApprovalInterruption, type ArmSummary, type CompactionCounts, type EvalArm, type EvalFixture, type EvalMetrics, type EvalOutcome, type EvalOutcomeDraft, type EvalReport, type EvalTask, type FactResult, type GateCheck, type GateOptions, type GateResult, JsonlParseError, type Judge, type ModelAction, type ModelTurn, type ParseJsonlOptions, type PlantedConstraint, type PlantedFact, type RecordedToolSpec, type RecordedToolsOptions, type RecordedToolset, type RenderOptions, type Replacement, type RunEvalOptions, type ScrubResult, type TimelineMetrics, type TokenTotals, type UnspillOptions, type UnspillResult, type ViolationWindow, ZERO_TOKENS, addTokens, aliasTable, assembleChunks, billableTokens, cacheHitRateOf, canonicalArgs, checkGate, countCompactions, countRepeatedToolCalls, finalTextOf, gradeExpect, isModelAction, jsonValuesAt, matchStrings, mean, measureTimeline, measureViolations, noneArm, parseEventsJsonl, parseFetchChunk, probeAnswerOf, recordedTools, renderReport, runEval, scrubEvents, splitModelTurns, sumTokens, summarize, thresholdArm, toEventsJsonl, unspillRecording, withCapabilities, withContextWindow };
|