Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions apps/decodex/src/orchestrator/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ struct LoopGuardrailWorktreeFingerprint {
head_sha: String,
tracked_status_hash: String,
tracked_diff_hash: String,
effective_status_hash: String,
effective_delta_present: bool,
}

struct FailureHandlingContext<'a, T>
Expand Down Expand Up @@ -1976,23 +1978,25 @@ fn retryable_failure_loop_guardrail_stop(
"{}:{}:{}:{}",
repo_gate_failure.error_class(),
worktree_fingerprint.head_sha.as_str(),
worktree_fingerprint.tracked_status_hash.as_str(),
worktree_fingerprint.effective_status_hash.as_str(),
worktree_fingerprint.tracked_diff_hash.as_str()
),
Some(repo_gate_failure.error_class()),
));
}

observations.push((
LoopGuardrailReason::NoEffectiveDiff,
format!(
"{}:{}:{}",
worktree_fingerprint.head_sha.as_str(),
worktree_fingerprint.tracked_status_hash.as_str(),
worktree_fingerprint.tracked_diff_hash.as_str()
),
retained_progress_source_error_class(error),
));
if !worktree_fingerprint.effective_delta_present {
observations.push((
LoopGuardrailReason::NoEffectiveDiff,
format!(
"{}:{}:{}",
worktree_fingerprint.head_sha.as_str(),
worktree_fingerprint.effective_status_hash.as_str(),
worktree_fingerprint.tracked_diff_hash.as_str()
),
retained_progress_source_error_class(error),
));
}

for (reason, fingerprint, source_error_class) in observations {
let checkpoint = state_store.observe_loop_guardrail_checkpoint(
Expand All @@ -2010,6 +2014,8 @@ fn retryable_failure_loop_guardrail_stop(
"head_sha": worktree_fingerprint.head_sha.as_str(),
"tracked_status_hash": worktree_fingerprint.tracked_status_hash.as_str(),
"tracked_diff_hash": worktree_fingerprint.tracked_diff_hash.as_str(),
"effective_status_hash": worktree_fingerprint.effective_status_hash.as_str(),
"effective_delta_present": worktree_fingerprint.effective_delta_present,
"threshold": LOOP_GUARDRAIL_CONVERGENCE_BUDGET,
})
.to_string(),
Expand Down Expand Up @@ -2051,19 +2057,45 @@ fn loop_guardrail_worktree_fingerprint(
else {
return Ok(None);
};
let Some(raw_status) = git_guardrail_output(worktree_path, &["status", "--porcelain"])? else {
return Ok(None);
};
let Some(tracked_diff) =
git_guardrail_output(worktree_path, &["diff", "--binary", "--no-ext-diff", "HEAD", "--"])?
else {
return Ok(None);
};
let effective_status = loop_guardrail_effective_status(&raw_status);

Ok(Some(LoopGuardrailWorktreeFingerprint {
head_sha,
tracked_status_hash: loop_guardrail_text_hash(&tracked_status),
tracked_diff_hash: loop_guardrail_text_hash(&tracked_diff),
effective_status_hash: loop_guardrail_text_hash(&effective_status),
effective_delta_present: !effective_status.trim().is_empty()
|| !tracked_diff.trim().is_empty(),
}))
}

fn loop_guardrail_effective_status(raw_status: &str) -> String {
let lines = raw_status
.lines()
.map(str::trim_end)
.filter(|line| !line.is_empty())
.filter(|line| !state::is_untracked_decodex_runtime_artifact_status_line(line))
.collect::<Vec<_>>();

if lines.is_empty() {
return String::new();
}

let mut status = lines.join("\n");

status.push('\n');

status
}

fn git_guardrail_output(worktree_path: &Path, args: &[&str]) -> Result<Option<String>> {
let output = Command::new("git")
.arg("-C")
Expand Down Expand Up @@ -2502,6 +2534,8 @@ fn architecture_recovery_retained_worktree(worktree_path: &Path) -> Result<Value
let fingerprint = loop_guardrail_worktree_fingerprint(worktree_path)?;
let tracked_status =
git_guardrail_output(worktree_path, &["status", "--porcelain", "--untracked-files=no"])?;
let raw_status = git_guardrail_output(worktree_path, &["status", "--porcelain"])?;
let effective_status = raw_status.as_deref().map(loop_guardrail_effective_status);
let diff_stat =
git_guardrail_output(worktree_path, &["diff", "--stat", "--no-ext-diff", "HEAD", "--"])?;

Expand All @@ -2511,7 +2545,14 @@ fn architecture_recovery_retained_worktree(worktree_path: &Path) -> Result<Value
.as_ref()
.map(|value| value.tracked_status_hash.as_str()),
"tracked_diff_hash": fingerprint.as_ref().map(|value| value.tracked_diff_hash.as_str()),
"effective_status_hash": fingerprint
.as_ref()
.map(|value| value.effective_status_hash.as_str()),
"effective_delta_present": fingerprint
.as_ref()
.map(|value| value.effective_delta_present),
"tracked_status": tracked_status.unwrap_or_default(),
"effective_status": effective_status.unwrap_or_default(),
"diff_stat": diff_stat.unwrap_or_default(),
}))
}
Expand Down
114 changes: 114 additions & 0 deletions apps/decodex/src/orchestrator/tests/runtime/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,120 @@ fn loop_guardrail_stops_no_effective_diff_for_retryable_errors_without_delta() {
assert_eq!(checkpoint.consecutive_count(), 3);
}

