supercode-harness 0.4.11

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
//! P4b (COMPOSABLE-HARNESS-DESIGN.md §5.2 "P4", context-assembly & session-
//! record subset): behavioral tests for the stop-gate hook, the steering
//! queue, the compaction pressure trigger + focus instructions, persisted
//! per-turn usage records, and auto-title — driven end to end with a mock
//! provider, no network, mirroring `agent_loop.rs`'s pattern.

use std::sync::atomic::{AtomicUsize, Ordering};

use async_trait::async_trait;
use supercode_harness::session_title::SessionTitler;
use supercode_harness::{
    Agent, ChatMessage, ChatRequest, Config, Provider, Role, SessionStore, Usage,
};

fn temp_dir(tag: &str) -> std::path::PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "supercode-p4b-{tag}-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

// ============================== stop-gate ================================

/// A provider that always answers with plain text (no tool calls), so the
/// loop's idle/stop-gate path fires on every model round-trip.
struct AlwaysFinalProvider {
    calls: AtomicUsize,
}

#[async_trait]
impl Provider for AlwaysFinalProvider {
    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);
        Ok((
            ChatMessage::assistant(format!("final-{n}")),
            Usage::default(),
        ))
    }
}

#[tokio::test]
async fn stop_gate_default_off_returns_immediately() {
    let config = Config::builder().build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(AlwaysFinalProvider {
            calls: AtomicUsize::new(0),
        }),
    );
    let reply = agent.send("go").await.unwrap();
    assert_eq!(reply, "final-0", "no stop_gate installed: stop immediately");
}

#[tokio::test]
async fn stop_gate_vetoes_once_then_allows_the_second_final_answer() {
    let veto_calls = std::sync::Arc::new(AtomicUsize::new(0));
    let vc = veto_calls.clone();
    let config = Config::builder()
        .stop_gate(Box::new(move |_content: &str| {
            let n = vc.fetch_add(1, Ordering::SeqCst);
            if n == 0 {
                Some("not done yet, keep going".to_string())
            } else {
                None
            }
        }))
        .build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(AlwaysFinalProvider {
            calls: AtomicUsize::new(0),
        }),
    );
    let reply = agent.send("go").await.unwrap();
    assert_eq!(reply, "final-1", "veto forces a second model round-trip");
    assert_eq!(
        veto_calls.load(Ordering::SeqCst),
        2,
        "gate consulted once per would-be-final answer"
    );
    // The veto reason was injected as a real user message in history.
    assert!(agent
        .history()
        .iter()
        .any(|m| m.role == Role::User && m.content.as_deref() == Some("not done yet, keep going")));
}

#[tokio::test]
async fn stop_gate_still_bounded_by_max_iterations_if_it_always_vetoes() {
    let config = Config::builder()
        .max_iterations(3)
        .stop_gate(Box::new(|_: &str| Some("never stop".to_string())))
        .build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(AlwaysFinalProvider {
            calls: AtomicUsize::new(0),
        }),
    );
    let err = agent.send("go").await.unwrap_err();
    assert!(
        matches!(err, supercode_harness::Error::MaxIterations(3)),
        "an always-vetoing gate must not loop forever — bounded by max_iterations: {err:?}"
    );
}

// ============================== steering ==================================

struct AssertingProvider<F> {
    check: F,
}

#[async_trait]
impl<F> Provider for AssertingProvider<F>
where
    F: Fn(&ChatRequest) -> ChatMessage + Send + Sync,
{
    async fn complete(
        &self,
        req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        Ok(((self.check)(req), Usage::default()))
    }
}

#[tokio::test]
async fn queued_steer_message_delivered_one_at_a_time_and_leftover_persists_for_next_turn() {
    let provider = AssertingProvider {
        check: |req: &ChatRequest| {
            assert!(
                req.messages
                    .iter()
                    .any(|m| m.content.as_deref() == Some("first")),
                "queued steer message must be injected before this request"
            );
            assert!(
                !req.messages
                    .iter()
                    .any(|m| m.content.as_deref() == Some("second")),
                "one-at-a-time mode must not deliver the second queued message yet"
            );
            ChatMessage::assistant("ok")
        },
    };
    let config = Config::builder().build();
    let mut agent = Agent::with_provider(config, Box::new(provider));
    agent.queue_steer("first");
    agent.queue_steer("second");
    let reply = agent.send("go").await.unwrap();
    assert_eq!(reply, "ok");
    assert_eq!(
        agent.queued_steer_count(),
        1,
        "the second steer message stays queued for the next turn"
    );
}

