@kirkchinese/harness-forge 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +46 -2
- package/assets/icon.svg +12 -0
- package/cordis.patch.yml +28 -0
- package/docs/DETAIL.zh.md +47 -0
- package/lib/agents/presets.d.ts +98 -0
- package/lib/agents/presets.js +292 -0
- package/lib/agents/profiles.d.ts +58 -0
- package/lib/agents/profiles.js +279 -0
- package/lib/agents/row.d.ts +17 -0
- package/lib/agents/row.js +148 -0
- package/lib/client/paths.d.ts +12 -0
- package/lib/client/paths.js +68 -0
- package/lib/client/styles.d.ts +8 -0
- package/lib/client/styles.js +89 -0
- package/lib/client/typert.d.ts +25 -0
- package/lib/client/typert.js +158 -0
- package/lib/client.js +23823 -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 +110 -0
- package/lib/dsh/host.js +217 -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 +271 -0
- package/lib/forge/contract.js +45 -0
- package/lib/forge/follow.d.ts +30 -0
- package/lib/forge/follow.js +73 -0
- package/lib/forge/ledger.d.ts +206 -0
- package/lib/forge/ledger.js +1005 -0
- package/lib/forge/live-mounts.d.ts +45 -0
- package/lib/forge/live-mounts.js +78 -0
- package/lib/forge/npm.d.ts +31 -0
- package/lib/forge/npm.js +60 -0
- package/lib/forge/projects.d.ts +97 -0
- package/lib/forge/projects.js +343 -0
- package/lib/forge/receipts.d.ts +5 -0
- package/lib/forge/receipts.js +31 -0
- package/lib/forge/remote/archive.d.ts +65 -0
- package/lib/forge/remote/archive.js +565 -0
- package/lib/forge/remote/code-parts.d.ts +18 -0
- package/lib/forge/remote/code-parts.js +76 -0
- package/lib/forge/remote/fetch.d.ts +265 -0
- package/lib/forge/remote/fetch.js +1133 -0
- package/lib/forge/remote/service.d.ts +256 -0
- package/lib/forge/remote/service.js +868 -0
- package/lib/forge/row.d.ts +32 -0
- package/lib/forge/row.js +59 -0
- package/lib/forge/service.d.ts +115 -0
- package/lib/forge/service.js +290 -0
- package/lib/forge/settings.d.ts +43 -0
- package/lib/forge/settings.js +122 -0
- package/lib/forge/state.d.ts +129 -0
- package/lib/forge/state.js +266 -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 +722 -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 +27 -0
- package/lib/hooks/broker/session-events.js +205 -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 +264 -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 +87 -0
- package/lib/hooks/condition.js +602 -0
- package/lib/hooks/config.d.ts +85 -0
- package/lib/hooks/config.js +154 -0
- package/lib/hooks/glob.d.ts +41 -0
- package/lib/hooks/glob.js +114 -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 +19 -0
- package/lib/hooks/permissions.js +202 -0
- package/lib/hooks/powershell-aliases.d.ts +19 -0
- package/lib/hooks/powershell-aliases.js +58 -0
- package/lib/hooks/process.d.ts +64 -0
- package/lib/hooks/process.js +289 -0
- package/lib/hooks/row.d.ts +17 -0
- package/lib/hooks/row.js +96 -0
- package/lib/hooks/runner.d.ts +98 -0
- package/lib/hooks/runner.js +322 -0
- package/lib/hooks/shell-commands.d.ts +28 -0
- package/lib/hooks/shell-commands.js +241 -0
- package/lib/hooks/shell.d.ts +44 -0
- package/lib/hooks/shell.js +129 -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 +366 -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 +39 -0
- package/lib/mcp/catalog.js +69 -0
- package/lib/mcp/mount.d.ts +113 -0
- package/lib/mcp/mount.js +317 -0
- package/lib/mcp/policy.d.ts +42 -0
- package/lib/mcp/policy.js +43 -0
- package/lib/mcp/remote.d.ts +45 -0
- package/lib/mcp/remote.js +133 -0
- package/lib/mcp/row.d.ts +9 -0
- package/lib/mcp/row.js +225 -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 +231 -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 +33 -0
- package/lib/mods/service/turns.js +136 -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 +168 -0
- package/lib/opencode/convert.d.ts +53 -0
- package/lib/opencode/convert.js +255 -0
- package/lib/opencode/row.d.ts +22 -0
- package/lib/opencode/row.js +104 -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 +354 -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/adapter-paths.d.ts +13 -0
- package/lib/porting/adapter-paths.js +120 -0
- package/lib/porting/port-gap.d.ts +36 -0
- package/lib/porting/port-gap.js +158 -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 +51 -0
- package/lib/porting/row.d.ts +16 -0
- package/lib/porting/row.js +27 -0
- package/lib/porting/verify-port.d.ts +46 -0
- package/lib/porting/verify-port.js +82 -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/chat-import.d.ts +48 -0
- package/lib/sessions/chat-import.js +44 -0
- package/lib/sessions/claude.d.ts +35 -0
- package/lib/sessions/claude.js +104 -0
- package/lib/sessions/codex-discover.d.ts +34 -0
- package/lib/sessions/codex-discover.js +205 -0
- package/lib/sessions/codex-meta.d.ts +63 -0
- package/lib/sessions/codex-meta.js +148 -0
- package/lib/sessions/codex-text.d.ts +9 -0
- package/lib/sessions/codex-text.js +44 -0
- package/lib/sessions/codex.d.ts +42 -0
- package/lib/sessions/codex.js +270 -0
- package/lib/sessions/opencode.d.ts +38 -0
- package/lib/sessions/opencode.js +339 -0
- package/lib/sessions/persist.d.ts +162 -0
- package/lib/sessions/persist.js +377 -0
- package/lib/sessions/row.d.ts +15 -0
- package/lib/sessions/row.js +101 -0
- package/lib/sessions/turns.d.ts +31 -0
- package/lib/sessions/turns.js +63 -0
- package/lib/sessions/zstd.d.ts +15 -0
- package/lib/sessions/zstd.js +152 -0
- package/lib/shared/invocation.d.ts +169 -0
- package/lib/shared/invocation.js +325 -0
- package/lib/shared/render.d.ts +101 -0
- package/lib/shared/render.js +1353 -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 +74 -0
- package/lib/skills/provider.js +346 -0
- package/lib/skills/row.d.ts +22 -0
- package/lib/skills/row.js +204 -0
- package/lib/workbench/adapter-coverage.d.ts +65 -0
- package/lib/workbench/adapter-coverage.js +141 -0
- package/lib/workbench/model-tools.d.ts +10 -0
- package/lib/workbench/model-tools.js +122 -0
- package/lib/workbench/overview.d.ts +153 -0
- package/lib/workbench/overview.js +246 -0
- package/lib/workbench/remote.d.ts +161 -0
- package/lib/workbench/remote.js +561 -0
- package/lib/workbench/row.d.ts +15 -0
- package/lib/workbench/row.js +16 -0
- package/lib/workbench/settings.d.ts +19 -0
- package/lib/workbench/settings.js +60 -0
- package/lib/workbench/workbench.d.ts +130 -0
- package/lib/workbench/workbench.js +361 -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 +7 -0
- package/package.json +247 -3
- package/runtime/opencode-host.mjs +270 -0
- package/skills/harness-forge/SKILL.md +47 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { ruleVerdict } from '../permissions.js';
|
|
2
|
+
import { specific } from '../output.js';
|
|
3
|
+
import { dshInput, dshValue, foreignTool, isShellTool, shellFailure } from '../vocabulary.js';
|
|
4
|
+
import { dshService } from '../../dsh/host.js';
|
|
5
|
+
import { isRecord, textOf } from './helpers.js';
|
|
6
|
+
import { messageOf } from '../../shared/util.js';
|
|
7
|
+
/**
|
|
8
|
+
* Tool calls and permission requests: PreToolUse with permission rules and
|
|
9
|
+
* mods' `tool.check`, rewritten input through `tools/execute`,
|
|
10
|
+
* PostToolUse / PostToolUseFailure, and PermissionRequest for approvals and
|
|
11
|
+
* questions.
|
|
12
|
+
*/
|
|
13
|
+
export class ToolGate {
|
|
14
|
+
engine;
|
|
15
|
+
/** Tool arguments by call id, for PermissionRequest and PostToolUse. */
|
|
16
|
+
calls = new Map();
|
|
17
|
+
/** Rewritten DSH arguments by call id. */
|
|
18
|
+
rewrites = new Map();
|
|
19
|
+
constructor(engine) {
|
|
20
|
+
this.engine = engine;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The DSH arguments a pending call runs with after a hook rewrote them.
|
|
24
|
+
* The tool's own `tools/execute` listener reads this.
|
|
25
|
+
*/
|
|
26
|
+
rewriteOf(callId) {
|
|
27
|
+
return callId === undefined ? undefined : this.rewrites.get(callId);
|
|
28
|
+
}
|
|
29
|
+
/** A mod rewrote a call's arguments (Claude vocabulary already mapped to DSH's): the hooks and the tool see them. */
|
|
30
|
+
rewrite(callId, args) {
|
|
31
|
+
this.rewrites.set(callId, args);
|
|
32
|
+
}
|
|
33
|
+
// -------------------------------------------------------------------------
|
|
34
|
+
// Tools
|
|
35
|
+
// -------------------------------------------------------------------------
|
|
36
|
+
async preTool(exec, next) {
|
|
37
|
+
const callId = exec.callId;
|
|
38
|
+
if (callId !== undefined)
|
|
39
|
+
this.calls.set(callId, { args: exec.arguments, started: Date.now(), contexts: [], ...(exec.agent !== undefined ? { agent: exec.agent } : {}) });
|
|
40
|
+
if (exec.agent !== undefined)
|
|
41
|
+
this.engine.state(exec.agent.session).stopContinuations = 0;
|
|
42
|
+
const rules = this.engine.host.permissionRules?.(exec.agent?.session.header);
|
|
43
|
+
const hasRules = rules !== undefined && rules.allow.length + rules.deny.length + rules.ask.length > 0;
|
|
44
|
+
const checked = this.engine.host.checks?.(exec) === true;
|
|
45
|
+
if (!this.engine.has('PreToolUse') && !hasRules && !checked)
|
|
46
|
+
return next();
|
|
47
|
+
if (checked)
|
|
48
|
+
return this.checkedDecision(exec, await this.decide(exec, rules, hasRules, next));
|
|
49
|
+
return this.decide(exec, rules, hasRules, next);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The decision Claude Code reaches, as a verdict mods' `tool.check` hooks
|
|
53
|
+
* may answer over; mapped back to DSH's pre-execute decision.
|
|
54
|
+
*/
|
|
55
|
+
async checkedDecision(exec, decided) {
|
|
56
|
+
const kind = isRecord(decided) ? decided['kind'] : undefined;
|
|
57
|
+
const reason = isRecord(decided) && typeof decided['reason'] === 'string' ? decided['reason'] : undefined;
|
|
58
|
+
const why = exec.callId === undefined ? undefined : this.behindDecision.get(exec.callId);
|
|
59
|
+
if (exec.callId !== undefined)
|
|
60
|
+
this.behindDecision.delete(exec.callId);
|
|
61
|
+
const verdict = {
|
|
62
|
+
decision: kind === 'deny' || kind === 'cancel' ? 'deny' : kind === 'allow' ? 'allow' : 'ask',
|
|
63
|
+
...(reason !== undefined ? { reason } : {}),
|
|
64
|
+
...(why?.rule !== undefined ? { rule: why.rule } : {}),
|
|
65
|
+
...(why?.hook !== undefined ? { hook: why.hook } : {}),
|
|
66
|
+
};
|
|
67
|
+
const answer = await this.engine.host.check(exec, verdict);
|
|
68
|
+
if (answer.decision === verdict.decision && answer.reason === verdict.reason)
|
|
69
|
+
return decided;
|
|
70
|
+
if (answer.decision === 'deny') {
|
|
71
|
+
this.markDenied(exec.callId);
|
|
72
|
+
return { kind: 'deny', reason: answer.reason ?? `Permission to use ${exec.name} has been denied.` };
|
|
73
|
+
}
|
|
74
|
+
return answer.decision === 'allow' ? { kind: 'allow' } : { kind: 'ask', ...(answer.reason !== undefined ? { reason: answer.reason } : {}) };
|
|
75
|
+
}
|
|
76
|
+
async decide(exec, rules, hasRules, next) {
|
|
77
|
+
const callId = exec.callId;
|
|
78
|
+
if (!this.engine.has('PreToolUse') && !hasRules)
|
|
79
|
+
return next();
|
|
80
|
+
const results = this.engine.has('PreToolUse')
|
|
81
|
+
? await this.engine.run({
|
|
82
|
+
event: 'PreToolUse',
|
|
83
|
+
...(exec.agent !== undefined ? { agent: exec.agent } : {}),
|
|
84
|
+
signal: exec.signal,
|
|
85
|
+
tool: { dshName: exec.name, args: (callId !== undefined ? this.rewrites.get(callId) : undefined) ?? exec.arguments, ...(callId !== undefined ? { callId } : {}) },
|
|
86
|
+
fields: callId !== undefined ? { tool_use_id: callId } : {},
|
|
87
|
+
})
|
|
88
|
+
: [];
|
|
89
|
+
const subject = results[0]?.toolName ?? exec.name;
|
|
90
|
+
if (exec.agent !== undefined)
|
|
91
|
+
this.engine.noticeAll(exec.agent, 'PreToolUse', subject, results);
|
|
92
|
+
const stop = this.engine.stopOf(results);
|
|
93
|
+
const contexts = this.engine.contexts('PreToolUse', results);
|
|
94
|
+
if (stop !== undefined) {
|
|
95
|
+
this.markDenied(callId);
|
|
96
|
+
if (exec.agent !== undefined)
|
|
97
|
+
this.engine.halt(exec.agent, 'PreToolUse', stop, results);
|
|
98
|
+
return { kind: 'deny', reason: stop };
|
|
99
|
+
}
|
|
100
|
+
// deny > defer > ask > allow.
|
|
101
|
+
const decisions = results.map(ran => ({ ran, decision: this.permissionDecision(ran) }));
|
|
102
|
+
const deny = decisions.find(item => item.decision?.kind === 'deny');
|
|
103
|
+
if (deny !== undefined) {
|
|
104
|
+
this.markDenied(callId);
|
|
105
|
+
const reason = deny.decision?.reason ?? 'blocked by a PreToolUse hook';
|
|
106
|
+
if (deny.decision?.endTurn === true && exec.agent !== undefined)
|
|
107
|
+
this.engine.halt(exec.agent, 'PreToolUse', reason, results);
|
|
108
|
+
if (contexts.length > 0 && exec.agent !== undefined)
|
|
109
|
+
for (const text of contexts)
|
|
110
|
+
exec.agent.inject(this.engine.contextMessage(exec.agent, 'PreToolUse', text));
|
|
111
|
+
this.behind(callId, { hook: 'PreToolUse' });
|
|
112
|
+
return { kind: 'deny', reason };
|
|
113
|
+
}
|
|
114
|
+
// `defer` only applies to non-interactive runs; DSH sessions are interactive, as in Claude Code's terminal.
|
|
115
|
+
const rewritten = [...decisions].reverse().find(item => item.decision?.updatedInput !== undefined);
|
|
116
|
+
if (rewritten !== undefined && callId !== undefined) {
|
|
117
|
+
try {
|
|
118
|
+
const tool = rewritten.decision.updatedInput;
|
|
119
|
+
this.rewrites.set(callId, dshInput(rewritten.ran.source.dialect, exec.name, exec.arguments, tool, this.engine.toolContext()));
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (exec.agent !== undefined)
|
|
123
|
+
this.engine.notice(exec.agent, { kind: 'hook-error', event: 'PreToolUse', subject, plugin: rewritten.ran.source.pluginId, text: `updatedInput ignored: ${messageOf(error)}` });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// Permission rules hold whatever a hook returned, and see the input the hooks rewrote.
|
|
127
|
+
const verdict = hasRules && rules !== undefined ? await this.ruleVerdictOf(exec, (callId !== undefined ? this.rewrites.get(callId) : undefined) ?? exec.arguments, rules) : undefined;
|
|
128
|
+
if (verdict?.kind === 'deny') {
|
|
129
|
+
this.markDenied(callId);
|
|
130
|
+
this.behind(callId, { rule: verdict.rule });
|
|
131
|
+
return { kind: 'deny', reason: `Permission to use ${verdict.tool} has been denied by the rule ${verdict.rule}.` };
|
|
132
|
+
}
|
|
133
|
+
if (callId !== undefined)
|
|
134
|
+
this.calls.get(callId)?.contexts.push(...contexts);
|
|
135
|
+
const ask = decisions.find(item => item.decision?.kind === 'ask');
|
|
136
|
+
if (ask !== undefined) {
|
|
137
|
+
this.behind(callId, { hook: 'PreToolUse' });
|
|
138
|
+
return { kind: 'ask', ...(ask.decision?.reason !== undefined ? { reason: ask.decision.reason } : {}) };
|
|
139
|
+
}
|
|
140
|
+
if (verdict?.kind === 'ask') {
|
|
141
|
+
this.behind(callId, { rule: verdict.rule });
|
|
142
|
+
return { kind: 'ask', reason: `${verdict.tool} matches the ask rule ${verdict.rule}` };
|
|
143
|
+
}
|
|
144
|
+
// `allow` skips the permission prompt.
|
|
145
|
+
if (verdict?.kind === 'allow')
|
|
146
|
+
this.behind(callId, { rule: verdict.rule });
|
|
147
|
+
else if (decisions.some(item => item.decision?.kind === 'allow'))
|
|
148
|
+
this.behind(callId, { hook: 'PreToolUse' });
|
|
149
|
+
if (decisions.some(item => item.decision?.kind === 'allow') || verdict?.kind === 'allow') {
|
|
150
|
+
if (callId !== undefined)
|
|
151
|
+
this.allowedCalls.add(callId);
|
|
152
|
+
return { kind: 'allow' };
|
|
153
|
+
}
|
|
154
|
+
return next();
|
|
155
|
+
}
|
|
156
|
+
/** Calls an allow rule or hook let through: DSH asks nobody about them (a sandbox escalation included). */
|
|
157
|
+
allowedCalls = new Set();
|
|
158
|
+
/** What stands behind a call's decision (a rule, a PreToolUse hook), for tool.check. */
|
|
159
|
+
behindDecision = new Map();
|
|
160
|
+
behind(callId, why) {
|
|
161
|
+
if (callId !== undefined && this.engine.host.checks !== undefined)
|
|
162
|
+
this.behindDecision.set(callId, why);
|
|
163
|
+
}
|
|
164
|
+
async ruleVerdictOf(exec, args, rules) {
|
|
165
|
+
const cwd = exec.agent !== undefined ? await this.engine.directory(exec.agent, exec.signal) : process.cwd();
|
|
166
|
+
const tool = foreignTool('claude-code', exec.name, args, this.engine.toolContext(exec.agent, cwd));
|
|
167
|
+
const verdict = ruleVerdict(rules, tool, cwd);
|
|
168
|
+
return verdict === undefined ? undefined : { ...verdict, tool: tool.name };
|
|
169
|
+
}
|
|
170
|
+
async executeTool(exec, next) {
|
|
171
|
+
const rewrite = exec.callId === undefined ? undefined : this.rewrites.get(exec.callId);
|
|
172
|
+
const extra = isShellTool(exec.name) && this.engine.host.shellEnv !== undefined ? await this.engine.host.shellEnv(exec).catch(() => ({})) : {};
|
|
173
|
+
const envPrefix = isShellTool(exec.name) && exec.agent !== undefined ? this.engine.shellEnvPrefix(exec.agent, exec.name, extra) : undefined;
|
|
174
|
+
if (rewrite === undefined && envPrefix === undefined)
|
|
175
|
+
return next();
|
|
176
|
+
const args = { ...(rewrite ?? exec.arguments) };
|
|
177
|
+
if (envPrefix !== undefined && typeof args['command'] === 'string')
|
|
178
|
+
args['command'] = `${envPrefix}${args['command']}`;
|
|
179
|
+
const tools = dshService(this.engine.ctx, 'tools');
|
|
180
|
+
const definition = tools?.get(exec.name, exec.agent);
|
|
181
|
+
if (definition === undefined)
|
|
182
|
+
return next();
|
|
183
|
+
try {
|
|
184
|
+
const value = await definition.execute(args, exec);
|
|
185
|
+
return { isError: false, value, content: definition.output.render(args, value) };
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
const message = messageOf(error);
|
|
189
|
+
return { isError: true, error: { message }, content: [{ type: 'text', text: `Error: ${message}` }] };
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async postTool(exec, result, next) {
|
|
193
|
+
const callId = exec.callId;
|
|
194
|
+
const call = callId === undefined ? undefined : this.calls.get(callId);
|
|
195
|
+
const args = (callId !== undefined ? this.rewrites.get(callId) : undefined) ?? exec.arguments;
|
|
196
|
+
if (callId !== undefined) {
|
|
197
|
+
this.calls.delete(callId);
|
|
198
|
+
this.rewrites.delete(callId);
|
|
199
|
+
}
|
|
200
|
+
const pending = call?.contexts ?? [];
|
|
201
|
+
const text = textOf(result.content);
|
|
202
|
+
// A shell command that exits non-zero failed, as in Claude Code.
|
|
203
|
+
const failure = result.isError ? result.error?.message ?? text : shellFailure(exec.name, result.value);
|
|
204
|
+
const event = failure !== undefined ? 'PostToolUseFailure' : 'PostToolUse';
|
|
205
|
+
const agent = exec.agent;
|
|
206
|
+
if (agent !== undefined && this.engine.has('PostToolBatch')) {
|
|
207
|
+
const tool = foreignTool('claude-code', exec.name, args, this.engine.toolContext(agent));
|
|
208
|
+
this.engine.state(agent.session).batch.push({ tool_name: tool.name, tool_input: tool.input, ...(callId !== undefined ? { tool_use_id: callId } : {}), tool_response: text });
|
|
209
|
+
}
|
|
210
|
+
if (call?.deniedByHook === true || (!this.engine.has(event) && pending.length === 0))
|
|
211
|
+
return next();
|
|
212
|
+
const results = this.engine.has(event)
|
|
213
|
+
? await this.engine.run({
|
|
214
|
+
event,
|
|
215
|
+
...(agent !== undefined ? { agent } : {}),
|
|
216
|
+
signal: exec.signal,
|
|
217
|
+
tool: { dshName: exec.name, args, ...(callId !== undefined ? { callId } : {}) },
|
|
218
|
+
fields: {
|
|
219
|
+
...(callId !== undefined ? { tool_use_id: callId } : {}),
|
|
220
|
+
...(call !== undefined ? { duration_ms: Date.now() - call.started } : {}),
|
|
221
|
+
...(failure !== undefined
|
|
222
|
+
? { error: failure, is_interrupt: exec.signal.aborted }
|
|
223
|
+
: {}),
|
|
224
|
+
},
|
|
225
|
+
// The structured response depends on the dialect; see `payload`.
|
|
226
|
+
...(failure !== undefined ? {} : { response: { value: result.value, text } }),
|
|
227
|
+
})
|
|
228
|
+
: [];
|
|
229
|
+
const subject = results[0]?.toolName ?? exec.name;
|
|
230
|
+
if (agent !== undefined)
|
|
231
|
+
this.engine.noticeAll(agent, event, subject, results);
|
|
232
|
+
const contexts = [...pending, ...this.engine.contexts(event, results)];
|
|
233
|
+
const blocks = [];
|
|
234
|
+
for (const ran of results) {
|
|
235
|
+
if (ran.decoded.block !== undefined)
|
|
236
|
+
blocks.push(ran.decoded.block);
|
|
237
|
+
else if (ran.decoded.json?.['decision'] === 'block')
|
|
238
|
+
blocks.push(String(ran.decoded.json['reason'] ?? `blocked by a ${event} hook`));
|
|
239
|
+
if (ran.verdict !== undefined && !ran.verdict.ok) {
|
|
240
|
+
const endTurn = event === 'PostToolUse' && ran.handler.type === 'prompt' && ran.handler.continueOnBlock !== true;
|
|
241
|
+
if (endTurn && agent !== undefined)
|
|
242
|
+
this.engine.halt(agent, event, ran.verdict.reason ?? `${event} hook condition not met`, results);
|
|
243
|
+
else
|
|
244
|
+
blocks.push(ran.verdict.reason ?? `${event} hook condition not met`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const stop = this.engine.stopOf(results);
|
|
248
|
+
if (stop !== undefined && agent !== undefined)
|
|
249
|
+
this.engine.halt(agent, event, stop, results);
|
|
250
|
+
const codex = results.some(ran => ran.source.dialect === 'codex');
|
|
251
|
+
const reminders = [
|
|
252
|
+
...contexts.map(item => this.engine.contextMessage(agent, event, item)),
|
|
253
|
+
...(codex ? [] : blocks.map(reason => this.engine.contextMessage(agent, event, `${event} hook blocking error: ${reason}`))),
|
|
254
|
+
].filter(item => item !== undefined);
|
|
255
|
+
if (codex && blocks.length > 0) {
|
|
256
|
+
return { kind: 'block', feedback: [{ type: 'text', text: blocks.join('\n') }], ...(reminders.length > 0 ? { additionalContexts: reminders } : {}) };
|
|
257
|
+
}
|
|
258
|
+
const replacement = this.outputReplacement(exec.name, result, results);
|
|
259
|
+
const downstream = await next();
|
|
260
|
+
if (downstream.kind !== 'accept')
|
|
261
|
+
return downstream;
|
|
262
|
+
return {
|
|
263
|
+
...downstream,
|
|
264
|
+
...replacement,
|
|
265
|
+
...(reminders.length > 0 ? { additionalContexts: [...reminders, ...downstream.additionalContexts ?? []] } : {}),
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/** `updatedToolOutput` / `updatedMCPToolOutput`: a DSH value when the shape maps, else the model-facing text. */
|
|
269
|
+
outputReplacement(dshName, result, results) {
|
|
270
|
+
for (const ran of [...results].reverse()) {
|
|
271
|
+
const hso = specific(ran.decoded);
|
|
272
|
+
const updated = hso?.['updatedToolOutput'] ?? (dshName.startsWith('mcp__') ? hso?.['updatedMCPToolOutput'] : undefined);
|
|
273
|
+
if (updated === undefined)
|
|
274
|
+
continue;
|
|
275
|
+
const value = dshValue(dshName, result.value, updated);
|
|
276
|
+
if (value !== undefined)
|
|
277
|
+
return { value };
|
|
278
|
+
return { content: [{ type: 'text', text: typeof updated === 'string' ? updated : JSON.stringify(updated) }] };
|
|
279
|
+
}
|
|
280
|
+
return {};
|
|
281
|
+
}
|
|
282
|
+
async permission(request, next) {
|
|
283
|
+
// An allowed call skips the permission prompt, whatever DSH would ask about it.
|
|
284
|
+
if (request.callId !== undefined && this.allowedCalls.has(request.callId))
|
|
285
|
+
return 'allowed-once';
|
|
286
|
+
if (!this.engine.has('PermissionRequest'))
|
|
287
|
+
return next();
|
|
288
|
+
const call = request.callId === undefined ? undefined : this.calls.get(request.callId);
|
|
289
|
+
const results = await this.engine.run({
|
|
290
|
+
event: 'PermissionRequest',
|
|
291
|
+
agent: request.agent,
|
|
292
|
+
signal: request.signal ?? this.engine.lifetime.signal,
|
|
293
|
+
tool: { dshName: request.toolName, args: call?.args ?? {}, ...(request.callId !== undefined ? { callId: request.callId } : {}) },
|
|
294
|
+
});
|
|
295
|
+
const subject = results[0]?.toolName ?? request.toolName;
|
|
296
|
+
this.engine.noticeAll(request.agent, 'PermissionRequest', subject, results);
|
|
297
|
+
const stop = this.engine.stopOf(results);
|
|
298
|
+
if (stop !== undefined) {
|
|
299
|
+
this.engine.halt(request.agent, 'PermissionRequest', stop, results);
|
|
300
|
+
return 'rejected';
|
|
301
|
+
}
|
|
302
|
+
let allow;
|
|
303
|
+
for (const ran of results) {
|
|
304
|
+
// Exit 2 is not honored on PermissionRequest; only the decision object and onFailure decide.
|
|
305
|
+
if (ran.failureBlock !== undefined)
|
|
306
|
+
return this.permissionDenied(request, ran.failureBlock, false, results);
|
|
307
|
+
const decision = specific(ran.decoded)?.['decision'];
|
|
308
|
+
if (typeof decision !== 'object' || decision === null)
|
|
309
|
+
continue;
|
|
310
|
+
const value = decision;
|
|
311
|
+
if (value['behavior'] === 'deny')
|
|
312
|
+
return this.permissionDenied(request, typeof value['message'] === 'string' ? value['message'] : 'denied by a PermissionRequest hook', value['interrupt'] === true, results);
|
|
313
|
+
if (value['behavior'] === 'allow')
|
|
314
|
+
allow = { ran, ...(isRecord(value['updatedInput']) ? { updatedInput: value['updatedInput'] } : {}) };
|
|
315
|
+
}
|
|
316
|
+
if (allow !== undefined) {
|
|
317
|
+
if (allow.updatedInput !== undefined && request.callId !== undefined) {
|
|
318
|
+
try {
|
|
319
|
+
this.rewrites.set(request.callId, dshInput(allow.ran.source.dialect, request.toolName, call?.args, allow.updatedInput, this.engine.toolContext(request.agent)));
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
this.engine.notice(request.agent, { kind: 'hook-error', event: 'PermissionRequest', subject, plugin: allow.ran.source.pluginId, text: `updatedInput ignored: ${messageOf(error)}` });
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return 'allowed-once';
|
|
326
|
+
}
|
|
327
|
+
return next();
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* `PermissionRequest` for a question the model asks (Claude's
|
|
331
|
+
* `AskUserQuestion` dialog): the hooks run while the person sees the
|
|
332
|
+
* question. A hook's `allow` with `updatedInput.answers` answers it; a
|
|
333
|
+
* `deny` closes it unanswered; otherwise the person's answer stands.
|
|
334
|
+
*/
|
|
335
|
+
async question(request, next) {
|
|
336
|
+
if (!this.engine.has('PermissionRequest') || request.agent === undefined)
|
|
337
|
+
return next();
|
|
338
|
+
const agent = request.agent;
|
|
339
|
+
const controller = new AbortController();
|
|
340
|
+
const outer = request.signal;
|
|
341
|
+
if (outer !== undefined) {
|
|
342
|
+
if (outer.aborted)
|
|
343
|
+
controller.abort(outer.reason);
|
|
344
|
+
else
|
|
345
|
+
outer.addEventListener('abort', () => { controller.abort(outer.reason); }, { once: true });
|
|
346
|
+
}
|
|
347
|
+
// The person's dialog closes when a hook decides first.
|
|
348
|
+
request.signal = controller.signal;
|
|
349
|
+
const person = next();
|
|
350
|
+
const args = { questions: request.questions.map(item => ({ question: item.question, ...(item.header !== undefined ? { header: item.header } : {}), options: item.options ?? [], multi_select: item.multiSelect === true })) };
|
|
351
|
+
const decided = this.engine.run({
|
|
352
|
+
event: 'PermissionRequest',
|
|
353
|
+
agent,
|
|
354
|
+
signal: controller.signal,
|
|
355
|
+
tool: { dshName: 'ask_user_question', args, ...(request.wait?.callId !== undefined ? { callId: request.wait.callId } : {}) },
|
|
356
|
+
}).then((results) => {
|
|
357
|
+
this.engine.noticeAll(agent, 'PermissionRequest', 'AskUserQuestion', results);
|
|
358
|
+
for (const ran of results) {
|
|
359
|
+
const decision = specific(ran.decoded)?.['decision'];
|
|
360
|
+
if (!isRecord(decision))
|
|
361
|
+
continue;
|
|
362
|
+
if (decision['behavior'] === 'deny')
|
|
363
|
+
return { answers: request.questions.map(item => ({ id: item.id, selected: [] })) };
|
|
364
|
+
if (decision['behavior'] === 'allow') {
|
|
365
|
+
const given = isRecord(decision['updatedInput']) && isRecord(decision['updatedInput']['answers']) ? decision['updatedInput']['answers'] : undefined;
|
|
366
|
+
if (given === undefined)
|
|
367
|
+
continue;
|
|
368
|
+
return {
|
|
369
|
+
answers: request.questions.map((item) => {
|
|
370
|
+
const value = given[item.question];
|
|
371
|
+
const text = typeof value === 'string' ? value : Array.isArray(value) ? value.map(String).join(', ') : '';
|
|
372
|
+
const labels = (item.options ?? []).map(option => option.label);
|
|
373
|
+
const chosen = text.split(/,\s*/u).filter(label => labels.includes(label));
|
|
374
|
+
return chosen.length > 0 ? { id: item.id, selected: chosen } : text.length > 0 ? { id: item.id, selected: [], custom: text } : { id: item.id, selected: [] };
|
|
375
|
+
}),
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return undefined;
|
|
380
|
+
}, () => undefined);
|
|
381
|
+
const first = await Promise.race([person.then(answer => ({ answer })), decided.then(answer => ({ hook: answer }))]);
|
|
382
|
+
if ('answer' in first)
|
|
383
|
+
return first.answer;
|
|
384
|
+
if (first.hook === undefined)
|
|
385
|
+
return person;
|
|
386
|
+
controller.abort(new Error('answered by a PermissionRequest hook'));
|
|
387
|
+
person.catch(() => undefined);
|
|
388
|
+
return first.hook;
|
|
389
|
+
}
|
|
390
|
+
permissionDenied(request, message, interrupt, results) {
|
|
391
|
+
if (interrupt)
|
|
392
|
+
this.engine.halt(request.agent, 'PermissionRequest', message, results);
|
|
393
|
+
else
|
|
394
|
+
request.agent.inject(this.engine.contextMessage(request.agent, 'PermissionRequest', `Permission to use ${request.toolName} was denied: ${message}`));
|
|
395
|
+
return 'rejected';
|
|
396
|
+
}
|
|
397
|
+
/** The PreToolUse decision one run expresses; `none` carries an `updatedInput` without a decision. */
|
|
398
|
+
permissionDecision(ran) {
|
|
399
|
+
if (ran.decoded.block !== undefined) {
|
|
400
|
+
const reason = ran.failureBlock ?? (ran.decoded.json !== undefined && specific(ran.decoded)?.['permissionDecisionReason'] !== undefined
|
|
401
|
+
? String(specific(ran.decoded)?.['permissionDecisionReason'])
|
|
402
|
+
: `PreToolUse:${ran.toolName ?? ''} hook error: [${ran.label}]: ${ran.decoded.block}`);
|
|
403
|
+
return { kind: 'deny', reason };
|
|
404
|
+
}
|
|
405
|
+
if (ran.verdict !== undefined && !ran.verdict.ok) {
|
|
406
|
+
const continueOnBlock = ran.handler.type === 'agent' || ran.handler.continueOnBlock === true;
|
|
407
|
+
return { kind: 'deny', reason: ran.verdict.reason ?? 'blocked by a PreToolUse hook', endTurn: !continueOnBlock };
|
|
408
|
+
}
|
|
409
|
+
const hso = specific(ran.decoded);
|
|
410
|
+
const reason = typeof hso?.['permissionDecisionReason'] === 'string' ? hso['permissionDecisionReason'] : typeof ran.decoded.json?.['reason'] === 'string' ? ran.decoded.json['reason'] : undefined;
|
|
411
|
+
const updated = isRecord(hso?.['updatedInput']) ? hso['updatedInput'] : undefined;
|
|
412
|
+
const legacy = ran.decoded.json?.['decision'];
|
|
413
|
+
const raw = hso?.['permissionDecision'] ?? (legacy === 'approve' ? 'allow' : legacy === 'block' ? 'deny' : undefined);
|
|
414
|
+
if (raw === 'allow' || raw === 'deny' || raw === 'ask' || raw === 'defer') {
|
|
415
|
+
return { kind: raw, ...(reason !== undefined ? { reason } : {}), ...(updated !== undefined && raw !== 'defer' ? { updatedInput: updated } : {}) };
|
|
416
|
+
}
|
|
417
|
+
return updated !== undefined ? { kind: 'none', updatedInput: updated } : undefined;
|
|
418
|
+
}
|
|
419
|
+
markDenied(callId) {
|
|
420
|
+
const call = callId === undefined ? undefined : this.calls.get(callId);
|
|
421
|
+
if (call !== undefined)
|
|
422
|
+
call.deniedByHook = true;
|
|
423
|
+
}
|
|
424
|
+
}
|