Files
2026-11-27 21:27:14 +08:00

59 lines
2.6 KiB
Python

"""Retain personnel review checkpoints/results, excluding other assistant workloads."""
import json
from pathlib import Path
import sqlite3
from contextlib import closing
def is_personnel(checkpoint, result):
for raw in (checkpoint, result):
try:
value = json.loads(raw) if raw else {}
except (ValueError, TypeError):
continue
if not isinstance(value, dict):
continue
if str(value.get('analysis_version', '')).startswith('personnel-'):
return True
if str(value.get('rule', '')).startswith('personnel-'):
return True
if isinstance(value.get('talent'), dict):
return True
return False
def retain_personnel(source, target):
source, target = Path(source).resolve(), Path(target).resolve()
if source == target or source in target.parents or target in source.parents:
raise ValueError('Source and destination must be separate runtime directories')
target.mkdir(parents=True, exist_ok=True)
stats = {'personnel_jobs': 0, 'ready': 0, 'history_pages': 0}
old = source / 'review-jobs.sqlite3'
if old.exists():
new = target / old.name
if new.exists():
raise FileExistsError('Destination review database already exists')
with closing(sqlite3.connect(old.as_uri() + '?mode=ro', uri=True, timeout=10)) as src:
schema = src.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='review_job'").fetchone()
if schema:
src.row_factory = sqlite3.Row
with closing(sqlite3.connect(new)) as dst:
dst.execute(schema[0])
for row in src.execute('SELECT * FROM review_job'):
if not is_personnel(row['checkpoint'], row['result']):
continue
dst.execute('INSERT INTO review_job VALUES(' + ','.join('?' for _ in row) + ')', tuple(row))
stats['personnel_jobs'] += 1
stats['ready'] += row['status'] == 'ready'
dst.commit()
old_cache = source / 'personnel-history.sqlite3'
if old_cache.exists():
new_cache = target / old_cache.name
if new_cache.exists():
raise FileExistsError('Destination personnel cache already exists')
with closing(sqlite3.connect(old_cache.as_uri() + '?mode=ro', uri=True, timeout=10)) as src:
with closing(sqlite3.connect(new_cache)) as dst:
src.backup(dst)
stats['history_pages'] = dst.execute('SELECT COUNT(*) FROM history_page').fetchone()[0]
return stats