persona mouth: opens for one person run one at a time; declines count in encrypted rooms
Soul findings on 77975b9: concurrent opens could make two rooms; a decline in an
encrypted DM was ignored; the not-E2EE notice could be lost after a held first
line; the unknown-kind refusal echoed an unbounded agent name; isServed could
read a mouth that had not started. Tests now pin the brake read before each
write exactly, the two-member DM rule, and that project rooms are not scanned.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
77975b9f0e
commit
29fba0965f
2 changed files with 97 additions and 14 deletions
|
|
@ -15,6 +15,7 @@ const ALIAS = `#swarm:${HS}`;
|
|||
async function homeserver({ people = [META, `@nova:${HS}`], now = () => 1000, failInvite: initialFailInvite = false } = {}) {
|
||||
const config = { failInvite: initialFailInvite };
|
||||
const rooms = new Map(); // room id -> { events }
|
||||
const stateReads = [];
|
||||
const log = []; // every write, in order, as "METHOD path"
|
||||
let counter = 0;
|
||||
const direct = { value: null };
|
||||
|
|
@ -41,6 +42,7 @@ async function homeserver({ people = [META, `@nova:${HS}`], now = () => 1000, fa
|
|||
if (path.startsWith("/directory/room/")) return reply(200, { room_id: PROJECT });
|
||||
if (path.startsWith("/profile/")) return people.includes(path.slice("/profile/".length)) ? reply(200, { displayname: "x" }) : reply(404, { errcode: "M_NOT_FOUND" });
|
||||
const state = path.match(/^\/rooms\/(.+)\/state$/);
|
||||
if (state) stateReads.push(state[1]);
|
||||
if (state) return rooms.has(state[1]) ? reply(200, rooms.get(state[1]).events) : reply(403, {});
|
||||
if (req.method === "POST" && path === "/createRoom") {
|
||||
write();
|
||||
|
|
@ -67,7 +69,7 @@ async function homeserver({ people = [META, `@nova:${HS}`], now = () => 1000, fa
|
|||
});
|
||||
await new Promise(ok => server.listen(0, "127.0.0.1", ok));
|
||||
return {
|
||||
url: `http://127.0.0.1:${server.address().port}`, rooms, log, direct,
|
||||
url: `http://127.0.0.1:${server.address().port}`, rooms, log, direct, stateReads,
|
||||
set failInvite(v) { config.failInvite = v; },
|
||||
seedDm(id, user, membership, { encrypted = false, created = 5, marker } = {}) {
|
||||
rooms.set(id, { events: [{ type: "m.room.create", state_key: "", origin_server_ts: created, content: { creator: ME, ...(marker ? { "org.gamecult.open_request": marker } : {}) } }] });
|
||||
|
|
@ -163,7 +165,7 @@ test("the brake is read fresh before createRoom, before the invite and before th
|
|||
}
|
||||
const writes = events.filter(e => e.startsWith("write"));
|
||||
for (let i = 0; i < events.length; i++) if (events[i].startsWith("write")) assert.equal(events[i - 1], "free", `free=${free}: ${events[i]} was not immediately preceded by a released brake read`);
|
||||
if (free >= 5) assert.equal(writes.length, 3);
|
||||
assert.equal(writes.length, Math.min(3, free - 1), `free=${free}: reads 2, 3 and 4 each gate exactly the createRoom, the invite and the line`);
|
||||
} finally { await hs.close(); }
|
||||
}
|
||||
});
|
||||
|
|
@ -273,3 +275,70 @@ test("a homeserver failure answers SendFailed and writes nothing the brake or a
|
|||
assert.equal(outcome(await ask(m, "r1")), "SendFailed");
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("two requests for one person at once make one room: opens for a person run one at a time", async () => {
|
||||
const hs = await homeserver();
|
||||
try {
|
||||
const m = mouthFor(hs);
|
||||
const answers = await Promise.all([ask(m, "a"), ask(m, "b")]);
|
||||
assert.equal(hs.writes(/createRoom/).length, 1);
|
||||
assert.deepEqual(answers.map(outcome).sort(), ["InvitePending", "Opened"]);
|
||||
const other = await Promise.all([ask(m, "c", `@nova:${HS}`), ask(m, "d", META)]);
|
||||
assert.equal(other[0].outcome, "Opened", "another person is not held behind");
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("a decline stands in an encrypted room too, and a pending invite there is still pending: no new room, no line", async () => {
|
||||
for (const [membership, expected] of [["leave", "Declined"], ["invite", "InvitePending"]]) {
|
||||
const hs = await homeserver();
|
||||
try {
|
||||
hs.seedDm("!enc:gamecult.org", META, membership, { encrypted: true });
|
||||
assert.equal(outcome(await ask(mouthFor(hs), "r1")), expected);
|
||||
assert.deepEqual(hs.log, []);
|
||||
} finally { await hs.close(); }
|
||||
}
|
||||
});
|
||||
|
||||
test("the not-E2EE notice reaches the person even when the first line was held back: a later line into the room this path made carries it once", async () => {
|
||||
const hs = await homeserver();
|
||||
try {
|
||||
let reads = 0;
|
||||
const m = mouthFor(hs, { brake: async () => ++reads <= 3 }); // top, createRoom, invite: then the brake is held before the line
|
||||
assert.equal(outcome(await ask(m, "r1")), "Brake");
|
||||
const roomId = [...hs.rooms.keys()][0];
|
||||
assert.equal(hs.rooms.get(roomId).sent, undefined, "the invite went out, the line did not");
|
||||
hs.set(roomId, META, "join");
|
||||
const answer = await ask(mouthFor(hs), "r2", META, "second");
|
||||
assert.deepEqual([answer.outcome, answer.roomId], ["Existing", roomId]);
|
||||
assert.match(hs.rooms.get(roomId).sent.get("r2").body.body, /^This room is not end-to-end encrypted.*\n\nsecond$/s);
|
||||
await ask(mouthFor(hs), "r3", META, "third"); // another process: the notice is repeated at worst once, never lost
|
||||
const sameProcess = mouthFor(hs);
|
||||
await ask(sameProcess, "r4", META, "fourth");
|
||||
await ask(sameProcess, "r5", META, "fifth");
|
||||
assert.equal(hs.rooms.get(roomId).sent.get("r5").body.body, "fifth", "once the notice went out in this process it is not repeated");
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("the lookup finds a DM only: a group room with the person is not one, and project rooms are never scanned", async () => {
|
||||
const hs = await homeserver();
|
||||
try {
|
||||
hs.seedDm("!group:gamecult.org", META, "join");
|
||||
hs.set("!group:gamecult.org", `@third:${HS}`, "join"); // three members
|
||||
const r = await ask(mouthFor(hs), "r1");
|
||||
assert.equal(r.outcome, "Opened");
|
||||
assert.notEqual(r.roomId, "!group:gamecult.org");
|
||||
assert.equal(hs.rooms.get("!group:gamecult.org").sent, undefined);
|
||||
assert.ok(!hs.stateReads.includes("!project:gamecult.org"), "a configured project room is not read as a DM candidate");
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
||||
test("another Mind this process serves is not a person, even before that Mind's own mouth has started", async () => {
|
||||
const hs = await homeserver();
|
||||
try {
|
||||
const nova = `@nova:${HS}`;
|
||||
let asked = null;
|
||||
const m = mouthFor(hs, { isServed: async id => { asked = id; return id === nova; } });
|
||||
assert.equal(outcome(await ask(m, "r1", nova)), "NotAPerson");
|
||||
assert.equal(asked, nova);
|
||||
} finally { await hs.close(); }
|
||||
});
|
||||
|
|
|
|||
|
|
@ -117,7 +117,7 @@ export function createMouth({
|
|||
now = Date.now, log = () => {}, syncTimeoutMs = 30000, maxPending = MAX_PENDING, gate = createRoomGate(), sendTimeoutMs = SEND_TIMEOUT_MS,
|
||||
presenceGraceMs = PRESENCE_GRACE_MS,
|
||||
images = null, // this Persona's image store (createImageStore), or null: images stay the typed text line
|
||||
isServed = () => false, // whether a Matrix user id is an account this process speaks for: a Mind is not a person to open a DM with
|
||||
isServed = async () => false, // (async) whether a Matrix user id is an account this process speaks for: a Mind is not a person to open a DM with
|
||||
}) {
|
||||
if (!persona || !homeserver || !token || !rooms?.length) throw new Error("persona, homeserver, rooms and token are required");
|
||||
const say = message => log(redact(String(message), token));
|
||||
|
|
@ -142,6 +142,8 @@ export function createMouth({
|
|||
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
|
||||
const noticed = new Set(); // rooms this mouth created where the not-E2EE first line has gone out
|
||||
const personTails = new Map(); // person's user id -> the open in flight: opens for one person run one at a time
|
||||
const openedOn = new Map(); // person's user id -> the UTC day this process last created a room for them: one creation per person per day
|
||||
|
||||
async function matrix(method, path, body, prefix = API, signal) {
|
||||
|
|
@ -501,16 +503,28 @@ export function createMouth({
|
|||
}
|
||||
|
||||
// The first line of an open: the invite is the ask and the room is not end-to-end encrypted, and the line says so.
|
||||
async function putLine(roomId, requestId, text, first) {
|
||||
// Every line into a room this path created carries the notice until one has gone out (a restart repeats it once at worst).
|
||||
async function putLine(roomId, requestId, text, made) {
|
||||
return gate.run(roomId, async () => {
|
||||
if (!(await released())) return null;
|
||||
const body = first ? `${NOT_E2EE}\n\n${text}` : text;
|
||||
return (await matrix("PUT", `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${encodeURIComponent(requestId)}`, { msgtype: "m.text", body }, API, AbortSignal.timeout(sendTimeoutMs))).event_id;
|
||||
const body = made && !noticed.has(roomId) ? `${NOT_E2EE}\n\n${text}` : text;
|
||||
const sent = (await matrix("PUT", `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${encodeURIComponent(requestId)}`, { msgtype: "m.text", body }, API, AbortSignal.timeout(sendTimeoutMs))).event_id;
|
||||
if (made) noticed.add(roomId);
|
||||
return sent;
|
||||
});
|
||||
}
|
||||
|
||||
// A Persona's request for a direct room with one person. One outcome word and, for Opened and Existing, the room and the line's event.
|
||||
async function openDirect({ requestId, persona: asked, userId, text }) {
|
||||
function openDirect(request) {
|
||||
const key = typeof request.userId === "string" ? request.userId : "";
|
||||
const run = () => openDirectNow(request);
|
||||
const tail = (personTails.get(key) ?? Promise.resolve()).then(run, run);
|
||||
personTails.set(key, tail);
|
||||
const done = () => { if (personTails.get(key) === tail) personTails.delete(key); };
|
||||
tail.then(done, done);
|
||||
return tail;
|
||||
}
|
||||
async function openDirectNow({ requestId, persona: asked, userId, text }) {
|
||||
const answer = (outcome, roomId = null, eventId = null) => { say(`open direct ${requestId}: ${outcome}${roomId ? ` ${roomId}` : ""}`); return { requestId, outcome, roomId, eventId }; };
|
||||
if (asked !== persona) return answer("PersonaMismatch");
|
||||
if (!(await released())) return answer("Brake");
|
||||
|
|
@ -518,23 +532,23 @@ export function createMouth({
|
|||
try {
|
||||
if (!started || unjoined()) await start();
|
||||
if (!serverName || typeof userId !== "string" || userId.slice(userId.indexOf(":") + 1) !== serverName) return answer("ForeignUser");
|
||||
if (userId === selfId || isServed(userId)) return answer("NotAPerson");
|
||||
if (userId === selfId || await isServed(userId)) return answer("NotAPerson");
|
||||
try { await matrix("GET", `/profile/${encodeURIComponent(userId)}`); } catch (error) {
|
||||
if (/ 404 /.test(error.message)) return answer("NoSuchUser");
|
||||
throw error;
|
||||
}
|
||||
const rooms = await directsWith(userId);
|
||||
const usable = rooms.filter(r => !r.encrypted);
|
||||
const usable = rooms.filter(r => !r.encrypted); // a line is never sent into an encrypted room
|
||||
const resumed = usable.find(r => r.marker === requestId); // a repeat of this very request after a crash
|
||||
const live = resumed ?? usable.find(r => r.theirs === "join");
|
||||
if (live) {
|
||||
const eventId = await putLine(live.roomId, requestId, text, resumed !== undefined);
|
||||
const eventId = await putLine(live.roomId, requestId, text, live.marker !== null);
|
||||
return eventId === null ? answer("Brake") : answer(resumed ? "Opened" : "Existing", live.roomId, eventId);
|
||||
}
|
||||
const latest = usable.find(r => r.theirs !== null);
|
||||
const latest = rooms.find(r => ["invite", "leave", "ban"].includes(r.theirs)); // an invite or a decline stands in an encrypted room too
|
||||
if (latest?.theirs === "invite") return answer("InvitePending");
|
||||
if (latest) return answer("Declined"); // left or banned, and nothing newer: only the person reopens it, by inviting her
|
||||
let roomId = usable.find(r => r.theirs === null)?.roomId ?? null; // made earlier, the invite never went out
|
||||
let roomId = usable.find(r => r.theirs === null && r.marker !== null)?.roomId ?? null; // made here earlier, the invite never went out
|
||||
if (roomId === null) {
|
||||
if (openedOn.get(userId) === dayOf(now())) return answer("DailyLimit");
|
||||
if (!(await released())) return answer("Brake");
|
||||
|
|
@ -655,7 +669,7 @@ export async function startChatEndpoint({ mouths, runtime, port, connectionKey,
|
|||
const document = docs.checkEnvelope(message, null);
|
||||
if (!served.includes(document.schemaId)) { // a kind this mouth does not serve is answered, loudly, and logged: never dropped
|
||||
log(`chat endpoint refused a request of an unknown kind: ${String(document.schemaId).slice(0, 120)}`);
|
||||
const who = typeof document.sourceAgentId === "string" && document.sourceAgentId !== "" ? document.sourceAgentId : "unknown";
|
||||
const who = typeof document.sourceAgentId === "string" && SLUG.test(document.sourceAgentId) ? document.sourceAgentId : "unknown";
|
||||
return respond(peer, who, docs.kinds.refusal, who, docs.refusalTuple({ persona: who, refusal: "UnknownKind" }));
|
||||
}
|
||||
const tuple = msgpack.decode(document.payload);
|
||||
|
|
@ -770,7 +784,7 @@ export async function main(env = process.env) {
|
|||
const mouths = new Map(personas.map(persona => {
|
||||
mkdirSync(join(stateRoot, persona), { recursive: true });
|
||||
return [persona, createMouth({
|
||||
persona, homeserver, serverName, rooms: roomsByPersona.get(persona), token: tokens.get(persona), gate, isServed: id => [...mouths.values()].some(m => m.userId === id), log: line => log(`${persona}: ${line}`),
|
||||
persona, homeserver, serverName, rooms: roomsByPersona.get(persona), token: tokens.get(persona), gate, isServed: async id => { await Promise.all([...mouths.values()].map(m => m.start().catch(() => {}))); return [...mouths.values()].some(m => m.userId === id); }, log: line => log(`${persona}: ${line}`),
|
||||
images: imageRoot ? createImageStore({ root: join(imageRoot, persona), log: line => log(`${persona}: ${line}`) }) : null,
|
||||
state: cultcacheState({ runtime, path: join(stateRoot, persona, "cursor.cc") }),
|
||||
brake: createBrakeReader({ controlPath: join(controlRoot, persona, "control.cc"), readStore: runtime.readSingleFileStore, decode: runtime.msgpack.decode }),
|
||||
|
|
|
|||
Loading…
Reference in a new issue