Skip to content

Dates and books

A workflow often counts days and moves quantities: it holds an order's goods until the payment is due, ships them once paid, and puts them back when not. A .flow does neither itself, since it has no arithmetic. The days are counted by a dates file of koyomi (.cal), and the quantities kept by a book of chobo (.book). A .flow reads them as it reads rulec's rules: it calls a date as it calls a rule, and runs an operation of a book as a task.

The invoice example does all of it, and runs as it is on every platform (examples/invoice):

workflow invoice v1
  now range >=2026-01-01 <=2027-12-31
…
use dates terms from "dates/payment_terms.cal"
  lambda "arn:aws:lambda:eu-west-2:123456789012:function:payment-terms"
use book stock from "books/stock.book"
  lambda "arn:aws:lambda:eu-west-2:123456789012:function:stock"
…
task reserve(order: string, sku: string, qty: int) -> stock.reserve
  book stock.reserve.hold
  starts stock.reserve
  errors out_of_stock
…
case hold : stock.reserve follows stock.reserve

flow
  hold <- reserve(order: order.id, sku: order.sku, qty: order.quantity)
    on out_of_stock => succeed outcome = out_of_stock, due = none
  let due = terms.payment(received: now)
  wait until due.at
  let payment = check_payment(order: order.id, due: due.day)
  match payment.paid
    true =>
      hold <- ship(order: order.id, sku: order.sku)
        on expired => succeed outcome = not_paid, due = due.day
      succeed outcome = shipped, due = due.day
  …

koyomi checks the dates file on every day of its range, and chobo checks the book's bounds and the reasons each operation can be refused with. dandori reads what they found through ritsu's ports, as it reads the rules: ritsu dandori reads the dates files and the books in the same process, and the dandori binary alone says to run a workflow that uses them that way (E018).

Dates

The dates file of the example closes on the 20th and pays on the 10th of the next month at 09:00, or on the business day before:

dates payment_terms v1
description "Closes on the 20th; pays on the 10th of the next month, at 09:00, or on the business day before when that day is closed"
use calendar "../calendars/weekdays.cal"

inputs
  received : date  range >=2026-01-01 <=2027-12-31

date closing = received
  close day 20          # "closes on the 20th"

date payment = closing
  day 10 of month +1    # "pays on the 10th of the next month"
  roll preceding        # "on the business day before when that day is closed"
  at 09:00
…

use dates <name> from "<file.cal>" reads it, and each date of the file is called by its name, like a rule: terms.payment(received: …). It answers a record of two fields: day, the date, and, when the date says a time of day (at 09:00), at, that time in UTC, ready for wait until. A date that says no time answers day alone.

  • A date input takes a date. When the file's calendar says its UTC offset (offset +09:00), it takes a timestamp too, read as the day that time falls on there: 2026-03-31T15:30:00Z is April 1 at +09:00. A time given to a date whose calendar says no offset is E003, and the note says to give the calendar one, or to pass a day.
  • An integer input takes an int, held to the range the file declares (E014, W104), as a rule's input is.
  • Each date of a file is called on its own, with the inputs it reads.
  • Under use dates, lambda "<function>" is the Lambda function that computes the dates on Step Functions and Lambda durable functions, and local has Temporal call them as local activities, as for a rule.

A date is called as a rule is, in an activity (a call on each platform), although koyomi's code is a pure function. A calendar's holidays change every year: computed in the workflow's code, a date would replay otherwise on a worker whose table was updated. As with a rule, the inputs and the answer stay in the run's history.

date is a type: a day, written YYYY-MM-DD. An input, an output, a field, a task's parameter or answer can be one, and a string can have one put in it ("{order.id} ships on {ship.day}"). It is checked where it comes in, like every other type, and held there to its range when it has one (range >=2026-01-01 <=2027-12-31; Ranges). An expression never compares a date or adds to one: counting days is what a dates file is for.

now is the time the statement runs at, in UTC, to the second: a timestamp. It can go to a date, to a task, to wait until, and into a string ("received at {now}"). A statement reads one moment, however many times it says now, and every try of a call that is tried again gives the moment of the first: each platform reads the clock once for the statement, so that a replay reads the same time again (the table below). In a workflow that says the period it runs in, now is a moment of the period. Under ritsu run, the clock goes on by each wait, so a date given now after a wait of six days counts from the day the wait came to.

The period a workflow runs in

A dates file takes its inputs over a range of days: the invoice's terms take orders received from 2026-01-01 to 2027-12-31. A workflow that gives a date now can say the period it runs in, under its first line, with the same days, as the invoice does:

workflow invoice v1
  now range >=2026-01-01 <=2027-12-31
…
  let due = terms.payment(received: now)

A run that starts outside the period calls nothing and fails with Dandori.OutOfPeriod, as an input outside its range does. Each statement that reads now checks the clock first, and reads the moment it checked: outside the period, the run goes to on failure, as after a task's error nothing handles. The invoice holds the goods before it reads now, so a run whose clock has left the period by then puts them back. now in on failure and on cancel is not checked, so that the clock never stops what settles a run.

ritsu check reads the days now can be at the offset of the dates file's calendar, and holds them to the days the date takes. With the period, the invoice's now gives received a day the terms take, and the border holds:

$ ritsu check .
…
ritsu check: 8 files (koyomi 4, chobo 2, dandori 2): all pass (6 warnings); borders between the languages: 8 checked, 6 undecided

The six warnings are W206: the hold has no timeout, so nothing bounds how long after it the goods are shipped. Without the period, now can be any day, and the border is a warning (W205); with a period that goes on past the days the file takes, it is an error (E205), as with a date or a time whose range does. The Japanese version's calendar is at +09:00, and its period is written at that offset (now range >=2026-01-01 <=2027-12-28 offset +09:00): the same period in UTC would end nine hours into a day its terms do not take.

Books

The book of the example keeps the stock of each SKU. A delivery adds to it, and an order holds what it takes until it ships, for 60 days at most:

book stock v1
…
unit pcs

account shelf(sku: string) : pcs
  …
  at least 0 refused as out_of_stock
account suppliers : pcs outside
account customers : pcs outside

transfer receive(delivery: string, sku: string, qty: pcs)
  key delivery, sku
  move qty from suppliers to shelf(sku)

transfer reserve(order: string, sku: string, qty: pcs)
  …
  key order, sku
  pending expires after 60 days
  move qty from shelf(sku) to customers

use book <name> from "<file.book>" reads it. A task runs an operation of one of its transfers with book <book>.<transfer>.<operation>, where the operation is chobo's own: do for a transfer done at once, and hold, post or void for one that holds first.

  • The task's parameters are the transfer's, by name: all of them for do and hold, those of the key for post and void. A post that takes the amounts too posts that much of the hold, and gives the rest back. An amount is an int, a whole number in the book's unit.
  • hold, post and void answer the hold: a record named after the transfer (stock.reserve), with the key's parameters and the hold's state. do answers nothing.
  • The reasons the book refuses with are the task's errors, as they are: errors out_of_stock, errors expired. A reason the book cannot refuse that operation with is E016. A refusal the task does not declare fails the call (failure).
  • A book's operation happens once for its key, so it takes no key: done again, it answers that it was done before, which reads as done, and a retry is safe. Nor does it take refused as (below).
  • Under use book, lambda "<function>" is the Lambda function that runs the book's operations on Step Functions.

tests/flows/dates_and_books.flow posts part of a hold:

task 一部を出す(注文: string, sku: string, 数: int) -> 倉庫.引当
  book 倉庫.引当.post
  sends post
  errors expired

A hold as a case

A hold has a life: it is held, then posted, voided, or expired. The book says it as a state machine, so case <name> : <book>.<transfer> follows <book>.<transfer> makes a hold a case, and the checker follows it as it follows a rule's machine. The task that holds says starts <book>.<transfer>, the one that posts sends post, and the one that voids sends void (E008 otherwise).

When the transfer's holds expire, expire happens on the other side by itself: the checker counts it before every post and void, with no external to write. A refusal is one of the book's reasons, which differ from state to state, so the case takes no refused when. The checker says when a reason can come back unhandled:

error[E022]: tests/fixtures/book_refusals.flow:41:1: the book may refuse `post` here with `expired` (when `押さえ` is in expired); declare `expired` in the `errors` of `出荷する`, and handle it with `on expired =>`
    41 |       押さえ <- 出荷する(注文: 受注.id, sku: 受注.sku)
  the run that gets there:
      38  引き当てる: 押さえ starts in held
      40  match 受注.急ぎ: true
          `expire` happens on the other side: 押さえ held → expired

A workflow that can end with the goods still held is E020, as with any case; the invoice puts them back in on failure.

The hold's record says what the last operation answered (held, posted or voided), never expired: an expiry shows as a refusal with expired, and an arm for expired in a match on the hold's state is one that is never taken (E011). After a post that failed, the book may have posted the hold all the same, so a void there can be refused with already_posted; the invoice's on failure takes that refusal too when it puts the goods back.

