diff --git a/docs/bridge.md b/docs/bridge.md index 14ede89..4468988 100644 --- a/docs/bridge.md +++ b/docs/bridge.md @@ -206,7 +206,7 @@ A client asks for the facts by the kind it polls with: `gamecult.persona.chat_pe | Field | Type | Meaning | |---|---|---| -| 12 `basis` | `"direct"`, `"tag"`, `"reply"`, `"room"` or nil | Why the message is for this Persona, by what Matrix says, first that holds: `direct` the room is one of her DMs; `tag` `m.mentions.user_ids` lists her id, or the client sent no `m.mentions` at all and an `` in the HTML body is exactly her `matrix.to` link; `reply` it replies to, or is in a thread under, an event her account wrote (her Self run speaks as her account, so its words count; any member's message in a thread under one of her events is a `reply` too, and the target is read once per room and event, a failure for a minute); `room` an `@room` mention. A `notice` or an `edit` costs no reply lookup. | +| 12 `basis` | `"direct"`, `"tag"`, `"reply"`, `"room"` or nil | Why the message is for this Persona, by what Matrix says, first that holds: `direct` the room is one of her DMs; `tag` `m.mentions.user_ids` lists her id, or the client sent no `m.mentions` at all and an `` in the HTML body is exactly her `matrix.to` link; `reply` it replies to, or is in a thread under, an event her account wrote (her Self run speaks as her account, so its words count; any member's message in a thread under one of her events is a `reply` too, and the target is read once per room and event, a failure for a minute); `room` an `@room` mention. A `notice` or an `edit` costs no reply lookup. Known limit: lookups run one at a time inside the `/sync` round (10 s each, at most two per reply), so a slow homeserver slows the round; they are not parallelised or capped per round. | | 13 `form` | `"message"`, `"notice"` or `"edit"` | `m.notice` is an automated line; `m.replace` is an edit of an earlier message. Only a `message` can address anyone. | What the mouth cannot know, and so what a nil `basis` means: **unproven, not "not for her"**. Her handle and public name in the text (any casing, with or without `@`, `'s`), the names her stewardships list, a project's repo name, and whether the sender is the operator or a Mind are all matched by Huginn, not here. A reply whose target the homeserver will not give is nil and logged. The mouth also cannot see whether a message was meant for her without naming her. Every delivery is still delivered, claimed tags included: nothing is dropped in the mouth, so the Huginn rule cannot lose a message the mouth had no way to judge. diff --git a/tests/persona-matrix-mouth.test.mjs b/tests/persona-matrix-mouth.test.mjs index fcaa255..9458370 100644 --- a/tests/persona-matrix-mouth.test.mjs +++ b/tests/persona-matrix-mouth.test.mjs @@ -1444,17 +1444,34 @@ test("addressing facts: a pill is her exact matrix.to anchor: a longer id, a non withContent("$a", META, html(`x`)), withContent("$b", META, html(`x`)), withContent("$c", META, html(`Eureka`)), + withContent("$d", META, html(`
her said
ok`)), ]); - assert.deepEqual(made.map(d => d.basis), [null, null, "tag"]); + assert.deepEqual(made.map(d => d.basis), [null, null, "tag", null]); }); -test("addressing facts: an edit or a notice replying to her costs no target fetch, and a target that failed is not refetched within a minute", async () => { - const rel = { "m.relates_to": { "m.in_reply_to": { event_id: "$gone" } } }; +test("addressing facts: an edit or a notice replying to her costs no target fetch", async () => { + const to = id => ({ "m.relates_to": { "m.in_reply_to": { event_id: id } } }); const { made, hs } = await deliver([ - withContent("$n", META, { msgtype: "m.notice", body: "n", ...rel }), - withContent("$r1", META, { body: "a", ...rel }), - withContent("$r2", META, { body: "b", ...rel }), + withContent("$n", META, { msgtype: "m.notice", body: "n", ...to("$noticed") }), + withContent("$e", META, { body: "* e", "m.relates_to": { rel_type: "m.replace", event_id: "$orig", "m.in_reply_to": { event_id: "$edited" } } }), ]); - assert.deepEqual(facts(made), [["$n", null, "notice"], ["$r1", null, "message"], ["$r2", null, "message"]]); - assert.equal(hs.seen.filter(r => r.path.includes("/event/")).length, 1); + assert.deepEqual(facts(made), [["$n", null, "notice"], ["$e", null, "edit"]]); + assert.equal(hs.seen.filter(r => r.path.includes("/event/")).length, 0); +}); + +test("addressing facts: a target that failed is not refetched within a minute, and is after it", async () => { + let t = 1_000_000; + const rel = { "m.relates_to": { "m.in_reply_to": { event_id: "$gone" } } }; + const round = id => joined({ [ROOM]: { timeline: { events: [withContent(id, META, { body: id, ...rel })] } } }); + const script = { null: { next_batch: "b0" }, b0: { next_batch: "b1", ...round("$r1") }, b1: { next_batch: "b2", ...round("$r2") }, b2: { next_batch: "b3", ...round("$r3") } }; + const hs = await fakeHomeserver({ sync: since => script[since] ?? { next_batch: since } }); + const fetched = () => hs.seen.filter(r => r.path.includes("/event/")).length; + try { + const mouth = mouthFor(hs, { now: () => t }); + await mouth.syncOnce(); await mouth.syncOnce(); + t += 59_000; await mouth.syncOnce(); + assert.equal(fetched(), 1, "inside the minute: not refetched"); + t += 2_000; await mouth.syncOnce(); + assert.equal(fetched(), 2, "past the minute: read again"); + } finally { await hs.close(); } }); diff --git a/tools/persona-matrix-mouth.mjs b/tools/persona-matrix-mouth.mjs index 636a303..439816e 100644 --- a/tools/persona-matrix-mouth.mjs +++ b/tools/persona-matrix-mouth.mjs @@ -302,7 +302,7 @@ export function createMouth({ return mentions?.room === true ? "room" : null; } // A pill is an anchor whose href is exactly her matrix.to link; a longer id, an attribute that merely contains one and a quoted reply do not count. - const pillFor = html => typeof html === "string" && [...html.matchAll(/]*?href\s*=\s*(["'])(.*?)\1/gi)].some(([, , href]) => [selfId, encodeURIComponent(selfId)].some(id => href === `https://matrix.to/#/${id}`)); + const pillFor = html => typeof html === "string" && [...html.replace(/[\s\S]*?<\/mx-reply>/gi, "").matchAll(/]*?href\s*=\s*(["'])(.*?)\1/gi)].some(([, , href]) => [selfId, encodeURIComponent(selfId)].some(id => href === `https://matrix.to/#/${id}`)); const authors = new Map(); // `${room id} ${event id}` -> { sender, at }: read results, a failure kept for NEGATIVE_MS, bounded; the key holds the room so another room's reply cannot borrow a hit const NEGATIVE_MS = 60 * 1000; async function authorOf(roomId, eventId) {