Commit 5fb6c2a
authored
feat(core): add coalesced harness display-state subscriptions (#15974)
Adds Harness.subscribeDisplayState() for UI, SSE, TUI, and bridge
consumers that render from HarnessDisplayState. Raw subscribe() still
delivers every event for logs, debugging, analytics, and replay.
Example usage:
```ts
render(harness.getDisplayState());
const unsubscribe = harness.subscribeDisplayState(render, {
windowMs: 250,
maxWaitMs: 500,
});
```
The scheduler coalesces high-frequency message and tool updates, emits
fresh display-state snapshots, and flushes critical lifecycle updates
immediately.
Fixes #15973.
Validated with `pnpm --filter ./packages/core test -- display-state`,
`pnpm --filter ./packages/core check`, and `pnpm build:core`.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## ELI5
This PR adds a smarter way for UI components to listen to harness
updates. Instead of getting bombarded with every single internal event,
they can now use `subscribeDisplayState()` to receive batched updates
that combine rapid changes into fewer snapshots, while keeping the old
raw `subscribe()` method unchanged for debugging and logging.
## Overview
This PR introduces `Harness.subscribeDisplayState()`, a new UI-facing
subscription API that delivers coalesced `HarnessDisplayState`
snapshots. It implements intelligent batching of high-frequency internal
events (messages, tool updates) into consolidated display-state
snapshots, while preserving the raw event semantics of the existing
`subscribe()` API. Critical lifecycle events (agent start/end, errors,
approvals, suspensions, questions, plans) bypass coalescing and flush
immediately.
## Key Changes
### New Display State Scheduler Module
- **`display-state-scheduler.ts`**: Introduces `DisplayStateScheduler`
class that buffers non-critical display state updates and dispatches
them using two configurable timers:
- `windowMs` (default 250ms): throttle window that resets on each
non-critical update
- `maxWaitMs` (default 500ms): maximum wait time to prevent starvation
- Critical updates bypass buffering and flush immediately
- Deep-clones entire `HarnessDisplayState` before invoking listeners to
ensure isolation
- Catches and logs listener errors via `console.error` without
disrupting other listeners
- Exports constants: `DEFAULT_DISPLAY_STATE_SUBSCRIPTION_OPTIONS` and
`CRITICAL_DISPLAY_STATE_EVENT_TYPES`
### API Additions to Harness
- **`getDisplayState()`**: Returns a snapshot of current
`HarnessDisplayState` for initial rendering
- **`subscribeDisplayState(listener, options?)`**: Registers a coalesced
display-state listener that returns an unsubscribe function
- Accepts optional `windowMs` and `maxWaitMs` tuning parameters
- Listener is not invoked immediately (caller must use
`getDisplayState()` for initial render)
- All subscriptions are cleaned up during `harness.destroy()`
### Integration Updates
- Modified `Harness.emit()` to notify schedulers after dispatching
events, passing `isCritical` flag based on event type
- Event type `display_state_changed` is skipped to avoid
double-notification
- Harness destroy method extended to dispose and clear all registered
schedulers
### Type System Enhancements
- **New types in `types.ts`**:
- `HarnessDisplayStateListener`: callback type receiving
`HarnessDisplayState`, may be sync or async
- `HarnessDisplayStateSubscriptionOptions`: configuration interface for
`windowMs` and `maxWaitMs`
- **Index re-exports**: Both new types exported from harness module
### Documentation
- Updated harness-class.mdx with comprehensive documentation of both new
APIs
- Clarified that `subscribe()` is intended for raw event logs while
`subscribeDisplayState()` is preferred for UI rendering
- Added changeset documenting the new API with usage examples
## Testing
Comprehensive test suite in `display-state.test.ts` covering:
- Coalescing behavior driven by `windowMs` with fake timers
- Forced flushes at `maxWaitMs` to prevent starvation
- Critical event types trigger immediate (non-coalesced) emission
- Emitted snapshots are fresh deep-clones isolated from harness internal
state
- Unsubscribe and `harness.destroy()` properly cancel pending timer
flushes
- Custom coalescing options change flush timing as configured
- Raw subscriptions continue to receive every event while display
subscribers remain coalesced
- Listener errors are caught and logged without preventing other
listeners
- Validation of key snapshot fields like `isRunning`, `pendingApproval`,
`pendingSuspension`, etc.
## Validation
Changes have been validated with:
- `pnpm --filter ./packages/core test -- display-state`
- `pnpm --filter ./packages/core check`
- `pnpm build:core`
Fixes issue #15973.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 2c83efc commit 5fb6c2a
7 files changed
Lines changed: 602 additions & 1 deletion
File tree
- .changeset
- docs/src/content/en/reference/harness
- packages/core/src/harness
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
347 | 347 | | |
348 | 348 | | |
349 | 349 | | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
350 | 358 | | |
351 | 359 | | |
352 | 360 | | |
| |||
917 | 925 | | |
918 | 926 | | |
919 | 927 | | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
920 | 951 | | |
921 | 952 | | |
922 | 953 | | |
923 | 954 | | |
| 955 | + | |
| 956 | + | |
924 | 957 | | |
925 | 958 | | |
926 | 959 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
0 commit comments