@nylorun/core 0.52.0-beta → 0.54.0-beta

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 (83) hide show
  1. package/CHANGELOG.md +119 -98
  2. package/dist/compatibility.d.ts +39 -12
  3. package/dist/compatibility.js +37 -10
  4. package/dist/contracts.d.ts +126 -44
  5. package/dist/contracts.js +127 -69
  6. package/dist/define.d.ts +2 -2
  7. package/dist/define.js +1 -1
  8. package/dist/definition/bound.d.ts +3 -1
  9. package/dist/definition/declaration.js +1 -0
  10. package/dist/definition/delegate.d.ts +7 -4
  11. package/dist/definition/delegate.js +4 -3
  12. package/dist/definition/from.js +3 -1
  13. package/dist/definition/manifest.js +7 -1
  14. package/dist/definition/sections.d.ts +9 -8
  15. package/dist/definition/sections.js +13 -5
  16. package/dist/definition/variant.d.ts +2 -2
  17. package/dist/definition/variant.js +4 -4
  18. package/dist/definition/workflow/runnable.d.ts +1 -1
  19. package/dist/definition/workflow/runnable.js +1 -1
  20. package/dist/definition/workflow/verdict.d.ts +1 -1
  21. package/dist/definition/workflow/verdict.js +1 -1
  22. package/dist/engine/blocks.d.ts +2 -2
  23. package/dist/engine/blocks.js +3 -3
  24. package/dist/engine/explain.d.ts +1 -1
  25. package/dist/engine/explain.js +3 -3
  26. package/dist/engine/features.d.ts +1 -1
  27. package/dist/engine/features.js +1 -1
  28. package/dist/engine/index.d.ts +1 -1
  29. package/dist/engine/index.js +1 -1
  30. package/dist/engine/mcp.d.ts +4 -4
  31. package/dist/engine/mcp.js +4 -4
  32. package/dist/engine/names.d.ts +1 -1
  33. package/dist/engine/names.js +1 -1
  34. package/dist/engine/preview.d.ts +2 -2
  35. package/dist/engine/preview.js +1 -1
  36. package/dist/engine/projection.d.ts +3 -3
  37. package/dist/engine/projection.js +8 -8
  38. package/dist/engine/tools.d.ts +8 -8
  39. package/dist/engine/tools.js +7 -7
  40. package/dist/errors.d.ts +1 -1
  41. package/dist/folder/compile.d.ts +2 -2
  42. package/dist/folder/compile.js +111 -15
  43. package/dist/folder/decompile.js +7 -7
  44. package/dist/folder/engine.d.ts +8 -2
  45. package/dist/folder/engine.js +3 -3
  46. package/dist/folder/fix.js +1 -1
  47. package/dist/folder/flow.d.ts +1 -1
  48. package/dist/folder/flow.js +1 -1
  49. package/dist/folder/index.d.ts +4 -2
  50. package/dist/folder/index.js +4 -2
  51. package/dist/folder/migrate-v3.js +2 -2
  52. package/dist/folder/migrate-v4.d.ts +33 -0
  53. package/dist/folder/migrate-v4.js +207 -0
  54. package/dist/folder/nested.js +14 -6
  55. package/dist/folder/plugins.d.ts +13 -1
  56. package/dist/folder/plugins.js +49 -12
  57. package/dist/folder/schemas.js +29 -9
  58. package/dist/folder/subagent-files.d.ts +110 -0
  59. package/dist/folder/subagent-files.js +514 -0
  60. package/dist/folder/v3.d.ts +2 -2
  61. package/dist/folder/v3.js +2 -2
  62. package/dist/harness-api/artifacts.d.ts +1 -1
  63. package/dist/harness-api/json-schema.js +6 -6
  64. package/dist/harness-api/messages.d.ts +1 -1
  65. package/dist/harness-api/messages.js +1 -1
  66. package/dist/harness-api/schema.d.ts +2 -2
  67. package/dist/harness-api/schema.js +8 -8
  68. package/dist/manifest-schemas.js +9 -6
  69. package/dist/plugins/index.js +1 -1
  70. package/dist/schemas/agent-folder/agent-md.schema.json +67 -18
  71. package/dist/schemas/agent-manifest.schema.json +65 -2
  72. package/dist/schemas/harness-manifest.schema.json +55 -2
  73. package/dist/types/manifest.d.ts +32 -7
  74. package/dist/types/model.d.ts +3 -3
  75. package/dist/types/observe.d.ts +1 -1
  76. package/dist/types/part.d.ts +5 -3
  77. package/dist/types/transcript.d.ts +1 -1
  78. package/dist/utils/definition-files.d.ts +23 -0
  79. package/dist/utils/definition-files.js +50 -4
  80. package/dist/utils/memory.js +1 -1
  81. package/dist/utils/runtime-limits.d.ts +2 -2
  82. package/dist/utils/runtime-limits.js +3 -3
  83. package/package.json +1 -1
