@jaybeeuu/agent-cortex 1.37.1 → 1.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/.agents/skills/style-versioning/SKILL.md +9 -8
  2. package/.github/workflows/ci.yml +190 -8
  3. package/AGENTS.md +25 -6
  4. package/CHANGELOG.md +42 -0
  5. package/README.md +132 -33
  6. package/agents/README.md +47 -13
  7. package/agents/plan/agent.md +51 -0
  8. package/agents/plan/claude/frontmatter.json +6 -0
  9. package/agents/plan/copilot/frontmatter.json +6 -0
  10. package/agents/plan/pi/frontmatter.json +6 -0
  11. package/agents/plan.agent.md +3 -0
  12. package/agents/ralph/agent.md +262 -0
  13. package/agents/ralph/claude/frontmatter.json +6 -0
  14. package/agents/ralph/claude/polling.md +31 -0
  15. package/agents/ralph/copilot/frontmatter.json +6 -0
  16. package/agents/ralph/copilot/polling.md +62 -0
  17. package/agents/ralph/pi/frontmatter.json +6 -0
  18. package/agents/ralph/pi/polling.md +74 -0
  19. package/agents/ralph-plan/agent.md +114 -0
  20. package/agents/ralph-plan/claude/frontmatter.json +6 -0
  21. package/agents/ralph-plan/copilot/frontmatter.json +6 -0
  22. package/agents/ralph-plan/pi/frontmatter.json +6 -0
  23. package/agents/ralph-plan.agent.md +5 -2
  24. package/agents/ralph.agent.md +83 -57
  25. package/agents/strategy/agent.md +104 -0
  26. package/agents/strategy/claude/frontmatter.json +6 -0
  27. package/agents/strategy/copilot/frontmatter.json +6 -0
  28. package/agents/strategy/pi/frontmatter.json +6 -0
  29. package/agents/strategy.agent.md +3 -0
  30. package/bin/agent-cortex.mjs +68 -1
  31. package/bin/installers/claude.mjs +262 -0
  32. package/bin/installers/copilot.mjs +129 -0
  33. package/bin/installers/pi.mjs +197 -0
  34. package/claude/.claude-plugin/plugin.json +1 -1
  35. package/claude/agents/plan.md +3 -1
  36. package/claude/agents/ralph-plan.md +5 -3
  37. package/claude/agents/strategy.md +3 -1
  38. package/claude/hooks/scripts/notify.mjs +54 -0
  39. package/claude/hooks.json +15 -0
  40. package/docs/claude-hooks.md +115 -0
  41. package/docs/ideas/auto-model-selection.md +75 -0
  42. package/docs/ideas/improve-pi-system-prompt.md +34 -0
  43. package/docs/ideas/model-selector.md +40 -0
  44. package/docs/ideas/pi-task-memory.md +34 -0
  45. package/docs/ideas/qa-extension.md +43 -0
  46. package/docs/ideas/unified-ci-pipeline.md +28 -0
  47. package/docs/skills/skill-anatomy.md +9 -0
  48. package/extensions/agent-modes/README.md +54 -0
  49. package/extensions/agent-modes/discover.test.ts +241 -0
  50. package/extensions/agent-modes/discover.ts +458 -0
  51. package/extensions/agent-modes/index.ts +13 -224
  52. package/extensions/agent-modes/node_modules/.bin/pi +21 -0
  53. package/extensions/agent-modes/node_modules/.bin/tsc +21 -0
  54. package/extensions/agent-modes/node_modules/.bin/tsserver +21 -0
  55. package/extensions/agent-modes/node_modules/.bin/tsx +21 -0
  56. package/extensions/agent-modes/package.json +20 -0
  57. package/extensions/agent-modes/tsconfig.json +12 -0
  58. package/extensions/subagent/README.md +19 -0
  59. package/extensions/subagent/index.ts +131 -45
  60. package/extensions/subagent/node_modules/.bin/pi +21 -0
  61. package/extensions/subagent/node_modules/.bin/pi-ai +21 -0
  62. package/extensions/subagent/node_modules/.bin/tsc +21 -0
  63. package/extensions/subagent/node_modules/.bin/tsserver +21 -0
  64. package/extensions/subagent/node_modules/.bin/tsx +21 -0
  65. package/extensions/subagent/package.json +23 -0
  66. package/extensions/subagent/tsconfig.json +12 -0
  67. package/extensions/subagent/wait.test.ts +202 -0
  68. package/extensions/subagent/wait.ts +118 -0
  69. package/hooks/claude/hooks.json +38 -0
  70. package/hooks/claude/scripts/notify.mjs +54 -0
  71. package/lib/cli.mjs +62 -3
  72. package/package.json +6 -4
  73. package/plugin.json +1 -1
  74. package/scripts/build-copilot-agents.mjs +30 -0
  75. package/scripts/lib/compose-agent.mjs +241 -0
  76. package/skills/engineering/design-an-interface/SKILL.md +1 -1
  77. package/skills/engineering/improve-codebase-architecture/SKILL.md +1 -1
  78. package/skills/workflow/ralph/REFERENCE.md +2 -2
  79. package/skills/workflow/ralph/SKILL.md +6 -6
  80. package/skills/workflow/run-pipeline-stage/scripts/generate-progress.ts +4 -2
  81. package/skills/workflow/using-agent-skills/SKILL.md +122 -0
  82. package/test/cli.test.mjs +115 -2
  83. package/test/copilot-installer.test.mjs +298 -0
  84. package/test/installer.test.mjs +179 -0
  85. package/test/notify-hook.test.mjs +63 -0
  86. package/test/pi-installer.test.mjs +371 -0
  87. package/token-map.README.md +128 -0
  88. package/token-map.json +106 -0
  89. package/.github/workflows/ci.test.ts +0 -217
  90. package/.github/workflows/release.yml +0 -58
  91. package/scripts/build-claude-agents.mjs +0 -179
  92. package/scripts/publish-release.sh +0 -32
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { parseArgs, buildHelpText, validateHarness, SUPPORTED_HARNESSES } from "../lib/cli.mjs";
3
+ import { parseArgs, buildHelpText, validateHarness } from "../lib/cli.mjs";
4
+ import { installPi } from "./installers/pi.mjs";
4
5
 
