From dd0e99394309645d53dce3eb222db87739bef60d Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Wed, 11 Nov 2026 19:36:34 +0530 Subject: [PATCH] fix(tools): preserve the strict flag when converting tool definitions Three tool rebuilds dropped the `strict` flag, so a strict-schema tool sent by a client reached the upstream as a non-strict tool: - convertOpenAIToAnthropic (OpenAI Chat -> Anthropic) built tools from name/description/parameters only. - ensureAnthropicToolFormat (orchestrator) did the same when unwrapping OpenAI-shaped tools and when normalising Anthropic ones. - convertAnthropicToolsToOpenRouter (Anthropic -> OpenAI-format upstreams) never emitted function.strict. Each now carries a boolean strict through, and omits it when the client did not set one. Adds regression tests, including a full OpenAI Chat round trip, and registers them in test:unit. Refs #116 --- package.json | 2 +- src/clients/openai-format.js | 23 +++++---- src/clients/openrouter-utils.js | 20 ++++---- src/orchestrator/index.js | 4 ++ test/tool-strict-preservation.test.js | 69 +++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 20 deletions(-) create mode 100644 test/tool-strict-preservation.test.js diff --git a/package.json b/package.json index 96fc082..2f7fc21 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "start:supervised": "while true; do node index.js 2>&1 | npx pino-pretty --sync; code=$?; echo \"[supervisor] lynkr exited ($code) — restarting in 3s\"; sleep 3; done", "lint": "eslint src index.js", "test": "npm run test:unit && npm run test:performance", - "test:unit": "LYNKR_KNN_DIR=/tmp/lynkr-test-knn DATABRICKS_API_KEY=test-key DATABRICKS_API_BASE=http://test.com LOG_FILE_ENABLED=false node --test test/routing.test.js test/hybrid-routing-integration.test.js test/retry-logic.test.js test/sse-transformer.test.js test/passthrough-stream.test.js test/passthrough-mode.test.js test/openrouter-error-resilience.test.js test/format-conversion.test.js test/azure-openai-config.test.js test/azure-openai-format-conversion.test.js test/azure-openai-routing.test.js test/azure-openai-streaming.test.js test/azure-openai-error-resilience.test.js test/azure-openai-integration.test.js test/openai-integration.test.js test/atlas-integration.test.js test/cheaperinference-integration.test.js test/atmorouter-integration.test.js test/runcrate-integration.test.js test/openscale-integration.test.js test/toon-compression.test.js test/gcf-compression.test.js test/llamacpp-integration.test.js test/resilience.test.js test/telemetry-routing.test.js test/memory/store.test.js test/memory/surprise.test.js test/memory/extractor.test.js test/memory/search.test.js test/memory/retriever.test.js test/memory/distiller.test.js test/memory/distiller-freeze.test.js test/memory/wiki.test.js test/memory/skills-cache.test.js test/memory/tencentdb-launcher.test.js test/distill.test.js test/large-payload.test.js test/prompt-cache-injection.test.js test/risk-analyzer.test.js test/interaction-block.test.js test/preflight.test.js test/token-reduction.test.js test/session-affinity.test.js test/cache-state.test.js test/cache-switch-cost.test.js test/lens-recommendations.test.js test/model-registry-cost.test.js test/output-format-guard.test.js test/tier-fallback.test.js test/wrap.test.js test/init.test.js test/tool-call-response-metadata.test.js test/degradation.test.js test/routing-telemetry-columns.test.js test/sticky-routing.test.js test/knn-ambiguous-escalate.test.js test/deescalator.test.js test/client-profiles.test.js test/openai-ingress-tool-injection.test.js test/tool-result-compression-gate.test.js test/strip-internal-fields.test.js test/complexity-tool-subtraction.test.js test/bandit.test.js test/routing-propensity.test.js test/reward-pipeline.test.js test/knn-cold-start.test.js test/calibration.test.js test/feedback-loop.test.js test/session-fingerprint.test.js test/side-request-guards.test.js test/verifier.test.js test/intent-score.test.js test/difficulty-classifier.test.js test/classifier-setup.test.js test/usage-stats.test.js test/loop-guard.test.js test/moonshot-model-mapping.test.js test/baidu-model-mapping.test.js test/tenant-policy-ingress-parity.test.js test/decide.test.js test/embeddings-degradation.test.js test/health-probe.test.js test/stuck-detector.test.js test/onnx-embedder.test.js test/ope.test.js test/hierarchical-budget.test.js test/token-rate-limit.test.js test/otel-export.test.js test/mcp-broker.test.js test/compression-budget.test.js test/gpt-utils.test.js test/dedup-observe-only.test.js test/context-window-header.test.js test/token-budget-auto.test.js test/opencode-setup.test.js test/auth-mode-first-party.test.js test/harness-envelope.test.js test/task-ledger.test.js test/jev-router.test.js test/jev-routing.test.js test/force-patterns.test.js test/upstream-fidelity.test.js test/tool-schema-compression.test.js test/passthrough-route.test.js test/quota-ledger.test.js test/shortfall-semantic.test.js test/routing-decisions.test.js test/routing-corpus.test.js test/switch-gate-grounding.test.js", + "test:unit": "LYNKR_KNN_DIR=/tmp/lynkr-test-knn DATABRICKS_API_KEY=test-key DATABRICKS_API_BASE=http://test.com LOG_FILE_ENABLED=false node --test test/routing.test.js test/hybrid-routing-integration.test.js test/retry-logic.test.js test/sse-transformer.test.js test/passthrough-stream.test.js test/passthrough-mode.test.js test/openrouter-error-resilience.test.js test/format-conversion.test.js test/azure-openai-config.test.js test/azure-openai-format-conversion.test.js test/azure-openai-routing.test.js test/azure-openai-streaming.test.js test/azure-openai-error-resilience.test.js test/azure-openai-integration.test.js test/openai-integration.test.js test/atlas-integration.test.js test/cheaperinference-integration.test.js test/atmorouter-integration.test.js test/runcrate-integration.test.js test/openscale-integration.test.js test/toon-compression.test.js test/gcf-compression.test.js test/llamacpp-integration.test.js test/resilience.test.js test/telemetry-routing.test.js test/memory/store.test.js test/memory/surprise.test.js test/memory/extractor.test.js test/memory/search.test.js test/memory/retriever.test.js test/memory/distiller.test.js test/memory/distiller-freeze.test.js test/memory/wiki.test.js test/memory/skills-cache.test.js test/memory/tencentdb-launcher.test.js test/distill.test.js test/large-payload.test.js test/prompt-cache-injection.test.js test/risk-analyzer.test.js test/interaction-block.test.js test/preflight.test.js test/token-reduction.test.js test/session-affinity.test.js test/cache-state.test.js test/cache-switch-cost.test.js test/lens-recommendations.test.js test/model-registry-cost.test.js test/output-format-guard.test.js test/tier-fallback.test.js test/wrap.test.js test/init.test.js test/tool-call-response-metadata.test.js test/degradation.test.js test/routing-telemetry-columns.test.js test/sticky-routing.test.js test/knn-ambiguous-escalate.test.js test/deescalator.test.js test/client-profiles.test.js test/openai-ingress-tool-injection.test.js test/tool-result-compression-gate.test.js test/strip-internal-fields.test.js test/complexity-tool-subtraction.test.js test/bandit.test.js test/routing-propensity.test.js test/reward-pipeline.test.js test/knn-cold-start.test.js test/calibration.test.js test/feedback-loop.test.js test/session-fingerprint.test.js test/side-request-guards.test.js test/verifier.test.js test/intent-score.test.js test/difficulty-classifier.test.js test/classifier-setup.test.js test/usage-stats.test.js test/loop-guard.test.js test/moonshot-model-mapping.test.js test/baidu-model-mapping.test.js test/tenant-policy-ingress-parity.test.js test/decide.test.js test/embeddings-degradation.test.js test/health-probe.test.js test/stuck-detector.test.js test/onnx-embedder.test.js test/ope.test.js test/hierarchical-budget.test.js test/token-rate-limit.test.js test/otel-export.test.js test/mcp-broker.test.js test/compression-budget.test.js test/gpt-utils.test.js test/dedup-observe-only.test.js test/context-window-header.test.js test/token-budget-auto.test.js test/opencode-setup.test.js test/auth-mode-first-party.test.js test/harness-envelope.test.js test/task-ledger.test.js test/jev-router.test.js test/jev-routing.test.js test/force-patterns.test.js test/upstream-fidelity.test.js test/tool-schema-compression.test.js test/tool-strict-preservation.test.js test/passthrough-route.test.js test/quota-ledger.test.js test/shortfall-semantic.test.js test/routing-decisions.test.js test/routing-corpus.test.js test/switch-gate-grounding.test.js", "test:memory": "LYNKR_KNN_DIR=/tmp/lynkr-test-knn DATABRICKS_API_KEY=test-key DATABRICKS_API_BASE=http://test.com node --test test/memory/store.test.js test/memory/surprise.test.js test/memory/extractor.test.js test/memory/search.test.js test/memory/retriever.test.js test/memory/distiller.test.js test/memory/distiller-freeze.test.js test/memory/wiki.test.js test/memory/skills-cache.test.js test/memory/tencentdb-launcher.test.js", "test:new-features": "LYNKR_KNN_DIR=/tmp/lynkr-test-knn DATABRICKS_API_KEY=test-key DATABRICKS_API_BASE=http://test.com node --test test/passthrough-mode.test.js test/openrouter-error-resilience.test.js test/format-conversion.test.js", "test:performance": "LYNKR_KNN_DIR=/tmp/lynkr-test-knn DATABRICKS_API_KEY=test-key DATABRICKS_API_BASE=http://test.com node test/hybrid-routing-performance.test.js && DATABRICKS_API_KEY=test-key DATABRICKS_API_BASE=http://test.com node test/performance-tests.js", diff --git a/src/clients/openai-format.js b/src/clients/openai-format.js index d2e5ab9..29f9c2e 100644 --- a/src/clients/openai-format.js +++ b/src/clients/openai-format.js @@ -198,15 +198,20 @@ function convertOpenAIToAnthropic(openaiRequest) { if (tools && tools.length > 0) { anthropicTools = tools .filter(tool => tool && tool.function && tool.function.name) // Filter out invalid tools - .map(tool => ({ - name: tool.function.name, - description: tool.function.description || "", - input_schema: tool.function.parameters || { - type: "object", - properties: {}, - required: [] - } - })); + .map(tool => { + const converted = { + name: tool.function.name, + description: tool.function.description || "", + input_schema: tool.function.parameters || { + type: "object", + properties: {}, + required: [] + } + }; + // Keep the strict-schema flag so it survives the round trip. + if (typeof tool.function.strict === "boolean") converted.strict = tool.function.strict; + return converted; + }); } // Build Anthropic request diff --git a/src/clients/openrouter-utils.js b/src/clients/openrouter-utils.js index 161b8a4..5f934ba 100644 --- a/src/clients/openrouter-utils.js +++ b/src/clients/openrouter-utils.js @@ -32,18 +32,18 @@ function convertAnthropicToolsToOpenRouter(anthropicTools) { // provider before routing picked this one, and re-wrapping strips the // name ({type:"function", function:{name:undefined}}). if (tool?.type === "function" && tool.function) return tool; - return { - type: "function", - function: { - name: tool.name, - description: tool.description || "", - parameters: tool.input_schema || { - type: "object", - properties: {}, - required: [] - } + const fn = { + name: tool.name, + description: tool.description || "", + parameters: tool.input_schema || { + type: "object", + properties: {}, + required: [] } }; + // Keep the strict-schema flag so the upstream enforces the schema. + if (typeof tool.strict === "boolean") fn.strict = tool.strict; + return { type: "function", function: fn }; }); } diff --git a/src/orchestrator/index.js b/src/orchestrator/index.js index 0b4a821..cecf832 100644 --- a/src/orchestrator/index.js +++ b/src/orchestrator/index.js @@ -259,6 +259,7 @@ function ensureAnthropicToolFormat(tools) { name: tool.function.name, description: tool.function.description, input_schema: tool.function.parameters, + ...(typeof tool.function.strict === "boolean" ? { strict: tool.function.strict } : {}), }; } // Ensure input_schema has required 'type' field @@ -272,6 +273,7 @@ function ensureAnthropicToolFormat(tools) { name: tool.name || "unnamed_tool", description: tool.description || tool.name || "No description provided", input_schema, + ...(typeof tool.strict === "boolean" ? { strict: tool.strict } : {}), }; }); } @@ -3338,6 +3340,8 @@ function toolResultCompressionAllowed(payload) { module.exports = { processMessage, + // Exported for unit testing of tool-format normalisation. + ensureAnthropicToolFormat, // Exported for unit testing of the client-profile gate on tool-result compression. toolResultCompressionAllowed, // Exported for unit testing of sanitizePayload tool handling. diff --git a/test/tool-strict-preservation.test.js b/test/tool-strict-preservation.test.js new file mode 100644 index 0000000..a3a4092 --- /dev/null +++ b/test/tool-strict-preservation.test.js @@ -0,0 +1,69 @@ +const assert = require("assert"); +const { describe, it } = require("node:test"); + +process.env.MODEL_PROVIDER = process.env.MODEL_PROVIDER || "databricks"; +process.env.DATABRICKS_API_KEY = process.env.DATABRICKS_API_KEY || "test-key"; +process.env.DATABRICKS_API_BASE = process.env.DATABRICKS_API_BASE || "http://test.com"; + +const { convertAnthropicToolsToOpenRouter } = require("../src/clients/openrouter-utils"); +const { convertOpenAIToAnthropic } = require("../src/clients/openai-format"); +const { ensureAnthropicToolFormat } = require("../src/orchestrator"); + +const schema = { type: "object", properties: { a: { type: "string" } }, required: ["a"], additionalProperties: false }; + +// Regression tests for the tool `strict` flag being dropped by tool rebuilds (issue #116). +describe("tool strict flag preservation", () => { + it("convertAnthropicToolsToOpenRouter keeps strict true and false", () => { + const out = convertAnthropicToolsToOpenRouter([ + { name: "t1", description: "d", strict: true, input_schema: schema }, + { name: "t2", description: "d", strict: false, input_schema: schema }, + ]); + assert.strictEqual(out[0].function.strict, true); + assert.strictEqual(out[1].function.strict, false); + }); + + it("convertAnthropicToolsToOpenRouter omits strict when unset", () => { + const [tool] = convertAnthropicToolsToOpenRouter([{ name: "t", input_schema: schema }]); + assert.ok(!("strict" in tool.function)); + }); + + it("convertOpenAIToAnthropic keeps strict", () => { + const out = convertOpenAIToAnthropic({ + model: "m", + messages: [{ role: "user", content: "hi" }], + tools: [{ type: "function", function: { name: "t", strict: true, parameters: schema } }], + }); + assert.strictEqual(out.tools[0].strict, true); + assert.deepStrictEqual(out.tools[0].input_schema, schema); + }); + + it("convertOpenAIToAnthropic omits strict when unset", () => { + const out = convertOpenAIToAnthropic({ + model: "m", + messages: [{ role: "user", content: "hi" }], + tools: [{ type: "function", function: { name: "t", parameters: schema } }], + }); + assert.ok(!("strict" in out.tools[0])); + }); + + it("ensureAnthropicToolFormat keeps strict from both tool shapes", () => { + const out = ensureAnthropicToolFormat([ + { type: "function", function: { name: "t1", strict: true, parameters: schema } }, + { name: "t2", description: "d", strict: true, input_schema: schema }, + { name: "t3", description: "d", input_schema: schema }, + ]); + assert.strictEqual(out[0].strict, true); + assert.strictEqual(out[1].strict, true); + assert.ok(!("strict" in out[2])); + }); + + it("OpenAI Chat tool survives the full round trip to the OpenAI-format upstream", () => { + const anthropic = convertOpenAIToAnthropic({ + model: "m", + messages: [{ role: "user", content: "hi" }], + tools: [{ type: "function", function: { name: "t", strict: true, parameters: schema } }], + }); + const [forwarded] = convertAnthropicToolsToOpenRouter(ensureAnthropicToolFormat(anthropic.tools)); + assert.strictEqual(forwarded.function.strict, true); + }); +});