service

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConsolidateMode

type ConsolidateMode string

ConsolidateMode selects how the opt-in LLM consolidation pipeline runs.

const (
	// ConsolidateAsync stores writes immediately and consolidates in the
	// background — writes never block on the LLM. The default.
	ConsolidateAsync ConsolidateMode = "async"
	// ConsolidateSync consolidates before returning, so a write reflects its
	// dedup/supersede outcome immediately (read-your-consolidated-writes).
	ConsolidateSync ConsolidateMode = "sync"
	// ConsolidateOff disables consolidation even when a consolidator is set.
	ConsolidateOff ConsolidateMode = "off"
)

type ListInput

type ListInput struct {
	Namespace         string
	Tiers             []memory.Tier
	IncludeExpired    bool
	IncludeSuperseded bool
	// Limit caps the result count; <= 0 returns all matches.
	Limit int
}

ListInput selects a slice of a namespace's memories for browsing. The zero value (besides Namespace) lists all live memories, newest store order.

type Metrics

type Metrics interface {
	// ConsolidateResult records one consolidation outcome: one of
	// "gated", "new", "update", "supersede", "noop", "error", "dropped".
	ConsolidateResult(result string)
	// ConsolidateQueueDepth reports the current async queue depth.
	ConsolidateQueueDepth(depth int)
	// RememberResult records the outcome of a Remember call: result is
	// "ok"|"error" and tier is the memory's tier.
	RememberResult(result, tier string)
	// RecallResult records the outcome of a Recall call. result is
	// "ok"|"error"; tierFilter is one of "all"|"working"|"episodic"|
	// "semantic"|"procedural"|"mixed"; hitsBucket is a pre-bucketed
	// count of returned memories: "0"|"1"|"2-5"|"6-20"|"21+".
	RecallResult(result, tierFilter, hitsBucket string)
	// ForgetResult records the outcome of a Forget call: "ok"|"not_found"|"error".
	ForgetResult(result string)
	// PromoteResult records one Promote batch: result is "ok"|"error";
	// facts is the number of semantic facts written.
	PromoteResult(result string, facts int)
	// FsckResult records one fsck pass: "ok"|"error". Counters for the
	// work done (purged, evicted, duplicate groups) are exposed separately
	// via the store's maintenance metrics.
	FsckResult(result string)
	// OpDuration observes end-to-end latency for a public operation.
	OpDuration(op string, d time.Duration)
}

Metrics receives service-level events for observability. Methods must be safe for concurrent use; a nil Metrics is replaced by a no-op.

type Option

type Option func(*Service)

Option customizes a Service.

func WithClock

func WithClock(now func() time.Time) Option

WithClock overrides the time source (tests).

func WithConsolidateMinScore

func WithConsolidateMinScore(minScore float64) Option

WithConsolidateMinScore sets the similarity gate: the LLM is only consulted when the nearest candidate scores at least minScore. 0 disables the gate.

func WithConsolidateMode

func WithConsolidateMode(m ConsolidateMode) Option

WithConsolidateMode selects async (default), sync, or off.

func WithConsolidator

func WithConsolidator(c llm.Consolidator) Option

WithConsolidator enables the opt-in LLM consolidation pipeline.

func WithDistiller

func WithDistiller(d llm.Distiller) Option

WithDistiller enables episodic→semantic promotion via RunPromoter.

func WithIDGenerator

func WithIDGenerator(gen func() string) Option

WithIDGenerator overrides ID generation (tests).

func WithMetrics

func WithMetrics(m Metrics) Option

WithMetrics installs an observability sink for consolidation events.

func WithPromoteMinAccess

func WithPromoteMinAccess(n int) Option

WithPromoteMinAccess sets the minimum access_count for an episodic memory to be eligible for promotion.

func WithQueryPrefix

func WithQueryPrefix(p string) Option

WithQueryPrefix prepends an instruction to recall queries before embedding (e.g. the retrieval instruct expected by Qwen3-Embedding or bge models). Documents keep bare embeddings; the keyword leg keeps the raw query.

func WithRecallPool

func WithRecallPool(factor, floor int) Option

WithRecallPool overrides the per-leg candidate pool sizing (max(k*factor, floor)) for hybrid recall. Non-positive values keep the defaults. Used by the benchmark harness to sweep pool depth.

func WithScoreFusion

func WithScoreFusion(alpha float64) Option

WithScoreFusion sets the hybrid fusion weight: the vector leg by alpha and the keyword leg by 1-alpha (score fusion). alpha < 0 selects rank fusion (RRF). The package default is score fusion at DefaultFusionAlpha.

func WithShortTermCap

func WithShortTermCap(cap int) Option

WithShortTermCap bounds short-term memories per namespace, enforced by fsck.

