@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,31 @@
1
+ /**
2
+ * Turns as a stored session and a source session are compared, to tell which source session a stored one holds:
3
+ * what the person said and what the assistant answered, in text, read one turn at a time and only as far as the
4
+ * comparison needs.
5
+ * @module @kirkchinese/harness-forge
6
+ */
7
+ /** One turn, as compared: the person's prompt and the assistant's text answer, whitespace aside. */
8
+ export interface TurnText {
9
+ readonly prompt: string;
10
+ readonly reply: string;
11
+ }
12
+ /** The turns of one session, read in order and only as far as asked. */
13
+ export interface TurnReader {
14
+ /** Turn `index` (0-based); `undefined` past the last. Rejects when the session cannot be read. */
15
+ at(index: number): Promise<TurnText | undefined>;
16
+ /** Let go of what it reads from. */
17
+ close(): Promise<void>;
18
+ }
19
+ /** How two sessions' turns compare: the same throughout, parting ways, or not known (unread, or one only begins like the other). */
20
+ export type TurnComparison = 'same' | 'different' | 'unknown';
21
+ /** One turn as compared, from what it says. */
22
+ export declare function turnText(prompt: string, replies: readonly string[]): TurnText;
23
+ /** The text of the assistant's answer in one step's content: its `text` blocks (not its reasoning nor its tool calls). */
24
+ export declare function replyText(content: readonly unknown[]): string[];
25
+ /**
26
+ * A reader over turns handed out one by one (a generator that reads its source as it goes); the turns read are
27
+ * kept, so several comparisons read the source once.
28
+ */
29
+ export declare function turnReader(source: AsyncIterator<TurnText, unknown>): TurnReader;
30
+ /** Compare two sessions turn by turn, prompt and reply, stopping at the first turn that differs. */
31
+ export declare function compareTurns(a: TurnReader, b: TurnReader): Promise<TurnComparison>;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Turns as a stored session and a source session are compared, to tell which source session a stored one holds:
3
+ * what the person said and what the assistant answered, in text, read one turn at a time and only as far as the
4
+ * comparison needs.
5
+ * @module @kirkchinese/harness-forge
6
+ */
7
+ /** One turn as compared, from what it says. */
8
+ export function turnText(prompt, replies) {
9
+ return { prompt: collapse(prompt), reply: collapse(replies.join('\n')) };
10
+ }
11
+ function collapse(text) {
12
+ return text.trim().replace(/\s+/gu, ' ');
13
+ }
14
+ /** The text of the assistant's answer in one step's content: its `text` blocks (not its reasoning nor its tool calls). */
15
+ export function replyText(content) {
16
+ return content.flatMap((block) => {
17
+ const value = block;
18
+ return value?.type === 'text' && typeof value.text === 'string' ? [value.text] : [];
19
+ });
20
+ }
21
+ /**
22
+ * A reader over turns handed out one by one (a generator that reads its source as it goes); the turns read are
23
+ * kept, so several comparisons read the source once.
24
+ */
25
+ export function turnReader(source) {
26
+ const turns = [];
27
+ let done = false;
28
+ return {
29
+ async at(index) {
30
+ while (!done && turns.length <= index) {
31
+ const next = await source.next();
32
+ if (next.done === true)
33
+ done = true;
34
+ else
35
+ turns.push(next.value);
36
+ }
37
+ return turns[index];
38
+ },
39
+ async close() {
40
+ if (!done)
41
+ await source.return?.(undefined);
42
+ done = true;
43
+ },
44
+ };
45
+ }
46
+ /** Compare two sessions turn by turn, prompt and reply, stopping at the first turn that differs. */
47
+ export async function compareTurns(a, b) {
48
+ try {
49
+ for (let index = 0;; index++) {
50
+ const [left, right] = await Promise.all([a.at(index), b.at(index)]);
51
+ if (left === undefined && right === undefined)
52
+ return index === 0 ? 'unknown' : 'same';
53
+ // One goes on past the other: it went on after the import, or the stored one did in DSH.
54
+ if (left === undefined || right === undefined)
55
+ return 'unknown';
56
+ if (left.prompt !== right.prompt || left.reply !== right.reply)
57
+ return 'different';
58
+ }
59
+ }
60
+ catch {
61
+ return 'unknown';
62
+ }
63
+ }
@@ -0,0 +1,15 @@
1
+ /** Whether this Node can read compressed rollouts (`zlib.createZstdDecompress`, Node 22.15+). */
2
+ export declare function zstdAvailable(): boolean;
3
+ /**
4
+ * The first `maxBytes` of a zstd file's text, without decoding the rest: the compressed bytes are streamed through the
5
+ * decoder, which is dropped once enough text came out. A stream cut inside a frame (the read stops early) is fine,
6
+ * and so is the decoder's complaint about it.
7
+ */
8
+ export declare function decodeZstdHead(compressed: Uint8Array, maxBytes: number): Promise<string>;
9
+ /**
10
+ * The decoded bytes of a zstd file, streamed. Node's decoder stops after the first frame of a stream just as
11
+ * the one-shot decoder does, so the frames are found by walking their headers and block sizes as the file is read,
12
+ * and each is decoded by a decoder of its own: nothing but one frame's current block is held. Throws on a malformed
13
+ * or truncated file, naming `path`.
14
+ */
15
+ export declare function zstdChunks(path: string): AsyncGenerator<Uint8Array>;
@@ -0,0 +1,152 @@
1
+ /**
2
+ * zstd decoding of compressed Codex rollouts (`rollout-*.jsonl.zst`, Codex 0.167+) with Node's own `zlib`.
3
+ * A file written in pieces is several concatenated zstd frames, and Node's decoder decodes only the first; so the
4
+ * frames are found by walking their headers and block sizes as the file streams by, and decoded one by one.
5
+ * @module @kirkchinese/harness-forge
6
+ */
7
+ import { createReadStream } from 'node:fs';
8
+ import { Readable } from 'node:stream';
9
+ import * as zlib from 'node:zlib';
10
+ import { READ_CHUNK_BYTES } from '@kirkchinese/harness-forge-core';
11
+ const FRAME_MAGIC = 0xFD2FB528;
12
+ const SKIPPABLE_MASK = 0xFFFFFFF0;
13
+ const SKIPPABLE_MAGIC = 0x184D2A50;
14
+ /** Whether this Node can read compressed rollouts (`zlib.createZstdDecompress`, Node 22.15+). */
15
+ export function zstdAvailable() {
16
+ return typeof zlib.createZstdDecompress === 'function';
17
+ }
18
+ /**
19
+ * The first `maxBytes` of a zstd file's text, without decoding the rest: the compressed bytes are streamed through the
20
+ * decoder, which is dropped once enough text came out. A stream cut inside a frame (the read stops early) is fine,
21
+ * and so is the decoder's complaint about it.
22
+ */
23
+ export function decodeZstdHead(compressed, maxBytes) {
24
+ const create = zlib.createZstdDecompress;
25
+ if (create === undefined)
26
+ return Promise.reject(new Error('this Node has no zstd support (Node 22.15+ has it)'));
27
+ return new Promise((resolve) => {
28
+ const chunks = [];
29
+ let size = 0;
30
+ let done = false;
31
+ const decoder = create();
32
+ const finish = () => {
33
+ if (done)
34
+ return;
35
+ done = true;
36
+ decoder.destroy();
37
+ resolve(Buffer.concat(chunks).subarray(0, maxBytes).toString('utf8'));
38
+ };
39
+ decoder.on('data', (chunk) => {
40
+ chunks.push(chunk);
41
+ size += chunk.length;
42
+ if (size >= maxBytes)
43
+ finish();
44
+ });
45
+ decoder.on('end', finish);
46
+ decoder.on('error', finish);
47
+ decoder.on('close', finish);
48
+ Readable.from([Buffer.from(compressed.buffer, compressed.byteOffset, compressed.byteLength)]).pipe(decoder);
49
+ });
50
+ }
51
+ /** Reads exactly-sized pieces from a byte stream, holding no more than the chunk being read. */
52
+ class ByteReader {
53
+ fail;
54
+ source;
55
+ pending = Buffer.alloc(0);
56
+ constructor(source, fail) {
57
+ this.fail = fail;
58
+ this.source = source[Symbol.asyncIterator]();
59
+ }
60
+ /** The next `size` bytes; `undefined` at the end of the stream when `optional` and nothing is left, else a malformed file. */
61
+ async take(size, optional = false) {
62
+ const parts = [];
63
+ let have = 0;
64
+ while (have < size) {
65
+ if (this.pending.length === 0) {
66
+ const next = await this.source.next();
67
+ if (next.done === true) {
68
+ if (optional && have === 0)
69
+ return undefined;
70
+ return this.fail();
71
+ }
72
+ this.pending = Buffer.from(next.value.buffer, next.value.byteOffset, next.value.byteLength);
73
+ continue;
74
+ }
75
+ const piece = this.pending.subarray(0, size - have);
76
+ this.pending = this.pending.subarray(piece.length);
77
+ parts.push(piece);
78
+ have += piece.length;
79
+ }
80
+ return parts.length === 1 ? parts[0] : Buffer.concat(parts);
81
+ }
82
+ async skip(size) {
83
+ for (let left = size; left > 0; left -= 65536)
84
+ await this.take(Math.min(left, 65536));
85
+ }
86
+ }
87
+ /** The bytes of one data frame, in the pieces its structure falls into (header, then each block); `first` is its magic, already read. */
88
+ async function* framePieces(reader, first, fail) {
89
+ yield first;
90
+ const descriptor = (await reader.take(1))[0];
91
+ const contentSizeFlag = descriptor >> 6;
92
+ const singleSegment = (descriptor >> 5) & 1;
93
+ const checksum = (descriptor >> 2) & 1;
94
+ const dictionaryFlag = descriptor & 3;
95
+ const rest = (singleSegment === 1 ? 0 : 1) + [0, 1, 2, 4][dictionaryFlag] + [singleSegment === 1 ? 1 : 0, 2, 4, 8][contentSizeFlag];
96
+ yield Buffer.concat([Buffer.from([descriptor]), rest > 0 ? (await reader.take(rest)) : Buffer.alloc(0)]);
97
+ for (let last = false; !last;) {
98
+ const head = (await reader.take(3));
99
+ const header = head[0] | (head[1] << 8) | (head[2] << 16);
100
+ last = (header & 1) === 1;
101
+ const type = (header >> 1) & 3;
102
+ if (type === 3)
103
+ fail();
104
+ // A run-length block stores one byte, however long the run it stands for.
105
+ const body = (await reader.take(type === 1 ? 1 : header >>> 3));
106
+ yield Buffer.concat([head, body]);
107
+ }
108
+ if (checksum === 1)
109
+ yield (await reader.take(4));
110
+ }
111
+ /**
112
+ * The decoded bytes of a zstd file, streamed. Node's decoder stops after the first frame of a stream just as
113
+ * the one-shot decoder does, so the frames are found by walking their headers and block sizes as the file is read,
114
+ * and each is decoded by a decoder of its own: nothing but one frame's current block is held. Throws on a malformed
115
+ * or truncated file, naming `path`.
116
+ */
117
+ export async function* zstdChunks(path) {
118
+ const create = zlib.createZstdDecompress;
119
+ if (create === undefined)
120
+ throw new Error(`cannot read ${path}: this Node has no zstd support (Node 22.15+ has it)`);
121
+ const fail = () => { throw new Error(`cannot read ${path}: malformed or truncated zstd data`); };
122
+ const file = createReadStream(path, { highWaterMark: READ_CHUNK_BYTES });
123
+ const reader = new ByteReader(file, fail);
124
+ try {
125
+ for (;;) {
126
+ const magicBytes = await reader.take(4, true);
127
+ if (magicBytes === undefined)
128
+ return;
129
+ const magic = magicBytes.readUInt32LE(0);
130
+ if ((magic & SKIPPABLE_MASK) >>> 0 === SKIPPABLE_MAGIC) {
131
+ await reader.skip((await reader.take(4)).readUInt32LE(0));
132
+ continue;
133
+ }
134
+ if (magic !== FRAME_MAGIC)
135
+ fail();
136
+ const decoder = create();
137
+ const source = Readable.from(framePieces(reader, magicBytes, fail));
138
+ source.on('error', (error) => { decoder.destroy(error); });
139
+ try {
140
+ for await (const chunk of source.pipe(decoder))
141
+ yield chunk;
142
+ }
143
+ finally {
144
+ source.destroy();
145
+ decoder.destroy();
146
+ }
147
+ }
148
+ }
149
+ finally {
150
+ file.destroy();
151
+ }
152
+ }
@@ -0,0 +1,169 @@
1
+ /**
2
+ * What a skill's or command's frontmatter asks of the turn that invokes it,
3
+ * as Claude Code applies it (commands and skills share the fields):
4
+ *
5
+ * - `allowed-tools`: allow rules for that turn; deny and ask rules still win.
6
+ * `${CLAUDE_SKILL_DIR}`, `${CLAUDE_PROJECT_DIR}`, `${CLAUDE_PLUGIN_ROOT}`
7
+ * and `${CLAUDE_PLUGIN_DATA}` expand inside the rules.
8
+ * - `disallowed-tools`: the tools are refused while the skill is active.
9
+ * - `model` / `effort`: the rest of the turn runs on that model and effort.
10
+ * A value naming no model DSH has (a Claude alias such as `opus`) keeps the
11
+ * session's model, and the session records why.
12
+ * - `context: fork`: the rendered content runs as the task of a new delegated
13
+ * agent (`agent` names its type, `general-purpose` by default) and its
14
+ * result comes back; `background: false` waits for it in the invoking turn,
15
+ * otherwise the result arrives as a message when the agent finishes.
16
+ *
17
+ * Everything clears when the user sends the next message.
18
+ * @module @kirkchinese/harness-forge
19
+ */
20
+ import type { Context } from '@deepseek-ai/cordis';
21
+ /** Where a skill or command lives, for the variables its rules may name. */
22
+ export interface InvocationPlaces {
23
+ readonly skillDir?: string;
24
+ readonly pluginRoot?: string;
25
+ readonly dataDir?: string;
26
+ }
27
+ /** One skill's or command's invocation frontmatter. */
28
+ export interface InvocationSpec {
29
+ /** The DSH name: the skill name, or the command name. */
30
+ readonly name: string;
31
+ readonly kind: 'skill' | 'command';
32
+ /** The name its own harness invokes it by (`plugin:skill`, or bare for the user's own). */
33
+ readonly foreignName?: string;
34
+ /** `plugin`, `user` or `project`, as `UserPromptExpansion` reports `command_source`. */
35
+ readonly source?: string;
36
+ readonly pluginId: string;
37
+ readonly componentId: string;
38
+ readonly frontmatter: Readonly<Record<string, unknown>>;
39
+ readonly places: InvocationPlaces;
40
+ }
41
+ /** The parsed directives of a spec. */
42
+ export interface Directives {
43
+ readonly allowedTools: readonly string[];
44
+ readonly disallowedTools: readonly string[];
45
+ readonly model?: string;
46
+ readonly effort?: string;
47
+ readonly fork?: {
48
+ readonly agent?: string;
49
+ readonly background: boolean;
50
+ };
51
+ }
52
+ /** Read the invocation fields of a frontmatter. */
53
+ export declare function directivesOf(frontmatter: Readonly<Record<string, unknown>>): Directives;
54
+ /** Expand the path variables a rule may name. */
55
+ export declare function expandRule(rule: string, places: InvocationPlaces & {
56
+ readonly projectDir: string;
57
+ }): string;
58
+ interface AgentLike {
59
+ readonly id?: string;
60
+ readonly options?: {
61
+ readonly provider?: string;
62
+ readonly model?: string;
63
+ readonly reasoningEffort?: string;
64
+ };
65
+ readonly session: {
66
+ readonly header: {
67
+ readonly id: string;
68
+ readonly cwd?: string;
69
+ };
70
+ };
71
+ followup(message: unknown): void;
72
+ }
73
+ /** Per-session invocation state of foreign skills and commands. */
74
+ export declare class Invocations {
75
+ private readonly ctx;
76
+ private readonly presetOf;
77
+ private readonly directory;
78
+ private readonly specs;
79
+ private readonly turns;
80
+ private readonly pending;
81
+ /**
82
+ * A command submitted its prompt: its frontmatter takes effect in the turn
83
+ * that prompt starts, after that turn clears the previous grants.
84
+ */
85
+ queue(sessionId: string, spec: InvocationSpec): void;
86
+ /** Withdraw a spec queued for a session's next turn (its prompt was not submitted after all). */
87
+ unqueue(sessionId: string, spec: InvocationSpec): void;
88
+ /** Take the specs queued for a session's next turn. */
89
+ takeQueued(sessionId: string): InvocationSpec[];
90
+ /**
91
+ * @param presetOf - the DSH preset of a foreign agent name, for `agent`.
92
+ * @param directory - the session's working directory.
93
+ */
94
+ constructor(ctx: Context, presetOf: (foreignName: string) => string | undefined, directory: (agent: AgentLike, signal: AbortSignal) => Promise<string>);
95
+ /** Make a skill's frontmatter take effect when the skill is invoked. Returns the withdrawal. */
96
+ registerSkill(spec: InvocationSpec): () => void;
97
+ /** The registered spec of a skill name. */
98
+ skill(name: string): InvocationSpec | undefined;
99
+ /** The turn's tool grants of a session, as allow and deny rules. */
100
+ rules(sessionId: string): {
101
+ allow: readonly string[];
102
+ deny: readonly string[];
103
+ };
104
+ /** The skills and commands active in a session's turn. */
105
+ active(sessionId: string): readonly string[];
106
+ /** The user sent a new message: grants and overrides end. */
107
+ clear(sessionId: string): void;
108
+ /**
109
+ * DSH 0.2.2 fixes a step's route before `agent/pre-step`, so a route is selected on the agent ahead of the step
110
+ * that must run on it: a typed command's or skill's when the line is dispatched (its turn has not begun), a
111
+ * model-invoked skill's during the step that loads it. The route the agent had is put back when that turn ends.
112
+ */
113
+ private readonly selected;
114
+ private applyRoute;
115
+ /** A typed command or skill line is about to run: its frontmatter route is selected for the turn it starts. */
116
+ preselect(agent: AgentLike, spec: InvocationSpec, signal: AbortSignal): Promise<void>;
117
+ /** A session's turn started: a route selected ahead of it now belongs to it. */
118
+ turnStarted(sessionId: string): void;
119
+ /**
120
+ * A session's turn ended: the route it ran on is put back. Called from a `session/event` listener, which runs
121
+ * while DSH publishes the event and may not append (selecting a route appends `model/selection`), so the
122
+ * selection is put back right after; the next step reads the route only later.
123
+ */
124
+ turnEnded(sessionId: string): void;
125
+ /** A typed line was answered without starting a turn: the route it selected is put back now. */
126
+ release(sessionId: string): void;
127
+ /** Apply a spec's grants and overrides to the agent's current turn. */
128
+ activate(agent: AgentLike, spec: InvocationSpec, signal: AbortSignal): Promise<Directives>;
129
+ /**
130
+ * Run rendered content in a forked agent. With `background`, return at once
131
+ * and deliver the result to the parent when the agent finishes.
132
+ * @returns the result text, or the launch note in the background case.
133
+ */
134
+ fork(agent: AgentLike, spec: InvocationSpec, directives: Directives, content: string, signal: AbortSignal): Promise<string>;
135
+ private expandedRules;
136
+ /**
137
+ * Resolve a frontmatter `model` and `effort` to a DSH route: an exact
138
+ * `provider/model`, a model id or name one provider lists (the current
139
+ * provider first), and an effort the model offers by name or, failing that,
140
+ * by rank.
141
+ */
142
+ private route;
143
+ }
144
+ /** A requested model and effort resolved against what DSH serves. */
145
+ export interface RouteResolution {
146
+ readonly route?: {
147
+ provider?: string;
148
+ model?: string;
149
+ reasoningEffort?: string;
150
+ };
151
+ /** Why a requested value was not applied. */
152
+ readonly note?: string;
153
+ }
154
+ /**
155
+ * Resolve a Claude `model` and `effort` to a DSH route: an exact
156
+ * `provider/model`, else a model id or name one provider lists (the current
157
+ * provider first); an effort the model offers by id or name, else by rank
158
+ * among Claude's `low`…`max`. `inherit` keeps the current model.
159
+ */
160
+ export declare function resolveRoute(ctx: Context, current: {
161
+ readonly provider?: string;
162
+ readonly model?: string;
163
+ }, wantedModel: string | undefined, wantedEffort: string | undefined, signal: AbortSignal): Promise<RouteResolution>;
164
+ /** The Claude family a `model` value names: an alias (`haiku`, `sonnet`, `opus`, `opusplan`) or a `claude-…` id. */
165
+ export declare function claudeFamily(wanted: string): ClaudeFamily | undefined;
166
+ export type ClaudeFamily = 'haiku' | 'sonnet' | 'opus';
167
+ /** The person's mapping of Claude aliases to served models (Harness Forge's `modelAliases` config). */
168
+ export declare function setModelAliases(aliases: Readonly<Partial<Record<ClaudeFamily, string>>> | undefined): void;
169
+ export {};