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

194 lines
6.9 KiB
Go

//go:build cgo
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/doltserver"
"github.com/steveyegge/beads/internal/lockfile"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/dbproxy/util"
"github.com/steveyegge/beads/internal/storage/dolt"
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
)
func usesSQLServer() bool {
if shouldUseGlobals() {
if serverMode || proxiedServerMode {
return true
}
} else if cmdCtx != nil && (cmdCtx.ServerMode || cmdCtx.ProxiedServerMode) {
return true
}
if doltserver.IsSharedServerMode() {
return true
}
return false // default: embedded
}
// isEmbeddedMode reports whether the command is using embedded Dolt storage.
func isEmbeddedMode() bool {
return !usesSQLServer()
}
func usesProxiedServer() bool {
if shouldUseGlobals() {
return proxiedServerMode
}
return cmdCtx != nil && cmdCtx.ProxiedServerMode
}
// newDoltStore creates a storage backend from an explicit config.
// Used by bd init and PersistentPreRun.
func newDoltStore(ctx context.Context, cfg *dolt.Config) (storage.DoltStorage, error) {
if cfg.ProxiedServer {
// TODO: this should not be a store
// it should be a uow provider
return nil, fmt.Errorf("proxy server store should be uow provider")
}
if cfg.ServerMode {
return dolt.New(ctx, cfg)
}
if cfg.ReadOnly {
// Read-only commands must not be bricked by the #4259
// remote-migrate gate (bd-578h9.5); server mode's ReadOnly opens
// already skip migration entirely.
return embeddeddolt.OpenForReadOnlyCommand(ctx, cfg.BeadsDir, cfg.Database, "main")
}
return embeddeddolt.Open(ctx, cfg.BeadsDir, cfg.Database, "main")
}
// acquireEmbeddedLock acquires an exclusive flock on the embeddeddolt data
// directory derived from beadsDir. The caller must defer lock.Unlock().
// Returns a no-op lock when serverMode is true (the server handles its own
// concurrency).
func acquireEmbeddedLock(beadsDir string, serverMode bool) (util.Unlocker, error) {
if serverMode {
return util.NoopLock{}, nil
}
dataDir := filepath.Join(beadsDir, "embeddeddolt")
lock, err := util.TryLock(filepath.Join(dataDir, ".lock"))
if err != nil {
if lockfile.IsLocked(err) {
return nil, fmt.Errorf("embeddeddolt: another process holds the exclusive lock on %s; "+
"the embedded backend supports only one writer at a time — "+
"use the dolt server backend for concurrent access", dataDir)
}
return nil, fmt.Errorf("embeddeddolt: acquiring lock: %w", err)
}
return lock, nil
}
// newDoltStoreFromConfig creates a storage backend from the beads directory's
// persisted metadata.json configuration. Uses embedded Dolt by default;
// connects to dolt sql-server when dolt_mode is "server".
//
// For embedded mode, legacy hyphenated database names (pre-GH#2142) are
// auto-sanitized to underscores and the fix is persisted to metadata.json.
func newDoltStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) {
cfg, err := configfile.Load(beadsDir)
if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() {
// TODO: this needs to be uow provider
return nil, fmt.Errorf("proxy server store should be uow provider")
// return newProxiedServerStore(ctx, &dolt.Config{
// BeadsDir: beadsDir,
// Database: cfg.GetDoltDatabase(),
// ProxiedServer: true,
// })
}
if err == nil && cfg != nil && cfg.IsDoltServerMode() {
return dolt.NewFromConfig(ctx, beadsDir)
}
database := configfile.DefaultDoltDatabase
if cfg != nil {
database = cfg.GetDoltDatabase()
}
if sanitized := sanitizeDBName(database); sanitized != database {
if err := migrateHyphenatedDB(beadsDir, cfg, database, sanitized); err != nil {
return nil, fmt.Errorf("auto-sanitize database name %q → %q: %w", database, sanitized, err)
}
database = sanitized
}
return embeddeddolt.Open(ctx, beadsDir, database, "main")
}
// migrateHyphenatedDB renames a legacy hyphenated database directory and
// persists the sanitized name to metadata.json so subsequent opens use it.
// This handles projects initialized before GH#2142 that upgrade to
// embedded-mode-default builds (GH#3231).
func migrateHyphenatedDB(beadsDir string, cfg *configfile.Config, oldName, newName string) error {
dataDir := filepath.Join(beadsDir, "embeddeddolt")
oldDir := filepath.Join(dataDir, oldName)
newDir := filepath.Join(dataDir, newName)
oldExists := false
if info, err := os.Stat(oldDir); err == nil && info.IsDir() {
oldExists = true
}
if oldExists {
_, newErr := os.Stat(newDir)
switch {
case newErr == nil:
return fmt.Errorf("cannot auto-migrate database: both %q and %q exist under %s; remove one manually and retry",
oldName, newName, dataDir)
case !os.IsNotExist(newErr):
return fmt.Errorf("checking target directory %q: %w", newDir, newErr)
default:
if err := os.Rename(oldDir, newDir); err != nil {
return fmt.Errorf("renaming database directory: %w", err)
}
fmt.Fprintf(os.Stderr, "bd: migrated database directory %q → %q (GH#3231)\n", oldName, newName)
}
}
if cfg != nil && cfg.DoltDatabase != newName {
cfg.DoltDatabase = newName
if err := cfg.Save(beadsDir); err != nil {
return fmt.Errorf("persisting sanitized database name to metadata.json: %w", err)
}
fmt.Fprintf(os.Stderr, "bd: updated metadata.json dolt_database %q → %q (GH#3231)\n", oldName, newName)
}
return nil
}
// newReadOnlyStoreFromConfig creates a read-only storage backend from the beads
// directory's persisted metadata.json configuration.
//
// For embedded mode, invalid characters (hyphens, dots) are sanitized in-memory
// only — no directory renames or metadata.json writes. This prevents cross-repo
// hydration from mutating foreign projects (GH#3231).
func newReadOnlyStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) {
cfg, err := configfile.Load(beadsDir)
if err == nil && cfg != nil && cfg.IsDoltProxiedServerMode() {
// TODO: this needs to be uow provider
return nil, fmt.Errorf("proxy server store needs to be uow provider")
// return newProxiedServerStore(ctx, &dolt.Config{
// BeadsDir: beadsDir,
// Database: cfg.GetDoltDatabase(),
// ProxiedServer: true,
// ReadOnly: true,
// })
}
if err == nil && cfg != nil && cfg.IsDoltServerMode() {
return dolt.NewFromConfigWithOptions(ctx, beadsDir, &dolt.Config{ReadOnly: true})
}
database := configfile.DefaultDoltDatabase
if cfg != nil {
database = cfg.GetDoltDatabase()
}
if sanitized := sanitizeDBName(database); sanitized != database {
database = sanitized
}
// OpenReadOnly, not Open: a read-only open of a foreign project must not
// run the remote-migrate gate (a behind, remote-backed database would fail
// hard) and must not write migrations into the target's history
// (bd-6dnrw.32, GH#3231).
return embeddeddolt.OpenReadOnly(ctx, beadsDir, database, "main")
}