Skip to content

Commit 0ac93d1

Browse files
committed
fix(context): skip fold prefetch without baseline metering
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
1 parent 7b9cee5 commit 0ac93d1

2 files changed

Lines changed: 44 additions & 11 deletions

File tree

‎loopx/control_plane/turn_driver/semantic_unit_hook.py‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def _run_semantic_unit_obs_command(
7070
context_epoch: str,
7171
prefix_digest: str | None,
7272
quiescent_boundary: bool,
73+
can_measure_savings: bool,
7374
cwd: object,
7475
) -> tuple[dict[str, object] | None, bool]:
7576
raw_obs = fold_cfg.get("obs_next_cmd")
@@ -106,6 +107,9 @@ def _run_semantic_unit_obs_command(
106107
)
107108
if not safety.get("eligible"):
108109
return obs, False
110+
if not can_measure_savings:
111+
# An unmeasured baseline cannot justify replacing the original context.
112+
return None, False
109113
argv = list(safety["safety"]["argv"])
110114
run_result = run_bounded_prefetch_subprocess(
111115
argv,
@@ -226,16 +230,6 @@ def _evaluate_semantic_unit_fold(
226230
) -> dict[str, object]:
227231
raw_prefix = fold_req.get("prefix_digest")
228232
prefix_digest = raw_prefix if isinstance(raw_prefix, str) else None
229-
obs_payload, obs_executed = _run_semantic_unit_obs_command(
230-
fold_req,
231-
anchor=anchor,
232-
todo_id=todo_id,
233-
turn_instance_id=turn_instance_id,
234-
context_epoch=context_epoch,
235-
prefix_digest=prefix_digest,
236-
quiescent_boundary=quiescent,
237-
cwd=cwd,
238-
)
239233
encoding_name = str(fold_req.get("tokenizer_encoding") or DEFAULT_TOKENIZER_ENCODING)
240234
baseline_tokens = _opt_int(fold_req.get("baseline_tokens"))
241235
baseline_bytes = _opt_int(fold_req.get("baseline_bytes"))
@@ -252,6 +246,17 @@ def _evaluate_semantic_unit_fold(
252246
if baseline_bytes is None:
253247
baseline_bytes = len(baseline_text.encode("utf-8"))
254248

249+
obs_payload, obs_executed = _run_semantic_unit_obs_command(
250+
fold_req,
251+
anchor=anchor,
252+
todo_id=todo_id,
253+
turn_instance_id=turn_instance_id,
254+
context_epoch=context_epoch,
255+
prefix_digest=prefix_digest,
256+
quiescent_boundary=quiescent,
257+
can_measure_savings=baseline_tokens is not None and tokenizer_meta is not None,
258+
cwd=cwd,
259+
)
255260
replacement_tokens = _opt_int(fold_req.get("replacement_tokens"))
256261
raw_receipt = fold_req.get("completion_receipt")
257262
def call_fold(

‎tests/control_plane/test_semantic_unit_fold_hooks.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,15 @@ def spy_exec(argv: list[str], *, timeout_ms: int, max_output_bytes: int, cwd: st
189189

190190
@pytest.mark.parametrize(
191191
"case_name",
192-
["missing_receipt", "non_quiescent", "command_error", "negative_savings", "prefix_mismatch"],
192+
[
193+
"missing_receipt",
194+
"non_quiescent",
195+
"command_error",
196+
"negative_savings",
197+
"zero_savings",
198+
"tokenizer_unavailable",
199+
"prefix_mismatch",
200+
],
193201
)
194202
def test_codex_context_hook_semantic_unit_fails_closed_keep_raw(
195203
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, case_name: str,
@@ -271,6 +279,14 @@ def spy_exec(argv: list[str], *, timeout_ms: int, max_output_bytes: int, cwd: st
271279
obs_cmd["command"] = ["cat", str(tmp_path / "missing-obs-file.txt")]
272280
elif case_name == "negative_savings":
273281
fold_spec["baseline_text"] = "short"
282+
elif case_name == "zero_savings":
283+
fold_spec.update(
284+
baseline_tokens=100,
285+
replacement_tokens=100,
286+
tokenizer={"name": "fixture", "encoding": "o200k_base"},
287+
)
288+
elif case_name == "tokenizer_unavailable":
289+
monkeypatch.setattr(hook_mod, "count_verifiable_tokens", lambda *args, **kwargs: None)
274290
elif case_name == "prefix_mismatch":
275291
fold_spec["prefix_digest"] = "9" * 64
276292

@@ -285,6 +301,18 @@ def spy_exec(argv: list[str], *, timeout_ms: int, max_output_bytes: int, cwd: st
285301
assert "retained obligation" in res["hookSpecificOutput"]["additionalContext"]
286302
if case_name in {"missing_receipt", "non_quiescent", "prefix_mismatch"}:
287303
assert obs_spawns == [], f"pre-check must prevent obs_next_cmd execution for {case_name}"
304+
if case_name == "tokenizer_unavailable":
305+
assert fold_out["fallback_reason"] == "token_estimation_unavailable"
306+
assert fold_out["baseline_tokens"] is None
307+
assert fold_out["replacement_tokens"] is None
308+
assert fold_out["net_savings_tokens"] is None
309+
assert fold_out["bytes_saved"] == 0
310+
assert fold_out["executed"] is False
311+
assert obs_spawns == []
312+
if case_name == "zero_savings":
313+
assert fold_out["fallback_reason"] == "negative_net_savings"
314+
assert fold_out["baseline_tokens"] == fold_out["replacement_tokens"] == 100
315+
assert fold_out["net_savings_tokens"] == 0
288316
saved_state = codex_context._state(ctx_dir)
289317
assert "todo_fail_1" in saved_state["semantic_unit_anchors"]
290318
assert not saved_state.get("semantic_unit_folds")

0 commit comments

Comments
 (0)