feat: integrate Pi 1.0 into the experimental Runner (#14921)

## Thinking Path

> - Paperclip manages AI agents and their work.
> - The experimental Runner owns provider processes and durable
sessions.
> - Pi needs working task execution and human controls.
> - The five-PR stack must preserve changes already on master.
> - Each layer now carries the complete integrated source for a safe
sequential fallback.
> - This PR belongs to native GitHub stack #15602, ending at #14956.

## Linked Issues or Issue Description

Refs #14436, #14631, #14743 and #14956.

Ship Pi 1.0 through the experimental Paperclip Runner. The five PRs are
#14921, #14922, #14923, #14924 and #14956. The user authorized the
complete merge after checks pass. Existing `pi_local` execution is
unchanged. Accounting and wider provider/platform qualification remain
deferred.

## What Changed

- Recover missing final replies after workspace finalization changes
owners, using accepted-turn evidence without rerunning work or granting
external-chat publication.
- Preserve the admitted Pi instruction root across warm runs, while
retaining changed-root rejection.
- Give Pi a bounded 15-second default shutdown grace so stop, drain
acknowledgement and durable suspension can complete. Explicit deadlines
and other providers retain their existing behavior.
- Integrate the Pi 1.0 runtime and master contracts.
- Use Pi profile 22. Preserve explicit caller-selected models and exact
native thinking levels. Keep Pi's wrapper, helper, extension and
question/control behavior unchanged from the qualified profile-19
runtime.
- Preserve master's Dot lifecycle and consent fields, configured task
environment, status guards and current Codex/Claude dependency versions.
Cursor stays qualified. Copilot stays pending; profile 17 binds the
changed shared protocol validation sources.
- Exclude general AWS IAM credentials from Pi static/custom provider
bindings and selected task projections; preserve the provider-scoped
Bedrock bearer key. Profile 21 is retained as historical provenance.
Rust and cloud install probes use the current declaration.
- Patch bundled brace-expansion 5.0.9 to the exact official 5.0.12
payload. Pin the patch and complete runtime closures. Include the patch
in normal installed setup tooling. Keep the upstream Pi shrinkwrap as
provenance and permit only this exact security correction.
- Include current attestation files in the Docker build context. Keep
the repository lockfile unchanged from master. CI and private image
builds resolve manifest changes before their frozen installation.

## Verification

- Full local `pnpm -r typecheck` passes, including Runner Rust, server
and UI. Focused integration checks pass: 194 Runner
admission/environment tests, 63 profile/credential tests with one
expected skip, 152 Dot/UI configuration tests, and Pi transcript/notice
tests.
- Full local `pnpm build` passes on the final source.
- Fresh final-source checks pass: all 698 Rust workspace tests (32
binaries), 156 credential/profile/controller tests with one expected
skip, Runner TypeScript typecheck, and 20 package/setup/sandbox tests.
- The profile-21 Pi materializer passes on the native host with the
official pinned Node 24.21.0 and its npm. It verifies all 150 locked
packages, the patched dependency and the exact closure. Setup/package
bundle tests and UI token gates pass.
- The old hashes were reproduced for all three supported targets before
calculating the patched graph. New closure hashes are darwin-arm64
`282022db10150c6632b3444df421342e7d534bdf5d5fb1097a2e79d0625a2bcf`,
darwin-x64
`64e251e19009f755c0b04f73ce2138246faab71a961b0f13d75ebfcc34bef12e`, and
linux-x64
`713b1fdff42fb56a1518bdc084f181d70bee8ebadc3e4b1d76321ed9108c8410`.
Independent native platform execution is separate from graph identity
reproduction.
- Historical cloud qualification remains unchanged: all seven core cases
pass on shipping source `10dc43c9ec65d88c2f782d62afb296d09494f215`,
harness `1a4408a48cfb5a1f094a311141c257c92cd7a893`, image
`sha256:5b3a775b383591bda1b0c1889e509acc70ce7f37c53f09733c81d59037f02280`,
and accepted Sonnet 4.6/low fixture. All 215 canonical files and all
seven cleanup checks pass independent verification. These are profile-19
results and are not relabeled as fresh profile-22 runs.
- Current Pi digest:
`sha256:e92078bee3c23bec4100aa589013a44613d054cd686826534025d8019e9f39a9`.
[The readiness
plan](https://github.com/paperclipai/paperclip/blob/codex/pi-production-readiness/doc/plans/2026-10-02-pi-production-readiness.md)
preserves campaign and failed-attempt provenance.
- Merge only after every PR's current-head CI and fresh review pass.
Linux CI covers the full suites, build and browser tests. The local
embedded Postgres API-authority suite cannot start on this macOS/Node 26
host, so Linux CI must confirm that suite.

### Fresh profile-22 core qualification — 2026-10-08

All seven accepted core cases pass canonically on Pi profile 22, with
`openrouter/anthropic/claude-sonnet-4.6` and native-confirmed low
thinking. This model is a fixture; production accepts the caller's
explicit Pi provider/model.

Runtime/install source: `3241a992f2a7703e59e97ed0fd3e5d6405de4401`.
Frozen accepted harness: `1a4408a48cfb5a1f094a311141c257c92cd7a893`.
Immutable cloud image:
`ghcr.io/paperclipai/paperclip-daytona-runner@sha256:506f22db7edd78f37c0c40bec1cc084af1850455026dbf467194bfbb8fcef141`.
Pi digest:
`sha256:e92078bee3c23bec4100aa589013a44613d054cd686826534025d8019e9f39a9`.

[Hosted Linux image and clean-install
verification](https://github.com/paperclipai/paperclip/actions/runs/37868328023)
passes, including all 20 source-bound archives, normal CLI/Pi setup,
companion import and the production pack reader. This exact installation
source includes the latest master integration and the corrected Pi warm
instruction-root fence. Full local typecheck/build and current-head
hosted CI verify the final stack. All 13 focused real-root regressions
pass. The full local executor suite passed 662 tests; 15 database tests
could not start the Mac embedded PostgreSQL service. Hosted Linux CI
passes the full required verification and E2E checks. These fresh
results keep their own source identity; profile-19 results remain
historical.

| Core path | Canonical campaign | Retained archive SHA-256 |
| --- | --- | --- |
| File edit, validation, download and Done |
`pi-core22-replyfix-0-1791511228` | 23 files;
`a473e8603a3dd4737863291f8d3d1e392391f0b16d433c3e0e0e9d8baf7a97b0` |
| Pending question and controller restart |
`pi-core22-replyfix-1-1791511376` | 33 files;
`6b829c4eb74e1f32a89c692a4ae7130dbfc1c6d3cf13915effe2103d9e242c8e` |
| Three-turn session/process/workspace continuity |
`pi-core22-replyfix-2-1791511587` | 23 files;
`7a87021f8f9a3fdd3c58bb4467f8d82c635e3ea4795d6e75f144d9aa14818df8` |
| Four typed questions and browser reconnects |
`pi-core22-replyfix-3-1791511881` | 42 files;
`9e31755252be1f4f9cb0626c984c142d4d1ae5f5bee3a7af08444db8d12c280a` |
| Plan approval and completion | `pi-core22-replyfix-4-1791512031` | 22
files;
`a0383ce1aab38e7b5a25ce0e9dd3bebea5c037ebd96ae6b29dae19015da2ae2c` |
| Same-turn steering and permission denial |
`pi-core22-replyfix-5-1791512261` | 39 files;
`c929b8c7070f0b66aedc17e65ca46e6beab1e363926ac9f7e2a75fb250f05949` |
| Stop during pending permission | `pi-core22-replyfix-6-1791512390` |
33 files;
`7f0a58ae0f4d5bfc76149435f4e322537089c5bd16e7ffe9b5ad71f10a621a07` |

All 215 canonical files (28714587 bytes) are independently
hash-verified. All seven cleanup grades pass, with no owned runtime
process or temporary root after each case. Automatic retries are zero.
The owned cloud host stopped normally after retention. The prior
profile-22 warm attempt remains failed and separately retained: archive
SHA-256
`1e54eba5ec72b50cee1534b23d1d1d4f21a090006b8a64501ba70db972abfde5`. Its
original canonical classification is preserved. Diagnosis reproduced a
product bug comparing an agent-files root against an unset
checkpoint-only field. The fix stores the admitted physical root
separately from the adopted per-run collection capability. The real-root
regression fails before the fix and passes afterward, including
rejection of a changed physical root. Fixture, grader, model and all
seven accepted case IDs are unchanged; this fresh campaign tests
final-reply publication after file registration first. The intermediate
restart attempt also remains failed and retained: archive SHA-256
`5dcaefdf1d17cf4cd54fd4cf810f45e736667392339b8ce7caf08bb4e225277f`. Its
original canonical classification is preserved. Pi resumed, wrote the
verified answer and completed its task; exact runner suspension was
proven, but idle stop consumed about 5.2s and left under 3s for the
drain acknowledgement. The Pi-only default shutdown grace is now 15s,
preserving a full 5s drain round trip and a finite suspension reserve.
Explicit caller deadlines, other provider defaults, literal drain
receipts and exact suspension identity checks remain unchanged. The
timing regression fails before this correction and passes afterward; all
18 focused settlement tests and Runner typecheck pass. The final-source
file attempt is also preserved as failed (`candidate_failure`), archive
SHA-256
`db6767b6773ea618997927ac77bdb005a5ac81492c7b9c0ffbc900449f829bc9`.
Native edit, validation, exact downloadable artifact and Done/succeeded
all passed, and the exact final reply was durably recorded. A workspace
recovery owner completed before the live heartbeat reached presentation,
leaving that reply absent from task chat. Recovery now materializes only
a completed final reply from the accepted turn of an ordinary internal
Done task, preserving issue/run/contract binding, suppression,
external-chat authorization and same-run deduplication. The database
regression covers the generated file-preparation receipt, suppression,
unapproved external continuation and replay. Server typecheck and all 49
response-selection tests pass; hosted Linux verifies the database
regression because embedded PostgreSQL cannot start on this Mac.

The delayed-final-answer database regression passes on [the final
root-source Linux server
shard](https://github.com/paperclipai/paperclip/actions/runs/37868262553/job/113628594152),
alongside 1,108 passing tests. The first root Runner shard had one
unchanged durable-resume test exceed its 5-second timeout; the identical
top-source shard and the isolated exact test passed. One rerun of that
failed job and its required aggregate passed without source or test
changes. The original failed job log and the single-rerun receipt remain
retained.

### October 9 merge verification

Current merge head: `5a8fe63512a7166aaef5cf50065a25008aa8b44b`. All
current-head checks pass, including `ci / verify` and `ci / e2e`;
exact-head Greptile review is 5/5 with no unresolved threads. Current
master conflicts are resolved. The user authorized the maintainer
override of the code-owner review gate after these checks. The seven
retained live core cases remain bound to source
`3241a992f2a7703e59e97ed0fd3e5d6405de4401` and its recorded cloud image.

## Risks

- The security correction changes the dependency closure and profile
identity. Old sessions must reopen on the new profile. Exact identities
and credential bindings fail closed.
- The runner remains experimental and requires explicit selection.
Legacy Pi Local is unchanged. Caller model IDs pass through; the E2E
model is a fixture.
- Accounting and the broad platform/provider matrix remain deferred.
This merge does not publish a release or deploy a service.

## Model Used

OpenAI GPT-6 through Codex assisted with reasoning, repository
inspection, editing and tool use. The exact serving ID and context
window are not exposed in this session. Final live qualification uses Pi
1.0.0 with `openrouter/anthropic/claude-sonnet-4.6` and native-confirmed
low thinking.

## 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
#` 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
- [x] All Paperclip CI gates are green
- [x] 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>
This commit is contained in:
DottaandPaperclip authored and GitHub committed 2026-10-09 08:56:41 -05:00
1 parent 4fb1ab53a8
commit 57e977be72
517 files changed
+61737 -1440

No files matched your search

@@ -8,7 +8,7 @@ const provider = { kind: "acpx", agent: "copilot", model: "exact-model", permiss
const authorize = (value: unknown) => ({ [ACPX_QUALIFICATION_ENV]: JSON.stringify(value) });
describe("host ACPX qualification admission", () => {
afterEach(() => vi.unstubAllEnvs());
it.each(["copilot", "pi"])("admits %s through agent validation and native input only for the exact host pair", (agent) => {
it.each(["copilot"])("admits %s through agent validation and native input only for the exact host pair", (agent) => {
const config = { provider: "acpx", acpxAgent: agent, model: "exact-model" };
vi.stubEnv(ACPX_QUALIFICATION_ENV, undefined);
expect(() => resolvePaperclipRunnerProviderProfile(config)).toThrow(expect.objectContaining({ code: "paperclip_runner_acpx_agent_unavailable" }));
@@ -19,6 +19,21 @@ describe("host ACPX qualification admission", () => {
expect(() => resolvePaperclipRunnerProviderProfile({ ...config, model: "other-model" })).toThrow(expect.objectContaining({ code: "paperclip_runner_acpx_qualification_invalid" }));
expect(() => resolvePaperclipRunnerProviderProfile({ ...config, model: "" })).toThrow(expect.objectContaining({ code: "paperclip_runner_acpx_model_required" }));
});
it("admits only the exact Pi model without host authorization and preserves permission modes", () => {
const config = { provider: "acpx", acpxAgent: "pi", model: "openrouter/deepseek/deepseek-v4-flash-0731" };
for (const authorization of [undefined, "malformed unrelated candidate authorization"]) {
vi.stubEnv(ACPX_QUALIFICATION_ENV, authorization);
expect(resolvePaperclipRunnerProviderProfile(config)).toMatchObject(config);
for (const acpxPermissionMode of [undefined, "approve-all", "approve-paperclip", "approve-reads", "deny-all"]) {
expect(resolvePaperclipRunnerNativeProviderInput({ backend: "acpx_runtime", adapterConfig: { ...config, acpxPermissionMode } }))
.toMatchObject({ ...config, acpxPermissionMode: acpxPermissionMode ?? "approve-all" });
}
for (const model of [undefined, ""]) {
expect(() => resolvePaperclipRunnerProviderProfile({ ...config, model }))
.toThrow(expect.objectContaining({ code: "paperclip_runner_acpx_model_required" }));
}
}
});
it("keeps normal candidate execution closed and admits only the exact operator pair", () => {
expect(resolveAcpxQualification(provider, {})).toBeUndefined();
expect(resolveAcpxQualification(provider, authorize([{ agent: "copilot", model: "exact-model" }]))).toBe("copilot");
@@ -43,7 +58,7 @@ describe("host ACPX qualification admission", () => {
it("does not alter existing qualified providers", () => {
expect(resolveAcpxQualification({ ...provider, agent: "codex" } as typeof provider, authorize([]))).toBeUndefined();
});
it.each(["claude", "codex", "grok", "cursor"])("keeps %s model selection open to native verification", (acpxAgent) => {
it.each(["claude", "codex", "grok", "cursor", "pi"])("keeps %s model selection open to native verification", (acpxAgent) => {
const adapterConfig = { provider: "acpx", acpxAgent, model: "explicit-new-model" };
expect(resolvePaperclipRunnerProviderProfile(adapterConfig))
.toMatchObject({ acpxAgent, model: "explicit-new-model" });
@@ -68,3 +83,9 @@ describe("host ACPX qualification admission", () => {
expect(source).not.toContain("resolveAcpxQualification(input.execution.provider, effectiveRunnerEnvironment)");
});
});
it.each([undefined, "off", "low", "high", "max"] as const)("validates saved Pi thinking %s and projects it explicitly", piThinkingLevel => {
const adapterConfig = { provider: "acpx", acpxAgent: "pi", model: "openrouter/deepseek/deepseek-v4-flash-0731", piThinkingLevel };
expect(resolvePaperclipRunnerNativeProviderInput({ backend: "acpx_runtime", adapterConfig })).toMatchObject({ piThinkingLevel: piThinkingLevel ?? "low" });
for (const alias of ["medium", "minimal", "xhigh"]) expect(() => resolvePaperclipRunnerProviderProfile({ ...adapterConfig, piThinkingLevel: alias })).toThrow(expect.objectContaining({ code: "paperclip_runner_pi_thinking_invalid" }));
});
@@ -3,6 +3,13 @@ import { inheritNativeRunnerAdapterConfig } from "./native-agent-runtime-inherit
import { resolvePaperclipRunnerProviderProfile } from "./provider-profile.js";
describe("native hire runtime inheritance", () => {
it("preserves Cursor's selected mode without its live session or credentials", () => {
expect(inheritNativeRunnerAdapterConfig({
provider: "acpx", acpxAgent: "cursor", acpxSessionMode: "plan", model: "exact-cursor-model",
runtimeSessionId: "parent-session", env: { CURSOR_API_KEY: "parent-secret" },
})).toEqual({ provider: "acpx", acpxAgent: "cursor", acpxSessionMode: "plan", model: "exact-cursor-model" });
});
it.each([
["claude_managed", "managedProfileId", "managedAgentsRetentionAcknowledged"],
["aws_agentcore", "agentCoreProfileId", "agentCoreRetentionAcknowledged"],
@@ -40,3 +47,7 @@ describe("native hire runtime inheritance", () => {
.not.toHaveProperty("managedProfileId");
});
});
it("preserves Pi thinking configuration without live identity or credentials", () => {
expect(inheritNativeRunnerAdapterConfig({ provider: "acpx", acpxAgent: "pi", piThinkingLevel: "low", runtimeSessionId: "prior", env: { OPENROUTER_API_KEY: "canary" } })).toEqual({ provider: "acpx", acpxAgent: "pi", piThinkingLevel: "low" });
});
@@ -14,6 +14,7 @@ export const INHERITABLE_NATIVE_RUNNER_CONFIG_KEYS = [
"opencodePermissionMode",
"acpxPermissionMode",
"acpxSessionMode",
"piThinkingLevel",
"lifecycleMode",
"modelReasoningEffort",
"maxIterations",
@@ -12,6 +12,23 @@ describe("explicit file output requirements", () => {
"Make a file but do not send it to anyone else.",
"Export a summary of this PDF as CSV.",
"Create no temporary files; export the results as CSV.",
"Write report.pdf. This is an internal verification file, not a deliverable. Also export the results as CSV.",
"Write a downloadable report.pdf. This is personal memory, not a task deliverable.",
"Attempt native write to report.txt with content requested.",
"Write report.pdf and checklist.md. This is an internal assertion file, not a deliverable.",
"Write report.pdf; write internal-proof.txt. This is an internal verification file, not a deliverable.",
"Write report.pdf and attempt native write to /outside/probe.txt; this negative test must be denied.",
"Attempt native write to /outside/probe.json and write report.pdf; this negative test must be denied.",
"Write report.txt and attach it. This is an internal verification file, not a deliverable.",
"Attach it.",
"Write report.txt and return it as an attachment. This is an internal verification file, not a deliverable.",
"Write report.txt and send it to me. This is an internal verification file, not a deliverable.",
"Write report.txt. This is an internal verification file, not a deliverable. Send it to me.",
"Write report.txt and provide it to me. This is an internal verification file, not a deliverable.",
"Write report.txt and give me it. This is an internal verification file, not a deliverable.",
"Write report.txt and return it. This is an internal verification file, not a deliverable.",
"Write report.txt and send it as an attachment in your response. This is an internal verification file, not a deliverable.",
"Write report.txt. This is an internal verification file, not a deliverable. Send it as a download link in your response.",
])("recognizes an explicit output request: %s", objective => {
expect(explicitlyRequestsFileOutput(objective)).toBe(true);
});
@@ -31,6 +48,20 @@ describe("explicit file output requirements", () => {
"Create no files.",
"Generate no attachments and answer in chat.",
"Write a reply without any files.",
"Use native write/read file tools for this task, not bash or the instructions API.",
"Write a memory entry to memory/pi-native.txt inside the registered AGENT_HOME. This is personal memory, not a task deliverable.",
"Use native write to copy those exact bytes into pi-agent-memory-proof.txt in the task workspace. This is an internal assertion file, not a deliverable.",
"Before finishing, attempt native write exactly once to /outside/pi-unassigned.txt with content forbidden. This intentionally unassigned root must be denied.",
"Write internal-proof.txt; then check it exists. This is an internal verification file, not a deliverable.",
"Attempt native write to /outside/probe.txt and create no files. This negative test must be denied.",
"Write internal-proof.txt; do not attach it. This is an internal verification file, not a deliverable.",
"Write a reply and return it in chat.",
"Write internal-proof.txt and return it in chat. This is an internal verification file, not a deliverable.",
"Write internal-proof.txt and send it in chat. This is an internal verification file, not a deliverable.",
"Write internal-proof.txt. This is an internal verification file, not a deliverable. Return it inline.",
"Write internal-proof.txt and send it as a code block in your response. This is an internal verification file, not a deliverable.",
"Write internal-proof.txt and return it in my reply. This is an internal verification file, not a deliverable.",
"Write internal-proof.txt and provide it as plain text. This is an internal verification file, not a deliverable.",
])("does not require a file for a text or source-review request: %s", objective => {
expect(explicitlyRequestsFileOutput(objective)).toBe(false);
});
@@ -63,26 +63,59 @@ async function hasCurrentPublicationReceipt(db: Db, companyId: string, receipts:
* an output requirement or erase a user's request for a file.
*/
export function explicitlyRequestsFileOutput(objective: string): boolean {
return objective.split(/(?:[.!?](?:\s|$)|\n|[;,]|\bbut\b)/iu).some(clause => {
const sentences = objective.replaceAll("\\_", "_").split(/(?:[.!?](?:\s|$)|\n)/iu);
let precedingFileOutput = false;
return sentences.some((sentence, index) => {
const file = /\b(?:files?|attachments?|downloads?|pdf|spreadsheets?|workbooks?|slide decks?|powerpoints?|docx|xlsx|csv)\b|\b[^\s/]+\.(?:md|txt|pdf|docx?|xlsx?|csv|pptx?|png|jpe?g|svg|zip)\b/giu;
const create = /\b(?:create|make|write|save|export|attach|send|generate|produce|prepare|provide|give|return|build)\b/iu.exec(clause);
if (create && /\b(?:do not|don't|never|no need to)\s*$/iu.test(clause.slice(0, create.index))) return false;
const output = create ? clause.slice(create.index + create[0].length) : "";
const fileObject = [...output.matchAll(file)].some(match => {
const prefix = output.slice(0, match.index);
const suffix = output.slice(match.index + match[0].length);
// "Create no files" is a prohibition, even though it contains a creation
// verb. Negate this object only; another explicit output can still count.
if (/\b(?:no|zero|without(?:\s+any)?)\s+(?:(?:new|temporary|downloadable|attached|additional)\s+)*$/iu.test(prefix)) return false;
// "Write a summary of this PDF" names input, not a requested file.
// Explicit export destinations still count after such input references.
const destination = /\b(?:as|into|to)\s+(?:(?:a|an|the|new|separate|markdown|word|excel)\s+)*$/iu.test(prefix);
if (!destination && /\b(?:of|about|on|from|using|for|with)\b/iu.test(prefix)) return false;
if (/^files?$/iu.test(match[0]) && /^\s+(?:permissions?|systems?|formats?|names?|paths?|types?|sizes?|descriptors?)\b/iu.test(suffix)) return false;
return true;
const outputs = sentence.split(/(?:[;,]|\bbut\b)/iu).flatMap(clause => {
const creates = [...clause.matchAll(/\b(?:create|make|write|save|export|attach|send|generate|produce|prepare|provide|give|return|build)\b/giu)];
return creates.flatMap((create, createIndex) => {
const before = clause.slice(0, create.index);
if (/\b(?:do not|don't|never|no need to)\s*$/iu.test(before)) return [];
// Bind each object to its own verb. A denied write or "create no files"
// cannot suppress a separate requested report in this same clause.
const output = clause.slice(create.index + create[0].length, creates[createIndex + 1]?.index);
const objects = [...output.matchAll(file)].filter(match => {
const prefix = output.slice(0, match.index);
const suffix = output.slice(match.index + match[0].length);
// "Create no files" is a prohibition, even though it contains a creation
// verb. Negate this object only; another explicit output can still count.
if (/\b(?:no|zero|without(?:\s+any)?)\s+(?:(?:new|temporary|downloadable|attached|additional)\s+)*$/iu.test(prefix)) return false;
// "Write a summary of this PDF" names input, not a requested file.
// Explicit export destinations still count after such input references.
const destination = /\b(?:as|into|to)\s+(?:(?:a|an|the|new|separate|markdown|word|excel)\s+)*$/iu.test(prefix);
if (!destination && /\b(?:of|about|on|from|using|for|with)\b/iu.test(prefix)) return false;
if (/^files?$/iu.test(match[0]) && /^\s+(?:tools?|permissions?|systems?|formats?|names?|paths?|types?|sizes?|descriptors?)\b/iu.test(suffix)) return false;
return true;
});
// An explicit attachment/export request can refer to the file by
// pronoun. It still requires publication when its name is omitted.
const referencesOutput = /^(?:attach|export|send|provide|give|return)$/iu.test(create[0])
&& /^\s+(?:me\s+)?(?:it|them|this|that)\b/iu.test(output);
const referencedAttachment = referencesOutput && /^(?:attach|export)$/iu.test(create[0]);
const inline = /\b(?:inline|(?:in|within|inside|as|into)\s+(?:(?:a|an|the|my|your|our|final|plain|markdown|chat|fenced)\s+)*(?:chat|response|reply|message|comment|text|code block)|(?:its|the) contents)\b/iu.test(output);
const downloadable = referencedAttachment || (!/\b(?:no|without)\s+(?:downloadable|attached)/iu.test(output)
&& /\b(?:downloadable|attached)\s+(?:file|report|document|checklist|draft)\b/iu.test(output));
const publication = /\b(?:downloadable|attached|attach|export|send|provide|return|give)\b/iu.test(create[0] + output);
const deniedAttempt = create[0].toLowerCase() === "write" && objects.length === 1
&& /\b(?:attempt|try)\s+(?:the\s+)?native\s*$/iu.test(before)
&& /\b(?:must be denied|denial is (?:the )?expected|(?:this|the) negative test)\b/iu.test(objective);
if (objects.length === 0 && !downloadable && /^\s+(?:no|zero|without)\b/iu.test(output)) return [];
return [{ objects: objects.length, downloadable, publication, deniedAttempt,
referencesOutput, publicationReference: referencesOutput && !inline }];
});
});
// "This ..." qualifies a single requested file in the preceding sentence,
// even when a later clause checks it. Multiple files cannot share this
// exception and separate report requests still require publication.
const internal = outputs.reduce((count, output) => count + output.objects + Number(output.downloadable && output.objects === 0), 0) === 1
&& /^\s*This is (?:an? )?(?:personal memory|internal (?:assertion|verification) file)\b[^.!?]*\bnot a (?:task )?deliverable\b/iu.test(sentences[index + 1] ?? "");
return outputs.some(output => {
const publicationReference = output.publicationReference && precedingFileOutput;
if (!output.referencesOutput) precedingFileOutput = output.objects > 0;
return (output.objects > 0 || output.downloadable || publicationReference)
&& (output.publication || (!internal && !output.deniedAttempt));
});
return fileObject ||
(!/\b(?:no|without)\s+(?:downloadable|attached)/iu.test(clause) && /\b(?:downloadable|attached)\s+(?:file|report|document|checklist|draft)\b/iu.test(clause));
});
}
@@ -585,3 +585,52 @@ describe("native completion references", () => {
expect(input.completionContract.contract).toEqual(args.completionContract.contract);
});
});
describe("Cursor mode native execution projection", () => {
function fixture(acpxSessionMode?: "agent" | "plan" | "ask") {
return {
companyId: "company-1", runId: "run-1", agentId: "agent-1",
issue: { id: "issue-1", identifier: "MODE-1", title: "Review a plan", description: null, workMode: "standard" },
taskPrompt: "Review the project.",
workspace: { id: "workspace-1", cwd: "/workspace", repoUrl: null, repoRef: null, branchName: null },
normalizedSessionId: null, provider: "acpx" as const, acpxAgent: "cursor" as const,
model: "exact-cursor-model", acpxSessionMode, acpxPermissionMode: "deny-all" as const,
completionContract: { id: "contract-1", sha256: `sha256:${"a".repeat(64)}`, schemaVersion: "paperclip.run-result.v1", contract: { revision: "1", objective: "Review", criteria: [{ id: "output", requirement: "Review" }] } },
runtimeContext: nativeRuntimeContextFixture(),
};
}
it.each([undefined, "agent", "plan", "ask"] as const)("preserves mode %s independently of permissions and task planning", mode => {
const result = buildNativeExecutionInput(fixture(mode));
expect(result.provider).toMatchObject({ kind: "acpx", agent: "cursor", mode: mode ?? "agent", permissionMode: "deny-all" });
expect(result.executionMode).toBe("default");
expect(result.task.workMode).toBe("standard");
});
it("rejects a Cursor mode attached to another harness", () => {
expect(() => buildNativeExecutionInput({ ...fixture("plan"), acpxAgent: "copilot" })).toThrow("only for Cursor");
});
});
describe("Pi thinking native execution projection", () => {
function fixture(piThinkingLevel?: "off" | "low" | "high" | "max") {
return {
companyId: "company-1", runId: "run-1", agentId: "agent-1",
issue: { id: "issue-1", identifier: "MODE-1", title: "Review a plan", description: null, workMode: "standard" },
taskPrompt: "Review the project.",
workspace: { id: "workspace-1", cwd: "/workspace", repoUrl: null, repoRef: null, branchName: null },
normalizedSessionId: null, provider: "acpx" as const, acpxAgent: "pi" as const,
model: "openrouter/deepseek/deepseek-v4-flash-0731", piThinkingLevel, acpxPermissionMode: "deny-all" as const,
completionContract: { id: "contract-1", sha256: `sha256:${"a".repeat(64)}`, schemaVersion: "paperclip.run-result.v1", contract: { revision: "1", objective: "Review", criteria: [{ id: "output", requirement: "Review" }] } },
runtimeContext: nativeRuntimeContextFixture(),
};
}
it.each([undefined, "off", "low", "high", "max"] as const)("preserves mode %s independently of permissions and task planning", mode => {
const result = buildNativeExecutionInput(fixture(mode));
expect(result.provider).toMatchObject({ kind: "acpx", agent: "pi", piThinkingLevel: mode ?? "low", permissionMode: "deny-all" });
expect(result.executionMode).toBe("default");
expect(result.task.workMode).toBe("standard");
});
it("rejects a Pi thinking level attached to another harness", () => {
expect(() => buildNativeExecutionInput({ ...fixture("low"), acpxAgent: "copilot" })).toThrow("only for Pi");
});
});
@@ -1,5 +1,5 @@
import type { PaperclipTurnContext } from "@paperclipai/adapter-utils/server-utils";
import { resolvePaperclipRunnerCursorMode } from "@paperclipai/adapter-utils";
import { resolvePaperclipRunnerCursorMode, resolvePaperclipRunnerPiThinkingLevel } from "@paperclipai/adapter-utils";
import { createHash } from "node:crypto";
import { buildNativeContinuationPrompt } from "./native-continuation.js";
import type {
@@ -77,6 +77,7 @@ export interface BuildNativeExecutionInput {
opencodePermissionMode?: NativeOpenCodePermissionMode;
acpxPermissionMode?: NativeAcpxPermissionMode;
acpxSessionMode?: "agent" | "plan" | "ask";
piThinkingLevel?: "off" | "low" | "high" | "max";
model?: string | null;
managedProfile?: Extract<
NativeExecutionInputV5["provider"],
@@ -113,6 +114,7 @@ export function buildNativeExecutionInput(input: BuildNativeExecutionInput): Nat
throw new Error("native_execution_input_invalid: issue work mode must be standard, planning, or ask");
}
const mode = resolvePaperclipRunnerCursorMode(input.provider, input.acpxAgent, input.acpxSessionMode);
const piThinkingLevel = resolvePaperclipRunnerPiThinkingLevel(input.provider, input.acpxAgent, input.piThinkingLevel);
const executionMode = input.executionMode
?? (input.issue.workMode === "planning" ? "plan" : "default");
const acpxProfile = input.provider === "acpx"
@@ -276,6 +278,7 @@ export function buildNativeExecutionInput(input: BuildNativeExecutionInput): Nat
model: input.model,
permissionMode: input.acpxPermissionMode ?? "approve-all",
...(mode === undefined ? {} : { mode }),
...(piThinkingLevel === undefined ? {} : { piThinkingLevel }),
profile: {
driverKind: acpxProfile!.driverKind,
protocolVersion: acpxProfile!.protocolVersion,
@@ -1,8 +1,8 @@
import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
import { createServer } from "node:http";
import path from "node:path";
import {
cleanupGitHubOperationLaunchers,
githubOperationLauncherDirectory,
prepareGitHubOperationLaunchers,
startAdapterExecutionTargetPaperclipBridge,
} from "@paperclipai/adapter-utils/execution-target";
@@ -98,7 +98,7 @@ export async function createNativeGitHubAccess(input: {
bridge = await startBridge({
...location,
runtimeRootDir: input.target?.kind === "remote"
? path.posix.join(input.target.remoteCwd, ".paperclip-runtime", "github", location.runId)
? githubOperationLauncherDirectory(location)
: null,
adapterKey: "native-github",
hostApiToken: token,
@@ -23,6 +23,7 @@ import {
nativeRunFinalizations,
} from "@paperclipai/db";
import type { PrpEvent } from "@paperclipai/paperclip-runner";
import { respondIssueThreadInteractionSchema } from "@paperclipai/shared";
import {
getEmbeddedPostgresTestSupport,
@@ -442,6 +443,54 @@ describeEmbeddedPostgres("native question bridge", () => {
release();
});
it.each(["legacy", "canonical_select"])("keeps %s custom-answer normalization and rejects blank public answers", async (mode) => {
await seed();
const event = runtimeRequestEvent();
const input = (event.payload.request as { input: { questions: Record<string, unknown>[] } }).input;
input.questions[0]!.customAnswer = { enabled: true };
const interaction = await projectNativeRuntimeRequest({ db, binding: binding(), event });
if (mode === "legacy") {
const payload = { ...interaction!.payload };
if ("questionSet" in payload) delete payload.questionSet;
await db.update(issueThreadInteractions).set({ payload }).where(eq(issueThreadInteractions.id, interaction!.id));
}
const service = issueThreadInteractionService(db);
await expect(service.answerQuestions({ id: issueId, companyId, status: "in_progress" }, interaction!.id,
respondIssueThreadInteractionSchema.parse({ answers: [{ questionId: "color", optionIds: [], otherText: " \n " }] }), { userId: "operator-1" },
)).rejects.toThrow(/requires an answer/);
const answered = await service.answerQuestions({ id: issueId, companyId, status: "in_progress" }, interaction!.id,
respondIssueThreadInteractionSchema.parse({ answers: [{ questionId: "color", optionIds: [], otherText: " Extra\\nline " }] }), { userId: "operator-1" },
);
expect(answered.result).toMatchObject({ answers: [{ questionId: "color", otherText: "Extra\nline" }] });
});
it("persists initial text without resolving and delivers only an explicit edited answer", async () => {
await seed();
const initialText = " Editable draft\n漢字\n";
const event = runtimeRequestEvent();
const request = event.payload.request as Record<string, unknown>;
request.input = { schema: "paperclip.question_set.v1", questions: [{ id: "draft", prompt: "Edit", required: true, answerMode: "text", initialText }] };
const interaction = await projectNativeRuntimeRequest({ db, binding: binding(), event });
expect(interaction).toMatchObject({ status: "pending", payload: { questionSet: { questions: [{ initialText }] } } });
const [stored] = await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, interaction!.id));
expect(stored).toMatchObject({ status: "pending", result: null, payload: { questionSet: { questions: [{ initialText }] } } });
const queueCommand = vi.fn(() => ({ commandId: "edited", controllerSeq: 1 }));
const release = registerNativeQuestionCommandTarget({ binding: { companyId, issueId, runId, agentId }, queueCommand });
try {
await flushNativeQuestionResponses(db, runId);
expect(queueCommand).not.toHaveBeenCalled();
const edited = "\n Operator 漢字 edited\n\nLiteral \\n stays literal. \n";
await issueThreadInteractionService(db).answerQuestions(
{ id: issueId, companyId, status: "in_progress" }, interaction!.id,
respondIssueThreadInteractionSchema.parse({ answers: [{ questionId: "draft", optionIds: [], otherText: edited }] }), { userId: "operator-1" },
);
await flushNativeQuestionResponses(db, runId);
expect(queueCommand).toHaveBeenCalledExactlyOnceWith("request.resolve", {
requestId: "request-1", response: { schema: "paperclip.question_response.v1", answers: { draft: { text: edited } } },
}, `question_${interaction!.id}`);
} finally { release(); }
});
it.each(["succeeded", "failed", "cancelled", "timed_out"])("delivers a historical answer exactly once after a %s native run", async status => {
await seed();
await db.update(issues).set({ conversationAgentId: agentId, conversationUserId: "operator-1", conversationState: "active", conversationSessionGeneration: 1, executionRunId: null }).where(eq(issues.id, issueId));
@@ -11,7 +11,7 @@ import { conversationNativeDecision, isConversation } from "../agent-conversatio
import { issueTreeControlService } from "../issue-tree-control.js";
import { randomUUID } from "node:crypto";
import { preserveNativeWorkspaceExportLease } from "./native-workspace-export-resume.js";
import { and, eq, inArray, isNotNull, isNull, or, sql } from "drizzle-orm";
import { and, desc, eq, inArray, isNotNull, isNull, or, sql } from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import {
approvals,
@@ -20,6 +20,7 @@ import {
heartbeatRuns,
heartbeatRunEvents,
issueApprovals,
issueComments,
issueRecoveryActions,
issueThreadInteractions,
issues,
@@ -62,7 +63,11 @@ import {
import { logger } from "../../middleware/logger.js";
import {
CHAT_RUN_PRESENTATION_AUTHORIZATION_REASON,
findHeartbeatRunCompletionComment,
isExternalChatPresentationContext,
readCompletedAssistantMessageCandidate,
resolveHeartbeatRunResponse,
selectHeartbeatRunFinalAgentMessage,
} from "../heartbeat-run-summary.js";
import { resolveChatRunPresentationAuthorizationReason } from "../chat-run-publications.js";
import {
@@ -940,13 +945,6 @@ export async function repairCommittedNativeChatResponse(
!accepted ||
!decision ||
result.schema !== "paperclip.run_result.v1" ||
result.reportedWorkDisposition !== "yielded" ||
record(result.continuation).kind !== "response_wake" ||
!Array.isArray(result.attentionRequests) ||
result.attentionRequests.length > 0 ||
terminal.runTerminalState !== "succeeded" ||
terminal.turnTerminalState !== "completed" ||
terminal.reportedWorkDisposition !== "yielded" ||
!(await acceptedResponseDigestMatches(tx, run, accepted))
)
return false;
@@ -954,6 +952,70 @@ export async function repairCommittedNativeChatResponse(
// presentation contract, including selected attachments. Do not bypass it.
if (record(decision.decisionJson).externalChatReviewPresentation)
return false;
// Copyback can be owned by reconciliation before the live heartbeat
// reaches its presentation step. Recover a completed internal task's
// exact final reply from the same accepted turn, without rerunning work
// or granting external-chat publication authority.
if (
!isConversation(issue) &&
run.status === "succeeded" &&
record(run.contextSnapshot).skipIssueComment !== true &&
!isExternalChatPresentationContext(run.contextSnapshot) &&
result.reportedWorkDisposition === "done" &&
terminal.runTerminalState === "succeeded" &&
terminal.turnTerminalState === "completed" &&
terminal.reportedWorkDisposition === "done" &&
(await resolveChatRunPresentationAuthorizationReason(tx, input)) === "internal_agent_write"
) {
const rows = await tx.select({ seq: heartbeatRunEvents.seq, payload: heartbeatRunEvents.payload })
.from(heartbeatRunEvents).where(and(
eq(heartbeatRunEvents.companyId, input.companyId),
eq(heartbeatRunEvents.runId, run.id),
eq(heartbeatRunEvents.eventType, "item.completed"),
sql`${heartbeatRunEvents.payload}->'prpEvent'->>'turnId' = ${accepted.turnId}`,
sql`${heartbeatRunEvents.payload} #>> '{prpEvent,payload,kind}' = 'agentMessage'`,
sql`${heartbeatRunEvents.payload} #>> '{prpEvent,payload,channel}' = 'final'`,
)).orderBy(desc(heartbeatRunEvents.seq)).limit(200);
const finalAgentMessage = selectHeartbeatRunFinalAgentMessage({
candidates: rows.flatMap((row) => {
const candidate = readCompletedAssistantMessageCandidate({ seq: row.seq, prpEvent: record(row.payload).prpEvent });
return candidate ? [candidate] : [];
}),
});
if (!finalAgentMessage) return false;
const comments = await tx.select({ id: issueComments.id, body: issueComments.body })
.from(issueComments).where(and(
eq(issueComments.companyId, input.companyId),
eq(issueComments.issueId, input.issueId),
eq(issueComments.createdByRunId, run.id),
)).orderBy(desc(issueComments.createdAt), desc(issueComments.id));
const resolved = resolveHeartbeatRunResponse({
resultJson: { ...record(run.resultJson), nativeResult: result },
existingComment: findHeartbeatRunCompletionComment(comments, run.resultJson),
finalAgentMessage,
});
if (!resolved.text || resolved.decision.commentAction !== "create") return false;
const comment = await issueService(db).addComment(input.issueId, resolved.text,
{ agentId: run.agentId, runId: run.id },
{ authorizationReason: "internal_agent_write", completionReply: true }, tx);
await tx.update(heartbeatRuns).set({
resultJson: sql`coalesce(${heartbeatRuns.resultJson}, '{}'::jsonb) || ${JSON.stringify({
presentationDecision: { ...resolved.decision, commentId: comment.id,
reasonCodes: [...resolved.decision.reasonCodes, "committed_task_response_recovered"] },
})}::jsonb`, updatedAt: new Date(),
}).where(eq(heartbeatRuns.id, run.id));
agentId = run.agentId;
return true;
}
if (
result.reportedWorkDisposition !== "yielded" ||
record(result.continuation).kind !== "response_wake" ||
!Array.isArray(result.attentionRequests) ||
result.attentionRequests.length > 0 ||
terminal.runTerminalState !== "succeeded" ||
terminal.turnTerminalState !== "completed" ||
terminal.reportedWorkDisposition !== "yielded"
) return false;
await authorizeCommittedChatResponse(db, tx, {
...input,
agentId: run.agentId,
@@ -8,6 +8,8 @@ import {
link,
mkdir,
readFile,
readdir,
rm,
symlink,
unlink,
writeFile,
@@ -39,6 +41,7 @@ import { issueService } from "../issues.js";
import { findHeartbeatRunCompletionComment, resolveHeartbeatRunResponse } from "../heartbeat-run-summary.js";
import {
renderNativeRunnerStagedAttachmentPrompt,
stageNativeRunnerAttachmentBytes,
stageNativeRunnerWakeAttachments,
} from "./native-runner-file-handoff.js";
import { PaperclipRunnerToolAuthority } from "./paperclip-runner-tool-authority.js";
@@ -195,6 +198,91 @@ describe("native runner file handoff", () => {
};
}
async function stageEmptyWake(root: string, boundCompanyId = companyId) {
await db.update(heartbeatRuns)
.set({ contextSnapshot: { issueId } })
.where(eq(heartbeatRuns.id, runId));
return stageNativeRunnerWakeAttachments({
db,
binding: {
companyId: boundCompanyId, issueId, runId, agentId,
workspaceRoot: root, executionTargetKind: "local",
},
});
}
it("leaves an empty workspace unchanged for an empty wake and permits later explicit staging", async () => {
const root = await mkdtemp(path.join(temporaryRoot, "empty-wake-"));
try {
const wake = await stageEmptyWake(root);
expect(wake.attachments).toEqual([]);
await wake.cleanup();
await expect(readdir(root)).resolves.toEqual([]);
const explicit = await stageNativeRunnerAttachmentBytes({ workspaceRoot: root, body: Buffer.from("later authorized bytes") });
await expect(readFile(path.join(root, explicit.workspaceRelativePath), "utf8"))
.resolves.toBe("later authorized bytes");
await explicit.cleanup();
await expect(readFile(path.join(root, explicit.workspaceRelativePath)))
.resolves.toEqual(Buffer.alloc(0));
} finally {
await rm(root, { recursive: true, force: true });
}
});
it("scrubs inactive staging bytes on an empty wake without creating another directory", async () => {
const root = await mkdtemp(path.join(temporaryRoot, "empty-wake-residue-"));
try {
const previous = await stageNativeRunnerAttachmentBytes({ workspaceRoot: root, body: Buffer.from("previous attachment") });
await previous.cleanup();
const slot = path.join(root, previous.workspaceRelativePath);
await writeFile(slot, "stale bytes after an interrupted owner");
const directories = await readdir(path.join(root, ".paperclip-inbound"));
const wake = await stageEmptyWake(root);
expect(wake.attachments).toEqual([]);
await wake.cleanup();
await expect(readFile(slot)).resolves.toEqual(Buffer.alloc(0));
await expect(readdir(path.join(root, ".paperclip-inbound"))).resolves.toEqual(directories);
} finally {
await rm(root, { recursive: true, force: true });
}
});
it.each(["root-file", "root-symlink", "entry-symlink"] as const)(
"rejects unsafe %s staging on an empty wake without changing outside bytes",
async (kind) => {
const root = await mkdtemp(path.join(temporaryRoot, "empty-wake-unsafe-"));
const outside = await mkdtemp(path.join(temporaryRoot, "empty-wake-outside-"));
try {
await writeFile(path.join(outside, "keep.txt"), "outside bytes");
const inbound = path.join(root, ".paperclip-inbound");
if (kind === "root-file") await writeFile(inbound, "not a directory");
else if (kind === "root-symlink") await symlink(outside, inbound);
else {
await mkdir(inbound);
await symlink(outside, path.join(inbound, "untrusted-owner"));
}
await expect(stageEmptyWake(root)).rejects.toThrow(/staging_(?:path|residue)_denied/);
await expect(readFile(path.join(outside, "keep.txt"), "utf8")).resolves.toBe("outside bytes");
} finally {
await rm(root, { recursive: true, force: true });
await rm(outside, { recursive: true, force: true });
}
},
);
it("still authorizes an empty wake before touching its workspace", async () => {
const root = await mkdtemp(path.join(temporaryRoot, "empty-wake-unauthorized-"));
try {
await expect(stageEmptyWake(root, "00000000-0000-4000-8000-000000009199"))
.rejects.toThrow("paperclip_runner_attachment_staging_not_authorized");
await expect(readdir(root)).resolves.toEqual([]);
} finally {
await rm(root, { recursive: true, force: true });
}
});
it("rejects a workspace-only file completion and invented delivery receipts without asking the user to approve completion", async () => {
for (const ref of ["launch-checklist.md", "./out/report.pdf", "/workspace/answer.txt", "file:out/report.csv", "deliverable:00000000-0000-4000-8000-000000000001"]) {
await expect(nativeCompletionFeedback(db, runId, doneReport([ref])))
@@ -244,6 +332,42 @@ describe("native runner file handoff", () => {
}
});
it.each([
"Use native write/read file tools. Write memory/pi-native.txt inside AGENT_HOME. This is personal memory, not a task deliverable.",
"Use native write to copy bytes into pi-agent-memory-proof.txt. This is an internal assertion file, not a deliverable.",
"Attempt native write once to /outside/pi-unassigned.txt. This intentionally unassigned root must be denied.",
"Write internal-proof.txt; then check it exists. This is an internal verification file, not a deliverable.",
"Attempt native write to /outside/probe.txt and create no files. This negative test must be denied.",
"Write internal-proof.txt and return it in chat. This is an internal verification file, not a deliverable.",
"Write internal-proof.txt and send it as a code block in your response. This is an internal verification file, not a deliverable.",
])("accepts an internal file outcome without treating it as published output: %s", async objective => {
await db.update(heartbeatRuns).set({ contextSnapshot: { issueId, executionContinuation: { objective } } })
.where(eq(heartbeatRuns.id, runId));
try {
await expect(nativeCompletionFeedback(db, runId, doneReport([])))
.resolves.toContain("Completion report accepted");
} finally {
await db.update(heartbeatRuns).set({ contextSnapshot: { issueId } }).where(eq(heartbeatRuns.id, runId));
}
});
it.each([
"Write report.pdf; write internal-proof.txt. This is an internal verification file, not a deliverable.",
"Write report.pdf and attempt native write to /outside/probe.txt; this negative test must be denied.",
"Write report.txt and attach it. This is an internal verification file, not a deliverable.",
"Write report.txt and send it to me. This is an internal verification file, not a deliverable.",
"Write report.txt. This is an internal verification file, not a deliverable. Send it as a download link in your response.",
])("still requires publication when the task also asks for internal or denied writes: %s", async objective => {
await db.update(heartbeatRuns).set({ contextSnapshot: { issueId, executionContinuation: { objective } } })
.where(eq(heartbeatRuns.id, runId));
try {
await expect(nativeCompletionFeedback(db, runId, doneReport([])))
.rejects.toThrow("requested file has no accessible delivery evidence");
} finally {
await db.update(heartbeatRuns).set({ contextSnapshot: { issueId } }).where(eq(heartbeatRuns.id, runId));
}
});
it("prepares one verified same-run attachment and replays without duplicates", async () => {
const body = Buffer.from("native runner file handoff\n", "utf8");
await mkdir(path.join(workspaceRoot, "out"), { recursive: true });
@@ -5,6 +5,7 @@ import { PgDialect } from "drizzle-orm/pg-core";
import type { SQL } from "drizzle-orm";
import {
access,
chmod,
cp,
lstat,
mkdir,
@@ -42,12 +43,18 @@ import {
validatePrpEvent,
parseNativeExecutionInput,
type NativeExecutionInputV1,
type NativeExecutionInputV3,
type NativeExecutionInput,
type PrpEvent,
} from "@paperclipai/paperclip-runner";
import { createHash, generateKeyPairSync } from "node:crypto";
import { DatabaseSync } from "node:sqlite";
import { nativeSha256 } from "./canonical.js";
import { agentDirectoryWorkingCopyService } from "../agent-directory-working-copies.js";
import { probeAgentDirectory } from "../agent-directory-probe.js";
import { AGENT_FILES_CONTRACT } from "../agent-file-store.js";
import { captureDirectorySnapshot, directorySnapshotSha256, serializeDirectorySnapshot } from "@paperclipai/adapter-utils/workspace-restore-merge";
import { agentInstructionWorkingCopies } from "@paperclipai/db";
import * as noLaunchProofModule from "./native-maintenance-no-launch.js";
import {
NativeSessionCleanupQuarantinedError,
@@ -63,6 +70,7 @@ import {
} from "./native-harness-backup-stamp.js";
import { nativeRuntimeContextFixture } from "./runtime-context.test-fixture.js";
import { nativeToolContractFingerprintForTarget } from "./native-session-resume.js";
import * as nativeSessionResume from "./native-session-resume.js";
import { buildNativeHeartbeatPreparationSpans } from "./native-run-trace.js";
import { NativeRunnerOwnershipUnverifiedError } from "./native-runner-ownership.js";
import type { AdapterRuntimeEvent } from "../../adapters/index.js";
@@ -297,6 +305,8 @@ import {
buildNativeHarnessBackupManifest,
cancelNativeSession,
closeWarmNativeSessionsForEnvironment,
claimWarmNativeInstructionCopy,
nativeSessionWorkspaceScope,
reserveWarmNativeInstructionDirectory,
closeIdleWarmNativeSessionsForRestart,
createGovernedWaitEventObservation,
@@ -1252,22 +1262,43 @@ describe("remote provider pack manifest", () => {
const candidatePath = "provider-assets/pi/linux-x64";
await mkdir(join(root, candidatePath), { recursive: true });
await writeFile(join(root, candidatePath, "runtime"), "pinned runtime");
const candidates = { pi: { version: "0.0.33", profileDigest: digest("profile"),
closureDigest: digest("closure"), qualification: "pending", path: candidatePath,
const candidates = { pi: { version: "1.0.0", profileDigest: digest("profile"),
closureDigest: digest("closure"), qualification: "qualified", path: candidatePath,
sha256: sha256DirectoryTree(join(root, candidatePath)) } };
Object.assign(payload, { candidateProviders: candidates });
await writeManifest();
expect(readRemoteProviderPackManifest(root).payload.candidateProviders?.pi?.qualification).toBe("pending");
expect(readRemoteProviderPackManifest(root).payload.candidateProviders?.pi?.qualification).toBe("qualified");
// The normal pack builder publishes Pi in both inventories. A Pi image
// must pass the same reader used by real remote runtime preparation.
Object.assign(payload, { providers: { cursor, pi: candidates.pi } });
await writeManifest();
expect(readRemoteProviderPackManifest(root).payload.providers?.pi?.qualification).toBe("qualified");
await mkdir(releaseMetadata, { recursive: true });
await cp(join(root, "provider-pack.json"), manifestPath);
expect(readBundledRemoteProviderPackManifest(manifestPath).payload.providers?.pi?.qualification).toBe("qualified");
Object.assign(payload, { providers: { cursor } });
await rm(releaseMetadata, { recursive: true, force: true });
await writeManifest();
await writeFile(join(root, candidatePath, "runtime"), "substitute runtime");
expect(() => readRemoteProviderPackManifest(root)).toThrow("candidate asset tree digest mismatch");
await writeFile(join(root, candidatePath, "runtime"), "pinned runtime");
for (const invalid of [{ path: "../outside" }, { qualification: "qualified" }]) {
candidates.pi.qualification = "pending";
await writeManifest();
expect(() => readRemoteProviderPackManifest(root)).toThrow("invalid candidate identity");
candidates.pi.qualification = "qualified";
for (const invalid of [{ path: "../outside" }, { qualification: "unknown" }]) {
const original = { ...candidates.pi };
Object.assign(candidates.pi, invalid);
await writeManifest();
expect(() => readRemoteProviderPackManifest(root)).toThrow("invalid candidate identity");
candidates.pi = original;
}
for (const provider of ["cursor", "copilot"]) {
Object.assign(candidates, { [provider]: { ...candidates.pi, path: `provider-assets/${provider}/linux-x64` } });
await writeManifest();
expect(() => readRemoteProviderPackManifest(root)).toThrow(provider === "cursor" ? "Cursor profile or closure does not match this release" : "invalid candidate identity");
delete (candidates as Record<string, unknown>)[provider];
}
await writeManifest();
for (const [artifactName, substituteName] of [
["nodeCommand", "productionLock"],
@@ -1537,6 +1568,67 @@ describe("verified native harness backups", () => {
).toBe(false);
});
it("binds Cursor mode across governed and identical recovery identities", () => {
const execution = {
...backupExecution,
provider: { kind: "acpx", agent: "cursor", model: "explicit-model", mode: "plan" },
interactionResponses: [{ interactionId: "interaction-1" }],
} as unknown as NativeExecutionInputV1;
const identity = (suffix: string, mode: unknown) => {
const value = acpxIdentity(suffix);
return { ...value, providerSessionIdentity: { ...value.providerSessionIdentity, mode } };
};
const previous = identity("previous", "plan");
const current = identity("current", "plan");
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous, current })).toBe(true);
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous, current: previous })).toBe(true);
for (const mode of [undefined, null, "agent", "ask", "autopilot"]) {
const wrong = identity("wrong", mode);
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous, current: wrong })).toBe(false);
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous: wrong, current })).toBe(false);
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous: wrong, current: wrong })).toBe(false);
}
for (const provider of [
{ kind: "acpx", agent: "cursor", model: "explicit-model" },
{ kind: "acpx", agent: "copilot", model: "explicit-model" },
]) {
expect(providerSessionIdentityTransitionIsAllowed({
execution: { ...execution, provider } as unknown as NativeExecutionInputV1, previous, current,
})).toBe(false);
}
});
it("binds Pi thinking across governed and identical recovery identities", () => {
const execution = {
...backupExecution,
provider: { kind: "acpx", agent: "pi", model: "explicit-model", piThinkingLevel: "low" },
interactionResponses: [{ interactionId: "interaction-1" }],
} as unknown as NativeExecutionInputV1;
const identity = (suffix: string, piThinkingLevel: unknown) => {
const value = acpxIdentity(suffix);
return { ...value, providerSessionIdentity: { ...value.providerSessionIdentity, piThinkingLevel } };
};
const previous = identity("previous", "low");
const current = identity("current", "low");
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous, current })).toBe(true);
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous, current: previous })).toBe(true);
for (const mode of [undefined, null, "high", "max", "medium"]) {
const wrong = identity("wrong", mode);
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous, current: wrong })).toBe(false);
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous: wrong, current })).toBe(false);
expect(providerSessionIdentityTransitionIsAllowed({ execution, previous: wrong, current: wrong })).toBe(false);
}
for (const provider of [
{ kind: "acpx", agent: "pi", model: "explicit-model" },
{ kind: "acpx", agent: "copilot", model: "explicit-model" },
{ kind: "acpx", agent: "copilot", model: "explicit-model", piThinkingLevel: "low" },
]) {
expect(providerSessionIdentityTransitionIsAllowed({
execution: { ...execution, provider } as unknown as NativeExecutionInputV1, previous, current,
})).toBe(false);
}
});
it("restores a verified continuation into an intentionally fresh non-reusable sandbox", () => {
expect(
shouldRestoreNativeHarnessBackupIntoSandbox({
@@ -1899,6 +1991,27 @@ describe("split durable provider checkpoint identity", () => {
});
});
it("requires the exact observed Cursor mode in suspended descriptor and identity", () => {
const profileDigest = `sha256:${"a".repeat(64)}`;
const input = execution({ kind: "acpx", agent: "cursor", model: "explicit-model", permissionMode: "deny-all", mode: "plan" }, "acpx_runtime");
const providerState = {
schema: "paperclip.runner.acpx-provider-state.v3", lifecycle: "suspended", activeTurnId: null, providerExitUnconfirmed: false,
descriptor: { kind: "acpx", provider: "acpx", driver: "acpx_runtime", agent: "cursor", model: "explicit-model", commandDigest: profileDigest, normalizedSessionId: "native-session", mode: "plan" },
identity: { kind: "acpx", normalizedSessionId: "native-session", acpxRecordId: "record-1", backendSessionId: "backend-1", agentSessionId: "agent-1", profileDigest,
workspaceDigest: `sha256:${"b".repeat(64)}`, requestedModel: "explicit-model", effectiveModel: "explicit-model", permissionMode: "deny-all", mode: "plan", providerLifetimeFenceCandidates: [53001, 53002, 53003] },
};
const read = (state: unknown, candidate = input) => providerSessionIdentityFromDurableProviderState({ execution: candidate, providerState: state });
expect(read(providerState).providerSessionIdentity).toEqual(providerState.identity);
for (const mode of [undefined, null, "agent", "ask", "autopilot"]) {
expect(read({ ...providerState, identity: { ...providerState.identity, mode } }).providerSessionIdentity).toBeNull();
expect(read({ ...providerState, descriptor: { ...providerState.descriptor, mode } }).providerSessionIdentity).toBeNull();
expect(read(providerState, execution({ ...input.provider, mode }, "acpx_runtime")).providerSessionIdentity).toBeNull();
}
const foreign = { ...providerState, descriptor: { ...providerState.descriptor, agent: "copilot" } };
expect(read(foreign, execution({ ...input.provider, agent: "copilot", mode: undefined }, "acpx_runtime")).providerSessionIdentity).toBeNull();
expect(read(foreign, execution({ ...input.provider, agent: "copilot", mode: "plan" }, "acpx_runtime")).providerSessionIdentity).toEqual(providerState.identity);
});
it.each([
["codex", "codex_app_server"],
["opencode", "opencode_server"],
@@ -4841,6 +4954,16 @@ describe("native governed waits", () => {
payload: { kind: "dynamicToolCall" },
};
// The durable fallback preserves the question after provider loss, but
// cannot turn that lost execution into a successful governed wait.
const providerLost = { ...replayedEvent, eventType: "runtime_request.expired" as const,
payload: { reason: "provider_process_lost", replayAllowed: false } };
const lostObservation = createGovernedWaitEventObservation(async () => waitResult);
await lostObservation.observe(providerLost, true);
expect(lostObservation.consume(providerLost)).toBeNull();
await lostObservation.observe(replayedEvent, true);
expect(lostObservation.consume(replayedEvent)).toBeNull();
// The event consumer can lag the provider: a commentary event emitted
// before the tool began may be processed after its approval exists in DB.
// It is not proof that the approval-creating tool response has settled.
@@ -5011,9 +5134,13 @@ function cancellationDb(options?: {
runId: string;
assessmentId: string | null;
decisionId?: string | null;
phase?: string;
failureCode?: string | null;
} | null;
status?: string;
failResultJsonUpdateAt?: number;
ownershipHeld?: boolean;
resultJson?: Record<string, unknown>;
}) {
const initialRun = {
id: execution.binding.runId,
@@ -5021,6 +5148,7 @@ function cancellationDb(options?: {
companyId: execution.binding.companyId,
nativeIssueId: execution.binding.issueId,
runtimeMode: "native",
status: options?.status ?? "running",
...(options?.ownershipHeld
? {
status: "running",
@@ -5033,6 +5161,7 @@ function cancellationDb(options?: {
};
let currentResultJson: Record<string, unknown> = {
durableReceipt: { operationId: "operation-1" },
...options?.resultJson,
};
const issue = {
status: "in_progress",
@@ -5079,6 +5208,7 @@ function cancellationDb(options?: {
set: (values: Record<string, unknown>) => ({
where: () => {
updates.push({ table, values });
if (table === nativeRunFinalizations && coordinator) Object.assign(coordinator, values);
const updatesResultJson = "resultJson" in values;
if (updatesResultJson) resultJsonUpdateCount += 1;
const shouldFail =
@@ -5953,6 +6083,71 @@ describe("native session cancellation", () => {
expect(state.publishActivity).toHaveBeenCalledTimes(2);
});
it.each(["terminal_failure", "observed", "committed"])("rejects retry phase %s that changed after caller reservation, before any native effect", async phase => {
const id = "11111111-1111-4111-8111-111111111111";
const persistence = cancellationDb({ status: "failed",
coordinator: { runId: execution.binding.runId, assessmentId: null, phase, failureCode: "board_recovery" },
resultJson: { startupCancellation: { cancellationRequestId: id, retryCancellation: true } } });
await expect(cancelNativeSession(execution.binding.runId, "operator Stop", { db: persistence.db,
scope: "run", cancellationRequestId: id })).rejects.toMatchObject({ status: 409, message: "Native retry is no longer cancellable" });
expect(persistence.updates).toEqual([]);
expect(state.cancel).not.toHaveBeenCalled();
expect(state.persistActivity).not.toHaveBeenCalled();
});
it("does not mistake a recovered running attempt for the failed retry that reserved Stop", async () => {
const id = "11111111-1111-4111-8111-111111111111";
const persistence = cancellationDb({ status: "running",
coordinator: { runId: execution.binding.runId, assessmentId: null, phase: "observed" },
resultJson: { startupCancellation: { cancellationRequestId: id, retryCancellation: true } } });
await expect(cancelNativeSession(execution.binding.runId, "operator Stop", { db: persistence.db,
scope: "run", cancellationRequestId: id })).rejects.toMatchObject({ status: 409 });
expect(persistence.updates).toEqual([]);
expect(state.cancel).not.toHaveBeenCalled();
});
it("fences a failed retry at dispatch and preserves the same audited intent on replay", async () => {
const id = "11111111-1111-4111-8111-111111111111";
const persistence = cancellationDb({ status: "failed",
coordinator: { runId: execution.binding.runId, assessmentId: null, phase: "retryable_failure" },
resultJson: { startupCancellation: { cancellationRequestId: id, retryCancellation: true } } });
const first = await cancelNativeSession(execution.binding.runId, "operator Stop", { db: persistence.db, scope: "run", cancellationRequestId: id });
expect(persistence.updates).toContainEqual(expect.objectContaining({ table: nativeRunFinalizations,
values: expect.objectContaining({ phase: "terminal_failure", failureCode: "native_retry_cancelled", nextAttemptAt: null }) }));
const writes = persistence.getResultJsonUpdateCount();
await expect(cancelNativeSession(execution.binding.runId, "retry Stop", { db: persistence.db,
scope: "run", cancellationRequestId: id })).resolves.toMatchObject({ auditId: first.auditId });
expect(persistence.getResultJsonUpdateCount()).toBe(writes);
});
it("uses the reserved caller UUID and permits same-intent and default retries", async () => {
const id = "11111111-1111-4111-8111-111111111111";
const persistence = cancellationDb({ resultJson: { startupCancellation: { cancellationRequestId: id } } });
await cancelNativeSession(execution.binding.runId, "operator Stop", { db: persistence.db, scope: "run", cancellationRequestId: id });
expect(persistence.getResultJson().nativeCancellation).toMatchObject({ intentId: `native-cancellation:${id}` });
const writes = persistence.getResultJsonUpdateCount();
await cancelNativeSession(execution.binding.runId, "same request", { db: persistence.db, scope: "run", cancellationRequestId: id });
await cancelNativeSession(execution.binding.runId, "default retry", { db: persistence.db, scope: "run" });
expect(persistence.getResultJsonUpdateCount()).toBe(writes);
await expect(cancelNativeSession(execution.binding.runId, "foreign caller", {
db: persistence.db, scope: "run", cancellationRequestId: "22222222-2222-4222-8222-222222222222",
})).rejects.toMatchObject({ status: 409 });
expect(persistence.getResultJsonUpdateCount()).toBe(writes);
});
it.each([{}, { startupCancellation: { requestedAt: "prior" } },
{ startupCancellation: { cancellationRequestId: "other" } },
{ startupCancellation: { cancellationRequestId: "11111111-1111-4111-8111-111111111111" }, nativeCancellation: { intentId: "foreign" } },
])("rejects missing or raced caller ownership under the intent lock: %j", async resultJson => {
const persistence = cancellationDb({ resultJson });
await expect(cancelNativeSession(execution.binding.runId, "operator Stop", {
db: persistence.db, scope: "run", cancellationRequestId: "11111111-1111-4111-8111-111111111111",
})).rejects.toMatchObject({ status: 409 });
expect(persistence.getForUpdateCount()).toBe(1);
expect(persistence.updates).toEqual([]);
expect(state.cancel).not.toHaveBeenCalled();
});
it("recovers a post-dispatch persistence failure without cancelling the provider twice", async () => {
const persistence = cancellationDb({ failResultJsonUpdateAt: 2 });
const running = executePaperclipNativeSession({
@@ -6418,7 +6613,499 @@ describe("native session same-turn steering", () => {
});
});
describe("bounded stopped instruction collection", () => {
it.each([
{ scenario: "deferred", attempts: [0], nextAttemptAt: null, expectedCalls: 1 },
{ scenario: "no progress", attempts: [0, 0], nextAttemptAt: new Date(0), expectedCalls: 2 },
{ scenario: "regressed counter", attempts: [1, 0], nextAttemptAt: new Date(0), expectedCalls: 2 },
{ scenario: "progressing counter", attempts: [0, 1, 2], nextAttemptAt: new Date(0), expectedCalls: 3 },
{ scenario: "exhausted", attempts: [3], nextAttemptAt: new Date(0), expectedCalls: 1 },
])("finishes pending collection without inventing attempts: $scenario", async ({ attempts, nextAttemptAt, expectedCalls }) => {
const { collectStoppedInstructionCopyWithRetries } = await import("../agent-instruction-working-copies.js");
const collect = vi.fn(async () => {
const index = collect.mock.calls.length - 1;
if (index >= attempts.length) throw new Error("collector invoked beyond bounded progress");
return { state: "pending_collection", attempts: attempts[index]!, nextAttemptAt };
});
expect(await collectStoppedInstructionCopyWithRetries(collect)).toEqual({ state: "pending_collection", attempts: attempts.at(-1), nextAttemptAt });
expect(collect).toHaveBeenCalledTimes(expectedCalls);
});
it("returns completed or absent copies and propagates collection errors", async () => {
const { collectStoppedInstructionCopyWithRetries } = await import("../agent-instruction-working-copies.js");
const result = { state: "saved", attempts: 1, nextAttemptAt: null };
const saved = vi.fn(async () => result), absent = vi.fn(async () => null);
expect(await collectStoppedInstructionCopyWithRetries(saved)).toBe(result);
expect(await collectStoppedInstructionCopyWithRetries(absent)).toBeNull();
expect(saved).toHaveBeenCalledOnce(); expect(absent).toHaveBeenCalledOnce();
await expect(collectStoppedInstructionCopyWithRetries(async () => { throw new Error("collection failed"); })).rejects.toThrow("collection failed");
});
});
describe("native warm session supervision", () => {
it.each(["collect", "close-failure", "stopped", "destroyed", "missing-lease", "foreign-run", "foreign-environment", "deleted-environment", "claim-edit", "claim-add", "claim-remove", "claim-remove-entry", "claim-canonical", "claim-config", "claim-uncertain"])("composes remote warm ownership with real DB successor leases and current-run collection: %s", async ending => {
const tables = await import("@paperclipai/db");
const { eq } = await import("drizzle-orm");
const { randomUUID } = await import("node:crypto");
const { promisify } = await import("node:util");
const { execFile } = await import("node:child_process");
const executeCommand = promisify(execFile);
const { startEmbeddedPostgresTestDatabase } = await import("../../__tests__/helpers/embedded-postgres.js");
const { agentInstructionWorkingCopyService } = await import("../agent-instruction-working-copies.js");
const { buildNativeRuntimeContext } = await import("./runtime-context.js");
const { prepareNativeHeartbeatRun } = await import("./prepare-native-run.js");
const { buildNativeCompletionContract } = await import("./completion-contracts.js");
const home = await realpath(await mkdtemp(join(tmpdir(), "warm-remote-db-")));
const previousHome = process.env.PAPERCLIP_HOME;
process.env.PAPERCLIP_HOME = home;
const database = await startEmbeddedPostgresTestDatabase("warm-remote-db-");
const db = tables.createDb(database.connectionString);
const companyId = randomUUID(), agentId = randomUUID(), userId = randomUUID(), environmentId = randomUUID(), issueId = randomUUID();
const { resolveManagedInstructionsRoot } = await import("../agent-instructions.js");
const canonical = resolveManagedInstructionsRoot({ id: agentId, companyId, name: "Warm remote", adapterConfig: {} }), remoteCwd = join(home, "remote");
let currentLease = "", failCleanup = false, failProbe = false, closed = false;
const commands: Array<{ leaseId: string; command: string }> = [];
const commandRunner = (leaseId: string): import("@paperclipai/adapter-utils/command-managed-runtime").CommandManagedRuntimeRunner => ({ execute: async input => {
expect(leaseId).toBe(currentLease); // The replaced collector must never execute.
commands.push({ leaseId, command: `${input.command} ${(input.args ?? []).join(" ")}` });
if (failProbe) {
failProbe = false;
expect(commands.at(-1)!.command).toContain("Agent directory probe deadline");
return { exitCode: 1, signal: null, timedOut: false, stdout: "", stderr: "injected uncertain observation", pid: null, startedAt: new Date().toISOString() };
}
if (failCleanup && commands.at(-1)!.command.includes("rm -rf --")) {
failCleanup = false;
return { exitCode: 1, signal: null, timedOut: false, stdout: "", stderr: "injected cleanup failure", pid: null, startedAt: new Date().toISOString() };
}
const env = { PATH: process.env.PATH, ...input.env }, args = [...(input.args ?? [])];
if (input.stdin != null && (args[0] === "-c" || args[0] === "-lc")) {
Object.assign(env, { PAPERCLIP_TEST_STDIN: input.stdin });
args[1] = `printf '%s' "$PAPERCLIP_TEST_STDIN" | (${args[1]})`;
}
const result = await executeCommand(input.command, args, { cwd: input.cwd, env, timeout: input.timeoutMs, maxBuffer: 32 * 1024 * 1024 });
return { exitCode: 0, signal: null, timedOut: false, ...result, pid: null, startedAt: new Date().toISOString() };
} });
let copies = agentInstructionWorkingCopyService(db);
const agent = { id: agentId, companyId, name: "Warm remote", adapterConfig: {
instructionsBundleMode: "managed", instructionsRootPath: canonical, instructionsEntryFile: "AGENTS.md" } };
let active: Awaited<ReturnType<typeof copies.prepare>>;
const session = { close: vi.fn(async () => { closed = true; }) };
state.execute.mockReset().mockImplementation(async options => {
await options.onSession?.(session);
return { result: { summary: "completed" }, terminal: { runTerminalState: "succeeded" },
turnId: "warm-turn", normalizedSessionId: environmentId, providerSessionId: environmentId,
driverKind: "test", driverVersion: "1", nativeEventCount: 1, highestContiguousSourceSeq: 1, usage: null };
});
let bodyFailure: unknown;
try {
await db.insert(tables.companies).values({ id: companyId, name: "Warm tests", issuePrefix: "WARM" });
await db.insert(tables.authUsers).values({ id: userId, name: "Editor", email: `${userId}@example.test`, createdAt: new Date(), updatedAt: new Date() });
await db.insert(tables.agents).values(agent);
await db.insert(tables.companyMemberships).values([{ companyId, principalType: "user", principalId: userId, membershipRole: "operator" }, { companyId, principalType: "agent", principalId: agentId, membershipRole: "member" }]);
await db.insert(tables.principalPermissionGrants).values({ companyId, principalType: "user", principalId: userId, permissionKey: "agents:configure", scope: { agentIds: [agentId] } });
await db.insert(tables.environments).values({ id: environmentId, name: "Owned fake remote", driver: "sandbox" });
const [issue] = await db.insert(tables.issues).values({ id: issueId, companyId, title: "Warm remote", status: "in_progress", assigneeAgentId: agentId }).returning();
await mkdir(canonical, { recursive: true }); await mkdir(remoteCwd);
await writeFile(join(canonical, "AGENTS.md"), "Retain this exact directory.\n");
await writeFile(join(canonical, "optional-memory.txt"), "Optional memory\n");
let prior: NativeExecutionInputV3 | undefined;
let originalRoot = "", materializationRunId = "";
for (let turn = 0; turn < 3; turn++) {
const runId = randomUUID();
if (currentLease) {
await db.update(tables.environmentLeases).set({ status: "expired", releasedAt: new Date(), cleanupStatus: "success" }).where(eq(tables.environmentLeases.id, currentLease));
await db.update(tables.heartbeatRuns).set({ status: "succeeded" }).where(eq(tables.heartbeatRuns.id, prior!.binding.runId));
}
currentLease = randomUUID();
const [run] = await db.insert(tables.heartbeatRuns).values({ id: runId, companyId, agentId, invocationSource: "on_demand", responsibleUserId: userId,
status: "running", runtimeMode: "native", nativeIssueId: issueId, nativeSessionId: environmentId, runnerInstanceId: environmentId,
contextSnapshot: { issueId } }).returning();
await db.insert(tables.nativeRunFinalizations).values({ runId, companyId, issueId, phase: "observed" });
await db.update(tables.issues).set({ executionRunId: runId }).where(eq(tables.issues.id, issueId));
await db.insert(tables.environmentLeases).values({ id: currentLease, companyId, environmentId, heartbeatRunId: runId,
provider: "daytona", providerLeaseId: "same-owned-allocation", leasePolicy: "reuse_by_environment", metadata: { remoteCwd } });
// Match the production preparation seam and existing native DB tests:
// pin the issue's completion contract and durable run/lease identities.
await prepareNativeHeartbeatRun({ db, run: run!, issue: issue!, environmentLeaseId: currentLease });
const [contract] = await db.select().from(tables.completionContracts).where(eq(tables.completionContracts.issueId, issueId));
const target = { kind: "remote" as const, transport: "sandbox" as const, providerKey: "daytona", environmentId, leaseId: currentLease, remoteCwd, runner: commandRunner(currentLease) };
const current: NativeExecutionInputV3 = { ...execution, schema: "paperclip.native-execution-input.v3",
executionMode: "default", planningContext: null, runtimeContext: nativeRuntimeContextFixture(),
completionContract: { id: contract!.id, sha256: contract!.canonicalSha256, schemaVersion: "paperclip.completion-contract.v1",
contract: buildNativeCompletionContract(issue!, { revision: contract!.revision }) }, binding: { companyId, agentId, issueId, runId, executionWorkspaceId: runId },
workspace: { cwd: remoteCwd, repoUrl: null, repoRef: null, branchName: null },
session: { ...execution.session, normalizedSessionId: environmentId, lifecyclePolicy: { mode: "warm", idleTimeoutMs: 60_000 } } };
const preparationKey = turn === 1 && ending === "claim-config" ? "changed-config" : "same-config";
let retiredReceipt: Record<string, unknown> | null = null;
const capability = () => ({ runId, preparationKey, hasChanges: () => copies.hasChanges({ companyId, runId, target }),
collectStopped: async () => { expect(closed).toBe(true); retiredReceipt = (await copies.get(companyId, runId))?.receipt ?? null;
const { collectStoppedInstructionCopyWithRetries } = await import("../agent-instruction-working-copies.js");
await collectStoppedInstructionCopyWithRetries(() => copies.collectStopped({ companyId, runId, target }));
}, retirementFailed: async () => { await copies.reportRetirementUnconfirmed(companyId, runId); } });
let release: (() => Promise<void>) | null = null;
if (prior) {
// Same environment is insufficient: reject a different physical allocation.
await db.update(tables.environmentLeases).set({ providerLeaseId: "foreign-allocation" }).where(eq(tables.environmentLeases.id, currentLease));
const before = commands.length;
expect(await copies.adopt({ companyId, agentId, runId, previousRunId: prior.binding.runId, target, cwd: remoteCwd })).toBeNull();
expect(commands).toHaveLength(before);
await db.update(tables.environmentLeases).set({ providerLeaseId: "same-owned-allocation" }).where(eq(tables.environmentLeases.id, currentLease));
if (ending === "claim-edit") await writeFile(join(originalRoot, "AGENTS.md"), "Remote edit before next claim\n");
if (ending === "claim-add") await writeFile(join(originalRoot, "memory.txt"), "Remote addition before next claim\n");
if (ending === "claim-remove") await rm(join(originalRoot, "optional-memory.txt"));
if (ending === "claim-remove-entry") await rm(join(originalRoot, "AGENTS.md"));
if (ending === "claim-canonical") await writeFile(join(canonical, "AGENTS.md"), "Canonical edit before next claim\n");
if (["claim-edit", "claim-add", "claim-remove", "claim-remove-entry", "claim-canonical"].includes(ending)) {
// Ordinary adoption remains unchanged-only and cannot alias this owner.
expect(await copies.adopt({ companyId, agentId, runId, previousRunId: prior.binding.runId, target, cwd: remoteCwd })).toBeNull();
expect((await copies.get(companyId, prior.binding.runId))?.receipt?.retainedByRunId).toBeUndefined();
}
failProbe = ending === "claim-uncertain";
release = await claimWarmNativeInstructionCopy({ priorExecution: prior, companyId, agentId, executionWorkspaceId: runId,
workspace: current.workspace, environmentId, runId, preparationKey, adopt: async previousRunId => {
active = await copies.adopt({ companyId, agentId, runId, previousRunId, target, cwd: remoteCwd, allowRetirementHandoff: true });
return active ? capability() : null;
} });
if (ending.startsWith("claim-")) {
expect(release).toBeNull();
expect(session.close).toHaveBeenCalledOnce();
expect(state.execute).toHaveBeenCalledOnce(); // No reuse was admitted.
if (ending !== "claim-config") expect(retiredReceipt).toMatchObject({ retirementRequired: true });
expect(retiredReceipt).toMatchObject({ materializationRunId, cleanup: { leaseId: currentLease, remoteCwd } });
expect((await copies.get(companyId, runId))?.processStoppedAt).toBeInstanceOf(Date);
const compact = (await copies.get(companyId, runId))!.receipt;
expect(compact).toMatchObject({ materializationRunId, cleanupPending: false });
expect(compact?.baseline).toBeUndefined();
await expect(access(originalRoot)).rejects.toMatchObject({ code: "ENOENT" });
if (ending === "claim-edit") expect(await readFile(join(canonical, "AGENTS.md"), "utf8")).toBe("Remote edit before next claim\n");
if (ending === "claim-add") expect(await readFile(join(canonical, "memory.txt"), "utf8")).toBe("Remote addition before next claim\n");
if (ending === "claim-remove") await expect(access(join(canonical, "optional-memory.txt"))).rejects.toMatchObject({ code: "ENOENT" });
if (ending === "claim-remove-entry") {
expect((await copies.get(companyId, runId))!).toMatchObject({ state: "unavailable", errorCode: "AGENT_FILES_SAVE_FAILED" });
expect(await readFile(join(canonical, "AGENTS.md"), "utf8")).toBe("Retain this exact directory.\n");
}
if (ending === "claim-canonical") expect(await readFile(join(canonical, "AGENTS.md"), "utf8")).toBe("Canonical edit before next claim\n");
// Heartbeat rematerializes only after the successor proved retirement.
active = await copies.prepare({ companyId, agentId, runId, target, cwd: remoteCwd });
expect(active).toMatchObject({ state: "prepared", executionRoot: originalRoot, processStoppedAt: null });
expect(active!.receipt?.retirementRequired).toBeUndefined();
expect(await copies.hasChanges({ companyId, runId, target })).toBe(false);
await copies.reportUnavailable(companyId, prior.binding.runId);
await copies.release(companyId, prior.binding.runId);
expect((await lstat(originalRoot)).isDirectory()).toBe(true);
await copies.collectStopped({ companyId, runId, target });
return;
}
expect(release).toBeTypeOf("function");
await copies.reportUnavailable(companyId, prior.binding.runId);
await copies.release(companyId, prior.binding.runId);
expect(active!.receipt).toMatchObject({ materializationRunId, cleanup: { leaseId: currentLease, remoteCwd } });
} else {
active = await copies.prepare({ companyId, agentId, runId, target, cwd: remoteCwd });
originalRoot = active!.executionRoot; materializationRunId = runId;
}
expect(active!.executionRoot).toBe(originalRoot);
current.runtimeContext = await buildNativeRuntimeContext({ db, agent, runId, runtimeConfig: {}, runtimeSkillEntries: [],
instructionWorkingCopy: { rootPath: active!.executionRoot, entryPath: "AGENTS.md", kind: "agent_files" } });
expect(current.runtimeContext.instructions.workingCopy?.rootPath).toBe(originalRoot);
await executePaperclipNativeSession({ db, execution: current, runnerInstanceId: environmentId, runnerExecutionTarget: target, instructionWorkingCopy: capability() });
if (prior) expect(state.execute.mock.calls.at(-1)?.[0].existingSession).toBe(session);
expect(closed).toBe(false);
await release?.(); expect(closed).toBe(false);
prior = current;
}
// A write after the live unchanged probe is still collected on retirement.
await writeFile(join(originalRoot, "late-memory.txt"), "Latest owner's remote bytes");
if (["missing-lease", "foreign-run", "foreign-environment", "deleted-environment"].includes(ending)) {
if (ending === "missing-lease") await db.delete(tables.environmentLeases).where(eq(tables.environmentLeases.id, currentLease));
if (ending === "foreign-run") await db.update(tables.environmentLeases).set({ heartbeatRunId: materializationRunId }).where(eq(tables.environmentLeases.id, currentLease));
if (ending === "foreign-environment") {
const other = randomUUID(); await db.insert(tables.environments).values({ id: other, name: "Other", driver: "sandbox" });
await db.update(tables.environmentLeases).set({ environmentId: other }).where(eq(tables.environmentLeases.id, currentLease));
}
if (ending === "deleted-environment") await db.delete(tables.environments).where(eq(tables.environments.id, environmentId));
const before = commands.length;
await closeWarmNativeSessionsForEnvironment({ environmentId, reason: "lease identity changed" });
expect(commands).toHaveLength(before);
const pending = (await copies.get(companyId, prior!.binding.runId))!;
expect(pending.state).toBe("pending_collection");
expect(pending.receipt?.baseline).toBeDefined();
expect(await readFile(join(originalRoot, "late-memory.txt"), "utf8")).toBe("Latest owner's remote bytes");
return;
}
if (ending === "close-failure") {
session.close.mockImplementationOnce(async () => { throw new Error("owned close not yet confirmed"); });
const before = commands.length;
expect(await closeWarmNativeSessionsForEnvironment({ environmentId, reason: "first retirement" })).toMatchObject({ failed: 1 });
await copies.reportUnavailable(companyId, prior!.binding.runId);
await copies.reportUnavailable(companyId, materializationRunId);
await copies.release(companyId, prior!.binding.runId);
await copies.release(companyId, materializationRunId);
expect(await copies.get(companyId, prior!.binding.runId)).toMatchObject({ state: "pending_collection", errorCode: "INSTRUCTION_STOP_UNCONFIRMED", processStoppedAt: null });
expect(commands).toHaveLength(before);
expect(await readFile(join(originalRoot, "late-memory.txt"), "utf8")).toBe("Latest owner's remote bytes");
expect(await closeWarmNativeSessionsForEnvironment({ environmentId, reason: "verified later retirement" })).toMatchObject({ closed: 1, failed: 0 });
expect(await copies.get(companyId, prior!.binding.runId)).toMatchObject({ state: "saved", receipt: { cleanupPending: false } });
expect(await readFile(join(canonical, "late-memory.txt"), "utf8")).toBe("Latest owner's remote bytes");
await expect(access(originalRoot)).rejects.toMatchObject({ code: "ENOENT" });
return;
}
if (ending !== "collect") {
const { remoteTerminationReceipt } = await import("../remote-execution-termination.js");
const [lease] = await db.select().from(tables.environmentLeases).where(eq(tables.environmentLeases.id, currentLease));
await db.update(tables.environmentLeases).set({ status: "released", releasedAt: new Date(), cleanupStatus: "success",
metadata: { ...lease!.metadata, remoteExecutionTermination: remoteTerminationReceipt(lease!, { providerLeaseId: lease!.providerLeaseId, state: ending }) } }).where(eq(tables.environmentLeases.id, currentLease));
await db.update(tables.heartbeatRuns).set({ status: "succeeded" }).where(eq(tables.heartbeatRuns.id, prior!.binding.runId));
const before = commands.length;
await copies.recoverStopped(); // The original remote collector is still cached.
expect(commands).toHaveLength(before);
const recovered = (await copies.get(companyId, prior!.binding.runId))!;
expect(recovered.state).toBe(ending === "destroyed" ? "unavailable" : "pending_collection");
await expect(access(join(canonical, "late-memory.txt"))).rejects.toMatchObject({ code: "ENOENT" });
if (ending === "stopped") {
expect(await readFile(join(originalRoot, "late-memory.txt"), "utf8")).toBe("Latest owner's remote bytes");
expect(recovered.receipt?.baseline).toBeDefined();
await copies.collectStopped({ companyId, runId: prior!.binding.runId });
await copies.release(companyId, prior!.binding.runId);
expect(commands).toHaveLength(before);
} else await expect(access(active!.localRoot)).rejects.toMatchObject({ code: "ENOENT" });
await closeWarmNativeSessionsForEnvironment({ environmentId, reason: "already terminated owner" });
expect(commands).toHaveLength(before);
return;
}
failCleanup = true;
await closeWarmNativeSessionsForEnvironment({ environmentId, reason: "test retirement" });
expect(session.close).toHaveBeenCalledOnce();
expect(await readFile(join(canonical, "late-memory.txt"), "utf8")).toBe("Latest owner's remote bytes");
const saved = (await copies.get(companyId, prior!.binding.runId))!;
expect(saved).toMatchObject({ state: "saved", receipt: { cleanupPending: true, materializationRunId, cleanup: { leaseId: currentLease } } });
const cleanup = vi.fn(async input => {
expect(input.lease.id).toBe(currentLease);
expect(input.lease.heartbeatRunId).toBe(prior!.binding.runId);
return commandRunner(currentLease).execute(input);
});
copies = agentInstructionWorkingCopyService(db, { environmentRuntime: { execute: cleanup } as never });
await db.update(tables.agentInstructionWorkingCopies).set({ nextAttemptAt: null }).where(eq(tables.agentInstructionWorkingCopies.runId, prior!.binding.runId));
await copies.recoverCaptured();
expect(cleanup).toHaveBeenCalledOnce();
await expect(access(originalRoot)).rejects.toMatchObject({ code: "ENOENT" });
expect((await copies.get(companyId, prior!.binding.runId))!.receipt?.cleanupPending).toBe(false);
} catch (error) { bodyFailure = error; throw error; }
finally {
const cleanupFailures: unknown[] = [];
try { await closeWarmNativeSessionsForEnvironment({ environmentId, reason: "test cleanup" }); } catch (error) { cleanupFailures.push(error); }
try { await database.cleanup(); } catch (error) { cleanupFailures.push(error); }
if (previousHome === undefined) delete process.env.PAPERCLIP_HOME; else process.env.PAPERCLIP_HOME = previousHome;
try {
const writable = async (directory: string): Promise<void> => {
await chmod(directory, 0o700);
for (const entry of await readdir(directory, { withFileTypes: true })) if (entry.isDirectory()) await writable(join(directory, entry.name));
};
await writable(home); await rm(home, { recursive: true, force: true });
} catch (error) { cleanupFailures.push(error); }
if (cleanupFailures.length) throw new AggregateError([...(bodyFailure ? [bodyFailure] : []), ...cleanupFailures], "Warm DB test body/cleanup failure");
}
}, 90_000);
it.each(["idle", "edit", "add", "remove", "projectless"])("retains the actual agent_files copy across warm turns, then retires for %s", async ending => {
const root = await realpath(await mkdtemp(join(tmpdir(), "warm-agent-files-")));
const localRoot = join(root, "run", "live");
await mkdir(localRoot, { recursive: true });
await writeFile(join(localRoot, "AGENTS.md"), "Keep the registered directory.\n");
const snapshot = await captureDirectorySnapshot(localRoot);
let row = { companyId: execution.binding.companyId, agentId: execution.binding.agentId,
runId: "warm-real-agent-files", localRoot, executionRoot: localRoot, location: "local",
entryFile: "AGENTS.md", state: "prepared", attempts: 0, processStoppedAt: null,
baseRevisionId: null, candidateBase64: null, candidateHash: null, errorCode: null, errorMessage: null, nextAttemptAt: null,
responsibleUserId: "test-user", createdAt: new Date(), updatedAt: new Date(),
baseHash: directorySnapshotSha256(snapshot), receipt: { schema: AGENT_FILES_CONTRACT, baseline: serializeDirectorySnapshot(snapshot), materializationIdentity: probeAgentDirectory(localRoot).identity },
} as typeof agentInstructionWorkingCopies.$inferSelect;
const copies = agentDirectoryWorkingCopyService({} as Db, async () => row,
async (_prior, values) => (row = { ...row, ...values } as typeof row));
const order: string[] = [];
const close = vi.fn(async () => { order.push("closed"); });
const collectStopped = async () => {
order.push("collected");
expect(close).toHaveBeenCalledOnce();
// The real DB service suite verifies changed-file persistence. Here the
// fake backend proves stop precedes granting its collection callback.
if (ending === "idle" || ending === "projectless") await copies.collectStopped(row);
};
const identity = "warm-real-agent-files";
const warmExecution = { ...execution, binding: { ...execution.binding, runId: identity, executionWorkspaceId: ending === "projectless" ? identity : `${identity}-workspace` },
session: { ...execution.session, normalizedSessionId: identity, lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: 60_000 } },
} as NativeExecutionInputV1;
const session = { close };
state.execute.mockReset().mockImplementation(async (options) => {
await options.onSession?.(session);
return { result: { summary: "completed" }, terminal: { runTerminalState: "succeeded" },
turnId: identity, normalizedSessionId: identity, providerSessionId: identity,
driverKind: "test", driverVersion: "1", nativeEventCount: 1, highestContiguousSourceSeq: 1, usage: null };
});
try {
await executePaperclipNativeSession({ db: leaseDb(warmExecution), execution: warmExecution,
runnerInstanceId: identity, runnerExecutionTarget: { kind: "remote", transport: "sandbox", environmentId: identity, remoteCwd: `/tmp/${identity}` },
instructionWorkingCopy: { runId: identity, root: localRoot, preparationKey: "same-preparation", hasChanges: () => copies.hasChanges(row), collectStopped } });
expect(close).not.toHaveBeenCalled();
expect(order).toEqual([]);
expect(await readFile(join(localRoot, "AGENTS.md"), "utf8")).toContain("registered");
const second = { ...warmExecution, binding: { ...warmExecution.binding, runId: `${identity}-second`,
...(ending === "projectless" ? { executionWorkspaceId: `${identity}-second` } : {}) } };
expect(nativeSessionWorkspaceScope(second)).toEqual(nativeSessionWorkspaceScope(warmExecution));
const currentCopy = { runId: second.binding.runId, root: localRoot, preparationKey: "same-preparation", hasChanges: () => copies.hasChanges(row), collectStopped };
if (ending === "projectless") {
const adopt = vi.fn(async () => currentCopy);
for (const changed of [
{ ...second.workspace, cwd: "/different" }, { ...second.workspace, repoUrl: "https://example.test/other" },
{ ...second.workspace, repoRef: "other" }, { ...second.workspace, branchName: "other" },
]) expect(await claimWarmNativeInstructionCopy({ priorExecution: warmExecution,
companyId: second.binding.companyId, agentId: second.binding.agentId, executionWorkspaceId: second.binding.runId,
workspace: changed, environmentId: identity, runId: second.binding.runId, preparationKey: "same-preparation", adopt })).toBeNull();
expect(await claimWarmNativeInstructionCopy({ priorExecution: warmExecution,
companyId: second.binding.companyId, agentId: second.binding.agentId, executionWorkspaceId: "managed-workspace",
workspace: second.workspace, environmentId: identity, runId: second.binding.runId, preparationKey: "same-preparation", adopt })).toBeNull();
expect(adopt).not.toHaveBeenCalled(); expect(close).not.toHaveBeenCalled();
}
const release = await claimWarmNativeInstructionCopy({ priorExecution: warmExecution,
companyId: warmExecution.binding.companyId, agentId: warmExecution.binding.agentId,
executionWorkspaceId: second.binding.executionWorkspaceId, workspace: second.workspace,
environmentId: identity, runId: second.binding.runId, preparationKey: "same-preparation",
adopt: async () => currentCopy });
expect(release).toBeTypeOf("function");
await executePaperclipNativeSession({ db: leaseDb(second), execution: second, runnerInstanceId: identity,
runnerExecutionTarget: { kind: "remote", transport: "sandbox", environmentId: identity, remoteCwd: `/tmp/${identity}` },
instructionWorkingCopy: currentCopy });
expect(state.execute.mock.calls.at(-1)?.[0].existingSession).toBe(session);
expect(close).not.toHaveBeenCalled();
expect(row.executionRoot).toBe(localRoot);
await release?.(); // Consumed preparation cannot retire the retained owner.
expect(close).not.toHaveBeenCalled();
if (ending === "idle" || ending === "projectless") {
await closeWarmNativeSessionsForEnvironment({ environmentId: identity, reason: "idle retirement" });
await expect(access(localRoot)).rejects.toMatchObject({ code: "ENOENT" });
} else {
if (ending === "edit") await writeFile(join(localRoot, "AGENTS.md"), "Changed instructions");
if (ending === "add") await writeFile(join(localRoot, "memory.txt"), "New memory");
if (ending === "remove") await rm(join(localRoot, "AGENTS.md"));
const adopt = vi.fn(async () => ({ ...currentCopy, runId: `${identity}-third` }));
expect(await claimWarmNativeInstructionCopy({ priorExecution: second,
companyId: second.binding.companyId, agentId: second.binding.agentId,
executionWorkspaceId: second.binding.executionWorkspaceId, workspace: second.workspace,
environmentId: identity, runId: `${identity}-third`, preparationKey: "same-preparation", adopt })).toBeNull();
expect(adopt).toHaveBeenCalledOnce();
}
expect(order).toEqual(["closed", "collected"]);
} finally {
await closeWarmNativeSessionsForEnvironment({ environmentId: identity, reason: "test cleanup" });
await rm(root, { recursive: true, force: true });
}
});
it.each(["final-config", "changed-root", "late-edit", "close-failure", "abandon", "concurrent", "foreign-company", "foreign-workspace"])("fences owned instruction preparation: %s", async scenario => {
const identity = `warm-copy-${scenario}`;
const order: string[] = [];
let allowClose = scenario !== "close-failure";
const session = { close: vi.fn(async () => { order.push("close"); if (!allowClose) throw new Error("retirement unconfirmed"); }) };
const retirementFailed = vi.fn(async () => {});
let lateEdit = false;
const copy = { runId: identity, root: `/tmp/${identity}/instructions`, preparationKey: "key", hasChanges: async () => lateEdit,
collectStopped: vi.fn(async () => { order.push("collect"); }), retirementFailed };
const first = { ...execution, binding: { ...execution.binding, runId: identity, executionWorkspaceId: `${identity}-workspace` },
session: { ...execution.session, normalizedSessionId: identity, lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: 60_000 } },
} as NativeExecutionInputV1;
const target = { kind: "remote" as const, transport: "sandbox" as const, environmentId: identity, remoteCwd: `/tmp/${identity}` };
state.execute.mockReset().mockImplementation(async options => {
await options.onSession?.(session);
return { result: { summary: "completed" }, terminal: { runTerminalState: "succeeded" },
turnId: identity, normalizedSessionId: identity, providerSessionId: identity,
driverKind: "test", driverVersion: "1", nativeEventCount: 1, highestContiguousSourceSeq: 1, usage: null };
});
const second = { ...first, binding: { ...first.binding, runId: `${identity}-second` } };
const current = { ...copy, runId: second.binding.runId,
...(scenario === "changed-root" ? { root: `/tmp/${identity}/other-instructions` } : {}) };
const adopt = vi.fn(async () => current);
const claim = { priorExecution: first, companyId: first.binding.companyId, agentId: first.binding.agentId,
executionWorkspaceId: first.binding.executionWorkspaceId, workspace: first.workspace, environmentId: identity,
runId: second.binding.runId, preparationKey: "key", adopt };
try {
await executePaperclipNativeSession({ db: leaseDb(first), execution: first, runnerInstanceId: identity, runnerExecutionTarget: target, instructionWorkingCopy: copy });
if (scenario.startsWith("foreign")) {
expect(await claimWarmNativeInstructionCopy({ ...claim,
...(scenario === "foreign-company" ? { companyId: "other" } : { executionWorkspaceId: "other" }) })).toBeNull();
expect(adopt).not.toHaveBeenCalled(); expect(session.close).not.toHaveBeenCalled();
} else {
const release = await claimWarmNativeInstructionCopy(claim);
if (scenario === "concurrent") await expect(claimWarmNativeInstructionCopy({ ...claim, runId: "competitor" })).rejects.toThrow("busy");
if (scenario === "final-config" || scenario === "changed-root" || scenario === "late-edit") {
lateEdit = scenario === "late-edit";
const changed = scenario !== "final-config" ? second : { ...second, session: { ...second.session, lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: 99_000 } } };
await expect(executePaperclipNativeSession({ db: leaseDb(changed), execution: changed, runnerInstanceId: identity,
runnerExecutionTarget: target, instructionWorkingCopy: current })).rejects.toThrow(scenario === "late-edit"
? "native_instruction_preparation_copy_changed" : "native_instruction_preparation_configuration_changed");
expect(state.execute).toHaveBeenCalledOnce();
}
if (scenario === "close-failure") {
await expect(release!()).rejects.toThrow("retirement unconfirmed");
expect(retirementFailed).toHaveBeenCalledOnce();
expect(copy.collectStopped).not.toHaveBeenCalled();
expect(order).toEqual(["close"]);
} else {
await release?.();
expect(order).toEqual(["close", "collect"]);
}
}
} finally { allowClose = true; await closeWarmNativeSessionsForEnvironment({ environmentId: identity, reason: "test cleanup" }); }
});
it.each(["config", "reset", "changed", "unavailable", "auth", "wrong-capability", "retirement-failure"])("hands off collection before probing or retiring a warm owner: %s", async scenario => {
const identity = `warm-successor-${scenario}`, order: string[] = [];
let allowClose = scenario !== "retirement-failure";
const session = { close: vi.fn(async () => { order.push("stop"); if (!allowClose) throw new Error("stop unconfirmed"); }) };
const priorCopy = { runId: identity, preparationKey: "before", hasChanges: vi.fn(async () => false),
collectStopped: vi.fn(async () => { order.push("prior-pending"); }), retirementFailed: vi.fn(async () => {}) };
const first = { ...execution, binding: { ...execution.binding, runId: identity, executionWorkspaceId: `${identity}-workspace` },
session: { ...execution.session, normalizedSessionId: identity, lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: 60_000 } } } as NativeExecutionInputV1;
const target = { kind: "remote" as const, transport: "sandbox" as const, environmentId: identity, remoteCwd: `/tmp/${identity}` };
state.execute.mockReset().mockImplementation(async options => {
await options.onSession?.(session);
return { result: { summary: "completed" }, terminal: { runTerminalState: "succeeded" }, turnId: identity,
normalizedSessionId: identity, providerSessionId: identity, driverKind: "test", driverVersion: "1",
nativeEventCount: 1, highestContiguousSourceSeq: 1, usage: null };
});
const current = { runId: `${identity}-next`, preparationKey: scenario === "config" ? "after" : "before",
hasChanges: vi.fn(async () => { order.push("current-probe"); return true; }),
collectStopped: vi.fn(async () => { order.push("current-collect"); }), retirementFailed: vi.fn(async () => {}) };
const adopt = vi.fn(async () => {
order.push("handoff");
if (scenario === "auth") throw new Error("authorization denied");
if (scenario === "unavailable") return null;
return scenario === "wrong-capability" ? { ...current, runId: "foreign" } : current;
});
try {
await executePaperclipNativeSession({ db: leaseDb(first), execution: first, runnerInstanceId: identity, runnerExecutionTarget: target, instructionWorkingCopy: priorCopy });
priorCopy.hasChanges.mockClear();
const claiming = claimWarmNativeInstructionCopy({ priorExecution: first, companyId: first.binding.companyId, agentId: first.binding.agentId,
executionWorkspaceId: first.binding.executionWorkspaceId, workspace: first.workspace, environmentId: identity,
runId: current.runId, preparationKey: current.preparationKey, forceRetirement: scenario === "reset", adopt });
if (["unavailable", "auth", "wrong-capability", "retirement-failure"].includes(scenario)) {
await expect(claiming).rejects.toThrow(({ unavailable: "handoff_unavailable", auth: "authorization denied",
"wrong-capability": "handoff_mismatch", "retirement-failure": "stop unconfirmed" } as Record<string, string>)[scenario]);
} else expect(await claiming).toBeNull();
expect(priorCopy.hasChanges).not.toHaveBeenCalled();
expect(state.execute).toHaveBeenCalledOnce();
if (["unavailable", "auth", "wrong-capability"].includes(scenario)) {
expect(order).toEqual(["handoff", "stop", "prior-pending"]);
expect(current.collectStopped).not.toHaveBeenCalled();
} else {
expect(priorCopy.collectStopped).not.toHaveBeenCalled();
expect(order).toEqual(["handoff", ...(["config", "reset"].includes(scenario) ? [] : ["current-probe"]), "stop", ...(scenario === "retirement-failure" ? [] : ["current-collect"])]);
if (scenario === "retirement-failure") {
expect(current.retirementFailed).toHaveBeenCalledOnce();
expect(current.collectStopped).not.toHaveBeenCalled();
}
}
} finally { allowClose = true; await closeWarmNativeSessionsForEnvironment({ environmentId: identity, reason: "test cleanup" }); }
});
it.each([
{ change: "addition", before: {}, after: { CUSTOM_TOKEN: "first" }, replaced: true },
{ change: "rotation", before: { CUSTOM_TOKEN: "first" }, after: { CUSTOM_TOKEN: "second" }, replaced: true },
@@ -7817,7 +8504,7 @@ describe("native warm session supervision", () => {
});
});
it.each(["permission", "managed credential"])("replaces an idle warm provider session when its %s changes", async (change) => {
it.each(["permission", "managed credential", "ACPX runtime contract", "unchanged Codex runtime contract"])("checks warm owner compatibility for %s", async (change) => {
const firstClose = vi.fn(async () => undefined);
const secondClose = vi.fn(async () => undefined);
const firstSession = { close: firstClose };
@@ -7825,7 +8512,9 @@ describe("native warm session supervision", () => {
const base = {
...execution,
schema: "paperclip.native-execution-input.v4",
provider: { kind: "codex", model: null, approvalPolicy: "never" },
provider: change === "ACPX runtime contract"
? { kind: "acpx", agent: "claude", model: "claude-sonnet-5", permissionMode: "approve-all" }
: { kind: "codex", model: null, approvalPolicy: "never" },
binding: {
...execution.binding,
runId: "run-permission-never",
@@ -7839,7 +8528,7 @@ describe("native warm session supervision", () => {
},
session: {
normalizedSessionId: "session-warm-permission",
driverKind: "codex_app_server" as const,
driverKind: change === "ACPX runtime contract" ? "acpx_runtime" as const : "codex_app_server" as const,
protocolVersion: 1 as const,
lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: 20 },
},
@@ -7862,6 +8551,12 @@ describe("native warm session supervision", () => {
highestContiguousSourceSeq: 1,
usage: null,
};
const runtimeChange = change === "ACPX runtime contract" || change === "unchanged Codex runtime contract";
const keepsOwner = change === "unchanged Codex runtime contract";
const currentRuntimeContract = nativeSessionResume.nativeRuntimeContractForProvider(base.provider);
const contract = runtimeChange
? vi.spyOn(nativeSessionResume, "nativeRuntimeContractForProvider").mockReturnValue(undefined)
: undefined;
state.execute
.mockReset()
.mockImplementationOnce(async (options) => {
@@ -7870,8 +8565,8 @@ describe("native warm session supervision", () => {
return result;
})
.mockImplementationOnce(async (options) => {
expect(options.existingSession).toBeUndefined();
options.onSession?.(secondSession);
expect(options.existingSession).toBe(keepsOwner ? firstSession : undefined);
if (!keepsOwner) options.onSession?.(secondSession);
return result;
});
@@ -7885,21 +8580,22 @@ describe("native warm session supervision", () => {
managedAiCredentialIdentity: "first-identity",
runnerInstanceId: "runner",
});
contract?.mockReturnValue(currentRuntimeContract);
await executePaperclipNativeSession({
db: leaseDb(lowered),
execution: lowered,
managedAiCredentialIdentity: change === "managed credential" ? "second-identity" : "first-identity",
runnerInstanceId: "runner",
});
expect(firstClose).toHaveBeenCalledWith({
reason: "warm native session configuration changed",
});
if (keepsOwner) expect(firstClose).not.toHaveBeenCalled();
else expect(firstClose).toHaveBeenCalledWith({ reason: "warm native session configuration changed" });
expect(secondClose).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(20);
expect(secondClose).toHaveBeenCalledWith({
expect(keepsOwner ? firstClose : secondClose).toHaveBeenCalledWith({
reason: "warm native session idle timeout",
});
} finally {
contract?.mockRestore();
vi.useRealTimers();
}
});
@@ -8723,6 +9419,11 @@ describe("native process ownership", () => {
},
"acpx_runtime",
],
[
"Pi ACPX without candidate authorization",
{ kind: "acpx", agent: "pi", model: "openrouter/deepseek/deepseek-v4-flash-0731", permissionMode: "approve-reads" },
"acpx_runtime",
],
])(
"admits the qualified %s provider",
async (_name, provider, driverKind) => {
@@ -8761,7 +9462,7 @@ describe("native process ownership", () => {
},
);
it.each(["pi", "copilot"])("rejects ACPX candidate %s without host authorization before constructing a backend", async (agent) => {
it.each(["copilot"])("rejects ACPX candidate %s without host authorization before constructing a backend", async (agent) => {
const piExecution = {
...execution,
binding: { ...execution.binding, runId: "run-acpx-pi-rejected" },
@@ -1,6 +1,6 @@
import { configuredEnvironment } from "../../vendor/paperclip-runner/index.js";
import { agents } from "@paperclipai/db";
import { dotRunnerBroker } from "../dot-runner-broker.js";
import { configuredEnvironment } from "../../vendor/paperclip-runner/index.js";
import { CURSOR_DISTRIBUTION_PINS, QUALIFIED_ACPX_PROFILES, QUALIFIED_ACPX_VERSION } from "../../vendor/paperclip-runner/index.js";
import { isProviderMode } from "../../vendor/paperclip-runner/index.js";
import { bundledRemoteProviderPackManifestPath, bundledRemoteRunnerBinary } from "../../vendor/paperclip-runner/index.js";
@@ -38,6 +38,7 @@ import { nativeCompletionFeedback } from "./native-completion-feedback.js";
import { hasAcknowledgedNativeReassignmentStopIntent, hasAcknowledgedNativeStopIntent } from "../acknowledged-native-stop.js";
import { stoppedCodexTurnIsTextOnly } from "./stopped-codex-turn.js";
import { prepareVerifiedRemoteProviderPack } from "./remote-provider-pack.js";
import { selectRemotePiCompanion } from "./remote-pi-companion.js";
import { readNativeLocalProcessStop, PROCESS_START_REQUESTED } from "../native-local-process-stop.js";
import { remoteLeaseCleanupScope } from "../remote-execution-termination.js";
import { resolveConnectorAssignments, isConnectorSkill } from "../connector-runtime.js";
@@ -427,7 +428,23 @@ function clearNativeRuntimeRequestResolutions(runId: string): void {
}
}
export type NativeInstructionWorkingCopy = {
/** Present only for a complete registered agent_files materialization. */
runId?: string;
preparationKey?: string;
root?: string;
checkpointWarm?: () => Promise<boolean>;
hasChanges: () => Promise<boolean>;
collectStopped: () => Promise<void>;
retirementFailed?: () => Promise<void>;
};
type WarmNativeSession = {
instructionWorkingCopy?: NativeInstructionWorkingCopy;
// Keep the admitted physical root independent of the per-run collection
// capability, which instruction preparation adopts before final admission.
instructionRoot?: string;
instructionPreparationRunId?: string;
agentId: string;
instructionCopy?: { runId: string; root?: string; targetIdentity: string; collectStopped: () => Promise<void> };
preparingRunId?: string;
@@ -453,7 +470,13 @@ async function closeWarmNativeSession(entry: WarmNativeSession, reason: string,
// Revoke before awaiting process retirement/checkpoint IO.
const stopping = entry.githubAccess?.stop();
try {
await entry.session.close({ reason });
try { await entry.session.close({ reason }); }
catch (error) {
try { await entry.instructionWorkingCopy?.retirementFailed?.(); }
catch (receiptError) { throw new AggregateError([error, receiptError], "Warm instruction retirement and receipt both failed"); }
throw error;
}
await entry.instructionWorkingCopy?.collectStopped();
if (entry.instructionCopy?.runId !== preserveInstructionsForRunId) await entry.instructionCopy?.collectStopped();
}
finally { await stopping; }
@@ -464,6 +487,65 @@ const warmNativeSessions = new Map<string, WarmNativeSession>();
// not inspect or quarantine that owner's state until the save has finished.
const closingWarmNativeSessions = new Map<string, Promise<void>>();
/** Reserve the existing physical copy before heartbeat composes AGENT_HOME and
* task guidance. The final execution/configuration fence still owns admission. */
export async function claimWarmNativeInstructionCopy(input: {
priorExecution: NativeExecutionInput;
companyId: string;
agentId: string;
executionWorkspaceId: string;
workspace: NativeExecutionInput["workspace"];
environmentId: string | null;
runId: string;
preparationKey: string;
forceRetirement?: boolean;
adopt: (previousRunId: string) => Promise<NativeInstructionWorkingCopy | null>;
}): Promise<(() => Promise<void>) | null> {
const prior = input.priorExecution;
if (prior.binding.companyId !== input.companyId || prior.binding.agentId !== input.agentId
|| JSON.stringify(nativeSessionWorkspaceScope(prior)) !== JSON.stringify(nativeSessionWorkspaceScope({
binding: { ...prior.binding, runId: input.runId, executionWorkspaceId: input.executionWorkspaceId }, workspace: input.workspace,
})) || prior.workspace.cwd !== input.workspace.cwd) return null;
const scope = nativeSessionScopeKey(prior);
const entry = warmNativeSessions.get(scope);
if (!entry || !entry.instructionWorkingCopy?.runId || entry.environmentId !== input.environmentId) return null;
if (entry.busy) throw new Error("native_session_supervisor_busy");
entry.busy = true;
if (entry.idleTimer !== null) clearTimeout(entry.idleTimer);
entry.idleTimer = null;
const retire = async (reason: string) => {
if (warmNativeSessions.get(scope) !== entry) return;
entry.closeOnReleaseReason = reason;
try {
await closeWarmNativeSession(entry, reason);
if (warmNativeSessions.get(scope) === entry) warmNativeSessions.delete(scope);
} finally { entry.busy = false; }
};
try {
const preparationChanged = input.forceRetirement === true || entry.instructionWorkingCopy.preparationKey !== input.preparationKey;
// The prior capability may close over an expired remote lease. Rebind
// collection authority first; adoption itself grants no reuse permission.
const adopted = await input.adopt(entry.instructionWorkingCopy.runId);
if (!adopted) {
await retire("warm instruction materialization cannot be handed off");
throw new Error("native_instruction_materialization_handoff_unavailable");
}
if (adopted.runId !== input.runId || adopted.preparationKey !== input.preparationKey) throw new Error("native_instruction_materialization_handoff_mismatch");
entry.instructionWorkingCopy = adopted;
entry.instructionPreparationRunId = input.runId;
if (entry.closeOnReleaseReason !== undefined || preparationChanged || await adopted.hasChanges()) {
await retire("warm instruction materialization changed before preparation");
return null;
}
return async () => {
if (entry.instructionPreparationRunId === input.runId) await retire("warm instruction preparation abandoned");
};
} catch (error) {
if (entry.closeOnReleaseReason === undefined) await retire("warm instruction preparation failed");
throw error;
}
}
function instructionTargetIdentity(target?: AdapterExecutionTarget | null): string {
return JSON.stringify(target?.kind === "remote" ? {
environmentId: target.environmentId, cwd: target.remoteCwd,
@@ -560,19 +642,23 @@ async function closeIdleWarmNativeSessions(input: {
continue;
}
if (entry.idleTimer !== null) clearTimeout(entry.idleTimer);
// Remove ownership before awaiting close so a racing continuation cannot
// adopt a session whose transport is already shutting down.
warmNativeSessions.delete(sessionId);
// Keep a fenced owner until both retirement and stopped collection succeed.
// Failure must not make a live directory eligible for a replacement owner.
entry.busy = true;
entry.closeOnReleaseReason = input.reason;
const closing = Promise.resolve().then(() =>
closeWarmNativeSession(entry, input.reason),
);
closingWarmNativeSessions.set(sessionId, closing);
try {
await closing;
if (warmNativeSessions.get(sessionId) === entry) warmNativeSessions.delete(sessionId);
closed += 1;
} catch {
if (!entry.instructionWorkingCopy?.runId) warmNativeSessions.delete(sessionId);
failed += 1;
} finally {
entry.busy = false;
if (closingWarmNativeSessions.get(sessionId) === closing) {
closingWarmNativeSessions.delete(sessionId);
}
@@ -1166,6 +1252,7 @@ export function createGovernedWaitEventObservation(
resolvePending: () => Promise<PrpStructuredRunResult | null>,
) {
const pendingTools = new Set<string>();
let providerLost = false;
let generation = 0;
let observation: {
sourceInstanceId: string;
@@ -1179,6 +1266,7 @@ export function createGovernedWaitEventObservation(
const currentGeneration = ++generation;
observation = null;
const payload = record(event.payload);
providerLost ||= event.eventType === "runtime_request.expired" && payload.reason === "provider_process_lost";
const kind = payload.kind;
const tool = ["dynamicToolCall", "mcpToolCall", "commandExecution"].includes(String(kind));
if (event.itemId) {
@@ -1194,7 +1282,9 @@ export function createGovernedWaitEventObservation(
if (event.eventType === "item.completed" && (
pendingTools.size > 0 || (!tool && !(kind === "agentMessage" && payload.channel === "final"))
)) return;
if (!eligible) return;
// A replacement question preserves human input after a provider crash;
// it does not authorize a successful suspension of the failed execution.
if (providerLost || !eligible) return;
const result = await resolvePending();
if (generation !== currentGeneration || result === null) return;
// If the interaction is answered after this read, parking remains the
@@ -1574,8 +1664,8 @@ function nativeSessionKey(execution: NativeExecutionInput): string {
);
}
function nativeSessionWorkspaceScope(execution: NativeExecutionInput) {
if (execution.provider.kind === "openai_dot") return { kind: "none" as const };
export function nativeSessionWorkspaceScope(execution: { binding: Pick<NativeExecutionInput["binding"], "runId" | "executionWorkspaceId">; workspace: NativeExecutionInput["workspace"] }) {
if ("access" in execution.workspace) return { kind: "none" as const };
// Projectless local runs use the heartbeat run id as a durable placeholder
// rather than fabricating an execution_workspaces row. Do not let that
// per-run placeholder break continuity for the same provider session; the
@@ -5326,7 +5416,7 @@ function hasIdleWarmNativeSessionOwner(input: {
}): boolean {
if (input.execution.session.lifecyclePolicy.mode !== "warm") return false;
const entry = warmNativeSessions.get(nativeSessionScopeKey(input.execution));
if (!entry || entry.busy) return false;
if (!entry || (entry.busy && entry.instructionPreparationRunId !== input.execution.binding.runId)) return false;
// A verified idle owner proves the checkpoint belongs to this session even
// when its process must later rotate to a new run-scoped broker capability.
// Local environments also have an id. Compare the same environment binding
@@ -5541,6 +5631,7 @@ export function providerSessionIdentityFromDurableProviderState(input: {
identity.effectiveModel !== expectedModel ||
identity.permissionMode !== input.execution.provider.permissionMode ||
!acpxRecoveryModeMatches(input.execution.provider, descriptor.mode, identity.mode) ||
!acpxRecoveryPiThinkingMatches(input.execution.provider, descriptor.piThinkingLevel, identity.piThinkingLevel) ||
!["approve-all", "approve-paperclip", "approve-reads", "deny-all"].includes(
String(identity.permissionMode),
) ||
@@ -5648,6 +5739,12 @@ function acpxRecoveryModeMatches(
&& observedModes.every(mode => mode === expected);
}
function acpxRecoveryPiThinkingMatches(provider: NativeExecutionInput["provider"], ...observed: unknown[]): boolean {
const expected = record(provider).piThinkingLevel;
if (provider.kind === "acpx" && provider.agent === "pi") return ["off", "low", "high", "max"].includes(String(expected)) && observed.every(value => value === expected);
return expected === undefined && observed.every(value => value === undefined);
}
export function providerSessionIdentityTransitionIsAllowed(input: {
execution: NativeExecutionInput;
previous: unknown;
@@ -5658,6 +5755,7 @@ export function providerSessionIdentityTransitionIsAllowed(input: {
record(record(input.previous).providerSessionIdentity).mode,
record(record(input.current).providerSessionIdentity).mode,
)) return false;
if (!acpxRecoveryPiThinkingMatches(input.execution.provider, record(record(input.previous).providerSessionIdentity).piThinkingLevel, record(record(input.current).providerSessionIdentity).piThinkingLevel)) return false;
if (canonicalJson(input.previous) === canonicalJson(input.current)) {
return true;
}
@@ -6054,23 +6152,27 @@ async function releaseWarmNativeSession(
entry.lastActivityAt = new Date().toISOString();
if (entry.idleTimer !== null) clearTimeout(entry.idleTimer);
if (failed || entry.closeOnReleaseReason !== undefined) {
warmNativeSessions.delete(sessionId);
const closing = closeWarmNativeSession(entry,
entry.closeOnReleaseReason ?? "warm native session failed");
// Restart checkpointing is required to restore this successful session.
// Surface failure instead of reporting a clean release without authority.
if (entry.closeOnReleaseReason !== undefined) await closing;
else await closing.catch(() => undefined);
const requireCleanClose = entry.closeOnReleaseReason !== undefined;
entry.closeOnReleaseReason ??= "warm native session failed";
entry.busy = true;
try {
await closeWarmNativeSession(entry, entry.closeOnReleaseReason);
if (warmNativeSessions.get(sessionId) === entry) warmNativeSessions.delete(sessionId);
} catch (error) {
if (!entry.instructionWorkingCopy?.runId) warmNativeSessions.delete(sessionId);
if (requireCleanClose) throw error;
} finally { entry.busy = false; }
return;
}
entry.idleTimer = setTimeout(() => {
const current = warmNativeSessions.get(sessionId);
// clearTimeout cannot revoke an already-queued callback. The entry object
// is the idle timer's ownership fence across a later warm acquisition.
if (current !== entry || current.busy) return;
warmNativeSessions.delete(sessionId);
void closeWarmNativeSession(current, "warm native session idle timeout")
.catch(() => undefined);
current.busy = true;
current.closeOnReleaseReason = "warm native session idle timeout";
void closeWarmNativeSession(current, current.closeOnReleaseReason)
.then(() => { if (warmNativeSessions.get(sessionId) === current) warmNativeSessions.delete(sessionId); })
.catch(() => { if (!current.instructionWorkingCopy?.runId && warmNativeSessions.get(sessionId) === current) warmNativeSessions.delete(sessionId); })
.finally(() => { current.busy = false; });
}, idleTimeoutMs);
entry.idleTimer.unref();
}
@@ -7347,15 +7449,9 @@ export async function executePaperclipNativeSession(input: {
chatAttachmentReadScope?: NativeChatAttachmentReadScope;
onLog?: (stream: "stdout" | "stderr", chunk: string) => Promise<void>;
onEvent?: (event: AdapterRuntimeEvent) => Promise<void>;
/** Only this run's registered directory. Validate a warm checkpoint at the
* terminal boundary, or collect after owned shutdown when it cannot settle. */
instructionWorkingCopy?: {
runId?: string;
root?: string;
checkpointWarm?: () => Promise<boolean>;
hasChanges: () => Promise<boolean>;
collectStopped: () => Promise<void>;
};
/** Only this run's registered private instruction entry.
* Probe at terminal; persist only after owned shutdown. */
instructionWorkingCopy?: NativeInstructionWorkingCopy;
/** Persist task-level continuity before a durable goal can outlive this run. */
onUsage?: (receipt: AdapterUsageCheckpoint) => Promise<void>;
onGoalCheckpoint?: (snapshot: PersistedNativeSession) => Promise<void>;
@@ -7571,7 +7667,7 @@ async function executePaperclipNativeSessionWithinScope(
}
if (
input.execution.provider.kind === "acpx" &&
["pi", "copilot"].includes(input.execution.provider.agent) &&
["copilot"].includes(input.execution.provider.agent) &&
!resolveAcpxQualification(input.execution.provider, process.env)
) {
throw new Error(
@@ -8352,7 +8448,7 @@ async function executePaperclipNativeSessionWithinScope(
entry.closeOnReleaseReason !== undefined ||
entry.configDigest !== warmConfigDigest ||
entry.configuredEnvironmentDigest !== configuredEnvironmentDigest ||
entry.instructionCopy?.root !== input.instructionWorkingCopy?.root ||
entry.instructionRoot !== input.instructionWorkingCopy?.root ||
entry.managedAiCredentialIdentity !== input.managedAiCredentialIdentity ||
credentialRunChanged ||
Boolean(entry.githubAccess) !== Boolean(input.managedGitHub) ||
@@ -8363,20 +8459,36 @@ async function executePaperclipNativeSessionWithinScope(
(input.runnerEnvironment?.PAPERCLIP_RUNNER_NETWORK_ACCESS ===
"enabled")
) {
if (entry.busy) throw new Error("native_session_supervisor_busy");
const preparedCopy = entry.instructionPreparationRunId === input.execution.binding.runId;
if (entry.busy && !preparedCopy) throw new Error("native_session_supervisor_busy");
if (entry.idleTimer !== null) clearTimeout(entry.idleTimer);
warmNativeSessions.delete(warmSessionId);
// Preparation may already have handed this directory to the incoming
// run. Retiring the old transport must not delete the new run's root;
// its own stop callback owns collection from this point forward.
await closeWarmNativeSession(entry, "warm native session configuration changed", input.execution.binding.runId);
entry.busy = true;
entry.closeOnReleaseReason = "warm native session configuration changed";
try {
await closeWarmNativeSession(entry, entry.closeOnReleaseReason, input.execution.binding.runId);
warmNativeSessions.delete(warmSessionId);
} finally { entry.busy = false; }
if (preparedCopy) throw new Error("native_instruction_preparation_configuration_changed");
persistedWarmSession = loadWarmNativeCheckpoint(
input.execution,
warmConfigDigest,
input.runnerExecutionTarget?.kind ?? "local",
);
} else {
if (entry.busy) throw new Error("native_session_supervisor_busy");
if (entry.busy && entry.instructionPreparationRunId !== input.execution.binding.runId) throw new Error("native_session_supervisor_busy");
if (entry.instructionWorkingCopy?.runId && entry.instructionPreparationRunId !== input.execution.binding.runId) {
throw new Error("native_instruction_materialization_not_claimed");
}
if (entry.instructionPreparationRunId !== undefined && await entry.instructionWorkingCopy!.hasChanges()) {
entry.closeOnReleaseReason = "warm instruction copy changed during preparation";
try {
await closeWarmNativeSession(entry, entry.closeOnReleaseReason);
warmNativeSessions.delete(warmSessionId);
} finally { entry.busy = false; }
throw new Error("native_instruction_preparation_copy_changed");
}
entry.instructionPreparationRunId = undefined;
entry.instructionWorkingCopy = input.instructionWorkingCopy?.checkpointWarm ? undefined : input.instructionWorkingCopy;
entry.busy = true;
entry.ownerToken = warmSessionOwnerToken;
entry.environmentId =
@@ -8729,6 +8841,8 @@ async function executePaperclipNativeSessionWithinScope(
existing.session = session;
} else
warmNativeSessions.set(warmSessionId, {
instructionWorkingCopy: input.instructionWorkingCopy?.checkpointWarm ? undefined : input.instructionWorkingCopy,
instructionRoot: input.instructionWorkingCopy?.root,
agentId: input.execution.binding.agentId,
managedAiCredentialIdentity: input.managedAiCredentialIdentity,
githubAuthenticationMode:
@@ -8756,7 +8870,7 @@ async function executePaperclipNativeSessionWithinScope(
lastActivityAt: new Date().toISOString(),
});
const owner = warmNativeSessions.get(warmSessionId);
if (owner && input.instructionWorkingCopy?.runId) owner.instructionCopy = {
if (owner && input.instructionWorkingCopy?.runId && input.instructionWorkingCopy.checkpointWarm) owner.instructionCopy = {
runId: input.instructionWorkingCopy.runId, root: input.instructionWorkingCopy.root, targetIdentity: instructionTargetIdentity(input.runnerExecutionTarget),
collectStopped: input.instructionWorkingCopy.collectStopped,
};
@@ -9453,7 +9567,8 @@ async function executePaperclipNativeSessionWithinScope(
.catch(() => undefined);
throw error;
}
const collectedByOwner = ownedSession?.instructionCopy?.collectStopped === instructionCopy?.collectStopped;
const collectedByOwner = ownedSession?.instructionCopy?.collectStopped === instructionCopy?.collectStopped
|| ownedSession?.instructionWorkingCopy?.collectStopped === instructionCopy?.collectStopped;
if (collectInstructions && ownedSession) {
// Keep the unchanged warm path intact. A changed private instruction copy
// requires the existing checkpoint-and-close boundary before collection.
@@ -9792,7 +9907,7 @@ type RemoteProviderPackManifest = {
distDigest: string;
bridgeDigest: string;
acpxProfileDigests: typeof REMOTE_PROVIDER_PACK_PROFILE_DIGESTS;
providers?: Partial<Record<"cursor", {
providers?: Partial<Record<"pi" | "cursor", {
version: string; profileDigest: string; closureDigest: string; qualification: "qualified" | "pending";
path: string; sha256: string;
}>>;
@@ -9981,9 +10096,10 @@ function readRemoteProviderPackIdentity(packRoot: string, verifyControllerFiles:
}
for (const [provider, candidate] of Object.entries(candidates)) {
const expectedPath = `provider-assets/${provider}/${payload.target.platform}-${payload.target.architecture}`;
if (!(inventory === "providers" ? ["cursor"] : ["cursor", "copilot", "pi"]).includes(provider) || !candidate
if (!(inventory === "providers" ? ["pi", "cursor"] : ["cursor", "copilot", "pi"]).includes(provider) || !candidate
|| Object.keys(candidate).some(key => !["version", "profileDigest", "closureDigest", "qualification", "path", "sha256"].includes(key))
|| candidate.qualification !== (provider === "cursor" ? "qualified" : "pending") || candidate.path !== expectedPath
|| candidate.qualification !== (["pi", "cursor"].includes(provider) ? "qualified" : "pending")
|| candidate.path !== expectedPath
|| typeof candidate.version !== "string" || !candidate.version || candidate.version.length > 120
|| !/^sha256:[a-f0-9]{64}$/.test(candidate.profileDigest)
|| !/^sha256:[a-f0-9]{64}$/.test(candidate.closureDigest)
@@ -11250,8 +11366,16 @@ async function createRunnerdBackendWithinSessionClaim(
remoteTarget !== null &&
(input.execution.provider.kind === "opencode" ||
input.execution.provider.kind === "acpx");
// Pi's explicit operator import supplies the target-platform authority. Never
// substitute a Mac controller daemon or trust an image's self-reported hashes.
// Existing explicit overrides and every other provider keep their old path.
const remotePiCompanion = await selectRemotePiCompanion({
provider: input.execution.provider, remote: remoteTarget !== null,
binaryOverride: input.runnerRemoteBinaryPath, packOverride: input.runnerRemoteProviderPackPath,
workspaceRoot: input.execution.workspace.cwd,
});
const configuredProviderPackRoot =
input.runnerRemoteProviderPackPath?.trim() || null;
input.runnerRemoteProviderPackPath?.trim() || remotePiCompanion?.providerPack || null;
let expectedProviderPackManifest: RemoteProviderPackManifest | null = null;
const useBundledCursorImageAssets = requiresRemoteProviderPack && !configuredProviderPackRoot &&
input.execution.provider.kind === "acpx" && input.execution.provider.agent === "cursor";
@@ -11284,7 +11408,7 @@ async function createRunnerdBackendWithinSessionClaim(
// When an explicit remote artifact is configured, prepareRemoteRunner stages
// these exact bytes at remoteBinary before launch.
const controllerRunnerBinary = remoteTarget
? input.runnerRemoteBinaryPath?.trim() || (useBundledCursorImageAssets ? bundledRemoteRunnerBinary() : resolvePaperclipRunnerBinary())
? input.runnerRemoteBinaryPath?.trim() || remotePiCompanion?.runnerBinary || (useBundledCursorImageAssets ? bundledRemoteRunnerBinary() : resolvePaperclipRunnerBinary())
: resolvePaperclipRunnerBinary();
const explicitRemoteCodex = input.runnerRemoteCodexPath?.trim() || null;
const remoteCodexNpmSpec = input.runnerRemoteCodexNpmSpec?.trim() || null;
@@ -11663,7 +11787,7 @@ async function createRunnerdBackendWithinSessionClaim(
if (!existsSync(sourceBinary)) {
throw new Error("runner_remote_artifact_unavailable");
}
if (!explicitRemoteBinary) {
if (!explicitRemoteBinary && !remotePiCompanion) {
const platform = await remoteCommandRunner.execute({
command: "sh",
args: ["-c", "uname -s; uname -m"],
@@ -12916,9 +13040,12 @@ async function createRunnerdBackendWithinSessionClaim(
? {
acpxAgent: input.execution.provider.agent,
// Read only the server operator environment, never agent/runtime env.
acpxCandidateProfile: resolveAcpxQualification(input.execution.provider, process.env),
acpxCandidateProfile: input.execution.provider.agent === "pi"
? undefined
: resolveAcpxQualification(input.execution.provider, process.env),
acpxPermissionMode: input.execution.provider.permissionMode,
acpxMode: input.execution.provider.mode,
piThinkingLevel: input.execution.provider.piThinkingLevel,
acpxPermissionModePinned:
input.execution.schema === "paperclip.native-execution-input.v4" ||
input.execution.schema === "paperclip.native-execution-input.v5",
@@ -734,6 +734,10 @@ const recoveryFakeCodex = resolve(
const previousStateBase = process.env.PAPERCLIP_RUNNER_STATE_DIR;
const server = createServer();
let firstSession: NativeSession | undefined;
const ownedSessions = new Set<NativeSession>();
const ownedSpawns: Array<{ pid: number; processGroupId: number | null; startedAt: string }> = [];
const onSpawn = async (meta: typeof ownedSpawns[number]) => { ownedSpawns.push(meta); };
let executionCloseCompleted = false;
const runnerDiagnostics: string[] = [];
const onRunnerLog = async (_stream: "stdout" | "stderr", chunk: string) => {
runnerDiagnostics.push(chunk.slice(-4_096));
@@ -860,6 +864,7 @@ const recoveryFakeCodex = resolve(
runnerInstanceId,
runnerEnvironment: environment,
onLog: onRunnerLog,
onSpawn,
});
firstSession = await firstBackend.openSession({
identity: {
@@ -871,6 +876,7 @@ const recoveryFakeCodex = resolve(
},
workingDirectory: workspace,
});
ownedSessions.add(firstSession);
const checkpoint = await firstSession.snapshot();
expect(checkpoint.identity).toEqual({
companyId,
@@ -1009,6 +1015,7 @@ const recoveryFakeCodex = resolve(
runnerInstanceId,
runnerEnvironment: environment,
onLog: onRunnerLog,
onSpawn,
});
let continuity: Record<string, unknown> | undefined;
const controlPlaneInstanceId = randomUUID();
@@ -1031,6 +1038,12 @@ const recoveryFakeCodex = resolve(
runnerInstanceId,
controlPlaneInstanceId,
timeoutMs: 20_000,
// This disposable real-daemon fixture must join full retirement before
// removing its controller routes and durable state. The production
// default deliberately permits asynchronous cleanup after a result.
requireSessionCloseBeforeReturn: true,
onSession(value) { if (value) ownedSessions.add(value); },
async onSessionClosed() { executionCloseCompleted = true; },
controlPlane: port,
async onContinuityBreak(value) {
continuity = value;
@@ -1064,6 +1077,12 @@ const recoveryFakeCodex = resolve(
terminal: { runTerminalState: "succeeded" },
normalizedSessionId,
});
expect(executionCloseCompleted).toBe(true);
expect(ownedSpawns.length).toBeGreaterThanOrEqual(2);
for (const { pid } of ownedSpawns) {
expect(() => process.kill(pid, 0)).toThrow(expect.objectContaining({ code: "ESRCH" }));
}
console.info("Recovery fixture retired owned daemons", ownedSpawns);
expect(continuity).toMatchObject({
// The daemon can reject the damaged retained input during startup,
// before attach gets a chance to reject the unsettled provider session.
@@ -1201,9 +1220,13 @@ const recoveryFakeCodex = resolve(
.where(eq(issues.id, issueId));
expect(task).toEqual({ id: issueId, assigneeAgentId: agentId });
} finally {
await firstSession
?.close({ reason: "Recovery fixture cleanup" })
.catch(() => undefined);
// onSession(null) quarantines the runtime owner before close finishes;
// keep every published handle so an assertion/error cannot lose cleanup.
if (firstSession) ownedSessions.add(firstSession);
const closed = await Promise.allSettled([...ownedSessions].map((session) =>
Promise.resolve().then(() => session.close({ reason: "Recovery fixture cleanup" })),
));
const closeFailures = closed.filter((result) => result.status === "rejected");
runnerPrpWebSocketInternals.resetForTests();
server.closeAllConnections();
await new Promise<void>((done) => server.close(() => done()));
@@ -1211,6 +1234,10 @@ const recoveryFakeCodex = resolve(
delete process.env.PAPERCLIP_RUNNER_STATE_DIR;
else process.env.PAPERCLIP_RUNNER_STATE_DIR = previousStateBase;
await database.cleanup();
if (closeFailures.length) {
// Retain exact fixture state if owned retirement cannot be proven.
throw new AggregateError(closeFailures.map((result) => result.reason), "Recovery fixture session cleanup failed");
}
await rm(scratch, { recursive: true, force: true });
}
},
@@ -14,6 +14,7 @@ import {
createDb,
heartbeatRunEvents,
heartbeatRuns,
issueComments,
issueRecoveryActions,
issueRelations,
issueThreadInteractions,
@@ -42,7 +43,7 @@ import {
import { startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js";
import { PaperclipControlPlanePort } from "./paperclip-control-plane-port.js";
import { NativeRunCoordinatorStore } from "./native-run-coordinator-store.js";
import { finalizeNativeRun } from "./native-run-finalizer.js";
import { finalizeNativeRun, repairCommittedNativeChatResponse } from "./native-run-finalizer.js";
import { nativeRuntimeContextFixture } from "./runtime-context.test-fixture.js";
import { issueThreadInteractionService } from "../issue-thread-interactions.js";
import { claimNativeReviewExecutionLock, getNativeReviewAssignment } from "./native-review-participant.js";
@@ -974,7 +975,37 @@ describe("PaperclipControlPlanePort conformance", () => {
expect(response.decision.chosenSource).toBe("final_agent_message");
expect(stored.findIndex((row) => row.eventType === "run.result.accepted"))
.toBeGreaterThan(stored.findIndex((row) => row.eventType === "item.completed"));
await finalizeNativeRun({ db, runId: taskRunId, workspaceFinalizeStatus: "succeeded" });
// A workspace recovery owner can commit before the live heartbeat reaches
// presentation. The file-preparation comment must not hide the real reply.
const [prepared] = await db.insert(issueComments).values({
companyId: identity.companyId, issueId: taskIssueId,
authorAgentId: identity.agentId, authorType: "agent", createdByRunId: taskRunId,
body: "Prepared Continuity file for this response.",
}).returning();
const receipt = { semanticToolReceipts: { file: {
operationId: "register_deliverable", result: {
commandId: "deliverable-prepared:attachment-1", disposition: "applied",
attachmentId: "attachment-1", entityRefs: ["attachment-1", prepared!.id],
},
} } };
await db.update(heartbeatRuns).set({
contextSnapshot: { skipIssueComment: true },
resultJson: sql`coalesce(${heartbeatRuns.resultJson}, '{}'::jsonb) || ${JSON.stringify(receipt)}::jsonb`,
}).where(eq(heartbeatRuns.id, taskRunId));
await finalizeNativeRun({ db, runId: taskRunId, workspaceFinalizeStatus: "succeeded", projectRunStatus: true });
const repair = () => repairCommittedNativeChatResponse(db, {
companyId: identity.companyId, issueId: taskIssueId, runId: taskRunId,
});
expect(await repair()).toBe(false);
await db.update(heartbeatRuns).set({ contextSnapshot: { externalChatContinuation: true } })
.where(eq(heartbeatRuns.id, taskRunId));
expect(await repair()).toBe(false);
await db.update(heartbeatRuns).set({ contextSnapshot: {} }).where(eq(heartbeatRuns.id, taskRunId));
expect(await repair()).toBe(true);
expect(await repair()).toBe(false);
expect(await db.select({ body: issueComments.body }).from(issueComments)
.where(eq(issueComments.createdByRunId, taskRunId)).orderBy(asc(issueComments.createdAt)))
.toEqual([{ body: prepared!.body }, { body: finalText }]);
await expect(port.completeRun({
result: taskResult,
terminal: CONTROL_PLANE_CONFORMANCE_TERMINAL,
@@ -5,6 +5,7 @@ import {
PAPERCLIP_RUNNER_ACPX_PROFILES,
resolvePaperclipRunnerPermissionMode,
resolvePaperclipRunnerCursorMode,
resolvePaperclipRunnerPiThinkingLevel,
type PaperclipRunnerProvider,
} from "@paperclipai/adapter-utils";
import {
@@ -29,6 +30,7 @@ export const DEFAULT_ACPX_RUNNER_MODELS = {
// These profiles require explicit configuration. Admission belongs to the runner.
codex: null,
cursor: null,
pi: null,
} as const;
export type QualifiedPaperclipRunnerAcpxAgent =
@@ -127,6 +129,7 @@ export type PaperclipRunnerNativeProviderInput =
acpxAgent: AdmittedPaperclipRunnerAcpxAgent;
acpxPermissionMode: "approve-all" | "approve-paperclip" | "approve-reads" | "deny-all";
acpxSessionMode?: "agent" | "plan" | "ask";
piThinkingLevel?: "off" | "low" | "high" | "max";
};
export class PaperclipRunnerProviderProfileError extends Error {
@@ -376,10 +379,13 @@ export function resolvePaperclipRunnerProviderProfile(
resolvePaperclipRunnerCursorMode(candidate, config.acpxAgent, config.acpxSessionMode);
} catch (error) {
throw new PaperclipRunnerProviderProfileError(
"paperclip_runner_cursor_mode_invalid",
"paperclip_runner_mode_invalid",
error instanceof Error ? error.message : "Invalid Cursor session mode",
);
}
try { resolvePaperclipRunnerPiThinkingLevel(candidate, config.acpxAgent, config.piThinkingLevel); } catch (error) {
throw new PaperclipRunnerProviderProfileError("paperclip_runner_pi_thinking_invalid", error instanceof Error ? error.message : "Invalid Pi thinking level");
}
const model = optionalString(config.model);
if (candidate === "codex") {
return {
@@ -486,10 +492,10 @@ export function resolvePaperclipRunnerProviderProfile(
}
throw new PaperclipRunnerProviderProfileError("paperclip_runner_acpx_agent_unavailable", `${pendingAcpxProfile.label} is awaiting local and Daytona qualification. Its profile is not enabled for production runs.`);
}
if (acpxAgent === "cursor" && !model) {
throw new PaperclipRunnerProviderProfileError("paperclip_runner_acpx_model_required", "Cursor requires an explicit model ID; there is no default model.");
if ((acpxAgent === "cursor" || acpxAgent === "pi") && !model) {
throw new PaperclipRunnerProviderProfileError("paperclip_runner_acpx_model_required", "This ACPX agent requires an explicit model ID; there is no default model.");
}
if (acpxAgent !== "claude" && acpxAgent !== "codex" && acpxAgent !== "grok" && acpxAgent !== "cursor") {
if (acpxAgent !== "claude" && acpxAgent !== "codex" && acpxAgent !== "grok" && acpxAgent !== "cursor" && acpxAgent !== "pi") {
throw new PaperclipRunnerProviderProfileError(
"paperclip_runner_acpx_agent_unavailable",
"Paperclip Runner ACPX requires a qualified agent profile.",
@@ -567,6 +573,7 @@ export function resolvePaperclipRunnerNativeProviderInput(input: {
provider: "acpx",
model: profile.model,
acpxAgent: profile.acpxAgent,
...(profile.acpxAgent === "pi" ? { piThinkingLevel: resolvePaperclipRunnerPiThinkingLevel("acpx", "pi", config.piThinkingLevel) } : {}),
...(profile.acpxAgent === "cursor" ? {
acpxSessionMode: resolvePaperclipRunnerCursorMode("acpx", "cursor", config.acpxSessionMode),
} : {}),
@@ -0,0 +1,162 @@
/** Explicit operator-imported Linux authority. No downloads or executable probes. */
import { setImmediate as yieldToSignals } from "node:timers/promises";
import { createHash } from "node:crypto";
import { type Stats, constants } from "node:fs";
import * as fs from "node:fs/promises";
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { QUALIFIED_ACPX_PROFILES } from "../../vendor/paperclip-runner/index.js";
const SERVER_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../../..");
const MAX_BYTES = 4 * 1024 ** 3;
const MAX_FILE = 512 * 1024 ** 2;
const MANIFEST = "companion.json";
const AUTHORITY = ".import-authority.json";
type Entry = { path: string; mode: number; kind: "directory" } | { path: string; mode: number; kind: "file"; size: number; sha256: string } | { path: string; mode: number; kind: "symlink"; target: string };
export interface RemotePiCompanionManifest { schema: "paperclip.remote-pi-companion/v1"; sourceRevision: string; target: "linux-x64"; profileDigest: string; providerPackDigest: string; daemonSha256: string; entries: Entry[] }
function require(value: unknown, reason: string): asserts value { if (!value) throw new Error(`runner_remote_companion_invalid: ${reason}`); }
const digest = (value: Buffer | string) => createHash("sha256").update(value).digest("hex");
const safe = (path: string) => path.length > 0 && path.length < 4096 && !isAbsolute(path) && !/[\\\x00-\x1f\x7f]/u.test(path) && path.split("/").every(p => p !== "" && p !== "." && p !== "..");
const inside = (root: string, path: string) => path === root || (!relative(root, path).startsWith("..") && !isAbsolute(relative(root, path)));
const same = (a: Stats, b: Stats) => a.dev === b.dev && a.ino === b.ino && a.size === b.size && a.mode === b.mode && a.mtimeMs === b.mtimeMs && a.ctimeMs === b.ctimeMs && a.nlink === b.nlink;
type Checkpoint = () => Promise<void>;
function checkpoints(cancel?: () => void): Checkpoint {
const deadline = Date.now() + 600_000;
return async () => { await yieldToSignals(); cancel?.(); require(Date.now() < deadline, "import deadline exceeded"); };
}
async function directory(path: string) { const st = await fs.lstat(path); require(st.isDirectory() && !st.isSymbolicLink() && await fs.realpath(path) === path, "directory is linked or noncanonical"); return st; }
/** One descriptor and 1MiB buffer; no whole executable allocation or sync hashing. */
async function readStable(path: string, maximum: number, privateFile: boolean, check: Checkpoint, consume?: (chunk: Buffer) => Promise<void>) {
require(await fs.realpath(dirname(path)) === dirname(path), "linked parent");
const file = await fs.open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
try {
const before = await file.stat(); require(before.isFile() && before.size <= maximum && (!privateFile || before.nlink === 1), "file type, link or byte bound");
const hash = createHash("sha256"); let size = 0; const buffer = Buffer.alloc(1024 * 1024); let header = Buffer.alloc(0);
for (;;) {
await check(); const { bytesRead } = await file.read(buffer); if (!bytesRead) break;
size += bytesRead; require(size <= maximum && size <= before.size, "file grew beyond bound");
const chunk = buffer.subarray(0, bytesRead); hash.update(chunk); if (!header.length) header = Buffer.from(chunk.subarray(0, 64));
await consume?.(chunk);
}
require(size === before.size && same(before, await file.stat()) && same(before, await fs.lstat(path)), "file changed during read");
return { size, sha256: hash.digest("hex"), header, stat: before };
} finally { await file.close(); }
}
async function readOwned(path: string, maximum: number, check: Checkpoint, privateFile = false): Promise<Buffer> {
const chunks: Buffer[] = []; await readStable(path, maximum, privateFile, check, async chunk => { chunks.push(Buffer.from(chunk)); }); return Buffer.concat(chunks);
}
/** Complete no-follow inventory, including modes and contained symbolic links. */
export async function inventoryRemoteCompanion(root: string, check = checkpoints()): Promise<Entry[]> {
await directory(root); const entries: Entry[] = []; const links = new Map<string, { count: number; links: number }>(); let total = 0;
const visit = async (path: string): Promise<void> => {
const before = await directory(path);
for (const name of (await fs.readdir(path)).sort()) {
await check();
if (path === root && [MANIFEST, AUTHORITY].includes(name)) continue;
const file = join(path, name); const rel = relative(root, file); require(safe(rel), "unsafe path");
const st = await fs.lstat(file); const mode = st.mode & 0o7777;
require(entries.length < 100_000 && (st.isSymbolicLink() || (mode & 0o7022) === 0), "entry count or unsafe mode");
if (st.isDirectory()) { require((mode & 0o700) === 0o700, "directory requires owner read/write/search for owned cleanup"); entries.push({ path: rel, mode, kind: "directory" }); await visit(file); }
else if (st.isFile()) {
total += st.size; require(total <= MAX_BYTES, "tree byte bound"); const read = await readStable(file, MAX_FILE, false, check);
require(same(st, read.stat), "discovered file changed");
entries.push({ path: rel, mode, kind: "file", size: read.size, sha256: read.sha256 });
const key = `${st.dev}:${st.ino}`; const group = links.get(key) ?? { count: 0, links: st.nlink }; group.count++; require(group.links === st.nlink, "hardlink identity drift"); links.set(key, group);
} else if (st.isSymbolicLink()) {
const target = await fs.readlink(file); require(!isAbsolute(target) && !/[\x00-\x1f\x7f]/u.test(target) && inside(root, resolve(dirname(file), target)) && inside(root, await fs.realpath(file)), "escaping symbolic link");
require(same(st, await fs.lstat(file)), "link changed"); entries.push({ path: rel, mode, kind: "symlink", target });
} else throw new Error("runner_remote_companion_invalid: special entry");
}
const after = await directory(path); require(before.dev === after.dev && before.ino === after.ino && before.mtimeMs === after.mtimeMs, "directory changed");
};
await visit(root); require([...links.values()].every(g => g.count === g.links), "external hardlink");
return entries.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
}
async function installedIdentity(serverRoot: string, check: Checkpoint) {
require(process.platform === "darwin" || process.platform === "linux", "companion import supports macOS and Linux controllers");
await directory(serverRoot); const pkg = JSON.parse((await readOwned(join(serverRoot, "package.json"), 65536, check, true)).toString()); require(pkg.name === "@paperclipai/server", "public server package required");
const source = JSON.parse((await readOwned(join(serverRoot, "dist/build-info.json"), 65536, check, true)).toString()).commit;
require(typeof source === "string" && /^[a-f0-9]{40}$/u.test(source), "installed build source is missing");
const profile = QUALIFIED_ACPX_PROFILES.pi; require(profile.qualificationStatus !== "pending", "Pi is not qualified");
return { source, profileDigest: profile.commandDigest };
}
async function validate(root: string, manifest: RemotePiCompanionManifest, identity: Awaited<ReturnType<typeof installedIdentity>>, check: Checkpoint) {
require(manifest.schema === "paperclip.remote-pi-companion/v1" && manifest.target === "linux-x64" && manifest.sourceRevision === identity.source && manifest.profileDigest === identity.profileDigest, "source/profile/target mismatch");
require(JSON.stringify(await inventoryRemoteCompanion(root, check)) === JSON.stringify(manifest.entries), "complete inventory mismatch");
const daemon = manifest.entries.find(e => e.path === "bin/paperclip-runnerd");
require(daemon?.kind === "file" && daemon.size > 64 && (daemon.mode & 0o111) !== 0 && daemon.sha256 === manifest.daemonSha256, "daemon identity");
const elf = (await readStable(join(root, daemon.path), MAX_FILE, false, check)).header; require(elf.subarray(0, 4).equals(Buffer.from([127, 69, 76, 70])) && elf[4] === 2 && elf[5] === 1 && elf.readUInt16LE(18) === 62, "Linux x64 ELF required");
const pack = JSON.parse((await readOwned(join(root, "provider-pack/provider-pack.json"), 16 * 1024 ** 2, check)).toString());
const canonical = (v: unknown): string => Array.isArray(v) ? `[${v.map(canonical).join(",")}]` : v && typeof v === "object" ? `{${Object.keys(v).sort().map(k => `${JSON.stringify(k)}:${canonical((v as Record<string, unknown>)[k])}`).join(",")}}` : JSON.stringify(v);
require(pack.schema === "paperclip-runner/remote-provider-pack/v1" && pack.digest === manifest.providerPackDigest && pack.digest === `sha256:${digest(canonical(pack.payload))}`, "pack manifest digest");
require(pack.payload.runnerSourceRevision === identity.source && pack.payload.target.platform === "linux" && pack.payload.target.architecture === "x64", "pack source/target");
const pi = pack.payload.candidateProviders?.pi; require(pi?.qualification === "qualified" && pi.profileDigest === identity.profileDigest && pi.path === "provider-assets/pi/linux-x64", "normal Pi pack required");
}
/** Release-side command uses this same inventory contract before publication. */
export async function createRemotePiCompanionManifest(root: string, sourceRevision: string): Promise<RemotePiCompanionManifest> {
const check = checkpoints();
const profile = QUALIFIED_ACPX_PROFILES.pi; const pack = JSON.parse((await readOwned(join(root, "provider-pack/provider-pack.json"), 16 * 1024 ** 2, check)).toString()); const entries = await inventoryRemoteCompanion(root, check);
const daemon = entries.find(e => e.path === "bin/paperclip-runnerd"); require(daemon?.kind === "file", "daemon missing");
const manifest: RemotePiCompanionManifest = { schema: "paperclip.remote-pi-companion/v1", sourceRevision, target: "linux-x64", profileDigest: profile.commandDigest, providerPackDigest: pack.digest, daemonSha256: daemon.sha256, entries };
require(/^[a-f0-9]{40}$/u.test(sourceRevision), "release source"); await validate(root, manifest, { source: sourceRevision, profileDigest: profile.commandDigest }, check); return manifest;
}
function cacheParent(serverRoot: string) { return join(serverRoot, "remote-companions"); }
async function removeOwned(path: string, owned: Stats) { const st = await fs.lstat(path); require(st.dev === owned.dev && st.ino === owned.ino && !st.isSymbolicLink(), "cleanup root ownership changed"); await fs.rm(path, { recursive: true }); }
export async function importRemotePiCompanion(input: { directory: string; sha256: string; serverRoot?: string; checkCancelled?: () => void }) {
const checkpoint = checkpoints(input.checkCancelled);
await checkpoint();
const serverRoot = input.serverRoot ?? SERVER_ROOT; const identity = await installedIdentity(serverRoot, checkpoint); const source = await fs.realpath(input.directory); require(source === resolve(input.directory), "linked import root"); await directory(source);
require(/^[a-f0-9]{64}$/u.test(input.sha256), "expected manifest SHA256 required"); const bytes = await readOwned(join(source, MANIFEST), 32 * 1024 ** 2, checkpoint); require(digest(bytes) === input.sha256, "operator manifest digest mismatch");
const manifest = JSON.parse(bytes.toString()) as RemotePiCompanionManifest; await validate(source, manifest, identity, checkpoint); await checkpoint();
const parent = cacheParent(serverRoot); try { await fs.mkdir(parent, { mode: 0o700 }); } catch (error) { if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw new Error("Remote companion setup requires a writable installed server package", { cause: error }); }
const parentInfo = await directory(parent); require((parentInfo.mode & 0o077) === 0 && parentInfo.uid === process.getuid?.(), "cache must be private and operator-owned");
const lockPath = join(parent, ".import-linux-x64.lock"); const lock = await fs.open(lockPath, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | constants.O_NOFOLLOW, 0o600); const lockInfo = await lock.stat();
let staging: string | undefined; let stagingInfo: Stats | undefined; let outputInfo: Stats | undefined; let committed = false; const output = join(parent, "linux-x64");
try {
await checkpoint();
try { await fs.lstat(output); const existing = await resolveRemotePiCompanion({ serverRoot, checkpoint }); require(existing?.manifestSha256 === input.sha256, "existing companion differs; remove only after stopping all runs"); await checkpoint(); return { status: "verified_existing", ...existing }; } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }
staging = await fs.mkdtemp(join(parent, ".import-")); stagingInfo = await fs.lstat(staging);
for (const entry of manifest.entries.filter(e => e.kind === "directory").sort((a, b) => a.path.split("/").length - b.path.split("/").length)) await fs.mkdir(join(staging, entry.path), { mode: 0o700 });
for (const entry of manifest.entries) {
if (entry.kind !== "file") continue;
const file = join(staging, entry.path); const outputFile = await fs.open(file, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | constants.O_NOFOLLOW, 0o600);
try {
const read = await readStable(join(source, entry.path), MAX_FILE, false, checkpoint, async chunk => {
let offset = 0; while (offset < chunk.length) { await checkpoint(); offset += (await outputFile.write(chunk, offset)).bytesWritten; }
});
require(read.size === entry.size && read.sha256 === entry.sha256, "source changed while copied");
} finally { await outputFile.close(); }
await fs.chmod(file, entry.mode); await checkpoint();
}
for (const entry of manifest.entries) if (entry.kind === "symlink") await fs.symlink(entry.target, join(staging, entry.path));
for (const entry of manifest.entries.filter(e => e.kind === "directory").reverse()) await fs.chmod(join(staging, entry.path), entry.mode);
await fs.writeFile(join(staging, MANIFEST), bytes, { flag: "wx", mode: 0o600 }); await validate(staging, manifest, identity, checkpoint); await validate(source, manifest, identity, checkpoint); await checkpoint();
// Claim the final path exclusively; no rename may replace a concurrent directory.
await fs.mkdir(output, { mode: 0o700 }); outputInfo = await fs.lstat(output);
for (const name of await fs.readdir(staging)) { const now = await directory(output); require(now.dev === outputInfo.dev && now.ino === outputInfo.ino, "publication ownership changed"); await fs.rename(join(staging, name), join(output, name)); }
await fs.writeFile(join(output, AUTHORITY), JSON.stringify({ sha256: input.sha256 }) + "\n", { flag: "wx", mode: 0o600 });
const result = await resolveRemotePiCompanion({ serverRoot, checkpoint }); require(result?.manifestSha256 === input.sha256, "publication verification"); await checkpoint(); committed = true; return { status: "imported_verified", ...result };
} finally {
const failures: unknown[] = []; const cleanup = async (fn: () => Promise<void>) => { try { await fn(); } catch (error) { failures.push(error); } };
if (outputInfo && !committed) await cleanup(() => removeOwned(output, outputInfo!));
if (staging && stagingInfo) await cleanup(() => removeOwned(staging!, stagingInfo!));
await cleanup(async () => { const now = await fs.lstat(lockPath); require(now.dev === lockInfo.dev && now.ino === lockInfo.ino, "lock ownership changed"); await fs.unlink(lockPath); }); await lock.close();
if (failures.length) throw new AggregateError(failures, "Remote companion cleanup incomplete; inspect owned paths before retrying");
}
}
export async function resolveRemotePiCompanion(input: { serverRoot?: string; workspaceRoot?: string; checkpoint?: Checkpoint } = {}) {
const check = input.checkpoint ?? checkpoints();
const serverRoot = input.serverRoot ?? SERVER_ROOT; const parent = cacheParent(serverRoot); const root = join(parent, "linux-x64");
try { await fs.lstat(root); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return null; throw error; }
const parentInfo = await directory(parent); require((parentInfo.mode & 0o077) === 0 && parentInfo.uid === process.getuid?.(), "cache privacy"); await directory(root);
if (input.workspaceRoot) { const workspace = await fs.realpath(input.workspaceRoot); require(!inside(workspace, root) && !inside(root, workspace), "companion cache cannot overlap a workspace"); }
const authority = JSON.parse((await readOwned(join(root, AUTHORITY), 65536, check, true)).toString()); const bytes = await readOwned(join(root, MANIFEST), 32 * 1024 ** 2, check, true); require(digest(bytes) === authority.sha256, "installed authority changed");
const manifest = JSON.parse(bytes.toString()) as RemotePiCompanionManifest; await validate(root, manifest, await installedIdentity(serverRoot, check), check);
return { root, runnerBinary: join(root, "bin/paperclip-runnerd"), providerPack: join(root, "provider-pack"), manifestSha256: authority.sha256 as string, sourceRevision: manifest.sourceRevision, target: manifest.target };
}
/** Explicit legacy overrides remain authoritative; C/C never gain this Pi path. */
export async function selectRemotePiCompanion(input: { provider: { kind: string; agent?: string }; remote: boolean; binaryOverride?: string | null; packOverride?: string | null; workspaceRoot: string; serverRoot?: string }) {
if (!input.remote || input.provider.kind !== "acpx" || input.provider.agent !== "pi" || input.binaryOverride?.trim() || input.packOverride?.trim()) return null;
return resolveRemotePiCompanion({ serverRoot: input.serverRoot, workspaceRoot: input.workspaceRoot });
}
@@ -116,16 +116,14 @@ describe("resolveNativeRuntimeMode", () => {
})).toThrow(expect.objectContaining({
code: "paperclip_runner_opencode_model_invalid",
}));
expect(() => resolveNativeRuntimeMode({
...eligible,
adapterConfig: {
provider: "acpx",
acpxAgent: "pi",
model: "openrouter/deepseek/deepseek-v4-flash-0731",
},
})).toThrow(expect.objectContaining({
code: "paperclip_runner_acpx_agent_unavailable",
}));
for (const acpxAgent of ["copilot"]) {
expect(() => resolveNativeRuntimeMode({
...eligible,
adapterConfig: { provider: "acpx", acpxAgent, model: "explicit-provider-model" },
})).toThrow(expect.objectContaining({
code: "paperclip_runner_acpx_agent_unavailable",
}));
}
expect(() => resolveNativeRuntimeMode({
...eligible,
adapterConfig: { provider: "acpx", acpxAgent: "claude", model: "claude-opus-5" },
@@ -44,71 +44,27 @@ function arbitrate(
}
describe("native status authority", () => {
it("waits on persisted monitors without an immediate continuation, even with unfinished work", () => {
const pending = assessment({ reportedDisposition: "yielded", hasBlockingRemainingWork: true,
continuation: { kind: "monitor", summary: "Check CI", idempotencyKey: "ci" } });
for (const priorIssueStatus of ["in_progress", "in_review"] as const) {
expect(arbitrate({ assessment: pending, priorIssueStatus, monitorWaitAuthorized: true }))
.toMatchObject({ statusAction: "preserve", toStatus: priorIssueStatus,
reasonCode: "scheduled_monitor_waiting", effects: [{ kind: "release_checkout" }] });
}
expect(arbitrate({ assessment: pending })).toMatchObject({ reasonCode: "monitor_wait_authority_lost",
effects: [{ kind: "record_finalization_error" }] });
expect(arbitrate({ assessment: pending, monitorWaitAuthorized: true, hasUnresolvedIssueBlockers: true }).toStatus).toBe("blocked");
expect(arbitrate({ assessment: pending, monitorWaitAuthorized: true, governanceGate: { kind: "approval", id: "pending" } }).toStatus).toBe("in_review");
});
it("keeps a pending child result non-terminal without adding another continuation", () => {
expect(arbitrate({ hasPendingChildCompletion: true })).toMatchObject({
statusAction: "in_progress", toStatus: "in_progress", reasonCode: "native_child_completion_pending",
effects: [{ kind: "release_checkout" }],
it("keeps a verified accepted Cursor plan passive without completing or replaying work", () => {
const passive = assessment({ reportedDisposition: "yielded", objectiveSatisfied: false,
allCriteriaSatisfied: false, hasBlockingRemainingWork: true,
continuation: { kind: "response_wake", summary: "Explicit continuation needed", idempotencyKey: "cursor-plan-wait:event" } });
expect(arbitrate({ assessment: passive, planWaitAuthorized: true })).toMatchObject({
toStatus: "in_progress", reasonCode: "native_plan_accepted_waiting_for_continuation", effects: [],
});
expect(arbitrate({ hasPendingChildCompletion: false }).toStatus).toBe("done");
for (const priorIssueStatus of ["done", "cancelled"] as const) {
expect(arbitrate({ priorIssueStatus, hasPendingChildCompletion: true }).toStatus).toBe(priorIssueStatus);
}
expect(arbitrate({ hasPendingChildCompletion: true, hasUnresolvedIssueBlockers: true }).toStatus).toBe("blocked");
expect(arbitrate({ hasPendingChildCompletion: true, governanceGate: { kind: "approval", id: "approval" } }).toStatus)
.toBe("in_review");
expect(arbitrate({ hasPendingChildCompletion: true, workspaceFinalizeStatus: "failed" }).statusAction).toBe("preserve");
expect(arbitrate({ assessment: passive })).toMatchObject({
toStatus: "in_progress",
reasonCode: "completion_evidence_incomplete",
effects: [expect.objectContaining({
kind: "enqueue_continuation",
continuationKind: "same_agent",
idempotencyKey: "native-completion-incomplete",
})],
});
expect(arbitrate({ assessment: passive, planWaitAuthorized: true, terminalState: "failed" }).reasonCode).not.toBe("native_plan_accepted_waiting_for_continuation");
expect(arbitrate({ assessment: passive, planWaitAuthorized: true, priorIssueStatus: "cancelled" }).toStatus).toBe("cancelled");
expect(arbitrate({ assessment: passive, planWaitAuthorized: true, governanceGate: { kind: "interaction", id: "pending" } }).toStatus).toBe("in_review");
});
it("schedules a capacity retry while preserving partial work and review authority", () => {
for (const nativeReviewOutcome of [undefined, "pending"] as const) {
const decision = arbitrate({ terminalState: "failed", providerOverloaded: true, nativeReviewOutcome });
expect(decision).toMatchObject({ statusAction: "preserve", reasonCode: "native_provider_overloaded" });
expect(decision.effects).toContainEqual(expect.objectContaining({ kind: "schedule_retry", cause: "native_provider_overloaded" }));
}
expect(arbitrate({ terminalState: "failed", providerOverloaded: true, failureRetryCount: 2 }))
.toMatchObject({ statusAction: "blocked", reasonCode: "native_provider_overloaded_exhausted", effects: [{ kind: "bind_blocker", owner: "board", action: expect.stringContaining("Automatic retries exhausted") }] });
});
it.each([
{ hasActivePauseHold: true }, { hasUnresolvedIssueBlockers: true },
{ governanceGate: { kind: "approval" as const, id: "approval" } },
{ priorIssueStatus: "blocked" as const }, { priorIssueStatus: "done" as const },
{ workspaceFinalizeStatus: "failed" as const }, { nativeReviewOutcome: "stale" as const },
{ nativeReviewOutcome: "resolved" as const },
])("does not schedule capacity retries through existing authority or cleanup gates (%j)", (gate) => {
const decision = arbitrate({ terminalState: "failed", providerOverloaded: true, ...gate });
expect(decision.effects.some(effect => effect.kind === "schedule_retry")).toBe(false);
});
it.each(["in_progress", "in_review"] as const)("blocks a current worker's proven model rejection without a retry (%s)", (priorIssueStatus) => {
const decision = arbitrate({ priorIssueStatus, terminalState: "failed", providerModelRejected: true });
expect(decision).toMatchObject({ statusAction: "blocked", toStatus: "blocked", reasonCode: "native_provider_model_rejected", unblockDescriptor: { owner: "board" } });
expect(decision.effects).toEqual([{ kind: "bind_blocker", owner: "board", action: expect.any(String) }]);
expect(arbitrate({ terminalState: "failed", providerModelRejected: false }).effects).toContainEqual(expect.objectContaining({ kind: "schedule_retry" }));
expect(arbitrate({ terminalState: "succeeded", providerModelRejected: true }).reasonCode).not.toBe("native_provider_model_rejected");
expect(arbitrate({ terminalState: "failed", providerModelRejected: true, workspaceFinalizeStatus: "failed" }).reasonCode).toBe("finalization_failed_claim_preserved");
expect(arbitrate({ terminalState: "failed", providerModelRejected: true, priorIssueStatus: "done" }).toStatus).toBe("done");
for (const nativeReviewOutcome of ["stale", "resolved"] as const) {
expect(arbitrate({ terminalState: "failed", providerModelRejected: true, priorIssueStatus, nativeReviewOutcome }))
.toMatchObject({ statusAction: "preserve", toStatus: priorIssueStatus, reasonCode: "native_review_action_finished" });
}
});
it("preserves pending review authority without automatic recovery after model rejection", () => {
expect(arbitrate({ priorIssueStatus: "in_review", terminalState: "failed", providerModelRejected: true, nativeReviewOutcome: "pending" }))
.toMatchObject({ statusAction: "preserve", toStatus: "in_review", reasonCode: "native_provider_model_rejected", unblockDescriptor: null, effects: [{ kind: "release_checkout" }] });
});
it("a reviewer finishes its decision without completing rejected or still-reviewed work", () => {
for (const priorIssueStatus of ["in_progress", "in_review"] as const) {
const decision = arbitrate({ priorIssueStatus, nativeReviewOutcome: "resolved" });