vigiles 2.4.0 → 2.6.0

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.
@@ -0,0 +1,64 @@
1
+ /**
2
+ * vigiles — Agent runtime: the PreToolUse tool-contract rail.
3
+ *
4
+ * A subagent declares an allowed-tools contract in its frontmatter (`tools:`).
5
+ * But that field is documentation, not a hard runtime boundary (Claude Code
6
+ * issue #54898): permissions are session-wide, a subagent inherits the parent
7
+ * session's grants, and `tools:` only filters what's *offered* — it can't deny
8
+ * what the session allows. The deterministic layer that actually closes the gap
9
+ * is a **PreToolUse hook** that blocks any tool the active agent's contract
10
+ * doesn't list.
11
+ *
12
+ * This is the same emit-a-hook pattern the skill runtime already ships
13
+ * (`src/skill-runtime.ts`): there a `Stop` hook reads the active skill's
14
+ * compiled SKILL.md and runs its result gate; here a `PreToolUse` hook reads
15
+ * the active agent's compiled `.md`, parses its `tools:` allowlist, and
16
+ * allows/denies the tool call. The compiled markdown's frontmatter is the
17
+ * single source of truth — the same list that documents intent IS the list the
18
+ * hook enforces, so the two agree by construction (see `enforcedTools`).
19
+ *
20
+ * Which agent is active is recorded in `.vigiles/active-agent.json` — Claude
21
+ * Code hooks don't surface the dispatched subagent, so vigiles records it
22
+ * (mirrors `.vigiles/active-skill.json`). The decision logic below is
23
+ * harness-agnostic and fully testable.
24
+ */
25
+ /**
26
+ * Parse an agent's allowed-tools contract from its compiled markdown.
27
+ *
28
+ * Returns the list of allowed tool names, or `null` when the agent declares no
29
+ * `tools:` line at all — which in Claude Code means it inherits EVERY tool (the
30
+ * #1 footgun). `null` is the "no restriction" signal the decision logic honors;
31
+ * an empty list (`tools:` with nothing after it) means "no tools allowed".
32
+ */
33
+ export declare function parseAgentTools(markdown: string): string[] | null;
34
+ export interface PreToolDecision {
35
+ /** Whether the tool call is allowed (true) or blocked (false). */
36
+ readonly allow: boolean;
37
+ /** Message fed back to the model on a block; empty on allow. */
38
+ readonly message: string;
39
+ }
40
+ /**
41
+ * Decide whether `tool` is allowed under an agent's tool contract. Pure, so the
42
+ * rail is unit-testable without spawning anything.
43
+ *
44
+ * - `allowed === null` → the agent declared no `tools:` line, so it inherits
45
+ * everything and the rail imposes no restriction (allow).
46
+ * - otherwise → allow iff the tool is in the allowlist; deny anything else,
47
+ * feeding the contract back to the model so it self-corrects.
48
+ */
49
+ export declare function decidePreToolUse(allowed: readonly string[] | null, tool: string): PreToolDecision;
50
+ /** Record the subagent currently dispatched, so PreToolUse enforces its contract. */
51
+ export declare function setActiveAgent(cwd: string, agentPath: string): void;
52
+ /** Clear the active-agent marker (the subagent finished). */
53
+ export declare function clearActiveAgent(cwd: string): void;
54
+ /** The path of the active agent's compiled `.md`, or null when none is active. */
55
+ export declare function readActiveAgent(cwd: string): string | null;
56
+ /**
57
+ * PreToolUse-hook decision. If an agent is active, parse its compiled `.md`
58
+ * tool contract and allow the call only when the tool is in the allowlist;
59
+ * otherwise block and tell the model which tools it may use. With no active
60
+ * agent (or an agent that inherits all tools), always allow — the rail only
61
+ * constrains agents that declared a contract.
62
+ */
63
+ export declare function evaluatePreToolUse(cwd: string, tool: string): PreToolDecision;
64
+ //# sourceMappingURL=agent-runtime.d.ts.map
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ /**
3
+ * vigiles — Agent runtime: the PreToolUse tool-contract rail.
4
+ *
5
+ * A subagent declares an allowed-tools contract in its frontmatter (`tools:`).
6
+ * But that field is documentation, not a hard runtime boundary (Claude Code
7
+ * issue #54898): permissions are session-wide, a subagent inherits the parent
8
+ * session's grants, and `tools:` only filters what's *offered* — it can't deny
9
+ * what the session allows. The deterministic layer that actually closes the gap
10
+ * is a **PreToolUse hook** that blocks any tool the active agent's contract
11
+ * doesn't list.
12
+ *
13
+ * This is the same emit-a-hook pattern the skill runtime already ships
14
+ * (`src/skill-runtime.ts`): there a `Stop` hook reads the active skill's
15
+ * compiled SKILL.md and runs its result gate; here a `PreToolUse` hook reads
16
+ * the active agent's compiled `.md`, parses its `tools:` allowlist, and
17
+ * allows/denies the tool call. The compiled markdown's frontmatter is the
18
+ * single source of truth — the same list that documents intent IS the list the
19
+ * hook enforces, so the two agree by construction (see `enforcedTools`).
20
+ *
21
+ * Which agent is active is recorded in `.vigiles/active-agent.json` — Claude
22
+ * Code hooks don't surface the dispatched subagent, so vigiles records it
23
+ * (mirrors `.vigiles/active-skill.json`). The decision logic below is
24
+ * harness-agnostic and fully testable.
25
+ */
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.parseAgentTools = parseAgentTools;
28
+ exports.decidePreToolUse = decidePreToolUse;
29
+ exports.setActiveAgent = setActiveAgent;
30
+ exports.clearActiveAgent = clearActiveAgent;
31
+ exports.readActiveAgent = readActiveAgent;
32
+ exports.evaluatePreToolUse = evaluatePreToolUse;
33
+ const node_fs_1 = require("node:fs");
34
+ const node_path_1 = require("node:path");
35
+ // ---------------------------------------------------------------------------
36
+ // Parse the tool contract from a compiled agent .md
37
+ // ---------------------------------------------------------------------------
38
+ /** Extract the YAML frontmatter block (between the first pair of `---` fences). */
39
+ function extractFrontmatter(markdown) {
40
+ const lines = markdown.split("\n");
41
+ let start = -1;
42
+ for (let i = 0; i < lines.length; i++) {
43
+ if (lines[i].trim() === "---") {
44
+ start = i;
45
+ break;
46
+ }
47
+ }
48
+ if (start === -1)
49
+ return null;
50
+ for (let i = start + 1; i < lines.length; i++) {
51
+ if (lines[i].trim() === "---") {
52
+ return lines.slice(start + 1, i).join("\n");
53
+ }
54
+ }
55
+ return null;
56
+ }
57
+ /**
58
+ * Parse an agent's allowed-tools contract from its compiled markdown.
59
+ *
60
+ * Returns the list of allowed tool names, or `null` when the agent declares no
61
+ * `tools:` line at all — which in Claude Code means it inherits EVERY tool (the
62
+ * #1 footgun). `null` is the "no restriction" signal the decision logic honors;
63
+ * an empty list (`tools:` with nothing after it) means "no tools allowed".
64
+ */
65
+ function parseAgentTools(markdown) {
66
+ const fm = extractFrontmatter(markdown);
67
+ if (fm === null)
68
+ return null;
69
+ const match = /^tools:[ \t]*(.*)$/m.exec(fm);
70
+ if (!match)
71
+ return null;
72
+ return match[1]
73
+ .split(",")
74
+ .map((t) => t.trim())
75
+ .filter((t) => t.length > 0);
76
+ }
77
+ /**
78
+ * Decide whether `tool` is allowed under an agent's tool contract. Pure, so the
79
+ * rail is unit-testable without spawning anything.
80
+ *
81
+ * - `allowed === null` → the agent declared no `tools:` line, so it inherits
82
+ * everything and the rail imposes no restriction (allow).
83
+ * - otherwise → allow iff the tool is in the allowlist; deny anything else,
84
+ * feeding the contract back to the model so it self-corrects.
85
+ */
86
+ function decidePreToolUse(allowed, tool) {
87
+ if (allowed === null)
88
+ return { allow: true, message: "" };
89
+ if (allowed.includes(tool))
90
+ return { allow: true, message: "" };
91
+ const list = allowed.length > 0 ? allowed.join(", ") : "(none)";
92
+ return {
93
+ allow: false,
94
+ message: `Tool "${tool}" is not in this subagent's allowed-tools contract ` +
95
+ `(${list}). Use only the listed tools, or widen the agent's \`tools\`.`,
96
+ };
97
+ }
98
+ // ---------------------------------------------------------------------------
99
+ // Active-agent tracking (mirrors .vigiles/active-skill.json)
100
+ // ---------------------------------------------------------------------------
101
+ const ACTIVE_PATH = ".vigiles/active-agent.json";
102
+ /** Record the subagent currently dispatched, so PreToolUse enforces its contract. */
103
+ function setActiveAgent(cwd, agentPath) {
104
+ const p = (0, node_path_1.resolve)(cwd, ACTIVE_PATH);
105
+ (0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(p), { recursive: true });
106
+ (0, node_fs_1.writeFileSync)(p, JSON.stringify({ agent: agentPath }) + "\n");
107
+ }
108
+ /** Clear the active-agent marker (the subagent finished). */
109
+ function clearActiveAgent(cwd) {
110
+ const p = (0, node_path_1.resolve)(cwd, ACTIVE_PATH);
111
+ if ((0, node_fs_1.existsSync)(p))
112
+ (0, node_fs_1.rmSync)(p);
113
+ }
114
+ /** The path of the active agent's compiled `.md`, or null when none is active. */
115
+ function readActiveAgent(cwd) {
116
+ const p = (0, node_path_1.resolve)(cwd, ACTIVE_PATH);
117
+ if (!(0, node_fs_1.existsSync)(p))
118
+ return null;
119
+ try {
120
+ const parsed = JSON.parse((0, node_fs_1.readFileSync)(p, "utf-8"));
121
+ return typeof parsed.agent === "string" ? parsed.agent : null;
122
+ }
123
+ catch {
124
+ return null;
125
+ }
126
+ }
127
+ // ---------------------------------------------------------------------------
128
+ // PreToolUse-hook decision
129
+ // ---------------------------------------------------------------------------
130
+ /**
131
+ * PreToolUse-hook decision. If an agent is active, parse its compiled `.md`
132
+ * tool contract and allow the call only when the tool is in the allowlist;
133
+ * otherwise block and tell the model which tools it may use. With no active
134
+ * agent (or an agent that inherits all tools), always allow — the rail only
135
+ * constrains agents that declared a contract.
136
+ */
137
+ function evaluatePreToolUse(cwd, tool) {
138
+ const agentPath = readActiveAgent(cwd);
139
+ if (!agentPath)
140
+ return { allow: true, message: "" };
141
+ const full = (0, node_path_1.resolve)(cwd, agentPath);
142
+ if (!(0, node_fs_1.existsSync)(full))
143
+ return { allow: true, message: "" };
144
+ const allowed = parseAgentTools((0, node_fs_1.readFileSync)(full, "utf-8"));
145
+ return decidePreToolUse(allowed, tool);
146
+ }
147
+ //# sourceMappingURL=agent-runtime.js.map
@@ -0,0 +1,10 @@
1
+ /**
2
+ * `vigiles/claude-code` — the Claude Code-specific harness pieces a *different*
3
+ * harness would swap out: the plugin/repo loader (reads real Claude Code plugin
4
+ * layouts) and the scriptable Anthropic Messages mock. Split from
5
+ * `vigiles/testing` on purpose — the test API above is the stable surface; this
6
+ * is the adapter, so a future `vigiles/<other-harness>` can sit beside it.
7
+ */
8
+ export * from "./plugin-loader.js";
9
+ export * from "./mock-model.js";
10
+ //# sourceMappingURL=claude-code.d.ts.map
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /**
18
+ * `vigiles/claude-code` — the Claude Code-specific harness pieces a *different*
19
+ * harness would swap out: the plugin/repo loader (reads real Claude Code plugin
20
+ * layouts) and the scriptable Anthropic Messages mock. Split from
21
+ * `vigiles/testing` on purpose — the test API above is the stable surface; this
22
+ * is the adapter, so a future `vigiles/<other-harness>` can sit beside it.
23
+ */
24
+ __exportStar(require("./plugin-loader.js"), exports);
25
+ __exportStar(require("./mock-model.js"), exports);
26
+ //# sourceMappingURL=claude-code.js.map
package/dist/cli.js CHANGED
@@ -23,6 +23,7 @@ const frontmatter_js_1 = require("./frontmatter.js");
23
23
  const generate_schema_js_1 = require("./generate-schema.js");
