Skip to content

Move the Workflows plugin onto shared live reads - #6418

Merged
ymichael merged 1 commit into
mainfrom
bb/workflows-plugin-onto-shared-live-reads-thr_p6zy4gjart
Oct 28, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/workflows-plugin-onto-shared-live-reads-thr_p6zy4gjart

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

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:]) 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 Move workflows onto tagged live reads #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

@ymichael
ymichael merged commit 0171f68 into main Oct 28, 2026
30 checks passed
@ymichael
ymichael deleted the bb/workflows-plugin-onto-shared-live-reads-thr_p6zy4gjart branch October 28, 2026 18:32
ymichael added a commit that referenced this pull request Oct 28, 2026
## Human comments

## What was wrong

`invalidates` on a plugin RPC contract method only fired when a write
went through that RPC method (`invokeRpcHandler` →
`hub.notifyPluginChanged`, plus the calling client invalidating the
response's `invalidated` tags). Plugin CLI commands, agent tools and
background work usually call the plugin's functions directly, so the
server already has to call `bb.rpc.experimental_invalidate` where the
data changes. An app-made write then refreshed twice: once from the
broadcast and once from the client's own `invalidated` handling. The
rule is now that the server marks tags stale where the data changes, and
nothing else does.

## What changed

**First-party users.** All four already invalidated where the data
changes, so their declarations are just deleted. No new invalidate call
was needed:
- Git (`plugins/git/rpc-contract.ts`) `markViewed`, `clearViewed`,
`setMergeBase`: core emits `diff-viewed-changed` / `metadata-changed`,
and `environmentChanged` in `plugins/git/server.ts` invalidates the
viewed and status tags for watched environments. The CLI (`bb git viewed
mark|unmark`, `bb environment update --merge-base-branch`) goes through
the same events.
- GitHub PR Status (`plugins/github-pr-status/contract.ts`) `refresh`,
`markReady`, `markDraft`, `merge`: each ends in a store load, and
`createPullRequestStore`'s `publish` broadcasts the row's tag when the
row changed. The store covers these cases by itself. The old
`invalidates` refetch ran before that reload and read the old row back,
so it was a wasted request.
- Push notifications (`plugins/push-notifications/contract.ts`)
`threadNotifications.set`: `preferences.set` calls `publishSubtree`,
shared with `bb push-notifications thread --level`.
- Workflows (`plugins/workflows/src/ui-contract.ts`) `workflowStopRun`,
added by #6418: `service.stop` publishes `runs:<threadId>`, shared with
`bb workflows stop`.

**Retired the way #6388 retired `experimental_threadHeaderAction`:**
- `PluginRpcMethodContract` (`packages/plugin-sdk/src/rpc-contract.ts`)
extends `Omit<ContractTagDeclarations, "invalidates">`, so declaring it
in `defineRpcContract` is a type error. `ContractTagDeclarations` in
`@bb/domain` is untouched: core `defineRoute` descriptors keep
`invalidates`, and the edit stays one line for the parallel move of that
type into the SDK.
- It still works at runtime. `readRpcMethodContract`,
`invokeRpcHandler`, `rpcResponseTags` and the fake host are unchanged,
so already-built plugins broadcast and refresh as before.
- `bb-plugin-build` warns when a server source file inside the plugin's
own directory declares `invalidates` (`retiredRpcInvalidatesWarnings` in
`packages/plugin-build/src/retired-sdk-members.ts`). The warning names
the replacement. The check is limited to the server build and the
plugin's own directory: contracts are always registered on the server,
so the warning prints once, and bundled workspace packages such as
`@bb/server-contract`, whose core route descriptors use `invalidates`,
don't trigger it. `bb plugin build`/`dev` print it, and the server
writes it to the plugin log.
- `docs/api_to_audit.md`: the live-reads bullet states the one rule, and
there's a new **Retired RPC contract `invalidates`** entry with its
callers.

**Marketplace callers.** I scanned all 364 git repositories and 14 npm
packages behind the community marketplace's 469 entries (2026-10-28).
Five plugins in two repos declare it:
- kalindudc/bb-marketplace: **loom** (`src/rpc-tags.ts`, spread into its
contract), **shuttle** and **vantage** (`src/contract.ts`)
- smsunarto/bb-plugins: **gtd-sidebar** (`lib/rpc-contract.ts`) and
**kitchen-sink**

**Docs.** The SDK doc comments on `defineRpcContract` and
`ExperimentalRpcMutationState` are updated. The Plugin Guide's wire card
(`plugins/plugin-api-docs/src/surfaces.ts`) states the rule. The
bb-plugin-authoring skill gets a "Tags and invalidation" section in
`frontend-hooks-and-ui.md`; `backend-events.md` is at its 500-line cap,
so its refusal line only drops "publishes no invalidations". Plugin SDK
bumped to 0.6.95.

No server/daemon wire change.

## How you verified

- `pnpm exec turbo run typecheck lint` for `@get-bb/plugin-sdk`,
`@bb/plugin-build`, the Git, GitHub PR Status, push-notifications,
Workflows, plugin-api-docs and bb-guide plugins, `@bb/server` and
`@bb/app`: pass.
- `turbo run test` for the same plugins, `@get-bb/plugin-sdk` and
`@bb/plugin-build`: pass. `@bb/server`: 3692/3698 on the first run. The
skills-quality cap failure was mine (fixed by moving the section). The
other failures were three builtin-plugins hot-reload timing tests that
ran while a dev instance was building; both files pass alone (65/65).
- New tests:
- `build-plugin-app.test.ts`: a server whose own `tags.ts` declares
`invalidates` (property and method forms) builds, keeps the field, and
gets exactly one warning. A file outside the plugin root that declares
it doesn't warn.
- `retired-sdk-members.test.ts`: the pattern flags property, quoted-key
and method declarations, and ignores `tags`, `.invalidates(` calls and
`invalidated`.
- `rpc-query-host.test.tsx`: the optimistic-mutation test drops
`invalidates` and invalidates after acknowledgement, as the server does.
Predictions still hold through acknowledgement, roll back on failure,
and keep the read error on refresh failure.
- `plugin-wire.test.ts`: the untyped built fixture that still declares
`invalidates` is renamed as the compatibility test. It still broadcasts.
- Live run on an isolated `pnpm dev` (fresh data dir), a scratch repo
project, with a thread open in a headless browser. A `fetch` hook
recorded every `/rpc/` request after each action:

| Write | From | Refetches seen in the open tab | Tab updated without
reload |
  | --- | --- | --- | --- |
| `setMergeBase` | merge-base picker | 1 `status` (+344 ms) | Branch →
other |
| `setMergeBase` | `bb plugin rpc call bb--git setMergeBase` | 1
`status` | Branch → other |
| merge base | `bb environment update --merge-base-branch main` | 1
`status` | Branch → main |
| `markViewed` | Viewed checkbox | 1 `viewed` (+277 ms) | a.txt checked
|
  | `markViewed` | `bb git viewed mark` | 1 `viewed` | b.txt checked |
| `clearViewed` | `bb git viewed unmark` | 1 `viewed` | a.txt unchecked
|
  | `clearViewed` | Viewed checkbox | 1 `viewed` | b.txt unchecked |
| `threadNotifications.set` | thread menu → Notifications → Muted | 1
`threadNotifications.keyed` | Muted |
| `threadNotifications.set` | `bb push-notifications thread --level
input-only` | 1 `threadNotifications.keyed` | Needs input only |

The picker's extra `branches` read after a merge-base change is the
branch list re-reading with its new `selectedBranch` input, not a
duplicate.
- Legacy fixture: a path plugin whose contract still declares
`invalidates`. `bb plugin build` printed the warning for `contract.ts`
once, and the server wrote it to the plugin's log on install. Clicking
Bump on its page updated the count from 0 to 1. Its request log shows
`bump` followed by two `count` reads, the double refresh this removes
for first-party plugins.
- GitHub PR Status, against this PR (#6462) from a clone of its branch:

| Write | From | Refetches seen in the open tab | Tab updated without
reload |
  | --- | --- | --- | --- |
| `markReady` | composer segment, Mark ready | 1 `pullRequests` (+5.0 s,
after `gh` and the store reload) | Draft → Open, Merge shown |
| `markDraft` | `bb pull-request draft` | 1 `pullRequests` | Open →
Draft, Mark ready shown |
| `refresh` | `bb plugin rpc call bb--github-pr-status refresh` | 1
`pullRequests` | yes |
| `markReady` | `bb pull-request ready` | 1 `pullRequests` | Draft →
Open |

`merge` was not run: it would merge this PR. It shares `runAction` →
`store.refresh` with ready and draft. The `bb--git status` reads that
also appear after these actions come from the Git plugin's environment
events (`gh` touches the checkout), which this PR doesn't change.

> AGENT GENERATED

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

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