7c3407ac65
- Fixed `/dump` output (`session-dump-format.ts`) and the RPC `get_state` `dumpTools` payload (`rpc-mode.ts`) serializing Zod schema instances — leaking `def`/`shape` internals and stringified methods — by converting parameters through `zodToWireSchema` like providers receive. - Fixed context-usage token estimation stringifying the Zod `def` tree, which overcounted tool schema tokens in the status line. - Fixed tool-discovery indexing (`tool-index.ts`) returning empty `schemaKeys` for Zod tools, weakening BM25 ranking; keys are now recovered via wire conversion. - Fixed the extension inspector panel rendering "(no arguments)" for Zod tools by reading properties off the converted wire schema. - Added coverage in `tool-index.test.ts` plus new `context-usage.test.ts` and `session-dump-format.test.ts`, and a changelog entry.
28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
/**
|
|
* Contract: tool schema token estimation reflects the wire JSON Schema.
|
|
*
|
|
* Tools authored with Zod must be counted by the JSON Schema providers
|
|
* actually receive — not by stringifying the Zod instance's enumerable
|
|
* internals (`def` tree), which massively overcounts.
|
|
*/
|
|
import { describe, expect, it } from "bun:test";
|
|
import { zodToWireSchema } from "@oh-my-pi/pi-ai/utils/schema";
|
|
import { estimateToolSchemaTokens } from "@oh-my-pi/pi-coding-agent/modes/utils/context-usage";
|
|
import { z } from "zod/v4";
|
|
|
|
describe("estimateToolSchemaTokens", () => {
|
|
it("counts Zod tool schemas by their wire JSON Schema, not Zod internals", () => {
|
|
const parameters = z.object({
|
|
query: z.string().describe("search query"),
|
|
limit: z.number().optional(),
|
|
});
|
|
const zodEstimate = estimateToolSchemaTokens([
|
|
{ name: "web_search", description: "Searches the web.", parameters } as never,
|
|
]);
|
|
const wireEstimate = estimateToolSchemaTokens([
|
|
{ name: "web_search", description: "Searches the web.", parameters: zodToWireSchema(parameters) } as never,
|
|
]);
|
|
expect(zodEstimate).toBe(wireEstimate);
|
|
});
|
|
});
|