mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 23:56:43 +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
@@ -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));
|
||||
|
||||
@@ -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 }]);
|
||||
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<Parameters<Db["transaction"]>[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<typeof issueWorkProducts.$inferInsert>, 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,
|
||||
|
||||
Reference in new issue
Block a user