@kirkchinese/harness-forge 0.0.0-stage → 0.5.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.
Files changed (279) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +46 -2
  3. package/assets/icon.svg +12 -0
  4. package/cordis.patch.yml +28 -0
  5. package/docs/DETAIL.zh.md +47 -0
  6. package/lib/agents/presets.d.ts +98 -0
  7. package/lib/agents/presets.js +292 -0
  8. package/lib/agents/profiles.d.ts +58 -0
  9. package/lib/agents/profiles.js +279 -0
  10. package/lib/agents/row.d.ts +17 -0
  11. package/lib/agents/row.js +148 -0
  12. package/lib/client/paths.d.ts +12 -0
  13. package/lib/client/paths.js +68 -0
  14. package/lib/client/styles.d.ts +8 -0
  15. package/lib/client/styles.js +89 -0
  16. package/lib/client/typert.d.ts +25 -0
  17. package/lib/client/typert.js +158 -0
  18. package/lib/client.js +23823 -0
  19. package/lib/commands/definition.d.ts +40 -0
  20. package/lib/commands/definition.js +113 -0
  21. package/lib/commands/output-styles.d.ts +42 -0
  22. package/lib/commands/output-styles.js +152 -0
  23. package/lib/commands/row.d.ts +9 -0
  24. package/lib/commands/row.js +157 -0
  25. package/lib/context/instructions.d.ts +32 -0
  26. package/lib/context/instructions.js +88 -0
  27. package/lib/context/row.d.ts +16 -0
  28. package/lib/context/row.js +17 -0
  29. package/lib/dsh/commands.d.ts +30 -0
  30. package/lib/dsh/commands.js +61 -0
  31. package/lib/dsh/host.d.ts +110 -0
  32. package/lib/dsh/host.js +217 -0
  33. package/lib/dsh/model-route.d.ts +21 -0
  34. package/lib/dsh/model-route.js +37 -0
  35. package/lib/dsh/session-records.d.ts +78 -0
  36. package/lib/dsh/session-records.js +36 -0
  37. package/lib/dsh/subagents.d.ts +46 -0
  38. package/lib/dsh/subagents.js +14 -0
  39. package/lib/forge/contract.d.ts +271 -0
  40. package/lib/forge/contract.js +45 -0
  41. package/lib/forge/follow.d.ts +30 -0
  42. package/lib/forge/follow.js +73 -0
  43. package/lib/forge/ledger.d.ts +206 -0
  44. package/lib/forge/ledger.js +1005 -0
  45. package/lib/forge/live-mounts.d.ts +45 -0
  46. package/lib/forge/live-mounts.js +78 -0
  47. package/lib/forge/npm.d.ts +31 -0
  48. package/lib/forge/npm.js +60 -0
  49. package/lib/forge/projects.d.ts +97 -0
  50. package/lib/forge/projects.js +343 -0
  51. package/lib/forge/receipts.d.ts +5 -0
  52. package/lib/forge/receipts.js +31 -0
  53. package/lib/forge/remote/archive.d.ts +65 -0
  54. package/lib/forge/remote/archive.js +565 -0
  55. package/lib/forge/remote/code-parts.d.ts +18 -0
  56. package/lib/forge/remote/code-parts.js +76 -0
  57. package/lib/forge/remote/fetch.d.ts +265 -0
  58. package/lib/forge/remote/fetch.js +1133 -0
  59. package/lib/forge/remote/service.d.ts +256 -0
  60. package/lib/forge/remote/service.js +868 -0
  61. package/lib/forge/row.d.ts +32 -0
  62. package/lib/forge/row.js +59 -0
  63. package/lib/forge/service.d.ts +115 -0
  64. package/lib/forge/service.js +290 -0
  65. package/lib/forge/settings.d.ts +43 -0
  66. package/lib/forge/settings.js +122 -0
  67. package/lib/forge/state.d.ts +129 -0
  68. package/lib/forge/state.js +266 -0
  69. package/lib/forge/trust.d.ts +48 -0
  70. package/lib/forge/trust.js +107 -0
  71. package/lib/forge/turns.d.ts +13 -0
  72. package/lib/forge/turns.js +56 -0
  73. package/lib/hooks/broker/engine.d.ts +112 -0
  74. package/lib/hooks/broker/engine.js +722 -0
  75. package/lib/hooks/broker/helpers.d.ts +10 -0
  76. package/lib/hooks/broker/helpers.js +22 -0
  77. package/lib/hooks/broker/lifecycle.d.ts +65 -0
  78. package/lib/hooks/broker/lifecycle.js +342 -0
  79. package/lib/hooks/broker/session-events.d.ts +27 -0
  80. package/lib/hooks/broker/session-events.js +205 -0
  81. package/lib/hooks/broker/tools.d.ts +52 -0
  82. package/lib/hooks/broker/tools.js +424 -0
  83. package/lib/hooks/broker/types.d.ts +264 -0
  84. package/lib/hooks/broker/types.js +1 -0
  85. package/lib/hooks/broker.d.ts +80 -0
  86. package/lib/hooks/broker.js +95 -0
  87. package/lib/hooks/codex-user.d.ts +5 -0
  88. package/lib/hooks/codex-user.js +109 -0
  89. package/lib/hooks/condition.d.ts +87 -0
  90. package/lib/hooks/condition.js +602 -0
  91. package/lib/hooks/config.d.ts +85 -0
  92. package/lib/hooks/config.js +154 -0
  93. package/lib/hooks/glob.d.ts +41 -0
  94. package/lib/hooks/glob.js +114 -0
  95. package/lib/hooks/matcher.d.ts +20 -0
  96. package/lib/hooks/matcher.js +39 -0
  97. package/lib/hooks/notice.d.ts +17 -0
  98. package/lib/hooks/notice.js +39 -0
  99. package/lib/hooks/output.d.ts +56 -0
  100. package/lib/hooks/output.js +214 -0
  101. package/lib/hooks/permissions.d.ts +19 -0
  102. package/lib/hooks/permissions.js +202 -0
  103. package/lib/hooks/powershell-aliases.d.ts +19 -0
  104. package/lib/hooks/powershell-aliases.js +58 -0
  105. package/lib/hooks/process.d.ts +64 -0
  106. package/lib/hooks/process.js +289 -0
  107. package/lib/hooks/row.d.ts +17 -0
  108. package/lib/hooks/row.js +96 -0
  109. package/lib/hooks/runner.d.ts +98 -0
  110. package/lib/hooks/runner.js +322 -0
  111. package/lib/hooks/shell-commands.d.ts +28 -0
  112. package/lib/hooks/shell-commands.js +241 -0
  113. package/lib/hooks/shell.d.ts +44 -0
  114. package/lib/hooks/shell.js +129 -0
  115. package/lib/hooks/transcript.d.ts +28 -0
  116. package/lib/hooks/transcript.js +141 -0
  117. package/lib/hooks/vocabulary.d.ts +57 -0
  118. package/lib/hooks/vocabulary.js +366 -0
  119. package/lib/index.d.ts +31 -0
  120. package/lib/index.js +287 -0
  121. package/lib/mcp/auth.d.ts +35 -0
  122. package/lib/mcp/auth.js +178 -0
  123. package/lib/mcp/catalog.d.ts +39 -0
  124. package/lib/mcp/catalog.js +69 -0
  125. package/lib/mcp/mount.d.ts +113 -0
  126. package/lib/mcp/mount.js +317 -0
  127. package/lib/mcp/policy.d.ts +42 -0
  128. package/lib/mcp/policy.js +43 -0
  129. package/lib/mcp/remote.d.ts +45 -0
  130. package/lib/mcp/remote.js +133 -0
  131. package/lib/mcp/row.d.ts +9 -0
  132. package/lib/mcp/row.js +225 -0
  133. package/lib/mods/bundler.d.ts +38 -0
  134. package/lib/mods/bundler.js +157 -0
  135. package/lib/mods/chain.d.ts +103 -0
  136. package/lib/mods/chain.js +436 -0
  137. package/lib/mods/claude.d.ts +27 -0
  138. package/lib/mods/claude.js +56 -0
  139. package/lib/mods/engine.d.ts +160 -0
  140. package/lib/mods/engine.js +701 -0
  141. package/lib/mods/expect.d.ts +8 -0
  142. package/lib/mods/expect.js +248 -0
  143. package/lib/mods/host.d.ts +105 -0
  144. package/lib/mods/host.js +462 -0
  145. package/lib/mods/loader.d.ts +53 -0
  146. package/lib/mods/loader.js +231 -0
  147. package/lib/mods/ops.d.ts +13 -0
  148. package/lib/mods/ops.js +117 -0
  149. package/lib/mods/row.d.ts +9 -0
  150. package/lib/mods/row.js +77 -0
  151. package/lib/mods/runtime.d.ts +60 -0
  152. package/lib/mods/runtime.js +133 -0
  153. package/lib/mods/service/commands.d.ts +17 -0
  154. package/lib/mods/service/commands.js +121 -0
  155. package/lib/mods/service/drawings.d.ts +36 -0
  156. package/lib/mods/service/drawings.js +64 -0
  157. package/lib/mods/service/sessions.d.ts +63 -0
  158. package/lib/mods/service/sessions.js +345 -0
  159. package/lib/mods/service/tools.d.ts +32 -0
  160. package/lib/mods/service/tools.js +253 -0
  161. package/lib/mods/service/turns.d.ts +33 -0
  162. package/lib/mods/service/turns.js +136 -0
  163. package/lib/mods/service/types.d.ts +151 -0
  164. package/lib/mods/service/types.js +1 -0
  165. package/lib/mods/service.d.ts +72 -0
  166. package/lib/mods/service.js +104 -0
  167. package/lib/mods/surfaces.d.ts +93 -0
  168. package/lib/mods/surfaces.js +309 -0
  169. package/lib/mods/values.d.ts +30 -0
  170. package/lib/mods/values.js +95 -0
  171. package/lib/opencode/bun.d.ts +40 -0
  172. package/lib/opencode/bun.js +168 -0
  173. package/lib/opencode/convert.d.ts +53 -0
  174. package/lib/opencode/convert.js +255 -0
  175. package/lib/opencode/row.d.ts +22 -0
  176. package/lib/opencode/row.js +104 -0
  177. package/lib/opencode/runtime/core.d.ts +74 -0
  178. package/lib/opencode/runtime/core.js +249 -0
  179. package/lib/opencode/runtime/events.d.ts +80 -0
  180. package/lib/opencode/runtime/events.js +281 -0
  181. package/lib/opencode/runtime/mount.d.ts +27 -0
  182. package/lib/opencode/runtime/mount.js +283 -0
  183. package/lib/opencode/runtime/server.d.ts +27 -0
  184. package/lib/opencode/runtime/server.js +354 -0
  185. package/lib/opencode/runtime/types.d.ts +109 -0
  186. package/lib/opencode/runtime/types.js +1 -0
  187. package/lib/opencode/runtime.d.ts +55 -0
  188. package/lib/opencode/runtime.js +79 -0
  189. package/lib/porting/adapter-paths.d.ts +13 -0
  190. package/lib/porting/adapter-paths.js +120 -0
  191. package/lib/porting/port-gap.d.ts +36 -0
  192. package/lib/porting/port-gap.js +158 -0
  193. package/lib/porting/port-sessions.d.ts +41 -0
  194. package/lib/porting/port-sessions.js +98 -0
  195. package/lib/porting/prompt.d.ts +23 -0
  196. package/lib/porting/prompt.js +51 -0
  197. package/lib/porting/row.d.ts +16 -0
  198. package/lib/porting/row.js +27 -0
  199. package/lib/porting/verify-port.d.ts +46 -0
  200. package/lib/porting/verify-port.js +82 -0
  201. package/lib/presets/fs-search.d.ts +25 -0
  202. package/lib/presets/fs-search.js +17 -0
  203. package/lib/presets/tool-filter.d.ts +35 -0
  204. package/lib/presets/tool-filter.js +91 -0
  205. package/lib/sessions/chat-import.d.ts +48 -0
  206. package/lib/sessions/chat-import.js +44 -0
  207. package/lib/sessions/claude.d.ts +35 -0
  208. package/lib/sessions/claude.js +104 -0
  209. package/lib/sessions/codex-discover.d.ts +34 -0
  210. package/lib/sessions/codex-discover.js +205 -0
  211. package/lib/sessions/codex-meta.d.ts +63 -0
  212. package/lib/sessions/codex-meta.js +148 -0
  213. package/lib/sessions/codex-text.d.ts +9 -0
  214. package/lib/sessions/codex-text.js +44 -0
  215. package/lib/sessions/codex.d.ts +42 -0
  216. package/lib/sessions/codex.js +270 -0
  217. package/lib/sessions/opencode.d.ts +38 -0
  218. package/lib/sessions/opencode.js +339 -0
  219. package/lib/sessions/persist.d.ts +162 -0
  220. package/lib/sessions/persist.js +377 -0
  221. package/lib/sessions/row.d.ts +15 -0
  222. package/lib/sessions/row.js +101 -0
  223. package/lib/sessions/turns.d.ts +31 -0
  224. package/lib/sessions/turns.js +63 -0
  225. package/lib/sessions/zstd.d.ts +15 -0
  226. package/lib/sessions/zstd.js +152 -0
  227. package/lib/shared/invocation.d.ts +169 -0
  228. package/lib/shared/invocation.js +325 -0
  229. package/lib/shared/render.d.ts +101 -0
  230. package/lib/shared/render.js +1353 -0
  231. package/lib/shared/util.d.ts +21 -0
  232. package/lib/shared/util.js +32 -0
  233. package/lib/skills/bundled.d.ts +3 -0
  234. package/lib/skills/bundled.js +42 -0
  235. package/lib/skills/provider.d.ts +74 -0
  236. package/lib/skills/provider.js +346 -0
  237. package/lib/skills/row.d.ts +22 -0
  238. package/lib/skills/row.js +204 -0
  239. package/lib/workbench/adapter-coverage.d.ts +65 -0
  240. package/lib/workbench/adapter-coverage.js +141 -0
  241. package/lib/workbench/model-tools.d.ts +10 -0
  242. package/lib/workbench/model-tools.js +122 -0
  243. package/lib/workbench/overview.d.ts +153 -0
  244. package/lib/workbench/overview.js +246 -0
  245. package/lib/workbench/remote.d.ts +161 -0
  246. package/lib/workbench/remote.js +561 -0
  247. package/lib/workbench/row.d.ts +15 -0
  248. package/lib/workbench/row.js +16 -0
  249. package/lib/workbench/settings.d.ts +19 -0
  250. package/lib/workbench/settings.js +60 -0
  251. package/lib/workbench/workbench.d.ts +130 -0
  252. package/lib/workbench/workbench.js +361 -0
  253. package/locale/agents/en.json +6 -0
  254. package/locale/agents/zh.json +6 -0
  255. package/locale/commands/en.json +6 -0
  256. package/locale/commands/zh.json +6 -0
  257. package/locale/context/en.json +6 -0
  258. package/locale/context/zh.json +6 -0
  259. package/locale/en.json +6 -0
  260. package/locale/hooks/en.json +6 -0
  261. package/locale/hooks/zh.json +6 -0
  262. package/locale/mcp/en.json +6 -0
  263. package/locale/mcp/zh.json +6 -0
  264. package/locale/mods/en.json +6 -0
  265. package/locale/mods/zh.json +6 -0
  266. package/locale/opencode/en.json +6 -0
  267. package/locale/opencode/zh.json +6 -0
  268. package/locale/porter/en.json +6 -0
  269. package/locale/porter/zh.json +6 -0
  270. package/locale/sessions/en.json +6 -0
  271. package/locale/sessions/zh.json +6 -0
  272. package/locale/skills/en.json +6 -0
  273. package/locale/skills/zh.json +6 -0
  274. package/locale/workbench/en.json +6 -0
  275. package/locale/workbench/zh.json +6 -0
  276. package/locale/zh.json +7 -0
  277. package/package.json +247 -3
  278. package/runtime/opencode-host.mjs +270 -0
  279. package/skills/harness-forge/SKILL.md +47 -0
@@ -0,0 +1,85 @@
1
+ import { type ForeignPlugin } from '@kirkchinese/harness-forge-core';
2
+ /** Fields every handler type accepts. */
3
+ interface HandlerCommon {
4
+ /** The `if` permission rule. */
5
+ readonly condition?: string;
6
+ readonly timeoutSec?: number;
7
+ readonly statusMessage?: string;
8
+ /** Only honored for skill-frontmatter hooks. */
9
+ readonly once?: boolean;
10
+ }
11
+ /** A `type: "command"` handler. */
12
+ export interface CommandHandler extends HandlerCommon {
13
+ readonly type: 'command';
14
+ readonly command: string;
15
+ /** Exec form when present: `command` is spawned directly with these arguments. */
16
+ readonly args?: readonly string[];
17
+ readonly shell?: 'bash' | 'powershell';
18
+ readonly async?: boolean;
19
+ readonly asyncRewake?: boolean;
20
+ readonly rewakeMessage?: string;
21
+ readonly onFailure?: 'continue' | 'block';
22
+ /** Codex: Windows-only command override. */
23
+ readonly commandWindows?: string;
24
+ /** Codex: approximate token threshold before additionalContext spills to a file; 0 disables it. */
25
+ readonly additionalContextLimit?: number;
26
+ }
27
+ /** A `type: "http"` handler. */
28
+ export interface HttpHandler extends HandlerCommon {
29
+ readonly type: 'http';
30
+ readonly url: string;
31
+ readonly headers?: Readonly<Record<string, string>>;
32
+ readonly allowedEnvVars?: readonly string[];
33
+ readonly onFailure?: 'continue' | 'block';
34
+ }
35
+ /** A `type: "mcp_tool"` handler. */
36
+ export interface McpToolHandler extends HandlerCommon {
37
+ readonly type: 'mcp_tool';
38
+ readonly server: string;
39
+ readonly tool: string;
40
+ readonly input?: unknown;
41
+ }
42
+ /** A `type: "prompt"` or `type: "agent"` handler. */
43
+ export interface ModelHandler extends HandlerCommon {
44
+ readonly type: 'prompt' | 'agent';
45
+ readonly prompt: string;
46
+ readonly model?: string;
47
+ readonly continueOnBlock?: boolean;
48
+ }
49
+ /** A handler the configuration does not describe validly. */
50
+ export interface InvalidHandler extends HandlerCommon {
51
+ readonly type: 'invalid';
52
+ readonly reason: string;
53
+ }
54
+ /** One configured handler. */
55
+ export type HookHandler = CommandHandler | HttpHandler | McpToolHandler | ModelHandler | InvalidHandler;
56
+ /** One matcher group. */
57
+ export interface HookGroup {
58
+ readonly matcher?: string;
59
+ readonly handlers: readonly HookHandler[];
60
+ }
61
+ /** Event name → matcher groups, in configuration order. */
62
+ export type HookTable = ReadonlyMap<string, readonly HookGroup[]>;
63
+ /** Read a hooks file or settings file. */
64
+ export declare function readHookTable(path: string): HookTable;
65
+ /**
66
+ * Parse an already-loaded hooks value: `{ hooks: { Event: [...] } }` or the
67
+ * bare `{ Event: [...] }` table (frontmatter form).
68
+ */
69
+ export declare function parseHookTable(raw: unknown): HookTable;
70
+ /** Every handler type a table uses, for the compatibility report. */
71
+ export declare function handlerTypes(table: HookTable): string[];
72
+ /** A hook source for one plugin's hooks file or one skill's, agent's or command's frontmatter. */
73
+ export declare function hookSource(componentId: string, plugin: ForeignPlugin, dataDir: string, table: HookTable, origin: 'plugin' | 'settings' | 'skill' | 'agent'): HookConfigSource;
74
+ /** One hooks configuration with where it comes from: what the hooks row runs. */
75
+ export interface HookConfigSource {
76
+ readonly componentId: string;
77
+ readonly pluginId: string;
78
+ readonly dialect: 'claude-code' | 'codex';
79
+ readonly pluginRoot: string;
80
+ readonly dataDir: string;
81
+ readonly table: HookTable;
82
+ readonly origin?: 'plugin' | 'settings' | 'skill' | 'agent';
83
+ readonly options?: Readonly<Record<string, string>>;
84
+ }
85
+ export {};
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Parse a foreign hooks configuration (a Claude Code or Codex `hooks.json`, a
3
+ * settings file's `hooks` key, an inline `plugin.json#hooks` object, or a
4
+ * skill's or agent's frontmatter `hooks`) into the broker's handler model,
5
+ * keeping every field the hook reference defines.
6
+ * @module @kirkchinese/harness-forge
7
+ */
8
+ import { readFileSync } from 'node:fs';
9
+ import { LEADS_TO_SHARE, leadsToShare } from '@kirkchinese/harness-forge-core';
10
+ /** Read a hooks file or settings file. */
11
+ export function readHookTable(path) {
12
+ // A file on a link to a network share it is not on fails as unreadable (see `leadsToShare`).
13
+ if (leadsToShare(path))
14
+ throw new Error(`${path} ${LEADS_TO_SHARE}`);
15
+ return parseHookTable(JSON.parse(readFileSync(path, 'utf8').replace(/^\uFEFF/u, '')));
16
+ }
17
+ /**
18
+ * Parse an already-loaded hooks value: `{ hooks: { Event: [...] } }` or the
19
+ * bare `{ Event: [...] }` table (frontmatter form).
20
+ */
21
+ export function parseHookTable(raw) {
22
+ const root = record(raw);
23
+ const hooks = record(root?.['hooks']) ?? root ?? {};
24
+ const table = new Map();
25
+ for (const [event, value] of Object.entries(hooks)) {
26
+ if (!Array.isArray(value))
27
+ continue;
28
+ const groups = [];
29
+ for (const entry of value) {
30
+ const group = record(entry);
31
+ if (group === undefined)
32
+ continue;
33
+ const handlers = Array.isArray(group['hooks']) ? group['hooks'].map(parseHandler) : [];
34
+ const matcher = typeof group['matcher'] === 'string' ? group['matcher'] : undefined;
35
+ groups.push({ ...(matcher !== undefined ? { matcher } : {}), handlers });
36
+ }
37
+ if (groups.length > 0)
38
+ table.set(event, groups);
39
+ }
40
+ return table;
41
+ }
42
+ /** Every handler type a table uses, for the compatibility report. */
43
+ export function handlerTypes(table) {
44
+ const types = new Set();
45
+ for (const groups of table.values())
46
+ for (const group of groups)
47
+ for (const handler of group.handlers)
48
+ types.add(handler.type);
49
+ return [...types];
50
+ }
51
+ function parseHandler(value) {
52
+ const handler = record(value) ?? {};
53
+ const common = commonFields(handler);
54
+ const type = handler['type'];
55
+ switch (type) {
56
+ case 'command': {
57
+ if (typeof handler['command'] !== 'string' || handler['command'].length === 0)
58
+ return invalid(common, 'command handler has no "command"');
59
+ const args = handler['args'];
60
+ const shell = handler['shell'];
61
+ const onFailure = handler['onFailure'];
62
+ const limit = handler['additionalContextLimit'];
63
+ const windows = handler['commandWindows'] ?? handler['command_windows'];
64
+ return {
65
+ ...common,
66
+ type,
67
+ command: handler['command'],
68
+ ...(Array.isArray(args) ? { args: args.map(item => String(item)) } : {}),
69
+ ...(shell === 'bash' || shell === 'powershell' ? { shell } : {}),
70
+ ...(handler['async'] === true ? { async: true } : {}),
71
+ ...(handler['asyncRewake'] === true ? { asyncRewake: true } : {}),
72
+ ...(typeof handler['rewakeMessage'] === 'string' ? { rewakeMessage: handler['rewakeMessage'] } : {}),
73
+ ...(onFailure === 'continue' || onFailure === 'block' ? { onFailure } : {}),
74
+ ...(typeof windows === 'string' && windows.length > 0 ? { commandWindows: windows } : {}),
75
+ ...(typeof limit === 'number' && limit >= 0 ? { additionalContextLimit: limit } : {}),
76
+ };
77
+ }
78
+ case 'http': {
79
+ if (typeof handler['url'] !== 'string' || handler['url'].length === 0)
80
+ return invalid(common, 'http handler has no "url"');
81
+ const headers = record(handler['headers']);
82
+ const allowed = handler['allowedEnvVars'];
83
+ const onFailure = handler['onFailure'];
84
+ return {
85
+ ...common,
86
+ type,
87
+ url: handler['url'],
88
+ ...(headers !== undefined ? { headers: Object.fromEntries(Object.entries(headers).map(([key, item]) => [key, String(item)])) } : {}),
89
+ ...(Array.isArray(allowed) ? { allowedEnvVars: allowed.map(item => String(item)) } : {}),
90
+ ...(onFailure === 'continue' || onFailure === 'block' ? { onFailure } : {}),
91
+ };
92
+ }
93
+ case 'mcp_tool': {
94
+ if (typeof handler['server'] !== 'string' || typeof handler['tool'] !== 'string')
95
+ return invalid(common, 'mcp_tool handler needs "server" and "tool"');
96
+ return { ...common, type, server: handler['server'], tool: handler['tool'], ...('input' in handler ? { input: handler['input'] } : {}) };
97
+ }
98
+ case 'prompt':
99
+ case 'agent': {
100
+ if (typeof handler['prompt'] !== 'string' || handler['prompt'].length === 0)
101
+ return invalid(common, `${type} handler has no "prompt"`);
102
+ return {
103
+ ...common,
104
+ type,
105
+ prompt: handler['prompt'],
106
+ ...(typeof handler['model'] === 'string' ? { model: handler['model'] } : {}),
107
+ ...(handler['continueOnBlock'] === true ? { continueOnBlock: true } : {}),
108
+ };
109
+ }
110
+ default:
111
+ return invalid(common, typeof type === 'string' ? `unknown handler type "${type}"` : 'handler has no "type"');
112
+ }
113
+ }
114
+ function commonFields(handler) {
115
+ const timeout = handler['timeout'];
116
+ const condition = handler['if'];
117
+ return {
118
+ ...(typeof condition === 'string' && condition.trim().length > 0 ? { condition: condition.trim() } : {}),
119
+ ...(typeof timeout === 'number' && timeout > 0 ? { timeoutSec: timeout } : {}),
120
+ ...(typeof handler['statusMessage'] === 'string' ? { statusMessage: handler['statusMessage'] } : {}),
121
+ ...(handler['once'] === true ? { once: true } : {}),
122
+ };
123
+ }
124
+ function invalid(common, reason) {
125
+ return { ...common, type: 'invalid', reason };
126
+ }
127
+ function record(value) {
128
+ return typeof value === 'object' && value !== null && !Array.isArray(value) ? value : undefined;
129
+ }
130
+ /** A hook source for one plugin's hooks file or one skill's, agent's or command's frontmatter. */
131
+ export function hookSource(componentId, plugin, dataDir, table, origin) {
132
+ return {
133
+ componentId,
134
+ pluginId: plugin.id,
135
+ dialect: plugin.source === 'codex' ? 'codex' : 'claude-code',
136
+ pluginRoot: plugin.root,
137
+ dataDir,
138
+ table,
139
+ origin,
140
+ options: userConfigDefaults(plugin.manifest['userConfig']),
141
+ };
142
+ }
143
+ /** Plugin `userConfig` option values: the declared defaults. */
144
+ function userConfigDefaults(value) {
145
+ if (typeof value !== 'object' || value === null || Array.isArray(value))
146
+ return {};
147
+ const out = {};
148
+ for (const [key, option] of Object.entries(value)) {
149
+ const fallback = typeof option === 'object' && option !== null ? option.default : undefined;
150
+ if (fallback !== undefined && fallback !== null)
151
+ out[key] = typeof fallback === 'string' ? fallback : JSON.stringify(fallback);
152
+ }
153
+ return out;
154
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Wildcard patterns matched in O(text × pattern) time, within a work budget.
3
+ * A wildcard rule turned into a regular expression (`*` → `.*`) backtracks
4
+ * polynomially in the number of wildcards, and rules and the text they match
5
+ * both come from outside; this matcher walks every reachable pattern position
6
+ * at once instead. That still runs on the synchronous permission path, so a
7
+ * pattern, text or text × pattern product beyond the budget below is not
8
+ * matched at all and answers undefined: the caller decides which way an
9
+ * unknown answer falls (deny, ask and hook conditions match: they fail closed).
10
+ * @module @kirkchinese/harness-forge
11
+ */
12
+ type Test = (char: string) => boolean;
13
+ /** At most this many pattern positions… */
14
+ export declare const GLOB_PATTERN_LIMIT = 1000;
15
+ /** …this many characters of text… */
16
+ export declare const GLOB_TEXT_LIMIT = 100000;
17
+ /** …and this much work (text × pattern) for one match. */
18
+ export declare const GLOB_WORK_LIMIT = 4000000;
19
+ /** A pattern built piece by piece; `test` matches a whole text. */
20
+ export declare class Glob {
21
+ private readonly nodes;
22
+ /** Set for a pattern refused before it was built: it matches nothing and answers undefined. */
23
+ private oversized;
24
+ /** A pattern too long to build: every match is undefined. Check {@link tooLong} before building one. */
25
+ static refused(): Glob;
26
+ /** Whether a pattern of this text is beyond the budget, known before anything is built (no pattern character makes more than one position). */
27
+ static tooLong(pattern: string): boolean;
28
+ /** These characters, exactly. */
29
+ literal(text: string): this;
30
+ /** One character that passes `test`. */
31
+ one(test?: Test): this;
32
+ /** Any number of characters that pass `test`. */
33
+ many(test?: Test): this;
34
+ /** What `build` adds, or nothing. */
35
+ optional(build: (glob: this) => void): this;
36
+ /** Whether the whole text matches; undefined when the pattern or the text is beyond the budget. */
37
+ match(text: string): boolean | undefined;
38
+ }
39
+ /** `*` any run of characters, everything else literal. */
40
+ export declare function wildcard(pattern: string, star?: Test): Glob;
41
+ export {};
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Wildcard patterns matched in O(text × pattern) time, within a work budget.
3
+ * A wildcard rule turned into a regular expression (`*` → `.*`) backtracks
4
+ * polynomially in the number of wildcards, and rules and the text they match
5
+ * both come from outside; this matcher walks every reachable pattern position
6
+ * at once instead. That still runs on the synchronous permission path, so a
7
+ * pattern, text or text × pattern product beyond the budget below is not
8
+ * matched at all and answers undefined: the caller decides which way an
9
+ * unknown answer falls (deny, ask and hook conditions match: they fail closed).
10
+ * @module @kirkchinese/harness-forge
11
+ */
12
+ const ANY = () => true;
13
+ /** At most this many pattern positions… */
14
+ export const GLOB_PATTERN_LIMIT = 1_000;
15
+ /** …this many characters of text… */
16
+ export const GLOB_TEXT_LIMIT = 100_000;
17
+ /** …and this much work (text × pattern) for one match. */
18
+ export const GLOB_WORK_LIMIT = 4_000_000;
19
+ /** A pattern built piece by piece; `test` matches a whole text. */
20
+ export class Glob {
21
+ nodes = [];
22
+ /** Set for a pattern refused before it was built: it matches nothing and answers undefined. */
23
+ oversized = false;
24
+ /** A pattern too long to build: every match is undefined. Check {@link tooLong} before building one. */
25
+ static refused() {
26
+ const glob = new Glob();
27
+ glob.oversized = true;
28
+ return glob;
29
+ }
30
+ /** Whether a pattern of this text is beyond the budget, known before anything is built (no pattern character makes more than one position). */
31
+ static tooLong(pattern) {
32
+ return pattern.length > GLOB_PATTERN_LIMIT;
33
+ }
34
+ /** These characters, exactly. */
35
+ literal(text) {
36
+ for (const char of text)
37
+ this.nodes.push({ kind: 'one', test: other => other === char });
38
+ return this;
39
+ }
40
+ /** One character that passes `test`. */
41
+ one(test = ANY) {
42
+ this.nodes.push({ kind: 'one', test });
43
+ return this;
44
+ }
45
+ /** Any number of characters that pass `test`. */
46
+ many(test = ANY) {
47
+ this.nodes.push({ kind: 'many', test });
48
+ return this;
49
+ }
50
+ /** What `build` adds, or nothing. */
51
+ optional(build) {
52
+ const skip = { kind: 'skip', to: 0 };
53
+ this.nodes.push(skip);
54
+ build(this);
55
+ skip.to = this.nodes.length;
56
+ return this;
57
+ }
58
+ /** Whether the whole text matches; undefined when the pattern or the text is beyond the budget. */
59
+ match(text) {
60
+ const end = this.nodes.length;
61
+ if (this.oversized || end > GLOB_PATTERN_LIMIT || text.length > GLOB_TEXT_LIMIT || (end + 1) * text.length > GLOB_WORK_LIMIT)
62
+ return undefined;
63
+ let current = new Uint8Array(end + 1);
64
+ let next = new Uint8Array(end + 1);
65
+ const add = (set, start) => {
66
+ const stack = [start];
67
+ while (stack.length > 0) {
68
+ const at = stack.pop() ?? end;
69
+ if (set[at] === 1)
70
+ continue;
71
+ set[at] = 1;
72
+ const node = this.nodes[at];
73
+ if (node === undefined)
74
+ continue;
75
+ if (node.kind === 'skip')
76
+ stack.push(at + 1, node.to);
77
+ else if (node.kind === 'many')
78
+ stack.push(at + 1);
79
+ }
80
+ };
81
+ add(current, 0);
82
+ for (const char of text) {
83
+ next.fill(0);
84
+ let alive = false;
85
+ for (let at = 0; at < end; at++) {
86
+ if (current[at] !== 1)
87
+ continue;
88
+ const node = this.nodes[at];
89
+ if (node === undefined || node.kind === 'skip' || !node.test(char))
90
+ continue;
91
+ add(next, node.kind === 'one' ? at + 1 : at);
92
+ alive = true;
93
+ }
94
+ if (!alive)
95
+ return false;
96
+ const swap = current;
97
+ current = next;
98
+ next = swap;
99
+ }
100
+ return current[end] === 1;
101
+ }
102
+ }
103
+ /** `*` any run of characters, everything else literal. */
104
+ export function wildcard(pattern, star = ANY) {
105
+ if (Glob.tooLong(pattern))
106
+ return Glob.refused();
107
+ const glob = new Glob();
108
+ pattern.split('*').forEach((part, index) => {
109
+ if (index > 0)
110
+ glob.many(star);
111
+ glob.literal(part);
112
+ });
113
+ return glob;
114
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Matcher-group selection, as each harness defines it.
3
+ *
4
+ * Claude Code: `*`, `""` or no matcher match everything; a matcher made only
5
+ * of letters, digits, `_`, `-`, spaces, `,` and `|` is a list of exact
6
+ * strings (`FileChanged` and `StopFailure` use the narrower `[A-Za-z0-9_|]`
7
+ * set); anything else is an unanchored JavaScript regular expression. Events
8
+ * without matcher support ignore the matcher.
9
+ *
10
+ * Codex: the matcher is always a regular expression, tested against the tool
11
+ * name and its aliases.
12
+ * @module @kirkchinese/harness-forge
13
+ */
14
+ import type { HookDialect } from './vocabulary.js';
15
+ /**
16
+ * Whether a matcher group runs for this occurrence.
17
+ * @param subjects - the value the event matches on; tool events pass every
18
+ * name the tool answers to.
19
+ */
20
+ export declare function matcherSelects(dialect: HookDialect, event: string, matcher: string | undefined, subjects: readonly string[]): boolean;
@@ -0,0 +1,39 @@
1
+ /** Claude Code events whose matcher is ignored. */
2
+ const CLAUDE_UNMATCHED = new Set([
3
+ 'UserPromptSubmit', 'PostToolBatch', 'Stop', 'TeammateIdle', 'TaskCreated', 'TaskCompleted',
4
+ 'WorktreeCreate', 'WorktreeRemove', 'MessageDisplay', 'CwdChanged',
5
+ ]);
6
+ /** Codex events whose matcher is ignored. */
7
+ const CODEX_UNMATCHED = new Set(['UserPromptSubmit', 'Stop', 'Interrupt', 'PreModelRequest', 'PostModelRequest']);
8
+ const NARROW_EXACT = new Set(['FileChanged', 'StopFailure']);
9
+ /**
10
+ * Whether a matcher group runs for this occurrence.
11
+ * @param subjects - the value the event matches on; tool events pass every
12
+ * name the tool answers to.
13
+ */
14
+ export function matcherSelects(dialect, event, matcher, subjects) {
15
+ if (matcher === undefined || matcher === '' || matcher === '*')
16
+ return true;
17
+ if ((dialect === 'codex' ? CODEX_UNMATCHED : CLAUDE_UNMATCHED).has(event))
18
+ return true;
19
+ if (dialect === 'claude-code') {
20
+ const exact = NARROW_EXACT.has(event) ? /^[A-Za-z0-9_|]+$/u : /^[A-Za-z0-9_\- ,|]+$/u;
21
+ if (exact.test(matcher)) {
22
+ const options = matcher.split(NARROW_EXACT.has(event) ? /\|/u : /[|,]/u).map(item => item.trim()).filter(item => item.length > 0);
23
+ return subjects.some(subject => options.includes(subject));
24
+ }
25
+ }
26
+ let pattern;
27
+ try {
28
+ pattern = new RegExp(matcher, 'u');
29
+ }
30
+ catch {
31
+ try {
32
+ pattern = new RegExp(matcher);
33
+ }
34
+ catch {
35
+ return false;
36
+ }
37
+ }
38
+ return subjects.some(subject => pattern.test(subject));
39
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * What hooks show the user rather than the model — a `systemMessage`, a
3
+ * `<event> hook error` notice, a block or stop reason, a terminal
4
+ * notification — recorded in the session as `plugin:harness-forge/notice`
5
+ * records. A reader that does not know the record skips it; the Harness
6
+ * Forge client renders it in the conversation.
7
+ * @module @kirkchinese/harness-forge
8
+ */
9
+ import { type HookNotice } from '../dsh/session-records.js';
10
+ export type { HookNotice, HookOutputRecord } from '../dsh/session-records.js';
11
+ /** Record one notice in a session; a session that refuses the record loses only the notice. */
12
+ export declare function appendNotice(session: unknown, notice: HookNotice): void;
13
+ /** Parse the notification a `terminalSequence` carries (OSC 9, 99, 777 and titles). */
14
+ export declare function terminalNotification(sequence: string): {
15
+ title?: string;
16
+ body: string;
17
+ } | undefined;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * What hooks show the user rather than the model — a `systemMessage`, a
3
+ * `<event> hook error` notice, a block or stop reason, a terminal
4
+ * notification — recorded in the session as `plugin:harness-forge/notice`
5
+ * records. A reader that does not know the record skips it; the Harness
6
+ * Forge client renders it in the conversation.
7
+ * @module @kirkchinese/harness-forge
8
+ */
9
+ import { appendRecord } from '../dsh/session-records.js';
10
+ /** Record one notice in a session; a session that refuses the record loses only the notice. */
11
+ export function appendNotice(session, notice) {
12
+ try {
13
+ appendRecord(session, 'plugin:harness-forge/notice', notice);
14
+ }
15
+ catch {
16
+ // The notice is informational.
17
+ }
18
+ }
19
+ /** Parse the notification a `terminalSequence` carries (OSC 9, 99, 777 and titles). */
20
+ export function terminalNotification(sequence) {
21
+ // eslint-disable-next-line no-control-regex
22
+ const osc = /\u001b\](\d+);([^\u0007\u001b]*)(?:\u0007|\u001b\\)/gu;
23
+ for (const match of sequence.matchAll(osc)) {
24
+ const code = match[1];
25
+ const payload = match[2] ?? '';
26
+ if (code === '777') {
27
+ const [, title, ...body] = payload.split(';');
28
+ return { ...(title !== undefined ? { title } : {}), body: body.join(';') };
29
+ }
30
+ if (code === '9' && !payload.startsWith('4;'))
31
+ return { body: payload };
32
+ if (code === '99')
33
+ return { body: payload.replace(/^[^;]*;/u, '') };
34
+ if (code === '0' || code === '2')
35
+ return { title: payload, body: '' };
36
+ }
37
+ // eslint-disable-next-line no-control-regex
38
+ return sequence.includes('\u0007') ? { body: '' } : undefined;
39
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Decode one hook run into what its harness would act on: the stdout
3
+ * classification (JSON object, parse failure, plain text), the JSON output
4
+ * schema check, and the exit-code table of the hook reference.
5
+ *
6
+ * | stdout \ exit | 0 | 2 | other |
7
+ * | valid JSON object | success, fields apply | block; fields still read | success; fields decide (failure for `onFailure`) |
8
+ * | invalid / bad schema | non-blocking error | block; stderr is the reason | non-blocking error |
9
+ * | plain text or nothing | success | block; stderr is the reason | non-blocking error |
10
+ * @module @kirkchinese/harness-forge
11
+ */
12
+ import type { HookDialect } from './vocabulary.js';
13
+ /** The raw outcome of one handler, whatever its type. */
14
+ export interface RawRun {
15
+ /** Exit status: a process exit code, 0 for a 2xx HTTP response or a successful tool/model call. */
16
+ readonly exitCode?: number;
17
+ readonly stdout: string;
18
+ readonly stderr: string;
19
+ /** The handler could not produce a result. */
20
+ readonly failure?: {
21
+ readonly kind: 'start' | 'timeout' | 'transport' | 'cancelled';
22
+ readonly message: string;
23
+ };
24
+ /** The process exited while a child still held its output (see {@link ../process}). */
25
+ readonly suspectCapture?: boolean;
26
+ }
27
+ /** What a harness acts on after one run. */
28
+ export interface Decoded {
29
+ /** Validated JSON output fields. */
30
+ readonly json?: Readonly<Record<string, unknown>>;
31
+ /** Plain stdout (only on success). */
32
+ readonly plain?: string;
33
+ /** The run blocks the action (exit 2): its reason. */
34
+ readonly block?: string;
35
+ /** The run is a hook error, with its notice text. */
36
+ readonly error?: string;
37
+ /** The run counts as a failure for `onFailure: "block"`. */
38
+ readonly failed: boolean;
39
+ /** The run was cancelled; it has no effect. */
40
+ readonly cancelled?: boolean;
41
+ }
42
+ /** Decode a synchronous run. */
43
+ export declare function decodeRun(event: string, dialect: HookDialect, run: RawRun): Decoded;
44
+ /**
45
+ * Decode a background (`async`) run: the JSON response may span stdout alone
46
+ * or sit on one line among other text; fields of the wrong type are dropped.
47
+ */
48
+ export declare function decodeAsyncRun(event: string, dialect: HookDialect, run: RawRun): Decoded;
49
+ /** Whether plain stdout of a successful run becomes context on this event. */
50
+ export declare function plainIsContext(event: string, dialect: HookDialect): boolean;
51
+ /** The `hookSpecificOutput` object of a decoded run. */
52
+ export declare function specific(decoded: Decoded): Readonly<Record<string, unknown>> | undefined;
53
+ /** The context strings a run adds: `additionalContext`, plus plain stdout where the event reads it. */
54
+ export declare function contextsOf(event: string, dialect: HookDialect, decoded: Decoded): string[];
55
+ /** The first non-empty line, trimmed and bounded. */
56
+ export declare function firstLine(text: string): string;