diff --git a/cmd/atlas-cli/index_cmds.go b/cmd/atlas-cli/index_cmds.go index 7907f19..282adbe 100644 --- a/cmd/atlas-cli/index_cmds.go +++ b/cmd/atlas-cli/index_cmds.go @@ -91,6 +91,7 @@ func indexMemory(ctx context.Context, root, scope string, download bool, step tu _ = os.Remove(clistate.Path(root, indexPendingFile)) // this pass covers what asked for it plan := embed.Current() + var downloadErr error if plan.Backend == embed.CPU && download && embed.LoadError() != nil { // Damaged files are kept by a download (it fetches what is missing): // they go first. The app's copy is the same files, as damaged. @@ -106,7 +107,13 @@ func indexMemory(ctx context.Context, root, scope string, download bool, step tu frac(0.8 * float64(done) / float64(total)) } }); err != nil { - return "", fmt.Errorf("download the model: %w", err) + if ctx.Err() != nil { + return "", fmt.Errorf("download the model: %w", ctx.Err()) + } + // The model is an improvement, not a requirement: without it + // the index is still built, on keywords. + downloadErr = err + step("The model could not be downloaded; indexing on keywords") } } vectors := record.Vectors() @@ -148,7 +155,9 @@ func indexMemory(ctx context.Context, root, scope string, download bool, step tu case embed.Off: msg += " Keyword search only (" + plan.Note + ")." case embed.CPU: - if err := embed.LoadError(); err != nil { + if downloadErr != nil { + msg += fmt.Sprintf(" The model could not be downloaded (%v): keyword search only for now (atlas-cli memory index tries again).", downloadErr) + } else if err := embed.LoadError(); err != nil { msg += fmt.Sprintf(" The model files do not load (%v): keyword search only (atlas-cli memory index downloads them again).", err) } else { msg += " No model on this machine: keyword search only (atlas-cli memory index downloads it)." diff --git a/cmd/atlas-cli/index_cmds_test.go b/cmd/atlas-cli/index_cmds_test.go new file mode 100644 index 0000000..b0c0d1c --- /dev/null +++ b/cmd/atlas-cli/index_cmds_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/tryatlas/cli/internal/embed" +) + +// A model that cannot be downloaded (offline, a proxy, Hugging Face out of +// reach) leaves keyword search: the index is still built and the command +// succeeds, saying why. +func TestIndexFallsBackToKeywordsWhenTheDownloadFails(t *testing.T) { + gitRepo(t) + if code, _, e := runCmd(t, "init", "--local"); code != 0 { + t.Fatal(e) + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + http.Error(w, "no route to the model", http.StatusBadGateway) + })) + defer srv.Close() + base := embed.BaseURL + embed.BaseURL = srv.URL + "/" + t.Setenv("ATLAS_MODELS_DIR", t.TempDir()) + t.Setenv("ATLAS_EMBED", "cpu") + embed.Reset() + t.Cleanup(func() { embed.BaseURL = base; embed.Reset() }) + + code, out, e := runCmd(t, "memory", "index") + if code != 0 { + t.Fatalf("exit %d: %s", code, e) + } + for _, want := range []string{"Memory index ready", "could not be downloaded", "502", "keyword search only"} { + if !strings.Contains(out, want) { + t.Errorf("output lacks %q: %s", want, out) + } + } + if embed.Available() { + t.Error("a failed download left a model") + } +} diff --git a/cmd/atlas-cli/init_wizard.go b/cmd/atlas-cli/init_wizard.go index 8b9e012..514f759 100644 --- a/cmd/atlas-cli/init_wizard.go +++ b/cmd/atlas-cli/init_wizard.go @@ -86,10 +86,16 @@ func finishInit(cmd *cobra.Command, root string, local, guided bool, steps initS 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 { - return err + if cmd.Context().Err() != nil { + return err + } + // Memory search is the last, optional step: a failed index + // (the bar has shown why) must not leave the agents unhooked. + tui.Note(out, "Memory search uses keywords for now; atlas-cli memory index sets it up later.") + spawnIndex(root) } - } else if embed.Available() { - spawnIndex(root) // the index, in the background + } else { + spawnIndex(root) // the index, in the background (keywords without the model) } if len(agents) > 0 || ask { fmt.Fprintln(out) diff --git a/internal/hooks/wrapper.go b/internal/hooks/wrapper.go index 2443ae7..3aadeee 100644 --- a/internal/hooks/wrapper.go +++ b/internal/hooks/wrapper.go @@ -40,7 +40,7 @@ func shellQuote(s string) string { // MissingMessage is the message a Claude-shaped agent shows when the hook // is installed but atlas-cli is not. -const MissingMessage = `{"systemMessage":"Atlas is enabled here but atlas-cli is not installed or not on PATH. Install: https://github.com/tryatlas/atlas-cli#install"}` +const MissingMessage = `{"systemMessage":"Atlas is enabled here but atlas-cli is not installed or not on PATH. Install: https://github.com/tryatlas/cli#install"}` // Owned reports that a hook command is Atlas's: the bare command, or the // bare command inside a Wrap. diff --git a/internal/tui/select.go b/internal/tui/select.go index 7f3c46b..6aef20b 100644 --- a/internal/tui/select.go +++ b/internal/tui/select.go @@ -8,6 +8,7 @@ import ( "strings" tea "charm.land/bubbletea/v2" + "github.com/charmbracelet/x/term" ) // ErrCancelled is returned when the user leaves a prompt without choosing. @@ -19,9 +20,10 @@ func Interactive() bool { return isTerminal(os.Stdin) && isTerminal(os.Stdout) } +// isTerminal asks the device itself: /dev/null is a character device too, +// and under cron, CI or a service it is both stdin and stdout. func isTerminal(f *os.File) bool { - info, err := f.Stat() - return err == nil && info.Mode()&os.ModeCharDevice != 0 + return term.IsTerminal(f.Fd()) } var ( diff --git a/internal/tui/terminal_test.go b/internal/tui/terminal_test.go new file mode 100644 index 0000000..8e48678 --- /dev/null +++ b/internal/tui/terminal_test.go @@ -0,0 +1,28 @@ +package tui + +import ( + "os" + "testing" +) + +// /dev/null is a character device but no terminal: with it as stdin and +// stdout (cron, CI, a service) a command must not start a prompt or a bar. +func TestDevNullIsNotATerminal(t *testing.T) { + f, err := os.OpenFile(os.DevNull, os.O_RDWR, 0) + if err != nil { + t.Skip(err) + } + defer f.Close() + if isTerminal(f) { + t.Fatal(os.DevNull + " was taken for a terminal") + } + r, w, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + defer r.Close() + defer w.Close() + if isTerminal(r) || isTerminal(w) { + t.Fatal("a pipe was taken for a terminal") + } +}