Skip to content

fix(query): apply terminal-key conditionals per item - #734

Merged
QuantumExplorer merged 7 commits into
developfrom
codex/fix-689-terminal-keys-conditionals
Aug 21, 2026
Merged

QuantumExplorer merged 7 commits into
developfrom
codex/fix-689-terminal-keys-conditionals

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented May 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Query::terminal_keys resolved conditional subquery branches independently of the queried items, so absence-proof verification and the query_keys_optional family could demand terminal keys for selectors that were never queried, or silently drop a queried key overridden by a (None, None) conditional branch.

Fixes #689.

Approach

The fix follows the project's standard versioned-function layout (v0/v1 sub-files with a dispatch on the GroveVersion slot, as in add_element_on_transaction):

  • grovedb-query/src/terminal_keys/v0.rs — the legacy walk, frozen bug-for-bug for GROVE_V1..GROVE_V3 (all live in production): conditional branches are expanded before the queried items. Token-identical to the previous implementation.
  • grovedb-query/src/terminal_keys/v1.rs — GROVE_V4+: terminal keys are computed per queried item, resolving the first conditional branch containing the key (IndexMap insertion order) with the default branch as fallback — mirroring runtime query execution (subquery_paths_and_value_for_sized_query).
  • Dispatch lives in PathQuery::terminal_keys (grovedb-query does not depend on grovedb-version), matching on path_query_methods.terminal_keys: 0 → v0, 1 → v1, otherwise UnknownVersionMismatch.

Version gating

  • terminal_keys: 0 on GROVE_V1..GROVE_V3, 1 on GROVE_V4, pinned by a regression test in grovedb-version.
  • terminal_keys is verifier-side result assembly (absence proofs) and trusted reads only — proof generation never calls it, so no wire-format change. The locked V0 prover/verifier functions are untouched; verification under v1–v3 grove versions is byte-identical to before.
  • Not a cost-tracked operation (no OperationCost), so no estimation/replay implications.

Breaking change (downstream)

The unversioned Query::terminal_keys is removed — it would have silently defaulted to one behavior. Callers outside this repo (e.g. Drive) that used it directly must switch to PathQuery::terminal_keys(max_results, grove_version) or pick terminal_keys_v0 / terminal_keys_v1 explicitly.

Tests

  • v3-vs-v4 dispatch test (path_query_terminal_keys_conditionals_gate_at_v4) proving legacy vs fixed semantics through PathQuery::terminal_keys, plus an unknown-version dispatch test.
  • Version-specific pins in grovedb-query/tests/query_terminal_and_merge.rs, including the frozen v0 quirk where a (None, None) conditional override drops the queried key; shared behavior (limits, depth cap, error paths) runs against both versions.
  • Absence-proof regression: verify_absence_ignores_unqueried_conditional_subquery_branches.

Verification

  • cargo test -p grovedb — 2809 passed, 0 failed
  • cargo test -p grovedb-query — 405 passed, 0 failed
  • cargo test -p grovedb-version — 52 passed, 0 failed
  • cargo fmt --check and cargo clippy --workspace --all-features -- -D warnings — clean

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@QuantumExplorer, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 6 minutes

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

Wait for the limit to reset, then comment @coderabbitai review or push new commits to the PR.

An organization admin can change what happens after included review limits in Billing.

How do review limits work?

CodeRabbit enforces per-developer PR review limits within each organization.

For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: dead32c7-ae14-47cb-af30-07b2bb0b457e

📥 Commits

Reviewing files that changed from the base of the PR and between c8f870f and b7122db.

📒 Files selected for processing (10)
  • grovedb-query/src/lib.rs
  • grovedb-query/src/query.rs
  • grovedb-query/src/terminal_keys/mod.rs
  • grovedb-query/src/terminal_keys/v0.rs
  • grovedb-query/src/terminal_keys/v1.rs
  • grovedb-query/tests/query_terminal_and_merge.rs
  • grovedb-version/src/tests.rs
  • grovedb-version/src/version/v4.rs
  • grovedb/src/query/mod.rs
  • grovedb/src/tests/proof_coverage_tests.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-689-terminal-keys-conditionals

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented May 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.37%. Comparing base (1ec9a9e) to head (b7122db).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #734      +/-   ##
===========================================
+ Coverage    92.30%   92.37%   +0.06%     
===========================================
  Files          278      280       +2     
  Lines        86145    86307     +162     
===========================================
+ Hits         79520    79724     +204     
+ Misses        6625     6583      -42     
Components Coverage Δ
grovedb-core 90.52% <100.00%> (+0.02%) ⬆️
merk 93.28% <ø> (ø)
storage 87.05% <ø> (ø)
commitment-tree 96.07% <ø> (ø)
mmr 96.42% <ø> (ø)
bulk-append-tree 91.20% <ø> (ø)
element 97.98% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@QuantumExplorer QuantumExplorer changed the title Apply terminal-key conditionals per queried item fix(query): apply terminal-key conditionals per item May 21, 2026
@QuantumExplorer
QuantumExplorer force-pushed the codex/fix-689-terminal-keys-conditionals branch from 99a27f5 to cc83d0f Compare May 21, 2026 04:01
QuantumExplorer added a commit that referenced this pull request Jul 30, 2026
* feat(version): add GROVE_V4, behaviourally identical to V3

