@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.
- package/LICENSE +21 -0
- package/README.md +42 -2
- package/assets/icon.svg +12 -0
- package/cordis.patch.yml +28 -0
- package/lib/agents/presets.d.ts +91 -0
- package/lib/agents/presets.js +282 -0
- package/lib/agents/profiles.d.ts +58 -0
- package/lib/agents/profiles.js +278 -0
- package/lib/agents/row.d.ts +17 -0
- package/lib/agents/row.js +146 -0
- package/lib/client/styles.d.ts +8 -0
- package/lib/client/styles.js +89 -0
- package/lib/client/typert.d.ts +14 -0
- package/lib/client/typert.js +110 -0
- package/lib/client.js +22500 -0
- package/lib/commands/definition.d.ts +40 -0
- package/lib/commands/definition.js +113 -0
- package/lib/commands/output-styles.d.ts +42 -0
- package/lib/commands/output-styles.js +152 -0
- package/lib/commands/row.d.ts +9 -0
- package/lib/commands/row.js +157 -0
- package/lib/context/instructions.d.ts +32 -0
- package/lib/context/instructions.js +88 -0
- package/lib/context/row.d.ts +16 -0
- package/lib/context/row.js +17 -0
- package/lib/dsh/commands.d.ts +30 -0
- package/lib/dsh/commands.js +61 -0
- package/lib/dsh/host.d.ts +107 -0
- package/lib/dsh/host.js +214 -0
- package/lib/dsh/model-route.d.ts +21 -0
- package/lib/dsh/model-route.js +37 -0
- package/lib/dsh/session-records.d.ts +78 -0
- package/lib/dsh/session-records.js +36 -0
- package/lib/dsh/subagents.d.ts +46 -0
- package/lib/dsh/subagents.js +14 -0
- package/lib/forge/contract.d.ts +231 -0
- package/lib/forge/contract.js +43 -0
- package/lib/forge/ledger.d.ts +143 -0
- package/lib/forge/ledger.js +733 -0
- package/lib/forge/live-mounts.d.ts +45 -0
- package/lib/forge/live-mounts.js +78 -0
- package/lib/forge/marketplace-install.d.ts +3 -0
- package/lib/forge/marketplace-install.js +64 -0
- package/lib/forge/npm.d.ts +31 -0
- package/lib/forge/npm.js +60 -0
- package/lib/forge/projects.d.ts +94 -0
- package/lib/forge/projects.js +336 -0
- package/lib/forge/receipts.d.ts +5 -0
- package/lib/forge/receipts.js +31 -0
- package/lib/forge/row.d.ts +32 -0
- package/lib/forge/row.js +59 -0
- package/lib/forge/service.d.ts +105 -0
- package/lib/forge/service.js +274 -0
- package/lib/forge/settings.d.ts +41 -0
- package/lib/forge/settings.js +115 -0
- package/lib/forge/state.d.ts +69 -0
- package/lib/forge/state.js +150 -0
- package/lib/forge/trust.d.ts +48 -0
- package/lib/forge/trust.js +107 -0
- package/lib/forge/turns.d.ts +13 -0
- package/lib/forge/turns.js +56 -0
- package/lib/hooks/broker/engine.d.ts +112 -0
- package/lib/hooks/broker/engine.js +728 -0
- package/lib/hooks/broker/helpers.d.ts +10 -0
- package/lib/hooks/broker/helpers.js +22 -0
- package/lib/hooks/broker/lifecycle.d.ts +65 -0
- package/lib/hooks/broker/lifecycle.js +342 -0
- package/lib/hooks/broker/session-events.d.ts +22 -0
- package/lib/hooks/broker/session-events.js +196 -0
- package/lib/hooks/broker/tools.d.ts +52 -0
- package/lib/hooks/broker/tools.js +424 -0
- package/lib/hooks/broker/types.d.ts +262 -0
- package/lib/hooks/broker/types.js +1 -0
- package/lib/hooks/broker.d.ts +80 -0
- package/lib/hooks/broker.js +95 -0
- package/lib/hooks/codex-user.d.ts +5 -0
- package/lib/hooks/codex-user.js +109 -0
- package/lib/hooks/condition.d.ts +11 -0
- package/lib/hooks/condition.js +181 -0
- package/lib/hooks/config.d.ts +85 -0
- package/lib/hooks/config.js +150 -0
- package/lib/hooks/matcher.d.ts +20 -0
- package/lib/hooks/matcher.js +39 -0
- package/lib/hooks/notice.d.ts +17 -0
- package/lib/hooks/notice.js +39 -0
- package/lib/hooks/output.d.ts +56 -0
- package/lib/hooks/output.js +214 -0
- package/lib/hooks/permissions.d.ts +14 -0
- package/lib/hooks/permissions.js +109 -0
- package/lib/hooks/process.d.ts +47 -0
- package/lib/hooks/process.js +134 -0
- package/lib/hooks/row.d.ts +17 -0
- package/lib/hooks/row.js +94 -0
- package/lib/hooks/runner.d.ts +85 -0
- package/lib/hooks/runner.js +293 -0
- package/lib/hooks/shell-commands.d.ts +19 -0
- package/lib/hooks/shell-commands.js +177 -0
- package/lib/hooks/shell.d.ts +43 -0
- package/lib/hooks/shell.js +126 -0
- package/lib/hooks/transcript.d.ts +28 -0
- package/lib/hooks/transcript.js +141 -0
- package/lib/hooks/vocabulary.d.ts +57 -0
- package/lib/hooks/vocabulary.js +364 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +287 -0
- package/lib/mcp/auth.d.ts +35 -0
- package/lib/mcp/auth.js +178 -0
- package/lib/mcp/catalog.d.ts +36 -0
- package/lib/mcp/catalog.js +61 -0
- package/lib/mcp/mount.d.ts +75 -0
- package/lib/mcp/mount.js +217 -0
- package/lib/mcp/remote.d.ts +45 -0
- package/lib/mcp/remote.js +143 -0
- package/lib/mcp/row.d.ts +9 -0
- package/lib/mcp/row.js +131 -0
- package/lib/mods/bundler.d.ts +38 -0
- package/lib/mods/bundler.js +157 -0
- package/lib/mods/chain.d.ts +103 -0
- package/lib/mods/chain.js +436 -0
- package/lib/mods/claude.d.ts +27 -0
- package/lib/mods/claude.js +56 -0
- package/lib/mods/engine.d.ts +160 -0
- package/lib/mods/engine.js +701 -0
- package/lib/mods/expect.d.ts +8 -0
- package/lib/mods/expect.js +248 -0
- package/lib/mods/host.d.ts +105 -0
- package/lib/mods/host.js +462 -0
- package/lib/mods/loader.d.ts +53 -0
- package/lib/mods/loader.js +227 -0
- package/lib/mods/ops.d.ts +13 -0
- package/lib/mods/ops.js +117 -0
- package/lib/mods/row.d.ts +9 -0
- package/lib/mods/row.js +77 -0
- package/lib/mods/runtime.d.ts +60 -0
- package/lib/mods/runtime.js +133 -0
- package/lib/mods/service/commands.d.ts +17 -0
- package/lib/mods/service/commands.js +121 -0
- package/lib/mods/service/drawings.d.ts +36 -0
- package/lib/mods/service/drawings.js +64 -0
- package/lib/mods/service/sessions.d.ts +63 -0
- package/lib/mods/service/sessions.js +345 -0
- package/lib/mods/service/tools.d.ts +32 -0
- package/lib/mods/service/tools.js +253 -0
- package/lib/mods/service/turns.d.ts +32 -0
- package/lib/mods/service/turns.js +161 -0
- package/lib/mods/service/types.d.ts +151 -0
- package/lib/mods/service/types.js +1 -0
- package/lib/mods/service.d.ts +72 -0
- package/lib/mods/service.js +104 -0
- package/lib/mods/surfaces.d.ts +93 -0
- package/lib/mods/surfaces.js +309 -0
- package/lib/mods/values.d.ts +30 -0
- package/lib/mods/values.js +95 -0
- package/lib/opencode/bun.d.ts +40 -0
- package/lib/opencode/bun.js +156 -0
- package/lib/opencode/convert.d.ts +53 -0
- package/lib/opencode/convert.js +255 -0
- package/lib/opencode/row.d.ts +9 -0
- package/lib/opencode/row.js +86 -0
- package/lib/opencode/runtime/core.d.ts +74 -0
- package/lib/opencode/runtime/core.js +249 -0
- package/lib/opencode/runtime/events.d.ts +80 -0
- package/lib/opencode/runtime/events.js +281 -0
- package/lib/opencode/runtime/mount.d.ts +27 -0
- package/lib/opencode/runtime/mount.js +283 -0
- package/lib/opencode/runtime/server.d.ts +27 -0
- package/lib/opencode/runtime/server.js +353 -0
- package/lib/opencode/runtime/types.d.ts +109 -0
- package/lib/opencode/runtime/types.js +1 -0
- package/lib/opencode/runtime.d.ts +55 -0
- package/lib/opencode/runtime.js +79 -0
- package/lib/porting/port-gap.d.ts +30 -0
- package/lib/porting/port-gap.js +122 -0
- package/lib/porting/port-sessions.d.ts +41 -0
- package/lib/porting/port-sessions.js +98 -0
- package/lib/porting/prompt.d.ts +23 -0
- package/lib/porting/prompt.js +49 -0
- package/lib/porting/row.d.ts +16 -0
- package/lib/porting/row.js +25 -0
- package/lib/porting/verify-port.d.ts +53 -0
- package/lib/porting/verify-port.js +104 -0
- package/lib/presets/fs-search.d.ts +25 -0
- package/lib/presets/fs-search.js +17 -0
- package/lib/presets/tool-filter.d.ts +35 -0
- package/lib/presets/tool-filter.js +91 -0
- package/lib/sessions/claude.d.ts +29 -0
- package/lib/sessions/claude.js +83 -0
- package/lib/sessions/codex.d.ts +30 -0
- package/lib/sessions/codex.js +303 -0
- package/lib/sessions/opencode.d.ts +32 -0
- package/lib/sessions/opencode.js +320 -0
- package/lib/sessions/row.d.ts +14 -0
- package/lib/sessions/row.js +35 -0
- package/lib/shared/invocation.d.ts +169 -0
- package/lib/shared/invocation.js +325 -0
- package/lib/shared/render.d.ts +72 -0
- package/lib/shared/render.js +588 -0
- package/lib/shared/util.d.ts +21 -0
- package/lib/shared/util.js +32 -0
- package/lib/skills/bundled.d.ts +3 -0
- package/lib/skills/bundled.js +42 -0
- package/lib/skills/provider.d.ts +72 -0
- package/lib/skills/provider.js +340 -0
- package/lib/skills/row.d.ts +22 -0
- package/lib/skills/row.js +204 -0
- package/lib/workbench/adapter-coverage.d.ts +47 -0
- package/lib/workbench/adapter-coverage.js +102 -0
- package/lib/workbench/model-tools.d.ts +10 -0
- package/lib/workbench/model-tools.js +122 -0
- package/lib/workbench/overview.d.ts +103 -0
- package/lib/workbench/overview.js +159 -0
- package/lib/workbench/remote.d.ts +118 -0
- package/lib/workbench/remote.js +422 -0
- package/lib/workbench/row.d.ts +15 -0
- package/lib/workbench/row.js +16 -0
- package/lib/workbench/settings.d.ts +15 -0
- package/lib/workbench/settings.js +56 -0
- package/lib/workbench/workbench.d.ts +100 -0
- package/lib/workbench/workbench.js +260 -0
- package/locale/agents/en.json +6 -0
- package/locale/agents/zh.json +6 -0
- package/locale/commands/en.json +6 -0
- package/locale/commands/zh.json +6 -0
- package/locale/context/en.json +6 -0
- package/locale/context/zh.json +6 -0
- package/locale/en.json +6 -0
- package/locale/hooks/en.json +6 -0
- package/locale/hooks/zh.json +6 -0
- package/locale/mcp/en.json +6 -0
- package/locale/mcp/zh.json +6 -0
- package/locale/mods/en.json +6 -0
- package/locale/mods/zh.json +6 -0
- package/locale/opencode/en.json +6 -0
- package/locale/opencode/zh.json +6 -0
- package/locale/porter/en.json +6 -0
- package/locale/porter/zh.json +6 -0
- package/locale/sessions/en.json +6 -0
- package/locale/sessions/zh.json +6 -0
- package/locale/skills/en.json +6 -0
- package/locale/skills/zh.json +6 -0
- package/locale/workbench/en.json +6 -0
- package/locale/workbench/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +246 -3
- package/runtime/opencode-host.mjs +270 -0
- package/skills/harness-forge/SKILL.md +47 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/** Universal JSON output fields and their types. */
|
|
2
|
+
const UNIVERSAL = {
|
|
3
|
+
continue: 'boolean',
|
|
4
|
+
stopReason: 'string',
|
|
5
|
+
suppressOutput: 'boolean',
|
|
6
|
+
systemMessage: 'string',
|
|
7
|
+
terminalSequence: 'string',
|
|
8
|
+
reason: 'string',
|
|
9
|
+
};
|
|
10
|
+
/** Events whose stdout Claude Code reads as plain text context. */
|
|
11
|
+
const PLAIN_CONTEXT = new Set(['UserPromptSubmit', 'UserPromptExpansion', 'SessionStart', 'PostModelSwitch', 'PreCompact']);
|
|
12
|
+
/** Decode a synchronous run. */
|
|
13
|
+
export function decodeRun(event, dialect, run) {
|
|
14
|
+
if (run.failure !== undefined) {
|
|
15
|
+
if (run.failure.kind === 'cancelled')
|
|
16
|
+
return { failed: false, cancelled: true };
|
|
17
|
+
return { error: run.failure.message, failed: true };
|
|
18
|
+
}
|
|
19
|
+
const classified = classify(run.stdout, false);
|
|
20
|
+
const checked = classified.kind === 'json' ? validate(event, dialect, classified.value) : classified;
|
|
21
|
+
const stderr = run.stderr.trim();
|
|
22
|
+
if (run.exitCode === 2) {
|
|
23
|
+
if (missingFileBlock(event, run))
|
|
24
|
+
return { error: `exit 2: ${firstLine(stderr)}`, failed: true };
|
|
25
|
+
const json = checked.kind === 'json' ? checked.value : undefined;
|
|
26
|
+
const reason = jsonReason(json) ?? (stderr.length > 0 ? stderr : `${event} hook exited 2`);
|
|
27
|
+
return { ...(json !== undefined ? { json } : {}), block: reason, failed: false };
|
|
28
|
+
}
|
|
29
|
+
if (checked.kind === 'invalid')
|
|
30
|
+
return { error: checked.message, failed: true };
|
|
31
|
+
if (run.exitCode === 0) {
|
|
32
|
+
if (checked.kind === 'json')
|
|
33
|
+
return { json: checked.value, failed: false };
|
|
34
|
+
if (dialect === 'codex' && (event === 'Stop' || event === 'SubagentStop') && checked.text.length > 0) {
|
|
35
|
+
return { error: `${event} hooks must print JSON; got plain text`, failed: true };
|
|
36
|
+
}
|
|
37
|
+
return { ...(checked.text.length > 0 ? { plain: checked.text } : {}), failed: false };
|
|
38
|
+
}
|
|
39
|
+
if (checked.kind === 'json')
|
|
40
|
+
return { json: checked.value, failed: true };
|
|
41
|
+
const detail = firstLine(stderr) || firstLine(run.stdout);
|
|
42
|
+
return { error: `Failed with non-blocking status code ${String(run.exitCode ?? 'none')}${detail.length > 0 ? `: ${detail}` : ''}`, failed: true };
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Decode a background (`async`) run: the JSON response may span stdout alone
|
|
46
|
+
* or sit on one line among other text; fields of the wrong type are dropped.
|
|
47
|
+
*/
|
|
48
|
+
export function decodeAsyncRun(event, dialect, run) {
|
|
49
|
+
if (run.failure !== undefined)
|
|
50
|
+
return run.failure.kind === 'cancelled' ? { failed: false, cancelled: true } : { error: run.failure.message, failed: true };
|
|
51
|
+
const classified = classify(run.stdout, true);
|
|
52
|
+
const json = classified.kind === 'json' ? dropInvalid(event, classified.value) : undefined;
|
|
53
|
+
const failed = run.exitCode !== 0 && run.exitCode !== 2;
|
|
54
|
+
return {
|
|
55
|
+
...(json !== undefined ? { json } : {}),
|
|
56
|
+
...(run.exitCode === 2 ? { block: run.stderr.trim() || run.stdout.trim() } : {}),
|
|
57
|
+
...(failed ? { error: `Failed with status code ${String(run.exitCode ?? 'none')}: ${firstLine(run.stderr)}` } : {}),
|
|
58
|
+
failed,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** Whether plain stdout of a successful run becomes context on this event. */
|
|
62
|
+
export function plainIsContext(event, dialect) {
|
|
63
|
+
if (dialect === 'codex')
|
|
64
|
+
return event === 'SessionStart' || event === 'UserPromptSubmit' || event === 'SubagentStart';
|
|
65
|
+
return PLAIN_CONTEXT.has(event);
|
|
66
|
+
}
|
|
67
|
+
/** The `hookSpecificOutput` object of a decoded run. */
|
|
68
|
+
export function specific(decoded) {
|
|
69
|
+
const value = decoded.json?.['hookSpecificOutput'];
|
|
70
|
+
return isRecord(value) ? value : undefined;
|
|
71
|
+
}
|
|
72
|
+
/** The context strings a run adds: `additionalContext`, plus plain stdout where the event reads it. */
|
|
73
|
+
export function contextsOf(event, dialect, decoded) {
|
|
74
|
+
const out = [];
|
|
75
|
+
const additional = specific(decoded)?.['additionalContext'];
|
|
76
|
+
if (typeof additional === 'string' && additional.length > 0)
|
|
77
|
+
out.push(additional);
|
|
78
|
+
if (decoded.plain !== undefined && plainIsContext(event, dialect) && event !== 'PreCompact')
|
|
79
|
+
out.push(decoded.plain);
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
function classify(stdout, async) {
|
|
83
|
+
const text = stdout.trim();
|
|
84
|
+
if (text.length === 0)
|
|
85
|
+
return { kind: 'plain', text: '' };
|
|
86
|
+
if (text.startsWith('{') && text.endsWith('}')) {
|
|
87
|
+
try {
|
|
88
|
+
const value = JSON.parse(text);
|
|
89
|
+
if (isRecord(value))
|
|
90
|
+
return { kind: 'json', value };
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// Several JSON lines, or a parse failure; see below.
|
|
94
|
+
}
|
|
95
|
+
const lines = text.split(/\r?\n/u).map(line => line.trim()).filter(line => line.length > 0);
|
|
96
|
+
const parsed = lines.map(tryParse);
|
|
97
|
+
if (lines.length > 1 && parsed.every(item => item.ok)) {
|
|
98
|
+
const objects = parsed.map(item => item.value).filter(isRecord);
|
|
99
|
+
if (async && objects.length >= 1)
|
|
100
|
+
return { kind: 'json', value: objects.at(-1) };
|
|
101
|
+
return objects.some(object => Object.keys(object).length > 0)
|
|
102
|
+
? { kind: 'invalid', message: 'stdout holds several JSON values; print one JSON object only' }
|
|
103
|
+
: { kind: 'plain', text };
|
|
104
|
+
}
|
|
105
|
+
if (!async)
|
|
106
|
+
return { kind: 'invalid', message: `invalid JSON output: ${parseError(text)}` };
|
|
107
|
+
}
|
|
108
|
+
if (async) {
|
|
109
|
+
const line = text.split(/\r?\n/u).map(item => item.trim()).filter(item => item.startsWith('{') && item.endsWith('}')).map(tryParse).find(item => item.ok && isRecord(item.value));
|
|
110
|
+
if (line !== undefined)
|
|
111
|
+
return { kind: 'json', value: line.value };
|
|
112
|
+
}
|
|
113
|
+
return { kind: 'plain', text };
|
|
114
|
+
}
|
|
115
|
+
function validate(event, dialect, value) {
|
|
116
|
+
for (const [field, type] of Object.entries(UNIVERSAL)) {
|
|
117
|
+
if (field in value && typeof value[field] !== type)
|
|
118
|
+
return { kind: 'invalid', message: `JSON output field "${field}" must be a ${type}` };
|
|
119
|
+
}
|
|
120
|
+
if ('decision' in value) {
|
|
121
|
+
const decision = value['decision'];
|
|
122
|
+
const allowed = event === 'PreToolUse' ? ['block', 'approve'] : ['block'];
|
|
123
|
+
if (typeof decision !== 'string' || !allowed.includes(decision))
|
|
124
|
+
return { kind: 'invalid', message: `JSON output field "decision" must be ${allowed.map(item => `"${item}"`).join(' or ')}` };
|
|
125
|
+
}
|
|
126
|
+
const hso = value['hookSpecificOutput'];
|
|
127
|
+
if (hso !== undefined) {
|
|
128
|
+
if (!isRecord(hso))
|
|
129
|
+
return { kind: 'invalid', message: 'hookSpecificOutput must be an object' };
|
|
130
|
+
if (hso['hookEventName'] !== event)
|
|
131
|
+
return { kind: 'invalid', message: `hookSpecificOutput.hookEventName must be "${event}"` };
|
|
132
|
+
if ('additionalContext' in hso && typeof hso['additionalContext'] !== 'string')
|
|
133
|
+
return { kind: 'invalid', message: 'additionalContext must be a string' };
|
|
134
|
+
if (event === 'PreToolUse' && 'permissionDecision' in hso && !['allow', 'deny', 'ask', 'defer'].includes(String(hso['permissionDecision']))) {
|
|
135
|
+
return { kind: 'invalid', message: 'permissionDecision must be "allow", "deny", "ask" or "defer"' };
|
|
136
|
+
}
|
|
137
|
+
if ('updatedInput' in hso && !isRecord(hso['updatedInput']))
|
|
138
|
+
return { kind: 'invalid', message: 'updatedInput must be an object' };
|
|
139
|
+
if (event === 'PermissionRequest' && 'decision' in hso) {
|
|
140
|
+
const decision = hso['decision'];
|
|
141
|
+
if (!isRecord(decision) || (decision['behavior'] !== 'allow' && decision['behavior'] !== 'deny')) {
|
|
142
|
+
return { kind: 'invalid', message: 'decision.behavior must be "allow" or "deny"' };
|
|
143
|
+
}
|
|
144
|
+
if (dialect === 'codex' && ('updatedInput' in decision || 'updatedPermissions' in decision || 'interrupt' in decision)) {
|
|
145
|
+
return { kind: 'invalid', message: 'Codex PermissionRequest hooks may not return updatedInput, updatedPermissions or interrupt' };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (dialect === 'codex' && event === 'PreToolUse') {
|
|
150
|
+
for (const field of ['continue', 'stopReason', 'suppressOutput']) {
|
|
151
|
+
if (field in value)
|
|
152
|
+
return { kind: 'invalid', message: `Codex PreToolUse hooks do not support "${field}"` };
|
|
153
|
+
}
|
|
154
|
+
if (isRecord(hso) && hso['permissionDecision'] === 'ask')
|
|
155
|
+
return { kind: 'invalid', message: 'Codex PreToolUse hooks do not support permissionDecision "ask"' };
|
|
156
|
+
if (value['decision'] === 'approve')
|
|
157
|
+
return { kind: 'invalid', message: 'Codex PreToolUse hooks do not support decision "approve"' };
|
|
158
|
+
}
|
|
159
|
+
return { kind: 'json', value };
|
|
160
|
+
}
|
|
161
|
+
/** Async output: keep the fields whose types are right. */
|
|
162
|
+
function dropInvalid(event, value) {
|
|
163
|
+
const out = {};
|
|
164
|
+
for (const [field, type] of Object.entries(UNIVERSAL)) {
|
|
165
|
+
if (typeof value[field] === type)
|
|
166
|
+
out[field] = value[field];
|
|
167
|
+
}
|
|
168
|
+
const hso = value['hookSpecificOutput'];
|
|
169
|
+
if (isRecord(hso) && hso['hookEventName'] === event && typeof hso['additionalContext'] === 'string') {
|
|
170
|
+
out['hookSpecificOutput'] = { hookEventName: event, additionalContext: hso['additionalContext'] };
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
/** The block reason a JSON output names for itself. */
|
|
175
|
+
function jsonReason(json) {
|
|
176
|
+
if (json === undefined)
|
|
177
|
+
return undefined;
|
|
178
|
+
const hso = isRecord(json['hookSpecificOutput']) ? json['hookSpecificOutput'] : {};
|
|
179
|
+
for (const value of [hso['permissionDecisionReason'], isRecord(hso['decision']) ? hso['decision']['message'] : undefined, json['reason']]) {
|
|
180
|
+
if (typeof value === 'string' && value.length > 0)
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
/** Stop, SubagentStop and a plugin's UserPromptSubmit: exit 2 caused by a missing file is a non-blocking error. */
|
|
186
|
+
function missingFileBlock(event, run) {
|
|
187
|
+
return (event === 'Stop' || event === 'SubagentStop' || event === 'TaskCompleted' || event === 'UserPromptSubmit')
|
|
188
|
+
&& run.stdout.trim().length === 0
|
|
189
|
+
&& /No such file or directory|cannot find the (path|file)/iu.test(run.stderr);
|
|
190
|
+
}
|
|
191
|
+
function tryParse(text) {
|
|
192
|
+
try {
|
|
193
|
+
return { ok: true, value: JSON.parse(text) };
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return { ok: false };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function parseError(text) {
|
|
200
|
+
try {
|
|
201
|
+
JSON.parse(text);
|
|
202
|
+
return 'not an object';
|
|
203
|
+
}
|
|
204
|
+
catch (error) {
|
|
205
|
+
return error instanceof Error ? error.message : String(error);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/** The first non-empty line, trimmed and bounded. */
|
|
209
|
+
export function firstLine(text) {
|
|
210
|
+
return text.split(/\r?\n/u).find(line => line.trim().length > 0)?.trim().slice(0, 500) ?? '';
|
|
211
|
+
}
|
|
212
|
+
function isRecord(value) {
|
|
213
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
214
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ForeignTool } from './vocabulary.js';
|
|
2
|
+
/** Rules in Claude Code's syntax. */
|
|
3
|
+
export interface PermissionRuleSet {
|
|
4
|
+
readonly allow: readonly string[];
|
|
5
|
+
readonly deny: readonly string[];
|
|
6
|
+
readonly ask: readonly string[];
|
|
7
|
+
}
|
|
8
|
+
/** The decision rules make for one call; undefined leaves it to the host. */
|
|
9
|
+
export type RuleVerdict = {
|
|
10
|
+
readonly kind: 'deny' | 'ask' | 'allow';
|
|
11
|
+
readonly rule: string;
|
|
12
|
+
} | undefined;
|
|
13
|
+
/** Evaluate rules against one call. */
|
|
14
|
+
export declare function ruleVerdict(rules: PermissionRuleSet, tool: ForeignTool, cwd: string): RuleVerdict;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude Code permission rules (`permissions.allow` / `ask` / `deny` in
|
|
3
|
+
* settings) evaluated against one tool call, as Claude Code evaluates them:
|
|
4
|
+
* deny, then ask, then allow; the first match decides and rule specificity
|
|
5
|
+
* does not change the order.
|
|
6
|
+
*
|
|
7
|
+
* - A shell call matches a deny or ask rule when any subcommand matches; it
|
|
8
|
+
* matches allow only when every subcommand matches an allow rule.
|
|
9
|
+
* - Deny and ask rules may name a top-level input parameter,
|
|
10
|
+
* `Tool(param:value)` with `*` wildcards; an omitted parameter never matches.
|
|
11
|
+
* A tool's primary content field cannot be matched this way.
|
|
12
|
+
* - Allow rules accept a tool-name glob only after a literal `mcp__<server>__`.
|
|
13
|
+
* @module @kirkchinese/harness-forge
|
|
14
|
+
*/
|
|
15
|
+
import { conditionMatches } from './condition.js';
|
|
16
|
+
import { shellCommands } from './shell-commands.js';
|
|
17
|
+
import { isShellTool } from './vocabulary.js';
|
|
18
|
+
const PRIMARY_FIELDS = new Set(['command', 'file_path', 'path', 'notebook_path', 'url']);
|
|
19
|
+
/** Evaluate rules against one call. */
|
|
20
|
+
export function ruleVerdict(rules, tool, cwd) {
|
|
21
|
+
const deny = rules.deny.find(rule => restrictiveMatch(rule, tool, cwd));
|
|
22
|
+
if (deny !== undefined)
|
|
23
|
+
return { kind: 'deny', rule: deny };
|
|
24
|
+
const ask = rules.ask.find(rule => restrictiveMatch(rule, tool, cwd));
|
|
25
|
+
if (ask !== undefined)
|
|
26
|
+
return { kind: 'ask', rule: ask };
|
|
27
|
+
const allow = allowMatch(rules.allow, tool, cwd);
|
|
28
|
+
return allow === undefined ? undefined : { kind: 'allow', rule: allow };
|
|
29
|
+
}
|
|
30
|
+
function split(rule) {
|
|
31
|
+
const match = /^\s*([A-Za-z0-9_\-:.*]+?)\s*(?:\((.*)\))?\s*$/su.exec(rule);
|
|
32
|
+
return match === null ? undefined : { name: match[1] ?? '', specifier: match[2] };
|
|
33
|
+
}
|
|
34
|
+
/** A deny or ask rule: tool-name globs, parameter rules, then the `if`-rule match. */
|
|
35
|
+
function restrictiveMatch(rule, tool, cwd) {
|
|
36
|
+
const parts = split(rule);
|
|
37
|
+
if (parts === undefined)
|
|
38
|
+
return false;
|
|
39
|
+
const { name, specifier } = parts;
|
|
40
|
+
if (name.includes('*') && !name.startsWith('mcp__')) {
|
|
41
|
+
if (!globName(name).test(tool.name) && !tool.aliases.some(alias => globName(name).test(alias)))
|
|
42
|
+
return false;
|
|
43
|
+
return specifier === undefined || specifier.trim() === '' || specifier.trim() === '*';
|
|
44
|
+
}
|
|
45
|
+
if (name.startsWith('mcp__') && name.includes('*')) {
|
|
46
|
+
// `mcp__*`, `mcp__server__*`, `mcp__server__get_*`.
|
|
47
|
+
return specifier === undefined && globName(name).test(tool.name);
|
|
48
|
+
}
|
|
49
|
+
const parameter = specifier === undefined ? undefined : /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(.*?)\s*$/su.exec(specifier);
|
|
50
|
+
if (parameter !== null && parameter !== undefined && !specifier.trim().endsWith(':*')) {
|
|
51
|
+
const key = parameter[1] ?? '';
|
|
52
|
+
if (Object.hasOwn(tool.input, key) || !isShellTool(tool.dshName)) {
|
|
53
|
+
if (PRIMARY_FIELDS.has(key))
|
|
54
|
+
return false;
|
|
55
|
+
if (!conditionMatches(name, tool, cwd))
|
|
56
|
+
return false;
|
|
57
|
+
const value = tool.input[key];
|
|
58
|
+
if (value === undefined || (typeof value === 'object' && value !== null))
|
|
59
|
+
return false;
|
|
60
|
+
return globName(parameter[2] ?? '').test(String(value));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return conditionMatches(rule, tool, cwd);
|
|
64
|
+
}
|
|
65
|
+
/** The allow rule that approves the call, if any. */
|
|
66
|
+
function allowMatch(rules, tool, cwd) {
|
|
67
|
+
const usable = rules.filter((rule) => {
|
|
68
|
+
const parts = split(rule);
|
|
69
|
+
if (parts === undefined)
|
|
70
|
+
return false;
|
|
71
|
+
// An unanchored tool-name glob never auto-approves.
|
|
72
|
+
if (parts.name.includes('*'))
|
|
73
|
+
return /^mcp__[^*_][^*]*?__/u.test(parts.name);
|
|
74
|
+
return true;
|
|
75
|
+
});
|
|
76
|
+
if (usable.length === 0)
|
|
77
|
+
return undefined;
|
|
78
|
+
if (isShellTool(tool.dshName)) {
|
|
79
|
+
const whole = usable.find((rule) => {
|
|
80
|
+
const parts = split(rule);
|
|
81
|
+
return (parts.specifier === undefined || parts.specifier.trim() === '*') && conditionMatches(parts.name, tool, cwd);
|
|
82
|
+
});
|
|
83
|
+
if (whole !== undefined)
|
|
84
|
+
return whole;
|
|
85
|
+
const parsed = shellCommands(String(tool.input['command'] ?? ''));
|
|
86
|
+
if (parsed === undefined || parsed.dynamic || parsed.commands.length === 0)
|
|
87
|
+
return undefined;
|
|
88
|
+
let first;
|
|
89
|
+
for (const command of parsed.commands) {
|
|
90
|
+
const single = { ...tool, input: { ...tool.input, command } };
|
|
91
|
+
const rule = usable.find(candidate => conditionMatches(candidate, single, cwd));
|
|
92
|
+
if (rule === undefined)
|
|
93
|
+
return undefined;
|
|
94
|
+
first ??= rule;
|
|
95
|
+
}
|
|
96
|
+
return first;
|
|
97
|
+
}
|
|
98
|
+
return usable.find((rule) => {
|
|
99
|
+
const parts = split(rule);
|
|
100
|
+
if (parts.name.startsWith('mcp__')) {
|
|
101
|
+
// An MCP allow rule with a specifier matches no call.
|
|
102
|
+
return parts.specifier === undefined && (parts.name.includes('*') ? globName(parts.name).test(tool.name) : conditionMatches(rule, tool, cwd));
|
|
103
|
+
}
|
|
104
|
+
return conditionMatches(rule, tool, cwd);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function globName(pattern) {
|
|
108
|
+
return new RegExp(`^${pattern.split('*').map(part => part.replace(/[.+?^${}()|[\]\\]/gu, '\\$&')).join('.*')}$`, 'su');
|
|
109
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run one hook process the way Claude Code does: JSON on stdin, output read
|
|
3
|
+
* until shortly after the process itself exits (a background child that
|
|
4
|
+
* keeps the pipes open does not hold the hook), an optional timeout, and
|
|
5
|
+
* cancellation through a signal. A timeout or cancellation ends the hook's
|
|
6
|
+
* whole process tree, not only the process that was started.
|
|
7
|
+
* @module @kirkchinese/harness-forge
|
|
8
|
+
*/
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
/** One process to run. */
|
|
11
|
+
export interface ProcessSpec {
|
|
12
|
+
readonly file: string;
|
|
13
|
+
readonly args: readonly string[];
|
|
14
|
+
readonly cwd: string;
|
|
15
|
+
readonly env: NodeJS.ProcessEnv;
|
|
16
|
+
readonly stdin: string;
|
|
17
|
+
/** Undefined runs without a timeout. */
|
|
18
|
+
readonly timeoutMs?: number;
|
|
19
|
+
readonly signal?: AbortSignal;
|
|
20
|
+
}
|
|
21
|
+
/** What one run produced. */
|
|
22
|
+
export interface ProcessOutcome {
|
|
23
|
+
/** Exit code; undefined when the process could not start, timed out or was cancelled. */
|
|
24
|
+
readonly exitCode?: number;
|
|
25
|
+
/** Why the process could not start. */
|
|
26
|
+
readonly startError?: string;
|
|
27
|
+
readonly timedOut: boolean;
|
|
28
|
+
readonly cancelled: boolean;
|
|
29
|
+
readonly stdout: string;
|
|
30
|
+
readonly stderr: string;
|
|
31
|
+
/** The process exited while something it started still held its output open. */
|
|
32
|
+
readonly suspectCapture: boolean;
|
|
33
|
+
readonly durationMs: number;
|
|
34
|
+
}
|
|
35
|
+
/** The command that ends a process and everything it started on Windows (`taskkill /T /F`). */
|
|
36
|
+
export declare function treeKillCommand(pid: number, platform?: NodeJS.Platform): {
|
|
37
|
+
file: string;
|
|
38
|
+
args: string[];
|
|
39
|
+
} | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* End a spawned process and its descendants. On POSIX the process leads its own
|
|
42
|
+
* group (it was spawned `detached`), so the group is killed; on Windows
|
|
43
|
+
* `taskkill /T /F` walks the tree.
|
|
44
|
+
*/
|
|
45
|
+
export declare function killProcessTree(child: ReturnType<typeof spawn>, platform?: NodeJS.Platform): void;
|
|
46
|
+
/** Spawn without a shell and collect the outcome. Never rejects. */
|
|
47
|
+
export declare function runProcess(spec: ProcessSpec): Promise<ProcessOutcome>;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run one hook process the way Claude Code does: JSON on stdin, output read
|
|
3
|
+
* until shortly after the process itself exits (a background child that
|
|
4
|
+
* keeps the pipes open does not hold the hook), an optional timeout, and
|
|
5
|
+
* cancellation through a signal. A timeout or cancellation ends the hook's
|
|
6
|
+
* whole process tree, not only the process that was started.
|
|
7
|
+
* @module @kirkchinese/harness-forge
|
|
8
|
+
*/
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
import { messageOf } from '../shared/util.js';
|
|
11
|
+
const OUTPUT_LIMIT = 8 * 1024 * 1024;
|
|
12
|
+
/** How long output is still read after the process exits. */
|
|
13
|
+
const DRAIN_MS = 250;
|
|
14
|
+
/** The command that ends a process and everything it started on Windows (`taskkill /T /F`). */
|
|
15
|
+
export function treeKillCommand(pid, platform = process.platform) {
|
|
16
|
+
return platform === 'win32' ? { file: 'taskkill', args: ['/T', '/F', '/PID', String(pid)] } : undefined;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* End a spawned process and its descendants. On POSIX the process leads its own
|
|
20
|
+
* group (it was spawned `detached`), so the group is killed; on Windows
|
|
21
|
+
* `taskkill /T /F` walks the tree.
|
|
22
|
+
*/
|
|
23
|
+
export function killProcessTree(child, platform = process.platform) {
|
|
24
|
+
const pid = child.pid;
|
|
25
|
+
if (pid === undefined) {
|
|
26
|
+
child.kill('SIGKILL');
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const windows = treeKillCommand(pid, platform);
|
|
30
|
+
if (windows !== undefined) {
|
|
31
|
+
try {
|
|
32
|
+
const killer = spawn(windows.file, windows.args, { stdio: 'ignore', windowsHide: true });
|
|
33
|
+
killer.on('error', () => { child.kill('SIGKILL'); });
|
|
34
|
+
killer.unref();
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
child.kill('SIGKILL');
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
process.kill(-pid, 'SIGKILL');
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
child.kill('SIGKILL');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Spawn without a shell and collect the outcome. Never rejects. */
|
|
49
|
+
export function runProcess(spec) {
|
|
50
|
+
const started = performance.now();
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
let stdout = '';
|
|
53
|
+
let stderr = '';
|
|
54
|
+
let timedOut = false;
|
|
55
|
+
let cancelled = false;
|
|
56
|
+
let settled = false;
|
|
57
|
+
let exitCode;
|
|
58
|
+
let stdoutOpen = true;
|
|
59
|
+
let stderrOpen = true;
|
|
60
|
+
let drainTimer;
|
|
61
|
+
let child;
|
|
62
|
+
try {
|
|
63
|
+
// Detached on POSIX: the hook leads its own process group, which a timeout or cancellation kills whole.
|
|
64
|
+
child = spawn(spec.file, [...spec.args], { cwd: spec.cwd, env: spec.env, windowsHide: true, stdio: 'pipe', detached: process.platform !== 'win32' });
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
resolve({ startError: messageOf(error), timedOut: false, cancelled: false, stdout: '', stderr: '', suspectCapture: false, durationMs: 0 });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const finish = (suspectCapture, startError) => {
|
|
71
|
+
if (settled)
|
|
72
|
+
return;
|
|
73
|
+
settled = true;
|
|
74
|
+
clearTimeout(timer);
|
|
75
|
+
clearTimeout(drainTimer);
|
|
76
|
+
spec.signal?.removeEventListener('abort', onAbort);
|
|
77
|
+
if (suspectCapture || timedOut || cancelled) {
|
|
78
|
+
child.stdout?.destroy();
|
|
79
|
+
child.stderr?.destroy();
|
|
80
|
+
child.stdin?.destroy();
|
|
81
|
+
}
|
|
82
|
+
resolve({
|
|
83
|
+
...(exitCode !== undefined && !timedOut && !cancelled ? { exitCode } : {}),
|
|
84
|
+
...(startError !== undefined ? { startError } : {}),
|
|
85
|
+
timedOut,
|
|
86
|
+
cancelled,
|
|
87
|
+
stdout,
|
|
88
|
+
stderr,
|
|
89
|
+
suspectCapture,
|
|
90
|
+
durationMs: Math.round(performance.now() - started),
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
const timer = spec.timeoutMs === undefined
|
|
94
|
+
? undefined
|
|
95
|
+
: setTimeout(() => {
|
|
96
|
+
timedOut = true;
|
|
97
|
+
killProcessTree(child);
|
|
98
|
+
finish(false);
|
|
99
|
+
}, spec.timeoutMs);
|
|
100
|
+
const onAbort = () => {
|
|
101
|
+
cancelled = true;
|
|
102
|
+
killProcessTree(child);
|
|
103
|
+
finish(false);
|
|
104
|
+
};
|
|
105
|
+
spec.signal?.addEventListener('abort', onAbort, { once: true });
|
|
106
|
+
if (spec.signal?.aborted === true)
|
|
107
|
+
onAbort();
|
|
108
|
+
const closed = () => {
|
|
109
|
+
if (exitCode !== undefined && !stdoutOpen && !stderrOpen)
|
|
110
|
+
finish(false);
|
|
111
|
+
};
|
|
112
|
+
child.stdout?.setEncoding('utf8').on('data', (chunk) => { if (stdout.length < OUTPUT_LIMIT)
|
|
113
|
+
stdout += chunk; });
|
|
114
|
+
child.stderr?.setEncoding('utf8').on('data', (chunk) => { if (stderr.length < OUTPUT_LIMIT)
|
|
115
|
+
stderr += chunk; });
|
|
116
|
+
child.stdout?.on('close', () => { stdoutOpen = false; closed(); });
|
|
117
|
+
child.stderr?.on('close', () => { stderrOpen = false; closed(); });
|
|
118
|
+
child.on('error', (error) => { finish(false, messageOf(error)); });
|
|
119
|
+
child.on('exit', (code, signal) => {
|
|
120
|
+
// The process has finished: a timeout that fires while its output drains is not the hook's.
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
exitCode = code ?? (signal !== null ? 128 : undefined);
|
|
123
|
+
if (!stdoutOpen && !stderrOpen) {
|
|
124
|
+
finish(false);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
drainTimer = setTimeout(() => { finish(stdoutOpen || stderrOpen); }, DRAIN_MS);
|
|
128
|
+
});
|
|
129
|
+
child.stdin?.on('error', () => {
|
|
130
|
+
// A hook that exits without reading stdin closes the pipe early.
|
|
131
|
+
});
|
|
132
|
+
child.stdin?.end(spec.stdin);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hooks row: imported hooks configurations (plugin hooks, settings hooks,
|
|
3
|
+
* skill, agent and command frontmatter hooks) run through one broker on DSH's
|
|
4
|
+
* interception points, and the run-time parts of a user's Codex configuration
|
|
5
|
+
* (`notify`, global instructions). Mods and OpenCode plugins add to it
|
|
6
|
+
* (`tool.check`, `shell.env`) through its API; it asks nothing of them.
|
|
7
|
+
* @module @kirkchinese/harness-forge/hooks
|
|
8
|
+
*/
|
|
9
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
10
|
+
import type { Disposer } from '../forge/contract.js';
|
|
11
|
+
import { type HarnessForge } from '../forge/service.js';
|
|
12
|
+
export declare const name = "harness-forge-hooks";
|
|
13
|
+
export declare const inject: string[];
|
|
14
|
+
/** The hooks row. */
|
|
15
|
+
export declare function apply(ctx: Context): void;
|
|
16
|
+
/** Start the hooks row on the shared service. Returns its teardown. */
|
|
17
|
+
export declare function startHooks(ctx: Context, forge: HarnessForge): Disposer;
|
package/lib/hooks/row.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { HarnessError } from '@kirkchinese/harness-forge-core';
|
|
2
|
+
import { mountCodexInstructions, mountCodexNotify } from './codex-user.js';
|
|
3
|
+
import { onDsh } from '../dsh/host.js';
|
|
4
|
+
import { FORGE_SERVICE } from '../forge/service.js';
|
|
5
|
+
import { HookBroker } from './broker.js';
|
|
6
|
+
import { hookSource, parseHookTable, readHookTable } from './config.js';
|
|
7
|
+
import { mounted, Parts, rowEffect } from '../forge/row.js';
|
|
8
|
+
export const name = 'harness-forge-hooks';
|
|
9
|
+
export const inject = [FORGE_SERVICE];
|
|
10
|
+
/** The hooks row. */
|
|
11
|
+
export function apply(ctx) {
|
|
12
|
+
rowEffect(ctx, 'hooks', forge => startHooks(ctx, forge));
|
|
13
|
+
}
|
|
14
|
+
/** Start the hooks row on the shared service. Returns its teardown. */
|
|
15
|
+
export function startHooks(ctx, forge) {
|
|
16
|
+
let checker;
|
|
17
|
+
const shellEnvs = new Set();
|
|
18
|
+
const broker = new HookBroker(ctx, {
|
|
19
|
+
shell: forge.shell,
|
|
20
|
+
dataRoot: forge.dataRoot,
|
|
21
|
+
observe: ({ componentId, event, ok, detail }) => { forge.observe(componentId, `hook:${event}`, ok, detail); },
|
|
22
|
+
agentName: presetId => forge.lookup('agents')?.foreignName(presetId),
|
|
23
|
+
presetOf: foreignName => forge.lookup('agents')?.presetOf(foreignName),
|
|
24
|
+
skillHooks: skill => forge.lookup('skills')?.skillHooks(skill),
|
|
25
|
+
agentHooks: presetId => forge.lookup('agents')?.agentHooks(presetId),
|
|
26
|
+
reloadSkills: () => { forge.lookup('skills')?.reload(); },
|
|
27
|
+
environment: session => forge.settings.env(session),
|
|
28
|
+
permissionRules: session => forge.settings.permissionRules(session),
|
|
29
|
+
hooksDisabled: session => forge.settings.hooksDisabled(session),
|
|
30
|
+
prepareSession: (agent, cwd) => forge.projects.prepare(agent, cwd),
|
|
31
|
+
releaseSession: sessionId => forge.projects.release(sessionId),
|
|
32
|
+
check: (exec, verdict) => checker === undefined ? Promise.resolve(verdict) : checker.check(exec, verdict),
|
|
33
|
+
checks: exec => checker?.checks(exec) ?? false,
|
|
34
|
+
shellEnv: async (exec) => {
|
|
35
|
+
const env = {};
|
|
36
|
+
for (const provider of shellEnvs)
|
|
37
|
+
Object.assign(env, await provider(exec));
|
|
38
|
+
return env;
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const api = {
|
|
42
|
+
rewrite: (callId, args) => { broker.rewrite(callId, args); },
|
|
43
|
+
expandSkill: (agent, skill, signal) => broker.expandSkill(agent, skill, signal),
|
|
44
|
+
expandCommand: (agent, command, signal) => broker.expandCommand(agent, command, signal),
|
|
45
|
+
add: source => broker.add(source),
|
|
46
|
+
setChecker: (next) => {
|
|
47
|
+
checker = next;
|
|
48
|
+
return () => { if (checker === next)
|
|
49
|
+
checker = undefined; };
|
|
50
|
+
},
|
|
51
|
+
addShellEnv: (provider) => {
|
|
52
|
+
shellEnvs.add(provider);
|
|
53
|
+
return () => { shellEnvs.delete(provider); };
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
const parts = new Parts(ctx, forge, 'hooks');
|
|
57
|
+
parts.add('broker', () => broker.install());
|
|
58
|
+
// A skill's frontmatter hooks run for the rest of the session once the skill is invoked.
|
|
59
|
+
parts.add('skill hooks', () => onDsh(ctx, 'tools/post-execute', ((exec, result, next) => {
|
|
60
|
+
if (exec.name === 'skill' && !result.isError && exec.agent !== undefined) {
|
|
61
|
+
const skill = exec.arguments?.name;
|
|
62
|
+
const source = typeof skill === 'string' ? forge.lookup('skills')?.skillHooks(skill) : undefined;
|
|
63
|
+
if (source !== undefined)
|
|
64
|
+
broker.add({ ...source, origin: 'skill', sessionId: exec.agent.session.header.id });
|
|
65
|
+
}
|
|
66
|
+
return next();
|
|
67
|
+
}), { owner: 'hooks' }));
|
|
68
|
+
parts.add('hooks mounter', () => forge.registerMounter('hooks', {
|
|
69
|
+
mount: async ({ component, plugin, dataDir, scope }) => {
|
|
70
|
+
if (component.kind !== 'hooks')
|
|
71
|
+
throw new HarnessError('component-failed', `action expects hooks: ${component.id}`);
|
|
72
|
+
const table = component.configPath.length > 0 ? readHookTable(component.configPath) : parseHookTable(component.inline ?? {});
|
|
73
|
+
const origin = component.metadata?.['origin'] === 'settings' ? 'settings' : 'plugin';
|
|
74
|
+
const dispose = broker.add({ ...hookSource(component.id, plugin, dataDir, table, origin), ...(scope.sessionId !== undefined ? { sessionId: scope.sessionId } : {}) });
|
|
75
|
+
return { outcome: mounted(component, 'L1b', [...table.keys()].join(',')), disposer: async () => { dispose(); } };
|
|
76
|
+
},
|
|
77
|
+
}));
|
|
78
|
+
parts.add('codex-notify mounter', () => forge.registerMounter('codex-notify', {
|
|
79
|
+
mount: async ({ component, plugin }) => {
|
|
80
|
+
const dispose = mountCodexNotify(ctx, plugin.root, (ok, detail) => { forge.observe(component.id, 'notify', ok, detail); });
|
|
81
|
+
return { outcome: mounted(component, 'L1b', 'runs after each completed turn'), disposer: async () => { dispose(); } };
|
|
82
|
+
},
|
|
83
|
+
}));
|
|
84
|
+
parts.add('codex-instructions mounter', () => forge.registerMounter('codex-instructions', {
|
|
85
|
+
mount: async ({ component, plugin }) => {
|
|
86
|
+
if (component.path === undefined)
|
|
87
|
+
throw new HarnessError('component-failed', `instructions without a file: ${component.id}`);
|
|
88
|
+
const dispose = mountCodexInstructions(ctx, plugin.root, component.path, (ok, detail) => { forge.observe(component.id, 'instructions', ok, detail); });
|
|
89
|
+
return { outcome: mounted(component, 'L1a', `${component.path} in the system prompt`), disposer: async () => { dispose(); } };
|
|
90
|
+
},
|
|
91
|
+
}));
|
|
92
|
+
parts.add('api', () => forge.provide('hooks', api));
|
|
93
|
+
return () => parts.dispose();
|
|
94
|
+
}
|