fix(nodb): set cmdCtx.StoreActive in initializeNoDbMode (GH#897)

The bug: initializeNoDbMode() was setting the legacy global storeActive
but not cmdCtx.StoreActive. When ensureStoreActive() checked
isStoreActive(), it used cmdCtx.StoreActive (which was false), causing
the JSONL-only mode error even when --no-db was passed.

The fix: Use accessor functions (lockStore, setStore, setStoreActive,
unlockStore) which set both the legacy globals and cmdCtx fields.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
beads/crew/grip
2026-01-04 16:24:05 -08:00
committed by Steve Yegge
parent 659f52d6bd
commit 85ac1e126b
2 changed files with 70 additions and 4 deletions

View File

@@ -73,10 +73,11 @@ func initializeNoDbMode() error {
debug.Logf("using prefix '%s'", prefix) debug.Logf("using prefix '%s'", prefix)
// Set global store and mark as active (fixes bd comment --no-db) // Set global store and mark as active (fixes bd comment --no-db)
storeMutex.Lock() // GH#897: Use accessor functions to also set cmdCtx fields, not just globals
store = memStore lockStore()
storeActive = true setStore(memStore)
storeMutex.Unlock() setStoreActive(true)
unlockStore()
return nil return nil
} }

View File

@@ -301,6 +301,71 @@ func TestInitializeNoDbMode_SetsStoreActive(t *testing.T) {
} }
} }
func TestInitializeNoDbMode_SetsCmdCtxStoreActive(t *testing.T) {
// GH#897: Verify that initializeNoDbMode sets cmdCtx.StoreActive, not just the global.
// This is critical for commands like `comments add` that call ensureStoreActive().
ensureCleanGlobalState(t)
tempDir := t.TempDir()
beadsDir := filepath.Join(tempDir, ".beads")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatalf("Failed to create .beads dir: %v", err)
}
// Create a minimal JSONL file with one issue
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
content := `{"id":"mmm-155","title":"Test Issue","status":"open"}
`
if err := os.WriteFile(jsonlPath, []byte(content), 0o600); err != nil {
t.Fatalf("Failed to write JSONL: %v", err)
}
// Initialize CommandContext (simulates what PersistentPreRun does)
initCommandContext()
oldCwd, _ := os.Getwd()
defer func() {
_ = os.Chdir(oldCwd)
resetCommandContext()
}()
// Change to temp dir so initializeNoDbMode finds .beads
if err := os.Chdir(tempDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
// Initialize no-db mode
if err := initializeNoDbMode(); err != nil {
t.Fatalf("initializeNoDbMode failed: %v", err)
}
// Verify cmdCtx.StoreActive is true (this was the bug - it was only setting globals)
ctx := GetCommandContext()
if ctx == nil {
t.Fatal("cmdCtx should not be nil after initCommandContext")
}
if !ctx.StoreActive {
t.Error("cmdCtx.StoreActive should be true after initializeNoDbMode (GH#897)")
}
if ctx.Store == nil {
t.Error("cmdCtx.Store should not be nil after initializeNoDbMode")
}
// ensureStoreActive should succeed
if err := ensureStoreActive(); err != nil {
t.Errorf("ensureStoreActive should succeed after initializeNoDbMode: %v", err)
}
// Comments should work
comment, err := ctx.Store.AddIssueComment(rootCtx, "mmm-155", "testuser", "Test comment")
if err != nil {
t.Fatalf("AddIssueComment failed: %v", err)
}
if comment.Text != "Test comment" {
t.Errorf("Expected 'Test comment', got %s", comment.Text)
}
}
func TestWriteIssuesToJSONL(t *testing.T) { func TestWriteIssuesToJSONL(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
beadsDir := filepath.Join(tempDir, ".beads") beadsDir := filepath.Join(tempDir, ".beads")