Documentation
¶
Overview ¶
Package memory bridges Claude Code's auto memory (MEMORY.md) into the .context/ directory with discovery, mirroring, and drift detection.
Claude Code maintains per-project auto memory at ~/.claude/projects/<slug>/memory/MEMORY.md. This package locates that file from the project root, mirrors it into .context/memory/mirror.md (git-tracked), and archives previous versions before each sync.
Discovery encodes the project root path into the Claude Code slug format: absolute path with "/" replaced by "-", prefixed with "-".
Sync state is tracked in .context/state/memory-import.json to support drift detection and future import/publish phases.
Index ¶
- Constants
- func Archive(contextDir string) (string, error)
- func ArchiveCount(contextDir string) int
- func Diff(contextDir, sourcePath string) (string, error)
- func DiscoverMemoryPath(projectRoot string) (string, error)
- func EntryHash(text string) string
- func HasDrift(contextDir, sourcePath string) bool
- func MergePublished(existing, published string) (string, bool)
- func ProjectSlug(absPath string) string
- func Promote(e Entry, classification Classification) error
- func RemovePublished(content string) (string, bool)
- func SaveState(contextDir string, s State) error
- type Classification
- type Entry
- type EntryKind
- type PublishResult
- type State
- type SyncResult
Constants ¶
const TargetSkip = "skip"
TargetSkip indicates an entry that doesn't match any classification rule.
Variables ¶
This section is empty.
Functions ¶
func Archive ¶
Archive copies the current mirror.md to archive/mirror-<timestamp>.md. Returns the archive path. Returns an error if no mirror exists.
func ArchiveCount ¶
ArchiveCount returns the number of archived mirror snapshots.
func Diff ¶
Diff returns a simple line-based diff between the mirror and the source. Returns empty string when files are identical.
func DiscoverMemoryPath ¶
DiscoverMemoryPath locates Claude Code's auto memory file for the given project root. The path is derived from how Claude Code encodes project directories: absolute path with "/" replaced by "-", prefixed with "-".
Returns the resolved path if the file exists, or an error if auto memory has not been created yet.
func EntryHash ¶
EntryHash computes a deduplication hash for an entry. Uses SHA-256 of the text, truncated to 16 hex chars.
func HasDrift ¶
HasDrift checks whether MEMORY.md has been modified since the last sync. Returns false if either file is missing (no drift to report).
func MergePublished ¶
MergePublished inserts or replaces the marker block in existing MEMORY.md content.
If markers exist, replaces everything between them. If markers are missing, appends the block at the end (recovery).
Parameters:
- existing: current MEMORY.md content
- published: formatted publish block to insert
Returns:
- string: merged content
- bool: true if markers were missing (appended instead of replaced)
func ProjectSlug ¶
ProjectSlug encodes an absolute project path into the Claude Code project directory slug format: "/" replaced by "-", prefixed with "-".
Example: /home/jose/WORKSPACE/ctx → -home-jose-WORKSPACE-ctx
func Promote ¶
func Promote(e Entry, classification Classification) error
Promote writes a classified entry to the appropriate .context/ file.
Uses the entry package for consistent formatting and indexing.
Parameters:
- e: the memory entry to promote
- classification: target type and confidence from classification
Returns:
- error: non-nil if the entry write fails
func RemovePublished ¶
RemovePublished strips the marker block from MEMORY.md content.
Parameters:
- content: current MEMORY.md content
Returns:
- string: content with the publish block removed
- bool: true if markers were found and removed
Types ¶
type Classification ¶
type Classification struct {
Target string // config.Entry* constant or "skip"
Keywords []string // Keywords that triggered the match
}
Classification is the result of heuristic entry classification.
func Classify ¶
func Classify(entry Entry) Classification
Classify assigns a target file type to an entry based on keyword heuristics.
Matching is case-insensitive. The first rule with a keyword match wins (priority: conventions > decisions > learnings > tasks > skip).
type Entry ¶
type Entry struct {
Text string // Raw text of the entry (trimmed)
StartLine int // 1-based line number where the entry begins
Kind EntryKind // How the entry was delimited
}
Entry is a discrete block parsed from MEMORY.md.
func ParseEntries ¶
ParseEntries splits MEMORY.md content into discrete entries.
Entry boundaries:
- Markdown headers (## or ###) start a new entry
- Blank lines separate paragraphs into distinct entries
- Consecutive list items (- or *) are grouped into a single entry
The top-level heading (# Title) is skipped as it's structural, not content.
type PublishResult ¶
type PublishResult struct {
Tasks []string
Decisions []string
Conventions []string
Learnings []string
TotalLines int
}
PublishResult holds what was selected for publishing.
func Publish ¶
func Publish(contextDir, memoryPath string, budget int) (PublishResult, error)
Publish writes selected content to MEMORY.md with marker-based merge.
Parameters:
- contextDir: path to the .context/ directory
- memoryPath: path to the MEMORY.md file
- budget: maximum number of lines in the published block
Returns:
- PublishResult: the content that was published
- error: non-nil if selection or file write fails
func SelectContent ¶
func SelectContent(contextDir string, budget int) (PublishResult, error)
SelectContent reads .context/ files and selects content within the line budget.
Priority order: tasks > decisions > conventions > learnings. If over budget, trims from bottom (learnings, conventions, decisions).
Parameters:
- contextDir: path to the .context/ directory
- budget: maximum number of lines in the published block
Returns:
- PublishResult: selected content with per-section slices
- error: non-nil if content selection fails
func (*PublishResult) Format ¶
func (r *PublishResult) Format() string
Format renders the publish result as a Markdown block (without markers).
Returns:
- string: formatted Markdown with section headings and items
type State ¶
type State struct {
LastSync *time.Time `json:"last_sync"`
LastImport *time.Time `json:"last_import"`
LastPublish *time.Time `json:"last_publish"`
ImportedHashes []string `json:"imported_hashes"`
}
State tracks memory bridge sync timestamps and import/publish progress.
func LoadState ¶
LoadState reads the sync state from .context/state/memory-import.json. Returns a zero-value State if the file does not exist.
func (*State) Imported ¶
Imported reports whether an entry hash has already been imported. Stored entries use format "hash:target:date"; matches on hash prefix.
func (*State) MarkImported ¶
MarkImported records an entry hash with its target and date.
func (*State) MarkImportedDone ¶
func (s *State) MarkImportedDone()
MarkImportedDone updates LastImport to the current time.
func (*State) MarkSynced ¶
func (s *State) MarkSynced()
MarkSynced updates the state with the current timestamp.
type SyncResult ¶
type SyncResult struct {
SourcePath string
MirrorPath string
ArchivedTo string // empty if no prior mirror existed
SourceLines int
MirrorLines int // lines in the previous mirror (0 if first sync)
}
SyncResult holds the outcome of a Sync operation.
func Sync ¶
func Sync(contextDir, sourcePath string) (SyncResult, error)
Sync copies sourcePath to .context/memory/mirror.md, archiving the previous mirror if one exists. Creates directories as needed.