Skip to content

Commit becb852

Browse files
committed
Write namespace RAG indexes under namespace dirs
1 parent 0608ba5 commit becb852

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

internal/cli/docs.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,21 @@ func buildDocsRAGIndex(ctx context.Context, deps *Deps, opts docsRAGOptions) (in
164164
b := &ragindex.Builder{
165165
DB: deps.DB,
166166
OutDir: opts.OutDir,
167-
IndexDir: opts.IndexDir,
167+
IndexDir: namespaceRagIndexDir(opts.IndexDir, ns),
168168
ProjectDesc: opts.ProjectDesc,
169169
}
170170
communities, files, err := b.Build(ctx)
171171
return communities, files, rebuilt, err
172172
}
173173

174+
// @intent align CLI RAG index output with MCP and Wiki server namespace lookup paths.
175+
func namespaceRagIndexDir(baseDir, namespace string) string {
176+
if ctxns.Normalize(namespace) == ctxns.DefaultNamespace {
177+
return baseDir
178+
}
179+
return filepath.Join(baseDir, namespace)
180+
}
181+
174182
// resolveRagIndexDir honors rag.index_dir when the CLI flag is left at its default.
175183
// @intent keep docs and rag-index commands aligned with config-based RAG output paths.
176184
func resolveRagIndexDir(flagValue string) string {

internal/cli/docs_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,40 @@ func TestDocsCommand_BuildsRAGIndexByDefault(t *testing.T) {
9898
}
9999
}
100100

101+
func TestDocsCommand_BuildsNamespaceIndexes(t *testing.T) {
102+
deps, stdout, stderr, db := setupDocsTest(t)
103+
104+
db.Create(&model.Node{
105+
Namespace: "alpha",
106+
QualifiedName: "pkg.Main",
107+
Kind: model.NodeKindFunction,
108+
Name: "Main",
109+
FilePath: "pkg/main.go",
110+
StartLine: 1, EndLine: 5,
111+
Hash: "h1", Language: "go",
112+
})
113+
114+
outDir := t.TempDir()
115+
indexDir := t.TempDir()
116+
if err := executeCmd(deps, stdout, stderr, "--namespace", "alpha", "docs", "--out", outDir, "--rag-index-dir", indexDir); err != nil {
117+
t.Fatalf("docs: %v", err)
118+
}
119+
120+
if _, err := os.Stat(filepath.Join(indexDir, "alpha", "doc-index.json")); err != nil {
121+
t.Fatalf("expected namespace doc-index.json to be created: %v", err)
122+
}
123+
if _, err := os.Stat(filepath.Join(indexDir, "alpha", "wiki-index.json")); err != nil {
124+
t.Fatalf("expected namespace wiki-index.json to be created: %v", err)
125+
}
126+
if _, err := os.Stat(filepath.Join(indexDir, "doc-index.json")); !os.IsNotExist(err) {
127+
t.Fatalf("default doc-index.json should not be written for named namespace, stat err=%v", err)
128+
}
129+
out := stdout.String()
130+
if !strings.Contains(out, "Wiki index written:") || !strings.Contains(out, "RAG index written:") {
131+
t.Fatalf("expected index output, got:\n%s", out)
132+
}
133+
}
134+
101135
func TestDocsCommand_RAGFlagCanDisableIndexBuild(t *testing.T) {
102136
deps, stdout, stderr, db := setupDocsTest(t)
103137

internal/cli/ragindex.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ func newRagIndexCmd(deps *Deps) *cobra.Command {
4141
b := &ragindex.Builder{
4242
DB: deps.DB,
4343
OutDir: resolveOutDir(outDir),
44-
IndexDir: indexDir,
44+
IndexDir: namespaceRagIndexDir(indexDir, viper.GetString("namespace")),
4545
ProjectDesc: projectDesc,
4646
}
4747

4848
ctx := cmd.Context()
49-
ns, _ := cmd.Flags().GetString("namespace")
49+
ns := viper.GetString("namespace")
5050
ctx = ctxns.WithNamespace(ctx, ns)
5151

5252
communities, files, err := b.Build(ctx)

internal/cli/ragindex_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestRagIndexCmd_NamespaceFiltersDocIndex(t *testing.T) {
141141
t.Fatalf("Execute() error: %v", err)
142142
}
143143

144-
idx, err := ragindex.LoadIndex(filepath.Join(indexDir, "doc-index.json"))
144+
idx, err := ragindex.LoadIndex(filepath.Join(indexDir, "backend", "doc-index.json"))
145145
if err != nil {
146146
t.Fatalf("loadIndex: %v", err)
147147
}

0 commit comments

Comments
 (0)