machine-bridge-mcp 0.12.2 → 0.14.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.
@@ -0,0 +1,56 @@
1
+ import { createHmac, randomBytes } from "node:crypto";
2
+
3
+ export class CapabilityObserver {
4
+ constructor(now = () => Date.now(), taskFingerprintKey = randomBytes(32)) {
5
+ this.now = now;
6
+ this.taskFingerprintKey = Buffer.from(taskFingerprintKey);
7
+ this.bootstrapCount = 0;
8
+ this.resolutionCount = 0;
9
+ this.lastBootstrap = null;
10
+ this.lastResolution = null;
11
+ }
12
+
13
+ recordBootstrap(result) {
14
+ this.bootstrapCount += 1;
15
+ this.lastBootstrap = {
16
+ observed_at: timestamp(this.now()),
17
+ target: result?.target || ".",
18
+ instruction_bytes: Buffer.byteLength(String(result?.instructions || "")),
19
+ builtin_loaded: Boolean(result?.builtin_instructions),
20
+ automatic_project_context_loaded: Boolean(result?.automatic_project_context),
21
+ global_model_instructions_loaded: Boolean(result?.model_instructions_file),
22
+ refresh_fingerprint: String(result?.capability_refresh?.instruction_and_command_fingerprint || ""),
23
+ };
24
+ }
25
+
26
+ recordResolution(task, result) {
27
+ this.resolutionCount += 1;
28
+ this.lastResolution = {
29
+ observed_at: timestamp(this.now()),
30
+ task_fingerprint: createHmac("sha256", this.taskFingerprintKey).update(String(task || "")).digest("hex"),
31
+ target: result?.target || ".",
32
+ selected_skill: result?.selected_skill?.name || null,
33
+ matched_skills: Array.isArray(result?.skill_matches) ? result.skill_matches.length : 0,
34
+ matched_commands: Array.isArray(result?.command_matches) ? result.command_matches.length : 0,
35
+ matched_applications: Array.isArray(result?.application_matches) ? result.application_matches.length : 0,
36
+ recommended_tools: Array.isArray(result?.recommended_tools) ? [...result.recommended_tools] : [],
37
+ refresh_fingerprint: String(result?.refresh?.fingerprint || ""),
38
+ };
39
+ }
40
+
41
+ snapshot() {
42
+ return {
43
+ bootstrap_observed: this.bootstrapCount > 0,
44
+ bootstrap_count: this.bootstrapCount,
45
+ task_resolution_observed: this.resolutionCount > 0,
46
+ task_resolution_count: this.resolutionCount,
47
+ last_bootstrap: this.lastBootstrap ? structuredClone(this.lastBootstrap) : null,
48
+ last_task_resolution: this.lastResolution ? structuredClone(this.lastResolution) : null,
49
+ enforcement_boundary: "The server can discover, rank, load, and report capabilities; the MCP host decides whether to call the resolver or recommended tools.",
50
+ };
51
+ }
52
+ }
53
+
54
+ function timestamp(milliseconds) {
55
+ return new Date(Number(milliseconds)).toISOString();
56
+ }
package/src/local/cli.mjs CHANGED
@@ -10,7 +10,7 @@ import { runStdioServer } from "./stdio.mjs";
10
10
  import { assertCanonicalFullPolicy, DEFAULT_POLICY_PROFILE, DEFAULT_POLICY_REVISION, POLICY_PROFILES, normalizePolicy, policyProfile, toolsForPolicy } from "./tools.mjs";
11
11
  import { classifyOperationalError, createLogger, normalizeLogLevel, sanitizeLogText } from "./log.mjs";
12
12
  import { activeManagedJobs, inspectResourceFile, loadManagedJobPlan, ManagedJobManager, publicResourceRegistry, validateResourceName } from "./managed-jobs.mjs";
13
- import { runWrangler } from "./shell.mjs";
13
+ import { run, runWrangler } from "./shell.mjs";
14
14
  import { generateRegisteredSshKey } from "./resource-operations.mjs";
15
15
  import { runFullAccessTest } from "./full-access-test.mjs";
16
16
  import { readBoundedRegularFileSync } from "./secure-file.mjs";
