feat(search): support exact hex palette lookup (#598)

This commit is contained in:
Vasiliy Sirotinskiy 2026-11-24 22:05:40 +03:00 committed by GitHub
parent cc7b20d85d
commit 8db3633881
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 129 additions and 3 deletions

View File

@ -10,6 +10,7 @@ import re
from pathlib import Path
from math import log
from collections import defaultdict
from itertools import islice
# ============ CONFIGURATION ============
DATA_DIR = Path(__file__).parent.parent / "data"
@ -487,6 +488,20 @@ def _search_csv_detailed(filepath, search_cols, output_cols, query, max_results,
if not data:
return [], None, {"reason": "empty-data"}
if routing_domain == "color" and re.fullmatch(r"#[0-9a-fA-F]{6}", query.strip()):
hex_value = query.strip().lower()
color_columns = [col for col in output_cols if col not in ("Product Type", "Notes")]
matches = (
row for row in data
if any((row.get(col) or "").strip().lower() == hex_value for col in color_columns)
)
results = [_project_row(row, output_cols) for row in islice(matches, max_results)]
reason = "exact-color" if results else "no-exact-color"
diagnostic = _exact_match_diagnostic(query, reason)
if not results:
diagnostic.update(abstained=True, token_coverage=0.0)
return results, None, diagnostic
bm25 = _get_bm25(filepath, search_cols, data, signature, cache_variant)
search_query, rewrites = _rewrite_query_for_domain(query, routing_domain, bm25)
ranked = bm25.score(search_query)

View File

@ -2,7 +2,7 @@
"schemaVersion": 1,
"status": "provisional-baseline-regression-gate",
"baselineRevision": "97eb2a2",
"runtimeFingerprint": "94da359a335caa9fb1532efc6b9792f01b559ae66b6500239e1e1f4c01204819",
"runtimeFingerprint": "d0ea451912fbf915e8e81aa5e387445c0f5e55f440b3468c80cac479ce5083a4",
"oracleFingerprint": "02e7226428c64a995ec24ae104ce4d186f5f91bf0f062556e90ef9b0fcb6d67a",
"approvingMaintainer": "repository maintainer approved Phase 1 testing checkpoint; second judgment review remains required before final-target promotion",
"units": "All metrics are ratios in [0,1]. Precision treats missing ranks as non-relevant. Negative abstention is measured across the hard-negative by domain/stack cross-product.",

View File

@ -13,6 +13,7 @@ or directly:
import os
import json
import shutil
import subprocess
import sys
import tempfile
@ -111,6 +112,32 @@ class TestBm25CoreBehavior(unittest.TestCase):
class TestSearchDomains(unittest.TestCase):
def test_exact_hex_search_matches_palette_roles(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
scripts = root / "scripts"
shutil.copytree(
SCRIPTS_DIR, scripts, ignore=shutil.ignore_patterns("tests", "__pycache__")
)
data = root / "data"
data.mkdir()
(data / "colors.csv").write_text(
"Product Type,Primary,Accent,Notes\n"
"Notes only,#FFFFFF,#FFFFFF,#A1B2C3\n"
"Matching palette,#a1b2c3,#A1B2C3,\n",
encoding="utf-8",
)
output = subprocess.check_output(
[sys.executable, "-B", str(scripts / "search.py"),
"#A1b2C3", "--domain", "color", "--json"],
cwd=root,
)
result = json.loads(output)
self.assertEqual(
[row["Product Type"] for row in result["results"]], ["Matching palette"]
)
def test_read_failure_is_not_reported_as_a_search_result(self):
failure = UnicodeDecodeError("utf-8", b"\xff", 0, 1, "invalid")
with patch("core._load_csv_snapshot", side_effect=failure):

View File

@ -10,6 +10,7 @@ import re
from pathlib import Path
from math import log
from collections import defaultdict
from itertools import islice
# ============ CONFIGURATION ============
DATA_DIR = Path(__file__).parent.parent / "data"
@ -487,6 +488,20 @@ def _search_csv_detailed(filepath, search_cols, output_cols, query, max_results,
if not data:
return [], None, {"reason": "empty-data"}
if routing_domain == "color" and re.fullmatch(r"#[0-9a-fA-F]{6}", query.strip()):
hex_value = query.strip().lower()
color_columns = [col for col in output_cols if col not in ("Product Type", "Notes")]
matches = (
row for row in data
if any((row.get(col) or "").strip().lower() == hex_value for col in color_columns)
)
results = [_project_row(row, output_cols) for row in islice(matches, max_results)]
reason = "exact-color" if results else "no-exact-color"
diagnostic = _exact_match_diagnostic(query, reason)
if not results:
diagnostic.update(abstained=True, token_coverage=0.0)
return results, None, diagnostic
bm25 = _get_bm25(filepath, search_cols, data, signature, cache_variant)
search_query, rewrites = _rewrite_query_for_domain(query, routing_domain, bm25)
ranked = bm25.score(search_query)

View File

@ -2,7 +2,7 @@
"schemaVersion": 1,
"status": "provisional-baseline-regression-gate",
"baselineRevision": "97eb2a2",
"runtimeFingerprint": "94da359a335caa9fb1532efc6b9792f01b559ae66b6500239e1e1f4c01204819",
"runtimeFingerprint": "d0ea451912fbf915e8e81aa5e387445c0f5e55f440b3468c80cac479ce5083a4",
"oracleFingerprint": "02e7226428c64a995ec24ae104ce4d186f5f91bf0f062556e90ef9b0fcb6d67a",
"approvingMaintainer": "repository maintainer approved Phase 1 testing checkpoint; second judgment review remains required before final-target promotion",
"units": "All metrics are ratios in [0,1]. Precision treats missing ranks as non-relevant. Negative abstention is measured across the hard-negative by domain/stack cross-product.",

View File

@ -13,6 +13,7 @@ or directly:
import os
import json
import shutil
import subprocess
import sys
import tempfile
@ -111,6 +112,32 @@ class TestBm25CoreBehavior(unittest.TestCase):
class TestSearchDomains(unittest.TestCase):
def test_exact_hex_search_matches_palette_roles(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
scripts = root / "scripts"
shutil.copytree(
SCRIPTS_DIR, scripts, ignore=shutil.ignore_patterns("tests", "__pycache__")
)
data = root / "data"
data.mkdir()
(data / "colors.csv").write_text(
"Product Type,Primary,Accent,Notes\n"
"Notes only,#FFFFFF,#FFFFFF,#A1B2C3\n"
"Matching palette,#a1b2c3,#A1B2C3,\n",
encoding="utf-8",
)
output = subprocess.check_output(
[sys.executable, "-B", str(scripts / "search.py"),
"#A1b2C3", "--domain", "color", "--json"],
cwd=root,
)
result = json.loads(output)
self.assertEqual(
[row["Product Type"] for row in result["results"]], ["Matching palette"]
)
def test_read_failure_is_not_reported_as_a_search_result(self):
failure = UnicodeDecodeError("utf-8", b"\xff", 0, 1, "invalid")
with patch("core._load_csv_snapshot", side_effect=failure):

View File

@ -10,6 +10,7 @@ import re
from pathlib import Path
from math import log
from collections import defaultdict
from itertools import islice
# ============ CONFIGURATION ============
DATA_DIR = Path(__file__).parent.parent / "data"
@ -487,6 +488,20 @@ def _search_csv_detailed(filepath, search_cols, output_cols, query, max_results,
if not data:
return [], None, {"reason": "empty-data"}
if routing_domain == "color" and re.fullmatch(r"#[0-9a-fA-F]{6}", query.strip()):
hex_value = query.strip().lower()
color_columns = [col for col in output_cols if col not in ("Product Type", "Notes")]
matches = (
row for row in data
if any((row.get(col) or "").strip().lower() == hex_value for col in color_columns)
)
results = [_project_row(row, output_cols) for row in islice(matches, max_results)]
reason = "exact-color" if results else "no-exact-color"
diagnostic = _exact_match_diagnostic(query, reason)
if not results:
diagnostic.update(abstained=True, token_coverage=0.0)
return results, None, diagnostic
bm25 = _get_bm25(filepath, search_cols, data, signature, cache_variant)
search_query, rewrites = _rewrite_query_for_domain(query, routing_domain, bm25)
ranked = bm25.score(search_query)

View File

@ -2,7 +2,7 @@
"schemaVersion": 1,
"status": "provisional-baseline-regression-gate",
"baselineRevision": "97eb2a2",
"runtimeFingerprint": "94da359a335caa9fb1532efc6b9792f01b559ae66b6500239e1e1f4c01204819",
"runtimeFingerprint": "d0ea451912fbf915e8e81aa5e387445c0f5e55f440b3468c80cac479ce5083a4",
"oracleFingerprint": "02e7226428c64a995ec24ae104ce4d186f5f91bf0f062556e90ef9b0fcb6d67a",
"approvingMaintainer": "repository maintainer approved Phase 1 testing checkpoint; second judgment review remains required before final-target promotion",
"units": "All metrics are ratios in [0,1]. Precision treats missing ranks as non-relevant. Negative abstention is measured across the hard-negative by domain/stack cross-product.",

View File

@ -13,6 +13,7 @@ or directly:
import os
import json
import shutil
import subprocess
import sys
import tempfile
@ -111,6 +112,32 @@ class TestBm25CoreBehavior(unittest.TestCase):
class TestSearchDomains(unittest.TestCase):
def test_exact_hex_search_matches_palette_roles(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
scripts = root / "scripts"
shutil.copytree(
SCRIPTS_DIR, scripts, ignore=shutil.ignore_patterns("tests", "__pycache__")
)
data = root / "data"
data.mkdir()
(data / "colors.csv").write_text(
"Product Type,Primary,Accent,Notes\n"
"Notes only,#FFFFFF,#FFFFFF,#A1B2C3\n"
"Matching palette,#a1b2c3,#A1B2C3,\n",
encoding="utf-8",
)
output = subprocess.check_output(
[sys.executable, "-B", str(scripts / "search.py"),
"#A1b2C3", "--domain", "color", "--json"],
cwd=root,
)
result = json.loads(output)
self.assertEqual(
[row["Product Type"] for row in result["results"]], ["Matching palette"]
)
def test_read_failure_is_not_reported_as_a_search_result(self):
failure = UnicodeDecodeError("utf-8", b"\xff", 0, 1, "invalid")
with patch("core._load_csv_snapshot", side_effect=failure):