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
//! Phase-1 plugin AGENT ingestion: discovery, namespacing, and resolution
//! (issue #3539).
//!
//! Why: a plugin's `agents/*.md` files are the exact same
//! Markdown+frontmatter format `agents::md_loader` already parses for
//! project/embedded agents — the only new work is namespacing
//! (`<plugin>:<name>`) and two Phase-1 leaf-only guarantees #3539 locks in:
//! unsupported frontmatter fields (`effort`/`maxTurns`/`memory`/`isolation`/
//! `disallowedTools` — trusty-mpm's own agent schema, which a plugin may
//! carry over) are dropped with a warning rather than failing the load, and
//! an `extends:` chain is never composed (a plugin agent is always a leaf —
//! cross-catalog compose is a later-phase item).
//! What: [`discover_plugin_agents`] lists every plugin agent as a fully
//! projected [`AgentConfig`] (used by `agents::protocol::agents_list`'s
//! `plugin` tier); [`find_plugin_agent_config`] resolves one namespaced
//! `<plugin>:<name>` on demand (used by `agents::resolve_agent`, the
//! dispatch path). Both route through [`load_plugin_agent`], which reuses
//! `agents::md_loader`'s frontmatter/body parsing
//! ([`crate::agents::md_loader::extract_body`],
//! [`crate::agents::md_loader::project_to_agent_config`]) rather than
//! duplicating the projection — the only difference from a disk agent load
//! is the namespaced name and the two Phase-1 guards above.
//! Test: `tests::*` — discovery finds and namespaces agents, unsupported
//! fields are dropped with a warning, `extends:` is warned-and-ignored,
//! resolution finds/misses a namespaced name, a base-agent-named plugin
//! agent is excluded from listing.
use Path;
use Context;
use agent_metadata_from_str;
use crateAgentConfig;
use crate;
use discover_plugin_roots;
/// trusty-mpm agent-frontmatter fields tcode's `AgentConfig` has no slot
/// for. Present on a plugin agent, they are dropped with one aggregated
/// warning rather than failing the load (#3539's locked Phase-1 contract).
const UNSUPPORTED_AGENT_FIELDS: & = &;
/// List every plugin agent across every discovered plugin, fully projected
/// and namespaced.
///
/// Why: `agents::protocol::agents_list`'s `plugin` tier needs the same
/// `AgentConfig` shape (for `description`/`model` in its wire entry) the
/// embedded and disk tiers already produce.
/// What: for each [`PluginRoot`](crate::plugins::PluginRoot), scans `agents_dir` via
/// `agents::discover_agents` (the same `.md`-only, `.toml`-warns scan disk
/// agents use), skips any local name `agents::protocol::is_base_agent`
/// recognizes (a plugin literally shipping `base-engineer.md` etc. is
/// excluded exactly like the embedded/disk tiers — #3539's base-filter
/// interaction clause), and loads the rest via
/// [`load_plugin_agent`]. A load failure is logged at WARN and skipped
/// (never aborts the whole scan), mirroring `agents::load_all_agents`'s
/// per-file resilience. Sorted by the final namespaced name.
/// Test: `tests::discover_plugin_agents_namespaces_entries`,
/// `tests::discover_plugin_agents_excludes_base_named_agent`,
/// `tests::discover_plugin_agents_skips_unparseable_agent`.
/// Load one plugin agent file, namespaced `<plugin>:<agent_name>`.
///
/// Why: the single load path both [`discover_plugin_agents`] (listing) and
/// [`find_plugin_agent_config`] (dispatch resolution) share, so the
/// namespacing + unsupported-field-drop + extends-warn contract is written
/// exactly once.
/// What: FIRST requires `path`'s canonicalized identity to stay contained
/// within `agents_dir`'s own canonicalization
/// (`plugins::path_is_contained` — code-critic PR #3547 re-review,
/// CRITICAL 5, CWE-59) — rejects a `path` that is (or resolves through) a
/// symlink escaping `agents_dir`, e.g. `agents/leak.md -> ~/.ssh/id_rsa`,
/// BEFORE any content is read. Only then reads `path`, parses frontmatter
/// via `trusty_agents_common::agents::metadata::agent_metadata_from_str`
/// (no `compose_agent` call — a plugin agent is a leaf, #3539), warns and
/// drops any [`UNSUPPORTED_AGENT_FIELDS`] present (see
/// [`warn_unsupported_fields`]), warns (never errors) on a present
/// `extends:` — the agent is still loaded as a direct/leaf document with no
/// inheritance resolved — then projects via
/// `agents::md_loader::project_to_agent_config` exactly as a disk agent
/// would, overriding only `agent.name` with the namespaced form. An
/// unreadable file or a rejected symlink are the only error paths.
/// Test: `tests::load_plugin_agent_projects_fields`,
/// `tests::load_plugin_agent_warns_and_drops_unsupported_fields`,
/// `tests::load_plugin_agent_warns_on_extends_and_treats_as_leaf`,
/// `tests::load_plugin_agent_missing_file_errors`,
/// `tests::load_plugin_agent_rejects_symlinked_file`.
pub
/// Warn once (one aggregated line) when `raw`'s frontmatter declares any
/// [`UNSUPPORTED_AGENT_FIELDS`].
///
/// Why: `#3539` requires "DROP unsupported plugin fields ... with a
/// one-line warn per agent" — one line naming every dropped field, not one
/// line per field (mirrors `agents::warn_on_orphaned_toml`'s aggregation
/// convention). Uses `skills::frontmatter::parse_frontmatter`'s generic
/// flat key/value scan (reused across the agents/skills domain split
/// rather than re-implemented) purely to detect KEY PRESENCE — the
/// supported-field values themselves are parsed separately via
/// `agent_metadata_from_str`.
/// What: no-op when none of the fields are present.
/// Test: `tests::load_plugin_agent_warns_and_drops_unsupported_fields`.
/// Resolve one namespaced `<plugin>:<agent_name>` to its [`AgentConfig`], or
/// `None` when no such plugin/agent exists.
///
/// Why: `agents::resolve_agent`'s dispatch-time entry point for a
/// namespaced name — mirrors that function's own `NotFound`
/// (nothing found, `None`) vs `Load` (found but failed to parse, `Some(Err)`)
/// distinction so its caller can build the identical error shape.
/// What: validates `plugin:agent_name` via
/// `plugins::is_valid_namespaced_name` FIRST, before any path is built
/// (code-critic PR #3547 review, HIGH 3 — the guard belongs to this
/// function, not merely to an upstream caller that happens to reject `:`
/// today) — `None` on a traversal/unsafe-charset payload like
/// `plugin:../../etc`. Then finds the [`PluginRoot`](crate::plugins::PluginRoot) whose resolved `name`
/// equals `plugin` (not the directory name, which may differ — see
/// [`PluginRoot`](crate::plugins::PluginRoot)'s doc); `None` if no such plugin. Within it, `None` if
/// `<agent_name>.md` does not exist; otherwise `Some` wrapping
/// [`load_plugin_agent`]'s result.
/// Test: `tests::find_plugin_agent_config_resolves_known_agent`,
/// `tests::find_plugin_agent_config_unknown_plugin_is_none`,
/// `tests::find_plugin_agent_config_unknown_agent_is_none`,
/// `tests::find_plugin_agent_config_rejects_traversal_name`.