diff --git a/doc/architecture/native-status-arbitration.md b/doc/architecture/native-status-arbitration.md index 91cd4d16621..93ba0a32f1e 100644 --- a/doc/architecture/native-status-arbitration.md +++ b/doc/architecture/native-status-arbitration.md @@ -191,6 +191,14 @@ progress or new input. The "Waiting for a reply" execution label shows only while the parked decision is still the issue's latest decision and the issue is `in_progress`. +A completion claim stays `in_progress` as `native_child_completion_pending` +while an undelivered child-completion wake for the same issue and agent is +queued, claimed or deferred. Only a child result saved after the finishing run +started counts as undelivered. A result saved before that run started was +already visible to it, so the claim completes the task and the deferred wake +is cancelled at release instead of starting another turn. A run with no +recorded start keeps every such wake pending. + ## Transactional decision commit `commitNativeStatusDecision()` applies the decision against authoritative issue diff --git a/server/src/__tests__/heartbeat-process-recovery.test.ts b/server/src/__tests__/heartbeat-process-recovery.test.ts index c90676328e5..ecb8058cfb0 100644 --- a/server/src/__tests__/heartbeat-process-recovery.test.ts +++ b/server/src/__tests__/heartbeat-process-recovery.test.ts @@ -266,6 +266,7 @@ import { import { claimNativeSessionResumptions } from "../services/native-runtime/native-finalization-reconciler.ts"; import { PaperclipControlPlanePort } from "../services/native-runtime/paperclip-control-plane-port.js"; import { finalizeNativeRun } from "../services/native-runtime/native-run-finalizer.js"; +import { createWakeQueue } from "../modules/wake-queue/index.js"; import { recordNativeAttentionAssessment } from "../services/native-runtime/work-assessments.js"; import { routeNativeAttention } from "../services/native-runtime/native-interaction-bridge.js"; import * as paperclipRunner from "../vendor/paperclip-runner/index.js"; @@ -15477,6 +15478,91 @@ describeEmbeddedPostgres("heartbeat orphaned process recovery", () => { .toMatchObject({ phase: "completed", label: "Completed" }); }); + it.each([ + ["before", "done", "completion_claim_policy_accepted"], + ["after", "in_progress", "native_child_completion_pending"], + ] as const)("treats a child result saved %s the parent run started as read when the run finishes the task", async ( + savedRelativeToStart, expectedStatus, expectedReason, + ) => { + const fixture = await seedStrandedIssueFixture({ status: "in_progress", runStatus: "succeeded", livenessState: "advanced" }); + const { companyId, agentId, issueId, runId } = fixture; + const childId = randomUUID(); + const contractId = randomUUID(); + const runnerInstanceId = randomUUID(); + const runStartedAt = new Date(Date.now() - 60_000); + const childResultSavedAt = new Date(runStartedAt.getTime() + (savedRelativeToStart === "before" ? -30_000 : 30_000)); + await db.insert(issues).values({ id: childId, companyId, parentId: issueId, title: "Child ledger", status: "done", + priority: "medium", responsibleUserId: "responsible-user" }); + const [childResult] = await db.insert(agentWakeupRequests).values({ + companyId, agentId, source: "automation", triggerDetail: "system", reason: "issue_execution_deferred", + requestedByActorType: "system", requestedByActorId: `native-status-wake-dispatch:${randomUUID()}`, + status: "deferred_issue_execution", createdAt: childResultSavedAt, updatedAt: childResultSavedAt, + payload: { issueId, _paperclipWakeContext: { + issueId, wakeReason: "issue_children_completed", source: "native_status_decision", + nativeChildCompletionDecisionId: randomUUID(), completedChildIssueId: childId, childIssueIds: [childId], + childIssueSummaries: [{ id: childId, status: "done", summary: "Ledger saved." }], + } }, + }).returning(); + await db.insert(completionContracts).values({ + id: contractId, companyId, issueId, revision: 1, schemaVersion: "paperclip.completion-contract.v1", + policyVersion: "phase6-v4", risk: "low", completionAuthority: "agent_claim_policy", + incompleteCriteriaPolicy: "preserve_non_terminal", + contractJson: { revision: "1", objective: "Merge the child ledgers.", criteria: [{ id: "objective", requirement: "Merge the child ledgers." }] }, + canonicalSha256: `child-result-contract-${runId}`, createdByActorType: "system", createdByActorId: "test", + }); + await db.update(heartbeatRuns).set({ + runtimeMode: "native", nativeIssueId: issueId, nativeSessionId: runId, completionContractId: contractId, + completionContractSha256: `child-result-contract-${runId}`, runnerInstanceId, + startedAt: runStartedAt, finishedAt: new Date(), + }).where(eq(heartbeatRuns.id, runId)); + await db.update(issues).set({ executionRunId: runId, executionLockedAt: runStartedAt }).where(eq(issues.id, issueId)); + await db.insert(issueComments).values({ companyId, issueId, authorType: "agent", authorAgentId: agentId, + createdByRunId: runId, body: "Both children are done; merged their ledgers. Task complete." }); + await new PaperclipControlPlanePort(db, { + companyId, issueId, runId, agentId, sessionId: runId, completionContractId: contractId, + completionContractSha256: `child-result-contract-${runId}`, sourceInstanceId: runnerInstanceId, + controlPlaneSourceInstanceId: `child-result-control-${runId}`, + }).completeRun({ + result: { + schema: "paperclip.run_result.v1", reportedWorkDisposition: "done", + summary: "Both children are done; merged their ledgers. Task complete.", + completionClaim: { contractRevision: "1", objectiveSatisfied: true, + criteria: [{ criterionId: "objective", status: "satisfied", evidenceRefs: [] }], remainingWork: [] }, + evidence: [], verification: [], attentionRequests: [], artifacts: [], + }, + terminal: { schema: "paperclip.prp.terminal.v1", runTerminalState: "succeeded", + turnTerminalState: "completed", reportedWorkDisposition: "done" }, + }); + + await finalizeNativeRun({ db, runId, workspaceFinalizeStatus: "succeeded", projectRunStatus: true }); + await createWakeQueue(db, { + resolveResponsibleUserId: async () => "responsible-user", + getRoutineEnv: async () => ({ routineId: null, env: null, responsibleUserId: null }), + resolveSessionBeforeForWakeup: async () => null, + wakeAdmissionHelpers: {} as Parameters[1]["wakeAdmissionHelpers"], + recovery: { escalateStrandedAssignedIssue: async () => {}, escalateStrandedRecoveryIssueInPlace: async () => {} }, + }).releaseIssueExecution({ companyId, runId, now: new Date() }); + + expect(await db.select().from(statusDecisions).where(eq(statusDecisions.runId, runId))).toEqual([ + expect.objectContaining({ reasonCode: expectedReason, toStatus: expectedStatus, applicationState: "applied" }), + ]); + expect(await db.select().from(issues).where(eq(issues.id, issueId))) + .toEqual([expect.objectContaining({ status: expectedStatus })]); + const runs = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, companyId)); + const [wake] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, childResult!.id)); + if (savedRelativeToStart === "before") { + expect(wake).toMatchObject({ status: "cancelled", runId: null }); + expect(runs).toHaveLength(1); + } else { + const successor = runs.find((run) => run.id !== runId); + expect(runs).toHaveLength(2); + expect(wake).toMatchObject({ runId: successor!.id }); + expect(successor!.contextSnapshot).toMatchObject({ wakeReason: "issue_children_completed", + childIssueSummaries: [{ id: childId, status: "done", summary: "Ledger saved." }] }); + } + expect(mockAdapterExecute).not.toHaveBeenCalled(); + }); + it("delays a no-progress native continuation until the rewake cooldown instead of dropping it", async () => { const fixture = await seedNativePassiveBoardResponse("same_agent", { assignmentWake: true }); const finishedAt = new Date(Date.now() - 30_000); diff --git a/server/src/services/native-runtime/native-child-completion-delivery.ts b/server/src/services/native-runtime/native-child-completion-delivery.ts index 8c16296798e..2d0b79f1160 100644 --- a/server/src/services/native-runtime/native-child-completion-delivery.ts +++ b/server/src/services/native-runtime/native-child-completion-delivery.ts @@ -1,5 +1,5 @@ import { and, eq, inArray, isNull, ne, or, sql } from "drizzle-orm"; -import { agentWakeupRequests, type Db } from "@paperclipai/db"; +import { agentWakeupRequests, heartbeatRuns, type Db } from "@paperclipai/db"; export interface NativeChildCompletionRecipient { companyId: string; @@ -24,6 +24,8 @@ export async function hasPendingNativeChildCompletion(db: Db, input: NativeChild sql`${agentWakeupRequests.payload}->>'issueId' = ${input.issueId}`, sql`nullif(${agentWakeupRequests.payload}->'_paperclipWakeContext'->>'nativeChildCompletionDecisionId', '') is not null`, or(isNull(agentWakeupRequests.runId), ne(agentWakeupRequests.runId, input.runId)), + sql`${agentWakeupRequests.createdAt} >= coalesce((select ${heartbeatRuns.startedAt} from ${heartbeatRuns} + where ${heartbeatRuns.id} = ${input.runId} and ${heartbeatRuns.companyId} = ${input.companyId}), '-infinity'::timestamptz)`, input.sourceIntentId ? ne(agentWakeupRequests.id, input.sourceIntentId) : undefined, )).limit(1); return Boolean(pending);