fix(remember): refuse to clobber a memory when content is its own key

`bd remember <x>` 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's content with the literal key.

Guard: when no explicit --key was given, the content round-trips through
slugify unchanged (already a bare slug -- the tell-tale of a mistyped
read), and a memory with that key already exists, refuse and point the
user at `bd recall`. 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).

Adds guard/bypass/new-slug subtests to TestEmbeddedMemory.
This commit is contained in:
Steve Yegge 2026-06-27 15:34:02 -07:00
commit 33fbdb9c45
2 changed files with 51 additions and 0 deletions

View file

@ -98,6 +98,22 @@ Examples:
verb = "Updated"
}
// Footgun guard: `bd remember <x>` 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 \"<new content>\" --key %s`",
key, key, key)
}
if err := store.SetConfig(ctx, storageKey, insight); err != nil {
return HandleErrorRespectJSON("storing memory: %v", err)
}

View file

@ -146,6 +146,41 @@ func TestEmbeddedMemory(t *testing.T) {
}
})
// Footgun guard: `bd remember <existing-key>` (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) {