refactor: DRY cleanup — extract shared helpers, remove duplication across tools, LSP, overlays, and runtime

Eliminate ~500 lines of duplicate code across 31 files by extracting
shared functions, helpers, and consolidating repeated patterns.

Highlights:
- Toast helpers (toast_info/success/warning/error) on AppStateRest
- push_event() helper for turn-event queue (19 callers consolidated)
- log_write_edit_tool() shared fn (turn.rs + engine.rs ~50 lines saved)
- resolve_api_key() shared fn (spawn.rs + provider.rs)
- LSP call_positional() helper on LspClient
- lsp_cursor_params() shared schema for 4 tool files
-overlay_block() helper for consistent overlay title/border styling
- cycle_selected_index(), path_not_found/a_directory() helpers
- mark_dirty(), save_settings() on AppStateRest
- Remove redundant Err(e) => Err(e) arms in LSP tools
- Consolidate generate_workspace_tree (turn.rs → workspace.rs)
- Simplify background-review wrapper args in auto/mod.rs

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
asepharyana
2026-07-18 03:18:27 +07:00
co-authored by Claude Opus 4.8
parent b02754acd2
commit 9a6ab62562
31 changed files with 628 additions and 870 deletions
@@ -248,16 +248,24 @@ fn spawn_background_review(
});
}
/// Collect the trailing arguments shared by all background-review spawners.
fn review_args<'a>(
file_paths: &'a [String],
session_dir: &'a Path,
workspaces: &'a [std::path::PathBuf],
turn_events: &'a Arc<Mutex<VecDeque<TurnEvent>>>,
abort_flag: Arc<AtomicBool>,
) -> (Vec<String>, std::path::PathBuf, Vec<std::path::PathBuf>, Arc<Mutex<VecDeque<TurnEvent>>>, Arc<AtomicBool>) {
(
file_paths.to_vec(),
session_dir.to_path_buf(),
workspaces.to_vec(),
turn_events.clone(),
abort_flag,
)
}
/// Spawn a background subagent that generates tests for modified files.
///
/// Uses the test-generator prompt and has read-write access so it can
/// create test files. Runs in a separate OS thread and reports completion
/// via `TurnEvent::SystemNote { kind: "bg-test-gen" }`.
///
/// Skipped (no-op) if a test-gen run is already in flight (guarded by
/// `TEST_GEN_RUNNING`) — prevents a chatty multi-turn edit session from
/// stacking overlapping runs. `abort_flag` is forwarded to the generic
/// spawner so the run can be cancelled if the turn aborts.
pub fn spawn_background_test_gen(
file_paths: &[String],
session_dir: &Path,
@@ -265,29 +273,15 @@ pub fn spawn_background_test_gen(
turn_events: &Arc<Mutex<VecDeque<TurnEvent>>>,
abort_flag: Arc<AtomicBool>,
) {
let (fps, sd, ws, te, af) = review_args(file_paths, session_dir, workspaces, turn_events, abort_flag);
spawn_background_review(
"bg-test-gen",
&TEST_GEN_RUNNING,
crate::prompts::TEST_GENERATOR_PROMPT,
"test-generator",
"coder",
file_paths.to_vec(),
session_dir.to_path_buf(),
workspaces.to_vec(),
turn_events.clone(),
abort_flag,
"bg-test-gen", &TEST_GEN_RUNNING,
crate::prompts::TEST_GENERATOR_PROMPT, "test-generator", "coder",
fps, sd, ws, te, af,
);
}
/// Spawn a background architecture-review subagent.
///
/// Inspects the modified files for architectural consistency (layering,
/// coupling, module boundaries). Reports via
/// `TurnEvent::SystemNote { kind: "bg-arch-review" }`.
///
/// Skipped (no-op) if an arch-review run is already in flight (guarded by
/// `ARCH_REVIEW_RUNNING`). `abort_flag` is forwarded to the generic
/// spawner so the run can be cancelled if the turn aborts.
pub fn spawn_background_arch_review(
file_paths: &[String],
session_dir: &Path,
@@ -295,31 +289,18 @@ pub fn spawn_background_arch_review(
turn_events: &Arc<Mutex<VecDeque<TurnEvent>>>,
abort_flag: Arc<AtomicBool>,
) {
let (fps, sd, ws, te, af) = review_args(file_paths, session_dir, workspaces, turn_events, abort_flag);
spawn_background_review(
"bg-arch-review",
&ARCH_REVIEW_RUNNING,
crate::prompts::ARCH_REVIEWER_PROMPT,
"arch-reviewer",
"reviewer",
file_paths.to_vec(),
session_dir.to_path_buf(),
workspaces.to_vec(),
turn_events.clone(),
abort_flag,
"bg-arch-review", &ARCH_REVIEW_RUNNING,
crate::prompts::ARCH_REVIEWER_PROMPT, "arch-reviewer", "reviewer",
fps, sd, ws, te, af,
);
}
/// Spawn a background security-review subagent.
///
/// Checks modified files for security vulnerabilities. Reports via
/// `TurnEvent::SystemNote { kind: "bg-security-review" }`.
///
/// Only reviews production code files for security — test files and
/// config files are out of scope for security review.
///
/// Skipped (no-op) if a security-review run is already in flight (guarded by
/// `SECURITY_REVIEW_RUNNING`). `abort_flag` is forwarded to the generic
/// spawner so the run can be cancelled if the turn aborts.
pub fn spawn_background_security_review(
file_paths: &[String],
session_dir: &Path,
@@ -327,25 +308,16 @@ pub fn spawn_background_security_review(
turn_events: &Arc<Mutex<VecDeque<TurnEvent>>>,
abort_flag: Arc<AtomicBool>,
) {
// Only review production code files for security — test files and
// config files are out of scope for security review.
let (_, sd, ws, te, af) = review_args(file_paths, session_dir, workspaces, turn_events, abort_flag);
let prod_paths: Vec<String> = file_paths
.iter()
.filter(|p| is_production_code(p))
.cloned()
.collect();
spawn_background_review(
"bg-security-review",
&SECURITY_REVIEW_RUNNING,
crate::prompts::SECURITY_REVIEWER_PROMPT,
"security-reviewer",
"reviewer",
prod_paths,
session_dir.to_path_buf(),
workspaces.to_vec(),
turn_events.clone(),
abort_flag,
"bg-security-review", &SECURITY_REVIEW_RUNNING,
crate::prompts::SECURITY_REVIEWER_PROMPT, "security-reviewer", "reviewer",
prod_paths, sd, ws, te, af,
);
}