From a8e5644fa63db0f8cdf2ec2a23b35de7ed347809 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Sun, 15 Mar 2026 21:07:42 +0000 Subject: [PATCH] Use single shared session for all command handlers --- crates/bough-cli/src/find_best_mutations.rs | 4 +- crates/bough-cli/src/main.rs | 107 +++++++++--------- crates/bough-cli/src/show_all_files.rs | 4 +- crates/bough-cli/src/show_all_mutations.rs | 5 +- crates/bough-cli/src/show_file_mutations.rs | 4 +- crates/bough-cli/src/show_language_files.rs | 4 +- .../bough-cli/src/show_language_mutations.rs | 5 +- crates/bough-cli/src/show_single_mutation.rs | 5 +- crates/bough-cli/src/step_apply_mutation.rs | 5 +- crates/bough-cli/src/step_init_workspace.rs | 7 +- crates/bough-cli/src/step_reset_workspace.rs | 7 +- crates/bough-cli/src/step_tend_state.rs | 5 +- crates/bough-cli/src/step_tend_workspaces.rs | 6 +- crates/bough-cli/src/step_test_mutation.rs | 7 +- crates/bough-cli/src/step_unapply_mutation.rs | 5 +- 15 files changed, 96 insertions(+), 84 deletions(-) diff --git a/crates/bough-cli/src/find_best_mutations.rs b/crates/bough-cli/src/find_best_mutations.rs index bca581a4..185292d4 100644 --- a/crates/bough-cli/src/find_best_mutations.rs +++ b/crates/bough-cli/src/find_best_mutations.rs @@ -1,3 +1,4 @@ +use std::ops::Deref; use std::path::PathBuf; use bough_core::{LanguageId, MutationHash, Session, State}; @@ -10,8 +11,7 @@ use crate::render::{ pub struct FindBestMutations(pub Vec<(MutationHash, State, f64)>); impl FindBestMutations { - pub fn run(config: Config, lang: Option, file: Option) -> Box { - let session = Session::new(config).expect("session creation"); + pub fn run(session: impl Deref>, lang: Option, file: Option) -> Box { let results = session.find_best_mutations().expect("find best mutations"); let filtered: Vec<_> = results .into_iter() diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index c230f9ee..07861c1e 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -37,7 +37,7 @@ fn main() { info!(log_level = %log_level, "tracing initialized"); - let mut session = Session::new(cli.config.clone()).expect("session creation"); + let session = Session::new(cli.config.clone()).expect("session creation"); let session = Arc::new(Mutex::new(session)); let result: Box = match cli.command { @@ -46,33 +46,33 @@ fn main() { match show { Show::Config => Box::new(cli.config.clone()), - Show::Files { lang: None } => show_all_files::ShowAllFiles::run(cli.config.clone()), + Show::Files { lang: None } => show_all_files::ShowAllFiles::run(session.lock().unwrap()), Show::Files { lang: Some(lang) } => { - show_language_files::ShowLanguageFiles::run(cli.config.clone(), *lang) + show_language_files::ShowLanguageFiles::run(session.lock().unwrap(), *lang) } Show::Mutations { lang: None, file: _, - } => show_all_mutations::ShowAllMutations::run(cli.config.clone()), + } => show_all_mutations::ShowAllMutations::run(session.lock().unwrap()), Show::Mutations { lang: Some(lang), file: None, - } => show_language_mutations::ShowLanguageMutations::run(cli.config.clone(), *lang), + } => show_language_mutations::ShowLanguageMutations::run(session.lock().unwrap(), *lang), Show::Mutations { lang: Some(lang), file: Some(file), } => show_file_mutations::ShowFileMutations::run( - cli.config.clone(), + session.lock().unwrap(), *lang, file.clone(), ), Show::Mutation { hash } => { - show_single_mutation::ShowSingleMutation::run(cli.config.clone(), hash) + show_single_mutation::ShowSingleMutation::run(session.lock().unwrap(), hash) } } } @@ -81,25 +81,25 @@ fn main() { debug!(subcommand = ?step, "executing step command"); match step { - config::Step::TendState => step_tend_state::StepTendState::run(cli.config.clone()), + config::Step::TendState => step_tend_state::StepTendState::run(session.lock().unwrap()), config::Step::TendWorkspaces => { - step_tend_workspaces::StepTendWorkspaces::run(cli.config.clone()) + step_tend_workspaces::StepTendWorkspaces::run(session.lock().unwrap(), cli.config.workers as usize) } config::Step::InitWorkspace { workspace_id } => { - step_init_workspace::StepInitWorkspace::run(cli.config.clone(), workspace_id) + step_init_workspace::StepInitWorkspace::run(session.lock().unwrap(), &cli.config, workspace_id) } config::Step::ResetWorkspace { workspace_id } => { - step_reset_workspace::StepResetWorkspace::run(cli.config.clone(), workspace_id) + step_reset_workspace::StepResetWorkspace::run(session.lock().unwrap(), &cli.config, workspace_id) } config::Step::ApplyMutation { workspace_id, mutation_hash, } => step_apply_mutation::StepApplyMutation::run( - cli.config.clone(), + session.lock().unwrap(), workspace_id, mutation_hash, ), @@ -108,7 +108,7 @@ fn main() { workspace_id, mutation_hash, } => step_unapply_mutation::StepUnapplyMutation::run( - cli.config.clone(), + session.lock().unwrap(), workspace_id, mutation_hash, ), @@ -117,7 +117,8 @@ fn main() { workspace_id, mutation_hash, } => step_test_mutation::StepTestMutation::run( - cli.config.clone(), + session.lock().unwrap(), + &cli.config, workspace_id, mutation_hash, ), @@ -126,18 +127,17 @@ fn main() { Command::Run => { info!("starting run"); - let mut session = Session::new(cli.config.clone()).expect("session creation"); - let added = session + let added = session.lock().unwrap() .tend_add_missing_states() .expect("tend add missing states"); - let removed = session + let removed = session.lock().unwrap() .tend_remove_stale_states() .expect("tend remove stale states"); println!("{}", (render::TendState { added, removed }).render(&cli)); let workers = cli.config.workers as usize; - let workspace_ids = session.tend_workspaces(workers).expect("tend workspaces"); + let workspace_ids = session.lock().unwrap().tend_workspaces(workers).expect("tend workspaces"); println!( "{}", @@ -147,47 +147,52 @@ fn main() { .render(&cli) ); - let base = session.base(); + let (init_duration, reset_duration, test_duration, total) = { + let mut guard = session.lock().unwrap(); + let base = guard.base(); - let init_duration = match base.run_init(&cli.config, None) { - Ok(outcome) => { - if outcome.exit_code() != 0 { - eprintln!("base init failed (exit {})", outcome.exit_code()); + let init_duration = match base.run_init(&cli.config, None) { + Ok(outcome) => { + if outcome.exit_code() != 0 { + eprintln!("base init failed (exit {})", outcome.exit_code()); + std::process::exit(1); + } + Some(outcome.duration()) + } + Err(bough_core::PhaseError::NoCmdConfigured) => None, + Err(e) => { + eprintln!("base init error: {e}"); std::process::exit(1); } - Some(outcome.duration()) - } - Err(bough_core::PhaseError::NoCmdConfigured) => None, - Err(e) => { - eprintln!("base init error: {e}"); - std::process::exit(1); - } - }; + }; - let reset_duration = match base.run_reset(&cli.config, None) { - Ok(outcome) => { - if outcome.exit_code() != 0 { - eprintln!("base reset failed (exit {})", outcome.exit_code()); + let reset_duration = match base.run_reset(&cli.config, None) { + Ok(outcome) => { + if outcome.exit_code() != 0 { + eprintln!("base reset failed (exit {})", outcome.exit_code()); + std::process::exit(1); + } + Some(outcome.duration()) + } + Err(bough_core::PhaseError::NoCmdConfigured) => None, + Err(e) => { + eprintln!("base reset error: {e}"); std::process::exit(1); } - Some(outcome.duration()) - } - Err(bough_core::PhaseError::NoCmdConfigured) => None, - Err(e) => { - eprintln!("base reset error: {e}"); + }; + + let test_outcome = base + .run_test(&cli.config, None) + .expect("base test execution"); + if test_outcome.exit_code() != 0 { + eprintln!("base test failed (exit {})", test_outcome.exit_code()); std::process::exit(1); } - }; - let test_outcome = base - .run_test(&cli.config, None) - .expect("base test execution"); - if test_outcome.exit_code() != 0 { - eprintln!("base test failed (exit {})", test_outcome.exit_code()); - std::process::exit(1); - } + let total = guard.get_count_mutation_needing_test() as u64; + (init_duration, reset_duration, test_outcome.duration(), total) + }; - let test_duration = test_outcome.duration(); let benchmark = render::BenchmarkTimesInBase { init: init_duration, reset: reset_duration, @@ -195,8 +200,6 @@ fn main() { }; println!("{}", benchmark.render(&cli)); - let total = session.get_count_mutation_needing_test() as u64; - let session = Arc::new(Mutex::new(session)); let done = Arc::new(AtomicBool::new(false)); let pb = if cli.progress { @@ -362,7 +365,7 @@ fn main() { } Command::Find { ref lang, ref file } => { - find_best_mutations::FindBestMutations::run(cli.config.clone(), *lang, file.clone()) + find_best_mutations::FindBestMutations::run(session.lock().unwrap(), *lang, file.clone()) } Command::Noop => { diff --git a/crates/bough-cli/src/show_all_files.rs b/crates/bough-cli/src/show_all_files.rs index 2dae68fe..7a7cbcda 100644 --- a/crates/bough-cli/src/show_all_files.rs +++ b/crates/bough-cli/src/show_all_files.rs @@ -1,3 +1,4 @@ +use std::ops::Deref; use std::path::PathBuf; use bough_core::{File, Session}; @@ -10,8 +11,7 @@ use crate::render::{PATH, RESET, Render}; pub struct ShowAllFiles(pub Vec); impl ShowAllFiles { - pub fn run(config: Config) -> Box { - let session = Session::new(config).expect("session creation"); + pub fn run(session: impl Deref>) -> Box { let base = session.base(); let twigs = base.twigs().collect::>(); let files = twigs diff --git a/crates/bough-cli/src/show_all_mutations.rs b/crates/bough-cli/src/show_all_mutations.rs index 2b6b0175..183d5a33 100644 --- a/crates/bough-cli/src/show_all_mutations.rs +++ b/crates/bough-cli/src/show_all_mutations.rs @@ -1,3 +1,5 @@ +use std::ops::DerefMut; + use bough_core::{Session, State}; use bough_typed_hash::TypedHashable; use facet::Facet; @@ -9,8 +11,7 @@ use crate::render::{Render, fmt_mutation_markdown_table, fmt_mutation_terse, fmt pub struct ShowAllMutations(pub Vec); impl ShowAllMutations { - pub fn run(config: Config) -> Box { - let mut session = Session::new(config).expect("session creation"); + pub fn run(mut session: impl DerefMut>) -> Box { session.tend_add_missing_states().expect("tend states"); let base = session.base(); let mutations: Vec<_> = base diff --git a/crates/bough-cli/src/show_file_mutations.rs b/crates/bough-cli/src/show_file_mutations.rs index 35350b6e..9b63ffe3 100644 --- a/crates/bough-cli/src/show_file_mutations.rs +++ b/crates/bough-cli/src/show_file_mutations.rs @@ -1,3 +1,4 @@ +use std::ops::DerefMut; use std::path::PathBuf; use bough_core::{LanguageId, Session, State}; @@ -11,8 +12,7 @@ use crate::render::{Render, fmt_mutation_markdown_table, fmt_mutation_terse, fmt pub struct ShowFileMutations(pub LanguageId, pub PathBuf, pub Vec); impl ShowFileMutations { - pub fn run(config: Config, lang: LanguageId, file: PathBuf) -> Box { - let mut session = Session::new(config).expect("session creation"); + pub fn run(mut session: impl DerefMut>, lang: LanguageId, file: PathBuf) -> Box { session.tend_add_missing_states().expect("tend states"); let base = session.base(); let mutations: Vec<_> = base diff --git a/crates/bough-cli/src/show_language_files.rs b/crates/bough-cli/src/show_language_files.rs index f420219d..81e30304 100644 --- a/crates/bough-cli/src/show_language_files.rs +++ b/crates/bough-cli/src/show_language_files.rs @@ -1,3 +1,4 @@ +use std::ops::Deref; use std::path::PathBuf; use bough_core::{File, LanguageId, Session}; @@ -10,8 +11,7 @@ use crate::render::{PATH, RESET, Render}; pub struct ShowLanguageFiles(pub LanguageId, pub Vec); impl ShowLanguageFiles { - pub fn run(config: Config, lang: LanguageId) -> Box { - let session = Session::new(config).expect("session creation"); + pub fn run(session: impl Deref>, lang: LanguageId) -> Box { let base = session.base(); let twigs = base.mutant_twigs().collect::>(); let files = twigs diff --git a/crates/bough-cli/src/show_language_mutations.rs b/crates/bough-cli/src/show_language_mutations.rs index c683315a..9f6ad8e8 100644 --- a/crates/bough-cli/src/show_language_mutations.rs +++ b/crates/bough-cli/src/show_language_mutations.rs @@ -1,3 +1,5 @@ +use std::ops::DerefMut; + use bough_core::{LanguageId, Session, State}; use bough_typed_hash::TypedHashable; use facet::Facet; @@ -9,8 +11,7 @@ use crate::render::{Render, fmt_mutation_markdown_table, fmt_mutation_terse, fmt pub struct ShowLanguageMutations(pub LanguageId, pub Vec); impl ShowLanguageMutations { - pub fn run(config: Config, lang: LanguageId) -> Box { - let mut session = Session::new(config).expect("session creation"); + pub fn run(mut session: impl DerefMut>, lang: LanguageId) -> Box { session.tend_add_missing_states().expect("tend states"); let base = session.base(); let mutations: Vec<_> = base diff --git a/crates/bough-cli/src/show_single_mutation.rs b/crates/bough-cli/src/show_single_mutation.rs index 3e398d0c..442d9335 100644 --- a/crates/bough-cli/src/show_single_mutation.rs +++ b/crates/bough-cli/src/show_single_mutation.rs @@ -1,3 +1,5 @@ +use std::ops::DerefMut; + use bough_core::{LanguageId, Session, State}; use bough_typed_hash::{TypedHash, TypedHashable, UnvalidatedHash}; @@ -15,8 +17,7 @@ pub struct ShowSingleMutation { } impl ShowSingleMutation { - pub fn run(config: Config, hash: &str) -> Box { - let mut session = Session::new(config).expect("session creation"); + pub fn run(mut session: impl DerefMut>, hash: &str) -> Box { session.tend_add_missing_states().expect("tend states"); let base = session.base(); let mutations: Vec<_> = base diff --git a/crates/bough-cli/src/step_apply_mutation.rs b/crates/bough-cli/src/step_apply_mutation.rs index 31500e27..a044260b 100644 --- a/crates/bough-cli/src/step_apply_mutation.rs +++ b/crates/bough-cli/src/step_apply_mutation.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use bough_core::Session; use bough_typed_hash::{TypedHash, TypedHashable, UnvalidatedHash}; @@ -10,8 +12,7 @@ pub struct StepApplyMutation { } impl StepApplyMutation { - pub fn run(config: Config, workspace_id: &str, mutation_hash: &str) -> Box { - let session = Session::new(config).expect("session creation"); + pub fn run(session: impl Deref>, workspace_id: &str, mutation_hash: &str) -> Box { let wid = bough_core::WorkspaceId::parse(workspace_id).expect("invalid workspace id"); let base = session.base(); let mutations: Vec<_> = base diff --git a/crates/bough-cli/src/step_init_workspace.rs b/crates/bough-cli/src/step_init_workspace.rs index 56a255df..d4765ba8 100644 --- a/crates/bough-cli/src/step_init_workspace.rs +++ b/crates/bough-cli/src/step_init_workspace.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use bough_core::Session; use crate::config::Config; @@ -12,11 +14,10 @@ pub struct StepInitWorkspace { } impl StepInitWorkspace { - pub fn run(config: Config, workspace_id: &str) -> Box { - let session = Session::new(config.clone()).expect("session creation"); + pub fn run(session: impl Deref>, config: &Config, workspace_id: &str) -> Box { let wid = bough_core::WorkspaceId::parse(workspace_id).expect("invalid workspace id"); let workspace = session.bind_workspace(&wid).expect("bind workspace"); - let outcome = workspace.run_init(&config, None).expect("init workspace"); + let outcome = workspace.run_init(config, None).expect("init workspace"); Box::new(Self { workspace_id: wid, outcome, diff --git a/crates/bough-cli/src/step_reset_workspace.rs b/crates/bough-cli/src/step_reset_workspace.rs index 5c5e7094..c18b596e 100644 --- a/crates/bough-cli/src/step_reset_workspace.rs +++ b/crates/bough-cli/src/step_reset_workspace.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use bough_core::Session; use crate::config::Config; @@ -12,12 +14,11 @@ pub struct StepResetWorkspace { } impl StepResetWorkspace { - pub fn run(config: Config, workspace_id: &str) -> Box { - let session = Session::new(config.clone()).expect("session creation"); + pub fn run(session: impl Deref>, config: &Config, workspace_id: &str) -> Box { let wid = bough_core::WorkspaceId::parse(workspace_id).expect("invalid workspace id"); let workspace = session.bind_workspace(&wid).expect("bind workspace"); let outcome = workspace - .run_reset(&config, None) + .run_reset(config, None) .expect("reset workspace"); Box::new(Self { workspace_id: wid, diff --git a/crates/bough-cli/src/step_tend_state.rs b/crates/bough-cli/src/step_tend_state.rs index 204bf8a6..aa15761e 100644 --- a/crates/bough-cli/src/step_tend_state.rs +++ b/crates/bough-cli/src/step_tend_state.rs @@ -1,3 +1,5 @@ +use std::ops::DerefMut; + use bough_core::Session; use crate::config::Config; @@ -9,8 +11,7 @@ pub struct StepTendState { } impl StepTendState { - pub fn run(config: Config) -> Box { - let mut session = Session::new(config).expect("session creation"); + pub fn run(mut session: impl DerefMut>) -> Box { let added = session .tend_add_missing_states() .expect("tend add missing states"); diff --git a/crates/bough-cli/src/step_tend_workspaces.rs b/crates/bough-cli/src/step_tend_workspaces.rs index fb57765f..466993ae 100644 --- a/crates/bough-cli/src/step_tend_workspaces.rs +++ b/crates/bough-cli/src/step_tend_workspaces.rs @@ -1,3 +1,5 @@ +use std::ops::DerefMut; + use bough_core::Session; use crate::config::Config; @@ -8,9 +10,7 @@ pub struct StepTendWorkspaces { } impl StepTendWorkspaces { - pub fn run(config: Config) -> Box { - let mut session = Session::new(config.clone()).expect("session creation"); - let workers = config.workers as usize; + pub fn run(mut session: impl DerefMut>, workers: usize) -> Box { let workspace_ids = session.tend_workspaces(workers).expect("tend workspaces"); Box::new(Self { workspace_ids }) } diff --git a/crates/bough-cli/src/step_test_mutation.rs b/crates/bough-cli/src/step_test_mutation.rs index 50c4a4cb..492041ea 100644 --- a/crates/bough-cli/src/step_test_mutation.rs +++ b/crates/bough-cli/src/step_test_mutation.rs @@ -1,3 +1,5 @@ +use std::ops::DerefMut; + use bough_core::Session; use bough_typed_hash::{TypedHash, TypedHashable, UnvalidatedHash}; @@ -12,8 +14,7 @@ pub struct StepTestMutation { } impl StepTestMutation { - pub fn run(config: Config, workspace_id: &str, mutation_hash: &str) -> Box { - let mut session = Session::new(config.clone()).expect("session creation"); + pub fn run(mut session: impl DerefMut>, config: &Config, workspace_id: &str, mutation_hash: &str) -> Box { let wid = bough_core::WorkspaceId::parse(workspace_id).expect("invalid workspace id"); let base = session.base(); let mutations: Vec<_> = base @@ -25,7 +26,7 @@ impl StepTestMutation { let mut workspace = session.bind_workspace(&wid).expect("bind workspace"); workspace.write_mutant(&mutation).expect("apply mutation"); let outcome = workspace - .run_test(&config, None) + .run_test(config, None) .expect("test mutation"); workspace.revert_mutant().expect("revert mutation"); let status = if outcome.exit_code() != 0 { diff --git a/crates/bough-cli/src/step_unapply_mutation.rs b/crates/bough-cli/src/step_unapply_mutation.rs index 23e224ab..581a8e6e 100644 --- a/crates/bough-cli/src/step_unapply_mutation.rs +++ b/crates/bough-cli/src/step_unapply_mutation.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use bough_core::Session; use crate::config::Config; @@ -9,8 +11,7 @@ pub struct StepUnapplyMutation { } impl StepUnapplyMutation { - pub fn run(config: Config, workspace_id: &str, mutation_hash: &str) -> Box { - let session = Session::new(config).expect("session creation"); + pub fn run(session: impl Deref>, workspace_id: &str, mutation_hash: &str) -> Box { let wid = bough_core::WorkspaceId::parse(workspace_id).expect("invalid workspace id"); let mut workspace = session.bind_workspace(&wid).expect("bind workspace"); workspace.revert_mutant().expect("unapply mutation"); -- knotmirror 0.0.1