Skip to content

The agent that grows with you

fix(desktop): key resume single-flight by connection, not profile scope

teknium166b5d634467b
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.test.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.test.ts
index fa4b04df878..74e38b54de2 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.test.ts
@@ -90,6 +90,26 @@ describe('singleFlightSessionResume', () => {
])
})
+ it('coalesces a scoped foreground resume with an unscoped recovery for the same runtime under a non-default profile', async () => {
+ // use-session-actions passes the owner scope; submit/rewind recovery and
+ // the route resolver pass none. Both dial the same socket, so they must
+ // share ONE flight or a wake-up storm mints two runtimes (#91276).
+ const run = vi.fn(async () => {
+ await new Promise(resolve => setTimeout(resolve, 10))
+
+ return { session_id: 'rt-work' }
+ })
+
+ const [scoped, unscoped] = await Promise.all([
+ singleFlightSessionResume('stored-work', run, { scope: 'work' }),
+ singleFlightSessionResume('stored-work', run)
+ ])
+
+ expect(scoped).toEqual({ session_id: 'rt-work' })
+ expect(unscoped).toEqual({ session_id: 'rt-work' })
+ expect(run).toHaveBeenCalledTimes(1)
+ })
+
it('a rejected flight is not cached: the next caller retries', async () => {
const run = vi
.fn<() => Promise<{ session_id: string }>>()
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.ts
index 9848d830fa1..149b19e025c 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.ts
@@ -1,7 +1,7 @@
-import { type ProfileScope, profileScopeKey } from '@/api/client'
+import { ambientOwnerConnectionId, type ProfileScope } from '@/api/client'
/**
- * Single-flight guard for `session.resume`, keyed by owner scope + STORED id.
+ * Single-flight guard for `session.resume`, keyed by backend connection + STORED id.
*
* After sleep/wake or a reconnect, many independent surfaces discover the same
* dead runtime at once — submit recovery, slash/rewind recovery, tile resumes,
@@ -13,8 +13,21 @@ import { type ProfileScope, profileScopeKey } from '@/api/client'
* per stored id, no matter which hook instance it lives in. All participating
* callers resolve to a `session.resume`-shaped response (an object carrying
* `session_id`); joiners receive whatever the winning call returns.
+ *
+ * The key is the CONNECTION, not the profile: a stored id is unique within one
+ * backend's state.db but two registry backends can hold the same id, so their
+ * flights must stay apart. Callers that pass no scope (submit/rewind recovery,
+ * the route resolver) dial the ambient socket, as does a bare-profile owner, so
+ * both fold onto the ambient connection and still coalesce with a scoped
+ * foreground resume of the same runtime under any profile.
*/
+function flightConnectionId(scope?: ProfileScope): string {
+ const explicit = scope && typeof scope === 'object' ? (scope.connectionId ?? '').trim() : ''
+
+ return explicit || ambientOwnerConnectionId() || ''
+}
+
interface SessionResumeFlight {
includesMessages: boolean
promise: Promise<unknown>
@@ -27,7 +40,7 @@ export function singleFlightSessionResume<T>(
run: () => Promise<T>,
options?: { requiresMessages?: boolean; scope?: ProfileScope }
): Promise<T> {
- const flightKey = JSON.stringify([profileScopeKey(options?.scope), storedSessionId])
+ const flightKey = JSON.stringify([flightConnectionId(options?.scope), storedSessionId])
const existing = _inFlightResumeByStoredSessionId.get(flightKey)
if (existing && (!options?.requiresMessages || existing.includesMessages)) {