A desktop coding agent with free access to frontier models.
TypeScript
test: install wrapper coverage
ogsamratc6c1c6dfe8d9
diff --git a/apps/server/src/install-log.test.ts b/apps/server/src/install-log.test.tsnew file mode 100644index 00000000..94418700--- /dev/null+++ b/apps/server/src/install-log.test.ts@@ -0,0 +1,129 @@+// @ts-nocheck -- ported from the CLI suite (test/install-log.test.js); exercises loosely typed fakes.+import { test } from "bun:test";+import assert from "node:assert/strict";+import { spawnSync } from "node:child_process";+import { createHash } from "node:crypto";+import fs from "node:fs";+import os from "node:os";+import path from "node:path";++import * as il from "./context/install-log.js";+import * as assets from "./context/install-log-assets.js";+import * as context from "./context/index.js";+import { engineInstallLogEnv, prepareDesktopInstallLog, resetDesktopInstallLog } from "./install-log-host.js";+import * as uploader from "./session-uploader.js";++// Install log (reproducible environments, phase 1): the engines' wrappers note+// package installs of the agent's shell; the capture context sends them as+// `context.install` lines with exact versions, and `context.env_turn` per turn,+// only while the server's capabilities say `install_log.enabled`.+const SCRUB = {+ text: (text) => uploader.redactUploadText(text).text,+ json: (value) => uploader.redactUploadJson(value).value,+ content: (p, text) => uploader.redactUploadContent(p, text),+};+const posix = process.platform !== "win32";+const tmpdir = (prefix) => fs.mkdtempSync(path.join(os.tmpdir(), prefix));+const has = (cmd) => spawnSync("sh", ["-c", `command -v ${cmd}`], { encoding: "utf8" }).status === 0;+const online = (() => {+ if (process.env.OMNIRUSH_TEST_OFFLINE === "1" || !has("npm")) return false;+ const probe = spawnSync("npm", ["view", "[email protected]", "version"], { encoding: "utf8", timeout: 20_000 });+ return probe.status === 0 && probe.stdout.trim() === "7.0.0";+})();++test("the embedded wrappers match their digests; the Windows one needs only kernel32", () => {+ const sha = (data) => createHash("sha256").update(data).digest("hex");+ assert.equal(sha(assets.POSIX_WRAPPER), assets.POSIX_WRAPPER_SHA256);+ assert.equal(sha(assets.PYTHON_HOOK), assets.PYTHON_HOOK_SHA256);+ const exe = Buffer.from(assets.WINDOWS_WRAPPER_BASE64, "base64");+ assert.equal(sha(exe), assets.WINDOWS_WRAPPER_SHA256);+ assert.equal(exe.subarray(0, 2).toString(), "MZ");+ assert.doesNotMatch(exe.toString("latin1"), /msvcrt|ucrtbase|api-ms-win-crt/i);+});++test("the server switch comes from the capabilities answer; a missing key is off", () => {+ assert.deepEqual(il.installLogGate({ schema_versions: [1, 2, 3], canonical_trace: true }), { enabled: false, samplePercent: 0 });+ assert.deepEqual(il.installLogGate({ install_log: { enabled: true, sample_percent: 10 } }), { enabled: true, samplePercent: 10 });+ assert.deepEqual(il.installLogGate({ install_log: { enabled: false, sample_percent: 100 } }), { enabled: false, samplePercent: 0 });+});++test("managed engines get the wrappers first on PATH (and only once the desktop prepared them)", async () => {+ const root = tmpdir("omnirush-pkglog-host-");+ try {+ resetDesktopInstallLog();+ assert.deepEqual(engineInstallLogEnv({ PATH: "/usr/bin" }), {});+ assert.equal(await prepareDesktopInstallLog(path.join(root, "state"), { OMNIRUSH_INSTALL_LOG: "0" }), null);+ const paths = await prepareDesktopInstallLog(path.join(root, "state"), {});+ assert.ok(paths);+ const env = engineInstallLogEnv({ PATH: "/usr/bin:/bin", PYTHONPATH: "/x" });+ assert.equal(env.PATH, `${paths.bin}${path.delimiter}/usr/bin:/bin`);+ assert.equal(env.PYTHONPATH, `${paths.py}${path.delimiter}/x`);+ assert.equal(env.OMNIRUSH_PKGLOG_DIR, paths.raw);+ if (posix) assert.ok(fs.lstatSync(path.join(paths.bin, "npm")).isSymbolicLink());+ } finally {+ resetDesktopInstallLog();+ fs.rmSync(root, { recursive: true, force: true });+ }+});++test("the wrapper is transparent and a broken log never breaks the tool", { skip: !posix }, async () => {+ const root = tmpdir("omnirush-pkglog-wrap-");+ try {+ const paths = await il.prepareInstallLog({ stateDir: path.join(root, "state") });+ const real = path.join(root, "real");+ fs.mkdirSync(real);+ fs.writeFileSync(path.join(real, "npm"), "#!/bin/sh\nprintf 'args:'; for a in \"$@\"; do printf ' [%s]' \"$a\"; done; printf '\\n'; cat; echo err >&2; exit 5\n", { mode: 0o755 });+ await il.setInstallLogActive(paths.raw, true);+ const env = { ...process.env, PATH: `${paths.bin}:${real}:/usr/bin:/bin`, OMNIRUSH_PKGLOG_DIR: paths.raw };+ const run = spawnSync("npm", ["install", "a b", "$X"], { env, input: "in", encoding: "utf8" });+ assert.equal(run.status, 5);+ assert.equal(run.stdout, "args: [install] [a b] [$X]\nin");+ assert.equal(run.stderr, "err\n");+ assert.equal(fs.readdirSync(paths.raw).filter((name) => name !== "on").length, 1);+ fs.rmSync(paths.raw, { recursive: true, force: true });+ const broken = spawnSync("npm", ["install", "x"], { env, input: "", encoding: "utf8" });+ assert.equal(broken.status, 5);+ assert.equal(broken.stdout, "args: [install] [x]\n");+ } finally {+ fs.rmSync(root, { recursive: true, force: true });+ }+});++test("integration: an npm install the engine's shell runs becomes one context.install line with exact versions", { skip: !posix || !online, timeout: 180_000 }, async () => {+ const root = tmpdir("omnirush-pkglog-npm-");+ try {+ const paths = await il.prepareInstallLog({ stateDir: path.join(root, "state") });+ const project = path.join(root, "proj");+ fs.mkdirSync(project);+ fs.writeFileSync(path.join(project, "package.json"), JSON.stringify({ name: "p", version: "1.0.0" }));+ const events = [];+ const cap = new context.ContextCapture({+ client: "gui",+ scrub: SCRUB,+ record: (sessionId, type, data) => events.push({ sessionId, type, data }),+ sections: { system_packages: false, services: false, setup: false, pm_config: false, shell_aliases: false, processes: false },+ network: false,+ installLog: { raw: paths.raw, bin: paths.bin, gate: async () => ({ enabled: true, samplePercent: 100 }) },+ });+ cap.sessionStarted("ses_gui_pkglog1", project);+ cap.turnStarted("ses_gui_pkglog1");+ for (let i = 0; i < 50 && !fs.existsSync(path.join(paths.raw, "on")); i++) await new Promise((resolve) => setTimeout(resolve, 100));+ const env = { ...process.env, ...il.installLogEnv(paths, process.env), npm_config_audit: "false", npm_config_fund: "false" };+ cap.toolEvent("ses_gui_pkglog1", { callId: "call_1", tool: "bash", status: "running", command: "npm i [email protected]", workdir: project, at: Date.now() });+ const run = spawnSync("sh", ["-c", "npm i [email protected] --registry https://registry.npmjs.org/"], { cwd: project, env, encoding: "utf8", timeout: 120_000 });+ assert.equal(run.status, 0, run.stderr);+ cap.toolEvent("ses_gui_pkglog1", { callId: "call_1", tool: "bash", status: "completed", at: Date.now() });+ cap.turnEnded("ses_gui_pkglog1");+ cap.turnMessages("ses_gui_pkglog1", [], true);+ for (let i = 0; i < 200 && !events.some((e) => e.type === "context.env_turn"); i++) await new Promise((resolve) => setTimeout(resolve, 100));+ const lines = events.filter((e) => e.type === "context.install").map((e) => JSON.parse(JSON.stringify(e.data)));+ assert.equal(lines.length, 1);+ assert.equal(lines[0].call, "call_1");+ assert.deepEqual(lines[0].resolved.map((p) => `${p.name}@${p.version}`), ["[email protected]"]);+ assert.equal(lines[0].lockfile_rel, "package-lock.json");+ assert.ok(events.some((e) => e.type === "context.env_turn" && e.data.installs === 1));+ cap.sessionEnded("ses_gui_pkglog1");+ } finally {+ fs.rmSync(root, { recursive: true, force: true });+ }+});