Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

add memory db

Justin D'Souza019830fc18c4
diff --git a/cli/beacon/internal/learning/store.go b/cli/beacon/internal/learning/store.go
new file mode 100644
index 00000000..9858713d
--- /dev/null
+++ b/cli/beacon/internal/learning/store.go
@@ -0,0 +1,576 @@
+package learning
+
+import (
+ "crypto/sha256"
+ "database/sql"
+ "encoding/hex"
+ "encoding/json"
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+
+ _ "modernc.org/sqlite"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+)
+
+const (
+ storeFile = "memory.db"
+ storeSchemaVersion = 1
+)
+
+type Store struct {
+ dbPath string
+}
+
+type Query struct {
+ ProjectPath string
+ ProjectID string
+ State string
+ Kind string
+ Q string
+ Limit int
+ Page int
+}
+
+func Open(path string) *Store {
+ return &Store{dbPath: path}
+}
+
+func PathForRuntimeLog(logPath string) string {
+ if strings.TrimSpace(logPath) == "" {
+ return storeFile
+ }
+ dir := filepath.Dir(logPath)
+ if filepath.Base(dir) == "logs" {
+ return filepath.Join(filepath.Dir(dir), storeFile)
+ }
+ return filepath.Join(dir, storeFile)
+}
+
+func (s *Store) Path() string {
+ return s.dbPath
+}
+
+func (s *Store) db() (*sql.DB, error) {
+ if s.dbPath == "" {
+ s.dbPath = storeFile
+ }
+ if err := os.MkdirAll(filepath.Dir(s.dbPath), 0700); err != nil && filepath.Dir(s.dbPath) != "." {
+ return nil, err
+ }
+ db, err := sql.Open("sqlite", sqliteURI(s.dbPath))
+ if err != nil {
+ return nil, err
+ }
+ if _, err := db.Exec(`PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000;`); err != nil {
+ _ = db.Close()
+ return nil, err
+ }
+ if err := ensureSchema(db); err != nil {
+ _ = db.Close()
+ return nil, err
+ }
+ return db, nil
+}
+
+func sqliteURI(path string) string {
+ abs, err := filepath.Abs(path)
+ if err != nil {
+ abs = path
+ }
+ return "file:" + filepath.ToSlash(abs) + "?_pragma=busy_timeout(5000)"
+}
+
+func ensureSchema(db *sql.DB) error {
+ var version int
+ if err := db.QueryRow(`PRAGMA user_version`).Scan(&version); err != nil {
+ return err
+ }
+ if version > storeSchemaVersion {
+ return fmt.Errorf("memory store schema %d is newer than this beacon supports (%d)", version, storeSchemaVersion)
+ }
+ stmts := []string{
+ `CREATE TABLE IF NOT EXISTS evaluations (
+ id TEXT PRIMARY KEY,
+ project_id TEXT NOT NULL,
+ trace_id TEXT NOT NULL,
+ status TEXT NOT NULL,
+ created_at TEXT NOT NULL,
+ updated_at TEXT NOT NULL,
+ rubric_hash TEXT NOT NULL,
+ evaluator TEXT NOT NULL,
+ evaluation_json TEXT NOT NULL
+ )`,
+ `CREATE INDEX IF NOT EXISTS evaluations_by_project_updated ON evaluations (project_id, updated_at DESC)`,
+ `CREATE UNIQUE INDEX IF NOT EXISTS evaluations_by_project_trace_rubric ON evaluations (project_id, trace_id, rubric_hash)`,
+ `CREATE TABLE IF NOT EXISTS candidates (
+ id TEXT PRIMARY KEY,
+ project_id TEXT NOT NULL,
+ state TEXT NOT NULL,
+ kind TEXT NOT NULL,
+ title TEXT NOT NULL,
+ source_evaluation_id TEXT,
+ memory_id TEXT,
+ created_at TEXT NOT NULL,
+ updated_at TEXT NOT NULL,
+ candidate_json TEXT NOT NULL
+ )`,
+ `CREATE INDEX IF NOT EXISTS candidates_by_project_state_updated ON candidates (project_id, state, updated_at DESC)`,
+ `CREATE INDEX IF NOT EXISTS candidates_by_source_eval ON candidates (source_evaluation_id)`,
+ `CREATE TABLE IF NOT EXISTS memories (
+ id TEXT PRIMARY KEY,
+ candidate_id TEXT NOT NULL,
+ project_id TEXT NOT NULL,
+ kind TEXT NOT NULL,
+ title TEXT NOT NULL,
+ created_at TEXT NOT NULL,
+ updated_at TEXT NOT NULL,
+ superseded_by TEXT,
+ memory_json TEXT NOT NULL
+ )`,
+ `CREATE INDEX IF NOT EXISTS memories_by_project_updated ON memories (project_id, updated_at DESC)`,
+ `CREATE UNIQUE INDEX IF NOT EXISTS memories_by_candidate ON memories (candidate_id)`,
+ }
+ for _, stmt := range stmts {
+ if _, err := db.Exec(stmt); err != nil {
+ return err
+ }
+ }
+ if version != storeSchemaVersion {
+ if _, err := db.Exec(fmt.Sprintf(`PRAGMA user_version = %d`, storeSchemaVersion)); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func ResolveProject(path string) (asymptoteobserve.LearningProjectV1, error) {
+ if strings.TrimSpace(path) == "" {
+ var err error
+ path, err = os.Getwd()
+ if err != nil {
+ return asymptoteobserve.LearningProjectV1{}, err
+ }
+ }
+ abs, err := filepath.Abs(path)
+ if err != nil {
+ return asymptoteobserve.LearningProjectV1{}, err
+ }
+ root := findGitRoot(abs)
+ if root == "" {
+ root = abs
+ }
+ project := asymptoteobserve.LearningProjectV1{
+ Path: filepath.Clean(root),
+ RemoteURL: readGitConfigValue(root, "remote \"origin\"", "url"),
+ Branch: readGitHeadBranch(root),
+ }
+ project.ID = ProjectID(project)
+ return project, nil
+}
+
+func ProjectID(project asymptoteobserve.LearningProjectV1) string {
+ key := firstNonEmpty(project.RemoteURL, project.Path)
+ if key == "" {
+ key = "unknown"
+ }
+ sum := sha256.Sum256([]byte(strings.ToLower(filepath.ToSlash(key))))
+ return "sha256:" + hex.EncodeToString(sum[:])
+}
+
+func findGitRoot(path string) string {
+ current := filepath.Clean(path)
+ info, err := os.Stat(current)
+ if err == nil && !info.IsDir() {
+ current = filepath.Dir(current)
+ }
+ for {
+ if _, err := os.Stat(filepath.Join(current, ".git")); err == nil {
+ return current
+ }
+ next := filepath.Dir(current)
+ if next == current {
+ return ""
+ }
+ current = next
+ }
+}
+
+func readGitHeadBranch(root string) string {
+ data, err := os.ReadFile(filepath.Join(root, ".git", "HEAD"))
+ if err != nil {
+ return ""
+ }
+ text := strings.TrimSpace(string(data))
+ const prefix = "ref: refs/heads/"
+ if strings.HasPrefix(text, prefix) {
+ return strings.TrimPrefix(text, prefix)
+ }
+ return ""
+}
+
+func readGitConfigValue(root, section, key string) string {
+ data, err := os.ReadFile(filepath.Join(root, ".git", "config"))
+ if err != nil {
+ return ""
+ }
+ var inSection bool
+ for _, line := range strings.Split(string(data), "\n") {
+ trimmed := strings.TrimSpace(line)
+ if strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]") {
+ inSection = strings.Trim(trimmed, "[]") == section
+ continue
+ }
+ if !inSection || !strings.Contains(trimmed, "=") {
+ continue
+ }
+ parts := strings.SplitN(trimmed, "=", 2)
+ if strings.TrimSpace(parts[0]) == key {
+ return strings.TrimSpace(parts[1])
+ }
+ }
+ return ""
+}
+
+func (s *Store) Status() (asymptoteobserve.LearningStatusV1, error) {
+ db, err := s.db()
+ if err != nil {
+ return asymptoteobserve.LearningStatusV1{}, err
+ }
+ defer db.Close()
+ status := asymptoteobserve.LearningStatusV1{
+ SchemaVersion: asymptoteobserve.LearningSchemaVersion,
+ Path: s.dbPath,
+ SizeBytes: fileSize(s.dbPath),
+ }
+ _ = db.QueryRow(`SELECT COUNT(*) FROM evaluations`).Scan(&status.Evaluations)
+ _ = db.QueryRow(`SELECT COUNT(*) FROM candidates`).Scan(&status.Candidates)
+ _ = db.QueryRow(`SELECT COUNT(*) FROM memories WHERE superseded_by IS NULL OR superseded_by = ''`).Scan(&status.ApprovedMemories)
+ return status, nil
+}
+
+func (s *Store) PutEvaluation(e asymptoteobserve.LearningEvaluationV1) error {
+ if e.SchemaVersion == "" {
+ e.SchemaVersion = asymptoteobserve.LearningSchemaVersion
+ }
+ now := nowString()
+ if e.CreatedAt == "" {
+ e.CreatedAt = now
+ }
+ e.UpdatedAt = now
+ if e.ID == "" {
+ e.ID = EvaluationID(e)
+ }
+ data, err := json.Marshal(e)
+ if err != nil {
+ return err
+ }
+ db, err := s.db()
+ if err != nil {
+ return err
+ }
+ defer db.Close()
+ _, err = db.Exec(`INSERT INTO evaluations (id, project_id, trace_id, status, created_at, updated_at, rubric_hash, evaluator, evaluation_json)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT(id) DO UPDATE SET status = excluded.status, updated_at = excluded.updated_at, evaluator = excluded.evaluator, evaluation_json = excluded.evaluation_json`,
+ e.ID, e.Project.ID, e.Trace.ID, e.Status, e.CreatedAt, e.UpdatedAt, e.RubricHash, e.Evaluator, string(data))
+ return err
+}
+
+func (s *Store) ListEvaluations(query Query) ([]asymptoteobserve.LearningEvaluationV1, error) {
+ db, err := s.db()
+ if err != nil {
+ return nil, err
+ }
+ defer db.Close()
+ where, args := queryWhere(query, "project_id", "status", "")
+ rows, err := db.Query(`SELECT evaluation_json FROM evaluations`+where+` ORDER BY updated_at DESC LIMIT ? OFFSET ?`, append(args, normalizeLimit(query.Limit), offset(query))...)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ var out []asymptoteobserve.LearningEvaluationV1
+ for rows.Next() {
+ var raw string
+ if err := rows.Scan(&raw); err != nil {
+ return nil, err
+ }
+ var value asymptoteobserve.LearningEvaluationV1
+ if err := json.Unmarshal([]byte(raw), &value); err != nil {
+ return nil, err
+ }
+ if query.Q == "" || matchesText(query.Q, value.ID, value.Trace.ID, value.Trace.Title, value.Evaluator, value.Status) {
+ out = append(out, value)
+ }
+ }
+ return out, rows.Err()
+}
+
+func (s *Store) GetEvaluation(id string) (asymptoteobserve.LearningEvaluationV1, bool, error) {
+ var raw string
+ ok, err := s.getJSON(`SELECT evaluation_json FROM evaluations WHERE id = ?`, id, &raw)
+ if err != nil || !ok {
+ return asymptoteobserve.LearningEvaluationV1{}, ok, err
+ }
+ var value asymptoteobserve.LearningEvaluationV1
+ return value, true, json.Unmarshal([]byte(raw), &value)
+}
+
+func (s *Store) PutCandidate(c asymptoteobserve.LearningCandidateV1) error {
+ if c.SchemaVersion == "" {
+ c.SchemaVersion = asymptoteobserve.LearningSchemaVersion
+ }
+ now := nowString()
+ if c.CreatedAt == "" {
+ c.CreatedAt = now
+ }
+ c.UpdatedAt = now
+ if c.State == "" {
+ c.State = asymptoteobserve.LearningCandidateStateCandidate
+ }
+ if c.ID == "" {
+ c.ID = CandidateID(c)
+ }
+ data, err := json.Marshal(c)
+ if err != nil {
+ return err
+ }
+ db, err := s.db()
+ if err != nil {
+ return err
+ }
+ defer db.Close()
+ _, err = db.Exec(`INSERT INTO candidates (id, project_id, state, kind, title, source_evaluation_id, memory_id, created_at, updated_at, candidate_json)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT(id) DO UPDATE SET state = excluded.state, kind = excluded.kind, title = excluded.title, memory_id = excluded.memory_id, updated_at = excluded.updated_at, candidate_json = excluded.candidate_json`,
+ c.ID, c.Project.ID, c.State, c.Kind, c.Title, c.SourceEvaluationID, c.MemoryID, c.CreatedAt, c.UpdatedAt, string(data))
+ return err
+}
+
+func (s *Store) ListCandidates(query Query) ([]asymptoteobserve.LearningCandidateV1, error) {
+ db, err := s.db()
+ if err != nil {
+ return nil, err
+ }
+ defer db.Close()
+ where, args := queryWhere(query, "project_id", "state", "kind")
+ rows, err := db.Query(`SELECT candidate_json FROM candidates`+where+` ORDER BY updated_at DESC LIMIT ? OFFSET ?`, append(args, normalizeLimit(query.Limit), offset(query))...)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ var out []asymptoteobserve.LearningCandidateV1
+ for rows.Next() {
+ var raw string
+ if err := rows.Scan(&raw); err != nil {
+ return nil, err
+ }
+ var value asymptoteobserve.LearningCandidateV1
+ if err := json.Unmarshal([]byte(raw), &value); err != nil {
+ return nil, err
+ }
+ if query.Q == "" || matchesText(query.Q, value.ID, value.Title, value.Body, value.Kind, value.State) {
+ out = append(out, value)
+ }
+ }
+ return out, rows.Err()
+}
+
+func (s *Store) GetCandidate(id string) (asymptoteobserve.LearningCandidateV1, bool, error) {
+ var raw string
+ ok, err := s.getJSON(`SELECT candidate_json FROM candidates WHERE id = ?`, id, &raw)
+ if err != nil || !ok {
+ return asymptoteobserve.LearningCandidateV1{}, ok, err
+ }
+ var value asymptoteobserve.LearningCandidateV1
+ return value, true, json.Unmarshal([]byte(raw), &value)
+}
+
+func (s *Store) PutMemory(m asymptoteobserve.LearningMemoryV1) error {
+ if m.SchemaVersion == "" {
+ m.SchemaVersion = asymptoteobserve.LearningSchemaVersion
+ }
+ now := nowString()
+ if m.CreatedAt == "" {
+ m.CreatedAt = now
+ }
+ m.UpdatedAt = now
+ if m.ID == "" {
+ m.ID = MemoryID(m)
+ }
+ data, err := json.Marshal(m)
+ if err != nil {
+ return err
+ }
+ db, err := s.db()
+ if err != nil {
+ return err
+ }
+ defer db.Close()
+ _, err = db.Exec(`INSERT INTO memories (id, candidate_id, project_id, kind, title, created_at, updated_at, superseded_by, memory_json)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT(id) DO UPDATE SET title = excluded.title, updated_at = excluded.updated_at, superseded_by = excluded.superseded_by, memory_json = excluded.memory_json`,
+ m.ID, m.CandidateID, m.Project.ID, m.Kind, m.Title, m.CreatedAt, m.UpdatedAt, m.SupersededBy, string(data))
+ return err
+}
+
+func (s *Store) ListMemories(query Query) ([]asymptoteobserve.LearningMemoryV1, error) {
+ db, err := s.db()
+ if err != nil {
+ return nil, err
+ }
+ defer db.Close()
+ where, args := queryWhere(query, "project_id", "", "kind")
+ if where == "" {
+ where = ` WHERE (superseded_by IS NULL OR superseded_by = '')`
+ } else {
+ where += ` AND (superseded_by IS NULL OR superseded_by = '')`
+ }
+ rows, err := db.Query(`SELECT memory_json FROM memories`+where+` ORDER BY updated_at DESC LIMIT ? OFFSET ?`, append(args, normalizeLimit(query.Limit), offset(query))...)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ var out []asymptoteobserve.LearningMemoryV1
+ for rows.Next() {
+ var raw string
+ if err := rows.Scan(&raw); err != nil {
+ return nil, err
+ }
+ var value asymptoteobserve.LearningMemoryV1
+ if err := json.Unmarshal([]byte(raw), &value); err != nil {
+ return nil, err
+ }
+ if query.Q == "" || matchesText(query.Q, value.ID, value.Title, value.Body, value.Kind) {
+ out = append(out, value)
+ }
+ }
+ return out, rows.Err()
+}
+
+func (s *Store) GetMemory(id string) (asymptoteobserve.LearningMemoryV1, bool, error) {
+ var raw string
+ ok, err := s.getJSON(`SELECT memory_json FROM memories WHERE id = ?`, id, &raw)
+ if err != nil || !ok {
+ return asymptoteobserve.LearningMemoryV1{}, ok, err
+ }
+ var value asymptoteobserve.LearningMemoryV1
+ return value, true, json.Unmarshal([]byte(raw), &value)
+}
+
+func (s *Store) getJSON(query, id string, dest *string) (bool, error) {
+ db, err := s.db()
+ if err != nil {
+ return false, err
+ }
+ defer db.Close()
+ err = db.QueryRow(query, id).Scan(dest)
+ if err == sql.ErrNoRows {
+ return false, nil
+ }
+ return err == nil, err
+}
+
+func EvaluationID(e asymptoteobserve.LearningEvaluationV1) string {
+ return idFor("eval", e.Project.ID, e.Trace.ID, e.RubricHash)
+}
+
+func CandidateID(c asymptoteobserve.LearningCandidateV1) string {
+ var evidence []string
+ for _, item := range c.Evidence {
+ evidence = append(evidence, item.TraceID, strings.Join(item.EventIDs, ","))
+ }
+ return idFor("candidate", c.Project.ID, c.Kind, c.Title, c.Body, strings.Join(evidence, "|"))
+}
+
+func MemoryID(m asymptoteobserve.LearningMemoryV1) string {
+ return idFor("memory", m.Project.ID, m.CandidateID, m.Kind, m.Title)
+}
+
+func idFor(prefix string, values ...string) string {
+ sum := sha256.Sum256([]byte(strings.Join(values, "\x00")))
+ return prefix + "_" + hex.EncodeToString(sum[:16])
+}
+
+func queryWhere(query Query, projectColumn, stateColumn, kindColumn string) (string, []interface{}) {
+ var clauses []string
+ var args []interface{}
+ projectID := strings.TrimSpace(query.ProjectID)
+ if projectID == "" && query.ProjectPath != "" {
+ if project, err := ResolveProject(query.ProjectPath); err == nil {
+ projectID = project.ID
+ }
+ }
+ if projectID != "" && projectColumn != "" {
+ clauses = append(clauses, projectColumn+" = ?")
+ args = append(args, projectID)
+ }
+ if query.State != "" && stateColumn != "" {
+ clauses = append(clauses, stateColumn+" = ?")
+ args = append(args, query.State)
+ }
+ if query.Kind != "" && kindColumn != "" {
+ clauses = append(clauses, kindColumn+" = ?")
+ args = append(args, query.Kind)
+ }
+ if len(clauses) == 0 {
+ return "", args
+ }
+ return " WHERE " + strings.Join(clauses, " AND "), args
+}
+
+func normalizeLimit(limit int) int {
+ if limit <= 0 {
+ return 50
+ }
+ if limit > 500 {
+ return 500
+ }
+ return limit
+}
+
+func offset(query Query) int {
+ page := query.Page
+ if page <= 1 {
+ return 0
+ }
+ return (page - 1) * normalizeLimit(query.Limit)
+}
+
+func matchesText(q string, values ...string) bool {
+ terms := strings.Fields(strings.ToLower(q))
+ if len(terms) == 0 {
+ return true
+ }
+ haystack := strings.ToLower(strings.Join(values, "\n"))
+ for _, term := range terms {
+ if !strings.Contains(haystack, term) {
+ return false
+ }
+ }
+ return true
+}
+
+func fileSize(path string) int64 {
+ info, err := os.Stat(path)
+ if err != nil {
+ return 0
+ }
+ return info.Size()
+}
+
+func nowString() string {
+ return time.Now().UTC().Format(time.RFC3339Nano)
+}
+
+func firstNonEmpty(values ...string) string {
+ for _, value := range values {
+ if strings.TrimSpace(value) != "" {
+ return strings.TrimSpace(value)
+ }
+ }
+ return ""
+}
diff --git a/cli/beacon/internal/learning/store_test.go b/cli/beacon/internal/learning/store_test.go
new file mode 100644
index 00000000..6fd9e84d
--- /dev/null
+++ b/cli/beacon/internal/learning/store_test.go
@@ -0,0 +1,149 @@
+package learning
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+)
+
+func TestPathForRuntimeLogUsesEndpointBaseDir(t *testing.T) {
+ got := PathForRuntimeLog(filepath.Join("/tmp", "beacon", "logs", "runtime.jsonl"))
+ want := filepath.Join("/tmp", "beacon", "memory.db")
+ if got != want {
+ t.Fatalf("PathForRuntimeLog = %s, want %s", got, want)
+ }
+}
+
+func TestResolveProjectUsesGitRootAndOrigin(t *testing.T) {
+ root := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(root, ".git"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(root, ".git", "HEAD"), []byte("ref: refs/heads/feature\n"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(root, ".git", "config"), []byte("[remote \"origin\"]\n\turl = https://github.com/acme/repo.git\n"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ child := filepath.Join(root, "sub", "dir")
+ if err := os.MkdirAll(child, 0755); err != nil {
+ t.Fatal(err)
+ }
+ project, err := ResolveProject(child)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if project.Path != root {
+ t.Fatalf("project path = %s, want %s", project.Path, root)
+ }
+ if project.RemoteURL != "https://github.com/acme/repo.git" {
+ t.Fatalf("remote = %q", project.RemoteURL)
+ }
+ if project.Branch != "feature" {
+ t.Fatalf("branch = %q", project.Branch)
+ }
+ if project.ID == "" {
+ t.Fatal("project ID is empty")
+ }
+}
+
+func TestStorePersistsLearningArtifacts(t *testing.T) {
+ store := Open(filepath.Join(t.TempDir(), "memory.db"))
+ project := asymptoteobserve.LearningProjectV1{ID: "project-1", Path: "/repo"}
+ eval := asymptoteobserve.LearningEvaluationV1{
+ ID: "eval-1",
+ Status: asymptoteobserve.LearningEvaluationStatusCompleted,
+ Project: project,
+ RubricVersion: "v1",
+ RubricHash: "sha256:rubric",
+ Evaluator: "jev",
+ Trace: asymptoteobserve.LearningTraceRefV1{ID: "trace-1", Title: "Fixed test flake"},
+ Questions: []asymptoteobserve.LearningEvaluationQuestionV1{
+ {ID: "task_success", Probability: 0.91, Confidence: 0.88},
+ },
+ Score: 0.91,
+ }
+ if err := store.PutEvaluation(eval); err != nil {
+ t.Fatalf("PutEvaluation: %v", err)
+ }
+ gotEval, ok, err := store.GetEvaluation("eval-1")
+ if err != nil || !ok {
+ t.Fatalf("GetEvaluation ok=%v err=%v", ok, err)
+ }
+ if gotEval.Trace.ID != "trace-1" || gotEval.SchemaVersion != asymptoteobserve.LearningSchemaVersion {
+ t.Fatalf("evaluation round trip = %#v", gotEval)
+ }
+
+ candidate := asymptoteobserve.LearningCandidateV1{
+ ID: "candidate-1",
+ State: asymptoteobserve.LearningCandidateStateCandidate,
+ Kind: asymptoteobserve.LearningMemoryKindDebuggingPattern,
+ Title: "Retry package smoke after flaky network",
+ Body: "When the package smoke fails on a transient fetch, rerun once after checking the error.",
+ Project: project,
+ SourceEvaluationID: "eval-1",
+ Evidence: []asymptoteobserve.LearningEvidenceV1{{TraceID: "trace-1", EventIDs: []string{"event-1"}}},
+ }
+ if err := store.PutCandidate(candidate); err != nil {
+ t.Fatalf("PutCandidate: %v", err)
+ }
+ candidates, err := store.ListCandidates(Query{ProjectID: "project-1", State: asymptoteobserve.LearningCandidateStateCandidate})
+ if err != nil {
+ t.Fatalf("ListCandidates: %v", err)
+ }
+ if len(candidates) != 1 || candidates[0].ID != "candidate-1" {
+ t.Fatalf("candidates = %#v", candidates)
+ }
+
+ memory := asymptoteobserve.LearningMemoryV1{
+ ID: "memory-1",
+ CandidateID: "candidate-1",
+ Kind: candidate.Kind,
+ Title: candidate.Title,
+ Body: candidate.Body,
+ Project: project,
+ Evidence: candidate.Evidence,
+ }
+ if err := store.PutMemory(memory); err != nil {
+ t.Fatalf("PutMemory: %v", err)
+ }
+ memories, err := store.ListMemories(Query{ProjectID: "project-1", Q: "package smoke"})
+ if err != nil {
+ t.Fatalf("ListMemories: %v", err)
+ }
+ if len(memories) != 1 || memories[0].ID != "memory-1" {
+ t.Fatalf("memories = %#v", memories)
+ }
+ status, err := store.Status()
+ if err != nil {
+ t.Fatalf("Status: %v", err)
+ }
+ if status.Evaluations != 1 || status.Candidates != 1 || status.ApprovedMemories != 1 {
+ t.Fatalf("status = %#v", status)
+ }
+}
+
+func TestStoreScopesByProject(t *testing.T) {
+ store := Open(filepath.Join(t.TempDir(), "memory.db"))
+ for _, projectID := range []string{"p1", "p2"} {
+ if err := store.PutMemory(asymptoteobserve.LearningMemoryV1{
+ ID: "memory-" + projectID,
+ CandidateID: "candidate-" + projectID,
+ Kind: asymptoteobserve.LearningMemoryKindConvention,
+ Title: "Project convention",
+ Body: "Use the local convention.",
+ Project: asymptoteobserve.LearningProjectV1{ID: projectID},
+ }); err != nil {
+ t.Fatal(err)
+ }
+ }
+ memories, err := store.ListMemories(Query{ProjectID: "p1"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(memories) != 1 || memories[0].Project.ID != "p1" {
+ t.Fatalf("project scoped memories = %#v", memories)
+ }
+}
diff --git a/images/beacon-hero.png b/images/beacon-hero.png
index c67c11bb..ef4613df 100644
Binary files a/images/beacon-hero.png and b/images/beacon-hero.png differ
diff --git a/pkg/asymptoteobserve/learning.go b/pkg/asymptoteobserve/learning.go
new file mode 100644
index 00000000..ed03573e
--- /dev/null
+++ b/pkg/asymptoteobserve/learning.go
@@ -0,0 +1,122 @@
+package asymptoteobserve
+
+const LearningSchemaVersion = "beacon.learning.v1"
+
+const (
+ LearningEvaluationStatusDryRun = "dry_run"
+ LearningEvaluationStatusCompleted = "completed"
+ LearningEvaluationStatusFailed = "failed"
+)
+
+const (
+ LearningCandidateStateCandidate = "candidate"
+ LearningCandidateStateApproved = "approved"
+ LearningCandidateStateRejected = "rejected"
+ LearningCandidateStateSuperseded = "superseded"
+)
+
+const (
+ LearningMemoryKindWorkflow = "workflow"
+ LearningMemoryKindCorrection = "correction"
+ LearningMemoryKindDebuggingPattern = "debugging_pattern"
+ LearningMemoryKindGotcha = "gotcha"
+ LearningMemoryKindConvention = "convention"
+)
+
+type LearningProjectV1 struct {
+ ID string `json:"id"`
+ Path string `json:"path,omitempty"`
+ RemoteURL string `json:"remote_url,omitempty"`
+ Branch string `json:"branch,omitempty"`
+}
+
+type LearningTraceRefV1 struct {
+ ID string `json:"id"`
+ Title string `json:"title,omitempty"`
+ Harness TraceHarnessV1 `json:"harness"`
+ Session *TraceSessionV1 `json:"session,omitempty"`
+ Repo *TraceRepositoryV1 `json:"repository,omitempty"`
+ EventIDs []string `json:"event_ids,omitempty"`
+}
+
+type LearningEvaluationQuestionV1 struct {
+ ID string `json:"id"`
+ Prompt string `json:"prompt"`
+ Probability float64 `json:"probability"`
+ Confidence float64 `json:"confidence,omitempty"`
+ Reason string `json:"reason,omitempty"`
+}
+
+type LearningEvaluationV1 struct {
+ SchemaVersion string `json:"schema_version"`
+ ID string `json:"id"`
+ Status string `json:"status"`
+ CreatedAt string `json:"created_at"`
+ UpdatedAt string `json:"updated_at"`
+ Project LearningProjectV1 `json:"project"`
+ RubricVersion string `json:"rubric_version"`
+ RubricHash string `json:"rubric_hash"`
+ Evaluator string `json:"evaluator"`
+ DryRun bool `json:"dry_run,omitempty"`
+ Trace LearningTraceRefV1 `json:"trace"`
+ Questions []LearningEvaluationQuestionV1 `json:"questions,omitempty"`
+ Score float64 `json:"score,omitempty"`
+ CostEstimateUSD float64 `json:"cost_estimate_usd,omitempty"`
+ Error string `json:"error,omitempty"`
+}
+
+type LearningEvidenceV1 struct {
+ TraceID string `json:"trace_id"`
+ EventIDs []string `json:"event_ids,omitempty"`
+ Summary string `json:"summary,omitempty"`
+ Redacted bool `json:"redacted,omitempty"`
+ Truncated bool `json:"truncated,omitempty"`
+ ContentHash string `json:"content_hash,omitempty"`
+}
+
+type LearningCandidateV1 struct {
+ SchemaVersion string `json:"schema_version"`
+ ID string `json:"id"`
+ MemoryID string `json:"memory_id,omitempty"`
+ State string `json:"state"`
+ Kind string `json:"kind"`
+ Title string `json:"title"`
+ Body string `json:"body"`
+ Applicability string `json:"applicability,omitempty"`
+ Tags []string `json:"tags,omitempty"`
+ Project LearningProjectV1 `json:"project"`
+ SourceEvaluationID string `json:"source_evaluation_id,omitempty"`
+ Evidence []LearningEvidenceV1 `json:"evidence"`
+ CreatedAt string `json:"created_at"`
+ UpdatedAt string `json:"updated_at"`
+ ApprovedAt string `json:"approved_at,omitempty"`
+ RejectedAt string `json:"rejected_at,omitempty"`
+ SupersededAt string `json:"superseded_at,omitempty"`
+ SupersededBy string `json:"superseded_by,omitempty"`
+ ReviewReason string `json:"review_reason,omitempty"`
+}
+
+type LearningMemoryV1 struct {
+ SchemaVersion string `json:"schema_version"`
+ ID string `json:"id"`
+ CandidateID string `json:"candidate_id"`
+ Kind string `json:"kind"`
+ Title string `json:"title"`
+ Body string `json:"body"`
+ Applicability string `json:"applicability,omitempty"`
+ Tags []string `json:"tags,omitempty"`
+ Project LearningProjectV1 `json:"project"`
+ Evidence []LearningEvidenceV1 `json:"evidence"`
+ CreatedAt string `json:"created_at"`
+ UpdatedAt string `json:"updated_at"`
+ SupersededBy string `json:"superseded_by,omitempty"`
+}
+
+type LearningStatusV1 struct {
+ SchemaVersion string `json:"schema_version"`
+ Path string `json:"path"`
+ Evaluations int `json:"evaluations"`
+ Candidates int `json:"candidates"`
+ ApprovedMemories int `json:"approved_memories"`
+ SizeBytes int64 `json:"size_bytes"`
+}