Skip to content

Enforce ADR-078 field/relationship shadowing on more write paths - #2866

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2829-adr078-shadowing
Sep 23, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2829-adr078-shadowing

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

  • Extends validate_no_field_redeclaration's enforcement to update_schema's plain add_fields-only path (no extends key in the call). Previously that check only ran from create_schema with extends, and from update_schema's extends re-target — a later, separate add_fields call on an already-extending schema could silently redeclare an inherited field with a diverged type.
  • Adds a new validate_no_relationship_redeclaration, the relationship counterpart to the field check, built on NodeService::resolve_relationships (the chain-merged effective relationship set). Wires it into handle_create_schema, update_schema's add_relationships path, and update_schema's extends re-target path — closing the gap where resolve_relationships's own doc comment described "nearest-first, first-declared wins" shadowing as the expected rule, but no write path ever rejected the collision.
  • Both new ancestor checks are skipped when the same update_schema call is also re-targeting extends, since that path already re-checks the fully-merged field/relationship list against the new parent.

Test plan

  • New unit tests in packages/core/src/schema/schema_test.rs covering: an add_fields-only call rejecting a redeclared inherited field (both immediate-parent and grandparent collisions), a non-colliding add_fields-only call still succeeding, create_schema rejecting a relationship colliding with an inherited one, an add_relationships-only call rejecting a redeclared inherited relationship (immediate-parent and grandparent), a non-colliding add_relationships-only call still succeeding, and an extends re-target rejecting a relationship collision with the new parent.
  • cargo test -p nodespace-core --lib schema::schema_test — 135 passed
  • cargo test -p nodespace-core --test schema_relationship_declarations_test --test extends_subtype_identity_test --test create_schema_result_reflects_persisted_state_test — 51 passed, no regressions
  • cargo clippy -p nodespace-core --lib --tests -- -D warnings — clean
  • bun run quality:fix — clean
  • Pre-push gate (bun run test:all + cargo build --bin nodespaced + test:e2e) — all green

Closes #2829.

🤖 Generated with Claude Code

update_schema's add_fields-only path only checked a new field against
the schema's own current fields, never against the extends chain's
effective set — so a later, separate add_fields call on an
already-extending schema could silently redeclare an inherited field
with a diverged type. Extend validate_no_field_redeclaration's call
sites to also cover a plain add_fields update, gated on the call not
also re-targeting extends (that path already re-checks the merged
field list against the new parent).

Relationships had no write-time collision check against the extends
chain at all, despite resolve_relationships's own doc comment
describing "nearest-first, first-declared wins" as the expected
shadowing rule. Add validate_no_relationship_redeclaration, mirroring
the field check and built on NodeService::resolve_relationships, and
call it from handle_create_schema, update_schema's add_relationships
path, and the extends re-target path.

Closes #2829.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Code Review Summary

This PR closes two concrete gaps in ADR-078 ("extends is additive-only") enforcement: (1) update_schema's add_fields-only path never checked a new field against the ancestor chain's effective field set, only its own current fields, and (2) relationships had no write-time collision check against the extends chain at all, on any path, despite NodeService::resolve_relationships's own doc comment promising "nearest-first, first-declared wins" as the shadowing rule. The fix is small, well-scoped, and closely mirrors the pre-existing validate_no_field_redeclaration pattern. I traced every call site (handle_create_schema, and all three handle_update_schema blocks — add_fields, add_relationships, extends re-target) by hand against the actual diff, verified the params.extends.is_none() gating logic against both the "add + retarget in one call" and "add-only on an already-extending schema" scenarios, and ran the full schema::schema_test module (135 tests, all passing).

Verdict: net positive, no correctness or security regressions found. A few edge cases worth a fast follow, none of which are introduced-and-unsafe — they're either fail-closed (over-conservative, not under-detecting) or pre-existing/out of this PR's scope.

Findings

Critical Issues

None.

