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
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
//! §5.2 "P3 — Module registry wired into the loop and `ToolRegistry`"
//! (`docs/composable-harness/COMPOSABLE-HARNESS-DESIGN.md`) — the P3 test
//! suite the design's own AC calls for:
//!
//! - a per-preset EXACT activation-set test (all 35 [`ModuleId`]s, not a
//!   sampled on/off subset — `composable_presets.rs` already covers the
//!   sampled form for §4.6's warning verdicts; this file is the exhaustive
//!   companion the P3 mandate specifically asks for);
//! - "disabled ⇒ contributes nothing", parameterized over every module with
//!   a tool contribution (§5.3 risk 5's shape);
//! - `todos`-off/`update_plan` and `skills`/`read_file`/`bash` cases (D-7);
//! - the mandatory risk-2 mitigation: `module_registry = false` (default)
//!   makes [`ToolRegistry::from_config`] byte-identical to
//!   [`ToolRegistry::with_builtins`];
//! - C1 warning cases (`tools_apply_patch` co-advertised with
//!   `edit_file`/`write_file` without per-model bits);
//! - SECURITY carry-forward case-sensitivity rejection tests for
//!   `[capabilities.permissions.*]`.

use std::collections::BTreeSet;

use supercode_harness::configfile::{resolve, HarnessConfig, ResolveOptions};
use supercode_harness::{Config, ModuleActivation, ModuleId, ToolRegistry};

fn resolve_preset(name: &str) -> supercode_harness::configfile::Resolved {
    let top = format!("extends = \"{name}\"\n");
    resolve(&top, None, &ResolveOptions::default())
        .unwrap_or_else(|e| panic!("preset `{name}` failed to resolve: {e}"))
}

/// Assert the FULL 35-module activation set matches `expected` exactly — not
/// a sampled on/off subset. A module present in neither list is asserted
/// OFF (§3.1: every module defaults to `enabled = false`).
fn assert_exact_activation(hc: &HarnessConfig, expected: &[ModuleId]) {
    let act = ModuleActivation::from_harness(hc);
    let expected_set: BTreeSet<ModuleId> = expected.iter().copied().collect();
    for &m in ModuleId::ALL {
        let want = expected_set.contains(&m);
        let got = act.is_active(m);
        assert_eq!(
            got, want,
            "module `{m}`: expected active={want}, got active={got}"
        );
    }
    assert_eq!(
        act.len(),
        expected_set.len(),
        "activation set size mismatch (dedup check)"
    );
}

// ---------------------------------------------------------------------------
// Per-preset EXACT activation sets — transcribed directly from the compiled
// preset TOML in `crates/harness/src/presets.rs` (the actual resolver input,
// not the prose "on/off at a glance" tables, which only list load-bearing
// subsets per design §4's own convention).
// ---------------------------------------------------------------------------

#[test]
fn pi_core_exact_activation_set() {
    let r = resolve_preset("pi-core");
    assert_exact_activation(
        &r.harness,
        &[
            ModuleId::Trust,
            ModuleId::SessionTree,
            ModuleId::SessionShare,
            ModuleId::Server,
            ModuleId::Plugins,
            ModuleId::Tui,
        ],
    );
}

#[test]
fn cc_parity_exact_activation_set() {
    let r = resolve_preset("cc-parity");
    assert_exact_activation(
        &r.harness,
        &[
            ModuleId::ToolsSearch,
            ModuleId::ToolsWeb,
            ModuleId::ToolsQuestion,
            ModuleId::Todos,
            ModuleId::PlanMode,
            ModuleId::Subagents,
            ModuleId::ToolsBackground,
            ModuleId::PermissionsApprovals,
            ModuleId::PermissionsRules,
            ModuleId::PermissionsProtectedPaths,
            ModuleId::Trust,
            ModuleId::McpClient,
            ModuleId::DeferredTools,
            ModuleId::Hooks,
            ModuleId::Memory,
            ModuleId::Checkpoint,
            ModuleId::SessionTree,
            ModuleId::ModelCatalog,
            ModuleId::Tui,
        ],
    );
}

#[test]
fn cx_parity_exact_activation_set() {
    let r = resolve_preset("cx-parity");
    assert_exact_activation(
        &r.harness,
        &[
            ModuleId::ToolsPersistentShell,
            ModuleId::ToolsApplyPatch,
            ModuleId::Todos,
            ModuleId::ToolsWeb,
            ModuleId::ToolsBackground,
            ModuleId::Subagents,
            ModuleId::DeferredTools,
            ModuleId::StructuredOutput,
            ModuleId::PermissionsApprovals,
            ModuleId::PermissionsRules,
            ModuleId::PermissionsProtectedPaths,
            ModuleId::Trust,
            ModuleId::McpClient,
            ModuleId::McpServer,
            ModuleId::Hooks,
            ModuleId::ModelCatalog,
            ModuleId::Tui,
        ],
    );
}

