mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 23:56:43 +01:00
feat(runner): add flagged Codex execution adapter (#12188)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The Paperclip Runner now has protocol, provider, tool, package, persistence, and hidden server boundaries. > - The server still cannot select that path for a real agent heartbeat. > - A new runtime must not change any existing direct adapter. > - An experimental runtime must fail closed when its rollout flag is off. > - This pull request adds one guarded Codex vertical slice through runnerd. > - The benefit is a production-built runner path that users cannot start by default. ## Linked Issues or Issue Description Refs #11962 Refs #12111 Refs #12169 Refs #12176 **Subsystem affected** Cross-cutting. The change affects the runner package, server orchestration, shared settings, and adapter configuration UI. **Problem or motivation** The hidden PRP coordinator cannot execute a real heartbeat. The application also needs an explicit rollout boundary before it can expose the experimental runner. Existing direct adapters must keep their current execution and finalization behavior. **Proposed solution** Add `paperclip_runner` as a Codex-only adapter behind the default-off `enableNativeRunner` instance flag. Select the native runtime only for that adapter. Persist the run binding before runnerd starts. Wait for the durable PRP result and terminal event. Resume the real Codex provider thread on later heartbeats. Keep persisted native runs readable and recoverable after the flag changes. **Alternatives considered** The server could route `codex_local` through runnerd. That option would change an existing adapter and weaken rollback safety. The server could expose all providers now. That option would add unreviewed provider behavior. The build could depend on a prebuilt runner binary. That option would make source builds architecture-dependent and difficult to verify. **Roadmap alignment** This work supports the shipped enforced-outcomes, governed-tool, and self-healing-run milestones. It does not add a new roadmap surface. It is the guarded execution step after the merged hidden runner boundaries. **Additional context** This is the next replacement for the closed large runner pull request. Task-thread presentation remains a separate follow-up so this change can preserve the current direct-adapter UI. ## What Changed - Add `paperclip_runner` as an explicit Codex-only adapter. - Add the default-off `enableNativeRunner` instance flag. - Reject fresh create, hire, import, switch, and execution requests while the flag is off. - Allow edits to persisted runner agents while the flag is off. - Recover an already persisted native run even after the flag is disabled. - Keep every built-in direct adapter on its existing runtime path. - Persist an immutable native run binding and revisioned completion contract before runnerd starts. - Execute server to PRP to runnerd to Codex to server through the hidden coordinator. - Validate the durable result against the terminal event and exact completion criteria before finalization. - Preserve the Codex provider thread ID and use `thread/resume` on the next heartbeat. - Strip unsupported Codex configuration fields from the experimental adapter. - Build a target-native release runner binary from source and vendor it into the server distribution. - Install Rust only in the Docker build stage. Do not add a workflow or lockfile change. - Stop the runner process group on completion, cancellation, and forced shutdown. ## Verification - Run `pnpm --filter @paperclipai/paperclip-runner check:all`. All 69 TypeScript tests and 58 Rust tests pass. Protocol, conformance, replay, formatting, and generated-file checks pass. - Run the 12 focused adapter, settings, runtime-selection, coordinator, direct-isolation, and real Codex integration test files. All 186 tests pass. - The real integration test uses PostgreSQL, HTTP, WebSocket, runnerd, and a fake Codex app server. It proves one `thread/start` followed by one `thread/resume`. - Run `pnpm -r typecheck`. - Run `pnpm build`. - Run `pnpm check:token-gates`. - Build the Docker `build` target from a clean context. Confirm that the server distribution contains an executable `paperclip-runnerd` built with Debian Rust 1.85. - Start the server through the source-mode tsx entry point with the package `dist` directory absent. Confirm the vendor shim resolves source exports and the server boots. - Run `pnpm test:run` twice. On this macOS host, 405 files pass and 1 file skips. Eight untouched workspace and loopback tests fail because macOS resolves `/tmp` and `/var` through `/private` and because PID-derived test ports exceed 65535. Linux CI must pass the full suite. - Confirm that the diff contains 52 files. Confirm that it contains no `.github` or `pnpm-lock.yaml` change. ## Risks - The feature flag is off by default. A fresh native start fails with a stable error while the flag is off. - A persisted native run remains recoverable after the flag changes. This prevents rollout changes from corrupting recorded work. - Only local Codex execution is accepted. Other providers and remote work modes fail closed. - Existing direct adapters do not start runnerd, create native rows, use native status arbitration, or enter native finalization. - The runner receives its one-use bootstrap ticket through the child environment. The server does not put the ticket in command arguments or logs. - The server validates the company, task, agent, run, runner, session, completion contract, result, and terminal binding before it accepts completion. - The build compiles a target-native Rust binary. Cross-platform release packaging remains a later concern. Source builds and Docker builds compile for their current target. - Docker needs enough build memory for the existing server TypeScript compile. The Docker build stage sets a 4 GB V8 heap limit. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex with GPT-5. The exact deployment ID and context-window size are not exposed. The model used agentic reasoning, repository tools, code execution, and test execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and applicable tests 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
This commit is contained in:
1 parent
243430f76e
commit
397de98193
52 files changed
+2531
-197
No files matched your search
@@ -60,7 +60,10 @@ function registerModuleMocks() {
|
||||
vi.doMock("../middleware/index.js", async () => vi.importActual("../middleware/index.js"));
|
||||
}
|
||||
|
||||
function createApp(actorOverrides: Partial<Express.Request["actor"]> = {}) {
|
||||
function createApp(
|
||||
actorOverrides: Partial<Express.Request["actor"]> = {},
|
||||
options: Parameters<typeof adapterRoutes>[0] = {},
|
||||
) {
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use((req, _res, next) => {
|
||||
@@ -74,7 +77,7 @@ function createApp(actorOverrides: Partial<Express.Request["actor"]> = {}) {
|
||||
};
|
||||
next();
|
||||
});
|
||||
app.use("/api", adapterRoutes());
|
||||
app.use("/api", adapterRoutes(options));
|
||||
app.use(errorHandler);
|
||||
return app;
|
||||
}
|
||||
@@ -146,6 +149,26 @@ describe("adapter routes", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps paperclip_runner hidden from selection unless the rollout flag is enabled", async () => {
|
||||
const disabledResponse = await request(createApp()).get("/api/adapters");
|
||||
expect(disabledResponse.status).toBe(200);
|
||||
expect(disabledResponse.body.find((adapter: any) => adapter.type === "paperclip_runner"))
|
||||
.toMatchObject({ disabled: true });
|
||||
|
||||
const enabledResponse = await request(createApp({}, {
|
||||
getNativeRunnerEnabled: async () => true,
|
||||
})).get("/api/adapters");
|
||||
expect(enabledResponse.status).toBe(200);
|
||||
expect(enabledResponse.body.find((adapter: any) => adapter.type === "paperclip_runner"))
|
||||
.toMatchObject({
|
||||
disabled: false,
|
||||
capabilities: {
|
||||
supportsInstructionsBundle: false,
|
||||
supportsModelProfiles: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("GET /api/adapters returns correct capabilities for built-in adapters", async () => {
|
||||
const app = createApp();
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ const mockApprovalService = vi.hoisted(() => ({
|
||||
|
||||
const mockInstanceSettingsService = vi.hoisted(() => ({
|
||||
getGeneral: vi.fn(async () => ({ censorUsernameInLogs: false })),
|
||||
getExperimental: vi.fn(async () => ({ enableNativeRunner: false })),
|
||||
}));
|
||||
|
||||
const mockLogActivity = vi.hoisted(() => vi.fn());
|
||||
@@ -234,6 +235,7 @@ describe("agent routes adapter validation", () => {
|
||||
mockAccessService.setPrincipalPermission.mockResolvedValue(undefined);
|
||||
mockLogActivity.mockResolvedValue(undefined);
|
||||
mockSecretService.syncEnvBindingsForTarget.mockResolvedValue(undefined);
|
||||
mockInstanceSettingsService.getExperimental.mockResolvedValue({ enableNativeRunner: false });
|
||||
mockAgentInstructionsService.materializeManagedBundle.mockImplementation(async (agent: { adapterConfig: unknown }) => ({
|
||||
adapterConfig: agent.adapterConfig,
|
||||
}));
|
||||
@@ -565,4 +567,52 @@ describe("agent routes adapter validation", () => {
|
||||
|
||||
expect(res.status, JSON.stringify(res.body)).toBe(201);
|
||||
});
|
||||
|
||||
it("rejects a new paperclip_runner selection while the rollout flag is off", async () => {
|
||||
const app = await createApp();
|
||||
const res = await requestApp(app, (baseUrl) =>
|
||||
request(baseUrl)
|
||||
.post("/api/companies/company-1/agents")
|
||||
.send({ name: "Native Codex", adapterType: "paperclip_runner" }),
|
||||
);
|
||||
|
||||
expect(res.status, JSON.stringify(res.body)).toBe(422);
|
||||
expect(res.body.details).toMatchObject({ code: "paperclip_runner_rollout_disabled" });
|
||||
expect(mockAgentService.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows a new paperclip_runner selection while the rollout flag is on", async () => {
|
||||
mockInstanceSettingsService.getExperimental.mockResolvedValue({ enableNativeRunner: true });
|
||||
const app = await createApp();
|
||||
const res = await requestApp(app, (baseUrl) =>
|
||||
request(baseUrl)
|
||||
.post("/api/companies/company-1/agents")
|
||||
.send({
|
||||
name: "Native Codex",
|
||||
adapterType: "paperclip_runner",
|
||||
adapterConfig: { provider: "codex" },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(res.status, JSON.stringify(res.body)).toBe(201);
|
||||
expect(mockAgentService.create).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("keeps an existing paperclip_runner agent editable after the flag is disabled", async () => {
|
||||
const existing = await mockAgentService.getById();
|
||||
mockAgentService.getById.mockResolvedValue({
|
||||
...existing,
|
||||
adapterType: "paperclip_runner",
|
||||
adapterConfig: { provider: "codex" },
|
||||
});
|
||||
const app = await createApp();
|
||||
const res = await requestApp(app, (baseUrl) =>
|
||||
request(baseUrl)
|
||||
.patch("/api/agents/11111111-1111-4111-8111-111111111111")
|
||||
.send({ name: "Native Codex (recorded)" }),
|
||||
);
|
||||
|
||||
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
||||
expect(mockAgentService.update).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -102,6 +102,10 @@ const agentInstructionsSvc = {
|
||||
materializeManagedBundle: vi.fn(),
|
||||
};
|
||||
|
||||
const instanceSettingsSvc = {
|
||||
getExperimental: vi.fn(async () => ({ enableNativeRunner: false })),
|
||||
};
|
||||
|
||||
vi.mock("../services/companies.js", () => ({
|
||||
companyService: () => companySvc,
|
||||
}));
|
||||
@@ -154,6 +158,10 @@ vi.mock("../services/agent-instructions.js", () => ({
|
||||
agentInstructionsService: () => agentInstructionsSvc,
|
||||
}));
|
||||
|
||||
vi.mock("../services/instance-settings.js", () => ({
|
||||
instanceSettingsService: () => instanceSettingsSvc,
|
||||
}));
|
||||
|
||||
vi.mock("../routes/org-chart-svg.js", () => ({
|
||||
renderOrgChartPng: vi.fn(async () => Buffer.from("png")),
|
||||
}));
|
||||
@@ -171,6 +179,7 @@ describe("company portability", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
instanceSettingsSvc.getExperimental.mockResolvedValue({ enableNativeRunner: false });
|
||||
secretSvc.create.mockResolvedValue({ id: "secret-created" });
|
||||
secretSvc.remove.mockResolvedValue(true);
|
||||
secretSvc.normalizeAdapterConfigForPersistence.mockImplementation(async (_companyId, config) => config);
|
||||
@@ -5808,6 +5817,48 @@ describe("company portability", () => {
|
||||
expect(preview.plan.projectPlans).toHaveLength(0);
|
||||
expect(preview.plan.issuePlans).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("rejects runner imports while disabled and accepts the same selection when enabled", async () => {
|
||||
const portability = companyPortabilityService({} as any);
|
||||
const exported = await portability.exportBundle("company-1", {
|
||||
include: { company: false, agents: true, projects: false, issues: false },
|
||||
});
|
||||
agentSvc.list.mockResolvedValue([]);
|
||||
agentSvc.create.mockImplementation(async (_companyId: string, input: Record<string, unknown>) => ({
|
||||
id: "agent-created",
|
||||
...input,
|
||||
}));
|
||||
const request = {
|
||||
source: {
|
||||
type: "inline" as const,
|
||||
rootPath: exported.rootPath,
|
||||
files: exported.files,
|
||||
},
|
||||
include: { company: false, agents: true, projects: false, issues: false },
|
||||
target: { mode: "existing_company" as const, companyId: "company-1" },
|
||||
agents: "all" as const,
|
||||
collisionStrategy: "rename" as const,
|
||||
adapterOverrides: {
|
||||
claudecoder: {
|
||||
adapterType: "paperclip_runner",
|
||||
adapterConfig: { provider: "codex" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await expect(portability.importBundle(request, "user-1")).rejects.toMatchObject({
|
||||
status: 422,
|
||||
details: { code: "paperclip_runner_rollout_disabled" },
|
||||
});
|
||||
expect(agentSvc.create).not.toHaveBeenCalled();
|
||||
|
||||
instanceSettingsSvc.getExperimental.mockResolvedValue({ enableNativeRunner: true });
|
||||
await portability.importBundle(request, "user-1");
|
||||
expect(agentSvc.create).toHaveBeenCalledWith("company-1", expect.objectContaining({
|
||||
adapterType: "paperclip_runner",
|
||||
adapterConfig: expect.objectContaining({ provider: "codex" }),
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
describe("dedupeImportedCompanyName", () => {
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { sql } from "drizzle-orm";
|
||||
import {
|
||||
agents,
|
||||
companies,
|
||||
completionContracts,
|
||||
createDb,
|
||||
nativeRunFinalizations,
|
||||
nativeRunResults,
|
||||
statusDecisions,
|
||||
workAssessments,
|
||||
} from "@paperclipai/db";
|
||||
import type { ServerAdapterModule } from "../adapters/index.js";
|
||||
import {
|
||||
getEmbeddedPostgresTestSupport,
|
||||
startEmbeddedPostgresTestDatabase,
|
||||
} from "./helpers/embedded-postgres.js";
|
||||
import {
|
||||
registerServerAdapter,
|
||||
unregisterServerAdapter,
|
||||
} from "../adapters/index.js";
|
||||
import { heartbeatService } from "../services/heartbeat.js";
|
||||
|
||||
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
||||
|
||||
if (!embeddedPostgresSupport.supported) {
|
||||
console.warn(
|
||||
`Skipping direct-adapter native-isolation tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`,
|
||||
);
|
||||
}
|
||||
|
||||
async function waitForRunToFinish(
|
||||
heartbeat: ReturnType<typeof heartbeatService>,
|
||||
runId: string,
|
||||
timeoutMs = 10_000,
|
||||
) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
const run = await heartbeat.getRun(runId);
|
||||
if (run && !["queued", "running"].includes(run.status)) return run;
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
}
|
||||
return heartbeat.getRun(runId);
|
||||
}
|
||||
|
||||
describeEmbeddedPostgres("direct adapter native-runner isolation", () => {
|
||||
let db!: ReturnType<typeof createDb>;
|
||||
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
|
||||
const execute = vi.fn<ServerAdapterModule["execute"]>();
|
||||
|
||||
beforeAll(async () => {
|
||||
tempDb = await startEmbeddedPostgresTestDatabase("heartbeat-direct-adapter-isolation-");
|
||||
db = createDb(tempDb.connectionString);
|
||||
const directCodexAdapter: ServerAdapterModule = {
|
||||
type: "codex_local",
|
||||
supportsLocalAgentJwt: false,
|
||||
execute,
|
||||
testEnvironment: async () => ({
|
||||
adapterType: "codex_local",
|
||||
status: "pass",
|
||||
checks: [],
|
||||
testedAt: new Date(0).toISOString(),
|
||||
}),
|
||||
};
|
||||
registerServerAdapter(directCodexAdapter);
|
||||
}, 20_000);
|
||||
|
||||
afterEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
await db.execute(sql.raw(`
|
||||
TRUNCATE TABLE
|
||||
"native_run_finalizations",
|
||||
"status_decisions",
|
||||
"work_assessments",
|
||||
"native_run_results",
|
||||
"completion_contracts",
|
||||
"environment_leases",
|
||||
"environments",
|
||||
"activity_log",
|
||||
"heartbeat_run_events",
|
||||
"heartbeat_runs",
|
||||
"agent_wakeup_requests",
|
||||
"agent_runtime_state",
|
||||
"company_skills",
|
||||
"agents",
|
||||
"companies"
|
||||
RESTART IDENTITY CASCADE
|
||||
`));
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
unregisterServerAdapter("codex_local");
|
||||
await tempDb?.cleanup();
|
||||
});
|
||||
|
||||
it("executes flag-off codex_local once without creating native records", async () => {
|
||||
const companyId = randomUUID();
|
||||
const agentId = randomUUID();
|
||||
const directProofJson = '{"schema":"direct-proof.v1","value":"byte-stable"}';
|
||||
execute.mockResolvedValue({
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
provider: "codex",
|
||||
model: "test-codex",
|
||||
summary: "Direct adapter summary.",
|
||||
resultJson: { directProofJson },
|
||||
});
|
||||
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Direct compatibility",
|
||||
issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
||||
requireBoardApprovalForNewAgents: false,
|
||||
defaultResponsibleUserId: "responsible-user",
|
||||
});
|
||||
await db.insert(agents).values({
|
||||
id: agentId,
|
||||
companyId,
|
||||
name: "Direct Codex",
|
||||
role: "engineer",
|
||||
status: "idle",
|
||||
adapterType: "codex_local",
|
||||
adapterConfig: {},
|
||||
runtimeConfig: {},
|
||||
permissions: {},
|
||||
});
|
||||
|
||||
const heartbeat = heartbeatService(db);
|
||||
const queued = await heartbeat.invoke(agentId, "on_demand", {}, "manual");
|
||||
expect(queued).not.toBeNull();
|
||||
const finished = await waitForRunToFinish(heartbeat, queued!.id);
|
||||
|
||||
expect(execute).toHaveBeenCalledOnce();
|
||||
expect(finished).toMatchObject({
|
||||
status: "succeeded",
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
runtimeMode: "legacy",
|
||||
nativePhase: null,
|
||||
});
|
||||
const persistedResult = finished?.resultJson as Record<string, unknown> | null;
|
||||
expect(persistedResult?.directProofJson).toBe(directProofJson);
|
||||
|
||||
const nativeRows = await Promise.all([
|
||||
db.select().from(completionContracts),
|
||||
db.select().from(nativeRunResults),
|
||||
db.select().from(workAssessments),
|
||||
db.select().from(statusDecisions),
|
||||
db.select().from(nativeRunFinalizations),
|
||||
]);
|
||||
expect(nativeRows.every((rows) => rows.length === 0)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -10,6 +10,7 @@ describe("instance settings service", () => {
|
||||
it("ignores retired experimental flags without resetting current settings", () => {
|
||||
expect(normalizeExperimentalSettings({
|
||||
enableEnvironments: true,
|
||||
enableNativeRunner: false,
|
||||
enableManagedSandboxOnly: false,
|
||||
enableIsolatedWorkspaces: true,
|
||||
enableIssuePlanDecompositions: true,
|
||||
@@ -26,6 +27,7 @@ describe("instance settings service", () => {
|
||||
enableNewestFirstIssueThread: true,
|
||||
})).toEqual({
|
||||
enableEnvironments: true,
|
||||
enableNativeRunner: false,
|
||||
enableManagedSandboxOnly: false,
|
||||
enableIsolatedWorkspaces: true,
|
||||
enableStreamlinedLeftNavigation: true,
|
||||
|
||||
@@ -5,6 +5,9 @@ import { describe, expect, it } from "vitest";
|
||||
const packageJsonPath = fileURLToPath(
|
||||
new URL("../../package.json", import.meta.url),
|
||||
);
|
||||
const runnerShimPath = fileURLToPath(
|
||||
new URL("../vendor/paperclip-runner/index.ts", import.meta.url),
|
||||
);
|
||||
|
||||
describe("server package build script", () => {
|
||||
it("builds the compiled package entry during prepack", () => {
|
||||
@@ -52,4 +55,15 @@ describe("server package build script", () => {
|
||||
"cp -R ../packages/paperclip-runner/dist/. dist/vendor/paperclip-runner/",
|
||||
);
|
||||
});
|
||||
|
||||
it("loads runner source when the source server starts before workspace builds", () => {
|
||||
const shim = readFileSync(runnerShimPath, "utf8");
|
||||
|
||||
expect(shim).toContain(
|
||||
'"../../../../packages/paperclip-runner/src/index.ts"',
|
||||
);
|
||||
expect(shim).not.toContain(
|
||||
'export * from "@paperclipai/paperclip-runner"',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ export const BUILTIN_ADAPTER_TYPES = new Set([
|
||||
"acpx_local",
|
||||
"claude_local",
|
||||
"codex_local",
|
||||
"paperclip_runner",
|
||||
"cursor_cloud",
|
||||
"cursor",
|
||||
"gemini_local",
|
||||
|
||||
@@ -339,6 +339,52 @@ const codexLocalAdapter: ServerAdapterModule = {
|
||||
loginCapability: codexLoginCapability,
|
||||
};
|
||||
|
||||
const paperclipRunnerAdapter: ServerAdapterModule = {
|
||||
type: "paperclip_runner",
|
||||
async execute(ctx) {
|
||||
const message = "paperclip_runner requires the native runner coordinator";
|
||||
await ctx.onLog("stderr", `${message}\n`);
|
||||
return {
|
||||
exitCode: 1,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
errorMessage: message,
|
||||
errorCode: "paperclip_runner_coordinator_required",
|
||||
provider: "codex",
|
||||
summary: message,
|
||||
};
|
||||
},
|
||||
async testEnvironment(context) {
|
||||
const result = await codexTestEnvironment(context);
|
||||
return { ...result, adapterType: "paperclip_runner" };
|
||||
},
|
||||
listSkills: listCodexSkills,
|
||||
syncSkills: syncCodexSkills,
|
||||
sessionCodec: codexSessionCodec,
|
||||
models: codexModels,
|
||||
listModels: listCodexModels,
|
||||
refreshModels: refreshCodexModels,
|
||||
supportsLocalAgentJwt: false,
|
||||
supportsInstructionsBundle: false,
|
||||
requiresMaterializedRuntimeSkills: false,
|
||||
getRuntimeCommandSpec: (config) => buildNpmRuntimeCommandSpec(config, "codex", "@openai/codex"),
|
||||
agentConfigurationDoc:
|
||||
"# Paperclip Runner\n\nAdapter: paperclip_runner\n\nRuns Codex through the Rust Paperclip runner and authenticated PRP transport.\n",
|
||||
getConfigSchema: () => ({
|
||||
fields: [
|
||||
{
|
||||
key: "provider",
|
||||
label: "Provider",
|
||||
type: "select",
|
||||
default: "codex",
|
||||
options: [{ value: "codex", label: "Codex" }],
|
||||
hint: "Paperclip Runner currently supports only Codex app-server.",
|
||||
},
|
||||
],
|
||||
}),
|
||||
loginCapability: codexLoginCapability,
|
||||
};
|
||||
|
||||
const cursorLocalAdapter: ServerAdapterModule = {
|
||||
type: "cursor",
|
||||
execute: cursorExecute,
|
||||
@@ -518,6 +564,7 @@ function registerBuiltInAdapters() {
|
||||
acpxLocalAdapter,
|
||||
claudeLocalAdapter,
|
||||
codexLocalAdapter,
|
||||
paperclipRunnerAdapter,
|
||||
openCodeLocalAdapter,
|
||||
piLocalAdapter,
|
||||
cursorCloudAdapter,
|
||||
|
||||
+5
-1
@@ -70,6 +70,7 @@ import { sidebarPreferenceRoutes } from "./routes/sidebar-preferences.js";
|
||||
import { resourceMembershipRoutes } from "./routes/resource-memberships.js";
|
||||
import { inboxDismissalRoutes } from "./routes/inbox-dismissals.js";
|
||||
import { instanceSettingsRoutes } from "./routes/instance-settings.js";
|
||||
import { instanceSettingsService } from "./services/instance-settings.js";
|
||||
import { openApiRoutes } from "./routes/openapi.js";
|
||||
import {
|
||||
instanceDatabaseBackupRoutes,
|
||||
@@ -644,7 +645,10 @@ export async function createApp(
|
||||
{ toolGateway },
|
||||
),
|
||||
);
|
||||
api.use(adapterRoutes());
|
||||
api.use(adapterRoutes({
|
||||
getNativeRunnerEnabled: async () =>
|
||||
(await instanceSettingsService(db).getExperimental()).enableNativeRunner === true,
|
||||
}));
|
||||
api.use(
|
||||
accessRoutes(db, {
|
||||
deploymentMode: opts.deploymentMode,
|
||||
|
||||
@@ -259,7 +259,9 @@ function registerWithSessionManagement(adapter: ServerAdapterModule): void {
|
||||
// Router
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function adapterRoutes() {
|
||||
export function adapterRoutes(options: {
|
||||
getNativeRunnerEnabled?: () => Promise<boolean>;
|
||||
} = {}) {
|
||||
const router = Router();
|
||||
|
||||
/**
|
||||
@@ -280,6 +282,8 @@ export function adapterRoutes() {
|
||||
listAdapterPlugins().map((r) => [r.type, r]),
|
||||
);
|
||||
const disabledSet = new Set(getDisabledAdapterTypes());
|
||||
const nativeRunnerEnabled = await options.getNativeRunnerEnabled?.().catch(() => false) ?? false;
|
||||
if (!nativeRunnerEnabled) disabledSet.add("paperclip_runner");
|
||||
|
||||
const result: AdapterInfo[] = registeredAdapters.map((adapter) =>
|
||||
buildAdapterInfo(adapter, externalRecords.get(adapter.type), disabledSet),
|
||||
|
||||
@@ -1568,8 +1568,17 @@ export function agentRoutes(
|
||||
* (listEnabledServerAdapters documents the same rule: hidden from selection,
|
||||
* still functional for agents that already use them).
|
||||
*/
|
||||
function assertSelectableAdapterType(type: string | null | undefined): string {
|
||||
async function assertSelectableAdapterType(type: string | null | undefined): Promise<string> {
|
||||
const adapterType = assertKnownAdapterType(type);
|
||||
if (adapterType === "paperclip_runner") {
|
||||
const experimental = await instanceSettings.getExperimental();
|
||||
if (experimental.enableNativeRunner !== true) {
|
||||
throw unprocessable(
|
||||
"Paperclip Runner is experimental and disabled on this instance.",
|
||||
{ code: "paperclip_runner_rollout_disabled" },
|
||||
);
|
||||
}
|
||||
}
|
||||
const disabled = new Set(getDisabledAdapterTypes());
|
||||
if (!disabled.has(adapterType)) return adapterType;
|
||||
const available = listServerAdapters()
|
||||
@@ -3341,7 +3350,7 @@ export function agentRoutes(
|
||||
applyStoredClaudeLogin: hireApplyStoredClaudeLogin,
|
||||
...hireInput
|
||||
} = req.body;
|
||||
hireInput.adapterType = assertSelectableAdapterType(hireInput.adapterType);
|
||||
hireInput.adapterType = await assertSelectableAdapterType(hireInput.adapterType);
|
||||
const rawHireAdapterConfig = (hireInput.adapterConfig ?? {}) as Record<string, unknown>;
|
||||
assertNoNewAgentLegacyPromptTemplate(
|
||||
hireInput.adapterType,
|
||||
@@ -3560,7 +3569,7 @@ export function agentRoutes(
|
||||
applyStoredClaudeLogin: createApplyStoredClaudeLogin,
|
||||
...createInput
|
||||
} = req.body;
|
||||
createInput.adapterType = assertSelectableAdapterType(createInput.adapterType);
|
||||
createInput.adapterType = await assertSelectableAdapterType(createInput.adapterType);
|
||||
const rawCreateAdapterConfig = (createInput.adapterConfig ?? {}) as Record<string, unknown>;
|
||||
assertNoNewAgentLegacyPromptTemplate(
|
||||
createInput.adapterType,
|
||||
@@ -3997,12 +4006,12 @@ export function agentRoutes(
|
||||
// it gets the selectable check; keeping the agent's current adapter (even
|
||||
// one since disabled) stays allowed, so a disabled harness does not make an
|
||||
// existing agent uneditable.
|
||||
const requestedAdapterType = hasOwn(patchData, "adapterType")
|
||||
? (() => {
|
||||
const next = assertKnownAdapterType(patchData.adapterType as string | null | undefined);
|
||||
return next === existing.adapterType ? next : assertSelectableAdapterType(next);
|
||||
})()
|
||||
const nextAdapterType = hasOwn(patchData, "adapterType")
|
||||
? assertKnownAdapterType(patchData.adapterType as string | null | undefined)
|
||||
: existing.adapterType;
|
||||
const requestedAdapterType = nextAdapterType === existing.adapterType
|
||||
? nextAdapterType
|
||||
: await assertSelectableAdapterType(nextAdapterType);
|
||||
let requestedRuntimeConfig: Record<string, unknown> | null = null;
|
||||
if (hasOwn(patchData, "runtimeConfig")) {
|
||||
const runtimeConfig = asRecord(patchData.runtimeConfig);
|
||||
|
||||
@@ -85,6 +85,7 @@ import { companyService } from "./companies.js";
|
||||
import { validateCron } from "./cron.js";
|
||||
import { documentService } from "./documents.js";
|
||||
import { issueService } from "./issues.js";
|
||||
import { instanceSettingsService } from "./instance-settings.js";
|
||||
import { projectService } from "./projects.js";
|
||||
import { workProductService } from "./work-products.js";
|
||||
import { routineService } from "./routines.js";
|
||||
@@ -5230,6 +5231,28 @@ export function companyPortabilityService(db: Db, storage?: StorageService) {
|
||||
const warnings = [...plan.preview.warnings];
|
||||
const include = plan.include;
|
||||
|
||||
if (include.agents) {
|
||||
const importedAgentSlugs = new Set(
|
||||
plan.preview.plan.agentPlans
|
||||
.filter((entry) => entry.action !== "skip")
|
||||
.map((entry) => entry.slug),
|
||||
);
|
||||
const selectsNativeRunner = sourceManifest.agents.some((agent) =>
|
||||
importedAgentSlugs.has(agent.slug)
|
||||
&& (input.adapterOverrides?.[agent.slug]?.adapterType ?? agent.adapterType)
|
||||
=== "paperclip_runner",
|
||||
);
|
||||
if (
|
||||
selectsNativeRunner
|
||||
&& (await instanceSettingsService(db).getExperimental()).enableNativeRunner !== true
|
||||
) {
|
||||
throw unprocessable(
|
||||
"Paperclip Runner is experimental and disabled on this instance.",
|
||||
{ code: "paperclip_runner_rollout_disabled" },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Content-addressed blobs double as the bundle's tamper seal. Verify every
|
||||
// blob before any row is written so a corrupted package cannot leave a
|
||||
// partially imported company behind.
|
||||
|
||||
@@ -58,6 +58,7 @@ import {
|
||||
issueThreadInteractions,
|
||||
issues,
|
||||
issueWorkProducts,
|
||||
nativeRunFinalizations,
|
||||
projects,
|
||||
projectWorkspaces,
|
||||
routineRevisions,
|
||||
@@ -333,12 +334,24 @@ import {
|
||||
} from "./effective-run-config-fingerprints.js";
|
||||
import type { PluginWorkerManager } from "./plugin-worker-manager.js";
|
||||
import { serverVersion } from "../version.js";
|
||||
import { executeNativeCodexRunner } from "./native-runtime/native-codex-runner.js";
|
||||
import { prepareNativeHeartbeatRun } from "./native-runtime/prepare-native-run.js";
|
||||
import {
|
||||
NativeRunnerSelectionError,
|
||||
resolveHeartbeatRuntimeMode,
|
||||
} from "./native-runtime/runtime-mode.js";
|
||||
|
||||
const MAX_LIVE_LOG_CHUNK_BYTES = 8 * 1024;
|
||||
const MAX_PERSISTED_LOG_CHUNK_CHARS = 64 * 1024;
|
||||
const MAX_RUN_EVENT_PAYLOAD_STRING_CHARS = 16 * 1024;
|
||||
const MAX_RUN_EVENT_PAYLOAD_ARRAY_ITEMS = 50;
|
||||
|
||||
function nativeRunnerErrorCode(error: unknown): string | null {
|
||||
if (error instanceof NativeRunnerSelectionError) return error.code;
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
return message.match(/^(paperclip_runner_[a-z0-9_]+)/)?.[1] ?? null;
|
||||
}
|
||||
|
||||
export function redactDetectedSuccessfulRunProgressSummaryForBoard(
|
||||
summary: string,
|
||||
currentUserRedactionOptions?: CurrentUserRedactionOptions,
|
||||
@@ -7271,6 +7284,7 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
description: issues.description,
|
||||
status: issues.status,
|
||||
workMode: issues.workMode,
|
||||
reviewPolicy: issues.reviewPolicy,
|
||||
priority: issues.priority,
|
||||
projectId: issues.projectId,
|
||||
projectWorkspaceId: issues.projectWorkspaceId,
|
||||
@@ -14373,6 +14387,7 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
priority: issueContext.priority,
|
||||
workMode: issueContext.workMode,
|
||||
description: issueContext.description,
|
||||
reviewPolicy: issueContext.reviewPolicy,
|
||||
projectId: issueContext.projectId,
|
||||
projectWorkspaceId: issueContext.projectWorkspaceId,
|
||||
executionWorkspaceId: issueContext.executionWorkspaceId,
|
||||
@@ -15970,6 +15985,18 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
});
|
||||
};
|
||||
|
||||
const runtimeResolution = resolveHeartbeatRuntimeMode({
|
||||
persisted: {
|
||||
runtimeMode: run.runtimeMode,
|
||||
runtimeModeResolvedAt: run.runtimeModeResolvedAt,
|
||||
},
|
||||
enabled: resolvedInstanceSettings.experimental.enableNativeRunner === true,
|
||||
adapterType: agent.adapterType,
|
||||
adapterConfig: agent.adapterConfig,
|
||||
agentStatus: runningAgent.status,
|
||||
issue: issueRef ? { workMode: issueRef.workMode } : null,
|
||||
executionTarget,
|
||||
});
|
||||
const adapter = getServerAdapter(agent.adapterType);
|
||||
const localAgentJwtScope =
|
||||
issueRef?.workMode === "skill_test"
|
||||
@@ -16176,59 +16203,110 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
|
||||
let adapterResult: Awaited<ReturnType<typeof adapter.execute>>;
|
||||
try {
|
||||
const adapterContext = { ...context };
|
||||
const runtimeMcpServers = await buildPaperclipRuntimeMcpServers({
|
||||
db,
|
||||
agent,
|
||||
runId: run.id,
|
||||
});
|
||||
const runtimeMcp = createAdapterRuntimeMcpAccess(runtimeMcpServers);
|
||||
const managedMcpConfig = await createManagedMcpRunConfig({
|
||||
db,
|
||||
agent,
|
||||
runId: run.id,
|
||||
config: runtimeConfig,
|
||||
projectId: issueRef?.projectId ?? null,
|
||||
issueId: issueRef?.id ?? null,
|
||||
});
|
||||
if (managedMcpConfig) {
|
||||
adapterContext.paperclipManagedMcp = managedMcpConfig;
|
||||
const onSpawn = async (meta: {
|
||||
pid: number;
|
||||
processGroupId: number | null;
|
||||
startedAt: string;
|
||||
}) => {
|
||||
await persistRunProcessMetadata(run.id, {
|
||||
pid: meta.pid,
|
||||
processGroupId: meta.processGroupId,
|
||||
startedAt: meta.startedAt,
|
||||
});
|
||||
};
|
||||
if (runtimeResolution.kind === "native") {
|
||||
if (!issueRef) throw new Error("paperclip_runner_issue_required");
|
||||
const native = await prepareNativeHeartbeatRun({
|
||||
db,
|
||||
run,
|
||||
issue: issueRef,
|
||||
environmentLeaseId: activeEnvironmentLease.lease.id,
|
||||
});
|
||||
const prompt = readNonEmptyString(context.paperclipTaskMarkdown)
|
||||
?? `# ${issueRef.identifier ?? issueRef.id}: ${issueRef.title}`;
|
||||
const configuredTimeoutSec = Number(runtimeConfig.timeoutSec);
|
||||
const timeoutMs = Number.isFinite(configuredTimeoutSec) && configuredTimeoutSec > 0
|
||||
? Math.min(configuredTimeoutSec * 1_000, 24 * 60 * 60 * 1_000)
|
||||
: 60 * 60 * 1_000;
|
||||
const environment = Object.fromEntries(
|
||||
Object.entries(parseObject(runtimeConfig.env)).filter(
|
||||
(entry): entry is [string, string] => typeof entry[1] === "string",
|
||||
),
|
||||
);
|
||||
await onAdapterMeta({
|
||||
adapterType: "paperclip_runner",
|
||||
command: "paperclip-runnerd",
|
||||
cwd: executionWorkspace.cwd,
|
||||
promptMetrics: { promptChars: prompt.length },
|
||||
context: { provider: "codex", protocolVersion: 1 },
|
||||
});
|
||||
adapterResult = await executeNativeCodexRunner({
|
||||
db,
|
||||
companyId: agent.companyId,
|
||||
issueId: issueRef.id,
|
||||
runId: run.id,
|
||||
agentId: agent.id,
|
||||
runnerInstanceId: native.runnerInstanceId,
|
||||
environmentLeaseId: native.environmentLeaseId,
|
||||
normalizedSessionId: native.normalizedSessionId,
|
||||
turnId: native.turnId,
|
||||
itemId: native.itemId,
|
||||
cwd: executionWorkspace.cwd,
|
||||
prompt,
|
||||
model: readNonEmptyString(runtimeConfig.model),
|
||||
resumeProviderSessionId: runtimeSessionIdForAdapter,
|
||||
completionContract: native.completionContract,
|
||||
timeoutMs,
|
||||
environment,
|
||||
onLog,
|
||||
onSpawn,
|
||||
});
|
||||
} else {
|
||||
const adapterContext = { ...context };
|
||||
const runtimeMcpServers = await buildPaperclipRuntimeMcpServers({
|
||||
db,
|
||||
agent,
|
||||
runId: run.id,
|
||||
});
|
||||
const runtimeMcp = createAdapterRuntimeMcpAccess(runtimeMcpServers);
|
||||
const managedMcpConfig = await createManagedMcpRunConfig({
|
||||
db,
|
||||
agent,
|
||||
runId: run.id,
|
||||
config: runtimeConfig,
|
||||
projectId: issueRef?.projectId ?? null,
|
||||
issueId: issueRef?.id ?? null,
|
||||
});
|
||||
if (managedMcpConfig) {
|
||||
adapterContext.paperclipManagedMcp = managedMcpConfig;
|
||||
}
|
||||
adapterResult = await adapter.execute({
|
||||
runId: run.id,
|
||||
agent,
|
||||
runtime: runtimeForAdapter,
|
||||
config: runtimeConfig,
|
||||
context: adapterContext,
|
||||
runtimeCommandSpec: adapter.getRuntimeCommandSpec?.(runtimeConfig) ?? null,
|
||||
executionTarget,
|
||||
executionTransport: remoteExecution
|
||||
? { remoteExecution: remoteExecution as unknown as Record<string, unknown> }
|
||||
: undefined,
|
||||
runtimeMcp,
|
||||
onLog,
|
||||
onMeta: onAdapterMeta,
|
||||
onEvent: onAdapterEvent,
|
||||
// The endpoint-gated OpenTelemetry startup trace context. It is a
|
||||
// no-op unless `OTEL_EXPORTER_OTLP_ENDPOINT` is set and the OTel
|
||||
// packages are installed, so the sandbox-start span path stays inert
|
||||
// by default.
|
||||
startupTraceContext: getStartupTraceContext(),
|
||||
onRuntimeProgress: async (progress) => {
|
||||
await recordCurrentHeartbeatRunRuntimeProgress(run, progress, issueId);
|
||||
},
|
||||
onSpawn,
|
||||
authToken: authToken ?? undefined,
|
||||
});
|
||||
}
|
||||
adapterResult = await adapter.execute({
|
||||
runId: run.id,
|
||||
agent,
|
||||
runtime: runtimeForAdapter,
|
||||
config: runtimeConfig,
|
||||
context: adapterContext,
|
||||
runtimeCommandSpec: adapter.getRuntimeCommandSpec?.(runtimeConfig) ?? null,
|
||||
executionTarget,
|
||||
executionTransport: remoteExecution
|
||||
? { remoteExecution: remoteExecution as unknown as Record<string, unknown> }
|
||||
: undefined,
|
||||
runtimeMcp,
|
||||
onLog,
|
||||
onMeta: onAdapterMeta,
|
||||
onEvent: onAdapterEvent,
|
||||
// The endpoint-gated OpenTelemetry startup trace context. It is a
|
||||
// no-op unless `OTEL_EXPORTER_OTLP_ENDPOINT` is set and the OTel
|
||||
// packages are installed, so the sandbox-start span path stays inert
|
||||
// by default.
|
||||
startupTraceContext: getStartupTraceContext(),
|
||||
onRuntimeProgress: async (progress) => {
|
||||
await recordCurrentHeartbeatRunRuntimeProgress(run, progress, issueId);
|
||||
},
|
||||
onSpawn: async (meta) => {
|
||||
await persistRunProcessMetadata(run.id, {
|
||||
pid: meta.pid,
|
||||
processGroupId:
|
||||
"processGroupId" in meta && typeof meta.processGroupId === "number"
|
||||
? meta.processGroupId
|
||||
: null,
|
||||
startedAt: meta.startedAt,
|
||||
});
|
||||
},
|
||||
authToken: authToken ?? undefined,
|
||||
});
|
||||
// Adapter returned cleanly, which means its workspace-restore finally
|
||||
// block also ran without throwing. Record the workspace_finalize
|
||||
// barrier so dependents that share this executionWorkspace can wake.
|
||||
@@ -16501,6 +16579,28 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
return;
|
||||
}
|
||||
|
||||
if (runtimeResolution.kind === "native") {
|
||||
const nativePhase = status === "succeeded" ? "completed" : "failed";
|
||||
await db.transaction(async (tx) => {
|
||||
await tx
|
||||
.update(nativeRunFinalizations)
|
||||
.set({
|
||||
phase: nativePhase,
|
||||
failureCode: status === "succeeded" ? null : (runErrorCode ?? "provider_failed"),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(nativeRunFinalizations.runId, run.id));
|
||||
await tx
|
||||
.update(heartbeatRuns)
|
||||
.set({
|
||||
nativePhase,
|
||||
nativePhaseUpdatedAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(heartbeatRuns.id, run.id));
|
||||
});
|
||||
}
|
||||
|
||||
let persistedRun = persistedRunWrite.run;
|
||||
if (persistedRun) {
|
||||
persistedRun = await classifyAndPersistRunLiveness(persistedRun, persistedResultJson) ?? persistedRun;
|
||||
@@ -16679,6 +16779,7 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
const failureErrorCode =
|
||||
workspaceValidationFailure?.code
|
||||
?? configurationIncompleteFailure?.code
|
||||
?? nativeRunnerErrorCode(err)
|
||||
?? recordedResponsibleUserDenialCode
|
||||
?? "adapter_failed";
|
||||
logger.error({ err, runId }, "heartbeat execution failed");
|
||||
@@ -16727,6 +16828,16 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
}
|
||||
|
||||
const failedRun = failedRunWrite.run;
|
||||
if (failedRun?.runtimeMode === "native") {
|
||||
await db
|
||||
.update(heartbeatRuns)
|
||||
.set({ nativePhase: "failed", nativePhaseUpdatedAt: new Date(), updatedAt: new Date() })
|
||||
.where(eq(heartbeatRuns.id, failedRun.id));
|
||||
await db
|
||||
.update(nativeRunFinalizations)
|
||||
.set({ phase: "failed", failureCode: failureErrorCode, updatedAt: new Date() })
|
||||
.where(eq(nativeRunFinalizations.runId, failedRun.id));
|
||||
}
|
||||
await setWakeupStatus(run.wakeupRequestId, "failed", {
|
||||
finishedAt: new Date(),
|
||||
error: message,
|
||||
@@ -16829,6 +16940,7 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
workspaceValidationSetupFailure?.code ??
|
||||
configurationIncompleteSetupFailure?.code ??
|
||||
(unresolvedBaseRefSetupFailure ? CONFIGURATION_INCOMPLETE_FAILURE_CODE : null) ??
|
||||
nativeRunnerErrorCode(outerErr) ??
|
||||
recordedResponsibleUserDenialCode ??
|
||||
"setup_failed";
|
||||
logger.error({ err: outerErr, runId }, "heartbeat execution setup failed");
|
||||
|
||||
@@ -219,6 +219,7 @@ export function normalizeExperimentalSettings(raw: unknown): InstanceExperimenta
|
||||
if (parsed.success) {
|
||||
return {
|
||||
enableEnvironments: parsed.data.enableEnvironments ?? false,
|
||||
enableNativeRunner: parsed.data.enableNativeRunner ?? false,
|
||||
enableManagedSandboxOnly: parsed.data.enableManagedSandboxOnly ?? false,
|
||||
enableIsolatedWorkspaces: parsed.data.enableIsolatedWorkspaces ?? false,
|
||||
enableStreamlinedLeftNavigation: parsed.data.enableStreamlinedLeftNavigation ?? true,
|
||||
@@ -257,6 +258,7 @@ export function normalizeExperimentalSettings(raw: unknown): InstanceExperimenta
|
||||
}
|
||||
return {
|
||||
enableEnvironments: false,
|
||||
enableNativeRunner: false,
|
||||
enableManagedSandboxOnly: false,
|
||||
enableIsolatedWorkspaces: false,
|
||||
enableStreamlinedLeftNavigation: true,
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
export function canonicalNativeJson(value: unknown): string {
|
||||
if (Array.isArray(value)) return `[${value.map(canonicalNativeJson).join(",")}]`;
|
||||
if (typeof value === "object" && value !== null) {
|
||||
const record = value as Record<string, unknown>;
|
||||
return `{${Object.keys(record)
|
||||
.filter((key) => record[key] !== undefined)
|
||||
.sort()
|
||||
.map((key) => `${JSON.stringify(key)}:${canonicalNativeJson(record[key])}`)
|
||||
.join(",")}}`;
|
||||
}
|
||||
return JSON.stringify(value) ?? "null";
|
||||
}
|
||||
|
||||
export function nativeSha256(value: unknown): string {
|
||||
return createHash("sha256").update(canonicalNativeJson(value), "utf8").digest("hex");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { buildNativeCompletionContract } from "./completion-contracts.js";
|
||||
|
||||
describe("buildNativeCompletionContract", () => {
|
||||
it("uses the task description as the single initial criterion", () => {
|
||||
expect(buildNativeCompletionContract({
|
||||
title: "Ship the runner",
|
||||
description: "Prove the Codex vertical slice.",
|
||||
})).toEqual({
|
||||
revision: "1",
|
||||
objective: "Ship the runner",
|
||||
criteria: [{ id: "objective", requirement: "Prove the Codex vertical slice." }],
|
||||
});
|
||||
});
|
||||
|
||||
it("binds the persisted numeric revision into the protocol contract", () => {
|
||||
expect(buildNativeCompletionContract({
|
||||
title: "Continue the runner",
|
||||
description: null,
|
||||
}, 3).revision).toBe("3");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,101 @@
|
||||
import { and, desc, eq, sql } from "drizzle-orm";
|
||||
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { completionContracts } from "@paperclipai/db";
|
||||
|
||||
import { nativeSha256 } from "./canonical.js";
|
||||
|
||||
export const NATIVE_COMPLETION_CONTRACT_SCHEMA = "paperclip.completion-contract.v1";
|
||||
export const NATIVE_COMPLETION_POLICY_VERSION = "paperclip-runner-v1";
|
||||
|
||||
interface NativeCompletionContractInput {
|
||||
revision: string;
|
||||
objective: string;
|
||||
criteria: Array<{ id: string; requirement: string }>;
|
||||
}
|
||||
|
||||
export function buildNativeCompletionContract(issue: {
|
||||
title: string;
|
||||
description: string | null;
|
||||
}, revision = 1): NativeCompletionContractInput {
|
||||
return {
|
||||
revision: String(revision),
|
||||
objective: issue.title,
|
||||
criteria: [{
|
||||
id: "objective",
|
||||
requirement: issue.description?.trim() || `Complete: ${issue.title}`,
|
||||
}],
|
||||
};
|
||||
}
|
||||
|
||||
export async function ensureNativeCompletionContract(input: {
|
||||
db: Db;
|
||||
companyId: string;
|
||||
issue: {
|
||||
id: string;
|
||||
title: string;
|
||||
description: string | null;
|
||||
reviewPolicy?: string | null;
|
||||
};
|
||||
actorId: string;
|
||||
}) {
|
||||
return input.db.transaction(async (tx) => {
|
||||
await tx.execute(sql`select pg_advisory_xact_lock(hashtextextended(${[
|
||||
"paperclip:native-completion-contract",
|
||||
input.companyId,
|
||||
input.issue.id,
|
||||
].join(":")}, 0))`);
|
||||
const externalReviewRequired = ["human_only", "not_creator"].includes(
|
||||
input.issue.reviewPolicy ?? "",
|
||||
);
|
||||
const policy = externalReviewRequired
|
||||
? { risk: "standard", completionAuthority: "server_arbiter" }
|
||||
: { risk: "low", completionAuthority: "agent_claim_policy" };
|
||||
const latest = await tx
|
||||
.select()
|
||||
.from(completionContracts)
|
||||
.where(and(
|
||||
eq(completionContracts.companyId, input.companyId),
|
||||
eq(completionContracts.issueId, input.issue.id),
|
||||
))
|
||||
.orderBy(desc(completionContracts.revision))
|
||||
.limit(1)
|
||||
.then((rows) => rows[0] ?? null);
|
||||
const latestRevision = latest?.revision ?? 1;
|
||||
const latestCandidate = buildNativeCompletionContract(input.issue, latestRevision);
|
||||
const latestCandidateSha256 = nativeSha256({
|
||||
schemaVersion: NATIVE_COMPLETION_CONTRACT_SCHEMA,
|
||||
policyVersion: NATIVE_COMPLETION_POLICY_VERSION,
|
||||
...policy,
|
||||
contract: latestCandidate,
|
||||
});
|
||||
if (latest?.canonicalSha256 === latestCandidateSha256) {
|
||||
return { row: latest, contract: latestCandidate };
|
||||
}
|
||||
|
||||
const nextRevision = latest ? latest.revision + 1 : 1;
|
||||
const contract = buildNativeCompletionContract(input.issue, nextRevision);
|
||||
const canonicalSha256 = nativeSha256({
|
||||
schemaVersion: NATIVE_COMPLETION_CONTRACT_SCHEMA,
|
||||
policyVersion: NATIVE_COMPLETION_POLICY_VERSION,
|
||||
...policy,
|
||||
contract,
|
||||
});
|
||||
const [row] = await tx.insert(completionContracts).values({
|
||||
companyId: input.companyId,
|
||||
issueId: input.issue.id,
|
||||
revision: nextRevision,
|
||||
schemaVersion: NATIVE_COMPLETION_CONTRACT_SCHEMA,
|
||||
policyVersion: NATIVE_COMPLETION_POLICY_VERSION,
|
||||
...policy,
|
||||
incompleteCriteriaPolicy: "preserve_non_terminal",
|
||||
contractJson: contract as unknown as Record<string, unknown>,
|
||||
canonicalSha256,
|
||||
createdByActorType: "system",
|
||||
createdByActorId: input.actorId,
|
||||
supersedesContractId: latest?.id ?? null,
|
||||
}).returning();
|
||||
if (!row) throw new Error("native_completion_contract_not_persisted");
|
||||
return { row, contract };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { existsSync } from "node:fs";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { createServer, type Server } from "node:http";
|
||||
import { tmpdir } from "node:os";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
agents,
|
||||
companies,
|
||||
createDb,
|
||||
heartbeatRunEvents,
|
||||
heartbeatRuns,
|
||||
issues,
|
||||
nativeRunFinalizations,
|
||||
nativeRunResults,
|
||||
} from "@paperclipai/db";
|
||||
|
||||
import {
|
||||
getEmbeddedPostgresTestSupport,
|
||||
startEmbeddedPostgresTestDatabase,
|
||||
} from "../../__tests__/helpers/embedded-postgres.js";
|
||||
import {
|
||||
runnerPrpWebSocketInternals,
|
||||
setupRunnerPrpWebSocketServer,
|
||||
} from "../../realtime/runner-prp-ws.js";
|
||||
import { executeNativeCodexRunner } from "./native-codex-runner.js";
|
||||
import { prepareNativeHeartbeatRun } from "./prepare-native-run.js";
|
||||
|
||||
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
||||
|
||||
if (!embeddedPostgresSupport.supported) {
|
||||
console.warn(
|
||||
`Skipping native Codex vertical-slice test on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`,
|
||||
);
|
||||
}
|
||||
|
||||
const runnerWorkspace = resolve(
|
||||
import.meta.dirname,
|
||||
"../../../../packages/paperclip-runner/runner",
|
||||
);
|
||||
const executableSuffix = process.platform === "win32" ? ".exe" : "";
|
||||
const runnerBinary = resolve(
|
||||
runnerWorkspace,
|
||||
"target",
|
||||
"release",
|
||||
`paperclip-runnerd${executableSuffix}`,
|
||||
);
|
||||
const fakeCodexBinary = resolve(
|
||||
runnerWorkspace,
|
||||
"target",
|
||||
"release",
|
||||
`fake-codex-app-server${executableSuffix}`,
|
||||
);
|
||||
|
||||
function ensureRunnerTestBinaries(): void {
|
||||
if (existsSync(runnerBinary) && existsSync(fakeCodexBinary)) return;
|
||||
execFileSync("cargo", [
|
||||
"build",
|
||||
"--release",
|
||||
"--locked",
|
||||
"-p",
|
||||
"paperclip-runner-core",
|
||||
"--bin",
|
||||
"paperclip-runnerd",
|
||||
"--bin",
|
||||
"fake-codex-app-server",
|
||||
], {
|
||||
cwd: runnerWorkspace,
|
||||
stdio: "inherit",
|
||||
timeout: 180_000,
|
||||
});
|
||||
}
|
||||
|
||||
async function closeServer(server: Server | null): Promise<void> {
|
||||
if (!server) return;
|
||||
server.closeAllConnections();
|
||||
if (!server.listening) return;
|
||||
await new Promise<void>((resolveClose, rejectClose) => {
|
||||
server.close((error) => error ? rejectClose(error) : resolveClose());
|
||||
});
|
||||
}
|
||||
|
||||
describeEmbeddedPostgres("native Codex server vertical slice", () => {
|
||||
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
|
||||
let runtimeRoot: string | null = null;
|
||||
let server: Server | null = null;
|
||||
|
||||
beforeAll(async () => {
|
||||
ensureRunnerTestBinaries();
|
||||
temporary = await startEmbeddedPostgresTestDatabase("native-codex-vertical-slice-");
|
||||
runtimeRoot = await mkdtemp(resolve(tmpdir(), "native-codex-runtime-"));
|
||||
server = createServer();
|
||||
await new Promise<void>((resolveListen) => server!.listen(0, "127.0.0.1", resolveListen));
|
||||
const address = server.address();
|
||||
if (!address || typeof address === "string") throw new Error("Expected a TCP listener");
|
||||
setupRunnerPrpWebSocketServer(server, {
|
||||
apiUrl: `http://127.0.0.1:${address.port}`,
|
||||
});
|
||||
}, 240_000);
|
||||
|
||||
afterAll(async () => {
|
||||
runnerPrpWebSocketInternals.resetForTests();
|
||||
await closeServer(server);
|
||||
await temporary?.cleanup();
|
||||
if (runtimeRoot) await rm(runtimeRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("returns a durable result and resumes the provider session on the next run", async () => {
|
||||
if (!temporary || !runtimeRoot) throw new Error("Vertical-slice fixture was not initialized");
|
||||
const db = createDb(temporary.connectionString);
|
||||
const companyId = randomUUID();
|
||||
const agentId = randomUUID();
|
||||
const issueId = randomUUID();
|
||||
const runId = randomUUID();
|
||||
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Native Codex vertical slice",
|
||||
issuePrefix: "NCV",
|
||||
requireBoardApprovalForNewAgents: false,
|
||||
});
|
||||
await db.insert(agents).values({
|
||||
id: agentId,
|
||||
companyId,
|
||||
name: "Native Codex",
|
||||
role: "engineer",
|
||||
status: "active",
|
||||
adapterType: "paperclip_runner",
|
||||
adapterConfig: { provider: "codex" },
|
||||
runtimeConfig: {},
|
||||
permissions: {},
|
||||
});
|
||||
await db.insert(issues).values({
|
||||
id: issueId,
|
||||
companyId,
|
||||
identifier: "NCV-1",
|
||||
title: "Complete the native Codex vertical slice",
|
||||
description: "Return a bound structured completion result.",
|
||||
status: "in_progress",
|
||||
priority: "medium",
|
||||
workMode: "standard",
|
||||
assigneeAgentId: agentId,
|
||||
});
|
||||
const [run] = await db.insert(heartbeatRuns).values({
|
||||
id: runId,
|
||||
companyId,
|
||||
agentId,
|
||||
status: "running",
|
||||
invocationSource: "assignment",
|
||||
triggerDetail: "system",
|
||||
contextSnapshot: { issueId },
|
||||
}).returning();
|
||||
if (!run) throw new Error("Failed to seed native run");
|
||||
await db
|
||||
.update(issues)
|
||||
.set({ executionRunId: runId })
|
||||
.where(eq(issues.id, issueId));
|
||||
|
||||
const native = await prepareNativeHeartbeatRun({
|
||||
db,
|
||||
run,
|
||||
issue: {
|
||||
id: issueId,
|
||||
title: "Complete the native Codex vertical slice",
|
||||
description: "Return a bound structured completion result.",
|
||||
reviewPolicy: null,
|
||||
},
|
||||
environmentLeaseId: "lease-native-codex-e2e",
|
||||
});
|
||||
const logs: string[] = [];
|
||||
const execute = executeNativeCodexRunner({
|
||||
db,
|
||||
companyId,
|
||||
issueId,
|
||||
runId,
|
||||
agentId,
|
||||
runnerInstanceId: native.runnerInstanceId,
|
||||
environmentLeaseId: native.environmentLeaseId,
|
||||
normalizedSessionId: native.normalizedSessionId,
|
||||
turnId: native.turnId,
|
||||
itemId: native.itemId,
|
||||
cwd: tmpdir(),
|
||||
prompt: "Complete the fake native Codex turn.",
|
||||
model: "test-model",
|
||||
resumeProviderSessionId: null,
|
||||
completionContract: native.completionContract,
|
||||
timeoutMs: 30_000,
|
||||
environment: {},
|
||||
runnerBinary,
|
||||
runtimeRoot,
|
||||
providerLaunch: {
|
||||
command: fakeCodexBinary,
|
||||
args: [
|
||||
"--state-file",
|
||||
resolve(runtimeRoot, "fake-codex-state.json"),
|
||||
"--call-log",
|
||||
resolve(runtimeRoot, "fake-codex-calls.log"),
|
||||
],
|
||||
providerVersion: "fake-codex-v1",
|
||||
},
|
||||
onLog: async (_stream, chunk) => {
|
||||
logs.push(chunk);
|
||||
},
|
||||
onSpawn: async () => undefined,
|
||||
});
|
||||
const result = await execute.catch((error) => {
|
||||
throw new Error(
|
||||
`${error instanceof Error ? error.message : String(error)}\n${logs.join("")}`,
|
||||
);
|
||||
});
|
||||
|
||||
expect(result).toMatchObject({
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
provider: "codex",
|
||||
sessionParams: { sessionId: "codex-thread-1" },
|
||||
summary: "Codex completed the fake turn.",
|
||||
resultJson: {
|
||||
nativeRunner: {
|
||||
result: {
|
||||
schema: "paperclip.run_result.v1",
|
||||
completionClaim: {
|
||||
contractRevision: "1",
|
||||
objectiveSatisfied: true,
|
||||
criteria: [{ criterionId: "objective", status: "satisfied" }],
|
||||
},
|
||||
},
|
||||
terminal: {
|
||||
schema: "paperclip.prp.terminal.v1",
|
||||
runTerminalState: "succeeded",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(logs.join("\n")).not.toContain("PAPERCLIP_RUNNER_BOOTSTRAP_TICKET");
|
||||
|
||||
const [persistedResult] = await db
|
||||
.select()
|
||||
.from(nativeRunResults)
|
||||
.where(eq(nativeRunResults.runId, runId));
|
||||
expect(persistedResult).toMatchObject({ schemaStatus: "accepted" });
|
||||
const [finalization] = await db
|
||||
.select()
|
||||
.from(nativeRunFinalizations)
|
||||
.where(eq(nativeRunFinalizations.runId, runId));
|
||||
expect(finalization).toMatchObject({ phase: "workspace_finalizing" });
|
||||
const eventTypes = await db
|
||||
.select({ eventType: heartbeatRunEvents.eventType })
|
||||
.from(heartbeatRunEvents)
|
||||
.where(eq(heartbeatRunEvents.runId, runId));
|
||||
expect(eventTypes.map((event) => event.eventType)).toEqual(expect.arrayContaining([
|
||||
"turn.completed",
|
||||
"run.result.proposed",
|
||||
"run.terminal",
|
||||
]));
|
||||
|
||||
const resumedRunId = randomUUID();
|
||||
const [resumedRun] = await db.insert(heartbeatRuns).values({
|
||||
id: resumedRunId,
|
||||
companyId,
|
||||
agentId,
|
||||
status: "running",
|
||||
invocationSource: "assignment",
|
||||
triggerDetail: "system",
|
||||
contextSnapshot: { issueId },
|
||||
}).returning();
|
||||
if (!resumedRun) throw new Error("Failed to seed resumed native run");
|
||||
await db
|
||||
.update(issues)
|
||||
.set({ executionRunId: resumedRunId })
|
||||
.where(eq(issues.id, issueId));
|
||||
const resumedNative = await prepareNativeHeartbeatRun({
|
||||
db,
|
||||
run: resumedRun,
|
||||
issue: {
|
||||
id: issueId,
|
||||
title: "Complete the native Codex vertical slice",
|
||||
description: "Return a bound structured completion result.",
|
||||
reviewPolicy: null,
|
||||
},
|
||||
environmentLeaseId: "lease-native-codex-resume",
|
||||
});
|
||||
const resumed = await executeNativeCodexRunner({
|
||||
db,
|
||||
companyId,
|
||||
issueId,
|
||||
runId: resumedRunId,
|
||||
agentId,
|
||||
runnerInstanceId: resumedNative.runnerInstanceId,
|
||||
environmentLeaseId: resumedNative.environmentLeaseId,
|
||||
normalizedSessionId: resumedNative.normalizedSessionId,
|
||||
turnId: resumedNative.turnId,
|
||||
itemId: resumedNative.itemId,
|
||||
cwd: tmpdir(),
|
||||
prompt: "Continue the fake native Codex session.",
|
||||
model: "test-model",
|
||||
resumeProviderSessionId: "codex-thread-1",
|
||||
completionContract: resumedNative.completionContract,
|
||||
timeoutMs: 30_000,
|
||||
environment: {},
|
||||
runnerBinary,
|
||||
runtimeRoot,
|
||||
providerLaunch: {
|
||||
command: fakeCodexBinary,
|
||||
args: [
|
||||
"--state-file",
|
||||
resolve(runtimeRoot, "fake-codex-state.json"),
|
||||
"--call-log",
|
||||
resolve(runtimeRoot, "fake-codex-calls.log"),
|
||||
],
|
||||
providerVersion: "fake-codex-v1",
|
||||
},
|
||||
onLog: async (_stream, chunk) => {
|
||||
logs.push(chunk);
|
||||
},
|
||||
onSpawn: async () => undefined,
|
||||
});
|
||||
expect(resumed).toMatchObject({
|
||||
exitCode: 0,
|
||||
sessionParams: { sessionId: "codex-thread-1" },
|
||||
});
|
||||
const providerCalls = await readFile(
|
||||
resolve(runtimeRoot, "fake-codex-calls.log"),
|
||||
"utf8",
|
||||
);
|
||||
expect(providerCalls.match(/^thread\/start$/gm)).toHaveLength(1);
|
||||
expect(providerCalls.match(/^thread\/resume$/gm)).toHaveLength(1);
|
||||
}, 60_000);
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { buildNativeRunnerArguments } from "./native-codex-runner.js";
|
||||
|
||||
describe("buildNativeRunnerArguments", () => {
|
||||
it("binds every durable identity without exposing the bootstrap ticket", () => {
|
||||
const args = buildNativeRunnerArguments({
|
||||
connectUrl: "ws://127.0.0.1:3000/api/runner/v1/connect/run-1",
|
||||
stateDirectory: "/tmp/runner-state",
|
||||
runnerInstanceId: "runner-1",
|
||||
environmentLeaseId: "lease-1",
|
||||
runId: "run-1",
|
||||
normalizedSessionId: "session-1",
|
||||
turnId: "turn-1",
|
||||
itemId: "item-1",
|
||||
runnerDigest: `sha256:${"a".repeat(64)}`,
|
||||
maxRuntimeMs: 60_000,
|
||||
});
|
||||
expect(args).toContain("--connect-url");
|
||||
expect(args).toContain("--runner-digest");
|
||||
expect(args.join(" ")).not.toContain("bootstrap");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,291 @@
|
||||
import { spawn, type ChildProcess } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { accessSync, chmodSync, constants, mkdirSync, readFileSync } from "node:fs";
|
||||
import { dirname, isAbsolute, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import type { AdapterExecutionResult } from "@paperclipai/adapter-utils";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
|
||||
import { resolvePaperclipInstanceRoot } from "../../home-paths.js";
|
||||
import { runnerPrpCoordinator } from "./runner-prp-coordinator.js";
|
||||
|
||||
const moduleDirectory = dirname(fileURLToPath(import.meta.url));
|
||||
const RUNNER_VERSION = "paperclip-runner-v1";
|
||||
|
||||
function executableName(): string {
|
||||
return process.platform === "win32" ? "paperclip-runnerd.exe" : "paperclip-runnerd";
|
||||
}
|
||||
|
||||
export function resolvePaperclipRunnerBinary(
|
||||
configuredPath = process.env.PAPERCLIP_RUNNER_BINARY,
|
||||
): string {
|
||||
const candidates = [
|
||||
configuredPath,
|
||||
resolve(moduleDirectory, "../../vendor/paperclip-runner/bin", executableName()),
|
||||
resolve(moduleDirectory, "../../../../packages/paperclip-runner/dist/bin", executableName()),
|
||||
resolve(
|
||||
moduleDirectory,
|
||||
"../../../../packages/paperclip-runner/runner/target/release",
|
||||
executableName(),
|
||||
),
|
||||
].filter((candidate): candidate is string => Boolean(candidate));
|
||||
if (configuredPath && !isAbsolute(configuredPath)) {
|
||||
throw new Error("PAPERCLIP_RUNNER_BINARY must be an absolute path");
|
||||
}
|
||||
for (const candidate of candidates) {
|
||||
try {
|
||||
accessSync(candidate, constants.R_OK | (process.platform === "win32" ? 0 : constants.X_OK));
|
||||
return candidate;
|
||||
} catch {
|
||||
// Continue through the fixed production and workspace locations.
|
||||
}
|
||||
}
|
||||
throw new Error(
|
||||
"paperclip_runner_binary_missing: build @paperclipai/paperclip-runner or set PAPERCLIP_RUNNER_BINARY",
|
||||
);
|
||||
}
|
||||
|
||||
export function buildNativeRunnerArguments(input: {
|
||||
connectUrl: string;
|
||||
stateDirectory: string;
|
||||
runnerInstanceId: string;
|
||||
environmentLeaseId: string;
|
||||
runId: string;
|
||||
normalizedSessionId: string;
|
||||
turnId: string;
|
||||
itemId: string;
|
||||
runnerDigest: string;
|
||||
maxRuntimeMs: number;
|
||||
}): string[] {
|
||||
return [
|
||||
"--connect-url", input.connectUrl,
|
||||
"--state-dir", input.stateDirectory,
|
||||
"--runner-id", input.runnerInstanceId,
|
||||
"--environment-lease-id", input.environmentLeaseId,
|
||||
"--run-id", input.runId,
|
||||
"--session-id", input.normalizedSessionId,
|
||||
"--turn-id", input.turnId,
|
||||
"--item-id", input.itemId,
|
||||
"--runner-version", RUNNER_VERSION,
|
||||
"--runner-digest", input.runnerDigest,
|
||||
"--max-runtime-ms", String(input.maxRuntimeMs),
|
||||
];
|
||||
}
|
||||
|
||||
function privateDirectory(path: string): void {
|
||||
mkdirSync(path, { recursive: true, mode: 0o700 });
|
||||
if (process.platform !== "win32") chmodSync(path, 0o700);
|
||||
}
|
||||
|
||||
function waitForExit(child: ChildProcess): Promise<{
|
||||
code: number | null;
|
||||
signal: NodeJS.Signals | null;
|
||||
}> {
|
||||
return new Promise((resolveExit, rejectExit) => {
|
||||
child.once("error", rejectExit);
|
||||
child.once("exit", (code, signal) => resolveExit({ code, signal }));
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForChildExit(exit: Promise<unknown>, timeoutMs: number): Promise<boolean> {
|
||||
let timer: NodeJS.Timeout | null = null;
|
||||
try {
|
||||
return await Promise.race([
|
||||
exit.then(() => true),
|
||||
new Promise<false>((resolveTimeout) => {
|
||||
timer = setTimeout(() => resolveTimeout(false), timeoutMs);
|
||||
timer.unref();
|
||||
}),
|
||||
]);
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
function signalRunnerProcessGroup(child: ChildProcess, signal: NodeJS.Signals): void {
|
||||
if (process.platform !== "win32" && child.pid) {
|
||||
try {
|
||||
process.kill(-child.pid, signal);
|
||||
return;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ESRCH") return;
|
||||
}
|
||||
}
|
||||
child.kill(signal);
|
||||
}
|
||||
|
||||
async function stopChild(
|
||||
child: ChildProcess,
|
||||
exit: Promise<unknown>,
|
||||
allowGracefulExit = false,
|
||||
): Promise<void> {
|
||||
if (child.exitCode !== null || child.signalCode !== null) return;
|
||||
if (allowGracefulExit && await waitForChildExit(exit, 5_000)) return;
|
||||
signalRunnerProcessGroup(child, "SIGTERM");
|
||||
if (await waitForChildExit(exit, 5_000)) return;
|
||||
if (child.exitCode === null && child.signalCode === null) {
|
||||
signalRunnerProcessGroup(child, "SIGKILL");
|
||||
}
|
||||
}
|
||||
|
||||
export async function executeNativeCodexRunner(input: {
|
||||
db: Db;
|
||||
companyId: string;
|
||||
issueId: string;
|
||||
runId: string;
|
||||
agentId: string;
|
||||
runnerInstanceId: string;
|
||||
environmentLeaseId: string;
|
||||
normalizedSessionId: string;
|
||||
turnId: string;
|
||||
itemId: string;
|
||||
cwd: string;
|
||||
prompt: string;
|
||||
model: string | null;
|
||||
resumeProviderSessionId: string | null;
|
||||
completionContract: { revision: string; criterionIds: string[] };
|
||||
timeoutMs: number;
|
||||
environment: Record<string, string>;
|
||||
/** Internal test seam; production always resolves the packaged binary. */
|
||||
runnerBinary?: string;
|
||||
/** Internal test seam; production always uses the instance runtime root. */
|
||||
runtimeRoot?: string;
|
||||
/** Internal conformance seam; production always launches `codex app-server`. */
|
||||
providerLaunch?: {
|
||||
command: string;
|
||||
args: string[];
|
||||
providerVersion?: string;
|
||||
};
|
||||
onLog: (stream: "stdout" | "stderr", chunk: string) => Promise<void>;
|
||||
onSpawn: (meta: {
|
||||
pid: number;
|
||||
processGroupId: number | null;
|
||||
startedAt: string;
|
||||
}) => Promise<void>;
|
||||
}): Promise<AdapterExecutionResult> {
|
||||
const binary = input.runnerBinary ?? resolvePaperclipRunnerBinary();
|
||||
const runnerDigest = `sha256:${createHash("sha256").update(readFileSync(binary)).digest("hex")}`;
|
||||
const runtimeRoot = input.runtimeRoot
|
||||
? resolve(input.runtimeRoot)
|
||||
: resolve(resolvePaperclipInstanceRoot(), "runtime", "paperclip-runner");
|
||||
const runnerStateDirectory = resolve(runtimeRoot, "runner", input.runId);
|
||||
privateDirectory(runtimeRoot);
|
||||
privateDirectory(resolve(runtimeRoot, "control-plane"));
|
||||
privateDirectory(resolve(runtimeRoot, "runner"));
|
||||
privateDirectory(runnerStateDirectory);
|
||||
|
||||
const prepared = await runnerPrpCoordinator(input.db, {
|
||||
stateRoot: resolve(runtimeRoot, "control-plane"),
|
||||
}).prepare({
|
||||
companyId: input.companyId,
|
||||
issueId: input.issueId,
|
||||
runId: input.runId,
|
||||
agentId: input.agentId,
|
||||
runnerInstanceId: input.runnerInstanceId,
|
||||
environmentLeaseId: input.environmentLeaseId,
|
||||
normalizedSessionId: input.normalizedSessionId,
|
||||
turnId: input.turnId,
|
||||
itemId: input.itemId,
|
||||
runnerVersion: RUNNER_VERSION,
|
||||
runnerDigest,
|
||||
});
|
||||
|
||||
prepared.queueCommand("run.prepare", {
|
||||
provider: {
|
||||
provider: "codex",
|
||||
driver: "codex_app_server",
|
||||
providerVersion: input.providerLaunch?.providerVersion ?? "codex-app-server-v1",
|
||||
command: input.providerLaunch?.command ?? "codex",
|
||||
args: input.providerLaunch?.args ?? ["app-server"],
|
||||
cwd: input.cwd,
|
||||
...(input.model ? { model: input.model } : {}),
|
||||
...(input.resumeProviderSessionId
|
||||
? { providerSessionId: input.resumeProviderSessionId }
|
||||
: {}),
|
||||
instructions: "",
|
||||
approvalPolicy: "never",
|
||||
},
|
||||
completionContract: input.completionContract,
|
||||
}, `prepare_${input.runId}`);
|
||||
prepared.queueCommand("session.open", {}, `open_${input.runId}`);
|
||||
prepared.queueCommand("turn.start", { text: input.prompt }, `turn_${input.runId}`);
|
||||
|
||||
const child = spawn(binary, buildNativeRunnerArguments({
|
||||
connectUrl: prepared.connectUrl,
|
||||
stateDirectory: runnerStateDirectory,
|
||||
runnerInstanceId: input.runnerInstanceId,
|
||||
environmentLeaseId: input.environmentLeaseId,
|
||||
runId: input.runId,
|
||||
normalizedSessionId: input.normalizedSessionId,
|
||||
turnId: input.turnId,
|
||||
itemId: input.itemId,
|
||||
runnerDigest,
|
||||
maxRuntimeMs: input.timeoutMs,
|
||||
}), {
|
||||
cwd: input.cwd,
|
||||
detached: process.platform !== "win32",
|
||||
env: {
|
||||
...process.env,
|
||||
...input.environment,
|
||||
PAPERCLIP_RUNNER_BOOTSTRAP_TICKET: prepared.bootstrapTicket,
|
||||
},
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
const exit = waitForExit(child);
|
||||
child.stdout?.on("data", (chunk: Buffer) => {
|
||||
void input.onLog("stdout", chunk.toString("utf8"));
|
||||
});
|
||||
child.stderr?.on("data", (chunk: Buffer) => {
|
||||
void input.onLog("stderr", chunk.toString("utf8"));
|
||||
});
|
||||
|
||||
try {
|
||||
if (!child.pid) throw new Error("paperclip_runner_process_not_started");
|
||||
await input.onSpawn({
|
||||
pid: child.pid,
|
||||
processGroupId: process.platform === "win32" ? null : child.pid,
|
||||
startedAt: new Date().toISOString(),
|
||||
});
|
||||
const completed = await Promise.race([
|
||||
prepared.waitForTerminal(input.timeoutMs),
|
||||
exit.then(async ({ code, signal }) => {
|
||||
const recovered = await prepared.waitForTerminal(2_000).catch(() => null);
|
||||
if (recovered) return recovered;
|
||||
throw new Error(
|
||||
`paperclip_runner_process_exited: code=${code ?? "null"} signal=${signal ?? "null"}`,
|
||||
);
|
||||
}),
|
||||
]);
|
||||
prepared.queueCommand("session.close", {}, `close_${input.runId}`);
|
||||
prepared.queueCommand("runner.shutdown", {}, `shutdown_${input.runId}`);
|
||||
await stopChild(child, exit, true);
|
||||
|
||||
const succeeded = completed.terminal.runTerminalState === "succeeded";
|
||||
return {
|
||||
exitCode: succeeded ? 0 : 1,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
...(succeeded ? {} : {
|
||||
errorCode: "paperclip_runner_provider_failed",
|
||||
errorMessage: completed.result.summary,
|
||||
}),
|
||||
provider: "codex",
|
||||
model: input.model,
|
||||
sessionParams: {
|
||||
sessionId: completed.providerSessionId ?? input.normalizedSessionId,
|
||||
},
|
||||
sessionDisplayId: completed.providerSessionId ?? input.normalizedSessionId,
|
||||
resultJson: {
|
||||
nativeRunner: {
|
||||
result: completed.result,
|
||||
terminal: completed.terminal,
|
||||
},
|
||||
},
|
||||
summary: completed.result.summary,
|
||||
};
|
||||
} finally {
|
||||
await stopChild(child, exit).catch(() => undefined);
|
||||
await prepared.release();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { and, desc, eq, lt } from "drizzle-orm";
|
||||
import { and, desc, eq, inArray, lt } from "drizzle-orm";
|
||||
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import {
|
||||
@@ -26,6 +26,8 @@ export interface NativeRunStoreBinding {
|
||||
readonly runnerSourceInstanceId: string;
|
||||
readonly completionContractId: string;
|
||||
readonly completionContractSha256: string;
|
||||
readonly completionContractRevision: string;
|
||||
readonly completionContractCriterionIds: readonly string[];
|
||||
}
|
||||
|
||||
export interface CompleteNativeRunInput {
|
||||
@@ -52,21 +54,63 @@ function sha256(value: unknown): string {
|
||||
return `sha256:${createHash("sha256").update(canonicalJson(value)).digest("hex")}`;
|
||||
}
|
||||
|
||||
function assertTerminal(value: PrpTerminalState): void {
|
||||
function assertTerminal(value: unknown): asserts value is PrpTerminalState {
|
||||
const terminal = value as Partial<PrpTerminalState> | null;
|
||||
if (
|
||||
value.schema !== "paperclip.prp.terminal.v1" ||
|
||||
typeof value !== "object" ||
|
||||
terminal === null ||
|
||||
Array.isArray(value) ||
|
||||
terminal.schema !== "paperclip.prp.terminal.v1" ||
|
||||
typeof terminal.turnTerminalState !== "string" ||
|
||||
!["completed", "failed", "interrupted", "cancelled"].includes(
|
||||
value.turnTerminalState,
|
||||
terminal.turnTerminalState,
|
||||
) ||
|
||||
!["succeeded", "failed", "cancelled"].includes(value.runTerminalState) ||
|
||||
typeof terminal.runTerminalState !== "string" ||
|
||||
!["succeeded", "failed", "cancelled"].includes(terminal.runTerminalState) ||
|
||||
typeof terminal.reportedWorkDisposition !== "string" ||
|
||||
!["done", "blocked", "needs_review", "yielded"].includes(
|
||||
value.reportedWorkDisposition,
|
||||
terminal.reportedWorkDisposition,
|
||||
)
|
||||
) {
|
||||
throw new Error("native_terminal_schema_invalid");
|
||||
}
|
||||
}
|
||||
|
||||
function validateBoundCompletion(
|
||||
binding: NativeRunStoreBinding,
|
||||
result: unknown,
|
||||
terminal: unknown,
|
||||
): { result: PrpStructuredRunResult; terminal: PrpTerminalState } {
|
||||
const validated = validatePrpStructuredRunResult(result);
|
||||
if (!validated.ok) throw new Error("native_result_schema_invalid");
|
||||
assertTerminal(terminal);
|
||||
if (
|
||||
validated.result.completionClaim.contractRevision !==
|
||||
binding.completionContractRevision
|
||||
) {
|
||||
throw new Error("native_result_completion_contract_mismatch");
|
||||
}
|
||||
const reportedCriterionIds = validated.result.completionClaim.criteria.map(
|
||||
(criterion) => criterion.criterionId,
|
||||
);
|
||||
if (
|
||||
reportedCriterionIds.length !== binding.completionContractCriterionIds.length
|
||||
|| reportedCriterionIds.some(
|
||||
(criterionId, index) =>
|
||||
criterionId !== binding.completionContractCriterionIds[index],
|
||||
)
|
||||
) {
|
||||
throw new Error("native_result_completion_contract_mismatch");
|
||||
}
|
||||
if (
|
||||
validated.result.reportedWorkDisposition !==
|
||||
terminal.reportedWorkDisposition
|
||||
) {
|
||||
throw new Error("native_result_terminal_disposition_mismatch");
|
||||
}
|
||||
return { result: validated.result, terminal };
|
||||
}
|
||||
|
||||
/** Durable DB boundary used by the hidden PRP coordinator. */
|
||||
export class NativeRunCoordinatorStore {
|
||||
readonly #db: Db;
|
||||
@@ -77,6 +121,79 @@ export class NativeRunCoordinatorStore {
|
||||
this.#binding = structuredClone(binding);
|
||||
}
|
||||
|
||||
async readCompletedRun(): Promise<{
|
||||
readonly result: PrpStructuredRunResult;
|
||||
readonly terminal: PrpTerminalState;
|
||||
readonly turnId?: string;
|
||||
} | null> {
|
||||
const [row] = await this.#db
|
||||
.select({ resultJson: nativeRunResults.resultJson })
|
||||
.from(nativeRunResults)
|
||||
.where(
|
||||
and(
|
||||
eq(nativeRunResults.companyId, this.#binding.companyId),
|
||||
eq(nativeRunResults.issueId, this.#binding.issueId),
|
||||
eq(nativeRunResults.runId, this.#binding.runId),
|
||||
eq(nativeRunResults.schemaStatus, "accepted"),
|
||||
),
|
||||
)
|
||||
.limit(1);
|
||||
if (!row) return null;
|
||||
const envelope = row.resultJson as Record<string, unknown>;
|
||||
if (!("result" in envelope) || !("terminal" in envelope)) {
|
||||
throw new Error("native_result_envelope_invalid");
|
||||
}
|
||||
const validated = validateBoundCompletion(
|
||||
this.#binding,
|
||||
envelope.result,
|
||||
envelope.terminal,
|
||||
);
|
||||
if (envelope.turnId !== null && envelope.turnId !== undefined && typeof envelope.turnId !== "string") {
|
||||
throw new Error("native_result_envelope_invalid");
|
||||
}
|
||||
return {
|
||||
...validated,
|
||||
...(envelope.turnId ? { turnId: envelope.turnId } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
async readProviderSessionId(): Promise<string | null> {
|
||||
const rows = await this.#db
|
||||
.select({ payload: heartbeatRunEvents.payload })
|
||||
.from(heartbeatRunEvents)
|
||||
.where(and(
|
||||
eq(heartbeatRunEvents.runId, this.#binding.runId),
|
||||
eq(heartbeatRunEvents.sourceInstanceId, this.#binding.runnerSourceInstanceId),
|
||||
inArray(heartbeatRunEvents.eventType, [
|
||||
"session.started",
|
||||
"session.resumed",
|
||||
"session.reconciled",
|
||||
]),
|
||||
))
|
||||
.orderBy(desc(heartbeatRunEvents.sourceSeq))
|
||||
.limit(3);
|
||||
for (const row of rows) {
|
||||
const event = (row.payload as Record<string, unknown> | null)?.prpEvent;
|
||||
const payload = typeof event === "object" && event !== null && !Array.isArray(event)
|
||||
? (event as Record<string, unknown>).payload
|
||||
: null;
|
||||
const providerSessionId = typeof payload === "object"
|
||||
&& payload !== null
|
||||
&& !Array.isArray(payload)
|
||||
? (payload as Record<string, unknown>).providerSessionId
|
||||
: null;
|
||||
if (
|
||||
typeof providerSessionId === "string"
|
||||
&& providerSessionId.length > 0
|
||||
&& providerSessionId.length <= 240
|
||||
&& ![...providerSessionId].some((character) => /[\u0000-\u001f\u007f]/.test(character))
|
||||
) {
|
||||
return providerSessionId;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async appendEvent(value: PrpEvent): Promise<{
|
||||
readonly disposition: "committed" | "duplicate";
|
||||
readonly cursor: number;
|
||||
@@ -205,18 +322,10 @@ export class NativeRunCoordinatorStore {
|
||||
readonly disposition: "committed" | "duplicate";
|
||||
readonly resultId: string;
|
||||
}> {
|
||||
const validated = validatePrpStructuredRunResult(input.result);
|
||||
if (!validated.ok) throw new Error("native_result_schema_invalid");
|
||||
assertTerminal(input.terminal);
|
||||
if (
|
||||
validated.result.reportedWorkDisposition !==
|
||||
input.terminal.reportedWorkDisposition
|
||||
) {
|
||||
throw new Error("native_result_terminal_disposition_mismatch");
|
||||
}
|
||||
const validated = validateBoundCompletion(this.#binding, input.result, input.terminal);
|
||||
const canonical = {
|
||||
result: validated.result,
|
||||
terminal: input.terminal,
|
||||
terminal: validated.terminal,
|
||||
turnId: input.turnId ?? null,
|
||||
};
|
||||
const canonicalSha256 = sha256(canonical);
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import { and, eq } from "drizzle-orm";
|
||||
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { completionContracts, heartbeatRuns } from "@paperclipai/db";
|
||||
|
||||
import { ensureNativeCompletionContract } from "./completion-contracts.js";
|
||||
import { NATIVE_RUNTIME_RESOLVER_VERSION } from "./runtime-mode.js";
|
||||
|
||||
function record(value: unknown): Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value)
|
||||
? value as Record<string, unknown>
|
||||
: {};
|
||||
}
|
||||
|
||||
function contractBinding(
|
||||
contractJson: unknown,
|
||||
persistedRevision: number,
|
||||
): { revision: string; criterionIds: string[] } {
|
||||
const contract = record(contractJson);
|
||||
const revision = typeof contract.revision === "string" ? contract.revision : "";
|
||||
const criteria = Array.isArray(contract.criteria) ? contract.criteria : [];
|
||||
const criterionIds = criteria
|
||||
.map((criterion) => record(criterion).id)
|
||||
.filter((id): id is string => typeof id === "string" && id.length > 0);
|
||||
if (
|
||||
(revision && revision !== String(persistedRevision))
|
||||
|| criterionIds.length === 0
|
||||
|| criterionIds.length !== criteria.length
|
||||
) {
|
||||
throw new Error("native_completion_contract_binding_invalid");
|
||||
}
|
||||
return { revision: String(persistedRevision), criterionIds };
|
||||
}
|
||||
|
||||
export async function prepareNativeHeartbeatRun(input: {
|
||||
db: Db;
|
||||
run: {
|
||||
id: string;
|
||||
companyId: string;
|
||||
agentId: string;
|
||||
runtimeMode: string | null;
|
||||
runtimeModeResolvedAt: Date | null;
|
||||
runnerProfileJson: Record<string, unknown> | null;
|
||||
runnerInstanceId: string | null;
|
||||
nativeSessionId: string | null;
|
||||
nativeIssueId: string | null;
|
||||
completionContractId: string | null;
|
||||
completionContractSha256: string | null;
|
||||
};
|
||||
issue: {
|
||||
id: string;
|
||||
title: string;
|
||||
description: string | null;
|
||||
reviewPolicy?: string | null;
|
||||
};
|
||||
environmentLeaseId: string;
|
||||
}) {
|
||||
if (input.run.runtimeModeResolvedAt && input.run.runtimeMode !== "native") {
|
||||
throw new Error("native_runtime_mode_conflict");
|
||||
}
|
||||
const persistedProfile = record(input.run.runnerProfileJson);
|
||||
const runnerInstanceId = input.run.runnerInstanceId ?? randomUUID();
|
||||
const normalizedSessionId = input.run.nativeSessionId ?? randomUUID();
|
||||
const turnId = typeof persistedProfile.turnId === "string"
|
||||
? persistedProfile.turnId
|
||||
: randomUUID();
|
||||
const itemId = typeof persistedProfile.itemId === "string"
|
||||
? persistedProfile.itemId
|
||||
: randomUUID();
|
||||
const environmentLeaseId = typeof persistedProfile.environmentLeaseId === "string"
|
||||
? persistedProfile.environmentLeaseId
|
||||
: input.environmentLeaseId;
|
||||
|
||||
const persistedContract = input.run.completionContractId
|
||||
? await input.db
|
||||
.select()
|
||||
.from(completionContracts)
|
||||
.where(and(
|
||||
eq(completionContracts.id, input.run.completionContractId),
|
||||
eq(completionContracts.companyId, input.run.companyId),
|
||||
eq(completionContracts.issueId, input.issue.id),
|
||||
))
|
||||
.limit(1)
|
||||
.then((rows) => rows[0] ?? null)
|
||||
: null;
|
||||
const completion = persistedContract
|
||||
? { row: persistedContract, contract: persistedContract.contractJson }
|
||||
: await ensureNativeCompletionContract({
|
||||
db: input.db,
|
||||
companyId: input.run.companyId,
|
||||
issue: input.issue,
|
||||
actorId: input.run.agentId,
|
||||
});
|
||||
const binding = contractBinding(completion.contract, completion.row.revision);
|
||||
|
||||
await input.db.transaction(async (tx) => {
|
||||
const [locked] = await tx
|
||||
.select()
|
||||
.from(heartbeatRuns)
|
||||
.where(eq(heartbeatRuns.id, input.run.id))
|
||||
.for("update")
|
||||
.limit(1);
|
||||
if (!locked) throw new Error("native_runtime_run_missing");
|
||||
if (locked.runtimeModeResolvedAt && locked.runtimeMode !== "native") {
|
||||
throw new Error("native_runtime_mode_conflict");
|
||||
}
|
||||
if (
|
||||
locked.runtimeModeResolvedAt
|
||||
&& (
|
||||
locked.runnerInstanceId !== runnerInstanceId
|
||||
|| locked.nativeSessionId !== normalizedSessionId
|
||||
|| locked.nativeIssueId !== input.issue.id
|
||||
|| locked.completionContractId !== completion.row.id
|
||||
)
|
||||
) {
|
||||
throw new Error("native_runtime_binding_conflict");
|
||||
}
|
||||
await tx
|
||||
.update(heartbeatRuns)
|
||||
.set({
|
||||
runtimeMode: "native",
|
||||
runtimeModeResolverVersion:
|
||||
locked.runtimeModeResolverVersion ?? NATIVE_RUNTIME_RESOLVER_VERSION,
|
||||
runtimeModeReason: locked.runtimeModeReason ?? "explicit_paperclip_runner",
|
||||
runtimeModeResolvedAt: locked.runtimeModeResolvedAt ?? new Date(),
|
||||
runnerProfileJson: {
|
||||
schema: "paperclip.runner.profile.v1",
|
||||
provider: "codex",
|
||||
turnId,
|
||||
itemId,
|
||||
environmentLeaseId,
|
||||
},
|
||||
runnerInstanceId,
|
||||
nativeSessionId: normalizedSessionId,
|
||||
nativeIssueId: input.issue.id,
|
||||
driverKind: "codex",
|
||||
driverVersion: "codex-app-server-v1",
|
||||
completionContractId: completion.row.id,
|
||||
completionContractSha256: completion.row.canonicalSha256,
|
||||
nativePhase: "provider_running",
|
||||
nativePhaseUpdatedAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(heartbeatRuns.id, input.run.id));
|
||||
});
|
||||
|
||||
return {
|
||||
runnerInstanceId,
|
||||
normalizedSessionId,
|
||||
turnId,
|
||||
itemId,
|
||||
environmentLeaseId,
|
||||
completionContract: binding,
|
||||
};
|
||||
}
|
||||
@@ -63,7 +63,7 @@ const result: PrpStructuredRunResult = {
|
||||
reportedWorkDisposition: "done",
|
||||
summary: "The hidden runner completed the bounded task.",
|
||||
completionClaim: {
|
||||
contractRevision: "contract-v1",
|
||||
contractRevision: "1",
|
||||
objectiveSatisfied: true,
|
||||
criteria: [],
|
||||
remainingWork: [],
|
||||
@@ -226,6 +226,8 @@ describeEmbeddedPostgres("hidden runner PRP coordinator", () => {
|
||||
runnerSourceInstanceId: seed.runnerInstanceId,
|
||||
completionContractId: seed.completionContractId,
|
||||
completionContractSha256: seed.completionContractSha256,
|
||||
completionContractRevision: "1",
|
||||
completionContractCriterionIds: [],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -339,6 +341,27 @@ describeEmbeddedPostgres("hidden runner PRP coordinator", () => {
|
||||
it("persists events and results idempotently and leases finalization", async () => {
|
||||
const seed = await seedNativeRun();
|
||||
const nativeStore = store(seed);
|
||||
await expect(nativeStore.completeRun({
|
||||
result: {
|
||||
...result,
|
||||
completionClaim: { ...result.completionClaim, contractRevision: "2" },
|
||||
},
|
||||
terminal,
|
||||
})).rejects.toThrow("native_result_completion_contract_mismatch");
|
||||
await expect(nativeStore.completeRun({
|
||||
result: {
|
||||
...result,
|
||||
completionClaim: {
|
||||
...result.completionClaim,
|
||||
criteria: [{
|
||||
criterionId: "not-bound",
|
||||
status: "satisfied",
|
||||
evidenceRefs: [],
|
||||
}],
|
||||
},
|
||||
},
|
||||
terminal,
|
||||
})).rejects.toThrow("native_result_completion_contract_mismatch");
|
||||
const event = runnerEvent(seed);
|
||||
await expect(nativeStore.appendEvent(event)).resolves.toMatchObject({
|
||||
disposition: "committed",
|
||||
|
||||
@@ -3,7 +3,7 @@ import { resolve } from "node:path";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { agents, heartbeatRuns, issues } from "@paperclipai/db";
|
||||
import { agents, completionContracts, heartbeatRuns, issues } from "@paperclipai/db";
|
||||
import {
|
||||
DurablePrpControlPlane,
|
||||
type PaperclipSemanticToolDefinition,
|
||||
@@ -56,6 +56,12 @@ export interface PreparedRunnerPrpSession {
|
||||
readonly disposition: "committed" | "duplicate";
|
||||
readonly resultId: string;
|
||||
}>;
|
||||
waitForTerminal(timeoutMs?: number): Promise<{
|
||||
readonly result: PrpStructuredRunResult;
|
||||
readonly terminal: PrpTerminalState;
|
||||
readonly turnId?: string;
|
||||
readonly providerSessionId?: string;
|
||||
}>;
|
||||
release(): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -95,6 +101,20 @@ function validateInput(input: PrepareRunnerPrpSessionInput): void {
|
||||
}
|
||||
}
|
||||
|
||||
function completionCriterionIds(value: unknown): string[] | null {
|
||||
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
||||
const criteria = (value as Record<string, unknown>).criteria;
|
||||
if (!Array.isArray(criteria)) return null;
|
||||
const ids = criteria.map((criterion) => {
|
||||
if (typeof criterion !== "object" || criterion === null || Array.isArray(criterion)) return null;
|
||||
const id = (criterion as Record<string, unknown>).id;
|
||||
return typeof id === "string" && id.length > 0 ? id : null;
|
||||
});
|
||||
if (ids.some((id) => id === null)) return null;
|
||||
const typedIds = ids as string[];
|
||||
return new Set(typedIds).size === typedIds.length ? typedIds : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the hidden, run-bound PRP authority. This module does not select a
|
||||
* runtime or start runnerd. The flagged adapter owns those actions later.
|
||||
@@ -126,7 +146,12 @@ export function runnerPrpCoordinator(
|
||||
);
|
||||
|
||||
const [binding] = await db
|
||||
.select({ run: heartbeatRuns, issue: issues, agent: agents })
|
||||
.select({
|
||||
run: heartbeatRuns,
|
||||
issue: issues,
|
||||
agent: agents,
|
||||
completionContract: completionContracts,
|
||||
})
|
||||
.from(heartbeatRuns)
|
||||
.innerJoin(
|
||||
issues,
|
||||
@@ -142,6 +167,14 @@ export function runnerPrpCoordinator(
|
||||
eq(agents.companyId, heartbeatRuns.companyId),
|
||||
),
|
||||
)
|
||||
.innerJoin(
|
||||
completionContracts,
|
||||
and(
|
||||
eq(completionContracts.id, heartbeatRuns.completionContractId),
|
||||
eq(completionContracts.companyId, heartbeatRuns.companyId),
|
||||
eq(completionContracts.issueId, heartbeatRuns.nativeIssueId),
|
||||
),
|
||||
)
|
||||
.where(
|
||||
and(
|
||||
eq(heartbeatRuns.id, input.runId),
|
||||
@@ -160,6 +193,7 @@ export function runnerPrpCoordinator(
|
||||
binding.run.driverKind !== "codex" ||
|
||||
!binding.run.completionContractId ||
|
||||
!binding.run.completionContractSha256 ||
|
||||
binding.completionContract.canonicalSha256 !== binding.run.completionContractSha256 ||
|
||||
binding.issue.assigneeAgentId !== input.agentId ||
|
||||
binding.issue.executionRunId !== input.runId ||
|
||||
["paused", "terminated", "pending_approval", "error"].includes(
|
||||
@@ -168,6 +202,10 @@ export function runnerPrpCoordinator(
|
||||
) {
|
||||
throw new Error("runner_prp_run_not_authorized");
|
||||
}
|
||||
const criterionIds = completionCriterionIds(
|
||||
binding.completionContract.contractJson,
|
||||
);
|
||||
if (!criterionIds) throw new Error("runner_prp_run_not_authorized");
|
||||
|
||||
const semanticAuthority = new PaperclipRunnerSemanticAuthority(db, {
|
||||
companyId: input.companyId,
|
||||
@@ -185,6 +223,22 @@ export function runnerPrpCoordinator(
|
||||
runnerSourceInstanceId: input.runnerInstanceId,
|
||||
completionContractId: binding.run.completionContractId,
|
||||
completionContractSha256: binding.run.completionContractSha256,
|
||||
completionContractRevision: String(binding.completionContract.revision),
|
||||
completionContractCriterionIds: criterionIds,
|
||||
});
|
||||
type StoredCompletedRun = NonNullable<Awaited<ReturnType<typeof nativeStore.readCompletedRun>>>;
|
||||
type CompletedRun = StoredCompletedRun & { readonly providerSessionId?: string };
|
||||
const withProviderSession = async (stored: StoredCompletedRun): Promise<CompletedRun> => {
|
||||
const providerSessionId = await nativeStore.readProviderSessionId();
|
||||
return {
|
||||
...stored,
|
||||
...(providerSessionId ? { providerSessionId } : {}),
|
||||
};
|
||||
};
|
||||
let completedRun: CompletedRun | null = null;
|
||||
let resolveTerminal!: (value: CompletedRun) => void;
|
||||
const terminalEvent = new Promise<CompletedRun>((resolveTerminalPromise) => {
|
||||
resolveTerminal = resolveTerminalPromise;
|
||||
});
|
||||
const authority = new DurablePrpControlPlane({
|
||||
stateDirectory: resolve(stateRoot, input.runId),
|
||||
@@ -202,6 +256,12 @@ export function runnerPrpCoordinator(
|
||||
onCommittedEvent: async (event) => {
|
||||
await nativeStore.appendEvent(event);
|
||||
await nativeStore.reconcileTerminalEvent(event);
|
||||
if (event.eventType === "run.terminal") {
|
||||
const stored = await nativeStore.readCompletedRun();
|
||||
if (!stored) throw new Error("native_terminal_result_missing");
|
||||
completedRun = await withProviderSession(stored);
|
||||
resolveTerminal(completedRun);
|
||||
}
|
||||
},
|
||||
onSemanticToolInput: async (call) => {
|
||||
const result = await semanticAuthority.dispatch({
|
||||
@@ -249,6 +309,33 @@ export function runnerPrpCoordinator(
|
||||
if (released) throw new Error("runner_prp_session_released");
|
||||
return nativeStore.completeRun(completeInput);
|
||||
},
|
||||
waitForTerminal: async (timeoutMs = 60 * 60 * 1_000) => {
|
||||
if (released) throw new Error("runner_prp_session_released");
|
||||
if (!Number.isInteger(timeoutMs) || timeoutMs < 1_000 || timeoutMs > 24 * 60 * 60 * 1_000) {
|
||||
throw new Error("runner_prp_terminal_timeout_invalid");
|
||||
}
|
||||
if (completedRun) return completedRun;
|
||||
const stored = await nativeStore.readCompletedRun();
|
||||
if (stored) {
|
||||
completedRun = await withProviderSession(stored);
|
||||
return completedRun;
|
||||
}
|
||||
let timer: NodeJS.Timeout | null = null;
|
||||
try {
|
||||
return await Promise.race([
|
||||
terminalEvent,
|
||||
new Promise<never>((_resolve, reject) => {
|
||||
timer = setTimeout(
|
||||
() => reject(new Error("runner_prp_terminal_timeout")),
|
||||
timeoutMs,
|
||||
);
|
||||
timer.unref();
|
||||
}),
|
||||
]);
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
}
|
||||
},
|
||||
release: async () => {
|
||||
if (released) return;
|
||||
released = true;
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { BUILTIN_ADAPTER_TYPES } from "../../adapters/builtin-adapter-types.js";
|
||||
import {
|
||||
NativeRunnerSelectionError,
|
||||
resolveHeartbeatRuntimeMode,
|
||||
} from "./runtime-mode.js";
|
||||
|
||||
const base = {
|
||||
persisted: { runtimeMode: "legacy", runtimeModeResolvedAt: null },
|
||||
enabled: true,
|
||||
adapterConfig: { provider: "codex" },
|
||||
agentStatus: "running",
|
||||
issue: { workMode: "standard" },
|
||||
executionTarget: { kind: "local" },
|
||||
} as const;
|
||||
|
||||
describe("resolveHeartbeatRuntimeMode", () => {
|
||||
it("keeps every direct built-in adapter on the legacy path", () => {
|
||||
for (const adapterType of BUILTIN_ADAPTER_TYPES) {
|
||||
if (adapterType === "paperclip_runner") continue;
|
||||
expect(resolveHeartbeatRuntimeMode({ ...base, adapterType })).toEqual({
|
||||
kind: "legacy",
|
||||
resolverVersion: "paperclip-runner-v1",
|
||||
reason: "direct_adapter",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("fails closed for fresh runner starts while the flag is off", () => {
|
||||
expect(() => resolveHeartbeatRuntimeMode({
|
||||
...base,
|
||||
enabled: false,
|
||||
adapterType: "paperclip_runner",
|
||||
})).toThrowError(expect.objectContaining({
|
||||
code: "paperclip_runner_rollout_disabled",
|
||||
}) as NativeRunnerSelectionError);
|
||||
});
|
||||
|
||||
it("selects only Codex on a local target", () => {
|
||||
expect(resolveHeartbeatRuntimeMode({
|
||||
...base,
|
||||
adapterType: "paperclip_runner",
|
||||
})).toMatchObject({ kind: "native", provider: "codex" });
|
||||
expect(() => resolveHeartbeatRuntimeMode({
|
||||
...base,
|
||||
adapterType: "paperclip_runner",
|
||||
adapterConfig: { provider: "opencode" },
|
||||
})).toThrow(/only the Codex provider/);
|
||||
expect(() => resolveHeartbeatRuntimeMode({
|
||||
...base,
|
||||
adapterType: "paperclip_runner",
|
||||
executionTarget: { kind: "remote" },
|
||||
})).toThrow(/local execution environment/);
|
||||
});
|
||||
|
||||
it("recovers a persisted native run after the flag changes", () => {
|
||||
expect(resolveHeartbeatRuntimeMode({
|
||||
...base,
|
||||
enabled: false,
|
||||
adapterType: "paperclip_runner",
|
||||
persisted: { runtimeMode: "native", runtimeModeResolvedAt: new Date() },
|
||||
})).toEqual({
|
||||
kind: "native",
|
||||
resolverVersion: "paperclip-runner-v1",
|
||||
reason: "persisted_native_selection",
|
||||
provider: "codex",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,102 @@
|
||||
export const NATIVE_RUNTIME_RESOLVER_VERSION = "paperclip-runner-v1" as const;
|
||||
|
||||
export type HeartbeatRuntimeResolution =
|
||||
| {
|
||||
kind: "legacy";
|
||||
resolverVersion: typeof NATIVE_RUNTIME_RESOLVER_VERSION;
|
||||
reason: "direct_adapter" | "persisted_legacy_selection";
|
||||
}
|
||||
| {
|
||||
kind: "native";
|
||||
resolverVersion: typeof NATIVE_RUNTIME_RESOLVER_VERSION;
|
||||
reason: "explicit_paperclip_runner" | "persisted_native_selection";
|
||||
provider: "codex";
|
||||
};
|
||||
|
||||
export class NativeRunnerSelectionError extends Error {
|
||||
constructor(readonly code: string, message: string) {
|
||||
super(message);
|
||||
this.name = "NativeRunnerSelectionError";
|
||||
}
|
||||
}
|
||||
|
||||
function record(value: unknown): Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value)
|
||||
? value as Record<string, unknown>
|
||||
: {};
|
||||
}
|
||||
|
||||
/** Resolve a run once. Persisted selections do not consult a later flag change. */
|
||||
export function resolveHeartbeatRuntimeMode(input: {
|
||||
persisted: {
|
||||
runtimeMode: string | null;
|
||||
runtimeModeResolvedAt: Date | null;
|
||||
};
|
||||
enabled: boolean;
|
||||
adapterType: string | null;
|
||||
adapterConfig: unknown;
|
||||
agentStatus: string;
|
||||
issue: { workMode: string } | null;
|
||||
executionTarget: { kind?: string } | null | undefined;
|
||||
}): HeartbeatRuntimeResolution {
|
||||
if (input.persisted.runtimeModeResolvedAt) {
|
||||
if (input.persisted.runtimeMode === "native") {
|
||||
return {
|
||||
kind: "native",
|
||||
resolverVersion: NATIVE_RUNTIME_RESOLVER_VERSION,
|
||||
reason: "persisted_native_selection",
|
||||
provider: "codex",
|
||||
};
|
||||
}
|
||||
return {
|
||||
kind: "legacy",
|
||||
resolverVersion: NATIVE_RUNTIME_RESOLVER_VERSION,
|
||||
reason: "persisted_legacy_selection",
|
||||
};
|
||||
}
|
||||
|
||||
if (input.adapterType !== "paperclip_runner") {
|
||||
return {
|
||||
kind: "legacy",
|
||||
resolverVersion: NATIVE_RUNTIME_RESOLVER_VERSION,
|
||||
reason: "direct_adapter",
|
||||
};
|
||||
}
|
||||
if (!input.enabled) {
|
||||
throw new NativeRunnerSelectionError(
|
||||
"paperclip_runner_rollout_disabled",
|
||||
"Paperclip Runner is experimental and disabled on this instance.",
|
||||
);
|
||||
}
|
||||
const provider = record(input.adapterConfig).provider ?? "codex";
|
||||
if (provider !== "codex") {
|
||||
throw new NativeRunnerSelectionError(
|
||||
"paperclip_runner_provider_unsupported",
|
||||
"Paperclip Runner currently supports only the Codex provider.",
|
||||
);
|
||||
}
|
||||
if (!input.issue || !["standard", "planning", "ask"].includes(input.issue.workMode)) {
|
||||
throw new NativeRunnerSelectionError(
|
||||
"paperclip_runner_issue_ineligible",
|
||||
"Paperclip Runner requires a standard, planning, or ask task.",
|
||||
);
|
||||
}
|
||||
if (!input.executionTarget || input.executionTarget.kind !== "local") {
|
||||
throw new NativeRunnerSelectionError(
|
||||
"paperclip_runner_environment_unsupported",
|
||||
"Paperclip Runner currently requires a local execution environment.",
|
||||
);
|
||||
}
|
||||
if (!["active", "running"].includes(input.agentStatus)) {
|
||||
throw new NativeRunnerSelectionError(
|
||||
"paperclip_runner_agent_ineligible",
|
||||
"Paperclip Runner requires an active agent.",
|
||||
);
|
||||
}
|
||||
return {
|
||||
kind: "native",
|
||||
resolverVersion: NATIVE_RUNTIME_RESOLVER_VERSION,
|
||||
reason: "explicit_paperclip_runner",
|
||||
provider: "codex",
|
||||
};
|
||||
}
|
||||
+36
-4
@@ -1,8 +1,40 @@
|
||||
/**
|
||||
* Development shim for the package-local runner runtime.
|
||||
*
|
||||
* The server build replaces this emitted module with the runner package's
|
||||
* compiled `dist` tree so published server packages have no workspace runtime
|
||||
* dependency. Keep server imports pointed at this relative boundary.
|
||||
* Source-mode server entry points do not build workspace dependencies first,
|
||||
* so this shim loads the package source through the TypeScript runtime. The
|
||||
* server build replaces the emitted shim with the package's compiled `dist`
|
||||
* tree so published server packages have no workspace runtime dependency.
|
||||
* Keep server imports pointed at this relative boundary.
|
||||
*/
|
||||
export * from "@paperclipai/paperclip-runner";
|
||||
type RunnerModule = typeof import("@paperclipai/paperclip-runner");
|
||||
|
||||
export type {
|
||||
PaperclipJsonValue,
|
||||
PaperclipSemanticActionBinding,
|
||||
PaperclipSemanticActionId,
|
||||
PaperclipSemanticAuthorizationRecord,
|
||||
PaperclipSemanticRunContext,
|
||||
PaperclipSemanticToolCall,
|
||||
PaperclipSemanticToolDefinition,
|
||||
PaperclipSemanticToolResult,
|
||||
PrpEvent,
|
||||
PrpStructuredRunResult,
|
||||
PrpTerminalState,
|
||||
} from "@paperclipai/paperclip-runner";
|
||||
export type DurablePrpControlPlane =
|
||||
import("@paperclipai/paperclip-runner").DurablePrpControlPlane;
|
||||
export type PaperclipSemanticDispatcher =
|
||||
import("@paperclipai/paperclip-runner").PaperclipSemanticDispatcher;
|
||||
|
||||
const sourceUrl = new URL(
|
||||
"../../../../packages/paperclip-runner/src/index.ts",
|
||||
import.meta.url,
|
||||
);
|
||||
const runner = await import(sourceUrl.href) as RunnerModule;
|
||||
|
||||
export const DurablePrpControlPlane = runner.DurablePrpControlPlane;
|
||||
export const PaperclipSemanticDispatcher = runner.PaperclipSemanticDispatcher;
|
||||
export const validatePrpEvent = runner.validatePrpEvent;
|
||||
export const validatePrpStructuredRunResult =
|
||||
runner.validatePrpStructuredRunResult;
|
||||
Reference in new issue
Block a user