Follow: anchor off the send path, clock slack on the early side, mutation-proof tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Heimdall 2026-11-12 12:05:51 +00:00
commit f66b222ea8
3 changed files with 145 additions and 10 deletions

View file

@ -259,9 +259,9 @@ A message from someone she is in conversation with is for her even when it names
With it on:
- **Anchor.** Her own post in a public room anchors that room for W = 600 s from the post. The mouth anchors at send time (the send's own time, in `send`) and also from the /sync echo of her own events, so a post from another client of her account anchors too; one event anchors once. Only her posting again re-anchors the room and re-snapshots who is counted; a follow never extends the window.
- **Counted senders** (snapshotted at the post): those who spoke in that room in the 10 minutes before the post (a per-room ring of recent senders with event times, 200 entries), plus anyone the post tagged (`m.mentions.user_ids`) or replied to (the author of the quoted `m.in_reply_to`, not a thread fallback pointer) when the event carries it. Someone who first speaks after her post is not counted. Nothing reads message text; no ids are scraped from bodies. She herself, and bridged senders (the `platformEvent` identifier rule), never count.
- **Follow.** A `message` (not an edit, not a notice) from a counted sender gets `follow` when it would otherwise have no basis or `room`; `tag`, `reply` and `direct` are never replaced. Its timestamp must lie inside the window and this process's clock must not be past the window's end by more than 60 s, so an old or forged `origin_server_ts` reuses nothing. At most K = 3 per (Mind, peer, room) in the hour before the message; past it the message keeps the basis it had.
- **Anchor.** Her own post in a public room anchors that room for W = 600 s from the post. The mouth anchors at send time (the send's own time, in `send`, in the background: the send receipt never waits on the room-state read) and also from the /sync echo of her own events, so a post from another client of her account anchors too; one event anchors once. Only her posting again re-anchors the room and re-snapshots who is counted; a follow never extends the window.
- **Counted senders** (snapshotted at the post): those who spoke in that room in the 10 minutes before the post (a per-room ring of recent senders with event times, 200 entries), plus anyone the post tagged (`m.mentions.user_ids`) or replied to (the author of the quoted `m.in_reply_to`, not a thread fallback pointer) when the event carries it. Someone who first speaks after her post is not counted. A member who spoke in the 10 minutes before her post is counted, bounded by 3 follows per hour per (peer, room). Nothing reads message text; no ids are scraped from bodies. She herself, and bridged senders (the `platformEvent` identifier rule), never count.
- **Follow.** A `message` (not an edit, not a notice) from a counted sender gets `follow` when it would otherwise have no basis or `room`; `tag`, `reply` and `direct` are never replaced. Its timestamp must lie inside the window (a homeserver clock up to 60 s behind still follows) and this process's clock must not be past the window's end by more than 60 s, so an old or forged `origin_server_ts` reuses nothing. At most K = 3 per (Mind, peer, room) in the hour before the message; past it the message keeps the basis it had.
- **"Public" means non-DM, non-encrypted, non-bridged.** The room is not one of her DMs and its state has no `m.room.encryption`, `m.bridge` or `uk.half-shot.bridge` (read once per room, kept 5 minutes; unreadable or malformed state is not public). An invite-only plaintext room counts as public. Nothing is anchored in, and no follow is delivered in, any other room.
State is in memory only (per-room anchor, sender ring, follow times), purged on every sync pass; nothing is stored, and a restart only shortens windows.

View file

@ -21,7 +21,7 @@ const msg = (id, sender, ts, content = {}) => ({ type: "m.room.message", event_i
const tagKiko = { "m.mentions": { user_ids: [KIKO] } };
// rounds: sync replies after the baseline, each { [roomId]: events }. state: room id -> state events.
function world({ rounds, state = {}, stateStatus = {}, follow = true, rooms = [ALIAS, OTHER_ALIAS], direct } = {}) {
function world({ rounds, state = {}, stateStatus = {}, hangState = false, baselineEvents = null, onSync = null, follow = true, rooms = [ALIAS, OTHER_ALIAS], direct } = {}) {
const seen = [];
const known = new Map();
let round = 0;
@ -36,12 +36,14 @@ function world({ rounds, state = {}, stateStatus = {}, follow = true, rooms = [A
if (p.startsWith("/directory/room/")) return reply(200, { room_id: { [ALIAS]: ROOM, [OTHER_ALIAS]: OTHER_ROOM }[p.slice(16)] });
if (p.startsWith("/presence/") || p.includes("/receipt/")) return reply(200, {});
const st = p.match(/^\/rooms\/([^/]+)\/state$/);
if (st && hangState) return new Promise(() => {});
if (st) return stateStatus[st[1]] ? reply(stateStatus[st[1]], { errcode: "M_UNKNOWN" }) : reply(200, state[st[1]] ?? []);
if (init.method === "PUT" && p.includes("/send/m.room.message/")) return reply(200, { event_id: "$sent1" });
const ev = p.match(/^\/rooms\/([^/]+)\/event\/(.+)$/);
if (ev) return known.has(ev[2]) ? reply(200, known.get(ev[2])) : reply(404, { errcode: "M_NOT_FOUND" });
if (p === "/sync") {
if (u.searchParams.get("since") === null) return reply(200, { next_batch: "b0" });
if (u.searchParams.get("since") === null) return reply(200, { next_batch: "b0", ...(baselineEvents ? { rooms: { join: Object.fromEntries(Object.entries(baselineEvents).map(([r, list]) => [r, { timeline: { events: list } }])) } } : {}) });
onSync?.(setClock);
const events = rounds[round++] ?? {};
for (const list of Object.values(events)) for (const e of list) known.set(e.event_id, e);
return reply(200, { next_batch: `b${round}`, rooms: { join: Object.fromEntries(Object.entries(events).map(([r, list]) => [r, { timeline: { events: list } }])) } });
@ -50,6 +52,7 @@ function world({ rounds, state = {}, stateStatus = {}, follow = true, rooms = [A
return reply(404, { errcode: "M_NOT_FOUND" });
};
let clock = T0;
const setClock = t => { clock = t; };
const mouth = createMouth({
persona: "eureka", homeserver: "http://hs.test", serverName: HS, rooms, token: "tok", fetch: hsFetch, follow,
state: { load: async () => null, save: async () => {} }, brake: async () => true, log: () => {}, syncTimeoutMs: 1, now: () => clock,
@ -201,3 +204,129 @@ test("follow: the basis is a name the delivery document accepts", () => {
const d = { seq: 1, roomAlias: ALIAS, roomId: ROOM, eventId: "$e", sender: KIKO, senderDisplay: "kiko", text: "x", atUnixMs: 1, context: [], image: null, mentions: [], isDirect: false, basis: "follow", form: "message" };
assert.equal(docs.parseDeliveryV3(docs.deliveryV3Tuple(d)).basis, "follow");
});
const tick = async () => { for (let i = 0; i < 20; i++) await new Promise(r => setImmediate(r)); };
test("follow: a send receipt is not delayed by a room-state read that hangs or fails; the anchor happens in the background", async () => {
for (const opts of [{ hangState: true }, { stateStatus: { [ROOM]: 500 } }]) {
const w = world({ ...opts, rounds: [] });
await w.mouth.syncOnce();
const sent = await Promise.race([w.mouth.send({ requestId: "r1", persona: "eureka", roomAlias: ALIAS, text: "hello" }), new Promise(r => setTimeout(() => r("late"), 500))]);
assert.equal(sent.refusal, null);
}
const ok = world({ rounds: [{ [ROOM]: [msg("$k0", KIKO, T0 - MIN)] }, { [ROOM]: [msg("$k1", KIKO, T0 + MIN)] }] });
await ok.mouth.syncOnce(); await ok.basisOfRound();
await ok.mouth.send({ requestId: "r1", persona: "eureka", roomAlias: ALIAS, text: "hello" });
await tick();
assert.deepEqual(await ok.basisOfRound(), [["$k1", "follow"]]);
});
test("follow: the flag off makes a send cost no extra call and no extra turn", async () => {
const w = world({ follow: false, rounds: [] });
await w.mouth.syncOnce();
const before = w.seen.length;
await w.mouth.send({ requestId: "r1", persona: "eureka", roomAlias: ALIAS, text: "hello" });
await tick();
assert.ok(!w.seen.slice(before).some(l => l.endsWith("/state")));
});
test("follow: a homeserver clock up to 60 s behind still follows; 61 s early does not; the very end of the window is out, 1 ms before it is in", async () => {
const w = world({ rounds: [
{ [ROOM]: [msg("$k0", KIKO, T0 - MIN), msg("$l0", LENA, T0 - MIN), msg("$e0", `@e:${HS}`, T0 - MIN), msg("$f0", `@f:${HS}`, T0 - MIN)] },
{ [ROOM]: [mine("$mine", T0)] },
{ [ROOM]: [msg("$x1", KIKO, T0 - 60 * SEC), msg("$x2", LENA, T0 - 61 * SEC), msg("$x3", `@e:${HS}`, T0 + W - 1), msg("$x4", `@f:${HS}`, T0 + W)] },
] });
await w.mouth.syncOnce(); await go(w, T0 - MIN); await go(w, T0);
assert.deepEqual((await go(w, T0 + 5 * SEC)).sort(), [["$x1", "follow"], ["$x2", null], ["$x3", "follow"], ["$x4", null]]);
});
test("follow: a window that ended while the pass was being read is out, whatever the message says", async () => {
let jump = null;
const w = world({ onSync: set => { if (jump !== null) set(jump); }, rounds: [{ [ROOM]: [msg("$k0", KIKO, T0 - MIN)] }, { [ROOM]: [mine("$mine", T0)] }, { [ROOM]: [msg("$x", KIKO, T0 + MIN)] }] });
await w.mouth.syncOnce(); await go(w, T0 - MIN);
const keep = w.mouth.followState;
w.at(T0); await w.basisOfRound(); // her post; the next sync moves the clock after the purge
assert.equal(keep().engagements.length, 1);
jump = T0 + 30 * MIN; w.at(T0); // the purge at the start of the pass sees T0; the clock moves while the pass reads
assert.deepEqual(await w.basisOfRound(), [["$x", null]]);
});
test("follow: the cap counts only the hour before the message", async () => {
const w = world({ rounds: [
{ [ROOM]: [msg("$k0", KIKO, T0 - SEC)] },
...[1, 2, 3].flatMap(n => [{ [ROOM]: [mine(`$m${n}`, T0 + n * MIN)] }, { [ROOM]: [msg(`$x${n}`, KIKO, T0 + n * MIN + SEC)] }]),
{ [ROOM]: [msg("$k90", KIKO, T0 + 89 * MIN), mine("$m90", T0 + 90 * MIN)] }, { [ROOM]: [msg("$x90", KIKO, T0 + 90 * MIN + SEC)] },
] });
await w.mouth.syncOnce(); await go(w, T0);
for (const n of [1, 1, 2, 2, 3, 3]) await go(w, T0 + n * MIN + SEC);
assert.deepEqual(await go(w, T0 + 30 * MIN), [["$k90", null]]); // stamps run ahead of this process's clock, so only the cap's own hour bound can tell
assert.deepEqual(await go(w, T0 + 30 * MIN), [["$x90", "follow"]]);
});
test("follow: whoever her reply quotes is counted though they spoke long ago; a thread fallback pointer names no one", async () => {
const w = world({ rounds: [
{ [ROOM]: [msg("$old", LENA, T0 - 30 * MIN), msg("$ghost", `@ghost:${HS}`, T0 - 30 * MIN)] },
{ [ROOM]: [mine("$mine", T0, { "m.relates_to": { "m.in_reply_to": { event_id: "$old" } } })] },
{ [ROOM]: [msg("$x1", LENA, T0 + MIN)] },
{ [ROOM]: [msg("$old2", KIKO, T0 + 2 * MIN)] },
{ [ROOM]: [mine("$mine2", T0 + 3 * MIN, { "m.relates_to": { rel_type: "m.thread", event_id: "$old", is_falling_back: true, "m.in_reply_to": { event_id: "$ghost" } } })] },
{ [ROOM]: [msg("$x2", STRANGER, T0 + 4 * MIN)] },
] });
await w.mouth.syncOnce();
await go(w, T0 - 30 * MIN); await go(w, T0);
assert.deepEqual(await go(w, T0 + MIN), [["$x1", "follow"]]);
await go(w, T0 + 2 * MIN);
await go(w, T0 + 3 * MIN);
const [engagement] = w.mouth.followState().engagements;
assert.ok(engagement.counted.includes(KIKO), "kiko spoke 1 minute before the second post");
assert.ok(!engagement.counted.includes(`@ghost:${HS}`), "a fallback pointer names no one");
});
test("follow: someone stamped after her send is not counted as having spoken before it", async () => {
const w = world({ rounds: [{ [ROOM]: [msg("$ahead", KIKO, T0 + MIN)] }, { [ROOM]: [msg("$x", KIKO, T0 + 2 * MIN)] }] });
await w.mouth.syncOnce(); await w.basisOfRound();
await w.mouth.send({ requestId: "r1", persona: "eureka", roomAlias: ALIAS, text: "hello" });
await tick();
assert.deepEqual(w.mouth.followState().engagements[0].counted, []);
assert.deepEqual(await go(w, T0 + 2 * MIN), [["$x", null]]);
});
test("follow: the ring keeps the newest 200 senders of a room; a flood pushes out the oldest, not the newest", async () => {
const flood = Array.from({ length: 250 }, (_, i) => msg(`$f${i}`, `@u${i}:${HS}`, T0 - 5 * MIN + i));
const w = world({ rounds: [{ [ROOM]: flood }, { [ROOM]: [mine("$mine", T0)] }, { [ROOM]: [msg("$x0", `@u0:${HS}`, T0 + MIN), msg("$x249", `@u249:${HS}`, T0 + MIN)] }] });
await w.mouth.syncOnce(); await w.basisOfRound();
assert.equal(w.mouth.followState().spoke[0].senders.length, 200);
await go(w, T0);
assert.deepEqual(await go(w, T0 + MIN + SEC), [["$x0", null], ["$x249", "follow"]]);
assert.equal(w.mouth.followState().engagements[0].counted.length, 200);
});
test("follow: only senders, and only real ones, are remembered and counted: not her, not a bridged id, not an edit, not a notice, not a malformed id", async () => {
const puppet = `@_${PLATFORM}_9:${HS}`;
const w = world({ rounds: [
{ [ROOM]: [msg("$a", KIKO, T0 - 2 * MIN), mine("$self", T0 - 2 * MIN), msg("$b", puppet, T0 - 2 * MIN),
msg("$c", LENA, T0 - 2 * MIN, { "m.relates_to": { rel_type: "m.replace", event_id: "$a" } }), msg("$d", STRANGER, T0 - 2 * MIN, { msgtype: "m.notice" })] },
{ [ROOM]: [mine("$mine", T0, { "m.mentions": { user_ids: [ME, puppet, "not-an-id", `@${"x".repeat(300)}:${HS}`, LENA] } })] },
] });
await w.mouth.syncOnce(); await go(w, T0 - MIN);
assert.deepEqual(w.mouth.followState().spoke, [{ room: ROOM, senders: [KIKO] }]);
await go(w, T0);
assert.deepEqual(w.mouth.followState().engagements[0].counted.sort(), [KIKO, LENA].sort());
});
test("follow: her own post in the baseline sync, which is history, anchors nothing", async () => {
const w = world({ baselineEvents: { [ROOM]: [msg("$k0", KIKO, T0 - MIN), mine("$mine", T0)] }, rounds: [] });
await w.mouth.syncOnce();
assert.deepEqual(w.mouth.followState().engagements, []);
});
test("follow: every pass forgets what has aged out: anchors, speakers and cap times", async () => {
const w = world({ rounds: [
{ [ROOM]: [msg("$k0", KIKO, T0 - MIN)] }, { [ROOM]: [mine("$mine", T0)] }, { [ROOM]: [msg("$x", KIKO, T0 + MIN)] }, {},
] });
await w.mouth.syncOnce(); await go(w, T0 - MIN); await go(w, T0); await go(w, T0 + MIN + SEC);
const s1 = w.mouth.followState();
assert.equal(s1.engagements.length, 1); assert.equal(s1.followsGiven, 1); assert.equal(s1.spoke.length, 1);
await go(w, T0 + 3 * 60 * MIN);
assert.deepEqual(w.mouth.followState(), { engagements: [], spoke: [], followsGiven: 0 });
});

View file

@ -364,9 +364,10 @@ export function createMouth({
}
// Her post at time `at` (the send's own time, or the echo's origin time): snapshot the room. `tagged`: ids from the event's m.mentions, `replyTo` the quoted event id.
async function anchor(roomId, eventId, at, tagged = [], replyTo = null) {
if (!follow || anchored.has(eventId) || !(await publicRoom(roomId))) return;
anchored.add(eventId);
if (!follow || anchored.has(eventId)) return;
anchored.add(eventId); // before the room read: the send path and its echo race, and one event anchors once
if (anchored.size > 200) anchored.delete(anchored.values().next().value);
if (!(await publicRoom(roomId))) return;
const counted = new Set((spoke.get(roomId) ?? []).filter(r => r.at >= at - FOLLOW_WINDOW_MS && r.at <= at).map(r => r.sender));
for (const id of tagged) counted.add(id);
if (typeof replyTo === "string" && replyTo !== "") { const author = await authorOf(roomId, replyTo); if (author !== null) counted.add(author); }
@ -385,7 +386,7 @@ export function createMouth({
if (!follow || directRooms.has(roomId) || formOf(event.content) !== "message" || platformEvent(event)) return false;
const e = engagements.get(roomId), at = eventAt(event), t = now();
// Both clocks must agree: the message's own time inside the window, and this process's time not past it (a forged or old timestamp reuses nothing).
if (!e || !e.counted.has(event.sender) || at < e.from || at >= e.ends || t >= e.ends + FOLLOW_SLACK_MS) return false;
if (!e || !e.counted.has(event.sender) || at < e.from - FOLLOW_SLACK_MS || at >= e.ends || t >= e.ends + FOLLOW_SLACK_MS) return false;
if (!(await publicRoom(roomId))) return false;
const key = `${roomId}|${event.sender}`;
const recent = (followsGiven.get(key) ?? []).filter(x => at - x < FOLLOW_HOUR_MS);
@ -742,7 +743,10 @@ export function createMouth({
const sent = await matrix("PUT", `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${encodeURIComponent(requestId)}`, { msgtype: "m.text", body: text }, API, AbortSignal.timeout(sendTimeoutMs));
return { requestId, eventId: sent.event_id, refusal: null, newer: [] };
});
if (result.refusal === null) { touch(); setPresence(true).catch(() => {}); await anchor(roomId, result.eventId, now()).catch(error => say(`follow: send not anchored: ${error.message}`)); } // outside the room gate and not awaited: a slow presence write holds neither another send nor this receipt
if (result.refusal === null) {
touch(); setPresence(true).catch(() => {}); // outside the room gate and not awaited: a slow presence write holds neither another send nor this receipt
if (follow) { const at = now(); anchor(roomId, result.eventId, at).catch(error => say(`follow: send not anchored: ${error.message}`)); } // in the background too: a room-state read that hangs or fails never delays the receipt
}
return result;
} catch (error) {
say(`send failed: ${error.message}`);
@ -982,7 +986,9 @@ export function createMouth({
}
}
return { start, syncOnce, ack, read, pendingAfter, tail, speakableRooms, send, openDirect, fetchAttachment, run, get userId() { return selfId; }, get pendingCount() { return pending.length; } };
// A plain copy of the follow memory, for tests: anchors with their counted senders, the sender ring, and how many cap entries are held.
const followState = () => ({ engagements: [...engagements].map(([room, e]) => ({ room, from: e.from, ends: e.ends, counted: [...e.counted] })), spoke: [...spoke].map(([room, ring]) => ({ room, senders: ring.map(r => r.sender) })), followsGiven: followsGiven.size });
return { followState, start, syncOnce, ack, read, pendingAfter, tail, speakableRooms, send, openDirect, fetchAttachment, run, get userId() { return selfId; }, get pendingCount() { return pending.length; } };
}
// The log's word for a failure: error class and HTTP status only, never the message (it can hold a URL, an mxc or a sender-chosen id).