supercode-harness 0.4.3

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
//! §2 "The Bolt-on Capability Taxonomy" (`docs/composable-harness/
//! COMPOSABLE-HARNESS-DESIGN.md`) — P3 of the composable-harness migration
//! (design §5.2, phase **P3**: "A `ModuleId` enum (the 35 names) + resolved
//! activation set on `Config`").
//!
//! [`ModuleId`] names all 35 §2 modules exactly as the table numbers them
//! (1-35). [`ModuleActivation`] is the resolved activation set — "module
//! activation is *pure config → set*, testable without the loop" (§5.3 risk
//! 2's mitigation): [`ModuleActivation::from_harness`] computes it from a
//! [`crate::configfile::HarnessConfig`] alone, no [`crate::Agent`] required.
//!
//! **Schema-collapse note.** Two of the 35 §2 rows have no *independent*
//! `[capabilities.<name>]` table of their own in the §3.1 schema — they are
//! represented as a field of a SIBLING module's table instead:
//! - module 10 `permissions.approvals` is `[capabilities.permissions]`'s own
//!   `enabled` flag (the `approval = "…"` mode lives in the same table as
//!   modules 11-13's parent, not a nested `approvals` sub-table) —
//!   [`ModuleId::PermissionsApprovals`] reads `capabilities.permissions.enabled`.
//! - module 16 `mcp.server` is `[capabilities.mcp].serve` (a bool field, not
//!   a nested table with its own `enabled`) — [`ModuleId::McpServer`] reads
//!   that field directly.
//!
//! Every other module maps onto exactly the [`crate::configfile::MODULE_NAMES`]
//! / [`crate::configfile::NESTED_MODULE_NAMES`] key P2 already resolves.

use std::collections::BTreeSet;

use crate::configfile::{module_enabled, module_setting_bool, HarnessConfig};

/// The 35 §2 capability modules, numbered exactly as the design's module
/// table (§2, rows 1-35).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ModuleId {
    /// 1. `tools.search` — glob/content-search/dir-listing tools.
    ToolsSearch,
    /// 2. `tools.apply_patch` — the apply_patch envelope tool.
    ToolsApplyPatch,
    /// 3. `tools.persistent_shell` — persistent/interactive shell.
    ToolsPersistentShell,
    /// 4. `tools.background` — background exec + monitor/event feed.
    ToolsBackground,
    /// 5. `tools.web` — web fetch + web search.
    ToolsWeb,
    /// 6. `tools.question` — structured user-question tool.
    ToolsQuestion,
    /// 7. `todos` — plan/task-checklist tool (`update_plan`).
    Todos,
    /// 8. `plan_mode` — plan enter/exit restriction mode.
    PlanMode,
    /// 9. `subagents` — spawn/named/background sub-agents.
    Subagents,
    /// 10. `permissions.approvals` — approval modes, interactive ask, caching.
    PermissionsApprovals,
    /// 11. `permissions.rules` — allow/ask/deny rule language.
    PermissionsRules,
    /// 12. `permissions.sandbox` — OS-level fs/network sandbox.
    PermissionsSandbox,
    /// 13. `permissions.protected_paths` — never-auto-approved paths.
    PermissionsProtectedPaths,
    /// 14. `trust` — project/workspace trust gate.
    Trust,
    /// 15. `mcp.client` — MCP stdio/remote/OAuth/resources/prompts.
    McpClient,
    /// 16. `mcp.server` — harness-as-MCP-server.
    McpServer,
    /// 17. `hooks` — config-registered lifecycle hooks.
    Hooks,
    /// 18. `plugins` — in-process extension API.
    Plugins,
    /// 19. `memory` — auto memory (agent-maintained, cross-session).
    Memory,
    /// 20. `checkpoint` — file checkpointing / shadow-git.
    Checkpoint,
    /// 21. `session.tree` — in-place tree, rewind, branch summaries.
    SessionTree,
    /// 22. `session.share` — public session-sharing links.
    SessionShare,
    /// 23. `reduction` — genuinely-optional lossless reduction policies.
    Reduction,
    /// 24. `deferred_tools` — deferred tool advertising + tool_search.
    DeferredTools,
    /// 25. `cache` — cache-aware architecture (plans, warnings, TTL).
    Cache,
    /// 26. `model.catalog` — aliases, fallback chains, small/utility model.
    ModelCatalog,
    /// 27. `model.oauth` — subscription OAuth login.
    ModelOauth,
    /// 28. `lsp` — LSP diagnostics in the edit path + query tool.
    Lsp,
    /// 29. `formatters` — format-on-write.
    Formatters,
    /// 30. `tui` — full-screen TUI.
    Tui,
    /// 31. `server` — full programmatic RPC/HTTP server.
    Server,
    /// 32. `notify` — external notify program / desktop / email.
    Notify,
    /// 33. `structured_output` — structured final output.
    StructuredOutput,
    /// 34. `telemetry` — OTel/analytics exporters.
    Telemetry,
    /// 35. `integrations` — cloud, IDE/ACP, CI bots, web UI, worktrees.
    Integrations,
}

