diff --git a/apps/server/src/provider/Layers/HermesAdapter.test.ts b/apps/server/src/provider/Layers/HermesAdapter.test.ts index b22dba20ce..09d81b534f 100644 --- a/apps/server/src/provider/Layers/HermesAdapter.test.ts +++ b/apps/server/src/provider/Layers/HermesAdapter.test.ts @@ -924,9 +924,11 @@ it.effect("cancels pending approvals and user input on reconnect without decidin yield* drain; // The plugin restarts and forgets both requests; answering them afterwards - // would come back `request-not-found` and park the turn forever. + // would come back `request-not-found` and park the turn forever. A + // restarted plugin reports no active turn — that absence is exactly how + // the adapter knows the requests died with the process. yield* harness.disconnect; - harness.state.resumeActiveTurnId = started.turnId; + harness.state.resumeActiveTurnId = undefined; yield* harness.reconnect; const resolvedApproval = seen.find( @@ -955,6 +957,61 @@ it.effect("cancels pending approvals and user input on reconnect without decidin }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); +it.effect("keeps pending interactions when the turn survives the reconnect", () => + Effect.gen(function* () { + // A bare socket drop the plugin process survived: its request dicts are + // intact and the turn is genuinely blocked on the user's answer. Cancelling + // here is the hang Macroscope flagged — the UI loses the request while + // Hermes keeps waiting for the response forever. + const instanceId = ProviderInstanceId.make("hermes_pending_alive"); + const threadId = ThreadId.make("thread-pending-alive"); + const sessionId = HermesGatewaySessionId.make("session-pending-alive"); + const harness = yield* makeReconnectHarness({ + instanceId, + threadId, + initialSessionId: sessionId, + }); + + yield* harness.adapter.startSession({ + threadId, + providerInstanceId: instanceId, + runtimeMode: "full-access", + }); + const started = yield* harness.adapter.sendTurn({ threadId, input: "needs approval" }); + const { seen, fiber } = yield* collectEvents(harness.adapter); + + yield* PubSub.publish(harness.brokerEvents, { + instanceId, + message: { + type: "request.opened", + protocolVersion: HERMES_GATEWAY_PROTOCOL_VERSION, + threadId, + sessionId, + turnId: started.turnId, + requestId: HermesGatewayRequestId.make("approval-still-live"), + requestType: "command_execution_approval", + detail: "Hermes wants to run a command", + }, + }); + yield* drain; + + yield* harness.disconnect; + harness.state.resumeActiveTurnId = started.turnId; + yield* harness.reconnect; + + assert.isUndefined( + seen.find((event) => event.type === "request.resolved"), + "a request the plugin still holds must stay open", + ); + assert.isUndefined( + seen.find((event) => event.type === "runtime.warning"), + "no cancellation warning may appear for a surviving turn", + ); + + yield* Fiber.interrupt(fiber); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + it.effect("fails sends immediately and actionably while the gateway is offline", () => Effect.gen(function* () { const instanceId = ProviderInstanceId.make("hermes_offline"); diff --git a/apps/server/src/provider/Layers/HermesAdapter.ts b/apps/server/src/provider/Layers/HermesAdapter.ts index 58fecd2d37..da555479d7 100644 --- a/apps/server/src/provider/Layers/HermesAdapter.ts +++ b/apps/server/src/provider/Layers/HermesAdapter.ts @@ -499,10 +499,14 @@ export const makeHermesAdapter = Effect.fn("makeHermesAdapter")(function* (input Effect.gen(function* () { const threadId = context.session.threadId; const previousTurnId = context.session.activeTurnId; - yield* cancelPendingInteractions( - context, - "the Hermes gateway reconnected and the plugin no longer holds them.", - ); + // Cancellation is deliberately NOT issued here. A connection-generation + // change covers two very different events: a plugin restart (its + // request dicts are gone, pending interactions are unanswerable) and a + // bare socket drop the plugin process survived (it still holds the + // requests and the turn is genuinely blocked on an answer). Which one + // happened is only knowable from the resume outcome below — cancelling + // up front wiped the approval UI while Hermes kept waiting for the + // response, hanging the thread with nothing left to click. const outcome = yield* broker .request(input.instanceId, { type: "session.ensure", @@ -516,7 +520,8 @@ export const makeHermesAdapter = Effect.fn("makeHermesAdapter")(function* (input // Anything other than a clean session.ready leaves us unable to tell // whether Hermes is still generating, with no channel to ask. Leaving the // thread "running" would hang it, so it settles with the reason spelled - // out instead. + // out instead — and its pending interactions are unanswerable, so they + // are cancelled too. const failure = outcome._tag === "Failure" ? `Hermes could not resume this thread after reconnecting: ${outcome.failure.detail}` @@ -526,6 +531,10 @@ export const makeHermesAdapter = Effect.fn("makeHermesAdapter")(function* (input ? `Hermes returned '${outcome.success.type}' instead of session.ready while resuming this thread.` : undefined; if (failure !== undefined) { + yield* cancelPendingInteractions( + context, + "the Hermes gateway reconnected but this thread could not be resumed.", + ); if (previousTurnId) return yield* settleDeadTurn(context, previousTurnId, failure); updateSession(context, { status: "error", lastError: failure }); return; @@ -534,7 +543,8 @@ export const makeHermesAdapter = Effect.fn("makeHermesAdapter")(function* (input const ready = outcome.success; // The session may have been stopped or exited while the request was in - // flight; do not resurrect it. + // flight; do not resurrect it. Its pending maps go with it — nothing to + // cancel on a context no longer routing events. if (sessions.get(threadId) !== context) return; // Hermes owns the session id and a restarted plugin mints a new one. @@ -549,13 +559,22 @@ export const makeHermesAdapter = Effect.fn("makeHermesAdapter")(function* (input const activeTurnId = ready.activeTurnId; if (activeTurnId !== undefined) { - // The turn outlived the socket. Keep streaming into it and emit nothing - // disruptive — what follows belongs to the turn the UI already renders. + // The turn outlived the socket, and with it the plugin process — its + // request dicts are intact and any pending approval/user-input is + // still answerable over the replacement connection. Keep streaming + // into the turn, keep the requests on screen, emit nothing disruptive. updateSession(context, { status: "running", activeTurnId }); trackTurn(context, activeTurnId); return; } + // No active turn on the Hermes side: the plugin restarted (or finished + // and forgot). Its request dicts are per-process, so whatever the user + // is still looking at can only ever be answered `request-not-found`. + yield* cancelPendingInteractions( + context, + "the Hermes gateway reconnected and the plugin no longer holds them.", + ); if (previousTurnId) { yield* settleDeadTurn( context,