Repository navigation
Description
Problem
Every J5 read surface refreshes on a foreground timer whether or not anything changed. On the crews stack head (crews/08) there are six visible-refresh polls:
| Surface | Interval | Where |
|---|---|---|
| Inbox bell count | 7.5s | apps/web/src/j5/a2a/HumanInboxBell.tsx |
| Inbox page | 7.5s | apps/web/src/j5/a2a/HumanInboxPage.tsx |
| Crew proposals (roster gate + bell) | 7.5s | apps/web/src/j5/crew/crewProposalsClient.ts |
| Fleet page + rail badge | 30s | apps/web/src/j5/fleet/fleetClient.ts |
| Fleet detail | 30s | apps/web/src/j5/fleet/fleetClient.ts |
| Squadron directory | 30s | apps/web/src/j5/squadron/SquadronDirectory.ts |
Each tick is a full re-read per connected environment. The Fleet tick also re-pends every requested thread id through threadHomes.refreshRequested, so memberships and children are re-read for the whole thread list in batches of 200 (#149 review). Every new J5 surface has added another timer. AGENTS.md: "We regularly audit for performance regressions, often caused by sending too much data over websockets".
Proposal
One J5 change stream per environment, built the way the artifact change stream was in #145:
Contract. packages/contracts/src/j5/reads.ts (new): J5_READS_WS_METHODS.subscribeChanges, an Rpc.make with stream: true, payload {} (per-environment, the session is the scope), success { source: "inbox" | "crewProposals" | "fleet" | "squadrons", revision: number }, error union with EnvironmentAuthorizationError. J5ReadsRpcGroup merged into WsRpcGroup beside the persona and artifact groups (one .merge(...) line in rpc.ts).
Server. A small in-process hub under apps/server/src/j5/a2a/ that the existing services publish to after their commits: the ledger (inbox), AgentCrewProposalService (proposals), PlacementService / HomeRegistrar / AgentCrewInstanceService (fleet), squadron management (squadrons). The RPC handler subscribes to the hub and emits one event per source change with a monotonic revision, coalescing bursts. Handler built in a J5 module and spread into the ws.ts handler map; scope spread into RPC_REQUIRED_SCOPES (orchestration:read). Same four one-line upstream touch points as case 33.
Client. One createEnvironmentRpcSubscriptionAtomFamily in packages/client-runtime/src/j5/ (tag added to the EnvironmentSubscriptionRpcTag union, as #145 did). Each event calls the refresh function the poll calls today (refreshFleet, refreshCrewProposals, the inbox count and page refreshes, the directory refresh), so the read sources, the cross-environment merge, and the capability probe are untouched. The timers drop to one long fallback (a few minutes) or go away. The subscription is durable across reconnects through the supervisor, like every other J5 stream; a server that lacks the method is detected the same way case 34 detects a missing route, and that client keeps polling.
Why this shape
- The notification is a few bytes on the socket that already exists and reconnects durably; the reads stay on HTTP where per-request auth, the capability probe, and the multi-environment merge already live. Same split as artifacts, which is now verified on direct, Tailscale, SSH, hosted web, and T3 Connect.
- Fixed upstream cost, once, instead of one more timer per surface.
- Mobile gets the same stream for free when it grows a J5 surface, through client-runtime.
Not in scope
- Do not block the crews stack on this. feat(web): the Fleet page and the sidebar's spawned-children expander #149 asks for the narrow in-PR fix (poll only rows that already have memberships or children; cache empty results).
- No payloads in the stream. It says what changed, never what the change was; the read models stay the source of truth.
Came out of the transport discussion during the #145 review and the Fleet poll finding in #149.
Activity
feat(j5): J5's read models follow a change stream, not timers (#586)