fix: reconnect no longer cancels interactions a live plugin still holds

`resumeSessionAfterReconnect` cancelled pending approvals/user-input before
asking whether the plugin survived. A bare socket drop leaves the plugin
process alive with its request dicts intact and the turn genuinely blocked
on an answer — cancelling wiped the request from the UI while Hermes kept
waiting for a response that could no longer be given, hanging the thread.

Cancellation now keys off the resume outcome: a session.ready carrying an
activeTurnId means the process (and its dicts) survived, so the requests
stay on screen and remain answerable over the replacement socket; no
activeTurnId or a failed resume means they died with the process, and they
are cancelled exactly as before. The stopped-mid-flight early return also
no longer cancels on a context that stopped routing events.

The existing cancellation test modelled a restart but reported the turn as
surviving — the exact combination that cannot occur (dicts are per-process)
and the one the old code got wrong. It now reports no active turn, and a
sibling test pins the surviving-turn case to zero resolved/warning events.

Flagged by Macroscope on #4678 (discussion_r3660942051).

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Ben DavisandClaude Fable 5 committed 2026-07-27 14:24:06 -07:00
1 parent 98317245ea
commit d22d5ac00f
2 files changed
+86 -10

No files matched your search

@@ -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");
@@ -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,