Skip to content

BE-921: Resolve entity patches before persistence - #10127

Draft
montekki wants to merge 1 commit into
fs/be-921-prepare-entity-patch-writesfrom
fs/be-921-resolve-patches-before-persistence
Draft

montekki wants to merge 1 commit into
fs/be-921-prepare-entity-patch-writesfrom
fs/be-921-resolve-patches-before-persistence

Conversation

@montekki

@montekki montekki commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

🌟 Purpose

Batching writes requires later patches to see earlier changes before those changes reach Postgres. Resolve patches in input order against working editions and temporal history, then persist the accumulated changes.

🔍 Changes

  1. Replace the per-patch load/prepare/write sequence in patch_entities in entity/mod.rs with load_entity_patch_set, resolve_entity_patches, and one final write_entity_patch_set call in entity/batch. Reads and locks still execute separately for each target in this frame.
  2. Add EntityPatchSet::get in entity/batch/mod.rs to reconstruct the edition visible at each patch's timestamps from working history. Resolve patches in input order and apply each result to that history so repeated targets see earlier pending changes.
  3. Move resource loading and policy preparation into the batch module. Return typed EntityHistory and EntityPatchResource values from the query helpers, and build each authorization context from the resolved edition and reusable principal data.
  4. Add pending-edition and link-endpoint caching in EntityPatchSet. Extend StoreProvider in store/validation.rs so validation can read pending editions before they reach Postgres.
  5. Remove the superseded single-patch persistence and locking helpers from entity/patch.rs and entity/mod.rs. Persist all accumulated editions and the final temporal diff after resolution, retaining intermediate editions for individual patch results.
  6. Add regression cases in tests/graph/integration/postgres/partial_updates.rs for repeated targets, decision-time ordering, permissions, link visibility, intermediate editions, and rollback.

Permission checks and link validation use the pending editions. Persist temporal changes in bulk through the shared writer. Tests cover repeated entities, reverse decision times, changed permissions, endpoint visibility, intermediate editions, and rollback.

Validation: 31 selected temporal, patch, draft, and read-only tests passed. Clippy, Rust formatting, embedded SQL formatting, and whitespace checks passed.

@montekki montekki self-assigned this Oct 20, 2026
@github-actions github-actions Bot added area/libs Relates to first-party libraries/crates/packages (area) type/eng > backend Owned by the @backend team area/tests New or updated tests labels Oct 20, 2026
@vercel

vercel Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hash Ready Ready Preview Oct 20, 2026 6:08pm UTC
petrinaut Ready Ready Preview Oct 20, 2026 6:08pm UTC
petrinaut-docs Ready Ready Preview Oct 20, 2026 6:08pm UTC
1 Skipped Deployment
Project Deployment Actions Updated
hashdotdesign-tokens Ignored Ignored Preview Oct 20, 2026 6:08pm UTC

Request Review

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 250 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.96%. Comparing base (70a667c) to head (9f0bd3e).

Files with missing lines Patch % Lines
...e/src/store/postgres/knowledge/entity/batch/mod.rs 0.00% 225 Missing ⚠️
...local/graph/postgres-store/src/store/validation.rs 0.00% 15 Missing ⚠️
...ore/src/store/postgres/knowledge/entity/history.rs 0.00% 9 Missing ⚠️
...s-store/src/store/postgres/knowledge/entity/mod.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@                            Coverage Diff                            @@
##           fs/be-921-prepare-entity-patch-writes   #10127      +/-   ##
=========================================================================
- Coverage                                  68.01%   67.96%   -0.06%     
=========================================================================
  Files                                       2009     2009              
  Lines                                     206637   206794     +157     
  Branches                                    8801     8801              
=========================================================================
+ Hits                                      140537   140539       +2     
- Misses                                     64429    64585     +156     
+ Partials                                    1671     1670       -1     
Flag Coverage Δ
hash-graph 21.07% <ø> (ø)
hash-graph-api 39.82% <ø> (ø)
hash-graph-atlas 80.01% <ø> (+<0.01%) ⬆️
hash-graph-postgres-store 31.83% <0.00%> (-0.23%) ⬇️
hashql-compiletest 28.71% <ø> (ø)
hashql-eval 81.55% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark results

hash-graph-benches – Integrations

policy_resolution_large

