/** * Anchors - Critical information that survives context compaction * * Anchors are pieces of information that: * 1. Never get compacted - Survive all context management * 2. Always re-injected - Present in every LLM call * 3. Scoped - Session, persistent, or temporary * 4. Prioritized - Critical, safety, or informational */ /** * Priority levels for anchors * - critical: Must remember, highest priority (e.g., "we just implemented X") * - safety: Check before acting (e.g., "verify before git reset") * - info: Useful context (e.g., "modified files this session") */ export type AnchorPriority = 'critical' | 'safety' | 'info'; /** * Scope determines anchor lifetime * - session: Lives for the current session only * - persistent: Saved to disk, survives across sessions * - temporary: Auto-expires after a duration or event */ export type AnchorScope = 'session' | 'persistent' | 'temporary'; /** * An anchor - critical information that survives context compaction */ export interface Anchor { /** * Unique identifier for this anchor */ id: string; /** * The content to remember (injected into every LLM call) */ content: string; /** * Priority level determines display order and emphasis */ priority: AnchorPriority; /** * Scope determines lifetime and persistence behavior */ scope: AnchorScope; /** * When this anchor was created */ createdAt: Date; /** * When this anchor expires (for temporary anchors) */ expiresAt?: Date; /** * Optional tags for filtering and grouping */ tags?: string[]; /** * Optional metadata for custom use */ metadata?: Record; /** * Optional project association for project-scoped anchors * If not provided, the anchor is considered "global" */ projectId?: string; } /** * Input for adding a new anchor (id and createdAt are auto-generated if not provided) */ export interface AnchorInput { id?: string; content: string; priority: AnchorPriority; scope: AnchorScope; expiresAt?: Date; tags?: string[]; metadata?: Record; /** * Optional project association for project-scoped anchors */ projectId?: string; } /** * Options for querying anchors */ export interface AnchorQueryOptions { /** * Filter by priority level */ priority?: AnchorPriority; /** * Filter by scope */ scope?: AnchorScope; /** * Filter by tags (anchors must have ALL specified tags) */ tags?: string[]; /** * Include expired temporary anchors (default: false) */ includeExpired?: boolean; /** * Filter by project ID * Only returns anchors associated with this project */ projectId?: string; /** * If true, only return global anchors (those without a projectId) * Cannot be used together with projectId */ globalOnly?: boolean; } /** * Options for clearing anchors */ export interface AnchorClearOptions { /** * Clear only anchors with this scope */ scope?: AnchorScope; /** * Clear only anchors with these tags */ tags?: string[]; /** * Clear only expired anchors */ expiredOnly?: boolean; /** * Clear only anchors associated with this project */ projectId?: string; /** * If true, only clear global anchors (those without a projectId) * Cannot be used together with projectId */ globalOnly?: boolean; } /** * Configuration for the AnchorManager */ export interface AnchorManagerOptions { /** * Maximum number of anchors to keep (default: 20) * When exceeded, oldest low-priority anchors are removed first */ maxAnchors?: number; /** * Maximum tokens budget for anchors (default: 2000) * Anchors exceeding this budget are truncated or removed */ maxTokens?: number; /** * Path for persisting anchors (for scope: 'persistent') * If not provided, persistent anchors work like session anchors */ persistPath?: string; /** * Include built-in default safety anchors (default: true) */ includeDefaults?: boolean; /** * Token estimator function (default: rough estimate based on chars) */ estimateTokens?: (content: string) => number; } /** * Serialized anchor for persistence */ export interface SerializedAnchor { id: string; content: string; priority: AnchorPriority; scope: AnchorScope; createdAt: string; expiresAt?: string; tags?: string[]; metadata?: Record; projectId?: string; } /** * Event types for anchor operations */ export type AnchorEventType = 'anchor:added' | 'anchor:removed' | 'anchor:expired' | 'anchor:budget_exceeded' | 'anchor:persisted' | 'anchor:loaded'; /** * Anchor event payload */ export interface AnchorEvent { type: AnchorEventType; anchor?: Anchor; anchors?: Anchor[]; message?: string; } /** * Event handler for anchor events */ export type AnchorEventHandler = (event: AnchorEvent) => void;