Bank8:Branch - Detailed Design Document
1. Scope & Context
Bank8:Branch owns: the branch schema (schema-per-service - domain
tables + branch.outbox / branch.audit_journal producer tables), the
office hierarchy, staff records, tellers, cashier sessions, cash
allocation and settlement, denomination counts, the shift summary, the
vault position, cash in transit, and the bank8:branch:* event family.
It does NOT own: money movement or balances (Bank8:Ledger), the book of account (Bank8:Accounting), customer records (Bank8:Ledger), the teller UI (apps/eteller), staff credentials or sign-in (Sentin8:Auth), or delegation journeys (Sentin8:BFF / Sentin8:Auth).
2. Runtime Architecture
Boot is shell-owned; this service spawns no embedded worker. It is a synchronous, request-driven service: the terminal drives sessions, counts, and movements, and each movement is one ledger posting.
Cash is a ledger position. The service holds no cash balance. Every
allocation, settlement, or counter cash movement builds a posting batch
and submits it to POST /v1/postings against the seeded internal
accounts (cash_deposit, cheque_clearing, clearing, suspense,
liquidity).
2.1 Posting mapping (normative)
The ledger classifies an instruction by its type
(debits_source() in bank8-ledger/src/handlers/postings.rs) and
applies debit -= amount, credit += amount. Cash movements are all
debit-direction:
| Business event | Batch legs |
|---|---|
| Vault → teller float | WITHDRAWAL: vault account → teller drawer account |
| Teller → vault settlement | WITHDRAWAL: teller drawer → vault account |
| Teller → teller transfer | WITHDRAWAL: source drawer → destination drawer |
| Counter cash deposit | WITHDRAWAL: teller drawer → customer account |
| Counter cash withdrawal | WITHDRAWAL: customer account → teller drawer |
| Cash in transit out | WITHDRAWAL: source → suspense account |
| Cash in transit receipt | WITHDRAWAL: suspense → destination |
The shift summary is computed from cash_transactions recorded during
the session - never from a client-supplied total. A client count is
input to the variance, not to the expected figure.
3. Data Model
| Table | Purpose | RLS tier |
|---|---|---|
offices | Hierarchy: code, name, dot-path hierarchy, parent, active | env |
staff | Staff bound to an office (business record; no credentials) | env |
tellers | Cash-handling positions with per-transaction and per-shift limits | env |
cashier_sessions | Open/close with expected, counted, and variance | env |
cash_transactions | Every movement with its ledger_posting_id and denomination breakdown | env |
denominations | The currency's note/coin catalogue per environment | env |
denomination_counts | Per-session physical counts by denomination | env |
vaults | The branch's vault position object | env |
vault_transfers | Vault↔vault and vault↔logistics movements | env |
cash_in_transit | In-flight movements with source, destination, and receipt | env |
branch.outbox / branch.audit_journal | Producer tables | FORCE |
Hierarchy querying. offices.hierarchy stores the dot-path
(e.g. .0001.0007.). A subtree is a prefix scan, so "all offices under
this branch" is an indexed range query rather than a recursive walk.
Money precision: all amounts NUMERIC(28,10); counted denominations
are integers times the denomination value.
4. API Surface
| Audience | Routes | Auth gate | Events emitted |
|---|---|---|---|
| Offices | GET/POST /v1/offices, GET/PUT /v1/offices/{id}, POST …/{id}/deactivate | bank8:branch:offices:read / :write | office:created, office:updated |
| Staff | GET/POST /v1/staff, GET/PUT /v1/staff/{id} | bank8:branch:staff:read / :write | staff:created, staff:updated |
| Tellers | GET/POST /v1/tellers, GET/PUT /v1/tellers/{id} | bank8:branch:tellers:read / :write | teller:created, teller:updated |
| Sessions | POST /v1/cashiers/{id}/open, POST /v1/cashiers/{id}/close, GET /v1/cashiers/{id}/shift-summary, GET /v1/cashiers | bank8:branch:cashiers:read / :write | cashier:opened, cashier:closed, cash:variance |
| Cash | POST /v1/cash-transactions, GET /v1/cash-transactions, POST /v1/cashiers/{id}/allocate, POST /v1/cashiers/{id}/settle | bank8:branch:cash:read / :write | cash:allocated, cash:settled |
| Vault | GET /v1/vault/position, POST /v1/vault/transfers, POST /v1/vault/transfers/{id}/receive | bank8:branch:vault:read / :write | vault:transferred |
| Denominations | GET/POST /v1/denominations | bank8:branch:offices:read / :write | - |
| Admin/SDUI | GET /v1/admin/ui/{page}, GET /v1/admin/overview | bank8:branch:admin | - |
Shift summary contract (server-generated, immutable after close):
GET /v1/cashiers/{id}/shift-summary
200 { "session_id": "...", "teller_id": "...", "office_id": "...",
"opened_at": "...", "closed_at": "...",
"movements": [ { "kind": "cash_deposit", "amount": "…",
"denomination": "CAD",
"ledger_posting_id": "…" } ],
"expected_cash": "…", "counted_cash": "…",
"variance": "0.00", "variance_explained": false }
variance is always present: a zero is an explicit assertion that the
drawer balanced. variance_explained flips only when an audited
adjustment referencing the session is recorded.
Admin/SDUI pages: overview, offices, staff, tellers,
sessions, vault, denominations.
5. Cross-Cutting Concerns
- Auth: platform token pipeline; Shield is the only gate. Staff identity is the platform's; Branch holds only the business record.
- Tenant + RLS:
set_tenant_contexton the same connection as the query; explicitWHERE env_id = $N; every tableENABLE+FORCE ROW LEVEL SECURITY. - Ledger interaction: posting via
POST /v1/postings; internal account reads via the public account API (staff-realm, internal-account scoped). No grant onledger.*. - Idempotency: every cash movement carries
branch:{session}:{kind}:{sequence}as its ledger idempotency key, so a retried terminal submission cannot double-move cash. Session open and close are unique per teller while open, and close is unique per session. - Event emission: outbox row in the same transaction as the domain write → relay → Bus. A variance emits on close only.
- Audit:
branch.audit_journalin the same transaction plus Sys audit push. Session close, adjustment, transfer receipt, and limit changes all record an actor. - TLS: PQ-TLS via
sentin8-tls.
6. Failure Modes & Recovery
| Failure | Behaviour |
|---|---|
| Movement without an open session | 409 with an explicit reason; nothing written. |
| Limit exceeded | 422; the movement is refused before any posting. |
| Ledger rejects | Branch surfaces the ledger's reason verbatim; no local cash record is created. |
| Local commit fails after ledger accept | Compensating reversal posting plus alert, mirroring the lending pattern. |
| Terminal crash mid-shift | Expected cash is recomputed from recorded movements on reload - the client's cached total is never authoritative. |
| Double-submitted movement | Absorbed by the deterministic ledger idempotency key. |
| Cash in transit never received | Visible as open in-transit with age; never silently landed. |
7. Concurrency & Performance Model
Pool capped (3–5). The hot path is a single cash movement: one ledger posting plus one local row. Session rows are row-locked on write so two terminals cannot close the same shift concurrently; the ledger's balance-row serialization handles the money side.
Shift summary is computed from indexed reads scoped by session_id;
the vault position is derived from movements, not maintained as a
mutable counter.
8. Invariants
- No session, no cash. A movement requires an open session at both source and destination where applicable.
- Every movement is a posting.
cash_transactions.ledger_posting_idis mandatory. - Limits are enforced pre-posting, not reported afterwards.
- Closed summaries are immutable. Corrections are new audited adjustment records referencing the session.
- Variance is always recorded, including zero.
- In-transit lands only on receipt.
- Offices with open work cannot be deactivated.
- No cross-service table access.
9. Testing Strategy
- Policy suite (
tests/policy.rs) - seeded scope-mirror text with the handler-action parity matrix. - DB invariants (
tests/db_invariants.rs, live bank8 DB): FORCE-RLS rejection; a single open session per teller; movement without a session rejected; closed summary immutability (UPDATE rejected); variance persisted including zero; in-transit cannot be marked received twice;offices.hierarchyprefix query returns the correct subtree; nobranchview referencesledger.*. - Shift arithmetic tests - expected cash equals the sum of recorded movements across deposit, withdrawal, allocation, and settlement mixes; variance computed from a counted breakdown.
- OpenAPI drift (
tests/openapi_drift.rs). - E2E - UI-only through the BFF, driven by eTeller: open shift, deposit, withdraw, close with a count, assert the summary and variance.
10. Open Questions
- Whether the vault is modelled as a teller with special limits or as a distinct object. Current design: distinct, so drawer and vault reconcile independently.
- Multi-currency drawers: a drawer per denomination vs. one drawer with a cross-denomination count.
- Whether cash-in-transit includes external logistics providers or stays internal to the branch network.
Architecture & Design: bank8:branch and its underlying concepts were designed by Hamid Jahanzad.