Skip to content

Saved queries sort client-side, duplicating backend ordering rules in TypeScript #2727

Closed

Description

@malibio

Problem Statement

backendAdapter.queryNodes cannot evaluate a QueryFilter list, so a saved query is fetched by its targetType and then filtered, sorted, and limited on the client — see the comment at packages/desktop-app/src/lib/components/query/query-node-model.ts (the "Client-side query execution" block). The wire type confirms it: NodeQuery in packages/desktop-app/src/lib/services/adapter-core.ts carries only id, mentionedBy, contentContains, titleContains, nodeType, limit — there is no sorting field, so sort config never reaches the backend.

The result is two independent implementations of the same query semantics, which must be kept in agreement by hand.

This became concrete with priority rank ordering. Because the frontend never delegates, it had the identical lexicographic-priority bug in its own comparator, and fixing it required copying the rank table into TypeScript:

  • TaskPriority::rank() — packages/core/src/models/task_node.rs
  • the SQL CASE in QueryService::resolve_order_field — pinned to rank() by test_sql_priority_rank_matches_enum_rank
  • TaskNodeHelpers.priorityRank — packages/desktop-app/src/lib/types/task-node.ts

The two Rust copies are pinned together by a test. The TypeScript copy is pinned to nothing — no test spans both languages, so a one-sided edit to the rank table produces a silent disagreement between what the backend returns and what a saved query displays. Comments on both sides record the coupling, which is the weakest possible guarantee.

The same split applies beyond priority: ordered() backs the gt/gte/lt/lte filter operators client-side, so any future ordering rule has the same duplication problem.

Proposed Solution

Add sorting (and ideally filters) to the NodeQuery wire type, route saved queries through QueryService, and delete the client-side executor (applyFilters / applySorting / executeQueryDefinition) along with the duplicated rank helper.

QueryService already implements all of this — data-layer.md's search_nodes routing section documents it as the sorting-capable path — so this is about reaching it from the frontend, not building new capability.

Worth confirming while doing this: whether the client-side limit slicing and the backend's LIMIT agree on ordering semantics, since the backend applies LIMIT in SQL before its in-Rust re-sort.

Acceptance Criteria

  • NodeQuery carries sorting (and filters, if the same pass covers it)
  • Saved-query execution routes through QueryService rather than sorting in the browser
  • applySorting / applyFilters / executeQueryDefinition deleted, along with TaskNodeHelpers.priorityRank and CORE_PRIORITY_RANKS
  • Priority ordering still verified end-to-end (the coverage added alongside the rank work must not simply be dropped with the code it tested)
  • Any query-view tests depending on client-side execution updated

Reference Files

  • packages/desktop-app/src/lib/components/query/query-node-model.ts — the client-side executor
  • packages/desktop-app/src/lib/services/adapter-core.ts — NodeQuery, the wire type missing sorting
  • packages/core/src/services/query_service/mod.rs — the backend implementation to delegate to
  • packages/desktop-app/src/lib/types/task-node.ts — the unpinned TS rank copy

Activity

  1. malibio commented on Sep 17, 2026

    @malibio
    CollaboratorAuthor

    Discovered while implementing #2692 (priority rank ordering). That issue deliberately duplicated the rank into TypeScript rather than leaving saved-query sorting alphabetically wrong — this issue removes the duplication by delegating to the backend.

  2. malibio commented on Sep 17, 2026

    @malibio
    CollaboratorAuthor

    Baseline on worktree-issue-2727-backend-query-sorting: Frontend 5183 passed (254 files), 61.94s.

  3. self-assigned this
    on Sep 17, 2026
  4. added a commit that references this issue on Sep 17, 2026
    326c41b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions