trusty-mpm 1.8.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
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
//! Renders `references/framework.md` — how the harness itself is laid out
//! (issue #4946).
//!
//! Why: a session could answer "what commands exist?" from the other five
//! reference files but not "where does my skill actually deploy?" or "which
//! agent tier wins?" — those answers only existed in Rust source, so every
//! question ended in a hand investigation. The one place the compiled prompt
//! did state a tier precedence
//! (`assets/instructions/sections/core.md`) named `~/.trusty-mpm/agents/`, a
//! tier no code reads: hand-written prose about the framework's own mechanics
//! rots silently, and stale self-knowledge is worse than none. So this file is
//! generated from the same path constants and tier resolvers the runtime uses
//! — move a directory or reorder a tier and the committed skill drifts,
//! failing `scripts/check_capabilities.sh`.
//! What: [`render`] emits the install layout ([`FrameworkPaths`] rendered under
//! a literal `~` base), the managed `CLAUDE_CONFIG_DIR`, the agent tier order
//! ([`deployed_agent_dirs_from`]), the skill deploy tiers
//! ([`skill_deploy_tiers`]), the per-session state layout, an
//! existence-checked index of the authoritative docs (separating what ships in
//! the published crate from what is repo-only), and the commands that report
//! live state — each annotated with its real clap `about` string.
//! Test: `framework_render_lists_the_real_agent_tier_order`,
//! `framework_render_never_names_the_phantom_agent_tier`,
//! `framework_render_is_deterministic`.

use std::fmt::Write as _;
use std::path::{Path, PathBuf};

use clap::CommandFactory;

use crate::cli::Cli;
use trusty_mpm::core::delegation_authority::deployed_agent_dirs_from;
use trusty_mpm::core::harness_root::{HARNESS_DIR, SESSIONS_DIR};
use trusty_mpm::core::instruction_pipeline::COMPILED_PROMPT_FILE;
use trusty_mpm::core::paths::FrameworkPaths;
use trusty_mpm::core::skill_deploy_tiers::skill_deploy_tiers;
use trusty_mpm::core::trusty_tools_config::managed_claude_config_dir_at;

/// The home-directory stand-in every rendered path is resolved against.
///
/// Why: [`FrameworkPaths::default`] resolves against the real `dirs::home_dir()`,
/// which would bake `/Users/<someone>` into a committed file and fail the drift
/// gate on every other machine. `~` is both reproducible and the form an
/// operator recognises.
const HOME: &str = "~";

/// The project stand-in for tiers that are resolved per-project.
const PROJECT: &str = "<project>";

/// Docs that live OUTSIDE the published crate, checked for existence.
///
/// Why: these are the authoritative long-form references, and pointing at one
/// that has been moved or deleted is exactly the silent rot this file exists to
/// prevent. Each path is probed against the repo root at generation time, so a
/// rename flips its rendered row and fails the drift gate instead of shipping a
/// dead pointer.
/// What: `(repo-relative path, what it holds)`.
const REPO_DOCS: &[(&str, &str)] = &[
    (
        "docs/adr/INDEX.md",
        "Index of every architecture decision record — the ADR is the authority on why a mechanism is shaped the way it is.",
    ),
    (
        "docs/specs/README.md",
        "Index of the numbered DOC-* specs (behaviour contracts). A spec, not an ADR, is what a source file's `# Spec References` block links back to.",
    ),
    (
        "docs/reference/",
        "Operational reference: release workflow, worktree discipline, SLOC cap, environment variables, threat model.",
    ),
    (
        "docs/SUMMARY.md",
        "mdBook table of contents spanning every crate's docs.",
    ),
];

/// Commands that report the harness's LIVE state, rather than this catalog's
/// generated-at-build-time view.
///
/// Why: this file describes the layout a correctly-installed harness has. When
/// a session needs what its own machine actually resolved — which tiers exist,
/// what the prompt really says — it must run something, not read prose.
/// What: `(command path through the clap tree, why you would run it)`. The
/// `about` text is pulled from clap at render time, so a renamed command
/// changes the output.
const LIVE_STATE_COMMANDS: &[(&[&str], &str)] = &[
    (
        &["doctor"],
        "Whether each tier is actually populated on this machine, and which check failed.",
    ),
    (
        &["sessions", "instructions"],
        "The composed prompt this project's session would receive, verbatim — the ground truth when the prompt and this catalog disagree.",
    ),
    (
        &["install"],
        "Re-materialise the bundled agent/skill/hook artifacts into the layout above.",
    ),
];

