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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,33 @@ atlas-cli status # what was captured, what synced, what agents remember
`atlas-cli settings` changes any of it later on one screen: account, capture and
memory, each agent, background sync, the local model, updates.

### One folder, several repositories

When a project is a folder of repositories, each with its own git:

```
acme-shop/ ← atlas-cli init here, once
├── frontend/ (git)
└── backend/ (git)
```

run `atlas-cli init --workspace` in `acme-shop/`. On a terminal, plain
`atlas-cli init` asks, with "This folder only" pre-selected; a folder already
bound alone becomes a workspace with `atlas-cli workspace on`. Agents started
in `frontend/` or `backend/` then record into the one Project and share one
memory, and the agent hooks are installed in every repository. A workspace is
never made by default: it captures every repository below it. A repository
cloned in later is part of the workspace at once; `atlas-cli workspace sync`
hooks it. A repository with its own `atlas-cli init` is split off as its own
Project.

| command | does |
|---|---|
| `atlas-cli workspace` | lists the workspace's folders and which agents are hooked in each |
| `atlas-cli workspace sync` | hooks folders added since, with the agents the workspace folder uses |
| `atlas-cli workspace on` / `off` | turns an already bound folder into a workspace, or back |
| `atlas-cli workspace add <folder>` / `remove <folder>` | a folder without git (or a repository deeper down) joins or leaves |

## Shared memory

Every agent in the project reads and writes one memory, through the
Expand Down
94 changes: 61 additions & 33 deletions cmd/atlas-cli/agents_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,36 +105,54 @@ func runAgentsAdd(cmd *cobra.Command, args []string) error {
}

