The data model, roles and the clock
What the Worker stores, who may read and change it, how sign-in works, and how a local run pretends it is another day. This page describes the code as it is. If the two ever disagree, the code wins: fix this page.
Local only for now
Nothing is deployed. Sign-in checks the Cloudflare Access token on every request; only local runs and tests use an unchecked stub login instead (below). Every example uses the made-up Addams household (Gomez is the admin, Morticia the member). The reasons behind the big choices are in ADR 0014 and ADR 0018.
Where it lives
- One database, Cloudflare D1 (SQLite), described in
apps/worker/src/db/schema.tswith Drizzle. SQL migrations inapps/worker/migrations/build it; a test fails if the schema and the migrations drift apart. - No money math in the database or the Worker. The Worker loads rows, hands them to
packages/core, and shapes the answer. Money is whole cents (ADR 0006); dates areYYYY-MM-DDdays on the household's clock; moments are ISO times in UTC. - The database refuses bad rows even if a route forgets to check: a cents column holds only whole numbers, a flag only 0 or 1, a date only a real date, and a fixed choice (an entry's status, say) only one of its listed words.
The tables
Seventeen tables, grouped by what they are for.
People and Settings
| Table | What it holds |
|---|---|
people | Each person: name, role (Admin or Member; none for a child, who has no login), the tag typed in notes, a built-in avatar key, the sign-in name. |
settings | Household facts, one row per key per start day. A change is a new row that takes over from a later day, never an edit. |
Settings include the time zone, the cushion, the mortgage set-aside, the reconcile mode (live, dry-run or off), the optional spending limit for Add it, and the admin's phone number. The phone number is never sent as a field of its own: it appears only inside the ready-made text-message link (and, for the Admin, in Settings).
One Settings row, simplefin_access, is not a household fact: its value is empty and its sealed_value column holds the SimpleFIN connection, encrypted (migration 0004; no new table). It is never listed or returned: the Admin's Settings page shows only connected or not connected, and a disconnect sets the column back to empty. How it is kept safe is in the threat model; the key (SETTINGS_KEY) is in Deploying.
The plan and the ledger
| Table | What it holds |
|---|---|
recurring | Bills, paychecks and transfers with their schedule (weekly, monthly or yearly) and a start and end day. |
envelopes | Budget and fund envelopes: weekly or a custom date range, private or not, closed or open. |
envelope_weekly_amounts | A weekly envelope's amount from a period's first day. A change is a new row. |
envelope_moves | Money moved from one envelope to another inside one period. |
entries | One amount on one day: Planned, Charged, Pending or Posted, typed in or from the bank, with a note. It also keeps the day the bank posted it, once it did, and who ticked Private. |
entry_parts | The envelopes an entry is split across (one row for a plain charge, two or three for a split). |
roll_state | Working data for reconcile: the day an entry started rolling forward, and the day its bank item vanished. |
ledger_work | The Lynx Ledger's short-lived working rows: the answer a request got (so a retry writes once) and the copy an Undo needs. Wiped after 30 minutes. |
Matching words
| Table | What it holds |
|---|---|
aliases | Words that pick out a merchant, with its usual envelope and owner. |
money_movements | Words that mean a kind of money movement (the kinds themselves are fixed in code). |
cards | A card's last four digits and whose it is, with dates. |
accounts | Which bank account is checking, savings or the trust, and the bank's last balances. |
The bank and what reconcile did
| Table | What it holds |
|---|---|
reconcile_runs | One row per run: when, live or dry run, how it ended, its counts and the balance check. |
pull_log | One row per account per bank pull. Never changed after it is written. |
job_health | A single row the "is the bank data fresh?" line reads. |
audit_log | Who changed what and when, with the row before and after, for every write. Never changed after it is written. |
What is computed, never stored
- Locks. An entry stores only whether reconcile confirmed it. A Posted, confirmed entry is locked; a day locks from its entries and the bank's balance. Both are worked out when asked.
- The bank's posted day. An entry's day is where it sits (a weekend's posts spread over the closed days, an older entry typed on another day).
entries.posted_onkeeps the day the bank itself posted it, set by reconcile when it adds or posts an entry, andGET /api/entriessends it aspostedOn(null until then, and for entries written before it was kept). It is a fact for words only: it changes no balance, lock or placement. - Balances. The only balances kept are the ones the bank last reported (on
accounts). Every other balance is worked out from the entries. - The forecast. The day-by-day balances and each envelope's reserve are built each time they are asked for.
Nothing is deleted
Rows are archived (they get an archived_at time), never removed.
- Database triggers refuse a
DELETEon every history table (one exception: the Lynx Ledger's Remove really deletes a Planned or Charged entry the bank has not confirmed), andaudit_logandpull_logrefuseUPDATEtoo. - A second set of triggers refuses an
INSERTthat would replace a row, because SQLite's replace deletes the old row without a trace. A row's id (and a setting's key and day) never changes. - Only
settingsandjob_healthmay be replaced on purpose.entry_parts,roll_stateandledger_workare working data and can be deleted. The Worker's own code never uses replace or upsert, and a test checks its source for them. - Triggers stop a stray query. They do not stop someone who has the right to change the database's structure.
Undo, Skip this one, Ended and Drop this? all work by archiving. See the Reconcile page.
Roles
The app has two roles, set on the person's row in people. A person with no role has no login.
| Role | Who in the Addams household |
|---|---|
| Admin | Gomez |
| Member | Morticia |
| none | Wednesday and Pugsley |
What each role may do
| Area | Admin | Member |
|---|---|---|
| Read the entries, envelopes, forecast, mortgage pot, people and a role summary | Yes | Yes |
| Open Partner's page and the dashboard (each is built for the person asking) | Yes | Yes |
| Add it, Add a charge, Move money | Yes | Yes |
| Save a note on a payment that needs one (the Member's job) | No | Yes |
| Undo an entry they added | Yes | Yes |
| Undo an entry somebody else added | Yes | No |
| Read Settings and the recurring items, aliases, cards, money movements | Yes | No |
| Read the SimpleFIN status and weekly envelope amounts | Yes | No |
| Paste a SimpleFIN setup token, or disconnect SimpleFIN | Yes | No |
| Save or remove an alias (a removal archives it) | Yes | No |
| Open the Reconcile page and press its buttons | Yes | No |
| See the trust account's balance | Yes | No |
| See the reconcile mode, last good pull and errors | Yes | No |
A Member is refused with a 403 in plain words ("Only an Admin can ..."), before the request's inputs are even read, so a refusal teaches them nothing about the route.
What a Member never sees
The Worker removes these before an answer leaves it, so the page never has them to hide:
- The trust account. A Member sees only the mortgage pot built from it.
- Reconcile detail. A Member's "is the data fresh?" answer is the small version: whether there is trouble and the last good live run. No mode, login state, bank day or error text. The Reconcile link is there but disabled.
- Settings warnings. Problems with a setting (a phone number that isn't one, a clashing tag spelling) go to the Admin only.
- Other people's private envelopes. Only the person who made a private envelope sees its name, tag and id. Everyone else, the Admin too, sees the name "Private", an opaque id, and
#privatewhere a note had its tag. The numbers stay, so the totals still add up. Archiving never un-hides one. - How someone else's entry has changed, in Recently added and Undo. The facts behind those two (edited since it was added, confirmed by reconcile) go to the Admin and the person who added it. For anyone else the entry reads as untouched and Undo says it isn't theirs. This is only those two. The entries list (
/api/entries) still returns every entry's confirmed flag, previous amount, adder and change time to a Member.
The table that keeps it honest
apps/worker/src/api/access.ts lists every route as public, any signed-in person or Admin. A test fails when a route is missing from the list, when a listed route doesn't exist, or when a Member can reach an Admin row. Settings, recurring items, aliases, cards, money movements, SimpleFIN, unlocking and weekly amounts are Admin only. The writes for those are stubs for now (except the Reconcile buttons, the SimpleFIN token and disconnect routes and saving or removing an alias, which are real): a Member gets 403, an Admin gets 501 "not implemented", and nothing is written.
Sign-in
Every route but /api/health needs a login: a Cf-Access-Jwt-Assertion header holding a token of three parts. Either way the Worker reads the token's sub (who it is), finds the person whose sign-in name matches, and uses that person's role from the database as the role of the request. A token naming nobody is 403, and so is a person who is archived or has no role. A role is never read from the token.
What differs is whether the token is checked:
| Mode | When | What the Worker does with the token |
|---|---|---|
| Verified | Every deployed environment (the variable AUTH_MODE is not stub) | Checks the signature, issuer, audience and expiry against the keys Cloudflare Access publishes for the team. |
| Local stub | Only when AUTH_MODE is exactly stub: the local config and tests | Reads the sub without checking the signature, so a local run and the tests can sign in as Gomez or Morticia. |
In the verified mode a token that is unsigned, signed with the wrong key, meant for another team or application, expired, or using a swapped algorithm is the same 401. The Worker needs two settings, TEAM_DOMAIN and POLICY_AUD, which the generated demo and production configs carry; if either is missing or odd, every route but /api/health is 401 (it fails closed), and the unsigned stub tokens are refused. Who may sign in at all (the Access policy and MFA) is set in Cloudflare, not in this repo. The exact checks are in apps/worker/CLAUDE.md.
AUTH_MODE=stub is set only in the local wrangler.jsonc and the test setup. The config generator never writes it, and a test proves a Worker with only the generated settings has the stub and the X-Now override off. The token is never logged. Because the roles come from the database, the role checks and the masking are the same in both modes (see ADR 0018).
The clock and the now override
Every route gets the same "now" from one place (api/clock.ts), and no other file asks the computer for the time. That is what lets a test or a demo pin the day.
- Today is the calendar day in the household's time zone (the
time_zonesetting), not in UTC. - The override is the
X-Nowrequest header, a full moment with a zone, like2026-12-03T15:00:00Z. - Only when allowed. The override works only while the variable
NOW_OVERRIDEis exactlyallowed. Otherwise the request is refused with 400now_override_not_allowed. The header is never quietly ignored and never used. The check comes before sign-in, so everyone gets the same answer. - A bad value (not a full moment, or a day outside 2000 to 2100) is a 400
invalid_now, everywhere. - Every signed-in answer carries an
X-Server-Nowheader with the moment the request used, so the app keeps time from the server and never from the device./api/healthignoresX-Now.
Like AUTH_MODE, NOW_OVERRIDE=allowed lives only in the local config. The app's own ?now= and ?login=gomez / ?login=morticia address options exist only in a build made for local work, npm run app:build:local; a production API build ignores them, and a test looks for them in its output. The demo build (mock data) has its own ?now= and ?as= switches, which only the mock reads.
Try it locally
The local database is a SQLite file made by Wrangler. Nothing here touches Cloudflare. Run these from the repo root:
| Command | What it does |
|---|---|
npm run db:reset:local -w @home-budget/worker | Wipes the local database, applies the migrations and loads the Addams household. |
npm run db:seed:demo:local -w @home-budget/worker | Adds the Reconcile demo rows (below). Run it after a reset. |
npm run app:build:local | Builds the app for the local Worker (API data, stub login). |
npm run app:build:public-demo | Builds the files-only public demo (mock data that follows today's date; ADR 0021). |
npm run dev:worker | Starts the Worker and the built app at http://localhost:8787. |
npm run dev:token -- gomez | Prints a stub token for the Admin (morticia for the Member). Add --curl /api/me for a ready curl. |
Then open http://localhost:8787/dashboard?login=gomez, or /spend?login=morticia for Partner's page. Add &now=2026-12-03T15:00:00Z to pin the clock to the seed's Thursday.
The seed and the demo rows
- The seed is every table's rows from
packages/mocks, written as SQL. The Worker has no route that seeds anything. It starts the reconcile mode atdry-run, and gives Gomez and Morticia their stub sign-in names. Running it twice fails on the first duplicate: usedb:reset:localto start over. - The demo rows are opt-in and local only. The seed has a live reconcile run and one amount that differs, but nothing waiting on the Admin, so the Reconcile page's lists would all be empty.
db:seed:demo:localadds a few made-up entries: three that Didn't post (one is Thing's manicure, so Ended has a schedule to end), one Drop this? and one Not at bank. None has an envelope or an adder, so they stay out of Partner's envelopes and Recently added. - Safe to run twice. Each row is inserted only if it isn't there yet (the no-replace triggers would refuse a plain insert over a row).
- Never part of the tests' seed.
npm run smoke:localapplies them, then drives the app in a real browser.
Not built yet
- The scheduled bank pull and reconcile run. There is no scheduled handler and no bank client in the Worker: the runs, pulls and job-health row on this page's tables are seeded rows only.
- Real sign-in and the checks it brings (see the threat model).
- Writes for Settings, recurring items, aliases, cards, money movements, unlocking and weekly amounts: Admin only, and an Admin gets "not implemented" for now. (SimpleFIN's token and disconnect are real; its "test the connection" route and the bank pulls themselves are not built.)
- The list of tag spellings has no editor; it is a Settings row.
- Two presses at once can both pass core's check before either writes (for example an envelope's last dollar). The write is one atomic batch, but the check and the write are two steps.