import json
import subprocess
import sys
def query_reflex(pattern: str, limit: int = 10, reflex_bin: str = "rfx") -> dict:
cmd = [reflex_bin, "query", pattern, "--json", "--limit", str(limit)]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return json.loads(result.stdout)
def ensure_fresh_index(response: dict) -> dict:
metadata = response["metadata"]
status = metadata["status"]
if status != "fresh":
print(f"⚠️ Index is stale: {status}", file=sys.stderr)
if "reason" in metadata:
print(f" Reason: {metadata['reason']}", file=sys.stderr)
action = metadata.get("action_required", "rfx index")
print(f"🔄 Running: {action}", file=sys.stderr)
subprocess.run(action.split(), check=True)
print("🔍 Re-running query with fresh index...", file=sys.stderr)
return response
def main():
pattern = "CacheManager"
print(f"Searching for: {pattern}", file=sys.stderr)
import os
reflex_candidates = [
"/ramdisk/target/release/rfx", "./target/release/rfx", "rfx", ]
reflex_bin = next((p for p in reflex_candidates if os.path.exists(p) or p == "rfx"), "rfx")
response = query_reflex(pattern, limit=5, reflex_bin=reflex_bin)
response = ensure_fresh_index(response)
results = response["results"]
print(f"\nFound {len(results)} results:", file=sys.stderr)
for result in results:
path = result["path"]
line = result["span"]["start_line"]
symbol = result["symbol"]
preview = result["preview"]
print(f"\n{path}:{line}")
print(f" Symbol: {symbol}")
print(f" Preview: {preview}")
if __name__ == "__main__":
main()