contextpack

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package contextpack builds the bounded context MemSpace hands to agents: at session start, a card for recent unfinished work in the project; on each prompt, history related to what the user asked.

Everything is wrapped in <memspace_context> with an explicit statement that it is historical, possibly stale, and not instructions, and fitted to a character budget by dropping whole items, lowest priority first (Claude Code replaces hook output over 10,000 characters with a stub, so overflowing is worse than omitting).

Index

Constants

View Source
const (
	ModeSessionStart = "session_start"
	ModePrompt       = "prompt"
	ModeExplicit     = "explicit" // MCP memspace_project_context
)

Modes.

View Source
const (
	SessionStartBudget = 4000
	PromptBudget       = 6000
	ExplicitBudget     = 8000
	// HardLimit stays below Claude Code's 10,000-character hook limit.
	HardLimit = 9500
)

Default character budgets (~1000 / ~2000 tokens).

View Source
const (
	// Prompt hooks have 1.5s total. Leave time for the 1s embedding timeout,
	// database hydration and rendering even when lexical retrieval is slow.
	PromptKeywordTimeout = 300 * time.Millisecond
	// Bound only the synchronous semantic query. The source text and full
	// lexical query remain intact. A character bound also covers long IDs that
	// the token estimator considers one word.
	PromptSemanticTokens = 128
	PromptSemanticChars  = 512
)

Variables

View Source
var RecentWindow = 72 * time.Hour

RecentWindow bounds how old an unfinished session may be to be offered at session start.

Functions

This section is empty.

Types

type BuildOptions added in v0.1.4

type BuildOptions struct{ MaxItems int }

BuildOptions bounds the number of visible items without shrinking retrieval depth. Zero preserves existing behavior. This is also used by eval adapters to honor top_k without slicing already-rendered source text.

type EvidenceGroup added in v0.1.4

type EvidenceGroup struct {
	ID       string           `json:"group_id"`
	Members  []EvidenceMember `json:"members"`
	Links    []evidence.Link  `json:"links"`
	Complete bool             `json:"complete"`
}

EvidenceGroup describes only the sources actually delivered. Complete means all required relationship endpoints were delivered in full, not that they prove an answer or exhaust the project's evidence.

type EvidenceMember added in v0.1.4

type EvidenceMember struct {
	ObjectID     string           `json:"object_id"`
	Ref          string           `json:"ref"`
	Revision     string           `json:"revision_sha"`
	Availability string           `json:"availability"`
	Partial      bool             `json:"partial"`
	Spans        []*evidence.Span `json:"required_spans,omitempty"`
}

type EvidenceRecord added in v0.1.4

type EvidenceRecord struct {
	Hit   model.SearchResult `json:"hit"`
	Body  string             `json:"body"`
	Chunk string             `json:"chunk,omitempty"`
}

EvidenceRecord is an owner/project-scoped source record, not a generated summary. Chunk is the search anchor's real matching chunk when available.

func DialogueWindow added in v0.1.4

func DialogueWindow(ctx context.Context, db *sql.DB, owner, project string, hit model.SearchResult) ([]EvidenceRecord, error)

DialogueWindow supplies at most one preceding and one following message around an anchor. These are diagnostic building blocks; ordinary Build does not expand its candidates or change qualification. Scope and provenance are re-read from the source table, never borrowed from a neighbouring session.

type Item

type Item struct {
	Key       string    `json:"key"`
	Kind      string    `json:"kind"` // session | history
	Ref       string    `json:"ref"`
	ProjectID string    `json:"project_id"`
	Device    string    `json:"device,omitempty"`
	Agent     string    `json:"agent,omitempty"`
	At        time.Time `json:"at"`
}

Item is one delivered piece, for provenance and de-duplication.

type Observer added in v0.1.4

type Observer func(TraceEvent)

Observer is per-call and synchronous. Instrumented runs are quality diagnostics, not measurements of uninstrumented production latency.

type Pack

type Pack struct {
	EvidenceGroups []EvidenceGroup `json:"evidence_groups,omitempty"`
	Text           string          `json:"text"`
	Items          []Item          `json:"items"`
	Truncated      bool            `json:"truncated"`
	ProjectID      string          `json:"project_id,omitempty"`
	Note           string          `json:"note,omitempty"`
}

Pack is the result. Text is empty when there is nothing worth injecting.

func Build

func Build(ctx context.Context, db *sql.DB, owner, deviceID string, req Request) (*Pack, error)

Build assembles a pack for the caller (owner, device).

func BuildFromCandidates added in v0.1.4

func BuildFromCandidates(ctx context.Context, db *sql.DB, owner, deviceID string, req Request, hits []model.SearchResult, observer Observer) (*Pack, error)

BuildFromCandidates is for trusted local quality experiments. It skips search and reuses a snapshot from the same query/index, including EvidenceText. Project/owner hydration, current-session exclusion, qualification, content validation and rendering still run. No HTTP/MCP endpoint accepts snapshots. Its elapsed time is not end-to-end search latency.

func BuildFromEvidence added in v0.1.4

func BuildFromEvidence(ctx context.Context, db *sql.DB, owner, device string, req Request, hits []model.SearchResult) (*Pack, error)

BuildFromEvidence preserves the supplied fragment order and roles rather than allowing an answer to suppress a selected prompt. This trusted local experiment API retains the normal scope/content checks and output budgets. It is not enabled by normal HTTP requests or the server configuration.

func BuildWithOptions added in v0.1.4

func BuildWithOptions(ctx context.Context, db *sql.DB, owner, device string, req Request, opts BuildOptions) (*Pack, error)

func BuildWithTrace added in v0.1.4

func BuildWithTrace(ctx context.Context, db *sql.DB, owner, deviceID string, req Request, observer Observer) (*Pack, error)

BuildWithTrace runs the same pipeline with a per-call diagnostic observer. The normal API and hook use Build and expose no intermediate source text.

type Request

type Request struct {
	Mode    string            `json:"mode"`
	Project search.ProjectRef `json:"project"`
	// The calling agent's own session, excluded from results.
	AgentType       string `json:"agent_type,omitempty"`
	NativeSessionID string `json:"native_session_id,omitempty"`
	Query           string `json:"query,omitempty"`
	// Budget in characters; 0 means the mode's default.
	Budget int `json:"budget,omitempty"`
	// Item keys already delivered to this session (not repeated).
	Exclude []string `json:"exclude,omitempty"`
}

Request asks for a context pack.

type TraceEvent added in v0.1.4

type TraceEvent struct {
	Stage    string               `json:"stage"`
	Hits     []model.SearchResult `json:"hits,omitempty"`
	Items    []Item               `json:"items,omitempty"`
	Text     string               `json:"text,omitempty"`
	Decision string               `json:"decision,omitempty"`
	Note     string               `json:"note,omitempty"`
}

TraceEvent observes one completed stage of context construction. It contains source text and is intended for local diagnostics, never the public response. No labels or expected answers are supplied to the retrieval pipeline.

Jump to

Keyboard shortcuts

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