5
6
  const argv = process.argv.slice(2);
6
7
  const parsed = parseArgs(argv);
@@ -16,12 +17,78 @@ if (parsed.command === "unknown") {
16
17
  }
17
18
 
18
19
  if (parsed.command === "install") {
20
+ if (parsed.optionError) {
21
+ process.stderr.write(`${parsed.optionError}\n`);
22
+ process.exit(1);
23
+ }
24
+
19
25
  const check = validateHarness(parsed.harness);
20
26
  if (!check.ok) {
21
27
  process.stderr.write(`${check.error}\nRun "agent-cortex --help" for usage.\n`);
22
28
  process.exit(1);
23
29
  }
24
30
 
31
+ if (parsed.harness === "pi") {
32
+ try {
33
+ const result = installPi({
34
+ dryRun: parsed.dryRun ?? false,
35
+ warn: () => {}, // warnings surface once in the printed summary
36
+ ...(parsed.output ? { output: parsed.output } : {}),
37
+ ...(parsed.pluginRoot ? { pluginRoot: parsed.pluginRoot } : {}),
38
+ });
39
+ printPiInstall(result);
40
+ } catch (err) {
41
+ process.stderr.write(`Install failed: ${err.message}\n`);
42
+ process.exit(1);
43
+ }
44
+ process.exit(0);
45
+ }
46
+
25
47
  process.stdout.write(`Installing agent-cortex for "${parsed.harness}" harness…\n`);
48
+
49
+ // claude is the first wired harness: generates the plugin subtree from the
50
+ // canonical sources (agents/, agents-native/, skills/, hooks/claude/) via the
51
+ // shared installClaude generator — `pnpm build:claude` is an alias of this
52
+ // invocation, so there is no separate build-time code path.
53
+ if (parsed.harness === "claude") {
54
+ // Avoid loading the installer on the help/summary paths and for other harnesses.
55
+ const { installClaude } = await import("./installers/claude.mjs");
56
+ try {
57
+ installClaude({ output: parsed.output, dryRun: parsed.dryRun });
58
+ process.exit(0);
59
+ } catch (err) {
60
+ process.stderr.write(`Install failed: ${err.message}\n`);
61
+ process.exit(1);
62
+ }
63
+ }
64
+
65
+ // copilot regenerates the flat agents/*.agent.md files the Copilot plugin
66
+ // loads via plugin.json ("agents": "agents/") — the same code path
67
+ // `pnpm build:copilot` runs.
68
+ if (parsed.harness === "copilot") {
69
+ const { installCopilot } = await import("./installers/copilot.mjs");
70
+ try {
71
+ installCopilot({ output: parsed.output, dryRun: parsed.dryRun });
72
+ process.exit(0);
73
+ } catch (err) {
74
+ process.stderr.write(`Install failed: ${err.message}\n`);
75
+ process.exit(1);
76
+ }
77
+ }
78
+
26
79
  process.exit(0);
27
80
  }
