|
| 1 | +// ABOUTME: end-to-end semantic search tests wiring a real internal/vector |
| 2 | +// ABOUTME: index into db.SearchContent, pinning anchor-local snippet centering. |
| 3 | +package db_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "go.kenn.io/agentsview/internal/db" |
| 14 | + "go.kenn.io/agentsview/internal/dbtest" |
| 15 | + "go.kenn.io/agentsview/internal/vector" |
| 16 | + kitvec "go.kenn.io/kit/vector" |
| 17 | +) |
| 18 | + |
| 19 | +// vectorIndexSearcher adapts a real *vector.Index to db.VectorSearcher for |
| 20 | +// tests, mirroring the production searcherAdapter in cmd/agentsview without |
| 21 | +// its staleness gate. |
| 22 | +type vectorIndexSearcher struct { |
| 23 | + ix *vector.Index |
| 24 | + enc kitvec.EncodeFunc |
| 25 | +} |
| 26 | + |
| 27 | +func (s vectorIndexSearcher) SemanticSearch( |
| 28 | + ctx context.Context, query string, limit int, |
| 29 | +) ([]db.VectorHit, error) { |
| 30 | + hits, err := s.ix.Search(ctx, s.enc, query, limit) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + out := make([]db.VectorHit, len(hits)) |
| 35 | + for i, h := range hits { |
| 36 | + out[i] = db.VectorHit{ |
| 37 | + SessionID: h.SessionID, |
| 38 | + Ordinal: h.Ordinal, |
| 39 | + OrdinalStart: h.OrdinalStart, |
| 40 | + OrdinalEnd: h.OrdinalEnd, |
| 41 | + Subordinate: h.Subordinate, |
| 42 | + Score: h.Score, |
| 43 | + Snippet: h.Snippet, |
| 44 | + } |
| 45 | + } |
| 46 | + return out, nil |
| 47 | +} |
| 48 | + |
| 49 | +// TestSearchContentSemanticCrossMemberChunkCentersOnAnchorMessage is the |
| 50 | +// end-to-end regression test for run-chunk snippet mislocation: a run whose |
| 51 | +// matched chunk spans two assistant messages must produce a ContentMatch |
| 52 | +// whose snippet centers on the ANCHOR message's content. Before the fix, the |
| 53 | +// vector layer returned the whole cross-member chunk as the snippet; the db |
| 54 | +// layer could not locate that text inside the anchor message's content and |
| 55 | +// fell back to centering on the query pattern (absent here), i.e. the start |
| 56 | +// of the message — losing the matched region entirely. |
| 57 | +func TestSearchContentSemanticCrossMemberChunkCentersOnAnchorMessage(t *testing.T) { |
| 58 | + ctx := context.Background() |
| 59 | + d := dbtest.OpenTestDB(t) |
| 60 | + |
| 61 | + memberA := "a short first assistant step" |
| 62 | + // The distinctive matched text sits past the snippet window's 60-byte |
| 63 | + // radius from the start of the anchor message, so a start-of-content |
| 64 | + // fallback cannot accidentally include it. |
| 65 | + memberB := strings.Repeat("background context sentence. ", 4) + |
| 66 | + "the particles remain entangled across any distance" |
| 67 | + msgs := []db.Message{ |
| 68 | + dbtest.UserMsg("s1", 0, "please explain the experiment results"), |
| 69 | + dbtest.AsstMsg("s1", 1, memberA), |
| 70 | + dbtest.AsstMsg("s1", 2, memberB), |
| 71 | + } |
| 72 | + dbtest.SeedSessionWithMessages(t, d, "s1", "proj", msgs, |
| 73 | + dbtest.WithMessageCounts(3, 2)) |
| 74 | + |
| 75 | + enc := func(_ context.Context, texts []string) ([][]float32, error) { |
| 76 | + out := make([][]float32, len(texts)) |
| 77 | + for i, text := range texts { |
| 78 | + if strings.Contains(text, "entangled") || strings.Contains(text, "quantum") { |
| 79 | + out[i] = []float32{1, 0, 0} |
| 80 | + } else { |
| 81 | + out[i] = []float32{0, 1, 0} |
| 82 | + } |
| 83 | + } |
| 84 | + return out, nil |
| 85 | + } |
| 86 | + |
| 87 | + ix, err := vector.Open(ctx, filepath.Join(t.TempDir(), "vectors.db"), false, 4000) |
| 88 | + require.NoError(t, err) |
| 89 | + defer func() { require.NoError(t, ix.Close()) }() |
| 90 | + gen := kitvec.Generation{Model: "fake-model", Dimensions: 3} |
| 91 | + _, err = ix.Build(ctx, d, enc, gen, vector.BuildOptions{}) |
| 92 | + require.NoError(t, err) |
| 93 | + |
| 94 | + d.SetVectorSearcher(vectorIndexSearcher{ix: ix, enc: enc}) |
| 95 | + |
| 96 | + // The query shares no literal token with the anchor message, so a |
| 97 | + // pattern-based fallback cannot rescue a mislocated snippet. |
| 98 | + page, err := d.SearchContent(ctx, db.ContentSearchFilter{ |
| 99 | + Pattern: "quantum superposition", Mode: "semantic", Limit: 10, |
| 100 | + }) |
| 101 | + require.NoError(t, err) |
| 102 | + require.NotEmpty(t, page.Matches) |
| 103 | + |
| 104 | + m := page.Matches[0] |
| 105 | + assert.Equal(t, "s1", m.SessionID) |
| 106 | + assert.Equal(t, 2, m.Ordinal, |
| 107 | + "anchor: the member containing the matched chunk's center") |
| 108 | + assert.Contains(t, m.Snippet, "entangled", |
| 109 | + "snippet must center on the anchor message's matched content") |
| 110 | + assert.NotContains(t, m.Snippet, memberA, |
| 111 | + "snippet must not carry text from a different run member") |
| 112 | +} |
0 commit comments