Skip to content

[DBA-292] Refactor databao-cli: Phase 2#74

Open
catstrike wants to merge 7 commits intomainfrom
ls/cli-refactoring-2
Open

[DBA-292] Refactor databao-cli: Phase 2#74
catstrike wants to merge 7 commits intomainfrom
ls/cli-refactoring-2

Conversation

@catstrike
Copy link
Collaborator

Closes DBA-292

Summary

Restructures the codebase into a clean four-layer architecture, removing business logic from commands/, consolidating scattered domain code, and fully decoupling features/ from Click.

Changes

1. Feature-Based Architecture

Restructures the codebase into the following layers:

commands/       # Click routing only
workflows/      # Interactive CLI sequences
features/       # Pure business logic
shared/         # Cross-feature utilities

Moves mcp/, ui/, log/, and project/ into features/ and shared/.

Files
  • src/databao_cli/features/ (ask, init, datasource, mcp, ui, build, index, status)
  • src/databao_cli/shared/ (project, log, cli_utils, context_engine_cli, executor_utils, errors)

2. commands/ as a Pure Routing Layer

Each command file now contains only Click wiring and delegates execution via a single call. Business logic, error handling, and I/O are no longer in commands/.

Files
  • src/databao_cli/commands/ask.py
  • src/databao_cli/commands/app.py
  • src/databao_cli/commands/build.py
  • src/databao_cli/commands/index.py
  • src/databao_cli/commands/init.py
  • src/databao_cli/commands/mcp.py
  • src/databao_cli/commands/status.py
  • src/databao_cli/commands/datasource/add.py
  • src/databao_cli/commands/datasource/check.py

3. features/ Decoupled from Click

All click imports and usage removed from features/. Feature functions now return structured data instead of printing, and do not implement REPL loops, user prompts, or output formatting. They are directly callable from tests, MCP tools, and the Streamlit UI.

Introduces FeatureError + @handle_feature_errors decorator so feature functions raise a typed exception instead of calling sys.exit.

Files
  • src/databao_cli/shared/errors.py
  • src/databao_cli/shared/cli_utils.py
  • src/databao_cli/features/ask/agent_factory.py
  • src/databao_cli/features/ask/service.py
  • src/databao_cli/features/ask/display.py
  • src/databao_cli/features/datasource/add.py
  • src/databao_cli/features/datasource/check.py
  • src/databao_cli/features/ui/cli.py
  • src/databao_cli/features/status.py

4. workflows/ Layer for Interactive CLI Sequences

Moves all interactive flows (REPL, wizards, output display) into workflows/. workflows/ owns CLI UX and orchestration; features/ owns business logic and correctness.

Files
  • src/databao_cli/workflows/ask.py
  • src/databao_cli/workflows/datasource/add.py
  • src/databao_cli/workflows/datasource/check.py

Test Plan

  • make check passes (ruff + mypy)
  • make test — 66/66 passing
  • databao --help smoke test
  • databao datasource add interactive wizard works end-to-end
  • databao status exits 0 both inside and outside a project directory

catstrike and others added 7 commits March 23, 2026 18:01
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Both test_build and test_index called duckdb.execute() on the shared
default in-memory connection instead of the file-scoped connection,
causing "table t1 already exists" when both tests ran in the same suite.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements “Phase 2” of the databao-cli refactor by reorganizing the codebase into a 4-layer architecture (commands/, workflows/, features/, shared/) and updating the CLI/UI/MCP entrypoints and tests to match.

Changes:

  • Introduces the new features/, shared/, and workflows/ layers and moves/rewires core logic accordingly.
  • Refactors Click commands to be thin routing wrappers and centralizes command registration via COMMANDS in __main__.py.
  • Updates Streamlit UI and MCP server/tool modules to their new package locations and adjusts tests/imports.

Reviewed changes

