From 97dcec42262e666bd1db42c0a31e9028d4102409 Mon Sep 17 00:00:00 2001 From: Dotta Date: Fri, 30 Oct 2026 22:42:23 -0500 Subject: [PATCH] test: await resolved interaction execution before outcome assertions Observe the target adapter-start event, then await its existing run-specific completion barrier. Preserve succeeded, execution-count and review-promotion assertions, including deliberately slow completion beyond the old terminal poll. Co-Authored-By: Paperclip --- ...heartbeat-stale-queue-invalidation.test.ts | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/server/src/__tests__/heartbeat-stale-queue-invalidation.test.ts b/server/src/__tests__/heartbeat-stale-queue-invalidation.test.ts index 65191c24d2f..f1447508ed5 100644 --- a/server/src/__tests__/heartbeat-stale-queue-invalidation.test.ts +++ b/server/src/__tests__/heartbeat-stale-queue-invalidation.test.ts @@ -1796,8 +1796,22 @@ describeEmbeddedPostgres("heartbeat stale queued-run invalidation", () => { const interactionId = randomUUID(); const wakeCommentId = randomUUID(); let claimedIssue: { status: string; executionRunId: string | null } | null = null; + let resumedRunId: string | undefined; + let signalAdapterStarted!: () => void; + const adapterStarted = new Promise(resolve => { signalAdapterStarted = resolve; }); + const execute = mockAdapterExecute.getMockImplementation()!; + mockAdapterExecute.mockImplementation(async (...args: unknown[]) => { + if ((args[0] as { runId: string }).runId === resumedRunId) { + signalAdapterStarted(); + // A claimed continuation can finish after the old terminal poll. + if (interactionStatus === "rejected" && interactionKind === "connection_intent") { + await new Promise(resolve => setTimeout(resolve, 3100)); + } + } + return execute(); + }); afterContinuationDispatchCheck = async ({ runId: checkedRunId, issueId: checkedIssueId }) => { - if (checkedIssueId !== issueId) return; + if (checkedIssueId !== issueId || checkedRunId !== resumedRunId) return; claimedIssue = await db .select({ status: issues.status, executionRunId: issues.executionRunId }) .from(issues) @@ -1831,13 +1845,14 @@ describeEmbeddedPostgres("heartbeat stale queued-run invalidation", () => { contextExtras: { interactionId, interactionKind, interactionStatus, wakeCommentId, originCommentIds: [wakeCommentId], interactionResolvedAt: new Date().toISOString(), mutation: "interaction", source: `${interactionKind}.resolved`, forceFreshSession: true } }); + resumedRunId = runId; await heartbeat.resumeQueuedRuns(); - expect(await waitForCondition(async () => (await db.select({ status: heartbeatRuns.status }).from(heartbeatRuns).where(eq(heartbeatRuns.id, runId)))[0]?.status === "succeeded")).toBe(true); - // Terminal status precedes completion bookkeeping. Drain those writes before - // afterEach truncates the fixture, otherwise PostgreSQL can deadlock. + // Adapter entry guarantees this exact execution is registered. Await its + // owning completion barrier before terminal assertions and fixture cleanup. + await adapterStarted; await heartbeat.waitForRunExecutionDrain(runId); + expect((await db.select({ status: heartbeatRuns.status }).from(heartbeatRuns).where(eq(heartbeatRuns.id, runId)))[0]?.status).toBe("succeeded"); expect(countExecuteCallsForRun(runId)).toBe(1); - await waitForCondition(async () => claimedIssue !== null); expect(claimedIssue).toEqual({ status: "in_progress", executionRunId: runId }); }); });