Lynkr icon indicating copy to clipboard operation
Lynkr copied to clipboard

Tool schema compression throws on a root-level `description`, silently disabling compression for the whole request

Open lhilton opened this issue 1 month ago • 0 comments

Environment

Lynkr 9.14.17 (also reproduces on main @ 1243a46)
MODEL_PROVIDER openrouter (z-ai/glm-5.3-flash) — the failure is provider-independent, it happens before any upstream call
TOOL_DESCRIPTIONS minimal (the documented default in .env.example)
SYSTEM_PROMPT_MODE dynamic (default)
Node 22.23.3 and 24.21.0 — identical behaviour
OS macOS 26.6.2, arm64 (also reproduced inside this project's own Docker image)

Steps to reproduce

Minimal — no server, no provider, no credentials required:

const { compressToolDescriptions } = require('./src/prompts/system');

compressToolDescriptions([{
  type: 'function',
  function: {
    name: 't',
    parameters: {
      type: 'object',
      description: 'root-level description',       // <-- the trigger
      properties: { command: { type: 'string' } }
    }
  }
}], 'minimal');

Against the released image:

$ docker run --rm --entrypoint node lynkr:latest -e '<snippet above>'
THREW: TypeError: Cannot read properties of null (reading 'toLowerCase')
at:    at isObviousFromName (/app/src/prompts/system.js:160:32)

End-to-end: POST /v1/chat/completions with a single tool whose parameters object carries a top-level description. No configuration change is needed — minimal is the default. Setting TOOL_DESCRIPTIONS=normal avoids it.

Narrowing: delete the root-level description, or move it down a level onto a property, and the crash disappears. Only the schema root triggers it.

Expected vs actual

Expected — minimal mode compresses description text and the request proceeds, exactly as test/tool-schema-compression.test.js already asserts for nested descriptions.

Actual — a TypeError, caught at src/orchestrator/index.js:1712 and downgraded to a warning. The entire compression pass is abandoned for that request, and since optimizeSystemPrompt() sits in the same try block it is skipped as well. The request still returns 200, so the only symptoms are a warn line and a larger-than-necessary upstream payload.

Error messages and logs

{"level":40,"err":{"type":"TypeError","message":"Cannot read properties of null (reading 'toLowerCase')",
 "stack":"TypeError: Cannot read properties of null (reading 'toLowerCase')
    at isObviousFromName (/app/src/prompts/system.js:160:32)
    at compressSchemaDescriptions (/app/src/prompts/system.js:115:14)
    at /app/src/prompts/system.js:97:26
    at Array.map (<anonymous>)
    at Object.compressToolDescriptions (/app/src/prompts/system.js:88:16)
    at runAgentLoop (/app/src/orchestrator/index.js:1669:45)"},
 "msg":"System prompt optimization failed, continuing with original"}

Root cause

compressSchemaDescriptions() is invoked with key = null for the schema root:

// src/prompts/system.js:97
out.input_schema = compressSchemaDescriptions(out.input_schema, null);

The first root-level description forwards that null into the helper:

// src/prompts/system.js:115
if (k === 'description' && typeof v === 'string') {
  if (!isObviousFromName(key)) out[k] = compressText(v, 30);

and the helper dereferences it unguarded:

// src/prompts/system.js:160
return obvious.includes(name.toLowerCase());

A schema root has no property name, so null is a legitimate input that the helper's contract never accounted for.

Lineage: introduced by #117 (abf042c, merged 2026-09-22) — the PR that rewrote compression to fix #116 and added test/tool-schema-compression.test.js. Before that rewrite, isObviousFromName() was only ever called with a real property name (old line 55), so the unguarded body was unreachable with null.

Impact

  • Hits the default configuration (TOOL_DESCRIPTIONS=minimal).
  • The throw happens inside a .map() over the tool array, so one offending tool disables compression for every tool in the request.
  • Measured on a 10-tool payload using usage.prompt_tokens reported by the upstream: 10056 → 4792 (−52.4%) once the crash is fixed. With only one of ten tools affected it was 9498 → 4594 (−51.6%).
  • On the deployment this was found on, 29 of 126 completed requests (23%) logged the warning.
  • It is invisible to monitoring: /metrics/tool-compression and the savings_events telemetry both cover tool-result compression, not tool-schema compression, so nothing surfaces the skip.

Proposed fix

Treat a non-string key as "not obvious" — a schema root has no name to judge against:

function isObviousFromName(name) {
  if (typeof name !== 'string') return false;
  ...

Regression tests and a PR to follow.

lhilton avatar Oct 08 '26 17:10 lhilton