Skip to content

cron: the cron lane: cap concurrent cron runs and pause for the person - #804

Open
randuev wants to merge 1 commit into
ClickHouse:mainfrom
randuev:randuev/cron-lane
Open

randuev wants to merge 1 commit into
ClickHouse:mainfrom
randuev:randuev/cron-lane

Conversation

@randuev

@randuev randuev commented Dec 4, 2026 •

Copy link
Copy Markdown

Why

Where every session shares one model server that serves one request at a time (a local one, or any serialized backend), cron runs overlapping each other or an interactive turn buy no parallelism: they pile into the same queue, and every run pays the wait of the ones ahead of it. This adds the cron lane: a run cap, a pause that hands the model to the person, and a per-job policy for held fires.

What

Three settings, all read at every lane decision, so a change follows a config reload without a restart:

  • cron.max_concurrent (default 4, 0 uncapped): how many agent cron runs may execute at once. Runs past the cap wait on the lane with their run-log row open, so the wait reads as a run in progress, not a fire that went missing. Workflow jobs take no slot (the run has its own workflows.max_concurrent_runs), source runners hold no model turn.
  • cron.pause_when_interactive_busy (+ cron.interactive_grace_minutes, default 5): while a person's session is mid-turn — or less than the grace after the last one ended — a due fire does not start. A person's session is any session that is neither cron: nor workflow:. Manual runs never wait.
  • CronJob.on_busy (queue default / skip / protect): a held fire defers and runs when the lane quiets (deferral order, one at a time, busy check re-made before each, one queued entry per job, superseded by a real fire); or waits for its next fire; or runs regardless and is never preempted.

