Skip to content

Move storage-retention onto tagged live reads - #5659

Closed
ymichael wants to merge 5 commits into
bb/live-data-stack-4-concurrency-limit-thread-list-thr_adfmverzkqfrom
bb/live-data-stack-6-storage-retention-thr_uzjechs2j6
Closed

ymichael wants to merge 5 commits into
bb/live-data-stack-4-concurrency-limit-thread-list-thr_adfmverzkqfrom
bb/live-data-stack-6-storage-retention-thr_uzjechs2j6

Conversation

@ymichael

@ymichael ymichael commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Storage-retention kept its own refresh and ordering logic:

  • useRealtime("changed") and "policy-changed" handlers;
  • host:changed and reconnect subscriptions;
  • state and refresh revision guards;
  • optimistic state flips with rollback.

Because report data and host metadata were loaded together, a host rename with unchanged report JSON could leave the labels stale. A retention sweep didn't refresh reports, and a failed metadata load was swallowed.

Stack 6 of 13, stacked on #5666. #5640 is merged and #5688 is folded into #5658. Review only this PR's commits. Merge order: #5658 → #5661 → #5669 → #5666 → #5659 → #5660 → #5663 → #5667 → #5668 → #5677 → #5673 → #5670.

What changed

  • src/contract.ts declares the read tags and the invalidates of every write.
  • A new metadata RPC returns the persistent-host list and primary host ID through the server SDK, so metadata refreshes on its own.
  • Server background work, CLI writes and retention sweeps publish through bb.rpc.experimental_invalidate.
  • app.tsx uses experimental_useRpcQuery for state, hosts and metadata, experimental_useRpcMutation for every action, and experimental_useRpcRefresh.
  • Removed: the plugin signals, manual refresh subscriptions, revision guards and follow-up refresh calls.
  • No SDK or mechanism changes.

Behaviour changes:

  • Reports and metadata refresh on host rename, connect, disconnect and primary-host changes, even when the report JSON is unchanged.
  • Storage jobs invalidate reports. Retention sweeps invalidate reports, policy state and previews. CLI policy writes refresh open clients.
  • A first-load error shows Try again. A refresh error keeps the loaded controls mounted and shows an inline error with Try again. A policy refresh error used to show a toast and now uses this inline treatment, per the shared error rule.
  • Settings save immediately on interaction but show the saved server value until the save and re-read finish. A failed write keeps the previous value, with no optimistic flip and rollback.
  • Cleanup controls stay pending until the affected reads re-read. Background jobs still show progress and completion toasts.

Optimistic updates (this PR): settings switches and selects update instantly through the shared optimistic option, and roll back if the write fails, as on main.

How you verified

  • pnpm exec turbo run build typecheck lint test --filter='...[origin/main]' --continue: 318/318 passed. Storage-retention: 79 tests, lint clean.
  • Checks against Let plugins share live data through tagged RPC reads #5582's parity audit and its fix commits, in an isolated source app (no production data):
Behaviour Result
Host rename refreshes reports and labels Passed
Host disconnect shows Offline and disables scan Passed (daemon child stopped, then reconnected)
Metadata reloads when report data is unchanged Passed (identical report payloads, new label shown)
Retention sweep refreshes reports Passed (real scheduler on a fresh store)
Metadata load error shown; Try again recovers Passed (first load and refresh, intercepted request)
CLI policy save updates an open client Passed
Policy refresh error: toast → inline Retry Deliberately different (shared error rule)

Part of #4944

AGENT GENERATED

🤖 Generated with Claude Code

@ymichael
ymichael force-pushed the bb/live-data-stack-6-storage-retention-thr_uzjechs2j6 branch from bd5f83b to 704e48f Compare October 17, 2026 01:24
ymichael added a commit that referenced this pull request Oct 17, 2026
@ymichael
ymichael force-pushed the bb/live-data-stack-6-storage-retention-thr_uzjechs2j6 branch from 704e48f to 85bdbee Compare October 17, 2026 03:36
@ymichael
ymichael changed the base branch from bb/live-data-stack-2-plugin-reads-and-writes-on-cor-thr_agngjehcrj to bb/live-data-stack-4-concurrency-limit-thread-list-thr_adfmverzkq October 17, 2026 03:36
@ymichael
ymichael force-pushed the bb/live-data-stack-4-concurrency-limit-thread-list-thr_adfmverzkq branch from 6d9f283 to 32451bf Compare October 17, 2026 04:51
ymichael added a commit that referenced this pull request Oct 17, 2026
ymichael added a commit that referenced this pull request Oct 17, 2026
@ymichael
ymichael force-pushed the bb/live-data-stack-6-storage-retention-thr_uzjechs2j6 branch from 85bdbee to 20b1077 Compare October 17, 2026 04:51
@ymichael
ymichael force-pushed the bb/live-data-stack-4-concurrency-limit-thread-list-thr_adfmverzkq branch from 32451bf to 981caaa Compare October 17, 2026 05:19
ymichael added a commit that referenced this pull request Oct 17, 2026
ymichael added a commit that referenced this pull request Oct 17, 2026
@ymichael
ymichael force-pushed the bb/live-data-stack-6-storage-retention-thr_uzjechs2j6 branch from 20b1077 to 1051535 Compare October 17, 2026 05:19
@ymichael
ymichael force-pushed the bb/live-data-stack-4-concurrency-limit-thread-list-thr_adfmverzkq branch from 981caaa to ecc94d6 Compare October 19, 2026 22:20
ymichael added a commit that referenced this pull request Oct 19, 2026
ymichael added a commit that referenced this pull request Oct 19, 2026
@ymichael
ymichael force-pushed the bb/live-data-stack-6-storage-retention-thr_uzjechs2j6 branch from 1051535 to 9918fb7 Compare October 19, 2026 22:20
@ymichael
ymichael force-pushed the bb/live-data-stack-4-concurrency-limit-thread-list-thr_adfmverzkq branch from ecc94d6 to 89ec38b Compare October 19, 2026 22:49
ymichael added a commit that referenced this pull request Oct 19, 2026
ymichael added a commit that referenced this pull request Oct 19, 2026
@ymichael
ymichael force-pushed the bb/live-data-stack-6-storage-retention-thr_uzjechs2j6 branch from 9918fb7 to 52cd1df Compare October 19, 2026 22:49
ymichael added a commit that referenced this pull request Oct 20, 2026
…mutations (#5640)

## Human comments

## What was wrong

**Stack 1 of 14.** Merge order: #5640 → #5658 → #5688 → #5661 → #5669 →
#5666 → #5659 → #5660 → #5663 → #5667 → #5668 → #5677 → #5673 → #5670.

When a core write changes data that other screens or clients show,
someone has to remember to refresh every query that depends on it. Today
each write does this by hand: a cache-owner module plus a system change
kind (for UI preferences, `ui-preferences-changed`, routed through the
realtime registry). Plugins have no equivalent at all, which is #4944.

This is step 1 of a stack that replaces #5582. Here, a contract declares
what a read depends on and what a write changes, and the client
refreshes from those declarations. The plugin hooks in later steps will
be thin wrappers over this same core path.

## What changed

- **`@bb/domain`:** a new `ContractTagDeclarations` type with
`tags(input, output)` and `invalidates(input, output)`. It contains
types only.
- **`@bb/hono-typed-routes`:**
  - `defineRoute` accepts these declarations.
- After a 2xx JSON response, `typedRoutes` publishes the route's
`invalidates` tags through a new `invalidateTags` option.
- Registering a route that declares `invalidates` throws if that option
is missing.
- **Server:** `hub.notifyTags` batches tags within a microtask and sends
`{type:"changed", entity:"tags", tags}` to every connected client. This
is a new message between the server and the app; the daemon is
untouched, so `HOST_DAEMON_PROTOCOL_VERSION` is unchanged.
- **New `@bb/query-tags` package:**
  - Tags are registered on queries through `meta`.
- `invalidateTags` refreshes the queries whose data carries a matching
tag, within a scope. Core uses `core`.
- A read that is in flight restarts only if an overlapping invalidation
arrives during it.
- `runTaggedMutation` settles only after the affected active reads have
re-read. That includes a first load whose tags aren't known yet, which
the reference branch got wrong.
- **App:**
- The realtime cache effects pass `tags` messages to `invalidateTags`,
and hold them while the tab is hidden.
- A small `route-tags.ts` adapter connects route contracts to these
reads and mutations.
- **UI preferences, which prove the mechanism:**
- The read declares `ui-preferences`, and update and reset invalidate
it.
  - The sync engine saves through `runRouteMutation`.
- `ui-preferences-cache-owner.ts`, the `ui-preferences-changed` change
kind and its registry entry are deleted.
- The sync keeps each acknowledged entry, so a failed re-read neither
reverts the preference nor bases the next write on a stale revision.

Not in this PR: plugin SDK hooks, plugin RPC, plugin WebSocket targets
and the CLI. Those come in later steps of the stack, each with its first
caller.

## How you verified

- `pnpm exec turbo run build typecheck lint test
--filter='...[origin/main]' --continue`: 296/296 tasks passed.
  - Lint has no errors, and the app's warnings match `origin/main`.
- New tests in `@bb/query-tags` cover tag matching, scope isolation,
stale inactive queries, in-flight races, and refresh failure and
recovery.
  - The first-load race test fails against the reference implementation.
- `@bb/hono-typed-routes` tests: invalidations are published only after
2xx JSON responses, and registration fails without the publisher.
- Two isolated browser profiles against a source server: a preference
write in one refreshed the footer preferences in the other, and a
composer draft survived the refresh.
- Two WebSocket clients with no subscriptions both received the `tags`
message after an HTTP write.
- Sidebar perf, five paired samples per checkout with 3,000 threads,
against `origin/main`:

| Metric | Main median (range) | Branch median (range) |
| --- | ---: | ---: |
| Mount, ms | 410 (404–419) | 408 (359–412) |
| Pin, ms | 72 (71–73) | 71 (70–73) |
| Status patch, ms | 69 (69–71) | 70 (69–73) |
| Membership refresh, ms | 103 (103–107) | 107 (103–108) |
| Query observers | 13 | 13 |

The ranges overlap and the number of query observers is unchanged. These
samples were taken before the unused pieces were removed; removing them
deleted code paths without changing the read path.

Part of #4944

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@ymichael
ymichael force-pushed the bb/live-data-stack-4-concurrency-limit-thread-list-thr_adfmverzkq branch from 89ec38b to d23a5ad Compare October 20, 2026 19:46
ymichael added a commit that referenced this pull request Oct 20, 2026
ymichael added a commit that referenced this pull request Oct 20, 2026
@ymichael
ymichael force-pushed the bb/live-data-stack-6-storage-retention-thr_uzjechs2j6 branch from 52cd1df to 38dcf61 Compare October 20, 2026 19:46
@ymichael
ymichael force-pushed the bb/live-data-stack-4-concurrency-limit-thread-list-thr_adfmverzkq branch from d23a5ad to afd991d Compare October 20, 2026 21:19
@ymichael
ymichael force-pushed the bb/live-data-stack-6-storage-retention-thr_uzjechs2j6 branch from 38dcf61 to 64f6ffd Compare October 20, 2026 21:19
@ymichael

Copy link
Copy Markdown
Collaborator Author

Closing as superseded. Plugin watching was rebuilt on core's watched-target registry in #5929 + #5939. The plugin migrations will be rebuilt one at a time from main on the merged #5658 API. This branch stays available for reference.

@ymichael ymichael closed this Oct 21, 2026
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