fix(work_assistant): 修复审核任务并发初始化与超时问题
- review_jobs/bid_queue 增加 schema 锁与双重检查初始化 - 调用失败时关闭连接,避免资源泄漏 - review_commentary 复用阶段剩余超时预算,避免误重启 - routes 捕获 SQLite BUSY/LOCKED 并返回 503 重试响应 - submission_advice 精简数据模型与提示语,聚焦用途校验
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
import hashlib
|
||||
import json
|
||||
import sqlite3
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
@@ -49,19 +50,33 @@ class JobValidationBlocked(ValueError):
|
||||
class ReviewJobs:
|
||||
def __init__(self, path=None):
|
||||
self.path = Path(path) if path else Path(__file__).resolve().parents[3] / '.private' / 'review-jobs.sqlite3'
|
||||
self._schema_lock = threading.Lock()
|
||||
self._initialized = False
|
||||
|
||||
def connect(self):
|
||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||
db = sqlite3.connect(self.path, timeout=5)
|
||||
db.row_factory = sqlite3.Row
|
||||
db.execute('CREATE TABLE IF NOT EXISTS review_job (job_key TEXT PRIMARY KEY, '
|
||||
'status TEXT NOT NULL, lease TEXT, lease_until REAL, attempts INTEGER NOT NULL DEFAULT 0, '
|
||||
'retry_at REAL NOT NULL DEFAULT 0, reason TEXT, checkpoint TEXT, result TEXT, updated_at REAL)')
|
||||
db.execute('CREATE TABLE IF NOT EXISTS review_service (id INTEGER PRIMARY KEY, failures INTEGER, '
|
||||
'open_until REAL, probe_until REAL, recovered_at REAL)')
|
||||
db.execute('INSERT OR IGNORE INTO review_service VALUES(1,0,0,0,0)')
|
||||
db.commit()
|
||||
return db
|
||||
try:
|
||||
if not self._initialized:
|
||||
with self._schema_lock:
|
||||
if not self._initialized:
|
||||
# Readers must not delay checkpoint writers. Migrate the
|
||||
# journal in place; existing jobs and results stay intact.
|
||||
if db.execute('PRAGMA journal_mode').fetchone()[0].lower() != 'wal':
|
||||
db.execute('PRAGMA journal_mode=WAL')
|
||||
db.execute('CREATE TABLE IF NOT EXISTS review_job (job_key TEXT PRIMARY KEY, '
|
||||
'status TEXT NOT NULL, lease TEXT, lease_until REAL, attempts INTEGER NOT NULL DEFAULT 0, '
|
||||
'retry_at REAL NOT NULL DEFAULT 0, reason TEXT, checkpoint TEXT, result TEXT, updated_at REAL)')
|
||||
db.execute('CREATE TABLE IF NOT EXISTS review_service (id INTEGER PRIMARY KEY, failures INTEGER, '
|
||||
'open_until REAL, probe_until REAL, recovered_at REAL)')
|
||||
db.execute('INSERT OR IGNORE INTO review_service VALUES(1,0,0,0,0)')
|
||||
db.commit()
|
||||
self._initialized = True
|
||||
return db
|
||||
except BaseException:
|
||||
db.close()
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def key(payload, rule):
|
||||
@@ -78,6 +93,15 @@ class ReviewJobs:
|
||||
lease = uuid.uuid4().hex
|
||||
db = self.connect()
|
||||
try:
|
||||
# Full-history validation can be expensive. Do it before taking the
|
||||
# global SQLite write lock, then check the row has not changed.
|
||||
prepared = db.execute('SELECT * FROM review_job WHERE job_key=?', (key,)).fetchone()
|
||||
prepared_result, prepared_checkpoint = None, None
|
||||
if prepared and (prepared['status'] == 'ready' or
|
||||
(resume_ready and prepared['reason'] == 'internal' and prepared['result'] and not prepared['checkpoint'])):
|
||||
prepared_result = json.loads(prepared['result'])
|
||||
prepared_checkpoint = resume_ready(prepared_result) if resume_ready else None
|
||||
now = time.time()
|
||||
db.execute('BEGIN IMMEDIATE')
|
||||
if guard:
|
||||
guard(db)
|
||||
@@ -87,8 +111,10 @@ class ReviewJobs:
|
||||
ready_checkpoint = None
|
||||
if row:
|
||||
if row['status'] == 'ready' or (resume_ready and row['reason']=='internal' and row['result'] and not row['checkpoint']):
|
||||
result = json.loads(row['result'])
|
||||
ready_checkpoint = resume_ready(result) if resume_ready else None
|
||||
if prepared_result is None or prepared is None or dict(row) != dict(prepared):
|
||||
raise ModelUnavailable('review_in_progress', retry_after=1)
|
||||
result = prepared_result
|
||||
ready_checkpoint = prepared_checkpoint
|
||||
if ready_checkpoint is None and row['status']=='ready':return result
|
||||
context_resume = resume_context_failures and row['status'] == 'suspended' and row['reason'] in {'model_context_full','context_capacity'}
|
||||
profile_resume = (progress_recovery and resume_context_failures and row['status'] == 'suspended'
|
||||
|
||||
Reference in New Issue
Block a user