Repository navigation
Closed
Description
Context
v1 supports a basic cron-like schedule per definition (issue 1 evaluator, next_run_at maintained by issue 2). The daemon already runs periodic sweeps (draft context sweep, push-rescue sweep), so the scheduler is another daemon sweep — not a new service. Every scheduled run goes through the same trigger primitive (issue 4) with the cost gate (issue 11).
Exactly-once per schedule slot matters: multiple daemons, restarts, or a slow sweep must not create two runs for the same 09:00 slot. We get this from two layers: (1) a compare-and-set claim on next_run_at and (2) an idempotency key schedule:<slot ISO> on the run (unique per definition).
Requirements
- New
src/agentRunScheduler.tsexportingrunAgentScheduleSweep(deps?)andscheduleAgentRunSweeps(intervalMs = 60_000)(returns a stop function), started fromsrc/daemon.tsnext to the existing sweeps and stopped on shutdown. - Schedule sweep:
- Select up to 50 definitions
WHERE schedule_enabled AND enabled AND next_run_at <= now ORDER BY next_run_at. - For each: compute
next = nextCronOccurrence(schedule, now); claim withUPDATE agent_definitions SET next_run_at = next WHERE id = ? AND next_run_at = <old>; skip if 0 rows (another sweep won). - Missed slots coalesce: if the daemon was down for 3 slots, only one run is created (for the latest due slot); the slot key uses the claimed
next_run_atvalue. - Re-check the owner is still an instance member (
resolveInstanceAuthorization-equivalent lookup in core:instance_membersor bootstrap admin list); if not, disable the schedule (schedule_enabled=false) and log — an offboarded user's agent must stop running. - Call
triggerAgentRun({ definition, trigger: 'schedule', triggerSource: 'schedule:<cron>', idempotencyKey: 'schedule:<slotIso>', gate: createAgentRunCostGate() }). Validation errors (AGENT_INVALID) record askippedrun with the reason so the history shows why nothing ran (add arecordSkippedRunhelper usingcreateAgentRun({ initialState: 'skipped' })).
- Select up to 50 definitions
- Deferred retry sweep (same interval):
listDueDeferredRuns(now, 50); for each re-evaluate the gate with{ deferrals }:proceed→deferred → queuedandenqueueAgentRunPhase(run, 'report').defer→ staydeferredwith newdeferred_until,incrementDeferrals. (Adddeferred → deferredhandling as a guarded same-state patch helperrescheduleDeferredRun.)skip→deferred → skippedwith reason.
- Grant cleanup sweep (every 10 minutes): revoke
mcp_records(kind='agent_run_grant')whose run is terminal or whose record expired — calls the internal revoke route from issue 9 (backstop for crashed workers). - Stuck-run recovery: runs in
running/actingwhose task (report_task_id/action_task_id) is terminal intask_historyfor more than 10 minutes →failedwith "worker stopped before recording the result" (aligns agent receipts with the existing task-state reconciler). - Tests
test/agentRunScheduler.test.tswith an in-memory DB and fake trigger/clock.
Implementation Specification
- Inject
now,trigger,enqueue,gate,isMemberfor tests. - Each definition is processed in its own try/catch; one bad definition never stops the sweep.
- Log one structured line per created/skipped/deferred run (
definitionId,slot,runId,decision).
Acceptance Criteria
- Two concurrent sweeps over the same due definition create exactly one run.
- After the daemon was down across three slots, one run is created and
next_run_atis the next future slot. - A due definition whose owner is no longer a member gets
schedule_enabled=falseand no run. - A deferred run whose provider recovered is queued and enqueued; one that stays limited past 6 deferrals is skipped.
- Daemon shutdown clears the intervals.
Created by ProPR AI Planner
Activity
propr-dev commentedon Oct 6, 2026
Suggested Implementation:
Implementation Approach
The scheduler is a daemon sweep, consistent with how ProPR already runs periodic background work (draft context sweep, push-rescue sweep). It owns only timing: deciding a slot is due and calling the trigger primitive. Everything else (validation, idempotency, cost gate, enqueue) is the shared primitive, so a scheduled run is indistinguishable from an API-triggered one except for its
triggerfield.Exactly-once per slot uses two independent guards: an optimistic claim on
next_run_at(cheap, avoids work) and the unique(definition_id, idempotency_key)index with keyschedule:<slot>(correctness even if two processes both pass the claim due to clock skew). Missed slots coalesce into one run because flooding a provider after downtime is exactly the cost risk this feature must avoid.File:
src/agentRunScheduler.ts(new file)Why this file: Time-based triggering and recovery sweeps for Agents, started by the daemon.
revokeStaleAgentRunGrants()lives insrc/jobs/agentRuns/mcpGrantClient.ts: it selectsmcp_recordsof kindagent_run_grantwhose run is terminal orexpires_at < now, and callsrevokeAgentRunMcpGrant(runId, phase)(parse<runId>:<phase>from the record id).File:
src/daemon.tsWhy this file: The daemon is where periodic intake/sweep work lives; it already starts and tears down similar sweeps.
File:
test/agentRunScheduler.test.ts(new file)Why this file: Exactly-once per slot, coalescing, offboarding and deferred retry.
propr-dev commentedon Oct 7, 2026
🤖 AI processing has started for this issue using claude agent with claude-opus-5-5 model.
I'll analyze the problem and work on a solution. This may take a few minutes.
Processing Details:
claudeclaude-opus-5-52811/claude-opus-5-5-agents-v1-12-17-cron-sche-20261007-0633-chq2800-epic-agents-v1-y7tissue-2811-20261007-0633-claude-opus-5-5-chq🔍 Track Task Execution
I've added the agent run scheduler for issue #2811. The daemon now fi…
I've added the agent run scheduler for issue #2811. The daemon now fi…