chore(curator): add embedding client and sampler interface prerequisites (#752)
* feat(curator): add embedding client and sampler wave-1 prerequisites Bead archie-core-1786637500752-415-434e5bb9 (GitHub #436) was closed with no linked PR/commits, and the surprisal-sampling bead archie-core-1786637500776-416-600a66e5 (GitHub #437) found none of its stated prerequisites -- an embedding client, a Sampler interface, or the four wave-1 cheap sampling strategies -- actually exist. This builds them: - internal/domain/embedding + internal/infrastructure/embedding: a narrow typed embedding client, config-driven via a providers table + a role in [models], mirroring the existing chat model role convention. Degrades to capability-unavailable (never fails startup or a call) for a missing role, unknown provider, unsupported class, or unresolved credential. Wired as an optional capability in bootstrap.go's setupEmbeddings. - internal/domain/sampling: the Sampler interface plus recency, random, all, and staleness-proxy strategies, per docs/prds/curator-sampler-wave1.md. Deterministic given fixed inputs, matching GitHub #437's stated acceptance criterion for the embedding-backed strategy it will add behind this same interface (not part of this change). Table-driven tests throughout; httptest for the embedding client, no live service. No curator wiring yet -- nothing consumes either capability in this change. * no-mistakes(document): docs(architecture): mark curator sampling prereqs as landed
This commit is contained in:
1 parent
cd2ab3516b
commit
6affe0eea3
18 files changed
+1186
-21
No files matched your search
@@ -0,0 +1,19 @@
|
||||
package sampling
|
||||
|
||||
import "context"
|
||||
|
||||
// allSampler is the "all" strategy: no filtering, returns every candidate
|
||||
// in input order, truncated to Request.Cap.
|
||||
type allSampler struct{}
|
||||
|
||||
// NewAll builds the "all" strategy.
|
||||
func NewAll() Sampler { return allSampler{} }
|
||||
|
||||
func (allSampler) Name() string { return "all" }
|
||||
|
||||
func (allSampler) Sample(_ context.Context, candidates []Candidate, req Request) ([]Candidate, error) {
|
||||
n := effectiveCap(req.Cap, len(candidates))
|
||||
out := make([]Candidate, n)
|
||||
copy(out, candidates[:n])
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAllSampler(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
candidates := []Candidate{
|
||||
{ID: "a", At: base},
|
||||
{ID: "b", At: base.Add(time.Hour)},
|
||||
{ID: "c", At: base.Add(2 * time.Hour)},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cap int
|
||||
wantIDs []string
|
||||
}{
|
||||
{name: "no cap returns everything in input order", cap: 0, wantIDs: []string{"a", "b", "c"}},
|
||||
{name: "cap truncates from the front", cap: 2, wantIDs: []string{"a", "b"}},
|
||||
{name: "cap larger than candidate count returns everything", cap: 100, wantIDs: []string{"a", "b", "c"}},
|
||||
{name: "negative cap means unlimited", cap: -1, wantIDs: []string{"a", "b", "c"}},
|
||||
}
|
||||
|
||||
s := NewAll()
|
||||
if s.Name() != "all" {
|
||||
t.Fatalf("Name() = %q, want %q", s.Name(), "all")
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := s.Sample(context.Background(), candidates, Request{Cap: tt.cap})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
assertIDs(t, got, tt.wantIDs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllSamplerEmptyInput(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := NewAll().Sample(context.Background(), nil, Request{})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("Sample() = %v, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllSamplerDoesNotMutateInput(t *testing.T) {
|
||||
t.Parallel()
|
||||
assertInputNotMutated(t, NewAll())
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Package sampling is the curator sampler family: given a bounded set of
|
||||
// candidates, pick a subset to spend a curator pass's agentic budget on.
|
||||
// See docs/prds/curator-sampler-wave1.md for the design and the four
|
||||
// wave-1 strategies (recency, random, all, staleness) this package ships.
|
||||
// The embedding-backed surprisal strategy (GitHub #437) is a separate,
|
||||
// deferred consumer of this same interface -- it does not live here.
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Candidate is one item eligible for sampling. It is deliberately narrow:
|
||||
// an opaque identifier, the timestamp the timestamp-based strategies score
|
||||
// against, and an open Metadata bag for whatever a future strategy needs
|
||||
// (e.g. the embedding-backed surprisal strategy's vector) without widening
|
||||
// this struct for every strategy that comes after wave 1.
|
||||
type Candidate struct {
|
||||
ID string
|
||||
At time.Time
|
||||
Metadata map[string]any
|
||||
}
|
||||
|
||||
// Request carries the knobs every strategy shares: a selection cap and a
|
||||
// deterministic seed. Both are optional -- Cap 0 (or negative) means
|
||||
// unlimited (the "all" strategy relies on this), Seed 0 is a valid seed,
|
||||
// not "unset".
|
||||
type Request struct {
|
||||
Cap int
|
||||
Seed int64
|
||||
}
|
||||
|
||||
// Sampler selects a subset of candidates to spend a curator pass's agentic
|
||||
// budget on. Every implementation -- the four in this package and the
|
||||
// future embedding-backed surprisal strategy (GitHub #437) -- must be
|
||||
// deterministic given the same candidates and Request: same inputs, same
|
||||
// output, every time. That is what makes a pass reproducible and testable.
|
||||
type Sampler interface {
|
||||
// Name identifies the strategy for logging/attribution (e.g.
|
||||
// "recency", "random", "all", "staleness", later "surprisal").
|
||||
Name() string
|
||||
// Sample selects from candidates per req. Implementations must not
|
||||
// mutate candidates or rely on anything but candidates and req --
|
||||
// determinism depends on that.
|
||||
Sample(ctx context.Context, candidates []Candidate, req Request) ([]Candidate, error)
|
||||
}
|
||||
|
||||
// effectiveCap resolves req.Cap against n candidates: zero, negative, or
|
||||
// larger than n means unlimited (every candidate); otherwise req.Cap.
|
||||
func effectiveCap(reqCap, n int) int {
|
||||
if reqCap <= 0 || reqCap > n {
|
||||
return n
|
||||
}
|
||||
return reqCap
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/rand/v2"
|
||||
)
|
||||
|
||||
// randomSampler is the "random" strategy: a uniform random subset, seeded
|
||||
// per call from Request.Seed so the same (candidates, req) pair always
|
||||
// produces the same selection -- a package-level or struct-level generator
|
||||
// would not give that guarantee under concurrent or repeated calls.
|
||||
type randomSampler struct{}
|
||||
|
||||
// NewRandom builds the "random" strategy.
|
||||
func NewRandom() Sampler { return randomSampler{} }
|
||||
|
||||
func (randomSampler) Name() string { return "random" }
|
||||
|
||||
func (randomSampler) Sample(_ context.Context, candidates []Candidate, req Request) ([]Candidate, error) {
|
||||
if len(candidates) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
n := effectiveCap(req.Cap, len(candidates))
|
||||
seed := uint64(req.Seed) //nolint:gosec // deterministic seeding, not a security boundary
|
||||
rng := rand.New(rand.NewPCG(seed, seed))
|
||||
order := rng.Perm(len(candidates))
|
||||
out := make([]Candidate, n)
|
||||
for i := range n {
|
||||
out[i] = candidates[order[i]]
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func candidatesFor(n int) []Candidate {
|
||||
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
out := make([]Candidate, n)
|
||||
for i := range out {
|
||||
out[i] = Candidate{ID: string(rune('a' + i)), At: base.Add(time.Duration(i) * time.Hour)}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestRandomSamplerName(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := NewRandom().Name(); got != "random" {
|
||||
t.Fatalf("Name() = %q, want %q", got, "random")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomSamplerCap(t *testing.T) {
|
||||
t.Parallel()
|
||||
candidates := candidatesFor(10)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cap int
|
||||
wantLen int
|
||||
}{
|
||||
{name: "no cap returns every candidate", cap: 0, wantLen: 10},
|
||||
{name: "cap smaller than count", cap: 3, wantLen: 3},
|
||||
{name: "cap larger than count returns every candidate", cap: 100, wantLen: 10},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := NewRandom().Sample(context.Background(), candidates, Request{Cap: tt.cap, Seed: 1})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
if len(got) != tt.wantLen {
|
||||
t.Fatalf("len(Sample()) = %d, want %d", len(got), tt.wantLen)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomSamplerSelectionIsASubsetOfInput(t *testing.T) {
|
||||
t.Parallel()
|
||||
candidates := candidatesFor(10)
|
||||
got, err := NewRandom().Sample(context.Background(), candidates, Request{Cap: 5, Seed: 42})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
|
||||
valid := make(map[string]bool, len(candidates))
|
||||
for _, c := range candidates {
|
||||
valid[c.ID] = true
|
||||
}
|
||||
seen := make(map[string]bool, len(got))
|
||||
for _, c := range got {
|
||||
if !valid[c.ID] {
|
||||
t.Fatalf("Sample() returned candidate %q not present in input", c.ID)
|
||||
}
|
||||
if seen[c.ID] {
|
||||
t.Fatalf("Sample() returned candidate %q more than once", c.ID)
|
||||
}
|
||||
seen[c.ID] = true
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomSamplerDeterministicForSameSeed(t *testing.T) {
|
||||
t.Parallel()
|
||||
candidates := candidatesFor(20)
|
||||
s := NewRandom()
|
||||
|
||||
first, err := s.Sample(context.Background(), candidates, Request{Cap: 8, Seed: 99})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
second, err := s.Sample(context.Background(), candidates, Request{Cap: 8, Seed: 99})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
|
||||
wantIDs := make([]string, len(first))
|
||||
for i, c := range first {
|
||||
wantIDs[i] = c.ID
|
||||
}
|
||||
assertIDs(t, second, wantIDs)
|
||||
}
|
||||
|
||||
func TestRandomSamplerDifferentSeedsCanDiffer(t *testing.T) {
|
||||
t.Parallel()
|
||||
candidates := candidatesFor(20)
|
||||
s := NewRandom()
|
||||
|
||||
a, err := s.Sample(context.Background(), candidates, Request{Cap: 8, Seed: 1})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
b, err := s.Sample(context.Background(), candidates, Request{Cap: 8, Seed: 2})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
|
||||
same := len(a) == len(b)
|
||||
if same {
|
||||
for i := range a {
|
||||
if a[i].ID != b[i].ID {
|
||||
same = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if same {
|
||||
t.Fatalf("Sample() with different seeds produced identical output: %v", a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomSamplerEmptyInput(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := NewRandom().Sample(context.Background(), nil, Request{Seed: 1})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("Sample() = %v, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomSamplerDoesNotMutateInput(t *testing.T) {
|
||||
t.Parallel()
|
||||
assertInputNotMutated(t, NewRandom())
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// recencySampler is the "recency" strategy: prefers more recently
|
||||
// created/updated content, i.e. the largest Candidate.At first.
|
||||
type recencySampler struct{}
|
||||
|
||||
// NewRecency builds the "recency" strategy.
|
||||
func NewRecency() Sampler { return recencySampler{} }
|
||||
|
||||
func (recencySampler) Name() string { return "recency" }
|
||||
|
||||
func (recencySampler) Sample(_ context.Context, candidates []Candidate, req Request) ([]Candidate, error) {
|
||||
sorted := sortByTime(candidates, true)
|
||||
return sorted[:effectiveCap(req.Cap, len(sorted))], nil
|
||||
}
|
||||
|
||||
// sortByTime returns a stably-sorted copy of candidates, never mutating the
|
||||
// input. newestFirst true sorts descending by At (recency); false sorts
|
||||
// ascending (staleness). A stable sort keeps ties in input order, which is
|
||||
// what makes both strategies deterministic when candidates share a
|
||||
// timestamp.
|
||||
func sortByTime(candidates []Candidate, newestFirst bool) []Candidate {
|
||||
sorted := append([]Candidate(nil), candidates...)
|
||||
slices.SortStableFunc(sorted, func(a, b Candidate) int {
|
||||
if newestFirst {
|
||||
return b.At.Compare(a.At)
|
||||
}
|
||||
return a.At.Compare(b.At)
|
||||
})
|
||||
return sorted
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRecencySampler(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
candidates := []Candidate{
|
||||
{ID: "oldest", At: base},
|
||||
{ID: "middle", At: base.Add(time.Hour)},
|
||||
{ID: "newest", At: base.Add(2 * time.Hour)},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cap int
|
||||
wantIDs []string
|
||||
}{
|
||||
{name: "no cap orders newest first", cap: 0, wantIDs: []string{"newest", "middle", "oldest"}},
|
||||
{name: "cap keeps only the most recent", cap: 2, wantIDs: []string{"newest", "middle"}},
|
||||
{name: "cap larger than count returns everything", cap: 100, wantIDs: []string{"newest", "middle", "oldest"}},
|
||||
}
|
||||
|
||||
s := NewRecency()
|
||||
if s.Name() != "recency" {
|
||||
t.Fatalf("Name() = %q, want %q", s.Name(), "recency")
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := s.Sample(context.Background(), candidates, Request{Cap: tt.cap})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
assertIDs(t, got, tt.wantIDs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecencySamplerTiesKeepInputOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
candidates := []Candidate{
|
||||
{ID: "first", At: at},
|
||||
{ID: "second", At: at},
|
||||
{ID: "third", At: at},
|
||||
}
|
||||
got, err := NewRecency().Sample(context.Background(), candidates, Request{})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
assertIDs(t, got, []string{"first", "second", "third"})
|
||||
}
|
||||
|
||||
func TestRecencySamplerDoesNotMutateInput(t *testing.T) {
|
||||
t.Parallel()
|
||||
assertInputNotMutated(t, NewRecency())
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// assertIDs checks got's IDs match want, in order.
|
||||
func assertIDs(t *testing.T, got []Candidate, want []string) {
|
||||
t.Helper()
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("got %d candidates, want %d (%v)", len(got), len(want), want)
|
||||
}
|
||||
for i, c := range got {
|
||||
if c.ID != want[i] {
|
||||
t.Fatalf("candidate[%d].ID = %q, want %q", i, c.ID, want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// assertInputNotMutated builds a candidate slice, samples it through s,
|
||||
// then verifies the original slice's order and values are untouched --
|
||||
// the non-mutation guarantee every Sampler implementation must uphold.
|
||||
func assertInputNotMutated(t *testing.T, s Sampler) {
|
||||
t.Helper()
|
||||
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
candidates := []Candidate{
|
||||
{ID: "a", At: base.Add(3 * time.Hour)},
|
||||
{ID: "b", At: base.Add(time.Hour)},
|
||||
{ID: "c", At: base.Add(2 * time.Hour)},
|
||||
{ID: "d", At: base},
|
||||
}
|
||||
original := append([]Candidate(nil), candidates...)
|
||||
|
||||
if _, err := s.Sample(context.Background(), candidates, Request{Cap: 2, Seed: 7}); err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
|
||||
if len(candidates) != len(original) {
|
||||
t.Fatalf("input slice length changed: got %d, want %d", len(candidates), len(original))
|
||||
}
|
||||
for i := range candidates {
|
||||
if candidates[i].ID != original[i].ID || !candidates[i].At.Equal(original[i].At) {
|
||||
t.Fatalf("input slice mutated at index %d: got %+v, want %+v", i, candidates[i], original[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package sampling
|
||||
|
||||
import "context"
|
||||
|
||||
// stalenessSampler is the "staleness proxy" strategy: prefers content that
|
||||
// hasn't been touched/reviewed in the longest time, i.e. the smallest
|
||||
// Candidate.At first. It is a cheap proxy only -- callers decide what
|
||||
// timestamp At holds (created, updated, or last-reviewed); this is never
|
||||
// the embedding-based surprisal calculation (see GitHub #437).
|
||||
type stalenessSampler struct{}
|
||||
|
||||
// NewStaleness builds the "staleness" strategy.
|
||||
func NewStaleness() Sampler { return stalenessSampler{} }
|
||||
|
||||
func (stalenessSampler) Name() string { return "staleness" }
|
||||
|
||||
func (stalenessSampler) Sample(_ context.Context, candidates []Candidate, req Request) ([]Candidate, error) {
|
||||
sorted := sortByTime(candidates, false)
|
||||
return sorted[:effectiveCap(req.Cap, len(sorted))], nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package sampling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestStalenessSampler(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
candidates := []Candidate{
|
||||
{ID: "newest", At: base.Add(2 * time.Hour)},
|
||||
{ID: "middle", At: base.Add(time.Hour)},
|
||||
{ID: "oldest", At: base},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cap int
|
||||
wantIDs []string
|
||||
}{
|
||||
{name: "no cap orders oldest first", cap: 0, wantIDs: []string{"oldest", "middle", "newest"}},
|
||||
{name: "cap keeps only the stalest", cap: 2, wantIDs: []string{"oldest", "middle"}},
|
||||
{name: "cap larger than count returns everything", cap: 100, wantIDs: []string{"oldest", "middle", "newest"}},
|
||||
}
|
||||
|
||||
s := NewStaleness()
|
||||
if s.Name() != "staleness" {
|
||||
t.Fatalf("Name() = %q, want %q", s.Name(), "staleness")
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := s.Sample(context.Background(), candidates, Request{Cap: tt.cap})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
assertIDs(t, got, tt.wantIDs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStalenessSamplerTiesKeepInputOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
candidates := []Candidate{
|
||||
{ID: "first", At: at},
|
||||
{ID: "second", At: at},
|
||||
{ID: "third", At: at},
|
||||
}
|
||||
got, err := NewStaleness().Sample(context.Background(), candidates, Request{})
|
||||
if err != nil {
|
||||
t.Fatalf("Sample() error = %v", err)
|
||||
}
|
||||
assertIDs(t, got, []string{"first", "second", "third"})
|
||||
}
|
||||
|
||||
func TestStalenessSamplerDoesNotMutateInput(t *testing.T) {
|
||||
t.Parallel()
|
||||
assertInputNotMutated(t, NewStaleness())
|
||||
}
|
||||
Reference in new issue
Block a user