Skip to content

The agent that grows with you

fix(plugins): keep retrying a plugin socket when the gateway connection can't be resolved yet

unsupportedpastels2f056af7c070
diff --git a/apps/desktop/src/api/plugins.test.ts b/apps/desktop/src/api/plugins.test.ts
index a0968e15908..7cb0cebb692 100644
--- a/apps/desktop/src/api/plugins.test.ts
+++ b/apps/desktop/src/api/plugins.test.ts
@@ -151,6 +151,84 @@ describe('pluginSocket credentials and refusals (PLG-1)', () => {
dispose()
})
+ // A restart can also fail before any dial: the descriptor lookup re-ensures the gateway, which
+ // rejects while it starts or drains, or after a stale ticket forgets the cached descriptor.
+ const dropOpenSocket = async () => {
+ FakeSocket.all.at(-1)?.onopen?.()
+ await vi.advanceTimersByTimeAsync(RECONNECT_STABLE_OPEN_MS)
+ FakeSocket.all.at(-1)?.close()
+ }
+
+ it('keeps retrying while the connection descriptor cannot be resolved, then reconnects', async () => {
+ vi.useFakeTimers()
+ vi.stubGlobal('WebSocket', FakeSocket)
+ desktop(local)
+ const getConnection = vi.mocked(window.hermesDesktop.getConnection)
+ const frames: unknown[] = []
+
+ const dispose = pluginSocket('kanban', '/events', frame => frames.push(frame))
+ await vi.advanceTimersByTimeAsync(0)
+
+ for (let i = 0; i < 8; i += 1) {
+ getConnection.mockRejectedValueOnce(new Error('Gateway is draining'))
+ }
+
+ await dropOpenSocket()
+ await vi.advanceTimersByTimeAsync(9 * 31_000)
+
+ expect(getConnection).toHaveBeenCalledTimes(10)
+ expect(FakeSocket.all).toHaveLength(2)
+ FakeSocket.all[1].onopen?.()
+ FakeSocket.all[1].onmessage?.({ data: '{"ok":1}' })
+ expect(frames).toEqual([{ ok: 1 }])
+ dispose()
+ })
+
+ it('re-dials when the connection descriptor lookup never answers', async () => {
+ vi.useFakeTimers()
+ vi.stubGlobal('WebSocket', FakeSocket)
+ desktop(local)
+ const getConnection = vi.mocked(window.hermesDesktop.getConnection)
+ const frames: unknown[] = []
+
+ const dispose = pluginSocket('kanban', '/events', frame => frames.push(frame))
+ await vi.advanceTimersByTimeAsync(0)
+ getConnection.mockImplementationOnce(() => new Promise(() => undefined))
+ await dropOpenSocket()
+ await vi.advanceTimersByTimeAsync(RECONNECT_ATTEMPT_TIMEOUT_MS + 2 * 31_000)
+
+ expect(getConnection).toHaveBeenCalledTimes(3)
+ expect(FakeSocket.all).toHaveLength(2)
+ FakeSocket.all[1].onopen?.()
+ FakeSocket.all[1].onmessage?.({ data: '{"ok":2}' })
+ expect(frames).toEqual([{ ok: 2 }])
+ dispose()
+ })
+
+ it('a socket waiting on the descriptor dials the new profile once it resolves', async () => {
+ vi.useFakeTimers()
+ vi.stubGlobal('WebSocket', FakeSocket)
+ setApiRequestProfile('coder')
+ const mint = desktop(local)
+ const getConnection = vi.mocked(window.hermesDesktop.getConnection)
+
+ const dispose = pluginSocket('kanban', '/events', () => undefined)
+ await vi.advanceTimersByTimeAsync(0)
+ getConnection.mockRejectedValue(new Error('Gateway is starting'))
+ await dropOpenSocket()
+ await vi.advanceTimersByTimeAsync(600_000)
+ expect(FakeSocket.all).toHaveLength(1)
+
+ getConnection.mockResolvedValue(local as never)
+ setApiRequestProfile('research')
+ await vi.advanceTimersByTimeAsync(31_000)
+
+ expect(getConnection).toHaveBeenLastCalledWith('research')
+ expect(mint).toHaveBeenLastCalledWith(expect.objectContaining({ profile: 'research' }))
+ expect(FakeSocket.all).toHaveLength(2)
+ dispose()
+ })
+
// Main names the one refusal it can identify: a grant mint refused for a permission reason.
it('stops after repeated permission refusals from main', async () => {
vi.useFakeTimers()
diff --git a/apps/desktop/src/api/plugins.ts b/apps/desktop/src/api/plugins.ts
index 363c5635077..26974398a27 100644
--- a/apps/desktop/src/api/plugins.ts
+++ b/apps/desktop/src/api/plugins.ts
@@ -125,9 +125,10 @@ const requestScopeKey = () => `${getApiRequestConnection() ?? ''}\n${getApiReque
* its REST twin uses; OAuth remotes resolve null (callers keep their polling
* fallback — every consumer must have one anyway, since a socket can drop).
*
- * Auto-reconnects with capped backoff until disposed. A dial that never opens
- * keeps retrying: the browser cannot tell a refused upgrade from a gateway that
- * is restarting, draining or being re-ensured. It stops after
+ * Auto-reconnects with capped backoff until disposed. A dial that never opens,
+ * or a connection descriptor or grant that can't be resolved yet, keeps
+ * retrying: the browser cannot tell a refused upgrade from a gateway that is
+ * restarting, draining or being re-ensured. It stops after
* MAX_REFUSED_PLUGIN_DIALS refusals in a row that do not clear by themselves:
* main refusing the grant mint for a permission reason, or a server that
* accepts and closes at once. A connection or profile change resets the count
@@ -164,11 +165,24 @@ export function pluginSocket(pluginId: string, path: string, onMessage: (data: u
}
const connect = async () => {
- const connection = await activeConnection().catch(() => null)
+ // No bridge: stay on the polling fallback.
+ if (!window.hermesDesktop?.getConnection) {
+ return
+ }
+
+ let connection: HermesConnection
+
+ try {
+ connection = await activeConnection()
+ } catch {
+ // The descriptor can't be resolved yet or the lookup timed out (gateway starting, draining,
+ // or re-ensured after a stale ticket): a connection failure, like a dial that never opened.
+ return retry(false)
+ }
- // No bridge / OAuth cookie auth (WS tickets are single-use, core-managed):
- // stay on the polling fallback rather than half-working.
- if (disposed || !connection || connection.authMode === 'oauth') {
+ // OAuth cookie auth (WS tickets are single-use, core-managed): stay on the
+ // polling fallback rather than half-working.
+ if (disposed || connection.authMode === 'oauth') {
return
}
diff --git a/website/docs/developer-guide/desktop-plugin-sdk.md b/website/docs/developer-guide/desktop-plugin-sdk.md
index baaefe3e9da..8bdc7c2c0ba 100644
--- a/website/docs/developer-guide/desktop-plugin-sdk.md
+++ b/website/docs/developer-guide/desktop-plugin-sdk.md
@@ -1884,10 +1884,11 @@ profile.
When the socket gives up (your polling fallback is then the only feed):
-- **Keeps retrying**, with jittered backoff capped at 30 s, while dials fail to
- open. The client can't tell a refused upgrade from a gateway that is
- restarting, draining or being re-ensured, so a gateway restart of any length
- reconnects on its own.
+- **Keeps retrying**, with jittered backoff capped at 30 s, while Desktop can't
+ resolve the gateway connection or mint the grant yet, or dials fail to open.
+ The client can't tell a refused upgrade from a gateway that is restarting,
+ draining or being re-ensured, so the socket keeps retrying through a restart
+ of any length and reconnects once the gateway answers again.
- **Stops after 5 in a row** of refusals that don't clear by themselves: Desktop
refusing to mint the grant for a permission reason (an unsafe gateway control
path or profile home), or your backend accepting the socket and closing it at