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() });