eval-magic 0.6.0

One-stop CLI for running skill evals — measure whether an agent skill actually shifts behavior.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! OpenCode transcript (event-stream) parsing.
//!
//! `opencode run --format json` emits a JSONL event stream whose envelopes
//! carry `{type, timestamp (epoch ms), sessionID, ...data}`. `tool_use` events
//! hold a self-contained tool part — the name at `part.tool`, the arguments at
//! `part.state.input`, and the outcome at `part.state.output` (completed) or
//! `part.state.error` (error) — which become ordered [`ToolInvocation`]s.
//! Token usage is summed from `step_finish` parts (`part.tokens`, cache
//! reads excluded — matching the codex parser's accounting), the final
//! message is the last `text` part's text, and the duration is the spread of
//! the envelope timestamps.

use crate::adapters::TranscriptSummary;
use crate::adapters::transcript::{TranscriptEvent, read_jsonl};
use crate::core::ToolInvocation;
use serde_json::Value;
use std::io;
use std::path::Path;

fn extract_invocations(records: &[Value]) -> Vec<ToolInvocation> {
    let mut invocations = Vec::new();
    for record in records {
        if record.get("type").and_then(Value::as_str) != Some("tool_use") {
            continue;
        }
        let Some(part) = record.get("part").and_then(Value::as_object) else {
            continue;
        };
        let Some(name) = part.get("tool").and_then(Value::as_str) else {
            continue;
        };
        let state = part.get("state");
        let args = state
            .and_then(|s| s.get("input"))
            .filter(|input| input.is_object())
            .cloned();
        // Completed states carry `output`, error states carry `error` (both
        // strings in OpenCode's SDK shapes).
        let result = state
            .and_then(|s| s.get("output").or_else(|| s.get("error")))
            .and_then(Value::as_str)
            .map(str::to_string);
        let ordinal = invocations.len() as u32;
        invocations.push(ToolInvocation {
            name: name.to_string(),
            args,
            result: result.map(Value::String),
            ordinal,
        });
    }
    invocations
}

fn extract_events(records: &[Value]) -> Vec<TranscriptEvent> {
    let mut events = Vec::new();
    for record in records {
        let ordinal = events.len() as u32;
        match record.get("type").and_then(Value::as_str) {
            Some("tool_use") => {
                let Some(part) = record.get("part").and_then(Value::as_object) else {
                    continue;
                };
                let Some(name) = part.get("tool").and_then(Value::as_str) else {
                    continue;
                };
                let state = part.get("state");
                let args = state
                    .and_then(|s| s.get("input"))
                    .filter(|input| input.is_object())
                    .cloned();
                let result = state
                    .and_then(|s| s.get("output").or_else(|| s.get("error")))
                    .and_then(Value::as_str)
                    .map(|value| Value::String(value.to_string()));
                events.push(TranscriptEvent::ToolInvocation {
                    ordinal,
                    name: name.to_string(),
                    args,
                    result,
                });
            }
            Some("text") => {
                if let Some(text) = record
                    .get("part")
                    .and_then(|part| part.get("text"))
                    .and_then(Value::as_str)
                {
                    events.push(TranscriptEvent::AssistantMessage {
                        ordinal,
                        text: text.to_string(),
                    });
                }
            }
            _ => {}
        }
    }
    events
}

/// Parse an OpenCode `--format json` event stream into ordered tool
/// invocations.
pub fn parse_opencode_events(jsonl_path: &Path) -> io::Result<Vec<ToolInvocation>> {
    Ok(extract_invocations(&read_jsonl::<Value>(jsonl_path)?))
}

