From 28c898e93504318056d7a6ab2bbc5eafa6b639ae Mon Sep 17 00:00:00 2001 From: Dotta Date: Wed, 28 Oct 2026 12:46:43 -0500 Subject: [PATCH] Wake ordinary notices when unadmitted Retry input is restored Register intent in the restoring transaction before its guarded receipt update. Prove reactivation after the earlier wake has drained, and retain current Retry compatibility. Co-Authored-By: Paperclip --- ...-10-25-inbound-wake-notices-sleep-audit.md | 40 +++++++++++++ .../chat-channels.integration.test.ts | 58 +++++++++++++++++++ .../chat-channels/task-retry-input.ts | 2 + 3 files changed, 100 insertions(+) diff --git a/doc/plans/2026-10-25-inbound-wake-notices-sleep-audit.md b/doc/plans/2026-10-25-inbound-wake-notices-sleep-audit.md index 58b2921bd9a..eb6520b32da 100644 --- a/doc/plans/2026-10-25-inbound-wake-notices-sleep-audit.md +++ b/doc/plans/2026-10-25-inbound-wake-notices-sleep-audit.md @@ -515,3 +515,43 @@ is `/tmp/sleep30-discovery-{red,green}.log`, and acceptance logs/status receipts are `/tmp/sleep30-discovery-final-{consumers,notices,retries}.*`. The earlier 264-case acceptance retains its original revision scope. None of these runs establishes full local repository acceptance or deployed sleep. + + +### Review repair: restore ordinary Retry input in its notifying transaction + +The fresh review of `661f2f6da8c6c22cdcc25edccd57502ee6fa8c4b` reports +[restored input stays silent](https://github.com/paperclipai/paperclip/pull/17647#discussion_r4404007181), +P1/4 out of 5. Earlier retry admission and denial notifications do not cover +`releaseUnadmittedTaskRetryInput`: reconciliation invokes this owner after the +failed action settles, and its separate transaction restores coalesced ordinary +receipts to deferred status. A consumed earlier wake cannot cover that later +commit. The earlier writer audit missed this restoration transaction. + +The restoration owner now awaits the existing company/task notice intent after +its action/issue/receipt guards and before updating the ordinary receipts. The +same transaction restores input and retires its unadmitted task Retry intent. +No guard, source authority, table, scheduler or per-caller wrapper is added. + +The causal regression seeds an unadmitted failed Retry owner and uses the real +input-claim transaction on supported ordinary chat input. It starts the actual +notice coordinator, consumes an earlier committed scope wake, and observes no +notice and two virtual minutes without select/transaction calls. Calling the +real release owner then exposes the restored row. Before the fix, the test fails +because this transaction emitted no intent. After the fix, intent/settlement and +the queued publication appear, and the coordinator settles without a deadline. +This is real PostgreSQL producer/consumer boundary proof with seeded durable +Retry-refusal state, not a complete public Retry request failure walkthrough. +The separate existing GitHub Retry compatibility cases exercise supported flows. + +Current restoration acceptance passes **126 distinct selected cases**: 54 +ordinary-notice/FIFO/producer cases (1,598 skipped) and 72 separate retry/budget +cases (1,580 skipped), using fresh independent databases. Server typechecking, +build and module boundaries pass. The prior 267-case discovery matrix retains +its original revision scope; unchanged complete consumer suites are not counted +as freshly rerun for this two-line producer hook. +Commands/fixtures: `/tmp/sleep30-restore-input-final-acceptance-command.json`. +The valid missing-intent failure remains in `/tmp/sleep30-restore-input-red.log`; +final results/exit receipts are `/tmp/sleep30-restore-input-final-{notices,retries}.*` +and `/tmp/sleep30-restore-input-{types,build,boundaries}.*`. +Fresh current-head remote CI and Greptile 5/5 remain mandatory. No full local +repository acceptance, live provider wake or physical suspension is claimed. diff --git a/server/src/__tests__/chat-channels.integration.test.ts b/server/src/__tests__/chat-channels.integration.test.ts index 90be8a3a010..29d1b0db701 100644 --- a/server/src/__tests__/chat-channels.integration.test.ts +++ b/server/src/__tests__/chat-channels.integration.test.ts @@ -5,6 +5,7 @@ import { cloudActorHeaderSourceFromHeaders, resolveCloudTenantActor } from "../m import { cloudTenantPrimaryCompanyId } from "../services/cloud-instance.js"; import { accessService } from "../services/access.js"; import { signalDatabaseWork, subscribeDatabaseWork } from "@paperclipai/db"; +import { claimTaskRetryInput, releaseUnadmittedTaskRetryInput } from "../services/chat-channels/task-retry-input.js"; import { restoreNativeChatReviewPresentationInTransaction } from "../services/native-runtime/native-chat-review-presentation.js"; import { notifyChatEndpointConfiguration, notifyChatCompanyConfiguration } from "../services/chat-channels/configuration-work-notifications.js"; import { deleteCompany } from "../services/company-deletion.js"; @@ -73881,6 +73882,63 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => { } }); + it("reactivates restored ordinary input after an earlier Retry notice wake is consumed", async () => { + const context = await deferredChatQueueFixture(); + const retryId = randomUUID(), intentId = randomUUID(); + const scope = { companyId: context.fixture.companyId, issueId: context.conversation.issueId }; + const errors: unknown[] = [], events: string[] = []; + const coordinator = createDeliveryWorkCoordinator({ owner: db, canRun: () => true, + canReconcile: () => true, onError: error => errors.push(error) }); + let unsubscribe = () => {}; + try { + const [receipt] = await readAgentWakeupRequestsById(context.action.id); + await db.insert(chatActions).values({ ...context.action, id: retryId, + kind: "failed_run_retry", status: "failed", providerActionId: `task-retry-restore:${retryId}`, + payload: { issueId: scope.issueId, taskRetryIntentId: intentId }, + }); + await db.insert(agentWakeupRequests).values({ ...receipt, id: retryId, status: "failed", + idempotencyKey: `task-retry-restore:${retryId}`, + }); + await db.transaction(tx => claimTaskRetryInput(tx, { companyId: scope.companyId, + retryActionId: retryId, sourceActionIds: [context.action.id] })); + expect((await readAgentWakeupRequestsById(context.action.id))[0]).toMatchObject({ + status: "coalesced", runId: null, payload: { coalescedIntoWakeupRequestId: retryId }, + }); + vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"], shouldAdvanceTime: true }); + const settle = async () => vi.waitFor(async () => { + await vi.advanceTimersByTimeAsync(1000); + expect(idleWorkSnapshot().active).toBe(0); expect(coordinator.nextWakeAt()).toBeNull(); + }, { timeout: 10_000 }); + await chatInboundWakeNoticeWork(context.service).register(coordinator, () => true).ready; + await db.transaction(tx => signalDatabaseWork(tx, DELIVERY_QUEUES.chatInboundWakeNotices, JSON.stringify(scope))); + await settle(); + expect(await readChatPublicationsByEndpointId(context.endpoint.id)).toEqual([]); + const select = vi.spyOn(db, "select"), transaction = vi.spyOn(db, "transaction"); + try { + await vi.advanceTimersByTimeAsync(120_000); + expect(select).not.toHaveBeenCalled(); expect(transaction).not.toHaveBeenCalled(); + } finally { select.mockRestore(); transaction.mockRestore(); } + unsubscribe = subscribeDatabaseWork(db, DELIVERY_QUEUES.chatInboundWakeNotices, (event, scopes) => { + if ([...scopes ?? []].some(value => value !== "*" && JSON.parse(value).companyId === scope.companyId)) events.push(event); + }); + await releaseUnadmittedTaskRetryInput(db, { companyId: scope.companyId, retryActionId: retryId }); + expect((await readAgentWakeupRequestsById(context.action.id))[0]).toMatchObject({ status: "deferred_issue_execution", runId: null }); + expect(events).toContain("intent"); expect(events).toContain("settled"); + await vi.waitFor(async () => { + await vi.advanceTimersByTimeAsync(1000); + expect(await readChatPublicationsByEndpointId(context.endpoint.id)).toEqual([expect.objectContaining({ + commentId: context.action.payload.commentId, payload: { text: "Your follow-up is queued.", progressState: "queued" }, + })]); + expect(coordinator.nextWakeAt()).toBeNull(); expect(idleWorkSnapshot().active).toBe(0); + }, { timeout: 10_000 }); + expect(errors).toEqual([]); + } finally { + unsubscribe(); await coordinator.stop(); vi.useRealTimers(); await context.service.shutdown(); + await db.delete(chatActions).where(eq(chatActions.id, retryId)); + await db.delete(agentWakeupRequests).where(eq(agentWakeupRequests.id, retryId)); + } + }); + it("isolates pre-run FIFO reconciliation scopes and exhausts published history", async () => { const first = await deferredChatQueueFixture(); const second = await deferredChatQueueFixture(); diff --git a/server/src/services/chat-channels/task-retry-input.ts b/server/src/services/chat-channels/task-retry-input.ts index 200f77bda1c..71743f42ad4 100644 --- a/server/src/services/chat-channels/task-retry-input.ts +++ b/server/src/services/chat-channels/task-retry-input.ts @@ -2,6 +2,7 @@ import { z } from "zod"; import { and, asc, eq, gt, inArray, isNull, or, sql } from "drizzle-orm"; import { agentWakeupRequests, chatActions, chatConversations, chatEndpoints, heartbeatRuns, issues, type Db } from "@paperclipai/db"; import type { DbOrTransaction } from "./runtime-types.js"; +import { notifyChatInboundWakeNotices } from "../chat-inbound-wake-notice-notifications.js"; export const taskRetryInputSchema = z.object({ // This is a server-selected snapshot of the durable queue, not client input. @@ -89,6 +90,7 @@ export async function releaseUnadmittedTaskRetryInput(db: Db, input: { companyId eq(agentWakeupRequests.companyId, input.companyId), eq(agentWakeupRequests.id, input.retryActionId), )).for("update"); if (receipt?.runId) return; + await notifyChatInboundWakeNotices(tx, { companyId: input.companyId, issueId: issue.id }); await tx.update(agentWakeupRequests).set({ status: "deferred_issue_execution", finishedAt: null, payload: sql`${agentWakeupRequests.payload} - 'coalescedIntoWakeupRequestId'`, updatedAt: new Date(), }).where(and(eq(agentWakeupRequests.companyId, input.companyId), eq(agentWakeupRequests.status, "coalesced"),