Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

26.3 KB
Raw Blame
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { createHash, randomBytes } from "node:crypto";
import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";

import { compileBuilder } from "../deploy/work-broker/broker.mjs";
import { buildBuilderAgentConfig, buildBuilderPrompt } from "../deploy/work-runner/docker-builder.mjs";
import { deriveBuilderDockerRuntime, runBuilderDockerLifecycle } from "../deploy/work-runner/docker-supervisor.mjs";
import { runRpcSession } from "../deploy/work-runner/rpc-client.mjs";
import { claimLease, createLeaseConsumption, discardPreparedRun, prepareBuilderRun } from "../deploy/work-runner/runner-core.mjs";
import { buildCapabilityRetentionEvidence, validateCapabilityRetentionEvidence } from "../scripts/work-capability-retention.mjs";
import { fixtureModelBackendLabelArguments } from "./fixtures/work/model-backend.mjs";
import { installFixtureModelQualification } from "./fixtures/work/model-qualification.mjs";

const IMAGE_ID_RE = /^sha256:[a-f0-9]{64}$/u;
const GIT_HASH_RE = /^[a-f0-9]{40}$/u;
const HASH_RE = /^[a-f0-9]{64}$/u;
const OMP_TOOLS = ["read", "grep", "glob", "write", "edit", "bash", "eval", "lsp", "debug", "task", "hub", "todo"];
const MAIN_MARKER = "PIXEL_CAPABILITY_RETENTION_OBJECTIVE";
const EXPECTED_OBSERVED_TOOLS = ["bash", "debug", "edit", "eval", "glob", "grep", "hub", "lsp", "read", "task", "write"];
const sha = (bytes) => createHash("sha256").update(bytes).digest("hex");

function docker(path, args, options = {}) {
  return execFileSync(path, args, {
    encoding: "utf8", windowsHide: true, timeout: options.timeout ?? 60000,
    maxBuffer: options.maxBuffer ?? 8 * 1024 * 1024,
    env: { HOME: "/nonexistent", PATH: "/usr/bin:/bin", LANG: "C.UTF-8", DOCKER_CONFIG: "/nonexistent" },
    stdio: options.stdio ?? ["ignore", "pipe", "pipe"],
  }).trim();
}

function octal(value, length) {
  return `${value.toString(8).padStart(length - 1, "0")}\0`;
}

function tarHeader(name, type, size) {
  const value = Buffer.alloc(512);
  value.write(name, 0, 100, "utf8");
  value.write(octal(type === "directory" ? 0o755 : 0o644, 8), 100, 8, "ascii");
  value.write(octal(0, 8), 108, 8, "ascii");
  value.write(octal(0, 8), 116, 8, "ascii");
  value.write(octal(size, 12), 124, 12, "ascii");
  value.write(octal(0, 12), 136, 12, "ascii");
  value.fill(0x20, 148, 156);
  value[156] = type === "directory" ? 0x35 : 0x30;
  value.write("ustar\0", 257, 6, "latin1");
  value.write("00", 263, 2, "ascii");
  let checksum = 0;
  for (const byte of value) checksum += byte;
  value.write(`${checksum.toString(8).padStart(6, "0")}\0 `, 148, 8, "ascii");
  return value;
}

function archive(items) {
  const blocks = [];
  for (const item of items) {
    const content = Buffer.from(item.content ?? "", "utf8");
    blocks.push(tarHeader(item.name, item.type ?? "file", content.length));
    if (content.length) {
      blocks.push(content);
      const padding = (512 - (content.length % 512)) % 512;
      if (padding) blocks.push(Buffer.alloc(padding));
    }
  }
  blocks.push(Buffer.alloc(1024));
  return Buffer.concat(blocks);
}

const FIXTURE = Object.freeze([
  { name: "nested/", type: "directory" },
  { name: "proofs/", type: "directory" },
  { name: "fixture.txt", content: "PIXEL_READ_TOKEN\n" },
  { name: "nested/search.txt", content: "ordinary\nPIXEL_SEARCH_TOKEN\n" },
  { name: "nested/discovery.flag", content: "DISCOVERY_OK\n" },
  { name: "edit.txt", content: "EDIT_BEFORE\n" },
  { name: "pyproject.toml", content: "[project]\nname = \"pixel-retention-fixture\"\nversion = \"0.0.0\"\n" },
  { name: "app.py", content: "def add(left: int, right: int) -> int:\n    return left + right\n\n\nanswer = add(20, 22)\n" },
  { name: "debug_target.py", content: "from pathlib import Path\n\nPath(\"proofs\").mkdir(exist_ok=True)\nvalue = \"DEBUG_OK\"\nPath(\"proofs/debug.txt\").write_text(f\"{value}\\n\", encoding=\"utf-8\")\n" },
]);

function fixtureArchive() {
  return archive(FIXTURE);
}

async function writeFixture(path) {
  await mkdir(join(path, "nested"), { recursive: true, mode: 0o700 });
  await mkdir(join(path, "proofs"), { recursive: true, mode: 0o700 });
  for (const item of FIXTURE) {
    if (item.type === "directory") continue;
    await writeFile(join(path, item.name), item.content, { mode: 0o600 });
  }
}

function ipv4(value) {
  return value.split(".").map(Number).reduce((result, part) => (((result << 8) | part) >>> 0), 0);
}

function pickSubnet(path) {
  const ids = docker(path, ["network", "ls", "--quiet"]).split(/\s+/u).filter(Boolean);
  const occupied = [];
  if (ids.length) {
    for (const network of JSON.parse(docker(path, ["network", "inspect", ...ids], { maxBuffer: 16 * 1024 * 1024 }))) {
      for (const config of network.IPAM?.Config ?? []) {
        const match = /^(\d+\.\d+\.\d+\.\d+)\/(\d+)$/.exec(config.Subnet ?? "");
        if (!match) continue;
        const prefix = Number(match[2]);
        const mask = prefix === 0 ? 0 : (0xffffffff << (32 - prefix)) >>> 0;
        const start = ipv4(match[1]) & mask;
        occupied.push({ start: start >>> 0, end: (start | (~mask >>> 0)) >>> 0 });
      }
    }
  }
  for (let third = 220; third < 240; third += 1) for (let fourth = 0; fourth < 256; fourth += 8) {
    const base = ipv4(`172.29.${third}.${fourth}`);
    if (occupied.every((range) => base + 7 < range.start || base > range.end)) return {
      networkSubnet: `172.29.${third}.${fourth}/29`, workerIp: `172.29.${third}.${fourth + 2}`, proxyIp: `172.29.${third}.${fourth + 3}`,
    };
  }
  throw new Error("No isolated /29 test subnet is available");
}

function budgets() {
  return {
    maxRuntimeSeconds: 240, maxIterations: 5, maxToolCalls: 40, maxConcurrentSubagents: 1,
    maxModelRequests: 30, maxInputTokens: 500000, maxOutputTokens: 20000,
    maxCpuCores: 1, maxMemoryMiB: 2048, maxDiskBytes: 128 * 1024 * 1024,
    maxArtifactBytes: 2 * 1024 * 1024, maxNetworkBytes: 16 * 1024 * 1024,
    maxFailures: 1, noProgressLimit: 1,
  };
}

async function waitForBackend(path, name) {
  for (let attempt = 0; attempt < 50; attempt += 1) {
    try {
      docker(path, ["container", "exec", name, "/opt/node/bin/node", "-e", 'fetch("http://127.0.0.1:8080/not-found",{method:"POST"}).then(async r=>{await r.arrayBuffer();if(r.status!==404)process.exit(1)}).catch(()=>process.exit(1))'], { timeout: 3000 });
      return;
    } catch (error) {
      if (attempt === 49) throw error;
      await new Promise((resolveDelay) => setTimeout(resolveDelay, 100));
    }
  }
}

function startBackend(path, image, network, name, alias, fakeServer, label) {
  docker(path, ["network", "create", "--driver", "bridge", "--internal", "--attachable=false", "--label", `com.osmantic.pixel.test=${label}`, network]);
  docker(path, [
    "run", "--rm", "-d", "--pull", "never", "--name", name,
    ...fixtureModelBackendLabelArguments(),
    "--network", network, "--network-alias", alias,
    "--read-only", "--cap-drop", "ALL", "--security-opt", "no-new-privileges:true",
    "--pids-limit", "64", "--memory", "512m", "--memory-swap", "512m", "--cpus", "1",
    "--ulimit", "nofile=128:128", "--ipc", "none", "--cgroupns", "private",
    "--stop-timeout", "3", "--log-driver", "none", "--user", `${process.getuid()}:${process.getgid()}`,
    "--tmpfs", "/tmp:rw,nosuid,nodev,noexec,size=64m,mode=1777",
    "--env", "HOME=/nonexistent", "--env", "NODE_ENV=production", "--env", "PIXEL_FAKE_MODEL_ID=assistant-model",
    "--mount", `type=bind,src=${fakeServer},dst=/opt/pixel-test/fake-llama-capability-retention.mjs,readonly`,
    "--entrypoint", "/opt/node/bin/node", image, "/opt/pixel-test/fake-llama-capability-retention.mjs",
  ]);
}

async function waitForCid(path) {
  for (let attempt = 0; attempt < 100; attempt += 1) {
    try {
      const value = (await readFile(path, "utf8")).trim();
      if (/^[a-f0-9]{64}$/u.test(value)) return value;
    } catch { /* worker has not written its CID yet */ }
    await new Promise((resolveDelay) => setTimeout(resolveDelay, 50));
  }
  throw new Error("direct OMP worker did not publish its container ID");
}

function validateDirectBoundary(container, network, runnerImageId, workspace, executorPath, configPath) {
  const host = container.HostConfig ?? {};
  const labels = container.Config?.Labels ?? {};
  const mounts = new Map((container.Mounts ?? []).map((entry) => [entry.Destination, entry]));
  assert.equal(container.Image, runnerImageId);
  assert.equal(container.Config?.User, `${process.getuid()}:${process.getgid()}`);
  assert.equal(labels["com.osmantic.pixel.test"], "capability-retention-direct");
  assert.equal(host.ReadonlyRootfs, true);
  assert.equal(host.Privileged, false);
  assert.ok((host.CapDrop ?? []).includes("ALL"));
  assert.ok((host.SecurityOpt ?? []).some((entry) => entry.replaceAll("=", ":") === "no-new-privileges:true"));
  assert.equal(host.NetworkMode, network);
  assert.equal(host.IpcMode, "none");
  assert.equal(host.CgroupnsMode, "private");
  assert.equal(host.PidsLimit, 512);
  assert.equal(host.Memory, 2048 * 1024 * 1024);
  assert.equal(host.MemorySwap, host.Memory);
  assert.equal(host.LogConfig?.Type, "none");
  assert.deepEqual(host.Devices ?? [], []);
  assert.deepEqual(host.PortBindings ?? {}, {});
  assert.equal(mounts.get("/workspace")?.Source, workspace);
  assert.equal(mounts.get("/workspace")?.RW, true);
  assert.equal(mounts.get("/opt/omp")?.Source, executorPath);
  assert.equal(mounts.get("/opt/omp")?.RW, false);
  assert.equal(mounts.get("/run/pixel/builder-config.json")?.Source, configPath);
  assert.equal(mounts.get("/run/pixel/builder-config.json")?.RW, false);
  assert.equal(mounts.has("/var/run/docker.sock"), false);
}

async function runDirectLane({ dockerPath, runnerImageId, executorPath, workspace, configPath, networkName, backendName, prompt, root }) {
  const cidPath = join(root, "direct-worker.cid");
  const workerName = `pixel-retention-direct-${randomBytes(6).toString("hex")}`;
  const args = [
    "run", "--rm", "--pull", "never", "--name", workerName,
    "--label", "com.osmantic.pixel.test=capability-retention-direct",
    "--network", networkName, "--read-only", "--cap-drop", "ALL", "--security-opt", "no-new-privileges:true",
    "--pids-limit", "512", "--memory", "2048m", "--memory-swap", "2048m", "--cpus", "1",
    "--ulimit", "nofile=512:512", "--ipc", "none", "--cgroupns", "private", "--stop-timeout", "3", "--log-driver", "none",
    "--user", `${process.getuid()}:${process.getgid()}`, "-i", "--hostname", "pixel-direct-omp", "--cidfile", cidPath, "--workdir", "/workspace",
    "--tmpfs", "/tmp:rw,nosuid,nodev,exec,size=512m,mode=1777",
    "--env", "HOME=/tmp/home", "--env", "XDG_CONFIG_HOME=/tmp/xdg/config", "--env", "XDG_CACHE_HOME=/tmp/xdg/cache",
    "--env", "XDG_DATA_HOME=/tmp/xdg/data", "--env", "PI_CODING_AGENT_DIR=/tmp/agent", "--env", "PI_CONFIG_DIR=.pixel-omp", "--env", "PI_NO_PTY=1", "--env", "PI_RPC_EMIT_TITLE=0",
    "--env", "PATH=/opt/node/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
    "--env", "LLAMA_CPP_BASE_URL=http://direct-model:8080", "--env", "NO_PROXY=direct-model,127.0.0.1,localhost",
    "--mount", `type=bind,src=${workspace},dst=/workspace`,
    "--mount", `type=bind,src=${executorPath},dst=/opt/omp,readonly`,
    "--mount", `type=bind,src=${configPath},dst=/run/pixel/builder-config.json,readonly`,
    "--entrypoint", "/opt/pixel/deploy/work-runner/builder-entrypoint.sh", runnerImageId,
    "--mode", "rpc", "--model", "llama.cpp/assistant-model", "--cwd", "/workspace", "--config", "/run/pixel/builder-config.json",
    "--no-session", "--no-extensions", "--no-skills", "--no-rules", "--no-title", "--no-pty",
    "--tools", OMP_TOOLS.join(","), "--approval-mode", "yolo", "--max-time", "240",
    "--system-prompt", "You are the direct OMP baseline inside a disposable synthetic workspace. Complete the immutable objective using the available local tools. No credentials, internet, host access, or external effects are authorized.",
  ];
  const rpcPromise = runRpcSession({
    command: dockerPath, args, cwd: "/",
    env: { HOME: "/nonexistent", PATH: "/usr/bin:/bin", LANG: "C.UTF-8", DOCKER_CONFIG: "/nonexistent" },
    prompt, allowedTools: OMP_TOOLS, maxPromptBytes: 65536, maxRuntimeMs: 240000,
    maxToolCalls: 40, maxResultBytes: 1024 * 1024, maxStderrBytes: 4 * 1024 * 1024, terminationTimeoutMs: 10000,
    onTerminate: () => { try { docker(dockerPath, ["container", "rm", "--force", workerName]); } catch { /* bounded cleanup */ } },
  });
  const cid = await waitForCid(cidPath);
  const container = JSON.parse(docker(dockerPath, ["container", "inspect", cid]))[0];
  validateDirectBoundary(container, networkName, runnerImageId, workspace, executorPath, configPath);
  const network = JSON.parse(docker(dockerPath, ["network", "inspect", networkName]))[0];
  assert.equal(network.Internal, true);
  assert.equal(network.Attachable, false);
  const peers = Object.values(network.Containers ?? {}).map((entry) => entry.Name).sort();
  assert.deepEqual(peers, [backendName, workerName].sort());
  return rpcPromise;
}

async function readOrNull(path) {
  try { return await readFile(path, "utf8"); } catch { return null; }
}

async function verifyLane(corpus, observedTools, content) {
  const results = [];
  for (const task of corpus.tasks) {
    const separator = task.proof.lastIndexOf("#");
    const path = task.proof.slice(0, separator);
    const token = task.proof.slice(separator + 1);
    const text = await content(path);
    results.push({ id: task.id, status: observedTools.includes(task.tool) && text?.includes(token) ? "pass" : "fail" });
  }
  return results;
}

