Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): retry transient D1 blips on the agent send/pull/ack routes

Claudette#987309fa367a7c68
diff --git a/src/web/src/app/api/community/agent/ack/route.ts b/src/web/src/app/api/community/agent/ack/route.ts
index 5b28e4d74..b7d301b0b 100644
--- a/src/web/src/app/api/community/agent/ack/route.ts
+++ b/src/web/src/app/api/community/agent/ack/route.ts
@@ -1,5 +1,5 @@
import { NextResponse, type NextRequest } from "next/server"
-import { queries, CommunityAgentAckRequestSchema } from "@alook/shared"
+import { queries, withD1Retry, CommunityAgentAckRequestSchema } from "@alook/shared"
import { getDb } from "@/lib/db"
import { withAgentRunnerAuth } from "@/lib/middleware/community-agent-runner-auth"
import { resolveTargetForMember, resolveErrorResponse } from "@/lib/community/resolve-ref"
@@ -47,7 +47,13 @@ export const POST = withAgentRunnerAuth(async (req: NextRequest, ctx) => {
if (!gate.ok) return NextResponse.json({ error: gate.error }, { status: gate.status })
}
- const bumped = await queries.communityReadState.bumpReadCursor(db, ctx.botUserId, scopeTarget, cursor.seq)
+ // Idempotent write — safe to retry a transient D1 blip (same treatment as
+ // the human-facing routes). A real "no such seq" is a structured `false`
+ // return below, not a thrown error, so it is never retried.
+ const bumped = await withD1Retry(
+ () => queries.communityReadState.bumpReadCursor(db, ctx.botUserId, scopeTarget, cursor.seq),
+ { route: "community/agent/ack:bump" },
+ )
if (!bumped) {
return NextResponse.json(
{ error: `no message with seq #${cursor.seq} in ${cursor.channel}` },
diff --git a/src/web/src/app/api/community/agent/inboxPull/route.test.ts b/src/web/src/app/api/community/agent/inboxPull/route.test.ts
index d01b55fe1..a5875141d 100644
--- a/src/web/src/app/api/community/agent/inboxPull/route.test.ts
+++ b/src/web/src/app/api/community/agent/inboxPull/route.test.ts
@@ -100,4 +100,27 @@ describe("POST /api/community/agent/inboxPull", () => {
expect(res.status).toBe(200)
expect(mockListUnreadMessagesForAgent).toHaveBeenCalledWith(expect.anything(), "bot_1", { max: 201 })
})
+
+ it("retries a transient D1 error and succeeds (withD1Retry wraps the pull reads)", async () => {
+ // First call throws a retryable transient (`internal error; reference` is
+ // on isRetryableD1Error's list), second succeeds — the route should NOT
+ // 500, it should retry and return the messages.
+ mockListUnreadMessagesForAgent
+ .mockRejectedValueOnce(new Error("D1_ERROR: internal error; reference = abc123"))
+ .mockResolvedValueOnce([{ id: "m_1" }])
+ const res = await POST(req(JSON.stringify({ max: 5 }), { Authorization: "Bearer crk_abc" }))
+ expect(res.status).toBe(200)
+ expect((await res.json()).messages).toHaveLength(1)
+ expect(mockListUnreadMessagesForAgent).toHaveBeenCalledTimes(2)
+ })
+
+ it("a NON-retryable error is not retried and surfaces (no silent swallow)", async () => {
+ // A logic error (not a transient) must propagate, not be retried into a
+ // false success. withD1Retry rethrows non-retryable errors immediately.
+ mockListUnreadMessagesForAgent.mockRejectedValue(new Error("SQLITE_CONSTRAINT: bad column"))
+ await expect(POST(req(JSON.stringify({ max: 5 }), { Authorization: "Bearer crk_abc" }))).rejects.toThrow(
+ /SQLITE_CONSTRAINT/,
+ )
+ expect(mockListUnreadMessagesForAgent).toHaveBeenCalledTimes(1)
+ })
})
diff --git a/src/web/src/app/api/community/agent/inboxPull/route.ts b/src/web/src/app/api/community/agent/inboxPull/route.ts
index 6d09cfd97..90c999389 100644
--- a/src/web/src/app/api/community/agent/inboxPull/route.ts
+++ b/src/web/src/app/api/community/agent/inboxPull/route.ts
@@ -1,5 +1,5 @@
import { NextResponse, type NextRequest } from "next/server"
-import { queries, CommunityAgentInboxPullRequestSchema } from "@alook/shared"
+import { queries, withD1Retry, CommunityAgentInboxPullRequestSchema } from "@alook/shared"
import { getDb } from "@/lib/db"
import { withAgentRunnerAuth } from "@/lib/middleware/community-agent-runner-auth"
@@ -35,7 +35,16 @@ export const POST = withAgentRunnerAuth(async (req: NextRequest, ctx) => {
const max = Math.min(parsed.data.max ?? MAX_PULL, MAX_PULL)
// Fetch one extra row to detect whether more unread remain beyond `max`.
- const rows = await queries.communityAgentInbox.listUnreadMessagesForAgent(db, ctx.botUserId, { max: max + 1 })
+ // Each D1 call is wrapped in withD1Retry so a transient miniflare/D1 blip
+ // (`internal error; reference`, on isRetryableD1Error's list) is retried
+ // instead of surfacing as a hard 500 to the bot — the same armor the
+ // human-facing bootstrap route already has. A non-transient throw still
+ // propagates (withD1Retry rethrows once retries are exhausted / on a
+ // non-retryable error).
+ const rows = await withD1Retry(
+ () => queries.communityAgentInbox.listUnreadMessagesForAgent(db, ctx.botUserId, { max: max + 1 }),
+ { route: "community/agent/inboxPull:list-unread" },
+ )
const hasMore = rows.length > max
const page = hasMore ? rows.slice(0, max) : rows
@@ -43,7 +52,10 @@ export const POST = withAgentRunnerAuth(async (req: NextRequest, ctx) => {
// rows (message_id = NULL) never match this inArray, so agent-uploaded
// pending rows are naturally excluded from the inbox.
const messageIds = page.map((r) => r.id)
- const attachmentRows = await queries.communityAttachment.listByMessageIds(db, messageIds)
+ const attachmentRows = await withD1Retry(
+ () => queries.communityAttachment.listByMessageIds(db, messageIds),
+ { route: "community/agent/inboxPull:attachments" },
+ )
const attachmentsByMessageId = new Map<string, Array<{ id: string; filename: string; contentType: string | null; size: number | null }>>()
for (const a of attachmentRows) {
if (!a.messageId) continue
@@ -52,11 +64,14 @@ export const POST = withAgentRunnerAuth(async (req: NextRequest, ctx) => {
attachmentsByMessageId.set(a.messageId, list)
}
- const messages = await queries.communityAgentInbox.toAgentMessages(
- db,
- page,
- ctx.botUserId,
- attachmentsByMessageId,
+ const messages = await withD1Retry(
+ () => queries.communityAgentInbox.toAgentMessages(
+ db,
+ page,
+ ctx.botUserId,
+ attachmentsByMessageId,
+ ),
+ { route: "community/agent/inboxPull:hydrate" },
)
return NextResponse.json({ messages, hasMore })
})
diff --git a/src/web/src/app/api/community/agent/send/route.ts b/src/web/src/app/api/community/agent/send/route.ts
index 28265fc19..ed880b0ce 100644
--- a/src/web/src/app/api/community/agent/send/route.ts
+++ b/src/web/src/app/api/community/agent/send/route.ts
@@ -1,5 +1,5 @@
import { NextResponse, type NextRequest } from "next/server"
-import { queries, CommunityAgentSendRequestSchema } from "@alook/shared"
+import { queries, withD1Retry, CommunityAgentSendRequestSchema } from "@alook/shared"
import { getDb } from "@/lib/db"
import { withAgentRunnerAuth } from "@/lib/middleware/community-agent-runner-auth"
import { resolveTargetForMember, resolveErrorResponse } from "@/lib/community/resolve-ref"
@@ -57,16 +57,31 @@ export const POST = withAgentRunnerAuth(async (req: NextRequest, ctx) => {
// would block it forever while the pull hands over nothing to advance its
// `lastReadSeq` — a permanent wedge. `latestSeq` is still fetched: it feeds
// the optimistic `expectedSeq` claim below and the `blocked` response shape.
+ // Each D1 call is wrapped in withD1Retry so a transient miniflare/D1 blip
+ // retries instead of hard-500ing the bot (same armor as bootstrap). The
+ // alignment DECISION (blocked/unaligned) and the CAS 409 below are
+ // structured returns, not thrown errors, so they never enter the retry —
+ // only genuine D1 exceptions do (Cecilia red line: never retry business
+ // failures).
const [latestSeq, readState] = await Promise.all([
- queries.communityAgentInbox.getLatestSeqForScope(db, resolved.channelId),
- queries.communityReadState.getReadState(db, { userId: ctx.botUserId, ...scopeTarget }),
+ withD1Retry(
+ () => queries.communityAgentInbox.getLatestSeqForScope(db, resolved.channelId),
+ { route: "community/agent/send:latest-seq" },
+ ),
+ withD1Retry(
+ () => queries.communityReadState.getReadState(db, { userId: ctx.botUserId, ...scopeTarget }),
+ { route: "community/agent/send:read-state" },
+ ),
])
const seen = body.seenUpToSeq ?? readState?.lastReadSeq ?? 0
- const hasUnread = await queries.communityAgentInbox.hasDeliverableUnreadForAgentScope(
- db,
- ctx.botUserId,
- resolved.channelId,
- seen,
+ const hasUnread = await withD1Retry(
+ () => queries.communityAgentInbox.hasDeliverableUnreadForAgentScope(
+ db,
+ ctx.botUserId,
+ resolved.channelId,
+ seen,
+ ),
+ { route: "community/agent/send:has-unread" },
)
if (hasUnread) {
return NextResponse.json({
@@ -110,11 +125,14 @@ export const POST = withAgentRunnerAuth(async (req: NextRequest, ctx) => {
// Any mismatch returns the same generic 400, no leakage of which id failed.
const attachmentTargetId = target.channelId
if (body.attachments.length > 0) {
- const rows = await queries.communityAttachment.findPendingAttachmentsForBot(db, {
- ids: body.attachments,
- uploaderId: ctx.botUserId,
- targetId: attachmentTargetId,
- })
+ const rows = await withD1Retry(
+ () => queries.communityAttachment.findPendingAttachmentsForBot(db, {
+ ids: body.attachments,
+ uploaderId: ctx.botUserId,
+ targetId: attachmentTargetId,
+ }),
+ { route: "community/agent/send:attachments" },
+ )
if (rows.length !== body.attachments.length) {
return NextResponse.json(
{ error: "attachment not found or not attachable to this target" },
@@ -130,10 +148,13 @@ export const POST = withAgentRunnerAuth(async (req: NextRequest, ctx) => {
// nothing is posted.
let replyToId: string | undefined
if (body.replyToSeq !== undefined) {
- const replyTarget = await queries.communityMessage.getMessageByChannelAndSeq(
- db,
- scopeTarget,
- body.replyToSeq,
+ const replyTarget = await withD1Retry(
+ () => queries.communityMessage.getMessageByChannelAndSeq(
+ db,
+ scopeTarget,
+ body.replyToSeq!,
+ ),
+ { route: "community/agent/send:reply-lookup" },
)
if (!replyTarget) {
return NextResponse.json(
@@ -151,6 +172,11 @@ export const POST = withAgentRunnerAuth(async (req: NextRequest, ctx) => {
// shape the gate above returns, with a freshly re-fetched `latestSeq` (the
// stale one is now off-by-at-least-one). The daemon's existing "blocked →
// inbox pull → retry" handling needs no changes for this (plan §4).
+ // NOT wrapped in withD1Retry: createCommunityMessage is a CAS-guarded write
+ // (`expectedSeq`). Blindly retrying it on a transient could re-attempt a
+ // partially-applied claim; its own conflict handling returns a structured
+ // 409 (below) which the daemon already realigns on. Its internal transient
+ // resilience, if any, belongs inside the handler, not a blanket outer retry.
const result = await createCommunityMessage({
db,
authorId: ctx.botUserId,
@@ -162,7 +188,10 @@ export const POST = withAgentRunnerAuth(async (req: NextRequest, ctx) => {
})
if (!result.ok) {
if (result.status === 409) {
- const freshLatestSeq = await queries.communityAgentInbox.getLatestSeqForScope(db, resolved.channelId)
+ const freshLatestSeq = await withD1Retry(
+ () => queries.communityAgentInbox.getLatestSeqForScope(db, resolved.channelId),
+ { route: "community/agent/send:fresh-latest-seq" },
+ )
return NextResponse.json({
state: "blocked",
reason: "unaligned",