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
9 changes: 9 additions & 0 deletions docs/incidents/xl_file_modes_kept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# xl. `xl_file_modes_kept`

**What happened:** A pull request held an executable `scripts/deploy.sh` and a symbolic link. CoderHelm brought the base into it. The merge conflicted, so CoderHelm built the merged tree itself, and every file it took from the base was written as a plain file. The script lost its executable bit, and the pipeline that ran it failed with "permission denied". A later fix round that edited the script dropped the bit again.

**Why:** Every tree CoderHelm wrote through GitHub's API named each file it wrote as a plain file, whatever the file was: the batch write of a round, the merge commit of a conflicted merge of the base, and a reader that filled in a plain file's mode when GitHub's listing named none. Only GitHub's own merge, used when nothing conflicts, kept modes.

**The guarantee:** Every entry CoderHelm writes keeps the mode the path already has in the tree the write is based on. A new path takes the mode of the entry it came from, such as the other side of a merge, or the executable bit a sandbox build left on a file it generated. Only a new path with no source is a plain file. Text is never written over a symbolic link or a submodule: the write is refused and says why. A conflicted merge of the base lays whole entries from the base over the branch, and a path both sides changed takes its mode as git merges it. A commit that still changes a mode names each change in its message and in the run's log. The mode values live in one helper, and a scan of the source fails on any other place that types one. On the pull request watch sweep, each open pull request's CoderHelm commits of the last 30 days are read once. A mode one of them dropped is put back at the head in one commit named "Restore file modes CoderHelm dropped", moved only as a fast forward. What can't safely be put back (a path a person changed since, a link whose text changed, a submodule) is listed on the run page and in an internal alert.

**Where it's enforced:** `clients::git_mode::{archive_sources, for_content, overlay, merged, tree_entry, changes, with_trailer, dropped, repair}`. `sandbox::read_changed_tar` and `CodegenOutcome::executable`. `GitHubClient::{entries_at, batch_write, batch_write_from, create_merge_commit, write_file, put_tree}`, `GitEntry::from_json`. `pr::{resolve_conflicts, merge_plan}`, `restack::resolve`, `agent::tool_outcome::remark`, `mode_repair::{sweep, repair_one, plan}` and `pr_watch::sweep`.
12 changes: 12 additions & 0 deletions services/common/src/agent_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ impl ToolOutcome {
pub fn to_value(&self) -> Value {
serde_json::to_value(self).unwrap_or(Value::Null)
}
/// The outcome with lines the code under the tool added (a commit that
/// changed file modes) put first, so the summary's cap never cuts them.
pub fn with_remarks(self, remarks: &[String]) -> Self {
if remarks.is_empty() {
Comment thread
coderhelm[bot] marked this conversation as resolved.
Resolved
return self;
}
let mut parts: Vec<&str> = remarks.iter().map(String::as_str).collect();
if let Some(s) = self.summary.as_deref() {
parts.push(s);
}
Self::new(self.status, parts.join("; "))
}
}

