Skip to content

Extend tool-schema wire-key guard to relationships, edge fields and nested items (#2836) - #3028

Merged
malibio merged 2 commits into
mainfrom
issue-2836-schema-wire-key-guard
Sep 25, 2026
Merged

malibio merged 2 commits into
mainfrom
issue-2836-schema-wire-key-guard

Conversation

@malibio

@malibio malibio commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Closes #2836

Summary

Generalizes schema_tool_field_keys_are_accepted_schema_field_wire_keys into a table-driven walk, schema_tool_item_keys_are_accepted_wire_keys, that round-trips every declared item key of create_schema/update_schema through the real deserializer:

Surface Struct
fields / add_fields SchemaField
relationships / add_relationships SchemaRelationship
…edgeFields[] EdgeField
…coreValues[] EnumValue
  • The walk descends into any declared array whose items declare properties. Each struct's WireShape maps nested keys (fields, itemFields, coreValues, userValues, edgeFields) to their child shapes. An unmapped nested schema fails with a message asking for the mapping, so it can't be skipped silently.
  • The test asserts that the nested paths (fields[].coreValues[], both edgeFields[]) were actually visited, so a walk that stops early can't pass vacuously.
  • sample_value_for_declared_key now has an enum arm that uses the first variant, which covers direction/cardinality/reverseCardinality. The unknown field check that separates harness gaps from real key failures is unchanged.

Test-only change. The sweep found no misspelled key.

Verification

  • The new test passes.
  • Mutation checks (both reverted): renaming edgeFields required → is_required fails with CreateSchema.relationships[].edgeFields[] declares key "is_required" … EdgeField rejects it, and renaming reverseCardinality → reverse_cardinality fails for SchemaRelationship.
  • The pre-push gate passed.

🤖 Generated with Claude Code

…ested items (closes #2836)

Generalize schema_tool_field_keys_are_accepted_schema_field_wire_keys into
a table-driven walk (schema_tool_item_keys_are_accepted_wire_keys) that
round-trips every declared item key of create_schema/update_schema through
its real struct: SchemaField for fields/add_fields, SchemaRelationship for
relationships/add_relationships, EdgeField for edgeFields, EnumValue for
coreValues. The walk descends into any nested item schema that declares
properties and fails asking for a struct mapping if one is missing; it also
pins that the nested surfaces were actually reached. The probe helper gains
an enum arm for direction/cardinality. No declared key was misspelled.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@malibio

malibio commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator Author

Pragmatic Code Review: PR #3028

Review Type: Initial Review (no prior reviews or comments on the PR)

Scope: Test-only change in packages/agent/src/local_agent/tools.rs. It turns schema_tool_field_keys_are_accepted_schema_field_wire_keys into the table-driven, recursive schema_tool_item_keys_are_accepted_wire_keys. cargo test -p nodespace-agent --lib schema_tool_item_keys passes locally (1 passed).

Requirements Check (issue #2836)

  • ✅ create_schema.relationships[] is checked against SchemaRelationship
  • ✅ update_schema.add_relationships[] is checked against SchemaRelationship
  • ✅ edgeFields[] (both tools) is checked against EdgeField by recursing from the relationship shape. The visited-path pin confirms both paths are reached.
  • ✅ Nested SchemaField items (fields/itemFields) are mapped in SCHEMA_FIELD_SHAPE.nested. The tool schemas don't declare these keys today, so the walk doesn't reach them yet, but it will as soon as either one is declared.
  • ✅ The probe helper has an enum arm for direction/cardinality/reverseCardinality. It takes the first declared variant, which is valid for every enum declared today.
  • ✅ The existing test was generalized, not copy-pasted, as the issue suggested. No production code changed.
  • ✅ It catches real misspellings. The author mutation-tested edgeFields.required → is_required and reverseCardinality → reverse_cardinality and both were caught. I also traced the base-key case: a misspelling of a key the probe base already sets (for example reverse_name next to the base's reverseName) still adds an extra key, so it still gets unknown field. That case is covered.

Code Review Findings

🟡 The top-level surface list is hand-maintained, so same-class sibling surfaces stay unguarded
📁 packages/agent/src/local_agent/tools.rs:4478-4491 (surface table), :1478-1514 (declarations)
Nested descent enforces itself: an undeclared mapping panics and asks for one. The top level doesn't. update_schema already declares two more item arrays with the same exposure, and neither is in the table:

  • rename_fields[] goes to FieldRename (packages/core/src/schema/mod.rs:1815, rename_all = "camelCase", deny_unknown_fields). It declares the multi-word key friendlyName, which is exactly the kind of key this test exists for. The e2e test at tools.rs:7615 sends a hand-written friendlyName, not the declared key, so it doesn't guard against drift between the declaration and the struct.
  • add_field_values[] goes to FieldValueAddition (mod.rs:1834, also deny_unknown_fields), with nested values[] going to EnumValue. skill_pipeline.rs:~1855 only checks that field/values are present. A misspelled extra key would get past it.

These are outside the issue's literal table, so this doesn't block. It is the same bug class the issue says has happened three times, though, and fixing it is cheap. Recommendation:

  1. Add FIELD_RENAME_SHAPE (base {from, to}) and FIELD_VALUE_ADDITION_SHAPE (base {field, values: []}, nested ("values", &ENUM_VALUE_SHAPE)). Both are reachable as nodespace_core::schema::{FieldRename, FieldValueAddition}.
  2. Make the top level enforce itself the way nested levels do. Iterate every property of both tools' parameters_schema, and panic if one has items.properties but no entry in the surface table. Then a new item-array parameter can't go unguarded without anyone noticing.
    Principle: the guard should fail closed at every level, not just the nested ones (consistency; poka-yoke).

🟢 EDGE_FIELD_SHAPE.nested coreValues mapping is unreachable today
📁 tools.rs:1413-1416 / :1536-1539 (edge coreValues declared with no items), tools.rs:4566
The edge-field coreValues has no items.properties, so the walk never recurses into it. The ENUM_VALUE_SHAPE mapping sits unused until someone adds items. The same goes for userValues/fields/itemFields on SCHEMA_FIELD_SHAPE. This is harmless, since it's forward-looking and costs nothing. Two options: add items: {value, label} to the edge coreValues declarations, which would also give the model a more exact contract and make the mapping live; or accept it as-is. If you keep the pre-mapped entries, a short comment saying they're pre-registered for keys not declared yet would help readers.
Principle: YAGNI and clarity. A mapping that's never exercised looks like coverage but isn't.

🟢 The vacuity pin skips UpdateSchema.add_fields[].coreValues[]
📁 tools.rs:4505-4509
The pin lists three nested paths. UpdateSchema.add_fields[].coreValues[] is declared with items.properties too (line 1467), and the walk does reach it, but the pin doesn't assert it. Add it so all four currently reachable nested surfaces are pinned. That way a future edit dropping items from one declaration can't quietly shrink coverage.
Principle: the test should assert what it claims to cover.

🟢 Nit: odd indentation in the rustfmt'd panic string
📁 tools.rs:~4622-4631
Inside the unwrap_or_else(|| panic!(...)) in the nested lookup, the string continuation lines start at inconsistent columns. The \ continuation strips leading whitespace, so the message is correct at runtime. It just reads oddly. Pulling the lookup into a let (or a small helper) would shorten the rustfmt nesting.

🟢 Nit: the enum arm always probes the first variant
📁 tools.rs:4654-4656
Fine for this test, which checks keys, not values. Noting it only so nobody later expects the enum arm to validate the whole vocabulary. The doc comment already describes the behaviour accurately.

What's good

  • WireShape statics with fn pointers and a generic round_trip::<T> are a lean, table-driven design. Adding a surface is one row plus one static (DRY, open/closed).
  • Keeping the unknown field check separate from other errors is the right call. A harness gap and a real schema bug produce clearly different failure messages.
  • The visited pin guards against the walk passing vacuously. That's the main risk with a recursive walker, and it's handled.
  • No lint suppression and no issue numbers in comments, per CLAUDE.md. The doc comment explains why the guard exists.

Recommendation: APPROVE

This improves the codebase and covers every requirement in the issue. The 🟡 finding (the rename_fields/add_field_values surfaces, plus making the top level fail closed) is a small follow-up in the same spirit. It's worth adding here, or filing separately, but it doesn't block the merge.

@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.

Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.

…at top level

- Add FieldRename and FieldValueAddition shapes (values[] -> EnumValue) so
  update_schema's rename_fields and add_field_values item keys are checked.
- Fail when any create_schema/update_schema parameter declares item
  properties without a row in the surfaces table, matching the nested walk.
- Pin UpdateSchema.add_fields[].coreValues[] and add_field_values[].values[]
  as reached paths.
- Document that WireShape::nested lists every nested array of the struct,
  including ones not yet declared; flatten the nested-shape lookup.

Skipped: adding items to edgeFields' coreValues declaration — changes the
production tool schema, outside this test-only PR.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@malibio

malibio commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator Author

Review addressed (e21c4f3)

✅ Addressed

  • 🟡 Sibling surfaces: added FIELD_RENAME_SHAPE for rename_fields[] and FIELD_VALUE_ADDITION_SHAPE for add_field_values[], whose values[] are checked as EnumValue. The top level now fails closed: if any create/update_schema parameter declares item properties but has no row in the surfaces table, the test fails.
  • 🟢 Visited paths: the reached-path check now also pins UpdateSchema.add_fields[].coreValues[] and UpdateSchema.add_field_values[].values[].
  • 🟢 Unused mappings: WireShape::nested now documents that it lists every nested array on the struct, including ones the tool schemas don't declare yet.
  • 🟢 Nested lookup: flattened into let … else.

⏭️ Skipped

  • Adding items to the edge coreValues declaration. That would change the production tool schema, and this PR is test-only.
  • The enum-arm nit, which needs no action.

Verification: each mutation below was tested and then reverted. All were caught with actionable messages. The pre-push gate passed.

Mutation Result
Removed the rename_fields row Failed: "has no row in this test's surfaces table"
friendlyName → friendly_name in rename_fields Failed: FieldRename rejects friendly_name

Re-review: not needed. The changes stay inside the reviewed pattern: two table rows, two statics, and one extra fail-closed loop.

@malibio
malibio merged commit cdf400c into main Sep 25, 2026
@malibio
malibio deleted the issue-2836-schema-wire-key-guard branch September 25, 2026 16:56
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.

Extend the tool-schema wire-key guard to SchemaRelationship, EdgeField and nested SchemaField items

1 participant