beads/internal/httpclient/memories.go
Stephanie Jarmak 559b5de1e2
fix(memory): preserve presence for empty values (#5964)
* fix(memory): preserve presence for empty values (#5963)

Recall and Forget now distinguish an existing empty memory row from an absent key across direct, embedded, and UOW storage paths.

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of sjarmak

* fix(memory): make bd recall report presence from row existence

printRecallResult (and the recallCmd caller) derived "found" from
value != "", so a memory stored as the empty string still printed
"No memory with key" and exited 1 even though the role's
RecallResult.Found was true and bd forget would delete the row.
Thread result.Found through instead: an empty-valued memory now
recalls cleanly (exit 0, empty value; JSON reports found: true), and
only an absent key is a miss.

Addresses maintainer review on #5964 (fixes #5963).

* test(httpapi): pin GET /v0/beads/memories/{key} as 200 for an empty value

TestGetMemoryAnswersAMissWithA404 pinned a RecallResult{Value: ""}
case (Found defaulting to its zero value, false) as a 404, a shape no
backend can produce since Found now comes from row existence rather
than value truthiness. Move that case into its own
TestGetMemoryAnswersAnEmptyValueWith200, which recalls Found: true
and asserts a 200 with an empty value. Also updates the handler's
doc comment, which still described the retired 404-on-empty
behavior.

Addresses maintainer review on #5964 (fixes #5963).

* docs(httpapi): fix openapi spec to say memory presence is row existence

The published spec still said an empty-valued memory is a 404 on
GET /v0/beads/memories/{key} and DELETE /v0/beads/memories/{key},
contradicting the role's RecallResult.Found (row existence). Rewrite
the operation descriptions, both 404 response bodies, Memory.value
and RememberedMemory.replaced to say presence is row existence
everywhere: an empty-string memory is a 200 (or is removed by
forget) with an empty value, matching what GET /v0/beads/memories
already enumerated. Regenerate types.gen.go via `make api-gen` so
the generated doc comments match.

Addresses maintainer review on #5964 (fixes #5963).

* docs(changelog): note the empty-valued memory presence fix

Add an Unreleased/Fixed entry for #5964 (fixes #5963): bd recall,
bd forget and GET /v0/beads/memories/{key} now treat an empty-valued
memory as present everywhere, instead of only on GET
/v0/beads/memories.

* fix(memory): make bd remember <bare-key> recall an empty-valued memory

rememberBareKeyPath decided presence from existing != "", so a bare slug
naming a memory stored as the empty string was refused as "no memory
named" instead of recalled. It now takes the role's Found, matching
printRecallResult, bd forget and the HTTP door.

* test(memory): pin empty-value presence through the real commands

TestPrintRecallResult and TestRememberBareKeyPath call the helpers directly,
so a RunE that went back to deciding presence from value != "" would leave
them green. TestEmbeddedMemory/empty_valued_memory_is_present empties a
memory row beneath bd and runs bd recall, bd remember <bare-key> and
bd forget against it, checking first that bd no longer sees the placeholder.

Two comments in problem.go and one in memories_test.go still said an
empty-valued memory is a 404. The CHANGELOG line now says a key with no row
is a miss, since a forgotten key was once stored.

* build(bazel): add the memoryops dep to the bd_test go_test (make bazel-sync)

memory_embedded_test.go now imports internal/storage/memoryops directly
(storagememoryops.StorageKey, to overwrite a memory row out of band), so
gazelle emits //internal/storage/memoryops on bd_test. cmd/bd/BUILD.bazel
landed on main after this PR branched, so the PR could not carry the dep;
without it, bazel.yml's "BUILD files in sync" step fails in the required
bazel-test lane. Generated by `make bazel-sync`.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(memory): maintainer fixup — adoption review nits

Maintainer fixup for the adopt-pr iteration-1 review of #5964. Both review
lanes approved and the quality scorecard approved at 981; these are its four
non-gating nits, each confined to lines this PR already adds.

- internal/httpapi: TestForgetMemoryOfAnEmptyValuedKeyIs200 pins the DELETE
  door's empty-value 200 the way TestGetMemoryAnswersAnEmptyValueWith200
  pins the read. Guarding the handler's 404 on Value == "" reddens exactly
  this test and nothing else.
- CHANGELOG: the bullet names DELETE /v0/beads/memories/{key} beside GET,
  whose answer changed the same way (404 with the row left standing -> 200
  with the row removed).
- uow: the two new GetAllConfig presence probes in Recall and Forget say why
  they read the whole table, by pointing at Remember's rationale.
- conformance: RunMemoriesEmptyRowIsPresentUntilForgotten runs its
  no-history-on-retry leg last and SKIPs it with a reason when a fixture has
  no CountHistory, as the MemoriesFixture doc promises, instead of passing
  quietly. All three in-tree fixtures wire the hook, so every in-tree run
  is unchanged (19/19 on dolt, embeddeddolt and uow, 0 SKIP).

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(httpclient): report an empty-valued memory as present (base-refresh repair)

Maintainer repair from refreshing #5964 onto main b630f96f5b. Main's #7247
added the HTTP client store (internal/httpclient) after this PR branched,
and its memory role and served conformance leg still treated a memory
stored as the empty string as absent:

- Recall set Found from Value != "".
- The RecallMemory doc said an empty-valued row was a 404.
- TestServedMemoriesRecallConflatesStoredEmptyWithAbsent called
  conformance.RunMemoriesRecallConflatesStoredEmptyWithAbsent, which this
  PR deletes.

The rebase applied cleanly, but the merged tree did not compile.

- internal/httpclient/memories.go: Recall reports Found true on any 200.
  With this PR the wire returns a 404 only for a key with no row; an
  empty-valued row comes back as a 200 with an empty value. Setting Found
  from Value would hide that row again on the HTTP backend. Forget already
  reported Found true on a 200.
- internal/httpclient/wire/writes.go: the RecallMemory doc now says the
  same.
- internal/httpclient/served_memories_deps_test.go: the hm10 served case
  now runs RunMemoriesEmptyRowIsPresentUntilForgotten, the contract case
  this PR adds in place of the one it deletes.

With BEADS_TEST_EMBEDDED_DOLT=1 the served memory tests pass with 0 SKIP.
Reverting the Recall line makes hm10 fail with
`Recall("hm10-stored-empty") = value "" found false, want value "" found
true`.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: sjarmak <t@t.co>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 19:28:54 +00:00

123 lines
4.8 KiB
Go

// Contributed by gascity from bd-enterprise (internal/enterprise/httpstore/memories.go@49d1df2f6)
// to OSS beads under the MIT license.
package httpclient
import (
"context"
"errors"
"strings"
"github.com/steveyegge/beads/internal/httpapi/apigen"
"github.com/steveyegge/beads/issueops"
"github.com/steveyegge/beads/memoryops"
)
// httpMemories serves memoryops.Memories from the four memory operations
// (design D8 row 15).
//
// The role is one plane and one interface, so the accessor cannot be flipped by
// halves: the two writes this bead owns arrive with the two reads that share
// their vocabulary, and all four go through the same key rules.
//
// THE MISS IS A RESULT, NOT AN ERROR. memoryops deliberately declares no
// ErrNotFound: a recall or a forget that found nothing answers Found false with
// a nil error. The wire spells the same miss as a 404, which the problem mapper
// turns into issueops.ErrNotFound — so the one piece of real translation in this
// file is turning that back into the role's own answer. Getting it wrong would
// make `bd recall` of an unknown key an error where every other backend prints
// "not found".
type httpMemories struct {
store *Store
wire WriteWire
}
var _ memoryops.Memories = (*httpMemories)(nil)
// Remember stores one memory. An ABSENT key asks the server to derive one from
// the content, which is why the wire member is a pointer: an empty string is not
// the same request as an omitted member, and the derived key comes back in the
// result so the caller learns where the memory landed.
func (m *httpMemories) Remember(ctx context.Context, req memoryops.RememberRequest) (memoryops.RememberResult, error) {
if strings.TrimSpace(req.Content) == "" {
return memoryops.RememberResult{}, invalid("content is required")
}
body := apigen.RememberRequest{Content: req.Content}
if req.Key != "" {
key := req.Key
body.Key = &key
}
res, err := m.wire.RememberMemory(ctx, body)
if err != nil {
return memoryops.RememberResult{}, err
}
return memoryops.RememberResult{Key: res.Key, Value: res.Value, Replaced: res.Replaced}, nil
}
// Recall reads one memory.
func (m *httpMemories) Recall(ctx context.Context, req memoryops.RecallRequest) (memoryops.RecallResult, error) {
if err := requireMemoryKey(req.Key); err != nil {
return memoryops.RecallResult{}, err
}
res, err := m.wire.RecallMemory(ctx, req.Key)
if errors.Is(err, issueops.ErrNotFound) {
return memoryops.RecallResult{Key: req.Key}, nil
}
if err != nil {
return memoryops.RecallResult{}, err
}
// Found is about the ROW, not the value: the wire spells a miss as the 404
// handled above, and a row stored as the empty string as a 200 with an
// empty value. Reading Found off Value would hide that row again.
return memoryops.RecallResult{Key: req.Key, Value: res.Value, Found: true}, nil
}
// Forget removes one memory and reports the value it removed.
func (m *httpMemories) Forget(ctx context.Context, req memoryops.ForgetRequest) (memoryops.ForgetResult, error) {
if err := requireMemoryKey(req.Key); err != nil {
return memoryops.ForgetResult{}, err
}
res, err := m.wire.ForgetMemory(ctx, req.Key)
if errors.Is(err, issueops.ErrNotFound) {
// Nothing was stored under the key, and nothing was deleted. A result,
// not an error: forgetting what was never remembered is the state the
// caller asked for.
return memoryops.ForgetResult{Key: req.Key}, nil
}
if err != nil {
return memoryops.ForgetResult{}, err
}
return memoryops.ForgetResult{Key: req.Key, Value: res.Value, Found: true}, nil
}
// List enumerates the memory plane, narrowed by Search.
//
// The folding is the ROLE's, so it happens server-side: the raw term goes on the
// wire as `search` and the server's own role lowercases both sides. Folding it
// here as well would be a second implementation of a rule that has one.
func (m *httpMemories) List(ctx context.Context, req memoryops.ListRequest) (memoryops.ListResult, error) {
page, err := m.wire.ListMemories(ctx, req.Search)
if err != nil {
return memoryops.ListResult{}, err
}
// Empty map, never nil: a caller ranges over it without a nil check, and
// "the plane is empty" is not something to spell as an absent answer.
out := make(map[string]string, len(page.Items))
for _, memory := range page.Items {
out[memory.Key] = memory.Value
}
return memoryops.ListResult{Memories: out}, nil
}
// requireMemoryKey applies the role's trim rule before a key becomes a path.
//
// The server refuses a blank key on these two operations as well, but as a 400
// alongside the 404 that means "no such memory" — and a caller cannot act on the
// difference if the client never made it. Refusing here keeps the role's
// ErrValidation promise and keeps a request that cannot succeed off the wire.
func requireMemoryKey(key string) error {
if strings.TrimSpace(key) == "" {
return invalid("memory key is required")
}
return nil
}