github.com/GustyCube/membrane/pkg/retrieval
The retrieval package implements layered memory retrieval per RFC 15.8. Records are queried in canonical layer order (working → semantic → competence → plan_graph → episodic), filtered by trust context and salience, optionally ranked by a multi-solution selector, and returned sorted by salience descending.
All retrieval operations require a non-nil
TrustContext. Passing nil returns ErrNilTrust.Service
NewServiceWithEmbedding to enable semantic similarity scoring at retrieval time. Membrane.New wires the correct constructor automatically based on the Config.
EmbeddingService interface
Retrieve
competence and plan_graph candidates through the Selector, then sorts and limits the result set.
RetrieveRequest
Natural-language description of the current task. When an embedding service is configured, this is encoded into a query vector used for applicability scoring in the
Selector.Trust context that gates which records can be returned. Must not be
nil.Restricts retrieval to the specified memory types. When empty, all types are queried in canonical layer order.
Records with
Salience below this threshold are excluded. 0 disables the filter.Maximum number of records to return.
0 means no limit.RetrieveResponse
Filtered, sorted, and limited result set.
Non-nil when
competence or plan_graph candidates were evaluated through the multi-solution selector. nil if no such candidates were found.RetrieveByID
storage.ErrNotFound if the record does not exist, or ErrAccessDenied if the trust context denies access.
Unique ID of the record to fetch.
Trust context for access control.
TrustContext
- Its
Sensitivitylevel does not exceedMaxSensitivity. - Its
Scopematches one of the listedScopes(or the record has no scope, orScopesis empty).
Maximum sensitivity level the requester is allowed to access.
Whether the requester has been authenticated.
Identifies who is making the retrieval request.
Scopes the requester is allowed to access. An empty slice grants access to all scopes.
Methods
AllowsRedacted returns true when the record’s sensitivity is exactly one level above MaxSensitivity, enabling a redacted metadata view per RFC Section 13.
Selector
competence and plan_graph candidates using three equally-weighted signals:
- Applicability — the record’s
Confidencefield (or embedding similarity when anEmbeddingProvideris configured). - Observed success rate — derived from
PerformanceStats(competence) orPlanMetrics.FailureRate(plan graph). Defaults to0.5when no data is available. - Recency — exponential decay with a 30-day half-life from
Lifecycle.LastReinforcedAt.
EmbeddingProvider interface
SelectionResult
Candidates ranked in descending composite score order.
Selection confidence in
[0, 1]. Computed as the normalised score gap between the best and second-best candidate.true when Confidence falls below the selector’s threshold, indicating disambiguation is needed.Composite score for each candidate record ID.
RetrieveByType
The memory type to query:
episodic, working, semantic, competence, or plan_graph.Trust context for access control. Must not be
nil.RetrieveByType is a lower-level convenience method on Service. The top-level Membrane facade does not expose it directly — use Retrieve with MemoryTypes set for equivalent behaviour through the public API.Errors
| Symbol | Description |
|---|---|
ErrAccessDenied | Returned by RetrieveByID when the trust context denies access to the record. |
ErrNilTrust | Returned by any operation when a nil TrustContext is provided. |