Skip to content

The agent that grows with you

fix(desktop): retry only the real cwd_explicit skew rejection, with a test

kshitijk4poor3da8b24babef
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index ec873e43292..8416102dc93 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -165,6 +165,7 @@ import { reconcilePersistedLiveTurn } from './persisted-live-turn'
import { provisionalTranscriptPaint, transcriptRestScope } from './provisional-transcript'
import { pendingClarifyToolPayload, restorePendingClarifyFromSnapshot } from './restore-pending-clarify'
import { projectPendingConnection, restorePendingConnectionFromSnapshot } from './restore-pending-connection'
+import { createGatewaySession } from './session-create-request'
import {
createPersistedDisplayTranscriptProvenance,
hasPersistedDisplayTranscriptProvenance,
@@ -373,28 +374,6 @@ async function desktopSessionCreateParams(
}
}
-/**
- * A desktop client can briefly outpace a pooled/remote backend during updates.
- * `cwd_explicit` is a newer session.create field, so retry only that narrow
- * contract mismatch without dropping the workspace cwd itself.
- */
-export function omitUnsupportedCwdExplicit(params: Record<string, unknown>): Record<string, unknown> {
- const { cwd_explicit: _cwdExplicit, ...compatible } = params
- return compatible
-}
-
-export async function requestSessionCreate<T>(
- params: Record<string, unknown>,
- request: (requestParams: Record<string, unknown>) => Promise<T>
-): Promise<T> {
- try {
- return await request(params)
- } catch (error) {
- if (!String(error).includes('cwd_explicit')) throw error
- return request(omitUnsupportedCwdExplicit(params))
- }
-}
-
interface FreshSessionDraftOptions {
preserveRoute?: boolean
replaceRoute?: boolean
@@ -752,19 +731,7 @@ export function useSessionActions({
let stored: null | string
try {
- created = await requestSessionCreate(params, requestParams =>
- capturedRoute
- ? requestGatewayForAgent<SessionCreateResponse>(
- capturedRoute.connectionId,
- capturedRoute.profile,
- 'session.create',
- requestParams,
- undefined,
- undefined,
- { spawnPriority: 'foreground' }
- )
- : requestGateway<SessionCreateResponse>('session.create', requestParams)
- )
+ created = await createGatewaySession(capturedRoute, params, requestGateway)
stored = created.stored_session_id ?? null
@@ -1043,19 +1010,7 @@ export function useSessionActions({
let stored: string | undefined
try {
- created = await requestSessionCreate(params, requestParams =>
- capturedRoute
- ? requestGatewayForAgent<SessionCreateResponse>(
- capturedRoute.connectionId,
- capturedRoute.profile,
- 'session.create',
- requestParams,
- undefined,
- undefined,
- { spawnPriority: 'foreground' }
- )
- : requestGateway<SessionCreateResponse>('session.create', requestParams)
- )
+ created = await createGatewaySession(capturedRoute, params, requestGateway)
stored = created.stored_session_id
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/session-create-request.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/session-create-request.test.ts
new file mode 100644
index 00000000000..1ce40c3efe6
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/session-create-request.test.ts
@@ -0,0 +1,52 @@
+import { JsonRpcGatewayError } from '@hermes/shared'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+const requestGatewayForAgent = vi.fn()
+
+vi.mock('@/store/gateway', () => ({
+ requestGatewayForAgent: (...args: unknown[]) => requestGatewayForAgent(...args)
+}))
+
+import { createGatewaySession } from './session-create-request'
+
+// Verbatim admission rejection from a backend predating #122899 (tui_gateway/contracts/registry.py).
+const PRE_122899_REJECTION = new JsonRpcGatewayError(
+ 'invalid params for session.create: cwd_explicit: Extra inputs are not permitted — the client and the ' +
+ 'Hermes backend are out of sync (different versions); run `hermes update` and restart both',
+ { code: 4000 }
+)
+
+const params = { cols: 96, cwd: '/work/repo', cwd_explicit: true, profile: 'default', source: 'desktop' }
+const route = { connectionId: 'cloud', profile: 'default' }
+
+describe('createGatewaySession', () => {
+ beforeEach(() => requestGatewayForAgent.mockReset())
+
+ it('resends once without cwd_explicit (cwd kept) when a pre-#122899 backend rejects the flag', async () => {
+ requestGatewayForAgent.mockRejectedValueOnce(PRE_122899_REJECTION).mockResolvedValueOnce({ session_id: 's1' })
+
+ await expect(createGatewaySession(route, params, vi.fn())).resolves.toEqual({ session_id: 's1' })
+
+ const sent = requestGatewayForAgent.mock.calls.map(call => call[3])
+ const { cwd_explicit: _flag, ...withoutFlag } = params
+
+ expect(sent).toEqual([params, withoutFlag])
+ })
+
+ it('never resends on any other failure, even one that mentions the field', async () => {
+ const failures = [
+ new Error('request timed out'),
+ new Error('handler error: could not resolve cwd_explicit workspace'),
+ PRE_122899_REJECTION
+ ]
+
+ for (const [index, failure] of failures.entries()) {
+ const requestGateway = vi.fn().mockRejectedValue(failure)
+ // The last case: the flag was never sent, so a resend would be identical.
+ const sentParams = index === failures.length - 1 ? { cwd: '/work/repo' } : params
+
+ await expect(createGatewaySession(null, sentParams, requestGateway)).rejects.toBe(failure)
+ expect(requestGateway).toHaveBeenCalledTimes(1)
+ }
+ })
+})
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/session-create-request.ts b/apps/desktop/src/app/session/hooks/use-session-actions/session-create-request.ts
new file mode 100644
index 00000000000..327758d8bc4
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/session-create-request.ts
@@ -0,0 +1,50 @@
+import { isOutOfSyncRpcParams } from '@/lib/gateway-rpc'
+import { requestGatewayForAgent } from '@/store/gateway'
+import type { AgentProfileRoute } from '@/store/profile'
+import type { SessionCreateResponse } from '@/types/hermes'
+
+type RequestGateway = <T>(method: string, params?: Record<string, unknown>) => Promise<T>
+
+/** A backend predating #122899 (contract without `cwd_explicit`) rejects the
+ * whole create at admission (`tui_gateway/contracts/registry.py::validate_params`,
+ * code 4000, handler never runs) — e.g. a Hermes Cloud backend behind a
+ * Desktop that updates from main (#128971). Those backends always honoured the
+ * client `cwd`, so resending without the flag reproduces their behaviour.
+ * Delete once no supported backend predates #122899. */
+function rejectsCwdExplicit(params: Record<string, unknown>, error: unknown): boolean {
+ const message = error instanceof Error ? error.message : String(error)
+
+ return 'cwd_explicit' in params && isOutOfSyncRpcParams(message) && /:\s*cwd_explicit:/.test(message)
+}
+
+/** `session.create` on the captured owner route (or the window's gateway). */
+export async function createGatewaySession(
+ route: AgentProfileRoute | null,
+ params: Record<string, unknown>,
+ requestGateway: RequestGateway
+): Promise<SessionCreateResponse> {
+ const send = (requestParams: Record<string, unknown>) =>
+ route
+ ? requestGatewayForAgent<SessionCreateResponse>(
+ route.connectionId,
+ route.profile,
+ 'session.create',
+ requestParams,
+ undefined,
+ undefined,
+ { spawnPriority: 'foreground' }
+ )
+ : requestGateway<SessionCreateResponse>('session.create', requestParams)
+
+ try {
+ return await send(params)
+ } catch (error) {
+ if (!rejectsCwdExplicit(params, error)) {
+ throw error
+ }
+
+ const { cwd_explicit: _cwdExplicit, ...compatible } = params
+
+ return send(compatible)
+ }
+}