KAN-474 feat(service): SqliteBackend persists the audit log on disk#282
Draft
fulsomenko wants to merge 5 commits into
Draft
KAN-474 feat(service): SqliteBackend persists the audit log on disk#282fulsomenko wants to merge 5 commits into
fulsomenko wants to merge 5 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Wires the SQLite backend's audit log through to disk and tightens the
CommandStorereturn shape:SessionCommandLogtype inkanban-domaincarries the in-RAM batch list with its own RwLock;InMemoryStorecomposes it instead of inlining aRwLock<Vec<Vec<Command>>>.CommandBatchstruct (#[serde(transparent)]overVec<Command>) is the named row type forCommandStore::load_commands. Wire-format identical to the bare Vec, so existing rows deserialize unchanged.load_all_commandsremoved from the trait (YAGNI; only the replay test used it).SqliteStoreimplementsCommandStoredirectly using the on-diskcommand_logtable. Inserts useINSERT ... VALUES (NULL, ...)so SQLite auto-allocatesbatch_indexatomically — noMAX(batch_index)+1pre-read.SqliteBackenddrops itsmem: InMemoryStoremirror field; everyCommandStorecall routes toself.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_logtable has existed in the schema since v0.6 but was unwritten —SqliteBackendmirrored commands into an in-RAMInMemoryStorethat vanished on close. Close-and-reopen lost the history. KAN-191 reframedCommandStoreas the audit-log foundation; this PR makes the SQLite story match by actually wiring the on-disk table.How:
SqliteStoregains aCommandStoreimpl with three private async helpers (insert_command_log_row,command_log_row_count,select_command_log_range) bridged through the existingrun()sync wrapper. Auto-allocatedbatch_indexviaNULLinsert avoids the pre-read race window.SqliteBackendbecomes a pure delegate. The trait is also narrowed to a true CR-only surface by removingload_all_commands(one test re-expressed ascount()+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).