Skip to content

Commit 137cd2f

Browse files
authored
fix(rooms): name the pair a room link seated (#927)
Owner: Dev3 Reviewer: Reviewer2 Written-by: Dev3 Fixes #923 A seat a link admitted was only a pair when the seat record kept `admittedBy: 'ticket'` **and** `hostBotId`. The local link-accept path (`answerLocalInvite` -> `addRoomParticipant`) wrote neither, and a seat older than those fields has no way back — so `ticketPeers()` (`core/src/host/reach.js`, the list Bot Social's room reach is derived from) skipped the seat, the room's own envelope fell to the plain pair path, named no room (`plugins/social/src/social.js:1622`), and Bot Social held it as `pending_approval`. Approving that card then ends in `no active relationship between these bots`, which is exactly what the owner's screenshot shows (`Group update for Chief`, `Bot Social decide_approval: no active relationship between these bots`). The card is **outbound**: `shownRoomShareText` (`plugins/social/client.js:1211-1221`) renders every op other than `join`/`leave`/`line` as `Group update for <to>`, and the `Approve` / `Auto-send from now on` pair is the `#417` "my Bot -> that person" hold. It proves this host cannot reach that guest; it is not evidence about the reverse direction (see "Open, not a gate"). ### Reproduce 1. Host room `war`, invite minted from the room (`mintRoomInvite`), a Bot of another home accepts (`op: 'ask', accept: true`). 2. `core/test/remote-room.test.js` (new test): the seat record has no `admittedBy` / `ticket` / `hostBotId`, and `ticketPeers(state)` has no pair for it. 3. A room-share envelope from a local Bot to that seat therefore reaches `sendOut` with `ticketPair()` undefined; `roomId` is `undefined` for `via: 'room-share'`, so `send_as_member` carries no room, Bot Social answers `pending_approval`, and `decide_approval` ends in `no active relationship` (bot-social `deliverMessage` -> `activeRelationship`). ### What changed - `core/src/host/reach.js`: `linkSeats(room)` reads the link's own `homes` (home -> the handles it seated, written on the accept), and a remote seat is a pair when it kept the ticket **or** a link of this room names it; the pair's `from` is the seat's `hostBotId` when it is a current member, else the room's lead (`room.admin`). A friend's Bot added by hand is named by no link and stays out. - `core/src/host/room-membership.js`: `addRoomParticipant` takes `opts.ticket` / `opts.hostBotId` and records `admittedBy: 'ticket'` / `ticket` / `hostBotId` on the seat; `answerLocalInvite` passes the link's ticket and the Bot the link was minted for (or the room's lead). New seats are right from the start; older rooms are covered by the reach-side record, so nothing is rewritten on load. - `plugins/social/src/social.js`: comment only. The old sentence said a local room "has no id on the server" — stale since #59: `mintRoomInvite` passes `roomId` to the server (`room-membership.js:1279`) and `sendTicketShare` names it. What the ordinary path lacks is the pair, not the id. - `docs/subsystems/core-host.md`: one sentence for #923 in the room-share section. - `changelog.d/923-room-link-seat-pair.fixed.md`. No change to the server, no widening of `activeRelationship`, and no new rejection: `not-inviter`, `unknown-room` and the `#909`/`#910` trust boundary are untouched. ### What I ran - `core`: full suite `node --import ./test/harness-resolve.mjs --test --test-concurrency=1 test/*.test.js` -> **1344/1344** - room suites (`remote-room` + `room-ticket-seat`) -> **75/75** - `plugins/social`: `node --import ./test/resolve.mjs --test "test/*.test.js"` -> **352/352** - `node scripts/ds-bot-patches.mjs generate` + `check` -> 10 patches apply cleanly; `core/` equals `ds-bot/` + patches; the three mirrors are byte-identical to their core files - `cd core && node scripts/lint.mjs` -> oxlint **0 errors** (44 pre-existing warnings), `arch-docs: OK` - `node .github/scripts/changelog.mjs check` -> 45 fragments well formed ### Red without the source change With only `core/src/host/reach.js` and `core/src/host/room-membership.js` reverted to pristine `0b22aa53` (the new test and the rest kept): `not ok 55 - #923: a link-admitted seat is the pair the link opened...`, **64 pass / 1 fail**, failing at `seat.admittedBy === 'ticket'` (`undefined`). With the change: that file **65/65**. The social assertion added to the `#883` test is a **guardrail**, not a red case: it pins that an approved (`approved: true`) room-share envelope still goes out by the pair and names the room, which is the "settle" half once the pair is enumerable (`sendOut` checks `ticketPair` before every other branch). ### Not run - Smoke test: this session has no DSH checkout for `dev/bench.sh`, so it was not run; the unit, plugin and arch-docs suites are the coverage here. - No live two-home run: the reproduction is in-process (`remote-room.test.js`), which is how `#852`/`#884` are covered too. ### Open, not a gate Whether the reverse direction (guest -> host) fails for the same reason is **not** decided by this PR: it needs the other machine's screen (`Not sent: ... you are not friends with this Bot` would mean the same server-side fallback at send time; `已发送` would mean delivery). The server half is `Birfy/bot-social#67`; this PR only makes the pair enumerable and leaves every existing rejection in place. ### Risk - Reach is widened only for seats a link of that room admitted; the negative control (a remote Bot added by hand) is asserted in the new test, and the `#884`/`#883` tests are unchanged and green. - A legacy room whose link record no longer names the seat (for example an invite pruned from `localInvites`) stays non-enumerable and keeps today's behaviour: the card, and the server refusal. - `hostBotId` falls back to the room's lead when the seat did not keep it; that is the Bot the link was minted for (`inviteBotOf`), and it must still be a current member. Docs-not-needed: the `plugins/social` change is one comment plus one assertion on the existing pair path; no plugin behaviour, route or setting changed, so `docs/subsystems/social.md` would only restate what the `#883` test already pins. The room-share pair itself is documented in `docs/subsystems/core-host.md` in this branch. Self-check: `git diff <base>...HEAD -- plugins/social` is 3 comment lines and 5 test lines.
1 parent 0b22aa5 commit 137cd2f

10 files changed

Lines changed: 170 additions & 14 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A group that a friend's Bot joined through an invite link now reaches that guest instead of asking Bot Social about a friendship the two homes never had: the seat the link admitted is the one pair the link opened, read from the seat itself or from the link's own record of who it admitted, so a group message or update goes out under the room rather than sitting on an approval that could not be approved. A Bot added to the group by hand is not a link pair and is unchanged ([#923](https://github.com/ThinkFlowLab/FlyBot/issues/923)).

‎core/src/host/reach.js‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,22 @@ function remoteInRoom(state, roomId, peer) {
204204
})
205205
}
206206

207+
// The seats each link of this room admitted: the invite's own `homes` (home → the handles it seated)
208+
// is written on the accept and survives even when the seat record itself did not keep the ticket.
209+
function linkSeats(room) {
210+
const out = new Map()
211+
const invites = [room?.localInvite, ...(Array.isArray(room?.localInvites) ? room.localInvites : [])]
212+
for (const invite of invites) {
213+
if (!invite || typeof invite.ticket !== 'string' || !invite.ticket) continue
214+
for (const handles of Object.values(invite.homes ?? {})) {
215+
for (const handle of Array.isArray(handles) ? handles : []) {
216+
if (typeof handle === 'string' && handle && !out.has(handle)) out.set(handle, invite)
217+
}
218+
}
219+
}
220+
return out
221+
}
222+
207223
// #883, #884: the only pairs a room link opens, as { from: local Bot id, to: remote handle, scope:
208224
// the room id Bot Social knows the room by, ticket? }. A seat in a room of this home is reached from
209225
// the Bot its link named; a room of another home that a ticket seated Bots of mine in is reached from
@@ -222,10 +238,21 @@ export function ticketPeers(state) {
222238
}
223239
continue
224240
}
241+
const linkOf = linkSeats(room)
225242
for (const p of live) {
226-
if (p.kind !== 'bot' || p.remote !== true || p.admittedBy !== 'ticket' || !p.hostBotId) continue
227-
if (!(room.members ?? []).includes(p.hostBotId)) continue
228-
peers.push({ from: p.hostBotId, to: handleOf(p), scope: String(room.id), ticket: p.ticket })
243+
if (p.kind !== 'bot' || p.remote !== true) continue
244+
// #923: a seat is a pair when a link of this room admitted it, whether the seat itself kept the
245+
// ticket (`admittedBy: 'ticket'`) or the link's own record of who it admitted is the only trace
246+
// — a room opened before the seat carried it must reach that seat the same way, or its group
247+
// envelopes fall back to a friendship the two homes never had. A friend's Bot added by hand is
248+
// named by no link and stays out.
249+
const ticket = p.admittedBy === 'ticket' ? p.ticket : linkOf.get(handleOf(p))?.ticket
250+
if (!ticket) continue
251+
// The seat names the Bot of this home whose handle it knocked on; a seat written before that
252+
// was recorded is reached from the room's own lead, which is the Bot its link was minted for.
253+
const host = p.hostBotId && (room.members ?? []).includes(p.hostBotId) ? p.hostBotId : room.admin
254+
if (!host || !(room.members ?? []).includes(host)) continue
255+
peers.push({ from: host, to: handleOf(p), scope: String(room.id), ticket })
229256
}
230257
}
231258
for (const [ticket, claim] of Object.entries(state.roomClaims ?? {})) {

‎core/src/host/room-membership.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,11 @@ export function install(rt) {
614614
}
615615
const kind = draft.kind
616616
const viaCode = opts?.viaCode === true && draft.kind === 'bot' && !isRemoteDraft(draft)
617+
// #923: a seat one of this room's links admitted names the pair reach.js enumerates. The link's
618+
// ticket, and the Bot of this home whose handle the guest knocked on, are what make that pair
619+
// visible; without them the seat's envelopes fall back to a friendship the two homes never had.
620+
const seatTicket = typeof opts?.ticket === 'string' && opts.ticket ? opts.ticket : undefined
621+
const seatHostBotId = seatTicket && typeof opts?.hostBotId === 'string' && opts.hostBotId ? opts.hostBotId : undefined
617622
const published = assertAddable(room, draft, actor, { viaCode })
618623
const role = draft.role === 'admin' ? 'admin' : 'member'
619624
const invitedBy = actor?.memberId
@@ -646,6 +651,8 @@ export function install(rt) {
646651
invitedAt,
647652
joinedSeq: line.seq,
648653
joinedAt: invitedAt,
654+
...(seatTicket ? { admittedBy: 'ticket', ticket: seatTicket } : {}),
655+
...(seatHostBotId ? { hostBotId: seatHostBotId } : {}),
649656
}
650657
room.participants.push(record)
651658
append(room, {
@@ -1546,7 +1553,9 @@ export function install(rt) {
15461553
void rt.notifyRoomShare?.(room, { op: 'join', handle: asker })
15471554
} else {
15481555
const ownerId = actor?.memberId ?? Object.keys(state.members || {})[0]
1549-
await rt.addRoomParticipant(room.id, { kind: 'bot', handle: asker }, actor ?? (ownerId ? { memberId: ownerId } : undefined))
1556+
// #923: the seat a link admitted keeps the link's ticket and the Bot whose handle it knocked
1557+
// on, so `reach.js` can enumerate the one pair this link opened.
1558+
await rt.addRoomParticipant(room.id, { kind: 'bot', handle: asker }, actor ?? (ownerId ? { memberId: ownerId } : undefined), { ticket: invite.ticket, hostBotId: invite.botId ?? room.admin })
15501559
}
15511560
} catch (error) {
15521561
// Nothing was added, so the record must not say it seated anyone.

‎core/test/remote-room.test.js‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
roomVisibleTo,
2020
viewerRole,
2121
} from '../src/host/room-membership.js'
22-
import { install as installReach, reach } from '../src/host/reach.js'
22+
import { install as installReach, reach, ticketPeers } from '../src/host/reach.js'
2323
import { friendBotOwner, friendBotTitle, remoteBotTitle, sharedRoomTitle } from '../src/shared/remote-label.js'
2424
import { encodeRoomShare, parseRoomInviteLink, parseRoomShare, roomInviteUrl } from '../src/shared/room-share.js'
2525
import { fenceUntrusted } from '../src/host/delivery.js'
@@ -1477,6 +1477,45 @@ test('#852: the knock asks the room record, and only the accept admits one home'
14771477
assert.equal(seats().length, 1, 'and seated nobody')
14781478
})
14791479

1480+
// #923: a room link seats a Bot of another home, and that seat is the one pair the link opened: the
1481+
// room's own envelopes to it are the room's consent, not a friendship the two homes never had. A seat
1482+
// recorded before it kept the ticket (or by a path that did not) is the same pair, read from the link's
1483+
// own record of who it admitted; a friend's Bot added by hand is not.
1484+
test('#923: a link-admitted seat is the pair the link opened, and a hand-added friend is not', async (t) => {
1485+
const { rt } = host(t, TEAM)
1486+
await rt.load()
1487+
const { alice, bob } = await signAliceAndBob(rt)
1488+
await rt.authenticate(alice.sessionToken)
1489+
publish(rt)
1490+
await grantToFriend(rt, 'chief-a')
1491+
await grantToFriend(rt, 'chief-b')
1492+
rt.state.bots['chief-a'].fields = { social: { member: { home: '@alice.home', handle: '@chief.a.home' } } }
1493+
const invite = await rt.mintRoomInvite('war')
1494+
const knock = (payload, fromHandle = '@coder.bob.home', fromHome = '@bob.home') => rt.applyInboundRoomShare({
1495+
text: encodeRoomShare(payload), fromHandle, fromHome, fromName: 'Coder',
1496+
})
1497+
const accepted = await knock({ op: 'ask', ticket: invite.ticket, accept: true })
1498+
assert.equal(accepted.accepted, true)
1499+
const seat = rt.state.rooms.war.participants.find(p => p.remote === true && p.handle === '@coder.bob.home')
1500+
assert.equal(seat.admittedBy, 'ticket', 'the accept keeps the link that seated it')
1501+
assert.equal(seat.ticket, invite.ticket)
1502+
assert.equal(seat.hostBotId, 'chief-a', 'reached from the Bot whose handle it knocked on')
1503+
const pairs = () => ticketPeers(rt.state).filter(p => p.to === '@coder.bob.home')
1504+
assert.deepEqual(pairs(), [{ from: 'chief-a', to: '@coder.bob.home', scope: 'war', ticket: invite.ticket }], 'one pair, the room Bot Social can check')
1505+
1506+
// A room opened before the seat kept the ticket: the link's own `homes` still names it, so the pair
1507+
// is the same one, and a group envelope to it is the room's, not a friend's.
1508+
delete seat.admittedBy
1509+
delete seat.ticket
1510+
delete seat.hostBotId
1511+
assert.deepEqual(pairs().map(p => [p.from, p.ticket]), [['chief-a', invite.ticket]], 'the link record alone still opens the pair')
1512+
1513+
// A friend's Bot added by hand is nobody's link seat, whatever else is true about it.
1514+
rt.state.rooms.war.participants.push({ kind: 'bot', remote: true, id: '@friend.eve.home', handle: '@friend.eve.home', joinedSeq: 999, joinedAt: 2 })
1515+
assert.equal(ticketPeers(rt.state).some(p => p.to === '@friend.eve.home'), false, 'a hand-added friend is not a room pair')
1516+
void bob
1517+
})
1518+
14801519

14811520
// #909: an inbound envelope may say who invited a room for display, but it never decides which room it
14821521
// may touch, which room gets created, or who wrote a line. The transport names the Bot that delivered it,

0 commit comments

Comments
 (0)