impl ModuleId {
    /// Every module, in §2 table order (1-35).
    pub const ALL: &'static [ModuleId] = &[
        ModuleId::ToolsSearch,
        ModuleId::ToolsApplyPatch,
        ModuleId::ToolsPersistentShell,
        ModuleId::ToolsBackground,
        ModuleId::ToolsWeb,
        ModuleId::ToolsQuestion,
        ModuleId::Todos,
        ModuleId::PlanMode,
        ModuleId::Subagents,
        ModuleId::PermissionsApprovals,
        ModuleId::PermissionsRules,
        ModuleId::PermissionsSandbox,
        ModuleId::PermissionsProtectedPaths,
        ModuleId::Trust,
        ModuleId::McpClient,
        ModuleId::McpServer,
        ModuleId::Hooks,
        ModuleId::Plugins,
        ModuleId::Memory,
        ModuleId::Checkpoint,
        ModuleId::SessionTree,
        ModuleId::SessionShare,
        ModuleId::Reduction,
        ModuleId::DeferredTools,
        ModuleId::Cache,
        ModuleId::ModelCatalog,
        ModuleId::ModelOauth,
        ModuleId::Lsp,
        ModuleId::Formatters,
        ModuleId::Tui,
        ModuleId::Server,
        ModuleId::Notify,
        ModuleId::StructuredOutput,
        ModuleId::Telemetry,
        ModuleId::Integrations,
    ];

    /// The §3.1 config key this module reads (the same strings
    /// [`crate::configfile::MODULE_NAMES`]/[`crate::configfile::NESTED_MODULE_NAMES`]
    /// use), for diagnostics. The two schema-collapsed modules (see the
    /// module doc comment) report their HOST table's name, since they have
    /// no independent table of their own.
    pub fn config_key(&self) -> &'static str {
        match self {
            ModuleId::ToolsSearch => "tools_search",
            ModuleId::ToolsApplyPatch => "tools_apply_patch",
            ModuleId::ToolsPersistentShell => "tools_persistent_shell",
            ModuleId::ToolsBackground => "tools_background",
            ModuleId::ToolsWeb => "tools_web",
            ModuleId::ToolsQuestion => "tools_question",
            ModuleId::Todos => "todos",
            ModuleId::PlanMode => "plan_mode",
            ModuleId::Subagents => "subagents",
            ModuleId::PermissionsApprovals => "permissions",
            ModuleId::PermissionsRules => "permissions.rules",
            ModuleId::PermissionsSandbox => "permissions.sandbox",
            ModuleId::PermissionsProtectedPaths => "permissions.protected_paths",
            ModuleId::Trust => "trust",
            ModuleId::McpClient => "mcp",
            ModuleId::McpServer => "mcp.serve",
            ModuleId::Hooks => "hooks",
            ModuleId::Plugins => "plugins",
            ModuleId::Memory => "memory",
            ModuleId::Checkpoint => "checkpoint",
            ModuleId::SessionTree => "session_tree",
            ModuleId::SessionShare => "session_share",
            ModuleId::Reduction => "reduction",
            ModuleId::DeferredTools => "deferred_tools",
            ModuleId::Cache => "cache",
            ModuleId::ModelCatalog => "model_catalog",
            ModuleId::ModelOauth => "model_oauth",
            ModuleId::Lsp => "lsp",
            ModuleId::Formatters => "formatters",
            ModuleId::Tui => "tui",
            ModuleId::Server => "server",
            ModuleId::Notify => "notify",
            ModuleId::StructuredOutput => "structured_output",
            ModuleId::Telemetry => "telemetry",
            ModuleId::Integrations => "integrations",
        }
    }

    /// Whether this module is active in a resolved `HarnessConfig` — the
    /// same [`module_enabled`]/`module_setting_bool` logic the P2 resolver
    /// already computes (§3.5 step 7's "module-activation set"), reused
    /// verbatim rather than re-derived, so [`ModuleActivation`] can never
    /// diverge from [`crate::configfile::Resolved::modules`].
    pub fn is_active(&self, hc: &HarnessConfig) -> bool {
        match self {
            ModuleId::McpServer => module_setting_bool(hc, "mcp", "serve"),
            other => module_enabled(hc, other.config_key()),
        }
    }
}

