@@ -30,6 +30,17 @@ def discovery():
3030 "index" : {"built_repository_state" : {"commit" : REVISION }}}
3131
3232
33+ def commit_revision (source ):
34+ subprocess .run (
35+ ["git" , "-C" , str (source ), "-c" , "user.name=Synthetic Fixture" ,
36+ "-c" , "user.email=fixture@example.invalid" , "-c" , "commit.gpgsign=false" ,
37+ "commit" , "--allow-empty" , "-qm" , "synthetic revision" ], check = True ,
38+ )
39+ return subprocess .check_output (
40+ ["git" , "-C" , str (source ), "rev-parse" , "HEAD" ], text = True ,
41+ ).strip ()
42+
43+
3344@pytest .fixture
3445def upstream (tmp_path , monkeypatch ):
3546 home = tmp_path / "home"
@@ -42,7 +53,8 @@ def upstream(tmp_path, monkeypatch):
4253 (root / ".git" ).mkdir ()
4354 monkeypatch .chdir (root )
4455 source = tmp_path / "upstream"
45- (source / ".git" ).mkdir (parents = True )
56+ subprocess .run (["git" , "init" , "-q" , str (source )], check = True )
57+ monkeypatch .setattr (sys .modules [__name__ ], "REVISION" , commit_revision (source ))
4658 (source / "knowledge/ops" ).mkdir (parents = True )
4759 card = source / "knowledge/ops/card.md"
4860 card .write_text ("\n " .join (f"synthetic line { i } " for i in range (401 )))
@@ -160,10 +172,12 @@ def test_read_binds_all_pages_to_search_digest(upstream):
160172 hit = json .loads (result .stdout )["external" ][0 ]["items" ][0 ]
161173 opened = cli (* shlex .split (hit ["open" ])[1 :])
162174 body = json .loads (opened .stdout )
175+ assert body ["revision" ] == REVISION
163176 assert body ["card_sha256" ] == hit ["card_sha256" ]
164177 assert body ["has_more" ]
165178 page = cli (* shlex .split (hit ["open" ])[1 :], "--page" , "2" )
166179 assert json .loads (page .stdout )["card_sha256" ] == hit ["card_sha256" ]
180+ assert json .loads (page .stdout )["revision" ] == REVISION
167181 card .write_text ("changed" )
168182 changed = cli (* shlex .split (hit ["open" ])[1 :], "--page" , "2" )
169183 assert changed .returncode != 0
@@ -181,6 +195,63 @@ def test_read_refuses_path_escape(upstream):
181195 assert "not knowledge" not in result .stdout + result .stderr
182196
183197
198+ @pytest .mark .parametrize ("page" , [1 , 2 ])
199+ def test_read_rejects_revision_drift_with_unchanged_card (upstream , page ):
200+ source , script , card = upstream
201+ script .write_text (
202+ "import json, sys\n "
203+ f"print(json.dumps({ discovery ()!r} if sys.argv[1] == 'discover' "
204+ f"else { search_payload (card )!r} ))\n "
205+ )
206+ found = json .loads (cli ("find" , "synthetic" , "--json" ).stdout )["external" ][0 ]
207+ hit = found ["items" ][0 ]
208+ assert f"--revision { found ['revision' ]} " in hit ["open" ]
209+ changed_revision = commit_revision (source )
210+ assert changed_revision != found ["revision" ]
211+ result = cli (* shlex .split (hit ["open" ])[1 :], "--page" , str (page ))
212+ assert result .returncode != 0
213+ assert "revision changed since the search" in result .stderr
214+ assert "synthetic line" not in result .stdout + result .stderr
215+ current = project .read (hit ["locator" ], page = page )
216+ assert current ["revision" ] == changed_revision
217+ assert current ["card_sha256" ] == hit ["card_sha256" ]
218+
219+
220+ def test_read_rejects_revision_change_during_read (upstream , monkeypatch , capsys ):
221+ from inferrsi .apps .cannbot import read_worker
222+
223+ source , _ , _ = upstream
224+ original = read_worker ._card_bytes
225+
226+ def changing_card (root , locator ):
227+ raw = original (root , locator )
228+ commit_revision (source )
229+ return raw
230+
231+ monkeypatch .setattr (read_worker , "_card_bytes" , changing_card )
232+ with pytest .raises (SystemExit ):
233+ read_worker .read (source , {"locator" : "ops/card" , "expected_revision" : REVISION })
234+ result = json .loads (capsys .readouterr ().out )
235+ assert result ["state" ] == "version_mismatch"
236+ assert "text" not in result
237+
238+
239+ def test_read_uses_approved_checkout_not_inherited_git_directory (upstream , monkeypatch ):
240+ source , _ , _ = upstream
241+ monkeypatch .setenv ("GIT_DIR" , str (source / "nonexistent-git-directory" ))
242+ assert project .read ("ops/card" , expected_revision = REVISION )["revision" ] == REVISION
243+
244+
245+ @pytest .mark .parametrize ("revision" , ["" , "HEAD" , "abc" , "A" * 40 , "a" * 39 , 42 ])
246+ def test_read_rejects_invalid_expected_revision_before_worker (monkeypatch , revision ):
247+ def unexpected (* args , ** kwargs ):
248+ pytest .fail ("invalid revision started a worker" )
249+
250+ monkeypatch .setattr (project , "_run" , unexpected )
251+ with pytest .raises (project .ChannelFailure , match = "expected_revision" ):
252+ project .read ("ops/card" , expected_revision = revision )
253+
254+
184255def test_native_fingerprint_and_raw_byte_digest_are_distinct (upstream ):
185256 _ , script , card = upstream
186257 native = script .parent / "retrieval/index.py"
0 commit comments