supercode-harness 0.4.4

The optional native Supercode agent and tool harness
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Acceptance tests for SPEC.md A7 (agent-loop-integrated reversible
//! tool-output truncation), A9 (image/base64 redaction through the agent
//! write-through), and A10 (old-turn clearing; re-founded `maybe_compact`).
//!
//! `reduce_projection.rs` covers `project()`/`project_messages()` as pure
//! functions in isolation; this file covers the two things that only show up
//! once an `Agent` is driving the loop: (1) the request view is built from
//! the *projected* history, not raw history, and (2) `maybe_compact` no
//! longer performs destructive surgery once a `ReductionPolicy` is active.

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use supercode_harness::reduce::{
    invert, project, project_messages, reduction_id, ReductionKind, ReductionLog, ReductionPolicy,
    REDUCTION_SENTINEL,
};
use supercode_harness::session::Session;
use supercode_harness::sidecar::SidecarWriter;
use supercode_harness::{
    Agent, ChatMessage, ChatRequest, Config, FunctionCall, Provider, Role, ToolCall, Usage,
};

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

fn load_codex() -> Session {
    Session::from_codex(fixture("codex_session.jsonl")).unwrap()
}

fn tool_indices(session: &Session) -> Vec<usize> {
    session
        .messages
        .iter()
        .enumerate()
        .filter(|(_, m)| m.role == Role::Tool)
        .map(|(i, _)| i)
        .collect()
}

/// Overwrite the `ordinal`-th tool result's content with a deterministic,
/// all-ASCII filler string of exactly `target_len` bytes (same idiom as
/// `reduce_projection.rs::pad_tool_output`).
fn pad_tool_output(session: &mut Session, ordinal: usize, target_len: usize) {
    let idx = tool_indices(session)[ordinal];
    let filler: String = (0..target_len)
        .map(|i| (b'a' + (i % 26) as u8) as char)
        .collect();
    session.messages[idx].content = Some(filler);
}

/// `,`-thousands-separated formatting, matching the stub grammar's style
/// (mirrors the private `reduce::format_commas`).
fn comma(n: usize) -> String {
    let digits = n.to_string();
    let bytes = digits.as_bytes();
    let mut out = String::with_capacity(bytes.len() + bytes.len() / 3);
    for (i, b) in bytes.iter().enumerate() {
        if i > 0 && (bytes.len() - i) % 3 == 0 {
            out.push(',');
        }
        out.push(*b as char);
    }
    out
}

/// Total wire bytes of a message slice — the same "serialized wire bytes"
/// basis D12's token estimator uses.
fn wire_bytes(msgs: &[ChatMessage]) -> usize {
    serde_json::to_string(msgs).unwrap().len()
}

fn temp_dir(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "supercode-reduce-loop-{tag}-{}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

// ---------------------------------------------------------------------------
// A7(b): tool_truncation_largest_first_and_reversible
// ---------------------------------------------------------------------------

#[test]
fn tool_truncation_largest_first_and_reversible() {
    let mut session = load_codex();
    pad_tool_output(&mut session, 0, 200_000); // the older tool result
    pad_tool_output(&mut session, 1, 50_000); // the newer tool result

    // ---- one-reduction case: a trigger strictly between 50 KB and 200 KB
    // captures ONLY the largest output as a candidate. ----
    let policy_one = ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 60_000,
        protect_last_n_tool_results: 0,
        ..ReductionPolicy::default()
    };
    let (_, log_one) = project(&session, &policy_one, &ReductionLog::default());
    assert_eq!(
        log_one.reductions.len(),
        1,
        "only the 200KB output should cross a 60KB trigger"
    );
    match log_one.reductions[0].kind {
        ReductionKind::ToolOutputTruncated { original_bytes, .. } => {
            assert_eq!(
                original_bytes, 200_000,
                "the captured candidate must be the 200KB output"
            )
        }
        ref other => panic!("expected ToolOutputTruncated, got {other:?}"),
    }

    // ---- both-reduced case: trigger=8192 keep=4096 protect_last=0 ----
    let policy = ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 8192,
        protect_last_n_tool_results: 0,
        ..ReductionPolicy::default()
    };
    let (view, log) = project(&session, &policy, &ReductionLog::default());
    assert_eq!(
        log.reductions.len(),
        2,
        "both padded outputs exceed the 8KB trigger"
    );

    // Largest wins first (#6 "largest wins").
    let sizes: Vec<usize> = log
        .reductions
        .iter()
        .map(|r| match r.kind {
            ReductionKind::ToolOutputTruncated { original_bytes, .. } => original_bytes,
            _ => 0,
        })
        .collect();
    assert_eq!(
        sizes,
        vec![200_000, 50_000],
        "the largest tool output must be reduced first"
    );

    // The placeholder contains the sentinel, the id, and both byte counts
    // (comma-formatted).
    for r in &log.reductions {
        assert!(
            r.placeholder.starts_with(REDUCTION_SENTINEL),
            "{}",
            r.placeholder
        );
        assert!(r.placeholder.contains(&r.id), "{}", r.placeholder);
        let (original_bytes, kept_bytes) = match r.kind {
            ReductionKind::ToolOutputTruncated {
                original_bytes,
                kept_bytes,
            } => (original_bytes, kept_bytes),
            _ => unreachable!(),
        };
        assert!(
            r.placeholder.contains(&comma(original_bytes)),
            "{}",
            r.placeholder
        );
        assert!(
            r.placeholder.contains(&comma(kept_bytes)),
            "{}",
            r.placeholder
        );
    }

    // Both-reduced case inverts to the exact originals (A6).
    let inverted = invert(&view, &log, &session).unwrap();
    assert_eq!(inverted.len(), session.messages.len());
    for (a, b) in inverted.iter().zip(&session.messages) {
        assert_eq!(a.role, b.role);
        assert_eq!(a.content, b.content);
    }

    // Reduced-view total bytes < 10% of full-view bytes for this constructed
    // case.
    let full_bytes = wire_bytes(&session.messages);
    let reduced_bytes = wire_bytes(&view);
    assert!(
        (reduced_bytes as f64) < 0.10 * (full_bytes as f64),
        "reduced {reduced_bytes} should be < 10% of full {full_bytes}"
    );
}

// ---------------------------------------------------------------------------
// A7 loop test (extends A3's `agent_records_full_fidelity_while_capping_view`)
// ---------------------------------------------------------------------------

/// A tool whose output is comfortably over a small `tool_output_trigger_bytes`
/// but well under the agent's `max_tool_output_bytes` safety net — so
/// `cap_tool_output` never intervenes and the FULL output reaches `history`;
/// only the projection layer shrinks it at request-build time.
struct BigOutputTool;
#[async_trait]
impl supercode_harness::tools::Tool for BigOutputTool {
    fn name(&self) -> &str {
        "list_dir"
    }
    fn description(&self) -> &str {
        "x"
    }
    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({"type": "object"})
    }
    async fn execute(
        &self,
        _a: serde_json::Value,
        _c: &supercode_harness::tools::ToolContext,
    ) -> supercode_harness::Result<String> {
        Ok("Q".repeat(20_000))
    }
}

/// Turn 0: call the tool. Turn 1: capture the exact request the model sees
/// (before answering) so the test can inspect the wire body directly.
struct BigOutputThenCapture {
    calls: AtomicUsize,
    captured: Arc<Mutex<Option<Vec<ChatMessage>>>>,
}
#[async_trait]
impl Provider for BigOutputThenCapture {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let n = self.calls.fetch_add(1, Ordering::SeqCst);
        if n == 0 {
            let call = ChatMessage {
                role: Role::Assistant,
                content: None,
                content_parts: None,
                tool_calls: Some(vec![ToolCall {
                    id: "c1".into(),
                    kind: "function".into(),
                    function: FunctionCall {
                        name: "list_dir".into(),
                        arguments: "{}".into(),
                    },
                }]),
                tool_call_id: None,
                name: None,
                metadata: Default::default(),
            };
            Ok((call, Usage::default()))
        } else {
            *self.captured.lock().unwrap() = Some(req.messages.clone());
            Ok((ChatMessage::assistant("done"), Usage::default()))
        }
    }
}

