feat(cli): add Rich human-readable output to bm tool commands
Closes #678
Summary
Adds human-readable terminal output to four bm tool commands — search-notes, read-note, build-context, recent-activity — with three output modes and a configurable interactive default.
Output modes & precedence
| Mode | When |
|---|---|
| JSON | --json, or any non-TTY stdout (piped/redirected) — unchanged script compat |
| Plain | --plain — greppable text, no ANSI colors, no box-drawing, no markup (dumb terminals, screen readers, CI logs) |
| Rich | interactive TTY default — Panels/Tables/Trees/rendered Markdown |
Precedence: --json > --plain (forces plain even when piped) > non-TTY → JSON > TTY → configured style. --json --plain together is an error. Resolution is centralized in _resolve_output_mode() rather than per-command branching.
New config option
cli_output_style: "rich" | "plain" (default rich, env BASIC_MEMORY_CLI_OUTPUT_STYLE) sets the interactive default for folks whose terminals don't render Rich well. Documented via the config Field description (the repo has no general CLI-settings doc yet — noted on #977).
Renderers
- search-notes — table with Type/Title/Score/Permalink/Snippet (~200 char matched chunk); panel title shows the query; count falls back to
len(results)when the API returns the knowntotal=0-with-results quirk - read-note — Rich: header panel + rendered Markdown, frontmatter panel only with
--include-frontmatter(the flag makes the API return the literal file as content; the Rich path strips the block from the body so it renders once). Plain: the payload verbatim — note body, or the literal file with the flag — no header, no placeholders, soread-note X --plain --include-frontmatter > note.mdround-trips a note byte-faithfully - build-context — tree over the real nested
ContextResultshape: primary entities, their observations ([category] content), and related results with relation types - recent-activity — table of recent items
- search/build-context/recent-activity have plain-text twins mirroring the same content; plain read-note is the faithful payload (see above)
Hardening (from manual QA + Codex review)
- Rich markup injection fixed: all user-sourced text (titles, snippets, categories, frontmatter, tree labels, the query) passes through
rich.markup.escape— a note titledSpec [draft] v2renders literally instead of being eaten as a style tag (and[red]can't restyle your terminal). Plain mode deliberately does NOT escape (it isn't markup). - String-typed tool results route to the JSON path (also keeps
ty'sstr | dictnarrowing clean).
Tests
test_cli_tool_rich_output.py: 40 tests covering Rich + plain + JSON modes per command, the full precedence matrix (incl. --plain while piped, config-driven defaults, --json --plain conflict), bracket-survival in both Rich and plain, frontmatter flag gating, and the total=0 count fallback. Fixtures use the real payload shapes (current_page, nested primary_result/observations/related_results). All existing JSON-path tests unchanged and green.
Flag rename
bm tool read-note --include-frontmatter is renamed to --frontmatter; the old name remains as a deprecated alias (it shipped in 0.22.0). MCP tool parameter include_frontmatter is unchanged.
Manual updates
- Done now:
manual/man3/read-note-3(team workspace) — CLI synopsis uses--frontmatter, ARGUMENTS notes the deprecated alias. - At merge: the manual should gain the output-mode story for the four commands (
--json/--plain/ Rich default,cli_output_styleconfig, plain read-note's faithful/round-trip behavior) —read-note-3's "json mode only" framing changes once this PR's display modes ship.
Review notes
RECENT_ACTIVITY_RESULTfixture documents that the real_extract_recent_rowsoutput has noupdated_atkey (display falls back tocreated_at).- Plain renderers are intentionally minimal (numbered lists / indented outlines) — formatting opinions welcome.
🤖 Generated with Claude Code
--plain output mode + cli_output_style config default
Extends the rich-tool-output work with a third output mode and a configurable interactive default for the four formatted bm tool commands (search-notes, read-note, build-context, recent-activity).
Three output modes
- JSON — raw machine-readable output. Used with
--json, or automatically when stdout is piped/redirected (unchanged script compatibility). - Rich — colored Panel/Table/Tree/Markdown. The out-of-the-box TTY experience.
- Plain — undecorated, greppable text: no ANSI colors, no box-drawing, no markup. Forced with
--plaineven when piped.- search → numbered results with title/score/permalink + indented snippet
- read-note →
title [permalink]header, optionalkey: valuefrontmatter (only with--include-frontmatter), then the raw markdown body - build-context → ASCII-indented outline (primary, then 2-space-indented observations and related items with relation types)
- recent-activity →
- title (type) permalink updatedlines
Plain renderers intentionally do not apply rich.markup.escape (that's only correct on the Rich path and would corrupt literal brackets), so [draft]/[fact] survive verbatim.
Precedence (highest first)
--json— wins over everything--plain— forces plain, even when piped- non-TTY stdout → JSON (script compatibility)
- TTY → config
cli_output_style(richby default)
Passing both --json and --plain is a typer error with a clear message and non-zero exit.
New config option
BasicMemoryConfig.cli_output_style: Literal["rich", "plain"] = "rich" (env BASIC_MEMORY_CLI_OUTPUT_STYLE) sets the interactive TTY default. It follows the existing CLI-behavior field conventions; since the repo has no general CLI-settings reference doc, the Field description documents the setting (same as write_note_overwrite_default).
Tests / gates
- Per-command plain renderers (incl. literal
[draft]/[fact]survival and thetotal=0count fallback in plain mode). - Precedence matrix:
--jsonalone → JSON;--plainforces plain when piped; non-TTY default still JSON; TTY+configrich→ Rich; TTY+configplain→ plain;--json --plaintogether → non-zero error. uv run pytest tests/cli/test_cli_tool_rich_output.py tests/cli/test_cli_tools.pygreen (42 passed); JSON-output/exit/config suites green;ruff check+format --checkclean;uv run ty check src tests test-intpasses.
🤖 Generated with Claude Code
This is good to go, but needs to ship in v0.next
@codex review
recheck
@codex review
recheck
@codex review
recheck
@codex review
recheck
Codex Review: Something went wrong. Try again later by commenting “@codex review”.
Unknown error
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
@codex review /recheck
Codex Review: Didn't find any major issues. Nice work!
Reviewed commit: 25fbae7a99
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".