From edb56e74a9d62213a798dddbfd55d0f352a52e5a Mon Sep 17 00:00:00 2001 From: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 Date: Mon, 22 Jun 2026 18:24:32 -0400 Subject: [PATCH] feat(channels): add msg: per-message read-state key family (LP4 P1.1) First step of the v3 per-message read-state redesign. Adds the msg: context-key constructor and validator alongside the existing thread:/channel keys, plus exports MAX_CONTEXTS for the upcoming msg:* eviction path. The validator rejects a thread key re-prefixed as a message key so the parent resolver and eviction can keep the two families distinct. No behavior change yet: nothing constructs msg: keys until the predicate swap (P1.3) and useChannelUnreadState wiring (P1.4) land. Co-authored-by: Will Pfleger Signed-off-by: Will Pfleger --- .../readState/readStateFormat.test.mjs | 33 +++++++++++++++++++ .../channels/readState/readStateFormat.ts | 23 ++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 desktop/src/features/channels/readState/readStateFormat.test.mjs diff --git a/desktop/src/features/channels/readState/readStateFormat.test.mjs b/desktop/src/features/channels/readState/readStateFormat.test.mjs new file mode 100644 index 0000000000..248692a91c --- /dev/null +++ b/desktop/src/features/channels/readState/readStateFormat.test.mjs @@ -0,0 +1,33 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { isMsgContextKey, msgContextKey } from "./readStateFormat.ts"; + +test("msgContextKey_prefixesId_returnsMsgKey", () => { + assert.equal(msgContextKey("abc123"), "msg:abc123"); +}); + +test("isMsgContextKey_wellFormedKey_returnsTrue", () => { + assert.equal(isMsgContextKey("msg:abc123"), true); +}); + +test("isMsgContextKey_threadKey_returnsFalse", () => { + assert.equal(isMsgContextKey(`thread:${"a".repeat(64)}`), false); +}); + +test("isMsgContextKey_channelKey_returnsFalse", () => { + assert.equal(isMsgContextKey("channel-1"), false); +}); + +test("isMsgContextKey_emptyId_returnsFalse", () => { + assert.equal(isMsgContextKey("msg:"), false); +}); + +test("isMsgContextKey_msgPrefixWrappingThreadKey_returnsFalse", () => { + // A thread key accidentally re-prefixed must not pass as a message key. + assert.equal(isMsgContextKey(`msg:thread:${"a".repeat(64)}`), false); +}); + +test("msgContextKey_output_roundTripsThroughValidator", () => { + assert.equal(isMsgContextKey(msgContextKey("event-id")), true); +}); diff --git a/desktop/src/features/channels/readState/readStateFormat.ts b/desktop/src/features/channels/readState/readStateFormat.ts index 6ce0d69c59..4a37d18330 100644 --- a/desktop/src/features/channels/readState/readStateFormat.ts +++ b/desktop/src/features/channels/readState/readStateFormat.ts @@ -8,7 +8,28 @@ export const READ_STATE_D_TAG_PREFIX = "read-state:"; export const READ_STATE_FETCH_LIMIT = 500; export const READ_STATE_HORIZON_SECONDS = 7 * 24 * 60 * 60; -const MAX_CONTEXTS = 10_000; +export const MAX_CONTEXTS = 10_000; + +// Context-key prefix for a per-MESSAGE read marker (LP4 v3). One grow-only +// marker per reply id; the badge predicate reads effective("msg:") live so +// reading an ancestor never covers a descendant (Issue 2 by construction). +// Distinct from THREAD_PREFIX so the parent resolver and eviction can tell the +// two key families apart. +export const MSG_PREFIX = "msg:"; +export const THREAD_PREFIX = "thread:"; + +export function msgContextKey(messageId: string): string { + return `${MSG_PREFIX}${messageId}`; +} + +// A well-formed per-message context key: the msg: prefix with a non-empty id +// that does NOT itself start with thread: (guards against a thread key being +// mistaken for, or double-prefixed into, a message key). +export function isMsgContextKey(value: string): value is `msg:${string}` { + if (!value.startsWith(MSG_PREFIX)) return false; + const id = value.slice(MSG_PREFIX.length); + return id.length > 0 && !id.startsWith(THREAD_PREFIX); +} export function localReadStateKey(pubkey: string): string { return `buzz.channel-read-state.v2:${pubkey}`;