Skip to content
Draft
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
6 changes: 3 additions & 3 deletions src/inferrsi/harness/execution_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,9 @@ def _run_steps(ctx: _Context, argvs: list[list[str]], deadline: float, cancellat
cancellation=cancellation,
)
runs.append((step, run))
if run["pid"] is None and run["status"] == "timeout":
records.append(_not_started_step(step, "deadline_exhausted"))
stop = "deadline_exhausted"
if run["pid"] is None and run["status"] in ("timeout", "cancelled"):
stop = "cancelled" if run["status"] == "cancelled" else "deadline_exhausted"
records.append(_not_started_step(step, stop))
continue
status = _STEP_STATUS[run["status"]]
exited = status == "exited"
Expand Down
8 changes: 8 additions & 0 deletions src/inferrsi/harness/local_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ def _supervise(
if os.name == "nt":
windows_job = _WindowsJob()
with stdout_path.open("wb") as stdout, stderr_path.open("wb") as stderr:
if cancellation is not None and cancellation.is_set():
run.update(status="cancelled", error="cancellation requested",
cancellation_requested_at=_utc_now(), cancellation_source="caller")
return run
if time.monotonic() >= deadline:
run.update(status="timeout",
error="whole-run deadline exhausted before command start")
return run
process = subprocess.Popen(
argv,
cwd=cwd,
Expand Down
41 changes: 41 additions & 0 deletions tests/test_harness_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import sys
import threading
import time
from datetime import datetime, timedelta, timezone
from pathlib import Path
from types import SimpleNamespace

import pytest

Expand Down Expand Up @@ -219,6 +221,45 @@ def test_h02_2_timeout_cancel_and_cleanup_are_recorded_separately(h: Harness) ->
assert late["steps"][0]["reasons"] == ["deadline_exhausted"]


@pytest.mark.parametrize("cancelled", [True, False], ids=["cancelled", "deadline"])
def test_prelaunch_limits_are_rechecked_after_opening_logs(h, monkeypatch, cancelled):
cancellation = threading.Event()
open_path = Path.open
offset = 0

def open_log(path, *args, **kwargs):
nonlocal offset
stream = open_path(path, *args, **kwargs)
if path.name == "step-0.stderr":
if cancelled:
cancellation.set()
else:
offset = 120
return stream

monkeypatch.setattr(Path, "open", open_log)
monkeypatch.setattr(local_process, "time", SimpleNamespace(
monotonic=lambda: time.monotonic() + offset, sleep=time.sleep,
))
deadline = (datetime.now(timezone.utc) + timedelta(minutes=1)).strftime("%Y-%m-%dT%H:%M:%SZ")
spec = make_spec(h, [step("import time; time.sleep(10)"),
step("print('second')", step_id="second")], deadline=deadline)
record = h.execute(spec, cancellation=cancellation)

schema_check(record)
assert record["started"] is False
assert record["phase"] == "finished"
assert record["process_outcome"] == ("cancelled" if cancelled else "not_started")
assert record["target_binding"]["runtime_bindings_by_step"] == []
assert record["cleanup"]["status"] == "not_needed"
assert record["cancellation"]["requested"] is cancelled
assert record["cancellation"]["source"] == ("caller" if cancelled else None)
reason = "cancelled" if cancelled else "deadline_exhausted"
assert [(s["status"], s["reasons"]) for s in record["steps"]] == [
("not_started", [reason]), ("not_started", [reason]),
]


def test_h02_3_actual_input_mismatch_commits_a_fixed_not_started(h: Harness, tmp_path) -> None:
source = tmp_path / "controller" / "candidate.txt"
source.write_bytes("actual bytes 实际\n".encode("utf-8"))
Expand Down
Loading