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:<id>
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 <pfleger.will@gmail.com>
Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
This commit is contained in:
npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
2026-06-22 18:24:32 -04:00
parent 996710a83b
commit edb56e74a9
2 changed files with 55 additions and 1 deletions

View File

@@ -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);
});

View File

@@ -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:<id>") 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}`;