beads/cmd/bd/kv_test.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

233 lines
6.2 KiB
Go

//go:build cgo
package main
import (
"context"
"strings"
"testing"
)
func TestKVCommands(t *testing.T) {
ctx := context.Background()
testStore, cleanup := setupTestDB(t)
defer cleanup()
// Test set and get
t.Run("set and get", func(t *testing.T) {
key := "kv.test_key"
value := "test_value"
err := testStore.SetConfig(ctx, key, value)
if err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
got, err := testStore.GetConfig(ctx, key)
if err != nil {
t.Fatalf("GetConfig failed: %v", err)
}
if got != value {
t.Errorf("Expected %q, got %q", value, got)
}
})
// Test get nonexistent key
t.Run("get nonexistent", func(t *testing.T) {
got, err := testStore.GetConfig(ctx, "kv.nonexistent")
if err != nil {
t.Fatalf("GetConfig failed: %v", err)
}
if got != "" {
t.Errorf("Expected empty string for nonexistent key, got %q", got)
}
})
// Test update existing key
t.Run("update existing", func(t *testing.T) {
key := "kv.update_test"
err := testStore.SetConfig(ctx, key, "original")
if err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
err = testStore.SetConfig(ctx, key, "updated")
if err != nil {
t.Fatalf("SetConfig update failed: %v", err)
}
got, err := testStore.GetConfig(ctx, key)
if err != nil {
t.Fatalf("GetConfig failed: %v", err)
}
if got != "updated" {
t.Errorf("Expected 'updated', got %q", got)
}
})
// Test delete
t.Run("delete", func(t *testing.T) {
key := "kv.delete_test"
err := testStore.SetConfig(ctx, key, "to_delete")
if err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
err = testStore.DeleteConfig(ctx, key)
if err != nil {
t.Fatalf("DeleteConfig failed: %v", err)
}
got, err := testStore.GetConfig(ctx, key)
if err != nil {
t.Fatalf("GetConfig after delete failed: %v", err)
}
if got != "" {
t.Errorf("Expected empty string after delete, got %q", got)
}
})
// Test delete nonexistent (should not error)
t.Run("delete nonexistent", func(t *testing.T) {
err := testStore.DeleteConfig(ctx, "kv.never_existed")
if err != nil {
t.Fatalf("DeleteConfig for nonexistent key should not error: %v", err)
}
})
// Test list (GetAllConfig with kv prefix filtering)
t.Run("list kv pairs", func(t *testing.T) {
// Set some KV pairs
err := testStore.SetConfig(ctx, "kv.list_test_1", "value1")
if err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
err = testStore.SetConfig(ctx, "kv.list_test_2", "value2")
if err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
// Also set a non-KV config to ensure filtering works
err = testStore.SetConfig(ctx, "other.config", "should_not_appear")
if err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
allConfig, err := testStore.GetAllConfig(ctx)
if err != nil {
t.Fatalf("GetAllConfig failed: %v", err)
}
// Filter for kv.* keys (mimicking kvListCmd logic)
kvCount := 0
for k := range allConfig {
if len(k) > 3 && k[:3] == "kv." {
kvCount++
}
}
if kvCount < 2 {
t.Errorf("Expected at least 2 kv.* entries, got %d", kvCount)
}
// Verify non-KV config exists but would be filtered
if allConfig["other.config"] != "should_not_appear" {
t.Errorf("Expected other.config to exist in raw config")
}
})
// Test special characters in values
t.Run("special characters", func(t *testing.T) {
testCases := []struct {
name string
value string
}{
{"url", "https://api.example.com/path?query=1&other=2"},
{"json", `{"key": "value", "num": 42}`},
{"multiline", "line1\nline2\nline3"},
{"unicode", "Hello \u4e16\u754c"},
{"empty", ""},
{"spaces", " leading and trailing "},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
key := "kv.special_" + tc.name
err := testStore.SetConfig(ctx, key, tc.value)
if err != nil {
t.Fatalf("SetConfig failed for %s: %v", tc.name, err)
}
got, err := testStore.GetConfig(ctx, key)
if err != nil {
t.Fatalf("GetConfig failed for %s: %v", tc.name, err)
}
if got != tc.value {
t.Errorf("Expected %q, got %q", tc.value, got)
}
})
}
})
}
func TestKVPrefix(t *testing.T) {
// Verify the kvPrefix constant matches expected value
if kvPrefix != "kv." {
t.Errorf("Expected kvPrefix to be 'kv.', got %q", kvPrefix)
}
}
func TestValidateKVKey(t *testing.T) {
testCases := []struct {
name string
key string
wantErr bool
errMsg string
}{
// Valid keys
{"simple key", "mykey", false, ""},
{"key with underscore", "my_key", false, ""},
{"key with dots", "my.key.name", false, ""},
{"key with numbers", "key123", false, ""},
// Invalid keys
{"empty key", "", true, "cannot be empty"},
{"whitespace only", " ", true, "cannot be only whitespace"},
{"kv prefix", "kv.nested", true, "cannot start with 'kv.'"},
{"sync prefix", "sync.remote", true, "reserved prefix"},
{"conflict prefix", "conflict.strategy", true, "reserved prefix"},
{"federation prefix", "federation.remote", true, "reserved prefix"},
{"jira prefix", "jira.url", true, "reserved prefix"},
{"linear prefix", "linear.key", true, "reserved prefix"},
{"export prefix", "export.path", true, "reserved prefix"},
{"import prefix", "import.path", true, "reserved prefix"},
// memory.* is reserved for `bd remember`: a generic kv.memory.* key is
// indistinguishable from a memory and the merge resolver auto-resolves it
// with --theirs (GH#2474), so it must not be settable via `bd kv set`.
{"memory prefix", "memory.foo", true, "reserved for persistent memories"},
{"memory prefix slug", "memory.test-wedge", true, "reserved for persistent memories"},
// A key that merely contains "memory" but does not start with the prefix
// is still valid — only the actual namespace is reserved.
{"memory not a prefix", "my.memory.note", false, ""},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := validateKVKey(tc.key)
if tc.wantErr {
if err == nil {
t.Errorf("Expected error for key %q, got nil", tc.key)
} else if tc.errMsg != "" && !strings.Contains(err.Error(), tc.errMsg) {
t.Errorf("Expected error containing %q, got %q", tc.errMsg, err.Error())
}
} else {
if err != nil {
t.Errorf("Unexpected error for key %q: %v", tc.key, err)
}
}
})
}
}