Files
msd-core/tests/vscode-browser-no-node-api.test.cjs
Tom Boucher a0fafedfa0 feat(#2103): drive VS Code through the Embeddable Orchestration System (ADR-1239)
VS Code is a net-new EoS runtime that — unlike every prior migration — is NOT
CLI-installed (Marketplace/VSIX extension). It has zero runtime==='vscode'
branches in bin/install.js and stays that way (regression-guarded); it is driven
entirely through the negotiated imperative Host-Integration adapter.

Registry + validator (the hard part):
- capabilities/vscode/capability.json (role:runtime): full hostIntegration block
  (imperative / palette / active vscode.lm model / engine hook bus /
  sandboxed-storage / mcp transport / sandboxed-web runtime; dispatch nested,
  maxDepth 5 per VS Code's documented subagent depth).
- capability-validator.cjs extended so a role:runtime capability can legitimately
  declare "extension-distributed, no config directory": new configHome.kind:'none'
  + installSurface:'none' (+ GATE-A pairing + the parity maps), with localConfigDir
  and configHome.name made conditional on kind!=='none'. All 18 runtimes still
  validate; getDirName returns a distinct sentinel (not '.claude') for a no-config
  runtime.
- The add-a-registry-runtime tax: NON_INSTALLABLE_RUNTIMES exemption in the
  runtime-flags drift guard, vscode added to global-config-home SPECIAL_CASED,
  EXPECTED_PROFILES.vscode='ide', and the config-adapter/derivation/pin-count
  guards updated. No golden-install fixture, model-catalog, or CONFIGURATION rows
  (vscode never enters allRuntimes).

Dispatch + extension surface:
- Fixed vscode/extension.js's createHub()-no-args bug (every dispatch was
  UnknownCommand, masked by a vacuous reachability test) — now reuses the shared
  dispatchGsdCommand subprocess-shim (Node/desktop); the reachability test is
  tightened to assert real dispatch.
- Promoted the #1933 host binding to a shipped vscode/host-binding.js; activate()
  now composes the model/hookBus/stateIO seams through it. Corrected the model
  seam to VS Code's real API (vscode.lm.selectChatModels() -> model.sendRequest();
  vscode.lm.sendRequest does not exist) so the binding actually composes on real
  desktop VS Code instead of throwing.
- New vscode/browser.js Web Extension entry with ZERO Node APIs (the engine's
  config/capability loading is Node-bound, so the web entry registers the surface
  and directs full dispatch to the native MCP server — honestly documented).
- UPGRADE 1: GSD skills as native Language Model Tools (contributes.languageModelTools
  + vscode.lm.registerTool), invoke() dispatching through the hub.
- UPGRADE 2: native subagent dispatch wired onto #runSubagent /
  chat.subagents.allowInvocationsFromSubagents (fail-soft on API availability,
  maxDepth 5 enforced).
- vscode/package.json: browser entry, engines.vscode ^1.105, chatParticipants +
  languageModelTools contributions; fixed a stale activationPoints->activationEvents
  manifest key. Added "vscode" to the package files array.