/// Render the full framework-model reference.
///
/// Why: see the module doc — every path and tier below is computed, never
/// restated.
/// What: seven sections (layout, managed config dir, agent tiers, skill tiers,
/// per-session state, documentation index, live-state commands). The
/// documentation index is probed against the checkout holding `start`
/// ([`repo_root`], #9298).
/// Test: `framework_render_lists_the_real_agent_tier_order`,
/// `framework_render_probes_the_checkout_holding_start_9298`.
pub(crate) fn render(start: &Path) -> String {
    let home = Path::new(HOME);
    let paths = FrameworkPaths::under(home);
    let managed = managed_claude_config_dir_at(home);

    let mut out = String::new();
    out.push_str("# Framework Model — Layout, Tiers, and Where the Answers Live\n\n");
    out.push_str(
        "Generated by `tm generate capabilities` from the harness's own path \
         constants and tier resolvers (`core::paths::FrameworkPaths`, \
         `core::delegation_authority::deployed_agent_dirs_from`, \
         `core::skill_deploy_tiers::skill_deploy_tiers`, \
         `core::harness_root`). Nothing here is restated by hand: move a \
         directory or reorder a tier and this file drifts, failing \
         `scripts/check_capabilities.sh`. Paths are shown against `~` because \
         the real layout is home-relative.\n\n",
    );

    render_layout(&mut out, &paths, &managed);
    render_agent_tiers(&mut out, &paths, &managed);
    render_skill_tiers(&mut out, &paths);
    render_session_state(&mut out);
    render_docs(&mut out, &repo_root(start));
    render_live_state(&mut out);
    out
}

/// Install-layout table plus the `CLAUDE_CONFIG_DIR` explanation.
fn render_layout(out: &mut String, paths: &FrameworkPaths, managed: &Path) {
    out.push_str("## Install Layout\n\n");
    out.push_str("| Path | What lives there |\n|---|---|\n");
    let rows: &[(&PathBuf, &str)] = &[
        (&paths.root, "Framework install root."),
        (
            &paths.framework,
            "tm-owned, auto-materialised bundled tree. Never hand-edit — `tm install` and session prep overwrite it.",
        ),
        (
            &paths.agents,
            "Bundled agent SOURCE files (pre-composition).",
        ),
        (
            &paths.skills,
            "Bundled skill SOURCE files, re-materialised whenever the binary's embedded bundle fingerprint changes.",
        ),
        (
            &paths.user_skills,
            "USER-authored skill source. Sits outside `framework/` precisely because that tree is overwritten. Deployed into every session, outranking a same-named bundled skill.",
        ),
        (&paths.hooks, "Optimizer / overseer hook policy."),
        (
            &paths.instructions,
            "Framework instruction assets. The prompt a session actually ran with is NOT here — it is per-session (see below).",
        ),
        (&paths.registry, "Shared project registry."),
    ];
    for (path, what) in rows {
        let _ = writeln!(out, "| `{}` | {what} |", path.display());
    }
    let _ = writeln!(
        out,
        "| `{}` | The tm-managed `CLAUDE_CONFIG_DIR` (see below). |",
        managed.display()
    );
    let _ = writeln!(
        out,
        "| `{}` | The operator's OWN Claude Code install. tm reads it as the lowest agent tier and never writes framework-owned agents into it. |\n",
        paths.claude_home_dir().join(".claude").display()
    );

    out.push_str("## Why `CLAUDE_CONFIG_DIR` Is Relocated\n\n");
    let _ = writeln!(
        out,
        "Managed sessions launch `claude` with `CLAUDE_CONFIG_DIR` pointed at \
         `{}`, not the operator's `~/.claude`. Two things follow. Framework-owned \
         agents and skills land in a tm-controlled directory, so they cannot \
         contaminate — or be contaminated by — a Claude Code install the operator \
         maintains for their own use. And because the relocated directory is \
         global rather than per-workspace, one roster serves every session instead \
         of each project carrying a mutable copy that can silently shadow the real \
         agent.",
        managed.display()
    );
    out.push_str(
        "\nA session that has this variable set is reading the managed tier. A \
         session that does not is reading the operator's own.\n\n",
    );
}

