Skip to content

Render schema relationships in EXISTING SCHEMAS and steer traversal to get_related_nodes (closes #2911) - #3049

Merged
malibio merged 3 commits into
mainfrom
issue-2911-relationship-traversal-prompt
Sep 25, 2026
Merged

malibio merged 3 commits into
mainfrom
issue-2911-relationship-traversal-prompt

Conversation

@malibio

@malibio malibio commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Closes #2911.

What changed

  • EntityTypeDescriptor::render_line (packages/core/src/ops/entity_types_block.rs): the compact EXISTING SCHEMAS line now lists a type's relationships after the field list: - incident_report -> resolved: boolean ~> on_call (person). Stage-2 routing pre-selects schema candidates without calling search_skills, so this line is often the only schema the model sees. Before this change, relationships appeared only in schema_metadata.
    • The target type is written in () rather than as on_call->person, because the line's grammar reserves -> for introducing the field list. A golden test pins -> to a single occurrence per line.
    • Relationship descriptions still stay out of the compact line.
  • Tool descriptions (packages/agent/src/local_agent/tools.rs): search_nodes says names after ~> are relationships and can't be used as filters. get_related_nodes says to use it for those names.
  • Tests: new render tests (relationships rendered and ordered after fields; no segment when none are declared). The golden and JSON-parity tests were updated. The stage2_tool_surface prompt golden was regenerated for the description change.

Verification: live A/B

gemma-4-e4b-q4km on an isolated daemon, same seed in both arms, 5 trials each, prompt "The incident Rowan was on call for — mark it resolved":

Arm Traversal via get_related_nodes(Rowan, on_call)
main 0/5 (5 errored search_nodes calls per trial, invented person.name filter)
this branch 5/5, correct incident every trial

The task itself still doesn't complete: routing picks Conflict Resolution, which has no update_node. That is gap 3, tracked in #2912. Full details and a seeding pitfall are in the issue comment.

Pushed with --no-verify at the maintainer's request (machine contention). Ran locally: cargo test -p nodespace-agent (all pass), cargo test -p nodespace-core --lib filtered to schema/context/skill ops (395 pass), quality:fix clean.

🤖 Generated with Claude Code

…o get_related_nodes (closes #2911)

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 #3049

Review Type: Initial review (no prior reviews). Scope is commit 3c67524f, which touches 3 files (+83/−19).

Requirements Check (#2911)

  • ✅ render_line() includes relationship names. They render as ~> name (target) after the field list. on_call (person) is used instead of on_call->person because the grammar reserves -> for the field list. That keeps the module's documented grammar intact, and a test pins -> to one occurrence per line.
  • ✅ search_nodes / get_related_nodes steer the model toward traversal. Both descriptions now say that names after ~> are relationships, not filters. The example is neutral so it doesn't trip the eval-contamination test.
  • ✅ The scenario was re-run. The live A/B on E4B went from 0/5 on main to 5/5 on this branch, and every trial traversed get_related_nodes(Rowan, on_call). This evidence is manual. No eval fixture was added, which the criterion allows ("or equivalent").
  • ✅ Gap 3 is filed separately as Stage 2 has no clarify path when retrieval's top skill is a lexical false-positive #2912.

Code Review Findings

🔴 Must fix before merge

📁 packages/core/src/ops/entity_types_block.rs:460 (and from_schema, line ~281): the type-system extends row now renders as a traversable relationship
from_schema copies schema.relationships verbatim. A raw SchemaNode from get_schema_node still contains the schema's own extends bookkeeping row. This is deliberate at the storage layer, and resolve_relationships in services/node_service/schema.rs:~740 explains why it is kept there. So every subtype schema now renders as, for example, - bug -> ... ~> extends (task). Methodology installs create such schemas. In the same PR, both tool descriptions tell the model to call get_related_nodes with any name after ~>. For an instance node, resolve_relationship_name goes through resolve_relationships, which filters extends out, so the call fails with "not declared in either direction". The model burns a turn on a relationship that no data node can carry.

  • Principle: don't advertise a capability the system can't honor. That is the "phantom referent" failure mode this prompt surface has hit before. graph_resolver.rs:585 and resolve_relationships both already exclude type-system relationships for exactly this reason.
  • Fix (one line plus a test): in from_schema, add .filter(|r| !crate::models::schema::is_type_system_relationship(&r.name)) before mapping. Putting the filter at construction also keeps extends out of schema_metadata (to_json). It leaked there before this PR, but the compact line makes it far more visible. Add a test asserting that a schema whose relationships include extends renders no ~> segment.

🟡 Should address

📁 packages/agent/src/local_agent/tools.rs:1246: get_related_nodes points at a name the compact line never shows
The existing sentence says "use its forward name from the source, or its declared reverseName from the target". The compact line renders only the forward name. reverse_name is not rendered, and the target type's line (e.g. person) shows nothing about inbound relationships. A model standing on the target therefore has no reverseName to use. It works today only because resolve_relationship_name accepts a forward name from the far end (InboundForward, with direction defaulting to both), which the A/B confirms. That is the path the model actually took.

  • Principle: guidance should reference only what is in the prompt.
  • Option A: reword to "the forward name shown after ~> works from either end". This is cheapest and matches observed behavior.
  • Option B: render the reverse name, e.g. on_call (person, reverse: incidents_on_call). This costs budget and grammar surface.

Option A is enough for this PR.

📁 packages/core/src/ops/entity_types_block.rs:839: tests don't cover an in-direction declaration
Declarations with direction: In render the same way as Out, which is fine because the name is traversable either way (rel_ops.rs:237). A small assertion would pin that deliberate choice so a future "show direction" change is made on purpose. Low effort; optional if you'd rather not.

🟢 Nits / observations

  • 📁 entity_types_block.rs:~441, grammar doc: stale caller list. "Both call sites — workspace-context … and per-candidate routing" is now inaccurate. There is a third caller: the already_exists error in schema/mod.rs:1865, which now also gains the ~> segment. That is an improvement, since it describes the real definition more fully. Consider saying "all call sites" rather than enumerating them.
  • Special characters in names. No validation stops a relationship name from containing ;, ( or ~>, which would make the line ambiguous. Field names already have the same exposure, so this is not new. Mentioned only for completeness. No action needed unless name validation is added globally.
  • Char budget (context_ops.rs:~889). Lines grow by roughly 15–40 chars per relationship. When a line overflows max_chars, the loop breaks, so a type with many relationships can push later schemas out of EXISTING SCHEMAS. This is acceptable at current schema sizes and the budget logic is untouched. Worth watching if relationship-heavy methodologies land.
  • Inherited relationships. from_schema renders only a schema's own relationships, not ones inherited through extends. Fields have the same limitation, so this is consistent and out of scope.
  • Comment hygiene. No issue numbers were added to code comments. The rationale comments explain why (Stage-2 routing shows only the compact line), which is good.
  • Test quality. The new tests are well targeted: ordering (-> before ~>), -> appearing exactly once, and no segment when nothing is declared. The golden update to renders_type_field_enum_required_and_template exercises the typed path end to end.

Recommendation: REQUEST CHANGES

The design is sound and the evidence is strong. ~> fits the documented grammar cleanly, and the A/B shows the intended behavior change (0/5 → 5/5). The only blocker is the extends leak: a one-line filter plus a test. Once that is fixed and the reverseName wording is tightened, this is ready to 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.

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

…_related_nodes at the ~> name

- from_schema drops type-system relationships (extends), which are not
  traversable from an instance and would send get_related_nodes to an error
- get_related_nodes description names the forward name shown after '~>' as
  usable from either end, since the compact line never shows reverseName
- render_line doc lists all three call sites
- Skipped: in-direction render test (rendering is direction-independent)

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 (ce7ffe5)

Addressed

  • 🔴 extends leak: EntityTypeDescriptor::from_schema now drops type-system relationships via is_type_system_relationship. This keeps extends out of both the compact line and schema_metadata. New test: type_system_extends_row_is_not_listed_as_a_relationship.
  • 🟡 get_related_nodes wording: the description now says the forward name shown after ~> works from either end with direction: both, and that reverseName also works from the target. It no longer depends on a name the compact line never shows.
  • 🟢 The render_line doc now lists all three call sites, including the already_exists error.

Skipped

  • 🟡 in-direction render test: rendering never reads direction, so the assertion would pin nothing the existing tests don't already cover. Worth adding if direction ever affects the render.
  • 🟢 Special characters, char budget, inherited relationships: no action. Each is pre-existing or matches how fields behave, as the review noted.

Tests: cargo test -p nodespace-agent all pass; core schema/context/skill ops 396 pass; clippy clean; quality:fix clean. Pushed with --no-verify at the maintainer's request.

Re-review: needed. The 🔴 fix changes what from_schema emits for every caller, including schema_metadata.

@malibio

malibio commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator Author

Pragmatic Code Review: PR #3049

Review Type: Re-Review. Scope is commit ce7ffe55, which touches 2 files (+27/−3) on top of 3c67524f.

Previous Review Summary

  • Previous recommendations: 1 🔴 (the extends row leaks into the rendered relationships), 2 🟡 (the get_related_nodes wording pointed at a reverseName the prompt never shows; no test for in-direction declarations), and 🟢 nits (stale caller list in the doc, special characters, char budget, inherited relationships).
  • Addressed:
    • 🔴 from_schema now filters with is_type_system_relationship, and a new test covers it.
    • 🟡 The wording now points at the ~> name.
    • 🟢 The caller-list doc was fixed.
  • Not addressed (reasonably skipped):
    • The in-direction render test. The stated reason is correct: rendering never reads direction.
    • Special characters, char budget and inherited relationships. These are pre-existing or match how fields behave.
  • New issues: one 🟡. The reworded get_related_nodes description now claims the ~> name works "from either end". That is false for in-direction declarations. Details below. Nothing blocking.

Blast-radius check: filtering extends in EntityTypeDescriptor::from_schema

I checked every consumer of the descriptor and of schema_metadata relationships. None of them needs extends.

  • ops/context_ops.rs:887 (EXISTING SCHEMAS): uses render_line only. Dropping extends is the intended fix.
  • ops/skill_ops.rs:527 and :573 (schema_metadata via to_json): go into the prompt only. On the agent side, from_json decodes back to descriptors that are rendered (tools.rs:1050/:1124, routing.rs). No agent code branches on a relationship named extends.
  • schema/mod.rs:1864 (already_exists error): uses render_line. Before this PR it never rendered relationships at all, so nothing is lost.
  • daemon/.../local_agent_service.rs:2432 (unlinked-pair check): reads the raw SchemaNode.relationships from get_schema_node, not the descriptor, so it is unaffected. This is also correct: extends shouldn't count as "linked" there either way.
  • Tests: no test asserts extends or extended_by in descriptor output (checked find_skills_schema_metadata_test.rs, schema_chain_blindness_guard_test.rs and prompt_assembly_snapshot.rs). cargo test -p nodespace-core --lib entity_types_block: 18 passed.
  • One observable change: schema_metadata no longer tells the model that a subtype (e.g. bug) extends task. Before this PR it did so only by accident, in a form that looked traversable. Inherited fields are not rendered either, so this is consistent. Details under 🟢 below.

Findings

🔴 Must fix before merge

None.

🟡 Should address (non-blocking)

📁 packages/agent/src/local_agent/tools.rs:1246: "the forward name shown after ~> … from either end" is false for in-direction declarations
render_line shows every declared name after ~>, whatever its direction. create_schema exposes direction: "in" to the model (tools.rs:1402). Take an in declaration x on schema A that targets B:

  • From an A node, x resolves (rel_ops.rs:237 → Reverse).
  • From a B node, resolve_relationship_name finds nothing. The reason is that get_inbound_relationships skips non-out declarations (relationship.rs:2182). So InboundForward never matches, and the call returns InvalidParams.

The description now tells the model it will work from either end.

  • Impact: low. No seeded schema uses in (only test fixtures in relationship.rs do). The error lists the valid spellings, so the model recovers in one turn. It is still guidance that promises something the resolver can't deliver. This is the same failure class the 🔴 was about.
  • Principle: guidance should promise only what the backend honors.
  • Cheapest fix: narrow the claim, e.g. "…traversed by its forward name from the declaring type, and usually from the target too (direction 'both')…". Or leave the text alone and track it as a follow-up.
  • Structural fix (follow-up issue): have the resolver treat an in declaration's name as resolvable from its target, by mapping to Forward on reverse_name. That makes the claim true instead of weakening it.

🟢 Nits / observations

  • Subtype information in schema_metadata. Filtering extends removes the only (accidental) signal that a type is a subtype. If Stage-2 routing ever needs "bug is a kind of task", render it as its own non-~> token (e.g. : task) rather than bringing it back as a relationship. No action now.
  • Nit: 📁 packages/core/src/ops/entity_types_block.rs:445-447. The reflowed doc comment breaks mid-sentence ("…share this one\n/// shape; only whether the quoted\n/// name segment…"). Worth rewrapping.
  • Test (entity_types_block.rs:872). The test is well targeted: it asserts both that relationships is empty and that no ~> appears. Using the shared EXTENDS_RELATIONSHIP/EXTENDED_BY_RELATIONSHIP constants keeps it from drifting from TYPE_SYSTEM_RELATIONSHIPS.
  • Filter placement. The filter belongs at construction. It gives one choke point for both the render and JSON paths, and matches the precedent in resolve_relationships and graph_resolver.rs.

Recommendation: APPROVE

The blocker is fixed at the right layer, with a test, and filtering extends breaks none of its consumers. The remaining 🟡 is a narrow overclaim about uncommon in declarations, and its failure mode recovers on its own. Tighten the wording in this PR or file a follow-up.

@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 (re-review): APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.

An in-direction declaration's name is not resolvable from its target, so the
get_related_nodes description now says 'usually' and points at the error that
lists valid names. Also rewraps a render_line doc sentence.

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

malibio commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator Author

Re-review addressed

  • 🟡 get_related_nodes overclaim: the description now says the ~> name works from the declaring type and usually from its target, and notes that an inapplicable name is rejected with an error listing the valid ones. The structural fix (making the resolver accept an in declaration's name from its target) is left as a possible follow-up, since no seeded schema uses in.
  • 🟢 Rewrapped the render_line doc sentence.
  • 🟢 The subtype signal no longer appears in schema_metadata: no action. It was only ever there by accident, in a form that looked traversable.

Tests: agent all pass, entity_types_block 18/18, quality:fix clean. These are wording-only changes, so no re-review is needed.

@malibio
malibio merged commit 0336e3f into main Sep 25, 2026
@malibio
malibio deleted the issue-2911-relationship-traversal-prompt branch September 25, 2026 22: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.

Agent can't traverse a schema relationship to complete a request: two prompt gaps confirmed live

1 participant