Repository navigation
fix(batch): commit bidirectional referrers to the bytes the flags callback stores - #991
Conversation
…lback stores Under GROVE_V4 the backward-references batch planner committed every referrer of a family item to the logical hash of the caller-supplied bytes. Merk then runs the caller's just-in-time flags update, and Drive's StorageFlags::update_element_flags rewrites flags on every cross-epoch update (same size: old flags; larger: MultiEpoch merge; smaller: removed bytes combined). Only the item's own node was rehashed, so verify_grovedb flagged every referrer and honest V1 proofs of them failed. - merk: TreeNode::provided_value_hash_put_final_value runs the apply's own just-in-time update on a detached node and returns the final bytes. - batch: the apply's four JIT callbacks move verbatim into just_in_time_value_update.rs so the apply and the prediction share them. - preprocessor pass 3 (settle_end_hashes): after planning, predict each flagged, referenced family item's landed bytes and re-propagate the landed logical hash where it moved. - same-batch plain references to a family item hash the predicted landed bytes, measured on the full element against the committed predecessor (the old prediction measured the stripped shape and treated family sum items as fixed-cost sum items). V4-only paths; V1-V3 behavior and costs are unchanged. Corrects the test comment that claimed the next write heals stale referrers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (11)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
🕓 Queued for automated review — 6th in line, estimated start in ~1.4 h (commit 635bd46)
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #991 +/- ##
===========================================
+ Coverage 92.95% 93.09% +0.14%
===========================================
Files 331 332 +1
Lines 104500 105213 +713
===========================================
+ Hits 97138 97949 +811
+ Misses 7362 7264 -98
🚀 New features to boost your workflow:
|
…diction Review follow-ups for the flags-callback settling pass: - The settling pass now predicts every flagged family item written over a stored value and hands the apply one map of landed elements. Same-batch plain references hash from it (no second, apply-time read and prediction), and the apply checks each item's stored bytes against it, refusing the batch when a non-deterministic callback answers the apply differently (JustInTimeElementFlagsClientError). The callback contract is documented on apply_batch_with_element_flags_update. - Later settling rounds re-predict only the items the previous round rewrote; non-convergence is reported as a callback (client) error. - The overlay store keeps committed bytes in their own map, so element reads no longer clone them. - merk: a provided-value-hash put drops the value-defined cost inherited from its predecessor (issue #908's twin). A family item written over a sum item was billed at the sum item's fixed size (196 grown bytes charged as 0 under an inert callback). Only V4 family writes use this op. - Prediction errors carry context. - Tests: worst-case estimate under Drive's epoch flags, a plain reference above a deeper target, count-tree host, ItemWithSumItem family variant, item-to-sum-item switch with a plain reference, the sum-item cost, a stateful callback refused, and the failing-callback error kind. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Issue being fixed or feature implemented
Under GROVE_V4, a batch that rewrites a flagged backward-references item left every referrer committed to a hash the item no longer has. Drive's flags callback triggers this on every cross-epoch document update. The root hash stayed deterministic and plain
getreturned correct values, butverify_grovedbreported each referrer as corrupted, and honest V1 proofs of those referrers failed withV1 mismatch in lower layer hash. The affected documents became permanently unprovable.The 2026-09-23 audit found this; it has not been filed as an issue. It is V4-only (backward-reference elements are refused on V1–V3, and
apply_batch.backward_references_maintenance = 1only inv4.rs), so it has to land before V4 activates.Root cause. The batch planner committed each referrer, and every upstream chain member, to the logical hash of the target's caller-supplied bytes (
plan_element_update'snew_logical_hash, and pass 2'starget_value_hashfrom the staged overlay). Merk then runs the just-in-time flags update with the caller's callback and rehashes only the target's own node from its final bytes.StorageFlags::update_element_flagsrewrites the flags on every cross-epoch update:MultiEpochSo the referrers keep the supplied hash. Later writes don't fix it, because the stored and supplied flags keep differing under that callback. The test comment that said the next write "heals" this was wrong.
Plain references written in the same batch had a related flaw. Their prediction (
process_old_element_flags) measured the target's stripped shape, while Merk measures the full stored element, referrer list included. It also treats a family sum item as a fixed-cost sum item that just copies the old flags over, but Merk gives family items no value-defined cost and does run the callback. Both cases are reproduced by the new tests.What was done?
Prediction through Merk's own routine.
TreeNode::provided_value_hash_put_final_value(merk) runs the same steps aPutWithProvidedValueHashover a stored value performs (install value and feature type,just_in_time_tree_node_value_update) on a detached node and returns the final bytes. A merk unit test pins it to a real apply.One set of apply callbacks. The four closures
execute_ops_on_pathhands Merk moved verbatim intobatch/just_in_time_value_update.rs. The apply and the prediction (predict_provided_value_hash_put) now call the same functions. This is a pure extraction with no behavior change.Pass 3 in the backward-references preprocessor (
Expansion::settle_end_hashes). It runs after planning, once every registration has been folded in, because the flags a callback derives depend on the item's full size. It predicts the landed bytes of every flagged family item written over a stored value. Where the landed logical hash differs from the one referrers were committed to, it re-propagates it along the referrer chains (reusingplan_propagationand the existing merge rules). Later rounds re-predict only items a propagation rewrote (dropping a dangling entry); two rounds settle any batch with deterministic callbacks. A callback that never settles is refused as a client error.One prediction, checked after the apply. The pass returns the predicted landed elements, and the apply uses them in two ways:
landed_backward_references_item_value_hash), with no second read or prediction;JustInTimeElementFlagsClientError) if they differ. That can only happen if a callback isn't deterministic.apply_batch_with_element_flags_updatenow documents that its callbacks must be deterministic.Merk: provided-value-hash puts drop an inherited value-defined cost ([audit][C010] Ordinary replacements retain stale specialized storage charges #908's twin). A family item written over a sum item was sized at the sum item's fixed cost: a 196-byte growth was charged as 0 added bytes under an inert callback. Only V4 family writes use this op, so no gate is needed.
The overlay store keeps the committed bytes it already reads in a separate map (no extra seeks, no extra clones on element reads).
Behavior note: a same-batch plain reference to a family item that switches between its item and sum-item forms is now accepted, since the prediction follows Merk. Before, the reference path's
process_old_element_flagsrefused it withNotSupported. The V1–V3Itemarm is unchanged.Versioning and costs.
batch_maintains_backward_references, and the family arms offollow_reference_get_value_hashare unreachable on V1–V3, where family elements are refused at batch entry.Item/SumItemplain-reference arm (process_old_element_flags) is left alone.How Has This Been Tested?
New
grovedb/src/tests/backward_references_flags_update_tests.rs, using Drive's realStorageFlagscallbacks. Each case asserts thatverify_grovedbis clean and that an honest proof of each referrer verifies against the live root:batch_backward_references_cost_tests)These tests fail without the fix:
Other checks:
provided_value_hash_put_final_value_matches_the_applycompares the helper with a real apply, for a growing and a shrinking write.cargo test -p grovedb --lib: 3505 passed, 0 failed.cargo clippy -p grovedb -p grovedb-merk --all-targets -- -D warningsandcargo fmt --checkare clean.Breaking Changes
None.
TreeNode::provided_value_hash_put_final_valueis a new public merk API. No existing signature changes (apply_batch_with_element_flags_updateonly binds its callback parameters asmut).Checklist:
For repository code-owners and collaborators only
🤖 Generated with Claude Code
PR Hygiene ·
635bd46/skip-botsproceeds without the ones not yet reported/self-reviewedonce the bots are doneWhen every box is checked the
PR Hygienecheck passes and this can merge.Summary by CodeRabbit