Repository navigation
Enforce ADR-078 field/relationship shadowing on more write paths - #2866
Conversation
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]>
Code Review SummaryThis PR closes two concrete gaps in ADR-078 (" 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. FindingsCritical IssuesNone. Suggested Improvements
Nitpicks
Verification notes
Recommendation: APPROVEThe 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 |
mstomar125
left a comment
There was a problem hiding this comment.
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]>
* 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]>
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]>
* 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]>
Summary
validate_no_field_redeclaration's enforcement toupdate_schema's plainadd_fields-only path (noextendskey in the call). Previously that check only ran fromcreate_schemawithextends, and fromupdate_schema'sextendsre-target — a later, separateadd_fieldscall on an already-extending schema could silently redeclare an inherited field with a diverged type.validate_no_relationship_redeclaration, the relationship counterpart to the field check, built onNodeService::resolve_relationships(the chain-merged effective relationship set). Wires it intohandle_create_schema,update_schema'sadd_relationshipspath, andupdate_schema'sextendsre-target path — closing the gap whereresolve_relationships's own doc comment described "nearest-first, first-declared wins" shadowing as the expected rule, but no write path ever rejected the collision.update_schemacall is also re-targetingextends, since that path already re-checks the fully-merged field/relationship list against the new parent.Test plan
packages/core/src/schema/schema_test.rscovering: anadd_fields-only call rejecting a redeclared inherited field (both immediate-parent and grandparent collisions), a non-collidingadd_fields-only call still succeeding,create_schemarejecting a relationship colliding with an inherited one, anadd_relationships-only call rejecting a redeclared inherited relationship (immediate-parent and grandparent), a non-collidingadd_relationships-only call still succeeding, and anextendsre-target rejecting a relationship collision with the new parent.cargo test -p nodespace-core --lib schema::schema_test— 135 passedcargo 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 regressionscargo clippy -p nodespace-core --lib --tests -- -D warnings— cleanbun run quality:fix— cleanbun run test:all+cargo build --bin nodespaced+test:e2e) — all greenCloses #2829.
🤖 Generated with Claude Code