Skip to content

The agent that grows with you

fix(desktop): route approval responses through the runtime event's exact owner

liuhao10247f53ad17413c
diff --git a/apps/desktop/src/store/session-states-runtime-map.test.ts b/apps/desktop/src/store/session-states-runtime-map.test.ts
index 1b12f91a9a5..7990c062eab 100644
--- a/apps/desktop/src/store/session-states-runtime-map.test.ts
+++ b/apps/desktop/src/store/session-states-runtime-map.test.ts
@@ -7,8 +7,10 @@ import { isSessionOwnerResolutionError } from '@/store/session-owner-resolution'
import {
$sessionTiles,
clearAllSessionStates,
+ dropSessionState,
knownOwnerForSession,
publishSessionState,
+ recordSessionEventScope,
requestForOwnedSession,
storedSessionIdForRuntimeId
} from '@/store/session-states'
@@ -126,4 +128,44 @@ describe('knownOwnerForSession / requestForOwnedSession', () => {
).resolves.toEqual({ ok: true })
expect(ambient).toHaveBeenCalledWith('approval.respond', { session_id: 'rt-orphan' })
})
+
+ it('routes a connection-tagged orphan runtime through the owner its inbound event recorded (#97511)', () => {
+ // Registry topology, multiple profiles, no tile/hint/row binding for the
+ // runtime — the approval.request event itself proved the exact owner.
+ $profiles.set([{ name: 'default' }, { name: 'omar' }] as never)
+ recordSessionEventScope({ connectionId: 'homelab', profile: 'omar', session_id: 'rt-unbound' })
+
+ expect(knownOwnerForSession('rt-unbound')).toEqual({ connectionId: 'homelab', profile: 'omar' })
+
+ // An event without a profile tag still records the 'default' convention
+ // every other owner source uses.
+ recordSessionEventScope({ connectionId: 'homelab', session_id: 'rt-unprofiled' })
+ expect(knownOwnerForSession('rt-unprofiled')).toEqual({ connectionId: 'homelab', profile: 'default' })
+ })
+
+ it('still prefers the durable stored owner when a stale runtime ledger entry collides with a stored id (#97511)', () => {
+ // Pathological collision: some dead runtime's id equals a live stored id.
+ // The persisted hint (durable identity) must outrank the ledger entry.
+ setSessionOwnerHint('stored-live', { connectionId: 'local', profile: 'omar' })
+ recordSessionEventScope({ connectionId: 'spark', profile: 'default', session_id: 'stored-live' })
+
+ expect(knownOwnerForSession('stored-live')).toEqual({ connectionId: 'local', profile: 'omar' })
+ })
+
+ it('keeps failing closed for untagged or unknown runtimes in multi-profile topology (#97511)', () => {
+ $profiles.set([{ name: 'default' }, { name: 'omar' }] as never)
+ // Untagged events carry no connectionId and record nothing.
+ recordSessionEventScope({ profile: 'omar', session_id: 'rt-untagged' })
+
+ expect(knownOwnerForSession('rt-untagged')).toBeUndefined()
+ expect(knownOwnerForSession('rt-never-seen')).toBeUndefined()
+ })
+
+ it('drops the recorded event owner together with the runtime state (#97511)', () => {
+ recordSessionEventScope({ connectionId: 'homelab', profile: 'omar', session_id: 'rt-dropped' })
+ expect(knownOwnerForSession('rt-dropped')).toEqual({ connectionId: 'homelab', profile: 'omar' })
+
+ dropSessionState('rt-dropped')
+ expect(knownOwnerForSession('rt-dropped')).toBeUndefined()
+ })
})
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index 56494073f40..cde1cd530f8 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -83,9 +83,22 @@ export const $sessionStates = atom<Record<string, ClientSessionState>>({})
const sessionScopeByRuntimeId = new Map<string, string>()
+// Structured twin of the scope ledger: the same inbound events also carry the
+// exact (connectionId, profile) owner, which the composite scope string
+// cannot give back. Consumed as the LAST rung of knownOwnerForSession so a
+// runtime whose event source already proved its owner can still route
+// session-scoped RPCs (approval.respond) when every durable binding
+// (tile / hint / row) is absent — while durable stored identity keeps
+// outranking it (#97511).
+const sessionOwnerByRuntimeId = new Map<string, SessionOwnerRoute>()
+
export function recordSessionEventScope(event: { connectionId?: string; profile?: string; session_id?: string }): void {
if (event.session_id && event.connectionId) {
sessionScopeByRuntimeId.set(event.session_id, registryBackendScopeKey(event.connectionId, event.profile))
+ sessionOwnerByRuntimeId.set(event.session_id, {
+ connectionId: event.connectionId,
+ profile: String(event.profile ?? '').trim() || 'default'
+ })
}
}
@@ -506,6 +519,7 @@ export function dropSessionState(runtimeId: string) {
clearWatchdog(runtimeId)
clearSessionProviderWait(runtimeId)
sessionScopeByRuntimeId.delete(runtimeId)
+ sessionOwnerByRuntimeId.delete(runtimeId)
const current = $sessionStates.get()
setSessionStalled(current[runtimeId]?.storedSessionId, false)
@@ -532,6 +546,7 @@ export function clearAllSessionStates() {
settledExpiry.clear()
clearAllProviderWaits()
sessionScopeByRuntimeId.clear()
+ sessionOwnerByRuntimeId.clear()
$stalledSessionIds.set([])
$sessionStates.set({})
}
@@ -970,6 +985,13 @@ export function openTileGatewayScopes(): Set<string> {
* `profile` stamp) was already loaded for the sidebar's cron section. The
* hint outranks the row for the same reason as contrib/wiring's ladder: a
* row can be stamped from the ambient profile and carries no connection.
+ * Last rung: the owner recorded from the inbound runtime event itself
+ * (sessionOwnerByRuntimeId, #97511) — an orphan runtime whose tile/hint/row
+ * binding is absent or stale still routes through the exact
+ * (connectionId, profile) its events proved, while every durable rung above
+ * keeps outranking it, so a stored-id collision never inherits a stale
+ * runtime ledger entry. Untagged events record nothing, so unknown owners in
+ * multi-profile topology still fail closed.
* Returns undefined when no owner is known — the caller fails closed
* (assertSessionOwnerResolved), never falls to "active".
*/
@@ -983,7 +1005,8 @@ export function knownOwnerForSession(sessionId: null | string | undefined): Sess
return (
sessionTileOwnerRoute(storedSessionId) ??
getSessionOwnerHint(storedSessionId) ??
- knownSessionOwner(ownerLookupSessionRows(), storedSessionId)
+ knownSessionOwner(ownerLookupSessionRows(), storedSessionId) ??
+ sessionOwnerByRuntimeId.get(sessionId)
)
}