beads/cmd/bd/memory_embedded_test.go
Bee 1ea5145092
feat(remember): opt-in memories.budget-chars — warn at 80%, refuse a write that crosses the corpus budget (--force overrides) (#6508)
* feat(remember): opt-in memories.budget-chars — refuse a write that crosses the corpus budget (wy-2pox7y)

The wheelhouse-side tripwire only REPORTS the memory-corpus size, so the corpus
re-trips red every few days and nothing has ever stopped the write that crossed
the line. This is the stop.

New config key `memories.budget-chars` (integer, default 0 = OFF), wired exactly
like `prime.max-memory-chars`: recognized in cmd/bd/config.go, routed to yaml in
internal/config YamlOnlyKeys (it is read through viper, so a DB-backed write
would be accepted and then never read), validated non-negative at set time, and
documented in docs/reference/configuration.md.

With the budget ON, `bd remember` measures the corpus as the sum over every
memory of len(key)+len(content) — BYTES, the same unit `bd prime`'s
--max-memory-chars counts in — projected to AFTER the write would apply. An
overwrite replaces that key's old content in the sum rather than adding to it,
so a memory near the ceiling can still be edited.

  - over budget            -> refused, nothing written, exit 1, one line on stderr
  - over budget + --force  -> written, and the crossing is still announced
  - >= 80% and within      -> written, then warned
  - below 80%, or budget 0 -> silent; with the budget off the corpus is not even
                              read, so behavior is byte-identical to before

Every line goes to STDERR, so --json output is never corrupted; a --json refusal
additionally gets the normal machine-readable error object on stdout.

The whole decision is one pure function (rememberBudgetVerdict), which is what
lets the branches be pinned without a database.

Tests run (targeted, never ./...):
  go build ./...                                             rc 0
  go vet ./cmd/bd/ ./internal/config/                        rc 0
  go test ./cmd/bd/ -run 'Memor|Remember|Prime|Budget' -count=1   rc 0
  go test ./internal/config/ -count=1                        rc 0
  BEADS_TEST_EMBEDDED_DOLT=1 go test ./cmd/bd/ \
      -run TestEmbeddedMemoryCorpusBudget -count=1 -v        rc 0 (6/6 subtests)
  golangci-lint run --config=.golangci.yml --build-tags=gms_pure_go \
      ./cmd/bd/... ./internal/config/...                     0 issues in touched
                                                             files (one
                                                             pre-existing gosec
                                                             G304 in
                                                             internal/config/permissions_open_nonlinux.go)

New tests: cmd/bd/memory_budget_pure_test.go (byte unit incl. multibyte,
overwrite delta, budget-off silence, config read + negative clamp, the
at-budget/one-over boundary, --force, the 80% warn band and its edges,
unclamped percentage, single-line contract);
cmd/bd/memory_embedded_test.go TestEmbeddedMemoryCorpusBudget (the same five
spec points end to end against a real embedded-Dolt store, through
`bd config set`, asserting the exact stderr lines, the nonzero exit, and that a
refused write leaves the old content intact);
internal/config/yaml_config_test.go validation + yaml-only routing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013XmKxqKuXTaKivDiCoBwBV

* fix(remember): one --json refusal line, ceil on the over-budget percentage (wy-2pox7y)

Review fixes on ca9f14d65d. Three items; one review item turned out to be a
false alarm and is recorded here rather than acted on.

- BLOCKER 1 (CLI docs regeneration) — NO CHANGE NEEDED, and that is the
  finding. docs/cli-reference/ and docs/CLI_REFERENCE.md are not generated
  from this checkout: docs/cli-docs.pin pins them to release tag v1.2.2, and
  scripts/generate-cli-docs.sh, check-cli-docs-drift.sh and check-doc-flags.sh
  all resolve that pin (resolve-docs-bd.sh) and build bd from the TAG, because
  the published docs site describes the latest release, not main's source. No
  CI path sets BD_DOCS_IGNORE_PIN. So changing `bd remember`'s help surface on
  main cannot move the generated docs, and it did not:
  ./scripts/generate-cli-docs.sh ran to completion ("Generated CLI docs from:
  bd version 1.2.2 (dev)") and left the tree clean — zero diff. The gate is
  green at this commit and was green at ca9f14d65d. The --force flag and the
  Corpus budget paragraph reach the docs when the pin is bumped at release.

- NIT 1: the --json refusal said the same sentence twice, prose on stderr AND
  the envelope on stdout. It now says it ONCE, the way
  HandleErrorWithHintRespectJSON does it: --json gets only the stdout
  envelope, every other route gets the bare sentence on stderr. New subtest
  TestEmbeddedMemoryCorpusBudget/json_refusal_is_said_once asserts a nonzero
  exit, EXACTLY one JSON document on stdout (a second Decode must be io.EOF),
  the refusal sentence inside the envelope, a stderr with no "memory corpus"
  in it, and that the refused write left the old content intact.

- NIT 2: floor arithmetic made the refusal at 201 of 200 read "(100%)" — the
  exactly-at-budget value this code deliberately ALLOWS, so the refusal wore
  the number of the case it is not. The percentage now CEILINGS over the
  budget (101%) and still FLOORS within it. The split is on the VALUE, not on
  --force, so one projection cannot report two different percentages
  depending on a flag; that is why the forced over-budget line moved too.
  TestRememberBudgetVerdictBoundary, TestRememberBudgetVerdictForceWrites and
  TestMemoryBudgetLinePercentIsUnclamped updated; the warn band gains a
  999-of-1000 case pinning the floor inside the budget, so "100%" can only
  ever mean at-budget.

- NIT 5: one comment at the List/Remember seam saying the budget is ADVISORY
  and not atomic — the two are separate transactions, so concurrent writers
  can both measure inside the budget and both land. No locking.

Nits 3 (primeConfigInt duplication) and 4 were left alone by instruction.

Gates run from the worktree:
  go build ./...                                                      rc 0
  go vet ./cmd/bd/ ./internal/config/                                 rc 0
  go test ./cmd/bd/ -run 'Memor|Remember|Prime|Budget' -count=1       rc 0
  go test ./internal/config/ -count=1                                 rc 0
  BEADS_TEST_EMBEDDED_DOLT=1 go test ./cmd/bd/ \
      -run TestEmbeddedMemoryCorpusBudget -count=1 -v                 rc 0 (7/7)
  gofmt -l cmd/bd internal/config                                     no output
  golangci-lint run ./cmd/bd/ ./internal/config/                      1 issue, all
      pre-existing: gosec G304 in internal/config/permissions_open_nonlinux.go
      (last touched f4d496fed, not by this branch)
  ./scripts/check-cli-docs-drift.sh                                   rc 0
  ./scripts/check-doc-flags.sh                                        rc 0

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFSKdUYCTyuhBM97tHyoye

* ci: list the PR's tests in the embedded shard manifest and cmd/bd BUILD.bazel

#7156 made the embedded-Dolt shard manifests exact-once, and Bazel equivalence
requires every test file in the go_test srcs. Adds TestEmbeddedMemoryCorpusBudget
(generator output, incremental) and memory_budget_pure_test.go (the line CI's
bazel-sync patch printed).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(remember): budget the key ResolveKey writes; hint on stderr too

Review fixes for #6508 (apply-fixes iteration 1; all minor or nit):

- The budget credits an overwrite under memoryapi.ResolveKey's key, the
  one rule every Remember implementation writes under, instead of a
  parallel flag-else-derived copy of it. A write ResolveKey refuses
  skips the budget, so a whitespace-only --key gets the role's "memory
  key must not be empty" instead of an over-budget refusal that points
  at --force.
- The stderr refusal now carries the same hint as the --json envelope
  (raise the budget, forget a memory, or --force), printed the way
  HandleErrorWithHintRespectJSON prints one.
- The memoryCorpusChars comment and `bd remember --help` no longer
  imply that a budget equal to prime.max-memory-chars keeps prime from
  eliding memories: the unit (bytes) is the same, but prime counts each
  memory as rendered, markup included, so the budget has to sit below
  that cap for that.
- New embedded pins: a write without --key that crosses the budget is
  refused with exit 1 and writes nothing; a blank --key gets the
  validation error; the Hint line is on stderr and not under --json.
  Each pin was checked red against a mutant of the code it guards.

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

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
Co-authored-by: Eddie the Engineer <julianknutsen@users.noreply.github.com>
2026-10-06 23:04:06 +00:00

712 lines
27 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
)
// countMemoryDoltCommits reads dolt_log for an embedded workspace. It is how
// the auto-commit epilogue is observed: a direct-route write that never flags
// it still answers `bd recall` perfectly well, because the row is in the
// working set — dolt_log is the only place the difference shows.
func countMemoryDoltCommits(t *testing.T, beadsDir string) int {
t.Helper()
cfg, _ := configfile.Load(beadsDir)
database := ""
if cfg != nil {
database = cfg.GetDoltDatabase()
}
db, cleanup, err := embeddeddolt.OpenSQL(t.Context(), filepath.Join(beadsDir, "embeddeddolt"), database, "main")
if err != nil {
t.Fatalf("OpenSQL: %v", err)
}
defer cleanup()
var count int
if err := db.QueryRowContext(t.Context(), "SELECT COUNT(*) FROM dolt_log").Scan(&count); err != nil {
t.Fatalf("query dolt_log: %v", err)
}
return count
}
// bdRemember runs "bd remember" with the given args and returns stdout.
func bdRemember(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"remember"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd remember %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdRememberFail runs "bd remember" expecting failure.
func bdRememberFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"remember"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd remember %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
// bdRecall runs "bd recall" with the given args and returns stdout.
func bdRecall(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"recall"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd recall %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdRecallFail runs "bd recall" expecting failure.
func bdRecallFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"recall"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd recall %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
// bdMemories runs "bd memories" with the given args and returns stdout.
func bdMemories(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"memories"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd memories %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdForget runs "bd forget" with the given args and returns stdout.
func bdForget(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"forget"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd forget %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdForgetFail runs "bd forget" expecting failure.
func bdForgetFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"forget"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd forget %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
func TestEmbeddedMemory(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "tm")
// ===== Remember and Recall =====
t.Run("remember_and_recall", func(t *testing.T) {
bdRemember(t, bd, dir, "always run tests with -race flag")
// Recall using auto-generated key — list memories to find it
out := bdMemories(t, bd, dir)
if !strings.Contains(out, "always run tests with -race flag") {
t.Errorf("expected memory in list: %s", out)
}
})
t.Run("remember_with_key", func(t *testing.T) {
bdRemember(t, bd, dir, "auth module uses JWT not sessions", "--key", "auth-jwt")
out := bdRecall(t, bd, dir, "auth-jwt")
if !strings.Contains(out, "JWT") {
t.Errorf("expected 'JWT' in recall output: %s", out)
}
})
t.Run("remember_overwrite", func(t *testing.T) {
bdRemember(t, bd, dir, "first version", "--key", "overwrite-test")
bdRemember(t, bd, dir, "second version", "--key", "overwrite-test")
out := bdRecall(t, bd, dir, "overwrite-test")
if !strings.Contains(out, "second version") {
t.Errorf("expected 'second version' after overwrite: %s", out)
}
})
// Desire path: `bd remember <existing-key>` (treating remember as a getter) is a READ --
// it recalls the memory instead of overwriting it with its own key as content.
t.Run("remember_bare_existing_key_recalls", func(t *testing.T) {
bdRemember(t, bd, dir, "the real insight worth keeping", "--key", "guard-key")
// Fat-finger a "read": positional arg is the existing bare key. Succeeds as a recall.
out := bdRemember(t, bd, dir, "guard-key")
if !strings.Contains(out, "the real insight worth keeping") {
t.Errorf("expected bare existing key to recall the memory content, 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("recall-instead-of-write 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_bare_slug_without_memory_refuses", func(t *testing.T) {
// A bare slug naming NO existing memory is a mistyped read (or junk content) --
// refuse rather than silently creating a memory whose content is its own key.
out := bdRememberFail(t, bd, dir, "brand-new-slug-memory")
if !strings.Contains(out, "memories") {
t.Errorf("expected refusal to point at `bd memories`, got: %s", out)
}
// Nothing may have been stored.
bdRecallFail(t, bd, dir, "brand-new-slug-memory")
})
// The two refusals that sit either side of the desire path, and the reason
// the bare-slug test is `derived != "" && derived == insight` rather than
// the shipped `slugify(insight) == insight`: DeriveKey("") is "", so empty
// and underivable content satisfies derived == insight and would be routed
// into a "recall" of the empty key instead of being refused. The shipped
// code was saved from that by an empty-content check that ran BEFORE the
// branch; that check belongs to the role now, so the branch has to exclude
// the empty key itself.
t.Run("remember_refuses_content_no_key_derives_from", func(t *testing.T) {
for _, tc := range []struct{ insight, want string }{
{"", "memory content cannot be empty"},
{" ", "memory content cannot be empty"},
{"!!!", "could not generate key from content"},
} {
out := bdRememberFail(t, bd, dir, tc.insight)
if !strings.Contains(out, tc.want) {
t.Errorf("bd remember %q: expected %q, got: %s", tc.insight, tc.want, out)
}
if strings.Contains(out, "a bare existing key READS") || strings.Contains(out, "no memory named") {
t.Errorf("bd remember %q took the bare-slug desire path, got: %s", tc.insight, out)
}
}
})
t.Run("remember_new_slug_with_explicit_key_stores", func(t *testing.T) {
// --key states write intent: slug-like content stores fine.
bdRemember(t, bd, dir, "brand-new-slug-memory", "--key", "brand-new-slug-memory")
out := bdRecall(t, bd, dir, "brand-new-slug-memory")
if !strings.Contains(out, "brand-new-slug-memory") {
t.Errorf("expected --key to store slug content, got: %s", out)
}
})
// ===== Memories List =====
t.Run("memories_list", func(t *testing.T) {
out := bdMemories(t, bd, dir)
// Should show at least the memories we stored above
if !strings.Contains(out, "auth-jwt") {
t.Errorf("expected 'auth-jwt' in memories list: %s", out)
}
})
t.Run("memories_search", func(t *testing.T) {
bdRemember(t, bd, dir, "dolt phantom DBs hide in three places", "--key", "dolt-phantoms")
out := bdMemories(t, bd, dir, "dolt")
if !strings.Contains(out, "dolt") {
t.Errorf("expected 'dolt' related memory in search: %s", out)
}
})
t.Run("memories_search_no_match", func(t *testing.T) {
out := bdMemories(t, bd, dir, "zzz_nonexistent_term_xyz")
// Should succeed but show no results or empty
_ = out
})
// ===== Forget =====
t.Run("forget", func(t *testing.T) {
bdRemember(t, bd, dir, "temporary memory to forget", "--key", "forget-me")
out := bdRecall(t, bd, dir, "forget-me")
if !strings.Contains(out, "temporary memory") {
t.Fatalf("expected memory before forget: %s", out)
}
bdForget(t, bd, dir, "forget-me")
// After forget, recall should fail
bdRecallFail(t, bd, dir, "forget-me")
})
// THE AUTO-COMMIT EPILOGUE, which is the trap this convergence was most
// likely to spring. A direct-route write lands in the Dolt WORKING SET and
// nothing else commits it: durability is the epilogue in main.go, keyed on
// commandDidWrite, which noteDirectMemoryWrite is the only thing that sets
// for this plane. A converged RunE that forgets it produces memories that
// exist until the process exits and then sit uncommitted — and every other
// assertion in this file still passes, because a working-set row recalls
// and lists exactly like a committed one.
//
// Its own workspace, so the counts are deterministic (the pattern
// close_multiple_ids_single_dolt_commit uses).
t.Run("write_verbs_flag_the_auto_commit_epilogue", func(t *testing.T) {
edir, ebeads, _ := bdInit(t, bd, "--prefix", "ep")
// Warm-up write: a fresh workspace's first command may commit
// bookkeeping of its own, which is not what this is measuring.
bdRemember(t, bd, edir, "warm the workspace up", "--key", "epilogue-warmup")
before := countMemoryDoltCommits(t, ebeads)
bdRemember(t, bd, edir, "the epilogue is what makes a direct write durable", "--key", "epilogue-subject")
afterRemember := countMemoryDoltCommits(t, ebeads)
if got := afterRemember - before; got != 1 {
t.Errorf("bd remember: dolt_log %d -> %d (delta %d), want exactly one auto-commit", before, afterRemember, got)
}
bdForget(t, bd, edir, "epilogue-subject")
afterForget := countMemoryDoltCommits(t, ebeads)
if got := afterForget - afterRemember; got != 1 {
t.Errorf("bd forget: dolt_log %d -> %d (delta %d), want exactly one auto-commit", afterRemember, afterForget, got)
}
// A forget that found nothing deleted nothing, so it must leave no
// commit either — the other half of the contract, and the one a
// `noteDirectMemoryWrite()` placed before the not-found branch would
// break while every output assertion stayed green.
bdForgetFail(t, bd, edir, "epilogue-subject")
if got := countMemoryDoltCommits(t, ebeads); got != afterForget {
t.Errorf("bd forget of an absent key: dolt_log %d -> %d, want no commit", afterForget, got)
}
})
// ===== Error Cases =====
t.Run("recall_missing_key", func(t *testing.T) {
bdRecallFail(t, bd, dir, "nonexistent-key-xyz")
})
t.Run("forget_missing_key", func(t *testing.T) {
bdForgetFail(t, bd, dir, "nonexistent-key-xyz")
})
t.Run("remember_no_args", func(t *testing.T) {
bdRememberFail(t, bd, dir)
})
t.Run("recall_no_args", func(t *testing.T) {
bdRecallFail(t, bd, dir)
})
t.Run("forget_no_args", func(t *testing.T) {
bdForgetFail(t, bd, dir)
})
}
// TestEmbeddedMemoryConcurrent exercises memory operations concurrently.
func TestEmbeddedMemoryConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "mx")
// Disable auto-export: this test exercises concurrent memory
// flock contention, not export behavior. With export.auto=true
// (the default since GH#2973), 8 concurrent writers also trigger
// post-write read paths that race with in-flight commits.
//
// The underlying race is not flock-level (flock already serializes
// bd subprocesses) but engine-shutdown-level: Dolt's working-set
// persistence can lag behind flock release, so the next subprocess
// sometimes commits with a stale view and overwrites a prior forget.
// See GH#3260 for the investigation (PR #3269 was the CI probe).
// Fix would require synchronous flush on engine close or a long-lived
// engine per store; both are substantial work. Until then, this
// workaround keeps the test deterministic.
disableAutoExport := exec.Command(bd, "config", "set", "export.auto", "false")
disableAutoExport.Dir = dir
disableAutoExport.Env = bdEnv(dir)
if out, err := disableAutoExport.CombinedOutput(); err != nil {
t.Fatalf("disable export.auto: %v\n%s", err, out)
}
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
// Each worker stores memories with unique keys
for i := 0; i < 3; i++ {
key := fmt.Sprintf("w%d-mem%d", worker, i)
content := fmt.Sprintf("worker %d memory %d content", worker, i)
cmd := exec.Command(bd, "remember", content, "--key", key)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("remember %s: %v\n%s", key, err, out)
results[worker] = r
return
}
}
// Recall each memory
for i := 0; i < 3; i++ {
key := fmt.Sprintf("w%d-mem%d", worker, i)
expected := fmt.Sprintf("worker %d memory %d content", worker, i)
cmd := exec.Command(bd, "recall", key)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("recall %s: %v\n%s", key, err, out)
results[worker] = r
return
}
if !strings.Contains(string(out), expected) {
r.err = fmt.Errorf("recall %s: expected %q, got %q", key, expected, string(out))
results[worker] = r
return
}
}
// Forget one memory
forgetKey := fmt.Sprintf("w%d-mem0", worker)
cmd := exec.Command(bd, "forget", forgetKey)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("forget %s: %v\n%s", forgetKey, err, out)
results[worker] = r
return
}
// List memories
cmd = exec.Command(bd, "memories")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err = cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("memories: %v\n%s", err, out)
results[worker] = r
return
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
// Verify memories only for workers that succeeded (err==nil).
// With exclusive flock, some workers may fail with "one writer at a time".
out := bdMemories(t, bd, dir)
var successCount int
for _, r := range results {
if r.err != nil {
continue
}
successCount++
w := r.worker
forgottenKey := fmt.Sprintf("w%d-mem0", w)
if strings.Contains(out, forgottenKey) {
t.Errorf("expected %s to be forgotten", forgottenKey)
}
for i := 1; i < 3; i++ {
key := fmt.Sprintf("w%d-mem%d", w, i)
if !strings.Contains(out, key) {
t.Errorf("expected %s to still exist in memories", key)
}
}
}
if successCount == 0 {
t.Fatal("expected at least 1 worker to succeed")
}
}
// bdRememberBuffers runs "bd remember" and hands back stdout, stderr and the
// error separately. The budget's lines are STDERR by contract — that is what
// keeps them out of --json output — so a helper that folds the two streams
// together cannot test them.
func bdRememberBuffers(t *testing.T, bd, dir string, args ...string) (string, string, error) {
t.Helper()
cmd := exec.Command(bd, append([]string{"remember"}, args...)...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
return stdout.String(), stderr.String(), err
}
// TestEmbeddedMemoryCorpusBudget exercises memories.budget-chars against a REAL
// store, end to end: the config key routed through `bd config set`, the corpus
// measured from rows that are actually there, the refusal's nonzero exit, and
// the fact that a refused write leaves NOTHING behind.
//
// The arithmetic below is exact and deliberate — every content length is chosen
// so the projected corpus lands on a named boundary — because a budget test
// that only asserts "some line appeared" would pass with an off-by-one budget.
// The pure branch coverage lives in memory_budget_pure_test.go; this is the
// wiring.
func TestEmbeddedMemoryCorpusBudget(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
// ===== Budget OFF: byte-identical to the pre-budget command =====
t.Run("budget_off_is_silent", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "mb")
stdout, stderr, err := bdRememberBuffers(t, bd, dir, strings.Repeat("x", 5000), "--key", "big")
if err != nil {
t.Fatalf("remember with no budget failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
}
if !strings.Contains(stdout, "Remembered [big]") {
t.Errorf("expected the shipped success line, got %q", stdout)
}
if strings.Contains(stderr, "memory corpus") {
t.Errorf("budget off must print no budget line, got stderr %q", stderr)
}
})
// ===== Budget ON =====
dir, _, _ := bdInit(t, bd, "--prefix", "mb")
// The arithmetic below assumes a fresh, empty corpus. Say so out loud, so
// a future `bd init` that seeds a memory fails HERE instead of failing as
// an inscrutable off-by-N in the assertions.
if out := bdMemories(t, bd, dir); !strings.Contains(out, "No memories stored") {
t.Fatalf("budget arithmetic needs an empty corpus after init, got:\n%s", out)
}
cfg := exec.Command(bd, "config", "set", "memories.budget-chars", "100")
cfg.Dir = dir
cfg.Env = bdEnv(dir)
if out, err := cfg.CombinedOutput(); err != nil {
t.Fatalf("bd config set memories.budget-chars failed: %v\n%s", err, out)
} else if strings.Contains(string(out), "not a recognized") || strings.Contains(string(out), "Unrecognized") {
t.Fatalf("memories.budget-chars must be a recognized config key, got:\n%s", out)
}
// 3 ("pad") + 90 = 93 of 100 -> writes, warns at 93%.
t.Run("warn_band_writes_and_warns", func(t *testing.T) {
_, stderr, err := bdRememberBuffers(t, bd, dir, strings.Repeat("a", 90), "--key", "pad")
if err != nil {
t.Fatalf("a write inside the budget must succeed: %v\nstderr:\n%s", err, stderr)
}
if want := "bd remember: memory corpus at 93 of 100 chars (93%)"; !strings.Contains(stderr, want) {
t.Errorf("stderr = %q, want the warn line %q", stderr, want)
}
if out := bdRecall(t, bd, dir, "pad"); !strings.Contains(out, strings.Repeat("a", 90)) {
t.Errorf("the warned write must still have landed, got %q", out)
}
})
// An OVERWRITE of the same size is a no-op on the corpus (delta, not sum).
// Were it summed, the projection would be 186 and this would be refused.
t.Run("overwrite_counts_delta_not_sum", func(t *testing.T) {
_, stderr, err := bdRememberBuffers(t, bd, dir, strings.Repeat("b", 90), "--key", "pad")
if err != nil {
t.Fatalf("a same-size overwrite must not be refused: %v\nstderr:\n%s", err, stderr)
}
if want := "bd remember: memory corpus at 93 of 100 chars (93%)"; !strings.Contains(stderr, want) {
t.Errorf("stderr = %q, want %q (an overwrite must not add to the sum)", stderr, want)
}
if out := bdRecall(t, bd, dir, "pad"); !strings.Contains(out, strings.Repeat("b", 90)) {
t.Errorf("overwrite did not land: %q", out)
}
})
// 93 + 2 ("ab") + 5 = exactly 100. Exactly AT the budget writes.
t.Run("exactly_at_budget_writes", func(t *testing.T) {
_, stderr, err := bdRememberBuffers(t, bd, dir, "ccccc", "--key", "ab")
if err != nil {
t.Fatalf("a write landing exactly on the budget must succeed: %v\nstderr:\n%s", err, stderr)
}
if want := "bd remember: memory corpus at 100 of 100 chars (100%)"; !strings.Contains(stderr, want) {
t.Errorf("stderr = %q, want %q", stderr, want)
}
})
// Overwriting "pad" with one more byte projects 101 — exactly ONE over.
t.Run("one_byte_over_refuses_and_writes_nothing", func(t *testing.T) {
stdout, stderr, err := bdRememberBuffers(t, bd, dir, strings.Repeat("d", 91), "--key", "pad")
if err == nil {
t.Fatalf("a write one byte over the budget must exit nonzero; stdout:\n%s\nstderr:\n%s", stdout, stderr)
}
want := "bd remember: memory corpus would be 101 chars, budget is 100 (101%) — refused; use --force to override"
if !strings.Contains(stderr, want) {
t.Errorf("stderr = %q, want %q", stderr, want)
}
// A terminal user is offered the same ways out as the --json envelope.
if hint := "Hint: raise memories.budget-chars, forget a memory, or pass --force"; !strings.Contains(stderr, hint) {
t.Errorf("stderr = %q, want the hint %q", stderr, hint)
}
if strings.Contains(stdout, "Remembered") || strings.Contains(stdout, "Updated") {
t.Errorf("a refused write must print no success line, got stdout %q", stdout)
}
if out := bdRecall(t, bd, dir, "pad"); !strings.Contains(out, strings.Repeat("b", 90)) {
t.Errorf("a refused write must leave the old content intact, got %q", out)
}
})
// The commonest call passes no --key, so the write lands under the key
// derived from the content, and that is the key the budget must measure:
// 100 + 22 ("derived-keys-count-too") + 24 (the content) projects 146.
t.Run("derived_key_write_is_budgeted", func(t *testing.T) {
stdout, stderr, err := bdRememberBuffers(t, bd, dir, "Derived keys count, too!")
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) || exitErr.ExitCode() != 1 {
t.Fatalf("a write with no --key over the budget must exit 1, got %v; stdout:\n%s\nstderr:\n%s", err, stdout, stderr)
}
want := "bd remember: memory corpus would be 146 chars, budget is 100 (146%) — refused; use --force to override"
if !strings.Contains(stderr, want) {
t.Errorf("stderr = %q, want %q", stderr, want)
}
if strings.Contains(stdout, "Remembered") || strings.Contains(stdout, "Updated") {
t.Errorf("a refused write must print no success line, got stdout %q", stdout)
}
bdRecallFail(t, bd, dir, "derived-keys-count-too")
})
// A whitespace-only --key names nothing, so the role refuses the write with
// its own validation sentence. The budget must not answer first: on this
// full corpus it would call the write over budget and point at --force,
// and the --force retry would only then meet the validation error.
t.Run("blank_key_gets_the_validation_error_not_the_budget", func(t *testing.T) {
stdout, stderr, err := bdRememberBuffers(t, bd, dir, "some content", "--key", " ")
if err == nil {
t.Fatalf("a whitespace-only --key must be refused; stdout:\n%s\nstderr:\n%s", stdout, stderr)
}
if !strings.Contains(stderr, "memory key must not be empty") {
t.Errorf("stderr = %q, want the role's validation error", stderr)
}
if strings.Contains(stderr, "memory corpus") {
t.Errorf("a write the role refuses must not be measured against the budget, got stderr %q", stderr)
}
})
// The same refusal under --json is said ONCE, and only on stdout: the
// machine-readable envelope, nothing else. Printing the prose to stderr as
// well would deliver one refusal twice to a caller reading both streams.
t.Run("json_refusal_is_said_once", func(t *testing.T) {
stdout, stderr, err := bdRememberBuffers(t, bd, dir, strings.Repeat("d", 91), "--key", "pad", "--json")
if err == nil {
t.Fatalf("a --json write one byte over the budget must exit nonzero; stdout:\n%s\nstderr:\n%s", stdout, stderr)
}
// Exactly ONE JSON document on stdout — no second envelope, no prose
// before or after it.
dec := json.NewDecoder(strings.NewReader(stdout))
var doc map[string]interface{}
if decErr := dec.Decode(&doc); decErr != nil {
t.Fatalf("stdout is not a JSON document (%v):\n%s", decErr, stdout)
}
if decErr := dec.Decode(new(json.RawMessage)); !errors.Is(decErr, io.EOF) {
t.Errorf("stdout carries more than one JSON document (second Decode = %v):\n%s", decErr, stdout)
}
// The envelope carries the refusal sentence, under either the enveloped
// or the flat error shape.
inner := doc
if data, ok := doc["data"].(map[string]interface{}); ok {
inner = data
}
msg, _ := inner["error"].(string)
want := "bd remember: memory corpus would be 101 chars, budget is 100 (101%) — refused; use --force to override"
if msg != want {
t.Errorf("JSON error = %q, want %q (full stdout:\n%s)", msg, want, stdout)
}
// ...and stderr says nothing, so the refusal arrived exactly once.
if strings.Contains(stderr, "memory corpus") || strings.Contains(stderr, "Hint:") {
t.Errorf("--json refusal must not also print the prose line to stderr, got stderr %q", stderr)
}
// A refused --json write still leaves the old content intact.
if out := bdRecall(t, bd, dir, "pad"); !strings.Contains(out, strings.Repeat("b", 90)) {
t.Errorf("a refused --json write must leave the old content intact, got %q", out)
}
})
// The same write with --force lands, and still says so.
t.Run("force_writes_over_budget", func(t *testing.T) {
_, stderr, err := bdRememberBuffers(t, bd, dir, strings.Repeat("d", 91), "--key", "pad", "--force")
if err != nil {
t.Fatalf("--force must write over the budget: %v\nstderr:\n%s", err, stderr)
}
if want := "bd remember: memory corpus at 101 of 100 chars (101%) — over budget, written anyway (--force)"; !strings.Contains(stderr, want) {
t.Errorf("stderr = %q, want %q", stderr, want)
}
if out := bdRecall(t, bd, dir, "pad"); !strings.Contains(out, strings.Repeat("d", 91)) {
t.Errorf("--force did not land the write, got %q", out)
}
})
}