24
24
  const compile_generator_js_1 = require("./compile-generator.js");
25
25
  const action_gate_js_1 = require("./action-gate.js");
26
+ const agent_runtime_js_1 = require("./agent-runtime.js");
26
27
  const refs_js_1 = require("./refs.js");
27
28
  const mcp_js_1 = require("./mcp.js");
28
29
  const skill_runtime_js_1 = require("./skill-runtime.js");
@@ -177,8 +178,57 @@ function compileSkillToFile(spec, specPath) {
177
178
  printErrors(specPath, errors);
178
179
  return false;
179
180
  }
181
+ /** Compile a subagent spec → agents/<name>.md (with its result-contract section). */
182
+ function compileAgentToFile(spec, specPath) {
183
+ const outputPath = specPath.replace(/\.spec\.ts$/, "");
184
+ const { markdown, errors } = (0, compile_js_1.compileAgent)(spec, {
185
+ basePath: process.cwd(),
186
+ specFile: specPath,
187
+ });
188
+ (0, node_fs_1.writeFileSync)((0, node_path_1.resolve)(process.cwd(), outputPath), markdown);
189
+ if (errors.length === 0) {
190
+ console.log(`\n✓ ${specPath} → ${outputPath}`);
191
+ return true;
192
+ }
193
+ console.log(`\n✗ ${specPath} — ${String(errors.length)} error(s)`);
194
+ printErrors(specPath, errors);
195
+ return false;
196
+ }
197
+ /**
198
+ * Compile a railway spec → the orchestrator command markdown. `knownAgents` is
199
+ * the set of compiled agent names in the project, so every `delegate()` target
200
+ * is resolved at compile time (an unknown target is a stale-ref error).
201
+ */
202
+ function compileRailwayToFile(spec, specPath, knownAgents) {
203
+ const outputPath = specPath.replace(/\.spec\.ts$/, "");
204
+ const { markdown, errors } = (0, compile_js_1.compileRailway)(spec, {
205
+ specFile: specPath,
206
+ knownAgents,
207
+ });
208
+ (0, node_fs_1.writeFileSync)((0, node_path_1.resolve)(process.cwd(), outputPath), markdown);
209
+ if (errors.length === 0) {
210
+ console.log(`\n✓ ${specPath} → ${outputPath}`);
211
+ return true;
212
+ }
213
+ console.log(`\n✗ ${specPath} — ${String(errors.length)} error(s)`);
214
+ printErrors(specPath, errors);
215
+ return false;
216
+ }
217
+ /** Names of every compiled agent spec in the project — resolves delegate() targets. */
218
+ async function collectAgentNames() {
219
+ const names = [];
220
+ for (const p of findSpecs()) {
221
+ const s = await loadSpec(p);
222
+ if (s && s._specType === "agent")
223
+ names.push(s.name);
224
+ }
225
+ return names;
226
+ }
180
227
  async function compile(specPaths, config) {
181
228
  let allValid = true;
229
+ // Resolved lazily on the first railway spec — every delegate() target is
230
+ // checked against the agents defined anywhere in the project.
231
+ let knownAgents = null;
182
232
  for (const specPath of specPaths) {
183
233
  // Generator skills can't be executed to markdown — compile from source.
184
234
  const source = (0, node_fs_1.readFileSync)((0, node_path_1.resolve)(process.cwd(), specPath), "utf-8");
@@ -202,6 +252,15 @@ async function compile(specPaths, config) {
202
252
  if (!compileSkillToFile(spec, specPath))
203
253
  allValid = false;
204
254
  }
255
+ else if (spec._specType === "agent") {
256
+ if (!compileAgentToFile(spec, specPath))
257
+ allValid = false;
258
+ }
259
+ else if (spec._specType === "railway") {
260
+ knownAgents ??= await collectAgentNames();
261
+ if (!compileRailwayToFile(spec, specPath, knownAgents))
262
+ allValid = false;
263
+ }
205
264
  }
206
265
  return allValid;
207
266
  }
@@ -1612,6 +1671,44 @@ function skillStartCommand(target) {
1612
1671
  (0, skill_runtime_js_1.setActiveSkill)(process.cwd(), target);
1613
1672
  console.log(`Active skill: ${target}`);
1614
1673
  }
1674
+ /**
1675
+ * PreToolUse-hook entrypoint: enforce the active subagent's allowed-tools
1676
+ * contract. Reads the tool event on stdin, parses the active agent's compiled
1677
+ * `.md` tool rail, and blocks (exit 2 + reason on stderr) any tool outside it —
1678
+ * the deterministic boundary `tools:` alone can't provide (Claude Code #54898).
1679
+ */
1680
+ function agentHookCommand() {
1681
+ let raw = "";
1682
+ try {
1683
+ raw = (0, node_fs_1.readFileSync)(0, "utf-8");
1684
+ }
1685
+ catch {
1686
+ /* no stdin */
1687
+ }
1688
+ let tool = "";
1689
+ try {
1690
+ tool = JSON.parse(raw).tool_name ?? "";
1691
+ }
1692
+ catch {
1693
+ /* malformed input → no tool, allow */
1694
+ }
1695
+ if (!tool)
1696
+ return;
1697
+ const decision = (0, agent_runtime_js_1.evaluatePreToolUse)(process.cwd(), tool);
1698
+ if (!decision.allow) {
1699
+ console.error(decision.message);
1700
+ process.exit(2);
1701
+ }
1702
+ }
1703
+ /** Mark a subagent active so the PreToolUse hook enforces its tool contract. */
1704
+ function agentStartCommand(target) {
1705
+ if (!target) {
1706
+ console.error("Usage: vigiles agent-start <agents/<name>.md>");
1707
+ process.exit(2);
1708
+ }
1709
+ (0, agent_runtime_js_1.setActiveAgent)(process.cwd(), target);
1710
+ console.log(`Active agent: ${target}`);
1711
+ }
1615
1712
  /** Dispatch the skill-runtime subcommands. Returns false if unrecognized. */
