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

257 lines
6.5 KiB
Go

package main
import (
"fmt"
"os"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/storage/kvkeys"
)
// kvPrefix is prepended to all user keys to separate them from internal config
const kvPrefix = kvkeys.Prefix
// validateKVKey checks if a key is valid for the KV store.
// Returns an error if the key is invalid.
func validateKVKey(key string) error {
if key == "" {
return fmt.Errorf("key cannot be empty")
}
if strings.TrimSpace(key) == "" {
return fmt.Errorf("key cannot be only whitespace")
}
// Prevent keys that would create nested kv.kv.* prefixes
if strings.HasPrefix(key, kvPrefix) {
return fmt.Errorf("key cannot start with 'kv.' (would create nested prefix)")
}
// Reserve the persistent-memory namespace: a generic memory.* key would
// store to kv.memory.*, indistinguishable from a `bd remember` memory, and
// the merge resolver auto-resolves kv.memory.* conflicts with --theirs
// (GH#2474). Without this guard a user's deliberate kv value could be
// silently overridden by a remote on pull. Keep the namespace owned by
// bd remember / bd forget.
if strings.HasPrefix(key, kvkeys.MemoryPrefix) {
return fmt.Errorf("key cannot start with %q (reserved for persistent memories; use 'bd remember' / 'bd forget')", kvkeys.MemoryPrefix)
}
// Prevent keys that look like internal config
if strings.HasPrefix(key, "sync.") || strings.HasPrefix(key, "conflict.") ||
strings.HasPrefix(key, "federation.") || strings.HasPrefix(key, "jira.") ||
strings.HasPrefix(key, "linear.") || strings.HasPrefix(key, "export.") ||
strings.HasPrefix(key, "import.") {
return fmt.Errorf("key cannot start with reserved prefix %q", strings.Split(key, ".")[0]+".")
}
return nil
}
// kvCmd is the parent command for kv subcommands
var kvCmd = &cobra.Command{
Use: "kv",
GroupID: "setup",
Short: "Key-value store commands",
Long: `Commands for working with the beads key-value store.
The key-value store is useful for storing flags, environment variables,
or other user-defined data that persists across sessions.
Examples:
bd kv set mykey myvalue # Set a value
bd kv get mykey # Get a value
bd kv clear mykey # Delete a key
bd kv list # List all key-value pairs`,
}
// kvSetCmd sets a key-value pair
var kvSetCmd = &cobra.Command{
Use: "set <key> <value>",
Short: "Set a key-value pair",
Long: `Set a key-value pair in the beads key-value store.
This is useful for storing flags, environment variables, or other
user-defined data that persists across sessions.
Examples:
bd kv set feature_flag true
bd kv set api_endpoint https://api.example.com
bd kv set max_retries 3`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
CheckReadonly("kv set")
if err := ensureDirectMode("kv set requires direct database access"); err != nil {
FatalError("%v", err)
}
key := args[0]
if err := validateKVKey(key); err != nil {
FatalErrorRespectJSON("invalid key: %v", err)
}
value := args[1]
storageKey := kvPrefix + key
ctx := rootCtx
if err := store.SetConfig(ctx, storageKey, value); err != nil {
FatalErrorRespectJSON("setting key: %v", err)
}
if jsonOutput {
outputJSON(map[string]string{
"key": key,
"value": value,
})
} else {
fmt.Printf("Set %s = %s\n", key, value)
}
},
}
// kvGetCmd gets a value by key
var kvGetCmd = &cobra.Command{
Use: "get <key>",
Short: "Get a value by key",
Long: `Get a value from the beads key-value store.
Examples:
bd kv get feature_flag
bd kv get api_endpoint`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := ensureDirectMode("kv get requires direct database access"); err != nil {
FatalError("%v", err)
}
key := args[0]
storageKey := kvPrefix + key
ctx := rootCtx
value, err := store.GetConfig(ctx, storageKey)
if err != nil {
FatalErrorRespectJSON("getting key: %v", err)
}
if jsonOutput {
result := map[string]interface{}{
"key": key,
"value": value,
"found": value != "",
}
outputJSON(result)
if value == "" {
os.Exit(1)
}
} else {
if value == "" {
fmt.Fprintf(os.Stderr, "%s (not set)\n", key)
os.Exit(1)
} else {
fmt.Printf("%s\n", value)
}
}
},
}
// kvClearCmd deletes a key
var kvClearCmd = &cobra.Command{
Use: "clear <key>",
Short: "Delete a key-value pair",
Long: `Delete a key from the beads key-value store.
Examples:
bd kv clear feature_flag
bd kv clear api_endpoint`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
CheckReadonly("kv clear")
if err := ensureDirectMode("kv clear requires direct database access"); err != nil {
FatalError("%v", err)
}
key := args[0]
if err := validateKVKey(key); err != nil {
FatalErrorRespectJSON("invalid key: %v", err)
}
storageKey := kvPrefix + key
ctx := rootCtx
if err := store.DeleteConfig(ctx, storageKey); err != nil {
FatalErrorRespectJSON("deleting key: %v", err)
}
if jsonOutput {
outputJSON(map[string]string{
"key": key,
"deleted": "true",
})
} else {
fmt.Printf("Cleared %s\n", key)
}
},
}
// kvListCmd lists all key-value pairs
var kvListCmd = &cobra.Command{
Use: "list",
Short: "List all key-value pairs",
Long: `List all key-value pairs in the beads key-value store.
Examples:
bd kv list
bd kv list --json`,
Run: func(cmd *cobra.Command, args []string) {
if err := ensureDirectMode("kv list requires direct database access"); err != nil {
FatalError("%v", err)
}
ctx := rootCtx
allConfig, err := store.GetAllConfig(ctx)
if err != nil {
FatalErrorRespectJSON("listing keys: %v", err)
}
// Filter for kv.* keys and strip prefix
kvPairs := make(map[string]string)
for k, v := range allConfig {
if strings.HasPrefix(k, kvPrefix) {
userKey := strings.TrimPrefix(k, kvPrefix)
kvPairs[userKey] = v
}
}
if jsonOutput {
outputJSON(kvPairs)
return
}
if len(kvPairs) == 0 {
fmt.Println("No key-value pairs set")
return
}
// Sort keys for consistent output
keys := make([]string, 0, len(kvPairs))
for k := range kvPairs {
keys = append(keys, k)
}
sort.Strings(keys)
fmt.Println("\nKey-Value Store:")
for _, k := range keys {
fmt.Printf(" %s = %s\n", k, kvPairs[k])
}
},
}
func init() {
// Register all kv subcommands under kvCmd
kvCmd.AddCommand(kvSetCmd)
kvCmd.AddCommand(kvGetCmd)
kvCmd.AddCommand(kvClearCmd)
kvCmd.AddCommand(kvListCmd)
// Register kv command
rootCmd.AddCommand(kvCmd)
}