cache

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatJSON             = "json"
	FormatMarkdownFallback = "markdown-fallback"
	// FormatPlainText is the marker for cache entries written by
	// PlainTextEmitter providers (CLI-based). The Content is the
	// host CLI's already-formatted output — no JSON parse, no
	// retry-once, no degrade warning. Renderer emits Content verbatim.
	FormatPlainText = "plain-text"
)

Format values for cache.Result.Format. Kept as package-level constants so callers (CLI, renderer) avoid stringly-typed comparisons.

View Source
const DefaultTTL = 7 * 24 * time.Hour
View Source
const SchemaVersion = 1

Variables

This section is empty.

Functions

func Compute

func Compute(args ComputeArgs) string

Compute returns the deterministic SHA-256 key (lowercase hex) for the cache lookup. The schema version is folded in so a future bump invalidates every existing entry without touching disk.

func EnsureGitignore

func EnsureGitignore(repoRoot string) (bool, error)

EnsureGitignore appends `.commitbrief/` to the repo's .gitignore if not already present. Idempotent: a file that already contains the entry is left untouched. Returns true if the file was modified.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

func Open

func Open(opts Options) (*Cache, error)

func (*Cache) Delete

func (c *Cache) Delete(key string) error

func (*Cache) Dir

func (c *Cache) Dir() string

func (*Cache) Get

func (c *Cache) Get(key string) (Entry, bool)

func (*Cache) Put

func (c *Cache) Put(key string, entry Entry) error

type ComputeArgs

type ComputeArgs struct {
	Diff         string
	SystemPrompt string
	Provider     string
	Model        string
	Lang         string

	// WithContext marks a --with-context run (ADR-0017). A context run and
	// a diff-only run on the same diff must not alias, so when true a
	// marker is folded into the key. When false NOTHING extra is written,
	// keeping diff-only keys byte-identical to pre-ADR-0017 entries — no
	// mass cache invalidation on upgrade.
	WithContext bool

	// Mode namespaces non-review cache entries (e.g. "commit" for the
	// commit-message generation, ADR-0019). The commit system prompt
	// already differs from the review prompt, so collision is unlikely;
	// the explicit marker makes the separation impossible. Folded in only
	// when non-empty, so review keys stay byte-identical to before.
	Mode string
}

type Entry

type Entry struct {
	Version   int       `json:"version"`
	CreatedAt time.Time `json:"created_at"`
	TTL       int64     `json:"ttl"`
	Key       KeyMeta   `json:"key"`
	Result    Result    `json:"result"`
}

func (Entry) Expired

func (e Entry) Expired() bool

func (Entry) ExpiredAt

func (e Entry) ExpiredAt(now time.Time) bool

type KeyMeta

type KeyMeta struct {
	DiffHash         string `json:"diff_hash"`
	SystemPromptHash string `json:"system_prompt_hash"`
	Model            string `json:"model"`
	Provider         string `json:"provider"`
	Lang             string `json:"lang"`
}

type Options

type Options struct {
	Dir string

	// RepoRoot, if non-empty, enables auto-add of `.commitbrief/` to the
	// repo's .gitignore on the first successful Put.
	RepoRoot string

	// TTL controls how long an entry is considered fresh. Zero falls back
	// to DefaultTTL (7 days).
	TTL time.Duration

	// MaxSizeBytes bounds the on-disk cache size. After each successful
	// Put, if the cache directory exceeds this many bytes, the oldest
	// entries (by CreatedAt, mtime fallback) are evicted oldest-first
	// until the total fits — see eviction.go. Zero or negative disables
	// eviction (unlimited; the manual `cache prune` stays the stand-in).
	// The just-written entry is never evicted, so a single entry larger
	// than the cap survives.
	MaxSizeBytes int64

	// Now overrides time.Now (test injection); production callers leave it nil.
	Now func() time.Time
}

type Result

type Result struct {
	Content string `json:"content"`
	Tokens  Tokens `json:"tokens"`

	// Format records which renderer path the cached entry should take on
	// replay. Values (ADR-0014 §4):
	//   "json"               — Content is the structured-findings JSON; the
	//                          renderer parses it as usual.
	//   "markdown-fallback"  — provider produced unparseable output; the
	//                          retry also failed. The cached Content is the
	//                          raw text and the renderer skips the parse
	//                          step entirely (no warning re-emitted).
	// Empty string is treated as "json" for backwards compatibility with
	// pre-ADR-0014 cache entries; they'll be invalidated next request
	// anyway via the system-prompt SHA change (ADR-0014 §6).
	Format string `json:"format,omitempty"`
}

type Tokens

type Tokens struct {
	Input  int `json:"input"`
	Output int `json:"output"`
	Cached int `json:"cached"`
}

Jump to

Keyboard shortcuts

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