gabriel / muse public
test_core_query_engine.py python
578 lines 22.3 KB
Raw
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor ⚠ breaking 120 days ago
1 """Tests for the generic query engine in muse/core/query_engine.py.
2
3 Also contains regression tests that prove the two dead walkers in
4 ``muse.plugins.code._query`` (``walk_commits`` and ``walk_commits_range``) are
5 fully covered by the live walkers (``walk_commits_bfs`` and
6 ``store.walk_commits_between``) before those dead functions are deleted.
7 """
8
9 import datetime
10 import pathlib
11 import tempfile
12
13 import pytest
14
15 from muse.core.query_engine import QueryMatch, format_matches, walk_history
16 from muse.core.snapshot import compute_commit_id, compute_snapshot_id
17 from muse.core.store import CommitRecord, write_commit, walk_commits_between
18 from muse.plugins.code._query import walk_commits_bfs
19 from muse.core.types import Manifest
20 from muse.core.paths import heads_dir, muse_dir
21
22
23 # ---------------------------------------------------------------------------
24 # Helpers
25 # ---------------------------------------------------------------------------
26
27
28 def _make_repo(tmp_path: pathlib.Path) -> pathlib.Path:
29 """Set up a minimal .muse/ structure for query_engine tests."""
30 muse = muse_dir(tmp_path)
31 muse.mkdir()
32 (muse / "repo.json").write_text('{"repo_id":"test-repo"}')
33 (muse / "HEAD").write_text("ref: refs/heads/main")
34 (muse / "commits").mkdir()
35 (muse / "snapshots").mkdir()
36 (muse / "refs" / "heads").mkdir(parents=True)
37 return tmp_path
38
39
40 def _write_commit(root: pathlib.Path, label: str, parent_id: str | None = None) -> CommitRecord:
41 """Write a content-addressed CommitRecord. *label* is used only in the message."""
42 snap_id = compute_snapshot_id({})
43 committed_at = datetime.datetime.now(datetime.timezone.utc)
44 parent_ids = [parent_id] if parent_id else []
45 commit_id = compute_commit_id(
46 parent_ids=parent_ids,
47 snapshot_id=snap_id,
48 message=f"commit {label}",
49 committed_at_iso=committed_at.isoformat(),
50 author="test-author",
51 )
52 record = CommitRecord(
53 repo_id="test-repo",
54 commit_id=commit_id,
55 branch="main",
56 snapshot_id=snap_id,
57 message=f"commit {label}",
58 committed_at=committed_at,
59 parent_commit_id=parent_id,
60 author="test-author",
61 )
62 write_commit(root, record)
63 return record
64
65
66 # ---------------------------------------------------------------------------
67 # walk_history
68 # ---------------------------------------------------------------------------
69
70
71 class TestWalkHistory:
72 def test_empty_branch_returns_empty(self) -> None:
73 with tempfile.TemporaryDirectory() as tmp:
74 root = _make_repo(pathlib.Path(tmp))
75 results = walk_history(root, "main", lambda c, m, r: [])
76 assert results == []
77
78 def test_single_commit_visited(self) -> None:
79 with tempfile.TemporaryDirectory() as tmp:
80 root = _make_repo(pathlib.Path(tmp))
81 c = _write_commit(root, "aaa111")
82 (heads_dir(root) / "main").write_text(c.commit_id)
83
84 visited: list[str] = []
85
86 def evaluator(commit: CommitRecord, manifest: Manifest, r: pathlib.Path) -> list[QueryMatch]:
87 visited.append(commit.commit_id)
88 return []
89
90 walk_history(root, "main", evaluator, load_manifest=False)
91 assert visited == [c.commit_id]
92
93 def test_chain_walked_newest_first(self) -> None:
94 with tempfile.TemporaryDirectory() as tmp:
95 root = _make_repo(pathlib.Path(tmp))
96 c_aaa = _write_commit(root, "aaa111")
97 c_bbb = _write_commit(root, "bbb222", parent_id=c_aaa.commit_id)
98 (heads_dir(root) / "main").write_text(c_bbb.commit_id)
99
100 visited: list[str] = []
101
102 def evaluator(commit: CommitRecord, manifest: Manifest, r: pathlib.Path) -> list[QueryMatch]:
103 visited.append(commit.commit_id)
104 return []
105
106 walk_history(root, "main", evaluator, load_manifest=False)
107 assert visited == [c_bbb.commit_id, c_aaa.commit_id]
108
109 def test_matches_collected(self) -> None:
110 with tempfile.TemporaryDirectory() as tmp:
111 root = _make_repo(pathlib.Path(tmp))
112 c = _write_commit(root, "ccc333")
113 (heads_dir(root) / "main").write_text(c.commit_id)
114
115 def evaluator(commit: CommitRecord, manifest: Manifest, r: pathlib.Path) -> list[QueryMatch]:
116 return [QueryMatch(
117 commit_id=commit.commit_id,
118 author=commit.author,
119 committed_at=commit.committed_at.isoformat(),
120 branch=commit.branch,
121 detail="test match",
122 extra={},
123 )]
124
125 results = walk_history(root, "main", evaluator, load_manifest=False)
126 assert len(results) == 1
127 assert results[0]["detail"] == "test match"
128
129 def test_max_commits_limits_walk(self) -> None:
130 with tempfile.TemporaryDirectory() as tmp:
131 root = _make_repo(pathlib.Path(tmp))
132 records: list[CommitRecord] = []
133 for i in range(10):
134 parent_id = records[i - 1].commit_id if i > 0 else None
135 records.append(_write_commit(root, f"commit{i:03d}", parent_id=parent_id))
136 (heads_dir(root) / "main").write_text(records[-1].commit_id)
137
138 visited: list[str] = []
139
140 def evaluator(commit: CommitRecord, manifest: Manifest, r: pathlib.Path) -> list[QueryMatch]:
141 visited.append(commit.commit_id)
142 return []
143
144 walk_history(root, "main", evaluator, max_commits=3, load_manifest=False)
145 assert len(visited) == 3
146
147 def test_head_commit_id_override(self) -> None:
148 with tempfile.TemporaryDirectory() as tmp:
149 root = _make_repo(pathlib.Path(tmp))
150 c_aaa = _write_commit(root, "aaa111")
151 c_bbb = _write_commit(root, "bbb222", parent_id=c_aaa.commit_id)
152 # HEAD points to bbb222 but we override to aaa111.
153 (heads_dir(root) / "main").write_text(c_bbb.commit_id)
154
155 visited: list[str] = []
156
157 def evaluator(commit: CommitRecord, manifest: Manifest, r: pathlib.Path) -> list[QueryMatch]:
158 visited.append(commit.commit_id)
159 return []
160
161 walk_history(root, "main", evaluator, head_commit_id=c_aaa.commit_id, load_manifest=False)
162 assert visited == [c_aaa.commit_id]
163
164
165 # ---------------------------------------------------------------------------
166 # format_matches
167 # ---------------------------------------------------------------------------
168
169
170 class TestFormatMatches:
171 def test_empty_returns_no_matches(self) -> None:
172 assert "No matches" in format_matches([])
173
174 def test_single_match_formatted(self) -> None:
175 m = QueryMatch(
176 commit_id="a" * 64,
177 author="gabriel",
178 committed_at="2026-03-18T12:00:00+00:00",
179 branch="main",
180 detail="my_function (added)",
181 extra={},
182 )
183 out = format_matches([m])
184 assert ("a" * 64)[:8] in out
185 assert "gabriel" in out
186 assert "my_function (added)" in out
187
188 def test_agent_id_shown_when_present(self) -> None:
189 m = QueryMatch(
190 commit_id="a" * 64,
191 author="bot",
192 committed_at="2026-03-18T12:00:00+00:00",
193 branch="main",
194 detail="something",
195 extra={},
196 agent_id="claude-v4",
197 )
198 out = format_matches([m])
199 assert "claude-v4" in out
200
201 def test_max_results_truncation_message_updated(self) -> None:
202 """format_matches uses '--limit' in the truncation hint (not '--max')."""
203 matches = [
204 QueryMatch(
205 commit_id=f"commit{i:04d}",
206 author="x",
207 committed_at="2026-01-01T00:00:00+00:00",
208 branch="main",
209 detail=f"match {i}",
210 extra={},
211 )
212 for i in range(10)
213 ]
214 out = format_matches(matches, max_results=5)
215 assert "--limit" in out
216
217 def test_max_results_capped(self) -> None:
218 matches = [
219 QueryMatch(
220 commit_id=f"commit{i:04d}",
221 author="x",
222 committed_at="2026-01-01T00:00:00+00:00",
223 branch="main",
224 detail=f"match {i}",
225 extra={},
226 )
227 for i in range(100)
228 ]
229 out = format_matches(matches, max_results=5)
230 assert "95 more" in out
231
232
233 # ---------------------------------------------------------------------------
234 # Regression tests: dead walkers covered by live walkers
235 #
236 # These tests prove that walk_commits_bfs and store.walk_commits_between
237 # fully cover the use-cases of the dead walk_commits and walk_commits_range
238 # before those functions are deleted. If these tests pass, deletion is safe.
239 # ---------------------------------------------------------------------------
240
241
242 def _make_repo_for_walker(tmp_path: pathlib.Path) -> pathlib.Path:
243 muse = muse_dir(tmp_path)
244 muse.mkdir()
245 (muse / "repo.json").write_text('{"repo_id":"walker-test"}')
246 (muse / "HEAD").write_text("main")
247 (muse / "commits").mkdir()
248 (muse / "snapshots").mkdir()
249 (muse / "refs" / "heads").mkdir(parents=True)
250 return tmp_path
251
252
253 def _commit(
254 root: pathlib.Path,
255 label: str,
256 parent: str | None = None,
257 parent2: str | None = None,
258 ) -> CommitRecord:
259 """Write a content-addressed CommitRecord. *label* is used only in the message."""
260 snap_id = compute_snapshot_id({})
261 committed_at = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc)
262 parent_ids = [p for p in [parent, parent2] if p is not None]
263 commit_id = compute_commit_id(
264 parent_ids=parent_ids,
265 snapshot_id=snap_id,
266 message=f"msg {label}",
267 committed_at_iso=committed_at.isoformat(),
268 author="tester",
269 )
270 rec = CommitRecord(
271 repo_id="walker-test",
272 commit_id=commit_id,
273 branch="main",
274 snapshot_id=snap_id,
275 message=f"msg {label}",
276 committed_at=committed_at,
277 parent_commit_id=parent,
278 parent2_commit_id=parent2,
279 author="tester",
280 )
281 write_commit(root, rec)
282 return rec
283
284
285 class TestWalkHistoryFollowMerges:
286 """Belt-and-suspenders tests for walk_history(follow_merges=True/False)."""
287
288 def test_follow_merges_false_skips_parent2(
289 self, tmp_path: pathlib.Path
290 ) -> None:
291 """follow_merges=False (default) stays on the main parent chain only."""
292 root = _make_repo_for_walker(tmp_path)
293 c_main1 = _commit(root, "main1")
294 c_feat1 = _commit(root, "feat1", parent=c_main1.commit_id)
295 c_merge = _commit(root, "merge_c", parent=c_main1.commit_id, parent2=c_feat1.commit_id)
296 (heads_dir(root) / "main").write_text(c_merge.commit_id)
297
298 visited: list[str] = []
299
300 def ev(c: CommitRecord, m: Manifest, r: pathlib.Path) -> list[QueryMatch]:
301 visited.append(c.commit_id)
302 return []
303
304 walk_history(root, "main", ev, follow_merges=False, load_manifest=False)
305 assert c_feat1.commit_id not in visited
306 assert c_merge.commit_id in visited
307 assert c_main1.commit_id in visited
308
309 def test_follow_merges_true_visits_parent2(
310 self, tmp_path: pathlib.Path
311 ) -> None:
312 """follow_merges=True visits both parents of a merge commit."""
313 root = _make_repo_for_walker(tmp_path)
314 c_base = _commit(root, "base")
315 c_feature = _commit(root, "feature", parent=c_base.commit_id)
316 c_merge = _commit(root, "merge_c", parent=c_base.commit_id, parent2=c_feature.commit_id)
317 (heads_dir(root) / "main").write_text(c_merge.commit_id)
318
319 visited: list[str] = []
320
321 def ev(c: CommitRecord, m: Manifest, r: pathlib.Path) -> list[QueryMatch]:
322 visited.append(c.commit_id)
323 return []
324
325 walk_history(root, "main", ev, follow_merges=True, load_manifest=False)
326 assert set(visited) == {c_merge.commit_id, c_base.commit_id, c_feature.commit_id}
327
328 def test_follow_merges_true_linear_chain(
329 self, tmp_path: pathlib.Path
330 ) -> None:
331 """follow_merges=True on a linear chain behaves identically to False."""
332 root = _make_repo_for_walker(tmp_path)
333 c_a = _commit(root, "a")
334 c_b = _commit(root, "b", parent=c_a.commit_id)
335 c_c = _commit(root, "c", parent=c_b.commit_id)
336 (heads_dir(root) / "main").write_text(c_c.commit_id)
337
338 visited_ff: list[str] = []
339 visited_ft: list[str] = []
340
341 def ev_ff(c: CommitRecord, m: Manifest, r: pathlib.Path) -> list[QueryMatch]:
342 visited_ff.append(c.commit_id)
343 return []
344
345 def ev_ft(c: CommitRecord, m: Manifest, r: pathlib.Path) -> list[QueryMatch]:
346 visited_ft.append(c.commit_id)
347 return []
348
349 walk_history(root, "main", ev_ff, follow_merges=False, load_manifest=False)
350 walk_history(root, "main", ev_ft, follow_merges=True, load_manifest=False)
351 assert set(visited_ff) == set(visited_ft) == {c_a.commit_id, c_b.commit_id, c_c.commit_id}
352
353 def test_follow_merges_since_filter_applies(
354 self, tmp_path: pathlib.Path
355 ) -> None:
356 """since filter still prunes commits even with follow_merges=True."""
357 root = _make_repo_for_walker(tmp_path)
358 # Pin explicit timestamps so since filter is deterministic.
359 t_old = datetime.datetime(2020, 1, 1, tzinfo=datetime.timezone.utc)
360 t_new = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc)
361
362 snap_id = compute_snapshot_id({})
363 cid_old = compute_commit_id(
364 parent_ids=[],
365 snapshot_id=snap_id,
366 message="old",
367 committed_at_iso=t_old.isoformat(),
368 author="tester",
369 )
370 rec_old = CommitRecord(
371 repo_id="walker-test",
372 commit_id=cid_old,
373 branch="main",
374 snapshot_id=snap_id,
375 message="old",
376 committed_at=t_old,
377 author="tester",
378 )
379 write_commit(root, rec_old)
380
381 cid_new = compute_commit_id(
382 parent_ids=[cid_old],
383 snapshot_id=snap_id,
384 message="new",
385 committed_at_iso=t_new.isoformat(),
386 author="tester",
387 )
388 rec_new = CommitRecord(
389 repo_id="walker-test",
390 commit_id=cid_new,
391 branch="main",
392 snapshot_id=snap_id,
393 message="new",
394 committed_at=t_new,
395 parent_commit_id=cid_old,
396 author="tester",
397 )
398 write_commit(root, rec_new)
399 (heads_dir(root) / "main").write_text(cid_new)
400
401 visited: list[str] = []
402
403 def ev(c: CommitRecord, m: Manifest, r: pathlib.Path) -> list[QueryMatch]:
404 visited.append(c.commit_id)
405 return []
406
407 since = datetime.datetime(2025, 1, 1, tzinfo=datetime.timezone.utc)
408 walk_history(root, "main", ev, follow_merges=True, since=since, load_manifest=False)
409 assert cid_new in visited
410 assert cid_old not in visited
411
412
413 def test_follow_merges_true_diamond_dag_no_duplicates(
414 self, tmp_path: pathlib.Path
415 ) -> None:
416 """BFS never visits the same commit twice (diamond DAG case)."""
417 root = _make_repo_for_walker(tmp_path)
418 # Diamond: base ← left ← merge, base ← right ← merge
419 c_base = _commit(root, "base")
420 c_left = _commit(root, "left", parent=c_base.commit_id)
421 c_right = _commit(root, "right", parent=c_base.commit_id)
422 c_merge = _commit(root, "merge_c", parent=c_left.commit_id, parent2=c_right.commit_id)
423 (heads_dir(root) / "main").write_text(c_merge.commit_id)
424
425 visited: list[str] = []
426
427 def ev(c: CommitRecord, m: Manifest, r: pathlib.Path) -> list[QueryMatch]:
428 visited.append(c.commit_id)
429 return []
430
431 walk_history(root, "main", ev, follow_merges=True, load_manifest=False)
432 # Each commit visited exactly once.
433 assert len(visited) == len(set(visited))
434 assert set(visited) == {c_base.commit_id, c_left.commit_id, c_right.commit_id, c_merge.commit_id}
435
436 def test_follow_merges_max_commits_respected(
437 self, tmp_path: pathlib.Path
438 ) -> None:
439 """max_commits caps BFS walk even with follow_merges=True."""
440 root = _make_repo_for_walker(tmp_path)
441 c1 = _commit(root, "c1")
442 c2 = _commit(root, "c2", parent=c1.commit_id)
443 c3 = _commit(root, "c3", parent=c2.commit_id)
444 c4 = _commit(root, "c4", parent=c3.commit_id)
445 (heads_dir(root) / "main").write_text(c4.commit_id)
446
447 visited: list[str] = []
448
449 def ev(c: CommitRecord, m: Manifest, r: pathlib.Path) -> list[QueryMatch]:
450 visited.append(c.commit_id)
451 return []
452
453 walk_history(root, "main", ev, follow_merges=True, max_commits=2, load_manifest=False)
454 assert len(visited) == 2
455
456 def test_follow_merges_evaluator_sees_match(
457 self, tmp_path: pathlib.Path
458 ) -> None:
459 """Matches from parent2 commits are included in results."""
460 root = _make_repo_for_walker(tmp_path)
461 c_base = _commit(root, "base")
462 c_feature = _commit(root, "feature", parent=c_base.commit_id)
463 c_merge = _commit(root, "merge_c", parent=c_base.commit_id, parent2=c_feature.commit_id)
464 (heads_dir(root) / "main").write_text(c_merge.commit_id)
465
466 def ev(c: CommitRecord, m: Manifest, r: pathlib.Path) -> list[QueryMatch]:
467 if c.commit_id == c_feature.commit_id:
468 return [QueryMatch(
469 commit_id=c.commit_id,
470 author=c.author,
471 committed_at=c.committed_at.isoformat(),
472 branch=c.branch,
473 detail="feature found",
474 extra={},
475 )]
476 return []
477
478 results = walk_history(root, "main", ev, follow_merges=True, load_manifest=False)
479 assert len(results) == 1
480 assert results[0]["detail"] == "feature found"
481
482 def test_follow_merges_false_misses_parent2_commit(
483 self, tmp_path: pathlib.Path
484 ) -> None:
485 """With follow_merges=False, parent2 commits are never evaluated."""
486 root = _make_repo_for_walker(tmp_path)
487 c_base = _commit(root, "base")
488 c_feature = _commit(root, "feature", parent=c_base.commit_id)
489 c_merge = _commit(root, "merge_c", parent=c_base.commit_id, parent2=c_feature.commit_id)
490 (heads_dir(root) / "main").write_text(c_merge.commit_id)
491
492 def ev(c: CommitRecord, m: Manifest, r: pathlib.Path) -> list[QueryMatch]:
493 if c.commit_id == c_feature.commit_id:
494 return [QueryMatch(
495 commit_id=c.commit_id,
496 author=c.author,
497 committed_at=c.committed_at.isoformat(),
498 branch=c.branch,
499 detail="feature found",
500 extra={},
501 )]
502 return []
503
504 results = walk_history(root, "main", ev, follow_merges=False, load_manifest=False)
505 assert results == [] # feature commit is never visited
506
507
508 class TestLiveWalkersContracts:
509 """Regression: walk_commits_bfs and walk_commits_between cover deleted walkers.
510
511 These tests lock down the contracts of the surviving walkers, proving
512 the deleted walk_commits and walk_commits_range are fully superseded.
513 """
514
515 def test_walk_commits_bfs_linear_chain(self, tmp_path: pathlib.Path) -> None:
516 """walk_commits_bfs on a linear chain returns all commits, newest first."""
517 root = _make_repo_for_walker(tmp_path)
518 c_aaa = _commit(root, "aaa")
519 c_bbb = _commit(root, "bbb", parent=c_aaa.commit_id)
520 c_ccc = _commit(root, "ccc", parent=c_bbb.commit_id)
521
522 live_commits, truncated = walk_commits_bfs(root, c_ccc.commit_id)
523 live_ids = [c.commit_id for c in live_commits]
524
525 assert truncated is False
526 assert set(live_ids) == {c_aaa.commit_id, c_bbb.commit_id, c_ccc.commit_id}
527
528 def test_walk_commits_bfs_follows_parent2(self, tmp_path: pathlib.Path) -> None:
529 """walk_commits_bfs reaches parent2 branches — supersedes dead linear walker."""
530 root = _make_repo_for_walker(tmp_path)
531 c_base = _commit(root, "base")
532 c_feature = _commit(root, "feature", parent=c_base.commit_id)
533 c_merge = _commit(root, "merge_commit", parent=c_base.commit_id, parent2=c_feature.commit_id)
534
535 live_commits, _ = walk_commits_bfs(root, c_merge.commit_id)
536 live_ids = set(c.commit_id for c in live_commits)
537
538 assert c_feature.commit_id in live_ids
539 assert c_base.commit_id in live_ids
540 assert c_merge.commit_id in live_ids
541
542 def test_walk_commits_between_range(self, tmp_path: pathlib.Path) -> None:
543 """walk_commits_between excludes from_commit_id — supersedes walk_commits_range."""
544 root = _make_repo_for_walker(tmp_path)
545 c1 = _commit(root, "c1")
546 c2 = _commit(root, "c2", parent=c1.commit_id)
547 c3 = _commit(root, "c3", parent=c2.commit_id)
548 c4 = _commit(root, "c4", parent=c3.commit_id)
549
550 result = walk_commits_between(root, to_commit_id=c4.commit_id, from_commit_id=c1.commit_id)
551 ids = [c.commit_id for c in result]
552
553 assert ids == [c4.commit_id, c3.commit_id, c2.commit_id]
554 assert c1.commit_id not in ids
555
556 def test_walk_commits_between_none_from(self, tmp_path: pathlib.Path) -> None:
557 """walk_commits_between with from_commit_id=None returns entire chain."""
558 root = _make_repo_for_walker(tmp_path)
559 c_x1 = _commit(root, "x1")
560 c_x2 = _commit(root, "x2", parent=c_x1.commit_id)
561
562 ids = [c.commit_id for c in walk_commits_between(root, c_x2.commit_id, None)]
563 assert ids == [c_x2.commit_id, c_x1.commit_id]
564
565 def test_walk_commits_bfs_stop_at_excludes_boundary(
566 self, tmp_path: pathlib.Path
567 ) -> None:
568 """walk_commits_bfs stop_at_commit_id excludes the boundary — same contract as walk_commits_between."""
569 root = _make_repo_for_walker(tmp_path)
570 c_p1 = _commit(root, "p1")
571 c_p2 = _commit(root, "p2", parent=c_p1.commit_id)
572 c_p3 = _commit(root, "p3", parent=c_p2.commit_id)
573
574 bfs_commits, _ = walk_commits_bfs(root, c_p3.commit_id, stop_at_commit_id=c_p1.commit_id)
575 bfs_ids = [c.commit_id for c in bfs_commits]
576
577 assert c_p1.commit_id not in bfs_ids
578 assert set(bfs_ids) == {c_p3.commit_id, c_p2.commit_id}
File History 1 commit
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor 120 days ago