diff --git a/src/specify_cli/workflows/engine.py b/src/specify_cli/workflows/engine.py index 2aca2c1a9f..fd23d2c3de 100644 --- a/src/specify_cli/workflows/engine.py +++ b/src/specify_cli/workflows/engine.py @@ -1223,8 +1223,8 @@ def execute( self._execute_steps(definition.steps, context, state, STEP_REGISTRY) except KeyboardInterrupt: state.status = RunStatus.PAUSED - state.append_log({"event": "workflow_interrupted"}) state.save() + state.append_log({"event": "workflow_interrupted"}) return state except Exception as exc: if state._checkpoint_failed: @@ -1232,14 +1232,14 @@ def execute( state.status = RunStatus.FAILED state.error = str(exc) state._recorded_exception = exc - state.append_log({"event": "workflow_failed", "error": str(exc)}) state.save() + state.append_log({"event": "workflow_failed", "error": str(exc)}) raise if state.status == RunStatus.RUNNING: state.status = RunStatus.COMPLETED - state.append_log({"event": "workflow_finished", "status": state.status.value}) state.save() + state.append_log({"event": "workflow_finished", "status": state.status.value}) return state def resume( diff --git a/tests/workflows/test_composition_execution.py b/tests/workflows/test_composition_execution.py index 3c2303f98b..30304f2699 100644 --- a/tests/workflows/test_composition_execution.py +++ b/tests/workflows/test_composition_execution.py @@ -890,6 +890,78 @@ def log(self, entry): assert probe["next"] == 1 +def test_run_log_write_failure_persists_failed_state(tmp_path, monkeypatch, probe): + original = RunState.append_log + broken = True + + def log(self, entry): + if broken: + raise OSError("log failed") + original(self, entry) + + monkeypatch.setattr(RunState, "append_log", log) + root = definition("parent", [{"id": "work", "type": "probe"}]) + with pytest.raises(OSError, match="log failed"): + WorkflowEngine(tmp_path).execute(root, run_id="broken-log") + + persisted = RunState.load("broken-log", tmp_path) + assert persisted.status == RunStatus.FAILED + + broken = False + resumed = WorkflowEngine(tmp_path).resume("broken-log") + assert resumed.status == RunStatus.COMPLETED + + +def test_run_completion_log_failure_keeps_terminal_state( + tmp_path, monkeypatch, probe +): + original = RunState.append_log + + def log(self, entry): + if entry["event"] == "workflow_finished": + raise OSError("log failed") + original(self, entry) + + monkeypatch.setattr(RunState, "append_log", log) + root = definition("parent", [{"id": "work", "type": "probe"}]) + with pytest.raises(OSError, match="log failed"): + WorkflowEngine(tmp_path).execute(root, run_id="finish-log") + + assert RunState.load("finish-log", tmp_path).status == RunStatus.COMPLETED + assert probe["work"] == 1 + + +def test_run_interrupt_log_failure_persists_paused_state( + tmp_path, monkeypatch, probe +): + original = RunState.append_log + broken = True + + def log(self, entry): + if broken and entry["event"] == "workflow_interrupted": + raise OSError("log failed") + original(self, entry) + + monkeypatch.setattr(RunState, "append_log", log) + fired = _interrupt_state_write( + monkeypatch, lambda data: data.get("current_step_id") == "second" + ) + root = definition( + "parent", + [{"id": "first", "type": "probe"}, {"id": "second", "type": "probe"}], + ) + with pytest.raises(OSError, match="log failed"): + WorkflowEngine(tmp_path).execute(root, run_id="interrupt-log") + + assert fired + assert RunState.load("interrupt-log", tmp_path).status == RunStatus.PAUSED + + broken = False + resumed = WorkflowEngine(tmp_path).resume("interrupt-log") + assert resumed.status == RunStatus.COMPLETED + assert probe == {"first": 1, "second": 1} + + @pytest.mark.parametrize("failure", [KeyboardInterrupt, RuntimeError]) def test_leave_failure_preserves_step_exception(tmp_path, monkeypatch, failure): from specify_cli.workflows._execution import Execution