supercode-core 0.1.0

A lightweight, fully-customizable AI coding agent SDK in Rust. Talks to any model via OpenRouter or any OpenAI-compatible endpoint.
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
414
415
416
417
418
419
420
421
422
423
424
//! TDD suite for *saving* sessions back out in Claude Code and Codex formats.
//!
//! The GIMP model: one canonical in-memory representation, with importers AND
//! exporters per format. The core correctness property is a **semantic
//! round-trip** — load a real session, save it in the same format, load it
//! again, and get the same conversation back.

use std::path::{Path, PathBuf};

use supercode::session::{Session, SessionFormat};
use supercode::{ChatMessage, Role};

fn fixture(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures")
        .join(name)
}

/// Compare two messages for semantic (not byte) equality: role, text content,
/// tool-result linkage, and tool calls (name, id, and *parsed* arguments).
fn msg_eq(a: &ChatMessage, b: &ChatMessage) -> bool {
    if a.role != b.role || a.content != b.content || a.tool_call_id != b.tool_call_id {
        return false;
    }
    let (ca, cb) = (a.tool_calls(), b.tool_calls());
    if ca.len() != cb.len() {
        return false;
    }
    ca.iter().zip(cb).all(|(x, y)| {
        x.id == y.id
            && x.function.name == y.function.name
            && x.function.parsed_arguments().ok() == y.function.parsed_arguments().ok()
    })
}

fn assert_messages_eq(a: &Session, b: &Session) {
    assert_eq!(
        a.messages.len(),
        b.messages.len(),
        "message count changed across round-trip"
    );
    for (i, (x, y)) in a.messages.iter().zip(&b.messages).enumerate() {
        assert!(
            msg_eq(x, y),
            "message {i} differs across round-trip:\n  before: {x:?}\n  after:  {y:?}"
        );
    }
}

#[test]
fn claude_code_round_trips() {
    let original = Session::from_claude_code(fixture("claude_code_session.jsonl")).unwrap();
    let jsonl = original.to_jsonl(SessionFormat::ClaudeCode);
    let reloaded = Session::from_claude_code_str(&jsonl).unwrap();

    assert_messages_eq(&original, &reloaded);
    assert_eq!(original.meta.session_id, reloaded.meta.session_id);
    assert_eq!(original.meta.model, reloaded.meta.model);
    assert_eq!(original.meta.cwd, reloaded.meta.cwd);
}

#[test]
fn codex_round_trips() {
    let original = Session::from_codex(fixture("codex_session.jsonl")).unwrap();
    let jsonl = original.to_jsonl(SessionFormat::Codex);
    let reloaded = Session::from_codex_str(&jsonl).unwrap();

    assert_messages_eq(&original, &reloaded);
    assert_eq!(original.meta.session_id, reloaded.meta.session_id);
    assert_eq!(original.meta.model, reloaded.meta.model);
    assert_eq!(original.meta.cwd, reloaded.meta.cwd);
    // Codex carries the base instructions; they must survive the round-trip.
    assert_eq!(original.meta.system_prompt, reloaded.meta.system_prompt);
}

#[test]
fn saved_output_is_valid_jsonl_for_each_format() {
    let s = Session::from_codex(fixture("codex_session.jsonl")).unwrap();

    let cc = s.to_jsonl(SessionFormat::ClaudeCode);
    for line in cc.lines().filter(|l| !l.trim().is_empty()) {
        let v: serde_json::Value = serde_json::from_str(line).expect("CC line must be JSON");
        assert!(v.get("type").is_some(), "CC lines carry a top-level type");
        assert!(
            v.get("payload").is_none(),
            "CC lines have no payload envelope"
        );
    }

    let cx = s.to_jsonl(SessionFormat::Codex);
    for line in cx.lines().filter(|l| !l.trim().is_empty()) {
        let v: serde_json::Value = serde_json::from_str(line).expect("Codex line must be JSON");
        assert!(
            v.get("payload").is_some(),
            "Codex lines carry a payload envelope"
        );
    }
}

