From ac38e9903cc882fb18213671e695b2c3ab71db55 Mon Sep 17 00:00:00 2001 From: dp-web4 Date: Thu, 19 Nov 2026 14:56:17 -0800 Subject: [PATCH] seed fit: the part-wise fitters budget at the being's measured ratio (#582) pub-claude, 2026-11-19: after #773/#774 the step-1 preflight divides by the being's measured chars-per-token, but fit_state and fit_to_window still budgeted at the fixed CPT 2.9. On pub-being that was ~37.3k chars against ~51.5k at its measured 3.999, so 112 of 112 beats stepped conversations to the sparsest rung ("STILL does not fit") on a seed the server counted ~4k tokens under the window. mcnugget-being's beats carry the same line. The beat now computes the calibration before the part-wise fitters and passes them fit_cpt(): the measured ratio once it is `measured`, else CPT (a declared prior, a provisional ratio or a probe keeps CPT). Frames are charged at the same ratio. fit_seed_to_preflight still measures the composed seed with the loop's own count afterwards and trims a seed the loop would refuse. Recorded as preflight_cpt.fit_cpt. Co-Authored-By: Claude Opus 5.5 --- sage/gateway/heartbeat.py | 80 ++++++++++++------- .../gateway/tests/test_fit_at_measured_cpt.py | 71 ++++++++++++++++ 2 files changed, 124 insertions(+), 27 deletions(-) create mode 100644 sage/gateway/tests/test_fit_at_measured_cpt.py diff --git a/sage/gateway/heartbeat.py b/sage/gateway/heartbeat.py index 7c3f3b30f..49e332389 100644 --- a/sage/gateway/heartbeat.py +++ b/sage/gateway/heartbeat.py @@ -1442,10 +1442,30 @@ def note_resolutions(esc_dir: Path, decisions, stamp: str, seen_by: str, decided ANSWER_RESERVE_CAP = 6144 -def window_budget_chars(num_ctx: int, num_predict: int, slack: int = 512) -> int: - """How many prompt chars fit beside a p99 answer. One producer for both fitters.""" +def window_budget_chars(num_ctx: int, num_predict: int, slack: int = 512, cpt: float = None) -> int: + """How many prompt chars fit beside a p99 answer. One producer for both fitters. + + `cpt` is the chars-per-token to budget with: the being's own measured ratio when the beat has one + (fit_cpt), else CPT. Measured on pub-being (SAGE #582, 2026-11-19): at the fixed 2.9 the budget was + ~37.3k chars against ~51.5k at its measured 3.999, so 112 of 112 beats showed conversations at the + sparsest rung while the server counted the seed ~4k tokens under the window.""" reserve = min(num_predict, ANSWER_RESERVE_CAP) - return int(max(0, (num_ctx - reserve - slack)) * CPT) + return int(max(0, (num_ctx - reserve - slack)) * (cpt or CPT)) + + +def fit_cpt(cal) -> float: + """The ratio the part-wise fitters budget with this beat: the measured step-1 ratio only once it is + `measured` (MEASURED_CPT_MIN_SAMPLES of this being's own counts, margin applied); a declared prior, a + provisional ratio or a probe keeps CPT. Whatever the fitters admit, fit_seed_to_preflight still measures + the composed seed with the loop's own count and trims a seed the loop would refuse.""" + try: + if cal.get("source") == "measured": + v = float(cal.get("used")) + if 1.5 <= v <= 8.0: + return v + except Exception: + pass + return CPT # What the beat shows of the conversations, from full to sparse: (turns per conversation, @@ -1519,7 +1539,7 @@ def _schema_chars_fallback(offered) -> int: return max(_SCHEMA_CHARS_FLOOR, n * _SCHEMA_CHARS_PER_VERB) -def fit_state(build, *, num_ctx, num_predict, other_chars: int, slack: int = 512): +def fit_state(build, *, num_ctx, num_predict, other_chars: int, slack: int = 512, cpt: float = None): """`build(per_conv, turn_chars) -> state text`. Returns (text, rung, intervention). Steps down CONV_LADDER until other_chars + len(text) fits the window budget. The @@ -1529,7 +1549,7 @@ def fit_state(build, *, num_ctx, num_predict, other_chars: int, slack: int = 512 the beat then runs overcommitted and says so (config.context_overcommitted).""" if not isinstance(num_ctx, int) or not isinstance(num_predict, int): return build(*CONV_LADDER[0]), CONV_LADDER[0], None - budget = window_budget_chars(num_ctx, num_predict, slack) + budget = window_budget_chars(num_ctx, num_predict, slack, cpt) first = None for i, rung in enumerate(CONV_LADDER): text = build(*rung) @@ -1550,7 +1570,8 @@ def fit_state(build, *, num_ctx, num_predict, other_chars: int, slack: int = 512 } -def fit_to_window(*, num_ctx, num_predict, fixed_chars: int, blocks: dict, slack: int = 512): +def fit_to_window(*, num_ctx, num_predict, fixed_chars: int, blocks: dict, slack: int = 512, + cpt: float = None): """Trim the seat-supplied blocks until prompt + num_predict fits inside num_ctx. WHY THIS EXISTS. A generate needs prompt + num_predict to fit in the window; when it @@ -1587,7 +1608,7 @@ def fit_to_window(*, num_ctx, num_predict, fixed_chars: int, blocks: dict, slack # leaving the digest something to say; reserving the full num_predict would floor the # digest every beat to buy room the model has never used. reserve = min(num_predict, ANSWER_RESERVE_CAP) - budget_chars = window_budget_chars(num_ctx, num_predict, slack) + budget_chars = window_budget_chars(num_ctx, num_predict, slack, cpt) order = ("digest", "recall") floors = FIT_FLOORS out, interventions = dict(blocks), [] @@ -5497,11 +5518,33 @@ def _build_state(per_conv, turn_chars): # token cost is converted and charged here. Measured 2,042 tokens for two frames, about a # third of the working room at a 24k window — un-budgeted it pushes the beat over the wall # and the conversation block takes the blame. - _frame_chars = sum(int(FRAME_TOKENS * CPT) for _ in _frame_b64s) + from sage.gateway.being_tool_loop import uncounted_chars, preflight_tokens, preflight_cpt + from sage.gateway.being_tool_loop import _convo_chars as btl_convo_chars, WINDOW_SEND_MARGIN as _WSM + # the step-1 ratio, measured on this being's own recent beats when there are enough (#582 part 2) + try: + llm.preflight_cpt_measured = None # the prior is the declared/default ratio, never last beat's + except Exception: + pass + _cpt_prior = preflight_cpt(llm) + _cpt_cal = measured_preflight_cpt(instance, args.model, prior=_cpt_prior) + try: + # set every beat, None included: a process that outlives a model switch must not keep the old ratio + llm.preflight_cpt_measured = _cpt_cal.get("value") + except Exception: + _cpt_cal["value"] = None + _cpt_cal["prior"] = _cpt_prior + _cpt_cal["used"] = preflight_cpt(llm) + _cpt_cal["source"] = "measured" if _cpt_cal.get("value") else "declared_or_default" + # THE PART-WISE FITTERS BUDGET AT THE SAME MEASURED RATIO (SAGE #582, pub-claude 2026-11-19): at the + # fixed CPT they stepped every beat to the sparsest conversation rung on a seed the server counted + # well under the window. Recorded as preflight_cpt.fit_cpt. + _fit_cpt = fit_cpt(_cpt_cal) + _cpt_cal["fit_cpt"] = _fit_cpt + _frame_chars = sum(int(FRAME_TOKENS * _fit_cpt) for _ in _frame_b64s) _other = (len(posture()) + len(inbox) + _schema_chars + 1200 + _frame_chars + 1200 + 400 + LOOP_GROWTH_CHARS) state_block, conv_rung, conv_intervention = fit_state( - _build_state, num_ctx=_num_ctx, num_predict=_num_predict, other_chars=_other) + _build_state, num_ctx=_num_ctx, num_predict=_num_predict, other_chars=_other, cpt=_fit_cpt) # entrustment_mode "daily": the showing is recorded from the state that is SENT, never from a # render (_build_state composes with mark_conversations=False, so nothing inside it records) try: @@ -5514,7 +5557,7 @@ def _build_state(per_conv, turn_chars): _fixed = len(posture()) + len(state_block) + len(inbox) + _schema_chars + 1200 _blocks, _fit_iv = fit_to_window(num_ctx=_num_ctx, num_predict=_num_predict, fixed_chars=_fixed, - blocks={"digest": digest, "recall": recall}) + blocks={"digest": digest, "recall": recall}, cpt=_fit_cpt) digest, recall = _blocks["digest"], _blocks["recall"] _harness = harness_revision(workspace) @@ -5557,23 +5600,6 @@ def _compose_seed(state_text, recall_text, digest_text): # THE SEED IS FITTED AGAIN, WHOLE, ON THE TOOL LOOP'S OWN STEP-1 COUNT (SAGE #582): the fitters # above budget from parts; this measures what will be sent and trims only if the loop would # refuse it. A beat whose seed the loop accepts is unchanged. - from sage.gateway.being_tool_loop import uncounted_chars, preflight_tokens, preflight_cpt - from sage.gateway.being_tool_loop import _convo_chars as btl_convo_chars, WINDOW_SEND_MARGIN as _WSM - # the step-1 ratio, measured on this being's own recent beats when there are enough (#582 part 2) - try: - llm.preflight_cpt_measured = None # the prior is the declared/default ratio, never last beat's - except Exception: - pass - _cpt_prior = preflight_cpt(llm) - _cpt_cal = measured_preflight_cpt(instance, args.model, prior=_cpt_prior) - try: - # set every beat, None included: a process that outlives a model switch must not keep the old ratio - llm.preflight_cpt_measured = _cpt_cal.get("value") - except Exception: - _cpt_cal["value"] = None - _cpt_cal["prior"] = _cpt_prior - _cpt_cal["used"] = preflight_cpt(llm) - _cpt_cal["source"] = "measured" if _cpt_cal.get("value") else "declared_or_default" _seed_uncounted = uncounted_chars(_explore_specs, explore_turn_mode(instance)) _states = {conv_rung: state_block} diff --git a/sage/gateway/tests/test_fit_at_measured_cpt.py b/sage/gateway/tests/test_fit_at_measured_cpt.py new file mode 100644 index 000000000..9e64e8fde --- /dev/null +++ b/sage/gateway/tests/test_fit_at_measured_cpt.py @@ -0,0 +1,71 @@ +"""The part-wise fitters budget at the being's measured step-1 ratio once it has one (SAGE #582). + +pub-claude, 2026-11-19: after #773/#774 the step-1 preflight divides by pub-being's measured ratio (3.999), +but fit_state and fit_to_window still budgeted at the fixed CPT 2.9 (~37.3k chars against ~51.5k). On 112 of +112 beats they stepped the conversations to the sparsest rung (2 turns x 700 chars) and recorded "STILL does +not fit at the sparsest rung" on a seed the server counted ~4k tokens under the window. mcnugget-being's +beats carry the same line. These pin: + + * window_budget_chars / fit_state / fit_to_window take the ratio; unset keeps CPT exactly; + * fit_cpt uses a ratio only when it is `measured`; a declared prior, a provisional ratio, a probe or junk + keeps CPT; + * at a measured ratio above CPT the same inputs get a fuller conversation rung; + * the beat computes the calibration BEFORE the part-wise fitters and passes them its ratio, and the + step-1 preflight refit still runs after them. +""" +from pathlib import Path + +from sage.gateway.heartbeat import (CONV_LADDER, CPT, fit_cpt, fit_state, fit_to_window, + window_budget_chars) + +SRC = (Path(__file__).resolve().parents[1] / "heartbeat.py").read_text() + + +def test_budget_takes_the_ratio_and_unset_keeps_cpt(): + assert window_budget_chars(16384, 3000) == int((16384 - 3000 - 512) * CPT) + assert window_budget_chars(16384, 3000, cpt=None) == window_budget_chars(16384, 3000) + assert window_budget_chars(16384, 3000, cpt=3.999) == int((16384 - 3000 - 512) * 3.999) + + +def test_fit_cpt_only_trusts_a_measured_ratio(): + assert fit_cpt({"source": "measured", "used": 3.524}) == 3.524 + for src in ("declared_or_default", "measured_provisional", "probe", None): + assert fit_cpt({"source": src, "used": 3.524}) == CPT, src + assert fit_cpt({"source": "measured", "used": 40.0}) == CPT # implausible + assert fit_cpt({"source": "measured", "used": None}) == CPT + assert fit_cpt({}) == CPT + assert fit_cpt(None) == CPT + + +def _build(per_conv, turn_chars): + # a conversation block whose size falls with the rung, like own_state's + return "x" * (per_conv * (turn_chars or 2000) * 4) + + +def test_a_measured_ratio_above_cpt_gives_back_rungs(): + other = 30_000 + _, rung_fixed, iv_fixed = fit_state(_build, num_ctx=16384, num_predict=3000, other_chars=other) + _, rung_meas, iv_meas = fit_state(_build, num_ctx=16384, num_predict=3000, other_chars=other, cpt=3.999) + assert CONV_LADDER.index(rung_meas) < CONV_LADDER.index(rung_fixed) + assert iv_fixed is not None + + +def test_fit_to_window_trims_less_at_a_measured_ratio(): + blocks = {"digest": "d" * 9000, "recall": "r" * 3000} + fixed = 33_000 + _, iv_fixed = fit_to_window(num_ctx=16384, num_predict=3000, fixed_chars=fixed, blocks=blocks) + out, iv_meas = fit_to_window(num_ctx=16384, num_predict=3000, fixed_chars=fixed, blocks=blocks, cpt=3.999) + assert iv_fixed and not iv_meas and out == blocks + + +def test_the_beat_calibrates_first_and_feeds_the_fitters(): + cal = SRC.index("_cpt_cal = measured_preflight_cpt(") + fit = SRC.index("_fit_cpt = fit_cpt(_cpt_cal)") + state = SRC.index("state_block, conv_rung, conv_intervention = fit_state(") + window = SRC.index("_blocks, _fit_iv = fit_to_window(") + refit = SRC.index("fit_seed_calibrating(\n") + assert cal < fit < state < window < refit + assert "other_chars=_other, cpt=_fit_cpt)" in SRC + assert 'blocks={"digest": digest, "recall": recall}, cpt=_fit_cpt)' in SRC + assert '_cpt_cal["fit_cpt"] = _fit_cpt' in SRC + assert "int(FRAME_TOKENS * _fit_cpt)" in SRC