Repository navigation
Consolidate extends-chain parent-map resolution onto one edge source - #2933
Conversation
…test, document tradeoffs Review on PR #2933 surfaced three findings: - SchemaNode::get_relationship (models/schema_node.rs) lost its only production caller when resolve_declared_relationship was consolidated onto resolve_relationships, leaving it reachable only from tests. Deleted, and its 5 test call sites in schema_test.rs inlined to the equivalent relationships.iter().find(...). - The new single-source-of-truth regression test only compared sorted field names between resolve_effective_fields and resolve_field_owners, which can't catch the two resolvers agreeing on names while disagreeing on a field's type/protection/required/core_values/etc. Strengthened to also compare every field's full serialized form (SchemaField doesn't derive PartialEq — it's a nodespace-types wire type used far more broadly than this test, so reusing Serialize avoids widening that type just for this assertion) and gave Root's test field real attributes (required, an enum vocabulary) for the comparison to have something to diverge on. - resolve_declared_relationship's doc comment now spells out the two behavioral consequences of delegating to resolve_relationships instead of hand-walking with an early exit: every chain scope is now read before the nearest match is picked (no more stopping at the first declaring scope), and a real DB error from a farther, uninvolved ancestor now propagates where it previously wouldn't have been reached. Both are accepted rather than incidental — extends chains are shallow by construction, and every other single-name lookup through resolve_relationships/resolve_field_owners already pays this same cost (check_node_completeness, find_duplicate_for) — so this brings relationship creation in line with the rest of the codebase rather than leaving it as the one outlier with a bespoke early exit. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…test, document tradeoffs Review on PR #2933 surfaced three findings: - SchemaNode::get_relationship (models/schema_node.rs) lost its only production caller when resolve_declared_relationship was consolidated onto resolve_relationships, leaving it reachable only from tests. Deleted, and its 5 test call sites in schema_test.rs inlined to the equivalent relationships.iter().find(...). - The new single-source-of-truth regression test only compared sorted field names between resolve_effective_fields and resolve_field_owners, which can't catch the two resolvers agreeing on names while disagreeing on a field's type/protection/required/core_values/etc. Strengthened to also compare every field's full serialized form (SchemaField doesn't derive PartialEq — it's a nodespace-types wire type used far more broadly than this test, so reusing Serialize avoids widening that type just for this assertion) and gave Root's test field real attributes (required, an enum vocabulary) for the comparison to have something to diverge on. - resolve_declared_relationship's doc comment now spells out the two behavioral consequences of delegating to resolve_relationships instead of hand-walking with an early exit: every chain scope is now read before the nearest match is picked (no more stopping at the first declaring scope), and a real DB error from a farther, uninvolved ancestor now propagates where it previously wouldn't have been reached. Both are accepted rather than incidental — extends chains are shallow by construction, and every other single-name lookup through resolve_relationships/resolve_field_owners already pays this same cost (check_node_completeness, find_duplicate_for) — so this brings relationship creation in line with the rest of the codebase rather than leaving it as the one outlier with a bespoke early exit. Co-Authored-By: Claude Sonnet 5 <[email protected]>
09eca29 to
a7d1d6d
Compare
load_parent_map (schema/mod.rs) rebuilt the schema-extends parent map by loading and hydrating every schema node via get_all_schemas() and reading each one's declaration back off declared_parent()'s JSON-parsed SchemaRelationship, independently of NodeService::resolve_type_chain's single get_extends_parent_map() query over the same edges read directly off indexed relationship columns. Two independently-derived views of the same extends edges, with nothing guarding that they stayed in agreement. Point load_parent_map at get_extends_parent_map() directly. resolve_declared_relationship (relationship.rs) hand-walked the extends chain with its own per-scope get_schema_node + SchemaRelationship:: get_relationship lookups instead of calling the existing resolve_relationships helper, which already performs the identical nearest-first chain walk via the shared flatten_chain_by_name generalization. Delegate to it instead. This also closes a gap the old hand-walk carried: resolve_relationships excludes the extends/extended_by type-system bookkeeping row, which SchemaNode::get_relationship did not — naming "extends" as a relationship_name previously resolved a schema's own extends declaration as if it were a real relationship a data-level edge could be created under. Adds a regression test pinning that resolve_effective_fields (schema layer) and NodeService::resolve_field_owners agree on the effective field set for the same extends chain, and a test pinning that create_relationship now rejects "extends" as a declared relationship name the way any other undeclared name is rejected. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…test, document tradeoffs Review on PR #2933 surfaced three findings: - SchemaNode::get_relationship (models/schema_node.rs) lost its only production caller when resolve_declared_relationship was consolidated onto resolve_relationships, leaving it reachable only from tests. Deleted, and its 5 test call sites in schema_test.rs inlined to the equivalent relationships.iter().find(...). - The new single-source-of-truth regression test only compared sorted field names between resolve_effective_fields and resolve_field_owners, which can't catch the two resolvers agreeing on names while disagreeing on a field's type/protection/required/core_values/etc. Strengthened to also compare every field's full serialized form (SchemaField doesn't derive PartialEq — it's a nodespace-types wire type used far more broadly than this test, so reusing Serialize avoids widening that type just for this assertion) and gave Root's test field real attributes (required, an enum vocabulary) for the comparison to have something to diverge on. - resolve_declared_relationship's doc comment now spells out the two behavioral consequences of delegating to resolve_relationships instead of hand-walking with an early exit: every chain scope is now read before the nearest match is picked (no more stopping at the first declaring scope), and a real DB error from a farther, uninvolved ancestor now propagates where it previously wouldn't have been reached. Both are accepted rather than incidental — extends chains are shallow by construction, and every other single-name lookup through resolve_relationships/resolve_field_owners already pays this same cost (check_node_completeness, find_duplicate_for) — so this brings relationship creation in line with the rest of the codebase rather than leaving it as the one outlier with a bespoke early exit. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…kups, cut redundant owners resolve get_relationship_mut had zero callers after the prior round deleted its sibling get_relationship — delete it too rather than leaving a structurally identical piece of dead code three lines below. schema_test.rs: replace 5 inline .relationships.iter().find(|r| r.name == "...") call sites with one test-local find_relationship helper, now that get_relationship's deletion left them duplicating the same lookup idiom. relationship.rs: resolve_declared_relationship built resolve_relationships's owners map and discarded it. create_relationship_in_tx's reverse_cardinality: One branch then re-derived the same owners map for the same schema_id with a second, redundant full-chain resolve_relationships call, inside the same open write transaction. Return the owners map from resolve_declared_relationship and reuse it instead, halving that transaction's chain resolutions without reintroducing a short-circuit hand-walk. The broader tradeoff (always resolving every scope rather than stopping at the nearest match) stays as documented: extends chains are shallow by construction, a short-circuit-then-fallback variant would reintroduce the duplicated per-scope walk this PR exists to remove, and the only real production extends declaration (the built-in linear methodology) is a single level deep. Co-Authored-By: Claude Sonnet 5 <[email protected]>
The pre-push gate's check-code-references ratchet caught this: a core#NNNN citation in a code comment, against CLAUDE.md's "describe the behavior/constraint directly" rule. Describe the deletion/fold-in directly instead of citing the issue. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…ionship's new return shape PR #2937 (merged to main after this branch's base) added enforce_cardinality_after_merge_in_tx, which calls resolve_declared_relationship expecting its old SchemaRelationship-only return. Rebasing surfaces the resulting compile break from this PR's own signature change (now returns the owners map alongside the match). Its reverse branch had the identical redundant-resolve pattern already fixed elsewhere in this file: it discarded resolve_declared_relationship's owners map, then immediately re-derived the same map for the same source.node_type via a second full-chain resolve_relationships call. Reuse the owners map from the tuple instead, for the same reason as the create_relationship_in_tx fix this mirrors. Co-Authored-By: Claude Sonnet 5 <[email protected]>
a7d1d6d to
88b9469
Compare
Pragmatic Code Review — PR #2933 (issue #2888)Review Type: Re-Review (rebase resolution onto origin/main Previous Review Summary:
Rebase-resolution checks
Verification run (in the worktree)
Code Review FindingsNone at 🔴/🟡/🟢 confidence worth blocking or nit-picking on. Recommendation: APPROVE (posted as a comment — self-authored PR) |
mstomar125
left a comment
There was a problem hiding this comment.
Pragmatic Code Review — PR #2933 (issue #2888)
Review Type: Re-Review (rebase resolution onto origin/main 0bc01e2d, head 88b9469f)
Previous Review Summary:
- Earlier rounds' recommendations (delete orphaned
get_relationship_mut, dedupe test lookups, cut redundant owners resolve, drop issue-number reference in doc) are all present in the rebased diff. - New issues found: 0
Rebase-resolution checks
-
declared_ownerskeying after the in→forward rewrite — correct.
📁packages/core/src/services/node_service/relationship.rs:1126-1131, 1210, 1230-1250, 1355-1365
forward_endpointsrebindssource_id/relationship_nameat the top ofwrite_relationship_in_tx, beforesourceis loaded;schema_id = &source.node_typeis therefore the forward source's type, andresolve_declared_relationship(schema_id, relationship_name)is called with exactly the pair the old secondresolve_relationships(schema_id)+owners.get(relationship_name)used. Neither binding is shadowed between the match and thereverse_cardinality: Onebranch. The owners map returned is the untouchedresolve_relationshipsowners map (same chain, same nearest-firstor_insert), so the reuse is behaviour-identical — it just drops one redundant full-chain resolve inside the open write tx. Theenforce_cardinality_after_merge_in_txreverse branch (:1652-1676) does the same withsource.node_type/relationship_type— also identical. -
Weekend behaviour — no regressions found.
- In-direction normalization (#2951): the
match (&forward_name, resolved)guard and its "inbound view of …" error are preserved verbatim; only the tuple is threaded through. resolve_relationshipsfiltersis_type_system_relationship, so a rewrittenextended_by→extendsnow hits the mismatch error rather than resolving; unreachable anyway because the forward source is a schema and is refused just above ("typed relationships between schemas are declarations").- Stored-edge labels (#3176), last-inbound-edge protection, delete refusal (#3095, now in
assert_schema_deletableat the store layer): none touch the changed lines.
- In-direction normalization (#2951): the
-
load_parent_map→get_extends_parent_map()— equivalent.
📁packages/core/src/schema/mod.rs:1034-1042
Theextendsdeclaration row is theextendsrelationship row between two schema nodes, soSELECT in_node, out_node WHERE relationship_type='extends'describes the same mapdeclared_parentderived by hydrating every schema. Its only callers are the extends-cycle check (:1091) andresolve_effective_fields(:1254); the delete-refusal path doesn't use it, and no caller overlays apendingedge (thePendingSchemaoverlay at:457works on chains, not this map) — the removed doc paragraph describing apendingparameter was already stale. -
Deleted
SchemaNode::get_relationship/_mut— no callers on main (grep ofpackages/clean). No duplicated work on main: the threeresolve_declared_relationshipcallers are all in this file and all adapted. -
schema_chain_blindness_guard_test— passes; no stale/unlisted allowlist entries.
Verification run (in the worktree)
cargo test -p nodespace-core --test relationship_in_name_normalization_test --test relationship_extends_chain_test --test schema_chain_blindness_guard_test --test rel_ops_forward_name_extends_chain_test --test node_completeness_extends_chain_test— all pass (18/7/6/4/2).cargo test -p nodespace-core --lib -- schema:: relationship— 322 passed (includes #3095's delete-refusal tests inschema_test.rs).
Code Review Findings
None at 🔴/🟡/🟢 confidence worth blocking or nit-picking on.
Recommendation: APPROVE (posted as a comment — self-authored PR)
Address-review summary (post-rebase round)Rebased onto main Rebase resolution: one textual conflict, in Dropped as redundant: nothing. Main still had Review: APPROVE, 0 findings. Nothing to address. Tests: Re-Review DecisionDecision: NO RE-REVIEW NEEDED. The review had no findings, so no code changed in this round. 🤖 Generated with Claude Code |
Summary
Closes #2888. Consolidates two of the three duplicated extends-chain walkers the issue identified onto the single edge source
NodeService::resolve_type_chainalready uses (SqliteStore::get_extends_parent_map), instead of leaving each caller with its own independently-derived view of the sameextendsedges.1.
load_parent_map(packages/core/src/schema/mod.rs:736)Previously rebuilt the parent map by calling
get_all_schemas()(a heavier query: all schema nodes + a JOIN over every declaration row) and reading each schema's parent back offextends_chain::declared_parent, which parses the declaration row'spropertiesJSON into aSchemaRelationship.Now delegates directly to
node_service.store().get_extends_parent_map()— the same single, cheaper query (SELECT in_node, out_node FROM relationship WHERE relationship_type = 'extends', reading the indexed columns directly rather than parsing JSON) thatNodeService::resolve_type_chain, the playbook engine, andquery.rsalready use.Why this was safe to consolidate: I traced
load_parent_mapandget_extends_parent_mapback to the same PR (#2739) that introduced both — there was no documented reason for the two different sources, and no other caller ofload_parent_mapreads anything else off theget_all_schemas()result. The two already had to describe the same edges (anextendsrow is only ever written byset_schema_declarationswithin_node= the declaring schema), so this removes duplication with no intended behavior change.get_extends_parent_map's own doc comment already called this out explicitly: readingin_node/out_nodedirectly "is what the closure queries below walk, so a disagreement between the two would be a bug this avoids having to detect" — i.e. the JSON-parsing path could in principle silently drop a malformed-but-still-structurally-valid declaration row that the column-based query would not.2.
resolve_declared_relationship(packages/core/src/services/node_service/relationship.rs:488)Previously hand-walked
resolve_type_chainitself, callingget_schema_nodeper scope andSchemaNode::get_relationship(name)(a lookup over that scope's own, un-filteredrelationshipslist) until the first match.Now delegates to the existing
resolve_relationshipshelper, which performs the identical nearest-first walk via the already-sharedflatten_chain_by_namegeneralization (core#2830/PR#2862).Behavior change, called out explicitly:
resolve_relationshipsdeliberately excludes theextends/extended_bytype-system bookkeeping row (see its own doc comment) —SchemaNode::get_relationshipdid not. Before this change, naming"extends"as arelationship_nametocreate_relationshipwould resolve a schema's ownextendsdeclaration as if it were a real, creatable relationship, and (nothing else increate_relationshiprejects the type-system names) would let a caller create a bogus data-levelextendsedge between two ordinary node instances. That path was never reachable through any real caller and had no test coverage either way — this closes it rather than preserving it, consistent withresolve_relationships's own stated rationale for the exclusion.Sub-change 1 vs 2
Both turned out to be small, low-risk, mechanical swaps once traced — no third case emerged where the two paths' behavior needed to diverge for a real reason. Landed together in one PR/commit rather than split, per the issue's own "fine to do them as two commits or complete only the safer one" guidance — here both were equally safe once verified.
Not touched
Per the issue's scope:
flatten_chain_by_name/resolve_field_owners/resolve_relationships's own bodies (schema.rs) are untouched — this PR only changes the two callers' chain-resolution setup that the issue named.Tests
test_resolve_effective_fields_agrees_with_resolve_field_owners_across_extends_chain(schema/schema_test.rs) — builds a 3-level extends chain and assertsresolve_effective_fields(schema layer) andNodeService::resolve_field_owners(usingresolve_type_chain) resolve the identical effective field set and chain order. This is the "single source of truth" regression guard the issue asked for on the field side.create_relationship_rejects_the_extends_type_system_name(tests/relationship_extends_chain_test.rs) — pins the type-system-exclusion fix:create_relationship(..., "extends", ...)on a node whose schema genuinely has anextendsdeclaration is now rejected the same way any other undeclared relationship name is.cargo test -p nodespace-core— 1720 lib tests + all integration test binaries (includingrelationship_extends_chain_test,schema::,services::node_service::,playbook::) — all pass, 0 failed.test:all+cargo build --bin nodespaced+ full Tauri-seamtest:e2e) passed clean on push.bun run quality:fixclean (rustfmt +cargo clippy --all-targets -D warnings -D clippy::unused-async+ eslint/tsc/svelte-check), no unrelated formatting diffs.🤖 Generated with Claude Code