Skip to content

resolve_query: constrain decomposition output JSON types instead of per-type coercion patching #1915

Closed

Description

@malibio

Background

PR #1910 (closing #1908) fixed resolve_query silently returning no_match when its nested decomposition LLM call emits a filter value whose JSON type doesn't match the target schema field's declared type (e.g. a numeric field's value emitted as a JSON string "2400" instead of 2400). SQLite's json_extract preserves the stored value's real type, so a type-mismatched equality filter compares unequal — silently indistinguishable from "no such node."

The fix added coerce_filter_value_to_field_type (packages/agent/src/local_agent/tools.rs), which normalizes stringified numbers and booleans to their correct JSON type before the filter reaches the query layer. This is a per-type patch: it now covers number and boolean, added as each was confirmed reachable.

The underlying issue

The decomposition prompt (exec_resolve_query's nested LLM call) gives the model no explicit JSON-type guidance per field type — it only names field: type (e.g. replacement_cost: number). Nothing constrains how the model encodes a value it maps onto that field. This means the same class of silent failure is reachable for any field type not yet covered by coerce_filter_value_to_field_type, most plausibly:

  • Reverse direction for text/enum fields: a numeric-looking value (a numeric ID stored as text, or a numeric-looking enum member) emitted as a JSON number instead of a string would silently mismatch the same way, in the opposite direction.
  • Any future field type added to the schema system without a corresponding coercion arm.

Per-type coercion patching in Rust will accrete one match arm per observed failure, discovered only when a user hits it live (as #1908 itself was — split out of #1894, filed, then took two live-model debugging sessions to root-cause).

Proposed direction (not yet designed)

Constrain the decomposition model's output directly, rather than patching after the fact:

  • Grammar/JSON-schema-constrained decoding for the decomposition call, if the inference engine supports it, so the model cannot emit a type-mismatched value in the first place.
  • Or: have the decomposition prompt state the expected JSON encoding per field type alongside name: type (e.g. replacement_cost: number (JSON number, not string)), and measure whether that alone closes the gap — cheaper to try first, consistent with this codebase's "measure against the real model, don't guess" standard for this subsystem.

Scope note

This needs its own live-model validation before implementation, following the same methodology PR #1910 used (drive the real locked model against the actual prompt, verify the fix genuinely changes model output — not just add code and assume). Not a quick patch; a proper investigation.

Reference

Activity

  1. malibio commented on Aug 3, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 171 test files passed, 4108 tests passed (49.87s)

  2. self-assigned this
    on Aug 3, 2026
  3. malibio commented on Aug 3, 2026

    @malibio
    CollaboratorAuthor

    Scoping question before implementation

    Issue proposes the cheaper fix first: state the expected JSON encoding per field type in the decomposition prompt (e.g. replacement_cost: number (JSON number, not string)), then live-model-validate whether that alone closes the gap.

    The field-line rendering used by exec_resolve_query's decomposition prompt (EntityFieldDescriptor::render_shape() in packages/core/src/ops/entity_types_block.rs) is shared: render_shape() is also called (via render(), which adds ", required") by the main RELEVANT ENTITY TYPES block used for create_node/search_nodes guidance to the primary agent.

    Two options:

    1. Scope to resolve_query only — add the JSON-type hint in a local helper inside exec_resolve_query's field_lines construction, not in the shared descriptor. Matches this issue's stated scope (the decomposition sub-prompt specifically), keeps blast radius to one prompt, and avoids touching prompt-snapshot/regression tests pinned elsewhere (routing latency suites reference the shared renderer's output).

    2. Add to shared render_shape() — every caller gets the hint, including the main entity-types block. More consistent long-term (the same stringified-value drift is plausible on create_node too, just not this issue's subject), but wider blast radius, and would need those other pinned tests (e.g. routing latency suite, entity_types_block tests) updated/re-validated too.

    Which scope do you want for this issue — (1) resolve_query-local, or (2) shared renderer? If (2), should a follow-up issue be filed for validating it against the create_node path specifically, or is validating resolve_query's live-model behavior considered sufficient coverage since they share the renderer?

  4. malibio commented on Aug 3, 2026

    @malibio
    CollaboratorAuthor

    Scope decision: (1), resolve_query-local

    Go with option 1 — add the JSON-type hint in a local helper inside exec_resolve_query's field_lines construction, not the shared render_shape().

    Reasoning: this cluster has a repeated, well-documented history of shared-surface changes validated only against their motivating scenario later regressing a bystander (#1904/#1912's routing regression, #1925/#1926's history-compaction fallout, the four rejected retrieval-merge variants in #1917). Option 2 would touch a renderer that create_node/search_nodes guidance also depends on, without this issue's scope including validation of either of those paths. That's exactly the pattern to avoid — fix the thing you're scoped to fix, on the surface you're scoped to touch.

    If the same stringified-value drift is real on create_node, that's genuinely separate scope per this project's standard (different call site, needs its own investigation/measurement) — file it separately if you find evidence for it while you're in this code, don't speculatively fix it here without validating it. Proceed with (1).

  5. malibio commented on Aug 3, 2026

    @malibio
    CollaboratorAuthor

    Implemented per the scope decision (resolve_query-local JSON-encoding hint). Live-model validation against the locked Gemma 4 E4B model confirms the fix, including the reverse-direction case (numeric-looking value into a text field). Pushing branch now; PR to follow.

  6. malibio commented on Aug 3, 2026

    @malibio
    CollaboratorAuthor

    Good — scope decision followed, live-model validated including the reverse-direction case. Proceed to PR, /pragmatic-code-review, merge — same flow as everything else in this cluster.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions