Skip to content
20 changes: 10 additions & 10 deletions sdk/go/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (c *Client) Confirm(ctx context.Context, ku KnowledgeUnit) (KnowledgeUnit,
}

if !ku.Tier.IsRemote() {
stored, err := c.store.Unit(ku.ID)
stored, err := c.store.Unit(ctx, ku.ID)
if err != nil {
return KnowledgeUnit{}, fmt.Errorf("reading knowledge unit: %w", err)
}
Expand All @@ -108,7 +108,7 @@ func (c *Client) Confirm(ctx context.Context, ku KnowledgeUnit) (KnowledgeUnit,
}

updated := applyConfirmation(*stored)
if err := c.store.Update(updated); err != nil {
if err := c.store.Update(ctx, updated); err != nil {
return KnowledgeUnit{}, fmt.Errorf("updating knowledge unit: %w", err)
}

Expand Down Expand Up @@ -145,7 +145,7 @@ func (c *Client) Drain(ctx context.Context) (DrainResult, error) {
return DrainResult{}, fmt.Errorf("no remote API configured")
}

units, err := c.store.All()
units, err := c.store.All(ctx)
if err != nil {
return DrainResult{}, fmt.Errorf("reading local units: %w", err)
}
Expand All @@ -169,7 +169,7 @@ func (c *Client) Drain(ctx context.Context) (DrainResult, error) {
continue
}

if err := c.store.Delete(ku.ID); err != nil {
if err := c.store.Delete(ctx, ku.ID); err != nil {
result.Warnings = append(result.Warnings, fmt.Errorf("deleting local %s: %w", ku.ID, err))

continue
Expand All @@ -189,7 +189,7 @@ func (c *Client) DrainableCount(ctx context.Context) (int, error) {
default:
}

units, err := c.store.All()
units, err := c.store.All(ctx)
if err != nil {
return 0, fmt.Errorf("reading local units: %w", err)
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func (c *Client) Flag(
}

if !ku.Tier.IsRemote() {
stored, err := c.store.Unit(ku.ID)
stored, err := c.store.Unit(ctx, ku.ID)
if err != nil {
return KnowledgeUnit{}, fmt.Errorf("reading knowledge unit: %w", err)
}
Expand All @@ -243,7 +243,7 @@ func (c *Client) Flag(
}

updated := applyFlag(*stored, reason, cfg)
if err := c.store.Update(updated); err != nil {
if err := c.store.Update(ctx, updated); err != nil {
return KnowledgeUnit{}, fmt.Errorf("updating knowledge unit: %w", err)
}

Expand Down Expand Up @@ -335,7 +335,7 @@ func (c *Client) Propose(ctx context.Context, params ProposeParams) (KnowledgeUn
ku.Evidence.FirstObserved = &now
ku.Evidence.LastConfirmed = &now

if err := c.store.Insert(ku); err != nil {
if err := c.store.Insert(ctx, ku); err != nil {
insertErr := fmt.Errorf("inserting knowledge unit: %w", err)
if remoteErr != nil {
return KnowledgeUnit{}, fmt.Errorf(
Expand Down Expand Up @@ -375,7 +375,7 @@ func (c *Client) Query(ctx context.Context, params QueryParams) (QueryResult, er
storeParams := params
storeParams.Limit = limit

storeResult, err := c.store.Query(storeParams)
storeResult, err := c.store.Query(ctx, storeParams)
if err != nil {
return QueryResult{}, fmt.Errorf("querying store: %w", err)
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func (c *Client) Status(ctx context.Context) (StoreStats, error) {
return StoreStats{}, err
}

stats, err := c.store.Stats(defaultRecentLimit)
stats, err := c.store.Stats(ctx, defaultRecentLimit)
if err != nil {
return StoreStats{}, fmt.Errorf("reading store stats: %w", err)
}
Expand Down
15 changes: 8 additions & 7 deletions sdk/go/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cq

import (
"cmp"
"context"
"errors"
"fmt"
"math"
Expand Down Expand Up @@ -50,32 +51,32 @@ var ErrStoreClosed = errors.New("store is closed")
// NOTE: implementations must be safe for concurrent use.
type Store interface {
// Unit returns the knowledge unit with the given ID, or nil when absent.
Unit(id string) (*KnowledgeUnit, error)
Unit(ctx context.Context, id string) (*KnowledgeUnit, error)

// All returns every knowledge unit in the store.
All() ([]KnowledgeUnit, error)
All(ctx context.Context) ([]KnowledgeUnit, error)

// Insert stores a new knowledge unit.
// NOTE: implementations must reject a duplicate ID and a unit whose
// domains are empty after normalization.
Insert(ku KnowledgeUnit) error
Insert(ctx context.Context, ku KnowledgeUnit) error

// Update replaces an existing knowledge unit.
// NOTE: implementations must error when the ID is absent and reject a
// unit whose domains are empty after normalization.
Update(ku KnowledgeUnit) error
Update(ctx context.Context, ku KnowledgeUnit) error

// Delete removes the knowledge unit with the given ID.
// NOTE: implementations must error when the ID is absent.
Delete(id string) error
Delete(ctx context.Context, id string) error

// Query returns knowledge units matching the parameters, ranked by
// relevance and confidence and truncated to the limit.
Query(params QueryParams) (StoreQueryResult, error)
Query(ctx context.Context, params QueryParams) (StoreQueryResult, error)

// Stats returns aggregated store statistics, including up to recentLimit
// most-recently-inserted units.
Stats(recentLimit int) (StoreStats, error)
Stats(ctx context.Context, recentLimit int) (StoreStats, error)

// Close releases the resources held by the store.
// NOTE: implementations must be safe to call more than once.
Expand Down
15 changes: 8 additions & 7 deletions sdk/go/store_memory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cq

import (
"context"
"errors"
"fmt"
"slices"
Expand All @@ -27,7 +28,7 @@ func NewInMemoryStore() Store {
}

// All returns every knowledge unit in insertion order.
func (s *inMemoryStore) All() ([]KnowledgeUnit, error) {
func (s *inMemoryStore) All(_ context.Context) ([]KnowledgeUnit, error) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
s.mu.Lock()
defer s.mu.Unlock()

Expand Down Expand Up @@ -55,7 +56,7 @@ func (s *inMemoryStore) Close() error {
}

// Delete removes a knowledge unit by ID.
func (s *inMemoryStore) Delete(id string) error {
func (s *inMemoryStore) Delete(_ context.Context, id string) error {
s.mu.Lock()
defer s.mu.Unlock()

Expand All @@ -76,7 +77,7 @@ func (s *inMemoryStore) Delete(id string) error {
}

// Insert stores a new knowledge unit. Error if ID exists or domains empty after normalization.
func (s *inMemoryStore) Insert(ku KnowledgeUnit) error {
func (s *inMemoryStore) Insert(_ context.Context, ku KnowledgeUnit) error {
s.mu.Lock()
defer s.mu.Unlock()

Expand Down Expand Up @@ -104,7 +105,7 @@ func (s *inMemoryStore) Insert(ku KnowledgeUnit) error {

// Query returns units matching any of the requested domains, ranked by the
// shared ranker. It does not run full-text search.
func (s *inMemoryStore) Query(params QueryParams) (StoreQueryResult, error) {
func (s *inMemoryStore) Query(_ context.Context, params QueryParams) (StoreQueryResult, error) {
norm, err := normalizeQueryParams(params)
if err != nil {
return StoreQueryResult{}, err
Expand Down Expand Up @@ -142,7 +143,7 @@ func (s *inMemoryStore) Query(params QueryParams) (StoreQueryResult, error) {
}

// Stats returns aggregated statistics, including up to recentLimit most-recently-inserted units.
func (s *inMemoryStore) Stats(recentLimit int) (StoreStats, error) {
func (s *inMemoryStore) Stats(_ context.Context, recentLimit int) (StoreStats, error) {
s.mu.Lock()
defer s.mu.Unlock()

Expand Down Expand Up @@ -190,7 +191,7 @@ func (s *inMemoryStore) Stats(recentLimit int) (StoreStats, error) {
}

// Unit retrieves a knowledge unit by ID. Returns nil, nil if not found.
func (s *inMemoryStore) Unit(id string) (*KnowledgeUnit, error) {
func (s *inMemoryStore) Unit(_ context.Context, id string) (*KnowledgeUnit, error) {
s.mu.Lock()
defer s.mu.Unlock()

Expand All @@ -209,7 +210,7 @@ func (s *inMemoryStore) Unit(id string) (*KnowledgeUnit, error) {
}

// Update replaces an existing knowledge unit.
func (s *inMemoryStore) Update(ku KnowledgeUnit) error {
func (s *inMemoryStore) Update(_ context.Context, ku KnowledgeUnit) error {
s.mu.Lock()
defer s.mu.Unlock()

Expand Down
11 changes: 7 additions & 4 deletions sdk/go/store_memory_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cq

import "testing"
import (
"context"
"testing"
)

// TestInMemoryStoreMatchesDomainsOnly verifies the in-memory store selects
// candidates by domain tag and does not consult full-text: a term that appears
Expand All @@ -21,11 +24,11 @@ func TestInMemoryStoreMatchesDomainsOnly(t *testing.T) {
Evidence: Evidence{Confidence: 0.7, Confirmations: 1},
Tier: Local,
}
if err := s.Insert(ku); err != nil {
if err := s.Insert(context.Background(), ku); err != nil {
t.Fatalf("Insert: %s", err)
}

got, err := s.Query(QueryParams{Domains: []string{"api"}, Limit: 5})
got, err := s.Query(context.Background(), QueryParams{Domains: []string{"api"}, Limit: 5})
if err != nil {
t.Fatalf("Query by domain: %s", err)
}
Expand All @@ -35,7 +38,7 @@ func TestInMemoryStoreMatchesDomainsOnly(t *testing.T) {

// "payments" appears only in the summary, never as a domain; the
// full-text-free store must not surface it.
none, err := s.Query(QueryParams{Domains: []string{"payments"}, Limit: 5})
none, err := s.Query(context.Background(), QueryParams{Domains: []string{"payments"}, Limit: 5})
if err != nil {
t.Fatalf("Query by summary term: %s", err)
}
Expand Down
Loading
Loading