Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ the handoff is being evaluated with real agents in [`eval/`](eval/README.md).
Made to be easy: one binary, no server, nothing but git. Useful on day one for a single person or a
small team; the multi-project features (index, conventions) are optional layers.

**Who gets what.** Measured on a real set of 16 repositories of one developer working alone with agents:
`relai workspace` (the overview of every project, about 400 tokens) and the handoff are what pay off today.
The team board ([RELAI-TEAM](https://github.com/ECG-Rudy-Glt/RELAI-TEAM)) answers a problem that starts when
several people or agents work in parallel on the same projects; with a single human it adds little. Savings
were measured once per case: treat them as indications, not as a guarantee.

## How it fits together

```mermaid
Expand Down
5 changes: 4 additions & 1 deletion cmd/relai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ func main() {
for _, n := range r.Notes {
fmt.Println("note:", n)
}
if !*quiet && len(r.Closed)+len(r.DeletedBranches) == 0 && len(r.Notes) == 0 {
if !*quiet && p.Deferred > 0 {
fmt.Printf("%d task(s) have a closing commit on this branch; they will be closed once it is merged into %s.\n", p.Deferred, p.Default)
}
if !*quiet && len(r.Closed)+len(r.DeletedBranches) == 0 && len(r.Notes) == 0 && p.Deferred == 0 {
fmt.Println("nothing to do.")
}
case "workspace":
Expand Down
6 changes: 5 additions & 1 deletion internal/initrepo/initrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ limit=${RELAI_HOOK_TIMEOUT:-60}
if command -v timeout >/dev/null 2>&1; then
timeout "$limit" relai tidy --apply --quiet || true
else
relai tidy --apply --quiet || true
# no timeout(1) (macOS): a watchdog stops relai if it takes too long
relai tidy --apply --quiet >/dev/null 2>&1 & pid=$!
( sleep "$limit"; kill "$pid" 2>/dev/null ) >/dev/null 2>&1 & dog=$!
wait "$pid" 2>/dev/null
kill "$dog" 2>/dev/null
fi
exit 0
`
Expand Down
6 changes: 5 additions & 1 deletion internal/scan/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Link struct {
const minNameLength = 4

// generated or vendored files say nothing about what a person works with.
var excluded = []string{":!graphify-out", ":!node_modules", ":!vendor", ":!*.lock", ":!package-lock.json", ":!go.sum", ":!*.min.js", ":!*.svg"}
var excluded = []string{":!graphify-out", ":!node_modules", ":!vendor", ":!*.lock", ":!package-lock.json", ":!go.sum", ":!*.min.js", ":!*.svg", ":!*_test.go", ":!testdata", ":!*.test.*", ":!*.spec.*"}

// Links returns the mentions between the repositories, strongest first. A repository is never linked
// to itself.
Expand All @@ -40,6 +40,10 @@ func Links(repos []Repo) []Link {
if a.Path == b.Path {
continue
}
// Two clones of the same remote are one project, not a dependency.
if a.Slug != "" && a.Slug == b.Slug {
continue
}
l := Link{From: a.Name, To: b.Name}
strong := map[string]bool{}
if b.Slug != "" {
Expand Down
44 changes: 43 additions & 1 deletion internal/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Repo struct {
Agents bool `json:"agents"` // has AGENTS.md or CLAUDE.md
LastCommit string `json:"last_commit"` // YYYY-MM-DD
Slug string `json:"slug,omitempty"` // owner/name of the origin remote, when it has one
// Worktrees are the other working copies of this same repository found in the scanned folder
// (git worktrees). They are one repository, not several.
Worktrees []string `json:"worktrees,omitempty"`
}

// Find returns the git repositories directly inside dir (and dir itself if it is one), sorted by name.
Expand Down Expand Up @@ -96,13 +99,14 @@ func FindDepth(dir string, depth int) ([]Repo, error) {
} else {
walk(abs, 1)
}
paths, wt := mergeWorktrees(paths)
var out []Repo
for _, p := range paths {
name := filepath.Base(p)
if rel, err := filepath.Rel(abs, p); err == nil && rel != "." && strings.Contains(rel, string(filepath.Separator)) {
name = filepath.ToSlash(rel)
}
r := Repo{Name: name, Path: p}
r := Repo{Name: name, Path: p, Worktrees: wt[p]}
r.Relai = exists(filepath.Join(p, "relai.yaml"))
r.Tracker = exists(filepath.Join(p, ".beads"))
r.OpenSpec = exists(filepath.Join(p, "openspec"))
Expand All @@ -120,6 +124,44 @@ func FindDepth(dir string, depth int) ([]Repo, error) {
return out, nil
}

// mergeWorktrees keeps one path per repository: the working copies sharing a git directory (git
// worktrees) are one project. The main working copy wins (its .git is a folder); with only linked
// copies the first path in name order stays. The others are returned under the kept path.
func mergeWorktrees(paths []string) ([]string, map[string][]string) {
group := map[string][]string{}
var order []string
for _, p := range paths {
key := git(p, "rev-parse", "--path-format=absolute", "--git-common-dir")
if key == "" {
key = p
}
if _, ok := group[key]; !ok {
order = append(order, key)
}
group[key] = append(group[key], p)
}
var kept []string
extra := map[string][]string{}
for _, k := range order {
ps := group[k]
sort.Strings(ps)
main := ps[0]
for _, p := range ps {
if st, err := os.Stat(filepath.Join(p, ".git")); err == nil && st.IsDir() {
main = p
break
}
}
kept = append(kept, main)
for _, p := range ps {
if p != main {
extra[main] = append(extra[main], p)
}
}
}
return kept, extra
}

func isRepo(p string) bool { return exists(filepath.Join(p, ".git")) }

func exists(p string) bool { _, err := os.Stat(p); return err == nil }
Expand Down
25 changes: 25 additions & 0 deletions internal/scan/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,28 @@ func TestWeakMentionsInConfigFilesCountMoreThanInDocuments(t *testing.T) {
t.Error("--min-weak 3 should make three documents enough")
}
}

func TestWorktreesAreOneRepo(t *testing.T) {
root := t.TempDir()
main := filepath.Join(root, "app")
run := func(dir string, args ...string) {
t.Helper()
c := exec.Command("git", append([]string{"-C", dir, "-c", "user.name=t", "-c", "user.email=t@t"}, args...)...)
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v %s", args, err, out)
}
}
if err := os.MkdirAll(main, 0o755); err != nil {
t.Fatal(err)
}
run(main, "init", "-q")
run(main, "commit", "-q", "--allow-empty", "-m", "x")
run(main, "worktree", "add", "-q", filepath.Join(root, "app-feature"), "-b", "feature")
repos, err := Find(root)
if err != nil {
t.Fatal(err)
}
if len(repos) != 1 || repos[0].Name != "app" || len(repos[0].Worktrees) != 1 {
t.Fatalf("want one repo app with one worktree, got %+v", repos)
}
}
16 changes: 14 additions & 2 deletions internal/tidy/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Plan struct {
Close []Closure `json:"close"`
Named []Closure `json:"named_not_closed"` // open tasks named in a commit without a closing keyword
Branches []string `json:"merged_branches"`
// Deferred counts the tasks a commit of the current (not default) branch says it closes; they are
// closed after the merge, not before.
Deferred int `json:"deferred_closures,omitempty"`
}

// Result is what Apply did.
Expand Down Expand Up @@ -98,6 +101,7 @@ func Build(dir string, limit int) (Plan, error) {
}
if !p.OnDefault {
// Work on a feature branch is not merged yet: closing its tasks now would be wrong.
p.Deferred = len(p.Close)
p.Close = nil
}
if out, err := git(dir, "branch", "--merged", p.Default, "--format=%(refname:short)"); err == nil {
Expand All @@ -112,7 +116,10 @@ func Build(dir string, limit int) (Plan, error) {
}

func closes(msg, id string) bool {
re := regexp.MustCompile(`(?i)\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\b[:\s]+(?:[^\n]*?[\s,]|)#?` + regexp.QuoteMeta(id) + `\b`)
// The keyword must be followed directly by task ids ("Closes a-1, a-2 and a-3"), never by free text:
// "Fixes typo in docs, see a-1" mentions a-1, it does not close it.
tok := `#?(?:[A-Za-z0-9]+(?:[.-][A-Za-z0-9]+)+|\d+)`
re := regexp.MustCompile(`(?im)\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\b:?[ \t]+(?:` + tok + `(?:[ \t]*(?:,|&|\band\b)?[ \t]+))*#?` + regexp.QuoteMeta(id) + `(?:[^A-Za-z0-9_-]|$)`)
return re.MatchString(msg)
}

Expand Down Expand Up @@ -197,8 +204,13 @@ func exportTasks(dir string) error {
func (p Plan) Text() string {
var b strings.Builder
fmt.Fprintf(&b, "tidy (default branch: %s%s)\n", p.Default, map[bool]string{true: "", false: "; you are on another branch, so no task is closed from here"}[p.OnDefault])
if p.Deferred > 0 {
fmt.Fprintf(&b, "%d task(s) have a closing commit on this branch; they will be closed once it is merged into %s.\n", p.Deferred, p.Default)
}
if len(p.Close) == 0 && len(p.Named) == 0 && len(p.Branches) == 0 {
b.WriteString("nothing to do.\n")
if p.Deferred == 0 {
b.WriteString("nothing to do.\n")
}
return b.String()
}
for _, c := range p.Close {
Expand Down
19 changes: 19 additions & 0 deletions internal/tidy/tidy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,22 @@ func mustLook(n string) string {
}
return p
}

func TestClosesNeedsKeywordThenIds(t *testing.T) {
yes := []string{"Closes p-4", "closes: p-4", "Fixes p-1, p-4", "Resolves p-1 and p-4", "feat: x\n\nCloses #p-4", "Closes relai-a7o.2", "Fix p-4."}
no := []string{"Fixes typo in docs, see p-4", "Closes the loop, p-4 is next", "see p-4", "Fixed a bug related to p-4", "Closes p-44"}
for _, m := range yes {
id := "p-4"
if strings.Contains(m, "a7o") {
id = "relai-a7o.2"
}
if !closes(m, id) {
t.Errorf("%q should close %s", m, id)
}
}
for _, m := range no {
if closes(m, "p-4") {
t.Errorf("%q must not close p-4", m)
}
}
}
Loading