Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

13.2 KB
Raw Blame
#!/usr/bin/env node
// Offline artifact-level regression. Default invocation rebuilds the production
// sidecar; --bundle runs an explicitly supplied artifact (e.g. a saved red baseline).
// No Electron, host-core, database, user config, live credentials, or source mocks.
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { execFileSync } from "node:child_process";
import { copyFile, cp, mkdir, mkdtemp, readFile, readdir, stat, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { parseArgs } from "node:util";
import { pathToFileURL } from "node:url";
import { isolatedEnv } from "./e2e/hosted-search-sidecar.mjs";
import { runScenarios } from "./e2e/hosted-search-scenarios.mjs";

async function collectArtifactFiles(directory, relative = "") {
  const entries = await readdir(join(directory, relative), { withFileTypes: true });
  const files = [];
  for (const entry of entries) {
    const child = relative ? join(relative, entry.name) : entry.name;
    if (entry.isDirectory()) files.push(...(await collectArtifactFiles(directory, child)));
    else if (entry.isFile()) files.push(child);
  }
  return files;
}

async function bundleArtifactHash(entry) {
  const directory = resolve(entry, "..");
  const packagePath = join(directory, "package.json");
  let packageManifest;
  try {
    packageManifest = await readFile(packagePath);
  } catch (error) {
    if (error?.code !== "ENOENT") throw error;
    packageManifest = Buffer.from('{"type":"module"}\n');
  }

  const files = [
    ["entry", await readFile(entry)],
    ["package.json", packageManifest],
  ];
  const chunksDirectory = join(directory, "chunks");
  try {
    if ((await stat(chunksDirectory)).isDirectory()) {
      for (const relative of await collectArtifactFiles(chunksDirectory)) {
        files.push([join("chunks", relative), await readFile(join(chunksDirectory, relative))]);
      }
    }
  } catch (error) {
    if (error?.code !== "ENOENT") throw error;
  }

  const digest = createHash("sha256");
  for (const [name, content] of files.sort(([left], [right]) => left.localeCompare(right))) {
    digest.update(name);
    digest.update("\0");
    digest.update(content);
  }
  return digest.digest("hex");
}

async function snapshotBundleArtifact(entry, directory) {
  await mkdir(directory, { recursive: true });
  const snapshotEntry = join(directory, "sidecar.mjs");
  await copyFile(entry, snapshotEntry);

  const sourceDirectory = resolve(entry, "..");
  const packagePath = join(sourceDirectory, "package.json");
  try {
    await copyFile(packagePath, join(directory, "package.json"));
  } catch (error) {
    if (error?.code !== "ENOENT") throw error;
    await writeFile(join(directory, "package.json"), '{"type":"module"}\n');
  }

  const chunksDirectory = join(sourceDirectory, "chunks");
  try {
    if ((await stat(chunksDirectory)).isDirectory()) {
      await cp(chunksDirectory, join(directory, "chunks"), { recursive: true });
    }
  } catch (error) {
    if (error?.code !== "ENOENT") throw error;
  }
  return snapshotEntry;
}

const { values } = parseArgs({ options: {
  bundle: { type: "string" }, "timeout-ms": { type: "string", default: "15000" },
  help: { type: "boolean", short: "h" },
} });
if (values.help) {
  console.log(`Usage: node scripts/e2e-hosted-search.mjs [--bundle PATH] [--timeout-ms 15000]
Default: build shared, agent-runtime, and the production sidecar bundle.
--bundle: build the resolver module and run that explicit sidecar artifact.
Evidence and isolated homes stay under PI_SCRATCH_DIR or mkdtemp(os.tmpdir()).
Ten cases: malformed tool-call continuation (one or three failures), next prompt, Read, instruction change, out-of-project instruction fallback, real Task/TaskWait, persisted restore, two invalid-history cases.
Files are retained for inspection; no automatic deletion and no dependency installation.
Reviewed source, patches, and lock metadata are fingerprinted before the build and again
when the run ends; any change between the two fails the run.`);
} else {
  const root = resolve(import.meta.dirname, "..");
  const timeoutMs = Number(values["timeout-ms"]);
  assert.ok(Number.isFinite(timeoutMs) && timeoutMs >= 1000 && timeoutMs <= 120000, "timeout must be 1000..120000 ms");
  // Scratch allocation is the one step that has nowhere to record a failure: if
  // it throws there is no evidence directory, so the uncaught error on stderr is
  // the intended signal. There is deliberately no fallback location.
  const dir = await mkdtemp(join(process.env.PI_SCRATCH_DIR || tmpdir(), "hosted-search-e2e-"));
  console.log(`Evidence: ${dir}`);
  const evidence = { startedAt: new Date().toISOString() };
  // Declared before the guard so the `finally` below can still run them when an
  // early step (env isolation, dependency inspection, pnpm) fails.
  let git = () => "unavailable";
  let fingerprint = async () => null;
  try {
    const env = await isolatedEnv(join(dir, "build"));
    // Reuse the already resolved project pnpm, not a PATH version-manager shim
    // that might try to bootstrap another copy after HOME becomes isolated.
    env.npm_config_manage_package_manager_versions = "false";
    const pnpmEntry = process.env.npm_execpath;
    const pnpmCommand = pnpmEntry && /\.[cm]?js$/.test(pnpmEntry) ? process.execPath : pnpmEntry ?? "pnpm";
    const pnpmArgs = pnpmEntry && /\.[cm]?js$/.test(pnpmEntry) ? [pnpmEntry] : [];
    const runPnpm = (args) => execFileSync(pnpmCommand, [...pnpmArgs, ...args], {
      cwd: root, env, encoding: "utf8", timeout: 120000, maxBuffer: 8_000_000,
    });
    git = (...args) => {
      try {
        return execFileSync("git", args, { cwd: root, env, encoding: "utf8", timeout: 20000, maxBuffer: 32_000_000 }).trim();
      } catch { return "unavailable"; }
    };
    const hash = async (path) => createHash("sha256").update(await readFile(path)).digest("hex");
    // Reviewed source, patch, and lock metadata only: dependency trees and
    // generated bundles are build outputs, so hashing them would fingerprint the
    // build instead of the artifact under review.
    const sourceScope = ["packages/agent-runtime", "packages/shared", "patches", "pnpm-lock.yaml", "pnpm-workspace.yaml", "package.json", "scripts/e2e-hosted-search.mjs", "scripts/e2e/hosted-search-provider.mjs", "scripts/e2e/hosted-search-sidecar.mjs", "scripts/e2e/hosted-search-scenarios.mjs"];
    const generated = /(^|\/)(node_modules|dist|dist-bundle|coverage|\.turbo)(\/|$)|\.tsbuildinfo$/;
    const listGit = (args) => {
      try {
        return execFileSync("git", args, { cwd: root, env, encoding: "utf8", timeout: 20000, maxBuffer: 32_000_000 })
          .split("\0").map((entry) => entry.trim()).filter(Boolean);
      } catch { return null; }
    };
    // One digest over tracked files plus untracked deliverables in scope: the
    // `git status --short` string alone cannot reveal an edit that leaves the
    // dirty set identical.
    fingerprint = async () => {
      const tracked = listGit(["ls-files", "-z", "--", ...sourceScope]);
      const untracked = listGit(["ls-files", "-z", "--others", "--exclude-standard", "--", ...sourceScope]);
      const deleted = listGit(["ls-files", "-z", "--deleted", "--", ...sourceScope]);
      if (!tracked || !untracked || !deleted) return null;
      const deletedFiles = new Set(deleted);
      const files = [...new Set([...tracked, ...untracked])].filter((path) => !generated.test(path)).sort();
      const digests = [];
      for (const path of files) {
        digests.push(`${path} ${deletedFiles.has(path) ? "deleted" : await hash(join(root, path))}`);
      }
      return { files: files.length, sha256: createHash("sha256").update(digests.join("\n")).digest("hex") };
    };
    evidence.nodeVersion = process.version;
    evidence.pnpmVersion = values.bundle ? "not invoked (supplied artifact)" : runPnpm(["--version"]).trim();
    evidence.installedDependencies = {};
    const runtimeManifest = JSON.parse(await readFile(join(root, "packages/agent-runtime/package.json"), "utf8"));
    const expectedVersion = runtimeManifest.dependencies?.["@earendil-works/pi-ai"];
    assert.ok(
      typeof expectedVersion === "string" && /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(expectedVersion),
      "agent-runtime must exactly pin @earendil-works/pi-ai",
    );
    for (const name of ["pi-ai", "pi-agent-core", "pi-coding-agent"]) {
      const manifest = JSON.parse(await readFile(join(root, "packages/agent-runtime/node_modules/@earendil-works", name, "package.json"), "utf8"));
      evidence.installedDependencies[name] = manifest.version;
      const packageName = `@earendil-works/${name}`;
      assert.equal(runtimeManifest.dependencies?.[packageName], expectedVersion, `${packageName} must share the exact pi-ai pin`);
      assert.equal(manifest.version, expectedVersion, `${name} must match the agent-runtime pi pin`);
    }
    evidence.lockfileSha256 = await hash(join(root, "pnpm-lock.yaml"));
    evidence.head = git("rev-parse", "HEAD");
    evidence.statusBefore = git("status", "--short");
    evidence.build = values.bundle ? "agent-runtime resolver build + explicit supplied artifact" : "shared + agent-runtime builds and production bundle";
    evidence.sourceFingerprintBefore = await fingerprint();
    assert.ok(evidence.head !== "unavailable" && evidence.sourceFingerprintBefore, "build source identity is unavailable");
    if (!values.bundle) {
      for (const [pkg, script, label] of [
        ["@pi-desktop/shared", "build", "shared-build"],
        ["@pi-desktop/agent-runtime", "build", "agent-runtime-build"],
        ["@pi-desktop/agent-runtime", "bundle", "sidecar-bundle"],
      ]) {
        const args = ["--filter", pkg, script];
        console.log(`Build: pnpm ${args.join(" ")}`);
        const output = runPnpm(args);
        await writeFile(join(dir, `${label}.log`), output);
      }
    } else {
      const args = ["--filter", "@pi-desktop/agent-runtime", "build"];
      console.log(`Build: pnpm ${args.join(" ")}`);
      const output = runPnpm(args);
      await writeFile(join(dir, "agent-runtime-build.log"), output);
    }
    const instructionModule = await import(pathToFileURL(
      join(root, "packages/agent-runtime/dist/project-instructions.js"),
    ).href);
    const source = resolve(root, values.bundle ?? "packages/agent-runtime/dist-bundle/sidecar.js");
    const bundleDirectory = join(dir, "artifact");
    evidence.bundleSource = source;
    evidence.sha256 = await bundleArtifactHash(source);
    evidence.entrySha256 = await hash(source);
    // Snapshot the entry, ESM package marker, and every relative chunk so a
    // concurrent bundle rebuild cannot replace code halfway through scenarios.
    const bundle = await snapshotBundleArtifact(source, bundleDirectory);
    assert.equal(await bundleArtifactHash(bundle), evidence.sha256, "bundle changed during snapshot; rebuild and retry");
    console.log(`Bundle artifact SHA-256: ${evidence.sha256}`);
    evidence.results = await runScenarios(bundle, dir, timeoutMs, (result) => {
      console.log(`${result.passed ? "PASS" : "FAIL"} ${result.name} (${result.requests} provider requests)`);
      if (!result.passed) console.error(result.error);
    }, instructionModule.loadInstructionChain);
    assert.equal(await bundleArtifactHash(bundle), evidence.sha256, "executed snapshot was modified");
    // The snapshot protects execution; qualification also requires the source
    // artifact and every relative chunk to remain stable during the run.
    evidence.bundleSourceSha256After = await bundleArtifactHash(source);
    evidence.bundleSourceRewrittenDuringRun = evidence.bundleSourceSha256After !== evidence.sha256;
    assert.equal(evidence.bundleSourceRewrittenDuringRun, false, "bundle source changed during qualification");
    evidence.passed = evidence.results.every((result) => result.passed);
    if (!evidence.passed) process.exitCode = 1;
  } catch (error) {
    evidence.passed = false;
    evidence.error = error.stack ?? String(error);
    console.error(evidence.error);
    process.exitCode = 1;
  } finally {
    // The evidence directory exists by now, so no failure may escape before
    // result.json is written.
    try {
      evidence.sourceFingerprintAfter = await fingerprint();
    } catch (error) {
      evidence.sourceFingerprintError = error.stack ?? String(error);
    }
    const before = evidence.sourceFingerprintBefore;
    const after = evidence.sourceFingerprintAfter;
    evidence.sourceChangedDuringRun = Boolean(before && after && before.sha256 !== after.sha256);
    if (!before || !after || evidence.sourceChangedDuringRun) {
      evidence.qualificationFailure = !before || !after
        ? "reviewed source identity could not be verified"
        : "reviewed source, lockfile, or patch artifacts changed during the run";
      evidence.passed = false;
      process.exitCode = 1;
    }
    evidence.statusAfter = git("status", "--short");
    evidence.finishedAt = new Date().toISOString();
    try {
      await writeFile(join(dir, "result.json"), JSON.stringify(evidence, null, 2));
    } catch (error) {
      console.error(`failed to write result.json: ${error.message}`);
      evidence.passed = false;
      process.exitCode = 1;
    }
    console.log(`Result: ${evidence.passed ? "PASS" : "FAIL"}; ${join(dir, "result.json")}`);
  }
}