The tax engine
Understand how a return is evaluated: the registry, the dependency graph, value types, parameters and diagnostics.
The engine turns a return's stored inputs into every calculated line, the applicability of every form and a list of diagnostics. It is pure TypeScript in packages/t2/src/engine, with no React and no I/O. Calculated values are never stored: they are recomputed by evaluate every time.
assess() then adds tie-outs and CRA data rules.The registry
buildRegistry(forms, params?, pkg?) in src/engine/evaluate.ts indexes every field and table by id, collects each one's deps (plus the deps of suggest, showIf and derive), and sorts them with Kahn's algorithm. It throws GraphError on a duplicate id, a calc field without a calc(), a dependency that doesn't exist, or a cycle, naming the ids involved.
export type Registry = {
forms: Map<string, FormDef>
fields: Map<string, FieldDef>
tables: Map<string, TableDef>
order: string[] // topological evaluation order
deps: Map<string, string[]>
dependents: Map<string, string[]>
params?: Params // the release's parameters
range?: Applies // taxation periods the release covers
withdrawn?: string[]
}Each release builds its own registry in makeRelease. Get it with registryFor(formSetId) from @workspace/t2/repo. ret.formSet holds the release id, such as 2026.1.
Evaluating a return
evaluate(reg, corporation, ret) walks reg.order inside withParams(reg.params, …), so forms that read FED.sbd or ON.basicRate get the values of the release being evaluated. For each field it uses an override if there is one (keeping the live calculated value too), else an entered or imported input, else the calc() result. It returns an Evaluation:
In the app, call evaluateReturn(corp, ret) from the repository, or evaluateCached(corp, ret) from lib/use-return.ts in a component. Both evaluate on factsFor(corp, ret): the corporation facts the return adopted, not later edits to the customer record.
Values never hide a gap
Every value is a tagged Value (src/model/types.ts): blank, amt (bigint cents; zero is amt 0), unknown (needs an answer), na (doesn't apply; reads as 0), blocked (depends on missing logic; carries the originating fields), bool, text, date and num.
Inside a calculation, ctx.amt(id) reads blank and na as 0 but throws a Propagate for blocked or unknown, so the dependent line becomes blocked or unknown too, with a reason such as "Depends on s4.180: …". A missing calculation can't quietly produce a zero. test/no-silent-zero.test.ts checks that a tax-relevant blank either fills in by itself or blocks approval.
Parameters and versions
Rates, limits and dates come from the release's parameter file. When a parameter has versions, the tax period picks one. If none covers the period, the line is blocked with "No enacted version of … applies to …", which blocks approval. Nothing falls back to the latest value. See Versioned parameters.
Tests or formulas that change by law (not just a number) are logic rules: defineRule({ id, label, versions }) in src/rules/logic.ts, called from a calc with rule(c, MY_RULE). test/rules-versioning.test.ts is the worked example.
Diagnostics and blocking
assess(corp, ret, ev) in src/repo.ts adds the repository's own checks: tie-outs (trial balance against GIFI, prior-year ties) and the CRA data rules run against the e-file data set. It returns:
const blocking = [...ev.diagnostics, ...checks].filter(
(d) => (d.severity === "error" || d.kind === "pending") && ev.applicability[d.form]?.status !== "not-required",
)Approval is refused while blocking isn't empty. Warnings can be acknowledged with a reason (ackDiagnostic). The acknowledgement is keyed by form, field and message (diagKey), so a reworded warning needs a new one. Errors can't be acknowledged.
Explaining a number
explain(reg, ev, id, depth?) returns an ExplainNode tree of the fields a value was built from. The app's calculation trail and the context panel's How it adds up use it. formState(reg, ev, ret, formId) in src/engine/status.ts gives the per-form applicability, support, preparation and review state the forms list shows.
Tests to read first
test/engine.test.ts: the graph, propagation and overrides.test/maple-ridge.test.tsandtest/birchwood.test.ts: full returns against hand-worked results intest/scenarios/*.md.test/corpus.test.ts: replays stored returns. It guards engine and helper code, which release manifests don't pin.test/cpa-2021.test.tstotest/cpa-2026.test.ts: per-year cases, including boundary dates.
Related
- FormsWrite or change a form definition: fields, tables, applicability, checks, references, support level and form revisions.
- Versioned parametersRead and edit a release's parameter file: entries, versions that apply by date, enactments, citations and what loadParams rejects.
- State, actions and permissionsRead and write firm data through the repository and store, check permissions with can(), and keep the audit trail and undo working.