impl std::fmt::Display for ModuleId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.config_key())
    }
}

/// `[capabilities.tools_search]`'s three per-tool sub-flags (§3.1 module 1:
/// `{ enabled, glob, content_search, list_dir }`), each defaulting to `true`
/// per the schema when the module itself is active and the flag is unset.
fn tools_search_subflag(hc: &HarnessConfig, key: &str) -> bool {
    hc.capabilities
        .get("tools_search")
        .and_then(|c| c.settings.get(key))
        .and_then(|v| v.as_bool())
        .unwrap_or(true)
}

/// P4c: `[capabilities.tools_web]`'s two per-tool sub-flags (§3.1 module 5:
/// `{ enabled, fetch, search }`), each defaulting to `true` per the schema
/// when the module itself is active and the flag is unset — same shape as
/// [`tools_search_subflag`].
fn tools_web_subflag(hc: &HarnessConfig, key: &str) -> bool {
    hc.capabilities
        .get("tools_web")
        .and_then(|c| c.settings.get(key))
        .and_then(|v| v.as_bool())
        .unwrap_or(true)
}

/// The resolved activation set for all 35 modules (§5.2 P3: "a resolved
/// activation set on `Config`") — pure `HarnessConfig` → set, no loop
/// required (§5.3 risk 2's testability mitigation). Also carries
/// `tools_search`'s three per-tool sub-flags, since [`crate::tools::ToolRegistry::from_config`]
/// needs them to decide which of `glob`/`search`/`list_dir` to register —
/// and (P4c) `tools_web`'s two, for `web_fetch`/`web_search`.
#[derive(Debug, Clone, Default)]
pub struct ModuleActivation {
    active: BTreeSet<ModuleId>,
    /// `[capabilities.tools_search].glob` (default `true`).
    pub tools_search_glob: bool,
    /// `[capabilities.tools_search].content_search` (default `true`).
    pub tools_search_content_search: bool,
    /// `[capabilities.tools_search].list_dir` (default `true`).
    pub tools_search_list_dir: bool,
    /// P4c: `[capabilities.tools_web].fetch` (default `true`).
    pub tools_web_fetch: bool,
    /// P4c: `[capabilities.tools_web].search` (default `true`).
    pub tools_web_search: bool,
}

impl ModuleActivation {
    /// Compute the activation set from a resolved `HarnessConfig` (the
    /// output of [`crate::configfile::resolve`]'s folding, before `Config`
    /// materialization).
    pub fn from_harness(hc: &HarnessConfig) -> Self {
        let mut active = BTreeSet::new();
        for &m in ModuleId::ALL {
            if m.is_active(hc) {
                active.insert(m);
            }
        }
        ModuleActivation {
            active,
            tools_search_glob: tools_search_subflag(hc, "glob"),
            tools_search_content_search: tools_search_subflag(hc, "content_search"),
            tools_search_list_dir: tools_search_subflag(hc, "list_dir"),
            tools_web_fetch: tools_web_subflag(hc, "fetch"),
            tools_web_search: tools_web_subflag(hc, "search"),
        }
    }

    /// Whether `id` is active.
    pub fn is_active(&self, id: ModuleId) -> bool {
        self.active.contains(&id)
    }

    /// Every active module, in [`ModuleId::ALL`] order.
    pub fn iter(&self) -> impl Iterator<Item = &ModuleId> {
        self.active.iter()
    }

    /// Count of active modules.
    pub fn len(&self) -> usize {
        self.active.len()
    }

