Commit e6f11c4
authored
perf(streaming): coalesce per-token publishes to Redis (50ms / 128-char window) (#333)
* perf(streaming): coalesce per-token publishes to Redis (50ms / 128-char window)
Per-token Redis publishes from TemporalStreamingModel were adding ~45s
(56-62%) overhead to agent response latency, mostly from head-of-line
blocking on the model's event loop: each `await streaming_context.stream_update(...)`
inside the OpenAI stream `async for` paused token consumption until the
publish round-trip completed.
This change introduces a `CoalescingBuffer` driven by an `asyncio.Event`,
so the producer never awaits on Redis. Deltas are merged consecutive-only
(preserving character order in every (type, index) channel) and flushed
on a 50ms timer, on a 128-char size threshold, or immediately for the
first delta to keep perceived responsiveness high. The buffer's `close()`
drains remaining deltas before the DONE event, so consumers see the full
sequence in order.
A new `StreamingMode = Literal["off", "per_token", "coalesced"]` lives
in `streaming.py` as the single source of truth and is plumbed through
the adk streaming module, `StreamingService.streaming_task_message_context`,
and `StreamingTaskMessageContext`. Default is `"coalesced"` everywhere,
so all 13+ existing context callers (claude_agents, langgraph, litellm
provider, openai sync provider, etc.) benefit automatically.
* chore(streaming): fix import ordering (ruff I001)
* fix(streaming): address greptile review findings
- _run: when CancelledError is raised mid-flush in the for-loop, re-enqueue
the in-flight item plus any remaining items in the local `drained` list
back into self._buf so close()'s final drain can recover them. Previously
the local `drained` list was unreachable after CancelledError exited the
for-loop, causing the last coalesced batch to be silently dropped on
close-during-flush races. Trade-off: the in-flight item may be duplicated
on the consumer side (Redis pub may have completed before cancel was
delivered), which is preferable to silent loss for streaming UX.
- _merge_pair: replace `return b` fallback with AssertionError. All six
current TaskMessageDelta variants have explicit isinstance branches, so
the fallback is unreachable today. But _can_merge returns True for any
same-type pair, so adding a 7th delta variant without updating
_merge_pair would silently drop `a`'s accumulated content. Asserting
turns a future silent data-loss into an immediate, diagnosable crash.
* test(streaming): add coalescing-layer tests; loosen one model assertion
After merging the test-suite repair from main (#334) into this branch, one
model test (test_responses_api_streaming) regressed because its
assert_called_with strict-matched all kwargs of streaming_task_message_context
and didn't tolerate the new `streaming_mode='coalesced'` kwarg this PR
adds. Switched to assert_called() + targeted kwarg checks so the test
verifies what it cares about (task_id threading) without locking in
implementation details.
Replaced the ad-hoc smoke scripts that lived in conversation with a real
pytest module at tests/lib/core/services/adk/test_streaming.py covering:
- _delta_char_len, _can_merge, _merge_pair: per-channel correctness +
None-handling
- _merge_consecutive: pure-text collapse, cross-channel order preservation,
per-channel reconstruction matches per-token semantics
- CoalescingBuffer: first-delta-immediate flush within ~20ms,
size-threshold flush before timer fires, multi-delta coalescing within
one window, idle close, add-after-close no-op
- CoalescingBuffer cancel-during-flush regression test for the P1 fix:
five queued chunks must all surface across publishes when close()
cancels mid-flush (asserts substring presence rather than exact
ordering, since the documented trade-off allows duplicates of the
in-flight item)
- StreamingTaskMessageContext mode dispatch: "off" suppresses publishes
but persists full content, "per_token" publishes each delta synchronously,
"coalesced" batches and persists full content
* chore(streaming): route TemporalStreamingModel logger through make_logger
The model file used raw ``logging.getLogger("agentex.temporal.streaming")``,
which returns a logger with no handler attached and no level configured —
so the existing ``[TemporalStreamingModel] Initialized ... streaming_mode=...``
INFO log was silently dropped, making it impossible to verify at runtime
that a coalesced (or any) streaming mode was actually wired.
Switch to the SDK's ``make_logger`` helper (level=INFO, RichHandler in
local mode, StreamHandler otherwise) used everywhere else in the SDK.
The explicit logger name ``agentex.temporal.streaming`` is preserved so
any external logging configuration targeting that name keeps working.1 parent 9d80e0b commit e6f11c4
7 files changed
Lines changed: 782 additions & 18 deletions
File tree
- src/agentex/lib
- adk/_modules
- core
- services/adk
- temporal/plugins/openai_agents
- models
- tests
- tests/lib/core/services
- adk
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| 54 | + | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
| |||
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
63 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
64 | 70 | | |
65 | 71 | | |
66 | 72 | | |
| |||
76 | 82 | | |
77 | 83 | | |
78 | 84 | | |
| 85 | + | |
79 | 86 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
39 | 41 | | |
40 | 42 | | |
41 | 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 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
42 | 236 | | |
43 | 237 | | |
44 | 238 | | |
| |||
176 | 370 | | |
177 | 371 | | |
178 | 372 | | |
| 373 | + | |
179 | 374 | | |
180 | 375 | | |
181 | 376 | | |
| |||
184 | 379 | | |
185 | 380 | | |
186 | 381 | | |
| 382 | + | |
| 383 | + | |
187 | 384 | | |
188 | 385 | | |
189 | 386 | | |
| |||
208 | 405 | | |
209 | 406 | | |
210 | 407 | | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
211 | 412 | | |
212 | 413 | | |
213 | 414 | | |
| |||
218 | 419 | | |
219 | 420 | | |
220 | 421 | | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
221 | 428 | | |
222 | 429 | | |
223 | 430 | | |
| |||
227 | 434 | | |
228 | 435 | | |
229 | 436 | | |
230 | | - | |
231 | | - | |
| 437 | + | |
| 438 | + | |
232 | 439 | | |
233 | 440 | | |
234 | 441 | | |
| |||
248 | 455 | | |
249 | 456 | | |
250 | 457 | | |
251 | | - | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
252 | 472 | | |
253 | 473 | | |
254 | 474 | | |
| |||
258 | 478 | | |
259 | 479 | | |
260 | 480 | | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
261 | 486 | | |
262 | 487 | | |
263 | 488 | | |
| |||
288 | 513 | | |
289 | 514 | | |
290 | 515 | | |
| 516 | + | |
291 | 517 | | |
292 | 518 | | |
293 | 519 | | |
294 | 520 | | |
295 | 521 | | |
296 | 522 | | |
| 523 | + | |
297 | 524 | | |
298 | 525 | | |
299 | 526 | | |
| |||
0 commit comments