Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ without checking it is the request that was built (`roomsKeysAct` does).
## A portal page uses rooms through the wallet's own methods (P7b)

`window.vtaWallet.rooms.{list, choose, forget, info, browse, read, conversation,
putNote, putMessage, getFile, putFile}` is how a community's member portal shows a member their rooms
putNote, putMessage, react, unreact, watch, getFile, putFile}` is how a community's member portal shows a member their rooms
(VTI `data-rooms-files.md` §8.5). It does **not** loosen "a page is not a
member": `requestTask` and `signTrustTask` still refuse the whole `rooms/`
family. The methods fix the verb, run the flow in the extension, and return
Expand Down Expand Up @@ -1107,6 +1107,36 @@ wallet's window like a note, worded as a message. Keep `putNote`'s title
required: a note and a message are different records, and a page choosing
between them says which.

**A page replies and reacts through the wallet, and only where the room takes
it.** `putMessage({ replyTo })` writes `inReplyTo`; `react` writes a record of
kind `reaction` (the value as `body`, naming the record — exactly what
`assembleRoomRecords` counts); `unreact` writes an empty version over each of
the member's own live reactions with that value on that record
(`ownReactions`), which only their author may. Before the member is asked, the
background reads the room's profile as the member (`recordMembers`) and
refuses with `recordMembersNotListed` where it does not list what the write
needs — a reply is never sent without `inReplyTo`, a reaction never as a
message. The confirm window shows the record answered or reacted to **as the
wallet read it** (`rooms/keys/read`, `glimpseOf`), never anything the page
said about it. Each reaction is confirmed like any write: no "remember".

**A page learns a shared room moved over a port, never a broadcast.**
`rooms.watch` opens one `ROOM_WATCH_PAGE_PORT` per watch (the content script
relays `roomWatch` frames as it relays `roomFile` ones). The background checks
the port's browser-attested origin and the share exactly as for a call, holds
the watch in its own `RoomWatcher` (one per background, so two tabs share a
room's watch), arms it through the member's VTA (`rooms/keys/watch/0.1`, the
offscreen `watch` op) after each read the page reports, and passes the room's
`rooms/keys/changed` — taken only from the offscreen document — to the ports
that armed it, as `{ kind: "changed", latestVersion }` and nothing more, after
checking the share is still live. Closing the port (`stop`, the tab going)
releases it; the last release cancels it at the VTA. The page pings the port
every 20 s, which keeps the worker alive while it watches; a worker restart
closes the port, and the page watches again. **What breaks it:** sending a
room's signal through `RUNTIME_BROADCAST_EVENT` (it reaches every tab of every
origin); passing a signal for a watch no page armed, or after the share went;
re-arming on a signal rather than after the page's read.

**An open room's file carries its own key.** With no storage key to derive from, `putRoomFile` mints
32 random bytes for the file, seals the blob exactly as on a sealed tier, and
writes `rooms/records/put/0.4` with the manifest — `fileKey` included — in
Expand Down
1 change: 1 addition & 0 deletions docs/page-bridge-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ source; a test fails if this table and that list differ.
| `notShared` | A room the page asked about is not shared with it, or its share ran out: ask the member to share it again (`rooms.choose`). |
| `offerNotListed` | Rooms: the community lists no such offer to the member now. |
| `offerInProgress` | Rooms: this offer is being accepted already, from another surface. |
| `recordMembersNotListed` | Rooms: the room's profile does not list the record members this write needs — `inReplyTo` for a reply, `kind` and `inReplyTo` for a reaction — so nothing was written. Someone holding `curate` or `admin` in the room turns them on. Offer the plain write (a message without the reply) instead. |
| `membershipNotHeld` | Elections: the member holds no membership credential issued by the party the election is put to, so they are not a member of that party here. |
| `credentialNotHeld` | Directory: the member's wallet holds no credential the fields can be filled from. |
| `disclosureExceeded` | Directory: the credential would disclose more than the member agreed to in the wallet's window; nothing was presented. |
Expand Down
11 changes: 11 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ For history before this file, see `git log` on `packages/core`.
room's profile lists them (`MESSAGE_KIND`). A room listing neither takes it
as an untitled record, which readers show as a message. Sealed on
`attributed` / `private`, in the clear on `open`; create-only.
- **Replies and reactions, written as the member.** `writeMessageAsMember`
takes `replyTo` (written as `inReplyTo`); `writeReactionAsMember` writes a
record of kind `reaction` (`REACTION_KIND`) — the value as its `body`, naming
the record it reacts to — and `withdrawReactionAsMember` takes one back with
an empty version of the member's own record, over the version read.
`ownReactions` finds the member's live reactions with a value on a record,
by the reading rules. A room whose profile does not list what a write needs
(`inReplyTo`; `kind` and `inReplyTo`) refuses it before anything is sealed or
sent: `RecordWriteRefused` with `code: "recordMembersNotListed"`
(`recordMembersRefusal`). `UnsignedRecordBody` and `putCleartextAsMember`
carry `inReplyTo`.

### Changed

Expand Down
130 changes: 127 additions & 3 deletions packages/core/src/rooms/act.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
memberNameRewriteDue,
memberNameWriteRefusal,
} from "./member-name.js";
import { reactionValue } from "./record-view.js";

import {
TYPE_URI as KEYS_ACT,
Expand Down Expand Up @@ -71,6 +72,7 @@ import {
TYPE_URI as RECORDS_PUT,
RESPONSE_TYPE_URI as RECORDS_PUT_RESPONSE,
type RoomsRecordsPutResponsePayload,
type RecordReference,
} from "../specs/rooms/records/put/0.5/payload.js";
import {
TYPE_URI as RECORDS_CURATE,
Expand Down Expand Up @@ -329,6 +331,8 @@ export interface UnsignedRecordBody {
writtenAt?: string;
/** When the author edited it (`RecordEditedAt`). Written only where the room's profile lists `editedAt`. */
editedAt?: string;
/** The record this one answers or, on a reaction, responds to (`RecordReference`). Written only where the room's profile lists `inReplyTo`. */
inReplyTo?: RecordReference;
}

/** Thrown when a record landed at a version it was not sealed for, every time. */
Expand Down Expand Up @@ -415,10 +419,11 @@ export async function putCleartextAsMember(
body?: string;
description?: string;
tags?: string[];
/** `kind`, `writtenAt` and `editedAt` (`rooms/records/put/0.5`): only where the room's profile lists them. */
/** `kind`, `writtenAt`, `editedAt` and `inReplyTo` (`rooms/records/put/0.5`): only where the room's profile lists them. */
kind?: string;
writtenAt?: string;
editedAt?: string;
inReplyTo?: RecordReference;
replaces?: number;
file?: { manifest: Record<string, unknown>; blobRef: string };
},
Expand All @@ -428,6 +433,7 @@ export async function putCleartextAsMember(
if (params.title !== undefined) cleartext.title = params.title;
if (params.writtenAt !== undefined) cleartext.writtenAt = params.writtenAt;
if (params.editedAt !== undefined) cleartext.editedAt = params.editedAt;
if (params.inReplyTo !== undefined) cleartext.inReplyTo = { ...params.inReplyTo };
if (params.description !== undefined) cleartext.description = params.description;
if (params.tags !== undefined) cleartext.tags = params.tags;
const payload: Record<string, unknown> = {
Expand Down Expand Up @@ -503,6 +509,7 @@ function wireBody(body: UnsignedRecordBody & { version: number }): Record<string
if (body.file !== undefined) out.file = body.file;
if (body.writtenAt !== undefined) out.writtenAt = body.writtenAt;
if (body.editedAt !== undefined) out.editedAt = body.editedAt;
if (body.inReplyTo !== undefined) out.inReplyTo = { ...body.inReplyTo };
return out;
}

Expand All @@ -522,12 +529,36 @@ export async function roomRecordMembersAsMember(room: PersonaRoom): Promise<stri

/** Thrown when a write is refused before anything is sent: the room's profile does not permit it, or the value is not one the schema allows. */
export class RecordWriteRefused extends Error {
constructor(message: string) {
constructor(
message: string,
/** Set when a page can act on the refusal: `recordMembersNotListed` — the room's profile does not list a member the write needs. */
readonly code?: "recordMembersNotListed",
) {
super(message);
this.name = "RecordWriteRefused";
}
}

/**
* Why a write needing `needed` record members is refused in a room whose
* profile lists `listed`, or `undefined` when it lists them all. A writer MUST
* NOT write a member the room's current profile does not list
* (`SealedRecordBody`, *When a writer may use these members*): a reply or a
* reaction is refused, never written without what makes it one.
*/
export function recordMembersRefusal(
listed: readonly string[] | undefined,
needed: readonly string[],
what: string,
): RecordWriteRefused | undefined {
const missing = needed.filter((m) => !(listed ?? []).includes(m));
if (missing.length === 0) return undefined;
return new RecordWriteRefused(
`Not written: this room does not take ${what} — its profile does not list ${missing.join(" and ")} in its record members.`,
"recordMembersNotListed",
);
}

/** A name record a member already holds in the room: the one a new name is written over. */
export interface HeldMemberName {
author: string;
Expand Down Expand Up @@ -605,14 +636,19 @@ export async function writeMemberNameAsMember(
*/
export async function writeMessageAsMember(
room: PersonaRoom,
params: { body: string; now?: () => Date },
params: { body: string; replyTo?: RecordReference; now?: () => Date },
): Promise<{ principal: string; key: string; version: number }> {
if (params.body.trim() === "") throw new RecordWriteRefused("Not written: a message needs some text.");
const listed = (await roomRecordMembersAsMember(room)) ?? [];
if (params.replyTo !== undefined) {
const refused = recordMembersRefusal(listed, ["inReplyTo"], "replies");
if (refused) throw refused;
}
const now = (params.now ?? (() => new Date()))().toISOString().replace(/\.\d{3}Z$/, "Z");
const shape = {
...(listed.includes("kind") ? { kind: MESSAGE_KIND } : {}),
...(listed.includes("writtenAt") ? { writtenAt: now } : {}),
...(params.replyTo !== undefined ? { inReplyTo: replyReference(params.replyTo) } : {}),
};
const { principal, info } = await roomInfoAsMember(room);
const key = opaqueRecordKey();
Expand All @@ -626,6 +662,94 @@ export async function writeMessageAsMember(
/** `RecordKind` for a chat message. */
export const MESSAGE_KIND = "message";

/** `RecordKind` for a reaction. */
export const REACTION_KIND = "reaction";

/** A reference as written: the key, and the version only when one was given. */
function replyReference(r: RecordReference): RecordReference {
return { key: r.key, ...(r.version !== undefined ? { version: r.version } : {}) };
}

/**
* React to a record, as the member: a new record of kind `reaction` whose
* `body` is the value and whose `inReplyTo` names the record (*Reactions*,
* `SealedRecordBody`) — the shape {@link assembleRoomRecords} counts, once per
* author, value and target.
*
* **Refused before anything is sent** unless the room's profile lists `kind`
* and `inReplyTo` (a reader that predates them would show the reaction as a
* message), and unless `value` is a reaction's value: not empty, no line break,
* at most 64 code points once normalised. `writtenAt` where the profile lists
* it. Sealed and signed by the VTA as the member on `attributed` and `private`,
* in the clear on `open`. Create-only.
*/
export async function writeReactionAsMember(
room: PersonaRoom,
params: { target: RecordReference; value: string; now?: () => Date },
): Promise<{ principal: string; key: string; version: number }> {
const value = reactionValue(params.value);
if (value.kind === "empty") throw new RecordWriteRefused("Not written: a reaction needs a value.");
if (value.kind === "invalid") throw new RecordWriteRefused(`Not written: the reaction's ${value.reason.replace(/^its /, "")}.`);
const listed = await roomRecordMembersAsMember(room);
const refused = recordMembersRefusal(listed, ["kind", "inReplyTo"], "reactions");
if (refused) throw refused;
const now = (params.now ?? (() => new Date()))().toISOString().replace(/\.\d{3}Z$/, "Z");
const shape = {
kind: REACTION_KIND,
inReplyTo: replyReference(params.target),
...(listed!.includes("writtenAt") ? { writtenAt: now } : {}),
};
const { principal, info } = await roomInfoAsMember(room);
const key = opaqueRecordKey();
const version =
info.visibility === "open"
? await putCleartextAsMember(room, { key, body: params.value, ...shape })
: await writeSealedAsMember(room, { roomId: room.roomId, key, author: principal, body: params.value, ...shape });
return { principal, key, version };
}

/** The member's own reaction record, as the reading of the room found it: the one a withdrawal is written over. */
export interface HeldReaction {
key: string;
version: number;
/** The record it reacts to: a withdrawal keeps it (*What a new version keeps*). */
target: RecordReference;
}

/**
* Withdraw the member's own reaction: a new version of the same record, still
* of kind `reaction` and naming the same record, with an empty `body` — which
* only the reaction's own author may write to withdraw it (*Withdrawing a
* reaction*). It `replaces` the version read, so a concurrent change wins and
* this writes nothing over it. `editedAt` where the profile lists it.
*/
export async function withdrawReactionAsMember(
room: PersonaRoom,
held: HeldReaction,
params: { now?: () => Date } = {},
): Promise<{ principal: string; key: string; version: number }> {
const listed = await roomRecordMembersAsMember(room);
const refused = recordMembersRefusal(listed, ["kind", "inReplyTo"], "reactions");
if (refused) throw refused;
const now = (params.now ?? (() => new Date()))().toISOString().replace(/\.\d{3}Z$/, "Z");
const shape = {
kind: REACTION_KIND,
inReplyTo: replyReference(held.target),
...(listed!.includes("editedAt") ? { editedAt: now } : {}),
};
const { principal, info } = await roomInfoAsMember(room);
const version =
info.visibility === "open"
? await putCleartextAsMember(room, { key: held.key, body: "", ...shape, replaces: held.version })
: await writeSealedAsMember(
room,
{ roomId: room.roomId, key: held.key, author: principal, body: "", ...shape },
undefined,
{ replaces: held.version },
);
return { principal, key: held.key, version };
}

