diff --git a/tests/persona-matrix-mouth-runtime.test.mjs b/tests/persona-matrix-mouth-runtime.test.mjs index a78d22c..e4dcf88 100644 --- a/tests/persona-matrix-mouth-runtime.test.mjs +++ b/tests/persona-matrix-mouth-runtime.test.mjs @@ -10,7 +10,7 @@ import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { resolve } from "node:path"; import * as docs from "../tools/persona-chat-documents.mjs"; -import { cultcacheState, connectionIdFor, createBrakeReader, loadRuntime, pageOf, startChatEndpoint } from "../tools/persona-matrix-mouth.mjs"; +import { createMouth, cultcacheState, connectionIdFor, createBrakeReader, loadRuntime, pageOf, startChatEndpoint } from "../tools/persona-matrix-mouth.mjs"; const root = resolve(import.meta.dirname, ".."); const runtime = loadRuntime(resolve(process.env.CULTLIB_ROOT ?? resolve(root, "..", "CultLib"))); @@ -370,3 +370,55 @@ test("the RUDP endpoint answers a v1 send with the three-field receipt and a v2 try { client.close(); } catch { /* the connection already closed its socket */ } } }); + +test("over the RUDP endpoint: pending posts no receipt, and an ack never holds up the same peer's next request", async () => { + const receipts = []; + let release; + const held = new Promise(r => { release = r; }); + const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); + const message = { type: "m.room.message", event_id: "$m1", sender: "@alice:t", origin_server_ts: 5, content: { msgtype: "m.text", body: "hi" } }; + const fetch = async (url, init = {}) => { + const u = new URL(url), p = u.pathname.replace("/_matrix/client/v3", ""); + if (p === "/account/whoami") return json({ user_id: "@eureka:t" }); + if (p === "/joined_rooms") return json({ joined_rooms: ["!r:t"] }); + if (p.startsWith("/directory/room/")) return json({ room_id: "!r:t" }); + if (p.includes("/account_data/")) return json({}, 404); + if (p === "/sync") return json(u.searchParams.get("since") === "b0" ? { next_batch: "b1", rooms: { join: { "!r:t": { timeline: { events: [message] } } } } } : { next_batch: "b0" }); + if (p.includes("/messages")) return json({ chunk: [] }); + if (p.includes("/receipt/")) { receipts.push(p); await held; return json({}); } + return json({}, 404); + }; + const state = { load: async () => null, save: async () => {} }; + const mouth = createMouth({ persona: "eureka", homeserver: "http://hs.test", rooms: ["#r:t"], token: "tok", state, fetch, serverName: "t", brake: async () => true, syncTimeoutMs: 1 }); + await mouth.syncOnce(); await mouth.syncOnce(); + const key = "connection-key-for-receipts"; + const server = await startChatEndpoint({ mouths: new Map([["eureka", mouth]]), runtime, port: 0, connectionKey: key, runtimeId: "mouth-test" }); + const client = dgram.createSocket("udp4"); + await new Promise(ok => client.bind(0, "127.0.0.1", ok)); + const connection = new runtime.cultnet.CultNetRudpSocketTransportConnection({ + runtimeId: "huginn-test", socket: client, mode: "client", remoteHost: "127.0.0.1", remotePort: server.address.port, + connectionId: connectionIdFor(key), initialSequence: 1, resendDelayMs: 50, resendPollMs: 10, maxFragmentBytes: 1200, + }); + const replies = []; + connection.on("frame", value => replies.push(runtime.cultnet.parseCultNetMessage(msgpack.decode(value.payload), "cultnet.schema.v0").document)); + const put = (kind, recordKey, tuple) => connection.send("schema", msgpack.encode(runtime.cultnet.encodeCultNetMessageForWire( + docs.envelope({ kind, recordKey, payload: msgpack.encode(tuple), sourceRuntimeId: "huginn-test", sourceAgentId: "eureka", sourceRole: "persona-organ" }), "cultnet.schema.v0"))); + const waitFor = async (check, what) => { for (let i = 0; i < 250 && !check(); i++) await new Promise(r => setTimeout(r, 20)); assert.ok(check(), what); }; + try { + connection.connect(); + await waitFor(() => connection.connected, "session opens"); + put(docs.kinds.pendingRequest, "eureka", docs.pendingRequestTuple({ persona: "eureka", afterSeq: 0 })); + await waitFor(() => replies.length === 1, "pending answered"); + assert.deepEqual(receipts, [], "a pending request posts no receipt"); + put(docs.kinds.ack, "eureka:1", docs.ackTuple({ persona: "eureka", throughSeq: 1 })); + await waitFor(() => receipts.length === 1, "the ack posted her receipt"); + put(docs.kinds.pendingRequest, "eureka", docs.pendingRequestTuple({ persona: "eureka", afterSeq: 0 })); + await waitFor(() => replies.length === 2, "the next request is answered while the receipt response is still held open"); + assert.deepEqual(docs.parsePending(msgpack.decode(replies[1].payload)).deliveries, [], "the ack applied"); + } finally { + release(); + server.close(); + connection.close(); + try { client.close(); } catch { /* already closed */ } + } +}); diff --git a/tests/persona-matrix-mouth.test.mjs b/tests/persona-matrix-mouth.test.mjs index 680eef8..89c0432 100644 --- a/tests/persona-matrix-mouth.test.mjs +++ b/tests/persona-matrix-mouth.test.mjs @@ -23,7 +23,7 @@ const tick = () => new Promise(r => setImmediate(r)); // A fake Matrix client-server API: just the endpoints the mouth uses. // It is also the record of every room: events the sync script emits and messages sent are kept, and // `preload` (room id -> events, oldest first) stands for history from before the mouth's process began. -async function fakeHomeserver({ sync, failWith, users, member, direct, preload = {}, failContext = false, media } = {}) { +async function fakeHomeserver({ sync, failWith, users, member, direct, preload = {}, failContext = false, media, failReceipts = false, deferReceipts = null, failPresence = 0 } = {}) { const seen = []; const record_ = new Map(Object.entries(preload).map(([room, events]) => [room, [...events]])); const remember = (room, event) => { const list = record_.get(room) ?? []; if (!list.some(e => e.event_id === event.event_id)) list.push(event); record_.set(room, list); }; @@ -40,6 +40,11 @@ async function fakeHomeserver({ sync, failWith, users, member, direct, preload = if (failWith) return reply(failWith, { errcode: "M_UNKNOWN_TOKEN", error: `bad token ${record.auth}` }); const p = record.path.replace("/_matrix/client/v3", "").replace("/_matrix/client/v1", ""); const bearer = (record.auth ?? "").replace("Bearer ", ""); + if (req.method === "POST" && p.includes("/receipt/m.read/")) { + if (deferReceipts) return deferReceipts.then(() => reply(200, {})); + return failReceipts ? reply(500, { errcode: "M_UNKNOWN", error: `down ${record.auth}` }) : reply(200, {}); + } + if (req.method === "PUT" && p.includes("/presence/")) return failPresence-- > 0 ? reply(500, { errcode: "M_UNKNOWN" }) : reply(200, {}); if (p.startsWith("/media/download/")) { const served = media?.(bearer, ...p.slice("/media/download/".length).split("/")) ?? { status: 404, body: "" }; res.writeHead(served.status ?? 200, served.headers ?? {}); @@ -91,7 +96,9 @@ async function fakeHomeserver({ sync, failWith, users, member, direct, preload = url: "http://hs.test", fetch, seen, - puts: () => seen.filter(r => r.method === "PUT"), + puts: () => seen.filter(r => r.method === "PUT" && !r.path.includes("/presence/")), + receipts: () => seen.filter(r => r.path.includes("/receipt/")), + presencePuts: () => seen.filter(r => r.path.includes("/presence/")), downloads: () => seen.filter(r => r.path.includes("/media/download/")), close: async () => {}, }; @@ -443,7 +450,7 @@ test("join_while_braked: no join and no send while the brake is held; joining ha const mouth = mouthFor(hs, { brake: async () => open, log: l => logs.push(l) }); await mouth.syncOnce(); await mouth.send({ requestId: "r", persona: "eureka", roomAlias: `#swarm:${HS_NAME}`, text: "speak" }); - assert.equal(hs.seen.filter(r => r.method === "POST" || r.method === "PUT").length, 0, "braked: nothing is joined or sent"); + assert.equal(hs.seen.filter(r => (r.method === "POST" || r.method === "PUT") && !r.path.includes("/presence/")).length, 0, "braked: nothing is joined or sent"); open = true; await mouth.syncOnce(); assert.deepEqual(hs.seen.filter(r => r.method === "POST").map(r => r.path.split("/join/")[1]).sort(), ["!operator:gamecult.test", "!swarm:gamecult.test"]); @@ -588,7 +595,7 @@ test("dm_invite_waits_for_release: an invitation received while braked is not jo try { const mouth = mouthFor(hs, { brake: async () => open }); await mouth.syncOnce(); await mouth.syncOnce(); await mouth.syncOnce(); - assert.deepEqual(hs.seen.filter(r => r.method === "POST" || r.method === "PUT"), [], "braked: no join, no leave, nothing written"); + assert.deepEqual(hs.seen.filter(r => (r.method === "POST" || r.method === "PUT") && !r.path.includes("/presence/")), [], "braked: no join, no leave, nothing written"); open = true; await mouth.syncOnce(); assert.deepEqual(joinsOf(hs), [DMROOM]); @@ -941,3 +948,120 @@ test("image_delivery: a store that cannot write leaves the line and a nil image, assert.ok(lines.some(l => l.includes("ENOSPC"))); } finally { await hs.close(); } }); + +// Read receipts and presence: her ack posts her m.read; her brake sets her presence. +const A = "!swarm:gamecult.test", B = "!operator:gamecult.test"; +const receiptScript = since => since === null ? { next_batch: "b0", ...joined({}) } : since === "b0" ? { + next_batch: "b1", + ...joined({ + [A]: { timeline: { events: [message("$a1", `@alice:${HS_NAME}`, "one", 10), message("$a2", `@alice:${HS_NAME}`, "two", 11)] } }, + [B]: { timeline: { events: [message("$b1", `@bob:${HS_NAME}`, "three", 12)] } }, + }), +} : { next_batch: since }; +const until = async check => { for (let i = 0; i < 200 && !check(); i++) await new Promise(r => setImmediate(r)); }; +const withMail = async (hs, overrides) => { + const mouth = mouthFor(hs, overrides); + await mouth.syncOnce(); + assert.equal((await mouth.syncOnce()).map(d => d.seq).join(), "1,2,3"); + return mouth; +}; +const receiptsOf = hs => hs.receipts().map(r => [r.path.split("/rooms/")[1].split("/receipt/")[0], r.path.split("/m.read/")[1], r.auth, r.body]); + +test("an ack posts one m.read per room at the newest covered event, as that Persona", async () => { + const hs = await fakeHomeserver({ sync: receiptScript }); + const mouth = await withMail(hs); + await mouth.ack(2); + await until(() => hs.receipts().length >= 1); + await new Promise(r => setTimeout(r, 20)); + assert.deepEqual(receiptsOf(hs), [[A, "$a2", `Bearer ${TOKEN}`, {}]], "room A at its second event; room B untouched"); + await mouth.ack(3); + await until(() => hs.receipts().length >= 2); + assert.deepEqual(receiptsOf(hs).slice(1), [[B, "$b1", `Bearer ${TOKEN}`, {}]]); + assert.equal(hs.receipts().length, 2); +}); + +test("pending posts no receipt", async () => { + const hs = await fakeHomeserver({ sync: receiptScript }); + const mouth = await withMail(hs); + for (const after of [0, 1, 2, 3]) mouth.pendingAfter(after); + await new Promise(r => setTimeout(r, 20)); + assert.deepEqual(hs.receipts(), []); +}); + +test("a failed receipt leaves the ack whole", async () => { + const hs = await fakeHomeserver({ sync: receiptScript, failReceipts: true }); + const lines = []; + const state = memoryState(); + const mouth = await withMail(hs, { state, log: l => lines.push(l) }); + const saves = []; + const save = state.save; + state.save = async v => { saves.push(v); return save(v); }; + await mouth.ack(3); + await until(() => lines.some(l => l.includes("receipt in"))); + assert.equal(mouth.pendingCount, 0); + assert.equal(state.box.value.nextBatch, "b1", "the cursor committed"); + assert.ok(saves.length >= 1, "state.save called"); + const failures = lines.filter(l => l.startsWith("receipt in ")); + assert.equal(failures.length, 2, "one line per room, nothing retried"); + assert.ok(failures.every(l => l.includes("not posted") && !l.includes(TOKEN))); + await new Promise(r => setTimeout(r, 20)); + assert.equal(hs.receipts().length, 2, "no retry"); +}); + +test("the ack does not wait for the receipt", async () => { + let release; + const hs = await fakeHomeserver({ sync: receiptScript, deferReceipts: new Promise(r => { release = r; }) }); + const mouth = await withMail(hs); + await mouth.ack(3); // resolves while the receipt response is held open + assert.equal(mouth.pendingCount, 0); + release(); + await until(() => hs.receipts().length >= 2); +}); + +test("presence follows the brake: online while released, offline and one PUT on the transition", async () => { + let open = true; + const hs = await fakeHomeserver({ sync: receiptScript }); + const mouth = mouthFor(hs, { brake: async () => open }); + const syncs = () => hs.seen.filter(r => r.path.endsWith("/sync")).map(r => r.query.get("set_presence")); + await mouth.syncOnce(); await mouth.syncOnce(); + assert.deepEqual(syncs(), ["online", "online"]); + assert.equal(hs.presencePuts().length, 0, "released: no PUT"); + open = false; + await mouth.syncOnce(); await mouth.syncOnce(); await mouth.syncOnce(); + assert.deepEqual(syncs().slice(2), ["offline", "offline", "offline"]); + assert.deepEqual(hs.presencePuts().map(r => [r.path, r.body]), [[`/_matrix/client/v3/presence/${SELF}/status`, { presence: "offline" }]], "exactly one PUT"); + open = true; + await mouth.syncOnce(); + assert.equal(syncs().at(-1), "online"); + open = false; + await mouth.syncOnce(); + assert.equal(hs.presencePuts().length, 2, "a second transition PUTs once more"); +}); + +test("the first braked round of a fresh mouth PUTs offline once; a failed PUT is tried again next round", async () => { + const hs = await fakeHomeserver({ sync: receiptScript, failPresence: 1 }); + const lines = []; + const mouth = mouthFor(hs, { brake: async () => false, log: l => lines.push(l) }); + await mouth.syncOnce(); + assert.equal(hs.presencePuts().length, 1); + assert.ok(lines.some(l => l.startsWith("presence not set")), "one log line"); + await mouth.syncOnce(); + assert.equal(hs.presencePuts().length, 2, "retried the next round"); + await mouth.syncOnce(); + assert.equal(hs.presencePuts().length, 2, "then settled"); +}); + +test("two Personas on one homeserver: receipts and presence carry only her own bearer and user id", async () => { + const ALICE = `@pa:${HS_NAME}`, BOB = `@pb:${HS_NAME}`; + const hs = await fakeHomeserver({ sync: receiptScript, users: { tokA: ALICE, tokB: BOB } }); + const a = await withMail(hs, { persona: "pa", token: "tokA" }); + const b = await withMail(hs, { persona: "pb", token: "tokB", brake: async () => false }); + await a.ack(3); + await b.syncOnce(); + await until(() => hs.receipts().length >= 2); + assert.ok(hs.receipts().every(r => r.auth === "Bearer tokA"), "only A acked"); + await b.ack(3); + await until(() => hs.receipts().length >= 4); + assert.deepEqual([...new Set(hs.receipts().map(r => r.auth))].sort(), ["Bearer tokA", "Bearer tokB"]); + assert.ok(hs.presencePuts().length >= 1 && hs.presencePuts().every(r => r.path.endsWith(`/presence/${BOB}/status`) && r.auth === "Bearer tokB")); +}); diff --git a/tools/persona-matrix-mouth.mjs b/tools/persona-matrix-mouth.mjs index 57b7405..dac8ac6 100644 --- a/tools/persona-matrix-mouth.mjs +++ b/tools/persona-matrix-mouth.mjs @@ -6,7 +6,12 @@ // its m.mentions user ids and whether the room is a DM. The /sync position // (next_batch) is persisted only when an ack covers it, so a restart before the // ack re-delivers instead of losing. Reading wakes nothing: the heartbeat polls -// with a non-consuming chat_pending_request and decides who runs. +// with a non-consuming chat_pending_request and decides who runs. An ack also posts this Persona's public +// m.read receipt per room, at the newest event the ack covers (fire and forget: the ack never waits on it, +// a failure is logged once and not retried, the room's next ack covers it). Her presence follows her +// brake: every /sync says online while it is released and offline while it is held, and a round that +// finds her braked makes one presence PUT to offline first. A brake placed mid long-poll shows at the next +// round, within syncTimeoutMs plus one PUT; a stopped mouth reads offline after the homeserver's sync timeout. // Mouth: a send request becomes one PUT with txnId = request_id (a retry is idempotent), and a room // is joined, only while that Persona's brake is released. A v2 send carries the basis of her turn (the // newest room event she saw): inside the room gate, one step per room across every Persona, the mouth @@ -119,6 +124,7 @@ export function createMouth({ let batches = []; // { token, maxSeq }: positions awaiting an ack let committed = null; // the position that is persisted let started = false; + let lastPresence = null; // 'online' | 'offline' | null: command-only, suppresses repeat presence PUTs const directContent = {}; // the account's m.direct content: user id -> room ids const invites = new Map(); // room id -> { inviter, direct }: invitations not yet answered const processed = new Set(); // event ids read since the position last advanced @@ -158,8 +164,7 @@ export function createMouth({ // A DM invitation from an account on our own homeserver is accepted, and only while the brake is // released; one received while braked waits for the release. An invitation from any other server is // declined. A same-server invitation to a room that is not a DM is not ours to accept and is dropped. - async function answerInvites() { - let free = null; + async function answerInvites(free) { for (const [roomId, invite] of [...invites]) { try { const room = encodeURIComponent(roomId); @@ -169,7 +174,6 @@ export function createMouth({ } else if (!invite.direct) { invites.delete(roomId); } else { - free ??= await released(); if (!free) continue; await matrix("POST", `/join/${room}`, {}); invites.delete(roomId); @@ -283,11 +287,19 @@ export function createMouth({ if (pending.length >= maxPending) return []; // unanswered mail is not outrun: the position stays where it is const filter = encodeURIComponent(JSON.stringify({ room: { timeline: { limit: CONTEXT } } })); const since = cursor === null ? "" : `&since=${encodeURIComponent(cursor)}`; - const reply = await matrix("GET", `/sync?timeout=${cursor === null ? 0 : syncTimeoutMs}&filter=${filter}${since}`); + const free = await released(); // the round's one brake read: presence and invites both follow it + if (!free && lastPresence !== "offline") { + try { + await matrix("PUT", `/presence/${encodeURIComponent(selfId)}/status`, { presence: "offline" }, API, AbortSignal.timeout(sendTimeoutMs)); + lastPresence = "offline"; + } catch (error) { say(`presence not set: ${error.message}`); } + } + const reply = await matrix("GET", `/sync?timeout=${cursor === null ? 0 : syncTimeoutMs}&set_presence=${free ? "online" : "offline"}&filter=${filter}${since}`); + if (free) lastPresence = "online"; const baseline = cursor === null; // first ever sync: history is context, not mail for (const event of reply.account_data?.events ?? []) if (event.type === "m.direct") noteDirect(event.content); noteInvites(reply.rooms?.invite); - await answerInvites(); + await answerInvites(free); const made = []; const round = new Map(); let truncated = false; // the pending cap was reached inside this round @@ -346,10 +358,23 @@ export function createMouth({ batches = batches.filter(b => b.maxSeq > throughSeq); } + // Her m.read receipt, display-only: public, unthreaded, at the newest event each room's ack covers. + // Never awaited by the ack; a failure is one log line and nothing else. + async function readUpTo(newest) { + for (const { roomId, eventId } of newest.values()) { + try { + await matrix("POST", `/rooms/${encodeURIComponent(roomId)}/receipt/m.read/${encodeURIComponent(eventId)}`, {}, API, AbortSignal.timeout(sendTimeoutMs)); + } catch (error) { say(`receipt in ${roomId} not posted: ${error.message}`); } + } + } + async function ack(throughSeq) { + const newest = new Map(); // roomId -> the highest-seq delivery this ack covers + for (const d of pending) if (d.seq <= throughSeq && (newest.get(d.roomId)?.seq ?? 0) < d.seq) newest.set(d.roomId, d); pending = pending.filter(d => d.seq > throughSeq); images?.acked(throughSeq); await commit(pending.length ? pending[0].seq - 1 : seqCounter); + readUpTo(newest); } function pendingAfter(afterSeq) {