Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): enforce monotone read-state at the writer level

Generbed8ce7b5d53
diff --git a/src/shared/src/db/queries/community/read-state.ts b/src/shared/src/db/queries/community/read-state.ts
index 155a15118..368b75b8d 100644
--- a/src/shared/src/db/queries/community/read-state.ts
+++ b/src/shared/src/db/queries/community/read-state.ts
@@ -1,4 +1,4 @@
-import { eq, and, inArray, sql } from "drizzle-orm";
+import { eq, and, inArray, lt, sql } from "drizzle-orm";
import {
communityReadState,
communityChannel,
@@ -90,6 +90,18 @@ export function markReadToMessageBuilder(
);
}
+ // Monotone guard: `setWhere` requires the incoming `lastReadAt` to be
+ // strictly greater than the row's current value. If a stale client PUT
+ // arrives (channel switch → return, remounted `useChannelWatermark`
+ // resets its local `maxSeen` and picks an older mid-viewport row as
+ // its first advance), the UPDATE portion no-ops and the existing row
+ // wins. INSERT (no row yet) is unaffected — you can't regress what
+ // doesn't exist. Sibling pattern to `createMessage`'s author-watermark
+ // upsert, which guards on `lastReadSeq < seq` for the same reason.
+ //
+ // Timestamp comparison is safe: `lastReadAt` is a TEXT ISO-8601 string
+ // (see schema note) and SQLite compares those lexicographically, which
+ // matches temporal order for ISO-8601.
if (channelId) {
return db
.insert(communityReadState)
@@ -107,6 +119,7 @@ export function markReadToMessageBuilder(
lastReadAt: message.createdAt,
lastReadMessageId: message.id,
},
+ setWhere: sql`${communityReadState.lastReadAt} < ${message.createdAt}`,
});
}
@@ -126,6 +139,7 @@ export function markReadToMessageBuilder(
lastReadAt: message.createdAt,
lastReadMessageId: message.id,
},
+ setWhere: sql`${communityReadState.lastReadAt} < ${message.createdAt}`,
});
}
@@ -231,11 +245,22 @@ export async function markAllServerChannelsRead(
// fires on user click "Mark all read", not in a hot loop). Alternative
// would be a `CASE WHEN ...` bulk UPDATE, which is uglier and only wins
// above ~50 channels.
+ //
+ // Monotone guard mirrors `markReadToMessageBuilder`: only advance rows
+ // whose current `lastReadAt` is strictly older than the channel's
+ // latest. If a stale row happens to already sit ahead of the current
+ // latest (rare — usually only under concurrent writes or right after
+ // a message delete), leave it alone rather than regressing.
for (const u of toUpdate) {
await db
.update(communityReadState)
.set({ lastReadAt: u.createdAt, lastReadMessageId: u.msgId })
- .where(eq(communityReadState.id, u.id));
+ .where(
+ and(
+ eq(communityReadState.id, u.id),
+ lt(communityReadState.lastReadAt, u.createdAt)
+ )
+ );
}
if (toInsert.length > 0) {
diff --git a/src/shared/test/queries/community-read-state.test.ts b/src/shared/test/queries/community-read-state.test.ts
index fcfe7968f..a91e48256 100644
--- a/src/shared/test/queries/community-read-state.test.ts
+++ b/src/shared/test/queries/community-read-state.test.ts
@@ -80,6 +80,22 @@ describe("markReadToMessageBuilder — channel branch", () => {
]);
expect(conflictArg.targetWhere).toBeDefined();
});
+
+ it("includes a monotone setWhere so stale-createdAt PUTs cannot regress the pointer", () => {
+ const db = createInsertBuilderMock();
+ readStateQueries.markReadToMessageBuilder(db, {
+ userId: "u_1",
+ channelId: "c_1",
+ message: { id: "m_42", createdAt: "2026-07-03T00:00:00Z" },
+ });
+ const conflictArg = db.onConflictDoUpdate.mock.calls[0][0];
+ // The setWhere is a Drizzle SQL fragment; we can't easily assert its
+ // exact structure without evaluating it, but its mere presence is
+ // the load-bearing invariant here — its absence would silently
+ // reintroduce the "channel switch → return → stale mid-viewport row
+ // overwrites the newer pointer" bug.
+ expect(conflictArg.setWhere).toBeDefined();
+ });
});
describe("markReadToMessageBuilder — dm branch", () => {
@@ -107,6 +123,8 @@ describe("markReadToMessageBuilder — dm branch", () => {
lastReadAt: "2026-07-04T00:00:00Z",
lastReadMessageId: "m_9",
});
+ // DM branch also carries the monotone setWhere — same guarantee.
+ expect(conflictArg.setWhere).toBeDefined();
});
it("throws when neither channelId nor dmConversationId is provided", () => {
@@ -189,13 +207,12 @@ function makeMassMarkDbMock(opts: {
}),
update: vi.fn(() => ({
set: vi.fn((s: any) => ({
- where: vi.fn((_w: any) => {
- // where clause is eq(readState.id, u.id) — we can't inspect the id
- // trivially without evaluating the eq() operator. Instead, we log
- // the set clause and the ORDER of update calls; combined with the
- // spy on latest-messages we can reconstruct which channel each
- // update belongs to.
- updates.push({ id: "unknown", set: s });
+ where: vi.fn((w: any) => {
+ // where clause is `and(eq(readState.id, u.id), lt(readState.lastReadAt, u.createdAt))`
+ // — we can't inspect its structure without evaluating the SQL
+ // fragment, but the mere presence of a non-null argument lets
+ // downstream tests assert that a guard was passed at all.
+ updates.push({ id: "unknown", set: s, where: w });
return Promise.resolve();
}),
})),
@@ -321,4 +338,28 @@ describe("markAllServerChannelsRead", () => {
expect(bTuple!.lastReadAt).toBe("2026-07-05T11:00:00Z");
spy.mockRestore();
});
+
+ it("each UPDATE carries a monotone guard in its WHERE so a stale row cannot be regressed", async () => {
+ const db = makeMassMarkDbMock({
+ memberChannelIds: ["c_a"],
+ latestByChannel: {},
+ existingReadStateChannels: ["c_a"],
+ });
+ const messageModule = await import("../../src/db/queries/community/message");
+ const spy = vi.spyOn(messageModule, "getLatestMessagesByChannelIds").mockResolvedValue([
+ { channelId: "c_a", id: "m_a_new", createdAt: "2026-07-05T10:00:00Z" },
+ ]);
+
+ await readStateQueries.markAllServerChannelsRead(db, "u_1");
+ // A raw eq(id, ...) is a Drizzle SQL fragment; a guarded and(eq(...), lt(...))
+ // is a distinct fragment. We can't evaluate them structurally without
+ // spinning up SQLite, but the WHERE clause is definitely not undefined
+ // AND (post-refactor) is now an `and(...)` compound rather than a bare
+ // eq. Presence is the load-bearing bit — absence would silently allow
+ // "mark all read" to regress a channel whose stale read-state row is
+ // ahead of its current latest message.
+ expect(db.__updates).toHaveLength(1);
+ expect(db.__updates[0].where).toBeDefined();
+ spy.mockRestore();
+ });
});