Skip to content

Bound QueryNodesSimple/GetRoots and add count-only RPCs - #2319

Merged
mstomar125 merged 1 commit into
mainfrom
issue-2289-bounded-list-query-rpcs
Aug 31, 2026
Merged

mstomar125 merged 1 commit into
mainfrom
issue-2289-bounded-list-query-rpcs

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

Closes #2289.

QueryNodesSimple's daemon handler hardcoded order_by: None on every request, so even though SqliteStore::query_nodes gained a real ORDER BY (#2211/#2212, merged earlier), the gRPC surface never actually used it — offset-based pagination through this RPC could return duplicate or skipped rows across pages. A client-requested limit also passed straight through to the store with no ceiling, so a large-enough request returned every matching row in one unary message and relied entirely on the gRPC message-size limit (raised in #2286) to badly fail anything larger still.

This PR bounds QueryNodesSimple/GetRoots instead of relying on that ceiling, and adds a count-only path for callers (like nodespace diagnostics) that only need a total.

  • Add a NodeSortOrder proto enum and order_by field on QueryNodesSimpleRequest. The daemon now always applies a deterministic default order (created ascending) when unspecified — see order_by_from_proto in packages/daemon/src/services/node_service.rs — so offset-based pagination through this RPC is finally stable, not just theoretically supported by the store layer.
  • Clamp QueryNodesSimple's limit to a max page size (500), mirroring the existing MAX_EXECUTE_QUERY_LIMIT pattern on ExecuteQuery. Give GetRoots the same default/max treatment, replacing its old "0 = unlimited" sentinel.
  • Add count_nodes/count_roots — new SqliteStore methods, NodeService methods, and CountNodes/CountRoots gRPC RPCs — so a caller that only needs a total doesn't have to fetch (and page through) the matching records. query_nodes's condition-building is refactored into a shared build_scalar_conditions helper so count_nodes can't drift from query_nodes's filter semantics.
  • Switch nodespace diagnostics to the new count-only RPCs for total_node_count/root_node_count, and a small CREATED_DESC-ordered page (limit=10) for recent_node_ids, instead of pulling up to 100,000 full records purely to call .len() on the result and sort client-side.

Explicitly out of scope

The issue also lists GetChildren, GetChildrenTree, GetCollectionMembers(Recursive), SearchNodes, GetAllSchemas, and ExportMarkdown as unbounded. Those don't currently model limit/offset via NodeQuery the way QueryNodesSimple/GetRoots do (some, like GetChildren, are edge-order-based and already deterministic; others would need a separate pagination design). This PR keeps to the RPCs the issue describes in the most concrete detail and that build directly on the order_by/limit/offset primitives NodeQuery already has. Happy to split the rest into a follow-up issue.

Test plan

  • bun run test:all — no new failures (frontend 227 files/4810 tests, full Rust workspace incl. the nodespace-app Tauri-seam suite, all green)
  • bun run quality:fix — cargo fmt, cargo clippy --all-targets -- -D warnings -D clippy::unused-async clean, eslint/tsc/svelte-check clean
  • New store-level tests (packages/core/src/db/sqlite_store/nodes.rs): count_nodes_agrees_with_query_nodes_len_across_filter_shapes, count_roots_agrees_with_get_roots_len
  • New daemon gRPC integration tests (packages/daemon/tests/grpc_round_trip.rs):
    • query_nodes_simple_pages_tile_full_set_exactly_once_with_default_ordering — 47-node dataset, 3 pages of limit=20, asserts the tiled pages reconstruct the full set with no duplicates/gaps and that repeating a page is stable
    • query_nodes_simple_clamps_an_oversized_limit_to_the_max_page_size — 505 seeded nodes, limit=100_000 request clamps to exactly 500
    • count_nodes_and_count_roots_report_totals_without_transferring_records
  • Existing CLI diagnostics tests (diagnostics_collect_reports_counts_and_recency, diagnostics_reports_unknown_counts_when_the_node_query_fails) and the QueryNodesSimple exceeds gRPC's 4MB message-size limit on large databases, and the failure is silently misreported (diagnostics shows 'Total nodes: 0') #2286 message-size-ceiling regression test (query_nodes_simple_handles_response_over_the_default_grpc_limit) still pass unmodified in behavior
  • Pushed through the full pre-push gate (test:all + cargo build --bin nodespaced + cargo test -p nodespace-app) — green

🤖 Generated with Claude Code

QueryNodesSimple's daemon handler hardcoded order_by: None on every
request, so even though SqliteStore::query_nodes gained a real ORDER BY
(#2211/#2212), the gRPC surface never used it — offset-based pagination
through this RPC could return duplicate or skipped rows across pages. A
client-requested limit also passed straight through with no ceiling,
relying entirely on the gRPC message-size limit to fail an oversized
response.

- Add a NodeSortOrder proto enum and order_by field on
  QueryNodesSimpleRequest; the daemon now always applies a deterministic
  default (created ascending) when unspecified, via order_by_from_proto.
- Clamp QueryNodesSimple's limit to a max page size (500) and give
  GetRoots the same default/max treatment, replacing its "0 = unlimited"
  sentinel.
- Add count_nodes/count_roots (store, service, and CountNodes/CountRoots
  RPCs) so a caller that only needs a total doesn't have to fetch (and
  page through) the matching records. Refactor query_nodes's condition
  building into build_scalar_conditions, shared with count_nodes.
- Switch `nodespace diagnostics` to the count-only RPCs for
  total_node_count/root_node_count and a small CreatedDesc-ordered page
  for recent_node_ids, instead of pulling up to 100,000 full records to
  call .len() on the result.

Regression tests: store-level count_nodes/count_roots agreement with
query_nodes/get_roots across every filter shape, and daemon-level gRPC
tests proving repeated limit/offset pages tile a 47-node set exactly
once (no dup/gaps) with stable repeat calls, an oversized limit against
505 seeded nodes clamps to exactly 500, and count_nodes/count_roots
report exact totals via a real gRPC round trip.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125
mstomar125 merged commit d899c57 into main Aug 31, 2026
@mstomar125
mstomar125 deleted the issue-2289-bounded-list-query-rpcs branch August 31, 2026 10:48
malibio added a commit that referenced this pull request Sep 8, 2026
* Bound GetChildrenTree's subtree payload with an actionable error (closes #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

* Address review: clarify short-circuit scope and heuristic nature of MAX_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

---------

Co-authored-by: Claude Sonnet 5 <[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.

Bound the list/query RPCs instead of relying on the gRPC message-size ceiling

1 participant