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
This commit is contained in:
Bee 2026-09-12 16:59:43 -07:00
commit ca9f14d65d
7 changed files with 598 additions and 1 deletions

View file

@ -1026,6 +1026,10 @@ var recognizedConfigKeys = map[string]bool{
"auto_compact_enabled": true, "schema_version": true,
"output.title-length": true,
"prime.max-memories": true, "prime.max-memory-chars": true,
// Opt-in ceiling on the persistent-memory corpus, enforced by `bd
// remember` (0 = off). Yaml-only for the same reason the prime caps are:
// it is read through viper, so a DB-backed write would never be seen.
"memories.budget-chars": true,
// The events-journal family. All four are startup settings that land in
// config.yaml (config.YamlOnlyKeys), and every one of them is documented as
// a `bd config set` invocation — including the auto-prune opt-out, where an

View file

@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/memoryapi"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/storage/kvkeys"
@ -106,6 +107,100 @@ func matchesKnownCommand(cmd *cobra.Command, insight string) (string, bool) {
return "", false
}
// memoryForceFlag is `bd remember --force`: write even when the corpus budget
// would be crossed. It only has meaning when memories.budget-chars is set.
var memoryForceFlag bool
// memoryConfigInt reads an integer config key (stubbable for tests), the way
// primeConfigInt does for the prime-side injection caps.
var memoryConfigInt = func(key string) int {
return config.GetInt(key)
}
// memoryCorpusBudget resolves the memories.budget-chars ceiling. 0 — the
// default — means OFF, and so does a negative value: the budget is opt-in, and
// a nonsense setting must not turn into a refusal the operator never asked for.
func memoryCorpusBudget() int {
budget := memoryConfigInt("memories.budget-chars")
if budget < 0 {
return 0
}
return budget
}
// memoryCorpusChars sums len(key)+len(content) over a corpus.
//
// The unit is BYTES (Go len), deliberately the same unit prime's
// --max-memory-chars cap counts in: the budget exists to bound what prime
// injects, so measuring it in a second unit would let a corpus sit inside the
// budget and still blow the injection cap.
func memoryCorpusChars(memories map[string]string) int {
total := 0
for k, v := range memories {
total += len(k) + len(v)
}
return total
}
// projectedMemoryCorpusChars is that sum AFTER a write of content under key
// would apply. An OVERWRITE replaces the key's old content in the sum instead
// of adding to it — the delta, not the sum — which is what makes editing an
// existing memory possible at all once a corpus is near its ceiling.
func projectedMemoryCorpusChars(existing map[string]string, key, content string) int {
total := memoryCorpusChars(existing)
if old, ok := existing[key]; ok {
total -= len(key) + len(old)
}
return total + len(key) + len(content)
}
// memoryBudgetLine renders the ONE stderr line the budget check emits, and
// says whether the write must be refused.
//
// Three outcomes, in order of severity:
// - over budget, no --force: refuse, naming the override.
// - over budget with --force: write, but SAY SO — a forced crossing that
// printed nothing would make the budget invisible exactly when it matters.
// - at or above 80% of budget and within it: write, warn. That is the whole
// point of a budget over a tripwire: the warning arrives while there is
// still room to act on it.
//
// Below 80%, and whenever the budget is off, it returns "" and the command is
// silent — byte-identical to the pre-budget behavior.
//
// The percentage is floor(projected*100/budget) over int64, so a large budget
// cannot overflow the comparison.
func memoryBudgetLine(projected, budget int, force bool) (line string, refuse bool) {
if budget <= 0 {
return "", false
}
pct := int(int64(projected) * 100 / int64(budget))
switch {
case projected > budget && !force:
return fmt.Sprintf("bd remember: memory corpus would be %d chars, budget is %d (%d%%) — refused; use --force to override", projected, budget, pct), true
case projected > budget:
return fmt.Sprintf("bd remember: memory corpus at %d of %d chars (%d%%) — over budget, written anyway (--force)", projected, budget, pct), false
case int64(projected)*5 >= int64(budget)*4:
return fmt.Sprintf("bd remember: memory corpus at %d of %d chars (%d%%)", projected, budget, pct), false
}
return "", false
}
// rememberBudgetVerdict is the whole budget decision as ONE pure function:
// given the corpus as it stands, the write about to be made, the budget and
// --force, it answers with the line to print (empty = say nothing) and whether
// the write must be refused. The command around it only has to fetch the
// corpus and obey.
//
// Keeping it pure is what lets every branch — off, boundary, overwrite delta,
// force, warn — be pinned by a test that needs neither a database nor cgo.
func rememberBudgetVerdict(existing map[string]string, key, content string, budget int, force bool) (line string, refuse bool) {
if budget <= 0 {
return "", false
}
return memoryBudgetLine(projectedMemoryCorpusChars(existing, key, content), budget, force)
}
// rememberBareKeyPath implements the desire-path / footgun guard for
// `bd remember <bare-slug>` (no --key): a bare slug naming an EXISTING memory
// is recalled instead of stored; a bare slug naming nothing is refused. The
@ -247,6 +342,12 @@ unless --key is given). As a convenience, if the arg is a bare key naming an
existing memory, it is RECALLED instead of stored (same as 'bd recall');
a bare key naming nothing is refused. Use --key to store slug-like content.
Corpus budget (opt-in, off by default): set the memories.budget-chars config
key to a byte ceiling for the whole memory corpus (sum of len(key)+len(content),
the unit bd prime's --max-memory-chars counts in). A write that would cross it
is refused, and --force overrides; a write that lands at or above 80% of the
budget warns. With the key unset nothing changes.
Examples:
bd remember "always run tests with -race flag"
bd remember "Dolt phantom DBs hide in three places" --key dolt-phantoms
@ -324,6 +425,50 @@ Examples:
return rememberBareKeyPath(derived, insight, recalled.Value)
}
// Corpus budget (memories.budget-chars, OPT-IN, 0 = off). The
// wheelhouse-side tripwire only REPORTS the corpus size, so the corpus
// re-trips red every few days and nothing ever stops the write that
// crossed the line. This is the stop.
//
// It sits here, between the read-shaped branches above and the write
// below, because it is the last thing that can decide NOT to write —
// and above the role because a budget is a front-door policy, not a
// property of the memory plane (an HTTP POST must not inherit it).
//
// When the budget is off the whole block is skipped: no extra List, no
// output, nothing. Behavior is then byte-identical to the pre-budget
// command, which is what TestRememberBudgetVerdictOffIsSilent and the embedded
// budget_off_is_silent case pin.
budgetKey := memoryKeyFlag
if budgetKey == "" {
budgetKey = derived
}
var budgetLine string
if budget := memoryCorpusBudget(); budget > 0 && budgetKey != "" && strings.TrimSpace(insight) != "" {
corpus, err := memories.List(rootCtx, memoryops.ListRequest{})
if err != nil {
return HandleErrorRespectJSON("reading the memory corpus for the budget check: %v", err)
}
line, refuse := rememberBudgetVerdict(corpus.Memories, budgetKey, insight, budget, memoryForceFlag)
if refuse {
// The budget line IS the refusal sentence, so it prints as
// itself: routing it through HandleError would prefix it with
// "Error: " and reword text agents will match on. It goes to
// STDERR on every route, including --json, so structured
// output can never be corrupted by it; --json additionally
// gets the normal machine-readable error object on stdout.
fmt.Fprintln(os.Stderr, line)
if jsonOutput {
jsonStdoutError(line, "raise memories.budget-chars, forget a memory, or pass --force")
}
return SilentExit()
}
// Not a refusal: the write happens first and the line reports the
// corpus that now exists, so no line ever describes a write that
// did not land.
budgetLine = line
}
result, err := memories.Remember(rootCtx, memoryops.RememberRequest{Key: memoryKeyFlag, Content: insight})
if err != nil {
// The role's two refusals ARE this command's shipped sentences —
@ -338,6 +483,10 @@ Examples:
}
noteDirectMemoryWrite()
if budgetLine != "" {
fmt.Fprintln(os.Stderr, budgetLine)
}
// Remembered versus Updated is Replaced, observed in the SAME
// transaction as the write. The shipped code read the row first and
// described a moment that had already passed.
@ -485,6 +634,7 @@ func truncateMemory(s string, maxLen int) string {
func init() {
rememberCmd.Flags().StringVar(&memoryKeyFlag, "key", "", "Explicit key for the memory (auto-generated from content if not set). If a memory with this key already exists, it will be updated in place")
rememberCmd.Flags().BoolVar(&memoryForceFlag, "force", false, "Store even when the write would cross the memories.budget-chars corpus budget (no effect when the budget is unset)")
rootCmd.AddCommand(rememberCmd)
rootCmd.AddCommand(memoriesCmd)

View file

@ -0,0 +1,244 @@
// Pure-Go tests for the `bd remember` corpus budget (memories.budget-chars).
// No cgo / Dolt dependency, so this file carries no build tag and compiles
// under CGO_ENABLED=0 with gms_pure_go, exactly like memory_pure_test.go.
//
// The whole budget decision lives in one pure function (rememberBudgetVerdict),
// so every branch the knob has — off, boundary, overwrite delta, --force, the
// 80% warn — is pinned here without a database. The end-to-end plumbing (the
// stderr routing and the nonzero exit) is pinned against a real store by
// TestEmbeddedMemoryCorpusBudget in memory_embedded_test.go.
package main
import (
"fmt"
"strings"
"testing"
)
// budgetCorpus builds a corpus whose measured size is exactly want bytes,
// under a single key. len(key)+len(content) is the unit the budget counts in,
// so the content is padded to want-len(key).
func budgetCorpus(t *testing.T, key string, want int) map[string]string {
t.Helper()
if want < len(key) {
t.Fatalf("corpus of %d bytes cannot hold key %q (%d bytes)", want, key, len(key))
}
corpus := map[string]string{key: strings.Repeat("x", want-len(key))}
if got := memoryCorpusChars(corpus); got != want {
t.Fatalf("corpus fixture measured %d bytes, want %d", got, want)
}
return corpus
}
// TestMemoryCorpusCharsCountsKeyAndContentBytes pins the UNIT: bytes of
// len(key)+len(content), the same unit prime's --max-memory-chars counts in.
// A rune-counting implementation would disagree on any non-ASCII memory, which
// is why the multibyte case is here.
func TestMemoryCorpusCharsCountsKeyAndContentBytes(t *testing.T) {
if got := memoryCorpusChars(nil); got != 0 {
t.Fatalf("empty corpus = %d, want 0", got)
}
corpus := map[string]string{"ab": "cde", "f": "gh"}
if got, want := memoryCorpusChars(corpus), 2+3+1+2; got != want {
t.Fatalf("corpus = %d, want %d", got, want)
}
// "é" is two bytes; the budget must see two.
if got, want := memoryCorpusChars(map[string]string{"k": "é"}), 3; got != want {
t.Fatalf("multibyte corpus = %d, want %d (bytes, not runes)", got, want)
}
}
// TestProjectedMemoryCorpusCharsOverwriteCountsDelta is spec point (c): an
// overwrite REPLACES the key's old content in the sum instead of adding to it.
// Without this, editing one memory down to nothing would still read as growth
// and a corpus at its ceiling could never be repaired.
func TestProjectedMemoryCorpusCharsOverwriteCountsDelta(t *testing.T) {
existing := map[string]string{"k": "aaaa", "other": "bb"} // 1+4 + 5+2 = 12
// New key: pure addition.
if got, want := projectedMemoryCorpusChars(existing, "new", "xyz"), 12+3+3; got != want {
t.Errorf("new key projection = %d, want %d", got, want)
}
// Overwrite with longer content: only the delta lands.
if got, want := projectedMemoryCorpusChars(existing, "k", "aaaaaa"), 12+2; got != want {
t.Errorf("growing overwrite projection = %d, want %d", got, want)
}
// Overwrite with shorter content: the corpus SHRINKS.
if got, want := projectedMemoryCorpusChars(existing, "k", "a"), 12-3; got != want {
t.Errorf("shrinking overwrite projection = %d, want %d", got, want)
}
// Identical rewrite: no change at all.
if got, want := projectedMemoryCorpusChars(existing, "k", "aaaa"), 12; got != want {
t.Errorf("identical overwrite projection = %d, want %d", got, want)
}
// A key whose stored value is empty still has its key counted once, not twice.
empty := map[string]string{"k": ""}
if got, want := projectedMemoryCorpusChars(empty, "k", "zz"), 3; got != want {
t.Errorf("empty-valued overwrite projection = %d, want %d", got, want)
}
}
// TestRememberBudgetVerdictOffIsSilent is spec point (a): with the budget unset
// (or 0, or a nonsense negative) nothing is ever refused and nothing is ever
// printed, no matter how large the corpus is.
func TestRememberBudgetVerdictOffIsSilent(t *testing.T) {
huge := budgetCorpus(t, "k", 1_000_000)
for _, budget := range []int{0, -1, -1000} {
for _, force := range []bool{false, true} {
line, refuse := rememberBudgetVerdict(huge, "new", strings.Repeat("y", 5000), budget, force)
if refuse || line != "" {
t.Fatalf("budget=%d force=%v: budget-off must be silent, got refuse=%v line=%q", budget, force, refuse, line)
}
}
}
}
// TestMemoryCorpusBudgetReadsConfig pins the config read, including the clamp
// that keeps a negative value meaning OFF rather than "refuse everything".
func TestMemoryCorpusBudgetReadsConfig(t *testing.T) {
orig := memoryConfigInt
t.Cleanup(func() { memoryConfigInt = orig })
values := map[string]int{}
memoryConfigInt = func(key string) int { return values[key] }
if got := memoryCorpusBudget(); got != 0 {
t.Fatalf("unset budget = %d, want 0 (off)", got)
}
values["memories.budget-chars"] = 4096
if got := memoryCorpusBudget(); got != 4096 {
t.Fatalf("configured budget = %d, want 4096", got)
}
values["memories.budget-chars"] = -5
if got := memoryCorpusBudget(); got != 0 {
t.Fatalf("negative budget = %d, want 0 (off)", got)
}
}
// TestRememberBudgetVerdictBoundary is spec point (b): a projection exactly AT
// the budget writes; exactly one byte over refuses. The two cases differ by a
// single byte of content, so nothing but the comparison can explain a failure.
func TestRememberBudgetVerdictBoundary(t *testing.T) {
const budget = 200
// Corpus at 100 bytes; the new memory's key costs 3.
existing := budgetCorpus(t, "old", 100)
// Exactly at the budget: 100 + 3 + 97 = 200. Writes (and warns: 100%).
atBudget, refuse := rememberBudgetVerdict(existing, "new", strings.Repeat("z", 97), budget, false)
if refuse {
t.Fatalf("a projection exactly at the budget must write, got refusal %q", atBudget)
}
if !strings.Contains(atBudget, "at 200 of 200 chars (100%)") {
t.Errorf("at-budget line = %q, want the 100%% warn line", atBudget)
}
// One byte over: 201. Refused.
over, refuse := rememberBudgetVerdict(existing, "new", strings.Repeat("z", 98), budget, false)
if !refuse {
t.Fatalf("a projection one byte over the budget must refuse, got line %q", over)
}
want := "bd remember: memory corpus would be 201 chars, budget is 200 (100%) — refused; use --force to override"
if over != want {
t.Errorf("refusal line =\n %q\nwant\n %q", over, want)
}
}
// TestRememberBudgetVerdictForceWrites is spec point (d): --force turns the
// refusal into a write, and STILL prints a line — a forced crossing that said
// nothing would make the budget invisible exactly when it matters most.
func TestRememberBudgetVerdictForceWrites(t *testing.T) {
existing := budgetCorpus(t, "old", 100)
line, refuse := rememberBudgetVerdict(existing, "new", strings.Repeat("z", 98), 200, true)
if refuse {
t.Fatalf("--force must not refuse, got refusal %q", line)
}
if line == "" {
t.Fatal("--force over budget must still print a line")
}
for _, want := range []string{"201", "200", "(100%)", "--force"} {
if !strings.Contains(line, want) {
t.Errorf("forced line %q is missing %q", line, want)
}
}
if strings.Contains(line, "refused") {
t.Errorf("forced line must not claim a refusal: %q", line)
}
}
// TestRememberBudgetVerdictWarnBand is spec point (e) plus its two edges: below
// 80% of the budget the command is silent (today's behavior), and from 80% up
// to the budget it writes AND warns.
func TestRememberBudgetVerdictWarnBand(t *testing.T) {
const budget = 1000
cases := []struct {
name string
projected int
wantLine bool
wantPct int
}{
{"well below", 100, false, 0},
{"one byte under the warn band", 799, false, 0},
{"exactly 80 percent", 800, true, 80},
{"inside the band", 950, true, 95},
{"exactly at budget", 1000, true, 100},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Project by writing a fresh key of known cost into an empty corpus.
key := "k"
content := strings.Repeat("q", tc.projected-len(key))
line, refuse := rememberBudgetVerdict(map[string]string{}, key, content, budget, false)
if refuse {
t.Fatalf("projection %d of %d must not refuse", tc.projected, budget)
}
if !tc.wantLine {
if line != "" {
t.Fatalf("projection %d of %d must be silent, got %q", tc.projected, budget, line)
}
return
}
want := fmt.Sprintf("bd remember: memory corpus at %d of %d chars (%d%%)", tc.projected, budget, tc.wantPct)
if line != want {
t.Errorf("warn line =\n %q\nwant\n %q", line, want)
}
})
}
}
// TestMemoryBudgetLinePercentIsUnclamped pins the arithmetic: the percentage is
// floor(projected*100/budget) and is NOT clamped at 100, so a corpus forced
// well past its ceiling reports how far past it actually is. A clamp here would
// make 101% and 400% read identically — exactly the reading an operator needs.
func TestMemoryBudgetLinePercentIsUnclamped(t *testing.T) {
cases := []struct {
projected, budget int
want string
}{
{101, 100, "(101%)"},
{201, 200, "(100%)"}, // floor(100.5)
{400, 100, "(400%)"},
}
for _, tc := range cases {
line, _ := memoryBudgetLine(tc.projected, tc.budget, false)
if !strings.Contains(line, tc.want) {
t.Errorf("memoryBudgetLine(%d, %d) = %q, want it to contain %q", tc.projected, tc.budget, line, tc.want)
}
}
}
// TestMemoryBudgetLineHasNoTrailingNewline guards the "ONE line" contract: the
// callers add the newline, so a line that carried its own would print a blank
// line into stderr between the warning and whatever follows.
func TestMemoryBudgetLineHasNoTrailingNewline(t *testing.T) {
for _, force := range []bool{false, true} {
line, _ := memoryBudgetLine(500, 100, force)
if line == "" {
t.Fatalf("force=%v: expected an over-budget line", force)
}
if strings.ContainsAny(line, "\n\r") {
t.Errorf("force=%v: budget line must be a single line, got %q", force, line)
}
}
}