@@ -1124,6 +1124,10 @@ async function doctorCommand(args) {
1124
1124
  const workspace = await chooseWorkspace(args, { promptOnFirstRun: false, save: false, allowPositional: true });
1125
1125
  const checks = [];
1126
1126
  checks.push({ name: "node", ok: isSupportedNodeVersion(), detail: process.version });
1127
+ const npmCommand = npmVersionCommand();
1128
+ const npm = await run(npmCommand.file, npmCommand.args, { capture: true, allowFailure: true, timeoutMs: 10_000 });
1129
+ const npmDetail = sanitizeLines(npm.stdout || npm.stderr);
1130
+ checks.push({ name: "npm", ok: npm.code === 0 && isSupportedNpmVersion(npmDetail), detail: npmDetail || "unavailable" });
1127
1131
  const wrangler = await runWrangler(["--version"], { capture: true, allowFailure: true });
1128
1132
  checks.push({ name: "wrangler", ok: wrangler.code === 0, detail: (wrangler.stdout || wrangler.stderr).trim() });
1129
1133
  const whoami = await runWrangler(["whoami"], { capture: true, allowFailure: true });
@@ -1557,10 +1561,22 @@ function validateWorkerName(value) {
1557
1561
  }
1558
1562
 
1559
1563
  export function isSupportedNodeVersion(version = process.versions.node) {
1560
- const major = Number(String(version || "").split(".")[0]);
1564
+ const major = Number(String(version || "").replace(/^v/, "").split(".")[0]);
1561
1565
  return Number.isInteger(major) && major >= 26;
1562
1566
  }
1563
1567
 
1568
+ export function isSupportedNpmVersion(version) {
1569
+ const major = Number(String(version || "").trim().replace(/^v/, "").split(".")[0]);
1570
+ return Number.isInteger(major) && major >= 12;
1571
+ }
1572
+
1573
+ export function npmVersionCommand(platform = process.platform, comspec = process.env.ComSpec) {
1574
+ if (platform === "win32") {
1575
+ return { file: comspec || "cmd.exe", args: ["/d", "/s", "/c", "npm --version"] };
1576
+ }
1577
+ return { file: "npm", args: ["--version"] };
1578
+ }
1579
+
1564
1580
  function assertNodeVersion() {
1565
1581
  if (!isSupportedNodeVersion()) throw new Error(`Node.js >=26 is required; current ${process.version}`);
1566
1582
  }
@@ -1568,8 +1584,10 @@ function assertNodeVersion() {
1568
1584
  function usage() {
1569
1585
  console.log(`machine-bridge-mcp
1570
1586
 
1587
+ Installation (run from a package-free temporary directory; Node.js >=26):
1588
+ npx --yes npm@12.0.1 install --global --omit=optional --allow-scripts=esbuild,workerd,sharp,fsevents machine-bridge-mcp@latest && machine-mcp
1589
+
1571
1590
  Usage:
1572
- npm install -g --omit=optional --allow-scripts=esbuild,workerd,sharp,fsevents machine-bridge-mcp@latest && machine-mcp
1573
1591
  npx machine-bridge-mcp@latest # no global install; autostart may rely on npm cache
1574
1592
  ./mbm # from source checkout
1575
1593
  .\\mbm.cmd # from source checkout on Windows cmd
@@ -2,8 +2,8 @@ import { createHash } from "node:crypto";
2
2
  import { constants as fsConstants } from "node:fs";
3
3
  import { lstat, open, opendir } from "node:fs/promises";
4
4
  import { join, relative, resolve, sep } from "node:path";
5
+ import { packageScriptDisplayCommand, readProjectPackageMetadata, safeVersionValue } from "./project-package.mjs";
5
6
 
6
- const MAX_METADATA_FILE_BYTES = 1024 * 1024;
7
7
  const MAX_PROJECT_CONTEXT_BYTES = 16 * 1024;
8
8
  const MAX_SCRIPT_NAMES = 24;
9
9
  const MAX_WORKFLOW_FILES = 20;
@@ -77,14 +77,6 @@ const DOCUMENT_FILES = Object.freeze([
77
77
  "docs/TESTING.md",
78
78
  ]);
79
79
 
80
- const LOCKFILES = Object.freeze([
81
- ["package-lock.json", "npm"],
82
- ["pnpm-lock.yaml", "pnpm"],
83
- ["yarn.lock", "yarn"],
84
- ["bun.lock", "bun"],
85
- ["bun.lockb", "bun"],
86
- ]);
87
-
88
80
  const CI_FILES = Object.freeze([
89
81
  ".gitlab-ci.yml",
90
82
  "azure-pipelines.yml",
@@ -155,52 +147,34 @@ export async function discoverAutomaticProjectInstruction({
155
147
  }
156
148
 
157
149
  async function readPackageFacts(root, throwIfCancelled) {
158
- const packagePath = join(root, "package.json");
159
- const packageText = await readOptionalRegularUtf8(packagePath, MAX_METADATA_FILE_BYTES);
160
- const lockfiles = [];
161
- for (const [name, manager] of LOCKFILES) {
162
- throwIfCancelled();
163
- if (await isRegularNonSymlink(join(root, name))) lockfiles.push({ name, manager });
164
- }
165
- if (!packageText) {
166
- const lines = lockfiles.length
167
- ? [`- JavaScript lockfiles detected without readable root package metadata: ${lockfiles.map((item) => `\`${item.name}\``).join(", ")}. Inspect before installing dependencies.`]
150
+ const metadata = await readProjectPackageMetadata(root, throwIfCancelled);
151
+ if (!metadata.detected) return { detected: false, lines: [] };
152
+ if (metadata.packageState === "missing") {
153
+ const lines = metadata.lockfiles.length
154
+ ? [`- JavaScript lockfiles detected without readable root package metadata: ${metadata.lockfiles.map((item) => `\`${item.name}\``).join(", ")}. Inspect before installing dependencies.`]
168
155
  : [];
169
- return { detected: lockfiles.length > 0, lines };
156
+ return { detected: metadata.lockfiles.length > 0, lines };
170
157
  }
171
-
172
- let parsed;
173
- try {
174
- parsed = JSON.parse(packageText);
175
- } catch {
158
+ if (metadata.packageState === "invalid-json") {
176
159
  return { detected: true, lines: ["- A root `package.json` exists but is not valid JSON. Do not infer package commands until it is repaired or understood."] };
177
160
  }
178
- if (!isPlainRecord(parsed)) return { detected: true, lines: ["- A root `package.json` exists but is not a JSON object."] };
161
+ if (metadata.packageState === "invalid-root") {
162
+ return { detected: true, lines: ["- A root `package.json` exists but is not a JSON object."] };
163
+ }
179
164
 
180
165
  const lines = [];
181
- const declaredManager = safePackageManager(parsed.packageManager);
182
- const managerName = packageManagerName(declaredManager) || uniqueLockManager(lockfiles);
183
- if (declaredManager) lines.push(`- Declared package manager: \`${escapeInlineCode(declaredManager)}\`.`);
184
- if (lockfiles.length === 1) lines.push(`- Package lockfile: \`${lockfiles[0].name}\`. Preserve it and use the matching package manager.`);
185
- if (lockfiles.length > 1) lines.push(`- Multiple JavaScript lockfiles are present: ${lockfiles.map((item) => `\`${item.name}\``).join(", ")}. Do not choose or rewrite one automatically; inspect project guidance first.`);
186
-
187
- const scripts = isPlainRecord(parsed.scripts)
188
- ? Object.entries(parsed.scripts).filter(([name, value]) => validScriptName(name) && typeof value === "string").map(([name]) => name)
189
- : [];
190
- if (scripts.length) {
191
- const selected = prioritizeScriptNames(scripts).slice(0, MAX_SCRIPT_NAMES);
192
- const commands = selected.map((name) => formatPackageScriptCommand(managerName, name));
193
- const suffix = scripts.length > selected.length ? `; ${scripts.length - selected.length} additional script(s) omitted` : "";
166
+ if (metadata.declaredManager) lines.push(`- Declared package manager: \`${escapeInlineCode(metadata.declaredManager)}\`.`);
167
+ if (metadata.lockfiles.length === 1) lines.push(`- Package lockfile: \`${metadata.lockfiles[0].name}\`. Preserve it and use the matching package manager.`);
168
+ if (metadata.lockfiles.length > 1) lines.push(`- Multiple JavaScript lockfiles are present: ${metadata.lockfiles.map((item) => `\`${item.name}\``).join(", ")}. Do not choose or rewrite one automatically; inspect project guidance first.`);
169
+ if (metadata.scripts.length) {
170
+ const selected = metadata.scripts.slice(0, MAX_SCRIPT_NAMES);
171
+ const commands = selected.map((name) => packageScriptDisplayCommand(metadata.managerName, name));
172
+ const suffix = metadata.scripts.length > selected.length ? `; ${metadata.scripts.length - selected.length} additional script(s) omitted` : "";
194
173
  lines.push(`- Declared package scripts (names only; bodies are not injected): ${commands.map((command) => `\`${escapeInlineCode(command)}\``).join(", ")}${suffix}.`);
195
174
  }
196
-
197
- if (isPlainRecord(parsed.engines)) {
198
- const engines = Object.entries(parsed.engines)
199
- .map(([name, value]) => [name, safeVersionValue(value)])
200
- .filter(([name, value]) => /^[A-Za-z0-9_.-]{1,40}$/.test(name) && value)
201
- .slice(0, 10)
202
- .map(([name, value]) => `\`${escapeInlineCode(name)} ${escapeInlineCode(value)}\``);
203
- if (engines.length) lines.push(`- Declared runtime constraints: ${engines.join(", ")}.`);
175
+ if (metadata.engines.length) {
176
+ const engines = metadata.engines.map(([name, value]) => `\`${escapeInlineCode(name)} ${escapeInlineCode(value)}\``);
177
+ lines.push(`- Declared runtime constraints: ${engines.join(", ")}.`);
204
178
  }
205
179
  return { detected: true, lines };
206
180
  }
@@ -275,50 +249,6 @@ function virtualInstruction(source, content, precedence, scope) {
275
249
  };
276
250
  }
277
251
 
278
- function packageManagerName(value) {
279
- if (!value) return "";
280
- const match = /^(npm|pnpm|yarn|bun)(?:@|$)/.exec(value.trim());
281
- return match?.[1] || "";
282
- }
283
-
284
- function uniqueLockManager(lockfiles) {
285
- const managers = [...new Set(lockfiles.map((item) => item.manager))];
286
- return managers.length === 1 ? managers[0] : "";
287
- }
288
-
289
- function formatPackageScriptCommand(manager, name) {
290
- if (manager === "pnpm") return `pnpm run ${name}`;
291
- if (manager === "yarn") return `yarn ${name}`;
292
- if (manager === "bun") return `bun run ${name}`;
293
- return `npm run ${name}`;
294
- }
295
-
296
- function prioritizeScriptNames(names) {
297
- const preferred = ["check", "test", "lint", "typecheck", "build", "format", "verify", "ci", "prepack", "start", "dev"];
298
- const rank = new Map(preferred.map((name, index) => [name, index]));
299
- return [...new Set(names)].sort((left, right) => {
300
- const leftRank = rank.has(left) ? rank.get(left) : preferred.length;
301
- const rightRank = rank.has(right) ? rank.get(right) : preferred.length;
302
- return leftRank - rightRank || left.localeCompare(right);
303
- });
304
- }
305
-
306
- function validScriptName(value) {
307
- return typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9:._/-]{0,119}$/.test(value);
308
- }
309
-
310
- function safePackageManager(value) {
311
- if (typeof value !== "string") return "";
312
- const normalized = value.trim();
313
- return /^(?:npm|pnpm|yarn|bun)@[A-Za-z0-9][A-Za-z0-9.+_-]{0,90}$/.test(normalized) ? normalized : "";
314
- }
315
-
316
- function safeVersionValue(value) {
317
- if (typeof value !== "string") return "";
318
- const normalized = safeSingleLine(value, 120);
319
- return normalized && /^[A-Za-z0-9][A-Za-z0-9 ._*+<>=~^|&!/-]{0,119}$/.test(normalized) ? normalized : "";
320
- }
321
-
322
252
  function skippableMetadataError(error) {
323
253
  return ["ENOENT", "ENOTDIR", "EACCES", "EPERM", "ELOOP", "EBUSY"].includes(error?.code);
324
254
  }
