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 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QingonqJgq96d3byM5qRh9
This commit is contained in:
raman325andClaude Fable 5 committed 2026-09-02 14:43:57 -04:00
1 parent 462742eb49
commit 50013a2591
2 files changed
+65 -9

No files matched your search

@@ -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");
@@ -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<EffectAcpSchema.ContentBlock> = [];
if (input.input?.trim()) {
promptParts.push({ type: "text", text: input.input.trim() });