Files
dxdesigner-part-replacement/tests/trial_task_center/test_runner.py
T

683 lines
33 KiB
Python

from __future__ import annotations
import json
import os
from pathlib import Path
import sqlite3
import pytest
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.models import ProjectStatus, TaskStatus
import dx_productivity.trial_task_center.runner as runner_module
from dx_productivity.trial_task_center.runner import TrialRunner
from dx_productivity.trial_task_center.store import TaskStore
FIXTURES = Path(__file__).parents[1] / "fixtures" / "trial_task_center"
def _write_profile(name: str, destination: Path, project: Path) -> None:
profile = json.loads((FIXTURES / name).read_text(encoding="utf-8"))
profile["project"]["identity"] = str(project)
destination.write_text(json.dumps(profile), encoding="utf-8")
def _runner_with_selected_projects(tmp_path: Path, count: int = 1) -> tuple[TrialRunner, TaskStore, str, tuple[str, ...]]:
store = TaskStore(tmp_path / "task-center.db")
task = store.create_task("Trial")
store.add_rule(task.task_id, "OLD-1", "NEW-1")
root = tmp_path / "projects"
root.mkdir()
paths = []
for number in range(count):
path = root / f"project-{number + 1}.prj"
path.write_text("project", encoding="utf-8")
paths.append(path)
projects = store.sync_projects(task.task_id, root, paths)
for project in projects:
store.set_project_selected(project.project_id, True)
probe_source = tmp_path / "menu-probe-default.json"
probe_source.write_text("baseline", encoding="utf-8")
return (
TrialRunner(
store,
configure_logging(tmp_path / "logs"),
probe_source_resolver=lambda _task, _project: probe_source,
),
store,
task.task_id,
tuple(project.project_id for project in projects),
)
def _runner_with_probe_source(
tmp_path: Path, count: int = 1
) -> tuple[TrialRunner, TaskStore, str, tuple[str, ...], Path]:
runner, store, task_id, project_ids = _runner_with_selected_projects(tmp_path, count)
probe_source = tmp_path / "menu-probe.json"
probe_source.write_text("baseline", encoding="utf-8")
return (
TrialRunner(
store,
configure_logging(tmp_path / "logs"),
probe_source_resolver=lambda _task, _project: probe_source,
),
store,
task_id,
project_ids,
probe_source,
)
def test_start_requires_disposable_confirmation_and_activates_exact_first_selected_project(tmp_path: Path) -> None:
"""Removing the confirmation or choosing an unselected project must block the trial."""
runner, store, task_id, project_ids = _runner_with_selected_projects(tmp_path, count=2)
store.set_project_selected(project_ids[0], False)
with pytest.raises(TaskCenterError) as raised:
runner.start_task(task_id, disposable_copy_confirmed=False)
assert raised.value.code == "disposable_copy_required"
snapshot = runner.start_task(task_id, disposable_copy_confirmed=True)
assert snapshot.task.status is TaskStatus.RUNNING
assert snapshot.active_project is not None
assert snapshot.active_project.project_id == project_ids[1]
assert snapshot.active_project.status is ProjectStatus.CAPTURING_BEFORE
def test_before_capture_supplies_project_and_matching_rules_to_operator(tmp_path: Path) -> None:
"""A before profile must be captured before native replacement guidance is shown."""
runner, store, task_id, (project_id,) = _runner_with_selected_projects(tmp_path)
project = store.list_projects(task_id)[0]
probe = tmp_path / "before.json"
_write_profile("profile-before.json", probe, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
snapshot = runner.capture_before(task_id, project_id, probe)
assert snapshot.active_project is not None
assert snapshot.active_project.status is ProjectStatus.AWAITING_OPERATOR
assert snapshot.instruction is not None
assert snapshot.instruction.project_path == project.path
assert [(rule.old_part_number, rule.new_part_number, rule.match_count) for rule in snapshot.instruction.rules] == [
("OLD-1", "NEW-1", 1)
]
assert snapshot.evidence_directory.joinpath("before.json").exists()
def test_operator_confirmation_does_not_mark_project_successful(tmp_path: Path) -> None:
"""Changing confirmation into completion would let an assertion bypass machine verification."""
runner, store, task_id, (project_id,) = _runner_with_selected_projects(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
_write_profile("profile-before.json", before, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_id, before)
snapshot = runner.confirm_native_attempt(task_id, project_id)
assert snapshot.task.status is TaskStatus.RUNNING
assert snapshot.active_project is not None
assert snapshot.active_project.status is ProjectStatus.AWAITING_OPERATOR
assert store.get_task(task_id).status is not TaskStatus.COMPLETED
def test_fresh_after_capture_that_passes_completes_project_and_advances_queue(tmp_path: Path) -> None:
"""Skipping comparison or next-project activation would leave verified work unsafe or stranded."""
runner, store, task_id, project_ids = _runner_with_selected_projects(tmp_path, count=2)
first, second = (store.list_projects(task_id)[index] for index in range(2))
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", before, first.path)
_write_profile("profile-after-valid.json", after, first.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_ids[0], before)
runner.confirm_native_attempt(task_id, project_ids[0])
snapshot = runner.capture_after(task_id, project_ids[0], after)
assert store.list_projects(task_id)[0].status is ProjectStatus.COMPLETED
assert snapshot.task.status is TaskStatus.RUNNING
assert snapshot.active_project is not None
assert snapshot.active_project.project_id == second.project_id
assert snapshot.active_project.status is ProjectStatus.CAPTURING_BEFORE
def test_hard_comparison_failure_keeps_project_failed_and_task_failed(tmp_path: Path) -> None:
"""Treating a hard profile mismatch as success would violate the trial safety gate."""
runner, store, task_id, (project_id,) = _runner_with_selected_projects(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", before, project.path)
_write_profile("profile-after-invalid.json", after, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_id, before)
with pytest.raises(TaskCenterError) as raised:
runner.capture_after(task_id, project_id, after)
assert raised.value.code == "comparison_failed"
assert store.list_projects(task_id)[0].status is ProjectStatus.FAILED
assert store.get_task(task_id).status is TaskStatus.FAILED
def test_paused_commit_still_records_a_failed_comparison_as_task_failure(tmp_path: Path) -> None:
"""Leaving a paused task nonfailed after a rejected commit would hide a hard safety failure."""
runner, store, task_id, (project_id,) = _runner_with_selected_projects(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", before, project.path)
_write_profile("profile-after-invalid.json", after, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_id, before)
runner.pause_task(task_id)
with pytest.raises(TaskCenterError, match="comparison_failed"):
runner.capture_after(task_id, project_id, after)
assert store.list_projects(task_id)[0].status is ProjectStatus.FAILED
assert store.get_task(task_id).status is TaskStatus.FAILED
def test_pause_prevents_next_project_then_resume_reactivates_it(tmp_path: Path) -> None:
"""Starting another project while paused would defeat the operator's pause request."""
runner, store, task_id, project_ids = _runner_with_selected_projects(tmp_path, count=2)
first = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", before, first.path)
_write_profile("profile-after-valid.json", after, first.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_ids[0], before)
runner.pause_task(task_id)
paused = runner.capture_after(task_id, project_ids[0], after)
assert paused.task.status is TaskStatus.PAUSED
assert paused.active_project is None
assert store.list_projects(task_id)[1].status is ProjectStatus.PENDING
resumed = runner.resume_task(task_id)
assert resumed.task.status is TaskStatus.RUNNING
assert resumed.active_project is not None
assert resumed.active_project.project_id == project_ids[1]
def test_retry_uses_new_attempt_directory_and_skip_finishes_queue(tmp_path: Path) -> None:
"""Reusing a failed attempt's evidence or failing to finish skipped work loses auditability."""
runner, store, task_id, (project_id,) = _runner_with_selected_projects(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", before, project.path)
_write_profile("profile-after-invalid.json", after, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_id, before)
with pytest.raises(TaskCenterError):
runner.capture_after(task_id, project_id, after)
failed_directory = runner.snapshot(task_id).evidence_directory
retried = runner.retry_project(task_id, project_id)
assert retried.active_project is not None
assert retried.evidence_directory != failed_directory
assert retried.evidence_directory.name == "attempt-2"
completed = runner.skip_project(task_id, project_id)
assert store.list_projects(task_id)[0].status is ProjectStatus.SKIPPED
assert completed.task.status is TaskStatus.COMPLETED
def test_passing_final_project_completes_the_task(tmp_path: Path) -> None:
"""Omitting terminal task completion would strand a verified one-project trial."""
runner, store, task_id, (project_id,) = _runner_with_selected_projects(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", before, project.path)
_write_profile("profile-after-valid.json", after, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_id, before)
completed = runner.capture_after(task_id, project_id, after)
assert completed.task.status is TaskStatus.COMPLETED
assert completed.active_project is None
def test_restart_recovery_uses_store_state_and_records_transition_events(tmp_path: Path) -> None:
"""A restarted process must recover durable work instead of trusting a stale runner instance."""
runner, store, task_id, (project_id,) = _runner_with_selected_projects(tmp_path)
runner.start_task(task_id, disposable_copy_confirmed=True)
restarted = TrialRunner(store, configure_logging(tmp_path / "logs"))
snapshot = restarted.snapshot(task_id)
events = [
json.loads(line)
for line in (tmp_path / "logs" / "tasks" / task_id / "events.jsonl").read_text(encoding="utf-8").splitlines()
]
assert snapshot.task.status is TaskStatus.PAUSED
assert snapshot.active_project is None
assert store.list_projects(task_id)[0].status is ProjectStatus.FAILED
recovery_events = [
event
for event in events
if event["operation"] == "recover_interrupted_work" and event["project_id"] == project_id
]
assert [event["details"]["phase"] for event in recovery_events] == ["before", "after"]
assert [event["state_before"] for event in recovery_events] == [
ProjectStatus.CAPTURING_BEFORE.value,
ProjectStatus.CAPTURING_BEFORE.value,
]
def test_restart_awaiting_operator_uses_persisted_before_stamp_and_instruction(tmp_path: Path) -> None:
"""Dropping an attempt context on restart must fail closed instead of accepting the old probe as after evidence."""
runner, store, task_id, (project_id,), probe_source = _runner_with_probe_source(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
_write_profile("profile-after-valid.json", probe_source, project.path)
_write_profile("profile-after-valid.json", before, project.path)
before_profile = json.loads(before.read_text(encoding="utf-8"))
before_profile["capture"] = "new-before-probe"
before.write_text(json.dumps(before_profile), encoding="utf-8")
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_id, before)
restarted = TrialRunner(
store,
configure_logging(tmp_path / "logs"),
probe_source_resolver=lambda _task, _project: probe_source,
)
recovered = restarted.snapshot(task_id)
assert recovered.instruction is not None
with pytest.raises(TaskCenterError) as raised:
restarted.capture_after(task_id, project_id, before)
assert raised.value.code == "profile_unchanged"
assert store.get_task(task_id).status is TaskStatus.FAILED
def test_restart_without_persisted_before_stamp_fails_closed(tmp_path: Path) -> None:
"""A damaged attempt record cannot safely advance from operator state to verification."""
runner, store, task_id, (project_id,), probe_source = _runner_with_probe_source(tmp_path)
project = store.list_projects(task_id)[0]
after = tmp_path / "after.json"
_write_profile("profile-before.json", probe_source, project.path)
_write_profile("profile-after-valid.json", after, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
store.set_project_status(project_id, ProjectStatus.AWAITING_OPERATOR)
restarted = TrialRunner(
store,
configure_logging(tmp_path / "logs"),
probe_source_resolver=lambda _task, _project: probe_source,
)
with pytest.raises(TaskCenterError) as raised:
restarted.capture_after(task_id, project_id, after)
assert raised.value.code == "attempt_before_missing"
assert store.list_projects(task_id)[0].status is ProjectStatus.FAILED
def test_capture_after_rechecks_persisted_before_content_hash(tmp_path: Path) -> None:
runner, store, task_id, (project_id,), probe_source = _runner_with_probe_source(tmp_path)
project = store.list_projects(task_id)[0]
_write_profile("profile-before.json", probe_source, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
before = json.loads(probe_source.read_text(encoding="utf-8"))
before["capture"] = "fresh-before"
probe_source.write_text(json.dumps(before), encoding="utf-8")
runner.capture_before(task_id, project_id, probe_source)
context = store.current_attempt(project_id)
assert context is not None and context.before_path is not None
altered = json.loads(context.before_path.read_text(encoding="utf-8"))
altered["components"][0]["pins"][0]["x"] = 999
context.before_path.write_text(json.dumps(altered), encoding="utf-8")
_write_profile("profile-after-valid.json", probe_source, project.path)
with pytest.raises(TaskCenterError) as raised:
runner.capture_after(task_id, project_id, probe_source)
assert raised.value.code == "evidence_integrity"
assert store.list_projects(task_id)[0].status is ProjectStatus.FAILED
def test_attempt_freezes_rules_for_instruction_and_comparison(tmp_path: Path) -> None:
"""Comparing with an edited mapping rather than the attempt mapping must reject the UI edit race."""
runner, store, task_id, (project_id,), probe_source = _runner_with_probe_source(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", probe_source, project.path)
_write_profile("profile-before.json", before, project.path)
before_profile = json.loads(before.read_text(encoding="utf-8"))
before_profile["capture"] = "new-before-probe"
before.write_text(json.dumps(before_profile), encoding="utf-8")
_write_profile("profile-after-valid.json", after, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_id, before)
rule = store.list_rules(task_id)[0]
store.update_rule(rule.rule_id, old_part_number="OLD-1", new_part_number="OTHER-1", enabled=True)
completed = runner.capture_after(task_id, project_id, after)
assert completed.task.status is TaskStatus.COMPLETED
def test_start_baseline_rejects_unchanged_before_probe(tmp_path: Path) -> None:
"""Capturing the probe present at Start as a before profile would permit stale evidence."""
runner, store, task_id, (project_id,), probe_source = _runner_with_probe_source(tmp_path)
project = store.list_projects(task_id)[0]
_write_profile("profile-before.json", probe_source, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
with pytest.raises(TaskCenterError) as raised:
runner.capture_before(task_id, project_id, probe_source)
assert raised.value.code == "profile_unchanged"
def test_global_active_project_gate_blocks_second_task(tmp_path: Path) -> None:
"""Selecting one active project per task still permits dangerous concurrent native work."""
first, store, first_task_id, _, probe_source = _runner_with_probe_source(tmp_path)
second_task = store.create_task("Second")
store.add_rule(second_task.task_id, "OLD-1", "NEW-1")
second_path = tmp_path / "second.prj"
second_path.write_text("project", encoding="utf-8")
second_project = store.sync_projects(second_task.task_id, tmp_path, (second_path,))[0]
store.set_project_selected(second_project.project_id, True)
second = TrialRunner(store, configure_logging(tmp_path / "logs"), probe_source_resolver=lambda _task, _project: probe_source)
first.start_task(first_task_id, disposable_copy_confirmed=True)
with pytest.raises(TaskCenterError) as raised:
second.start_task(second_task.task_id, disposable_copy_confirmed=True)
assert raised.value.code == "active_project_exists"
assert store.get_task(second_task.task_id).status is TaskStatus.DRAFT
def test_retry_slot_conflict_leaves_failed_task_and_project_unchanged(tmp_path: Path) -> None:
"""A rejected retry claim must not first persist a runnable task or pending project."""
blocker, store, blocker_task_id, _, probe_source = _runner_with_probe_source(tmp_path)
blocked_task = store.create_task("Blocked retry")
store.add_rule(blocked_task.task_id, "OLD-1", "NEW-1")
path = tmp_path / "blocked-retry.prj"
path.write_text("project", encoding="utf-8")
blocked_project = store.sync_projects(blocked_task.task_id, tmp_path, (path,))[0]
store.set_project_selected(blocked_project.project_id, True)
store.set_task_status(blocked_task.task_id, TaskStatus.RUNNING)
store.set_project_status(blocked_project.project_id, ProjectStatus.CAPTURING_BEFORE)
store.set_project_status(blocked_project.project_id, ProjectStatus.FAILED)
store.set_task_status(blocked_task.task_id, TaskStatus.FAILED)
blocker.start_task(blocker_task_id, disposable_copy_confirmed=True)
events_path = tmp_path / "logs" / "tasks" / blocked_task.task_id / "events.jsonl"
with pytest.raises(TaskCenterError) as raised:
blocker.retry_project(blocked_task.task_id, blocked_project.project_id)
assert raised.value.code == "active_project_exists"
assert store.get_task(blocked_task.task_id).status is TaskStatus.FAILED
assert store.list_projects(blocked_task.task_id)[0].status is ProjectStatus.FAILED
assert not events_path.exists() or "retry started" not in events_path.read_text(encoding="utf-8")
def test_resume_slot_conflict_leaves_paused_task_and_pending_project_unchanged(tmp_path: Path) -> None:
"""A rejected resume claim must not advertise success after another task owns the slot."""
blocker, store, blocker_task_id, _, probe_source = _runner_with_probe_source(tmp_path)
blocked_task = store.create_task("Blocked resume")
store.add_rule(blocked_task.task_id, "OLD-1", "NEW-1")
path = tmp_path / "blocked-resume.prj"
path.write_text("project", encoding="utf-8")
blocked_project = store.sync_projects(blocked_task.task_id, tmp_path, (path,))[0]
store.set_project_selected(blocked_project.project_id, True)
store.set_task_status(blocked_task.task_id, TaskStatus.RUNNING)
store.set_task_status(blocked_task.task_id, TaskStatus.PAUSED)
blocker.start_task(blocker_task_id, disposable_copy_confirmed=True)
events_path = tmp_path / "logs" / "tasks" / blocked_task.task_id / "events.jsonl"
with pytest.raises(TaskCenterError) as raised:
blocker.resume_task(blocked_task.task_id)
assert raised.value.code == "active_project_exists"
assert store.get_task(blocked_task.task_id).status is TaskStatus.PAUSED
assert store.list_projects(blocked_task.task_id)[0].status is ProjectStatus.PENDING
assert not events_path.exists() or "trial resumed" not in events_path.read_text(encoding="utf-8")
def test_resume_completes_terminal_task_while_another_task_owns_the_active_slot(tmp_path: Path) -> None:
"""A finished queue needs no native-work slot, so resuming it must be allowed to complete."""
blocker, store, blocker_task_id, _, _ = _runner_with_probe_source(tmp_path)
completed_task = store.create_task("Completed queue")
path = tmp_path / "completed-queue.prj"
path.write_text("project", encoding="utf-8")
completed_project = store.sync_projects(completed_task.task_id, tmp_path, (path,))[0]
store.set_project_selected(completed_project.project_id, True)
store.set_task_status(completed_task.task_id, TaskStatus.RUNNING)
for status in (
ProjectStatus.CAPTURING_BEFORE,
ProjectStatus.AWAITING_OPERATOR,
ProjectStatus.CAPTURING_AFTER,
ProjectStatus.VERIFYING,
ProjectStatus.COMPLETED,
):
store.set_project_status(completed_project.project_id, status)
store.set_task_status(completed_task.task_id, TaskStatus.PAUSED)
blocker.start_task(blocker_task_id, disposable_copy_confirmed=True)
resumed = blocker.resume_task(completed_task.task_id)
assert resumed.task.status is TaskStatus.COMPLETED
assert resumed.active_project is None
def test_v1_migration_recovery_emits_before_and_after_events(tmp_path: Path) -> None:
"""Fail-closed v1 migration must retain enough durable context for recovery audit events."""
database = tmp_path / "task-center.db"
first = TaskStore(database)
task = first.create_task("Trial")
path = tmp_path / "one.prj"
path.touch()
project = first.sync_projects(task.task_id, tmp_path, (path,))[0]
first.set_task_status(task.task_id, TaskStatus.RUNNING)
first.set_project_status(project.project_id, ProjectStatus.CAPTURING_BEFORE)
first.close()
with sqlite3.connect(database) as connection:
connection.execute("DROP TABLE attempts")
connection.execute("UPDATE metadata SET value = '1' WHERE key = 'schema_version'")
migrated = TaskStore(database)
TrialRunner(migrated, configure_logging(tmp_path / "logs"))
events = [
json.loads(line)
for line in (tmp_path / "logs" / "tasks" / task.task_id / "events.jsonl").read_text(encoding="utf-8").splitlines()
]
project_events = [
event for event in events if event["operation"] == "recover_v1_attemptless_work" and event["project_id"] == project.project_id
]
task_events = [
event for event in events if event["operation"] == "recover_v1_attemptless_work" and event["project_id"] is None
]
assert [event["details"]["phase"] for event in project_events] == ["before", "after"]
assert [event["state_before"] for event in project_events] == [ProjectStatus.CAPTURING_BEFORE.value] * 2
assert [event["state_after"] for event in project_events] == [ProjectStatus.FAILED.value] * 2
assert [event["details"]["phase"] for event in task_events] == ["before", "after"]
assert [event["state_before"] for event in task_events] == [TaskStatus.RUNNING.value] * 2
assert [event["state_after"] for event in task_events] == [TaskStatus.PAUSED.value] * 2
@pytest.mark.parametrize("failure_point", ("append_after_write", "acknowledge_before_delete"))
def test_v1_migration_recovery_events_are_idempotent_across_restart(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, failure_point: str
) -> None:
"""Restarting after a partial recovery must not duplicate an event that is already durable."""
database = tmp_path / "task-center.db"
first = TaskStore(database)
task = first.create_task("Trial")
path = tmp_path / "one.prj"
path.touch()
project = first.sync_projects(task.task_id, tmp_path, (path,))[0]
first.set_task_status(task.task_id, TaskStatus.RUNNING)
first.set_project_status(project.project_id, ProjectStatus.CAPTURING_BEFORE)
first.close()
with sqlite3.connect(database) as connection:
connection.execute("DROP TABLE attempts")
connection.execute("UPDATE metadata SET value = '1' WHERE key = 'schema_version'")
migrated = TaskStore(database)
logging_context = configure_logging(tmp_path / "logs")
original_append = runner_module.append_task_event
original_acknowledge = TaskStore.acknowledge_migration_recoveries
append_count = 0
if failure_point == "append_after_write":
def append_then_fail(*args: object, **kwargs: object) -> dict[str, object]:
nonlocal append_count
event = original_append(*args, **kwargs)
append_count += 1
if append_count == 2:
raise TaskCenterError("event_write_failed", "simulated crash after durable append")
return event
monkeypatch.setattr(runner_module, "append_task_event", append_then_fail)
else:
def fail_acknowledge(_store: TaskStore, _project_ids: object) -> None:
raise TaskCenterError("recovery_ack_failed", "simulated crash before recovery acknowledgement")
monkeypatch.setattr(TaskStore, "acknowledge_migration_recoveries", fail_acknowledge)
with pytest.raises(TaskCenterError):
TrialRunner(migrated, logging_context)
migrated.close()
monkeypatch.setattr(runner_module, "append_task_event", original_append)
monkeypatch.setattr(TaskStore, "acknowledge_migration_recoveries", original_acknowledge)
restarted_store = TaskStore(database)
TrialRunner(restarted_store, configure_logging(tmp_path / "logs"))
events = [
json.loads(line)
for line in (tmp_path / "logs" / "tasks" / task.task_id / "events.jsonl").read_text(encoding="utf-8").splitlines()
]
recovery_events = [event for event in events if event["operation"] == "recover_v1_attemptless_work"]
assert len(recovery_events) == 4
assert len({event["details"]["recovery_event_id"] for event in recovery_events}) == 4
assert restarted_store.pending_migration_recoveries() == ()
def test_completed_project_event_failure_reconciles_task_without_illegal_failure(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""An event-write failure after completion must not attempt an illegal completed-to-failed transition."""
runner, store, task_id, (project_id,), probe_source = _runner_with_probe_source(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", probe_source, project.path)
_write_profile("profile-before.json", before, project.path)
os.utime(probe_source, ns=(1_700_000_000_000_000_000, 1_700_000_000_000_000_000))
os.utime(before, ns=(1_700_000_001_000_000_000, 1_700_000_001_000_000_000))
_write_profile("profile-after-valid.json", after, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_id, before)
original_append = runner_module.append_task_event
def fail_terminal_event(*args: object, **kwargs: object) -> dict[str, object]:
if kwargs.get("operation") == "compare_profiles" and kwargs.get("details") == {"phase": "after"}:
raise TaskCenterError("event_write_failed", "cannot append task event")
return original_append(*args, **kwargs)
monkeypatch.setattr(runner_module, "append_task_event", fail_terminal_event)
with pytest.raises(TaskCenterError, match="event_write_failed"):
runner.capture_after(task_id, project_id, after)
assert store.list_projects(task_id)[0].status is ProjectStatus.COMPLETED
assert store.get_task(task_id).status is TaskStatus.COMPLETED
def test_restart_reconciles_completed_project_when_task_completion_was_interrupted(tmp_path: Path) -> None:
"""A crash after project completion must not leave a restartable trial permanently running."""
runner, store, task_id, (project_id,), _ = _runner_with_probe_source(tmp_path)
runner.start_task(task_id, disposable_copy_confirmed=True)
store.set_project_status(project_id, ProjectStatus.FAILED)
store.set_project_status(project_id, ProjectStatus.PENDING)
store.set_project_status(project_id, ProjectStatus.CAPTURING_BEFORE)
store.set_project_status(project_id, ProjectStatus.AWAITING_OPERATOR)
store.set_project_status(project_id, ProjectStatus.CAPTURING_AFTER)
store.set_project_status(project_id, ProjectStatus.VERIFYING)
store.set_project_status(project_id, ProjectStatus.COMPLETED)
restarted = TrialRunner(store, configure_logging(tmp_path / "logs"))
assert restarted.snapshot(task_id).task.status is TaskStatus.COMPLETED
def test_retry_uses_blank_current_instruction_without_mutating_prior_attempt_evidence(tmp_path: Path) -> None:
"""Reusing a previous attempt instruction can direct the operator with stale match evidence."""
runner, store, task_id, (project_id,), probe_source = _runner_with_probe_source(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", probe_source, project.path)
_write_profile("profile-before.json", before, project.path)
before_profile = json.loads(before.read_text(encoding="utf-8"))
before_profile["capture"] = "new-before-probe"
before.write_text(json.dumps(before_profile), encoding="utf-8")
_write_profile("profile-after-invalid.json", after, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
first_attempt = runner.capture_before(task_id, project_id, before)
original_before = (first_attempt.evidence_directory / "before.json").read_bytes()
with pytest.raises(TaskCenterError):
runner.capture_after(task_id, project_id, after)
retried = runner.retry_project(task_id, project_id)
assert retried.instruction is None
assert (first_attempt.evidence_directory / "before.json").read_bytes() == original_before
def test_before_capture_rejects_duplicate_logical_identity(tmp_path: Path) -> None:
"""Ignoring malformed components before operator action creates unverified replacement guidance."""
runner, store, task_id, (project_id,), probe_source = _runner_with_probe_source(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
_write_profile("profile-before.json", probe_source, project.path)
profile = json.loads((FIXTURES / "profile-before.json").read_text(encoding="utf-8"))
profile["project"]["identity"] = str(project.path)
profile["components"].append(dict(profile["components"][0]))
before.write_text(json.dumps(profile), encoding="utf-8")
runner.start_task(task_id, disposable_copy_confirmed=True)
with pytest.raises(TaskCenterError) as raised:
runner.capture_before(task_id, project_id, before)
assert raised.value.code == "before_profile_invalid"
assert store.list_projects(task_id)[0].status is ProjectStatus.FAILED
def test_failed_project_can_be_deselected_and_reconciles_task(tmp_path: Path) -> None:
"""A failed deselected project must not keep an otherwise empty queue in failed state forever."""
runner, store, task_id, (project_id,), probe_source = _runner_with_probe_source(tmp_path)
project = store.list_projects(task_id)[0]
before = tmp_path / "before.json"
after = tmp_path / "after.json"
_write_profile("profile-before.json", probe_source, project.path)
_write_profile("profile-before.json", before, project.path)
before_profile = json.loads(before.read_text(encoding="utf-8"))
before_profile["capture"] = "before-evidence"
before.write_text(json.dumps(before_profile), encoding="utf-8")
_write_profile("profile-after-invalid.json", after, project.path)
runner.start_task(task_id, disposable_copy_confirmed=True)
runner.capture_before(task_id, project_id, before)
with pytest.raises(TaskCenterError):
runner.capture_after(task_id, project_id, after)
snapshot = runner.set_project_selected(task_id, project_id, False)
assert not store.list_projects(task_id)[0].selected
assert snapshot.task.status is TaskStatus.PAUSED