@@ -0,0 +1,110 @@
1
+ /**
2
+ * `agent-folder/4`: a subagent is one file, `agents/<id>.md`, and the manifest's
3
+ * `tools/`, `skills/`, `mcp.json`, `plugins/` and Runtime plugins are its **library**, which every
4
+ * model loop in it draws on: the root's own, and each subagent's, which inherits it and narrows it
5
+ * with `tools`, `disallowedTools`, `skills` and `mcpServers`.
6
+ *
7
+ * ```markdown
8
+ * ---
9
+ * name: history
10
+ * description: Searches the person's past work.
11
+ * tools: [search_work, get_task]
12
+ * ---
13
+ * You find earlier work for a bot…
14
+ * ```
15
+ *
16
+ * A subagent is named by its frontmatter `name`, whatever its file is called, as Claude Code and
17
+ * Cursor name theirs. An Agent Plugin's `agents/*.md` under `plugins/<name>/` are subagents too, read
18
+ * by the same rules and labelled with the plugin; no subagent inherits another.
19
+ *
20
+ * The compiler keeps every check it has by **expanding** format 4 into the layout format 3 already
21
+ * compiles: each subagent file becomes a virtual `agents/<name>/` holding the file's bytes as its
22
+ * `AGENT.md` and a copy of the library (the same bytes and hashes), or, for a file with `flow:`,
23
+ * its flow file and the library tools the flow names. The subagent's compile then narrows what it
24
+ * inherited (`narrowFront`, `narrowParts`), and every diagnostic is moved back to the file people
25
+ * wrote (`compileFormat4`). Pure, like the compiler.
26
+ */
27
+ import type { CompileAgentFolderInput, CompiledAgentFolder } from "./compile.js";
28
+ import { PLUGIN_NAMES, pluginTools, type PluginName } from "../plugins/index.js";
29
+ import type { ReportOptions } from "./context.js";
30
+ /** The format name a v4 folder writes. */
31
+ export declare const AGENT_FOLDER_V4 = "agent-folder/4";
32
+ /** Whether `value` names the v4 format: `agent-folder/4` or `4`. */
33
+ export declare function isFolderV4(value: unknown): boolean;
34
+ /** The keys a subagent file adds to `AGENT.md`'s, and the root's in format 4. */
35
+ export declare const SUBAGENT_FILE_KEYS: readonly ["tools", "disallowedTools", "flow", "readonly"];
36
+ /** The root's own narrowing keys in format 4. */
37
+ export declare const ROOT_V4_KEYS: readonly ["tools", "disallowedTools"];
38
+ /**
39
+ * Keys Claude Code and Cursor write that mean nothing here: noted and ignored, so their files drop
40
+ * in. `memory` is one only as a string (Claude Code's `user`, `project` or `local`).
41
+ */
42
+ export declare const HOST_KEYS: readonly ["model", "color", "permissionMode", "hooks", "maxTurns", "background", "isolation", "effort", "initialPrompt", "omitClaudeMd", "autoCompactWindow", "experimental", "is_background", "license"];
43
+ /** Claude Code's tool names that are ours under another spelling. */
44
+ export declare const CLAUDE_TOOL_ALIASES: Readonly<Record<string, string>>;
45
+ /** The Runtime plugins only the root agent uses: a subagent never inherits them, and names their tools only with an error. */
46
+ export declare const ROOT_ONLY_PLUGINS: readonly PluginName[];
47
+ /** Whether the uploaded folder's `AGENT.md` names format 4, read quietly. */
48
+ export declare function folderIsV4(input: CompileAgentFolderInput): boolean;
49
+ /** A subagent file's id from its path, or undefined: `agents/<id>.md`. */
50
+ export declare function subagentFileId(path: string): string | undefined;
51
+ /** The path `flow:` names, from a subagent file in `dir`: `./x` and `x` from `dir`, `/x` from the root. */
52
+ export declare function flowPathOf(value: unknown, dir?: string): string | undefined;
53
+ /**
54
+ * Compiles a format 4 folder: expands its subagent files, compiles the result as `compile` does a
55
+ * format 3 folder, and moves each diagnostic back to the file people wrote.
56
+ */
57
+ export declare function compileFormat4(input: CompileAgentFolderInput, compile: (input: CompileAgentFolderInput) => CompiledAgentFolder): CompiledAgentFolder;
58
+ /** What a subagent file, or a format 4 root, keeps of what it inherits. Undefined: everything. */
59
+ export interface Narrowing {
60
+ /** The HTTP tools it keeps, by name; undefined keeps every one. */
61
+ readonly http?: ReadonlySet<string>;
62
+ /** HTTP tools it drops. */
63
+ readonly httpDenied: ReadonlySet<string>;
64
+ /** The skills it keeps; undefined keeps every one it may have. */
65
+ readonly skills?: readonly {
66
+ readonly name: string;
67
+ readonly index: number;
68
+ }[];
69
+ /** The servers it keeps: `mcpServers`' keys; undefined keeps every one it may have. */
70
+ readonly servers?: readonly string[];
71
+ /** A subagent file driven by a flow (`flow:`). */
72
+ readonly flow: boolean;
73
+ }
74
+ /** What `narrowFront` needs to know. */
75
+ export interface NarrowOptions {
76
+ readonly role: "root" | "subagent";
77
+ /** The HTTP tools this folder (or the expansion's copy of the library) holds. */
78
+ readonly httpTools: readonly string[];
79
+ /** The Runtime plugins to inherit from: the parent's (a subagent's), or the root's own. */
80
+ readonly plugins: readonly {
81
+ readonly name: PluginName;
82
+ readonly tools: readonly string[];
83
+ }[];
84
+ readonly error: (code: string, message: string, path?: readonly PropertyKey[], options?: ReportOptions) => void;
85
+ readonly note: (code: string, message: string, path?: readonly PropertyKey[], options?: ReportOptions) => void;
86
+ }
87
+ /**
88
+ * Reads and strips the format 4 keys of `front` (a subagent file's, or a v4 root's), noting other
89
+ * hosts' keys, and writes the Runtime `plugins:` and `mcpServers.nylorun` its tools leave on, so the
90
+ * format 3 compiler that follows sees a folder it knows. Returns what it keeps of the rest.
91
+ */
92
+ export declare function narrowFront(front: Record<string, unknown>, options: NarrowOptions): Narrowing;
93
+ /** What a subagent file may not inherit unnamed: the root's skills with secrets and servers with a credential. */
94
+ export interface RootSecrets {
95
+ readonly skills: readonly string[];
96
+ readonly servers: readonly string[];
97
+ }
98
+ /** The root's skills with secrets and plugin servers it gives a credential, read from its frontmatter. */
99
+ export declare function rootSecretsOf(front: Record<string, unknown>): RootSecrets;
100
+ /**
101
+ * Keeps, of the HTTP tools, skills and servers a subagent file inherited (or a v4 root holds),
102
+ * what its narrowing allows. A subagent also drops the root's skills with secrets and servers with
103
+ * a credential unless it names them, which then fail the checks that follow.
104
+ */
105
+ export declare function narrowParts(harness: Record<string, unknown>, narrowing: Narrowing, options: {
106
+ readonly role: "root" | "subagent";
107
+ readonly secrets: RootSecrets;
108
+ readonly error: (code: string, message: string, path?: readonly PropertyKey[], options?: ReportOptions) => void;
109
+ }): void;
110
+ export { PLUGIN_NAMES, pluginTools };
@@ -0,0 +1,514 @@
1
+ import { parseMarkdown } from "./frontmatter.js";
2
+ import { PLUGIN_NAMES, pluginOfTool, pluginTools, RUNTIME_SERVER } from "../plugins/index.js";
3
+ import { suggestion } from "./suggest.js";
4
+ import { pluginLabelOf, pluginSubagentFile, pluginSubagentFlow } from "./plugins.js";
5
+ /** The format name a v4 folder writes. */
6
+ export const AGENT_FOLDER_V4 = "agent-folder/4";
7
+ /** Whether `value` names the v4 format: `agent-folder/4` or `4`. */
8
+ export function isFolderV4(value) {
9
+ return value === 4 || value === AGENT_FOLDER_V4;
10
+ }
11
+ /** The keys a subagent file adds to `AGENT.md`'s, and the root's in format 4. */
12
+ export const SUBAGENT_FILE_KEYS = ["tools", "disallowedTools", "flow", "readonly"];
13
+ /** The root's own narrowing keys in format 4. */
14
+ export const ROOT_V4_KEYS = ["tools", "disallowedTools"];
15
+ /**
16
+ * Keys Claude Code and Cursor write that mean nothing here: noted and ignored, so their files drop
17
+ * in. `memory` is one only as a string (Claude Code's `user`, `project` or `local`).
18
+ */
19
+ export const HOST_KEYS = [
20
+ "model",
21
+ "color",
22
+ "permissionMode",
23
+ "hooks",
24
+ "maxTurns",
25
+ "background",
26
+ "isolation",
27
+ "effort",
28
+ "initialPrompt",
29
+ "omitClaudeMd",
30
+ "autoCompactWindow",
31
+ "experimental",
32
+ "is_background",
33
+ "license",
34
+ ];
35
+ /** Claude Code's tool names that are ours under another spelling. */
36
+ export const CLAUDE_TOOL_ALIASES = { Read: "read", Grep: "grep", Glob: "glob", Write: "write", Edit: "edit", Bash: "bash" };
37
+ /** Claude Code's other tool names: noted and ignored. */
38
+ const CLAUDE_TOOLS = ["WebFetch", "WebSearch", "Agent", "Task", "Skill", "TodoWrite", "NotebookEdit", "NotebookRead", "MultiEdit", "LS", "BashOutput", "KillShell", "KillBash", "ExitPlanMode", "SlashCommand", "ListMcpResourcesTool", "ReadMcpResourceTool"];
39
+ /** The Runtime plugins only the root agent uses: a subagent never inherits them, and names their tools only with an error. */
40
+ export const ROOT_ONLY_PLUGINS = ["nylorun-triggers", "nylorun-plan", "nylorun-interactions"];
41
+ /** The Runtime plugin a subagent inherits only when it names its tool. */
42
+ const NAMED_ONLY_PLUGINS = ["nylorun-memory"];
43
+ const READONLY_TOOLS = ["read", "grep", "glob"];
44
+ const AGENT_FILE = "AGENT.md";
45
+ const FLOW_FILE = "flow.json";
46
+ const decoder = new TextDecoder("utf-8", { fatal: true });
47
+ const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
48
+ const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
49
+ function textOf(bytes) {
50
+ if (!bytes)
51
+ return undefined;
52
+ try {
53
+ return decoder.decode(bytes).replace(/^/, "");
54
+ }
55
+ catch {
56
+ return undefined;
57
+ }
58
+ }
59
+ function frontOf(bytes) {
60
+ const text = textOf(bytes);
61
+ if (text === undefined)
62
+ return undefined;
63
+ const result = parseMarkdown(text);
64
+ return result.ok ? result.data : undefined;
65
+ }
66
+ function jsonOf(bytes) {
67
+ const text = textOf(bytes);
68
+ if (text === undefined)
69
+ return undefined;
70
+ try {
71
+ return JSON.parse(text);
72
+ }
73
+ catch {
74
+ return undefined;
75
+ }
76
+ }
77
+ /** Whether the uploaded folder's `AGENT.md` names format 4, read quietly. */
78
+ export function folderIsV4(input) {
79
+ return isFolderV4(frontOf(input.files.get(AGENT_FILE))?.format);
80
+ }
81
+ /** A subagent file's id from its path, or undefined: `agents/<id>.md`. */
82
+ export function subagentFileId(path) {
83
+ const match = /^agents\/([^/]+)\.md$/.exec(path);
84
+ return match?.[1];
85
+ }
86
+ /** The path `flow:` names, from a subagent file in `dir`: `./x` and `x` from `dir`, `/x` from the root. */
87
+ export function flowPathOf(value, dir = "agents") {
88
+ if (typeof value !== "string" || value === "")
89
+ return undefined;
90
+ const target = value.startsWith("/") ? value.slice(1) : `${dir}/${value.replace(/^\.\//, "")}`;
91
+ return target.split("/").some((segment) => segment === "" || segment === "." || segment === "..") ? undefined : target;
92
+ }
93
+ /** The ids a flow's nodes name as `agent` and the names they name as `tool`. */
94
+ function flowNames(tree) {
95
+ const agents = new Set();
96
+ const tools = new Set();
97
+ const walk = (value) => {
98
+ if (Array.isArray(value))
99
+ value.forEach(walk);
100
+ else if (isObject(value)) {
101
+ if (typeof value.agent === "string")
102
+ agents.add(value.agent);
103
+ if (typeof value.tool === "string")
104
+ tools.add(value.tool);
105
+ Object.values(value).forEach(walk);
106
+ }
107
+ };
108
+ walk(tree);
109
+ return { agents, tools };
110
+ }
111
+ /**
112
+ * A plugin's subagent folder: `plugins/<name>/agents/`, with what it holds. Its files stay in the
113
+ * library as the plugin's files (mounted with the plugin), but a subagent's compile never reads them
114
+ * as subagents: no subagent inherits another.
115
+ */
116
+ const PLUGIN_AGENTS = /^plugins\/([^/]+)\/agents\/(.+)$/;
117
+ const isLibrary = (path) => path === "mcp.json" || /^(tools|skills|plugins)\//.test(path);
118
+ /** A name a subagent can go by: the tool the model calls. */
119
+ const SUBAGENT_NAME = /^[A-Za-z0-9_-]{1,64}$/;
120
+ /**
121
+ * Compiles a format 4 folder: expands its subagent files, compiles the result as `compile` does a
122
+ * format 3 folder, and moves each diagnostic back to the file people wrote.
123
+ */
124
+ export function compileFormat4(input, compile) {
125
+ const byPath = new Map(input.entries.map((entry) => [entry.path, entry]));
126
+ const library = input.entries.filter((entry) => isLibrary(entry.path));
127
+ const flowRoot = byPath.has(FLOW_FILE);
128
+ const entries = [];
129
+ const files = new Map(input.files);
130
+ const extra = [];
131
+ /** Where each subagent's flow file is, by name. */
132
+ const flows = new Map();
133
+ // Every subagent file, the folder's own and its plugins', by the name it goes by.
134
+ const sources = new Map();
135
+ const fronts = new Map();
136
+ const labels = new Map();
137
+ const nameOf = (front, fallback) => (typeof front.name === "string" && SUBAGENT_NAME.test(front.name) ? front.name : fallback);
138
+ const take = (source, front) => {
139
+ const other = sources.get(source.name);
140
+ const tool = byPath.has(`tools/${source.name}.json`) ? `tools/${source.name}.json` : undefined;
141
+ if (other || (tool && source.plugin)) {
142
+ const where = other?.path ?? tool;
143
+ extra.push(source.plugin || other?.plugin
144
+ ? { severity: "error", code: "plugin_duplicate", message: `${source.name} is also ${where}: an agent has one subagent or tool of a name`, file: source.path }
145
+ : { severity: "error", code: "tool_twice", message: `${other.path} and ${source.path} both name the subagent ${source.name}: rename one`, file: source.path });
146
+ return;
147
+ }
148
+ sources.set(source.name, source);
149
+ fronts.set(source.name, front);
150
+ if (source.plugin)
151
+ labels.set(source.name, source.plugin);
152
+ };
153
+ for (const entry of [...input.entries].sort((a, b) => compare(a.path, b.path))) {
154
+ const id = subagentFileId(entry.path);
155
+ if (id === undefined)
156
+ continue;
157
+ const front = frontOf(input.files.get(entry.path)) ?? {};
158
+ take({ name: nameOf(front, id), path: entry.path }, front);
159
+ }
160
+ const pluginLabels = new Map();
161
+ for (const entry of [...input.entries].sort((a, b) => compare(a.path, b.path))) {
162
+ const match = PLUGIN_AGENTS.exec(entry.path);
163
+ if (!match)
164
+ continue;
165
+ const [, plugin, rest] = match;
166
+ const file = pluginSubagentFile(`agents/${rest}`);
167
+ // A flow file a subagent's `flow:` names is read from where it is when it is named.
168
+ if (file === undefined && pluginSubagentFlow(`agents/${rest}`))
169
+ continue;
170
+ if (file === undefined) {
171
+ extra.push({ severity: "note", code: "plugin_part_ignored", message: "Kept with the source: a plugin's subagents are its agents/*.md files", file: entry.path });
172
+ continue;
173
+ }
174
+ if (!pluginLabels.has(plugin))
175
+ pluginLabels.set(plugin, pluginLabelOf(jsonOf(input.files.get(`plugins/${plugin}/plugin.json`))));
176
+ // A plugin whose plugin.json names it nothing is refused by the plugin's own checks.
177
+ const label = pluginLabels.get(plugin);
178
+ if (!label)
179
+ continue;
180
+ const front = frontOf(input.files.get(entry.path)) ?? {};
181
+ take({ name: nameOf(front, file), path: entry.path, plugin: label }, front);
182
+ }
183
+ const names = [...sources.keys()].sort(compare);
184
+ // A folder under agents/ is format 3's: refused, and left out of the expansion. A plugin's
185
+ // subagent files are read above as subagents, and stay in the entries as the plugin's files.
186
+ const folders = new Set();
187
+ for (const entry of input.entries) {
188
+ const segments = entry.path.split("/");
189
+ if (segments[0] === "agents" && segments.length >= 3) {
190
+ if (!folders.has(segments[1]))
191
+ extra.push({
192
+ severity: "error",
193
+ code: "subagent_folder",
194
+ message: `agents/${segments[1]}/ is a subagent folder, which agent-folder/4 writes as one file, agents/${segments[1]}.md, drawing on the manifest's tools/, skills/ and mcp.json. Fix: run nylorun migrate folder <dir> --to 4`,
195
+ file: `agents/${segments[1]}/${byPath.has(`agents/${segments[1]}/AGENT.md`) ? AGENT_FILE : segments.slice(2).join("/")}`,
196
+ });
197
+ folders.add(segments[1]);
198
+ continue;
199
+ }
200
+ entries.push(entry);
201
+ }
202
+ const add = (path, source) => {
203
+ if (entries.some((entry) => entry.path === path))
204
+ return;
205
+ entries.push({ path, size: source.size, sha256: source.sha256 });
206
+ const bytes = input.files.get(source.path);
207
+ if (bytes)
208
+ files.set(path, bytes);
209
+ };
210
+ /** Expands one subagent file at `prefix` (`agents/<name>/`); `namespace` is a flow subagent's own agents/ under a root driven by a model loop. */
211
+ const expand = (prefix, name, namespace) => {
212
+ const written = sources.get(name);
213
+ const source = written && byPath.get(written.path);
214
+ if (!written || !source)
215
+ return;
216
+ add(`${prefix}${AGENT_FILE}`, source);
217
+ const front = fronts.get(name) ?? {};
218
+ if (front.flow === undefined) {
219
+ for (const entry of library)
220
+ add(prefix + entry.path, entry);
221
+ return;
222
+ }
223
+ const flowPath = flowPathOf(front.flow, written.path.slice(0, written.path.lastIndexOf("/")));
224
+ const flowEntry = flowPath === undefined ? undefined : byPath.get(flowPath);
225
+ if (!flowEntry || !flowPath)
226
+ return;
227
+ flows.set(name, flowPath);
228
+ add(`${prefix}${FLOW_FILE}`, flowEntry);
229
+ const names = flowNames(jsonOf(input.files.get(flowPath)));
230
+ for (const tool of names.tools) {
231
+ const entry = byPath.get(`tools/${tool}.json`);
232
+ if (entry)
233
+ add(`${prefix}tools/${tool}.json`, entry);
234
+ }
235
+ // Its stages: the root's, when the root is a flow; else its own agents/, from the sibling files.
236
+ if (namespace === undefined)
237
+ return;
238
+ for (const stage of names.agents)
239
+ if (fronts.has(stage) && !entries.some((entry) => entry.path === `${namespace}${stage}/${AGENT_FILE}`))
240
+ expand(`${namespace}${stage}/`, stage, namespace);
241
+ };
242
+ for (const name of names) {
243
+ const front = fronts.get(name) ?? {};
244
+ const prefix = `agents/${name}/`;
245
+ expand(prefix, name, front.flow !== undefined && !flowRoot ? `${prefix}agents/` : undefined);
246
+ }
247
+ const compiled = compile({ ...input, entries, files });
248
+ /** Where a subagent's file was written, by name. */
249
+ const writtenAt = (name) => sources.get(name)?.path ?? `agents/${name}.md`;
250
+ /** A path of the expansion, as the folder people wrote names it. */
251
+ const real = (path) => {
252
+ let rest = path;
253
+ let id;
254
+ for (;;) {
255
+ const match = /^agents\/([^/]+)\/(.*)$/.exec(rest);
256
+ if (!match || !fronts.has(match[1]))
257
+ break;
258
+ id = match[1];
259
+ rest = match[2];
260
+ }
261
+ if (id === undefined) {
262
+ const bare = /^agents\/([^/]+)$/.exec(path)?.[1];
263
+ return bare !== undefined && fronts.has(bare) ? writtenAt(bare) : path;
264
+ }
265
+ if (rest === AGENT_FILE || rest === "")
266
+ return writtenAt(id);
267
+ if (rest === FLOW_FILE)
268
+ return flows.get(id) ?? writtenAt(id);
269
+ return rest;
270
+ };
271
+ const seen = new Set();
272
+ const diagnostics = [];
273
+ for (const item of [...extra, ...compiled.diagnostics]) {
274
+ const moved = {
275
+ ...item,
276
+ ...(item.file === undefined ? {} : { file: real(item.file) }),
277
+ ...(item.fix === undefined ? {} : { fix: { ...item.fix, file: real(item.fix.file) } }),
278
+ };
279
+ const key = JSON.stringify([moved.severity, moved.code, moved.message, moved.file, moved.line, moved.column]);
280
+ if (seen.has(key))
281
+ continue;
282
+ seen.add(key);
283
+ diagnostics.push(moved);
284
+ }
285
+ const order = { error: 0, warning: 1, info: 2, note: 3 };
286
+ diagnostics.sort((a, b) => order[a.severity] - order[b.severity] || compare(a.file ?? "", b.file ?? "") || (a.line ?? 0) - (b.line ?? 0) || (a.column ?? 0) - (b.column ?? 0));
287
+ const failed = diagnostics.some((item) => item.severity === "error");
288
+ return { ...(failed || !compiled.manifest ? {} : { manifest: labelled(compiled.manifest, labels) }), diagnostics, compiler: AGENT_FOLDER_V4 };
289
+ }
290
+ /** The manifest with each plugin subagent's tool labelled with its plugin, as a plugin's skills and servers are. */
291
+ function labelled(manifest, labels) {
292
+ const harness = manifest?.harness;
293
+ if (labels.size === 0 || !harness || !Array.isArray(harness.tools))
294
+ return manifest;
295
+ const tools = harness.tools.map((tool) => {
296
+ const label = typeof tool.name === "string" && tool.agent !== undefined ? labels.get(tool.name) : undefined;
297
+ return label ? { ...tool, plugin: label } : tool;
298
+ });
299
+ return { ...manifest, harness: { ...harness, tools } };
300
+ }
301
+ /** A tool list as written: a YAML list, or a comma-separated string as Claude Code writes it. */
302
+ function namesOf(value) {
303
+ if (typeof value === "string")
304
+ return value
305
+ .split(",")
306
+ .map((name) => name.trim())
307
+ .filter((name) => name !== "")
308
+ .map((name) => ({ name, index: -1 }));
309
+ if (Array.isArray(value) && value.every((item) => typeof item === "string"))
310
+ return value.map((name, index) => ({ name: name.trim(), index }));
311
+ return undefined;
312
+ }
313
+ /**
314
+ * Reads and strips the format 4 keys of `front` (a subagent file's, or a v4 root's), noting other
315
+ * hosts' keys, and writes the Runtime `plugins:` and `mcpServers.nylorun` its tools leave on, so the
316
+ * format 3 compiler that follows sees a folder it knows. Returns what it keeps of the rest.
317
+ */
318
+ export function narrowFront(front, options) {
319
+ const { role, error, note } = options;
320
+ const subagent = role === "subagent";
321
+ if (subagent) {
322
+ for (const key of HOST_KEYS)
323
+ if (front[key] !== undefined) {
324
+ note("host_key", `${key} is a key other hosts write (Claude Code, Cursor), with no meaning here: ignored`, [key], { key: true });
325
+ delete front[key];
326
+ }
327
+ if (typeof front.memory === "string") {
328
+ note("host_key", "memory as a string is Claude Code's memory directory, with no meaning here: ignored. A subagent may view the Agent's memories when the root has them", ["memory"], { key: true });
329
+ delete front.memory;
330
+ }
331
+ }
332
+ const flow = subagent && front.flow !== undefined;
333
+ if (flow && typeof front.flow !== "string")
334
+ error("value_invalid", "flow is the path of the subagent's flow file, such as ./research.flow.json", ["flow"]);
335
+ if (flow)
336
+ for (const key of ["tools", "disallowedTools", "skills", "mcpServers", "readonly", "plugins"])
337
+ if (front[key] !== undefined)
338
+ error("flow_refused", `${key} belongs to a model loop: a subagent driven by a flow has none of its own, and its stages each have theirs`, [key], { key: true });
339
+ delete front.flow;
340
+ if (flow) {
341
+ for (const key of ["tools", "disallowedTools", "skills", "mcpServers", "readonly", "plugins"])
342
+ delete front[key];
343
+ return { httpDenied: new Set(), flow: true };
344
+ }
345
+ // The servers it names, before the Runtime's own (`nylorun`) is touched below: its allowlist.
346
+ const servers = subagent && isObject(front.mcpServers) ? Object.keys(front.mcpServers).filter((key) => key !== RUNTIME_SERVER) : undefined;
347
+ // tools and disallowedTools: HTTP tools and the Runtime plugins' tools, by name.
348
+ const known = [...new Set([...options.httpTools, ...options.plugins.flatMap((plugin) => plugin.tools)])];
349
+ const read = (key) => {
350
+ const value = front[key];
351
+ delete front[key];
352
+ if (value === undefined)
353
+ return undefined;
354
+ const names = namesOf(value);
355
+ if (!names) {
356
+ error("value_invalid", `${key} is a list of tool names, such as [read, grep, glob]`, [key]);
357
+ return [];
358
+ }
359
+ const kept = [];
360
+ for (const { name, index } of names) {
361
+ const at = index < 0 ? [key] : [key, index];
362
+ const alias = CLAUDE_TOOL_ALIASES[name];
363
+ if (alias !== undefined) {
364
+ note("tool_alias", `${name} is Claude Code's name for ${alias}: read as ${alias}. Fix: write ${alias}`, at);
365
+ if (known.includes(alias))
366
+ kept.push(alias);
367
+ else
368
+ error("library_unknown", `${alias} is not a tool this agent can have: its library holds ${known.join(", ") || "none"}`, at);
369
+ continue;
370
+ }
371
+ if (CLAUDE_TOOLS.includes(name) || name.startsWith("mcp__")) {
372
+ note("host_key", `${name} is Claude Code's tool name, with no meaning here: ignored${name.startsWith("mcp__") ? ". An MCP server's tools are narrowed with mcpServers" : ""}`, at);
373
+ continue;
374
+ }
375
+ if (!known.includes(name)) {
376
+ const close = suggestion(name, known);
377
+ error("library_unknown", `No tool ${name} is in the manifest's library (tools/ and the Runtime plugins it declares).${close ? ` Did you mean '${close}'?` : ""}`, at);
378
+ continue;
379
+ }
380
+ const plugin = pluginOfTool(name);
381
+ if (subagent && plugin && ROOT_ONLY_PLUGINS.includes(plugin)) {
382
+ error("library_unknown", `${name} is the root agent's (${plugin}): a subagent can't pause, set triggers or plan for the whole turn`, at);
383
+ continue;
384
+ }
385
+ kept.push(name);
386
+ }
387
+ return kept;
388
+ };
389
+ let allowed = read("tools");
390
+ const denied = new Set(read("disallowedTools") ?? []);
391
+ if (subagent && front.readonly !== undefined) {
392
+ if (typeof front.readonly !== "boolean")
393
+ error("value_invalid", "readonly is true or false", ["readonly"]);
394
+ else if (front.readonly && allowed === undefined) {
395
+ note("host_key", "readonly: true is Cursor's: read as tools: [read, grep, glob]", ["readonly"], { key: true });
396
+ allowed = READONLY_TOOLS.filter((tool) => known.includes(tool));
397
+ }
398
+ else if (front.readonly)
399
+ note("host_key", "readonly: true is Cursor's, and tools already lists what this subagent gets: ignored", ["readonly"], { key: true });
400
+ }
401
+ delete front.readonly;
402
+ const keep = (tool) => (allowed === undefined || allowed.includes(tool)) && !denied.has(tool);
403
+ // The Runtime plugins: a subagent inherits its parent's, but the root's only, and memory only
404
+ // when it names it; the root keeps its own. Either keeps only the tools it allows.
405
+ if (subagent && front.plugins !== undefined)
406
+ error("value_invalid", "plugins is the root agent's: a subagent file gets the root's Runtime plugins, narrowed with tools", ["plugins"], { key: true });
407
+ const plugins = subagent
408
+ ? options.plugins.filter((plugin) => !ROOT_ONLY_PLUGINS.includes(plugin.name) &&
409
+ (!NAMED_ONLY_PLUGINS.includes(plugin.name) || (allowed !== undefined && plugin.tools.some((tool) => allowed.includes(tool)))))
410
+ : options.plugins;
411
+ const on = plugins.filter((plugin) => plugin.tools.some(keep));
412
+ if (subagent) {
413
+ if (on.length > 0)
414
+ front.plugins = on.map((plugin) => plugin.name);
415
+ else
416
+ delete front.plugins;
417
+ }
418
+ const off = plugins.flatMap((plugin) => plugin.tools.filter((tool) => !keep(tool)));
419
+ if (off.length > 0) {
420
+ const servers = isObject(front.mcpServers) ? front.mcpServers : (front.mcpServers = {});
421
+ const settings = isObject(servers[RUNTIME_SERVER]) ? servers[RUNTIME_SERVER] : (servers[RUNTIME_SERVER] = {});
422
+ const tools = isObject(settings.tools) ? settings.tools : (settings.tools = {});
423
+ for (const tool of off)
424
+ if (on.some((plugin) => plugin.tools.includes(tool)) || !subagent)
425
+ tools[tool] = { enabled: false };
426
+ if (Object.keys(tools).length === 0)
427
+ delete servers[RUNTIME_SERVER];
428
+ if (Object.keys(servers).length === 0)
429
+ delete front.mcpServers;
430
+ }
431
+ const http = allowed === undefined ? undefined : new Set(allowed.filter((tool) => options.httpTools.includes(tool)));
432
+ const httpDenied = new Set([...denied].filter((tool) => options.httpTools.includes(tool)));
433
+ // skills: a list in a subagent file; the root's stays the map that gives skills secrets.
434
+ let skills;
435
+ if (subagent && front.skills !== undefined) {
436
+ const names = namesOf(front.skills);
437
+ if (!names)
438
+ error("value_invalid", "skills in a subagent file lists the library's skills it gets, by name: [checklist]. Only the root agent's skills get secrets", ["skills"]);
439
+ skills = names ?? [];
440
+ delete front.skills;
441
+ }
442
+ return {
443
+ ...(http === undefined ? {} : { http }),
444
+ httpDenied,
445
+ ...(skills === undefined ? {} : { skills }),
446
+ ...(servers === undefined ? {} : { servers }),
447
+ flow: false,
448
+ };
449
+ }
450
+ /** The root's skills with secrets and plugin servers it gives a credential, read from its frontmatter. */
451
+ export function rootSecretsOf(front) {
452
+ const skills = isObject(front.skills)
453
+ ? Object.entries(front.skills)
454
+ .filter(([, value]) => isObject(value) && Array.isArray(value.secrets) && value.secrets.length > 0)
455
+ .map(([key]) => key)
456
+ : [];
457
+ const servers = isObject(front.mcpServers)
458
+ ? Object.entries(front.mcpServers)
459
+ .filter(([, value]) => isObject(value) && value.credential !== undefined)
460
+ .map(([key]) => key)
461
+ : [];
462
+ return { skills, servers };
463
+ }
464
+ /**
465
+ * Keeps, of the HTTP tools, skills and servers a subagent file inherited (or a v4 root holds),
466
+ * what its narrowing allows. A subagent also drops the root's skills with secrets and servers with
467
+ * a credential unless it names them, which then fail the checks that follow.
468
+ */
469
+ export function narrowParts(harness, narrowing, options) {
470
+ if (narrowing.flow)
471
+ return;
472
+ if (Array.isArray(harness.tools)) {
473
+ const tools = harness.tools.filter((tool) => {
474
+ const name = String(tool.name);
475
+ if (tool.agent !== undefined)
476
+ return true;
477
+ return (narrowing.http === undefined || narrowing.http.has(name)) && !narrowing.httpDenied.has(name);
478
+ });
479
+ if (tools.length > 0)
480
+ harness.tools = tools;
481
+ else
482
+ delete harness.tools;
483
+ }
484
+ if (options.role !== "subagent")
485
+ return;
486
+ const skills = isObject(harness.skills) ? harness.skills : {};
487
+ if (narrowing.skills)
488
+ for (const { name, index } of narrowing.skills)
489
+ if (!(name in skills)) {
490
+ const close = suggestion(name, Object.keys(skills));
491
+ options.error("library_unknown", `No skill ${name} is in the manifest's library (skills/ and its plugins).${close ? ` Did you mean '${close}'?` : ""}`, ["skills", index]);
492
+ }
493
+ else if (options.secrets.skills.includes(name))
494
+ options.error("credential_unsupported", `Only the root agent's skills get secrets: ${name} gets them from the root's AGENT.md, so a subagent can't use it`, ["skills", index]);
495
+ for (const key of Object.keys(skills)) {
496
+ const named = narrowing.skills?.some((item) => item.name === key);
497
+ if ((narrowing.skills !== undefined && !named) || (!named && options.secrets.skills.includes(key)))
498
+ delete skills[key];
499
+ }
500
+ if (isObject(harness.skills) && Object.keys(skills).length === 0)
501
+ delete harness.skills;
502
+ const servers = isObject(harness.mcpServers) ? harness.mcpServers : {};
503
+ for (const [key, server] of Object.entries(servers)) {
504
+ const named = narrowing.servers?.includes(key) === true;
505
+ const secret = (isObject(server) && server.credential !== undefined) || options.secrets.servers.includes(key);
506
+ if (named && options.secrets.servers.includes(key))
507
+ options.error("credential_unsupported", `${key} authenticates with a credential the root's AGENT.md gives it, and a subagent's MCP servers take no credential: give the server to the root agent`, ["mcpServers", key]);
508
+ if ((narrowing.servers !== undefined && !named) || (!named && secret))
509
+ delete servers[key];
510
+ }
511
+ if (isObject(harness.mcpServers) && Object.keys(servers).length === 0)
512
+ delete harness.mcpServers;
513
+ }
514
+ export { PLUGIN_NAMES, pluginTools };
@@ -1,5 +1,5 @@
1
1
  /**
2
- * `agent-folder/3` (ADR-106): a folder declares the Runtime's features as plugins, by name, and
2
+ * `agent-folder/3`: a folder declares the Runtime's features as plugins, by name, and
3
3
  * narrows their tools with the settings any MCP server takes:
4
4
  *
5
5
  * ```yaml
@@ -26,7 +26,7 @@ export declare const AGENT_FOLDER_V3 = "agent-folder/3";
26
26
  export declare const V3_REFUSED_KEYS: readonly ["memory"];
27
27
  /**
28
28
  * The `loop` keys a v3 folder declares as plugins instead. `loop` itself stays the harness's own
29
- * settings, such as `compaction` (ADR-157).
29
+ * settings, such as `compaction`.
30
30
  */