// installAgents writes the git hook, then each agent's hook and memory entry,
// one line per agent.
// one line per agent. In a workspace each member repository gets them too, so
// an agent started in any of them records into the workspace (ADR-0015).
func installAgents(ctx context.Context, out io.Writer, root string, agents []hooks.Agent, force, noMem bool) error {
if changed, err := hooks.InstallGitHooks(ctx, root); err != nil {
return fmt.Errorf("git hooks: %w", err)
} else if changed {
targets := project.Targets(root)
gitChanged := false
for _, t := range targets {
changed, err := hooks.InstallGitHooks(ctx, t)
if err != nil {
return fmt.Errorf("git hooks in %s: %w", t, err)
}
gitChanged = gitChanged || changed
}
if gitChanged {
fmt.Fprintln(out, "Installed the git post-commit hook (links sessions to commits).")
}
for _, a := range agents {
changed, err := a.Install(root, force)
if err != nil {
return fmt.Errorf("%s: %w", a.ID(), err)
changed, mchanged := false, false
w, hasMem := mcpconfig.For(a.ID())
for _, t := range targets {
c, err := a.Install(t, force)
if err != nil {
return fmt.Errorf("%s in %s: %w", a.ID(), t, err)
}
changed = changed || c
if hasMem && !noMem {
mc, err := w.Write(t)
if err != nil {
return fmt.Errorf("%s memory config in %s: %w", a.ID(), t, err)
}
mchanged = mchanged || mc
}
}
what := "already installed"
if changed {
what = "installed"
}
line := fmt.Sprintf("%s: hook %s", a.Name(), what)
if w, ok := mcpconfig.For(a.ID()); ok && !noMem {
mchanged, err := w.Write(root)
if err != nil {
return fmt.Errorf("%s memory config: %w", a.ID(), err)
}
if mchanged {
line += ", memory server added to " + strings.TrimPrefix(w.Path(root), root+"/")
} else {
line += ", memory server present"
}
} else if !ok && !noMem {
switch {
case hasMem && !noMem && mchanged:
line += ", memory server added to " + strings.TrimPrefix(w.Path(root), root+"/")
case hasMem && !noMem:
line += ", memory server present"
case !noMem:
line += " (no memory config writer for this agent yet)"
}
if n := len(targets) - 1; n > 0 {
line += " (workspace folder and " + repos(n) + ")"
}
fmt.Fprintln(out, line)
if _, err := exec.LookPath(a.Binary()); err != nil {
fmt.Fprintf(out, " note: %s is not on PATH here\n", a.Binary())
Expand Down Expand Up @@ -182,29 +200,39 @@ func agentLines(w io.Writer) io.Writer {
}

// removeAgents takes the Atlas entries out of each agent's config, and the
// git hook once no agent is left.
// git hook once no agent is left, in every member repository of a workspace.
func removeAgents(ctx context.Context, out io.Writer, root string, agents []hooks.Agent) error {
targets := project.Targets(root)
for _, a := range agents {
if err := a.Uninstall(root); err != nil {
return fmt.Errorf("%s: %w", a.ID(), err)
}
if w, ok := mcpconfig.For(a.ID()); ok {
if err := w.Remove(root); err != nil {
return fmt.Errorf("%s memory config: %w", a.ID(), err)
for _, t := range targets {
if err := a.Uninstall(t); err != nil {
return fmt.Errorf("%s in %s: %w", a.ID(), t, err)
}
if w, ok := mcpconfig.For(a.ID()); ok {
if err := w.Remove(t); err != nil {
return fmt.Errorf("%s memory config in %s: %w", a.ID(), t, err)
}
}
}
fmt.Fprintf(out, "%s: Atlas hook and memory entry removed.\n", a.Name())
}
left := false
for _, a := range hooks.All() {
if a.State(root) != hooks.Absent {
left = true
removed := false
for _, t := range targets {
left := false
for _, a := range hooks.All() {
if a.State(t) != hooks.Absent {
left = true
}
}
}
if !left {
if err := hooks.UninstallGitHooks(ctx, root); err != nil {
if left || hooks.GitHooksState(ctx, t) == hooks.Absent {
continue
}
if err := hooks.UninstallGitHooks(ctx, t); err != nil {
return err
}
removed = true
}
if removed {
fmt.Fprintln(out, "No agent left: the git post-commit hook was removed too.")
}
return nil
Expand Down Expand Up @@ -377,7 +405,7 @@ func printAgents(ctx context.Context, w io.Writer, root string) {
return
}
fmt.Fprintf(w, "Agents: %s\n", strings.Join(on, ", "))
if hooks.GitHooksState(ctx, root) != hooks.Current {
if project.InRepo(ctx, root) && hooks.GitHooksState(ctx, root) != hooks.Current {
fmt.Fprintln(w, "Git hook: post-commit not installed (atlas-cli agents add <agent> installs it)")
}
if states, _ := hooks.ListStates(root, ""); len(states) > 0 {
Expand Down
4 changes: 3 additions & 1 deletion cmd/atlas-cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func newRootCmd() *cobra.Command {
newUninstallCmd(),
withRun(leaf("status", "Show login, organisation, Project, gates, capture health and agent integrations", cobra.NoArgs), runStatus),
newAgentsCmd(),
newWorkspaceCmd(),
newHooksCmd(),
newACPCmd(),
newMCPBridgeCmd(),
Expand All @@ -65,7 +66,7 @@ var splashGroups = []struct {
cmds []string
}{
{"Start", []string{"login", "init", "status", "settings"}},
{"Agents", []string{"agents", "sync"}},
{"Agents", []string{"agents", "workspace", "sync"}},
{"Memory and capture", []string{"memory", "capture"}},
{"Account", []string{"whoami", "org", "logout", "update", "uninstall"}},
}
Expand Down Expand Up @@ -138,6 +139,7 @@ func newInitCmd() *cobra.Command {
c.Flags().StringSlice("agents", nil, "hook these agents (comma-separated ids, atlas-cli agents list); skips the agent step")
c.Flags().Bool("all-agents", false, "hook every agent detected on this machine; skips the agent step")
c.Flags().String("sync", "", "background sync: on or off (atlas-cli sync); skips the sync step")
c.Flags().Bool("workspace", false, "bind this folder of repositories once: agents started in each repository record here (off unless given; asked on a terminal)")
c.MarkFlagsMutuallyExclusive("agents", "all-agents")
c.MarkFlagsMutuallyExclusive("project", "create")
c.MarkFlagsMutuallyExclusive("local", "project")
Expand Down
81 changes: 79 additions & 2 deletions cmd/atlas-cli/init_wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os/exec"
"path/filepath"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -54,7 +55,7 @@ func finishInit(cmd *cobra.Command, root string, local, guided bool, steps initS
}
}
model := false
if guided && !embed.Available() && project.MemorySharing(project.ScopeRoot(cmd.Context(), root)) {
if guided && !embed.Available() && project.MemorySharing(project.MemoryRoot(cmd.Context(), root)) {
i, err := chooseStep(steps.step("Memory search"), "Download the memory search model? Agents then find memories by meaning, not only by words.", []tui.Choice{
{Label: "Download", Detail: embed.Size + ", once; the same model the Atlas app uses"},
{Label: "Not now", Detail: "keyword search; atlas-cli memory index downloads it later"},
Expand Down Expand Up @@ -82,7 +83,7 @@ func finishInit(cmd *cobra.Command, root string, local, guided bool, steps initS
// On a terminal the work runs under the progress bar, each line a step.
if model {
fmt.Fprintln(out)
scope := project.ScopeRoot(cmd.Context(), root)
scope := project.MemoryRoot(cmd.Context(), root)
if err := tui.WorkProgress(out, "Setting up memory search", func(step tui.StepFunc, frac tui.FracFunc) (string, error) {
return indexMemory(cmd.Context(), root, scope, true, step, frac)
}); err != nil {
Expand Down Expand Up @@ -223,3 +224,79 @@ func pickSync(cmd *cobra.Command, root string, guided bool, step tui.Step) (on,
}
return i == 0, true, nil
}

// pickWorkspace decides whether the launch dir is bound as a workspace, a
// folder of repositories bound once (ADR-0015). A workspace captures every
// repository below it, so it is opt-in: --workspace when given; else, for a
// folder outside git that holds repositories, asked on a terminal with "This
// folder only" pre-selected, and never without one (init names the
// repositories and how to capture them). A folder that is already a
// workspace stays one unless --workspace=false or the prompt says otherwise.
func pickWorkspace(cmd *cobra.Command, launch, scope string, guided bool) (bool, error) {
flags := cmd.Flags()
members := project.Members(launch)
names := memberNames(launch, members)
if flags.Changed("workspace") {
if on, _ := flags.GetBool("workspace"); !on {
return false, nil
}
if launch != scope {
return false, fmt.Errorf("%s is inside the repository %s; a workspace is the folder that holds the repositories", launch, scope)
}
if len(members) == 0 {
return false, fmt.Errorf("%s holds no git repository; a workspace is a folder of repositories", launch)
}
if !guided {
fmt.Fprintf(cmd.ErrOrStderr(), "Binding %s as a workspace of %s (%s).\n", launch, repos(len(members)), names)
}
return true, nil
}
current := project.IsWorkspace(launch)
if len(members) == 0 || (!current && project.InRepo(cmd.Context(), launch)) {
return current, nil
}
if !guided {
if !current {
fmt.Fprintf(cmd.ErrOrStderr(), "Note: %s holds %s (%s). Agents started in them are not captured; atlas-cli workspace on captures them here.\n",
launch, repos(len(members)), names)
}
return current, nil
}
initial := 0
if current {
initial = 1
}
i, err := chooseStep(tui.Step{Title: "Workspace"}, fmt.Sprintf("This folder holds %s: %s.", repos(len(members)), names), []tui.Choice{
{Label: "This folder only", Detail: "agents started in those repositories are not captured here"},
{Label: "Workspace", Detail: "agents started in any of them record here and share one memory"},
}, initial)
if err != nil {
return false, err
}
return i == 1, nil
}

// memberNames lists member repositories by folder name, at most five.
func memberNames(root string, members []string) string {
var names []string
for i, m := range members {
if i == 5 {
names = append(names, fmt.Sprintf("and %d more", len(members)-5))
break
}
rel, err := filepath.Rel(root, m)
if err != nil {
rel = filepath.Base(m)
}
names = append(names, rel)
}
return strings.Join(names, ", ")
}

// repos is "1 repository" or "n repositories".
func repos(n int) string {
if n == 1 {
return "1 repository"
}
return fmt.Sprintf("%d repositories", n)
}
11 changes: 11 additions & 0 deletions cmd/atlas-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func TestMain(m *testing.M) {
state, _ := os.MkdirTemp("", "atlas-state-") // never the user's CLI state
_ = os.Setenv("XDG_STATE_HOME", state)
_ = os.Setenv("ATLAS_MODELS_DIR", state) // no embedding model: deterministic
// Never the user's Projects: a test that does not chdir must not find a
// binding (or a workspace) above the source tree.
cwd, _ := os.MkdirTemp("", "atlas-cwd-")
_ = os.Chdir(cwd)
os.Exit(m.Run())
}

Expand Down Expand Up @@ -73,6 +77,12 @@ var wantCommands = []string{
"uninstall",
"update",
"whoami",
"workspace",
"workspace add",
"workspace off",
"workspace on",
"workspace remove",
"workspace sync",
}

// allCommands is wantCommands plus one hooks entry point per registered
Expand Down Expand Up @@ -118,6 +128,7 @@ var implemented = map[string]bool{
"settings": true, "sync": true, "update": true, "uninstall": true, "memory index": true, "memory compute": true,
"agents add": true, "agents list": true, "agents remove": true, "agents sessions": true,
"hooks extract": true, "hooks git post-commit": true,
"workspace": true, "workspace on": true, "workspace off": true, "workspace sync": true, "workspace add": true, "workspace remove": true,
}

func TestStubsReportNotImplemented(t *testing.T) {
Expand Down
18 changes: 13 additions & 5 deletions cmd/atlas-cli/memory_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import (
"github.com/tryatlas/cli/internal/tui/memview"
)

// openRecord opens the shared-memory record of the repository around the cwd.
// openRecord opens the shared-memory record of the repository (or workspace)
// around the cwd.
func openRecord(cmd *cobra.Command, create bool) (*record.Store, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
scope := project.ScopeRoot(cmd.Context(), cwd)
scope := project.MemoryRoot(cmd.Context(), cwd)
st, err := record.Open(scope, create)
if errors.Is(err, record.ErrNoRecord) {
return nil, fmt.Errorf("no shared memory yet for %s", scope)
Expand Down Expand Up @@ -117,15 +118,22 @@ func runMCPBridge(cmd *cobra.Command, args []string) error {
}
root = cwd
}
cwd := root
if c, err := project.Canonical(root); err == nil {
cwd, root = c, c
}
if p, err := project.Discover(cmd.Context(), root); err == nil {
root = p.Root
} else if c, err := project.Canonical(root); err == nil {
root = c
if !p.Workspace {
cwd = root
}
// In a workspace the agent works in one member repository: its
// relative paths resolve there, not in the workspace folder.
}
if agentID == "" {
agentID = "unknown"
}
g := server.Grant{Agent: agentID, Cwd: root, SessionID: hooks.CurrentSession(root, agentID)}
g := server.Grant{Agent: agentID, Cwd: cwd, SessionID: hooks.CurrentSession(root, agentID)}
if rf := listener.ReadRunFile(root); rf != nil {
return server.RunBridge(cmd.Context(), cmd.InOrStdin(), cmd.OutOrStdout(), server.NewUnixForwarder(rf.Socket, g))
}
Expand Down
Loading
Loading