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
13 changes: 11 additions & 2 deletions cmd/atlas-cli/index_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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)."
Expand Down
43 changes: 43 additions & 0 deletions cmd/atlas-cli/index_cmds_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
12 changes: 9 additions & 3 deletions cmd/atlas-cli/init_wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/hooks/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions internal/tui/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 (
Expand Down
28 changes: 28 additions & 0 deletions internal/tui/terminal_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading