Skip to content

feat(query): note.query and notesctl query for ADR-0037's Bases subset - #245

Merged
bketelsen merged 3 commits into
mainfrom
feat/note-query
Nov 17, 2026
Merged

bketelsen merged 3 commits into
mainfrom
feat/note-query

Conversation

@frostyard-machines

Copy link
Copy Markdown
Contributor

Summary

The first step of roadmap M5 (queries): notesd evaluates a subset of Obsidian Bases, per ADR-0037. M5's query done-when is that Dataview dashboards are rebuilt as .base files and render. Every client (the desktop app, the web app, the later MCP tool query_notes of ADR-0012) needs one evaluator in notesd, so this PR adds it first.

  • note.query in schema/rpc/note.yaml, additive in protocol 1 (make gen). Input: path or source, from, view, limit. Output: the view, every view's name and type, columns with display names, rows, groups, total, the unshown column keys, a notice for a view outside the subset, and a cards view's image.
  • internal/query parses the YAML (yaml.Node, so errors name a location such as views[0].filters.and[1]) and the statements. It folds constants (today(), now(), durations, date(), this.file.*) and compiles the filters, sort and groupBy to SQL over files LEFT JOIN notes. Every property key and literal is a bound parameter. Every condition is 0 or 1, never NULL. Anything outside the subset is refused with its location, or is an unshown column, or gives a view notice. Never a wrong row.
  • store.DB.RunQuery runs it. No table, no migration. frostnotes_time, a deterministic SQL function, reads ISO 8601 frontmatter dates in notesd's time zone.
  • notesctl query PATH|- [--view] [--limit] [--from] [--json].
  • Docs: the contract docs/specs/bases.md. Each rule is labelled Obsidian docs, ADR-0037 or choice; a choice waits for the comparison with Obsidian's rows, which is check 3 in Phase 4 of the plan. Also: plan 0010 maps ADR-0037's 10 checks to phases, a Queries section in docs/design/vault.md, and ADR-0037 set to Accepted. One sentence of the ADR changes: list contains reads json_each, because props rows lose the difference between 5 and "5".
  • Not in this PR: UI, the MCP tool, the graph. These are plan 0010, Phases 2 and 3, and the graph half of M5.

Risk tier

Risk tier: 2 — a new read-only RPC method and a SQL compiler. No path in .github/risk-paths.json matches. The method only reads the index. No base text reaches SQL text: FuzzCompile checks this.

Docs housekeeping

  • New docs started from their category's TEMPLATE.md (specs/bases.md, plans/0010-m5-queries.md)
  • Every new doc indexed in docs/README.md
  • Cross-links added in both directions (ADR-0037 ↔ specs/bases.md ↔ design/vault.md#queries ↔ plan 0010 ↔ roadmap)

Verification

  • make ci green on the host at the code of this head (verify, make ui-check in nsl with Biome, tsc and Vitest at 96 files, make desktop-build, the race run). After the run, only docs/specs/bases.md changed (the Fixtures paragraph).
  • make gen run and make gen-check green
  • Tests in internal/query:
    • Table tests over a fixture vault of made-up notes, indexed by notesd's own scan. They cover each statement form, operator, file property, function and method, typed and untyped properties, dates and durations, this, columns, sort, groups, limit and notices.
    • About 200 refusal rows, each with its location.
    • A complement check: not: [x] returns exactly the rows that x does not.
    • FuzzCompile and BenchmarkQuery: on 1,000 generated notes, a filter with a sort takes 5.0 ms, a group 11.1 ms, a text search 13.9 ms.
  • Other tests: an RPC test through internal/rpctest, the engine test (this from path and from, param errors) and a notesctl query test.

Add note.query to the note domain (additive in protocol 1) and
notesctl query. internal/query parses a base's YAML, checks the M5
subset (filters with and/or/not, properties with displayName, table and
cards views) and compiles it to one SQL condition and ORDER BY over
files LEFT JOIN notes, with every property key and literal bound.
store.DB.RunQuery runs it; the engine serves it. Anything outside the
subset is refused with its location, shown as an unshown column, or
gives a view notice; never a wrong row.

ADR-0037 is accepted. The contract is docs/specs/bases.md, the work
order plan 0010, and design/vault.md gains a Queries section.
@github-actions github-actions Bot added the level:L3 Skim label Nov 17, 2026
Fix the 7 blocking points of the cross-review at 745a40b, each with a
test that fails without its fix (the snapshot fix with an AGENTS.md
rule): integers fold and group without float64 loss; rows and total
come from 1 statement; a base is sized by the bytes read; a bad second
YAML document is refused; only the filters and keys read are checked;
a list constant expands only in file.hasTag, containsAny and
containsAll; calendar durations are tested across a DST change.
SQLite compares an integer with a real without rounding the integer;
the fold now does the same with math/big, so 9007199254740993 ==
9007199254740992.0 is false in both.
@frostyard-machines

Copy link
Copy Markdown
Contributor Author

Cross-review record

  • Reviewer: openai/gpt-6.1-sol. Coder: github-copilot/claude-sonnet-5.5. Base: f1958e1a837ff4df4ae921cb37377ff79016fd0a.
  • Initial review at 745a40bdb76e24eac90c0994c97d70e927c34c11. All substantive files were covered, in 5 batches; the generated files were left out (api/zz_generated.go, app/src/rpc/gen/api.ts, docs/specs/rpc-api.md). Not ready: 7 blocking points, and no SQL injection path found.
    1. Number precision in folds and groups.
    2. total and the rows came from 2 statements.
    3. The 1 MiB check used the index's stale size.
    4. A bad second YAML document was ignored.
    5. Anchors in views or keys that are not read refused the base.
    6. contains(this.file.tags) acted as containsAny.
    7. No test crossed a DST change.
  • Re-review 1 at 72fb76facfc883c6ebbaaeb4a93af81303edbe91: points 2 to 7 resolved. Point 1 was only partly fixed: an integer folded against a real still lost precision.
  • Re-review 2 at 999b0a34a2e558e238cdf7c328669ae8c691de1b: Ready, Blocking: none.
  • Guards:
    • A test for each point. Point 2 has no deterministic test, so its guard is the AGENTS.md rule "rows and count in 1 statement" plus TestTotalWithTheLimit.
    • The record file .memory/corrections/2026-11-16-pr-245-query-review-guards.json.
  • Revert test:
    • Setup: a scratch worktree at 999b0a3, with these files restored to 745a40b and the tests kept: internal/query/{compile,node,base,result}.go, internal/store/basequery.go, internal/engine/note.go.
    • Command: go test -count=1 ./internal/query/ ./internal/engine/.
    • Failed on assertions: TestFilterStatements (whole numbers, and a whole number against a real), TestGroupByLargeIntegers, TestRefusals/a_bad_second_document, TestRefusalsWithThis, TestReadsOnlyWhatItNeeds, TestNoteQuery/a_base_that_grew_on_disk_since_the_index and …shrank….
    • TestDaysAreCalendarDays passes there too, as it should: point 7 added a missing test for behaviour that was already right.
  • make ci passed on the host at 72fb76f. 999b0a3 changes only foldCompare and its tests; make check and the internal/query tests pass there.

@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 17, 2026
@bketelsen
bketelsen merged commit 4fa430d into main Nov 17, 2026
11 checks passed
@bketelsen
bketelsen deleted the feat/note-query branch November 17, 2026 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L3 Skim ready-for-human Reviewed, CI green: ready for Brian to review and merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants