From f331999b2dcc0d7f9d9eabed99483bcb3e935f12 Mon Sep 17 00:00:00 2001 From: Inference_ <68734681+Inference1@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:13:22 +0800 Subject: [PATCH] fix(harness): recheck execution limits before worker launch Recheck the caller cancellation handle and monotonic deadline after log setup. Commit an unstarted step and preserve the cancellation request when no worker was launched. Agent-assisted-by: Codex (author) Signed-off-by: Inference1 <3617237115@qq.com> --- src/inferrsi/harness/execution_service.py | 6 ++-- src/inferrsi/harness/local_process.py | 8 +++++ tests/test_harness_execute.py | 41 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/inferrsi/harness/execution_service.py b/src/inferrsi/harness/execution_service.py index edc40a3fa..af43862c0 100644 --- a/src/inferrsi/harness/execution_service.py +++ b/src/inferrsi/harness/execution_service.py @@ -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" diff --git a/src/inferrsi/harness/local_process.py b/src/inferrsi/harness/local_process.py index b14f0dec1..52f9d1acb 100644 --- a/src/inferrsi/harness/local_process.py +++ b/src/inferrsi/harness/local_process.py @@ -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, diff --git a/tests/test_harness_execute.py b/tests/test_harness_execute.py index efd054515..70c2052f4 100644 --- a/tests/test_harness_execute.py +++ b/tests/test_harness_execute.py @@ -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 @@ -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"))