Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
3 changes: 3 additions & 0 deletions engine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 28 additions & 3 deletions server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -10786,14 +10810,15 @@ 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:
side.no_carry = bool(me["own"]) or lane_kind is not 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
Expand Down
71 changes: 71 additions & 0 deletions tests/test_home_wait_cost.py
Original file line number Diff line number Diff line change
@@ -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")
Loading