Skip to content

refactor: template laid out by app in three crates [ORB-27923] - #23

Open
danieljhkim wants to merge 1 commit into
agent-mainfrom
orb-27923-apps-store-crate
Open

danieljhkim wants to merge 1 commit into
agent-mainfrom
orb-27923-apps-store-crate

Conversation

@danieljhkim

Copy link
Copy Markdown
Member

Restructures the template into three crates laid out by app: tmpl-cli -> tmpl-cli-core -> tmpl-cli-store, following the layout approved for scribe on 2026-11-24. Daniel's words: "can you also update rust-cli-template similarly? separate create for store. runtime, composition, apps in core."

Behaviour is unchanged: the CLI, help, output goldens, exit codes, error messages and codes, the order on stderr, and the on-disk format are all the same. No golden changed.

What moved where

tmpl-cli (binary)

  • app.rs -> tmpl-cli-core/src/composition.rs (AppEnv is now Env, and holds --root as well).
  • audit_middleware.rs -> tmpl-cli-core/src/runtime/audit.rs.
  • commands/note.rs now only maps arguments to apps::note::Command, calls apps::note::exec, and turns the Outcome into a payload (list_notice stays here).
  • main.rs captures Env::from_process() once and passes it to run.
  • CliError::Core and CliError::NoDataDir become CliError::Note(apps::note::Error), still through a single #[from].
  • The binary names only tmpl_cli_core: record shapes come from tmpl_cli_core::contracts.

tmpl-cli-core

  • apps/note/:
    • mod.rs: Command, Outcome, Error (NotFound, IdExhausted, Runtime), exec, run, and the audited-command table Command::audit.
    • add.rs, list.rs, show.rs: one flow per command.
    • rules.rs: the old query.rs plus next_id (was NoteId::next_after) and NewNote::into_note.
  • runtime/:
    • Runtime, Ctx;
    • the audit guard;
    • errors.rs, the shared Error (NoDataDir, Store).
  • composition.rs: Env, the data directory, the Store, the actor and the clock. It is the only place in the core that reads the environment.
  • test_support.rs (feature test-support): exposes Ctx and note_run.

tmpl-cli-store (new)

  • store.rs: Store::open, read() -> Snapshot, write(label, |tx| …), audit() (was store/mod.rs, minus add/list/show).
  • db/: fsio.rs, format.rs (the notes document and UPGRADES), audit.rs (the persisted audit line and its upgrades), and the file-name constants.
  • repository/: Snapshot/Tx, notes (all, get, bad_records, bad_record, put), audit (append, was Store::record_audit).
  • contracts/: note.rs (was core note.rs, without next_after and NewNote) and audit.rs (AuditEvent, AuditStatus).
  • error.rs: the store Error (minus NotFound and IdExhausted, which moved to the note app) and BadRecord.

Guards and docs

  • scripts/check-dependency-direction.sh:
    • a policy for the new crate, and the binary is banned from naming tmpl-cli-store;
    • store: only db/ does I/O, db/fsio knows no records, and contracts/ has no db/repository imports, no check_* functions and no clock;
    • core: no file or process access, std::env only in composition.rs, runtime/ imports no app, no apps/common|shared|util, and rules are pure;
    • binary: commands/ reads no environment and opens no store;
    • the retired paths are listed.
    • I checked each new ban by planting a violation in a scratch copy, and each one fired.
  • ARCHITECTURE.md is rewritten.
  • 4_decisions.md gains "The template is laid out by app in three crates". It marks "A domain library crate and a CLI crate" and "Persistence is one module inside the core crate" superseded, and updates the code anchors and adds update notes in the live decisions it touches.
  • 1_overview.md, 2_design.md, specs/store.md, AGENTS.md code rules (the standards block is untouched), README/about ("three crates"), the Makefile doctest comment and the rename-template.sh header are updated.
  • CHANGELOG.md is untouched (compiled at release).

Departures from the target

  1. exec takes a finish callback: exec(env, command, finish) instead of exec(env, command) -> Result<Outcome, Error>. The audit line has to be written after the payload or JSON error object is flushed. json_error_object_precedes_audit_failure_warning_on_stderr pins that order, and a plain return would drop the guard before rendering. This is recorded in the new decision.
  2. The core's own integration test uses test-support. The core dev-depends on itself with features = ["test-support"] so tests/note.rs can pin the clock and the lock wait. The binary's integration tests did not need run/Ctx.
  3. Some test call sites had to change, because the APIs they called moved or no longer exist (Store::add/list/show, Context::with_store, commands::run_with, AuditGuard::mark_result(&Result<Output, CliError>)). Test names and assertions are kept:
    • crates/tmpl-cli-core/tests/note.rs (was tests/store.rs): every test body is byte-identical. The header adds a small NoteApp adapter trait on Store that runs the app's commands through note_run. Four matches! patterns now unwrap the store error through store_error(&err), since store errors reach the app wrapped.
    • the_next_id_is_one_past_the_highest_whatever_the_order calls next_id(..) instead of NoteId::next_after(..).
    • only_note_add_is_audited and the lock-timeout chokepoint test moved from the binary to core/src/apps/note/tests/note.rs. They build Command values instead of parsing argv, and call run_with(&ctx, …) with a Ctx.
    • The audit guard tests (runtime/tests/audit.rs) produce the outcome through note::run and pass Ok(target) / Err(code) to mark_result.
    • The store unit tests (tmpl-cli-store/src/tests/store.rs) write through store.write and store.audit().append.
  4. The bad-record write refusal is enforced twice. The add flow refuses on its own read before any side effect (the STD-02 R34 ordering, and the same error precedence as before). Store::write refuses again under the lock, since a rewrite would drop the bad records. I treat the second check as a store integrity guard, not a note rule.
  5. Outcome::Listed echoes the ListFilter, so the binary can build the empty-result notice without keeping its own copy.
  6. Doctest location: the Title doctest moved with Title to the store crate, so the core crate has no doctest now. The Makefile comment says so. No test was added.
  7. .config/nextest.toml: lock_timeout_audited_command_waits_one_deadline… now runs alone (threads-required = "num-cpus"). It asserts wall time (under two 80 ms deadlines, including an fsync'd audit append). In its new test binary, nextest schedules it alongside the process-spawning scripts/rename_template tests: it failed 8 of 10 full runs at 160–168 ms, while agent-main passed 10 of 10 with the test scheduled earlier. Its bound is unchanged. With the override it passed 10 of 10.
  8. Output keeps its own presentation variables: output/ still reads TMPL_CLI_FORMAT, TMPL_CLI_LOG, NO_COLOR and the rest. Only main captures the core's Env.

No tools crate and no providers, as the brief said.

Validation

  • make ci in the worktree, without CI=true: passed.
    • fmt-check, structure (dependency-direction, test-modules, terminal-guard, private-task-ids), standards-check, docs-check, clippy -D warnings: passed.
    • nextest: 138 of 138 passed, the same count as agent-main.
    • Doctests and goldens (3 of 3): passed.
  • cargo deny: not run, because cargo-deny is not installed locally, so make ci skipped it with a warning. CI runs it. No external dependency changed; the new crate is MIT via license.workspace.
  • STANDARDS_STRICT=1 make standards-check: passed.
  • cargo clippy --workspace -- -D warnings (non-test build, without test-support): passed.
  • Full nextest suite 10 times after the nextest override: 10 of 10 passed.
  • scripts/rename-template.sh demo-cli on a full copy of the tree: it produced demo-cli, demo-cli-core and demo-cli-store. In the renamed copy, cargo clippy --workspace --all-targets --offline -- -D warnings, check-dependency-direction.sh and cargo fmt --check all passed. The rename_template integration test also passed.

🤖 Generated with Claude Code

Split the template into tmpl-cli -> tmpl-cli-core -> tmpl-cli-store,
following the layout approved for scribe.

- tmpl-cli: argument declarations, mapping to the app's Command,
  rendering. main captures the environment into the core's Env once.
- tmpl-cli-core: apps/note (Command, Outcome, Error, exec, run, one flow
  per command, pure rules: next id, list selection), runtime (Ctx, the
  audit guard, shared errors), composition.rs (Env, data directory,
  store, clock), and a test-support feature exposing Ctx and run.
- tmpl-cli-store: db/ (fsio, persisted format and upgrades, audit line,
  locks), repository/ (notes, audit), contracts/ (note and audit record
  shapes). No rule of the note app.

No change to the CLI, help, goldens, exit codes, messages or on-disk
format. Tests moved with their code; call sites changed where the API
they called moved. The guard script, ARCHITECTURE.md and the design docs
describe the new layout, with a decision superseding the two-crate and
store-module decisions. The one wall-clock test runs alone under nextest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant