Commit 87be539
fix(agents): pull-wake runner resilience — fetch cache stacking, hung connections, xstate lifecycle (#4542)
## Summary
Fixes two bugs that left the desktop agents runtime unable to pick up
sessions until a full app restart, then ports the pull-wake runner's
lifecycle to an xstate v5 state machine so this class of bug can't recur
silently. No public API changes — all 21 pre-existing behavioral tests
pass unmodified.
## Root cause
Two independent failures combined into "runner looks alive but never
picks up work, and restarting the runtime doesn't help":
1. **Hung connection couldn't be aborted.** The heartbeat-failure →
stream-reset path (`requestStreamReconnect`) was a no-op unless
`streamConnected` was true. If the wake stream hung while
*(re)connecting* (e.g. after an agents-server restart), heartbeat
failures counted up but nothing could cancel the stuck `streamFactory`
call — the runner sat in `connecting` forever.
2. **Runtime restart stacked cache interceptors.** Every
`BuiltinAgentsServer.start()` composed a new undici cache interceptor
onto the global dispatcher without a guard. Restarting the runtime
in-process stacked SQLite-backed cache layers over the same file; only a
full app restart got a clean dispatcher.
## Approach
- `installDurableStreamsFetchCache` gets a module-level installed guard
(warns on repeat calls).
- The runner lifecycle moves into an xstate machine
(`pull-wake-machine.ts`): `stopped → running.{connecting, streaming,
reconnecting} → stopping`. The structural win: **invoked actors are
auto-aborted on state exit**, so a `STREAM_RESET` event during
`connecting` cancels the in-flight connect via the promise actor's
signal — no manual `connectAbort` bookkeeping, which is how the original
bug slipped through. Backoff timers (`after`) cancel the same way.
- Diagnostics, heartbeat coalescing, and claim processing stay in the
runner closure as effects the machine triggers
(`PullWakeMachineEffects`). The machine decides *when*; the closure does
*what*.
## Key invariants
- Every (state × event) pair is pinned by an exhaustive 35-case
transition matrix test — adding a state or event forces a deliberate
decision.
- Repeated heartbeat failures (≥2) reset the stream from **any** running
substate.
- One shutdown sequence regardless of concurrent `stop()` calls;
`stop()` rejects with drain errors.
- Claim actors survive stream reconnects and only drain (1s grace) at
stop.
## Non-goals
- No behavioral changes to claiming, heartbeat cadence, or backoff
timing (1s → ×2 → 30s cap preserved).
- Heartbeat send/coalescing logic was not moved into the machine — it's
effect plumbing, not lifecycle.
## Verification
```bash
pnpm --filter @electric-ax/agents-runtime exec vitest run test/pull-wake-runner.test.ts # 61 tests
pnpm --filter @electric-ax/agents exec vitest run test/durable-streams-cache.test.ts # 2 tests
pnpm --filter @electric-ax/agents-runtime build
```
Full agents-runtime suite: 852 passed; 2 pre-existing environment
failures unrelated to this PR (`sandbox-docker` Docker-timing assertion,
`tool-providers` unbuilt workspace dep).
## Files changed
- `packages/agents-runtime/src/pull-wake-machine.ts` — new: xstate v5
lifecycle machine + effects interface
- `packages/agents-runtime/src/pull-wake-runner.ts` — rewritten as a
thin adapter: same public API, closure keeps
diagnostics/heartbeat/claims, machine owns lifecycle
- `packages/agents-runtime/test/pull-wake-runner.test.ts` — +1
hung-connection regression test, +40 machine transition tests; 21
original tests untouched
- `packages/agents/src/durable-streams-cache.ts` — idempotency guard +
warning
- `packages/agents/test/durable-streams-cache.test.ts` — idempotency
test
- `packages/agents-runtime/package.json` — adds `xstate` (zero runtime
deps)
- `.changeset/pull-wake-runner-resilience.md` — patch bumps for both
packages
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent 004bea1 commit 87be539
8 files changed
Lines changed: 645 additions & 186 deletions
File tree
- .changeset
- packages
- agents-runtime
- src
- test
- agents
- src
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| 128 | + | |
128 | 129 | | |
129 | 130 | | |
130 | 131 | | |
| |||
| 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 | + | |
| 221 | + | |
| 222 | + | |
0 commit comments