Skip to content

KAN-474 feat(service): SqliteBackend persists the audit log on disk#282

Draft
fulsomenko wants to merge 5 commits into
developfrom
KAN-474/audit-log-on-disk
Draft

KAN-474 feat(service): SqliteBackend persists the audit log on disk#282
fulsomenko wants to merge 5 commits into
developfrom
KAN-474/audit-log-on-disk

Conversation

@fulsomenko

Copy link
Copy Markdown
Owner

Wires the SQLite backend's audit log through to disk and tightens the CommandStore return shape:

  • New SessionCommandLog type in kanban-domain carries the in-RAM batch list with its own RwLock; InMemoryStore composes it instead of inlining a RwLock<Vec<Vec<Command>>>.
  • New CommandBatch struct (#[serde(transparent)] over Vec<Command>) is the named row type for CommandStore::load_commands. Wire-format identical to the bare Vec, so existing rows deserialize unchanged.
  • load_all_commands removed from the trait (YAGNI; only the replay test used it).
  • SqliteStore implements CommandStore directly using the on-disk command_log table. Inserts use INSERT ... VALUES (NULL, ...) so SQLite auto-allocates batch_index atomically — no MAX(batch_index)+1 pre-read.
  • SqliteBackend drops its mem: InMemoryStore mirror field; every CommandStore call routes to self.db.

What: SQLite-backed kanban files now persist the audit log of executed command batches across sessions. JSON files and the no-persistence in-memory backend keep their existing session-scoped behaviour.

Why: The command_log table has existed in the schema since v0.6 but was unwritten — SqliteBackend mirrored commands into an in-RAM InMemoryStore that vanished on close. Close-and-reopen lost the history. KAN-191 reframed CommandStore as the audit-log foundation; this PR makes the SQLite story match by actually wiring the on-disk table.

How: SqliteStore gains a CommandStore impl with three private async helpers (insert_command_log_row, command_log_row_count, select_command_log_range) bridged through the existing run() sync wrapper. Auto-allocated batch_index via NULL insert avoids the pre-read race window. SqliteBackend becomes a pure delegate. The trait is also narrowed to a true CR-only surface by removing load_all_commands (one test re-expressed as count() + load_commands(0, count)).

Testing: cargo test -p kanban-service --test sqlite_audit_log_persistence (new — close-and-reopen, append-returns-count, batch-stays-grouped, load clamps out-of-range), cargo test --workspace (86 binaries, no regressions), cargo clippy --all-targets --all-features -- -D warnings (clean), cargo fmt --all -- --check (clean).

InMemoryStore's CommandStore implementation was inlined as a
RwLock<Vec<Vec<Command>>> field. Two responsibilities in one struct:
entity-state CRUD and an in-RAM command log. Splitting them gives the
log its own focused type with explicit semantics (append, count,
load, load_all).

SRP: SessionCommandLog has one job. InMemoryStore composes it.

No behaviour change. InMemoryStore::new still produces an empty log;
CommandStore impl forwards to the composed field. JSON backend and
any in-memory test setup continue to work unchanged.

This sets up the next step: SqliteBackend dropping its embedded
InMemoryStore and writing the audit log directly to the on-disk
command_log table, while JSON keeps using InMemoryStore (and
therefore SessionCommandLog) for its per-session audit story.
The audit log returned Vec<Vec<Command>> — a matrix with no name.
The outer level is one user action; the inner level is the commands
within that action. Calling code had to remember that on every read,
and the type system did not help.

Introduce CommandBatch as the named row type:

  pub struct CommandBatch {
      pub commands: Vec<Command>,
  }

bare Vec<Command>, so existing SQLite command_log rows deserialize
unchanged.

Plumb Vec<CommandBatch> through CommandStore (trait + every impl):
SessionCommandLog, InMemoryStore, JsonDataStore, SqliteBackend. Two
test files (audit_log_append_only, command_replay) updated to access
the inner Vec via `.commands` explicitly.

Forward compat: when an audit-log UI lands, CommandBatch is the
natural place to grow per-entry metadata (timestamp, index, actor)
without rewriting every consumer's return-type unpacking.
YAGNI: the only caller (a single replay test) does not need the
atomic count+load that load_all_commands was added to provide. The
test switches to command_count() + load_commands(0, count), which is
non-atomic but safe under a single-threaded test runtime.

Drops:
- CommandStore::load_all_commands trait method
- InMemoryStore and JsonDataStore impls
- SessionCommandLog::load_all helper and its unit test

If a real multi-writer audit-log consumer ever appears, the atomic
form can be reintroduced behind its own purpose-specific API. Until
then the trait stays minimal.
SqliteBackend dropped the per-session InMemoryStore mirror it was
using to satisfy CommandStore, and now writes append-only batches
straight to the on-disk command_log table.

Changes:

- SqliteStore implements CommandStore directly (sync, via the
  existing block_in_place run() helper, symmetric with how DataStore
  is implemented).
- Async helpers on SqliteStore for the three trait methods:
  insert_command_log_row (uses INSERT ... VALUES (NULL, ...) so
  batch_index is auto-allocated atomically by SQLite, no read side),
  command_log_row_count, select_command_log_range.
- The old append_command_batch / load_all_command_batches /
  truncate_command_log_after / shift_command_log functions are gone;
  the new helpers replace them, accessed through CommandStore.
- SqliteBackend drops the mem: InMemoryStore field and routes every
  CommandStore call to self.db.

LSP: SQLite no longer mints a divergent audit-log behaviour. Open the
file, close it, reopen it, and load_commands returns what you wrote.
JSON keeps its in-RAM session log via InMemoryStore + SessionCommandLog.
The backend choice does not change what CommandStore promises.

DIP: KanbanContext and the wider service code never reach into a
backend-specific log API. They depend only on CommandStore. Persistence
of the audit log is a property of the backend implementation, not of
the orchestration layer.

New regression test (`sqlite_audit_log_persistence.rs`) covers:
- Close + reopen retains entries
- append returns the new count
- Multi-command batch stays grouped
- load clamps out-of-range
@fulsomenko fulsomenko self-assigned this May 18, 2026
@fulsomenko fulsomenko marked this pull request as draft May 24, 2026 00:53
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