/// Parse an OpenCode `--format json` event stream into a full
/// [`TranscriptSummary`].
pub fn parse_opencode_events_full(jsonl_path: &Path) -> io::Result<TranscriptSummary> {
    let records = read_jsonl::<Value>(jsonl_path)?;

    let mut first_ts: Option<i64> = None;
    let mut last_ts: Option<i64> = None;
    let mut timestamp_count = 0usize;
    let mut final_text: Option<String> = None;
    let mut total_tokens: Option<i64> = None;
    let session_id = records
        .iter()
        .find_map(|record| record.get("sessionID").and_then(Value::as_str))
        .map(str::to_string);

    for record in &records {
        // OpenCode envelope timestamps are epoch milliseconds (numbers), not
        // RFC 3339 strings.
        if let Some(ts) = record.get("timestamp").and_then(Value::as_i64) {
            if first_ts.is_none() {
                first_ts = Some(ts);
            }
            last_ts = Some(ts);
            timestamp_count += 1;
        }

        let rtype = record.get("type").and_then(Value::as_str);

        if rtype == Some("text")
            && let Some(text) = record
                .get("part")
                .and_then(|p| p.get("text"))
                .and_then(Value::as_str)
        {
            final_text = Some(text.to_string());
        }

        if rtype == Some("step_finish")
            && let Some(tokens) = record
                .get("part")
                .and_then(|p| p.get("tokens"))
                .and_then(Value::as_object)
        {
            let get = |k: &str| tokens.get(k).and_then(Value::as_i64).unwrap_or(0);
            // cache.read/write excluded, matching the codex parser's accounting.
            let sum = get("input") + get("output") + get("reasoning");
            total_tokens = Some(total_tokens.unwrap_or(0) + sum);
        }
    }

    let duration_ms = match (first_ts, last_ts) {
        (Some(f), Some(l)) if timestamp_count >= 2 => Some(l - f),
        _ => None,
    };

    Ok(TranscriptSummary {
        tool_invocations: extract_invocations(&records),
        events: extract_events(&records),
        session_id,
        total_tokens,
        duration_ms,
        final_text,
    })
}

#[cfg(test)]
mod tests {
    use super::{parse_opencode_events, parse_opencode_events_full};
    use crate::core::ToolInvocation;
    use serde_json::{Value, json};
    use std::fs;
    use std::path::Path;
    use tempfile::TempDir;

    fn write_jsonl(path: &Path, lines: &[Value]) {
        let body = lines
            .iter()
            .map(|l| l.to_string())
            .collect::<Vec<_>>()
            .join("\n");
        fs::write(path, format!("{body}\n")).unwrap();
    }

    /// A `tool_use` envelope wrapping a tool part with the given state.
    fn tool_use(ts: i64, tool: &str, state: Value) -> Value {
        json!({
            "type": "tool_use",
            "timestamp": ts,
            "sessionID": "ses_1",
            "part": {
                "id": "prt_1",
                "sessionID": "ses_1",
                "messageID": "msg_1",
                "type": "tool",
                "callID": "call_1",
                "tool": tool,
                "state": state,
            }
        })
    }

    #[test]
    fn extracts_completed_and_errored_tool_calls_with_ordinals_args_and_results() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.jsonl");
        write_jsonl(
            &path,
            &[
                tool_use(
                    1_000,
                    "bash",
                    json!({"status": "completed", "input": {"command": "bun test", "description": "run tests"}, "output": "2 pass\n0 fail", "title": "bun test", "metadata": {}, "time": {"start": 900, "end": 1_000}}),
                ),
                tool_use(
                    2_000,
                    "edit",
                    json!({"status": "error", "input": {"filePath": "/tmp/x.md", "oldString": "a", "newString": "b"}, "error": "permission denied", "time": {"start": 1_900, "end": 2_000}}),
                ),
                tool_use(
                    3_000,
                    "read",
                    json!({"status": "completed", "output": "file contents", "title": "read", "metadata": {}, "time": {"start": 2_900, "end": 3_000}}),
                ),
            ],
        );

