-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplugin_bundle.rs
More file actions
386 lines (353 loc) · 14.5 KB
/
Copy pathplugin_bundle.rs
File metadata and controls
386 lines (353 loc) · 14.5 KB
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
//! Shared plugin bundle registry.
//!
//! The source tree is unified where host formats match; host-specific overlays
//! remain where each installer needs a different manifest, hook, command, or
//! agent format.
//!
//! Layout of `plugin/`:
//! - `plugin/skills/*/SKILL.md` — the 17 shared model-invocable skills **plus**
//! the 13 canonical (`claude`/`codex`) workflow dispatcher skills (30 total).
//! Cursor deploys only the 17 model-invocable skills (not the dispatcher
//! skills); its explicit dispatch is native commands (below).
//! - `plugin/overlays/cursor/commands/tracedecay-*.md` — Cursor 1.6+ native
//! slash commands, one per workflow slug, deployed to `commands/<slug>.md`.
//! These replace the old Cursor dispatcher *skills*.
//! - `plugin/agents/*.md` — Claude-form subagents (deployed by Claude).
//! - `plugin/overlays/cursor/agents/*.md` — Cursor-form subagents.
//! - `plugin/commands/*.md` — Claude slash commands.
//! - `plugin/rules/*.mdc` — Cursor rules.
//! - `plugin/hooks/hooks-<host>.json` — per-host hook wiring; each deploys to
//! `hooks/hooks.json`.
//! - `plugin/.claude-plugin/{plugin,marketplace}.json`,
//! `plugin/.cursor-plugin/plugin.json`, `plugin/.codex-plugin/plugin.json` —
//! host manifests (deploy to the same dot-dir path).
//! - `plugin/.mcp.json` — shared Claude/Codex MCP config (byte-identical);
//! `plugin/mcp-cursor.json` — Cursor MCP config (deploys to `mcp.json`).
//! - `plugin/README-<host>.md` — per-host README (deploys to `README.md`).
//!
//! Composed per-host view = `GENERATED_SKILL_FILES` (recursively embedded from
//! `plugin/skills/`, filtered per host) ∪ `<HOST>_MANIFEST_FILES` and extras.
use crate::errors::Result;
/// Stamp the plugin manifest `version` field with the crate version, returning
/// pretty-printed JSON with a trailing newline. Shared by every host installer
/// (Claude/Cursor/Codex), which all render the same manifest round-trip.
pub(crate) fn stamp_manifest_version(raw: &str) -> Result<String> {
let mut manifest: serde_json::Value = serde_json::from_str(raw)?;
manifest["version"] = serde_json::json!(env!("CARGO_PKG_VERSION"));
Ok(format!("{}\n", serde_json::to_string_pretty(&manifest)?))
}
/// Point the MCP config's `mcpServers.tracedecay.command` at the resolved
/// binary path, returning pretty-printed JSON with a trailing newline. Claude
/// and Cursor use this directly; Codex layers scope-specific args/env on top.
pub(crate) fn set_mcp_command(raw: &str, bin: &str) -> Result<String> {
let mut mcp: serde_json::Value = serde_json::from_str(raw)?;
mcp["mcpServers"]["tracedecay"]["command"] = serde_json::json!(bin);
Ok(format!("{}\n", serde_json::to_string_pretty(&mcp)?))
}
/// One embedded plugin file: `relative` is its deploy path; `contents` may come
/// from a different source path in the shared `plugin/` tree.
#[derive(Clone, Copy)]
pub struct PluginFile {
pub relative: &'static str,
pub contents: &'static str,
}
macro_rules! plugin_file {
($relative:literal, $source:literal) => {
PluginFile {
relative: $relative,
contents: include_str!(concat!("../../plugin/", $source)),
}
};
}
// Every file under `plugin/skills/`, including support files, embedded by build.rs.
include!(concat!(env!("OUT_DIR"), "/plugin_bundle_generated.rs"));
/// Prefix of the dispatcher skills that Cursor does **not** deploy (they are
/// native commands on Cursor). Claude/Codex deploy every skill.
const CURSOR_EXCLUDED_SKILL_PREFIX: &str = "skills/tracedecay-";
fn all_skill_files() -> impl Iterator<Item = &'static PluginFile> {
GENERATED_SKILL_FILES.iter()
}
fn cursor_skill_files() -> impl Iterator<Item = &'static PluginFile> {
GENERATED_SKILL_FILES
.iter()
.filter(|file| !file.relative.starts_with(CURSOR_EXCLUDED_SKILL_PREFIX))
}
/// Cursor's native slash commands for the canonical workflow slugs.
const CURSOR_COMMAND_FILES: &[PluginFile] = &[
plugin_file!(
"commands/tracedecay-audit-safety.md",
"overlays/cursor/commands/tracedecay-audit-safety.md"
),
plugin_file!(
"commands/tracedecay-check-health.md",
"overlays/cursor/commands/tracedecay-check-health.md"
),
plugin_file!(
"commands/tracedecay-clean-dead-code.md",
"overlays/cursor/commands/tracedecay-clean-dead-code.md"
),
plugin_file!(
"commands/tracedecay-compare-branches.md",
"overlays/cursor/commands/tracedecay-compare-branches.md"
),
plugin_file!(
"commands/tracedecay-curate-memory.md",
"overlays/cursor/commands/tracedecay-curate-memory.md"
),
plugin_file!(
"commands/tracedecay-draft-commit.md",
"overlays/cursor/commands/tracedecay-draft-commit.md"
),
plugin_file!(
"commands/tracedecay-find-impact.md",
"overlays/cursor/commands/tracedecay-find-impact.md"
),
plugin_file!(
"commands/tracedecay-fix-build.md",
"overlays/cursor/commands/tracedecay-fix-build.md"
),
plugin_file!(
"commands/tracedecay-map-architecture.md",
"overlays/cursor/commands/tracedecay-map-architecture.md"
),
plugin_file!(
"commands/tracedecay-port-code.md",
"overlays/cursor/commands/tracedecay-port-code.md"
),
plugin_file!(
"commands/tracedecay-recall-memory.md",
"overlays/cursor/commands/tracedecay-recall-memory.md"
),
plugin_file!(
"commands/tracedecay-review-diff.md",
"overlays/cursor/commands/tracedecay-review-diff.md"
),
plugin_file!(
"commands/tracedecay-test-changes.md",
"overlays/cursor/commands/tracedecay-test-changes.md"
),
];
/// Claude-form subagents.
const CLAUDE_AGENT_FILES: &[PluginFile] = &[
plugin_file!("agents/code-explorer.md", "agents/code-explorer.md"),
plugin_file!(
"agents/code-health-auditor.md",
"agents/code-health-auditor.md"
),
plugin_file!("agents/session-historian.md", "agents/session-historian.md"),
];
/// Cursor-form subagents.
const CURSOR_AGENT_FILES: &[PluginFile] = &[
plugin_file!(
"agents/code-explorer.md",
"overlays/cursor/agents/code-explorer.md"
),
plugin_file!(
"agents/code-health-auditor.md",
"overlays/cursor/agents/code-health-auditor.md"
),
plugin_file!(
"agents/session-historian.md",
"overlays/cursor/agents/session-historian.md"
),
];
/// Claude slash commands.
const CLAUDE_COMMAND_FILES: &[PluginFile] = &[
plugin_file!("commands/audit-safety.md", "commands/audit-safety.md"),
plugin_file!("commands/check-health.md", "commands/check-health.md"),
plugin_file!("commands/clean-dead-code.md", "commands/clean-dead-code.md"),
plugin_file!(
"commands/compare-branches.md",
"commands/compare-branches.md"
),
plugin_file!("commands/curate-memory.md", "commands/curate-memory.md"),
plugin_file!("commands/draft-commit.md", "commands/draft-commit.md"),
plugin_file!("commands/find-impact.md", "commands/find-impact.md"),
plugin_file!("commands/fix-build.md", "commands/fix-build.md"),
plugin_file!(
"commands/map-architecture.md",
"commands/map-architecture.md"
),
plugin_file!("commands/port-code.md", "commands/port-code.md"),
plugin_file!("commands/recall-memory.md", "commands/recall-memory.md"),
plugin_file!("commands/review-diff.md", "commands/review-diff.md"),
plugin_file!("commands/test-changes.md", "commands/test-changes.md"),
];
/// Cursor `.mdc` rules.
const CURSOR_RULE_FILES: &[PluginFile] = &[
plugin_file!("rules/tracedecay.mdc", "rules/tracedecay.mdc"),
plugin_file!("rules/tracedecay-memory.mdc", "rules/tracedecay-memory.mdc"),
];
/// Claude manifest dir + shared MCP + Claude hooks + README.
pub const CLAUDE_MANIFEST_FILES: &[PluginFile] = &[
plugin_file!(
".claude-plugin/marketplace.json",
".claude-plugin/marketplace.json"
),
plugin_file!(".claude-plugin/plugin.json", ".claude-plugin/plugin.json"),
plugin_file!(".mcp.json", ".mcp.json"),
plugin_file!("README.md", "README-claude.md"),
plugin_file!("hooks/hooks.json", "hooks/hooks-claude.json"),
];
/// Cursor manifest + Cursor MCP + Cursor hooks + README.
pub const CURSOR_MANIFEST_FILES: &[PluginFile] = &[
plugin_file!(".cursor-plugin/plugin.json", ".cursor-plugin/plugin.json"),
plugin_file!("README.md", "README-cursor.md"),
plugin_file!("mcp.json", "mcp-cursor.json"),
plugin_file!("hooks/hooks.json", "hooks/hooks-cursor.json"),
];
/// Codex manifest + shared MCP + Codex hooks + README.
pub const CODEX_MANIFEST_FILES: &[PluginFile] = &[
plugin_file!(".codex-plugin/plugin.json", ".codex-plugin/plugin.json"),
plugin_file!(".mcp.json", ".mcp.json"),
plugin_file!("README.md", "README-codex.md"),
plugin_file!("hooks/hooks.json", "hooks/hooks-codex.json"),
];
/// Compose a host's deploy set as deterministic `(relative, contents)` tuples.
fn compose(
sections: &[&'static [PluginFile]],
skills: impl Iterator<Item = &'static PluginFile>,
) -> Vec<(&'static str, &'static str)> {
sections
.iter()
.flat_map(|section| section.iter())
.chain(skills)
.map(|file| (file.relative, file.contents))
.collect()
}
/// Files Claude deploys: manifest + Claude agents + Claude commands + every
/// skill file (all 30 skills incl. dispatchers, plus any support files).
pub fn claude_files() -> Vec<(&'static str, &'static str)> {
compose(
&[
CLAUDE_MANIFEST_FILES,
CLAUDE_AGENT_FILES,
CLAUDE_COMMAND_FILES,
],
all_skill_files(),
)
}
/// Files Cursor deploys: manifest + Cursor rules + Cursor agents + Cursor
/// native commands + the shared skill files *without* the `tracedecay-*`
/// dispatcher skills (those slugs are native commands on Cursor).
pub fn cursor_files() -> Vec<(&'static str, &'static str)> {
compose(
&[
CURSOR_MANIFEST_FILES,
CURSOR_RULE_FILES,
CURSOR_AGENT_FILES,
CURSOR_COMMAND_FILES,
],
cursor_skill_files(),
)
}
/// Files Codex deploys: manifest + every skill file (all 30 skills incl.
/// dispatchers, plus any support files). Codex ships no agents/commands/rules.
pub fn codex_files() -> Vec<(&'static str, &'static str)> {
compose(&[CODEX_MANIFEST_FILES], all_skill_files())
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
fn plugin_source_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("plugin")
}
/// No host deploys the same relative path twice.
fn assert_unique_relatives(files: &[(&str, &str)], host: &str) {
let mut seen = BTreeSet::new();
for (relative, _) in files {
assert!(
seen.insert(*relative),
"{host}: duplicate deploy path {relative}"
);
}
}
#[test]
fn each_host_deploys_unique_relative_paths() {
assert_unique_relatives(&claude_files(), "claude");
assert_unique_relatives(&cursor_files(), "cursor");
assert_unique_relatives(&codex_files(), "codex");
}
#[test]
fn every_embedded_file_has_content() {
// The macro embeds at compile time, so a missing source fails the build.
// Every file we ship (skills, manifests, mcp, hooks, README) is
// non-empty, so an empty embed signals a truncated or wrong source.
for host in [claude_files(), cursor_files(), codex_files()] {
for (relative, contents) in host {
assert!(!contents.is_empty(), "{relative} embedded empty");
}
}
}
#[test]
fn each_host_composes_the_expected_file_count() {
// Skill files are embedded recursively (SKILL.md + support files), so
// the skill count is derived from the generated set rather than a
// frozen literal. Cursor drops the `tracedecay-*` dispatcher skills.
let all_skills = GENERATED_SKILL_FILES.len();
let cursor_skills = cursor_skill_files().count();
// Claude: skills + 5 manifest (2 dot + mcp + hooks + README) + 3 agents
// + 13 commands.
assert_eq!(claude_files().len(), all_skills + 5 + 3 + 13);
// Cursor: cursor-subset skills + 4 manifest (dot + mcp + hooks +
// README) + 2 rules + 3 agents + 13 native commands.
assert_eq!(cursor_files().len(), cursor_skills + 4 + 2 + 3 + 13);
// Codex: skills + 4 manifest (dot + mcp + hooks + README).
assert_eq!(codex_files().len(), all_skills + 4);
}
/// Every embedded skill file maps to an on-disk source under `plugin/`.
#[test]
fn generated_skill_files_have_source_paths() {
let root = plugin_source_root();
assert!(
!GENERATED_SKILL_FILES.is_empty(),
"generated skill file set is empty"
);
for file in GENERATED_SKILL_FILES {
assert!(
root.join(file.relative).exists(),
"skill source missing: plugin/{}",
file.relative
);
}
}
/// The recursive embed must cover the on-disk skill tree exactly — every
/// file under `plugin/skills/` is embedded, and nothing extra.
#[test]
fn generated_skill_files_cover_the_skill_tree_exactly() {
let skills_root = plugin_source_root().join("skills");
let mut on_disk = BTreeSet::new();
collect_relative(&skills_root, &skills_root, &mut on_disk);
let embedded: BTreeSet<String> = GENERATED_SKILL_FILES
.iter()
.map(|file| {
file.relative
.strip_prefix("skills/")
.expect("skill deploy path is under skills/")
.to_string()
})
.collect();
assert_eq!(
embedded, on_disk,
"GENERATED_SKILL_FILES must match every file under plugin/skills/ exactly"
);
}
fn collect_relative(base: &Path, dir: &Path, out: &mut BTreeSet<String>) {
for entry in std::fs::read_dir(dir).expect("read skills dir").flatten() {
let path = entry.path();
if path.is_dir() {
collect_relative(base, &path, out);
} else if path.is_file() {
out.insert(
path.strip_prefix(base)
.expect("under base")
.to_string_lossy()
.replace('\\', "/"),
);
}
}
}
}