Whether a hold has expired by the time a call posts or voids it is what ritsu check weighs where the flow meets the book (X5): the shortest and the longest time from the hold to the call, against the transfer's pending expires after. A wait for a length counts as that length, and so does a task's timeout. A wait until the time of a date the flow asks koyomi for after the hold, with now (wait until due.at, or timeout until due.at on a callback or an event), counts as the days koyomi says that date can come after its input, the fewest and the most. A call of a rule or of a date counts as 33 seconds at the most, its limit on every platform, and each try of a callback as its timeout and the minute it may take to hand its id on (How long a call may take). When the hold has expired on every run, that is an error (E206); when it may or may not have, a warning (W206), which says the day that makes the wait the longest.

Whether the book takes the amount a task gives a transfer is what ritsu check weighs where the amount can be a rule's output (X4): chobo takes a whole number from 0 to 2⁶³ − 1, and fails a call with any other rather than refusing it. The output is counted over what the call of the rule can be given on the runs that reach the transfer: the range of a number, and the values of an enum or a bool that the arms of a match before the transfer leave. A rule that gives seats back as a negative number for a handback can give its output to a transfer in the arms without it (match kind, handback => pass, workshop, talk => give_seats(…)), or after an arm for it that ends in fail. When a run can give the transfer an amount chobo does not take, that is an error (E203), which says the input of the rule that comes to it and, for an enum or a bool the flow can branch on, the value to leave out; when rulec counts the output reaching outside and no input of the rule there comes to such an amount, a warning (W203). A match holds the call only while nothing puts another value in the variable the call was given.

On each platform

Step Functions Temporal Lambda durable functions Argo Workflows pydantic-graph
a date a Lambda Task; dandori writes the function around the Python koyomi writes an activity around koyomi's TypeScript, Python or Go (local: a local activity) a step that invokes the same Lambda function through the Transport the caller image, around koyomi's TypeScript a function around koyomi's Python
an operation of a book the book's Lambda Task; dandori writes the function around chobo's Python client, and makes the hold from the arguments an activity dandori writes, through the Transport's book, on the chobo client you give it the same, in a step the same, in the caller image the same, through Deps.tasks
now the time the state was entered ($states.context.State.EnteredTime, to the second) the workflow's clock, read once before the call, which a replay reads again the same a step that reads the clock, its answer checkpointed (one operation, counted by E040) a template of its own before the statement, which reads now() once (four nodes, counted by E040) Deps.clock, read once at the start of the node
the period (now range) a Pass that reads the clock, and a Choice; outside, a Fail at the start, else the way to on failure an if in the workflow's code, which throws what on failure catches the same, on the clock a step reads a template that reads the clock, and one that holds it to the period a node that reads the clock and holds it to the period

What you put beside the generated code:

  • The dates. koyomi gen <file.cal> --target <the wrappers' language> --out koyomi (typescript, python or go) writes the code of a dates file, which the generated wrappers import from koyomi/ beside them. On Temporal in Go, each dates file is a Go module of its own (koyomi/go/<package>), which your go.mod requires and replaces, as rules.go says at its top.
  • The books. chobo writes a client for the book (chobo gen <file.book> --target typescript, --target python --books tigerbeetle, --target go --books mysql, …). Give the Transport the book value it makes, by the book's name: transport({ books: { stock: postgres(pool, { tenant }) } }) in TypeScript, transport(books={"stock": postgres(connection)}) in Python, and TransportOptions{Books: map[string]any{"stock": stock.Postgres(pool, tenant)}} in Go. On Step Functions, deploy lambda/book_stock_handler.py with chobo's Python client, made by handler = make_handler(lambda: tigerbeetle(ClientSync(…))).

How it is checked

  • Every platform plays every scenario against the reference interpreter, as for every other call: a scenario answers a date with a day (and the time of that day the date says), and an operation of a book with done, done before, or a refusal. Each platform's clock is set to the run's moment and does not move, so that now reads the same everywhere. A run of a workflow with a period can say two: the moment it starts at, and the one every now after the start reads, so that the runs that leave the period are played too.
  • The scenarios ritsu dandori scenarios writes leave the dates to koyomi and the books to chobo ("dates": "koyomi", and books, what was done to each book before the run): a refusal comes from what is in the book and from the time that goes by, not from an answer chosen. Every one runs under ritsu run unchanged, on a clock that goes on by each wait, to the end ritsu dandori run comes to.
  • The code dandori writes around koyomi's (the Lambda function, rules.ts, rules.py, rules.go) runs with the code koyomi writes, on every input of the file's range, and must answer what koyomi says each date comes to; one input in seven goes as a time of that day instead, read at the calendar's offset.
  • The operations of every flow's runs go through the Transport dandori writes in TypeScript, Python and Go, and through its Lambda function for Step Functions, to the clients chobo writes, on PostgreSQL, on TigerBeetle and on MySQL, and must answer what chobo's reference interpreter answers: on an empty book, on one a delivery has filled, and once more on the same book, where each operation was done before.