@haven_ai/connect 0.1.29-alpha.0 → 0.1.30-alpha.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/README.md +92 -4
- package/dist/cli.cjs +2290 -1621
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +2273 -1604
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +2293 -1620
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +50 -4
- package/dist/index.d.ts +50 -4
- package/dist/index.js +2299 -1626
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/cli.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { mkdir, rm, stat, readdir, readFile, chmod, access, writeFile, unlink } from 'fs/promises';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import { Wallet } from 'ethers';
|
|
4
|
+
import { mkdir, rm, stat, readdir, readFile, chmod, access, writeFile, unlink, rename } from 'fs/promises';
|
|
6
5
|
import { homedir, platform } from 'os';
|
|
7
6
|
import { join, resolve, dirname, basename } from 'path';
|
|
7
|
+
import { ensureConsent, computeConsentHash, loadCredentials, consentInputFromClient, registeredToolNames, MCP_VERSION } from '@haven_ai/mcp';
|
|
8
|
+
import { ensureSignerConsent, computeSignerConsentHash, loadSignerCredentials, createEdgeSigner, toolSchemas } from '@haven_ai/signer';
|
|
9
|
+
import { isSupportedNodeVersion, SKILL_FOLDER_NAME, resolveTokenFromAddress, HAVEN_SKILL_MD, HAVEN_SKILL_BODY_MD, HAVEN_MINIMUM_NODE_VERSION, unsupportedNodeVersionMessage } from '@haven_ai/sdk';
|
|
8
10
|
import { parseDocument, isMap, stringify } from 'yaml';
|
|
9
11
|
import { execFile, spawn } from 'child_process';
|
|
10
12
|
import { promisify } from 'util';
|
|
11
13
|
import { realpathSync } from 'fs';
|
|
12
14
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
13
|
-
import crypto from 'crypto';
|
|
14
|
-
import { Wallet } from 'ethers';
|
|
15
15
|
import { createInterface } from 'readline';
|
|
16
16
|
|
|
17
17
|
var __defProp = Object.defineProperty;
|
|
@@ -24,6 +24,125 @@ var __export = (target, all) => {
|
|
|
24
24
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
// src/api.ts
|
|
28
|
+
function createConnectApiClient(baseUrl, fetchImpl = fetch) {
|
|
29
|
+
const root = baseUrl.replace(/\/+$/, "");
|
|
30
|
+
return {
|
|
31
|
+
resolveSetup: (input) => request(fetchImpl, `${root}/agent-connection-setups/resolve`, {
|
|
32
|
+
method: "POST",
|
|
33
|
+
body: JSON.stringify({
|
|
34
|
+
setup_token: input.setupToken,
|
|
35
|
+
connector_version: input.connectorVersion,
|
|
36
|
+
runtime: input.runtime
|
|
37
|
+
})
|
|
38
|
+
}),
|
|
39
|
+
registerSetup: (input) => request(fetchImpl, `${root}/agent-connection-setups/register`, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
body: JSON.stringify({
|
|
42
|
+
setup_token: input.setupToken,
|
|
43
|
+
challenge_id: input.challengeId,
|
|
44
|
+
delegate_address: input.delegateAddress,
|
|
45
|
+
proof_signature: input.proofSignature,
|
|
46
|
+
api_key_hash: input.apiKeyHash,
|
|
47
|
+
api_key_prefix: input.apiKeyPrefix,
|
|
48
|
+
runtime: input.runtime,
|
|
49
|
+
connector_version: input.connectorVersion,
|
|
50
|
+
mcp_server_name: input.mcpServerName,
|
|
51
|
+
connector_context: input.connectorContext,
|
|
52
|
+
install_capabilities: input.installCapabilities && {
|
|
53
|
+
can_write_runtime_config: input.installCapabilities.canWriteRuntimeConfig,
|
|
54
|
+
restart_required: input.installCapabilities.restartRequired
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
}),
|
|
58
|
+
getAgentIdentity: (apiKey) => request(fetchImpl, `${root}/machine-payments/agent`, {
|
|
59
|
+
method: "GET",
|
|
60
|
+
headers: { Authorization: `Bearer ${apiKey}` }
|
|
61
|
+
}),
|
|
62
|
+
getConnectorStatus: (setupId, apiKey) => request(fetchImpl, `${root}/agent-connection-setups/${encodeURIComponent(setupId)}/connector-status`, {
|
|
63
|
+
method: "GET",
|
|
64
|
+
headers: { Authorization: `Bearer ${apiKey}` }
|
|
65
|
+
}),
|
|
66
|
+
updateInstallStatus: async (setupId, apiKey, input) => {
|
|
67
|
+
await request(fetchImpl, `${root}/agent-connection-setups/${encodeURIComponent(setupId)}/install-status`, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
70
|
+
body: JSON.stringify({
|
|
71
|
+
runtime: input.runtime,
|
|
72
|
+
connector_version: input.connectorVersion,
|
|
73
|
+
runtime_mcp_mode: input.runtimeMcpMode,
|
|
74
|
+
hosted_mcp_configured: input.hostedMcpConfigured,
|
|
75
|
+
local_signer_configured: input.localSignerConfigured,
|
|
76
|
+
local_mcp_configured: input.localMcpConfigured,
|
|
77
|
+
credential_files_written: input.credentialFilesWritten,
|
|
78
|
+
signer_acknowledged: input.signerAcknowledged,
|
|
79
|
+
local_mcp_acknowledged: input.localMcpAcknowledged,
|
|
80
|
+
activation_command_available: input.activationCommandAvailable,
|
|
81
|
+
skill_installed: input.skillInstalled,
|
|
82
|
+
probe_result: input.probeResult,
|
|
83
|
+
restart_required: input.restartRequired,
|
|
84
|
+
next_user_action: input.nextUserAction,
|
|
85
|
+
error_code: input.errorCode ?? null,
|
|
86
|
+
environment_label: input.environmentLabel
|
|
87
|
+
})
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
async function request(fetchImpl, url, init) {
|
|
93
|
+
const response = await fetchImpl(url, {
|
|
94
|
+
...init,
|
|
95
|
+
headers: {
|
|
96
|
+
"Content-Type": "application/json",
|
|
97
|
+
...init.headers ?? {}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
const text = await response.text();
|
|
101
|
+
const body = text ? JSON.parse(text) : null;
|
|
102
|
+
if (!response.ok) {
|
|
103
|
+
const message = body?.error ?? body?.message ?? `${response.status} ${response.statusText}`;
|
|
104
|
+
throw new ConnectRequestError(`Haven setup request failed: ${message}`, response.status);
|
|
105
|
+
}
|
|
106
|
+
return body;
|
|
107
|
+
}
|
|
108
|
+
var ConnectRequestError;
|
|
109
|
+
var init_api = __esm({
|
|
110
|
+
"src/api.ts"() {
|
|
111
|
+
ConnectRequestError = class extends Error {
|
|
112
|
+
constructor(message, status) {
|
|
113
|
+
super(message);
|
|
114
|
+
this.status = status;
|
|
115
|
+
this.name = "ConnectRequestError";
|
|
116
|
+
}
|
|
117
|
+
status;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
function generateDelegateKey() {
|
|
122
|
+
return delegateKeyFromPrivateKey(Wallet.createRandom().privateKey);
|
|
123
|
+
}
|
|
124
|
+
function delegateKeyFromPrivateKey(privateKey) {
|
|
125
|
+
const wallet = new Wallet(privateKey);
|
|
126
|
+
return {
|
|
127
|
+
privateKey: wallet.privateKey,
|
|
128
|
+
address: wallet.address,
|
|
129
|
+
signChallenge: (message) => wallet.signMessage(message)
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function generateAgentApiKey() {
|
|
133
|
+
return `sk_agent_${crypto.randomBytes(24).toString("hex")}`;
|
|
134
|
+
}
|
|
135
|
+
function hashAgentApiKey(apiKey) {
|
|
136
|
+
return crypto.createHash("sha256").update(apiKey).digest("hex");
|
|
137
|
+
}
|
|
138
|
+
function agentApiKeyPrefix(apiKey) {
|
|
139
|
+
return apiKey.slice(0, 12);
|
|
140
|
+
}
|
|
141
|
+
var init_key = __esm({
|
|
142
|
+
"src/key.ts"() {
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
27
146
|
// src/redact.ts
|
|
28
147
|
function redactSecrets(value) {
|
|
29
148
|
return value.replace(API_KEY_RE, "sk_agent_[redacted]").replace(PRIVATE_KEY_RE, "0x[redacted-private-key]");
|
|
@@ -86,121 +205,417 @@ var init_server_names = __esm({
|
|
|
86
205
|
SLUG_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
87
206
|
}
|
|
88
207
|
});
|
|
89
|
-
function
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
208
|
+
async function preflightCredentialStorage(input = {}) {
|
|
209
|
+
const directory = defaultCredentialRoot(input.baseDir);
|
|
210
|
+
await mkdir(directory, { recursive: true, mode: 448 });
|
|
211
|
+
await restrictPermissions(directory, 448, input.warn);
|
|
212
|
+
const probePath = join(directory, `.haven-connect-preflight-${crypto.randomBytes(8).toString("hex")}`);
|
|
213
|
+
try {
|
|
214
|
+
await writeOwnerOnlyJson(probePath, { ok: true }, input.warn);
|
|
215
|
+
} finally {
|
|
216
|
+
await rm(probePath, { force: true }).catch(() => void 0);
|
|
217
|
+
}
|
|
218
|
+
return directory;
|
|
97
219
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
minimumNodeVersion: HAVEN_MINIMUM_NODE_VERSION,
|
|
115
|
-
supportedClients: ["codex-cli", "codex-desktop", "claude-code"],
|
|
116
|
-
requiredTools: registeredToolNames(),
|
|
117
|
-
/**
|
|
118
|
-
* The signer MCP's tool surface, DERIVED from the pinned @haven_ai/signer
|
|
119
|
-
* package (#1587) — same anti-drift rule as `requiredTools` above: a
|
|
120
|
-
* literal list here would rot the first time the signer gains a tool.
|
|
121
|
-
* The handshake probe requires all of them.
|
|
122
|
-
*/
|
|
123
|
-
requiredSignerTools: Object.keys(toolSchemas)
|
|
124
|
-
};
|
|
220
|
+
async function writeCredentialFiles(input) {
|
|
221
|
+
const directory = defaultAgentDirectory(input.serverName ?? input.agentId, input.baseDir);
|
|
222
|
+
await mkdir(directory, { recursive: true, mode: 448 });
|
|
223
|
+
await restrictPermissions(directory, 448, input.warn);
|
|
224
|
+
const identityPath = join(directory, "identity.json");
|
|
225
|
+
const signerPath = join(directory, "signer.json");
|
|
226
|
+
const agentPath = join(directory, "agent.json");
|
|
227
|
+
await assertDoesNotExist(identityPath);
|
|
228
|
+
await assertDoesNotExist(signerPath);
|
|
229
|
+
await assertDoesNotExist(agentPath);
|
|
230
|
+
await writeOwnerOnlyJson(signerPath, signerPayload(input), input.warn);
|
|
231
|
+
try {
|
|
232
|
+
await writeOwnerOnlyJson(identityPath, identityPayload(input), input.warn);
|
|
233
|
+
} catch (err) {
|
|
234
|
+
await rm(signerPath, { force: true }).catch(() => void 0);
|
|
235
|
+
throw err;
|
|
125
236
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return writeJsonRuntimeConfig(input, cursorConfigPath(input.homeDir), "mcpServers");
|
|
134
|
-
case "vscode":
|
|
135
|
-
return writeJsonRuntimeConfig(input, vscodeConfigPath(input.homeDir), "servers");
|
|
136
|
-
case "vscode-insiders":
|
|
137
|
-
return writeJsonRuntimeConfig(input, vscodeInsidersConfigPath(input.homeDir), "servers");
|
|
138
|
-
case "claude-desktop":
|
|
139
|
-
return writeJsonRuntimeConfig(input, claudeDesktopConfigPath(input.homeDir), "mcpServers");
|
|
140
|
-
case "hermes":
|
|
141
|
-
return writeHermesConfig(input, deps);
|
|
142
|
-
default:
|
|
143
|
-
return {
|
|
144
|
-
hostedConfigured: false,
|
|
145
|
-
signerConfigured: false,
|
|
146
|
-
localMcpConfigured: false,
|
|
147
|
-
runtimeMcpMode: "manual",
|
|
148
|
-
target: "manual runtime setup",
|
|
149
|
-
changed: false,
|
|
150
|
-
restartRequired: true,
|
|
151
|
-
messages: ["Runtime config needs to be added manually for this agent environment."],
|
|
152
|
-
errorCode: "manual_runtime_setup_required"
|
|
153
|
-
};
|
|
237
|
+
try {
|
|
238
|
+
await writeOwnerOnlyJson(agentPath, agentPayload(input), input.warn);
|
|
239
|
+
} catch (err) {
|
|
240
|
+
await rm(signerPath, { force: true }).catch(() => void 0);
|
|
241
|
+
await rm(identityPath, { force: true }).catch(() => void 0);
|
|
242
|
+
await rm(agentPath, { force: true }).catch(() => void 0);
|
|
243
|
+
throw err;
|
|
154
244
|
}
|
|
245
|
+
return { directory, identityPath, signerPath, agentPath };
|
|
155
246
|
}
|
|
156
|
-
function
|
|
157
|
-
if (runtime === "vscode" || runtime === "vscode-insiders") {
|
|
158
|
-
return {
|
|
159
|
-
type: "http",
|
|
160
|
-
url: hostedMcpUrl,
|
|
161
|
-
headers: { Authorization: `Bearer ${apiKey}` }
|
|
162
|
-
};
|
|
163
|
-
}
|
|
247
|
+
function signerPayload(input) {
|
|
164
248
|
return {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
249
|
+
delegate_key: input.delegateKey,
|
|
250
|
+
delegate_address: input.delegateAddress,
|
|
251
|
+
agent_id: input.agentId,
|
|
252
|
+
safe_address: input.safeAddress,
|
|
253
|
+
chain_id: input.chainId,
|
|
254
|
+
network: input.network,
|
|
255
|
+
x402_binding_signer: input.x402BindingSigner,
|
|
256
|
+
note: "Local signer credential. Haven backend never receives this private key."
|
|
173
257
|
};
|
|
174
258
|
}
|
|
175
|
-
function
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
259
|
+
function identityPayload(input) {
|
|
260
|
+
return {
|
|
261
|
+
api_key: input.apiKey,
|
|
262
|
+
agent_id: input.agentId,
|
|
263
|
+
safe_address: input.safeAddress,
|
|
264
|
+
chain_id: input.chainId,
|
|
265
|
+
network: input.network,
|
|
266
|
+
api_url: input.apiUrl,
|
|
267
|
+
hosted_mcp_url: input.hostedMcpUrl,
|
|
268
|
+
agent_budget: input.agentBudget,
|
|
269
|
+
note: "Haven API key identifies the agent only. It cannot spend without the local signer key and on-chain Haven wallet rules."
|
|
179
270
|
};
|
|
180
|
-
if (runtime === "vscode" || runtime === "vscode-insiders") return { type: "stdio", ...server };
|
|
181
|
-
return server;
|
|
182
271
|
}
|
|
183
|
-
function
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
272
|
+
function agentPayload(input) {
|
|
273
|
+
return {
|
|
274
|
+
agent_id: input.agentId,
|
|
275
|
+
delegate_address: input.delegateAddress,
|
|
276
|
+
safe_address: input.safeAddress,
|
|
277
|
+
chain_id: input.chainId,
|
|
278
|
+
network: input.network,
|
|
279
|
+
agent_budget: input.agentBudget,
|
|
280
|
+
note: "Non-secret orientation for the agent: public delegate/Haven wallet identity + configured budget. Contains no API key or signing key. For the live remaining budget, call haven_get_allowances."
|
|
191
281
|
};
|
|
192
|
-
return `${JSON.stringify(config, null, 2)}
|
|
193
|
-
`;
|
|
194
282
|
}
|
|
195
|
-
function
|
|
196
|
-
|
|
197
|
-
|
|
283
|
+
async function readStoredCredentials(serverName, agentIdOrSlug, baseDir) {
|
|
284
|
+
const key = serverName ?? agentIdOrSlug;
|
|
285
|
+
const directory = key ? defaultAgentDirectory(key, baseDir) : await discoverSoleAgentDirectory(baseDir);
|
|
286
|
+
const identity = await readJsonFile(join(directory, "identity.json"));
|
|
287
|
+
if (!identity) {
|
|
288
|
+
throw new Error(
|
|
289
|
+
`No Haven credentials at ${directory}. Nothing to re-key \u2014 connect this agent first, or pass the --name you wired it under.`
|
|
290
|
+
);
|
|
198
291
|
}
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
292
|
+
const agent = await readJsonFile(join(directory, "agent.json")) ?? {};
|
|
293
|
+
const signer = await readJsonFile(join(directory, "signer.json")) ?? {};
|
|
294
|
+
const agentId = asString(identity.agent_id);
|
|
295
|
+
const apiKey = asString(identity.api_key);
|
|
296
|
+
const apiUrl = asString(identity.api_url);
|
|
297
|
+
const hostedMcpUrl = asString(identity.hosted_mcp_url);
|
|
298
|
+
if (!agentId || !apiKey || !apiUrl || !hostedMcpUrl) {
|
|
299
|
+
throw new Error(
|
|
300
|
+
`The credential set at ${directory} is incomplete (identity.json is missing agent_id, api_key, api_url or hosted_mcp_url). Re-key cannot rebuild it \u2014 reconnect the agent instead.`
|
|
301
|
+
);
|
|
202
302
|
}
|
|
203
|
-
|
|
303
|
+
return {
|
|
304
|
+
directory,
|
|
305
|
+
agentId,
|
|
306
|
+
apiKey,
|
|
307
|
+
apiUrl,
|
|
308
|
+
hostedMcpUrl,
|
|
309
|
+
delegateAddress: asString(agent.delegate_address) ?? asString(signer.delegate_address),
|
|
310
|
+
safeAddress: asString(identity.safe_address) ?? asString(agent.safe_address),
|
|
311
|
+
chainId: typeof identity.chain_id === "number" ? identity.chain_id : void 0,
|
|
312
|
+
network: asString(identity.network),
|
|
313
|
+
x402BindingSigner: asString(signer.x402_binding_signer),
|
|
314
|
+
agentBudget: Array.isArray(identity.agent_budget) ? identity.agent_budget : void 0
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
async function discoverSoleAgentDirectory(baseDir) {
|
|
318
|
+
const root = defaultCredentialRoot(baseDir);
|
|
319
|
+
let entries = [];
|
|
320
|
+
try {
|
|
321
|
+
entries = await readdir(root);
|
|
322
|
+
} catch {
|
|
323
|
+
throw new Error(`No Haven credentials found under ${root}. Connect an agent on this machine first.`);
|
|
324
|
+
}
|
|
325
|
+
const candidates = [];
|
|
326
|
+
for (const entry of entries) {
|
|
327
|
+
const directory = join(root, entry);
|
|
328
|
+
if (!await readJsonFile(join(directory, "identity.json"))) continue;
|
|
329
|
+
if (await readJsonFile(join(directory, "TOMBSTONE.json"))) continue;
|
|
330
|
+
candidates.push(directory);
|
|
331
|
+
}
|
|
332
|
+
if (candidates.length === 1) return candidates[0];
|
|
333
|
+
if (candidates.length === 0) {
|
|
334
|
+
throw new Error(`No Haven credentials found under ${root}. Connect an agent on this machine first.`);
|
|
335
|
+
}
|
|
336
|
+
throw new Error(
|
|
337
|
+
`Several agents are wired on this machine, so --rekey cannot tell which one you mean:
|
|
338
|
+
` + candidates.map((d) => ` ${d}`).join("\n") + "\nRe-run with --name <slug> to pick one."
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
async function rewriteCredentialFiles(input) {
|
|
342
|
+
const directory = defaultAgentDirectory(input.serverName ?? input.agentId, input.baseDir);
|
|
343
|
+
const identityPath = join(directory, "identity.json");
|
|
344
|
+
const signerPath = join(directory, "signer.json");
|
|
345
|
+
const agentPath = join(directory, "agent.json");
|
|
346
|
+
const targets = [
|
|
347
|
+
{ path: signerPath, payload: signerPayload(input) },
|
|
348
|
+
{ path: identityPath, payload: identityPayload(input) },
|
|
349
|
+
{ path: agentPath, payload: agentPayload(input) }
|
|
350
|
+
];
|
|
351
|
+
const originals = /* @__PURE__ */ new Map();
|
|
352
|
+
for (const { path } of targets) {
|
|
353
|
+
originals.set(path, await readRawFile(path));
|
|
354
|
+
}
|
|
355
|
+
const temps = [];
|
|
356
|
+
try {
|
|
357
|
+
for (const { path, payload } of targets) {
|
|
358
|
+
const temp = `${path}.rekey-${crypto.randomBytes(6).toString("hex")}.tmp`;
|
|
359
|
+
await writeOwnerOnlyJson(temp, payload, input.warn);
|
|
360
|
+
temps.push({ from: temp, to: path });
|
|
361
|
+
}
|
|
362
|
+
for (const { from, to } of temps) {
|
|
363
|
+
await rename(from, to);
|
|
364
|
+
}
|
|
365
|
+
} catch (err) {
|
|
366
|
+
for (const { from } of temps) await rm(from, { force: true }).catch(() => void 0);
|
|
367
|
+
for (const [path, contents] of originals) {
|
|
368
|
+
if (contents === null) {
|
|
369
|
+
await rm(path, { force: true }).catch(() => void 0);
|
|
370
|
+
} else {
|
|
371
|
+
await writeFile(path, contents, { mode: 384 }).catch(() => void 0);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
throw err;
|
|
375
|
+
}
|
|
376
|
+
return { directory, identityPath, signerPath, agentPath };
|
|
377
|
+
}
|
|
378
|
+
async function writeRekeyPending(directory, pending, warn) {
|
|
379
|
+
const path = join(directory, REKEY_PENDING_FILENAME);
|
|
380
|
+
await rm(path, { force: true }).catch(() => void 0);
|
|
381
|
+
await writeOwnerOnlyJson(path, { ...pending }, warn);
|
|
382
|
+
return path;
|
|
383
|
+
}
|
|
384
|
+
async function readRekeyPending(directory, now = Date.now()) {
|
|
385
|
+
const path = join(directory, REKEY_PENDING_FILENAME);
|
|
386
|
+
const raw = await readJsonFile(path);
|
|
387
|
+
if (!raw) {
|
|
388
|
+
throw new Error(
|
|
389
|
+
`No re-key in progress at ${directory}. Run the connector with --rekey first \u2014 it prints the new signing address to paste into the Haven agent page.`
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
const pending = raw;
|
|
393
|
+
if (!pending.new_delegate_key || !pending.new_delegate_address || !pending.agent_id) {
|
|
394
|
+
throw new Error(`The pending re-key at ${path} is unreadable. Delete it and start again with --rekey.`);
|
|
395
|
+
}
|
|
396
|
+
if (pending.expires_at && Date.parse(pending.expires_at) < now) {
|
|
397
|
+
throw new Error(
|
|
398
|
+
`The re-key started at ${pending.started_at} has expired. Start again with --rekey \u2014 the address currently shown in your dashboard is no longer the one this machine holds.`
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
return pending;
|
|
402
|
+
}
|
|
403
|
+
async function inspectRekeyPending(directory, now = Date.now()) {
|
|
404
|
+
const path = join(directory, REKEY_PENDING_FILENAME);
|
|
405
|
+
const raw = await readJsonFile(path);
|
|
406
|
+
if (!raw) {
|
|
407
|
+
const present = await readRawFile(path) !== null;
|
|
408
|
+
return present ? { state: "unreadable", path } : null;
|
|
409
|
+
}
|
|
410
|
+
const agentId = asString(raw.agent_id);
|
|
411
|
+
const newDelegateAddress = asString(raw.new_delegate_address);
|
|
412
|
+
const startedAt = asString(raw.started_at);
|
|
413
|
+
const expiresAt = asString(raw.expires_at);
|
|
414
|
+
if (!agentId || !newDelegateAddress) {
|
|
415
|
+
return { state: "unreadable", path, ...startedAt ? { startedAt } : {} };
|
|
416
|
+
}
|
|
417
|
+
const expired = expiresAt !== void 0 && Date.parse(expiresAt) < now;
|
|
418
|
+
return {
|
|
419
|
+
state: expired ? "expired" : "pending",
|
|
420
|
+
path,
|
|
421
|
+
agentId,
|
|
422
|
+
newDelegateAddress,
|
|
423
|
+
...startedAt ? { startedAt } : {},
|
|
424
|
+
...expiresAt ? { expiresAt } : {}
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
async function clearRekeyPending(directory) {
|
|
428
|
+
await rm(join(directory, REKEY_PENDING_FILENAME), { force: true }).catch(() => void 0);
|
|
429
|
+
}
|
|
430
|
+
async function readJsonFile(path) {
|
|
431
|
+
const raw = await readRawFile(path);
|
|
432
|
+
if (raw === null) return null;
|
|
433
|
+
try {
|
|
434
|
+
const parsed = JSON.parse(raw);
|
|
435
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : null;
|
|
436
|
+
} catch {
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
async function readRawFile(path) {
|
|
441
|
+
try {
|
|
442
|
+
return await readFile(path, "utf8");
|
|
443
|
+
} catch {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function asString(value) {
|
|
448
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
449
|
+
}
|
|
450
|
+
async function assertServerSlugAvailable(serverName, baseDir) {
|
|
451
|
+
const directory = defaultAgentDirectory(serverName, baseDir);
|
|
452
|
+
try {
|
|
453
|
+
await stat(join(directory, "identity.json"));
|
|
454
|
+
} catch {
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
throw new Error(
|
|
458
|
+
`The name "${serverName}" is already wired on this machine (${directory} holds credentials). Pick a different --name, or revoke and remove that agent first \u2014 connect never overwrites credentials.`
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
function defaultAgentDirectory(agentId, baseDir = join(homedir(), ".haven", "agents")) {
|
|
462
|
+
return resolve(defaultCredentialRoot(baseDir), safePathPart(agentId));
|
|
463
|
+
}
|
|
464
|
+
function defaultCredentialRoot(baseDir = join(homedir(), ".haven", "agents")) {
|
|
465
|
+
return resolve(baseDir);
|
|
466
|
+
}
|
|
467
|
+
async function writeOwnerOnlyJson(path, value, warn) {
|
|
468
|
+
const json = JSON.stringify(dropUndefined(value), null, 2);
|
|
469
|
+
await writeFile(path, `${json}
|
|
470
|
+
`, { mode: 384, flag: "wx" });
|
|
471
|
+
await restrictPermissions(path, 384, warn);
|
|
472
|
+
}
|
|
473
|
+
function safePathPart(value) {
|
|
474
|
+
return value.replace(/[^A-Za-z0-9_.-]/g, "_");
|
|
475
|
+
}
|
|
476
|
+
function dropUndefined(value) {
|
|
477
|
+
return Object.fromEntries(Object.entries(value).filter(([, field]) => field !== void 0));
|
|
478
|
+
}
|
|
479
|
+
async function assertDoesNotExist(path) {
|
|
480
|
+
try {
|
|
481
|
+
await access(path);
|
|
482
|
+
} catch (err) {
|
|
483
|
+
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") return;
|
|
484
|
+
throw err;
|
|
485
|
+
}
|
|
486
|
+
throw new Error(`Refusing to overwrite existing Haven credential file: ${path}`);
|
|
487
|
+
}
|
|
488
|
+
async function restrictPermissions(path, mode, warn) {
|
|
489
|
+
try {
|
|
490
|
+
await chmod(path, mode);
|
|
491
|
+
} catch (err) {
|
|
492
|
+
warn?.(
|
|
493
|
+
`Warning: could not restrict permissions on ${path} to ${mode.toString(8)}. Move this credential to a private location or run chmod ${mode.toString(8)} ${path}. ${err instanceof Error ? err.message : String(err)}`
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
var REKEY_PENDING_FILENAME, REKEY_PENDING_TTL_MS;
|
|
498
|
+
var init_storage = __esm({
|
|
499
|
+
"src/storage.ts"() {
|
|
500
|
+
REKEY_PENDING_FILENAME = "rekey-pending.json";
|
|
501
|
+
REKEY_PENDING_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
function mcpPackageSpec() {
|
|
505
|
+
return `${MCP_RUNTIME_MANIFEST.mcpPackage}@${MCP_RUNTIME_MANIFEST.mcpVersion}`;
|
|
506
|
+
}
|
|
507
|
+
function sdkPackageSpec() {
|
|
508
|
+
return `${MCP_RUNTIME_MANIFEST.sdkPackage}@${MCP_RUNTIME_MANIFEST.sdkVersion}`;
|
|
509
|
+
}
|
|
510
|
+
function signerPackageSpec() {
|
|
511
|
+
return `${MCP_RUNTIME_MANIFEST.signerPackage}@${MCP_RUNTIME_MANIFEST.signerVersion}`;
|
|
512
|
+
}
|
|
513
|
+
var MCP_RUNTIME_MANIFEST;
|
|
514
|
+
var init_runtime_manifest = __esm({
|
|
515
|
+
"src/runtime-manifest.ts"() {
|
|
516
|
+
MCP_RUNTIME_MANIFEST = {
|
|
517
|
+
mcpPackage: "@haven_ai/mcp",
|
|
518
|
+
mcpVersion: MCP_VERSION,
|
|
519
|
+
sdkPackage: "@haven_ai/sdk",
|
|
520
|
+
sdkVersion: "0.1.30-alpha.0",
|
|
521
|
+
signerPackage: "@haven_ai/signer",
|
|
522
|
+
signerVersion: "0.1.30-alpha.0",
|
|
523
|
+
// Sourced from the SDK, never a literal (#1161). This field read '20.0.0'
|
|
524
|
+
// while every package's `engines` said `>=24` and the docs said `>=24.0.0`,
|
|
525
|
+
// so the guard that was supposed to enforce the floor waved Node v23 through
|
|
526
|
+
// — including on the `--local` path where it does run. A hand-maintained
|
|
527
|
+
// second copy of a number is a drift waiting to happen; a guard test pins
|
|
528
|
+
// this against `package.json`'s `engines.node`.
|
|
529
|
+
minimumNodeVersion: HAVEN_MINIMUM_NODE_VERSION,
|
|
530
|
+
supportedClients: ["codex-cli", "codex-desktop", "claude-code"],
|
|
531
|
+
requiredTools: registeredToolNames(),
|
|
532
|
+
/**
|
|
533
|
+
* The signer MCP's tool surface, DERIVED from the pinned @haven_ai/signer
|
|
534
|
+
* package (#1587) — same anti-drift rule as `requiredTools` above: a
|
|
535
|
+
* literal list here would rot the first time the signer gains a tool.
|
|
536
|
+
* The handshake probe requires all of them.
|
|
537
|
+
*/
|
|
538
|
+
requiredSignerTools: Object.keys(toolSchemas)
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
async function writeRuntimeConfig(input, deps = {}) {
|
|
543
|
+
switch (input.runtime) {
|
|
544
|
+
case "codex-cli":
|
|
545
|
+
case "codex-desktop":
|
|
546
|
+
return writeCodexConfig(input);
|
|
547
|
+
case "cursor":
|
|
548
|
+
return writeJsonRuntimeConfig(input, cursorConfigPath(input.homeDir), "mcpServers");
|
|
549
|
+
case "vscode":
|
|
550
|
+
return writeJsonRuntimeConfig(input, vscodeConfigPath(input.homeDir), "servers");
|
|
551
|
+
case "vscode-insiders":
|
|
552
|
+
return writeJsonRuntimeConfig(input, vscodeInsidersConfigPath(input.homeDir), "servers");
|
|
553
|
+
case "claude-desktop":
|
|
554
|
+
return writeJsonRuntimeConfig(input, claudeDesktopConfigPath(input.homeDir), "mcpServers");
|
|
555
|
+
case "hermes":
|
|
556
|
+
return writeHermesConfig(input, deps);
|
|
557
|
+
default:
|
|
558
|
+
return {
|
|
559
|
+
hostedConfigured: false,
|
|
560
|
+
signerConfigured: false,
|
|
561
|
+
localMcpConfigured: false,
|
|
562
|
+
runtimeMcpMode: "manual",
|
|
563
|
+
target: "manual runtime setup",
|
|
564
|
+
changed: false,
|
|
565
|
+
restartRequired: true,
|
|
566
|
+
messages: ["Runtime config needs to be added manually for this agent environment."],
|
|
567
|
+
errorCode: "manual_runtime_setup_required"
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
function buildHostedServer(hostedMcpUrl, apiKey, runtime) {
|
|
572
|
+
if (runtime === "vscode" || runtime === "vscode-insiders") {
|
|
573
|
+
return {
|
|
574
|
+
type: "http",
|
|
575
|
+
url: hostedMcpUrl,
|
|
576
|
+
headers: { Authorization: `Bearer ${apiKey}` }
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
return {
|
|
580
|
+
url: hostedMcpUrl,
|
|
581
|
+
headers: { Authorization: `Bearer ${apiKey}` }
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
function resolveSignerLaunchSpec(input) {
|
|
585
|
+
return input.signerCommand ?? {
|
|
586
|
+
command: "npx",
|
|
587
|
+
args: ["-y", signerPackageSpec(), "--credentials", input.signerPath]
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
function buildSignerServer(spec, runtime) {
|
|
591
|
+
const server = {
|
|
592
|
+
command: spec.command,
|
|
593
|
+
args: spec.args
|
|
594
|
+
};
|
|
595
|
+
if (runtime === "vscode" || runtime === "vscode-insiders") return { type: "stdio", ...server };
|
|
596
|
+
return server;
|
|
597
|
+
}
|
|
598
|
+
function mergeJsonMcpConfig(existingJson, serverRoot, hostedServer, signerServer, names = serverNamesFor(), configPath) {
|
|
599
|
+
const config = existingJson?.trim() ? parseJsonObject(existingJson, configPath) : {};
|
|
600
|
+
const existingRoot = config[serverRoot];
|
|
601
|
+
const servers = existingRoot && typeof existingRoot === "object" && !Array.isArray(existingRoot) ? existingRoot : {};
|
|
602
|
+
config[serverRoot] = {
|
|
603
|
+
...servers,
|
|
604
|
+
[names.hosted]: hostedServer,
|
|
605
|
+
[names.signer]: signerServer
|
|
606
|
+
};
|
|
607
|
+
return `${JSON.stringify(config, null, 2)}
|
|
608
|
+
`;
|
|
609
|
+
}
|
|
610
|
+
function mergeHermesYaml(existingYaml, hostedServer, signerServer, names = serverNamesFor(), configPath) {
|
|
611
|
+
if (!existingYaml?.trim()) {
|
|
612
|
+
return renderHermesYaml({ [names.hosted]: hostedServer, [names.signer]: signerServer });
|
|
613
|
+
}
|
|
614
|
+
const doc = parseDocument(existingYaml, { keepSourceTokens: true });
|
|
615
|
+
if (doc.errors.length > 0 || !isMap(doc.contents)) {
|
|
616
|
+
throw new UnreadableRuntimeConfigError(configPath ?? "the Hermes config", "it is not a YAML object");
|
|
617
|
+
}
|
|
618
|
+
const mcpPair = doc.contents.items.find((item) => item.key?.toString() === "mcp_servers");
|
|
204
619
|
const existingServers = mcpPair && isMap(mcpPair.value) ? mcpPair.value.toJSON() : {};
|
|
205
620
|
const servers = isRecord(existingServers) ? existingServers : {};
|
|
206
621
|
const mergedServers = {
|
|
@@ -845,24 +1260,104 @@ var init_config_writers = __esm({
|
|
|
845
1260
|
};
|
|
846
1261
|
}
|
|
847
1262
|
});
|
|
848
|
-
async function
|
|
849
|
-
let response;
|
|
1263
|
+
async function acknowledgeLocalMcpConsent(identityPath, signerPath, log) {
|
|
850
1264
|
try {
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
1265
|
+
const input = await buildLocalMcpConsentInput(identityPath, signerPath);
|
|
1266
|
+
const decision = await ensureConsent(input, {
|
|
1267
|
+
credentialsPath: identityPath,
|
|
1268
|
+
writeAck: true,
|
|
1269
|
+
out: log ? { write: (chunk) => writeLogChunk(log, chunk) } : void 0
|
|
854
1270
|
});
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
1271
|
+
return {
|
|
1272
|
+
acknowledged: decision.ok,
|
|
1273
|
+
hash: decision.hash,
|
|
1274
|
+
reason: decision.reason
|
|
1275
|
+
};
|
|
1276
|
+
} catch (err) {
|
|
1277
|
+
return {
|
|
1278
|
+
acknowledged: false,
|
|
1279
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1280
|
+
};
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
async function getLocalMcpConsentStatus(identityPath, signerPath) {
|
|
1284
|
+
try {
|
|
1285
|
+
const input = await buildLocalMcpConsentInput(identityPath, signerPath);
|
|
1286
|
+
const hash = computeConsentHash(input);
|
|
1287
|
+
const stored = await readLocalMcpAckFile(localMcpAckPath(identityPath));
|
|
1288
|
+
if (stored === hash) {
|
|
1289
|
+
return { acknowledged: true, hash, reason: "ack_file_match" };
|
|
1290
|
+
}
|
|
1291
|
+
return {
|
|
1292
|
+
acknowledged: false,
|
|
1293
|
+
hash,
|
|
1294
|
+
reason: stored ? "ack_file_mismatch" : "ack_file_missing"
|
|
1295
|
+
};
|
|
1296
|
+
} catch (err) {
|
|
1297
|
+
return {
|
|
1298
|
+
acknowledged: false,
|
|
1299
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
function localMcpAckPath(identityPath) {
|
|
1304
|
+
return resolve(`${identityPath}.ack.json`);
|
|
1305
|
+
}
|
|
1306
|
+
async function buildLocalMcpConsentInput(identityPath, signerPath) {
|
|
1307
|
+
const credentials = await loadCredentials({ identityPath, signerPath });
|
|
1308
|
+
const unavailableDuringSetup = {
|
|
1309
|
+
getAllowances: async () => {
|
|
1310
|
+
throw new Error("Haven approval is not complete yet.");
|
|
1311
|
+
}
|
|
1312
|
+
};
|
|
1313
|
+
return consentInputFromClient(
|
|
1314
|
+
unavailableDuringSetup,
|
|
1315
|
+
{
|
|
1316
|
+
apiKey: credentials.apiKey,
|
|
1317
|
+
apiUrl: credentials.apiUrl,
|
|
1318
|
+
agentId: credentials.agentId,
|
|
1319
|
+
safeAddress: credentials.safeAddress,
|
|
1320
|
+
delegateAddress: credentials.delegateAddress,
|
|
1321
|
+
chainId: credentials.chainId,
|
|
1322
|
+
allowanceSummary: credentials.allowanceSummary
|
|
1323
|
+
},
|
|
1324
|
+
registeredToolNames()
|
|
1325
|
+
);
|
|
1326
|
+
}
|
|
1327
|
+
async function readLocalMcpAckFile(path) {
|
|
1328
|
+
try {
|
|
1329
|
+
const parsed = JSON.parse(await readFile(path, "utf8"));
|
|
1330
|
+
return typeof parsed.ack === "string" ? parsed.ack : null;
|
|
1331
|
+
} catch {
|
|
1332
|
+
return null;
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
function writeLogChunk(log, chunk) {
|
|
1336
|
+
const message = String(chunk).trimEnd();
|
|
1337
|
+
if (message) log(message);
|
|
1338
|
+
}
|
|
1339
|
+
var init_local_mcp_consent = __esm({
|
|
1340
|
+
"src/local-mcp-consent.ts"() {
|
|
1341
|
+
}
|
|
1342
|
+
});
|
|
1343
|
+
async function probeHostedAgentIdentity(apiKey, apiUrl, fetchImpl = fetch) {
|
|
1344
|
+
let response;
|
|
1345
|
+
try {
|
|
1346
|
+
response = await fetchWithTimeout(fetchImpl, `${apiUrl.replace(/\/+$/, "")}/machine-payments/agent`, {
|
|
1347
|
+
method: "GET",
|
|
1348
|
+
headers: { Authorization: `Bearer ${apiKey}`, Accept: "application/json" }
|
|
1349
|
+
});
|
|
1350
|
+
} catch {
|
|
1351
|
+
return { status: "network_error" };
|
|
1352
|
+
}
|
|
1353
|
+
if (response.status === 401 || response.status === 403) return { status: "unauthorized" };
|
|
1354
|
+
if (!response.ok) return { status: "bad_response" };
|
|
1355
|
+
try {
|
|
1356
|
+
const payload = JSON.parse(await response.text());
|
|
1357
|
+
if (typeof payload?.delegate_address !== "string") return { status: "bad_response" };
|
|
1358
|
+
return { status: "ok", agentId: payload.id, delegateAddress: payload.delegate_address };
|
|
1359
|
+
} catch {
|
|
1360
|
+
return { status: "bad_response" };
|
|
866
1361
|
}
|
|
867
1362
|
}
|
|
868
1363
|
async function probeHostedMcpTools(apiKey, hostedMcpUrl, fetchImpl = fetch) {
|
|
@@ -1160,79 +1655,355 @@ var init_signer_runtime = __esm({
|
|
|
1160
1655
|
SIGNER_INSTALL_HEARTBEAT_MS = 15e3;
|
|
1161
1656
|
}
|
|
1162
1657
|
});
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
"
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1658
|
+
async function prepareLocalMcpRuntime(input, deps = {}) {
|
|
1659
|
+
assertSupportedNodeVersion(input.nodeVersion);
|
|
1660
|
+
const homeDir = input.homeDir ?? homedir();
|
|
1661
|
+
const runtimeDirectory = resolve(homeDir, ".haven", "mcp-runtime", MCP_RUNTIME_MANIFEST.mcpVersion);
|
|
1662
|
+
const npmCacheDirectory = resolve(homeDir, ".haven", "npm-cache");
|
|
1663
|
+
const cliPath = join(runtimeDirectory, "node_modules", "@haven_ai", "mcp", "dist", "cli.js");
|
|
1664
|
+
const messages = [];
|
|
1665
|
+
await mkdir(runtimeDirectory, { recursive: true, mode: 448 });
|
|
1666
|
+
await chmod(runtimeDirectory, 448).catch(() => void 0);
|
|
1667
|
+
await mkdir(npmCacheDirectory, { recursive: true, mode: 448 });
|
|
1668
|
+
await chmod(npmCacheDirectory, 448).catch(() => void 0);
|
|
1669
|
+
if (await installedRuntimeMatches2(runtimeDirectory, cliPath)) {
|
|
1670
|
+
messages.push(`Using existing local Haven MCP runtime ${mcpPackageSpec()}.`);
|
|
1671
|
+
} else {
|
|
1672
|
+
await installRuntimePackages2(runtimeDirectory, npmCacheDirectory, deps);
|
|
1673
|
+
messages.push(`Installed local Haven MCP runtime ${mcpPackageSpec()}.`);
|
|
1178
1674
|
}
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1675
|
+
await assertFileExists2(cliPath, "local Haven MCP CLI");
|
|
1676
|
+
const wrapperPath = join(input.credentialDirectory, "bin", "haven-mcp");
|
|
1677
|
+
await writeWrapper2({
|
|
1678
|
+
wrapperPath,
|
|
1679
|
+
cliPath,
|
|
1680
|
+
identityPath: input.identityPath,
|
|
1681
|
+
signerPath: input.signerPath
|
|
1682
|
+
});
|
|
1683
|
+
await writeRuntimeSidecar2({
|
|
1684
|
+
path: join(input.credentialDirectory, "mcp-runtime.json"),
|
|
1685
|
+
wrapperPath,
|
|
1686
|
+
runtimeDirectory,
|
|
1687
|
+
npmCacheDirectory,
|
|
1688
|
+
cliPath,
|
|
1689
|
+
serverName: input.serverName
|
|
1690
|
+
});
|
|
1691
|
+
messages.push(`Prepared stable local Haven MCP wrapper: ${wrapperPath}`);
|
|
1692
|
+
return {
|
|
1693
|
+
command: wrapperPath,
|
|
1694
|
+
args: [],
|
|
1695
|
+
wrapperPath,
|
|
1696
|
+
runtimeDirectory,
|
|
1697
|
+
npmCacheDirectory,
|
|
1698
|
+
cliPath,
|
|
1699
|
+
messages
|
|
1700
|
+
};
|
|
1184
1701
|
}
|
|
1185
|
-
function
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1702
|
+
function assertSupportedNodeVersion(nodeVersion = process.versions.node, minimumNodeVersion = MCP_RUNTIME_MANIFEST.minimumNodeVersion, subject = "Haven setup") {
|
|
1703
|
+
if (!isSupportedNodeVersion(nodeVersion, minimumNodeVersion)) {
|
|
1704
|
+
throw new UnsupportedNodeVersionError(nodeVersion, minimumNodeVersion, subject);
|
|
1705
|
+
}
|
|
1189
1706
|
}
|
|
1190
|
-
async function
|
|
1191
|
-
const
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1707
|
+
async function installRuntimePackages2(runtimeDirectory, npmCacheDirectory, deps) {
|
|
1708
|
+
const { runCommand, onProgress } = deps;
|
|
1709
|
+
const baseArgs = [
|
|
1710
|
+
"install",
|
|
1711
|
+
"--prefix",
|
|
1712
|
+
runtimeDirectory,
|
|
1713
|
+
"--no-audit",
|
|
1714
|
+
"--no-fund",
|
|
1715
|
+
"--omit=dev",
|
|
1716
|
+
"--prefer-offline",
|
|
1717
|
+
mcpPackageSpec(),
|
|
1718
|
+
sdkPackageSpec()
|
|
1719
|
+
];
|
|
1720
|
+
const run = async (args) => {
|
|
1721
|
+
const startedAt = Date.now();
|
|
1722
|
+
const heartbeat = setInterval(() => {
|
|
1723
|
+
const seconds = Math.round((Date.now() - startedAt) / 1e3);
|
|
1724
|
+
onProgress?.(`Still installing the local Haven MCP runtime\u2026 (${seconds}s \u2014 a cold cache can take several minutes)`);
|
|
1725
|
+
}, SIGNER_INSTALL_HEARTBEAT_MS);
|
|
1726
|
+
heartbeat.unref?.();
|
|
1727
|
+
try {
|
|
1728
|
+
if (runCommand) await runCommand("npm", args);
|
|
1729
|
+
else await execFileAsync2("npm", args, { timeout: SIGNER_INSTALL_TIMEOUT_MS, maxBuffer: 1024 * 1024 });
|
|
1730
|
+
} finally {
|
|
1731
|
+
clearInterval(heartbeat);
|
|
1200
1732
|
}
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
throw new
|
|
1209
|
-
"runtime_unrecognized",
|
|
1210
|
-
`"${supplied}" is not an agent runtime Haven knows. Valid values: ${RUNTIME_FLAG_VALUES} (the aliases cowork, codex and openclaw are accepted too). Re-run with one of those, or --runtime other to store credentials and finish the MCP setup by hand. Nothing was written and the Haven setup token is still unused.`,
|
|
1211
|
-
"rerun_connect_with_a_valid_runtime_name"
|
|
1212
|
-
);
|
|
1733
|
+
};
|
|
1734
|
+
try {
|
|
1735
|
+
await run(baseArgs);
|
|
1736
|
+
} catch {
|
|
1737
|
+
try {
|
|
1738
|
+
await run([...baseArgs, "--cache", npmCacheDirectory]);
|
|
1739
|
+
} catch (err) {
|
|
1740
|
+
throw new Error(`Could not install local Haven MCP runtime ${mcpPackageSpec()}: ${err instanceof Error ? err.message : String(err)}`);
|
|
1213
1741
|
}
|
|
1214
|
-
return { runtime: detected, source: "detected", discardedHint: supplied };
|
|
1215
1742
|
}
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1743
|
+
}
|
|
1744
|
+
async function installedRuntimeMatches2(runtimeDirectory, cliPath) {
|
|
1745
|
+
try {
|
|
1746
|
+
await assertFileExists2(cliPath, "local Haven MCP CLI");
|
|
1747
|
+
const [mcpPackage, sdkPackage] = await Promise.all([
|
|
1748
|
+
readPackageJson2(join(runtimeDirectory, "node_modules", "@haven_ai", "mcp", "package.json")),
|
|
1749
|
+
readPackageJson2(join(runtimeDirectory, "node_modules", "@haven_ai", "sdk", "package.json"))
|
|
1750
|
+
]);
|
|
1751
|
+
return mcpPackage.version === MCP_RUNTIME_MANIFEST.mcpVersion && sdkPackage.version === MCP_RUNTIME_MANIFEST.sdkVersion;
|
|
1752
|
+
} catch {
|
|
1753
|
+
return false;
|
|
1223
1754
|
}
|
|
1224
|
-
return { runtime: null, source: "none" };
|
|
1225
1755
|
}
|
|
1226
|
-
function
|
|
1227
|
-
|
|
1228
|
-
return mode === "restart-session" || mode === "restart-app";
|
|
1756
|
+
async function readPackageJson2(path) {
|
|
1757
|
+
return JSON.parse(await readFile(path, "utf8"));
|
|
1229
1758
|
}
|
|
1230
|
-
function
|
|
1231
|
-
|
|
1232
|
-
|
|
1759
|
+
async function writeWrapper2(input) {
|
|
1760
|
+
await mkdir(dirname(input.wrapperPath), { recursive: true, mode: 448 });
|
|
1761
|
+
await chmod(dirname(input.wrapperPath), 448).catch(() => void 0);
|
|
1762
|
+
const source = [
|
|
1763
|
+
"#!/usr/bin/env node",
|
|
1764
|
+
"import { spawn } from 'node:child_process'",
|
|
1765
|
+
"",
|
|
1766
|
+
`const cliPath = ${JSON.stringify(input.cliPath)}`,
|
|
1767
|
+
`const identityPath = ${JSON.stringify(input.identityPath)}`,
|
|
1768
|
+
`const signerPath = ${JSON.stringify(input.signerPath)}`,
|
|
1769
|
+
"",
|
|
1770
|
+
"const child = spawn(process.execPath, [cliPath, '--identity', identityPath, '--signer', signerPath, ...process.argv.slice(2)], {",
|
|
1771
|
+
" stdio: 'inherit',",
|
|
1772
|
+
"})",
|
|
1773
|
+
"",
|
|
1774
|
+
"child.on('exit', (code, signal) => {",
|
|
1775
|
+
" if (signal) process.kill(process.pid, signal)",
|
|
1776
|
+
" else process.exit(code ?? 1)",
|
|
1777
|
+
"})",
|
|
1778
|
+
""
|
|
1779
|
+
].join("\n");
|
|
1780
|
+
await writeFile(input.wrapperPath, source, { mode: 448 });
|
|
1781
|
+
await chmod(input.wrapperPath, 448).catch(() => void 0);
|
|
1233
1782
|
}
|
|
1234
|
-
function
|
|
1235
|
-
const
|
|
1783
|
+
async function writeRuntimeSidecar2(input) {
|
|
1784
|
+
const value = {
|
|
1785
|
+
...input.serverName ? { server_name: input.serverName } : {},
|
|
1786
|
+
mcp_package: MCP_RUNTIME_MANIFEST.mcpPackage,
|
|
1787
|
+
mcp_version: MCP_RUNTIME_MANIFEST.mcpVersion,
|
|
1788
|
+
sdk_package: MCP_RUNTIME_MANIFEST.sdkPackage,
|
|
1789
|
+
sdk_version: MCP_RUNTIME_MANIFEST.sdkVersion,
|
|
1790
|
+
minimum_node_version: MCP_RUNTIME_MANIFEST.minimumNodeVersion,
|
|
1791
|
+
wrapper_path: input.wrapperPath,
|
|
1792
|
+
runtime_directory: input.runtimeDirectory,
|
|
1793
|
+
npm_cache_directory: input.npmCacheDirectory,
|
|
1794
|
+
cli_path: input.cliPath
|
|
1795
|
+
};
|
|
1796
|
+
await writeFile(input.path, `${JSON.stringify(value, null, 2)}
|
|
1797
|
+
`, { mode: 384 });
|
|
1798
|
+
await chmod(input.path, 384).catch(() => void 0);
|
|
1799
|
+
}
|
|
1800
|
+
async function assertFileExists2(path, label) {
|
|
1801
|
+
try {
|
|
1802
|
+
await access(path);
|
|
1803
|
+
} catch {
|
|
1804
|
+
throw new Error(`Missing ${label}: ${path}`);
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
var execFileAsync2, UnsupportedNodeVersionError;
|
|
1808
|
+
var init_local_mcp_runtime = __esm({
|
|
1809
|
+
"src/local-mcp-runtime.ts"() {
|
|
1810
|
+
init_signer_runtime();
|
|
1811
|
+
init_runtime_manifest();
|
|
1812
|
+
execFileAsync2 = promisify(execFile);
|
|
1813
|
+
UnsupportedNodeVersionError = class extends Error {
|
|
1814
|
+
code = "local_mcp_unsupported_node_version";
|
|
1815
|
+
nodeVersion;
|
|
1816
|
+
minimumNodeVersion;
|
|
1817
|
+
constructor(nodeVersion, minimumNodeVersion, subject = "Haven setup") {
|
|
1818
|
+
super(unsupportedNodeVersionMessage({ subject, nodeVersion, minimumNodeVersion }));
|
|
1819
|
+
this.name = "UnsupportedNodeVersionError";
|
|
1820
|
+
this.nodeVersion = nodeVersion;
|
|
1821
|
+
this.minimumNodeVersion = minimumNodeVersion;
|
|
1822
|
+
}
|
|
1823
|
+
};
|
|
1824
|
+
}
|
|
1825
|
+
});
|
|
1826
|
+
async function installSkillForRuntime(runtime, deps = {}) {
|
|
1827
|
+
switch (runtime) {
|
|
1828
|
+
case "claude-code":
|
|
1829
|
+
return installSkillFile(
|
|
1830
|
+
resolve(deps.homeDir ?? homedir(), ".claude", "skills", SKILL_FOLDER_NAME),
|
|
1831
|
+
"~/.claude/skills/haven-pay"
|
|
1832
|
+
);
|
|
1833
|
+
case "hermes":
|
|
1834
|
+
return installSkillFile(
|
|
1835
|
+
join(hermesHome(deps), "skills", SKILL_FOLDER_NAME),
|
|
1836
|
+
"the Hermes skills folder"
|
|
1837
|
+
);
|
|
1838
|
+
case "codex-cli":
|
|
1839
|
+
case "codex-desktop":
|
|
1840
|
+
return installCodexAgentsSection(deps);
|
|
1841
|
+
default:
|
|
1842
|
+
return void 0;
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
async function installSkillFile(skillDir, label) {
|
|
1846
|
+
try {
|
|
1847
|
+
await mkdir(skillDir, { recursive: true });
|
|
1848
|
+
const target = join(skillDir, "SKILL.md");
|
|
1849
|
+
await writeFile(target, HAVEN_SKILL_MD, "utf8");
|
|
1850
|
+
return {
|
|
1851
|
+
installed: true,
|
|
1852
|
+
target,
|
|
1853
|
+
messages: [`Installed the generic Haven payment skill (${label}). It contains no secrets.`]
|
|
1854
|
+
};
|
|
1855
|
+
} catch (err) {
|
|
1856
|
+
return {
|
|
1857
|
+
installed: false,
|
|
1858
|
+
messages: [
|
|
1859
|
+
`Could not install the Haven payment skill: ${err instanceof Error ? err.message : String(err)}. Download it from the Haven dashboard instead.`
|
|
1860
|
+
]
|
|
1861
|
+
};
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
async function installCodexAgentsSection(deps) {
|
|
1865
|
+
try {
|
|
1866
|
+
const codexDir = resolve(deps.homeDir ?? homedir(), ".codex");
|
|
1867
|
+
const target = join(codexDir, "AGENTS.md");
|
|
1868
|
+
await mkdir(codexDir, { recursive: true });
|
|
1869
|
+
const existing = await readFile(target, "utf8").catch(() => null);
|
|
1870
|
+
const next = upsertManagedSection(existing, codexManagedSection());
|
|
1871
|
+
if (next !== existing) {
|
|
1872
|
+
await writeFile(target, next, "utf8");
|
|
1873
|
+
}
|
|
1874
|
+
return {
|
|
1875
|
+
installed: true,
|
|
1876
|
+
target,
|
|
1877
|
+
messages: [
|
|
1878
|
+
"Installed the generic Haven payment guidance as a managed section in ~/.codex/AGENTS.md (Codex reads it as global instructions). It contains no secrets; your own content in that file is untouched."
|
|
1879
|
+
]
|
|
1880
|
+
};
|
|
1881
|
+
} catch (err) {
|
|
1882
|
+
return {
|
|
1883
|
+
installed: false,
|
|
1884
|
+
messages: [
|
|
1885
|
+
`Could not install the Haven payment guidance into ~/.codex/AGENTS.md: ${err instanceof Error ? err.message : String(err)}. Download the skill from the Haven dashboard instead.`
|
|
1886
|
+
]
|
|
1887
|
+
};
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
function codexManagedSection() {
|
|
1891
|
+
return `${CODEX_AGENTS_BEGIN_MARKER}
|
|
1892
|
+
|
|
1893
|
+
${HAVEN_SKILL_BODY_MD.trimEnd()}
|
|
1894
|
+
|
|
1895
|
+
${CODEX_AGENTS_END_MARKER}
|
|
1896
|
+
`;
|
|
1897
|
+
}
|
|
1898
|
+
function upsertManagedSection(existing, section) {
|
|
1899
|
+
if (existing === null || existing.trim() === "") return section;
|
|
1900
|
+
const begins = markerLineIndexes(existing, CODEX_AGENTS_BEGIN_MARKER);
|
|
1901
|
+
const ends = markerLineIndexes(existing, CODEX_AGENTS_END_MARKER);
|
|
1902
|
+
if (begins.length === 1 && ends.length === 1 && ends[0] > begins[0]) {
|
|
1903
|
+
const afterEnd = ends[0] + CODEX_AGENTS_END_MARKER.length;
|
|
1904
|
+
const tail = existing.startsWith("\r\n", afterEnd) ? existing.slice(afterEnd + 2) : existing.startsWith("\n", afterEnd) ? existing.slice(afterEnd + 1) : existing.slice(afterEnd);
|
|
1905
|
+
return existing.slice(0, begins[0]) + section + tail;
|
|
1906
|
+
}
|
|
1907
|
+
if (begins.length > 0 || ends.length > 0) {
|
|
1908
|
+
throw new Error(
|
|
1909
|
+
"found a damaged Haven marker section (orphaned or duplicated markers); remove the leftover marker lines and re-run setup"
|
|
1910
|
+
);
|
|
1911
|
+
}
|
|
1912
|
+
return `${existing.replace(/\n*$/, "\n\n")}${section}`;
|
|
1913
|
+
}
|
|
1914
|
+
function markerLineIndexes(text, marker) {
|
|
1915
|
+
const indexes = [];
|
|
1916
|
+
for (let from = 0; ; ) {
|
|
1917
|
+
const at = text.indexOf(marker, from);
|
|
1918
|
+
if (at === -1) return indexes;
|
|
1919
|
+
if (at === 0 || text[at - 1] === "\n") indexes.push(at);
|
|
1920
|
+
from = at + marker.length;
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
function hermesHome(deps) {
|
|
1924
|
+
const env = deps.env ?? process.env;
|
|
1925
|
+
return env.HERMES_HOME ?? join(deps.homeDir ?? homedir(), ".hermes");
|
|
1926
|
+
}
|
|
1927
|
+
var CODEX_AGENTS_BEGIN_MARKER, CODEX_AGENTS_END_MARKER;
|
|
1928
|
+
var init_skill_install = __esm({
|
|
1929
|
+
"src/skill-install.ts"() {
|
|
1930
|
+
CODEX_AGENTS_BEGIN_MARKER = "<!-- BEGIN haven-pay (managed by @haven_ai/connect; edits inside this section are overwritten on re-setup) -->";
|
|
1931
|
+
CODEX_AGENTS_END_MARKER = "<!-- END haven-pay -->";
|
|
1932
|
+
}
|
|
1933
|
+
});
|
|
1934
|
+
|
|
1935
|
+
// src/connect-error.ts
|
|
1936
|
+
var ConnectError;
|
|
1937
|
+
var init_connect_error = __esm({
|
|
1938
|
+
"src/connect-error.ts"() {
|
|
1939
|
+
ConnectError = class extends Error {
|
|
1940
|
+
code;
|
|
1941
|
+
nextAction;
|
|
1942
|
+
constructor(code, message, nextAction2) {
|
|
1943
|
+
super(message);
|
|
1944
|
+
this.name = "ConnectError";
|
|
1945
|
+
this.code = code;
|
|
1946
|
+
this.nextAction = nextAction2;
|
|
1947
|
+
}
|
|
1948
|
+
};
|
|
1949
|
+
}
|
|
1950
|
+
});
|
|
1951
|
+
|
|
1952
|
+
// src/runtime-registry.ts
|
|
1953
|
+
function runtimeProfile(runtime, env = process.env) {
|
|
1954
|
+
return RUNTIME_PROFILES[normalizeRuntime(runtime, env)];
|
|
1955
|
+
}
|
|
1956
|
+
function normalizeRuntime(runtime, env = process.env) {
|
|
1957
|
+
const explicit = normalizeRuntimeName(runtime);
|
|
1958
|
+
if (explicit) return explicit;
|
|
1959
|
+
return detectRuntime(env) ?? "other";
|
|
1960
|
+
}
|
|
1961
|
+
async function resolveRuntimeSelection(explicit, force, options = {}) {
|
|
1962
|
+
const env = options.env ?? process.env;
|
|
1963
|
+
if (force !== void 0) {
|
|
1964
|
+
const forced = normalizeRuntimeName(force);
|
|
1965
|
+
if (!forced) {
|
|
1966
|
+
throw new ConnectError(
|
|
1967
|
+
"runtime_force_unrecognized",
|
|
1968
|
+
`Unknown --runtime-force value "${force}". Valid values: ${RUNTIME_FLAG_VALUES}.`,
|
|
1969
|
+
"rerun_connect_with_a_valid_runtime_name"
|
|
1970
|
+
);
|
|
1971
|
+
}
|
|
1972
|
+
return { runtime: forced, source: "force" };
|
|
1973
|
+
}
|
|
1974
|
+
const detected = detectRuntime(env);
|
|
1975
|
+
const supplied = explicit?.trim() || options.selfReported?.trim() || void 0;
|
|
1976
|
+
const hint = normalizeRuntimeName(supplied);
|
|
1977
|
+
if (supplied && !hint) {
|
|
1978
|
+
if (!detected) {
|
|
1979
|
+
throw new ConnectError(
|
|
1980
|
+
"runtime_unrecognized",
|
|
1981
|
+
`"${supplied}" is not an agent runtime Haven knows. Valid values: ${RUNTIME_FLAG_VALUES} (the aliases cowork, codex and openclaw are accepted too). Re-run with one of those, or --runtime other to store credentials and finish the MCP setup by hand. Nothing was written and the Haven setup token is still unused.`,
|
|
1982
|
+
"rerun_connect_with_a_valid_runtime_name"
|
|
1983
|
+
);
|
|
1984
|
+
}
|
|
1985
|
+
return { runtime: detected, source: "detected", discardedHint: supplied };
|
|
1986
|
+
}
|
|
1987
|
+
if (detected && hint && detected !== hint) {
|
|
1988
|
+
return { runtime: detected, source: "detected", overrodeHint: hint };
|
|
1989
|
+
}
|
|
1990
|
+
if (hint) return { runtime: hint, source: "explicit" };
|
|
1991
|
+
if (detected) return { runtime: detected, source: "detected" };
|
|
1992
|
+
if (options.promptForRuntime) {
|
|
1993
|
+
return { runtime: await options.promptForRuntime(), source: "prompted" };
|
|
1994
|
+
}
|
|
1995
|
+
return { runtime: null, source: "none" };
|
|
1996
|
+
}
|
|
1997
|
+
function restartRequiredForRuntime(runtime, env = process.env) {
|
|
1998
|
+
const mode = runtimeProfile(runtime, env).restartMode;
|
|
1999
|
+
return mode === "restart-session" || mode === "restart-app";
|
|
2000
|
+
}
|
|
2001
|
+
function runtimeVerificationInstruction(runtime) {
|
|
2002
|
+
const label = RUNTIME_PROFILES[runtime].label;
|
|
2003
|
+
return `In ${label}, run the read-only \`haven_get_agent\` and \`haven_get_allowances\` tools to confirm the Haven wallet and live budget. Do not sign, fund, or create a payment to verify setup.`;
|
|
2004
|
+
}
|
|
2005
|
+
function normalizeRuntimeName(runtime) {
|
|
2006
|
+
const key = runtime?.trim().toLowerCase();
|
|
1236
2007
|
if (!key) return null;
|
|
1237
2008
|
return RUNTIME_ALIASES[key.replace(/\s+/g, "-")] ?? null;
|
|
1238
2009
|
}
|
|
@@ -1438,1519 +2209,1319 @@ var init_signer_consent = __esm({
|
|
|
1438
2209
|
"src/signer-consent.ts"() {
|
|
1439
2210
|
}
|
|
1440
2211
|
});
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
].join("\n");
|
|
1476
|
-
}
|
|
1477
|
-
async function writeAgentTombstone(input) {
|
|
1478
|
-
const dirStat = await stat(input.directory).catch(() => null);
|
|
1479
|
-
if (!dirStat?.isDirectory()) {
|
|
1480
|
-
throw new Error(`Not a directory: ${input.directory} \u2014 nothing to tombstone.`);
|
|
1481
|
-
}
|
|
1482
|
-
const info = {
|
|
1483
|
-
// reason / replaced_by are persisted to disk and re-emitted to the host's
|
|
1484
|
-
// MCP stderr log on EVERY stale probe, potentially for months — redact
|
|
1485
|
-
// like every other output path, at the write layer so any future caller
|
|
1486
|
-
// inherits it. (#1681 review, finding 1)
|
|
1487
|
-
agent_id: input.agentId,
|
|
1488
|
-
retired_at: input.retiredAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
1489
|
-
reason: redactSecrets(input.reason),
|
|
1490
|
-
...input.replacedBy ? { replaced_by: redactSecrets(input.replacedBy) } : {}
|
|
1491
|
-
};
|
|
1492
|
-
const binDir = join(input.directory, "bin");
|
|
1493
|
-
await mkdir(binDir, { recursive: true });
|
|
1494
|
-
const wrapperPath = join(binDir, "haven-signer.mjs");
|
|
1495
|
-
await writeFile(wrapperPath, tombstoneScript(info), "utf8");
|
|
1496
|
-
await chmod(wrapperPath, 493);
|
|
1497
|
-
await writeFile(join(input.directory, TOMBSTONE_FILENAME), JSON.stringify(info, null, 2) + "\n", "utf8");
|
|
1498
|
-
return info;
|
|
1499
|
-
}
|
|
1500
|
-
async function readAgentTombstone(directory) {
|
|
1501
|
-
try {
|
|
1502
|
-
const parsed = JSON.parse(await readFile(join(directory, TOMBSTONE_FILENAME), "utf8"));
|
|
1503
|
-
if (typeof parsed?.agent_id !== "string") return null;
|
|
1504
|
-
return parsed;
|
|
1505
|
-
} catch {
|
|
1506
|
-
return null;
|
|
1507
|
-
}
|
|
1508
|
-
}
|
|
1509
|
-
var TOMBSTONE_FILENAME, TOMBSTONE_MARKER;
|
|
1510
|
-
var init_tombstone = __esm({
|
|
1511
|
-
"src/tombstone.ts"() {
|
|
1512
|
-
init_redact();
|
|
1513
|
-
TOMBSTONE_FILENAME = "TOMBSTONE.json";
|
|
1514
|
-
TOMBSTONE_MARKER = "HAVEN-TOMBSTONE";
|
|
1515
|
-
}
|
|
1516
|
-
});
|
|
1517
|
-
|
|
1518
|
-
// src/doctor.ts
|
|
1519
|
-
var doctor_exports = {};
|
|
1520
|
-
__export(doctor_exports, {
|
|
1521
|
-
runDoctor: () => runDoctor,
|
|
1522
|
-
runRepair: () => runRepair
|
|
1523
|
-
});
|
|
1524
|
-
async function discoverCredentialDirectory(homeDir, explicit) {
|
|
1525
|
-
const root = explicit ? dirname(explicit) : join(homeDir, ".haven", "agents");
|
|
1526
|
-
let entries = [];
|
|
1527
|
-
try {
|
|
1528
|
-
entries = await readdir(root);
|
|
1529
|
-
} catch {
|
|
1530
|
-
return explicit ? { directory: explicit, others: [] } : { others: [] };
|
|
2212
|
+
async function installRuntime(input, deps = {}) {
|
|
2213
|
+
const runtime = normalizeRuntime(input.runtime, deps.env);
|
|
2214
|
+
const profile = runtimeProfile(runtime, deps.env);
|
|
2215
|
+
const progress = deps.onProgress ?? (() => void 0);
|
|
2216
|
+
const localRuntime = input.localMcp === true && supportsLocalMcp(runtime);
|
|
2217
|
+
const consentMessages = [];
|
|
2218
|
+
const localMcpConsent = localRuntime ? await resolveLocalMcpConsent(input, consentMessages) : void 0;
|
|
2219
|
+
const signerConsent = localRuntime ? void 0 : await resolveSignerConsent(input, consentMessages);
|
|
2220
|
+
if (runtime === "other") {
|
|
2221
|
+
const signerCredentialReady2 = await probeLocalSignerCredential(input.signerPath);
|
|
2222
|
+
const signerReady = signerCredentialReady2 && signerConsent?.acknowledged;
|
|
2223
|
+
return {
|
|
2224
|
+
runtime,
|
|
2225
|
+
runtimeMcpMode: "manual",
|
|
2226
|
+
hostedMcpConfigured: false,
|
|
2227
|
+
localSignerConfigured: false,
|
|
2228
|
+
localMcpConfigured: false,
|
|
2229
|
+
probeResult: signerReady ? "manual_runtime_setup_required_local_signer_ready" : "manual_runtime_setup_required_local_signer_unavailable",
|
|
2230
|
+
restartRequired: true,
|
|
2231
|
+
nextUserAction: "return_to_haven_for_wallet_approval_then_configure_runtime",
|
|
2232
|
+
errorCode: "manual_runtime_setup_required",
|
|
2233
|
+
configTarget: "manual runtime setup",
|
|
2234
|
+
signerAcknowledged: signerConsent?.acknowledged,
|
|
2235
|
+
localMcpAcknowledged: false,
|
|
2236
|
+
messages: [
|
|
2237
|
+
...consentMessages,
|
|
2238
|
+
"Custom runtime: Haven did not auto-configure it. Your credentials are on disk (chmod 600) \u2014 read them at runtime; never paste a key into the agent prompt, memory, or logs.",
|
|
2239
|
+
` identity (hosted MCP Bearer): ${input.identityPath}`,
|
|
2240
|
+
` signer (local signing key): ${input.signerPath}`,
|
|
2241
|
+
"After wallet approval, wire the runtime to Haven by reference:",
|
|
2242
|
+
` Hosted MCP + local signer: point your MCP client at ${input.hostedMcpUrl} with the api_key from identity.json, then run npx -y ${signerPackageSpec()} --credentials ${input.signerPath}`,
|
|
2243
|
+
` Fully local MCP (no hosted dependency): npx -y ${mcpPackageSpec()} --identity ${input.identityPath} --signer ${input.signerPath}`
|
|
2244
|
+
]
|
|
2245
|
+
};
|
|
1531
2246
|
}
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
const directory = join(root, entry);
|
|
2247
|
+
let localRuntimeInstall;
|
|
2248
|
+
let localRuntimeError;
|
|
2249
|
+
if (localRuntime) {
|
|
1536
2250
|
try {
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
try {
|
|
1541
|
-
await stat(join(directory, TOMBSTONE_FILENAME));
|
|
1542
|
-
tombstonedOnly.push(directory);
|
|
1543
|
-
} catch {
|
|
1544
|
-
}
|
|
2251
|
+
localRuntimeInstall = await prepareRuntimeForLocalMcp(input, deps);
|
|
2252
|
+
} catch (err) {
|
|
2253
|
+
localRuntimeError = err;
|
|
1545
2254
|
}
|
|
1546
2255
|
}
|
|
1547
|
-
|
|
1548
|
-
|
|
2256
|
+
if (localRuntimeError) {
|
|
2257
|
+
const errorCode2 = localRuntimePrepareErrorCode(localRuntimeError);
|
|
1549
2258
|
return {
|
|
1550
|
-
|
|
1551
|
-
|
|
2259
|
+
runtime,
|
|
2260
|
+
runtimeMcpMode: "local_stdio",
|
|
2261
|
+
hostedMcpConfigured: false,
|
|
2262
|
+
localSignerConfigured: false,
|
|
2263
|
+
localMcpConfigured: false,
|
|
2264
|
+
probeResult: errorCode2 === "local_mcp_unsupported_node_version" ? "local_stdio_mcp_unsupported_node_version" : "local_stdio_mcp_runtime_install_failed",
|
|
2265
|
+
restartRequired: true,
|
|
2266
|
+
nextUserAction: nextAction(runtime, profile.restartMode, errorCode2),
|
|
2267
|
+
errorCode: errorCode2,
|
|
2268
|
+
configTarget: profile.label,
|
|
2269
|
+
signerAcknowledged: signerConsent?.acknowledged,
|
|
2270
|
+
localMcpAcknowledged: localMcpConsent?.acknowledged,
|
|
2271
|
+
activationCommand: void 0,
|
|
2272
|
+
messages: [
|
|
2273
|
+
...consentMessages,
|
|
2274
|
+
`Could not prepare local Haven MCP runtime: ${localRuntimeError instanceof Error ? localRuntimeError.message : String(localRuntimeError)}`
|
|
2275
|
+
]
|
|
1552
2276
|
};
|
|
1553
2277
|
}
|
|
1554
|
-
|
|
2278
|
+
let signerCommand;
|
|
2279
|
+
if (!localRuntime) {
|
|
2280
|
+
progress("Getting the signer ready\u2026");
|
|
2281
|
+
try {
|
|
2282
|
+
const signerRuntime = await prepareSignerForRuntime(input, deps);
|
|
2283
|
+
signerCommand = { command: signerRuntime.command, args: signerRuntime.args };
|
|
2284
|
+
consentMessages.push(...signerRuntime.messages);
|
|
2285
|
+
} catch (err) {
|
|
2286
|
+
return {
|
|
2287
|
+
runtime,
|
|
2288
|
+
runtimeMcpMode: "hosted_plus_signer",
|
|
2289
|
+
hostedMcpConfigured: false,
|
|
2290
|
+
localSignerConfigured: false,
|
|
2291
|
+
localMcpConfigured: false,
|
|
2292
|
+
probeResult: "signer_runtime_install_failed",
|
|
2293
|
+
restartRequired: false,
|
|
2294
|
+
nextUserAction: "The local Haven signer runtime could not be installed, so no configuration was written. Check your network (a cold install downloads the signer package set) and re-run: npx @haven_ai/connect@alpha",
|
|
2295
|
+
errorCode: "signer_runtime_install_failed",
|
|
2296
|
+
configTarget: profile.label,
|
|
2297
|
+
signerAcknowledged: signerConsent?.acknowledged,
|
|
2298
|
+
localMcpAcknowledged: localMcpConsent?.acknowledged,
|
|
2299
|
+
activationCommand: void 0,
|
|
2300
|
+
signerRuntimePrepared: false,
|
|
2301
|
+
messages: [
|
|
2302
|
+
...consentMessages,
|
|
2303
|
+
`Could not pre-install the local Haven signer: ${err instanceof Error ? err.message : String(err)}`,
|
|
2304
|
+
"No runtime configuration was written (fail-closed): a config pointing at an uninstalled signer looks wired but cannot start.",
|
|
2305
|
+
"Re-run `npx @haven_ai/connect@alpha` to retry the setup."
|
|
2306
|
+
]
|
|
2307
|
+
};
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
const signerRuntimePrepared = localRuntime ? void 0 : signerCommand !== void 0;
|
|
2311
|
+
progress("Setting up your Haven tools\u2026");
|
|
2312
|
+
const configResult = localRuntime ? runtime === "claude-code" ? await configureClaudeCode(deps, localRuntimeInstall?.command ?? "", input.serverName) : await writeRuntimeConfig({
|
|
2313
|
+
runtime,
|
|
2314
|
+
hostedMcpUrl: input.hostedMcpUrl,
|
|
2315
|
+
apiKey: input.apiKey,
|
|
2316
|
+
identityPath: input.identityPath,
|
|
2317
|
+
signerPath: input.signerPath,
|
|
2318
|
+
serverName: input.serverName,
|
|
2319
|
+
credentialDirectory: input.credentialDirectory,
|
|
2320
|
+
localMcpCommand: localRuntimeInstall?.command,
|
|
2321
|
+
signerCommand,
|
|
2322
|
+
homeDir: deps.homeDir,
|
|
2323
|
+
mode: "local"
|
|
2324
|
+
}) : await writeHostedRuntimeConfig(deps, { ...input, runtime }, signerCommand);
|
|
2325
|
+
if (deps.onRuntimeConfigured) {
|
|
2326
|
+
const signerCredentialOnDisk = await probeLocalSignerCredential(input.signerPath);
|
|
2327
|
+
const earlyLocalMcpOk = configResult.runtimeMcpMode === "local_stdio" && configResult.localMcpConfigured && signerCredentialOnDisk && Boolean(localMcpConsent?.acknowledged);
|
|
2328
|
+
const earlySignerOk = configResult.runtimeMcpMode === "local_stdio" ? earlyLocalMcpOk : configResult.signerConfigured && signerCredentialOnDisk && Boolean(signerConsent?.acknowledged);
|
|
2329
|
+
try {
|
|
2330
|
+
await deps.onRuntimeConfigured({
|
|
2331
|
+
runtime,
|
|
2332
|
+
runtimeMcpMode: configResult.runtimeMcpMode,
|
|
2333
|
+
hostedMcpConfigured: configResult.hostedConfigured,
|
|
2334
|
+
localSignerConfigured: earlySignerOk,
|
|
2335
|
+
localMcpConfigured: earlyLocalMcpOk,
|
|
2336
|
+
signerAcknowledged: signerConsent?.acknowledged,
|
|
2337
|
+
localMcpAcknowledged: localMcpConsent?.acknowledged,
|
|
2338
|
+
restartRequired: configResult.restartRequired || restartRequiredForRuntime(runtime, deps.env),
|
|
2339
|
+
nextUserAction: nextAction(runtime, profile.restartMode, configResult.errorCode),
|
|
2340
|
+
errorCode: configResult.errorCode
|
|
2341
|
+
});
|
|
2342
|
+
} catch {
|
|
2343
|
+
}
|
|
2344
|
+
}
|
|
2345
|
+
progress("Almost there \u2014 just confirming everything connects\u2026");
|
|
2346
|
+
const localProbePromise = configResult.runtimeMcpMode === "local_stdio" && localRuntimeInstall ? runLocalMcpProbe(localRuntimeInstall, deps) : Promise.resolve(void 0);
|
|
2347
|
+
const signerProbePromise = configResult.runtimeMcpMode !== "local_stdio" && signerCommand ? (deps.probeSignerTools ?? probeLocalMcpTools)(
|
|
2348
|
+
signerCommand.command,
|
|
2349
|
+
signerCommand.args,
|
|
2350
|
+
MCP_RUNTIME_MANIFEST.requiredSignerTools
|
|
2351
|
+
) : Promise.resolve(void 0);
|
|
2352
|
+
const [hostedProbe, signerCredentialReady, localMcpProbe, signerProbe] = await Promise.all([
|
|
2353
|
+
configResult.hostedConfigured ? probeHostedMcpTools(input.apiKey, input.hostedMcpUrl, deps.fetch) : Promise.resolve({ status: "bad_response" }),
|
|
2354
|
+
probeLocalSignerCredential(input.signerPath),
|
|
2355
|
+
localProbePromise,
|
|
2356
|
+
signerProbePromise
|
|
2357
|
+
]);
|
|
2358
|
+
const hostedOk = configResult.hostedConfigured && hostedProbe.status === "ok";
|
|
2359
|
+
const localMcpOk = configResult.runtimeMcpMode === "local_stdio" && configResult.localMcpConfigured && signerCredentialReady && Boolean(localMcpConsent?.acknowledged) && localMcpProbe?.status === "ok";
|
|
2360
|
+
const signerOk = configResult.runtimeMcpMode === "local_stdio" ? localMcpOk : configResult.signerConfigured && signerCredentialReady && Boolean(signerConsent?.acknowledged) && // #1587: no handshake, no green. A signer command that was registered
|
|
2361
|
+
// but not probed (manual topology) keeps the old semantics.
|
|
2362
|
+
(signerProbe === void 0 || signerProbe.status === "ok");
|
|
2363
|
+
const restartRequired = configResult.restartRequired || restartRequiredForRuntime(runtime, deps.env);
|
|
2364
|
+
const errorCode = configResult.errorCode ?? (configResult.runtimeMcpMode === "local_stdio" ? localMcpErrorCode(signerCredentialReady, localMcpConsent, localMcpProbe?.status) : hostedMcpErrorCode(configResult.hostedConfigured, hostedProbe.status) ?? signerConsentErrorCode(signerCredentialReady, signerConsent) ?? signerProbeErrorCode(signerProbe));
|
|
2365
|
+
const hostedProbeMessages = configResult.hostedConfigured && hostedProbe.status !== "ok" ? [`Hosted Haven MCP probe failed: ${hostedProbe.status}.`] : configResult.hostedConfigured ? ["Verified hosted Haven MCP tools with a read-only handshake."] : [];
|
|
2366
|
+
const signerProbeMessages = signerProbe ? signerProbe.status === "ok" ? ["Verified local Haven signer with a stdio handshake."] : [
|
|
2367
|
+
`Local Haven signer handshake failed: ${signerProbe.status}.`,
|
|
2368
|
+
"Re-run `npx @haven_ai/connect@alpha` to repair the signer setup."
|
|
2369
|
+
] : [];
|
|
2370
|
+
const localProbeMessages = localMcpProbe && localMcpProbe.status !== "ok" ? [`Local Haven MCP handshake failed: ${localMcpProbe.status}.`] : localMcpProbe?.status === "ok" ? ["Verified local Haven MCP tools with a stdio handshake."] : [];
|
|
2371
|
+
const skillInstall = !configResult.errorCode ? await installSkillForRuntime(runtime, { homeDir: deps.homeDir, env: deps.env }) : void 0;
|
|
1555
2372
|
return {
|
|
1556
|
-
|
|
1557
|
-
|
|
2373
|
+
runtime,
|
|
2374
|
+
runtimeMcpMode: configResult.runtimeMcpMode,
|
|
2375
|
+
hostedMcpConfigured: hostedOk,
|
|
2376
|
+
localSignerConfigured: signerOk,
|
|
2377
|
+
localMcpConfigured: localMcpOk,
|
|
2378
|
+
probeResult: buildProbeResult(configResult.runtimeMcpMode, configResult.hostedConfigured, hostedProbe.status, signerOk, localMcpOk, localMcpProbe?.status),
|
|
2379
|
+
restartRequired,
|
|
2380
|
+
nextUserAction: nextAction(runtime, profile.restartMode, errorCode),
|
|
2381
|
+
errorCode,
|
|
2382
|
+
configTarget: configResult.target,
|
|
2383
|
+
signerAcknowledged: signerConsent?.acknowledged,
|
|
2384
|
+
localMcpAcknowledged: localMcpConsent?.acknowledged,
|
|
2385
|
+
activationCommand: configResult.activationCommand,
|
|
2386
|
+
skillInstalled: skillInstall?.installed,
|
|
2387
|
+
signerRuntimePrepared,
|
|
2388
|
+
messages: [...consentMessages, ...localRuntimeInstall?.messages ?? [], ...configResult.messages, ...hostedProbeMessages, ...signerProbeMessages, ...localProbeMessages, ...skillInstall?.messages ?? []]
|
|
1558
2389
|
};
|
|
1559
2390
|
}
|
|
1560
|
-
function
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
return false;
|
|
1567
|
-
}
|
|
1568
|
-
if (sidecar?.wrapper_path && configText.includes(sidecar.wrapper_path)) return true;
|
|
1569
|
-
if (bareOwnerExists) return false;
|
|
1570
|
-
return isPrimary && Boolean(identity?.hosted_mcp_url && configText.includes(identity.hosted_mcp_url));
|
|
2391
|
+
function runtimeInstallCapabilities(runtime, env = process.env) {
|
|
2392
|
+
const profile = runtimeProfile(runtime, env);
|
|
2393
|
+
return {
|
|
2394
|
+
canWriteRuntimeConfig: profile.canWriteRuntimeConfig,
|
|
2395
|
+
restartRequired: restartRequiredForRuntime(runtime, env)
|
|
2396
|
+
};
|
|
1571
2397
|
}
|
|
1572
|
-
async function
|
|
2398
|
+
async function configureClaudeCode(deps, localMcpCommand, serverName) {
|
|
2399
|
+
const runCommand = deps.runCommand ?? defaultRunCommand;
|
|
2400
|
+
const serverJson = JSON.stringify({
|
|
2401
|
+
type: "stdio",
|
|
2402
|
+
command: localMcpCommand,
|
|
2403
|
+
args: [],
|
|
2404
|
+
env: {}
|
|
2405
|
+
});
|
|
1573
2406
|
try {
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
2407
|
+
if (!localMcpCommand) throw new Error("local MCP wrapper command is required");
|
|
2408
|
+
const names = serverNamesFor(serverName);
|
|
2409
|
+
await runCommand("claude", ["mcp", "remove", names.hosted]).catch(() => void 0);
|
|
2410
|
+
await runCommand("claude", ["mcp", "remove", names.signer]).catch(() => void 0);
|
|
2411
|
+
await runCommand("claude", ["mcp", "add-json", names.hosted, serverJson, "--scope", "user"]).catch(async () => {
|
|
2412
|
+
await runCommand("claude", ["mcp", "add", names.hosted, "--scope", "user", "--", localMcpCommand]);
|
|
2413
|
+
});
|
|
2414
|
+
const verified = await runCommand("claude", ["mcp", "get", names.hosted]).then(() => true).catch(() => false);
|
|
2415
|
+
return {
|
|
2416
|
+
hostedConfigured: false,
|
|
2417
|
+
signerConfigured: true,
|
|
2418
|
+
localMcpConfigured: true,
|
|
2419
|
+
runtimeMcpMode: "local_stdio",
|
|
2420
|
+
target: "Claude Code MCP config",
|
|
2421
|
+
changed: true,
|
|
2422
|
+
restartRequired: true,
|
|
2423
|
+
messages: [
|
|
2424
|
+
"Updated local Haven MCP entry with Claude Code.",
|
|
2425
|
+
...verified ? ["Verified Claude Code MCP entry."] : []
|
|
2426
|
+
]
|
|
2427
|
+
};
|
|
2428
|
+
} catch (err) {
|
|
2429
|
+
return {
|
|
2430
|
+
hostedConfigured: false,
|
|
2431
|
+
signerConfigured: false,
|
|
2432
|
+
localMcpConfigured: false,
|
|
2433
|
+
runtimeMcpMode: "local_stdio",
|
|
2434
|
+
target: "Claude Code MCP config",
|
|
2435
|
+
changed: false,
|
|
2436
|
+
restartRequired: true,
|
|
2437
|
+
messages: [
|
|
2438
|
+
`Could not update Claude Code MCP config: ${err instanceof Error ? err.message : String(err)}`,
|
|
2439
|
+
"Install Claude Code or rerun the Haven setup command inside a Claude Code terminal."
|
|
2440
|
+
],
|
|
2441
|
+
errorCode: "claude_code_config_failed"
|
|
2442
|
+
};
|
|
1589
2443
|
}
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
2444
|
+
}
|
|
2445
|
+
async function writeHostedRuntimeConfig(deps, input, signerCommand) {
|
|
2446
|
+
if (input.runtime === "claude-code") {
|
|
2447
|
+
return configureClaudeCodeHosted(deps, input, signerCommand);
|
|
2448
|
+
}
|
|
2449
|
+
return writeRuntimeConfig({
|
|
2450
|
+
runtime: input.runtime,
|
|
2451
|
+
hostedMcpUrl: input.hostedMcpUrl,
|
|
2452
|
+
apiKey: input.apiKey,
|
|
2453
|
+
identityPath: input.identityPath,
|
|
2454
|
+
signerPath: input.signerPath,
|
|
2455
|
+
serverName: input.serverName,
|
|
2456
|
+
credentialDirectory: input.credentialDirectory,
|
|
2457
|
+
signerCommand,
|
|
2458
|
+
homeDir: deps.homeDir,
|
|
2459
|
+
mode: "hosted"
|
|
1597
2460
|
});
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
2461
|
+
}
|
|
2462
|
+
async function configureClaudeCodeHosted(deps, input, signerCommand) {
|
|
2463
|
+
const runCommand = deps.runCommand ?? defaultRunCommand;
|
|
2464
|
+
const hostedJson = JSON.stringify({
|
|
2465
|
+
type: "http",
|
|
2466
|
+
url: input.hostedMcpUrl,
|
|
2467
|
+
headers: { Authorization: `Bearer ${input.apiKey}` }
|
|
2468
|
+
});
|
|
2469
|
+
const signerJson = JSON.stringify({
|
|
2470
|
+
type: "stdio",
|
|
2471
|
+
command: signerCommand?.command ?? "npx",
|
|
2472
|
+
args: signerCommand?.args ?? ["-y", signerPackageSpec(), "--credentials", input.signerPath],
|
|
2473
|
+
env: {}
|
|
2474
|
+
});
|
|
2475
|
+
try {
|
|
2476
|
+
const names = serverNamesFor(input.serverName);
|
|
2477
|
+
await runCommand("claude", ["mcp", "remove", names.hosted]).catch(() => void 0);
|
|
2478
|
+
await runCommand("claude", ["mcp", "remove", names.signer]).catch(() => void 0);
|
|
2479
|
+
await runCommand("claude", ["mcp", "add-json", names.hosted, hostedJson, "--scope", "user"]);
|
|
2480
|
+
await runCommand("claude", ["mcp", "add-json", names.signer, signerJson, "--scope", "user"]);
|
|
2481
|
+
const verified = await runCommand("claude", ["mcp", "get", names.hosted]).then(() => true).catch(() => false);
|
|
2482
|
+
return {
|
|
2483
|
+
hostedConfigured: true,
|
|
2484
|
+
signerConfigured: true,
|
|
2485
|
+
localMcpConfigured: false,
|
|
2486
|
+
runtimeMcpMode: "hosted_plus_signer",
|
|
2487
|
+
target: "Claude Code MCP config",
|
|
2488
|
+
changed: true,
|
|
2489
|
+
restartRequired: true,
|
|
2490
|
+
messages: [
|
|
2491
|
+
"Updated hosted Haven MCP and local signer entries with Claude Code.",
|
|
2492
|
+
...verified ? ["Verified Claude Code MCP entry."] : []
|
|
2493
|
+
]
|
|
2494
|
+
};
|
|
2495
|
+
} catch (err) {
|
|
2496
|
+
return {
|
|
2497
|
+
hostedConfigured: false,
|
|
2498
|
+
signerConfigured: false,
|
|
2499
|
+
localMcpConfigured: false,
|
|
2500
|
+
runtimeMcpMode: "hosted_plus_signer",
|
|
2501
|
+
target: "Claude Code MCP config",
|
|
2502
|
+
changed: false,
|
|
2503
|
+
restartRequired: true,
|
|
2504
|
+
messages: [
|
|
2505
|
+
`Could not update Claude Code MCP config: ${err instanceof Error ? err.message : String(err)}`,
|
|
2506
|
+
"Install Claude Code or rerun the Haven setup command inside a Claude Code terminal."
|
|
2507
|
+
],
|
|
2508
|
+
errorCode: "claude_code_config_failed"
|
|
2509
|
+
};
|
|
1617
2510
|
}
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
...probe.status === "ok" ? {} : {
|
|
1627
|
-
repair: probe.status === "unauthorized" ? `The stored API key was rejected \u2014 re-run the full setup with a fresh token: ${RERUN} --setup <token>.` : "Check network access to the hosted MCP URL, then re-run --doctor."
|
|
1628
|
-
}
|
|
1629
|
-
});
|
|
1630
|
-
} else {
|
|
1631
|
-
checks.push({
|
|
1632
|
-
id: "hosted_mcp",
|
|
1633
|
-
label: "Hosted Haven MCP",
|
|
1634
|
-
ok: false,
|
|
1635
|
-
detail: "No stored API key / hosted MCP URL to probe with.",
|
|
1636
|
-
repair: `Re-run the full setup: ${RERUN} --setup <token>.`
|
|
1637
|
-
});
|
|
2511
|
+
}
|
|
2512
|
+
async function defaultRunCommand(command, args) {
|
|
2513
|
+
await execFileAsync3(command, args, { timeout: 1e4 });
|
|
2514
|
+
}
|
|
2515
|
+
function buildProbeResult(mode, hostedConfigured, hostedStatus, signerReady, localMcpReady, localMcpProbeStatus) {
|
|
2516
|
+
if (mode === "local_stdio") {
|
|
2517
|
+
if (localMcpReady) return "local_stdio_mcp_ready";
|
|
2518
|
+
return localMcpProbeStatus ? `local_stdio_mcp_${localMcpProbeStatus}` : "local_stdio_mcp_unavailable";
|
|
1638
2519
|
}
|
|
1639
|
-
const
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
);
|
|
1646
|
-
if (
|
|
1647
|
-
|
|
1648
|
-
id: "identity_match",
|
|
1649
|
-
label: "Hosted identity matches the local signing key",
|
|
1650
|
-
ok: false,
|
|
1651
|
-
detail: probe.status === "unauthorized" ? "The stored API key was rejected, so the agent it authenticates as cannot be compared with the local signing key." : `Could not read the hosted identity (${probe.status}) \u2014 the comparison did not happen, so it cannot be reported as a match.`,
|
|
1652
|
-
repair: probe.status === "unauthorized" ? `Re-run the full setup with a fresh token: ${RERUN} --setup <token>.` : `Restore network access to the Haven API, then re-run: ${RERUN} --doctor --runtime ${input.runtime}`
|
|
1653
|
-
});
|
|
1654
|
-
} else if (!localDelegate) {
|
|
1655
|
-
checks.push({
|
|
1656
|
-
id: "identity_match",
|
|
1657
|
-
label: "Hosted identity matches the local signing key",
|
|
1658
|
-
ok: false,
|
|
1659
|
-
detail: "signer.json holds no delegate_address to compare against the hosted identity.",
|
|
1660
|
-
repair: `Re-run the full setup with a fresh token: ${RERUN} --setup <token>.`
|
|
1661
|
-
});
|
|
2520
|
+
const hostedPart = hostedConfigured ? `hosted_${hostedStatus}` : "hosted_not_configured";
|
|
2521
|
+
const signerPart = signerReady ? "local_signer_ready" : "local_signer_unavailable";
|
|
2522
|
+
return `${hostedPart}_${signerPart}`.slice(0, 120);
|
|
2523
|
+
}
|
|
2524
|
+
async function resolveLocalMcpConsent(input, messages) {
|
|
2525
|
+
if (input.ackLocalTools || input.ackSigner) {
|
|
2526
|
+
const status = await acknowledgeLocalMcpConsent(input.identityPath, input.signerPath, (message) => messages.push(message));
|
|
2527
|
+
if (status.acknowledged) {
|
|
2528
|
+
messages.push("Prepared the local Haven tools acknowledgement.");
|
|
1662
2529
|
} else {
|
|
1663
|
-
|
|
1664
|
-
checks.push({
|
|
1665
|
-
id: "identity_match",
|
|
1666
|
-
label: "Hosted identity matches the local signing key",
|
|
1667
|
-
ok: same,
|
|
1668
|
-
detail: same ? `The stored API key authenticates as the agent whose signing key is in this directory (${shortAddress(localDelegate)}).` : `MISMATCH: the stored API key authenticates as agent ${probe.agentId ?? "unknown"} with delegate ${shortAddress(probe.delegateAddress ?? "unknown")}, but signer.json here holds ${shortAddress(localDelegate)}. This runtime would quote as one agent and sign as another.`,
|
|
1669
|
-
...same ? {} : {
|
|
1670
|
-
repair: `Re-run setup for this agent so its API key and signing key come from one run: ${RERUN} --setup <token>. Do not hand-edit either file.`
|
|
1671
|
-
}
|
|
1672
|
-
});
|
|
2530
|
+
messages.push("Local Haven tools acknowledgement still needs attention.");
|
|
1673
2531
|
}
|
|
2532
|
+
return status;
|
|
1674
2533
|
}
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
detail: "The local-tools consent is not acknowledged, so the signer refuses to start (by design).",
|
|
1683
|
-
repair: `Run: ${RERUN} --ack-local-tools --setup <token> (or re-run your original setup command with --ack-local-tools).`
|
|
1684
|
-
});
|
|
2534
|
+
return getLocalMcpConsentStatus(input.identityPath, input.signerPath);
|
|
2535
|
+
}
|
|
2536
|
+
async function resolveSignerConsent(input, messages) {
|
|
2537
|
+
if (input.ackSigner || input.ackLocalTools) {
|
|
2538
|
+
const status = await acknowledgeLocalSignerConsent(input.signerPath, (message) => messages.push(message));
|
|
2539
|
+
if (status.acknowledged) {
|
|
2540
|
+
messages.push("Prepared the local Haven signer acknowledgement.");
|
|
1685
2541
|
} else {
|
|
1686
|
-
|
|
1687
|
-
sidecar.wrapper_path,
|
|
1688
|
-
[],
|
|
1689
|
-
MCP_RUNTIME_MANIFEST.requiredSignerTools
|
|
1690
|
-
);
|
|
1691
|
-
const experimental = probe.capabilities?.experimental ?? probe.capabilities;
|
|
1692
|
-
const compat = experimental?.["haven/signer-compatibility"];
|
|
1693
|
-
signerCapabilities = compat ? { "haven/signer-compatibility": compat } : void 0;
|
|
1694
|
-
const compatDetail = compat ? ` Compat: x402 expected-context v${JSON.stringify(compat.x402_expected_context_versions ?? "?")}.` : "";
|
|
1695
|
-
checks.push({
|
|
1696
|
-
id: "signer_process",
|
|
1697
|
-
label: "Signer stdio handshake",
|
|
1698
|
-
ok: probe.status === "ok",
|
|
1699
|
-
detail: probe.status === "ok" ? `Signer started, listed ${probe.toolNames?.length ?? 0} tools${probe.serverInfo?.version ? ` (v${probe.serverInfo.version})` : ""}.${compatDetail}` : `Handshake failed: ${probe.status}.`,
|
|
1700
|
-
...probe.status === "ok" ? {} : { repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}` }
|
|
1701
|
-
});
|
|
2542
|
+
messages.push("Local Haven signer acknowledgement still needs attention.");
|
|
1702
2543
|
}
|
|
1703
|
-
|
|
1704
|
-
checks.push({
|
|
1705
|
-
id: "signer_process",
|
|
1706
|
-
label: "Signer stdio handshake",
|
|
1707
|
-
ok: false,
|
|
1708
|
-
detail: "Skipped \u2014 no prepared signer runtime to probe.",
|
|
1709
|
-
repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}`
|
|
1710
|
-
});
|
|
2544
|
+
return status;
|
|
1711
2545
|
}
|
|
1712
|
-
return
|
|
2546
|
+
return getLocalSignerConsentStatus(input.signerPath);
|
|
1713
2547
|
}
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
const { directory, others } = await discoverCredentialDirectory(homeDir, input.credentialsDir);
|
|
1719
|
-
const configPath = runtimeConfigPathFor(input.runtime, homeDir);
|
|
1720
|
-
let configText = null;
|
|
1721
|
-
if (configPath !== null) {
|
|
1722
|
-
try {
|
|
1723
|
-
configText = await readFile(configPath, "utf8");
|
|
1724
|
-
} catch {
|
|
1725
|
-
configText = null;
|
|
1726
|
-
}
|
|
1727
|
-
}
|
|
1728
|
-
const allDirectories = directory ? [directory, ...others] : others;
|
|
1729
|
-
let bareOwnerExists = false;
|
|
1730
|
-
for (const dir of allDirectories) {
|
|
1731
|
-
const sidecar = await readRuntimeSidecar(dir);
|
|
1732
|
-
if (!sidecar?.server_name && sidecar?.wrapper_path && configText?.includes(sidecar.wrapper_path)) {
|
|
1733
|
-
bareOwnerExists = true;
|
|
1734
|
-
break;
|
|
1735
|
-
}
|
|
1736
|
-
}
|
|
1737
|
-
const inventory = [];
|
|
1738
|
-
const capabilitiesByDirectory = /* @__PURE__ */ new Map();
|
|
1739
|
-
const primaryChecksById = /* @__PURE__ */ new Map();
|
|
1740
|
-
for (const dir of allDirectories) {
|
|
1741
|
-
const identity = await readIdentity(dir);
|
|
1742
|
-
const sidecar = await readRuntimeSidecar(dir);
|
|
1743
|
-
const tombstone = await readAgentTombstone(dir);
|
|
1744
|
-
const slug = sidecar?.server_name;
|
|
1745
|
-
const names = serverNamesFor(slug);
|
|
1746
|
-
if (!identity?.api_key) {
|
|
1747
|
-
inventory.push({
|
|
1748
|
-
...slug ? { slug } : {},
|
|
1749
|
-
...tombstone?.agent_id ? { agentId: tombstone.agent_id } : {},
|
|
1750
|
-
directory: dir,
|
|
1751
|
-
classification: tombstone ? "retired" : "orphaned",
|
|
1752
|
-
checks: []
|
|
1753
|
-
});
|
|
1754
|
-
continue;
|
|
1755
|
-
}
|
|
1756
|
-
const wired = agentIsWired(configText, names, slug, identity, sidecar, dir === directory, bareOwnerExists);
|
|
1757
|
-
const entry = {
|
|
1758
|
-
...slug ? { slug } : {},
|
|
1759
|
-
...identity.agent_id ? { agentId: identity.agent_id } : {},
|
|
1760
|
-
directory: dir,
|
|
1761
|
-
classification: wired ? "wired" : "superseded",
|
|
1762
|
-
checks: []
|
|
1763
|
-
};
|
|
1764
|
-
if (wired) {
|
|
1765
|
-
const result = await checksForAgent({ directory: dir, identity, sidecar }, input, deps);
|
|
1766
|
-
entry.checks = result.checks;
|
|
1767
|
-
capabilitiesByDirectory.set(dir, result.signerCapabilities);
|
|
1768
|
-
}
|
|
1769
|
-
inventory.push(entry);
|
|
1770
|
-
}
|
|
1771
|
-
const wiredDirectories = inventory.filter((entry) => entry.classification === "wired").map((entry) => entry.directory);
|
|
1772
|
-
const primaryDirectory = input.credentialsDir ? directory : wiredDirectories.includes(directory ?? "") ? directory : wiredDirectories[0] ?? directory;
|
|
1773
|
-
if (primaryDirectory) {
|
|
1774
|
-
const primaryEntry = inventory.find((entry) => entry.directory === primaryDirectory);
|
|
1775
|
-
signerCapabilities = capabilitiesByDirectory.get(primaryDirectory);
|
|
1776
|
-
for (const check of primaryEntry?.checks ?? []) primaryChecksById.set(check.id, check);
|
|
1777
|
-
}
|
|
1778
|
-
if (!primaryDirectory) {
|
|
1779
|
-
checks.push({
|
|
1780
|
-
id: "credentials",
|
|
1781
|
-
label: "Agent credentials",
|
|
1782
|
-
ok: false,
|
|
1783
|
-
detail: "No agent credential directory with an identity.json under ~/.haven/agents.",
|
|
1784
|
-
repair: `Run the full setup once: ${RERUN} --setup <token from the Haven dashboard>.`
|
|
1785
|
-
});
|
|
1786
|
-
} else {
|
|
1787
|
-
const primaryIdentity = await readIdentity(primaryDirectory);
|
|
1788
|
-
const primarySidecar = await readRuntimeSidecar(primaryDirectory);
|
|
1789
|
-
if (primaryChecksById.size === 0) {
|
|
1790
|
-
const result = await checksForAgent(
|
|
1791
|
-
{ directory: primaryDirectory, identity: primaryIdentity, sidecar: primarySidecar },
|
|
1792
|
-
input,
|
|
1793
|
-
deps
|
|
1794
|
-
);
|
|
1795
|
-
signerCapabilities = result.signerCapabilities;
|
|
1796
|
-
for (const check of result.checks) primaryChecksById.set(check.id, check);
|
|
1797
|
-
}
|
|
1798
|
-
for (const id of ["credentials", "signer_runtime"]) {
|
|
1799
|
-
const check = primaryChecksById.get(id);
|
|
1800
|
-
if (check) checks.push(check);
|
|
1801
|
-
}
|
|
1802
|
-
}
|
|
1803
|
-
if (configPath === null) {
|
|
1804
|
-
checks.push({
|
|
1805
|
-
id: "runtime_config",
|
|
1806
|
-
label: "Runtime MCP config",
|
|
1807
|
-
ok: true,
|
|
1808
|
-
detail: `Runtime '${input.runtime}' has no file-based config the connector owns (CLI-managed) \u2014 skipping the file check.`
|
|
1809
|
-
});
|
|
1810
|
-
} else if (configText === null) {
|
|
1811
|
-
checks.push({
|
|
1812
|
-
id: "runtime_config",
|
|
1813
|
-
label: "Runtime MCP config",
|
|
1814
|
-
ok: false,
|
|
1815
|
-
detail: `No runtime config at ${configPath}.`,
|
|
1816
|
-
repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}`
|
|
1817
|
-
});
|
|
1818
|
-
} else {
|
|
1819
|
-
const primaryIdentity = await readIdentity(primaryDirectory ?? "");
|
|
1820
|
-
const primarySidecar = primaryDirectory ? await readRuntimeSidecar(primaryDirectory) : null;
|
|
1821
|
-
const hasHaven = primaryIdentity?.hosted_mcp_url ? configText.includes(primaryIdentity.hosted_mcp_url) : configText.includes("haven");
|
|
1822
|
-
const signerViaNpx = configText.includes("@haven_ai/signer");
|
|
1823
|
-
const wrapperReferenced = primarySidecar ? configText.includes(primarySidecar.wrapper_path) : false;
|
|
1824
|
-
const ok = hasHaven && !signerViaNpx && (primarySidecar ? wrapperReferenced : true);
|
|
1825
|
-
checks.push({
|
|
1826
|
-
id: "runtime_config",
|
|
1827
|
-
label: "Runtime MCP config",
|
|
1828
|
-
ok,
|
|
1829
|
-
detail: ok ? `Config at ${configPath} references the hosted server and the prepared signer wrapper.` : signerViaNpx ? `Config at ${configPath} still launches the signer via npx \u2014 the pre-#1586 shape that cannot start under a 120s startup timeout.` : `Config at ${configPath} is missing the Haven entries${primarySidecar && !wrapperReferenced ? " (or references a different signer wrapper)" : ""}.`,
|
|
1830
|
-
...ok ? {} : { repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}` }
|
|
1831
|
-
});
|
|
1832
|
-
}
|
|
1833
|
-
for (const id of ["hosted_mcp", "identity_match"]) {
|
|
1834
|
-
const check = primaryChecksById.get(id);
|
|
1835
|
-
if (check) checks.push(check);
|
|
1836
|
-
}
|
|
1837
|
-
const otherEntries = inventory.filter((entry) => entry.directory !== primaryDirectory);
|
|
1838
|
-
if (otherEntries.length > 0) {
|
|
1839
|
-
const live = [];
|
|
1840
|
-
const revoked = [];
|
|
1841
|
-
const unverifiable = [];
|
|
1842
|
-
const retired = [];
|
|
1843
|
-
for (const entry of otherEntries) {
|
|
1844
|
-
const identity = await readIdentity(entry.directory);
|
|
1845
|
-
const tombstone = await readAgentTombstone(entry.directory);
|
|
1846
|
-
const otherAgent = identity?.agent_id ?? tombstone?.agent_id ?? basename(entry.directory);
|
|
1847
|
-
const otherUrl = identity?.hosted_mcp_url ?? (identity?.api_url ? `${identity.api_url}/mcp` : void 0);
|
|
1848
|
-
if (!identity?.api_key || !otherUrl) {
|
|
1849
|
-
if (tombstone) retired.push(`${otherAgent} (retired ${tombstone.retired_at})`);
|
|
1850
|
-
else unverifiable.push(`${otherAgent} (no stored key/URL to probe)`);
|
|
1851
|
-
continue;
|
|
1852
|
-
}
|
|
1853
|
-
const suffix = tombstone ? " [tombstoned \u2014 key material still present]" : "";
|
|
1854
|
-
const probe = await (deps.probeHosted ?? probeHostedMcpTools)(identity.api_key, otherUrl, deps.fetch);
|
|
1855
|
-
if (probe.status === "ok") live.push({ label: `${otherAgent}${suffix}`, entry });
|
|
1856
|
-
else if (probe.status === "unauthorized") revoked.push(`${otherAgent}${suffix}`);
|
|
1857
|
-
else unverifiable.push(`${otherAgent} (${probe.status})${suffix}`);
|
|
1858
|
-
}
|
|
1859
|
-
const parts = [];
|
|
1860
|
-
if (live.length > 0) parts.push(`STILL SPEND-CAPABLE: ${live.map((item) => item.label).join(", ")}`);
|
|
1861
|
-
if (revoked.length > 0) parts.push(`already revoked: ${revoked.join(", ")}`);
|
|
1862
|
-
if (retired.length > 0) parts.push(`tombstoned (keys removed): ${retired.join(", ")}`);
|
|
1863
|
-
if (unverifiable.length > 0) parts.push(`could not verify: ${unverifiable.join(", ")}`);
|
|
1864
|
-
const supersededLive = live.filter((item) => item.entry.classification !== "wired").map((item) => item.label);
|
|
1865
|
-
checks.push({
|
|
1866
|
-
id: "superseded_agents",
|
|
1867
|
-
label: "Superseded agent credentials",
|
|
1868
|
-
ok: supersededLive.length === 0,
|
|
1869
|
-
detail: supersededLive.length > 0 ? `${otherEntries.length} other credential dir(s) found \u2014 ${parts.join("; ")}. A host started before your latest setup keeps authenticating (and spending) as the old agent.` : `${otherEntries.length} other credential dir(s) found \u2014 ${parts.join("; ")}.`,
|
|
1870
|
-
...supersededLive.length > 0 ? {
|
|
1871
|
-
repair: `Revoke ${supersededLive.join(", ")} on the Haven agent page, then remove the old director(y/ies) under ~/.haven/agents. Connect never revokes or deletes for you.`
|
|
1872
|
-
} : {}
|
|
1873
|
-
});
|
|
1874
|
-
}
|
|
1875
|
-
const signerProcess = primaryChecksById.get("signer_process");
|
|
1876
|
-
if (signerProcess) checks.push(signerProcess);
|
|
1877
|
-
const restart = restartRequiredForRuntime(input.runtime, deps.env);
|
|
1878
|
-
checks.push({
|
|
1879
|
-
id: "restart",
|
|
1880
|
-
label: "Runtime restart",
|
|
1881
|
-
ok: true,
|
|
1882
|
-
detail: restart ? "This runtime loads MCP config at startup \u2014 restart it after any repair before expecting the tools to appear." : "No restart requirement known for this runtime."
|
|
1883
|
-
});
|
|
1884
|
-
const wiredOk = inventory.filter((entry) => entry.classification === "wired").every((entry) => entry.checks.every((check) => check.ok));
|
|
1885
|
-
return {
|
|
1886
|
-
version: 1,
|
|
1887
|
-
ok: checks.every((check) => check.ok) && wiredOk,
|
|
1888
|
-
runtime: input.runtime,
|
|
1889
|
-
credentialDirectory: primaryDirectory,
|
|
1890
|
-
checks,
|
|
1891
|
-
agents: inventory,
|
|
1892
|
-
...signerCapabilities ? { signerCapabilities } : {}
|
|
1893
|
-
};
|
|
1894
|
-
}
|
|
1895
|
-
async function runRepair(input, deps = {}) {
|
|
1896
|
-
const homeDir = deps.homeDir ?? homedir();
|
|
1897
|
-
const messages = [];
|
|
1898
|
-
const { directory, others } = await discoverCredentialDirectory(homeDir, input.credentialsDir);
|
|
1899
|
-
if (others.length > 0) {
|
|
1900
|
-
messages.push(`Note: ${others.length} other agent credential dir(s) exist \u2014 run --doctor for their status.`);
|
|
1901
|
-
}
|
|
1902
|
-
if (!directory) {
|
|
1903
|
-
return {
|
|
1904
|
-
ok: false,
|
|
1905
|
-
messages: [`No agent credentials found to repair \u2014 run the full setup: ${RERUN} --setup <token>.`]
|
|
1906
|
-
};
|
|
1907
|
-
}
|
|
1908
|
-
let identity;
|
|
1909
|
-
try {
|
|
1910
|
-
identity = JSON.parse(await readFile(join(directory, "identity.json"), "utf8"));
|
|
1911
|
-
} catch {
|
|
1912
|
-
return { ok: false, messages: ["identity.json is unreadable \u2014 re-run the full setup with a fresh token."] };
|
|
1913
|
-
}
|
|
1914
|
-
if (!identity.api_key || !(identity.hosted_mcp_url || identity.api_url)) {
|
|
1915
|
-
return { ok: false, messages: ["identity.json lacks the stored API key / hosted URL \u2014 re-run the full setup."] };
|
|
1916
|
-
}
|
|
1917
|
-
const configPath = runtimeConfigPathFor(input.runtime, homeDir);
|
|
1918
|
-
if (configPath) {
|
|
1919
|
-
try {
|
|
1920
|
-
const existing = await readFile(configPath, "utf8");
|
|
1921
|
-
if (existing.includes("bin/haven-mcp") || existing.includes(".haven/mcp-runtime")) {
|
|
1922
|
-
return {
|
|
1923
|
-
ok: false,
|
|
1924
|
-
messages: [
|
|
1925
|
-
`The config at ${configPath} is the LOCAL-stdio topology (--local). Repair currently rewrites only the hosted+signer shape and will not touch it.`,
|
|
1926
|
-
"Re-run your original setup command (with --local) to repair a local-stdio install."
|
|
1927
|
-
]
|
|
1928
|
-
};
|
|
1929
|
-
}
|
|
1930
|
-
} catch {
|
|
1931
|
-
}
|
|
1932
|
-
}
|
|
1933
|
-
const signerPath = join(directory, "signer.json");
|
|
1934
|
-
const prepared = await prepareSignerRuntime(
|
|
1935
|
-
{ credentialDirectory: directory, signerPath, homeDir },
|
|
1936
|
-
{ runCommand: deps.runCommand }
|
|
1937
|
-
);
|
|
1938
|
-
messages.push(...prepared.messages);
|
|
1939
|
-
const configResult = await writeRuntimeConfig({
|
|
1940
|
-
runtime: input.runtime,
|
|
1941
|
-
hostedMcpUrl: identity.hosted_mcp_url ?? `${identity.api_url}/mcp`,
|
|
1942
|
-
apiKey: identity.api_key,
|
|
1943
|
-
identityPath: join(directory, "identity.json"),
|
|
1944
|
-
signerPath,
|
|
1945
|
-
credentialDirectory: directory,
|
|
1946
|
-
signerCommand: { command: prepared.command, args: prepared.args },
|
|
1947
|
-
homeDir,
|
|
1948
|
-
mode: "hosted"
|
|
1949
|
-
});
|
|
1950
|
-
messages.push(...configResult.messages);
|
|
1951
|
-
messages.push("Repair complete \u2014 restart the runtime, then verify with --doctor.");
|
|
1952
|
-
return { ok: true, messages };
|
|
1953
|
-
}
|
|
1954
|
-
var RERUN;
|
|
1955
|
-
var init_doctor = __esm({
|
|
1956
|
-
"src/doctor.ts"() {
|
|
1957
|
-
init_runtime_manifest();
|
|
1958
|
-
init_probes();
|
|
1959
|
-
init_signer_runtime();
|
|
1960
|
-
init_config_writers();
|
|
1961
|
-
init_runtime_registry();
|
|
1962
|
-
init_signer_consent();
|
|
1963
|
-
init_tombstone();
|
|
1964
|
-
init_server_names();
|
|
1965
|
-
init_redact();
|
|
1966
|
-
RERUN = "npx @haven_ai/connect@alpha";
|
|
1967
|
-
}
|
|
1968
|
-
});
|
|
1969
|
-
|
|
1970
|
-
// src/api.ts
|
|
1971
|
-
function createConnectApiClient(baseUrl, fetchImpl = fetch) {
|
|
1972
|
-
const root = baseUrl.replace(/\/+$/, "");
|
|
1973
|
-
return {
|
|
1974
|
-
resolveSetup: (input) => request(fetchImpl, `${root}/agent-connection-setups/resolve`, {
|
|
1975
|
-
method: "POST",
|
|
1976
|
-
body: JSON.stringify({
|
|
1977
|
-
setup_token: input.setupToken,
|
|
1978
|
-
connector_version: input.connectorVersion,
|
|
1979
|
-
runtime: input.runtime
|
|
1980
|
-
})
|
|
1981
|
-
}),
|
|
1982
|
-
registerSetup: (input) => request(fetchImpl, `${root}/agent-connection-setups/register`, {
|
|
1983
|
-
method: "POST",
|
|
1984
|
-
body: JSON.stringify({
|
|
1985
|
-
setup_token: input.setupToken,
|
|
1986
|
-
challenge_id: input.challengeId,
|
|
1987
|
-
delegate_address: input.delegateAddress,
|
|
1988
|
-
proof_signature: input.proofSignature,
|
|
1989
|
-
api_key_hash: input.apiKeyHash,
|
|
1990
|
-
api_key_prefix: input.apiKeyPrefix,
|
|
1991
|
-
runtime: input.runtime,
|
|
1992
|
-
connector_version: input.connectorVersion,
|
|
1993
|
-
connector_context: input.connectorContext,
|
|
1994
|
-
install_capabilities: input.installCapabilities && {
|
|
1995
|
-
can_write_runtime_config: input.installCapabilities.canWriteRuntimeConfig,
|
|
1996
|
-
restart_required: input.installCapabilities.restartRequired
|
|
1997
|
-
}
|
|
1998
|
-
})
|
|
1999
|
-
}),
|
|
2000
|
-
getConnectorStatus: (setupId, apiKey) => request(fetchImpl, `${root}/agent-connection-setups/${encodeURIComponent(setupId)}/connector-status`, {
|
|
2001
|
-
method: "GET",
|
|
2002
|
-
headers: { Authorization: `Bearer ${apiKey}` }
|
|
2003
|
-
}),
|
|
2004
|
-
updateInstallStatus: async (setupId, apiKey, input) => {
|
|
2005
|
-
await request(fetchImpl, `${root}/agent-connection-setups/${encodeURIComponent(setupId)}/install-status`, {
|
|
2006
|
-
method: "POST",
|
|
2007
|
-
headers: { Authorization: `Bearer ${apiKey}` },
|
|
2008
|
-
body: JSON.stringify({
|
|
2009
|
-
runtime: input.runtime,
|
|
2010
|
-
connector_version: input.connectorVersion,
|
|
2011
|
-
runtime_mcp_mode: input.runtimeMcpMode,
|
|
2012
|
-
hosted_mcp_configured: input.hostedMcpConfigured,
|
|
2013
|
-
local_signer_configured: input.localSignerConfigured,
|
|
2014
|
-
local_mcp_configured: input.localMcpConfigured,
|
|
2015
|
-
credential_files_written: input.credentialFilesWritten,
|
|
2016
|
-
signer_acknowledged: input.signerAcknowledged,
|
|
2017
|
-
local_mcp_acknowledged: input.localMcpAcknowledged,
|
|
2018
|
-
activation_command_available: input.activationCommandAvailable,
|
|
2019
|
-
skill_installed: input.skillInstalled,
|
|
2020
|
-
probe_result: input.probeResult,
|
|
2021
|
-
restart_required: input.restartRequired,
|
|
2022
|
-
next_user_action: input.nextUserAction,
|
|
2023
|
-
error_code: input.errorCode ?? null,
|
|
2024
|
-
environment_label: input.environmentLabel
|
|
2025
|
-
})
|
|
2026
|
-
});
|
|
2027
|
-
}
|
|
2028
|
-
};
|
|
2029
|
-
}
|
|
2030
|
-
var ConnectRequestError = class extends Error {
|
|
2031
|
-
constructor(message, status) {
|
|
2032
|
-
super(message);
|
|
2033
|
-
this.status = status;
|
|
2034
|
-
this.name = "ConnectRequestError";
|
|
2035
|
-
}
|
|
2036
|
-
status;
|
|
2037
|
-
};
|
|
2038
|
-
async function request(fetchImpl, url, init) {
|
|
2039
|
-
const response = await fetchImpl(url, {
|
|
2040
|
-
...init,
|
|
2041
|
-
headers: {
|
|
2042
|
-
"Content-Type": "application/json",
|
|
2043
|
-
...init.headers ?? {}
|
|
2044
|
-
}
|
|
2045
|
-
});
|
|
2046
|
-
const text = await response.text();
|
|
2047
|
-
const body = text ? JSON.parse(text) : null;
|
|
2048
|
-
if (!response.ok) {
|
|
2049
|
-
const message = body?.error ?? body?.message ?? `${response.status} ${response.statusText}`;
|
|
2050
|
-
throw new ConnectRequestError(`Haven setup request failed: ${message}`, response.status);
|
|
2051
|
-
}
|
|
2052
|
-
return body;
|
|
2053
|
-
}
|
|
2054
|
-
function generateDelegateKey() {
|
|
2055
|
-
return delegateKeyFromPrivateKey(Wallet.createRandom().privateKey);
|
|
2056
|
-
}
|
|
2057
|
-
function delegateKeyFromPrivateKey(privateKey) {
|
|
2058
|
-
const wallet = new Wallet(privateKey);
|
|
2059
|
-
return {
|
|
2060
|
-
privateKey: wallet.privateKey,
|
|
2061
|
-
address: wallet.address,
|
|
2062
|
-
signChallenge: (message) => wallet.signMessage(message)
|
|
2063
|
-
};
|
|
2064
|
-
}
|
|
2065
|
-
function generateAgentApiKey() {
|
|
2066
|
-
return `sk_agent_${crypto.randomBytes(24).toString("hex")}`;
|
|
2067
|
-
}
|
|
2068
|
-
function hashAgentApiKey(apiKey) {
|
|
2069
|
-
return crypto.createHash("sha256").update(apiKey).digest("hex");
|
|
2070
|
-
}
|
|
2071
|
-
function agentApiKeyPrefix(apiKey) {
|
|
2072
|
-
return apiKey.slice(0, 12);
|
|
2073
|
-
}
|
|
2074
|
-
|
|
2075
|
-
// src/runtime.ts
|
|
2076
|
-
init_redact();
|
|
2077
|
-
init_server_names();
|
|
2078
|
-
async function preflightCredentialStorage(input = {}) {
|
|
2079
|
-
const directory = defaultCredentialRoot(input.baseDir);
|
|
2080
|
-
await mkdir(directory, { recursive: true, mode: 448 });
|
|
2081
|
-
await restrictPermissions(directory, 448, input.warn);
|
|
2082
|
-
const probePath = join(directory, `.haven-connect-preflight-${crypto.randomBytes(8).toString("hex")}`);
|
|
2083
|
-
try {
|
|
2084
|
-
await writeOwnerOnlyJson(probePath, { ok: true }, input.warn);
|
|
2085
|
-
} finally {
|
|
2086
|
-
await rm(probePath, { force: true }).catch(() => void 0);
|
|
2087
|
-
}
|
|
2088
|
-
return directory;
|
|
2548
|
+
function signerConsentErrorCode(signerCredentialReady, signerConsent) {
|
|
2549
|
+
if (!signerCredentialReady) return "local_signer_credential_unavailable";
|
|
2550
|
+
if (!signerConsent?.acknowledged) return "local_signer_ack_required";
|
|
2551
|
+
return void 0;
|
|
2089
2552
|
}
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
await restrictPermissions(directory, 448, input.warn);
|
|
2094
|
-
const identityPath = join(directory, "identity.json");
|
|
2095
|
-
const signerPath = join(directory, "signer.json");
|
|
2096
|
-
const agentPath = join(directory, "agent.json");
|
|
2097
|
-
await assertDoesNotExist(identityPath);
|
|
2098
|
-
await assertDoesNotExist(signerPath);
|
|
2099
|
-
await assertDoesNotExist(agentPath);
|
|
2100
|
-
await writeOwnerOnlyJson(
|
|
2101
|
-
signerPath,
|
|
2102
|
-
{
|
|
2103
|
-
delegate_key: input.delegateKey,
|
|
2104
|
-
delegate_address: input.delegateAddress,
|
|
2105
|
-
agent_id: input.agentId,
|
|
2106
|
-
safe_address: input.safeAddress,
|
|
2107
|
-
chain_id: input.chainId,
|
|
2108
|
-
network: input.network,
|
|
2109
|
-
x402_binding_signer: input.x402BindingSigner,
|
|
2110
|
-
note: "Local signer credential. Haven backend never receives this private key."
|
|
2111
|
-
},
|
|
2112
|
-
input.warn
|
|
2113
|
-
);
|
|
2114
|
-
try {
|
|
2115
|
-
await writeOwnerOnlyJson(
|
|
2116
|
-
identityPath,
|
|
2117
|
-
{
|
|
2118
|
-
api_key: input.apiKey,
|
|
2119
|
-
agent_id: input.agentId,
|
|
2120
|
-
safe_address: input.safeAddress,
|
|
2121
|
-
chain_id: input.chainId,
|
|
2122
|
-
network: input.network,
|
|
2123
|
-
api_url: input.apiUrl,
|
|
2124
|
-
hosted_mcp_url: input.hostedMcpUrl,
|
|
2125
|
-
agent_budget: input.agentBudget,
|
|
2126
|
-
note: "Haven API key identifies the agent only. It cannot spend without the local signer key and on-chain Haven wallet rules."
|
|
2127
|
-
},
|
|
2128
|
-
input.warn
|
|
2129
|
-
);
|
|
2130
|
-
} catch (err) {
|
|
2131
|
-
await rm(signerPath, { force: true }).catch(() => void 0);
|
|
2132
|
-
throw err;
|
|
2133
|
-
}
|
|
2134
|
-
try {
|
|
2135
|
-
await writeOwnerOnlyJson(
|
|
2136
|
-
agentPath,
|
|
2137
|
-
{
|
|
2138
|
-
agent_id: input.agentId,
|
|
2139
|
-
delegate_address: input.delegateAddress,
|
|
2140
|
-
safe_address: input.safeAddress,
|
|
2141
|
-
chain_id: input.chainId,
|
|
2142
|
-
network: input.network,
|
|
2143
|
-
agent_budget: input.agentBudget,
|
|
2144
|
-
note: "Non-secret orientation for the agent: public delegate/Haven wallet identity + configured budget. Contains no API key or signing key. For the live remaining budget, call haven_get_allowances."
|
|
2145
|
-
},
|
|
2146
|
-
input.warn
|
|
2147
|
-
);
|
|
2148
|
-
} catch (err) {
|
|
2149
|
-
await rm(signerPath, { force: true }).catch(() => void 0);
|
|
2150
|
-
await rm(identityPath, { force: true }).catch(() => void 0);
|
|
2151
|
-
await rm(agentPath, { force: true }).catch(() => void 0);
|
|
2152
|
-
throw err;
|
|
2153
|
-
}
|
|
2154
|
-
return { directory, identityPath, signerPath, agentPath };
|
|
2553
|
+
function signerProbeErrorCode(probe) {
|
|
2554
|
+
if (!probe || probe.status === "ok") return void 0;
|
|
2555
|
+
return `local_signer_probe_${probe.status}`;
|
|
2155
2556
|
}
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
await stat(join(directory, "identity.json"));
|
|
2160
|
-
} catch {
|
|
2161
|
-
return;
|
|
2162
|
-
}
|
|
2163
|
-
throw new Error(
|
|
2164
|
-
`The name "${serverName}" is already wired on this machine (${directory} holds credentials). Pick a different --name, or revoke and remove that agent first \u2014 connect never overwrites credentials.`
|
|
2165
|
-
);
|
|
2557
|
+
function hostedMcpErrorCode(hostedConfigured, hostedProbeStatus) {
|
|
2558
|
+
if (!hostedConfigured || hostedProbeStatus === "ok") return void 0;
|
|
2559
|
+
return `hosted_mcp_probe_${hostedProbeStatus}`;
|
|
2166
2560
|
}
|
|
2167
|
-
function
|
|
2168
|
-
|
|
2561
|
+
function localMcpErrorCode(signerCredentialReady, localMcpConsent, localMcpProbeStatus) {
|
|
2562
|
+
if (!signerCredentialReady) return "local_signer_credential_unavailable";
|
|
2563
|
+
if (!localMcpConsent?.acknowledged) return "local_mcp_ack_required";
|
|
2564
|
+
if (localMcpProbeStatus && localMcpProbeStatus !== "ok") return `local_mcp_probe_${localMcpProbeStatus}`;
|
|
2565
|
+
return void 0;
|
|
2169
2566
|
}
|
|
2170
|
-
function
|
|
2171
|
-
|
|
2567
|
+
function nextAction(runtime, restartMode, errorCode) {
|
|
2568
|
+
if (errorCode) return "return_to_haven_for_wallet_approval_then_finish_runtime_setup";
|
|
2569
|
+
if (restartMode === "hot-reload") return "return_to_haven_for_wallet_approval";
|
|
2570
|
+
if (runtime === "codex-cli" || runtime === "codex-desktop") return "return_to_haven_for_wallet_approval_then_restart_codex";
|
|
2571
|
+
if (runtime === "claude-code") return "return_to_haven_for_wallet_approval_then_restart_claude_code";
|
|
2572
|
+
if (restartMode === "restart-app") return "return_to_haven_for_wallet_approval_then_restart_app";
|
|
2573
|
+
if (restartMode === "restart-session") return "return_to_haven_for_wallet_approval_then_restart_agent_session";
|
|
2574
|
+
return "return_to_haven_for_wallet_approval_then_configure_runtime";
|
|
2172
2575
|
}
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
await writeFile(path, `${json}
|
|
2176
|
-
`, { mode: 384, flag: "wx" });
|
|
2177
|
-
await restrictPermissions(path, 384, warn);
|
|
2576
|
+
function supportsLocalMcp(runtime) {
|
|
2577
|
+
return runtime === "codex-cli" || runtime === "codex-desktop" || runtime === "claude-code";
|
|
2178
2578
|
}
|
|
2179
|
-
function
|
|
2180
|
-
|
|
2579
|
+
async function prepareRuntimeForLocalMcp(input, deps) {
|
|
2580
|
+
const prepare = deps.prepareLocalMcpRuntime ?? ((runtimeInput) => prepareLocalMcpRuntime(runtimeInput, { runCommand: deps.runCommand, onProgress: deps.onProgress }));
|
|
2581
|
+
return prepare({
|
|
2582
|
+
credentialDirectory: input.credentialDirectory,
|
|
2583
|
+
identityPath: input.identityPath,
|
|
2584
|
+
signerPath: input.signerPath,
|
|
2585
|
+
homeDir: deps.homeDir,
|
|
2586
|
+
serverName: input.serverName
|
|
2587
|
+
});
|
|
2181
2588
|
}
|
|
2182
|
-
function
|
|
2183
|
-
|
|
2589
|
+
async function prepareSignerForRuntime(input, deps) {
|
|
2590
|
+
const prepare = deps.prepareSignerRuntime ?? ((runtimeInput) => (
|
|
2591
|
+
// onProgress threaded through on purpose (#1586 review): without it the
|
|
2592
|
+
// install heartbeat was dead code in production and the console still
|
|
2593
|
+
// went silent for the whole cold install — the exact symptom the issue
|
|
2594
|
+
// set out to remove, at a longer timeout.
|
|
2595
|
+
prepareSignerRuntime(runtimeInput, { runCommand: deps.runCommand, onProgress: deps.onProgress })
|
|
2596
|
+
));
|
|
2597
|
+
return prepare({
|
|
2598
|
+
credentialDirectory: input.credentialDirectory,
|
|
2599
|
+
signerPath: input.signerPath,
|
|
2600
|
+
homeDir: deps.homeDir,
|
|
2601
|
+
serverName: input.serverName
|
|
2602
|
+
});
|
|
2184
2603
|
}
|
|
2185
|
-
async function
|
|
2604
|
+
async function runLocalMcpProbe(runtimeInstall, deps) {
|
|
2605
|
+
const probe = deps.probeLocalMcpTools ?? probeLocalMcpTools;
|
|
2186
2606
|
try {
|
|
2187
|
-
await
|
|
2188
|
-
} catch
|
|
2189
|
-
|
|
2190
|
-
throw err;
|
|
2607
|
+
return await probe(runtimeInstall.command, runtimeInstall.args, MCP_RUNTIME_MANIFEST.requiredTools);
|
|
2608
|
+
} catch {
|
|
2609
|
+
return { status: "process_error" };
|
|
2191
2610
|
}
|
|
2192
|
-
throw new Error(`Refusing to overwrite existing Haven credential file: ${path}`);
|
|
2193
2611
|
}
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
} catch (err) {
|
|
2198
|
-
warn?.(
|
|
2199
|
-
`Warning: could not restrict permissions on ${path} to ${mode.toString(8)}. Move this credential to a private location or run chmod ${mode.toString(8)} ${path}. ${err instanceof Error ? err.message : String(err)}`
|
|
2200
|
-
);
|
|
2612
|
+
function localRuntimePrepareErrorCode(err) {
|
|
2613
|
+
if (err && typeof err === "object" && "code" in err && err.code === "local_mcp_unsupported_node_version") {
|
|
2614
|
+
return "local_mcp_unsupported_node_version";
|
|
2201
2615
|
}
|
|
2616
|
+
return "local_mcp_runtime_install_failed";
|
|
2202
2617
|
}
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
hash: decision.hash,
|
|
2218
|
-
reason: decision.reason
|
|
2219
|
-
};
|
|
2220
|
-
} catch (err) {
|
|
2221
|
-
return {
|
|
2222
|
-
acknowledged: false,
|
|
2223
|
-
error: err instanceof Error ? err.message : String(err)
|
|
2224
|
-
};
|
|
2618
|
+
var execFileAsync3;
|
|
2619
|
+
var init_runtime_install = __esm({
|
|
2620
|
+
"src/runtime-install.ts"() {
|
|
2621
|
+
init_config_writers();
|
|
2622
|
+
init_server_names();
|
|
2623
|
+
init_local_mcp_consent();
|
|
2624
|
+
init_probes();
|
|
2625
|
+
init_local_mcp_runtime();
|
|
2626
|
+
init_signer_runtime();
|
|
2627
|
+
init_runtime_manifest();
|
|
2628
|
+
init_skill_install();
|
|
2629
|
+
init_runtime_registry();
|
|
2630
|
+
init_signer_consent();
|
|
2631
|
+
execFileAsync3 = promisify(execFile);
|
|
2225
2632
|
}
|
|
2633
|
+
});
|
|
2634
|
+
|
|
2635
|
+
// src/tombstone.ts
|
|
2636
|
+
var tombstone_exports = {};
|
|
2637
|
+
__export(tombstone_exports, {
|
|
2638
|
+
TOMBSTONE_FILENAME: () => TOMBSTONE_FILENAME,
|
|
2639
|
+
TOMBSTONE_MARKER: () => TOMBSTONE_MARKER,
|
|
2640
|
+
readAgentTombstone: () => readAgentTombstone,
|
|
2641
|
+
writeAgentTombstone: () => writeAgentTombstone
|
|
2642
|
+
});
|
|
2643
|
+
function tombstoneScript(info) {
|
|
2644
|
+
const lines = [
|
|
2645
|
+
`${TOMBSTONE_MARKER}: this Haven agent was retired.`,
|
|
2646
|
+
"",
|
|
2647
|
+
` agent: ${info.agent_id}`,
|
|
2648
|
+
` retired at: ${info.retired_at}`,
|
|
2649
|
+
` reason: ${info.reason}`,
|
|
2650
|
+
...info.replaced_by ? [` replaced by: ${info.replaced_by}`] : [],
|
|
2651
|
+
"",
|
|
2652
|
+
"This process is running with a wiring snapshot that predates the",
|
|
2653
|
+
"retirement \u2014 it loaded its MCP config at startup and has kept it since.",
|
|
2654
|
+
"Restart THIS host to pick up the current wiring. If several long-lived",
|
|
2655
|
+
"hosts are running (a gateway, a TUI worker, an editor), restart EVERY",
|
|
2656
|
+
"one of them: each holds the snapshot from its own start time, so after",
|
|
2657
|
+
"a chain of recreations each can be parked on a DIFFERENT old agent.",
|
|
2658
|
+
"",
|
|
2659
|
+
"Then verify with: npx @haven_ai/connect@alpha --doctor --runtime <runtime>"
|
|
2660
|
+
];
|
|
2661
|
+
return [
|
|
2662
|
+
"#!/usr/bin/env node",
|
|
2663
|
+
`// ${TOMBSTONE_MARKER} \u2014 written by @haven_ai/connect (#1681). Safe to delete`,
|
|
2664
|
+
"// once every long-lived MCP host on this machine has been restarted.",
|
|
2665
|
+
`process.stderr.write(${JSON.stringify(lines.join("\n") + "\n")})`,
|
|
2666
|
+
"process.exit(1)",
|
|
2667
|
+
""
|
|
2668
|
+
].join("\n");
|
|
2226
2669
|
}
|
|
2227
|
-
async function
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
const stored = await readLocalMcpAckFile(localMcpAckPath(identityPath));
|
|
2232
|
-
if (stored === hash) {
|
|
2233
|
-
return { acknowledged: true, hash, reason: "ack_file_match" };
|
|
2234
|
-
}
|
|
2235
|
-
return {
|
|
2236
|
-
acknowledged: false,
|
|
2237
|
-
hash,
|
|
2238
|
-
reason: stored ? "ack_file_mismatch" : "ack_file_missing"
|
|
2239
|
-
};
|
|
2240
|
-
} catch (err) {
|
|
2241
|
-
return {
|
|
2242
|
-
acknowledged: false,
|
|
2243
|
-
error: err instanceof Error ? err.message : String(err)
|
|
2244
|
-
};
|
|
2670
|
+
async function writeAgentTombstone(input) {
|
|
2671
|
+
const dirStat = await stat(input.directory).catch(() => null);
|
|
2672
|
+
if (!dirStat?.isDirectory()) {
|
|
2673
|
+
throw new Error(`Not a directory: ${input.directory} \u2014 nothing to tombstone.`);
|
|
2245
2674
|
}
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
}
|
|
2675
|
+
const info = {
|
|
2676
|
+
// reason / replaced_by are persisted to disk and re-emitted to the host's
|
|
2677
|
+
// MCP stderr log on EVERY stale probe, potentially for months — redact
|
|
2678
|
+
// like every other output path, at the write layer so any future caller
|
|
2679
|
+
// inherits it. (#1681 review, finding 1)
|
|
2680
|
+
agent_id: input.agentId,
|
|
2681
|
+
retired_at: input.retiredAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
2682
|
+
reason: redactSecrets(input.reason),
|
|
2683
|
+
...input.replacedBy ? { replaced_by: redactSecrets(input.replacedBy) } : {}
|
|
2256
2684
|
};
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
delegateAddress: credentials.delegateAddress,
|
|
2265
|
-
chainId: credentials.chainId,
|
|
2266
|
-
allowanceSummary: credentials.allowanceSummary
|
|
2267
|
-
},
|
|
2268
|
-
registeredToolNames()
|
|
2269
|
-
);
|
|
2685
|
+
const binDir = join(input.directory, "bin");
|
|
2686
|
+
await mkdir(binDir, { recursive: true });
|
|
2687
|
+
const wrapperPath = join(binDir, "haven-signer.mjs");
|
|
2688
|
+
await writeFile(wrapperPath, tombstoneScript(info), "utf8");
|
|
2689
|
+
await chmod(wrapperPath, 493);
|
|
2690
|
+
await writeFile(join(input.directory, TOMBSTONE_FILENAME), JSON.stringify(info, null, 2) + "\n", "utf8");
|
|
2691
|
+
return info;
|
|
2270
2692
|
}
|
|
2271
|
-
async function
|
|
2693
|
+
async function readAgentTombstone(directory) {
|
|
2272
2694
|
try {
|
|
2273
|
-
const parsed = JSON.parse(await readFile(
|
|
2274
|
-
|
|
2695
|
+
const parsed = JSON.parse(await readFile(join(directory, TOMBSTONE_FILENAME), "utf8"));
|
|
2696
|
+
if (typeof parsed?.agent_id !== "string") return null;
|
|
2697
|
+
return parsed;
|
|
2275
2698
|
} catch {
|
|
2276
2699
|
return null;
|
|
2277
2700
|
}
|
|
2278
2701
|
}
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
init_probes();
|
|
2286
|
-
|
|
2287
|
-
// src/local-mcp-runtime.ts
|
|
2288
|
-
init_signer_runtime();
|
|
2289
|
-
init_runtime_manifest();
|
|
2290
|
-
var execFileAsync2 = promisify(execFile);
|
|
2291
|
-
var UnsupportedNodeVersionError = class extends Error {
|
|
2292
|
-
code = "local_mcp_unsupported_node_version";
|
|
2293
|
-
nodeVersion;
|
|
2294
|
-
minimumNodeVersion;
|
|
2295
|
-
constructor(nodeVersion, minimumNodeVersion, subject = "Haven setup") {
|
|
2296
|
-
super(unsupportedNodeVersionMessage({ subject, nodeVersion, minimumNodeVersion }));
|
|
2297
|
-
this.name = "UnsupportedNodeVersionError";
|
|
2298
|
-
this.nodeVersion = nodeVersion;
|
|
2299
|
-
this.minimumNodeVersion = minimumNodeVersion;
|
|
2300
|
-
}
|
|
2301
|
-
};
|
|
2302
|
-
async function prepareLocalMcpRuntime(input, deps = {}) {
|
|
2303
|
-
assertSupportedNodeVersion(input.nodeVersion);
|
|
2304
|
-
const homeDir = input.homeDir ?? homedir();
|
|
2305
|
-
const runtimeDirectory = resolve(homeDir, ".haven", "mcp-runtime", MCP_RUNTIME_MANIFEST.mcpVersion);
|
|
2306
|
-
const npmCacheDirectory = resolve(homeDir, ".haven", "npm-cache");
|
|
2307
|
-
const cliPath = join(runtimeDirectory, "node_modules", "@haven_ai", "mcp", "dist", "cli.js");
|
|
2308
|
-
const messages = [];
|
|
2309
|
-
await mkdir(runtimeDirectory, { recursive: true, mode: 448 });
|
|
2310
|
-
await chmod(runtimeDirectory, 448).catch(() => void 0);
|
|
2311
|
-
await mkdir(npmCacheDirectory, { recursive: true, mode: 448 });
|
|
2312
|
-
await chmod(npmCacheDirectory, 448).catch(() => void 0);
|
|
2313
|
-
if (await installedRuntimeMatches2(runtimeDirectory, cliPath)) {
|
|
2314
|
-
messages.push(`Using existing local Haven MCP runtime ${mcpPackageSpec()}.`);
|
|
2315
|
-
} else {
|
|
2316
|
-
await installRuntimePackages2(runtimeDirectory, npmCacheDirectory, deps);
|
|
2317
|
-
messages.push(`Installed local Haven MCP runtime ${mcpPackageSpec()}.`);
|
|
2702
|
+
var TOMBSTONE_FILENAME, TOMBSTONE_MARKER;
|
|
2703
|
+
var init_tombstone = __esm({
|
|
2704
|
+
"src/tombstone.ts"() {
|
|
2705
|
+
init_redact();
|
|
2706
|
+
TOMBSTONE_FILENAME = "TOMBSTONE.json";
|
|
2707
|
+
TOMBSTONE_MARKER = "HAVEN-TOMBSTONE";
|
|
2318
2708
|
}
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2709
|
+
});
|
|
2710
|
+
|
|
2711
|
+
// src/rekey.ts
|
|
2712
|
+
var rekey_exports = {};
|
|
2713
|
+
__export(rekey_exports, {
|
|
2714
|
+
finishRekey: () => finishRekey,
|
|
2715
|
+
startRekey: () => startRekey
|
|
2716
|
+
});
|
|
2717
|
+
async function startRekey(options, deps = {}) {
|
|
2718
|
+
const now = deps.now ?? (() => Date.now());
|
|
2719
|
+
const stored = await readStoredCredentials(
|
|
2720
|
+
options.serverName,
|
|
2721
|
+
options.agentId,
|
|
2722
|
+
options.credentialsDir
|
|
2723
|
+
);
|
|
2724
|
+
const api = (deps.createApi ?? ((url) => createConnectApiClient(url)))(stored.apiUrl);
|
|
2725
|
+
const identity = await probeIdentity(api, stored.apiKey, "current");
|
|
2726
|
+
assertRekeyable(identity, stored);
|
|
2727
|
+
const key = (deps.generateKey ?? generateDelegateKey)();
|
|
2728
|
+
const startedAt = new Date(now()).toISOString();
|
|
2729
|
+
const expiresAt = new Date(now() + REKEY_PENDING_TTL_MS).toISOString();
|
|
2730
|
+
await writeRekeyPending(stored.directory, {
|
|
2731
|
+
agent_id: stored.agentId,
|
|
2732
|
+
new_delegate_address: key.address,
|
|
2733
|
+
new_delegate_key: key.privateKey,
|
|
2734
|
+
started_at: startedAt,
|
|
2735
|
+
expires_at: expiresAt
|
|
2334
2736
|
});
|
|
2335
|
-
|
|
2737
|
+
const finishCommand = [
|
|
2738
|
+
"npx @haven_ai/connect@alpha --rekey-finish",
|
|
2739
|
+
options.serverName ? `--name ${options.serverName}` : void 0,
|
|
2740
|
+
"--api-key <the key the dashboard showed you>",
|
|
2741
|
+
options.runtime ? `--runtime ${options.runtime}` : "--runtime <your runtime>"
|
|
2742
|
+
].filter(Boolean).join(" ");
|
|
2336
2743
|
return {
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2744
|
+
started: true,
|
|
2745
|
+
agentId: stored.agentId,
|
|
2746
|
+
directory: stored.directory,
|
|
2747
|
+
newDelegateAddress: key.address,
|
|
2748
|
+
expiresAt,
|
|
2749
|
+
messages: [
|
|
2750
|
+
`New signing key generated on this machine for agent ${identity.name || stored.agentId}.`,
|
|
2751
|
+
"",
|
|
2752
|
+
` New signing address: ${key.address}`,
|
|
2753
|
+
"",
|
|
2754
|
+
"The private half stays here \u2014 Haven never receives it, and there is no way to move it",
|
|
2755
|
+
"between machines. That is what keeps the account non-custodial.",
|
|
2756
|
+
"",
|
|
2757
|
+
'Next, on the Haven agent page: choose "Replace signing key" and paste that address.',
|
|
2758
|
+
"When it finishes it shows a new API key ONCE. Come back here and run:",
|
|
2759
|
+
"",
|
|
2760
|
+
` ${finishCommand}`,
|
|
2761
|
+
"",
|
|
2762
|
+
`Nothing has changed yet \u2014 the agent keeps working on its old key until you finish.`,
|
|
2763
|
+
`This pending re-key expires ${expiresAt}.`
|
|
2764
|
+
]
|
|
2344
2765
|
};
|
|
2345
2766
|
}
|
|
2346
|
-
function
|
|
2347
|
-
|
|
2348
|
-
|
|
2767
|
+
async function finishRekey(options, deps = {}) {
|
|
2768
|
+
const now = deps.now ?? (() => Date.now());
|
|
2769
|
+
if (!options.newApiKey) {
|
|
2770
|
+
throw new Error("--rekey-finish needs --api-key <key> \u2014 the one the Haven agent page showed once.");
|
|
2349
2771
|
}
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2772
|
+
const stored = await readStoredCredentials(
|
|
2773
|
+
options.serverName,
|
|
2774
|
+
options.agentId,
|
|
2775
|
+
options.credentialsDir
|
|
2776
|
+
);
|
|
2777
|
+
const pending = await readRekeyPending(stored.directory, now());
|
|
2778
|
+
if (pending.agent_id !== stored.agentId) {
|
|
2779
|
+
throw new Error(
|
|
2780
|
+
`The pending re-key at ${stored.directory} belongs to agent ${pending.agent_id}, but that directory now holds ${stored.agentId}. Refusing to write a key into the wrong agent.`
|
|
2781
|
+
);
|
|
2782
|
+
}
|
|
2783
|
+
const api = (deps.createApi ?? ((url) => createConnectApiClient(url)))(stored.apiUrl);
|
|
2784
|
+
const identity = await probeIdentity(api, options.newApiKey, "new");
|
|
2785
|
+
if (identity.id !== stored.agentId) {
|
|
2786
|
+
throw new Error(
|
|
2787
|
+
`That API key belongs to agent ${identity.id}, not ${stored.agentId}. Nothing was changed.`
|
|
2788
|
+
);
|
|
2789
|
+
}
|
|
2790
|
+
const onChain = (identity.delegate_address ?? "").toLowerCase();
|
|
2791
|
+
const expected = pending.new_delegate_address.toLowerCase();
|
|
2792
|
+
if (onChain !== expected) {
|
|
2793
|
+
throw new Error(
|
|
2794
|
+
`Haven says this agent's signing address is ${identity.delegate_address ?? "unset"}, but this machine generated ${pending.new_delegate_address}. Nothing was changed. Either the re-key on the agent page used a different address, or it has not finished yet.`
|
|
2795
|
+
);
|
|
2796
|
+
}
|
|
2797
|
+
await rewriteCredentialFiles({
|
|
2798
|
+
baseDir: options.credentialsDir,
|
|
2799
|
+
agentId: stored.agentId,
|
|
2800
|
+
serverName: options.serverName,
|
|
2801
|
+
apiKey: options.newApiKey,
|
|
2802
|
+
delegateKey: pending.new_delegate_key,
|
|
2803
|
+
delegateAddress: pending.new_delegate_address,
|
|
2804
|
+
safeAddress: stored.safeAddress ?? identity.safe_address ?? void 0,
|
|
2805
|
+
chainId: stored.chainId ?? identity.chain_id ?? void 0,
|
|
2806
|
+
network: stored.network,
|
|
2807
|
+
agentBudget: stored.agentBudget,
|
|
2808
|
+
apiUrl: stored.apiUrl,
|
|
2809
|
+
hostedMcpUrl: stored.hostedMcpUrl,
|
|
2810
|
+
x402BindingSigner: stored.x402BindingSigner,
|
|
2811
|
+
warn: deps.log
|
|
2812
|
+
});
|
|
2813
|
+
await clearRekeyPending(stored.directory);
|
|
2814
|
+
const names = serverNamesFor(options.serverName);
|
|
2815
|
+
const messages = [
|
|
2816
|
+
`Agent ${identity.name || stored.agentId} is now on its new signing key.`,
|
|
2817
|
+
` Signing address: ${pending.new_delegate_address}`,
|
|
2818
|
+
` Credentials: ${stored.directory} (rewritten in place)`,
|
|
2819
|
+
` MCP servers: ${names.hosted} / ${names.signer} (unchanged names)`
|
|
2820
|
+
];
|
|
2821
|
+
let configRewritten = false;
|
|
2822
|
+
if (options.runtime) {
|
|
2823
|
+
const prepared = await (deps.prepareSigner ?? prepareSignerRuntime)(
|
|
2824
|
+
{
|
|
2825
|
+
credentialDirectory: stored.directory,
|
|
2826
|
+
signerPath: `${stored.directory}/signer.json`,
|
|
2827
|
+
homeDir: options.homeDir
|
|
2828
|
+
},
|
|
2829
|
+
{ runCommand: deps.runCommand }
|
|
2830
|
+
);
|
|
2831
|
+
const result = await (deps.writeConfig ?? writeHostedRuntimeConfig)(
|
|
2832
|
+
{ runCommand: deps.runCommand, homeDir: options.homeDir },
|
|
2833
|
+
{
|
|
2834
|
+
runtime: options.runtime,
|
|
2835
|
+
hostedMcpUrl: stored.hostedMcpUrl,
|
|
2836
|
+
apiKey: options.newApiKey,
|
|
2837
|
+
identityPath: `${stored.directory}/identity.json`,
|
|
2838
|
+
signerPath: `${stored.directory}/signer.json`,
|
|
2839
|
+
credentialDirectory: stored.directory,
|
|
2840
|
+
serverName: options.serverName
|
|
2841
|
+
},
|
|
2842
|
+
{ command: prepared.command, args: prepared.args }
|
|
2843
|
+
);
|
|
2844
|
+
configRewritten = result.hostedConfigured;
|
|
2845
|
+
messages.push(` Config: ${result.target}`);
|
|
2846
|
+
messages.push(...result.messages.map((line) => ` ${line}`));
|
|
2847
|
+
if (!configRewritten) {
|
|
2848
|
+
messages.push(
|
|
2849
|
+
"",
|
|
2850
|
+
"WARNING: the MCP config was NOT updated, so it still carries the OLD API key and every",
|
|
2851
|
+
`wired host will fail with 401. Fix the cause above and re-run with --runtime ${options.runtime},`,
|
|
2852
|
+
"or update the config by hand. The credential files on disk are already on the new key."
|
|
2853
|
+
);
|
|
2376
2854
|
}
|
|
2855
|
+
} else {
|
|
2856
|
+
messages.push(
|
|
2857
|
+
"",
|
|
2858
|
+
"NOTE: no --runtime was given, so the MCP config still carries the OLD API key and every",
|
|
2859
|
+
"wired host will fail with 401. Re-run with --runtime <name> to rewrite it."
|
|
2860
|
+
);
|
|
2861
|
+
}
|
|
2862
|
+
return {
|
|
2863
|
+
finished: true,
|
|
2864
|
+
agentId: stored.agentId,
|
|
2865
|
+
directory: stored.directory,
|
|
2866
|
+
newDelegateAddress: pending.new_delegate_address,
|
|
2867
|
+
serverNames: { hosted: names.hosted, signer: names.signer },
|
|
2868
|
+
configRewritten,
|
|
2869
|
+
messages
|
|
2377
2870
|
};
|
|
2871
|
+
}
|
|
2872
|
+
async function probeIdentity(api, apiKey, which) {
|
|
2378
2873
|
try {
|
|
2379
|
-
await
|
|
2380
|
-
} catch {
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2874
|
+
return await api.getAgentIdentity(apiKey);
|
|
2875
|
+
} catch (err) {
|
|
2876
|
+
const status = err?.status;
|
|
2877
|
+
if (status === 401 || status === 403) {
|
|
2878
|
+
throw new Error(
|
|
2879
|
+
which === "current" ? "This machine's Haven API key is no longer accepted. If a re-key already finished elsewhere, run --rekey-finish with the key that re-key produced instead of starting a new one." : "Haven rejected that API key. Check you pasted the whole key from the agent page \u2014 nothing was changed."
|
|
2880
|
+
);
|
|
2385
2881
|
}
|
|
2882
|
+
throw new Error(
|
|
2883
|
+
`Could not reach Haven to check this agent (${redactSecrets(
|
|
2884
|
+
err instanceof Error ? err.message : String(err)
|
|
2885
|
+
)}). Nothing was changed.`
|
|
2886
|
+
);
|
|
2386
2887
|
}
|
|
2387
2888
|
}
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
readPackageJson2(join(runtimeDirectory, "node_modules", "@haven_ai", "sdk", "package.json"))
|
|
2394
|
-
]);
|
|
2395
|
-
return mcpPackage.version === MCP_RUNTIME_MANIFEST.mcpVersion && sdkPackage.version === MCP_RUNTIME_MANIFEST.sdkVersion;
|
|
2396
|
-
} catch {
|
|
2397
|
-
return false;
|
|
2889
|
+
function assertRekeyable(identity, stored) {
|
|
2890
|
+
if (identity.id !== stored.agentId) {
|
|
2891
|
+
throw new Error(
|
|
2892
|
+
`The credentials at ${stored.directory} say agent ${stored.agentId}, but Haven says that key belongs to ${identity.id}. Refusing to re-key an agent this directory does not own.`
|
|
2893
|
+
);
|
|
2398
2894
|
}
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
"import { spawn } from 'node:child_process'",
|
|
2409
|
-
"",
|
|
2410
|
-
`const cliPath = ${JSON.stringify(input.cliPath)}`,
|
|
2411
|
-
`const identityPath = ${JSON.stringify(input.identityPath)}`,
|
|
2412
|
-
`const signerPath = ${JSON.stringify(input.signerPath)}`,
|
|
2413
|
-
"",
|
|
2414
|
-
"const child = spawn(process.execPath, [cliPath, '--identity', identityPath, '--signer', signerPath, ...process.argv.slice(2)], {",
|
|
2415
|
-
" stdio: 'inherit',",
|
|
2416
|
-
"})",
|
|
2417
|
-
"",
|
|
2418
|
-
"child.on('exit', (code, signal) => {",
|
|
2419
|
-
" if (signal) process.kill(process.pid, signal)",
|
|
2420
|
-
" else process.exit(code ?? 1)",
|
|
2421
|
-
"})",
|
|
2422
|
-
""
|
|
2423
|
-
].join("\n");
|
|
2424
|
-
await writeFile(input.wrapperPath, source, { mode: 448 });
|
|
2425
|
-
await chmod(input.wrapperPath, 448).catch(() => void 0);
|
|
2426
|
-
}
|
|
2427
|
-
async function writeRuntimeSidecar2(input) {
|
|
2428
|
-
const value = {
|
|
2429
|
-
...input.serverName ? { server_name: input.serverName } : {},
|
|
2430
|
-
mcp_package: MCP_RUNTIME_MANIFEST.mcpPackage,
|
|
2431
|
-
mcp_version: MCP_RUNTIME_MANIFEST.mcpVersion,
|
|
2432
|
-
sdk_package: MCP_RUNTIME_MANIFEST.sdkPackage,
|
|
2433
|
-
sdk_version: MCP_RUNTIME_MANIFEST.sdkVersion,
|
|
2434
|
-
minimum_node_version: MCP_RUNTIME_MANIFEST.minimumNodeVersion,
|
|
2435
|
-
wrapper_path: input.wrapperPath,
|
|
2436
|
-
runtime_directory: input.runtimeDirectory,
|
|
2437
|
-
npm_cache_directory: input.npmCacheDirectory,
|
|
2438
|
-
cli_path: input.cliPath
|
|
2439
|
-
};
|
|
2440
|
-
await writeFile(input.path, `${JSON.stringify(value, null, 2)}
|
|
2441
|
-
`, { mode: 384 });
|
|
2442
|
-
await chmod(input.path, 384).catch(() => void 0);
|
|
2443
|
-
}
|
|
2444
|
-
async function assertFileExists2(path, label) {
|
|
2445
|
-
try {
|
|
2446
|
-
await access(path);
|
|
2447
|
-
} catch {
|
|
2448
|
-
throw new Error(`Missing ${label}: ${path}`);
|
|
2895
|
+
if (identity.execution_rail === "legacy") {
|
|
2896
|
+
throw new Error(
|
|
2897
|
+
`Agent ${identity.name || identity.id} is on the legacy rail, which cannot be re-keyed \u2014 its authority is per-token Safe allowances, not a signed delegation there is anything to re-issue. Re-onboard the agent on the delegation rail instead.`
|
|
2898
|
+
);
|
|
2899
|
+
}
|
|
2900
|
+
if (identity.status === "revoked") {
|
|
2901
|
+
throw new Error(
|
|
2902
|
+
`Agent ${identity.name || identity.id} is revoked. Re-keying would hand a revoked agent fresh credentials \u2014 create a new agent instead.`
|
|
2903
|
+
);
|
|
2449
2904
|
}
|
|
2450
2905
|
}
|
|
2906
|
+
var init_rekey = __esm({
|
|
2907
|
+
"src/rekey.ts"() {
|
|
2908
|
+
init_api();
|
|
2909
|
+
init_runtime_install();
|
|
2910
|
+
init_signer_runtime();
|
|
2911
|
+
init_key();
|
|
2912
|
+
init_redact();
|
|
2913
|
+
init_server_names();
|
|
2914
|
+
init_storage();
|
|
2915
|
+
}
|
|
2916
|
+
});
|
|
2451
2917
|
|
|
2452
|
-
// src/
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
case "codex-cli":
|
|
2470
|
-
case "codex-desktop":
|
|
2471
|
-
return installCodexAgentsSection(deps);
|
|
2472
|
-
default:
|
|
2473
|
-
return void 0;
|
|
2918
|
+
// src/rekey-restart.ts
|
|
2919
|
+
var rekey_restart_exports = {};
|
|
2920
|
+
__export(rekey_restart_exports, {
|
|
2921
|
+
restartGuidance: () => restartGuidance
|
|
2922
|
+
});
|
|
2923
|
+
function restartGuidance(runtime) {
|
|
2924
|
+
const profile = runtime ? BY_RUNTIME[runtime] : void 0;
|
|
2925
|
+
const lines = ["The new key is only live in a process that started after now.", ""];
|
|
2926
|
+
if (profile) {
|
|
2927
|
+
lines.push(profile.how);
|
|
2928
|
+
for (const command of profile.commands) lines.push(` ${command}`);
|
|
2929
|
+
if (profile.commands.length > 0) lines.push("");
|
|
2930
|
+
} else {
|
|
2931
|
+
lines.push(
|
|
2932
|
+
runtime ? `No standard restart command is known for "${runtime}" \u2014 restart it the way you start it.` : "Pass --runtime <name> to get the restart command for a specific host."
|
|
2933
|
+
);
|
|
2934
|
+
lines.push("");
|
|
2474
2935
|
}
|
|
2936
|
+
lines.push(SWEEP);
|
|
2937
|
+
return { commands: profile?.commands ?? [], lines };
|
|
2475
2938
|
}
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2939
|
+
var SWEEP, BY_RUNTIME;
|
|
2940
|
+
var init_rekey_restart = __esm({
|
|
2941
|
+
"src/rekey-restart.ts"() {
|
|
2942
|
+
SWEEP = "Then restart EVERY other long-lived MCP host on this machine \u2014 gateways, TUI workers, editors. Each holds the wiring snapshot from its own start time, so after a re-key each one is still presenting the OLD API key and will fail with 401 until it restarts.";
|
|
2943
|
+
BY_RUNTIME = {
|
|
2944
|
+
"claude-code": {
|
|
2945
|
+
commands: ["claude --continue"],
|
|
2946
|
+
how: "Exit this Claude Code session and start a new one:"
|
|
2947
|
+
},
|
|
2948
|
+
"codex-cli": {
|
|
2949
|
+
commands: ["codex resume --last"],
|
|
2950
|
+
how: "Start a fresh Codex CLI session:"
|
|
2951
|
+
},
|
|
2952
|
+
"codex-desktop": {
|
|
2953
|
+
commands: [],
|
|
2954
|
+
how: "Quit Codex Desktop completely (not just the window) and reopen it."
|
|
2955
|
+
},
|
|
2956
|
+
"claude-desktop": {
|
|
2957
|
+
commands: [],
|
|
2958
|
+
how: "Quit Claude Desktop completely (not just the window) and reopen it."
|
|
2959
|
+
},
|
|
2960
|
+
hermes: {
|
|
2961
|
+
commands: ["systemctl --user restart hermes-gateway", "/restart"],
|
|
2962
|
+
how: "Restart the Hermes gateway, or run /restart inside a Hermes session. The first form is the one that matters if you run it as a user service:"
|
|
2963
|
+
},
|
|
2964
|
+
cursor: {
|
|
2965
|
+
commands: [],
|
|
2966
|
+
how: 'Cursor hot-reloads MCP config, so it will pick the new key up on its own. If a tool call still fails with 401, reload the window (Cmd/Ctrl+Shift+P \u2192 "Reload Window").'
|
|
2967
|
+
},
|
|
2968
|
+
vscode: {
|
|
2969
|
+
commands: [],
|
|
2970
|
+
how: 'VS Code hot-reloads MCP config. If a tool call still fails with 401, reload the window (Cmd/Ctrl+Shift+P \u2192 "Reload Window").'
|
|
2971
|
+
},
|
|
2972
|
+
"vscode-insiders": {
|
|
2973
|
+
commands: [],
|
|
2974
|
+
how: 'VS Code Insiders hot-reloads MCP config. If a tool call still fails with 401, reload the window (Cmd/Ctrl+Shift+P \u2192 "Reload Window").'
|
|
2975
|
+
}
|
|
2485
2976
|
};
|
|
2486
|
-
}
|
|
2977
|
+
}
|
|
2978
|
+
});
|
|
2979
|
+
|
|
2980
|
+
// src/doctor.ts
|
|
2981
|
+
var doctor_exports = {};
|
|
2982
|
+
__export(doctor_exports, {
|
|
2983
|
+
runDoctor: () => runDoctor,
|
|
2984
|
+
runRepair: () => runRepair
|
|
2985
|
+
});
|
|
2986
|
+
async function discoverCredentialDirectory(homeDir, explicit) {
|
|
2987
|
+
const root = explicit ? dirname(explicit) : join(homeDir, ".haven", "agents");
|
|
2988
|
+
let entries = [];
|
|
2989
|
+
try {
|
|
2990
|
+
entries = await readdir(root);
|
|
2991
|
+
} catch {
|
|
2992
|
+
return explicit ? { directory: explicit, others: [], parkedOnly: /* @__PURE__ */ new Set() } : { others: [], parkedOnly: /* @__PURE__ */ new Set() };
|
|
2993
|
+
}
|
|
2994
|
+
const candidates = [];
|
|
2995
|
+
const tombstonedOnly = [];
|
|
2996
|
+
const parkedOnly = [];
|
|
2997
|
+
for (const entry of entries) {
|
|
2998
|
+
const directory = join(root, entry);
|
|
2999
|
+
try {
|
|
3000
|
+
const s = await stat(join(directory, "identity.json"));
|
|
3001
|
+
candidates.push({ directory, mtimeMs: s.mtimeMs });
|
|
3002
|
+
} catch {
|
|
3003
|
+
try {
|
|
3004
|
+
await stat(join(directory, TOMBSTONE_FILENAME));
|
|
3005
|
+
tombstonedOnly.push(directory);
|
|
3006
|
+
} catch {
|
|
3007
|
+
try {
|
|
3008
|
+
await stat(join(directory, REKEY_PENDING_FILENAME));
|
|
3009
|
+
parkedOnly.push(directory);
|
|
3010
|
+
} catch {
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
}
|
|
3014
|
+
}
|
|
3015
|
+
const parkedOnlySet = new Set(parkedOnly);
|
|
3016
|
+
candidates.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
3017
|
+
if (explicit) {
|
|
2487
3018
|
return {
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
]
|
|
3019
|
+
directory: explicit,
|
|
3020
|
+
others: [...candidates.map((c) => c.directory), ...tombstonedOnly, ...parkedOnly].filter((d) => d !== explicit),
|
|
3021
|
+
parkedOnly: parkedOnlySet
|
|
2492
3022
|
};
|
|
2493
3023
|
}
|
|
3024
|
+
if (candidates.length === 0 && tombstonedOnly.length === 0 && parkedOnly.length === 0) {
|
|
3025
|
+
return { others: [], parkedOnly: parkedOnlySet };
|
|
3026
|
+
}
|
|
3027
|
+
return {
|
|
3028
|
+
directory: candidates[0]?.directory,
|
|
3029
|
+
others: [...candidates.slice(1).map((c) => c.directory), ...tombstonedOnly, ...parkedOnly],
|
|
3030
|
+
parkedOnly: parkedOnlySet
|
|
3031
|
+
};
|
|
2494
3032
|
}
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
const
|
|
2499
|
-
|
|
2500
|
-
const existing = await readFile(target, "utf8").catch(() => null);
|
|
2501
|
-
const next = upsertManagedSection(existing, codexManagedSection());
|
|
2502
|
-
if (next !== existing) {
|
|
2503
|
-
await writeFile(target, next, "utf8");
|
|
3033
|
+
function agentIsWired(configText, names, slug, identity, sidecar, isPrimary, bareOwnerExists) {
|
|
3034
|
+
if (configText === null) return isPrimary;
|
|
3035
|
+
if (slug) {
|
|
3036
|
+
for (const name of [names.hosted, names.codexHosted, names.signer, names.codexSigner]) {
|
|
3037
|
+
if (new RegExp(`(^|[."'\\s\\[])${name}(["'\\]:\\s]|$)`, "m").test(configText)) return true;
|
|
2504
3038
|
}
|
|
3039
|
+
return false;
|
|
3040
|
+
}
|
|
3041
|
+
if (sidecar?.wrapper_path && configText.includes(sidecar.wrapper_path)) return true;
|
|
3042
|
+
if (bareOwnerExists) return false;
|
|
3043
|
+
return isPrimary && Boolean(identity?.hosted_mcp_url && configText.includes(identity.hosted_mcp_url));
|
|
3044
|
+
}
|
|
3045
|
+
function rekeyPendingCheck(status, hostedDelegateAddress, runtime, slug) {
|
|
3046
|
+
const label = "Pending re-key";
|
|
3047
|
+
const nameFlag = slug ? ` --name ${slug}` : "";
|
|
3048
|
+
if (status.state === "unreadable") {
|
|
2505
3049
|
return {
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
3050
|
+
id: "rekey_pending",
|
|
3051
|
+
label,
|
|
3052
|
+
ok: false,
|
|
3053
|
+
detail: `A re-key was started here but ${status.path} does not parse, so neither the address it generated nor when it started can be read. The file still holds what was a private key.`,
|
|
3054
|
+
repair: `Delete ${status.path}, then start again: ${RERUN} --rekey${nameFlag}`
|
|
2511
3055
|
};
|
|
2512
|
-
}
|
|
3056
|
+
}
|
|
3057
|
+
const started = status.startedAt ?? "an unknown time";
|
|
3058
|
+
const address = status.newDelegateAddress ?? "unknown";
|
|
3059
|
+
const completedOnHaven = hostedDelegateAddress !== void 0 && status.newDelegateAddress !== void 0 && hostedDelegateAddress.toLowerCase() === status.newDelegateAddress.toLowerCase();
|
|
3060
|
+
if (completedOnHaven) {
|
|
2513
3061
|
return {
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
3062
|
+
id: "rekey_pending",
|
|
3063
|
+
label,
|
|
3064
|
+
ok: false,
|
|
3065
|
+
detail: `A re-key started ${started} has COMPLETED on Haven \u2014 the agent's signing address is already ${address}, the one this machine generated \u2014 but the local half was never finished, so the credential files here still hold the old key. Parked at ${status.path}.` + (status.state === "expired" ? " The local file is also past its 24h TTL, which --rekey-finish refuses, so the finish command below will not accept it any more." : ""),
|
|
3066
|
+
repair: status.state === "expired" ? `The parked key expired. Start again \u2014 ${RERUN} --rekey${nameFlag} \u2014 and re-run "Replace signing key" on the Haven agent page with the new address it prints.` : `Run: ${RERUN} --rekey-finish${nameFlag} --api-key <the key the agent page showed you> --runtime ${runtime}`
|
|
3067
|
+
};
|
|
3068
|
+
}
|
|
3069
|
+
const wedgeNote = "Haven is NOT yet on this address, so the re-key did not complete. This machine cannot tell whether the on-chain revoke on the agent page already ran: if it did not, closing this costs nothing; if it did, the agent's old delegations are revoked, no new ones were issued, and only an owner re-grant restores its spend authority (#1868). Check the agent page before assuming the harmless case.";
|
|
3070
|
+
if (status.state === "expired") {
|
|
3071
|
+
return {
|
|
3072
|
+
id: "rekey_pending",
|
|
3073
|
+
label,
|
|
3074
|
+
ok: false,
|
|
3075
|
+
detail: `A re-key started ${started} EXPIRED ${status.expiresAt ?? ""} without being finished. Its address was ${address}; the private half it generated is still on disk at ${status.path}. ` + wedgeNote,
|
|
3076
|
+
repair: `Either delete ${status.path} to drop the parked key, or start over: ${RERUN} --rekey${nameFlag}. Connect never deletes it for you \u2014 an expired TTL is a refusal to USE the key, not a licence to destroy key material you may still be mid-flow on.`
|
|
2518
3077
|
};
|
|
2519
3078
|
}
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
${
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
3079
|
+
return {
|
|
3080
|
+
id: "rekey_pending",
|
|
3081
|
+
label,
|
|
3082
|
+
ok: true,
|
|
3083
|
+
detail: `A re-key started ${started} is still open (expires ${status.expiresAt ?? "unknown"}). Paste this address into "Replace signing key" on the Haven agent page: ${address}. Parked at ${status.path}. ` + wedgeNote
|
|
3084
|
+
};
|
|
3085
|
+
}
|
|
3086
|
+
async function readIdentity(directory) {
|
|
3087
|
+
try {
|
|
3088
|
+
return JSON.parse(await readFile(join(directory, "identity.json"), "utf8"));
|
|
3089
|
+
} catch {
|
|
3090
|
+
return void 0;
|
|
3091
|
+
}
|
|
3092
|
+
}
|
|
3093
|
+
async function checksForAgent(entry, input, deps) {
|
|
3094
|
+
const { directory, identity, sidecar } = entry;
|
|
3095
|
+
const checks = [];
|
|
3096
|
+
let signerCapabilities;
|
|
3097
|
+
let signerFile;
|
|
3098
|
+
try {
|
|
3099
|
+
const parsed = JSON.parse(await readFile(join(directory, "signer.json"), "utf8"));
|
|
3100
|
+
signerFile = typeof parsed === "object" && parsed !== null ? parsed : void 0;
|
|
3101
|
+
} catch {
|
|
3102
|
+
signerFile = void 0;
|
|
3103
|
+
}
|
|
3104
|
+
const credentialsOk = Boolean(identity?.api_key) && signerFile !== void 0;
|
|
3105
|
+
checks.push({
|
|
3106
|
+
id: "credentials",
|
|
3107
|
+
label: "Agent credentials",
|
|
3108
|
+
ok: credentialsOk,
|
|
3109
|
+
detail: credentialsOk ? `identity.json and signer.json parse (agent ${identity?.agent_id ?? "unknown"})` : "identity.json or signer.json is missing or unparseable.",
|
|
3110
|
+
...credentialsOk ? {} : { repair: `Re-run the full setup with a fresh token: ${RERUN} --setup <token>.` }
|
|
3111
|
+
});
|
|
3112
|
+
if (!sidecar) {
|
|
3113
|
+
checks.push({
|
|
3114
|
+
id: "signer_runtime",
|
|
3115
|
+
label: "Signer runtime (preinstalled wrapper)",
|
|
3116
|
+
ok: false,
|
|
3117
|
+
detail: "No signer-runtime.json sidecar \u2014 the pinned signer runtime was never prepared (or a pre-#1586 npx config).",
|
|
3118
|
+
repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}`
|
|
3119
|
+
});
|
|
3120
|
+
} else {
|
|
3121
|
+
const matches = await installedRuntimeMatches(sidecar.runtime_directory, sidecar.cli_path);
|
|
3122
|
+
const versionOk = sidecar.signer_version === MCP_RUNTIME_MANIFEST.signerVersion;
|
|
3123
|
+
const ok = matches && versionOk;
|
|
3124
|
+
checks.push({
|
|
3125
|
+
id: "signer_runtime",
|
|
3126
|
+
label: "Signer runtime (preinstalled wrapper)",
|
|
3127
|
+
ok,
|
|
3128
|
+
detail: ok ? `Installed ${sidecar.signer_package}@${sidecar.signer_version} at ${sidecar.runtime_directory}` : matches ? `Installed version ${sidecar.signer_version} does not match the connector's pinned ${MCP_RUNTIME_MANIFEST.signerVersion}.` : `Runtime directory is stale or empty (${sidecar.runtime_directory}) \u2014 the CLI or package versions are missing.`,
|
|
3129
|
+
...ok ? {} : { repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}` }
|
|
3130
|
+
});
|
|
3131
|
+
}
|
|
3132
|
+
const hostedUrl = identity?.hosted_mcp_url ?? (identity?.api_url ? `${identity.api_url}/mcp` : void 0);
|
|
3133
|
+
if (identity?.api_key && hostedUrl) {
|
|
3134
|
+
const probe = await (deps.probeHosted ?? probeHostedMcpTools)(identity.api_key, hostedUrl, deps.fetch);
|
|
3135
|
+
checks.push({
|
|
3136
|
+
id: "hosted_mcp",
|
|
3137
|
+
label: "Hosted Haven MCP",
|
|
3138
|
+
ok: probe.status === "ok",
|
|
3139
|
+
detail: probe.status === "ok" ? `Reachable and authorized (${hostedUrl}).` : `Probe failed: ${probe.status} (${hostedUrl}).`,
|
|
3140
|
+
...probe.status === "ok" ? {} : {
|
|
3141
|
+
repair: probe.status === "unauthorized" ? `The stored API key was rejected \u2014 re-run the full setup with a fresh token: ${RERUN} --setup <token>.` : "Check network access to the hosted MCP URL, then re-run --doctor."
|
|
3142
|
+
}
|
|
3143
|
+
});
|
|
3144
|
+
} else {
|
|
3145
|
+
checks.push({
|
|
3146
|
+
id: "hosted_mcp",
|
|
3147
|
+
label: "Hosted Haven MCP",
|
|
3148
|
+
ok: false,
|
|
3149
|
+
detail: "No stored API key / hosted MCP URL to probe with.",
|
|
3150
|
+
repair: `Re-run the full setup: ${RERUN} --setup <token>.`
|
|
3151
|
+
});
|
|
2537
3152
|
}
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
3153
|
+
const localDelegate = typeof signerFile?.delegate_address === "string" ? signerFile.delegate_address : void 0;
|
|
3154
|
+
let hostedDelegateAddress;
|
|
3155
|
+
if (identity?.api_key && identity.api_url) {
|
|
3156
|
+
const probe = await (deps.probeHostedIdentity ?? probeHostedAgentIdentity)(
|
|
3157
|
+
identity.api_key,
|
|
3158
|
+
identity.api_url,
|
|
3159
|
+
deps.fetch
|
|
2541
3160
|
);
|
|
3161
|
+
if (probe.status === "ok") hostedDelegateAddress = probe.delegateAddress;
|
|
3162
|
+
if (probe.status !== "ok") {
|
|
3163
|
+
checks.push({
|
|
3164
|
+
id: "identity_match",
|
|
3165
|
+
label: "Hosted identity matches the local signing key",
|
|
3166
|
+
ok: false,
|
|
3167
|
+
detail: probe.status === "unauthorized" ? "The stored API key was rejected, so the agent it authenticates as cannot be compared with the local signing key." : `Could not read the hosted identity (${probe.status}) \u2014 the comparison did not happen, so it cannot be reported as a match.`,
|
|
3168
|
+
repair: probe.status === "unauthorized" ? `Re-run the full setup with a fresh token: ${RERUN} --setup <token>.` : `Restore network access to the Haven API, then re-run: ${RERUN} --doctor --runtime ${input.runtime}`
|
|
3169
|
+
});
|
|
3170
|
+
} else if (!localDelegate) {
|
|
3171
|
+
checks.push({
|
|
3172
|
+
id: "identity_match",
|
|
3173
|
+
label: "Hosted identity matches the local signing key",
|
|
3174
|
+
ok: false,
|
|
3175
|
+
detail: "signer.json holds no delegate_address to compare against the hosted identity.",
|
|
3176
|
+
repair: `Re-run the full setup with a fresh token: ${RERUN} --setup <token>.`
|
|
3177
|
+
});
|
|
3178
|
+
} else {
|
|
3179
|
+
const same = probe.delegateAddress?.toLowerCase() === localDelegate.toLowerCase();
|
|
3180
|
+
checks.push({
|
|
3181
|
+
id: "identity_match",
|
|
3182
|
+
label: "Hosted identity matches the local signing key",
|
|
3183
|
+
ok: same,
|
|
3184
|
+
detail: same ? `The stored API key authenticates as the agent whose signing key is in this directory (${shortAddress(localDelegate)}).` : `MISMATCH: the stored API key authenticates as agent ${probe.agentId ?? "unknown"} with delegate ${shortAddress(probe.delegateAddress ?? "unknown")}, but signer.json here holds ${shortAddress(localDelegate)}. This runtime would quote as one agent and sign as another.`,
|
|
3185
|
+
...same ? {} : {
|
|
3186
|
+
repair: `Re-run setup for this agent so its API key and signing key come from one run: ${RERUN} --setup <token>. Do not hand-edit either file.`
|
|
3187
|
+
}
|
|
3188
|
+
});
|
|
3189
|
+
}
|
|
2542
3190
|
}
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
const indexes = [];
|
|
2547
|
-
for (let from = 0; ; ) {
|
|
2548
|
-
const at = text.indexOf(marker, from);
|
|
2549
|
-
if (at === -1) return indexes;
|
|
2550
|
-
if (at === 0 || text[at - 1] === "\n") indexes.push(at);
|
|
2551
|
-
from = at + marker.length;
|
|
3191
|
+
const pending = await inspectRekeyPending(directory, deps.now?.() ?? Date.now());
|
|
3192
|
+
if (pending) {
|
|
3193
|
+
checks.push(rekeyPendingCheck(pending, hostedDelegateAddress, input.runtime, sidecar?.server_name));
|
|
2552
3194
|
}
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
"Custom runtime: Haven did not auto-configure it. Your credentials are on disk (chmod 600) \u2014 read them at runtime; never paste a key into the agent prompt, memory, or logs.",
|
|
2590
|
-
` identity (hosted MCP Bearer): ${input.identityPath}`,
|
|
2591
|
-
` signer (local signing key): ${input.signerPath}`,
|
|
2592
|
-
"After wallet approval, wire the runtime to Haven by reference:",
|
|
2593
|
-
` Hosted MCP + local signer: point your MCP client at ${input.hostedMcpUrl} with the api_key from identity.json, then run npx -y ${signerPackageSpec()} --credentials ${input.signerPath}`,
|
|
2594
|
-
` Fully local MCP (no hosted dependency): npx -y ${mcpPackageSpec()} --identity ${input.identityPath} --signer ${input.signerPath}`
|
|
2595
|
-
]
|
|
2596
|
-
};
|
|
3195
|
+
if (sidecar) {
|
|
3196
|
+
const consent = await getLocalSignerConsentStatus(join(directory, "signer.json"));
|
|
3197
|
+
if (!consent.acknowledged) {
|
|
3198
|
+
checks.push({
|
|
3199
|
+
id: "signer_process",
|
|
3200
|
+
label: "Signer stdio handshake",
|
|
3201
|
+
ok: false,
|
|
3202
|
+
detail: "The local-tools consent is not acknowledged, so the signer refuses to start (by design).",
|
|
3203
|
+
repair: `Run: ${RERUN} --ack-local-tools --setup <token> (or re-run your original setup command with --ack-local-tools).`
|
|
3204
|
+
});
|
|
3205
|
+
} else {
|
|
3206
|
+
const probe = await (deps.probeSignerTools ?? probeLocalMcpTools)(
|
|
3207
|
+
sidecar.wrapper_path,
|
|
3208
|
+
[],
|
|
3209
|
+
MCP_RUNTIME_MANIFEST.requiredSignerTools
|
|
3210
|
+
);
|
|
3211
|
+
const experimental = probe.capabilities?.experimental ?? probe.capabilities;
|
|
3212
|
+
const compat = experimental?.["haven/signer-compatibility"];
|
|
3213
|
+
signerCapabilities = compat ? { "haven/signer-compatibility": compat } : void 0;
|
|
3214
|
+
const compatDetail = compat ? ` Compat: x402 expected-context v${JSON.stringify(compat.x402_expected_context_versions ?? "?")}.` : "";
|
|
3215
|
+
checks.push({
|
|
3216
|
+
id: "signer_process",
|
|
3217
|
+
label: "Signer stdio handshake",
|
|
3218
|
+
ok: probe.status === "ok",
|
|
3219
|
+
detail: probe.status === "ok" ? `Signer started, listed ${probe.toolNames?.length ?? 0} tools${probe.serverInfo?.version ? ` (v${probe.serverInfo.version})` : ""}.${compatDetail}` : `Handshake failed: ${probe.status}.`,
|
|
3220
|
+
...probe.status === "ok" ? {} : { repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}` }
|
|
3221
|
+
});
|
|
3222
|
+
}
|
|
3223
|
+
} else {
|
|
3224
|
+
checks.push({
|
|
3225
|
+
id: "signer_process",
|
|
3226
|
+
label: "Signer stdio handshake",
|
|
3227
|
+
ok: false,
|
|
3228
|
+
detail: "Skipped \u2014 no prepared signer runtime to probe.",
|
|
3229
|
+
repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}`
|
|
3230
|
+
});
|
|
2597
3231
|
}
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
3232
|
+
return { checks, ...signerCapabilities ? { signerCapabilities } : {} };
|
|
3233
|
+
}
|
|
3234
|
+
async function runDoctor(input, deps = {}) {
|
|
3235
|
+
const homeDir = deps.homeDir ?? homedir();
|
|
3236
|
+
const checks = [];
|
|
3237
|
+
let signerCapabilities;
|
|
3238
|
+
const { directory, others, parkedOnly } = await discoverCredentialDirectory(homeDir, input.credentialsDir);
|
|
3239
|
+
const configPath = runtimeConfigPathFor(input.runtime, homeDir);
|
|
3240
|
+
let configText = null;
|
|
3241
|
+
if (configPath !== null) {
|
|
2601
3242
|
try {
|
|
2602
|
-
|
|
2603
|
-
} catch
|
|
2604
|
-
|
|
3243
|
+
configText = await readFile(configPath, "utf8");
|
|
3244
|
+
} catch {
|
|
3245
|
+
configText = null;
|
|
2605
3246
|
}
|
|
2606
3247
|
}
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
localMcpConfigured: false,
|
|
2615
|
-
probeResult: errorCode2 === "local_mcp_unsupported_node_version" ? "local_stdio_mcp_unsupported_node_version" : "local_stdio_mcp_runtime_install_failed",
|
|
2616
|
-
restartRequired: true,
|
|
2617
|
-
nextUserAction: nextAction(runtime, profile.restartMode, errorCode2),
|
|
2618
|
-
errorCode: errorCode2,
|
|
2619
|
-
configTarget: profile.label,
|
|
2620
|
-
signerAcknowledged: signerConsent?.acknowledged,
|
|
2621
|
-
localMcpAcknowledged: localMcpConsent?.acknowledged,
|
|
2622
|
-
activationCommand: void 0,
|
|
2623
|
-
messages: [
|
|
2624
|
-
...consentMessages,
|
|
2625
|
-
`Could not prepare local Haven MCP runtime: ${localRuntimeError instanceof Error ? localRuntimeError.message : String(localRuntimeError)}`
|
|
2626
|
-
]
|
|
2627
|
-
};
|
|
2628
|
-
}
|
|
2629
|
-
let signerCommand;
|
|
2630
|
-
if (!localRuntime) {
|
|
2631
|
-
progress("Getting the signer ready\u2026");
|
|
2632
|
-
try {
|
|
2633
|
-
const signerRuntime = await prepareSignerForRuntime(input, deps);
|
|
2634
|
-
signerCommand = { command: signerRuntime.command, args: signerRuntime.args };
|
|
2635
|
-
consentMessages.push(...signerRuntime.messages);
|
|
2636
|
-
} catch (err) {
|
|
2637
|
-
return {
|
|
2638
|
-
runtime,
|
|
2639
|
-
runtimeMcpMode: "hosted_plus_signer",
|
|
2640
|
-
hostedMcpConfigured: false,
|
|
2641
|
-
localSignerConfigured: false,
|
|
2642
|
-
localMcpConfigured: false,
|
|
2643
|
-
probeResult: "signer_runtime_install_failed",
|
|
2644
|
-
restartRequired: false,
|
|
2645
|
-
nextUserAction: "The local Haven signer runtime could not be installed, so no configuration was written. Check your network (a cold install downloads the signer package set) and re-run: npx @haven_ai/connect@alpha",
|
|
2646
|
-
errorCode: "signer_runtime_install_failed",
|
|
2647
|
-
configTarget: profile.label,
|
|
2648
|
-
signerAcknowledged: signerConsent?.acknowledged,
|
|
2649
|
-
localMcpAcknowledged: localMcpConsent?.acknowledged,
|
|
2650
|
-
activationCommand: void 0,
|
|
2651
|
-
signerRuntimePrepared: false,
|
|
2652
|
-
messages: [
|
|
2653
|
-
...consentMessages,
|
|
2654
|
-
`Could not pre-install the local Haven signer: ${err instanceof Error ? err.message : String(err)}`,
|
|
2655
|
-
"No runtime configuration was written (fail-closed): a config pointing at an uninstalled signer looks wired but cannot start.",
|
|
2656
|
-
"Re-run `npx @haven_ai/connect@alpha` to retry the setup."
|
|
2657
|
-
]
|
|
2658
|
-
};
|
|
3248
|
+
const allDirectories = directory ? [directory, ...others] : others;
|
|
3249
|
+
let bareOwnerExists = false;
|
|
3250
|
+
for (const dir of allDirectories) {
|
|
3251
|
+
const sidecar = await readRuntimeSidecar(dir);
|
|
3252
|
+
if (!sidecar?.server_name && sidecar?.wrapper_path && configText?.includes(sidecar.wrapper_path)) {
|
|
3253
|
+
bareOwnerExists = true;
|
|
3254
|
+
break;
|
|
2659
3255
|
}
|
|
2660
3256
|
}
|
|
2661
|
-
const
|
|
2662
|
-
|
|
2663
|
-
const
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
runtime,
|
|
2683
|
-
runtimeMcpMode: configResult.runtimeMcpMode,
|
|
2684
|
-
hostedMcpConfigured: configResult.hostedConfigured,
|
|
2685
|
-
localSignerConfigured: earlySignerOk,
|
|
2686
|
-
localMcpConfigured: earlyLocalMcpOk,
|
|
2687
|
-
signerAcknowledged: signerConsent?.acknowledged,
|
|
2688
|
-
localMcpAcknowledged: localMcpConsent?.acknowledged,
|
|
2689
|
-
restartRequired: configResult.restartRequired || restartRequiredForRuntime(runtime, deps.env),
|
|
2690
|
-
nextUserAction: nextAction(runtime, profile.restartMode, configResult.errorCode),
|
|
2691
|
-
errorCode: configResult.errorCode
|
|
3257
|
+
const inventory = [];
|
|
3258
|
+
const capabilitiesByDirectory = /* @__PURE__ */ new Map();
|
|
3259
|
+
const primaryChecksById = /* @__PURE__ */ new Map();
|
|
3260
|
+
for (const dir of allDirectories) {
|
|
3261
|
+
const identity = await readIdentity(dir);
|
|
3262
|
+
const sidecar = await readRuntimeSidecar(dir);
|
|
3263
|
+
const tombstone = await readAgentTombstone(dir);
|
|
3264
|
+
const slug = sidecar?.server_name;
|
|
3265
|
+
const names = serverNamesFor(slug);
|
|
3266
|
+
const rekeyPending = await inspectRekeyPending(dir, deps.now?.() ?? Date.now());
|
|
3267
|
+
if (!identity?.api_key) {
|
|
3268
|
+
const agentId = tombstone?.agent_id ?? rekeyPending?.agentId;
|
|
3269
|
+
inventory.push({
|
|
3270
|
+
...slug ? { slug } : {},
|
|
3271
|
+
...agentId ? { agentId } : {},
|
|
3272
|
+
directory: dir,
|
|
3273
|
+
// A tombstone is a deliberate record and outranks the discovery tell:
|
|
3274
|
+
// a retired directory that also holds a parked key stays `retired`.
|
|
3275
|
+
classification: tombstone ? "retired" : parkedOnly.has(dir) ? "parked" : "orphaned",
|
|
3276
|
+
checks: rekeyPending ? [rekeyPendingCheck(rekeyPending, void 0, input.runtime, slug)] : [],
|
|
3277
|
+
...rekeyPending ? { rekeyPending } : {}
|
|
2692
3278
|
});
|
|
2693
|
-
|
|
3279
|
+
continue;
|
|
3280
|
+
}
|
|
3281
|
+
const wired = agentIsWired(configText, names, slug, identity, sidecar, dir === directory, bareOwnerExists);
|
|
3282
|
+
const entry = {
|
|
3283
|
+
...slug ? { slug } : {},
|
|
3284
|
+
...identity.agent_id ? { agentId: identity.agent_id } : {},
|
|
3285
|
+
directory: dir,
|
|
3286
|
+
classification: wired ? "wired" : "superseded",
|
|
3287
|
+
checks: [],
|
|
3288
|
+
...rekeyPending ? { rekeyPending } : {}
|
|
3289
|
+
};
|
|
3290
|
+
if (wired) {
|
|
3291
|
+
const result = await checksForAgent({ directory: dir, identity, sidecar }, input, deps);
|
|
3292
|
+
entry.checks = result.checks;
|
|
3293
|
+
capabilitiesByDirectory.set(dir, result.signerCapabilities);
|
|
3294
|
+
} else if (rekeyPending) {
|
|
3295
|
+
entry.checks = [rekeyPendingCheck(rekeyPending, void 0, input.runtime, slug)];
|
|
2694
3296
|
}
|
|
3297
|
+
inventory.push(entry);
|
|
2695
3298
|
}
|
|
2696
|
-
|
|
2697
|
-
const
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
const localMcpOk = configResult.runtimeMcpMode === "local_stdio" && configResult.localMcpConfigured && signerCredentialReady && Boolean(localMcpConsent?.acknowledged) && localMcpProbe?.status === "ok";
|
|
2711
|
-
const signerOk = configResult.runtimeMcpMode === "local_stdio" ? localMcpOk : configResult.signerConfigured && signerCredentialReady && Boolean(signerConsent?.acknowledged) && // #1587: no handshake, no green. A signer command that was registered
|
|
2712
|
-
// but not probed (manual topology) keeps the old semantics.
|
|
2713
|
-
(signerProbe === void 0 || signerProbe.status === "ok");
|
|
2714
|
-
const restartRequired = configResult.restartRequired || restartRequiredForRuntime(runtime, deps.env);
|
|
2715
|
-
const errorCode = configResult.errorCode ?? (configResult.runtimeMcpMode === "local_stdio" ? localMcpErrorCode(signerCredentialReady, localMcpConsent, localMcpProbe?.status) : hostedMcpErrorCode(configResult.hostedConfigured, hostedProbe.status) ?? signerConsentErrorCode(signerCredentialReady, signerConsent) ?? signerProbeErrorCode(signerProbe));
|
|
2716
|
-
const hostedProbeMessages = configResult.hostedConfigured && hostedProbe.status !== "ok" ? [`Hosted Haven MCP probe failed: ${hostedProbe.status}.`] : configResult.hostedConfigured ? ["Verified hosted Haven MCP tools with a read-only handshake."] : [];
|
|
2717
|
-
const signerProbeMessages = signerProbe ? signerProbe.status === "ok" ? ["Verified local Haven signer with a stdio handshake."] : [
|
|
2718
|
-
`Local Haven signer handshake failed: ${signerProbe.status}.`,
|
|
2719
|
-
"Re-run `npx @haven_ai/connect@alpha` to repair the signer setup."
|
|
2720
|
-
] : [];
|
|
2721
|
-
const localProbeMessages = localMcpProbe && localMcpProbe.status !== "ok" ? [`Local Haven MCP handshake failed: ${localMcpProbe.status}.`] : localMcpProbe?.status === "ok" ? ["Verified local Haven MCP tools with a stdio handshake."] : [];
|
|
2722
|
-
const skillInstall = !configResult.errorCode ? await installSkillForRuntime(runtime, { homeDir: deps.homeDir, env: deps.env }) : void 0;
|
|
2723
|
-
return {
|
|
2724
|
-
runtime,
|
|
2725
|
-
runtimeMcpMode: configResult.runtimeMcpMode,
|
|
2726
|
-
hostedMcpConfigured: hostedOk,
|
|
2727
|
-
localSignerConfigured: signerOk,
|
|
2728
|
-
localMcpConfigured: localMcpOk,
|
|
2729
|
-
probeResult: buildProbeResult(configResult.runtimeMcpMode, configResult.hostedConfigured, hostedProbe.status, signerOk, localMcpOk, localMcpProbe?.status),
|
|
2730
|
-
restartRequired,
|
|
2731
|
-
nextUserAction: nextAction(runtime, profile.restartMode, errorCode),
|
|
2732
|
-
errorCode,
|
|
2733
|
-
configTarget: configResult.target,
|
|
2734
|
-
signerAcknowledged: signerConsent?.acknowledged,
|
|
2735
|
-
localMcpAcknowledged: localMcpConsent?.acknowledged,
|
|
2736
|
-
activationCommand: configResult.activationCommand,
|
|
2737
|
-
skillInstalled: skillInstall?.installed,
|
|
2738
|
-
signerRuntimePrepared,
|
|
2739
|
-
messages: [...consentMessages, ...localRuntimeInstall?.messages ?? [], ...configResult.messages, ...hostedProbeMessages, ...signerProbeMessages, ...localProbeMessages, ...skillInstall?.messages ?? []]
|
|
2740
|
-
};
|
|
2741
|
-
}
|
|
2742
|
-
function runtimeInstallCapabilities(runtime, env = process.env) {
|
|
2743
|
-
const profile = runtimeProfile(runtime, env);
|
|
2744
|
-
return {
|
|
2745
|
-
canWriteRuntimeConfig: profile.canWriteRuntimeConfig,
|
|
2746
|
-
restartRequired: restartRequiredForRuntime(runtime, env)
|
|
2747
|
-
};
|
|
2748
|
-
}
|
|
2749
|
-
async function configureClaudeCode(deps, localMcpCommand, serverName) {
|
|
2750
|
-
const runCommand = deps.runCommand ?? defaultRunCommand;
|
|
2751
|
-
const serverJson = JSON.stringify({
|
|
2752
|
-
type: "stdio",
|
|
2753
|
-
command: localMcpCommand,
|
|
2754
|
-
args: [],
|
|
2755
|
-
env: {}
|
|
2756
|
-
});
|
|
2757
|
-
try {
|
|
2758
|
-
if (!localMcpCommand) throw new Error("local MCP wrapper command is required");
|
|
2759
|
-
const names = serverNamesFor(serverName);
|
|
2760
|
-
await runCommand("claude", ["mcp", "remove", names.hosted]).catch(() => void 0);
|
|
2761
|
-
await runCommand("claude", ["mcp", "remove", names.signer]).catch(() => void 0);
|
|
2762
|
-
await runCommand("claude", ["mcp", "add-json", names.hosted, serverJson, "--scope", "user"]).catch(async () => {
|
|
2763
|
-
await runCommand("claude", ["mcp", "add", names.hosted, "--scope", "user", "--", localMcpCommand]);
|
|
3299
|
+
const wiredDirectories = inventory.filter((entry) => entry.classification === "wired").map((entry) => entry.directory);
|
|
3300
|
+
const primaryDirectory = input.credentialsDir ? directory : wiredDirectories.includes(directory ?? "") ? directory : wiredDirectories[0] ?? directory;
|
|
3301
|
+
if (primaryDirectory) {
|
|
3302
|
+
const primaryEntry = inventory.find((entry) => entry.directory === primaryDirectory);
|
|
3303
|
+
signerCapabilities = capabilitiesByDirectory.get(primaryDirectory);
|
|
3304
|
+
for (const check of primaryEntry?.checks ?? []) primaryChecksById.set(check.id, check);
|
|
3305
|
+
}
|
|
3306
|
+
if (!primaryDirectory) {
|
|
3307
|
+
checks.push({
|
|
3308
|
+
id: "credentials",
|
|
3309
|
+
label: "Agent credentials",
|
|
3310
|
+
ok: false,
|
|
3311
|
+
detail: "No agent credential directory with an identity.json under ~/.haven/agents.",
|
|
3312
|
+
repair: `Run the full setup once: ${RERUN} --setup <token from the Haven dashboard>.`
|
|
2764
3313
|
});
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
hostedConfigured: false,
|
|
2782
|
-
signerConfigured: false,
|
|
2783
|
-
localMcpConfigured: false,
|
|
2784
|
-
runtimeMcpMode: "local_stdio",
|
|
2785
|
-
target: "Claude Code MCP config",
|
|
2786
|
-
changed: false,
|
|
2787
|
-
restartRequired: true,
|
|
2788
|
-
messages: [
|
|
2789
|
-
`Could not update Claude Code MCP config: ${err instanceof Error ? err.message : String(err)}`,
|
|
2790
|
-
"Install Claude Code or rerun the Haven setup command inside a Claude Code terminal."
|
|
2791
|
-
],
|
|
2792
|
-
errorCode: "claude_code_config_failed"
|
|
2793
|
-
};
|
|
3314
|
+
} else {
|
|
3315
|
+
const primaryIdentity = await readIdentity(primaryDirectory);
|
|
3316
|
+
const primarySidecar = await readRuntimeSidecar(primaryDirectory);
|
|
3317
|
+
if (!primaryChecksById.has("credentials")) {
|
|
3318
|
+
const result = await checksForAgent(
|
|
3319
|
+
{ directory: primaryDirectory, identity: primaryIdentity, sidecar: primarySidecar },
|
|
3320
|
+
input,
|
|
3321
|
+
deps
|
|
3322
|
+
);
|
|
3323
|
+
signerCapabilities = result.signerCapabilities;
|
|
3324
|
+
for (const check of result.checks) primaryChecksById.set(check.id, check);
|
|
3325
|
+
}
|
|
3326
|
+
for (const id of ["credentials", "signer_runtime"]) {
|
|
3327
|
+
const check = primaryChecksById.get(id);
|
|
3328
|
+
if (check) checks.push(check);
|
|
3329
|
+
}
|
|
2794
3330
|
}
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
})
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
await
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
const
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
messages: [
|
|
2825
|
-
"Updated hosted Haven MCP and local signer entries with Claude Code.",
|
|
2826
|
-
...verified ? ["Verified Claude Code MCP entry."] : []
|
|
2827
|
-
]
|
|
2828
|
-
};
|
|
2829
|
-
} catch (err) {
|
|
2830
|
-
return {
|
|
2831
|
-
hostedConfigured: false,
|
|
2832
|
-
signerConfigured: false,
|
|
2833
|
-
localMcpConfigured: false,
|
|
2834
|
-
runtimeMcpMode: "hosted_plus_signer",
|
|
2835
|
-
target: "Claude Code MCP config",
|
|
2836
|
-
changed: false,
|
|
2837
|
-
restartRequired: true,
|
|
2838
|
-
messages: [
|
|
2839
|
-
`Could not update Claude Code MCP config: ${err instanceof Error ? err.message : String(err)}`,
|
|
2840
|
-
"Install Claude Code or rerun the Haven setup command inside a Claude Code terminal."
|
|
2841
|
-
],
|
|
2842
|
-
errorCode: "claude_code_config_failed"
|
|
2843
|
-
};
|
|
3331
|
+
if (configPath === null) {
|
|
3332
|
+
checks.push({
|
|
3333
|
+
id: "runtime_config",
|
|
3334
|
+
label: "Runtime MCP config",
|
|
3335
|
+
ok: true,
|
|
3336
|
+
detail: `Runtime '${input.runtime}' has no file-based config the connector owns (CLI-managed) \u2014 skipping the file check.`
|
|
3337
|
+
});
|
|
3338
|
+
} else if (configText === null) {
|
|
3339
|
+
checks.push({
|
|
3340
|
+
id: "runtime_config",
|
|
3341
|
+
label: "Runtime MCP config",
|
|
3342
|
+
ok: false,
|
|
3343
|
+
detail: `No runtime config at ${configPath}.`,
|
|
3344
|
+
repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}`
|
|
3345
|
+
});
|
|
3346
|
+
} else {
|
|
3347
|
+
const primaryIdentity = await readIdentity(primaryDirectory ?? "");
|
|
3348
|
+
const primarySidecar = primaryDirectory ? await readRuntimeSidecar(primaryDirectory) : null;
|
|
3349
|
+
const hasHaven = primaryIdentity?.hosted_mcp_url ? configText.includes(primaryIdentity.hosted_mcp_url) : configText.includes("haven");
|
|
3350
|
+
const signerViaNpx = configText.includes("@haven_ai/signer");
|
|
3351
|
+
const wrapperReferenced = primarySidecar ? configText.includes(primarySidecar.wrapper_path) : false;
|
|
3352
|
+
const ok = hasHaven && !signerViaNpx && (primarySidecar ? wrapperReferenced : true);
|
|
3353
|
+
checks.push({
|
|
3354
|
+
id: "runtime_config",
|
|
3355
|
+
label: "Runtime MCP config",
|
|
3356
|
+
ok,
|
|
3357
|
+
detail: ok ? `Config at ${configPath} references the hosted server and the prepared signer wrapper.` : signerViaNpx ? `Config at ${configPath} still launches the signer via npx \u2014 the pre-#1586 shape that cannot start under a 120s startup timeout.` : `Config at ${configPath} is missing the Haven entries${primarySidecar && !wrapperReferenced ? " (or references a different signer wrapper)" : ""}.`,
|
|
3358
|
+
...ok ? {} : { repair: `Run: ${RERUN} --doctor --repair --runtime ${input.runtime}` }
|
|
3359
|
+
});
|
|
2844
3360
|
}
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
}
|
|
2849
|
-
function buildProbeResult(mode, hostedConfigured, hostedStatus, signerReady, localMcpReady, localMcpProbeStatus) {
|
|
2850
|
-
if (mode === "local_stdio") {
|
|
2851
|
-
if (localMcpReady) return "local_stdio_mcp_ready";
|
|
2852
|
-
return localMcpProbeStatus ? `local_stdio_mcp_${localMcpProbeStatus}` : "local_stdio_mcp_unavailable";
|
|
3361
|
+
for (const id of ["hosted_mcp", "identity_match", "rekey_pending"]) {
|
|
3362
|
+
const check = primaryChecksById.get(id);
|
|
3363
|
+
if (check) checks.push(check);
|
|
2853
3364
|
}
|
|
2854
|
-
const
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
const
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
3365
|
+
const otherEntries = inventory.filter((entry) => entry.directory !== primaryDirectory);
|
|
3366
|
+
if (otherEntries.length > 0) {
|
|
3367
|
+
const live = [];
|
|
3368
|
+
const revoked = [];
|
|
3369
|
+
const unverifiable = [];
|
|
3370
|
+
const retired = [];
|
|
3371
|
+
for (const entry of otherEntries) {
|
|
3372
|
+
const identity = await readIdentity(entry.directory);
|
|
3373
|
+
const tombstone = await readAgentTombstone(entry.directory);
|
|
3374
|
+
const otherAgent = entry.agentId ?? basename(entry.directory);
|
|
3375
|
+
const otherUrl = identity?.hosted_mcp_url ?? (identity?.api_url ? `${identity.api_url}/mcp` : void 0);
|
|
3376
|
+
if (!identity?.api_key || !otherUrl) {
|
|
3377
|
+
if (tombstone) retired.push(`${otherAgent} (retired ${tombstone.retired_at})`);
|
|
3378
|
+
else unverifiable.push(`${otherAgent} (no stored key/URL to probe)`);
|
|
3379
|
+
continue;
|
|
3380
|
+
}
|
|
3381
|
+
const suffix = tombstone ? " [tombstoned \u2014 key material still present]" : "";
|
|
3382
|
+
const probe = await (deps.probeHosted ?? probeHostedMcpTools)(identity.api_key, otherUrl, deps.fetch);
|
|
3383
|
+
if (probe.status === "ok") live.push({ label: `${otherAgent}${suffix}`, entry });
|
|
3384
|
+
else if (probe.status === "unauthorized") revoked.push(`${otherAgent}${suffix}`);
|
|
3385
|
+
else unverifiable.push(`${otherAgent} (${probe.status})${suffix}`);
|
|
2865
3386
|
}
|
|
2866
|
-
|
|
3387
|
+
const parts = [];
|
|
3388
|
+
if (live.length > 0) parts.push(`STILL SPEND-CAPABLE: ${live.map((item) => item.label).join(", ")}`);
|
|
3389
|
+
if (revoked.length > 0) parts.push(`already revoked: ${revoked.join(", ")}`);
|
|
3390
|
+
if (retired.length > 0) parts.push(`tombstoned (keys removed): ${retired.join(", ")}`);
|
|
3391
|
+
if (unverifiable.length > 0) parts.push(`could not verify: ${unverifiable.join(", ")}`);
|
|
3392
|
+
const supersededLive = live.filter((item) => item.entry.classification !== "wired").map((item) => item.label);
|
|
3393
|
+
checks.push({
|
|
3394
|
+
id: "superseded_agents",
|
|
3395
|
+
label: "Superseded agent credentials",
|
|
3396
|
+
ok: supersededLive.length === 0,
|
|
3397
|
+
detail: supersededLive.length > 0 ? `${otherEntries.length} other credential dir(s) found \u2014 ${parts.join("; ")}. A host started before your latest setup keeps authenticating (and spending) as the old agent.` : `${otherEntries.length} other credential dir(s) found \u2014 ${parts.join("; ")}.`,
|
|
3398
|
+
...supersededLive.length > 0 ? {
|
|
3399
|
+
repair: `Revoke ${supersededLive.join(", ")} on the Haven agent page, then remove the old director(y/ies) under ~/.haven/agents. Connect never revokes or deletes for you.`
|
|
3400
|
+
} : {}
|
|
3401
|
+
});
|
|
2867
3402
|
}
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
3403
|
+
const parkedElsewhere = inventory.filter((entry) => entry.directory !== primaryDirectory && entry.rekeyPending).map((entry) => ({ entry, pending: entry.rekeyPending }));
|
|
3404
|
+
if (parkedElsewhere.length > 0) {
|
|
3405
|
+
const abandoned = parkedElsewhere.filter((item) => item.pending.state !== "pending");
|
|
3406
|
+
const describe = (item) => `${item.entry.slug ?? item.entry.agentId ?? basename(item.entry.directory)} (${item.pending.state}, ${item.pending.path})`;
|
|
3407
|
+
checks.push({
|
|
3408
|
+
id: "rekey_pending_elsewhere",
|
|
3409
|
+
label: "Parked re-keys in other credential directories",
|
|
3410
|
+
ok: abandoned.length === 0,
|
|
3411
|
+
detail: abandoned.length > 0 ? `ABANDONED re-key key material outside the agent this report describes: ${abandoned.map(describe).join(", ")}. Each holds a private key that was generated for a re-key nobody finished.` : `${parkedElsewhere.length} other director(y/ies) hold an open pending re-key: ${parkedElsewhere.map(describe).join(", ")}.`,
|
|
3412
|
+
...abandoned.length > 0 ? {
|
|
3413
|
+
repair: "Check the Haven agent page for each before deleting: if its on-chain revoke already ran, the agent has no spend authority until you re-grant it (#1868), and that is not visible from this machine. Connect never deletes key material for you."
|
|
3414
|
+
} : {}
|
|
3415
|
+
});
|
|
2879
3416
|
}
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
if (!probe || probe.status === "ok") return void 0;
|
|
2889
|
-
return `local_signer_probe_${probe.status}`;
|
|
2890
|
-
}
|
|
2891
|
-
function hostedMcpErrorCode(hostedConfigured, hostedProbeStatus) {
|
|
2892
|
-
if (!hostedConfigured || hostedProbeStatus === "ok") return void 0;
|
|
2893
|
-
return `hosted_mcp_probe_${hostedProbeStatus}`;
|
|
2894
|
-
}
|
|
2895
|
-
function localMcpErrorCode(signerCredentialReady, localMcpConsent, localMcpProbeStatus) {
|
|
2896
|
-
if (!signerCredentialReady) return "local_signer_credential_unavailable";
|
|
2897
|
-
if (!localMcpConsent?.acknowledged) return "local_mcp_ack_required";
|
|
2898
|
-
if (localMcpProbeStatus && localMcpProbeStatus !== "ok") return `local_mcp_probe_${localMcpProbeStatus}`;
|
|
2899
|
-
return void 0;
|
|
2900
|
-
}
|
|
2901
|
-
function nextAction(runtime, restartMode, errorCode) {
|
|
2902
|
-
if (errorCode) return "return_to_haven_for_wallet_approval_then_finish_runtime_setup";
|
|
2903
|
-
if (restartMode === "hot-reload") return "return_to_haven_for_wallet_approval";
|
|
2904
|
-
if (runtime === "codex-cli" || runtime === "codex-desktop") return "return_to_haven_for_wallet_approval_then_restart_codex";
|
|
2905
|
-
if (runtime === "claude-code") return "return_to_haven_for_wallet_approval_then_restart_claude_code";
|
|
2906
|
-
if (restartMode === "restart-app") return "return_to_haven_for_wallet_approval_then_restart_app";
|
|
2907
|
-
if (restartMode === "restart-session") return "return_to_haven_for_wallet_approval_then_restart_agent_session";
|
|
2908
|
-
return "return_to_haven_for_wallet_approval_then_configure_runtime";
|
|
2909
|
-
}
|
|
2910
|
-
function supportsLocalMcp(runtime) {
|
|
2911
|
-
return runtime === "codex-cli" || runtime === "codex-desktop" || runtime === "claude-code";
|
|
2912
|
-
}
|
|
2913
|
-
async function prepareRuntimeForLocalMcp(input, deps) {
|
|
2914
|
-
const prepare = deps.prepareLocalMcpRuntime ?? ((runtimeInput) => prepareLocalMcpRuntime(runtimeInput, { runCommand: deps.runCommand, onProgress: deps.onProgress }));
|
|
2915
|
-
return prepare({
|
|
2916
|
-
credentialDirectory: input.credentialDirectory,
|
|
2917
|
-
identityPath: input.identityPath,
|
|
2918
|
-
signerPath: input.signerPath,
|
|
2919
|
-
homeDir: deps.homeDir,
|
|
2920
|
-
serverName: input.serverName
|
|
2921
|
-
});
|
|
2922
|
-
}
|
|
2923
|
-
async function prepareSignerForRuntime(input, deps) {
|
|
2924
|
-
const prepare = deps.prepareSignerRuntime ?? ((runtimeInput) => (
|
|
2925
|
-
// onProgress threaded through on purpose (#1586 review): without it the
|
|
2926
|
-
// install heartbeat was dead code in production and the console still
|
|
2927
|
-
// went silent for the whole cold install — the exact symptom the issue
|
|
2928
|
-
// set out to remove, at a longer timeout.
|
|
2929
|
-
prepareSignerRuntime(runtimeInput, { runCommand: deps.runCommand, onProgress: deps.onProgress })
|
|
2930
|
-
));
|
|
2931
|
-
return prepare({
|
|
2932
|
-
credentialDirectory: input.credentialDirectory,
|
|
2933
|
-
signerPath: input.signerPath,
|
|
2934
|
-
homeDir: deps.homeDir,
|
|
2935
|
-
serverName: input.serverName
|
|
3417
|
+
const signerProcess = primaryChecksById.get("signer_process");
|
|
3418
|
+
if (signerProcess) checks.push(signerProcess);
|
|
3419
|
+
const restart = restartRequiredForRuntime(input.runtime, deps.env);
|
|
3420
|
+
checks.push({
|
|
3421
|
+
id: "restart",
|
|
3422
|
+
label: "Runtime restart",
|
|
3423
|
+
ok: true,
|
|
3424
|
+
detail: restart ? "This runtime loads MCP config at startup \u2014 restart it after any repair before expecting the tools to appear." : "No restart requirement known for this runtime."
|
|
2936
3425
|
});
|
|
3426
|
+
const wiredOk = inventory.filter((entry) => entry.classification === "wired").every((entry) => entry.checks.every((check) => check.ok));
|
|
3427
|
+
return {
|
|
3428
|
+
version: 1,
|
|
3429
|
+
ok: checks.every((check) => check.ok) && wiredOk,
|
|
3430
|
+
runtime: input.runtime,
|
|
3431
|
+
credentialDirectory: primaryDirectory,
|
|
3432
|
+
checks,
|
|
3433
|
+
agents: inventory,
|
|
3434
|
+
...signerCapabilities ? { signerCapabilities } : {}
|
|
3435
|
+
};
|
|
2937
3436
|
}
|
|
2938
|
-
async function
|
|
2939
|
-
const
|
|
3437
|
+
async function runRepair(input, deps = {}) {
|
|
3438
|
+
const homeDir = deps.homeDir ?? homedir();
|
|
3439
|
+
const messages = [];
|
|
3440
|
+
const { directory, others } = await discoverCredentialDirectory(homeDir, input.credentialsDir);
|
|
3441
|
+
if (others.length > 0) {
|
|
3442
|
+
messages.push(`Note: ${others.length} other agent credential dir(s) exist \u2014 run --doctor for their status.`);
|
|
3443
|
+
}
|
|
3444
|
+
if (!directory) {
|
|
3445
|
+
return {
|
|
3446
|
+
ok: false,
|
|
3447
|
+
messages: [`No agent credentials found to repair \u2014 run the full setup: ${RERUN} --setup <token>.`]
|
|
3448
|
+
};
|
|
3449
|
+
}
|
|
3450
|
+
let identity;
|
|
2940
3451
|
try {
|
|
2941
|
-
|
|
3452
|
+
identity = JSON.parse(await readFile(join(directory, "identity.json"), "utf8"));
|
|
2942
3453
|
} catch {
|
|
2943
|
-
return {
|
|
3454
|
+
return { ok: false, messages: ["identity.json is unreadable \u2014 re-run the full setup with a fresh token."] };
|
|
2944
3455
|
}
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
if (err && typeof err === "object" && "code" in err && err.code === "local_mcp_unsupported_node_version") {
|
|
2948
|
-
return "local_mcp_unsupported_node_version";
|
|
3456
|
+
if (!identity.api_key || !(identity.hosted_mcp_url || identity.api_url)) {
|
|
3457
|
+
return { ok: false, messages: ["identity.json lacks the stored API key / hosted URL \u2014 re-run the full setup."] };
|
|
2949
3458
|
}
|
|
2950
|
-
|
|
3459
|
+
const configPath = runtimeConfigPathFor(input.runtime, homeDir);
|
|
3460
|
+
if (configPath) {
|
|
3461
|
+
try {
|
|
3462
|
+
const existing = await readFile(configPath, "utf8");
|
|
3463
|
+
if (existing.includes("bin/haven-mcp") || existing.includes(".haven/mcp-runtime")) {
|
|
3464
|
+
return {
|
|
3465
|
+
ok: false,
|
|
3466
|
+
messages: [
|
|
3467
|
+
`The config at ${configPath} is the LOCAL-stdio topology (--local). Repair currently rewrites only the hosted+signer shape and will not touch it.`,
|
|
3468
|
+
"Re-run your original setup command (with --local) to repair a local-stdio install."
|
|
3469
|
+
]
|
|
3470
|
+
};
|
|
3471
|
+
}
|
|
3472
|
+
} catch {
|
|
3473
|
+
}
|
|
3474
|
+
}
|
|
3475
|
+
const existingSidecar = await readRuntimeSidecar(directory);
|
|
3476
|
+
const serverName = existingSidecar?.server_name;
|
|
3477
|
+
const signerPath = join(directory, "signer.json");
|
|
3478
|
+
const prepared = await prepareSignerRuntime(
|
|
3479
|
+
{ credentialDirectory: directory, signerPath, homeDir, serverName },
|
|
3480
|
+
{ runCommand: deps.runCommand }
|
|
3481
|
+
);
|
|
3482
|
+
messages.push(...prepared.messages);
|
|
3483
|
+
const names = serverNamesFor(serverName);
|
|
3484
|
+
messages.push(`Rewriting MCP entries ${names.hosted} / ${names.signer}${serverName ? ` (agent "${serverName}")` : " (unnamed pair)"} \u2014 no other pair is touched.`);
|
|
3485
|
+
const configResult = await writeRuntimeConfig({
|
|
3486
|
+
runtime: input.runtime,
|
|
3487
|
+
hostedMcpUrl: identity.hosted_mcp_url ?? `${identity.api_url}/mcp`,
|
|
3488
|
+
apiKey: identity.api_key,
|
|
3489
|
+
identityPath: join(directory, "identity.json"),
|
|
3490
|
+
signerPath,
|
|
3491
|
+
credentialDirectory: directory,
|
|
3492
|
+
signerCommand: { command: prepared.command, args: prepared.args },
|
|
3493
|
+
homeDir,
|
|
3494
|
+
mode: "hosted",
|
|
3495
|
+
serverName
|
|
3496
|
+
});
|
|
3497
|
+
messages.push(...configResult.messages);
|
|
3498
|
+
messages.push("Repair complete \u2014 restart the runtime, then verify with --doctor.");
|
|
3499
|
+
return { ok: true, messages };
|
|
2951
3500
|
}
|
|
3501
|
+
var RERUN;
|
|
3502
|
+
var init_doctor = __esm({
|
|
3503
|
+
"src/doctor.ts"() {
|
|
3504
|
+
init_runtime_manifest();
|
|
3505
|
+
init_probes();
|
|
3506
|
+
init_signer_runtime();
|
|
3507
|
+
init_config_writers();
|
|
3508
|
+
init_runtime_registry();
|
|
3509
|
+
init_signer_consent();
|
|
3510
|
+
init_tombstone();
|
|
3511
|
+
init_server_names();
|
|
3512
|
+
init_storage();
|
|
3513
|
+
init_redact();
|
|
3514
|
+
RERUN = "npx @haven_ai/connect@alpha";
|
|
3515
|
+
}
|
|
3516
|
+
});
|
|
2952
3517
|
|
|
2953
3518
|
// src/runtime.ts
|
|
3519
|
+
init_api();
|
|
3520
|
+
init_key();
|
|
3521
|
+
init_redact();
|
|
3522
|
+
init_server_names();
|
|
3523
|
+
init_storage();
|
|
3524
|
+
init_runtime_install();
|
|
2954
3525
|
init_runtime_registry();
|
|
2955
3526
|
init_connect_error();
|
|
2956
3527
|
|
|
@@ -3123,8 +3694,9 @@ function defaultPromptIo() {
|
|
|
3123
3694
|
}
|
|
3124
3695
|
|
|
3125
3696
|
// src/runtime.ts
|
|
3697
|
+
init_local_mcp_runtime();
|
|
3126
3698
|
init_runtime_manifest();
|
|
3127
|
-
var CONNECTOR_VERSION = "0.1.
|
|
3699
|
+
var CONNECTOR_VERSION = "0.1.30-alpha.0";
|
|
3128
3700
|
var CONNECT_OUTCOME_SCHEMA_VERSION = 1;
|
|
3129
3701
|
async function runConnect(options, deps = {}) {
|
|
3130
3702
|
assertSupportedNodeVersion(deps.nodeVersion, MCP_RUNTIME_MANIFEST.minimumNodeVersion);
|
|
@@ -3200,6 +3772,11 @@ async function runConnect(options, deps = {}) {
|
|
|
3200
3772
|
proofSignature,
|
|
3201
3773
|
apiKeyHash: hashAgentApiKey(localApiKey),
|
|
3202
3774
|
apiKeyPrefix: agentApiKeyPrefix(localApiKey),
|
|
3775
|
+
// #1878: report the pair we are ACTUALLY wiring, bare pair included, so
|
|
3776
|
+
// the dashboard can name it. Derived here rather than sent as the raw
|
|
3777
|
+
// slug — `serverNamesFor` is the one place the naming rule lives, and
|
|
3778
|
+
// the hosted name is what a user pastes into an MCP config.
|
|
3779
|
+
mcpServerName: serverNamesFor(options.serverName).hosted,
|
|
3203
3780
|
connectorContext: {
|
|
3204
3781
|
environment_label: options.environmentLabel ?? "Local workspace",
|
|
3205
3782
|
config_target: installCapabilities.canWriteRuntimeConfig ? "agent runtime MCP config" : "local credential files"
|
|
@@ -3603,6 +4180,8 @@ function parseArgs(argv, env = process.env) {
|
|
|
3603
4180
|
let json = false;
|
|
3604
4181
|
let doctor = false;
|
|
3605
4182
|
let repair = false;
|
|
4183
|
+
let rekeyPhase;
|
|
4184
|
+
let newApiKey;
|
|
3606
4185
|
let tombstoneDir;
|
|
3607
4186
|
let tombstoneReason;
|
|
3608
4187
|
let tombstoneReplacedBy;
|
|
@@ -3616,6 +4195,12 @@ function parseArgs(argv, env = process.env) {
|
|
|
3616
4195
|
doctor = true;
|
|
3617
4196
|
} else if (arg === "--repair") {
|
|
3618
4197
|
repair = true;
|
|
4198
|
+
} else if (arg === "--rekey") {
|
|
4199
|
+
rekeyPhase = "start";
|
|
4200
|
+
} else if (arg === "--rekey-finish") {
|
|
4201
|
+
rekeyPhase = "finish";
|
|
4202
|
+
} else if (arg === "--api-key") {
|
|
4203
|
+
newApiKey = requireValue(argv, ++i, arg);
|
|
3619
4204
|
} else if (arg === "--tombstone") {
|
|
3620
4205
|
tombstoneDir = requireValue(argv, ++i, arg);
|
|
3621
4206
|
} else if (arg === "--reason") {
|
|
@@ -3653,20 +4238,38 @@ function parseArgs(argv, env = process.env) {
|
|
|
3653
4238
|
}
|
|
3654
4239
|
}
|
|
3655
4240
|
const tombstone = tombstoneDir ? { directory: tombstoneDir, reason: tombstoneReason, replacedBy: tombstoneReplacedBy } : void 0;
|
|
4241
|
+
const rekey = rekeyPhase ? { phase: rekeyPhase, newApiKey } : void 0;
|
|
3656
4242
|
if (help) {
|
|
3657
|
-
return { options, help, json, doctor, repair, tombstone };
|
|
4243
|
+
return { options, help, json, doctor, repair, tombstone, rekey };
|
|
4244
|
+
}
|
|
4245
|
+
if (rekey) {
|
|
4246
|
+
if (options.setupToken) {
|
|
4247
|
+
throw new Error("--rekey replaces an existing agent's key; it does not take --setup. Drop one of them.");
|
|
4248
|
+
}
|
|
4249
|
+
if (rekey.phase === "start" && newApiKey !== void 0) {
|
|
4250
|
+
throw new Error(
|
|
4251
|
+
"--api-key belongs to --rekey-finish. --rekey generates the new key here and prints the address to paste into Haven; the API key does not exist yet."
|
|
4252
|
+
);
|
|
4253
|
+
}
|
|
4254
|
+
if (rekey.phase === "finish" && !newApiKey) {
|
|
4255
|
+
throw new Error("--rekey-finish needs --api-key <key> \u2014 the one the Haven agent page showed once.");
|
|
4256
|
+
}
|
|
4257
|
+
return { options, help, json, doctor, repair, tombstone, rekey };
|
|
4258
|
+
}
|
|
4259
|
+
if (newApiKey !== void 0) {
|
|
4260
|
+
throw new Error("--api-key requires --rekey-finish.");
|
|
3658
4261
|
}
|
|
3659
4262
|
if (!tombstoneDir && (tombstoneReason !== void 0 || tombstoneReplacedBy !== void 0)) {
|
|
3660
4263
|
throw new Error("--reason and --replaced-by require --tombstone <dir>.");
|
|
3661
4264
|
}
|
|
3662
4265
|
if (tombstone) {
|
|
3663
|
-
return { options, help, json, doctor, repair, tombstone };
|
|
4266
|
+
return { options, help, json, doctor, repair, tombstone, rekey };
|
|
3664
4267
|
}
|
|
3665
4268
|
if (doctor || repair) {
|
|
3666
4269
|
if (!options.runtime) {
|
|
3667
4270
|
throw new Error("--doctor/--repair need --runtime <runtime> (which config to examine).");
|
|
3668
4271
|
}
|
|
3669
|
-
return { options, help, json, doctor, repair, tombstone };
|
|
4272
|
+
return { options, help, json, doctor, repair, tombstone, rekey };
|
|
3670
4273
|
}
|
|
3671
4274
|
if (!options.setupToken) {
|
|
3672
4275
|
throw new Error("Missing --setup <hv_setup_...> setup token.");
|
|
@@ -3675,7 +4278,7 @@ function parseArgs(argv, env = process.env) {
|
|
|
3675
4278
|
throw new Error("Missing --api <Haven API URL>.");
|
|
3676
4279
|
}
|
|
3677
4280
|
options.apiBaseUrl = options.apiBaseUrl.replace(/\/+$/, "");
|
|
3678
|
-
return { options, help, json, doctor, repair, tombstone };
|
|
4281
|
+
return { options, help, json, doctor, repair, tombstone, rekey };
|
|
3679
4282
|
}
|
|
3680
4283
|
function helpText() {
|
|
3681
4284
|
return [
|
|
@@ -3712,6 +4315,14 @@ function helpText() {
|
|
|
3712
4315
|
" --repair Repair, then re-diagnose (implies --doctor): reinstall the pinned signer",
|
|
3713
4316
|
" runtime, rewrite the wrapper and runtime config from stored credentials.",
|
|
3714
4317
|
" Hosted topology only (refuses to touch a --local config). No keys, no token.",
|
|
4318
|
+
" --rekey Replace this agent's signing key (no token). Generates a fresh keypair HERE and",
|
|
4319
|
+
" prints its public address to paste into the Haven agent page. Nothing changes",
|
|
4320
|
+
" until you finish; the agent keeps working on its old key throughout.",
|
|
4321
|
+
" Add --name <slug> for a named agent. Refuses a legacy-rail or revoked agent.",
|
|
4322
|
+
" --rekey-finish Second half of --rekey: writes the new key and the API key the agent page",
|
|
4323
|
+
" showed once, in place at the same path, and rewrites only this agent's MCP",
|
|
4324
|
+
" config pair. Server names do not change, so wired hosts need only a restart.",
|
|
4325
|
+
" --api-key <key> The new API key, for --rekey-finish.",
|
|
3715
4326
|
" --tombstone <dir> Retire an agent credential directory in place (no token): replaces its signer",
|
|
3716
4327
|
" wrapper with a diagnostic that names the retirement in MCP stderr logs, and",
|
|
3717
4328
|
" writes TOMBSTONE.json. Touches NO key material and revokes nothing.",
|
|
@@ -3757,13 +4368,13 @@ async function runCli(argv, io = {
|
|
|
3757
4368
|
}
|
|
3758
4369
|
if (parsed.tombstone) {
|
|
3759
4370
|
const { writeAgentTombstone: writeAgentTombstone2 } = await Promise.resolve().then(() => (init_tombstone(), tombstone_exports));
|
|
3760
|
-
const { readFile:
|
|
4371
|
+
const { readFile: readFile12 } = await import('fs/promises');
|
|
3761
4372
|
const { join: join10 } = await import('path');
|
|
3762
4373
|
try {
|
|
3763
4374
|
let agentId = "unknown";
|
|
3764
4375
|
try {
|
|
3765
4376
|
const identity = JSON.parse(
|
|
3766
|
-
await
|
|
4377
|
+
await readFile12(join10(parsed.tombstone.directory, "identity.json"), "utf8")
|
|
3767
4378
|
);
|
|
3768
4379
|
agentId = identity.agent_id ?? "unknown";
|
|
3769
4380
|
} catch {
|
|
@@ -3790,6 +4401,64 @@ async function runCli(argv, io = {
|
|
|
3790
4401
|
return 0;
|
|
3791
4402
|
} catch (err) {
|
|
3792
4403
|
io.stderr(`${redactSecrets(err instanceof Error ? err.message : String(err))}
|
|
4404
|
+
`);
|
|
4405
|
+
return 1;
|
|
4406
|
+
}
|
|
4407
|
+
}
|
|
4408
|
+
if (parsed.rekey) {
|
|
4409
|
+
const { startRekey: startRekey2, finishRekey: finishRekey2 } = await Promise.resolve().then(() => (init_rekey(), rekey_exports));
|
|
4410
|
+
const { restartGuidance: restartGuidance2 } = await Promise.resolve().then(() => (init_rekey_restart(), rekey_restart_exports));
|
|
4411
|
+
const common = {
|
|
4412
|
+
serverName: parsed.options.serverName,
|
|
4413
|
+
credentialsDir: parsed.options.credentialsDir,
|
|
4414
|
+
runtime: parsed.options.runtime
|
|
4415
|
+
};
|
|
4416
|
+
try {
|
|
4417
|
+
if (parsed.rekey.phase === "start") {
|
|
4418
|
+
const result2 = await startRekey2(common);
|
|
4419
|
+
if (parsed.json) {
|
|
4420
|
+
io.stdout(
|
|
4421
|
+
`${redactSecrets(
|
|
4422
|
+
JSON.stringify({
|
|
4423
|
+
rekey: "started",
|
|
4424
|
+
agent_id: result2.agentId,
|
|
4425
|
+
new_delegate_address: result2.newDelegateAddress,
|
|
4426
|
+
expires_at: result2.expiresAt
|
|
4427
|
+
})
|
|
4428
|
+
)}
|
|
4429
|
+
`
|
|
4430
|
+
);
|
|
4431
|
+
} else {
|
|
4432
|
+
for (const line of result2.messages) io.stdout(redactSecrets(`${line}
|
|
4433
|
+
`));
|
|
4434
|
+
}
|
|
4435
|
+
return 0;
|
|
4436
|
+
}
|
|
4437
|
+
const result = await finishRekey2({ ...common, newApiKey: parsed.rekey.newApiKey });
|
|
4438
|
+
const restart = restartGuidance2(parsed.options.runtime);
|
|
4439
|
+
if (parsed.json) {
|
|
4440
|
+
io.stdout(
|
|
4441
|
+
`${redactSecrets(
|
|
4442
|
+
JSON.stringify({
|
|
4443
|
+
rekey: "finished",
|
|
4444
|
+
agent_id: result.agentId,
|
|
4445
|
+
new_delegate_address: result.newDelegateAddress,
|
|
4446
|
+
mcp_servers: result.serverNames,
|
|
4447
|
+
restart_commands: restart.commands
|
|
4448
|
+
})
|
|
4449
|
+
)}
|
|
4450
|
+
`
|
|
4451
|
+
);
|
|
4452
|
+
} else {
|
|
4453
|
+
for (const line of result.messages) io.stdout(redactSecrets(`${line}
|
|
4454
|
+
`));
|
|
4455
|
+
io.stdout("\n");
|
|
4456
|
+
for (const line of restart.lines) io.stdout(redactSecrets(`${line}
|
|
4457
|
+
`));
|
|
4458
|
+
}
|
|
4459
|
+
return 0;
|
|
4460
|
+
} catch (err) {
|
|
4461
|
+
io.stderr(`${redactSecrets(err instanceof Error ? err.message : String(err))}
|
|
3793
4462
|
`);
|
|
3794
4463
|
return 1;
|
|
3795
4464
|
}
|
|
@@ -3822,7 +4491,7 @@ async function runCli(argv, io = {
|
|
|
3822
4491
|
for (const agent of otherAgents) {
|
|
3823
4492
|
const name = agent.slug ? `${agent.slug} (${agent.agentId ?? "unknown"})` : agent.agentId ?? "unknown";
|
|
3824
4493
|
const failed = agent.checks.filter((check) => !check.ok);
|
|
3825
|
-
const verdict = agent.classification === "wired" ? failed.length === 0 ? "wired, all checks passed" : `wired, ${failed.length} check(s) FAILED` : agent.classification;
|
|
4494
|
+
const verdict = agent.classification === "wired" ? failed.length === 0 ? "wired, all checks passed" : `wired, ${failed.length} check(s) FAILED` : agent.classification === "parked" ? "parked re-key only \u2014 no identity.json in this directory, but key material is still there" : agent.classification;
|
|
3826
4495
|
io.stdout(redactSecrets(` ${failed.length > 0 ? "\u2717" : "\u2022"} ${name}: ${verdict}
|
|
3827
4496
|
`));
|
|
3828
4497
|
for (const check of failed) {
|