From 93efd82dce5aaa1b6cd461421716f179f5d10b3b Mon Sep 17 00:00:00 2001 From: Rudy Gault Date: Thu, 5 Nov 2026 09:05:04 +0100 Subject: [PATCH 1/2] fix: make index build --scan repeatable and cumulative Only the last --scan was used, silently. Every --scan folder is now scanned and added up (with a list file and --depth); a repository reached through overlapping folders or a symlink is indexed once. A single --scan prints the same scan line as before. --- README.md | 1 + cmd/relai/args.go | 10 +++ cmd/relai/main.go | 29 +++----- cmd/relai/scan.go | 50 +++++++++++++ cmd/relai/scan_test.go | 162 +++++++++++++++++++++++++++++++++++++++++ docs/HANDOFF.md | 2 +- 6 files changed, 234 insertions(+), 20 deletions(-) create mode 100644 cmd/relai/scan.go create mode 100644 cmd/relai/scan_test.go diff --git a/README.md b/README.md index dd975b8..607a1b5 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ You do not have to write the list of your projects by hand: ```sh relai index build --scan ~/projects # every git repo of the folder (symlinks included); says which need `relai init` +relai index build --scan ~/work --scan ~/oss # --scan is repeatable and adds up (also with a list file and --depth); a repo reached by several --scan folders is indexed once relai discover --links ~/projects # the mentions between them: who points at whom, and how strongly relai discover --links ~/projects --depth 2 # also look one folder deeper (nested repos are named by path) relai discover --links ~/projects --prompt # instructions for your agent to check each mention with you diff --git a/cmd/relai/args.go b/cmd/relai/args.go index c700617..ceba1f9 100644 --- a/cmd/relai/args.go +++ b/cmd/relai/args.go @@ -52,3 +52,13 @@ func containsDashDash(args []string) bool { } return false } + +// stringList is a flag that can be given several times; every value is kept, in order. +type stringList []string + +func (l *stringList) String() string { return strings.Join(*l, ",") } + +func (l *stringList) Set(v string) error { + *l = append(*l, v) + return nil +} diff --git a/cmd/relai/main.go b/cmd/relai/main.go index 5a78edb..8c079bc 100644 --- a/cmd/relai/main.go +++ b/cmd/relai/main.go @@ -33,8 +33,9 @@ Usage: relai handoff [dir] print the current state of the work (≤ 2,000 tokens by default) relai discover [dir] list what the project already gives agents (skills, hooks, specs, docs…); --prompt prints instructions for your agent to draft .relai/notes.md - relai index build [-o ] [--scan ] [] - read each repo path/URL in (and/or every git repo in --scan ) + relai index build [-o ] [--scan ]... [--depth N] [] + read each repo path/URL in (and/or every git repo in each --scan ; + repeat --scan to cover several folders, a repo reached by several of them is indexed once) and collect their relai.yaml into an index (default: .relai/index.json) relai discover --links [--depth N] [--propose [--min-weak N] | --prompt] [--json] mentions between the git repos of (strong: remote URL or relative path; @@ -154,11 +155,12 @@ func main() { } fs := flag.NewFlagSet("index build", flag.ExitOnError) out := fs.String("o", defaultIndexPath, "write the index to this file") - scanDir := fs.String("scan", "", "find the git repositories in this folder instead of (or in addition to) a list file") + var scanDirs stringList + fs.Var(&scanDirs, "scan", "find the git repositories in this folder instead of (or in addition to) a list file; repeatable") scanDepth := fs.Int("depth", 1, "with --scan: how many folder levels to look down for repositories") _ = fs.Parse(reorderArgs(fs, os.Args[3:])) - if fs.NArg() < 1 && *scanDir == "" { - fmt.Fprintln(os.Stderr, "usage: relai index build [-o ] [--scan ] []") + if fs.NArg() < 1 && len(scanDirs) == 0 { + fmt.Fprintln(os.Stderr, "usage: relai index build [-o ] [--scan ]... []") os.Exit(2) } var sources []string @@ -167,21 +169,10 @@ func main() { sources, err = index.ReadList(fs.Arg(0)) exitOn(err) } - if *scanDir != "" { - repos, err := scan.FindDepth(*scanDir, *scanDepth) + if len(scanDirs) > 0 { + scanned, err := scanSources(scanDirs, *scanDepth, os.Stdout) exitOn(err) - var without []string - for _, r := range repos { - sources = append(sources, r.Path) - if !r.Relai { - without = append(without, r.Name) - } - } - fmt.Printf("scan: %d repositories in %s, %d with a relai.yaml", len(repos), *scanDir, len(repos)-len(without)) - if len(without) > 0 { - fmt.Printf("; without one (skipped, run `relai init` there to include them): %s", strings.Join(without, ", ")) - } - fmt.Println() + sources = append(sources, scanned...) } entries, skipped := index.Build(sources) for _, sk := range skipped { diff --git a/cmd/relai/scan.go b/cmd/relai/scan.go new file mode 100644 index 0000000..3d4e66f --- /dev/null +++ b/cmd/relai/scan.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "io" + "path/filepath" + "strings" + + "github.com/ECG-Rudy-Glt/RELAI/internal/scan" +) + +// scanSources finds the git repositories of every folder in dirs (depth levels down) and returns their +// paths, in the order found. A repository reached more than once, through two folders that overlap or +// through a symbolic link, is returned once, by the first folder that found it. One "scan:" line per +// folder is written to w; with several folders, the count is of the repositories that folder added. +func scanSources(dirs []string, depth int, w io.Writer) ([]string, error) { + var sources []string + seen := map[string]bool{} + for _, dir := range dirs { + repos, err := scan.FindDepth(dir, depth) + if err != nil { + return nil, err + } + var kept []scan.Repo + for _, r := range repos { + key := r.Path + if real, err := filepath.EvalSymlinks(r.Path); err == nil { + key = real + } + if seen[key] { + continue + } + seen[key] = true + kept = append(kept, r) + } + var without []string + for _, r := range kept { + sources = append(sources, r.Path) + if !r.Relai { + without = append(without, r.Name) + } + } + fmt.Fprintf(w, "scan: %d repositories in %s, %d with a relai.yaml", len(kept), dir, len(kept)-len(without)) + if len(without) > 0 { + fmt.Fprintf(w, "; without one (skipped, run `relai init` there to include them): %s", strings.Join(without, ", ")) + } + fmt.Fprintln(w) + } + return sources, nil +} diff --git a/cmd/relai/scan_test.go b/cmd/relai/scan_test.go new file mode 100644 index 0000000..803f3fd --- /dev/null +++ b/cmd/relai/scan_test.go @@ -0,0 +1,162 @@ +package main + +import ( + "bytes" + "encoding/json" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// mkRepo creates a real git repository at dir/name with a relai.yaml (id = name), and returns its path. +func mkRepo(t *testing.T, dir, name string) string { + t.Helper() + p := filepath.Join(dir, name) + if err := os.MkdirAll(p, 0o755); err != nil { + t.Fatal(err) + } + if out, err := exec.Command("git", "-C", p, "init", "-q").CombinedOutput(); err != nil { + t.Skipf("git init: %v: %s", err, out) + } + yaml := "id: " + name + "\nsummary: test repo\n" + if err := os.WriteFile(filepath.Join(p, "relai.yaml"), []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } + return p +} + +func TestScanSourcesAccumulates(t *testing.T) { + root := t.TempDir() + a, b := filepath.Join(root, "a"), filepath.Join(root, "b") + ra := mkRepo(t, a, "one") + rb := mkRepo(t, b, "two") + var out bytes.Buffer + got, err := scanSources([]string{a, b}, 1, &out) + if err != nil { + t.Fatal(err) + } + if len(got) != 2 || got[0] != ra || got[1] != rb { + t.Fatalf("want [%s %s], got %v", ra, rb, got) + } + if n := strings.Count(out.String(), "scan: "); n != 2 { + t.Errorf("want one scan line per folder, got:\n%s", out.String()) + } +} + +func TestScanSourcesOverlapNoDuplicate(t *testing.T) { + root := t.TempDir() + r := mkRepo(t, root, "one") + mkRepo(t, root, "two") + link := filepath.Join(t.TempDir(), "link") + if err := os.Symlink(root, link); err != nil { + t.Skip("no symlinks:", err) + } + // the same folder twice, a symlink to it, and a folder that is itself the repository + got, err := scanSources([]string{root, root, link, r}, 1, &bytes.Buffer{}) + if err != nil { + t.Fatal(err) + } + if len(got) != 2 { + t.Fatalf("want 2 repositories once each, got %v", got) + } +} + +func TestScanSourcesSingleKeepsOutput(t *testing.T) { + root := t.TempDir() + mkRepo(t, root, "one") + p := filepath.Join(root, "bare") + if err := os.MkdirAll(p, 0o755); err != nil { + t.Fatal(err) + } + _ = exec.Command("git", "-C", p, "init", "-q").Run() + var out bytes.Buffer + if _, err := scanSources([]string{root}, 1, &out); err != nil { + t.Fatal(err) + } + want := "scan: 2 repositories in " + root + ", 1 with a relai.yaml; without one (skipped, run `relai init` there to include them): bare\n" + if out.String() != want { + t.Errorf("got %q\nwant %q", out.String(), want) + } +} + +func TestScanSourcesMissingFolder(t *testing.T) { + if _, err := scanSources([]string{filepath.Join(t.TempDir(), "nope")}, 1, &bytes.Buffer{}); err == nil { + t.Error("a missing folder must be an error") + } +} + +// runIndexBuild builds the real binary once per test and runs `relai index build` with args. +func runIndexBuild(t *testing.T, args ...string) (string, []string) { + t.Helper() + bin := filepath.Join(t.TempDir(), "relai") + if out, err := exec.Command("go", "build", "-o", bin, ".").CombinedOutput(); err != nil { + t.Fatalf("build: %v\n%s", err, out) + } + idx := filepath.Join(t.TempDir(), "index.json") + cmd := exec.Command(bin, append([]string{"index", "build", "-o", idx}, args...)...) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("index build: %v\n%s", err, out) + } + b, err := os.ReadFile(idx) + if err != nil { + t.Fatal(err) + } + var entries []struct { + Source string + } + if err := json.Unmarshal(b, &entries); err != nil { + t.Fatal(err) + } + var sources []string + for _, e := range entries { + sources = append(sources, e.Source) + } + return string(out), sources +} + +func TestIndexBuildRepeatedScan(t *testing.T) { + root := t.TempDir() + a, b := filepath.Join(root, "a"), filepath.Join(root, "b") + mkRepo(t, a, "one") + mkRepo(t, b, "two") + _, sources := runIndexBuild(t, "--scan", a, "--scan", b) + if len(sources) != 2 { + t.Fatalf("both folders must be indexed, got %v", sources) + } + // overlapping folders (root holds a and b at depth 2) must not duplicate + _, sources = runIndexBuild(t, "--scan", a, "--scan", root, "--depth", "2") + if len(sources) != 2 { + t.Fatalf("overlap must not duplicate, got %v", sources) + } +} + +func TestIndexBuildSingleScanUnchanged(t *testing.T) { + root := t.TempDir() + mkRepo(t, root, "one") + out, sources := runIndexBuild(t, "--scan", root) + if len(sources) != 1 { + t.Fatalf("got %v", sources) + } + if !strings.Contains(out, "scan: 1 repositories in "+root+", 1 with a relai.yaml\n") { + t.Errorf("scan line changed:\n%s", out) + } +} + +func TestIndexBuildScansWithListFile(t *testing.T) { + root := t.TempDir() + a, b := filepath.Join(root, "a"), filepath.Join(root, "b") + mkRepo(t, a, "one") + mkRepo(t, b, "two") + listed := mkRepo(t, filepath.Join(root, "c"), "three") + list := filepath.Join(root, "list.txt") + if err := os.WriteFile(list, []byte(listed+"\n"), 0o644); err != nil { + t.Fatal(err) + } + _, sources := runIndexBuild(t, "--scan", a, "--scan", b, list) + if len(sources) != 3 { + t.Fatalf("list file and both scans must add up, got %v", sources) + } +} diff --git a/docs/HANDOFF.md b/docs/HANDOFF.md index 05a711b..0e0ad15 100644 --- a/docs/HANDOFF.md +++ b/docs/HANDOFF.md @@ -20,7 +20,7 @@ no central service, no Backstage, no federation or SSO. | Area | State | |---|---| | Release | **v0.2.0** is published (6 platform archives + `checksums.txt`); `scripts/install.sh` verifies the SHA-256 before installing and never runs `sudo` on its own. `main` is ahead of it by the fixes below once they merge | -| CLI | Go, stdlib only, `cmd/relai`: `init` (AGENTS.md, relai.yaml, `.relai/notes.md`, and for Claude Code a `CLAUDE.md` plus a SessionStart hook; never overwrites; `--no-claude`), `handoff`, `discover`, `index build` / `find`, `index build --scan ` and `discover --links ` (find the repos of a folder and the mentions between them: evidence, never verdicts), `tidy` (close the tasks merged commits say they close, refresh the export, delete merged branches; run by the post-merge hook), `workspace` (one budgeted overview of all indexed projects, files only), `new