@vulcan-energy/mcp-helper 0.2.4 → 0.2.5

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.
@@ -8,8 +8,10 @@ import { fileURLToPath } from 'node:url';
8
8
 
9
9
  const hash = bytes => createHash('sha256').update(bytes).digest('hex');
10
10
  const RELEASE_ORIGIN = 'https://usevulcan.app';
11
- // Source archives and notices stay in the published release; execution needs only model artifacts.
12
- const isModelArtifact = path => !path.startsWith('source/') && !path.startsWith('notices/');
11
+ // As targets.ts:modelArtifacts: not source archives or notices, and not a pinned converter
12
+ // that is not also the runtime. The host converts with the package's own WASM.
13
+ const isModelArtifact = (manifest, path) => !path.startsWith('source/') && !path.startsWith('notices/')
14
+ && !([manifest.preparation?.javascript, manifest.preparation?.wasm].includes(path) && ![manifest.runtime?.javascript, manifest.runtime?.wasm].includes(path));
13
15
  function assertArtifactPath(path) {
14
16
  if (!path || path.includes('\\') || path.startsWith('/') || path.split('/').some(part => !part || part === '..' || part === '.')) throw new Error('Invalid target artifact path');
15
17
  }
@@ -23,7 +25,7 @@ export async function readVerifiedBundle(manifestPath, expectedHash) {
23
25
  const routes = new Map();
24
26
  for (const artifact of manifest.artifacts) {
25
27
  assertArtifactPath(artifact.path);
26
- if (!isModelArtifact(artifact.path)) continue;
28
+ if (!isModelArtifact(manifest, artifact.path)) continue;
27
29
  const file = await realpath(resolve(root, artifact.path));
28
30
  if (!file.startsWith(root + sep)) throw new Error('Target artifact escapes bundle directory');
29
31
  const contents = await readFile(file);
@@ -33,6 +35,27 @@ export async function readVerifiedBundle(manifestPath, expectedHash) {
33
35
  return { manifest, routes };
34
36
  }
35
37
 
38
+ /** The package's own WASM, served at /app/: the glue, the binary, the version file, and the glue's relative imports. */
39
+ export async function readPackageWasm(wasmDir) {
40
+ if (!wasmDir) throw new Error('No WASM directory given: target preparation converts with the package\'s own WASM.');
41
+ const read = name => readFile(join(wasmDir, name)).catch(() => {
42
+ throw new Error(`The package WASM is incomplete: ${join(wasmDir, name)} is missing. Reinstall the package, or pass a complete WASM directory.`);
43
+ });
44
+ const [version, glue, binary] = await Promise.all(['wasm_version.json', 'wasm_wrapper.js', 'wasm_wrapper_bg.wasm'].map(read));
45
+ const info = JSON.parse(version);
46
+ // The glue and binary are checked against wasm_version.json. The wasm-bindgen-rayon snippets/ file the Free glue
47
+ // imports is served unhashed: the version file has no hash for it. This catches a mismatched build; it is not a security boundary.
48
+ for (const key of ['js_sha256', 'sha256']) if (!info[key]) throw new Error(`${join(wasmDir, 'wasm_version.json')} has no ${key}`);
49
+ if (hash(glue) !== info.js_sha256 || hash(binary) !== info.sha256) throw new Error(`The WASM in ${wasmDir} does not match its wasm_version.json`);
50
+ const routes = new Map([['/app/wasm_version.json', version], ['/app/wasm_wrapper.js', glue], ['/app/wasm_wrapper_bg.wasm', binary]]);
51
+ // The same import scan deploy-mcp.sh uses to copy these files into the package.
52
+ for (const [, path] of glue.toString().matchAll(/(?:from |import\()['"]\.\/([^'"]+)/g)) {
53
+ assertArtifactPath(path);
54
+ routes.set(`/app/${path}`, await read(path));
55
+ }
56
+ return routes;
57
+ }
58
+
36
59
  async function releasePin(bundleId, pins) {
37
60
  if (!pins) {
38
61
  try { pins = (await import('./release-manifests.mjs')).RELEASE_MANIFEST_HASHES; }
@@ -79,7 +102,7 @@ export async function resolveReleaseBundle(bundleId, { cacheDir = join(homedir()
79
102
  await writeAtomic(manifestPath, bytes);
80
103
  }
81
104
  const manifest = JSON.parse(bytes);
82
- const artifacts = manifest.artifacts.filter(artifact => { assertArtifactPath(artifact.path); return isModelArtifact(artifact.path); });
105
+ const artifacts = manifest.artifacts.filter(artifact => { assertArtifactPath(artifact.path); return isModelArtifact(manifest, artifact.path); });
83
106
  const missing = [];
84
107
  const realRoot = await realpath(root);
85
108
  for (const artifact of artifacts) {
@@ -112,13 +135,19 @@ export async function resolveReleaseBundle(bundleId, { cacheDir = join(homedir()
112
135
  return { manifestPath, manifestSha256: expected };
113
136
  }
114
137
 
138
+ /** The pinned runtime's panic hook writes `[WASM] PANIC…` lines to the console of its worker; the
139
+ * worker itself reports only the trap. Pass those lines to stderr so the CLI shows the engine's panic. */
140
+ export function forwardPanicLines(page, write = line => process.stderr.write(`${line}\n`)) {
141
+ page.on('console', message => { if (message.text().startsWith('[WASM] PANIC')) write(message.text()); });
142
+ }
143
+
115
144
  export async function runTargetBrowser(options, dependencies = {}) {
116
145
  // A bundle id resolves only after the workspace lock, so a long first download is reported as batch progress.
117
146
  const bundleId = options.manifestPath ? undefined : options.targetBundleId;
118
147
  let manifest, routes;
119
148
  if (options.manifestPath) ({ manifest, routes } = await readVerifiedBundle(options.manifestPath, options.manifestSha256));
120
149
  else await releasePin(bundleId, dependencies.pins);
121
- const assets = new Map();
150
+ const assets = await readPackageWasm(options.wasmDir);
122
151
  const runtimeRoot = options.runtimeRoot ?? dirname(fileURLToPath(import.meta.url));
123
152
  for (const name of ['host.mjs', 'python-worker.mjs', 'rust-worker.mjs']) {
124
153
  try { assets.set('/' + name, await readFile(join(runtimeRoot, name))); }
@@ -220,6 +249,7 @@ export async function runTargetBrowser(options, dependencies = {}) {
220
249
  });
221
250
  if (interrupted) throw interrupted;
222
251
  const page = await browser.newPage();
252
+ forwardPanicLines(page);
223
253
  await page.route('**/*', route => route.request().url().startsWith(origin + '/') ? route.continue() : route.abort());
224
254
  await page.exposeFunction('targetCliRead', read);
225
255
  await page.exposeFunction('targetCliWrite', write);
@@ -232,7 +262,7 @@ export async function runTargetBrowser(options, dependencies = {}) {
232
262
  const { run } = await import('/host.mjs');
233
263
  return { result: await run(request) };
234
264
  } catch (error) { return { error: error instanceof Error ? error.message : String(error) }; }
235
- }, { manifest, operation: options.operation, csv: options.csv, config: options.config, configName: options.configName, jobId, startedAt });
265
+ }, { manifest, operation: options.operation, csv: options.csv, config: options.config, configName: options.configName, jobId, startedAt, wasmRemedy: options.wasmRemedy });
236
266
  if (interrupted) throw interrupted;
237
267
  if ('error' in outcome) throw new Error(outcome.error);
238
268
  const { result } = outcome;
@@ -0,0 +1,126 @@
1
+ // ../community/packages/geometry-document/src/geometryCsvSections.ts
2
+ function parseGeometryCsvLine(line) {
3
+ const fields = [];
4
+ let current = "";
5
+ let inQuotes = false;
6
+ let index = 0;
7
+ while (index < line.length) {
8
+ const character = line[index];
9
+ if (inQuotes) {
10
+ if (character === '"') {
11
+ if (index + 1 < line.length && line[index + 1] === '"') {
12
+ current += '"';
13
+ index += 2;
14
+ } else {
15
+ inQuotes = false;
16
+ index += 1;
17
+ }
18
+ } else {
19
+ current += character;
20
+ index += 1;
21
+ }
22
+ } else if (character === '"') {
23
+ inQuotes = true;
24
+ index += 1;
25
+ } else if (character === ",") {
26
+ fields.push(current);
27
+ current = "";
28
+ index += 1;
29
+ } else {
30
+ current += character;
31
+ index += 1;
32
+ }
33
+ }
34
+ fields.push(current);
35
+ return fields;
36
+ }
37
+
38
+ // ../community/packages/geometry-editor/src/geometry/io/csvSectionRows.ts
39
+ var parseCsvLine = parseGeometryCsvLine;
40
+ function csvMetadataValue(csv, key) {
41
+ const rows = csv.replace(/^\uFEFF/, "").split(/\r?\n/).map(parseCsvLine);
42
+ const end = rows.findIndex((fields) => fields.every((field) => !field.trim()));
43
+ const values = new Set(rows.slice(0, end === -1 ? void 0 : end).filter((fields) => fields[0]?.replace(/^#\s*/, "").trim() === key).map((fields) => fields[1]?.trim() ?? ""));
44
+ if (values.size > 1) throw new Error(`CSV contains conflicting ${key} metadata`);
45
+ return values.values().next().value;
46
+ }
47
+
48
+ // ../community/packages/geometry-editor/src/lib/workspacePaths.ts
49
+ var DEFAULT_DEFAULTS_PATH = "input/defaults/defaults_template.json";
50
+ var LEGACY_DEFAULT_DEFAULTS_PATH = "input/batch_parameters/base_json/defaults_template.json";
51
+ function isLegacyDefaultsTemplatePath(p) {
52
+ const n = p.trim().replace(/\\/g, "/");
53
+ return n === LEGACY_DEFAULT_DEFAULTS_PATH || n.endsWith("/batch_parameters/base_json/defaults_template.json");
54
+ }
55
+ var LEGACY_SUFFIX = "batch_parameters/base_json/defaults_template.json";
56
+ var CANONICAL_SUFFIX = "defaults/defaults_template.json";
57
+ function siblingCanonicalDefaultsPathForRead(legacyPath) {
58
+ const n = legacyPath.trim().replace(/\\/g, "/");
59
+ const idx = n.lastIndexOf(LEGACY_SUFFIX);
60
+ if (idx >= 0) {
61
+ return `${n.slice(0, idx)}${CANONICAL_SUFFIX}`;
62
+ }
63
+ return DEFAULT_DEFAULTS_PATH;
64
+ }
65
+ function defaultsReadPathAttempts(workspaceRelativePath) {
66
+ const t = workspaceRelativePath.trim();
67
+ if (!t) return [];
68
+ if (isLegacyDefaultsTemplatePath(t)) {
69
+ return [siblingCanonicalDefaultsPathForRead(t), t];
70
+ }
71
+ return [t];
72
+ }
73
+
74
+ // ../hem-target-runtime/src/hem/targets.ts
75
+ var HEM_TARGETS = [
76
+ {
77
+ id: "rust-fhs-a7-62d3df70-c5ba2673-v1",
78
+ label: "Rust HEM:FHS 1.0.0a7",
79
+ runtime: "rust",
80
+ version: "1.0.0a7",
81
+ engineVersion: "1.0.0a7",
82
+ wrapperVersion: "1.0.0a7",
83
+ wrapper: "fhs",
84
+ engineCommit: "62d3df705690f33b3fc3e905c9971d4f3743bf2e",
85
+ wrapperCommit: "c5ba2673fbd886cfe4fb528f61b376bdf406ebbd",
86
+ conversionProfile: "current_rust_fhs"
87
+ },
88
+ {
89
+ id: "python-fhs-a9-918addad-b9b90138-v1",
90
+ label: "Python HEM:FHS 1.0.0a9",
91
+ runtime: "python",
92
+ version: "1.0.0a9",
93
+ engineVersion: "1.0.0a9",
94
+ wrapperVersion: "1.0.0a9",
95
+ wrapper: "fhs",
96
+ engineCommit: "918addad2f9ffbcd9c7a4ed73497d047210be2d2",
97
+ wrapperCommit: "b9b901382feb0e5d27f969532842250147fb4068",
98
+ conversionProfile: "python_fhs_a9"
99
+ },
100
+ {
101
+ id: "python-fhs-a8-f2ab6cf7-8ca182b0-v1",
102
+ label: "Python HEM:FHS 1.0.0a8",
103
+ runtime: "python",
104
+ version: "1.0.0a8",
105
+ engineVersion: "1.0.0a8",
106
+ wrapperVersion: "1.0.0a8",
107
+ wrapper: "fhs",
108
+ engineCommit: "f2ab6cf78860c36d260cb21396b747cc372f2c9d",
109
+ wrapperCommit: "8ca182b0171a3a928059a9c0ec0145ecefe3f4e9",
110
+ conversionProfile: "python_fhs_a8"
111
+ }
112
+ ];
113
+ var RECOMMENDED_HEM_TARGET = HEM_TARGETS[0].id;
114
+ var UNPINNED_CSV_TARGET = "rust-fhs-a7-62d3df70-c5ba2673-v1";
115
+
116
+ // ../hem-target-runtime/src/hem/csvTarget.ts
117
+ function csvTarget(csv) {
118
+ return csvMetadataValue(csv, "TargetBundleId") || UNPINNED_CSV_TARGET;
119
+ }
120
+ function csvDefaultsReadPaths(csv) {
121
+ return defaultsReadPathAttempts(csvMetadataValue(csv, "DefaultsPath") ?? "");
122
+ }
123
+ export {
124
+ csvDefaultsReadPaths,
125
+ csvTarget
126
+ };