From a963f04ffedaa02e2c3b693a115b8d0ec3cfa73d Mon Sep 17 00:00:00 2001 From: Ilya perevozchikov Date: Sun, 15 Nov 2026 01:21:47 +0200 Subject: [PATCH 1/3] fix(rag): preserve index when document discovery fails --- docs/CHANGELOG.md | 4 + docs/CONFIGURATION.md | 2 + internal/rag/discovery_failure_test.go | 137 +++++++++++++++++++++++++ internal/rag/documents.go | 20 ++-- 4 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 internal/rag/discovery_failure_test.go diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e097e38..fc7a4ce 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Indexing preserves existing documents when a configured source is missing or unreadable, or a scan fails. Restore unavailable sources, or remove intentionally deleted sources from `MCP_INDEX_DIRS`. + ## [0.17.0] - 2026-11-14 ### Added diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 294a3e3..ca8f769 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -121,6 +121,8 @@ The recommended solo-local preset stores them under `.agent-memory/`. ### Indexing safety controls +Every path in `MCP_INDEX_DIRS` must be available. If a configured file or directory cannot be accessed, a directory cannot be scanned, or a supported file cannot be read, indexing fails before changing the existing index. This prevents a temporarily unavailable source from being treated as deleted. Restore the source and retry; if you intentionally removed it, remove its entry from `MCP_INDEX_DIRS`. Files deleted inside an accessible source directory are still removed from the index normally. + RAG indexing scans supported docs and engineering text files, but you can further reduce risk with explicit controls: - built-in excluded directories such as `.git`, `.agent-memory`, `node_modules`, `logs`, and `.terraform` diff --git a/internal/rag/discovery_failure_test.go b/internal/rag/discovery_failure_test.go new file mode 100644 index 0000000..67dd035 --- /dev/null +++ b/internal/rag/discovery_failure_test.go @@ -0,0 +1,137 @@ +package rag + +import ( + "context" + "errors" + "os" + "path/filepath" + "reflect" + "sort" + "strings" + "testing" + + "github.com/ipiton/agent-memory-mcp/internal/vectorstore" +) + +func TestIndexDiscoveryFailurePreservesPublishedIndex(t *testing.T) { + for _, tc := range []struct { + name string + path string + missing bool + }{ + {name: "missing directory", path: "docs", missing: true}, + {name: "missing configured file", path: "README.md", missing: true}, + {name: "unreadable directory", path: "notes"}, + {name: "unreadable discovered file", path: "docs/a.md"}, + {name: "unreadable configured file", path: "README.md"}, + } { + t.Run(tc.name, func(t *testing.T) { + srv := newBatchEmbeddingServer(t) + t.Cleanup(srv.Close) + root := newRootProject(t, "a.md", "b.md") + writeCorpusFile(t, root, "notes/c.md", "# Notes\n\nretained knowledge\n") + writeCorpusFile(t, root, "README.md", "# Project\n\nproject overview\n") + engine := newRootEngine(t, root, t.TempDir(), srv.URL) + engine.docService.config.IndexDirs = []string{"docs", "notes", "README.md"} + if err := engine.IndexDocuments(context.Background()); err != nil { + t.Fatalf("initial index: %v", err) + } + store := engine.vecService.store + filesBefore, err := store.GetAllIndexedFiles() + if err != nil { + t.Fatal(err) + } + chunksBefore := discoveryTestChunks(t, store) + metadataBefore := discoveryTestMetadata(t, store) + + path := filepath.Join(root, tc.path) + var restore func() + if tc.missing { + moved := filepath.Join(t.TempDir(), "unavailable") + if err := os.Rename(path, moved); err != nil { + t.Fatal(err) + } + restore = func() { + if err := os.Rename(moved, path); err != nil { + t.Fatal(err) + } + } + } else { + info, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if err := os.Chmod(path, 0); err != nil { + t.Fatal(err) + } + restore = func() { + if err := os.Chmod(path, info.Mode()); err != nil { + t.Fatal(err) + } + } + defer restore() + var readErr error + if info.IsDir() { + _, readErr = os.ReadDir(path) + } else { + _, readErr = os.ReadFile(path) + } + if readErr == nil { + t.Skip("filesystem permissions do not prevent reading") + } + } + + err = engine.IndexDocuments(context.Background()) + if err == nil { + t.Fatal("indexing with an unavailable source succeeded") + } + if !strings.Contains(err.Error(), path) { + t.Errorf("error %q does not name unavailable path %q", err, path) + } + if tc.missing && !errors.Is(err, os.ErrNotExist) { + t.Errorf("error %v does not preserve os.ErrNotExist", err) + } + filesAfter, err := store.GetAllIndexedFiles() + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(filesBefore, filesAfter) { + t.Error("discovery failure changed indexed-file records") + } + if !reflect.DeepEqual(chunksBefore, discoveryTestChunks(t, store)) { + t.Error("discovery failure changed published chunks") + } + if !reflect.DeepEqual(metadataBefore, discoveryTestMetadata(t, store)) { + t.Error("discovery failure changed index metadata") + } + restore() + if err := engine.IndexDocuments(context.Background()); err != nil { + t.Fatalf("index after restoring source: %v", err) + } + assertIndexed(t, engine, "README.md", "docs/a.md", "docs/b.md", "notes/c.md") + }) + } +} + +func discoveryTestChunks(t *testing.T, store vectorstore.Store) []vectorstore.Chunk { + t.Helper() + chunks, err := store.AllChunks() + if err != nil { + t.Fatal(err) + } + sort.Slice(chunks, func(i, j int) bool { return chunks[i].ID < chunks[j].ID }) + return chunks +} + +func discoveryTestMetadata(t *testing.T, store vectorstore.Store) map[string]string { + t.Helper() + values := make(map[string]string) + for _, key := range []string{indexStateMetadataKey, indexStartedAtKey, indexRootMetadataKey, "index_generation", "last_indexed", "embedding_model", "chunker_version"} { + value, err := store.GetMetadata(key) + if err != nil { + t.Fatal(err) + } + values[key] = value + } + return values +} diff --git a/internal/rag/documents.go b/internal/rag/documents.go index 77f5b1e..ffb09b1 100644 --- a/internal/rag/documents.go +++ b/internal/rag/documents.go @@ -50,8 +50,7 @@ func (ds *documentService) collectDocuments() ([]document, error) { info, err := os.Stat(fullPath) if err != nil { - ds.logger.Warn("Path not found", zap.String("path", fullPath)) - continue + return nil, fmt.Errorf("access configured index source %q: %w", fullPath, err) } if !info.IsDir() { @@ -62,13 +61,12 @@ func (ds *documentService) collectDocuments() ([]document, error) { considered++ docs, err := ds.processFile(fullPath) if err != nil { - ds.logger.Warn("Failed to process file", zap.String("path", fullPath), zap.Error(err)) - } else { - if len(docs) == 0 { - empty++ - } - allDocs = append(allDocs, docs...) + return nil, fmt.Errorf("process indexed file %q: %w", fullPath, err) } + if len(docs) == 0 { + empty++ + } + allDocs = append(allDocs, docs...) } continue } @@ -87,8 +85,7 @@ func (ds *documentService) collectDocuments() ([]document, error) { considered++ docs, err := ds.processFile(path) if err != nil { - ds.logger.Warn("Failed to process file", zap.String("path", path), zap.Error(err)) - return nil + return fmt.Errorf("process indexed file %q: %w", path, err) } if len(docs) == 0 { empty++ @@ -98,7 +95,8 @@ func (ds *documentService) collectDocuments() ([]document, error) { return nil }) if err != nil { - ds.logger.Error("Failed to walk directory", zap.String("path", fullPath), zap.Error(err)) + // An incomplete scan must not turn unreadable sources into deletions. + return nil, fmt.Errorf("walk index source %q: %w", fullPath, err) } } From a2a7853762c977dc47b49f95ad595754389da085 Mon Sep 17 00:00:00 2001 From: Ilya perevozchikov Date: Sun, 15 Nov 2026 01:32:24 +0200 Subject: [PATCH 2/3] docs(rag): describe unavailable source failures --- docs/MCP_TOOLS.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/MCP_TOOLS.md b/docs/MCP_TOOLS.md index b0b4fd0..88a412c 100644 --- a/docs/MCP_TOOLS.md +++ b/docs/MCP_TOOLS.md @@ -331,9 +331,11 @@ query against a corpus that never included the material returns "no results", which is indistinguishable from "there is nothing like that". A root that is configured and contributes zero files is therefore reported by name (T97) — omitting it would make it look like a root nobody had configured. Common -causes: the path does not exist under `MCP_ROOT`, it holds no supported file -types, or `MCP_INDEX_EXCLUDE_DIRS` / `MCP_INDEX_EXCLUDE_GLOBS` filtered -everything out. +causes: it holds no supported file types, or `MCP_INDEX_EXCLUDE_DIRS` / +`MCP_INDEX_EXCLUDE_GLOBS` filtered everything out. A missing or unreadable +configured path fails indexing before the existing index is changed. Restore +the path and retry, or remove an intentionally deleted source from +`MCP_INDEX_DIRS`. ## Engineering workflow tools From 3ae9c3b671622523d9818c017a186368e90c386d Mon Sep 17 00:00:00 2001 From: Ilya perevozchikov Date: Tue, 17 Nov 2026 21:09:58 +0200 Subject: [PATCH 3/3] docs: clarify retirement of overlapping index sources --- docs/CONFIGURATION.md | 2 +- docs/MCP_TOOLS.md | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index b9e6aa9..db1049a 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -125,7 +125,7 @@ The recommended solo-local preset stores them under `.agent-memory/`. ### Indexing safety controls -Unavailable sources are logged by path. Indexing retains existing records and chunks under failed paths that are still included in the configuration. Newly excluded paths are pruned even when their source is unavailable. Accessible sources continue to update. A missing optional file that has never been indexed (for example, `CHANGELOG.md`) does not block indexing. Restore unavailable sources and retry; to retire a configured source intentionally, remove its entry from `MCP_INDEX_DIRS`. A model or chunker change requires a complete rebuild, so that run fails before changing the index if any included indexed source is unavailable. This prevents old vectors from being labelled with the new model or chunker. Automatic adoption of another index root also waits for these sources. An explicit `--allow-root-change` replacement does not carry over unscanned records from the old root. +Unavailable sources are logged by path. Indexing retains existing records and chunks under failed paths that are still included in the configuration. Newly excluded paths are pruned even when their source is unavailable. Sources outside the failed paths continue to update. A missing optional file that has never been indexed (for example, `CHANGELOG.md`) does not block indexing. Restore unavailable sources and retry. To retire a source intentionally, remove all coverage for its path from `MCP_INDEX_DIRS` or add an indexing exclusion, then run indexing. A model or chunker change requires a complete rebuild, so that run fails before changing the index if any included indexed source is unavailable. This prevents old vectors from being labelled with the new model or chunker. Automatic adoption of another index root also waits for these sources. An explicit `--allow-root-change` replacement does not carry over unscanned records from the old root. RAG indexing scans supported docs and engineering text files, but you can further reduce risk with explicit controls: diff --git a/docs/MCP_TOOLS.md b/docs/MCP_TOOLS.md index ae3d412..ee9fea7 100644 --- a/docs/MCP_TOOLS.md +++ b/docs/MCP_TOOLS.md @@ -368,9 +368,10 @@ omitting it would make it look like a root nobody had configured. Common causes: it holds no supported file types, or `MCP_INDEX_EXCLUDE_DIRS` / `MCP_INDEX_EXCLUDE_GLOBS` filtered everything out. A missing or unreadable configured path is logged. Records and chunks for paths still included in the -configuration are retained while accessible sources continue to update. A missing optional path that has never -been indexed does not block the run. Restore the path and retry, or remove an -intentionally retired source from `MCP_INDEX_DIRS`. A model or chunker rebuild +configuration are retained. Paths outside the failed paths continue to update. +A missing optional path that has never been indexed does not block the run. Restore the path and retry. To retire a +source, remove coverage from every configured root or add an indexing exclusion, +then re-index. A model or chunker rebuild waits for unavailable indexed sources that are still included before changing the index. ## Engineering workflow tools