repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

fix(klaus): name the tool behind an input-schema rejection

c3312988d77deac36dcc76fb4db3c8a91e8e4abd

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

The API rejects an invalid tool schema by position, and Claude Code cannot pre-filter those tools because Klaus disables feature-flag fetching with CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC.
Klaus knows the order it sent, so it resolves tools.N.custom.input_schema to the Pi tool name in both the result-error and transport-error paths.

Changed files (4)

CHANGELOG.md

Modified
5 5 - Expose Klaus tools to Claude under their Pi names instead of the `mcp__klaus__` prefix, while still accepting prefixed names from resumed sessions and older runtimes
6 6 - Stop Klaus from replacing large Pi tool results with a `<persisted-output>` pointer into a deleted temp directory, and isolate the Klaus child from ambient Claude Code context, deferred tool loading, tool backgrounding, and non-streaming retries
7 7 - Capture Klaus tool identities from Claude's `PreToolUse` hook so tool calls keep working when a permission rule answers them without asking Klaus, and truncate tool results at Claude's result-size ceiling instead of losing them to a persisted file
8 +- Name the Klaus tool behind an Anthropic input-schema rejection instead of leaving the API's positional `tools.N` reference unexplained
8 9 - Reuse the running Claude child for the next turn in a Klaus session instead of spawning one per turn, swap its tool list in place when Pi changes tools, and drop it after a two-minute idle lease or any lineage change, saving about 1.7 seconds per follow-up turn
9 10 - Run every Klaus query in streaming input mode, interrupt the child instead of killing it so an abandoned turn no longer spends an extra request, flush mirrored transcripts eagerly, and surface child API retries, self-decided permission denials, warnings, and worker shutdowns to the human
10 11 - Add Bak for asynchronous S3-compatible session backup and conflict-safe restore, with host initialization, manual backup, refresh, and status commands, durable retries, safe partial-failure handling, and no filesystem or network work during Pi startup

extensions/klaus/__e2e__/provider-scenarios.test.ts

Modified
1647 1647 expect(childSessionId(requests[0])).not.toBe(childSessionId(requests[1]));
1648 1648 }, 120_000);
1650 + it("names the tool behind an invalid input schema rejection", async () => {
1651 + const server = await useServer((_request, _index, response) => {
1652 + response.writeHead(400, { "content-type": "application/json" });
1653 + response.end(
1654 + JSON.stringify({
1655 + type: "error",
1656 + error: {
1657 + type: "invalid_request_error",
1658 + message: "tools.1.custom.input_schema: JSON schema is invalid",
1659 + },
1660 + }),
1661 + );
1662 + });
1664 + const result = await directQuery(
1665 + request({
1666 + tools: [
1668 + name: "sound_tool",
1669 + description: "Fine.",
1670 + inputSchema: { type: "object" },
1671 + },
1673 + name: "broken_tool",
1674 + description: "Broken.",
1675 + inputSchema: { type: "object" },
1676 + },
1677 + ],
1678 + }),
1679 + );
1681 + expect(result.error?.message).toContain('Klaus tool "broken_tool"');
1682 + expect(result.error?.message).toContain("input schema");
1683 + expect(server.messageRequests().length).toBeGreaterThanOrEqual(1);
1684 + }, 120_000);
1650 1686 it("surfaces child API retries as notices", async () => {
1651 1687 const server = await useServer((_request, index, response) => {
1652 1688 if (index <= 2) {

extensions/klaus/src/agent-sdk.ts

Modified
7 7 type ThinkingConfig,
8 8 } from "@anthropic-ai/claude-agent-sdk";
9 9 import { dbg } from "./debug.js";
10 -import type { KlausQueryCallbacks, KlausRequest } from "./protocol.js";
10 +import type {
11 + KlausQueryCallbacks,
12 + KlausRequest,
13 + KlausTool,
14 +} from "./protocol.js";
11 15 import { CLAUDE_VERSION, SDK_VERSION } from "./protocol.js";
12 16 import { createChildRuntime } from "./runtime/index.js";
13 17 import { TOOL_ID_FIELD, ToolBridge } from "./tool-bridge.js";
152 156 return secret ? text.replaceAll(secret, "<redacted>") : text;
155 -function normalizeProviderError(text: string): string {
156 - return /context(?:_| )length|prompt is too long|too many tokens/i.test(text)
157 - ? `context_length_exceeded: ${text}`
158 - : text;
159 +/** The API rejects an invalid tool schema by position, and Claude Code cannot
160 + * pre-filter those tools because Klaus disables feature-flag fetching. Klaus
161 + * knows the order it sent, so it names the tool instead. */
162 +function nameToolSchemaError(text: string, tools: KlausTool[]): string {
163 + const match = /tools\.(\d+)\.custom\.input_schema/.exec(text);
164 + if (!match) return text;
165 + const position = Number(match[1]);
166 + const tool = Number.isSafeInteger(position) ? tools[position] : undefined;
167 + dbg?.("sdk.toolSchemaError", { position, name: tool?.name });
168 + return tool
169 + ? `Klaus tool "${tool.name}" has an input schema the Anthropic API rejects: ${text}`
170 + : `A Klaus tool at position ${position} has an input schema the Anthropic API rejects: ${text}`;
173 +function normalizeProviderError(text: string, tools: KlausTool[]): string {
174 + const named = nameToolSchemaError(text, tools);
175 + return /context(?:_| )length|prompt is too long|too many tokens/i.test(named)
176 + ? `context_length_exceeded: ${named}`
177 + : named;
161 180 function isOutputLimit(text: string): boolean {
486 505 blocks,
487 506 active,
488 507 messageState,
508 + bridge.activeTools(),
489 509 () => {
490 510 if (closed) return;
491 511 closed = true;
512 532 const detail = redact(stderr.trim(), oauthToken);
513 533 const message = redact(errorText(error), oauthToken);
514 534 dbg?.("sdk.pump.error", { message, detail });
515 - active.onError(new Error(detail ? `${message}: ${detail}` : message));
535 + active.onError(
536 + new Error(
537 + nameToolSchemaError(
538 + detail ? `${message}: ${detail}` : message,
539 + bridge.activeTools(),
540 + ),
541 + ),
542 + );
517 544 } finally {
518 545 dbg?.("sdk.pump.finally", { closed });
572 599 blocks: Map<number, BlockState>,
573 600 callbacks: KlausQueryCallbacks,
574 601 state: MessageState,
602 + tools: KlausTool[],
575 603 stopMaxRecovery: () => void,
576 604 ): Promise<boolean> {
577 605 dbg?.("sdk.handleMessage", {
684 712 new Error(
685 713 normalizeProviderError(
686 714 detail || `Claude ended with ${message.subtype}.`,
715 + tools,

extensions/klaus/src/tool-bridge.ts

Modified
189 189 this.permissions.push({ id, name: originalName(name), input });
192 + /** The tool list in the exact order Claude receives it, which is the order the
193 + * API reports a rejected tool schema by position. */
194 + activeTools(): KlausTool[] {
195 + return [...this.tools.values()];
192 198 toolNames(): string[] {
193 199 const names = [...this.tools.keys()];
194 200 dbg?.("bridge.toolNames", { names });