@@ -0,0 +1,34 @@
1
+ import { HttpsProxyAgent } from "https-proxy-agent";
2
+ import { getProxyForUrl } from "proxy-from-env";
3
+
4
+ export function proxyAgentForWebSocket(webSocketUrl, proxyResolver = getProxyForUrl) {
5
+ const target = new URL(String(webSocketUrl));
6
+ if (target.protocol !== "ws:" && target.protocol !== "wss:") throw new Error("relay WebSocket URL must use ws or wss");
7
+ const proxyLookupUrl = new URL(target);
8
+ proxyLookupUrl.protocol = target.protocol === "wss:" ? "https:" : "http:";
9
+ const proxyValue = String(proxyResolver(proxyLookupUrl.href) || "").trim();
10
+ if (!proxyValue) return { agent: null, mode: "direct" };
11
+ let proxyUrl;
12
+ try {
13
+ proxyUrl = new URL(proxyValue);
14
+ } catch {
15
+ throw proxyConfigurationError("relay proxy configuration is not a valid URL");
16
+ }
17
+ if (!new Set(["http:", "https:"]).has(proxyUrl.protocol)) {
18
+ throw proxyConfigurationError("relay proxy must use HTTP or HTTPS");
19
+ }
20
+ try {
21
+ return {
22
+ agent: new HttpsProxyAgent(proxyUrl),
23
+ mode: "proxy",
24
+ };
25
+ } catch {
26
+ throw proxyConfigurationError("relay proxy configuration could not be initialized");
27
+ }
28
+ }
29
+
30
+ function proxyConfigurationError(message) {
31
+ const error = new Error(message);
32
+ error.code = "relay_proxy_configuration";
33
+ return error;
34
+ }
@@ -253,6 +253,13 @@ export function terminateProcessTree(child, signal) {
253
253
  }
