@@ -4369,22 +4369,24 @@ def _synchronize_knowledge_base_with_reality(self, verbose: bool = False) -> Lis
43694369 # ========================================================================
43704370
43714371 # Step 1: Build map of disk instances by their installation hashes
4372+ safe_print(" -> [DIAGNOSTIC] Building map of all instances found on disk...")
43724373 disk_instance_map_by_hash = {}
43734374 for dist in all_discovered_dists:
43744375 try:
43754376 c_name = canonicalize_name(dist.metadata['Name'])
4377+ raw_path_str = str(dist._path)
43764378 resolved_path_str = str(dist._path.resolve())
43774379 unique_instance_identifier = f"{resolved_path_str}::{dist.version}"
43784380 instance_hash = hashlib.sha256(unique_instance_identifier.encode()).hexdigest()[:12]
43794381
4380-
4382+ safe_print(f" - DISK: Found '{c_name}=={dist.version}'. Path: '{raw_path_str}'. Resolved Path: '{resolved_path_str}'. -> HASH: {instance_hash}")
43814383
4382- # This will print for every package found during the full sync
43834384 disk_instance_map_by_hash[instance_hash] = dist
43844385 except Exception:
43854386 continue
43864387
43874388 # Step 2: Get all KB instance keys and extract their installation hashes
4389+ safe_print(" -> [DIAGNOSTIC] Getting all instance hashes from Knowledge Base (Redis)...")
43884390 kb_instance_keys = set(self.cache_client.keys(self.redis_key_prefix.replace(':pkg:', ':inst:') + '*'))
43894391
43904392 kb_hashes_to_keys = {} # Map: installation_hash -> redis_key
@@ -4396,13 +4398,34 @@ def _synchronize_knowledge_base_with_reality(self, verbose: bool = False) -> Lis
43964398 for key, stored_hash in zip(kb_instance_keys, results):
43974399 if stored_hash:
43984400 kb_hashes_to_keys[stored_hash] = key
4401+ safe_print(f" - KB: Found Key '{key}' -> HASH: {stored_hash}")
4402+ else:
4403+ safe_print(f" - KB WARNING: Key '{key}' is missing its 'installation_hash' field!")
43994404
44004405 # Step 3: Identify instances that need registration (hash not in KB)
44014406 disk_hashes = set(disk_instance_map_by_hash.keys())
44024407 kb_hashes = set(kb_hashes_to_keys.keys())
44034408
44044409 hashes_needing_registration = disk_hashes - kb_hashes
44054410 instances_to_rebuild = [disk_instance_map_by_hash[hash_val] for hash_val in hashes_needing_registration]
4411+
4412+ if instances_to_rebuild:
4413+ safe_print(f" -> [DIAGNOSTIC] The following {len(hashes_needing_registration)} hashes are NEW (on disk, but not in KB):")
4414+ for h in hashes_needing_registration:
4415+ dist = disk_instance_map_by_hash.get(h)
4416+ safe_print(f" - NEW HASH: {h} (corresponds to disk path: '{dist._path.resolve() if dist else '??'}')")
4417+
4418+ # Step 4: Clean up ghost instances (KB hashes that don't exist on disk)
4419+ ghost_hashes = kb_hashes - disk_hashes
4420+ if ghost_hashes:
4421+ safe_print(f" -> [DIAGNOSTIC] The following {len(ghost_hashes)} hashes are GHOSTS (in KB, but not found on disk):")
4422+ for h in ghost_hashes:
4423+ safe_print(f" - GHOST HASH: {h} (corresponds to KB key: '{kb_hashes_to_keys.get(h)}')")
4424+
4425+ safe_print(f" -> 👻 Removing {len(ghost_hashes)} ghost instance(s) from KB...")
4426+ ghost_keys = [kb_hashes_to_keys[hash_val] for hash_val in ghost_hashes]
4427+ if ghost_keys:
4428+ self.cache_client.delete(*ghost_keys)
44064429
44074430 if instances_to_rebuild:
44084431 safe_print(f" -> 🔍 Found {len(instances_to_rebuild)} unregistered instance(s) at new paths.")
@@ -4421,15 +4444,7 @@ def _synchronize_knowledge_base_with_reality(self, verbose: bool = False) -> Lis
44214444
44224445 gatherer.run(pre_discovered_distributions=instances_to_rebuild)
44234446 self._installed_packages_cache = None
4424-
4425- # Step 4: Clean up ghost instances (KB hashes that don't exist on disk)
4426- ghost_hashes = kb_hashes - disk_hashes
4427- if ghost_hashes:
4428- safe_print(f" -> 👻 Removing {len(ghost_hashes)} ghost instance(s) from KB...")
4429- ghost_keys = [kb_hashes_to_keys[hash_val] for hash_val in ghost_hashes]
4430- if ghost_keys:
4431- self.cache_client.delete(*ghost_keys)
4432-
4447+
44334448 if instances_to_rebuild or ghost_hashes:
44344449 safe_print(" -> ✅ Instance-level healing complete.")
44354450
@@ -4496,6 +4511,7 @@ def _synchronize_knowledge_base_with_reality(self, verbose: bool = False) -> Lis
44964511
44974512 return all_discovered_dists
44984513
4514+
44994515 def _get_disk_specs_for_context(self, python_version: str) -> set:
45004516 """
45014517 (V3 - ROBUST PATH FIX) A lightweight, READ-ONLY function to get the ground truth
0 commit comments