GROVE_V3 is live, so a fix that changes an accepted/rejected outcome, a
committed root hash, or a tracked cost cannot be applied unconditionally —
nodes carrying it would diverge from nodes that do not. There is currently
nowhere for such a fix to land, which has left several of them stuck:

  - #776: overwriting an indexed tree with a bare Reference skips the
    per-axis secondary cleanup. Closing it costs an extra stored-element read
    on EVERY reference overwrite (+1 seek, +79 storage_loaded_bytes, measured
    by the refresh-reference cost tests), and references over plain trees are
    shipped functionality.
  - Batch DeleteTree treats the caller-declared tree type as authoritative
    when selecting cleanup namespaces. Reading the stored element instead
    fixes both an indexed type-confusion and a live CommitmentTree
    wrong-emptiness-path bug, but adds a read to a released path.
  - Per project notes, five audit-fix PRs (#726, #730, #732, #734, #739)
    are gated on v3 and need re-gating before they can merge.

This adds the version and nothing else. Every method-version slot is copied
from V3 unchanged, so activating protocol version 4 today is a no-op; each
gate is a deliberate, separately-reviewable slot bump.

Verified rather than assumed: registering V4 changes what
`GroveVersion::latest()` resolves to, and the whole test suite defaults to
latest. The full workspace suite passes with V4 as latest (2459 grovedb + 705
merk + the rest), and the only two failures were the version registry's own
self-describing tests — `grove_version_latest_returns_v3` and
`grove_versions_count` — which are updated here. That is the evidence the
change is inert.

Adds `grove_v4_is_behaviourally_identical_to_v3_until_a_gate_is_added`, which
compares every slot and fails the moment one is bumped. That failure is the
intended prompt to document the gate rather than let V4 accrete behaviour
silently.

Co-Authored-By: Claude Fable 5 <[email protected]>

* test(version): drop the V3/V4 slot-identity assertion

It pinned V4's initial state as inert, which was worth verifying once but
becomes churn the moment a gate is added — and the DeleteTree read and #776
are both queued to gate on V4 next, so it would fail immediately and be
deleted anyway.

The evidence it provided is preserved where it belongs: the PR description
records that the full workspace suite passed with V4 as latest and that only
the registry's own self-describing tests changed.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[email protected]>
QuantumExplorer and others added 4 commits August 22, 2026 01:39
GROVE_V3 went live in production, so the terminal_keys fix from #689 can
no longer activate there: v3 must keep the legacy walk that expands
every conditional branch before the queried items. Move the gate to
GROVE_V4 (terminal_keys: 1), keep v1..v3 at 0, and pin all four
versions with a regression test.

Also adapt to the unified-PathQuery develop:
- add the new Query::read_mode field to the absence-proof test's
  struct literal
- add a PathQuery-level test proving v3 resolves terminal keys with the
  legacy semantics while v4 applies conditionals per queried item
- document the flipped slot in the v4 module header

Co-Authored-By: Claude Fable 5 <[email protected]>
Replace the ad-hoc terminal_keys_for_version(u16) API and _legacy suffix
with the project's versioned-function layout (as in
add_element_on_transaction and Drive): a terminal_keys module in
grovedb-query with v0.rs (legacy walk, frozen for GROVE_V1..V3,
bug-for-bug per issue #689) and v1.rs (per-item conditional resolution,
GROVE_V4+), each with a doc header explaining what is frozen and why.

grovedb-query does not depend on grovedb-version, so the GroveVersion
match dispatch lives in PathQuery::terminal_keys, selecting
terminal_keys_v0 / terminal_keys_v1 and returning
UnknownVersionMismatch otherwise.

The unversioned Query::terminal_keys is removed entirely — it silently
defaulted to the new semantics, so any external caller now has to pick
a version explicitly. Tests updated: shared behavior runs against both
versions, version-specific semantics (including the frozen v0
(None, None)-override key drop) are pinned per version, and an
unknown-version dispatch test is added in grovedb.

Co-Authored-By: Claude Fable 5 <[email protected]>

@QuantumExplorer QuantumExplorer left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reviewed

llvm-cov showed v0.rs at 80.7% line coverage (v1.rs was 100%). Cover the
remaining v0-only paths: the conditional subquery-path-without-subquery
arm (terminal key emission, at-limit error, empty-path corruption
error), both over-limit loop guards, and error propagation through
nested subqueries reached via conditional and default branches. Both
files are now at 100% line and function coverage.

Co-Authored-By: Claude Fable 5 <[email protected]>
@QuantumExplorer
QuantumExplorer merged commit 062a701 into develop Aug 21, 2026
2 checks passed
@QuantumExplorer
QuantumExplorer deleted the codex/fix-689-terminal-keys-conditionals branch August 21, 2026 20:08
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.

[audit][medium] terminal_keys resolves conditional branches differently from query execution

1 participant