|
| 1 | +"""FORCE_GRAPH_REBUILD must actually bypass the stale flag. |
| 2 | +
|
| 3 | +The variable was documented in five files -- CLAUDE.md, ADR-002, the deployment |
| 4 | +guide, RUNBOOK_STALE_GRAPH.md and the troubleshooting guide -- and read by no |
| 5 | +code at all. RUNBOOK Option B instructs an operator to `export |
| 6 | +FORCE_GRAPH_REBUILD=1` while recovering from a stale-graph incident, and that |
| 7 | +command did nothing. A wrong default is confusing; a recovery procedure that |
| 8 | +silently fails is worse, because it is consulted precisely when something has |
| 9 | +already gone wrong. |
| 10 | +
|
| 11 | +The scenario ADR-002 names is the one that matters here, and it is the case the |
| 12 | +stale flag cannot cover: if `call_graph_stale` is never set -- because the node |
| 13 | +that sets it crashed -- the guard cannot fire, and Step 5 reviews an outdated |
| 14 | +snapshot in silence. There is no in-state signal for the operator to correct, so |
| 15 | +the override has to come from outside the state. Every test below therefore |
| 16 | +leaves `call_graph_stale` unset and supplies a usable cached snapshot: without |
| 17 | +the override, the cache is what the function returns. |
| 18 | +
|
| 19 | +Each assertion is paired with its opposite. "Forced rebuild returns fresh data" |
| 20 | +proves nothing on its own, because a function that always rebuilds satisfies it |
| 21 | +too -- so the unset and "0" cases assert that the cache is still honoured. |
| 22 | +""" |
| 23 | + |
| 24 | +import sys |
| 25 | +from pathlib import Path |
| 26 | +from unittest.mock import patch |
| 27 | + |
| 28 | +import pytest |
| 29 | + |
| 30 | +_PROJECT_ROOT = Path(__file__).resolve().parent.parent |
| 31 | +if str(_PROJECT_ROOT) not in sys.path: |
| 32 | + sys.path.insert(0, str(_PROJECT_ROOT)) |
| 33 | + |
| 34 | +from langgraph_engine.sdlc_pipeline.call_graph_analyzer import refresh_call_graph_if_stale # noqa: E402 |
| 35 | + |
| 36 | +CACHED = {"nodes": ["cached_node"], "call_graph_available": True} |
| 37 | +FRESH = {"nodes": ["fresh_node"], "call_graph_available": True} |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture() |
| 41 | +def not_stale_state(): |
| 42 | + """State with a usable cache and no stale flag -- ADR-002's crash scenario.""" |
| 43 | + return {"step4_pre_change_graph": dict(CACHED)} |
| 44 | + |
| 45 | + |
| 46 | +def _run(state, tmp_path): |
| 47 | + """Call the guard with snapshot_call_graph stubbed, returning (result, mock).""" |
| 48 | + with patch("langgraph_engine.sdlc_pipeline.call_graph_analyzer.snapshot_call_graph") as mock_snap: |
| 49 | + mock_snap.return_value = dict(FRESH) |
| 50 | + result = refresh_call_graph_if_stale(state, str(tmp_path)) |
| 51 | + return result, mock_snap |
| 52 | + |
| 53 | + |
| 54 | +class TestTheOverrideBypassesTheStaleFlag: |
| 55 | + """FORCE_GRAPH_REBUILD=1 rebuilds even when nothing marked the graph stale.""" |
| 56 | + |
| 57 | + def test_forced_rebuild_ignores_a_usable_cache(self, monkeypatch, not_stale_state, tmp_path): |
| 58 | + monkeypatch.setenv("FORCE_GRAPH_REBUILD", "1") |
| 59 | + result, mock_snap = _run(not_stale_state, tmp_path) |
| 60 | + mock_snap.assert_called_once_with(str(tmp_path)) |
| 61 | + assert result["nodes"] == ["fresh_node"], "the cache was returned despite the override" |
| 62 | + |
| 63 | + |
| 64 | +class TestTheOverrideIsOffByDefault: |
| 65 | + """The half that fails if the rebuild became unconditional.""" |
| 66 | + |
| 67 | + def test_unset_returns_the_cached_snapshot(self, monkeypatch, not_stale_state, tmp_path): |
| 68 | + monkeypatch.delenv("FORCE_GRAPH_REBUILD", raising=False) |
| 69 | + result, mock_snap = _run(not_stale_state, tmp_path) |
| 70 | + mock_snap.assert_not_called() |
| 71 | + assert result["nodes"] == ["cached_node"] |
| 72 | + |
| 73 | + def test_zero_returns_the_cached_snapshot(self, monkeypatch, not_stale_state, tmp_path): |
| 74 | + """Documented default is 0, so 0 must behave exactly like unset.""" |
| 75 | + monkeypatch.setenv("FORCE_GRAPH_REBUILD", "0") |
| 76 | + result, mock_snap = _run(not_stale_state, tmp_path) |
| 77 | + mock_snap.assert_not_called() |
| 78 | + assert result["nodes"] == ["cached_node"] |
| 79 | + |
| 80 | + def test_only_the_literal_1_enables_it(self, monkeypatch, not_stale_state, tmp_path): |
| 81 | + """Matches the ENABLE_* convention used across the engine: == "1", not truthiness. |
| 82 | +
|
| 83 | + "true" is the value an operator is most likely to reach for by habit, so |
| 84 | + it is worth pinning that it does NOT enable the override rather than |
| 85 | + leaving the reader to infer it from the comparison. |
| 86 | + """ |
| 87 | + monkeypatch.setenv("FORCE_GRAPH_REBUILD", "true") |
| 88 | + result, mock_snap = _run(not_stale_state, tmp_path) |
| 89 | + mock_snap.assert_not_called() |
| 90 | + assert result["nodes"] == ["cached_node"] |
| 91 | + |
| 92 | + |
| 93 | +class TestTheOverrideDoesNotDisableTheStaleFlag: |
| 94 | + """Setting it to 0 must not switch off ordinary stale-based rebuilding. |
| 95 | +
|
| 96 | + RUNBOOK Option A used to read as though `FORCE_GRAPH_REBUILD=0` turned that |
| 97 | + off and unsetting it turned it back on. It never did, and this pins that the |
| 98 | + two paths are independent so the corrected wording cannot silently drift |
| 99 | + back. |
| 100 | + """ |
| 101 | + |
| 102 | + def test_stale_still_rebuilds_when_the_override_is_zero(self, monkeypatch, tmp_path): |
| 103 | + monkeypatch.setenv("FORCE_GRAPH_REBUILD", "0") |
| 104 | + state = {"call_graph_stale": True, "step4_pre_change_graph": dict(CACHED)} |
| 105 | + result, mock_snap = _run(state, tmp_path) |
| 106 | + mock_snap.assert_called_once_with(str(tmp_path)) |
| 107 | + assert result["nodes"] == ["fresh_node"] |
0 commit comments