Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 14 additions & 9 deletions src/clients/openai-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · medium
The condition typeof tool.function.strict === "boolean" does not protect against a null/undefined tool.function in case the earlier .filter logic changes or tool structure varies. Accessing tool.function.strict directly risks a TypeError if tool.function is missing.

Suggestion:

Suggested change
if (typeof tool.function.strict === "boolean") converted.strict = tool.function.strict;
if (tool.function && typeof tool.function.strict === "boolean") converted.strict = tool.function.strict;

return converted;
});
}

// Build Anthropic request
Expand Down
20 changes: 10 additions & 10 deletions src/clients/openrouter-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
}
};
Comment on lines +35 to 43

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · low
Similar to the previous file, tool.strict is accessed without ensuring tool itself is valid (though unlikely given the earlier checks). More importantly, the new fn object construction duplicates the schema fallback logic (type: 'object', properties: {}, required: []) already present in openai-format.js. Consider extracting this schema fallback into a shared utility for consistency.

// 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 };
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/orchestrator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } : {}),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · low
The spread operator with a conditional ternary for including strict adds unnecessary verbosity. A simpler approach would be to check the type once and assign directly, or use a helper function if this pattern repeats elsewhere in the codebase.

};
}
// Ensure input_schema has required 'type' field
Expand All @@ -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 } : {}),
};
});
}
Expand Down Expand Up @@ -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.
Expand Down
69 changes: 69 additions & 0 deletions test/tool-strict-preservation.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading