Skip to content

The agent that grows with you

fix(cron): receipt recovery books blank and [CRON_FAILURE] answers as failures

Tekniumf0f0907a640e
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 33d55f23efc..c10f7a89ae5 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -3476,10 +3476,8 @@ def _run_one_job_body(
_record_fire_ownership_lost(job, fire_owner, execution_id)
return True
- # Empty final_response is a soft failure so last_status is not "ok".
- if d.success and not final_response.strip():
- d.success = False
- d.error = "Agent completed but produced empty response (model error, timeout, or misconfiguration)"
+ from cron.scheduler_bookkeeping import fail_empty_response
+ fail_empty_response(d, final_response)
if _fire_claim_ownership_lost():
# #105861: the claim check is one sample; a miss AFTER a completed delivery must not
diff --git a/cron/scheduler_authority.py b/cron/scheduler_authority.py
index f87b434ab1d..148d4c71594 100644
--- a/cron/scheduler_authority.py
+++ b/cron/scheduler_authority.py
@@ -108,7 +108,9 @@ def reconcile_pending(*, allow_connect=True):
from hermes_cli.gateway_client import connect_gateway
from cron.executions import execution_owner_live
from cron.jobs import pause_job
- from cron.scheduler import _RunDelivery, _FireOwnership, _save_compose_deliver, _finish_completed_run
+ from cron.scheduler import (_RunDelivery, _FireOwnership, _apply_agent_failure_marker,
+ _save_compose_deliver, _finish_completed_run)
+ from cron.scheduler_bookkeeping import fail_empty_response
from utils import atomic_json_write
import logging
@@ -147,12 +149,16 @@ def reconcile_pending(*, allow_connect=True):
success, output, answer, error = state['result']
job = dict(state['job'], **(state.get('job_flags') or {}))
job['execution_id'] = params['request_id']
- delivery = _RunDelivery(job, success, error)
+ # The same result verdicts as the ordinary tail: an agent-declared [CRON_FAILURE] and a
+ # blank answer are failures, whichever process books them.
+ success, error, declared = _apply_agent_failure_marker(job, success, error, answer)
+ delivery = _RunDelivery(job, success, error, agent_declared=declared)
# The canonical receipt supplies the result; a dead scheduler's fire claim does not
# authorize replay and must not prevent finishing its durable bookkeeping.
fence = _FireOwnership(dict(job, fire_claim=None))
_save_compose_deliver(delivery, fence, answer, output, adapters=None, loop=None,
verbose=False, execution_token=None)
+ fail_empty_response(delivery, answer)
_finish_completed_run(delivery, None, params['request_id'], recovered=True)
except Exception:
logging.getLogger(__name__).warning('Cron receipt recovery deferred: %s', journal, exc_info=True)
diff --git a/cron/scheduler_bookkeeping.py b/cron/scheduler_bookkeeping.py
index 37a11277c60..24d756140a6 100644
--- a/cron/scheduler_bookkeeping.py
+++ b/cron/scheduler_bookkeeping.py
@@ -1,5 +1,15 @@
"""Terminal bookkeeping phases shared by scheduler execution paths."""
+EMPTY_RESPONSE_ERROR = "Agent completed but produced empty response (model error, timeout, or misconfiguration)"
+
+
+def fail_empty_response(d, final_response):
+ """A "successful" run with a blank answer is a soft failure (never ``ok``): the ordinary tail and
+ receipt recovery both book it, so a recovered whitespace answer cannot turn the job green."""
+ if d.success and not str(final_response or "").strip():
+ d.success = False
+ d.error = EMPTY_RESPONSE_ERROR
+
def finish_interrupted_run(job, execution_id, delivery_error):
"""Shutdown already advanced this run; update a failed notice without spending another fire."""
diff --git a/tests/cron/test_canonical_receipt_bookkeeping.py b/tests/cron/test_canonical_receipt_bookkeeping.py
index f325f6ba608..ff28188e09e 100644
--- a/tests/cron/test_canonical_receipt_bookkeeping.py
+++ b/tests/cron/test_canonical_receipt_bookkeeping.py
@@ -115,3 +115,26 @@ def test_direct_retry_reuses_generated_fire_identity(monkeypatch, initial):
with pytest.raises(scheduler_authority.CronExecutionUnknown):
scheduler_authority.run_canonical_job(job)
assert requests[0] == requests[1]
+
+
[email protected]('answer', [' \n', '[CRON_FAILURE]\nchild task failed'], ids=['blank', 'declared'])
+def test_recovered_receipt_books_the_same_failure_verdicts_as_the_firer(tmp_path, monkeypatch, answer):
+ """A blank answer and an agent-declared [CRON_FAILURE] fail the run on the ordinary tail;
+ receipt recovery must not book the same result green (ok / delivery_queued -> ok)."""
+ with jobs.use_cron_store(tmp_path / 'cron'):
+ monkeypatch.setattr('cron.delivery_queue.DELIVERY_DB', tmp_path / 'deliveries.db')
+ job = jobs.create_job(prompt='report', schedule='every 1h', deliver='telegram:1')
+ execution = executions.create_execution(job['id'], source='test')
+ executions.mark_execution_running(execution['id'])
+ with executions._transaction() as conn:
+ conn.execute('UPDATE executions SET process_id=?,pid=? WHERE id=?', ('old-owner', 999999, execution['id']))
+ params = {'job_id': job['id'], 'request_id': execution['id'], 'extra_prompt': None}
+ journal = scheduler_authority.journal_path(job['id'], execution['id'])
+ journal.parent.mkdir(parents=True, exist_ok=True)
+ journal.write_text(json.dumps({'params': params, 'receipt': None}))
+ peer(monkeypatch, lambda method, params: {'status': 'terminal', 'job': job,
+ 'result': [True, 'document', answer, None]})
+ scheduler_authority.reconcile_pending()
+ saved = jobs.get_job(job['id'])
+ assert (saved['last_status'], saved['failure_streak']) == ('error', 1)
+ assert executions.get_execution(execution['id'])['status'] == 'failed'