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(cli): warn once on invalid REPOWISE_FULL_RESCORE_INTERVAL_DAYS across workspace fan-out (#1844)

Nehal K30a6845d71e8
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
index e2fd2688e..40011c91f 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
@@ -10,7 +10,6 @@ from __future__ import annotations
import contextlib
import os
-import sys
import time
from dataclasses import dataclass, field
from pathlib import Path
@@ -22,6 +21,7 @@ from repowise.cli.commands._index_outcome import refresh_read_snapshots # noqa:
from repowise.cli.helpers import (
as_commit_id,
console,
+ err_console,
head_commit_ts,
load_config,
load_state,
@@ -1267,22 +1267,27 @@ def _run_full_health_rescore(
# stays deterministic on historical checkouts; override for tests.
_FULL_RESCORE_INTERVAL_DAYS = 7.0
+# The malformed-value warning is global, not per-repo. `_full_rescore_interval_days`
+# is consulted once per repo in a workspace fan-out, `repowise watch` and post-commit
+# hook updates, so a naive warn-in-except would spam the terminal once per repo. Hoist
+# the once-per-invocation guarantee into a module-level guard (#1371).
+_FULL_RESCORE_WARNING_SENT = False
+
def _full_rescore_interval_days() -> float:
+ global _FULL_RESCORE_WARNING_SENT
raw = os.environ.get("REPOWISE_FULL_RESCORE_INTERVAL_DAYS", "").strip()
if raw:
try:
return max(0.0, float(raw))
except ValueError:
- # Say it where the user will actually see it: a silently-ignored
- # value makes the rescore cadence look deliberate when it is
- # actually the default (issue #1370). stderr matches how
- # build_embedder reports the same class of misconfiguration.
- print(
- f"REPOWISE_FULL_RESCORE_INTERVAL_DAYS={raw!r} is not a number; "
- f"using the default {_FULL_RESCORE_INTERVAL_DAYS} days.",
- file=sys.stderr,
- )
+ if not _FULL_RESCORE_WARNING_SENT:
+ _FULL_RESCORE_WARNING_SENT = True
+ err_console.print(
+ "[yellow]Warning:[/yellow] "
+ f"REPOWISE_FULL_RESCORE_INTERVAL_DAYS must be a number "
+ f"(got '{raw}'); using {_FULL_RESCORE_INTERVAL_DAYS:.0f}-day default"
+ )
return _FULL_RESCORE_INTERVAL_DAYS
diff --git a/tests/unit/cli/test_health_rescore_gate.py b/tests/unit/cli/test_health_rescore_gate.py
index 0e2bece7b..f3c72a72c 100644
--- a/tests/unit/cli/test_health_rescore_gate.py
+++ b/tests/unit/cli/test_health_rescore_gate.py
@@ -182,3 +182,71 @@ def test_init_never_stamps_the_cadence_for_a_run_that_scored_nothing(module_name
f"{module_name} resolves the cadence stamp without checking that this "
f"run produced a health report: {line.strip()}"
)
+
+
+def _capture_warnings(monkeypatch) -> list[str]:
+ """Collect what ``_full_rescore_interval_days`` warns, via the shared err_console."""
+ from repowise.cli import helpers
+
+ printed: list[str] = []
+ monkeypatch.setattr(
+ helpers.err_console, "print", lambda *a, **k: printed.append(" ".join(str(x) for x in a))
+ )
+ return printed
+
+
+class TestFullRescoreIntervalWarningOnce:
+ """#1371: a malformed REPOWISE_FULL_RESCORE_INTERVAL_DAYS must warn once per invocation.
+
+ ``_full_rescore_interval_days`` is consulted once per repo in a workspace
+ fan-out, ``repowise watch`` and post-commit hook updates. A naive warn-in-
+ except spams one line per repo; the module-level guard collapses it to a
+ single warning.
+ """
+
+ def test_warns_once_across_many_repos(self, monkeypatch):
+ monkeypatch.setenv("REPOWISE_FULL_RESCORE_INTERVAL_DAYS", "not-a-number")
+ monkeypatch.setattr(
+ "repowise.cli.commands.update_cmd.persistence._FULL_RESCORE_WARNING_SENT", False
+ )
+ printed = _capture_warnings(monkeypatch)
+
+ # Simulate the per-repo fan-out: many independent reads in one invocation.
+ for _ in range(5):
+ assert _full_rescore_interval_days() == 7.0
+
+ (line,) = printed
+ assert "REPOWISE_FULL_RESCORE_INTERVAL_DAYS" in line
+ assert "not-a-number" in line
+ assert len(printed) == 1 # not once per repo
+
+ def test_valid_value_stays_silent(self, monkeypatch):
+ monkeypatch.setenv("REPOWISE_FULL_RESCORE_INTERVAL_DAYS", "7")
+ monkeypatch.setattr(
+ "repowise.cli.commands.update_cmd.persistence._FULL_RESCORE_WARNING_SENT", False
+ )
+ printed = _capture_warnings(monkeypatch)
+
+ assert _full_rescore_interval_days() == 7.0
+ assert printed == []
+
+ def test_default_is_unchanged(self, monkeypatch):
+ monkeypatch.delenv("REPOWISE_FULL_RESCORE_INTERVAL_DAYS", raising=False)
+ monkeypatch.setattr(
+ "repowise.cli.commands.update_cmd.persistence._FULL_RESCORE_WARNING_SENT", False
+ )
+ assert _full_rescore_interval_days() == 7.0
+
+ def test_warning_survives_valid_reads_after_invalid(self, monkeypatch):
+ """A valid read after the invalid one must not re-arm the guard."""
+ monkeypatch.setenv("REPOWISE_FULL_RESCORE_INTERVAL_DAYS", "oops")
+ monkeypatch.setattr(
+ "repowise.cli.commands.update_cmd.persistence._FULL_RESCORE_WARNING_SENT", False
+ )
+ printed = _capture_warnings(monkeypatch)
+
+ assert _full_rescore_interval_days() == 7.0
+ # Valid now: still no re-warning.
+ monkeypatch.setenv("REPOWISE_FULL_RESCORE_INTERVAL_DAYS", "7")
+ assert _full_rescore_interval_days() == 7.0
+ assert len(printed) == 1