Skip to content

In read mode a local strategy opens from the store when the store is known to equal Hive for it, and every open and page switch says where it came from - #737

Open
SunkenInTime wants to merge 2 commits into
mainfrom
t3/open-from-store
Open

SunkenInTime wants to merge 2 commits into
mainfrom
t3/open-from-store

Conversation

@SunkenInTime

Copy link
Copy Markdown
Owner

This is step 3 of reading the local library from the store: in --library-store=read, a local strategy now opens from the store's rows instead of Hive. Hive stays the truth. Saves still go to Hive through LocalLibrary, so the editor's state comes from rows while the saved data stays Hive's. This is the first step where the flag can change saved data, so most of this PR is proof that it doesn't.

What the editor takes from the store, and what goes back

A local open takes three things from storage: the page list in sort order, the shown page, and the theme. In read mode, each page load either comes wholly from the store or wholly from Hive, never a mix. "Wholly" covers the page data, the page list the pages bar shows, and the theme.

A save writes back only these, starting from Hive's own record:

  • the shown page's 9 fields: its six item lists, settings, side and lineups;
  • the map, the theme and the strategy's edit time.

Page names and order, the other pages, the folder and the strategy name always come from Hive. So do migration, the cleanup of unused images, and every page operation (add, delete, reorder, duplicate, apply to all pages).

When the store is used

The open comes from the store only when all of these hold:

  • the open did not migrate Hive's record;
  • the mirror is following;
  • the strategy is not among those the mirror says may differ from Hive (pending mirror work, a Hive write in flight, a refusal, an id that is not its Hive key);
  • the store keeps the strategy on this device;
  • no two pages share a position;
  • every row read is one this build can show;
  • no Hive write to the strategy began while the store was being read.

That last one is checked before and after the reads, with a per-strategy count of Hive writes begun.

If any of these fails, the load comes from Hive. Nothing is ever refused, and nothing waits for the mirror. Each page switch runs the same test.

Every open and switch has an outcome

StorePageReads (lib/library_store/store_page_reads.dart) records one outcome per page load. It is either served from the store, or served from Hive with a reason:

Reason When
migrationRan the open migrated Hive's record
mirrorNotFollowing the mirror is not following
mayDiffer the store may differ from Hive for this strategy
notInStore the store doesn't keep this strategy here
tiedPositions two pages share a position
noPages the strategy has no pages
damagedRows the store holds rows this build can't show
hiveWriteDuringRead a Hive write began during the read
readFailed the store read failed

The driver prints every load (STORE page ...). The new {"logPageReads": tag, "expect": {...}} step checks counts. Every run in read mode ends with the counts by reason.

A page switch practically always comes from Hive (mayDiffer). On main, every local switch first writes the page it leaves back to Hive, even with no edit. That write isn't copied by the mirror yet when the next page is read. My design note missed this. The rule is still safe; it just means only opens are served from the store at this step.

What a build without the flag runs

localPageReadsProvider is null unless --library-store=read overrides it. That flag only takes effect in drive and debug builds (shadowReachable), so a shipped build never sets it. At each hook, a build without the flag runs:

  • LocalStrategyPageSource.loadPage: one provider read that returns null, then main's Hive code unchanged.
  • _applyLoadedPageData: one provider read that returns null, then the same listPageIds wait and the same two theme lookups, in the same order.
  • loadFromHive: a null-aware call on null. Dart doesn't evaluate its arguments, so migratedStrategy != newStrat isn't evaluated either.

test/local_open_from_hive_test.dart pins today's local open and switch behaviour. Its 4 tests pass on main at 9d955e0 unchanged. They cover:

  • the first page in sort order;
  • tied page positions kept in their stored order in the pages bar;
  • the stored theme, or the default profile when none is named;
  • a switch reads Hive's record as it is at the switch;
  • a no-edit switch writes the page it leaves, moving the edit time on and naming the default theme.

Proof

Outcome tests (test/library_store/store_page_reads_test.dart, 11). They go through the real editor open in a container wired as read mode wires it.

  • A caption planted only in the store is what the canvas shows, so the open read the store.
  • A switch, with or without an edit, comes from Hive with mayDiffer.
  • Migration, a mirror that isn't following, a pending Hive write, tied positions, a strategy the store doesn't hold, a missing settings row, and a failed read each send the open to Hive with their reason.
  • The store read is held open, Hive is written, and the read is released: the open came from Hive with hiveWriteDuringRead, and the canvas shows Hive's page rather than the planted one.
    • With the before-and-after count removed, that test still goes to Hive, but under mayDiffer. That's because the mirror hasn't copied the write when the read ends.
    • The count matters for a write the mirror copies between the header read and the page read. The store runs each call in turn, so I couldn't make that interleaving deterministic without a test-only seam.
  • Only read mode installs the page reads.

