Skip to content

Add NodeService::is_persisted and non-cascading delete_leaf_node - #3860

Merged
mstomar125 merged 2 commits into
mainfrom
issue-596-core-apis
Oct 20, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-596-core-apis

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Closes #3859

What

Two additive APIs that nodespace-sync#596 needs (no existing signature, behaviour or wire type changes; no TS-generated types touched).

  • NodeService::is_persisted(&self, id: &str) -> Result<bool, NodeServiceError>: true only when a row is stored. get_node returns a computed version-1 placeholder for any unstored YYYY-MM-DD id, so a persisted date node was indistinguishable from a virtual one at snapshot 0; this reads the node table directly.
  • NodeService::delete_leaf_node(&self, node_id: &str, expected_version: i64) -> Result<db::LeafDeleteOutcome, NodeServiceError> (store primitive SqliteStore::delete_leaf_atomic): deletes exactly one node and its own edges. The has_child children check runs inside the write transaction, so a child created after the caller collected its subtree yields LeafDeleteOutcome::HasChildren { child_count } and nothing is deleted (the caller re-collects and retries). Same OCC (VersionConflict), access gate (ADR-041, one-node check, bypass-aware) and single NodeDeleted event as delete_node. LeafDeleteOutcome = Deleted | NotFound | HasChildren { child_count: u64 }.

Tests

Six new node_service tests (leaf delete + one event + sibling kept, refusal with children, child attached after version read, version conflict / missing node, access gate refusal, virtual vs persisted date). Each mutation-checked (removing the behaviour fails the test). Existing test_delete_node* still pass. clippy -D warnings and fmt clean; pre-push gate passed.

🤖 Generated with Claude Code

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

APPROVE WITH NITS

Independent review (initial). Checked out in a throwaway worktree; ran the 6 new tests and the 10 existing test_delete_node* tests: 16/16 pass.

(a) Additive: confirmed. The diff is +340/-2. The only touched existing lines are the pub use list in db/mod.rs, which gains LeafDeleteOutcome. No existing signature or behaviour changes. LeafDeleteOutcome derives no Serialize/ts-rs, and nothing is added to nodespace-types, the registry, MCP, CLI or gRPC. No generated TypeScript changes. The sync repo pins core at rev 432bd7c and does not reference either new API yet, so nothing there is affected.

(b) Atomicity: sound.

  • Every writer goes through the single writer mutex (connections.rs). delete_leaf_atomic holds that guard across the children and version recheck, the DELETE and the commit (nodes.rs ~1831-1884). So an in-process create_parent_edge or move onto the leaf cannot interleave. The deferred BEGIN is fine because the connection is exclusive, and delete_subtree_atomic relies on the same thing. A second delete of the same id returns NotFound.
  • Edges: DELETE FROM node removes relationship rows through FK CASCADE on both in_node and out_node. That covers the has_child edge to the parent, incoming mentions and member_of. It is the same mechanism as delete_node and delete_subtree_atomic, so the set is identical. Sibling order is $.order in edge properties (nodes.rs ~615, 747), not pointers, so the neighbours need no repair.
  • The COUNT uses in_node = leaf AND has_child. That is the correct direction (in_node is the parent).

(c) Parity with delete_node: matches.

  • Embedding-root capture, requeue only when root_id != node_id, and VersionConflict via ?? all match.
  • Order is gate, then schema-deletable check, then OCC, which matches delete_node.
  • The FTS and type-ancestry triggers fire on the DELETE.
  • One NodeDeleted event is emitted after commit and only on Deleted.
  • Tombstones: core does not do them on this path.

(d) is_persisted. It reads SELECT 1 FROM node WHERE id=? on a reader, with no virtual-date fallback. A virtual date gives false. A stored date gives true. Archived or soft-state nodes still have a row, so they give true. That is the right "is a row" semantics, and the doc says so.

Findings

  1. Non-blocking, crud.rs ~2651 (delete_leaf_node). The access gate runs before the existence check. delete_node returns existed:false for an absent target before consulting the gate. A sync gate that denies unknown or unreadable ids would turn an idempotent absent-id delete into SubtreeAccessDenied. This contradicts the doc line "an absent node is NotFound". Either check existence first or document it. Gate-then-NotFound is arguably safer against id probing.
  2. Non-blocking, test gap, nodes.rs ~1838. I removed the in-transaction version recheck and all 5 tests still pass. The earlier pre-transaction check masks it. That recheck is the TOCTOU guard and has no test. A deterministic test is hard, since it needs a hook between the two reads, so this is acceptable. Alternatively drop the redundant pre-transaction OCC check and rely on the in-transaction one only.
  3. Non-blocking, nodes.rs ~1818. There is a redundant pre-transaction get_node, plus assert_schema_deletable before the lock. This mirrors delete_subtree_atomic, but the recheck inside the transaction already returns NotFound. Fine as is.
  4. Non-blocking, mod.rs tests. Not covered:
    • the sync bypass path;
    • the embedding requeue (the claim in the PR description);
    • a leaf that has mentions or incoming edges;
    • a schema node (schema_has_declarations).
      The one-event test also only asserts the ids of events that arrive within a 500ms window each.
  5. Non-blocking, crud.rs ~2682. If get_embedding_root_id fails on a HasChildren or NotFound outcome, nothing is requeued. That is correct.

(e) Mutation check.

  • Disabling the children check makes 2 tests fail: refuses_a_node_with_children and sees_a_child_attached_after.... Good.
  • Disabling the in-transaction version recheck makes none fail. See finding 2.

(f) Docs and comments are accurate apart from finding 1.

No blocking issues.

mstomar125 and others added 2 commits October 20, 2026 14:37
is_persisted(id) reports whether a node is a stored row, without the
virtual-date fallback get_node applies to unstored YYYY-MM-DD ids.

delete_leaf_node(id, expected_version) deletes exactly one node and its own
edges, checking version and has_child children inside the write transaction;
a node with children yields LeafDeleteOutcome::HasChildren and nothing is
deleted. Same access gate, OCC error and NodeDeleted event as delete_node.
SqliteStore::delete_leaf_atomic is the store-level primitive.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… edge, bypass, schema and requeue tests

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@mstomar125
mstomar125 merged commit b371cae into main Oct 20, 2026
@mstomar125
mstomar125 deleted the issue-596-core-apis branch October 20, 2026 12:37
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.

NodeService: is_persisted and non-cascading delete_leaf_node (for sync#596)

1 participant