Skip to content

Commit a3eb212

Browse files
committed
Fix chunky response streaming in the WebUI
Reduce state-push coalescing from 100 ms to 25 ms so transport batching does not combine successive response updates into large visible bursts. The existing per-connection timer and in-flight push guards still bound work and coalesce dirty state. Skip inner Scroller measurements for root response bodies, which grow with the chat viewport. The existing chat scroller owns following, and the collapse handler still resets collapsed content. Other message bodies retain their scrolling behavior. Update the owning docs and regressions. Validation: - 42 focused streaming, state-sync, snapshot and WebUI tests passed. - 31 WebSocket security, CSRF and handler tests passed. - Live DeepSeek and Opus checks showed roughly halved median visible update intervals; response scrolling and collapse/expand were checked. Live browser checks used headless Chromium. Long Opus responses still show occasional dropped frames as more frequent updates increase render work.
1 parent 785ef77 commit a3eb212

6 files changed

Lines changed: 68 additions & 9 deletions

File tree

‎helpers/state_monitor.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ConnectionProjection:
4242
class StateMonitor:
4343
"""Per-sid dirty tracking with debounced snapshot push scheduling."""
4444

45-
def __init__(self, debounce_seconds: float = 0.1) -> None:
45+
def __init__(self, debounce_seconds: float = 0.025) -> None:
4646
self.debounce_seconds = float(debounce_seconds)
4747
self._lock = threading.RLock()
4848
self._projections: dict[ConnectionIdentity, ConnectionProjection] = {}
@@ -223,7 +223,7 @@ def _schedule_debounce_on_loop(self, identity: ConnectionIdentity) -> None:
223223

224224
# Throttled coalescing: schedule at most one push per debounce window.
225225
# Do not postpone the scheduled push on subsequent dirties; this keeps
226-
# streaming updates smooth while still capping to <= 1 push / 100ms / sid.
226+
# streaming updates live while bounding each connection's push rate.
227227
existing = self._debounce_handles.get(identity)
228228
if existing is not None and not existing.cancelled():
229229
return

‎helpers/state_monitor.py.dox.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
## Key Concepts
3636

3737
- Important called helpers/classes observed in the source: `threading.RLock`, `field`, `ws_debug`, `_ws_debug_enabled`, `context_id.strip`, `loop.call_soon_threadsafe`, `self._schedule_debounce_on_loop`, `asyncio.get_running_loop`, `asyncio.current_task`, `loop.is_closed`, `self._debounce_handles.pop`, `self._push_tasks.pop`, `self._projections.pop`, `self.mark_dirty`, `self._mark_dirty_on_loop`, `runtime.is_development`, `loop.call_later`, `StateMonitor`, `ConnectionProjection`, `handle.cancel`.
38-
- The default monitor coalesces dirty waves into at most one state push per 100 ms per connection without postponing an already scheduled push.
38+
- The default monitor coalesces dirty waves into at most one state push per 25 ms per connection without postponing an already scheduled push. Keep this window below the agent's response-stream cadence so transport batching does not turn live text into large bursts.
3939
- Collection-dirty versions are coalesced independently from ordinary stream dirties. Only clients that explicitly negotiate `collections_delta` may receive `contexts: null` and `tasks: null`; legacy clients remain on full snapshots.
4040
- Keep request/response, tool, or helper semantics documented here at the same time as source changes.
4141

‎tests/test_state_monitor.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
sys.path.insert(0, str(PROJECT_ROOT))
99

1010

11-
def test_state_monitor_defaults_to_ten_pushes_per_second() -> None:
11+
def test_state_monitor_batching_stays_below_response_stream_cadence() -> None:
1212
from helpers.state_monitor import StateMonitor
1313

14-
assert StateMonitor().debounce_seconds == 0.1
14+
assert StateMonitor().debounce_seconds == 0.025
1515

1616

1717
@pytest.mark.asyncio

‎tests/test_webui_message_window.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,59 @@
2020
MESSAGE_COLLAPSE_JS = PROJECT_ROOT / "webui" / "js" / "message-collapse.js"
2121

2222

