Skip to content

Bound GetChildrenTree's subtree payload with an actionable error - #2493

Merged
malibio merged 2 commits into
mainfrom
issue-2108-diagnostics-grpc-limits
Sep 8, 2026
Merged

malibio merged 2 commits into
mainfrom
issue-2108-diagnostics-grpc-limits

Conversation

@malibio

@malibio malibio commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Summary

Closes #2108. The diagnostics count/unknown-vs-zero, count-only RPCs, bounded ordered recent-node query, and the configured 64MiB gRPC message-size ceiling were already fixed by #2294/#2319. This PR addresses the one remaining defect from the issue: get_children_tree has no node-count bound, only a depth bound (MAX_TREE_DEPTH), so a wide-but-shallow subtree can still produce a payload the gRPC transport rejects with an opaque OutOfRange at decode time — on the app's hot path for opening a document.

The frontend eagerly loads a document's full tree in one call with no lazy-expand fallback, so truncating the response would silently drop content. Per the issue's stated preference, this states and enforces a maximum document size instead of raising the limit or truncating:

  • Add MAX_TREE_NODES (20,000) next to the existing MAX_TREE_DEPTH guard, checked in get_children_tree before the tree is built.
  • Add NodeServiceError::TreeTooLarge → OpsError::TreeTooLarge → a RESOURCE_EXHAUSTED gRPC status carrying node_id/count/max in metadata (mirrors the existing SubtreeAccessDenied → FAILED_PRECONDITION pattern), so a caller gets a specific, actionable failure instead of a raw transport decode error.

Test plan

  • cargo test --workspace — all passing
  • bun run test — 233 files / 4913 tests passing
  • bun run quality:fix — clean (eslint, svelte-check, tsc, rustfmt, clippy)
  • New core-level tests (RUN_LONG_TESTS=1, ~80s, matches existing large-subtree test convention): subtree just over the limit is refused with TreeTooLarge; subtree at exactly the limit is served
  • New daemon-level unit test: TreeTooLarge maps to RESOURCE_EXHAUSTED with x-tree-node-count/x-tree-node-max metadata
  • Pre-push gate (test:all + cargo build + e2e) passed

🤖 Generated with Claude Code

https://claude.ai/code/session_01KavcAieXx31wYVQfjz1wk6

 #2108)

The rest of #2108's issues were already fixed by #2294/#2319 (unknown-vs-zero
diagnostics fallbacks, count-only RPCs, bounded ordered recent-node query, and
the configured 64MiB gRPC message-size ceiling). The one remaining defect: on
the app's hot path for opening a document, get_children_tree still built and
serialized an entire subtree into one JSON payload with no node-count bound —
only depth is guarded (MAX_TREE_DEPTH). A wide-but-shallow subtree (many
siblings under one parent) can still produce a message the gRPC transport
rejects with an opaque OutOfRange at decode time.

