Skip to content

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

Resolve relayed memory project paths without reading the filesystem

Claude1424c56f93b4
diff --git a/cli/beacon/internal/learning/store.go b/cli/beacon/internal/learning/store.go
index 9da5a0af..f05e01b7 100644
--- a/cli/beacon/internal/learning/store.go
+++ b/cli/beacon/internal/learning/store.go
@@ -25,6 +25,14 @@ type Store struct {
dbPath string
}
+// Query selects stored learning records.
+//
+// ProjectPath is the trust boundary of this package. The CLI resolves an
+// operator-supplied path itself and only ever sets ProjectID; ProjectPath is
+// set solely by relayed callers such as the local dashboard and the MCP
+// server, whose value arrives in a request. It is therefore matched against
+// the projects the store already holds (Store.ProjectIDForPath) rather than
+// resolved against the filesystem.
type Query struct {
ProjectPath string
ProjectID string
@@ -147,6 +155,10 @@ func ensureSchema(db *sql.DB) error {
return nil
}
+// ResolveProject reads git metadata to describe the project at path. It is for
+// operator-supplied paths only: the CLI's --project flag and the process
+// working directory. A path that arrives in a request is resolved by
+// Store.ProjectIDForPath instead, which does not touch the filesystem.
func ResolveProject(path string) (asymptoteobserve.LearningProjectV1, error) {
if strings.TrimSpace(path) == "" {
var err error
@@ -181,6 +193,101 @@ func ProjectID(project asymptoteobserve.LearningProjectV1) string {
return "sha256:" + hex.EncodeToString(sum[:])
}
+// KnownProjects returns the distinct projects this store holds records for.
+func (s *Store) KnownProjects() ([]asymptoteobserve.LearningProjectV1, error) {
+ db, err := s.db()
+ if err != nil {
+ return nil, err
+ }
+ defer db.Close()
+ rows, err := db.Query(`
+ SELECT project_id, json_extract(evaluation_json, '$.project.path') FROM evaluations
+ UNION
+ SELECT project_id, json_extract(candidate_json, '$.project.path') FROM candidates
+ UNION
+ SELECT project_id, json_extract(memory_json, '$.project.path') FROM memories`)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ var out []asymptoteobserve.LearningProjectV1
+ for rows.Next() {
+ var id string
+ var path sql.NullString
+ if err := rows.Scan(&id, &path); err != nil {
+ return nil, err
+ }
+ if strings.TrimSpace(id) == "" {
+ continue
+ }
+ out = append(out, asymptoteobserve.LearningProjectV1{ID: id, Path: strings.TrimSpace(path.String)})
+ }
+ return out, rows.Err()
+}
+
+// ProjectIDForPath maps a project path supplied by a relayed caller onto the
+// ID of a project this store already holds records for, choosing the most
+// specific known project that contains the path. It deliberately never touches
+// the filesystem: the dashboard and the MCP server pass a value that arrives in
+// a request, and such a value must not reach a path expression. A path no
+// stored project covers resolves to the same deterministic ID a plain
+// directory would get, which matches no rows, so an unknown project keeps
+// scoping the query to nothing rather than widening it to every project.
+func (s *Store) ProjectIDForPath(path string) (string, error) {
+ trimmed := strings.TrimSpace(path)
+ if trimmed == "" {
+ return "", nil
+ }
+ cleaned := filepath.Clean(trimmed)
+ projects, err := s.KnownProjects()
+ if err != nil {
+ return "", err
+ }
+ var best asymptoteobserve.LearningProjectV1
+ for _, project := range projects {
+ if project.Path == "" || !pathWithin(cleaned, project.Path) {
+ continue
+ }
+ if len(project.Path) > len(best.Path) {
+ best = project
+ }
+ }
+ if best.ID != "" {
+ return best.ID, nil
+ }
+ return ProjectID(asymptoteobserve.LearningProjectV1{Path: cleaned}), nil
+}
+
+// scopeQuery resolves a relayed ProjectPath into a project ID once, before any
+// statement runs. A caller that already knows the ID keeps it.
+func (s *Store) scopeQuery(query Query) (Query, error) {
+ if strings.TrimSpace(query.ProjectID) != "" || strings.TrimSpace(query.ProjectPath) == "" {
+ query.ProjectPath = ""
+ return query, nil
+ }
+ id, err := s.ProjectIDForPath(query.ProjectPath)
+ if err != nil {
+ return Query{}, err
+ }
+ query.ProjectID = id
+ query.ProjectPath = ""
+ return query, nil
+}
+
+// pathWithin reports whether path is root or sits under it. Comparison is
+// case-insensitive to match how ProjectID folds case when it hashes a path.
+func pathWithin(path, root string) bool {
+ path = filepath.ToSlash(filepath.Clean(path))
+ root = filepath.ToSlash(filepath.Clean(root))
+ if strings.EqualFold(path, root) {
+ return true
+ }
+ if !strings.HasSuffix(root, "/") {
+ root += "/"
+ }
+ return strings.HasPrefix(strings.ToLower(path), strings.ToLower(root))
+}
+
func findGitRoot(path string) string {
current := filepath.Clean(path)
info, err := os.Stat(current)
@@ -281,6 +388,10 @@ func (s *Store) PutEvaluation(e asymptoteobserve.LearningEvaluationV1) error {
}
func (s *Store) ListEvaluations(query Query) ([]asymptoteobserve.LearningEvaluationV1, error) {
+ query, err := s.scopeQuery(query)
+ if err != nil {
+ return nil, err
+ }
db, err := s.db()
if err != nil {
return nil, err
@@ -351,6 +462,10 @@ func (s *Store) PutCandidate(c asymptoteobserve.LearningCandidateV1) error {
}
func (s *Store) ListCandidates(query Query) ([]asymptoteobserve.LearningCandidateV1, error) {
+ query, err := s.scopeQuery(query)
+ if err != nil {
+ return nil, err
+ }
db, err := s.db()
if err != nil {
return nil, err
@@ -418,6 +533,10 @@ func (s *Store) PutMemory(m asymptoteobserve.LearningMemoryV1) error {
}
func (s *Store) ListMemories(query Query) ([]asymptoteobserve.LearningMemoryV1, error) {
+ query, err := s.scopeQuery(query)
+ if err != nil {
+ return nil, err
+ }
db, err := s.db()
if err != nil {
return nil, err
@@ -523,11 +642,6 @@ func queryWhere(query Query, projectColumn, stateColumn, kindColumn string) (str
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)
diff --git a/cli/beacon/internal/learning/store_test.go b/cli/beacon/internal/learning/store_test.go
index 11e09e33..7f2f6faa 100644
--- a/cli/beacon/internal/learning/store_test.go
+++ b/cli/beacon/internal/learning/store_test.go
@@ -233,3 +233,121 @@ func setMemoryUpdatedAt(t *testing.T, store *Store, id, updatedAt string) {
t.Fatal(err)
}
}
+
+func TestProjectIDForPathMatchesTheMostSpecificKnownProject(t *testing.T) {
+ store := Open(filepath.Join(t.TempDir(), "memory.db"))
+ outer := asymptoteobserve.LearningProjectV1{ID: "project-outer", Path: "/work"}
+ inner := asymptoteobserve.LearningProjectV1{ID: "project-inner", Path: "/work/repo"}
+ for _, project := range []asymptoteobserve.LearningProjectV1{outer, inner} {
+ memory := asymptoteobserve.LearningMemoryV1{
+ ID: "memory-" + project.ID,
+ CandidateID: "candidate-" + project.ID,
+ Kind: asymptoteobserve.LearningMemoryKindConvention,
+ Title: "Title for " + project.ID,
+ Project: project,
+ }
+ if err := store.PutMemory(memory); err != nil {
+ t.Fatalf("PutMemory: %v", err)
+ }
+ }
+
+ got, err := store.ProjectIDForPath("/work/repo/cli/beacon")
+ if err != nil {
+ t.Fatalf("ProjectIDForPath: %v", err)
+ }
+ if got != inner.ID {
+ t.Fatalf("project ID = %q, want %q", got, inner.ID)
+ }
+
+ unknown, err := store.ProjectIDForPath("/elsewhere")
+ if err != nil {
+ t.Fatalf("ProjectIDForPath: %v", err)
+ }
+ if want := ProjectID(asymptoteobserve.LearningProjectV1{Path: "/elsewhere"}); unknown != want {
+ t.Fatalf("unknown project ID = %q, want the path-derived ID %q", unknown, want)
+ }
+}
+
+func TestListMemoriesScopesARelayedProjectPathToOneProject(t *testing.T) {
+ store := Open(filepath.Join(t.TempDir(), "memory.db"))
+ for _, project := range []asymptoteobserve.LearningProjectV1{
+ {ID: "project-a", Path: "/work/a"},
+ {ID: "project-b", Path: "/work/b"},
+ } {
+ memory := asymptoteobserve.LearningMemoryV1{
+ ID: "memory-" + project.ID,
+ CandidateID: "candidate-" + project.ID,
+ Kind: asymptoteobserve.LearningMemoryKindConvention,
+ Title: "Title for " + project.ID,
+ Project: project,
+ }
+ if err := store.PutMemory(memory); err != nil {
+ t.Fatalf("PutMemory: %v", err)
+ }
+ }
+
+ memories, err := store.ListMemories(Query{ProjectPath: "/work/a/sub"})
+ if err != nil {
+ t.Fatalf("ListMemories: %v", err)
+ }
+ if len(memories) != 1 || memories[0].Project.ID != "project-a" {
+ t.Fatalf("memories = %#v, want only project-a", memories)
+ }
+
+ none, err := store.ListMemories(Query{ProjectPath: "/work/c"})
+ if err != nil {
+ t.Fatalf("ListMemories: %v", err)
+ }
+ if len(none) != 0 {
+ t.Fatalf("unknown project path returned %d memories, want none", len(none))
+ }
+}
+
+// A relayed ProjectPath arrives in a request, so resolving it must not read
+// the filesystem. The store holds the project under the ID its origin URL
+// produces; if the query still resolved the path through .git, it would find
+// that same ID and return the row.
+func TestRelayedProjectPathDoesNotReadGitMetadata(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/main\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)
+ }
+ resolved, err := ResolveProject(root)
+ if err != nil {
+ t.Fatal(err)
+ }
+ project := asymptoteobserve.LearningProjectV1{ID: resolved.ID, RemoteURL: resolved.RemoteURL}
+
+ store := Open(filepath.Join(t.TempDir(), "memory.db"))
+ if err := store.PutMemory(asymptoteobserve.LearningMemoryV1{
+ ID: "memory-1",
+ CandidateID: "candidate-1",
+ Kind: asymptoteobserve.LearningMemoryKindConvention,
+ Title: "Recorded against the origin URL",
+ Project: project,
+ }); err != nil {
+ t.Fatalf("PutMemory: %v", err)
+ }
+
+ memories, err := store.ListMemories(Query{ProjectPath: root})
+ if err != nil {
+ t.Fatalf("ListMemories: %v", err)
+ }
+ if len(memories) != 0 {
+ t.Fatalf("relayed project path resolved through git metadata: %#v", memories)
+ }
+
+ byID, err := store.ListMemories(Query{ProjectID: project.ID})
+ if err != nil {
+ t.Fatalf("ListMemories: %v", err)
+ }
+ if len(byID) != 1 {
+ t.Fatalf("memories by project ID = %d, want 1", len(byID))
+ }
+}
diff --git a/docs/cli/mcp.mdx b/docs/cli/mcp.mdx
index 8b397e73..99ab9e03 100644
--- a/docs/cli/mcp.mdx
+++ b/docs/cli/mcp.mdx
@@ -52,7 +52,10 @@ Search and summary tools accept filters such as `since`, `until`, `limit`, `q`,
Memory tools read approved project memory only. They do not run Jev, approve
candidates, install Agent Skills, or expose pending/rejected candidates. Use
`project_id` or the MCP client's project context when a client needs memory for a
-specific repository.
+specific repository. A `project` path sent by a client is matched against the
+projects the memory store already holds records for; Beacon does not read git
+metadata from a path a client supplies, so an unrecognized path returns nothing
+rather than widening the search.
## Flags