feat: persist verification tasks inputs and attempts

This commit is contained in:
阿白叔 committed 2026-11-17 15:22:10 +08:00
1 parent 0406028a02
commit f5d39f6261
3 files changed
+270

No files matched your search

@@ -65,6 +65,8 @@ class StoreSchema:
from .execution_journal import create_execution_schema
create_history_schema(self._store._connection)
create_execution_schema(self._store._connection)
from .verification.store import create_schema as create_verification_schema
create_verification_schema(self._store._connection)
def _create_schema_v1(self) -> None:
statements = (
@@ -0,0 +1,185 @@
"""Transactional verification records; replacement receipts are never modified."""
from datetime import datetime, timezone
import json
from uuid import uuid4
from .contracts import ACTIVE, TERMINAL, digest, fail, identifier, validate_input
from ..errors import TaskCenterError
def now():
return datetime.now(timezone.utc).isoformat()
def encode(value):
return json.dumps(value, ensure_ascii=False, sort_keys=True, allow_nan=False)
def create_schema(c):
statements = (
'''CREATE TABLE IF NOT EXISTS verification_tasks (
task_id TEXT PRIMARY KEY, name TEXT NOT NULL, created TEXT NOT NULL,
view_revision INTEGER NOT NULL DEFAULT 0)''',
'''CREATE TABLE IF NOT EXISTS verification_items (
item_id TEXT PRIMARY KEY, task_id TEXT NOT NULL REFERENCES verification_tasks(task_id),
item_key TEXT NOT NULL, project_key TEXT NOT NULL, before_id TEXT,
display_name TEXT NOT NULL, current_version INTEGER NOT NULL,
UNIQUE(task_id,item_key))''',
'''CREATE TABLE IF NOT EXISTS verification_input_revisions (
item_id TEXT NOT NULL REFERENCES verification_items(item_id), input_version INTEGER NOT NULL,
payload_json TEXT NOT NULL, payload_digest TEXT NOT NULL, created TEXT NOT NULL,
PRIMARY KEY(item_id,input_version))''',
'''CREATE TABLE IF NOT EXISTS verification_attempts (
attempt_id TEXT PRIMARY KEY, item_id TEXT NOT NULL, input_version INTEGER NOT NULL,
generation INTEGER NOT NULL, state TEXT NOT NULL, owner_id TEXT, run_id TEXT,
result_json TEXT, reason TEXT NOT NULL DEFAULT '', resource_state TEXT NOT NULL DEFAULT 'not_started',
created TEXT NOT NULL, updated TEXT NOT NULL,
FOREIGN KEY(item_id,input_version) REFERENCES verification_input_revisions(item_id,input_version))''',
'''CREATE UNIQUE INDEX IF NOT EXISTS verification_active_attempt ON verification_attempts(item_id,input_version)
WHERE state IN ('queued','running','publishing','stopping')''',
'''CREATE TABLE IF NOT EXISTS verification_runs (
run_id TEXT PRIMARY KEY, task_id TEXT NOT NULL REFERENCES verification_tasks(task_id),
members_json TEXT NOT NULL, stop_requested INTEGER NOT NULL DEFAULT 0,
state TEXT NOT NULL DEFAULT 'queued', created TEXT NOT NULL)''',
'''CREATE TABLE IF NOT EXISTS verification_requests (
kind TEXT NOT NULL, request_id TEXT NOT NULL, request_hash TEXT NOT NULL, result_json TEXT NOT NULL,
PRIMARY KEY(kind,request_id))''',
)
for statement in statements: c.execute(statement)
class VerificationRepository:
def __init__(self, task_store):
self.store = task_store
with task_store.transaction(): create_schema(task_store._connection)
@property
def connection(self): return self.store._connection
def _request(self, kind, request_id, body):
identifier(request_id)
row = self.connection.execute('SELECT * FROM verification_requests WHERE kind=? AND request_id=?',
(kind,request_id)).fetchone()
if row:
if row['request_hash'] != digest(body): fail('相同请求标识对应不同内容。','verification_conflict')
return json.loads(row['result_json'])
def _remember(self, kind, request_id, body, result):
self.connection.execute('INSERT INTO verification_requests VALUES (?,?,?,?)',
(kind,request_id,digest(body),encode(result)))
return result
def _touch(self, task_id):
self.connection.execute('UPDATE verification_tasks SET view_revision=view_revision+1 WHERE task_id=?',(task_id,))
def _one(self, table, key, value):
with self.store._lock:
row = self.connection.execute(f'SELECT * FROM {table} WHERE {key}=?',(value,)).fetchone()
if row is None: fail('找不到校验记录。','verification_not_found')
return dict(row)
def task(self, task_id): return self._one('verification_tasks','task_id',task_id)
def item(self, item_id): return self._one('verification_items','item_id',item_id)
def revision(self, item_id, version):
with self.store._lock:
row = self.connection.execute('SELECT * FROM verification_input_revisions WHERE item_id=? AND input_version=?',
(item_id,version)).fetchone()
if row is None: fail('输入版本不存在。','verification_not_found')
return dict(row) | {'payload':json.loads(row['payload_json'])}
def attempt(self, attempt_id):
row = self._one('verification_attempts','attempt_id',attempt_id)
row['result'] = json.loads(row['result_json']) if row['result_json'] else None
return row
def latest_attempt(self, item_id, version):
with self.store._lock:
row = self.connection.execute('SELECT attempt_id FROM verification_attempts WHERE item_id=? AND input_version=? ORDER BY generation DESC LIMIT 1',
(item_id,version)).fetchone()
return self.attempt(row[0]) if row else None
def create_task(self, name, *, request_id):
name = identifier(name).strip()
with self.store.transaction():
old = self._request('create',request_id,name)
if old is not None: return old
task_id = uuid4().hex
self.connection.execute('INSERT INTO verification_tasks(task_id,name,created) VALUES (?,?,?)',(task_id,name,now()))
return self._remember('create',request_id,name,self.task(task_id))
def publish(self, task_id, inputs, *, submission_id):
body = {'task_id':task_id,'inputs':inputs}
with self.store.transaction():
self.task(task_id)
old = self._request('publish',submission_id,body)
if old is not None: return old
result = {key:[] for key in ('accepted','duplicates','stale_ignored','rejected')}
for index,value in enumerate(inputs):
try:
row = validate_input(value)
item = self.connection.execute('SELECT * FROM verification_items WHERE task_id=? AND item_key=?',
(task_id,row['item_key'])).fetchone()
before_id = row['before_ref']['manifest_id'] if row['before_ref'] else None
if item:
if item['project_key'] != row['project_key'] or (item['before_id'] and item['before_id'] != before_id):
fail('工程身份或原始基线变化,请新增比较项。','verification_conflict')
item_id = item['item_id']
existing = self.connection.execute('SELECT payload_digest FROM verification_input_revisions WHERE item_id=? AND input_version=?',
(item_id,row['input_version'])).fetchone()
ref = {'item_id':item_id,'input_version':row['input_version']}
if existing:
if existing[0] != row['payload_digest']: fail('同一版本内容冲突。','verification_conflict')
result['duplicates'].append(ref); continue
if row['input_version'] < item['current_version']:
result['stale_ignored'].append(ref); continue
else:
item_id = uuid4().hex
self.connection.execute('INSERT INTO verification_items VALUES (?,?,?,?,?,?,?)',
(item_id,task_id,row['item_key'],row['project_key'],before_id,row['display_name'],row['input_version']))
self.connection.execute('INSERT INTO verification_input_revisions VALUES (?,?,?,?,?)',
(item_id,row['input_version'],encode(row),row['payload_digest'],now()))
self.connection.execute('UPDATE verification_items SET current_version=?,before_id=COALESCE(before_id,?),display_name=? WHERE item_id=?',
(row['input_version'],before_id,row['display_name'],item_id))
result['accepted'].append({'item_id':item_id,'input_version':row['input_version']})
self._touch(task_id)
except TaskCenterError as error:
result['rejected'].append({'index':index,'code':error.code,'reason':error.message})
return self._remember('publish',submission_id,body,result)
def create_attempt(self, item_id, version, *, request_id, run_id=None):
body = [item_id,version]
with self.store.transaction():
old = self._request('attempt',request_id,body)
if old is not None: return old
item = self.item(item_id)
revision = self.revision(item_id,version)
if revision['payload']['input_state'] != 'ready': fail('前后输入不可用。')
current = self.latest_attempt(item_id,version)
if current and current['state'] in ACTIVE:
return self._remember('attempt',request_id,body,current)
generation = (current['generation'] + 1) if current else 1
attempt_id, stamp = uuid4().hex, now()
self.connection.execute('''INSERT INTO verification_attempts
(attempt_id,item_id,input_version,generation,state,run_id,created,updated)
VALUES (?,?,?,?,'queued',?,?,?)''',(attempt_id,item_id,version,generation,run_id,stamp,stamp))
self._touch(item['task_id'])
return self._remember('attempt',request_id,body,self.attempt(attempt_id))
def claim(self, attempt_id, *, owner_id):
with self.store.transaction():
changed = self.connection.execute("UPDATE verification_attempts SET state='running',owner_id=?,updated=? WHERE attempt_id=? AND state='queued'",
(identifier(owner_id),now(),attempt_id)).rowcount
return self.attempt(attempt_id) if changed else None
def finish(self, attempt_id, *, generation, result):
with self.store.transaction():
attempt = self.attempt(attempt_id)
if attempt['generation'] != generation or attempt['state'] not in {'running','publishing'}: return False
if result.get('resource_state') != 'released': fail('资源退出尚未确认。','verification_resources')
state = result.get('attempt_state','succeeded')
if state not in TERMINAL: fail('无效校验终态。')
self.connection.execute('UPDATE verification_attempts SET state=?,result_json=?,resource_state=?,updated=? WHERE attempt_id=?',
(state,encode(result),'released',now(),attempt_id))
self._touch(self.item(attempt['item_id'])['task_id'])
return True