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
//! In-memory `extends:`-chain composition — no filesystem required.
//!
//! Why: [`builder::compose_agent`](crate::agents::builder::compose_agent) is filesystem-coupled: it scans a
//! `source_dir` for `*.md` files before resolving `extends:`. trusty-code
//! wants to bundle a curated subset of trusty-mpm's agents as
//! `include_str!`-embedded assets (issue #2958, epic #2892 Slice E) — those
//! bytes never touch disk, so there is no `source_dir` to scan. Rather than
//! forking the chain-walk (cycle detection, depth limiting, base-first
//! ordering, frontmatter merge) into a second copy, [`builder::resolve`](crate::agents::builder::resolve) and
//! [`builder::render_composed`](crate::agents::builder::render_composed) were generalised over the
//! `pub(crate)` [`builder::SourceLookup`](crate::agents::builder::SourceLookup) trait (Slice E1) so this module
//! only has to supply a new, disk-free implementation of that one seam.
//! What: [`InMemorySources`] is a case-folded `name -> markdown content`
//! map, built via [`build_in_memory_source_map`] from `(name, content)`
//! pairs (e.g. `include_str!` consts). [`compose_agent_in_memory`] resolves
//! an `extends:` chain — including chains through BASE-* templates — entirely
//! against that map and returns the same composed-document shape
//! [`builder::compose_agent`](crate::agents::builder::compose_agent) does for the fs path, with identical behavior
//! for identical content (verified by
//! `fs_and_in_memory_compose_are_byte_equivalent`, below).
//! Test: `cargo test -p trusty-agents-common agents::builder_in_memory`
//! covers single-level extends, a multi-level BASE-template chain, a
//! missing-source error, a missing-extends-target error, a cycle error, a
//! depth-exceeded error, a frontmatter-parse error, `.md`-suffixed key
//! normalisation, and fs/in-memory byte-equivalence.
use HashMap;
use ;
/// A case-folded, in-memory index of agent name -> raw markdown content.
///
/// Why: mirrors [`builder::SourceMap`](crate::agents::builder::SourceMap)'s case-insensitive resolution
/// (`BASE-QA.md` on disk vs. an `extends: base-qa` reference) but for
/// embedded assets that have no filesystem path — e.g. trusty-code's
/// `include_str!`-embedded tm agent `.md` bytes. Without case-folding, an
/// embedded `BASE-QA.md` asset registered under its uppercase stem would
/// fail to resolve `extends: base-qa`, even though the fs path handles this
/// transparently via [`builder::build_source_map`](crate::agents::builder::build_source_map).
/// What: wraps a `HashMap<String, String>` keyed by lowercased name. Never
/// constructed directly by external callers in the common case — use
/// [`build_in_memory_source_map`] — but [`InMemorySources::insert`] is
/// exposed for tests and any caller that builds the map incrementally.
/// Test: `compose_in_memory_single_level`, `case_insensitive_in_memory_resolve`.
;
/// Build an [`InMemorySources`] map from `(name, content)` pairs.
///
/// Why: trusty-code will construct this from ~33 `include_str!` constants
/// (5 BASE templates + 28 coding-relevant agents, issue #2958 Slice E2) —
/// a batch constructor over an iterator is the natural shape for that call
/// site, avoiding 33 individual `insert` calls at each construction point.
/// What: folds `entries` into a fresh [`InMemorySources`] via
/// [`InMemorySources::insert`] (so the same case-folding and last-write-wins
/// semantics apply); an empty iterator yields an empty map.
/// Test: `compose_in_memory_multi_level_base_chain`,
/// `fs_and_in_memory_compose_are_byte_equivalent`.
/// Resolve `name`'s `extends:` chain against `sources` and return the
/// composed Markdown document — the disk-free counterpart of
/// [`builder::compose_agent`](crate::agents::builder::compose_agent).
///
/// Why: issue #2958 (epic #2892 Slice E) needs `extends:` resolved against
/// trusty-code's embedded asset map instead of a `source_dir` on disk, so a
/// fresh tcode project can ship a real agent roster with zero filesystem
/// dependency. Reuses [`builder::resolve`](crate::agents::builder::resolve) (the generic chain walk) and
/// [`builder::render_composed`](crate::agents::builder::render_composed) (the merge/join tail) rather than
/// duplicating either — the only new code here is [`InMemorySources`]'s
/// [`SourceLookup`] implementation.
/// What: walks `name`'s `extends:` chain base-first via `sources`, merges
/// frontmatter child-wins (union for `skills:`, override for `tools:`,
/// identically to the fs path), and concatenates bodies base-first. Returns
/// the same [`AgentBuildError`] variants the fs path does — `NotFound` for a
/// name absent from `sources`, `Cycle`/`DepthExceeded` for a malformed chain,
/// `FrontmatterParse` for malformed frontmatter in any chain member — since
/// both paths share [`builder::resolve`](crate::agents::builder::resolve) and `builder::split_frontmatter`.
/// Test: `compose_in_memory_single_level`,
/// `compose_in_memory_multi_level_base_chain`,
/// `compose_in_memory_missing_source_errors`, `in_memory_cycle_detection`,
/// `in_memory_depth_exceeded`, `in_memory_frontmatter_parse_error`,
/// `fs_and_in_memory_compose_are_byte_equivalent`.