31
31
  export declare const V3_REFUSED_LOOP_KEYS: readonly ["plan", "connections", "ask"];
32
32
  /** The keys of `engine:` a v3 folder declares as plugins instead. */
package/dist/folder/v3.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * `agent-folder/3` (ADR-106): a folder declares the Runtime's features as plugins, by name, and
2
+ * `agent-folder/3`: a folder declares the Runtime's features as plugins, by name, and
3
3
  * narrows their tools with the settings any MCP server takes:
4
4
  *
5
5
  * ```yaml
@@ -26,7 +26,7 @@ export const AGENT_FOLDER_V3 = "agent-folder/3";
26
26
  export const V3_REFUSED_KEYS = ["memory"];
27
27
  /**
28
28
  * The `loop` keys a v3 folder declares as plugins instead. `loop` itself stays the harness's own
29
- * settings, such as `compaction` (ADR-157).
29
+ * settings, such as `compaction`.
30
30
  */
31
31
  export const V3_REFUSED_LOOP_KEYS = ["plan", "connections", "ask"];
32
32
  /** The keys of `engine:` a v3 folder declares as plugins instead. */
@@ -44,7 +44,7 @@ export interface ArtifactWriteOptions {
44
44
  readonly effectId?: string;
45
45
  /** The body's size when known: core refuses one past its limits before a byte is sent. */
46
46
  readonly size?: number;
47
- /** `model`: a new artifact stored only for the model to see, never an output (ADR-139). */
47
+ /** `model`: a new artifact stored only for the model to see, never an output. */
48
48
  readonly audience?: "model";
49
49
  /** The most bytes one chunk carries: the run's `turn.start.limits.chunkBytes`. */
50
50
  readonly chunkBytes: number;