Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Long-chat gate: block on CPU / paint growth with chat length, judged on fresh-page rechecks (#17336)

Daniel Hanff80d4e7d57a
diff --git a/tests/studio/_area_matrix.py b/tests/studio/_area_matrix.py
index 044e73cd51f..d166a978110 100644
--- a/tests/studio/_area_matrix.py
+++ b/tests/studio/_area_matrix.py
@@ -63,15 +63,24 @@ LENGTH_RATIO = float(os.environ.get("LONG_THREAD_AREAS_LENGTH_RATIO", "2.0"))
COUNT_FLOOR = int(os.environ.get("LONG_THREAD_AREAS_COUNT_FLOOR", "3000"))
# Paint and main-thread CPU, long over short chat (the cheapest long reading over the median short one,
# each from the first reading and its rechecks):
-# fails over TIME_LENGTH_RATIO AND over TIME_LENGTH_FLOOR_MS more. A short reading below
+# fails over TIME_LENGTH_RATIO AND over TIME_LENGTH_FLOOR_MS more (TIME_LENGTH_CPU_FLOOR_MS for CPU). A short reading below
# TIME_BASE_FLOOR_MS counts as that, so a near-free area cannot make a large ratio.
TIME_LENGTH_RATIO = float(os.environ.get("LONG_THREAD_AREAS_TIME_LENGTH_RATIO", "2.5"))
-# Report-only for now: the readings, ratios and a "would fail" list are printed, the exit code is left
-# alone. Main reads up to the ceilings below on GitHub runners (no margin yet) and the cost behind them
-# is message-level actions (click, menu, tool / reasoning toggles, source card), being worked on
-# separately. Set True (or LONG_THREAD_AREAS_LENGTH_TIME_BLOCKING=1) to make them fail the gate.
-LENGTH_TIME_BLOCKING = os.environ.get("LONG_THREAD_AREAS_LENGTH_TIME_BLOCKING", "0") == "1"
+# Blocking. A first reading at LONG turns carries 100-250 ms of work spilled over from the areas run
+# before it (chat_row_menu 404-518 ms first, 178-268 on a fresh page), so a cell over the limit is
+# measured again on fresh pages at both lengths and judged on those. Main, 3 local gate runs: worst
+# 2.1x after rechecks, 2.4x on a first reading that stayed under the limit; a menu-open script walk
+# over every message (+~1 s at 200 turns) reads 2.8-3.0x after rechecks. Set
+# LONG_THREAD_AREAS_LENGTH_TIME_BLOCKING=0 to report only.
+LENGTH_TIME_BLOCKING = os.environ.get("LONG_THREAD_AREAS_LENGTH_TIME_BLOCKING", "1") == "1"
TIME_LENGTH_FLOOR_MS = float(os.environ.get("LONG_THREAD_AREAS_TIME_LENGTH_FLOOR_MS", "50"))
+# Main-thread CPU also has to grow by this much. On 2-core GitHub runners (10 gate runs, rechecked) message-level
+# actions and a few sidebar surfaces read 2.3-2.9x, up to +175 ms (sidebar_toggle 119 -> 294, tools_menu
+# 99 -> 269), so a ratio alone has no margin there; a feature adding real per-interaction work over the
+# thread (the synthetic menu-open walk: about +1 s) is far above this.
+TIME_LENGTH_CPU_FLOOR_MS = float(
+ os.environ.get("LONG_THREAD_AREAS_TIME_LENGTH_CPU_FLOOR_MS", "250")
+)
TIME_BASE_FLOOR_MS = float(os.environ.get("LONG_THREAD_AREAS_TIME_BASE_FLOOR_MS", "20"))
MAX_RATIO = float(os.environ.get("LONG_THREAD_AREAS_MAX_RATIO", "4.0"))
# Also judged on CPU growth with chat length: a streamed reply restyles only its own message, so its
@@ -100,30 +109,6 @@ KNOWN_DEFAULT = ",".join(
+ [f"{m}:thread_scroll=7" for m in _GLASS_MODES]
+ [f"{m}:settings_scroll=7" for m in _GLASS_MODES]
+ [f"{m}:type=6" for m in _GLASS_MODES]
- # Main-thread CPU length (renderer main thread, 40 -> 200 turns, judged value, 3 GitHub-runner gate
- # runs of main, default and Liquid 3D, probes that do not themselves scale with the chat). Presses
- # that land inside a message (click, its menu, tool / reasoning toggles, source card) still read
- # 2.5-2.9x; a few sidebar / composer surfaces touch 2.4-2.5x, inside the 0.4x run-to-run spread of
- # the 2.5x rule. Ceilings: 3.5x over 2.5, 3.0x at 2.4-2.5. Everything else keeps the plain 2.5x.
- # Fix work: per-interaction script cost that grows with the mounted thread (not yet assigned);
- # remove each entry as its area drops under 2.5x.
- + [f"cpu:{a}=3.5" for a in ("tool_toggle", "message_click", "message_menu")] # 2.0-2.9x
- + [
- f"cpu:{a}=3" # 1.6-2.5x
- for a in (
- "reasoning_toggle",
- "source_card",
- "chat_row_menu",
- "sidebar_sections",
- "run_settings",
- "thread_scroll",
- "permission_menu",
- "chat_options",
- "organize_chats",
- "sidebar_toggle",
- "api_monitor",
- )
- ]
)
KNOWN = os.environ.get("LONG_THREAD_AREAS_KNOWN", KNOWN_DEFAULT)
@@ -1854,7 +1839,9 @@ def judge(result: dict, known: str | None = None) -> list[dict]:
"base_value": sv,
"ratio": ratio,
"limit": tlimit,
- "would_fail": (over := ratio > tlimit and lv - sv > TIME_LENGTH_FLOOR_MS),
+ "would_fail": (
+ over := ratio > tlimit and lv - sv > length_floor_ms(metric)
+ ),
"fail": LENGTH_TIME_BLOCKING and over,
}
)
@@ -1977,9 +1964,15 @@ def thread_split(threads: dict[str, float]) -> str:
return ", ".join(f"{n or '?'} {v:.0f} ms" for n, v in top)
+def length_floor_ms(metric: str) -> float:
+ """How much a paint / main-thread length cell must grow, on top of the ratio, to fail."""
+ return TIME_LENGTH_CPU_FLOOR_MS if metric == "main_ms" else TIME_LENGTH_FLOOR_MS
+
+
def recheck_keys(verdicts: list[dict]) -> list[tuple[int, str, str]]:
- """(turns, mode, area) to measure again: every failing CPU cell and broken area, and a blocking paint
- / main-thread length failure at both lengths."""
+ """(turns, mode, area) to measure again: every failing CPU cell and broken area, and a paint /
+ main-thread length failure at both lengths (also while report-only: a single first reading at LONG
+ turns carries 100-250 ms of work spilled over from the areas run before it)."""
keys = {
(v["turns"], v["mode"], v["area"])
for v in verdicts
@@ -1994,7 +1987,7 @@ def recheck_keys(verdicts: list[dict]) -> list[tuple[int, str, str]]:
keys |= {
(t, v["mode"], v["area"])
for v in verdicts
- if v["fail"] and v["kind"] == "length" and v["metric"] in ("paint_ms", "main_ms")
+ if v.get("would_fail") and v["kind"] == "length" and v["metric"] in ("paint_ms", "main_ms")
for t in (SHORT, LONG)
}
return sorted(keys)
@@ -2051,7 +2044,7 @@ def run(
failures.append(
f"area {v['area']} ({v['mode']}) costs more as the chat grows: {what} {v['base_value']:,.0f} ms at "
f"{SHORT} turns -> {v['value']:,.0f} ms at {LONG} turns = {v['ratio']:.1f}x (limit {v['limit']:.1f}x over "
- f"{TIME_LENGTH_FLOOR_MS:.0f} ms; LONG_THREAD_AREAS_KNOWN {key}:{v['area']}=... or {key}@{v['mode']}:{v['area']}=...). "
+ f"{length_floor_ms(v['metric']):.0f} ms more; LONG_THREAD_AREAS_KNOWN {key}:{v['area']}=... or {key}@{v['mode']}:{v['area']}=...). "
"Element and layout counts can stay flat while this grows: the thread behind the surface is repainted, "
"or script walks every message"
)
diff --git a/tests/studio/test_area_matrix.py b/tests/studio/test_area_matrix.py
index 8adfaf51c8b..4d996735e2d 100644
--- a/tests/studio/test_area_matrix.py
+++ b/tests/studio/test_area_matrix.py
@@ -395,13 +395,15 @@ def test_an_appearance_failure_that_appears_after_a_recheck_is_measured_again(mo
# thread burst in every look (default 1,266 ms CPU, Liquid 3D 1,005). Another mode's failure got
# the default page measured again (154 ms); Liquid 3D was then judged 1,005 / 154 = 6.5x against
# 6x on its never-rechecked first reading. Its own recheck reads 1.6x.
- cell = lambda cpu: {
- "elements": 70,
- "layout_objects": 200,
- "cpu_ms": cpu,
- "main_ms": cpu / 2,
- "paint_ms": 15,
- }
+ cell = (
+ lambda cpu: {
+ "elements": 70,
+ "layout_objects": 200,
+ "cpu_ms": cpu,
+ "main_ms": 60, # flat, so the paint / CPU length rule (rechecked even report-only) stays out of it
+ "paint_ms": 15,
+ }
+ )
pages = {
(am.SHORT, "default"): _page({"type": cell(150)}),
(am.SHORT, "frosted"): _page({"type": cell(160)}),
@@ -636,12 +638,12 @@ def test_length_ratio_divides_by_the_median_short_reading(metric, length_blockin
if v["metric"] == metric and v["kind"] == "length"
]
assert [(v["value"], v["base_value"], v["fail"]) for v in rows] == [(76, 34, False)]
- # A real growth still fails over the same median: 34 -> 160 ms is 4.7x and 126 ms more.
+ # A real growth still fails over the same median: 34 -> 300 ms is 8.8x and 266 ms more.
long_cell = {
"elements": 10,
"layout_objects": 0,
- metric: 170,
- "again": [{"status": "OK", metric: 160}],
+ metric: 310,
+ "again": [{"status": "OK", metric: 300}],
}
pages[(am.LONG, "default")] = _page({"a": long_cell})
rows = [
@@ -649,12 +651,47 @@ def test_length_ratio_divides_by_the_median_short_reading(metric, length_blockin
for v in am.judge(_result(pages), known = "")
if v["metric"] == metric and v["kind"] == "length"
]
- assert [(v["value"], v["base_value"], v["fail"]) for v in rows] == [(160, 34, True)]
+ assert [(v["value"], v["base_value"], v["fail"]) for v in rows] == [(300, 34, True)]
+
+
+def test_cpu_length_growth_needs_the_cpu_floor_as_well_as_the_ratio(length_blocking):
+ # GitHub runner, Liquid 3D tool_toggle after rechecks: 51 -> 132 ms is 2.6x but only 81 ms more, inside
+ # the 2-core runner spread for message-level actions; paint keeps the 50 ms floor.
+ def rows(short, long):
+ cells = lambda v: {
+ "elements": 10,
+ "layout_objects": 0,
+ "main_ms": v,
+ "paint_ms": v,
+ "again": [{"status": "OK", "main_ms": v, "paint_ms": v}],
+ }
+ pages = {
+ (am.SHORT, "default"): _page({"a": cells(short)}),
+ (am.LONG, "default"): _page({"a": cells(long)}),
+ }
+ return {
+ v["metric"]: v["fail"]
+ for v in am.judge(_result(pages), known = "")
+ if v["kind"] == "length" and v["metric"] in ("main_ms", "paint_ms")
+ }
+
+ assert am.length_floor_ms("main_ms") == am.TIME_LENGTH_CPU_FLOOR_MS == 250
+ assert am.length_floor_ms("paint_ms") == am.TIME_LENGTH_FLOOR_MS == 50
+ assert rows(51, 132) == {"main_ms": False, "paint_ms": True}
+ # The synthetic per-message walk on a menu open: about +1 s at 200 turns.
+ assert rows(547, 1535) == {"main_ms": True, "paint_ms": True}
+
+
+def test_length_time_checks_block_by_default():
+ # Blocking since the length cells are judged on fresh-page rechecks (no spilled-over first reading).
+ assert am.LENGTH_TIME_BLOCKING is True
+ assert not [
+ k for k in am.known_limits(am.KNOWN_DEFAULT) if k[0].split("@")[0] in ("cpu", "paint")
+ ], "paint / CPU length ceilings were for single first readings; rechecks replace them"
-def test_length_time_checks_report_but_do_not_fail_by_default():
- # Report-only until message-level actions stop growing with the chat (LENGTH_TIME_BLOCKING).
- assert am.LENGTH_TIME_BLOCKING is False
+def test_length_time_checks_can_report_only(monkeypatch):
+ monkeypatch.setattr(am, "LENGTH_TIME_BLOCKING", False)
pages = {
(am.SHORT, "default"): _page(
{
@@ -686,7 +723,9 @@ def test_length_time_checks_report_but_do_not_fail_by_default():
("paint_ms", True, False),
("main_ms", True, False),
]
- assert not am.recheck_keys(verdicts), "a report-only reading costs no recheck time"
+ # Even report-only, the reading is measured again at both lengths: a single first reading at LONG
+ # turns carries work spilled over from the areas before it.
+ assert am.recheck_keys(verdicts) == [(am.SHORT, "default", "a"), (am.LONG, "default", "a")]
text = "\n".join(am.table(result, verdicts))
assert "would fail (report-only, not failing the gate): default/a paint 15->104 ms" in text
@@ -787,3 +826,60 @@ def test_stream_wait_is_cheap_while_the_reply_streams():
body = am.STREAMED.split("if (!window.__gateStreamDone) return false;")
assert len(body) == 2 and "querySelector" not in body[0]
assert "__gateStreamDone = true" in am.STREAM_SHIM
+
+
+def _length_run(monkeypatch, first_long_main, again_long_main):
+ """am.run on one area whose main-thread time is 130 ms at SHORT turns; the LONG reading is
+ first_long_main, then again_long_main on each fresh-page recheck."""
+ cell = lambda main: {
+ "elements": 10,
+ "layout_objects": 0,
+ "cpu_ms": 200,
+ "main_ms": main,
+ "paint_ms": 5,
+ }
+ pages = {
+ (am.SHORT, "default"): _page({"chat_row_menu": cell(130)}),
+ (am.LONG, "default"): _page({"chat_row_menu": cell(first_long_main)}),
+ }
+ fake = {
+ "pages": pages,
+ "modes": {m["name"]: m for m in am.app_modes()},
+ "areas": ["chat_row_menu"],
+ "seconds": 0.0,
+ }
+ monkeypatch.setattr(am, "measure", lambda *a, **k: fake)
+ calls = []
+
+ def remeasure(result, items, log):
+ calls.append(sorted(items))
+ for turns, mode, area in items:
+ reading = {"status": "OK", **cell(130 if turns == am.SHORT else again_long_main)}
+ result["pages"][(turns, mode)]["cells"][area].setdefault("again", []).extend(
+ [reading] * am.RECHECKS
+ )
+
+ monkeypatch.setattr(am, "remeasure", remeasure)
+ _, failures, _, _ = am.run(Path("unused"), [], log = lambda *a, **k: None)
+ return calls, [f for f in failures if "costs more as the chat grows" in f]
+
+
+def test_a_spilled_over_first_long_reading_clears_on_recheck(monkeypatch, length_blocking):
+ # Gate order, 200 turns: chat_row_menu read 404-518 ms on its first reading and 178-268 ms on a
+ # fresh page (130 ms at 40 turns); the work belonged to the areas run before it.
+ calls, failures = _length_run(monkeypatch, first_long_main = 470, again_long_main = 220)
+ assert calls == [
+ [(am.SHORT, "default", "chat_row_menu"), (am.LONG, "default", "chat_row_menu")]
+ ]
+ assert failures == []
+
+
+def test_length_growth_that_survives_rechecks_fails(monkeypatch, length_blocking):
+ calls, failures = _length_run(monkeypatch, first_long_main = 470, again_long_main = 450)
+ assert len(calls) == 1
+ assert len(failures) == 1 and "area chat_row_menu (default)" in failures[0]
+
+
+def test_an_area_under_the_limit_costs_no_recheck(monkeypatch, length_blocking):
+ calls, failures = _length_run(monkeypatch, first_long_main = 250, again_long_main = 250)
+ assert calls == [] and failures == []