Skip to content

The agent that grows with you

refactor(desktop): extract replay-gap owner matching into src/lib

teknium1878be7709f2e
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts
index 9202c1a91fd..01c29fdc5d1 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts
@@ -1,5 +1,6 @@
import type { HermesSkin } from '@hermes/shared/skin'
+import { eventSourceMatchesOwner, gatewayEventSource } from '@/lib/replay-gap-owner'
import {
notifyCronChanged,
notifyPairingChanged,
@@ -130,34 +131,15 @@ export function handleLifecycleEvent(ctx: GatewayEventContext): boolean {
? deps.sessionStateByRuntimeIdRef.current.get(runtimeId)?.storedSessionId
: null
- const eventProfile = event.profile?.trim() || 'default'
- const eventConnectionId = event.connectionId?.trim() || ''
-
- const eventMatchesOwner = (owner: SessionOwnerScope) => {
- if (!owner) {
- return false
- }
-
- if (typeof owner === 'string') {
- // A profile-only owner is the legacy/local pool route. It is safe only
- // for an untagged primary event; an explicitly tagged source must have
- // an exact route so same-named remote profiles remain isolated.
- return !eventConnectionId && eventProfile === (owner.trim() || 'default')
- }
-
- return eventConnectionId === owner.connectionId.trim() && eventProfile === (owner.profile.trim() || 'default')
- }
+ const source = gatewayEventSource(event)
const ownerForStoredSession = (id: string): SessionOwnerScope =>
- getSessionOwnerHint(id, {
- connectionId: eventConnectionId,
- profile: eventProfile
- }) ?? knownSessionOwner(ownerLookupSessionRows(), id)
+ getSessionOwnerHint(id, source) ?? knownSessionOwner(ownerLookupSessionRows(), id)
if (storedSessionId && runtimeId === deps.activeSessionIdRef.current) {
const ownerRoute = ownerForStoredSession(storedSessionId)
- if (eventMatchesOwner(ownerRoute)) {
+ if (eventSourceMatchesOwner(source, ownerRoute)) {
requestSessionResume(storedSessionId, ownerRoute && typeof ownerRoute === 'object' ? ownerRoute : undefined, {
authoritativeSnapshot: true
})
@@ -171,7 +153,7 @@ export function handleLifecycleEvent(ctx: GatewayEventContext): boolean {
return false
}
- return eventMatchesOwner(candidate.ownerRoute ?? ownerForStoredSession(candidate.storedSessionId))
+ return eventSourceMatchesOwner(source, candidate.ownerRoute ?? ownerForStoredSession(candidate.storedSessionId))
})
if (tile) {
diff --git a/apps/desktop/src/lib/replay-gap-owner.test.ts b/apps/desktop/src/lib/replay-gap-owner.test.ts
new file mode 100644
index 00000000000..34a1793ba84
--- /dev/null
+++ b/apps/desktop/src/lib/replay-gap-owner.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, it } from 'vitest'
+
+import { eventSourceMatchesOwner, gatewayEventSource } from './replay-gap-owner'
+
+describe('eventSourceMatchesOwner', () => {
+ it('matches an exact route only from its own connection, and a bare profile only from the untagged primary', () => {
+ const remote = gatewayEventSource({ connectionId: 'backend-a', profile: 'work' })
+ const local = gatewayEventSource({ profile: 'work' })
+
+ // Exact owners: same connection + profile, never a colliding id from elsewhere.
+ expect(eventSourceMatchesOwner(remote, { connectionId: 'backend-a', profile: 'work' })).toBe(true)
+ expect(eventSourceMatchesOwner(remote, { connectionId: 'backend-b', profile: 'work' })).toBe(false)
+ expect(eventSourceMatchesOwner(remote, { connectionId: 'backend-a', profile: 'default' })).toBe(false)
+
+ // Profile-only owners are the local pool route: an untagged event for the
+ // profile matches, a tagged one must not (same-named remote profiles).
+ expect(eventSourceMatchesOwner(local, 'work')).toBe(true)
+ expect(eventSourceMatchesOwner(remote, 'work')).toBe(false)
+
+ // No owner resolved at all: never act on an unattributable gap.
+ expect(eventSourceMatchesOwner(local, undefined)).toBe(false)
+ expect(eventSourceMatchesOwner(local, '')).toBe(false)
+ })
+})
diff --git a/apps/desktop/src/lib/replay-gap-owner.ts b/apps/desktop/src/lib/replay-gap-owner.ts
new file mode 100644
index 00000000000..27138ce64a9
--- /dev/null
+++ b/apps/desktop/src/lib/replay-gap-owner.ts
@@ -0,0 +1,38 @@
+import type { GatewayEvent } from '@hermes/shared'
+
+import type { SessionOwnerScope } from '@/store/session-request-router'
+
+/** Normalized source tags of a gateway event: the registry connection whose
+ * socket delivered it ('' for the untagged local primary) and its profile. */
+export interface GatewayEventSource {
+ connectionId: string
+ profile: string
+}
+
+export function gatewayEventSource(event: Pick<GatewayEvent, 'connectionId' | 'profile'>): GatewayEventSource {
+ return {
+ connectionId: event.connectionId?.trim() || '',
+ profile: event.profile?.trim() || 'default'
+ }
+}
+
+/**
+ * Whether a session-scoped event from `source` may act on a session owned by
+ * `owner`. Runtime ids are only unique per backend, so a `session.replay_gap`
+ * naming a colliding id from another gateway must not re-resume this one.
+ *
+ * A profile-only owner is the legacy/local pool route: it matches only an
+ * UNTAGGED primary event for that profile. An explicitly tagged source needs
+ * an exact route so same-named remote profiles stay isolated.
+ */
+export function eventSourceMatchesOwner(source: GatewayEventSource, owner: SessionOwnerScope): boolean {
+ if (!owner) {
+ return false
+ }
+
+ if (typeof owner === 'string') {
+ return !source.connectionId && source.profile === (owner.trim() || 'default')
+ }
+
+ return source.connectionId === owner.connectionId.trim() && source.profile === (owner.profile.trim() || 'default')
+}