knowledge

package
v0.62.18 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 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

View Source
const KindAvoid = "avoid"

KindAvoid marks a lesson about an approach that never worked, as opposed to a fix that did. The two must render differently wherever a lesson is shown to a model: telling it "this worked: <the thing that never worked>" is worse than saying nothing.

Variables

This section is empty.

Functions

func HasCredential added in v0.57.1

func HasCredential(s string) bool

HasCredential reports whether s contains something that looks like a secret. Used to decide whether a rejected lesson can be logged at all: a store that refuses to keep a token is no use if the rejection writes it to agent.log.

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.

func IsProjectSpecific added in v0.45.0

func IsProjectSpecific(s string) bool

IsProjectSpecific reports whether s carries a value from the run it was learned in rather than a general lesson: a file path, a path out of the workspace or into $HOME, a URL, an IP, a host:port, or anything that looks like a credential.

What it deliberately does NOT catch: a bare directory or branch or model name ("run from the nemotron-extreme-quant directory", "checkout feature/x"). Catching those needs a rule like "word/word" or "an unusual noun", which eats "go test ./...", "internal/knowledge" and most real advice with it. The backstop for those is that the store is PER WORKSPACE (see config.DefaultKBPath), so such a lesson can only mislead the project it came from. Reported live, from a real debug log: a stored lesson read "Provide proper path parameter: {"path": "nemotron-extreme-quant/PLAN.md", "content": "..."}" and surfaced — in a DIFFERENT project — on a failure whose real cause was the encoding of the params blob, not the path. The model quoted that hint back in its own reasoning and kept retrying the wrong thing. The reflection prompt already says to exclude anything project-specific; a model that ignores it must not be able to poison the store anyway, so this is enforced on the way in.

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. A nil KB reports none rather than panicking: callers that merely DISPLAY what is known (the /config panel) must not be the reason a session dies when the store failed to open.

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. Nil-safe, like All.

func (*KB) Delete added in v0.45.0

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

Delete removes the one lesson matching p's (domain, normalized error pattern), reporting whether anything was removed. Pass a Pitfall straight from Sorted.

func (*KB) DropWhere added in v0.45.0

func (k *KB) DropWhere(drop func(Pitfall) bool) int

DropWhere removes every lesson for which drop reports true, returning how many went. Used to retire lessons a later rule made unstorable (see IsProjectSpecific) without waiting for them to age out.

func (*KB) MarkUsed added in v0.51.0

func (k *KB) MarkUsed(p Pitfall)

MarkUsed records that a lesson was actually SURFACED to the model, bumping its Hits and freshness.

Hits used to move only in Add — i.e. only when the reflection pass re-derived the same lesson from a fresh failure. That made the ranking measure exactly the wrong thing: a lesson that WORKS stops the failure recurring, so it is never re-derived, its Hits and LastSeen freeze, and Purge eventually deletes it; a lesson that never helps keeps being re-derived, climbs the ranking and stays fresh forever. Counting retrieval instead means usefulness, not futility, is what keeps a lesson alive.

Not persisted on its own: the next Save writes it, which is enough for a ranking signal and keeps a hot path off the disk.

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 changes onto that fresh copy instead of blindly overwriting it with pitfalls — otherwise two separate ipsupport-code processes sharing the same same store could have one's learned lesson silently lost to the other's last write (the mutex only protects against races WITHIN one process). With nothing pending 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 is 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).

func (*KB) Sorted added in v0.45.0

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

Sorted returns every lesson in a stable, content-derived order (domain, then normalized error pattern). Listing and deleting a lesson by its position both go through this: the store's own slice order depends on insertion history and on Purge compaction, so a number printed by one command could otherwise point at a different lesson by the time the next command runs.

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"`
	// Kind is "" (a fix that actually worked — the original and default shape) or
	// KindAvoid (a dead end: the same approach was retried and kept failing, and
	// ProvenFix says what to do differently). Reported live: a run died after ten
	// identical failing tool calls, and the ONE lesson worth keeping from it —
	// "re-sending this shape never works" — could not be expressed at all, because
	// the store only ever recorded fixes that succeeded. Omitted from JSON when
	// empty, so lessons written before this field existed load unchanged.
	Kind     string `json:"kind,omitempty"`
	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