#[test]
fn cross_format_export_preserves_the_conversation() {
    // GIMP "export as": converting Codex → Claude Code is allowed to drop
    // format-specific framing (a Codex developer/system turn has no Claude Code
    // transcript slot), but the user/assistant/tool conversation must survive.
    let codex = Session::from_codex(fixture("codex_session.jsonl")).unwrap();
    let as_cc = codex.to_jsonl(SessionFormat::ClaudeCode);
    let reloaded = Session::from_claude_code_str(&as_cc).unwrap();

    let convo = |s: &Session| -> Vec<ChatMessage> {
        s.messages
            .iter()
            .filter(|m| m.role != Role::System)
            .cloned()
            .collect()
    };
    let before = convo(&codex);
    let after = convo(&reloaded);
    assert_eq!(before.len(), after.len());
    for (x, y) in before.iter().zip(&after) {
        assert!(
            msg_eq(x, y),
            "cross-format conversation differs:\n{x:?}\n{y:?}"
        );
    }
}

#[test]
fn save_writes_a_file() {
    let s = Session::from_claude_code(fixture("claude_code_session.jsonl")).unwrap();
    let out = std::env::temp_dir().join(format!("supercode-save-{}.jsonl", std::process::id()));
    s.save(&out, SessionFormat::ClaudeCode).unwrap();
    let reloaded = Session::load(&out).unwrap();
    assert_messages_eq(&s, &reloaded);
    std::fs::remove_file(&out).ok();
}

// ---- P4: lossless native format + file-history-snapshot retention ---------

#[test]
fn native_format_round_trips_losslessly_inline() {
    // A Claude transcript including a file-history-snapshot, which has no
    // canonical message representation and is dropped by normalization.
    let jsonl = concat!(
        r#"{"type":"user","message":{"role":"user","content":"hi"},"sessionId":"s","cwd":"/tmp"}"#,
        "\n",
        r#"{"type":"file-history-snapshot","messageId":"m1","snapshot":{"files":{"/a.rs":"old"}},"isSnapshotUpdate":false}"#,
        "\n",
        r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"ok"}]},"sessionId":"s"}"#,
    );
    let original = Session::from_claude_code_str(jsonl).unwrap();

    // Normalization drops the snapshot from the conversation…
    assert!(!original.messages.iter().any(|m| m
        .content
        .as_deref()
        .unwrap_or("")
        .contains("snapshot")));
    // …but the raw record is retained.
    assert!(original
        .raw
        .iter()
        .any(|l| l.contains("file-history-snapshot")));

    // Native round-trip is byte-for-byte lossless on the raw lines.
    let native = original.to_native_jsonl();
    let reloaded = Session::from_native_str(&native).unwrap();
    assert_eq!(
        original.raw, reloaded.raw,
        "native round-trip must be lossless"
    );
    assert_eq!(reloaded.meta.source, SessionFormat::ClaudeCode.source());
    // The file-history-snapshot survives the native round-trip.
    assert!(reloaded
        .raw
        .iter()
        .any(|l| l.contains("file-history-snapshot")));
}

/// Corpus proof: real sessions (both formats) round-trip losslessly through the
/// native format — every original line is preserved verbatim.
#[test]
#[ignore = "requires local session corpus; set SUPERCODE_CORPUS=1"]
fn native_format_lossless_over_corpus() {
    if std::env::var("SUPERCODE_CORPUS").is_err() {
        panic!(
            "SUPERCODE_CORPUS not set — this corpus test asserts nothing without \
             the maintainer's local session logs; set SUPERCODE_CORPUS=1 to run it."
        );
    }
    let home = std::env::var("HOME").unwrap();
    let mut checked = 0usize;
    for sub in [".claude/projects", ".codex/sessions"] {
        let dir = PathBuf::from(&home).join(sub);
        for path in jsonl_files(&dir).into_iter().take(300) {
            let Ok(original) = Session::load(&path) else {
                continue;
            };
            if original.raw.is_empty() {
                continue;
            }
            let native = original.to_native_jsonl();
            let reloaded = Session::from_native_str(&native).unwrap();
            assert_eq!(
                original.raw,
                reloaded.raw,
                "{}: native round-trip not lossless",
                path.display()
            );
            checked += 1;
        }
    }
    eprintln!("native lossless round-trip verified on {checked} real sessions");
    assert!(checked > 0);
}