1616
1713
  function handleSkillCommand(command, restArgs) {
1617
1714
  switch (command) {
@@ -1627,6 +1724,15 @@ function handleSkillCommand(command, restArgs) {
1627
1724
  case "skill-hook":
1628
1725
  skillHookCommand();
1629
1726
  return true;
1727
+ case "agent-start":
1728
+ agentStartCommand(restArgs[0]);
1729
+ return true;
1730
+ case "agent-done":
1731
+ (0, agent_runtime_js_1.clearActiveAgent)(process.cwd());
1732
+ return true;
1733
+ case "agent-hook":
1734
+ agentHookCommand();
1735
+ return true;
1630
1736
  case "action-hook":
1631
1737
  actionHookCommand();
1632
1738
  return true;
package/dist/compile.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * Reads .spec.ts files, validates references, and produces
5
5
  * markdown instruction files with integrity hashes.
6
6
  */
7
- import type { ClaudeSpec, SkillSpec } from "./spec.js";
7
+ import type { ClaudeSpec, SkillSpec, AgentSpec, Railway } from "./spec.js";
8
8
  import type { LinterCheckResult } from "./linters.js";
9
9
  /** @internal Compute SHA-256 hash of content (excluding any existing hash line). */
10
10
  export declare function computeHash(content: string): string;
@@ -24,7 +24,7 @@ export declare function verifyHash(content: string): {
24
24
  */
25
25
  /** @internal */ export declare function estimateTokens(text: string): number;
26
26
  export interface CompileError {
27
- type: "stale-file" | "stale-command" | "stale-ref" | "invalid-rule" | "budget-exceeded" | "section-too-long" | "section-has-header" | "reserved-section-key" | "spec-name-mismatch";
27
+ type: "stale-file" | "stale-command" | "stale-ref" | "invalid-rule" | "budget-exceeded" | "section-too-long" | "section-has-header" | "reserved-section-key" | "spec-name-mismatch" | "unknown-tool" | "invalid-railway";
28
28
  message: string;
29
29
  path?: string;
30
30
  }
@@ -79,6 +79,35 @@ export declare function compileSkill(spec: SkillSpec, options?: {
79
79
  basePath?: string;
80
80
  specFile?: string;
81
81
  }): CompileSkillResult;
82
+ export interface CompileAgentResult {
83
+ markdown: string;
84
+ errors: CompileError[];
85
+ }
86
+ /**
87
+ * Compile an AgentSpec into a subagent markdown file with YAML frontmatter.
88
+ * Verifies the tool contract and the body's references; the marks the body
89
+ * carries (`vigiles:symbol`, file/cmd refs) are the same ones `audit` re-checks.
90
+ */
91
+ export declare function compileAgent(spec: AgentSpec, options?: {
92
+ basePath?: string;
93
+ specFile?: string;
94
+ }): CompileAgentResult;
95
+ export interface CompileRailwayOptions {
96
+ /** Names of compiled agents, to resolve `delegate` targets. Skipped if omitted. */
97
+ knownAgents?: readonly string[];
98
+ specFile?: string;
99
+ }
100
+ export interface CompileRailwayResult {
101
+ markdown: string;
102
+ errors: CompileError[];
103
+ }
104
+ /** Verify a railway: non-empty, bounded recovery, every delegate target real. */
105
+ export declare function validateRailway(rw: Railway, knownAgents?: readonly string[]): CompileError[];
106
+ /**
107
+ * Compile a railway into an orchestrator command markdown (with integrity hash),
108
+ * resolving every delegate target against `knownAgents` when provided.
109
+ */
110
+ export declare function compileRailway(rw: Railway, options?: CompileRailwayOptions): CompileRailwayResult;
82
111
  export interface HashCheckResult {
83
112
  hasHash: boolean;
84
113
  valid: boolean;
@@ -101,5 +130,5 @@ export interface AdoptResult {
101
130
  * Compare a generated file against what the spec would produce.
102
131
  * Returns the diff so users can see what was manually changed.
103
132
  */
104
- export declare function adoptDiff(filePath: string, spec: ClaudeSpec | SkillSpec, basePath: string): AdoptResult;
133
+ export declare function adoptDiff(filePath: string, spec: ClaudeSpec | SkillSpec | AgentSpec, basePath: string): AdoptResult;
105
134
  //# sourceMappingURL=compile.d.ts.map