import assert from "node:assert/strict"; import { access } from "node:fs/promises"; import { basename, dirname, join } from "node:path"; import { SELECTED_REGISTRATION_SETTINGS } from "../../../dist/application/SetupRegistrationEnvironment.js"; import { clientProjectConfigurationPaths, openCodeManagedPreferencePaths, openCodeSystemConfigPaths, } from "../../../dist/application/SupportedClientLocations.js"; import { currentUser } from "../../../dist/application/OpenCodeManagedPreferences.js"; import { AGENT_RESULT_BUDGET_ENVIRONMENT } from "../../../dist/domain/agentResultBudget.js"; /** Redirect client state and remove inherited configuration and REA test seeds. */ export function openCodeVerificationEnvironment({ inherited, account, profile, lab, dialect, runId, }) { const environment = { ...inherited, HOME: account, USERPROFILE: account, 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"), OPENCODE_DISABLE_AUTOUPDATE: "true", OPENCODE_DISABLE_MODELS_FETCH: "true", ...(dialect === "v1" ? { OPENCODE_PURE: "true" } : {}), REA_PROCESS_RUN_ID: runId, }; // Windows also admits mixed-case spellings of these environment names. const clearedNames = new Set([ "opencode_config", "opencode_config_content", "opencode_config_dir", AGENT_RESULT_BUDGET_ENVIRONMENT.toLowerCase(), ...SELECTED_REGISTRATION_SETTINGS.map((name) => name.toLowerCase()), ]); for (const key of Object.keys(environment)) { if (clearedNames.has(key.toLowerCase())) delete environment[key]; } environment.OPENCODE_CONFIG_DIR = profile; environment.REA_MCP_TOOL_CATALOG_PROFILE = "full"; return environment; } /** Refuse foreign configuration and config-free executable plugin sources. */ export async function assertIsolatedOpenCodeSources( workspaceDirectory, dialect, context, ) { if (dialect !== "v2") return; const projectConfigs = clientProjectConfigurationPaths( { name: "opencode", format: "opencode" }, workspaceDirectory, context.platform, ); assert( projectConfigs.ok, "OpenCode V2 project configuration paths must resolve for the lab workspace", ); const systemConfigs = openCodeSystemConfigPaths(context); const pluginRoots = new Set([ ...projectConfigs.value .map(dirname) .filter((path) => basename(path) === ".opencode"), ...systemConfigs.map(dirname), ]); const sources = new Set([ ...projectConfigs.value, ...systemConfigs, ...openCodeManagedPreferencePaths(context, currentUser()), // OpenCode discovers these directories without an entry in a config file. ...[...pluginRoots].flatMap((root) => [ join(root, "plugin"), join(root, "plugins"), ]), ]); for (const path of sources) { await access(path).then( () => { assert.fail( `OpenCode V2 has an uncontrolled configuration or plugin source at ${path}; use an isolated verification environment before claiming lab isolation`, ); }, (cause) => { if (cause?.code !== "ENOENT") throw cause; }, ); } }