Suggested Improvements

  1. add_fields/add_relationships-only ancestor checks read the pre-call schema.relationships snapshot, not this call's own live relationship list. Both new gated checks (mod.rs ~1933 and ~2094) call declared_extends_parent(&schema.relationships) — schema as loaded at the top of the handler, before this same call's remove_relationships is applied to the local relationships variable. Concretely: a single request with {"remove_relationships": ["extends"], "add_fields": [...]} will still validate the new field against the old parent, even though the call is simultaneously detaching from it. This isn't unsafe (it fails closed — an over-conservative rejection, never a missed collision), and it's a narrow admin-operation combination, but it's inconsistent with the extends re-target block a few lines down, which explicitly validates "as [the list stands] after this call's add/remove/rename, not as they were loaded" (mod.rs:2120-2122) — the exact posture the add_fields/add_relationships blocks don't take here. Worth aligning (read the post-remove_relationships local relationships list instead of schema.relationships) for consistency, even if not required for correctness.

  2. Adjacent, pre-existing gap (not introduced by this PR): remove_relationships has no special case for extends. reject_reserved_relationship_names blocks a caller from adding a relationship named extends/extended_by, but nothing stops remove_relationships: ["extends"] from silently detaching a schema from its parent — which directly contradicts the extends re-target block's own comment that "there is no way to clear" the edge (mod.rs:2110-2111). This predates the PR and is outside its stated scope, but since it interacts with the exact code this PR touches, it's worth a follow-up issue to decide whether that's an intentional escape hatch or an oversight.

  3. Test coverage gap: the combined "add + retarget in one call" case isn't directly tested. The params.extends.is_none() gating is the single most subtle piece of new logic, and its rationale (a same-call add_fields/add_relationships entry is folded into the merged list and re-checked against the new parent, so skipping the old-parent check is correct rather than a hole) is explained at length in comments but never exercised by a test. A test that retargets extends to a new, non-colliding parent while adding a field/relationship that would collide with the old parent (and asserts success) would directly validate the gating logic rather than relying on code-reading trust. All 8 new tests instead validate one dimension at a time (add-only, retarget-only) — solid, but this specific interaction between the two is the part most likely to regress silently.

