knowledge

package
v0.42.64 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 13 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 knowledge is the agent's persistent memory: a JSON-backed store of Pitfalls (lessons) it has learned. It is the mechanism that lets the agent actually remember experience across restarts — the store is read on startup and written after each task's reflection pass.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsGenericErrorPattern added in v0.41.12

func IsGenericErrorPattern(pattern string) bool

IsGenericErrorPattern reports whether pattern is too generic to usefully key a lesson on (see genericErrorPattern). Checked both when a new lesson is about to be stored (internal/reflect) and whenever existing lessons are queried (Query below) — a pitfall stored before this check existed, or by any other future write path, must not surface either.

Types

type KB

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

KB is an in-memory view of the pitfall store plus its backing file path. The same *KB is shared by the main agent and parallel sub-agents (which record lessons and Query from concurrent tool calls), so it guards its state with mu.

func Open

func Open(path string) (*KB, error)

Open loads the store at path. A missing file is an empty store, not an error.

func (*KB) Add

func (k *KB) Add(p Pitfall) bool

Add inserts a pitfall, de-duplicating on (domain, normalized error pattern). A duplicate bumps Hits and back-fills any empty fields; returns true only when a genuinely new lesson was stored.

func (*KB) All

func (k *KB) All() []Pitfall

All returns a copy of the stored pitfalls.

func (*KB) Clear added in v0.9.3

func (k *KB) Clear() int

Clear removes every lesson, returning how many were dropped.

func (*KB) Count added in v0.9.3

func (k *KB) Count() int

Count reports how many lessons are stored.

func (*KB) Purge added in v0.9.3

func (k *KB) Purge(maxAgeDays int) int

Purge drops lessons last seen more than maxAgeDays ago (by LastSeen), returning how many were removed. maxAgeDays <= 0 is a no-op. Undated entries are kept (Open back-fills them, so they age from first load).

func (*KB) Query

func (k *KB) Query(domain, errText string, limit int) []Pitfall

Query returns pitfalls for a domain. With a non-empty errText it keeps only keyword-overlapping lessons, ranked by overlap then Hits. With an empty errText it returns every lesson in the domain ranked by Hits (used by the help tool). limit <= 0 means no cap.

func (*KB) Save

func (k *KB) Save() error

Save writes the store to disk as pretty JSON, creating the parent directory. Before writing, it re-reads the file and replays this KB's own pending Add() lessons onto that fresh copy instead of blindly overwriting it with pitfalls — otherwise two separate ipsupport-code processes sharing the same global KB could have one's learned lesson silently lost to the other's last write (the mutex only protects against races WITHIN one process). Purge/ Clear set overwrite, skipping the merge: those are a deliberate replace of the whole store. When neither applies — nothing pending and no overwrite due — there is nothing of this KB's own to persist, so Save skips the write entirely instead of falling through to an unconditional write of k.pitfalls: that write would have no fresh read backing it, so a stale in-memory snapshot (e.g. after an idle flush, or a no-op Purge, with no Add in between) could silently clobber a fresher file a different process wrote since this one last read it. A file lock around the whole read-merge-write cycle keeps two such processes from interleaving (one's read landing before the other's write, so each only ever merges its own delta) the same way the in-process mutex keeps two goroutines from interleaving; an in-memory KB (path=="") skips it, since there's nothing on disk to serialize around. The write itself is atomic (temp + rename) so a crash mid-write can't truncate the lessons file either. pending/overwrite are only cleared once that write actually succeeds — if it fails, they're left intact so the next Save replays them instead of silently losing them (a concurrent Add's own Save wouldn't otherwise know to replay a lesson it never recorded).

type KnowledgeError

type KnowledgeError struct {
	Op   string
	Path string
	Err  error
}

KnowledgeError wraps a host-level failure of the knowledge store (read, parse, or write). Model-recoverable conditions never surface as this type.

func (*KnowledgeError) Error

func (e *KnowledgeError) Error() string

func (*KnowledgeError) Unwrap

func (e *KnowledgeError) Unwrap() error

type Pitfall

type Pitfall struct {
	Domain       string `json:"domain"`
	ErrorPattern string `json:"error_pattern"`
	Context      string `json:"context"`
	ProvenFix    string `json:"proven_fix"`
	Hits         int    `json:"hits"`
	Added        string `json:"added,omitempty"`
	LastSeen     string `json:"last_seen,omitempty"`
}

Pitfall is one durable lesson: when ErrorPattern is seen in Domain while doing Context, ProvenFix is what worked. Hits counts reuse, for ranking and pruning. Added/LastSeen (YYYY-MM-DD) drive age-based pruning — LastSeen bumps each time the lesson recurs, so a still-relevant lesson stays fresh.

Jump to

Keyboard shortcuts

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