#[test]
fn oc_parity_exact_activation_set() {
    let r = resolve_preset("oc-parity");
    assert_exact_activation(
        &r.harness,
        &[
            ModuleId::ToolsSearch,
            ModuleId::Todos,
            ModuleId::ToolsWeb,
            ModuleId::Subagents,
            ModuleId::ToolsApplyPatch,
            ModuleId::PermissionsApprovals,
            ModuleId::PermissionsRules,
            ModuleId::Trust,
            ModuleId::McpClient,
            ModuleId::Plugins,
            ModuleId::Lsp,
            ModuleId::Formatters,
            ModuleId::Checkpoint,
            ModuleId::SessionShare,
            ModuleId::Server,
            ModuleId::ModelCatalog,
            ModuleId::Tui,
        ],
    );
}

#[test]
fn token_saver_exact_activation_set() {
    let r = resolve_preset("token-saver");
    // Inherits pi-core's set (extends = "pi-core") plus its own four
    // overrides — nothing pi-core turns off is touched.
    assert_exact_activation(
        &r.harness,
        &[
            ModuleId::Trust,
            ModuleId::SessionTree,
            ModuleId::SessionShare,
            ModuleId::Server,
            ModuleId::Plugins,
            ModuleId::Tui,
            ModuleId::Reduction,
            ModuleId::DeferredTools,
            ModuleId::Cache,
            ModuleId::ModelCatalog,
        ],
    );
}

#[test]
fn supercode_default_exact_activation_set() {
    let r = resolve_preset("supercode-default");
    // pi-core's set MINUS {trust, session_tree, session_share, server,
    // plugins} PLUS the six with_builtins() extras (tools_search,
    // tools_apply_patch, tools_persistent_shell, todos) + notify — the
    // literal "no config file" default (design §4 intro, S10 fix).
    assert_exact_activation(
        &r.harness,
        &[
            ModuleId::Tui,
            ModuleId::ToolsSearch,
            ModuleId::ToolsApplyPatch,
            ModuleId::ToolsPersistentShell,
            ModuleId::Todos,
            ModuleId::Notify,
        ],
    );
}

// ---------------------------------------------------------------------------
// Mandatory risk-2 mitigation (§5.3 risk 2): `module_registry = false`
// (default) ⇒ `ToolRegistry::from_config` is byte-identical to
// `ToolRegistry::with_builtins()` — same names, same order.
// ---------------------------------------------------------------------------

fn tool_names(r: &ToolRegistry) -> Vec<String> {
    r.iter().map(|t| t.name().to_string()).collect()
}

#[test]
fn module_registry_off_by_default_matches_with_builtins_exactly() {
    let config = Config::default();
    assert!(!config.module_registry, "module_registry must default off");
    let from_config = ToolRegistry::from_config(&config);
    let builtins = ToolRegistry::with_builtins();
    assert_eq!(tool_names(&from_config), tool_names(&builtins));
}

#[test]
fn module_registry_off_matches_with_builtins_even_with_a_nonempty_activation_set() {
    // Even if a caller populated `module_activation`/`core_tools_enabled`
    // without flipping the flag, the flag alone gates the new path — the
    // risk-2 mitigation is "flag off ⇒ identical", full stop.
    let r = resolve_preset("cc-parity");
    let mut config = r.config;
    assert!(!config.module_registry);
    config.module_activation = ModuleActivation::from_harness(&r.harness);
    let from_config = ToolRegistry::from_config(&config);
    let builtins = ToolRegistry::with_builtins();
    assert_eq!(tool_names(&from_config), tool_names(&builtins));
}

/// The resolver's own [`HarnessConfig::from_toml_str`] + `[experimental]
/// module_registry = true` end-to-end: `supercode-default` resolved with the
/// flag ON must still produce the SAME tool set as `with_builtins()` — it's
/// defined to reproduce today's unfiltered default stack (design §4 intro),
/// so flipping the flag on for exactly this preset must not observably
/// change the registry.
#[test]
fn supercode_default_with_module_registry_on_still_matches_with_builtins() {
    let top = "extends = \"supercode-default\"\n[experimental]\nmodule_registry = true\n";
    let r = resolve(top, None, &ResolveOptions::default()).expect("resolves");
    assert!(r.config.module_registry);
    let from_config = ToolRegistry::from_config(&r.config);
    let builtins = ToolRegistry::with_builtins();
    assert_eq!(tool_names(&from_config), tool_names(&builtins));
}

