dsh-eval 0.2.0 → 0.3.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/README.md +31 -1
- package/lib/index.js +7 -0
- package/lib/types/command.d.ts +7 -0
- package/lib/types/import.d.ts +36 -0
- package/lib/types/types.d.ts +11 -0
- package/package.json +4 -4
- package/src/benchmark.ts +9 -0
- package/src/command.ts +27 -0
- package/src/import.ts +327 -0
- package/src/index.ts +7 -0
- package/src/runner.ts +56 -16
- package/src/types.ts +12 -0
package/README.md
CHANGED
|
@@ -98,6 +98,35 @@ hallucination rate.
|
|
|
98
98
|
|
|
99
99
|
`dsh eval compare run-v1.json run-v2.json` renders both runs as a markdown table: benchmark/model identity, completed trials, steps, tool/task/tool-selection rates, invalid calls, retries, tokens, cost, and latency, with signed `B - A` deltas. When both runs complete the same case x trial keys, the table appends paired statistics: trial count, win/lose/tie on the first available metric (task success, tool selection accuracy, or final-answer score), and mean `B - A` deltas for steps, tokens, and judge score. The runs' `seed` is recorded provenance for the pairing, not a guarantee of identical model output.
|
|
100
100
|
|
|
101
|
+
## Replay (keyless CI)
|
|
102
|
+
|
|
103
|
+
A keyed run records every trial's session log under its `tempRoot`. Copy the
|
|
104
|
+
recorded trial trees into a `replay.dir` and rerun without credentials: the
|
|
105
|
+
spawned harness mounts `@deepseek-ai/dsh-llm-replay`, which reconstructs each
|
|
106
|
+
model stream from the recorded `assistant/chunk` events.
|
|
107
|
+
|
|
108
|
+
```yaml
|
|
109
|
+
replay:
|
|
110
|
+
dir: ./recorded
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The directory holds one tree per trial named `<caseId>-<trial>/`, with child
|
|
114
|
+
logs riding alongside: `recorded/fix-tests-001-1/session.jsonl`. A missing
|
|
115
|
+
fixture fails that trial, and `judge` cannot be combined with `replay` in one
|
|
116
|
+
run. The npm release of the replay plugin lags the source workspace, so replay
|
|
117
|
+
runs require a source-mode harness checkout.
|
|
118
|
+
|
|
119
|
+
## Importing external traces
|
|
120
|
+
|
|
121
|
+
`dsh eval import codex|claude-code <session.jsonl> --out run.json` imports a
|
|
122
|
+
Codex or Claude Code session log as a one-trial run with folded metrics:
|
|
123
|
+
|
|
124
|
+
- one synthesized turn (and step) per user message;
|
|
125
|
+
- assistant text, tool calls, and tool results mapped into the trace vocabulary;
|
|
126
|
+
- token usage is not part of either external format, so token and cost metrics stay zero.
|
|
127
|
+
|
|
128
|
+
The imported run drops into the same `report`/`compare` pipeline.
|
|
129
|
+
|
|
101
130
|
## Extension points
|
|
102
131
|
|
|
103
132
|
The runner is the only consumer today: `runBenchmark` takes a loaded benchmark and returns the run record, and the report module persists and renders it. The judge chat seam is injected by the host (a dsh-llm stream) and stubbed in tests, keeping the suite keyless.
|
|
@@ -114,7 +143,8 @@ None; this package neither assembles nor sends a provider request, so no request
|
|
|
114
143
|
|
|
115
144
|
- **Judge output is best-effort** — a judge chat failure or unparsable reply yields null verdict fields, and judge calls consume model quota outside the trial's measured cost.
|
|
116
145
|
- **Paired comparison pairs as-is** — trials pair by case id and trial index; `seed` is recorded provenance, not a deterministic guarantee of identical model output, and per-arm leaderboards are not rendered.
|
|
117
|
-
- **Replay
|
|
146
|
+
- **Replay binds by first-call order** — the replay plugin keys recorded scripts to live sessions by first-call order, so concurrent subagents replay non-deterministically.
|
|
147
|
+
- **Imported traces lack token usage** — Codex and Claude Code logs do not record provider usage, so imported runs report zero tokens and no cost.
|
|
118
148
|
- **Direct-child timeout only** — a timed-out trial kills the direct dsh process; on Windows its descendants may survive.
|
|
119
149
|
- **Windows launcher commands** — the spawned command runs without a shell, so `.cmd`/`.bat` shims need a direct executable or `node <path>` override (`--dsh "node C:/.../apps/cli/lib/bin.js"`).
|
|
120
150
|
- **No atomic report write** — the run JSON is written in place; a crash mid-write can truncate the report.
|
package/lib/index.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import { loadBenchmark } from "./benchmark.js";
|
|
15
15
|
import { applyEval } from "./command.js";
|
|
16
16
|
import { compareRuns, renderCompareMarkdown } from "./compare.js";
|
|
17
|
+
import { importTraceFile } from "./import.js";
|
|
17
18
|
import { llmJudgeChat } from "./judge.js";
|
|
18
19
|
import { renderMarkdownReport, writeRunReport, loadRunReport } from "./report.js";
|
|
19
20
|
import { runBenchmark } from "./runner.js";
|
|
@@ -62,6 +63,12 @@ function summary(run) {
|
|
|
62
63
|
*/
|
|
63
64
|
export async function executeEval(values, io, services = {}) {
|
|
64
65
|
try {
|
|
66
|
+
if (values.kind === 'import') {
|
|
67
|
+
const run = await importTraceFile(values.format, values.path, values.caseId);
|
|
68
|
+
await writeRunReport(run, values.outPath);
|
|
69
|
+
io.stdout.write(`Wrote ${values.outPath} (1 imported trial, ${summary(run)})\n`);
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
65
72
|
if (values.kind === 'report') {
|
|
66
73
|
const run = await loadRunReport(values.runPath);
|
|
67
74
|
io.stdout.write(renderMarkdownReport(run));
|
package/lib/types/command.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { Command } from 'commander';
|
|
10
10
|
import type { Context } from '@deepseek-ai/cordis';
|
|
11
|
+
import type { ImportFormat } from './import.ts';
|
|
11
12
|
/** What the eval app resolved from its command line. */
|
|
12
13
|
export type EvalStartupValues = {
|
|
13
14
|
kind: 'run';
|
|
@@ -23,6 +24,12 @@ export type EvalStartupValues = {
|
|
|
23
24
|
kind: 'compare';
|
|
24
25
|
runPathA: string;
|
|
25
26
|
runPathB: string;
|
|
27
|
+
} | {
|
|
28
|
+
kind: 'import';
|
|
29
|
+
format: ImportFormat;
|
|
30
|
+
path: string;
|
|
31
|
+
outPath: string;
|
|
32
|
+
caseId: string;
|
|
26
33
|
};
|
|
27
34
|
/**
|
|
28
35
|
* Build a fresh eval commander program. The root action rejects a bare
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-harness trace import: Codex and Claude Code session JSONL become the
|
|
3
|
+
* eval trace vocabulary so report and compare can fold metrics over runs
|
|
4
|
+
* recorded by other agents. Each importer synthesizes one turn (and one step)
|
|
5
|
+
* per user message, preserving assistant text and tool calls/results; model
|
|
6
|
+
* token usage is not part of either external format, so token metrics stay
|
|
7
|
+
* zero.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-eval/import
|
|
10
|
+
*/
|
|
11
|
+
import type { EvalRun, EvalTrace } from './types.ts';
|
|
12
|
+
/**
|
|
13
|
+
* Import a Codex session JSONL (lines of `response_item` records) as a trace.
|
|
14
|
+
* @param text - the Codex log's UTF-8 text.
|
|
15
|
+
* @param source - the source file path, used for the synthetic session id.
|
|
16
|
+
* @returns the imported trace; non-`response_item` lines are skipped.
|
|
17
|
+
*/
|
|
18
|
+
export declare function importCodexLog(text: string, source: string): EvalTrace;
|
|
19
|
+
/**
|
|
20
|
+
* Import a Claude Code session JSONL (lines of `user`/`assistant` records) as
|
|
21
|
+
* a trace.
|
|
22
|
+
* @param text - the Claude Code log's UTF-8 text.
|
|
23
|
+
* @param source - the source file path, used for the synthetic session id.
|
|
24
|
+
* @returns the imported trace; non-user/assistant lines are skipped.
|
|
25
|
+
*/
|
|
26
|
+
export declare function importClaudeLog(text: string, source: string): EvalTrace;
|
|
27
|
+
/** Import formats the CLI accepts. */
|
|
28
|
+
export type ImportFormat = 'codex' | 'claude-code';
|
|
29
|
+
/**
|
|
30
|
+
* Load an external session log and build a one-trial run record from it.
|
|
31
|
+
* @param format - the external harness format.
|
|
32
|
+
* @param path - the session JSONL path.
|
|
33
|
+
* @param caseId - the imported trial's case id.
|
|
34
|
+
* @returns the run record with one completed trial.
|
|
35
|
+
*/
|
|
36
|
+
export declare function importTraceFile(format: ImportFormat, path: string, caseId: string): Promise<EvalRun>;
|
package/lib/types/types.d.ts
CHANGED
|
@@ -46,6 +46,15 @@ export interface BenchmarkJudge {
|
|
|
46
46
|
/** Maximum final-answer score; defaults to 10. */
|
|
47
47
|
maxScore: number;
|
|
48
48
|
}
|
|
49
|
+
/** Keyless replay configuration: recorded session logs replayed instead of live model calls. */
|
|
50
|
+
export interface BenchmarkReplay {
|
|
51
|
+
/**
|
|
52
|
+
* Directory holding one recorded-log tree per trial, named
|
|
53
|
+
* `<caseId>-<trial>/session.jsonl` (child logs ride alongside). A run with
|
|
54
|
+
* a key records these under its `tempRoot`.
|
|
55
|
+
*/
|
|
56
|
+
dir: string;
|
|
57
|
+
}
|
|
49
58
|
/** A validated benchmark document. */
|
|
50
59
|
export interface Benchmark {
|
|
51
60
|
/** Benchmark name; also the run record's `benchmark` field. */
|
|
@@ -68,6 +77,8 @@ export interface Benchmark {
|
|
|
68
77
|
pricing?: Readonly<Record<string, BenchmarkPricing>>;
|
|
69
78
|
/** LLM-judge configuration, when final-answer scoring is enabled. */
|
|
70
79
|
judge?: BenchmarkJudge;
|
|
80
|
+
/** Keyless replay configuration, when model calls replay from recorded logs. */
|
|
81
|
+
replay?: BenchmarkReplay;
|
|
71
82
|
/** Directory the benchmark document was loaded from; relative paths resolve against it. */
|
|
72
83
|
baseDir: string;
|
|
73
84
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-eval",
|
|
3
3
|
"description": "Agent evaluation platform: benchmark YAML, headless run orchestration, trace-based metrics, and run reports",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -71,12 +71,12 @@
|
|
|
71
71
|
"typescript": "^6.0.3",
|
|
72
72
|
"vitest": "^4.1.8",
|
|
73
73
|
"@deepseek-ai/cordis": "^4.0.1",
|
|
74
|
-
"@deepseek-ai/dsh-cmdline": "^0.1.0-rc.5",
|
|
75
74
|
"@deepseek-ai/cordis-plugin-include": "^1.0.6",
|
|
75
|
+
"@deepseek-ai/cordis-plugin-loader": "^1.0.2",
|
|
76
|
+
"@deepseek-ai/dsh-cmdline": "^0.1.0-rc.5",
|
|
77
|
+
"@deepseek-ai/dsh-invariants": "^0.1.0-rc.5",
|
|
76
78
|
"@deepseek-ai/dsh-llm": "^0.1.0-rc.5",
|
|
77
79
|
"@deepseek-ai/dsh-llm-retry": "^0.1.0-rc.5",
|
|
78
|
-
"@deepseek-ai/dsh-invariants": "^0.1.0-rc.5",
|
|
79
|
-
"@deepseek-ai/cordis-plugin-loader": "^1.0.2",
|
|
80
80
|
"@deepseek-ai/dsh-session": "^0.1.0-rc.5"
|
|
81
81
|
},
|
|
82
82
|
"scripts": {
|
package/src/benchmark.ts
CHANGED
|
@@ -19,6 +19,10 @@ const judgeSchema = z.object({
|
|
|
19
19
|
maxScore: z.number().int().positive().default(10),
|
|
20
20
|
}).strict()
|
|
21
21
|
|
|
22
|
+
const replaySchema = z.object({
|
|
23
|
+
dir: z.string().min(1),
|
|
24
|
+
}).strict()
|
|
25
|
+
|
|
22
26
|
const pricingSchema = z.object({
|
|
23
27
|
inputUsdPerMTokens: z.number().nonnegative(),
|
|
24
28
|
cacheReadUsdPerMTokens: z.number().nonnegative(),
|
|
@@ -54,6 +58,7 @@ const benchmarkSchema = z.object({
|
|
|
54
58
|
cases: z.array(benchmarkCaseSchema).min(1),
|
|
55
59
|
pricing: z.record(z.string().min(1), pricingSchema).optional(),
|
|
56
60
|
judge: judgeSchema.optional(),
|
|
61
|
+
replay: replaySchema.optional(),
|
|
57
62
|
}).strict()
|
|
58
63
|
|
|
59
64
|
/**
|
|
@@ -105,6 +110,7 @@ export async function parseBenchmark(text: string, baseDir: string): Promise<Ben
|
|
|
105
110
|
}
|
|
106
111
|
const pricing = parsed.pricing
|
|
107
112
|
const judge = parsed.judge
|
|
113
|
+
const replay = parsed.replay
|
|
108
114
|
return {
|
|
109
115
|
name: parsed.name,
|
|
110
116
|
model: parsed.model,
|
|
@@ -118,6 +124,9 @@ export async function parseBenchmark(text: string, baseDir: string): Promise<Ben
|
|
|
118
124
|
...(judge !== undefined
|
|
119
125
|
? { judge: { provider: judge.provider, model: judge.model ?? parsed.model, ...(judge.rubric !== undefined ? { rubric: judge.rubric } : {}), maxScore: judge.maxScore } }
|
|
120
126
|
: {}),
|
|
127
|
+
...(replay !== undefined
|
|
128
|
+
? { replay: { dir: isAbsolute(replay.dir) ? replay.dir : resolve(baseDir, replay.dir) } }
|
|
129
|
+
: {}),
|
|
121
130
|
baseDir,
|
|
122
131
|
}
|
|
123
132
|
}
|
package/src/command.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import { Command } from 'commander'
|
|
11
11
|
import type { Context } from '@deepseek-ai/cordis'
|
|
12
12
|
import { parseCmdline } from '@deepseek-ai/dsh-cmdline'
|
|
13
|
+
import type { ImportFormat } from './import.ts'
|
|
13
14
|
|
|
14
15
|
/** What the eval app resolved from its command line. */
|
|
15
16
|
export type EvalStartupValues =
|
|
@@ -30,6 +31,13 @@ export type EvalStartupValues =
|
|
|
30
31
|
runPathA: string
|
|
31
32
|
runPathB: string
|
|
32
33
|
}
|
|
34
|
+
| {
|
|
35
|
+
kind: 'import'
|
|
36
|
+
format: ImportFormat
|
|
37
|
+
path: string
|
|
38
|
+
outPath: string
|
|
39
|
+
caseId: string
|
|
40
|
+
}
|
|
33
41
|
|
|
34
42
|
/** Options accepted by `eval run`. */
|
|
35
43
|
interface RunCommandOptions {
|
|
@@ -39,6 +47,12 @@ interface RunCommandOptions {
|
|
|
39
47
|
profile?: string
|
|
40
48
|
}
|
|
41
49
|
|
|
50
|
+
/** Options accepted by `eval import`. */
|
|
51
|
+
interface ImportCommandOptions {
|
|
52
|
+
out: string
|
|
53
|
+
caseId: string
|
|
54
|
+
}
|
|
55
|
+
|
|
42
56
|
/** Split a single `--dsh` value into argv on whitespace. */
|
|
43
57
|
function splitCommand(value: string | undefined): readonly string[] | undefined {
|
|
44
58
|
if (value === undefined) return undefined
|
|
@@ -98,6 +112,19 @@ export function evalCommand(publish: (values: EvalStartupValues) => void): Comma
|
|
|
98
112
|
.action((runPathA: string, runPathB: string) => {
|
|
99
113
|
publish({ kind: 'compare', runPathA, runPathB })
|
|
100
114
|
})
|
|
115
|
+
const importer = program.command('import').description('import an external harness session log as a run report')
|
|
116
|
+
importer
|
|
117
|
+
.argument('<format>', 'external harness format: codex or claude-code')
|
|
118
|
+
.argument('<session.jsonl>', 'path to the external session log')
|
|
119
|
+
.option('--out <path>', 'output run JSON path', 'imported-run.json')
|
|
120
|
+
.option('--case-id <id>', 'case id for the imported trial', 'imported')
|
|
121
|
+
.action((format: string, path: string, options: ImportCommandOptions) => {
|
|
122
|
+
if (format !== 'codex' && format !== 'claude-code') {
|
|
123
|
+
program.error('error: import format must be codex or claude-code')
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
publish({ kind: 'import', format, path, outPath: options.out, caseId: options.caseId })
|
|
127
|
+
})
|
|
101
128
|
return program
|
|
102
129
|
}
|
|
103
130
|
|
package/src/import.ts
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-harness trace import: Codex and Claude Code session JSONL become the
|
|
3
|
+
* eval trace vocabulary so report and compare can fold metrics over runs
|
|
4
|
+
* recorded by other agents. Each importer synthesizes one turn (and one step)
|
|
5
|
+
* per user message, preserving assistant text and tool calls/results; model
|
|
6
|
+
* token usage is not part of either external format, so token metrics stay
|
|
7
|
+
* zero.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-eval/import
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { readFile } from 'node:fs/promises'
|
|
13
|
+
import { basename, dirname, resolve } from 'node:path'
|
|
14
|
+
import { computeMetrics } from './metrics.ts'
|
|
15
|
+
import { parseSessionLog } from './trace.ts'
|
|
16
|
+
import type { EvalRun, EvalTrace } from './types.ts'
|
|
17
|
+
|
|
18
|
+
/** Synthetic event-time spacing keeps imported metrics deterministic. */
|
|
19
|
+
const STEP_MS = 1000
|
|
20
|
+
/** Epoch base for imported traces without a real timestamp. */
|
|
21
|
+
const BASE_MS = 1_700_000_000_000
|
|
22
|
+
|
|
23
|
+
/** A pending event row before seq/time assignment. */
|
|
24
|
+
interface ParsedEvent {
|
|
25
|
+
type: string
|
|
26
|
+
data: unknown
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Join an external-log line's timestamp (ISO or epoch) with a synthetic offset. */
|
|
30
|
+
function eventTime(lineIndex: number): number {
|
|
31
|
+
return BASE_MS + lineIndex * STEP_MS
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Extract display text from a string, a text block, or a block array. */
|
|
35
|
+
function textOf(value: unknown): string {
|
|
36
|
+
if (typeof value === 'string') return value
|
|
37
|
+
if (Array.isArray(value)) {
|
|
38
|
+
return value.flatMap((block) => {
|
|
39
|
+
if (typeof block !== 'object' || block === null) return []
|
|
40
|
+
const text = (block as Record<string, unknown>).text
|
|
41
|
+
return typeof text === 'string' ? [text] : []
|
|
42
|
+
}).join('')
|
|
43
|
+
}
|
|
44
|
+
return ''
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Stable synthetic session id from the source filename. */
|
|
48
|
+
function sessionIdFrom(source: string): string {
|
|
49
|
+
const safe = basename(source).replace(/[^A-Za-z0-9._-]/gu, '-').replace(/^-+|-+$/gu, '')
|
|
50
|
+
return safe === '' ? 'imported' : safe
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Assemble parsed events into a validated trace with contiguous seqs. */
|
|
54
|
+
function assembleTrace(events: readonly ParsedEvent[], id: string): EvalTrace {
|
|
55
|
+
const lines = [
|
|
56
|
+
JSON.stringify({ type: 'session', version: 0, id, createdAt: eventTime(0), delegationDepth: 0 }),
|
|
57
|
+
...events.map((event, index) => JSON.stringify({ seq: index, time: eventTime(index + 1), type: event.type, data: event.data })),
|
|
58
|
+
'',
|
|
59
|
+
]
|
|
60
|
+
return parseSessionLog(lines.join('\n'))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Close the current synthesized turn with step/end and turn/end. */
|
|
64
|
+
function closeTurn(
|
|
65
|
+
events: ParsedEvent[],
|
|
66
|
+
turn: number,
|
|
67
|
+
step: number,
|
|
68
|
+
open: boolean,
|
|
69
|
+
): boolean {
|
|
70
|
+
if (!open) return false
|
|
71
|
+
events.push({ type: 'step/end', data: { turn, step } })
|
|
72
|
+
events.push({ type: 'turn/end', data: { turn, reason: { kind: 'completed' } } })
|
|
73
|
+
return true
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Import a Codex session JSONL (lines of `response_item` records) as a trace.
|
|
78
|
+
* @param text - the Codex log's UTF-8 text.
|
|
79
|
+
* @param source - the source file path, used for the synthetic session id.
|
|
80
|
+
* @returns the imported trace; non-`response_item` lines are skipped.
|
|
81
|
+
*/
|
|
82
|
+
export function importCodexLog(text: string, source: string): EvalTrace {
|
|
83
|
+
const events: ParsedEvent[] = []
|
|
84
|
+
let turn = 0
|
|
85
|
+
let step = 0
|
|
86
|
+
let open = false
|
|
87
|
+
let callCounter = 0
|
|
88
|
+
let messageCounter = 0
|
|
89
|
+
const close = (): void => {
|
|
90
|
+
open = closeTurn(events, turn, step, open)
|
|
91
|
+
}
|
|
92
|
+
for (const line of text.split(/\r?\n/u)) {
|
|
93
|
+
if (line.trim() === '') continue
|
|
94
|
+
let record: unknown
|
|
95
|
+
try {
|
|
96
|
+
record = JSON.parse(line)
|
|
97
|
+
} catch {
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
if (typeof record !== 'object' || record === null) continue
|
|
101
|
+
const row = record as Record<string, unknown>
|
|
102
|
+
if (row.type !== 'response_item' || typeof row.payload !== 'object' || row.payload === null) continue
|
|
103
|
+
const payload = row.payload as Record<string, unknown>
|
|
104
|
+
if (payload.type === 'message' && payload.role === 'user') {
|
|
105
|
+
close()
|
|
106
|
+
turn += 1
|
|
107
|
+
step = 0
|
|
108
|
+
open = true
|
|
109
|
+
messageCounter += 1
|
|
110
|
+
events.push({ type: 'turn/start', data: { turn } })
|
|
111
|
+
events.push({
|
|
112
|
+
type: 'user/message',
|
|
113
|
+
data: {
|
|
114
|
+
id: `user-${messageCounter}`,
|
|
115
|
+
role: 'user',
|
|
116
|
+
content: [{ type: 'text', text: textOf(payload.content) }],
|
|
117
|
+
source: { kind: 'user' },
|
|
118
|
+
},
|
|
119
|
+
})
|
|
120
|
+
events.push({ type: 'step/start', data: { turn, step } })
|
|
121
|
+
} else if (payload.type === 'message' && payload.role === 'assistant') {
|
|
122
|
+
messageCounter += 1
|
|
123
|
+
events.push({
|
|
124
|
+
type: 'assistant/message',
|
|
125
|
+
data: {
|
|
126
|
+
turn,
|
|
127
|
+
step,
|
|
128
|
+
message: {
|
|
129
|
+
id: `assistant-${messageCounter}`,
|
|
130
|
+
role: 'assistant',
|
|
131
|
+
content: [{ type: 'text', text: textOf(payload.content) }],
|
|
132
|
+
source: { kind: 'model' },
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
})
|
|
136
|
+
} else if (payload.type === 'function_call') {
|
|
137
|
+
callCounter += 1
|
|
138
|
+
const callId = typeof payload.call_id === 'string' ? payload.call_id : `codex-call-${callCounter}`
|
|
139
|
+
const name = typeof payload.name === 'string' ? payload.name : 'tool'
|
|
140
|
+
const argumentsText = typeof payload.arguments === 'string' ? payload.arguments : '{}'
|
|
141
|
+
events.push({ type: 'tool/call', data: { turn, step, callId, name, arguments: argumentsText } })
|
|
142
|
+
} else if (payload.type === 'function_call_output') {
|
|
143
|
+
callCounter += 1
|
|
144
|
+
const callId = typeof payload.call_id === 'string' ? payload.call_id : `codex-call-${callCounter}`
|
|
145
|
+
const output = textOf(payload.output ?? payload.content)
|
|
146
|
+
events.push({
|
|
147
|
+
type: 'tool/result',
|
|
148
|
+
data: {
|
|
149
|
+
turn,
|
|
150
|
+
step,
|
|
151
|
+
message: {
|
|
152
|
+
id: `tool-result-${callCounter}`,
|
|
153
|
+
role: 'user',
|
|
154
|
+
content: [{ type: 'tool-result', text: output, isError: payload.is_error === true }],
|
|
155
|
+
source: { kind: 'tool', callId },
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
close()
|
|
162
|
+
return assembleTrace(events, sessionIdFrom(source))
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Whether a Claude Code user message carries only tool results (not a new turn). */
|
|
166
|
+
function isToolResultUser(content: unknown): boolean {
|
|
167
|
+
if (!Array.isArray(content)) return false
|
|
168
|
+
return content.every(block =>
|
|
169
|
+
typeof block === 'object' && block !== null && (block as Record<string, unknown>).type === 'tool_result')
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Import a Claude Code session JSONL (lines of `user`/`assistant` records) as
|
|
174
|
+
* a trace.
|
|
175
|
+
* @param text - the Claude Code log's UTF-8 text.
|
|
176
|
+
* @param source - the source file path, used for the synthetic session id.
|
|
177
|
+
* @returns the imported trace; non-user/assistant lines are skipped.
|
|
178
|
+
*/
|
|
179
|
+
export function importClaudeLog(text: string, source: string): EvalTrace {
|
|
180
|
+
const events: ParsedEvent[] = []
|
|
181
|
+
let turn = 0
|
|
182
|
+
let step = 0
|
|
183
|
+
let open = false
|
|
184
|
+
let messageCounter = 0
|
|
185
|
+
const close = (): void => {
|
|
186
|
+
open = closeTurn(events, turn, step, open)
|
|
187
|
+
}
|
|
188
|
+
for (const line of text.split(/\r?\n/u)) {
|
|
189
|
+
if (line.trim() === '') continue
|
|
190
|
+
let record: unknown
|
|
191
|
+
try {
|
|
192
|
+
record = JSON.parse(line)
|
|
193
|
+
} catch {
|
|
194
|
+
continue
|
|
195
|
+
}
|
|
196
|
+
if (typeof record !== 'object' || record === null) continue
|
|
197
|
+
const row = record as Record<string, unknown>
|
|
198
|
+
if ((row.type !== 'user' && row.type !== 'assistant')
|
|
199
|
+
|| typeof row.message !== 'object' || row.message === null) continue
|
|
200
|
+
const message = row.message as Record<string, unknown>
|
|
201
|
+
const content = message.content
|
|
202
|
+
if (row.type === 'user' && !isToolResultUser(content)) {
|
|
203
|
+
close()
|
|
204
|
+
turn += 1
|
|
205
|
+
step = 0
|
|
206
|
+
open = true
|
|
207
|
+
messageCounter += 1
|
|
208
|
+
events.push({ type: 'turn/start', data: { turn } })
|
|
209
|
+
events.push({
|
|
210
|
+
type: 'user/message',
|
|
211
|
+
data: {
|
|
212
|
+
id: `user-${messageCounter}`,
|
|
213
|
+
role: 'user',
|
|
214
|
+
content: [{ type: 'text', text: textOf(content) }],
|
|
215
|
+
source: { kind: 'user' },
|
|
216
|
+
},
|
|
217
|
+
})
|
|
218
|
+
events.push({ type: 'step/start', data: { turn, step } })
|
|
219
|
+
continue
|
|
220
|
+
}
|
|
221
|
+
if (row.type === 'assistant') {
|
|
222
|
+
if (typeof content === 'string') {
|
|
223
|
+
messageCounter += 1
|
|
224
|
+
events.push({
|
|
225
|
+
type: 'assistant/message',
|
|
226
|
+
data: {
|
|
227
|
+
turn,
|
|
228
|
+
step,
|
|
229
|
+
message: {
|
|
230
|
+
id: `assistant-${messageCounter}`,
|
|
231
|
+
role: 'assistant',
|
|
232
|
+
content: [{ type: 'text', text: content }],
|
|
233
|
+
source: { kind: 'model' },
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
})
|
|
237
|
+
} else {
|
|
238
|
+
for (const block of Array.isArray(content) ? content : []) {
|
|
239
|
+
if (typeof block !== 'object' || block === null) continue
|
|
240
|
+
const item = block as Record<string, unknown>
|
|
241
|
+
if (item.type === 'text') {
|
|
242
|
+
messageCounter += 1
|
|
243
|
+
events.push({
|
|
244
|
+
type: 'assistant/message',
|
|
245
|
+
data: {
|
|
246
|
+
turn,
|
|
247
|
+
step,
|
|
248
|
+
message: {
|
|
249
|
+
id: `assistant-${messageCounter}`,
|
|
250
|
+
role: 'assistant',
|
|
251
|
+
content: [{ type: 'text', text: typeof item.text === 'string' ? item.text : '' }],
|
|
252
|
+
source: { kind: 'model' },
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
})
|
|
256
|
+
} else if (item.type === 'tool_use') {
|
|
257
|
+
const callId = typeof item.id === 'string' ? item.id : `claude-call-${messageCounter}`
|
|
258
|
+
const name = typeof item.name === 'string' ? item.name : 'tool'
|
|
259
|
+
const argumentsText = JSON.stringify(item.input ?? {})
|
|
260
|
+
events.push({ type: 'tool/call', data: { turn, step, callId, name, arguments: argumentsText } })
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
continue
|
|
265
|
+
}
|
|
266
|
+
// Reaching here means a tool-result user message: the plain-user branch
|
|
267
|
+
// above already excluded non-tool-result content.
|
|
268
|
+
const blocks = content as readonly Record<string, unknown>[]
|
|
269
|
+
for (const item of blocks) {
|
|
270
|
+
const callId = typeof item.tool_use_id === 'string' ? item.tool_use_id : `claude-call-${messageCounter}`
|
|
271
|
+
events.push({
|
|
272
|
+
type: 'tool/result',
|
|
273
|
+
data: {
|
|
274
|
+
turn,
|
|
275
|
+
step,
|
|
276
|
+
message: {
|
|
277
|
+
id: `tool-result-${messageCounter}`,
|
|
278
|
+
role: 'user',
|
|
279
|
+
content: [{ type: 'tool-result', text: textOf(item.content), isError: item.is_error === true }],
|
|
280
|
+
source: { kind: 'tool', callId },
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
})
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
close()
|
|
287
|
+
return assembleTrace(events, sessionIdFrom(source))
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Import formats the CLI accepts. */
|
|
291
|
+
export type ImportFormat = 'codex' | 'claude-code'
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Load an external session log and build a one-trial run record from it.
|
|
295
|
+
* @param format - the external harness format.
|
|
296
|
+
* @param path - the session JSONL path.
|
|
297
|
+
* @param caseId - the imported trial's case id.
|
|
298
|
+
* @returns the run record with one completed trial.
|
|
299
|
+
*/
|
|
300
|
+
export async function importTraceFile(format: ImportFormat, path: string, caseId: string): Promise<EvalRun> {
|
|
301
|
+
const absolute = resolve(path)
|
|
302
|
+
const text = await readFile(absolute, 'utf8')
|
|
303
|
+
const trace = format === 'codex'
|
|
304
|
+
? importCodexLog(text, absolute)
|
|
305
|
+
: importClaudeLog(text, absolute)
|
|
306
|
+
const metrics = computeMetrics(trace.events, undefined)
|
|
307
|
+
return {
|
|
308
|
+
benchmark: `import:${format}`,
|
|
309
|
+
model: 'imported',
|
|
310
|
+
createdAt: Date.now(),
|
|
311
|
+
trials: 1,
|
|
312
|
+
seed: 0,
|
|
313
|
+
pricing: null,
|
|
314
|
+
tempRoot: dirname(absolute),
|
|
315
|
+
cases: [{
|
|
316
|
+
caseId,
|
|
317
|
+
trial: 1,
|
|
318
|
+
status: 'completed',
|
|
319
|
+
tracePath: absolute,
|
|
320
|
+
exitCode: null,
|
|
321
|
+
timedOut: false,
|
|
322
|
+
metrics,
|
|
323
|
+
}],
|
|
324
|
+
aggregate: metrics,
|
|
325
|
+
grading: null,
|
|
326
|
+
}
|
|
327
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type { Context } from '@deepseek-ai/cordis'
|
|
|
16
16
|
import { loadBenchmark } from './benchmark.ts'
|
|
17
17
|
import { applyEval, type EvalStartupValues } from './command.ts'
|
|
18
18
|
import { compareRuns, renderCompareMarkdown } from './compare.ts'
|
|
19
|
+
import { importTraceFile } from './import.ts'
|
|
19
20
|
import { llmJudgeChat, type JudgeChat } from './judge.ts'
|
|
20
21
|
import { renderMarkdownReport, writeRunReport, loadRunReport } from './report.ts'
|
|
21
22
|
import { runBenchmark } from './runner.ts'
|
|
@@ -78,6 +79,12 @@ export async function executeEval(
|
|
|
78
79
|
services: { judgeChat?: JudgeChat | undefined } = {},
|
|
79
80
|
): Promise<number> {
|
|
80
81
|
try {
|
|
82
|
+
if (values.kind === 'import') {
|
|
83
|
+
const run = await importTraceFile(values.format, values.path, values.caseId)
|
|
84
|
+
await writeRunReport(run, values.outPath)
|
|
85
|
+
io.stdout.write(`Wrote ${values.outPath} (1 imported trial, ${summary(run)})\n`)
|
|
86
|
+
return 0
|
|
87
|
+
}
|
|
81
88
|
if (values.kind === 'report') {
|
|
82
89
|
const run = await loadRunReport(values.runPath)
|
|
83
90
|
io.stdout.write(renderMarkdownReport(run))
|
package/src/runner.ts
CHANGED
|
@@ -23,21 +23,6 @@ import { tryJudgeTrial, type JudgeChat } from './judge.ts'
|
|
|
23
23
|
import { aggregateMetrics, computeMetrics } from './metrics.ts'
|
|
24
24
|
import { findSessionLogs, loadTrace, mergeTraces } from './trace.ts'
|
|
25
25
|
|
|
26
|
-
/** Overlay forcing the harvested log to plain JSONL and the run to be non-interactive. */
|
|
27
|
-
const EVAL_OVERLAY = `- id: session-persistence-jsonl
|
|
28
|
-
config:
|
|
29
|
-
root: !!js dshHomePath('sessions')
|
|
30
|
-
packChunks: false
|
|
31
|
-
compression: none
|
|
32
|
-
- id: sandbox-policy
|
|
33
|
-
config:
|
|
34
|
-
mode: workspace-write
|
|
35
|
-
workspaceRoot: !!js process.cwd()
|
|
36
|
-
- id: approval
|
|
37
|
-
config:
|
|
38
|
-
policy: never
|
|
39
|
-
`
|
|
40
|
-
|
|
41
26
|
/** Cap per-stream child output so a runaway trial cannot exhaust memory. */
|
|
42
27
|
const MAX_OUTPUT_BYTES = 512 * 1024
|
|
43
28
|
|
|
@@ -82,6 +67,39 @@ function sanitizeSegment(value: string): string {
|
|
|
82
67
|
return safe === '' ? 'case' : safe
|
|
83
68
|
}
|
|
84
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Build the per-trial overlay: plain-JSONL persistence, non-interactive
|
|
72
|
+
* permissions, and the keyless replay plugin when recorded logs exist.
|
|
73
|
+
* @param replay - the replay primary log and child logs, or undefined.
|
|
74
|
+
* @returns the overlay YAML text.
|
|
75
|
+
*/
|
|
76
|
+
function evalOverlay(replay: { file: string; childFiles: readonly string[] } | undefined): string {
|
|
77
|
+
const lines = [
|
|
78
|
+
'- id: session-persistence-jsonl',
|
|
79
|
+
' config:',
|
|
80
|
+
" root: !!js dshHomePath('sessions')",
|
|
81
|
+
' packChunks: false',
|
|
82
|
+
' compression: none',
|
|
83
|
+
'- id: sandbox-policy',
|
|
84
|
+
' config:',
|
|
85
|
+
' mode: workspace-write',
|
|
86
|
+
' workspaceRoot: !!js process.cwd()',
|
|
87
|
+
'- id: approval',
|
|
88
|
+
' config:',
|
|
89
|
+
' policy: never',
|
|
90
|
+
]
|
|
91
|
+
if (replay !== undefined) {
|
|
92
|
+
lines.push(
|
|
93
|
+
'- id: llm-replay',
|
|
94
|
+
" name: '@deepseek-ai/dsh-llm-replay'",
|
|
95
|
+
' config:',
|
|
96
|
+
` file: ${JSON.stringify(replay.file)}`,
|
|
97
|
+
...(replay.childFiles.length === 0 ? [] : [` childFiles: ${JSON.stringify(replay.childFiles)}`]),
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
return `${lines.join('\n')}\n`
|
|
101
|
+
}
|
|
102
|
+
|
|
85
103
|
/** Split a single command string into argv on whitespace. */
|
|
86
104
|
function splitWords(value: string): string[] {
|
|
87
105
|
return value.trim().split(/\s+/u).filter(part => part !== '')
|
|
@@ -236,8 +254,27 @@ async function runCaseTrial(
|
|
|
236
254
|
await mkdir(workspace, { recursive: true })
|
|
237
255
|
await mkdir(dshHome, { recursive: true, mode: 0o700 })
|
|
238
256
|
if (caseValue.workspace !== undefined) await cp(caseValue.workspace, workspace, { recursive: true })
|
|
257
|
+
const replayFixture = benchmark.replay === undefined
|
|
258
|
+
? undefined
|
|
259
|
+
: join(benchmark.replay.dir, `${sanitizeSegment(caseValue.id)}-${trial}`)
|
|
260
|
+
let replay: { file: string; childFiles: string[] } | undefined
|
|
261
|
+
if (replayFixture !== undefined) {
|
|
262
|
+
const logs = await findSessionLogs(replayFixture)
|
|
263
|
+
if (logs.length === 0) {
|
|
264
|
+
return {
|
|
265
|
+
caseId: caseValue.id,
|
|
266
|
+
trial,
|
|
267
|
+
status: 'error',
|
|
268
|
+
error: `no replay fixture found at ${replayFixture}`,
|
|
269
|
+
exitCode: null,
|
|
270
|
+
timedOut: false,
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const primary = logs[0]!
|
|
274
|
+
replay = { file: primary, childFiles: logs.slice(1) }
|
|
275
|
+
}
|
|
239
276
|
const overlayPath = join(trialDir, 'eval.cordis.yml')
|
|
240
|
-
await writeFile(overlayPath,
|
|
277
|
+
await writeFile(overlayPath, evalOverlay(replay), 'utf8')
|
|
241
278
|
let outcome: SpawnOutcome
|
|
242
279
|
try {
|
|
243
280
|
outcome = await runCommand(
|
|
@@ -315,6 +352,9 @@ export async function runBenchmark(benchmark: Benchmark, options: RunOptions = {
|
|
|
315
352
|
if (benchmark.judge !== undefined && options.judgeChat === undefined) {
|
|
316
353
|
throw new Error('eval: benchmark configures a judge but no LLM chat seam is available')
|
|
317
354
|
}
|
|
355
|
+
if (benchmark.judge !== undefined && benchmark.replay !== undefined) {
|
|
356
|
+
throw new Error('eval: judge and replay cannot be combined in one run')
|
|
357
|
+
}
|
|
318
358
|
const root = await mkdtemp(join(options.tempRoot ?? tmpdir(), 'dsh-eval-'))
|
|
319
359
|
const trials = options.trials ?? benchmark.trials
|
|
320
360
|
const effective: Benchmark = {
|
package/src/types.ts
CHANGED
|
@@ -52,6 +52,16 @@ export interface BenchmarkJudge {
|
|
|
52
52
|
maxScore: number
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/** Keyless replay configuration: recorded session logs replayed instead of live model calls. */
|
|
56
|
+
export interface BenchmarkReplay {
|
|
57
|
+
/**
|
|
58
|
+
* Directory holding one recorded-log tree per trial, named
|
|
59
|
+
* `<caseId>-<trial>/session.jsonl` (child logs ride alongside). A run with
|
|
60
|
+
* a key records these under its `tempRoot`.
|
|
61
|
+
*/
|
|
62
|
+
dir: string
|
|
63
|
+
}
|
|
64
|
+
|
|
55
65
|
/** A validated benchmark document. */
|
|
56
66
|
export interface Benchmark {
|
|
57
67
|
/** Benchmark name; also the run record's `benchmark` field. */
|
|
@@ -74,6 +84,8 @@ export interface Benchmark {
|
|
|
74
84
|
pricing?: Readonly<Record<string, BenchmarkPricing>>
|
|
75
85
|
/** LLM-judge configuration, when final-answer scoring is enabled. */
|
|
76
86
|
judge?: BenchmarkJudge
|
|
87
|
+
/** Keyless replay configuration, when model calls replay from recorded logs. */
|
|
88
|
+
replay?: BenchmarkReplay
|
|
77
89
|
/** Directory the benchmark document was loaded from; relative paths resolve against it. */
|
|
78
90
|
baseDir: string
|
|
79
91
|
}
|