This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

115 lines
3.5 KiB
Markdown

# Context Management for Multi-Repo Support
## Problem
MCP servers don't receive working directory context from AI clients (Claude Code/Amp), causing database routing issues:
1. MCP server process starts with its own CWD
2. `bd` uses tree-walking to discover databases based on CWD
3. Without correct CWD, `bd` discovers wrong database or falls back to `~/.beads`
4. Result: Issues get misrouted across repositories
## Current Implementation (Partial Solution)
We've added two new MCP tools to allow explicit context management:
### Tools
#### `set_context`
Sets the workspace root directory for all bd operations.
**Parameters:**
- `workspace_root` (string): Absolute path to workspace/project root directory
**Returns:**
Confirmation message with resolved paths (workspace root and database)
**Behavior:**
1. Resolves to git repo root if inside a git repository
2. Walks up directory tree to find `.beads/*.db`
3. Sets `BEADS_WORKING_DIR`, `BEADS_DB`, and `BEADS_CONTEXT_SET` environment variables
#### `where_am_i`
Shows current workspace context and database path for debugging.
**Returns:**
Current context information including workspace root, database path, and actor
### Write Operation Protection
All write operations (`create`, `update`, `close`, `reopen`, `dep`, `init`) are decorated with `@require_context`.
**Enforcement:** Only enforced when `BEADS_REQUIRE_CONTEXT=1` environment variable is set.
This allows backward compatibility while adding safety for multi-repo setups.
## Limitations
**Environment Variable Persistence:** FastMCP's architecture doesn't guarantee environment variables persist between tool calls. This means:
- `set_context` sets env vars for that tool call
- Subsequent tool calls may not see those env vars
- Context needs to be re-established for each session
## Recommended Usage
### For Single Repository (Current Default)
No changes needed. The MCP server works as before with auto-discovery.
### For Multiple Repositories (Future)
**Option 1: Explicit Database Path (Current Workaround)**
```json
{
"mcpServers": {
"beads-repo1": {
"command": "uvx",
"args": ["beads-mcp"],
"env": {
"BEADS_DB": "/path/to/repo1/.beads/prefix.db"
}
},
"beads-repo2": {
"command": "uvx",
"args": ["beads-mcp"],
"env": {
"BEADS_DB": "/path/to/repo2/.beads/prefix.db"
}
}
}
}
```
**Option 2: Client-Side Context Management (Future)**
AI clients would need to:
1. Call `set_context` at session start with workspace root
2. MCP protocol would need to support persistent session state
**Option 3: Dolt Server with RPC (Future - Path 1.5 from bd-105)**
- Add `cwd` parameter to Dolt server RPC protocol
- Server performs tree-walking per request
- MCP server passes workspace_root via RPC
- Benefits: Centralized routing, supports multiple contexts per server
**Option 4: Advanced Routing Server (Future - Path 2 from bd-105)**
For >50 repos:
- Dedicated routing server with repo->DB mappings
- MCP becomes thin shim
- Enables shared connection pooling, cross-repo queries
## Testing
The context management tools are tested in:
- `tests/test_mcp_server_integration.py`: MCP tool tests
- Manual testing: See `/tmp/test-repo-{1,2}` example
Run tests:
```bash
uv run pytest tests/test_mcp_server_integration.py -v
```
## Future Work
See [bd-105](https://github.com/gastownhall/beads/issues/105) for full architectural analysis and roadmap.
Priority: P0/P1 - Active data corruption risk in multi-repo setups.