Skip to content

Move workflows onto tagged live reads - #5660

Closed
ymichael wants to merge 3 commits into
bb/live-data-stack-6-storage-retention-thr_uzjechs2j6from
bb/live-data-stack-7-workflows-thr_yb23e6wnnq
Closed

ymichael wants to merge 3 commits into
bb/live-data-stack-6-storage-retention-thr_uzjechs2j6from
bb/live-data-stack-7-workflows-thr_yb23e6wnnq

Conversation

@ymichael

@ymichael ymichael commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The workflows UI kept itself fresh by hand:

  • workflow-runs signals;
  • a 15-second fallback poll;
  • a failed-poll backoff;
  • coalescing, request-sequence guards and visibility handling.

Refresh errors could be hidden: a warning kept promising retries after they had stopped, and a cached "Workflow run not found" covered up a later failure.

Stack 7 of 13, stacked on #5659. #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

  • workflowActiveRuns and workflowRunView declare the thread's runs:<threadId> tag, including when the lookup finds nothing. workflowStopRun invalidates it on every successful response, including stopped:false.
  • The workflow service publishes progress and lifecycle changes through bb.rpc.experimental_invalidate instead of signals.
  • The UI uses experimental_useRpcQuery and experimental_useRpcMutation, so it inherits core's cache, retry, reconnect and resume behaviour.
  • Removed: the poll, the backoff, signal subscriptions, coalescing, sequence guards, visibility handling and realtime-channel.ts.
  • One UI interval remains. It updates elapsed labels once per second and fetches nothing.
  • No SDK or mechanism changes. Net −265 lines.

Behaviour changes:

  • Initial-load errors offer Retry.
  • Refresh failures keep the loaded preview, panel and active-run banner visible with an inline Retry. Warnings no longer promise retries that won't happen.
  • A refresh failure after a cached missing-run result shows the actual error and Retry instead of the stale "not found".
  • Elapsed labels in the preview and panel tick once per second and freeze at completion.
  • Stop refreshes all of the thread's run views, even when it returns stopped:false for a run that had already finished.

How you verified

Behaviour Result
Warning stops claiming "retrying" Passed
Refresh error overrides cached "not found" Passed
Running elapsed labels keep updating Passed (CLI-started run in an open thread)
Active banner keeps stale data with Retry Passed
Initial and refresh errors offer Retry; success clears them Passed
Stop on an active run, and on an already-terminal run (stopped:false), refreshes views Passed
Plugin-only failed-read retry timer Deliberately removed (no plugin retry timer; core lifecycle)

Part of #4944

AGENT GENERATED

🤖 Generated with Claude Code

@ymichael
ymichael force-pushed the bb/live-data-stack-7-workflows-thr_yb23e6wnnq branch from 8189989 to e928c32 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-7-workflows-thr_yb23e6wnnq branch from e928c32 to 2d91f1d 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-6-storage-retention-thr_uzjechs2j6 October 17, 2026 03:36
@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 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-7-workflows-thr_yb23e6wnnq branch from 2d91f1d to eee5436 Compare October 17, 2026 04:51
@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 added a commit that referenced this pull request Oct 17, 2026
@ymichael
ymichael force-pushed the bb/live-data-stack-7-workflows-thr_yb23e6wnnq branch from eee5436 to 3ac0069 Compare October 17, 2026 05:19
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 1051535 to 9918fb7 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-7-workflows-thr_yb23e6wnnq branch from 3ac0069 to 5386514 Compare October 19, 2026 22:20
@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 19, 2026
ymichael added a commit that referenced this pull request Oct 19, 2026
@ymichael
ymichael force-pushed the bb/live-data-stack-7-workflows-thr_yb23e6wnnq branch from 5386514 to 4ed0dde 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-6-storage-retention-thr_uzjechs2j6 branch from 52cd1df to 38dcf61 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-7-workflows-thr_yb23e6wnnq branch from 4ed0dde to 258e30d Compare October 20, 2026 19:46
@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
ymichael force-pushed the bb/live-data-stack-7-workflows-thr_yb23e6wnnq branch from 258e30d to a5cc05c 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
ymichael added a commit that referenced this pull request Oct 28, 2026
## Human comments

## What was wrong

The Workflows UI kept itself fresh by hand. `src/app.tsx` subscribed to
a `workflow-runs` realtime channel, ran a 15-second fallback poll while
a run was active, backed failed reads off from 2 s up to 60 s, coalesced
signal bursts, guarded against stale responses with request sequence
numbers, and refetched when the tab or realtime connection came back.
All of that duplicates what core's live-read lifecycle already does for
`plugins/git`, `plugins/github-pr-status` and `plugins/docs`.

This rebuilds #5660 on main. #5660 was stacked on SDK changes that never
landed, and it also bundled new behaviour (inline Retry rows, Retry on
errors), which this PR leaves out. No Plugin SDK changes were needed.

## What changed

