fix(memory): fail startup on an unusable memory directory

Both stores treat a missing file as an empty document, so a memory
directory that does not exist looked exactly like a memory with nothing
in it yet. archied logged "memory manager started" and ran with no
memory at all; the failure would have surfaced at the agent's first
write, as a tool error mid-conversation, long after startup said it was
fine.

Found live: a config copied from carina kept work_dir=/var/lib/archie
/work, a container path with no counterpart on the host.

New now establishes the directory and reports a path it cannot use, so
the existing error path exits at startup where an operator can see it.

Refs archie-core-rml
This commit is contained in:
sam committed 2026-08-04 02:00:12 +10:00
1 parent 6532537e24
commit d615de5c99
2 files changed
+91

No files matched your search

+81
View File
@@ -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)
}
})
}
}
+10
View File
@@ -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