@kirkchinese/harness-forge 0.0.0-stage → 0.4.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 (246) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -2
  3. package/assets/icon.svg +12 -0
  4. package/cordis.patch.yml +28 -0
  5. package/lib/agents/presets.d.ts +91 -0
  6. package/lib/agents/presets.js +282 -0
  7. package/lib/agents/profiles.d.ts +58 -0
  8. package/lib/agents/profiles.js +278 -0
  9. package/lib/agents/row.d.ts +17 -0
  10. package/lib/agents/row.js +146 -0
  11. package/lib/client/styles.d.ts +8 -0
  12. package/lib/client/styles.js +89 -0
  13. package/lib/client/typert.d.ts +14 -0
  14. package/lib/client/typert.js +110 -0
  15. package/lib/client.js +22500 -0
  16. package/lib/commands/definition.d.ts +40 -0
  17. package/lib/commands/definition.js +113 -0
  18. package/lib/commands/output-styles.d.ts +42 -0
  19. package/lib/commands/output-styles.js +152 -0
  20. package/lib/commands/row.d.ts +9 -0
  21. package/lib/commands/row.js +157 -0
  22. package/lib/context/instructions.d.ts +32 -0
  23. package/lib/context/instructions.js +88 -0
  24. package/lib/context/row.d.ts +16 -0
  25. package/lib/context/row.js +17 -0
  26. package/lib/dsh/commands.d.ts +30 -0
  27. package/lib/dsh/commands.js +61 -0
  28. package/lib/dsh/host.d.ts +107 -0
  29. package/lib/dsh/host.js +214 -0
  30. package/lib/dsh/model-route.d.ts +21 -0
  31. package/lib/dsh/model-route.js +37 -0
  32. package/lib/dsh/session-records.d.ts +78 -0
  33. package/lib/dsh/session-records.js +36 -0
  34. package/lib/dsh/subagents.d.ts +46 -0
  35. package/lib/dsh/subagents.js +14 -0
  36. package/lib/forge/contract.d.ts +231 -0
  37. package/lib/forge/contract.js +43 -0
  38. package/lib/forge/ledger.d.ts +143 -0
  39. package/lib/forge/ledger.js +733 -0
  40. package/lib/forge/live-mounts.d.ts +45 -0
  41. package/lib/forge/live-mounts.js +78 -0
  42. package/lib/forge/marketplace-install.d.ts +3 -0
  43. package/lib/forge/marketplace-install.js +64 -0
  44. package/lib/forge/npm.d.ts +31 -0
  45. package/lib/forge/npm.js +60 -0
  46. package/lib/forge/projects.d.ts +94 -0
  47. package/lib/forge/projects.js +336 -0
  48. package/lib/forge/receipts.d.ts +5 -0
  49. package/lib/forge/receipts.js +31 -0
  50. package/lib/forge/row.d.ts +32 -0
  51. package/lib/forge/row.js +59 -0
  52. package/lib/forge/service.d.ts +105 -0
  53. package/lib/forge/service.js +274 -0
  54. package/lib/forge/settings.d.ts +41 -0
  55. package/lib/forge/settings.js +115 -0
  56. package/lib/forge/state.d.ts +69 -0
  57. package/lib/forge/state.js +150 -0
  58. package/lib/forge/trust.d.ts +48 -0
  59. package/lib/forge/trust.js +107 -0
  60. package/lib/forge/turns.d.ts +13 -0
  61. package/lib/forge/turns.js +56 -0
  62. package/lib/hooks/broker/engine.d.ts +112 -0
  63. package/lib/hooks/broker/engine.js +728 -0
  64. package/lib/hooks/broker/helpers.d.ts +10 -0
  65. package/lib/hooks/broker/helpers.js +22 -0
  66. package/lib/hooks/broker/lifecycle.d.ts +65 -0
  67. package/lib/hooks/broker/lifecycle.js +342 -0
  68. package/lib/hooks/broker/session-events.d.ts +22 -0
  69. package/lib/hooks/broker/session-events.js +196 -0
  70. package/lib/hooks/broker/tools.d.ts +52 -0
  71. package/lib/hooks/broker/tools.js +424 -0
  72. package/lib/hooks/broker/types.d.ts +262 -0
  73. package/lib/hooks/broker/types.js +1 -0
  74. package/lib/hooks/broker.d.ts +80 -0
  75. package/lib/hooks/broker.js +95 -0
  76. package/lib/hooks/codex-user.d.ts +5 -0
  77. package/lib/hooks/codex-user.js +109 -0
  78. package/lib/hooks/condition.d.ts +11 -0
  79. package/lib/hooks/condition.js +181 -0
  80. package/lib/hooks/config.d.ts +85 -0
  81. package/lib/hooks/config.js +150 -0
  82. package/lib/hooks/matcher.d.ts +20 -0
  83. package/lib/hooks/matcher.js +39 -0
  84. package/lib/hooks/notice.d.ts +17 -0
  85. package/lib/hooks/notice.js +39 -0
  86. package/lib/hooks/output.d.ts +56 -0
  87. package/lib/hooks/output.js +214 -0
  88. package/lib/hooks/permissions.d.ts +14 -0
  89. package/lib/hooks/permissions.js +109 -0
  90. package/lib/hooks/process.d.ts +47 -0
  91. package/lib/hooks/process.js +134 -0
  92. package/lib/hooks/row.d.ts +17 -0
  93. package/lib/hooks/row.js +94 -0
  94. package/lib/hooks/runner.d.ts +85 -0
  95. package/lib/hooks/runner.js +293 -0
  96. package/lib/hooks/shell-commands.d.ts +19 -0
  97. package/lib/hooks/shell-commands.js +177 -0
  98. package/lib/hooks/shell.d.ts +43 -0
  99. package/lib/hooks/shell.js +126 -0
  100. package/lib/hooks/transcript.d.ts +28 -0
  101. package/lib/hooks/transcript.js +141 -0
  102. package/lib/hooks/vocabulary.d.ts +57 -0
  103. package/lib/hooks/vocabulary.js +364 -0
  104. package/lib/index.d.ts +31 -0
  105. package/lib/index.js +287 -0
  106. package/lib/mcp/auth.d.ts +35 -0
  107. package/lib/mcp/auth.js +178 -0
  108. package/lib/mcp/catalog.d.ts +36 -0
  109. package/lib/mcp/catalog.js +61 -0
  110. package/lib/mcp/mount.d.ts +75 -0
  111. package/lib/mcp/mount.js +217 -0
  112. package/lib/mcp/remote.d.ts +45 -0
  113. package/lib/mcp/remote.js +143 -0
  114. package/lib/mcp/row.d.ts +9 -0
  115. package/lib/mcp/row.js +131 -0
  116. package/lib/mods/bundler.d.ts +38 -0
  117. package/lib/mods/bundler.js +157 -0
  118. package/lib/mods/chain.d.ts +103 -0
  119. package/lib/mods/chain.js +436 -0
  120. package/lib/mods/claude.d.ts +27 -0
  121. package/lib/mods/claude.js +56 -0
  122. package/lib/mods/engine.d.ts +160 -0
  123. package/lib/mods/engine.js +701 -0
  124. package/lib/mods/expect.d.ts +8 -0
  125. package/lib/mods/expect.js +248 -0
  126. package/lib/mods/host.d.ts +105 -0
  127. package/lib/mods/host.js +462 -0
  128. package/lib/mods/loader.d.ts +53 -0
  129. package/lib/mods/loader.js +227 -0
  130. package/lib/mods/ops.d.ts +13 -0
  131. package/lib/mods/ops.js +117 -0
  132. package/lib/mods/row.d.ts +9 -0
  133. package/lib/mods/row.js +77 -0
  134. package/lib/mods/runtime.d.ts +60 -0
  135. package/lib/mods/runtime.js +133 -0
  136. package/lib/mods/service/commands.d.ts +17 -0
  137. package/lib/mods/service/commands.js +121 -0
  138. package/lib/mods/service/drawings.d.ts +36 -0
  139. package/lib/mods/service/drawings.js +64 -0
  140. package/lib/mods/service/sessions.d.ts +63 -0
  141. package/lib/mods/service/sessions.js +345 -0
  142. package/lib/mods/service/tools.d.ts +32 -0
  143. package/lib/mods/service/tools.js +253 -0
  144. package/lib/mods/service/turns.d.ts +32 -0
  145. package/lib/mods/service/turns.js +161 -0
  146. package/lib/mods/service/types.d.ts +151 -0
  147. package/lib/mods/service/types.js +1 -0
  148. package/lib/mods/service.d.ts +72 -0
  149. package/lib/mods/service.js +104 -0
  150. package/lib/mods/surfaces.d.ts +93 -0
  151. package/lib/mods/surfaces.js +309 -0
  152. package/lib/mods/values.d.ts +30 -0
  153. package/lib/mods/values.js +95 -0
  154. package/lib/opencode/bun.d.ts +40 -0
  155. package/lib/opencode/bun.js +156 -0
  156. package/lib/opencode/convert.d.ts +53 -0
  157. package/lib/opencode/convert.js +255 -0
  158. package/lib/opencode/row.d.ts +9 -0
  159. package/lib/opencode/row.js +86 -0
  160. package/lib/opencode/runtime/core.d.ts +74 -0
  161. package/lib/opencode/runtime/core.js +249 -0
  162. package/lib/opencode/runtime/events.d.ts +80 -0
  163. package/lib/opencode/runtime/events.js +281 -0
  164. package/lib/opencode/runtime/mount.d.ts +27 -0
  165. package/lib/opencode/runtime/mount.js +283 -0
  166. package/lib/opencode/runtime/server.d.ts +27 -0
  167. package/lib/opencode/runtime/server.js +353 -0
  168. package/lib/opencode/runtime/types.d.ts +109 -0
  169. package/lib/opencode/runtime/types.js +1 -0
  170. package/lib/opencode/runtime.d.ts +55 -0
  171. package/lib/opencode/runtime.js +79 -0
  172. package/lib/porting/port-gap.d.ts +30 -0
  173. package/lib/porting/port-gap.js +122 -0
  174. package/lib/porting/port-sessions.d.ts +41 -0
  175. package/lib/porting/port-sessions.js +98 -0
  176. package/lib/porting/prompt.d.ts +23 -0
  177. package/lib/porting/prompt.js +49 -0
  178. package/lib/porting/row.d.ts +16 -0
  179. package/lib/porting/row.js +25 -0
  180. package/lib/porting/verify-port.d.ts +53 -0
  181. package/lib/porting/verify-port.js +104 -0
  182. package/lib/presets/fs-search.d.ts +25 -0
  183. package/lib/presets/fs-search.js +17 -0
  184. package/lib/presets/tool-filter.d.ts +35 -0
  185. package/lib/presets/tool-filter.js +91 -0
  186. package/lib/sessions/claude.d.ts +29 -0
  187. package/lib/sessions/claude.js +83 -0
  188. package/lib/sessions/codex.d.ts +30 -0
  189. package/lib/sessions/codex.js +303 -0
  190. package/lib/sessions/opencode.d.ts +32 -0
  191. package/lib/sessions/opencode.js +320 -0
  192. package/lib/sessions/row.d.ts +14 -0
  193. package/lib/sessions/row.js +35 -0
  194. package/lib/shared/invocation.d.ts +169 -0
  195. package/lib/shared/invocation.js +325 -0
  196. package/lib/shared/render.d.ts +72 -0
  197. package/lib/shared/render.js +588 -0
  198. package/lib/shared/util.d.ts +21 -0
  199. package/lib/shared/util.js +32 -0
  200. package/lib/skills/bundled.d.ts +3 -0
  201. package/lib/skills/bundled.js +42 -0
  202. package/lib/skills/provider.d.ts +72 -0
  203. package/lib/skills/provider.js +340 -0
  204. package/lib/skills/row.d.ts +22 -0
  205. package/lib/skills/row.js +204 -0
  206. package/lib/workbench/adapter-coverage.d.ts +47 -0
  207. package/lib/workbench/adapter-coverage.js +102 -0
  208. package/lib/workbench/model-tools.d.ts +10 -0
  209. package/lib/workbench/model-tools.js +122 -0
  210. package/lib/workbench/overview.d.ts +103 -0
  211. package/lib/workbench/overview.js +159 -0
  212. package/lib/workbench/remote.d.ts +118 -0
  213. package/lib/workbench/remote.js +422 -0
  214. package/lib/workbench/row.d.ts +15 -0
  215. package/lib/workbench/row.js +16 -0
  216. package/lib/workbench/settings.d.ts +15 -0
  217. package/lib/workbench/settings.js +56 -0
  218. package/lib/workbench/workbench.d.ts +100 -0
  219. package/lib/workbench/workbench.js +260 -0
  220. package/locale/agents/en.json +6 -0
  221. package/locale/agents/zh.json +6 -0
  222. package/locale/commands/en.json +6 -0
  223. package/locale/commands/zh.json +6 -0
  224. package/locale/context/en.json +6 -0
  225. package/locale/context/zh.json +6 -0
  226. package/locale/en.json +6 -0
  227. package/locale/hooks/en.json +6 -0
  228. package/locale/hooks/zh.json +6 -0
  229. package/locale/mcp/en.json +6 -0
  230. package/locale/mcp/zh.json +6 -0
  231. package/locale/mods/en.json +6 -0
  232. package/locale/mods/zh.json +6 -0
  233. package/locale/opencode/en.json +6 -0
  234. package/locale/opencode/zh.json +6 -0
  235. package/locale/porter/en.json +6 -0
  236. package/locale/porter/zh.json +6 -0
  237. package/locale/sessions/en.json +6 -0
  238. package/locale/sessions/zh.json +6 -0
  239. package/locale/skills/en.json +6 -0
  240. package/locale/skills/zh.json +6 -0
  241. package/locale/workbench/en.json +6 -0
  242. package/locale/workbench/zh.json +6 -0
  243. package/locale/zh.json +6 -0
  244. package/package.json +246 -3
  245. package/runtime/opencode-host.mjs +270 -0
  246. package/skills/harness-forge/SKILL.md +47 -0
@@ -0,0 +1,588 @@
1
+ /**
2
+ * Render a Claude Code command or skill body as Claude Code does before the
3
+ * model sees it: argument placeholders, path variables, inline `` !`cmd` ``
4
+ * and fenced ```` ```! ```` shell injection, and `@file` attachments.
5
+ *
6
+ * The template's structure is fixed before any value goes in: shell lines and
7
+ * `@file` references are found in the body as written, so an argument (or a
8
+ * command's output) is always data and never becomes a shell line or an
9
+ * attachment. Inside a shell line a value reaches the shell only through an
10
+ * environment variable, referenced only where that reference provably stays
11
+ * data; a placeholder anywhere else keeps the line from running (a visible note
12
+ * says so), unlike Claude Code, which splices the text in. An `@file` reference
13
+ * attaches a file only where the template may reach (see `attach`); anything
14
+ * else gets a visible note.
15
+ * @module @kirkchinese/harness-forge
16
+ */
17
+ import { readFileSync, statSync } from 'node:fs';
18
+ import { homedir } from 'node:os';
19
+ import { basename, isAbsolute, join, resolve } from 'node:path';
20
+ import { isInside, realOrLexical, workspaceRootOf } from '@kirkchinese/harness-forge-core';
21
+ /** What stands in place of a shell line that did not run. */
22
+ function shellNote(reason) {
23
+ return `[Harness Forge: this shell command was not run: ${reason}]`;
24
+ }
25
+ const COMMAND_TIMEOUT_MS = 120_000;
26
+ const FILE_LIMIT = 256 * 1024;
27
+ /** `\$`, a path variable, `$ARGUMENTS[n]`, `$ARGUMENTS`, `$n` or `$name`. */
28
+ const PLACEHOLDER = /\\\$|\$\{(CLAUDE_SKILL_DIR|CLAUDE_PROJECT_DIR|CLAUDE_SESSION_ID|CLAUDE_PLUGIN_ROOT|CLAUDE_PLUGIN_DATA)\}|\$ARGUMENTS\[(\d+)\]|\$ARGUMENTS\b|\$(\d+)|\$([A-Za-z_][A-Za-z0-9_-]*)/gu;
29
+ /** Split a body into prose and shell lines, as written: a fenced ```! block, or an inline !`cmd` at line start or after whitespace. */
30
+ function segmentsOf(body) {
31
+ const segments = [];
32
+ let cursor = 0;
33
+ for (const match of body.matchAll(/^```!\s*\r?\n([\s\S]*?)\r?\n```[ \t]*$|(^|\s)!`([^`\n]+)`/gmu)) {
34
+ segments.push({ kind: 'text', text: body.slice(cursor, match.index) });
35
+ segments.push(match[1] !== undefined ? { kind: 'shell', command: match[1], lead: '' } : { kind: 'shell', command: match[3] ?? '', lead: match[2] ?? '' });
36
+ cursor = (match.index ?? 0) + match[0].length;
37
+ }
38
+ segments.push({ kind: 'text', text: body.slice(cursor) });
39
+ return segments;
40
+ }
41
+ /** Render one body. */
42
+ export async function renderBody(body, context) {
43
+ const args = splitArguments(context.rawArguments);
44
+ let consumed = false;
45
+ /** The value a placeholder stands for; undefined leaves it as written. */
46
+ const valueOf = (match, variable, indexed, short, name) => {
47
+ if (variable !== undefined)
48
+ return pathVariable(variable, context);
49
+ if (match === '$ARGUMENTS') {
50
+ consumed = true;
51
+ return context.rawArguments.trim();
52
+ }
53
+ const index = indexed ?? short;
54
+ if (index !== undefined) {
55
+ const value = args[Number(index)];
56
+ if (value === undefined)
57
+ return undefined;
58
+ consumed = true;
59
+ return value;
60
+ }
61
+ if (name !== undefined) {
62
+ const position = context.argumentNames.indexOf(name);
63
+ if (position === -1)
64
+ return undefined;
65
+ consumed = true;
66
+ return args[position] ?? '';
67
+ }
68
+ return undefined;
69
+ };
70
+ const substituteText = (text) => text
71
+ .replace(PLACEHOLDER, (match, variable, indexed, short, name) => {
72
+ if (match === '\\$')
73
+ return '\\$';
74
+ return valueOf(match, variable, indexed, short, name) ?? match;
75
+ })
76
+ .replace(/\\\$(?=\d|ARGUMENTS|[A-Za-z_])/gu, '$');
77
+ const segments = segmentsOf(body);
78
+ const references = segments.flatMap(segment => segment.kind === 'text' ? fileReferences(segment.text, context) : []);
79
+ const kind = context.shellKind === 'powershell' ? 'powershell' : context.shell.defaultKind;
80
+ let commands = 0;
81
+ let text = '';
82
+ for (const segment of segments) {
83
+ if (segment.kind === 'text') {
84
+ text += substituteText(segment.text);
85
+ continue;
86
+ }
87
+ if (context.refuseShell !== undefined) {
88
+ text += segment.lead + shellNote(context.refuseShell);
89
+ continue;
90
+ }
91
+ const values = {};
92
+ const line = shellCommand(segment.command, kind, (match, variable, indexed, short, name) => {
93
+ const value = valueOf(match, variable, indexed, short, name);
94
+ if (value === undefined)
95
+ return undefined;
96
+ const key = `HF_ARG_${String(Object.keys(values).length)}`;
97
+ values[key] = value;
98
+ return key;
99
+ });
100
+ if ('refused' in line) {
101
+ text += segment.lead + shellNote(`${line.refused}, where Harness Forge cannot pass it safely`);
102
+ continue;
103
+ }
104
+ commands += 1;
105
+ text += segment.lead + await execute(line.command, kind, values, context);
106
+ }
107
+ const withArguments = !consumed && context.rawArguments.trim().length > 0
108
+ ? `${text.trimEnd()}\n\nARGUMENTS: ${context.rawArguments.trim()}`
109
+ : text;
110
+ const attached = attach(references, context);
111
+ const allowed = { user: 'the workspace and ~/.claude', project: 'the workspace', plugin: 'the plugin and the workspace' }[context.scope ?? 'plugin'];
112
+ const notes = attached.refused.map(reference => `[Harness Forge: @${reference} was not attached: it is outside ${allowed}]`);
113
+ return { text: [withArguments.trim(), ...notes].join('\n\n').trim(), files: attached.files, commands };
114
+ }
115
+ /**
116
+ * A Codex custom prompt with its arguments, as Codex expands it: `$1`…`$9`
117
+ * are the positional arguments, `$ARGUMENTS` all of them, `$NAME` the value
118
+ * of `NAME=value`, `$$` a literal `$`. Codex runs no commands and attaches
119
+ * no files from a prompt body.
120
+ */
121
+ export function renderCodexPrompt(body, rawArguments) {
122
+ const words = splitArguments(rawArguments);
123
+ const named = new Map();
124
+ const positional = [];
125
+ for (const word of words) {
126
+ const match = /^([A-Z][A-Z0-9_]*)=(.*)$/su.exec(word);
127
+ if (match !== null)
128
+ named.set(match[1] ?? '', match[2] ?? '');
129
+ else
130
+ positional.push(word);
131
+ }
132
+ const text = body.replace(/\$\$|\$ARGUMENTS\b|\$([1-9])|\$([A-Z][A-Z0-9_]*)/gu, (match, digit, name) => {
133
+ if (match === '$$')
134
+ return '$';
135
+ if (match === '$ARGUMENTS')
136
+ return positional.join(' ');
137
+ if (digit !== undefined)
138
+ return positional[Number(digit) - 1] ?? '';
139
+ if (name !== undefined)
140
+ return named.get(name) ?? match;
141
+ return match;
142
+ });
143
+ return { text: text.trim(), files: [], commands: 0 };
144
+ }
145
+ /** Shell-style split: quotes group words; backslash escapes the next char. */
146
+ export function splitArguments(raw) {
147
+ const args = [];
148
+ let current = '';
149
+ let quote;
150
+ let started = false;
151
+ for (let index = 0; index < raw.length; index++) {
152
+ const char = raw[index] ?? '';
153
+ if (quote !== undefined) {
154
+ if (char === quote)
155
+ quote = undefined;
156
+ else if (char === '\\' && quote === '"' && index + 1 < raw.length)
157
+ current += raw[++index] ?? '';
158
+ else
159
+ current += char;
160
+ continue;
161
+ }
162
+ if (char === '"' || char === "'") {
163
+ quote = char;
164
+ started = true;
165
+ }
166
+ else if (/\s/u.test(char)) {
167
+ if (started)
168
+ args.push(current);
169
+ current = '';
170
+ started = false;
171
+ }
172
+ else {
173
+ if (char === '\\' && index + 1 < raw.length)
174
+ current += raw[++index] ?? '';
175
+ else
176
+ current += char;
177
+ started = true;
178
+ }
179
+ }
180
+ if (started)
181
+ args.push(current);
182
+ return args;
183
+ }
184
+ function pathVariable(name, context) {
185
+ const posix = (path) => path?.replace(/\\/gu, '/');
186
+ switch (name) {
187
+ case 'CLAUDE_SKILL_DIR': return posix(context.skillDir);
188
+ case 'CLAUDE_PROJECT_DIR': return posix(context.cwd);
189
+ case 'CLAUDE_SESSION_ID': return context.sessionId;
190
+ case 'CLAUDE_PLUGIN_ROOT': return posix(context.pluginRoot);
191
+ case 'CLAUDE_PLUGIN_DATA': return posix(context.dataDir);
192
+ default: return undefined;
193
+ }
194
+ }
195
+ const WHERE = {
196
+ dq: 'double quotes',
197
+ sq: 'single quotes',
198
+ ansi: "ANSI-C quotes ($'…')",
199
+ backtick: 'backticks',
200
+ subst: 'a nested command or subexpression',
201
+ param: 'a parameter expansion',
202
+ comment: 'a comment',
203
+ heredoc: 'a here-document',
204
+ arith: 'an arithmetic expression',
205
+ cond: 'a [[ ]] test',
206
+ herestring: 'a here-string',
207
+ 'block-comment': 'a comment',
208
+ };
209
+ /** Words after which bash still expects a command name. */
210
+ const BASH_PREFIXES = new Set(['if', 'then', 'else', 'elif', 'do', 'while', 'until', '!', '{', '}', 'fi', 'done', 'esac']);
211
+ /** bash builtins and keywords: none may take a value, apart from those in `argumentOf`. */
212
+ const BASH_BUILTINS = new Set(['.', ':', '[', 'alias', 'bg', 'bind', 'break', 'builtin', 'caller', 'case', 'cd', 'command', 'compgen', 'complete', 'compopt', 'continue', 'coproc', 'declare', 'dirs', 'disown', 'echo', 'enable', 'eval', 'exec', 'exit', 'export', 'false', 'fc', 'fg', 'for', 'function', 'getopts', 'hash', 'help', 'history', 'jobs', 'kill', 'let', 'local', 'logout', 'mapfile', 'popd', 'printf', 'pushd', 'pwd', 'read', 'readarray', 'readonly', 'return', 'select', 'set', 'shift', 'shopt', 'source', 'suspend', 'test', 'time', 'times', 'trap', 'true', 'type', 'typeset', 'ulimit', 'umask', 'unalias', 'unset', 'wait']);
213
+ /** Programs that run a command or code they are given. */
214
+ const RUNNERS = new Set(['env', 'sudo', 'doas', 'su', 'xargs', 'nohup', 'nice', 'ionice', 'timeout', 'stdbuf', 'setsid', 'chroot', 'unshare', 'nsenter', 'flock', 'watch', 'strace', 'ltrace', 'parallel', 'ssh', 'sed', 'awk', 'gawk', 'mawk', 'nawk', 'sh', 'bash', 'dash', 'zsh', 'ksh', 'mksh', 'fish', 'csh', 'tcsh', 'busybox', 'pwsh', 'powershell', 'cmd', 'python', 'python2', 'python3', 'perl', 'ruby', 'node', 'deno', 'bun', 'php', 'lua', 'tclsh', 'expect', 'script']);
215
+ /** PowerShell commands (and aliases) that run, load or define code they are given. */
216
+ const POWERSHELL_RUNNERS = new Set(['.', 'iex', 'invoke-expression', 'icm', 'invoke-command', 'saps', 'start', 'start-process', 'sajb', 'start-job', 'start-threadjob', 'ii', 'invoke-item', 'add-type', 'nmo', 'new-module', 'ipmo', 'import-module', 'foreach-object', 'where-object', 'sv', 'set-variable', 'nv', 'new-variable', 'si', 'set-item', 'ni', 'new-item']);
217
+ /** A `test` / `[` operator whose operand stays a plain string or file name (not `-v`, `-a`, `-o`, `!`). */
218
+ const TEST_OPERATOR = /^(?:-[bcdefghknprsStuwxzGLNO]|-(?:eq|ne|lt|le|gt|ge|nt|ot|ef)|==?|!=)$/u;
219
+ /** A line that defines a function or alias, whose body may run its arguments. */
220
+ const DEFINES = { bash: /\bfunction\s|[\w.:-]\s*\(\s*\)|\balias\b/u, powershell: /\b(?:function|filter|workflow)\s|\b(?:set|new)-alias\b|\b(?:sal|nal)\b|\b(?:function|alias):/iu };
221
+ /**
222
+ * Whether a value may stand in the word being read after `words` (the words
223
+ * of the current simple command so far): only as an argument of a command
224
+ * named by a plain word, not as the command itself or an assignment, and not
225
+ * of a builtin or program that runs, evaluates or names what it is given.
226
+ */
227
+ function argumentOf(words, kind) {
228
+ const at = words.findIndex(word => kind === 'powershell' || !/^[A-Za-z_][A-Za-z0-9_]*(?:\[[^\]]*\])?\+?=/u.test(word));
229
+ const name = words[at];
230
+ if (at === -1 || name === undefined || name.startsWith('-'))
231
+ return false;
232
+ const args = words.slice(at + 1);
233
+ if (kind === 'powershell') {
234
+ const command = basename(name.replace(/\\/gu, '/')).toLowerCase().replace(/\.exe$/u, '');
235
+ return /^[\w./\\:+@,-]+$/u.test(name) && !POWERSHELL_RUNNERS.has(command) && !RUNNERS.has(command)
236
+ && !args.some(arg => /^-(?:c|command|e|ec|encodedcommand|sb|scriptblock|cwa|commandwithargs)$/iu.test(arg));
237
+ }
238
+ if (!/^[\w./:+@,-]+$|^\[$/u.test(name))
239
+ return false;
240
+ const command = basename(name);
241
+ if (RUNNERS.has(command) || (command === 'find' && args.some(arg => /^-(?:exec|execdir|ok|okdir)$/u.test(arg))))
242
+ return false;
243
+ if (!BASH_BUILTINS.has(name))
244
+ return true;
245
+ switch (name) {
246
+ case 'echo':
247
+ case 'cd':
248
+ case 'pushd':
249
+ case 'pwd':
250
+ case 'true':
251
+ case 'false':
252
+ case ':': return true;
253
+ // `printf -v NAME` evaluates NAME; so would a value standing as the format.
254
+ case 'printf': return args.length > 0 && !args.includes('-v');
255
+ case 'test':
256
+ case '[': return TEST_OPERATOR.test(args.at(-1) ?? '');
257
+ default: return false;
258
+ }
259
+ }
260
+ /**
261
+ * A shell line with each placeholder replaced by a reference to an
262
+ * environment variable holding its value. Values never enter the source: a
263
+ * reference is written only where it provably stays one word of data (bash:
264
+ * unquoted or inside plain double quotes; PowerShell: outside quotes or inside
265
+ * plain double quotes), and only in an argument of a simple command that does
266
+ * not run it (see `argumentOf`), in a line that defines no function or alias.
267
+ * A placeholder anywhere else (single or ANSI-C quotes, here-documents and
268
+ * here-strings, backticks, `$(…)`, parameter expansions, arithmetic, `[[ ]]`,
269
+ * comments, the command position, an assignment, the arguments of `eval` and
270
+ * the like) makes the whole line refused. `\$` stays as written, for the shell.
271
+ */
272
+ export function shellCommand(command, kind, value) {
273
+ const stack = [];
274
+ const heredocs = [];
275
+ const placeholder = new RegExp(PLACEHOLDER.source, 'uy');
276
+ const single = kind === 'powershell' ? /['\u2018\u2019\u201A\u201B]/u : /'/u;
277
+ const double = kind === 'powershell' ? /["\u201C\u201D\u201E]/u : /"/u;
278
+ const escape = kind === 'bash' ? '\\' : '`';
279
+ let out = '';
280
+ let index = 0;
281
+ const take = (count) => {
282
+ out += command.slice(index, index + count);
283
+ index += count;
284
+ };
285
+ const wordStart = () => index === 0 || /[\s;&|(<>]/u.test(command[index - 1] ?? '');
286
+ const lineStart = () => index === 0 || command[index - 1] === '\n';
287
+ // The words of the current simple command, read at the top level only.
288
+ let words = [];
289
+ let wordAt;
290
+ const endWord = () => {
291
+ if (wordAt === undefined)
292
+ return;
293
+ const word = command.slice(wordAt, index);
294
+ if (!(kind === 'bash' && words.length === 0 && BASH_PREFIXES.has(word)))
295
+ words.push(word);
296
+ wordAt = undefined;
297
+ };
298
+ const separates = (char, next) => {
299
+ const previous = command[index - 1] ?? '';
300
+ if (/[\n;()]/u.test(char))
301
+ return true;
302
+ if (kind === 'powershell')
303
+ return /[{}|]/u.test(char) || (char === '&' && (next === '&' || previous === '&'));
304
+ return (char === '|' && previous !== '>') || (char === '&' && !/[<>]/u.test(previous) && next !== '>');
305
+ };
306
+ while (index < command.length) {
307
+ const frame = stack.at(-1);
308
+ const char = command[index] ?? '';
309
+ const next = command[index + 1] ?? '';
310
+ // A here-document (bash) or here-string (PowerShell) body ends at its own line.
311
+ if ((frame?.kind === 'heredoc' || frame?.kind === 'herestring') && lineStart()) {
312
+ const end = command.indexOf('\n', index);
313
+ const line = command.slice(index, end === -1 ? command.length : end);
314
+ const closes = frame.kind === 'heredoc' ? frame.end !== undefined && (frame.strip === true ? line.replace(/^\t+/u, '') : line) === frame.end : line.startsWith(frame.end ?? '\0');
315
+ if (closes) {
316
+ stack.pop();
317
+ take(frame.kind === 'heredoc' ? line.length : (frame.end ?? '').length);
318
+ continue;
319
+ }
320
+ }
321
+ if (stack.length === 0) {
322
+ if (/[ \t\r]/u.test(char))
323
+ endWord();
324
+ else if (separates(char, next)) {
325
+ endWord();
326
+ words = [];
327
+ }
328
+ else
329
+ wordAt ??= index;
330
+ }
331
+ if (char === '$' || char === '\\') {
332
+ placeholder.lastIndex = index;
333
+ const match = placeholder.exec(command);
334
+ if (match !== null && match[0] === '\\$') {
335
+ take(2);
336
+ continue;
337
+ }
338
+ if (match !== null) {
339
+ const key = value(match[0], match[1], match[2], match[3], match[4]);
340
+ if (key !== undefined) {
341
+ if (frame !== undefined && !(stack.length === 1 && frame.kind === 'dq'))
342
+ return { refused: `an argument or path variable stands inside ${WHERE[frame.kind]}` };
343
+ if (!argumentOf(words, kind) || DEFINES[kind].test(command))
344
+ return { refused: 'an argument or path variable stands where it is not plainly an argument of a command' };
345
+ out += kind === 'powershell' ? `\${env:${key}}` : frame === undefined ? `"\${${key}}"` : `\${${key}}`;
346
+ index += match[0].length;
347
+ continue;
348
+ }
349
+ }
350
+ }
351
+ if (frame?.kind === 'heredoc' || frame?.kind === 'herestring') {
352
+ take(1);
353
+ continue;
354
+ }
355
+ if (frame?.kind === 'comment') {
356
+ // The line break ends the comment and is read again as code.
357
+ if (char === '\n')
358
+ stack.pop();
359
+ else
360
+ take(1);
361
+ continue;
362
+ }
363
+ if (frame?.kind === 'block-comment') {
364
+ if (char === '#' && next === '>') {
365
+ stack.pop();
366
+ take(2);
367
+ }
368
+ else
369
+ take(1);
370
+ continue;
371
+ }
372
+ if (frame?.kind === 'sq') {
373
+ if (kind === 'powershell' && single.test(char) && single.test(next))
374
+ take(2);
375
+ else {
376
+ if (single.test(char))
377
+ stack.pop();
378
+ take(1);
379
+ }
380
+ continue;
381
+ }
382
+ if (frame?.kind === 'ansi' || frame?.kind === 'backtick' || frame?.kind === 'param') {
383
+ if (char === '\\' && index + 1 < command.length)
384
+ take(2);
385
+ else {
386
+ const closes = frame.kind === 'ansi' ? char === "'" : frame.kind === 'backtick' ? char === '`' : char === '}';
387
+ if (closes)
388
+ stack.pop();
389
+ else if (frame.kind === 'param' && char === '$' && next === '{')
390
+ stack.push({ kind: 'param' });
391
+ take(closes || !(char === '$' && next === '{') ? 1 : 2);
392
+ }
393
+ continue;
394
+ }
395
+ if (char === escape && index + 1 < command.length) {
396
+ take(2);
397
+ continue;
398
+ }
399
+ if (frame?.kind === 'dq') {
400
+ if (double.test(char)) {
401
+ if (kind === 'powershell' && double.test(next))
402
+ take(2);
403
+ else {
404
+ stack.pop();
405
+ take(1);
406
+ }
407
+ }
408
+ else if (char === '$' && next === '(') {
409
+ stack.push({ kind: 'subst', depth: 0 });
410
+ take(2);
411
+ }
412
+ else if (char === '$' && next === '{') {
413
+ stack.push({ kind: 'param' });
414
+ take(2);
415
+ }
416
+ else if (kind === 'bash' && char === '`') {
417
+ stack.push({ kind: 'backtick' });
418
+ take(1);
419
+ }
420
+ else
421
+ take(1);
422
+ continue;
423
+ }
424
+ if ((frame?.kind === 'arith' || frame?.kind === 'cond') && frame.end !== undefined && command.startsWith(frame.end, index)) {
425
+ stack.pop();
426
+ // A `let` ends at a separator, read again as code.
427
+ if (!/^[;&|\n]$/u.test(frame.end))
428
+ take(frame.end.length);
429
+ continue;
430
+ }
431
+ // Code: the top level, a `$(…)`, an arithmetic expression or a `[[ ]]` test.
432
+ if (char === '\n' && heredocs.length > 0) {
433
+ take(1);
434
+ stack.push(...heredocs.splice(0).reverse());
435
+ continue;
436
+ }
437
+ if (kind === 'powershell' && char === '@' && (single.test(next) || double.test(next)) && /^[ \t]*\r?\n/u.test(command.slice(index + 2))) {
438
+ stack.push({ kind: 'herestring', end: single.test(next) ? `'@` : `"@` });
439
+ take(2);
440
+ continue;
441
+ }
442
+ if (kind === 'powershell' && char === '<' && next === '#') {
443
+ stack.push({ kind: 'block-comment' });
444
+ take(2);
445
+ continue;
446
+ }
447
+ if (kind === 'bash' && char === '$' && next === "'") {
448
+ stack.push({ kind: 'ansi' });
449
+ take(2);
450
+ continue;
451
+ }
452
+ if (char === '$' && (next === '(' || next === '{' || (kind === 'bash' && next === '['))) {
453
+ stack.push(next === '(' ? { kind: 'subst', depth: 0 } : next === '{' ? { kind: 'param' } : { kind: 'arith', end: ']' });
454
+ take(2);
455
+ continue;
456
+ }
457
+ if (single.test(char))
458
+ stack.push({ kind: 'sq' });
459
+ else if (double.test(char))
460
+ stack.push({ kind: 'dq' });
461
+ else if (kind === 'bash' && char === '`')
462
+ stack.push({ kind: 'backtick' });
463
+ else if (char === '#' && wordStart())
464
+ stack.push({ kind: 'comment' });
465
+ else if (kind === 'bash' && wordStart() && char === '(' && next === '(') {
466
+ stack.push({ kind: 'arith', end: '))' });
467
+ take(2);
468
+ continue;
469
+ }
470
+ else if (kind === 'bash' && wordStart() && char === '[' && next === '[') {
471
+ stack.push({ kind: 'cond', end: ']]' });
472
+ take(2);
473
+ continue;
474
+ }
475
+ else if (kind === 'bash' && /(?:^|[;&|(\n])[ \t]*$/u.test(command.slice(0, index)) && /^let\b/u.test(command.slice(index))) {
476
+ // `let` evaluates its arguments as arithmetic, to the end of the command (undefined: the end of the line).
477
+ const end = /[;&|\n]/u.exec(command.slice(index))?.[0];
478
+ stack.push({ kind: 'arith', ...(end !== undefined ? { end } : {}) });
479
+ }
480
+ else if (kind === 'bash' && char === '<' && next === '<' && command[index + 2] !== '<') {
481
+ // A here-document: its body starts after this line and ends at the delimiter.
482
+ const operator = /^<<(-?)[ \t]*(?:'([^'\n]+)'|"([^"\n]+)"|\\?([A-Za-z0-9_.-]+))/u.exec(command.slice(index));
483
+ heredocs.push({ kind: 'heredoc', strip: operator?.[1] === '-', ...(operator !== null ? { end: operator[2] ?? operator[3] ?? operator[4] ?? '' } : {}) });
484
+ take(operator?.[0].length ?? 2);
485
+ continue;
486
+ }
487
+ else if (kind === 'bash' && char === '<' && next === '<') {
488
+ take(3);
489
+ continue;
490
+ }
491
+ else if (frame?.kind === 'subst' && char === '(')
492
+ frame.depth = (frame.depth ?? 0) + 1;
493
+ else if (frame?.kind === 'subst' && char === ')') {
494
+ if ((frame.depth ?? 0) === 0)
495
+ stack.pop();
496
+ else
497
+ frame.depth = (frame.depth ?? 0) - 1;
498
+ }
499
+ take(1);
500
+ }
501
+ return { command: out };
502
+ }
503
+ async function execute(command, kind, values, context) {
504
+ const result = await context.shell.runShell(command, kind, {
505
+ cwd: context.cwd,
506
+ timeoutMs: COMMAND_TIMEOUT_MS,
507
+ env: {
508
+ ...values,
509
+ ...(context.pluginRoot !== undefined ? { CLAUDE_PLUGIN_ROOT: context.pluginRoot.replace(/\\/gu, '/') } : {}),
510
+ ...(context.dataDir !== undefined ? { CLAUDE_PLUGIN_DATA: context.dataDir.replace(/\\/gu, '/') } : {}),
511
+ CLAUDE_PROJECT_DIR: context.cwd.replace(/\\/gu, '/'),
512
+ },
513
+ ...(context.signal !== undefined ? { signal: context.signal } : {}),
514
+ });
515
+ if (result.startError !== undefined)
516
+ return `[command failed: ${result.startError}]`;
517
+ // Claude Code merges stderr into stdout for injected commands.
518
+ return `${result.stdout}${result.stderr}`.trimEnd();
519
+ }
520
+ /**
521
+ * The `@path` references of a template's prose, with path variables expanded.
522
+ * A reference built from an argument is not one: what the person typed stays text.
523
+ */
524
+ function fileReferences(text, context) {
525
+ const found = [];
526
+ for (const match of text.matchAll(/(?:^|\s)@([^\s`'"()<>]+)/gmu)) {
527
+ const raw = (match[1] ?? '').replace(/[.,;:]+$/u, '');
528
+ if (!/[/\\.]/u.test(raw))
529
+ continue;
530
+ let fromArgument = false;
531
+ const reference = raw.replace(PLACEHOLDER, (whole, variable) => {
532
+ if (variable !== undefined)
533
+ return pathVariable(variable, context) ?? whole;
534
+ if (whole !== '\\$')
535
+ fromArgument = true;
536
+ return whole;
537
+ });
538
+ if (!fromArgument)
539
+ found.push(reference);
540
+ }
541
+ return found;
542
+ }
543
+ /**
544
+ * Read the referenced files that lie where the template may reach; name the
545
+ * others. A project's template reaches the workspace only; a plugin's its own
546
+ * directory and the workspace; only the person's own templates reach `~/.claude`.
547
+ */
548
+ function attach(references, context) {
549
+ const home = context.home ?? homedir();
550
+ const scope = context.scope ?? 'plugin';
551
+ const roots = [workspaceRootOf(context.cwd), ...scope === 'project' ? [] : [context.pluginRoot, context.skillDir], ...scope === 'user' ? [join(home, '.claude')] : []]
552
+ .filter((root) => root !== undefined)
553
+ .map(realOrLexical);
554
+ const files = [];
555
+ const refused = [];
556
+ const seen = new Set();
557
+ for (const reference of references) {
558
+ const path = reference.startsWith('~/') ? join(home, reference.slice(2)) : isAbsolute(reference) ? reference : resolve(context.cwd, reference);
559
+ let stat;
560
+ try {
561
+ stat = statSync(path);
562
+ }
563
+ catch {
564
+ continue; // A missing file stays literal text, as in Claude Code.
565
+ }
566
+ const real = realOrLexical(path);
567
+ if (seen.has(real) || !stat.isFile() || stat.size > FILE_LIMIT)
568
+ continue;
569
+ seen.add(real);
570
+ if (!roots.some(root => isInside(root, real))) {
571
+ refused.push(reference);
572
+ continue;
573
+ }
574
+ try {
575
+ files.push({ path: reference, content: readFileSync(real, 'utf8') });
576
+ }
577
+ catch {
578
+ // Unreadable references stay as literal text, as in Claude Code.
579
+ }
580
+ }
581
+ return { files, refused };
582
+ }
583
+ /** Append `@` attachments to a rendered prompt. */
584
+ export function withAttachments(rendered) {
585
+ if (rendered.files.length === 0)
586
+ return rendered.text;
587
+ return [rendered.text, ...rendered.files.map(file => `<file path="${file.path}">\n${file.content}\n</file>`)].join('\n\n');
588
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Small helpers every part of Harness Forge shares: error text, and the
3
+ * isolation of one part's start and teardown from the others'.
4
+ * @module @kirkchinese/harness-forge
5
+ */
6
+ /** Human-safe error text. */
7
+ export declare function messageOf(error: unknown): string;
8
+ /** A disposer, synchronous or not. */
9
+ export type Disposer = () => unknown;
10
+ /** Where a part's failure goes: the plugin's log. */
11
+ export interface FaultLog {
12
+ warn?(...args: unknown[]): void;
13
+ info?(...args: unknown[]): void;
14
+ }
15
+ /** The logger of a Cordis context, when it has one. */
16
+ export declare function loggerOf(ctx: object): FaultLog | undefined;
17
+ /**
18
+ * Run every disposer, in order, each awaited and each isolated: one that
19
+ * throws is logged and the rest still run.
20
+ */
21
+ export declare function disposeAll(log: FaultLog | undefined, label: string, disposers: readonly Disposer[]): Promise<void>;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Small helpers every part of Harness Forge shares: error text, and the
3
+ * isolation of one part's start and teardown from the others'.
4
+ * @module @kirkchinese/harness-forge
5
+ */
6
+ /** Human-safe error text. */
7
+ export function messageOf(error) {
8
+ return error instanceof Error ? error.message : String(error);
9
+ }
10
+ /** The logger of a Cordis context, when it has one. */
11
+ export function loggerOf(ctx) {
12
+ try {
13
+ return ctx.logger;
14
+ }
15
+ catch {
16
+ return undefined;
17
+ }
18
+ }
19
+ /**
20
+ * Run every disposer, in order, each awaited and each isolated: one that
21
+ * throws is logged and the rest still run.
22
+ */
23
+ export async function disposeAll(log, label, disposers) {
24
+ for (const dispose of disposers) {
25
+ try {
26
+ await dispose();
27
+ }
28
+ catch (error) {
29
+ log?.warn?.(`harness-forge: ${label} teardown step failed: ${messageOf(error)}`);
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,3 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ /** Register every `skills/<name>/SKILL.md` bundled with this package. */
3
+ export declare function registerBundledSkills(ctx: Context): void;