Skip to content

Commit 631e153

Browse files
committed
refactor: harden error handling and improve observability
Use .expect() instead of .unwrap() on static regex compilation for a clear panic message. Log print task panics instead of silently dropping them. Log individual validation violations before retry. Document the non-atomic TOCTOU risk in the split commit flow.
1 parent 9d87954 commit 631e153

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/app.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ impl App {
240240
.generate(&prompt, tx, self.cancel_token.clone())
241241
.await?;
242242

243-
let _ = print_handle.await;
243+
if let Err(e) = print_handle.await {
244+
warn!("print task panicked: {e}");
245+
}
244246

245247
if num_candidates == 1 {
246248
eprintln!(); // Newline after streaming
@@ -531,7 +533,9 @@ impl App {
531533
.generate(&prompt, tx, self.cancel_token.clone())
532534
.await?;
533535

534-
let _ = print_handle.await;
536+
if let Err(e) = print_handle.await {
537+
warn!("print task panicked: {e}");
538+
}
535539

536540
if raw_message.trim().is_empty() {
537541
return Err(Error::Provider {
@@ -576,7 +580,10 @@ impl App {
576580
return Err(Error::Cancelled);
577581
}
578582

579-
// Execute: unstage all, then stage+commit per group
583+
// Execute: unstage all, then stage+commit per group.
584+
// NOTE: This is non-atomic — if an intermediate commit fails, earlier
585+
// commits are already applied with no automatic rollback. The index
586+
// state between unstage_all() and stage_files() is also a TOCTOU window.
580587
for (i, (message, files)) in commit_messages.iter().enumerate() {
581588
git.unstage_all().await?;
582589
git.stage_files(files).await?;
@@ -1068,10 +1075,13 @@ fi
10681075
};
10691076
}
10701077

1078+
for v in &violations {
1079+
debug!(attempt, violation = %v, "validation failed");
1080+
}
10711081
debug!(
10721082
attempt,
10731083
violations = violations.len(),
1074-
"violations detected, retrying with corrections"
1084+
"retrying with corrections"
10751085
);
10761086

10771087
let corrections = CommitValidator::format_corrections(&violations);

src/services/analyzer.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ pub struct DiffHunk {
2323
}
2424

2525
// Robust regex for parsing unified diff hunk headers
26-
static HUNK_REGEX: LazyLock<Regex> =
27-
LazyLock::new(|| Regex::new(r"^@@\s*-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s*@@").unwrap());
26+
static HUNK_REGEX: LazyLock<Regex> = LazyLock::new(|| {
27+
Regex::new(r"^@@\s*-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s*@@")
28+
.expect("static hunk header regex is valid")
29+
});
2830

2931
impl DiffHunk {
3032
/// Parse hunks from unified diff format

0 commit comments

Comments
 (0)