Skip to content

0012 engine style

github-actions[bot] edited this page Nov 18, 2026 · 2 revisions

The engine is small pure functions, in the style of date-fns

Status: accepted · Implementation: done · Date: 2026-11-18

From LibreLifts ADR 0023. Built 2026-11-18: isSetDone, nextSetNumber, planFromRoutine, previousSets, workoutDuration and workoutSummary, with 31 tests. Load in a summary is added weight times reps, per unit; help and holds add nothing to it.

Context

packages/engine holds the rules of the log: how a routine becomes a workout, which sets count as "last time", what a finished set is, how a workout is summed up (0006). The app runs these rules and the website may show them. They are the part of LibreBars that must not be wrong.

Rules written as objects with state are hard to test and hard to trust. date-fns shows another way: a library of small functions that take plain values and return new ones.

Decision

  • One function per file, named after the function, with its test in a file beside it.
  • Plain data in, new values out. Inputs are read-only and are never changed.
  • No classes and no hidden state. Calling a function twice with the same arguments gives the same result.
  • Nothing impure. A function never reads the clock, storage or the network. When a rule depends on the time, the time is an argument.
  • No dependencies, inside the workspace or outside it. The engine declares the shapes it takes itself and does not import them from contracts.
  • Every function is exported by name from the package's index.
  • Loads in different units are never added together. A stored load is kept as entered (0024), so a sum is given per unit.

The same habit is kept outside the engine: a function that reads or writes data takes the database and the current time as arguments.

Consequences

  • Every rule can be tested with a table of inputs and outputs, and read on its own.
  • Rules that need many inputs take them all as arguments, which makes some calls long.
  • State, such as a person's history, lives in the apps. The engine only computes from what it is given.
  • The engine's input shapes and the stored rows are written separately, and the apps map one to the other.

LibreBars

Clone this wiki locally