/** The member's own name record, as rule 2 gives it from the records its client holds: what a rewrite near the head writes again. */
export interface OwnMemberName {
key: string;
Expand Down
34 changes: 34 additions & 0 deletions packages/core/src/rooms/record-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,37 @@ export function assembleRoomRecords(inputs: readonly RecordInput[], opts: { now:
nameRecords,
};
}

/** One of the member's own reactions, as the room's records show it now. */
export interface OwnReaction {
key: string;
version: number;
target: RecordReference;
}

/**
* The member's own reactions with `value` on the record at `target`, as the
* records read now show them: each a record of kind `reaction` whose current
* version is readable, is by `author`, names `target` and carries `value` once
* normalised (*A reaction's value*). What a withdrawal is written over — every
* one of them, since each counts until it is withdrawn. An empty (withdrawn)
* one is not among them.
*/
export function ownReactions(
inputs: readonly RecordInput[],
match: { author: string; target: string; value: string },
): OwnReaction[] {
const wanted = reactionValue(match.value);
if (wanted.kind !== "value") return [];
const out: OwnReaction[] = [];
for (const input of inputs) {
if (input.reading.state !== "read") continue;
const c = input.reading.content;
if (c.kind !== "reaction" || c.author !== match.author || c.inReplyTo?.key !== match.target) continue;
const v = reactionValue(c.body);
if (v.kind === "value" && v.normalised === wanted.normalised) {
out.push({ key: input.key, version: input.version, target: { ...c.inReplyTo } });
}
}
return out;
}
Loading