// ---------------------------------------------------------------------------
// "Disabled ⇒ contributes nothing" — parameterized over every module with a
// tool contribution (§5.3 risk 5's shape: "disabled ⇒ contributes nothing"
// is one parameterized test).
// ---------------------------------------------------------------------------

struct ToolModuleCase {
    module_toml: &'static str,
    tool_name: &'static str,
}

const TOOL_MODULE_CASES: &[ToolModuleCase] = &[
    ToolModuleCase {
        module_toml: "[capabilities.todos]\nenabled = true\n",
        tool_name: "update_plan",
    },
    ToolModuleCase {
        module_toml: "[capabilities.tools_apply_patch]\nenabled = true\n",
        tool_name: "apply_patch",
    },
    ToolModuleCase {
        module_toml: "[capabilities.tools_persistent_shell]\nenabled = true\n",
        tool_name: "shell",
    },
];

fn resolve_module_registry_on(extra: &str) -> supercode_harness::Config {
    let top = format!("[experimental]\nmodule_registry = true\n{extra}");
    resolve(&top, None, &ResolveOptions::default())
        .expect("resolves")
        .config
}

#[test]
fn disabled_module_contributes_no_tool_enabled_module_does() {
    for case in TOOL_MODULE_CASES {
        // Module OFF (nothing set beyond the flag): tool absent.
        let off_config = resolve_module_registry_on("");
        let off_registry = ToolRegistry::from_config(&off_config);
        assert!(
            off_registry.get(case.tool_name).is_none(),
            "`{}` must be ABSENT from the registry when its module is off",
            case.tool_name
        );

        // Module ON: tool present.
        let on_config = resolve_module_registry_on(case.module_toml);
        let on_registry = ToolRegistry::from_config(&on_config);
        assert!(
            on_registry.get(case.tool_name).is_some(),
            "`{}` must be PRESENT in the registry when its module is on",
            case.tool_name
        );
    }
}

#[test]
fn tools_search_off_means_none_of_list_dir_glob_search_are_registered() {
    let config = resolve_module_registry_on("");
    let registry = ToolRegistry::from_config(&config);
    for name in ["list_dir", "glob", "search"] {
        assert!(
            registry.get(name).is_none(),
            "`{name}` must be absent when tools_search is off"
        );
    }
}

#[test]
fn tools_search_on_registers_all_three_by_default() {
    let config = resolve_module_registry_on("[capabilities.tools_search]\nenabled = true\n");
    let registry = ToolRegistry::from_config(&config);
    for name in ["list_dir", "glob", "search"] {
        assert!(
            registry.get(name).is_some(),
            "`{name}` must be present when tools_search is on with default sub-flags"
        );
    }
}

#[test]
fn tools_search_sub_flags_narrow_individually() {
    let config = resolve_module_registry_on(
        "[capabilities.tools_search]\nenabled = true\nlist_dir = false\nglob = false\n",
    );
    let registry = ToolRegistry::from_config(&config);
    assert!(registry.get("list_dir").is_none());
    assert!(registry.get("glob").is_none());
    assert!(
        registry.get("search").is_some(),
        "content_search sub-flag stayed at its true default"
    );
}

#[test]
fn core_tools_enabled_list_gates_the_default_four() {
    // cx-parity's `core.tools.enabled = ["bash", "view_image"]` — no
    // read_file/write_file/edit_file at all (the C1 resolution: disabling
    // edit/write advertising).
    let config = resolve_module_registry_on("[core.tools]\nenabled = [\"bash\"]\n");
    let registry = ToolRegistry::from_config(&config);
    assert!(registry.get("bash").is_some());
    assert!(registry.get("read_file").is_none());
    assert!(registry.get("write_file").is_none());
    assert!(registry.get("edit_file").is_none());
}

// ---------------------------------------------------------------------------
// D-7 / skills prompt section: only appears when `core.skills.enabled` AND a
// read pathway (`read_file` or `bash`) is active — the design's own
// illustration of "a disabled module contributes no prompt sections".
// ---------------------------------------------------------------------------

fn system_prompt_for(top_toml: &str) -> String {
    let config = resolve(top_toml, None, &ResolveOptions::default())
        .expect("resolves")
        .config;
    let provider: Box<dyn supercode_harness::Provider> =
        Box::new(supercode_harness::OpenAiProvider::new(
            "http://127.0.0.1:0",
            "test-key",
            Default::default(),
        ));
    let agent = supercode_harness::Agent::with_provider(config, provider);
    agent.history()[0].content.clone().unwrap_or_default()
}

#[test]
fn skills_off_means_no_skills_section() {
    let top = "[experimental]\nmodule_registry = true\n[core.prompts]\nfoo = \"bar {args}\"\n";
    let prompt = system_prompt_for(top);
    assert!(
        !prompt.contains("# Skills"),
        "skills off must not add a section"
    );
}

#[test]
fn skills_on_with_read_file_active_adds_the_section() {
    let top = "[experimental]\nmodule_registry = true\n\
[core.skills]\nenabled = true\n\
[core.prompts]\nfoo = \"bar {args}\"\n";
    let prompt = system_prompt_for(top);
    assert!(prompt.contains("# Skills"), "expected a skills section");
    assert!(prompt.contains("foo"));
}

#[test]
fn skills_on_with_only_bash_active_still_adds_the_section_d7_warn_not_error() {
    let top = "[experimental]\nmodule_registry = true\n\
[core.tools]\nenabled = [\"bash\"]\n\
[core.skills]\nenabled = true\n\
[core.prompts]\nfoo = \"bar {args}\"\n";
    // D-7 (S3-amended): bash-only degrades to a resolver WARNING, not a hard
    // failure — the skills section still appears (cx-parity's own shape).
    let resolved =
        resolve(top, None, &ResolveOptions::default()).expect("resolves (warn, not error)");
    assert!(resolved.warnings.iter().any(|w| w.contains("D-7")));
    let provider: Box<dyn supercode_harness::Provider> =
        Box::new(supercode_harness::OpenAiProvider::new(
            "http://127.0.0.1:0",
            "test-key",
            Default::default(),
        ));
    let agent = supercode_harness::Agent::with_provider(resolved.config, provider);
    assert!(agent.history()[0]
        .content
        .as_deref()
        .unwrap_or("")
        .contains("# Skills"));
}

#[test]
fn skills_on_with_no_read_pathway_at_all_is_a_hard_resolve_error() {
    let top = "[experimental]\nmodule_registry = true\n\
[core.tools]\nenabled = []\n\
[core.skills]\nenabled = true\n";
    let err = resolve(top, None, &ResolveOptions::default())
        .expect_err("no read_file/bash at all must hard-error (D-7)");
    assert!(format!("{err}").contains("core.skills"));
}

#[test]
fn skills_gated_by_module_registry_flag_even_when_enabled_on_config() {
    // Flag OFF: even with `skills_enabled = true` set directly on a
    // hand-built `Config` (bypassing the resolver entirely), the assembly
    // site must not add the section — `module_registry` is the master gate
    // for ALL P3 behavior (§5.3 risk 2), checked before `skills_enabled` is
    // ever consulted.
    let config = supercode_harness::Config::builder()
        .skills_enabled(true)
        .core_tools_enabled(vec!["read_file".to_string()])
        .prompt("foo", "bar {args}")
        .build();
    assert!(!config.module_registry);
    let provider: Box<dyn supercode_harness::Provider> =
        Box::new(supercode_harness::OpenAiProvider::new(
            "http://127.0.0.1:0",
            "test-key",
            Default::default(),
        ));
    let agent = supercode_harness::Agent::with_provider(config, provider);
    assert!(
        !agent.history()[0]
            .content
            .as_deref()
            .unwrap_or("")
            .contains("# Skills"),
        "module_registry off must suppress the skills section regardless of skills_enabled"
    );
}

// ---------------------------------------------------------------------------
// C1 warning cases (§2.2 conflict 1): `tools_apply_patch` co-advertised with
// `edit_file`/`write_file` without per-model bits.
// ---------------------------------------------------------------------------

#[test]
fn c1_fires_when_apply_patch_and_edit_write_coexist_without_bits() {
    let top = "[core.tools]\nenabled = [\"read_file\", \"bash\", \"edit_file\", \"write_file\"]\n\
[capabilities.tools_apply_patch]\nenabled = true\n";
    let r = resolve(top, None, &ResolveOptions::default()).expect("resolves");
    assert!(
        r.warnings.iter().any(|w| w.contains("C1")),
        "expected a C1 warning: {:?}",
        r.warnings
    );
}

