@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,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remote MCP servers (Streamable HTTP and legacy SSE) with OAuth sign-in.
|
|
3
|
+
* The MCP SDK client connects; tools register through the official
|
|
4
|
+
* `createMcpToolDefinition`, so naming, schemas, task rejection and result
|
|
5
|
+
* projection match DSH's native MCP client. A server that requires sign-in
|
|
6
|
+
* stays mounted in the `sign-in required` state until the user signs in.
|
|
7
|
+
*
|
|
8
|
+
* The bridge runs inside `ctx.inject(['tools'])`: Cordis only permits the
|
|
9
|
+
* `ctx.tools` accessor in a context that declared the dependency.
|
|
10
|
+
* @module @kirkchinese/harness-forge
|
|
11
|
+
*/
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
13
|
+
import { Client, SSEClientTransport, StreamableHTTPClientTransport, UnauthorizedError } from '@modelcontextprotocol/client';
|
|
14
|
+
import { createMcpToolDefinition } from '@deepseek-ai/dsh-mcp-client';
|
|
15
|
+
import { DEFAULT_TOOL_CALL_TIMEOUT_MS, mcpToolPolicy, publicToolName } from './policy.js';
|
|
16
|
+
/** This package's version, as the MCP client names itself to a server. */
|
|
17
|
+
const PACKAGE_VERSION = createRequire(import.meta.url)('../../package.json').version;
|
|
18
|
+
/** One live remote server. */
|
|
19
|
+
export class RemoteMcp {
|
|
20
|
+
ctx;
|
|
21
|
+
component;
|
|
22
|
+
serverName;
|
|
23
|
+
auth;
|
|
24
|
+
onState;
|
|
25
|
+
client;
|
|
26
|
+
transport;
|
|
27
|
+
toolCtx;
|
|
28
|
+
fiber;
|
|
29
|
+
registrations = [];
|
|
30
|
+
authorizationUrl;
|
|
31
|
+
state = { kind: 'failed', error: 'not connected' };
|
|
32
|
+
closed = false;
|
|
33
|
+
constructor(ctx, component, serverName, auth, onState) {
|
|
34
|
+
this.ctx = ctx;
|
|
35
|
+
this.component = component;
|
|
36
|
+
this.serverName = serverName;
|
|
37
|
+
this.auth = auth;
|
|
38
|
+
this.onState = onState;
|
|
39
|
+
}
|
|
40
|
+
/** Connect and register tools; resolves with the resulting state. */
|
|
41
|
+
async start() {
|
|
42
|
+
this.toolCtx ??= await new Promise((resolve) => {
|
|
43
|
+
this.fiber = this.ctx.inject(['tools'], (toolCtx) => { resolve(toolCtx); });
|
|
44
|
+
});
|
|
45
|
+
await this.auth.prepare(this.component.url);
|
|
46
|
+
return this.connect();
|
|
47
|
+
}
|
|
48
|
+
/** Open the sign-in page, finish the OAuth exchange, then connect. */
|
|
49
|
+
async signIn() {
|
|
50
|
+
if (this.state.kind !== 'sign-in-required' || this.authorizationUrl === undefined || this.transport === undefined)
|
|
51
|
+
return this.state;
|
|
52
|
+
const params = await this.auth.signIn(this.component.url, this.authorizationUrl);
|
|
53
|
+
await this.transport.finishAuth(params);
|
|
54
|
+
return this.connect();
|
|
55
|
+
}
|
|
56
|
+
get current() {
|
|
57
|
+
return this.state;
|
|
58
|
+
}
|
|
59
|
+
/** Whether the server was disposed: it waits for nothing any more. */
|
|
60
|
+
get disposed() {
|
|
61
|
+
return this.closed;
|
|
62
|
+
}
|
|
63
|
+
async dispose() {
|
|
64
|
+
this.closed = true;
|
|
65
|
+
this.unregister();
|
|
66
|
+
await this.client?.close().catch(() => undefined);
|
|
67
|
+
await this.fiber?.dispose();
|
|
68
|
+
}
|
|
69
|
+
async connect() {
|
|
70
|
+
this.unregister();
|
|
71
|
+
await this.client?.close().catch(() => undefined);
|
|
72
|
+
const url = new URL(this.component.url);
|
|
73
|
+
const options = {
|
|
74
|
+
requestInit: { headers: { ...this.component.headers } },
|
|
75
|
+
authProvider: this.auth.provider(this.component.url, (authorizationUrl) => { this.authorizationUrl = authorizationUrl; }),
|
|
76
|
+
};
|
|
77
|
+
const transport = this.component.transport === 'sse'
|
|
78
|
+
? new SSEClientTransport(url, options)
|
|
79
|
+
: new StreamableHTTPClientTransport(url, options);
|
|
80
|
+
const client = new Client({ name: 'harness-forge', version: PACKAGE_VERSION }, { capabilities: {}, versionNegotiation: { mode: 'auto' } });
|
|
81
|
+
this.transport = transport;
|
|
82
|
+
this.client = client;
|
|
83
|
+
try {
|
|
84
|
+
await client.connect(transport);
|
|
85
|
+
const response = await client.listTools(undefined, { cacheMode: 'refresh' });
|
|
86
|
+
// Tools the server's configuration switches off are never registered.
|
|
87
|
+
const policy = mcpToolPolicy(this.component, this.serverName);
|
|
88
|
+
const tools = response.tools.filter(tool => policy?.runs(tool.name) ?? true).map(tool => this.register(client, tool));
|
|
89
|
+
return this.settle({ kind: 'connected', tools });
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (error instanceof UnauthorizedError && this.authorizationUrl !== undefined) {
|
|
93
|
+
return this.settle({ kind: 'sign-in-required', url: this.authorizationUrl.href });
|
|
94
|
+
}
|
|
95
|
+
await client.close().catch(() => undefined);
|
|
96
|
+
return this.settle({ kind: 'failed', error: rootCause(error) });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
register(client, tool) {
|
|
100
|
+
const toolCtx = this.toolCtx;
|
|
101
|
+
const name = publicToolName(this.serverName, tool.name);
|
|
102
|
+
const definition = createMcpToolDefinition(toolCtx, {
|
|
103
|
+
name,
|
|
104
|
+
rawName: tool.name,
|
|
105
|
+
description: tool.description ?? '',
|
|
106
|
+
inputSchema: tool.inputSchema,
|
|
107
|
+
outputSchema: tool.outputSchema,
|
|
108
|
+
taskRequired: tool.execution?.taskSupport === 'required',
|
|
109
|
+
call: (args, execution) => client.callTool({ name: tool.name, arguments: args }, { signal: execution.signal, timeout: this.component.toolTimeoutMs ?? DEFAULT_TOOL_CALL_TIMEOUT_MS, toolDefinition: tool }),
|
|
110
|
+
});
|
|
111
|
+
this.registrations.push(toolCtx.tools.register(definition));
|
|
112
|
+
return name;
|
|
113
|
+
}
|
|
114
|
+
unregister() {
|
|
115
|
+
for (const dispose of this.registrations.splice(0))
|
|
116
|
+
dispose();
|
|
117
|
+
}
|
|
118
|
+
settle(state) {
|
|
119
|
+
this.state = state;
|
|
120
|
+
this.onState(state);
|
|
121
|
+
return state;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function rootCause(error) {
|
|
125
|
+
let current = error;
|
|
126
|
+
let message = error instanceof Error ? error.message : String(error);
|
|
127
|
+
while (current instanceof Error && current.cause !== undefined) {
|
|
128
|
+
current = current.cause;
|
|
129
|
+
if (current instanceof Error)
|
|
130
|
+
message = current.message;
|
|
131
|
+
}
|
|
132
|
+
return message;
|
|
133
|
+
}
|
package/lib/mcp/row.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { Disposer } from '../forge/contract.js';
|
|
3
|
+
import { type HarnessForge } from '../forge/service.js';
|
|
4
|
+
export declare const name = "harness-forge-mcp";
|
|
5
|
+
export declare const inject: string[];
|
|
6
|
+
/** The MCP row. */
|
|
7
|
+
export declare function apply(ctx: Context): void;
|
|
8
|
+
/** Start the MCP row on the shared service. Returns its teardown. */
|
|
9
|
+
export declare function startMcp(ctx: Context, forge: HarnessForge): Disposer;
|
package/lib/mcp/row.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MCP row: imported MCP servers, stdio through DSH's own MCP client (or
|
|
3
|
+
* the capability catalog when the profile composes it), Streamable HTTP and
|
|
4
|
+
* SSE with OAuth sign-in. Their tool calls are the components' evidence.
|
|
5
|
+
* @module @kirkchinese/harness-forge/mcp
|
|
6
|
+
*/
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { HarnessError } from '@kirkchinese/harness-forge-core';
|
|
9
|
+
import { onDsh } from '../dsh/host.js';
|
|
10
|
+
import { FORGE_SERVICE } from '../forge/service.js';
|
|
11
|
+
import { McpAuthStore } from './auth.js';
|
|
12
|
+
import { CATALOG_CALL_TOOL, CATALOG_SERVICES, catalogEntry } from './catalog.js';
|
|
13
|
+
import { foreignServerName, harnessVariables, mcpServerName, mountMcp, servedByCatalog } from './mount.js';
|
|
14
|
+
import { mcpToolPolicy } from './policy.js';
|
|
15
|
+
import { messageOf } from '../shared/util.js';
|
|
16
|
+
import { mounted, Parts, rowEffect } from '../forge/row.js';
|
|
17
|
+
export const name = 'harness-forge-mcp';
|
|
18
|
+
export const inject = [FORGE_SERVICE];
|
|
19
|
+
/** The MCP row. */
|
|
20
|
+
export function apply(ctx) {
|
|
21
|
+
rowEffect(ctx, 'mcp', forge => startMcp(ctx, forge));
|
|
22
|
+
}
|
|
23
|
+
/** Claude Code's scoped hook server name `plugin:<plugin>:<server>`. */
|
|
24
|
+
const SCOPED_HOOK_SERVER = /^plugin:([^:]+):(.+)$/u;
|
|
25
|
+
/** Start the MCP row on the shared service. Returns its teardown. */
|
|
26
|
+
export function startMcp(ctx, forge) {
|
|
27
|
+
const auth = new McpAuthStore(join(forge.dataRoot, 'mcp-auth'));
|
|
28
|
+
/**
|
|
29
|
+
* Remote servers that reported a state, by server object: the same name may
|
|
30
|
+
* be mounted process-wide and in several sessions' projects at once, each
|
|
31
|
+
* with its own sign-in state and component.
|
|
32
|
+
*/
|
|
33
|
+
const remotes = new Map();
|
|
34
|
+
/**
|
|
35
|
+
* Each server's registration, with its component and session: published
|
|
36
|
+
* before its tools are, and kept until their withdrawal succeeds, so its
|
|
37
|
+
* policy covers every moment they can be called.
|
|
38
|
+
*/
|
|
39
|
+
const servers = new Set();
|
|
40
|
+
const remoteHost = (componentId, scope = {}) => ({
|
|
41
|
+
auth,
|
|
42
|
+
onState: (server, state) => {
|
|
43
|
+
remotes.set(server, { componentId, server, ...(scope.sessionId !== undefined ? { sessionId: scope.sessionId } : {}) });
|
|
44
|
+
if (state.kind === 'sign-in-required')
|
|
45
|
+
forge.observe(componentId, 'sign-in', false, `sign-in required: run /harness login ${server.serverName}`);
|
|
46
|
+
else
|
|
47
|
+
forge.clearObservation(componentId, 'sign-in');
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
/** Servers waiting for sign-in that a session sees (its own and process-wide ones); every one without a session. */
|
|
51
|
+
const waiting = (sessionId) => {
|
|
52
|
+
for (const server of [...remotes.keys()])
|
|
53
|
+
if (server.disposed)
|
|
54
|
+
remotes.delete(server);
|
|
55
|
+
return [...remotes.values()].filter(entry => entry.server.current.kind === 'sign-in-required'
|
|
56
|
+
&& (sessionId === undefined || entry.sessionId === undefined || entry.sessionId === sessionId));
|
|
57
|
+
};
|
|
58
|
+
const api = {
|
|
59
|
+
pendingSignIns: sessionId => [...new Set(waiting(sessionId).map(entry => entry.server.serverName))],
|
|
60
|
+
startSignIn: (serverName, sessionId) => {
|
|
61
|
+
const named = waiting(sessionId).filter(entry => entry.server.serverName === serverName);
|
|
62
|
+
// The session's own server first, then the process-wide one.
|
|
63
|
+
const remote = named.find(entry => sessionId !== undefined && entry.sessionId === sessionId)
|
|
64
|
+
?? named.find(entry => entry.sessionId === undefined)
|
|
65
|
+
?? named[0];
|
|
66
|
+
const state = remote?.server.current;
|
|
67
|
+
if (remote === undefined || state?.kind !== 'sign-in-required')
|
|
68
|
+
throw new Error(`MCP server "${serverName}" is not waiting for sign-in`);
|
|
69
|
+
void remote.server.signIn().then((next) => {
|
|
70
|
+
if (next.kind === 'failed')
|
|
71
|
+
forge.observe(remote.componentId, 'sign-in', false, `sign-in failed: ${next.error}`);
|
|
72
|
+
}, (error) => {
|
|
73
|
+
forge.observe(remote.componentId, 'sign-in', false, `sign-in failed: ${messageOf(error)}`);
|
|
74
|
+
});
|
|
75
|
+
return state.url;
|
|
76
|
+
},
|
|
77
|
+
remoteHost,
|
|
78
|
+
toolPolicy: call => toolPolicy(call),
|
|
79
|
+
hookServer: (pluginId, server, sessionId) => {
|
|
80
|
+
const scoped = SCOPED_HOOK_SERVER.exec(server);
|
|
81
|
+
const declared = scoped?.[2] ?? server;
|
|
82
|
+
const named = [...servers].filter(entry => entry.declaredName === declared);
|
|
83
|
+
const pick = (matching) => matching.find(entry => sessionId !== undefined && entry.sessionId === sessionId) ?? matching.find(entry => entry.sessionId === undefined);
|
|
84
|
+
const own = pick(named.filter(entry => scoped === null ? entry.pluginId === pluginId : entry.pluginName === scoped[1]));
|
|
85
|
+
return (own ?? (scoped === null ? pick(named.filter(entry => entry.userLevel)) : undefined))?.serverName;
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
const mounter = {
|
|
89
|
+
mount: async ({ component, plugin, dataDir, scope }) => {
|
|
90
|
+
if (component.kind !== 'mcp')
|
|
91
|
+
throw new HarnessError('component-failed', `action expects MCP: ${component.id}`);
|
|
92
|
+
const serverName = mcpServerName(foreignServerName(plugin, component.name));
|
|
93
|
+
// A session's project servers stay in its own context; the catalog is process-wide.
|
|
94
|
+
const vars = { pluginRoot: plugin.root, dataDir, env: forge.settings.env(), provided: harnessVariables(plugin) };
|
|
95
|
+
const policy = mcpToolPolicy(component, serverName);
|
|
96
|
+
const options = { catalog: scope.sessionId === undefined };
|
|
97
|
+
const entry = (route) => ({
|
|
98
|
+
serverName,
|
|
99
|
+
componentId: component.id,
|
|
100
|
+
route,
|
|
101
|
+
pluginId: plugin.id,
|
|
102
|
+
pluginName: plugin.name,
|
|
103
|
+
declaredName: component.declaredName ?? component.name,
|
|
104
|
+
userLevel: plugin.manifest?.['scope'] === 'user',
|
|
105
|
+
...(scope.sessionId !== undefined ? { sessionId: scope.sessionId } : {}),
|
|
106
|
+
...(policy !== undefined ? { policy } : {}),
|
|
107
|
+
});
|
|
108
|
+
// Published before the server's tools can be called; a mount that fails has withdrawn them itself.
|
|
109
|
+
let server = entry(servedByCatalog(scope.ctx ?? ctx, component, options) ? 'catalog' : 'direct');
|
|
110
|
+
servers.add(server);
|
|
111
|
+
let mount;
|
|
112
|
+
try {
|
|
113
|
+
mount = await mountMcp(scope.ctx ?? ctx, component, serverName, vars, remoteHost(component.id, scope), options);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
servers.delete(server);
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
// The catalog came or went while the server mounted: the policy follows the route it took.
|
|
120
|
+
const route = mount.catalog === true ? 'catalog' : 'direct';
|
|
121
|
+
if (route !== server.route) {
|
|
122
|
+
const moved = entry(route);
|
|
123
|
+
servers.add(moved);
|
|
124
|
+
servers.delete(server);
|
|
125
|
+
server = moved;
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
outcome: mounted(component, 'L1b', mount.catalog === true ? `${serverName} (capability catalog)` : serverName),
|
|
129
|
+
// A withdrawal that fails leaves the tools registered, so their policy stays until one succeeds.
|
|
130
|
+
disposer: async () => {
|
|
131
|
+
await mount.dispose();
|
|
132
|
+
servers.delete(server);
|
|
133
|
+
if (mount.remote !== undefined)
|
|
134
|
+
remotes.delete(mount.remote);
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* The server a call goes to: a catalog call (`capability_call`) only among
|
|
141
|
+
* servers registered in the catalog, a direct call only among direct ones,
|
|
142
|
+
* so two servers of one name, one in each, never lend each other a policy.
|
|
143
|
+
* Among those, the calling session's own first, then the process-wide one of
|
|
144
|
+
* that name. A catalog call names the server and the raw
|
|
145
|
+
* tool exactly. A direct call names the tool `dsh-mcp-client` registered in
|
|
146
|
+
* the server's namespace `mcp__<server>__`; server names never hold `__`
|
|
147
|
+
* ({@link mcpServerName}), so one namespace holds it, and the policy knows
|
|
148
|
+
* the raw name of every tool it configures by that same public name.
|
|
149
|
+
*/
|
|
150
|
+
const targetOf = (exec) => {
|
|
151
|
+
const sessionId = exec.agent?.session?.header?.id;
|
|
152
|
+
const pick = (route, matches) => {
|
|
153
|
+
const matching = [...servers].filter(server => server.route === route && matches(server));
|
|
154
|
+
return matching.find(candidate => sessionId !== undefined && candidate.sessionId === sessionId)
|
|
155
|
+
?? matching.find(candidate => candidate.sessionId === undefined);
|
|
156
|
+
};
|
|
157
|
+
if (exec.name === CATALOG_CALL_TOOL) {
|
|
158
|
+
const entry = catalogEntry(exec.arguments?.id);
|
|
159
|
+
const server = entry === undefined ? undefined : pick('catalog', candidate => candidate.serverName === entry.server);
|
|
160
|
+
return entry === undefined || server === undefined ? undefined : { server, rawName: entry.tool, toolName: `mcp__${entry.server}__${entry.tool}` };
|
|
161
|
+
}
|
|
162
|
+
const server = pick('direct', candidate => exec.name.startsWith(`mcp__${candidate.serverName}__`));
|
|
163
|
+
const rawName = server?.policy?.rawName(exec.name);
|
|
164
|
+
return server === undefined ? undefined : { server, ...(rawName !== undefined ? { rawName } : {}), toolName: exec.name };
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Calls refused before they ran. DSH still runs `tools/post-execute` for the refusal's error result, but the
|
|
168
|
+
* server never saw the call: a refusal is the policy's doing, not evidence about the part, and recording it
|
|
169
|
+
* would mark the part failed (and list it in Diagnostics as one that failed to start).
|
|
170
|
+
*/
|
|
171
|
+
const refused = new WeakSet();
|
|
172
|
+
/** A server's own filters and approval modes for one call (see `McpApi.toolPolicy`). */
|
|
173
|
+
function toolPolicy(exec) {
|
|
174
|
+
const target = targetOf(exec);
|
|
175
|
+
return target?.server.policy?.decide(target.rawName, target.toolName);
|
|
176
|
+
}
|
|
177
|
+
const parts = new Parts(ctx, forge, 'mcp');
|
|
178
|
+
// A server's own tool filters and approval modes: a switched-off tool is refused, an asking one asks whatever
|
|
179
|
+
// the rest of the chain allowed. First in the chain, so nothing before it can allow the call outright.
|
|
180
|
+
parts.add('tool policy', () => onDsh(ctx, 'tools/pre-execute', (async (exec, next) => {
|
|
181
|
+
const target = targetOf(exec);
|
|
182
|
+
const verdict = toolPolicy(exec);
|
|
183
|
+
if (verdict?.kind === 'deny') {
|
|
184
|
+
refused.add(exec);
|
|
185
|
+
return { kind: 'deny', reason: verdict.reason };
|
|
186
|
+
}
|
|
187
|
+
const decided = await next();
|
|
188
|
+
if (decided.kind === 'deny' && target !== undefined)
|
|
189
|
+
refused.add(exec);
|
|
190
|
+
return verdict?.kind === 'ask' && decided.kind === 'allow' ? { kind: 'ask', reason: verdict.reason } : decided;
|
|
191
|
+
}), { owner: 'mcp', prepend: true }));
|
|
192
|
+
parts.add('tool evidence', () => onDsh(ctx, 'tools/post-execute', ((exec, result, next) => {
|
|
193
|
+
const target = targetOf(exec);
|
|
194
|
+
if (target !== undefined && !refused.has(exec)) {
|
|
195
|
+
forge.observe(target.server.componentId, `tool:${target.toolName}`, !result.isError, result.isError ? result.error?.message ?? 'tool call failed' : `tool call returned${target.toolName !== exec.name ? ' through the capability catalog' : ''}`);
|
|
196
|
+
}
|
|
197
|
+
return next();
|
|
198
|
+
}), { owner: 'mcp' }));
|
|
199
|
+
let unregister = () => { };
|
|
200
|
+
let stopping = false;
|
|
201
|
+
parts.add('mcp mounter', () => {
|
|
202
|
+
unregister = forge.registerMounter('mcp', mounter);
|
|
203
|
+
return () => unregister();
|
|
204
|
+
});
|
|
205
|
+
// The capability catalog is optional and may come and go: its stdio servers move into it when it starts and
|
|
206
|
+
// mount directly again when it stops. Nothing waits for it.
|
|
207
|
+
parts.add('capability catalog', () => {
|
|
208
|
+
const fiber = ctx.inject([...CATALOG_SERVICES], (catalogCtx) => {
|
|
209
|
+
const remount = () => {
|
|
210
|
+
if (stopping)
|
|
211
|
+
return;
|
|
212
|
+
unregister();
|
|
213
|
+
unregister = forge.registerMounter('mcp', mounter);
|
|
214
|
+
};
|
|
215
|
+
remount();
|
|
216
|
+
catalogCtx.effect(() => remount, 'harness-forge.mcp-catalog');
|
|
217
|
+
});
|
|
218
|
+
return () => fiber.dispose();
|
|
219
|
+
});
|
|
220
|
+
parts.add('api', () => forge.provide('mcp', api));
|
|
221
|
+
parts.add('sign-in store', () => () => { auth.close(); });
|
|
222
|
+
// Torn down first: the catalog's going away with this row moves nothing.
|
|
223
|
+
parts.add('stopping', () => () => { stopping = true; });
|
|
224
|
+
return () => parts.dispose();
|
|
225
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Bundler } from './loader.js';
|
|
2
|
+
/** The rolldown version mods are bundled with. */
|
|
3
|
+
export declare const ROLLDOWN_VERSION = "1.3.2";
|
|
4
|
+
/** What decides which binding package fits this machine. */
|
|
5
|
+
export interface BundlerPlatform {
|
|
6
|
+
readonly os: NodeJS.Platform;
|
|
7
|
+
readonly arch: string;
|
|
8
|
+
/** Linux with musl libc (Alpine). */
|
|
9
|
+
readonly musl?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The `@rolldown/binding-*` package rolldown loads on `platform`, as its
|
|
13
|
+
* binding loader picks it, among those rolldown publishes (its
|
|
14
|
+
* `optionalDependencies`). Undefined where rolldown has no native build.
|
|
15
|
+
*/
|
|
16
|
+
export declare function rolldownBinding(platform: BundlerPlatform): string | undefined;
|
|
17
|
+
/** What {@link ensureBundler} does outside its own logic; tests replace it. */
|
|
18
|
+
export interface BundlerHost {
|
|
19
|
+
readonly platform: BundlerPlatform;
|
|
20
|
+
/** `npm install <specs>` into `prefix`. */
|
|
21
|
+
install(specs: readonly string[], prefix: string): Promise<void>;
|
|
22
|
+
/** Import the rolldown installed under `prefix`. */
|
|
23
|
+
load(prefix: string): Promise<Bundler>;
|
|
24
|
+
}
|
|
25
|
+
/** The packages to install: the pinned rolldown and its binding for `platform`. */
|
|
26
|
+
export declare function bundlerSpecs(platform: BundlerPlatform): string[];
|
|
27
|
+
/** Where the bundler is installed under a data root. */
|
|
28
|
+
export declare function bundlerPrefix(dataRoot: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* The bundler, installing it under `dataRoot` on first use. One install runs
|
|
31
|
+
* at a time per data root; a failed one is forgotten, so the next mod tries
|
|
32
|
+
* again.
|
|
33
|
+
*/
|
|
34
|
+
export declare function ensureBundler(dataRoot: string, host?: BundlerHost): Promise<Bundler>;
|
|
35
|
+
/** Whether {@link ensureBundler} would answer without installing anything. */
|
|
36
|
+
export declare function bundlerInstalled(dataRoot: string, host?: BundlerHost): boolean;
|
|
37
|
+
/** For tests: forget the bundlers loaded so far. */
|
|
38
|
+
export declare function resetBundlers(): void;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bundler of Claude Code mods: rolldown, fetched on first use into
|
|
3
|
+
* Harness Forge's data directory, as Bun is for OpenCode plugins.
|
|
4
|
+
*
|
|
5
|
+
* rolldown is not a dependency of Harness Forge: its native binding comes as
|
|
6
|
+
* platform-specific optional dependencies, and once a DSH profile's
|
|
7
|
+
* `node_modules` holds such a package, pnpm (11.x, hoisted) stops exiting
|
|
8
|
+
* after later installs, so every plugin installed after Harness Forge would
|
|
9
|
+
* stall. Installed here with npm, outside the profile, it never reaches pnpm.
|
|
10
|
+
* The install is the pinned `rolldown` and the one binding package rolldown's
|
|
11
|
+
* own loader picks for this machine; neither has an install script.
|
|
12
|
+
* @module @kirkchinese/harness-forge/mods
|
|
13
|
+
*/
|
|
14
|
+
import { readFileSync } from 'node:fs';
|
|
15
|
+
import { createRequire } from 'node:module';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
import { pathToFileURL } from 'node:url';
|
|
18
|
+
import { npmInstall } from '../forge/npm.js';
|
|
19
|
+
/** The rolldown version mods are bundled with. */
|
|
20
|
+
export const ROLLDOWN_VERSION = '1.3.2';
|
|
21
|
+
/**
|
|
22
|
+
* The `@rolldown/binding-*` package rolldown loads on `platform`, as its
|
|
23
|
+
* binding loader picks it, among those rolldown publishes (its
|
|
24
|
+
* `optionalDependencies`). Undefined where rolldown has no native build.
|
|
25
|
+
*/
|
|
26
|
+
export function rolldownBinding(platform) {
|
|
27
|
+
const { os, arch } = platform;
|
|
28
|
+
const libc = platform.musl === true ? 'musl' : 'gnu';
|
|
29
|
+
const target = (() => {
|
|
30
|
+
switch (os) {
|
|
31
|
+
case 'darwin': return arch === 'x64' || arch === 'arm64' ? `darwin-${arch}` : undefined;
|
|
32
|
+
case 'win32': return arch === 'x64' || arch === 'arm64' ? `win32-${arch}-msvc` : undefined;
|
|
33
|
+
case 'freebsd': return arch === 'x64' ? 'freebsd-x64' : undefined;
|
|
34
|
+
case 'android': return arch === 'arm64' ? 'android-arm64' : arch === 'arm' ? 'android-arm-eabi' : undefined;
|
|
35
|
+
case 'openharmony': return arch === 'arm64' ? 'openharmony-arm64' : undefined;
|
|
36
|
+
case 'linux':
|
|
37
|
+
if (arch === 'x64' || arch === 'arm64')
|
|
38
|
+
return `linux-${arch}-${libc}`;
|
|
39
|
+
if (libc === 'musl')
|
|
40
|
+
return undefined;
|
|
41
|
+
if (arch === 'arm')
|
|
42
|
+
return 'linux-arm-gnueabihf';
|
|
43
|
+
if (arch === 'ppc64' || arch === 's390x')
|
|
44
|
+
return `linux-${arch}-gnu`;
|
|
45
|
+
return undefined;
|
|
46
|
+
default: return undefined;
|
|
47
|
+
}
|
|
48
|
+
})();
|
|
49
|
+
return target === undefined ? undefined : `@rolldown/binding-${target}`;
|
|
50
|
+
}
|
|
51
|
+
/** Whether this process runs on musl Linux, the way rolldown's binding loader decides it. */
|
|
52
|
+
function isMusl() {
|
|
53
|
+
if (process.platform !== 'linux')
|
|
54
|
+
return false;
|
|
55
|
+
try {
|
|
56
|
+
return readFileSync('/usr/bin/ldd', 'utf8').includes('musl');
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// Fall through to the process report.
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const report = process.report?.getReport();
|
|
63
|
+
if (report === undefined)
|
|
64
|
+
return false;
|
|
65
|
+
if (report.header?.glibcVersionRuntime !== undefined)
|
|
66
|
+
return false;
|
|
67
|
+
return (report.sharedObjects ?? []).some(file => file.includes('libc.musl-') || file.includes('ld-musl-'));
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** Import rolldown and its `parseAst` from `prefix/node_modules`, by file URL. */
|
|
74
|
+
async function load(prefix) {
|
|
75
|
+
const require = createRequire(join(prefix, 'package.json'));
|
|
76
|
+
const url = (specifier) => pathToFileURL(require.resolve(specifier)).href;
|
|
77
|
+
const { build } = await import(url('rolldown'));
|
|
78
|
+
const { parseAst } = await import(url('rolldown/parseAst'));
|
|
79
|
+
if (typeof build !== 'function' || typeof parseAst !== 'function')
|
|
80
|
+
throw new Error(`the rolldown under ${prefix} has no build or parseAst`);
|
|
81
|
+
return { build, parseAst };
|
|
82
|
+
}
|
|
83
|
+
const defaultHost = {
|
|
84
|
+
get platform() { return { os: process.platform, arch: process.arch, musl: isMusl() }; },
|
|
85
|
+
install: (specs, prefix) => npmInstall(specs, prefix),
|
|
86
|
+
load,
|
|
87
|
+
};
|
|
88
|
+
/** The packages to install: the pinned rolldown and its binding for `platform`. */
|
|
89
|
+
export function bundlerSpecs(platform) {
|
|
90
|
+
const binding = rolldownBinding(platform);
|
|
91
|
+
return [`rolldown@${ROLLDOWN_VERSION}`, ...(binding !== undefined ? [`${binding}@${ROLLDOWN_VERSION}`] : [])];
|
|
92
|
+
}
|
|
93
|
+
/** Where the bundler is installed under a data root. */
|
|
94
|
+
export function bundlerPrefix(dataRoot) {
|
|
95
|
+
return join(dataRoot, 'rolldown');
|
|
96
|
+
}
|
|
97
|
+
/** Whether `prefix` holds the pinned rolldown and its binding already. */
|
|
98
|
+
function installed(prefix, platform) {
|
|
99
|
+
const version = (name) => {
|
|
100
|
+
try {
|
|
101
|
+
return JSON.parse(readFileSync(join(prefix, 'node_modules', ...name.split('/'), 'package.json'), 'utf8')).version;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const binding = rolldownBinding(platform);
|
|
108
|
+
return version('rolldown') === ROLLDOWN_VERSION && (binding === undefined || version(binding) === ROLLDOWN_VERSION);
|
|
109
|
+
}
|
|
110
|
+
const loading = new Map();
|
|
111
|
+
/**
|
|
112
|
+
* The bundler, installing it under `dataRoot` on first use. One install runs
|
|
113
|
+
* at a time per data root; a failed one is forgotten, so the next mod tries
|
|
114
|
+
* again.
|
|
115
|
+
*/
|
|
116
|
+
export function ensureBundler(dataRoot, host = defaultHost) {
|
|
117
|
+
const prefix = bundlerPrefix(dataRoot);
|
|
118
|
+
const known = loading.get(prefix);
|
|
119
|
+
if (known !== undefined)
|
|
120
|
+
return known;
|
|
121
|
+
const attempt = bootstrap(prefix, host);
|
|
122
|
+
loading.set(prefix, attempt);
|
|
123
|
+
attempt.catch(() => { if (loading.get(prefix) === attempt)
|
|
124
|
+
loading.delete(prefix); });
|
|
125
|
+
return attempt;
|
|
126
|
+
}
|
|
127
|
+
/** Whether {@link ensureBundler} would answer without installing anything. */
|
|
128
|
+
export function bundlerInstalled(dataRoot, host = defaultHost) {
|
|
129
|
+
return installed(bundlerPrefix(dataRoot), host.platform);
|
|
130
|
+
}
|
|
131
|
+
async function bootstrap(prefix, host) {
|
|
132
|
+
const platform = host.platform;
|
|
133
|
+
const specs = bundlerSpecs(platform);
|
|
134
|
+
if (installed(prefix, platform)) {
|
|
135
|
+
try {
|
|
136
|
+
return await host.load(prefix);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// Install again over what is there.
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let problem;
|
|
143
|
+
try {
|
|
144
|
+
if (rolldownBinding(platform) === undefined)
|
|
145
|
+
throw new Error(`rolldown has no native build for ${platform.os} ${platform.arch}${platform.musl === true ? ' (musl)' : ''}`);
|
|
146
|
+
await host.install(specs, prefix);
|
|
147
|
+
return await host.load(prefix);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
problem = error instanceof Error ? error.message : String(error);
|
|
151
|
+
}
|
|
152
|
+
throw new Error(`The bundler Claude Code mods need (rolldown ${ROLLDOWN_VERSION}) could not be installed automatically (${problem}). Install it by hand with \`npm install ${specs.join(' ')} --ignore-scripts\` in ${prefix}, then restart DSH.`);
|
|
153
|
+
}
|
|
154
|
+
/** For tests: forget the bundlers loaded so far. */
|
|
155
|
+
export function resetBundlers() {
|
|
156
|
+
loading.clear();
|
|
157
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/** The chain's tiers, outermost first. */
|
|
2
|
+
export declare const TIERS: readonly ["prepend", "user", "append", "builtin", "core"];
|
|
3
|
+
export type Tier = typeof TIERS[number];
|
|
4
|
+
export type PluginTier = Exclude<Tier, 'core'>;
|
|
5
|
+
/** Who raised a dispatch. */
|
|
6
|
+
export interface Origin {
|
|
7
|
+
readonly plugin: string;
|
|
8
|
+
readonly tier: Tier;
|
|
9
|
+
}
|
|
10
|
+
export declare const ENGINE_ORIGIN: Origin;
|
|
11
|
+
/** A hook's own time and a `.catch` handler's grace, in milliseconds. */
|
|
12
|
+
export declare const HOOK_BUDGET_MS = 10000;
|
|
13
|
+
export declare const CATCH_BUDGET_MS = 1000;
|
|
14
|
+
/** Why a hook failed, as its `.catch` handler reads `next.error`. */
|
|
15
|
+
export interface HookFailure {
|
|
16
|
+
readonly kind: 'throw' | 'timeout' | 're-entry';
|
|
17
|
+
readonly message?: string;
|
|
18
|
+
readonly budget: number;
|
|
19
|
+
readonly cause?: 'rewritten' | 'lent';
|
|
20
|
+
}
|
|
21
|
+
/** A loaded mod, as the chain orders and names it. */
|
|
22
|
+
export interface ModRef {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly tier: PluginTier;
|
|
25
|
+
/** Load order within its tier. */
|
|
26
|
+
readonly rank: number;
|
|
27
|
+
}
|
|
28
|
+
export type HookFn = ($: unknown, e: unknown, next: unknown) => unknown;
|
|
29
|
+
/** One `on(...)` registration. */
|
|
30
|
+
export interface HookEntry {
|
|
31
|
+
readonly id: number;
|
|
32
|
+
readonly mod: ModRef;
|
|
33
|
+
readonly pattern: string;
|
|
34
|
+
readonly matcher: unknown;
|
|
35
|
+
readonly fn: HookFn;
|
|
36
|
+
/** Registration order within the mod. */
|
|
37
|
+
readonly seq: number;
|
|
38
|
+
catchFn?: HookFn;
|
|
39
|
+
}
|
|
40
|
+
export type TraceOutcome = 'caught' | 'expired' | 'kept' | 'passed' | 'rejected' | 'returned' | 'skipped';
|
|
41
|
+
/** One link of a dispatch, as `next.trace` lists it. */
|
|
42
|
+
export interface TraceEntry {
|
|
43
|
+
readonly index: number;
|
|
44
|
+
readonly plugin: string;
|
|
45
|
+
readonly tier: Tier;
|
|
46
|
+
readonly event: string;
|
|
47
|
+
readonly outcome: TraceOutcome;
|
|
48
|
+
readonly reason?: string;
|
|
49
|
+
readonly ms: number;
|
|
50
|
+
readonly received: unknown;
|
|
51
|
+
readonly returned: unknown;
|
|
52
|
+
}
|
|
53
|
+
/** A hook run's frame: its budget, which `$` calls pause. */
|
|
54
|
+
export interface HookFrame {
|
|
55
|
+
readonly hook: HookEntry;
|
|
56
|
+
readonly event: string;
|
|
57
|
+
readonly origin: Origin;
|
|
58
|
+
readonly signal: AbortSignal;
|
|
59
|
+
/** True until the hook (or handler) settles; `$` calls after it are the mod's own. */
|
|
60
|
+
readonly active: boolean;
|
|
61
|
+
/** Stop the hook's clock while a `$` call is in flight; returns the resume. */
|
|
62
|
+
pause(): () => void;
|
|
63
|
+
}
|
|
64
|
+
/** What a dispatch needs from the engine. */
|
|
65
|
+
export interface DispatchRequest {
|
|
66
|
+
readonly event: string;
|
|
67
|
+
readonly input: unknown;
|
|
68
|
+
readonly origin: Origin;
|
|
69
|
+
/** The hooks the pattern selects, in chain order. */
|
|
70
|
+
readonly hooks: readonly HookEntry[];
|
|
71
|
+
/** Core: the host's own behavior, or the test's stubs. */
|
|
72
|
+
readonly bottom: (e: unknown, signal: AbortSignal, above: HookEntry | undefined) => Promise<unknown>;
|
|
73
|
+
/** What a hook's `next` resolves to, as that hook sees it (engine.create's table). */
|
|
74
|
+
readonly received?: (hook: HookEntry, value: unknown) => unknown;
|
|
75
|
+
/** The event a hook passes to `next`, as the next reader gets it (kept in step with what it received). */
|
|
76
|
+
readonly passing?: (received: unknown, passed: unknown) => unknown;
|
|
77
|
+
/** Each link that answered (returned or caught), as it settles: innermost first. */
|
|
78
|
+
readonly observe?: (hook: HookEntry, result: unknown) => void;
|
|
79
|
+
/** The `$` a hook receives for this run. */
|
|
80
|
+
readonly api: (frame: HookFrame) => unknown;
|
|
81
|
+
/** Whether a hook runs for this input (matcher and per-event rules). */
|
|
82
|
+
readonly applies: (hook: HookEntry, e: unknown) => boolean;
|
|
83
|
+
/** Hooks skipped because the event came from their own `$` call. */
|
|
84
|
+
readonly reentry?: (hook: HookEntry) => HookFailure['cause'] | boolean;
|
|
85
|
+
/** Why a result is the wrong shape for this event, or undefined. */
|
|
86
|
+
readonly validate?: (result: unknown, called: boolean) => string | undefined;
|
|
87
|
+
readonly report: (line: string) => void;
|
|
88
|
+
readonly signal?: AbortSignal;
|
|
89
|
+
readonly budgetMs?: number;
|
|
90
|
+
readonly catchMs?: number;
|
|
91
|
+
}
|
|
92
|
+
/** Whether `pattern` (a name, `ns.*`, `*`, or `!pattern`) selects `event`. */
|
|
93
|
+
export declare function selects(pattern: string, event: string): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Whether a matcher accepts a value: a scalar is `===`, a RegExp tests the
|
|
96
|
+
* value as a string, an array is any-of, an object a partial at any depth;
|
|
97
|
+
* against an array value a pattern holds when some element matches.
|
|
98
|
+
*/
|
|
99
|
+
export declare function matches(pattern: unknown, value: unknown): boolean;
|
|
100
|
+
/** Chain order: tier, then load order, then registration order. */
|
|
101
|
+
export declare function chainOrder(a: HookEntry, b: HookEntry): number;
|
|
102
|
+
/** Run one dispatch through its hooks and core. */
|
|
103
|
+
export declare function dispatch(request: DispatchRequest): Promise<unknown>;
|