    /// Whether no module is active at all.
    pub fn is_empty(&self) -> bool {
        self.active.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::configfile::HarnessConfig;

    #[test]
    fn all_has_exactly_35_modules_matching_the_design_table() {
        assert_eq!(ModuleId::ALL.len(), 35);
        // No duplicates.
        let set: BTreeSet<ModuleId> = ModuleId::ALL.iter().copied().collect();
        assert_eq!(set.len(), 35);
    }

    #[test]
    fn default_harness_config_activates_nothing() {
        let hc = HarnessConfig::default();
        let act = ModuleActivation::from_harness(&hc);
        assert!(act.is_empty(), "expected no modules active by default");
        for &m in ModuleId::ALL {
            assert!(!act.is_active(m), "{m} unexpectedly active by default");
        }
    }

    #[test]
    fn mcp_server_reads_the_serve_bool_not_a_nested_table() {
        let hc = HarnessConfig::from_toml_str(
            r#"
[capabilities.mcp]
enabled = true
serve = true
"#,
        )
        .expect("parses");
        let act = ModuleActivation::from_harness(&hc);
        assert!(act.is_active(ModuleId::McpClient));
        assert!(act.is_active(ModuleId::McpServer));
    }

    #[test]
    fn permissions_approvals_reads_the_parent_tables_own_enabled() {
        let hc = HarnessConfig::from_toml_str(
            r#"
[capabilities.permissions]
enabled = true
approval = "untrusted"
"#,
        )
        .expect("parses");
        let act = ModuleActivation::from_harness(&hc);
        assert!(act.is_active(ModuleId::PermissionsApprovals));
        // The nested sub-modules stay off — a separate `enabled` each.
        assert!(!act.is_active(ModuleId::PermissionsRules));
        assert!(!act.is_active(ModuleId::PermissionsSandbox));
    }

    #[test]
    fn tools_search_subflags_default_true_when_module_active() {
        let hc = HarnessConfig::from_toml_str(
            r#"
[capabilities.tools_search]
enabled = true
"#,
        )
        .expect("parses");
        let act = ModuleActivation::from_harness(&hc);
        assert!(act.is_active(ModuleId::ToolsSearch));
        assert!(act.tools_search_glob);
        assert!(act.tools_search_content_search);
        assert!(act.tools_search_list_dir);
    }

    #[test]
    fn tools_search_subflags_honor_explicit_false() {
        let hc = HarnessConfig::from_toml_str(
            r#"
[capabilities.tools_search]
enabled = true
list_dir = false
"#,
        )
        .expect("parses");
        let act = ModuleActivation::from_harness(&hc);
        assert!(act.tools_search_glob);
        assert!(!act.tools_search_list_dir);
    }

    /// P4c: `tools_web`'s `fetch`/`search` sub-flags default `true` when the
    /// module is active — same shape as `tools_search`'s three.
    #[test]
    fn tools_web_subflags_default_true_when_module_active() {
        let hc = HarnessConfig::from_toml_str(
            r#"
[capabilities.tools_web]
enabled = true
"#,
        )
        .expect("parses");
        let act = ModuleActivation::from_harness(&hc);
        assert!(act.is_active(ModuleId::ToolsWeb));
        assert!(act.tools_web_fetch);
        assert!(act.tools_web_search);
    }

    /// P4c: `tools_web`'s sub-flags honor an explicit `false`.
    #[test]
    fn tools_web_subflags_honor_explicit_false() {
        let hc = HarnessConfig::from_toml_str(
            r#"
[capabilities.tools_web]
enabled = true
search = false
"#,
        )
        .expect("parses");
        let act = ModuleActivation::from_harness(&hc);
        assert!(act.tools_web_fetch);
        assert!(!act.tools_web_search);
    }

    /// Default-off: `tools_web` inactive by default (no `[capabilities.tools_web]`
    /// table at all) leaves both sub-flags at their struct default (`false`)
    /// — never consulted anyway since `ToolRegistry::from_config` gates on
    /// `act.is_active(ModuleId::ToolsWeb)` first.
    #[test]
    fn tools_web_inactive_by_default() {
        let hc = HarnessConfig::default();
        let act = ModuleActivation::from_harness(&hc);
        assert!(!act.is_active(ModuleId::ToolsWeb));
    }
}