lean-ctx 3.12.0

LeanCTX Engine: the open-source runtime for the Context Gateway for AI Systems. CLI, MCP server, and supported proxy integrations.
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
// SPDX-License-Identifier: Apache-2.0
//! The lean-ctx Claude Code mod (`integrations/claude-code-mod`): distribution
//! and lifecycle.
//!
//! The binary carries the mod and installs it from a local marketplace in the
//! lean-ctx data dir, so the installed mod always matches the engine that
//! serves it and nothing is fetched from the network. Claude Code caches a
//! plugin per version string, so the version is the engine version plus a
//! content hash (`3.10.5+1a2b3c4d`, semver build metadata): any change to the
//! mod's sources — a release or a `dev-install` — rolls it forward, an
//! unchanged mod never churns. The lifecycle is driven through Claude Code's
//! own `claude plugin` CLI — verified against 2.1.287: `marketplace add` and
//! `install` are idempotent, `marketplace update` + `update` move between
//! versions, including ones that differ only in build metadata.
//!
//! Setup and update install the mod where Claude Code can host it, say so in
//! their report (what it does, that it applies in new sessions, how to remove
//! it), and roll an installed mod forward. The mod is code that runs inside
//! Claude Code with the user's permissions, so removing it is a standing
//! decision: `lean-ctx claude-mod uninstall` leaves a marker that keeps every
//! later setup or update from installing it again, until an explicit
//! `lean-ctx claude-mod install` clears it.

use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};

/// First Claude Code release with mods (`hooks.json` `modules`).
pub const MIN_CLAUDE_VERSION: (u32, u32, u32) = (2, 1, 287);
/// Marketplace and plugin name; the plugin id is `lean-ctx@lean-ctx`.
pub const MARKETPLACE: &str = "lean-ctx";
pub const PLUGIN_ID: &str = "lean-ctx@lean-ctx";

pub const REGISTER_TS: &str = include_str!("../../templates/claude_mod/register.ts");
pub const HOOKS_JSON: &str = include_str!("../../templates/claude_mod/hooks.json");
/// The cockpit (sidebar, pulse line, overlays), imported by `register.ts`.
pub const COCKPIT_TSX: &str = include_str!("../../templates/claude_mod/cockpit.tsx");
/// ctx_patch / ctx_read / ctx_search results drawn as diffs and code,
/// imported by `register.ts`.
pub const RENDER_TSX: &str = include_str!("../../templates/claude_mod/render.tsx");
/// The `$.state` contract the manifest names as `types`.
pub const TYPES_DTS: &str = include_str!("../../templates/claude_mod/cockpit.d.ts");
/// Source manifest; [`plugin_manifest`] stamps the engine version into it.
pub const PLUGIN_JSON: &str = include_str!("../../templates/claude_mod/plugin.json");

const CLAUDE_TIMEOUT: Duration = Duration::from_mins(1);

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ModStatus {
    /// No usable `claude`: not on PATH, untrusted location, or it failed to
    /// run — the reason is kept so the user sees which (#2061).
    ClaudeMissing(String),
    /// Claude Code is older than [`MIN_CLAUDE_VERSION`].
    ClaudeTooOld(String),
    NotInstalled,
    /// Installed and matching this engine.
    Current(String),
    /// Installed from an older (or newer) engine; `install` refreshes it.
    Stale(String),
}

impl ModStatus {
    #[must_use]
    pub fn describe(&self) -> String {
        let want = mod_version();
        match self {
            Self::ClaudeMissing(reason) => format!("Claude Code not usable: {reason}"),
            Self::ClaudeTooOld(v) => format!(
                "Claude Code {v} predates mods (needs {}.{}.{}+)",
                MIN_CLAUDE_VERSION.0, MIN_CLAUDE_VERSION.1, MIN_CLAUDE_VERSION.2
            ),
            Self::NotInstalled => "not installed".to_string(),
            Self::Current(v) => format!("installed ({v})"),
            Self::Stale(v) => {
                format!("installed ({v}), this engine ships {want} — refresh pending")
            }
        }
    }
}

