Documentation
¶
Overview ¶
caller_type.go — X-Caller-Type middleware (W20 Day2 Phase 1).
The middleware reads the X-Caller-Type request header and stashes a canonicalised value in the request context. Downstream handlers can call CallerTypeFromContext(r.Context()) to resolve the target collection name without re-parsing headers.
Valid header values: "user" | "agent-self" | "reflection". Unknown / missing → defaults to "user" (safe default — user collection is the most-restricted recall surface, never the privileged ones).
collections_admin.go — admin API skeleton for collection management (W20 Day2 Phase 1).
Endpoints:
POST /collections — register a new collection. Body: {"name":"...","ttl":"30d"}.
Returns 201 + collection info on success, 409 on duplicate,
400 on bad request.
GET /collections — list all registered collections.
Phase 1 scope: in-memory registry only. Phase 2+ will wire actual Qdrant collection lifecycle (create/drop) and persistence.
collections_memory.go — per-collection CRUD routing (W20 Day2 Phase 2).
Phase 2 scope: surface-level routing only. The handlers under /collections/{name}/memories validate that the URL-supplied collection matches the caller's resolved collection (per X-Caller-Type), and then delegate to the existing CRUD handlers. Underlying Qdrant storage is still single-collection — Phase 4 will wire per-collection Qdrant lifecycle and physical isolation.
This gives BMO + reflection-engine a stable REST surface to start migrating against (reflection writes go to /collections/engram_reflection/... with X-Caller-Type: reflection), and we can swap the storage backend later without breaking callers.
cross_search.go — POST /memories/cross-search (Phase 4: physical isolation).
Strict-mode cross-collection search. The request body MUST include a non-empty `collections` array — defaulting to "all collections" was rejected at Day1 review (BMO, 2026-05-05) because it makes recall surface implicit and hard to reason about (e.g. reflection insights silently leaking into user recall).
Each collection in the list must be registered. Unknown name → 400. The handler searches each physical Qdrant collection independently, merges results, re-sorts by score, and applies the limit. Dedup by ID is no longer needed since physical isolation guarantees the same ID cannot exist in multiple collections.
Package server — HTTP transport for Engram.
This file adds a standalone HTTP server with a /reflect endpoint that triggers the Reflection Engine on demand. It complements the MCP (stdio) transport without replacing it.
Endpoints:
POST /reflect — Run one Reflection Engine cycle.
Optional JSON body: {"dry_run": true}
Returns RunResult JSON.
GET /reflect/check — Check trigger conditions without running.
Returns CheckResult JSON.
GET /health — Liveness probe. Returns {"status":"ok"}.
GET /memories/expiry-candidates — List memories eligible for policy-based deletion.
Returns [{id, type, importance, age_days,
content_preview, tags}], capped at 50.
DELETE /memories/expired — Execute policy-based cleanup.
?dry_run=true (default) — report without deleting.
?dry_run=false&confirm=true — delete + snapshot.
Returns {deleted_count, skipped_count, snapshot_path}.
v0.2 CRUD routes:
POST /memories — Create a new memory. Returns 201 + Memory JSON.
GET /memories/{id} — Get a memory by ID. Returns Memory JSON.
PATCH /memories/{id} — Partial update. Forbids content field. Enforces FSM.
PUT /memories/{id} — Full replace (content re-embedded). Preserves lifecycle.
DELETE /memories/{id} — Soft delete: sets lifecycle_status=archived.
POST /memories/{id}/reset — Restore archived/deprecated → active.
POST /memories/search — Vector search. Body: {query, limit, include_archived}.
Package server implements MCP and REST API transports for Engram.
Index ¶
- Constants
- func CallerTypeFromContext(ctx context.Context) string
- func CallerTypeMiddleware(next http.Handler) http.Handler
- func CollectionFromContext(ctx context.Context) string
- func StartExpiryCleanup(ctx context.Context, store memory.Store, interval time.Duration)
- type ExpiryCandidate
- type HTTPServer
- type Server
Constants ¶
const DefaultExpiryInterval = 10 * time.Minute
DefaultExpiryInterval is the default period between expiry cleanup runs.
Variables ¶
This section is empty.
Functions ¶
func CallerTypeFromContext ¶ added in v0.2.0
CallerTypeFromContext returns the caller type stashed by the middleware. Defaults to "user" when missing.
func CallerTypeMiddleware ¶ added in v0.2.0
CallerTypeMiddleware injects the canonicalised X-Caller-Type into ctx.
func CollectionFromContext ¶ added in v0.2.0
CollectionFromContext is the convenience wrapper that maps the caller type directly to a collection name via the default registry.
func StartExpiryCleanup ¶
StartExpiryCleanup launches a background goroutine that periodically deletes expired memories (valid_until > 0 && valid_until < now) from the store.
The goroutine respects ctx cancellation and stops cleanly when ctx is done. interval controls how often cleanup runs (default: 10 minutes). Pass 0 to use DefaultExpiryInterval.
The first run happens after one full interval (not immediately at startup) to avoid competing with the startup sequence.
Types ¶
type ExpiryCandidate ¶ added in v0.2.0
type ExpiryCandidate struct {
ID string `json:"id"`
Type memory.MemoryType `json:"type"`
Importance float64 `json:"importance"`
AgeDays float64 `json:"age_days"`
ContentPreview string `json:"content_preview"`
Tags []string `json:"tags"`
}
ExpiryCandidate is a memory eligible for policy-based deletion.
type HTTPServer ¶ added in v0.2.0
type HTTPServer struct {
// contains filtered or unexported fields
}
HTTPServer wraps the Engram Server with an HTTP interface.
func NewHTTPServer ¶ added in v0.2.0
func NewHTTPServer(s *Server, port int, apiKey string) *HTTPServer
NewHTTPServer creates an HTTPServer bound to the given port. If apiKey is non-empty, every request must carry the header:
Authorization: Bearer <apiKey>
func (*HTTPServer) Handler ¶ added in v0.2.0
func (h *HTTPServer) Handler() http.Handler
Handler returns the underlying http.Handler for use with httptest.Server or any other HTTP server. This allows tests to create a test server without binding to a real TCP port via ListenAndServe.
func (*HTTPServer) ListenAndServe ¶ added in v0.2.0
func (h *HTTPServer) ListenAndServe(ctx context.Context) error
ListenAndServe starts the HTTP server. It blocks until ctx is cancelled.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server wraps the MCP server with Engram's memory operations.
func (*Server) GetMCPServer ¶
GetMCPServer returns the underlying mcp-go server for testing or advanced use.
func (*Server) PerCollectionStats ¶ added in v0.2.0
PerCollectionStats returns per-collection memory counts if the backing store supports it; returns nil otherwise.
func (*Server) ServeStdio ¶
ServeStdio starts the server using stdio transport (for MCP clients).
func (*Server) SetEmbedCache ¶ added in v0.2.0
func (s *Server) SetEmbedCache(c memory.EmbedCache)
SetEmbedCache registers a cache so its stats can be exposed via /metrics.
func (*Server) SetMetrics ¶ added in v0.2.0
func (s *Server) SetMetrics(m *engrammetrics.Metrics)
SetMetrics registers prometheus metrics so handlers can record latency.