@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,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hook broker's observational events: committed session events and file
|
|
3
|
+
* watching.
|
|
4
|
+
* @module @kirkchinese/harness-forge
|
|
5
|
+
*/
|
|
6
|
+
import { existsSync, watch } from 'node:fs';
|
|
7
|
+
import { basename, dirname, join } from 'node:path';
|
|
8
|
+
import { contentPath } from '@kirkchinese/harness-forge-core';
|
|
9
|
+
import { specific } from '../output.js';
|
|
10
|
+
import { foreignTool } from '../vocabulary.js';
|
|
11
|
+
import { isRecord, lastAssistantText, textOf } from './helpers.js';
|
|
12
|
+
/**
|
|
13
|
+
* Committed session events (compaction, turn ends, working-directory
|
|
14
|
+
* changes, approvals, model responses), the idle notification and the
|
|
15
|
+
* FileChanged watchers, each raised in the background.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* The files a `FileChanged` matcher names (`a.txt|b.md`), resolved against the working directory; a name on a
|
|
19
|
+
* network share the working directory is not on is left out unwatched (see `contentPath`).
|
|
20
|
+
*/
|
|
21
|
+
export function fileChangedTargets(matcher, cwd) {
|
|
22
|
+
return matcher.split('|').map(item => item.trim()).filter(item => item.length > 0)
|
|
23
|
+
.map(name => contentPath(name, cwd)).filter((path) => path !== undefined);
|
|
24
|
+
}
|
|
25
|
+
export class SessionEvents {
|
|
26
|
+
engine;
|
|
27
|
+
constructor(engine) {
|
|
28
|
+
this.engine = engine;
|
|
29
|
+
}
|
|
30
|
+
// -------------------------------------------------------------------------
|
|
31
|
+
// Committed session events
|
|
32
|
+
// -------------------------------------------------------------------------
|
|
33
|
+
sessionEvent(session, event) {
|
|
34
|
+
const agent = this.engine.agentOf(session.header.id);
|
|
35
|
+
const data = event.data ?? {};
|
|
36
|
+
switch (event.type) {
|
|
37
|
+
case 'compaction/start': {
|
|
38
|
+
if (!this.engine.has('PreCompact'))
|
|
39
|
+
return;
|
|
40
|
+
const trigger = data['sourceCommandId'] !== undefined ? 'manual' : 'auto';
|
|
41
|
+
this.detachedEvent(agent, { event: 'PreCompact', subject: trigger, fields: { trigger, custom_instructions: null } });
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
case 'compaction/summary':
|
|
45
|
+
this.engine.state(session).lastSummary = textOf(Array.isArray(data['summary']) ? data['summary'] : []);
|
|
46
|
+
return;
|
|
47
|
+
case 'compaction/end': {
|
|
48
|
+
if (!this.engine.has('PostCompact') || data['error'] !== undefined)
|
|
49
|
+
return;
|
|
50
|
+
const trigger = data['sourceCommandId'] !== undefined ? 'manual' : 'auto';
|
|
51
|
+
this.detachedEvent(agent, { event: 'PostCompact', subject: trigger, fields: { trigger, compact_summary: this.engine.state(session).lastSummary ?? '' } }, true);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
case 'turn/end': {
|
|
55
|
+
const reason = isRecord(data['reason']) ? data['reason'] : {};
|
|
56
|
+
if (reason['kind'] === 'error' && this.engine.has('StopFailure')) {
|
|
57
|
+
const failure = isRecord(reason['error']) ? reason['error'] : {};
|
|
58
|
+
const error = stopFailureType(String(failure['code'] ?? ''), String(failure['message'] ?? ''));
|
|
59
|
+
this.detachedEvent(agent, {
|
|
60
|
+
event: 'StopFailure',
|
|
61
|
+
subject: error,
|
|
62
|
+
fields: { error, error_details: String(failure['message'] ?? ''), last_assistant_message: `API Error: ${String(failure['message'] ?? 'unknown error')}` },
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (reason['kind'] === 'max-tokens' && this.engine.has('StopFailure')) {
|
|
66
|
+
this.detachedEvent(agent, { event: 'StopFailure', subject: 'max_output_tokens', fields: { error: 'max_output_tokens', last_assistant_message: lastAssistantText(session) } });
|
|
67
|
+
}
|
|
68
|
+
if (reason['kind'] === 'aborted' && isRecord(reason['reason']) && reason['reason']['kind'] === 'user' && this.engine.has('Interrupt')) {
|
|
69
|
+
this.detachedEvent(agent, { event: 'Interrupt', fields: { turn_id: String(data['turn'] ?? '') } });
|
|
70
|
+
}
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
case 'working-directory/change': {
|
|
74
|
+
if (agent === undefined)
|
|
75
|
+
return;
|
|
76
|
+
const state = this.engine.state(session);
|
|
77
|
+
const oldCwd = state.cwd ?? session.header.cwd ?? '';
|
|
78
|
+
state.cwd = String(data['cwd'] ?? '');
|
|
79
|
+
this.engine.clearEnv(session, 'cwd');
|
|
80
|
+
if (this.engine.has('CwdChanged')) {
|
|
81
|
+
this.detachedEvent(agent, { event: 'CwdChanged', fields: { old_cwd: oldCwd, new_cwd: state.cwd }, cwd: state.cwd }, true, this.engine.envFile(session, 'cwd'));
|
|
82
|
+
}
|
|
83
|
+
this.startWatching(agent);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
case 'approval/asked': {
|
|
87
|
+
if (!this.engine.has('Notification') || agent === undefined)
|
|
88
|
+
return;
|
|
89
|
+
const id = String(data['id'] ?? '');
|
|
90
|
+
const tool = foreignTool('claude-code', String(data['toolName'] ?? ''), {}, this.engine.toolContext(agent)).name;
|
|
91
|
+
this.engine.notifyTimers.set(id, setTimeout(() => {
|
|
92
|
+
this.engine.notifyTimers.delete(id);
|
|
93
|
+
this.detachedEvent(agent, { event: 'Notification', subject: 'permission_prompt', fields: { notification_type: 'permission_prompt', title: 'Permission needed', message: `Claude needs your permission to use ${tool}` } });
|
|
94
|
+
}, 6_000));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
case 'approval/decided': {
|
|
98
|
+
const id = String(data['id'] ?? '');
|
|
99
|
+
clearTimeout(this.engine.notifyTimers.get(id));
|
|
100
|
+
this.engine.notifyTimers.delete(id);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
case 'assistant/message': {
|
|
104
|
+
if (!this.engine.has('PostModelRequest') || agent === undefined)
|
|
105
|
+
return;
|
|
106
|
+
const message = isRecord(data['message']) ? data['message'] : {};
|
|
107
|
+
this.detachedEvent(agent, { event: 'PostModelRequest', fields: { model: agent.options?.model, turn_id: String(data['turn'] ?? ''), status: 'completed', error: null, token_usage: message['usage'] ?? null } });
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
default:
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** Arm the `idle_prompt` notification: 60 s after a root turn ends without new input. */
|
|
114
|
+
armIdle(agent) {
|
|
115
|
+
if (!this.engine.has('Notification'))
|
|
116
|
+
return;
|
|
117
|
+
const id = agent.session.header.id;
|
|
118
|
+
clearTimeout(this.engine.idleTimers.get(id));
|
|
119
|
+
this.engine.idleTimers.set(id, setTimeout(() => {
|
|
120
|
+
this.engine.idleTimers.delete(id);
|
|
121
|
+
this.detachedEvent(agent, { event: 'Notification', subject: 'idle_prompt', fields: { notification_type: 'idle_prompt', title: 'Waiting for input', message: 'Claude is waiting for your input' } });
|
|
122
|
+
}, 60_000));
|
|
123
|
+
}
|
|
124
|
+
// -------------------------------------------------------------------------
|
|
125
|
+
// FileChanged
|
|
126
|
+
// -------------------------------------------------------------------------
|
|
127
|
+
setWatchPaths(agent, paths) {
|
|
128
|
+
this.engine.state(agent.session).watchPaths = [...paths];
|
|
129
|
+
this.startWatching(agent);
|
|
130
|
+
}
|
|
131
|
+
/** Watch the FileChanged matcher filenames in the working directory plus the dynamic watch list. */
|
|
132
|
+
startWatching(agent) {
|
|
133
|
+
if (!this.engine.has('FileChanged'))
|
|
134
|
+
return;
|
|
135
|
+
const state = this.engine.state(agent.session);
|
|
136
|
+
for (const watcher of state.watchers)
|
|
137
|
+
watcher.close();
|
|
138
|
+
state.watchers = [];
|
|
139
|
+
const cwd = state.cwd ?? this.engine.committedDirectory(agent) ?? agent.session.header.cwd ?? process.cwd();
|
|
140
|
+
const files = new Set(state.watchPaths);
|
|
141
|
+
for (const source of this.engine.sources.values()) {
|
|
142
|
+
for (const group of source.table.get('FileChanged') ?? []) {
|
|
143
|
+
for (const file of fileChangedTargets(group.matcher ?? '', cwd))
|
|
144
|
+
files.add(file);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const byDir = new Map();
|
|
148
|
+
for (const file of files) {
|
|
149
|
+
const dir = dirname(file);
|
|
150
|
+
if (!existsSync(dir))
|
|
151
|
+
continue;
|
|
152
|
+
byDir.set(dir, (byDir.get(dir) ?? new Set()).add(basename(file)));
|
|
153
|
+
}
|
|
154
|
+
for (const [dir, names] of byDir) {
|
|
155
|
+
try {
|
|
156
|
+
const watcher = watch(dir, (_type, filename) => {
|
|
157
|
+
if (filename === null || !names.has(filename.toString()))
|
|
158
|
+
return;
|
|
159
|
+
const path = join(dir, filename.toString());
|
|
160
|
+
const kind = existsSync(path) ? 'change' : 'unlink';
|
|
161
|
+
this.detachedEvent(agent, { event: 'FileChanged', subject: basename(path), fields: { file_path: path, event: kind } }, true, this.engine.envFile(agent.session, 'cwd'));
|
|
162
|
+
});
|
|
163
|
+
watcher.unref();
|
|
164
|
+
state.watchers.push(watcher);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
// A directory that cannot be watched is skipped.
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/** Fire an observational event in the background. */
|
|
172
|
+
detachedEvent(agent, occurrence, userErrors = false, envFile) {
|
|
173
|
+
this.engine.track(this.engine.run({ ...occurrence, ...(agent !== undefined ? { agent } : {}), signal: this.engine.lifetime.signal }, envFile !== undefined ? { envFile } : {}).then((results) => {
|
|
174
|
+
if (agent === undefined)
|
|
175
|
+
return;
|
|
176
|
+
this.engine.noticeAll(agent, occurrence.event, occurrence.subject, results);
|
|
177
|
+
for (const ran of results) {
|
|
178
|
+
if (userErrors && ran.decoded.block !== undefined)
|
|
179
|
+
this.engine.notice(agent, { kind: 'hook-error', event: occurrence.event, ...(occurrence.subject !== undefined ? { subject: occurrence.subject } : {}), plugin: ran.source.pluginId, text: ran.decoded.block });
|
|
180
|
+
const watchPaths = specific(ran.decoded)?.['watchPaths'];
|
|
181
|
+
if ((occurrence.event === 'CwdChanged' || occurrence.event === 'FileChanged') && Array.isArray(watchPaths))
|
|
182
|
+
this.setWatchPaths(agent, watchPaths.map(String));
|
|
183
|
+
}
|
|
184
|
+
}));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** Claude's StopFailure error type for a DSH LLM failure. */
|
|
188
|
+
function stopFailureType(code, message) {
|
|
189
|
+
const text = `${code} ${message}`.toLowerCase();
|
|
190
|
+
if (/rate.?limit|429/u.test(text))
|
|
191
|
+
return 'rate_limit';
|
|
192
|
+
if (/overload|529/u.test(text))
|
|
193
|
+
return 'overloaded';
|
|
194
|
+
if (/auth|401|403|api key/u.test(text))
|
|
195
|
+
return 'authentication_failed';
|
|
196
|
+
if (/billing|quota|payment|402/u.test(text))
|
|
197
|
+
return 'billing_error';
|
|
198
|
+
if (/model.*(not.?found|does not exist)/u.test(text))
|
|
199
|
+
return 'model_not_found';
|
|
200
|
+
if (/invalid|400/u.test(text))
|
|
201
|
+
return 'invalid_request';
|
|
202
|
+
if (/5\d\d|server/u.test(text))
|
|
203
|
+
return 'server_error';
|
|
204
|
+
return 'unknown';
|
|
205
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { HookEngine } from './engine.js';
|
|
2
|
+
import type { ApprovalRequest, PostDecision, QuestionAnswer, QuestionRequest, ToolExecutionLike, ToolResultLike } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Tool calls and permission requests: PreToolUse with permission rules and
|
|
5
|
+
* mods' `tool.check`, rewritten input through `tools/execute`,
|
|
6
|
+
* PostToolUse / PostToolUseFailure, and PermissionRequest for approvals and
|
|
7
|
+
* questions.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ToolGate {
|
|
10
|
+
private readonly engine;
|
|
11
|
+
/** Tool arguments by call id, for PermissionRequest and PostToolUse. */
|
|
12
|
+
private readonly calls;
|
|
13
|
+
/** Rewritten DSH arguments by call id. */
|
|
14
|
+
private readonly rewrites;
|
|
15
|
+
constructor(engine: HookEngine);
|
|
16
|
+
/**
|
|
17
|
+
* The DSH arguments a pending call runs with after a hook rewrote them.
|
|
18
|
+
* The tool's own `tools/execute` listener reads this.
|
|
19
|
+
*/
|
|
20
|
+
rewriteOf(callId: string | undefined): Record<string, unknown> | undefined;
|
|
21
|
+
/** A mod rewrote a call's arguments (Claude vocabulary already mapped to DSH's): the hooks and the tool see them. */
|
|
22
|
+
rewrite(callId: string, args: Record<string, unknown>): void;
|
|
23
|
+
preTool(exec: ToolExecutionLike, next: () => Promise<unknown>): Promise<unknown>;
|
|
24
|
+
/**
|
|
25
|
+
* The decision Claude Code reaches, as a verdict mods' `tool.check` hooks
|
|
26
|
+
* may answer over; mapped back to DSH's pre-execute decision.
|
|
27
|
+
*/
|
|
28
|
+
private checkedDecision;
|
|
29
|
+
private decide;
|
|
30
|
+
/** Calls an allow rule or hook let through: DSH asks nobody about them (a sandbox escalation included). */
|
|
31
|
+
private readonly allowedCalls;
|
|
32
|
+
/** What stands behind a call's decision (a rule, a PreToolUse hook), for tool.check. */
|
|
33
|
+
private readonly behindDecision;
|
|
34
|
+
private behind;
|
|
35
|
+
private ruleVerdictOf;
|
|
36
|
+
executeTool(exec: ToolExecutionLike, next: () => Promise<unknown>): Promise<unknown>;
|
|
37
|
+
postTool(exec: ToolExecutionLike, result: ToolResultLike, next: () => Promise<PostDecision>): Promise<PostDecision>;
|
|
38
|
+
/** `updatedToolOutput` / `updatedMCPToolOutput`: a DSH value when the shape maps, else the model-facing text. */
|
|
39
|
+
private outputReplacement;
|
|
40
|
+
permission(request: ApprovalRequest, next: () => Promise<string>): Promise<string>;
|
|
41
|
+
/**
|
|
42
|
+
* `PermissionRequest` for a question the model asks (Claude's
|
|
43
|
+
* `AskUserQuestion` dialog): the hooks run while the person sees the
|
|
44
|
+
* question. A hook's `allow` with `updatedInput.answers` answers it; a
|
|
45
|
+
* `deny` closes it unanswered; otherwise the person's answer stands.
|
|
46
|
+
*/
|
|
47
|
+
question(request: QuestionRequest, next: () => Promise<QuestionAnswer>): Promise<QuestionAnswer>;
|
|
48
|
+
private permissionDenied;
|
|
49
|
+
/** The PreToolUse decision one run expresses; `none` carries an `updatedInput` without a decision. */
|
|
50
|
+
private permissionDecision;
|
|
51
|
+
private markDenied;
|
|
52
|
+
}
|