beads/cmd/bd/memory.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

675 lines
26 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"sort"
"strings"
"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"
"github.com/steveyegge/beads/internal/storage/uow"
"github.com/steveyegge/beads/memoryops"
)
// openMemories hands back the persistent-memory role for whichever route this
// invocation is on, each through its OWN capability accessor — the store's for
// the direct route and the provider's for the proxied one.
//
// directRequirement is the message `ensureDirectMode` reports when a workspace
// is reachable by neither route. It is per-verb because the shipped text names
// the verb.
func openMemories(directRequirement string) (memoryops.Memories, error) {
if usesProxiedServer() {
return proxiedMemories()
}
if err := ensureDirectMode(directRequirement); err != nil {
return nil, err
}
return store.Memories()
}
// proxiedMemories hands back the guarded persistent-memory surface for this
// invocation's proxied-server provider, through the provider's OWN capability
// accessor — the same two-step proxiedWorkspaceConfig performs.
func proxiedMemories() (memoryops.Memories, error) {
if uowProvider == nil {
return nil, errors.New("proxied-server UOW provider not initialized")
}
return memoriesFromProvider(uowProvider)
}
// memoriesFromProvider is that accessor step for a provider the caller names.
//
// It takes the provider rather than reading the global one because `bd prime`
// opens a provider SCOPED to its read — prime is in noDbCommands, so the root
// pre-run opens nothing — and one spelling of "ask this provider for the memory
// surface" is the whole point of having an accessor at all.
func memoriesFromProvider(provider uow.UnitOfWorkProvider) (memoryops.Memories, error) {
src, ok := provider.(uow.MemoriesSource)
if !ok {
return nil, fmt.Errorf("proxied-server provider %T does not offer the persistent-memory surface", provider)
}
return src.Memories()
}
// noteDirectMemoryWrite marks the invocation as having written, which is what
// the auto-commit epilogue in main.go keys on.
//
// It is DIRECT-ROUTE ONLY, and both halves of that matter. A direct memory
// write lands in the Dolt working set and nothing else commits it, so a verb
// that forgets to call this stores a memory that exists until the process exits
// and then sits uncommitted — visible to the session that wrote it and to
// nothing after. A proxied write already committed inside the role's unit of
// work, so flagging it there would ask the epilogue to commit a second time on
// a route with nothing outstanding.
//
// The RunEs cannot make that distinction themselves: openMemories hides which
// route they are on, which is the point. So the guard lives here, once, the way
// noteDirectConfigWrite does for the settings plane.
func noteDirectMemoryWrite() {
if !usesProxiedServer() {
commandDidWrite.Store(true)
}
}
// memoryPrefix is prepended (after kvPrefix) to all memory keys.
const memoryPrefix = kvkeys.MemoryPrefix
// memoryKeyFlag allows explicit key override for bd remember.
var memoryKeyFlag string
// matchesKnownCommand reports whether insight is a single bare word that
// matches the name or an alias of a top-level bd command. It is used to catch
// `bd remember <subcommand>` mistakes before they become accidental memories.
// Multi-word insights (the normal case) always pass, since they contain
// whitespace and so cannot be a single command token.
func matchesKnownCommand(cmd *cobra.Command, insight string) (string, bool) {
word := strings.TrimSpace(insight)
if word == "" || strings.ContainsAny(word, " \t\r\n") {
return "", false
}
for _, c := range cmd.Root().Commands() {
if strings.EqualFold(c.Name(), word) {
return c.Name(), true
}
for _, alias := range c.Aliases {
if strings.EqualFold(alias, word) {
return c.Name(), true
}
}
}
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, so the two numbers are comparable. They are
// not the same MEASURE: prime counts each entry as it renders it — the key and
// value plus a few bytes of heading or bullet markup per memory, and the
// compact form truncates long values — so a corpus sitting exactly at a budget
// equal to prime.max-memory-chars can still have memories elided at injection.
// A budget meant to keep prime from eliding anything has to sit below that 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 FLOORS within the budget and CEILINGS over it, and the
// rounding is not cosmetic: floor(201*100/200) is 100, and "100%" is exactly
// the at-budget reading this code deliberately ALLOWS — so a floored refusal
// would report the number of the case it is not. Ceiling above the line makes
// every over-budget line read 101% or more, and the split is on the VALUE, not
// on --force, so the same projection never reports two different percentages
// depending on a flag. Both forms are int64 arithmetic, 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))
if projected > budget {
pct = int((int64(projected)*100 + int64(budget) - 1) / 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
// caller only invokes it when memoryKeyFlag == "" and the insight round-trips
// through memoryapi.DeriveKey unchanged, having already read the key.
func rememberBareKeyPath(key, insight, existing string) error {
if existing != "" {
if jsonOutput {
return outputJSON(map[string]interface{}{
"key": key,
"value": existing,
"found": true,
"action": "recalled",
})
}
fmt.Fprintf(os.Stderr,
"(recalled %q -- a bare existing key READS. To overwrite: `bd remember \"<new content>\" --key %s`)\n",
key, key)
fmt.Printf("%s\n", existing)
return nil
}
return HandleErrorRespectJSON(
"no memory named %q to recall -- and refusing to store a bare key-like token as its own content. "+
"`bd remember` WRITES (its positional arg is CONTENT, not a key). "+
"To store it anyway: `bd remember %q --key %s`. To browse keys: `bd memories`",
key, insight, key)
}
// printRememberResult renders the `bd remember` success output.
func printRememberResult(verb, key, insight string) error {
if jsonOutput {
return outputJSON(map[string]string{
"key": key,
"value": insight,
"action": strings.ToLower(verb),
})
}
fmt.Printf("%s [%s]: %s\n", verb, key, truncateMemory(insight, 80))
return nil
}
// printMemoriesResult renders the `bd memories` output.
func printMemoriesResult(memories map[string]string, search string) error {
if jsonOutput {
return outputJSON(memories)
}
if len(memories) == 0 {
if search != "" {
fmt.Printf("No memories matching %q\n", search)
} else {
fmt.Println("No memories stored. Use 'bd remember \"insight\"' to add one.")
}
return nil
}
keys := make([]string, 0, len(memories))
for k := range memories {
keys = append(keys, k)
}
sort.Strings(keys)
if search != "" {
fmt.Printf("Memories matching %q:\n\n", search)
} else {
fmt.Printf("Memories (%d):\n\n", len(memories))
}
for _, k := range keys {
v := memories[k]
fmt.Printf(" %s\n", k)
fmt.Printf(" %s\n\n", truncateMemory(v, 120))
}
return nil
}
// printForgetNotFound renders the `bd forget` missing-key output (including
// the SilentExit contract).
func printForgetNotFound(key string) error {
if jsonOutput {
if jerr := outputJSON(map[string]string{
"key": key,
"found": "false",
}); jerr != nil {
return jerr
}
return SilentExit()
}
fmt.Fprintf(os.Stderr, "No memory with key %q\n", key)
return SilentExit()
}
// printForgetResult renders the `bd forget` success output.
func printForgetResult(key, existing string) error {
if jsonOutput {
return outputJSON(map[string]string{
"key": key,
"deleted": "true",
})
}
fmt.Printf("Forgot [%s]: %s\n", key, truncateMemory(existing, 80))
return nil
}
// printRecallResult renders the `bd recall` output (including the not-found
// SilentExit contract).
func printRecallResult(key, value string) error {
if jsonOutput {
if jerr := outputJSON(map[string]interface{}{
"key": key,
"value": value,
"found": value != "",
}); jerr != nil {
return jerr
}
if value == "" {
return SilentExit()
}
return nil
}
if value == "" {
fmt.Fprintf(os.Stderr, "No memory with key %q\n", key)
return SilentExit()
}
fmt.Printf("%s\n", value)
return nil
}
// rememberCmd stores a memory.
var rememberCmd = &cobra.Command{
Use: `remember "<insight>"`,
Short: "Store a persistent memory",
Long: `Store a memory that persists across sessions and account rotations.
Memories are injected at prime time (bd prime) so you have them
in every session without manual loading.
The positional arg is the memory CONTENT (the key is auto-generated from it
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)).
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.
bd prime's --max-memory-chars also counts bytes, but of each memory as
injected, formatting included, so a budget equal to that cap does not
guarantee prime injects every memory; set the budget below it for that.
Examples:
bd remember "always run tests with -race flag"
bd remember "Dolt phantom DBs hide in three places" --key dolt-phantoms
bd remember "auth module uses JWT not sessions" --key auth-jwt
bd remember dolt-phantoms # bare existing key: reads it (= bd recall)`,
GroupID: "setup",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
CheckReadonly("remember") // also covers the migration freeze check (dc-6jaq)
evt := metrics.NewCommandEvent("remember")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
insight := args[0]
// Guard against a subcommand-like first argument being silently stored
// as memory content. `bd remember` is a leaf command, so a mistaken
// `bd remember recall` (or any bare bd command name) would otherwise
// store the word "recall" as a memory instead of doing what the user
// intended (GH#4401). A genuine insight is a phrase, so only a single
// bare word that matches a known command is treated as suspect, and an
// explicit --key signals deliberate intent and bypasses the guard.
//
// It stays at the FRONT DOOR and stays FIRST: it reads the cobra command
// tree, which no role can see, and it must answer before any storage is
// opened so that `bd remember list` in a directory with no workspace
// still says "looks like a command".
if memoryKeyFlag == "" {
if name, ok := matchesKnownCommand(cmd, insight); ok {
return HandleErrorWithHintRespectJSON(
fmt.Sprintf("%q looks like a command, not something to remember", insight),
fmt.Sprintf("Did you mean 'bd %s'? To store %q as a memory anyway, give it an explicit key: bd remember %q --key <key>", name, insight, insight),
)
}
}
memories, err := openMemories("remember requires direct database access")
if err != nil {
return HandleError("%v", err)
}
// Desire path + footgun guard: `bd remember <x>` is a WRITE whose positional arg is
// the CONTENT, not a key -- but "remember X" reads as a getter in English, so agents
// routinely type `bd remember some-key` meaning "do you remember X?". The tell-tale of
// a mistyped read is content that round-trips through the key derivation unchanged (a
// bare slug); real prose insights never do. When that happens and no explicit --key was
// given:
// - the key EXISTS -> pave the desire path: recall it instead of writing
// - no such key -> refuse; storing a key-like token as its own content would
// create a junk memory that hides the mistake
// Passing --key states write intent and bypasses both branches.
//
// It stays ABOVE the role because it decides WHETHER TO WRITE AT ALL, and
// because it exists to disambiguate English: an HTTP POST is not ambiguous
// and must not inherit it. The read below is a plain Recall, so this whole
// branch touches nothing.
//
// `derived != ""` is load-bearing and is not decoration: DeriveKey("")
// is "", so without it every empty or unslugifiable insight would satisfy
// derived == insight and be routed into a "recall" of the empty key. The
// shipped code was saved from that by an empty-content check that ran
// first; that check is the role's now, so the condition has to say it.
derived := memoryapi.DeriveKey(insight)
if memoryKeyFlag == "" && derived != "" && derived == insight {
recalled, err := memories.Recall(rootCtx, memoryops.RecallRequest{Key: derived})
if err != nil {
return HandleErrorRespectJSON("recalling memory: %v", err)
}
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.
//
// The key an overwrite is credited against is ResolveKey's, the one
// rule every Remember implementation lands a write under, so it cannot
// drift from the store's key. A write ResolveKey refuses (empty
// content, a whitespace-only --key, content that derives no key) skips
// the budget: Remember refuses it below with its own validation
// sentence, and a refused write crosses nothing.
budgetKey, budgetKeyErr := memoryapi.ResolveKey(memoryKeyFlag, insight)
var budgetLine string
if budget := memoryCorpusBudget(); budget > 0 && budgetKeyErr == nil {
// ADVISORY, not atomic: this List and the Remember below are
// separate transactions, so two concurrent writers can both measure
// a corpus inside the budget and both land, leaving it over. The
// budget is a guardrail against a corpus drifting past its ceiling
// one deliberate write at a time, not a hard limit — no locking.
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 is said ONCE, on exactly one stream, the way
// HandleErrorWithHintRespectJSON does it: --json gets only the
// machine-readable envelope on stdout (printing the prose to
// stderr as well would make the same refusal arrive twice),
// and every other route gets the bare sentence on stderr,
// where it can never corrupt structured output. Both routes
// carry the same hint, so a terminal user is offered the same
// ways out as a JSON consumer.
const hint = "raise memories.budget-chars, forget a memory, or pass --force"
if jsonOutput {
jsonStdoutError(line, hint)
} else {
fmt.Fprintln(os.Stderr, line)
fmt.Fprintf(os.Stderr, "Hint: %s\n", hint)
}
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 —
// "memory content cannot be empty" and "could not generate key from
// content; use --key to specify one" — so they print as themselves.
// Wrapping would reword output an agent may be matching on into
// "storing memory: validation failed: ..." to say the same thing.
if errors.Is(err, memoryops.ErrValidation) {
return HandleErrorRespectJSON("%s", strings.TrimPrefix(err.Error(), memoryops.ErrValidation.Error()+": "))
}
return HandleErrorRespectJSON("storing memory: %v", err)
}
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.
verb := "Remembered"
if result.Replaced {
verb = "Updated"
}
return printRememberResult(verb, result.Key, result.Value)
},
}
// memoriesCmd lists and searches memories.
var memoriesCmd = &cobra.Command{
Use: "memories [search]",
Short: "List or search persistent memories",
Long: `List all memories, or search by keyword.
Examples:
bd memories # list all memories
bd memories dolt # search for memories about dolt
bd memories "race flag" # search for a phrase`,
GroupID: "setup",
Args: cobra.MaximumNArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
evt := metrics.NewCommandEvent("memories")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
var search string
if len(args) > 0 {
search = args[0]
}
memories, err := openMemories("memories requires direct database access")
if err != nil {
return HandleError("%v", err)
}
// The term goes to the role RAW. Case folding is List's, so the two
// routes cannot come to disagree about what matches — which is the
// whole reason the filter moved down.
result, err := memories.List(rootCtx, memoryops.ListRequest{Search: search})
if err != nil {
return HandleErrorRespectJSON("listing memories: %v", err)
}
// The ECHO, on the other hand, has always been lowercased: `bd memories
// FOO` prints `No memories matching "foo"`. It is a wart — a front door
// should say back what the user typed — but it is shipped output, and
// this commit is a convergence, not a change. Fixing it is a one-liner
// with its own test, like truncateMemory's rune splitting.
return printMemoriesResult(result.Memories, strings.ToLower(search))
},
}
// forgetCmd removes a memory.
var forgetCmd = &cobra.Command{
Use: "forget <key>",
Short: "Remove a persistent memory",
Long: `Remove a memory by its key.
Use 'bd memories' to see available keys.
Examples:
bd forget dolt-phantoms
bd forget auth-jwt`,
GroupID: "setup",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
CheckReadonly("forget")
evt := metrics.NewCommandEvent("forget")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
memories, err := openMemories("forget requires direct database access")
if err != nil {
return HandleError("%v", err)
}
// No pre-read here, deliberately: the value printed below is the one
// the role's transaction actually deleted, not the one an earlier read
// happened to see.
result, err := memories.Forget(rootCtx, memoryops.ForgetRequest{Key: args[0]})
if err != nil {
return HandleErrorRespectJSON("forgetting memory: %v", err)
}
if !result.Found {
return printForgetNotFound(result.Key)
}
noteDirectMemoryWrite()
return printForgetResult(result.Key, result.Value)
},
}
// recallCmd retrieves a specific memory by key.
var recallCmd = &cobra.Command{
Use: "recall <key>",
Short: "Retrieve a specific memory",
Long: `Retrieve the full content of a memory by its key.
Examples:
bd recall dolt-phantoms
bd recall auth-jwt`,
GroupID: "setup",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
evt := metrics.NewCommandEvent("recall")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
memories, err := openMemories("recall requires direct database access")
if err != nil {
return HandleError("%v", err)
}
result, err := memories.Recall(rootCtx, memoryops.RecallRequest{Key: args[0]})
if err != nil {
return HandleErrorRespectJSON("recalling memory: %v", err)
}
return printRecallResult(result.Key, result.Value)
},
}
// truncateMemory shortens a string to maxLen for display.
func truncateMemory(s string, maxLen int) string {
// Replace newlines with spaces for single-line display
s = strings.ReplaceAll(s, "\n", " ")
return truncate(s, maxLen)
}
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)
rootCmd.AddCommand(forgetCmd)
rootCmd.AddCommand(recallCmd)
}