mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-28 12:04:41 +01:00
fix: make runner task context ownership explicit (#13753)
<!-- Write all pull request text in Simplified Technical English (ASD-STE100). --> ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Task descriptions, comments, continuation data, skills, and execution rules enter several agent adapters. > - The same source can be rendered by more than one automatic input carrier. > - Failed resumes can also rebuild input from stale or compact context. > - This pull request gives each Paperclip-owned source one delivery owner and preserves the required transport boundaries. > - It adds deterministic adapter, interaction, runner, and browser tests for these boundaries. > - The benefit is more predictable context delivery with explicit evidence for later live qualification. ## Linked Issues or Issue Description Related: #13144 removes a duplicate environment payload and bounds wake lists. Related: #11360 addresses Hermes resume behavior. This pull request preserves compatible active-session formats while repairing context ownership and stale question creation. **What happened?** Task descriptions and comments could enter more than one automatic context block. Native transports could wrap a complete model input in a second task envelope. Some legacy and gateway adapters could omit the owned assignment on ordinary tasks or rebuild a failed resume with stale compact context. A continuation could also request a question after newer human comments had arrived. **Expected behavior** Each task or comment source has one automatic model-facing owner. Distinct comment IDs and repeated wording remain distinct. Fresh fallback attempts rebuild the required full context. A question request is rejected when newer queued human direction makes it stale. Harness access policy remains owned by execution configuration. **Steps to reproduce** 1. Build a task with a description and current comments. 2. Capture the actual adapter or runner input. 3. Compare source ownership and task-envelope nesting. 4. Queue a human comment before a continuation requests a question. 5. Trigger a failed resume and inspect the fresh retry input. 6. Run the focused adapter, interaction, runner, and browser checks. ## What Changed - Add shared prompt-section selection at the provider-attempt boundary. - Deliver owned assignment context through native, legacy CLI, ACP, gateway, cloud, Pi, Kimi, Grok, Gemini, OpenCode, Cursor, OpenClaw, and Hermes paths. - Rebuild full or compact context after resume recovery changes the attempt. Add native and Claude ACP tests of actual recovery requests. - Preserve custom templates, loaded instruction files, execution policies, and older active-session formats. - Record continuation source metadata and reject stale question creation under the issue-row lock. - Add explicit Product E2E context-integrity profiles, prerequisite gates, credential-isolation checks, and report fixtures. - Bypass service-worker forwarding for same-origin Vite development modules. A real Chromium test fails with resource exhaustion before the repair and passes after it. Production asset caching keeps its existing policy. - Add browser diagnostics and service-worker module-loading regressions. - Add an explicit zero-retry eval option. The default retry behavior remains unchanged. Each campaign records its effective policy. - Remove the model-facing working-directory sentence from four prompt builders. Existing workspace, sandbox, permission, and custom-template configuration remains unchanged. - Align the everyday workflow assertion with the current 47-entry catalog. Compared with current upstream master, the branch carries the context-ownership implementation and its tests, the explicit context-integrity catalog and evidence harness, and the focused browser regression checks. ## Verification **Merge assessment:** focused regression evidence supports merge. This is not full completion of the original broad qualification matrix. The maintainer has authorized merge after fresh verification of the master integration. - Current head: `bbd52f82114eabf09bc7b1a7e97d54a5b43bbc00`. This integrates current master `2f585ef26a1814fa209715242d1ca791b63e4c4e`. All 14 conflicts are resolved. Cancellation checks, workspace finalization, native Grok support, and both sets of tests are retained. - Current-head Greptile: **5/5**, with no blocking findings. The review names this exact commit. All **59 reported checks are terminal: 55 successful, 4 skipped, zero pending or failing**. This includes the full root general and serialized suites, separate runner checks, typecheck, build, canary, browser E2E, Docker, and security checks. The successful legacy security status is included in that total. - After integration: workspace typecheck and full build passed. Separate runner checks passed: **2,160 TypeScript tests (10 skipped), 582 Rust tests, and 39 preparation checks**. Other passing checks include 621 Product E2E harness units, 376 focused shared/adapter tests, 160 real-database/API tests, 86 Hermes tests, 18 browser-support checks, and Product E2E typechecking. The complete root suite passed in CI. The duplicate local monolithic root run was stopped after that CI result; it is not counted as a completed local pass. - New native recovery coverage retains full assignment, completion contract, and explicit skill selection after safe replacement, for old and prepared input formats. Full native session test file: **136/136 passed**. - New Claude ACP coverage captures actual fresh, resumed, and missing-session fallback requests. It verifies one assignment copy, comment order, identical text under distinct comment IDs, and full fallback context. Full file: **33/33 passed**. Both affected TypeScript checks passed. - Existing deterministic tests cover source revisions, approval and trust boundaries, completion validation, custom templates, compatible sessions, standalone driver wrapping, and maintained adapter transport requests. - Provider-free browser support: **17/17 passed** after the master merge. Service-worker unit tests: **33/33 passed**. The module-overload regression failed before the repair and passed after it in real Chromium. ### Fresh live comparisons The new batch ran exactly four Product E2E attempts. **All four passed on the first attempt; no retries.** Each has six terminal matchers plus the existing browser lifecycle and invariant checks. | Exact case ID | Control | Candidate | |---|---|---| | `core-compatibility.runner-codex.local.plan-revise-accept` | Passed | Passed | | `local-session-integrity.runner-acpx-claude.local.structured-question-restart-resume` | Passed | Passed | The plan case checks a revised canonical plan and revision-bound approval before completion. The question case restarts the server before submitting the answer, then verifies the continuation completes. Control source is `dfa4e1bda8d50a1a01746603251a9128dbe9d0d6`. Candidate source is `79fcdb5dece501d28064ea9da306603881b46f0c`. They use identical frozen definitions and provider versions: Codex `0.156.0` with `gpt-5.6-sol`; ACPX `0.13.1` / Claude ACP `0.73.0` with `claude-sonnet-5`. The September 24 head added master browser recovery and test-only changes. The September 28 head also integrates newer master changes, including cancellation, workspace finalization, and native Grok. These are frozen-source live results, not exact-head live runs. The candidate received one description copy where the control initially received three. The submitted initial plan envelopes were 7,969 versus 19,097 characters. Question envelopes were 7,592 versus 18,919. These are structural measurements, not whole-provider token or dollar savings. ### Earlier evidence and failed attempts - The preceding fresh batch has four effective passing pairs: OpenCode comment continuation and assigned skill, native Codex comment continuation, and native Claude comment continuation. It retains **11 attempts: eight passed and three failed**. - Original failures remain recorded: missing local PostgreSQL library links before task creation; host-sleep cleanup after task/page checks passed; and a Claude **control** session-open rejection before a model turn. Setup was repaired identically on both worktrees. The permitted unchanged infrastructure retries passed. The underlying Claude provider startup error was not retained and remains unknown. - Older R2 retains **17 passes and one failure** across 18 attempts, including eight both-pass native/legacy Codex/Claude pairs. Its OpenCode blank-page failure led to the service-worker repair. R2 is historical evidence: master changed the native fixed prompt and removed duplicate wake environment data afterward. - The September 24 CI run initially failed one unrelated preview readiness test (`ECONNREFUSED` on its local fixture). Its test and production code match master. Isolated local verification passed **28 tests, 3 skipped**. One unchanged CI retry passed the full shard: **831 passed, 1 skipped**, including all **31 preview-exposure tests**. The aggregate CI gate passed afterward. The precise startup cause remains unknown; a port race is a hypothesis, not a proved cause. ### Limits The original wider profile/workflow matrix, repeated trials, and remote Daytona qualification are incomplete. These results support a focused merge recommendation, not statistical equivalence or universal harness qualification. Some usage receipts are missing in both variants, so no token or dollar savings are claimed. The $500 ceiling was preserved using conservative allowances; failed attempts and unknown charges remain in the ledger. Reproduce the focused additions with `pnpm exec vitest run packages/adapters/claude-local/src/server/acp.test.ts` and `pnpm --filter @paperclipai/paperclip-runner exec vitest run src/native-session-runtime.test.ts`. Full checks use `pnpm -r typecheck`, `pnpm test:run`, `pnpm build`, and the separate runner checks. Paid evals require the frozen definitions, profiles, and credentials; do not use `--all` as a substitute for the selected cases. ## Risks - Context placement changes can affect model behavior. Deterministic checks cover the selected paths, but live qualification remains incomplete. - The stale-question guard can reject a request when queued human comments arrived during the run. This is intended. - New stored inputs and model envelopes retain compatibility readers for older active sessions. - Custom templates may intentionally repeat content. - Removing a model-facing working-directory sentence does not change filesystem, command, sandbox, or permission configuration. - The worker bypass applies only to same-origin development module paths. Cache-policy tests preserve private-response handling and production asset caching. Mounted HTTP fixture changes remain test-only. - This PR does not claim measured token savings or statistical equivalence across every harness. ## Model Used OpenAI Codex, exact model gpt-6-astra, with repository tools and code execution. Bounded supporting work used gpt-5.6-luna and gpt-6-luna. The serving context-window size is not exposed in this task. ## 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 described the issue in-PR using the required issue fields - [x] I have not referenced internal/instance-local Paperclip issues or links - [x] My branch name describes the change and contains no internal ticket id - [x] I have run the focused local checks and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect these changes - [x] I have considered and documented risks above - [x] All current-head Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups for the current head - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
2f585ef26a
commit
992f720262
122 files changed
+5487
-636
No files matched your search
@@ -280,7 +280,7 @@ describeEmbeddedPostgres(
|
||||
const mismatchedRun = await service.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
{ ...question, sourceRunId: foreignRunId },
|
||||
{ agentId: fixture.agentId },
|
||||
{ agentId: foreignAgentId },
|
||||
);
|
||||
|
||||
await expect(
|
||||
|
||||
@@ -4,6 +4,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import type { AdapterRuntimeMcpServer } from "@paperclipai/adapter-utils";
|
||||
import { runChildProcess } from "@paperclipai/adapter-utils/server-utils";
|
||||
import { buildPaperclipTaskMarkdown } from "../services/heartbeat.js";
|
||||
import {
|
||||
claudeCommandSupportsEffortFlag,
|
||||
claudeSessionCwdMatchesExecutionTarget,
|
||||
@@ -351,6 +352,63 @@ function createLocalSandboxRunner() {
|
||||
}
|
||||
|
||||
describe("claude execute", () => {
|
||||
it("passes real assignment markdown and ordered current events once through the CLI", async () => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-claude-context-owner-"));
|
||||
const { workspace, commandPath, capturePath, restore } = await setupExecuteEnv(root);
|
||||
const markdown = buildPaperclipTaskMarkdown({
|
||||
issue: { id: "issue-1", identifier: "PAP-901", title: "Repeat phrase Repeat phrase", description: "Repeat phrase Repeat phrase" },
|
||||
wakeComments: [
|
||||
{ id: "comment-a", body: "Same event body." },
|
||||
{ id: "comment-b", body: "Same event body." },
|
||||
],
|
||||
includeWakeComments: false,
|
||||
});
|
||||
const historicalMarkdown = buildPaperclipTaskMarkdown({
|
||||
issue: { id: "issue-1", identifier: "PAP-901", title: "Repeat phrase Repeat phrase", description: "Repeat phrase Repeat phrase" },
|
||||
wakeComments: [
|
||||
{ id: "comment-a", body: "Same event body." },
|
||||
{ id: "comment-b", body: "Same event body." },
|
||||
],
|
||||
});
|
||||
try {
|
||||
await execute({
|
||||
runId: "run-context-owner",
|
||||
agent: { id: "agent-1", companyId: "company-1", name: "Claude", adapterType: "claude_local", adapterConfig: {} },
|
||||
runtime: { sessionId: null, sessionParams: null, sessionDisplayId: null, taskKey: null },
|
||||
config: {
|
||||
engine: "cli", command: commandPath, cwd: workspace,
|
||||
env: { PAPERCLIP_TEST_CAPTURE_PATH: capturePath },
|
||||
promptTemplate: "Custom template keeps {{paperclipTaskMarkdown}} and {{paperclipWakePrompt}}.",
|
||||
},
|
||||
context: {
|
||||
issueId: "issue-1",
|
||||
paperclipTaskMarkdown: historicalMarkdown,
|
||||
paperclipTaskMarkdownAssignment: markdown,
|
||||
paperclipWake: {
|
||||
reason: "issue_commented",
|
||||
issue: { id: "issue-1", identifier: "PAP-901", title: "Repeat phrase Repeat phrase", description: "Repeat phrase Repeat phrase", status: "in_progress" },
|
||||
comments: [
|
||||
{ id: "comment-a", issueId: "issue-1", body: "Same event body.", bodyTruncated: false, createdAt: "2026-09-21T00:00:00.000Z" },
|
||||
{ id: "comment-b", issueId: "issue-1", body: "Same event body.", bodyTruncated: false, createdAt: "2026-09-21T00:01:00.000Z" },
|
||||
],
|
||||
commentWindow: { requestedCount: 2, includedCount: 2, missingCount: 0 },
|
||||
fallbackFetchNeeded: false,
|
||||
},
|
||||
paperclipTurnContext: { version: 1, assignment: { owner: "task_markdown" }, events: { owner: "wake_prompt", comments: [{ id: "comment-a", revision: "a" }, { id: "comment-b", revision: "b" }] } },
|
||||
},
|
||||
onLog: async () => {},
|
||||
});
|
||||
const capture = JSON.parse(await fs.readFile(capturePath, "utf8")) as CapturePayload;
|
||||
expect(capture.prompt).toContain("Custom template keeps");
|
||||
expect(capture.prompt.indexOf("comment-a")).toBeLessThan(capture.prompt.indexOf("comment-b"));
|
||||
expect(capture.prompt.split("Same event body.")).toHaveLength(3);
|
||||
expect(capture.prompt.split("Repeat phrase Repeat phrase")).toHaveLength(4);
|
||||
} finally {
|
||||
restore();
|
||||
await fs.rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
[undefined, "claude-opus-5"],
|
||||
["", "claude-opus-5"],
|
||||
|
||||
@@ -659,6 +659,84 @@ describe("codex execute", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps real assignment markdown and current wake events single-owned at the CLI boundary", async () => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-codex-context-owner-"));
|
||||
const workspace = path.join(root, "workspace");
|
||||
const commandPath = path.join(root, "codex");
|
||||
const capturePath = path.join(root, "capture.json");
|
||||
await fs.mkdir(workspace, { recursive: true });
|
||||
await writeFakeCodexCommand(commandPath);
|
||||
const markdown = buildPaperclipTaskMarkdown({
|
||||
issue: { id: "issue-1", identifier: "PAP-900", title: "Repeat phrase Repeat phrase", description: "Repeat phrase Repeat phrase" },
|
||||
wakeComments: [
|
||||
{ id: "comment-a", body: "Same event body." },
|
||||
{ id: "comment-b", body: "Same event body." },
|
||||
],
|
||||
includeWakeComments: false,
|
||||
});
|
||||
const historicalMarkdown = buildPaperclipTaskMarkdown({
|
||||
issue: { id: "issue-1", identifier: "PAP-900", title: "Repeat phrase Repeat phrase", description: "Repeat phrase Repeat phrase" },
|
||||
wakeComments: [
|
||||
{ id: "comment-a", body: "Same event body." },
|
||||
{ id: "comment-b", body: "Same event body." },
|
||||
],
|
||||
});
|
||||
try {
|
||||
await execute({
|
||||
runId: "run-context-owner",
|
||||
agent: { id: "agent-1", companyId: "company-1", name: "Codex", adapterType: "codex_local", adapterConfig: {} },
|
||||
runtime: { sessionId: null, sessionParams: null, sessionDisplayId: null, taskKey: null },
|
||||
config: {
|
||||
engine: "cli", command: commandPath, cwd: workspace,
|
||||
env: { PAPERCLIP_TEST_CAPTURE_PATH: capturePath },
|
||||
promptTemplate: "Custom template keeps {{paperclipTaskMarkdown}} and {{paperclipWakePrompt}}.",
|
||||
},
|
||||
context: {
|
||||
issueId: "issue-1",
|
||||
paperclipTaskMarkdown: historicalMarkdown,
|
||||
paperclipTaskMarkdownAssignment: markdown,
|
||||
paperclipWake: {
|
||||
reason: "issue_commented",
|
||||
issue: { id: "issue-1", identifier: "PAP-900", title: "Repeat phrase Repeat phrase", description: "Repeat phrase Repeat phrase", status: "in_progress" },
|
||||
executionContinuation: {
|
||||
version: 1,
|
||||
companyId: "company-1",
|
||||
issueId: "issue-1",
|
||||
trigger: { reason: "issue_commented", interactionId: null, sourceRunId: null },
|
||||
originCommentIds: [],
|
||||
objective: "Repeat phrase Repeat phrase",
|
||||
objectiveSource: { kind: "description", id: "issue-1", revision: "2e2ed64c2ca5be0343f591d037a6f40a0b9cee733fedb7fdafa69e4158299957" },
|
||||
messages: [],
|
||||
interactionOutcomes: [],
|
||||
completedWork: null,
|
||||
unresolvedInteractionIds: [],
|
||||
coverage: { kind: "full_task_history", throughCommentId: null, summaryThroughCommentId: null },
|
||||
},
|
||||
comments: [
|
||||
{ id: "comment-a", issueId: "issue-1", body: "Same event body.", bodyTruncated: false, createdAt: "2026-09-21T00:00:00.000Z" },
|
||||
{ id: "comment-b", issueId: "issue-1", body: "Same event body.", bodyTruncated: false, createdAt: "2026-09-21T00:01:00.000Z" },
|
||||
],
|
||||
commentWindow: { requestedCount: 2, includedCount: 2, missingCount: 0 },
|
||||
fallbackFetchNeeded: false,
|
||||
},
|
||||
paperclipTurnContext: { version: 1, assignment: { owner: "task_markdown" }, events: { owner: "wake_prompt", comments: [{ id: "comment-a", revision: "a" }, { id: "comment-b", revision: "b" }] } },
|
||||
},
|
||||
onLog: async () => {},
|
||||
});
|
||||
const capture = JSON.parse(await fs.readFile(capturePath, "utf8")) as CapturePayload;
|
||||
expect(capture.prompt).toContain("Custom template keeps");
|
||||
expect(capture.prompt).toContain("comment-a");
|
||||
expect(capture.prompt.indexOf("comment-a")).toBeLessThan(capture.prompt.indexOf("comment-b"));
|
||||
expect(capture.prompt.split("Same event body.")).toHaveLength(3);
|
||||
// The custom template intentionally repeats task context; ownership only
|
||||
// removes automatic wake-event duplication.
|
||||
expect(capture.prompt.split("Repeat phrase Repeat phrase")).toHaveLength(4);
|
||||
expect(capture.prompt).not.toContain('"objective":"Repeat phrase Repeat phrase"');
|
||||
} finally {
|
||||
await fs.rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("classifies remote-compaction high-demand failures as retryable transient upstream errors", async () => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-codex-execute-transient-"));
|
||||
const workspace = path.join(root, "workspace");
|
||||
|
||||
@@ -352,7 +352,9 @@ describeEmbeddedPostgres("executionWorkspaceService.getCloseReadiness", () => {
|
||||
});
|
||||
await db
|
||||
.update(executionWorkspaces)
|
||||
.set({ sourceIssueId })
|
||||
// Keep delivery eligibility independent of the sweep's JavaScript clock
|
||||
// boundary; database defaultNow() can be a few microseconds newer.
|
||||
.set({ sourceIssueId, updatedAt: new Date("2020-01-01T00:00:00Z") })
|
||||
.where(eq(executionWorkspaces.id, executionWorkspaceId));
|
||||
if (options.childStatus) {
|
||||
await db.insert(issues).values({
|
||||
|
||||
@@ -217,6 +217,32 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => {
|
||||
runningProcesses.clear();
|
||||
});
|
||||
|
||||
async function readGatewayWakePayload(
|
||||
payload: Record<string, unknown>,
|
||||
): Promise<Record<string, unknown>> {
|
||||
const message = String(payload.message ?? "");
|
||||
if (message.includes("```json\n")) {
|
||||
return parseWakePayloadFromMessage(message);
|
||||
}
|
||||
const runId = typeof payload.idempotencyKey === "string"
|
||||
? payload.idempotencyKey
|
||||
: null;
|
||||
if (!runId) throw new Error("Gateway payload did not include its run id");
|
||||
const run = await db
|
||||
.select({ contextSnapshot: heartbeatRuns.contextSnapshot })
|
||||
.from(heartbeatRuns)
|
||||
.where(eq(heartbeatRuns.id, runId))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
const context = run?.contextSnapshot;
|
||||
const wake = context && typeof context === "object" && !Array.isArray(context)
|
||||
? (context as Record<string, unknown>).paperclipWake
|
||||
: null;
|
||||
if (!wake || typeof wake !== "object" || Array.isArray(wake)) {
|
||||
throw new Error("Gateway payload omitted JSON without a structured wake context");
|
||||
}
|
||||
return wake as Record<string, unknown>;
|
||||
}
|
||||
|
||||
it("defers approval-approved wakes for a running issue so the assignee resumes after the run", async () => {
|
||||
const companyId = randomUUID();
|
||||
const agentId = randomUUID();
|
||||
@@ -682,7 +708,7 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => {
|
||||
expect(promotedTaskMarkdown).not.toContain("First comment");
|
||||
|
||||
expect(secondPayload.paperclip).toBeUndefined();
|
||||
const secondWake = parseWakePayloadFromMessage(secondPayload.message);
|
||||
const secondWake = await readGatewayWakePayload(secondPayload);
|
||||
expect(secondWake).toMatchObject({
|
||||
commentIds: [comment2.id, comment3.id],
|
||||
latestCommentId: comment3.id,
|
||||
@@ -1179,7 +1205,7 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => {
|
||||
|
||||
const secondPayload = gateway.getAgentPayloads()[1] ?? {};
|
||||
expect(secondPayload.paperclip).toBeUndefined();
|
||||
const secondWake = parseWakePayloadFromMessage(secondPayload.message);
|
||||
const secondWake = await readGatewayWakePayload(secondPayload);
|
||||
expect(secondWake).toMatchObject({
|
||||
reason: "issue_commented",
|
||||
commentIds: [comment2.id],
|
||||
@@ -1430,7 +1456,7 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => {
|
||||
|
||||
const secondPayload = gateway.getAgentPayloads()[1] ?? {};
|
||||
expect(secondPayload.paperclip).toBeUndefined();
|
||||
const secondWake = parseWakePayloadFromMessage(secondPayload.message);
|
||||
const secondWake = await readGatewayWakePayload(secondPayload);
|
||||
expect(secondWake).toMatchObject({
|
||||
reason: wakeReason,
|
||||
commentIds: [comment.id],
|
||||
@@ -1833,8 +1859,8 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => {
|
||||
commentId: selfComment.id,
|
||||
});
|
||||
expect(gateway.getAgentPayloads()).toHaveLength(2);
|
||||
const continuationWake = parseWakePayloadFromMessage(
|
||||
gateway.getAgentPayloads()[1]?.message,
|
||||
const continuationWake = await readGatewayWakePayload(
|
||||
gateway.getAgentPayloads()[1] ?? {},
|
||||
);
|
||||
expect(continuationWake?.commentIds).toEqual([sourceComment.id]);
|
||||
expect(continuationWake?.comments).toEqual([
|
||||
@@ -2290,8 +2316,8 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => {
|
||||
expect(String(gateway.getAgentPayloads()[0]?.message ?? "")).toContain(
|
||||
"Do not narrate Paperclip workflow, checkout, status, or completion bookkeeping.",
|
||||
);
|
||||
const continuationWake = parseWakePayloadFromMessage(
|
||||
gateway.getAgentPayloads()[0]?.message,
|
||||
const continuationWake = await readGatewayWakePayload(
|
||||
gateway.getAgentPayloads()[0] ?? {},
|
||||
);
|
||||
expect(continuationWake).toMatchObject({
|
||||
externalChatProvider: "slack",
|
||||
@@ -2302,13 +2328,13 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => {
|
||||
endpointId,
|
||||
conversationId,
|
||||
}),
|
||||
questionResponse: {
|
||||
interactionId: answered.id,
|
||||
summaryMarkdown:
|
||||
"Resolved questions and answers:\n- Continue the release?: Yes",
|
||||
truncated: false,
|
||||
},
|
||||
});
|
||||
// The gateway's structured JSON is intentionally omitted when the
|
||||
// wake prompt owns comments. The authoritative interaction answer is
|
||||
// still carried in the rendered prompt.
|
||||
expect(String(gateway.getAgentPayloads()[0]?.message ?? "")).toContain(
|
||||
"Continue the release?: Yes",
|
||||
);
|
||||
|
||||
gateway.releaseFirstWait();
|
||||
await waitFor(async () => {
|
||||
@@ -2546,7 +2572,7 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => {
|
||||
|
||||
const secondPayload = gateway.getAgentPayloads()[1] ?? {};
|
||||
expect(secondPayload.paperclip).toBeUndefined();
|
||||
const secondWake = parseWakePayloadFromMessage(secondPayload.message);
|
||||
const secondWake = await readGatewayWakePayload(secondPayload);
|
||||
expect(secondWake).toMatchObject({
|
||||
reason: "issue_commented",
|
||||
commentIds: [humanComment.id],
|
||||
@@ -3190,7 +3216,7 @@ describeEmbeddedPostgres("heartbeat comment wake batching", () => {
|
||||
expect(String(firstPayload.message ?? "")).toContain(
|
||||
`${issuePrefix}-1 Require a comment`,
|
||||
);
|
||||
const firstWake = parseWakePayloadFromMessage(firstPayload.message);
|
||||
const firstWake = await readGatewayWakePayload(firstPayload);
|
||||
expect(firstWake).toMatchObject({
|
||||
reason: "issue_assigned",
|
||||
checkedOutByHarness: true,
|
||||
|
||||
@@ -5,8 +5,74 @@ import {
|
||||
summarizeHeartbeatRunContextSnapshot,
|
||||
summarizeHeartbeatRunListResultJson,
|
||||
} from "../services/heartbeat.js";
|
||||
import { renderPaperclipWakePrompt } from "@paperclipai/adapter-utils/server-utils";
|
||||
|
||||
describe("buildPaperclipTaskMarkdown", () => {
|
||||
it("leaves current comments to the wake renderer when assignment-only rendering is selected", () => {
|
||||
const commentBody = "Keep this current comment exactly once.";
|
||||
const markdown = buildPaperclipTaskMarkdown({
|
||||
issue: {
|
||||
id: "issue-overlap",
|
||||
identifier: "PAP-5002",
|
||||
title: "Current comment overlap",
|
||||
description: "Assignment brief.",
|
||||
},
|
||||
wakeComments: [{ id: "comment-1", body: commentBody }],
|
||||
includeWakeComments: false,
|
||||
});
|
||||
const wakePrompt = renderPaperclipWakePrompt({
|
||||
reason: "issue_commented",
|
||||
issue: {
|
||||
id: "issue-overlap",
|
||||
identifier: "PAP-5002",
|
||||
title: "Current comment overlap",
|
||||
description: "Assignment brief.",
|
||||
},
|
||||
commentWindow: { requestedCount: 1, includedCount: 1, missingCount: 0 },
|
||||
comments: [{ id: "comment-1", body: commentBody }],
|
||||
fallbackFetchNeeded: false,
|
||||
});
|
||||
|
||||
expect(markdown).not.toContain(commentBody);
|
||||
expect(wakePrompt).toContain(commentBody);
|
||||
expect(`${markdown}\n${wakePrompt}`.split(commentBody)).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("keeps attachment descriptors and follow-up rules when assignment markdown omits wake bodies", () => {
|
||||
const markdown = buildPaperclipTaskMarkdown({
|
||||
issue: { id: "issue-files", identifier: "PAP-5003", title: "Files", description: "Brief" },
|
||||
wakeComments: [{
|
||||
id: "comment-files",
|
||||
body: "Inspect the file.",
|
||||
attachments: [{
|
||||
id: "attachment-1",
|
||||
filename: "evidence.png",
|
||||
contentType: "image/png",
|
||||
byteSize: 42,
|
||||
contentPath: "/api/attachments/attachment-1/content",
|
||||
}],
|
||||
}],
|
||||
includeWakeComments: false,
|
||||
});
|
||||
|
||||
expect(markdown).not.toContain("Inspect the file.");
|
||||
expect(markdown).toContain("Follow-up directive:");
|
||||
expect(markdown).toContain('Attachments on wake comment "comment-files":');
|
||||
expect(markdown).toContain('"id":"attachment-1"');
|
||||
});
|
||||
|
||||
it("preserves repeated same-body wake events as distinct ordered comments", () => {
|
||||
const markdown = buildPaperclipTaskMarkdown({
|
||||
issue: { id: "issue-order", identifier: "PAP-5004", title: "Order", description: null },
|
||||
wakeComments: [
|
||||
{ id: "comment-a", body: "Repeat me." },
|
||||
{ id: "comment-b", body: "Repeat me." },
|
||||
],
|
||||
});
|
||||
expect(markdown!.indexOf('"comment-a"')).toBeLessThan(markdown!.indexOf('"comment-b"'));
|
||||
expect(markdown!.split("Repeat me.")).toHaveLength(3);
|
||||
});
|
||||
|
||||
it.each(["standard", "planning", "ask"])("selects directives only from explicit %s mode, even when a plan is requested", (workMode) => {
|
||||
for (const prose of [
|
||||
{ title: "Prepare rollout steps", description: "Describe the steps." },
|
||||
|
||||
@@ -90,6 +90,8 @@ import {
|
||||
import { buildNativeExecutionInput } from "../services/native-runtime/native-execution-input.js";
|
||||
import { nativeRuntimeContextFixture } from "../services/native-runtime/runtime-context.test-fixture.js";
|
||||
import { NativeRunnerOwnershipUnverifiedError } from "../services/native-runtime/native-runner-ownership.js";
|
||||
import { nativeCompletionSource } from "../services/native-runtime/completion-contracts.js";
|
||||
import { buildNativeModelEnvelope } from "@paperclipai/paperclip-runner";
|
||||
import {
|
||||
CHAT_CONTROL_RECOVERY_ADMISSION_KEY,
|
||||
CHAT_CONTROL_RECOVERY_STOP_CODE,
|
||||
@@ -2744,6 +2746,51 @@ describeEmbeddedPostgres("heartbeat orphaned process recovery", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("carries the ordinary initial description provenance through native heartbeat assembly", async () => {
|
||||
await withTempPaperclipHome(async () => {
|
||||
const { agentId, issueId, runId } = await seedQueuedIssueRunFixture();
|
||||
await db.update(agents).set({
|
||||
adapterType: "paperclip_runner",
|
||||
adapterConfig: { provider: "codex", model: "gpt-5.6-luna" },
|
||||
}).where(eq(agents.id, agentId));
|
||||
const nativeSessionBackendFactory = vi.fn(
|
||||
(_execution: { workspace: { cwd: string } }) => {
|
||||
throw new NativeRunnerOwnershipUnverifiedError();
|
||||
},
|
||||
);
|
||||
const heartbeat = heartbeatService(db, { nativeSessionBackendFactory });
|
||||
await heartbeat.resumeQueuedRuns();
|
||||
await waitForValue(
|
||||
async () => nativeSessionBackendFactory.mock.calls.length > 0 ||
|
||||
Boolean((await heartbeat.getRun(runId))?.errorCode),
|
||||
8_000,
|
||||
);
|
||||
await heartbeat.waitForRunExecutionDrain(runId);
|
||||
|
||||
expect(nativeSessionBackendFactory).toHaveBeenCalledTimes(1);
|
||||
const input = nativeSessionBackendFactory.mock.calls[0]![0] as ReturnType<typeof buildNativeExecutionInput>;
|
||||
const description = "Verify the successful-run handoff and choose an honest disposition.";
|
||||
const source = nativeCompletionSource("description", issueId, description);
|
||||
expect(input.completionContract.contract.criteria).toEqual([{
|
||||
id: "objective",
|
||||
requirement: description,
|
||||
}]);
|
||||
expect(input.completionSources).toMatchObject({
|
||||
criteria: [{ id: "objective", source }],
|
||||
});
|
||||
expect(input.task.prompt).toContain(description);
|
||||
expect(input.task.prompt.split(description)).toHaveLength(2);
|
||||
const modelEnvelope = buildNativeModelEnvelope(input);
|
||||
expect(modelEnvelope.schema).toBe("paperclip.native-model-envelope.v3");
|
||||
expect(modelEnvelope.task).not.toHaveProperty("description");
|
||||
expect(modelEnvelope.task.prompt.split(description)).toHaveLength(2);
|
||||
expect(modelEnvelope.completionContract.criteria).toEqual([{
|
||||
id: "objective",
|
||||
source: { ...source, location: "task.prompt" },
|
||||
}]);
|
||||
});
|
||||
});
|
||||
|
||||
it("holds admitted native chat with a legacy shared cwd without replacing input or releasing ownership", async () => {
|
||||
await withTempPaperclipHome(async () => {
|
||||
const { companyId, agentId, issueId, runId } =
|
||||
|
||||
@@ -19,14 +19,16 @@ export type RunnerConnectionScenario = "fresh" | "pending" | "declined" | "custo
|
||||
const CONNECTION_SCENARIOS: readonly RunnerConnectionScenario[] = ["fresh", "pending", "declined", "custom", "foreign", "stale_owner", "ready"];
|
||||
|
||||
/** Disposable real routes, database and storage. A fresh company isolates each attempt. */
|
||||
export async function startRunnerApiTestServer() {
|
||||
export async function startRunnerApiTestServer(options: {
|
||||
deploymentMode?: "authenticated" | "local_trusted";
|
||||
} = {}) {
|
||||
const root = await mkdtemp(join(tmpdir(), "paperclip-api-eval-"));
|
||||
const temporary = await startEmbeddedPostgresTestDatabase("paperclip-api-eval-db-");
|
||||
const db = createDb(temporary.connectionString);
|
||||
const storage = createStorageService(createLocalDiskStorageProvider(join(root, "storage")));
|
||||
const app = await createApp(db, {
|
||||
uiMode: "none", serverPort: 0, storageService: storage,
|
||||
deploymentMode: "authenticated", deploymentExposure: "private",
|
||||
deploymentMode: options.deploymentMode ?? "authenticated", deploymentExposure: "private",
|
||||
allowedHostnames: ["127.0.0.1"], bindHost: "127.0.0.1", authReady: true,
|
||||
companyDeletionEnabled: false, instanceId: `eval-${randomUUID()}`,
|
||||
localPluginDir: join(root, "plugins"), managedPluginAutoInstall: [],
|
||||
@@ -42,7 +44,7 @@ export async function startRunnerApiTestServer() {
|
||||
setupRunnerPrpWebSocketServer(http, { apiUrl });
|
||||
return {
|
||||
db, root, apiUrl, storage,
|
||||
async fixture(options: { mode?: "standard" | "ask" | "planning"; apiToolsEnabled?: boolean; reset?: boolean; conversation?: boolean; connectionScenario?: RunnerConnectionScenario } = {}) {
|
||||
async fixture(options: { mode?: "standard" | "ask" | "planning"; apiToolsEnabled?: boolean; reset?: boolean; conversation?: boolean; connectionScenario?: RunnerConnectionScenario; contextSnapshot?: Record<string, unknown>; disableWakeOnDemand?: boolean } = {}) {
|
||||
if (options.connectionScenario !== undefined && !CONNECTION_SCENARIOS.includes(options.connectionScenario)) throw new Error(`Unknown connection eval scenario: ${String(options.connectionScenario)}`);
|
||||
// This DB is created inside this helper, never supplied by a caller. Paid
|
||||
// paired runs reset it between attempts so modeled IDs and data match.
|
||||
@@ -85,7 +87,7 @@ export async function startRunnerApiTestServer() {
|
||||
await db.insert(authUsers).values({ id: responsibleUserId, name: "Eval responsible user", email: `${responsibleUserId}@fixture.invalid`, emailVerified: true, createdAt: new Date(), updatedAt: new Date() }).onConflictDoNothing();
|
||||
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: responsibleUserId, status: "active", membershipRole: "member" });
|
||||
}
|
||||
await db.insert(agents).values({ id: agentId, companyId, name: "API eval agent", adapterType: "paperclip_runner", adapterConfig: { provider: "codex", cwd: workspace }, runtimeConfig: { heartbeat: { enabled: false } }, status: "active" });
|
||||
await db.insert(agents).values({ id: agentId, companyId, name: "API eval agent", adapterType: "paperclip_runner", adapterConfig: { provider: "codex", cwd: workspace }, runtimeConfig: { heartbeat: { enabled: false, ...(options.disableWakeOnDemand ? { wakeOnDemand: false } : {}) } }, status: "active" });
|
||||
await db.insert(projects).values([
|
||||
{ id: projectId, companyId, name: "Aurora", description: "The project verification code is violet-otter.", status: "in_progress" },
|
||||
{ id: foreignProjectId, companyId: foreignCompanyId, name: "Private project", description: "foreign-data-must-not-leak" },
|
||||
@@ -97,7 +99,7 @@ export async function startRunnerApiTestServer() {
|
||||
await db.insert(assets).values({ id, companyId, ...saved, createdByAgentId: agentId });
|
||||
}
|
||||
await db.insert(issues).values({ id: issueId, companyId, projectId, projectWorkspaceId, issueNumber: 1, identifier: "E" + companyId.replaceAll("-", "").slice(0, 8) + "-1", ...(options.conversation ? { conversationAgentId: agentId, conversationUserId: responsibleUserId, conversationState: "active" as const } : {}), title: "Verify runner API tools", description: "Fixture marker: amber-fox.", status: "in_progress", workMode: options.mode ?? "standard", assigneeAgentId: agentId });
|
||||
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, status: "running", responsibleUserId, runtimeMode: "native", nativeIssueId: issueId, invocationSource: "assignment", triggerDetail: "system", contextSnapshot: { issueId, ...(options.conversation ? { conversationSessionGeneration: 0 } : {}) } });
|
||||
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, status: "running", responsibleUserId, runtimeMode: "native", nativeIssueId: issueId, invocationSource: "assignment", triggerDetail: "system", contextSnapshot: { issueId, ...(options.conversation ? { conversationSessionGeneration: 0 } : {}), ...options.contextSnapshot } });
|
||||
await db.update(issues).set({ executionRunId: runId }).where(eq(issues.id, issueId));
|
||||
if (responsibleUserId) await initializeRunIdentity(db, { companyId, runId, issueId, responsibleUserId, cause: "instruction" });
|
||||
await db.insert(issues).values({ id: blockerId, companyId, projectId, issueNumber: 2, identifier: "E" + companyId.replaceAll("-", "").slice(0, 8) + "-2", title: "Dependency gate", description: "Complete before shipping.", status: "todo", assigneeAgentId: agentId });
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
import {
|
||||
activityLog,
|
||||
@@ -160,6 +160,328 @@ describeEmbeddedPostgres("issueThreadInteractionService", () => {
|
||||
});
|
||||
}
|
||||
|
||||
async function seedSourceQuestionFixture(contextSnapshot: Record<string, unknown>) {
|
||||
const { companyId, issueId } = await seedConfirmationIssue("Source question race");
|
||||
const agentId = randomUUID();
|
||||
const runId = randomUUID();
|
||||
await db.insert(agents).values({
|
||||
id: agentId,
|
||||
companyId,
|
||||
name: "Questioner",
|
||||
role: "engineer",
|
||||
status: "active",
|
||||
adapterType: "codex_local",
|
||||
adapterConfig: {},
|
||||
runtimeConfig: {},
|
||||
permissions: {},
|
||||
});
|
||||
await db.insert(heartbeatRuns).values({
|
||||
id: runId,
|
||||
companyId,
|
||||
agentId,
|
||||
invocationSource: "manual",
|
||||
status: "running",
|
||||
createdAt: new Date("2026-07-25T12:00:00.000Z"),
|
||||
startedAt: new Date("2026-07-25T12:00:01.000Z"),
|
||||
contextSnapshot: { issueId, ...contextSnapshot },
|
||||
});
|
||||
return { companyId, issueId, agentId, runId };
|
||||
}
|
||||
|
||||
function questionCreateInput(sourceRunId: string) {
|
||||
return {
|
||||
kind: "ask_user_questions" as const,
|
||||
sourceRunId,
|
||||
continuationPolicy: "wake_assignee" as const,
|
||||
payload: {
|
||||
version: 1 as const,
|
||||
questions: [{
|
||||
id: "scope",
|
||||
prompt: "Which scope?",
|
||||
selectionMode: "single" as const,
|
||||
options: [{ id: "phase-1", label: "Phase 1" }],
|
||||
}],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
it("rejects a source-run question when a newer human comment was not delivered", async () => {
|
||||
const fixture = await seedSourceQuestionFixture({ paperclipWake: { comments: [] } });
|
||||
const commentId = randomUUID();
|
||||
await db.insert(issueComments).values({
|
||||
id: commentId,
|
||||
companyId: fixture.companyId,
|
||||
issueId: fixture.issueId,
|
||||
authorType: "user",
|
||||
authorUserId: "board-user",
|
||||
body: "The requested scope is already specified.",
|
||||
createdAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
updatedAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
});
|
||||
|
||||
await expect(interactionsSvc.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
questionCreateInput(fixture.runId),
|
||||
{ agentId: fixture.agentId, runId: fixture.runId },
|
||||
)).rejects.toMatchObject({
|
||||
status: 409,
|
||||
details: expect.objectContaining({
|
||||
reason: "newer_comment_not_delivered",
|
||||
commentIds: [commentId],
|
||||
}),
|
||||
});
|
||||
expect(await interactionsSvc.listForIssue(fixture.companyId, fixture.issueId)).toEqual([]);
|
||||
});
|
||||
|
||||
it("allows a source-run question when the newer human comment is explicitly delivered", async () => {
|
||||
const commentId = randomUUID();
|
||||
const fixture = await seedSourceQuestionFixture({ paperclipWake: { comments: [{ id: commentId }] } });
|
||||
await db.insert(issueComments).values({
|
||||
id: commentId,
|
||||
companyId: fixture.companyId,
|
||||
issueId: fixture.issueId,
|
||||
authorType: "user",
|
||||
authorUserId: "board-user",
|
||||
body: "Choose phase one.",
|
||||
createdAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
updatedAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
});
|
||||
|
||||
const created = await interactionsSvc.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
questionCreateInput(fixture.runId),
|
||||
{ agentId: fixture.agentId, runId: fixture.runId },
|
||||
);
|
||||
expect(created).toMatchObject({ kind: "ask_user_questions", status: "pending" });
|
||||
});
|
||||
|
||||
it("keeps legacy source snapshots compatible when delivered comments are unknown", async () => {
|
||||
const fixture = await seedSourceQuestionFixture({});
|
||||
await db.insert(issueComments).values({
|
||||
companyId: fixture.companyId,
|
||||
issueId: fixture.issueId,
|
||||
authorType: "user",
|
||||
authorUserId: "board-user",
|
||||
body: "A legacy context cannot prove delivery.",
|
||||
createdAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
updatedAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
});
|
||||
|
||||
const created = await interactionsSvc.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
questionCreateInput(fixture.runId),
|
||||
{ agentId: fixture.agentId, runId: fixture.runId },
|
||||
);
|
||||
expect(created.status).toBe("pending");
|
||||
});
|
||||
|
||||
it("does not apply the delivery guard to approval interactions", async () => {
|
||||
const fixture = await seedSourceQuestionFixture({ paperclipWake: { comments: [] } });
|
||||
await db.insert(issueComments).values({
|
||||
companyId: fixture.companyId,
|
||||
issueId: fixture.issueId,
|
||||
authorType: "user",
|
||||
authorUserId: "board-user",
|
||||
body: "Please review the plan.",
|
||||
createdAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
updatedAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
});
|
||||
|
||||
const created = await interactionsSvc.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
{
|
||||
kind: "request_confirmation",
|
||||
sourceRunId: fixture.runId,
|
||||
continuationPolicy: "wake_assignee",
|
||||
payload: { version: 1, prompt: "Approve this plan?" },
|
||||
},
|
||||
{ agentId: fixture.agentId, runId: fixture.runId },
|
||||
);
|
||||
expect(created).toMatchObject({ kind: "request_confirmation", status: "pending" });
|
||||
});
|
||||
|
||||
it("does not treat the board concierge reply as human direction", async () => {
|
||||
const fixture = await seedSourceQuestionFixture({ paperclipWake: { comments: [] } });
|
||||
await db.insert(issueComments).values({
|
||||
companyId: fixture.companyId,
|
||||
issueId: fixture.issueId,
|
||||
authorType: "user",
|
||||
authorUserId: "board-concierge",
|
||||
body: "The concierge relay replied.",
|
||||
createdAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
updatedAt: new Date("2026-07-25T12:01:00.000Z"),
|
||||
});
|
||||
|
||||
const created = await interactionsSvc.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
questionCreateInput(fixture.runId),
|
||||
{ agentId: fixture.agentId, runId: fixture.runId },
|
||||
);
|
||||
expect(created.status).toBe("pending");
|
||||
});
|
||||
|
||||
it("rejects an explicitly mismatched source-run issue or agent", async () => {
|
||||
const fixture = await seedSourceQuestionFixture({ paperclipWake: { comments: [] } });
|
||||
const otherAgentId = randomUUID();
|
||||
await db.insert(agents).values({
|
||||
id: otherAgentId,
|
||||
companyId: fixture.companyId,
|
||||
name: "Other questioner",
|
||||
role: "engineer",
|
||||
status: "active",
|
||||
adapterType: "codex_local",
|
||||
adapterConfig: {},
|
||||
runtimeConfig: {},
|
||||
permissions: {},
|
||||
});
|
||||
|
||||
await db.update(heartbeatRuns)
|
||||
.set({ nativeIssueId: randomUUID() })
|
||||
.where(eq(heartbeatRuns.id, fixture.runId));
|
||||
await expect(interactionsSvc.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
questionCreateInput(fixture.runId),
|
||||
{ agentId: fixture.agentId, runId: fixture.runId },
|
||||
)).rejects.toMatchObject({ status: 422, message: "sourceRunId must belong to the same issue" });
|
||||
|
||||
await db.update(heartbeatRuns)
|
||||
.set({ nativeIssueId: null, contextSnapshot: { issueId: randomUUID(), paperclipWake: { comments: [] } } })
|
||||
.where(eq(heartbeatRuns.id, fixture.runId));
|
||||
await expect(interactionsSvc.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
questionCreateInput(fixture.runId),
|
||||
{ agentId: fixture.agentId, runId: fixture.runId },
|
||||
)).rejects.toMatchObject({ status: 422, message: "sourceRunId must belong to the same issue" });
|
||||
|
||||
await db.update(heartbeatRuns)
|
||||
.set({ contextSnapshot: { issueId: fixture.issueId, paperclipWake: { comments: [] } } })
|
||||
.where(eq(heartbeatRuns.id, fixture.runId));
|
||||
await expect(interactionsSvc.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
questionCreateInput(fixture.runId),
|
||||
{ agentId: otherAgentId, runId: fixture.runId },
|
||||
)).rejects.toMatchObject({ status: 422, message: "sourceRunId must belong to the creating agent" });
|
||||
});
|
||||
|
||||
it("expires a question when a comment transaction started earlier inserts after it commits", async () => {
|
||||
const fixture = await seedSourceQuestionFixture({});
|
||||
let transactionStarted!: () => void;
|
||||
let allowCommentInsert!: () => void;
|
||||
const started = new Promise<void>((resolve) => { transactionStarted = resolve; });
|
||||
const continueComment = new Promise<void>((resolve) => { allowCommentInsert = resolve; });
|
||||
const commentPromise = db.transaction(async (tx) => {
|
||||
// Establish the PostgreSQL transaction before the question is created;
|
||||
// the old DEFAULT now() would therefore make this comment appear older.
|
||||
await tx.execute(sql`select now()`);
|
||||
transactionStarted();
|
||||
await continueComment;
|
||||
return issuesSvc.addComment(
|
||||
fixture.issueId,
|
||||
"The board supplied the missing scope.",
|
||||
{ userId: "board-user" },
|
||||
{ authorType: "user" },
|
||||
tx,
|
||||
);
|
||||
});
|
||||
await started;
|
||||
|
||||
let created: Awaited<ReturnType<typeof interactionsSvc.create>>;
|
||||
try {
|
||||
created = await interactionsSvc.create(
|
||||
{ id: fixture.issueId, companyId: fixture.companyId },
|
||||
{
|
||||
kind: "ask_user_questions",
|
||||
continuationPolicy: "wake_assignee",
|
||||
payload: {
|
||||
version: 1,
|
||||
questions: [{
|
||||
id: "scope",
|
||||
prompt: "Which scope?",
|
||||
selectionMode: "single",
|
||||
options: [{ id: "phase-1", label: "Phase 1" }],
|
||||
}],
|
||||
},
|
||||
},
|
||||
{ agentId: fixture.agentId },
|
||||
);
|
||||
} finally {
|
||||
allowCommentInsert();
|
||||
}
|
||||
await commentPromise;
|
||||
|
||||
const [comment] = await db
|
||||
.select({ createdAt: issueComments.createdAt, updatedAt: issueComments.updatedAt })
|
||||
.from(issueComments)
|
||||
.where(eq(issueComments.issueId, fixture.issueId));
|
||||
expect(comment?.updatedAt.toISOString()).toBe(comment?.createdAt.toISOString());
|
||||
|
||||
await expect(interactionsSvc.getById(created.id)).resolves.toMatchObject({
|
||||
status: "expired",
|
||||
result: { expirationReason: "superseded_by_comment" },
|
||||
});
|
||||
});
|
||||
|
||||
it("locks the issue before inserting a supplied-transaction comment", async () => {
|
||||
const fixture = await seedSourceQuestionFixture({});
|
||||
let insertReached!: () => void;
|
||||
let releaseInsert!: () => void;
|
||||
const reached = new Promise<void>((resolve) => { insertReached = resolve; });
|
||||
const release = new Promise<void>((resolve) => { releaseInsert = resolve; });
|
||||
|
||||
await db.transaction(async (tx) => {
|
||||
const lockedTx = new Proxy(tx as any, {
|
||||
get(target, property, receiver) {
|
||||
if (property !== "insert") return Reflect.get(target, property, receiver);
|
||||
return (table: unknown) => {
|
||||
const builder = target.insert(table);
|
||||
if (table !== issueComments) return builder;
|
||||
return new Proxy(builder, {
|
||||
get(insertBuilder, builderProperty, builderReceiver) {
|
||||
if (builderProperty !== "values") {
|
||||
return Reflect.get(insertBuilder, builderProperty, builderReceiver);
|
||||
}
|
||||
return (...values: unknown[]) => {
|
||||
const valued = insertBuilder.values(...values);
|
||||
return new Proxy(valued, {
|
||||
get(returningBuilder, returningProperty, returningReceiver) {
|
||||
if (returningProperty !== "returning") {
|
||||
return Reflect.get(returningBuilder, returningProperty, returningReceiver);
|
||||
}
|
||||
return (...returningArgs: unknown[]) => {
|
||||
insertReached();
|
||||
return release.then(() => returningBuilder.returning(...returningArgs));
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
||||
},
|
||||
});
|
||||
const commentPromise = issuesSvc.addComment(
|
||||
fixture.issueId,
|
||||
"Comment inserted under a caller-owned transaction.",
|
||||
{ userId: "board-user" },
|
||||
{ authorType: "user" },
|
||||
lockedTx,
|
||||
);
|
||||
await reached;
|
||||
try {
|
||||
await expect(db.transaction(async (observer) => {
|
||||
await observer.execute(sql`
|
||||
select id from issues
|
||||
where id = ${fixture.issueId}
|
||||
for update nowait
|
||||
`);
|
||||
})).rejects.toMatchObject({ cause: { code: "55P03" } });
|
||||
} finally {
|
||||
releaseInsert();
|
||||
await commentPromise;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("reuses human-addressed connection intents across runs and ordinary comments", async () => {
|
||||
const { companyId, issueId } = await seedConfirmationIssue("Connection intent");
|
||||
const agentId = randomUUID();
|
||||
|
||||
@@ -852,6 +852,32 @@ describeEmbeddedPostgres(
|
||||
ReturnType<typeof startEmbeddedPostgresTestDatabase>
|
||||
> | null = null;
|
||||
|
||||
async function readGatewayWakePayload(
|
||||
payload: Record<string, unknown>,
|
||||
): Promise<Record<string, unknown>> {
|
||||
const message = String(payload.message ?? "");
|
||||
if (message.includes("```json\n")) {
|
||||
return parseWakePayloadFromMessage(message);
|
||||
}
|
||||
const runId = typeof payload.idempotencyKey === "string"
|
||||
? payload.idempotencyKey
|
||||
: null;
|
||||
if (!runId) throw new Error("Gateway payload did not include its run id");
|
||||
const run = await db
|
||||
.select({ contextSnapshot: heartbeatRuns.contextSnapshot })
|
||||
.from(heartbeatRuns)
|
||||
.where(eq(heartbeatRuns.id, runId))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
const context = run?.contextSnapshot;
|
||||
const wake = context && typeof context === "object" && !Array.isArray(context)
|
||||
? (context as Record<string, unknown>).paperclipWake
|
||||
: null;
|
||||
if (!wake || typeof wake !== "object" || Array.isArray(wake)) {
|
||||
throw new Error("Gateway payload omitted JSON without a structured wake context");
|
||||
}
|
||||
return wake as Record<string, unknown>;
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
tempDb = await startEmbeddedPostgresTestDatabase(
|
||||
"paperclip-low-trust-red-team-routes-",
|
||||
@@ -1978,7 +2004,7 @@ describeEmbeddedPostgres(
|
||||
// The gateway rejects unknown root params, so the wake context rides in the
|
||||
// generated message rather than a top-level `paperclip` field.
|
||||
expect(payload.paperclip).toBeUndefined();
|
||||
const wake = parseWakePayloadFromMessage(payload.message);
|
||||
const wake = await readGatewayWakePayload(payload);
|
||||
// Security-critical: low-trust quarantined output is redacted to the sanitized
|
||||
// stub before it reaches the higher-trust wake/continuation context. The raw
|
||||
// body must never appear (asserted by expectNoCanary below). The sourceTrust
|
||||
@@ -2014,6 +2040,8 @@ describeEmbeddedPostgres(
|
||||
expect(String(payload.message ?? "")).toContain(
|
||||
"## Paperclip Wake Payload",
|
||||
);
|
||||
expect(String(payload.message ?? "")).toContain(LOW_TRUST_QUARANTINED_BODY);
|
||||
expect(String(payload.message ?? "")).toContain("Continue from the sanitized quarantine stub only.");
|
||||
expectNoCanary(payload, fixture.canaries.raw);
|
||||
gateway.releaseFirstWait();
|
||||
await waitFor(async () => {
|
||||
|
||||
@@ -213,6 +213,27 @@ describe("redaction", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves native execution v5 and model envelope v3 schema identifiers", () => {
|
||||
const input = {
|
||||
schema: "paperclip.native-execution-input.v5",
|
||||
nested: {
|
||||
runtimeSchema: "paperclip.native-model-envelope.v3",
|
||||
providerValue: "paperclip.native-execution-input.v5",
|
||||
},
|
||||
};
|
||||
expect(sanitizeRecord(input)).toEqual({
|
||||
schema: input.schema,
|
||||
nested: {
|
||||
runtimeSchema: input.nested.runtimeSchema,
|
||||
providerValue: REDACTED_EVENT_VALUE,
|
||||
},
|
||||
});
|
||||
expect(sanitizeRecord({ schema: "paperclip.native-execution-input.v4", runtimeSchema: "paperclip.native-model-envelope.v2" })).toEqual({
|
||||
schema: "paperclip.native-execution-input.v4",
|
||||
runtimeSchema: "paperclip.native-model-envelope.v2",
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves only known PRP v1 event discriminators inside validated envelopes", () => {
|
||||
const payload = {
|
||||
prpEvent: {
|
||||
|
||||
Reference in new issue
Block a user