kb

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 11 Imported by: 0

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

func FormatKBSection(entries []*Entry) string

FormatKBSection formats a slice of entries as a "## Project Knowledge" markdown section suitable for injection into AI prompts.

func KeywordScore

func KeywordScore(entry *Entry, query string) int

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

func Remove(workDir string, id int) error

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

func Save(workDir string, kb *KB) error

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

func SearchPrompt(query string, entries []*Entry) string

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

func Add(workDir, title, content string, tags []string) (*Entry, error)

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).

func Get

func Get(workDir string, id int) (*Entry, error)

Get returns the entry with the given ID, or an error if not found.

func TopRelevant

func TopRelevant(workDir, query string, n int) ([]*Entry, error)

TopRelevant returns up to n entries most relevant to the query using keyword overlap. Entries with zero score are excluded.

type KB

type KB struct {
	Entries []*Entry `json:"entries"`
}

KB is the in-memory representation of the knowledge base.

func Load

func Load(workDir string) (*KB, error)

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.

Jump to

Keyboard shortcuts

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