81
+
82
+ function printPiInstall(result) {
83
+ process.stdout.write(`Installing agent-cortex for "pi" harness…\n`);
84
+ for (const agent of result.agents) {
85
+ process.stdout.write(` ✓ ${agent.name}.agent.md → ${agent.filePath}\n`);
86
+ }
87
+ process.stdout.write(` ✓ Substituted ${result.skills.md} markdown file(s) across ${result.skills.skills} skill(s) → ${result.skills.dir}\n`);
88
+ for (const warning of result.warnings) {
89
+ process.stdout.write(` ⚠ ${warning}\n`);
90
+ }
91
+ if (result.dryRun) {
92
+ process.stdout.write("(dry-run — nothing written)\n");
93
+ }
94
+ }
@@ -0,0 +1,262 @@
1
+ // Install-time generator for the Claude Code plugin subtree — the ONLY code path
2
+ // for producing claude/ from the canonical sources. There is no build-time
3
+ // generator: the package script `pnpm build:claude` is a pure alias of the CLI
4
+ // (`node bin/agent-cortex.mjs install claude`), so regenerating the committed
5
+ // subtree and installing the plugin run exactly the same code:
6
+ //
7
+ // node bin/agent-cortex.mjs install claude `pnpm build:claude` (regenerates
8
+ // the committed claude/ subtree in place; `--output
9
+ // <dir>` targets another dir, `--dry-run` plans
10
+ // without writing)
11
+ //
12
+ // claude/ stays committed (not gitignored): the marketplace install flow
13
+ // (`claude plugin marketplace add <repo>` → `claude plugin install
14
+ // agent-cortex@jaybeeuu`) reads the subtree straight from the working tree, so a
15
+ // fresh clone installs as-is with no generation step. CI regenerates it via the
16
+ // same installer and runs `git diff --exit-code claude/...` — byte-for-byte
17
+ // validation that the committed output equals installer output, catching stale
18
+ // output and hand-edits to generated files alike.
19
+ //
20
+ // Generated from:
21
+ // claude/agents/<slug>.md agents/<name>/agent.md +
22
+ // agents/<name>/claude/frontmatter.json +
23
+ // agents/<name>/claude/<section>.md ({{SECTION:...}}),
24
+ // with {{TOOL:...}}/{{PATH:...}} substituted against
25
+ // the claude column of token-map.json. Agents in the
26
+ // DEFER set ship instead from agents-native/<name>.md.
27
+ // claude/skills/<name> symlinks to the grouped skills/<group>/<name> dirs
28
+ // (single source — Claude scans skills one level deep)
29
+ // claude/.claude-plugin/plugin.json name/version/description from package.json
30
+ // (version tracks the package — never stale)
31
+ // claude/hooks.json copied from hooks/claude/hooks.json when present
32
+ // claude/hooks/<scripts> copied from hooks/claude/ (support files bundled
33
+ // alongside hooks.json, referenced via ${CLAUDE_PLUGIN_ROOT})
34
+ //
35
+ // The claude/ subtree stays committed and CI drift-checks it (build:claude +
36
+ // git diff --exit-code), so a fresh clone remains installable as-is. Hand-authored
37
+ // support files under claude/ (claude/.mcp.json, claude/scripts/) are left untouched.
38
+ //
39
+ // Zero dependencies so it runs on the CI Node and local Node alike.
40
+
41
+ import { readFileSync, writeFileSync, mkdirSync, rmSync, symlinkSync, statSync, existsSync, readdirSync } from "node:fs";
42
+ import { join, dirname, relative } from "node:path";
43
+ import { fileURLToPath } from "node:url";
44
+ import { composeAgent } from "../../scripts/lib/compose-agent.mjs";
45
+
46
+ const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
47
+ const DEFAULT_ROOT = join(MODULE_DIR, "..", "..");
48
+
49
+ // Agents NOT transformed from the composable sources. `ralph` is authored natively for Claude
50
+ // instead (agents-native/ralph.md) — its event-driven orchestration can't be produced by
51
+ // section composition from the poll-loop sources.
52
+ const DEFER = new Set(["ralph"]);
53
+
54
+ // Ralph-coupled Copilot skills that don't apply to the lean Claude Ralph (and carry dead
55
+ // ~/.copilot orchestration paths). Not symlinked into the Claude plugin.
56
+ const SKILL_EXCLUDE = new Set(["ralph", "run-pipeline-stage"]);
57
+
58
+ const NAME_PREFIX = "agent-cortex:";
59
+
60
+ function isFile(p) {
61
+ try {
62
+ return statSync(p).isFile();
63
+ } catch {
64
+ return false;
65
+ }
66
+ }
67
+
68
+ function byName(a, b) {
69
+ return a.localeCompare(b);
70
+ }
71
+
72
+ /** Compose agents from agents/<name>/; DEFERed agents come from agents-native/ instead. */
73
+ function buildAgents(root) {
74
+ const files = [];
75
+ const skipped = [];
76
+ const entries = readdirSync(join(root, "agents"), { withFileTypes: true }).sort((a, b) => byName(a.name, b.name));
77
+
78
+ for (const entry of entries) {
79
+ if (!entry.isDirectory()) continue;
80
+ const dir = join(root, "agents", entry.name);
81
+ if (!isFile(join(dir, "agent.md")) || !isFile(join(dir, "claude", "frontmatter.json"))) continue;
82
+
83
+ if (DEFER.has(entry.name)) {
84
+ skipped.push(entry.name);
85
+ continue;
86
+ }
87
+
88
+ const fm = composeAgent(root, entry.name, "claude");
89
+ const slug = fm.name.startsWith(NAME_PREFIX) ? fm.name.slice(NAME_PREFIX.length) : fm.name;
90
+ const header =
91
+ `---\n` +
92
+ `# GENERATED from agents/${entry.name}/ by bin/installers/claude.mjs — DO NOT EDIT.\n` +
93
+ `name: ${slug}\n` +
94
+ `description: ${JSON.stringify(fm.description)}\n` +
95
+ `tools: ${fm.tools.join(", ")}\n` +
96
+ `---\n`;
97
+ files.push({ file: `${slug}.md`, content: `${header}\n${fm.body}\n` });
98
+ }
99
+ return { files, skipped };
100
+ }
101
+
102
+ /** Copy hand-authored Claude-native agents into claude/agents/ verbatim. */
103
+ function buildNatives(root, nativeSrc) {
104
+ const files = [];
105
+ if (!existsSync(nativeSrc)) return files;
106
+ for (const file of readdirSync(nativeSrc).filter((f) => f.endsWith(".md")).sort(byName)) {
107
+ files.push({ file, content: readFileSync(join(nativeSrc, file), "utf-8") });
108
+ }
109
+ return files;
110
+ }
111
+
112
+ /** Flatten grouped skills (skills/<group>/<name>/) into claude/skills/ as relative symlinks. */
113
+ function buildSkills(root) {
114
+ const links = [];
115
+ const seen = new Map();
116
+ for (const group of readdirSync(join(root, "skills")).sort(byName)) {
117
+ const groupDir = join(root, "skills", group);
118
+ if (!statSync(groupDir).isDirectory()) continue;
119
+ for (const name of readdirSync(groupDir).sort(byName)) {
120
+ const skillDir = join(groupDir, name);
121
+ if (!isFile(join(skillDir, "SKILL.md"))) continue;
122
+ if (SKILL_EXCLUDE.has(name)) continue;
123
+ if (seen.has(name)) {
124
+ throw new Error(`duplicate skill name "${name}" (${seen.get(name)} and ${group}) — names must be unique when flattened`);
125
+ }
126
+ seen.set(name, group);
127
+ links.push({ name, target: skillDir });
128
+ }
129
+ }
130
+ return links;
131
+ }
132
+
133
+ /** plugin.json manifest; version tracks the package so it can never go stale. */
134
+ function buildPluginJson(root) {
135
+ const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf-8"));
136
+ return (
137
+ JSON.stringify(
138
+ {
139
+ name: "agent-cortex",
140
+ version: pkg.version,
141
+ description: "Personal Claude Code plugin with custom agents and skills",
142
+ author: { name: "jaybeeuu" },
143
+ license: pkg.license ?? "MIT",
144
+ repository: pkg.repository,
145
+ hooks: "./hooks.json",
146
+ },
147
+ null,
148
+ 2,
149
+ ) + "\n"
150
+ );
151
+ }
152
+
153
+ /**
154
+ * Hook support files (hooks/claude/ except hooks.json) bundled into
155
+ * claude/hooks/ so hook commands can reference them via ${CLAUDE_PLUGIN_ROOT}.
156
+ * hooks.json itself is special-cased to the plugin root (claude/hooks.json).
157
+ */
158
+ function buildHookFiles(root) {
159
+ const files = [];
160
+ const srcDir = join(root, "hooks", "claude");
161
+ if (!existsSync(srcDir)) return files;
162
+ const walk = (dir, rel) => {
163
+ for (const entry of readdirSync(dir, { withFileTypes: true }).sort((a, b) => byName(a.name, b.name))) {
164
+ const full = join(dir, entry.name);
165
+ if (entry.isDirectory()) walk(full, join(rel, entry.name));
166
+ else if (entry.name !== "hooks.json")
167
+ files.push({ rel: join(rel, entry.name), content: readFileSync(full, "utf-8") });
168
+ }
169
+ };
170
+ walk(srcDir, "");
171
+ return files;
172
+ }
173
+
174
+ /**
175
+ * Generate the Claude Code plugin subtree.
176
+ *
177
+ * @param {object} options
178
+ * @param {string} [options.root] Package root (contains agents/, skills/, token-map.json, …).
179
+ * Defaults to the repo containing this module.
180
+ * @param {string} [options.output] Target directory (default: <root>/claude).
181
+ * @param {boolean} [options.dryRun] Plan and report without writing anything.
182
+ * @returns {{ output: string, agents: string[], natives: string[], skills: string[], hooks: boolean, dryRun: boolean }}
183
+ */
184
+ export function installClaude({ root = DEFAULT_ROOT, output, dryRun = false } = {}) {
185
+ const out = output ?? join(root, "claude");
186
+
187
+ const agents = buildAgents(root);
188
+ const natives = buildNatives(root, join(root, "agents-native"));
189
+ const skills = buildSkills(root);
190
+ const pluginJson = buildPluginJson(root);
191
+ const hooksSrc = join(root, "hooks", "claude", "hooks.json");
192
+ const hooksJson = isFile(hooksSrc) ? readFileSync(hooksSrc, "utf-8") : null;
193
+ const hookFiles = buildHookFiles(root);
194
+
195
+ const summary = {
196
+ output: out,
197
+ agents: agents.files.map((f) => f.file.replace(/\.md$/, "")),
198
+ natives: natives.map((f) => f.file.replace(/\.md$/, "")),
199
+ skills: skills.map((l) => l.name),
200
+ hooks: hooksJson !== null,
201
+ hookFiles: hookFiles.map((f) => f.rel),
202
+ dryRun,
203
+ };
204
+
205
+ if (agents.skipped.length) console.log(`Skipped composable source(s): ${agents.skipped.join(", ")}`);
206
+
207
+ if (dryRun) {
208
+ console.log(`DRY-RUN — would generate into ${out}, no files written:`);
209
+ console.log(` agents: ${summary.agents.join(", ")}`);
210
+ if (summary.natives.length) console.log(` native agents: ${summary.natives.join(", ")}`);
211
+ console.log(` skills: ${summary.skills.length} symlink(s)`);
212
+ console.log(` .claude-plugin/plugin.json (version ${JSON.parse(pluginJson).version})`);
213
+ if (summary.hooks) console.log(" hooks.json (from hooks/claude/hooks.json)");
214
+ if (summary.hookFiles.length)
215
+ console.log(` hook scripts: ${summary.hookFiles.join(", ")} (bundled into claude/hooks/)`);
216
+ return summary;
217
+ }
218
+
219
+ // Generated subsets only — hand-authored support files (.mcp.json, scripts/)
220
+ // under output are left untouched.
221
+ const agentOut = join(out, "agents");
222
+ rmSync(agentOut, { recursive: true, force: true });
223
+ mkdirSync(agentOut, { recursive: true });
224
+ for (const { file, content } of [...agents.files, ...natives]) {
225
+ writeFileSync(join(agentOut, file), content);
226
+ }
227
+ console.log(`Generated ${summary.agents.length} Claude agent(s): ${summary.agents.join(", ")}`);
228
+ if (summary.natives.length) console.log(`Copied ${summary.natives.length} native Claude agent(s): ${summary.natives.join(", ")}`);
229
+
230
+ const skillOut = join(out, "skills");
231
+ rmSync(skillOut, { recursive: true, force: true });
232
+ mkdirSync(skillOut, { recursive: true });
233
+ for (const { name, target } of skills) {
234
+ symlinkSync(relative(skillOut, target), join(skillOut, name));
235
+ }
236
+ console.log(`Linked ${summary.skills.length} skill(s) into ${skillOut}`);
237
+
238
+ mkdirSync(join(out, ".claude-plugin"), { recursive: true });
239
+ writeFileSync(join(out, ".claude-plugin", "plugin.json"), pluginJson);
240
+ console.log(`Wrote ${join(out, ".claude-plugin", "plugin.json")}`);
241
+
242
+ if (summary.hooks) {
243
+ writeFileSync(join(out, "hooks.json"), hooksJson);
244
+ console.log(`Wrote ${join(out, "hooks.json")} (from ${hooksSrc})`);
245
+
246
+ if (hookFiles.length > 0) {
247
+ const hookOut = join(out, "hooks");
248
+ rmSync(hookOut, { recursive: true, force: true });
249
+ mkdirSync(hookOut, { recursive: true });
250
+ for (const { rel, content } of hookFiles) {
251
+ const dest = join(hookOut, rel);
252
+ mkdirSync(dirname(dest), { recursive: true });
253
+ writeFileSync(dest, content);
254
+ }
255
+ console.log(`Bundled ${hookFiles.length} hook support file(s) into ${hookOut}`);
256
+ }
257
+ } else {
258
+ console.log(`Skipped hooks.json — ${hooksSrc} not found (existing file left untouched)`);
259
+ }
260
+
261
+ return summary;
262
+ }
@@ -0,0 +1,129 @@
1
+ // Install-time generator for the Copilot CLI flat agent files (agents/*.agent.md).
2
+ // This is the single code path for producing those files from the canonical
3
+ // composable sources — both entry points below call installCopilot(), so
4
+ // install-time and build-time output can never diverge:
5
+ //
6
+ // - bin/agent-cortex.mjs `agent-cortex install copilot` (install-time;
7
+ // `--output <dir>` overrides the target,
8
+ // `--dry-run` plans without writing)
9
+ // - scripts/build-copilot-agents.mjs `pnpm build:copilot` (dev/CI: regenerates
10
+ // the committed agents/*.agent.md files in place)
11
+ //
12
+ // Output lands in <root>/agents/ (the default `output`) because that is the
13
+ // directory the Copilot plugin actually loads: plugin.json declares
14
+ // "agents": "agents/", which scans for *.agent.md files. The generated flat
15
+ // files therefore share the agents/ directory with the canonical composable
16
+ // dirs they are composed from (agents/<name>/) — only flat *.agent.md files are
17
+ // written, nothing is deleted, so a generated file can never clobber a
18
+ // composable source. `--output` exists for the same preview/test contract as the
19
+ // claude and pi installers: point it at a scratch dir and inspect the output.
20
+ //
21
+ // agents/<name>.agent.md composed from agents/<name>/agent.md +
22
+ // agents/<name>/copilot/frontmatter.json +
23
+ // agents/<name>/copilot/<section>.md ({{SECTION:...}}),
24
+ // with {{TOOL:...}}/{{PATH:...}} substituted against
25
+ // the copilot column of token-map.json.
26
+ //
27
+ // The flat files stay committed and CI drift-checks them (build:copilot +
28
+ // git diff --exit-code -- 'agents/*.agent.md'), so a fresh clone (or a direct
29
+ // path plugin install) ships the plugin read-ready.
30
+ //
31
+ // Zero dependencies so it runs on the CI Node and local Node alike.
32
+
33
+ import { writeFileSync, mkdirSync, readdirSync, statSync, existsSync } from "node:fs";
34
+ import { join, dirname } from "node:path";
35
+ import { fileURLToPath } from "node:url";
36
+ import { composeAgent } from "../../scripts/lib/compose-agent.mjs";
37
+
38
+ const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
39
+ const DEFAULT_ROOT = join(MODULE_DIR, "..", "..");
40
+
41
+ function isDirectory(p) {
42
+ try {
43
+ return statSync(p).isDirectory();
44
+ } catch {
45
+ return false;
46
+ }
47
+ }
48
+
49
+ function byName(a, b) {
50
+ return a.localeCompare(b);
51
+ }
52
+
53
+ /**
54
+ * Serialise the flat-file YAML frontmatter. Field order and names mirror the
55
+ * pre-migration flat sources (description, name, tools, argument-hint) so
56
+ * Copilot CLI sees the same agent metadata. The GENERATED comment references
57
+ * scripts/build-copilot-agents.mjs — the committed entry point — so the
58
+ * generated bytes are identical whichever of the two entry points ran.
59
+ */
60
+ function frontmatterYaml(name, fm) {
61
+ const lines = [
62
+ "---",
63
+ `# GENERATED from agents/${name}/ by scripts/build-copilot-agents.mjs — DO NOT EDIT.`,
64
+ `description: ${JSON.stringify(fm.description)}`,
65
+ `name: ${JSON.stringify(fm.name)}`,
66
+ `tools: [${fm.tools.map((t) => JSON.stringify(t)).join(", ")}]`,
67
+ ];
68
+ if (fm.argumentHint) lines.push(`argument-hint: ${JSON.stringify(fm.argumentHint)}`);
69
+ lines.push("---");
70
+ return lines.join("\n");
71
+ }
72
+
73
+ /** Compose agents from agents/<name>/; every composable dir must ship a copilot harness. */
74
+ function buildAgents(root) {
75
+ const files = [];
76
+ const entries = readdirSync(join(root, "agents"), { withFileTypes: true }).sort((a, b) => byName(a.name, b.name));
77
+
78
+ for (const entry of entries) {
79
+ if (!entry.isDirectory()) continue; // skip generated *.agent.md flat files and README
80
+ const dir = join(root, "agents", entry.name);
81
+ if (!existsSync(join(dir, "agent.md"))) continue; // not a composable agent dir
82
+ if (!isDirectory(join(dir, "copilot")) || !existsSync(join(dir, "copilot", "frontmatter.json"))) {
83
+ throw new Error(`agent "${entry.name}": composable directory without copilot/frontmatter.json — define the copilot harness or exclude the agent`);
84
+ }
85
+
86
+ const fm = composeAgent(root, entry.name, "copilot");
87
+ files.push({
88
+ name: entry.name,
89
+ file: `${entry.name}.agent.md`,
90
+ content: `${frontmatterYaml(entry.name, fm)}\n\n${fm.body}\n`,
91
+ });
92
+ }
93
+ return files;
94
+ }
95
+
96
+ /**
97
+ * Generate the Copilot CLI flat agent files.
98
+ *
99
+ * @param {object} options
100
+ * @param {string} [options.root] Package root (contains agents/, token-map.json, …).
101
+ * Defaults to the repo containing this module.
102
+ * @param {string} [options.output] Directory to write the flat *.agent.md files into
103
+ * (default: <root>/agents — the dir plugin.json's
104
+ * "agents": "agents/" scans for the plugin).
105
+ * @param {boolean} [options.dryRun] Plan and report without writing anything.
106
+ * @returns {{ output: string, agents: string[], dryRun: boolean }}
107
+ */
108
+ export function installCopilot({ root = DEFAULT_ROOT, output, dryRun = false } = {}) {
109
+ const out = output ?? join(root, "agents");
110
+ const files = buildAgents(root);
111
+ const summary = {
112
+ output: out,
113
+ agents: files.map((f) => f.name),
114
+ dryRun,
115
+ };
116
+
117
+ if (dryRun) {
118
+ console.log(`DRY-RUN — would generate into ${out}, no files written:`);
119
+ console.log(` agents: ${summary.agents.join(", ")}`);
120
+ return summary;
121
+ }
122
+
123
+ mkdirSync(out, { recursive: true });
124
+ for (const { file, content } of files) {
125
+ writeFileSync(join(out, file), content);
126
+ }
127
+ console.log(`Generated ${files.length} Copilot agent file(s): ${summary.agents.join(", ")}`);
128
+ return summary;
129
+ }
@@ -0,0 +1,197 @@
1
+ // Pi harness installer: agent-cortex install pi
2
+ //
3
+ // Materialises the agent-cortex package into the pi runtime's user scope:
4
+ //
5
+ // <output>/agents/<slug>.agent.md composed agents (default ~/.pi/agent/agents)
6
+ // <output>/skills/<group>/<name>/ token-substituted skill copies (default ~/.pi/agent/skills)
7
+ //
8
+ // Composition per the token-map.json contract:
9
+ // 1. {{SECTION:name}} is resolved from the agent's pi/<name>.md section file
10
+ // (shared composer, scripts/lib/compose-agent.mjs).
11
+ // 2. {{TOOL:key}} is substituted with the pi column; a null mapping drops the
12
+ // token from prose with a warning (pi has no ask_user/skill tool).
13
+ // 3. {{PATH:key}} named keys resolve through the paths table; bare relative
14
+ // paths resolve against the plugin root (default: token-map.json's pi value,
15
+ // overridable with --plugin-root for checkout/symlinked installs).
16
+ // Frontmatter tool lists are translated the same way agent-modes does at runtime:
17
+ // canonical → pi column, null → omitted, unknown (native pi tool) → pass through.
18
+ // Skill markdown files get the same TOOL/PATH substitution so pi never serves
19
+ // literal token syntax; other skill files are copied verbatim.
20
+ //
21
+ // Skills install into ~/.pi/agent/skills (pi's user-global skill dir), which pi
22
+ // loads before package skills, so the substituted copies shadow the raw package
23
+ // skills of the same name (pi dedupes by name, user wins).
24
+ //
25
+ // Zero dependencies so it runs on the CI Node and local Node alike.
26
+
27
+ import { readFileSync, writeFileSync, mkdirSync, copyFileSync, readdirSync, statSync } from "node:fs";
28
+ import { join, dirname } from "node:path";
29
+ import { fileURLToPath } from "node:url";
30
+ import { homedir } from "node:os";
31
+ import { composeAgent, loadTokenMap, substituteTokens, translateToolList } from "../../scripts/lib/compose-agent.mjs";
32
+
33
+ const PI = "pi";
34
+
35
+ // The token-map contract version this installer implements (token-map.json
36
+ // "version" field). A higher map version is rejected: the contract must be
37
+ // extended before this installer can trust it.
38
+ const CONTRACT_VERSION = 1;
39
+
40
+ const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
41
+ const DEFAULT_OUTPUT = join(homedir(), ".pi", "agent");
42
+
43
+ const DEFAULT_WARN = (msg) => console.warn(`[pi-installer] ${msg}`);
44
+
45
+ function isDirectory(p) {
46
+ try {
47
+ return statSync(p).isDirectory();
48
+ } catch {
49
+ return false;
50
+ }
51
+ }
52
+
53
+ function isFile(p) {
54
+ try {
55
+ return statSync(p).isFile();
56
+ } catch {
57
+ return false;
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Install the agent-cortex pi harness into the pi runtime.
63
+ *
64
+ * @param {object} options
65
+ * @param {string} [options.root] Package root (defaults to this file's repo)
66
+ * @param {string} [options.output] Pi user-scope dir; agents go to
67
+ * <output>/agents, skills to <output>/skills
68
+ * (default ~/.pi/agent)
69
+ * @param {boolean} [options.dryRun] Compute and report without writing anything
70
+ * @param {string} [options.pluginRoot] Override plugin_root used for {{PATH:...}}
71
+ * resolution (default: token-map pi value)
72
+ * @param {(msg: string) => void} [options.warn] Warning sink, also collected in `warnings`
73
+ * @returns {{ agents: {name:string, filePath:string}[], skills: {skills:number, md:number, files:number, dir:string},
74
+ * warnings: string[], dryRun: boolean, agentsDir: string, skillsDir: string }}
75
+ */
76
+ export function installPi(options = {}) {
77
+ const root = options.root ?? REPO_ROOT;
78
+ const output = options.output ?? DEFAULT_OUTPUT;
79
+ const dryRun = options.dryRun ?? false;
80
+ const warnings = [];
81
+ const warn = (msg) => {
82
+ warnings.push(msg);
83
+ (options.warn ?? DEFAULT_WARN)(msg);
84
+ };
85
+
86
+ const tokenMap = loadTokenMap(root);
87
+ if (typeof tokenMap.version === "number" && tokenMap.version > CONTRACT_VERSION) {
88
+ throw new Error(
89
+ `token-map.json version ${tokenMap.version} is newer than the contract version ${CONTRACT_VERSION} this installer implements — upgrade agent-cortex`,
90
+ );
91
+ }
92
+
93
+ const pluginRoot = options.pluginRoot ?? tokenMap.paths.plugin_root?.[PI];
94
+ const agentsDir = join(output, "agents");
95
+ const skillsDir = join(output, "skills");
96
+
97
+ const agents = installAgents({ root, agentsDir, dryRun, pluginRoot, tokenMap, warn });
98
+ const skills = installSkills({ root, skillsDir, dryRun, pluginRoot, tokenMap, warn });
99
+
100
+ return { agents, skills, warnings, dryRun, agentsDir, skillsDir };
101
+ }
102
+
103
+ // ─── Agents ──────────────────────────────────────────────────────────────────
104
+
105
+ function installAgents({ root, agentsDir, dryRun, pluginRoot, tokenMap, warn }) {
106
+ const written = [];
107
+ const agentsSrc = join(root, "agents");
108
+ for (const entry of readdirSync(agentsSrc, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
109
+ if (!entry.isDirectory()) continue; // skip generated *.agent.md flat files
110
+ const dir = join(agentsSrc, entry.name);
111
+ if (!isFile(join(dir, "agent.md"))) continue; // not a composable agent dir
112
+ if (!isDirectory(join(dir, PI)) || !isFile(join(dir, PI, "frontmatter.json"))) {
113
+ throw new Error(`agent "${entry.name}": composable directory without pi/frontmatter.json — define the pi harness or exclude the agent`);
114
+ }
115
+
116
+ const fm = composeAgent(root, entry.name, PI, {
117
+ dropNullTools: true,
118
+ pluginRoot,
119
+ resolveRelativePaths: true,
120
+ warn,
121
+ });
122
+ const tools = translateToolList(fm.tools, tokenMap, PI, { warn });
123
+ const content = renderPiAgent(entry.name, fm, tools);
124
+ const filePath = join(agentsDir, `${entry.name}.agent.md`);
125
+ if (!dryRun) {
126
+ mkdirSync(agentsDir, { recursive: true });
127
+ writeFileSync(filePath, content);
128
+ }
129
+ written.push({ name: entry.name, filePath });
130
+ }
131
+ return written;
132
+ }
133
+
134
+ function renderPiAgent(name, fm, tools) {
135
+ const lines = [
136
+ "---",
137
+ `# GENERATED from agents/${name}/ by bin/installers/pi.mjs — DO NOT EDIT.`,
138
+ `name: ${JSON.stringify(fm.name)}`,
139
+ `description: ${JSON.stringify(fm.description)}`,
140
+ `tools: ${JSON.stringify(tools.join(" "))}`,
141
+ ];
142
+ if (fm.model) lines.push(`model: ${JSON.stringify(fm.model)}`);
143
+ if (fm.argumentHint) lines.push(`argument-hint: ${JSON.stringify(fm.argumentHint)}`);
144
+ lines.push("---");
145
+ return `${lines.join("\n")}\n\n${fm.body}\n`;
146
+ }
147
+
148
+ // ─── Skills ──────────────────────────────────────────────────────────────────
149
+
150
+ function installSkills({ root, skillsDir, dryRun, pluginRoot, tokenMap, warn }) {
151
+ const skillsSrc = join(root, "skills");
152
+ if (!isDirectory(skillsSrc)) return { skills: 0, md: 0, files: 0, dir: skillsDir };
153
+
154
+ const transform = (content) =>
155
+ substituteTokens(content, PI, tokenMap, { dropNullTools: true, pluginRoot, resolveRelativePaths: true, warn });
156
+
157
+ if (!dryRun) mkdirSync(skillsDir, { recursive: true });
158
+ let skills = 0;
159
+ let md = 0;
160
+ let files = 0;
161
+ for (const group of readdirSync(skillsSrc).sort()) {
162
+ const groupDir = join(skillsSrc, group);
163
+ if (!isDirectory(groupDir)) continue;
164
+ for (const name of readdirSync(groupDir).sort()) {
165
+ if (!isFile(join(groupDir, name, "SKILL.md"))) continue;
166
+ skills += 1;
167
+ const stats = copyTree(join(groupDir, name), join(skillsDir, group, name), transform, dryRun);
168
+ md += stats.md;
169
+ files += stats.files;
170
+ }
171
+ }
172
+ return { skills, md, files, dir: skillsDir };
173
+ }
174
+
175
+ /** Recursively copy a tree; transform .md file contents, copy everything else verbatim. */
176
+ function copyTree(src, dest, transform, dryRun) {
177
+ let md = 0;
178
+ let files = 0;
179
+ const entries = readdirSync(src, { withFileTypes: true });
180
+ for (const entry of entries) {
181
+ const from = join(src, entry.name);
182
+ const to = join(dest, entry.name);
183
+ if (entry.isDirectory()) {
184
+ const stats = copyTree(from, to, transform, dryRun);
185
+ md += stats.md;
186
+ files += stats.files;
187
+ } else if (entry.isFile()) {
188
+ files += 1;
189
+ if (entry.name.endsWith(".md")) md += 1;
190
+ if (dryRun) continue;
191
+ mkdirSync(dest, { recursive: true });
192
+ if (entry.name.endsWith(".md")) writeFileSync(to, transform(readFileSync(from, "utf-8")));
193
+ else copyFileSync(from, to);
194
+ }
195
+ }
196
+ return { md, files };
197
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-cortex",
3
- "version": "1.31.2",
3
+ "version": "1.38.1",
4
4
  "description": "Personal Claude Code plugin with custom agents and skills",
5
5
  "author": {
6
6
  "name": "jaybeeuu"