Skip to content

The agent that grows with you

fix(desktop): guard render-reachable route lookups against orphaned rows

Teknium40ab950ae3c2
diff --git a/apps/desktop/src/plugins/hermes-bots/plugin.js b/apps/desktop/src/plugins/hermes-bots/plugin.js
index 3c1a812cde1..756098d728e 100644
--- a/apps/desktop/src/plugins/hermes-bots/plugin.js
+++ b/apps/desktop/src/plugins/hermes-bots/plugin.js
@@ -5155,7 +5155,9 @@ function botConnectionRoute(bot) {
const BOTS_HOME_OWNER_KEY = 'bots:home'
function botWorkspaceOwnerKey(bot) {
- const route = botConnectionRoute(bot)
+ // Render-reachable (sidebar sync, Bots home open, context menus): an
+ // orphaned row must yield a stable degraded key, never the dispatch throw.
+ const route = resolveBotConnectionRoute(bot).route
return `bot:${route ? botRouteKey(route) : String(bot?.name || 'default')}`
}
@@ -5165,7 +5167,9 @@ function groupWorkspaceOwnerKey(group) {
}
function setBotsWorkspaceOwner(ownerKey, bot = null, blockedMessage = 'Select a Bot or group first.') {
- const route = bot ? botConnectionRoute(bot) : null
+ // Render-reachable (sidebar listener fires on visibility flips). An
+ // orphaned row degrades to the blocked target instead of throwing.
+ const route = bot ? resolveBotConnectionRoute(bot).route : null
const target = route ? { kind: 'route', route } : { kind: 'blocked', message: blockedMessage }
host.setWorkspaceScope?.('bots', ownerKey || BOTS_HOME_OWNER_KEY, target)
@@ -6234,7 +6238,11 @@ function groupChatMemberBots(group, roster, metaByName) {
* here is what keeps the same room intact across machines. */
function durableGroupChatMembers(bots) {
return (bots || []).map(bot => {
- const route = botConnectionRoute(bot)
+ // Persistence pass over the whole seated roster: an orphaned member
+ // (connection deleted) keeps its identity and simply persists with no
+ // route — the same degraded shape the hydrate annotate produces. The
+ // strict throw here would lose the entire room update over one row.
+ const route = resolveBotConnectionRoute(bot).route
// Keep the friendly identity on the stored descriptor: after a
// connection switch the live roster row may be gone, and renamed-tag
// mentions must still resolve against the persisted member.
@@ -6249,6 +6257,9 @@ function durableGroupChatMembers(bots) {
connectionKind: bot.connectionKind,
connectionLabel: bot.connectionLabel,
...(route ? { route, targetProfile: route.targetProfile } : {}),
+ // A swept/annotated member keeps its degraded mark across the rebuild —
+ // otherwise the next room send would silently un-mark an orphaned row.
+ ...(bot.sourceMissing ? { sourceMissing: true, sourceReachable: false } : {}),
remoteSource: true,
sourceScoped: Boolean(route)
}
@@ -8388,7 +8399,11 @@ function BotRow({ bot, onDelete, onEdit, onGroup, showHandle }) {
// ── model picker (provider/model dropdowns via model.options) ───────────────
function useModelOptions(bot = null) {
- const route = botConnectionRoute(bot)
+ // Hook body runs during render: an orphaned row must paint the picker
+ // disabled/erroring, not throw into the pane's error boundary.
+ const resolved = bot ? resolveBotConnectionRoute(bot) : null
+ const route = resolved?.status === 'resolved' ? resolved.route : null
+ const orphaned = resolved?.status === 'owner_removed'
return useQuery({
queryKey: [ID, 'model-options', route ? botRouteKey(route) : 'active'],
@@ -8397,6 +8412,7 @@ function useModelOptions(bot = null) {
explicit_only: false,
refresh: true
}),
+ enabled: !orphaned,
staleTime: 120000,
retry: false
})
@@ -8600,7 +8616,9 @@ function AdvancedProfileConfig({ bot, state, setState }) {
const [loaded, setLoaded] = useState(false)
const [unsupported, setUnsupported] = useState(false)
const [skillFilter, setSkillFilter] = useState('')
- const botRoute = botConnectionRoute(bot)
+ // Component body = render path: degrade an orphaned row to the bot's own
+ // name scope instead of throwing into the dialog's error boundary.
+ const botRoute = resolveBotConnectionRoute(bot).route
const backendProfile = botRoute?.targetProfile || botRoute?.profile || bot.name
const backendScope = botBackendProfileScope(botRoute, bot.name)
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/embed-real-capabilities.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/embed-real-capabilities.test.mjs
index 8faf9e677e6..a72cbdc5d0f 100644
--- a/apps/desktop/src/plugins/hermes-bots/tests/embed-real-capabilities.test.mjs
+++ b/apps/desktop/src/plugins/hermes-bots/tests/embed-real-capabilities.test.mjs
@@ -27,7 +27,9 @@ test('AdvancedProfileConfig embeds the real McpTab with a live gateway + profile
})
test('AdvancedProfileConfig scopes SkillsView to the connection and backend target profile', () => {
- assert.match(source, /const botRoute = botConnectionRoute\(bot\)/)
+ // Route lookup is the NON-throwing resolver (#93492): the dialog renders
+ // degraded for an orphaned row instead of crashing the pane.
+ assert.match(source, /const botRoute = resolveBotConnectionRoute\(bot\)\.route/)
assert.match(source, /fixedProfile: backendProfile/)
assert.match(source, /fixedConnection: botRoute\.connectionId/)
})
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/orphaned-connection-members.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/orphaned-connection-members.test.mjs
new file mode 100644
index 00000000000..e5ccbcd2879
--- /dev/null
+++ b/apps/desktop/src/plugins/hermes-bots/tests/orphaned-connection-members.test.mjs
@@ -0,0 +1,182 @@
+import assert from 'node:assert/strict'
+import { readFileSync } from 'node:fs'
+import test from 'node:test'
+import vm from 'node:vm'
+
+const source = readFileSync(new URL('../plugin.js', import.meta.url), 'utf8')
+
+// #93492 root fix: deleting a connection sweeps/annotates the persisted
+// group-chat member rows that referenced it (registry 'removed' push), and a
+// hydrate-time pass annotates rows orphaned before the sweep existed. Rows
+// are marked (sourceMissing → the existing 'Gateway removed' degraded state),
+// never silently deleted.
+
+function runtime() {
+ const atom = initial => {
+ let value = initial
+ return { get: () => value, set: next => { value = next }, listen: () => () => undefined }
+ }
+ const jsx = (type, props = {}) => ({ type, props })
+ const storageWrites = []
+ const context = {
+ atom,
+ jsx,
+ jsxs: jsx,
+ useQuery: () => ({}),
+ useValue: value => (value?.get ? value.get() : value),
+ useState: value => [value, () => undefined],
+ document: { getElementById: () => null, createElement: () => ({}), head: { appendChild: () => undefined } },
+ host: {
+ state: {
+ connectionId: { get: () => 'local', listen: () => undefined },
+ profile: { get: () => 'ops', listen: () => undefined }
+ },
+ request: () => undefined
+ },
+ sdk: new Proxy({}, { get: () => undefined })
+ }
+ const code = source
+ .replace(/^import \* as sdk from '@hermes\/plugin-sdk'\r?\n/m, '')
+ .replace(/^import\s+\{[\s\S]*?\}\s+from '@hermes\/plugin-sdk'\r?\n/m, '')
+ .replace(/^import .* from 'react'\r?\n/m, '')
+ .replace(/^import .* from 'react\/jsx-runtime'\r?\n/m, '')
+ .replace('export default {', 'globalThis.plugin = {')
+ .concat(
+ '\nglobalThis.__orphan = { $groupChats, sweepGroupChatMembersForRemovedConnection, annotateOrphanedGroupChatMembers, markOrphanedGroupMemberDescriptor, groupMemberReferencesConnection, botSourceStatus, durableGroupChatMembers, botWorkspaceOwnerKey, setBotsWorkspaceOwner, useModelOptions, pluginCtxRef: () => pluginCtx, setPluginCtx: value => { pluginCtx = value } };'
+ )
+ vm.runInNewContext(code, context)
+ context.__storageWrites = storageWrites
+ return context
+}
+
+function member(overrides = {}) {
+ return {
+ name: 'dixie',
+ handle: 'dixie',
+ connectionId: 'conn-gone',
+ remoteSource: true,
+ sourceScoped: true,
+ route: { connectionId: 'conn-gone', mode: 'remote', profile: 'dixie', targetProfile: 'dixie' },
+ ...overrides
+ }
+}
+
+test('removed-connection sweep annotates matching members and persists; other rows untouched', () => {
+ const { __orphan: o } = runtime()
+ const writes = []
+ o.setPluginCtx({ storage: { set: (key, value) => { writes.push([key, value]); return Promise.resolve() } } })
+
+ o.$groupChats.set({
+ Crew: {
+ log: [{ id: '1', at: 1 }],
+ watermarks: {},
+ members: [member(), member({ name: 'bob', connectionId: 'conn-live', route: { connectionId: 'conn-live', mode: 'remote', profile: 'bob', targetProfile: 'bob' } })]
+ }
+ })
+
+ assert.equal(o.sweepGroupChatMembersForRemovedConnection('conn-gone'), true)
+
+ const room = o.$groupChats.get().Crew
+ const swept = room.members.find(m => m.name === 'dixie')
+ const kept = room.members.find(m => m.name === 'bob')
+
+ // Annotated, not deleted: identity survives, marked degraded.
+ assert.equal(room.members.length, 2)
+ assert.equal(swept.sourceMissing, true)
+ assert.equal(swept.sourceReachable, false)
+ assert.equal(kept.sourceMissing, undefined)
+ // The degraded mark renders as the existing 'Gateway removed' state.
+ assert.equal(o.botSourceStatus(swept).label, 'Gateway removed')
+ // Persisted so the fix survives restarts (the poisoned row lived in storage).
+ assert.ok(writes.some(([key]) => key === 'group-chats'))
+})
+
+test('removed-connection sweep is idempotent and ignores blank ids', () => {
+ const { __orphan: o } = runtime()
+ o.setPluginCtx({ storage: { set: () => Promise.resolve() } })
+ o.$groupChats.set({ Crew: { log: [], watermarks: {}, members: [member()] } })
+
+ assert.equal(o.sweepGroupChatMembersForRemovedConnection(''), false)
+ assert.equal(o.sweepGroupChatMembersForRemovedConnection('conn-gone'), true)
+ // Already annotated: nothing left to change.
+ assert.equal(o.sweepGroupChatMembersForRemovedConnection('conn-gone'), false)
+})
+
+test('hydrate annotate: lost-connectionId rows are marked even without a registry', () => {
+ const { __orphan: o } = runtime()
+ const rooms = {
+ Crew: {
+ log: [],
+ watermarks: {},
+ // The exact persisted shape from #93492: remoteSource kept, connectionId gone.
+ members: [{ name: 'halakukhan', handle: 'halakukhan', connectionId: null, remoteSource: true }]
+ }
+ }
+
+ const { rooms: next, changed } = o.annotateOrphanedGroupChatMembers(rooms, null)
+
+ assert.equal(changed, true)
+ assert.equal(next.Crew.members[0].sourceMissing, true)
+ assert.equal(o.botSourceStatus(next.Crew.members[0]).label, 'Gateway removed')
+})
+
+test('hydrate annotate: with a live registry, members on dead connections are marked, live and local kept', () => {
+ const { __orphan: o } = runtime()
+ const rooms = {
+ Crew: {
+ log: [],
+ watermarks: {},
+ members: [
+ member(),
+ member({ name: 'bob', connectionId: 'conn-live', route: { connectionId: 'conn-live', mode: 'remote', profile: 'bob', targetProfile: 'bob' } }),
+ { name: 'local-pal' }
+ ]
+ }
+ }
+
+ const { rooms: next, changed } = o.annotateOrphanedGroupChatMembers(rooms, new Set(['conn-live']))
+
+ assert.equal(changed, true)
+ assert.equal(next.Crew.members.find(m => m.name === 'dixie').sourceMissing, true)
+ assert.equal(next.Crew.members.find(m => m.name === 'bob').sourceMissing, undefined)
+ assert.equal(next.Crew.members.find(m => m.name === 'local-pal').sourceMissing, undefined)
+})
+
+test('hydrate annotate: no registry means only the unresolvable-route shape is touched', () => {
+ const { __orphan: o } = runtime()
+ const rooms = { Crew: { log: [], watermarks: {}, members: [member()] } }
+
+ // conn-gone still has an id; without a registry we cannot prove it dead.
+ const { changed } = o.annotateOrphanedGroupChatMembers(rooms, null)
+
+ assert.equal(changed, false)
+})
+
+test('render-reachable route callers degrade on an orphaned row instead of throwing', () => {
+ const { __orphan: o } = runtime()
+ const orphaned = { name: 'halakukhan', handle: 'halakukhan', connectionId: null, remoteSource: true }
+
+ // botWorkspaceOwnerKey (sidebar sync / Bots home / context menus)
+ assert.doesNotThrow(() => o.botWorkspaceOwnerKey(orphaned))
+ assert.equal(o.botWorkspaceOwnerKey(orphaned), 'bot:halakukhan')
+ // setBotsWorkspaceOwner (sidebar visibility listener)
+ assert.doesNotThrow(() => o.setBotsWorkspaceOwner('bot:halakukhan', orphaned))
+ // durableGroupChatMembers (every group send / roster refresh)
+ assert.doesNotThrow(() => o.durableGroupChatMembers([orphaned]))
+ const durable = o.durableGroupChatMembers([{ ...orphaned, sourceMissing: true, sourceReachable: false }])[0]
+ // The degraded mark survives the durable rebuild.
+ assert.equal(durable.sourceMissing, true)
+ // useModelOptions (hook body — runs during pane render)
+ assert.doesNotThrow(() => o.useModelOptions(orphaned))
+})
+
+test('source contract: the sweep is wired to the registry removed push and unbound on dispose', () => {
+ assert.match(source, /connections\?\.onChanged\?\.\(/)
+ assert.match(source, /payload\?\.reason === 'removed'/)
+ assert.match(source, /sweepGroupChatMembersForRemovedConnection\(payload\.connectionId\)/)
+ assert.match(source, /unbindConnectionsChanged\(\)/)
+})
+
+test('source contract: hydrate runs the orphan annotate over persisted rooms', () => {
+ assert.match(source, /annotateOrphanedGroupChatMembers\(\$groupChats\.get\(\), liveIds\)/)
+})