mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-20 09:50:48 +01:00
Keep PR-backed native tasks open until delivery is merged
Native completion accepted an agent claim after a PR was opened, even when CI and review remained unfinished. Follow-up contracts could narrow the reported objective to opening the PR and lose the task's delivery obligation. Preserve that obligation in task guidance and verify declared PR outputs with the company-scoped GitHub resolver before terminal completion. Recheck current outputs under the status lock, including reassessment, while allowing configured review stages and conversation responses to retain their lifecycle. Verified with server typecheck and 461 focused tests covering completion, finalization, configured review, changed outputs, and a stalled HTTP body. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
07d9316218
commit
88249dc41d
11 files changed
+318
-10
No files matched your search
@@ -123,6 +123,35 @@ 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.
|
||||
|
||||
The transactional commit also checks these outputs, including reassessment
|
||||
paths. 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
|
||||
|
||||
@@ -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";
|
||||
@@ -3145,6 +3146,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));
|
||||
|
||||
@@ -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." }],
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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) => ({
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
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 { 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 { verifyNativePullRequestCompletion } from "./native-pull-request-completion.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 +48,106 @@ 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("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("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<void>(resolve => { observed = resolve; });
|
||||
let disconnected!: () => void;
|
||||
const closed = new Promise<void>(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<void>(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<void>(resolve => { server.close(() => resolve()); });
|
||||
}
|
||||
});
|
||||
|
||||
// Cohort 15 (agents.hire-needs-approval): the agent copied "/approvals/<id>" 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 () => {
|
||||
|
||||
@@ -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.`);
|
||||
|
||||
@@ -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<string> },
|
||||
result: unknown,
|
||||
): Promise<void> {
|
||||
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);
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
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<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
||||
}
|
||||
|
||||
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" && typeof ref === "string" && /\/pull\/[1-9][0-9]*#[^\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<ReadonlySet<string>> {
|
||||
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));
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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<string> | 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();
|
||||
|
||||
Reference in new issue
Block a user