Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix: bind evaluation caches to their complete judge inputs (#1219)

Ilia Alshanetskybca00383cafa
diff --git a/changelog.d/+cr-017-evaluation-cache-inputs.fixed.md b/changelog.d/+cr-017-evaluation-cache-inputs.fixed.md
new file mode 100644
index 00000000..76129155
--- /dev/null
+++ b/changelog.d/+cr-017-evaluation-cache-inputs.fixed.md
@@ -0,0 +1 @@
+Search-quality evaluations now reuse cached judgments only when the judge model and complete prompt match, so changed results, topics, or grading instructions receive fresh scores. Older caches are refreshed before reuse.
diff --git a/skills/last30days/scripts/evaluate_search_quality.py b/skills/last30days/scripts/evaluate_search_quality.py
index d5e3aad2..15597e4c 100644
--- a/skills/last30days/scripts/evaluate_search_quality.py
+++ b/skills/last30days/scripts/evaluate_search_quality.py
@@ -4,6 +4,7 @@
from __future__ import annotations
import argparse
+import hashlib
import json
import math
import os
@@ -282,30 +283,25 @@ def get_judgments(
) -> dict[str, int]:
cache_file = output_dir / "judgments" / f"{slug}.json"
cache_file.parent.mkdir(parents=True, exist_ok=True)
+ prompt = build_judge_prompt(topic, query_type, items)
+ input_sha256 = hashlib.sha256(json.dumps([judge_model, prompt]).encode("utf-8")).hexdigest()
stale_cache = False
if cache_file.exists():
payload = json.loads(cache_file.read_text())
- # The cache key is the topic slug alone, but judgments are model-
- # specific. Only reuse the cache when it was produced by the same judge
- # model; otherwise re-judge, so a --judge-model change cannot return
- # stale grades that silently skew precision@k / nDCG. Caches written
- # before judge_model was recorded miss here and get refreshed once.
- if payload.get("judge_model") == judge_model:
+ if payload.get("input_sha256") == input_sha256:
return {row["id"]: int(row["grade"]) for row in payload.get("judgments") or []}
stale_cache = True
if not gemini_api_key or not items:
- if stale_cache:
- # Discarded a different-model cache but can't re-judge. Returning {}
- # scores every item as ungraded (zero precision@k / nDCG); say so
- # rather than letting the run report silently wrong numbers.
+ if stale_cache and not gemini_api_key:
sys.stderr.write(
- f"[Eval] Cached judgments for {slug!r} were graded by a different "
- f"judge model and no Gemini API key is set to re-judge; returning "
+ f"[Eval] Cached judgments for {slug!r} have different or unverified "
+ f"judge inputs/model and no Gemini API key is set to re-judge; returning "
f"no grades (metrics for this topic will be zero).\n"
)
return {}
- payload = call_gemini_judge(gemini_api_key, judge_model, build_judge_prompt(topic, query_type, items))
+ payload = call_gemini_judge(gemini_api_key, judge_model, prompt)
payload["judge_model"] = judge_model
+ payload["input_sha256"] = input_sha256
cache_file.write_text(json.dumps(payload, indent=2))
return {row["id"]: int(row["grade"]) for row in payload.get("judgments") or []}
diff --git a/tests/test_evaluator_v3.py b/tests/test_evaluator_v3.py
index e05f2d17..30c20b45 100644
--- a/tests/test_evaluator_v3.py
+++ b/tests/test_evaluator_v3.py
@@ -2,6 +2,7 @@ import contextlib
import io
import json
import os
+import sys
import tempfile
import unittest
from pathlib import Path
@@ -11,6 +12,60 @@ import evaluate_search_quality as evaluator
class EvaluatorV3Tests(unittest.TestCase):
+ def test_main_rejudges_changed_results_and_reuses_identical_results(self):
+ for changed_id in (False, True):
+ with self.subTest(changed_id=changed_id), tempfile.TemporaryDirectory() as tmp:
+ output_dir = Path(tmp)
+ topics = output_dir / "topics.json"
+ topics.write_text(json.dumps([{"topic": "test topic", "query_type": "general"}]))
+ baseline_url = "https://example.com/baseline-only"
+ shared_url = "https://example.com/shared"
+ old_url = "https://example.com/old"
+ new_url = "https://example.com/new" if changed_id else old_url
+ shared_item = {"url": shared_url, "title": "Shared relevant result", "score": 80}
+ baseline_report = {"reddit": [
+ {"url": baseline_url, "title": "Unrelated baseline result", "score": 90},
+ shared_item,
+ ]}
+ old_report = {"reddit": [shared_item, {"url": old_url, "title": "Relevant result", "score": 90}]}
+ new_title = "New relevant result" if changed_id else "Off-topic replacement"
+ new_report = {"reddit": [shared_item, {"url": new_url, "title": new_title, "score": 90}]}
+ new_grade = 3 if changed_id else 0
+ responses = [
+ mock.Mock(returncode=0, stdout=json.dumps(report), stderr="")
+ for report in (baseline_report, old_report, baseline_report, new_report, baseline_report, new_report)
+ ]
+ argv = [
+ "evaluate_search_quality.py", "--baseline=WORKTREE", "--candidate=WORKTREE",
+ "--topics-file", str(topics), "--output-dir", str(output_dir),
+ ]
+ with (
+ mock.patch.object(sys, "argv", argv),
+ mock.patch.dict(os.environ, {"GOOGLE_API_KEY": "dummy-evaluation-key"}),
+ mock.patch.object(evaluator.subprocess, "run", autospec=True, side_effect=responses),
+ mock.patch.object(evaluator, "call_gemini_judge", autospec=True, side_effect=[
+ {"judgments": [{"id": baseline_url, "grade": 0}, {"id": shared_url, "grade": 3}, {"id": old_url, "grade": 3}]},
+ {"judgments": [{"id": baseline_url, "grade": 0}, {"id": shared_url, "grade": 3}, {"id": new_url, "grade": new_grade}]},
+ ]) as judge,
+ contextlib.redirect_stdout(io.StringIO()),
+ ):
+ self.assertEqual(0, evaluator.main())
+ first = json.loads((output_dir / "metrics.json").read_text())
+ self.assertEqual(0.5, first["topics"][0]["baseline"]["precision_at_5"])
+ self.assertEqual(1.0, first["topics"][0]["candidate"]["precision_at_5"])
+ self.assertEqual(0, evaluator.main())
+ second = json.loads((output_dir / "metrics.json").read_text())
+ self.assertEqual(0.5, second["topics"][0]["baseline"]["precision_at_5"])
+ self.assertEqual((1 + (new_grade >= 2)) / 2, second["topics"][0]["candidate"]["precision_at_5"])
+ self.assertEqual(2, judge.call_count)
+ for call, result_url in zip(judge.call_args_list, (old_url, new_url)):
+ judged_ids = [line.removeprefix("- id: ") for line in call.args[2].splitlines() if line.startswith("- id: ")]
+ self.assertCountEqual([baseline_url, shared_url, result_url], judged_ids)
+ self.assertEqual(0, evaluator.main())
+ third = json.loads((output_dir / "metrics.json").read_text())
+ self.assertEqual(second["topics"], third["topics"])
+ self.assertEqual(2, judge.call_count)
+
def test_build_ranked_items_uses_multi_source_provenance_and_best_date(self):
report = {
"ranked_candidates": [
@@ -115,24 +170,28 @@ class EvaluatorV3Tests(unittest.TestCase):
def test_get_judgments_uses_cache_and_skips_when_not_configured(self):
with tempfile.TemporaryDirectory() as tmp:
output_dir = Path(tmp)
- cache_dir = output_dir / "judgments"
- cache_dir.mkdir()
- (cache_dir / "topic.json").write_text(
- json.dumps(
- {
- "judge_model": "gemini-3.1-flash-lite",
- "judgments": [{"id": "a", "grade": 3}],
- }
+ items = [{"key": "a", "source": "reddit", "text": "Test topic", "url": "https://example.com/a"}]
+ with mock.patch.object(evaluator, "call_gemini_judge", autospec=True, return_value={
+ "judgments": [{"id": "a", "grade": 3}],
+ }) as judge:
+ evaluator.get_judgments(
+ output_dir=output_dir,
+ slug="topic",
+ topic="test topic",
+ query_type="general",
+ items=items,
+ judge_model="gemini-3.1-flash-lite",
+ gemini_api_key="dummy-evaluation-key",
)
- )
+ judge.assert_called_once()
cached = evaluator.get_judgments(
output_dir=output_dir,
slug="topic",
topic="test topic",
query_type="general",
- items=[{"key": "a"}],
+ items=items,
judge_model="gemini-3.1-flash-lite",
- gemini_api_key="key",
+ gemini_api_key=None,
)
self.assertEqual({"a": 3}, cached)
@@ -147,9 +206,7 @@ class EvaluatorV3Tests(unittest.TestCase):
)
self.assertEqual({}, skipped)
- def test_get_judgments_remisses_on_judge_model_change(self):
- """A cache written by a different judge model must not be reused; a
- --judge-model change forces a re-judge instead of returning stale grades."""
+ def test_get_judgments_rejects_unverified_legacy_cache_without_api_key(self):
with tempfile.TemporaryDirectory() as tmp:
output_dir = Path(tmp)
cache_dir = output_dir / "judgments"
@@ -162,10 +219,6 @@ class EvaluatorV3Tests(unittest.TestCase):
}
)
)
- # Same slug, different model, no API key to re-judge: the stale
- # grades must NOT come back — an empty result signals "re-judge
- # needed" rather than silently wrong numbers, and the discard is
- # announced on stderr instead of failing silently.
stderr = io.StringIO()
with contextlib.redirect_stderr(stderr):
result = evaluator.get_judgments(
@@ -173,12 +226,65 @@ class EvaluatorV3Tests(unittest.TestCase):
slug="topic",
topic="test topic",
query_type="general",
- items=[{"key": "a"}],
- judge_model="gemini-2.5-pro",
+ items=[{"key": "a", "source": "reddit", "text": "Test topic", "url": "https://example.com/a"}],
+ judge_model="gemini-3.1-flash-lite",
gemini_api_key=None,
)
self.assertEqual({}, result)
- self.assertIn("different", stderr.getvalue())
+ self.assertIn("unverified", stderr.getvalue())
+
+ def test_get_judgments_refreshes_legacy_cache(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ output_dir = Path(tmp)
+ cache_dir = output_dir / "judgments"
+ cache_dir.mkdir()
+ (cache_dir / "topic.json").write_text(json.dumps({
+ "judge_model": "gemini-3.1-flash-lite",
+ "judgments": [{"id": "a", "grade": 3}],
+ }))
+ with mock.patch.object(evaluator, "call_gemini_judge", autospec=True, return_value={
+ "judgments": [{"id": "a", "grade": 0}],
+ }) as judge:
+ result = evaluator.get_judgments(
+ output_dir=output_dir,
+ slug="topic",
+ topic="test topic",
+ query_type="general",
+ items=[{"key": "a", "source": "reddit", "text": "Off topic", "url": "https://example.com/a"}],
+ judge_model="gemini-3.1-flash-lite",
+ gemini_api_key="dummy-evaluation-key",
+ )
+ self.assertEqual({"a": 0}, result)
+ judge.assert_called_once()
+
+ def test_get_judgments_rejudges_changed_context(self):
+ changes = [
+ {"topic": "other topic"},
+ {"query_type": "comparison"},
+ {"judge_model": "other-model"},
+ {"source": "youtube"},
+ {"url": "https://example.com/changed"},
+ {"date": "2026-10-02"},
+ ]
+ for change in changes:
+ with self.subTest(change=change), tempfile.TemporaryDirectory() as tmp:
+ item = {"key": "a", "source": "reddit", "text": "Test topic", "url": "https://example.com/a", "date": "2026-10-01"}
+ arguments = dict(
+ output_dir=Path(tmp), slug="topic", topic="test topic", query_type="general",
+ items=[item], judge_model="gemini-3.1-flash-lite", gemini_api_key="dummy-evaluation-key",
+ )
+ with mock.patch.object(evaluator, "call_gemini_judge", autospec=True, side_effect=[
+ {"judgments": [{"id": "a", "grade": 3}]},
+ {"judgments": [{"id": "a", "grade": 0}]},
+ ]) as judge:
+ self.assertEqual({"a": 3}, evaluator.get_judgments(**arguments))
+ for key, value in change.items():
+ if key in item:
+ item[key] = value
+ else:
+ arguments[key] = value
+ self.assertEqual({"a": 0}, evaluator.get_judgments(**arguments))
+ self.assertEqual(2, judge.call_count)
def test_create_eval_env_and_run_last30days(self):
credential_env = {