View file

@ -486,3 +486,141 @@ func TestEmbeddedMemoryConcurrent(t *testing.T) {
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)
}
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 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)
}
})
}

View file

@ -88,7 +88,7 @@ The full namespaces routed to YAML are:
Plus these individual keys:
`no-db`, `json`, `db`, `actor`, `identity`, `no-push`, `no-git-ops`, `agent.profile`, `create.require-description`, `import.auto`, `import.path`, `prime.max-memories`, `prime.max-memory-chars`, and the secret keys `github.token`, `gitlab.token`, `jira.api_token`, `ado.pat`, `linear.api_key`, `linear.oauth_client_id`, `linear.oauth_client_secret`.
`no-db`, `json`, `db`, `actor`, `identity`, `no-push`, `no-git-ops`, `agent.profile`, `create.require-description`, `import.auto`, `import.path`, `prime.max-memories`, `prime.max-memory-chars`, `memories.budget-chars`, and the secret keys `github.token`, `gitlab.token`, `jira.api_token`, `ado.pat`, `linear.api_key`, `linear.oauth_client_id`, `linear.oauth_client_secret`.
Any key whose name contains `api_key`, `api-key`, `secret`, `token`, or `password` is treated as a secret: it is refused on git-tracked `config.yaml` files unless you pass `--force-git-tracked`. Prefer exporting the value as an environment variable instead (e.g. `LINEAR_API_KEY`).
@ -106,6 +106,7 @@ Any key whose name contains `api_key`, `api-key`, `secret`, `token`, or `passwor
| `agent.profile` | — | `BD_AGENT_PROFILE` | `conservative` | Policy profile `bd prime` uses for git/commit authority: `conservative`, `minimal`, `team-maintainer`; invalid values fall back to `conservative` |
| `prime.max-memories` | `--max-memories` | `BD_PRIME_MAX_MEMORIES` | `0` | Max persistent memories injected by `bd prime` (0 = unlimited) |
| `prime.max-memory-chars` | `--max-memory-chars` | `BD_PRIME_MAX_MEMORY_CHARS` | `0` | Max total bytes of memory entries injected by `bd prime`, at whole-memory boundaries (0 = unlimited) |
| `memories.budget-chars` | — | `BD_MEMORIES_BUDGET_CHARS` | `0` | Ceiling on the whole persistent-memory corpus, in bytes of `len(key)+len(content)` summed over every memory (0 = off). `bd remember` refuses a write that would cross it — pass `--force` to store anyway — and warns on a write that lands at or above 80% of it. Read at `bd remember` time only; `bd prime` injection is capped separately by `prime.max-memory-chars` |
| `dolt.auto-commit` | `--dolt-auto-commit` | `BD_DOLT_AUTO_COMMIT` | `on` | Create a Dolt history commit after each successful write (see [below](#auto-commit-sql-commits-vs-dolt-commits)) |
| `dolt.auto-push` | — | `BD_DOLT_AUTO_PUSH` | `false` | Auto-push to Dolt remote after writes (opt-in; see [below](#auto-push)) |
| `dolt.auto-push-interval` | — | `BD_DOLT_AUTO_PUSH_INTERVAL` | `5m` | Minimum time between auto-pushes |

View file

@ -68,6 +68,11 @@ var YamlOnlyKeys = map[string]bool{
"prime.max-memories": true,
"prime.max-memory-chars": true,
// Memory-corpus budget enforced by `bd remember` (0 = off). Yaml for the
// same reason the prime caps are: it is read through viper (config.GetInt),
// so a DB-backed write would be set successfully and then never read.
"memories.budget-chars": true,
// Validation settings (bd-t7jq)
// Values: "warn" | "error" | "none"
"validation.on-create": true,
@ -916,6 +921,14 @@ func validateYamlConfigValue(key, value string) error {
if n < 0 {
return fmt.Errorf("prime.max-memory-chars must be a non-negative integer (0 = unlimited), got %q", value)
}
case "memories.budget-chars":
n, err := strconv.Atoi(value)
if err != nil {
return fmt.Errorf("memories.budget-chars must be a non-negative integer (0 = off), got %q", value)
}
if n < 0 {
return fmt.Errorf("memories.budget-chars must be a non-negative integer (0 = off), got %q", value)
}
}
return nil
}

View file

@ -770,6 +770,53 @@ func TestValidateYamlConfigValue_PrimeMaxMemoryChars(t *testing.T) {
}
}
// TestValidateYamlConfigValue_MemoriesBudgetChars tests validation of
// memories.budget-chars, the opt-in `bd remember` corpus ceiling. A negative
// value is rejected at SET time rather than silently clamped at read time, so
// an operator who types "-1" hears about it instead of getting a budget that
// is quietly off.
func TestValidateYamlConfigValue_MemoriesBudgetChars(t *testing.T) {
tests := []struct {
name string
value string
expectErr bool
errMsg string
}{
{"valid zero (off)", "0", false, ""},
{"valid positive", "250000", false, ""},
{"invalid negative", "-1", true, "memories.budget-chars must be a non-negative integer (0 = off), got \"-1\""},
{"invalid non-integer", "abc", true, "memories.budget-chars must be a non-negative integer (0 = off), got \"abc\""},
{"invalid float", "3.5", true, "memories.budget-chars must be a non-negative integer (0 = off), got \"3.5\""},
{"invalid empty", "", true, "memories.budget-chars must be a non-negative integer (0 = off), got \"\""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateYamlConfigValue("memories.budget-chars", tt.value)
if tt.expectErr {
if err == nil {
t.Errorf("expected error for value %q, got nil", tt.value)
} else if err.Error() != tt.errMsg {
t.Errorf("expected error %q, got %q", tt.errMsg, err.Error())
}
} else {
if err != nil {
t.Errorf("unexpected error for value %q: %v", tt.value, err)
}
}
})
}
}
// TestMemoriesBudgetCharsIsYamlOnly pins the routing: the key is read through
// viper (config.GetInt), so a DB-backed write would be accepted and then never
// read by `bd remember`. YamlOnlyKeys is what keeps `bd config set` honest.
func TestMemoriesBudgetCharsIsYamlOnly(t *testing.T) {
if !IsYamlOnlyKey("memories.budget-chars") {
t.Error("memories.budget-chars must be yaml-only: bd remember reads it through viper")
}
}
func TestIsSecretKey(t *testing.T) {
tests := []struct {
key string