Function Value Mean Flame graphs
resolve_policies_for_actor user: empty, selectivity: high, policies: 2002 $$29.6 \mathrm{ms} \pm 307 \mathrm{μs}\left({\color{red}6.12 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: low, policies: 1 $$3.57 \mathrm{ms} \pm 29.4 \mathrm{μs}\left({\color{gray}0.374 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: medium, policies: 1002 $$13.4 \mathrm{ms} \pm 155 \mathrm{μs}\left({\color{gray}0.552 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: high, policies: 3314 $$44.9 \mathrm{ms} \pm 564 \mathrm{μs}\left({\color{gray}1.24 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: low, policies: 1 $$14.4 \mathrm{ms} \pm 154 \mathrm{μs}\left({\color{gray}1.18 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: medium, policies: 1527 $$25.1 \mathrm{ms} \pm 270 \mathrm{μs}\left({\color{gray}2.31 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: high, policies: 2078 $$31.0 \mathrm{ms} \pm 279 \mathrm{μs}\left({\color{gray}4.86 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: low, policies: 1 $$3.93 \mathrm{ms} \pm 35.4 \mathrm{μs}\left({\color{gray}4.14 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: medium, policies: 1033 $$15.8 \mathrm{ms} \pm 160 \mathrm{μs}\left({\color{red}9.32 \mathrm{\%}}\right) $$ Flame Graph

policy_resolution_medium

Function Value Mean Flame graphs
resolve_policies_for_actor user: empty, selectivity: high, policies: 102 $$4.07 \mathrm{ms} \pm 41.9 \mathrm{μs}\left({\color{gray}3.44 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: low, policies: 1 $$3.15 \mathrm{ms} \pm 32.8 \mathrm{μs}\left({\color{gray}3.56 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: medium, policies: 52 $$3.55 \mathrm{ms} \pm 34.3 \mathrm{μs}\left({\color{gray}3.30 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: high, policies: 269 $$5.62 \mathrm{ms} \pm 56.4 \mathrm{μs}\left({\color{red}5.13 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: low, policies: 1 $$3.76 \mathrm{ms} \pm 34.4 \mathrm{μs}\left({\color{gray}4.96 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: medium, policies: 108 $$4.39 \mathrm{ms} \pm 47.2 \mathrm{μs}\left({\color{gray}3.51 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: high, policies: 133 $$4.71 \mathrm{ms} \pm 60.8 \mathrm{μs}\left({\color{gray}3.00 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: low, policies: 1 $$3.64 \mathrm{ms} \pm 35.8 \mathrm{μs}\left({\color{gray}4.96 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: medium, policies: 63 $$4.53 \mathrm{ms} \pm 52.6 \mathrm{μs}\left({\color{red}6.70 \mathrm{\%}}\right) $$ Flame Graph

policy_resolution_none

Function Value Mean Flame graphs
resolve_policies_for_actor user: empty, selectivity: high, policies: 2 $$2.86 \mathrm{ms} \pm 24.7 \mathrm{μs}\left({\color{gray}1.67 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: low, policies: 1 $$2.77 \mathrm{ms} \pm 17.4 \mathrm{μs}\left({\color{gray}1.54 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: medium, policies: 2 $$2.91 \mathrm{ms} \pm 17.1 \mathrm{μs}\left({\color{gray}1.83 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: high, policies: 8 $$3.21 \mathrm{ms} \pm 21.3 \mathrm{μs}\left({\color{gray}3.35 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: low, policies: 1 $$2.98 \mathrm{ms} \pm 24.8 \mathrm{μs}\left({\color{gray}2.00 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: medium, policies: 3 $$3.30 \mathrm{ms} \pm 26.4 \mathrm{μs}\left({\color{gray}3.03 \mathrm{\%}}\right) $$ Flame Graph

policy_resolution_small

Function Value Mean Flame graphs
resolve_policies_for_actor user: empty, selectivity: high, policies: 52 $$3.32 \mathrm{ms} \pm 35.6 \mathrm{μs}\left({\color{red}5.94 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: low, policies: 1 $$2.92 \mathrm{ms} \pm 25.9 \mathrm{μs}\left({\color{gray}2.84 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: empty, selectivity: medium, policies: 26 $$3.13 \mathrm{ms} \pm 28.1 \mathrm{μs}\left({\color{gray}3.07 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: high, policies: 94 $$3.73 \mathrm{ms} \pm 39.3 \mathrm{μs}\left({\color{gray}2.97 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: low, policies: 1 $$3.19 \mathrm{ms} \pm 26.9 \mathrm{μs}\left({\color{gray}2.85 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: seeded, selectivity: medium, policies: 27 $$3.45 \mathrm{ms} \pm 51.1 \mathrm{μs}\left({\color{gray}1.82 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: high, policies: 66 $$3.66 \mathrm{ms} \pm 37.0 \mathrm{μs}\left({\color{gray}3.81 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: low, policies: 1 $$3.20 \mathrm{ms} \pm 40.7 \mathrm{μs}\left({\color{gray}4.01 \mathrm{\%}}\right) $$ Flame Graph
resolve_policies_for_actor user: system, selectivity: medium, policies: 29 $$3.52 \mathrm{ms} \pm 32.5 \mathrm{μs}\left({\color{gray}2.13 \mathrm{\%}}\right) $$ Flame Graph

read_scaling_complete

Function Value Mean Flame graphs
entity_by_id;one_depth 1 entities $$34.5 \mathrm{ms} \pm 287 \mathrm{μs}\left({\color{red}6.88 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;one_depth 10 entities $$76.5 \mathrm{ms} \pm 905 \mathrm{μs}\left({\color{red}7.18 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;one_depth 25 entities $$38.8 \mathrm{ms} \pm 350 \mathrm{μs}\left({\color{red}6.86 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;one_depth 5 entities $$42.0 \mathrm{ms} \pm 389 \mathrm{μs}\left({\color{red}6.92 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;one_depth 50 entities $$46.0 \mathrm{ms} \pm 350 \mathrm{μs}\left({\color{red}6.44 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 1 entities $$36.7 \mathrm{ms} \pm 393 \mathrm{μs}\left({\color{red}6.12 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 10 entities $$445 \mathrm{ms} \pm 1.99 \mathrm{ms}\left({\color{gray}4.66 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 25 entities $$94.5 \mathrm{ms} \pm 694 \mathrm{μs}\left({\color{gray}-0.532 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 5 entities $$83.5 \mathrm{ms} \pm 1.06 \mathrm{ms}\left({\color{gray}3.97 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;two_depth 50 entities $$291 \mathrm{ms} \pm 1.77 \mathrm{ms}\left({\color{lightgreen}-6.763 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 1 entities $$11.2 \mathrm{ms} \pm 87.6 \mathrm{μs}\left({\color{gray}4.66 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 10 entities $$11.3 \mathrm{ms} \pm 90.6 \mathrm{μs}\left({\color{red}5.04 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 25 entities $$11.9 \mathrm{ms} \pm 101 \mathrm{μs}\left({\color{red}8.03 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 5 entities $$11.6 \mathrm{ms} \pm 94.0 \mathrm{μs}\left({\color{red}7.88 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id;zero_depth 50 entities $$11.6 \mathrm{ms} \pm 84.5 \mathrm{μs}\left({\color{red}7.37 \mathrm{\%}}\right) $$ Flame Graph

read_scaling_linkless

Function Value Mean Flame graphs
entity_by_id 1 entities $$11.4 \mathrm{ms} \pm 95.7 \mathrm{μs}\left({\color{red}7.68 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 10 entities $$11.4 \mathrm{ms} \pm 90.3 \mathrm{μs}\left({\color{red}7.10 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 100 entities $$11.4 \mathrm{ms} \pm 113 \mathrm{μs}\left({\color{red}6.31 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 1000 entities $$11.4 \mathrm{ms} \pm 96.4 \mathrm{μs}\left({\color{gray}4.10 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 10000 entities $$12.0 \mathrm{ms} \pm 106 \mathrm{μs}\left({\color{red}7.35 \mathrm{\%}}\right) $$ Flame Graph

representative_read_entity

Function Value Mean Flame graphs
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/block/v/1 $$12.0 \mathrm{ms} \pm 91.2 \mathrm{μs}\left({\color{red}9.26 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/book/v/1 $$12.1 \mathrm{ms} \pm 91.3 \mathrm{μs}\left({\color{red}7.29 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/building/v/1 $$12.2 \mathrm{ms} \pm 123 \mathrm{μs}\left({\color{red}8.37 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/organization/v/1 $$12.3 \mathrm{ms} \pm 104 \mathrm{μs}\left({\color{red}9.77 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/page/v/2 $$12.0 \mathrm{ms} \pm 94.1 \mathrm{μs}\left({\color{red}6.61 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/person/v/1 $$12.0 \mathrm{ms} \pm 82.6 \mathrm{μs}\left({\color{red}8.11 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/playlist/v/1 $$12.3 \mathrm{ms} \pm 107 \mathrm{μs}\left({\color{red}11.5 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/song/v/1 $$12.2 \mathrm{ms} \pm 89.9 \mathrm{μs}\left({\color{red}10.3 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/uk-address/v/1 $$12.4 \mathrm{ms} \pm 97.6 \mathrm{μs}\left({\color{red}11.3 \mathrm{\%}}\right) $$ Flame Graph

representative_read_entity_type

Function Value Mean Flame graphs
get_entity_type_by_id Account ID: bf5a9ef5-dc3b-43cf-a291-6210c0321eba $$8.67 \mathrm{ms} \pm 71.1 \mathrm{μs}\left({\color{red}6.41 \mathrm{\%}}\right) $$ Flame Graph

representative_read_multiple_entities

Function Value Mean Flame graphs
entity_by_property traversal_paths=0 0 $$63.4 \mathrm{ms} \pm 517 \mathrm{μs}\left({\color{red}13.0 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=255 1,resolve_depths=inherit:1;values:255;properties:255;links:127;link_dests:126;type:true $$121 \mathrm{ms} \pm 956 \mathrm{μs}\left({\color{red}11.7 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:0;links:0;link_dests:0;type:false $$70.8 \mathrm{ms} \pm 624 \mathrm{μs}\left({\color{red}14.8 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:0;links:1;link_dests:0;type:true $$81.0 \mathrm{ms} \pm 828 \mathrm{μs}\left({\color{red}14.0 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:2;links:1;link_dests:0;type:true $$89.6 \mathrm{ms} \pm 673 \mathrm{μs}\left({\color{red}12.1 \mathrm{\%}}\right) $$
entity_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:2;properties:2;links:1;link_dests:0;type:true $$98.6 \mathrm{ms} \pm 754 \mathrm{μs}\left({\color{red}13.0 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=0 0 $$52.1 \mathrm{ms} \pm 512 \mathrm{μs}\left({\color{red}13.7 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=255 1,resolve_depths=inherit:1;values:255;properties:255;links:127;link_dests:126;type:true $$80.6 \mathrm{ms} \pm 711 \mathrm{μs}\left({\color{red}8.16 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:0;links:0;link_dests:0;type:false $$57.6 \mathrm{ms} \pm 480 \mathrm{μs}\left({\color{red}10.0 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:0;links:1;link_dests:0;type:true $$67.8 \mathrm{ms} \pm 547 \mathrm{μs}\left({\color{red}12.5 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:0;properties:2;links:1;link_dests:0;type:true $$68.0 \mathrm{ms} \pm 542 \mathrm{μs}\left({\color{red}6.82 \mathrm{\%}}\right) $$
link_by_source_by_property traversal_paths=2 1,resolve_depths=inherit:0;values:2;properties:2;links:1;link_dests:0;type:true $$69.2 \mathrm{ms} \pm 575 \mathrm{μs}\left({\color{red}10.9 \mathrm{\%}}\right) $$

scenarios

Function Value Mean Flame graphs
full_test query-limited $$115 \mathrm{ms} \pm 860 \mathrm{μs}\left({\color{gray}0.666 \mathrm{\%}}\right) $$ Flame Graph
full_test query-unlimited $$126 \mathrm{ms} \pm 826 \mathrm{μs}\left({\color{gray}0.419 \mathrm{\%}}\right) $$ Flame Graph
linked_queries query-limited $$23.6 \mathrm{ms} \pm 204 \mathrm{μs}\left({\color{gray}-2.567 \mathrm{\%}}\right) $$ Flame Graph
linked_queries query-unlimited $$488 \mathrm{ms} \pm 2.21 \mathrm{ms}\left({\color{gray}-2.166 \mathrm{\%}}\right) $$ Flame Graph

This branch was successfully deployed

3 active deployments
Preview – petrinaut-docs — 9f0bd3ee Deployed Oct 20, 2026 by vercel[bot]
Preview – hash — 9f0bd3ee Deployed Oct 20, 2026 by vercel[bot]
Preview – petrinaut — 9f0bd3ee Deployed Oct 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/libs Relates to first-party libraries/crates/packages (area) area/tests New or updated tests type/eng > backend Owned by the @backend team

Development

Successfully merging this pull request may close these issues.

1 participant