#[tokio::test]
async fn loop_next_request_carries_reduced_view_not_full_output() {
    let dir = temp_dir("nextreq");
    let sidecar_path = dir.join("sess.sidecar.jsonl");

    // Default `max_tool_output_bytes` (100 KB) never trips for a 20 KB
    // output — it flows uncapped into `history`; the reduction layer's own
    // (much smaller) trigger is what shrinks the NEXT request's view.
    let config = Config::builder().cwd(dir.clone()).build();
    let mut reg = supercode_harness::tools::ToolRegistry::new();
    reg.register(BigOutputTool);
    let captured = Arc::new(Mutex::new(None));
    let mut agent = Agent::with_parts(
        config,
        Box::new(BigOutputThenCapture {
            calls: AtomicUsize::new(0),
            captured: captured.clone(),
        }),
        reg,
    );

    let empty_session = Session::from_claude_code_str("").unwrap();
    let writer = SidecarWriter::create(&sidecar_path, &empty_session).unwrap();
    agent.set_recorder(writer);
    agent.set_reduction_policy(ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 8192,
        protect_last_n_tool_results: 0,
        ..ReductionPolicy::default()
    });

    let reply = agent.send("go").await.unwrap();
    assert_eq!(reply, "done");

    // (i) `history` kept the FULL, uncapped tool output — cap_tool_output's
    // hard safety net never tripped for 20 KB.
    let full_in_history = agent
        .history()
        .iter()
        .rev()
        .find(|m| m.role == Role::Tool)
        .and_then(|m| m.content.clone())
        .unwrap();
    assert_eq!(full_in_history.len(), 20_000);

    // (ii) the NEXT request's wire body carries the placeholder, not the full
    // output, and no `metadata`/`sc.` bookkeeping ever reaches the wire.
    let captured_msgs = captured.lock().unwrap().clone().unwrap();
    let body = serde_json::to_string(&captured_msgs).unwrap();
    assert!(
        body.contains(REDUCTION_SENTINEL),
        "wire body missing reduction stub: {body}"
    );
    assert!(
        !body.contains(&"Q".repeat(20_000)),
        "wire body must not contain the full tool output"
    );
    assert!(
        !body.contains("\"metadata\""),
        "wire body must never carry the metadata key: {body}"
    );
    assert!(
        !body.contains("sc."),
        "wire body must never carry an sc.* pointer: {body}"
    );

    std::fs::remove_dir_all(&dir).ok();
}

// ---------------------------------------------------------------------------
// A10(b): turn_clearing_reversible_and_supersedes_compaction
// ---------------------------------------------------------------------------

/// Always answers with plain text (no tool calls, so `send` returns after one
/// model turn) — and records every request's message vector it was given, so
/// the test can inspect how the view evolved turn over turn.
struct PlainAnswerCapturing {
    calls: AtomicUsize,
    requests: Arc<Mutex<Vec<Vec<ChatMessage>>>>,
}
#[async_trait]
impl Provider for PlainAnswerCapturing {
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        let n = self.calls.fetch_add(1, Ordering::SeqCst);
        self.requests.lock().unwrap().push(req.messages.clone());
        Ok((
            ChatMessage::assistant(format!("reply {n}")),
            Usage::default(),
        ))
    }
}

