1010from copy import deepcopy
1111from typing import Any
1212
13- from codewiki .src .config import CLUSTERING_BATCH_CACHE_FILENAME
13+ from codewiki .src .config import CLUSTERING_BATCH_CACHE_FILENAME , Config
1414from codewiki .src .utils import file_manager
1515
1616logger = logging .getLogger (__name__ )
1717
18- CACHE_VERSION = 3
18+ CACHE_VERSION = 4
1919
2020
2121def clustering_cache_scope (module_path : list [str ]) -> str :
@@ -25,11 +25,46 @@ def clustering_cache_scope(module_path: list[str]) -> str:
2525 return "/" .join (module_path )
2626
2727
28- def batch_entry_key (scope : str , batch_index : int , node_ids : list [str ]) -> str :
29- """Scope, 1-based batch index, and a fingerprint of the batch's component IDs."""
30- canonical = "\n " .join (sorted (node_ids ))
28+ def clustering_request_fingerprint (config : Config , prompt : str ) -> str :
29+ """Identify the rendered request and configured routing without persisting secrets."""
30+ routing = {
31+ name : getattr (config , name , None )
32+ for name in (
33+ "provider" ,
34+ "cluster_model" ,
35+ "main_model" ,
36+ "llm_base_url" ,
37+ "azure_deployment" ,
38+ "api_version" ,
39+ "aws_region" ,
40+ "google_project" ,
41+ "google_location" ,
42+ "max_tokens" ,
43+ )
44+ }
45+ if routing ["provider" ] == "google-vertex" :
46+ routing ["google_project" ] = routing ["google_project" ] or os .getenv (
47+ "GOOGLE_CLOUD_PROJECT" , ""
48+ )
49+ routing ["google_location" ] = (
50+ routing ["google_location" ] or os .getenv ("GOOGLE_CLOUD_LOCATION" ) or "global"
51+ )
52+ context = {
53+ "repo" : os .path .realpath (config .repo_path ),
54+ "routing" : routing ,
55+ "prompt" : prompt ,
56+ }
57+ canonical = json .dumps (context , sort_keys = True , ensure_ascii = False , separators = ("," , ":" ))
58+ return hashlib .sha256 (canonical .encode ("utf-8" )).hexdigest ()
59+
60+
61+ def batch_entry_key (
62+ scope : str , batch_index : int , node_ids : list [str ], request_fingerprint : str
63+ ) -> str :
64+ """Scope, batch index, component set, and exact request identity."""
65+ canonical = json .dumps (sorted (node_ids ), ensure_ascii = False , separators = ("," , ":" ))
3166 digest = hashlib .sha256 (canonical .encode ("utf-8" )).hexdigest ()[:16 ]
32- return f"{ scope } |{ batch_index } |{ digest } "
67+ return f"{ scope } |{ batch_index } |{ digest } | { request_fingerprint } "
3368
3469
3570def clustering_batch_cache_path (docs_dir : str ) -> str :
@@ -58,6 +93,8 @@ def _valid_entry(entry: Any) -> bool:
5893 partial = entry .get ("partial_tree" )
5994 return (
6095 isinstance (entry .get ("scope" ), str )
96+ and isinstance (entry .get ("request_fingerprint" ), str )
97+ and bool (entry ["request_fingerprint" ])
6198 and isinstance (entry .get ("batch_index" ), int )
6299 and not isinstance (entry ["batch_index" ], bool )
63100 and entry ["batch_index" ] > 0
@@ -141,15 +178,22 @@ def scope_entry_count(self, scope: str) -> int:
141178 return 0
142179 return sum (1 for entry in entries .values () if entry .get ("scope" ) == scope )
143180
144- def lookup (self , scope : str , batch_index : int , node_ids : list [str ]) -> dict [str , Any ] | None :
181+ def lookup (
182+ self , scope : str , batch_index : int , node_ids : list [str ], * , request_fingerprint : str
183+ ) -> dict [str , Any ] | None :
145184 if not self ._enabled :
146185 return None
147- key = batch_entry_key (scope , batch_index , node_ids )
186+ key = batch_entry_key (scope , batch_index , node_ids , request_fingerprint )
148187 entry = self ._data ["entries" ].get (key )
149188 if not _valid_entry (entry ):
150189 return None
151190 stored_ids = entry .get ("node_ids" )
152- if stored_ids != sorted (node_ids ):
191+ if (
192+ stored_ids != sorted (node_ids )
193+ or entry ["scope" ] != scope
194+ or entry ["batch_index" ] != batch_index
195+ or entry ["request_fingerprint" ] != request_fingerprint
196+ ):
153197 return None
154198 partial = entry .get ("partial_tree" )
155199 if not isinstance (partial , dict ) or not partial :
@@ -168,14 +212,17 @@ def store(
168212 batch_index : int ,
169213 node_ids : list [str ],
170214 partial_tree : dict [str , Any ],
215+ * ,
216+ request_fingerprint : str ,
171217 ) -> None :
172218 if not self ._enabled or not partial_tree :
173219 return
174- key = batch_entry_key (scope , batch_index , node_ids )
220+ key = batch_entry_key (scope , batch_index , node_ids , request_fingerprint )
175221 entry = {
176222 "scope" : scope ,
177223 "batch_index" : batch_index ,
178224 "node_ids" : sorted (node_ids ),
225+ "request_fingerprint" : request_fingerprint ,
179226 "partial_tree" : deepcopy (partial_tree ),
180227 }
181228 if not _valid_entry (entry ):
0 commit comments