From 50013a2591dd28e24f2ff6dfab344d0d01ce7174 Mon Sep 17 00:00:00 2001 From: raman325 <7243222+raman325@users.noreply.github.com> Date: Tue, 1 Sep 2026 23:11:35 -0400 Subject: [PATCH] fix(server): announce a Hermes turn before fallible configuration Publishing the active turn id before the awaited session configuration let a concurrent sendTurn merge into a turn whose preparation could still fail. Because a steer skips its own turn.started, the merged turn could settle without one ever being emitted. Emit turn.started in the same window that publishes the id, before the configuration await, and compare the skip-if-match guard against the model captured before the session record adopts this turn's selection. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01QingonqJgq96d3byM5qRh9 --- .../src/provider/Layers/HermesAdapter.test.ts | 46 +++++++++++++++++++ .../src/provider/Layers/HermesAdapter.ts | 28 +++++++---- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/apps/server/src/provider/Layers/HermesAdapter.test.ts b/apps/server/src/provider/Layers/HermesAdapter.test.ts index a57a0353e9..39893d9f91 100644 --- a/apps/server/src/provider/Layers/HermesAdapter.test.ts +++ b/apps/server/src/provider/Layers/HermesAdapter.test.ts @@ -231,6 +231,52 @@ it.layer(hermesAdapterTestLayer)("HermesAdapter (mock ACP agent)", (it) => { }), ); + it.effect("announces the turn before configuration that can fail", () => + Effect.gen(function* () { + const threadId = ThreadId.make("hermes-failed-preparation"); + const wrapperPath = yield* Effect.promise(() => makeMockHermesWrapper()); + const adapter = yield* makeTestAdapter(wrapperPath); + + const runtimeEvents: ProviderRuntimeEvent[] = []; + const runtimeEventsFiber = yield* Stream.runForEach(adapter.streamEvents, (event) => + Effect.sync(() => { + if (String(event.threadId) === String(threadId)) { + runtimeEvents.push(event); + } + }), + ).pipe(Effect.forkChild); + + yield* adapter.startSession({ + threadId, + provider: ProviderDriverKind.make("hermes"), + cwd: process.cwd(), + runtimeMode: "full-access", + }); + + // The mock rejects model ids it does not advertise, so this turn fails + // inside session configuration. It must still have announced itself: a + // steer merging into it skips its own turn.started, so a turn that can + // settle without ever starting is the regression. + yield* Effect.flip( + adapter.sendTurn({ + threadId, + input: "configure with a model the agent rejects", + attachments: [], + modelSelection: { + instanceId: ProviderInstanceId.make("hermes"), + model: "model-the-mock-never-advertises", + }, + }), + ); + + const started = runtimeEvents.filter((event) => event.type === "turn.started"); + assert.lengthOf(started, 1); + + yield* Fiber.interrupt(runtimeEventsFiber); + yield* adapter.stopSession(threadId); + }), + ); + it.effect("merges a sendTurn racing session configuration into the in-flight turn", () => Effect.gen(function* () { const threadId = ThreadId.make("hermes-steer-race"); diff --git a/apps/server/src/provider/Layers/HermesAdapter.ts b/apps/server/src/provider/Layers/HermesAdapter.ts index 368b85d520..0ef2544de0 100644 --- a/apps/server/src/provider/Layers/HermesAdapter.ts +++ b/apps/server/src/provider/Layers/HermesAdapter.ts @@ -864,15 +864,11 @@ export function makeHermesAdapter( ? turnModelSelection.model : undefined; const model = requestedTurnModel ?? ctx.session.model; - yield* applyRequestedSessionConfiguration({ - runtime: ctx.acp, - runtimeMode: ctx.session.runtimeMode, - interactionMode: input.interactionMode, - modelSelection: model === undefined ? undefined : { model }, - currentModelId: ctx.session.model, - mapError: ({ cause, method }) => - mapAcpToAdapterError(PROVIDER, input.threadId, method, cause), - }); + // Captured before the session record below adopts `model`: the + // skip-if-match guard must compare against the model the ACP session + // is actually on, not the one this turn is about to request. + const sessionModelBeforeTurn = ctx.session.model; + if (steeringTurnId === undefined) { ctx.lastPlanFingerprint = undefined; } @@ -883,6 +879,10 @@ export function makeHermesAdapter( updatedAt: yield* nowIso, }; + // Announce the turn before the awaited configuration below. A steer + // that merges into this turn skips its own `turn.started`, so if + // preparation could fail after the id was published the merged turn + // would settle without ever having started. if (steeringTurnId === undefined) { yield* offerRuntimeEvent({ type: "turn.started", @@ -894,6 +894,16 @@ export function makeHermesAdapter( }); } + yield* applyRequestedSessionConfiguration({ + runtime: ctx.acp, + runtimeMode: ctx.session.runtimeMode, + interactionMode: input.interactionMode, + modelSelection: model === undefined ? undefined : { model }, + currentModelId: sessionModelBeforeTurn, + mapError: ({ cause, method }) => + mapAcpToAdapterError(PROVIDER, input.threadId, method, cause), + }); + const promptParts: Array = []; if (input.input?.trim()) { promptParts.push({ type: "text", text: input.input.trim() });