Mouth: a v3 send may say resend; a resend skips the basis check and PUTs under the same txn id
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5d9fc14598
commit
618962ecfe
4 changed files with 86 additions and 5 deletions
|
|
@ -370,6 +370,8 @@ const v2Samples = {
|
|||
"gamecult.persona.chat_send_request.v2.nil-basis": { tuple: v2Request(null), parse: docs.parseSendRequestV2 },
|
||||
"gamecult.persona.chat_send_request.v2.seen-and-since": { tuple: v2Request({ seenEventId: "$seen", sinceUnixMs: 1793698425998 }), parse: docs.parseSendRequestV2 },
|
||||
"gamecult.persona.chat_send_request.v2.since-only": { tuple: v2Request({ seenEventId: null, sinceUnixMs: 1793698425998 }), parse: docs.parseSendRequestV2 },
|
||||
"gamecult.persona.chat_send_request.v3.resend": { tuple: docs.sendRequestV3Tuple({ requestId: "req-1", persona: "eureka", roomAlias: "#swarm:gamecult.org", text: "hi", basis: { seenEventId: "$seen", sinceUnixMs: 1793698425998 }, resend: true }), parse: docs.parseSendRequestV3 },
|
||||
"gamecult.persona.chat_send_request.v3.first-try": { tuple: docs.sendRequestV3Tuple({ requestId: "req-1", persona: "eureka", roomAlias: "#swarm:gamecult.org", text: "hi", basis: null }), parse: docs.parseSendRequestV3 },
|
||||
"gamecult.persona.chat_send_receipt.v2.sent": { tuple: docs.sendReceiptV2Tuple({ requestId: "req-1", eventId: "$sent" }), parse: docs.parseSendReceiptV2 },
|
||||
"gamecult.persona.chat_send_receipt.v2.room-moved": { tuple: docs.sendReceiptV2Tuple({ requestId: "req-1", refusal: "RoomMoved", newer: ["$a", "$b"] }), parse: docs.parseSendReceiptV2 },
|
||||
};
|
||||
|
|
@ -394,6 +396,9 @@ test("RoomMoved is v2 only, and the v2 documents are strict about arity and newe
|
|||
assert.throws(() => docs.parseSendRequest(v2Request(null)), /4-field/, "a v2 tuple is not a v1 request");
|
||||
assert.throws(() => docs.parseSendRequestV2(["r", "eureka", "#a:b", "x", [null]]), /2-field/);
|
||||
assert.throws(() => docs.parseSendRequestV2(["r", "eureka", "#a:b", "x", [null, -1]]));
|
||||
assert.throws(() => docs.parseSendRequestV3(v2Request(null)), /6-field/, "a v2 tuple is not a v3 request");
|
||||
assert.throws(() => docs.parseSendRequestV3([...v2Request(null), "yes"]), /resend must be a boolean/);
|
||||
assert.throws(() => docs.sendRequestV3Tuple({ requestId: "r", persona: "eureka", roomAlias: "#a:b", text: "x", resend: 1 }), /boolean/);
|
||||
});
|
||||
|
||||
test("the RUDP endpoint answers a v1 send with the three-field receipt and a v2 send with the four-field one", async () => {
|
||||
|
|
|
|||
|
|
@ -223,3 +223,62 @@ test("a send queued at the gate re-checks the brake before its PUT: braked durin
|
|||
assert.deepEqual(hs.puts().map(p => p.body.body), ["line g1"], "the queued send never reaches the room");
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
// A resend (v3): the first try answered SendFailed or never arrived, and the room moved before the repeat.
|
||||
const slowPut = { ms: 250 };
|
||||
const current = () => basisOn(swarm.at(-1)); // nothing newer when the first try is made
|
||||
const someoneAnswers = hs => hs.room.push({ type: "m.room.message", event_id: "$after-first-try", sender: `@nibu:${HS}`, origin_server_ts: t0 + 5000, content: { msgtype: "m.text", body: "answered her line" } });
|
||||
const resend = (m, requestId, basis) => m.send({ requestId, persona: "eureka", roomAlias: ALIAS, text: `line ${requestId}`, basis, resend: true });
|
||||
|
||||
test("a resend whose first try posted returns the same event id, reads no head and posts nothing new", async () => {
|
||||
const hs = await homeserver({ users: { tok: EUREKA }, delay: kind => (kind === "put" ? slowPut.ms : 0) });
|
||||
try {
|
||||
const m = mouth(hs, "eureka", "tok", { sendTimeoutMs: 80 });
|
||||
const first = await send(m, "r20", current());
|
||||
assert.deepEqual([first.eventId, first.refusal], [null, "SendFailed"], "the mouth gave up after its timeout");
|
||||
await new Promise(r => setTimeout(r, 400)); // the homeserver persisted it anyway
|
||||
assert.equal(hs.room.filter(e => e.sender === EUREKA && e.event_id.startsWith("$sent")).length, 1);
|
||||
someoneAnswers(hs);
|
||||
slowPut.ms = 0;
|
||||
const heads = hs.heads().length;
|
||||
const second = await resend(m, "r20", current());
|
||||
assert.deepEqual([second.eventId, second.refusal, second.newer], ["$sent-1", null, []]);
|
||||
assert.equal(hs.heads().length, heads, "a resend reads no head");
|
||||
assert.equal(hs.puts().at(-1).path.endsWith("/r20"), true, "under the same txn id");
|
||||
assert.equal(hs.room.filter(e => e.event_id.startsWith("$sent")).length, 1, "nothing new in the room");
|
||||
} finally { slowPut.ms = 250; await hs.close(); }
|
||||
});
|
||||
|
||||
test("a resend whose first try never posted posts once, though the room moved", async () => {
|
||||
const hs = await homeserver({ users: { tok: EUREKA } });
|
||||
try {
|
||||
someoneAnswers(hs);
|
||||
const receipt = await resend(mouth(hs, "eureka", "tok"), "r21", current());
|
||||
assert.deepEqual([receipt.eventId, receipt.refusal], ["$sent-1", null]);
|
||||
assert.equal(hs.puts().length, 1);
|
||||
assert.equal(hs.heads().length, 0);
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("a non-resend retry under the same request id is still refused RoomMoved when the room moved", async () => {
|
||||
const hs = await homeserver({ users: { tok: EUREKA } });
|
||||
try {
|
||||
const m = mouth(hs, "eureka", "tok");
|
||||
await send(m, "r22", current());
|
||||
someoneAnswers(hs);
|
||||
const receipt = await send(m, "r22", current());
|
||||
assert.deepEqual([receipt.refusal, receipt.eventId, receipt.newer], ["RoomMoved", null, ["$after-first-try"]]);
|
||||
assert.equal(hs.puts().length, 1);
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("a resend still answers PersonaMismatch, UnknownRoom and Brake as any send", async () => {
|
||||
const hs = await homeserver({ users: { tok: EUREKA } });
|
||||
try {
|
||||
const m = mouth(hs, "eureka", "tok");
|
||||
assert.equal((await m.send({ requestId: "r23", persona: "aqua", roomAlias: ALIAS, text: "x", resend: true })).refusal, "PersonaMismatch");
|
||||
assert.equal((await m.send({ requestId: "r24", persona: "eureka", roomAlias: `#nowhere:${HS}`, text: "x", resend: true })).refusal, "UnknownRoom");
|
||||
assert.equal((await mouth(hs, "eureka", "tok", { brake: async () => false }).send({ requestId: "r25", persona: "eureka", roomAlias: ALIAS, text: "x", resend: true })).refusal, "Brake");
|
||||
assert.equal(hs.puts().length, 0);
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ export const kinds = Object.freeze({
|
|||
sendRequest: "gamecult.persona.chat_send_request.v1",
|
||||
sendReceipt: "gamecult.persona.chat_send_receipt.v1",
|
||||
sendRequestV2: "gamecult.persona.chat_send_request.v2",
|
||||
sendRequestV3: "gamecult.persona.chat_send_request.v3", // the v2 tuple plus resend; answered with the v2 receipt
|
||||
sendReceiptV2: "gamecult.persona.chat_send_receipt.v2",
|
||||
refusal: "gamecult.persona.chat_refusal.v1",
|
||||
openDirectRequest: "gamecult.persona.chat_open_direct_request.v1",
|
||||
|
|
@ -129,6 +130,18 @@ export function parseSendRequestV2(t) {
|
|||
return { requestId: text(requestId, "request_id"), persona: text(persona, "persona"), roomAlias: text(roomAlias, "room_alias"), text: body, basis: parsed };
|
||||
}
|
||||
|
||||
// v3: the v2 send plus `resend`, true when this repeats an earlier send by the same caller under the same request_id.
|
||||
// A resend is not checked against the basis (the decision was made against the original); the homeserver's answer is the receipt.
|
||||
export function sendRequestV3Tuple({ resend = false, ...request }) {
|
||||
if (typeof resend !== "boolean") throw new Error("chat_send_request_v3 resend must be a boolean");
|
||||
return [...sendRequestV2Tuple(request), resend];
|
||||
}
|
||||
export function parseSendRequestV3(t) {
|
||||
const fields = tuple(t, 6, "chat_send_request_v3");
|
||||
if (typeof fields[5] !== "boolean") throw new Error("chat_send_request_v3 resend must be a boolean");
|
||||
return { ...parseSendRequestV2(fields.slice(0, 5)), resend: fields[5] };
|
||||
}
|
||||
|
||||
// `newer` is the ids of the messages that moved the room, oldest first: non-empty exactly when the refusal is RoomMoved.
|
||||
function checkReceiptV2(eventId, refusal, newer) {
|
||||
if ((eventId === null) === (refusal === null)) throw new Error("a send receipt carries exactly one of event_id and refusal");
|
||||
|
|
|
|||
|
|
@ -22,6 +22,9 @@
|
|||
// newest room event she saw): inside the room gate, one step per room across every Persona, the mouth
|
||||
// reads the room's head from Matrix and answers RoomMoved, naming the messages that moved it, instead
|
||||
// of the PUT. A v1 send, and a v2 send with no basis, are not checked; both still pass through the gate.
|
||||
// A v3 send may say resend: a repeat of an earlier send by the same caller under the same request_id. It skips the basis check
|
||||
// (her decision was made against the original basis) and PUTs under the same txnId; Synapse dedupes the txnId and returns the
|
||||
// original event id if the first try posted, and a new one if it did not.
|
||||
//
|
||||
// Follow (CHAT_FOLLOW=1, default off; docs/bridge.md "Follow"): her own send in a public room (not a DM, not encrypted, not bridged) anchors the room
|
||||
// for 10 minutes; those who spoke there in the 10 minutes before it, and whoever it tagged or replied to, get basis "follow" on their later messages
|
||||
|
|
@ -735,7 +738,7 @@ export function createMouth({
|
|||
return moved.reverse();
|
||||
}
|
||||
|
||||
async function send({ requestId, persona: asked, roomAlias, text, basis }) {
|
||||
async function send({ requestId, persona: asked, roomAlias, text, basis, resend = false }) {
|
||||
const refuse = (refusal, newer = []) => ({ requestId, eventId: null, refusal, newer });
|
||||
if (asked !== persona) return refuse("PersonaMismatch");
|
||||
if (!(await released())) return refuse("Brake");
|
||||
|
|
@ -745,7 +748,7 @@ export function createMouth({
|
|||
const roomId = speakableRoomId(roomAlias);
|
||||
if (!roomId) return refuse("UnknownRoom");
|
||||
const result = await gate.run(roomId, async () => {
|
||||
if (basis) {
|
||||
if (basis && !resend) {
|
||||
const newer = await newerThan(roomId, basis);
|
||||
if (newer.length > 0) return refuse("RoomMoved", newer);
|
||||
}
|
||||
|
|
@ -1100,7 +1103,7 @@ export async function startChatEndpoint({ mouths, runtime, port, connectionKey,
|
|||
const respond = (peer, persona, kind, recordKey, tuple) => server.send(peer, "schema", msgpack.encode(cultnet.encodeCultNetMessageForWire(
|
||||
docs.envelope({ kind, recordKey, payload: msgpack.encode(tuple), sourceRuntimeId: runtimeId, sourceAgentId: persona, sourceRole: "bifrost-persona-matrix-mouth" }), "cultnet.schema.v0")));
|
||||
const refuseUnknown = (peer, persona) => respond(peer, persona, docs.kinds.refusal, persona, docs.refusalTuple({ persona, refusal: "UnknownPersona" }));
|
||||
const served = [docs.kinds.pendingRequest, docs.kinds.pendingRequestV3, ...(replyLink ? [docs.kinds.pendingRequestV4] : []), docs.kinds.ack, docs.kinds.read, docs.kinds.sendRequest, docs.kinds.sendRequestV2, docs.kinds.openDirectRequest, ...(fetchEnabled ? [docs.kinds.fetchRequest] : [])];
|
||||
const served = [docs.kinds.pendingRequest, docs.kinds.pendingRequestV3, ...(replyLink ? [docs.kinds.pendingRequestV4] : []), docs.kinds.ack, docs.kinds.read, docs.kinds.sendRequest, docs.kinds.sendRequestV2, docs.kinds.sendRequestV3, docs.kinds.openDirectRequest, ...(fetchEnabled ? [docs.kinds.fetchRequest] : [])];
|
||||
const handle = async ({ peer, channelId, payload }) => {
|
||||
try {
|
||||
if (channelId !== "schema") throw new Error("chat request arrived on a foreign channel");
|
||||
|
|
@ -1141,8 +1144,9 @@ export async function startChatEndpoint({ mouths, runtime, port, connectionKey,
|
|||
if (!mouth) return refuseUnknown(peer, request.persona);
|
||||
mouth.read(request).catch(error => log(`read report failed: ${error?.message ?? error}`)); // the peer's next request does not wait on Matrix
|
||||
} else {
|
||||
const v2 = document.schemaId === docs.kinds.sendRequestV2;
|
||||
const request = v2 ? docs.parseSendRequestV2(tuple) : docs.parseSendRequest(tuple);
|
||||
const v3 = document.schemaId === docs.kinds.sendRequestV3;
|
||||
const v2 = v3 || document.schemaId === docs.kinds.sendRequestV2; // v3 is answered with the v2 receipt
|
||||
const request = v3 ? docs.parseSendRequestV3(tuple) : v2 ? docs.parseSendRequestV2(tuple) : docs.parseSendRequest(tuple);
|
||||
const mouth = mouths.get(request.persona);
|
||||
const receipt = mouth ? await mouth.send(request) : { requestId: request.requestId, eventId: null, refusal: "UnknownPersona", newer: [] };
|
||||
if (v2) respond(peer, request.persona, docs.kinds.sendReceiptV2, receipt.requestId, docs.sendReceiptV2Tuple(receipt));
|
||||
|
|
|
|||
Loading…
Reference in a new issue