trusty-code 0.5.0

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! Markdown+frontmatter agent loader for tcode — the ONLY on-disk agent
//! source format as of #2897 Slice D (epic #2892). It was DARK-LAUNCHED
//! alongside the (now-retired) TOML loader in Slice B, and became the loader
//! for tcode's own EMBEDDED default agents in Slice C.
//!
//! Why: trusty-agents-common's `agents::builder` compose machinery (Slice A,
//! #2952) already resolves `extends:` inheritance chains for trusty-mpm's
//! `.md` agent format and now carries `max_tokens`/`tools` frontmatter fields
//! that mirror tcode's `AgentConfig` schema. Rather than fork a second `.md`
//! parser, tcode reuses that composer and projects its output onto its
//! existing `AgentConfig`. Slice C (#2897) additionally routes
//! `crate::assets::DEFAULT_AGENTS`'s embedded fallback through this module:
//! the embedded strings have no source directory to resolve an `extends:`
//! chain against (they are compiled-in `&'static str`, not files on disk), so
//! [`project_embedded_md`] skips `compose_agent` entirely and calls
//! [`agent_metadata_from_str`]/[`extract_body`] directly on the raw string —
//! both are already string-in, string/struct-out, so no file-path dependency
//! stands in the way. The two entry points ([`load_md_agent`] for disk,
//! [`project_embedded_md`] for embedded strings) share the exact same
//! [`project_to_agent_config`] mapping, so there is only ever one frontmatter
//! -> `AgentConfig` projection to maintain.
//! What: [`load_md_agent`] reads a `.md` agent source file, calls
//! `trusty_agents_common::agents::builder::compose_agent` to resolve its
//! `extends:` chain into one flattened document, projects the composed
//! frontmatter (via `agents::metadata::agent_metadata_from_str`) and prose
//! body onto tcode's `AgentConfig`. [`project_embedded_md`] does the same
//! projection for an in-memory `&'static str` with no `extends:` resolution.
//! Test: `load_md_agent_base_case`, `tools_projection_*` (the
//! `Option<Vec<String>>` direct-map), `hr1_initial_prompt_not_leaked_into_body`
//! (the HR-1 guard), `assets::tests::*` (embedded-default field-identity vs.
//! the retired TOML fixtures, kept as a historical regression pin).

use std::path::Path;

use trusty_agents_common::agents::builder::compose_agent;
use trusty_agents_common::agents::builder_in_memory::{
    build_in_memory_source_map, compose_agent_in_memory,
};
use trusty_agents_common::agents::metadata::{AgentMetadata, agent_metadata_from_str};

use super::config::{AgentConfig, AgentInfo, LlmParams, SystemPrompt, ToolsConfig};

/// Load a tcode [`AgentConfig`] from a `.md` agent source file.
///
/// Why: the primary entry point for the (now sole) agent loader — takes a
/// file path and returns a fully-populated `AgentConfig` or a descriptive
/// error.
/// What: resolves `path`'s parent directory as the `extends:` source dir and
/// its file stem as the agent name, composes the inheritance chain via
/// `compose_agent`, then projects the result via [`project_to_agent_config`].
/// A missing parent directory, an unreadable file stem, or any
/// `AgentBuildError` (not found, cycle, depth exceeded, malformed
/// frontmatter) is surfaced as a descriptive `anyhow::Error` — never a panic.
/// Test: `load_md_agent_base_case`, `load_md_agent_missing_file_errors`,
/// `load_md_agent_with_extends_resolves_chain`.
pub fn load_md_agent(path: &Path) -> anyhow::Result<AgentConfig> {
    let source_dir = path
        .parent()
        .ok_or_else(|| anyhow::anyhow!("agent file {} has no parent directory", path.display()))?;
    let name = path
        .file_stem()
        .and_then(|s| s.to_str())
        .ok_or_else(|| anyhow::anyhow!("agent file {} has no usable file stem", path.display()))?;

    let composed = compose_agent(name, source_dir).map_err(|e| {
        anyhow::anyhow!(
            "failed to compose agent '{name}' from {}: {e}",
            path.display()
        )
    })?;

    let metadata = agent_metadata_from_str(&composed);
    let body = extract_body(&composed);

    Ok(project_to_agent_config(name, metadata, body))
}

/// Project an embedded (in-memory, no source directory) `.md` agent document
/// onto tcode's [`AgentConfig`] — the entry point for
/// `crate::assets::DEFAULT_AGENTS`'s embedded fallback (Slice C, #2897).
///
/// Why: `load_md_agent` resolves an `extends:` inheritance chain via
/// `compose_agent`, which requires a real source directory to scan for
/// sibling `.md` files. Embedded default agents are compiled-in
/// `&'static str` constants with no filesystem location and no `extends:`
/// chain, so composing would be both impossible (no directory to resolve
/// against) and unnecessary (none of the three bundled defaults declare
/// `extends:`). This function shares every other step of the projection —
/// [`agent_metadata_from_str`] for the frontmatter and [`extract_body`] for
/// the prose body — with [`load_md_agent`], so the frontmatter -> `AgentConfig`
/// mapping in [`project_to_agent_config`] is written and tested exactly once.
/// What: parses `raw`'s frontmatter and body directly (no `compose_agent`
/// call), then delegates to [`project_to_agent_config`]. `default_name` is
/// used only when `raw`'s frontmatter carries no `name:` field, mirroring
/// `load_md_agent`'s file-stem fallback.
/// Test: `assets::tests::default_agents_field_identical_to_retired_toml`,
/// `assets::tests::default_agents_parse_and_names_match`.
pub(crate) fn project_embedded_md(default_name: &str, raw: &str) -> AgentConfig {
    let metadata = agent_metadata_from_str(raw);
    let body = extract_body(raw);
    project_to_agent_config(default_name, metadata, body)
}

/// Project an embedded tm-catalog agent onto tcode's [`AgentConfig`],
/// resolving its `extends:` chain entirely against
/// `crate::assets::EMBEDDED_TM_AGENT_SOURCES` -- no filesystem access
/// (Slice E2, #2958).
///
/// Why: [`project_embedded_md`] handles tcode's own 3 defaults, none of
/// which declare `extends:`. The bundled tm agent catalog (5 `BASE-*`
/// templates + 28 coding-relevant roster agents) DOES use `extends:`
/// chains -- e.g. `rust-engineer` extends `base-engineer` extends
/// `base-agent` -- and [`compose_agent`] can't resolve those because it
/// requires a real `source_dir` to scan, which embedded `&'static str`
/// constants don't have. `trusty_agents_common::agents::builder_in_memory`
/// (Slice E1, PR #3013) supplies the disk-free counterpart: an
/// `InMemorySources` map plus `compose_agent_in_memory`, built here from
/// `crate::assets::EMBEDDED_TM_AGENT_SOURCES` and passed the requested
/// `name`.
/// What: builds an `InMemorySources` map from the embedded catalog table,
/// resolves `name`'s `extends:` chain via `compose_agent_in_memory`, then
/// projects the composed document through the same
/// `agent_metadata_from_str` + `extract_body` + [`project_to_agent_config`]
/// pipeline [`load_md_agent`] uses for the disk path -- so embedded and
/// on-disk `.md` agents share one frontmatter -> `AgentConfig` mapping. Any
/// `AgentBuildError` (unknown name, cycle, depth exceeded, malformed
/// frontmatter anywhere in the chain) is surfaced as a descriptive
/// `anyhow::Error`, never a panic. Called from
/// `crate::agents::load_embedded_default_agents` for every
/// `crate::assets::EmbeddedAgent::Composed` entry in `DEFAULT_AGENTS` (Slice
/// E3, #2958) -- the 28 roster agents are dispatchable defaults as of this
/// slice.
/// Test: `project_embedded_md_with_extends_resolves_rust_engineer_from_base_engineer`,
/// `project_embedded_md_with_extends_unknown_name_errors`,
/// `assets::tests::default_agents_parse_and_names_match`.
pub fn project_embedded_md_with_extends(name: &str) -> anyhow::Result<AgentConfig> {
    let sources =
        build_in_memory_source_map(crate::assets::EMBEDDED_TM_AGENT_SOURCES.iter().copied());

    let composed = compose_agent_in_memory(name, &sources).map_err(|e| {
        anyhow::anyhow!("failed to compose embedded tm-catalog agent '{name}': {e}")
    })?;

    let metadata = agent_metadata_from_str(&composed);
    let body = extract_body(&composed);

    Ok(project_to_agent_config(name, metadata, body))
}