#[tokio::test]
async fn turn_clearing_reversible_and_supersedes_compaction() {
    let dir = temp_dir("turnclear");
    let sidecar_path = dir.join("sess.sidecar.jsonl");

    let requests = Arc::new(Mutex::new(Vec::new()));
    let config = Config::builder()
        .cwd(dir.clone())
        .compact_after_messages(8)
        .build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(PlainAnswerCapturing {
            calls: AtomicUsize::new(0),
            requests: requests.clone(),
        }),
    );

    let empty_session = Session::from_claude_code_str("").unwrap();
    let writer = SidecarWriter::create(&sidecar_path, &empty_session).unwrap();
    agent.set_recorder(writer);
    // Tool-output knobs irrelevant here (no tool calls in this scenario);
    // only `clear_turns_older_than` (derived by `maybe_compact` from
    // `compact_after_messages`) matters.
    agent.set_reduction_policy(ReductionPolicy::default());

    for i in 0..12 {
        agent.send(format!("turn {i}")).await.unwrap();
    }

    // (ii) `agent.history()` length never decreases — it only ever grows:
    // the synthetic system prompt, plus one (user, assistant) pair per turn.
    assert_eq!(agent.history().len(), 1 + 12 * 2);

    let reqs = requests.lock().unwrap().clone();
    let placeholder_counts: Vec<usize> = reqs
        .iter()
        .map(|msgs| msgs.iter().filter(|m| reduction_id(m).is_some()).count())
        .collect();

    // (i) request views shrink after the trigger and contain exactly one
    // `TurnsCleared` placeholder — never zero-after-triggering, never more
    // than one (it is a singleton reduction, A10).
    assert!(
        placeholder_counts.contains(&1),
        "no request ever carried the TurnsCleared placeholder: {placeholder_counts:?}"
    );
    assert!(
        placeholder_counts.iter().all(|&c| c <= 1),
        "a request carried more than one TurnsCleared placeholder: {placeholder_counts:?}"
    );
    let last_req = reqs.last().unwrap();
    assert!(
        last_req.len() < agent.history().len(),
        "the final request view ({}) should be smaller than full history ({})",
        last_req.len(),
        agent.history().len()
    );

    // (iii) sidecar reload yields all 12 turns (every message, full
    // fidelity) — clearing only ever shrinks the projected VIEW, never
    // `history` or the sidecar.
    let raw = std::fs::read_to_string(&sidecar_path).unwrap();
    let reloaded = Session::from_native_str(&raw).unwrap();
    assert_eq!(
        reloaded.messages.len(),
        agent.history().len() - 1,
        "sidecar must retain every turn, uncleared"
    );

    // (iv) `invert` of the final projected view (reconstructed via
    // `project_messages` over `history[1..]` with the final log) is
    // identical to `history[1..]`.
    let policy = agent.reduction_policy().unwrap().clone();
    let (final_view, final_log) =
        project_messages(&agent.history()[1..], &policy, agent.reduction_log());
    assert_eq!(
        &final_log,
        agent.reduction_log(),
        "re-projecting the final state with its own log must not invent new reductions"
    );
    let inverted = invert(&final_view, &final_log, &reloaded).unwrap();
    assert_eq!(inverted.len(), agent.history().len() - 1);
    for (a, b) in inverted.iter().zip(&agent.history()[1..]) {
        assert_eq!(a.role, b.role);
        assert_eq!(a.content, b.content);
    }

    // (v) with no policy at all, `maybe_compact`'s legacy in-place-surgery
    // behavior is unchanged — covered by the pre-existing
    // `agent_loop.rs::slash_prompt_expansion_and_compaction` test, which
    // still passes byte-identically (D6: this file changes nothing about
    // the no-policy path).

    std::fs::remove_dir_all(&dir).ok();
}

// ---------------------------------------------------------------------------
// Determinism (A5's contract, exercised through the loop-facing entry point)
// ---------------------------------------------------------------------------

#[test]
fn project_messages_is_byte_identical_across_repeated_calls() {
    let mut session = load_codex();
    pad_tool_output(&mut session, 0, 100_000);
    let policy = ReductionPolicy {
        tool_output_keep_bytes: 4096,
        tool_output_trigger_bytes: 8192,
        protect_last_n_tool_results: 0,
        clear_turns_older_than: Some(4),
        ..ReductionPolicy::default()
    };
    let prior = ReductionLog::default();

    let (view1, log1) = project_messages(&session.messages, &policy, &prior);
    let (view2, log2) = project_messages(&session.messages, &policy, &prior);

    let wire1 = serde_json::to_string(&view1).unwrap();
    let wire2 = serde_json::to_string(&view2).unwrap();
    assert_eq!(
        wire1, wire2,
        "identical inputs must produce a byte-identical view"
    );
    assert_eq!(
        log1, log2,
        "identical inputs must produce a byte-identical log"
    );
}

