Skip to content

2.05 Accounting Periods, Closing and Trial Balance: Exit Evidence

Evidence · Step: 2.05 Accounting Periods, Closing and Trial Balance

Step 2.05 Accounting Periods, Closing and Trial Balance
Date 2026-11-14
Database PostgreSQL 18 (postgres:18-alpine, compose.yaml) in Docker Desktop on Windows 11
Result Passed
Deliverable Where
Periods migrations/0007_periods.{up,down}.sql: periods, journal_entries.period_id, the guards and triggers (MZ008); crates/engine/src/periods.rs (Period, current_period, all_periods, the posting lock)
Close crates/engine/src/close.rs: close_period, begin_close, finish_close, period_snapshots, PeriodFinding; period_snapshots table
Reports crates/engine/src/reports.rs: trial_balance(AsOf::Now | Period | Time), general_ledger(account, from, to)
Operator command mezgeb-cli close-period --next LABEL
Runbook (the exit gate) docs/runbooks/close-period.md
Decision record ADR-0016: periods follow posting order; a close switches periods under a lock
Tests crates/engine/tests/periods.rs; the CLI’s parser tests
Documentation spec.md §6, invariants.md, INV-10, db-schema.md, Periods

The work arrived in six pull requests: posting into the open period (#63), the close and the reports (#64), the CLI command (#65), the runbook and ADR-0016 (#66), the reference docs (#67), and this evidence.

Task Done
periods and period_snapshots tables Yes: migration 0007. One open and at most one closing period (partial unique indexes); a period only moves open → closing → closed; snapshots are append-only
close_period: 1. block new postings dated in it Yes: the switch. Postings take the period lock shared and read the open period under it; the switch takes it exclusively, waits for postings under way, records the period’s last entry and opens the next period. Every later posting goes to the next period
2. run verify for the period Yes: every entry of the period lies in its range and balances in every currency, and each account’s totals are computed two ways (previous snapshot plus the period’s lines, and the stored running totals) and must agree. The full mezgeb-cli verify runs after the close in the runbook
3. write snapshots, 4. mark closed Yes, in one transaction, only if verification is clean. With findings, nothing is written and the period stays closing; running the close again retries it
A DB trigger rejecting lines whose entry’s period is closed Yes: entries and lines are refused unless their period is open when written, and entries are checked again at commit (MZ008)
Reports: trial_balance(as_of) and general_ledger(account, from, to) Yes

Effective date vs posting date. An entry belongs to the period open when it is posted. Its original date is its event_time. A late posting, such as an offline payment from last week, goes to the current open period with that reference to its original date, as the step asks. The reports use the posting date (recorded_time); a report by event date reads event_time.

Requirement Test Result
Posting into a closed period is rejected (engine and DB) posting_into_a_closed_period_is_refused Pass. The database refuses an entry written into a closed period (MZ008), a line added to one of its entries, reopening a period, changing a snapshot and deleting a period. The engine refuses to post with NoOpenPeriod when no period is open
Snapshot + lines after it = current balance a_snapshot_plus_later_lines_is_the_current_balance Pass, for every account, after 40 more random postings
Close while postings are in flight: no posting lands in the closed period closing_while_postings_are_in_flight (8 writers, 20 connections, 480 postings) Pass: five closes while the writers run, and one after. Every close is clean, no entry of any period lies after its end, all 486 entries are accounted for, and every snapshot equals the recomputation
The trial balance always balances reports_balance_and_agree_with_each_other; every day of the 30-day run Pass: as of now, at a period’s end and at a time. The trial balance at a time before the close equals the one from the snapshot, and an unclosed period is PeriodNotClosed. The general ledger split at any time opens where the first half closed

Also tested:

  • A close that stopped part-way. After the switch, the next close_period finishes the same period and opens nothing new.
  • A tampered period stays closing. A line slipped into one of the period’s entries, around every trigger, makes verification report EntryUnbalanced and TotalsDisagree. Nothing is written and the period stays closing.
  • The CLI. mezgeb-cli close-period was run against a migrated local database: it closed three periods in turn and printed each range, and an empty period prints “no entries”.

Snapshots verified against the full recomputation for 30 simulated days. thirty_days_of_snapshots_match_the_full_recomputation posts 5 to 34 random payments, transfers and deposits a day, then closes the day. After each of the 30 closes:

  • the close is clean;
  • every account’s snapshot equals the sum of every line up to the period’s end, recomputed from scratch;
  • the trial balance from the snapshot balances, and equals the one from the stored totals.

All 30 days pass.

The close procedure is documented as a runbook. docs/runbooks/close-period.md covers what the close does, what to check before, the command, a clean result, the checks after (full verification, the day’s trial balance, the record), what to do for every failure and exit status, the month-end close, late postings, and what never to do.

Every posting now takes one more advisory lock, which is shared and uncontended, and fires two more triggers. On this machine (Windows, Docker Desktop), two runs of mezgeb-bench before and after this change, 8 seconds each:

Scenario Before (two runs) After (two runs)
2-line, uniform accounts 191, 243 /s 183, 209 /s
4-line, uniform accounts 168, 206 /s 175, 182 /s
2-line, one hot account 52, 53 /s 51, 52 /s

The run-to-run noise here (about 25%) is larger than any difference, so the cost cannot be told apart from it. Every run verified clean. The performance step (3.01) measures it on the benchmark machine.

  • A signed journal hash at each close: step 2.09 (audit).
  • Reports by event date, and by accounting month: from the daily periods, in step 3.05 (reporting).