Skip to content

C3b: Enforce container rule (canHaveChildren) in the daemon; source the frontend UI gate from schema - #1264

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1260-c3b-container-rules
May 30, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1260-c3b-container-rules

Conversation

@malibio

@malibio malibio commented May 30, 2026

Copy link
Copy Markdown
Collaborator

Closes #1260

@malibio

malibio commented May 30, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review — C3b: Enforce container rule (canHaveChildren) in the daemon

Verdict: Approve with one suggested improvement

This is a clean, well-scoped enforcement change. All acceptance criteria from #1260 are met, all 133 Rust tests and 3876 frontend Vitest tests pass clean. The architectural intent — daemon is authoritative, frontend keeps a derived synchronous gate — is correctly implemented.


Findings

Suggested Improvements

upsert_node_with_parent is missing the container guard (packages/core/src/services/node_service/crud.rs:1392)

This function is the third parent-setting path in the service, alongside move_node (guarded) and create_node_with_parent (guarded). It currently auto-creates the parent as a date node if missing, then calls store.move_node directly — bypassing the !can_have_children() check. Since the function is exposed via the gRPC UpsertNodeWithParent RPC (mapped in packages/daemon/src/services/node_service.rs:451), an agent or Tauri command calling save_node_with_parent can still place a child under a leaf node.

Granted, upsert_node_with_parent is flagged with a deprecated _root_id parameter and its Tauri caller (save_node_with_parent) does not appear to be called from any current frontend code — but it remains a live RPC endpoint. Adding the same behavior lookup + NotAContainer guard after the parent-existence check (around line 1430) would close this gap consistently and is low-effort given the pattern is already established twice in this PR.

Rollback test is under-specified (packages/desktop-app/src/tests/services/reactive-node-service.test.ts:1497)

The test only asserts expect(events.hierarchyChanged).toHaveBeenCalled(), which is also satisfied by the successful optimistic update path. It does not distinguish whether the rollback branch executed. A stronger assertion — e.g., toHaveBeenCalledTimes(2) (once for optimistic, once for rollback) or checking that the node's depth/parent returns to its original value — would verify the rollback contract, not just that the indent ran. The existing rollback does fire (code at lines 966-977 is correct), but the test does not assert it.


Nitpicks

  • Nit: packages/core/src/services/node_service/crud.rs:296-300 — the unwrap_or_else(|| Arc::new(CustomNodeBehavior::new(...))) fallback is copy-pasted identically in move_node and create_node_with_parent (and would be needed again in upsert_node_with_parent). A private helper like fn behavior_for(&self, node_type: &str) -> Arc<dyn NodeBehavior> would DRY this up and make future changes to the fallback logic a single-site edit. This is a minor refactor and not blocking.

  • Nit: The parity test's RUST_CAN_HAVE_CHILDREN map hardcodes checkbox: true with the comment "no Rust behavior → CustomNodeBehavior default = true". This is correct today, but if a CheckboxNodeBehavior is ever added with can_have_children() = false, the comment will silently mislead. Worth noting that the test itself will catch the divergence — the comment is the only hazard.


Acceptance Criteria Status

  • MoveNode (and create-with-parent) RPC rejects placing a child under a non-container — verified by 4 Rust integration tests
  • Frontend canHaveChildren predicate derives from the same source as the daemon — enforced by the parity test in core-plugins.test.ts
  • Indent UI disables synchronously (synchronous gate at reactive-node-service.svelte.ts:853 unchanged, no round-trip)
  • Daemon container rejection rolls back the optimistic indent — rollback code path confirmed at lines 966-977; test coverage is weak but the path is correct
  • upsert_node_with_parent path unguarded (not in original AC scope, but creates an inconsistency)

Test Execution

All tests green:

  • cargo test -p nodespace-core: 133 passed, 0 failed
  • cargo test -p nodespace-daemon "not_a_container": 1 passed
  • bun run test: 3876 passed (132 test files), 0 failed

