Skip to content

The agent that grows with you

refactor(cron): extract tick housekeeping and failure-marker helpers; log legacy drain tracebacks

Teknium16781fa2a791
diff --git a/cron/bot_chat_legacy.py b/cron/bot_chat_legacy.py
index 7e1a56b85b2..5087f199175 100644
--- a/cron/bot_chat_legacy.py
+++ b/cron/bot_chat_legacy.py
@@ -80,7 +80,7 @@ def _dispose(path: Path, record: dict) -> bool:
# The door is idempotent per id (a replay returns the existing receipt), so the
# record stays queued for the next tick; log loudly once, quietly after.
logger.log(logging.DEBUG if path in _warned else logging.WARNING,
- "Legacy Bot Chat pending record %s not handed off yet: %s", path, exc)
+ "Legacy Bot Chat pending record %s not handed off yet: %s", path, exc, exc_info=True)
_warned.add(path)
return False
receipt_status = receipt.get("status") if isinstance(receipt, dict) else None
@@ -104,7 +104,7 @@ def drain_legacy_pending() -> None:
atomic_json_write(path, record, fsync_dir=True, mode=0o600)
except Exception as exc:
logger.log(logging.DEBUG if path in _warned else logging.WARNING,
- "Legacy Bot Chat pending record %s left queued: %s", path, exc)
+ "Legacy Bot Chat pending record %s left queued: %s", path, exc, exc_info=True)
_warned.add(path)
continue
logger.info("Legacy Bot Chat pending record %s -> %s", path.name, record["status"])
diff --git a/cron/scheduler.py b/cron/scheduler.py
index eae838e381d..ba28b167b9d 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -3362,6 +3362,19 @@ def _start_owned_run(job: dict, execution_id: str) -> "Optional[contextvars.Toke
return enter_cron_execution(job, execution_id, record or {})
+def _apply_agent_failure_marker(job: dict, success: bool, error, final_response):
+ """An agent can finish its own turn after a delegated child has failed. Let it explicitly
+ declare that semantic failure so the existing failure path updates status, streaks, ledger,
+ and notification routing instead of recording a false healthy result.
+
+ Returns ``(success, error, agent_declared)``."""
+ if success and not job.get("no_agent"):
+ marker_error = _cron_failure_marker_error(final_response)
+ if marker_error is not None:
+ return False, marker_error, True
+ return success, error, False
+
+
def _run_one_job_body(
job: dict, *, adapters=None, loop=None, verbose: bool = False,
extra_prompt: Optional[str] = None, claim_lost: Optional[_CancelEventLike] = None,
@@ -3453,14 +3466,7 @@ def _run_one_job_body(
_record_fire_ownership_lost(job, fire_owner, execution_id)
return True
- # An agent can finish its own turn after a delegated child has failed. Let it explicitly
- # declare that semantic failure so the existing failure path updates status, streaks,
- # ledger, and notification routing instead of recording a false healthy result.
- agent_declared = False
- if success and not job.get("no_agent"):
- marker_error = _cron_failure_marker_error(final_response)
- if marker_error is not None:
- success, error, agent_declared = False, marker_error, True
+ success, error, agent_declared = _apply_agent_failure_marker(job, success, error, final_response)
# Agent is still live through delivery; wrap ALL of save/compose/deliver in try/finally so a
# raise anywhere still tears the deferred agent down.
diff --git a/cron/scheduler_tick.py b/cron/scheduler_tick.py
index d22330c5637..a5944db2c77 100644
--- a/cron/scheduler_tick.py
+++ b/cron/scheduler_tick.py
@@ -72,6 +72,23 @@ def _advance_or_drop_recurring(_sched, due_jobs: list) -> list:
return [j for j in due_jobs if not is_recurring(j)]
+def _pre_dispatch_housekeeping(_sched, headless: bool) -> None:
+ """Admission reconcile, legacy upgrade drain and dead-owner reap, run under the tick lock
+ before due jobs are read."""
+ from cron.scheduler_authority import reconcile_pending
+ if headless:
+ reconcile_pending(allow_connect=False)
+ else:
+ reconcile_pending()
+ # Upgrade drain for the retired CLI lane's records: never let it stop due jobs below.
+ try:
+ from cron.bot_chat_legacy import drain_legacy_pending
+ drain_legacy_pending()
+ except Exception as _legacy_exc:
+ _sched.logger.warning("Legacy Bot Chat pending drain failed: %s", _legacy_exc, exc_info=True)
+ _sched._maybe_reap_dead_owners()
+
+
def _tick_admitted(
verbose: bool = True, adapters=None, loop=None, sync: bool = True, *, can_dispatch=None,
headless: bool = False):
@@ -105,18 +122,7 @@ def _tick_admitted(
_sched.logger.debug("Cron dispatch paused while gateway drains existing work")
return 0
- from cron.scheduler_authority import reconcile_pending
- if headless:
- reconcile_pending(allow_connect=False)
- else:
- reconcile_pending()
- # Upgrade drain for the retired CLI lane's records: never let it stop due jobs below.
- try:
- from cron.bot_chat_legacy import drain_legacy_pending
- drain_legacy_pending()
- except Exception as _legacy_exc:
- _sched.logger.warning("Legacy Bot Chat pending drain failed: %s", _legacy_exc)
- _sched._maybe_reap_dead_owners()
+ _pre_dispatch_housekeeping(_sched, headless)
# Periodic worktree GC (6h, threaded) — the only sweep gateway-only boxes get.
try:
_sched._maybe_run_worktree_maintenance()