Skip to content

fix(knowledge): omit explicit undefined fields to satisfy DSH lossless JSON check - #90

Open
DancingLi wants to merge 1 commit into
Soren-ABT:mainfrom
DancingLi:fix/lossless-json-undefined-fields
Open

DancingLi wants to merge 1 commit into
Soren-ABT:mainfrom
DancingLi:fix/lossless-json-undefined-fields

Conversation

@DancingLi

Copy link
Copy Markdown

Summary

Fixes Error: tool "knowledge_get_document" returned invalid output: value is not lossless JSON (and the same error in knowledge_read_evidence) that occurs immediately after importing or reindexing documents parsed by the built-in text processor (e.g. .md, .txt, .xlsx, .json).

Root Cause

  1. ArtifactRepository.saveRevision() (src/knowledge/artifacts.ts) unconditionally assigned optional properties (processorVersion, pageCount, processedPages) onto DocumentProcessingInfo. When parsedTextDocument() omits those optional fields, the in-memory doc.processing object retains three own properties with value undefined.
  2. Related sites could also attach explicit undefined own properties in memory:
    • composeDocumentEvidence() (src/knowledge/evidence.ts): processing: doc.processing
    • KnowledgeService.reindexDocument() (src/knowledge/index.ts): processing: rebuilt.processed?.processing
    • normalizeMineruArchive() (src/knowledge/parsed-document.ts): bbox: sourceBox(...) and processorVersion: version
  3. toolOutput() (src/tool-knowledge/tool-schemas.ts) cloned values with structuredClone(), which preserves own undefined properties (unlike JSON.stringify). Because DSH's tool runtime validates lossless JSON strictly, knowledge_get_document (both page and context-window modes) and knowledge_read_evidence failed with INVALID_TOOL_OUTPUT until a host restart reloaded knowledge.json from disk.

Changes

  • src/knowledge/artifacts.ts: Only include processorVersion, pageCount, and processedPages on DocumentProcessingInfo when defined.
  • src/knowledge/evidence.ts: Only include processing on DocumentEvidence when defined.
  • src/knowledge/index.ts: Strip stale processing from the reindexDocument candidate and only attach processing when defined.
  • src/knowledge/parsed-document.ts: Only attach bbox and processorVersion when defined in normalizeMineruArchive().
  • src/tool-knowledge/tool-schemas.ts: Strip own undefined keys recursively in toolOutput() after structuredClone() as defense-in-depth.
  • tests/tool-contract.spec.ts: Added end-to-end regression test through the real @deepseek-ai/dsh-tools runtime covering builtin file import, knowledge_get_document (page & context-window modes), knowledge_read_evidence, and live knowledge_reindex_document (mode: "reparse").
  • tests/processing-lifecycle.spec.ts: Added unit test asserting ArtifactRepository.save(), composeDocumentEvidence(), and toolOutput() omit own undefined keys while keeping clones detached.

Verification

All required checks from CONTRIBUTING.md passed locally on Windows (Node v24.16.0, DSH host SDK commit 9c2670d3):

  • npm run verify:workspace — passed (5 explicit decisions)
  • npm run typecheck — passed
  • npm test — passed (59 test files, 738/738 tests; the new tool-contract.spec.ts test reproduced the exact value is not lossless JSON failure prior to the fix)
  • npm run build — passed
  • npm run audit:prod — passed (critical=0, high=0)
  • npm run verify:package — passed
  • node scripts/benchmark-retrieval.mjs — passed (Hit@1 100.0%, MRR 1.000, 0 overruns)
  • node scripts/smoke-worker-protocol.mjs — passed (4/4 bundles)

Impact

  • Compatibility: Fully backwards-compatible; serialized JSON shapes are unchanged (JSON.stringify already dropped undefined keys).
  • Security: No security surface change.
  • Storage migration: None required.
  • Release notes: Fix value is not lossless JSON errors in knowledge_get_document and knowledge_read_evidence after importing or reindexing files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant