Skip to content

Add persistent cyber loops schedules run by the service (M2.4 part B) - #160

Merged
leonardoaraujosantos merged 4 commits into
mainfrom
feat/m2-4-schedules
Nov 20, 2026
Merged

leonardoaraujosantos merged 4 commits into
mainfrom
feat/m2-4-schedules

Conversation

@leonardoaraujosantos

Copy link
Copy Markdown
Contributor

Summary

This is part B of the OpenSpec change implement-m2-4-schedules, and it completes M2.4. It adds persistent cyber loops schedules that the Cyber service runs with no client open. It also covers the unattended Mode rules, deterministic jitter, completion notifications, budgets across runs, the schedule.run.1 record, the ScheduleRun hook and usage-limit waits for scheduled runs.

It builds on part A (#159), so this PR's diff includes part A's commit until #159 is merged.

Spec requirements implemented (loops-scheduling)

Requirement What ships
Persistent local schedules cyber loops add --cron "<expr>" [--name] [--dir] [--agent] [--mode] [--model] [--new-session|--session <id>] [--notify] [--missed] [--max-cost|--max-tokens|--max-turns|--timeout] "<prompt>", plus list, show, pause, resume, run and rm. Each schedule is its own event stream (schedule.*), projected into the new schedule and schedule_run tables (new migration). Only the served App runs the sweep: every 5 s, plus nudges. Each run creates a new unattended Session, or resumes the given root Session. The prompt is admitted queued as schedi_<id>_<n>, and the outcome is recorded when the Drain goes idle. add warns when the service was not running.
Unattended Mode for schedules The default Mode is dont-ask. bypass needs loops.allow_bypass: true, and the setting must come from a global: (user) or managed: layer; project, local, env, profile and CLI layers cannot enable it. The service's own bypass gate still applies. Under dont-ask, and for any ask no approver can answer, the model receives Denied: no approver in unattended schedule and the run continues. The directory must exist and its checkout must be trusted. The directory, trust, Mode and target Session are checked at add and again before every run. A failed check ends the run refused and notifies.
Jitter Each schedule gets a fixed offset of up to loops.jitter_seconds (default 60, capped at 3600, 0 turns it off), derived from SHA-256 of its ID. The next run is next_occurrence(now − jitter) + jitter, and list and show report it.
Completion notifications notify is always, on_change (default) or never. A run reports noop through schedule_wakeup, which is now offered during scheduled runs; its delay is ignored there. Failed and refused runs notify, and so do budget stops. A notification goes to the run's Session (TUI, remote devices) and to the desktop through a DesktopNotifier port: macOS runs osascript and Linux notify-send, with the text passed only as arguments.
Budgets for loops The add flags, else budgets.loop, apply across runs. They are checked before and after each run. An exhausted schedule stops with budget_exceeded and notifies; resume refuses it.
Loop events and hooks schedule.run.1 records each run in phases: started, reported and ended (with outcome, duration, cost, tokens and turns). ScheduleRun is an observational runtime lifecycle event.
Restore A run the service stopped during ends interrupted. Missed runs follow --missed skip (the default) or run-once.
Usage limit waits A Session whose current run is a scheduled one counts as unattended, so it waits for a usage-limit reset.

Security (non-negotiable rules)

  • bypass: a schedule never runs in bypass unless loops.allow_bypass comes from user or managed config. cyber-core::loops::allow_bypass checks the merged value and the label of the layer that set it, and fails closed on anything else. Tests cover the project, local, env, profile and CLI layers, and a real trusted checkout whose cyber.jsonc sets it.
  • Asks under dont-ask: they are denied with feedback, and the run continues.
  • The model cannot create persistent schedules:
    • no tool creates one;
    • the routes need the owner credential, which model subprocesses never receive;
    • cyber loops add, resume and run are refused when CYBER_SESSION_ID is set;
    • the model's own cron_ tasks (part A) stay Session-scoped and capped.
  • Trusted directories only: checked at add and again before every run.
  • Fail closed everywhere:
    • a host without the checkout resolver refuses schedules (the ToolHost::schedule_policy default and BuiltinHost without hook_config);
    • a configuration that fails to load refuses;
    • a missing directory, or an unusable or busy Session, refuses or skips the run;
    • schedule routes are outside the IDE token scope.
  • Single run per occurrence: every transition is decided inside the writer transaction that records it. A start requires the schedule to be free and at n − 1 runs; an end requires the run to still be running.

Design decisions that changed the spec

These are in openspec/changes/implement-m2-4-schedules/specs/loops-scheduling/spec.md:

  • Persistent local schedules:
    • there is no cyber service install in this codebase, so the warning offers cyber service start;
    • agent subprocesses are refused;
    • the routes require the owner credential.
  • Unattended Mode for schedules:
    • project, local, env, profile and CLI layers cannot enable loops.allow_bypass;
    • runs happen only in trusted, existing directories, and this is re-checked before every run;
    • the refused outcome;
    • the deny message also applies to asks no approver can answer in any Mode.
  • Completion notifications: scheduled runs report noop through schedule_wakeup.

Other choices are in design.md:

  • the jitter cap (3600 s);
  • a busy --session target skips the run;
  • the schedule's max_wall_seconds is checked between runs, not during a usage-limit wait;
  • desktop notifications are best effort with a 5 s timeout.

Tests

  • cyber-server/tests/schedules.rs:
    • a nightly run in a new unattended dont-ask Session, with its events and notifications;
    • overlap skip;
    • jitter determinism and the next run;
    • the gate: untrusted directory, bypass without the opt-in, a missing directory, and trust lost after add → refused with a notification and no Session;
    • a budget stop that refuses resume;
    • pause, resume and remove;
    • a resumed Session switched to the schedule's Mode, and a child Session refused;
    • restore of an interrupted run;
    • missed skip and run-once after a restart.
  • cyber-server/tests/usage_limit.rs: a scheduled run in an attended Session waits for the reset and completes.
  • cyber-app/tests/schedules.rs (production App and tool host):
    • an ask under dont-ask gets Denied: no approver in unattended schedule;
    • schedule_wakeup with noop: true makes a quiet run, and the ScheduleRun hook payload is checked;
    • the HTTP routes and error tags;
    • project config cannot allow bypass.
  • cyber-cli/tests/loops_cli.rs (real binary and service): add with the service warning, list, run until completed, pause, resume, rm, and the refusal inside an agent subprocess.
  • Unit tests:
    • cyber-core::loops: allow_bypass by layer, and the jitter settings;
    • schedules: jitter, the next run, request validation, run IDs and the notify policy;
    • the desktop notifier passes its text only as arguments;
    • the cyber loops budget flags and ID checks.

Docs

  • docs/local-core-guide.md: a new "Persistent schedules" section.
  • docs/features.md, README.md and ROADMAP.md (the M2.4 status line).
  • docs/cli.md is regenerated, so cyber help-markdown equals it.
  • sdk/openapi.json and the TypeScript SDK are regenerated, with ScheduleNotFoundError.

…oopIteration hook

The model can now schedule prompts in its own Session with cron_create,
cron_list and cron_delete. A task is a Loop of kind cron (cron_ ID) that
reuses the loop events, sweep, overlap, budgets and restore, and shares
the loops.max_per_session cap with /loop. Expressions are 5-field cron in
local time (cyber-core::cron, DST-aware); one-shot tasks take an at time.
The tools are offered only to root Sessions outside Workflow Runs, and
cron_delete cannot stop a user's /loop.

Restore applies the missed-run policy to cron tasks; a one-shot task whose
time passed under skip ends expired.

/loop gains --until-goal (stops with goal_ended when the recorded Goal
ends) and --notify always|on_change|never. Iterations that report
noop: false or fail, and failed or budget_exceeded stops, record a Session
notification. LoopIteration hooks fire after loop.iteration.ended.1.

/loop list rows show the kind, the next run and the stop conditions.

Adds the OpenSpec change implement-m2-4-schedules (proposal, design,
tasks, spec deltas) covering both this part and persistent schedules.
Schedules are their own event streams (schedule.created/updated/run),
projected into new schedule and schedule_run tables. The served App runs
a sweep that starts due runs through an unattended gate, creates a new
unattended Session per run (or resumes a root Session switched to the
schedule's Mode) and admits the prompt queued as schedi_<id>_<n>; the
Drain's idle exit records the outcome. Transitions are decided inside
the writer transaction so a run is never started or ended twice.

The gate refuses missing or untrusted directories, refuses bypass unless
loops.allow_bypass comes from user or managed config, and is re-checked
before every run. Asks in scheduled runs are denied with 'Denied: no
approver in unattended schedule'. cyber loops add/resume/run are refused
inside agent subprocesses.

Adds deterministic per-schedule jitter (loops.jitter_seconds), notify
policies with Session and desktop notifications (osascript/notify-send,
text passed as argv), budgets across runs, restore of interrupted and
missed runs, schedule_wakeup noop reporting during scheduled runs, the
ScheduleRun hook, and usage-limit waits for scheduled runs.

Exposes /api/v1/schedules routes, regenerates the OpenAPI document, SDK
and docs/cli.md, and updates the guide, features, README and roadmap.
Without it the production host fell back to the refusing default, so the
service would refuse every schedule. Caught by
app_host_forwards_every_tool_host_method.
@leonardoaraujosantos
leonardoaraujosantos merged commit 5032d79 into main Nov 20, 2026
28 checks passed
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.

1 participant