Skip to content
Open
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
163 changes: 151 additions & 12 deletions tests/harness/test_managed_box_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,29 +328,168 @@ def test_retry_cannot_hide_a_disconnected_sample(tmp_path):
assert warmup["status"] == "completed" and len(warmup["box_requests"]) == 1


def _install_entered_markers(root):
return sorted(
path.relative_to(root).as_posix()
for path in (root / "search").glob("*/install-entered")
)


def _watch_install_marker(root, cancel, stop_watcher, *, timeout_seconds=15.0, poll_seconds=0.01):
"""Set cancel only after install-entered appears; never fabricate cancellation."""
deadline = time.monotonic() + timeout_seconds
while True:
if stop_watcher.is_set():
markers = _install_entered_markers(root)
trigger = (
"screen_finished_before_cancel" if markers else "screen_finished_before_marker"
)
return {"trigger": trigger, "markers": markers}
markers = _install_entered_markers(root)
if markers:
cancel.set()
return {"trigger": "install_entered", "markers": markers}
remaining = deadline - time.monotonic()
if remaining <= 0:
return {"trigger": "watcher_timeout", "markers": []}
stop_watcher.wait(min(poll_seconds, remaining))


def _cancellation_diagnostics(root, result, watcher_state, cancel, *, watcher_alive=False):
attempts = []
for index, attempt in enumerate(result.get("attempts", [])):
execution = attempt.get("execution") or {}
assessment = execution.get("assessment") or {}
steps = execution.get("steps") or []
attempts.append({
"index": index,
"path": attempt.get("path"),
"failed_stage": execution.get("failed_stage"),
"steps": [f"{step.get('id')}:{step.get('status')}" for step in steps],
"verdict": assessment.get("verdict"),
"reason": assessment.get("reason"),
"cleanup_verified": execution.get("cleanup_verified"),
})
return {
"watcher": dict(watcher_state),
"watcher_alive": watcher_alive,
"cancel_set": cancel.is_set(),
"markers_on_disk": _install_entered_markers(root),
"status": result.get("status"),
"source": result.get("source"),
"error": result.get("error"),
"cleanup_error": result.get("cleanup_error"),
"attempts": attempts,
"result": result,
}


def test_cancellation_terminates_owned_service(tmp_path):
root, _, candidates, profile, lease = campaign(tmp_path, "cancelled")
cancel = threading.Event()
stop_watcher = threading.Event()
watcher_state = {"trigger": "not_started", "markers": []}

def request_cancel():
deadline = time.monotonic() + 15
while time.monotonic() < deadline and not any((root / "search").glob("*/install-entered")):
if cancel.wait(0.01):
return
cancel.set()
watcher_state.update(_watch_install_marker(root, cancel, stop_watcher))

thread = threading.Thread(target=request_cancel, daemon=True)
thread.start()
try:
result = screen(root, candidates, profile, lease, cancel=cancel)
finally:
cancel.set()
thread.join()
assert result["status"] == "inconclusive", result
assert result["cleanup_verified"] is True
assert len(result["attempts"]) == 2
assert "install did not settle" in result["attempts"][-1]["execution"]["assessment"]["reason"]
assert list((root / "search").glob("*/install-entered"))
stop_watcher.set()
thread.join(timeout=5)
diagnostics = _cancellation_diagnostics(
root, result, watcher_state, cancel, watcher_alive=thread.is_alive(),
)
assert not thread.is_alive(), diagnostics
assert watcher_state["trigger"] == "install_entered", diagnostics
assert cancel.is_set(), diagnostics
assert result["status"] == "inconclusive", diagnostics
assert result["cleanup_verified"] is True, diagnostics
assert len(result["attempts"]) == 2, diagnostics
last_reason = result["attempts"][-1]["execution"]["assessment"]["reason"]
assert "install did not settle" in last_reason, diagnostics
assert list((root / "search").glob("*/install-entered")), diagnostics


def test_cancellation_watcher_never_fabricates_cancel_without_marker(tmp_path):
root = tmp_path / "campaign"
batch = root / "search" / "batch-01"
batch.mkdir(parents=True)

timeout_cancel = threading.Event()
timeout_stop = threading.Event()
timed_out = _watch_install_marker(
root, timeout_cancel, timeout_stop, timeout_seconds=0.02, poll_seconds=0.005,
)
assert timed_out == {"trigger": "watcher_timeout", "markers": []}
assert not timeout_cancel.is_set()

early_cancel = threading.Event()
early_stop = threading.Event()
early_stop.set()
early = _watch_install_marker(root, early_cancel, early_stop, timeout_seconds=1.0)
assert early == {"trigger": "screen_finished_before_marker", "markers": []}
assert not early_cancel.is_set()

marker = batch / "install-entered"
marker.touch()

late_cancel = threading.Event()
late_stop = threading.Event()
late_stop.set()
late = _watch_install_marker(root, late_cancel, late_stop, timeout_seconds=1.0)
assert late == {
"trigger": "screen_finished_before_cancel",
"markers": ["search/batch-01/install-entered"],
}
assert not late_cancel.is_set()

matched_cancel = threading.Event()
matched_stop = threading.Event()
matched = _watch_install_marker(root, matched_cancel, matched_stop, timeout_seconds=1.0)
assert matched == {
"trigger": "install_entered",
"markers": ["search/batch-01/install-entered"],
}
assert matched_cancel.is_set()

synthetic_result = {
"status": "inconclusive",
"source": "box_hot_search",
"cleanup_verified": True,
"attempts": [{
"path": "search/batch-01/parent",
"execution": {
"failed_stage": "service",
"steps": [
{"id": "service", "status": "failed"},
{"id": "quality", "status": "skipped"},
{"id": "profile", "status": "skipped"},
],
"assessment": {"verdict": "INCONCLUSIVE", "reason": "service: timeout"},
"cleanup_verified": True,
},
}],
}
diagnostics = _cancellation_diagnostics(root, synthetic_result, early, early_cancel)
assert diagnostics["watcher"]["trigger"] == "screen_finished_before_marker"
assert diagnostics["cancel_set"] is False
assert diagnostics["markers_on_disk"] == ["search/batch-01/install-entered"]
assert diagnostics["status"] == "inconclusive"
assert diagnostics["source"] == "box_hot_search"
assert diagnostics["attempts"] == [{
"index": 0,
"path": "search/batch-01/parent",
"failed_stage": "service",
"steps": ["service:failed", "quality:skipped", "profile:skipped"],
"verdict": "INCONCLUSIVE",
"reason": "service: timeout",
"cleanup_verified": True,
}]
assert diagnostics["result"] is synthetic_result


def test_unknown_cleanup_blocks_formal_attempt_and_device_reuse(tmp_path, monkeypatch):
Expand Down
Loading