Skip to content

Conversation

@baxen
Copy link
Collaborator

@baxen baxen commented Dec 25, 2025

Summary

Refactors the main cli() function in crates/goose-cli/src/cli.rs by:

  1. Breaking up the monolithic function into smaller, well-named helper functions
  2. Consolidating related CLI arguments into logical option structs using clap's #[command(flatten)] pattern

Changes

Helper Functions

Split the monolithic cli() function into smaller, well-named helper functions:

  • get_command_name(): Extract command name for telemetry
  • handle_mcp_command(): MCP server dispatch
  • handle_session_subcommand(): Session list/remove/export/diagnostics
  • handle_interactive_session(): Interactive session with telemetry
  • log_session_completion(): Shared session completion logging
  • parse_run_input(): Parse run command input sources
  • handle_run_command(): Execute run command
  • handle_schedule_command(): Schedule subcommand dispatch
  • handle_bench_command(): Benchmark subcommand dispatch
  • handle_recipe_subcommand(): Recipe subcommand dispatch
  • handle_term_subcommand(): Terminal subcommand dispatch
  • handle_default_session(): Default session when no command

Option Structs

Consolidated related arguments into logical groupings:

Struct Fields Used By
SessionOptions debug, max_tool_repetitions, max_turns Session, Run
ExtensionOptions extensions, remote_extensions, streamable_http_extensions, builtins Session, Run
InputOptions instructions, input_text, recipe, system, params, additional_sub_recipes, explain, render_recipe Run only
OutputOptions quiet, output_format Run only
ModelOptions provider, model Run only
RunBehavior interactive, no_session, resume, scheduled_job_id Run only

Benefits

  • Reduced duplication: SessionOptions and ExtensionOptions are shared between Session and Run commands
  • Cleaner function signatures: handle_run_command reduced from 11 args to 7 struct args
  • Better maintainability: Adding a new session option only requires changing one struct
  • No internal-only wrapper structs: Avoided the pattern of creating structs just to pass args around

Testing

  • All existing tests pass (cargo test -p goose-cli)
  • Clippy passes
  • Code compiles successfully

Related

Part of TSK-696: Simplify clippy baseline system

Split the main CLI function into smaller, well-named helper functions:
- get_command_name(): Extract command name for telemetry
- handle_mcp_command(): MCP server dispatch
- handle_session_subcommand(): Session list/remove/export/diagnostics
- handle_interactive_session(): Interactive session with telemetry
- log_session_completion(): Shared session completion logging
- parse_run_input(): Parse run command input sources
- handle_run_command(): Execute run command
- handle_schedule_command(): Schedule subcommand dispatch
- handle_bench_command(): Benchmark subcommand dispatch
- handle_recipe_subcommand(): Recipe subcommand dispatch
- handle_term_subcommand(): Terminal subcommand dispatch
- handle_default_session(): Default session when no command

Introduced structs to avoid too_many_arguments clippy warnings:
- InteractiveSessionArgs
- RunCommandArgs
- ParseRunInputArgs

The cli() function is now 131 lines, well under the 200 line target.
Copilot AI review requested due to automatic review settings December 25, 2025 02:56
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 successfully refactors the monolithic cli() function from 557 lines to 131 lines by extracting command handling logic into focused helper functions. The refactoring maintains all existing behavior while significantly improving code maintainability.

Key changes:

  • Extracted 12 helper functions for different command handlers (MCP, session, run, schedule, bench, recipe, term, etc.)
  • Introduced 3 parameter structs (InteractiveSessionArgs, RunCommandArgs, ParseRunInputArgs) to avoid clippy's too_many_arguments warnings
  • Simplified the main cli() function to a clean match statement that delegates to specialized handlers

@baxen baxen marked this pull request as draft December 25, 2025 03:04
@baxen
Copy link
Collaborator Author

baxen commented Dec 25, 2025

this PR is auto generated and will be refined. will move out of draft when ready for human review

- Merge InputSource and RecipeOptions into InputOptions
- Create shared SessionOptions and ExtensionOptions for Session/Run commands
- Create Run-specific structs: OutputOptions, ModelOptions, RunBehavior
- Remove internal-only structs: InteractiveSessionArgs, RunCommandArgs, ParseRunInputArgs
- Reduce handle_run_command from 11 args to 7 struct args
@baxen baxen marked this pull request as ready for review December 25, 2025 05:58
Copilot AI review requested due to automatic review settings December 25, 2025 05:58
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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

@block block deleted a comment from Copilot AI Dec 25, 2025
@baxen baxen changed the title TSK-697: Refactor cli() function from 557 to 131 lines chore: refactor cli() function to reduce line count Dec 25, 2025
@baxen baxen mentioned this pull request Dec 26, 2025
11 tasks
retry_config: recipe_info.as_ref().and_then(|r| r.retry_config.clone()),
output_format: output_opts.output_format,
})
.await;
Copy link
Collaborator

Choose a reason for hiding this comment

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

probably not for now, but we should really use a, eh, builder pattern for build_session(SessionBuilderConfig

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.

4 participants