fix: MCP init tool creates .beads in current directory

Critical bug: MCP init tool reported success but didn't create .beads
directory. It was using --db flag which tells bd to use an existing
database elsewhere instead of creating a new one.

Root cause:
- bd_client.init() was calling _global_flags() which adds --db flag
- bd init --db <path> uses existing db instead of creating new one
- Result: init appeared to succeed but created nothing

Fix:
- Remove _global_flags() from init command
- Only pass --actor flag (safe for init)
- Do NOT pass --db flag to init (defeats the purpose)
- Add explicit comment explaining why

Testing:
- Added test_init_creates_beads_directory() integration test
- Verifies .beads directory is created in current working directory
- Verifies database file has correct prefix
- All 91 tests pass

This was causing silent failures where agents thought they initialized
bd but the .beads directory was never created.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-10-14 16:47:29 -07:00
parent e6697939f0
commit ba8fdf1a8c
2 changed files with 53 additions and 1 deletions

View File

@@ -462,7 +462,11 @@ class BdClient:
if params.prefix:
cmd.extend(["--prefix", params.prefix])
cmd.extend(self._global_flags())
# NOTE: Do NOT add --db flag for init!
# init creates a NEW database in the current directory.
# Only add actor-related flags.
if self.actor:
cmd.extend(["--actor", self.actor])
try:
process = await asyncio.create_subprocess_exec(

View File

@@ -349,3 +349,51 @@ async def test_dependency_types(bd_client):
show_params = ShowIssueParams(issue_id=issue1.id)
shown = await bd_client.show(show_params)
assert len(shown.dependencies) > 0
@pytest.mark.asyncio
async def test_init_creates_beads_directory(bd_executable):
"""Test that init creates .beads directory in current working directory.
This is a critical test for the bug where init was using --db flag
and creating the database in the wrong location.
"""
import asyncio
from beads_mcp.bd_client import BdClient
from beads_mcp.models import InitParams
# Create a temporary directory to test in
with tempfile.TemporaryDirectory(prefix="beads_init_test_", dir="/tmp") as temp_dir:
temp_path = Path(temp_dir)
beads_dir = temp_path / ".beads"
# Ensure .beads doesn't exist yet
assert not beads_dir.exists()
# Create client WITHOUT beads_db set (this was the bug!)
client = BdClient(bd_path=bd_executable, beads_db=None)
# Change to temp directory and run init
original_cwd = os.getcwd()
try:
os.chdir(temp_dir)
# Initialize with custom prefix
params = InitParams(prefix="test")
result = await client.init(params)
# Verify .beads directory was created in current directory
assert beads_dir.exists(), f".beads directory not created in {temp_dir}"
assert beads_dir.is_dir(), f".beads exists but is not a directory"
# Verify database file was created with correct prefix
db_files = list(beads_dir.glob("*.db"))
assert len(db_files) > 0, "No database file created in .beads/"
assert any("test" in str(db.name) for db in db_files), \
f"Database file doesn't contain prefix 'test': {[db.name for db in db_files]}"
# Verify success message
assert "initialized" in result.lower() or "created" in result.lower()
finally:
os.chdir(original_cwd)