Files
reverse-engineering-anything/tests/boundary/process/opencodeClientVerifier.test.ts
T
GamerDubz 104c1c189e test(clients): verify native OpenCode integration against OpenCode V2 (#10178)
* test(clients): verify native OpenCode integration against OpenCode V2

The OpenCode client lane targeted OpenCode V1 (opencode-ai@1.18.35) and
could not pass against OpenCode V2. The V2 skill tool names its argument
`id`, so the lane's `{ name: ... }` call was rejected with "id: Missing
key" and the personal skill never loaded.

Bring the lane to OpenCode V2:

- Call the skill tool with `id` under V2 and keep `name` for V1.
- Register REA in the native `mcp.servers` table and read additional skill
  sources from the `skills` array; grant tool access with a `permissions`
  ruleset. The V1 dialect remains selectable with
  `REA_VERIFY_OPENCODE_DIALECT=v1`.
- OpenCode V2 groups MCP tools behind its Code Mode tool by default, so the
  lane sets the documented `codemode: false` server option to observe the
  forwarded catalog as direct function tools. REA's refresh preserves that
  client-owned option, which the lane now covers.
- Start a private server under V2. The shared background service keeps the
  environment it started with and would read the caller's own configuration
  instead of the lane's isolated one.
- Isolate the home for both platforms. The lane set only USERPROFILE, so a
  POSIX run resolved the caller's real home for the shared skill directory.
- Take the expected catalog from the runtime under test, so
  `REA_VERIFY_RUNTIME_ROOT` can point at an installed package whose catalog
  differs from this checkout.
- Record the dialect, plugin-isolation mechanism and tool surface in the
  receipt. OpenCode V2 dropped OPENCODE_PURE, so the V1 isolation claim no
  longer described what ran.

The lane no longer refuses Windows, so the OpenCode integration can be
verified on a native Windows host.

* fix(clients): isolate OpenCode verifier config and coverage claims

Clear inherited OPENCODE_CONFIG and OPENCODE_CONFIG_CONTENT so the lab
cannot load the caller's configuration layers. Record V2 as verified
only on native Windows and leave POSIX V2 explicitly unverified.

* fix(clients): pin OpenCode verifier to a full REA catalog

Clear inherited REA_MCP_TOOL_FAMILIES and set REA_MCP_TOOL_CATALOG_PROFILE=full so setup and the MCP child always advertise the complete catalog the fixture asserts.

* fix(ci): pin Native MCP OpenCode checks to @opencode/cli@2.2.7

The verifier defaults to the OpenCode V2 dialect, but the Native MCP
registration workflow still installed opencode-ai@1.19.6 without selecting
v1. Point the matrix at the published V2 CLI that supports the V2 config
and --standalone.

* fix(clients): harden OpenCode V2 verifier modes and launch seeds

Keep Code Mode enabled for scoped-chat and assert the paginated catalog
search, clear inherited REA MCP capacity/schema settings that can reject
fixture calls, and accept Aube-managed shim launchers alongside the
raw Node/script/mcp shape.

* fix(clients): only require Code Mode catalog search for OpenCode V2

* fix(clients): clear OpenCode verifier ambient budget and Windows env aliases

Unset inherited REA_AGENT_RESULT_BUDGET_BYTES so the fixture keeps complete
Evidence, and delete cleared OpenCode/REA launch seeds case-insensitively
before spawn so mixed-case Windows aliases cannot survive.

* fix(clients): require Windows packaged OpenCode runtime and isolate V2 ancestors

Document that Windows call verification needs REA_VERIFY_RUNTIME_ROOT with a
matching packaged runtime, and refuse OpenCode V2 lab runs when an ancestor
project configuration file would join the private server.

* fix(clients): qualify POSIX OpenCode V2 coverage and dialect Windows hint

Limit checked-in POSIX V2 claims to scoped-chat catalog search and point
Windows preflight recovery at the selected OpenCode package major.

* fix(clients): match OpenCode runtime contracts and clear registration seeds

Reject a REA_VERIFY_RUNTIME_ROOT whose package version or tool catalog
differs from this checkout, and clear every selected registration setting
from SetupRegistrationEnvironment instead of a hand-maintained MCP list.

* fix(verification): bind OpenCode fixtures to selected runtime

* fix(verification): refuse discovered OpenCode plugin sources

* test(verification): reject plugins before starting OpenCode

* test(ci): exercise packaged OpenCode on Windows

* fix(verification): type the selected-runtime fixture reader

* chore(ci): use pinned npm for Windows client proof

* docs(verification): distinguish Windows runner isolation

* test(verification): keep compiled fixtures in the built test lane

* test(verification): type catalog fixtures without generated output

* fix(ci): classify automated OpenCode verification
2026-12-02 13:39:47 +08:00

211 lines
7.1 KiB
TypeScript

import { mkdir, unlink, writeFile } from "node:fs/promises";
import { spawnSync } from "node:child_process";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { expect, it } from "vitest";
import {
assertIsolatedOpenCodeSources,
openCodeVerificationEnvironment,
} from "../../../scripts/verify/clients/opencode-environment.mjs";
import { SELECTED_REGISTRATION_SETTINGS } from "../../../src/application/SetupRegistrationEnvironment.js";
import { AGENT_RESULT_BUDGET_ENVIRONMENT } from "../../../src/domain/agentResultBudget.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const contextFor = (root: string) => ({
home: join(root, "account"),
env: {},
platform: process.platform,
systemPaths: {
openCodeSystemDirectory: join(root, "system"),
openCodeManagedPreferencesDirectory: join(root, "managed"),
},
});
it.each(["v1", "v2"])(
"isolates %s without changing its inherited environment",
async (dialect) => {
const lab = await createTestTempDirectory("rea-opencode-environment-");
const account = join(lab, "account");
const profile = join(lab, "selected");
const poisoned = [
"OPENCODE_CONFIG",
"OPENCODE_CONFIG_CONTENT",
"OPENCODE_CONFIG_DIR",
AGENT_RESULT_BUDGET_ENVIRONMENT,
...SELECTED_REGISTRATION_SETTINGS,
];
const inherited = Object.fromEntries(
poisoned.flatMap((key) => [
[key, "poison"],
[
key.toLowerCase().replace(/^./u, (letter) => letter.toUpperCase()),
"poison",
],
]),
);
inherited.PATH = "fixture-path";
inherited.HOME = "caller-home";
inherited.KEEP_ME = "preserved";
const before = { ...inherited };
const environment = openCodeVerificationEnvironment({
inherited,
account,
profile,
lab,
dialect,
runId: "fixture-run",
});
expect(inherited).toEqual(before);
expect(Object.values(environment)).not.toContain("poison");
expect(environment).toMatchObject({
HOME: account,
USERPROFILE: account,
PATH: "fixture-path",
KEEP_ME: "preserved",
OPENCODE_CONFIG_DIR: profile,
REA_MCP_TOOL_CATALOG_PROFILE: "full",
XDG_CONFIG_HOME: join(lab, "config"),
XDG_CACHE_HOME: join(lab, "cache"),
XDG_DATA_HOME: join(lab, "data"),
XDG_STATE_HOME: join(lab, "state"),
APPDATA: join(lab, "appdata", "roaming"),
LOCALAPPDATA: join(lab, "appdata", "local"),
REA_PROCESS_RUN_ID: "fixture-run",
});
expect(environment.OPENCODE_PURE).toBe(
dialect === "v1" ? "true" : undefined,
);
},
);
it.each([
"opencode.json",
"opencode.jsonc",
".opencode/opencode.json",
".opencode/opencode.jsonc",
])("refuses the ancestor layer %s without changing it", async (name) => {
const root = await createTestTempDirectory("rea-opencode-ancestor-");
const workspace = join(root, "workspace");
await mkdir(workspace);
await expect(
assertIsolatedOpenCodeSources(workspace, "v2", contextFor(root)),
).resolves.toBeUndefined();
await mkdir(join(root, ".opencode"), { recursive: true });
const path = join(root, name);
await writeFile(path, "{}");
await expect(
assertIsolatedOpenCodeSources(workspace, "v2", contextFor(root)),
).rejects.toThrow(path);
await expect(
assertIsolatedOpenCodeSources(workspace, "v1", contextFor(root)),
).resolves.toBeUndefined();
await unlink(path);
await expect(
assertIsolatedOpenCodeSources(workspace, "v2", contextFor(root)),
).resolves.toBeUndefined();
});
it("preserves filesystem failures instead of treating them as missing layers", async () => {
const root = await createTestTempDirectory("rea-opencode-ancestor-error-");
const file = join(root, "file");
await writeFile(file, "not a directory");
await expect(
assertIsolatedOpenCodeSources(
join(file, "workspace"),
"v2",
contextFor(root),
),
).rejects.toHaveProperty("code", "ENOTDIR");
});
it.each(["plugin", "plugins"])(
"refuses an ancestor %s directory without a config file",
async (name) => {
const root = await createTestTempDirectory("rea-opencode-plugin-");
const workspace = join(root, "workspace");
await mkdir(workspace);
const pluginDirectory = join(root, ".opencode", name);
await mkdir(pluginDirectory, { recursive: true });
await writeFile(
join(pluginDirectory, "example.ts"),
"export default {};\n",
);
await expect(
assertIsolatedOpenCodeSources(workspace, "v2", contextFor(root)),
).rejects.toThrow(pluginDirectory);
},
);
it.each(["opencode.json", "plugins/example.js"])(
"refuses a system source at %s",
async (name) => {
const root = await createTestTempDirectory("rea-opencode-system-");
const workspace = join(root, "workspace");
await mkdir(workspace);
const context = contextFor(root);
const path = join(context.systemPaths.openCodeSystemDirectory, name);
await mkdir(join(context.systemPaths.openCodeSystemDirectory, "plugins"), {
recursive: true,
});
await writeFile(path, name.endsWith(".js") ? "export default {};\n" : "{}");
await expect(
assertIsolatedOpenCodeSources(workspace, "v2", context),
).rejects.toThrow(
name.endsWith(".js")
? join(context.systemPaths.openCodeSystemDirectory, "plugins")
: path,
);
},
);
it.skipIf(process.platform !== "darwin")(
"refuses machine-managed OpenCode preferences",
async () => {
const root = await createTestTempDirectory("rea-opencode-managed-");
const workspace = join(root, "workspace");
await mkdir(workspace);
const context = contextFor(root);
const path = join(
context.systemPaths.openCodeManagedPreferencesDirectory,
"ai.opencode.managed.plist",
);
await mkdir(context.systemPaths.openCodeManagedPreferencesDirectory);
await writeFile(path, "managed preferences");
await expect(
assertIsolatedOpenCodeSources(workspace, "v2", context),
).rejects.toThrow(path);
},
);
it("refuses discovered plugins before invoking the selected client", async () => {
const root = await createTestTempDirectory("rea-opencode-preflight-");
const pluginDirectory = join(root, ".opencode", "plugins");
await mkdir(pluginDirectory, { recursive: true });
await writeFile(join(pluginDirectory, "example.ts"), "export default {};\n");
const script = fileURLToPath(
new URL("../../../scripts/verify-opencode-client.mjs", import.meta.url),
);
const repo = fileURLToPath(new URL("../../../", import.meta.url));
const result = spawnSync(process.execPath, [script, "scoped-chat"], {
encoding: "utf8",
timeout: 15_000,
env: {
...process.env,
TMPDIR: root,
TMP: root,
TEMP: root,
REA_VERIFY_RUNTIME_ROOT: repo,
REA_VERIFY_OPENCODE_DIALECT: "v2",
REA_VERIFY_OPENCODE_COMMAND: join(root, "client-must-not-start"),
},
});
expect(result.error).toBeUndefined();
expect(result.status).not.toBe(0);
expect(result.stderr).toContain(
"uncontrolled configuration or plugin source",
);
expect(result.stderr).not.toContain("OpenCode preflight failed");
});