fix(desktop): read Bot Mode avatars over the active socket, not one dial per bot

useRoster repaints every 5s and hands pullServerAvatars the active-source
rows. Since the multi-source merge (ed20a6f01a) every such row is
sourceScoped, so the avatar sync branch chose requestForBot and dialed each
bot's OWN backend to read a profile-directory PNG: a fresh WebSocket with
one JSON-RPC message, torn down at refcount 0, per bot per tick (#99336's
"ws accepted / ws closed messages=1" every ~5.2s on background profiles),
and for every bot with no running backend a pool spawn that waits out
POOL_SLOT_WAIT_MS (30s) and is re-queued by the next paint, forever, once
the pool is full (#102913's per-bot "waiting for a free local slot ...
timed out" cadence). The loop was self-sustaining because the plugin's own
160px face raster is deliberately not parked in $botMeta, so the empty
image slot re-fetched it on every tick.

Assets are files under the profile directory; the gateway that just
answered profiles.list reads them for any of its profiles. Route the three
avatar RPCs through host.request like the roster query itself, and remember
face-only answers so a row is fetched once, not once per tick.

Not changed: relay.ts (its loops dedupe to one route per registered
connection and return early below two connections, so a single-connection
desktop never issues a relay RPC), and useRoster's own profiles.list, which
already rides the active socket via requestForBot({name}).
This commit is contained in:
Teknium
2026-09-11 03:34:05 -07:00
parent e4080b381b
commit 52fb4e0877
2 changed files with 41 additions and 5 deletions
@@ -15,8 +15,9 @@ import { $botMeta } from './data'
import { duplicateBot } from './profile-ops'
import type { RosterRow } from './types'
const { ensureBotMetadataMock, hostMock, storageMock } = vi.hoisted(() => ({
const { ensureBotMetadataMock, faceOnlyMock, hostMock, storageMock } = vi.hoisted(() => ({
ensureBotMetadataMock: vi.fn(),
faceOnlyMock: vi.fn((_data: string) => false),
hostMock: {
request: vi.fn(),
requestProfile: vi.fn(),
@@ -39,13 +40,14 @@ vi.mock('@hermes/plugin-sdk', async () => {
})
vi.mock('./shared', () => ({ getPluginCtx: () => ({ storage: storageMock }), ID: 'hermes-bots' }))
vi.mock('./avatar-image', () => ({ isBackfilledFacePng: () => false }))
vi.mock('./avatar-image', () => ({ isBackfilledFacePng: (data: string) => faceOnlyMock(data) }))
vi.mock('./canonical-chat', () => ({ ensureBotMetadata: ensureBotMetadataMock }))
const calls: Array<{ method: string; params: Record<string, unknown> }> = []
beforeEach(() => {
vi.clearAllMocks()
faceOnlyMock.mockReturnValue(false)
calls.length = 0
$botMeta.set({})
storageMock.set.mockResolvedValue(undefined)
@@ -209,4 +211,30 @@ describe('roster avatar sync (#102978)', () => {
expect(hostMock.requestProfile).not.toHaveBeenCalled()
expect(hostMock.request).toHaveBeenCalledWith('profiles.get_asset', { asset: 'avatar', name: 'default' })
})
it('does not re-fetch a face-only raster on the next roster tick (#99336)', async () => {
const { pullServerAvatars } = await import('./profile-ops')
faceOnlyMock.mockReturnValue(true)
hostMock.request.mockResolvedValue({ data: 'data:image/png;base64,AAAA', found: true })
const row = {
connectionId: 'local',
has_avatar: true,
name: 'secretary',
route: { connectionId: 'local', mode: 'local', profile: 'secretary', targetProfile: 'secretary' },
sourceScoped: true
} as RosterRow
pullServerAvatars([row])
await vi.waitFor(() => expect(hostMock.request).toHaveBeenCalledTimes(1))
// Let the first fetch settle (its in-flight guard clears in a finally) so
// the second tick is decided by the memo, not by that guard.
await new Promise(resolve => setTimeout(resolve, 0))
pullServerAvatars([row])
await new Promise(resolve => setTimeout(resolve, 0))
expect(hostMock.request).toHaveBeenCalledTimes(1)
// The raster is a notice-only copy of the live face, never parked on the roster.
expect($botMeta.get()['local::secretary']?.image).toBeUndefined()
})
})
@@ -39,8 +39,11 @@ import type { RosterRow } from './types'
const avatarFetchInflight = new Set<string>()
const avatarPushInflight = new Set<string>()
// Rows whose server avatar is only the plugin's own face raster: nothing to
// paint, so the roster must not re-fetch it on every tick.
const avatarFaceOnly = new Set<string>()
/** Asset RPC for a row of the ACTIVE source's roster (#102978). These rows
/** Asset RPC for a row of the ACTIVE source's roster (#102978, #99336, #102913). These rows
* came back from the active gateway's own `profiles.list`, which reads every
* local profile's directory — `profiles.get_asset` / `set_asset` are the
* same directory reads, so the active gateway answers them with the row's
@@ -167,7 +170,7 @@ export function pullServerAvatars(roster: RosterRow[]) {
for (const bot of roster) {
const key = botMetaKey(bot)
if (!bot.has_avatar || avatarFetchInflight.has(key)) {
if (!bot.has_avatar || avatarFetchInflight.has(key) || avatarFaceOnly.has(key)) {
continue
}
@@ -189,8 +192,12 @@ export function pullServerAvatars(roster: RosterRow[]) {
const mine = current[key] || {}
// A 160px raster of the vector face is only for inter-agent
// notices. Do not park it on the roster or the live face dies.
// notices. Do not park it on the roster or the live face dies —
// and remember the answer, or the empty image slot re-fetches the
// same raster on every roster tick.
if (isBackfilledFacePng(res.data) && mine.imageKind !== 'photo' && !mine.pet) {
avatarFaceOnly.add(key)
return
}
@@ -429,6 +436,7 @@ export async function deleteBot(bot: RosterRow) {
forgetSessionUnread([bot.canonical_session?.id, bot.canonical_session?.resolved_id], bot.name)
rosterWatermarks.delete(botSelectionKey(bot))
avatarFetchInflight.delete(botMetaKey(bot))
avatarFaceOnly.delete(botMetaKey(bot))
avatarPushInflight.delete(botMetaKey(bot))
if ($selectedBot.get() === botSelectionKey(bot)) {