Skip to content

Enhancement: add typed provenance to composed codedb_context output #688

Description

@justrach

What changed

Add an optional structured output mode for codedb_context that identifies the origin and validation state of every composed section while preserving the current compact Markdown response as the default.

A candidate schema should distinguish at least:

  • exact source snippets (path, line range, content hash/revision when available)
  • parsed symbol declarations or bodies
  • graph-derived neighbors/connectors, including edge type and reason
  • generated .codedb/reader.md overview content, including validated/stale/malformed state and declared source set
  • omissions caused by max_tokens or other caps

This is a focused follow-up to #531: JSON provenance shipped for codedb_search and codedb_symbol, but composed codedb_context output still lacks an equivalent machine-readable contract.

Why

Problem / failure mode

codedb_context intentionally mixes exact source, parsed structure, graph-derived context, ranked snippets, and an optional agent-authored reader.md. Human-readable headings help, and stale reader.md hashes are already detected, but MCP clients cannot reliably distinguish or threshold those evidence classes without parsing prose/comments.

That becomes important when generated orientation conflicts with current source, when a graph neighbor is only indirectly relevant, or when the token budget omits higher-cost evidence.

TencentDB Agent Memory's useful transferable principle is to preserve the layer and lifecycle of memory instead of flattening every result into one undifferentiated prompt. We should apply that principle to codedb evidence without adopting conversation storage or LLM extraction.

Reason for this approach

An opt-in structured schema gives clients trustworthy provenance while retaining the low-token Markdown path that existing agents use. It also extends the provenance model already shipped for search/symbol rather than creating a parallel API.

Constraints and trade-offs

  • Default output and token economics must remain backward compatible.
  • Hashes/revisions should validate content, not act as stable identifiers across unrelated projects.
  • Structured output must not expose absolute host paths or files outside the project root.
  • Provenance metadata can consume substantial tokens; compact encoding and optional fields are required.
  • reader.md remains untrusted, agent-authored context even when its source hash is current.

Rejected alternatives

  • Do not mark generated overview text as equivalent to exact source.
  • Do not require every client to parse Markdown headings or HTML comments.
  • Do not replace the existing compact output unconditionally.

Experiments required

E1: schema prototype

Prototype format=json or an MCP structured-content equivalent using the same conventions as search/symbol provenance. Test at least these states:

  • no reader.md
  • valid reader.md
  • stale source hash
  • malformed metadata
  • graph neighbors present/absent
  • max_tokens omissions
  • deferred/incomplete indexes

Compare a section-oriented schema against a flat result list. Prefer the smallest schema that preserves evidence type, location, validation, score/reason, and omission metadata.

E2: token and latency cost

Across existing codedb_context benchmark tasks, measure:

  • response bytes and estimated tokens
  • serialization latency
  • peak allocation/RSS
  • cache behavior

Ship gate: default Markdown is unchanged; structured mode adds less than 5% execution latency and keeps metadata overhead below 10% of the returned evidence tokens on the median task.

E3: trust-conflict evaluation

Create tasks where:

  • reader.md is stale
  • a current but incorrect overview claim conflicts with exact source
  • a graph neighbor looks relevant but does not contain the answer
  • exact source is omitted under a tight token budget

Compare agent behavior with prose-only versus typed provenance. Measure whether the agent cites/reads exact source before acting and whether false conclusions decrease.

Ship gate: structured provenance must not lower task pass rate and should reduce unsupported conclusions on the conflict set.

E4: compatibility and schema stability

Run representative MCP clients against both formats. Add golden/schema tests for field names and omission semantics. Unknown future evidence types must be ignorable without breaking clients.

Acceptance criteria

  • Structured context output distinguishes exact, parsed, graph-derived, and generated evidence.
  • reader.md validation state and source set are explicit.
  • Token-budget omissions are machine-readable.
  • No absolute paths or out-of-root data are exposed.
  • Existing default output remains byte-compatible unless separately justified.
  • Token, latency, and trust-conflict experiment results are recorded.
  • Unit and MCP E2E tests pass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions