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

grimoire

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package grimoire provides .grimoire project context file handling.

Index

Constants

View Source
const (
	PriorityGlobal   = 10 // ~/.celeste/grimoire.md
	PriorityProject  = 20 // /repo-root/.grimoire
	PriorityFragment = 30 // /repo-root/.celeste/grimoire/*.md
	PriorityLocal    = 40 // /repo-root/.grimoire.local
)

Priority levels for grimoire sources (lowest to highest).

View Source
const MaxIncludeDepth = 3

MaxIncludeDepth is the maximum nesting depth for @include resolution.

View Source
const MaxSize = 25 * 1024 // 25KB

MaxSize is the maximum total grimoire context size in bytes.

Variables

This section is empty.

Functions

func AgentsTemplate

func AgentsTemplate(info *ProjectInfo) string

AgentsTemplate is a starting AGENTS.md for the detected project.

func ContextFilePaths

func ContextFilePaths(workspace string) []string

ContextFilePaths are the AGENTS.md and CLAUDE.md paths ContextFiles would read for workspace, present or not, in reading order.

func Describe

func Describe(dir string) (text string, found bool)

Describe is /grimoire and `celeste grimoire` (2.0 W4, ruling 2): the context-file warnings, the sources, the merged grimoire with its staleness note, then the AGENTS.md / CLAUDE.md section. found is false when neither a grimoire nor a context file loads; the text then says so (after any warnings).

func GenerateTemplate

func GenerateTemplate(info *ProjectInfo, dir string) string

GenerateTemplate creates a starter .grimoire file from project info.

func GitRoot

func GitRoot(dir string) (string, bool)

GitRoot returns the nearest ancestor of dir (or dir) holding .git, a directory or (in a worktree) a file.

func GrimoireMeta

func GrimoireMeta(dir string) string

GrimoireMeta returns a metadata header block for the grimoire file. Includes timestamp, git hash, and branch so staleness can be detected.

func HasProjectContext

func HasProjectContext(workspace string) bool

