Documentation
¶
Overview ¶
Package kb implements a persistent project-scoped knowledge base stored in .cloop/kb.json. Each entry has an ID, title, content, tags, and creation timestamp. The KB is used to inject relevant context into task execution prompts.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatKBSection ¶
FormatKBSection formats a slice of entries as a "## Project Knowledge" markdown section suitable for injection into AI prompts.
func KeywordScore ¶
KeywordScore returns a relevance score for an entry against a query string. It counts normalized word overlaps between the query and the entry title+content.
func Remove ¶
Remove deletes the entry with the given ID. Returns an error if not found.
Holds kbMu for the entire load → filter → save cycle so a concurrent Add cannot have its appended entry silently dropped by Remove's stale snapshot.
func Save ¶
Save writes the knowledge base to disk, creating the .cloop directory if needed.
The write is atomic — data is staged in a sibling .tmp file, fsynced, then renamed into place. A torn write (crash, ENOSPC, two writers racing across processes) would otherwise leave the KB unparseable and silently break context injection on the next run.
func SearchPrompt ¶
SearchPrompt builds a prompt for AI-powered semantic search over KB entries.
Types ¶
type Entry ¶
type Entry struct {
ID int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Tags []string `json:"tags,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
Entry is a single knowledge base record.
func AISearch ¶
func AISearch(ctx context.Context, p provider.Provider, model, workDir, query string) ([]*Entry, error)
AISearch performs semantic search using the provider and returns relevant entries.
func Add ¶
Add creates a new entry and appends it to the KB. Returns the new entry.
Holds kbMu for the entire load → assign-ID → save cycle so two concurrent Add calls cannot read the same nextID and produce duplicate IDs (or worse, have one call's append clobber the other on save).
type KB ¶
type KB struct {
Entries []*Entry `json:"entries"`
}
KB is the in-memory representation of the knowledge base.
func Load ¶
Load reads the knowledge base from disk. Returns an empty KB if the file does not exist.
On parse failure (zero-byte file from a torn pre-atomicfile write, schema drift, manual edit gone wrong) the corrupt kb.json is quarantined aside as kb.json.corrupt-<unix> and an empty KB is returned. Refusing the load would silently break context injection on every subsequent task — `cloop run` would run with the AI deprived of project knowledge instead of surfacing the corruption — and the only fix would be a manual `rm`. The quarantine surfaces the problem on stderr and lets work continue.