Skip to content

2.01 Operations Layer and Posting Templates: Exit Evidence

Evidence · Step: 2.01 Operations Layer and Posting Templates

Step 2.01 Operations Layer and Posting Templates
Date 2026-11-14
Database None needed: mezgeb-ops is pure, and the engine is unchanged
Result Passed
Deliverable File
mezgeb-ops crate crates/ops/src/lib.rs
Template trait, roles, legs, draft crates/ops/src/template.rs
Template registry crates/ops/src/registry.rs (TemplateRegistry, RELEASED)
Stable errors 2001–2013 crates/ops/src/error.rs (TemplateError)
First template, p2p_transfer.v1 crates/ops/src/p2p_transfer.rs
Tests crates/ops/tests/templates.rs, plus unit tests in error.rs and template.rs
Documentation spec.md 4.6, architecture.md §4
Task Done
mezgeb-ops crate with a Template trait: typed request → EntryDraft Yes. Template::request builds the typed request (for p2p_transfer.v1, P2pTransferRequest) and Template::book books it. draft::<T> turns a typed request into an EntryDraft; TemplateRegistry::draft does the same for an untyped OperationRequest that names its template
TemplateRegistry keyed by name.vN; entries store template name and version Yes. TemplateId parses and prints name.vN. The draft records the template in its metadata under the reserved key template, which the engine stores with the entry and includes in the idempotency fingerprint. See why metadata
A template may only reference accounts by role, which the request resolves Yes, by construction: a template returns Legs, which name a role (&'static str), a side and an amount. There is no way for a template to name an account ID. Each Role also lists the chart account codes it may be bound to (TM-07)
First template: p2p_transfer.v1 Yes: payer_wallet and payee_wallet, both 2010 Customer wallets

15 tests in mezgeb-ops, run by cargo test --workspace in CI:

Requirement Test
Each template is tested against its step 1.01 worked example p2p_transfer_v1_books_worked_entry_e06: the E06 table is read from docs/accounting/entries/payments.md with the same parser as the core’s worked-entry tests. OB–E05 are replayed into the reference ledger, then the template’s draft must equal E06’s lines exactly, and it posts, leaving Abebe with 850 ETB and Hana with 150 ETB
Property test: any valid request yields an entry that validate accepts any_valid_p2p_request_validates: 2–5 funded wallets, any payer, any other payee, any amount up to the payer’s balance, random metadata and event time. The draft validates, with the expected lines, total, metadata and event time. any_request_is_drafted_or_refused_cleanly sends arbitrary template IDs, role bindings and amounts: each is refused with a code in 2001–2013, or drafted into two positive lines naming p2p_transfer.v1
Unknown template or version → stable error every_refusal_has_its_stable_code: teleport.v1 → 2002, p2p_transfer.v2 → 2003, malformed IDs → 2001, and one case for every other rule. malformed_template_ids_are_refused covers 16 malformed IDs; codes_are_unique_and_in_the_2000_range pins the numbering

Also tested:

  • the_typed_request_books_the_same_draft: draft::<P2pTransferV1> and the registry give the same draft.
  • the_template_does_not_check_funds_the_posting_rules_do: a transfer from an empty wallet drafts, then validate refuses it with 1012. Templates check structure; the posting rules check money.
  • a_template_that_books_an_undeclared_role_is_refused: a defective template gets 2013 rather than a wrong entry.
  • the_standard_registry_has_every_released_version: the registry registers exactly RELEASED, which is append-only.

The engine has no knowledge of any template name. Checked by the_engine_knows_no_template_name: no file under crates/engine/src, crates/core/src or migrations/ mentions any released template name, and crates/engine/Cargo.toml does not depend on mezgeb-ops. The layering is one way: mezgeb-ops depends on mezgeb-core only.

A new template can be added without touching the engine. a_new_template_and_version_can_be_added_from_outside writes two templates in the test file, against the public API only: test_fee_sweep.v1 (between the two bank accounts, 1010 and 1020) and a v2 that reads an extra amount. Both are registered next to the standard templates, v1 is drafted and posted to the reference ledger, and the posted entry names test_fee_sweep.v1. Each version keeps its own rules: v1 refuses the v2 amount (2005), and v2 requires it (2004). Registering a version twice is refused. No engine or core file changed in this step.

The architecture lists “template + version” as a field of the journal entry. This step stores it as the metadata key template instead of a new column, because:

  • the engine stays exactly as it was, which is what the acceptance criterion asks for; a column would make the engine carry a template concept, even without names;
  • the metadata is already stored with the entry, returned with it, and part of the idempotency fingerprint, so the same key with a different template is refused as IdempotencyKeyReused;
  • template is reserved: a request that sets it is refused (2012).

If the API (2.06) or reporting (3.05) needs to query entries by template efficiently, an expression index on metadata->>'template' gives that without a schema change to the entry itself. Raw entries posted directly through the engine can still set any metadata; step 2.08 closes that by letting clients post only through templates (TM-07).