mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 09:39:53 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Native runs save an immutable execution context for restart recovery. > - The context includes the prompt text, revision, and content hashes. > - The parser required that saved prompt to match the current release. > - A server upgrade could reject a valid saved run before provider recovery. > - This pull request validates and preserves the saved prompt snapshot. > - Routine prompt changes no longer need a catalog of past strings. ## Linked Issues or Issue Description **What happened?** A hot restart selected a dead native runner for same-run recovery. Reading its saved v5 execution input failed with `input.runtimeContext.prompt must match the fixed Paperclip prompt revision`. The new controller accepted only v6. **Expected behavior** Recovery uses the saved prompt and validates its content hashes. It preserves the same run and provider session without starting a duplicate turn. **Steps to reproduce** 1. Start a native run and save its execution input and provider checkpoint. 2. Change the fixed execution prompt in the server release. 3. Stop the runner and recover the saved run with the new controller. 4. Observe that the old parser rejects the saved prompt before provider recovery. **Paperclip version or commit** The v5-to-v6 prompt change was introduced in #15446. The defect also reproduces on current master before this fix. **Deployment mode** Source-built server with the native runner. Related work: #15446 added task-monitor guidance. The held prompt-size experiment in #15489 changes prompt wording but does not add recovery compatibility. ## What Changed - Read the prompt text and revision from the saved execution snapshot. - Treat the revision as non-empty metadata and preserve the exact saved bytes. - Validate the prompt SHA-256 and the aggregate context digest. - Keep fresh-run builders on the current prompt constants. - Test arbitrary saved prompts, malformed fields, altered text, stale hashes, and aggregate drift. - Test recovery parsing for Codex input versions v3-v5 and OpenCode, ACPX Pi, and Dot v6 inputs. - Extend the real-process restart suite with both the incident's v5 wire fixture and a prompt unknown to this release. - Run the restart recovery suite in the existing Rust-equipped PR lane, where its runner and fake-provider binaries are built. Verify complete, non-overlapping test coverage for PR, release, and local callers. - Document recovery from saved snapshots without a historical prompt catalog. ## Verification - Red: the new contract regressions fail against the catalog-based parser with the original prompt-validation error. - Green: 53 focused contract and materialization tests pass. - Red: the real-process unknown-prompt regression fails with master's original parser at the saved-input recovery read after process loss. - Green: all 15 real-process restart tests pass locally on the final branch. The saved-prompt cases keep the run and provider session, replace the PID, and record one `turn/start`. - Local repository `pnpm -r typecheck` and `pnpm build` passed after rebase on `89f09dad723766e5351953f0731b9aa5daada28d`. The 53 focused tests also passed on that head. - Red: the new test-roster checks fail against the old CI placement. - Green: all 26 test-scheduling checks pass after moving the restart suite. - CI ran all 15 restart recovery tests with no skips on final head `6719fc2bb7a31a0f72ea04c7e525a63dcc6f9105`. [Runner test job](https://github.com/paperclipai/paperclip/actions/runs/37764782334/job/113271464966). - Greptile scored 5/5 on that exact head with no actionable findings. - The complete CI matrix passed on final head `6719fc2bb7a31a0f72ea04c7e525a63dcc6f9105`: general/workspace tests, serialized server suites, both runner Vitest lanes, Rust and static checks, all browser shards, typecheck, build, and the canary dry run. [CI run](https://github.com/paperclipai/paperclip/actions/runs/37764782334). - There are no unresolved review threads or merge conflicts. - Reproduce focused tests with `pnpm --filter @paperclipai/paperclip-runner exec vitest run src/contracts/runtime-context.test.ts src/contracts/native-execution.test.ts src/drivers/runtime-context-materializer.test.ts`. - Reproduce restart tests with `pnpm --filter @paperclipai/paperclip-runner build:rust` followed by `pnpm exec vitest run server/src/services/native-runtime/native-runner-restart-recovery.integration.test.ts`. They use temporary PostgreSQL, real runner processes, and a fake Codex provider. They do not use paid inference. ## Risks - The parser now accepts internally consistent saved prompt text that is absent from the current source. Inputs must come from trusted server persistence. Content hashes verify consistency; they do not authenticate authorship. - Existing execution-schema, ownership, checkpoint, provider, permission, and session-compatibility checks still apply. - This change validates the saved base prompt. It does not make all additional code-generated instruction strings versioned. - The process-level recovery proof uses Codex. Other provider coverage verifies the shared input parser and retained provider configuration. ## Model Used OpenAI Codex, GPT-6 family, with repository inspection, code editing, and test tools. The exact serving model ID and context-window size are not exposed in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
666 lines
30 KiB
TypeScript
666 lines
30 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { buildNativeModelEnvelope, parseNativeExecutionInput, NATIVE_EXECUTION_INPUT_SCHEMA, NATIVE_EXECUTION_INPUT_SCHEMA_V6, type NativeExecutionInputV1 } from "./native-execution.js";
|
|
import {
|
|
NATIVE_RUNTIME_ASSET_SCHEMA,
|
|
PAPERCLIP_EXECUTION_PROMPT,
|
|
PAPERCLIP_EXECUTION_PROMPT_REVISION,
|
|
canonicalNativeRuntimeContextDigest,
|
|
composeNativeSystemInstructions,
|
|
nativeRuntimePromptDigest,
|
|
} from "./runtime-context.js";
|
|
|
|
const input: NativeExecutionInputV1 = {
|
|
schema: "paperclip.native-execution-input.v1",
|
|
binding: {
|
|
companyId: "company-1",
|
|
runId: "run-1",
|
|
issueId: "issue-1",
|
|
agentId: "agent-1",
|
|
executionWorkspaceId: "workspace-1",
|
|
},
|
|
task: {
|
|
identifier: "PAP-1",
|
|
title: "Safe task",
|
|
description: null,
|
|
prompt: "# PAP-1: Safe task\n\nPlease address the latest comment.",
|
|
workMode: "standard",
|
|
},
|
|
workspace: { cwd: "/safe/workspace", repoUrl: null, repoRef: null, branchName: null },
|
|
session: {
|
|
normalizedSessionId: null,
|
|
driverKind: "codex_app_server",
|
|
protocolVersion: 1,
|
|
lifecyclePolicy: { mode: "per_turn", idleTimeoutMs: null },
|
|
},
|
|
provider: { kind: "codex", model: null },
|
|
completionContract: {
|
|
id: "contract-1",
|
|
sha256: "abc123",
|
|
schemaVersion: "paperclip.completion-contract.v1",
|
|
contract: {
|
|
revision: "1",
|
|
objective: "Complete the safe task.",
|
|
criteria: [{ id: "objective", requirement: "The task is complete." }],
|
|
},
|
|
},
|
|
interactionResponses: [],
|
|
credentialBindings: [{
|
|
bindingId: "opaque-binding",
|
|
service: "github",
|
|
destination: "github.com",
|
|
expiresAt: null,
|
|
displayName: "GitHub",
|
|
}],
|
|
};
|
|
|
|
describe("NativeExecutionInputV1", () => {
|
|
it.each(["paperclip.native-execution-input.v3", "paperclip.native-execution-input.v4", NATIVE_EXECUTION_INPUT_SCHEMA])(
|
|
"preserves a saved %s execution through recovery parsing",
|
|
(schema) => {
|
|
const digest = "0".repeat(64);
|
|
const text = "Saved system instructions absent from the current release.";
|
|
const context = {
|
|
prompt: { revision: "saved-prompt-before-upgrade", text, digest: createHash("sha256").update(text).digest("hex") },
|
|
instructions: {
|
|
entryPath: "AGENTS.md",
|
|
bundle: { schema: NATIVE_RUNTIME_ASSET_SCHEMA, digest, manifestDigest: digest, rootPath: "/runtime/instructions", fileCount: 1, totalBytes: 42 },
|
|
},
|
|
skills: [],
|
|
mcp: { assignmentSetId: "none", digest, bindingId: null },
|
|
};
|
|
const persisted = JSON.parse(JSON.stringify({
|
|
...input,
|
|
schema,
|
|
provider: schema === "paperclip.native-execution-input.v3" ? input.provider : { ...input.provider, approvalPolicy: "never" },
|
|
executionMode: "default",
|
|
planningContext: null,
|
|
runtimeContext: { ...context, aggregateDigest: canonicalNativeRuntimeContextDigest(context) },
|
|
}));
|
|
const recovered = parseNativeExecutionInput(persisted);
|
|
expect(recovered.runtimeContext).toEqual(persisted.runtimeContext);
|
|
expect(parseNativeExecutionInput(recovered)).toEqual(recovered);
|
|
},
|
|
);
|
|
|
|
it("parses v3 immutable runtime context without changing the model task envelope", () => {
|
|
const digest = "0".repeat(64);
|
|
const context = {
|
|
prompt: { revision: PAPERCLIP_EXECUTION_PROMPT_REVISION, text: PAPERCLIP_EXECUTION_PROMPT, digest: nativeRuntimePromptDigest() },
|
|
instructions: {
|
|
entryPath: "AGENTS.md",
|
|
bundle: { schema: NATIVE_RUNTIME_ASSET_SCHEMA, digest, manifestDigest: digest, rootPath: "/runtime/instructions", fileCount: 2, totalBytes: 42 },
|
|
},
|
|
skills: [{
|
|
key: "company/research",
|
|
runtimeName: "research",
|
|
versionId: "version-1",
|
|
bundle: { schema: NATIVE_RUNTIME_ASSET_SCHEMA, digest, manifestDigest: digest, rootPath: "/runtime/skills/research", fileCount: 2, totalBytes: 42 },
|
|
}],
|
|
mcp: { assignmentSetId: "sha256:test", digest, bindingId: "native-mcp:run-1" },
|
|
} as const;
|
|
const parsed = parseNativeExecutionInput({
|
|
...input,
|
|
schema: "paperclip.native-execution-input.v3",
|
|
executionMode: "default",
|
|
planningContext: null,
|
|
runtimeContext: { ...context, aggregateDigest: canonicalNativeRuntimeContextDigest(context) },
|
|
});
|
|
expect(parsed.schema).toBe("paperclip.native-execution-input.v3");
|
|
const envelope = buildNativeModelEnvelope(parsed);
|
|
expect(envelope.task).toEqual(buildNativeModelEnvelope(input).task);
|
|
expect(envelope.completionContract).toEqual(buildNativeModelEnvelope(input).completionContract);
|
|
expect(JSON.stringify(envelope)).not.toContain("runtimeContext");
|
|
expect(JSON.stringify(envelope)).not.toContain(PAPERCLIP_EXECUTION_PROMPT);
|
|
expect(composeNativeSystemInstructions(parsed.runtimeContext, "Follow sibling.md")).toBe(
|
|
`${PAPERCLIP_EXECUTION_PROMPT}\n\nFollow sibling.md\n\nRead-only instruction sibling root: /runtime/instructions`,
|
|
);
|
|
expect(canonicalNativeRuntimeContextDigest({
|
|
...context,
|
|
mcp: { ...context.mcp, bindingId: "native-mcp:run-2" },
|
|
})).toBe(parsed.runtimeContext.aggregateDigest);
|
|
const current = parseNativeExecutionInput({
|
|
...parsed,
|
|
schema: "paperclip.native-execution-input.v4",
|
|
provider: { kind: "codex", model: null, approvalPolicy: "on-request" },
|
|
});
|
|
const withDelta = parseNativeExecutionInput({ ...current, continuationPrompt: '{"messages":[{"authorType":"user","body":"Just this new comment"}]}' });
|
|
// No checkpoint / failed provider recovery must retain full bootstrap input.
|
|
expect(buildNativeModelEnvelope(withDelta)).toEqual(buildNativeModelEnvelope(current));
|
|
const delta = buildNativeModelEnvelope(withDelta, { resumedSession: true });
|
|
expect(delta).toEqual({
|
|
schema: "paperclip.native-continuation.v1",
|
|
events: '{"messages":[{"authorType":"user","body":"Just this new comment"}]}',
|
|
completion: { revision: "1", criterionIds: ["objective"],
|
|
instruction: "Before ending this turn, obtain one accepted paperclip_finish or paperclip_block result. Earlier reports belong to earlier turns; a final message alone does not complete this turn." },
|
|
});
|
|
expect(JSON.stringify(delta)).not.toContain(input.task.title);
|
|
expect(JSON.stringify(delta)).not.toContain(input.completionContract.contract.objective);
|
|
expect(JSON.stringify(delta)).not.toContain("opaque-binding");
|
|
const withGuidance = parseNativeExecutionInput({ ...current, initialCommunicationGuidance: "Saved Slack instructions" });
|
|
expect(buildNativeModelEnvelope(withGuidance).task.prompt).toBe(`Saved Slack instructions\n\n${current.task.prompt}`);
|
|
// Structured question/approval resumes may use a full envelope rather than
|
|
// a delta. They still must not repeat initial communication instructions.
|
|
expect(buildNativeModelEnvelope(withGuidance, { resumedSession: true })).toEqual(buildNativeModelEnvelope(current));
|
|
expect(buildNativeModelEnvelope(parseNativeExecutionInput({ ...withGuidance, continuationPrompt: "new message" }), { resumedSession: true })).not.toHaveProperty("task");
|
|
expect(buildNativeModelEnvelope(withGuidance).task.prompt.match(/Saved Slack instructions/g)).toHaveLength(1);
|
|
expect(current).toMatchObject({
|
|
schema: "paperclip.native-execution-input.v4",
|
|
provider: { kind: "codex", approvalPolicy: "on-request" },
|
|
});
|
|
const withEffort = parseNativeExecutionInput({
|
|
...current,
|
|
schema: NATIVE_EXECUTION_INPUT_SCHEMA,
|
|
provider: { kind: "codex", model: "gpt-6-astra", approvalPolicy: "on-request", reasoningEffort: "ultra" },
|
|
});
|
|
expect(withEffort.provider).toMatchObject({ kind: "codex", reasoningEffort: "ultra" });
|
|
expect(parseNativeExecutionInput(withEffort)).toEqual(withEffort);
|
|
expect(() => parseNativeExecutionInput({
|
|
...withEffort,
|
|
provider: { kind: "codex", model: "gpt-6-astra", approvalPolicy: "on-request", reasoningEffort: "impossible" },
|
|
})).toThrow("reasoningEffort");
|
|
expect(() => parseNativeExecutionInput({
|
|
...current,
|
|
provider: { kind: "codex", model: null, approvalPolicy: "on-request", reasoningEffort: "ultra" },
|
|
})).toThrow("input.provider");
|
|
expect(() => parseNativeExecutionInput({
|
|
...parsed,
|
|
schema: "paperclip.native-execution-input.v4",
|
|
provider: { kind: "codex", model: null, approvalPolicy: "sometimes" },
|
|
})).toThrow("approvalPolicy");
|
|
});
|
|
|
|
it("rejects runtime-context traversal and aggregate digest drift", () => {
|
|
const digest = "0".repeat(64);
|
|
const context = {
|
|
prompt: { revision: PAPERCLIP_EXECUTION_PROMPT_REVISION, text: PAPERCLIP_EXECUTION_PROMPT, digest: nativeRuntimePromptDigest() },
|
|
instructions: {
|
|
entryPath: "../AGENTS.md",
|
|
bundle: { schema: NATIVE_RUNTIME_ASSET_SCHEMA, digest, manifestDigest: digest, rootPath: "/runtime/instructions", fileCount: 1, totalBytes: 1 },
|
|
},
|
|
skills: [],
|
|
mcp: { assignmentSetId: "none", digest, bindingId: null },
|
|
aggregateDigest: digest,
|
|
} as const;
|
|
expect(() => parseNativeExecutionInput({ ...input, schema: "paperclip.native-execution-input.v3", executionMode: "default", planningContext: null, runtimeContext: context })).toThrow("bundle root");
|
|
expect(() => parseNativeExecutionInput({
|
|
...input,
|
|
schema: "paperclip.native-execution-input.v3",
|
|
executionMode: "default",
|
|
planningContext: null,
|
|
runtimeContext: { ...context, instructions: { ...context.instructions, entryPath: "AGENTS.md" } },
|
|
})).toThrow("aggregateDigest");
|
|
});
|
|
|
|
it("builds a model envelope without authority or credential bindings", () => {
|
|
const parsed = parseNativeExecutionInput(input);
|
|
const model = buildNativeModelEnvelope(parsed);
|
|
const serialized = JSON.stringify(model);
|
|
expect(serialized).not.toContain("company-1");
|
|
expect(serialized).not.toContain("run-1");
|
|
expect(serialized).not.toContain("opaque-binding");
|
|
expect(model.task.title).toBe("Safe task");
|
|
expect(model.task.prompt).toContain("latest comment");
|
|
});
|
|
|
|
it("rejects unknown context or environment escape hatches", () => {
|
|
expect(() => parseNativeExecutionInput({ ...input, context: { secret: "canary" } })).toThrow(
|
|
"unknown field context",
|
|
);
|
|
expect(() => parseNativeExecutionInput({
|
|
...input,
|
|
workspace: { ...input.workspace, env: { PAPERCLIP_API_KEY: "canary" } },
|
|
})).toThrow("unknown field env");
|
|
});
|
|
|
|
it("accepts a persisted OpenCode driver/model pair and rejects mismatches", () => {
|
|
const opencode = parseNativeExecutionInput({
|
|
...input,
|
|
session: { ...input.session, driverKind: "opencode_server" },
|
|
provider: { kind: "opencode", model: "openrouter/deepseek/deepseek-v4-flash-0731" },
|
|
});
|
|
expect(opencode.provider).toEqual({
|
|
kind: "opencode",
|
|
model: "openrouter/deepseek/deepseek-v4-flash-0731",
|
|
});
|
|
expect(parseNativeExecutionInput(opencode)).toEqual(opencode);
|
|
expect(() => parseNativeExecutionInput({
|
|
...input,
|
|
session: { ...input.session, driverKind: "opencode_server" },
|
|
provider: { kind: "codex", model: null },
|
|
})).toThrow("does not match");
|
|
});
|
|
|
|
it("deserializes pre-provider Codex state as Codex", () => {
|
|
const legacy = structuredClone(input) as Record<string, unknown>;
|
|
delete legacy.provider;
|
|
expect(parseNativeExecutionInput(legacy).provider).toEqual({
|
|
kind: "codex",
|
|
model: null,
|
|
});
|
|
expect(parseNativeExecutionInput(parseNativeExecutionInput(legacy))).toEqual(
|
|
parseNativeExecutionInput(legacy),
|
|
);
|
|
});
|
|
|
|
it("accepts an immutable Claude Managed Agent profile and rejects driver or beta drift", () => {
|
|
const claudeManaged = {
|
|
...input,
|
|
session: { ...input.session, driverKind: "claude_managed_agents_api" },
|
|
provider: {
|
|
kind: "claude_managed",
|
|
model: "claude-sonnet-5",
|
|
managedProfile: {
|
|
profileId: "managed-profile-1",
|
|
anthropicAgentId: "agent_01",
|
|
agentVersion: "3",
|
|
environmentId: "environment_01",
|
|
betaVersion: "managed-agents-2026-04-01",
|
|
},
|
|
maxSessionListCostUsd: 1,
|
|
},
|
|
} as const;
|
|
const parsed = parseNativeExecutionInput(claudeManaged);
|
|
expect(parsed.provider).toEqual(claudeManaged.provider);
|
|
expect(buildNativeModelEnvelope(parsed).workspace).toBeNull();
|
|
expect(() => parseNativeExecutionInput({
|
|
...claudeManaged,
|
|
session: { ...claudeManaged.session, driverKind: "codex_app_server" },
|
|
})).toThrow("does not match");
|
|
expect(() => parseNativeExecutionInput({
|
|
...claudeManaged,
|
|
provider: {
|
|
...claudeManaged.provider,
|
|
managedProfile: { ...claudeManaged.provider.managedProfile, betaVersion: "future-beta" },
|
|
},
|
|
})).toThrow("betaVersion");
|
|
});
|
|
|
|
it("accepts a closed AWS AgentCore Harness snapshot and rejects drift or unsafe limits", () => {
|
|
const awsAgentCore = {
|
|
...input,
|
|
session: { ...input.session, driverKind: "aws_agentcore_harness_api" },
|
|
provider: {
|
|
kind: "aws_agentcore",
|
|
model: "global.anthropic.claude-sonnet-4-6",
|
|
agentCoreProfile: {
|
|
profileId: "agentcore-development",
|
|
region: "us-east-1",
|
|
accountId: "123456789012",
|
|
harnessArn: "arn:aws:bedrock-agentcore:us-east-1:123456789012:harness/harness-1",
|
|
harnessVersion: "3",
|
|
endpointArn: "arn:aws:bedrock-agentcore:us-east-1:123456789012:harness-endpoint/harness-1/paperclip",
|
|
endpointQualifier: "paperclip",
|
|
agentRuntimeArn: "arn:aws:bedrock-agentcore:us-east-1:123456789012:runtime/runtime-1",
|
|
memoryArn: "arn:aws:bedrock-agentcore:us-east-1:123456789012:memory/memory-1",
|
|
memoryId: "memory-1",
|
|
invocationRoleArn: "arn:aws:iam::123456789012:role/paperclip-agentcore-runner",
|
|
contextBucket: "paperclip-agentcore-context",
|
|
contextPrefix: "paperclip/runtime",
|
|
contextKmsKeyArn: "arn:aws:kms:us-east-1:123456789012:key/test",
|
|
qualificationRevision: "aws-agentcore-harness-context-v2",
|
|
eventExpiryDays: 90,
|
|
},
|
|
maxEstimatedSessionCostUsd: 1,
|
|
invocationLimits: { maxIterations: 8, maxOutputTokens: 4096, timeoutSeconds: 300 },
|
|
},
|
|
} as const;
|
|
const parsed = parseNativeExecutionInput(awsAgentCore);
|
|
expect(parsed.provider).toEqual(awsAgentCore.provider);
|
|
expect(buildNativeModelEnvelope(parsed).workspace).toBeNull();
|
|
expect(JSON.stringify(parsed)).not.toContain("AWS_SECRET_ACCESS_KEY");
|
|
expect(() => parseNativeExecutionInput({
|
|
...awsAgentCore,
|
|
session: { ...awsAgentCore.session, driverKind: "codex_app_server" },
|
|
})).toThrow("does not match");
|
|
expect(() => parseNativeExecutionInput({
|
|
...awsAgentCore,
|
|
provider: { ...awsAgentCore.provider, invocationLimits: { ...awsAgentCore.provider.invocationLimits, maxIterations: 9 } },
|
|
})).toThrow("maxIterations");
|
|
expect(() => parseNativeExecutionInput({
|
|
...awsAgentCore,
|
|
provider: { ...awsAgentCore.provider, agentCoreProfile: { ...awsAgentCore.provider.agentCoreProfile, eventExpiryDays: 30 } },
|
|
})).toThrow("eventExpiryDays");
|
|
});
|
|
|
|
it.each([1, 2, 3, 4, 5] as const)("accepts only a closed ACPX profile matching the driver and agent at profile version %s", (agentProfileVersion) => {
|
|
const provider = {
|
|
kind: "acpx",
|
|
agent: "pi",
|
|
model: "openrouter/deepseek/deepseek-v4-flash-0731",
|
|
permissionPolicy: "interactive",
|
|
profile: {
|
|
driverKind: "acpx_runtime",
|
|
protocolVersion: 1,
|
|
acpxVersion: "0.13.1",
|
|
agent: "pi",
|
|
agentProfileVersion,
|
|
agentServerPackage: "pi-acp",
|
|
agentServerVersion: "0.0.33",
|
|
agentRuntimePackage: "@earendil-works/pi-coding-agent",
|
|
agentRuntimeVersion: "0.84.2",
|
|
commandDigest: "sha256:24ff73fda6e3c76ddce2d359a79f5c4b8f292eb290e4d2ab85aac94676b2c2dc",
|
|
},
|
|
} as const;
|
|
const parsed = parseNativeExecutionInput({
|
|
...input,
|
|
session: { ...input.session, driverKind: "acpx_runtime" },
|
|
provider,
|
|
});
|
|
expect(parsed.provider).toEqual({
|
|
kind: "acpx",
|
|
agent: "pi",
|
|
model: "openrouter/deepseek/deepseek-v4-flash-0731",
|
|
permissionPolicy: "interactive",
|
|
profile: provider.profile,
|
|
});
|
|
expect(parseNativeExecutionInput(parsed)).toEqual(parsed);
|
|
for (const unsupportedVersion of [0, 6, 1.5, "5", null]) {
|
|
expect(() => parseNativeExecutionInput({
|
|
...input,
|
|
session: { ...input.session, driverKind: "acpx_runtime" },
|
|
provider: { ...provider, profile: { ...provider.profile, agentProfileVersion: unsupportedVersion } },
|
|
})).toThrow("qualified ACPX v1 profile");
|
|
}
|
|
expect(buildNativeModelEnvelope(parsed).workspace).toEqual({ cwd: "/safe/workspace" });
|
|
expect(() => parseNativeExecutionInput({
|
|
...input,
|
|
session: { ...input.session, driverKind: "acpx_runtime" },
|
|
provider: { ...provider, profile: { ...provider.profile, agent: "claude" } },
|
|
})).toThrow("qualified ACPX v1 profile");
|
|
expect(() => parseNativeExecutionInput({
|
|
...input,
|
|
session: { ...input.session, driverKind: "opencode_server" },
|
|
provider,
|
|
})).toThrow("does not match");
|
|
});
|
|
|
|
it("defaults legacy lifecycle state to per-turn and validates warm timeouts", () => {
|
|
const legacy = structuredClone(input) as Record<string, unknown>;
|
|
delete (legacy.session as Record<string, unknown>).lifecyclePolicy;
|
|
expect(parseNativeExecutionInput(legacy).session.lifecyclePolicy).toEqual({
|
|
mode: "per_turn",
|
|
idleTimeoutMs: null,
|
|
});
|
|
expect(parseNativeExecutionInput({
|
|
...input,
|
|
session: {
|
|
...input.session,
|
|
lifecyclePolicy: { mode: "warm", idleTimeoutMs: 300_000 },
|
|
},
|
|
}).session.lifecyclePolicy).toEqual({ mode: "warm", idleTimeoutMs: 300_000 });
|
|
expect(() => parseNativeExecutionInput({
|
|
...input,
|
|
session: {
|
|
...input.session,
|
|
lifecyclePolicy: { mode: "warm", idleTimeoutMs: 0 },
|
|
},
|
|
})).toThrow("positive integer");
|
|
expect(() => parseNativeExecutionInput({
|
|
...input,
|
|
session: {
|
|
...input.session,
|
|
lifecyclePolicy: { mode: "warm", idleTimeoutMs: 86_400_001 },
|
|
},
|
|
})).toThrow("no greater than 86400000");
|
|
});
|
|
});
|
|
|
|
describe("NativeExecutionInputV2 planning", () => {
|
|
const planning = {
|
|
...input,
|
|
schema: "paperclip.native-execution-input.v2",
|
|
executionMode: "plan",
|
|
task: { ...input.task, workMode: "planning" },
|
|
planningContext: {
|
|
documentId: "document-1",
|
|
baseRevisionId: "revision-3",
|
|
baseRevisionNumber: 3,
|
|
markdown: "# Existing plan",
|
|
sha256: "plan-digest",
|
|
reviewContext: { threads: [{ id: "annotation-1" }] },
|
|
},
|
|
} as const;
|
|
|
|
it("round-trips native plan mode and its pinned canonical revision into model context", () => {
|
|
const parsed = parseNativeExecutionInput(planning);
|
|
expect(parsed).toMatchObject({ schema: "paperclip.native-execution-input.v2", executionMode: "plan" });
|
|
expect(buildNativeModelEnvelope(parsed)).toMatchObject({
|
|
schema: "paperclip.native-model-envelope.v2",
|
|
executionMode: "plan",
|
|
planningContext: { baseRevisionId: "revision-3", baseRevisionNumber: 3 },
|
|
});
|
|
});
|
|
|
|
it("fails closed when plan mode omits its pinned Paperclip context", () => {
|
|
expect(() => parseNativeExecutionInput({ ...planning, planningContext: null }))
|
|
.toThrow("planningContext is required");
|
|
expect(() => parseNativeExecutionInput({
|
|
...planning,
|
|
task: { ...planning.task, workMode: "standard" },
|
|
})).toThrow("requires planning work mode");
|
|
});
|
|
|
|
it("allows an accepted planning issue to continue in fresh default execution mode", () => {
|
|
expect(parseNativeExecutionInput({
|
|
...planning,
|
|
executionMode: "default",
|
|
planningContext: null,
|
|
})).toMatchObject({ task: { workMode: "planning" }, executionMode: "default" });
|
|
});
|
|
});
|
|
|
|
describe("NativeExecutionInputV2 ask mode", () => {
|
|
it("round-trips ask mode as a default execution without planning context", () => {
|
|
const parsed = parseNativeExecutionInput({
|
|
...input,
|
|
schema: "paperclip.native-execution-input.v2",
|
|
executionMode: "default",
|
|
task: { ...input.task, workMode: "ask" },
|
|
planningContext: null,
|
|
});
|
|
expect(parsed).toMatchObject({
|
|
schema: "paperclip.native-execution-input.v2",
|
|
executionMode: "default",
|
|
task: { workMode: "ask" },
|
|
});
|
|
expect(buildNativeModelEnvelope(parsed)).toMatchObject({
|
|
schema: "paperclip.native-model-envelope.v2",
|
|
task: { workMode: "ask" },
|
|
executionMode: "default",
|
|
planningContext: null,
|
|
});
|
|
});
|
|
|
|
it("rejects plan execution for ask mode", () => {
|
|
expect(() => parseNativeExecutionInput({
|
|
...input,
|
|
schema: "paperclip.native-execution-input.v2",
|
|
executionMode: "plan",
|
|
task: { ...input.task, workMode: "ask" },
|
|
planningContext: {
|
|
documentId: null,
|
|
baseRevisionId: null,
|
|
baseRevisionNumber: 0,
|
|
markdown: "",
|
|
sha256: "digest",
|
|
reviewContext: {},
|
|
},
|
|
})).toThrow("plan execution mode requires planning work mode");
|
|
});
|
|
});
|
|
|
|
|
|
describe("native task context ownership", () => {
|
|
function currentInput() {
|
|
const digest = "0".repeat(64);
|
|
const context = {
|
|
prompt: { revision: PAPERCLIP_EXECUTION_PROMPT_REVISION, text: PAPERCLIP_EXECUTION_PROMPT, digest: nativeRuntimePromptDigest() },
|
|
instructions: { entryPath: "AGENTS.md", bundle: { schema: NATIVE_RUNTIME_ASSET_SCHEMA, digest, manifestDigest: digest, rootPath: "/runtime/instructions", fileCount: 1, totalBytes: 1 } },
|
|
skills: [],
|
|
mcp: { assignmentSetId: "none", digest, bindingId: null },
|
|
} as const;
|
|
return parseNativeExecutionInput({
|
|
...input,
|
|
schema: NATIVE_EXECUTION_INPUT_SCHEMA,
|
|
task: { ...input.task, description: "Use $assigned-skill. Repeat this. Repeat this.", prompt: "# PAP-1\n\nIssue description:\nUse $assigned-skill. Repeat this. Repeat this." },
|
|
provider: { kind: "codex", model: null, approvalPolicy: "never" },
|
|
executionMode: "default",
|
|
planningContext: null,
|
|
runtimeContext: { ...context, aggregateDigest: canonicalNativeRuntimeContextDigest(context) },
|
|
});
|
|
}
|
|
|
|
it.each([
|
|
{ driverKind: "opencode_server", provider: { kind: "opencode", model: "openrouter/deepseek/deepseek-v4-flash-0731", permissionMode: "deny" } },
|
|
{ driverKind: "acpx_runtime", provider: {
|
|
kind: "acpx", agent: "pi", model: "openrouter/deepseek/deepseek-v4-flash-0731", permissionMode: "deny-all",
|
|
profile: { driverKind: "acpx_runtime", protocolVersion: 1, acpxVersion: "0.13.1", agent: "pi", agentProfileVersion: 1,
|
|
agentServerPackage: "pi-acp", agentServerVersion: "0.0.33", agentRuntimePackage: "@earendil-works/pi-coding-agent",
|
|
agentRuntimeVersion: "0.84.2", commandDigest: `sha256:${"a".repeat(64)}` },
|
|
} },
|
|
{ driverKind: "openai_dot_mcp", provider: {
|
|
kind: "openai_dot", model: null,
|
|
binding: { bindingId: "dot-binding", bindingGeneration: 1, companyId: input.binding.companyId, agentId: input.binding.agentId,
|
|
acceptByUnixMs: 1_000, expiresAtUnixMs: 2_000 },
|
|
} },
|
|
])("preserves an unregistered saved prompt for $provider.kind", ({ driverKind, provider }) => {
|
|
const current = currentInput();
|
|
if (!("runtimeContext" in current)) throw new Error("Expected a runtime context");
|
|
const text = "Saved instructions absent from the current release.";
|
|
const context = { ...current.runtimeContext,
|
|
prompt: { revision: "saved-prompt-before-upgrade", text, digest: createHash("sha256").update(text).digest("hex") } };
|
|
context.aggregateDigest = canonicalNativeRuntimeContextDigest(context);
|
|
const persisted = JSON.parse(JSON.stringify({
|
|
...current, provider, session: { ...current.session, driverKind }, runtimeContext: context,
|
|
...(provider.kind === "openai_dot" ? {
|
|
schema: NATIVE_EXECUTION_INPUT_SCHEMA_V6,
|
|
workspace: { access: "none", cwd: null, repoUrl: null, repoRef: null, branchName: null },
|
|
credentialBindings: [],
|
|
} : {}),
|
|
}));
|
|
const recovered = parseNativeExecutionInput(persisted);
|
|
expect(recovered.runtimeContext).toEqual(context);
|
|
expect(recovered.provider).toEqual(provider);
|
|
expect(recovered.session.driverKind).toBe(driverKind);
|
|
expect(parseNativeExecutionInput(recovered)).toEqual(recovered);
|
|
});
|
|
|
|
it("carries an opaque provider mode without a vendor restriction and fences obsolete field names", () => {
|
|
const current = currentInput();
|
|
const provider = {
|
|
kind: "acpx", agent: "codex", model: "gpt-5.6-sol", permissionMode: "approve-all", mode: "architect",
|
|
profile: { driverKind: "acpx_runtime", protocolVersion: 1, acpxVersion: "0.13.1", agent: "codex", agentProfileVersion: 3,
|
|
agentServerPackage: "@agentclientprotocol/codex-acp", agentServerVersion: "1.6.2",
|
|
agentRuntimePackage: "@openai/codex", agentRuntimeVersion: "0.160.0", commandDigest: `sha256:${"a".repeat(64)}` },
|
|
};
|
|
const value = { ...current, session: { ...current.session, driverKind: "acpx_runtime" }, provider };
|
|
const parsed = parseNativeExecutionInput(value);
|
|
expect(parsed.provider).toMatchObject({ agent: "codex", mode: "architect" });
|
|
expect(parseNativeExecutionInput(parsed)).toEqual(parsed);
|
|
expect(JSON.stringify(buildNativeModelEnvelope(parsed))).not.toContain("architect");
|
|
for (const mode of [null, 1, "", " ", "x".repeat(241), "plan\0", "plan\n"]) {
|
|
expect(() => parseNativeExecutionInput({ ...value, provider: { ...provider, mode } })).toThrow(/provider.mode/);
|
|
}
|
|
const { mode: _mode, ...withoutMode } = provider;
|
|
expect(() => parseNativeExecutionInput({ ...value, provider: { ...withoutMode, cursorMode: "plan" } })).toThrow(/input.provider/);
|
|
});
|
|
|
|
it.each([
|
|
["v4", "paperclip.native-execution-input.v4", "paperclip.native-model-envelope.v2"],
|
|
["v5", NATIVE_EXECUTION_INPUT_SCHEMA, "paperclip.native-model-envelope.v3"],
|
|
] as const)("applies communication guidance once for fresh %s input and never on resume", (_label, schema, envelopeSchema) => {
|
|
const guidance = "Saved Slack instructions";
|
|
const v5 = currentInput();
|
|
const source = {
|
|
kind: "description" as const,
|
|
id: v5.binding.issueId,
|
|
revision: createHash("sha256").update(v5.task.description!).digest("hex"),
|
|
};
|
|
const contract = {
|
|
...v5.completionContract.contract,
|
|
criteria: [{ id: "objective", requirement: v5.task.description! }],
|
|
};
|
|
const parsed = parseNativeExecutionInput({
|
|
...v5,
|
|
schema,
|
|
initialCommunicationGuidance: guidance,
|
|
...(schema === NATIVE_EXECUTION_INPUT_SCHEMA ? {
|
|
completionContract: { ...v5.completionContract, contract },
|
|
completionSources: {
|
|
promptSha256: createHash("sha256").update(v5.task.prompt).digest("hex"),
|
|
contractRevision: contract.revision,
|
|
criteria: [{ id: "objective", source }],
|
|
},
|
|
} : {}),
|
|
});
|
|
const fresh = buildNativeModelEnvelope(parsed);
|
|
expect(fresh.schema).toBe(envelopeSchema);
|
|
expect(fresh.task.prompt).toBe(`${guidance}\n\n${parsed.task.prompt}`);
|
|
expect(fresh.task.prompt.match(/Saved Slack instructions/g)).toHaveLength(1);
|
|
if (schema === NATIVE_EXECUTION_INPUT_SCHEMA) {
|
|
expect(fresh.task).not.toHaveProperty("description");
|
|
expect(fresh.completionContract.criteria).toEqual([
|
|
{ id: "objective", source: { ...source, location: "task.prompt" } },
|
|
]);
|
|
} else {
|
|
expect(fresh.task).toHaveProperty("description", parsed.task.description);
|
|
}
|
|
|
|
const fullResume = buildNativeModelEnvelope(parsed, { resumedSession: true });
|
|
expect(fullResume.schema).toBe(envelopeSchema);
|
|
expect(fullResume.task.prompt).toBe(parsed.task.prompt);
|
|
expect(fullResume.task.prompt).not.toContain(guidance);
|
|
|
|
const compactResume = buildNativeModelEnvelope(
|
|
parseNativeExecutionInput({ ...parsed, continuationPrompt: "new message" }),
|
|
{ resumedSession: true },
|
|
);
|
|
expect(compactResume).toMatchObject({ schema: "paperclip.native-continuation.v1" });
|
|
expect(JSON.stringify(compactResume)).not.toContain(guidance);
|
|
});
|
|
|
|
it("selects model fields without losing the internal skill-selection description", () => {
|
|
const execution = currentInput();
|
|
const envelope = buildNativeModelEnvelope(execution);
|
|
expect(envelope.task).toEqual({
|
|
identifier: execution.task.identifier,
|
|
title: execution.task.title,
|
|
prompt: execution.task.prompt,
|
|
workMode: execution.task.workMode,
|
|
});
|
|
expect(execution.task.description).toBe("Use $assigned-skill. Repeat this. Repeat this.");
|
|
expect(envelope.task.prompt).toContain("Repeat this. Repeat this.");
|
|
expect(envelope.completionContract).toEqual(execution.completionContract.contract);
|
|
});
|
|
|
|
it("references only verified criterion sources and preserves independent requirements", () => {
|
|
const execution = currentInput();
|
|
const source = { kind: "description", id: execution.binding.issueId, revision: createHash("sha256").update(execution.task.description!).digest("hex") };
|
|
const contract = { ...execution.completionContract.contract, criteria: [
|
|
{ id: "objective", requirement: execution.task.description! },
|
|
{ id: "independent", requirement: "Also report a measured result." },
|
|
] };
|
|
const completeInput = parseNativeExecutionInput({
|
|
...execution,
|
|
completionContract: { ...execution.completionContract, contract },
|
|
completionSources: { promptSha256: createHash("sha256").update(execution.task.prompt).digest("hex"), contractRevision: contract.revision, criteria: [{ id: "objective", source }] },
|
|
});
|
|
const before = structuredClone(completeInput.completionContract);
|
|
const envelope = buildNativeModelEnvelope(completeInput);
|
|
expect(envelope.completionContract.criteria).toEqual([
|
|
{ id: "objective", source: { ...source, location: "task.prompt" } },
|
|
{ id: "independent", requirement: "Also report a measured result." },
|
|
]);
|
|
expect(completeInput.completionContract).toEqual(before);
|
|
// A different prompt or contract revision invalidates the projection, never the saved contract.
|
|
for (const stale of [
|
|
{ ...completeInput, task: { ...completeInput.task, prompt: "Recovery without the original brief" } },
|
|
{ ...completeInput, completionContract: { ...completeInput.completionContract, contract: { ...contract, revision: "new" } } },
|
|
]) {
|
|
expect(buildNativeModelEnvelope(stale).completionContract).toEqual(stale.completionContract.contract);
|
|
}
|
|
});
|
|
});
|