Write a policy step
Most policies are standard steps with their settings. When your business has a rule no standard step makes, you write a step of your own in Lua (Luau). You test it with scenarios, and publish it in a policy package. This page walks through one step from start to finish. The policy step reference lists every stage and everything a step can call.
Check the policy cookbook first. Of its recipes, only two have steps of their own. Both are exporters that write a payroll system’s file layout.
How a step works
Section titled “How a step works”A step is a Lua table in a policy package. A policy layer names the step it runs and gives its parameters:
[[layer.step]]id = "long-day"uses = "plant/long_day" # <package>/<step>params = { max = "12h" }When a ledger is worked out, the step runs at its stage. It reads what that stage gives it, and asks for changes through ctx. It never edits a line or a segment itself. The engine makes each change. It stamps each change with the step and the layers that configured it. After every step, it checks that no worked time was made or lost. A step that fails, or asks for a change that cannot be made, stops the ledger with an error that names the step.
The standard steps are written the same way, through the same ctx.
1. Lay out the package
Section titled “1. Lay out the package”A policy package is a folder:
plant/ package.toml # name, version, description, effective day, needs, layers main.lua # its own steps and exporters scenarios/*.toml # what it must come topackage.toml names the package and holds its layers. This one has one layer that runs the step for everyone:
name = "plant"version = "1.0.0"description = "Days over 12 hours of work are flagged for review."
[[layer]]id = "review"[[layer.step]]id = "long-day"uses = "plant/long_day"params = { max = "12h" }A package’s own steps are named <package>/<step>, here plant/long_day. The standard ones are standard/<step>, such as standard/threshold.
2. Write the step
Section titled “2. Write the step”main.lua returns a table of steps. This step runs in the exceptions stage and adds up each workday’s work. It raises an exception about the day when the day is over the limit:
local M = {}
-- A workday with more than `max` of work is flagged for review.M.long_day = { stage = "exceptions", params = { max = { type = "duration", required = true }, }, run = function(ctx, p) local worked = 0 for _, s in ipairs(ctx.segments) do if s.kind == "work" and s["end"] ~= nil then worked += s["end"] - s.start end end if worked > time.duration(p.max) then ctx:exception("long_day", 0, "more than " .. p.max .. " of work") end end,}
return Mstagesays where the step runs. Pick the stage that makes the decision you need. The stages lists what each one decides and what happens without a step in it.paramsdeclares the parameters the step takes. The engine checks a layer’s parameters against it when the layer or package is published. It fills in defaults beforerunis called.run(ctx, p)is the step.pis its parameters, read only.- Time is whole milliseconds.
time.duration("12h")turns text into a duration. endis a Lua keyword, so a segment’s end iss["end"].
Steps that move time, such as an overtime rule in the allocate stage, name the rule that fired and let the engine word the stamp:
M.threshold = { stage = "allocate", window = "week", params = { over = { type = "duration", required = true }, to = { type = "string", required = true }, }, reasons = { over = "over {over} in the {window}" }, run = function(ctx, p) local filter = { worked = true, days = ctx.window.applies } local over = ctx:time_in(filter) - time.duration(p.over) if over > 0 then ctx:move_latest(over, filter, p.to, { rule = "over" }) end end,}Take a layer overtime that runs it as step weekly with over = "40h". The stamp on the moved time reads weekly [overtime rev 1]: moved 2h from work to overtime: over 40h in the week. See reasons.
Beside the words, the reason carries a stable key and the values that fill it in. The key is <package>/<step>.<rule>. So the rule over of a package’s plant/threshold step is plant/threshold.over, with the values over and window. An app shows the reason in the person’s own language from its own wording of each key. A step that passes its own words as text has no key.
Read balances, periods and averages
Section titled “Read balances, periods and averages”A step reads more than its stage’s lines. The reference lists each helper with what it returns:
- Periods.
ctx:period(spec, at), in every stage, works out a year, a blackout, a weekend’s nights or any other period. It works it out as of a date or an instant in milliseconds. Withoutat, it uses the window’s first day. It has methods to list its dates and combine it with another (periods in a step). Declare a parametertype = "period"and a policy can give the step any of them. The balance below once a year and leave rules recipes use it. - Leave rules. A
leavestep asks forctx:balance_after(category),ctx:balance_changes(category, direction, amount, period),ctx:requests(period),ctx:leave_taken(categories, period)andctx:earned(category, period). It refuses withctx:refuse(why). See protected leave and earning capped each year. - Averages of weeks.
ctx:average_week{ ... }gives the average workweek before a date, inallocatesteps only. It works frombefore,weeks,back,counts,measureandincludes. A step that reads the weeks before its pay period declares how far back withlooks_back. That is a number of days or a function of its parameters. See holiday pay on average earnings. - Balances in an allocate step. A step that declares
reads_balances = truecan callctx:balance(category). That is the balance of an accrual category as the pay period opens, before what the period’saccruesteps earn. See comp time. - Paying a balance out. In an
accruestep,ctx:pay_out(category, ms, pay_category, why, date)lowers a balance on a day of the window. It pays that time in a pay category set up as not worked. See comp time and year end by category. - The schedule as it stood. In any stage,
ctx:shifts_as_of(date, at)gives a workday’s shifts as the schedule stood at the instantat.ctx:schedule_changes(date)lists that workday’s overrides in the order they were stored. Each has itsid,kind,storedinstant,byandreason. In anallocatestep,ctx.window.previous_endis when the last segment before the window ended. It is for rules on the rest between shifts. See predictive scheduling. - Break attestations. In
allocateandexceptionssteps,ctx:attestation(date)gives a workday’s attestation, or nil. See break attestation. - Late changes. A
latestep readsctx.change.balancesbeside the change’s time and counts. So it can refuse or route a change that moves only a balance. See late change limits.
A step on a window of days longer than a pay period, such as a measurement period of several months, declares only_where_window_ends = true. It then runs only in the pay period that holds the window’s last day. It sees the whole window (a window longer than a pay period).
3. Test it with scenarios
Section titled “3. Test it with scenarios”A scenario is one person’s punches and setup, with what their timecard must come to. Put scenarios in the package’s scenarios/ folder. Each runs with the package published and adopted. So it holds only the person’s facts and what to expect:
description = "A 13-hour day is flagged for review; an 8-hour day is not"
[policy]zone = "America/Chicago"
[[punch]]at = "2026-10-05 06:00"kind = "in"[[punch]]at = "2026-10-05 19:00"kind = "out"[[punch]]at = "2026-10-06 08:00"kind = "in"[[punch]]at = "2026-10-06 16:00"kind = "out"
[expect]week = "2026-10-05"worked = "21h"exceptions = [ { kind = "long_day", at = "2026-10-05 00:00", detail = "more than 12h of work" },]Run them offline:
hourctl attend test plant/Every exception is compared, so the scenario also proves that the 8-hour day raises none. Write a scenario for each parameter and for the cases at the edge of the rule, such as a day of exactly 12 hours.
4. Publish and adopt
Section titled “4. Publish and adopt”hourctl -t acme attend pkg publish plant/ # the tenant's own packagehourctl -t acme attend pkg adopt plant@1 # follow version 1.xPublishing runs the package’s scenarios on the server first. A package whose scenarios fail is kept as a failed revision with what failed, and refused. No tenant sees a change. A published version never changes. A fix is a new version.
To publish one package for every tenant of the install, add --shared. --effective 2026-11-02 sets the workday it is in force from. Adopting, following a major version, pinning a version and effective days are covered in policy packages. The API is in the attendance API.
Rules a step must follow
Section titled “Rules a step must follow”The engine enforces these. A step that breaks one fails with an error that names it:
- Stay inside your workday or window. A
timeorexceptionsstep changes, or raises exceptions about, only its own workday’s segments. Anallocatestep adds counts and hours only inside its window. This keeps recalculation exact when a single day changes. - Make no worked time. An
allocatestep moves worked time between categories. It never adds or loses any. Only time not worked, such as holiday pay, can be added, withctx:add_hours. - Keep nothing between calls. A step that changes its parameters, a global or a module-level table fails. What a step works out for one day or person must not depend on what ran before it.
- Be pure. There is no
os, no files and no network. The same facts always give the same ledger, so any ledger can be worked out again and audited.
Write an exporter
Section titled “Write an exporter”A package can also hold exporters, which write a pay period’s timecards in another system’s file layout. An exporter is an entry with stage = "export" and a person function. The function returns the text for one person’s timecard:
M.hours = { stage = "export", content_type = "text/csv", extension = "csv", header = function(p) return csv({ "employee", "date", "code", "hours" }) end, person = function(x, p) local out = {} for _, d in ipairs(x.days) do for category, hours in pairs(d.categories) do table.insert(out, csv({ x.attributes.payroll_id, d.date, category, hours })) end end return table.concat(out) end,}Test it with an [expect.export] scenario that gives the whole file (scenarios). The fields an exporter is given are in exporters. Running an export is in payroll export.