From 7a8fb41c8d3ad1211207c0f6e3bcbcd2338f6000 Mon Sep 17 00:00:00 2001 From: AgentSparko <124781906+AgentSparko@users.noreply.github.com> Date: Sat, 21 Nov 2026 11:30:43 +0200 Subject: [PATCH 1/2] The home wait is cost-aware: a conversation too big to re-read waits at home instead of hopping SIDE_HOME_WAIT_S (10 s) was sized for SRV-528's 45k conversation (re-read measured 80.9 s). On 2026-11-21 a 133,075-token conversation hopped on that same budget: the re-read of 133k rows on a slot holding nothing of it never finished inside the client's 900 s (seven abandoned rows at 900 s intervals), each retry hopped again, and every hop charged another conversation's rows to be re-read too. The wait at home costs only the first token; a hop costs the whole prompt, and only a request that stayed on its slot can resume the rows prefilled before a client left (_prefill_watch). _home_wait_budget_s scales the home budget with the estimated re-read cost (rows / HOP_READ_TOK_S, clamped to [SIDE_HOME_WAIT_S, HOME_WAIT_MAX_S]); knobs registered, the queued line and the [parallel] numbers carry the budget. tests/test_home_wait_cost.py 5/5; test_slot_affinity.py 12/12 unchanged (its hop replays a 2k prompt, which keeps the 10 s floor). --- engine/settings.py | 3 ++ server/app.py | 31 ++++++++++++++-- tests/test_home_wait_cost.py | 71 ++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 tests/test_home_wait_cost.py diff --git a/engine/settings.py b/engine/settings.py index 79e143f65..ec156d1d6 100644 --- a/engine/settings.py +++ b/engine/settings.py @@ -244,6 +244,9 @@ 'SIDE_MIN_ROOM': ('16384', 'server/app.py'), 'SIDE_NEW_FIRST': ('on', 'server/app.py'), 'SIDE_HOME_WAIT_S': ('10', 'server/app.py'), + # the home wait is cost-aware: the hop's re-read is estimated at this loaded rate + 'HOP_READ_TOK_S': ('500', 'server/app.py'), + 'HOME_WAIT_MAX_S': ('600', 'server/app.py'), 'SIDE_RESIDENT_GB': ('0.5', 'server/app.py'), 'SIDE_STASH_GB': ('1.0', 'server/app.py'), # SPD-257: the end anchor's buffers are each resident prefix's own from the load (off: asked of the policy) diff --git a/server/app.py b/server/app.py index 6c49dc67e..9a05c0a31 100644 --- a/server/app.py +++ b/server/app.py @@ -1112,6 +1112,28 @@ def _served_share(share: int, notes) -> int: #: (`_carry_from_side`) or read again SIDE_HOME_WAIT_S = float(_S.get("SIDE_HOME_WAIT_S") or 10.0) +#: The home wait is cost-aware (2026-11-21 02:58-04:28, engine-20261121-013742.log, the ledger's +#: seven `abandoned` rows at 900 s each): the fixed 10 s budget was written for SRV-528's 45k +#: conversation, whose re-read measured 80.9 s — hopping then cost about what a longer wait +#: would. A 133,075-token conversation hopped on the same 10 s budget and re-read its rows on a +#: slot holding nothing of it: under the load of the other resident streams the re-read never +#: finished inside the client's 900 s, each retry hopped again, and every hop charged another +#: conversation's rows to be re-read too. Waiting at home costs only the first token; a hop +#: costs the whole prompt. So the home budget scales with the estimated re-read cost at +#: HOP_READ_TOK_S (a conservative loaded rate), clamped to [SIDE_HOME_WAIT_S, HOME_WAIT_MAX_S]: +#: a small chat still hops after 10 s, a 133k conversation waits ~4.5 min at home first — and +#: rows prefilled before a client left stay good for its retry (`_prefill_watch`), which only a +#: request that stayed on its slot can resume. +HOP_READ_TOK_S = float(_S.get("HOP_READ_TOK_S") or 500.0) +HOME_WAIT_MAX_S = float(_S.get("HOME_WAIT_MAX_S") or 600.0) + + +def _home_wait_budget_s(need) -> float: + """How long a turn waits for its busy home slot before hopping: the fixed floor, or the + estimated cost of re-reading `need` rows on a slot that holds none of them, whichever is + more, capped.""" + return min(HOME_WAIT_MAX_S, max(SIDE_HOME_WAIT_S, (need or 0) / HOP_READ_TOK_S)) + #: FX17: a new chat of at most SIDE_NEW_SHORT rendered tokens (plus the margins) beside a long #: resident conversation waits SIDE_NEW_WAIT_S for a busy side slot before it takes slot 0, where #: it would send the long conversation to the NVMe (restorable, about 3 s, but its next turn @@ -1823,7 +1845,8 @@ def _queue_why(pool, why: dict, side_need, waited: float, me: dict) -> str: return f"too long for a side slot ({longest:,} tokens); waits for slot 0" return _wait_reason(me)[0] if why.get("home") is not None and why.get("home_wait") is None: - return f"waits up to {SIDE_HOME_WAIT_S:g} s for slot {why['home']}, which holds its conversation" + return (f"waits up to {float(why.get('home_budget_s') or SIDE_HOME_WAIT_S):g} s " + f"for slot {why['home']}, which holds its conversation") if why.get("hop_from") is not None: return (f"moved from slot {why['hop_from']} (busy {why.get('home_wait')} s): re-reading its " f"conversation on the next free slot") @@ -1846,7 +1869,8 @@ def _par_nums(why: dict) -> str: request with images their count and the expanded rows (SRV-629).""" nums = " ".join(f"{k}={why[k]}" for k in ("view", "view_common", "valid", "res_common", "conv", "need", "guest", "park", "slot0_wait_s", - "home", "home_share", "home_wait", "hop_from", + "home", "home_share", "home_wait", "home_budget_s", + "hop_from", "prefer_side", "prefer_side_end", "slot0_stale", "new_wait", "spill", "spill_head", "spill_home", "slot0_wait") if k in why) @@ -10786,6 +10810,7 @@ def mem_wait(short: float, place: int = 1) -> None: new_wait = False par_why["new_wait"] = round(time.monotonic() - t_wait, 1) if home is not None: + par_why["home_budget_s"] = round(_home_wait_budget_s(side_need), 1) side = par.take_slot(int(home), side_need, owner=cid) got = side is not None if got: @@ -10793,7 +10818,7 @@ def mem_wait(short: float, place: int = 1) -> None: with par._lk: par.stats["home_side"] = par.stats.get("home_side", 0) + 1 break - if time.monotonic() - t_wait < SIDE_HOME_WAIT_S: + if time.monotonic() - t_wait < _home_wait_budget_s(side_need): time.sleep(0.05) if self._client_gone(): left = True diff --git a/tests/test_home_wait_cost.py b/tests/test_home_wait_cost.py new file mode 100644 index 000000000..3b6807225 --- /dev/null +++ b/tests/test_home_wait_cost.py @@ -0,0 +1,71 @@ +"""The home wait is cost-aware: a conversation too big to re-read waits at home, not hops. + +THE INCIDENT (2026-11-21 02:58-04:28, flashnext 3.05 on the 121 GiB box, +engine-20261121-013742.log, the ledger's seven `abandoned` rows at 900 s intervals, +completion_tokens None). An agent conversation of 133,075 tokens lived on side slot 5. Its next +turn found the home slot busy for more than the fixed SIDE_HOME_WAIT_S (10 s), hopped, and +re-read 133k rows on a slot holding nothing of it: under the load of the other resident streams +that re-read never finished inside the client's 900 s. The retry hopped again (the first retry +found its old home busy too — its own predecessor's rows were still there), and every hop +charged another conversation's rows to be re-read as well. The spiral held until the session +gave up. Waiting at home costs only the first token; a hop costs the whole prompt — and rows +prefilled before a client left stay good for its retry (`_prefill_watch`), which only a request +that stayed on its own slot can resume. + +THE FIX (server/app.py): `_home_wait_budget_s` scales the home budget with the estimated +re-read cost — prompt rows / HOP_READ_TOK_S, clamped to [SIDE_HOME_WAIT_S, HOME_WAIT_MAX_S]. +A small chat keeps the old 10 s floor; SRV-528's 45k conversation (re-read measured 80.9 s on +2026-10-17) now waits long enough to cover its own measured re-read; a 133k conversation waits +minutes at home before a hop is even considered. + +Run: python tests/test_home_wait_cost.py +""" + +from __future__ import annotations + +import os +import sys + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from server import app as A # noqa: E402 + + +def test_small_chat_keeps_the_fixed_floor(): + assert A._home_wait_budget_s(500) == A.SIDE_HOME_WAIT_S + assert A._home_wait_budget_s(None) == A.SIDE_HOME_WAIT_S + return f"floor {A.SIDE_HOME_WAIT_S:g} s unchanged" + + +def test_srv_528_size_covers_its_measured_reread(): + # 45,778 rows were re-read in 80.9 s on 2026-10-17; the budget must cover that + b = A._home_wait_budget_s(45_778) + assert b > 80.9, b + return f"45k waits {b:.0f} s (measured re-read was 80.9 s)" + + +def test_133k_waits_minutes_not_seconds(): + b = A._home_wait_budget_s(133_075) + assert b > 200, b + return f"133k waits {b:.0f} s at home instead of a 900 s re-read that never finished" + + +def test_the_budget_is_capped(): + assert A._home_wait_budget_s(10_000_000) == A.HOME_WAIT_MAX_S + return f"cap {A.HOME_WAIT_MAX_S:g} s" + + +def test_the_budget_grows_with_the_prompt(): + small, big = A._home_wait_budget_s(2_000), A._home_wait_budget_s(100_000) + assert big > small * 10, (small, big) + return f"2k -> {small:g} s, 100k -> {big:g} s" + + +if __name__ == "__main__": + passed = 0 + for name, fn in list(globals().items()): + if name.startswith("test_") and callable(fn): + print(f" {name:72s} ok {fn() or ''}", flush=True) + passed += 1 + print(f"{passed} passed") \ No newline at end of file From eccc05e19550a4361e2ad6208befc09632bbbad4 Mon Sep 17 00:00:00 2001 From: AgentSparko <124781906+AgentSparko@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:09:52 +0200 Subject: [PATCH 2/2] docs: regenerate variables reference for the home-wait knobs --- docs/reference/variables.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/reference/variables.md b/docs/reference/variables.md index 179c065d0..21312e0ba 100644 --- a/docs/reference/variables.md +++ b/docs/reference/variables.md @@ -359,6 +359,8 @@ TandemEngine reads four families of settings. Engine switches are `QWEN38_*` var | `QWEN38_GRAPH_SHARE_FROM` | `2` | `engine/verify_graph.py` | | `QWEN38_GRAPH_SHARE_OUT` | `0` | `engine/verify_graph.py` | | `QWEN38_HEAD_GEMM` | `none` | `tools/head_gemv.py` | +| `QWEN38_HOME_WAIT_MAX_S` | `600` | `server/app.py` | +| `QWEN38_HOP_READ_TOK_S` | `500` | `server/app.py` | | `QWEN38_HOST_ASYNC` | `0` | `engine/model.py` | | `QWEN38_HOST_WALK` | `0` | `1` · `engine/drafters/dflash2.py` | | `QWEN38_JOINT_ATTN_GROUP` | `1` | `engine/models/qwen3_5/attn_group.py` |