gitgram/aitimemachine/beads/cmd/bd/store_reopen.go
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

106 lines
3.0 KiB
Go

package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/utils"
)
// openReadOnlyStoreForDBPath reopens a read-only store from an existing dbPath
// while preserving repo-local metadata such as dolt_database and the resolved
// Dolt server port. Falls back to deriving the beads directory from the dbPath
// parent when no matching metadata.json can be found.
func openReadOnlyStoreForDBPath(ctx context.Context, dbPath string) (storage.DoltStorage, error) {
if dbPath == "" {
return nil, fmt.Errorf("no database path available")
}
if beadsDir := resolveBeadsDirForDBPath(dbPath); beadsDir != "" {
return newReadOnlyStoreFromConfig(ctx, beadsDir)
}
// Fallback: derive beads dir from dbPath parent directory.
return newReadOnlyStoreFromConfig(ctx, filepath.Dir(dbPath))
}
// resolveBeadsDirForDBPath maps a database path back to its owning .beads
// directory when metadata.json is available. This is needed for repos that use
// non-default dolt_database names or custom dolt_data_dir locations.
func resolveBeadsDirForDBPath(dbPath string) string {
actualDBPath := utils.CanonicalizePath(dbPath)
if parent := filepath.Dir(dbPath); filepath.Base(parent) == ".beads" {
if _, err := os.Stat(filepath.Join(parent, "metadata.json")); err == nil {
return parent
}
}
if parent := filepath.Dir(actualDBPath); filepath.Base(parent) == ".beads" {
if _, err := os.Stat(filepath.Join(parent, "metadata.json")); err == nil {
return parent
}
}
seen := map[string]struct{}{}
candidates := make([]string, 0, 16)
addCandidate := func(path string) {
if path == "" {
return
}
key := utils.NormalizePathForComparison(path)
if key == "" {
return
}
if _, ok := seen[key]; ok {
return
}
seen[key] = struct{}{}
candidates = append(candidates, path)
}
addAncestorCandidates := func(path string) {
for dir := path; dir != "" && dir != filepath.Dir(dir); dir = filepath.Dir(dir) {
addCandidate(filepath.Join(dir, ".beads"))
if filepath.Base(dir) == ".beads" {
addCandidate(dir)
}
}
}
if info, err := os.Stat(dbPath); err == nil && info.IsDir() {
addCandidate(dbPath)
}
if info, err := os.Stat(actualDBPath); err == nil && info.IsDir() {
addCandidate(actualDBPath)
}
addCandidate(filepath.Dir(dbPath))
addCandidate(filepath.Dir(actualDBPath))
addAncestorCandidates(filepath.Dir(dbPath))
addAncestorCandidates(filepath.Dir(actualDBPath))
if found := beads.FindBeadsDir(); found != "" {
addCandidate(found)
addCandidate(utils.CanonicalizePath(found))
}
for _, beadsDir := range candidates {
cfg, err := configfile.Load(beadsDir)
if err != nil || cfg == nil {
continue
}
if utils.PathsEqual(beadsDir, dbPath) || utils.PathsEqual(beadsDir, actualDBPath) {
return beadsDir
}
if utils.PathsEqual(cfg.DatabasePath(beadsDir), dbPath) || utils.PathsEqual(cfg.DatabasePath(beadsDir), actualDBPath) {
return beadsDir
}
}
return ""
}