@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,48 @@
1
+ import { type TrustSource } from '@kirkchinese/harness-forge-core';
2
+ /** The outcome message of a project part held back until the workspace is trusted. */
3
+ export declare const WAITING_FOR_TRUST = "waiting for you to trust this workspace";
4
+ /** The outcome message of a project `.mcp.json` server Claude Code has not approved (and Harness Forge was not asked to trust). */
5
+ export declare const WAITING_FOR_APPROVAL = "waiting for approval: approve it in Claude Code, or trust this workspace in Harness Forge";
6
+ /** Why a project's command or skill runs no shell line: the reason its rendering shows. */
7
+ export declare function untrustedShell(workspace: string): string;
8
+ /** Whether the workspace of a directory is trusted now, as commands and skills ask when they render. */
9
+ export type TrustCheck = (dir: string) => {
10
+ readonly trusted: boolean;
11
+ readonly workspace: string;
12
+ };
13
+ /** A workspace's trust as the workbench shows it. */
14
+ export interface WorkspaceTrustState {
15
+ readonly workspace: string;
16
+ readonly trusted: boolean;
17
+ /** Where the trust comes from; empty when untrusted. */
18
+ readonly sources: readonly TrustSource[];
19
+ /** The person chose "Not now" for this workspace: its banner stays hidden. */
20
+ readonly dismissed: boolean;
21
+ }
22
+ /** A workspace Harness Forge has a decision on, for the Trust settings list. */
23
+ export interface TrustRecord extends WorkspaceTrustState {
24
+ readonly trustedAt?: string;
25
+ readonly dismissedAt?: string;
26
+ }
27
+ /** The per-profile store of workspaces trusted in Harness Forge. */
28
+ export declare class WorkspaceTrust {
29
+ private readonly home;
30
+ private readonly path;
31
+ private readonly listeners;
32
+ constructor(statePath: string, home?: string);
33
+ private read;
34
+ private write;
35
+ /** The trust of the workspace that holds `cwd`. */
36
+ state(cwd: string): WorkspaceTrustState;
37
+ /** The workspaces Harness Forge holds a decision on (trusted here, or dismissed), most recent first. */
38
+ list(): TrustRecord[];
39
+ /**
40
+ * Trust (or stop trusting) the workspace that holds `cwd` in Harness Forge; trust kept by Claude Code or Codex stays theirs.
41
+ * Trusting ends a dismissal; revoking leaves the workspace undecided again.
42
+ */
43
+ set(cwd: string, trusted: boolean): WorkspaceTrustState;
44
+ /** Hide (or show again) the trust banner of the workspace that holds `cwd`; this trusts nothing. */
45
+ dismiss(cwd: string, dismissed: boolean): WorkspaceTrustState;
46
+ /** Hear each change made here. */
47
+ onChange(listener: (workspace: string, trusted: boolean) => void): () => void;
48
+ }
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Workspace trust: whether a project's parts that run code or reach the
3
+ * network may run in DSH. A workspace is trusted when the person accepted
4
+ * Claude Code's trust dialog for it, Codex marks it trusted, or they trusted
5
+ * it in Harness Forge. Harness Forge's own grants live in `trust.json` beside
6
+ * the ledger (per DSH profile), never in the profile's DSH configuration.
7
+ * See `@kirkchinese/harness-forge-core` `trust.ts` for the rules.
8
+ * @module @kirkchinese/harness-forge
9
+ */
10
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
11
+ import { homedir } from 'node:os';
12
+ import { dirname, join, resolve } from 'node:path';
13
+ import { claudeCodeTrusts, codexTrusts, trustKeys, workspaceRootOf } from '@kirkchinese/harness-forge-core';
14
+ /** The outcome message of a project part held back until the workspace is trusted. */
15
+ export const WAITING_FOR_TRUST = 'waiting for you to trust this workspace';
16
+ /** The outcome message of a project `.mcp.json` server Claude Code has not approved (and Harness Forge was not asked to trust). */
17
+ export const WAITING_FOR_APPROVAL = 'waiting for approval: approve it in Claude Code, or trust this workspace in Harness Forge';
18
+ /** Why a project's command or skill runs no shell line: the reason its rendering shows. */
19
+ export function untrustedShell(workspace) {
20
+ return `the workspace ${workspace} is not trusted (trust it in Harness Forge → Migrated)`;
21
+ }
22
+ /** The per-profile store of workspaces trusted in Harness Forge. */
23
+ export class WorkspaceTrust {
24
+ home;
25
+ path;
26
+ listeners = new Set();
27
+ constructor(statePath, home = homedir()) {
28
+ this.home = home;
29
+ this.path = join(dirname(statePath), 'trust.json');
30
+ }
31
+ read() {
32
+ try {
33
+ const raw = JSON.parse(readFileSync(this.path, 'utf8'));
34
+ if (typeof raw === 'object' && raw !== null && typeof raw.workspaces === 'object' && raw.workspaces !== null)
35
+ return raw;
36
+ }
37
+ catch {
38
+ // Absent or unreadable: nothing is trusted here.
39
+ }
40
+ return { schemaVersion: 1, workspaces: {}, dismissed: {} };
41
+ }
42
+ write(workspaces, dismissed) {
43
+ mkdirSync(dirname(this.path), { recursive: true });
44
+ const temporary = `${this.path}.tmp`;
45
+ writeFileSync(temporary, `${JSON.stringify({ schemaVersion: 1, workspaces, dismissed }, null, 2)}\n`, 'utf8');
46
+ renameSync(temporary, this.path);
47
+ }
48
+ /** The trust of the workspace that holds `cwd`. */
49
+ state(cwd) {
50
+ const workspace = workspaceRootOf(resolve(cwd));
51
+ const granted = this.read().workspaces;
52
+ const sources = [];
53
+ if (claudeCodeTrusts(workspace, this.home))
54
+ sources.push('claude-code');
55
+ if (codexTrusts(workspace, this.home))
56
+ sources.push('codex');
57
+ if (trustKeys(workspace, this.home).some(key => granted[key] !== undefined))
58
+ sources.push('forge');
59
+ return { workspace, trusted: sources.length > 0, sources, dismissed: this.read().dismissed?.[workspace] !== undefined };
60
+ }
61
+ /** The workspaces Harness Forge holds a decision on (trusted here, or dismissed), most recent first. */
62
+ list() {
63
+ const stored = this.read();
64
+ const roots = new Set([...Object.keys(stored.workspaces), ...Object.keys(stored.dismissed ?? {})]);
65
+ return [...roots].map((root) => {
66
+ const trustedAt = stored.workspaces[root]?.trustedAt;
67
+ const dismissedAt = stored.dismissed?.[root]?.dismissedAt;
68
+ return { ...this.state(root), workspace: root, ...(trustedAt !== undefined ? { trustedAt } : {}), ...(dismissedAt !== undefined ? { dismissedAt } : {}) };
69
+ }).sort((left, right) => (right.trustedAt ?? right.dismissedAt ?? '').localeCompare(left.trustedAt ?? left.dismissedAt ?? ''));
70
+ }
71
+ /**
72
+ * Trust (or stop trusting) the workspace that holds `cwd` in Harness Forge; trust kept by Claude Code or Codex stays theirs.
73
+ * Trusting ends a dismissal; revoking leaves the workspace undecided again.
74
+ */
75
+ set(cwd, trusted) {
76
+ const workspace = workspaceRootOf(resolve(cwd));
77
+ const current = this.read();
78
+ const workspaces = { ...current.workspaces };
79
+ const dismissed = { ...current.dismissed };
80
+ delete dismissed[workspace];
81
+ if (trusted)
82
+ workspaces[workspace] = { trustedAt: new Date().toISOString() };
83
+ else
84
+ delete workspaces[workspace];
85
+ this.write(workspaces, dismissed);
86
+ for (const listener of [...this.listeners])
87
+ listener(workspace, trusted);
88
+ return this.state(workspace);
89
+ }
90
+ /** Hide (or show again) the trust banner of the workspace that holds `cwd`; this trusts nothing. */
91
+ dismiss(cwd, dismissed) {
92
+ const workspace = workspaceRootOf(resolve(cwd));
93
+ const current = this.read();
94
+ const marks = { ...current.dismissed };
95
+ if (dismissed)
96
+ marks[workspace] = { dismissedAt: new Date().toISOString() };
97
+ else
98
+ delete marks[workspace];
99
+ this.write(current.workspaces, marks);
100
+ return this.state(workspace);
101
+ }
102
+ /** Hear each change made here. */
103
+ onChange(listener) {
104
+ this.listeners.add(listener);
105
+ return () => { this.listeners.delete(listener); };
106
+ }
107
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The turn lifecycle of invoked skills and commands, which every row's
3
+ * frontmatter effects share: a user's message ends the previous turn's
4
+ * grants and starts the queued command's, a typed `/skill` line runs on the
5
+ * skill's route from its turn's first step, and a route ends with its turn.
6
+ * The `harness-forge` row listens for these.
7
+ * @module @kirkchinese/harness-forge
8
+ */
9
+ import type { Context } from '@deepseek-ai/cordis';
10
+ import type { Disposer } from './contract.js';
11
+ import type { HarnessForge } from './service.js';
12
+ /** Listen for the turn lifecycle. Returns the disposers, one per listener. */
13
+ export declare function turnListeners(ctx: Context, forge: HarnessForge): Array<[string, () => Disposer | undefined]>;
@@ -0,0 +1,56 @@
1
+ import { interceptCommands } from '../dsh/commands.js';
2
+ import { dshService, onDsh } from '../dsh/host.js';
3
+ /** Listen for the turn lifecycle. Returns the disposers, one per listener. */
4
+ export function turnListeners(ctx, forge) {
5
+ const invocations = forge.invocations;
6
+ return [
7
+ ['turn start', () => onDsh(ctx, 'agent/pre-step', (async (payload, next) => {
8
+ const { agent, messages, signal } = payload;
9
+ // A forked skill's agent keeps its grants for its whole run; only a user's message ends a turn's.
10
+ if ((agent.session.header.delegationDepth ?? 0) === 0 && messages.some(message => message.source?.kind === 'user')) {
11
+ const sessionId = agent.session.header.id;
12
+ invocations.clear(sessionId);
13
+ // A command's frontmatter belongs to the turn its submitted prompt starts.
14
+ for (const spec of invocations.takeQueued(sessionId))
15
+ await invocations.activate(agent, spec, signal);
16
+ }
17
+ return next();
18
+ }), { prepend: true, owner: 'harness-forge' })],
19
+ ['turn end', () => onDsh(ctx, 'session/event', ((session, event) => {
20
+ const id = session.header?.id ?? session.id;
21
+ if (id === undefined)
22
+ return;
23
+ if (event.type === 'turn/start')
24
+ invocations.turnStarted(id);
25
+ else if (event.type === 'turn/end')
26
+ invocations.turnEnded(id);
27
+ }), { owner: 'harness-forge' })],
28
+ // A typed `/skill` line runs on the skill's route from its turn's first step (DSH 0.2.2 fixes a step's route
29
+ // before agent/pre-step, where the skill is otherwise activated); the route ends with that turn.
30
+ ['typed skills', () => {
31
+ const commands = dshService(ctx, 'commands');
32
+ if (commands === undefined)
33
+ return undefined;
34
+ return interceptCommands(commands, async (args, next) => {
35
+ const [agent, line, , signal] = args;
36
+ const name = /^\/([^\s]+)/u.exec(String(line ?? '').trim())?.[1];
37
+ const spec = name === undefined ? undefined : invocations.skill(name);
38
+ if (spec === undefined || agent === undefined)
39
+ return next();
40
+ await invocations.preselect(agent, spec, signal ?? new AbortController().signal).catch(() => undefined);
41
+ // A line that fails or is refused starts no turn: the route it selected is put back now, not after an unrelated turn.
42
+ let execution;
43
+ try {
44
+ execution = await next();
45
+ }
46
+ catch (error) {
47
+ invocations.release(agent.session.header.id);
48
+ throw error;
49
+ }
50
+ if (execution?.result?.kind !== 'success')
51
+ invocations.release(agent.session.header.id);
52
+ return execution;
53
+ });
54
+ }],
55
+ ];
56
+ }
@@ -0,0 +1,112 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import { type ContextFormed } from '@deepseek-ai/dsh-llm';
3
+ import { type HookNotice } from '../notice.js';
4
+ import { TranscriptMirror } from '../transcript.js';
5
+ import type { AgentLike, HookBrokerHost, HookSource, Occurrence, Ran, SessionLike, SessionState } from './types.js';
6
+ declare module '@deepseek-ai/dsh-llm' {
7
+ interface MessageSourceMap {
8
+ 'harness-forge': {
9
+ kind: 'harness-forge';
10
+ readonly event?: string;
11
+ } & ContextFormed;
12
+ }
13
+ }
14
+ export declare const CONTEXT_SOURCE: {
15
+ readonly kind: "harness-forge";
16
+ };
17
+ /**
18
+ * The broker's shared engine: the registered sources, per-session state, the
19
+ * handler runs and how their outcomes combine, notices, environment files and
20
+ * the host facts every part of the broker reads.
21
+ */
22
+ export declare class HookEngine {
23
+ readonly ctx: Context;
24
+ readonly host: HookBrokerHost;
25
+ readonly sources: Map<string, HookSource>;
26
+ readonly sessions: Map<string, SessionState>;
27
+ private readonly runner;
28
+ readonly transcripts: TranscriptMirror;
29
+ readonly detached: Set<Promise<unknown>>;
30
+ readonly ending: Set<Promise<unknown>>;
31
+ readonly lifetime: AbortController;
32
+ private readonly onceDone;
33
+ readonly notifyTimers: Map<string, NodeJS.Timeout>;
34
+ readonly idleTimers: Map<string, NodeJS.Timeout>;
35
+ constructor(ctx: Context, host: HookBrokerHost);
36
+ /** Register one source; the returned disposer removes it. */
37
+ add(source: HookSource): () => void;
38
+ has(event: string): boolean;
39
+ hasAgentStop(agent: AgentLike): boolean;
40
+ /**
41
+ * Run every matching handler of one occurrence in parallel and decode the
42
+ * synchronous ones. Background handlers deliver their output later.
43
+ */
44
+ run(occurrence: Occurrence & {
45
+ response?: {
46
+ value: unknown;
47
+ text: string;
48
+ };
49
+ }, extra?: {
50
+ envFile?: string;
51
+ }): Promise<Ran[]>;
52
+ private runHandler;
53
+ /** Whether a failed run of this handler blocks the action. */
54
+ private blocksOnFailure;
55
+ /** Whether a decoded output allows the action outright. */
56
+ private allows;
57
+ /** Deliver a background run: context and system message on the next model request; asyncRewake wakes the agent on exit 2. */
58
+ private deliverAsync;
59
+ /** The stdin payload / POST body of one handler. */
60
+ private payload;
61
+ private timeoutFor;
62
+ /** The SessionEnd budget: 1.5 s, raised by settings hooks' own timeouts up to 60 s, or the env override. */
63
+ sessionEndBudget(): number;
64
+ sessionEndDefault(): number;
65
+ /** The first `continue: false`, as its stop reason. */
66
+ stopOf(results: readonly Ran[]): string | undefined;
67
+ /** A top-level `decision: "block"` or a blocking exit, as the block reason. */
68
+ blockOf(event: string, results: readonly Ran[]): string | undefined;
69
+ /** A prompt/agent `ok: false`. */
70
+ modelBlock(results: readonly Ran[], continueOnBlock: boolean): string | undefined;
71
+ /** Every context string the runs add for the model. */
72
+ contexts(event: string, results: readonly Ran[]): string[];
73
+ /** Apply the 10,000-character cap (Codex: `additionalContextLimit` tokens): spill to a file with a preview. */
74
+ private bound;
75
+ /** Notices for hook errors, system messages and terminal notifications of every run. */
76
+ noticeAll(agent: AgentLike, event: string, subject: string | undefined, results: readonly Ran[]): void;
77
+ notice(agent: AgentLike | undefined, notice: HookNotice): void;
78
+ /** `continue: false` or an ending block: stop the agent and tell the user why. */
79
+ halt(agent: AgentLike, event: string, reason: string, results: readonly Ran[]): void;
80
+ /** Context for the model, as a system reminder naming the hook event. */
81
+ reminder(_agent: AgentLike | undefined, event: string, text: string): string;
82
+ contextMessage(agent: AgentLike | undefined, event: string, text: string): unknown;
83
+ /** The env file a session's SessionStart (`session`) or CwdChanged/FileChanged (`cwd`) hooks append to. */
84
+ envFile(session: SessionLike, scope: 'session' | 'cwd'): string;
85
+ clearEnv(session: SessionLike, scope: 'cwd'): void;
86
+ /**
87
+ * The prefix that applies imported settings `env`, then the session's env
88
+ * files, to one shell tool command.
89
+ */
90
+ shellEnvPrefix(agent: AgentLike, dshName: string, extra?: Readonly<Record<string, string>>): string | undefined;
91
+ state(session: SessionLike): SessionState & {
92
+ cwd?: string;
93
+ lastSummary?: string;
94
+ };
95
+ private sourceApplies;
96
+ toolContext(agent?: AgentLike, cwd?: string): {
97
+ cwd?: string;
98
+ agentName: (id: string) => string | undefined;
99
+ presetOf: (name: string) => string | undefined;
100
+ };
101
+ agentOf(sessionId: string): AgentLike | undefined;
102
+ directory(agent: AgentLike, signal: AbortSignal): Promise<string>;
103
+ committedDirectory(agent: AgentLike): string | undefined;
104
+ private lastTurn;
105
+ presetOf(agent: AgentLike): string | undefined;
106
+ /** Claude's `agent_type`: the imported agent's own name, else `general-purpose`. */
107
+ agentTypeOf(child: AgentLike): string;
108
+ /** The foreign permission mode for the session's DSH permission preset and plan mode. */
109
+ private permissionMode;
110
+ rename(agent: AgentLike, title: string): void;
111
+ track(promise: Promise<unknown>): void;
112
+ }