#[test]
fn c1_does_not_fire_with_per_model_bits_and_model_catalog_on() {
    let top = "[core.tools]\nenabled = [\"read_file\", \"bash\", \"edit_file\", \"write_file\"]\n\
[capabilities.tools_apply_patch]\nenabled = true\nper_model = true\n\
[capabilities.model_catalog]\nenabled = true\n";
    let r = resolve(top, None, &ResolveOptions::default()).expect("resolves");
    assert!(
        !r.warnings.iter().any(|w| w.contains("C1")),
        "expected no C1 warning with per_model bits + model_catalog on: {:?}",
        r.warnings
    );
}

#[test]
fn c1_does_not_fire_when_apply_patch_is_the_only_write_path() {
    // cx-parity's own shape: edit_file/write_file simply not enabled at all.
    let top = "[core.tools]\nenabled = [\"bash\"]\n\
[capabilities.tools_apply_patch]\nenabled = true\nper_model = true\n";
    let r = resolve(top, None, &ResolveOptions::default()).expect("resolves");
    assert!(!r.warnings.iter().any(|w| w.contains("C1")));
}

#[test]
fn supercode_default_carries_the_c1_warning_faithfully() {
    // §4.6's own verdict: supercode-default legitimately fires C1 (today's
    // actual with_builtins() posture has no per-model filtering at all).
    let r = resolve_preset("supercode-default");
    assert!(r.warnings.iter().any(|w| w.contains("C1")));
}

// ---------------------------------------------------------------------------
// SECURITY carry-forward: case-sensitive, deny-unknown-fields rejection for
// `[capabilities.permissions.*]` — a wrong-case key must never be silently
// honored.
// ---------------------------------------------------------------------------

#[test]
fn case_mismatched_sandbox_key_is_rejected_with_a_warning_not_honored() {
    let top = "[capabilities.permissions]\nenabled = true\nSandbox = \"danger_full_access\"\n";
    let r = resolve(top, None, &ResolveOptions::default()).expect("resolves (warns, not errors)");
    assert!(
        r.warnings.iter().any(|w| w.contains("SECURITY")),
        "expected a SECURITY case-sensitivity warning: {:?}",
        r.warnings
    );
    // Never honored: the lowercase-only reader never sees `Sandbox`, so the
    // resolved posture keeps the safe default (danger_full_access is
    // ALREADY the fail-safe default in this codebase's config model, so the
    // meaningful assertion is the resolver's `permissions.sandbox` MODULE
    // activation, which must NOT read the case-mismatched key as if it
    // named a real table).
    assert!(!supercode_harness::modules::ModuleId::PermissionsSandbox.is_active(&r.harness));
}

#[test]
fn case_mismatched_tier_key_inside_sandbox_table_is_rejected() {
    let top = "[capabilities.permissions]\nenabled = true\n\
[capabilities.permissions.sandbox]\nenabled = true\nTier = \"read_only\"\n";
    let r = resolve(top, None, &ResolveOptions::default()).expect("resolves (warns, not errors)");
    assert!(
        r.warnings.iter().any(|w| w.contains("SECURITY")),
        "expected a SECURITY case-sensitivity warning: {:?}",
        r.warnings
    );
}

#[test]
fn correctly_cased_permissions_keys_never_trip_the_security_warning() {
    let top = "[capabilities.permissions]\nenabled = true\napproval = \"untrusted\"\nsandbox = \"read_only\"\nauto_approved_tools = [\"read_file\"]\n\
[capabilities.permissions.rules]\nenabled = true\ndeny = []\nask = []\nallow = [\"*\"]\n\
[capabilities.permissions.protected_paths]\nenabled = true\npaths = [\".git/**\"]\n";
    let r = resolve(top, None, &ResolveOptions::default()).expect("resolves");
    assert!(
        !r.warnings.iter().any(|w| w.contains("SECURITY")),
        "correctly-cased keys must never trip the SECURITY warning: {:?}",
        r.warnings
    );
}

#[test]
fn every_reserved_preset_never_trips_the_security_warning() {
    // The six compiled-in presets are themselves the ground truth for
    // "correctly cased" — a regression here would mean a design-doc TOML
    // transcription typo slipped past P2's own golden tests.
    for name in supercode_harness::presets::RESERVED_PRESET_NAMES {
        let r = resolve_preset(name);
        assert!(
            !r.warnings.iter().any(|w| w.contains("SECURITY")),
            "preset `{name}` unexpectedly tripped the SECURITY warning: {:?}",
            r.warnings
        );
    }
}