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
155 changes: 121 additions & 34 deletions services/worker/src/clients/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,15 @@ impl EgressBlocks {
const MARKER_CODEGEN_END: &str = "CODERHELM_CODEGEN_END exit=";
const MARKER_CODEGEN_UPLOADED: &str = "===CODERHELM_CODEGEN_UPLOADED===";
const MARKER_CODEGEN_UPLOAD_FAILED: &str = "===CODERHELM_CODEGEN_UPLOAD_FAILED===";
/// Caps on captured codegen output (regenerated files are large but bounded).
const MAX_CODEGEN_FILES: usize = 40;
const MAX_CODEGEN_FILE_BYTES: u64 = 5 * 1024 * 1024;
/// Caps on captured codegen output (regenerated files are large but
/// bounded). A generated types file for a large CMS schema passed the old
/// 5 MB cap and was dropped without a word: the run committed one of the two
/// files the generator changed and called it regenerated. A file past a cap
/// is now named in `CodegenOutcome::dropped`, never left out silently. The
/// size cap stays well under GitHub's 100 MB blob limit and the worker's
/// memory.
const MAX_CODEGEN_FILES: usize = 200;
const MAX_CODEGEN_FILE_BYTES: u64 = 50 * 1024 * 1024;

/// Result of a sandbox codegen run — the files the repo's codegen CHANGED,
/// captured for the worker to commit back to the branch (making the sandbox
Expand All @@ -270,6 +276,10 @@ pub struct CodegenOutcome {
pub passed: bool,
/// (path, content) for each file codegen created or modified.
pub changed: Vec<(String, String)>,
/// Files codegen changed that couldn't be brought back (too large, not
/// text, past the file cap), each with why. A run with any is not a
/// complete regeneration.
pub dropped: Vec<String>,
/// Tail of the codegen output.
pub output: String,
}
Expand All @@ -280,11 +290,61 @@ impl CodegenOutcome {
ran: false,
passed: false,
changed: Vec::new(),
dropped: Vec::new(),
output: reason.into(),
}
}
}

/// The files of a codegen build's changed-files archive: the text files to
/// commit, and the ones left out with why.
#[derive(Debug, Default, PartialEq)]
pub struct Captured {
pub files: Vec<(String, String)>,
pub dropped: Vec<String>,
}

/// Reads a changed-files archive (tar.gz, paths from the repository root).
pub fn read_changed_tar(
bytes: &[u8],
max_files: usize,
max_bytes: u64,
) -> Result<Captured, Box<dyn std::error::Error + Send + Sync>> {
let gz = flate2::read::GzDecoder::new(bytes);
let mut ar = tar::Archive::new(gz);
let mut out = Captured::default();
for entry in ar.entries()? {
let mut entry = entry?;
if entry.header().entry_type() != tar::EntryType::Regular {
continue;
}
let path = entry.path()?.to_string_lossy().replace('\\', "/");
let path = path.trim_start_matches("./").to_string();
if out.files.len() >= max_files {
out.dropped
.push(format!("`{path}` (more than {max_files} changed files)"));
continue;
}
if entry.size() > max_bytes {
warn!(path = %path, bytes = entry.size(), "codegen: changed file over the size cap");
out.dropped.push(format!(
"`{path}` ({} MB, over the {} MB cap)",
entry.size() / (1024 * 1024),
max_bytes / (1024 * 1024)
));
continue;
}
let mut content = String::new();
use std::io::Read;
if entry.read_to_string(&mut content).is_err() {
out.dropped.push(format!("`{path}` (not a text file)"));
continue;
}
out.files.push((path, content));
}
Ok(out)
}