…ate (closes #1260)

## Daemon enforcement
- Add `NodeServiceError::NotAContainer { parent_id, node_type }` variant with constructor
- `move_node`: look up new parent's behavior; reject with `NotAContainer` when `!can_have_children()`
- `create_node_with_parent`: same guard in Step 2 (parent validation), using the behavior registry
- Map `NotAContainer` → `OpsError::ValidationFailed` in `ops/mod.rs`
- `service_error_to_status` already maps `ValidationFailed` → `InvalidArgument` via the generic path

## Frontend gate
- `pluginRegistry.canHaveChildren` at `indentNode:853` remains as the synchronous UX gate
- Existing rollback at lines 966-977 handles daemon rejections; confirmed it fires for non-ignorable errors

## Tests
- 4 Rust integration tests: `move_node` rejects non-container parent, accepts container; same for `create_node_with_parent`
- 1 daemon error-mapping test: `NotAContainer` → `InvalidArgument`
- 1 Vitest parity test: every registered core plugin's `canHaveChildren` matches the Rust `can_have_children()` ground truth
- 2 Vitest behavioral tests: container gate returns false for non-container prevSibling; rollback fires on non-ignorable moveNode rejection

Co-Authored-By: Claude <[email protected]>
@malibio
malibio force-pushed the worktree-issue-1260-c3b-container-rules branch from 9bd6bc9 to f79b8af Compare May 30, 2026 13:43
…trengthen rollback test

- Add NotAContainer guard to upsert_node_with_parent (third parent-setting RPC path)
- Extract behavior_for() helper on NodeService; replace 3 inline unwrap_or_else fallbacks
- Rollback test: await waitForPendingMoveOperations() to drain background move, then
  assert depth returns to pre-indent value rather than a brittle call-count check

Addresses reviewer recommendations from PR #1264.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented May 30, 2026

Copy link
Copy Markdown
Collaborator Author

Review addressed

All three reviewer recommendations implemented:

✅ upsert_node_with_parent container guard — Added behavior_for(parent_type).can_have_children() check after the parent-ensure block. The parent is now re-fetched via store.get_node to get its type, then rejected with NotAContainer if it's a leaf. Closes the third parent-setting RPC path.

✅ behavior_for helper — Extracted pub(crate) fn behavior_for(&self, node_type: &str) on NodeService (next to is_embeddable_type). Replaced the 3 inline unwrap_or_else(|| Arc::new(CustomNodeBehavior::new(...))) blocks in move_node, create_node_with_parent, and upsert_node_with_parent.

✅ Stronger rollback test — Test now imports waitForPendingMoveOperations from $lib/services/pending-operations, awaits it after indentNode to drain the background move promise, then asserts depthAfter === depthBefore (the node's depth reverts). Count-based assertion dropped in favor of state-based assertion (more robust against additional hierarchyChanged firings from setExpanded).

All tests green: 133 Rust tests, 3862 Vitest tests.

@malibio
malibio merged commit 3f7fb24 into main May 30, 2026
@malibio
malibio deleted the worktree-issue-1260-c3b-container-rules branch May 30, 2026 13:55
malibio added a commit that referenced this pull request Jun 22, 2026
…he frontend UI gate from schema (#1264)

* C3b: Enforce container rule in daemon; add parity test for frontend gate (closes #1260)

## Daemon enforcement
- Add `NodeServiceError::NotAContainer { parent_id, node_type }` variant with constructor
- `move_node`: look up new parent's behavior; reject with `NotAContainer` when `!can_have_children()`
- `create_node_with_parent`: same guard in Step 2 (parent validation), using the behavior registry
- Map `NotAContainer` → `OpsError::ValidationFailed` in `ops/mod.rs`
- `service_error_to_status` already maps `ValidationFailed` → `InvalidArgument` via the generic path

## Frontend gate
- `pluginRegistry.canHaveChildren` at `indentNode:853` remains as the synchronous UX gate
- Existing rollback at lines 966-977 handles daemon rejections; confirmed it fires for non-ignorable errors

## Tests
- 4 Rust integration tests: `move_node` rejects non-container parent, accepts container; same for `create_node_with_parent`
- 1 daemon error-mapping test: `NotAContainer` → `InvalidArgument`
- 1 Vitest parity test: every registered core plugin's `canHaveChildren` matches the Rust `can_have_children()` ground truth
- 2 Vitest behavioral tests: container gate returns false for non-container prevSibling; rollback fires on non-ignorable moveNode rejection

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

* Address review: guard upsert_node_with_parent, DRY behavior lookup, strengthen rollback test

- Add NotAContainer guard to upsert_node_with_parent (third parent-setting RPC path)
- Extract behavior_for() helper on NodeService; replace 3 inline unwrap_or_else fallbacks
- Rollback test: await waitForPendingMoveOperations() to drain background move, then
  assert depth returns to pre-indent value rather than a brittle call-count check

Addresses reviewer recommendations from PR #1264.

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

---------

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

C3b: Enforce container rule (canHaveChildren) in the daemon; source the frontend UI gate from schema

1 participant