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.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pipeline still calls it there.
| bn. `bn_infra_list_follows_aws` | Security, Infrastructure kept ten rows while AWS had moved on: an RDS instance deleted two days before still showed two pending actions, a Lambda function on a retired runtime came from a Terraform module nothing called, five rows already fixed kept their Runbook and Fix PR buttons, an older CVE row and the engine patch to the same version stood as two rows, and a serverless platform update and an operating system update on each instance AWS listed never appeared. The read kept every resource for three days and the match took them all as running. | Each read says which kinds it saw in full; what a full read no longer lists is dropped and its findings close as removed, with the time; a failed or partial read drops and closes nothing. The same move on one database is one item. Every pending action is an item, applied by CoderHelm after one approval when it has no code change, and a version change on a resource in code is a pull request whichever source raised it. Closed items offer nothing to do. Opening the page refreshes a read older than five minutes, at most once per team in that time. |
| bp. `bp_repo_traced_not_asked` | A secrets finding on a production API's log group posted "Which repo should this fix go to?" with the right repo as its best guess, two others as "also possible", no fix started, and "there is no direct AWS pointer" as its reason. The task definition's image came from an ECR repository named for the app plus its environment word, tagged with the app's name and the commit it was built from, and pushed by the app repo's workflow; none of it counted, and the hedged answer was cached for a week. | The repo is traced (`common::log_repo`): the running image's commit is looked up in every repo's history, a workflow or build file that ships the image counts as shipping it while Terraform that only defines it is context, image repository and tag names count without their environment word, and the classes and text of the error's own lines are looked for in the code. Evidence for one repo starts the fix there, saves the repo and says why in one line; split evidence still starts on the best guess with one card offering "Wrong repo? Move it"; only a log group nothing ties to any repo asks. An older resolver's or an unsure answer is not served from the cache for long. |
| bq. `bq_upstream_read_not_guessed` | A card said "1 new log finding: Seen again: Request failed with 503 in AUTH GUARD" for two lines of an error the API logs most days, with "the evidence does not show which upstream service returned the 503", an AWS account id line and a generic link; nothing had read the guard's code, which names the service it calls, and each analysis sent every line of a burst to the model. | A sighting is judged against its own week first (`common::log_triage`, the analyzer's same cases): at its usual rate it is counted with no card and no fix, rising is act, new or back after a quiet week is said once. A failed call's code is read: the card names the upstream (host and setting), its timeout and retries, the lines, tasks and the week's rate, with the decision on the first line and no account id. A burst is one entry before any model call, known errors are counted without the model, and an hourly budget caps analyses. |
| br. `br_description_writer_upgrade` | After the description writer was upgraded (a final review in the one save, the repository's template as the structure), a pull request opened the day before kept its old text: six checks "Not run: still running when this was written", the file to read first and the way back said twice, filler and a raw page id. Its head had not moved and its checked text was unchanged, so its stamp said current and nothing rebuilt it. | The stamp names the writer version (`description::WRITER`, raised with every change to what a description says or how it is laid out); a description built by another writer, or before the version, is not current. The pull request watch's sweep catches up open pull requests CoderHelm opened, whatever the age of their run: a few per team each sweep (`pr_watch::CATCH_UP_PER_TEAM`), inside its GitHub budget, each rebuilt once and marked on its run. A description still wholly CoderHelm's is built again from the facts of the head alone: nothing of the old text is kept, not even in a fold, except a line that closes an issue. A description with no CoderHelm stamp, or whose checked text a person changed, is left as it is. |

## Adding a case

Expand Down
5 changes: 4 additions & 1 deletion services/worker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,15 @@ async fn handle_message_inner(state: Arc<WorkerState>, message: models::WorkerMe
// Finished runs' pull requests: a conflict with a moved base,
// checks that went red later, a reviewer verdict missing on the
// head, a merge whose deploy nobody watches. Each is picked
// back up by the mechanism it already has.
// back up by the mechanism it already has. Then a few open pull
// requests per team whose description an earlier writer built
// are rebuilt by this one.
let watch = as_sweep(who::SWEEP_PR_WATCH, passes::pr_watch::sweep(&state)).await;
info!(
candidates = watch.candidates,
looked_at = watch.looked_at,
acted = ?watch.acted,
descriptions_rebuilt = ?watch.caught_up,
"Pull request watch sweep done"
);
// Deploy counts that stopped between passes carry on without
Expand Down
205 changes: 187 additions & 18 deletions services/worker/src/passes/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub async fn head_moved(
m.number,
m.ticket_id,
true,
false,
Some(Reader { provider, usage }),
)
.await;
Expand Down Expand Up @@ -286,24 +287,63 @@ pub fn stamp_of(body: &str) -> Option<&str> {
/// The stamp's head and, when it carries one, the digest of the text the
/// fact check read.
fn stamp_parts(body: &str) -> Option<(&str, Option<&str>)> {
stamp_all(body).map(|(h, d, _)| (h, d))
stamp_all(body).map(|s| (s.head, s.digest))
}

/// The version of the description writer: what builds, lays out and reviews
/// a description (`description_facts`, `common::pr_layout`,
/// `common::pr_template`, `common::pr_review`). Raise it whenever any of
/// them changes what a description says or how it is laid out: every
/// description stamped by an earlier writer is then not current, and is
/// rebuilt (by the pull request watch's catch-up, [`catch_up`]) even when
/// its head has not moved.
pub const WRITER: u32 = 2;

/// The stamp's word for the writer version: `writer-<n>`.
const WRITER_WORD: &str = "writer-";

/// The stamp's word for a description built while checks on its head were
/// still running: it is not current until they end and it is rebuilt.
const CHECKS_RUNNING: &str = "checks-running";

/// The stamp's head, digest, and whether checks were running when it was
/// built.
fn stamp_all(body: &str) -> Option<(&str, Option<&str>, bool)> {
/// What a stamp says.
struct Stamp<'a> {
head: &'a str,
/// The digest of the text the fact check read (none on the oldest
/// stamps).
digest: Option<&'a str>,
/// Checks on the head were running when it was built.
running: bool,
/// The writer version that built it (none on stamps from before the
/// version was kept).
writer: Option<u32>,
}

/// The stamp's head, digest, whether checks were running when it was
/// built, and the writer version. A stamp from before any of these parses
/// with that part missing.
fn stamp_all(body: &str) -> Option<Stamp<'_>> {
let at = body.rfind(STAMP_START)? + STAMP_START.len();
let rest = &body[at..];
let inner = rest[..rest.find(STAMP_END)?].trim();
let mut parts = inner.split_whitespace();
let head = parts.next().filter(|h| !h.is_empty())?;
let digest = parts.next();
let running = parts.any(|p| p == CHECKS_RUNNING);
Some((head, digest, running))
let mut stamp = Stamp {
head,
digest: None,
running: false,
writer: None,
};
for (i, p) in parts.enumerate() {
if p == CHECKS_RUNNING {
stamp.running = true;
} else if let Some(n) = p.strip_prefix(WRITER_WORD) {
stamp.writer = n.parse().ok();
} else if i == 0 {
stamp.digest = Some(p);
}
}
Some(stamp)
}

/// Whether the description shows a check on its head as still running.
Expand Down Expand Up @@ -353,7 +393,7 @@ pub fn with_stamp(body: &str, head: &str) -> String {
String::new()
};
format!(
"{}\n\n{STAMP_START}{head} {digest}{running}{STAMP_END}",
"{}\n\n{STAMP_START}{head} {digest} {WRITER_WORD}{WRITER}{running}{STAMP_END}",
body.trim_end()
)
}
Expand All @@ -363,22 +403,56 @@ fn same_head(stamped: &str, head: &str) -> bool {
stamped == head || (stamped.len() >= 7 && head.starts_with(stamped))
}

/// Whether the description was built for `head`, the text the fact check
/// read is still the text that is there, and no check was running when it
/// was built (a description showing running checks is rebuilt until they
/// end). A stamp from before the digest answers for its head alone.
/// Whether the description was built for `head` by this writer
/// ([`WRITER`]), the text the fact check read is still the text that is
/// there, and no check was running when it was built (a description
/// showing running checks is rebuilt until they end). A stamp from before
/// the writer version is never current: an upgrade rebuilds it. A stamp
/// from before the digest answers for its head and writer alone.
pub fn is_current(body: &str, head: &str) -> bool {
match stamp_all(body) {
Some((h, digest, running)) => {
Some(s) => {
!head.is_empty()
&& !running
&& same_head(h, head)
&& digest.is_none_or(|d| d == checked_digest(body))
&& !s.running
&& s.writer == Some(WRITER)
&& same_head(s.head, head)
&& s.digest.is_none_or(|d| d == checked_digest(body))
}
None => false,
}
}

/// What the catch-up after an upgrade does with an open pull request's
/// description ([`catch_up`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CatchUp {
/// Built for its head by this writer: nothing to do.
Current,
/// CoderHelm's own text, built by an earlier writer or for another
/// head: rebuilt through the one rebuild ([`reconcile`]).
Rebuild,
/// Not CoderHelm's to rebuild here: no stamp (a description CoderHelm
/// never built), or a person changed the text the fact check read
/// since CoderHelm built it. Left as it is; the paths that move the
/// head still rebuild it.
Leave,
}

/// Whether the catch-up rebuilds `body` on `head`. Only a description that
/// carries CoderHelm's stamp and whose checked text is still what
/// CoderHelm wrote is rebuilt; a person's edit is never written over by a
/// sweep.
pub fn catch_up(body: &str, head: &str) -> CatchUp {
if is_current(body, head) {
return CatchUp::Current;
}
match stamp_all(body) {
None => CatchUp::Leave,
Some(s) if s.digest.is_some_and(|d| d != checked_digest(body)) => CatchUp::Leave,
Some(_) => CatchUp::Rebuild,
}
}

/// How a reconcile ended.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Reconciled {
Expand Down Expand Up @@ -406,7 +480,7 @@ pub async fn reconcile(
ticket_id: &str,
) -> Reconciled {
build(
state, github, team_id, owner, repo, number, ticket_id, false, None,
state, github, team_id, owner, repo, number, ticket_id, false, false, None,
)
.await
}
Expand All @@ -433,6 +507,7 @@ pub async fn reconcile_read(
number,
ticket_id,
true,
false,
Some(reader),
)
.await
Expand All @@ -450,7 +525,7 @@ pub async fn rebuild(
ticket_id: &str,
) -> Reconciled {
build(
state, github, team_id, owner, repo, number, ticket_id, true, None,
state, github, team_id, owner, repo, number, ticket_id, true, false, None,
)
.await
}
Expand Down Expand Up @@ -717,6 +792,9 @@ async fn build(
number: u64,
ticket_id: &str,
force: bool,
// The catch-up's rebuild ([`catch_up_run`]): a description wholly
// CoderHelm's is built again from the facts alone.
fresh: bool,
reader: Option<Reader<'_>>,
) -> Reconciled {
if number == 0 {
Expand All @@ -737,6 +815,11 @@ async fn build(
if !force && is_current(&body, &head) {
return Reconciled::Current;
}
// Read again here, as it is now: a person who edited it since the sweep
// looked is never written over.
if fresh && catch_up(&body, &head) != CatchUp::Rebuild {
return Reconciled::Current;
}
// Everything computed from the branch, for this head: a dependency
// pull request's table, counts, "What to check" and Proof, and the
// lockfile facts they are written from.
Expand All @@ -756,6 +839,7 @@ async fn build(
};
let (base, dependency) = match computed {
Some((b, d)) => (b, Some(d)),
None if fresh => (fresh_base(&body), None),
None => (body.clone(), None),
};
// The facts of this head, and the description as a view of them: every
Expand Down Expand Up @@ -817,6 +901,45 @@ async fn build(
}
}

/// The catch-up's rebuild of a run's pull request after an upgrade: when
/// the description is wholly CoderHelm's ([`catch_up`] says Rebuild), it is
/// built again from the facts of the head alone and nothing of the earlier
/// writer's text is kept ([`fresh_base`]), so no old line survives in a
/// fold. Anything else is left as it is.
pub async fn catch_up_run(
state: &WorkerState,
github: &GitHubClient,
team_id: &str,
run_id: &str,
owner: &str,
repo: &str,
number: u64,
) -> Reconciled {
let ticket = run_ticket(state, team_id, run_id).await;
build(
state, github, team_id, owner, repo, number, &ticket, false, true, None,
)
.await
}

/// What a description wholly CoderHelm's keeps when the catch-up builds it
/// again: only the lines that close an issue (`Closes #12`), which GitHub
/// acts on at merge. Everything else an earlier writer wrote (its lead,
/// its reviewer block, its frozen check rows) is dropped, and the body is
/// rendered from the facts of the head.
pub fn fresh_base(body: &str) -> String {
body.lines()
.map(str::trim)
.filter(|l| {
let l = l.to_lowercase();
["closes #", "fixes #", "resolves #"]
.iter()
.any(|k| l.starts_with(k))
})
.collect::<Vec<_>>()
.join("\n")
}

/// [`reconcile`] for a run's pull request, its ticket read from the run.
pub async fn reconcile_run(
state: &WorkerState,
Expand Down Expand Up @@ -1142,6 +1265,52 @@ mod tests {
);
}

/// The stamp names the writer that built the description: one from
/// before the version, or from another writer, is not current on an
/// unchanged head; this writer's is.
#[test]
fn a_stamp_names_its_writer_version() {
let body = "## Summary\n\nKept glob-walk on the 2 line.\n";
let head = "aaa111bbb222ccc";
let stamped = with_stamp(body, head);
assert!(
stamped.contains(&format!(" {WRITER_WORD}{WRITER} ")),
"{stamped}"
);
assert!(is_current(&stamped, head));
assert_eq!(catch_up(&stamped, head), CatchUp::Current);
// The stamp as the writer before the version wrote it: same head,
// same digest, no checks running, and no version.
let digest = checked_digest(body);
let old = format!(
"{}\n\n{STAMP_START}aaa111bbb222 {digest}{STAMP_END}",
body.trim_end()
);
assert_eq!(
stamp_of(&old),
Some("aaa111bbb222"),
"an old stamp still parses"
);
assert!(!is_current(&old, head), "no version: not current");
assert_eq!(catch_up(&old, head), CatchUp::Rebuild);
// An old stamp built while checks ran parses the same way.
let old_running = old.replace(STAMP_END, &format!(" {CHECKS_RUNNING}{STAMP_END}"));
let s = stamp_all(&old_running).unwrap();
assert!(s.running && s.writer.is_none() && s.digest == Some(digest.as_str()));
// Another writer version: not current either.
let other = stamped.replace(
&format!("{WRITER_WORD}{WRITER}"),
&format!("{WRITER_WORD}{}", WRITER - 1),
);
assert!(!is_current(&other, head));
assert_eq!(catch_up(&other, head), CatchUp::Rebuild);
// A body with no stamp is not CoderHelm's to rebuild in the
// catch-up, and one whose checked text a person changed is left.
assert_eq!(catch_up(body, head), CatchUp::Leave);
let edited = old.replace("Kept glob-walk", "We kept glob-walk, see the thread");
assert_eq!(catch_up(&edited, head), CatchUp::Leave);
}

/// Every place that rests on the description reconciles it first, and
/// a run never completes while stamp and head differ.
#[test]
Expand Down
Loading
Loading