Commit 94bc4d1
authored
Fix remote-device tool timing out on scheduled runs (Redis-backed broker) (#2511)
* fix: route remote-device tool through Redis so scheduled runs reach the device
The remote-device tool worked interactively but timed out on every scheduled
run. DeviceBroker was an in-process, in-memory singleton, but scheduled runs
execute in the Celery worker — a different process from the gunicorn web tier
that holds the device's SSE session — so a worker-side dispatch never reached
the device and the tool always hit its deadline.
Make the broker Redis-backed so every hop crosses the process boundary:
- queued commands -> Redis list dev:cmd:{device_id}
- output chunks -> Redis stream dev:out:{invocation_id}
- invocation metadata -> Redis hash dev:inv:{invocation_id}
- SSE upgrade tickets -> Redis key dev:ticket:{device_id}
Per-connection SSE session state stays in the web process. Reuses the existing
get_redis_instance()/CACHE_REDIS_URL; no new infrastructure. Also makes the web
tier safe to scale past one worker.
Concurrency hardening (from adversarial review + real-Redis e2e):
- XADD the output/control chunk before flipping completed=1, and have
drain_output do a final non-blocking flush after observing completion, so a
reader can't see completion and stop before the control chunk lands (this had
reintroduced the false "device did not respond (timed out)" under a race).
- _collect_result builds the result from drained chunks, checks the deadline
only after capturing a chunk, and falls back to the authoritative snapshot
(before cleanup) when no control chunk was observed.
- Audit outcome is written from locally-known fields so it survives the worker
racing to delete the invocation; a denied command now records a terminal
"denied" outcome instead of staying "dispatched".
- cmd-queue TTL raised to 900s (>= max drain deadline); dispatch-failure and
reaped-invocation cleanup; UTF-8 byte counts.
Tests: new tests/devices/{conftest (FakeRedis double), test_broker_cross_process,
test_broker_race, test_submit_output_audit}; drain/cleanup/ticket tests rewritten
for the Redis contract. The race tests fail against the pre-fix code. ruff clean;
device + tool-executor suites green.
* fix: log instead of silently passing on failed-dispatch cleanup
Addresses the code-quality lint on the best-effort hash delete in
dispatch_invocation's failure path: replace the bare `except: pass` with a
logger.debug carrying the invocation_id. No behavior change — cleanup stays
best-effort and still returns a failed Invocation.1 parent 666db29 commit 94bc4d1
11 files changed
Lines changed: 1396 additions & 387 deletions
File tree
- application
- agents/tools
- api/devices
- core
- devices
- tests/devices
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
249 | | - | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
250 | 266 | | |
251 | 267 | | |
252 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
253 | 273 | | |
254 | 274 | | |
255 | 275 | | |
256 | 276 | | |
257 | | - | |
258 | | - | |
259 | 277 | | |
260 | 278 | | |
261 | 279 | | |
262 | 280 | | |
263 | 281 | | |
264 | 282 | | |
265 | | - | |
266 | | - | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
267 | 302 | | |
268 | 303 | | |
269 | 304 | | |
270 | | - | |
| 305 | + | |
271 | 306 | | |
272 | | - | |
273 | | - | |
| 307 | + | |
| 308 | + | |
274 | 309 | | |
275 | 310 | | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
280 | 315 | | |
281 | | - | |
| 316 | + | |
282 | 317 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
| |||
118 | 117 | | |
119 | 118 | | |
120 | 119 | | |
121 | | - | |
122 | | - | |
123 | | - | |
| 120 | + | |
| 121 | + | |
124 | 122 | | |
125 | 123 | | |
126 | 124 | | |
127 | 125 | | |
128 | 126 | | |
129 | 127 | | |
130 | | - | |
| 128 | + | |
131 | 129 | | |
132 | 130 | | |
133 | 131 | | |
| |||
164 | 162 | | |
165 | 163 | | |
166 | 164 | | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
167 | 181 | | |
168 | 182 | | |
169 | 183 | | |
| |||
189 | 203 | | |
190 | 204 | | |
191 | 205 | | |
| 206 | + | |
192 | 207 | | |
193 | 208 | | |
194 | 209 | | |
| |||
199 | 214 | | |
200 | 215 | | |
201 | 216 | | |
| 217 | + | |
| 218 | + | |
202 | 219 | | |
203 | 220 | | |
204 | 221 | | |
205 | | - | |
206 | | - | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
207 | 230 | | |
208 | | - | |
209 | 231 | | |
210 | 232 | | |
211 | 233 | | |
212 | 234 | | |
213 | | - | |
214 | | - | |
| 235 | + | |
| 236 | + | |
215 | 237 | | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
229 | 244 | | |
230 | 245 | | |
231 | 246 | | |
| |||
235 | 250 | | |
236 | 251 | | |
237 | 252 | | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
238 | 263 | | |
239 | 264 | | |
240 | 265 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
244 | 252 | | |
245 | 253 | | |
246 | 254 | | |
| |||
0 commit comments