Skip to content

The agent that grows with you

fix(desktop): read the live union roster in mention-completions + mention middleware

ArthurFranckPat770f9a574763
diff --git a/apps/desktop/src/plugins/hermes-bots/plugin.js b/apps/desktop/src/plugins/hermes-bots/plugin.js
index d2315d471e0..9cbcf975ec3 100644
--- a/apps/desktop/src/plugins/hermes-bots/plugin.js
+++ b/apps/desktop/src/plugins/hermes-bots/plugin.js
@@ -2832,6 +2832,52 @@ function useRoster() {
})
}
+/** Synchronous union-roster read for the composer surfaces (autocomplete
+ * provider + mention middleware). useRoster caches under
+ * [...ROSTER_KEY, activeConnectionId] — a 3-element key — so a bare
+ * getQueryData(ROSTER_KEY) exact-match lookup returns undefined forever
+ * (issue #89303: remote handles absent from @ autocomplete, mentions
+ * unrouted). Read the live connection's entry first, then fall back to a
+ * prefix scan keeping the freshest snapshot. Never throws: cold cache or
+ * legacy queryClient returns null and callers fall back to their own path. */
+function cachedUnionRoster() {
+ if (typeof queryClient === 'undefined' || !queryClient || typeof queryClient.getQueryData !== 'function') {
+ return null
+ }
+
+ try {
+ const connectionId = String(
+ host.state.connectionId?.get?.() || host.activeConnectionId?.() || 'local'
+ )
+ const exact = queryClient.getQueryData([...ROSTER_KEY, connectionId])
+
+ if (Array.isArray(exact?.profiles)) {
+ return exact
+ }
+
+ if (typeof queryClient.getQueriesData === 'function') {
+ let best = null
+
+ // v5 takes a filters object; a legacy v3 queryClient treats the same
+ // object as the key itself and simply matches nothing — harmless.
+ for (const [, data] of queryClient.getQueriesData({ queryKey: ROSTER_KEY })) {
+ if (
+ Array.isArray(data?.profiles) &&
+ (!best || Number(data.fetchedAt || 0) > Number(best.fetchedAt || 0))
+ ) {
+ best = data
+ }
+ }
+
+ return best
+ }
+ } catch {
+ /* cache hiccup — caller falls back (middleware refetches) */
+ }
+
+ return null
+}
+
/** Merge the union agent roster (host.agents) over the active gateway's
* profiles.list. Active-source rows — matched by the LIVE connection id,
* falling back to the roster's primaryConnectionId, then the legacy
@@ -10733,7 +10779,7 @@ export default {
area: COMPOSER_AREAS.atCompletions,
data: {
provide: query => {
- const roster = queryClient.getQueryData(ROSTER_KEY)
+ const roster = cachedUnionRoster()
const profiles = Array.isArray(roster?.profiles) ? roster.profiles : []
if (!profiles.length) {
@@ -11044,9 +11090,7 @@ export default {
name: (host.state.profile.get() || 'default').trim() || 'default',
connectionId: String(host.state.connectionId?.get?.() || host.activeConnectionId?.() || 'local')
}
- const cached = typeof queryClient !== 'undefined' && queryClient && typeof queryClient.getQueryData === 'function'
- ? queryClient.getQueryData(ROSTER_KEY)
- : null
+ const cached = cachedUnionRoster()
const roster = Array.isArray(cached?.profiles) ? cached.profiles : null
let mentionedBots = roster ? resolveRosterMentions(text, roster, live) : []
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/mention-completions.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/mention-completions.test.mjs
index dd9c8c65172..8d89609052c 100644
--- a/apps/desktop/src/plugins/hermes-bots/tests/mention-completions.test.mjs
+++ b/apps/desktop/src/plugins/hermes-bots/tests/mention-completions.test.mjs
@@ -38,12 +38,20 @@ function loadProvide({ roster, active = 'default', meta = {} } = {}) {
},
document: { getElementById: () => null, createElement: () => ({}), head: { appendChild: () => undefined } },
queryClient: {
- getQueryData: () => roster,
+ // Key-exact semantics like the real QueryClient (v5): a 2-element
+ // ROSTER_KEY lookup MUST miss the 3-element cache keys useRoster
+ // writes — the bug behind issue #89303 was invisible under the old
+ // key-ignoring mock.
+ getQueryData: key =>
+ key.length === 3 && key[0] === 'hermes-bots' && key[1] === 'roster'
+ ? roster
+ : undefined,
+ getQueriesData: () => (Array.isArray(roster?.profiles) ? [[['hermes-bots', 'roster', 'local'], roster]] : []),
invalidateQueries: () => undefined,
setQueryData: () => undefined
},
host: {
- state: { profile: { get: () => active, listen: () => () => undefined }, gateway: { get: () => null, listen: () => () => undefined } },
+ state: { profile: { get: () => active, listen: () => () => undefined }, gateway: { get: () => null, listen: () => () => undefined }, connectionId: { get: () => 'local', listen: () => () => undefined } },
request: async () => ({}),
onEvent: () => () => undefined
},