Mouth: a failure before the PUT answers NotSent; SendFailed means a PUT was attempted
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
343d09f985
commit
a352ee5091
5 changed files with 74 additions and 8 deletions
|
|
@ -319,10 +319,12 @@ A send is one `PUT /rooms/{room}/send/m.room.message/{request_id}`: the request
|
|||
| --- | --- | --- |
|
||||
| `gamecult.persona.chat_send_request.v1` | `[request_id, persona, room_alias, text]` | `chat_send_receipt.v1` `[request_id, event_id, refusal]` |
|
||||
| `gamecult.persona.chat_send_request.v2` | the v1 tuple, then `basis`: nil or `[seen_event_id or nil, since_unix_ms]` | `chat_send_receipt.v2` `[request_id, event_id, refusal, newer]` |
|
||||
| `gamecult.persona.chat_send_request.v3` | the v2 tuple, then `resend`: a boolean | `chat_send_receipt.v2`, whose refusal may be `RoomMoved` or `ResendUnknown` |
|
||||
| `gamecult.persona.chat_send_request.v3` | the v2 tuple, then `resend`: a boolean | `chat_send_receipt.v2`, whose refusal may be `RoomMoved`, `ResendUnknown` or `NotSent` |
|
||||
|
||||
A v2 or v3 send with a `basis` is checked inside the room gate: if messages that move the room are newer than what she saw, the mouth makes no PUT and answers `RoomMoved` with their ids (`newer`, oldest first, at most 50).
|
||||
|
||||
**`NotSent` and `SendFailed`** (v2 receipt) say whether a PUT was attempted. `NotSent`: the mouth attempted no PUT and posted nothing, because something failed before it (the room join or start, the room-head read for the basis check); the request id is not remembered, so the caller sends the line again as a first send (`resend` false). A resend of that id is answered `ResendUnknown`, which is why it must not be sent as a resend. `SendFailed`: a PUT was attempted and its outcome is unknown (a timeout, a 5xx, a dropped connection); the id is remembered, and the caller may repeat it with `resend` true. A resend that itself fails before its PUT is `SendFailed`, not `NotSent`, because the try it repeats may have posted. A v1 caller has no word for `NotSent` and is answered `SendFailed`.
|
||||
|
||||
**`resend`** is true when the request repeats an earlier send by the same caller under the same `request_id`, for instance after `SendFailed` (the mouth's 10 s send timeout fires although the homeserver may still persist the event). The mouth honours it only for a request id it attempted itself (the id, the room and the time of the first PUT attempt, in memory, at most 500 per Persona, the oldest forgotten first), for the same Persona and room, less than 25 minutes after that first attempt (inside Synapse's 30 minute minimum txn dedupe window; a repeat does not renew it). It then skips the basis check, because her decision was made against the original basis, and PUTs under the same transaction id. The homeserver's answer is the receipt: the original event id if the first try posted, a new one if it did not. Every other resend is answered `ResendUnknown` (v2 receipt only) and posts nothing: the mouth cannot vouch for the earlier send, because it does not remember the id, the id is older than 25 minutes, it was attempted for another room, or the mouth restarted since. The caller must treat the line as not known to have gone out. `PersonaMismatch`, `Brake`, `EmptyText`, `UnknownRoom` and the room gate apply as for any send. `basis` stays in the tuple for shape uniformity and is ignored on a resend. A request that does not repeat an earlier send must send `resend` false, and a retry that does not say it is a resend is checked as a new send and may be answered `RoomMoved`. Golden bytes: `tests/fixtures/persona-chat-v2/` (`chat_send_request.v3.first-try`, `.resend`).
|
||||
|
||||
### Fetch a named file (`chat_fetch_request.v1`, `chat_fetch_receipt.v1`)
|
||||
|
|
|
|||
|
|
@ -391,6 +391,8 @@ test("RoomMoved is v2 only, and the v2 documents are strict about arity and newe
|
|||
assert.throws(() => docs.refusalTuple({ persona: "p", refusal: "RoomMoved" }));
|
||||
assert.deepEqual(docs.parseSendReceiptV2(docs.sendReceiptV2Tuple({ requestId: "r", refusal: "ResendUnknown" })), { requestId: "r", eventId: null, refusal: "ResendUnknown", newer: [] });
|
||||
assert.throws(() => docs.sendReceiptTuple({ requestId: "r", refusal: "ResendUnknown" }), /unknown refusal/, "v2 only, like RoomMoved");
|
||||
assert.deepEqual(docs.parseSendReceiptV2(docs.sendReceiptV2Tuple({ requestId: "r", refusal: "NotSent" })), { requestId: "r", eventId: null, refusal: "NotSent", newer: [] });
|
||||
assert.throws(() => docs.sendReceiptTuple({ requestId: "r", refusal: "NotSent" }), /unknown refusal/, "v2 only; the mouth answers a v1 caller SendFailed");
|
||||
assert.throws(() => docs.sendReceiptV2Tuple({ requestId: "r", refusal: "RoomMoved" }), /newer/, "RoomMoved names what moved the room");
|
||||
assert.throws(() => docs.sendReceiptV2Tuple({ requestId: "r", eventId: "$e", newer: ["$x"] }), /newer/, "a sent receipt names nothing");
|
||||
assert.throws(() => docs.sendReceiptV2Tuple({ requestId: "r", refusal: "RoomMoved", newer: Array.from({ length: 51 }, (_, i) => `$${i}`) }), /50/);
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ const edit = synthetic("replace", t0 + 20, `@aqua:${HS}`, { msgtype: "m.text", b
|
|||
const notice = synthetic("notice", t0 + 30, `@root-watch:${HS}`, { msgtype: "m.notice", body: "sudo audit line" });
|
||||
|
||||
// users: bearer token -> user id. events: the room's history, oldest first. hooks.delay(kind) -> ms to hold a GET /messages or a PUT.
|
||||
async function homeserver({ events = swarm, users, headStatus = 200, headHangs = false, delay = () => 0, failPut = () => false } = {}) {
|
||||
async function homeserver({ events = swarm, users, headStatus = 200, headHangs = false, delay = () => 0, failPut = () => false, whoamiStatus = 200 } = {}) {
|
||||
const room = [...events];
|
||||
const seen = [];
|
||||
const sent = new Map();
|
||||
|
|
@ -41,7 +41,7 @@ async function homeserver({ events = swarm, users, headStatus = 200, headHangs =
|
|||
const reply = (status, body) => { res.writeHead(status, { "content-type": "application/json" }); res.end(JSON.stringify(body)); };
|
||||
const record = { method: req.method, path, body: raw ? JSON.parse(raw) : null, who: users[bearer], query: url.searchParams };
|
||||
seen.push(record);
|
||||
if (path === "/account/whoami") return reply(200, { user_id: users[bearer] });
|
||||
if (path === "/account/whoami") return whoamiStatus !== 200 ? reply(whoamiStatus, { errcode: "M_UNKNOWN", error: "Internal server error" }) : reply(200, { user_id: users[bearer] });
|
||||
if (path === "/joined_rooms") return reply(200, { joined_rooms: [ROOM, ROOM2] });
|
||||
if (path.includes("/account_data/m.direct")) return reply(404, { errcode: "M_NOT_FOUND", error: "Account data not found" });
|
||||
if (path.startsWith("/directory/room/")) return reply(200, { room_id: path.includes("other") ? ROOM2 : ROOM, servers: [HS] });
|
||||
|
|
@ -140,13 +140,13 @@ test("no seen event: the since floor decides; a seen event outside the page: the
|
|||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("a failed head read makes no PUT and answers SendFailed; so does a head read that hangs", async () => {
|
||||
test("a failed head read makes no PUT and answers NotSent; so does a head read that hangs", async () => {
|
||||
for (const mode of [{ headStatus: 500 }, { headHangs: true }]) {
|
||||
const hs = await homeserver({ users: { tok: EUREKA }, ...mode });
|
||||
const logs = [];
|
||||
try {
|
||||
const receipt = await send(mouth(hs, "eureka", "tok", { sendTimeoutMs: 80, log: l => logs.push(l) }), "r7", basisOn(question));
|
||||
assert.deepEqual([receipt.eventId, receipt.refusal, receipt.newer], [null, "SendFailed", []], JSON.stringify(mode));
|
||||
assert.deepEqual([receipt.eventId, receipt.refusal, receipt.newer], [null, "NotSent", []], JSON.stringify(mode));
|
||||
assert.equal(hs.puts().length, 0, "the guarantee does not fail open");
|
||||
} finally { await hs.close(); }
|
||||
}
|
||||
|
|
@ -344,3 +344,56 @@ test("the attempt record is bounded: the oldest id is forgotten, never a newer o
|
|||
assert.equal((await resend(m, "b500", undefined)).refusal, null);
|
||||
} finally { await hs.close(); }
|
||||
}, { timeout: 60000 });
|
||||
|
||||
// NotSent: the mouth failed before any PUT. Nothing is remembered, so a resend of that id is ResendUnknown and the caller sends it as a first send.
|
||||
test("a failure before the PUT answers NotSent, records no attempt, and the id then goes out as a first send", async () => {
|
||||
for (const mode of [{ headStatus: 500 }, { headHangs: true }, { whoamiStatus: 500 }]) {
|
||||
clock.t = T0;
|
||||
const down = { ...mode };
|
||||
const hs = await homeserver({ users: { tok: EUREKA }, ...down });
|
||||
try {
|
||||
const m = timed(hs, "eureka", "tok", { sendTimeoutMs: 80 });
|
||||
const first = await send(m, "r30", current());
|
||||
assert.deepEqual([first.eventId, first.refusal, first.newer], [null, "NotSent", []], JSON.stringify(mode));
|
||||
assert.equal(hs.puts().length, 0, JSON.stringify(mode));
|
||||
if (!mode.whoamiStatus) unknown(await resend(m, "r30", current())); // no PUT was attempted, so there is nothing to vouch for (a resend with the join down fails first, SendFailed)
|
||||
assert.equal(hs.puts().length, 0);
|
||||
} finally { await hs.close(); }
|
||||
}
|
||||
const hs = await homeserver({ users: { tok: EUREKA } });
|
||||
try {
|
||||
const receipt = await send(timed(hs, "eureka", "tok"), "r30", current());
|
||||
assert.deepEqual([receipt.eventId, receipt.refusal], ["$sent-1", null], "the same id as a first send posts");
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("a timed-out PUT answers SendFailed with the attempt recorded; a resend that fails before its PUT is SendFailed too", async () => {
|
||||
clock.t = T0;
|
||||
const hs = await homeserver({ users: { tok: EUREKA }, failPut: () => true });
|
||||
try {
|
||||
const m = timed(hs, "eureka", "tok", { sendTimeoutMs: 80 });
|
||||
const first = await send(m, "r31", current());
|
||||
assert.deepEqual([first.eventId, first.refusal], [null, "SendFailed"]);
|
||||
assert.equal(hs.puts().length, 1, "a PUT was attempted");
|
||||
const again = await resend(m, "r31", current());
|
||||
assert.equal(again.refusal, "SendFailed", "vouched: the PUT is attempted again");
|
||||
assert.equal(hs.puts().length, 2);
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("a resend that fails before its PUT is SendFailed, not NotSent: the earlier try may have posted", async () => {
|
||||
clock.t = T0;
|
||||
const hs = await homeserver({ users: { tok: EUREKA }, headStatus: 500 });
|
||||
try {
|
||||
const m = timed(hs, "eureka", "tok", { sendTimeoutMs: 80 });
|
||||
const first = await m.send({ requestId: "r32", persona: "eureka", roomAlias: ALIAS, text: "x", basis: current(), resend: true });
|
||||
assert.equal(first.refusal, "ResendUnknown", "a resend skips the head read, so only the vouch can refuse it");
|
||||
} finally { await hs.close(); }
|
||||
const down = await homeserver({ users: { tok: EUREKA }, whoamiStatus: 500 });
|
||||
try {
|
||||
const m = timed(down, "eureka", "tok", { sendTimeoutMs: 80 });
|
||||
const receipt = await resend(m, "r32", current());
|
||||
assert.deepEqual([receipt.eventId, receipt.refusal], [null, "SendFailed"], "the join failed before the PUT, and the earlier try it repeats is unknown");
|
||||
assert.equal(down.puts().length, 0);
|
||||
} finally { await down.close(); }
|
||||
});
|
||||
|
|
|
|||
|
|
@ -31,7 +31,9 @@ export const refusals = Object.freeze(["Brake", "PersonaMismatch", "UnknownRoom"
|
|||
|
||||
// RoomMoved and ResendUnknown exist only in the v2 receipt: the v1 tuple, a v1 parse and the typed refusal document do not know it.
|
||||
// ResendUnknown too: a resend the mouth cannot vouch for (it does not remember the request id for that Persona and room, or it is older than the resend window); nothing was posted.
|
||||
export const refusalsV2 = Object.freeze([...refusals, "RoomMoved", "ResendUnknown"]);
|
||||
// NotSent too: the mouth made no PUT and posted nothing (the room join or the head read failed first); safe to send again as a first send.
|
||||
// SendFailed then means a PUT was attempted and its outcome is unknown. A v1 caller gets SendFailed for a NotSent.
|
||||
export const refusalsV2 = Object.freeze([...refusals, "RoomMoved", "ResendUnknown", "NotSent"]);
|
||||
|
||||
// The typed refusal document also answers a request of a kind this mouth does not serve (no Persona is known then).
|
||||
const refusalDocument = Object.freeze([...refusals, "UnknownKind"]);
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@
|
|||
// original event id if the first try posted, and a new one if it did not. The mouth honours resend only for a request id it attempted
|
||||
// itself, for the same Persona and room, less than RESEND_WINDOW_MS ago (inside Synapse's 30 minute txn dedupe); any other resend
|
||||
// (forgotten, too old, lost in a restart, another room) is answered ResendUnknown and posts nothing. The record is memory only, bounded.
|
||||
// A failure before the PUT (start or room join, the room-head read) is answered NotSent: nothing posted and no PUT attempted, so the id is not
|
||||
// remembered and the caller sends it again as a first send. SendFailed means a PUT was attempted and its outcome is unknown. A resend that fails
|
||||
// before its PUT is SendFailed: the earlier try it repeats may have posted.
|
||||
//
|
||||
// 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
|
||||
|
|
@ -760,6 +763,7 @@ export function createMouth({
|
|||
if (asked !== persona) return refuse("PersonaMismatch");
|
||||
if (!(await released())) return refuse("Brake");
|
||||
if (typeof text !== "string" || text.trim() === "") return refuse("EmptyText");
|
||||
let putAttempted = false; // set the moment before this call's PUT; a failure after it is SendFailed, one before it NotSent
|
||||
try {
|
||||
if (!started || unjoined()) await start();
|
||||
const roomId = speakableRoomId(roomAlias);
|
||||
|
|
@ -772,6 +776,7 @@ export function createMouth({
|
|||
}
|
||||
if (!(await released())) return refuse("Brake"); // a brake placed during the gate wait or the head read stops this send
|
||||
remember(requestId, roomId);
|
||||
putAttempted = true;
|
||||
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: [] };
|
||||
});
|
||||
|
|
@ -781,8 +786,9 @@ export function createMouth({
|
|||
}
|
||||
return result;
|
||||
} catch (error) {
|
||||
say(`send failed: ${error.message}`);
|
||||
return refuse("SendFailed");
|
||||
say(`send failed${putAttempted ? "" : " before any PUT"}: ${error.message}`);
|
||||
// NotSent: this call attempted no PUT, so it posted nothing, and no earlier try can have (a resend has one: its outcome stays unknown).
|
||||
return refuse(putAttempted || resend ? "SendFailed" : "NotSent");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1168,6 +1174,7 @@ export async function startChatEndpoint({ mouths, runtime, port, connectionKey,
|
|||
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 && receipt.refusal === "NotSent") receipt.refusal = "SendFailed"; // the v1 receipt has no word for it; SendFailed is the safe reading
|
||||
if (v2) respond(peer, request.persona, docs.kinds.sendReceiptV2, receipt.requestId, docs.sendReceiptV2Tuple(receipt));
|
||||
else respond(peer, request.persona, docs.kinds.sendReceipt, receipt.requestId, docs.sendReceiptTuple(receipt));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue