ScheduleStore

org.llm4s.agent.service.ScheduleStore
@Experimental

The schedules of scheduled runs and the firings they made (#2446, decisions 1, 2 and 6 of docs/superpowers/specs/2026-11-22-scheduled-runs-design.md), in the database that holds the run queue. Like RunQueue it is a separate SPI: no transaction spans a firing and the queue entry it submits. A fire time happens once because it is claimed here, in one step, before anything is submitted for it.

Implementations must be safe to call from several threads and processes, and must:

  • '''judge time by the store's clock''' (now): a schedule's created and updated times, what is due, a claim's time, a settle's time and every age are the store's, never the caller's, so hosts whose clocks disagree agree on what is due (Stage 5 rule 1);
  • '''create''' a schedule under its (tenantId, id), which is unique: a second create of the id in the tenant is refused (ScheduleError.Exists) and leaves the first as it was; a tenant's schedules are counted in the create's own transaction (or under its lock), so of concurrent creates no more succeed than maxPerTenant allows (ScheduleError.TooMany); a refusal for an existing id comes before one for the count; and never reuse a revision of the key: a schedule created again after a delete starts above the last revision the deleted one reached, so an update read from the deleted one is a ScheduleError.Conflict rather than overwriting the new one;
  • '''update''' a schedule only from the revision it was read at: a compare-and-set, each success giving it the next revision (ScheduleError.Conflict otherwise), so of two edits from one read the second is refused; and keep the stored next fire time unless the update names one, so an edit never conflicts with a claim and never puts the schedule back on a fire time already claimed: a person's edit never loses to the scheduler; an update that names a next fire time at or before one of the schedule's firings is a ScheduleError.Conflict too, in the update's own step, so a reschedule computed before a claim cannot rewind onto it;
  • '''scope by tenant''' (rule 9): create, update, delete, schedule, schedules and firings answer for the caller's tenant only (None and Some differ), another tenant's schedule being ScheduleError.NotFound; due, claim, settle, threadsToDelete, threadDeleted and reclaim are the operator's, store-wide;
  • '''claim''' a fire time in one step (claim): only while the schedule exists, is not paused and its next fire time, its target, its cron expression and its zone are still the ones the caller read, insert the firing, keyed (tenantId, scheduleId, fireAt), move the schedule's next fire time on, and answer the schedule as it then stands; never moving its revision; so of any number of concurrent claims of one fire time, from any number of processes, exactly one succeeds;
  • '''copy inputs''': a schedule's input is held as written and answered as a copy, so changing an input a caller passed or was answered changes nothing stored;
  • '''settle''' a firing once: the first settle's outcome is kept and later ones change nothing;
  • '''keep firings''' until reclaim removes them, after their schedule is deleted too: never one that is unsettled, nor one whose thread is still to be deleted.

ScheduleStoreContract in llm4s-agent-testkit is the executable form of this contract.

Attributes

Source
ScheduleStore.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes

Members list

Value members

Abstract methods

def claim(schedule: Schedule, fireAt: Instant, nextFireAt: Instant, threadId: ThreadId, missed: Int): Result[Option[ClaimedFiring]]

Claims schedule's fire time fireAt for one firing on threadId that passed over missed later fire times, and moves the schedule's next fire time to nextFireAt, in one step: only while the stored schedule exists, is not paused, its next fire time is still fireAt, its target still schedule.target (the one threadId was chosen under), and its cron expression and zone still schedule.cron and schedule.zone (the cadence nextFireAt was computed from), and no firing of fireAt exists. Answers the firing, claimed now by the store's clock, with the schedule as the claim left it (its current input, which the firing runs with), or None when another claim came first, or the schedule was paused, deleted, given another next fire time, another target or another cron expression or zone since it was read: read it again and claim from that. An edit of only the input or the other fields does not stop the claim. A claim leaves the schedule's revision and updatedAt as they were. A ValidationError when fireAt is not schedule.nextFireAt: only the next fire time the caller read can be claimed.

Claims schedule's fire time fireAt for one firing on threadId that passed over missed later fire times, and moves the schedule's next fire time to nextFireAt, in one step: only while the stored schedule exists, is not paused, its next fire time is still fireAt, its target still schedule.target (the one threadId was chosen under), and its cron expression and zone still schedule.cron and schedule.zone (the cadence nextFireAt was computed from), and no firing of fireAt exists. Answers the firing, claimed now by the store's clock, with the schedule as the claim left it (its current input, which the firing runs with), or None when another claim came first, or the schedule was paused, deleted, given another next fire time, another target or another cron expression or zone since it was read: read it again and claim from that. An edit of only the input or the other fields does not stop the claim. A claim leaves the schedule's revision and updatedAt as they were. A ValidationError when fireAt is not schedule.nextFireAt: only the next fire time the caller read can be claimed.

Attributes

Source
ScheduleStore.scala
def create(schedule: Schedule, nextFireAt: Instant, maxPerTenant: Int): Result[Schedule]

Stores schedule as a new schedule of its tenant, at revision 1 - or, when the tenant had a schedule of the id that was deleted, one above the last revision that one reached - created and updated now by the store's clock, its next fire time nextFireAt, and answers it as stored. Refused with ScheduleError.Exists when the tenant has a schedule of the id, and otherwise with ScheduleError.TooMany when it already has maxPerTenant.

Stores schedule as a new schedule of its tenant, at revision 1 - or, when the tenant had a schedule of the id that was deleted, one above the last revision that one reached - created and updated now by the store's clock, its next fire time nextFireAt, and answers it as stored. Refused with ScheduleError.Exists when the tenant has a schedule of the id, and otherwise with ScheduleError.TooMany when it already has maxPerTenant.

Attributes

Source
ScheduleStore.scala
def delete(tenantId: Option[TenantId], id: ScheduleId): Result[Unit]

Removes the tenant's schedule id; ScheduleError.NotFound when there is none. Its firings stay until reclaimed.

Removes the tenant's schedule id; ScheduleError.NotFound when there is none. Its firings stay until reclaimed.

Attributes

Source
ScheduleStore.scala
def due(redriveAfter: FiniteDuration, limit: Int): Result[DueSchedules]

The store's now, up to limit schedules due by it, and up to limit firings claimed more than redriveAfter before it and not yet settled; see DueSchedules.

The store's now, up to limit schedules due by it, and up to limit firings claimed more than redriveAfter before it and not yet settled; see DueSchedules.

Attributes

Source
ScheduleStore.scala
def firings(tenantId: Option[TenantId], id: ScheduleId, limit: Int): Result[Vector[Firing]]

Up to limit firings of the tenant's schedule id, newest fire time first, whether or not the schedule still exists; empty when there are none.

Up to limit firings of the tenant's schedule id, newest fire time first, whether or not the schedule still exists; empty when there are none.

Attributes

Source
ScheduleStore.scala

The store's time: what every time this store records and every age it judges is measured by.

The store's time: what every time this store records and every age it judges is measured by.

Attributes

Source
ScheduleStore.scala
def reclaim(lifetime: FiniteDuration, limit: Int): Result[Int]

Removes up to limit settled firings that have no thread left to delete and were settled lifetime or more ago, by the store's clock, oldest first, and answers how many it removed. Never an unsettled firing.

Removes up to limit settled firings that have no thread left to delete and were settled lifetime or more ago, by the store's clock, oldest first, and answers how many it removed. Never an unsettled firing.

Attributes

Source
ScheduleStore.scala

The tenant's schedule id; ScheduleError.NotFound when there is none.

The tenant's schedule id; ScheduleError.NotFound when there is none.

Attributes

Source
ScheduleStore.scala
def schedules(tenantId: Option[TenantId], thread: Option[ThreadId], after: Option[ScheduleId], limit: Int): Result[Vector[Schedule]]

Up to limit of the tenant's schedules, by id in the byte order of its ASCII characters (COLLATE "C" in SQL, never a locale's collation; ScheduleId's Ordering), after after when it is set; only those bound to thread (ScheduleTarget.Thread) when it is set.

Up to limit of the tenant's schedules, by id in the byte order of its ASCII characters (COLLATE "C" in SQL, never a locale's collation; ScheduleId's Ordering), after after when it is set; only those bound to thread (ScheduleTarget.Thread) when it is set.

Attributes

Source
ScheduleStore.scala
def settle(firing: Firing, outcome: FiringOutcome): Result[Firing]

Settles firing with outcome, now by the store's clock, and answers it as stored: a firing settled already keeps its first outcome and is answered with it. ScheduleError.NotFound for a firing the store no longer holds.

Settles firing with outcome, now by the store's clock, and answers it as stored: a firing settled already keeps its first outcome and is answered with it. ScheduleError.NotFound for a firing the store no longer holds.

Attributes

Source
ScheduleStore.scala
def threadDeleted(firing: Firing): Result[Unit]

Records that firing's thread has been deleted, so it is no longer listed by threadsToDelete.

Records that firing's thread has been deleted, so it is no longer listed by threadsToDelete.

Attributes

Source
ScheduleStore.scala

Up to limit settled firings whose thread is due for deletion by the store's clock (Firing.deleteThreadAt), earliest deadline first.

Up to limit settled firings whose thread is due for deletion by the store's clock (Firing.deleteThreadAt), earliest deadline first.

Attributes

Source
ScheduleStore.scala
def update(schedule: Schedule, nextFireAt: Option[Instant]): Result[Schedule]

Replaces the tenant's schedule of schedule.id with schedule, if the stored one is still at schedule.revision: answers it at the next revision, updated now by the store's clock, its creation time kept. Its next fire time is nextFireAt when set - an edit of the cron expression or the zone passes the recomputed one - and otherwise the stored one, whatever schedule.nextFireAt says, so an edit of the input or the other fields never conflicts with a claim and never puts the schedule back on a fire time already claimed. ScheduleError.Conflict, naming the stored revision, when the schedule has been updated since it was read (a claim does not count), or when nextFireAt is set at or before the fire time of one of the schedule's firings, already claimed: recompute it from a fresh read; ScheduleError.NotFound when the tenant has no schedule of the id. The store keeps its own copy of the input: changing the caller's afterwards changes nothing stored.

Replaces the tenant's schedule of schedule.id with schedule, if the stored one is still at schedule.revision: answers it at the next revision, updated now by the store's clock, its creation time kept. Its next fire time is nextFireAt when set - an edit of the cron expression or the zone passes the recomputed one - and otherwise the stored one, whatever schedule.nextFireAt says, so an edit of the input or the other fields never conflicts with a claim and never puts the schedule back on a fire time already claimed. ScheduleError.Conflict, naming the stored revision, when the schedule has been updated since it was read (a claim does not count), or when nextFireAt is set at or before the fire time of one of the schedule's firings, already claimed: recompute it from a fresh read; ScheduleError.NotFound when the tenant has no schedule of the id. The store keeps its own copy of the input: changing the caller's afterwards changes nothing stored.

Attributes

Source
ScheduleStore.scala