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
1 change: 1 addition & 0 deletions docs/incidents/xy_merge_keeps_both_sides_and_parses.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
- Only a hunk both sides changed differently goes to the model, alone with its ancestor, ours and theirs and a few unchanged lines around it. Its reply must be that hunk's lines: prose, markdown fences and conflict markers are stripped where they wrap it and refused inside it, it can't be longer than both sides together, and every identifier either side added must still be there.
- The merged file is checked before anything is committed (`merge3::check_merged`): no conflict markers or prose, a line count within both sides' changes together, both sides' added identifiers present, and it parses for its language as well as the sides do (TypeScript, TSX, JavaScript, Python and Rust by their parsers; JSON, YAML and TOML must parse; other code by the bracket scan).
- A merge that fails is tried once more with smaller hunks. Failing again, nothing is pushed and the branch stays as it was. The failure is CoderHelm's own problem: an internal alert, a line on the run page, and the reason as the stop reason.
- A conflict round on CoderHelm's own pull request that meets a refused merge runs again in a fresh step against the base's newest head, while the base moves between tries and under the round's step ceiling. Out of tries, a person is asked only when both sides changed the same lines with incompatible intent and the model's merge of them was refused on both tries. That is a typed product decision with one question showing the two versions: keep A, keep B, or combine them. Anything else stops as CoderHelm's own problem, and no person is asked to "resolve the conflict".

The case replays it with invented content: a TSX file where the base changed many lines and the branch added two merges mechanically without asking the model, the result that was pushed fails every check, and a model reply of prose plus a rewritten file for a real overlapping hunk pushes nothing.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
//! language as well as the sides do. A failed merge is tried once more with
//! smaller hunks; failing again, nothing is pushed, the branch stays as it
//! was, and the failure is CoderHelm's own problem (an internal alert) with
//! the reason as the stop reason.
//! the reason as the stop reason. A conflict round on CoderHelm's own pull
//! request tries again in a fresh step against the base's newest head while
//! that makes progress; only a hunk both sides changed with incompatible
//! intent, refused on both tries, becomes one question to a person, with
//! the two versions (keep A, keep B, or combine). Never "resolve the
//! conflict".

use crate::passes::merge3::{self, HunkAsk, HunkModel};

Expand Down Expand Up @@ -282,8 +287,8 @@ fn an_overlapping_hunk_reaches_the_model_alone_and_a_prose_reply_pushes_nothing(
&mut model,
))
.unwrap_err();
assert!(err.0.contains("merging each conflicting hunk"), "{err}");
assert!(err.0.contains("smaller pieces"), "{err}");
assert!(err.why.contains("merging each conflicting hunk"), "{err}");
assert!(err.why.contains("smaller pieces"), "{err}");
// It saw the hunk, never the whole file.
assert!(!model.asked.is_empty());
for ask in &model.asked {
Expand Down Expand Up @@ -338,6 +343,17 @@ fn the_conflicted_merge_goes_through_the_three_way_merge() {
assert!(!pr.contains("Produce the correctly merged file"));
// The rebuilt stack merges through the same path.
assert!(include_str!("../restack.rs").contains("super::pr::merge_file("));
// The conflict round retries a refused merge and asks only the typed
// question; it never hands a person a generic conflict.
let resume = include_str!("../resume.rs");
let round = super::fixture::fn_src(resume, "async fn run_conflict_round(");
assert!(round.contains("after_refused(round, refused, &base_head, &base)"));
assert!(!round.contains("Please resolve"));
let after = super::fixture::fn_src(resume, "pub(crate) fn after_refused(");
assert!(after.contains(".stepped(base_head).parked(true)"));
assert!(after.contains("may_continue()"));
let ask = super::fixture::fn_src(resume, "pub(crate) fn contested_question(");
assert!(ask.contains("person_only::Want::ProductDecision"));
// A refused merge leaves the conflict standing where it was reported.
for src in [
include_str!("../await_merge.rs"),
Expand Down
116 changes: 102 additions & 14 deletions services/worker/src/passes/merge3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,37 @@ const DP_CELLS: usize = 4_000_000;
/// A merge CoderHelm refused to commit, with why. The branch is left as it
/// was.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Refused(pub String);
pub struct Refused {
pub why: String,
/// The hunk both sides changed with incompatible intent, when the
/// model's merge of it was refused on both tries (`None` for any other
/// failure, which is CoderHelm's own).
pub contested: Option<Contested>,
}

impl Refused {
/// A refusal that is CoderHelm's own, with no contested hunk.
pub fn own(why: impl Into<String>) -> Refused {
Refused {
why: why.into(),
contested: None,
}
}
}

/// The two competing versions of one hunk.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Contested {
pub path: String,
/// The branch's lines.
pub ours: String,
/// The other side's lines.
pub theirs: String,
}

impl std::fmt::Display for Refused {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
write!(f, "{}", self.why)
}
}

Expand Down Expand Up @@ -810,14 +836,31 @@ fn context_after(pieces: &[Piece<'_>], k: usize) -> String {
}
}

/// Why one try at the conflicts failed: the whole hunk whose reply was
/// refused (its ours and theirs), when the model replied and the reply was
/// refused.
struct Failed {
why: String,
refused_hunk: Option<(String, String)>,
}

impl From<String> for Failed {
fn from(why: String) -> Failed {
Failed {
why,
refused_hunk: None,
}
}
}

/// Every conflict of `pieces` resolved by the model at `scope`.
async fn resolve_all<M: HunkModel + ?Sized>(
path: &str,
their_label: &str,
pieces: &[Piece<'_>],
scope: Scope,
model: &mut M,
) -> Result<(String, usize), String> {
) -> Result<(String, usize), Failed> {
let mut text = String::new();
let mut asked = 0;
for (k, piece) in pieces.iter().enumerate() {
Expand Down Expand Up @@ -858,9 +901,18 @@ async fn resolve_all<M: HunkModel + ?Sized>(
.resolve(&ask)
.await
.map_err(|e| format!("the model couldn't merge a hunk ({e})"))?;
let out = hunk_reply(path, &raw, small)?;
echoed(&out, &before, &after, small)?;
text.push_str(&out);
let checked = hunk_reply(path, &raw, small)
.and_then(|out| echoed(&out, &before, &after, small).map(|()| out));
match checked {
Ok(out) => text.push_str(&out),
// The whole hunk is what a person would be asked about.
Err(why) => {
return Err(Failed {
why,
refused_hunk: Some((join(&h.ours), join(&h.theirs))),
})
}
}
}
}
Ok((text, asked))
Expand Down Expand Up @@ -914,22 +966,41 @@ pub async fn merge_text<M: HunkModel + ?Sized>(
.collect();
return check_merged(path, &base, ours, theirs, &text)
.map(|()| Merged { text, by_model: 0 })
.map_err(|e| Refused(format!("the mechanical merge failed its checks: {e}")));
.map_err(|e| Refused::own(format!("the mechanical merge failed its checks: {e}")));
}
let mut why = Vec::new();
// The hunk each try's reply was refused on (`None`: the try failed some
// other way, which is CoderHelm's own).
let mut refused_on = Vec::new();
for scope in [Scope::Hunk, Scope::Split] {
let tried = match resolve_all(path, their_label, &pieces, scope, model).await {
Ok((text, by_model)) => {
check_merged(path, &base, ours, theirs, &text).map(|()| Merged { text, by_model })
}
Ok((text, by_model)) => check_merged(path, &base, ours, theirs, &text)
.map(|()| Merged { text, by_model })
.map_err(Failed::from),
Err(e) => Err(e),
};
match tried {
Ok(m) => return Ok(m),
Err(e) => why.push(format!("{}: {e}", scope.words())),
Err(f) => {
why.push(format!("{}: {}", scope.words(), f.why));
refused_on.push(f.refused_hunk);
}
}
}
Err(Refused(why.join("; then ")))
// Both tries had the model's reply refused, on the same hunk: its two
// sides changed the same lines with incompatible intent.
let contested = match (&refused_on[0], &refused_on[1]) {
(Some(a), Some(b)) if a == b => Some(Contested {
path: path.to_string(),
ours: a.0.clone(),
theirs: a.1.clone(),
}),
_ => None,
};
Err(Refused {
why: why.join("; then "),
contested,
})
}

#[cfg(test)]
Expand Down Expand Up @@ -1101,8 +1172,25 @@ mod tests {
&mut m,
))
.unwrap_err();
assert!(err.0.contains("merging each conflicting hunk"));
assert!(err.0.contains("smaller pieces"));
assert!(err.why.contains("merging each conflicting hunk"));
assert!(err.why.contains("smaller pieces"));
// Both replies were refused on the same hunk: its two sides are
// contested.
let c = err.contested.expect("contested");
assert_eq!(c.ours, "b1\nkeep\nc1\n");
assert_eq!(c.theirs, "b2\nkeep\nc2\n");
// A model that never answered is CoderHelm's own problem.
let mut m = scripted(&[]);
let err = run(merge_text(
"x.txt",
Some(base),
theirs,
ours,
"main branch",
&mut m,
))
.unwrap_err();
assert_eq!(err.contested, None);
}

#[test]
Expand Down
5 changes: 4 additions & 1 deletion services/worker/src/passes/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,10 @@ async fn refuse_merge(
super::own_problems::alert(state, &msg.team_id, "", own.subject.code(), &own.text).await
}
}
merge3::Refused(text)
merge3::Refused {
why: text,
contested: why.contested.clone(),
}
}

/// A path both sides changed whose two versions are text files: merged
Expand Down
Loading