Skip to content

Refactor application orchestration and runtime bootstrap#179

Merged
michaelchu merged 9 commits intomainfrom
codex/architecture-review
Apr 6, 2026
Merged

Refactor application orchestration and runtime bootstrap#179
michaelchu merged 9 commits intomainfrom
codex/architecture-review

Conversation

@michaelchu
Copy link
Copy Markdown
Member

Summary

  • extract shared backtest, sweep, pipeline, and task orchestration into src/application/
  • rewire REST handlers, queued tasks, and MCP backtest flows to use the application layer instead of cross-importing handler code
  • consolidate runtime bootstrap into a validated services bundle and switch raw price logging to tracing

Testing

  • cargo build
  • cargo clippy --all-targets
  • cargo test --test backtest_api --test task_api --test pipeline

Copilot AI review requested due to automatic review settings April 6, 2026 17:37
Copy link
Copy Markdown
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 refactors backtest/sweep/pipeline orchestration into a new src/application/ layer and centralizes runtime bootstrap wiring into AppServices, reducing cross-importing between REST handlers, queued tasks, and MCP tool handlers.

Changes:

  • Added src/application/ modules (backtests, sweeps, pipeline, tasks) and rewired MCP/REST/task flows to call into these shared services.
  • Introduced AppServices bootstrap bundle (src/bootstrap.rs) and refactored main.rs to use transport-specific runners (run_http / run_stdio).
  • Replaced raw price endpoint eprintln! logging with structured tracing::debug!.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/tools/raw_prices.rs Switches raw price request logging from eprintln! to tracing.
src/tools/backtest.rs Routes single backtests, sweeps, and pipeline mode through the new application services.
src/server/handlers/tasks.rs Simplifies queued task execution via shared application::tasks orchestration and app-layer backtest/sweep/pipeline executors.
src/server/handlers/sweeps.rs Removes duplicated sweep logic and delegates sweep execution/persistence to application::sweeps.
src/server/handlers/run_script.rs Converts handler into compatibility wrappers around application::backtests.
src/server/handlers/pipeline.rs Delegates pipeline execution to application::pipeline instead of the MCP backtest tool.
src/server/handlers/backtests.rs Delegates backtest persistence to application::backtests helpers.
src/main.rs Refactors startup into run_http/run_stdio and uses AppServices for validated wiring.
src/lib.rs Exposes new application and bootstrap modules.
src/bootstrap.rs Adds AppServices to centralize DB/store/task-manager construction from env.
src/application/mod.rs Defines the application-layer module surface.
src/application/tasks.rs Introduces shared queued-task lifecycle helpers (permit, cancel, mark running/completed/failed).
src/application/sweeps.rs Adds shared sweep execution + persistence orchestration (grid/bayesian + permutation gate).
src/application/pipeline.rs Adds shared pipeline orchestration built on persisted sweep + existing pipeline stages.
src/application/backtests.rs Adds shared script execution + persistence helpers (symbol/capital resolution, trade stripping, DB insertion).
Comments suppressed due to low confidence (1)

src/server/handlers/backtests.rs:124

  • backtests::persist_backtest can return errors unrelated to database insertion (e.g., missing/resolution of symbol). Emitting these as "DB insert failed: ..." in the SSE stream is misleading; please surface the actual error (and optionally differentiate validation vs persistence failures).
                match backtests::persist_backtest(
                    &*state.run_store,
                    &strategy_key,
                    &req.params,
                    &response,
                    "manual",
                    None,
                ) {
                    Ok((id, _)) => {
                        if let Ok(Some(detail)) = state.run_store.get_run(&id) {
                            let json = serde_json::to_string(&detail).unwrap_or_default();
                            let _ = tx.send(Event::default().event("result").data(json)).await;
                        }
                    }
                    Err((_status, msg)) => {
                        let _ = tx
                            .send(
                                Event::default()
                                    .event("error")
                                    .data(format!("DB insert failed: {msg}")),
                            )
                            .await;
                    }

Comment thread src/server/handlers/tasks.rs Outdated
Comment thread src/server/handlers/tasks.rs
Copy link
Copy Markdown
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 15 out of 15 changed files in this pull request and generated 6 comments.

Comments suppressed due to low confidence (1)

src/server/handlers/backtests.rs:109

  • backtests::persist_backtest can return non-storage errors (e.g., symbol resolution -> BAD_REQUEST). This handler’s error path later prefixes all persist_backtest failures with "DB insert failed", which can be misleading. Consider branching on the returned StatusCode (only prefix INTERNAL_SERVER_ERROR) or emitting the raw message for validation/other failures.
                match backtests::persist_backtest(
                    &*state.run_store,
                    &strategy_key,
                    &req.params,
                    &response,
                    "manual",
                    None,
                ) {

Comment thread src/application/backtests.rs Outdated
Comment thread src/application/sweeps.rs
Comment thread src/application/sweeps.rs
Comment thread src/application/sweeps.rs
Comment thread src/main.rs
Comment thread src/main.rs
@michaelchu michaelchu merged commit 83b2ef7 into main Apr 6, 2026
5 checks passed
@michaelchu michaelchu deleted the codex/architecture-review branch April 6, 2026 18:13
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