|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { test } from 'node:test' |
| 6 | +import { createRuntime } from '../index.js' |
| 7 | +import { fakeContext } from './fake-ctx.js' |
| 8 | + |
| 9 | +// #2607: deleting a Bot ends its seats in every room, the way leaving does. Before this the deletion |
| 10 | +// only filtered the legacy `room.members` array, the room's `participants` entry stayed current, and |
| 11 | +// the next membership write rebuilt `room.members` from that entry — the deleted id came back and the |
| 12 | +// following save wrote it to disk. Two rooms keep the older shape without `participants`: that path |
| 13 | +// must still drop the id from `members` and stay without a participant list. |
| 14 | + |
| 15 | +const live = room => (room.participants ?? []).filter(p => p.leftSeq === undefined).map(p => `${p.kind}:${p.id}`) |
| 16 | + |
| 17 | +const team = () => ({ |
| 18 | + version: 2, |
| 19 | + revision: 1, |
| 20 | + mainBotIds: ['chief'], |
| 21 | + members: { owner: { id: 'owner', createdAt: 1, designatedChiefId: 'chief' } }, |
| 22 | + bots: { |
| 23 | + chief: { id: 'chief', sessionId: 'chief', name: 'Chief', role: 'Lead', instructions: '', createdAt: 1, ownerId: 'owner' }, |
| 24 | + writer: { id: 'writer', sessionId: 'writer', name: 'Writer', role: 'Write', instructions: '', createdAt: 2, ownerId: 'owner' }, |
| 25 | + coder: { id: 'coder', sessionId: 'coder', name: 'Coder', role: 'Code', instructions: '', createdAt: 3, ownerId: 'owner' }, |
| 26 | + }, |
| 27 | + rooms: { |
| 28 | + // The Bot being deleted is a plain member here. |
| 29 | + desk: { |
| 30 | + id: 'desk', |
| 31 | + name: 'Desk', |
| 32 | + members: ['chief', 'writer'], |
| 33 | + admin: 'chief', |
| 34 | + named: true, |
| 35 | + log: [], |
| 36 | + seq: 0, |
| 37 | + seen: {}, |
| 38 | + participants: [ |
| 39 | + { kind: 'human', id: 'owner', role: 'admin', displayName: 'Owner', joinedSeq: 0, joinedAt: 1 }, |
| 40 | + { kind: 'bot', id: 'chief', role: 'admin', displayName: 'Chief', joinedSeq: 0, joinedAt: 1 }, |
| 41 | + { kind: 'bot', id: 'writer', role: 'member', displayName: 'Writer', joinedSeq: 0, joinedAt: 1 }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + // The deleted Bot is the room's only admin and its only Bot — `members` is empty afterwards. |
| 45 | + solo: { |
| 46 | + id: 'solo', |
| 47 | + name: 'Solo', |
| 48 | + members: ['writer'], |
| 49 | + admin: 'writer', |
| 50 | + named: true, |
| 51 | + log: [], |
| 52 | + seq: 0, |
| 53 | + seen: {}, |
| 54 | + participants: [ |
| 55 | + { kind: 'human', id: 'owner', role: 'admin', displayName: 'Owner', joinedSeq: 0, joinedAt: 1 }, |
| 56 | + { kind: 'bot', id: 'writer', role: 'admin', displayName: 'Writer', joinedSeq: 0, joinedAt: 1 }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + // No `participants` array: the shipped single-owner shape. |
| 60 | + legacy: { id: 'legacy', name: 'Legacy', members: ['chief', 'writer'], admin: 'chief', mode: 'everyone' }, |
| 61 | + }, |
| 62 | + exchanges: [], |
| 63 | + exchangeTurns: {}, |
| 64 | + questions: {}, |
| 65 | + revision: 1, |
| 66 | +}) |
| 67 | + |
| 68 | +function context() { |
| 69 | + const ctx = fakeContext() |
| 70 | + Object.assign(ctx, { |
| 71 | + sessions: { get: () => undefined, flush: async () => {} }, |
| 72 | + workspaceRegistry: { get: () => undefined, resolveByPath: async () => undefined, create: async path => ({ id: 'w1', path }), archiveSession: async () => {} }, |
| 73 | + sessionController: { create: async () => ({ sessionId: 'new1' }), resolveAgent: async id => ({ agent: { id, session: { id, header: {} } } }), selectModel: async () => {} }, |
| 74 | + sessionTitle: { rename: async () => {} }, |
| 75 | + llm: { listProviders: () => [] }, |
| 76 | + }) |
| 77 | + return ctx |
| 78 | +} |
| 79 | + |
| 80 | +async function open(home) { |
| 81 | + const ctx = context() |
| 82 | + const rt = createRuntime(ctx, { home, github: false, composio: false, diagnosticsFile: false }) |
| 83 | + await rt.load() |
| 84 | + return { ctx, rt } |
| 85 | +} |
| 86 | + |
| 87 | +async function setup(t) { |
| 88 | + const home = mkdtempSync(join(tmpdir(), 'ds-bot-delete-seat-')) |
| 89 | + writeFileSync(join(home, 'state.json'), JSON.stringify(team())) |
| 90 | + const first = await open(home) |
| 91 | + t.after(async () => { |
| 92 | + await first.ctx.dispose() |
| 93 | + rmSync(home, { recursive: true, force: true, maxRetries: 5 }) |
| 94 | + }) |
| 95 | + return { home, rt: first.rt, ctx: first.ctx } |
| 96 | +} |
| 97 | + |
| 98 | +test('deleting a Bot closes its participant seat and the next membership write does not write it back (#2607)', async (t) => { |
| 99 | + const { home, rt, ctx } = await setup(t) |
| 100 | + await rt.deleteBot('writer') |
| 101 | + assert.equal(rt.botOf('writer'), undefined, 'the Bot record is gone') |
| 102 | + const desk = rt.state.rooms.desk |
| 103 | + const seat = desk.participants.find(p => p.kind === 'bot' && p.id === 'writer') |
| 104 | + assert.ok(seat.leftSeq !== undefined, 'the deleted Bot no longer holds a current seat') |
| 105 | + assert.equal(typeof seat.leftAt, 'number', 'the seat records when it ended') |
| 106 | + assert.deepEqual(live(desk), ['human:owner', 'bot:chief'], 'the participant list no longer answers the deleted Bot') |
| 107 | + assert.deepEqual(rt.participantsOf(desk).map(p => `${p.kind}:${p.id}`), ['human:owner', 'bot:chief'], 'the drawer reads the same list') |
| 108 | + assert.deepEqual(desk.members, ['chief'], 'members is derived from the seats, not filtered beside them') |
| 109 | + |
| 110 | + // One ordinary membership write is what used to resurrect the id through `syncMembers`. |
| 111 | + await rt.addRoomParticipant('desk', { kind: 'bot', id: 'coder' }) |
| 112 | + assert.deepEqual(desk.members, ['chief', 'coder'], 'the deleted id does not come back') |
| 113 | + assert.deepEqual(live(desk), ['human:owner', 'bot:chief', 'bot:coder']) |
| 114 | + |
| 115 | + // The deletion and the write were saved, so a host that reads the same state.json must agree. |
| 116 | + await ctx.dispose() |
| 117 | + const second = await open(home) |
| 118 | + t.after(async () => { await second.ctx.dispose() }) |
| 119 | + const reread = second.rt.state.rooms.desk |
| 120 | + assert.equal(second.rt.botOf('writer'), undefined) |
| 121 | + assert.equal(live(reread).includes('bot:writer'), false, 'the saved seat stays closed') |
| 122 | + assert.deepEqual(reread.members, ['chief', 'coder'], 'the saved members list does not carry the deleted id') |
| 123 | +}) |
| 124 | + |
| 125 | +test('a deleted Bot that was the only admin, and a room left with no Bots, take the next membership write (#2607)', async (t) => { |
| 126 | + const { rt } = await setup(t) |
| 127 | + await rt.deleteBot('writer') |
| 128 | + const solo = rt.state.rooms.solo |
| 129 | + assert.deepEqual(solo.members, [], 'the room is left with no Bots') |
| 130 | + assert.equal(solo.admin, null, 'the deleted Bot is not still the admin') |
| 131 | + assert.deepEqual(live(solo), ['human:owner']) |
| 132 | + await rt.addRoomParticipant('solo', { kind: 'bot', id: 'coder' }) |
| 133 | + assert.deepEqual(solo.members, ['coder']) |
| 134 | + assert.deepEqual(live(solo), ['human:owner', 'bot:coder']) |
| 135 | + // The room without a participants array keeps the shipped shape. |
| 136 | + const legacy = rt.state.rooms.legacy |
| 137 | + assert.equal(legacy.participants, undefined, 'no participant list is invented for a legacy room') |
| 138 | + assert.deepEqual(legacy.members, ['chief']) |
| 139 | +}) |
0 commit comments