diff --git a/cmd/bd/memory.go b/cmd/bd/memory.go index 4b9480126..5520470cd 100644 --- a/cmd/bd/memory.go +++ b/cmd/bd/memory.go @@ -98,6 +98,22 @@ Examples: verb = "Updated" } + // Footgun guard: `bd remember ` is a WRITE whose positional arg is the CONTENT, not a + // key. A common slip is treating it like a getter -- `bd remember some-existing-key` -- + // which slugifies the bare key back to itself and SILENTLY OVERWRITES that memory. Refuse + // when (a) no explicit --key was given, (b) the content round-trips through slugify + // unchanged (so it is already a bare slug, the tell-tale of a mistyped read), and (c) a + // memory with that key already exists. Existing keys are themselves slugify outputs, so + // this precisely catches "content names an existing memory" with ~zero false positives on + // real prose insights. Passing --key explicitly bypasses the guard (deliberate intent). + if memoryKeyFlag == "" && existing != "" && slugify(insight) == insight { + return HandleErrorRespectJSON( + "refusing to overwrite existing memory %q with its own key as content -- "+ + "`bd remember` WRITES (its positional arg is CONTENT, not a key). "+ + "To read it: `bd recall %s`. To overwrite anyway: `bd remember \"\" --key %s`", + key, key, key) + } + if err := store.SetConfig(ctx, storageKey, insight); err != nil { return HandleErrorRespectJSON("storing memory: %v", err) } diff --git a/cmd/bd/memory_embedded_test.go b/cmd/bd/memory_embedded_test.go index 23cf4c6e7..2248a56fc 100644 --- a/cmd/bd/memory_embedded_test.go +++ b/cmd/bd/memory_embedded_test.go @@ -146,6 +146,41 @@ func TestEmbeddedMemory(t *testing.T) { } }) + // Footgun guard: `bd remember ` (treating remember as a getter) must NOT + // silently overwrite that memory with its own key as content. + t.Run("remember_guard_blocks_key_as_content", func(t *testing.T) { + bdRemember(t, bd, dir, "the real insight worth keeping", "--key", "guard-key") + // Now fat-finger a "read": positional arg is the existing bare key. + out := bdRememberFail(t, bd, dir, "guard-key") + if !strings.Contains(out, "recall") { + t.Errorf("expected guard error to suggest `bd recall`, got: %s", out) + } + // The original content must be intact, not clobbered to "guard-key". + recalled := bdRecall(t, bd, dir, "guard-key") + if !strings.Contains(recalled, "the real insight worth keeping") { + t.Errorf("guard must preserve original content, got: %s", recalled) + } + }) + + t.Run("remember_guard_bypass_with_explicit_key", func(t *testing.T) { + bdRemember(t, bd, dir, "original", "--key", "bypass-key") + // Explicit --key signals deliberate intent and bypasses the guard. + bdRemember(t, bd, dir, "bypass-key", "--key", "bypass-key") + out := bdRecall(t, bd, dir, "bypass-key") + if !strings.Contains(out, "bypass-key") { + t.Errorf("explicit --key should bypass guard and overwrite, got: %s", out) + } + }) + + t.Run("remember_guard_allows_new_slug_content", func(t *testing.T) { + // Slug-shaped content is fine when no memory by that key exists yet (no overwrite). + bdRemember(t, bd, dir, "brand-new-slug-memory") + out := bdRecall(t, bd, dir, "brand-new-slug-memory") + if !strings.Contains(out, "brand-new-slug-memory") { + t.Errorf("expected new slug-keyed memory to be stored, got: %s", out) + } + }) + // ===== Memories List ===== t.Run("memories_list", func(t *testing.T) {