The frontend eagerly loads a document's full tree in one call with no
lazy-expand fallback (shared-node-store.svelte.ts's loadChildrenTree), so
truncating the response would silently drop content rather than degrade
gracefully. Per the issue's stated preference, state and enforce a maximum
document size instead:

- Add MAX_TREE_NODES (20,000) next to the existing MAX_TREE_DEPTH guard in
  node_service/mod.rs, checked in get_children_tree before the tree is built.
- Add NodeServiceError::TreeTooLarge -> OpsError::TreeTooLarge -> a
  RESOURCE_EXHAUSTED gRPC status carrying node_id/count/max in metadata
  (mirrors the SubtreeAccessDenied FAILED_PRECONDITION pattern), so a caller
  gets a specific, actionable failure instead of a raw transport decode error.
- Regression tests: core-level tests seeding a subtree just over/at the limit
  (gated behind RUN_LONG_TESTS, matching the existing large-subtree test
  convention — this seeds 20k+ nodes and takes ~80s), and a fast daemon-level
  unit test asserting the status code and metadata.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KavcAieXx31wYVQfjz1wk6
@malibio

malibio commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This PR closes out the last remaining gap from issue #2108: get_children_tree had a depth bound (MAX_TREE_DEPTH) but no breadth/count bound, so a wide-shallow subtree could still produce an oversized gRPC payload that fails with an opaque transport-level OutOfRange at decode time. The fix is small, well-scoped, and follows the codebase's existing SubtreeAccessDenied/FAILED_PRECONDITION error-threading pattern closely. Net positive — this is a targeted, low-risk fix that closes a real hot-path failure mode with an actionable error instead of a confusing transport failure.

I verified the full acceptance criteria list from #2108 against the current state of the codebase (prior PRs #2294/#2319 plus this diff), not just this diff in isolation — see below.

Acceptance Criteria Verification (issue #2108)

  • A count that cannot be measured renders as unavailable, never 0 (prior PR) — confirmed present in packages/cli/src/commands/diagnostics.rs
  • nodespace diagnostics reports the correct node count on a 31k-node database (prior PR)
  • Node counting uses an aggregate query rather than transferring rows (prior PR) — count_nodes / QueryNodesSimpleRequest count-only path confirmed at diagnostics.rs:182
  • recent_node_ids uses a bounded ordered query instead of sorting a full snapshot (prior PR) — confirmed server-ordered small page at diagnostics.rs:119,215
  • gRPC message limits are explicitly configured rather than inherited from the tonic default (prior PR) — MAX_MESSAGE_SIZE_BYTES = 64 * 1024 * 1024 in packages/proto/src/lib.rs:32, applied via a shared macro to both client and server stubs
  • get_children_tree payload growth is bounded, or a maximum supported document size is stated and enforced (this PR) — MAX_TREE_NODES = 20_000 in packages/core/src/services/node_service/mod.rs:1994, checked in get_children_tree
  • A payload exceeding the limit surfaces as an actionable error at the call site, not a plausible-looking value (this PR) — NodeServiceError::TreeTooLarge → OpsError::TreeTooLarge → RESOURCE_EXHAUSTED with x-tree-node-count/x-tree-node-max metadata
  • Regression coverage for the "transport failed" path (prior PR for diagnostics; this PR adds coverage for the tree-size path specifically)

All eight acceptance criteria are now satisfied across #2294, #2319, and this PR. No gaps remain.

Findings

Critical Issues

None.

Suggested Improvements

  • packages/core/src/services/node_service/hierarchy.rs:106-116 — The count check runs after get_subtree_data has already pulled the entire subtree (nodes + relationships) from SQLite in one query. This PR correctly places the check before the more expensive remaining work (the mention-lookup RPC and the recursive tree-build/serialization into JSON), which is the right call given get_subtree_data is a single consolidated query that can't cheaply be split into "count first, fetch second" without a schema/query change. Worth a one-line comment acknowledging this is a partial short-circuit (saves the mention lookup + serialization, not the initial fetch) so a future reader doesn't assume the DB round-trip itself is bounded — the current comment ("Checked before building the tree... so an oversized subtree fails fast") could be read as implying the whole operation is short-circuited. Not blocking; the current behavior is still strictly better than before.

  • 20,000-node limit justification (packages/core/src/services/node_service/mod.rs:1988-1993) — The comment gives a reasonable order-of-magnitude argument (a few hundred bytes to a few KB per node × 20,000 stays under the 64 MiB ceiling) but doesn't cite an actual measurement. Given the byte-vs-row mismatch was literally the root cause of the original issue (Diagnostics reports 0 nodes for a 31k-node database: failed gRPC reads render as valid numbers, and payloads are bounded by rows not bytes #2108's defect Create design system foundation #2: "a limit expressed in rows cannot protect a ceiling expressed in bytes while row size is unbounded"), it would strengthen this fix to either: (a) note the worst-case per-node size used in the estimate (e.g., "assuming up to Nkb of content/properties per node, 20k nodes × Nkb = X MiB, comfortably under 64 MiB"), or (b) also assert a per-node size ceiling somewhere already enforced. As written, a document with 20,000 nodes that each carry unusually large content/properties blobs could in principle still exceed 64 MiB even under this limit — the same class of bug the issue was filed for, just pushed one level down. This is a reasonable pragmatic choice for now (nodes are typically short todo/text fragments), not a blocker, but worth a one-sentence acknowledgment that the guard is a heuristic node-count cap, not a hard byte guarantee.

Nitpicks

  • Nit: packages/daemon/src/services/node_service.rs — the new TreeTooLarge arm only attaches count/max to gRPC metadata, and correctly leaves node_id out of metadata (avoiding any ASCII-metadata-value parse-failure edge case, unlike count/max which are usize and always parse cleanly). This is a good, conservative choice; no gap here, just noting it was checked given the review brief's concern about .parse::<MetadataValue<Ascii>>() silently swallowing errors via if let Ok(...). For count/max specifically, a usize::to_string() is always plain ASCII digits, so the if let Ok(...) here is unreachable-in-practice defensive code, consistent with the identical pattern already used for x-subtree-inaccessible-count in SubtreeAccessDenied. No change needed — flagging only because the brief asked for scrutiny here.

  • Nit: The regression tests (get_children_tree_rejects_subtree_over_the_node_limit / _accepts_subtree_at_exactly_the_node_limit) are solid boundary tests (off-by-one on both sides of the limit) and appropriately gated behind RUN_LONG_TESTS=1 following the existing large_subtree_chunking_tests convention. Confirmed they compile and the fast daemon-level unit test (error_mapping_tree_too_large_returns_resource_exhausted) passes. No action needed.

Verdict

Approve (posting as a comment-only review since this is a self-authored PR and --approve/--request-changes are rejected by GitHub for the PR author). The two "Suggested Improvements" above are worth a one- or two-line comment update if convenient, but neither blocks merge — the change is a clear net improvement that closes the final gap in #2108 with a pattern-consistent, well-tested fix.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial review posted above. Net positive — closes the final gap in #2108 with a pattern-consistent (mirrors SubtreeAccessDenied/FAILED_PRECONDITION), well-tested fix. Two non-blocking Suggested Improvements noted (partial short-circuit comment clarity; 20k-node limit is a heuristic, not a hard byte guarantee). No Critical Issues. Approve (posting as comment since PR author cannot self-approve).

…AX_TREE_NODES

Comment-only changes from pragmatic-code-review on PR #2493 (both Suggested
Improvements, non-blocking):

- hierarchy.rs: clarify the MAX_TREE_NODES check only skips the remaining
  mention-lookup and tree-build/serialization, not the DB fetch that already
  ran in get_subtree_data — that fetch is one consolidated query and can't be
  split into count-then-fetch without a schema/query change.
- mod.rs: note MAX_TREE_NODES is a heuristic node-count cap, not a guaranteed
  byte ceiling, since per-node content/properties size is still unbounded.
  Accepted deliberately given typical nodes are short text/task fragments,
  rather than adding a byte-accumulation pass to close the residual case.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KavcAieXx31wYVQfjz1wk6
@malibio

malibio commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator Author

Address Review Summary

Both Suggested Improvements from the pragmatic-code-review addressed (comment-only, no logic change):

  • ✅ packages/core/src/services/node_service/hierarchy.rs:106-116 — clarified that the MAX_TREE_NODES check only short-circuits the remaining mention-lookup RPC and tree-build/serialization, not the DB fetch in get_subtree_data (which is one consolidated query already run by the time node_map.len() is known).
  • ✅ packages/core/src/services/node_service/mod.rs:1994 — clarified MAX_TREE_NODES is a heuristic node-count cap, not a guaranteed byte ceiling: a document with 20,000 unusually large nodes could in principle still exceed the 64 MiB message limit. Accepted deliberately (typical nodes are short text/task fragments) rather than adding a byte-accumulation pass to close that residual case.

No items skipped. Both were low-risk, worthwhile clarifications.

Verification: cargo build -p nodespace-core -p nodespace-daemon clean, bun run quality:fix clean (eslint/svelte-check/tsc/rustfmt/clippy), the two affected unit tests (error_mapping_tree_too_large_returns_resource_exhausted, tree_size_limit_tests) pass, and the pre-push gate (test:all + cargo build + full e2e suite) passed on push 156dcfdb.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: Both changes are doc-comment-only clarifications with zero logic/behavior change — no new functionality, no architectural change, no risk of introducing a new issue. The prior review already found no Critical or Important issues and recommended Approve.

🤖 Generated with Claude Code

@malibio
malibio merged commit 5b1d08e into main Sep 8, 2026
@malibio
malibio deleted the issue-2108-diagnostics-grpc-limits branch September 8, 2026 15:24
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.

Diagnostics reports 0 nodes for a 31k-node database: failed gRPC reads render as valid numbers, and payloads are bounded by rows not bytes

1 participant