Skip to content

Surface access-gated cascade-delete refusal as a typed error + modal (nodespace-sync#413 core half) - #2029

Merged
mstomar125 merged 2 commits into
mainfrom
issue-413-subtree-access-refusal
Aug 11, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-413-subtree-access-refusal

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

The core half of sync#413. The SubtreeAccessGate seam (ADR-041, core#1870) already refuses a cascade delete whose subtree contains nodes the caller can't read — but it mapped that refusal to a generic HierarchyViolation → INVALID_ARGUMENT, indistinguishable from ordinary validation, so the frontend couldn't tell a refusal apart to show anything useful. This makes the refusal a first-class, user-visible outcome, threaded end-to-end, and restores the optimistically-removed node when it happens.

Changes

Backend

  • NodeServiceError::SubtreeAccessDenied { inaccessible_count } — a dedicated variant (not the shared bare-string HierarchyViolation, which is also used for real hierarchy errors like "cannot move root"). The gate's Denied branch in delete_node returns it.
  • OpsError::SubtreeAccessDenied → mapped in the daemon's ops_error_to_status to Status::failed_precondition with the count in x-subtree-inaccessible-count metadata — mirroring exactly how a version conflict carries x-version-conflict. Distinct from INVALID_ARGUMENT (validation) and ABORTED (OCC).

Frontend

  • status_to_command_error → a SUBTREE_ACCESS_DENIED command code carrying { inaccessibleCount }; an isSubtreeAccessDenied type-guard (mirroring isVersionConflict).
  • A global refusal modal (subtree-access-denied-modal.svelte, mounted in +layout.svelte next to the delete-confirmation modal), triggered via a small $state store — "N item(s) not visible to you — nothing was deleted."
  • Optimistic-restore: the delete removes the node locally before the backend call, so on a refusal the store re-adds exactly what it stripped (node, version, persisted-set membership, subscriber notify) and shows the modal. Descendants are only removed via post-commit events — which never fire on a refusal — so restoring the target node is sufficient. Non-refusal errors keep today's behavior.

Safety

  • The community / no-Pro path is unchanged: the default AlwaysAllowGate never denies, so none of this branch is reachable there.
  • The gate check happens before delete_subtree_atomic; a Denied returns the error with no mutation. Version-conflict still takes precedence (existing test preserved).

Scope / what follows separately

This is the core half only. The daemon-side ProSubtreeAccessGate that actually calls the live cloud subtree_inaccessible_count over PostgREST lives in nodespace-sync and is blocked on a core-pin bump there (the pinned rev predates the SubtreeAccessGate seam). That gate + a core-pin bump + live Pro/RLS validation are the remaining work on #413. The deployed cloud function is root-only (server-side has_child walk; raises 42501 if the root itself is unreadable), so the gate passes just the root — no client-supplied id list (which also closes the count-fishing vector the threat model flagged).

Tests

  • Updated gate tests assert the new SubtreeAccessDenied { inaccessible_count } (and that version-conflict still wins; fully-readable/atomic/idempotent tests unchanged). cargo test -p nodespace-core --lib 1138 pass.
  • New daemon test: OpsError::SubtreeAccessDenied → failed_precondition + x-subtree-inaccessible-count.
  • Frontend: isSubtreeAccessDenied guard cases + a restore test driving the real SharedNodeStore (vi.spyOn on backendAdapter, no singleton vi.mock) — asserts restore + modal on refusal, and that a non-refusal error leaves the node deleted. bun run test 4555 pass; lint + svelte-check clean.

Part of #413.

🤖 Generated with Claude Code

mstomar125 and others added 2 commits August 11, 2026 11:41
… + modal

The SubtreeAccessGate seam (ADR-041) already refused a cascade delete whose
subtree contains nodes the caller can't read, but mapped it to a generic
HierarchyViolation → INVALID_ARGUMENT, indistinguishable from ordinary
validation — so the frontend couldn't tell a refusal apart. Make it a
first-class, user-visible outcome:

- New NodeServiceError::SubtreeAccessDenied { inaccessible_count } (not the
  shared bare-string HierarchyViolation); the gate's Denied branch returns it.
- New OpsError::SubtreeAccessDenied → maps to gRPC Status::failed_precondition
  with the count in x-subtree-inaccessible-count metadata (mirroring how a
  version conflict carries x-version-conflict), so the status is distinct from
  validation and from OCC.
- Frontend: status_to_command_error → a SUBTREE_ACCESS_DENIED command code
  carrying { inaccessibleCount }; an isSubtreeAccessDenied guard; a global
  refusal modal ("N item(s) not visible — nothing was deleted").
- The delete is optimistic (the node is removed locally before the backend
  call), so on a refusal the store restores exactly what the optimistic removal
  stripped (node, version, persisted-set membership, subscriber notify) and
  shows the modal. Descendants are only removed via post-commit events, which
  never fire on a refusal, so restoring the target node is sufficient.

Community/no-Pro path is unchanged (AlwaysAllowGate never denies). This is the
core half of sync#413; the daemon-side gate that actually calls the cloud
subtree_inaccessible_count, and live validation, follow separately.

Part of #413.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…code

Two real defects from review:

- The optimistic restore lived only at the store layer, but the reactive view
  service owns `_rootNodeIds` (the top-level view's source of truth) and strips
  the node from it synchronously — so a refused top-level delete restored the
  store yet the node stayed gone from the UI. The store's deleteNode now takes an
  `onRefused` callback it invokes (after restoring its own state) so the view
  service restores `_rootNodeIds`/`_uiState` too. The store delete is
  fire-and-forget, so this callback is how the refusal (which lands async)
  reaches the view layer. Covered by store-level onRefused tests (fires on a
  refusal, not on success or a non-refusal error).

- status_to_command_error branded EVERY FailedPrecondition as
  SUBTREE_ACCESS_DENIED, but the daemon returns FailedPrecondition from unrelated
  paths (node-create / schema failures). Now the code is assigned only when the
  x-subtree-inaccessible-count metadata is actually present; otherwise it falls
  through to GRPC_ERROR.

Also (minor): renamed the version-conflict precedence test to say what it
verifies (conflict reported when the gate allows) and added a genuine both-true
test asserting the access refusal PRECEDES the version conflict — the gate runs
before the OCC check, so we never leak version state on an access denial.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE (self-review, mstomar125)

Two-pass adversarial review of this security path.

Core abort integrity confirmed first pass: the gate runs strictly before delete_subtree_atomic, the same collect_subtree_ids set feeds both the gate and the delete (scope == access-check scope), and a Denied returns the new typed error with no mutation. Status/metadata threading mirrors x-version-conflict losslessly.

First pass found two real MAJORs the delegated build's own tests masked — (1) the optimistic restore lived only at the store layer, so a refused top-level delete restored the store but left the node gone from the UI (_rootNodeIds); (2) status_to_command_error branded every FailedPrecondition as the refusal code. Both fixed in 62327e77: a store onRefused callback restores the view layer's _rootNodeIds/_uiState (unit-tested to fire only on a genuine refusal), and the refusal code is now gated on the count metadata being present. Also renamed a misleading precedence test and added a genuine both-true test proving the access refusal precedes the OCC check (no version-state leak on an access denial).

Second pass verified all three fixes correct and complete, no new defects. Gate green on both pushes; core lib 1139, daemon, frontend 4557 tests; lint + svelte-check clean.

Part of nodespace-sync#413 (core half). The daemon-side gate + core-pin bump + live Pro/RLS validation follow separately.

@mstomar125
mstomar125 merged commit c15d90c into main Aug 11, 2026
@mstomar125
mstomar125 deleted the issue-413-subtree-access-refusal branch August 11, 2026 10:02
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.

1 participant