254
254
  }
255
255
 
256
+ export function terminateProcessTreeWithEscalation(child, options = {}) {
257
+ const graceMs = Number.isFinite(Number(options.graceMs)) ? Math.max(0, Number(options.graceMs)) : 2000;
258
+ const schedule = typeof options.setTimeout === "function" ? options.setTimeout : setTimeout;
259
+ terminateProcessTree(child, "SIGTERM");
260
+ return schedule(() => terminateProcessTree(child, "SIGKILL"), graceMs);
261
+ }
262
+
256
263
  function waitForSpawn(child) {
257
264
  return new Promise((resolvePromise, rejectPromise) => {
258
265
  const onSpawn = () => { cleanup(); resolvePromise(); };
@@ -0,0 +1,234 @@
1
+ import { constants as fsConstants } from "node:fs";
2
+ import { lstat, open } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+
5
+ const MAX_METADATA_FILE_BYTES = 1024 * 1024;
6
+ const MAX_PACKAGE_COMMANDS = 64;
7
+ const PACKAGE_MANAGERS = new Set(["npm", "pnpm", "yarn", "bun"]);
8
+
9
+ const PACKAGE_SCRIPT_INTENTS = Object.freeze({
10
+ check: "check verify validate validation test lint typecheck 完整测试 检查 验证 测试 校验 审查",
11
+ verify: "check verify validate validation test 检查 验证 测试 校验",
12
+ validate: "check verify validate validation test 检查 验证 测试 校验",
13
+ test: "test tests testing unit integration 完整测试 测试 单元测试 集成测试",
14
+ lint: "lint static analysis code quality 检查 静态检查 代码质量",
15
+ typecheck: "typecheck type check typescript 类型检查",
16
+ build: "build compile bundle package 构建 编译 打包",
17
+ compile: "build compile 构建 编译",
18
+ format: "format formatting 格式化",
19
+ start: "start run launch 启动 运行",
20
+ dev: "develop development start server 开发 启动 服务",
21
+ serve: "serve server start 服务 启动",
22
+ audit: "audit security vulnerability 审计 安全 漏洞",
23
+ deploy: "deploy deployment 部署",
24
+ release: "release publish 发布",
25
+ publish: "publish release 发布",
26
+ pack: "pack package 打包",
27
+ prepack: "pack package 打包",
28
+ });
29
+
30
+ const LOCKFILES = Object.freeze([
31
+ ["package-lock.json", "npm"],
32
+ ["pnpm-lock.yaml", "pnpm"],
33
+ ["yarn.lock", "yarn"],
34
+ ["bun.lock", "bun"],
35
+ ["bun.lockb", "bun"],
36
+ ]);
37
+
38
+ export async function readProjectPackageMetadata(root, throwIfCancelled = () => {}) {
39
+ const packagePath = join(root, "package.json");
40
+ const packageText = await readOptionalRegularUtf8(packagePath, MAX_METADATA_FILE_BYTES);
41
+ const lockfiles = [];
42
+ for (const [name, manager] of LOCKFILES) {
43
+ throwIfCancelled();
44
+ if (await isRegularNonSymlink(join(root, name))) lockfiles.push({ name, manager });
45
+ }
46
+ if (!packageText) {
47
+ return {
48
+ detected: lockfiles.length > 0,
49
+ packagePath,
50
+ packageState: "missing",
51
+ declaredManager: "",
52
+ managerName: uniqueLockManager(lockfiles),
53
+ lockfiles,
54
+ scripts: [],
55
+ engines: [],
56
+ };
57
+ }
58
+
59
+ let parsed;
60
+ try {
61
+ parsed = JSON.parse(packageText);
62
+ } catch {
63
+ return invalidPackageMetadata(packagePath, lockfiles, "invalid-json");
64
+ }
65
+ if (!isPlainRecord(parsed)) return invalidPackageMetadata(packagePath, lockfiles, "invalid-root");
66
+
67
+ const declaredManager = safePackageManager(parsed.packageManager);
68
+ const scripts = isPlainRecord(parsed.scripts)
69
+ ? Object.entries(parsed.scripts)
70
+ .filter(([name, value]) => validScriptName(name) && typeof value === "string")
71
+ .map(([name]) => name)
72
+ : [];
73
+ const engines = isPlainRecord(parsed.engines)
74
+ ? Object.entries(parsed.engines)
75
+ .map(([name, value]) => [name, safeVersionValue(value)])
76
+ .filter(([name, value]) => /^[A-Za-z0-9_.-]{1,40}$/.test(name) && value)
77
+ .slice(0, 10)
78
+ : [];
79
+
80
+ const inferredManager = packageManagerName(declaredManager) || uniqueLockManager(lockfiles);
81
+ return {
82
+ detected: true,
83
+ packagePath,
84
+ packageState: "valid",
85
+ declaredManager,
86
+ managerName: inferredManager || (lockfiles.length === 0 ? "npm" : ""),
87
+ lockfiles,
88
+ scripts: prioritizeScriptNames(scripts),
89
+ engines,
90
+ };
91
+ }
92
+
93
+ export function packageScriptCommand(manager, script, platform = process.platform, commandShell = process.env.ComSpec || "cmd.exe") {
94
+ const executable = PACKAGE_MANAGERS.has(manager) ? manager : "npm";
95
+ if (!validScriptName(script)) throw new Error("package script name is invalid");
96
+ if (platform === "win32") return [commandShell, "/d", "/s", "/c", `${executable} run ${script}`];
97
+ return [executable, "run", script];
98
+ }
99
+
100
+ export function packageScriptDisplayCommand(manager, script) {
101
+ const executable = PACKAGE_MANAGERS.has(manager) ? manager : "npm";
102
+ if (!validScriptName(script)) throw new Error("package script name is invalid");
103
+ return `${executable} run ${script}`;
104
+ }
105
+
106
+ export function automaticPackageCommands(metadata, cwd, platform = process.platform) {
107
+ if (metadata?.packageState !== "valid" || !metadata.managerName || !metadata.scripts?.length) return new Map();
108
+ const commands = new Map();
109
+ for (const script of metadata.scripts.slice(0, MAX_PACKAGE_COMMANDS)) {
110
+ const base = normalizedCommandName(script);
111
+ if (!base) continue;
112
+ let name = `package.${base}`;
113
+ let suffix = 2;
114
+ while (commands.has(name)) name = `package.${base}.${suffix++}`;
115
+ commands.set(name, {
116
+ name,
117
+ description: `Run the declared package script '${script}' using the detected package manager.`,
118
+ argv: packageScriptCommand(metadata.managerName, script, platform),
119
+ cwd,
120
+ timeoutSeconds: 600,
121
+ allowExtraArgs: false,
122
+ source: metadata.packagePath,
123
+ sourceType: "automatic-package-script",
124
+ searchTerms: packageScriptSearchTerms(script),
125
+ script,
126
+ });
127
+ }
128
+ return commands;
129
+ }
130
+
131
+ export function safeVersionValue(value) {
132
+ if (typeof value !== "string") return "";
133
+ const normalized = safeSingleLine(value, 120);
134
+ return normalized && /^[A-Za-z0-9][A-Za-z0-9 ._*+<>=~^|&!/-]{0,119}$/.test(normalized) ? normalized : "";
135
+ }
136
+
137
+
138
+ function packageScriptSearchTerms(script) {
139
+ const normalized = String(script).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
140
+ const root = normalized.split("-")[0];
141
+ return PACKAGE_SCRIPT_INTENTS[root] || "";
142
+ }
143
+
144
+ function invalidPackageMetadata(packagePath, lockfiles, packageState) {
145
+ return {
146
+ detected: true,
147
+ packagePath,
148
+ packageState,
149
+ declaredManager: "",
150
+ managerName: uniqueLockManager(lockfiles),
151
+ lockfiles,
152
+ scripts: [],
153
+ engines: [],
154
+ };
155
+ }
156
+
157
+ function packageManagerName(value) {
158
+ if (!value) return "";
159
+ const match = /^(npm|pnpm|yarn|bun)(?:@|$)/.exec(value.trim());
160
+ return match?.[1] || "";
161
+ }
162
+
163
+ function uniqueLockManager(lockfiles) {
164
+ const managers = [...new Set(lockfiles.map((item) => item.manager))];
165
+ return managers.length === 1 ? managers[0] : "";
166
+ }
167
+
168
+ function prioritizeScriptNames(names) {
169
+ const preferred = ["check", "test", "lint", "typecheck", "build", "format", "verify", "ci", "prepack", "start", "dev"];
170
+ const rank = new Map(preferred.map((name, index) => [name, index]));
171
+ return [...new Set(names)].sort((left, right) => {
172
+ const leftRank = rank.has(left) ? rank.get(left) : preferred.length;
173
+ const rightRank = rank.has(right) ? rank.get(right) : preferred.length;
174
+ return leftRank - rightRank || left.localeCompare(right);
175
+ });
176
+ }
177
+
178
+ function validScriptName(value) {
179
+ return typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9:._/-]{0,119}$/.test(value);
180
+ }
181
+
182
+ function normalizedCommandName(value) {
183
+ const normalized = String(value).toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 48);
184
+ return /^[a-z][a-z0-9._-]{0,47}$/.test(normalized) ? normalized : "";
185
+ }
186
+
187
+ function safePackageManager(value) {
188
+ if (typeof value !== "string") return "";
189
+ const normalized = value.trim();
190
+ return /^(?:npm|pnpm|yarn|bun)@[A-Za-z0-9][A-Za-z0-9.+_-]{0,90}$/.test(normalized) ? normalized : "";
191
+ }
192
+
193
+ async function isRegularNonSymlink(filePath) {
194
+ const info = await lstat(filePath).catch((error) => skippableMetadataError(error) ? null : Promise.reject(error));
195
+ return Boolean(info && !info.isSymbolicLink() && info.isFile());
196
+ }
197
+
198
+ async function readOptionalRegularUtf8(filePath, maxBytes) {
199
+ const info = await lstat(filePath).catch((error) => skippableMetadataError(error) ? null : Promise.reject(error));
200
+ if (!info || info.isSymbolicLink() || !info.isFile() || info.size > maxBytes) return null;
201
+ const handle = await open(filePath, fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW || 0)).catch((error) => skippableMetadataError(error) ? null : Promise.reject(error));
202
+ if (!handle) return null;
203
+ try {
204
+ const current = await handle.stat();
205
+ if (!current.isFile() || current.size > maxBytes) return null;
206
+ const buffer = Buffer.alloc(current.size);
207
+ let offset = 0;
208
+ while (offset < buffer.length) {
209
+ const { bytesRead } = await handle.read(buffer, offset, buffer.length - offset, offset);
210
+ if (!bytesRead) break;
211
+ offset += bytesRead;
212
+ }
213
+ try {
214
+ return new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, offset));
215
+ } catch {
216
+ return null;
217
+ }
218
+ } finally {
219
+ await handle.close();
220
+ }
221
+ }
222
+
223
+ function skippableMetadataError(error) {
224
+ return ["ENOENT", "ENOTDIR", "EACCES", "EPERM", "ELOOP", "EBUSY"].includes(error?.code);
225
+ }
226
+
227
+ function safeSingleLine(value, maxLength) {
228
+ if (typeof value !== "string") return "";
229
+ return value.replace(/[\r\n\t]+/g, " ").replace(/\s+/g, " ").trim().slice(0, maxLength);
230
+ }
231
+
232
+ function isPlainRecord(value) {
233
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
234
+ }
@@ -1,5 +1,6 @@
1
1
  import WebSocket from "ws";