/// Thin driver over CodeBuild + S3 + CloudWatch Logs. Holds only borrows from
/// `WorkerState`; construct it per pass.
pub struct SandboxClient<'a> {
Expand Down Expand Up @@ -657,8 +717,8 @@ impl<'a> SandboxClient<'a> {
// for builds that had ALREADY succeeded and uploaded the regenerated
// files, so the agent re-ran the (expensive, always-succeeding) codegen
// build over and over until the pass timed out. Check S3 first.
let changed = self.download_changed(out_key).await.unwrap_or_default();
if !changed.is_empty() {
let captured = self.download_changed(out_key).await.unwrap_or_default();
if !captured.files.is_empty() || !captured.dropped.is_empty() {
let output = self
.fetch_log(group, stream)
.await
Expand All @@ -667,7 +727,8 @@ impl<'a> SandboxClient<'a> {
return Ok(CodegenOutcome {
ran: true,
passed: true,
changed,
changed: captured.files,
dropped: captured.dropped,
output,
});
}
Expand Down Expand Up @@ -706,11 +767,12 @@ impl<'a> SandboxClient<'a> {
// not be dropped as "nothing to regenerate".
if printed(&full, MARKER_CODEGEN_UPLOADED) {
let retry = self.download_changed(out_key).await.unwrap_or_default();
if !retry.is_empty() {
if !retry.files.is_empty() || !retry.dropped.is_empty() {
return Ok(CodegenOutcome {
ran: true,
passed: true,
changed: retry,
changed: retry.files,
dropped: retry.dropped,
output,
});
}
Expand All @@ -725,16 +787,18 @@ impl<'a> SandboxClient<'a> {
ran: true,
passed,
changed: Vec::new(),
dropped: Vec::new(),
output,
})
}

/// Download the captured tar.gz of changed files and read them into
/// (path, content) pairs, bounded in count and per-file size.
/// (path, content) pairs, bounded in count and per-file size; what the
/// bounds leave out is named.
async fn download_changed(
&self,
out_key: &str,
) -> Result<Vec<(String, String)>, Box<dyn std::error::Error + Send + Sync>> {
) -> Result<Captured, Box<dyn std::error::Error + Send + Sync>> {
let obj = self
.s3
.get_object()
Expand All @@ -744,30 +808,7 @@ impl<'a> SandboxClient<'a> {
.await
.map_err(|e| format!("codegen output download failed: {e}"))?;
let bytes = obj.body.collect().await?.into_bytes();
let gz = flate2::read::GzDecoder::new(&bytes[..]);
let mut ar = tar::Archive::new(gz);
let mut out = Vec::new();
for entry in ar.entries()? {
let mut entry = entry?;
if entry.header().entry_type() != tar::EntryType::Regular {
continue;
}
if entry.size() > MAX_CODEGEN_FILE_BYTES {
warn!("codegen: skipping oversized changed file");
continue;
}
let path = entry.path()?.to_string_lossy().replace('\\', "/");
let mut content = String::new();
use std::io::Read;
if entry.read_to_string(&mut content).is_err() {
continue; // skip binary/non-utf8
}
out.push((path, content));
if out.len() >= MAX_CODEGEN_FILES {
break;
}
}
Ok(out)
read_changed_tar(&bytes, MAX_CODEGEN_FILES, MAX_CODEGEN_FILE_BYTES)
}

async fn build_and_read(
Expand Down Expand Up @@ -1406,6 +1447,52 @@ mod tests {
assert!(get.contains(&ttl) && put.contains(&ttl), "{get}");
}

/// The production layout: the generator runs in `apps/studio` and writes
/// `../web/src/lib/sanity/sanity.types.ts`; the build lists both changed
/// files from the repository root. Both come back, and a file over a cap
/// is named, never dropped silently.
#[test]
fn both_changed_files_come_back_and_a_dropped_one_is_named() {
fn archive(files: &[(&str, &[u8])]) -> Vec<u8> {
let mut b = tar::Builder::new(flate2::write::GzEncoder::new(
Vec::new(),
flate2::Compression::fast(),
));
for (p, data) in files {
let mut h = tar::Header::new_gnu();
h.set_size(data.len() as u64);
h.set_mode(0o644);
h.set_cksum();
b.append_data(&mut h, p, *data).unwrap();
}
b.into_inner().unwrap().finish().unwrap()
}
let types = vec![b'x'; 7 * 1024 * 1024];
let tgz = archive(&[
("apps/studio/schema.json", b"{}"),
("apps/web/src/lib/sanity/sanity.types.ts", &types),
]);
let got = read_changed_tar(&tgz, MAX_CODEGEN_FILES, MAX_CODEGEN_FILE_BYTES).unwrap();
let paths: Vec<&str> = got.files.iter().map(|(p, _)| p.as_str()).collect();
assert_eq!(
paths,
[
"apps/studio/schema.json",
"apps/web/src/lib/sanity/sanity.types.ts"
],
"the 7 MB types file passed the old 5 MB cap"
);
assert!(got.dropped.is_empty());
// Past a cap: named, with why.
let small = read_changed_tar(&tgz, MAX_CODEGEN_FILES, 5 * 1024 * 1024).unwrap();
assert_eq!(small.files.len(), 1);
assert_eq!(small.dropped.len(), 1);
assert!(small.dropped[0].contains("apps/web/src/lib/sanity/sanity.types.ts"));
assert!(small.dropped[0].contains("over the 5 MB cap"));
let one = read_changed_tar(&tgz, 1, MAX_CODEGEN_FILE_BYTES).unwrap();
assert!(one.dropped[0].contains("more than 1 changed files"));
}

#[test]
fn no_deadline_always_has_time() {
let c = aws_sdk_codebuild::Client::from_conf(
Expand Down
57 changes: 55 additions & 2 deletions services/worker/src/passes/completeness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,21 @@ pub enum Section {
ReviewFixes,
/// What is still open, at the bottom (#238's "Not yet verified").
OpenItems,
/// Planned work the check before the PR still found not done after its
/// fix round, at the top: the PR opens as a draft with it rather than
/// the run ending with no PR (see `proof::unfinished_section`).
Unfinished,
}

impl Section {
const ALL: [Section; 6] = [
const ALL: [Section; 7] = [
Section::Links,
Section::Mirrors,
Section::MergeOrder,
Section::NotInPr,
Section::ReviewFixes,
Section::OpenItems,
Section::Unfinished,
];

/// Its start and end markers.
Expand All @@ -150,6 +155,7 @@ impl Section {
super::review_fix::evidence::END,
),
Section::OpenItems => (super::proof::UNVERIFIED_START, super::proof::UNVERIFIED_END),
Section::Unfinished => (super::proof::UNFINISHED_START, super::proof::UNFINISHED_END),
}
}
}
Expand Down Expand Up @@ -183,6 +189,7 @@ pub struct Owned {
pub not_in_pr: String,
pub review_fixes: String,
pub open_items: String,
pub unfinished: String,
}

impl Owned {
Expand All @@ -194,6 +201,7 @@ impl Owned {
Section::NotInPr => &mut self.not_in_pr,
Section::ReviewFixes => &mut self.review_fixes,
Section::OpenItems => &mut self.open_items,
Section::Unfinished => &mut self.unfinished,
}
}

Expand Down Expand Up @@ -304,7 +312,8 @@ pub fn normalize_open_items(section: &str) -> String {
/// Changes, then the link and redirect checks, the blocks the change mirrors, the merge order and the
/// steps not in this PR (before the Risk heading, or before Verification
/// when there is no Risk), the writer's Risk and Verification, then Open
/// items at the bottom, above the footer. Empty sections are left out.
/// items at the bottom, above the footer; planned work not done, when any,
/// above everything. Empty sections are left out.
pub fn assemble(rest: &str, owned: &Owned) -> String {
let (main, footer) = split_footer(rest);
let main = main.trim();
Expand Down Expand Up @@ -346,6 +355,15 @@ pub fn assemble(rest: &str, owned: &Owned) -> String {
format!("{body}\n\n{open}")
};
}
// Planned work not done goes first: a reader sees it before anything.
let unfinished = owned.unfinished.trim();
if !unfinished.is_empty() {
body = if body.is_empty() {
unfinished.to_string()
} else {
format!("{unfinished}\n\n{body}")
};
}
format!("{body}{footer}")
}

Expand Down Expand Up @@ -1080,6 +1098,7 @@ mod tests {
fn the_description_is_put_together_in_its_order() {
let writer = "Closes #9\n\n**Summary** The report needs a reader.\n\n**Changes**\n* **Users** add `analytics_reader`\n\n**Risk** **Low**: one new user.\n\n**Verification**\n1. Run the plan.";
let owned = Owned {
unfinished: String::new(),
links: String::new(),
mirrors: mirrors_section(&[Mirror {
path: "envs/prod/users.tf".into(),
Expand Down Expand Up @@ -1133,6 +1152,40 @@ mod tests {
assert!(!is_heading("Risk is low", "risk"));
}

/// Planned work not done after the fix round: the PR opens as a draft
/// with it above everything, and a later rewrite keeps it there.
#[test]
fn unfinished_work_goes_at_the_top() {
let top = crate::passes::proof::unfinished_section(&[crate::passes::proof::Finding {
point: "planned_work".into(),
detail: "The hours block in apps/web/src/components/hours.tsx is not built.".into(),
..Default::default()
}]);
let owned = Owned {
unfinished: top.clone(),
open_items: crate::passes::proof::gaps_section(&[crate::passes::proof::Finding {
point: "impact".into(),
detail: "not counted".into(),
..Default::default()
}]),
..Default::default()
};
let body = assemble("Closes #4\n\n**Summary** x\n\n**Risk** Low", &owned);
assert!(
body.starts_with(crate::passes::proof::UNFINISHED_START),
"{body}"
);
assert!(body.contains("hours.tsx"));
assert!(
body.ends_with(crate::passes::proof::UNVERIFIED_END),
"{body}"
);
let (again, rest) = take_owned(&body);
assert_eq!(again.unfinished, top);
assert_eq!(assemble(&rest, &again), body);
assert!(crate::passes::proof::holds_unfinished(&body));
}

#[test]
fn a_top_warning_from_before_moves_to_the_bottom_as_open_items() {
let old = "<!-- coderhelm:unverified -->\n> [!WARNING]\n> **Not yet verified**\n> - **Impact not measured:** not counted. Settle: count them.\n> - **Waiting on CI:** lint did not run <!-- coderhelm:ci -->\n<!-- /coderhelm:unverified -->\n\nCloses #4\n\n**Summary** x\n\n**Risk** Low";
Expand Down
Loading
Loading