memorylayer

package
v1.100.3 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package memorylayer assembles the memory layer behind one Handle: the Postgres-backed memory store, the embedding provider that powers vector search, the memory_manage / memory_capture toolkit (with its recall-first checker), the memory↔enrichment middleware adapter, and the background staleness watcher.

Construction takes explicit inputs — a *sql.DB, the shared semantic.Provider (nil disables the staleness watcher), and the resolved memory / embedding / staleness config values — so the subsystem is constructible and testable without a Platform. It imports pkg/memory, pkg/toolkits/memory, pkg/embedding, and pkg/middleware, never pkg/platform. The *sql.DB and the embedding provider back many other subsystems, so they stay owned by Platform: the *sql.DB is passed in, and the embedding provider is built here and handed back via EmbeddingProvider for Platform to store and pass on to the other owners.

Construction is two-phase: New builds the store + embedder + toolkit + adapter and constructs (but does not start) the staleness watcher, and Start launches the watcher goroutine. The split lets Platform register the toolkit between the two — preserving the original order (register, then start the watcher) so a registration failure leaves no detached goroutine running.

Toolkit registration stays a Platform/registry concern: New builds the toolkit and exposes it via Toolkit() for Platform to register into the shared toolkit registry. The only background resource this package owns for shutdown is the staleness watcher, stopped via Stop.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// ToolkitName is the memory toolkit instance name (the platform passes its
	// default).
	ToolkitName string
	// EmbeddingProvider selects the embedding backend: "ollama" builds the
	// Ollama provider from Ollama below; any other value selects the noop
	// provider (with the startup WARN). Reported verbatim in the enabled log.
	EmbeddingProvider string
	// Ollama configures the Ollama embedder; used only when EmbeddingProvider
	// is "ollama".
	Ollama embedding.OllamaConfig
	// StalenessEnabled gates the background staleness watcher; the watcher also
	// requires a non-nil semantic provider (see New).
	StalenessEnabled bool
	// Staleness tunes the watcher's interval and batch size; ignored when the
	// watcher is not started.
	Staleness memory.StalenessConfig
}

Config carries the resolved memory / embedding / staleness values the owner needs to assemble the layer. Platform translates its own config into this shape (and resolves the toolkit instance name) so this package stays free of the platform's config types and defaulting rules.

type Handle

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

Handle owns the assembled memory layer: the memory store, the embedding provider, the memory toolkit (and its recall-first checker), the memory↔enrichment adapter, and the background staleness watcher (its goroutine). The read accessors expose the store / embedder / toolkit / adapter that Platform surfaces through its MemoryStore() / EmbeddingProvider() accessors, registers into the shared toolkit registry, wires as the thread-linker + reflexive-capture target, reads in the knowledge insight adapter and search router, gates the indexqueue memory consumer on, and injects into the enrichment middleware. Stop is the shutdown seam Platform wires into stopBackgroundTrackers (the watcher must stop before Platform closes its *sql.DB).

func New

func New(db *sql.DB, semanticProvider semantic.Provider, cfg Config) (*Handle, error)

New assembles the memory store, embedder, toolkit (with its recall-first checker), enrichment adapter, and — when StalenessEnabled and a non-nil semantic provider is supplied — constructs the background staleness watcher. It does NOT start the watcher goroutine: the caller registers the toolkit first, then calls Start (so a registration failure leaves no goroutine running). It returns (nil, nil) when db is nil: the memory layer is a no-op without a database, matching the platform precondition, so a memory-disabled / no-DB deployment still gets the nil handle (and downstream nil/noop embedder behavior). It returns an error only when the memory toolkit fails to build.

func (*Handle) EmbeddingProvider

func (h *Handle) EmbeddingProvider() embedding.Provider

EmbeddingProvider returns the embedding provider the layer built, or nil on a nil Handle. Platform stores this as its own field and passes it to the other subsystems (portalstore, indexqueue, api-gateway, search/knowledge) that share the embedder.

func (*Handle) MemoryProvider

func (h *Handle) MemoryProvider() middleware.MemoryProvider

MemoryProvider returns the memory↔enrichment adapter for Platform to inject into the semantic-enrichment middleware config, or nil on a nil Handle.

func (*Handle) MemoryStore

func (h *Handle) MemoryStore() memory.Store

MemoryStore returns the memory store, or nil on a nil Handle (memory disabled or no database).

func (*Handle) Start

func (h *Handle) Start()

Start launches the background staleness-watcher goroutine (idempotent, and a no-op on a nil Handle or when the watcher was not constructed — staleness disabled or no semantic provider). The caller invokes this after registering the toolkit, so a failed registration never leaves a detached watcher running.

func (*Handle) Stop

func (h *Handle) Stop()

Stop stops the background staleness watcher and waits for its goroutine to exit. No-op on a nil Handle or when the watcher was never started (staleness disabled or no semantic provider). Platform calls this from stopBackgroundTrackers, before it closes the shared *sql.DB.

func (*Handle) Toolkit

func (h *Handle) Toolkit() *memorykit.Toolkit

Toolkit returns the memory toolkit for Platform to register into the shared toolkit registry and to wire as the thread-linker + reflexive-capture target, or nil on a nil Handle.

Jump to

Keyboard shortcuts

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