diff --git a/apps/server/src/provider/Layers/HermesAdapter.test.ts b/apps/server/src/provider/Layers/HermesAdapter.test.ts index 83559759d8..9fbeb37abb 100644 --- a/apps/server/src/provider/Layers/HermesAdapter.test.ts +++ b/apps/server/src/provider/Layers/HermesAdapter.test.ts @@ -192,6 +192,70 @@ it("resolves approval-required to an approval-shaped mode before implement", () assert.equal(modeId, "ask"); }); +// The exact session modes the live `hermes acp` CLI (v0.21) advertises. All +// three mention "ask", so approval selection cannot lean on substring order. +const liveHermesModes = [ + { id: "default", name: "Default", description: "Ask before edits." }, + { + id: "accept_edits", + name: "Accept Edits", + description: "Auto-allow workspace and /tmp edits; still asks for sensitive paths.", + }, + { + id: "dont_ask", + name: "Don't Ask", + description: "Auto-allow file edits for this session except sensitive paths.", + }, +] as const; + +it("resolves approval-required to the mode that still prompts", () => { + const modeId = resolveRequestedModeId({ + interactionMode: undefined, + runtimeMode: "approval-required", + modeState: { currentModeId: "default", availableModes: liveHermesModes }, + }); + assert.equal(modeId, "default"); +}); + +it("resolves approval-required independently of the order Hermes lists modes in", () => { + const modeId = resolveRequestedModeId({ + interactionMode: undefined, + runtimeMode: "approval-required", + modeState: { + currentModeId: "dont_ask", + availableModes: [liveHermesModes[2], liveHermesModes[1], liveHermesModes[0]], + }, + }); + assert.equal(modeId, "default"); +}); + +it("still resolves the implement mode when approvals are not required", () => { + const modeId = resolveRequestedModeId({ + interactionMode: undefined, + runtimeMode: "full-access", + modeState: { + currentModeId: "dont_ask", + availableModes: [liveHermesModes[2], liveHermesModes[1], liveHermesModes[0]], + }, + }); + assert.equal(modeId, "default"); +}); + +it("resolves approval-required to an asking mode that shares no alias with its id", () => { + const modeId = resolveRequestedModeId({ + interactionMode: undefined, + runtimeMode: "approval-required", + modeState: { + currentModeId: "dont_ask", + availableModes: [ + { id: "dont_ask", name: "Don't Ask", description: "Auto-allow." }, + { id: "confirm_edits", name: "Confirm Edits", description: "Ask before applying edits." }, + ], + }, + }); + assert.equal(modeId, "confirm_edits"); +}); + it("resolves no mode when the agent advertises none", () => { const modeId = resolveRequestedModeId({ interactionMode: undefined, diff --git a/apps/server/src/provider/Layers/HermesAdapter.ts b/apps/server/src/provider/Layers/HermesAdapter.ts index 84ae40cfd4..6ccb138e23 100644 --- a/apps/server/src/provider/Layers/HermesAdapter.ts +++ b/apps/server/src/provider/Layers/HermesAdapter.ts @@ -80,6 +80,25 @@ const HERMES_RESUME_VERSION = 1 as const; const ACP_PLAN_MODE_ALIASES = ["plan", "architect"]; const ACP_IMPLEMENT_MODE_ALIASES = ["code", "agent", "default", "chat", "implement"]; const ACP_APPROVAL_MODE_ALIASES = ["ask"]; +/** + * Normalized text fragments that mark a mode as one which acts without stopping + * to ask. Each entry is unambiguous on its own: bare "always" is not, because + * "always ask" and "always allow" mean opposite things, so only the allowing + * phrasings are listed. + */ +const ACP_PROMPT_SUPPRESSING_SIGNALS = [ + "dont ask", + "don t ask", + "do not ask", + "never ask", + "without asking", + "auto", + "accept", + "always allow", + "allow always", + "skip", + "bypass", +]; function encodeJsonStringForDiagnostics(input: unknown): string | undefined { const result = encodeUnknownJsonStringExit(input); @@ -189,9 +208,29 @@ function normalizeModeSearchText(mode: AcpSessionMode): string { .trim(); } +/** + * Whether a mode's own text says it acts without prompting first. Guards + * approval-intent matching: the live Hermes mode set is `default` + * ("Ask before edits."), `accept_edits` ("Auto-allow workspace and /tmp edits; + * still asks for sensitive paths.") and `dont_ask` ("Don't Ask"), so every one + * of them contains the "ask" alias. Plain substring matching therefore picked + * whichever auto-allowing mode Hermes happened to list first, which silently + * turns "approve every edit" into "approve nothing". + */ +function modeSuppressesPrompts(mode: AcpSessionMode): boolean { + const searchText = normalizeModeSearchText(mode); + return ACP_PROMPT_SUPPRESSING_SIGNALS.some((signal) => searchText.includes(signal)); +} + +/** + * Set `requireProactivePrompting` when the alias expresses "the user wants to be + * asked". It narrows only the substring pass, and only for that intent: an + * "Accept Edits" mode is a perfectly good implement-mode candidate. + */ function findModeByAliases( modes: ReadonlyArray, aliases: ReadonlyArray, + options?: { readonly requireProactivePrompting?: boolean }, ): AcpSessionMode | undefined { const normalizedAliases = aliases.map((alias) => alias.toLowerCase()); for (const alias of normalizedAliases) { @@ -204,8 +243,11 @@ function findModeByAliases( return exact; } } + const partialCandidates = options?.requireProactivePrompting + ? modes.filter((mode) => !modeSuppressesPrompts(mode)) + : modes; for (const alias of normalizedAliases) { - const partial = modes.find((mode) => normalizeModeSearchText(mode).includes(alias)); + const partial = partialCandidates.find((mode) => normalizeModeSearchText(mode).includes(alias)); if (partial) { return partial; } @@ -233,7 +275,9 @@ export function resolveRequestedModeId(input: { if (input.runtimeMode === "approval-required") { return ( - findModeByAliases(modeState.availableModes, ACP_APPROVAL_MODE_ALIASES)?.id ?? + findModeByAliases(modeState.availableModes, ACP_APPROVAL_MODE_ALIASES, { + requireProactivePrompting: true, + })?.id ?? findModeByAliases(modeState.availableModes, ACP_IMPLEMENT_MODE_ALIASES)?.id ?? modeState.availableModes.find((mode) => !isPlanMode(mode))?.id ?? modeState.currentModeId