From c5d93292aee25ecf0e2e66c1d19e641c71b949e6 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 22:31:25 +0000 Subject: [PATCH 1/6] Record boundary context for linear cost samples Signed-off-by: zlareb1 --- tests/unit/code_fixtures.py | 32 ++++++++- tests/unit/lane_walks.py | 6 +- tests/unit/test_code_fixtures.py | 119 ++++++++++++++++++++++++++++++- 3 files changed, 152 insertions(+), 5 deletions(-) diff --git a/tests/unit/code_fixtures.py b/tests/unit/code_fixtures.py index cafbec4830..f7643b2774 100644 --- a/tests/unit/code_fixtures.py +++ b/tests/unit/code_fixtures.py @@ -54,6 +54,23 @@ def _linear_failure_context() -> dict[str, Any]: } +def linear_sample_context() -> dict[str, Any]: + """State outside a cost call; clock deltas include the observer's own work.""" + try: + return { + "process_cpu_ns": time.process_time_ns(), + "monotonic_ns": time.monotonic_ns(), + "thread_cpu_ns": time.thread_time_ns(), + "allocated_blocks": sys.getallocatedblocks(), + "gc_enabled": gc.isenabled(), + "gc_count": gc.get_count(), + "trace_active": sys.gettrace() is not None, + "profile_active": sys.getprofile() is not None, + } + except Exception as error: # noqa: BLE001 — observation must not change the cost judge + return {"context_error": type(error).__name__} + + def assert_linear( cost: Callable[[Any], float], small: Any, @@ -66,6 +83,16 @@ def assert_linear( quadratic one takes 16) or under `floor` seconds. On a loaded host the best of three runs of each counts. No absolute bound: a linear cost at `big` takes several times as long on a slow runner as on a fast one.""" + samples: list[dict[str, Any]] = [] + original = cost + + def observed(size: Any) -> float: + before = linear_sample_context() + value = original(size) + samples.append({"cost_s": value, "before": before, "after": linear_sample_context()}) + return value + + cost = observed a, b = cost(small), cost(big) if b > ratio * a: a, b = min(a, cost(small), cost(small)), min(b, cost(big), cost(big)) @@ -73,10 +100,11 @@ def assert_linear( assert b < ratio * a or b < floor, (a, b) except AssertionError as error: try: - # Diagnostic imports and allocations must follow the failed judge. + # JSON encoding and cumulative context follow the failed judge. import json - note = "linear_cost_context=" + json.dumps(_linear_failure_context(), sort_keys=True) + context = {**_linear_failure_context(), "samples": samples} + note = "linear_cost_context=" + json.dumps(context, sort_keys=True) except Exception as context_error: # Preserve the failed judge. error.add_note("linear_cost_context_error=" + type(context_error).__name__) raise error from context_error diff --git a/tests/unit/lane_walks.py b/tests/unit/lane_walks.py index c0d1b67fa9..3b85ac8053 100644 --- a/tests/unit/lane_walks.py +++ b/tests/unit/lane_walks.py @@ -10,7 +10,7 @@ import time from collections.abc import Callable -from tests.unit.code_fixtures import untraced +from tests.unit.code_fixtures import linear_sample_context, untraced def _markdown(text: str) -> object: @@ -779,6 +779,8 @@ def cost( source = (head + text(n)).encode() parse_s, parse_bytes, parse_raised = 0.0, 0, False errors = {} + # These boundaries include untraced setup and restoration, outside the timed walk. + before = linear_sample_context() if samples is not None else None with untraced(): t0 = time.process_time() # CPU time, as in `walk_cost` if samples is None: @@ -794,6 +796,8 @@ def cost( "n": n, "source_bytes": len(source), "cpu_s": took, + "before": before, + "after": linear_sample_context(), "parsed_bytes": parse_bytes, "lane": lane, "finished": finished, diff --git a/tests/unit/test_code_fixtures.py b/tests/unit/test_code_fixtures.py index a3711d8d9f..29827c4d96 100644 --- a/tests/unit/test_code_fixtures.py +++ b/tests/unit/test_code_fixtures.py @@ -100,7 +100,10 @@ def cost(size: str) -> float: assert len(notes) == 1 assert notes[0].startswith("linear_cost_context=") assert "private-small" not in notes[0] and "private-big" not in notes[0] - assert json.loads(notes[0].removeprefix("linear_cost_context=")) == context + received = json.loads(notes[0].removeprefix("linear_cost_context=")) + samples = received.pop("samples") + assert [sample["cost_s"] for sample in samples] == values + assert received == context @pytest.mark.parametrize("failure", [AssertionError("cost failed"), RuntimeError("cost failed")]) @@ -167,6 +170,118 @@ def numeric(value: object) -> bool: return all(numeric(item) for item in value.values()) if isinstance(value, list): return all(numeric(item) for item in value) - return type(value) in (int, bool) + return type(value) in (int, float, bool) assert numeric(context) + + +@pytest.mark.parametrize("values", [[1.0, 8.0], [1.0, 16.0, 2.0, 3.0, 17.0, 18.0]]) +def test_failed_cost_samples_keep_values_and_boundary_context(values: list[float]) -> None: + calls: list[str] = [] + measurements = iter(values) + + def cost(size: str) -> float: + calls.append(size) + return next(measurements) + + with pytest.raises(AssertionError) as error: + code_fixtures.assert_linear(cost, "private-small", "private-big") + context = json.loads(error.value.__notes__[0].removeprefix("linear_cost_context=")) + samples = context["samples"] + assert [sample["cost_s"] for sample in samples] == values + expected = ["private-small", "private-big"] + if len(values) == 6: + expected += ["private-small", "private-small", "private-big", "private-big"] + assert calls == expected + assert error.value.args == ((values[0], values[1]),) + assert "private-small" not in error.value.__notes__[0] + for sample in samples: + before, after = sample["before"], sample["after"] + for clock in ("process_cpu_ns", "monotonic_ns", "thread_cpu_ns"): + assert after[clock] >= before[clock] >= 0 + assert before["trace_active"] == after["trace_active"] == (sys.gettrace() is not None) + assert before["profile_active"] == after["profile_active"] == (sys.getprofile() is not None) + assert isinstance(before["gc_enabled"], bool) + assert len(before["gc_count"]) == len(after["gc_count"]) == 3 + + +def test_registry_sample_context_surrounds_original_cost_and_excludes_warmup( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from types import SimpleNamespace + + from server.code.domain.extract import bounded_parse + from tests.unit import lane_walks + + # The registry normally runs in a child; this check must restore its wrapper. + monkeypatch.setattr(bounded_parse, "parse", bounded_parse.parse) + clocks = iter([0.0, 1.0, 10.0, 11.0, 20.0, 24.0]) + monkeypatch.setattr(lane_walks, "time", SimpleNamespace(process_time=lambda: next(clocks))) + monkeypatch.setattr(lane_walks, "SHAPES", [("observed", lambda n: "a\\n" * n)]) + calls: list[int] = [] + + def extract(path: str, source: bytes, **kwargs: object) -> tuple[object, bool]: + calls.append(len(source)) + return SimpleNamespace(lang="python", tree=object()), True + + monkeypatch.setattr(lane_walks, "_extract", extract) + diagnostics: dict[str, list[dict[str, object]]] = {} + got = lane_walks.registry_cost("python", diagnostics=diagnostics) + assert got == [("observed", "N", 1.0, 4.0, 0, 0)] + assert len(calls) == 3 + samples = diagnostics["observed"] + assert [sample["cpu_s"] for sample in samples] == [1.0, 4.0] + for sample in samples: + assert sample["after"]["thread_cpu_ns"] >= sample["before"]["thread_cpu_ns"] + + +@pytest.mark.parametrize("large", [4.0, 8.0]) +def test_sample_context_error_preserves_the_original_judge( + monkeypatch: pytest.MonkeyPatch, large: float +) -> None: + def unavailable() -> int: + raise RuntimeError("private context error") + + monkeypatch.setattr(code_fixtures.time, "thread_time_ns", unavailable) + + def cost(n: int) -> float: + return 1.0 if n == 1 else large + + if large == 4.0: + code_fixtures.assert_linear(cost, 1, 4) + else: + with pytest.raises(AssertionError) as error: + code_fixtures.assert_linear(cost, 1, 4) + assert error.value.args == ((1.0, 8.0),) + context = json.loads(error.value.__notes__[0].removeprefix("linear_cost_context=")) + assert [sample["cost_s"] for sample in context["samples"]] == [1.0, 8.0] + for sample in context["samples"]: + assert sample["before"] == sample["after"] == {"context_error": "RuntimeError"} + assert "private context error" not in error.value.__notes__[0] + + +def test_sample_context_is_collected_outside_the_cost_call( + monkeypatch: pytest.MonkeyPatch, +) -> None: + inside = False + snapshots = 0 + real = code_fixtures.linear_sample_context + + def snapshot() -> dict[str, object]: + nonlocal snapshots + assert not inside + snapshots += 1 + return real() + + monkeypatch.setattr(code_fixtures, "linear_sample_context", snapshot) + + def cost(n: int) -> float: + nonlocal inside + inside = True + try: + return float(n) + finally: + inside = False + + code_fixtures.assert_linear(cost, 1, 4) + assert snapshots == 4 From 185948632c89520e1263053aec627187030f9105 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 21:09:53 +0000 Subject: [PATCH 2/6] Scope person-query observations to the decision task Shared client schema reads run with an independent context. Record only decision-tagged statements while retaining unbounded decision reads as failures. Signed-off-by: zlareb1 (cherry picked from commit 57327d5ea5778042f860cadf6cebec9eebb25821) Signed-off-by: zlareb1 --- tests/integration/test_scim_person_live_ch.py | 8 +- .../test_scim_person_query_observation.py | 116 ++++++++++++++++++ 2 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 tests/unit/test_scim_person_query_observation.py diff --git a/tests/integration/test_scim_person_live_ch.py b/tests/integration/test_scim_person_live_ch.py index 34818825b7..a3eeafaa1b 100644 --- a/tests/integration/test_scim_person_live_ch.py +++ b/tests/integration/test_scim_person_live_ch.py @@ -125,13 +125,17 @@ async def people(monkeypatch: pytest.MonkeyPatch) -> AsyncIterator[People]: query_all, query_one = client.query_all, client.query_one async def all_rows(sql: str, params: dict[str, Any] | None = None, **kwargs: Any) -> Any: + observing_person = ch.current_query_tag() == "auth.scim_people" answer = await query_all(sql, params=params, **kwargs) - made.results.append((sql, ch.statement_settings(sql), answer)) + if observing_person: + made.results.append((sql, ch.statement_settings(sql), answer)) return answer async def one_row(sql: str, params: dict[str, Any] | None = None, **kwargs: Any) -> Any: + observing_person = ch.current_query_tag() == "auth.scim_people" answer = await query_one(sql, params=params, **kwargs) - made.results.append((sql, ch.statement_settings(sql), answer)) + if observing_person: + made.results.append((sql, ch.statement_settings(sql), answer)) return answer monkeypatch.setattr(client, "query_all", all_rows) diff --git a/tests/unit/test_scim_person_query_observation.py b/tests/unit/test_scim_person_query_observation.py new file mode 100644 index 0000000000..a0f9ef7f06 --- /dev/null +++ b/tests/unit/test_scim_person_query_observation.py @@ -0,0 +1,116 @@ +"""Person-query evidence excludes reads from independent background tasks.""" + +from __future__ import annotations + +import asyncio +from contextvars import Context +from typing import Any + +import pytest + +from server.auth import scim_people +from server.core import ch, rmt_carry +from server.settings import settings +from tests.integration.test_scim_person_live_ch import people as person_fixture +from tests.unit.test_ch_client_hygiene import _client, _FakeAsync + + +class Queries: + def __init__(self) -> None: + self.started = asyncio.Event() + self.background_done = asyncio.Event() + self.block_person = False + self.transport: list[tuple[str, dict[str, Any]]] = [] + + async def connect(self) -> None: + pass + + async def disconnect(self) -> None: + pass + + async def _read(self, sql: str) -> dict[str, Any]: + self.transport.append((sql, ch.statement_settings(sql, cls="read"))) + if sql == "SELECT 1 AS person" and self.block_person: + self.started.set() + await self.background_done.wait() + return {"n": 0, "at": None, "altered": None} + + async def query_one(self, sql: str, params: Any = None) -> dict[str, Any]: + return await self._read(sql) + + async def query_all(self, sql: str, params: Any = None) -> list[dict[str, Any]]: + return [await self._read(sql)] + + +@pytest.fixture +async def recording(monkeypatch): + client = Queries() + monkeypatch.setattr(ch, "get_client", lambda: client) + stream = person_fixture.__wrapped__(monkeypatch) + observed = await anext(stream) + try: + yield observed, client + finally: + await stream.aclose() + + +@pytest.mark.parametrize("read_timeout", [0, 2.5]) +@pytest.mark.parametrize("many", [False, True]) +async def test_background_schema_reads_do_not_enter_person_evidence( + recording, monkeypatch, read_timeout, many +): + observed, client = recording + monkeypatch.setattr(settings, "ch_timeout_read_s", read_timeout) + client.block_person = True + + async def background(): + await client.started.wait() + try: + with ch.query_tag("rmt_carry.columns"): + await rmt_carry._schema_stamp(client) + finally: + client.background_done.set() + + watcher = asyncio.create_task(background(), context=Context()) + try: + with ch.statement_timeout(1.75), ch.query_tag("auth.scim_people"): + query = client.query_all if many else client.query_one + await query("SELECT 1 AS person") + await watcher + finally: + if not watcher.done(): + watcher.cancel() + await asyncio.gather(watcher, return_exceptions=True) + + assert len(client.transport) == 2 + assert any("system.tables" in sql for sql, _ in client.transport) + assert [sql for sql, _, _ in observed.results] == ["SELECT 1 AS person"] + assert 0 < observed.results[0][1]["max_execution_time"] <= 1.75 + + +@pytest.mark.parametrize("many", [False, True]) +async def test_unbounded_person_reads_remain_visible(recording, monkeypatch, many): + observed, client = recording + monkeypatch.setattr(settings, "ch_timeout_read_s", 0) + with ch.query_tag("auth.scim_people"): + query = client.query_all if many else client.query_one + await query("SELECT 1 AS person") + assert len(observed.results) == 1 + with pytest.raises(KeyError, match="max_execution_time"): + assert 0 < observed.results[0][1]["max_execution_time"] <= 1.75 + + +@pytest.mark.parametrize("read_timeout", [0, 2.5]) +@pytest.mark.parametrize("many", [False, True]) +async def test_actual_person_reader_applies_its_finite_budget_to_driver( + monkeypatch, read_timeout, many +): + driver = _FakeAsync() + client = _client(monkeypatch, driver, ch_timeout_read_s=read_timeout) + monkeypatch.setattr(ch, "get_client", lambda: client) + decision = scim_people._Decision("org", "owner", "owner@example.test", scim_people._now() + 10) + with ch.statement_timeout(1.75), ch.query_tag("auth.scim_people"): + await decision.query("SELECT 1 AS person", {}, many=many) + calls = [applied for kind, sql, applied in driver.calls if sql == "SELECT 1 AS person"] + assert len(calls) == 1 + assert 0 < calls[0]["max_execution_time"] <= 1.75 From dd53f47593fd40daaef33e8265b348d5ccb50461 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 22:47:46 +0000 Subject: [PATCH 3/6] Record boundary context for SQL ratio samples Signed-off-by: zlareb1 --- tests/unit/test_code_fixtures.py | 90 ++++++++++++++++++++++++++ tests/unit/test_document_extraction.py | 26 +++++--- 2 files changed, 107 insertions(+), 9 deletions(-) diff --git a/tests/unit/test_code_fixtures.py b/tests/unit/test_code_fixtures.py index 29827c4d96..d404e79e6e 100644 --- a/tests/unit/test_code_fixtures.py +++ b/tests/unit/test_code_fixtures.py @@ -285,3 +285,93 @@ def cost(n: int) -> float: code_fixtures.assert_linear(cost, 1, 4) assert snapshots == 4 + + +@pytest.mark.parametrize("raises", [False, True]) +def test_document_cpu_samples_surround_the_original_timed_call( + monkeypatch: pytest.MonkeyPatch, raises: bool +) -> None: + from types import SimpleNamespace + + from tests.unit import test_document_extraction as document + + clocks = iter([3.0, 5.0]) + monkeypatch.setattr(document, "time", SimpleNamespace(process_time=lambda: next(clocks))) + snapshots: list[str] = [] + inside = False + real = code_fixtures.linear_sample_context + + def snapshot() -> dict[str, object]: + assert not inside + snapshots.append("boundary") + return real() + + monkeypatch.setattr(document, "linear_sample_context", snapshot, raising=False) + failure = RuntimeError("private cost failure") + + def cost() -> None: + nonlocal inside + inside = True + try: + assert sys.gettrace() is None + if raises: + raise failure + finally: + inside = False + + samples: list[dict[str, object]] = [] + if raises: + with pytest.raises(RuntimeError) as error: + document._cpu(cost, samples=samples) + assert error.value is failure + else: + assert document._cpu(cost, samples=samples) == 2.0 + assert snapshots == ["boundary", "boundary"] + assert len(samples) == 1 + assert samples[0]["after"]["thread_cpu_ns"] >= samples[0]["before"]["thread_cpu_ns"] + assert "private cost failure" not in json.dumps(samples) + + +def test_document_cpu_without_samples_does_not_collect_context( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from types import SimpleNamespace + + from tests.unit import test_document_extraction as document + + clocks = iter([3.0, 5.0]) + monkeypatch.setattr(document, "time", SimpleNamespace(process_time=lambda: next(clocks))) + + def forbidden() -> dict[str, object]: + pytest.fail("A caller without diagnostics must not collect context") + + monkeypatch.setattr(document, "linear_sample_context", forbidden, raising=False) + assert document._cpu(lambda: None) == 2.0 + + +def test_sql_failure_keeps_the_complete_sample_context( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from tests.unit import test_document_extraction as document + + values = iter([1.0, 25.0]) + recorded: list[dict[str, object]] = [] + + def cpu(fn: object, *, samples: list[dict[str, object]]) -> float: + context = { + "before": {"thread_cpu_ns": len(recorded)}, + "after": {"thread_cpu_ns": len(recorded) + 1}, + } + samples.append(context) + recorded.append(context) + return next(values) + + monkeypatch.setattr(document, "_cpu", cpu) + with pytest.raises(AssertionError) as error: + document.test_one_line_sql_with_a_comment_after_every_semicolon_splits_in_linear_time() + assert json.loads(str(error.value.args[0]).splitlines()[0]) == { + "small_s": 1.0, + "large_s": 25.0, + "samples": recorded, + } + assert len(recorded) == 2 diff --git a/tests/unit/test_document_extraction.py b/tests/unit/test_document_extraction.py index 879b5796a1..c403655e18 100644 --- a/tests/unit/test_document_extraction.py +++ b/tests/unit/test_document_extraction.py @@ -25,7 +25,7 @@ from loom_extractor import ExtractionError, Options, decode, detect_format, extract from loom_extractor.artifact import Artifact, Block, Coverage, Locator from server.knowledge.domain.extract import InvalidArtifact, parse_artifact, structural -from tests.unit.code_fixtures import untraced +from tests.unit.code_fixtures import linear_sample_context, untraced FIXTURES = Path(__file__).resolve().parent / "fixtures" / "knowledge" @@ -1388,14 +1388,19 @@ def test_a_heading_line_with_a_long_run_of_blanks_is_read_whole() -> None: assert (heading.type, heading.text) == ("heading", "a b") -def _cpu(fn: Any) -> float: +def _cpu(fn: Any, *, samples: list[dict[str, Any]] | None = None) -> float: """This process's CPU seconds `fn` takes, timed `untraced`, so neither a test running beside it nor the tracer counts. Every caller runs extractor code, which the unit job's coverage does not measure, so the pause loses no coverage.""" - with untraced(): - start = time.process_time() - fn() - return time.process_time() - start + before = linear_sample_context() if samples is not None else None + try: + with untraced(): + start = time.process_time() + fn() + return time.process_time() - start + finally: + if samples is not None: + samples.append({"before": before, "after": linear_sample_context()}) def _within(seconds: float, fn: Any) -> None: @@ -1583,13 +1588,16 @@ def test_one_line_sql_with_a_comment_after_every_semicolon_splits_in_linear_time sql._spans("SELECT 1;", "clickhouse") # imports sqlglot and the dialect before timing small_text = "SELECT 1; /* c */ " * 10_000 - small = _cpu(lambda: sql._spans(small_text, "clickhouse")) + samples: list[dict[str, Any]] = [] + small = _cpu(lambda: sql._spans(small_text, "clickhouse"), samples=samples) text = "SELECT 1; /* c */ " * 160_000 out: list[list[Any]] = [] - large = _cpu(lambda: out.append(sql._spans(text, "clickhouse"))) + large = _cpu(lambda: out.append(sql._spans(text, "clickhouse")), samples=samples) # A linear split costs about 17x for 16x the statements; a newline search from each `;` # not stopped at the next token scans to the end of this one-line text (about 50x). - assert large < 24 * small + 0.05, (small, large) + assert large < 24 * small + 0.05, json.dumps( + {"small_s": small, "large_s": large, "samples": samples}, sort_keys=True + ) assert len(out[0]) == 160_000 # No span cuts a comment: each holds every `*/` its `/*` opens. assert all( From 7905953ae9e537798548dc4dcd5c65ba399c268d Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 23:41:03 +0000 Subject: [PATCH 4/6] Lane-walk failures retain all six sample contexts Signed-off-by: zlareb1 --- tests/unit/lane_walks.py | 19 ++- tests/unit/test_code_fixtures.py | 245 +++++++++++++++++++++++++++++ tests/unit/test_code_regex_cost.py | 10 +- 3 files changed, 269 insertions(+), 5 deletions(-) diff --git a/tests/unit/lane_walks.py b/tests/unit/lane_walks.py index 3b85ac8053..1c298155ab 100644 --- a/tests/unit/lane_walks.py +++ b/tests/unit/lane_walks.py @@ -506,17 +506,32 @@ def _inline_templates(n: int) -> str: _WALK_N = 100_000 -def walk_cost(name: str) -> tuple[float, float]: +def walk_cost( + name: str, *, diagnostics: list[dict[str, object]] | None = None +) -> tuple[float, float]: """The best of three times of the walk at N and 4N characters.""" walk, text = next((w[1], w[2]) for w in WALKS if w[0] == name) - def cost(n: int) -> float: + def timed_cost(n: int) -> float: t = text(n) with untraced(): # a full collection costs in proportion to the heap, not to the text t0 = time.process_time() # CPU time: a loaded host stretches wall time, not this walk(t) return time.process_time() - t0 + def cost(n: int) -> float: + if diagnostics is None: + return timed_cost(n) + before = linear_sample_context() + cpu_s = None + try: + cpu_s = timed_cost(n) + return cpu_s + finally: + diagnostics.append( + {"n": n, "cpu_s": cpu_s, "before": before, "after": linear_sample_context()} + ) + return min(cost(_WALK_N) for _ in range(3)), min(cost(4 * _WALK_N) for _ in range(3)) diff --git a/tests/unit/test_code_fixtures.py b/tests/unit/test_code_fixtures.py index d404e79e6e..3dfa1d5469 100644 --- a/tests/unit/test_code_fixtures.py +++ b/tests/unit/test_code_fixtures.py @@ -375,3 +375,248 @@ def cpu(fn: object, *, samples: list[dict[str, object]]) -> float: "samples": recorded, } assert len(recorded) == 2 + + +@pytest.mark.parametrize( + "values", [[3.0, 1.0, 2.0, 6.0, 4.0, 5.0], [3.0, 1.0, 2.0, 10.0, 8.0, 9.0]] +) +def test_walk_samples_preserve_six_costs_and_boundary_context( + monkeypatch: pytest.MonkeyPatch, values: list[float] +) -> None: + from contextlib import contextmanager + from types import SimpleNamespace + + from tests.unit import lane_walks + + clocks = iter( + value + for index, value in enumerate(values) + for value in (index * 100.0, index * 100.0 + value) + ) + events: list[tuple[str, object]] = [] + inside = False + + @contextmanager + def region(): + nonlocal inside + inside = True + try: + yield + finally: + inside = False + + def source(n: int) -> str: + events.append(("source", n)) + return str(n) + + def walk(text: str) -> None: + assert inside + events.append(("walk", text)) + + def snapshot() -> dict[str, object]: + assert not inside + events.append(("snapshot", len(events))) + return {"boundary": len(events)} + + monkeypatch.setattr(lane_walks, "WALKS", [("observed", walk, source)]) + monkeypatch.setattr(lane_walks, "untraced", region) + monkeypatch.setattr(lane_walks, "time", SimpleNamespace(process_time=lambda: next(clocks))) + monkeypatch.setattr(lane_walks, "linear_sample_context", snapshot) + samples: list[dict[str, object]] = [] + small, big = lane_walks.walk_cost("observed", diagnostics=samples) + assert (small, big) == (min(values[:3]), min(values[3:])) + assert (big < 0.1 or big < 8.0 * small) == (values[-2] == 4.0) + sizes = [100_000] * 3 + [400_000] * 3 + assert [sample["n"] for sample in samples] == sizes + assert [sample["cpu_s"] for sample in samples] == values + assert [value for event, value in events if event == "source"] == sizes + assert [value for event, value in events if event == "walk"] == [str(n) for n in sizes] + assert len([event for event, _ in events if event == "snapshot"]) == 12 + for sample in samples: + assert sample["after"]["boundary"] > sample["before"]["boundary"] + + +def test_walk_sample_keeps_the_original_walker_error( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from types import SimpleNamespace + + from tests.unit import lane_walks + + failure = RuntimeError("private walker error") + seen: list[str] = [] + + def walk(text: str) -> None: + seen.append(text) + raise failure + + monkeypatch.setattr(lane_walks, "WALKS", [("observed", walk, lambda n: "private source")]) + monkeypatch.setattr(lane_walks, "time", SimpleNamespace(process_time=lambda: 0.0)) + samples: list[dict[str, object]] = [] + with pytest.raises(RuntimeError) as error: + lane_walks.walk_cost("observed", diagnostics=samples) + assert error.value is failure + assert seen == ["private source"] + assert len(samples) == 1 + assert samples[0]["n"] == 100_000 + assert samples[0]["cpu_s"] is None + assert "private source" not in json.dumps(samples) + assert "private walker error" not in json.dumps(samples) + + +@pytest.mark.parametrize("large", [4.0, 8.0]) +def test_walk_context_error_keeps_selected_costs( + monkeypatch: pytest.MonkeyPatch, large: float +) -> None: + from types import SimpleNamespace + + from tests.unit import lane_walks + + def unavailable() -> int: + raise RuntimeError("private observer error") + + clocks = iter( + [ + 0.0, + 1.0, + 10.0, + 11.0, + 20.0, + 21.0, + 30.0, + 30.0 + large, + 40.0, + 40.0 + large, + 50.0, + 50.0 + large, + ] + ) + monkeypatch.setattr(code_fixtures.time, "thread_time_ns", unavailable) + monkeypatch.setattr(lane_walks, "WALKS", [("observed", lambda text: None, lambda n: "")]) + monkeypatch.setattr(lane_walks, "time", SimpleNamespace(process_time=lambda: next(clocks))) + samples: list[dict[str, object]] = [] + small, big = lane_walks.walk_cost("observed", diagnostics=samples) + assert (small, big) == (1.0, large) + assert (big < 0.1 or big < 8.0 * small) == (large == 4.0) + assert len(samples) == 6 + for sample in samples: + assert sample["before"] == sample["after"] == {"context_error": "RuntimeError"} + assert "private observer error" not in json.dumps(samples) + + +def test_walk_without_diagnostics_does_not_collect_context( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from types import SimpleNamespace + + from tests.unit import lane_walks + + def forbidden() -> dict[str, object]: + pytest.fail("A caller without diagnostics must not collect context") + + monkeypatch.setattr(lane_walks, "linear_sample_context", forbidden) + monkeypatch.setattr(lane_walks, "WALKS", [("observed", lambda text: None, lambda n: "")]) + clocks = iter(range(12)) + monkeypatch.setattr(lane_walks, "time", SimpleNamespace(process_time=lambda: next(clocks))) + assert lane_walks.walk_cost("observed") == (1, 1) + + +def test_walk_child_publishes_the_complete_sample_packet( + monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] +) -> None: + import faulthandler + from types import SimpleNamespace + + from tests.unit import lane_walks + from tests.unit import test_code_regex_cost as costs + + samples = [ + {"n": n, "cpu_s": cpu, "before": {"clock": i}, "after": {"clock": i + 1}} + for i, (n, cpu) in enumerate( + zip([100_000] * 3 + [400_000] * 3, [3.0, 1.0, 2.0, 6.0, 4.0, 5.0], strict=True) + ) + ] + + def walk_cost(name: str, *, diagnostics: list[dict[str, object]]) -> tuple[float, float]: + assert name == "observed" + diagnostics.extend(samples) + return 1.0, 4.0 + + monkeypatch.setattr(lane_walks, "walk_cost", walk_cost) + monkeypatch.setattr(sys, "argv", ["child", "observed"]) + + child_faults = {name: getattr(faulthandler, name) for name in dir(faulthandler)} + child_faults["dump_traceback_later"] = lambda *a, **kw: None + monkeypatch.setitem(sys.modules, "faulthandler", SimpleNamespace(**child_faults)) + exec(costs._WALK_CHILD, {}) + assert json.loads(capsys.readouterr().out) == {"costs": [1.0, 4.0], "samples": samples} + + +@pytest.mark.parametrize("large", [4.0, 8.0]) +def test_walk_parent_keeps_judge_and_failure_packet( + monkeypatch: pytest.MonkeyPatch, large: float +) -> None: + from types import SimpleNamespace + + from tests.unit import test_code_regex_cost as costs + + samples = [ + {"n": n, "cpu_s": cpu, "before": {"clock": i}, "after": {"clock": i + 1}} + for i, (n, cpu) in enumerate( + zip( + [100_000] * 3 + [400_000] * 3, + [3.0, 1.0, 2.0, large + 2, large, large + 1], + strict=True, + ) + ) + ] + packet = {"costs": [1.0, large], "samples": samples} + + def run(argv: list[str], **kwargs: object) -> object: + assert argv[:2] == [sys.executable, "-c"] + assert argv[-1] == "observed" + assert kwargs["capture_output"] is True and kwargs["text"] is True + assert kwargs["timeout"] == 120 + return SimpleNamespace(returncode=0, stdout=json.dumps(packet), stderr="") + + monkeypatch.setattr(costs.subprocess, "run", run) + if large == 4.0: + costs.test_a_lane_walk_costs_linear_time("observed") + else: + with pytest.raises(AssertionError) as error: + costs.test_a_lane_walk_costs_linear_time("observed") + assert json.loads(str(error.value.args[0]).splitlines()[0]) == { + "name": "observed", + **packet, + } + + +@pytest.mark.parametrize("failure", ["child", "invalid", "timeout"]) +def test_walk_child_errors_remain_failures(monkeypatch: pytest.MonkeyPatch, failure: str) -> None: + from types import SimpleNamespace + + from tests.unit import test_code_regex_cost as costs + + timeout = costs.subprocess.TimeoutExpired("walk-child", 120) + + def run(*args: object, **kwargs: object) -> object: + if failure == "timeout": + raise timeout + return SimpleNamespace( + returncode=1 if failure == "child" else 0, + stdout="invalid packet", + stderr="child failed", + ) + + monkeypatch.setattr(costs.subprocess, "run", run) + expected = { + "child": AssertionError, + "invalid": json.JSONDecodeError, + "timeout": costs.subprocess.TimeoutExpired, + }[failure] + with pytest.raises(expected) as error: + costs.test_a_lane_walk_costs_linear_time("observed") + if failure == "timeout": + assert error.value is timeout + elif failure == "child": + assert str(error.value.args[0]).startswith("child failed") diff --git a/tests/unit/test_code_regex_cost.py b/tests/unit/test_code_regex_cost.py index da46a0698b..800bc9e248 100644 --- a/tests/unit/test_code_regex_cost.py +++ b/tests/unit/test_code_regex_cost.py @@ -293,7 +293,8 @@ def cost(n: int, pre: str = pre, unit: str = unit, end: str = end) -> float: import faulthandler, json, sys faulthandler.dump_traceback_later({_WALK_DEADLINE_S}, exit=True) from tests.unit.lane_walks import walk_cost -print(json.dumps(walk_cost(sys.argv[1]))) +samples = [] +print(json.dumps({{"costs": walk_cost(sys.argv[1], diagnostics=samples), "samples": samples}})) """ @@ -307,8 +308,11 @@ def test_a_lane_walk_costs_linear_time(name: str) -> None: cwd=pathlib.Path(__file__).resolve().parents[2], ) assert done.returncode == 0, done.stderr[-4000:] - small, big = json.loads(done.stdout) - assert big < _FLOOR_S or big < RATIO * small, (name, small, big) + packet = json.loads(done.stdout) + small, big = packet["costs"] + assert big < _FLOOR_S or big < RATIO * small, json.dumps( + {"name": name, **packet}, sort_keys=True + ) # The class guard: every lane parser in the ingest's registry, over generated shapes (runs From 1b0052bfbc692130090dd1718ad5a6809ff8af95 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 23:36:42 +0000 Subject: [PATCH 5/6] Hook contract cleanup waits for detached jobs Keep each test sandbox until its detached flusher removes its final job marker. Preserve hook deadlines and verify unfinished jobs refuse cleanup. Signed-off-by: zlareb1 (cherry picked from commit 2e637644d2abe404e48ac541967164b85a735036) Signed-off-by: zlareb1 --- .../tests/hook-contract.test.mjs | 13 +- .../claude-code-plugin/tests/hook-sandbox.mjs | 12 ++ .../tests/hook-sandbox.test.mjs | 176 ++++++++++++++++++ 3 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 integrations/claude-code-plugin/tests/hook-sandbox.mjs create mode 100644 integrations/claude-code-plugin/tests/hook-sandbox.test.mjs diff --git a/integrations/claude-code-plugin/tests/hook-contract.test.mjs b/integrations/claude-code-plugin/tests/hook-contract.test.mjs index 7150c7da1b..bb3a7e4b6c 100644 --- a/integrations/claude-code-plugin/tests/hook-contract.test.mjs +++ b/integrations/claude-code-plugin/tests/hook-contract.test.mjs @@ -13,6 +13,7 @@ import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { hostEnv } from "./host-env.mjs"; +import { removeSandbox } from "./hook-sandbox.mjs"; const here = path.dirname(fileURLToPath(import.meta.url)); const root = path.join(here, ".."); @@ -285,7 +286,7 @@ test("malformed, empty and non-object payloads: every hook exits 0 silently", as assertContract(await runAll(cases), 8000); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -305,7 +306,7 @@ test("no token anywhere: every hook exits 0 silently and sends no request", asyn assert.deepEqual(loom.ops, [], "a hook without a token reached the server"); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -330,7 +331,7 @@ test("server unreachable, answering 401, or hanging: every hook stays within its } finally { await unauthorized.close(); await hang.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -348,7 +349,7 @@ test("a host that never closes stdin does not hold a hook open", async () => { assertContract(await runAll(cases), 8000); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -366,7 +367,7 @@ test("a well-formed session against a healthy server: every hook exits 0 silentl assert.ok(loom.ops.length > 0, "no hook reached a healthy server"); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -385,6 +386,6 @@ test("a payload over the stdin cap is dropped, not parsed: every hook exits 0 si assertContract(await runAll(cases, 4), 15000); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); diff --git a/integrations/claude-code-plugin/tests/hook-sandbox.mjs b/integrations/claude-code-plugin/tests/hook-sandbox.mjs new file mode 100644 index 0000000000..127c0ff544 --- /dev/null +++ b/integrations/claude-code-plugin/tests/hook-sandbox.mjs @@ -0,0 +1,12 @@ +import fs from "node:fs"; +import { setTimeout as delay } from "node:timers/promises"; + +export async function removeSandbox(box, { timeoutMs = 45000 } = {}) { + const deadline = performance.now() + timeoutMs; + // The flusher removes its job only after its last cursor/recording write. + while (fs.readdirSync(box.tmp).some((name) => /^loom-flush-\d+-\d+\.json$/.test(name))) { + if (performance.now() >= deadline) throw new Error("unfinished detached jobs in hook sandbox"); + await delay(10); + } + fs.rmSync(box.dir, { recursive: true, force: true }); +} diff --git a/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs b/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs new file mode 100644 index 0000000000..2584c21e18 --- /dev/null +++ b/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs @@ -0,0 +1,176 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { fork, spawn } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import http from "node:http"; +import path from "node:path"; +import { once } from "node:events"; +import { setImmediate as immediate } from "node:timers/promises"; +import { removeSandbox } from "./hook-sandbox.mjs"; +import { dispatchFlush } from "../lib/flush-job.mjs"; + +function sandbox() { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "loom-hook-drain-")); + const tmp = path.join(dir, "tmp"); + fs.mkdirSync(tmp); + return { dir, tmp }; +} + +test("sandbox removal waits for its detached job's final write", async () => { + const box = sandbox(); + const script = path.join(box.dir, "writer.cjs"); + fs.writeFileSync( + script, + ` +const fs = require("node:fs"); +const path = require("node:path"); +const root = process.argv[2]; +const job = path.join(root, "tmp", "loom-flush-1-" + process.pid + ".json"); +fs.writeFileSync(job, "{}"); +process.send("ready"); +process.once("message", () => { + fs.writeFileSync(path.join(root, "cursor"), "done"); + fs.unlinkSync(job); + process.send("written", () => process.disconnect()); +}); +`, + ); + const child = fork(script, [box.dir], { env: {}, stdio: ["ignore", "ignore", "pipe", "ipc"], detached: true }); + const closed = once(child, "close"); + let removing; + let released = false; + let stderr = ""; + child.stderr.on("data", (part) => { + stderr += part; + }); + try { + const [ready] = await once(child, "message"); + assert.equal(ready, "ready"); + let removed = false; + removing = removeSandbox(box).then(() => { + removed = true; + }); + await immediate(); + assert.equal(removed, false, "the owned writer still has a pending job"); + assert.equal(fs.existsSync(box.dir), true); + const written = once(child, "message"); + released = true; + child.send("release"); + assert.equal((await written)[0], "written"); + await removing; + assert.deepEqual(await closed, [0, null], stderr); + assert.equal(fs.existsSync(box.dir), false); + } finally { + if (!released && child.connected) child.send("release"); + await closed; + if (removing) await removing; + fs.rmSync(box.dir, { recursive: true, force: true }); + } +}); + +test("an unfinished job refuses removal at the cleanup deadline", async () => { + const box = sandbox(); + const job = path.join(box.tmp, "loom-flush-2-3.json"); + fs.writeFileSync(job, "{}"); + try { + await assert.rejects(removeSandbox(box, { timeoutMs: 0 }), /unfinished detached jobs/); + assert.equal(fs.existsSync(job), true); + } finally { + fs.rmSync(box.dir, { recursive: true, force: true }); + } +}); + +test("an empty job census permits removal with other sandbox files", async () => { + const box = sandbox(); + fs.writeFileSync(path.join(box.tmp, "cursor"), "42"); + await removeSandbox(box); + assert.equal(fs.existsSync(box.dir), false); +}); + +test("sandbox removal waits for the real flusher's completion marker", async () => { + const box = sandbox(); + let arrived; + const request = new Promise((resolve) => { + arrived = resolve; + }); + let respond; + const server = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + respond = () => { + if (!res.writableEnded) { + res.writeHead(200, { "content-type": "application/json" }); + res.end("{}"); + } + }; + arrived(); + }); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + let child; + let closed; + let removing; + const token = [ + "e30", + Buffer.from(JSON.stringify({ org: "test", namespaces: ["scope"] })).toString("base64url"), + "test", + ].join("."); + try { + assert.equal( + dispatchFlush( + { + appends: [{ org: "test", namespace: "scope", agent: "test", session_id: "test" }], + cursorPath: path.join(box.tmp, "cursor"), + advanceTo: 42, + }, + { + tmp: box.tmp, + timeoutMs: 1000, + spawnImpl: (command, args, options) => { + child = spawn(command, args, { + ...options, + env: { + HOME: box.dir, + TMPDIR: box.tmp, + LOOM_BASE_URL: `http://127.0.0.1:${server.address().port}`, + LOOM_API_KEY: token, + LOOM_HOOK_TIMEOUT_MS: "1000", + LOOM_AUTO_SIGNUP: "off", + }, + }); + closed = once(child, "close"); + return child; + }, + }, + ), + true, + ); + await Promise.race([ + request, + closed.then(() => { + throw new Error("flusher exited before its request"); + }), + ]); + let removed = false; + removing = removeSandbox(box).then(() => { + removed = true; + }); + await immediate(); + assert.equal(removed, false); + assert.equal(fs.existsSync(box.dir), true); + respond(); + assert.deepEqual(await closed, [0, null]); + await removing; + assert.equal(fs.existsSync(box.dir), false); + } finally { + respond?.(); + try { + if (closed) await closed; + if (removing) await removing; + } finally { + await new Promise((resolve) => server.close(resolve)); + fs.rmSync(box.dir, { recursive: true, force: true }); + } + } +}); From e12f192a70c6f100e6e19a133e6df65b6b849751 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 23:41:55 +0000 Subject: [PATCH 6/6] Hook sandbox regressions use isolated homes on every platform Signed-off-by: zlareb1 (cherry picked from commit 334a924acbf4e7b561a37d2bdcc7ca632f36a373) Signed-off-by: zlareb1 --- .../claude-code-plugin/tests/hook-sandbox.test.mjs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs b/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs index 2584c21e18..d9be5727eb 100644 --- a/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs +++ b/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs @@ -1,3 +1,4 @@ +import "./temp-home.mjs"; import { test } from "node:test"; import assert from "node:assert/strict"; import { fork, spawn } from "node:child_process"; @@ -8,6 +9,7 @@ import path from "node:path"; import { once } from "node:events"; import { setImmediate as immediate } from "node:timers/promises"; import { removeSandbox } from "./hook-sandbox.mjs"; +import { hostEnv } from "./host-env.mjs"; import { dispatchFlush } from "../lib/flush-job.mjs"; function sandbox() { @@ -36,7 +38,11 @@ process.once("message", () => { }); `, ); - const child = fork(script, [box.dir], { env: {}, stdio: ["ignore", "ignore", "pipe", "ipc"], detached: true }); + const child = fork(script, [box.dir], { + env: hostEnv({}), + stdio: ["ignore", "ignore", "pipe", "ipc"], + detached: true, + }); const closed = once(child, "close"); let removing; let released = false; @@ -130,14 +136,14 @@ test("sandbox removal waits for the real flusher's completion marker", async () spawnImpl: (command, args, options) => { child = spawn(command, args, { ...options, - env: { + env: hostEnv({ HOME: box.dir, TMPDIR: box.tmp, LOOM_BASE_URL: `http://127.0.0.1:${server.address().port}`, LOOM_API_KEY: token, LOOM_HOOK_TIMEOUT_MS: "1000", LOOM_AUTO_SIGNUP: "off", - }, + }), }); closed = once(child, "close"); return child;