If a semantic search returns no results above the specified --threshold, ck may output a "near-miss" hint to stderr. This hint suggests a lower threshold that might yield results.
An effective AI agent pattern is to monitor stderr for the string Near-miss and the suggestion Try lowering threshold to, then automatically retry the search with the suggested value.
def adaptive_search(query: str, threshold: float = 0.6) -> list:
"""Search with adaptive threshold based on results."""
cmd = ["ck", "--sem", query, ".", "--json", "--threshold", str(threshold), "--limit", "10"]
result = subprocess.run(cmd, capture_output=True, text=True)
# Check for near-miss hint in stderr
if "Near-miss" in result.stderr and "Try lowering threshold to" in result.stderr:
# Extract suggested threshold
suggested = extract_threshold_hint(result.stderr)
# Retry with suggested threshold
return adaptive_search(query, suggested)
return json.loads(result.stdout) if result.stdout else []