Files
PaperClipAI/tests/runner-e2e/installed-cli.ts
T
DottaandPaperclip 7c247243e5 Qualify and package Copilot for ordinary installations (#15706)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - A provider must work through normal installed packages on local and
Daytona environments.
> - Candidate-only success does not prove that the shipped package
contains the manifest and native runtime needed for Daytona.
> - The release must preserve saved-token access and task lifecycle
guarantees.
> - This pull request will qualify Copilot and include the exact-source
remote manifest in normal release archives.
> - The benefit is a reviewable part of the Copilot production release.

## Linked Issues or Issue Description

Refs #15701, #15703 and #15705. Supersedes qualification work in #15563
and the original candidate in #14434. Base: #15705.

## What Changed

- Keep Copilot discovery passive during reconnect reconciliation;
refresh on explicit opening, account/environment changes and reconnect.

- Retain the selected Product cases, permission/continuity evidence and
cleanup checks.
- Build the Linux provider manifest from the existing pinned Docker
stage in the cloud.
- Join all four native daemon receipts with that manifest before server
packing.
- Reject missing or incompatible remote authority during normal release
assembly; retain historical workflow compatibility.
- Promote the completed profile47 qualification marker, publish the
current qualification record and preserve every failed attempt.
- Keep one current release checklist and the shipped-canary gates
explicit.

## Verification

Runtime #15701 is merged at `d855e550ad8d2a267312983f26457aabf5393c3a`.
History #15703 is merged at `df80616d98119cbf5abd40549794d9980a5fabf6`.
Remaining source
[`42bcbd61299655f7e37aabe6bb5f19bba770ad1d`](https://github.com/paperclipai/paperclip/commit/42bcbd61299655f7e37aabe6bb5f19bba770ad1d)
contains the four-file authorization repair. The remaining PR trees
preserve six nonoverlapping files from the newer public mainline
managed-settings fix and the CI-owned lockfile. No unrelated provider is
enabled.

The security finding is repaired: existing-agent discovery requires
`agent_config:update` before connection selection or token resolution.
The saved environment is the default. Authorized editors can select a
prospective environment. Both denied board-viewer and agent-reader
regressions fail on the original source. All49 route tests and server
typecheck pass with the fix. Denied requests perform no connection
selection, credential resolution or probe. Current-head security scans
pass on both remaining PRs; the repaired thread is resolved.

Copilot CLI1.0.88 and profile47 retain digest
`sha256:55044aa235e88b3fe36ec8a556ee1e3382590870aab907e4462ef7d64af49cea`.
The [qualification
record](https://github.com/paperclipai/paperclip/blob/42bcbd61299655f7e37aabe6bb5f19bba770ad1d/doc/plans/2026-10-20-copilot-qualification-v47.json)
reports20/20 Product cells,154 assertions and7/7 protocol cases,46
assertions against frozen normal installed source
`c2616370800f5f6df45246b0890ba35a7cd2faab`. The exact qualification
model is `gpt-5.6-luna`. Every passing Product case confirms cleanup.
Original failures remain recorded; automatic behavior retries are
disabled. The route-only authorization repair changes no bound
runtime/profile bytes and does not relabel the frozen campaign.

The ordinary installed remote journey passed saved-token selection,
model discovery, normal UI save/reopen, metadata-only Test, completed
task, accepted receipt and exact19-byte browser download. An open model
catalog caused no additional discovery requests during117seconds. Actual
installed runtime smokes passed on macOS ARM64, macOS x64 under Rosetta
and native Linux x64 on Daytona. Raw token browser entry was not
exercised; encrypted connection creation used the normal API.

The first Daytona file attempt timed out during delayed semantic
dispatch; its precise stalled layer remains unresolved. Independent
ordinary remote delivery and the other19 Product cells passed before one
instrumented recheck passed8/8 with unchanged source,prompt,assertions
and deadlines. No causal latency repair is claimed.

Before the authorization repair, all four final PR heads passed required
verify/E2E and fresh Greptile5/5 reviews with zero unresolved threads.
Current-head CI/review is running for the two repaired heads. Local
focused checks retain129 connection,59 runner,72 shared-eval and88
recovery/workspace passes, plus migration safety and token gates. The
additive Copilot migration is361; released master history is preserved.

[Cloud normal-package
run39414239704](https://github.com/paperclipai/paperclip/actions/runs/39414239704)
builds the pre-security source4f3f. All four native producers and Linux
package assembly pass; the final clean installed consumers are
finishing. All18 retained archives match their hashes;37,863 regular
files contain zero selected credentials. That package does not contain
the later route repair. The actual final published package must include
it and pass the local/Daytona live canaries. The frozen Daytona image
remains published and independently verified. No separate full-image
scan is claimed. Docker builds run in the cloud.

**Remaining:** current-head CI/review, setup and qualification merges,
final publication, and ordinary local/Daytona canaries against the
actual shipped package. The user explicitly authorizes the maintainer
review override; CI remains a merge gate. No production deployment is
claimed. Provider USD remains unknown and GitHub-attributed.

## Risks

An omitted or mismatched image manifest prevents ordinary Daytona
startup. Final normal archives must contain the matching image authority
and platform daemons. The completed runtime campaign remains bound to
its original source; the final UI-only repair needs its own
normal-install setup proof. Credit notices are not measured USD
receipts. Native steering, native plan mode and detached work remain
outside this release.

## Model Used

OpenAI Codex, based on GPT-6. The exact serving model ID and context
window are not exposed in this task. Used for code changes, review and
tool-assisted verification.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-10-20 07:33:14 -05:00

196 lines
16 KiB
TypeScript

/** Optional published-install lane. No production hook or admission override. */
import { constants } from "node:fs";
import { lstat, mkdir, mkdtemp, open, realpath, rm } from "node:fs/promises";
import { createHash } from "node:crypto";
import { execFileSync } from "node:child_process";
import { findPackageJSON } from "node:module";
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
import { tmpdir } from "node:os";
import { pathToFileURL } from "node:url";
import { CREDENTIAL_NAMES, type MatrixExecution } from "./types.js";
export const installedCliKeys = ["PAPERCLIP_RUNNER_E2E_INSTALLED_CLI", "PAPERCLIP_RUNNER_E2E_INSTALLED_CLI_SHA256", "PAPERCLIP_RUNNER_E2E_INSTALLED_SERVER_ROOT", "PAPERCLIP_RUNNER_E2E_INSTALLED_SERVER_SHA256"] as const;
const overrideKeys = ["PAPERCLIP_RUNNER_BINARY", "PAPERCLIP_RUNNER_REMOTE_BINARY_PATH", "PAPERCLIP_RUNNER_REMOTE_PROVIDER_PACK_PATH", "PAPERCLIP_RUNNER_ACPX_QUALIFICATION", "PAPERCLIP_ACPX_BUILTIN_ROOT", "PAPERCLIP_ACPX_PROVIDER_PACKAGE_ROOT", "PAPERCLIP_ACPX_PROVIDER_PACKAGE_MANIFEST"] as const;
const suites = new Set(["extended-harnesses", "pi-native", "pi-controls", "rich-acp-warm-continuity"]);
const installedCodexCells = new Set([
"first-task.runner-codex.local.task-card-accept",
"everyday-workflows.runner-codex.local.build-revise",
"agent-chat.runner-codex.local.continuity-restart",
]);
const installedCopilotCells = new Set([
"copilot-protection.runner-acpx-copilot.daytona.attached-async-settlement",
"copilot-protection.runner-acpx-copilot.daytona.native-permission-deny-write",
"copilot-protection.runner-acpx-copilot.local.attached-async-settlement",
"copilot-protection.runner-acpx-copilot.local.native-permission-deny-write",
"copilot-provider-death.runner-acpx-copilot.daytona.pending-permission-provider-death",
"copilot-provider-death.runner-acpx-copilot.local.pending-permission-provider-death",
"extended-harnesses.runner-acpx-copilot.daytona.file-edit-validate",
"extended-harnesses.runner-acpx-copilot.daytona.hello-complete",
"extended-harnesses.runner-acpx-copilot.daytona.plan-approve-complete",
"extended-harnesses.runner-acpx-copilot.daytona.question-resume-complete",
"extended-harnesses.runner-acpx-copilot.daytona.structured-question-restart-resume",
"extended-harnesses.runner-acpx-copilot.local.file-edit-validate",
"extended-harnesses.runner-acpx-copilot.local.hello-complete",
"extended-harnesses.runner-acpx-copilot.local.plan-approve-complete",
"extended-harnesses.runner-acpx-copilot.local.question-resume-complete",
"extended-harnesses.runner-acpx-copilot.local.structured-question-restart-resume",
"native-active-stop.runner-acpx-copilot.daytona.pending-permission-stop",
"native-active-stop.runner-acpx-copilot.local.pending-permission-stop",
"rich-acp-warm-continuity.runner-acpx-copilot.daytona.warm-three-turn",
"rich-acp-warm-continuity.runner-acpx-copilot.local.warm-three-turn",
]);
export function usesInstalledCli(env: NodeJS.ProcessEnv): boolean {
return installedCliKeys.some(key => env[key] !== undefined);
}
export function assertInstalledCliSelection(env: NodeJS.ProcessEnv, executions: readonly MatrixExecution[]): void {
if (!usesInstalledCli(env)) return;
if (!installedCliKeys.every(key => Boolean(env[key])) || executions.length === 0 || executions.some(e => !(
(e.profile.id === "runner-acpx-pi" && e.profile.qualificationCandidate === "pi" && e.suite.manualOnly && suites.has(e.suite.id)) ||
(e.profile.id === "runner-codex" && e.profile.provider === "codex" && e.environment.id === "local" && installedCodexCells.has(e.id)) ||
(e.profile.id === "runner-acpx-copilot" && e.profile.qualificationCandidate === "copilot" && e.suite.manualOnly && installedCopilotCells.has(e.id))
))) throw new Error("Installed CLI proof requires complete pins and explicit supported Pi, Codex, or Copilot cases");
for (const key of overrideKeys) if (env[key] !== undefined) throw new Error(`Installed CLI proof forbids ${key}`);
if (env.NODE_OPTIONS !== undefined || env.NODE_PATH !== undefined) throw new Error("Installed CLI proof forbids ambient Node injection");
}
async function readOwned(path: string, maximum: number): Promise<Buffer> {
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
try {
const before = await handle.stat({ bigint: true });
if (!before.isFile() || before.size < 1n || before.size > BigInt(maximum)) throw new Error("Installed CLI proof requires a bounded regular file");
const bytes = await handle.readFile(); const after = await handle.stat({ bigint: true }); const named = await lstat(path, { bigint: true });
if (before.dev !== after.dev || before.ino !== after.ino || before.size !== after.size || before.mtimeNs !== after.mtimeNs || before.ctimeNs !== after.ctimeNs || named.dev !== before.dev || named.ino !== before.ino || named.isSymbolicLink() || bytes.length !== Number(before.size)) throw new Error("Installed CLI proof file changed during admission");
return bytes;
} finally { await handle.close(); }
}
export async function verifyInstalledCli(env: NodeJS.ProcessEnv, executions: readonly MatrixExecution[]) {
if (!usesInstalledCli(env)) return undefined;
assertInstalledCliSelection(env, executions);
const entry = env.PAPERCLIP_RUNNER_E2E_INSTALLED_CLI!;
const serverRoot = env.PAPERCLIP_RUNNER_E2E_INSTALLED_SERVER_ROOT!;
for (const path of [entry, serverRoot]) if (!isAbsolute(path) || resolve(path) !== path || await realpath(path) !== path) throw new Error("Installed CLI proof paths must be canonical and unlinked");
if (!entry.endsWith("/dist/index.js")) throw new Error("Installed CLI proof requires published dist/index.js");
const cliRoot = resolve(dirname(entry), "..");
const cli = JSON.parse((await readOwned(join(cliRoot, "package.json"), 65536)).toString());
const manifest = findPackageJSON("@paperclipai/server", pathToFileURL(entry));
if (!manifest || await realpath(manifest) !== join(serverRoot, "package.json")) throw new Error("Installed CLI resolved a foreign server dependency");
const server = JSON.parse((await readOwned(join(serverRoot, "package.json"), 65536)).toString());
if (cli.name !== "paperclipai" || cli.bin?.paperclipai !== "./dist/index.js" || server.name !== "@paperclipai/server" || typeof cli.version !== "string" || cli.version !== server.version || server.exports?.["."]?.import !== "./dist/index.js") throw new Error("Installed CLI/server public package identity differs");
const serverEntry = join(serverRoot, "dist/index.js");
if (await realpath(serverEntry) !== serverEntry) throw new Error("Installed server entrypoint escapes its package");
const digest = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex");
const cliSha256 = digest(await readOwned(entry, 64 * 1024 * 1024));
const serverSha256 = digest(await readOwned(serverEntry, 16 * 1024 * 1024));
if (cliSha256 !== env.PAPERCLIP_RUNNER_E2E_INSTALLED_CLI_SHA256 || serverSha256 !== env.PAPERCLIP_RUNNER_E2E_INSTALLED_SERVER_SHA256) throw new Error("Installed CLI/server bytes differ from their reviewed pins");
return { schema: "paperclip.e2e.installed-cli-admission/v1", entry, cliRoot, cliSha256, serverRoot, serverEntry, serverSha256, version: cli.version, defaultRuntimeResolution: true, qualificationOverride: false };
}
export async function probeInstalledCodexStartup(input: {
consumerRoot: string;
serverRoot: string;
expectedRunnerSha256: string;
timeoutMs?: number;
}) {
for (const key of [...overrideKeys, "NODE_OPTIONS", "NODE_PATH"]) {
if (process.env[key] !== undefined) throw new Error(`Installed Codex startup forbids ${key}`);
}
const inside = (root: string, filename: string) => {
const name = relative(root, filename);
return name !== "" && name !== ".." && !name.startsWith(`..${sep}`) && !isAbsolute(name);
};
for (const filename of [input.consumerRoot, input.serverRoot]) {
if (!isAbsolute(filename) || resolve(filename) !== filename || await realpath(filename) !== filename) throw new Error("Installed Codex startup paths must be canonical and unlinked");
}
if (!inside(join(input.consumerRoot, "node_modules"), input.serverRoot) || !/^[a-f0-9]{64}$/.test(input.expectedRunnerSha256)) throw new Error("Installed Codex startup requires a contained server and reviewed daemon digest");
const manifest = JSON.parse((await readOwned(join(input.serverRoot, "package.json"), 65536)).toString());
if (manifest.name !== "@paperclipai/server" || manifest.exports?.["."]?.import !== "./dist/index.js") throw new Error("Installed Codex startup requires the published server");
const vendorRoot = join(input.serverRoot, "dist/vendor/paperclip-runner");
const entries = [join(vendorRoot, "index.js"), join(input.serverRoot, "dist/services/native-runtime/native-codex-runner.js")];
for (const filename of entries) if (await realpath(filename) !== filename) throw new Error("Installed Codex startup runtime escapes its package");
const [runtime, production] = await Promise.all(entries.map(filename => import(pathToFileURL(filename).href)));
const runnerBinary = await realpath(production.resolvePaperclipRunnerBinary());
if (runnerBinary !== join(vendorRoot, "bin", `${process.platform}-${process.arch}`, "paperclip-runnerd")) throw new Error("Installed Codex startup requires the normal platform daemon; no source or flat fallback");
const digest = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex");
const runnerSha256 = digest(await readOwned(runnerBinary, 512 * 1024 * 1024));
if (runnerSha256 !== input.expectedRunnerSha256) throw new Error("Installed Codex startup daemon differs from its reviewed pin");
const directory = await realpath(await mkdtemp(join(tmpdir(), "pc-codex-startup-")));
const home = join(directory, "home"), codexHome = join(directory, "codex-home"), workspace = join(directory, "workspace");
for (const filename of [home, codexHome, workspace]) await mkdir(filename, { mode: 0o700 });
const environment = { PATH: `${dirname(process.execPath)}:/usr/bin:/bin:/usr/sbin`, HOME: home, CODEX_HOME: codexHome,
TMPDIR: directory, LC_ALL: "C", TZ: "UTC", PAPERCLIP_RUNNER_NETWORK_ACCESS: "disabled" };
let session: any;
let bundle: any;
let cleanupConfirmed = false;
let startupFailed = false;
let startupFailure: unknown;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(new Error("Installed Codex startup timed out")), Math.max(1, Math.min(input.timeoutMs ?? 45000, 120000)));
try {
const codexCommand = await realpath(runtime.resolveCodexCommand(undefined, environment, workspace));
if (!inside(join(input.consumerRoot, "node_modules"), codexCommand)) throw new Error("Installed Codex startup resolved an ambient Codex executable");
const codexSha256 = digest(await readOwned(codexCommand, 512 * 1024 * 1024));
const codexVersion = execFileSync(codexCommand, ["--version"], { cwd: workspace, env: environment, encoding: "utf8", timeout: 10000, maxBuffer: 16384 }).trim();
if (!codexVersion || codexVersion.length > 200) throw new Error("Installed Codex startup returned no bounded version identity");
const args = runtime.createIsolatedCodexAppServerArgs(environment);
args.splice(args.indexOf("app-server"), 0, "-c", 'cli_auth_credentials_store="file"');
bundle = runtime.createRunnerdCodexTransport({ provider: "codex", stateDirectory: directory, environment,
sourceCodexHome: "", runnerBinary, codexCommand, codexArgs: args, closeGraceMs: 10000,
lifecyclePolicy: { mode: "per_turn", idleTimeoutMs: null } });
const driver = new runtime.CodexAppServerDriver({
taskEnvelope: { schema: "paperclip.skillless_task.v1", objective: "Credential-free installed startup proof.",
completionContract: { revision: "installed-startup-v1", criteria: [] }, constraints: [], expectedResultSchema: "paperclip.run_result.v1" },
model: "gpt-5.6-sol", conversationMode: "direct", requestedCollaborationMode: "plan", approvalPolicy: "never",
includeCollaborationModeInstructions: false, dynamicTools: [], environment,
transportFactory: () => bundle.transport, requireProviderSessionIdentity: true,
});
session = await driver.openSession({ runId: "installed-startup", normalizedSessionId: "installed-startup", workingDirectory: workspace, signal: controller.signal });
const event = await session.events()[Symbol.asyncIterator]().next();
const identity = session.ids();
const evidence = bundle.evidence();
if (event.value?.eventType !== "session.started" || event.value.payload?.context?.model !== "gpt-5.6-sol" ||
event.value.payload?.context?.sandbox?.permissionProfile?.id !== "paperclip-runner-workspace-read-only" ||
!identity.driverSessionId || !identity.providerSessionId || evidence.providerDriver !== "codex_app_server" ||
!(evidence.runnerPid > 0) || !(evidence.codexPid > 0)) throw new Error("Installed Codex startup returned no matching native identity or read-only model context");
await session.close({ reason: "credential-free installed startup complete" });
session = undefined;
if (!bundle.evidence().runnerExited) throw new Error("Installed Codex startup cleanup is incomplete");
cleanupConfirmed = true;
return { schema: "paperclip.e2e.installed-codex-startup/v1", platform: `${process.platform}-${process.arch}`,
runnerSha256, codexSha256, codexVersion, providerDriver: "codex_app_server", model: "gpt-5.6-sol",
nativeSessionStarted: true, cleanupConfirmed: true, providerTurns: 0, credentialsUsed: false,
defaultRuntimeResolution: true, reasoningEffortExercised: false };
} catch (error) {
startupFailed = true;
startupFailure = error;
throw error;
} finally {
clearTimeout(timer);
const cleanupFailures: unknown[] = [];
try {
if (session) await session.close({ reason: "installed startup cleanup" });
else if (bundle && !cleanupConfirmed) await bundle.transport.close("installed startup cleanup");
} catch (error) { cleanupFailures.push(error); }
try {
if (!bundle || bundle.evidence().runnerExited) await rm(directory, { recursive: true, force: true });
} catch (error) { cleanupFailures.push(error); }
if (startupFailed) {
const evidence = bundle?.evidence();
const bounded = (value: unknown) => String(value).replaceAll(directory, "[private-startup]").slice(0, 2000);
const primaryMessage = bounded(startupFailure instanceof Error ? startupFailure.message : startupFailure);
const diagnostic = { schema: "paperclip.e2e.installed-codex-startup-failure/v1", platform: `${process.platform}-${process.arch}`,
runnerExited: evidence?.runnerExited ?? null, runnerExitCode: evidence?.runnerExitCode ?? null,
runnerSignal: evidence?.runnerSignal ?? null,
diagnostics: (Array.isArray(evidence?.diagnostics) ? evidence.diagnostics : []).slice(-16).map(bounded),
cleanupFailures: cleanupFailures.map(error => bounded(error instanceof Error ? error.message : error)) };
throw new AggregateError([startupFailure, ...cleanupFailures], `Installed Codex startup failed: ${primaryMessage}\n${JSON.stringify(diagnostic)}`, { cause: startupFailure });
}
if (cleanupFailures.length) throw new AggregateError(cleanupFailures, "Installed Codex startup cleanup failed", { cause: cleanupFailures[0] });
}
}
/** This prep lane cannot create agents or read the normal local credential file. */
export function assertInstalledStartupOnly(env: NodeJS.ProcessEnv, executions: readonly MatrixExecution[], options: { ids: string[]; maxParallel: number; maxAutomaticRetries: number; headed: boolean; ui: boolean; debug: boolean; list: boolean; matrixJson: boolean }) {
assertInstalledCliSelection(env, executions);
if (!usesInstalledCli(env) || executions.length !== 1 || executions[0]?.id !== "extended-harnesses.runner-acpx-pi.local.hello-complete" || options.ids.length !== 1 || options.ids[0] !== executions[0]?.id || options.maxParallel !== 1 || options.maxAutomaticRetries !== 0 || options.headed || options.ui || options.debug || options.list || options.matrixJson) throw new Error("Installed startup-only requires one explicit local Pi hello, no retries or interactive mode");
if (CREDENTIAL_NAMES.some(key => env[key] !== undefined) || Object.keys(env).some(key => /^(?:OPENAI|ANTHROPIC|OPENROUTER|DAYTONA|XAI|GROK|CURSOR|COPILOT|GITHUB|GH)(?:_|$)/.test(key) && env[key] !== undefined)) throw new Error("Installed startup-only forbids provider credential inputs");
}