Skip to content

[FEATURE] Auto-Format on Save #466

@divideby0

Description

@divideby0

Feature Description

Add optional automatic file formatting using external formatters (like Prettier) when Basic Memory writes files. Includes a standalone basic-memory format command for batch formatting existing files.

Problem This Feature Solves

Basic Memory writes .md, .canvas, and .json files without formatting, leading to:

  • Inconsistent file styling across projects
  • Noisy version control diffs when users manually format files
  • Extra manual steps for users who want formatted output
  • No standardized formatting for teams without external tooling

Proposed Solution

Auto-Format on Save

Add configuration options to automatically format files after MCP tools write them:

{
  "format_on_save": true,
  "formatter_command": "prettier --write {file}",
  "formatters": {
    "md": "prettier --write {file}",
    "json": "prettier --write {file}"
  },
  "formatter_timeout": 5.0
}

Environment variables:

  • BASIC_MEMORY_FORMAT_ON_SAVE=true
  • BASIC_MEMORY_FORMATTER_COMMAND="prettier --write {file}"
  • BASIC_MEMORY_FORMATTERS='{"md": "prettier --write {file}"}'

Integration points (where formatting hooks in):

  • write_note MCP tool
  • edit_note MCP tool
  • canvas MCP tool

Standalone Format Command

# Format all files in current/default project
basic-memory format

# Format files in specific project
basic-memory format --project my-research

# Format specific file or directory
basic-memory format notes/meeting.md

Key Design Decisions

  • Disabled by default (opt-in)
  • Formatter errors warn but don't fail the write
  • Async subprocess execution with configurable timeout
  • Re-reads file after formatting to compute correct checksum
  • Per-extension formatters with global fallback

Trade-offs

Formatting adds a subprocess call per file write, which has minor performance overhead and requires external tools (like prettier) to be installed. However, the token efficiency impact is likely negligible or even positive - consistent formatting produces predictable patterns that may be easier for LLMs to parse than inconsistently formatted content.

Alternative Solutions

  1. Claude Code hooks - Users can set up post-tool hooks to run prettier, but this only works in Claude Code, not other MCP clients
  2. Pre-commit hooks - Requires separate tooling setup and doesn't format on save
  3. Editor integration - Requires users to open files in an editor with format-on-save

Additional Context

  • PRD with full technical details: .taskmaster/docs/prd.md
  • Feature branch: feature/auto-format-on-save
  • Uses existing BasicMemoryConfig Pydantic model with env_prefix="BASIC_MEMORY_"

Impact

  • Developers: Consistent formatting without manual intervention
  • Teams: Standardized output across all team members
  • Obsidian users: Properly formatted canvas JSON files
  • Version control: Cleaner diffs with consistent styling

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions