Evidence: Block 3 · Foundational (block-3)
- block type: block
- date: 2026-10-27
- resulting state: closed/verified
- story points (combined assembled cost): 19652
- execution id: 20261027.142255.936Z-95bffe2d
Stories built
- Provide the
ledgerexecutable at the application root with a command registry, usage-error handling, and the stdout/stderr contract. (foundation-cli) [story]
Reusable compacts
- DATABASE_compact.md
Stacked context
- stack: python_compact.md (SP 921)
- compass: COMPASS.md (SP 1134)
- implements: FEATURE-CliEntry.md (SP 2804)
- context: ARCHITECTURE_compact.md (SP 332)
- context: DATABASE.md (SP 9825)
- stack: common_compact.md (SP 1653)
- stack: cli.md (SP 2689)
Build directory changes
- cli/init.py
- cli/commands/init.py
- cli/registry.py
- cli/testing.py
- data/app.db-shm
- data/app.db-wal
- data/logs/test_2026-10-27_102358.log
- data/logs/test_2026-10-27_102453.log
- ledger
- tests/test_cli.py
Pre-build acceptance observation
- RED: cli-registry-load (FEATURE-CliEntry.md)
intent: Registry loading is idempotent and every key is a tuple of words. error: foundation not built: cli is the package the cli foundation (cli.md) builds, and the build directory has no cli/. The criterion exercises the built application, so it was not run.
- RED: cli-unknown-command (FEATURE-CliEntry.md)
intent: An unknown command, or no command at all, exits 2 with an empty stdout and exactly one stderr line. error: foundation not built: cli is the package the cli foundation (cli.md) builds, and the build directory has no cli/. The criterion exercises the built application, so it was not run.
- RED: cli-dispatch-success (FEATURE-CliEntry.md)
intent: A registered command receives its arguments and its rows print as unquoted CSV with newline endings. error: foundation not built: cli is the package the cli foundation (cli.md) builds, and the build directory has no cli/. The criterion exercises the built application, so it was not run.
- RED: cli-usage-errors (FEATURE-CliEntry.md)
intent: A wrong argument count, a bare group word, and a group word with an unknown sub-word each exit 2 with empty stdout. error: foundation not built: cli is the package the cli foundation (cli.md) builds, and the build directory has no cli/. The criterion exercises the built application, so it was not run.
- RED: cli-domain-error-codes (FEATURE-CliEntry.md)
intent: Handlers raising Rejected, NotFound, or Operational exit 3, 4, or 1 and print nothing to stdout. error: foundation not built: cli is the package the cli foundation (cli.md) builds, and the build directory has no cli/. The criterion exercises the built application, so it was not run.
- RED: cli-unopenable-database (FEATURE-CliEntry.md)
intent: A database location that cannot be opened exits 1 with empty stdout. error: foundation not built: cli is the package the cli foundation (cli.md) builds, and the build directory has no cli/. The criterion exercises the built application, so it was not run.
- RED: cli-launcher (FEATURE-CliEntry.md)
intent: The ledger launcher at the application root is executable and exits 2 for an unknown command with empty stdout. return code: 1 stderr: Traceback (most recent call last): File "cli-launcher.py", line 4, in <module> assert os.access("ledger", os.X_OK) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AssertionError
Post-build programmatic acceptance
- PASS: cli-registry-load (FEATURE-CliEntry.md)
intent: Registry loading is idempotent and every key is a tuple of words. target interpreter: /mnt/c/Users/barlo/projects/drydock/uat/LayersTesting/runs/20261027.135355/build/LayersTesting/.venv/bin/python provisioning: not required return code: 0
- PASS: cli-unknown-command (FEATURE-CliEntry.md)
intent: An unknown command, or no command at all, exits 2 with an empty stdout and exactly one stderr line. target interpreter: /mnt/c/Users/barlo/projects/drydock/uat/LayersTesting/runs/20261027.135355/build/LayersTesting/.venv/bin/python provisioning: not required return code: 0 stdout: 2 '' "usage: unknown command 'zzfrobnicate'\n" 2 '' 'usage: no command given\n'
- PASS: cli-dispatch-success (FEATURE-CliEntry.md)
intent: A registered command receives its arguments and its rows print as unquoted CSV with newline endings. target interpreter: /mnt/c/Users/barlo/projects/drydock/uat/LayersTesting/runs/20261027.135355/build/LayersTesting/.venv/bin/python provisioning: not required return code: 0 stdout: 0 'h1,h2\nalpha,v\n' ''
- PASS: cli-usage-errors (FEATURE-CliEntry.md)
intent: A wrong argument count, a bare group word, and a group word with an unknown sub-word each exit 2 with empty stdout. target interpreter: /mnt/c/Users/barlo/projects/drydock/uat/LayersTesting/runs/20261027.135355/build/LayersTesting/.venv/bin/python provisioning: not required return code: 0 stdout: ('zzgrp2', 'ok') 2 '' "usage: wrong number of arguments for 'zzgrp2 ok'\n" ('zzgrp2', 'ok', 'a', 'b') 2 '' "usage: wrong number of arguments for 'zzgrp2 ok'\n" ('zzgrp2',) 2 '' "usage: unknown command 'zzgrp2'\n" ('zzgrp2', 'nosuch') 2 '' "usage: unknown command 'zzgrp2 nosuch'\n"
- PASS: cli-domain-error-codes (FEATURE-CliEntry.md)
intent: Handlers raising Rejected, NotFound, or Operational exit 3, 4, or 1 and print nothing to stdout. target interpreter: /mnt/c/Users/barlo/projects/drydock/uat/LayersTesting/runs/20261027.135355/build/LayersTesting/.venv/bin/python provisioning: not required return code: 0 stdout: rej 3 '' 'error: probe failure\n' nf 4 '' 'error: probe failure\n' op 1 '' 'error: probe failure\n'
- PASS: cli-unopenable-database (FEATURE-CliEntry.md)
intent: A database location that cannot be opened exits 1 with empty stdout. target interpreter: /mnt/c/Users/barlo/projects/drydock/uat/LayersTesting/runs/20261027.135355/build/LayersTesting/.venv/bin/python provisioning: not required return code: 0 stdout: 1 '' 'error: cannot open database: database directory does not exist: /tmp/drydock-database-config-h_l6e9jp/missing\n'
- PASS: cli-launcher (FEATURE-CliEntry.md)
intent: The ledger launcher at the application root is executable and exits 2 for an unknown command with empty stdout. target interpreter: /mnt/c/Users/barlo/projects/drydock/uat/LayersTesting/runs/20261027.135355/build/LayersTesting/.venv/bin/python provisioning: not required return code: 0 stdout: usage: unknown command 'zzfrobnicate'
Build summary
I'll start by inspecting the build directory and the existing files.The cli package doesn't exist yet. I'll read the persistence layer and existing tests to see what the CLI will depend on.The cli package is missing, and the ledger launcher with it. Before writing dispatch I need to confirm how open_database behaves on a bad path and what the Database close API is.I have what I need. The plan:
cli/__init__.py:main(argv) -> intdoing longest-prefix dispatch, the usage and domain error mapping, and CSV output after the handler returns.cli/registry.py:Command,COMMANDS, and an idempotentload()that merges eachcli/commands/*module'sCOMMANDS.cli/commands/__init__.py: an empty package. Commands arrive in later stories.cli/testing.py:cli_runner, in-process.ledger: a POSIX launcher that activates.venvand returnsmain's exit code.
Starting with the package.Next, the registry, the command package, the in-process runner, and the launcher.The launcher and usage paths behave. Now the test suite, using the project's cli_runner and asserting on return values and streams rather than log text.One cleanup before running: an unused rows variable in the partial-work test.The failure is real: open_database returns without touching the file, so a bad location exits 0. I need to see when Database actually connects.Connections open lazily on first use, so open_database cannot report an unopenable store by itself. I'll check whether the persistence library exposes a public way to force the connection, rather than reaching into the driver from the CLI.The SQLite driver calls mkdir on the parent directory before connecting. So missing-directory/app.db is not unopenable under the shipped library. I'll confirm that and clean up the directory my test run may have created.Opening is lazy, so the directory isn't created until a connection is made. I'll force a connection from a scratch directory so the build tree stays clean.The -I flag also ignores PYTHONPATH, so I'll add the build path explicitly.Confirmed: the shipped library creates a missing SQLite parent directory on connect. So under the library as written, the missing/app.db location in the acceptance criterion would open and exit 0. The CLI can't fix that inside the library, which must not be edited. The CLI can refuse a SQLite location whose directory is absent, before the library creates it. That keeps a typo from silently creating a store, and it is a bounded choice I'll record for the user.I'll tighten the unopenable test so it also proves no directory gets created, using tmp_path for isolation.Running the full suite again, then checking the ledger launcher manually for the unopenable-location case the criterion uses.All 53 tests pass through bin/test.sh (exit 0 in the log; the exit=0 line above is tail's status, not the suite's). Last, I'll check whether git will record the launcher's executable bit, since I can't stage anything myself.Everything is in place. The launcher is executable on disk, but core.fileMode is false in this repo, so git may record it as 100644 when committed. I'll flag that rather than change git config.
Result summary before the final block: the ledger story is built and the full suite passes (53 tests, bin/test.sh exit 0). I did not commit, as instructed.
<reusable-compact filename="DATABASE.md">
Database: LayersTesting (version 20261027 V1). Build order 1. Provides persistence.accounts, persistence.categories, persistence.transactions.
Access: open_database(dsn) returns Database; db.<table>.<op>(...). Money: integer cents everywhere. No aggregate operations; callers compute counts, sums, next txn_id from list results.
Operations (input -> output; * = match key):
- accounts.create(account, name) -> (account, name)
- accounts.get(account*) -> (account, name) | None
- accounts.update(account*, name) -> (account, name)
- accounts.delete(account*) -> (account)
- accounts.list() -> [(account, name)] order account asc
- accounts.clear(min_account) -> count; removes account >= min_account; "" removes all
- categories.create(category, description) -> (category, description)
- categories.get(category*) -> (category, description) | None
- categories.update(category*, description) -> (category, description)
- categories.delete(category*) -> (category)
- categories.list() -> [(category, description)] order category asc
- categories.clear(min_category) -> count; "" removes all
- transactions.create(txn_id, date, account, category, amount, memo) -> (txn_id, date, account, category, amount, memo)
- transactions.get(txn_id*) -> row | None
- transactions.update(txn_id*, amount) -> row
- transactions.delete(txn_id*) -> (txn_id)
- transactions.list(account?, category?, start_date?, end_date?) -> [row] filters account, category, date>=start_date, date<=end_date; order txn_id asc
- transactions.clear(min_txn_id) -> count; 0 removes all
Schema:
- accounts(account TEXT PK, 1-12 chars A-Z0-9; name TEXT NOT NULL, 1-40 chars)
- categories(category TEXT PK, 1-12 chars A-Z0-9; description TEXT NOT NULL, 1-40 chars)
- transactions(txn_id INTEGER PK; date TEXT YYYY-MM-DD; account FK accounts; category FK categories; amount INTEGER cents signed, never zero; memo TEXT 0-60 chars default ''); INDEX(account, date)
Config: DATABASE_URL (SQLite path, optional sqlite:// prefix, wins), DATABASE_PATH (fallback), default data/app.db.
Guardrails: only the persistence package opens a driver; amounts never float; counts/balances never hardcoded. </reusable-compact>
RESULT: SUCCESS
FILES CHANGED:
- ledger
- cli/init.py
- cli/registry.py
- cli/commands/init.py
- cli/testing.py
- tests/test_cli.py
SUMMARY:
ledgeris a POSIX launcher at the application root. It changes to the project root, activates.venvwhen present, and runscli.main(sys.argv[1:]), exiting with its return value.cli/__init__.pyprovidesmain(argv) -> int. It matches the longest registered command-word prefix. Unknown, empty, and wrong-count invocations exit 2 with one stderr line and empty stdout. The handler runs before any stdout is written. Output is unquoted CSV with\nendings.LedgerErrormaps to its exit code (3, 4, or 1). Any other exception exits 1.cli/registry.pyprovidesCommand(handler, min_args, max_args),COMMANDS, and an idempotentload()that merges eachcli/commands/*module'sCOMMANDS.cli/commands/is an empty package. Commands arrive in later stories.cli/testing.pyprovidescli_runner(*args), which runs in process.tests/test_cli.pyhas 37 tests covering the registry, dispatch, usage errors, domain exit codes, CSV output, the unopenable-store path, and one subprocess test of the launcher.
Verification run: sh bin/test.sh → 53 passed (log exit 0). This includes the existing foundation tests. I also ran ./ledger zzfrobnicate by hand and got exit 2 with empty stdout.
Choices I made for you to review:
- SQLite location pre-check. The shipped SQLite driver creates a missing parent directory on connect, and
open_databaseis lazy. Somissing/app.dbwould open and exit 0, which breaks thecli-unopenable-databasecriterion. I added a check incli/__init__.pythat refuses a SQLite location whose directory does not exist (exit 1). PostgreSQL DSNs skip the check. This also stops a typo from creating an empty store. If you'd rather the library create directories, this check should change on replan. - Launcher changes to the project root. This makes the default
data/app.dbroot-relative, which matches the project layout. A relativeDATABASE_PATHis therefore resolved against the project root, not the caller's directory. - No
argparseparser. Dispatch is by command-word prefix, becauseargparsesubcommands don't fit multi-word commands likeaccount add.
<blueprint-decisions> [{"spec":"FEATURE-CliEntry.md","severity":"Material","subject":"Unopenable database detection","decision":"open_database is lazy and the shipped SQLite driver creates a missing parent directory on connect, so the criterion's 'missing/app.db' would open and exit 0. I refuse a non-PostgreSQL location whose parent directory does not exist, raising Operational (exit 1) before the library can create it. Is that acceptable, or should the library's directory creation be allowed?"},{"spec":"FEATURE-CliEntry.md","severity":"Low","subject":"Launcher working directory","decision":"The ledger launcher changes to the project root before running, so the default data/app.db and any relative DATABASE_PATH resolve from the root, not the caller's directory. Confirm this matches the intended layout."}] </blueprint-decisions>
BLOCKERS:
core.fileModeisfalsein this repo, soledgermay be committed as100644and failcli-launcher(os.access(X_OK)) on a fresh checkout. The file is executable on disk now. Fix by committing withgit update-index --chmod=+x ledgeror enablingcore.fileMode. I did not change git config or the index.