The corpus (test/library_store/open_from_store_corpus_test.dart). It skips without ICARUS_LIBRARY_CORPUS, and copies each store to scratch, never opening it in place.

  • Every strategy is opened twice: once from Hive with no page reads, once in read mode over a SQLite store the mirror has copied. Each time, every page is visited and the strategy saved. Hive's record is then compared field for field in Hive's own encoding, edit time excluded.
  • A second pass does the same after one scripted edit: a caption on the first agent of the first page.
  • Result on the 552 stores and 705 strategies (1,234 pages) of the real-library corpus:
loads in the store runs: open from the store 1410, pageSwitch from Hive: mayDiffer 998
noEdit: strategies whose Hive record differs between the Hive open and the store open 0
oneEdit: strategies whose Hive record differs between the Hive open and the store open 0
  • All 1,410 opens in the store runs came from the store. The test fails if any didn't, because that would compare Hive with Hive. The edit pass changed 222 strategies (the ones with an agent to edit).
  • Negative control: making the store open serve one wrong caption makes the no-edit pass report agentData differing for that strategy.

Running app: I ran six local editor scripts once in mirror mode and once in read mode, on Windows (release driver build) and on Linux. The scripts were quick-page-switch-local, lineup-order-local, autosave-after-side-on-all-pages-local, transition-marker-sizes, transition-drawings-local and marker-size-undo.

  • Every run ends SUMMARY ... CLEAN, in both modes, on both platforms.
  • On Linux, the logged pages and pages bars of every script match line for line between the two modes, once run ids are replaced.
  • In read mode, every open came from the store: 7 in all. lineup-order-local opens twice.
  • Every page switch came from Hive with mayDiffer: 17 in all, for example mayDiffer 9, openFromStore 1, switchFromHive 9 in quick-page-switch-local.

close-with-unsaved-local ends by closing the window, so it has no summary in either mode. Both modes reach the same last logged page.

Speed

Time from the tap's release to the first frame showing the page, on Windows, in a release driver build, three runs each. These come from the new open-timed-largest.json and open-timed-typical.json, started with --hive-store on copies of the real stores:

Mirror (from Hive) Read (from the store) The store read inside it
Open, largest real strategy (canvas-compatibility-v97, 20 pages, 549 items) 669, 619, 579 ms 583, 656, 650 ms 5.6, 8.7, 9.0 ms
Open, typical strategy (Lotus replay · Oct 31, 2026, 2 pages, 27 items) 549, 550, 559 ms 551, 552, 556 ms 1.0–1.1 ms
Page switch, largest 577–614 ms 585–620 ms none: from Hive
Page switch, typical 548–598 ms 548–585 ms none: from Hive
  • The store's part of an open is the read itself: 1 ms for the typical strategy, 6–9 ms for the largest. That's under one frame in both cases, and the totals differ between modes by less than they vary run to run.
  • Hive serves the same open from memory in microseconds. The store pays for a SQLite read, JSON parsing, and a header load that lists every key the strategy holds.
  • The roughly 550 ms both modes share is the rest of the open and the switch, and it's the same in both.

Reading a whole strategy from the store costs 0.44 s at 100 pages, and the cost grows faster than the page count. The open never does that; it reads page by page. Filed as #734.

Not in this PR

  • Ops mode still opens from Hive.
  • Exports and backups are the next step.

ci: analyze test

…known to equal Hive for it, and every open and page switch records where it was served from and why
… each page load logs, and how to time an open and a switch
@coderabbitai

coderabbitai Bot commented Nov 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

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

Next included review available in 44 minutes.

Check out review usage here.

View limit details

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

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 91f28ce5-d1d1-4968-8228-86aa480e2956
Commits

Reviewing files that changed from the base of the PR and between d2ec6c1 and 2c76b25.

Files selected for processing: 15

  • lib/dev/drive/drive.dart
  • lib/dev/drive/drive_library_store.dart
  • lib/library_store/hive_reads.dart
  • lib/library_store/library_store_start.dart
  • lib/library_store/store_page_reads.dart
  • lib/providers/strategy_page_session_provider.dart
  • lib/providers/strategy_provider.dart
  • lib/strategy/local_page_reads.dart
  • lib/strategy/strategy_page_source.dart
  • test/library_store/open_from_store_corpus_test.dart
  • test/library_store/store_page_reads_test.dart
  • test/local_open_from_hive_test.dart
  • tool/drive/README.md
  • tool/drive/scripts/open-timed-largest.json
  • tool/drive/scripts/open-timed-typical.json
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

This branch has not been deployed

No deployments
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.

1 participant