beads/cmd/bd/memory.go
Steve Yegge 387959695d
fix(dolt): pre-pull commit must include config (kv.memory.* pull-wedge) (#4412)
* fix(dolt): pre-pull commit must include config; auto-resolve convergent kv.memory.* config conflicts

bd remember stores persistent memories as kv.memory.* rows in the synced
config table. Commit() deliberately excludes config (GH#2455, to avoid
sweeping a concurrent writer's stale issue_prefix into an unrelated commit),
so the GH#2474 pre-pull auto-commit at pullFromRemote (store.go) and PullFrom
(federation.go) leaves config permanently dirty once any memory exists. The
"clean the working set before merging" step then leaves config dirty and
DOLT_MERGE refuses to start ("cannot merge with uncommitted changes"). Two
correct PRs colliding over config. Diagnosed from a live server-mode repro
(Wyvern wy-71t); see PROPOSAL-pull-config-wedge.md.

Fix (server mode): add commitBeforePull, which stages config explicitly via
DOLT_ADD rather than relying on CommitWithConfig's DOLT_COMMIT('-Am'). The
proposal author observed -Am not staging config reliably under the server-mode
stored-procedure path, and the new embedded test TestEmbeddedConfigCommitStagesConfig
confirms -Am DOES stage config in the embedded engine — so the unreliability is
server-mode-specific and explicit staging is the robust choice. Commit() and
commitBeforePull now share commitWorkingSet(includeConfig bool).

Hardening (both modes): making config a committed/synced table means two clones
editing the same memory key collide on a config merge conflict. Extend
TryAutoResolveMergeConflicts to auto-resolve config conflicts with --theirs ONLY
when every conflicted row is a kv.memory.* key (machine-convergent, same policy
as metadata); any other config key (issue_prefix above all) is left for the
operator. Without this the primary fix would trade a silent pull wedge for an
operator-visible conflict wedge on the exact bd remember workload.

Embedded pre-pull (CommitPending -> -Am) already stages config and is unchanged;
it gains the resolver hardening.

Tests: server-mode pull_config_wedge_test.go (commit-includes-config + resolver
positive/negative, run under Docker in CI) and embedded pull_config_wedge_test.go
(in-process: -Am-stages-config probe + resolver positive/negative, all green).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs: add pull-config-wedge diagnosis/proposal (PR #4412)

The diagnosis-and-patch write-up behind this PR: how GH#2455 (Commit excludes
config) and GH#2474 (pre-pull auto-commit) collide over the synced config table,
leaving kv.memory.* rows permanently uncommitted so DOLT_MERGE refuses to start.
Includes the live-DB validation (Wyvern wy-71t) and the rejected alternatives.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(dolt): adopt-pr maintainer fixes for pull/sync config wedge

Maintainer review fixups for PR #4412, squashed into one commit on top of
the contributor's branch.

Pre-merge config wedge (sync): bd federation sync committed with the
config-excluding Commit before its merge while this PR switched the pull
paths to commitBeforePull, so any DB that ran `bd remember` carried dirty
kv.memory.* config rows and hit the "cannot merge with uncommitted changes"
wedge. Sync now uses commitBeforePull too, with a regression test.

Pre-pull auto-commit safety (GH#2455): commitBeforePull no longer stages the
whole config table. It auto-commits only convergent memory rows
(kv.memory.*); if any other config key is dirty (issue_prefix above all) it
refuses with operator guidance instead of sweeping a stale value into the
merge basis, restoring the GH#2455 protection the broad pre-pull staging had
reopened.

Sync --strategy config commit: a config-only conflict resolution committed
through the config-excluding Commit, so it could report success without
creating the merge-resolution commit and re-wedge the next sync. Sync now
commits resolutions through a config-inclusive path (commitMergeResolution).

Embedded sync parity: EmbeddedDoltStore.Sync fetched and merged without the
pre-merge CommitPending guard already used by embedded Pull/PullRemote/PullFrom
and the new server Sync, so the same dirty kv.memory.* working set could still
wedge `bd federation sync` in embedded mode. Sync now commits pending changes
before fetch/merge (embedded Commit is DOLT_COMMIT('-Am'), which stages
config), with an embedded regression test that drives Sync with a dirty
kv.memory.* row.

KV/memory namespace collision: `bd kv set memory.*` stored to kv.memory.*,
indistinguishable from a `bd remember` memory the merge resolver auto-resolves
with --theirs. validateKVKey now reserves the memory. prefix so a generic KV
value can't be silently overridden by the remote on pull.

Shared prefix + tests: centralize the kv.memory. prefix in
internal/storage/kvkeys (used by cmd/bd and the merge resolver) with a
contract test; route the remaining cmd/bd kv. prefix literals (the doctor KV
check, the validateKVKey nested-prefix guard, and the memory listing) through
the kvkeys constants; and strengthen the server-mode non-memory config
conflict test to prove a conflict materialized before the resolver declines it.

Maintainer review fixup for PR #4412.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <ci@beads.test>
2026-06-19 21:31:33 -07:00

311 lines
7.5 KiB
Go

package main
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/storage/kvkeys"
)
// memoryPrefix is prepended (after kvPrefix) to all memory keys.
const memoryPrefix = kvkeys.MemoryPrefix
// memoryKeyFlag allows explicit key override for bd remember.
var memoryKeyFlag string
// slugify converts a string to a URL-friendly slug for use as a memory key.
// Takes the first ~8 words, lowercases, replaces non-alphanumeric with hyphens.
func slugify(s string) string {
s = strings.ToLower(s)
// Replace non-alphanumeric chars with hyphens
re := regexp.MustCompile(`[^a-z0-9]+`)
s = re.ReplaceAllString(s, "-")
s = strings.Trim(s, "-")
// Limit to first ~8 "words" (hyphen-separated segments)
parts := strings.SplitN(s, "-", 10)
if len(parts) > 8 {
parts = parts[:8]
}
slug := strings.Join(parts, "-")
// Cap total length
if len(slug) > 60 {
slug = slug[:60]
// Don't end on a hyphen
slug = strings.TrimRight(slug, "-")
}
return slug
}
// rememberCmd stores a memory.
var rememberCmd = &cobra.Command{
Use: `remember "<insight>"`,
Short: "Store a persistent memory",
Long: `Store a memory that persists across sessions and account rotations.
Memories are injected at prime time (bd prime) so you have them
in every session without manual loading.
Examples:
bd remember "always run tests with -race flag"
bd remember "Dolt phantom DBs hide in three places" --key dolt-phantoms
bd remember "auth module uses JWT not sessions" --key auth-jwt`,
GroupID: "setup",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
CheckReadonly("remember")
if err := ensureDirectMode("remember requires direct database access"); err != nil {
FatalError("%v", err)
}
insight := args[0]
if strings.TrimSpace(insight) == "" {
FatalErrorRespectJSON("memory content cannot be empty")
}
// Generate or use provided key
key := memoryKeyFlag
if key == "" {
key = slugify(insight)
}
if key == "" {
FatalErrorRespectJSON("could not generate key from content; use --key to specify one")
}
storageKey := kvPrefix + memoryPrefix + key
ctx := rootCtx
// Check if updating an existing memory
existing, _ := store.GetConfig(ctx, storageKey)
verb := "Remembered"
if existing != "" {
verb = "Updated"
}
if err := store.SetConfig(ctx, storageKey, insight); err != nil {
FatalErrorRespectJSON("storing memory: %v", err)
}
commandDidWrite.Store(true)
if jsonOutput {
outputJSON(map[string]string{
"key": key,
"value": insight,
"action": strings.ToLower(verb),
})
} else {
fmt.Printf("%s [%s]: %s\n", verb, key, truncateMemory(insight, 80))
}
},
}
// memoriesCmd lists and searches memories.
var memoriesCmd = &cobra.Command{
Use: "memories [search]",
Short: "List or search persistent memories",
Long: `List all memories, or search by keyword.
Examples:
bd memories # list all memories
bd memories dolt # search for memories about dolt
bd memories "race flag" # search for a phrase`,
GroupID: "setup",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := ensureDirectMode("memories requires direct database access"); err != nil {
FatalError("%v", err)
}
ctx := rootCtx
allConfig, err := store.GetAllConfig(ctx)
if err != nil {
FatalErrorRespectJSON("listing memories: %v", err)
}
// Filter for kv.memory.* keys
fullPrefix := kvkeys.MemoryConfigKeyPrefix
memories := make(map[string]string)
for k, v := range allConfig {
if strings.HasPrefix(k, fullPrefix) {
userKey := strings.TrimPrefix(k, fullPrefix)
memories[userKey] = v
}
}
// Apply search filter if provided
var search string
if len(args) > 0 {
search = strings.ToLower(args[0])
}
if search != "" {
filtered := make(map[string]string)
for k, v := range memories {
if strings.Contains(strings.ToLower(k), search) ||
strings.Contains(strings.ToLower(v), search) {
filtered[k] = v
}
}
memories = filtered
}
if jsonOutput {
outputJSON(memories)
return
}
if len(memories) == 0 {
if search != "" {
fmt.Printf("No memories matching %q\n", search)
} else {
fmt.Println("No memories stored. Use 'bd remember \"insight\"' to add one.")
}
return
}
// Sort keys for consistent output
keys := make([]string, 0, len(memories))
for k := range memories {
keys = append(keys, k)
}
sort.Strings(keys)
if search != "" {
fmt.Printf("Memories matching %q:\n\n", search)
} else {
fmt.Printf("Memories (%d):\n\n", len(memories))
}
for _, k := range keys {
v := memories[k]
fmt.Printf(" %s\n", k)
// Indent the value, wrapping long lines
fmt.Printf(" %s\n\n", truncateMemory(v, 120))
}
},
}
// forgetCmd removes a memory.
var forgetCmd = &cobra.Command{
Use: "forget <key>",
Short: "Remove a persistent memory",
Long: `Remove a memory by its key.
Use 'bd memories' to see available keys.
Examples:
bd forget dolt-phantoms
bd forget auth-jwt`,
GroupID: "setup",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
CheckReadonly("forget")
if err := ensureDirectMode("forget requires direct database access"); err != nil {
FatalError("%v", err)
}
key := args[0]
storageKey := kvPrefix + memoryPrefix + key
ctx := rootCtx
// Check if it exists first
existing, _ := store.GetConfig(ctx, storageKey)
if existing == "" {
if jsonOutput {
outputJSON(map[string]string{
"key": key,
"found": "false",
})
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "No memory with key %q\n", key)
os.Exit(1)
}
if err := store.DeleteConfig(ctx, storageKey); err != nil {
FatalErrorRespectJSON("forgetting memory: %v", err)
}
commandDidWrite.Store(true)
if jsonOutput {
outputJSON(map[string]string{
"key": key,
"deleted": "true",
})
} else {
fmt.Printf("Forgot [%s]: %s\n", key, truncateMemory(existing, 80))
}
},
}
// recallCmd retrieves a specific memory by key.
var recallCmd = &cobra.Command{
Use: "recall <key>",
Short: "Retrieve a specific memory",
Long: `Retrieve the full content of a memory by its key.
Examples:
bd recall dolt-phantoms
bd recall auth-jwt`,
GroupID: "setup",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := ensureDirectMode("recall requires direct database access"); err != nil {
FatalError("%v", err)
}
key := args[0]
storageKey := kvPrefix + memoryPrefix + key
ctx := rootCtx
value, err := store.GetConfig(ctx, storageKey)
if err != nil {
FatalErrorRespectJSON("recalling memory: %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, "No memory with key %q\n", key)
os.Exit(1)
}
fmt.Printf("%s\n", value)
}
},
}
// truncateMemory shortens a string to maxLen for display.
func truncateMemory(s string, maxLen int) string {
// Replace newlines with spaces for single-line display
s = strings.ReplaceAll(s, "\n", " ")
if len(s) <= maxLen {
return s
}
return s[:maxLen-3] + "..."
}
func init() {
rememberCmd.Flags().StringVar(&memoryKeyFlag, "key", "", "Explicit key for the memory (auto-generated from content if not set). If a memory with this key already exists, it will be updated in place")
rootCmd.AddCommand(rememberCmd)
rootCmd.AddCommand(memoriesCmd)
rootCmd.AddCommand(forgetCmd)
rootCmd.AddCommand(recallCmd)
}