From 25901488b85e32405213d66ce2a01178a0012374 Mon Sep 17 00:00:00 2001 From: Andrew Chen <48723787+chuenchen309@users.noreply.github.com> Date: Thu, 26 Nov 2026 12:36:03 +0800 Subject: [PATCH 1/2] fix(workflows): save run state before logging in workflow run execute() appended the workflow_interrupted, workflow_failed and workflow_finished events before saving the matching state. When log.jsonl cannot be written, the run stays persisted as running and workflow resume refuses it. #5492 fixed the same ordering in resume(). Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous) --- src/specify_cli/workflows/engine.py | 6 +-- tests/workflows/test_composition_execution.py | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) 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..7b5ee40fc1 100644 --- a/tests/workflows/test_composition_execution.py +++ b/tests/workflows/test_composition_execution.py @@ -890,6 +890,47 @@ 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 + + @pytest.mark.parametrize("failure", [KeyboardInterrupt, RuntimeError]) def test_leave_failure_preserves_step_exception(tmp_path, monkeypatch, failure): from specify_cli.workflows._execution import Execution From 0516e6aad26538469e24a38d6031a9bb7b20e293 Mon Sep 17 00:00:00 2001 From: chuenchen309 <48723787+chuenchen309@users.noreply.github.com> Date: Sat, 5 Dec 2026 10:02:03 +0800 Subject: [PATCH 2/2] test(workflows): cover interrupt log failure in workflow run Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous) --- tests/workflows/test_composition_execution.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/workflows/test_composition_execution.py b/tests/workflows/test_composition_execution.py index 7b5ee40fc1..30304f2699 100644 --- a/tests/workflows/test_composition_execution.py +++ b/tests/workflows/test_composition_execution.py @@ -931,6 +931,37 @@ def log(self, entry): 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