basic-memory icon indicating copy to clipboard operation
basic-memory copied to clipboard

feat(cli): add Rich human-readable output to bm tool commands

Open groksrc opened this issue 5 months ago • 2 comments

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 known total=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, so read-note X --plain --include-frontmatter > note.md round-trips a note byte-faithfully
  • build-context — tree over the real nested ContextResult shape: 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 titled Spec [draft] v2 renders 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's str | dict narrowing 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_style config, 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_RESULT fixture documents that the real _extract_recent_rows output has no updated_at key (display falls back to created_at).
  • Plain renderers are intentionally minimal (numbered lists / indented outlines) — formatting opinions welcome.

🤖 Generated with Claude Code

groksrc avatar Jun 11 '26 06:06 groksrc

--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 --plain even when piped.
    • search → numbered results with title/score/permalink + indented snippet
    • read-note → title [permalink] header, optional key: value frontmatter (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 updated lines

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)

  1. --json — wins over everything
  2. --plain — forces plain, even when piped
  3. non-TTY stdout → JSON (script compatibility)
  4. TTY → config cli_output_style (rich by 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 the total=0 count fallback in plain mode).
  • Precedence matrix: --json alone → JSON; --plain forces plain when piped; non-TTY default still JSON; TTY+config rich → Rich; TTY+config plain → plain; --json --plain together → non-zero error.
  • uv run pytest tests/cli/test_cli_tool_rich_output.py tests/cli/test_cli_tools.py green (42 passed); JSON-output/exit/config suites green; ruff check + format --check clean; uv run ty check src tests test-int passes.

🤖 Generated with Claude Code

groksrc avatar Jun 12 '26 01:06 groksrc

This is good to go, but needs to ship in v0.next

groksrc avatar Jun 12 '26 17:06 groksrc

@codex review

phernandez avatar Jul 15 '26 02:07 phernandez

recheck

phernandez avatar Jul 15 '26 02:07 phernandez

@codex review

phernandez avatar Jul 15 '26 03:07 phernandez

recheck

phernandez avatar Jul 15 '26 03:07 phernandez

@codex review

phernandez avatar Jul 15 '26 03:07 phernandez

recheck

phernandez avatar Jul 15 '26 03:07 phernandez

@codex review

recheck

phernandez avatar Jul 15 '26 04:07 phernandez

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

phernandez avatar Jul 15 '26 05:07 phernandez

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".