Copilot reviewed 65 out of 91 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/test_query_executor_race_conditions.py Updates imports/patch paths for moved Streamlit query executor.
tests/test_query_executor.py Updates imports/patch paths for moved Streamlit query executor.
tests/test_index.py Updates ProjectLayout import and fixes DuckDB usage to close connection.
tests/test_build.py Updates ProjectLayout import and fixes DuckDB usage to close connection.
tests/test_app.py Updates patch target to new Streamlit bootstrap location.
tests/test_add_datasource.py Updates init implementation import to new feature layer.
tests/conftest.py Updates init/layout imports to new feature/shared locations.
src/databao_cli/workflows/datasource/check.py Adds CLI printing workflow for datasource connection checks.
src/databao_cli/workflows/datasource/add.py Moves datasource “add wizard” interaction into workflows layer; delegates to feature functions.
src/databao_cli/workflows/datasource/init.py Package marker for datasource workflows.
src/databao_cli/workflows/ask.py Adds interactive/one-shot ask workflows and CLI result display.
src/databao_cli/workflows/init.py Package marker for workflows.
src/databao_cli/ui/models/init.py Removes legacy UI models package re-export (moved under features).
src/databao_cli/shared/project/layout.py Introduces shared project layout + project discovery utilities.
src/databao_cli/shared/project/init.py Package marker for shared project utilities.
src/databao_cli/shared/log/logging.py Updates import path for moved ProjectLayout.
src/databao_cli/shared/log/llm_errors.py Adds shared LLM error formatting helpers for CLI/UI.
src/databao_cli/shared/log/init.py Package marker for shared logging.
src/databao_cli/shared/executor_utils.py Centralizes executor/provider constants and LLM config builder.
src/databao_cli/shared/errors.py Adds FeatureError exception type for feature-layer failures.
src/databao_cli/shared/context_engine_cli.py Adds Click↔DCE user input adapter (ClickUserInputCallback).
src/databao_cli/shared/cli_utils.py Adds get_project_or_raise and @handle_feature_errors decorator for Click commands.
src/databao_cli/shared/init.py Package marker for shared utilities.
src/databao_cli/mcp/tools/databao_ask/init.py Removes legacy MCP tool package re-export (moved under features).
src/databao_cli/features/ui/suggestions.py Adds Streamlit suggested-questions generation + background execution.
src/databao_cli/features/ui/streaming.py Adds streaming writer used by UI and CLI workflows.
src/databao_cli/features/ui/services/storage.py Adds UI storage path management with basic validation and secure mkdir.
src/databao_cli/features/ui/services/settings_persistence.py Updates imports to new UI models/storage locations.
src/databao_cli/features/ui/services/query_executor.py Updates imports for moved results/models/streaming modules.
src/databao_cli/features/ui/services/llm_models.py Updates shared executor utils import for model preference selection.
src/databao_cli/features/ui/services/dce_operations.py Updates init/status/layout imports to new feature/shared locations.
src/databao_cli/features/ui/services/chat_title.py Updates chat session import path.
src/databao_cli/features/ui/services/chat_persistence.py Updates storage/chat session imports to new feature locations.
src/databao_cli/features/ui/services/build_service.py Updates dce_operations import path.
src/databao_cli/features/ui/services/init.py Updates service re-exports to new feature/ui service module paths.
src/databao_cli/features/ui/project_utils.py Updates ProjectLayout import path to shared.
src/databao_cli/features/ui/pages/welcome.py Updates imports to new feature/ui module structure.
src/databao_cli/features/ui/pages/general_settings.py Updates imports to new feature/ui module structure.
src/databao_cli/features/ui/pages/context_settings.py Updates imports to new feature/ui module structure.
src/databao_cli/features/ui/pages/chat.py Updates imports to new feature/ui module structure.
src/databao_cli/features/ui/pages/agent_settings.py Updates imports to shared executor utils and new feature/ui services.
src/databao_cli/features/ui/pages/init.py Adds package docstring for UI pages.
src/databao_cli/features/ui/models/settings.py Introduces new settings dataclasses and YAML (de)serialization.
src/databao_cli/features/ui/models/chat_session.py Updates streaming writer type import path.
src/databao_cli/features/ui/models/init.py Adds models package re-exports in new location.
src/databao_cli/features/ui/components/status.py Adds Streamlit status component with fragment polling + context manager.
src/databao_cli/features/ui/components/sidebar.py Updates imports to new feature/shared modules.
src/databao_cli/features/ui/components/results.py Updates imports to new feature/ui services and models.
src/databao_cli/features/ui/components/icons.py Adds DB icon/type detection utilities for UI.
src/databao_cli/features/ui/components/datasource_manager.py Updates imports to new feature/ui module structure.
src/databao_cli/features/ui/components/datasource_form.py Adds dynamic form rendering driven by DCE config definitions.
src/databao_cli/features/ui/components/chat.py Updates imports to new feature/ui module structure.
src/databao_cli/features/ui/components/init.py Adds package docstring for UI components.
src/databao_cli/features/ui/cli.py Updates Streamlit app module resolution and adds app_impl wrapper raising FeatureError.
src/databao_cli/features/ui/assets/bao.png Adds UI asset image.
src/databao_cli/features/ui/app.py Updates imports throughout to new feature/shared module layout.
src/databao_cli/features/ui/init.py Adds package docstring for UI feature.
src/databao_cli/features/status.py Moves status business logic into features layer.
src/databao_cli/features/mcp/tools/databao_ask/tool.py Updates imports to new feature/mcp module layout.
src/databao_cli/features/mcp/tools/databao_ask/agent_factory.py Updates imports and shared LLM config builder usage.
src/databao_cli/features/mcp/tools/databao_ask/init.py Adds MCP tool package re-export in new location.
src/databao_cli/features/mcp/tools/init.py Package marker for feature MCP tools.
src/databao_cli/features/mcp/server.py Updates tool import path and adds mcp_impl entrypoint.
src/databao_cli/features/mcp/init.py Package marker for feature MCP module.
src/databao_cli/features/init/service.py Moves init project creation logic into features layer.
src/databao_cli/features/init/errors.py Adds typed init error hierarchy.
src/databao_cli/features/init/init.py Package marker for init feature.
src/databao_cli/features/index.py Moves index logic into features layer.
src/databao_cli/features/datasource/check.py Moves datasource check logic into features layer.
src/databao_cli/features/datasource/add.py Moves datasource config creation helpers into features layer.
src/databao_cli/features/datasource/init.py Package marker for datasource feature.
src/databao_cli/features/build.py Moves build logic into features layer.
src/databao_cli/features/ask/service.py Adds ask feature service that validates inputs and initializes agent.
src/databao_cli/features/ask/display.py Adds DataFrame→PrettyTable formatter for CLI display.
src/databao_cli/features/ask/agent_factory.py Moves agent initialization logic into feature layer.
src/databao_cli/features/ask/init.py Package marker for ask feature.
src/databao_cli/features/init.py Package marker for features.
src/databao_cli/commands/status.py Refactors command into Click-only routing; delegates to features.status.
src/databao_cli/commands/mcp.py Implements Click command wrapper for MCP server execution.
src/databao_cli/commands/init.py Refactors init command to route to init feature + datasource workflow.
src/databao_cli/commands/index.py Refactors index command to route to features.index.
src/databao_cli/commands/datasource/check.py Adds datasource check Click command routing to feature + workflow print.
src/databao_cli/commands/datasource/add.py Adds datasource add Click command routing to workflow.
src/databao_cli/commands/datasource/init.py Adds datasource Click group and registers subcommands.
src/databao_cli/commands/build.py Refactors build command to route to features.build.
src/databao_cli/commands/ask.py Refactors ask command to route to ask feature + ask workflows.
src/databao_cli/commands/app.py Refactors app command to route to UI feature CLI bootstrap.
src/databao_cli/main.py Centralizes command registration via COMMANDS list and adds commands to root group.
examples/demo-snowflake-project/src/databao_snowflake_demo/app.py Updates example import to new Streamlit app module path.
docs/architecture.md Updates architecture documentation to describe the new 4-layer structure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@catstrike catstrike self-assigned this Mar 23, 2026
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.

2 participants