Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix(community): since-mode response carries an older-side signal
Generc20b94756450
diff --git a/src/web/src/app/api/community/channels/[id]/messages/route.test.ts b/src/web/src/app/api/community/channels/[id]/messages/route.test.tsindex 4ccb88eaa..6a18f5046 100644--- a/src/web/src/app/api/community/channels/[id]/messages/route.test.ts+++ b/src/web/src/app/api/community/channels/[id]/messages/route.test.ts@@ -570,11 +570,17 @@ describe("GET /api/community/channels/[id]/messages", () => {messages: Array<{ id: string }>hasMoreNewer: booleannewerCursor?: string+ hasMoreOlder: boolean+ olderCursor?: stringlatestSeq: number}expect(body.messages.map((m) => m.id)).toEqual(["m_1", "m_2"])expect(body.hasMoreNewer).toBe(false)expect(body.latestSeq).toBe(2)+ // A since delta must carry an older-side signal so it can be paged back+ // through if it lands as the sole/oldest cache page (the DM-history bug).+ expect(body.hasMoreOlder).toBe(true)+ expect(body.olderCursor).toBe("2026-06-30T00:00:01.000Z|m_1")// Anchor-mode paths must not fire.expect(mockGetMessageInScope).not.toHaveBeenCalled()expect(mockListMessagesAround).not.toHaveBeenCalled()diff --git a/src/web/src/app/api/community/channels/[id]/messages/route.ts b/src/web/src/app/api/community/channels/[id]/messages/route.tsindex 7fe46c1df..1a49c000a 100644--- a/src/web/src/app/api/community/channels/[id]/messages/route.ts+++ b/src/web/src/app/api/community/channels/[id]/messages/route.ts@@ -63,9 +63,9 @@ export const GET = withAuth(async (req: NextRequest, ctx) => {since,limit: pageSize,})- const { items, hasMoreNewer, newerCursor } = buildSinceResponse(rows, pageSize)+ const { items, hasMoreNewer, newerCursor, hasMoreOlder, olderCursor } = buildSinceResponse(rows, pageSize)const { messages, latestSeq } = await enrichMessages(db, ctx.userId, { channelId }, items)- return writeJSON({ messages, hasMoreNewer, newerCursor, latestSeq })+ return writeJSON({ messages, hasMoreNewer, newerCursor, hasMoreOlder, olderCursor, latestSeq })}// Legacy branch (unchanged behavior beyond `latestSeq` addition): newest pagediff --git a/src/web/src/app/api/community/dm/[id]/messages/route.test.ts b/src/web/src/app/api/community/dm/[id]/messages/route.test.tsindex 5c93faa38..5a46bcd16 100644--- a/src/web/src/app/api/community/dm/[id]/messages/route.test.ts+++ b/src/web/src/app/api/community/dm/[id]/messages/route.test.ts@@ -388,11 +388,17 @@ describe("GET /api/community/dm/[id]/messages", () => {messages: Array<{ id: string }>hasMoreNewer: booleannewerCursor?: string+ hasMoreOlder: boolean+ olderCursor?: stringlatestSeq: number}expect(body.messages.map((m) => m.id)).toEqual(["m_1"])expect(body.hasMoreNewer).toBe(false)expect(body.latestSeq).toBe(1)+ // A since delta must carry an older-side signal so it can be paged back+ // through if it lands as the sole/oldest cache page (the DM-history bug).+ expect(body.hasMoreOlder).toBe(true)+ expect(body.olderCursor).toBe("2026-06-30T00:00:01.000Z|m_1")expect(mockGetMessageInScope).not.toHaveBeenCalled()expect(mockListMessagesAround).not.toHaveBeenCalled()})diff --git a/src/web/src/app/api/community/dm/[id]/messages/route.ts b/src/web/src/app/api/community/dm/[id]/messages/route.tsindex 8ed04deab..77b056f1c 100644--- a/src/web/src/app/api/community/dm/[id]/messages/route.ts+++ b/src/web/src/app/api/community/dm/[id]/messages/route.ts@@ -56,9 +56,9 @@ export const GET = withAuth(async (req: NextRequest, ctx) => {since,limit: pageSize,})- const { items, hasMoreNewer, newerCursor } = buildSinceResponse(rows, pageSize)+ const { items, hasMoreNewer, newerCursor, hasMoreOlder, olderCursor } = buildSinceResponse(rows, pageSize)const { messages, latestSeq } = await enrichMessages(db, ctx.userId, { channelId: dmId, isDm: true }, items)- return writeJSON({ messages, hasMoreNewer, newerCursor, latestSeq })+ return writeJSON({ messages, hasMoreNewer, newerCursor, hasMoreOlder, olderCursor, latestSeq })}const rows = await queries.communityMessage.listMessages(db, {diff --git a/src/web/src/lib/community/messages.test.ts b/src/web/src/lib/community/messages.test.tsindex 5c54b117b..264794247 100644--- a/src/web/src/lib/community/messages.test.ts+++ b/src/web/src/lib/community/messages.test.ts@@ -1,5 +1,52 @@import { describe, it, expect } from "vitest"-import { groupAttachments } from "./messages"+import { groupAttachments, buildSinceResponse } from "./messages"++// A since response is a forward delta, but it can land as the sole/oldest page+// in the cache (reconnect install, persister rehydrate). The client reads the+// older-side signal off the oldest page, so a since page MUST carry one or+// scroll-up dies and pre-delta history becomes unreachable (the DM-history+// bug). It reports the older edge honestly: the delta's oldest row is `since`+1,+// so everything at/before `since` is provably older.+describe("buildSinceResponse", () => {+ const row = (id: string, createdAt: string) => ({ id, createdAt })++ it("emits hasMoreOlder + olderCursor on a non-empty delta", () => {+ const rows = [+ row("m_1", "2026-07-01T00:00:01.000Z"),+ row("m_2", "2026-07-01T00:00:02.000Z"),+ ]+ const res = buildSinceResponse(rows, 10)+ expect(res.hasMoreOlder).toBe(true)+ // olderCursor points at the OLDEST returned row (items[0]).+ expect(res.olderCursor).toBe("2026-07-01T00:00:01.000Z|m_1")+ // newerCursor is absent — no probe row means no more newer.+ expect(res.hasMoreNewer).toBe(false)+ expect(res.newerCursor).toBeUndefined()+ })++ it("fabricates no older signal on an empty delta", () => {+ const res = buildSinceResponse([], 10)+ expect(res.hasMoreOlder).toBe(false)+ expect(res.olderCursor).toBeUndefined()+ expect(res.items).toHaveLength(0)+ })++ it("keeps the newer-side probe/slice while adding the older signal", () => {+ // pageSize 2 with 3 rows → hasMoreNewer true, probe sliced off, olderCursor+ // still keyed on the oldest KEPT row (not the dropped probe).+ const rows = [+ row("m_1", "2026-07-01T00:00:01.000Z"),+ row("m_2", "2026-07-01T00:00:02.000Z"),+ row("m_3", "2026-07-01T00:00:03.000Z"),+ ]+ const res = buildSinceResponse(rows, 2)+ expect(res.items.map((r) => r.id)).toEqual(["m_1", "m_2"])+ expect(res.hasMoreNewer).toBe(true)+ expect(res.newerCursor).toBe("2026-07-01T00:00:02.000Z|m_2")+ expect(res.hasMoreOlder).toBe(true)+ expect(res.olderCursor).toBe("2026-07-01T00:00:01.000Z|m_1")+ })+})// `groupAttachments` derives `url` from `r2Key` via the shared// `mediaUrlFromKey` helper — the DB row itself no longer carries a `url`diff --git a/src/web/src/lib/community/messages.ts b/src/web/src/lib/community/messages.tsindex 7b098ba48..4ee4872b2 100644--- a/src/web/src/lib/community/messages.ts+++ b/src/web/src/lib/community/messages.ts@@ -94,16 +94,43 @@ export function buildAnchorResponse<T extends { createdAt: string; id: string }>// Compose the since-mode response envelope. Rows arrive ASC with `+1` extra// probe; slice off the probe and encode `newerCursor` as the newest row.+//+// A since response is a FORWARD delta (rows strictly newer than the `since`+// cursor), designed to MERGE into an existing multi-page cache where the older+// edge is supplied by the pre-existing oldest page. But a since page can end up+// being the ONLY / oldest page (reconnect installs it as pages[0] with prior+// pages gc'd; or the persister rehydrates it standalone). When it does, the+// client reads the older-side signal off THIS page (`hasMoreOlder ?? hasMore ??+// false`) — and a since envelope carried none, so scroll-up silently died and+// all history before the delta became unreachable. Emit the older-side signal+// too: the delta's oldest row is `since`+1, so the `since` row and everything+// before it are provably older → `hasMoreOlder: true` + `olderCursor` = the+// oldest returned row. This is a true statement, not a fabrication, and it's+// inert on the healthy path (a since page there is pages[0]=newest, never the+// oldest page the client reads the older-signal from). Empty delta (no rows+// newer than `since`) fabricates nothing — same `items.length > 0` guard as+// `newerCursor`. Backward pagination terminates naturally once it reaches the+// true oldest row (that older fetch returns `hasMore: false`).export function buildSinceResponse<T extends { createdAt: string; id: string }>(rows: T[],pageSize: number,-): { items: T[]; hasMoreNewer: boolean; newerCursor: string | undefined } {+): {+ items: T[]+ hasMoreNewer: boolean+ newerCursor: string | undefined+ hasMoreOlder: boolean+ olderCursor: string | undefined+} {const hasMoreNewer = rows.length > pageSizeconst items = hasMoreNewer ? rows.slice(0, pageSize) : rowsconst newerCursor = hasMoreNewer && items.length > 0? `${items[items.length - 1].createdAt}|${items[items.length - 1].id}`: undefined- return { items, hasMoreNewer, newerCursor }+ const hasMoreOlder = items.length > 0+ const olderCursor = hasMoreOlder+ ? `${items[0].createdAt}|${items[0].id}`+ : undefined+ return { items, hasMoreNewer, newerCursor, hasMoreOlder, olderCursor }}// Build a paginated member response — same shape as buildPaginatedResponse butdiff --git a/src/web/src/lib/query-persister.test.ts b/src/web/src/lib/query-persister.test.tsindex 458dfcfdc..09cf920a7 100644--- a/src/web/src/lib/query-persister.test.ts+++ b/src/web/src/lib/query-persister.test.ts@@ -304,6 +304,34 @@ describe("isTrustedMessagesPageZero", () => {).toBe(false)})+ it("rejects a tail-attached page that carries NO older-side signal", () => {+ // hasMoreNewer===false alone is not enough — without an older-side signal+ // the next mount computes `hasMoreOlder ?? hasMore ?? false === false` and+ // scroll-up dies. This is the exact shape the DM-history bug rehydrated.+ expect(+ isTrustedMessagesPageZero({+ messages: [],+ hasMoreNewer: false,+ newerCursor: undefined,+ latestSeq: 0,+ }),+ ).toBe(false)+ })++ it("trusts a since page once it carries the older-side signal (post-fix shape)", () => {+ // buildSinceResponse now emits hasMoreOlder + olderCursor, so even a since+ // delta that lands as the standalone tail can be paged back through.+ expect(+ isTrustedMessagesPageZero({+ messages: [],+ hasMoreNewer: false,+ hasMoreOlder: true,+ olderCursor: "cur_older",+ latestSeq: 0,+ }),+ ).toBe(true)+ })+it("rejects anchor-mode envelopes that still have newer history above (hasMoreNewer=true)", () => {expect(isTrustedMessagesPageZero({diff --git a/src/web/src/lib/query-persister.ts b/src/web/src/lib/query-persister.tsindex 72c6b84cd..7e8e5b562 100644--- a/src/web/src/lib/query-persister.ts+++ b/src/web/src/lib/query-persister.ts@@ -95,7 +95,19 @@ export function isTrustedMessagesPageZero(page: MessagesPage | undefined): boolepage.hasMoreOlder === undefined &&page.hasMoreNewer === undefinedif (isLegacyNewest) return true- if (page.hasMoreNewer === false) return true+ // Defense-in-depth (paired with buildSinceResponse now emitting an older-side+ // signal): a page is only a trustworthy standalone tail if the NEXT mount can+ // read back through it. `hasMoreNewer === false` alone isn't enough — a since+ // page carried that yet lacked any older signal, so rehydrating it as the+ // sole page stranded scroll-up (the bug this guards). Require an older-side+ // signal (`hasMoreOlder`/`hasMore` present) so a page that can't self-report+ // its older edge never survives to disk, whatever produced it.+ if (+ page.hasMoreNewer === false &&+ (page.hasMoreOlder !== undefined || page.hasMore !== undefined)+ ) {+ return true+ }return false}