@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,262 @@
1
+ /**
2
+ * The hook broker's types: what an imported hooks configuration, its host and
3
+ * a permission verdict look like, and structural views of the DSH objects the
4
+ * broker touches.
5
+ * @module @kirkchinese/harness-forge
6
+ */
7
+ import type { FSWatcher } from 'node:fs';
8
+ import type { ContentBlock } from '@deepseek-ai/dsh-llm';
9
+ import type { HookHandler, HookTable } from '../config.js';
10
+ import type { Decoded } from '../output.js';
11
+ import type { ModelVerdict } from '../runner.js';
12
+ import type { ForeignShell } from '../shell.js';
13
+ import type { HookDialect } from '../vocabulary.js';
14
+ /** One imported hooks configuration. */
15
+ export interface HookSource {
16
+ readonly componentId: string;
17
+ readonly pluginId: string;
18
+ readonly dialect: HookDialect;
19
+ readonly pluginRoot: string;
20
+ readonly dataDir: string;
21
+ readonly table: HookTable;
22
+ /** Where the hooks are declared; only skill hooks honor `once`, only settings hooks deduplicate. */
23
+ readonly origin?: 'plugin' | 'settings' | 'skill' | 'agent';
24
+ /** Skill hooks: active in this session after the skill ran. */
25
+ readonly sessionId?: string;
26
+ /** Agent hooks: active while this delegated session runs; their `Stop` runs as `SubagentStop`. */
27
+ readonly agentSessionId?: string;
28
+ /** Plugin user-config values. */
29
+ readonly options?: Readonly<Record<string, string>>;
30
+ }
31
+ /** One handler run, reported to the evidence ledger. */
32
+ export interface HookObservation {
33
+ readonly componentId: string;
34
+ readonly event: string;
35
+ readonly ok: boolean;
36
+ readonly detail: string;
37
+ }
38
+ /** What the broker needs from its host. */
39
+ export interface HookBrokerHost {
40
+ readonly shell: ForeignShell;
41
+ /** Variables other runtimes add to a shell call's environment (OpenCode's `shell.env`). */
42
+ shellEnv?(exec: ToolExecutionLike): Promise<Record<string, string>>;
43
+ /** Profile-local writable directory: transcripts, env files, spilled output. */
44
+ readonly dataRoot: string;
45
+ observe(observation: HookObservation): void;
46
+ /** The foreign agent name (`plugin:agent`) of an imported agent preset. */
47
+ agentName(presetId: string): string | undefined;
48
+ /** The DSH preset of a foreign agent name. */
49
+ presetOf?(foreignName: string): string | undefined;
50
+ /** Hooks declared in an imported skill's frontmatter, by DSH skill name. */
51
+ skillHooks?(skillName: string): HookSource | undefined;
52
+ /** Hooks declared in an imported agent's frontmatter, by preset id. */
53
+ agentHooks?(presetId: string): HookSource | undefined;
54
+ /** Re-scan imported skills (SessionStart `reloadSkills`). */
55
+ reloadSkills?(): void;
56
+ /** Environment imported settings declare for a session, set for every hook process. */
57
+ environment?(session?: {
58
+ readonly id: string;
59
+ readonly parentSession?: string;
60
+ }): Readonly<Record<string, string>>;
61
+ /** Imported Claude-syntax permission rules that gate a session's tool calls. */
62
+ permissionRules?(session?: {
63
+ readonly id: string;
64
+ readonly parentSession?: string;
65
+ }): {
66
+ readonly allow: readonly string[];
67
+ readonly deny: readonly string[];
68
+ readonly ask: readonly string[];
69
+ };
70
+ /** True while the settings in effect say `disableAllHooks: true`. */
71
+ hooksDisabled?(session?: {
72
+ readonly id: string;
73
+ readonly parentSession?: string;
74
+ }): boolean;
75
+ /** Load a new top-level session's project assets (its hooks among them) before SessionStart. */
76
+ prepareSession?(agent: unknown, cwd: string): Promise<void>;
77
+ /** A session ended: withdraw what `prepareSession` loaded. */
78
+ releaseSession?(sessionId: string): Promise<void>;
79
+ /**
80
+ * Mods' `tool.check` over the verdict Claude Code would reach for a call
81
+ * (PreToolUse hooks, permission rules, the mode); returns the decision.
82
+ */
83
+ check?(exec: unknown, verdict: ToolVerdict): Promise<ToolVerdict>;
84
+ /** Whether any mod hooks `tool.check` in the call's session. */
85
+ checks?(exec: unknown): boolean;
86
+ }
87
+ /** A permission verdict as Claude Code's `tool.check` carries it. */
88
+ export interface ToolVerdict {
89
+ readonly decision: 'allow' | 'ask' | 'deny';
90
+ readonly reason?: string;
91
+ readonly rule?: string;
92
+ readonly hook?: string;
93
+ }
94
+ /** Structural view of the DSH objects the broker touches. */
95
+ export interface AgentLike {
96
+ readonly id?: string;
97
+ readonly ctx?: unknown;
98
+ readonly options?: {
99
+ readonly provider?: string;
100
+ readonly model?: string;
101
+ readonly reasoningEffort?: string;
102
+ };
103
+ readonly status?: unknown;
104
+ readonly session: SessionLike;
105
+ inject(message: unknown): void;
106
+ steer(message: unknown): void;
107
+ followup(message: unknown): void;
108
+ cancel?(cause: {
109
+ kind: 'hook';
110
+ reason: string;
111
+ }): void;
112
+ }
113
+ export interface SessionLike {
114
+ readonly header: {
115
+ readonly id: string;
116
+ readonly cwd?: string;
117
+ readonly delegationDepth?: number;
118
+ readonly parentSession?: string;
119
+ };
120
+ readonly seq: number;
121
+ snapshotEvents(): ReadonlyArray<{
122
+ readonly type: string;
123
+ readonly time: number;
124
+ readonly data: unknown;
125
+ }>;
126
+ }
127
+ export interface ToolExecutionLike {
128
+ readonly name: string;
129
+ readonly arguments: unknown;
130
+ readonly callId?: string;
131
+ readonly agent?: AgentLike;
132
+ readonly signal: AbortSignal;
133
+ }
134
+ export interface ToolResultLike {
135
+ readonly isError: boolean;
136
+ readonly content: readonly ContentBlock[];
137
+ readonly value?: unknown;
138
+ readonly error?: {
139
+ readonly message?: string;
140
+ readonly info?: {
141
+ readonly code?: string;
142
+ };
143
+ };
144
+ }
145
+ export interface WorkingDirectoryLike {
146
+ ensure(agent: AgentLike, signal: AbortSignal): Promise<string>;
147
+ get(session: unknown): string | undefined;
148
+ }
149
+ /** One occurrence of a foreign event. */
150
+ export interface Occurrence {
151
+ readonly event: string;
152
+ /** The agent the event happens in. */
153
+ readonly agent?: AgentLike;
154
+ readonly signal: AbortSignal;
155
+ /** What a non-tool event matches on. */
156
+ readonly subject?: string;
157
+ readonly tool?: {
158
+ readonly dshName: string;
159
+ readonly args: unknown;
160
+ readonly callId?: string;
161
+ };
162
+ /** Event-specific payload fields. */
163
+ readonly fields?: Readonly<Record<string, unknown>>;
164
+ /** The command of a slash-command event, matched in both spellings. */
165
+ readonly subjects?: readonly string[];
166
+ readonly cwd?: string;
167
+ /** Fixed per-hook timeout default, when the event has its own. */
168
+ readonly defaultTimeoutMs?: number;
169
+ }
170
+ /** One handler's decoded outcome. */
171
+ export interface Ran {
172
+ readonly source: HookSource;
173
+ readonly handler: Exclude<HookHandler, {
174
+ type: 'invalid';
175
+ }>;
176
+ readonly decoded: Decoded;
177
+ readonly verdict?: ModelVerdict;
178
+ /** The block produced by `onFailure: "block"` or a suspect capture. */
179
+ readonly failureBlock?: string;
180
+ /** What the handler ran, for messages (`[cmd]`). */
181
+ readonly label: string;
182
+ /** The tool name the handler saw. */
183
+ readonly toolName?: string;
184
+ }
185
+ /** Per-session state. */
186
+ export interface SessionState {
187
+ lastTurn?: number;
188
+ promptId?: string;
189
+ stopActive: boolean;
190
+ stopContinuations: number;
191
+ /** Tool calls of the current step, for PostToolBatch. */
192
+ batch: Array<{
193
+ tool_name: string;
194
+ tool_input: unknown;
195
+ tool_use_id?: string;
196
+ tool_response: unknown;
197
+ }>;
198
+ /** Expanded command prompts already checked by UserPromptSubmit. */
199
+ expanded: Set<string>;
200
+ /** Last async SessionStart text per handler, to skip unchanged repeats. */
201
+ asyncSessionStart: Map<string, string>;
202
+ watchers: FSWatcher[];
203
+ watchPaths: string[];
204
+ }
205
+ export interface PreStepPayload {
206
+ readonly agent: AgentLike;
207
+ readonly messages: ReadonlyArray<{
208
+ readonly content: readonly ContentBlock[];
209
+ readonly source?: {
210
+ readonly kind?: string;
211
+ };
212
+ }>;
213
+ readonly turn: number;
214
+ readonly signal: AbortSignal;
215
+ }
216
+ export type PreStepDecision = {
217
+ readonly kind: string;
218
+ readonly messages?: readonly unknown[];
219
+ };
220
+ export type PostDecision = {
221
+ readonly kind: string;
222
+ readonly additionalContexts?: readonly unknown[];
223
+ readonly [key: string]: unknown;
224
+ };
225
+ /** DSH's question to the person (`user-questions/request`). */
226
+ export interface QuestionRequest {
227
+ readonly questions: ReadonlyArray<{
228
+ readonly id: string;
229
+ readonly question: string;
230
+ readonly header?: string;
231
+ readonly options?: ReadonlyArray<{
232
+ readonly label: string;
233
+ readonly description?: string;
234
+ }>;
235
+ readonly multiSelect?: boolean;
236
+ }>;
237
+ readonly agent?: AgentLike;
238
+ signal?: AbortSignal;
239
+ readonly wait?: {
240
+ readonly callId: string;
241
+ };
242
+ }
243
+ export interface QuestionAnswer {
244
+ readonly answers: ReadonlyArray<{
245
+ readonly id: string;
246
+ readonly selected: readonly string[];
247
+ readonly custom?: string;
248
+ }>;
249
+ }
250
+ export interface ApprovalRequest {
251
+ readonly agent: AgentLike;
252
+ readonly toolName: string;
253
+ readonly callId?: string;
254
+ readonly reason?: string;
255
+ readonly signal?: AbortSignal;
256
+ }
257
+ export interface ToolDefinitionLike {
258
+ execute(args: unknown, exec: unknown): Promise<unknown>;
259
+ readonly output: {
260
+ render(args: unknown, value: unknown): ContentBlock[];
261
+ };
262
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,80 @@
1
+ /**
2
+ * One hook broker for every imported plugin. Each foreign hooks configuration
3
+ * becomes a source; the broker listens once on DSH's interception points,
4
+ * presents each occurrence to the matching handlers in their own dialect
5
+ * (event names, matchers, tool names and shapes, payload fields, transcript),
6
+ * runs the handlers in parallel, and applies their combined outcome the way
7
+ * Claude Code or Codex does: blocking, rewriting input or output, adding
8
+ * context, continuing or stopping the turn, notifying the user.
9
+ *
10
+ * | Foreign event | DSH seam |
11
+ * | SessionStart / SubagentStart | `agent/created` of a root / delegated agent |
12
+ * | UserPromptSubmit, PostToolBatch, PreModelRequest | `agent/pre-step` |
13
+ * | UserPromptExpansion | an imported command's handler |
14
+ * | PreToolUse | `tools/pre-execute`; `updatedInput` runs through `tools/execute` |
15
+ * | PermissionRequest | `approval/request` |
16
+ * | PostToolUse / PostToolUseFailure | `tools/post-execute` |
17
+ * | Stop / SubagentStop | `agent/turn-stopping` of a root / delegated agent |
18
+ * | SessionEnd | `agent/disposed` |
19
+ * | PreCompact, PostCompact, StopFailure, Notification, CwdChanged, Interrupt, PostModelRequest | committed session events |
20
+ * | FileChanged | a file watcher |
21
+ * @module @kirkchinese/harness-forge
22
+ */
23
+ import type { Context } from '@deepseek-ai/cordis';
24
+ import type { AgentLike, HookBrokerHost, HookSource } from './broker/types.js';
25
+ export type { HookBrokerHost, HookObservation, HookSource, ToolVerdict } from './broker/types.js';
26
+ /** Coordinates every imported hooks configuration. */
27
+ export declare class HookBroker {
28
+ private readonly ctx;
29
+ private readonly engine;
30
+ private readonly events;
31
+ private readonly lifecycle;
32
+ private readonly tools;
33
+ constructor(ctx: Context, host: HookBrokerHost);
34
+ /** Register one source; the returned disposer removes it. */
35
+ add(source: HookSource): () => void;
36
+ /** Listen on every interception point. Returns the disposer for all of them. */
37
+ install(): () => void;
38
+ /**
39
+ * DSH is exiting: the sessions still open end now, and their SessionEnd hooks start (within DSH's shutdown grace,
40
+ * so first). Harness Forge being reloaded or removed while DSH goes on ends no session.
41
+ */
42
+ beginShutdown(): void;
43
+ /** Settle background hook runs (tests and shutdown). */
44
+ drain(): Promise<void>;
45
+ /**
46
+ * An imported slash command was typed: `UserPromptSubmit` sees the typed
47
+ * line, then `UserPromptExpansion` sees the command. Returns the block
48
+ * message, or the context to send with the expanded prompt.
49
+ */
50
+ expandCommand(agent: AgentLike, command: {
51
+ readonly name: string;
52
+ readonly foreignName: string;
53
+ readonly args: string;
54
+ readonly expanded: string;
55
+ }, signal: AbortSignal): Promise<{
56
+ blocked?: string;
57
+ context: string[];
58
+ }>;
59
+ /**
60
+ * The user typed `/skill args`: `UserPromptExpansion` sees the skill (the
61
+ * typed line already went through `UserPromptSubmit`). Returns the block
62
+ * message, or the reminders to send with the skill content.
63
+ */
64
+ expandSkill(agent: AgentLike, skill: {
65
+ readonly name: string;
66
+ readonly foreignName: string;
67
+ readonly args: string;
68
+ readonly source: string;
69
+ }, signal: AbortSignal): Promise<{
70
+ blocked?: string;
71
+ context: string[];
72
+ }>;
73
+ /**
74
+ * The DSH arguments a pending call runs with after a hook rewrote them.
75
+ * The tool's own `tools/execute` listener reads this.
76
+ */
77
+ rewriteOf(callId: string | undefined): Record<string, unknown> | undefined;
78
+ /** A mod rewrote a call's arguments (Claude vocabulary already mapped to DSH's): the hooks and the tool see them. */
79
+ rewrite(callId: string, args: Record<string, unknown>): void;
80
+ }
@@ -0,0 +1,95 @@
1
+ import { hostShuttingDown, onDsh } from '../dsh/host.js';
2
+ import { HookEngine } from './broker/engine.js';
3
+ import { SessionEvents } from './broker/session-events.js';
4
+ import { SessionLifecycle } from './broker/lifecycle.js';
5
+ import { ToolGate } from './broker/tools.js';
6
+ /** Coordinates every imported hooks configuration. */
7
+ export class HookBroker {
8
+ ctx;
9
+ engine;
10
+ events;
11
+ lifecycle;
12
+ tools;
13
+ constructor(ctx, host) {
14
+ this.ctx = ctx;
15
+ this.engine = new HookEngine(ctx, host);
16
+ this.events = new SessionEvents(this.engine);
17
+ this.lifecycle = new SessionLifecycle(this.engine, this.events);
18
+ this.tools = new ToolGate(this.engine);
19
+ }
20
+ /** Register one source; the returned disposer removes it. */
21
+ add(source) {
22
+ return this.engine.add(source);
23
+ }
24
+ /** Listen on every interception point. Returns the disposer for all of them. */
25
+ install() {
26
+ const ctx = { on: (name, listener, options) => onDsh(this.ctx, name, listener, { ...options, owner: 'hooks' }) };
27
+ const disposers = [
28
+ ctx.on('agent/created', ((payload) => this.lifecycle.agentCreated(payload))),
29
+ ctx.on('agent/pre-step', ((payload, next) => this.lifecycle.preStep(payload, next))),
30
+ ctx.on('tools/pre-execute', ((exec, next) => this.tools.preTool(exec, next))),
31
+ ctx.on('tools/execute', ((exec, next) => this.tools.executeTool(exec, next))),
32
+ ctx.on('tools/post-execute', ((exec, result, next) => this.tools.postTool(exec, result, next))),
33
+ ctx.on('agent/turn-stopping', ((payload) => this.lifecycle.turnStopping(payload))),
34
+ ctx.on('agent/disposed', ((payload) => this.lifecycle.agentDisposed(payload.agent))),
35
+ // Ahead of DSH's own answerer (the person), as PermissionRequest hooks and allow rules run before Claude Code shows its dialog.
36
+ ctx.on('approval/request', ((request, next) => this.tools.permission(request, next)), { prepend: true }),
37
+ // Claude Code's question dialog is a permission dialog too: PermissionRequest[AskUserQuestion] may answer or close it.
38
+ ctx.on('user-questions/request', ((request, next) => this.tools.question(request, next)), { prepend: true }),
39
+ ctx.on('session/event', ((session, event) => this.events.sessionEvent(session, event))),
40
+ ];
41
+ return async () => {
42
+ for (const dispose of disposers)
43
+ dispose();
44
+ this.beginShutdown();
45
+ // SessionEnd hooks of sessions closed during shutdown keep their own budget.
46
+ await Promise.allSettled([...this.engine.ending]);
47
+ this.engine.lifetime.abort();
48
+ for (const timer of [...this.engine.notifyTimers.values(), ...this.engine.idleTimers.values()])
49
+ clearTimeout(timer);
50
+ for (const state of this.engine.sessions.values())
51
+ for (const watcher of state.watchers)
52
+ watcher.close();
53
+ };
54
+ }
55
+ /**
56
+ * DSH is exiting: the sessions still open end now, and their SessionEnd hooks start (within DSH's shutdown grace,
57
+ * so first). Harness Forge being reloaded or removed while DSH goes on ends no session.
58
+ */
59
+ beginShutdown() {
60
+ if (hostShuttingDown(this.ctx))
61
+ this.lifecycle.endLive();
62
+ }
63
+ /** Settle background hook runs (tests and shutdown). */
64
+ async drain() {
65
+ while (this.engine.detached.size > 0)
66
+ await Promise.allSettled([...this.engine.detached]);
67
+ }
68
+ /**
69
+ * An imported slash command was typed: `UserPromptSubmit` sees the typed
70
+ * line, then `UserPromptExpansion` sees the command. Returns the block
71
+ * message, or the context to send with the expanded prompt.
72
+ */
73
+ expandCommand(agent, command, signal) {
74
+ return this.lifecycle.expandCommand(agent, command, signal);
75
+ }
76
+ /**
77
+ * The user typed `/skill args`: `UserPromptExpansion` sees the skill (the
78
+ * typed line already went through `UserPromptSubmit`). Returns the block
79
+ * message, or the reminders to send with the skill content.
80
+ */
81
+ expandSkill(agent, skill, signal) {
82
+ return this.lifecycle.expandSkill(agent, skill, signal);
83
+ }
84
+ /**
85
+ * The DSH arguments a pending call runs with after a hook rewrote them.
86
+ * The tool's own `tools/execute` listener reads this.
87
+ */
88
+ rewriteOf(callId) {
89
+ return this.tools.rewriteOf(callId);
90
+ }
91
+ /** A mod rewrote a call's arguments (Claude vocabulary already mapped to DSH's): the hooks and the tool see them. */
92
+ rewrite(callId, args) {
93
+ this.tools.rewrite(callId, args);
94
+ }
95
+ }
@@ -0,0 +1,5 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ /** Run Codex's `notify` program at every completed main-conversation turn. Returns the disposer. */
3
+ export declare function mountCodexNotify(ctx: Context, codexRoot: string, observe: (ok: boolean, detail: string) => void): () => void;
4
+ /** Add Codex's global instructions to the main conversation's system prompt. Returns the disposer. */
5
+ export declare function mountCodexInstructions(ctx: Context, codexRoot: string, file: string, observe: (ok: boolean, detail: string) => void): () => void;
@@ -0,0 +1,109 @@
1
+ /**
2
+ * The parts of a user's Codex configuration that act at run time:
3
+ *
4
+ * - `notify`: when a turn of a main conversation completes, Codex runs the
5
+ * configured program with one more argument, a JSON object
6
+ * `{ type: "agent-turn-complete", "thread-id", "turn-id", cwd,
7
+ * "input-messages", "last-assistant-message" }`; so does DSH.
8
+ * - the global instructions (`AGENTS.override.md`, else `AGENTS.md`), which
9
+ * Codex adds to every conversation, as a section of the main system prompt.
10
+ * @module @kirkchinese/harness-forge
11
+ */
12
+ import { spawn } from 'node:child_process';
13
+ import { readFileSync } from 'node:fs';
14
+ import { join } from 'node:path';
15
+ import { readCodexConfig } from '@kirkchinese/harness-forge-core';
16
+ import { dshService, onDsh } from '../dsh/host.js';
17
+ /** Run Codex's `notify` program at every completed main-conversation turn. Returns the disposer. */
18
+ export function mountCodexNotify(ctx, codexRoot, observe) {
19
+ const argv = (Array.isArray(readCodexConfig(codexRoot)['notify']) ? readCodexConfig(codexRoot)['notify'] : []).filter((item) => typeof item === 'string');
20
+ if (argv.length === 0)
21
+ return () => { };
22
+ const on = (name, listener) => onDsh(ctx, name, listener);
23
+ return on('session/event', ((session, event) => {
24
+ if (event.type !== 'turn/end' || event.data.reason === 'aborted')
25
+ return;
26
+ const agent = dshService(ctx, 'agents')?.get(session.id);
27
+ if (agent === undefined || (agent.session.header.delegationDepth ?? 0) > 0)
28
+ return;
29
+ const payload = {
30
+ type: 'agent-turn-complete',
31
+ 'thread-id': session.id,
32
+ 'turn-id': String(event.data.turn ?? ''),
33
+ cwd: agent.session.header.cwd ?? process.cwd(),
34
+ 'input-messages': turnInputs(agent, event.data.turn),
35
+ 'last-assistant-message': lastAnswer(agent),
36
+ };
37
+ const [command = '', ...args] = argv;
38
+ try {
39
+ const child = spawn(command, [...args, JSON.stringify(payload)], { detached: true, stdio: 'ignore', windowsHide: true });
40
+ child.on('error', (error) => { observe(false, `notify could not start: ${error.message}`); });
41
+ child.on('spawn', () => { observe(true, `notify ran after turn ${String(event.data.turn ?? '')}`); });
42
+ child.unref();
43
+ }
44
+ catch (error) {
45
+ observe(false, `notify could not start: ${error instanceof Error ? error.message : String(error)}`);
46
+ }
47
+ }));
48
+ }
49
+ /** The user's messages of one turn, as text. */
50
+ function turnInputs(agent, turn) {
51
+ const out = [];
52
+ let inTurn = false;
53
+ for (let seq = 0; seq < agent.session.seq; seq++) {
54
+ const event = agent.session.eventAt(seq);
55
+ if (event === undefined)
56
+ continue;
57
+ if (event.type === 'turn/start')
58
+ inTurn = event.data.turn === turn;
59
+ if (!inTurn || event.type !== 'user/message')
60
+ continue;
61
+ const message = event.data;
62
+ if (message.source?.kind !== 'user')
63
+ continue;
64
+ out.push((message.content ?? []).filter(block => block.type === 'text').map(block => block.text ?? '').join(''));
65
+ }
66
+ return out;
67
+ }
68
+ function lastAnswer(agent) {
69
+ for (let seq = agent.session.seq - 1; seq >= 0; seq--) {
70
+ const event = agent.session.eventAt(seq);
71
+ if (event?.type !== 'assistant/message')
72
+ continue;
73
+ const message = event.data.message;
74
+ const text = (message?.content ?? []).filter(block => block.type === 'text').map(block => block.text ?? '').join('');
75
+ if (text.length > 0)
76
+ return text;
77
+ }
78
+ return '';
79
+ }
80
+ /** Add Codex's global instructions to the main conversation's system prompt. Returns the disposer. */
81
+ export function mountCodexInstructions(ctx, codexRoot, file, observe) {
82
+ const prompt = dshService(ctx, 'systemPrompt');
83
+ if (prompt === undefined)
84
+ return () => { };
85
+ const path = join(codexRoot, file);
86
+ let reported = false;
87
+ return prompt.section({
88
+ name: 'harness-forge:codex-instructions',
89
+ order: prompt.getSectionOrder('DEPLOYMENT_PERSONA_SUFFIX') - 2,
90
+ interpolate: false,
91
+ text: ({ scope }) => {
92
+ const depth = scope?.session?.header?.delegationDepth ?? 0;
93
+ if (depth > 0)
94
+ return '';
95
+ let text;
96
+ try {
97
+ text = readFileSync(path, 'utf8').trim();
98
+ }
99
+ catch {
100
+ return '';
101
+ }
102
+ if (!reported) {
103
+ reported = true;
104
+ observe(true, `${file} is in the system prompt`);
105
+ }
106
+ return text.length === 0 ? '' : `# Instructions from ~/.codex/${file}\n\n${text}`;
107
+ },
108
+ });
109
+ }
@@ -0,0 +1,11 @@
1
+ import { type ForeignTool } from './vocabulary.js';
2
+ /**
3
+ * Whether `rule` selects this tool call.
4
+ * @param cwd - the session's working directory, for relative file rules.
5
+ */
6
+ export declare function conditionMatches(rule: string, tool: ForeignTool, cwd: string): boolean;
7
+ /**
8
+ * Whether a gitignore-style pattern, relative to the working directory,
9
+ * matches a path (skill `paths`, `Read(...)`/`Edit(...)` rules).
10
+ */
11
+ export declare function pathPatternMatches(pattern: string, path: string, cwd: string): boolean;