Initial commit

This commit is contained in:
2025-12-14 21:59:59 +00:00
commit d7c20cea54
126 changed files with 21728 additions and 0 deletions
+347
View File
@@ -0,0 +1,347 @@
// Package sqlite provides SQLite database operations for claude-mnemonic.
package sqlite
import (
"database/sql"
"fmt"
"time"
)
// Migration represents a database schema migration.
type Migration struct {
Version int
Name string
SQL string
}
// Migrations is the list of all database migrations in order.
var Migrations = []Migration{
{
Version: 4,
Name: "sdk_agent_architecture",
SQL: `
-- SDK Sessions (main session tracking)
CREATE TABLE IF NOT EXISTS sdk_sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
claude_session_id TEXT UNIQUE NOT NULL,
sdk_session_id TEXT UNIQUE,
project TEXT NOT NULL,
user_prompt TEXT,
started_at TEXT NOT NULL,
started_at_epoch INTEGER NOT NULL,
completed_at TEXT,
completed_at_epoch INTEGER,
status TEXT CHECK(status IN ('active', 'completed', 'failed')) NOT NULL DEFAULT 'active'
);
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_claude_id ON sdk_sessions(claude_session_id);
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_sdk_id ON sdk_sessions(sdk_session_id);
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_project ON sdk_sessions(project);
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_status ON sdk_sessions(status);
CREATE INDEX IF NOT EXISTS idx_sdk_sessions_started ON sdk_sessions(started_at_epoch DESC);
-- Observations (extracted learnings)
CREATE TABLE IF NOT EXISTS observations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sdk_session_id TEXT NOT NULL,
project TEXT NOT NULL,
text TEXT,
type TEXT NOT NULL CHECK(type IN ('decision', 'bugfix', 'feature', 'refactor', 'discovery', 'change')),
created_at TEXT NOT NULL,
created_at_epoch INTEGER NOT NULL,
FOREIGN KEY(sdk_session_id) REFERENCES sdk_sessions(sdk_session_id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_observations_sdk_session ON observations(sdk_session_id);
CREATE INDEX IF NOT EXISTS idx_observations_project ON observations(project);
CREATE INDEX IF NOT EXISTS idx_observations_type ON observations(type);
CREATE INDEX IF NOT EXISTS idx_observations_created ON observations(created_at_epoch DESC);
-- Session Summaries
CREATE TABLE IF NOT EXISTS session_summaries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sdk_session_id TEXT NOT NULL,
project TEXT NOT NULL,
request TEXT,
investigated TEXT,
learned TEXT,
completed TEXT,
next_steps TEXT,
files_read TEXT,
files_edited TEXT,
notes TEXT,
created_at TEXT NOT NULL,
created_at_epoch INTEGER NOT NULL,
FOREIGN KEY(sdk_session_id) REFERENCES sdk_sessions(sdk_session_id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_session_summaries_sdk_session ON session_summaries(sdk_session_id);
CREATE INDEX IF NOT EXISTS idx_session_summaries_project ON session_summaries(project);
CREATE INDEX IF NOT EXISTS idx_session_summaries_created ON session_summaries(created_at_epoch DESC);
`,
},
{
Version: 5,
Name: "worker_port_column",
SQL: `ALTER TABLE sdk_sessions ADD COLUMN worker_port INTEGER;`,
},
{
Version: 6,
Name: "prompt_tracking_columns",
SQL: `
ALTER TABLE sdk_sessions ADD COLUMN prompt_counter INTEGER DEFAULT 0;
ALTER TABLE observations ADD COLUMN prompt_number INTEGER;
ALTER TABLE session_summaries ADD COLUMN prompt_number INTEGER;
`,
},
{
Version: 8,
Name: "observation_hierarchical_fields",
SQL: `
ALTER TABLE observations ADD COLUMN title TEXT;
ALTER TABLE observations ADD COLUMN subtitle TEXT;
ALTER TABLE observations ADD COLUMN facts TEXT;
ALTER TABLE observations ADD COLUMN narrative TEXT;
ALTER TABLE observations ADD COLUMN concepts TEXT;
ALTER TABLE observations ADD COLUMN files_read TEXT;
ALTER TABLE observations ADD COLUMN files_modified TEXT;
`,
},
{
Version: 10,
Name: "user_prompts_table",
SQL: `
-- User prompts table
CREATE TABLE IF NOT EXISTS user_prompts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
claude_session_id TEXT NOT NULL,
prompt_number INTEGER NOT NULL,
prompt_text TEXT NOT NULL,
created_at TEXT NOT NULL,
created_at_epoch INTEGER NOT NULL,
FOREIGN KEY(claude_session_id) REFERENCES sdk_sessions(claude_session_id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_user_prompts_claude_session ON user_prompts(claude_session_id);
CREATE INDEX IF NOT EXISTS idx_user_prompts_created ON user_prompts(created_at_epoch DESC);
CREATE INDEX IF NOT EXISTS idx_user_prompts_prompt_number ON user_prompts(prompt_number);
CREATE INDEX IF NOT EXISTS idx_user_prompts_lookup ON user_prompts(claude_session_id, prompt_number);
-- FTS5 virtual table for user prompts
CREATE VIRTUAL TABLE IF NOT EXISTS user_prompts_fts USING fts5(
prompt_text,
content='user_prompts',
content_rowid='id'
);
-- Triggers for FTS5 sync
CREATE TRIGGER IF NOT EXISTS user_prompts_ai AFTER INSERT ON user_prompts BEGIN
INSERT INTO user_prompts_fts(rowid, prompt_text)
VALUES (new.id, new.prompt_text);
END;
CREATE TRIGGER IF NOT EXISTS user_prompts_ad AFTER DELETE ON user_prompts BEGIN
INSERT INTO user_prompts_fts(user_prompts_fts, rowid, prompt_text)
VALUES('delete', old.id, old.prompt_text);
END;
CREATE TRIGGER IF NOT EXISTS user_prompts_au AFTER UPDATE ON user_prompts BEGIN
INSERT INTO user_prompts_fts(user_prompts_fts, rowid, prompt_text)
VALUES('delete', old.id, old.prompt_text);
INSERT INTO user_prompts_fts(rowid, prompt_text)
VALUES (new.id, new.prompt_text);
END;
`,
},
{
Version: 11,
Name: "discovery_tokens_column",
SQL: `
ALTER TABLE observations ADD COLUMN discovery_tokens INTEGER DEFAULT 0;
ALTER TABLE session_summaries ADD COLUMN discovery_tokens INTEGER DEFAULT 0;
`,
},
{
Version: 12,
Name: "observations_fts",
SQL: `
-- FTS5 virtual table for observations
CREATE VIRTUAL TABLE IF NOT EXISTS observations_fts USING fts5(
title, subtitle, narrative,
content='observations',
content_rowid='id'
);
-- Triggers for FTS5 sync
CREATE TRIGGER IF NOT EXISTS observations_ai AFTER INSERT ON observations BEGIN
INSERT INTO observations_fts(rowid, title, subtitle, narrative)
VALUES (new.id, new.title, new.subtitle, new.narrative);
END;
CREATE TRIGGER IF NOT EXISTS observations_ad AFTER DELETE ON observations BEGIN
INSERT INTO observations_fts(observations_fts, rowid, title, subtitle, narrative)
VALUES('delete', old.id, old.title, old.subtitle, old.narrative);
END;
CREATE TRIGGER IF NOT EXISTS observations_au AFTER UPDATE ON observations BEGIN
INSERT INTO observations_fts(observations_fts, rowid, title, subtitle, narrative)
VALUES('delete', old.id, old.title, old.subtitle, old.narrative);
INSERT INTO observations_fts(rowid, title, subtitle, narrative)
VALUES (new.id, new.title, new.subtitle, new.narrative);
END;
`,
},
{
Version: 13,
Name: "session_summaries_fts",
SQL: `
-- FTS5 virtual table for session summaries
CREATE VIRTUAL TABLE IF NOT EXISTS session_summaries_fts USING fts5(
request, investigated, learned, completed, next_steps, notes,
content='session_summaries',
content_rowid='id'
);
-- Triggers for FTS5 sync
CREATE TRIGGER IF NOT EXISTS session_summaries_ai AFTER INSERT ON session_summaries BEGIN
INSERT INTO session_summaries_fts(rowid, request, investigated, learned, completed, next_steps, notes)
VALUES (new.id, new.request, new.investigated, new.learned, new.completed, new.next_steps, new.notes);
END;
CREATE TRIGGER IF NOT EXISTS session_summaries_ad AFTER DELETE ON session_summaries BEGIN
INSERT INTO session_summaries_fts(session_summaries_fts, rowid, request, investigated, learned, completed, next_steps, notes)
VALUES('delete', old.id, old.request, old.investigated, old.learned, old.completed, old.next_steps, old.notes);
END;
CREATE TRIGGER IF NOT EXISTS session_summaries_au AFTER UPDATE ON session_summaries BEGIN
INSERT INTO session_summaries_fts(session_summaries_fts, rowid, request, investigated, learned, completed, next_steps, notes)
VALUES('delete', old.id, old.request, old.investigated, old.learned, old.completed, old.next_steps, old.notes);
INSERT INTO session_summaries_fts(rowid, request, investigated, learned, completed, next_steps, notes)
VALUES (new.id, new.request, new.investigated, new.learned, new.completed, new.next_steps, new.notes);
END;
`,
},
{
Version: 14,
Name: "observation_scope_column",
SQL: `
-- Add scope column for project isolation
-- 'project' = only visible within same project (default)
-- 'global' = visible across all projects (best practices, patterns)
ALTER TABLE observations ADD COLUMN scope TEXT DEFAULT 'project' CHECK(scope IN ('project', 'global'));
-- Index for efficient scope-based queries
CREATE INDEX IF NOT EXISTS idx_observations_scope ON observations(scope);
CREATE INDEX IF NOT EXISTS idx_observations_project_scope ON observations(project, scope);
`,
},
{
Version: 15,
Name: "observation_file_mtimes",
SQL: `
-- Store file modification times at observation creation
-- JSON object: {"path": mtime_epoch_ms, ...}
-- Used to detect staleness when files change
ALTER TABLE observations ADD COLUMN file_mtimes TEXT;
`,
},
{
Version: 16,
Name: "prompt_matched_observations",
SQL: `
-- Track how many observations were found relevant for each prompt
-- Displayed in dashboard timeline
ALTER TABLE user_prompts ADD COLUMN matched_observations INTEGER DEFAULT 0;
`,
},
}
// MigrationManager handles database schema migrations.
type MigrationManager struct {
db *sql.DB
}
// NewMigrationManager creates a new migration manager.
func NewMigrationManager(db *sql.DB) *MigrationManager {
return &MigrationManager{db: db}
}
// EnsureSchemaVersionsTable creates the schema_versions table if it doesn't exist.
func (m *MigrationManager) EnsureSchemaVersionsTable() error {
_, err := m.db.Exec(`
CREATE TABLE IF NOT EXISTS schema_versions (
id INTEGER PRIMARY KEY,
version INTEGER UNIQUE NOT NULL,
applied_at TEXT NOT NULL
)
`)
return err
}
// GetAppliedVersions returns all applied migration versions.
func (m *MigrationManager) GetAppliedVersions() (map[int]bool, error) {
rows, err := m.db.Query("SELECT version FROM schema_versions ORDER BY version")
if err != nil {
return nil, err
}
defer rows.Close()
versions := make(map[int]bool)
for rows.Next() {
var version int
if err := rows.Scan(&version); err != nil {
return nil, err
}
versions[version] = true
}
return versions, rows.Err()
}
// ApplyMigration applies a single migration.
func (m *MigrationManager) ApplyMigration(migration Migration) error {
tx, err := m.db.Begin()
if err != nil {
return fmt.Errorf("begin transaction: %w", err)
}
defer tx.Rollback()
// Execute migration SQL
if _, err := tx.Exec(migration.SQL); err != nil {
return fmt.Errorf("execute migration %d (%s): %w", migration.Version, migration.Name, err)
}
// Record migration
_, err = tx.Exec(
"INSERT INTO schema_versions (version, applied_at) VALUES (?, ?)",
migration.Version, time.Now().Format(time.RFC3339),
)
if err != nil {
return fmt.Errorf("record migration %d: %w", migration.Version, err)
}
return tx.Commit()
}
// RunMigrations applies all pending migrations.
func (m *MigrationManager) RunMigrations() error {
if err := m.EnsureSchemaVersionsTable(); err != nil {
return fmt.Errorf("ensure schema_versions table: %w", err)
}
applied, err := m.GetAppliedVersions()
if err != nil {
return fmt.Errorf("get applied versions: %w", err)
}
for _, migration := range Migrations {
if applied[migration.Version] {
continue
}
if err := m.ApplyMigration(migration); err != nil {
return err
}
}
return nil
}
+513
View File
@@ -0,0 +1,513 @@
// Package sqlite provides SQLite database operations for claude-mnemonic.
package sqlite
import (
"context"
"database/sql"
"encoding/json"
"strings"
"time"
"github.com/lukaszraczylo/claude-mnemonic/pkg/models"
)
// CleanupFunc is a callback for when observations are cleaned up.
// Receives the IDs of deleted observations for downstream cleanup (e.g., vector DB).
type CleanupFunc func(ctx context.Context, deletedIDs []int64)
// ObservationStore provides observation-related database operations.
type ObservationStore struct {
store *Store
cleanupFunc CleanupFunc
}
// NewObservationStore creates a new observation store.
func NewObservationStore(store *Store) *ObservationStore {
return &ObservationStore{store: store}
}
// SetCleanupFunc sets the callback for when observations are deleted during cleanup.
func (s *ObservationStore) SetCleanupFunc(fn CleanupFunc) {
s.cleanupFunc = fn
}
// StoreObservation stores a new observation.
func (s *ObservationStore) StoreObservation(ctx context.Context, sdkSessionID, project string, obs *models.ParsedObservation, promptNumber int, discoveryTokens int64) (int64, int64, error) {
now := time.Now()
nowEpoch := now.UnixMilli()
// Ensure session exists (auto-create if missing)
if err := s.ensureSessionExists(ctx, sdkSessionID, project); err != nil {
return 0, 0, err
}
// Determine scope: use parsed scope if set, otherwise auto-determine from concepts
scope := obs.Scope
if scope == "" {
scope = models.DetermineScope(obs.Concepts)
}
factsJSON, _ := json.Marshal(obs.Facts)
conceptsJSON, _ := json.Marshal(obs.Concepts)
filesReadJSON, _ := json.Marshal(obs.FilesRead)
filesModifiedJSON, _ := json.Marshal(obs.FilesModified)
fileMtimesJSON, _ := json.Marshal(obs.FileMtimes)
const query = `
INSERT INTO observations
(sdk_session_id, project, scope, type, title, subtitle, facts, narrative, concepts,
files_read, files_modified, file_mtimes, prompt_number, discovery_tokens, created_at, created_at_epoch)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`
result, err := s.store.ExecContext(ctx, query,
sdkSessionID, project, string(scope), string(obs.Type),
nullString(obs.Title), nullString(obs.Subtitle),
string(factsJSON), nullString(obs.Narrative), string(conceptsJSON),
string(filesReadJSON), string(filesModifiedJSON), string(fileMtimesJSON),
nullInt(promptNumber), discoveryTokens,
now.Format(time.RFC3339), nowEpoch,
)
if err != nil {
return 0, 0, err
}
id, _ := result.LastInsertId()
// Cleanup old observations beyond the limit for this project
if project != "" {
deletedIDs, _ := s.CleanupOldObservations(ctx, project)
if len(deletedIDs) > 0 && s.cleanupFunc != nil {
s.cleanupFunc(ctx, deletedIDs)
}
}
return id, nowEpoch, nil
}
// ensureSessionExists creates a session if it doesn't exist.
func (s *ObservationStore) ensureSessionExists(ctx context.Context, sdkSessionID, project string) error {
const checkQuery = `SELECT id FROM sdk_sessions WHERE sdk_session_id = ?`
var id int64
err := s.store.QueryRowContext(ctx, checkQuery, sdkSessionID).Scan(&id)
if err == nil {
return nil // Session exists
}
if err != sql.ErrNoRows {
return err
}
// Auto-create session
now := time.Now()
const insertQuery = `
INSERT INTO sdk_sessions
(claude_session_id, sdk_session_id, project, started_at, started_at_epoch, status)
VALUES (?, ?, ?, ?, ?, 'active')
`
_, err = s.store.ExecContext(ctx, insertQuery,
sdkSessionID, sdkSessionID, project,
now.Format(time.RFC3339), now.UnixMilli(),
)
return err
}
// GetObservationByID retrieves an observation by ID.
func (s *ObservationStore) GetObservationByID(ctx context.Context, id int64) (*models.Observation, error) {
const query = `
SELECT id, sdk_session_id, project, COALESCE(scope, 'project') as scope, type, title, subtitle, facts, narrative,
concepts, files_read, files_modified, file_mtimes, prompt_number, discovery_tokens,
created_at, created_at_epoch
FROM observations
WHERE id = ?
`
obs, err := scanObservation(s.store.QueryRowContext(ctx, query, id))
if err == sql.ErrNoRows {
return nil, nil
}
return obs, err
}
// GetObservationsByIDs retrieves observations by a list of IDs.
func (s *ObservationStore) GetObservationsByIDs(ctx context.Context, ids []int64, orderBy string, limit int) ([]*models.Observation, error) {
if len(ids) == 0 {
return nil, nil
}
// Build query with placeholders
// #nosec G202 -- query uses parameterized placeholders, not user input
query := `
SELECT id, sdk_session_id, project, COALESCE(scope, 'project') as scope, type, title, subtitle, facts, narrative,
concepts, files_read, files_modified, file_mtimes, prompt_number, discovery_tokens,
created_at, created_at_epoch
FROM observations
WHERE id IN (?` + repeatPlaceholders(len(ids)-1) + `)
ORDER BY created_at_epoch `
if orderBy == "date_asc" {
query += "ASC"
} else {
query += "DESC"
}
if limit > 0 {
query += " LIMIT ?"
}
// Convert []int64 to []interface{}
args := make([]interface{}, len(ids))
for i, id := range ids {
args[i] = id
}
if limit > 0 {
args = append(args, limit)
}
rows, err := s.store.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return scanObservationRows(rows)
}
// GetRecentObservations retrieves recent observations for a project.
// This includes project-scoped observations for the specified project AND global observations.
func (s *ObservationStore) GetRecentObservations(ctx context.Context, project string, limit int) ([]*models.Observation, error) {
const query = `
SELECT id, sdk_session_id, project, COALESCE(scope, 'project') as scope, type, title, subtitle, facts, narrative,
concepts, files_read, files_modified, file_mtimes, prompt_number, discovery_tokens,
created_at, created_at_epoch
FROM observations
WHERE (project = ? AND (scope IS NULL OR scope = 'project'))
OR scope = 'global'
ORDER BY created_at_epoch DESC
LIMIT ?
`
rows, err := s.store.QueryContext(ctx, query, project, limit)
if err != nil {
return nil, err
}
defer rows.Close()
return scanObservationRows(rows)
}
// GetObservationCount returns the count of observations for a project (including global).
func (s *ObservationStore) GetObservationCount(ctx context.Context, project string) (int, error) {
const query = `
SELECT COUNT(*) FROM observations
WHERE project = ? OR scope = 'global'
`
var count int
err := s.store.QueryRowContext(ctx, query, project).Scan(&count)
return count, err
}
// GetAllRecentObservations retrieves recent observations across all projects.
func (s *ObservationStore) GetAllRecentObservations(ctx context.Context, limit int) ([]*models.Observation, error) {
const query = `
SELECT id, sdk_session_id, project, COALESCE(scope, 'project') as scope, type, title, subtitle, facts, narrative,
concepts, files_read, files_modified, file_mtimes, prompt_number, discovery_tokens,
created_at, created_at_epoch
FROM observations
ORDER BY created_at_epoch DESC
LIMIT ?
`
rows, err := s.store.QueryContext(ctx, query, limit)
if err != nil {
return nil, err
}
defer rows.Close()
return scanObservationRows(rows)
}
// SearchObservationsFTS performs full-text search on observations.
func (s *ObservationStore) SearchObservationsFTS(ctx context.Context, query, project string, limit int) ([]*models.Observation, error) {
if limit <= 0 {
limit = 10
}
// Extract keywords from the query (words > 3 chars, not common)
keywords := extractKeywords(query)
if len(keywords) == 0 {
return nil, nil
}
// Build FTS5 query: keyword1 OR keyword2 OR keyword3
ftsTerms := strings.Join(keywords, " OR ")
// Use FTS5 to search title, subtitle, and narrative
const ftsQuery = `
SELECT o.id, o.sdk_session_id, o.project, COALESCE(o.scope, 'project') as scope, o.type,
o.title, o.subtitle, o.facts, o.narrative, o.concepts, o.files_read, o.files_modified,
o.file_mtimes, o.prompt_number, o.discovery_tokens, o.created_at, o.created_at_epoch
FROM observations o
JOIN observations_fts fts ON o.id = fts.rowid
WHERE observations_fts MATCH ?
AND (o.project = ? OR o.scope = 'global')
ORDER BY rank
LIMIT ?
`
rows, err := s.store.QueryContext(ctx, ftsQuery, ftsTerms, project, limit)
if err != nil {
// FTS failed, try LIKE fallback
return s.searchObservationsLike(ctx, keywords, project, limit)
}
defer rows.Close()
observations, err := scanObservationRows(rows)
if err != nil {
return nil, err
}
// If FTS returned nothing, try LIKE search
if len(observations) == 0 {
return s.searchObservationsLike(ctx, keywords, project, limit)
}
return observations, nil
}
// searchObservationsLike performs fallback LIKE search on observations.
func (s *ObservationStore) searchObservationsLike(ctx context.Context, keywords []string, project string, limit int) ([]*models.Observation, error) {
if len(keywords) == 0 {
return nil, nil
}
// Build LIKE conditions for each keyword
var conditions []string
var args []interface{}
for _, kw := range keywords {
pattern := "%" + kw + "%"
conditions = append(conditions, "(title LIKE ? OR subtitle LIKE ? OR narrative LIKE ?)")
args = append(args, pattern, pattern, pattern)
}
// #nosec G202 -- query uses parameterized placeholders, not user input
query := `
SELECT id, sdk_session_id, project, COALESCE(scope, 'project') as scope, type,
title, subtitle, facts, narrative, concepts, files_read, files_modified,
file_mtimes, prompt_number, discovery_tokens, created_at, created_at_epoch
FROM observations
WHERE (` + strings.Join(conditions, " OR ") + `)
AND (project = ? OR scope = 'global')
ORDER BY created_at_epoch DESC
LIMIT ?
`
args = append(args, project, limit)
rows, err := s.store.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return scanObservationRows(rows)
}
// extractKeywords extracts significant words from a query.
func extractKeywords(query string) []string {
// Common words to skip
stopWords := map[string]bool{
"what": true, "is": true, "the": true, "a": true, "an": true,
"how": true, "does": true, "do": true, "can": true, "could": true,
"would": true, "should": true, "where": true, "when": true, "why": true,
"which": true, "who": true, "this": true, "that": true, "these": true,
"those": true, "it": true, "its": true, "for": true, "from": true,
"with": true, "about": true, "into": true, "through": true, "during": true,
"before": true, "after": true, "above": true, "below": true, "to": true,
"of": true, "in": true, "on": true, "at": true, "by": true, "and": true,
"or": true, "but": true, "if": true, "then": true, "else": true,
"function": true, "method": true, "class": true, "file": true,
"code": true, "work": true, "works": true, "working": true,
"please": true, "help": true, "me": true, "my": true, "i": true,
"tell": true, "show": true, "explain": true, "describe": true,
}
// Split and filter
words := strings.FieldsFunc(strings.ToLower(query), func(r rune) bool {
return !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_')
})
var keywords []string
seen := make(map[string]bool)
for _, word := range words {
// Skip short words, stop words, and duplicates
if len(word) < 4 || stopWords[word] || seen[word] {
continue
}
seen[word] = true
keywords = append(keywords, word)
}
return keywords
}
// ExistsSimilarObservation checks if an observation about the same files exists for a project.
// Used to prevent duplicate observations when re-reading the same files.
func (s *ObservationStore) ExistsSimilarObservation(ctx context.Context, project string, filesRead, filesModified []string) (bool, error) {
// If no files tracked, can't deduplicate
if len(filesRead) == 0 && len(filesModified) == 0 {
return false, nil
}
// Check if any observation exists with the same primary file
// Use the first file as the key identifier
var primaryFile string
if len(filesRead) > 0 {
primaryFile = filesRead[0]
} else if len(filesModified) > 0 {
primaryFile = filesModified[0]
}
const query = `
SELECT COUNT(*) FROM observations
WHERE project = ? AND (files_read LIKE ? OR files_modified LIKE ?)
`
pattern := "%" + primaryFile + "%"
var count int
err := s.store.QueryRowContext(ctx, query, project, pattern, pattern).Scan(&count)
if err != nil {
return false, err
}
return count > 0, nil
}
// DeleteObservations deletes multiple observations by ID.
func (s *ObservationStore) DeleteObservations(ctx context.Context, ids []int64) (int64, error) {
if len(ids) == 0 {
return 0, nil
}
query := `DELETE FROM observations WHERE id IN (?` + repeatPlaceholders(len(ids)-1) + `)` // #nosec G202 -- uses parameterized placeholders
args := make([]interface{}, len(ids))
for i, id := range ids {
args[i] = id
}
result, err := s.store.db.ExecContext(ctx, query, args...)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
// MaxObservationsPerProject is the hard limit of observations per project.
const MaxObservationsPerProject = 100
// CleanupOldObservations deletes observations beyond the limit for a project.
// Keeps the most recent MaxObservationsPerProject observations per project.
// Returns the IDs of deleted observations for downstream cleanup (e.g., vector DB).
func (s *ObservationStore) CleanupOldObservations(ctx context.Context, project string) ([]int64, error) {
// First, find IDs that will be deleted
const selectQuery = `
SELECT id FROM observations
WHERE project = ? AND id NOT IN (
SELECT id FROM observations
WHERE project = ?
ORDER BY created_at_epoch DESC
LIMIT ?
)
`
rows, err := s.store.QueryContext(ctx, selectQuery, project, project, MaxObservationsPerProject)
if err != nil {
return nil, err
}
defer rows.Close()
var toDelete []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
toDelete = append(toDelete, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
if len(toDelete) == 0 {
return nil, nil
}
// Delete the observations
const deleteQuery = `
DELETE FROM observations
WHERE project = ? AND id NOT IN (
SELECT id FROM observations
WHERE project = ?
ORDER BY created_at_epoch DESC
LIMIT ?
)
`
_, err = s.store.ExecContext(ctx, deleteQuery, project, project, MaxObservationsPerProject)
if err != nil {
return nil, err
}
return toDelete, nil
}
// Helper functions
// scanObservation scans a single observation from a row scanner.
// This reduces code duplication across all observation query methods.
func scanObservation(scanner interface{ Scan(...interface{}) error }) (*models.Observation, error) {
var obs models.Observation
if err := scanner.Scan(
&obs.ID, &obs.SDKSessionID, &obs.Project, &obs.Scope, &obs.Type,
&obs.Title, &obs.Subtitle, &obs.Facts, &obs.Narrative,
&obs.Concepts, &obs.FilesRead, &obs.FilesModified, &obs.FileMtimes,
&obs.PromptNumber, &obs.DiscoveryTokens,
&obs.CreatedAt, &obs.CreatedAtEpoch,
); err != nil {
return nil, err
}
return &obs, nil
}
// scanObservationRows scans multiple observations from rows.
// Caller must close rows after calling this function.
func scanObservationRows(rows *sql.Rows) ([]*models.Observation, error) {
var observations []*models.Observation
for rows.Next() {
obs, err := scanObservation(rows)
if err != nil {
return nil, err
}
observations = append(observations, obs)
}
return observations, rows.Err()
}
func nullString(s string) sql.NullString {
return sql.NullString{String: s, Valid: s != ""}
}
func nullInt(i int) sql.NullInt64 {
return sql.NullInt64{Int64: int64(i), Valid: i > 0}
}
func repeatPlaceholders(n int) string {
if n <= 0 {
return ""
}
result := ""
for i := 0; i < n; i++ {
result += ", ?"
}
return result
}
+374
View File
@@ -0,0 +1,374 @@
// Package sqlite provides SQLite database operations for claude-mnemonic.
package sqlite
import (
"context"
"testing"
"time"
"github.com/lukaszraczylo/claude-mnemonic/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testObservationStore creates an ObservationStore with a test database including FTS5.
func testObservationStore(t *testing.T) (*ObservationStore, *Store, func()) {
t.Helper()
db, _, cleanup := testDB(t)
createAllTables(t, db)
store := newStoreFromDB(db)
obsStore := NewObservationStore(store)
return obsStore, store, cleanup
}
func TestObservationStore_StoreAndRetrieve(t *testing.T) {
obsStore, _, cleanup := testObservationStore(t)
defer cleanup()
ctx := context.Background()
obs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Test Observation",
Subtitle: "A subtitle",
Narrative: "This is a test observation about testing",
Facts: []string{"Fact 1", "Fact 2"},
Concepts: []string{"testing", "golang"},
FilesRead: []string{"test.go"},
FilesModified: []string{},
}
id, epoch, err := obsStore.StoreObservation(ctx, "session-1", "project-a", obs, 1, 100)
require.NoError(t, err)
assert.Greater(t, id, int64(0))
assert.Greater(t, epoch, int64(0))
// Retrieve by ID
retrieved, err := obsStore.GetObservationByID(ctx, id)
require.NoError(t, err)
require.NotNil(t, retrieved)
assert.Equal(t, id, retrieved.ID)
assert.Equal(t, "session-1", retrieved.SDKSessionID)
assert.Equal(t, "project-a", retrieved.Project)
assert.Equal(t, models.ObsTypeDiscovery, retrieved.Type)
assert.Equal(t, "Test Observation", retrieved.Title.String)
assert.Equal(t, "A subtitle", retrieved.Subtitle.String)
assert.Equal(t, "This is a test observation about testing", retrieved.Narrative.String)
assert.Equal(t, []string{"Fact 1", "Fact 2"}, []string(retrieved.Facts))
assert.Equal(t, []string{"testing", "golang"}, []string(retrieved.Concepts))
}
func TestObservationStore_GetRecentObservations(t *testing.T) {
obsStore, _, cleanup := testObservationStore(t)
defer cleanup()
ctx := context.Background()
// Create multiple observations
for i := 0; i < 10; i++ {
obs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Observation " + string(rune('A'+i)),
Narrative: "Content " + string(rune('A'+i)),
Concepts: []string{"test"},
}
_, _, err := obsStore.StoreObservation(ctx, "session-1", "project-a", obs, i+1, 100)
require.NoError(t, err)
time.Sleep(time.Millisecond) // Ensure different timestamps
}
// Get recent with limit 5
recent, err := obsStore.GetRecentObservations(ctx, "project-a", 5)
require.NoError(t, err)
assert.Len(t, recent, 5)
// Get recent with limit 20 (more than exists)
recent, err = obsStore.GetRecentObservations(ctx, "project-a", 20)
require.NoError(t, err)
assert.Len(t, recent, 10)
}
func TestObservationStore_SearchObservationsFTS(t *testing.T) {
obsStore, _, cleanup := testObservationStore(t)
defer cleanup()
// FTS5 tables are created by testObservationStore via testutil.CreateAllTables
ctx := context.Background()
// Create observations with different content
observations := []struct {
title string
narrative string
}{
{"Authentication implementation", "JWT based authentication flow"},
{"Database setup", "PostgreSQL configuration and migrations"},
{"Caching layer", "Redis caching implementation"},
{"User authentication fix", "Fixed authentication bug in login"},
{"API endpoints", "REST API implementation details"},
}
for _, o := range observations {
obs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: o.title,
Narrative: o.narrative,
}
_, _, err := obsStore.StoreObservation(ctx, "session-1", "project-a", obs, 1, 100)
require.NoError(t, err)
time.Sleep(time.Millisecond)
}
// Search for authentication - should find 2 observations
results, err := obsStore.SearchObservationsFTS(ctx, "authentication", "project-a", 50)
require.NoError(t, err)
assert.GreaterOrEqual(t, len(results), 2, "should find at least 2 authentication-related observations")
// Search for database - should find 1 observation
results, err = obsStore.SearchObservationsFTS(ctx, "database PostgreSQL", "project-a", 50)
require.NoError(t, err)
assert.GreaterOrEqual(t, len(results), 1, "should find at least 1 database-related observation")
}
func TestObservationStore_SearchObservationsFTS_LimitRespected(t *testing.T) {
obsStore, _, cleanup := testObservationStore(t)
defer cleanup()
// FTS5 tables are created by testObservationStore via testutil.CreateAllTables
ctx := context.Background()
// Create 20 observations with similar content
for i := 0; i < 20; i++ {
obs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Testing observation " + string(rune('A'+i)),
Narrative: "This is about testing and quality assurance " + string(rune('A'+i)),
}
_, _, err := obsStore.StoreObservation(ctx, "session-1", "project-a", obs, 1, 100)
require.NoError(t, err)
time.Sleep(time.Millisecond)
}
// Search with limit 5
results, err := obsStore.SearchObservationsFTS(ctx, "testing quality", "project-a", 5)
require.NoError(t, err)
assert.LessOrEqual(t, len(results), 5, "should respect limit of 5")
// Search with limit 15
results, err = obsStore.SearchObservationsFTS(ctx, "testing quality", "project-a", 15)
require.NoError(t, err)
assert.LessOrEqual(t, len(results), 15, "should respect limit of 15")
// Search with limit 50 (our new default)
results, err = obsStore.SearchObservationsFTS(ctx, "testing quality", "project-a", 50)
require.NoError(t, err)
assert.LessOrEqual(t, len(results), 50, "should respect limit of 50")
assert.Equal(t, 20, len(results), "should return all 20 matching observations")
}
func TestObservationStore_GlobalScope(t *testing.T) {
obsStore, _, cleanup := testObservationStore(t)
defer cleanup()
ctx := context.Background()
// Create a project-scoped observation
projectObs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Project specific code",
Narrative: "This is specific to project-a",
Concepts: []string{"project-specific"},
}
_, _, err := obsStore.StoreObservation(ctx, "session-1", "project-a", projectObs, 1, 100)
require.NoError(t, err)
// Create a global-scoped observation (has a globalizable concept)
globalObs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Security best practice",
Narrative: "Always validate user input",
Concepts: []string{"security", "best-practice"}, // "security" is in GlobalizableConcepts
}
_, _, err = obsStore.StoreObservation(ctx, "session-1", "project-a", globalObs, 1, 100)
require.NoError(t, err)
// Get recent for project-a - should see both
results, err := obsStore.GetRecentObservations(ctx, "project-a", 10)
require.NoError(t, err)
assert.Len(t, results, 2)
// Get recent for project-b - should only see global observation
results, err = obsStore.GetRecentObservations(ctx, "project-b", 10)
require.NoError(t, err)
assert.Len(t, results, 1)
assert.Equal(t, "Security best practice", results[0].Title.String)
assert.Equal(t, models.ScopeGlobal, results[0].Scope)
}
func TestObservationStore_DeleteObservations(t *testing.T) {
obsStore, _, cleanup := testObservationStore(t)
defer cleanup()
ctx := context.Background()
// Create observations
var ids []int64
for i := 0; i < 5; i++ {
obs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Observation " + string(rune('A'+i)),
}
id, _, err := obsStore.StoreObservation(ctx, "session-1", "project-a", obs, 1, 100)
require.NoError(t, err)
ids = append(ids, id)
}
// Verify all exist
all, err := obsStore.GetRecentObservations(ctx, "project-a", 10)
require.NoError(t, err)
assert.Len(t, all, 5)
// Delete first 3
deleted, err := obsStore.DeleteObservations(ctx, ids[:3])
require.NoError(t, err)
assert.Equal(t, int64(3), deleted)
// Verify only 2 remain
remaining, err := obsStore.GetRecentObservations(ctx, "project-a", 10)
require.NoError(t, err)
assert.Len(t, remaining, 2)
}
func TestObservationStore_GetObservationCount(t *testing.T) {
obsStore, _, cleanup := testObservationStore(t)
defer cleanup()
ctx := context.Background()
// Create observations for different projects
for i := 0; i < 5; i++ {
obs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Project A observation " + string(rune('0'+i)),
}
_, _, err := obsStore.StoreObservation(ctx, "session-1", "project-a", obs, 1, 100)
require.NoError(t, err)
}
for i := 0; i < 3; i++ {
obs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Project B observation " + string(rune('0'+i)),
}
_, _, err := obsStore.StoreObservation(ctx, "session-1", "project-b", obs, 1, 100)
require.NoError(t, err)
}
// Create a global observation
globalObs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Global observation",
Concepts: []string{"best-practice"}, // Makes it global
}
_, _, err := obsStore.StoreObservation(ctx, "session-1", "project-a", globalObs, 1, 100)
require.NoError(t, err)
// Count for project-a includes its own + global
count, err := obsStore.GetObservationCount(ctx, "project-a")
require.NoError(t, err)
assert.Equal(t, 6, count) // 5 project-a + 1 global
// Count for project-b includes its own + global
count, err = obsStore.GetObservationCount(ctx, "project-b")
require.NoError(t, err)
assert.Equal(t, 4, count) // 3 project-b + 1 global
}
func TestObservationStore_CleanupOldObservations(t *testing.T) {
obsStore, _, cleanup := testObservationStore(t)
defer cleanup()
ctx := context.Background()
// Create more observations than the limit (MaxObservationsPerProject = 100)
// We'll create a smaller number and verify the logic works
for i := 0; i < 10; i++ {
obs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Observation " + string(rune('A'+i)),
}
_, _, err := obsStore.StoreObservation(ctx, "session-1", "project-a", obs, i+1, 100)
require.NoError(t, err)
time.Sleep(time.Millisecond)
}
// Cleanup should return empty since we're under the limit
deletedIDs, err := obsStore.CleanupOldObservations(ctx, "project-a")
require.NoError(t, err)
assert.Empty(t, deletedIDs)
// All 10 should still exist
count, err := obsStore.GetObservationCount(ctx, "project-a")
require.NoError(t, err)
assert.Equal(t, 10, count)
}
func TestObservationStore_SetCleanupFunc(t *testing.T) {
obsStore, _, cleanup := testObservationStore(t)
defer cleanup()
ctx := context.Background()
// Track cleanup calls
var cleanupCalledWith []int64
obsStore.SetCleanupFunc(func(ctx context.Context, deletedIDs []int64) {
cleanupCalledWith = deletedIDs
})
// Store an observation (should trigger cleanup, but won't delete anything under limit)
obs := &models.ParsedObservation{
Type: models.ObsTypeDiscovery,
Title: "Test observation",
}
_, _, err := obsStore.StoreObservation(ctx, "session-1", "project-a", obs, 1, 100)
require.NoError(t, err)
// Cleanup func should not have been called since nothing was deleted
assert.Empty(t, cleanupCalledWith)
}
func TestExtractKeywords(t *testing.T) {
tests := []struct {
query string
expected []string
}{
{
query: "What is the authentication flow?",
expected: []string{"authentication", "flow"},
},
{
query: "How does the database connection work?",
expected: []string{"database", "connection"},
},
{
query: "JWT token validation",
expected: []string{"token", "validation"},
},
{
query: "the a an is are", // All stop words
expected: nil,
},
}
for _, tt := range tests {
t.Run(tt.query, func(t *testing.T) {
keywords := extractKeywords(tt.query)
for _, exp := range tt.expected {
assert.Contains(t, keywords, exp, "should contain keyword: "+exp)
}
})
}
}
+241
View File
@@ -0,0 +1,241 @@
// Package sqlite provides SQLite database operations for claude-mnemonic.
package sqlite
import (
"context"
"time"
"github.com/lukaszraczylo/claude-mnemonic/pkg/models"
)
// PromptCleanupFunc is a callback for when prompts are cleaned up.
// Receives the IDs of deleted prompts for downstream cleanup (e.g., vector DB).
type PromptCleanupFunc func(ctx context.Context, deletedIDs []int64)
// MaxPromptsGlobal is the hard limit of prompts across all projects.
const MaxPromptsGlobal = 500
// PromptStore provides user prompt-related database operations.
type PromptStore struct {
store *Store
cleanupFunc PromptCleanupFunc
}
// NewPromptStore creates a new prompt store.
func NewPromptStore(store *Store) *PromptStore {
return &PromptStore{store: store}
}
// SetCleanupFunc sets the callback for when prompts are deleted during cleanup.
func (s *PromptStore) SetCleanupFunc(fn PromptCleanupFunc) {
s.cleanupFunc = fn
}
// SaveUserPromptWithMatches saves a user prompt with matched observation count.
func (s *PromptStore) SaveUserPromptWithMatches(ctx context.Context, claudeSessionID string, promptNumber int, promptText string, matchedObservations int) (int64, error) {
now := time.Now()
const query = `
INSERT INTO user_prompts
(claude_session_id, prompt_number, prompt_text, matched_observations, created_at, created_at_epoch)
VALUES (?, ?, ?, ?, ?, ?)
`
result, err := s.store.ExecContext(ctx, query,
claudeSessionID, promptNumber, promptText, matchedObservations,
now.Format(time.RFC3339), now.UnixMilli(),
)
if err != nil {
return 0, err
}
id, _ := result.LastInsertId()
// Cleanup old prompts beyond the global limit
deletedIDs, _ := s.CleanupOldPrompts(ctx)
if len(deletedIDs) > 0 && s.cleanupFunc != nil {
s.cleanupFunc(ctx, deletedIDs)
}
return id, nil
}
// CleanupOldPrompts deletes prompts beyond the global limit.
// Keeps the most recent MaxPromptsGlobal prompts.
// Returns the IDs of deleted prompts for downstream cleanup (e.g., vector DB).
func (s *PromptStore) CleanupOldPrompts(ctx context.Context) ([]int64, error) {
// First, find IDs that will be deleted
const selectQuery = `
SELECT id FROM user_prompts
WHERE id NOT IN (
SELECT id FROM user_prompts
ORDER BY created_at_epoch DESC
LIMIT ?
)
`
rows, err := s.store.QueryContext(ctx, selectQuery, MaxPromptsGlobal)
if err != nil {
return nil, err
}
defer rows.Close()
var toDelete []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
toDelete = append(toDelete, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
if len(toDelete) == 0 {
return nil, nil
}
// Delete the prompts
const deleteQuery = `
DELETE FROM user_prompts
WHERE id NOT IN (
SELECT id FROM user_prompts
ORDER BY created_at_epoch DESC
LIMIT ?
)
`
_, err = s.store.ExecContext(ctx, deleteQuery, MaxPromptsGlobal)
if err != nil {
return nil, err
}
return toDelete, nil
}
// GetPromptsByIDs retrieves user prompts by a list of IDs.
func (s *PromptStore) GetPromptsByIDs(ctx context.Context, ids []int64, orderBy string, limit int) ([]*models.UserPromptWithSession, error) {
if len(ids) == 0 {
return nil, nil
}
// Build query with placeholders
// #nosec G202 -- query uses parameterized placeholders, not user input
query := `
SELECT up.id, up.claude_session_id, up.prompt_number, up.prompt_text,
up.created_at, up.created_at_epoch, s.project, s.sdk_session_id
FROM user_prompts up
JOIN sdk_sessions s ON up.claude_session_id = s.claude_session_id
WHERE up.id IN (?` + repeatPlaceholders(len(ids)-1) + `)
ORDER BY up.created_at_epoch `
if orderBy == "date_asc" {
query += "ASC"
} else {
query += "DESC"
}
if limit > 0 {
query += " LIMIT ?"
}
// Convert []int64 to []interface{}
args := make([]interface{}, len(ids))
for i, id := range ids {
args[i] = id
}
if limit > 0 {
args = append(args, limit)
}
rows, err := s.store.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var prompts []*models.UserPromptWithSession
for rows.Next() {
var prompt models.UserPromptWithSession
if err := rows.Scan(
&prompt.ID, &prompt.ClaudeSessionID, &prompt.PromptNumber, &prompt.PromptText,
&prompt.CreatedAt, &prompt.CreatedAtEpoch, &prompt.Project, &prompt.SDKSessionID,
); err != nil {
return nil, err
}
prompts = append(prompts, &prompt)
}
return prompts, rows.Err()
}
// GetAllRecentUserPrompts retrieves recent user prompts across all sessions.
func (s *PromptStore) GetAllRecentUserPrompts(ctx context.Context, limit int) ([]*models.UserPromptWithSession, error) {
const query = `
SELECT up.id, up.claude_session_id, up.prompt_number, up.prompt_text,
COALESCE(up.matched_observations, 0) as matched_observations,
up.created_at, up.created_at_epoch,
COALESCE(s.project, '') as project,
COALESCE(s.sdk_session_id, '') as sdk_session_id
FROM user_prompts up
LEFT JOIN sdk_sessions s ON up.claude_session_id = s.claude_session_id
ORDER BY up.created_at_epoch DESC
LIMIT ?
`
rows, err := s.store.QueryContext(ctx, query, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var prompts []*models.UserPromptWithSession
for rows.Next() {
var prompt models.UserPromptWithSession
if err := rows.Scan(
&prompt.ID, &prompt.ClaudeSessionID, &prompt.PromptNumber, &prompt.PromptText,
&prompt.MatchedObservations, &prompt.CreatedAt, &prompt.CreatedAtEpoch,
&prompt.Project, &prompt.SDKSessionID,
); err != nil {
return nil, err
}
prompts = append(prompts, &prompt)
}
return prompts, rows.Err()
}
// GetRecentUserPromptsByProject retrieves recent user prompts for a specific project.
func (s *PromptStore) GetRecentUserPromptsByProject(ctx context.Context, project string, limit int) ([]*models.UserPromptWithSession, error) {
const query = `
SELECT up.id, up.claude_session_id, up.prompt_number, up.prompt_text,
COALESCE(up.matched_observations, 0) as matched_observations,
up.created_at, up.created_at_epoch,
COALESCE(s.project, '') as project,
COALESCE(s.sdk_session_id, '') as sdk_session_id
FROM user_prompts up
LEFT JOIN sdk_sessions s ON up.claude_session_id = s.claude_session_id
WHERE s.project = ?
ORDER BY up.created_at_epoch DESC
LIMIT ?
`
rows, err := s.store.QueryContext(ctx, query, project, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var prompts []*models.UserPromptWithSession
for rows.Next() {
var prompt models.UserPromptWithSession
if err := rows.Scan(
&prompt.ID, &prompt.ClaudeSessionID, &prompt.PromptNumber, &prompt.PromptText,
&prompt.MatchedObservations, &prompt.CreatedAt, &prompt.CreatedAtEpoch,
&prompt.Project, &prompt.SDKSessionID,
); err != nil {
return nil, err
}
prompts = append(prompts, &prompt)
}
return prompts, rows.Err()
}
+196
View File
@@ -0,0 +1,196 @@
package sqlite
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testPromptStore(t *testing.T) (*PromptStore, *Store, func()) {
t.Helper()
db, _, cleanup := testDB(t)
createAllTables(t, db)
store := newStoreFromDB(db)
promptStore := NewPromptStore(store)
return promptStore, store, cleanup
}
func TestPromptStore_SaveUserPromptWithMatches(t *testing.T) {
promptStore, store, cleanup := testPromptStore(t)
defer cleanup()
ctx := context.Background()
// Create a session first
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
// Save a prompt
id, err := promptStore.SaveUserPromptWithMatches(ctx, "claude-1", 1, "Help me fix this bug", 5)
require.NoError(t, err)
assert.Greater(t, id, int64(0))
// Verify it was saved
var count int
err = storeDB(store).QueryRow("SELECT COUNT(*) FROM user_prompts WHERE id = ?", id).Scan(&count)
require.NoError(t, err)
assert.Equal(t, 1, count)
}
func TestPromptStore_GetAllRecentUserPrompts(t *testing.T) {
promptStore, store, cleanup := testPromptStore(t)
defer cleanup()
ctx := context.Background()
// Create a session
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
// Save multiple prompts
for i := 1; i <= 5; i++ {
_, err := promptStore.SaveUserPromptWithMatches(ctx, "claude-1", i, "Prompt "+string(rune('A'+i-1)), i)
require.NoError(t, err)
time.Sleep(time.Millisecond) // Ensure different timestamps
}
// Get recent prompts
prompts, err := promptStore.GetAllRecentUserPrompts(ctx, 3)
require.NoError(t, err)
assert.Len(t, prompts, 3)
// Should be in descending order (most recent first)
assert.Equal(t, 5, prompts[0].PromptNumber)
}
func TestPromptStore_GetRecentUserPromptsByProject(t *testing.T) {
promptStore, store, cleanup := testPromptStore(t)
defer cleanup()
ctx := context.Background()
// Create sessions for different projects
seedSession(t, storeDB(store), "claude-1", "sdk-1", "project-a")
seedSession(t, storeDB(store), "claude-2", "sdk-2", "project-b")
// Save prompts for both projects
for i := 1; i <= 3; i++ {
_, err := promptStore.SaveUserPromptWithMatches(ctx, "claude-1", i, "Project A prompt", 0)
require.NoError(t, err)
}
for i := 1; i <= 2; i++ {
_, err := promptStore.SaveUserPromptWithMatches(ctx, "claude-2", i, "Project B prompt", 0)
require.NoError(t, err)
}
// Get prompts for project-a
prompts, err := promptStore.GetRecentUserPromptsByProject(ctx, "project-a", 10)
require.NoError(t, err)
assert.Len(t, prompts, 3)
// Get prompts for project-b
prompts, err = promptStore.GetRecentUserPromptsByProject(ctx, "project-b", 10)
require.NoError(t, err)
assert.Len(t, prompts, 2)
}
func TestPromptStore_CleanupOldPrompts(t *testing.T) {
promptStore, store, cleanup := testPromptStore(t)
defer cleanup()
ctx := context.Background()
// Create a session
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
// Save more prompts than the limit
// Note: MaxPromptsGlobal is 500, but we'll test with a smaller number
// by directly calling CleanupOldPrompts
for i := 1; i <= 10; i++ {
_, err := storeDB(store).Exec(`
INSERT INTO user_prompts (claude_session_id, prompt_number, prompt_text, created_at, created_at_epoch)
VALUES (?, ?, ?, datetime('now'), ?)
`, "claude-1", i, "Prompt "+string(rune('A'+i-1)), time.Now().UnixMilli()+int64(i))
require.NoError(t, err)
}
// Verify we have 10 prompts
var count int
err := storeDB(store).QueryRow("SELECT COUNT(*) FROM user_prompts").Scan(&count)
require.NoError(t, err)
assert.Equal(t, 10, count)
// Cleanup should return empty since we're under the limit
deletedIDs, err := promptStore.CleanupOldPrompts(ctx)
require.NoError(t, err)
assert.Empty(t, deletedIDs)
}
func TestPromptStore_SetCleanupFunc(t *testing.T) {
promptStore, store, cleanup := testPromptStore(t)
defer cleanup()
ctx := context.Background()
// Track cleanup calls
var cleanupCalledWith []int64
promptStore.SetCleanupFunc(func(ctx context.Context, deletedIDs []int64) {
cleanupCalledWith = deletedIDs
})
// Create a session
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
// Save a prompt (should trigger cleanup, but won't delete anything under limit)
_, err := promptStore.SaveUserPromptWithMatches(ctx, "claude-1", 1, "Test prompt", 0)
require.NoError(t, err)
// Cleanup func should not have been called since nothing was deleted
assert.Empty(t, cleanupCalledWith)
}
func TestPromptStore_GetPromptsByIDs(t *testing.T) {
promptStore, store, cleanup := testPromptStore(t)
defer cleanup()
ctx := context.Background()
// Create a session
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
// Save some prompts and collect their IDs
var ids []int64
for i := 1; i <= 5; i++ {
id, err := promptStore.SaveUserPromptWithMatches(ctx, "claude-1", i, "Prompt "+string(rune('A'+i-1)), 0)
require.NoError(t, err)
ids = append(ids, id)
time.Sleep(time.Millisecond)
}
// Get specific prompts by ID
prompts, err := promptStore.GetPromptsByIDs(ctx, ids[:3], "date_desc", 10)
require.NoError(t, err)
assert.Len(t, prompts, 3)
// Test with ascending order
prompts, err = promptStore.GetPromptsByIDs(ctx, ids, "date_asc", 2)
require.NoError(t, err)
assert.Len(t, prompts, 2)
assert.Equal(t, 1, prompts[0].PromptNumber)
}
func TestPromptStore_GetPromptsByIDs_EmptyInput(t *testing.T) {
promptStore, _, cleanup := testPromptStore(t)
defer cleanup()
ctx := context.Background()
// Empty IDs should return nil
prompts, err := promptStore.GetPromptsByIDs(ctx, []int64{}, "date_desc", 10)
require.NoError(t, err)
assert.Nil(t, prompts)
}
+184
View File
@@ -0,0 +1,184 @@
// Package sqlite provides SQLite database operations for claude-mnemonic.
package sqlite
import (
"context"
"database/sql"
"time"
"github.com/lukaszraczylo/claude-mnemonic/pkg/models"
)
// SessionStore provides session-related database operations.
type SessionStore struct {
store *Store
}
// NewSessionStore creates a new session store.
func NewSessionStore(store *Store) *SessionStore {
return &SessionStore{store: store}
}
// CreateSDKSession creates a new SDK session (idempotent - returns existing ID if exists).
// This is the KEY to how claude-mnemonic stays unified across hooks.
func (s *SessionStore) CreateSDKSession(ctx context.Context, claudeSessionID, project, userPrompt string) (int64, error) {
now := time.Now()
// CRITICAL: INSERT OR IGNORE makes this idempotent
const query = `
INSERT OR IGNORE INTO sdk_sessions
(claude_session_id, sdk_session_id, project, user_prompt, started_at, started_at_epoch, status)
VALUES (?, ?, ?, ?, ?, ?, 'active')
`
result, err := s.store.ExecContext(ctx, query,
claudeSessionID, claudeSessionID, project, userPrompt,
now.Format(time.RFC3339), now.UnixMilli(),
)
if err != nil {
return 0, err
}
// Check if insert happened
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
// Session exists - UPDATE project and user_prompt if we have non-empty values
if project != "" {
const updateQuery = `
UPDATE sdk_sessions
SET project = ?, user_prompt = ?
WHERE claude_session_id = ?
`
_, _ = s.store.ExecContext(ctx, updateQuery, project, userPrompt, claudeSessionID)
}
// Fetch existing ID
var id int64
const selectQuery = `SELECT id FROM sdk_sessions WHERE claude_session_id = ? LIMIT 1`
err := s.store.QueryRowContext(ctx, selectQuery, claudeSessionID).Scan(&id)
return id, err
}
id, _ := result.LastInsertId()
return id, nil
}
// GetSessionByID retrieves a session by its database ID.
func (s *SessionStore) GetSessionByID(ctx context.Context, id int64) (*models.SDKSession, error) {
const query = `
SELECT id, claude_session_id, sdk_session_id, project, user_prompt,
worker_port, prompt_counter, status, started_at, started_at_epoch,
completed_at, completed_at_epoch
FROM sdk_sessions
WHERE id = ?
LIMIT 1
`
var sess models.SDKSession
err := s.store.QueryRowContext(ctx, query, id).Scan(
&sess.ID, &sess.ClaudeSessionID, &sess.SDKSessionID, &sess.Project, &sess.UserPrompt,
&sess.WorkerPort, &sess.PromptCounter, &sess.Status, &sess.StartedAt, &sess.StartedAtEpoch,
&sess.CompletedAt, &sess.CompletedAtEpoch,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return &sess, nil
}
// FindAnySDKSession finds any session by Claude session ID (any status).
func (s *SessionStore) FindAnySDKSession(ctx context.Context, claudeSessionID string) (*models.SDKSession, error) {
const query = `
SELECT id, claude_session_id, sdk_session_id, project, user_prompt,
worker_port, prompt_counter, status, started_at, started_at_epoch,
completed_at, completed_at_epoch
FROM sdk_sessions
WHERE claude_session_id = ?
LIMIT 1
`
var sess models.SDKSession
err := s.store.QueryRowContext(ctx, query, claudeSessionID).Scan(
&sess.ID, &sess.ClaudeSessionID, &sess.SDKSessionID, &sess.Project, &sess.UserPrompt,
&sess.WorkerPort, &sess.PromptCounter, &sess.Status, &sess.StartedAt, &sess.StartedAtEpoch,
&sess.CompletedAt, &sess.CompletedAtEpoch,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return &sess, nil
}
// IncrementPromptCounter increments the prompt counter and returns the new value.
func (s *SessionStore) IncrementPromptCounter(ctx context.Context, id int64) (int, error) {
const updateQuery = `
UPDATE sdk_sessions
SET prompt_counter = COALESCE(prompt_counter, 0) + 1
WHERE id = ?
`
if _, err := s.store.ExecContext(ctx, updateQuery, id); err != nil {
return 0, err
}
const selectQuery = `SELECT prompt_counter FROM sdk_sessions WHERE id = ?`
var counter int
err := s.store.QueryRowContext(ctx, selectQuery, id).Scan(&counter)
return counter, err
}
// GetPromptCounter returns the current prompt counter for a session.
func (s *SessionStore) GetPromptCounter(ctx context.Context, id int64) (int, error) {
const query = `SELECT COALESCE(prompt_counter, 0) FROM sdk_sessions WHERE id = ?`
var counter int
err := s.store.QueryRowContext(ctx, query, id).Scan(&counter)
return counter, err
}
// GetSessionsToday returns the count of sessions started today.
func (s *SessionStore) GetSessionsToday(ctx context.Context) (int, error) {
// Get start of today in milliseconds
now := time.Now()
startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
startEpoch := startOfDay.UnixMilli()
const query = `SELECT COUNT(*) FROM sdk_sessions WHERE started_at_epoch >= ?`
var count int
err := s.store.QueryRowContext(ctx, query, startEpoch).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
// GetAllProjects returns all unique project names.
func (s *SessionStore) GetAllProjects(ctx context.Context) ([]string, error) {
const query = `
SELECT DISTINCT project
FROM sdk_sessions
WHERE project IS NOT NULL AND project != ''
ORDER BY project ASC
`
rows, err := s.store.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var projects []string
for rows.Next() {
var project string
if err := rows.Scan(&project); err != nil {
return nil, err
}
projects = append(projects, project)
}
return projects, rows.Err()
}
+218
View File
@@ -0,0 +1,218 @@
package sqlite
import (
"context"
"testing"
"time"
"github.com/lukaszraczylo/claude-mnemonic/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testSessionStore(t *testing.T) (*SessionStore, *Store, func()) {
t.Helper()
db, _, cleanup := testDB(t)
createAllTables(t, db)
store := newStoreFromDB(db)
sessionStore := NewSessionStore(store)
return sessionStore, store, cleanup
}
func TestSessionStore_CreateSDKSession(t *testing.T) {
sessionStore, _, cleanup := testSessionStore(t)
defer cleanup()
ctx := context.Background()
// Create a new session
id, err := sessionStore.CreateSDKSession(ctx, "claude-1", "test-project", "initial prompt")
require.NoError(t, err)
assert.Greater(t, id, int64(0))
// Retrieve and verify
sess, err := sessionStore.GetSessionByID(ctx, id)
require.NoError(t, err)
require.NotNil(t, sess)
assert.Equal(t, "claude-1", sess.ClaudeSessionID)
assert.Equal(t, "test-project", sess.Project)
assert.Equal(t, models.SessionStatusActive, sess.Status)
}
func TestSessionStore_CreateSDKSession_Idempotent(t *testing.T) {
sessionStore, _, cleanup := testSessionStore(t)
defer cleanup()
ctx := context.Background()
// Create first session
id1, err := sessionStore.CreateSDKSession(ctx, "claude-1", "project-a", "prompt 1")
require.NoError(t, err)
// Create again with same claude_session_id but different project
id2, err := sessionStore.CreateSDKSession(ctx, "claude-1", "project-b", "prompt 2")
require.NoError(t, err)
// Should return same ID (idempotent)
assert.Equal(t, id1, id2)
// Should have updated project to project-b
sess, err := sessionStore.GetSessionByID(ctx, id1)
require.NoError(t, err)
assert.Equal(t, "project-b", sess.Project)
}
func TestSessionStore_FindAnySDKSession(t *testing.T) {
sessionStore, _, cleanup := testSessionStore(t)
defer cleanup()
ctx := context.Background()
// Create a session
_, err := sessionStore.CreateSDKSession(ctx, "claude-1", "test-project", "")
require.NoError(t, err)
// Find it
sess, err := sessionStore.FindAnySDKSession(ctx, "claude-1")
require.NoError(t, err)
require.NotNil(t, sess)
assert.Equal(t, "claude-1", sess.ClaudeSessionID)
// Try to find non-existent
sess, err = sessionStore.FindAnySDKSession(ctx, "claude-nonexistent")
require.NoError(t, err)
assert.Nil(t, sess)
}
func TestSessionStore_IncrementPromptCounter(t *testing.T) {
sessionStore, _, cleanup := testSessionStore(t)
defer cleanup()
ctx := context.Background()
// Create a session
id, err := sessionStore.CreateSDKSession(ctx, "claude-1", "test-project", "")
require.NoError(t, err)
// Initial counter should be 0
counter, err := sessionStore.GetPromptCounter(ctx, id)
require.NoError(t, err)
assert.Equal(t, 0, counter)
// Increment
counter, err = sessionStore.IncrementPromptCounter(ctx, id)
require.NoError(t, err)
assert.Equal(t, 1, counter)
// Increment again
counter, err = sessionStore.IncrementPromptCounter(ctx, id)
require.NoError(t, err)
assert.Equal(t, 2, counter)
// Verify via GetPromptCounter
counter, err = sessionStore.GetPromptCounter(ctx, id)
require.NoError(t, err)
assert.Equal(t, 2, counter)
}
func TestSessionStore_GetSessionsToday(t *testing.T) {
sessionStore, _, cleanup := testSessionStore(t)
defer cleanup()
ctx := context.Background()
// Initially no sessions today
count, err := sessionStore.GetSessionsToday(ctx)
require.NoError(t, err)
assert.Equal(t, 0, count)
// Create some sessions
_, err = sessionStore.CreateSDKSession(ctx, "claude-1", "project-a", "")
require.NoError(t, err)
_, err = sessionStore.CreateSDKSession(ctx, "claude-2", "project-b", "")
require.NoError(t, err)
_, err = sessionStore.CreateSDKSession(ctx, "claude-3", "project-c", "")
require.NoError(t, err)
// Should have 3 sessions today
count, err = sessionStore.GetSessionsToday(ctx)
require.NoError(t, err)
assert.Equal(t, 3, count)
}
func TestSessionStore_GetAllProjects(t *testing.T) {
sessionStore, _, cleanup := testSessionStore(t)
defer cleanup()
ctx := context.Background()
// Create sessions for different projects
_, err := sessionStore.CreateSDKSession(ctx, "claude-1", "alpha-project", "")
require.NoError(t, err)
_, err = sessionStore.CreateSDKSession(ctx, "claude-2", "beta-project", "")
require.NoError(t, err)
_, err = sessionStore.CreateSDKSession(ctx, "claude-3", "alpha-project", "") // duplicate
require.NoError(t, err)
_, err = sessionStore.CreateSDKSession(ctx, "claude-4", "gamma-project", "")
require.NoError(t, err)
// Get all projects
projects, err := sessionStore.GetAllProjects(ctx)
require.NoError(t, err)
assert.Len(t, projects, 3)
assert.Contains(t, projects, "alpha-project")
assert.Contains(t, projects, "beta-project")
assert.Contains(t, projects, "gamma-project")
// Should be sorted alphabetically
assert.Equal(t, "alpha-project", projects[0])
}
func TestSessionStore_GetSessionByID_NotFound(t *testing.T) {
sessionStore, _, cleanup := testSessionStore(t)
defer cleanup()
ctx := context.Background()
// Non-existent ID should return nil, nil (not an error)
sess, err := sessionStore.GetSessionByID(ctx, 999)
require.NoError(t, err)
assert.Nil(t, sess)
}
func TestSessionStore_SessionFields(t *testing.T) {
sessionStore, store, cleanup := testSessionStore(t)
defer cleanup()
ctx := context.Background()
// Create a session with full details
id, err := sessionStore.CreateSDKSession(ctx, "claude-full", "full-project", "full user prompt")
require.NoError(t, err)
// Manually update additional fields for testing
now := time.Now()
_, err = storeDB(store).Exec(`
UPDATE sdk_sessions
SET worker_port = ?, completed_at = ?, completed_at_epoch = ?, status = 'completed'
WHERE id = ?
`, 37777, now.Format(time.RFC3339), now.UnixMilli(), id)
require.NoError(t, err)
// Retrieve and verify all fields
sess, err := sessionStore.GetSessionByID(ctx, id)
require.NoError(t, err)
require.NotNil(t, sess)
assert.Equal(t, id, sess.ID)
assert.Equal(t, "claude-full", sess.ClaudeSessionID)
assert.Equal(t, "full-project", sess.Project)
assert.Equal(t, models.SessionStatusCompleted, sess.Status)
assert.True(t, sess.WorkerPort.Valid)
assert.Equal(t, int64(37777), sess.WorkerPort.Int64)
assert.True(t, sess.CompletedAt.Valid)
assert.True(t, sess.CompletedAtEpoch.Valid)
}
+134
View File
@@ -0,0 +1,134 @@
// Package sqlite provides SQLite database operations for claude-mnemonic.
package sqlite
import (
"context"
"database/sql"
"fmt"
"sync"
_ "github.com/mattn/go-sqlite3"
)
// Store provides database operations with connection pooling and prepared statements.
type Store struct {
db *sql.DB
stmtCache map[string]*sql.Stmt
stmtMu sync.RWMutex
}
// StoreConfig holds configuration for the database store.
type StoreConfig struct {
Path string
MaxConns int
WALMode bool
}
// NewStore creates a new database store with the given configuration.
func NewStore(cfg StoreConfig) (*Store, error) {
// Build connection string with pragmas
connStr := cfg.Path + "?_journal_mode=WAL&_synchronous=NORMAL&_foreign_keys=ON"
db, err := sql.Open("sqlite3", connStr)
if err != nil {
return nil, fmt.Errorf("open database: %w", err)
}
// Configure connection pool
maxConns := cfg.MaxConns
if maxConns <= 0 {
maxConns = 4
}
db.SetMaxOpenConns(maxConns)
db.SetMaxIdleConns(maxConns)
db.SetConnMaxLifetime(0) // Never expire - SQLite connections are cheap
// Verify connection
if err := db.Ping(); err != nil {
_ = db.Close()
return nil, fmt.Errorf("ping database: %w", err)
}
store := &Store{
db: db,
stmtCache: make(map[string]*sql.Stmt),
}
// Run migrations
mgr := NewMigrationManager(db)
if err := mgr.RunMigrations(); err != nil {
_ = db.Close()
return nil, fmt.Errorf("run migrations: %w", err)
}
return store, nil
}
// Close closes the database connection and all cached statements.
func (s *Store) Close() error {
s.stmtMu.Lock()
defer s.stmtMu.Unlock()
for _, stmt := range s.stmtCache {
_ = stmt.Close()
}
s.stmtCache = nil
return s.db.Close()
}
// GetStmt returns a cached prepared statement, creating it if necessary.
func (s *Store) GetStmt(query string) (*sql.Stmt, error) {
s.stmtMu.RLock()
stmt, ok := s.stmtCache[query]
s.stmtMu.RUnlock()
if ok {
return stmt, nil
}
s.stmtMu.Lock()
defer s.stmtMu.Unlock()
// Double-check after acquiring write lock
if stmt, ok := s.stmtCache[query]; ok {
return stmt, nil
}
stmt, err := s.db.Prepare(query)
if err != nil {
return nil, err
}
s.stmtCache[query] = stmt
return stmt, nil
}
// ExecContext executes a query that doesn't return rows.
func (s *Store) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
stmt, err := s.GetStmt(query)
if err != nil {
// Fall back to direct execution
return s.db.ExecContext(ctx, query, args...)
}
return stmt.ExecContext(ctx, args...)
}
// QueryContext executes a query that returns rows.
func (s *Store) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
stmt, err := s.GetStmt(query)
if err != nil {
// Fall back to direct execution
return s.db.QueryContext(ctx, query, args...)
}
return stmt.QueryContext(ctx, args...)
}
// QueryRowContext executes a query that returns a single row.
func (s *Store) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
stmt, err := s.GetStmt(query)
if err != nil {
// Fall back to direct execution
return s.db.QueryRowContext(ctx, query, args...)
}
return stmt.QueryRowContext(ctx, args...)
}
+200
View File
@@ -0,0 +1,200 @@
// Package sqlite provides SQLite database operations for claude-mnemonic.
package sqlite
import (
"context"
"database/sql"
"time"
"github.com/lukaszraczylo/claude-mnemonic/pkg/models"
)
// SummaryStore provides summary-related database operations.
type SummaryStore struct {
store *Store
}
// NewSummaryStore creates a new summary store.
func NewSummaryStore(store *Store) *SummaryStore {
return &SummaryStore{store: store}
}
// StoreSummary stores a new session summary.
func (s *SummaryStore) StoreSummary(ctx context.Context, sdkSessionID, project string, summary *models.ParsedSummary, promptNumber int, discoveryTokens int64) (int64, int64, error) {
now := time.Now()
nowEpoch := now.UnixMilli()
// Ensure session exists (auto-create if missing)
if err := s.ensureSessionExists(ctx, sdkSessionID, project); err != nil {
return 0, 0, err
}
const query = `
INSERT INTO session_summaries
(sdk_session_id, project, request, investigated, learned, completed,
next_steps, notes, prompt_number, discovery_tokens, created_at, created_at_epoch)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`
result, err := s.store.ExecContext(ctx, query,
sdkSessionID, project,
nullString(summary.Request), nullString(summary.Investigated),
nullString(summary.Learned), nullString(summary.Completed),
nullString(summary.NextSteps), nullString(summary.Notes),
nullInt(promptNumber), discoveryTokens,
now.Format(time.RFC3339), nowEpoch,
)
if err != nil {
return 0, 0, err
}
id, _ := result.LastInsertId()
return id, nowEpoch, nil
}
// ensureSessionExists creates a session if it doesn't exist.
func (s *SummaryStore) ensureSessionExists(ctx context.Context, sdkSessionID, project string) error {
const checkQuery = `SELECT id FROM sdk_sessions WHERE sdk_session_id = ?`
var id int64
err := s.store.QueryRowContext(ctx, checkQuery, sdkSessionID).Scan(&id)
if err == nil {
return nil // Session exists
}
if err != sql.ErrNoRows {
return err
}
// Auto-create session
now := time.Now()
const insertQuery = `
INSERT INTO sdk_sessions
(claude_session_id, sdk_session_id, project, started_at, started_at_epoch, status)
VALUES (?, ?, ?, ?, ?, 'active')
`
_, err = s.store.ExecContext(ctx, insertQuery,
sdkSessionID, sdkSessionID, project,
now.Format(time.RFC3339), now.UnixMilli(),
)
return err
}
// GetSummariesByIDs retrieves summaries by a list of IDs.
func (s *SummaryStore) GetSummariesByIDs(ctx context.Context, ids []int64, orderBy string, limit int) ([]*models.SessionSummary, error) {
if len(ids) == 0 {
return nil, nil
}
// Build query with placeholders
// #nosec G202 -- query uses parameterized placeholders, not user input
query := `
SELECT id, sdk_session_id, project, request, investigated, learned, completed,
next_steps, notes, prompt_number, discovery_tokens, created_at, created_at_epoch
FROM session_summaries
WHERE id IN (?` + repeatPlaceholders(len(ids)-1) + `)
ORDER BY created_at_epoch `
if orderBy == "date_asc" {
query += "ASC"
} else {
query += "DESC"
}
if limit > 0 {
query += " LIMIT ?"
}
// Convert []int64 to []interface{}
args := make([]interface{}, len(ids))
for i, id := range ids {
args[i] = id
}
if limit > 0 {
args = append(args, limit)
}
rows, err := s.store.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var summaries []*models.SessionSummary
for rows.Next() {
var summary models.SessionSummary
if err := rows.Scan(
&summary.ID, &summary.SDKSessionID, &summary.Project,
&summary.Request, &summary.Investigated, &summary.Learned, &summary.Completed,
&summary.NextSteps, &summary.Notes, &summary.PromptNumber, &summary.DiscoveryTokens,
&summary.CreatedAt, &summary.CreatedAtEpoch,
); err != nil {
return nil, err
}
summaries = append(summaries, &summary)
}
return summaries, rows.Err()
}
// GetRecentSummaries retrieves recent summaries for a project.
func (s *SummaryStore) GetRecentSummaries(ctx context.Context, project string, limit int) ([]*models.SessionSummary, error) {
const query = `
SELECT id, sdk_session_id, project, request, investigated, learned, completed,
next_steps, notes, prompt_number, discovery_tokens, created_at, created_at_epoch
FROM session_summaries
WHERE project = ?
ORDER BY created_at_epoch DESC
LIMIT ?
`
rows, err := s.store.QueryContext(ctx, query, project, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var summaries []*models.SessionSummary
for rows.Next() {
var summary models.SessionSummary
if err := rows.Scan(
&summary.ID, &summary.SDKSessionID, &summary.Project,
&summary.Request, &summary.Investigated, &summary.Learned, &summary.Completed,
&summary.NextSteps, &summary.Notes, &summary.PromptNumber, &summary.DiscoveryTokens,
&summary.CreatedAt, &summary.CreatedAtEpoch,
); err != nil {
return nil, err
}
summaries = append(summaries, &summary)
}
return summaries, rows.Err()
}
// GetAllRecentSummaries retrieves recent summaries across all projects.
func (s *SummaryStore) GetAllRecentSummaries(ctx context.Context, limit int) ([]*models.SessionSummary, error) {
const query = `
SELECT id, sdk_session_id, project, request, investigated, learned, completed,
next_steps, notes, prompt_number, discovery_tokens, created_at, created_at_epoch
FROM session_summaries
ORDER BY created_at_epoch DESC
LIMIT ?
`
rows, err := s.store.QueryContext(ctx, query, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var summaries []*models.SessionSummary
for rows.Next() {
var summary models.SessionSummary
if err := rows.Scan(
&summary.ID, &summary.SDKSessionID, &summary.Project,
&summary.Request, &summary.Investigated, &summary.Learned, &summary.Completed,
&summary.NextSteps, &summary.Notes, &summary.PromptNumber, &summary.DiscoveryTokens,
&summary.CreatedAt, &summary.CreatedAtEpoch,
); err != nil {
return nil, err
}
summaries = append(summaries, &summary)
}
return summaries, rows.Err()
}
+242
View File
@@ -0,0 +1,242 @@
package sqlite
import (
"context"
"testing"
"time"
"github.com/lukaszraczylo/claude-mnemonic/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testSummaryStore(t *testing.T) (*SummaryStore, *Store, func()) {
t.Helper()
db, _, cleanup := testDB(t)
createAllTables(t, db)
store := newStoreFromDB(db)
summaryStore := NewSummaryStore(store)
return summaryStore, store, cleanup
}
func TestSummaryStore_StoreSummary(t *testing.T) {
summaryStore, store, cleanup := testSummaryStore(t)
defer cleanup()
ctx := context.Background()
// Create a session first
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
summary := &models.ParsedSummary{
Request: "Add new feature",
Investigated: "Looked at existing code",
Learned: "Found the pattern to follow",
Completed: "Implemented the feature",
NextSteps: "Add tests",
Notes: "Some additional notes",
}
id, epoch, err := summaryStore.StoreSummary(ctx, "sdk-1", "test-project", summary, 1, 100)
require.NoError(t, err)
assert.Greater(t, id, int64(0))
assert.Greater(t, epoch, int64(0))
// Verify it was saved
var count int
err = storeDB(store).QueryRow("SELECT COUNT(*) FROM session_summaries WHERE id = ?", id).Scan(&count)
require.NoError(t, err)
assert.Equal(t, 1, count)
}
func TestSummaryStore_StoreSummary_AutoCreateSession(t *testing.T) {
summaryStore, store, cleanup := testSummaryStore(t)
defer cleanup()
ctx := context.Background()
// Don't create session beforehand - should be auto-created
summary := &models.ParsedSummary{
Request: "Test request",
}
id, _, err := summaryStore.StoreSummary(ctx, "auto-session", "test-project", summary, 1, 0)
require.NoError(t, err)
assert.Greater(t, id, int64(0))
// Verify session was auto-created
var sessionCount int
err = storeDB(store).QueryRow("SELECT COUNT(*) FROM sdk_sessions WHERE sdk_session_id = ?", "auto-session").Scan(&sessionCount)
require.NoError(t, err)
assert.Equal(t, 1, sessionCount)
}
func TestSummaryStore_GetRecentSummaries(t *testing.T) {
summaryStore, store, cleanup := testSummaryStore(t)
defer cleanup()
ctx := context.Background()
// Create a session
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
// Store multiple summaries
for i := 0; i < 5; i++ {
summary := &models.ParsedSummary{
Request: "Request " + string(rune('A'+i)),
}
_, _, err := summaryStore.StoreSummary(ctx, "sdk-1", "test-project", summary, i+1, 0)
require.NoError(t, err)
time.Sleep(time.Millisecond) // Ensure different timestamps
}
// Get recent summaries with limit
summaries, err := summaryStore.GetRecentSummaries(ctx, "test-project", 3)
require.NoError(t, err)
assert.Len(t, summaries, 3)
// Should be in descending order
assert.Equal(t, int64(5), summaries[0].PromptNumber.Int64)
}
func TestSummaryStore_GetAllRecentSummaries(t *testing.T) {
summaryStore, store, cleanup := testSummaryStore(t)
defer cleanup()
ctx := context.Background()
// Create sessions for different projects
seedSession(t, storeDB(store), "claude-1", "sdk-1", "project-a")
seedSession(t, storeDB(store), "claude-2", "sdk-2", "project-b")
// Store summaries for both projects
for i := 0; i < 3; i++ {
summary := &models.ParsedSummary{Request: "Project A request"}
_, _, err := summaryStore.StoreSummary(ctx, "sdk-1", "project-a", summary, i+1, 0)
require.NoError(t, err)
}
for i := 0; i < 2; i++ {
summary := &models.ParsedSummary{Request: "Project B request"}
_, _, err := summaryStore.StoreSummary(ctx, "sdk-2", "project-b", summary, i+1, 0)
require.NoError(t, err)
}
// Get all summaries (should include both projects)
summaries, err := summaryStore.GetAllRecentSummaries(ctx, 10)
require.NoError(t, err)
assert.Len(t, summaries, 5)
}
func TestSummaryStore_GetSummariesByIDs(t *testing.T) {
summaryStore, store, cleanup := testSummaryStore(t)
defer cleanup()
ctx := context.Background()
// Create a session
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
// Store summaries and collect IDs
var ids []int64
for i := 0; i < 5; i++ {
summary := &models.ParsedSummary{Request: "Request " + string(rune('A'+i))}
id, _, err := summaryStore.StoreSummary(ctx, "sdk-1", "test-project", summary, i+1, 0)
require.NoError(t, err)
ids = append(ids, id)
time.Sleep(time.Millisecond)
}
// Get specific summaries by ID
summaries, err := summaryStore.GetSummariesByIDs(ctx, ids[:3], "date_desc", 10)
require.NoError(t, err)
assert.Len(t, summaries, 3)
// Test with ascending order
summaries, err = summaryStore.GetSummariesByIDs(ctx, ids, "date_asc", 2)
require.NoError(t, err)
assert.Len(t, summaries, 2)
assert.Equal(t, int64(1), summaries[0].PromptNumber.Int64)
}
func TestSummaryStore_GetSummariesByIDs_EmptyInput(t *testing.T) {
summaryStore, _, cleanup := testSummaryStore(t)
defer cleanup()
ctx := context.Background()
// Empty IDs should return nil
summaries, err := summaryStore.GetSummariesByIDs(ctx, []int64{}, "date_desc", 10)
require.NoError(t, err)
assert.Nil(t, summaries)
}
func TestSummaryStore_SummaryFields(t *testing.T) {
summaryStore, store, cleanup := testSummaryStore(t)
defer cleanup()
ctx := context.Background()
// Create a session
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
// Store a summary with all fields
summary := &models.ParsedSummary{
Request: "Add authentication",
Investigated: "Reviewed existing auth code",
Learned: "OAuth is preferred",
Completed: "Implemented OAuth flow",
NextSteps: "Add refresh token support",
Notes: "Consider rate limiting",
}
id, _, err := summaryStore.StoreSummary(ctx, "sdk-1", "test-project", summary, 5, 1500)
require.NoError(t, err)
// Retrieve and verify all fields
summaries, err := summaryStore.GetSummariesByIDs(ctx, []int64{id}, "date_desc", 1)
require.NoError(t, err)
require.Len(t, summaries, 1)
s := summaries[0]
assert.Equal(t, id, s.ID)
assert.Equal(t, "sdk-1", s.SDKSessionID)
assert.Equal(t, "test-project", s.Project)
assert.Equal(t, "Add authentication", s.Request.String)
assert.Equal(t, "Reviewed existing auth code", s.Investigated.String)
assert.Equal(t, "OAuth is preferred", s.Learned.String)
assert.Equal(t, "Implemented OAuth flow", s.Completed.String)
assert.Equal(t, "Add refresh token support", s.NextSteps.String)
assert.Equal(t, "Consider rate limiting", s.Notes.String)
assert.Equal(t, int64(5), s.PromptNumber.Int64)
assert.Equal(t, int64(1500), s.DiscoveryTokens)
}
func TestSummaryStore_EmptySummary(t *testing.T) {
summaryStore, store, cleanup := testSummaryStore(t)
defer cleanup()
ctx := context.Background()
// Create a session
seedSession(t, storeDB(store), "claude-1", "sdk-1", "test-project")
// Store an empty summary
summary := &models.ParsedSummary{}
id, _, err := summaryStore.StoreSummary(ctx, "sdk-1", "test-project", summary, 0, 0)
require.NoError(t, err)
assert.Greater(t, id, int64(0))
// Retrieve and verify null fields
summaries, err := summaryStore.GetSummariesByIDs(ctx, []int64{id}, "date_desc", 1)
require.NoError(t, err)
require.Len(t, summaries, 1)
s := summaries[0]
assert.False(t, s.Request.Valid || s.Request.String != "")
assert.False(t, s.Investigated.Valid || s.Investigated.String != "")
assert.False(t, s.Learned.Valid || s.Learned.String != "")
}
+315
View File
@@ -0,0 +1,315 @@
package sqlite
import (
"database/sql"
"os"
"testing"
_ "github.com/mattn/go-sqlite3"
)
// newStoreFromDB creates a Store from an existing database connection for testing.
func newStoreFromDB(db *sql.DB) *Store {
return &Store{
db: db,
stmtCache: make(map[string]*sql.Stmt),
}
}
// storeDB returns the underlying database connection from a store for testing.
func storeDB(s *Store) *sql.DB {
return s.db
}
// testDB creates a temporary SQLite database for testing.
// Returns the database, path, and a cleanup function.
func testDB(t *testing.T) (*sql.DB, string, func()) {
t.Helper()
tmpDir, err := os.MkdirTemp("", "claude-mnemonic-test-*")
if err != nil {
t.Fatalf("create temp dir: %v", err)
}
dbPath := tmpDir + "/test.db"
connStr := dbPath + "?_journal_mode=WAL&_synchronous=NORMAL&_foreign_keys=ON"
db, err := sql.Open("sqlite3", connStr)
if err != nil {
_ = os.RemoveAll(tmpDir)
t.Fatalf("open database: %v", err)
}
cleanup := func() {
_ = db.Close()
_ = os.RemoveAll(tmpDir)
}
return db, dbPath, cleanup
}
// createBaseTables creates the base tables without FTS5 for unit testing.
func createBaseTables(t *testing.T, db *sql.DB) {
t.Helper()
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS schema_versions (
id INTEGER PRIMARY KEY,
version INTEGER UNIQUE NOT NULL,
applied_at TEXT NOT NULL
)
`)
if err != nil {
t.Fatalf("create schema_versions: %v", err)
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS sdk_sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
claude_session_id TEXT UNIQUE NOT NULL,
sdk_session_id TEXT UNIQUE,
project TEXT NOT NULL,
user_prompt TEXT,
started_at TEXT NOT NULL,
started_at_epoch INTEGER NOT NULL,
completed_at TEXT,
completed_at_epoch INTEGER,
status TEXT CHECK(status IN ('active', 'completed', 'failed')) NOT NULL DEFAULT 'active',
worker_port INTEGER,
prompt_counter INTEGER DEFAULT 0
)
`)
if err != nil {
t.Fatalf("create sdk_sessions: %v", err)
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS observations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sdk_session_id TEXT NOT NULL,
project TEXT NOT NULL,
text TEXT,
type TEXT NOT NULL CHECK(type IN ('decision', 'bugfix', 'feature', 'refactor', 'discovery', 'change')),
title TEXT,
subtitle TEXT,
facts TEXT,
narrative TEXT,
concepts TEXT,
files_read TEXT,
files_modified TEXT,
file_mtimes TEXT,
scope TEXT DEFAULT 'project' CHECK(scope IN ('project', 'global')),
prompt_number INTEGER,
discovery_tokens INTEGER DEFAULT 0,
created_at TEXT NOT NULL,
created_at_epoch INTEGER NOT NULL,
FOREIGN KEY(sdk_session_id) REFERENCES sdk_sessions(sdk_session_id) ON DELETE CASCADE
)
`)
if err != nil {
t.Fatalf("create observations: %v", err)
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS session_summaries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sdk_session_id TEXT NOT NULL,
project TEXT NOT NULL,
request TEXT,
investigated TEXT,
learned TEXT,
completed TEXT,
next_steps TEXT,
files_read TEXT,
files_edited TEXT,
notes TEXT,
prompt_number INTEGER,
discovery_tokens INTEGER DEFAULT 0,
created_at TEXT NOT NULL,
created_at_epoch INTEGER NOT NULL,
FOREIGN KEY(sdk_session_id) REFERENCES sdk_sessions(sdk_session_id) ON DELETE CASCADE
)
`)
if err != nil {
t.Fatalf("create session_summaries: %v", err)
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS user_prompts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
claude_session_id TEXT NOT NULL,
prompt_number INTEGER NOT NULL,
prompt_text TEXT NOT NULL,
matched_observations INTEGER DEFAULT 0,
created_at TEXT NOT NULL,
created_at_epoch INTEGER NOT NULL,
FOREIGN KEY(claude_session_id) REFERENCES sdk_sessions(claude_session_id) ON DELETE CASCADE
)
`)
if err != nil {
t.Fatalf("create user_prompts: %v", err)
}
indexes := []string{
`CREATE INDEX IF NOT EXISTS idx_sdk_sessions_claude_id ON sdk_sessions(claude_session_id)`,
`CREATE INDEX IF NOT EXISTS idx_sdk_sessions_sdk_id ON sdk_sessions(sdk_session_id)`,
`CREATE INDEX IF NOT EXISTS idx_sdk_sessions_project ON sdk_sessions(project)`,
`CREATE INDEX IF NOT EXISTS idx_observations_sdk_session ON observations(sdk_session_id)`,
`CREATE INDEX IF NOT EXISTS idx_observations_project ON observations(project)`,
`CREATE INDEX IF NOT EXISTS idx_observations_scope ON observations(scope)`,
`CREATE INDEX IF NOT EXISTS idx_observations_created ON observations(created_at_epoch DESC)`,
`CREATE INDEX IF NOT EXISTS idx_session_summaries_sdk_session ON session_summaries(sdk_session_id)`,
`CREATE INDEX IF NOT EXISTS idx_session_summaries_project ON session_summaries(project)`,
`CREATE INDEX IF NOT EXISTS idx_user_prompts_claude_session ON user_prompts(claude_session_id)`,
`CREATE INDEX IF NOT EXISTS idx_user_prompts_created ON user_prompts(created_at_epoch DESC)`,
}
for _, idx := range indexes {
if _, err := db.Exec(idx); err != nil {
t.Fatalf("create index: %v", err)
}
}
}
// seedSession creates a test session in the database.
func seedSession(t *testing.T, db *sql.DB, claudeSessionID, sdkSessionID, project string) {
t.Helper()
_, err := db.Exec(`
INSERT INTO sdk_sessions (claude_session_id, sdk_session_id, project, started_at, started_at_epoch, status)
VALUES (?, ?, ?, datetime('now'), strftime('%s', 'now') * 1000, 'active')
`, claudeSessionID, sdkSessionID, project)
if err != nil {
t.Fatalf("seed session: %v", err)
}
}
// hasFTS5 checks if FTS5 is available in the SQLite build.
func hasFTS5(db *sql.DB) bool {
_, err := db.Exec("CREATE VIRTUAL TABLE IF NOT EXISTS fts5_test USING fts5(content)")
if err != nil {
return false
}
_, _ = db.Exec("DROP TABLE IF EXISTS fts5_test")
return true
}
// createFTSTables creates FTS5 virtual tables and triggers for full-text search.
func createFTSTables(t *testing.T, db *sql.DB) {
t.Helper()
if !hasFTS5(db) {
t.Skip("FTS5 not available in this SQLite build")
}
_, err := db.Exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS observations_fts USING fts5(
title, subtitle, narrative,
content='observations',
content_rowid='id'
)
`)
if err != nil {
t.Fatalf("create observations_fts: %v", err)
}
_, err = db.Exec(`
CREATE TRIGGER IF NOT EXISTS observations_ai AFTER INSERT ON observations BEGIN
INSERT INTO observations_fts(rowid, title, subtitle, narrative)
VALUES (new.id, new.title, new.subtitle, new.narrative);
END
`)
if err != nil {
t.Fatalf("create observations_ai trigger: %v", err)
}
_, err = db.Exec(`
CREATE TRIGGER IF NOT EXISTS observations_ad AFTER DELETE ON observations BEGIN
INSERT INTO observations_fts(observations_fts, rowid, title, subtitle, narrative)
VALUES ('delete', old.id, old.title, old.subtitle, old.narrative);
END
`)
if err != nil {
t.Fatalf("create observations_ad trigger: %v", err)
}
_, err = db.Exec(`
CREATE TRIGGER IF NOT EXISTS observations_au AFTER UPDATE ON observations BEGIN
INSERT INTO observations_fts(observations_fts, rowid, title, subtitle, narrative)
VALUES ('delete', old.id, old.title, old.subtitle, old.narrative);
INSERT INTO observations_fts(rowid, title, subtitle, narrative)
VALUES (new.id, new.title, new.subtitle, new.narrative);
END
`)
if err != nil {
t.Fatalf("create observations_au trigger: %v", err)
}
_, err = db.Exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS session_summaries_fts USING fts5(
request, investigated, learned, completed, next_steps, notes,
content='session_summaries',
content_rowid='id'
)
`)
if err != nil {
t.Fatalf("create session_summaries_fts: %v", err)
}
_, err = db.Exec(`
CREATE TRIGGER IF NOT EXISTS summaries_ai AFTER INSERT ON session_summaries BEGIN
INSERT INTO session_summaries_fts(rowid, request, investigated, learned, completed, next_steps, notes)
VALUES (new.id, new.request, new.investigated, new.learned, new.completed, new.next_steps, new.notes);
END
`)
if err != nil {
t.Fatalf("create summaries_ai trigger: %v", err)
}
_, err = db.Exec(`
CREATE TRIGGER IF NOT EXISTS summaries_ad AFTER DELETE ON session_summaries BEGIN
INSERT INTO session_summaries_fts(session_summaries_fts, rowid, request, investigated, learned, completed, next_steps, notes)
VALUES ('delete', old.id, old.request, old.investigated, old.learned, old.completed, old.next_steps, old.notes);
END
`)
if err != nil {
t.Fatalf("create summaries_ad trigger: %v", err)
}
_, err = db.Exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS user_prompts_fts USING fts5(
prompt_text,
content='user_prompts',
content_rowid='id'
)
`)
if err != nil {
t.Fatalf("create user_prompts_fts: %v", err)
}
_, err = db.Exec(`
CREATE TRIGGER IF NOT EXISTS prompts_ai AFTER INSERT ON user_prompts BEGIN
INSERT INTO user_prompts_fts(rowid, prompt_text)
VALUES (new.id, new.prompt_text);
END
`)
if err != nil {
t.Fatalf("create prompts_ai trigger: %v", err)
}
_, err = db.Exec(`
CREATE TRIGGER IF NOT EXISTS prompts_ad AFTER DELETE ON user_prompts BEGIN
INSERT INTO user_prompts_fts(user_prompts_fts, rowid, prompt_text)
VALUES ('delete', old.id, old.prompt_text);
END
`)
if err != nil {
t.Fatalf("create prompts_ad trigger: %v", err)
}
}
// createAllTables creates all tables including FTS5 for comprehensive testing.
func createAllTables(t *testing.T, db *sql.DB) {
t.Helper()
createBaseTables(t, db)
createFTSTables(t, db)
}