// ---------------------------------------------------------------------------
// A9(b): data_url_image_redacted_and_inverted
// ---------------------------------------------------------------------------

/// Always answers `"described"` with no tool calls — just enough of a
/// provider to let `send_with_images` complete one loop iteration.
struct SaysDescribed;
#[async_trait]
impl Provider for SaysDescribed {
    async fn complete(
        &self,
        _req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        Ok((ChatMessage::assistant("described"), Usage::default()))
    }
}

#[tokio::test]
async fn data_url_image_redacted_and_inverted() {
    let dir = temp_dir("images");
    let sidecar_path = dir.join("sess.sidecar.jsonl");

    // A ~400KB `data:` URL image, as SPEC.md A9(b) calls for.
    let big_image = format!("data:image/png;base64,{}", "A".repeat(400_000));

    let config = Config::builder().cwd(dir.clone()).build();
    let mut agent = Agent::with_provider(config, Box::new(SaysDescribed));

    let empty_session = Session::from_claude_code_str("").unwrap();
    let writer = SidecarWriter::create(&sidecar_path, &empty_session).unwrap();
    agent.set_recorder(writer);
    agent.set_reduction_policy(ReductionPolicy {
        redact_images: true,
        ..ReductionPolicy::default()
    });

    let reply = agent
        .send_with_images("caption", std::slice::from_ref(&big_image))
        .await
        .unwrap();
    assert_eq!(reply, "described");

    // Reload the full-fidelity sidecar (A3 write-through) — the ONE canonical
    // model `project`/`invert` operate against.
    let raw = std::fs::read_to_string(&sidecar_path).unwrap();
    let sidecar_session = Session::from_native_str(&raw).unwrap();
    assert_eq!(
        sidecar_session.messages.len(),
        2,
        "sidecar records the multimodal user turn and the assistant reply"
    );
    let user_idx = sidecar_session
        .messages
        .iter()
        .position(|m| m.role == Role::User)
        .unwrap();
    assert!(
        sidecar_session.messages[user_idx].content_parts.is_some(),
        "the recorded user message must keep its full-fidelity image part"
    );

    let policy = agent.reduction_policy().unwrap().clone();
    let (view, log) = project(&sidecar_session, &policy, &ReductionLog::default());

    assert_eq!(
        log.reductions.len(),
        1,
        "exactly one ImageRedacted reduction for the single over-threshold image"
    );
    assert!(matches!(
        log.reductions[0].kind,
        ReductionKind::ImageRedacted { .. }
    ));

    // The reduced message's content_parts carries the caption text part plus
    // the redaction stub — never the original image.
    let parts = view[user_idx].content_parts.as_ref().unwrap();
    assert_eq!(parts.len(), 2, "caption text part + redaction stub");
    assert_eq!(parts[0]["type"], "text");
    assert_eq!(parts[0]["text"], "caption");
    assert_eq!(parts[1]["type"], "text");
    let stub_text = parts[1]["text"].as_str().unwrap();
    assert!(stub_text.starts_with(REDUCTION_SENTINEL), "{stub_text}");
    assert!(stub_text.contains("image/png"), "{stub_text}");

    // No `data:` substring anywhere in the serialized reduced request body
    // (system prompt + projected view — the exact shape `build_request_messages`
    // assembles).
    let mut request_body = vec![agent.history()[0].clone()];
    request_body.extend(view.clone());
    let body = serde_json::to_string(&request_body).unwrap();
    assert!(
        !body.contains("data:"),
        "reduced request body must never carry the raw data: URL: {body}"
    );

    // Invert restores the part byte-identically.
    let inverted = invert(&view, &log, &sidecar_session).unwrap();
    assert_eq!(
        inverted[user_idx].content_parts, sidecar_session.messages[user_idx].content_parts,
        "invert must restore the original image part byte-identically"
    );

    std::fs::remove_dir_all(&dir).ok();
}