@stigmer/plugin-package 0.1.0-dev.20261017020314.g644422724c27

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 (273) hide show
  1. package/LICENSE +194 -0
  2. package/README.md +72 -0
  3. package/client/archive.d.ts +27 -0
  4. package/client/archive.d.ts.map +1 -0
  5. package/client/archive.js +38 -0
  6. package/client/archive.js.map +1 -0
  7. package/client/builtin.d.ts +37 -0
  8. package/client/builtin.d.ts.map +1 -0
  9. package/client/builtin.js +49 -0
  10. package/client/builtin.js.map +1 -0
  11. package/client/ignore/defaults.d.ts +2 -0
  12. package/client/ignore/defaults.d.ts.map +1 -0
  13. package/client/ignore/defaults.js +140 -0
  14. package/client/ignore/defaults.js.map +1 -0
  15. package/client/ignore/match.d.ts +3 -0
  16. package/client/ignore/match.d.ts.map +1 -0
  17. package/client/ignore/match.js +166 -0
  18. package/client/ignore/match.js.map +1 -0
  19. package/client/ignore/matcher.d.ts +50 -0
  20. package/client/ignore/matcher.d.ts.map +1 -0
  21. package/client/ignore/matcher.js +116 -0
  22. package/client/ignore/matcher.js.map +1 -0
  23. package/client/ignore/pattern.d.ts +13 -0
  24. package/client/ignore/pattern.d.ts.map +1 -0
  25. package/client/ignore/pattern.js +119 -0
  26. package/client/ignore/pattern.js.map +1 -0
  27. package/client/prepare-eval.d.ts +62 -0
  28. package/client/prepare-eval.d.ts.map +1 -0
  29. package/client/prepare-eval.js +60 -0
  30. package/client/prepare-eval.js.map +1 -0
  31. package/client/prepare.d.ts +99 -0
  32. package/client/prepare.d.ts.map +1 -0
  33. package/client/prepare.js +89 -0
  34. package/client/prepare.js.map +1 -0
  35. package/client/presentation.d.ts +19 -0
  36. package/client/presentation.d.ts.map +1 -0
  37. package/client/presentation.js +44 -0
  38. package/client/presentation.js.map +1 -0
  39. package/client/refs.d.ts +76 -0
  40. package/client/refs.d.ts.map +1 -0
  41. package/client/refs.js +123 -0
  42. package/client/refs.js.map +1 -0
  43. package/client/release.d.ts +20 -0
  44. package/client/release.d.ts.map +1 -0
  45. package/client/release.js +28 -0
  46. package/client/release.js.map +1 -0
  47. package/client/reroot.d.ts +21 -0
  48. package/client/reroot.d.ts.map +1 -0
  49. package/client/reroot.js +37 -0
  50. package/client/reroot.js.map +1 -0
  51. package/client/select.d.ts +74 -0
  52. package/client/select.d.ts.map +1 -0
  53. package/client/select.js +121 -0
  54. package/client/select.js.map +1 -0
  55. package/client/vocabulary.d.ts +13 -0
  56. package/client/vocabulary.d.ts.map +1 -0
  57. package/client/vocabulary.js +17 -0
  58. package/client/vocabulary.js.map +1 -0
  59. package/client.d.ts +51 -0
  60. package/client.d.ts.map +1 -0
  61. package/client.js +51 -0
  62. package/client.js.map +1 -0
  63. package/detect.d.ts +83 -0
  64. package/detect.d.ts.map +1 -0
  65. package/detect.js +210 -0
  66. package/detect.js.map +1 -0
  67. package/dialects/claude.d.ts +34 -0
  68. package/dialects/claude.d.ts.map +1 -0
  69. package/dialects/claude.js +84 -0
  70. package/dialects/claude.js.map +1 -0
  71. package/dialects/codex.d.ts +20 -0
  72. package/dialects/codex.d.ts.map +1 -0
  73. package/dialects/codex.js +21 -0
  74. package/dialects/codex.js.map +1 -0
  75. package/dialects/cursor.d.ts +25 -0
  76. package/dialects/cursor.d.ts.map +1 -0
  77. package/dialects/cursor.js +66 -0
  78. package/dialects/cursor.js.map +1 -0
  79. package/dialects/manifest.d.ts +164 -0
  80. package/dialects/manifest.d.ts.map +1 -0
  81. package/dialects/manifest.js +256 -0
  82. package/dialects/manifest.js.map +1 -0
  83. package/dialects/open.d.ts +19 -0
  84. package/dialects/open.d.ts.map +1 -0
  85. package/dialects/open.js +49 -0
  86. package/dialects/open.js.map +1 -0
  87. package/documents.d.ts +58 -0
  88. package/documents.d.ts.map +1 -0
  89. package/documents.js +121 -0
  90. package/documents.js.map +1 -0
  91. package/evals/eval-dir.d.ts +59 -0
  92. package/evals/eval-dir.d.ts.map +1 -0
  93. package/evals/eval-dir.js +84 -0
  94. package/evals/eval-dir.js.map +1 -0
  95. package/evals/fields.d.ts +78 -0
  96. package/evals/fields.d.ts.map +1 -0
  97. package/evals/fields.js +156 -0
  98. package/evals/fields.js.map +1 -0
  99. package/evals/glob.d.ts +40 -0
  100. package/evals/glob.d.ts.map +1 -0
  101. package/evals/glob.js +146 -0
  102. package/evals/glob.js.map +1 -0
  103. package/evals/graders.d.ts +52 -0
  104. package/evals/graders.d.ts.map +1 -0
  105. package/evals/graders.js +279 -0
  106. package/evals/graders.js.map +1 -0
  107. package/evals/markdown.d.ts +24 -0
  108. package/evals/markdown.d.ts.map +1 -0
  109. package/evals/markdown.js +48 -0
  110. package/evals/markdown.js.map +1 -0
  111. package/evals/mocks.d.ts +50 -0
  112. package/evals/mocks.d.ts.map +1 -0
  113. package/evals/mocks.js +182 -0
  114. package/evals/mocks.js.map +1 -0
  115. package/evals/read-eval-suite.d.ts +110 -0
  116. package/evals/read-eval-suite.d.ts.map +1 -0
  117. package/evals/read-eval-suite.js +603 -0
  118. package/evals/read-eval-suite.js.map +1 -0
  119. package/evals/summary.d.ts +61 -0
  120. package/evals/summary.d.ts.map +1 -0
  121. package/evals/summary.js +69 -0
  122. package/evals/summary.js.map +1 -0
  123. package/evals/types.d.ts +203 -0
  124. package/evals/types.d.ts.map +1 -0
  125. package/evals/types.js +21 -0
  126. package/evals/types.js.map +1 -0
  127. package/files.d.ts +135 -0
  128. package/files.d.ts.map +1 -0
  129. package/files.js +209 -0
  130. package/files.js.map +1 -0
  131. package/frontmatter.d.ts +51 -0
  132. package/frontmatter.d.ts.map +1 -0
  133. package/frontmatter.js +61 -0
  134. package/frontmatter.js.map +1 -0
  135. package/index.d.ts +37 -0
  136. package/index.d.ts.map +1 -0
  137. package/index.js +33 -0
  138. package/index.js.map +1 -0
  139. package/marketplace/messages.d.ts +20 -0
  140. package/marketplace/messages.d.ts.map +1 -0
  141. package/marketplace/messages.js +49 -0
  142. package/marketplace/messages.js.map +1 -0
  143. package/marketplace/outcome.d.ts +70 -0
  144. package/marketplace/outcome.d.ts.map +1 -0
  145. package/marketplace/outcome.js +29 -0
  146. package/marketplace/outcome.js.map +1 -0
  147. package/marketplace/read-marketplace.d.ts +52 -0
  148. package/marketplace/read-marketplace.d.ts.map +1 -0
  149. package/marketplace/read-marketplace.js +351 -0
  150. package/marketplace/read-marketplace.js.map +1 -0
  151. package/messages.d.ts +55 -0
  152. package/messages.d.ts.map +1 -0
  153. package/messages.js +167 -0
  154. package/messages.js.map +1 -0
  155. package/normalise/hooks.d.ts +91 -0
  156. package/normalise/hooks.d.ts.map +1 -0
  157. package/normalise/hooks.js +437 -0
  158. package/normalise/hooks.js.map +1 -0
  159. package/normalise/ignored.d.ts +34 -0
  160. package/normalise/ignored.d.ts.map +1 -0
  161. package/normalise/ignored.js +78 -0
  162. package/normalise/ignored.js.map +1 -0
  163. package/normalise/mcp-servers.d.ts +50 -0
  164. package/normalise/mcp-servers.d.ts.map +1 -0
  165. package/normalise/mcp-servers.js +398 -0
  166. package/normalise/mcp-servers.js.map +1 -0
  167. package/normalise/settings.d.ts +32 -0
  168. package/normalise/settings.d.ts.map +1 -0
  169. package/normalise/settings.js +72 -0
  170. package/normalise/settings.js.map +1 -0
  171. package/normalise/skills.d.ts +39 -0
  172. package/normalise/skills.d.ts.map +1 -0
  173. package/normalise/skills.js +135 -0
  174. package/normalise/skills.js.map +1 -0
  175. package/normalise/stigmer-folder.d.ts +17 -0
  176. package/normalise/stigmer-folder.d.ts.map +1 -0
  177. package/normalise/stigmer-folder.js +19 -0
  178. package/normalise/stigmer-folder.js.map +1 -0
  179. package/normalise/sub-agents.d.ts +54 -0
  180. package/normalise/sub-agents.d.ts.map +1 -0
  181. package/normalise/sub-agents.js +219 -0
  182. package/normalise/sub-agents.js.map +1 -0
  183. package/normalise/variables.d.ts +31 -0
  184. package/normalise/variables.d.ts.map +1 -0
  185. package/normalise/variables.js +170 -0
  186. package/normalise/variables.js.map +1 -0
  187. package/outcome.d.ts +59 -0
  188. package/outcome.d.ts.map +1 -0
  189. package/outcome.js +32 -0
  190. package/outcome.js.map +1 -0
  191. package/package.json +47 -0
  192. package/placeholders.d.ts +65 -0
  193. package/placeholders.d.ts.map +1 -0
  194. package/placeholders.js +99 -0
  195. package/placeholders.js.map +1 -0
  196. package/presentation.d.ts +53 -0
  197. package/presentation.d.ts.map +1 -0
  198. package/presentation.js +156 -0
  199. package/presentation.js.map +1 -0
  200. package/read-plugin-package.d.ts +20 -0
  201. package/read-plugin-package.d.ts.map +1 -0
  202. package/read-plugin-package.js +73 -0
  203. package/read-plugin-package.js.map +1 -0
  204. package/src/client/archive.ts +44 -0
  205. package/src/client/builtin.ts +60 -0
  206. package/src/client/ignore/defaults.ts +155 -0
  207. package/src/client/ignore/match.ts +158 -0
  208. package/src/client/ignore/matcher.ts +156 -0
  209. package/src/client/ignore/pattern.ts +132 -0
  210. package/src/client/prepare-eval.ts +92 -0
  211. package/src/client/prepare.ts +160 -0
  212. package/src/client/presentation.ts +47 -0
  213. package/src/client/refs.ts +170 -0
  214. package/src/client/release.ts +29 -0
  215. package/src/client/reroot.ts +35 -0
  216. package/src/client/select.ts +161 -0
  217. package/src/client/vocabulary.ts +19 -0
  218. package/src/client.ts +102 -0
  219. package/src/detect.ts +245 -0
  220. package/src/dialects/claude.ts +102 -0
  221. package/src/dialects/codex.ts +26 -0
  222. package/src/dialects/cursor.ts +77 -0
  223. package/src/dialects/manifest.ts +316 -0
  224. package/src/dialects/open.ts +50 -0
  225. package/src/documents.ts +158 -0
  226. package/src/evals/eval-dir.ts +101 -0
  227. package/src/evals/fields.ts +184 -0
  228. package/src/evals/glob.ts +141 -0
  229. package/src/evals/graders.ts +293 -0
  230. package/src/evals/markdown.ts +56 -0
  231. package/src/evals/mocks.ts +202 -0
  232. package/src/evals/read-eval-suite.ts +713 -0
  233. package/src/evals/summary.ts +100 -0
  234. package/src/evals/types.ts +217 -0
  235. package/src/files.ts +235 -0
  236. package/src/frontmatter.ts +70 -0
  237. package/src/index.ts +129 -0
  238. package/src/marketplace/messages.ts +61 -0
  239. package/src/marketplace/outcome.ts +91 -0
  240. package/src/marketplace/read-marketplace.ts +388 -0
  241. package/src/messages.ts +248 -0
  242. package/src/normalise/hooks.ts +469 -0
  243. package/src/normalise/ignored.ts +79 -0
  244. package/src/normalise/mcp-servers.ts +423 -0
  245. package/src/normalise/settings.ts +81 -0
  246. package/src/normalise/skills.ts +145 -0
  247. package/src/normalise/stigmer-folder.ts +23 -0
  248. package/src/normalise/sub-agents.ts +231 -0
  249. package/src/normalise/variables.ts +181 -0
  250. package/src/outcome.ts +158 -0
  251. package/src/placeholders.ts +110 -0
  252. package/src/presentation.ts +170 -0
  253. package/src/read-plugin-package.ts +80 -0
  254. package/src/testing.ts +347 -0
  255. package/src/tool-names.ts +30 -0
  256. package/src/trim.ts +10 -0
  257. package/src/types.ts +214 -0
  258. package/testing.d.ts +127 -0
  259. package/testing.d.ts.map +1 -0
  260. package/testing.js +266 -0
  261. package/testing.js.map +1 -0
  262. package/tool-names.d.ts +21 -0
  263. package/tool-names.d.ts.map +1 -0
  264. package/tool-names.js +28 -0
  265. package/tool-names.js.map +1 -0
  266. package/trim.d.ts +7 -0
  267. package/trim.d.ts.map +1 -0
  268. package/trim.js +12 -0
  269. package/trim.js.map +1 -0
  270. package/types.d.ts +179 -0
  271. package/types.d.ts.map +1 -0
  272. package/types.js +19 -0
  273. package/types.js.map +1 -0
@@ -0,0 +1,469 @@
1
+ /**
2
+ * A plugin's hooks files into the tool-call hooks Stigmer reads, in the
3
+ * format the plugin wrote them.
4
+ *
5
+ * Sources are resolved by `detect.ts`: every vendor manifest's declared
6
+ * `hooks` (a file or an inline object) and `hooks/hooks.json`, each file
7
+ * once. A file is `{"hooks": {<event>: [...]}}` in both formats; what sits
8
+ * in an event's array decides which. Claude Code's entries are matcher
9
+ * groups holding a `hooks` array of handlers; Cursor's are the handlers
10
+ * themselves, each with its own matcher, and only Cursor's file carries
11
+ * `version`. A file whose entries are neither, or both, or whose format no
12
+ * manifest reaching it reads, is refused. A Claude or Codex manifest's
13
+ * inline object is the bare event map; Cursor documents no inline form, so
14
+ * a Cursor inline object is read in its file's shape.
15
+ *
16
+ * Stigmer reads hooks on tool calls only, and only command handlers that
17
+ * can decide a call: `PreToolUse` and `PostToolUse` in Claude Code's
18
+ * format (the native engine runs them), and Cursor's `preToolUse`, `beforeShellExecution`,
19
+ * `beforeMCPExecution`, `postToolUse` and `afterMCPExecution`. Everything
20
+ * else is named, never dropped silently: an event once per file
21
+ * (`hook-event-not-run`), a handler that is not a command, runs in the
22
+ * background (`async`, `asyncRewake`) or needs PowerShell
23
+ * (`hook-handler-not-run`), and a field Stigmer does not read
24
+ * (`hook-field-ignored`; `once` among them, which Claude Code itself honours
25
+ * only in skill frontmatter). The stored hooks therefore hold only what
26
+ * runs, and the runner needs no table of its own. A `hooks/` folder in a
27
+ * plugin with no vendor manifest is named too (`hooks-not-read`): the open
28
+ * format defines no hooks.
29
+ *
30
+ * A plugin runs one format. When both formats carry tool-call hooks
31
+ * Stigmer reads, Claude Code's win (the reader's detection precedence, and
32
+ * the format the project treats as primary) and each Cursor source that
33
+ * carries some is named once as not run (`hooks-format-not-run`); its own
34
+ * event and handler warnings are not reported, since none of it runs. The
35
+ * choice counts only sources that carry tool-call hooks, so a Claude file
36
+ * with nothing but lifecycle events never displaces a Cursor guard.
37
+ *
38
+ * What is carried is checked, and a broken hook refuses the install, as a
39
+ * broken sub-agent does: a matcher that is neither "every call", an exact
40
+ * list, nor a regular expression JavaScript compiles (both formats test
41
+ * matchers with JavaScript's `RegExp`), an `if` that is not a permission
42
+ * rule's `Tool` or `Tool(specifier)` shape, a command handler with no
43
+ * command. What a specifier means is the runner's to evaluate. Matchers,
44
+ * conditions and commands are kept verbatim, placeholders included: a
45
+ * hook's script sees tool names too, so nothing is rewritten at install.
46
+ *
47
+ * `${user_config.KEY}` in an exec-form handler (one with `args`) is a
48
+ * reference to `KEY`, returned so a variable only a hook uses is not
49
+ * reported as unreferenced; `hookVariableReferences` reads the same rule off
50
+ * a read plugin's hooks, for the installer that declares them on the agent.
51
+ *
52
+ * The event set, `isValidMatcher` and `HOOK_CONDITION_PATTERN` are exported
53
+ * so a hooks block written in an agent by hand is checked by the rules a
54
+ * plugin's are.
55
+ */
56
+
57
+ import type { ManifestSet, ResolvedHookSource } from "../detect.js";
58
+ import { describeValue, fields, isJsonObject, isStringArray, type JsonObject, parseJsonObject, readText } from "../documents.js";
59
+ import type { PluginFileIndex } from "../files.js";
60
+ import type { Findings } from "../messages.js";
61
+ import type { FindingContext, PluginWarningKind } from "../outcome.js";
62
+ import { userConfigReferences } from "../placeholders.js";
63
+ import type { HookFormat, PluginHookGroup, PluginHookHandler, PluginHooks } from "../types.js";
64
+
65
+ /** The events Stigmer runs, per format. */
66
+ export const RUN_EVENTS: Readonly<Record<HookFormat, ReadonlySet<string>>> = {
67
+ "claude-code": new Set(["PreToolUse", "PostToolUse"]),
68
+ cursor: new Set(["preToolUse", "beforeShellExecution", "beforeMCPExecution", "postToolUse", "afterMCPExecution"]),
69
+ };
70
+
71
+ /** Claude Code's exact-match matcher alphabet; any other character makes the matcher a regular expression. */
72
+ const CLAUDE_EXACT_MATCHER = /^[A-Za-z0-9_\- ,|]+$/;
73
+
74
+ /** A permission rule: `Tool` or `Tool(specifier)`, the shape a handler's `if` must take. */
75
+ export const HOOK_CONDITION_PATTERN = /^[A-Za-z_][A-Za-z0-9_-]*(\([^\r\n]+\))?$/;
76
+
77
+ /** Top-level keys a hooks file may carry beside `hooks` without a warning. */
78
+ const FILE_KEYS: Readonly<Record<HookFormat, ReadonlySet<string>>> = {
79
+ "claude-code": new Set(["hooks", "description"]),
80
+ cursor: new Set(["hooks", "version"]),
81
+ };
82
+
83
+ const CLAUDE_GROUP_KEYS: ReadonlySet<string> = new Set(["matcher", "hooks"]);
84
+ const CLAUDE_HANDLER_KEYS: ReadonlySet<string> = new Set(["type", "command", "args", "timeout", "if", "async", "asyncRewake", "shell"]);
85
+ const CURSOR_HANDLER_KEYS: ReadonlySet<string> = new Set(["type", "command", "matcher", "timeout", "failClosed"]);
86
+
87
+ const FORMAT_NAMES: Readonly<Record<HookFormat, string>> = { "claude-code": "Claude Code's", cursor: "Cursor's" };
88
+
89
+ export interface HooksResult {
90
+ /** The tool-call hooks read; absent when there are none. */
91
+ readonly hooks?: PluginHooks;
92
+ /** `${user_config.KEY}` names the carried handlers reference. */
93
+ readonly references: ReadonlySet<string>;
94
+ }
95
+
96
+ /** What one source contributed, held until the plugin's run format is known. */
97
+ interface SourceRead {
98
+ readonly path: string;
99
+ /** `undefined` when the source has no event entries, so its format is moot. */
100
+ readonly format: HookFormat | undefined;
101
+ readonly groups: PluginHookGroup[];
102
+ readonly warnings: { readonly kind: PluginWarningKind; readonly ctx: FindingContext }[];
103
+ readonly references: string[];
104
+ }
105
+
106
+ export function normaliseHooks(index: PluginFileIndex, set: ManifestSet, findings: Findings): HooksResult {
107
+ // The open format defines no hooks: with no vendor manifest, a hooks/
108
+ // folder is named rather than given a format's semantics.
109
+ if (!set.readsAgents && index.childDirectories("").includes("hooks")) {
110
+ findings.warn("hooks-not-read", { path: "hooks/" });
111
+ }
112
+ const reads: SourceRead[] = [];
113
+ for (const source of set.hookSources) {
114
+ const read = readSource(index, source, findings);
115
+ if (read !== undefined) reads.push(read);
116
+ }
117
+
118
+ const carries = (format: HookFormat): boolean => reads.some((r) => r.format === format && r.groups.length > 0);
119
+ const runFormat: HookFormat | undefined = carries("claude-code") ? "claude-code" : carries("cursor") ? "cursor" : undefined;
120
+
121
+ const groups: PluginHookGroup[] = [];
122
+ const references = new Set<string>();
123
+ for (const read of reads) {
124
+ if (read.format !== undefined && read.format !== runFormat && read.groups.length > 0) {
125
+ findings.warn("hooks-format-not-run", { path: read.path, detail: FORMAT_NAMES[read.format] });
126
+ continue;
127
+ }
128
+ for (const warning of read.warnings) findings.warn(warning.kind, warning.ctx);
129
+ groups.push(...read.groups);
130
+ for (const name of read.references) references.add(name);
131
+ }
132
+
133
+ return {
134
+ ...(runFormat !== undefined && groups.length > 0 && { hooks: { format: runFormat, groups } }),
135
+ references,
136
+ };
137
+ }
138
+
139
+ function readSource(index: PluginFileIndex, source: ResolvedHookSource, findings: Findings): SourceRead | undefined {
140
+ if (source.kind === "inline") {
141
+ const path = `${source.manifest}#hooks`;
142
+ if (source.format === "claude-code") return readEventMap(source.value, path, undefined, findings, true);
143
+ return readFileShape(source.value, path, [source.format], findings);
144
+ }
145
+ if (!index.has(source.path)) {
146
+ findings.warn("path-missing", { path: source.manifest, subject: `./${source.path}` });
147
+ return undefined;
148
+ }
149
+ const text = readText(index, source.path, "hooks", findings);
150
+ if (text === undefined) return undefined;
151
+ const object = parseJsonObject(text, source.path, "hooks-unreadable", findings);
152
+ if (object === undefined) return undefined;
153
+ return readFileShape(object, source.path, source.formats, findings);
154
+ }
155
+
156
+ /** `{"hooks": {...}}`, with the format decided by the entries and checked against what the manifests read. */
157
+ function readFileShape(object: JsonObject, path: string, accepted: readonly HookFormat[], findings: Findings): SourceRead | undefined {
158
+ const events = object["hooks"];
159
+ if (!isJsonObject(events)) {
160
+ findings.error("hooks-shape", {
161
+ path,
162
+ detail:
163
+ events === undefined
164
+ ? "a hooks file wraps its events in a top-level 'hooks' object"
165
+ : "the top-level 'hooks' must be an object mapping events to their hooks",
166
+ });
167
+ return undefined;
168
+ }
169
+ const format = detectFormat(events, path, findings);
170
+ if (format === null) return undefined;
171
+ if (format !== undefined && !accepted.includes(format)) {
172
+ findings.error("hooks-shape", {
173
+ path,
174
+ detail: `they are written in ${FORMAT_NAMES[format]} format, but the manifest that reads them expects ${accepted
175
+ .map((f) => FORMAT_NAMES[f])
176
+ .join(" or ")}`,
177
+ });
178
+ return undefined;
179
+ }
180
+ const read = readEventMap(events, path, format, findings, false);
181
+ if (read === undefined) return undefined;
182
+ const known = FILE_KEYS[format ?? "claude-code"];
183
+ for (const [key] of fields(object)) {
184
+ if (!known.has(key) && !(format === undefined && FILE_KEYS.cursor.has(key))) {
185
+ read.warnings.push({ kind: "hook-field-ignored", ctx: { path, detail: key } });
186
+ }
187
+ }
188
+ return read;
189
+ }
190
+
191
+ /**
192
+ * The format an event map's entries are written in; `undefined` when it has
193
+ * none, `null` after a `hooks-shape` finding.
194
+ */
195
+ function detectFormat(events: JsonObject, path: string, findings: Findings): HookFormat | undefined | null {
196
+ let claude = false;
197
+ let cursor = false;
198
+ for (const [event, entries] of fields(events)) {
199
+ if (!Array.isArray(entries)) {
200
+ findings.error("hooks-shape", { path, subject: event, detail: `the hooks on '${event}' must be an array` });
201
+ return null;
202
+ }
203
+ for (const entry of entries) {
204
+ if (!isJsonObject(entry)) {
205
+ findings.error("hooks-shape", { path, subject: event, detail: `every entry on '${event}' must be an object` });
206
+ return null;
207
+ }
208
+ if (entry["hooks"] !== undefined) claude = true;
209
+ else cursor = true;
210
+ }
211
+ }
212
+ if (claude && cursor) {
213
+ findings.error("hooks-shape", { path, detail: "they mix Claude Code matcher groups with Cursor handlers" });
214
+ return null;
215
+ }
216
+ return claude ? "claude-code" : cursor ? "cursor" : undefined;
217
+ }
218
+
219
+ /** An event map in a known (or moot) format into a source read. */
220
+ function readEventMap(
221
+ events: JsonObject,
222
+ path: string,
223
+ format: HookFormat | undefined,
224
+ findings: Findings,
225
+ inline: boolean,
226
+ ): SourceRead | undefined {
227
+ if (inline) {
228
+ // A bare map: its entries must be Claude Code groups.
229
+ for (const [event, entries] of fields(events)) {
230
+ if (!Array.isArray(entries)) {
231
+ findings.error("hooks-shape", {
232
+ path,
233
+ subject: event,
234
+ detail:
235
+ event === "hooks" && isJsonObject(entries)
236
+ ? "an inline hooks object is the event map itself, with no 'hooks' wrapper"
237
+ : `the hooks on '${event}' must be an array`,
238
+ });
239
+ return undefined;
240
+ }
241
+ if (entries.some((entry) => !isJsonObject(entry) || !Array.isArray(entry["hooks"]))) {
242
+ findings.error("hooks-shape", { path, subject: event, detail: `every entry on '${event}' must be a matcher group with a 'hooks' array` });
243
+ return undefined;
244
+ }
245
+ }
246
+ if (fields(events).some(([, entries]) => Array.isArray(entries) && entries.length > 0)) format = "claude-code";
247
+ }
248
+ const out: SourceRead = { path, format, groups: [], warnings: [], references: [] };
249
+ if (format === undefined) return out;
250
+
251
+ let refused = false;
252
+ for (const [event, entries] of fields(events)) {
253
+ const list = entries as readonly JsonObject[];
254
+ if (list.length === 0) continue;
255
+ if (!RUN_EVENTS[format].has(event)) {
256
+ out.warnings.push({ kind: "hook-event-not-run", ctx: { path, subject: event } });
257
+ if (format === "claude-code" && !checkClaudeGroupsShape(list, event, path, findings)) refused = true;
258
+ continue;
259
+ }
260
+ for (const entry of list) {
261
+ const ok = format === "claude-code" ? readClaudeGroup(entry, event, path, out, findings) : readCursorHandler(entry, event, path, out, findings);
262
+ if (!ok) refused = true;
263
+ }
264
+ }
265
+ return refused ? undefined : out;
266
+ }
267
+
268
+ /** A not-run event's groups still have to be groups. */
269
+ function checkClaudeGroupsShape(groups: readonly JsonObject[], event: string, path: string, findings: Findings): boolean {
270
+ for (const group of groups) {
271
+ if (!Array.isArray(group["hooks"]) || !group["hooks"].every(isJsonObject)) {
272
+ findings.error("hooks-shape", { path, subject: event, detail: `a matcher group on '${event}' must hold a 'hooks' array of handlers` });
273
+ return false;
274
+ }
275
+ }
276
+ return true;
277
+ }
278
+
279
+ function readClaudeGroup(group: JsonObject, event: string, path: string, out: SourceRead, findings: Findings): boolean {
280
+ const handlers = group["hooks"];
281
+ if (!Array.isArray(handlers) || !handlers.every(isJsonObject)) {
282
+ findings.error("hooks-shape", { path, subject: event, detail: `a matcher group on '${event}' must hold a 'hooks' array of handlers` });
283
+ return false;
284
+ }
285
+ const matcher = readMatcher(group, event, path, "claude-code", findings);
286
+ if (matcher === null) return false;
287
+ for (const [key] of fields(group)) {
288
+ if (!CLAUDE_GROUP_KEYS.has(key)) out.warnings.push({ kind: "hook-field-ignored", ctx: { path, subject: event, detail: key } });
289
+ }
290
+ let ok = true;
291
+ const carried: PluginHookHandler[] = [];
292
+ for (const handler of handlers) {
293
+ const read = readClaudeHandler(handler, event, path, out, findings);
294
+ if (read === null) ok = false;
295
+ else if (read !== undefined) carried.push(read);
296
+ }
297
+ if (ok && carried.length > 0) out.groups.push({ event, matcher, handlers: carried });
298
+ return ok;
299
+ }
300
+
301
+ /** A carried handler, `undefined` when it is warned as not run, `null` after an error. */
302
+ function readClaudeHandler(
303
+ handler: JsonObject,
304
+ event: string,
305
+ path: string,
306
+ out: SourceRead,
307
+ findings: Findings,
308
+ ): PluginHookHandler | undefined | null {
309
+ const type = handler["type"] ?? "command";
310
+ if (typeof type !== "string") {
311
+ findings.error("hooks-shape", { path, subject: event, detail: "a handler's 'type' must be a string" });
312
+ return null;
313
+ }
314
+ const notRun = (detail: string): undefined => {
315
+ out.warnings.push({ kind: "hook-handler-not-run", ctx: { path, subject: event, detail } });
316
+ return undefined;
317
+ };
318
+ if (type !== "command") return notRun(`its type is '${type}', and Stigmer runs command hooks`);
319
+ if (handler["async"] === true) return notRun("it runs in the background ('async'), so it cannot decide a call");
320
+ if (handler["asyncRewake"] === true) return notRun("it runs in the background ('asyncRewake'), so it cannot decide a call");
321
+
322
+ const args = handler["args"] ?? [];
323
+ if (!isStringArray(args)) {
324
+ findings.error("hooks-shape", { path, subject: event, detail: "a handler's 'args' must be a list of strings" });
325
+ return null;
326
+ }
327
+ if (handler["shell"] === "powershell" && args.length === 0) return notRun("it runs in PowerShell ('shell'), which the runner does not have");
328
+
329
+ const command = readCommand(handler, event, path, findings);
330
+ const timeout = readTimeout(handler, event, path, findings);
331
+ const condition = readCondition(handler, event, path, findings);
332
+ if (command === undefined || timeout === null || condition === null) return null;
333
+
334
+ for (const [key] of fields(handler)) {
335
+ if (!CLAUDE_HANDLER_KEYS.has(key)) out.warnings.push({ kind: "hook-field-ignored", ctx: { path, subject: event, detail: key } });
336
+ }
337
+ out.references.push(...handlerVariableReferences(command, args));
338
+ return {
339
+ command,
340
+ args,
341
+ ...(timeout !== undefined && { timeoutSeconds: timeout }),
342
+ ...(condition !== undefined && { condition }),
343
+ failClosed: false,
344
+ };
345
+ }
346
+
347
+ function readCursorHandler(handler: JsonObject, event: string, path: string, out: SourceRead, findings: Findings): boolean {
348
+ const type = handler["type"] ?? "command";
349
+ if (typeof type !== "string") {
350
+ findings.error("hooks-shape", { path, subject: event, detail: "a handler's 'type' must be a string" });
351
+ return false;
352
+ }
353
+ if (type !== "command") {
354
+ out.warnings.push({ kind: "hook-handler-not-run", ctx: { path, subject: event, detail: `its type is '${type}', and Stigmer runs command hooks` } });
355
+ return true;
356
+ }
357
+ const command = readCommand(handler, event, path, findings);
358
+ const matcher = readMatcher(handler, event, path, "cursor", findings);
359
+ const timeout = readTimeout(handler, event, path, findings);
360
+ const failClosed = handler["failClosed"] ?? false;
361
+ if (typeof failClosed !== "boolean") {
362
+ findings.error("hooks-shape", { path, subject: event, detail: "a handler's 'failClosed' must be true or false" });
363
+ return false;
364
+ }
365
+ if (command === undefined || matcher === null || timeout === null) return false;
366
+ for (const [key] of fields(handler)) {
367
+ if (!CURSOR_HANDLER_KEYS.has(key)) out.warnings.push({ kind: "hook-field-ignored", ctx: { path, subject: event, detail: key } });
368
+ }
369
+ out.groups.push({
370
+ event,
371
+ matcher,
372
+ handlers: [{ command, args: [], ...(timeout !== undefined && { timeoutSeconds: timeout }), failClosed }],
373
+ });
374
+ return true;
375
+ }
376
+
377
+ /** The command as written, or `undefined` after `hook-command-missing`. */
378
+ function readCommand(handler: JsonObject, event: string, path: string, findings: Findings): string | undefined {
379
+ const command = handler["command"];
380
+ if (typeof command !== "string" || command.trim() === "") {
381
+ findings.error("hook-command-missing", { path, subject: event });
382
+ return undefined;
383
+ }
384
+ return command;
385
+ }
386
+
387
+ /** The matcher as written (`""` when absent), or `null` after a finding. */
388
+ function readMatcher(object: JsonObject, event: string, path: string, format: HookFormat, findings: Findings): string | null {
389
+ const matcher = object["matcher"] ?? "";
390
+ if (typeof matcher !== "string") {
391
+ findings.error("hooks-shape", { path, subject: event, detail: "a 'matcher' must be a string" });
392
+ return null;
393
+ }
394
+ if (!isValidMatcher(matcher, format)) {
395
+ findings.error("hook-matcher-invalid", { path, subject: event, detail: matcher });
396
+ return null;
397
+ }
398
+ return matcher;
399
+ }
400
+
401
+ /**
402
+ * The `${user_config.KEY}` names one Claude Code handler references: in its
403
+ * command and arguments when it runs in exec form (it has `args`), where
404
+ * Claude substitutes them; a shell-form command's text is the shell's.
405
+ */
406
+ function handlerVariableReferences(command: string, args: readonly string[]): string[] {
407
+ if (args.length === 0) return [];
408
+ return [command, ...args].flatMap((value) => [...userConfigReferences(value)]);
409
+ }
410
+
411
+ /**
412
+ * Every `${user_config.KEY}` name a hook set references, in first-seen
413
+ * order. It takes the shape it reads, so a read plugin's hooks and a stored
414
+ * `HookConfig` give the same names: install declares them on the agent it
415
+ * composes, and the console on an agent a person switches the hooks on for.
416
+ */
417
+ export function hookVariableReferences(hooks: {
418
+ readonly groups: readonly { readonly handlers: readonly { readonly command: string; readonly args: readonly string[] }[] }[];
419
+ }): readonly string[] {
420
+ const names = new Set<string>();
421
+ for (const group of hooks.groups) {
422
+ for (const handler of group.handlers) {
423
+ for (const name of handlerVariableReferences(handler.command, handler.args)) names.add(name);
424
+ }
425
+ }
426
+ return [...names];
427
+ }
428
+
429
+ /**
430
+ * True when a matcher means something in its format: every call (`""` or
431
+ * `*`, in both), an exact list (Claude Code), or a regular expression
432
+ * JavaScript compiles.
433
+ */
434
+ export function isValidMatcher(matcher: string, format: HookFormat): boolean {
435
+ if (matcher === "" || matcher === "*") return true;
436
+ if (format === "claude-code" && CLAUDE_EXACT_MATCHER.test(matcher)) return true;
437
+ try {
438
+ new RegExp(matcher);
439
+ return true;
440
+ } catch {
441
+ return false;
442
+ }
443
+ }
444
+
445
+ /** Whole seconds, rounded up; `undefined` when absent, `null` after a finding. */
446
+ function readTimeout(handler: JsonObject, event: string, path: string, findings: Findings): number | undefined | null {
447
+ const timeout = handler["timeout"];
448
+ if (timeout === undefined) return undefined;
449
+ if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout < 0 || timeout > 2 ** 31 - 1) {
450
+ findings.error("hooks-shape", { path, subject: event, detail: `a handler's 'timeout' must be a number of seconds, not ${describeValue(timeout)}` });
451
+ return null;
452
+ }
453
+ return Math.ceil(timeout);
454
+ }
455
+
456
+ /** The `if` as written; `undefined` when absent, `null` after a finding. */
457
+ function readCondition(handler: JsonObject, event: string, path: string, findings: Findings): string | undefined | null {
458
+ const condition = handler["if"];
459
+ if (condition === undefined) return undefined;
460
+ if (typeof condition !== "string") {
461
+ findings.error("hooks-shape", { path, subject: event, detail: "a handler's 'if' must be a string" });
462
+ return null;
463
+ }
464
+ if (!HOOK_CONDITION_PATTERN.test(condition)) {
465
+ findings.error("hook-condition-invalid", { path, subject: event, detail: condition });
466
+ return null;
467
+ }
468
+ return condition;
469
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * The components Stigmer reads past, found on disk.
3
+ *
4
+ * Every dialect ships things that configure the IDE rather than describe
5
+ * knowledge or tools: rules, hooks, slash commands, canvases, output styles,
6
+ * themes, monitors, LSP servers, Codex apps, bundled binaries, a settings
7
+ * file, a logo. Stigmer installs none of them, and says so once per
8
+ * component (a directory or a file, never per file inside), so an author
9
+ * knows what an install leaves behind. Manifest fields that name the same
10
+ * components are recorded by the dialect readers; the two lists are merged
11
+ * and deduplicated by kind and path in `read-plugin-package.ts`.
12
+ *
13
+ * `agents/` joins the list only when no vendor manifest is present: the
14
+ * open format defines no such component, so a root-manifest-only plugin
15
+ * with that folder is told it is not read rather than having semantics
16
+ * assigned to it. A `hooks/` folder in the same plugin is a hook warning
17
+ * (`hooks-not-read`, `normalise/hooks.ts`), so every surface lists it with
18
+ * the plugin's other hooks that do not run. `settings.json` is read
19
+ * for a Claude plugin (`normalise/settings.ts`) and ignored otherwise. `README`, `CHANGELOG`, `LICENSE`
20
+ * and dotfiles are not components and pass silently. Neither is `evals/`:
21
+ * it is the plugin's test suite, read by `evals/read-eval-suite.ts` and
22
+ * summarised on the install rather than reported as left behind.
23
+ */
24
+
25
+ import type { PluginFileIndex } from "../files.js";
26
+ import type { IgnoredComponent, IgnoredComponentKind } from "../types.js";
27
+
28
+ const IGNORED_DIRECTORIES: Readonly<Record<string, IgnoredComponentKind>> = {
29
+ rules: "rules",
30
+ commands: "commands",
31
+ canvases: "canvases",
32
+ workflows: "workflows",
33
+ "output-styles": "output-styles",
34
+ themes: "themes",
35
+ monitors: "monitors",
36
+ bin: "bin",
37
+ assets: "assets",
38
+ };
39
+
40
+ const IGNORED_FILES: Readonly<Record<string, IgnoredComponentKind>> = {
41
+ ".lsp.json": "lsp-servers",
42
+ "settings.json": "settings",
43
+ ".app.json": "apps",
44
+ };
45
+
46
+ /**
47
+ * `readsVendorComponents`: a vendor manifest is present, so `agents/` is
48
+ * read. `readsSettings`: a Claude manifest is present, so
49
+ * `settings.json` is read.
50
+ */
51
+ export function ignoredOnDisk(index: PluginFileIndex, readsVendorComponents: boolean, readsSettings: boolean): readonly IgnoredComponent[] {
52
+ const ignored: IgnoredComponent[] = [];
53
+ for (const dir of index.childDirectories("")) {
54
+ const kind = IGNORED_DIRECTORIES[dir];
55
+ if (kind !== undefined) ignored.push({ kind, path: `${dir}/` });
56
+ if (dir === "agents" && !readsVendorComponents) ignored.push({ kind: "agents", path: `${dir}/` });
57
+ }
58
+ for (const file of index.childFiles("")) {
59
+ const kind = IGNORED_FILES[file];
60
+ if (kind === "settings" && readsSettings) continue;
61
+ if (kind !== undefined) ignored.push({ kind, path: file });
62
+ }
63
+ return ignored;
64
+ }
65
+
66
+ /** One entry per (kind, path), in first-seen order. */
67
+ export function dedupeIgnored(lists: readonly (readonly IgnoredComponent[])[]): readonly IgnoredComponent[] {
68
+ const seen = new Set<string>();
69
+ const out: IgnoredComponent[] = [];
70
+ for (const list of lists) {
71
+ for (const component of list) {
72
+ const key = `${component.kind}\u0000${component.path}`;
73
+ if (seen.has(key)) continue;
74
+ seen.add(key);
75
+ out.push(component);
76
+ }
77
+ }
78
+ return out;
79
+ }