This commit is contained in:
zyp
2026-11-27 14:43:37 +08:00
parent 1ae94dfb1f
commit b18c6efe92
64 changed files with 5629 additions and 2414 deletions
+42 -11
View File
@@ -6,7 +6,7 @@ import time
import uuid
from pathlib import Path
from app.modules.work_assistant.model_gateway import ModelUnavailable
from app.modules.work_assistant.model_gateway import ModelUnavailable, model_successes
from app.modules.work_assistant.bid_timing import RECOVERY_SECONDS
@@ -21,6 +21,8 @@ def service_ready_at(service, continuous=False):
def job_ready_at(row, service, continuous=False):
if row['reason'] == 'annual_panel_retry':
return row['retry_at'] or 0
if service['recovered_at'] > (row['updated_at'] or 0):
return 0
until = row['retry_at'] or 0
@@ -67,7 +69,7 @@ class ReviewJobs:
sort_keys=True, separators=(',', ':'))).encode()).hexdigest()
def run(self, payload, rule, action, *, lease_seconds=90, continuous=False, rapid_recovery=False, guard=None, register_owner=None, progress_recovery=False,
resume_context_failures=False):
resume_context_failures=False, resume_incomplete=None, resume_ready=None):
if not 30 <= lease_seconds <= 600:
raise ValueError("Invalid review lease")
# Scope changes (including date) invalidate completed results and checkpoints.
@@ -82,23 +84,27 @@ class ReviewJobs:
row = db.execute('SELECT * FROM review_job WHERE job_key=?', (key,)).fetchone()
service = db.execute('SELECT * FROM review_service WHERE id=1').fetchone()
probe = service['failures'] >= 3
ready_checkpoint = None
if row:
if row['status'] == 'ready':
return json.loads(row['result'])
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 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'
and row['reason'] in {'validation', 'attempt_limit', 'profile_context_limit',
'direct_output_truncated', 'gateway_output_truncated'})
reopen = context_resume or profile_resume
internal_resume = progress_recovery and resume_context_failures and row['status']=='suspended' and row['reason']=='internal'
reopen = context_resume or profile_resume or internal_resume
if row['status'] == 'suspended' and not reopen and not (continuous and row['reason'] in {'validation', 'attempt_limit'}):
raise ModelUnavailable('review_suspended')
if row['lease_until'] and row['lease_until'] > now:
raise ModelUnavailable('review_in_progress', retry_after=row['lease_until'] - now)
recovered = service['recovered_at'] > row['updated_at']
ready_at = job_ready_at(row, service, continuous or rapid_recovery)
if ready_at > now and not reopen:
if ready_at > now and (not reopen or internal_resume):
raise ModelUnavailable('review_backoff', retry_after=ready_at - now)
checkpoint = json.loads(row['checkpoint']) if row['checkpoint'] else None
checkpoint = ready_checkpoint if ready_checkpoint is not None else json.loads(row['checkpoint']) if row['checkpoint'] else None
attempts = 1 if reopen or recovered or probe else row['attempts'] + 1
if progress_recovery:
# A worker can be restarted after saving but before it
@@ -120,6 +126,8 @@ class ReviewJobs:
"status='running',lease=excluded.lease,lease_until=excluded.lease_until,"
"attempts=excluded.attempts,updated_at=excluded.updated_at",
(key, lease, now + lease_seconds, attempts, now))
if ready_checkpoint is not None:
db.execute('UPDATE review_job SET checkpoint=? WHERE job_key=?',(json.dumps(ready_checkpoint,ensure_ascii=False),key))
if register_owner:
register_owner(db, key, lease, now + lease_seconds if probe else 0)
db.commit()
@@ -135,8 +143,18 @@ class ReviewJobs:
checkpoint=candidate
self.update(key, lease, guard=guard, checkpoint=json.dumps(candidate, ensure_ascii=False))
successes_before = model_successes.get()
try:
result = action(checkpoint, save)
if resume_incomplete is not None:
pending = resume_incomplete(result)
update = dict(status='queued', lease_until=0, attempts=0, retry_at=0,
reason='review_continuation' if pending else None)
if not pending:
update['checkpoint'] = None
self.update(key, lease, guard=guard, **update)
self.service_result(False, release_only=bool(pending) and model_successes.get()==successes_before, probe=probe)
return result
incomplete = result.get('requires_manual', False)
self.update(key, lease, guard=guard,
status=('suspended' if attempts >= 3 else 'failed') if incomplete else 'ready',
@@ -145,7 +163,10 @@ class ReviewJobs:
except JobDeferred:
self.update(key, lease, status='queued', lease_until=0, retry_at=time.time() + 2,
reason='review_continuation', attempts=max(0, attempts - 1))
self.service_result(False, release_only=True, probe=probe)
# A successful model substep proves the outage is over. Waiting
# for an entire multi-step report otherwise serializes the queue
# behind the recovery probe for minutes or hours.
self.service_result(False, release_only=model_successes.get()==successes_before, probe=probe)
raise
except JobPaused:
# Keep the last durable checkpoint. Hold the lease until execution
@@ -169,11 +190,19 @@ class ReviewJobs:
self.service_result(False, probe=probe)
raise
except ModelUnavailable as exc:
admission = exc.reason in {'circuit_open', 'queue_full', 'queue_timeout'}
admission = exc.reason in {'circuit_open', 'queue_full', 'queue_timeout','token_count_unavailable'}
content_retry = exc.reason in {'annual_panel_retry','leadership_retry','annual_output_retry','role_fit_retry'}
transient = continuous or exc.retryable or admission
delay = min(900, 60 * 2 ** min(4, max(0, attempts - 1)))
if continuous or rapid_recovery:
delay = min(delay, RECOVERY_SECONDS)
if content_retry:
delay = max(delay, min(900, exc.retry_after or 300))
if progress_recovery and rapid_recovery:
# Personnel content corrections are local resumable work,
# not a model-service outage. Do not inherit a five-minute
# cooldown from an old failed validation round.
delay = min(delay, 30)
if exc.reason in {'queue_full', 'queue_timeout'}:
delay = min(delay, 5)
if continuous and exc.reason in {'direct_output_truncated', 'gateway_output_truncated'}:
@@ -185,10 +214,12 @@ class ReviewJobs:
reason=exc.reason, lease_until=0, attempts=retry_attempts,
retry_at=time.time() + delay)
exc.retry_after = delay
self.service_result(not admission and exc.retryable, release_only=admission or (continuous and not exc.retryable), probe=probe)
self.service_result(not admission and not content_retry and exc.retryable,
release_only=admission or (content_retry and model_successes.get()==successes_before) or (continuous and not exc.retryable), probe=probe)
raise
except Exception:
self.update(key, lease, status='suspended', reason='internal', lease_until=0)
self.update(key, lease, status='failed' if progress_recovery else 'suspended', reason='internal', lease_until=0,
retry_at=time.time()+300 if progress_recovery else 0, attempts=min(attempts,2) if progress_recovery else attempts)
self.service_result(False, release_only=True, probe=probe)
raise
self.service_result(False, probe=probe)