Nitpicks

  • Nit: The task description accompanying this review says 9 new tests were added; I count 8 in the actual diff (test_add_fields_only_call_rejects_a_redeclared_inherited_field, ..._rejects_a_field_inherited_from_a_grandparent, ..._still_allows_a_genuinely_new_field, test_create_schema_rejects_a_relationship_colliding_with_an_inherited_relationship, test_add_relationships_only_call_rejects_a_redeclared_inherited_relationship, ..._rejects_a_relationship_inherited_from_a_grandparent, ..._still_allows_a_genuinely_new_relationship, test_extends_retarget_rejects_a_relationship_colliding_with_the_new_parent). Immaterial to code quality, just noting the discrepancy.
  • Nit: validate_no_relationship_redeclaration resolves its ancestor set via NodeService::resolve_relationships (built on resolve_type_chain/store.get_extends_parent_map()), while its field-level twin validate_no_field_redeclaration resolves its ancestor set via the module-local load_parent_map/get_all_schemas(). Both are pre-existing, independently-implemented ways of walking the same underlying "which schema extends which parent" fact (neither introduced by this PR), but this PR further cements reliance on both in parallel. Not a blocker — just a long-term drift risk if the two ever diverge in what they consider a valid/missing chain link.
  • Nit: In the add_relationships block, the new ancestor-collision check (mod.rs ~2093-2097) runs before reject_reserved_relationship_names/validate_edge_field_declarations. A caller that (incorrectly) tries to declare a relationship literally named extends will pass the ancestor check harmlessly (verified: resolve_relationships's inherited set always excludes type-system relationship names via is_type_system_relationship, so rel.name == "extends" can never match anything in inherited) and then get rejected by the more specific, more informative reserved-name error a few lines later — correct end result, just a slightly less direct error path than if the reserved-name check ran first.

Verification notes

  • Confirmed the extends/extended_by edge case called out in the task is genuinely harmless: NodeService::resolve_relationships filters is_type_system_relationship on the ancestor-providing side when building inherited (schema.rs:632-635), so the synthesized extends row that may be present in own_relationships on the checking side can never spuriously self-match.
  • Confirmed the create-schema wiring (handle_create_schema, mod.rs:1213-1217) checks params.relationships (the caller's raw list) before any internal extends row is synthesized, so there's no ordering hazard there either.
  • Ran cargo test -p nodespace-core --lib schema::schema_test:: — 135 passed, 0 failed, consistent with the pre-push gate result already reported for this PR.

Recommendation: APPROVE

The two gaps in issue #2829 are fixed correctly, the gating logic holds up under hand-tracing of every combination I could construct (add-only, retarget-only, add+retarget together), the new validation function correctly parallels its field-level counterpart in both logic and error messaging, and the fix stays tightly scoped to packages/core/src/schema/mod.rs with no scope creep. The three "Suggested Improvements" above are fast-follow material, not blockers — none represent a silent ADR-078 violation slipping through; the one true behavioral edge case (item 1) fails closed rather than open.

@mstomar125 mstomar125 left a comment

Copy link
Copy Markdown
Contributor 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.

…checks

Two real bugs found in review of the ancestor-collision checks added for
issue #2829:

- Both the add_fields and add_relationships ancestor-collision gates in
  handle_update_schema determined the schema's current extends parent
  from the pre-call schema snapshot. A single call combining
  remove_relationships: ["extends"] with add_fields/add_relationships
  was wrongly rejected against the parent it was simultaneously
  detaching from. Fixed by resolving the relationships list (with
  remove_relationships applied) once, ahead of field processing, and
  computing the effective ancestor for both gates from that live list —
  which also removes the duplicated params.extends.is_none() gating
  logic previously repeated at each call site.

- validate_no_relationship_redeclaration discarded resolve_relationships's
  owner map and always blamed the immediate parent for a collision, even
  when the actual declaring schema was a grandparent or further ancestor.
  Now uses the owner map to name the real declaring schema, matching what
  the map exists for per resolve_relationships's own doc comment.

Adds regression tests for both staleness bugs, a test for the correct
grandparent-owner blame, and strengthens three existing tests that only
asserted a substring match on the colliding name (not the additive-only
reason) to also check the reason phrase, so a regression to
immediate-parent-only chain resolution can't hide behind an unrelated
error that happens to name the same field/relationship.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125
mstomar125 merged commit 65be3ff into main Sep 23, 2026
@mstomar125
mstomar125 deleted the issue-2829-adr078-shadowing branch September 23, 2026 05:48
mstomar125 added a commit that referenced this pull request Sep 23, 2026
* Reject extends/extended_by in remove_relationships (closes #2873)

update_schema's remove_relationships is a generic by-name removal over
the same relationship-table-backed declaration list the extends edge
is stored in, so a caller could pass remove_relationships: ["extends"]
to silently clear it — contradicting UpdateSchemaParams::extends's own
documented contract that the edge can only be re-targeted, never
cleared. reject_reserved_relationship_names already blocked a
hand-declared "extends"/"extended_by" relationship in add_relationships;
apply the same rejection (built-in structural names too, for parity)
to remove_relationships's name list before any mutation runs.

Rewrite the two tests that previously asserted remove_relationships
could clear extends (added alongside this back door's discovery in
PR #2866) to assert the rejection instead, add reverse-name and
built-in-name coverage, and correct the now-stale comments describing
remove_relationships as a path that can drop the extends declaration.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Address review: fix undeletable-extending-schema regression

The pragmatic-code-review on PR #2893 found that closing the
remove_relationships back door had a critical side effect: any schema
ever given an extends parent became permanently undeletable, because
count_schema_declaration_edges (packages/core/src/db/sqlite_store/relationships.rs)
counted a schema's own extends declaration as a blocking relationship,
and the only way to previously clear that declaration was the back
door this PR closes.

Fix: exclude a schema's own extends declaration (in_node = the
schema being checked, i.e. it's the declaring/child side) from the
blocking count, while still counting it when the schema is the
target/parent another schema still extends (out_node = the schema
being checked) — deleting a schema deletes its own extends row along
with it, but deleting a schema other schemas still extend would
silently strand those children. Adds
deleting_an_extending_schema_succeeds_but_deleting_its_parent_is_still_blocked
covering both directions.

Also from review: shared the reserved-name classification between
reject_reserved_relationship_names and
reject_reserved_relationship_removal_names (both now call
classify_reserved_relationship_name) instead of duplicating the
built-in/type-system checks; corrected the TypeSystem doc comment to
note extended_by was already an inert no-op pre-PR; and updated the
DELETE_A_SCHEMA skill rule (packages/agent/src/skill_rules.rs) with
an extends exception, regenerating packages/skill/references/cli.md
and the prompt-assembly golden to match.

Two lower-severity review findings intentionally left open, noted as
follow-ups in the PR description: the new removal-name check runs
before the schema-existence check (produces a misleading error for a
nonexistent schema_id + remove_relationships:["extends"] — low
severity, matches existing precedent for other early input-shape
checks in this function); and a suggestion to make the reserved-name
guard structural (e.g. via builtin_exclusion_sql) rather than a
second name-based gate, which is an architecture-level change beyond
this issue's scope.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
mstomar125 added a commit that referenced this pull request Sep 23, 2026
validate_no_field_redeclaration resolved the effective field set via
resolve_effective_fields, which discards per-field ownership, and always
named the immediate extends parent as the "already declared by" schema
in its collision error — even when the true declaring schema was a
grandparent or further ancestor. Switch to
NodeService::resolve_field_owners's owner map, mirroring the fix already
applied to validate_no_relationship_redeclaration in #2866.

Adds a blame assertion to the two existing grandparent-collision tests
(create_schema-with-extends and add_fields-only) confirming the error
names the true declaring schema, not the immediate parent it's merely
inherited through.

Closes #2871.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
mstomar125 added a commit that referenced this pull request Sep 23, 2026
* Fix validate_no_field_redeclaration blaming the wrong ancestor

validate_no_field_redeclaration resolved the effective field set via
resolve_effective_fields, which discards per-field ownership, and always
named the immediate extends parent as the "already declared by" schema
in its collision error — even when the true declaring schema was a
grandparent or further ancestor. Switch to
NodeService::resolve_field_owners's owner map, mirroring the fix already
applied to validate_no_relationship_redeclaration in #2866.

Adds a blame assertion to the two existing grandparent-collision tests
(create_schema-with-extends and add_fields-only) confirming the error
names the true declaring schema, not the immediate parent it's merely
inherited through.

Closes #2871.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Address review: don't misname the schema on a field-owner-resolution failure

validate_no_field_redeclaration's internal-error wrapper named parent_id
as though it were the schema whose lookup failed, even though the DB
failure inside resolve_field_owners can occur on any ancestor further up
the chain. The true failing schema id was still present (nested inside
the underlying NodeServiceError), just prefixed by a wrapper phrase that
read as if parent_id was the one that failed. Reword the wrapper to
describe the chain walk starting from parent_id, not the failing schema.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Address review: add grandparent-collision regression test at the extends re-target call site

The re-target call site's existing collision test only collided with the
immediate new parent, where declaring_schema == new_parent regardless of
whether the blame fix is present — it couldn't distinguish correct blame
from buggy blame at that specific call site. Add a grandparent-collision
test mirroring the ones already covering the create-schema and
add-fields-only call sites, confirming the error names the schema that
actually declares the field, not the immediate new parent.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
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.

ADR-078 shadowing/collision not enforced for relationships, or for add-fields-only schema updates

1 participant