mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-30 17:14:54 +01:00
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 afee630120. Greptile is 5/5 with no
unresolved review threads.
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
673742bfc6
commit
d4b7ae5532
17 files changed
+536
-32
No files matched your search
@@ -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,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<string>() }, 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<number>(resolve => { acquired = resolve; });
|
||||
let release!: () => void;
|
||||
const unlocked = new Promise<void>(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<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,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<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" && 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<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,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<ReturnType<typeof commitNativeStatusDecision>>;
|
||||
try {
|
||||
committed = await commitNativeStatusDecision({
|
||||
|
||||
@@ -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