diff --git a/server/src/modules/wake-queue/adapters/postgres.ts b/server/src/modules/wake-queue/adapters/postgres.ts index 7e92e667918..471e7a00088 100644 --- a/server/src/modules/wake-queue/adapters/postgres.ts +++ b/server/src/modules/wake-queue/adapters/postgres.ts @@ -1375,7 +1375,7 @@ export function createPostgresWakeQueueAdapter(db: Db, deps: WakeQueuePostgresAd issueStatus: issueRow?.status ?? "", hasAssigneeUser: Boolean(issueRow?.assigneeUserId), assigneeAgentMatchesRunAgent: issueRow?.assigneeAgentId === run.agentId, - legacyExecutionNeedsReconciliation: evidence.kind === "complete" && + legacyExecutionNeedsReconciliation: evidence.kind === "complete" && !agentRunSupersededByFreshContinuation(run) && await legacyExecutionNeedsReconciliationWithEvidence(tx as unknown as Db, evidence.run), // An operator stop never promotes old queued work by itself. The // next explicit wake adopts those messages atomically when it diff --git a/server/src/services/fresh-continuation-fallback.test.ts b/server/src/services/fresh-continuation-fallback.test.ts index d49d055867d..e4f65754f30 100644 --- a/server/src/services/fresh-continuation-fallback.test.ts +++ b/server/src/services/fresh-continuation-fallback.test.ts @@ -995,6 +995,33 @@ suite("fresh-continuation fallback", () => { .toMatchObject({ kind: "continued", delivery: taken ? "recorded" : "saved_input" }); }, 60_000); + it.each(["native", "legacy"] as const)("starts a run for the person's message once the task continues from saved work (%s)", async runtime => { + const f = await unfinishedRun({ runtime, errorCode: runtime === "native" ? "native_provider_terminal_failed" : "process_lost" }); + const [comment] = await db.insert(issueComments).values({ companyId, issueId: f.issueId, authorType: "user", + authorUserId: userId, body: "try again" } as typeof issueComments.$inferInsert).returning(); + const heartbeat = heartbeatService(db); + await heartbeat.wakeup(agentId, { source: "automation", triggerDetail: "system", reason: "issue_commented", + payload: { issueId: f.issueId, commentId: comment!.id, mutation: "comment" }, + requestedByActorType: "user", requestedByActorId: userId, + contextSnapshot: { issueId: f.issueId, taskId: f.issueId, commentId: comment!.id, wakeCommentId: comment!.id, + source: "issue.comment", wakeReason: "issue_commented" } }); + const wakes = () => db.select().from(agentWakeupRequests).where(sql`${agentWakeupRequests.payload}->>'issueId' = ${f.issueId}`); + expect((await wakes()).map(w => w.status)).toEqual(["deferred_issue_execution"]); + expect(await ensureFreshContinuation(db, { companyId, runId: f.runId, trigger: "recovery_refused" })) + .toMatchObject({ kind: "continued", delivery: "saved_input" }); + await occupySlot(); + await deliver(); + await heartbeat.resumeQueuedRuns(); + const runs = await db.select().from(heartbeatRuns).where(and( + sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${f.issueId}`, sql`${heartbeatRuns.id} <> ${f.runId}`)); + const saved = await wakes(); + expect(runs, JSON.stringify({ issue: await issue(f.issueId), + wakes: saved.map(w => ({ status: w.status, error: w.error, runId: w.runId, payload: w.payload })) })).toHaveLength(1); + expect(runs[0]!.contextSnapshot?.wakeCommentIds).toContain(comment!.id); + expect(saved).toHaveLength(1); + expect(saved[0]!.runId).toBe(runs[0]!.id); + }, 60_000); + it.each([ { title: "a pending approval", setup: "approval" as const, errorCode: "native_continuation_requires_reconciliation" }, { title: "a credential the person has not shared", setup: "none" as const, errorCode: "configuration_incomplete" },