fix: read-only baseline queries skip _runs upsert (register_run flag)
This commit is contained in:
+32
-18
@@ -52,6 +52,9 @@ class HarnessLog:
|
||||
run_id: 本次运行的唯一标识。
|
||||
git_sha: 代码版本,默认自动获取。
|
||||
config_snapshot: 本次运行的配置快照。
|
||||
register_run: 是否注册运行(upsert _runs + 退出时同步 status)。默认 True;
|
||||
只读查询已有 run(如基线预测回读)时传 False,避免把该 run 的
|
||||
started_at/config/status 改写、把基线元数据污染成本次进程的运行状态。
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@@ -60,27 +63,33 @@ class HarnessLog:
|
||||
run_id: str,
|
||||
git_sha: str | None = None,
|
||||
config_snapshot: dict[str, Any] | None = None,
|
||||
*,
|
||||
register_run: bool = True,
|
||||
) -> None:
|
||||
self._run_id = run_id
|
||||
self._register_run = register_run
|
||||
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
self._conn = sqlite3.connect(db_path, check_same_thread=False)
|
||||
self._lock = threading.Lock()
|
||||
self._conn.row_factory = sqlite3.Row
|
||||
self._conn.execute("PRAGMA journal_mode=WAL")
|
||||
self._init_fixed_tables()
|
||||
resolved_sha = git_sha or _get_git_sha()
|
||||
config_json = json.dumps(config_snapshot, ensure_ascii=False) if config_snapshot else None
|
||||
self._conn.execute(
|
||||
"INSERT INTO _runs"
|
||||
" (run_id, git_sha, started_at, config, status)"
|
||||
" VALUES (?, ?, ?, ?, ?)"
|
||||
" ON CONFLICT(run_id) DO UPDATE SET"
|
||||
" started_at=excluded.started_at,"
|
||||
" config=excluded.config,"
|
||||
" status=excluded.status",
|
||||
(run_id, resolved_sha, _now_iso(), config_json, "running"),
|
||||
)
|
||||
self._conn.commit()
|
||||
if register_run:
|
||||
resolved_sha = git_sha or _get_git_sha()
|
||||
config_json = (
|
||||
json.dumps(config_snapshot, ensure_ascii=False) if config_snapshot else None
|
||||
)
|
||||
self._conn.execute(
|
||||
"INSERT INTO _runs"
|
||||
" (run_id, git_sha, started_at, config, status)"
|
||||
" VALUES (?, ?, ?, ?, ?)"
|
||||
" ON CONFLICT(run_id) DO UPDATE SET"
|
||||
" started_at=excluded.started_at,"
|
||||
" config=excluded.config,"
|
||||
" status=excluded.status",
|
||||
(run_id, resolved_sha, _now_iso(), config_json, "running"),
|
||||
)
|
||||
self._conn.commit()
|
||||
|
||||
def _init_fixed_tables(self) -> None:
|
||||
"""创建 _runs 和 _events 固定表。"""
|
||||
@@ -217,13 +226,18 @@ class HarnessLog:
|
||||
|
||||
参数:
|
||||
status: 最终状态,"completed" 或 "failed"。
|
||||
|
||||
关键实现:
|
||||
register_run=False(只读打开)时跳过 status 更新,仅关闭连接,
|
||||
避免只读回读把已有 run 的 finished_at/status 改写。
|
||||
"""
|
||||
with self._lock:
|
||||
self._conn.execute(
|
||||
"UPDATE _runs SET finished_at = ?, status = ? WHERE run_id = ?",
|
||||
(_now_iso(), status, self._run_id),
|
||||
)
|
||||
self._conn.commit()
|
||||
if self._register_run:
|
||||
self._conn.execute(
|
||||
"UPDATE _runs SET finished_at = ?, status = ? WHERE run_id = ?",
|
||||
(_now_iso(), status, self._run_id),
|
||||
)
|
||||
self._conn.commit()
|
||||
self._conn.close()
|
||||
|
||||
def __enter__(self) -> HarnessLog:
|
||||
|
||||
Reference in New Issue
Block a user