/// A message the worker put into the conversation itself.
Expand Down
20 changes: 11 additions & 9 deletions services/worker/src/agent/anthropic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,14 +1150,15 @@ pub async fn converse_tool_loop(
let tool_start = std::time::Instant::now();
info!(tool = %tool_name, "Executing tool");

let (gated, noted) = super::tool_outcome::capture(super::policy::gated_execute(
gate,
tool_executor,
tool_name,
tool_input,
common::truncate_str(why.trim(), 500),
))
.await;
let (gated, noted, remarks) =
super::tool_outcome::capture(super::policy::gated_execute(
gate,
tool_executor,
tool_name,
tool_input,
common::truncate_str(why.trim(), 500),
))
.await;
let ran = match gated {
super::policy::Gated::Ran(r) => r,
super::policy::Gated::Refused(message) => {
Expand Down Expand Up @@ -1237,7 +1238,8 @@ pub async fn converse_tool_loop(
Some(o) if !is_error => o,
Some(o) if o.status == common::agent_log::OutcomeStatus::Error => o,
_ => common::agent_log::ToolOutcome::default_for(is_error),
};
}
.with_remarks(&remarks);
if let Some(o) = observer {
o.on_tool_ended(&super::llm::ToolEnded {
name: tool_name,
Expand Down
50 changes: 41 additions & 9 deletions services/worker/src/agent/tool_outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use common::agent_log::ToolOutcome;

tokio::task_local! {
static SLOT: RefCell<Option<ToolOutcome>>;
static REMARKS: RefCell<Vec<String>>;
}

/// Records the outcome of the tool call running now. Outside a call (a
Expand All @@ -22,13 +23,26 @@ pub fn note(outcome: ToolOutcome) {
let _ = SLOT.try_with(|s| *s.borrow_mut() = Some(outcome));
}

/// Adds a line to the outcome of the tool call running now, whatever its
/// tool notes: what the code under the tool did that the run's log must
/// show (a commit that changed file modes). Outside a call it does nothing.
pub fn remark(line: impl Into<String>) {
let line = line.into();
let _ = REMARKS.try_with(|r| r.borrow_mut().push(line));
}

/// Runs one tool call and returns what it returned with the outcome its
/// tool noted, if it noted one.
pub async fn capture<F: Future>(call: F) -> (F::Output, Option<ToolOutcome>) {
/// tool noted, if it noted one, and the remarks made under it.
pub async fn capture<F: Future>(call: F) -> (F::Output, Option<ToolOutcome>, Vec<String>) {
SLOT.scope(RefCell::new(None), async move {
let out = call.await;
let noted = SLOT.with(|s| s.borrow_mut().take());
(out, noted)
REMARKS
.scope(RefCell::new(Vec::new()), async move {
let out = call.await;
let noted = SLOT.with(|s| s.borrow_mut().take());
let remarks = REMARKS.with(|r| std::mem::take(&mut *r.borrow_mut()));
(out, noted, remarks)
})
.await
})
.await
}
Expand All @@ -39,7 +53,7 @@ mod tests {

#[tokio::test]
async fn a_noted_outcome_is_captured_and_the_last_note_wins() {
let (v, o) = capture(async {
let (v, o, _) = capture(async {
note(ToolOutcome::ok("first"));
tokio::task::yield_now().await;
note(ToolOutcome::soft_fail("NO edits applied"));
Expand All @@ -52,16 +66,34 @@ mod tests {

#[tokio::test]
async fn no_note_is_none_and_notes_never_leak_between_calls() {
let (_, o) = capture(async {}).await;
let (_, o, r) = capture(async {}).await;
assert!(r.is_empty());
assert_eq!(o, None);
// A nested call (a sub-agent's tool) keeps its own outcome.
let (_, outer) = capture(async {
let (_, inner) = capture(async { note(ToolOutcome::error("inner")) }).await;
let (_, outer, _) = capture(async {
let (_, inner, _) = capture(async { note(ToolOutcome::error("inner")) }).await;
assert_eq!(inner, Some(ToolOutcome::error("inner")));
})
.await;
assert_eq!(outer, None);
// Outside any call a note does nothing (and never panics).
note(ToolOutcome::ok("nowhere"));
remark("nowhere");
}

#[tokio::test]
async fn a_remark_is_kept_whatever_the_tool_notes_after_it() {
let (_, o, r) = capture(async {
remark("File modes changed: `run.sh`");
note(ToolOutcome::ok("Wrote run.sh"));
})
.await;
assert_eq!(o, Some(ToolOutcome::ok("Wrote run.sh")));
assert_eq!(r, vec!["File modes changed: `run.sh`".to_string()]);
let (_, outcome, _) = capture(async {}).await;
let merged = outcome
.unwrap_or_else(|| ToolOutcome::default_for(false))
.with_remarks(&r);
assert!(merged.summary.unwrap().contains("File modes changed"));
}
}
Loading