#[tokio::test]
async fn steering_mode_all_drains_every_queued_message_at_once() {
    let provider = AssertingProvider {
        check: |req: &ChatRequest| {
            let combined = req
                .messages
                .iter()
                .rev()
                .find(|m| m.role == Role::User && m.content.as_deref() != Some("go"))
                .and_then(|m| m.content.clone());
            assert_eq!(combined.as_deref(), Some("first\n\nsecond"));
            ChatMessage::assistant("ok")
        },
    };
    let config = Config::builder()
        .steering_mode(supercode_harness::SteeringMode::All)
        .build();
    let mut agent = Agent::with_provider(config, Box::new(provider));
    agent.queue_steer("first");
    agent.queue_steer("second");
    agent.send("go").await.unwrap();
    assert_eq!(
        agent.queued_steer_count(),
        0,
        "All mode drains everything at once"
    );
}

#[tokio::test]
async fn queued_follow_up_message_is_delivered_at_idle_and_continues_the_loop() {
    struct FollowUpProvider {
        calls: AtomicUsize,
    }
    #[async_trait]
    impl Provider for FollowUpProvider {
        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 {
                Ok((ChatMessage::assistant("first answer"), Usage::default()))
            } else {
                assert!(
                    req.messages
                        .iter()
                        .any(|m| m.content.as_deref() == Some("follow up question")),
                    "follow-up message must be injected once the loop went idle"
                );
                Ok((ChatMessage::assistant("second answer"), Usage::default()))
            }
        }
    }
    let config = Config::builder().build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(FollowUpProvider {
            calls: AtomicUsize::new(0),
        }),
    );
    agent.queue_follow_up("follow up question");
    let reply = agent.send("go").await.unwrap();
    assert_eq!(reply, "second answer");
    assert_eq!(agent.queued_steer_count(), 0);
}

#[tokio::test]
async fn queued_follow_up_takes_priority_over_a_pending_stop_gate_veto() {
    // A queued follow-up is more input to answer, not a veto of an answer
    // already given — it should be delivered without ever consulting the
    // stop gate for that idle point.
    struct OnceProvider {
        calls: AtomicUsize,
    }
    #[async_trait]
    impl Provider for OnceProvider {
        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);
            Ok((
                ChatMessage::assistant(format!("answer-{n}")),
                Usage::default(),
            ))
        }
    }
    let gate_calls = std::sync::Arc::new(AtomicUsize::new(0));
    let gc = gate_calls.clone();
    let config = Config::builder()
        .stop_gate(Box::new(move |_: &str| {
            gc.fetch_add(1, Ordering::SeqCst);
            None
        }))
        .build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(OnceProvider {
            calls: AtomicUsize::new(0),
        }),
    );
    agent.queue_follow_up("one more thing");
    let reply = agent.send("go").await.unwrap();
    assert_eq!(reply, "answer-1");
    assert_eq!(
        gate_calls.load(Ordering::SeqCst),
        1,
        "stop_gate is skipped entirely on the idle point where a follow-up was delivered instead"
    );
}

// ============================ compaction pressure ==========================

struct EchoLongProvider;

#[async_trait]
impl Provider for EchoLongProvider {
    async fn complete(
        &self,
        _req: &ChatRequest,
        _on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        Ok((ChatMessage::assistant("x".repeat(3000)), Usage::default()))
    }
}

#[tokio::test]
async fn compaction_pressure_trigger_fires_and_appends_focus_instructions() {
    // An unrecognized model id falls back to `UNKNOWN_MODEL_CONTEXT_FLOOR`
    // (200,000 tokens, see `provider::model_context_limit`'s doc comment) —
    // `reserve_tokens` just under that floor means any nontrivial history
    // trips the pressure trigger (`used + reserve > limit`).
    let config = Config::builder()
        .model("totally-unrecognized-model-xyz")
        .compaction_pressure(199_000, 500)
        .compaction_focus_instructions("stay focused on the login bug")
        .build();
    let mut agent = Agent::with_provider(config, Box::new(EchoLongProvider));
    for i in 0..8 {
        agent.send(format!("turn {i}")).await.unwrap();
    }
    let compacted = agent.history().iter().any(|m| {
        m.role == Role::System
            && m.content
                .as_deref()
                .is_some_and(|c| c.contains("earlier conversation compacted"))
    });
    assert!(compacted, "pressure trigger must have fired across 8 turns");
    let has_focus = agent.history().iter().any(|m| {
        m.content
            .as_deref()
            .is_some_and(|c| c.contains("Focus: stay focused on the login bug"))
    });
    assert!(
        has_focus,
        "the marker must carry the configured focus instructions"
    );
}