func WithSyncReinforce

func WithSyncReinforce() Option

WithSyncReinforce makes recall reinforcement run synchronously (tests).

func WithWriteDedup

func WithWriteDedup(minScore float64) Option

WithWriteDedup coalesces a fresh write into an existing same-tier memory when their vector similarity is at or above minScore, instead of storing a near-duplicate. It only acts when the LLM consolidation pipeline is not handling the write (no consolidator, a non-durable tier, or consolidation off), giving headless deployments basic corpus hygiene. 0 disables it.

type RecallInput

type RecallInput struct {
	Namespace string
	Query     string
	Tiers     []memory.Tier
	Limit     int
	// IncludeExpired / IncludeSuperseded relax the default live-only filter.
	IncludeExpired    bool
	IncludeSuperseded bool
}

RecallInput describes a hybrid recall query.

type RememberInput

type RememberInput struct {
	Namespace  string
	Content    string
	Tier       memory.Tier
	Summary    string
	Tags       []string
	Metadata   map[string]any
	Importance float64
	// TTL overrides the tier default. A negative TTL means "never expire".
	TTL *time.Duration
	// ID upserts an existing memory when set; otherwise a new ID is generated.
	ID string
}

RememberInput describes a memory to store. Only Namespace and Content are required; Tier defaults to working and TTL to the tier default.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service wires storage and embeddings together. It is safe for concurrent use.

func New

func New(st store.Store, e embed.Embedder, opts ...Option) *Service

New builds a Service from a store and embedder.

func (*Service) Forget

func (s *Service) Forget(ctx context.Context, namespace, id string) error

Forget deletes a memory by ID.

func (*Service) Fsck

func (s *Service) Fsck(ctx context.Context) (maintenance.Report, error)

Fsck runs a consistency sweep: purge expired, enforce the short-term cap, and audit live memories for duplicate clusters.

func (*Service) Get

func (s *Service) Get(ctx context.Context, namespace, id string) (*memory.Memory, error)

Get returns a single memory by ID.

func (*Service) List

func (s *Service) List(ctx context.Context, in ListInput) ([]*memory.Memory, error)

List returns memories in a namespace matching the filter, without embeddings. It backs the UI memory browser and the client-derived relationship graph.

func (*Service) Namespaces

func (s *Service) Namespaces(ctx context.Context) ([]string, error)

Namespaces returns the distinct namespaces holding memories, for the UI tenant switcher.

func (*Service) Promote

func (s *Service) Promote(ctx context.Context) (int, error)

Promote distills frequently-accessed, not-yet-promoted episodic memories in each namespace into durable semantic facts (written via Remember so they get the similarity gate and consolidation dedup), then stamps the sources so they aren't reprocessed. Returns the number of facts written. No-op without a distiller.

func (*Service) Recall

func (s *Service) Recall(ctx context.Context, in RecallInput) ([]store.Scored, error)

Recall runs hybrid (vector + keyword) retrieval fused with RRF.

func (*Service) Remember

func (s *Service) Remember(ctx context.Context, in RememberInput) (*memory.Memory, error)

Remember embeds and stores a memory, returning the persisted record.

func (*Service) RunPromoter

func (s *Service) RunPromoter(ctx context.Context, interval time.Duration)

RunPromoter periodically distills frequently-accessed episodic memories into durable semantic facts until ctx is cancelled. It is a no-op without a distiller or a positive interval. Call once, typically in its own goroutine.

func (*Service) StartConsolidator

func (s *Service) StartConsolidator(ctx context.Context)

StartConsolidator runs the background consolidation worker until ctx is cancelled, then drains queued jobs within a bounded timeout. It is a no-op unless the service was built with a consolidator in async mode. Call once, typically in its own goroutine.

func (*Service) Stats

func (s *Service) Stats(ctx context.Context, namespace string) (Stats, error)

Stats computes a per-namespace overview by scanning all of its memories (including expired and superseded, so those can be counted separately).

type Stats

type Stats struct {
	Namespace     string              `json:"namespace"`
	Total         int                 `json:"total"`      // live memories (excludes expired/superseded)
	ByTier        map[memory.Tier]int `json:"by_tier"`    // live count per tier
	Expired       int                 `json:"expired"`    // past-TTL, not yet swept
	Superseded    int                 `json:"superseded"` // contradiction-tombstoned
	TotalAccesses int                 `json:"total_accesses"`
	AvgImportance float64             `json:"avg_importance"`
	LastWriteAt   *time.Time          `json:"last_write_at,omitempty"`
}

Stats summarizes a namespace for the UI dashboard. Counts are computed from a full listing, so callers should treat it as a curated-namespace overview, not a hot-path metric (Prometheus /metrics remains the source for operational counters).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL