|
| 1 | +"""`grapharc go <dir>` on a plan that has already run. |
| 2 | +
|
| 3 | +Bare `go` has always skipped executed plans — `find_unexecuted_plan` passes |
| 4 | +over any record carrying an `executed_run_id`. The explicitly-named-directory |
| 5 | +form did not make the same check, so a second `go <dir>` re-ran the whole graph |
| 6 | +and overwrote the stamp, leaving a `plan.json` that named one run while the |
| 7 | +trace correctly held three. |
| 8 | +
|
| 9 | +Two separate claims are under test here, and the second is the load-bearing |
| 10 | +one. The *record* must be able to name every run that executed the plan. And |
| 11 | +the *decision* must not be spent twice: an approval binds to a proposal |
| 12 | +fingerprint, which does not change between runs, so a silent re-run of a |
| 13 | +`mutating` plan executes on the strength of an earlier human yes. |
| 14 | +
|
| 15 | +The planner is scripted throughout — no model backend, no network. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import json |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +from grapharc.cli.main import main |
| 24 | +from grapharc.cli.plan import _executed_run_ids |
| 25 | + |
| 26 | + |
| 27 | +def _saved_plan(tmp_path, capsys) -> Path: |
| 28 | + """A run directory holding an admitted, unexecuted `plan.json`.""" |
| 29 | + trace = tmp_path / "run" / "trace.jsonl" |
| 30 | + assert main(["plan", "investigate", "--scripted", "--trace", str(trace), "--json"]) == 0 |
| 31 | + capsys.readouterr() # drop the plan document |
| 32 | + return trace.parent |
| 33 | + |
| 34 | + |
| 35 | +def _last_document(text: str) -> dict: |
| 36 | + """The final JSON document in a stream that may carry several.""" |
| 37 | + decoder = json.JSONDecoder() |
| 38 | + documents, index = [], 0 |
| 39 | + while index < len(text): |
| 40 | + if text[index] != "{": |
| 41 | + index += 1 |
| 42 | + continue |
| 43 | + try: |
| 44 | + document, index = decoder.raw_decode(text, index) |
| 45 | + except json.JSONDecodeError: |
| 46 | + index += 1 |
| 47 | + continue |
| 48 | + documents.append(document) |
| 49 | + assert documents, f"no JSON document in: {text[:200]!r}" |
| 50 | + return documents[-1] |
| 51 | + |
| 52 | + |
| 53 | +def _record(run_dir: Path) -> dict: |
| 54 | + return json.loads((run_dir / "plan.json").read_text(encoding="utf-8")) |
| 55 | + |
| 56 | + |
| 57 | +def _runs_in_trace(run_dir: Path) -> list[str]: |
| 58 | + seen: list[str] = [] |
| 59 | + for line in (run_dir / "trace.jsonl").read_text(encoding="utf-8").splitlines(): |
| 60 | + if not line.strip(): |
| 61 | + continue |
| 62 | + run_id = json.loads(line).get("run_id") |
| 63 | + if run_id and run_id not in seen: |
| 64 | + seen.append(run_id) |
| 65 | + return seen |
| 66 | + |
| 67 | + |
| 68 | +# -- the refusal ------------------------------------------------------------ |
| 69 | + |
| 70 | + |
| 71 | +def test_a_second_go_on_an_executed_plan_is_refused(tmp_path, capsys): |
| 72 | + """The bug: this used to exit 0 having silently run the whole graph again.""" |
| 73 | + run_dir = _saved_plan(tmp_path, capsys) |
| 74 | + assert main(["go", str(run_dir), "--json"]) == 0 |
| 75 | + first = _record(run_dir)["executed_run_id"] |
| 76 | + before = _runs_in_trace(run_dir) |
| 77 | + capsys.readouterr() |
| 78 | + |
| 79 | + code = main(["go", str(run_dir), "--json"]) |
| 80 | + |
| 81 | + assert code == 2 |
| 82 | + payload = _last_document(capsys.readouterr().out) |
| 83 | + assert payload["ok"] is False |
| 84 | + assert payload["executed_run_id"] == first |
| 85 | + assert "already been executed" in payload["error"] |
| 86 | + assert "--again" in payload["error"] |
| 87 | + # Refused before anything ran: the trace gained no run from the refusal. |
| 88 | + # (It holds two — the planning run, then the one execution.) |
| 89 | + assert _runs_in_trace(run_dir) == before |
| 90 | + |
| 91 | + |
| 92 | +def test_the_refusal_names_the_run_and_when_it_happened(tmp_path, capsys): |
| 93 | + """A refusal a reader cannot act on is an obstacle, not a gate.""" |
| 94 | + run_dir = _saved_plan(tmp_path, capsys) |
| 95 | + assert main(["go", str(run_dir), "--json"]) == 0 |
| 96 | + capsys.readouterr() |
| 97 | + |
| 98 | + assert main(["go", str(run_dir)]) == 2 |
| 99 | + |
| 100 | + message = capsys.readouterr().err |
| 101 | + assert _record(run_dir)["executed_run_id"] in message |
| 102 | + assert _record(run_dir)["executed_at"] in message |
| 103 | + |
| 104 | + |
| 105 | +def test_again_executes_it_a_second_time(tmp_path, capsys): |
| 106 | + """Explicit re-runs stay possible; only the silent ones stop.""" |
| 107 | + run_dir = _saved_plan(tmp_path, capsys) |
| 108 | + assert main(["go", str(run_dir), "--json"]) == 0 |
| 109 | + first = _record(run_dir)["executed_run_id"] |
| 110 | + capsys.readouterr() |
| 111 | + |
| 112 | + code = main(["go", str(run_dir), "--again", "--json"]) |
| 113 | + |
| 114 | + assert code == 0 |
| 115 | + payload = _last_document(capsys.readouterr().out) |
| 116 | + assert payload["executed"] is True |
| 117 | + assert payload["run_id"] != first |
| 118 | + # The planning run, then both executions. |
| 119 | + assert _runs_in_trace(run_dir)[-2:] == [first, payload["run_id"]] |
| 120 | + |
| 121 | + |
| 122 | +# -- the record ------------------------------------------------------------- |
| 123 | + |
| 124 | + |
| 125 | +def test_the_record_names_every_run_that_executed_the_plan(tmp_path, capsys): |
| 126 | + """Three executions used to leave a plan.json naming one, while the trace |
| 127 | + — the audit trail, and the one that was right — held all three.""" |
| 128 | + run_dir = _saved_plan(tmp_path, capsys) |
| 129 | + assert main(["go", str(run_dir), "--json"]) == 0 |
| 130 | + assert main(["go", str(run_dir), "--again", "--json"]) == 0 |
| 131 | + assert main(["go", str(run_dir), "--again", "--json"]) == 0 |
| 132 | + capsys.readouterr() |
| 133 | + |
| 134 | + record = _record(run_dir) |
| 135 | + assert len(record["executed_run_ids"]) == 3 |
| 136 | + # The record now agrees with the trace, which was always right. The trace |
| 137 | + # also carries the planning run that produced the plan, hence the slice. |
| 138 | + assert record["executed_run_ids"] == _runs_in_trace(run_dir)[-3:] |
| 139 | + # The scalar stays the newest: `find_unexecuted_plan` and the MCP driver |
| 140 | + # read it, and an older reader must keep working. |
| 141 | + assert record["executed_run_id"] == record["executed_run_ids"][-1] |
| 142 | + |
| 143 | + |
| 144 | +def test_a_plan_that_never_executed_carries_no_history(tmp_path, capsys): |
| 145 | + """Absent rather than empty: a reader that tests for the key must not see |
| 146 | + one appear merely because the plan was saved.""" |
| 147 | + run_dir = _saved_plan(tmp_path, capsys) |
| 148 | + |
| 149 | + record = _record(run_dir) |
| 150 | + assert "executed_run_id" not in record |
| 151 | + assert "executed_run_ids" not in record |
| 152 | + assert _executed_run_ids(record) == [] |
| 153 | + |
| 154 | + |
| 155 | +# -- compatibility with records written before the list existed ------------- |
| 156 | + |
| 157 | + |
| 158 | +def test_an_old_record_with_only_the_scalar_reports_its_one_run(): |
| 159 | + """A `plan.json` written before `executed_run_ids` existed must report the |
| 160 | + run it does know about, not report none.""" |
| 161 | + assert _executed_run_ids({"executed_run_id": "abc123"}) == ["abc123"] |
| 162 | + assert _executed_run_ids({}) == [] |
| 163 | + # A malformed list is a record for a human to read, not a place to raise. |
| 164 | + assert _executed_run_ids({"executed_run_ids": "not-a-list"}) == [] |
| 165 | + |
| 166 | + |
| 167 | +def test_an_old_record_is_refused_and_then_accumulates_from_its_scalar(tmp_path, capsys): |
| 168 | + """The upgrade path: a pre-existing scalar-only record still refuses a |
| 169 | + silent re-run, and `--again` grows the list from it rather than losing it.""" |
| 170 | + run_dir = _saved_plan(tmp_path, capsys) |
| 171 | + record = _record(run_dir) |
| 172 | + record["executed_run_id"] = "old-run-id" |
| 173 | + (run_dir / "plan.json").write_text(json.dumps(record, indent=2) + "\n", encoding="utf-8") |
| 174 | + capsys.readouterr() |
| 175 | + |
| 176 | + assert main(["go", str(run_dir), "--json"]) == 2 |
| 177 | + capsys.readouterr() |
| 178 | + assert main(["go", str(run_dir), "--again", "--json"]) == 0 |
| 179 | + capsys.readouterr() |
| 180 | + |
| 181 | + grown = _record(run_dir) |
| 182 | + assert grown["executed_run_ids"][0] == "old-run-id" |
| 183 | + assert len(grown["executed_run_ids"]) == 2 |
| 184 | + |
| 185 | + |
| 186 | +# -- bare `go` is unchanged ------------------------------------------------- |
| 187 | + |
| 188 | + |
| 189 | +def test_bare_go_still_skips_an_executed_plan(tmp_path, capsys, monkeypatch): |
| 190 | + """`find_unexecuted_plan` already passed over executed plans; the new guard |
| 191 | + must not turn that quiet skip into a refusal.""" |
| 192 | + monkeypatch.chdir(tmp_path) |
| 193 | + trace = tmp_path / ".grapharc" / "runs" / "r1" / "trace.jsonl" |
| 194 | + assert main(["plan", "investigate", "--scripted", "--trace", str(trace), "--json"]) == 0 |
| 195 | + capsys.readouterr() |
| 196 | + |
| 197 | + assert main(["go", "--json"]) == 0 |
| 198 | + capsys.readouterr() |
| 199 | + |
| 200 | + # Nothing left unexecuted: the bare form reports that, rather than refusing |
| 201 | + # a directory it was never given. |
| 202 | + assert main(["go", "--json"]) == 1 |
| 203 | + payload = _last_document(capsys.readouterr().out) |
| 204 | + assert "no unexecuted plan" in payload["error"] |
0 commit comments