Run artifact

workspace/targets/LayersTesting/blueprint/FEATURE-Balance-Invariant.md

FEATURE: Balance Invariant

FieldValue
Version20261020 V1
DescriptionEnforce non-negative account balances before every transaction mutation.
Depends OnFEATURE-Transaction-Management.md, FEATURE-Transaction-Loading.md
Providesprospective balance validation
Consumestransactions.create, transactions.update, transactions.delete

Purpose

Every posting, amendment, deletion, transfer, and transaction load computes the affected account balances before writing. Any negative result rejects the complete command with no table changes.

Invariant

For each account, the sum of all persisted transaction amounts must be greater than or equal to zero after every successful command.

Database Interface

Programmatic Acceptance

=== AC negative-posting-rejected ===
Intent: A transaction that would make an account balance negative is rejected atomically. import os import tempfile from pathlib import Path from cli.testing import cli_runner

with tempfile.TemporaryDirectory() as d: os.environ["DATABASE_PATH"] = str(Path(d) / "ledger.db") assert cli_runner("init")[0] == 0 assert cli_runner("account", "add", "A1", "Drawer")[0] == 0 assert cli_runner("category", "add", "C1", "Supply")[0] in (0, 2, 3) assert cli_runner("txn", "post", "A1", "C1", "-1.00", "2026-01-01")[0] == 3 assert cli_runner("txn", "get", "1")[0] == 4 === END AC negative-posting-rejected ===

=== AC negative-amend-rejected ===
Intent: An amendment that would make the account balance negative leaves the transaction unchanged. import csv import os import tempfile from pathlib import Path from cli.testing import cli_runner

with tempfile.TemporaryDirectory() as d: os.environ["DATABASE_PATH"] = str(Path(d) / "ledger.db") assert cli_runner("init")[0] == 0 assert cli_runner("account", "add", "A1", "Drawer")[0] == 0 assert cli_runner("category", "add", "C1", "Sales")[0] in (0, 2, 3) assert cli_runner("txn", "post", "A1", "C1", "5.00", "2026-01-01")[0] == 0 assert cli_runner("txn", "amend", "1", "-6.00")[0] == 3 code, out, _ = cli_runner("txn", "get", "1") assert code == 0 assert list(csv.reader(out.splitlines()))[1][4] == "5.00" === END AC negative-amend-rejected ===

=== AC negative-delete-rejected ===
Intent: Deleting the only positive transaction is rejected when it would make the balance negative. import os import tempfile from pathlib import Path from cli.testing import cli_runner

with tempfile.TemporaryDirectory() as d: os.environ["DATABASE_PATH"] = str(Path(d) / "ledger.db") assert cli_runner("init")[0] == 0 assert cli_runner("account", "add", "A1", "Drawer")[0] == 0 assert cli_runner("category", "add", "C1", "Sales")[0] in (0, 2, 3) assert cli_runner("txn", "post", "A1", "C1", "5.00", "2026-01-01")[0] == 0 assert cli_runner("txn", "delete", "1")[0] == 3 assert cli_runner("txn", "get", "1")[0] == 0 === END AC negative-delete-rejected ===

User Acceptance

Guardrails