fix(desktop): a Desktop-driven room's engine reaches plugin storage

Independent review P2 (engine: 'desktop' is never written to local
storage). updateGroupChat's durable map, durableGroupChatRooms (the
post-merge rewrite) and disband's rewrite of the remaining rooms all
rebuilt each room field by field without `engine`, so hydration read a
field no writer stored and an edited all-local room fell back to the
membership check whenever the ui_meta mirror had not landed. Spread
stickyGroupEngine(room) in all three. The hydration test now seeds
storage through the real writer instead of by hand, and a new test pins
each writer.
This commit is contained in:
Teknium
2026-11-03 01:53:26 -08:00
parent 3c974102df
commit 51f600ae60
4 changed files with 111 additions and 7 deletions

View File

@@ -0,0 +1,88 @@
/**
* A Desktop-driven room's `engine: 'desktop'` reaches plugin storage through
* every writer of the stored room map: a room write, the post-merge rewrite and
* the rewrite of the rooms left after a disband. Hydration reads the field back
* (group-chat-hydrate-engine.test.ts), so a writer that drops it hands an edited
* all-local room back to the membership check after a reload.
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type * as groupChat from './group-chat'
import type * as groupChatView from './group-chat-view'
import { createGroupGateway, runTimersInline, scriptedStorage } from './group-test-utils'
import type { ScriptedGateway } from './group-test-utils'
import type { GroupChat } from './types'
const { host } = vi.hoisted(() => ({ host: {} as Record<string, unknown> }))
vi.mock('@hermes/plugin-sdk', async () => {
const { pluginSdkMock } = await import('./group-test-utils')
return pluginSdkMock(host)
})
interface Room {
chat: typeof groupChat
gateway: ScriptedGateway
view: typeof groupChatView
}
async function loadRoom(): Promise<Room> {
vi.resetModules()
const gateway = createGroupGateway()
for (const key of Object.keys(host)) {
delete host[key]
}
Object.assign(host, gateway.host)
const [chat, view, shared] = await Promise.all([import('./group-chat'), import('./group-chat-view'), import('./shared')])
shared.setPluginCtx(scriptedStorage(gateway.storage))
return { chat, gateway, view }
}
const stored = (room: Room) => (room.gateway.storage.get('group-chats') || {}) as Record<string, GroupChat>
function seed(room: Room) {
for (const [name, engine] of [['Mixed', 'desktop'], ['Plain', undefined]] as const) {
room.chat.updateGroupChat(
name,
r => ({ ...r, members: [{ name: 'alpha' }, { name: 'beta' }], roomId: `room-${name}`, ...(engine ? { engine } : {}) }),
{ sync: false }
)
}
}
beforeEach(() => {
runTimersInline()
})
describe('engine reaches plugin storage', () => {
it('through a room write and the post-merge rewrite', async () => {
const room = await loadRoom()
seed(room)
expect(stored(room).Mixed.engine).toBe('desktop')
expect(stored(room).Plain.engine).toBeUndefined()
room.gateway.storage.delete('group-chats')
await room.chat.persistGroupChatRooms()
expect(stored(room).Mixed.engine).toBe('desktop')
expect(stored(room).Plain.engine).toBeUndefined()
})
it('through the rewrite of the rooms a disband leaves', async () => {
const room = await loadRoom()
seed(room)
room.chat.updateGroupChat('Gone', r => ({ ...r, members: [] }), { sync: false })
await room.view.disbandGroupChat('Gone', [])
expect(Object.keys(stored(room)).sort()).toEqual(['Mixed', 'Plain'])
expect(stored(room).Mixed.engine).toBe('desktop')
})
})

View File

@@ -1,12 +1,14 @@
/**
* A Desktop-driven room's engine (`engine: 'desktop'`, set when the room was
* created across machines) survives a window reload: plugin hydration rebuilds
* each stored room field by field, and a dropped field would hand the room back
* to the membership check after the next membership edit.
* created across machines) survives a window reload: the room writer stores it
* and plugin hydration rebuilds each stored room field by field, so a field
* dropped on either side hands the room back to the membership check after the
* next membership edit.
*/
import { describe, expect, it, vi } from 'vitest'
import type * as GroupChatModule from './group-chat'
import type * as I18nModule from './i18n'
import type { RosterRow } from './types'
@@ -107,10 +109,19 @@ vi.mock('./i18n', async importOriginal => {
const plugin = (await import('./plugin')).default
const { $groupChats } = await import('./group-chat')
const stored = {
Mixed: { engine: 'desktop', log: [], members: [{ name: 'alpha' }], roomId: 'r1' },
Plain: { log: [], members: [{ name: 'beta' }], roomId: 'r2' }
}
// The stored map comes from the real writer, not a hand-built seed: a field the
// writer drops is one hydration never sees.
const stored = await (async () => {
const writes = new Map<string, unknown>()
const writer = await vi.importActual<typeof GroupChatModule>('./group-chat')
const { setPluginCtx } = await import('./shared')
setPluginCtx({ storage: { get: async () => null, set: async (key: string, value: unknown) => void writes.set(key, value) } } as never)
writer.updateGroupChat('Mixed', room => ({ ...room, engine: 'desktop', members: [{ name: 'alpha' }], roomId: 'r1' }), { sync: false })
writer.updateGroupChat('Plain', room => ({ ...room, members: [{ name: 'beta' }], roomId: 'r2' }), { sync: false })
return writes.get('group-chats')
})()
describe('group-chat hydration', () => {
it("keeps a stored room's Desktop engine and adds none to a room without one", async () => {

View File

@@ -90,6 +90,7 @@ import { GroupClarifyCard, GroupImageControls, GroupMentionInput } from './group
import type { GroupRoomPrompt } from './group-chat-parts'
import { GroupMemberPicker } from './group-chat-view-members'
import { compressGroupMemberHistory } from './group-compress'
import { stickyGroupEngine } from './group-engine'
import { sweepExternalGroupWrites } from './group-external-writes'
import { GroupHoldStatus } from './group-hold-status'
import {
@@ -227,6 +228,7 @@ export async function disbandGroupChat(group: string, members: RosterRow[]) {
sessionOwners: room.sessionOwners || {},
members: Array.isArray(room.members) ? room.members : [],
roomId: typeof room.roomId === 'string' && room.roomId ? room.roomId : null,
...stickyGroupEngine(room),
image: room.image || null,
syncRevision: Math.max(0, Number(room.syncRevision || 0))
}

View File

@@ -931,6 +931,7 @@ export function durableGroupChatRooms(all: Record<string, GroupChat> = $groupCha
// name-keyed identity — same field updateGroupChat's inline map
// already carries.
roomId: typeof room.roomId === 'string' && room.roomId ? room.roomId : null,
...stickyGroupEngine(room),
image: room.image || null,
rosterOrder: room.rosterOrder,
pinned: room.pinned,
@@ -1648,6 +1649,8 @@ export function updateGroupChat(
members: Array.isArray(room.members) ? room.members : [],
// Immutable room identity: the member-session title for new rooms.
roomId: typeof room.roomId === 'string' && room.roomId ? room.roomId : null,
// A Desktop-driven room stays one across reloads (plugin.tsx hydration).
...stickyGroupEngine(room),
// Room picture (small data URL, same normalization as bot avatars).
image: room.image || null,
rosterOrder: room.rosterOrder,