1616
1717DEFAULT_MAX_ROWS = 2000
1818
19+
20+ def max_cache_rows () -> int :
21+ """Return LRU capacity (override via CLAUDE_CODE_CHAT_BROWSER_SUMMARY_CACHE_MAX_ROWS)."""
22+ raw = os .environ .get ("CLAUDE_CODE_CHAT_BROWSER_SUMMARY_CACHE_MAX_ROWS" , "" ).strip ()
23+ if not raw :
24+ return DEFAULT_MAX_ROWS
25+ return max (1 , int (raw ))
26+
27+
1928_lock = threading .Lock ()
2029_conn : sqlite3 .Connection | None = None
2130
@@ -54,7 +63,9 @@ def _ensure_connection() -> sqlite3.Connection:
5463 return _conn
5564 path = cache_db_path ()
5665 path .parent .mkdir (parents = True , exist_ok = True )
57- conn = sqlite3 .connect (str (path ), check_same_thread = False )
66+ conn = sqlite3 .connect (str (path ), check_same_thread = False , timeout = 30.0 )
67+ conn .execute ("PRAGMA journal_mode=WAL" )
68+ conn .execute ("PRAGMA busy_timeout=30000" )
5869 conn .execute (
5970 """
6071 CREATE TABLE IF NOT EXISTS summary_cache (
@@ -102,15 +113,6 @@ def get_summary(path: str, mtime: float, rules_fingerprint: str) -> SummaryCache
102113 ).fetchone ()
103114 if row is None :
104115 return None
105- now = time .time ()
106- conn .execute (
107- (
108- "UPDATE summary_cache SET accessed_at = ? "
109- "WHERE path = ? AND mtime = ? AND rules_fp = ?"
110- ),
111- (now , abspath , mtime , rules_fingerprint ),
112- )
113- conn .commit ()
114116 return _payload_to_row (str (row [0 ]))
115117
116118
@@ -126,6 +128,10 @@ def put_summary(
126128 payload = _row_to_payload (row )
127129 with _lock :
128130 conn = _ensure_connection ()
131+ conn .execute (
132+ "DELETE FROM summary_cache WHERE path = ? AND rules_fp = ? AND mtime != ?" ,
133+ (abspath , rules_fingerprint , mtime ),
134+ )
129135 conn .execute (
130136 """
131137 INSERT INTO summary_cache (path, mtime, rules_fp, payload, accessed_at)
@@ -137,7 +143,8 @@ def put_summary(
137143 (abspath , mtime , rules_fingerprint , payload , now ),
138144 )
139145 count = conn .execute ("SELECT COUNT(*) FROM summary_cache" ).fetchone ()
140- if count is not None and int (count [0 ]) > DEFAULT_MAX_ROWS :
146+ limit = max_cache_rows ()
147+ if count is not None and int (count [0 ]) > limit :
141148 conn .execute (
142149 """
143150 DELETE FROM summary_cache
@@ -147,7 +154,7 @@ def put_summary(
147154 LIMIT ?
148155 )
149156 """ ,
150- (int (count [0 ]) - DEFAULT_MAX_ROWS ,),
157+ (int (count [0 ]) - limit ,),
151158 )
152159 conn .commit ()
153160
0 commit comments