diff --git a/internal/memory/builtin/dir_test.go b/internal/memory/builtin/dir_test.go new file mode 100644 index 000000000..963e2d9fa --- /dev/null +++ b/internal/memory/builtin/dir_test.go @@ -0,0 +1,81 @@ +package builtin + +import ( + "os" + "path/filepath" + "testing" +) + +// A missing memory directory is indistinguishable from an empty memory: both +// stores treat ENOENT as "no content yet". That makes a misconfigured Dir +// invisible until the agent's first memory write fails mid-conversation, long +// after startup reported success. New must settle the question up front. +func TestNewEstablishesDir(t *testing.T) { + tests := []struct { + name string + dir func(t *testing.T) string + wantErr bool + }{ + { + name: "existing directory", + dir: func(t *testing.T) string { return t.TempDir() }, + }, + { + name: "missing directory is created", + dir: func(t *testing.T) string { + return filepath.Join(t.TempDir(), "nested", "memory") + }, + }, + { + name: "unwritable parent is reported", + dir: func(t *testing.T) string { + parent := filepath.Join(t.TempDir(), "ro") + if err := os.Mkdir(parent, 0o500); err != nil { + t.Fatalf("mkdir: %v", err) + } + return filepath.Join(parent, "memory") + }, + wantErr: true, + }, + { + name: "path occupied by a file is reported", + dir: func(t *testing.T) string { + p := filepath.Join(t.TempDir(), "memory") + if err := os.WriteFile(p, []byte("not a dir"), 0o600); err != nil { + t.Fatalf("write: %v", err) + } + return p + }, + wantErr: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Running as root defeats permission-based cases. + if tc.wantErr && os.Geteuid() == 0 { + t.Skip("running as root: permission cases are not meaningful") + } + dir := tc.dir(t) + + _, err := New(Config{Dir: dir}) + if tc.wantErr { + if err == nil { + t.Fatalf("New(%q) = nil error, want a failure the operator can act on", dir) + } + return + } + if err != nil { + t.Fatalf("New(%q): %v", dir, err) + } + + info, statErr := os.Stat(dir) + if statErr != nil { + t.Fatalf("New returned success but %q does not exist: %v", dir, statErr) + } + if !info.IsDir() { + t.Fatalf("%q is not a directory", dir) + } + }) + } +} diff --git a/internal/memory/builtin/provider.go b/internal/memory/builtin/provider.go index e3db4ad9e..b21bced46 100644 --- a/internal/memory/builtin/provider.go +++ b/internal/memory/builtin/provider.go @@ -2,6 +2,7 @@ package builtin import ( "fmt" + "os" "path/filepath" "strings" "sync" @@ -78,6 +79,15 @@ func New(cfg Config) (*Provider, error) { return nil, fmt.Errorf("builtin: Config.Dir must not be empty") } + // Establish the directory up front. Both stores treat a missing file as + // an empty document, so without this a Dir that does not exist -- or one + // this process cannot write to -- is indistinguishable from a memory that + // simply has nothing in it yet: startup reports success and the failure + // surfaces at the agent's first memory write, mid-conversation. + if err := os.MkdirAll(cfg.Dir, 0o755); err != nil { + return nil, fmt.Errorf("builtin: memory directory %s unusable: %w", cfg.Dir, err) + } + memStore, err := NewStore(filepath.Join(cfg.Dir, "MEMORY.md"), cfg.MaxFileBytes) if err != nil { return nil, err