mirror of
https://github.com/morluto/rea.git
synced 2026-12-02 13:11:18 -05:00
126 lines
4.7 KiB
JavaScript
126 lines
4.7 KiB
JavaScript
import { runOwnedVerifierTerminal } from "./owned-verifier-terminal.mjs";
|
|
import {
|
|
captureVerifierFileSnapshot,
|
|
assertVerifierFilesUnchanged,
|
|
} from "./verifier-file-snapshots.mjs";
|
|
|
|
export const terminalModesRestored = (termios) => {
|
|
if (typeof termios !== "string") return false;
|
|
const modes = new Set(termios.trim().split(/\s+/u));
|
|
return modes.has("icanon") && modes.has("echo");
|
|
};
|
|
|
|
async function runSetupTerminal(
|
|
{ commands, command, environment, root },
|
|
{ wrapper, marker, timeoutMs, timeoutMessage, onData },
|
|
) {
|
|
let result;
|
|
try {
|
|
result = await runOwnedVerifierTerminal({
|
|
commands,
|
|
command: process.execPath,
|
|
args: ["-e", wrapper, command],
|
|
cwd: root,
|
|
environment: { ...environment, NO_COLOR: "1", TERM: "xterm-256color" },
|
|
timeoutMs,
|
|
...(onData === undefined ? {} : { onData }),
|
|
});
|
|
} catch (cause) {
|
|
if (cause?._tag === "VerifierTerminalFailure" && cause.reason === "timeout")
|
|
throw new Error(timeoutMessage + ": " + cause.output, { cause });
|
|
throw cause;
|
|
}
|
|
const { output, exitCode } = result;
|
|
const markerAt = output.lastIndexOf(marker);
|
|
let childResult;
|
|
try {
|
|
if (markerAt >= 0)
|
|
childResult = JSON.parse(output.slice(markerAt + marker.length).trim());
|
|
} catch {
|
|
childResult = undefined;
|
|
}
|
|
return { output, exitCode, childResult };
|
|
}
|
|
|
|
export async function verifyInteractiveSetup(options) {
|
|
const { commands, unchangedPaths } = options;
|
|
const unchanged = await captureVerifierFileSnapshot(commands, unchangedPaths);
|
|
let cancelled = false;
|
|
const marker = "__REA_SETUP_EXIT__";
|
|
const wrapper = `
|
|
const { spawnSync } = require("node:child_process");
|
|
const child = spawnSync(process.argv[1], ["setup"], { stdio: "inherit", env: process.env, shell: process.platform === "win32" });
|
|
const terminal = process.platform === "win32"
|
|
? spawnSync("cmd.exe", ["/c", "mode", "CON"], { encoding: "utf8", stdio: ["inherit", "pipe", "inherit"] })
|
|
: spawnSync("stty", ["-a"], { encoding: "utf8", stdio: ["inherit", "pipe", "inherit"] });
|
|
process.stdout.write("\\n${marker}" + JSON.stringify({ status: child.status, termios: terminal.stdout, error: child.error?.message }));
|
|
process.exit(child.status ?? 1);
|
|
`;
|
|
const { output, exitCode, childResult } = await runSetupTerminal(options, {
|
|
wrapper,
|
|
marker,
|
|
timeoutMs: 20_000,
|
|
timeoutMessage: "packaged setup wizard timed out after output",
|
|
onData(output, write) {
|
|
if (!cancelled && output.includes("Which agents should use REA?")) {
|
|
cancelled = true;
|
|
write("\u0003");
|
|
}
|
|
},
|
|
});
|
|
if (
|
|
!cancelled ||
|
|
exitCode !== 0 ||
|
|
childResult?.status !== 0 ||
|
|
!output.includes("\u001b[?25h") ||
|
|
!terminalModesRestored(childResult?.termios) ||
|
|
!output.includes("No changes were made.")
|
|
)
|
|
throw new Error(
|
|
`packaged rea-agents setup did not cancel with a restored terminal and exit 0: ${output}`,
|
|
);
|
|
await assertVerifierFilesUnchanged(
|
|
commands,
|
|
unchanged,
|
|
"Interactive cancellation",
|
|
);
|
|
}
|
|
|
|
export async function verifyRedirectedStdoutSetup(options) {
|
|
const { commands, unchangedPaths } = options;
|
|
const unchanged = await captureVerifierFileSnapshot(commands, unchangedPaths);
|
|
const marker = "__REA_REDIRECTED_SETUP__";
|
|
const wrapper = `
|
|
const { spawnSync } = require("node:child_process");
|
|
const child = spawnSync(process.argv[1], ["setup"], { encoding: "utf8", env: process.env, stdio: ["inherit", "pipe", "inherit"], shell: process.platform === "win32" });
|
|
process.stdout.write("\\n${marker}" + JSON.stringify({ status: child.status, stdout: child.stdout, error: child.error?.message, stdinTTY: process.stdin.isTTY, stderrTTY: process.stderr.isTTY, stdoutRedirected: true }));
|
|
process.exit(child.status ?? 1);
|
|
`;
|
|
const { output, exitCode, childResult } = await runSetupTerminal(options, {
|
|
wrapper,
|
|
marker,
|
|
timeoutMs: 15_000,
|
|
timeoutMessage:
|
|
"packaged setup started an interactive prompt with redirected stdout",
|
|
});
|
|
const allOutput = `${output}\n${childResult?.stdout ?? ""}`;
|
|
if (
|
|
(exitCode !== 0 && exitCode !== 1) ||
|
|
childResult?.status !== exitCode ||
|
|
childResult?.stdinTTY !== true ||
|
|
childResult?.stderrTTY !== true ||
|
|
childResult?.stdoutRedirected !== true ||
|
|
childResult?.stdout?.includes("Which agents should use REA?") ||
|
|
allOutput.includes("Which agents should use REA?") ||
|
|
/\u001b\[[0-9;?]*[ -/]*[@-~]/u.test(allOutput)
|
|
)
|
|
throw new Error(
|
|
`packaged setup did not remain plain and non-interactive with redirected stdout: ${output}`,
|
|
);
|
|
await assertVerifierFilesUnchanged(
|
|
commands,
|
|
unchanged,
|
|
"Redirected-stdout setup",
|
|
);
|
|
}
|