23+
def test_response_updates_leave_scrolling_to_the_chat_viewport():
24+
if not shutil.which("node"):
25+
pytest.skip("Node.js is required to execute the message-render regression.")
26+
27+
source = (PROJECT_ROOT / "webui" / "js" / "messages.js").read_text()
28+
draw_message = source[
29+
source.index("export function _drawMessage("):
30+
source.index("\nfunction renderStandaloneMessageContent(")
31+
].replace("export function", "function", 1)
32+
script = f"""
33+
import assert from "node:assert/strict";
34+
let measurements = 0, scrolls = 0, renders = 0;
35+
class Scroller {{
36+
constructor(body) {{ measurements++; void body.scrollHeight; }}
37+
reApplyScroll() {{ scrolls++; }}
38+
}}
39+
const renderStandaloneMessageContent = () => renders++;
40+
const isMassRender = () => false;
41+
const _windowedRender = false;
42+
{draw_message}
43+
44+
function update(mainClass, expanded) {{
45+
const body = {{ dataset: {{}}, scrollTop: 8,
46+
get scrollHeight() {{
47+
assert.notEqual(mainClass, "message-agent-response",
48+
"response updates must not force layout for an inner scroll viewport");
49+
return 100;
50+
}},
51+
}};
52+
const message = {{
53+
classList: {{ contains: name => name === "expanded" && expanded, remove() {{}} }},
54+
querySelector: selector => selector === ".message-body" ? body : null,
55+
}};
56+
_drawMessage({{
57+
messageContainer: {{ querySelector: () => message }},
58+
mainClass, content: "streaming content", markdown: true,
59+
}});
60+
return body;
61+
}}
62+
63+
update("message-agent-response", true);
64+
update("message-agent-response", false);
65+
assert.equal(measurements, 0);
66+
assert.equal(scrolls, 0);
67+
update("message-default", true);
68+
assert.equal(scrolls, 1, "other standalone messages must retain auto-scroll");
69+
assert.equal(update("message-default", false).scrollTop, 0);
70+
assert.equal(measurements, 2);
71+
assert.equal(renders, 4, "scroll changes must not skip content rendering");
72+
"""
73+
subprocess.run(["node", "--input-type=module", "-e", script], check=True, text=True)
74+
75+
2376
def test_text_editor_and_code_execution_keep_memory_search_in_the_process_group():
2477
if not shutil.which("node"):
2578
pytest.skip("Node.js is required to execute the message-window regression.")

‎webui/js/AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- `message-window.js` owns the bounded, tail-first raw-log window used by message rendering.
2727
- `loading-indicators.js` owns reusable DOM factories for shared loading visuals.
2828
- `scroller.js` owns bottom-following snapshots and cancellation of pending scroll effects.
29+
- Root response bodies grow with the chat viewport and must not create an inner `Scroller`; the message batch owns chat-level following. Other standalone message bodies retain their existing scrolling behavior.
2930
- Other modules own focused UI utilities such as modals, messages, safe markdown, shortcuts, TTS/STT, surfaces, and initialization.
3031

3132
## Local Contracts

‎webui/js/messages.js‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,10 @@ export function _drawMessage({
17351735

17361736
// reapply scroll position or autoscroll
17371737
bodyDiv.dataset.scrollStabilization = "1";
1738-
const scroller = new Scroller(bodyDiv, { smooth: !isMassRender() });
1738+
// Responses grow in the chat viewport; their body has no independent scroll area.
1739+
const scroller = mainClass === "message-agent-response"
1740+
? null
1741+
: new Scroller(bodyDiv, { smooth: !isMassRender() });
17391742

17401743
const contentText = String(content ?? "");
17411744
const lazyContent =
@@ -1777,9 +1780,11 @@ export function _drawMessage({
17771780
}
17781781

17791782
// reapply scroll position or reset for collapsed
1780-
messageDiv.classList.contains("expanded")
1781-
? scroller.reApplyScroll()
1782-
: (bodyDiv.scrollTop = 0);
1783+
if (scroller) {
1784+
messageDiv.classList.contains("expanded")
1785+
? scroller.reApplyScroll()
1786+
: (bodyDiv.scrollTop = 0);
1787+
}
17831788

17841789
return messageDiv;
17851790
}

0 commit comments

Comments
 (0)