Skip to content

resolve_query: state JSON encoding per field type in decomposition prompt - #1945

Merged
malibio merged 1 commit into
mainfrom
issue-1915-decomposition-json-types
Aug 3, 2026
Merged

malibio merged 1 commit into
mainfrom
issue-1915-decomposition-json-types

Conversation

@malibio

@malibio malibio commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Adds an explicit JSON-encoding hint ((JSON number, not string), etc.) to each field line in resolve_query's decomposition sub-prompt, alongside the existing name: type notation.
  • Scoped locally to exec_resolve_query via a new render_resolve_query_field_line helper — deliberately not added to the shared EntityFieldDescriptor::render_shape(), which also feeds the create_node/search_nodes guidance block, per the scope decision recorded on the issue.
  • coerce_filter_value_to_field_type (from Fix resolve_query no_match against a node whose property does contain the value #1910) is kept as defense-in-depth; the prompt hint reduces how often the model needs it, but doesn't guarantee elimination.

Why

resolve_query's decomposition call could silently fail to match a node when the model encoded a filter value with the wrong JSON type for its target field (e.g. "2400" instead of 2400 for a number field). #1910 patched this per-type in Rust after the fact. This issue's "cheaper to try first" fix states the expected encoding directly in the prompt instead, closing the gap for field types not yet covered by the Rust-side coercion — most notably the reverse direction (a numeric-looking value landing on a text/enum field).

Test plan

  • cargo test -p nodespace-agent --lib resolve_query — 27 passed (includes new resolve_query_prompt_states_json_encoding_per_field_type prompt-content test)
  • Live-model validation against the locked Gemma 4 E4B model (E2E_MODEL=... cargo test ... live_resolve_query_phrasings_against_real_model -- --ignored --nocapture), including a new reverse-direction scenario (numeric-looking value → text field vendor_code) — all 4 phrasings resolved correctly
  • bun run test:all — full frontend + skill + Rust suite green (pre-push hook)
  • cargo clippy -p nodespace-agent --lib -- -D warnings — clean
  • cargo fmt — clean on touched file

Closes #1915

🤖 Generated with Claude Code

…ompt (closes #1915)

The decomposition sub-prompt named a field's declared type (`name: type`)
but never told the model how to encode a value it maps onto that field,
so a small model reading digits or booleans out of prose could emit the
wrong JSON type — the exact silent-mismatch failure #1908/#1910 patched
per-type via coerce_filter_value_to_field_type. Per the issue's own
"cheaper to try first" proposal, add an explicit JSON-encoding hint
(number/boolean/text/enum) to each field line in exec_resolve_query's
prompt, scoped locally rather than to the shared render_shape() renderer
per the issue's scope decision. Live-model validation against the locked
Gemma 4 E4B model confirms the reverse-direction case (a numeric-looking
value identifying a text field) now resolves correctly.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

Net positive, tightly scoped, well-tested. This is a clean, minimal implementation of the "cheaper to try first" fix from #1915: a single new helper (render_resolve_query_field_line) wraps the existing render_shape() output with a per-field-type JSON-encoding hint, swapped into exec_resolve_query's prompt construction. Scope discipline matches the recorded decision exactly — only packages/agent/src/local_agent/tools.rs changed; the shared render_shape()/EntityFieldDescriptor renderer in packages/core/src/ops/entity_types_block.rs is untouched. Verified via git diff --name-only origin/main...HEAD.

Build is clean (cargo build -p nodespace-agent), clippy is clean, and the full nodespace-agent lib test suite passes (456 passed, 1 ignored, 0 failed), including both new tests and the pre-existing resolve_query_coerces_stringified_number_to_match_numeric_field / resolve_query_coerces_stringified_boolean_to_match_boolean_field regression tests, confirming the coercion defense-in-depth path is untouched and still exercised. cargo fmt --check shows drift only in packages/agent/tests/golden_scenario6_sequence.rs, which this PR does not touch (confirmed pre-existing on origin/main) — not attributable to this change.

Findings

Critical Issues

None.

Suggested Improvements

None blocking. One observation, not a change request: the _ => "" fallback in render_resolve_query_field_line means any future schema field type outside {number, boolean, text, enum} silently gets no encoding hint — identical to today's behavior before this PR, and identical in kind to coerce_filter_value_to_field_type's own fallback. I checked whether date needed a case: it doesn't — the prompt already instructs the model elsewhere to resolve dates to YYYY-MM-DD strings (tools.rs:380, :1605), and the existing doc comment on coerce_filter_value_to_field_type (tools.rs:273) already notes date filters "arrive as YYYY-MM-DD strings by construction." No enumerated Rust type for field_type exists in the schema layer (it's a free string), so there's no closed set to exhaustively match against — the issue itself frames "any future field type" as an open, ongoing risk rather than something this PR is expected to close. Acceptable as-is.

Verification Notes

  • Helper correctness: render_resolve_query_field_line correctly calls field.render_shape() and appends the hint after it, not interleaved. For enum fields, render_shape() already renders "name: type (value1, value2)"; the hint is appended after that entire string, producing "condition: enum (checked_out, returned) (JSON string, even if the value looks numeric)" — verified this is exactly what the new unit test asserts and matches render_shape()'s real implementation (packages/core/src/ops/entity_types_block.rs:86-97).
  • Test quality: The new unit test (resolve_query_prompt_states_json_encoding_per_field_type) asserts on stable, meaningful substrings ("replacement_cost: number (JSON number, not string)", etc.) rather than exact whole-prompt equality, so it won't break on unrelated prompt formatting changes elsewhere while still pinning the exact contract that matters.
  • Live-model test scenario: The new reverse-direction case (vendor_code text field holding "48219", phrased as "Mark the invoice from vendor 48219 as paid") is a faithful, well-constructed reproduction of the issue's own motivating example (issue body explicitly calls out a numeric-looking value in a text/enum field as the reverse-direction risk). It follows the exact same pattern as the three pre-existing live-model scenarios in the same #[ignore]d test (same make_test_service/handle_create_schema/create_invoice setup, same hard assert_eq!(result.result["resolved"], json!(true)) check with a descriptive failure message). I can't re-run it in this environment (no GPU/model access), but the scenario construction is credible and consistent with the rest of the suite — not flagged as a gap.
  • Defense-in-depth reasoning: Keeping coerce_filter_value_to_field_type unchanged is correctly justified. This is not a backward-compatibility shim under this project's CLAUDE.md "delete old patterns" rule — it's a hedge against live-model non-determinism (a prompt hint reduces but cannot guarantee correct output from a small local model). The new doc comment on the coercion function states this distinction explicitly and accurately.
  • Documentation quality: Doc comments are accurate against the code they describe and appropriately explain why (the silent json_extract type-mismatch failure mode, the scope decision rationale citing Scenario 8a regressed 3/3 to 0/5: #1904's Schema Creation rewording routes "start tracking" to create_node #1912/create_schema emits corrupted JSON field names under grammar-constrained decoding on retry (scenario 8b regression) #1926) rather than restating mechanics. Not overclaiming — the comments describe what was actually built and verified.

Nitpicks

None.

Requirements Validation

  • JSON-encoding guidance added to the decomposition prompt per field type (prompt-hint approach, not constrained decoding — matches the issue's stated "cheaper to try first" preference)
  • Change scoped to exec_resolve_query only; shared render_shape()/EntityFieldDescriptor renderer untouched (verified via git diff --name-only)
  • Live-model validation performed against the real locked model, per the issue's explicit scope note — credible evidence in the diff (well-constructed #[ignore]d test scenario faithfully reproducing the issue's own example; commit message and doc comments claim it was run and passed); not independently reproducible in this review environment, as expected
  • Existing number/boolean coercion regression tests and behavior preserved (both regression tests pass unchanged)

Verdict: Approve.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Verdict: APPROVE (self-approval blocked by GitHub — this account authored the PR). Full review posted as a PR comment above. Summary: scope discipline confirmed (only tools.rs changed, shared render_shape() untouched), build/clippy clean, full agent lib test suite passes (456 passed, 1 ignored, 0 failed) including both new tests and the pre-existing number/boolean coercion regression tests. No blocking issues found.

@malibio
malibio merged commit 2bc7f0a into main Aug 3, 2026
@malibio
malibio deleted the issue-1915-decomposition-json-types branch August 3, 2026 20:41
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.

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

1 participant