// ---- P4: Codex execution-settings + lineage survive round-trip ------------

#[test]
fn codex_turn_context_and_lineage_survive_round_trip() {
    // A Codex rollout whose header carries execution settings (turn_context) and
    // session lineage (session_meta) that have no slot in the canonical message
    // model. They must survive load -> save -> load because the exporter replays
    // the original header records verbatim.
    let jsonl = r#"{"type":"session_meta","payload":{"id":"sess-1","cwd":"/tmp","originator":"codex_exec","cli_version":"0.141.0","model_provider":"openai","thread_source":"subagent","forked_from_id":"parent-9","source":{"subagent":{"thread_spawn":{"parent_thread_id":"parent-9","depth":2,"agent_role":"worker","agent_nickname":"Euler"}}}}}
{"type":"turn_context","payload":{"model":"gpt-5.5","approval_policy":"never","sandbox_policy":"read-only","effort":"high","personality":"pragmatic","user_instructions":"be terse","collaboration_mode":"solo","workspace_roots":["/tmp"],"truncation_policy":"auto","permission_profile":"default"}}
{"type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"hello"}]}}"#;

    let original = Session::from_codex(write_temp(jsonl))
        .unwrap_or_else(|_| Session::from_codex_str(jsonl).unwrap());
    let saved = original.to_jsonl(SessionFormat::Codex);

    // Every execution setting + lineage key is present verbatim in the export.
    for needle in [
        "\"approval_policy\":\"never\"",
        "\"sandbox_policy\":\"read-only\"",
        "\"effort\":\"high\"",
        "\"personality\":\"pragmatic\"",
        "\"user_instructions\":\"be terse\"",
        "\"collaboration_mode\":\"solo\"",
        "\"truncation_policy\":\"auto\"",
        "\"permission_profile\":\"default\"",
        "\"originator\":\"codex_exec\"",
        "\"cli_version\":\"0.141.0\"",
        "\"forked_from_id\":\"parent-9\"",
        "\"thread_source\":\"subagent\"",
        "\"agent_nickname\":\"Euler\"",
    ] {
        assert!(
            saved.contains(needle),
            "round-trip dropped {needle}\n{saved}"
        );
    }

    // And it still loads, with lineage recovered.
    let reloaded = Session::from_codex_str(&saved).unwrap();
    assert_eq!(reloaded.meta.session_id.as_deref(), Some("sess-1"));
    assert_eq!(reloaded.meta.model.as_deref(), Some("gpt-5.5"));
    assert_eq!(
        reloaded
            .meta
            .lineage
            .get("forked_from_id")
            .map(String::as_str),
        Some("parent-9")
    );
    assert_eq!(
        reloaded
            .meta
            .lineage
            .get("agent_nickname")
            .map(String::as_str),
        Some("Euler")
    );
}

fn write_temp(jsonl: &str) -> std::path::PathBuf {
    let p = std::env::temp_dir().join(format!("sc-p4-{}.jsonl", std::process::id()));
    std::fs::write(&p, jsonl).unwrap();
    p
}

// ---- header fidelity (verified against the real codex binary) -------------
//
// The stock `codex` CLI validates the rollout header strictly ("does not start
// with session metadata"). These guard the two ways we produce that header.

#[test]
fn codex_export_replays_original_header_and_overrides_id() {
    let mut s = Session::from_codex(fixture("codex_session.jsonl")).unwrap();
    assert!(
        !s.meta.codex_headers.is_empty(),
        "header should be captured on load"
    );
    s.meta.session_id = Some("new-id-123".into());

    let jsonl = s.to_jsonl(SessionFormat::Codex);
    let first: serde_json::Value = serde_json::from_str(jsonl.lines().next().unwrap()).unwrap();
    assert_eq!(first["type"], "session_meta");
    assert_eq!(
        first["payload"]["id"], "new-id-123",
        "id override must apply"
    );
    // The fields the real codex reader expects are present because we replay
    // the original header verbatim.
    for k in [
        "cwd",
        "model_provider",
        "originator",
        "source",
        "base_instructions",
    ] {
        assert!(
            first["payload"].get(k).is_some(),
            "replayed header missing `{k}`"
        );
    }
}

#[test]
fn synthesized_codex_header_has_required_fields() {
    // A Claude Code source has no codex header to replay, so the writer
    // synthesizes one — it must still carry the fields codex requires.
    let s = Session::from_claude_code(fixture("claude_code_session.jsonl")).unwrap();
    assert!(s.meta.codex_headers.is_empty());

    let jsonl = s.to_jsonl(SessionFormat::Codex);
    let first: serde_json::Value = serde_json::from_str(jsonl.lines().next().unwrap()).unwrap();
    assert_eq!(first["type"], "session_meta");
    for k in [
        "id",
        "cwd",
        "originator",
        "cli_version",
        "source",
        "thread_source",
        "model_provider",
    ] {
        assert!(
            first["payload"].get(k).is_some(),
            "synthesized header missing `{k}`"
        );
    }
}

// ---- corpus round-trip (opt-in) -------------------------------------------

#[test]
#[ignore = "requires local session corpus; set SUPERCODE_CORPUS=1"]
fn corpus_round_trips() {
    if std::env::var("SUPERCODE_CORPUS").is_err() {
        panic!(
            "SUPERCODE_CORPUS not set — this corpus test asserts nothing without \
             the maintainer's local session logs; set SUPERCODE_CORPUS=1 to run it."
        );
    }
    let home = std::env::var("HOME").unwrap();
    let cases = [
        (
            PathBuf::from(&home).join(".claude/projects"),
            SessionFormat::ClaudeCode,
            500usize,
        ),
        (
            PathBuf::from(&home).join(".codex/sessions"),
            SessionFormat::Codex,
            500usize,
        ),
    ];

    let mut checked = 0;
    let mut stable = 0;
    for (dir, format, limit) in cases {
        for path in jsonl_files(&dir).into_iter().take(limit) {
            let Ok(original) = Session::load(&path) else {
                continue;
            };
            if original.messages.is_empty() {
                continue;
            }
            checked += 1;
            let jsonl = original.to_jsonl(format);
            let reloaded = match Session::load_str(&jsonl, format) {
                Ok(s) => s,
                Err(e) => {
                    eprintln!("re-parse failed {}: {e}", path.display());
                    continue;
                }
            };
            if original.messages.len() == reloaded.messages.len()
                && original
                    .messages
                    .iter()
                    .zip(&reloaded.messages)
                    .all(|(a, b)| msg_eq(a, b))
            {
                stable += 1;
            } else {
                eprintln!("NOT stable: {}", path.display());
            }
        }
    }

    eprintln!("round-trip: checked={checked} stable={stable}");
    assert!(checked > 0);
    // Saving must reproduce the conversation for the overwhelming majority.
    assert!(
        stable as f64 / checked as f64 > 0.97,
        "round-trip unstable: {stable}/{checked}"
    );
}

fn jsonl_files(dir: &Path) -> Vec<PathBuf> {
    let mut out = Vec::new();
    let walker = ignore::WalkBuilder::new(dir)
        .standard_filters(false)
        .build();
    for entry in walker.flatten() {
        let p = entry.into_path();
        if p.extension().and_then(|e| e.to_str()) == Some("jsonl") {
            out.push(p);
        }
    }
    out
}