#[test]
fn loop_guardrail_does_not_classify_dirty_retained_diff_as_no_effective_diff() {
let (_temp_dir, config, _workflow) = temp_project_layout();
let state_store = StateStore::open_in_memory().expect("state store should open");
let issue = sample_issue("In Progress", &[]);

fs::write(config.repo_root().join("README.md"), "retained validation-ready patch\n")
.expect("tracked file should become dirty");

for attempt_number in 1..=3 {
let issue_run = loop_guardrail_issue_run(&config, &issue, attempt_number);
let error = Report::msg("phase completed local validation without terminal handoff");
let stop = orchestrator::retryable_failure_loop_guardrail_stop(
&config,
&state_store,
&issue_run,
&error,
)
.expect("guardrail observation should evaluate");

assert!(
stop.is_none(),
"dirty retained progress should not be reported as no_effective_diff"
);
}

assert!(
state_store
.loop_guardrail_checkpoint(config.service_id(), &issue.id, "no_effective_diff")
.expect("no-diff checkpoint should read")
.is_none(),
"no_effective_diff is reserved for retryable failures with no effective delta"
);
}

#[test]
fn loop_guardrail_does_not_classify_untracked_retained_files_as_no_effective_diff() {
let (_temp_dir, config, _workflow) = temp_project_layout();
let state_store = StateStore::open_in_memory().expect("state store should open");
let issue = sample_issue("In Progress", &[]);

fs::write(config.repo_root().join("new-runbook.md"), "retained validation-ready file\n")
.expect("untracked source file should write");

for attempt_number in 1..=3 {
let issue_run = loop_guardrail_issue_run(&config, &issue, attempt_number);
let error = Report::msg("phase completed local validation without terminal handoff");
let stop = orchestrator::retryable_failure_loop_guardrail_stop(
&config,
&state_store,
&issue_run,
&error,
)
.expect("guardrail observation should evaluate");

assert!(
stop.is_none(),
"untracked retained source files should not be reported as no_effective_diff"
);
}

assert!(
state_store
.loop_guardrail_checkpoint(config.service_id(), &issue.id, "no_effective_diff")
.expect("no-diff checkpoint should read")
.is_none(),
"no_effective_diff is reserved for retryable failures with no effective delta"
);
}

#[test]
fn loop_guardrail_ignores_untracked_decodex_runtime_artifacts_for_no_effective_diff() {
let (_temp_dir, config, _workflow) = temp_project_layout();
let state_store = StateStore::open_in_memory().expect("state store should open");
let issue = sample_issue("In Progress", &[]);
let control_dir = config.repo_root().join(".decodex-run-control");

fs::write(config.repo_root().join(RUN_ACTIVITY_MARKER_FILE), "heartbeat\n")
.expect("runtime activity marker should write");
fs::create_dir_all(&control_dir).expect("runtime control directory should exist");
fs::write(control_dir.join("command.json"), "{}\n")
.expect("runtime control file should write");

for attempt_number in 1..=2 {
let issue_run = loop_guardrail_issue_run(&config, &issue, attempt_number);
let error = Report::msg("child exited without useful change");

assert!(
orchestrator::retryable_failure_loop_guardrail_stop(
&config,
&state_store,
&issue_run,
&error,
)
.expect("guardrail observation should persist")
.is_none()
);
}

let issue_run = loop_guardrail_issue_run(&config, &issue, 3);
let error = Report::msg("child exited without useful change");
let stop = orchestrator::retryable_failure_loop_guardrail_stop(
&config,
&state_store,
&issue_run,
&error,
)
.expect("third no-diff observation should evaluate")
.expect("runtime-only artifacts should still count as no effective diff");

assert_eq!(stop.reason, orchestrator::LoopGuardrailReason::NoEffectiveDiff);
assert_eq!(stop.consecutive_count, 3);
}

#[test]
fn app_server_terminal_failures_preserve_specific_error_classes() {
let cases = [
Expand Down
Loading