Help us shape the future of Go! Take the 2026 Go Developer Survey

memory

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PriorityExplicitRelation = 0 // frontmatter related_ids, parent_id, source_ids
	PriorityExplicitLink     = 1 // wikilinks, markdown links, embeds
	PriorityMediumConfidence = 2 // inferred edges with medium confidence
	PriorityLowConfidence    = 3 // inferred edges below medium
)

Priority tier constants for edgePriority. Lower value = higher priority (closer to the target in the context-pack ranking).

The numeric values also appear in the distance-weighted ranking formula (distance*10 + priority), so the gaps between tiers encode the relative cost of "one edge further away" vs "one tier weaker confidence". Don't renumber without updating the distance formula.

Variables

This section is empty.

Functions

func EstimateTokens

func EstimateTokens(content string) int

EstimateTokens estimates the number of LLM tokens for the given content. Uses the heuristic of 4 characters per token with ceiling division.

func Excerpt added in v0.7.0

func Excerpt(body string, maxTokens int) string

Excerpt returns the most decision-relevant part of a note body, within maxTokens.

WHY THIS EXISTS. The packer includes bodies all-or-nothing (`if bodyTokens <= *remaining`), so a note larger than the leftover budget contributes nothing at all and the pack reports "3 items, 0 with bodies" — items present, content absent. Measured on the identity vault, whose median note is ~1,084 tokens against a 900-token hook budget, that is the common case rather than the edge case.

An excerpt always fits, so it turns "no body" into "the part that matters".

It prefers the Principle section because of how arcs are written: the first paragraph is the Trigger — story setup — and the rule an agent could act on is several sections down. Injecting the lead hands over the anecdote and withholds the lesson.

Types

type ContextItem

type ContextItem struct {
	ID           string                 `json:"id"`
	EdgeType     string                 `json:"edge_type"`
	Confidence   string                 `json:"confidence"`
	Frontmatter  map[string]interface{} `json:"frontmatter"`
	BodyIncluded bool                   `json:"body_included"`
	// BodyExcerpted marks Body as the note's most decision-relevant passage
	// rather than its whole text. It is recorded rather than inferred because
	// "the agent got the note" and "the agent got the gist" are different facts,
	// and a metric that cannot tell them apart reports the second as the first.
	BodyExcerpted bool   `json:"body_excerpted,omitempty"`
	Body          string `json:"body,omitempty"`
}

ContextItem holds context metadata for a single related note.

type ContextPackConfig

type ContextPackConfig struct {
	Input            string
	Budget           int                // token budget
	Depth            int                // BFS traversal depth; 0 or 1 = direct neighbors only (default)
	MaxItems         int                // max context items to return; 0 = unlimited (default, backward-compat)
	Slim             bool               // reduce context item frontmatter to {type, title, status} only
	ActivationScores map[string]float64 // optional activation scores keyed by note ID

	// ExcerptTokens caps a per-item excerpt used when a whole body will not fit
	// the remaining budget. 0 (default) keeps the released all-or-nothing
	// behaviour, where such an item contributes no text at all.
	//
	// Without this, a pack can honestly report "3 items" while handing the agent
	// nothing but titles — which is what a 900-token hook budget does against a
	// vault whose median note is larger than the budget.
	ExcerptTokens int
}

ContextPackConfig holds parameters for a ContextPack operation.

type ContextPackResult

type ContextPackResult struct {
	TargetID        string             `json:"target_id"`
	BudgetTokens    int                `json:"budget_tokens"`
	UsedTokens      int                `json:"used_tokens"`
	BudgetExhausted bool               `json:"budget_exhausted"`
	Truncated       bool               `json:"truncated"`
	Target          *ContextPackTarget `json:"target"`
	Context         []ContextItem      `json:"context"`
}

ContextPackResult is the full output of a ContextPack operation.

func ContextPack

func ContextPack(resolver *graph.Resolver, db *index.DB, cfg ContextPackConfig) (*ContextPackResult, error)

ContextPack resolves the input, loads the target note, and fills a token budget with the target body and related note frontmatter in priority order.

type ContextPackTarget

