Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(pipeline): keep a failing on_error from failing a guarded step (#4079)

Yunus Ozen10a1c44b1697
diff --git a/packages/core/src/repowise/core/pipeline/step_runner.py b/packages/core/src/repowise/core/pipeline/step_runner.py
index 686610bb5..5867c6cf1 100644
--- a/packages/core/src/repowise/core/pipeline/step_runner.py
+++ b/packages/core/src/repowise/core/pipeline/step_runner.py
@@ -72,7 +72,14 @@ async def run_step(ctx: PersistRun, step: Step) -> None:
await step.run(ctx)
except Exception as exc:
if step.on_error is not None:
- step.on_error(ctx)
+ # Bookkeeping about the failure must not replace it: the step is
+ # still reported as skipped and the run carries on.
+ try:
+ step.on_error(ctx)
+ except Exception as bookkeeping_exc:
+ ctx.log(
+ f"[yellow]{step.name}: could not record the failure: {bookkeeping_exc}[/yellow]"
+ )
ctx.skip(step.name, exc, range_scoped=step.range_scoped)
if step.reraise is not None and step.reraise(ctx):
raise
diff --git a/tests/unit/pipeline/test_incremental_failure_isolation.py b/tests/unit/pipeline/test_incremental_failure_isolation.py
index 8c82db6b5..8bee53b6f 100644
--- a/tests/unit/pipeline/test_incremental_failure_isolation.py
+++ b/tests/unit/pipeline/test_incremental_failure_isolation.py
@@ -208,3 +208,37 @@ async def test_vector_cleanup_open_failure_keeps_debt_for_retry(repo, monkeypatc
await _persist_once()
assert deleted == [orphan]
assert load_cleanup_debt(repo)["vectors"] == set()
+
+
+async def test_unwritable_cleanup_debt_still_degrades_the_step(repo, monkeypatch):
+ import repowise.core.persistence.vector_store as vector_store_mod
+ import repowise.core.pipeline.cleanup_debt as cleanup_debt_mod
+
+ cleanup_debt_mod.record_cleanup_debt(repo, "vectors", {"file_page:removed.py"})
+ (repo / ".repowise" / "lancedb").mkdir()
+
+ def _open(*args, **kwargs):
+ raise RuntimeError("lance unavailable")
+
+ def _ledger_fails(*args, **kwargs):
+ raise OSError("ledger is read-only")
+
+ monkeypatch.setattr(vector_store_mod, "LanceDBVectorStore", _open)
+ monkeypatch.setattr(cleanup_debt_mod, "record_cleanup_debt", _ledger_fails)
+
+ degraded: list[str] = []
+ failed_steps: list[str] = []
+ await persist_incremental_index(
+ repo,
+ object(),
+ {},
+ None,
+ None,
+ [],
+ log=lambda _msg: None,
+ degraded=degraded,
+ failed_steps=failed_steps,
+ )
+
+ assert "Tombstone vector-store open" in failed_steps
+ assert any(entry.startswith("Tombstone vector-store open:") for entry in degraded)
diff --git a/tests/unit/pipeline/test_step_runner.py b/tests/unit/pipeline/test_step_runner.py
new file mode 100644
index 000000000..e4e844a6c
--- /dev/null
+++ b/tests/unit/pipeline/test_step_runner.py
@@ -0,0 +1,58 @@
+"""``run_step``: a failing ``on_error`` must not replace the step's own failure."""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.pipeline.step_runner import PersistRun, Step, run_step
+
+
+def _ctx(log: list[str]) -> PersistRun:
+ return PersistRun(log=log.append, degraded=[], failed_steps=[])
+
+
+async def _boom(_ctx: PersistRun) -> None:
+ raise RuntimeError("boom")
+
+
+def _ledger_fails(_ctx: PersistRun) -> None:
+ raise OSError("ledger")
+
+
+async def test_failing_on_error_still_skips_the_step() -> None:
+ log: list[str] = []
+ ctx = _ctx(log)
+ step = Step("Cleanup", _boom, range_scoped=True, on_error=_ledger_fails)
+
+ await run_step(ctx, step)
+
+ assert ctx.degraded == ["Cleanup: boom"]
+ assert ctx.failed_steps == ["Cleanup"]
+ assert any("Cleanup" in line and "ledger" in line for line in log)
+
+
+async def test_reraise_raises_the_step_error_not_the_bookkeeping_one() -> None:
+ ctx = _ctx([])
+ step = Step("Cleanup", _boom, on_error=_ledger_fails, reraise=lambda _ctx: True)
+
+ with pytest.raises(RuntimeError, match="boom"):
+ await run_step(ctx, step)
+
+ assert ctx.degraded == ["Cleanup: boom"]
+
+
+async def test_on_error_runs_once_before_skip() -> None:
+ calls: list[str] = []
+
+ class _Run(PersistRun):
+ def skip(self, step: str, exc: Exception, *, range_scoped: bool = False) -> None:
+ calls.append("skip")
+ super().skip(step, exc, range_scoped=range_scoped)
+
+ ctx = _Run(degraded=[], failed_steps=[])
+ step = Step("Cleanup", _boom, on_error=lambda _ctx: calls.append("on_error"))
+
+ await run_step(ctx, step)
+
+ assert calls == ["on_error", "skip"]
+ assert ctx.degraded == ["Cleanup: boom"]