Skip to content

fix(cli): fail cleanly when a DB-requiring command has no backend - #729

Open
syf2211 wants to merge 3 commits into
Metabuilder-Labs:mainfrom
syf2211:fix/db-none-clean-error
Open

fix(cli): fail cleanly when a DB-requiring command has no backend#729
syf2211 wants to merge 3 commits into
Metabuilder-Labs:mainfrom
syf2211:fix/db-none-clean-error

Conversation

@syf2211

@syf2211 syf2211 commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Add a shared guard in TjCommand.invoke that raises a ClickException when a DB-requiring leaf command runs with ctx.obj["db"] is None, instead of crashing with an AttributeError traceback.

Motivation

Issue #727: commands that skip open_db() (including the --help skip path from #726) but still execute can reach DB-touching code with db=None. cmd_optimize already had a hand-rolled message; other commands like loop annotate did not.

Changes

  • Add db_required_message() and _ensure_db_available() to TjCommand (leaf commands only; groups skip the guard)
  • Set ctx.obj["requires_db"] in cli/main.py (False for no_db_commands, True otherwise)
  • Remove the redundant db is None check from cmd_optimize; reuse the shared message for the ApiBackend path
  • Add unit + integration regression tests

Tests

pytest tests/unit/test_tj_db_guard.py tests/integration/test_cli.py::test_db_requiring_command_fails_cleanly_when_db_is_none tests/integration/test_cli.py::test_nested_subcommand_help_skips_db_probe -v
ruff check tokenjam/cli/tj_status.py tokenjam/cli/main.py tokenjam/cli/cmd_optimize.py
mypy tokenjam/cli/tj_status.py tokenjam/cli/main.py

All passed locally.

Notes

Fixes #727

syf2211 and others added 3 commits August 17, 2026 00:06
Detect --help in remaining Click args during TjGroup/TjCommand invoke
and set a context flag so the root callback skips open_db(). Fixes
nested invocations like `tj optimize downsize --help` failing with a
database-lock error while tj serve is running.

Fixes Metabuilder-Labs#580.
Add a shared guard in TjCommand.invoke that raises ClickException when
db is None on DB-requiring leaf commands (Metabuilder-Labs#727). Wire requires_db through
cli/main.py and reuse db_required_message in cmd_optimize.

Includes regression tests for the loop annotate false-positive --help
path and unit coverage for the guard.
@syf2211
syf2211 requested a review from anilmurty as a code owner August 18, 2026 00:09
@greptile-apps

greptile-apps Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR adds a centralized Click command guard that converts missing-backend crashes into actionable errors for database-requiring leaf commands.

  • Propagates database requirements through the root CLI context.
  • Reuses a shared missing-database message in the optimize command.
  • Adds unit and integration coverage for missing backends and nested help.

Confidence Score: 5/5

The PR appears safe to merge, with no actionable defects identified in the centralized database guard or its command coverage.

The current registered database-requiring leaves route through TjCommand, database-independent top-level commands retain their opt-out behavior, and missing backends now fail through an actionable Click exception.

Important Files Changed

Filename Overview
tokenjam/cli/tj_status.py Adds shared database-requirement metadata, missing-backend validation, and help-token detection to the central command class.
tokenjam/cli/main.py Records whether the selected top-level command requires a database and skips database opening on the nested-help path.
tokenjam/cli/cmd_optimize.py Delegates missing-backend handling to the shared guard and reuses the centralized error message.
tests/unit/test_tj_db_guard.py Verifies shared error formatting, leaf-command enforcement, and group-command exemption.
tests/integration/test_cli.py Covers nested help without a database probe and clean failure when a database-requiring leaf executes without a backend.

Reviews (1): Last reviewed commit: "fix(cli): fail cleanly when a DB-requiri..." | Re-trigger Greptile

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.

CLI: a command that needs a DB should fail cleanly when the backend is None

2 participants