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

228 lines
6.6 KiB
Go

package dolt
import (
"context"
"database/sql"
"fmt"
"log"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/storage/domain"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
)
// SetConfig sets a configuration value
func (s *DoltStore) SetConfig(ctx context.Context, key, value string) error {
if err := s.withRetryTx(ctx, func(tx *sql.Tx) error {
if err := issueops.SetConfigInTx(ctx, tx, key, value); err != nil {
return err
}
// Sync normalized tables when config keys change
switch key {
case "status.custom":
if err := issueops.SyncCustomStatusesTable(ctx, tx, value); err != nil {
return fmt.Errorf("syncing custom_statuses table: %w", err)
}
case "types.custom":
if err := issueops.SyncCustomTypesTable(ctx, tx, value); err != nil {
return fmt.Errorf("syncing custom_types table: %w", err)
}
}
return nil
}); err != nil {
return err
}
// Invalidate caches for keys that affect cached data
s.cacheMu.Lock()
switch key {
case "status.custom":
s.customStatusCached = false
s.customStatusCache = nil
s.customStatusDetailedCache = nil
case "types.custom":
s.customTypeCached = false
s.customTypeCache = nil
case "types.infra":
s.infraTypeCached = false
s.infraTypeCache = nil
}
s.cacheMu.Unlock()
return nil
}
// GetConfig retrieves a configuration value
func (s *DoltStore) GetConfig(ctx context.Context, key string) (string, error) {
var value string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
value, err = issueops.GetConfigInTx(ctx, tx, key)
return err
})
return value, err
}
// GetAllConfig retrieves all configuration values
func (s *DoltStore) GetAllConfig(ctx context.Context) (map[string]string, error) {
var result map[string]string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetAllConfigInTx(ctx, tx)
return err
})
return result, err
}
// DeleteConfig removes a configuration value
func (s *DoltStore) DeleteConfig(ctx context.Context, key string) error {
return s.withRetryTx(ctx, func(tx *sql.Tx) error {
return issueops.DeleteConfigInTx(ctx, tx, key)
})
}
// SetMetadata sets a metadata value
func (s *DoltStore) SetMetadata(ctx context.Context, key, value string) error {
return s.withRetryTx(ctx, func(tx *sql.Tx) error {
return issueops.SetMetadataInTx(ctx, tx, key, value)
})
}
// GetMetadata retrieves a metadata value
func (s *DoltStore) GetMetadata(ctx context.Context, key string) (string, error) {
var value string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
value, err = issueops.GetMetadataInTx(ctx, tx, key)
return err
})
return value, err
}
// SetLocalMetadata sets a value in the dolt-ignored local_metadata table.
// Used for clone-local state that should not generate merge conflicts.
func (s *DoltStore) SetLocalMetadata(ctx context.Context, key, value string) error {
return s.withRetryTx(ctx, func(tx *sql.Tx) error {
return issueops.SetLocalMetadataInTx(ctx, tx, key, value)
})
}
// GetLocalMetadata retrieves a value from the dolt-ignored local_metadata table.
// Returns ("", nil) if the key does not exist.
func (s *DoltStore) GetLocalMetadata(ctx context.Context, key string) (string, error) {
var value string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
value, err = issueops.GetLocalMetadataInTx(ctx, tx, key)
return err
})
return value, err
}
func (s *DoltStore) loadCustomConfigCache(ctx context.Context) {
s.cacheMu.Lock()
if s.customStatusCached && s.customTypeCached {
s.cacheMu.Unlock()
return
}
s.cacheMu.Unlock()
var statuses []types.CustomStatus
var customTypes []string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var resolveErr error
statuses, customTypes, resolveErr = issueops.ResolveCustomConfigInTx(ctx, tx)
return resolveErr
})
if err != nil {
log.Printf("warning: failed to resolve custom config: %v", err)
if yamlStatuses := config.GetCustomStatusesFromYAML(); len(yamlStatuses) > 0 {
statuses = issueops.ParseStatusFallback(yamlStatuses)
}
if yamlTypes := config.GetCustomTypesFromYAML(); len(yamlTypes) > 0 {
customTypes = yamlTypes
}
}
s.cacheMu.Lock()
if !s.customStatusCached {
s.customStatusDetailedCache = statuses
s.customStatusCache = types.CustomStatusNames(statuses)
s.customStatusCached = true
}
if !s.customTypeCached {
s.customTypeCache = customTypes
s.customTypeCached = true
}
s.cacheMu.Unlock()
}
func (s *DoltStore) GetCustomStatuses(ctx context.Context) ([]string, error) {
s.loadCustomConfigCache(ctx)
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
return s.customStatusCache, nil
}
func (s *DoltStore) GetCustomStatusesDetailed(ctx context.Context) ([]types.CustomStatus, error) {
s.loadCustomConfigCache(ctx)
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
return s.customStatusDetailedCache, nil
}
// GetCustomTypes returns custom issue type values from config.
// If the database doesn't have custom types configured, falls back to config.yaml.
// Returns an empty slice if no custom types are configured.
// Results are cached per DoltStore lifetime and invalidated when SetConfig
// updates the "types.custom" key.
func (s *DoltStore) GetCustomTypes(ctx context.Context) ([]string, error) {
s.loadCustomConfigCache(ctx)
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
return s.customTypeCache, nil
}
// GetInfraTypes returns infrastructure type names from config.
// Infrastructure types are routed to the wisps table to keep the versioned
// issues table clean. Defaults to ["agent", "rig", "role", "message"] if
// no custom configuration exists.
// Falls back: DB config "types.infra" → config.yaml types.infra → defaults.
// Results are cached per DoltStore lifetime and invalidated when SetConfig
// updates the "types.infra" key.
func (s *DoltStore) GetInfraTypes(ctx context.Context) map[string]bool {
s.cacheMu.Lock()
if s.infraTypeCached {
result := s.infraTypeCache
s.cacheMu.Unlock()
return result
}
s.cacheMu.Unlock()
var result map[string]bool
if err := s.withReadTx(ctx, func(tx *sql.Tx) error {
result = issueops.ResolveInfraTypesInTx(ctx, tx)
return nil
}); err != nil || result == nil {
// DB unavailable — fall back to YAML then defaults.
var typeList []string
if yamlTypes := config.GetInfraTypesFromYAML(); len(yamlTypes) > 0 {
typeList = yamlTypes
} else {
typeList = domain.DefaultInfraTypes()
}
result = make(map[string]bool, len(typeList))
for _, t := range typeList {
result[t] = true
}
}
s.cacheMu.Lock()
s.infraTypeCache = result
s.infraTypeCached = true
s.cacheMu.Unlock()
return result
}