fix(server): honor acceptAlways and never announce an unstartable turn

`selectHermesPermissionOptionId` handled only `acceptForSession` and
`accept`, so the valid `acceptAlways` decision fell through to the
agent's reject option — a permanent approval denied the tool call.
Both persistent decisions now map to `allow_always`, with the same
`allow_once` fallback.

`turn.started` was also published before prompt validation, so a
whitespace-only prompt or an attachment set with no images announced a
turn that then failed with no terminal event, leaving consumers with a
permanently running turn. Announcement now happens after both fallible
preparation steps (validation and session configuration) and is keyed on
a session-level announced flag, so a steer that overtakes a not-yet
announced turn still announces it exactly once. Preparation failures
release the turn claim so the next sendTurn opens a fresh turn.

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 50013a2591
commit 79a4f3409d
2 files changed
+98 -36

No files matched your search

@@ -107,6 +107,34 @@ it("answers a manual decision with the agent's advertised option id", () => {
assert.equal(selectHermesPermissionOptionId(request, "decline"), "deny-9");
});
it("treats acceptAlways as a persistent allow, never a rejection", () => {
const options = [
{ optionId: "permit-42", name: "Allow once", kind: "allow_once" as const },
{ optionId: "permit-always-7", name: "Always allow", kind: "allow_always" as const },
{ optionId: "deny-9", name: "Reject", kind: "reject_once" as const },
];
assert.equal(
selectHermesPermissionOptionId(
{ sessionId: "mock-session-1", toolCall: { toolCallId: "tool-1" }, options },
"acceptAlways",
),
"permit-always-7",
);
// Without allow_always the persistent accept degrades to a one-shot allow,
// never to the reject option.
assert.equal(
selectHermesPermissionOptionId(
{
sessionId: "mock-session-1",
toolCall: { toolCallId: "tool-1" },
options: options.filter((option) => option.kind !== "allow_always"),
},
"acceptAlways",
),
"permit-42",
);
});
it("falls back to allow_once for a session accept when allow_always is absent", () => {
const optionId = selectHermesPermissionOptionId(
{
@@ -231,7 +259,7 @@ it.layer(hermesAdapterTestLayer)("HermesAdapter (mock ACP agent)", (it) => {
}),
);
it.effect("announces the turn before configuration that can fail", () =>
it.effect("never announces a turn whose preparation fails", () =>
Effect.gen(function* () {
const threadId = ThreadId.make("hermes-failed-preparation");
const wrapperPath = yield* Effect.promise(() => makeMockHermesWrapper());
@@ -253,10 +281,10 @@ it.layer(hermesAdapterTestLayer)("HermesAdapter (mock ACP agent)", (it) => {
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.
// Rejected prompt: whitespace-only text with no usable attachments.
yield* Effect.flip(adapter.sendTurn({ threadId, input: " \n ", attachments: [] }));
// Rejected configuration: the mock refuses model ids it never advertised.
yield* Effect.flip(
adapter.sendTurn({
threadId,
@@ -269,8 +297,19 @@ it.layer(hermesAdapterTestLayer)("HermesAdapter (mock ACP agent)", (it) => {
}),
);
const started = runtimeEvents.filter((event) => event.type === "turn.started");
assert.lengthOf(started, 1);
// Neither failure may surface a turn: a started turn that cannot settle
// leaves consumers with a permanently running turn.
assert.lengthOf(
runtimeEvents.filter((event) => event.type === "turn.started"),
0,
);
// The claim was released, so a good turn afterwards still runs normally.
yield* adapter.sendTurn({ threadId, input: "now a real prompt", attachments: [] });
assert.lengthOf(
runtimeEvents.filter((event) => event.type === "turn.started"),
1,
);
yield* Fiber.interrupt(runtimeEventsFiber);
yield* adapter.stopSession(threadId);
@@ -132,6 +132,10 @@ interface HermesSessionContext {
readonly turns: Array<{ id: TurnId; items: Array<unknown> }>;
lastPlanFingerprint: string | undefined;
activeTurnId: TurnId | undefined;
/** Whether `turn.started` has been published for `activeTurnId`. A steer
* arriving before the first prompt announced the turn must announce it, so
* exactly one start is emitted and no turn can settle without one. */
activeTurnAnnounced: boolean;
/** Number of sendTurn prompts currently in flight or being prepared.
* >0 means a turn is actively running, so a new sendTurn is a steer that
* continues it, and only the last remaining prompt settles the turn. */
@@ -294,15 +298,17 @@ function applyRequestedSessionConfiguration<E>(input: {
* advertised. Hermes mints its own option ids, so the decision must be matched
* by `kind` and answered with the advertised id — replying with a canonical
* literal leaves the permission-gated turn blocked whenever the agent uses
* opaque ids. Falls back to `allow_once` for a session-wide accept, since an
* agent that omits `allow_always` still honours a one-shot allow.
* opaque ids. Both persistent decisions (`acceptForSession`, `acceptAlways`)
* map to `allow_always`; they differ in T3's retention scope, not in what the
* agent is being told. Falls back to `allow_once` for either, since an agent
* that omits `allow_always` still honours a one-shot allow.
*/
export function selectHermesPermissionOptionId(
request: EffectAcpSchema.RequestPermissionRequest,
decision: Exclude<ProviderApprovalDecision, "cancel">,
): string | undefined {
const preferredKind =
decision === "acceptForSession"
decision === "acceptForSession" || decision === "acceptAlways"
? "allow_always"
: decision === "accept"
? "allow_once"
@@ -313,7 +319,7 @@ export function selectHermesPermissionOptionId(
if (preferredId) {
return preferredId;
}
if (decision === "acceptForSession") {
if (decision === "acceptForSession" || decision === "acceptAlways") {
const onceId = request.options.find((option) => option.kind === "allow_once")?.optionId.trim();
if (onceId) {
return onceId;
@@ -722,6 +728,7 @@ export function makeHermesAdapter(
turns: [],
lastPlanFingerprint: undefined,
activeTurnId: undefined,
activeTurnAnnounced: false,
promptsInFlight: 0,
stopped: false,
};
@@ -850,8 +857,12 @@ export function makeHermesAdapter(
// the same synchronous window: a concurrent sendTurn arriving during
// the awaited session configuration below reads it to merge into this
// turn instead of opening a duplicate.
const previousActiveTurnId = ctx.activeTurnId;
ctx.promptsInFlight += 1;
ctx.activeTurnId = turnId;
if (steeringTurnId === undefined) {
ctx.activeTurnAnnounced = false;
}
return yield* Effect.gen(function* () {
const turnModelSelection =
@@ -879,31 +890,6 @@ 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",
...(yield* makeEventStamp()),
provider: PROVIDER,
threadId: input.threadId,
turnId,
payload: model === undefined ? {} : { model },
});
}
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() });
@@ -953,6 +939,33 @@ 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),
});
// Announced only once both fallible preparation steps — prompt
// validation and session configuration — have passed, so a turn
// consumers can see is always a turn that can settle. Keyed on the
// session flag rather than "am I a steer" so a steer that overtook a
// not-yet-announced turn still announces it, exactly once.
if (!ctx.activeTurnAnnounced) {
ctx.activeTurnAnnounced = true;
yield* offerRuntimeEvent({
type: "turn.started",
...(yield* makeEventStamp()),
provider: PROVIDER,
threadId: input.threadId,
turnId,
payload: model === undefined ? {} : { model },
});
}
const result = yield* ctx.acp
.prompt({
prompt: promptParts,
@@ -1002,6 +1015,16 @@ export function makeHermesAdapter(
Effect.ensuring(
Effect.sync(() => {
ctx.promptsInFlight = Math.max(0, ctx.promptsInFlight - 1);
// Preparation failed before the turn was ever announced: release
// the claim so the next sendTurn opens a fresh turn instead of
// steering one consumers never saw.
if (
!ctx.activeTurnAnnounced &&
ctx.promptsInFlight === 0 &&
ctx.activeTurnId === turnId
) {
ctx.activeTurnId = previousActiveTurnId;
}
}),
),
);