fix(harness): change _runs INSERT OR IGNORE to ON CONFLICT DO UPDATE for incremental infer

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-12 22:45:18 -04:00
parent 8b48005a17
commit 73ae1f7143
2 changed files with 41 additions and 7 deletions
+9 -4
View File
@@ -1,8 +1,9 @@
"""HarnessLogSQLite 薄包装 + RunLogImpl 只读查询端口。
HarnessLog 提供统一的结构化日志接口,从 TRM4 直搬,保留全部线程安全与幂等语义。
同 run_id 重复创建时通过 ON CONFLICT DO UPDATE 更新 started_at/config/status。
RunLogImpl 实现 core/evolution/protocols.py::RunLog Protocol,用独立连接做只读 SELECT,
不经 HarnessLog 生命周期(不触发 _runs INSERT OR IGNORE),避免污染运行状态。
不经 HarnessLog 生命周期(不触发 _runs upsert),避免污染运行状态。
"""
from __future__ import annotations
@@ -41,7 +42,7 @@ class HarnessLog:
关键设计:
- WAL 模式 + threading.Lock 保证共享连接下并发安全。
- INSERT OR IGNORE INTO _runs 保证幂等(同 run_id 多次创建不报错)
- ON CONFLICT DO UPDATE 保证幂等(同 run_id 多次创建不报错)并更新 started_at/config/status
- query 也持锁:共享连接(check_same_thread=False)下并发 SELECT + INSERT
在同一连接上 execute 会损坏游标状态,故读也须串行化。
- context manager 语义:正常退出 completed,异常退出 failed。
@@ -70,9 +71,13 @@ class HarnessLog:
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 OR IGNORE INTO _runs"
"INSERT INTO _runs"
" (run_id, git_sha, started_at, config, status)"
" VALUES (?, ?, ?, ?, ?)",
" 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()