/// Agent tier precedence, rendered from the real resolver.
fn render_agent_tiers(out: &mut String, paths: &FrameworkPaths, managed: &Path) {
    out.push_str("## Agent Tier Precedence\n\n");
    out.push_str(
        "Highest precedence first — on a name collision the earlier tier wins, \
         case-insensitively. Rendered from `deployed_agent_dirs_from`, the same \
         function the runtime resolves against.\n\n",
    );
    let dirs = deployed_agent_dirs_from(
        Path::new(PROJECT),
        Some(managed),
        &paths.claude_agents_dir(),
    );
    let notes = [
        "Hand-placed and project-custom agents only. tm does not deploy bundled agents here.",
        "Where every BUNDLED agent deploys. The `CLAUDE_CONFIG_DIR` env var overrides this path when set.",
        "The operator's own Claude Code agents. Read, never written by tm.",
    ];
    for (i, dir) in dirs.iter().enumerate() {
        let note = notes.get(i).copied().unwrap_or("");
        let _ = writeln!(out, "{}. `{}` — {note}", i + 1, dir.display());
    }
    out.push_str(
        "\nThere is no `~/.trusty-mpm/agents/` tier. No code reads that path; a \
         copy placed there is loaded by nothing.\n\n",
    );
}

/// Skill deploy tiers, rendered from the real enumerator.
fn render_skill_tiers(out: &mut String, paths: &FrameworkPaths) {
    out.push_str("## Skill Deploy Tiers\n\n");
    out.push_str(
        "Skills, unlike agents, have no single destination — a bundled skill is \
         deployed into each of these independently, so one can be stale while \
         another is current. Rendered from `skill_deploy_tiers`.\n\n",
    );
    out.push_str("| Tier | Directory |\n|---|---|\n");
    for tier in skill_deploy_tiers(paths, Some(Path::new(PROJECT))) {
        let _ = writeln!(out, "| {} | `{}` |", tier.label, tier.dir.display());
    }
    out.push_str(
        "\nSource precedence when the same skill name exists in more than one \
         source: project-custom, then user-custom (`~/.trusty-mpm/skills/`), then \
         bundled. A deployed skill you hand-edit is frozen — its checksum no \
         longer matches, so redeploy skips it rather than overwriting your edit. \
         A skill whose slug contains `mcp` is never deployed; it would shadow \
         Claude Code's built-in `/mcp`.\n\n",
    );
}

/// Per-session on-disk state.
fn render_session_state(out: &mut String) {
    out.push_str("## Per-Session State\n\n");
    let session_dir = Path::new(PROJECT)
        .join(HARNESS_DIR)
        .join(SESSIONS_DIR)
        .join("<session-id>");
    let _ = writeln!(
        out,
        "Session records live under `{}`, and the prompt a session was actually \
         launched with is `{}` inside it. Both are keyed by session id, so two \
         concurrent sessions in one project cannot overwrite each other.\n",
        session_dir.display(),
        COMPILED_PROMPT_FILE
    );
    let _ = writeln!(
        out,
        "The `{PROJECT}` here is the checkout that OWNS the project — the main \
         checkout, never one of its git worktrees. Harness state belongs to the \
         project; a worktree carries only code.\n"
    );
}

