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

110 lines
3.2 KiB
Go

package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/routing"
"github.com/steveyegge/beads/internal/storage"
)
var routingConfigKeys = []string{
"routing.mode",
"routing.contributor",
"routing.default",
"routing.maintainer",
"contributor.auto_route",
"contributor.planning_repo",
}
func resolveRoutingConfigValue(key string, dbValues map[string]string) string {
if src := config.GetValueSource(key); src != config.SourceDefault {
if value := strings.TrimSpace(config.GetString(key)); value != "" {
return value
}
}
return strings.TrimSpace(dbValues[key])
}
func getRoutingConfigValue(ctx context.Context, store storage.DoltStorage, key string) string {
if src := config.GetValueSource(key); src != config.SourceDefault {
if value := strings.TrimSpace(config.GetString(key)); value != "" {
return value
}
}
if store == nil {
return ""
}
dbValue, err := store.GetConfig(ctx, key)
if err != nil {
debug.Logf("DEBUG: failed to read config %q from store: %v\n", key, err)
return ""
}
return strings.TrimSpace(dbValue)
}
func determineAutoRoutedRepoPath(ctx context.Context, store storage.DoltStorage) string {
userRole, err := routing.DetectUserRole(".")
if err != nil {
debug.Logf("Warning: failed to detect user role: %v\n", err)
}
var dbValues map[string]string
if store != nil {
all, allErr := store.GetAllConfig(ctx)
if allErr != nil {
debug.Logf("DEBUG: failed to read config from store: %v\n", allErr)
} else {
dbValues = make(map[string]string, len(routingConfigKeys))
for _, key := range routingConfigKeys {
if v, ok := all[key]; ok {
dbValues[key] = v
}
}
}
}
routingMode := resolveRoutingConfigValue("routing.mode", dbValues)
contributorRepo := resolveRoutingConfigValue("routing.contributor", dbValues)
if routingMode == "" {
if resolveRoutingConfigValue("contributor.auto_route", dbValues) == "true" {
routingMode = "auto"
}
}
if contributorRepo == "" {
contributorRepo = resolveRoutingConfigValue("contributor.planning_repo", dbValues)
}
routingConfig := &routing.RoutingConfig{
Mode: routingMode,
DefaultRepo: resolveRoutingConfigValue("routing.default", dbValues),
MaintainerRepo: resolveRoutingConfigValue("routing.maintainer", dbValues),
ContributorRepo: contributorRepo,
ExplicitOverride: "",
}
return routing.DetermineTargetRepo(routingConfig, userRole, ".")
}
// openRoutedReadStore opens the auto-routed target store for read commands.
// Returns routed=false when reads should stay in the current store.
func openRoutedReadStore(ctx context.Context, store storage.DoltStorage) (storage.DoltStorage, bool, error) {
repoPath := determineAutoRoutedRepoPath(ctx, store)
if repoPath == "" || repoPath == "." {
return nil, false, nil
}
targetRepoPath := routing.ExpandPath(repoPath)
targetBeadsDir := filepath.Join(targetRepoPath, ".beads")
targetStore, err := newReadOnlyStoreFromConfig(ctx, targetBeadsDir)
if err != nil {
return nil, false, fmt.Errorf("failed to open routed store at %s: %w", targetRepoPath, err)
}
return targetStore, true, nil
}