HasProjectContext reports a project grimoire (.grimoire, .grimoire.local, .celeste/grimoire/*.md) that LoadAll would load, or a context file (AGENTS.md, CLAUDE.md) between the git root and the workspace (2.0 W4, ruling 6). The global ~/.celeste/grimoire.md is not project context, and neither is an empty or blank grimoire, which renders nothing.

func Init

func Init(dir string) (string, error)

Init creates a .grimoire file in the given directory. It writes nothing else: the project's .gitignore is left alone (2.0 W4). Returns the path to the created file, or an error if one already exists.

func InitAgents

func InitAgents(dir string) (string, error)

InitAgents writes AGENTS.md in dir from AgentsTemplate; it never overwrites one.

func RenderContextFiles

func RenderContextFiles(files []ContextFile) string

RenderContextFiles is the project context's "# Project instructions" section (ruling 2); "" for no files.

func ResolveIncludes

func ResolveIncludes(g *Grimoire, baseDir string) error

ResolveIncludes expands @./path references in the Incantations section, confined to the repository holding baseDir (see includeScope). It never returns an error for individual file failures; instead, it records errors on the individual IncludeRef entries.

func RunInit

func RunInit(dir string, agents bool) ([]string, error)

RunInit is /init and `celeste init` (2.0 W4, ruling 7): it writes .grimoire with Init and, with agents, AGENTS.md with InitAgents. It returns one line per step for the front end to print. A file that is already there is left alone and noted; when nothing was written the error joins those notes (errors.Is(err, fs.ErrExist) holds). Any other failure stops the run and is returned with the lines so far.

Types

type ContextFile

type ContextFile struct {
	Path      string // absolute
	Rel       string // relative to the git root (or the workspace)
	Content   string
	Truncated int // bytes cut by the caps; 0 when whole
}

ContextFile is one AGENTS.md or CLAUDE.md the project context carries.

func ContextFiles

func ContextFiles(workspace string) ([]ContextFile, []string)

ContextFiles reads AGENTS.md and CLAUDE.md from the git root down to the workspace (rulings 1 and 3): regular files only, 32 KiB per file and 64 KiB in all. Warnings name each file a cap cut or skipped.

type GitSnapshot

type GitSnapshot struct {
	Branch        string
	MainBranch    string
	Status        string // short status output
	RecentCommits string // last 5 commits oneline
	UserName      string
}

GitSnapshot captures the current git state for system prompt injection.

func CaptureGitSnapshot

func CaptureGitSnapshot(workDir string) *GitSnapshot

CaptureGitSnapshot runs 5 git commands in parallel and returns the snapshot. Returns nil if not in a git repo. Truncates status to 2KB max.

func (*GitSnapshot) FormatForPrompt

func (s *GitSnapshot) FormatForPrompt() string

FormatForPrompt formats the snapshot for system prompt injection.

type Grimoire

type Grimoire struct {
	Sources      []string          // file paths that contributed (ordered by priority)
	Bindings     []string          // project facts (language, structure, commands)
	Rituals      []string          // behavioral rules (always/never do)
	Incantations []IncludeRef      // @path includes with resolved content
	Wards        []string          // protected areas
	Hooks        []HookEntry       // pre/post tool execution commands
	RawSections  map[string]string // unparsed section content by heading
	// StreamRules are "## Stream Rules" sections (2.0 W3), one per file, in
	// source order. They are rules for the rules package, never rendered
	// into the prompt: a rule costs no context until it fires.
	StreamRules []RuleSection
	Meta        GrimoireMetadata // embedded metadata (last updated, git hash, etc.)
}

Grimoire represents a fully resolved project context.

func LoadAll

func LoadAll(startDir string) (*Grimoire, error)

LoadAll discovers, parses, and merges all grimoire sources for the given directory.

func Merge

func Merge(grimoires ...*Grimoire) *Grimoire

Merge combines multiple grimoires into one, preserving order. Later grimoires take precedence for RawSections keys.

func Parse

func Parse(content string, baseDir string) (*Grimoire, error)

Parse parses a .grimoire markdown file into a Grimoire struct. It splits on ## headings and maps content to known section types.

func (*Grimoire) IsEmpty

func (g *Grimoire) IsEmpty() bool

IsEmpty returns true if the grimoire has no content.

func (*Grimoire) Render

func (g *Grimoire) Render() string

Render produces the grimoire content for injection into a system prompt.

func (*Grimoire) StalenessInfo

func (g *Grimoire) StalenessInfo(currentDir string) string

StalenessInfo returns how stale the grimoire is relative to current git state. Returns a human-readable message, or "" if metadata is unavailable.

type GrimoireMetadata

type GrimoireMetadata struct {
	LastUpdated    time.Time
	GitHash        string
	GitBranch      string
	GitCommitCount int
}

GrimoireMetadata holds the embedded metadata from the grimoire file.

type GrimoireSource

type GrimoireSource struct {
	Path     string
	Priority int
	// contains filtered or unexported fields
}

GrimoireSource represents a discovered .grimoire file with its priority.

func Discover

func Discover(startDir string) ([]GrimoireSource, error)

Discover walks upward from startDir to the filesystem root, collecting all .grimoire sources ordered by priority (lowest first).

type HookEntry

type HookEntry struct {
	Phase    string // "PreToolUse" or "PostToolUse"
	ToolName string // tool name pattern (e.g., "bash", "write_file")
	Command  string // shell command to execute
}

HookEntry represents a pre/post tool execution hook.

type IncludeRef

type IncludeRef struct {
	Path     string // original @path from grimoire
	Resolved string // absolute path after resolution
	Content  string // file content (empty if unresolved)
	Error    string // non-empty if resolution failed
}

IncludeRef represents an @include directive with its resolved content.

type ProjectInfo

type ProjectInfo struct {
	Language     string
	ModulePath   string
	TestCommand  string
	LintCommand  string
	BuildCommand string
	EntryPoint   string
	Framework    string
}

ProjectInfo holds detected project metadata.

func DetectProject

func DetectProject(dir string) (*ProjectInfo, error)

DetectProject inspects the given directory and returns project information.

type RuleSection

type RuleSection struct {
	Source string
	Body   string
}

RuleSection is one file's "## Stream Rules" body and the file it came from.

Jump to

Keyboard shortcuts

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