-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_search_index.py
More file actions
518 lines (477 loc) · 20.3 KB
/
Copy pathtest_search_index.py
File metadata and controls
518 lines (477 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""Tests for utils/search_index.py (FTS search index)."""
from __future__ import annotations
import sqlite3
from contextlib import closing, contextmanager
from datetime import UTC, datetime, timedelta
from pathlib import Path
from unittest.mock import patch
import pytest
from api.search import _resolve_search_results, _search_via_index
from tests.conftest import index_patches as _index_patches, write_session as _write_session
from utils.exclusion_rules import load_rules
from utils.search_index import (
build_search_index,
index_is_usable,
index_search_enabled,
query_index_hits,
reset_background_for_tests,
resolve_search_since_ms,
start_search_index_background,
timestamp_to_ms,
tool_result_searchable_text,
)
@pytest.fixture
def indexed_tree(tmp_path, monkeypatch):
"""Temp projects dir + isolated search index cache."""
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
project = projects / "demo-proj"
session_path = project / "session_alpha.jsonl"
_write_session(
session_path,
[
{
"type": "user",
"timestamp": "2026-05-19T10:00:00Z",
"message": {"content": [{"type": "text", "text": "find indexed-unique-sentinel"}]},
},
{
"type": "assistant",
"timestamp": "2026-05-19T10:00:01Z",
"message": {
"model": "claude-test",
"content": [{"type": "text", "text": "acknowledged"}],
},
},
],
)
monkeypatch.delenv("CLAUDE_CODE_CHAT_BROWSER_NO_SEARCH_INDEX", raising=False)
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
reset_background_for_tests()
patches = _index_patches(cache_root)
with patches[0]:
built = build_search_index(str(projects), [], force=True)
assert built is True
pointer = cache_root / "search_index.active"
assert pointer.is_file()
index_name = pointer.read_text(encoding="utf-8").strip()
index_path = cache_root / index_name
assert index_path.is_file()
yield {
"projects": str(projects),
"cache_root": cache_root,
"index_path": index_path,
"term": "indexed-unique-sentinel",
}
class TestSearchIndexBuild:
def test_schema_tables_exist(self, indexed_tree):
with closing(sqlite3.connect(indexed_tree["index_path"])) as conn:
tables = {
row[0]
for row in conn.execute(
"SELECT name FROM sqlite_master WHERE type IN ('table', 'virtual table')"
)
}
assert "index_meta" in tables
assert "sessions" in tables
assert "messages_fts" in tables
def test_index_is_usable_after_build(self, indexed_tree):
patches = _index_patches(indexed_tree["cache_root"])
with patches[0]:
assert index_is_usable(indexed_tree["projects"], []) is True
def test_fts_finds_term(self, indexed_tree):
patches = _index_patches(indexed_tree["cache_root"])
with patches[0]:
result = query_index_hits(indexed_tree["term"], since_ms=None, max_results=10)
assert result["query_ok"] is True
assert len(result["hits"]) == 1
assert result["hits"][0]["session_id"] == "session_alpha"
assert indexed_tree["term"] in result["hits"][0]["text"]
def test_pointer_swap_uses_uuid_file(self, indexed_tree):
index_path = indexed_tree["index_path"]
assert index_path.name.startswith("search_index.")
assert index_path.name.endswith(".sqlite")
assert index_path.name != "search_index.sqlite"
def test_rebuild_when_manifest_changes(self, indexed_tree):
patches = _index_patches(indexed_tree["cache_root"])
with patches[0]:
assert build_search_index(indexed_tree["projects"], [], force=False) is False
new_session = Path(indexed_tree["projects"]) / "demo-proj" / "session_beta.jsonl"
_write_session(
new_session,
[
{
"type": "user",
"timestamp": "2026-06-01T10:00:00Z",
"message": {"content": [{"type": "text", "text": "beta only"}]},
}
],
)
assert build_search_index(indexed_tree["projects"], [], force=False) is True
assert index_is_usable(indexed_tree["projects"], []) is True
class TestToolResultIndexing:
def test_tool_result_stdout_indexed(self, tmp_path, monkeypatch):
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
session_path = projects / "tool-proj" / "session_tool.jsonl"
sentinel = "tool-result-sentinel-stdout"
_write_session(
session_path,
[
{
"type": "user",
"timestamp": "2026-05-19T11:00:02Z",
"message": {"content": []},
"toolUseResult": {"stdout": f"{sentinel}\n", "stderr": "", "exitCode": 0},
}
],
)
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
patches = _index_patches(cache_root)
with patches[0]:
assert build_search_index(str(projects), [], force=True) is True
hits = query_index_hits(sentinel, since_ms=None, max_results=5)
assert hits["query_ok"] is True
assert any(sentinel in hit["text"] for hit in hits["hits"])
def test_tool_result_searchable_text_helper(self):
text = tool_result_searchable_text({"stdout": "hello", "stderr": "warn"})
assert "hello" in text
assert "warn" in text
class TestSearchWindow:
def test_window_excludes_old_session(self, tmp_path, monkeypatch):
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
old_ts = (datetime.now(UTC) - timedelta(days=60)).strftime("%Y-%m-%dT%H:%M:%SZ")
_write_session(
projects / "win-proj" / "old_session.jsonl",
[
{
"type": "user",
"timestamp": old_ts,
"message": {"content": [{"type": "text", "text": "window-old-sentinel"}]},
}
],
)
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
patches = _index_patches(cache_root)
with patches[0]:
build_search_index(str(projects), [], force=True)
since_ms = resolve_search_since_ms(all_history=False)
hits = query_index_hits("window-old-sentinel", since_ms=since_ms, max_results=5)
assert hits["query_ok"] is True
assert hits["hits"] == []
def test_all_history_includes_old_session(self, tmp_path, monkeypatch):
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
old_ts = (datetime.now(UTC) - timedelta(days=60)).strftime("%Y-%m-%dT%H:%M:%SZ")
_write_session(
projects / "win-proj" / "old_session.jsonl",
[
{
"type": "user",
"timestamp": old_ts,
"message": {"content": [{"type": "text", "text": "history-old-sentinel"}]},
}
],
)
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
patches = _index_patches(cache_root)
with patches[0]:
build_search_index(str(projects), [], force=True)
hits = query_index_hits("history-old-sentinel", since_ms=None, max_results=5)
assert hits["query_ok"] is True
assert len(hits["hits"]) == 1
def test_undated_message_in_window(self, tmp_path, monkeypatch):
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
_write_session(
projects / "win-proj" / "undated.jsonl",
[
{
"type": "user",
"message": {"content": [{"type": "text", "text": "undated-window-sentinel"}]},
}
],
)
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
patches = _index_patches(cache_root)
with patches[0]:
build_search_index(str(projects), [], force=True)
since_ms = resolve_search_since_ms(all_history=False)
hits = query_index_hits("undated-window-sentinel", since_ms=since_ms, max_results=5)
assert hits["query_ok"] is True
assert len(hits["hits"]) == 1
class TestQueryIndexHits:
def test_results_ordered_newest_first(self, tmp_path, monkeypatch):
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
_write_session(
projects / "order-proj" / "session.jsonl",
[
{
"type": "user",
"timestamp": "2026-05-19T10:00:00Z",
"message": {"content": [{"type": "text", "text": "order alpha token"}]},
},
{
"type": "user",
"timestamp": "2026-06-19T10:00:00Z",
"message": {"content": [{"type": "text", "text": "order beta token"}]},
},
{
"type": "user",
"timestamp": "2026-07-19T10:00:00Z",
"message": {"content": [{"type": "text", "text": "order gamma token"}]},
},
],
)
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
patches = _index_patches(cache_root)
with patches[0]:
build_search_index(str(projects), [], force=True)
hits = query_index_hits("order", since_ms=None, max_results=10)
assert hits["query_ok"] is True
assert len(hits["hits"]) == 3
timestamps = [hit["timestamp"] for hit in hits["hits"]]
assert timestamps == sorted(timestamps, reverse=True)
def test_phrase_filter_paginates_past_decoy_token_matches(self, tmp_path, monkeypatch):
"""Multi-word phrase matches beyond one FTS batch are not skipped."""
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
lines: list[dict[str, object]] = []
for i in range(250):
lines.append(
{
"type": "user",
"timestamp": f"2026-07-{(i % 28) + 1:02d}T10:00:00Z",
"message": {
"content": [
{
"type": "text",
"text": f"phrase target decoy unique token only {i}",
}
]
},
}
)
phrase = "decoy unique token only phrase target"
lines.append(
{
"type": "user",
"timestamp": "2025-12-01T10:00:00Z",
"message": {
"content": [
{
"type": "text",
"text": phrase,
}
]
},
}
)
_write_session(projects / "page-proj" / "session.jsonl", lines)
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
patches = _index_patches(cache_root)
with patches[0]:
build_search_index(str(projects), [], force=True)
outcome = _search_via_index(
str(projects),
[],
phrase,
phrase.lower(),
since_ms=None,
max_results=1,
)
assert outcome.hits is not None
assert len(outcome.hits) == 1
snippet = outcome.hits[0]["snippet"]
assert phrase in snippet or "phrase target" in snippet
def test_fts_failure_returns_query_not_ok(self, indexed_tree):
@contextmanager
def _broken_conn(*, readonly: bool = True):
class _Conn:
def execute(self, *args: object, **kwargs: object) -> None:
raise sqlite3.OperationalError("database is locked")
def close(self) -> None:
pass
yield _Conn()
patches = _index_patches(indexed_tree["cache_root"])
with (
patches[0],
patch(
"utils.search_index._index_db_conn",
_broken_conn,
),
):
result = query_index_hits(indexed_tree["term"], since_ms=None, max_results=5)
assert result["query_ok"] is False
assert result["hits"] == []
def test_tokenless_query_is_not_index_ok(self, indexed_tree):
patches = _index_patches(indexed_tree["cache_root"])
with patches[0]:
result = query_index_hits("!!!", since_ms=None, max_results=5)
assert result["query_ok"] is False
assert result["hits"] == []
class TestBypassAndBackground:
def test_no_search_index_env_disables_index(self, indexed_tree, monkeypatch):
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_NO_SEARCH_INDEX", "1")
patches = _index_patches(indexed_tree["cache_root"])
with patches[0]:
assert index_search_enabled() is False
result = query_index_hits(indexed_tree["term"], since_ms=None, max_results=5)
assert result["query_ok"] is False
assert result["hits"] == []
def test_background_worker_starts_once(self, indexed_tree, monkeypatch):
monkeypatch.setenv(
"CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR",
str(indexed_tree["cache_root"]),
)
reset_background_for_tests()
patches = _index_patches(indexed_tree["cache_root"])
with (
patches[0],
patch("utils.search_index.threading.Thread") as mock_thread,
):
# The mock worker is not a real thread; report it as stopped so the
# teardown reset does not treat it as a live worker that won't join.
mock_thread.return_value.is_alive.return_value = False
start_search_index_background(indexed_tree["projects"], [])
start_search_index_background(indexed_tree["projects"], [])
assert mock_thread.call_count == 1
assert mock_thread.call_args.kwargs.get("daemon") is True
assert mock_thread.return_value.start.call_count == 1
reset_background_for_tests()
class TestIndexSearchCompleteness:
def test_partial_batch_not_marked_exhausted(self, tmp_path, monkeypatch):
"""sql_exhausted stays false until every row in the SQL batch is scanned."""
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
lines: list[dict[str, object]] = []
for i in range(120):
lines.append(
{
"type": "user",
"timestamp": f"2026-07-{(i % 28) + 1:02d}T10:00:00Z",
"message": {
"content": [{"type": "text", "text": f"batch sentinel token {i}"}],
},
}
)
_write_session(projects / "batch-proj" / "session.jsonl", lines)
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
patches = _index_patches(cache_root)
with patches[0]:
build_search_index(str(projects), [], force=True)
result = query_index_hits(
"batch sentinel token",
since_ms=None,
max_results=50,
sql_offset=0,
)
assert result["query_ok"] is True
assert len(result["hits"]) == 50
assert result["sql_rows_fetched"] < 120
assert result["sql_exhausted"] is False
def test_index_search_fills_limit_after_excluded_hits(self, tmp_path, monkeypatch):
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
term = "quota-fill-sentinel"
excl_lines: list[dict[str, object]] = []
for i in range(80):
excl_lines.append(
{
"type": "user",
"timestamp": f"2026-08-{(i % 28) + 1:02d}T10:00:00Z",
"message": {"content": [{"type": "text", "text": f"{term} excluded"}]},
}
)
good_lines: list[dict[str, object]] = []
for i in range(60):
good_lines.append(
{
"type": "user",
"timestamp": f"2026-05-{(i % 28) + 1:02d}T10:00:00Z",
"message": {"content": [{"type": "text", "text": f"{term} good"}]},
}
)
_write_session(projects / "excl-proj" / "session.jsonl", excl_lines)
_write_session(projects / "good-proj" / "session.jsonl", good_lines)
rules_path = tmp_path / "rules.txt"
rules_path.write_text("excl-proj\n", encoding="utf-8")
rules = load_rules(str(rules_path))
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
patches = _index_patches(cache_root)
with patches[0]:
build_search_index(str(projects), rules, force=True)
outcome = _search_via_index(
str(projects),
rules,
term,
term.lower(),
since_ms=None,
max_results=50,
)
assert outcome.hits is not None
assert len(outcome.hits) == 50
assert all(hit["project"] == "good-proj" for hit in outcome.hits)
def test_system_content_index_and_live_scan_parity(self, tmp_path, monkeypatch):
cache_root = tmp_path / "cache"
cache_root.mkdir()
projects = tmp_path / "projects"
sentinel = "system-role-sentinel-xyz"
_write_session(
projects / "sys-proj" / "session.jsonl",
[
{
"type": "user",
"timestamp": "2026-06-01T10:00:00Z",
"message": {"content": [{"type": "text", "text": "hello"}]},
},
{
"type": "system",
"timestamp": "2026-06-01T10:00:01Z",
"content": f"compact boundary note {sentinel}",
},
{
"type": "assistant",
"timestamp": "2026-06-01T10:00:02Z",
"message": {"content": [{"type": "text", "text": "ack"}]},
},
],
)
monkeypatch.setenv("CLAUDE_CODE_CHAT_BROWSER_SEARCH_INDEX_DIR", str(cache_root))
patches = _index_patches(cache_root)
with patches[0]:
build_search_index(str(projects), [], force=True)
indexed = query_index_hits(sentinel, since_ms=None, max_results=5)
assert indexed["query_ok"] is True
assert len(indexed["hits"]) == 1
assert indexed["hits"][0]["role"] == "system"
resolved = _resolve_search_results(
str(projects),
[],
sentinel,
sentinel.lower(),
since_ms=None,
max_results=5,
)
assert len(resolved) == 1
assert resolved[0]["role"] == "system"
class TestResolveSearchSinceMs:
def test_all_history_none(self):
assert resolve_search_since_ms(all_history=True) is None
def test_default_thirty_day_window(self):
now = datetime(2026, 7, 7, 12, 0, tzinfo=UTC)
since = resolve_search_since_ms(all_history=False, now=now)
assert since == timestamp_to_ms("2026-06-07T12:00:00Z")
def test_zero_days_searches_all(self):
assert resolve_search_since_ms(all_history=False, since_days=0) is None