function patchReader(patch) {
  const files = new Map(patch.changes.filter((entry) => entry.contentBase64 !== null).map((entry) => [entry.path.replace(/^source\//u, ""), Buffer.from(entry.contentBase64, "base64").toString("utf8")]));
  return async (path) => files.get(path) ?? null;
}

async function runNamedLane(name, operation) {
  try {
    return await operation();
  } catch (error) {
    if (error instanceof Error) error.message = `${name} lane: ${error.message}`;
    throw error;
  }
}

async function main() {
  if (process.platform !== "linux") throw new Error("Live capability-retention qualification requires Linux");
  process.umask(0o077);
  const dockerPath = process.env.PIXEL_WORK_DOCKER_PATH ?? "/usr/bin/docker";
  const runnerImageId = process.env.PIXEL_WORK_RUNNER_IMAGE_ID;
  const backendImageId = process.env.PIXEL_WORK_FAKE_MODEL_IMAGE_ID ?? runnerImageId;
  const executorPath = resolve(process.env.PIXEL_WORK_OMP_PATH ?? "");
  const sourceCommit = process.env.PIXEL_SOURCE_COMMIT;
  const sourceTree = process.env.PIXEL_SOURCE_TREE;
  const sourceArchiveSha256 = process.env.PIXEL_SOURCE_ARCHIVE_SHA256;
  const expectedContractSha = process.env.PIXEL_BUILDER_CONTRACT_SHA256;
  if (!IMAGE_ID_RE.test(runnerImageId ?? "") || !IMAGE_ID_RE.test(backendImageId ?? "")) throw new Error("Exact local runner and fake-model image IDs are required");
  if (!GIT_HASH_RE.test(sourceCommit ?? "") || !GIT_HASH_RE.test(sourceTree ?? "") || !HASH_RE.test(sourceArchiveSha256 ?? "") || !HASH_RE.test(expectedContractSha ?? "")) throw new Error("Exact source archive, Git identity, and Builder contract bindings are required");
  const sourceRoot = resolve(import.meta.dirname, "..");
  const sourceStatus = execFileSync("git", ["status", "--porcelain=v1", "--untracked-files=all"], { cwd: sourceRoot, encoding: "utf8" });
  const observedCommit = execFileSync("git", ["rev-parse", "HEAD"], { cwd: sourceRoot, encoding: "utf8" }).trim();
  const observedTree = execFileSync("git", ["rev-parse", "HEAD^{tree}"], { cwd: sourceRoot, encoding: "utf8" }).trim();
  if (sourceStatus !== "" || observedCommit !== sourceCommit || observedTree !== sourceTree) throw new Error("Live capability-retention source is not the exact clean Git identity selected for qualification");
  const executorBytes = await readFile(executorPath);
  const builderContractBytes = await readFile(new URL("../deploy/work-runner/builder-runtime.json", import.meta.url));
  if (sha(builderContractBytes) !== expectedContractSha) throw new Error("independently selected Builder contract hash differs from the live source");
  const corpus = JSON.parse(await readFile(new URL("../security-evals/assurance/builder-capability-corpus-v1.json", import.meta.url), "utf8"));

  const root = await mkdtemp(join(tmpdir(), "pixel-work-retention-live-"));
  await chmod(root, 0o700);
  const objectStore = join(root, "objects");
  const workspaceRoot = join(root, "workspaces");
  const stateRoot = join(root, "state");
  const directWorkspace = join(root, "direct-workspace");
  await Promise.all([objectStore, workspaceRoot, stateRoot, directWorkspace].map((path) => mkdir(path, { mode: 0o700 })));
  await writeFixture(directWorkspace);
  const fakeServer = resolve(import.meta.dirname, "fixtures/work/fake-llama-capability-retention.mjs");
  const suffix = randomBytes(6).toString("hex");
  const now = new Date();
  const startedAt = now.toISOString();
  const jobId = `work-${String(now.getTime()).padStart(13, "0")}-${suffix}`;
  const directNetwork = `pixel-retention-direct-net-${suffix}`;
  const directBackend = `pixel-retention-direct-model-${suffix}`;
  const containedNetwork = `pixel-retention-pixel-net-${suffix}`;
  const containedBackend = `pixel-retention-pixel-model-${suffix}`;
  let prepared;
  let directStarted = false;
  let containedStarted = false;
  try {
    const bytes = fixtureArchive();
    const contentSha256 = sha(bytes);
    await writeFile(join(objectStore, `${contentSha256}.tar`), bytes, { mode: 0o600 });
    const policy = JSON.parse(await readFile(new URL("../deploy/work-broker/policy.example.json", import.meta.url), "utf8"));
    policy.enabled = true;
    policy.executor.sha256 = sha(executorBytes);
    policy.runner.prepared = true;
    policy.runner.imageDigest = runnerImageId;
    policy.runner.imageRef = `local/pixel-work-runner@${runnerImageId}`;
    policy.localModel.prepared = true;
    policy.localModel.imageDigest = backendImageId;
    policy.localModel.imageRef = `local/pixel-fake-model@${backendImageId}`;
    policy.localModel.id = "assistant-model";
    policy.localModel.contextWindow = 32768;
    policy.profiles.scout.enabled = true;
    policy.profiles.builder.enabled = true;
    policy.verifier.enabled = true;
    await installFixtureModelQualification(policy, root, now);
    const request = {
      $schema: "https://osmantic.com/pixel/schemas/work-job-v1.schema.json", schemaVersion: 1,
      jobId, createdAt: now.toISOString(), requester: "pixel", profile: "builder",
      objective: `${MAIN_MARKER}: Complete every public synthetic capability operation and leave its exact local proof under one continuously authorized objective.`,
      acceptanceCriteria: ["All eleven corpus proof selectors pass external verification"],
      verification: {
        mode: "independent",
        checks: [
          { id: "patch-boundary", kind: "patch-integrity", criterionIndexes: [0] },
          {
            id: "retention-proofs", kind: "command", criterionIndexes: [0], workingDirectory: "source",
            argv: ["/opt/node/bin/node", "-e", "const f=require('fs');const e=(p,s)=>f.readFileSync(p,'utf8').includes(s);if(!e('proofs/semantic.txt','READ_OK')||!e('proofs/semantic.txt','SEARCH_OK')||!e('proofs/semantic.txt','GLOB_OK')||!e('proofs/semantic.txt','LSP_OK')||!e('proofs/write.txt','WRITE_OK')||!e('edit.txt','EDIT_AFTER')||!e('proofs/bash.txt','BASH_OK')||!e('proofs/debug.txt','DEBUG_OK')||!e('proofs/eval.txt','EVAL_OK')||!e('proofs/coordination.txt','SUBAGENT_OK')||!e('proofs/coordination.txt','HUB_OK'))process.exit(1)"],
            timeoutSeconds: 60, maxOutputBytes: 65536,
          },
        ],
        immutablePathPrefixes: [], maxRuntimeSeconds: 60, maxOutputBytes: 65536, network: "none",
        boundary: "Immutable controller-selected checks only. Worker output cannot alter criteria, commands, protected paths, budgets, network, or pass/fail rules.",
      },
      dataClassification: "public",
      inputs: [{ id: "source", kind: "repository-snapshot", mountMode: "read-only", contentSha256, maxBytes: bytes.length, classification: "public" }],
      requestedCapabilities: {
        filesystem: "disposable-read-write", tools: ["read", "search", "write", "edit", "bash", "eval", "lsp", "debug", "task", "hub"],
        network: { mode: "brokered", services: ["local-model"] }, modelRoute: "local-only",
        hostAccess: false, ambientCredentials: false, externalEffects: false, mergeAuthority: false, deployAuthority: false, policyMutation: false,
      },
      budgets: budgets(), outputs: { mode: "patch", requiredKinds: ["patch", "test-evidence"] },
      boundary: "This request proposes a bounded disposable job only. It grants no capability, credential, network, merge, deployment, policy-change, production, publication, purchase, or external-effect authority.",
    };
    const entries = [{
      id: "source", kind: "repository-snapshot", objectName: `${contentSha256}.tar`, contentSha256,
      bytes: bytes.length, classification: "public", mountMode: "read-only",
    }];
    const compiled = compileBuilder(request, policy, entries, { now, suffix });
    prepared = await prepareBuilderRun({
      ...compiled, policy, objectStore, workspaceRoot, executorPath,
      archiveLimits: { maxEntries: 100, maxFileBytes: 1024 * 1024 }, now,
    });
    const claim = createLeaseConsumption(prepared, { now, suffix });
    await claimLease(stateRoot, claim);
    const runtimeOptions = {
      dockerPath, stateRoot, ...pickSubnet(dockerPath), backendNetworkName: containedNetwork, backendContainerName: containedBackend,
      imageIdentifier: runnerImageId, backendImageIdentifier: backendImageId, uid: process.getuid(), gid: process.getgid(),
    };
    const runtime = deriveBuilderDockerRuntime(prepared, claim, runtimeOptions);
    const directConfig = join(root, "direct-builder-config.json");
    await writeFile(directConfig, `${JSON.stringify(buildBuilderAgentConfig(prepared, claim, runtime))}\n`, { mode: 0o600 });
    const prompt = buildBuilderPrompt(compiled.plan);

    startBackend(dockerPath, backendImageId, directNetwork, directBackend, "direct-model", fakeServer, "capability-retention-direct-model");
    directStarted = true;
    await waitForBackend(dockerPath, directBackend);
    const baseline = await runNamedLane("direct OMP", () => runDirectLane({
      dockerPath, runnerImageId, executorPath, workspace: directWorkspace, configPath: directConfig,
      networkName: directNetwork, backendName: directBackend, prompt, root,
    }));
    assert.match(baseline.text, /PIXEL_CAPABILITY_RETENTION_GREEN/u);
    assert.deepEqual(baseline.observedTools, EXPECTED_OBSERVED_TOOLS);
    const baselineResults = await verifyLane(corpus, baseline.observedTools, (path) => readOrNull(join(directWorkspace, path)));

    docker(dockerPath, ["container", "rm", "--force", directBackend]);
    docker(dockerPath, ["network", "rm", directNetwork]);
    directStarted = false;

    startBackend(dockerPath, backendImageId, containedNetwork, containedBackend, "pixel-local-model", fakeServer, "capability-retention-pixel-model");
    containedStarted = true;
    await waitForBackend(dockerPath, containedBackend);
    const contained = await runNamedLane("Pixel Builder", () => runBuilderDockerLifecycle(prepared, claim, runtimeOptions));
    assert.match(contained.text, /PIXEL_CAPABILITY_RETENTION_GREEN/u);
    assert.deepEqual(contained.observedTools, EXPECTED_OBSERVED_TOOLS);
    assert.equal(contained.verification.evidence.status, "pass");
    const patch = JSON.parse(await readFile(contained.patch.path, "utf8"));
    const containedResults = await verifyLane(corpus, contained.observedTools, patchReader(patch));
    const evidence = buildCapabilityRetentionEvidence({
      sourceCommit, sourceTree, sourceArchiveSha256, executorVersion: policy.executor.version, executorArtifactSha256: policy.executor.sha256,
      runnerImageDigest: runnerImageId, builderContractSha256: expectedContractSha, corpus,
      baselineResults, containedResults, startedAt, finishedAt: new Date().toISOString(),
    });
    assert.deepEqual(validateCapabilityRetentionEvidence(evidence, corpus, {
      sourceCommit, sourceTree, sourceArchiveSha256, executorVersion: policy.executor.version, executorArtifactSha256: policy.executor.sha256,
      runnerImageDigest: runnerImageId, builderContractSha256: expectedContractSha,
    }), []);
    if (process.env.PIXEL_CAPABILITY_RETENTION_EVIDENCE_PATH) {
      const path = resolve(process.env.PIXEL_CAPABILITY_RETENTION_EVIDENCE_PATH);
      await mkdir(dirname(path), { recursive: true, mode: 0o700 });
      await writeFile(path, `${JSON.stringify(evidence, null, 2)}\n`, { mode: 0o600, flag: "wx" });
    }
    assert.equal(docker(dockerPath, ["container", "ls", "--all", "--quiet", "--filter", `label=com.osmantic.pixel.work-job=${jobId}`]), "");
    assert.equal(docker(dockerPath, ["network", "ls", "--quiet", "--filter", `label=com.osmantic.pixel.work-job=${jobId}`]), "");
    assert.equal(docker(dockerPath, ["volume", "ls", "--quiet", "--filter", `label=com.osmantic.pixel.work-job=${jobId}`]), "");
    process.stdout.write(`${JSON.stringify({
      status: "pass", baselinePasses: evidence.baseline.passed, containedPasses: evidence.contained.passed,
      retentionPermille: evidence.comparison.retentionPermille, baselineToolCalls: baseline.toolCalls,
      containedToolCalls: contained.toolCalls, containedModelRequests: contained.proxyReceipt.modelRequests,
      runnerImageDigest: runnerImageId, evidenceSha256: sha(Buffer.from(`${JSON.stringify(evidence, null, 2)}\n`)),
    })}\n`);
  } finally {
    if (prepared) await discardPreparedRun(prepared).catch(() => {});
    for (const [started, name, network] of [[directStarted, directBackend, directNetwork], [containedStarted, containedBackend, containedNetwork]]) {
      if (!started) continue;
      try { docker(dockerPath, ["container", "rm", "--force", name]); } catch { /* bounded cleanup */ }
      try { docker(dockerPath, ["network", "rm", network]); } catch { /* bounded cleanup */ }
    }
    await rm(root, { recursive: true, force: true });
  }
}

main().catch((error) => {
  const details = error instanceof Error ? {
    message: error.message, exitCode: error.exitCode ?? null, exitSignal: error.exitSignal ?? null,
    protocolStage: error.protocolStage ?? null, observedFrames: error.observedFrames ?? null, stderrBytes: error.stderrBytes ?? null,
  } : { message: "failed" };
  process.stderr.write(`pixel-work-capability-retention-live: ${JSON.stringify(details)}\n`);
  process.exitCode = 1;
});