/// The installed plugin version: engine version + 8 hex of the mod sources'
/// BLAKE3 (`3.10.5+1a2b3c4d`). Deterministic per build.
#[must_use]
pub fn mod_version() -> String {
    let mut hasher = blake3::Hasher::new();
    for part in [
        REGISTER_TS,
        COCKPIT_TSX,
        RENDER_TSX,
        TYPES_DTS,
        HOOKS_JSON,
        PLUGIN_JSON,
    ] {
        hasher.update(part.as_bytes());
        hasher.update(&[0]);
    }
    let hash = hasher.finalize().to_hex();
    format!("{}+{}", env!("CARGO_PKG_VERSION"), &hash[..8])
}

/// `<data dir>/claude-mod` — the local marketplace root.
pub fn marketplace_dir() -> Result<PathBuf, String> {
    crate::core::data_dir::lean_ctx_data_dir().map(|d| d.join("claude-mod"))
}

/// Printed when setup or update installs the mod: what it is, when it applies,
/// how to remove it.
pub const INSTALLED_NOTICE: &str = "Installed the LeanCTX plugin for Claude Code: the cockpit (what Claude saw, \
tokens kept out, the two checks, receipts), wake on background jobs and a focused lean-ctx tool \
surface. Active in new Claude Code sessions (or /reload-plugins). Remove anytime with \
`lean-ctx claude-mod uninstall` — it then stays removed.";

/// `<data dir>/claude-mod.declined`: written by `uninstall`, outside the
/// marketplace root that uninstall deletes, so the decision outlives it.
fn declined_marker() -> Result<PathBuf, String> {
    crate::core::data_dir::lean_ctx_data_dir().map(|d| d.join("claude-mod.declined"))
}

/// Whether the user removed the mod: setup and update then never install it.
#[must_use]
pub fn auto_install_declined() -> bool {
    declined_marker().is_ok_and(|p| p.exists())
}

/// The plugin manifest with `version` set to [`mod_version`].
pub fn plugin_manifest() -> String {
    let mut manifest: serde_json::Value =
        serde_json::from_str(PLUGIN_JSON).expect("templates/claude_mod/plugin.json is valid JSON");
    manifest["version"] = serde_json::json!(mod_version());
    serde_json::to_string_pretty(&manifest).expect("manifest serializes") + "\n"
}

fn marketplace_manifest() -> String {
    let manifest = serde_json::json!({
        "name": MARKETPLACE,
        "description": "lean-ctx's Claude Code mod, installed by the lean-ctx binary that serves it.",
        "owner": { "name": "Thinkery / Yves Gugger" },
        "plugins": [{
            "name": "lean-ctx",
            "source": "./plugins/lean-ctx",
            "description": "The LeanCTX cockpit, wake on background job completion, a focused lean-ctx tool surface, and per-session request usage.",
        }]
    });
    serde_json::to_string_pretty(&manifest).expect("manifest serializes") + "\n"
}

/// Write the marketplace (manifest + plugin files) under `root`; only files
/// whose bytes differ are rewritten. Returns whether anything changed.
pub fn materialize(root: &Path) -> Result<bool, String> {
    let plugin = root.join("plugins").join("lean-ctx");
    let files: [(PathBuf, String); 7] = [
        (
            root.join(".claude-plugin/marketplace.json"),
            marketplace_manifest(),
        ),
        (plugin.join(".claude-plugin/plugin.json"), plugin_manifest()),
        (plugin.join("hooks/hooks.json"), HOOKS_JSON.to_string()),
        (plugin.join("hooks/register.ts"), REGISTER_TS.to_string()),
        (plugin.join("hooks/cockpit.tsx"), COCKPIT_TSX.to_string()),
        (plugin.join("hooks/render.tsx"), RENDER_TSX.to_string()),
        (plugin.join("types/index.d.ts"), TYPES_DTS.to_string()),
    ];
    let mut changed = false;
    for (path, content) in files {
        if std::fs::read_to_string(&path).is_ok_and(|on_disk| on_disk == content) {
            continue;
        }
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .map_err(|e| format!("create {}: {e}", parent.display()))?;
        }
        std::fs::write(&path, content).map_err(|e| format!("write {}: {e}", path.display()))?;
        changed = true;
    }
    Ok(changed)
}

/// Parse `claude --version` output (`2.1.287 (Claude Code)`).
#[must_use]
pub fn parse_claude_version(output: &str) -> Option<(u32, u32, u32)> {
    let token = output.split_whitespace().next()?;
    let mut parts = token.split('.').map(|p| p.parse::<u32>().ok());
    Some((parts.next()??, parts.next()??, parts.next()??))
}

/// Version of an installed `lean-ctx@lean-ctx` from `claude plugin list --json`.
#[must_use]
pub fn installed_version(list_json: &str) -> Option<String> {
    let entries: Vec<serde_json::Value> = serde_json::from_str(list_json).ok()?;
    entries
        .iter()
        .find(|e| e.get("id").and_then(serde_json::Value::as_str) == Some(PLUGIN_ID))
        .and_then(|e| e.get("version").and_then(serde_json::Value::as_str))
        .map(str::to_string)
}

/// Current lifecycle state. Runs `claude --version` and `claude plugin list`.
pub fn status() -> ModStatus {
    let version_out = match run_claude(&["--version"]) {
        Ok(out) => out,
        Err(reason) => return ModStatus::ClaudeMissing(reason),
    };
    let version = version_out
        .split_whitespace()
        .next()
        .unwrap_or("")
        .to_string();
    match parse_claude_version(&version_out) {
        Some(v) if v >= MIN_CLAUDE_VERSION => {}
        _ => return ModStatus::ClaudeTooOld(version),
    }
    match run_claude(&["plugin", "list", "--json"])
        .ok()
        .and_then(|out| installed_version(&out))
    {
        None => ModStatus::NotInstalled,
        Some(v) if v == mod_version() => ModStatus::Current(v),
        Some(v) => ModStatus::Stale(v),
    }
}

/// Install, or refresh an existing install to this engine's version.
/// Idempotent; returns a one-line human summary.
pub fn install() -> Result<String, String> {
    let before = status();
    match &before {
        ModStatus::ClaudeMissing(_) | ModStatus::ClaudeTooOld(_) => {
            return Err(before.describe());
        }
        ModStatus::Current(_) | ModStatus::NotInstalled | ModStatus::Stale(_) => {}
    }
    let root = marketplace_dir()?;
    materialize(&root)?;
    let root_str = root.to_string_lossy().to_string();
    // Idempotent: re-adding an existing local marketplace is a no-op success.
    run_claude(&["plugin", "marketplace", "add", &root_str])?;
    // An install (explicit, or by setup while nothing was declined) lifts an
    // earlier removal: from here on, updates keep the mod current again.
    if let Ok(marker) = declined_marker() {
        let _ = std::fs::remove_file(marker);
    }
    match before {
        ModStatus::NotInstalled => {
            run_claude(&["plugin", "install", PLUGIN_ID, "--scope", "user"])?;
            Ok(format!(
                "installed {PLUGIN_ID} {} — active in new Claude Code sessions (or /reload-plugins)",
                mod_version()
            ))
        }
        ModStatus::Stale(old) => {
            run_claude(&["plugin", "marketplace", "update", MARKETPLACE])?;
            run_claude(&["plugin", "update", PLUGIN_ID, "--scope", "user"])?;
            Ok(format!(
                "updated {PLUGIN_ID} {old} → {} — applies in new sessions",
                mod_version()
            ))
        }
        _ => Ok(format!("{PLUGIN_ID} is current ({})", mod_version())),
    }
}

