Back to feed

sledtools/pika branch #78

pika-git-chat-store-1

Extract chat persistence into chat_store

Target branch: master

Merge Commit: 755ddf775190c10ecc6302a39662fb562edfd8fa

branch: merged tutorial: ready ci: success
Open CI Details

Continuous Integration

CI: success

Compact status on the review page, with full logs on the CI page.

Open CI Details

Latest run #100 success

2 passed

head f14a38c7051f8858e4357e8ab342fff34ad986d1 · queued 2026-03-26 00:19:24 · 2 lane(s)

queued 7s · ran 23s

check-notifications · success check-agent-contracts · success

Summary

This branch extracts all chat-persistence logic from the monolithic storage.rs file into a dedicated chat_store.rs module within the pika-news crate. Ten methods covering PR chat sessions, branch-review chat sessions, Claude session ID management, and message appending are moved verbatim into the new file, which continues to implement on the existing Store type. The main.rs module declaration is updated to register the new module. No signatures, SQL queries, or business logic changed — this is a pure structural refactor to improve code organization.

Tutorial Steps

Create the `chat_store` module with extracted chat-persistence methods

Intent: Move all chat-related database methods out of `storage.rs` into a focused `chat_store.rs` file so that chat persistence concerns are isolated in their own module. The new file adds an `impl Store` block containing the ten relocated methods.

Affected files: crates/pika-news/src/chat_store.rs

Evidence
@@ -0,0 +1,258 @@
+use anyhow::Context;
+use rusqlite::{params, OptionalExtension};
+
+use crate::storage::{ChatMessage, Store};
+
+impl Store {

A new file crates/pika-news/src/chat_store.rs is introduced containing a single impl Store block. It imports ChatMessage and Store from crate::storage and re-implements the following ten public methods exactly as they appeared in storage.rs:

MethodPurpose
get_artifact_session_idFetch the Claude session ID for the current ready artifact of a PR
get_or_create_chat_sessionFind or create a per-user chat session for a PR artifact, returning history
get_branch_review_artifact_session_idFetch the Claude session ID for a branch-review artifact
get_or_create_branch_review_chat_sessionFind or create a per-user chat session for a branch-review artifact
get_chat_claude_session_idLook up the Claude session ID from a PR chat session row
get_branch_review_chat_claude_session_idLook up the Claude session ID from a branch chat session row
update_chat_claude_session_idUpdate the Claude session ID on a PR chat session
update_branch_review_chat_claude_session_idUpdate the Claude session ID on a branch chat session
append_chat_messageInsert a new message into a PR chat session
append_branch_review_chat_messageInsert a new message into a branch-review chat session

All SQL queries, parameter bindings, error contexts, and return types are identical to the originals — no behavioral change.

Remove the chat methods from `storage.rs`

Intent: Delete the 252-line block of chat-persistence methods from `storage.rs` to eliminate duplication now that the logic lives in `chat_store.rs`.

Affected files: crates/pika-news/src/storage.rs

Evidence
@@ -637,258 +637,6 @@ impl Store {
     }
 
-    pub fn get_artifact_session_id(&self, pr_id: i64) -> anyhow::Result<Option<String>> {
...
-    pub fn append_branch_review_chat_message(
-        &self,
-        session_id: i64,
-        role: &str,
-        content: &str,
-    ) -> anyhow::Result<()> {
...
     // --- Inbox methods ---

Lines 639–894 of storage.rs (all ten chat methods) are deleted in a single contiguous removal. The file now jumps directly from the method preceding the chat block to the // --- Inbox methods --- section comment. Because Rust allows multiple impl blocks for the same type across different modules, callers that reference Store methods are unaffected — the compiler resolves them from the new module instead.

Register the `chat_store` module in `main.rs`

Intent: Ensure the compiler includes the new module by adding a `mod chat_store;` declaration to the crate root.

Affected files: crates/pika-news/src/main.rs

Evidence
@@ -1,5 +1,6 @@
 mod auth;
 mod branch_store;
+mod chat_store;
 mod ci;

mod chat_store; is inserted in alphabetical order among the existing module declarations in main.rs. This is the only change needed to wire the new file into the compilation unit; no pub use re-exports are required because the methods are defined directly on Store.

Diff