- `ui-contract.ts`: `workflowActiveRuns` and `workflowRunView` declare
the origin thread's `runs:<threadId>` tag, and `workflowStopRun`
invalidates it.
- `service.ts`: `publishRunsChanged` now calls
`bb.rpc.experimental_invalidate([`runs:<threadId>`])` instead of
`bb.realtime.publish`. It runs at the same points as before: start,
claim, phase or call change, settle, cancel. `realtime-channel.ts` is
deleted.
- `app.tsx`: the two read hooks are now `experimental_useRpcQuery`, and
Stop uses `experimental_useRpcMutation`. The poll, backoff, coalescing,
sequence guards and visibility/reconnect handling are deleted (−318/+78
in `app.tsx`). Load, error and refresh-error states map onto the same UI
as before, and a loaded "Workflow run not found" is still replaced by a
later read error. Stop keeps "Stopping…" until the refreshed view lands,
as it did on main.
- README: the paragraph describing the refresh mechanism is updated.

**User-visible differences** (each one forced by the deleted poll; there
are no other UI changes):
- **Refresh warning text.** "Could not refresh: X. Retrying…" is now
"Could not refresh: X." Without the plugin's backoff loop, the next
attempt comes from the next invalidation, a reconnect or a resume, not a
timer. Keeping "Retrying…" would bring back the false promise #5660
found.
- **Elapsed labels.** On main, the duration labels in the message
preview and the side panel only advanced when a poll or signal
re-rendered them, about every 15 s (measured max gap: 15.0 s). The
branch has no poll, so those labels now tick locally once per second
while a run is active. That reuses the 1 s tick the composer card
already had, and the label freezes once the run finishes. Without it
they would freeze for the whole length of a long agent call.
- **Banner on a failed refresh.** On main, one failed
`workflowActiveRuns` refresh hid the composer banner until the next
successful retry. The branch keeps showing the last loaded runs, because
core retains data through refresh errors.
- **Retries after HTTP failures.** Core does not retry HTTP failures on
a timer; it retries network failures twice at 250 ms. A read that fails
with an HTTP error now recovers on the next invalidation, reconnect or
resume. For an active run, invalidations arrive with every progress
change.

Neither bug #5660 listed reproduces on main today. I checked with a
probe test against main's code: the warning really did retry and cleared
after recovery, and a later failure already replaced a cached "Workflow
run not found". The second case now has a regression test.

No server/daemon wire changes. No CLI or SDK surface changes.

## How you verified

- `pnpm exec turbo run typecheck lint test
--filter=bb-plugin-workflows`: 247/247 tests passed, lint reports 4
pre-existing warnings (main has 7 in `app.tsx`).
- `pnpm exec turbo run build typecheck lint test
--filter='...[origin/main]'`: 189/191 tasks passed. The two failures
were in unrelated files (`install-machine-script`, `builtin-plugins`,
`FilePreview`) while the machine's load average was 23–38. `FilePreview`
passed on its first rerun; `builtin-plugins` failed on a different test
on the first rerun (a 40 s hot-reload timeout) and passed 28/28 on the
second.
- New app tests drive real tag invalidations
(`experimental_invalidateRpc`):
- only the thread's own tag refreshes the banner, and runs stay visible
through a failed refresh;
- initial and refresh failures recover on the next invalidation, and the
warning makes no retry promise;
  - a loaded missing run is replaced by a later read error;
  - the elapsed label ticks while running and freezes once finished.

Reverting the missing-run branch or the label tick makes the
corresponding test fail.
- Service tests now assert `runs:origin` invalidations for start, claim,
cancel, and phase and call progress.

**Main vs branch on one real `pnpm dev` instance** (fresh store,
scripted echo provider so agent calls are real worker threads with fixed
`delay:` lengths). Each comparison swaps only `plugins/workflows` and
reloads the plugin. Requests were counted in-page on `/rpc/workflow*`,
and UI timing was taken from DOM changes against each call's server-side
`finishedAt`:

| Flow | main | branch |
| --- | --- | --- |
| 56 s run (2 parallel agents, then 1), thread open with banner +
preview: workflow RPCs | 28 / 27 | 22 |
| …of which 15 s fallback polls | 6 | 0 |
| 60 s idle after completion | 0 | 0 |
| Call finished → preview count updates | 20–39 ms | 27–62 ms |
| Run finished → preview "Complete" / banner removed | 14–36 ms | 25–51
ms |
| Preview elapsed label, max gap between updates | 15.0 s | 1.0 s |
| Open in right panel → Stop button shown | 141 ms | 79–108 ms |
| Click Stop → panel shows Cancelled (button stays "Stopping…"
throughout) | 49 / 76 / 62 ms | 49 / 42 / 94 ms |
| RPCs in the 10 s after Stop | 8 | 10 |
| Tab hidden while two calls finish, then shown → preview + banner
correct | 107 / 264 ms (0 reads while hidden) | 150 / 152 ms (1 read
pair while hidden) |

One branch Stop sample took 395 ms. Its first read started before the
server had finished cancelling, and the next three samples were 42–94
ms.

I also clicked through the branch in the running app: expanded the
banner card, used Open in right panel, and clicked Stop (the CLI reports
`cancelled`). I injected 500s on `workflowRunView` through browser
request interception: the preview and panel showed "Could not refresh:
injected outage." with the run still visible, and the warning cleared on
the next invalidation.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Michael Yong <review@local>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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