/// Installed plugin versions read from Claude Code's plugin cache
/// (`<claude dir>/plugins/cache/lean-ctx/lean-ctx/<version>/`) — a file-only
/// probe for `doctor`, which must not spawn `claude`. Directory names are as
/// Claude Code writes them: it replaces the `+` of build metadata with `-`
/// (`3.10.5+5ed30c51` is cached as `3.10.5-5ed30c51`), see [`cache_dir_name`].
#[must_use]
pub fn cached_versions(claude_dir: &Path) -> Vec<String> {
    let mut versions: Vec<String> = std::fs::read_dir(
        claude_dir
            .join("plugins/cache")
            .join(MARKETPLACE)
            .join("lean-ctx"),
    )
    .map(|entries| {
        entries
            .filter_map(Result::ok)
            .filter(|e| e.path().is_dir())
            .filter_map(|e| e.file_name().to_str().map(str::to_string))
            .collect()
    })
    .unwrap_or_default();
    versions.sort();
    versions
}

/// The cache directory Claude Code (2.1.287) creates for a plugin version:
/// `+` is not kept in the path, so build metadata appears as `-…`.
#[must_use]
pub fn cache_dir_name(version: &str) -> String {
    version.replace('+', "-")
}

/// Remove the plugin, the marketplace, and the materialized files.
///
/// Success is verified, not assumed: the local files are only deleted once
/// Claude Code no longer lists the plugin — deleting them first would leave a
/// registered plugin pointing at a missing directory.
pub fn uninstall() -> Result<String, String> {
    if let missing @ ModStatus::ClaudeMissing(_) = status() {
        return Err(missing.describe());
    }
    let uninstall = run_claude(&["plugin", "uninstall", PLUGIN_ID, "--scope", "user"]);
    let remove = run_claude(&["plugin", "marketplace", "remove", MARKETPLACE]);
    let still_installed =
        run_claude(&["plugin", "list", "--json"]).map(|out| installed_version(&out).is_some())?;
    let marketplace_listed = run_claude(&["plugin", "marketplace", "list", "--json"])
        .map(|out| marketplace_listed(&out))?;
    if still_installed || marketplace_listed {
        let cause = uninstall.err().or(remove.err()).unwrap_or_default();
        return Err(format!(
            "{PLUGIN_ID} is still registered with Claude Code{}{cause} — local files kept",
            if cause.is_empty() { "" } else { ": " }
        ));
    }
    if let Ok(root) = marketplace_dir()
        && root.exists()
    {
        std::fs::remove_dir_all(&root).map_err(|e| format!("remove {}: {e}", root.display()))?;
    }
    // A removal is a standing decision: no later setup or update reinstalls it.
    if let Ok(marker) = declined_marker() {
        if let Some(parent) = marker.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        std::fs::write(&marker, "removed with `lean-ctx claude-mod uninstall`; `lean-ctx claude-mod install` lifts this\n")
            .map_err(|e| format!("write {}: {e}", marker.display()))?;
    }
    Ok(format!(
        "removed {PLUGIN_ID} — setup and update will not reinstall it (lean-ctx claude-mod install brings it back)"
    ))
}

/// Whether `claude plugin marketplace list --json` names our marketplace.
#[must_use]
pub fn marketplace_listed(list_json: &str) -> bool {
    serde_json::from_str::<Vec<serde_json::Value>>(list_json).is_ok_and(|entries| {
        entries
            .iter()
            .any(|e| e.get("name").and_then(serde_json::Value::as_str) == Some(MARKETPLACE))
    })
}

/// Run `claude <args>` with a hard timeout; stdout on success. stdout/stderr
/// are drained on their own threads (a full pipe would otherwise stall the
/// child until the timeout), and the child is killed and reaped on every
/// failure path so no zombie outlives the call.
fn run_claude(args: &[&str]) -> Result<String, String> {
    use std::io::Read as _;
    let label = || format!("claude {}", args.join(" "));
    let binary = crate::core::editor_registry::validate_claude_binary()?;
    let mut child = Command::new(&binary)
        .args(args)
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .map_err(|e| format!("{}: {e}", label()))?;
    let drain = |pipe: Option<Box<dyn std::io::Read + Send>>| {
        std::thread::spawn(move || {
            let mut buf = Vec::new();
            if let Some(mut p) = pipe {
                let _ = p.read_to_end(&mut buf);
            }
            String::from_utf8_lossy(&buf).into_owned()
        })
    };
    let stdout = drain(child.stdout.take().map(|p| Box::new(p) as Box<_>));
    let stderr = drain(child.stderr.take().map(|p| Box::new(p) as Box<_>));
    let start = Instant::now();
    let status = loop {
        match child.try_wait() {
            Ok(Some(status)) => break Ok(status),
            Ok(None) if start.elapsed() > CLAUDE_TIMEOUT => {
                break Err(format!("{} timed out", label()));
            }
            Ok(None) => std::thread::sleep(Duration::from_millis(50)),
            Err(e) => break Err(format!("{}: {e}", label())),
        }
    };
    let status = match status {
        Ok(status) => status,
        Err(e) => {
            let _ = child.kill();
            let _ = child.wait();
            return Err(e);
        }
    };
    let stdout = stdout.join().unwrap_or_default();
    let stderr = stderr.join().unwrap_or_default();
    if status.success() {
        Ok(stdout)
    } else {
        let detail = stderr
            .trim()
            .lines()
            .chain(stdout.trim().lines())
            .last()
            .unwrap_or("");
        Err(format!("{} failed: {detail}", label()))
    }
}

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

    /// Observed on a real install (2.1.287): Claude Code caches
    /// `3.10.5+5ed30c51` as `…/3.10.5-5ed30c51/` next to the older `3.10.5/`;
    /// doctor compared against the `+` form and reported a current mod stale.
    #[test]
    fn cache_dir_matches_the_installed_version() {
        let dir = tempfile::tempdir().unwrap();
        let cache = dir.path().join("plugins/cache/lean-ctx/lean-ctx");
        let current = cache_dir_name(&mod_version());
        for v in ["3.10.5", current.as_str()] {
            std::fs::create_dir_all(cache.join(v)).unwrap();
        }
        assert!(cached_versions(dir.path()).contains(&current));
        assert!(!current.contains('+'), "{current}");
    }

    #[test]
    fn version_gate_parses_claude_output() {
        assert_eq!(
            parse_claude_version("2.1.287 (Claude Code)"),
            Some((2, 1, 287))
        );
        assert!(parse_claude_version("2.1.286 (Claude Code)").unwrap() < MIN_CLAUDE_VERSION);
        assert!(parse_claude_version("2.2.0 (Claude Code)").unwrap() >= MIN_CLAUDE_VERSION);
        assert_eq!(parse_claude_version("garbage"), None);
    }

    /// Shape observed from `claude plugin list --json` (2.1.287).
    #[test]
    fn installed_version_reads_only_our_plugin() {
        let list = r#"[{"id":"other@x","version":"9.9.9"},
                       {"id":"lean-ctx@lean-ctx","version":"3.10.5","scope":"user"}]"#;
        assert_eq!(installed_version(list).as_deref(), Some("3.10.5"));
        assert_eq!(installed_version("[]"), None);
        assert_eq!(installed_version("not json"), None);
        // `claude plugin marketplace list --json` (2.1.287); uninstall only
        // deletes local files once this no longer names our marketplace.
        let markets = r#"[{"name":"lean-ctx","source":"directory","path":"/d"}]"#;
        assert!(marketplace_listed(markets));
        assert!(!marketplace_listed(r#"[{"name":"other"}]"#));
        assert!(!marketplace_listed("garbage"));
    }

    /// A removal is a standing decision; an explicit install lifts it. Checked
    /// through the marker path itself (the CLI round-trips need `claude`).
    #[test]
    fn removal_marker_lives_outside_the_marketplace_root() {
        let marker = declined_marker().unwrap();
        let root = marketplace_dir().unwrap();
        assert!(
            !marker.starts_with(&root),
            "{} inside {}",
            marker.display(),
            root.display()
        );
        assert_eq!(marker.parent(), root.parent());
    }

    /// The installed manifest must carry the engine version plus a content
    /// hash (Claude caches plugins by version string; a version that ignores
    /// the mod's sources froze dev-installs and same-version patches), and a
    /// second materialize must be a no-op.
    #[test]
    fn materialize_stamps_engine_version_and_is_idempotent() {
        let dir = tempfile::tempdir().unwrap();
        assert!(materialize(dir.path()).unwrap());
        let manifest: serde_json::Value = serde_json::from_str(
            &std::fs::read_to_string(
                dir.path()
                    .join("plugins/lean-ctx/.claude-plugin/plugin.json"),
            )
            .unwrap(),
        )
        .unwrap();
        let version = manifest["version"].as_str().unwrap();
        let (engine, hash) = version.split_once('+').expect("engine+hash version");
        assert_eq!(engine, env!("CARGO_PKG_VERSION"));
        assert!(
            hash.len() == 8 && hash.chars().all(|c| c.is_ascii_hexdigit()),
            "{version}"
        );
        assert_eq!(manifest["name"], "lean-ctx");
        assert!(
            std::fs::read_to_string(dir.path().join("plugins/lean-ctx/hooks/register.ts"))
                .unwrap()
                .contains("export const register")
        );
        // The cockpit and the `$.state` contract the manifest points at ship too.
        let plugin = dir.path().join("plugins/lean-ctx");
        assert_eq!(
            std::fs::read_to_string(plugin.join("hooks/cockpit.tsx")).unwrap(),
            COCKPIT_TSX
        );
        assert_eq!(manifest["types"], "./types/index.d.ts");
        assert_eq!(
            std::fs::read_to_string(plugin.join("types/index.d.ts")).unwrap(),
            TYPES_DTS
        );
        assert!(
            !materialize(dir.path()).unwrap(),
            "second run must not rewrite"
        );
    }

    /// The mod defers every lean-ctx tool outside `front_loaded_tools`, and
    /// `alwaysLoad` cannot undo that. A routed tool left out (ctx_patch,
    /// ctx_glob, ctx_tree in 3.11.4) sends the model back to native Edit/Glob,
    /// which replace mode denies. Both copies of the default must cover every
    /// core tool the injected rules route an intent to.
    #[test]
    fn front_loaded_defaults_cover_every_routed_core_tool() {
        use crate::core::rules_canonical::{BULLETS, INTENT};

        let manifest: serde_json::Value = serde_json::from_str(PLUGIN_JSON).unwrap();
        let manifest_default: Vec<&str> = manifest["userConfig"]["front_loaded_tools"]["default"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_str().unwrap())
            .collect();
        let (_, list) = REGISTER_TS
            .split_once("const DEFAULT_FRONT_LOADED_TOOLS = [")
            .expect("register.ts declares DEFAULT_FRONT_LOADED_TOOLS");
        let (list, _) = list.split_once("];").unwrap();
        let register_default: Vec<&str> = list
            .split(',')
            .map(|s| s.trim().trim_matches('"'))
            .filter(|s| !s.is_empty())
            .collect();
        assert_eq!(register_default, manifest_default);

        let routed = format!("{BULLETS}\n{INTENT}");
        for tool in crate::tool_defs::core_tool_names()
            .iter()
            .filter(|t| t.starts_with("ctx_"))
            .filter(|t| {
                routed
                    .split(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
                    .any(|word| word == **t)
            })
        {
            assert!(
                manifest_default.contains(tool),
                "{tool} is routed by the rules but deferred by the mod"
            );
        }
    }
}