Appearance
Metrics
A metric is a number with a precise, named definition: revenue, gross margin, days sales outstanding. A control is a condition that independent calculations agree: the trial balance is zero, the receivables ledger matches the accounts. Both are certified: the compiler computes a fingerprint of their complete definition, and typerule check fails when the meaning of a certified definition changes until someone approves it again.
metric
typerule
metric revenue(company: Company, period: DateRange) -> Money<company.currency>:
label: "Net revenue"
owner: "Finance"
definition: """
Net amounts of the posted customer invoices dated in the period, minus credit
notes, converted to the company currency at the rate of each invoice.
Excluded: VAT, drafts, cancelled invoices.
"""
value = Invoice
.where(i => i.company == company and i.direction is Out and i.state is Posted and i.date in period)
.sum(i => convert(i.untaxed * i.sign, to=company.currency, rate=i.rate).round())
metric dso(company: Company, on: Date) -> Decimal:
label: "Days sales outstanding"
owner: "Treasury"
definition: "Open receivables at the date / revenue of the last 365 days × 365."
value =
let r = revenue(company, DateRange(on - 365 days, on))
open_receivables(company, on) / r * 365 if r != 0 else 0metric name(parameters) -> Type: followed by:
| Line | Meaning |
|---|---|
key: expression | metadata: label, owner, definition, or any other key; type-checked, not part of the value |
value = expression | the value; it may span several lines with let (see Syntax) |
The value is a formula: today() and now() are not allowed, and convert needs on= or rate=. It is checked against the return type: cannot store … in a metric of type ….
A metric is called like a function, from formulas, actions, tests and other metrics:
typerule
entity Sale:
date: Date
amount: Money<EUR>
posted: Bool default false
metric sales(period: DateRange) -> Money<EUR>:
label: "Sales"
value = Sale.where(s => s.posted and s.date in period).sum(s => s.amount)
metric average_sale(period: DateRange) -> Money<EUR>:
label: "Average sale"
value =
let n = Sale.count(s => s.posted and s.date in period)
(sales(period) / n).round() if n > 0 else 0 EUR
test "sales of November":
Sale(date=2026-11-02, amount=100.00 EUR, posted=true)
Sale(date=2026-11-03, amount=50.00 EUR, posted=true)
Sale(date=2026-12-01, amount=10.00 EUR, posted=true)
expect sales(DateRange(2026-11-01, 2026-11-30)) == 150.00 EUR
expect average_sale(DateRange(2026-11-01, 2026-11-30)) == 75.00 EURcontrol
typerule
control trial_balance:
label: "The trial balance is zero, for every company"
holds = Company.all(c => GL.where(g => g.company == c).sum(g => g.amount) == 0)
control invoice_numbering:
label: "Customer invoice numbers have no gaps and no duplicates"
holds = Invoice
.where(i => i.direction is Out and i.state is not Draft)
.group_by(i => (journal: i.journal, year: i.date.year))
.all(g => g.items.map(i => i.number?.sequence_part()).is_contiguous())control name: has metadata lines and a holds = condition formula. Unlike a metric, a control has no parameters, and may read the clock (today()).
Controls are type-checked, fingerprinted and certified like metrics. The engine does not evaluate them on its own (not at commit, not periodically): evaluating them and keeping their outcome is the next step.
Fingerprints
The fingerprint of a metric or control is a SHA-256 hash of the canonical text of its definition and of everything it depends on, down to stored fields:
| Included | |
|---|---|
| the metric | its parameters, return type and formulas |
| every formula it reads, transitively | its type, expression and per company |
| every stored field it reaches | its type and its default (and what the default reads) |
| every ledger it reads | its columns and every post to block that feeds it, with its condition |
| other metrics it calls | their own definition, transitively |
Comments, formatting and the metadata lines (label, definition…) do not change the fingerprint; any change of meaning does, even far away: changing how an invoice line rounds its subtotal changes the fingerprint of revenue.
typerule trace shows the same dependency graph for a field: see CLI.
metrics.lock
typerule certify writes the fingerprints to metrics.lock, at the root of the project:
toml
# Generated by `typerule certify`. Do not edit by hand.
# Each metric and control is bound to the fingerprint of its complete definition: its
# formula and everything it depends on, down to stored fields. If the fingerprint
# computed from the code differs, `typerule check` fails until it is approved again.
[revenue]
fingerprint = "sha256:14c221ff12831093"
dependencies = 14
approved_by = "m.bianchi (CFO)"
approved_on = "2026-11-21"| Key | Meaning |
|---|---|
fingerprint | the first 16 hexadecimal digits of the hash |
dependencies | how many definitions it depends on |
approved_by | who approved it (unapproved when nobody was named) |
approved_on | the date of the certification |
When the project has a metrics.lock, typerule check compares it with the code:
| Situation | Result |
|---|---|
| a metric is not in the file | warning: `revenue` is not certified: run `typerule certify revenue` |
| the fingerprint differs | error: the definition of `revenue` changed since it was certified by m.rossi on 2026-11-22 (sha256:cda454518478a70e → sha256:b90d90d11ac3cad4): run `typerule certify revenue` once the change is approved |
Since a metric that calls another depends on it, changing revenue also invalidates dso. Being an error, it stops typerule check, typerule test and deploys from the web editor until the metrics are certified again.
Certifying
bash
typerule certify # every metric and control
typerule certify revenue dso --approved-by "m.bianchi (CFO)"| Rule | Detail |
|---|---|
| Names | Without names, every metric and control is certified; with names, only those. |
--approved-by who | Recorded as the approver. Without it, a definition already in the file keeps its previous approver, a new one gets unapproved. |
| Unchanged definitions | Keep their entry (approver and date) as they are. |
| Output | certified revenue: sha256:b90d90d11ac3cad4 (3 dependencies) for each new or changed entry |
--approved-by can come anywhere: typerule certify --approved-by alice records alice as the approver of every new or changed metric and control. A name that is not a metric or a control is an error (unknown metric `x` (metrics: …)), and nothing is written.
typerule change preview lists the certified metrics a change of data touches (Migrations and changes).