Skip to content
Merged
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
17 changes: 17 additions & 0 deletions test/test_default_context_admission.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path
from unittest.mock import Mock

Expand All @@ -28,8 +29,24 @@
]


class _FrozenClock(datetime):
"""A ``datetime`` whose ``now()`` does not advance.

The prompt carries a minute-resolution ``[CURRENT DATE]`` line, so two builds
that straddle a minute boundary differ in that line alone -- and across
midnight the weekday name changes length, so an exact-fit cap taken from the
first build does not fit the second. Tests here compare builds byte for
byte and size caps from them, so every build reads one instant.
"""

@classmethod
def now(cls, tz=None): # type: ignore[override]
return datetime(2026, 1, 1, 12, 0).replace(tzinfo=tz)


@pytest.fixture
def rig(tmp_path, monkeypatch):
monkeypatch.setattr(ctx, "datetime", _FrozenClock)
cfg = KiroCrewConfig()
monkeypatch.setattr(ctx.KiroCrewConfig, "load", lambda: cfg)
monkeypatch.setattr(ctx, "agent_skill_globs", lambda agent: [])
Expand Down
31 changes: 11 additions & 20 deletions test/test_diag_procs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,21 +1166,25 @@ def test_a_read_refused_as_too_soon_does_not_restart_the_window(tmp_path: Path)
table.add(GATEWAY, OUTSIDER, cmdline=GATEWAY_ARGV, env=dict(MARKER))
table.add(CHAT, GATEWAY, cmdline=CHAT_ARGV, env=dict(MARKER), utime=0, runq_ns=0)

# Every read is pinned to one clock reading, so the hand-aged windows are
# the whole window. Unpinned, the second read's gap is 0.8s plus however
# long the runner takes to finish the first scan and start the second, which
# on a loaded runner crosses the 1s floor and admits the read under test.
baseline = procs.RateBaseline()
table.scan_rated(baseline)
table.scan_rated(baseline, monotonic=SCAN_MONOTONIC)
_age_baseline(baseline, 0.8)
stored = baseline._prev
assert stored is not None
pinned = stored.monotonic

refused = table.scan_rated(baseline)
refused = table.scan_rated(baseline, monotonic=SCAN_MONOTONIC)
assert refused.nodes[CHAT].cpu_pct is None, "0.8s apart is under the floor"
assert baseline._prev is not None
assert baseline._prev.monotonic == pinned, "the refused read replaced the baseline"

# 0.8s + 0.6s clears the 1s floor. Had the refused read replaced the
# baseline, only the 0.6s would remain and this read would be refused too.
# 0.7 CPU-seconds and 0.14s of run-queue wait over that window.
# 0.7 CPU-seconds and 0.14s of run-queue wait over exactly that 1.4s window.
_age_baseline(baseline, 0.6)
table.add(
CHAT,
Expand All @@ -1190,23 +1194,10 @@ def test_a_read_refused_as_too_soon_does_not_restart_the_window(tmp_path: Path)
utime=table.clk_tck * 7 // 10,
runq_ns=140_000_000,
)
recovered = table.scan_rated(baseline)

# A BAND, not a point. Both ageings have to stay under the 1s floor for the
# discrimination above to mean anything, which caps the elapsed window under
# 2s, so the runner's own microseconds are never negligible here -- 34 ms of
# it moved this figure 1.2 points. The ceiling is the real invariant: the
# window is at least the 1.4s aged, so 0.7 CPU-seconds cannot read above 50%.
# The exact arithmetic is pinned by the 10s-window test instead, where
# overhead is a rounding error.
assert recovered.nodes[CHAT].cpu_pct is not None, "the window should have recovered"
# The ceiling itself is float arithmetic on a large monotonic base: the
# 1.4s window is `now - (stored - 0.8 - 0.6)`, and when `now` is the same
# coarse-clock tick as `stored` the difference is 1.4 to a few ulps, so
# 0.7 / window can read 50.000000000005 on Windows. Slack, not a point.
assert 20.0 < recovered.nodes[CHAT].cpu_pct <= 50.0 + _FLOAT_SLACK
assert recovered.nodes[CHAT].runq_wait_pct is not None
assert 4.0 < recovered.nodes[CHAT].runq_wait_pct <= 10.0 + _FLOAT_SLACK
recovered = table.scan_rated(baseline, monotonic=SCAN_MONOTONIC)

assert recovered.nodes[CHAT].cpu_pct == pytest.approx(50.0, abs=_FLOAT_SLACK)
assert recovered.nodes[CHAT].runq_wait_pct == pytest.approx(10.0, abs=_FLOAT_SLACK)


def test_a_read_refused_as_too_old_does_replace_the_baseline(tmp_path: Path) -> None:
Expand Down
24 changes: 20 additions & 4 deletions test/test_snapshot_absent_vs_wrong_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from __future__ import annotations

import tarfile
from datetime import datetime, timezone
from pathlib import Path

import pytest
Expand Down Expand Up @@ -98,14 +99,29 @@ def test_a_genuinely_absent_entry_still_skips(self, home, tmp_path, capsys):


class TestTheRollbackDirectoryBelongsToOneRestore:
def test_a_second_allocation_in_the_same_second_gets_its_own_directory(self, home):
"""Two restores inside one UTC second must not share a rollback set."""
def test_a_second_allocation_in_the_same_second_gets_its_own_directory(self, home, monkeypatch):
"""Two restores inside one UTC second must not share a rollback set.

The stamp has one-second resolution, so "the same second" has to be held
rather than hoped for: on a slow runner the two allocations straddle a
second boundary often enough that the second gets a fresh, unsuffixed
name and the collision path under test never runs.
"""
frozen = datetime(2026, 1, 1, 12, 0, 0, tzinfo=timezone.utc)

class _Frozen:
@staticmethod
def now(tz=None):
return frozen

monkeypatch.setattr(snap, "datetime", _Frozen)
first = snap._allocate_rollback_dir(home)
second = snap._allocate_rollback_dir(home)
assert first != second, "the second restore reused the first's rollback directory"
assert first.is_dir() and second.is_dir()
assert second.name.startswith(
first.name
assert first.name == "pre-restore-20260101T120000Z"
assert (
second.name == "pre-restore-20260101T120000Z-2"
), f"the suffixed name should stay recognisable: {second.name}"

def test_the_operator_readable_stem_is_kept(self, home):
Expand Down
Loading