Skip to content

fix(cli): honour SURREAL_MEMORY_BRAIN when --brain is omitted - #90

Open
RobertSigmundsson wants to merge 1 commit into
acidkill:mainfrom
RobertSigmundsson:upstream-pr/cli-resolve-brain-env
Open

fix(cli): honour SURREAL_MEMORY_BRAIN when --brain is omitted#90
RobertSigmundsson wants to merge 1 commit into
acidkill:mainfrom
RobertSigmundsson:upstream-pr/cli-resolve-brain-env

Conversation

@RobertSigmundsson

Copy link
Copy Markdown
Contributor

Problem

get_storage has its own env-var fallback for the brain name, but it only fires when the caller passes
None. Several CLI commands need the brain name before calling get_storage (to echo progress), and
they resolve it with the brain or config.current_brain pattern. That expression is never None, so
get_storage skips its own env check and SURREAL_MEMORY_BRAIN is silently ignored — the command
prints and operates on the config-file brain instead of the requested one.

Change

Add a single resolve_brain(brain, config, *, default=None) helper in cli/_helpers.py implementing the
documented priority — explicit arg > env var > default/config — and route brain.py, shortcuts.py
and tools.py through it. The helper's docstring states why the brain or config.current_brain pattern
must not be used directly.

Why upstream benefits

SURREAL_MEMORY_BRAIN is a documented knob; today it works for some commands and not others, with no
error to indicate which. One helper makes the precedence rule single-sourced and testable.

Side effect worth noting

main currently has a redundant expression at three call sites, e.g. cli/_helpers.py:90:

env_brain = os.environ.get("SURREAL_MEMORY_BRAIN") or os.environ.get("SURREAL_MEMORY_BRAIN")

— the same lookup on both sides of the or (also cli/commands/brain.py:39 and :87). Harmless, but a
clear leftover. Routing through resolve_brain removes all three.

Evidence

Check Result
cherry-pick onto origin/main clean
import surreal_memory OK
ruff check All checks passed!
ruff format --check 702 files already formatted
mypy src/ --ignore-missing-imports Success: no issues found in 369 source files
pytest -m "not stress" -n auto 6542 passed, 84 skipped, 1 xfailed (baseline 6535 → +7)

New file: tests/unit/test_cli_resolve_brain.py (+59) covering all three precedence branches.

Risks / notes for the reviewer

  • Behaviour change by design: a user who exported SURREAL_MEMORY_BRAIN and relied on those specific
    commands ignoring it will now see them honour it. That was the bug.
  • No storage-layer changes; backend-agnostic.

CLI commands resolved the brain as `brain or config.current_brain` before
calling get_storage(), always passing a non-None name. That made get_storage()
take its explicit-arg branch and skip its own env-var check, so
SURREAL_MEMORY_BRAIN was silently ignored and commands ran against the wrong
brain (falling back to 'default').

Add a central resolve_brain(brain, config, *, default=None) helper
(explicit-arg > env var > default/config) and route get_storage() plus all ten
CLI call sites through it: consolidate, decay, lifecycle (tools.py); export,
import (shortcuts.py); brain list/export/import/health (brain.py). Also drops
the duplicated SURREAL_MEMORY_BRAIN lookup in get_storage().

Add tests/unit/test_cli_resolve_brain.py covering the priority order and the
empty-env-var edge case.

(cherry picked from commit 1bdc7b2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant