Files
archie/internal/domain/sampling/random.go
T
sam 6affe0eea3 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
2026-09-05 22:53:08 +10:00

33 lines
1002 B
Go

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
}