Docs (## vscode matrix section) + changeset (Added).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 23:24:42 -04:00

129 lines
5.3 KiB
JavaScript

// allow-test-rule: source-text-is-the-product (#2103). browser.js's entire
// contract IS "zero Node APIs" — a VS Code Web/webworker host has no Node
// core modules at all, so this is a runtime-contract source check, not a
// behavioral proxy for something observable another way (there is no Node
// runtime to observe failing against in CI). Mirrors the existing
// phase6-capstone-conformance.test.cjs precedent for the same exemption class.
'use strict';
/**
* VS Code browser (Web Extension) entry static guard — #2103.
*
* `vscode/browser.js` is the `browser` field entry VS Code Web / vscode.dev
* loads in a webworker context, which has NO Node core modules at all
* (`fs`, `path`, `child_process`) and no Node globals (`process`, `Buffer`,
* `__dirname`, `__filename`). Requiring one throws immediately at load time.
*
* This is the reason browser.js does NOT require `./host-binding.js` (whose
* transitive engine-lib dependencies pull in `node:fs` — see host-binding.js's
* and browser.js's own header comments for the full chain) — it implements an
* independent, minimal composition directly against `vscode.lm`.
*
* No real browser or VS Code host is available in CI (mock vscode only, per
* every other vscode-*.test.cjs in this suite).
*/
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const BROWSER_PATH = path.join(__dirname, '..', 'vscode', 'browser.js');
test('vscode/browser.js source contains ZERO require("fs" | "path" | "child_process") (or "node:" prefixed forms)', () => {
const src = fs.readFileSync(BROWSER_PATH, 'utf8');
const bannedRequires = [
/require\(\s*['"]fs['"]\s*\)/,
/require\(\s*['"]node:fs['"]\s*\)/,
/require\(\s*['"]path['"]\s*\)/,
/require\(\s*['"]node:path['"]\s*\)/,
/require\(\s*['"]child_process['"]\s*\)/,
/require\(\s*['"]node:child_process['"]\s*\)/,
];
const offenders = [];
for (const line of src.split(/\r?\n/)) {
// Skip comment lines (this file's own header documents the constraint in
// prose, which legitimately contains the string `require('fs')` etc.).
const trimmed = line.trim();
if (trimmed.startsWith('*') || trimmed.startsWith('//')) continue;
for (const re of bannedRequires) {
if (re.test(line)) offenders.push(line.trim());
}
}
assert.deepEqual(offenders, [],
`vscode/browser.js must never require a Node core module outside a comment; found: ${JSON.stringify(offenders)}`);
});
test('vscode/browser.js does NOT require ./host-binding.js or ./extension.js (both pull in fs-heavy engine-lib modules transitively)', () => {
const src = fs.readFileSync(BROWSER_PATH, 'utf8');
const codeLines = src.split(/\r?\n/).filter((l) => {
const t = l.trim();
return !(t.startsWith('*') || t.startsWith('//'));
}).join('\n');
assert.doesNotMatch(codeLines, /require\(\s*['"]\.\/(host-binding|extension)\.js['"]\s*\)/,
'browser.js must compose its own zero-Node-API surface, not require a module with fs-pulling transitive deps');
});
test('vscode/browser.js source contains no Node-only globals (process/Buffer/__dirname/__filename) outside comments', () => {
const src = fs.readFileSync(BROWSER_PATH, 'utf8');
const offenders = [];
for (const line of src.split(/\r?\n/)) {
const trimmed = line.trim();
if (trimmed.startsWith('*') || trimmed.startsWith('//')) continue;
if (/\bprocess\.|\bBuffer\.|__dirname\b|__filename\b/.test(line)) offenders.push(line.trim());
}
assert.deepEqual(offenders, []);
});
test('vscode/browser.js is valid, loadable JS (node --check equivalent — require does not throw)', () => {
assert.doesNotThrow(() => require(BROWSER_PATH));
});
test('REACHABILITY: browser.js activate() runs against a mock vscode host without throwing (no real VS Code/browser in CI)', () => {
const Module = require('module');
const originalLoad = Module._load;
const registeredCommands = {};
let chatCreated = false;
const toolNames = [];
const mockVscode = {
commands: {
registerCommand(id, handler) {
registeredCommands[id] = handler;
return { dispose() {} };
},
},
chat: {
createChatParticipant(id, handler) {
chatCreated = true;
return { id, handler, dispose() {} };
},
},
lm: {
registerTool(name) {
toolNames.push(name);
return { dispose() {} };
},
},
workspace: { getConfiguration: () => ({ get: () => undefined }) },
LanguageModelTextPart: class { constructor(t) { this.text = t; } },
LanguageModelToolResult: class { constructor(p) { this.parts = p; } },
};
Module._load = function (request, ...rest) {
if (request === 'vscode') return mockVscode;
return originalLoad.call(this, request, ...rest);
};
try {
delete require.cache[BROWSER_PATH];
const browser = require(BROWSER_PATH);
const context = { subscriptions: [] };
assert.doesNotThrow(() => browser.activate(context));
assert.ok(registeredCommands['gsd.invoke'], 'gsd.invoke command registered on web too');
assert.ok(chatCreated, 'chat participant registered on web');
assert.ok(toolNames.length > 0, 'LM tools registered on web');
assert.ok(context.subscriptions.length > 0);
} finally {
Module._load = originalLoad;
delete require.cache[BROWSER_PATH];
}
});