From d4b7ae55322afefb3e082922ab440478779cbc93 Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Wed, 18 Nov 2026 16:20:17 -0800 Subject: [PATCH] fix(tasks): require merged PR deliverables before native completion (#22244) Keep native tasks with declared pull request deliverables active until GitHub verifies every deliverable is merged. Preserve delivery guidance in follow-up contracts and recheck the current outputs under task locks. Use bounded incomplete-work feedback during reassessment, including for saved non-Done reports. Serialize work-product restoration and identity changes with completion. Preserve configured reviews and conversations. Add regression coverage and repair the CI fixtures exposed during qualification. All CI checks pass on afee6301201. Greptile is 5/5 with no unresolved review threads. Co-Authored-By: Paperclip --- doc/architecture/native-status-arbitration.md | 35 ++++ scripts/ci/stalled-file-reporter.test.mjs | 20 +- .../native-status-arbiter-corpus.test.ts | 89 +++++++++ .../src/__tests__/run-start-handover.test.ts | 8 +- server/src/__tests__/work-products.test.ts | 10 +- .../completion-contracts.test.ts | 12 +- .../native-runtime/completion-contracts.ts | 5 +- .../native-completion-feedback.test.ts | 180 +++++++++++++++++- .../native-completion-feedback.ts | 3 +- .../native-deliverable-feedback.ts | 18 +- ...tive-execution-policy-review-stage.test.ts | 20 +- .../native-pull-request-completion.ts | 87 +++++++++ .../native-runtime/native-run-finalizer.ts | 3 +- .../native-status-reassessment.ts | 35 +++- .../status-decision-committer.ts | 11 +- server/src/services/work-products.ts | 22 ++- .../planning-mode-visual-verification.spec.ts | 10 +- 17 files changed, 536 insertions(+), 32 deletions(-) create mode 100644 server/src/services/native-runtime/native-pull-request-completion.ts diff --git a/doc/architecture/native-status-arbitration.md b/doc/architecture/native-status-arbitration.md index 93ba0a32f1e..85843264c4a 100644 --- a/doc/architecture/native-status-arbitration.md +++ b/doc/architecture/native-status-arbitration.md @@ -123,6 +123,41 @@ Under that policy, a result can complete the issue when: This produces `completion_claim_policy_accepted`. Independently governed tools and effects still enforce their own authorization and approval rules. +### Pull request deliverables + +For repository implementation work, native task contracts retain delivery +obligations across follow-up comments. Agents register their delivery PR as a +`pull_request` work product and include its URL in result `artifacts`. Creating +a local commit or opening a PR does not complete that delivery. + +Before accepting Done, the server verifies each non-archived PR work product and +GitHub PR artifact with the company-scoped GitHub resolver. Only a verified merge +passes. Agent-written `status` and `metadata.state` values cannot prove a merge. +Open, draft, closed without merge, unavailable, and malformed PR deliverables +keep the task open. The finish tool returns corrective feedback; finalization +uses its existing bounded incomplete-work continuation. The agent can fix review +or CI failures, use the existing monitor for an external wait, or report a +concrete blocker. This creates no generic human completion approval. + +Reassessment uses the same bounded incomplete-delivery continuation as initial +finalization. A stable unmerged PR does not create a status-race retry deadline. +The proposed server status controls delivery validation, including when new +evidence completes a result that previously reported another disposition. +The transactional commit also checks these outputs, including reassessment +paths. Human and agent work-product updates lock their source and destination +tasks in a stable order before changing status or identity. Provider reads occur +before the transaction, with at most twenty PRs and a shared ten-second abort +signal for GitHub requests. The locked transaction requires merge proof +for every current deliverable, so a changed output requires a new assessment. +There is no new polling worker or schema. Existing saved contracts remain valid; +new contracts include the delivery guidance and existing unfinished results use +the server guard. Already terminal tasks are preserved. + +Entering a configured review stage keeps the task open and does not require a +merge first. Conversation responses and server-assigned completion-review runs retain their +own lifecycle. PR URLs mentioned only in prose as review input or background +are not declared deliverables and do not create a merge obligation. + ## Decision order `arbitrateNativeStatus()` is a pure function. It evaluates higher-authority diff --git a/scripts/ci/stalled-file-reporter.test.mjs b/scripts/ci/stalled-file-reporter.test.mjs index 840cf6d5e6f..ab9e58b70da 100644 --- a/scripts/ci/stalled-file-reporter.test.mjs +++ b/scripts/ci/stalled-file-reporter.test.mjs @@ -3,7 +3,7 @@ import { spawnSync } from "node:child_process"; import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; -import { fileURLToPath } from "node:url"; +import { fileURLToPath, pathToFileURL } from "node:url"; import test from "node:test"; import StalledFileReporter, { defaultStallMs, stallAnnotation, stalledFileReporterEnabled, stallLimitMs } from "./stalled-file-reporter.mjs"; @@ -83,9 +83,9 @@ test("a file that never finishes loading is named from the moment it is queued", // The failure this guards: a test awaiting a promise that never settles, with // a test timeout longer than the CI job, holds Vitest until the job is // cancelled and never prints the file's name. -for (const [label, body, step] of [ - ["a test that never settles", 'import { test } from "vitest";\ntest("waits forever", () => new Promise(() => {}), 600_000);\n', 'test "waits forever"'], - ["a top-level await that never settles", 'import { test } from "vitest";\nawait new Promise(() => {});\ntest("never collected", () => {});\n', "loading the file"], +for (const [label, body, steps] of [ + ["a test that never settles", 'import { test } from "vitest";\nimport { writeFileSync } from "node:fs";\ntest("waits forever", () => { writeFileSync(process.env.PAPERCLIP_WATCHDOG_TEST_STARTED, "started"); return new Promise(() => {}); }, 600_000);\n', ['test "waits forever"']], + ["a top-level await that never settles", 'import { test } from "vitest";\nawait new Promise(() => {});\ntest("never collected", () => {});\n', ["loading the file"]], ]) test(`a real Vitest run with ${label} fails fast and names the file`, () => { const directory = realpathSync(mkdtempSync(path.join(tmpdir(), "stalled-file-reporter-"))); try { @@ -93,21 +93,27 @@ for (const [label, body, step] of [ writeFileSync(path.join(directory, "quick.test.mjs"), `import { test } from "vitest"; test("passes", () => {}); `); + const startedMarker = path.join(directory, "started"); + let fixtureReporter = reporterPath; + if (label === "a test that never settles") { + fixtureReporter = path.join(directory, "after-test-start.mjs"); + writeFileSync(fixtureReporter, 'import Base from ' + JSON.stringify(pathToFileURL(reporterPath).href) + ';\nimport { existsSync } from "node:fs";\nexport default class extends Base { check() { if (!existsSync(' + JSON.stringify(startedMarker) + ')) return; super.check(); } }\n'); + } const configFile = path.join(directory, "vitest.config.mjs"); writeFileSync(configFile, `export default ${JSON.stringify({ test: { include: ["*.test.mjs"], - reporters: ["default", [reporterPath, { limitMs: 1500, graceMs: 5000, root: directory }]] } })};`); + reporters: ["default", [fixtureReporter, { limitMs: 1500, graceMs: 5000, root: directory }]] } })};`); const started = Date.now(); const result = spawnSync(process.execPath, [path.join(root, "node_modules/vitest/vitest.mjs"), "run", "--config", configFile, "--root", directory], { cwd: directory, encoding: "utf8", timeout: 60_000, - env: { ...process.env, GITHUB_ACTIONS: "", PAPERCLIP_VITEST_FILE_STALL_MS: "" }, + env: { ...process.env, GITHUB_ACTIONS: "", PAPERCLIP_VITEST_FILE_STALL_MS: "", PAPERCLIP_WATCHDOG_TEST_STARTED: startedMarker }, }); const output = `${result.stdout}\n${result.stderr}`; assert.equal(result.signal, null, `Vitest was still running after 60 s:\n${output}`); assert.notEqual(result.status, 0, output); assert.ok(Date.now() - started < 30_000, `took ${Date.now() - started} ms:\n${output}`); assert.match(output, /::error file=stuck\.test\.mjs,title=Test file stopped making progress::stuck\.test\.mjs made no progress/); - assert.ok(output.includes(`while in: ${step}.`), output); + assert.ok(steps.some(step => output.includes(`while in: ${step}.`)), output); } finally { rmSync(directory, { recursive: true, force: true }); } diff --git a/server/src/__tests__/native-status-arbiter-corpus.test.ts b/server/src/__tests__/native-status-arbiter-corpus.test.ts index f0701e88be0..507ba04b11c 100644 --- a/server/src/__tests__/native-status-arbiter-corpus.test.ts +++ b/server/src/__tests__/native-status-arbiter-corpus.test.ts @@ -1,3 +1,4 @@ +import * as pullRequestMerge from "../services/github-pull-request-merge.js"; import { retainedNativeSessionCleanupWork } from "../services/native-runtime/retained-native-session-cleanup-work.js"; import { pendingNativeGovernance } from "../services/native-runtime/native-governance.js"; import { withNativeWorkspaceFinalizationOwnership } from "../services/native-runtime/native-workspace-finalization-ownership.js"; @@ -2919,6 +2920,59 @@ describe("P6-31 Section 18.13 executable status-authority corpus", () => { expect(await db.select().from(statusDecisions).where(eq(statusDecisions.issueId, seeded.issueId))).toHaveLength(2); }); + it.each([ + { trigger: "retired review", correctiveUsed: false, disposition: "done" }, + { trigger: "new evidence", correctiveUsed: false, disposition: "done" }, + { trigger: "retired review", correctiveUsed: true, disposition: "done" }, + { trigger: "new evidence", correctiveUsed: true, disposition: "done" }, + ...["needs_review", "yielded", "blocked"].flatMap(disposition => [false, true].map(correctiveUsed => + ({ trigger: "new evidence", correctiveUsed, disposition }))), + ])("settles unmerged delivery from $trigger without retry polling (disposition=$disposition, corrective used=$correctiveUsed)", async ({ trigger, correctiveUsed, disposition }) => { + const seeded = await seedAutomaticReview(); + const [saved] = await db.select().from(nativeRunResults).where(eq(nativeRunResults.id, seeded.resultId!)); + await db.update(nativeRunResults).set({ resultJson: { ...saved.resultJson, + result: { ...(saved.resultJson.result as object), reportedWorkDisposition: disposition } } }) + .where(eq(nativeRunResults.id, saved.id)); + const [originalAssessment] = await db.select().from(workAssessments).where(eq(workAssessments.id, seeded.assessmentId)); + await db.update(workAssessments).set({ createdAt: new Date(Date.now() - 2000), + assessmentJson: { ...originalAssessment.assessmentJson, allCriteriaSatisfied: false } }).where(eq(workAssessments.id, seeded.assessmentId)); + await db.insert(issueWorkProducts).values({ companyId, issueId: seeded.issueId, type: "pull_request", + provider: "github", title: "Closed implementation PR", status: "closed", url: "https://github.com/example/project/pull/42", + createdAt: new Date(Date.now() - 1000), updatedAt: new Date(Date.now() - 1000) }); + if (trigger === "retired review") await dismissAutomaticCompletionReviews(db, seeded.issueId); + else await db.update(issueThreadInteractions).set({ status: "cancelled", result: { version: 1, outcome: "withdrawn" } }) + .where(eq(issueThreadInteractions.id, seeded.interaction.id)); + if (correctiveUsed) { + const [wake] = await db.insert(agentWakeupRequests).values({ companyId, agentId, source: "automation", + triggerDetail: "system", reason: "issue_status_changed", status: "consumed", + requestedByActorType: "system", requestedByActorId: "native-status-committer", + payload: { continuationIdempotencyKey: "native-completion-incomplete" } }).returning(); + await db.update(heartbeatRuns).set({ wakeupRequestId: wake.id }).where(eq(heartbeatRuns.id, seeded.runId)); + } + const lookup = vi.fn().mockResolvedValue("open"); + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver").mockReturnValue(lookup); + try { + const work = nativeStatusReassessmentWork(db), scope = selectedScope(seeded); + const [pending] = await work.load(scope); + expect(pending).toBeDefined(); + expect(await work.check(pending)).toBeNull(); + const [latest] = await db.select().from(statusDecisions).where(eq(statusDecisions.issueId, seeded.issueId)) + .orderBy(statusDecisions.createdAt, statusDecisions.id).then(rows => rows.filter(row => row.id !== seeded.decision.id)); + const effects = latest.decisionJson.effects as Array<{ kind: string; summary?: string; nextAction?: string }>; + expect(latest.toStatus).not.toBe("done"); + expect(effects.some(effect => effect.kind === "enqueue_continuation")).toBe(!correctiveUsed); + expect(effects.some(effect => effect.kind === "record_finalization_error")).toBe(correctiveUsed); + expect(effects.some(effect => (effect.summary ?? effect.nextAction ?? "").includes("not been verified as merged"))).toBe(true); + await db.update(statusDecisionEffects).set({ deliveryState: "delivered" }) + .where(eq(statusDecisionEffects.decisionId, latest.id)); + const assessments = await db.select().from(workAssessments).where(eq(workAssessments.runId, seeded.runId)); + expect(await work.load(scope)).toEqual([]); + expect(await work.check(pending)).toBeNull(); + expect(await db.select().from(workAssessments).where(eq(workAssessments.runId, seeded.runId))).toHaveLength(assessments.length); + expect(lookup).toHaveBeenCalledTimes(1); + } finally { resolve.mockRestore(); } + }); + it("respects a saved retry and live lease, then reloads the released owner", async () => { const seeded = await seedAutomaticReview(); await dismissAutomaticCompletionReviews(db, seeded.issueId); @@ -3145,6 +3199,41 @@ describe("P6-31 Section 18.13 executable status-authority corpus", () => { } finally { boundary.mockRestore(); worker.stop(); await coordinator.stop(); } }, 30_000); + it.each(["open", "unknown", "merged"] as const)("gates native finalization on a %s PR deliverable", async state => { + const seeded = await seedFixture({ ...corpus.fixtures[0]!, id: `pr-completion-${randomUUID()}` }); + await db.update(completionContracts).set({ risk: "low", completionAuthority: "agent_claim_policy" }) + .where(eq(completionContracts.id, seeded.contractId!)); + await db.insert(issueWorkProducts).values({ companyId, issueId: seeded.issueId, createdByRunId: seeded.runId, + type: "pull_request", provider: "github", title: "Implementation PR", status: "active", + url: "https://github.com/example/project/pull/42", metadata: { state: "merged" } }); + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver") + .mockReturnValue(vi.fn().mockResolvedValue(state)); + try { + await finalizeNativeRun({ db, runId: seeded.runId, workspaceFinalizeStatus: "succeeded" }); + expect((await issueService(db).getById(seeded.issueId))!.status).toBe(state === "merged" ? "done" : "in_progress"); + const decisions = await db.select().from(statusDecisions).where(eq(statusDecisions.runId, seeded.runId)); + expect(decisions.some(decision => decision.toStatus === "done")).toBe(state === "merged"); + expect(await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.issueId, seeded.issueId))).toHaveLength(0); + } finally { resolve.mockRestore(); } + }, 30_000); + + it("refuses a direct Done commit for an unmerged PR during reassessment", async () => { + const seeded = await seedFixture({ ...corpus.fixtures[0]!, id: `pr-direct-commit-${randomUUID()}` }); + await db.insert(issueWorkProducts).values({ companyId, issueId: seeded.issueId, createdByRunId: seeded.runId, + type: "pull_request", provider: "github", title: "Implementation PR", status: "merged", + url: "https://github.com/example/project/pull/42" }); + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver") + .mockReturnValue(vi.fn().mockResolvedValue("open")); + try { + await expect(commitNativeStatusDecision({ db, companyId, issueId: seeded.issueId, runId: seeded.runId, + assessmentId: seeded.assessmentId, priorStatus: "in_progress", priorStatusVersion: 0, priorDecisionId: null, + decision: { policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION, statusAction: "done", toStatus: "done", + reasonCode: "decision_superseded_by_new_evidence", unblockDescriptor: null, effects: [{ kind: "release_checkout" }] }, + })).rejects.toThrow("native_status_race"); + expect((await issueService(db).getById(seeded.issueId))!.status).toBe("in_progress"); + } finally { resolve.mockRestore(); } + }, 30_000); + it("completes a new merge run after an old CI review and bounds repeated incomplete results", async () => { const seeded = await seedAutomaticReview(); const [sourceRun] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, seeded.runId)); diff --git a/server/src/__tests__/run-start-handover.test.ts b/server/src/__tests__/run-start-handover.test.ts index 56b79603a12..adf58022a08 100644 --- a/server/src/__tests__/run-start-handover.test.ts +++ b/server/src/__tests__/run-start-handover.test.ts @@ -110,7 +110,7 @@ suite("run start hand-over between an old and a new server during a deploy overl it("the old server stops starting runs once the new one starts them, and the new one starts the queued run", async () => { // The old server booted alone long ago: it recorded its own start and holds no hand-over. - const old = await bootServer("old"); + const old = await bootServer("old", { timing: { skipHintMinMs: 500 } }); await old.handover.begin(); expect(old.handover.isHandingOver()).toBe(false); const before = await queueRun(); @@ -126,12 +126,16 @@ suite("run start hand-over between an old and a new server during a deploy overl expect(next.handover.isHandingOver()).toBe(true); expect(await marker(next)).toMatchObject({ live: true }); await vi.waitFor(() => expect(old.handover.isActive()).toBe(true), { timeout: 10_000 }); + await vi.waitFor(() => expect(next.dispatchHints).toBeGreaterThan(0), { timeout: 10_000 }); const handedOver = await queueRun(); const hintsBefore = next.dispatchHints; expect(await dispatch(old, handedOver)).toBe(false); // The skipped start wakes the new server's queued-run dispatch instead of waiting for a sweep. - await vi.waitFor(() => expect(next.dispatchHints).toBeGreaterThan(hintsBefore), { timeout: 10_000 }); + await vi.waitFor(async () => { + expect(await dispatch(old, handedOver)).toBe(false); + expect(next.dispatchHints).toBeGreaterThan(hintsBefore); + }, { timeout: 10_000 }); expect(await dispatch(next, handedOver)).toBe(true); expect(claims.filter(claim => claim.runId === handedOver)).toEqual([{ server: "new", runId: handedOver }]); diff --git a/server/src/__tests__/work-products.test.ts b/server/src/__tests__/work-products.test.ts index cef395409fd..ede8420ddd9 100644 --- a/server/src/__tests__/work-products.test.ts +++ b/server/src/__tests__/work-products.test.ts @@ -1,3 +1,4 @@ +import { issues } from "@paperclipai/db"; import { PgDialect } from "drizzle-orm/pg-core"; import type { SQL } from "drizzle-orm"; import { describe, expect, it, vi } from "vitest"; @@ -202,8 +203,11 @@ describe("workProductService", () => { it("uses a transaction when promoting an existing work product to primary", async () => { const existingRow = createWorkProductRow({ isPrimary: false }); - const selectWhere = vi.fn(async () => [existingRow]); - const selectFrom = vi.fn(() => ({ where: selectWhere })); + const selectFrom = vi.fn((table: unknown) => { + const rows = table === issues ? [{ id: "issue-1" }] : [existingRow]; + const query = Promise.resolve(rows); + return Object.assign(query, { where: vi.fn(() => query), orderBy: vi.fn(() => query), for: vi.fn(() => query) }); + }); const txSelect = vi.fn(() => ({ from: selectFrom })); const updateReturning = vi @@ -231,7 +235,7 @@ describe("workProductService", () => { }); expect(transaction).toHaveBeenCalledTimes(1); - expect(txSelect).toHaveBeenCalledTimes(1); + expect(txSelect).toHaveBeenCalled(); expect(txUpdate).toHaveBeenCalledTimes(2); expect(result?.reviewState).toBe("ready_for_review"); }); diff --git a/server/src/services/native-runtime/completion-contracts.test.ts b/server/src/services/native-runtime/completion-contracts.test.ts index bfd77b56a8e..110686d9278 100644 --- a/server/src/services/native-runtime/completion-contracts.test.ts +++ b/server/src/services/native-runtime/completion-contracts.test.ts @@ -11,6 +11,16 @@ import { } from "./completion-contracts.js"; describe("buildNativeCompletionContract", () => { + it("keeps PR delivery obligations when the follow-up only asks to push a PR", () => { + const contract = buildNativeCompletionContract( + { title: "Fix task history", description: "Implement and deliver the fix." }, + { immediateRequest: "Push a PR" }, + ); + expect(contract.objective).toContain("merged"); + expect(contract.objective).toContain("register"); + expect(contract.objective).toContain("original task"); + }); + it("scopes standing-chat criteria to the response while preserving every current request and source", () => { const issue = { id: "chat", title: "Chat with CEO", description: null, conversationAgentId: "ceo", conversationUserId: "operator" }; const comments = [ @@ -65,7 +75,7 @@ describe("buildNativeCompletionContract", () => { description: "Prove the Codex vertical slice.", })).toEqual({ revision: "1", - objective: "Ship the runner", + objective: expect.stringContaining("Ship the runner"), criteria: [{ id: "objective", requirement: "Prove the Codex vertical slice." }], }); }); diff --git a/server/src/services/native-runtime/completion-contracts.ts b/server/src/services/native-runtime/completion-contracts.ts index 72130a45a02..062cbd6d443 100644 --- a/server/src/services/native-runtime/completion-contracts.ts +++ b/server/src/services/native-runtime/completion-contracts.ts @@ -113,13 +113,14 @@ export function buildNativeCompletionContract( // follow-up contract. Keep this guidance stable across comments and resumes. const followUpObjective = "Complete the current authorized stage using the task brief and current user direction in the supplied context. Later human direction replaces conflicting scope; preserve other requirements, assigned-skill instructions, and approval gates. Apply authenticated humanResponses only to their question or decision. Clarification is not approval. If acceptance is required, propose or save the requested plan and wait before executing."; const conversationObjective = "Complete the current conversation response using the supplied context and current user direction. Carry out requested chat actions within current authority and verify their results before claiming success. For delegated work, verify the saved handoff and report it without waiting for or performing the worker's task. For an unavailable action, explain the specific limitation and supported handoff without claiming the action occurred. Finishing this response leaves underlying delegated or unavailable work incomplete until separately verified. Later human direction replaces conflicting scope; preserve other requirements, assigned-skill instructions, and approval gates. Apply authenticated humanResponses only to their question or decision. Clarification is not approval."; + const deliveryGuidance = " Preserve the original task's delivery obligations on follow-ups. For implementation work delivered by PR, register the PR as a pull_request work product and cite it in artifacts. Keep the task active through CI and review until the PR is verified merged; a local commit or opened PR is not delivery. Review-only requests retain their stated scope."; return { revision: String(options.revision ?? 1), objective: conversation ? conversationObjective : hasFollowUp - ? followUpObjective - : issue.title, + ? followUpObjective + deliveryGuidance + : issue.title + deliveryGuidance, criteria: hasFollowUp ? [ ...immediateRequests.map((request, index) => ({ diff --git a/server/src/services/native-runtime/native-completion-feedback.test.ts b/server/src/services/native-runtime/native-completion-feedback.test.ts index 654eafe3e86..8081ed60dd7 100644 --- a/server/src/services/native-runtime/native-completion-feedback.test.ts +++ b/server/src/services/native-runtime/native-completion-feedback.test.ts @@ -1,10 +1,15 @@ +import { createServer } from "node:http"; import { createHash, randomUUID } from "node:crypto"; -import { eq } from "drizzle-orm"; -import { afterAll, beforeAll, describe, expect, it } from "vitest"; -import { agents, approvals, companies, createDb, documentRevisions, executionWorkspaces, executionWorkspaceRepositories, heartbeatRuns, issueExecutionDecisions, issues, issueApprovals, issueThreadInteractions, issueTreeHolds, issueDocuments } from "@paperclipai/db"; +import { eq, sql } from "drizzle-orm"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { agents, approvals, companies, createDb, documentRevisions, executionWorkspaces, executionWorkspaceRepositories, heartbeatRuns, issueExecutionDecisions, issues, issueApprovals, issueThreadInteractions, issueTreeHolds, issueDocuments, issueWorkProducts } from "@paperclipai/db"; import { startRecoveryPostgres } from "../../__tests__/helpers/recovery-postgres.js"; import type { PrpStructuredRunResult } from "../../vendor/paperclip-runner/index.js"; import { documentService } from "../documents.js"; +import * as pullRequestMerge from "../github-pull-request-merge.js"; +import { validateNativeDeliverableEvidence } from "./native-deliverable-feedback.js"; +import { nativePullRequestCompletionReferences, verifyNativePullRequestCompletion } from "./native-pull-request-completion.js"; +import { workProductService } from "../work-products.js"; import { nativeCompletionFeedback } from "./native-completion-feedback.js"; import { executionStageApprovedByRun } from "./native-run-finalizer.js"; import { PaperclipRunnerToolAuthority } from "./paperclip-runner-tool-authority.js"; @@ -44,6 +49,175 @@ describe("native final-response feedback", () => { } }) as { downloadPath: string }; return { ...done, evidence: [{ ref: `${receipt.downloadPath}#download` }] }; } + it("refuses to finish a task while its PR deliverable is open, even if metadata claims merged", async () => { + const value = await fixture(); + const resolve = vi.fn().mockResolvedValue("open"); + const spy = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver").mockReturnValue(resolve); + try { + await db.insert(issueWorkProducts).values({ companyId: value.companyId, issueId: value.issueId, + createdByRunId: value.runId, type: "pull_request", provider: "github", title: "Task fix", + url: "https://github.com/example/project/pull/42", status: "active", metadata: { state: "merged" } }); + await expect(nativeCompletionFeedback(db, value.runId, done)).rejects.toThrow(/pull request.*merged/i); + expect(resolve).toHaveBeenCalledWith(value.companyId, expect.objectContaining({ owner: "example", repo: "project", number: 42 }), expect.anything()); + resolve.mockResolvedValue("merged"); + await expect(nativeCompletionFeedback(db, value.runId, done)).resolves.toContain("Completion report accepted"); + } finally { spy.mockRestore(); } + }); + + it("checks a PR artifact without a registered work product and preserves monitor waits", async () => { + const value = await fixture(); + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver") + .mockReturnValue(vi.fn().mockResolvedValue("unknown")); + const report = { ...done, artifacts: [{ kind: "pull_request", ref: "https://github.com/example/project/pull/42" }] }; + try { + await expect(nativeCompletionFeedback(db, value.runId, report)).rejects.toThrow(/verified as merged/); + await expect(nativeCompletionFeedback(db, value.runId, { ...report, reportedWorkDisposition: "yielded", + completionClaim: { ...report.completionClaim, objectiveSatisfied: false, + remainingWork: [{ description: "Wait for the PR merge", blocksCompletion: true }] }, + continuation: { kind: "same_agent", summary: "Continue review", idempotencyKey: "pr-review" }, + })).resolves.toContain("report accepted"); + } finally { resolve.mockRestore(); } + }); + + it("does not gate a chat response or a PR mentioned only as review input", async () => { + const value = await fixture(); + const lookup = vi.fn(); + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver").mockReturnValue(lookup); + try { + await expect(nativeCompletionFeedback(db, value.runId, { ...done, + summary: "Reviewed https://github.com/example/project/pull/42; findings are attached." })).resolves.toContain("Completion report accepted"); + await expect(nativeCompletionFeedback(db, value.runId, { ...done, + artifacts: [{ kind: "review_comment", ref: "https://github.com/example/project/pull/42#issuecomment-123" }] })) + .resolves.toContain("Completion report accepted"); + await db.update(issues).set({ conversationAgentId: value.agentId, conversationUserId: "operator", conversationState: "active" }).where(eq(issues.id, value.issueId)); + await expect(nativeCompletionFeedback(db, value.runId, { ...done, + artifacts: [{ kind: "pull_request", ref: "https://github.com/example/project/pull/42" }] })).resolves.toContain("Completion report accepted"); + expect(lookup).not.toHaveBeenCalled(); + } finally { resolve.mockRestore(); } + }); + + it.each([ + "https://github.com/example/project/pull/not-a-number", + "https://github.com/example/project/pull/0", + "https://github.com/example/project/pull/9007199254740992", + "https://example.test/example/project/pull/42", + ])("rejects an explicitly declared PR artifact with an unreadable identity: %s", async ref => { + const value = await fixture(); + const lookup = vi.fn(); + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver").mockReturnValue(lookup); + const report = { ...done, artifacts: [{ kind: "pull_request", ref }] }; + try { + await expect(nativeCompletionFeedback(db, value.runId, report)).rejects.toThrow("PR artifact has no verifiable GitHub identity"); + await expect(db.transaction(tx => validateNativeDeliverableEvidence(tx as unknown as typeof db, + { ...value, objective: "Deliver the implementation.", semanticToolReceipts: {}, lockSources: true, + authoritativeDone: true, verifiedMergedPullRequests: new Set() }, report))) + .rejects.toThrow("PR artifact has no verifiable GitHub identity"); + expect(lookup).not.toHaveBeenCalled(); + } finally { resolve.mockRestore(); } + }); + + it.each([ + "https://github.com/example/project/pull/42/files#r123", + "https://github.com/example/project/pull/42/files?diff=split#r123", + "https://github.com/example/project/pull/42/changes#r123", + "https://github.com/example/project/pull/42#discussion_r123", + ])("does not treat a file-level review comment as delivery of the reviewed PR: %s", async ref => { + const value = await fixture(); + const lookup = vi.fn().mockResolvedValue("open"); + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver").mockReturnValue(lookup); + try { + await expect(nativeCompletionFeedback(db, value.runId, { ...done, + artifacts: [{ kind: "review_comment", ref }] })).resolves.toContain("Completion report accepted"); + expect(lookup).not.toHaveBeenCalled(); + await expect(nativeCompletionFeedback(db, value.runId, { ...done, + artifacts: [{ kind: "pull_request", ref }] })).rejects.toThrow("not been verified as merged"); + } finally { resolve.mockRestore(); } + }); + + it("rechecks current deliverables under the commit lock without making network calls", async () => { + const value = await fixture(); + const lookup = vi.fn().mockResolvedValue("merged"); + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver").mockReturnValue(lookup); + try { + const report = { ...done, artifacts: [{ kind: "pull_request", ref: "https://github.com/example/project/pull/42" }] }; + const proof = await verifyNativePullRequestCompletion(db, value, report); + await db.insert(issueWorkProducts).values({ companyId: value.companyId, issueId: value.issueId, + type: "pull_request", provider: "github", title: "Another delivery PR", status: "ready_for_review", + url: "https://github.com/example/project/pull/43" }); + await expect(db.transaction(tx => validateNativeDeliverableEvidence(tx as unknown as typeof db, + { ...value, objective: "Deliver the implementation.", semanticToolReceipts: {}, + lockSources: true, authoritativeDone: true, verifiedMergedPullRequests: proof }, report))) + .rejects.toThrow("deliverables changed before completion"); + expect(lookup).toHaveBeenCalledTimes(1); + } finally { resolve.mockRestore(); } + }); + + it("serializes human restoration of an archived PR with the completion task lock", async () => { + const value = await fixture(); + const [product] = await db.insert(issueWorkProducts).values({ companyId: value.companyId, issueId: value.issueId, + type: "pull_request", provider: "github", title: "Archived delivery", status: "archived", + url: "https://github.com/example/project/pull/42" }).returning(); + let acquired!: (pid: number) => void; + const locked = new Promise(resolve => { acquired = resolve; }); + let release!: () => void; + const unlocked = new Promise(resolve => { release = resolve; }); + const completion = db.transaction(async tx => { + await tx.select({ id: issues.id }).from(issues).where(eq(issues.id, value.issueId)).for("update"); + expect(await nativePullRequestCompletionReferences(tx as unknown as typeof db, { ...value, lockSources: true }, done)).toEqual([]); + const [owner] = await tx.execute(sql`select pg_backend_pid() as pid`) as unknown as Array<{ pid: number }>; + acquired(owner.pid); + await unlocked; + }); + const ownerPid = await locked; + let restored = false; + const restoration = workProductService(db).update(product.id, { status: "active", url: "https://github.com/example/project/pull/43" }, null) + .then(row => { restored = true; return row; }); + try { + await vi.waitFor(async () => { + const [wait] = await db.execute(sql`select exists(select 1 from pg_stat_activity activity + where ${ownerPid} = any(pg_blocking_pids(activity.pid))) as blocked`) as unknown as Array<{ blocked: boolean }>; + expect(restored).toBe(false); + expect(wait.blocked).toBe(true); + }, { timeout: 1500 }); + } finally { release(); await completion; await restoration; } + expect(restored).toBe(true); + }); + + it("aborts a stalled provider body without holding the task lock or completing the task", async () => { + const value = await fixture(); + let observed!: () => void; + const requested = new Promise(resolve => { observed = resolve; }); + let disconnected!: () => void; + const closed = new Promise(resolve => { disconnected = resolve; }); + const server = createServer((_request, response) => { + response.on("close", disconnected); + response.writeHead(200, { "content-type": "application/json" }); + response.write('{"merged":'); + observed(); + }); + await new Promise(resolve => { server.listen(0, "127.0.0.1", resolve); }); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("Expected loopback address"); + const original = pullRequestMerge.createPullRequestMergeStateResolver; + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver").mockImplementation(database => original(database, { + tokenProvider: () => null, requestTimeoutMs: 1500, + fetch: (url, init) => fetch(`http://127.0.0.1:${address.port}${new URL(url).pathname}`, init), + })); + try { + const finished = nativeCompletionFeedback(db, value.runId, { ...done, + artifacts: [{ kind: "pull_request", ref: "https://github.com/example/project/pull/42" }] }) + .then(() => "unexpected completion", error => String(error)); + await requested; + await db.update(issues).set({ title: "Still reviewing the implementation" }).where(eq(issues.id, value.issueId)); + expect(await finished).toContain("not been verified as merged"); + await closed; + expect((await db.select().from(issues).where(eq(issues.id, value.issueId)))[0]!.status).toBe("in_progress"); + } finally { + resolve.mockRestore(); server.closeAllConnections(); + await new Promise(resolve => { server.close(() => resolve()); }); + } + }); + // Cohort 15 (agents.hire-needs-approval): the agent copied "/approvals/" from this feedback into its reply. // Board routes live under the company prefix, so the feedback gives the canonical prefixed links. it("links pending approvals and the task on their company-prefixed board routes", async () => { diff --git a/server/src/services/native-runtime/native-completion-feedback.ts b/server/src/services/native-runtime/native-completion-feedback.ts index 2fe75c5a674..bca0ed7d210 100644 --- a/server/src/services/native-runtime/native-completion-feedback.ts +++ b/server/src/services/native-runtime/native-completion-feedback.ts @@ -236,7 +236,8 @@ export async function nativeCompletionFeedback( const objective = typeof continuation?.objective === "string" ? continuation.objective : [issue.title, issue.description].filter(Boolean).join("\n"); await validateNativeDeliverableEvidence(db, { companyId: run.companyId, issueId: issue.id, runId, - objective, semanticToolReceipts: run.resultJson?.semanticToolReceipts }, result); + objective, semanticToolReceipts: run.resultJson?.semanticToolReceipts, + enteringConfiguredReview: !isConversation(issue) && completionReviewStageEntry(issue, run.agentId).kind === "starts" }, result); if (result.reportedWorkDisposition === "done") { const quality = await readRequiredQualityReview(db, issue); if (!quality.satisfied) throw new Error(`Required human quality review is incomplete: ${JSON.stringify(quality.missing)}. Read each existing review and preserve approved unchanged outputs. For missing or revised outputs call request_human_input with a concise confirmation payload.qualityReview:{key} (or item_verdicts items with qualityReview:{key}), payload.target with the exact current output, top-level resolverPolicy:human_only and continuationPolicy:wake_assignee, and payload.supersedeOnUserComment:false. Do not omit payload or its bindings after a validation error. Wait for its recorded response; do not report done, create a generic completion review, or treat plan acceptance/file presence as quality approval.`); diff --git a/server/src/services/native-runtime/native-deliverable-feedback.ts b/server/src/services/native-runtime/native-deliverable-feedback.ts index 606c4ee2443..9ca7d377772 100644 --- a/server/src/services/native-runtime/native-deliverable-feedback.ts +++ b/server/src/services/native-runtime/native-deliverable-feedback.ts @@ -1,3 +1,4 @@ +import { NativePullRequestCompletionError, nativePullRequestCompletionKey, nativePullRequestCompletionReferences, verifyNativePullRequestCompletion } from "./native-pull-request-completion.js"; import { and, eq } from "drizzle-orm"; import { createHash } from "node:crypto"; import { assets, companies, completionContracts, documents, documentRevisions, heartbeatRuns, issueDocuments, issueAttachments, issues, issueThreadInteractions, issueWorkProducts, mcpAttachmentUploads, dotRunnerAssignments, type Db } from "@paperclipai/db"; @@ -346,11 +347,26 @@ async function completedChildAttachment(db: Db, binding: { companyId: string; is /** Files cited as completed output must be reachable outside the agent workspace. */ export async function validateNativeDeliverableEvidence( db: Db, - binding: { companyId: string; issueId: string; runId: string; objective: string; semanticToolReceipts: unknown; lockSources?: boolean; authoritativeDone?: true }, + binding: { companyId: string; issueId: string; runId: string; objective: string; semanticToolReceipts: unknown; lockSources?: boolean; authoritativeDone?: true; enteringConfiguredReview?: boolean; verifiedMergedPullRequests?: ReadonlySet }, result: unknown, ): Promise { const evidence = publicationEvidence(result); if (!binding.authoritativeDone && evidence.disposition !== "done") return; + if (!binding.enteringConfiguredReview) { + try { + if (binding.lockSources) { + const references = await nativePullRequestCompletionReferences(db, binding, result); + if (references.some(reference => !binding.verifiedMergedPullRequests?.has(nativePullRequestCompletionKey(reference)))) { + throw new NativePullRequestCompletionError("Pull request deliverables changed before completion. Recheck their merge state."); + } + } else { + await verifyNativePullRequestCompletion(db, binding, result); + } + } catch (error) { + if (!(error instanceof NativePullRequestCompletionError)) throw error; + throw new NativeDeliverableEvidenceError(error.message); + } + } const fileRequested = explicitlyRequestsFileOutput(binding.objective); const taskDocumentRequested = explicitlyRequestsTaskDocumentOutput(binding.objective) && !await declinedTaskDocumentNotRequired(db, binding); diff --git a/server/src/services/native-runtime/native-execution-policy-review-stage.test.ts b/server/src/services/native-runtime/native-execution-policy-review-stage.test.ts index fee42a153d7..17113d7d578 100644 --- a/server/src/services/native-runtime/native-execution-policy-review-stage.test.ts +++ b/server/src/services/native-runtime/native-execution-policy-review-stage.test.ts @@ -1,6 +1,7 @@ +import * as pullRequestMerge from "../github-pull-request-merge.js"; import { randomUUID } from "node:crypto"; import { eq } from "drizzle-orm"; -import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; import { agentWakeupRequests, agents, companies, companyMemberships, completionContracts, createDb, heartbeatRuns, issueThreadInteractions, issueWorkProducts, issues, statusDecisions } from "@paperclipai/db"; import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js"; import { completionReviewStageEntry } from "../issue-execution-policy.js"; @@ -89,6 +90,23 @@ describe.skipIf(!support.supported)("native completion with a configured review expect(r.decisions.map(row => row.reasonCode)).toEqual(["completion_contract_satisfied"]); }); + it("allows an open PR to enter its configured review without claiming completion", async () => { + const lookup = vi.fn().mockResolvedValue("open"); + const resolve = vi.spyOn(pullRequestMerge, "createPullRequestMergeStateResolver").mockReturnValue(lookup); + try { + const r = await finishVerifiedDone({ stage: "user", beforeFinish: async (runId, result) => { + const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, runId)); + await db.insert(issueWorkProducts).values({ companyId: run.companyId, issueId: run.nativeIssueId!, + type: "pull_request", provider: "github", title: "Implementation", status: "ready_for_review", + url: "https://github.com/example/project/pull/42" }); + expect(await nativeCompletionFeedback(db, runId, result)).toContain("configured review stage starts"); + } }); + expect(r.task.status).toBe("in_review"); + expect(r.decisions.map(row => row.reasonCode)).toEqual(["execution_stage_review_required"]); + expect(lookup).not.toHaveBeenCalled(); + } finally { resolve.mockRestore(); } + }); + it("starts the configured human review stage instead of closing verified work", async () => { const r = await finishVerifiedDone({ stage: "user", monitor: true }); expect(r.task).toMatchObject({ status: "in_review", assigneeUserId: r.reviewerId, assigneeAgentId: null, monitorNextCheckAt: null }); diff --git a/server/src/services/native-runtime/native-pull-request-completion.ts b/server/src/services/native-runtime/native-pull-request-completion.ts new file mode 100644 index 00000000000..901d2d9e1eb --- /dev/null +++ b/server/src/services/native-runtime/native-pull-request-completion.ts @@ -0,0 +1,87 @@ +import { and, eq, ne } from "drizzle-orm"; +import { heartbeatRuns, issues, issueWorkProducts, nativeRunFinalizations, nativeRunResults, type Db } from "@paperclipai/db"; +import { isConversation } from "../agent-conversations.js"; +import { createPullRequestMergeStateResolver, extractGitHubPullRequestReferences } from "../github-pull-request-merge.js"; +import { getNativeReviewAssignment, readNativeReviewAssignmentContext } from "./native-review-participant.js"; + +export class NativePullRequestCompletionError extends Error {} + +type Binding = { companyId: string; issueId: string; runId: string; lockSources?: boolean }; + +function record(value: unknown): Record { + return value && typeof value === "object" && !Array.isArray(value) ? value as Record : {}; +} + +export async function nativePullRequestCompletionReferences(db: Db, binding: Binding, result: unknown) { + const sourceQuery = db.select({ issue: issues, context: heartbeatRuns.contextSnapshot, agentId: heartbeatRuns.agentId }).from(issues) + .innerJoin(heartbeatRuns, and(eq(heartbeatRuns.id, binding.runId), eq(heartbeatRuns.companyId, binding.companyId), + eq(heartbeatRuns.nativeIssueId, binding.issueId))) + .where(and(eq(issues.id, binding.issueId), eq(issues.companyId, binding.companyId))).limit(1); + const [source] = await (binding.lockSources ? sourceQuery.for("share", { noWait: true }) : sourceQuery); + if (!source) throw new NativePullRequestCompletionError("Pull request completion source is unavailable."); + if (["done", "cancelled"].includes(source.issue.status)) return []; + if (isConversation(source.issue)) return []; + if (readNativeReviewAssignmentContext(source.context) && await getNativeReviewAssignment(db, { + ...binding, agentId: source.agentId, contextSnapshot: source.context, allowResolvedByRunId: binding.runId, + })) return []; + const query = db.select({ url: issueWorkProducts.url, metadata: issueWorkProducts.metadata }).from(issueWorkProducts) + .where(and(eq(issueWorkProducts.companyId, binding.companyId), eq(issueWorkProducts.issueId, binding.issueId), + eq(issueWorkProducts.type, "pull_request"), ne(issueWorkProducts.status, "archived"))).limit(21); + const products = await (binding.lockSources ? query.for("share", { noWait: true }) : query); + if (products.length > 20) throw new NativePullRequestCompletionError("Too many pull request deliverables to verify. Keep the task active and verify a bounded delivery scope."); + const artifacts = record(result).artifacts; + const references = extractGitHubPullRequestReferences([ + ...(Array.isArray(artifacts) ? artifacts.flatMap(value => { + const artifact = record(value); + const ref = typeof value === "string" ? value : artifact.ref; + if (artifact.kind === "pull_request" && extractGitHubPullRequestReferences([ref]).length !== 1) { + throw new NativePullRequestCompletionError("A declared PR artifact has no verifiable GitHub identity. Register its canonical PR URL before completing the task."); + } + if (artifact.kind !== "pull_request" && (artifact.kind === "review_comment" || + (typeof ref === "string" && /\/pull\/[1-9][0-9]*(?:[/?][^#\s]*)?#[^\s]+/u.test(ref)))) return []; + return [ref]; + }) : []), + ...products.flatMap(product => { + const metadata = record(product.metadata); + const repo = metadata.repo ?? metadata.repository; + const values = [product.url, metadata.url, typeof repo === "string" && typeof metadata.number === "number" + ? `${repo}#${metadata.number}` : null]; + if (extractGitHubPullRequestReferences(values).length === 0) throw new NativePullRequestCompletionError( + "A task pull request deliverable has no verifiable GitHub identity. Register its canonical PR URL before completing the task.", + ); + return values; + }), + ]); + if (references.length > 20) throw new NativePullRequestCompletionError("Too many pull request deliverables to verify. Keep the task active and verify a bounded delivery scope."); + return references; +} + +export function nativePullRequestCompletionKey(reference: { owner: string; repo: string; number: number }) { + return `${reference.owner.toLowerCase()}/${reference.repo.toLowerCase()}#${reference.number}`; +} + +export async function verifyNativePullRequestCompletion(db: Db, binding: Binding, result?: unknown): Promise> { + if (result === undefined) { + const [saved] = await db.select({ envelope: nativeRunResults.resultJson }).from(nativeRunFinalizations) + .innerJoin(nativeRunResults, and(eq(nativeRunResults.id, nativeRunFinalizations.resultId), + eq(nativeRunResults.companyId, binding.companyId), eq(nativeRunResults.issueId, binding.issueId), + eq(nativeRunResults.runId, binding.runId), eq(nativeRunResults.schemaStatus, "accepted"))) + .where(and(eq(nativeRunFinalizations.companyId, binding.companyId), eq(nativeRunFinalizations.issueId, binding.issueId), + eq(nativeRunFinalizations.runId, binding.runId))).limit(1); + if (!saved) throw new NativePullRequestCompletionError("Pull request completion result is unavailable."); + result = record(saved.envelope).result; + } + const references = await nativePullRequestCompletionReferences(db, binding, result); + if (references.length === 0) return new Set(); + const resolve = createPullRequestMergeStateResolver(db); + const signal = AbortSignal.timeout(10_000); + const states = await Promise.all(references.map(async reference => { + try { return await resolve(binding.companyId, reference, signal); } catch { return "unknown"; } + })); + const pending = references.filter((_, index) => states[index] !== "merged"); + if (pending.length) throw new NativePullRequestCompletionError( + `Task pull request deliverables have not been verified as merged: ${pending.map(nativePullRequestCompletionKey).join(", ")}. ` + + "Keep the task active through CI, review, and merge. Fix actionable failures within your authority. Use the existing task monitor for an external wait, or report a concrete blocker. An open, draft, closed-unmerged, or unavailable PR cannot complete the task. Do not request a generic completion approval.", + ); + return new Set(references.map(nativePullRequestCompletionKey)); +} diff --git a/server/src/services/native-runtime/native-run-finalizer.ts b/server/src/services/native-runtime/native-run-finalizer.ts index 5967a6f18f4..b6305dea131 100644 --- a/server/src/services/native-runtime/native-run-finalizer.ts +++ b/server/src/services/native-runtime/native-run-finalizer.ts @@ -1848,7 +1848,8 @@ export async function finalizeNativeRun(input: { : [authoritativeIssue.title, authoritativeIssue.description].filter(Boolean).join("\n"); try { await validateNativeDeliverableEvidence(input.db, { companyId: run.companyId, issueId: authoritativeIssue.id, - runId: run.id, objective, semanticToolReceipts: run.resultJson?.semanticToolReceipts }, result); + runId: run.id, objective, semanticToolReceipts: run.resultJson?.semanticToolReceipts, + enteringConfiguredReview: proposedDecision.reasonCode === COMPLETION_REVIEW_STAGE_REASON }, result); } catch (error) { if (!(error instanceof NativeDeliverableEvidenceError)) throw error; const incomplete = resolveNativeFinalizerStatus({ ...statusInput, diff --git a/server/src/services/native-runtime/native-status-reassessment.ts b/server/src/services/native-runtime/native-status-reassessment.ts index b2dca3eaa56..629169388d4 100644 --- a/server/src/services/native-runtime/native-status-reassessment.ts +++ b/server/src/services/native-runtime/native-status-reassessment.ts @@ -1,8 +1,9 @@ +import { NativeDeliverableEvidenceError, validateNativeDeliverableEvidence } from "./native-deliverable-feedback.js"; import { readRequiredQualityReview } from "../required-quality-review.js"; import { completionReviewStageEntry } from "../issue-execution-policy.js"; import { hasNativeWorkspaceFinalizationOwner } from "./native-workspace-finalization-recovery-state.js"; import { and, desc, eq, gt, or } from "drizzle-orm"; -import { completionContracts, heartbeatRuns, issueWorkProducts, issues, nativeRunResults, +import { agentWakeupRequests, completionContracts, heartbeatRuns, issueWorkProducts, issues, nativeRunResults, statusDecisions, workAssessments, workspaceOperations, type Db } from "@paperclipai/db"; import { issueService } from "../issues.js"; import { decisionHasRetiredAutomaticReview } from "./automatic-completion-reviews.js"; @@ -246,6 +247,38 @@ export async function reassessNativeStatusFromSource( decision = supersedeStaleOwnerCompletion(decision, { ...currentIssue, status: row.issueStatus }, row.agentId, row.runId); } decision = gateDoneOnConfiguredReview(decision, reviewStage, row.agentId); + if (decision.statusAction === "done" && currentIssue) { + const run = currentRun ?? await db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.id, row.runId), + eq(heartbeatRuns.companyId, row.companyId), eq(heartbeatRuns.nativeIssueId, row.issueId))) + .limit(1).then(rows => rows[0]); + if (!run) return { kind: "deferred", retry: true }; + const continuation = record(run.contextSnapshot?.executionContinuation); + const objective = typeof continuation.objective === "string" ? continuation.objective + : [currentIssue.title, currentIssue.description].filter(Boolean).join("\n"); + try { + await validateNativeDeliverableEvidence(db, { companyId: row.companyId, issueId: row.issueId, + runId: row.runId, objective, semanticToolReceipts: run.resultJson?.semanticToolReceipts, authoritativeDone: true }, + record(resultRow.resultJson).result); + } catch (error) { + if (!(error instanceof NativeDeliverableEvidenceError)) throw error; + const [wake] = run.wakeupRequestId ? await db.select({ payload: agentWakeupRequests.payload }).from(agentWakeupRequests) + .where(and(eq(agentWakeupRequests.id, run.wakeupRequestId), eq(agentWakeupRequests.companyId, row.companyId))) : []; + const dependency = readiness ?? await issueService(db).getDependencyReadiness(row.issueId, db); + const gate = governanceGate ?? await pendingNativeGovernance({ db, companyId: row.companyId, issueId: row.issueId, + runId: row.runId, executionState: record(currentIssue.executionState) }); + const incomplete = resolveNativeFinalizerStatus({ + assessment: { ...reassessment, hasBlockingRemainingWork: true, continuation: null }, + terminalState: "succeeded", workspaceFinalizeStatus: "succeeded", governanceGate: gate, + hasPendingChildCompletion, hasUnresolvedIssueBlockers: dependency.unresolvedBlockerCount > 0, + allowIncompleteContinuation: record(wake?.payload).continuationIdempotencyKey !== "native-completion-incomplete", + priorIssueStatus: row.issueStatus as NativeAuthoritativeIssueStatus, agentId: row.agentId, + reviewOwnerUserId: currentIssue.responsibleUserId ?? currentIssue.createdByUserId, + }); + decision = { ...incomplete, effects: incomplete.effects.map(effect => effect.kind === "enqueue_continuation" + ? { ...effect, summary: error.message } : effect.kind === "record_finalization_error" + ? { ...effect, nextAction: error.message } : effect) }; + } + } let committed: Awaited>; try { committed = await commitNativeStatusDecision({ diff --git a/server/src/services/native-runtime/status-decision-committer.ts b/server/src/services/native-runtime/status-decision-committer.ts index cc44ecd1659..85fc187fe6f 100644 --- a/server/src/services/native-runtime/status-decision-committer.ts +++ b/server/src/services/native-runtime/status-decision-committer.ts @@ -1,3 +1,4 @@ +import { NativePullRequestCompletionError, verifyNativePullRequestCompletion } from "./native-pull-request-completion.js"; import { notifyNativeStatusWakeWork } from "../deferred-execution-notifications.js"; import { toolCallLoopContinuationKey } from "./repeated-tool-failure-guard.js"; import { assertAgentInteraction } from "../agent-interaction-policy.js"; @@ -1727,6 +1728,14 @@ export async function commitNativeStatusDecision(input: { || reasonCode === NATIVE_AGENT_BLOCKER_HANDOFF_CODE) && !input.requireProviderFailureOwner) { throw new Error("native_provider_failure_owner_required"); } + let verifiedMergedPullRequests: ReadonlySet | undefined; + if (input.decision.statusAction === "done") { + try { verifiedMergedPullRequests = await verifyNativePullRequestCompletion(input.db, input); } + catch (error) { + if (error instanceof NativePullRequestCompletionError) throw new NativeStatusRaceError(); + throw error; + } + } const publications: ActivityPublication[] = []; const terminalRunsToEmit: (typeof heartbeatRuns.$inferSelect)[] = []; const committed = await input.db.transaction(async (tx) => { @@ -1901,7 +1910,7 @@ export async function commitNativeStatusDecision(input: { : [issue.title, issue.description].filter(Boolean).join("\n"); await validateNativeDeliverableEvidence(tx as unknown as Db, { companyId: input.companyId, issueId: input.issueId, runId: input.runId, objective, semanticToolReceipts: saved.receipts?.semanticToolReceipts, lockSources: true, - authoritativeDone: true }, record(saved.envelope).result); + authoritativeDone: true, verifiedMergedPullRequests }, record(saved.envelope).result); } catch (error) { if (error instanceof NativeDeliverableEvidenceError || record(error).code === "55P03" || record(record(error).cause).code === "55P03") throw new NativeStatusRaceError(); diff --git a/server/src/services/work-products.ts b/server/src/services/work-products.ts index e4373a34081..107023b6cf0 100644 --- a/server/src/services/work-products.ts +++ b/server/src/services/work-products.ts @@ -1,9 +1,9 @@ import { notifyNativeStatusReassessment } from "./native-status-reassessment-notifications.js"; import { assertAgentInteraction } from "./agent-interaction-policy.js"; import { notifyApprovalQueueWork } from "./approval-work-notifications.js"; -import { and, desc, eq, inArray } from "drizzle-orm"; +import { and, desc, eq, inArray, or } from "drizzle-orm"; import type { Db } from "@paperclipai/db"; -import { executionWorkspaces, heartbeatRunEvents, issueWorkProducts, workspaceRuntimeServices } from "@paperclipai/db"; +import { executionWorkspaces, heartbeatRunEvents, issues, issueWorkProducts, workspaceRuntimeServices } from "@paperclipai/db"; import type { IssueWorkProduct } from "@paperclipai/shared"; import { unprocessable } from "../errors.js"; import { insertRowsInChunks } from "./batch-insert.js"; @@ -23,6 +23,14 @@ import type { ImportIssueWorkProductRow } from "./import-write-types.js"; type IssueWorkProductRow = typeof issueWorkProducts.$inferSelect; type WorkProductTransaction = Parameters[0]>[0]; +async function lockWorkProductIssues(tx: WorkProductTransaction, scopes: Array<{ companyId: string; issueId: string }>) { + const unique = [...new Map(scopes.map(scope => [`${scope.companyId}:${scope.issueId}`, scope])).values()]; + const rows = await tx.select({ id: issues.id }).from(issues).where(or(...unique.map(scope => + and(eq(issues.id, scope.issueId), eq(issues.companyId, scope.companyId))))) + .orderBy(issues.id).for("update"); + if (rows.length !== unique.length) throw unprocessable("Work product tasks must exist in their bound companies."); +} + async function validateExecutionWorkspace( tx: WorkProductTransaction, companyId: string, @@ -311,10 +319,18 @@ export function workProductService( update: async (id: string, patch: Partial, actorAgentId?: string | null) => { const row = await db.transaction(async (tx) => { + const [location] = await tx.select({ companyId: issueWorkProducts.companyId, issueId: issueWorkProducts.issueId }) + .from(issueWorkProducts).where(eq(issueWorkProducts.id, id)); + if (!location) return null; + await lockWorkProductIssues(tx, [location, { + companyId: patch.companyId ?? location.companyId, issueId: patch.issueId ?? location.issueId, + }]); const existing = await tx .select() .from(issueWorkProducts) - .where(eq(issueWorkProducts.id, id)) + .where(and(eq(issueWorkProducts.id, id), eq(issueWorkProducts.companyId, location.companyId), + eq(issueWorkProducts.issueId, location.issueId))) + .for("update") .then((rows) => rows[0] ?? null); if (!existing) return null; await assertAgentInteraction(tx, { companyId: existing.companyId, actorAgentId, operation: "edit", issueId: existing.issueId, diff --git a/tests/e2e/planning-mode-visual-verification.spec.ts b/tests/e2e/planning-mode-visual-verification.spec.ts index 785ae5a2b94..309ee89b457 100644 --- a/tests/e2e/planning-mode-visual-verification.spec.ts +++ b/tests/e2e/planning-mode-visual-verification.spec.ts @@ -125,7 +125,7 @@ test("captures planning mode UI for desktop and mobile", async ({ page }) => { const issueIdentifier = issue.identifier ?? issue.id; const issuePath = `/${company.issuePrefix ?? company.id}/issues/${issueIdentifier}`; const companyPrefix = company.issuePrefix ?? company.id; - const issueLinkSelector = `a[href$="/issues/${issueIdentifier}"]`; + const issueRow = page.locator(`[data-issue-row-details="${issue.id}"]`); const setMode = async (mode: "standard" | "planning") => { const patchRes = await page.request.patch(`${baseOrigin}/api/issues/${issue.id}`, { @@ -159,8 +159,8 @@ test("captures planning mode UI for desktop and mobile", async ({ page }) => { }); await page.goto(`/${companyPrefix}/issues`); - await expect(page.locator(issueLinkSelector)).toBeVisible(); - await expect(page.locator(issueLinkSelector)).not.toContainText("Plan mode"); + await expect(issueRow).toBeVisible(); + await expect(issueRow).not.toContainText("Plan mode"); await page.screenshot({ path: `${screenshotDir}/desktop-planning-row-${timestamp}.png`, fullPage: true, @@ -190,8 +190,8 @@ test("captures planning mode UI for desktop and mobile", async ({ page }) => { }); await page.goto(`/${companyPrefix}/issues`); - await expect(page.locator(issueLinkSelector)).toBeVisible(); - await expect(page.locator(issueLinkSelector)).not.toContainText("Plan mode"); + await expect(issueRow).toBeVisible(); + await expect(issueRow).not.toContainText("Plan mode"); await page.screenshot({ path: `${screenshotDir}/mobile-planning-row-${timestamp}.png`, fullPage: true,