        let result = parse_opencode_events(&path).unwrap();
        assert_eq!(
            result,
            vec![
                ToolInvocation {
                    name: "bash".into(),
                    args: Some(json!({"command": "bun test", "description": "run tests"})),
                    result: Some(Value::String("2 pass\n0 fail".into())),
                    ordinal: 0,
                },
                ToolInvocation {
                    name: "edit".into(),
                    args: Some(
                        json!({"filePath": "/tmp/x.md", "oldString": "a", "newString": "b"})
                    ),
                    result: Some(Value::String("permission denied".into())),
                    ordinal: 1,
                },
                // No `input` object on the state → null args.
                ToolInvocation {
                    name: "read".into(),
                    args: None,
                    result: Some(Value::String("file contents".into())),
                    ordinal: 2,
                },
            ]
        );
    }

    #[test]
    fn skill_tool_calls_keep_the_name_argument_for_the_meta_check() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.jsonl");
        write_jsonl(
            &path,
            &[tool_use(
                1_000,
                "skill",
                json!({"status": "completed", "input": {"name": "slow-powers-eval-1-with-skill-mr-review"}, "output": "<skill_content/>", "title": "skill", "metadata": {}, "time": {"start": 900, "end": 1_000}}),
            )],
        );

        let invocations = parse_opencode_events(&path).unwrap();
        assert_eq!(invocations.len(), 1);
        assert_eq!(invocations[0].name, "skill");
        assert_eq!(
            invocations[0].args.as_ref().and_then(|a| a.get("name")),
            Some(&json!("slow-powers-eval-1-with-skill-mr-review"))
        );
    }

    #[test]
    fn non_tool_events_produce_no_invocations() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.jsonl");
        write_jsonl(
            &path,
            &[
                json!({"type": "step_start", "timestamp": 1_000, "sessionID": "ses_1", "part": {"id": "p1", "type": "step-start"}}),
                json!({"type": "reasoning", "timestamp": 2_000, "sessionID": "ses_1", "part": {"id": "p2", "type": "reasoning", "text": "thinking"}}),
                json!({"type": "text", "timestamp": 3_000, "sessionID": "ses_1", "part": {"id": "p3", "type": "text", "text": "hello"}}),
                json!({"type": "step_finish", "timestamp": 4_000, "sessionID": "ses_1", "part": {"id": "p4", "type": "step-finish", "reason": "stop", "cost": 0.0, "tokens": {"input": 1, "output": 1, "reasoning": 0, "cache": {"read": 0, "write": 0}}}}),
                json!({"type": "error", "timestamp": 5_000, "sessionID": "ses_1", "error": {"name": "UnknownError", "data": {"message": "boom"}}}),
            ],
        );
        assert_eq!(parse_opencode_events(&path).unwrap(), vec![]);
    }

    #[test]
    fn malformed_tool_use_parts_are_skipped() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.jsonl");
        write_jsonl(
            &path,
            &[
                json!({"type": "tool_use", "timestamp": 1_000, "sessionID": "ses_1"}),
                json!({"type": "tool_use", "timestamp": 2_000, "sessionID": "ses_1", "part": "not an object"}),
                json!({"type": "tool_use", "timestamp": 3_000, "sessionID": "ses_1", "part": {"id": "p1", "type": "tool"}}),
                json!({"type": "tool_use", "timestamp": 4_000, "sessionID": "ses_1", "part": {"id": "p2", "type": "tool", "tool": 7}}),
            ],
        );
        assert_eq!(parse_opencode_events(&path).unwrap(), vec![]);
    }

    #[test]
    fn skips_malformed_jsonl_lines() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.jsonl");
        let good = tool_use(
            1_000,
            "grep",
            json!({"status": "completed", "input": {"pattern": "todo"}, "output": "3 matches", "title": "grep", "metadata": {}, "time": {"start": 900, "end": 1_000}}),
        );
        fs::write(&path, format!("{good}\nnot valid json\n")).unwrap();
        assert_eq!(parse_opencode_events(&path).unwrap().len(), 1);
    }

    #[test]
    fn full_summary_extracts_final_text_token_sum_and_timestamp_duration() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.jsonl");
        write_jsonl(
            &path,
            &[
                json!({"type": "step_start", "timestamp": 1_000, "sessionID": "ses_1", "part": {"id": "p1", "type": "step-start"}}),
                tool_use(
                    2_000,
                    "bash",
                    json!({"status": "completed", "input": {"command": "ls"}, "output": "README.md", "title": "ls", "metadata": {}, "time": {"start": 1_900, "end": 2_000}}),
                ),
                json!({"type": "text", "timestamp": 3_000, "sessionID": "ses_1", "part": {"id": "p3", "type": "text", "text": "First."}}),
                json!({"type": "text", "timestamp": 4_000, "sessionID": "ses_1", "part": {"id": "p4", "type": "text", "text": "Final."}}),
                json!({"type": "step_finish", "timestamp": 5_000, "sessionID": "ses_1", "part": {"id": "p5", "type": "step-finish", "reason": "stop", "cost": 0.002, "tokens": {"input": 100, "output": 20, "reasoning": 5, "cache": {"read": 75, "write": 0}}}}),
                json!({"type": "step_finish", "timestamp": 6_000, "sessionID": "ses_1", "part": {"id": "p6", "type": "step-finish", "reason": "stop", "cost": 0.001, "tokens": {"input": 40, "output": 2, "reasoning": 0, "cache": {"read": 0, "write": 0}}}}),
            ],
        );

        let full = parse_opencode_events_full(&path).unwrap();
        assert_eq!(full.session_id.as_deref(), Some("ses_1"));
        assert_eq!(
            serde_json::to_value(&full.events).unwrap(),
            json!([
                {
                    "type": "tool_invocation",
                    "ordinal": 0,
                    "name": "bash",
                    "args": {"command": "ls"},
                    "result": "README.md"
                },
                {
                    "type": "assistant_message",
                    "ordinal": 1,
                    "text": "First."
                },
                {
                    "type": "assistant_message",
                    "ordinal": 2,
                    "text": "Final."
                }
            ])
        );
        assert_eq!(full.tool_invocations.len(), 1);
        assert_eq!(full.tool_invocations[0].name, "bash");
        assert_eq!(full.final_text, Some("Final.".into()));
        // cache.read (75) excluded, matching the codex parser's accounting.
        assert_eq!(full.total_tokens, Some(167));
        assert_eq!(full.duration_ms, Some(5_000));
    }

    #[test]
    fn returns_null_usage_and_duration_when_sparse() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.jsonl");
        write_jsonl(
            &path,
            &[
                json!({"type": "text", "timestamp": 1_000, "sessionID": "ses_1", "part": {"id": "p1", "type": "text", "text": "Done."}}),
            ],
        );
        let full = parse_opencode_events_full(&path).unwrap();
        assert_eq!(full.final_text, Some("Done.".into()));
        assert_eq!(full.total_tokens, None);
        assert_eq!(full.duration_ms, None);
    }

    #[test]
    fn step_finish_without_tokens_leaves_the_total_untouched() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("events.jsonl");
        write_jsonl(
            &path,
            &[
                json!({"type": "step_finish", "timestamp": 1_000, "sessionID": "ses_1", "part": {"id": "p1", "type": "step-finish", "reason": "tool-calls", "cost": 0.0}}),
                json!({"type": "step_finish", "timestamp": 2_000, "sessionID": "ses_1", "part": {"id": "p2", "type": "step-finish", "reason": "stop", "cost": 0.001, "tokens": {"input": 10, "output": 2, "reasoning": 0, "cache": {"read": 0, "write": 0}}}}),
            ],
        );
        assert_eq!(
            parse_opencode_events_full(&path).unwrap().total_tokens,
            Some(12)
        );
    }
}