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
- func IsGenericErrorPattern(pattern string) bool
- func IsProjectSpecific(s string) bool
- type KB
- func (k *KB) Add(p Pitfall) bool
- func (k *KB) All() []Pitfall
- func (k *KB) Clear() int
- func (k *KB) Count() int
- func (k *KB) Delete(p Pitfall) bool
- func (k *KB) DropWhere(drop func(Pitfall) bool) int
- func (k *KB) Purge(maxAgeDays int) int
- func (k *KB) Query(domain, errText string, limit int) []Pitfall
- func (k *KB) Save() error
- func (k *KB) Sorted() []Pitfall
- type KnowledgeError
- type Pitfall
Constants ¶
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 IsGenericErrorPattern ¶ added in v0.41.12
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
IsProjectSpecific reports whether s carries a value from the run it was learned in rather than a general lesson. 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 (*KB) Add ¶
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) Delete ¶ added in v0.45.0
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
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) Purge ¶ added in v0.9.3
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 ¶
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 ¶
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 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). 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).
func (*KB) Sorted ¶ added in v0.45.0
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 ¶
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.