/// Documentation index, split by what actually ships.
fn render_docs(out: &mut String, root: &Path) {
    out.push_str("## Authoritative Documentation\n\n");
    out.push_str(
        "This catalog answers layout and tier questions. Design rationale, \
         behaviour contracts, and operational procedure live in documents — but \
         only some of them travel with the installed binary.\n\n",
    );

    // #9012: the bundled docs moved into the instructional content.
    out.push_str("### Ships with the instructional content\n\n");
    out.push_str(
        "`tm install` writes these into `~/.trusty-mpm/framework/docs/` from the \
         content `tm content install` pinned (or from a checkout's `content/`).\n\n",
    );
    let content_docs = crate_doc_files(root);
    if content_docs.is_empty() {
        out.push_str("- (none found under `content/instructions/docs/`)\n");
    } else {
        for name in &content_docs {
            let _ = writeln!(out, "- `content/instructions/docs/{name}`");
        }
    }
    out.push_str("\n### Ships in the published crate\n\n");
    out.push_str("- `crates/trusty-mpm/README.md`, `crates/trusty-mpm/CHANGELOG.md`\n\n");

    out.push_str("### Repo-only — NOT in the published crate\n\n");
    out.push_str(
        "`cargo package` includes only files under `crates/trusty-mpm/`, so \
         everything below requires a checkout of \
         `github.com/bobmatnyc/trusty-tools`. A session running against an \
         installed binary alone cannot open these.\n\n",
    );
    out.push_str("| Path | Holds | Present in this checkout |\n|---|---|---|\n");
    for (rel, what) in REPO_DOCS {
        let present = if root.join(rel).exists() { "yes" } else { "NO" };
        let _ = writeln!(out, "| `{rel}` | {what} | {present} |");
    }
    out.push('\n');
}

/// Commands that report live state, annotated with clap's real `about` text.
fn render_live_state(out: &mut String) {
    out.push_str("## Resolving Live State\n\n");
    out.push_str(
        "This file describes the layout a correct install has. To learn what \
         THIS machine actually resolved, run one of these rather than \
         guessing — the `about` text is pulled from the CLI itself.\n\n",
    );
    for (path, why) in LIVE_STATE_COMMANDS {
        let about = command_about(path);
        let _ = writeln!(out, "- `tm {}` — {why}", path.join(" "));
        let _ = writeln!(out, "  - CLI: {about}");
    }
    out.push_str(
        "\nWhen the compiled prompt and this catalog disagree about a path, the \
         prompt is the thing the session is actually obeying and this catalog is \
         the thing derived from code. Report the disagreement; do not silently \
         pick one.\n",
    );
}

/// Look up a command's `about` string by walking the clap tree.
///
/// Why: hard-coding the description would reintroduce the hand-maintained prose
/// this whole file exists to eliminate. Resolving it live means a renamed or
/// removed command changes the rendered output and trips the drift gate.
/// What: walks `Cli::command()` one segment at a time, returning the `about`
/// text, or an explicit not-found marker.
/// Test: `framework_render_resolves_clap_about_text`.
fn command_about(path: &[&str]) -> String {
    let mut current = Cli::command();
    for segment in path {
        let next = current.find_subcommand(segment).cloned();
        match next {
            Some(sub) => current = sub,
            None => {
                return format!(
                    "(no such command — `tm {}` was renamed or removed)",
                    path.join(" ")
                );
            }
        }
    }
    current
        .get_about()
        .map(|about| about.to_string())
        .unwrap_or_else(|| "(no description)".to_string())
}

/// The checkout holding `start`: its nearest ancestor with a `.git` entry.
///
/// Why (#9298): the compile-time `CARGO_MANIFEST_DIR` named the BUILD
/// checkout, so an installed `tm` probed a reclaimed worktree and rendered
/// every doc row `NO`. Same resolution as `resolve_skills_asset_dir` (#7776).
/// What: the nearest ancestor of `start` holding `.git` (a directory, or a
/// worktree's file); `start` itself when there is none, so the probes report
/// the docs absent rather than reading another tree.
/// Test: `framework_render_probes_the_checkout_holding_start_9298`.
fn repo_root(start: &Path) -> PathBuf {
    start
        .ancestors()
        .find(|d| d.join(".git").exists())
        .unwrap_or(start)
        .to_path_buf()
}

