diff --git a/src/agent/agent-contract.ts b/src/agent/agent-contract.ts index a889bd376..4dc4fa6e0 100644 --- a/src/agent/agent-contract.ts +++ b/src/agent/agent-contract.ts @@ -27,6 +27,7 @@ import type { AgentMetrics } from "../tracing/agent-metrics.js"; import type { StructuredLogger } from "../tracing/structured-logger.js"; import type { ProfileClippedEvent } from "./profile-clip-warning.js"; import type { HistoryTrimmedEvent } from "./history-trim-warning.js"; +import type { ToolDisabledEvent } from "./tool-disabled-notice.js"; import type { ReviewStallSignal } from "./review-stall.js"; /** See `AgentLoopDependencies.localEngineState`. */ @@ -925,6 +926,11 @@ export type AgentLoopEvent = | ProfileClippedEvent /** The packer left older history out of the prompt (ATO-295). */ | HistoryTrimmedEvent + /** + * A tool was left out of this turn's requests: the server refused its + * JSON schema (ATO-1417). Once per tool per turn. + */ + | ToolDisabledEvent | { type: "step_started"; stepIndex: number } | { type: "step_finished"; diff --git a/src/agent/agent-loop-tool-disabled.test.ts b/src/agent/agent-loop-tool-disabled.test.ts new file mode 100644 index 000000000..78c4de98d --- /dev/null +++ b/src/agent/agent-loop-tool-disabled.test.ts @@ -0,0 +1,138 @@ +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +import { afterEach, beforeEach, describe, expect, it } from "vitest"; + +import type { CompletionResult } from "../llm/llama-server-client.js"; +import { SlotManager } from "../llm/slot-manager.js"; +import type { + CapabilitiesSummary, + ToolDescriptor, +} from "../prompt/stable-prefix.js"; +import { createEmptySessionState } from "../session/session-state.js"; +import { buildDefaultToolRegistry } from "../tools/index.js"; + +import { AgentLoop } from "./agent-loop.js"; +import type { AgentLoopEvent, MemoryContextProvider } from "./agent-loop.js"; +import type { ToolDisabledEvent } from "./tool-disabled-notice.js"; + +/** + * ATO-1417, loop wiring. A completion whose request went out without a + * tool (`droppedTools`, the server refused its schema) becomes one + * `tool_disabled` event per tool per turn, however many completions of + * the turn report it. + */ + +const TOOLS: ToolDescriptor[] = [ + { + name: "finish", + summary: "Finish the session with a summary.", + argsSchema: '{"summary": string}', + }, +]; +const CAPS: CapabilitiesSummary = { + platform: "darwin", + arch: "arm64", + browserChannel: "chrome", + workingDir: "/work", + hasClipboard: true, + hasWmctrl: false, + hasNotifications: true, +}; +const NOOP_PROVIDER: MemoryContextProvider = { + buildMemoryContext: () => ({ recalled: [], index: [] }), +}; +const DROPPED = [ + { name: "notion.search", reason: "schema_rejected" as const }, +]; +const completion = (content: string, dropped = true): CompletionResult => ({ + content, + reasoningContent: "", + stop: true, + truncated: false, + cacheHitTokens: 0, + slotId: 0, + modelId: "mock", + timing: { promptMs: 1, predictedMs: 1, promptTokens: 10, predictedTokens: 5 }, + ...(dropped ? { droppedTools: DROPPED } : {}), +}); +const REPLY = JSON.stringify({ tool: "reply", args: { text: "ok" } }); + +describe("AgentLoop says when a tool was left out of the request", () => { + let workingDir: string; + beforeEach(() => { + workingDir = mkdtempSync(join(tmpdir(), "atomic-tool-disabled-")); + }); + afterEach(() => { + rmSync(workingDir, { recursive: true, force: true }); + }); + + const makeLoop = ( + events: AgentLoopEvent[], + complete: () => CompletionResult, + ): AgentLoop => + new AgentLoop({ + registry: buildDefaultToolRegistry(), + slotManager: new SlotManager(2), + grammar: 'root ::= "ok"', + llmComplete: async () => complete(), + toolDescriptors: TOOLS, + capabilities: CAPS, + skillCatalog: [], + memoryContextProvider: NOOP_PROVIDER, + onEvent: (event) => events.push(event), + }); + const disabled = (events: AgentLoopEvent[]): ToolDisabledEvent[] => + events.filter((e): e is ToolDisabledEvent => e.type === "tool_disabled"); + const completions = (events: AgentLoopEvent[]): number => + events.filter( + (e) => e.type === "llm_event" && e.event.type === "llm_completed", + ).length; + const turn = (loop: AgentLoop, id: string) => + loop.runTurn(createEmptySessionState({ id, workingDir }), { + userMessage: "hello", + maxSteps: 3, + signal: new AbortController().signal, + }); + + it("names the tool once, however many completions of the turn drop it", async () => { + const events: AgentLoopEvent[] = []; + // An unparseable first completion makes the turn ask again: two + // completions in one turn, both sent without the same tool. + const replies = [completion("{{garbage"), completion(REPLY)]; + const loop = makeLoop(events, () => replies.shift() ?? completion(REPLY)); + + await turn(loop, "s1"); + + expect(completions(events)).toBeGreaterThanOrEqual(2); + expect(disabled(events)).toEqual([ + { + type: "tool_disabled", + stepIndex: 0, + tool: "notion.search", + reason: "schema_rejected", + }, + ]); + }); + + it("names it again on the next turn", async () => { + const events: AgentLoopEvent[] = []; + const loop = makeLoop(events, () => completion(REPLY)); + + await turn(loop, "s2"); + await turn(loop, "s2"); + + expect(disabled(events)).toHaveLength(2); + }); + + it("says nothing when every tool went out", async () => { + const events: AgentLoopEvent[] = []; + const loop = makeLoop(events, () => completion(REPLY, false)); + + await turn(loop, "s3"); + + expect(completions(events)).toBeGreaterThan(0); + expect(disabled(events)).toEqual([]); + }); +}); diff --git a/src/agent/agent-loop.ts b/src/agent/agent-loop.ts index 14b5b7fb6..a5008a6f5 100644 --- a/src/agent/agent-loop.ts +++ b/src/agent/agent-loop.ts @@ -50,6 +50,7 @@ import { getConfig } from "../config/index.js"; import { ProfileClipWarnings, reportProfileClip } from "./profile-clip-warning.js"; import { HistoryTrimWarnings, reportHistoryTrim } from "./history-trim-warning.js"; +import { toolDisabledEvents } from "./tool-disabled-notice.js"; import { CompactPromptLatch } from "../prompt/compact-prompt.js"; /** @@ -148,6 +149,9 @@ export class AgentLoop { const { fusionOrchestratorTurn, toolRole, claimEvidence, linkEvidence, progressNotes, unknownTools } = policies; let { fusionState, reviewStall, replan } = policies; const turn = createTurnLoopState(); + // ATO-1417: tools a provider left out of this turn's requests (the + // server refused their schema), each said once per turn. + const announcedDroppedTools = new Set(); // ATO-486: a new message on a session whose plan is still open gets // one short reminder on the turn's first step, so the model carries // the checklist on or says it is dropped. Never on a plan that is @@ -574,6 +578,13 @@ export class AgentLoop { // spend ceiling exists for. if (event.type === "llm_completed") { recordStepSpend(turn, event.completion, contextProviderId, this.deps.priceCompletion); + for (const disabled of toolDisabledEvents( + event.completion.droppedTools, + announcedDroppedTools, + i, + )) { + this.deps.onEvent?.(disabled); + } } if (event.type === "prompt_built") { turn.lastPromptTokens = event.prompt.tokens.total; diff --git a/src/agent/tool-disabled-notice.test.ts b/src/agent/tool-disabled-notice.test.ts new file mode 100644 index 000000000..fe36db97c --- /dev/null +++ b/src/agent/tool-disabled-notice.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from "vitest"; + +import { toolDisabledEvents, toolDisabledNotice } from "./tool-disabled-notice.js"; + +describe("tool_disabled (ATO-1417)", () => { + it("says which tool the model cannot use and why, in one sentence", () => { + expect(toolDisabledNotice("notion.search")).toBe( + "Tool notion.search is disabled for this turn: the model server did not accept its schema.", + ); + }); + + it("one event per tool the turn has not announced yet", () => { + const announced = new Set(); + const dropped = [ + { name: "notion.search", reason: "schema_rejected" as const }, + { name: "records.lookup", reason: "schema_rejected" as const }, + ]; + expect(toolDisabledEvents(dropped, announced, 1)).toEqual([ + { type: "tool_disabled", stepIndex: 1, tool: "notion.search", reason: "schema_rejected" }, + { type: "tool_disabled", stepIndex: 1, tool: "records.lookup", reason: "schema_rejected" }, + ]); + // The next step sends the same tools and is refused the same way. + expect(toolDisabledEvents(dropped, announced, 2)).toEqual([]); + expect([...announced]).toEqual(["notion.search", "records.lookup"]); + }); + + it("nothing for a completion that dropped nothing", () => { + const announced = new Set(); + expect(toolDisabledEvents(undefined, announced, 0)).toEqual([]); + expect(toolDisabledEvents([], announced, 0)).toEqual([]); + expect(announced.size).toBe(0); + }); +}); diff --git a/src/agent/tool-disabled-notice.ts b/src/agent/tool-disabled-notice.ts new file mode 100644 index 000000000..4ead11362 --- /dev/null +++ b/src/agent/tool-disabled-notice.ts @@ -0,0 +1,52 @@ +import type { DroppedTool } from "../llm/provider/completion-types.js"; + +/** + * ATO-1417. The provider sent a request without one of the session's + * tools because the server refused that tool's JSON schema (ATO-1399: + * LM Studio and llama.cpp reject some MCP schemas outright), so the model + * cannot call it. Before this event the only trace was a log line, and + * the person saw a model that simply never used their tool. Rides the + * `AgentLoopEvent` union so the TUI and the HTTP stream can say it in the + * chat. + * + * Emitted once per tool per turn: every step of the turn sends the same + * tool list, and so is refused and resent the same way. + */ +export interface ToolDisabledEvent { + type: "tool_disabled"; + stepIndex: number; + /** The tool's own name, as the registry knows it. */ + tool: string; + /** `schema_rejected`: the server did not accept its JSON schema. */ + reason: DroppedTool["reason"]; +} + +/** The chat line for a `tool_disabled` event, shared by the TUI and the HTTP frame. */ +export function toolDisabledNotice(tool: string): string { + return `Tool ${tool} is disabled for this turn: the model server did not accept its schema.`; +} + +/** + * The `tool_disabled` events a completion calls for: one per dropped + * tool that this turn has not announced yet. `announced` is the turn's + * own set of tool names and is updated in place. + */ +export function toolDisabledEvents( + droppedTools: readonly DroppedTool[] | undefined, + announced: Set, + stepIndex: number, +): ToolDisabledEvent[] { + if (!droppedTools || droppedTools.length === 0) return []; + const events: ToolDisabledEvent[] = []; + for (const dropped of droppedTools) { + if (!dropped.name || announced.has(dropped.name)) continue; + announced.add(dropped.name); + events.push({ + type: "tool_disabled", + stepIndex, + tool: dropped.name, + reason: dropped.reason, + }); + } + return events; +} diff --git a/src/http/README.md b/src/http/README.md index afd6d3147..556ebdc2f 100644 --- a/src/http/README.md +++ b/src/http/README.md @@ -25,7 +25,7 @@ Read README.md and ../runtime/docs/lifecycle.md for session/stream/steering chan `npx vitest run src/http`; `npm run lint`; `npm run docs:check`. The production typecheck excludes tests; see [development checks](../../docs/development.md). -[Compaction routes](route-compaction.ts) expose `POST /api/sessions/{id}/compact` and `GET /api/sessions/{id}/compaction`. The POST waits for runtime completion and cancels only its operation on disconnect. See [the shared contract](../runtime/docs/compaction.md). With `x-atomic-extensions: 1`, a chat stream also carries `compaction_started`, `compaction_progress`, `compaction_completed`, `compaction_failed` and `history_trimmed` frames ([mapping](stream-maintenance-frames.ts)): counts and statuses only. It also carries `usage_updated` (ATO-569): the session's spend so far, priced / unpriced / local / plan call counts, and the account's rate-limit windows (used percent, remaining, reset time); the same snapshot is persisted as `sessionUsage` on `GET /api/sessions/{id}`. +[Compaction routes](route-compaction.ts) expose `POST /api/sessions/{id}/compact` and `GET /api/sessions/{id}/compaction`. The POST waits for runtime completion and cancels only its operation on disconnect. See [the shared contract](../runtime/docs/compaction.md). With `x-atomic-extensions: 1`, a chat stream also carries `compaction_started`, `compaction_progress`, `compaction_completed`, `compaction_failed` and `history_trimmed` frames ([mapping](stream-maintenance-frames.ts)): counts and statuses only. It also carries `usage_updated` (ATO-569): the session's spend so far, priced / unpriced / local / plan call counts, and the account's rate-limit windows (used percent, remaining, reset time); the same snapshot is persisted as `sessionUsage` on `GET /api/sessions/{id}`. A `tool_disabled` frame (ATO-1417) says a tool was left out of the turn's requests because the server refused its JSON schema: `tool`, `reason` (`schema_rejected`), `step_index` and the chat sentence in `notice`, once per tool per turn. [Turn phases](stream-turn-phase.ts) (ATO-1027): with `x-atomic-extensions: 1`, a chat stream carries `event: turn_phase` on every change of what the turn is doing, `{ object: "atomic.turn_phase", session_id, phase, step_index?, tool?, phase_elapsed_ms, turn_elapsed_ms, progress? }`. `phase` is `starting`, `loading_model` (the local server answered "Loading model"), `waiting_provider`, `processing_prompt` (`progress: { processed, total, cached, fraction }` when llama-server reports prompt progress), `thinking`, `generating`, `running_tool` (`tool` names the call) or `compacting`. A host counts the seconds itself from `phase_elapsed_ms`. Once per turn, when a local model has produced nothing 45 s after the turn began, `event: slow_first_token` carries `{ object: "atomic.slow_first_token", session_id, waited_ms, phase?, model_id?, lighter_model?: { id, name, file_size_gb, downloaded }, cloud_available }` ([hint](slow-first-token-hint.ts)): what the host may offer instead. Nothing is switched. diff --git a/src/http/openai-chat-completions.ts b/src/http/openai-chat-completions.ts index cab4d5a99..f8f5560d4 100644 --- a/src/http/openai-chat-completions.ts +++ b/src/http/openai-chat-completions.ts @@ -40,6 +40,7 @@ import { } from "./request-context.js"; import { deriveChatSessionId } from "./openai-session-id.js"; import { maintenanceFrame } from "./stream-maintenance-frames.js"; +import { toolDisabledNotice } from "../agent/tool-disabled-notice.js"; import { describeToolCall } from "../tools/describe-tool-call.js"; import { createTurnPhaseTracker } from "./stream-turn-phase.js"; import { claimTurnProgress } from "../agent/step/turn-progress-opt-in.js"; @@ -446,6 +447,8 @@ async function handleStream( * (extensions opt-in only, ATO-486) * - compaction lifecycle and `history_trimmed` → named frames of the * same names (extensions opt-in only, `stream-maintenance-frames.ts`) + * - `tool_disabled` → `event: tool_disabled` with the chat sentence + * (extensions opt-in only, ATO-1417) * - `step_error` / `loop_failed` → `emitStreamError` (shape depends on * whether extensions are opted in) * @@ -763,6 +766,24 @@ export function buildStreamEventHook( } return; } + /* ATO-1417: the server refused a tool's JSON schema and the provider + resent the request without it, so the model cannot call that tool + this turn. One plain sentence (`notice`) for the chat, once per + tool per turn. Extensions-only: an older host never asked for named + frames, and one that did ignores a frame name it does not know. */ + if (event.type === "tool_disabled") { + if (env.request.extensionsEnabled) { + sse.writeEvent("tool_disabled", { + object: "atomic.tool_disabled", + session_id: env.session.id, + step_index: event.stepIndex, + tool: event.tool, + reason: event.reason, + notice: toolDisabledNotice(event.tool), + }); + } + return; + } if (event.type === "provider_recovered") { if (env.request.extensionsEnabled) { sse.writeEvent("provider_recovered", { diff --git a/src/http/tool-disabled-stream.test.ts b/src/http/tool-disabled-stream.test.ts new file mode 100644 index 000000000..ff8c45b5b --- /dev/null +++ b/src/http/tool-disabled-stream.test.ts @@ -0,0 +1,61 @@ +import { describe, expect, it } from "vitest"; + +import { buildStreamEventHook } from "./openai-chat-completions.js"; + +/** + * ATO-1417: the server refused a tool's schema and the request went out + * without it. A host that opted into extensions gets one named frame + * with the chat sentence; the plain OpenAI stream stays as it was. + */ +describe("tool_disabled over SSE", () => { + const makeSse = () => { + const written: Array<{ name: string | null; payload: unknown }> = []; + return { + written, + writer: { + closed: false, + writeEvent(name: string | null, payload: unknown) { + written.push({ name, payload }); + }, + }, + }; + }; + const env = (extensionsEnabled: boolean) => + ({ + completionId: "cmpl-1", + created: 0, + session: { id: "sess-1" }, + request: { model: "atomic-agent", extensionsEnabled }, + }) as never; + const event = { + type: "tool_disabled" as const, + stepIndex: 0, + tool: "notion.search", + reason: "schema_rejected" as const, + }; + + it("names the tool and carries the sentence for the chat", () => { + const sse = makeSse(); + buildStreamEventHook(sse.writer as never, env(true))(event as never); + expect(sse.written).toEqual([ + { + name: "tool_disabled", + payload: { + object: "atomic.tool_disabled", + session_id: "sess-1", + step_index: 0, + tool: "notion.search", + reason: "schema_rejected", + notice: + "Tool notion.search is disabled for this turn: the model server did not accept its schema.", + }, + }, + ]); + }); + + it("stays off the plain OpenAI stream", () => { + const plain = makeSse(); + buildStreamEventHook(plain.writer as never, env(false))(event as never); + expect(plain.written).toEqual([]); + }); +}); diff --git a/src/llm/provider/completion-types.ts b/src/llm/provider/completion-types.ts index ba21a6f4e..2308515e0 100644 --- a/src/llm/provider/completion-types.ts +++ b/src/llm/provider/completion-types.ts @@ -320,6 +320,23 @@ export interface CompletionResult { * can be looked up at the provider. */ generationId?: string; + /** + * Tools the provider left out of the request that produced this + * completion, in the order it dropped them: the server refused the + * JSON schema of each one, so the request was sent again without it + * (ATO-1399). The model never saw them. The agent loop turns each into + * a `tool_disabled` event so the person is told (ATO-1417). Absent + * when every tool the caller sent went out. + */ + droppedTools?: readonly DroppedTool[]; +} + +/** One tool a provider left out of a request (`CompletionResult.droppedTools`). */ +export interface DroppedTool { + /** The tool's own name, as the registry knows it (not its wire name). */ + name: string; + /** `schema_rejected`: the server did not accept the tool's JSON schema. */ + reason: "schema_rejected"; } export interface OpenAiToolCall { diff --git a/src/llm/provider/openai/openai-provider.ts b/src/llm/provider/openai/openai-provider.ts index 0a7ea4602..6268df162 100644 --- a/src/llm/provider/openai/openai-provider.ts +++ b/src/llm/provider/openai/openai-provider.ts @@ -10,6 +10,7 @@ import { import type { CompletionRequest, CompletionResult, + DroppedTool, StreamChunk, StreamFinalResult, } from "../completion-types.js"; @@ -286,7 +287,10 @@ export class OpenAiProvider implements LlmProvider { const reduced = this.withoutRejectedTool(request, err); if (!reduced) throw err; try { - return await this.completeOnce(reduced); + return withDroppedTool( + await this.completeOnce(reduced.request), + reduced.dropped, + ); } catch (retryErr) { if (request.signal?.aborted) throw retryErr; // The schema refusal is what the user needs to read, not @@ -448,14 +452,17 @@ export class OpenAiProvider implements LlmProvider { /** * The request without the one tool a schema error names - * (`tool-schema-rejection.ts`), or null when the error is something - * else or names no single tool. The warning says which tool went and - * why: the turn carries on without it rather than failing outright. + * (`tool-schema-rejection.ts`), and that tool, or null when the error + * is something else or names no single tool. The warning says which + * tool went and why: the turn carries on without it rather than + * failing outright. The dropped tool rides the completion + * (`CompletionResult.droppedTools`) so the chat can say it too + * (ATO-1417). */ private withoutRejectedTool( request: CompletionRequest, err: unknown, - ): CompletionRequest | null { + ): { request: CompletionRequest; dropped: DroppedTool } | null { if (request.signal?.aborted) return null; if (!(err instanceof OpenAiHttpError) || err.status === null) return null; const tools = request.tools; @@ -481,8 +488,11 @@ export class OpenAiProvider implements LlmProvider { }, ); return { - ...request, - tools: tools.filter((_, index) => index !== culprit.index), + request: { + ...request, + tools: tools.filter((_, index) => index !== culprit.index), + }, + dropped: { name: tool, reason: "schema_rejected" }, }; } @@ -533,7 +543,7 @@ export class OpenAiProvider implements LlmProvider { // Whether the one resend without a rejected tool has been spent, and // the schema refusal that spent it: if the resend fails before any // output, that refusal is the error the caller gets. - let toolDropped = false; + let toolDropped: DroppedTool | undefined; let schemaError: unknown; // One completion, one budget. Passed into every open so a reopen // draws from the same pot the opens do — the alternative is two @@ -673,9 +683,9 @@ export class OpenAiProvider implements LlmProvider { if (!committed && !toolDropped) { const reduced = this.withoutRejectedTool(activeRequest, err); if (reduced) { - toolDropped = true; + toolDropped = reduced.dropped; schemaError = err; - activeRequest = reduced; + activeRequest = reduced.request; body = this.buildBody(activeRequest, true, shape); continue; } @@ -774,7 +784,7 @@ export class OpenAiProvider implements LlmProvider { request, this.taggedToolOptions(), ); - return this.withRateLimits(withSentMaxTokens( + const safe = withSentMaxTokens( applyToolCallTerminationSafety( adaptedFinal, // A stream the consumer ended itself has no provider terminal @@ -785,7 +795,10 @@ export class OpenAiProvider implements LlmProvider { streamFinal?.earlyStop !== undefined, ), sentBody, - )); + ); + return this.withRateLimits( + toolDropped ? withDroppedTool(safe, toolDropped) : safe, + ); } /** @@ -1012,6 +1025,19 @@ function withSentMaxTokens( return { ...result, sentMaxTokens: cap }; } +/** + * Record a tool the request went out without (ATO-1417). Appended after + * any the completion already names, once per tool name. + */ +function withDroppedTool( + result: CompletionResult, + dropped: DroppedTool, +): CompletionResult { + const already = result.droppedTools ?? []; + if (already.some((tool) => tool.name === dropped.name)) return result; + return { ...result, droppedTools: [...already, dropped] }; +} + /** * The error a turn the user cancelled should fail with. * diff --git a/src/llm/provider/openai/openai-tool-schema-retry.test.ts b/src/llm/provider/openai/openai-tool-schema-retry.test.ts index bdd544771..e7ad3165f 100644 --- a/src/llm/provider/openai/openai-tool-schema-retry.test.ts +++ b/src/llm/provider/openai/openai-tool-schema-retry.test.ts @@ -132,6 +132,11 @@ describe("OpenAiProvider — drop a tool the server cannot use (ATO-1399)", () = tool: "plugin.records.lookup", status: 500, }); + // ATO-1417: the completion names the tool it went out without, by + // its registry name, so the chat can say so. + expect(result.droppedTools).toEqual([ + { name: "plugin.records.lookup", reason: "schema_rejected" }, + ]); }); it("resends a streamed completion once without the named tool", async () => { @@ -146,6 +151,26 @@ describe("OpenAiProvider — drop a tool the server cannot use (ATO-1399)", () = expect(result.content).toBe("ok"); expect(fetchImpl).toHaveBeenCalledTimes(2); expect(names(bodies[1]!)).toEqual(["os__fs__read"]); + expect(result.droppedTools).toEqual([ + { name: "plugin.records.lookup", reason: "schema_rejected" }, + ]); + }); + + it("names no dropped tool when every tool went out (ATO-1417)", async () => { + const unary = setup([okUnary]); + const plain = await unary.provider.complete({ + prompt: "hi", + tools: [GOOD_TOOL, BAD_TOOL], + }); + expect(plain.droppedTools).toBeUndefined(); + const streamed = setup([okStream]); + const live = await drain( + streamed.provider.completeStream({ + prompt: "hi", + tools: [GOOD_TOOL, BAD_TOOL], + }), + ); + expect(live.droppedTools).toBeUndefined(); }); it("surfaces the original error when no single tool matches", async () => { diff --git a/src/tui/agent-event-reducer.test.ts b/src/tui/agent-event-reducer.test.ts index f11903a01..7533fb7b8 100644 --- a/src/tui/agent-event-reducer.test.ts +++ b/src/tui/agent-event-reducer.test.ts @@ -1763,6 +1763,32 @@ describe("history trimmed (ATO-295)", () => { }); }); +describe("tool disabled (ATO-1417)", () => { + it("tells the chat which tool the model cannot use and why", () => { + const initial = createInitialTuiState(fakeSession({ sessionId: "s1" })); + const next = reduceTuiState(initial, { + type: "agent_event", + sessionId: "s1", + event: { type: "tool_disabled", stepIndex: 0, tool: "notion.search", reason: "schema_rejected" }, + }); + const last = next.messages.at(-1); + expect(last?.role).toBe("system"); + expect(last?.text).toBe( + "Tool notion.search is disabled for this turn: the model server did not accept its schema.", + ); + }); + + it("does not paint a background session's notice into the chat on screen", () => { + const initial = createInitialTuiState(fakeSession({ sessionId: "s1" })); + const next = reduceTuiState(initial, { + type: "agent_event", + sessionId: "s2", + event: { type: "tool_disabled", stepIndex: 0, tool: "notion.search", reason: "schema_rejected" }, + }); + expect(next.messages).toEqual(initial.messages); + }); +}); + describe("empty-completion recovery", () => { const recovered = (over: Record = {}): TuiAction => ({ type: "agent_event", diff --git a/src/tui/agent-event-reducer.ts b/src/tui/agent-event-reducer.ts index 24f160f9e..40bd04dbf 100644 --- a/src/tui/agent-event-reducer.ts +++ b/src/tui/agent-event-reducer.ts @@ -1,5 +1,6 @@ import type { AgentLoopEvent } from "../agent/agent-contract.js"; import { HISTORY_TRIMMED_NOTICE } from "../agent/history-trim-warning.js"; +import { toolDisabledNotice } from "../agent/tool-disabled-notice.js"; import { formatUsageReport } from "../session/usage-meter.js"; import { describeFailedAttempts, falloverSentence } from "../llm/fallback/index.js"; import { @@ -1069,6 +1070,11 @@ function reduceAgentEvent(state: TuiState, event: AgentLoopEvent): TuiState { // ATO-295: the packer left older history out of the prompt. The // loop says so once per cut, so this cannot fill the transcript. return appendChatMessage(state, { role: "system", text: HISTORY_TRIMMED_NOTICE }); + case "tool_disabled": + // ATO-1417: the server refused this tool's schema and the provider + // resent the request without it, so the model cannot call it. The + // loop says so once per tool per turn. + return appendChatMessage(state, { role: "system", text: toolDisabledNotice(event.tool) }); case "profile_clipped": { // Issue #407: the clip used to show only as a `[truncated]` inside // a prompt nobody reads. The loop fires this once per session, and