Yielding the model: an interactive turn starting reclaims the model — the agent cron runs in flight are stopped the way Stop stops a web turn (protect aside). A run whose turn starts into the busy lane after the hand-over already ran (cron turns queue on the engine's semaphore before registering as running) is stopped by that same hand-over at its own start event.

Log honesty: every outcome where the run produced nothing — deferred, skipped, yielded, cancelled — closes its row as the neutral missed status, the same one a scheduler-dropped fire carries. success would fool the interval anchor and the startup catch-up into counting the work as done; error would raise health notices for a pause working as configured. Cancelled runs close their own row from inside the cancellation rather than leaving it "going" forever.

Config coercion follows the house pattern: from_dict keeps the written value (floored at 0), the service getters apply the ceilings at every decision (so a reload of a changed value applies), and nerve config validate warns above the ceilings (64 / 240).

Tests

tests/test_cron_lane.py: 36 tests — config coercion and validate warnings, on_busy round-trip, lane serialization and per-decision limit reads, busy/grace accounting, defer-then-resume, skip, protect, manual passthrough, preemption (including a stop that answers only after the run completed, a killed leg, and the start-into-a-busy-lane path), cancelled-behind-the-cap row closing, scan survival across a cancelled deferred leg, listener wiring.

Targeted regression (all test_cron*, test_config*, workflow-cron, docs-cron-paths, lockdown, cli-reload, usage-by-cron-job): 1626 passed, 3 skipped.

Docs: docs/cron.md gains the "The cron lane" section and the on_busy job field; docs/config.md gains the key table and the hot-reload row.

🤖 Generated with Claude Code


Note

Medium Risk
Touches core cron execution, session preemption via stop_turn, and run-log semantics on shared model servers—high-impact scheduling path but bounded by defaults and extensive tests.

Overview
Adds a cron lane so scheduled agent jobs don't monopolize a shared, serial model server. Global settings cron.max_concurrent (default 4, 0 = uncapped), cron.pause_when_interactive_busy, and cron.interactive_grace_minutes are read on every lane decision and apply on config reload without restart.

CronJob.on_busy (queue / skip / protect) defines what happens when a fire is due during the pause: defer until the lane is quiet, skip until the next fire, or run without preemption. Manual runs bypass the pause.

CronService implements a slot-limited _CronLane, tracks interactive sessions via the engine's running listener, defers jobs with a resume scan, and preempts in-flight cron runs (except protect) when a person starts a turn—logging yields, skips, and slot waits as missed so catch-up and health notices stay honest.

Validation warns when values exceed ceilings (64 concurrent / 240 grace minutes). Documentation and tests/test_cron_lane.py cover config, deferral, preemption, and shutdown behavior.

Reviewed by Cursor Bugbot for commit b6257cd. Bugbot is set up for automated code reviews on this repo. Configure here.

…erson

Where every session shares one model server that serves one request at a
time, cron runs overlapping each other or an interactive turn buy no
parallelism: they pile into the same queue and every run pays the wait.

Three settings, all read at every lane decision so a reload applies
without a restart:

- cron.max_concurrent caps how many agent cron runs execute at once
  (default 4; 0 uncapped). Runs past the cap wait with their run-log row
  open. Workflow jobs and source runners take no slot; each has its own
  cap.
- cron.pause_when_interactive_busy holds a due fire while a person's
  session is mid-turn, and cron.interactive_grace_minutes (default 5)
  after the last one ended. Manual runs never wait.
- CronJob.on_busy says what a held fire does: queue (defer and run when
  the lane quiets, in deferral order), skip (wait for the next fire), or
  protect (run regardless, never preempted).

An interactive turn starting reclaims the model: agent cron runs in
flight are stopped the way Stop stops a web turn (protect aside), and
runs whose turn starts into the busy lane are stopped by the same
hand-over at their own start event. Every outcome where the run produced
nothing (deferred, skipped, yielded, cancelled) closes its row as the
neutral missed status, never success or error: success would fool the
interval anchor and the startup catch-up, and a pause is not a failure.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@cla-assistant

cla-assistant Bot commented Dec 4, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit b6257cd. Configure here.

Comment thread nerve/cron/service.py
if session_id is not None:
self._lane_sessions.pop(session_id, None)
self._preempted.discard(session_id)
await self._lane.release()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard stop skips lane cleanup

High Severity

A hard stop cancels the cron run's task, and that cancellation stays pending until uncancel() is called. The handler then awaits log_cron_finish and _lane.release without clearing it, so those awaits are cancelled too: the log row stays open and the lane slot is never returned. A deferred run is the scan task itself, so swallowing the error in _resume_deferred also drops or strands the rest of the queue.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b6257cd. Configure here.

Comment thread nerve/cron/service.py
logger.info(
"Not running the deferred %s: a run of it is still going", job_id,
)
return False

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred run overlaps scheduled fire

Medium Severity

_resume_deferred removes a job from _deferred before the run starts, and that run is not a scheduler instance. _may_start blocks a scheduled fire only for an in-flight catch-up or delivery, not for origin deferred. After the deferred run has begun, the job's next scheduled fire starts a second run beside it.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b6257cd. Configure here.

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

AI review gate — Cursor Bugbot found issues

  • Cursor Bugbot found issues that are not resolved (check concluded 'neutral').

Unresolved findings (2):

What to do:

  • Fix the findings and push. The reviewer re-reviews the new commit and the gate re-runs by itself.
  • A finding you think is wrong: a maintainer of the repository (maintain or admin role, the author included) replies in its thread with why and resolves it, then re-run the gate.
  • Finding wrong, or reviewer stuck? Ask a human with write access who is not the PR author to approve this exact commit, then re-run the gate. Their approval stands in for the AI review (a new push needs a new approval).

Re-run the gate with “Re-run jobs” after an approval or a finished review. A push re-runs it automatically.

@nerveai

nerveai commented Dec 4, 2026 •

Copy link
Copy Markdown

Nerve PR review · 🟡 Merge after small fixes

This adds a cron lane: a cap on concurrent agent cron runs, an opt-in pause while a person is mid-turn, per-job on_busy, and preemption of cron runs when a person's turn starts. It reads its limits per decision, uses the existing running listener and writes honest missed rows. It is a good fit for serialized model servers.

Completeness: 36 tests, docs, validate warnings; no migration.

Before merge:

  • Bugbot's two threads fail the AI gate. The _may_start overlap holds. The hard-stop one is half right: the cleanup awaits do run, but the deferred queue dies. A deferred leg runs inline in the scan (nerve/cron/service.py:1040), so Stop targets the scan task (nerve/agent/engine.py:4600). interrupt_turn waits up to 5 s, then cancels it (nerve/agent/sessions.py:755-764) while it sleeps out the busy lane (service.py:1031). The remaining entries wait for another deferral to re-arm the scan (:976). Run each leg in its own task.

Worth considering:

  • Any session that is not cron: or workflow: counts as a person (service.py:823), so impl- and hook: turns also preempt crons; session_origin already knows the trigger. A yielded queue run is dropped, not re-queued (:2616).
  • The default max_concurrent: 4 (nerve/config.py:2938) caps every install, and with no run timeout four wedged runs stall all crons. A default of 0 keeps it opt-in.

Checked: full suite in an offline sandbox at b6257cd (3 chunks): 12037 passed, 41 skipped; test_slack_integration.py excluded (hangs offline on main too); read the lane and run path, stop_turn/interrupt_turn; CI: AI gate red on Bugbot, CLA pending · automated review by nerveai — mention @nerveai to ask a follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants