diff --git a/libs/hackbot-runtime/hackbot_runtime/runtime.py b/libs/hackbot-runtime/hackbot_runtime/runtime.py index 4cecbb629c..a99299b48e 100644 --- a/libs/hackbot-runtime/hackbot_runtime/runtime.py +++ b/libs/hackbot-runtime/hackbot_runtime/runtime.py @@ -172,7 +172,6 @@ def _finish(ctx: HackbotContext, outcome: object) -> int: log.error(msg) payload = _error_payload(ctx, msg) exit_code = 1 - payload["agent_version"] = ctx.config.agent_version try: _publish_log(ctx) @@ -181,8 +180,17 @@ def _finish(ctx: HackbotContext, outcome: object) -> int: try: ctx.publish_changes() - except Exception: + except Exception as exc: log.exception("Failed to publish source changes") + if exit_code == 0: + payload = _error_payload( + ctx, + f"Failed to publish source changes: {type(exc).__name__}: {exc}", + traceback_str=traceback.format_exc(), + ) + exit_code = 1 + + payload["agent_version"] = ctx.config.agent_version # Upload when a signed policy is configured, else write into the local # artifacts dir (so local/compose/direct runs leave it on the host). diff --git a/libs/hackbot-runtime/tests/test_runtime.py b/libs/hackbot-runtime/tests/test_runtime.py index ca1581e2dc..99aceb3d4c 100644 --- a/libs/hackbot-runtime/tests/test_runtime.py +++ b/libs/hackbot-runtime/tests/test_runtime.py @@ -65,6 +65,24 @@ def test_summary_written_for_exception(tmp_path): assert "boom" in summary["error"] +def test_run_fails_when_publishing_changes_raises(tmp_path, monkeypatch): + ctx = _ctx(tmp_path) + + def _raise(self): + raise RuntimeError("git status failed") + + monkeypatch.setattr(HackbotContext, "publish_changes", _raise) + + code = _finish(ctx, HackbotAgentResult(num_turns=1)) + + assert code == 1 + summary = json.loads( + (tmp_path / "artifacts" / "local-test" / "summary.json").read_text() + ) + assert summary["status"] == "error" + assert "git status failed" in summary["error"] + + def test_removed_action_is_absent_from_summary(tmp_path): ctx = _ctx(tmp_path) inaccurate = ctx.actions.record(