#[tokio::test]
async fn compaction_pressure_trigger_off_by_default_never_fires() {
    // No `compaction_reserve_tokens` set (the default): history grows
    // unbounded across many turns, byte-identical to pre-P4b behavior.
    let config = Config::builder()
        .model("totally-unrecognized-model-xyz")
        .build();
    let mut agent = Agent::with_provider(config, Box::new(EchoLongProvider));
    for i in 0..8 {
        agent.send(format!("turn {i}")).await.unwrap();
    }
    let compacted = agent.history().iter().any(|m| {
        m.content
            .as_deref()
            .is_some_and(|c| c.contains("compacted"))
    });
    assert!(
        !compacted,
        "pressure trigger must stay off when unconfigured"
    );
}

// ============================ usage records =================================

struct UsageProvider {
    calls: AtomicUsize,
}

#[async_trait]
impl Provider for UsageProvider {
    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) as u64;
        let usage = Usage {
            prompt_tokens: 100 + n,
            completion_tokens: 10 + n,
            total_tokens: 110 + 2 * n,
            prompt_tokens_details: None,
        };
        Ok((ChatMessage::assistant(format!("answer {n}")), usage))
    }
}

#[tokio::test]
async fn usage_records_accumulate_per_turn_and_round_trip_losslessly_through_the_store() {
    let config = Config::builder().model("test-model").build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(UsageProvider {
            calls: AtomicUsize::new(0),
        }),
    );
    agent.send("q1").await.unwrap();
    agent.send("q2").await.unwrap();

    let records = agent.usage_records().to_vec();
    assert_eq!(records.len(), 2);
    assert_eq!(records[0].turn, 0);
    assert_eq!(records[0].model, "test-model");
    assert_eq!(records[0].prompt_tokens, 100);
    assert_eq!(records[1].turn, 1);
    assert_eq!(records[1].prompt_tokens, 101);

    let dir = temp_dir("usage-log-roundtrip");
    let store = SessionStore::open(&dir).unwrap();
    agent.save_usage_log(&store, "sess1").unwrap();
    let loaded = store.load_usage_log("sess1").unwrap();
    assert_eq!(
        loaded, records,
        "usage log must round-trip losslessly through the store"
    );

    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn usage_log_is_empty_by_default_no_calls_made_yet() {
    let config = Config::builder().build();
    let agent = Agent::with_provider(
        config,
        Box::new(UsageProvider {
            calls: AtomicUsize::new(0),
        }),
    );
    assert!(agent.usage_records().is_empty());
}

// ============================== auto-title ==================================

struct FakeTitler {
    response: String,
}
impl SessionTitler for FakeTitler {
    fn title(&self, _preview: &str) -> supercode_harness::Result<String> {
        Ok(self.response.clone())
    }
    fn model_id(&self) -> &str {
        "fake-small-model"
    }
}

#[tokio::test]
async fn agent_auto_title_uses_installed_titler_and_persists_via_session_store() {
    let config = Config::builder().auto_title(true).build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(AlwaysFinalProvider {
            calls: AtomicUsize::new(0),
        }),
    );
    agent.send("please fix the login bug").await.unwrap();
    agent.set_session_titler(FakeTitler {
        response: "Fix the login bug".to_string(),
    });
    let title = agent.auto_title().unwrap();
    assert_eq!(title, "Fix the login bug");

    let dir = temp_dir("auto-title-roundtrip");
    let store = SessionStore::open(&dir).unwrap();
    store.save("sess1", &title, "{}").unwrap();
    let info = store
        .list()
        .into_iter()
        .find(|i| i.name == "sess1")
        .unwrap();
    assert_eq!(info.title, title, "title must round-trip through the store");

    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn agent_auto_title_is_none_when_no_titler_installed() {
    let config = Config::builder().auto_title(true).build();
    let mut agent = Agent::with_provider(
        config,
        Box::new(AlwaysFinalProvider {
            calls: AtomicUsize::new(0),
        }),
    );
    agent.send("hello").await.unwrap();
    assert!(agent.auto_title().is_none());
}