/// Every `.md` file under `content/instructions/docs/` (#9012), sorted.
///
/// Why: this is the set `tm install` deploys from content, so it is read from
/// disk rather than listed by hand — adding or removing a bundled doc changes
/// the generated skill and is caught by the drift gate.
/// What: sorted file names; an unreadable directory yields an empty list.
/// Test: `framework_render_lists_crate_docs`.
fn crate_doc_files(root: &Path) -> Vec<String> {
    let dir = root.join("content/instructions/docs");
    let Ok(entries) = std::fs::read_dir(&dir) else {
        return Vec::new();
    };
    let mut names: Vec<String> = entries
        .flatten()
        .filter_map(|e| {
            let name = e.file_name().to_string_lossy().into_owned();
            name.ends_with(".md").then_some(name)
        })
        .collect();
    names.sort();
    names
}

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

    /// The checkout this test runs for (#9298: runtime, not compile-time).
    fn checkout() -> PathBuf {
        trusty_common::test_harness::test_repo_root().expect("resolve the checkout")
    }

    /// Renders against [`checkout`].
    fn render() -> String {
        super::render(&checkout())
    }

    #[test]
    fn framework_render_lists_the_real_agent_tier_order() {
        let rendered = render();
        let project = rendered
            .find("1. `<project>/.claude/agents`")
            .expect("project tier ranks first");
        let managed = rendered
            .find("claude-config/agents`")
            .expect("managed tier is listed");
        assert!(project < managed, "project tier must precede managed tier");
    }

    #[test]
    fn framework_render_never_names_the_phantom_agent_tier() {
        // #4946: `~/.trusty-mpm/agents/` is the tier the compiled prompt used to
        // claim and no code reads. It may appear only in the sentence that
        // denies it.
        let rendered = render();
        let mentions = rendered.matches("~/.trusty-mpm/agents/").count();
        assert_eq!(
            mentions, 1,
            "the phantom tier may appear once, in the sentence refuting it"
        );
        assert!(
            rendered.contains("There is no `~/.trusty-mpm/agents/` tier"),
            "{rendered}"
        );
    }

    #[test]
    fn framework_render_resolves_clap_about_text() {
        let rendered = render();
        assert!(
            !rendered.contains("no such command"),
            "a documented live-state command was renamed or removed: {rendered}"
        );
    }

    #[test]
    fn framework_render_lists_crate_docs() {
        let docs = crate_doc_files(&checkout());
        assert!(
            !docs.is_empty(),
            "content/instructions/docs/ should hold the bundled docs"
        );
        let rendered = render();
        for name in docs {
            assert!(rendered.contains(&name), "{name} missing from {rendered}");
        }
    }

    #[test]
    fn framework_render_marks_repo_only_docs_present() {
        // Every REPO_DOCS pointer must resolve in a real checkout; a `NO` row
        // means the pointer is dead and the generated file just said so.
        let rendered = render();
        assert!(
            !rendered.contains("| NO |"),
            "a repo-only doc pointer no longer resolves: {rendered}"
        );
    }

    #[test]
    fn framework_render_has_no_blank_line_runs() {
        // Sections are assembled by separate writers; a stray trailing newline
        // in one of them is invisible in source and obvious in the rendered file.
        let rendered = render();
        assert!(!rendered.contains("\n\n\n"), "{rendered}");
    }

    #[test]
    fn framework_render_is_deterministic() {
        assert_eq!(render(), render());
    }

    /// #9298: the docs index probes the checkout holding `start`, found by
    /// walking up, never the checkout that built this binary.
    #[test]
    fn framework_render_probes_the_checkout_holding_start_9298() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let fake = tmp.path().join("fake");
        let docs = fake.join("content/instructions/docs");
        std::fs::create_dir_all(&docs).expect("mkdir docs");
        std::fs::create_dir_all(fake.join(".git")).expect("mkdir .git");
        std::fs::write(docs.join("only-in-the-fake-9298.md"), "x").expect("write doc");
        let start = fake.join("crates/trusty-mpm/src");
        std::fs::create_dir_all(&start).expect("mkdir start");

        assert_eq!(repo_root(&start), fake);
        let rendered = super::render(&start);
        assert!(
            rendered.contains("content/instructions/docs/only-in-the-fake-9298.md"),
            "{rendered}"
        );
        // The fake holds none of the repo-only docs, so every row reads NO.
        assert!(rendered.contains("| `docs/adr/INDEX.md` |"), "{rendered}");
        assert!(!rendered.contains("| yes |"), "{rendered}");
    }
}