type ContextPackTarget struct {
	ID          string                 `json:"id"`
	Frontmatter map[string]interface{} `json:"frontmatter"`
	// BodyExcerpted marks Body as the note's decision-bearing passage rather
	// than its whole text — same meaning as on ContextItem, and needed here for
	// the same reason: a renderer that cannot tell the difference will truncate
	// an already-bounded excerpt a second time.
	BodyExcerpted bool   `json:"body_excerpted,omitempty"`
	Body          string `json:"body,omitempty"`
}

ContextPackTarget holds the fully-loaded target note.

type RecallConfig

type RecallConfig struct {
	Input         string
	Depth         int
	MinConfidence string
	MaxNodes      int
}

RecallConfig holds parameters for a Recall operation.

type RecallEdge

type RecallEdge struct {
	SourceID   string `json:"source_id"`
	TargetID   string `json:"target_id"`
	EdgeType   string `json:"edge_type"`
	Confidence string `json:"confidence"`
}

RecallEdge describes a directed edge between two nodes in the result.

type RecallNode

type RecallNode struct {
	ID             string                 `json:"id"`
	Type           string                 `json:"type"`
	Title          string                 `json:"title"`
	Distance       int                    `json:"distance"`
	EdgeFromParent *graph.TraverseEdge    `json:"edge_from_parent,omitempty"`
	Frontmatter    map[string]interface{} `json:"frontmatter"`
}

RecallNode is an enriched graph node returned by Recall.

type RecallResult

type RecallResult struct {
	TargetID        string       `json:"target_id"`
	Depth           int          `json:"depth"`
	MaxNodes        int          `json:"max_nodes"`
	MaxNodesReached bool         `json:"max_nodes_reached"`
	Nodes           []RecallNode `json:"nodes"`
	Edges           []RecallEdge `json:"edges"`
}

RecallResult is the full output of a Recall operation.

func Recall

func Recall(resolver *graph.Resolver, db *index.DB, cfg RecallConfig) (*RecallResult, error)

Recall resolves the input entity and returns an enriched subgraph neighbourhood. It resolves the input via the resolver, traverses the graph using BFS, and enriches each traversal node with type, title, and frontmatter from the database.

type RelatedConfig

type RelatedConfig struct {
	Input string
	Mode  string // "mixed", "explicit", or "inferred"
}

RelatedConfig holds parameters for a Related operation.

type RelatedItem

type RelatedItem struct {
	ID         string  `json:"id"`
	Type       string  `json:"type"`
	Title      string  `json:"title"`
	EdgeType   string  `json:"edge_type"`
	Confidence string  `json:"confidence"`
	Origin     string  `json:"origin"`
	Score      float64 `json:"score,omitempty"`
}

RelatedItem is a single related note returned by Related.

type RelatedResult

type RelatedResult struct {
	TargetID string        `json:"target_id"`
	Mode     string        `json:"mode"`
	Related  []RelatedItem `json:"related"`
}

RelatedResult is the full output of a Related operation.

func Related(resolver *graph.Resolver, db *index.DB, cfg RelatedConfig) (*RelatedResult, error)

Related resolves the input entity and returns directly connected notes, filtered by the given mode ("mixed", "explicit", or "inferred").

type SummarizeConfig

type SummarizeConfig struct {
	NoteIDs     []string
	IncludeBody bool
	MaxBodyLen  int // max chars per note body (0 = full)
}

SummarizeConfig controls which notes to load and how much body text to include.

type SummarizeResult

type SummarizeResult struct {
	Sources   []SummarizeSource `json:"sources"`
	NoteCount int               `json:"note_count"`
	NotFound  []string          `json:"not_found,omitempty"`
}

SummarizeResult is the assembled output for agent consumption.

func Summarize

func Summarize(db *index.DB, cfg SummarizeConfig) (*SummarizeResult, error)

Summarize loads frontmatter and optional body excerpts from the requested notes. It does not call an LLM — it assembles raw material for agent-driven synthesis.

type SummarizeSource

type SummarizeSource struct {
	ID          string                 `json:"id"`
	Type        string                 `json:"type"`
	Title       string                 `json:"title"`
	Frontmatter map[string]interface{} `json:"frontmatter"`
	BodyExcerpt string                 `json:"body_excerpt,omitempty"`
}

SummarizeSource holds assembled data for a single note.

Jump to

Keyboard shortcuts

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