mirror of
https://github.com/lukaszraczylo/claude-mnemonic.git
synced 2026-06-06 23:13:50 +00:00
5c2685c7b6
* feat(leann-phase2): implement hybrid vector storage and graph-based search
- [x] Add AST-aware code chunking for Go, Python, and TypeScript using tree-sitter
- [x] Implement LEANN-inspired hybrid vector storage with hub detection and selective embedding storage (60-80% savings)
- [x] Add observation relationship graph with CSR format and edge detection (file overlap, semantic similarity, temporal, concept)
- [x] Implement graph-aware search with two-level traversal and relationship-based ranking
- [x] Add auto-tuning system for dynamic hub threshold adjustment based on query performance
- [x] Add comprehensive metrics tracking for vector storage, queries, latency, and graph traversals
- [x] Update configuration system with graph and hybrid storage settings
- [x] Add graph stats and vector metrics endpoints to worker service
- [x] Enhance UI sidebar with advanced metrics display and graph visualization
- [x] Optimize struct field alignment throughout codebase for memory efficiency
- [x] Update documentation with LEANN Phase 2 features and performance benefits
- [x] Add tree-sitter dependency for AST parsing
* fix: add fts5 build tag to CI workflow
Pass build-tags: "fts5" to shared workflow to properly compile
sqlite-vec-go-bindings with SQLite FTS5 support.
This fixes test failures in hybrid vector storage tests that require
CGO and FTS5 build tags.
Requires shared-actions@8f7f235 or later.
* docs: add testing documentation and macOS ARM64 known issue
Document the macOS ARM64 CGO linking issue with sqlite-vec-go-bindings
that prevents hybrid package tests from compiling locally.
Added:
- .github/TESTING.md: Comprehensive testing guide with platform-specific
issues, workarounds, and CI configuration details
- internal/vector/hybrid/README.md: Package-specific documentation
explaining the macOS limitation
- .github/CI_FIX_SUMMARY.md: Technical details of the CI fix
Key points:
- 41 out of 42 packages test successfully on all platforms
- hybrid package tests fail only on macOS ARM64 (local dev issue)
- Linux CI tests pass with proper build-tags: "fts5" configuration
- Production builds and runtime functionality unaffected
This is a known limitation of sqlite-vec-go-bindings on macOS ARM64
and does not impact CI/CD or production deployments.
* fix: add SQLite busy_timeout to prevent database locked errors
Set PRAGMA busy_timeout=5000 (5 seconds) to allow SQLite to retry
when the database is locked instead of failing immediately.
This fixes race conditions when multiple goroutines try to write
simultaneously, particularly in tests where StoreObservation spawns
async cleanup goroutines.
Root cause:
- StoreObservation launches goroutine -> CleanupOldObservations
- Multiple concurrent cleanups caused "database is locked" errors
- Without busy_timeout, SQLite fails immediately on lock contention
Solution:
- Add 5-second busy timeout for automatic retry on lock
- Standard practice for concurrent SQLite usage
- Works with existing WAL mode configuration
Fixes TestObservationStore_CleanupOldObservations in CI.
* docs: complete summary of all CI test fixes
Comprehensive documentation of all fixes applied:
1. Missing build tags (fts5)
2. Database locked errors (busy_timeout)
All 41/42 packages now pass tests. The hybrid package has a known
macOS ARM64 limitation that doesn't affect CI or production.
No functionality was removed - all fixes are additive only.
* fix: add SQLite driver import to hybrid tests for CGO linking
Add blank import of mattn/go-sqlite3 to hybrid test files to ensure
the SQLite driver is linked into the test binary. This provides the
SQLite symbols that sqlite-vec-go-bindings requires.
Root cause:
- hybrid package imports sqlitevec (transitively depends on sqlite-vec CGO)
- Test binary needs SQLite symbols for linking
- sqlitevec tests already had this import, but hybrid tests didn't
- Without the driver import, linker fails with "undefined symbols"
This fix enables hybrid tests to run with -race flag on all platforms.
Before: 41/42 packages pass (hybrid failed to link)
After: 42/42 packages pass ✅
Fixes hybrid test compilation on macOS ARM64, Linux, and Windows.
* docs: remove outdated macOS limitation documentation
The hybrid test linking issue has been fixed by adding the SQLite
driver import. All tests now pass on all platforms including macOS.
Removed:
- internal/vector/hybrid/README.md (documented workaround no longer needed)
- .github/TESTING.md (macOS limitation section obsolete)
All 42/42 packages now test successfully with -race flag.
* docs: final comprehensive summary of all CI fixes
All three issues now resolved:
1. Missing fts5 build tags
2. Database busy_timeout for concurrent writes
3. Missing SQLite driver import in hybrid tests
Result: 42/42 packages pass with -race on all platforms.
Credit to reviewer for identifying the race detector concern.
425 lines
12 KiB
Go
425 lines
12 KiB
Go
// Package models contains domain models for claude-mnemonic.
|
|
package models
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
// ObservationSuite is a test suite for Observation operations.
|
|
type ObservationSuite struct {
|
|
suite.Suite
|
|
}
|
|
|
|
func TestObservationSuite(t *testing.T) {
|
|
suite.Run(t, new(ObservationSuite))
|
|
}
|
|
|
|
// TestObservationTypeConstants tests observation type constants.
|
|
func (s *ObservationSuite) TestObservationTypeConstants() {
|
|
s.Equal(ObservationType("discovery"), ObsTypeDiscovery)
|
|
s.Equal(ObservationType("decision"), ObsTypeDecision)
|
|
s.Equal(ObservationType("bugfix"), ObsTypeBugfix)
|
|
s.Equal(ObservationType("feature"), ObsTypeFeature)
|
|
s.Equal(ObservationType("refactor"), ObsTypeRefactor)
|
|
s.Equal(ObservationType("change"), ObsTypeChange)
|
|
}
|
|
|
|
// TestScopeConstants tests scope constants.
|
|
func (s *ObservationSuite) TestScopeConstants() {
|
|
s.Equal(ObservationScope("project"), ScopeProject)
|
|
s.Equal(ObservationScope("global"), ScopeGlobal)
|
|
}
|
|
|
|
// TestGlobalizableConcepts tests that globalizable concepts are defined.
|
|
func (s *ObservationSuite) TestGlobalizableConcepts() {
|
|
expected := []string{
|
|
"best-practice", "pattern", "anti-pattern", "architecture",
|
|
"security", "performance", "testing",
|
|
"debugging", "workflow", "tooling",
|
|
}
|
|
s.Equal(expected, GlobalizableConcepts)
|
|
}
|
|
|
|
// TestDetermineScope_TableDriven tests scope determination with various concepts.
|
|
func (s *ObservationSuite) TestDetermineScope_TableDriven() {
|
|
tests := []struct {
|
|
name string
|
|
expected ObservationScope
|
|
concepts []string
|
|
}{
|
|
{
|
|
name: "empty concepts - project scope",
|
|
concepts: []string{},
|
|
expected: ScopeProject,
|
|
},
|
|
{
|
|
name: "no globalizable concepts - project scope",
|
|
concepts: []string{"how-it-works", "custom-tag"},
|
|
expected: ScopeProject,
|
|
},
|
|
{
|
|
name: "security concept - global scope",
|
|
concepts: []string{"security"},
|
|
expected: ScopeGlobal,
|
|
},
|
|
{
|
|
name: "best-practice concept - global scope",
|
|
concepts: []string{"best-practice"},
|
|
expected: ScopeGlobal,
|
|
},
|
|
{
|
|
name: "mixed concepts with globalizable - global scope",
|
|
concepts: []string{"how-it-works", "security"},
|
|
expected: ScopeGlobal,
|
|
},
|
|
{
|
|
name: "performance concept - global scope",
|
|
concepts: []string{"performance"},
|
|
expected: ScopeGlobal,
|
|
},
|
|
{
|
|
name: "testing concept - global scope",
|
|
concepts: []string{"testing"},
|
|
expected: ScopeGlobal,
|
|
},
|
|
{
|
|
name: "pattern concept - global scope",
|
|
concepts: []string{"pattern"},
|
|
expected: ScopeGlobal,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
s.Run(tt.name, func() {
|
|
result := DetermineScope(tt.concepts)
|
|
s.Equal(tt.expected, result)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestParsedObservation_FileMtimesJSON tests FileMtimes JSON serialization.
|
|
func (s *ObservationSuite) TestParsedObservation_FileMtimesJSON() {
|
|
obs := &ParsedObservation{
|
|
Type: ObsTypeDiscovery,
|
|
Title: "Test",
|
|
FileMtimes: map[string]int64{"file1.go": 1234567890, "file2.go": 1234567891},
|
|
}
|
|
|
|
// Verify mtimes can be marshaled
|
|
data, err := json.Marshal(obs.FileMtimes)
|
|
s.NoError(err)
|
|
s.Contains(string(data), "file1.go")
|
|
s.Contains(string(data), "1234567890")
|
|
}
|
|
|
|
// TestObservation_CheckStaleness_TableDriven tests staleness checking.
|
|
func (s *ObservationSuite) TestObservation_CheckStaleness_TableDriven() {
|
|
tests := []struct {
|
|
storedMtimes map[string]int64
|
|
currentMtimes map[string]int64
|
|
name string
|
|
expectedStale bool
|
|
}{
|
|
{
|
|
name: "empty stored mtimes - not stale",
|
|
storedMtimes: map[string]int64{},
|
|
currentMtimes: map[string]int64{"file.go": 1000},
|
|
expectedStale: false,
|
|
},
|
|
{
|
|
name: "matching mtimes - not stale",
|
|
storedMtimes: map[string]int64{"file.go": 1000},
|
|
currentMtimes: map[string]int64{"file.go": 1000},
|
|
expectedStale: false,
|
|
},
|
|
{
|
|
name: "file modified - stale",
|
|
storedMtimes: map[string]int64{"file.go": 1000},
|
|
currentMtimes: map[string]int64{"file.go": 2000},
|
|
expectedStale: true,
|
|
},
|
|
{
|
|
name: "file missing from current - not stale (files might not be checked)",
|
|
storedMtimes: map[string]int64{"file.go": 1000},
|
|
currentMtimes: map[string]int64{},
|
|
expectedStale: false, // Missing files don't mark as stale per the implementation
|
|
},
|
|
{
|
|
name: "multiple files, one modified - stale",
|
|
storedMtimes: map[string]int64{"file1.go": 1000, "file2.go": 2000},
|
|
currentMtimes: map[string]int64{"file1.go": 1000, "file2.go": 3000},
|
|
expectedStale: true,
|
|
},
|
|
{
|
|
name: "nil current mtimes - not stale",
|
|
storedMtimes: map[string]int64{"file.go": 1000},
|
|
currentMtimes: nil,
|
|
expectedStale: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
s.Run(tt.name, func() {
|
|
obs := &Observation{
|
|
FileMtimes: tt.storedMtimes,
|
|
}
|
|
result := obs.CheckStaleness(tt.currentMtimes)
|
|
s.Equal(tt.expectedStale, result)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestObservation_MarshalJSON tests JSON marshaling of Observation.
|
|
func (s *ObservationSuite) TestObservation_MarshalJSON() {
|
|
obs := &Observation{
|
|
ID: 1,
|
|
Project: "test-project",
|
|
Type: ObsTypeDiscovery,
|
|
Title: sql.NullString{String: "Test Title", Valid: true},
|
|
Scope: ScopeProject,
|
|
}
|
|
|
|
data, err := json.Marshal(obs)
|
|
s.NoError(err)
|
|
s.Contains(string(data), `"id":1`)
|
|
s.Contains(string(data), `"project":"test-project"`)
|
|
s.Contains(string(data), `"type":"discovery"`)
|
|
}
|
|
|
|
// TestParsedObservation_Fields tests ParsedObservation field access.
|
|
func (s *ObservationSuite) TestParsedObservation_Fields() {
|
|
obs := &ParsedObservation{
|
|
Type: ObsTypeFeature,
|
|
Title: "Add authentication",
|
|
Subtitle: "JWT-based auth",
|
|
Narrative: "Implemented JWT authentication for API endpoints",
|
|
Facts: []string{"Uses RS256 algorithm", "Tokens expire in 24h"},
|
|
Concepts: []string{"security", "auth"},
|
|
FilesRead: []string{"config.go"},
|
|
FilesModified: []string{"handler.go", "middleware.go"},
|
|
FileMtimes: map[string]int64{"handler.go": 1234567890},
|
|
}
|
|
|
|
s.Equal(ObsTypeFeature, obs.Type)
|
|
s.Equal("Add authentication", obs.Title)
|
|
s.Equal("JWT-based auth", obs.Subtitle)
|
|
s.Contains(obs.Narrative, "JWT")
|
|
s.Len(obs.Facts, 2)
|
|
s.Len(obs.Concepts, 2)
|
|
s.Len(obs.FilesRead, 1)
|
|
s.Len(obs.FilesModified, 2)
|
|
s.Len(obs.FileMtimes, 1)
|
|
}
|
|
|
|
// TestObservation_NullFields tests handling of nullable fields.
|
|
func (s *ObservationSuite) TestObservation_NullFields() {
|
|
// Test with null fields
|
|
obs := &Observation{
|
|
ID: 1,
|
|
Project: "test",
|
|
Type: ObsTypeDiscovery,
|
|
Title: sql.NullString{Valid: false},
|
|
Subtitle: sql.NullString{Valid: false},
|
|
Narrative: sql.NullString{Valid: false},
|
|
}
|
|
|
|
s.False(obs.Title.Valid)
|
|
s.False(obs.Subtitle.Valid)
|
|
s.False(obs.Narrative.Valid)
|
|
|
|
// Test with valid fields
|
|
obs2 := &Observation{
|
|
ID: 2,
|
|
Project: "test",
|
|
Type: ObsTypeBugfix,
|
|
Title: sql.NullString{String: "Fix bug", Valid: true},
|
|
Subtitle: sql.NullString{String: "Memory leak", Valid: true},
|
|
Narrative: sql.NullString{String: "Fixed memory leak in handler", Valid: true},
|
|
}
|
|
|
|
s.True(obs2.Title.Valid)
|
|
s.Equal("Fix bug", obs2.Title.String)
|
|
s.True(obs2.Subtitle.Valid)
|
|
s.Equal("Memory leak", obs2.Subtitle.String)
|
|
}
|
|
|
|
// TestNewObservation tests observation creation from parsed data.
|
|
func TestNewObservation(t *testing.T) {
|
|
parsed := &ParsedObservation{
|
|
Type: ObsTypeFeature,
|
|
Title: "Add authentication",
|
|
Subtitle: "JWT-based",
|
|
Narrative: "Implemented JWT auth",
|
|
Facts: []string{"Uses RS256"},
|
|
Concepts: []string{"security"},
|
|
FilesRead: []string{"config.go"},
|
|
FilesModified: []string{"handler.go"},
|
|
FileMtimes: map[string]int64{"handler.go": 1234567890},
|
|
}
|
|
|
|
obs := NewObservation("sdk-123", "test-project", parsed, 5, 1000)
|
|
|
|
assert.Equal(t, "sdk-123", obs.SDKSessionID)
|
|
assert.Equal(t, "test-project", obs.Project)
|
|
assert.Equal(t, ScopeGlobal, obs.Scope) // security triggers global
|
|
assert.Equal(t, ObsTypeFeature, obs.Type)
|
|
assert.Equal(t, "Add authentication", obs.Title.String)
|
|
assert.True(t, obs.Title.Valid)
|
|
assert.Equal(t, int64(5), obs.PromptNumber.Int64)
|
|
assert.Equal(t, int64(1000), obs.DiscoveryTokens)
|
|
assert.NotEmpty(t, obs.CreatedAt)
|
|
assert.Greater(t, obs.CreatedAtEpoch, int64(0))
|
|
}
|
|
|
|
// TestParsedObservation_ToStoredObservation tests conversion.
|
|
func TestParsedObservation_ToStoredObservation(t *testing.T) {
|
|
parsed := &ParsedObservation{
|
|
Type: ObsTypeDiscovery,
|
|
Title: "Test Title",
|
|
Subtitle: "Test Subtitle",
|
|
Narrative: "Test narrative",
|
|
Facts: []string{"Fact 1"},
|
|
Concepts: []string{"testing"},
|
|
}
|
|
|
|
obs := parsed.ToStoredObservation()
|
|
|
|
assert.Equal(t, ObsTypeDiscovery, obs.Type)
|
|
assert.Equal(t, "Test Title", obs.Title.String)
|
|
assert.True(t, obs.Title.Valid)
|
|
assert.Equal(t, "Test Subtitle", obs.Subtitle.String)
|
|
assert.True(t, obs.Subtitle.Valid)
|
|
}
|
|
|
|
// TestJSONStringArray tests JSONStringArray scanning.
|
|
func TestJSONStringArray(t *testing.T) {
|
|
tests := []struct {
|
|
input interface{}
|
|
name string
|
|
expected JSONStringArray
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "nil input",
|
|
input: nil,
|
|
wantErr: false,
|
|
expected: nil,
|
|
},
|
|
{
|
|
name: "empty string",
|
|
input: "",
|
|
wantErr: false,
|
|
expected: nil,
|
|
},
|
|
{
|
|
name: "json array string",
|
|
input: `["item1", "item2"]`,
|
|
wantErr: false,
|
|
expected: JSONStringArray{"item1", "item2"},
|
|
},
|
|
{
|
|
name: "json array bytes",
|
|
input: []byte(`["a", "b", "c"]`),
|
|
wantErr: false,
|
|
expected: JSONStringArray{"a", "b", "c"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var arr JSONStringArray
|
|
err := arr.Scan(tt.input)
|
|
if tt.wantErr {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.expected, arr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestJSONInt64Map tests JSONInt64Map scanning.
|
|
func TestJSONInt64Map(t *testing.T) {
|
|
tests := []struct {
|
|
input interface{}
|
|
expected JSONInt64Map
|
|
name string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "nil input",
|
|
input: nil,
|
|
wantErr: false,
|
|
expected: nil,
|
|
},
|
|
{
|
|
name: "empty string",
|
|
input: "",
|
|
wantErr: false,
|
|
expected: nil,
|
|
},
|
|
{
|
|
name: "json map string",
|
|
input: `{"file.go": 1234567890}`,
|
|
wantErr: false,
|
|
expected: JSONInt64Map{"file.go": 1234567890},
|
|
},
|
|
{
|
|
name: "json map bytes",
|
|
input: []byte(`{"a.go": 100, "b.go": 200}`),
|
|
wantErr: false,
|
|
expected: JSONInt64Map{"a.go": 100, "b.go": 200},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var m JSONInt64Map
|
|
err := m.Scan(tt.input)
|
|
if tt.wantErr {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.expected, m)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestObservation_JSONRoundTrip tests that observations can be marshaled and unmarshaled.
|
|
func TestObservation_JSONRoundTrip(t *testing.T) {
|
|
original := &Observation{
|
|
ID: 1,
|
|
SDKSessionID: "session-123",
|
|
Project: "test-project",
|
|
Type: ObsTypeDiscovery,
|
|
Title: sql.NullString{String: "Test Title", Valid: true},
|
|
Subtitle: sql.NullString{String: "Test Subtitle", Valid: true},
|
|
Narrative: sql.NullString{String: "Test narrative content", Valid: true},
|
|
Scope: ScopeProject,
|
|
CreatedAt: "2024-01-01T00:00:00Z",
|
|
CreatedAtEpoch: 1704067200000,
|
|
}
|
|
|
|
// Marshal
|
|
data, err := json.Marshal(original)
|
|
require.NoError(t, err)
|
|
|
|
// Unmarshal into map to check fields
|
|
var result map[string]interface{}
|
|
err = json.Unmarshal(data, &result)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, float64(1), result["id"])
|
|
assert.Equal(t, "test-project", result["project"])
|
|
assert.Equal(t, "discovery", result["type"])
|
|
assert.Equal(t, "Test Title", result["title"])
|
|
}
|