This commit is contained in:
zyp
2026-11-25 17:25:46 +08:00
parent c27f84cf91
commit 066a5023b7
11 changed files with 767 additions and 53 deletions
+18 -5
View File
@@ -66,7 +66,8 @@ class ReviewJobs:
return hashlib.sha256((rule + '\n' + json.dumps(payload, ensure_ascii=False,
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):
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):
if not 30 <= lease_seconds <= 600:
raise ValueError("Invalid review lease")
# Scope changes (including date) invalidate completed results and checkpoints.
@@ -84,16 +85,25 @@ class ReviewJobs:
if row:
if row['status'] == 'ready':
return json.loads(row['result'])
if row['status'] == 'suspended' and not (continuous and row['reason'] in {'validation', 'attempt_limit'}):
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
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:
if ready_at > now and not reopen:
raise ModelUnavailable('review_backoff', retry_after=ready_at - now)
checkpoint = json.loads(row['checkpoint']) if row['checkpoint'] else None
attempts = 1 if recovered or probe else row['attempts'] + 1
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
# releases its lease. Recover that same attempt safely.
attempts = min(attempts, 3)
else:
checkpoint, attempts = None, 1
ready_at = max(service_ready_at(service, continuous or rapid_recovery), service['probe_until'])
@@ -168,8 +178,11 @@ class ReviewJobs:
delay = min(delay, 5)
if continuous and exc.reason in {'direct_output_truncated', 'gateway_output_truncated'}:
delay = min(60, 10 * 2 ** min(3, max(0, attempts - 1)))
# A durable personnel step should survive an outage even when
# other requests succeed between its model attempts.
retry_attempts = min(attempts, 2) if progress_recovery and exc.retryable else attempts - int(admission)
self.update(key, lease, status='failed' if transient else 'suspended',
reason=exc.reason, lease_until=0, attempts=attempts - int(admission),
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)