/// Strip the leading YAML frontmatter block from a composed agent document.
///
/// Why: `compose_agent`'s merged frontmatter block may carry an HR-1-injected
/// `initialPrompt` (auto-derived from the agent's `role:`, keyed off
/// trusty-mpm's role table — see
/// `trusty_agents_common::agents::builder::merge_frontmatter`'s "HR-1 Part B"
/// enrichment). tcode's own role vocabulary (`engineer`/`qa`) collides with
/// that table, so an mpm-role-derived `initialPrompt` must NEVER leak into
/// tcode's `system_prompt.content` — tcode's system prompt is the composed
/// PROSE BODY only, exactly as the TOML loader's `system_prompt.content` is a
/// hand-authored prompt with no injected preamble. `AgentMetadata` (the
/// public frontmatter projection this module also uses) already omits
/// `initial_prompt`/`resource_tier` entirely, so there is no accessor that
/// could leak it into the projection; this function provides the second,
/// independent guarantee on the body side by never inspecting the
/// frontmatter block's contents at all — it locates the closing fence
/// structurally and returns only what follows.
/// What: `compose_agent`'s output always opens with a `---` line; this scans
/// past it to the next `---` line and returns everything after, trimmed. A
/// document with no opening fence (defensive fallback; `compose_agent` always
/// emits one) returns the whole string trimmed.
/// Test: `hr1_initial_prompt_not_leaked_into_body`,
/// `interior_horizontal_rule_survives_in_body`,
/// `frontmatter_only_agent_has_empty_body`.
///
/// `pub(crate)`: also reused by `plugins::agents::load_plugin_agent` (#3539)
/// so a plugin agent's body is stripped identically to a disk agent's,
/// without a second frontmatter-fence scanner.
pub(crate) fn extract_body(composed: &str) -> String {
    let mut lines = composed.lines();
    match lines.next() {
        Some(first) if first.trim() == "---" => {}
        _ => return composed.trim().to_string(),
    }

    let mut in_frontmatter = true;
    let mut body_lines: Vec<&str> = Vec::new();
    for line in lines {
        if in_frontmatter {
            if line.trim() == "---" {
                in_frontmatter = false;
            }
            continue;
        }
        body_lines.push(line);
    }
    body_lines.join("\n").trim().to_string()
}

