Files
rea/scripts/verify-windows-package.mjs
T
fd65e278be fix(setup): resolve the configuration MSIX Claude Desktop reads (#2426)
* fix(setup): resolve the configuration MSIX Claude Desktop reads

On Windows, setup, doctor and uninstall always used
%APPDATA%\Claude\claude_desktop_config.json. The MSIX build of Claude
Desktop (package family Claude_pzs8sxrjxfjjc) virtualizes AppData writes
and reads its package-private copy under
%LOCALAPPDATA%\Packages\<family>\LocalCache\Roaming\Claude once it exists.
Setup from an ordinary process therefore wrote a file the app never reads,
and doctor reported that file healthy.

Resolve the Claude Desktop location once, in supportedClients(): the
private copy when it exists, else the AppData file the package still
reads, else a new private copy. An unrecognized Claude_* publisher or
unreadable package storage is reported instead of falling back to AppData.

Compare one of the package's private files with its AppData path through
realpath.native to learn whether the running package started this
process. From that redirected view, or when no file can be compared,
setup and uninstall refuse to change Claude Desktop's configuration,
because the running app rewrites it from memory. From a redirected view
they also refuse writes under %APPDATA% and %LOCALAPPDATA% for other
clients and skills, which would land in the package's private storage.

Setup refuses to register a launcher stored in any MSIX package's
private storage, discloses which file the package reads, and tells the
user to quit Claude Desktop fully on every platform. Configuration fails
with target_changed if the package switched to its private copy after
the plan was shown. Uninstall removes REA from both files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(setup): record the verified MSIX Claude Desktop registration

On the Windows 11 host with the MSIX build 2.31226.1.0, setup from an
ordinary terminal with the app quit wrote the package-private copy. After
the app restarted, the entry persisted, doctor reported it aligned, and
Claude Desktop's MCP log showed REA starting and answering tools/list.
Uninstall against the real app remains unverified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style(setup): format MSIX review corrections

* fix(setup): carry the admitted legacy path through uninstall removal

clientRegistrationRemoval returned a removeLegacy boolean, and removeClient
recomputed the legacy path separately, so TypeScript could not prove the
path existed when removeLegacy was true and `tsc --noEmit` failed in
Uninstall.ts. The decision now returns the admitted legacy path itself;
preflight and removal consume that one value.

The alias regression matched new-destination children with a "/"
separator while building paths with path.join, so it failed on Windows.
It now uses path.sep.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(setup): split MSIX Claude Desktop lifecycle cases under the function length limit

The uninstall cases pushed the registration lifecycle describe past the 150-line lint limit. Group them separately, with the AppData registration case kept among the registration cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-10-13 22:04:22 +08:00

257 lines
7.5 KiB
JavaScript

#!/usr/bin/env node
import { execFile } from "node:child_process";
import {
access,
mkdir,
mkdtemp,
readFile,
rm,
writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { parse as parseJsonc } from "jsonc-parser";
import { supportedClients } from "../dist/application/SupportedClients.js";
import { promisify } from "node:util";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { verifyCompleteToolCatalog } from "./lib/verify-package-core.mjs";
import { verifyPackageUpdate } from "./verify-package-update.mjs";
import { completeVerifierRun, createVerifierRun } from "./lib/verifier-run.mjs";
const exec = promisify(execFile);
const verifierRun = createVerifierRun();
const root = process.cwd();
const workspace = await mkdtemp(join(tmpdir(), "rea-windows-package-"));
const prefix = join(workspace, "prefix");
let report;
try {
const packed = JSON.parse(
(
await npm(
["pack", "--json", "--silent", "--pack-destination", workspace],
root,
)
).stdout,
);
const packageResult = packed[0];
if (
packageResult === undefined ||
typeof packageResult.filename !== "string" ||
!Array.isArray(packageResult.files)
)
throw new Error(
`npm pack returned invalid metadata: ${JSON.stringify(packed)}`,
);
const packagedPaths = new Set(packageResult.files.map(({ path }) => path));
for (const required of [
"bridge/ghidra/ReaGhidraBridge.java",
"native/windows/build/manifest.json",
"native/windows/build/rea-windows-x64.node",
"dist/android/JadxWorkspaceRoot.js",
"dist/main.js",
"dist/cli.js",
"scripts/rea-android-workspace-guardian.mjs",
"scripts/rea.mjs",
])
if (!packagedPaths.has(required))
throw new Error(`Windows package omitted ${required}`);
const tarball = join(workspace, packageResult.filename);
await access(tarball);
await npm(
["install", "--no-package-lock", "--no-save", "--prefix", prefix, tarball],
workspace,
);
const entry = join(
prefix,
"node_modules",
"rea-agents",
"scripts",
"rea.mjs",
);
const environment = {
...process.env,
REA_ANALYSIS_PROVIDER: "auto",
};
const help = await exec(process.execPath, [entry, "--help"], {
env: environment,
windowsHide: true,
});
if (
!/^\s{2}inspect\s/mu.test(help.stdout) ||
!/^\s{2}decompile\s/mu.test(help.stdout) ||
!/^\s{2}providers\s/mu.test(help.stdout)
)
throw new Error("Packaged Windows CLI help omitted analysis commands");
const server = new StdioClientTransport({
command: process.execPath,
args: [entry, "mcp"],
env: environment,
stderr: "pipe",
});
const client = new Client({ name: "rea-windows-package", version: "1.0.0" });
let toolCount = 0;
try {
await client.connect(server);
toolCount = (await verifyCompleteToolCatalog(client)).length;
} finally {
await client.close();
}
const home = join(workspace, "home");
const setupEnvironment = {
...environment,
HOME: home,
USERPROFILE: home,
APPDATA: join(home, "AppData", "Roaming"),
LOCALAPPDATA: join(home, "AppData", "Local"),
CLAUDE_CONFIG_DIR: home,
CODEX_HOME: join(home, ".codex"),
XDG_CONFIG_HOME: join(home, ".config"),
OPENCODE_CONFIG: join(home, ".config", "opencode", "opencode.jsonc"),
COPILOT_HOME: join(home, ".copilot"),
GROK_HOME: join(home, ".grok"),
SAND_DATA_ROOT: join(home, ".grokbot"),
OMP_PROFILE: undefined,
PI_CODING_AGENT_DIR: undefined,
PI_CONFIG_DIR: undefined,
PI_PROFILE: undefined,
};
await mkdir(join(home, ".config", "opencode"), { recursive: true });
await writeFile(
setupEnvironment.OPENCODE_CONFIG,
'{\n // preserve user settings\n "theme": "dark",\n}\n',
);
const setupArgs = [
"setup",
"--yes",
"--client",
"opencode",
"--client",
"vscode",
"--client",
"copilot_cli",
"--skill=false",
"--json",
];
const setup = JSON.parse(
(
await exec(process.execPath, [entry, ...setupArgs], {
env: setupEnvironment,
windowsHide: true,
})
).stdout,
);
if (setup.status !== "ready" || setup.appliedActions.length !== 3)
throw new Error(
`Packaged agent-only setup failed: ${JSON.stringify(setup)}`,
);
const expectedCommand =
process.platform === "win32"
? [process.execPath, entry, "mcp"]
: [entry, "mcp"];
const clients = supportedClients(home, process.platform, setupEnvironment);
for (const id of ["opencode", "vscode", "copilot_cli"]) {
const clientLocation = clients.find(({ name }) => name === id);
if (clientLocation === undefined)
throw new Error(`Missing packaged client ${id}`);
const text = await readFile(clientLocation.configPath, "utf8");
const config = parseJsonc(text);
const registration =
id === "opencode"
? config.mcp?.rea
: id === "vscode"
? config.servers?.rea
: config.mcpServers?.rea;
const actualCommand =
id === "opencode"
? registration?.command
: [registration?.command, ...(registration?.args ?? [])];
if (JSON.stringify(actualCommand) !== JSON.stringify(expectedCommand))
throw new Error(
`Packaged ${id} has an unusable launcher: ${JSON.stringify(actualCommand)}`,
);
if (id === "opencode" && !text.includes("// preserve user settings"))
throw new Error("Packaged setup discarded OpenCode JSONC comments");
}
const repeated = JSON.parse(
(
await exec(process.execPath, [entry, ...setupArgs], {
env: setupEnvironment,
windowsHide: true,
})
).stdout,
);
if (
repeated.status !== "ready" ||
repeated.plannedActions.length !== 0 ||
repeated.appliedActions.length !== 0
)
throw new Error("Packaged agent setup is not idempotent");
const uninstalled = JSON.parse(
(
await exec(process.execPath, [entry, "uninstall", "--json"], {
env: setupEnvironment,
windowsHide: true,
})
).stdout,
);
for (const id of ["opencode", "vscode", "copilot_cli"])
if (
uninstalled.items?.find(({ name }) => name === id)?.status !== "removed"
)
throw new Error(`Packaged uninstall did not remove ${id}`);
if (process.platform !== "win32")
await npm(
["install", "--global", "--ignore-scripts", "--prefix", prefix, tarball],
workspace,
);
const update = await verifyPackageUpdate({
prefix,
packageRoot:
process.platform === "win32"
? join(prefix, "node_modules", "rea-agents")
: join(prefix, "lib", "node_modules", "rea-agents"),
tarball,
workspace,
environment,
});
report = {
ok: true,
platform: process.platform,
package: packageResult.filename,
tools: toolCount,
ghidra_bridge: "present",
agent_setup: ["opencode", "vscode", "copilot_cli"],
setup_idempotent: true,
uninstall: true,
update,
};
} finally {
await rm(workspace, { recursive: true, force: true });
}
process.stdout.write(
`${JSON.stringify({ verifier_run: await completeVerifierRun(verifierRun), ...report })}\n`,
);
function npm(arguments_, cwd) {
const npmExecPath = process.env.npm_execpath;
return npmExecPath === undefined
? exec(process.platform === "win32" ? "npm.cmd" : "npm", arguments_, {
cwd,
windowsHide: true,
})
: exec(process.execPath, [npmExecPath, ...arguments_], {
cwd,
windowsHide: true,
});
}