2
2
  import { classifyOperationalError } from "./log.mjs";
3
+ import { proxyAgentForWebSocket } from "./network-proxy.mjs";
3
4
 
4
5
  const DEFAULT_HEARTBEAT_INTERVAL_MS = 25_000;
5
6
  const DEFAULT_HEARTBEAT_TIMEOUT_MS = 75_000;
@@ -35,6 +36,8 @@ export class RelayConnection {
35
36
  this.scheduler = options.scheduler || DEFAULT_SCHEDULER;
36
37
  this.now = typeof options.now === "function" ? options.now : Date.now;
37
38
  this.reconnectDelay = typeof options.reconnectDelay === "function" ? options.reconnectDelay : reconnectDelay;
39
+ this.proxyAgentForUrl = typeof options.proxyAgentForUrl === "function" ? options.proxyAgentForUrl : proxyAgentForWebSocket;
40
+ this.networkRoute = "unresolved";
38
41
  this.maxPayload = boundedPositiveInteger(options.maxPayload, 8 * 1024 * 1024);
39
42
  this.heartbeatIntervalMs = boundedPositiveInteger(options.heartbeatIntervalMs, DEFAULT_HEARTBEAT_INTERVAL_MS);
40
43
  this.heartbeatTimeoutMs = boundedPositiveInteger(options.heartbeatTimeoutMs, DEFAULT_HEARTBEAT_TIMEOUT_MS);
@@ -72,6 +75,16 @@ export class RelayConnection {
72
75
  this.connectedOnceReject = null;
73
76
  }
74
77
 
78
+ status() {
79
+ return {
80
+ ready: this.ready,
81
+ closed: this.closed,
82
+ network_route: this.networkRoute,
83
+ reconnect_attempt: this.reconnectAttempt,
84
+ outage_active: this.outageStartedAt > 0,
85
+ };
86
+ }
87
+
75
88
  start() {
76
89
  if (!this.closed && this.connectedOnce) return this.connectedOnce;
77
90
  this.closed = false;
@@ -182,11 +195,20 @@ export class RelayConnection {
182
195
  this.logger.debug?.("connecting to remote relay", { endpoint: redactUrl(wsUrl), attempt: this.reconnectAttempt + 1 });
183
196
  let socket;
184
197
  try {
198
+ const proxy = this.proxyAgentForUrl(wsUrl);
199
+ this.networkRoute = proxy?.agent ? "proxy" : "direct";
185
200
  socket = new this.WebSocketClass(wsUrl, {
186
201
  headers: { "X-Bridge-Token": this.secret },
187
202
  maxPayload: this.maxPayload,
203
+ ...(proxy?.agent ? { agent: proxy.agent } : {}),
188
204
  });
205
+ this.logger.debug?.("remote relay network route selected", { route: this.networkRoute });
189
206
  } catch (error) {
207
+ if (error?.code === "relay_proxy_configuration") {
208
+ this.networkRoute = "invalid-proxy-configuration";
209
+ this.failPermanently("relay_proxy_configuration");
210
+ return;
211
+ }
190
212
  this.lastTransportErrorClass = classifyOperationalError(error);
191
213
  this.logger.debug?.("remote relay connection could not be created", { error_class: this.lastTransportErrorClass });
192
214
  this.scheduleReconnect("connection_interrupted");
@@ -475,6 +497,9 @@ function relayFatalMessage(category) {
475
497
  if (category === "relay_protocol_error") {
476
498
  return "remote relay protocol error; upgrade and redeploy both components, then restart the daemon";
477
499
  }
500
+ if (category === "relay_proxy_configuration") {
501
+ return "remote relay proxy configuration is invalid; check HTTP_PROXY, HTTPS_PROXY, and NO_PROXY";
502
+ }
478
503
  return "remote relay rejected the daemon connection; verify credentials or redeploy the Worker";
479
504
  }
480
505
 
@@ -491,6 +516,7 @@ function relayCloseUserCause(category) {
491
516
  relay_heartbeat_timeout: "relay stopped responding",
492
517
  relay_transport_error: "relay transport error",
493
518
  relay_protocol_error: "relay protocol error",
519
+ relay_proxy_configuration: "relay proxy configuration invalid",
494
520
  invalid_transport_payload: "invalid transport payload",
495
521
  message_too_large: "message exceeded the relay limit",
496
522
  normal_close: "connection closed",