fix: escape all LIKE specials in step-row cleanup

This commit is contained in:
2026-07-17 01:17:02 -04:00
parent 16993ed362
commit b3aba7c31d
2 changed files with 43 additions and 3 deletions
+20 -3
View File
@@ -602,6 +602,21 @@ def _write_skip_report(
)
def _escape_sql_like(text: str) -> str:
"""转义 SQL LIKE 模式中的全部特殊字符(`\\`、`%`、`_`)为字面匹配。
参数:
text: 待作为 LIKE 前缀字面使用的原始字符串。
返回:
可安全拼入 `LIKE ? ESCAPE '\\'` 模式的转义串。
关键实现细节:
反斜杠必须最先转义,否则会二次转义后续替换产生的转义符。
"""
return text.replace("\\", "\\\\").replace("%", r"\%").replace("_", r"\_")
def _clear_step_rows(db_path: str, *, baseline_run_id: str, epoch: int, step: int) -> None:
"""清空一个 step 的全部旧行(rollout + gate 派生),保证崩溃重跑幂等。
@@ -621,13 +636,15 @@ def _clear_step_rows(db_path: str, *, baseline_run_id: str, epoch: int, step: in
无。
关键实现细节:
predictions/traces 的 gate 行按 LIKE 前缀删除,'_' 通配显式转义
(ESCAPE)钉死字面匹配,避免 `..._s1` 误匹配 `..._s10` 类前缀陷阱。
predictions/traces 的 gate 行按 LIKE 前缀删除,`\\`/`%`/`_` 三个 LIKE
特殊字符全部显式转义(ESCAPE)钉死字面匹配,避免 `..._s1` 误匹配
`..._s10` 类前缀陷阱,也防 run_id 含 `%`/`\\` 时通配误删他 run 行。
"""
from app.harness.inference import PREDICTIONS_SCHEMA, TRACES_SCHEMA
from app.harness.log import HarnessLog
step_run_id = f"{baseline_run_id}_e{epoch}_s{step}"
escaped = _escape_sql_like(step_run_id)
with HarnessLog(db_path, step_run_id, register_run=False) as log:
log.create_table("predictions", PREDICTIONS_SCHEMA)
log.create_table("traces", TRACES_SCHEMA)
@@ -635,7 +652,7 @@ def _clear_step_rows(db_path: str, *, baseline_run_id: str, epoch: int, step: in
log.execute(f"DELETE FROM {table} WHERE run_id=?", (step_run_id,))
log.execute(
f"DELETE FROM {table} WHERE run_id LIKE ? ESCAPE '\\'",
(step_run_id.replace("_", r"\_") + r"\_gate\_%",),
(escaped + r"\_gate\_%",),
)
for table in ("gate_evidence", "quadrant_pair"):
exists = log.query(