/// Project a composed `.md` agent's metadata + body onto tcode's `AgentConfig`.
///
/// Why: centralises the frontmatter -> TOML-schema field mapping so
/// `load_md_agent` stays a thin IO/compose wrapper.
/// What:
/// - `name` -> `agent.name` (falls back to the file-stem `default_name` when
///   the frontmatter declares no `name:`, mirroring how a source file is
///   identified by its filename).
/// - `role`/`description` -> `agent.role`/`agent.description` (direct map).
/// - `model` -> `agent.model` (the higher-precedence slot per
///   `provider::routing::resolve_model`; `llm.model_override` is left unset —
///   the `.md` frontmatter has exactly one model concept, matching how the
///   TOML fixtures set `[agent].model` and leave `[llm].model_override` unset).
/// - `max_tokens` -> `llm.max_tokens` (direct map).
/// - `tools: Option<Vec<String>>` -> `ToolsConfig.allowed` (DIRECT map — both
///   sides share identical `None`=all-allowed / `Some([])`=deny-all /
///   `Some(list)`=allowlist semantics, per `Frontmatter::tools`'s doc comment
///   and `runner::in_process`'s `agent.tools.as_ref().and_then(|t|
///   t.allowed.as_ref())` consumer, which treats an absent `[tools]` section
///   (outer `None`) and a present-but-unset `allowed` identically — so always
///   wrapping in `Some(ToolsConfig { allowed })` is safe and matches the
///   instructed direct-map).
/// - composed prose body -> `system_prompt.content`.
/// - `skills:` -> intentionally DROPPED. `SystemPrompt::append_skills` exists
///   in tcode's schema but has no consumer anywhere in the codebase (verified:
///   `grep -rn append_skills crates/trusty-code/src` only shows the field
///   declaration and TOML asset literals that set it — nothing reads it). The
///   shared frontmatter's `skills:` list is real and populated
///   (DOC-42/#2889), but mapping it into a dead field would fabricate a
///   consumer that doesn't exist. Left as a gap for a future slice that adds
///   an actual skills consumer to tcode.
///
/// `pub(crate)`: also reused by `plugins::agents::load_plugin_agent` (#3539),
/// which calls this with the plugin's LOCAL agent name (not yet namespaced)
/// and overrides `agent.name` with the namespaced form afterward — so the
/// frontmatter -> `AgentConfig` mapping stays written exactly once across
/// disk, embedded, and plugin agents.
pub(crate) fn project_to_agent_config(
    default_name: &str,
    meta: AgentMetadata,
    body: String,
) -> AgentConfig {
    AgentConfig {
        agent: AgentInfo {
            name: meta.name.unwrap_or_else(|| default_name.to_string()),
            role: meta.role,
            model: meta.model,
            description: meta.description,
        },
        llm: LlmParams {
            temperature: None,
            max_tokens: meta.max_tokens,
            model_override: None,
        },
        system_prompt: SystemPrompt {
            content: body,
            // Gap: no tcode consumer for agent-declared skills yet — see the
            // doc comment above. Left empty rather than mapping into a dead
            // field.
            append_skills: Vec::new(),
        },
        tools: Some(ToolsConfig {
            allowed: meta.tools,
        }),
        runner: None,
    }
}

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

    /// A `.md` agent with no `extends:` composes and projects cleanly.
    ///
    /// Why: the base case — a self-contained agent, no inheritance chain.
    /// What: writes a single `.md` file, loads it, asserts every projected
    /// field.
    /// Test: this test.
    #[test]
    fn load_md_agent_base_case() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            tmp.path().join("solo.md"),
            "---\nname: solo\nrole: engineer\ndescription: A lone agent\nmodel: sonnet\nmax_tokens: 4096\ntools: [read_file, grep]\n---\n\nYou are a solo agent.\n",
        )
        .expect("write");

        let cfg = load_md_agent(&tmp.path().join("solo.md")).expect("load");
        assert_eq!(cfg.agent.name, "solo");
        assert_eq!(cfg.agent.role.as_deref(), Some("engineer"));
        assert_eq!(cfg.agent.description.as_deref(), Some("A lone agent"));
        assert_eq!(cfg.agent.model.as_deref(), Some("sonnet"));
        assert_eq!(cfg.llm.max_tokens, Some(4096));
        assert_eq!(cfg.system_prompt.content, "You are a solo agent.");
        assert_eq!(
            cfg.tools.and_then(|t| t.allowed),
            Some(vec!["read_file".to_string(), "grep".to_string()])
        );
    }

    /// `tools:` projects with identical `Option<Vec<String>>` semantics on
    /// both sides: absent key -> `None` (all allowed), `tools: []` ->
    /// `Some([])` (deny-all), `tools: [a, b]` -> `Some([a, b])` (allowlist).
    ///
    /// Why: this is the load-bearing contract from Slice A (#2952) — a naive
    /// `is_empty()` check would collapse deny-all into "inherit"/"allow-all".
    /// What: three fixtures, one per case.
    /// Test: this test.
    #[test]
    fn tools_projection_absent_is_none() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            tmp.path().join("a.md"),
            "---\nname: a\nrole: engineer\n---\n\nBody.\n",
        )
        .expect("write");
        let cfg = load_md_agent(&tmp.path().join("a.md")).expect("load");
        assert_eq!(cfg.tools.and_then(|t| t.allowed), None);
    }

    #[test]
    fn tools_projection_empty_list_is_deny_all() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            tmp.path().join("a.md"),
            "---\nname: a\nrole: engineer\ntools: []\n---\n\nBody.\n",
        )
        .expect("write");
        let cfg = load_md_agent(&tmp.path().join("a.md")).expect("load");
        assert_eq!(cfg.tools.and_then(|t| t.allowed), Some(vec![]));
    }

    #[test]
    fn tools_projection_list_is_allowlist() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            tmp.path().join("a.md"),
            "---\nname: a\nrole: engineer\ntools: [read_file, grep]\n---\n\nBody.\n",
        )
        .expect("write");
        let cfg = load_md_agent(&tmp.path().join("a.md")).expect("load");
        assert_eq!(
            cfg.tools.and_then(|t| t.allowed),
            Some(vec!["read_file".to_string(), "grep".to_string()])
        );
    }

    /// HR-1 GUARD: a `.md` agent declaring `role: engineer` must NOT get an
    /// mpm-role-derived `initialPrompt` injected into its `system_prompt.content`.
    ///
    /// Why: `merge_frontmatter`'s HR-1 Part B enrichment auto-injects
    /// `initialPrompt: "Begin implementation. ..."` for `role: engineer` (and
    /// a distinct prompt for `role: qa`) because trusty-mpm agents rely on
    /// that injection. tcode's roles collide with that same table by name,
    /// but tcode's system prompt is the composed PROSE BODY only — the
    /// injected `initialPrompt` lives in the frontmatter block, which
    /// `extract_body` never inspects, and `AgentMetadata` (the frontmatter
    /// projection) has no `initial_prompt` field at all. This test proves
    /// both guarantees hold end-to-end.
    /// What: an `engineer`-role agent whose body is a short known string;
    /// asserts `system_prompt.content` equals exactly that string with no
    /// HR-1 preamble prepended or appended.
    /// Test: this test.
    #[test]
    fn hr1_initial_prompt_not_leaked_into_body() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            tmp.path().join("eng.md"),
            "---\nname: eng\nrole: engineer\n---\n\nYou write Rust code.\n",
        )
        .expect("write");

        let cfg = load_md_agent(&tmp.path().join("eng.md")).expect("load");
        assert_eq!(
            cfg.system_prompt.content, "You write Rust code.",
            "HR-1's role-derived initialPrompt must not leak into the body"
        );
        assert!(
            !cfg.system_prompt.content.contains("Begin implementation"),
            "HR-1's engineer-role initialPrompt text must not appear anywhere in the body"
        );
    }

    /// `extends:` chains resolve through `compose_agent` exactly as
    /// trusty-mpm's own `.md` agents do.
    ///
    /// Why: proves the composer is genuinely reused, not reimplemented.
    /// What: a base + child fixture; the child's `AgentConfig` carries the
    /// base's body concatenated before the child's, and the child's
    /// `max_tokens` wins (scalar child-wins merge).
    /// Test: this test.
    #[test]
    fn load_md_agent_with_extends_resolves_chain() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            tmp.path().join("base-x.md"),
            "---\nname: base-x\nmax_tokens: 1000\n---\n\nBase instructions.\n",
        )
        .expect("write");
        std::fs::write(
            tmp.path().join("child.md"),
            "---\nname: child\nextends: base-x\nmax_tokens: 2000\n---\n\nChild instructions.\n",
        )
        .expect("write");

        let cfg = load_md_agent(&tmp.path().join("child.md")).expect("load");
        assert_eq!(cfg.agent.name, "child");
        assert_eq!(cfg.llm.max_tokens, Some(2000), "child max_tokens wins");
        assert!(cfg.system_prompt.content.contains("Base instructions."));
        assert!(cfg.system_prompt.content.contains("Child instructions."));
    }

    /// A missing `.md` file surfaces a descriptive error, never a panic.
    ///
    /// Why: `load_all_agents` needs a fallible, never-panicking contract so
    /// it can `filter_map` a directory listing without one bad path
    /// crashing the whole scan.
    /// What: `load_md_agent` on a nonexistent path returns `Err`.
    /// Test: this test.
    #[test]
    fn load_md_agent_missing_file_errors() {
        let result = load_md_agent(Path::new("/nonexistent/agents/dir/ghost.md"));
        assert!(result.is_err());
    }

    /// A full-fidelity `.md` fixture projects every field the schema exposes
    /// in one document (name, role, model, max_tokens, tools, system-prompt
    /// body) — the successor to Slice B's retired
    /// `parallel_fixture_equivalence` (which additionally compared against a
    /// hand-authored TOML twin; #2897 Slice D removed the TOML loader that
    /// comparison depended on, so this test keeps the full-field-set
    /// assertion on the `.md` fixture alone).
    ///
    /// Why: pins that every projected field — not just the ones individual
    /// narrower tests each cover — round-trips together from one realistic
    /// agent document.
    /// What: one `.md` fixture setting every field; asserts `model`,
    /// `max_tokens`, `tools.allowed`, and `system_prompt.content` all match
    /// the authored values.
    /// Test: this test.
    #[test]
    fn full_fidelity_md_fixture_projects_every_field() {
        let tmp = tempfile::tempdir().expect("tempdir");

        std::fs::write(
            tmp.path().join("twin.md"),
            "---\nname: twin\nrole: engineer\nmodel: sonnet\nmax_tokens: 8192\ntools: [read_file, grep]\n---\n\nYou are a twin agent.\n",
        )
        .expect("write md");

        let cfg = load_md_agent(&tmp.path().join("twin.md")).expect("load md");

        assert_eq!(cfg.agent.name, "twin");
        assert_eq!(cfg.agent.role.as_deref(), Some("engineer"));
        assert_eq!(cfg.agent.model.as_deref(), Some("sonnet"));
        assert_eq!(cfg.llm.max_tokens, Some(8192));
        assert_eq!(
            cfg.tools.and_then(|t| t.allowed),
            Some(vec!["read_file".to_string(), "grep".to_string()])
        );
        assert_eq!(cfg.system_prompt.content, "You are a twin agent.");
    }

    /// `extract_body` closes the frontmatter block on the FIRST `---` fence
    /// only and never re-enters — a `---` line appearing later, inside the
    /// prose body (a markdown horizontal rule between two paragraphs), must
    /// survive verbatim in `system_prompt.content` rather than being mistaken
    /// for a second frontmatter delimiter.
    ///
    /// Why: code-critic finding on PR #2954 — this is the highest-risk edge
    /// case in the fence-scanning loop (`in_frontmatter` flips to `false` and
    /// stays `false`), and had no regression pin.
    /// What: a `.md` fixture whose body contains a paragraph, a bare `---`
    /// horizontal-rule line, then a second paragraph. Loads through the real
    /// `compose_agent` -> `load_md_agent` path (not `extract_body` in
    /// isolation) so the assertion exercises the actual composed output.
    /// Asserts the horizontal rule is present verbatim as its own line in the
    /// resulting body.
    /// Test: this test.
    #[test]
    fn interior_horizontal_rule_survives_in_body() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            tmp.path().join("hr.md"),
            "---\nname: hr\nrole: engineer\n---\n\nFirst paragraph.\n\n---\n\nSecond paragraph.\n",
        )
        .expect("write");

        let cfg = load_md_agent(&tmp.path().join("hr.md")).expect("load");
        assert!(
            cfg.system_prompt
                .content
                .lines()
                .any(|line| line.trim() == "---"),
            "interior horizontal rule must survive verbatim as its own line; got: {:?}",
            cfg.system_prompt.content
        );
        assert!(cfg.system_prompt.content.contains("First paragraph."));
        assert!(cfg.system_prompt.content.contains("Second paragraph."));
    }

    /// A `.md` agent with a valid frontmatter block and NO prose body after
    /// the closing fence projects to an EMPTY `system_prompt.content` — no
    /// panic, no leftover blank lines, no stray `---`.
    ///
    /// Why: code-critic finding on PR #2954 — the frontmatter-only/empty-body
    /// case is the other edge of the fence-scanning loop and had no
    /// regression pin.
    /// What: a `.md` fixture whose closing fence is the last line of the
    /// file. Loads through the real `compose_agent` -> `load_md_agent` path.
    /// Asserts `system_prompt.content == ""`.
    /// Test: this test.
    #[test]
    fn frontmatter_only_agent_has_empty_body() {
        let tmp = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            tmp.path().join("empty.md"),
            "---\nname: empty\nrole: engineer\n---\n",
        )
        .expect("write");

        let cfg = load_md_agent(&tmp.path().join("empty.md")).expect("load");
        assert_eq!(
            cfg.system_prompt.content, "",
            "frontmatter-only agent must project to an empty body, got: {:?}",
            cfg.system_prompt.content
        );
    }

    /// `project_embedded_md_with_extends` resolves a real 3-level
    /// `extends:` chain (`rust-engineer` -> `base-engineer` -> `base-agent`)
    /// entirely against the embedded tm catalog table, with no filesystem
    /// access.
    ///
    /// Why: this is the acceptance criterion for Slice E2 (#2958) -- the
    /// embedded-extends entry point must actually compose a chain, not just
    /// parse a single flat document. `rust-engineer.md`'s frontmatter
    /// declares `extends: base-engineer`, which itself declares
    /// `extends: base-agent` (see `assets/agents/rust-engineer.md` and
    /// `assets/agents/BASE-ENGINEER.md`), so a correct compose must pull
    /// prose from all three tiers into the final body.
    /// What: calls `project_embedded_md_with_extends("rust-engineer")` and
    /// asserts the name/role/model project correctly and that the composed
    /// body contains marker text unique to each of the three tiers
    /// (BASE-AGENT's "Foundation for all trusty-mpm agents", BASE-ENGINEER's
    /// "Foundation for all engineer agents", and rust-engineer's own
    /// "toolchains-rust-core").
    /// Test: this test.
    #[test]
    fn project_embedded_md_with_extends_resolves_rust_engineer_from_base_engineer() {
        let cfg = project_embedded_md_with_extends("rust-engineer").expect("compose rust-engineer");

        assert_eq!(cfg.agent.name, "rust-engineer");
        assert_eq!(cfg.agent.role.as_deref(), Some("engineer"));
        assert_eq!(cfg.agent.model.as_deref(), Some("sonnet"));
        assert!(
            cfg.system_prompt
                .content
                .contains("Foundation for all trusty-mpm agents"),
            "composed body must include BASE-AGENT tier content"
        );
        assert!(
            cfg.system_prompt
                .content
                .contains("Foundation for all engineer agents"),
            "composed body must include BASE-ENGINEER tier content"
        );
        assert!(
            cfg.system_prompt.content.contains("toolchains-rust-core"),
            "composed body must include rust-engineer's own content"
        );
    }

    /// `project_embedded_md_with_extends` surfaces an unknown agent name as
    /// a descriptive `anyhow::Error`, never a panic.
    ///
    /// Why: mirrors `load_md_agent`'s error-surfacing contract for the fs
    /// path -- a caller iterating the embedded catalog with a typo'd name
    /// must get a message it can log, not an unwind.
    /// What: requests a name absent from `EMBEDDED_TM_AGENT_SOURCES` and
    /// asserts the call errors.
    /// Test: this test.
    #[test]
    fn project_embedded_md_with_extends_unknown_name_errors() {
        let result = project_embedded_md_with_extends("does-not-exist");
        assert!(result.is_err(), "unknown embedded agent name must error");
    }
}