From 237d697c76000d6f6fcfc43bbd26797fdd219106 Mon Sep 17 00:00:00 2001 From: Nam Rodriguez Date: Wed, 28 Oct 2026 23:21:36 -0400 Subject: [PATCH] Outside work is done by the run first; a note carries what is left, looked up, ordered and confirmable A reviewer found an "Also needed outside the code" note that said it could not look up the CMS content while the CMS read API was public, told editors to add an item that was already there without describing it, put content before the studio deploy that makes the new fields exist, gave no exact values or confirm query, mixed up the project id and the dataset, and handed every step to a person. - The run does the work first. Each outside step is decided in order and recorded with who does it: the change itself (a migration in the repository's migrations folder, fixtures or end-to-end setup, config), then CoderHelm after the PR opens (starting the studio/staging deploy or end-to-end workflow by workflow_dispatch or its label, running the confirm query), then a person only for what is left, with why CoderHelm couldn't (the missing permission or connection, named) and how to grant it. Results (run links, query output) go into the note. - outside_work reads what the repository offers: the CMS project id and dataset from its config, .env files or codegen environment (so the public query endpoint is derived), its deploy and end-to-end workflows and package scripts, its migrations and fixtures folders. - The planner gets query_content, a read-only query on that endpoint, and the repository's options. Its note is checked in code and sent back when it says it couldn't look while a readable source exists, quotes no lookup, leaves found items undescribed or adds a second one, has no ordered steps with owners, puts content before the schema deploy, gives a person a step the change or CoderHelm could do, gives a person a step with no reason, has content without exact values or a minimum, has no confirm query, or labels an identifier wrongly. - The ticket note and the PR's Not in this PR section render from the same steps; after the PR opens the note is posted once with results (edited in place on GitHub) and the PR section is updated with them. - The plan check counts outside steps: a person's step with its reason is listed as not done; one with no reason is listed as not covered. - content.rs looks existing blocks up through the public read API without a write connection, sets the least content from the spec's own counts, and every blocked line says why CoderHelm didn't and how to grant it. --- services/common/src/agent_policy/registry.rs | 1 + services/common/src/tool_registry.rs | 1 + services/worker/src/passes/feature/content.rs | 379 +++- services/worker/src/passes/feature/mod.rs | 29 +- services/worker/src/passes/fix_location.rs | 113 +- services/worker/src/passes/mod.rs | 127 +- services/worker/src/passes/outside_work.rs | 1766 +++++++++++++++++ services/worker/src/passes/plan.rs | 56 + services/worker/src/passes/plan_coverage.rs | 90 + 9 files changed, 2491 insertions(+), 71 deletions(-) create mode 100644 services/worker/src/passes/outside_work.rs diff --git a/services/common/src/agent_policy/registry.rs b/services/common/src/agent_policy/registry.rs index 2cb5765b5..e2af74512 100644 --- a/services/common/src/agent_policy/registry.rs +++ b/services/common/src/agent_policy/registry.rs @@ -62,6 +62,7 @@ pub const BUILTIN: &[(&str, Risk)] = &[ ("graph_impact", Risk::Read), ("check_package", Risk::Read), ("check_url", Risk::Read), + ("query_content", Risk::Read), ("count_lines", Risk::Read), // Data sources and connections, read only. ("live_describe", Risk::Read), diff --git a/services/common/src/tool_registry.rs b/services/common/src/tool_registry.rs index c3472873f..880c8d7da 100644 --- a/services/common/src/tool_registry.rs +++ b/services/common/src/tool_registry.rs @@ -68,6 +68,7 @@ pub const BUILTIN: &[(&str, Risk)] = &[ ("graph_impact", Risk::Read), ("check_package", Risk::Read), ("check_url", Risk::Read), + ("query_content", Risk::Read), ("count_lines", Risk::Read), // Data sources and connections, read only. ("live_describe", Risk::Read), diff --git a/services/worker/src/passes/feature/content.rs b/services/worker/src/passes/feature/content.rs index a5f852b11..76e1510af 100644 --- a/services/worker/src/passes/feature/content.rs +++ b/services/worker/src/passes/feature/content.rs @@ -22,8 +22,13 @@ //! blocks of that type already there ([`instance_queries`]): one in an //! older shape is to be replaced or updated, by its document and key, with //! the fields to set and to remove and the query that confirms it -//! ([`block_line`]). Without a connection to look with, the note says it -//! couldn't look instead of guessing. +//! ([`block_line`]). Without a write connection, the lookup goes through +//! the CMS's public read API when the repository names its project and +//! dataset (`outside_work::content_source`); only when neither can be read +//! does the note say it couldn't look, and why. +//! - every line that stops the run is a [`Blocked`]: what must change, why +//! CoderHelm couldn't do it (the missing connection, by name, or why it is +//! an editor's choice) and how to grant it so it can next time. use std::collections::BTreeSet; @@ -225,11 +230,7 @@ impl Need { ), }, Need::BlockOnPage { block, spec } => format!( - "add a `{block}` block to the staging page `{spec}` opens, so the new test finds \ - it. I couldn't look up existing staging content (no staging CMS connection for \ - this repository), so check first: if that page already has a `{block}` block, \ - update that one (set the new fields, remove the old ones) instead of adding a \ - second" + "add a `{block}` block to the staging page `{spec}` opens, so the new test finds it" ), } } @@ -291,6 +292,39 @@ pub struct BlockShape { pub fields: Vec, pub required: Vec, pub held_in: Vec<(String, String)>, + /// The fewest items the spec needs on the page to check anything + /// ([`min_items`]). + pub min_items: Option, +} + +/// The fewest items a spec's assertions need: `toBeGreaterThan(N)` needs +/// N + 1, `toBeGreaterThanOrEqual(N)` and `toHaveCount(N)` need N, `.nth(N)` +/// needs N + 1. The largest of them, or `None` when the spec has none. +pub fn min_items(spec: &str) -> Option { + let mut best: Option = None; + for (pat, extra) in [ + ("toBeGreaterThanOrEqual(", 0), + ("toBeGreaterThan(", 1), + ("toHaveCount(", 0), + (".nth(", 1), + ] { + let mut from = 0; + while let Some(i) = spec[from..].find(pat) { + let at = from + i + pat.len(); + from = at; + let digits: String = spec[at..] + .chars() + .take_while(char::is_ascii_digit) + .collect(); + if let Ok(n) = digits.parse::() { + let need = n + extra; + if need > 1 { + best = Some(best.map_or(need, |b| b.max(need))); + } + } + } + } + best } /// The fields a schema file declares after its type's own name, and which @@ -407,10 +441,29 @@ pub fn parse_instances(result: &Value) -> Vec { /// or update the block that is there (fields to set and to remove, and the /// query that confirms it), or add one when none is. pub fn block_line(block: &str, spec: &str, shape: &BlockShape, found: &[Existing]) -> String { + let least = shape + .min_items + .map(|n| { + format!( + ", with at least {n} items in it: `{spec}` checks that many, so fewer leave it \ + nothing to check" + ) + }) + .unwrap_or_default(); let Some(e) = found.first() else { return format!( - "add a `{block}` block to the staging page `{spec}` opens, so the new test finds it \ - (looked up on staging: no `{block}` block exists yet)" + "add a `{block}` block to the staging page `{spec}` opens, so the new test finds \ + it{least} (looked up on staging: no `{block}` block exists yet). Set {}", + if shape.fields.is_empty() { + "its required fields".to_string() + } else { + shape + .fields + .iter() + .map(|f| format!("`{f}`")) + .collect::>() + .join(", ") + } ); }; let set: Vec = shape @@ -465,9 +518,9 @@ pub fn block_line(block: &str, spec: &str, shape: &BlockShape, found: &[Existing String::new() }; format!( - "staging already has {target}{more} in an older shape: replace or update that one, do \ - not add a second. Set {}; remove {}. Confirm with the query `{verify}`: it returns the \ - block with the new fields", + "staging already has {target}{more} in an older shape: replace that one's content, do \ + not add a second{least}. Set {}; remove {}. Confirm with the query `{verify}`: it returns \ + the block with the new fields", list(&set), list(&remove) ) @@ -517,13 +570,71 @@ pub fn mutations(n: &Need, ids: &[String]) -> Vec { .collect() } +/// Content CoderHelm couldn't change itself: what, why, and how to grant +/// it the access so it can next time. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Blocked { + pub what: String, + pub why_person: String, + pub grant: String, +} + +impl Blocked { + pub fn line(&self) -> String { + format!( + "{} (why CoderHelm didn't: {}; so it can next time: {})", + self.what, self.why_person, self.grant + ) + } +} + +/// Why CoderHelm can't change staging content without a connection, and +/// how to give it one. +fn no_connection(repo: &str) -> (String, String) { + ( + format!("there is no staging CMS write connection for {repo}"), + format!( + "add one in Settings, Repositories, {repo}, Staging CMS (a staging-only write token); \ + CoderHelm then removes dropped fields and sets schema defaults on staging itself" + ), + ) +} + +/// Why a need is a person's even with a connection, and what would let +/// CoderHelm do it. +fn editors_choice(n: &Need) -> (String, String) { + match n { + Need::Require { field, .. } => ( + format!("the schema gives `{field}` no default, so its value is an editor's choice"), + format!( + "give `{field}` an `initialValue` in the schema and CoderHelm sets it on staging" + ), + ), + Need::BlockOnPage { spec, .. } => ( + "which page shows the block and what it says is an editor's choice".to_string(), + format!( + "have `{spec}` create its own content in its setup (a fixture or a seeded \ + document), so the test needs no editor" + ), + ), + Need::Unset { .. } => (String::new(), String::new()), + } +} + /// What happened to the staging content, for the description. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct Outcome { /// What was changed on staging, one line each. pub changed: Vec, - /// What must still change, one line each: stops the pull request. - pub blocking: Vec, + /// What must still change: stops the pull request. + pub blocking: Vec, +} + +impl Outcome { + /// The blocking lines, each with why and how to grant. + pub fn blocking_lines(&self) -> Vec { + self.blocking.iter().map(Blocked::line).collect() + } } /// Runs a read-only query on staging and returns its result. @@ -573,68 +684,141 @@ async fn affected( .collect()) } -/// Makes what can be made on staging and lists what can't. `conn`: the -/// repository's connection and its opened token, `None` without one. -/// `production_in_code`: datasets the repository's production settings -/// name, refused like production. +/// How staging content is read and written: the repository's write +/// connection, its CMS's public read API, or neither. +#[derive(Clone, Copy)] +pub struct Access<'a> { + /// `owner/name`, named in what to grant. + pub repo: &'a str, + /// The write connection and its opened token. + pub conn: Option<(&'a StagingCms, &'a str)>, + /// The public read API the repository's config names. + pub public: Option<&'a crate::passes::outside_work::ContentSource>, +} + +/// Looks up blocks of `block` already there and writes the line for it. +async fn block_need( + http: &reqwest::Client, + access: Access<'_>, + n: &Need, + block: &str, + spec: &str, + shapes: &[(String, BlockShape)], +) -> String { + let shape = shapes + .iter() + .find(|(b, _)| b == block) + .map(|(_, s)| s.clone()) + .unwrap_or_default(); + let mut found = Vec::new(); + let mut failed = None; + let mut looked = false; + for q in instance_queries(block, &shape) { + let r = match (access.conn, access.public) { + (Some((c, token)), _) => query(http, c, token, &q).await, + (None, Some(p)) => crate::passes::outside_work::public_query(http, p, &q).await, + (None, None) => break, + }; + looked = true; + match r { + Ok(v) => found.extend(parse_instances(&v)), + Err(e) => failed = Some(e), + } + } + let line = n.line(); + match failed { + _ if !looked => format!( + "{line}. I couldn't look up existing content: the repository names no CMS project id \ + and dataset in its config or codegen environment, and it has no staging CMS \ + connection. Check first: if that page already has a `{block}` block, replace that \ + one's content instead of adding a second" + ), + Some(e) if found.is_empty() => format!( + "{line}. The lookup for existing `{block}` blocks failed ({e}): check first, and \ + replace one that is there instead of adding a second" + ), + _ => block_line(block, spec, &shape, &found), + } +} + +/// Makes what can be made on staging and lists what can't, each with why +/// CoderHelm couldn't and how to grant it. `production_in_code`: datasets +/// the repository's production settings name, refused like production. pub async fn settle( http: &reqwest::Client, - conn: Option<(&StagingCms, &str)>, + access: Access<'_>, production_in_code: &[String], needs: &[Need], shapes: &[(String, BlockShape)], ) -> Outcome { let mut out = Outcome::default(); - let Some((c, token)) = conn else { - out.blocking = needs.iter().map(Need::line).collect(); + let Some((c, token)) = access.conn else { + let (why, grant) = no_connection(access.repo); + for n in needs { + let what = match n { + Need::BlockOnPage { block, spec } => { + block_need(http, access, n, block, spec, shapes).await + } + _ => n.line(), + }; + let (why, grant) = match n { + Need::Unset { .. } + | Need::Require { + default: Some(_), .. + } => (why.clone(), grant.clone()), + _ => editors_choice(n), + }; + out.blocking.push(Blocked { + what, + why_person: why, + grant, + }); + } return out; }; if let Err(e) = c .check_writable() .and_then(|_| c.check_against_code(production_in_code)) { - out.blocking = needs.iter().map(Need::line).collect(); - out.blocking.push(e); + for n in needs { + out.blocking.push(Blocked { + what: n.line(), + why_person: e.clone(), + grant: "point the repository's staging CMS connection at a staging dataset".into(), + }); + } return out; } for n in needs { // A block a spec needs: look for one already on staging first, so // the note says which to replace instead of asking for a second. if let Need::BlockOnPage { block, spec } = n { - let shape = shapes - .iter() - .find(|(b, _)| b == block) - .map(|(_, s)| s.clone()) - .unwrap_or_default(); - let mut found = Vec::new(); - let mut failed = None; - for q in instance_queries(block, &shape) { - match query(http, c, token, &q).await { - Ok(v) => found.extend(parse_instances(&v)), - Err(e) => failed = Some(e), - } - } - out.blocking.push(match failed { - Some(e) if found.is_empty() => format!( - "{} (the staging lookup failed: {e})", - n.line().replace( - "no staging CMS connection for this repository", - "the lookup failed" - ) - ), - _ => block_line(block, spec, &shape, &found), + let (why, grant) = editors_choice(n); + out.blocking.push(Blocked { + what: block_need(http, access, n, block, spec, shapes).await, + why_person: why, + grant, }); continue; } let Some(q) = n.automatic().then(|| affected_query(n)).flatten() else { - out.blocking.push(n.line()); + let (why, grant) = editors_choice(n); + out.blocking.push(Blocked { + what: n.line(), + why_person: why, + grant, + }); continue; }; let ids = match affected(http, c, token, &q).await { Ok(ids) => ids, Err(e) => { warn!(error = %e, "Could not read staging content"); - out.blocking.push(format!("{} ({e})", n.line())); + out.blocking.push(Blocked { + what: n.line(), + why_person: format!("reading staging failed: {e}"), + grant: "check the staging CMS connection's token can read the dataset".into(), + }); continue; } }; @@ -656,10 +840,16 @@ pub async fn settle( c.dataset, ids.iter().take(5).cloned().collect::>().join(", ") )), - Ok(r) => out - .blocking - .push(format!("{} (staging returned {})", n.line(), r.status())), - Err(e) => out.blocking.push(format!("{} ({e})", n.line())), + Ok(r) => out.blocking.push(Blocked { + what: n.line(), + why_person: format!("staging refused the change ({})", r.status()), + grant: "give the staging CMS connection's token write access".into(), + }), + Err(e) => out.blocking.push(Blocked { + what: n.line(), + why_person: format!("the staging write failed: {e}"), + grant: "retry; if it keeps failing, check the staging CMS connection".into(), + }), } } out @@ -772,6 +962,7 @@ mod tests { let shape = BlockShape { fields, required, + min_items: None, held_in: held_in( "promoBanner", &[ @@ -808,14 +999,51 @@ mod tests { // Nothing there: add, saying it was looked up. assert!(block_line("promoBanner", "e2e/promo.spec.ts", &shape, &[]) .contains("looked up on staging: no `promoBanner` block exists yet")); - // No connection: it says it couldn't look. - let n = Need::BlockOnPage { - block: "promoBanner".into(), - spec: "e2e/promo.spec.ts".into(), + // The spec's own count sets the least content. + let shape = BlockShape { + min_items: min_items( + "await expect(cards).toHaveCount(3)\nexpect(n).toBeGreaterThan(4)", + ), + ..shape + }; + assert_eq!(shape.min_items, Some(5)); + assert!( + block_line("promoBanner", "e2e/promo.spec.ts", &shape, &found) + .contains("with at least 5 items in it") + ); + assert_eq!(min_items("expect(x).toBeVisible()"), None); + } + + /// Neither a connection nor a public read API: it says it couldn't look + /// and why, and every blocked line says why CoderHelm didn't and how to + /// grant it. + #[tokio::test] + async fn without_any_access_the_note_says_why_and_how_to_grant() { + let n = vec![ + Need::BlockOnPage { + block: "promoBanner".into(), + spec: "e2e/promo.spec.ts".into(), + }, + Need::Require { + doc_type: "hero".into(), + field: "cta".into(), + default: None, + }, + ]; + let access = Access { + repo: "o/r", + conn: None, + public: None, }; - assert!(n - .line() - .contains("couldn't look up existing staging content")); + let out = settle(&reqwest::Client::new(), access, &[], &n, &[]).await; + assert!(out.blocking[0].what.contains( + "I couldn't look up existing content: the repository names no CMS project id" + )); + assert!(out.blocking[0].grant.contains("create its own content")); + assert!(out.blocking[1].why_person.contains("no default")); + assert!( + out.blocking_lines()[1].contains("so it can next time: give `cta` an `initialValue`") + ); } #[tokio::test] @@ -824,12 +1052,23 @@ mod tests { doc_type: "hero".into(), field: "subtitle".into(), }]; - let out = settle(&reqwest::Client::new(), None, &[], &n, &[]).await; + let access = Access { + repo: "o/r", + conn: None, + public: None, + }; + let out = settle(&reqwest::Client::new(), access, &[], &n, &[]).await; assert!(out.changed.is_empty()); assert_eq!( - out.blocking, - vec!["remove `subtitle` from every `hero` document that still has it"] + out.blocking[0].what, + "remove `subtitle` from every `hero` document that still has it" ); + assert!(out.blocking[0] + .why_person + .contains("no staging CMS write connection for o/r")); + assert!(out.blocking[0] + .grant + .contains("Settings, Repositories, o/r, Staging CMS")); } #[tokio::test] @@ -845,23 +1084,31 @@ mod tests { doc_type: "hero".into(), field: "subtitle".into(), }]; - let out = settle(&reqwest::Client::new(), Some((&c, "t")), &[], &n, &[]).await; + let access = |c| Access { + repo: "o/r", + conn: Some((c, "t")), + public: None, + }; + let out = settle(&reqwest::Client::new(), access(&c), &[], &n, &[]).await; assert!(out.changed.is_empty()); - assert!(out.blocking.iter().any(|b| b.contains("only to staging"))); + assert!(out + .blocking_lines() + .iter() + .any(|b| b.contains("only to staging"))); let c2 = StagingCms { dataset: "copy".into(), - ..c + ..c.clone() }; let out = settle( &reqwest::Client::new(), - Some((&c2, "t")), + access(&c2), &["copy".into()], &n, &[], ) .await; assert!(out - .blocking + .blocking_lines() .iter() .any(|b| b.contains("production settings"))); } diff --git a/services/worker/src/passes/feature/mod.rs b/services/worker/src/passes/feature/mod.rs index ebccaed7a..1c45a1785 100644 --- a/services/worker/src/passes/feature/mod.rs +++ b/services/worker/src/passes/feature/mod.rs @@ -717,15 +717,38 @@ async fn content_outcome( fields, required, held_in: content::held_in(block, &schemas), + min_items: specs + .iter() + .filter(|(p, _)| { + matches!(n, content::Need::BlockOnPage { spec, .. } if spec == p) + }) + .find_map(|(_, t)| content::min_items(t)), }, )) } _ => None, }) .collect(); + // Without a write connection, existing content is still looked up + // through the CMS's public read API when the repository names it. + let env = super::load_codegen_env(w.state, &w.msg.team_id, owner, repo).await; + let mut cfg_files = Vec::new(); + for p in super::outside_work::files_to_read(tree) + .into_iter() + .filter(|p| !p.starts_with(".github/") && !p.ends_with("package.json")) + { + if let Ok(t) = w.github.read_file(owner, repo, &p, base).await { + cfg_files.push((p, t)); + } + } + let public = super::outside_work::content_source(&cfg_files, &env); let out = content::settle( &w.state.http, - conn.as_ref().map(|(c, t)| (c, t.as_str())), + content::Access { + repo: &full, + conn: conn.as_ref().map(|(c, t)| (c, t.as_str())), + public: public.as_ref(), + }, &prod, &needs, &shapes, @@ -741,7 +764,7 @@ async fn content_outcome( } /// The repository's staging CMS connection with its opened token. -async fn load_connection( +pub(crate) async fn load_connection( state: &WorkerState, team_id: &str, repo: &str, @@ -923,7 +946,7 @@ pub async fn gate( } else { format!(" (already changed: {})", c.changed.join("; ")) }, - c.blocking.join("; ") + c.blocking_lines().join("; ") )); } facts.registrations_checked = places; diff --git a/services/worker/src/passes/fix_location.rs b/services/worker/src/passes/fix_location.rs index 0af9cfd02..825a4ec9a 100644 --- a/services/worker/src/passes/fix_location.rs +++ b/services/worker/src/passes/fix_location.rs @@ -198,6 +198,24 @@ pub struct OutsideChange { /// looked up. Empty when nothing was looked up. #[serde(default)] pub existing: String, + /// The work outside the code, in the order it must happen, each with who + /// does it (`this_pr`, `coderhelm`, `person`) and, for a person, why + /// CoderHelm couldn't and how to grant it (see `outside_work`). + #[serde(default)] + pub steps: Vec, + /// The lookup behind the note: query, endpoint, result or error. + #[serde(default)] + pub lookup: Option, + /// Items already there that the work replaces. + #[serde(default)] + pub existing_items: Vec, + /// How to confirm: the query and the result it must return, the page, + /// the end-to-end test. + #[serde(default)] + pub confirm: Option, + /// Identifiers the note uses, each with its label. + #[serde(default)] + pub names: Vec, } /// Words that make a change add content. @@ -691,6 +709,10 @@ List every step the ticket needs to work in production, not only this repository - permissions and read grants (roles, policies, grants on tables or schemas); - the handoff to whoever uses it (who is told, what they get, where it is written down); - the deploy and merge order (which repository merges first, what deploys when). +A run does everything the ticket needs; work outside the code is the exception. Decide each piece in this order, and record it in `outside_change.steps`: +1. The change itself does it: a task in tasks.md (a content or data migration in the repository's migrations folder that replaces the existing item by key, test fixtures or end-to-end setup that create their own content, config or registration). Then it is not an outside step at all, or a `by: this_pr` step. +2. CoderHelm does it after the pull request opens with access it has (`by: coderhelm`): starting the repository's studio/staging deploy workflow or adding the label its CI deploys on, re-running the end-to-end workflow, running the confirm query. Its results (run link, query output) are reported on the PR and the ticket. +3. Only what is left goes to a person (`by: person`): production content, an action that needs a person's approval, access CoderHelm lacks. Each such step says why (`why_person`, naming the missing permission or connection exactly) and how to grant it (`grant`). A person step without both is sent back, and the plan check counts it as not covered. Each step says who does it: `this_pr` (this change), `other_repo` (a pull request in another repository, named in `repo`), `person` (a person or team, named in `owner`) or `vendor` (an outside company, named in `owner`). When the plan spans several repositories, order the `## owner/repo` groups in tasks.md in the order they must merge. After the four openspec files, output this block (JSON, all fields required; `outside_change` is null for `code`): @@ -714,7 +736,12 @@ After the four openspec files, output this block (JSON, all fields required; `ou "change": "exactly what to change, naming the record(s)", "where": "who can make it, or where in that system", "verify": "how to confirm afterwards (for example: after the next sync, the item shows on the site); for content, the API query that shows it and what it returns", - "existing": "when the change adds content (a block, an entry, a record): look up content of that kind in the environment the tests use first (the team's connected data sources or CMS API), and write what came back by id and key. If one exists, `change` says to REPLACE or update that exact one (fields to set, fields to remove), never to add a second. If you could not look, say why here. Empty only when the change adds no content" + "existing": "when the change adds content (a block, an entry, a record): look up content of that kind in the environment the tests use first (the team's connected data sources or CMS API), and write what came back by id and key. If one exists, `change` says to REPLACE or update that exact one (fields to set, fields to remove), never to add a second. If a lookup failed, quote its error here. Empty only when the change adds no content", + "lookup": {"endpoint": "the query endpoint you used (the CMS read API listed in your instructions, or the data source)", "query": "the exact read query", "result": "what came back, quoted", "error": "only when the attempt failed: the error, quoted"}, + "existing_items": [{"id": "document id", "key": "array key for a block inside a document", "title": "its title", "old_fields": ["fields the new schema doesn't know"], "missing_fields": ["new required fields it lacks"], "left_by": "the earlier PR or branch that left it (from the ticket's earlier PRs or git history), or where you searched"}], + "steps": [{"step": "each piece of work outside the code, in the order it must happen (a schema change: the studio/schema deploy first, then content)", "by": "this_pr | coderhelm | person (decide in that order, see below)", "owner": "who does it (the engineer merging, a content editor)", "how": "the workflow, command or screen", "values": ["exact field = value, reference targets, tab definitions with their settings"], "remove": ["fields to remove"], "minimum": "the least content the feature and its tests need (for example: at least one tab with more than N cards, N from the component and test code)", "why_person": "for a person only: the exact permission or connection CoderHelm lacks, or why a person must decide", "grant": "for a person only: how to give CoderHelm that access so it can do it next time"}], + "confirm": {"query": "the exact read query", "expect": "the result shape (exactly one item, count of tabs >= 2, reference set, old fields not defined)", "page": "the page to look at", "e2e": "the end-to-end test that must run and pass, not skip"}, + "names": [{"label": "CMS project id | dataset | environment", "value": "the value, never under another's label"}] }, "history_check": "only when the prompt has a ticket history: each earlier pull request by number, what you reuse from it, what changes this time and why (and for one closed without a reason in its comments: who closed it and when); otherwise empty", "questions": ["each question only a person can answer, one decision each; [] when there are none"], @@ -855,6 +882,18 @@ pub const NOT_LOOKED_UP: &str = "I didn't look up whether content of this kind a instead of adding a second."; fn push_change(out: &mut String, c: &OutsideChange) { + // A note with its steps renders from them: the same steps the pull + // request lists under Not in this PR. + if !c.steps.is_empty() { + out.push_str(&format!("- **Where:** {}", c.system.trim())); + if !c.where_.trim().is_empty() { + out.push_str(&format!(" ({})", c.where_.trim())); + } + out.push('\n'); + out.push_str(&format!("- **What:** {}\n", one_line(&c.change))); + out.push_str(&super::outside_work::note_markdown(c)); + return; + } out.push_str(&format!("- **Where:** {}", c.system.trim())); if !c.where_.trim().is_empty() { out.push_str(&format!(" ({})", c.where_.trim())); @@ -1093,6 +1132,24 @@ pub fn not_in_pr_steps(d: Option<&FixDecision>, repo: &str) -> Vec<(String, Stri .map(|s| (one_line(&s.step), s.owner_label())) .collect(); if d.effective_location() == FixLocation::Both { + // The note's own steps, rendered as the ticket note renders them. + if let Some(c) = d + .outside_change + .as_ref() + .filter(|c| c.is_usable() && !c.steps.is_empty()) + { + let mut lines = super::outside_work::context_lines(c) + .into_iter() + .map(|l| (l, "found by CoderHelm".to_string())) + .collect::>(); + lines.extend(super::outside_work::pr_lines(c)); + for l in lines { + if !out.iter().any(|(s, _)| s == &l.0) { + out.push(l); + } + } + return out; + } if let Some(c) = d.outside_change.as_ref().filter(|c| c.is_usable()) { let step = one_line(&format!("{}: {}", c.system.trim(), c.change.trim())); if !out.iter().any(|(s, _)| s == &step) { @@ -1750,4 +1807,58 @@ mod intent_tests { assert!(unknown_report(&open, false) .contains("**Not yet settled in this plan**\n- [env_unproven]")); } + + /// The ticket note and the PR's Not in this PR come from the same + /// steps: every line the PR lists is in the ticket note, word for word. + #[test] + fn the_ticket_note_and_the_pr_carry_the_same_steps() { + use super::super::outside_work::{Confirm, ContentLookup, NoteStep}; + let d = FixDecision { + fix_location: FixLocation::Both, + outside_change: Some(OutsideChange { + system: "the CMS".into(), + change: "Replace the hero block's content on the events page".into(), + lookup: Some(ContentLookup { + endpoint: "https://p1.api.sanity.io/v2021-10-21/data/query/staging".into(), + query: "*[_type == \"page\"]{_id}".into(), + result: "[{\"_id\":\"page-events\"}]".into(), + error: String::new(), + }), + steps: vec![ + NoteStep { + step: "Deploy the studio to staging".into(), + by: "coderhelm".into(), + result: "started `Deploy studio`: https://example.test/run/1".into(), + done: true, + ..Default::default() + }, + NoteStep { + step: "Replace the hero block's content on production".into(), + by: "person".into(), + owner: "a content editor".into(), + values: vec!["title = Classes".into()], + why_person: "CoderHelm never writes production content".into(), + grant: "none: production stays with editors".into(), + ..Default::default() + }, + ], + confirm: Some(Confirm { + query: "count(*[_type == \"hero\"])".into(), + expect: "exactly 1".into(), + ..Default::default() + }), + ..Default::default() + }), + ..Default::default() + }; + let ticket = also_outside_section(Some(&d)); + let pr = not_in_pr_steps(Some(&d), "o/r"); + assert!(pr.len() >= 4, "{pr:?}"); + for (line, _) in &pr { + assert!(ticket.contains(line.as_str()), "{line}\n---\n{ticket}"); + } + assert!(ticket.contains("1. Deploy the studio to staging Done by CoderHelm: started `Deploy studio`: https://example.test/run/1.")); + assert!(ticket.contains("(owner: a content editor)")); + assert!(!ticket.contains("Not checked")); + } } diff --git a/services/worker/src/passes/mod.rs b/services/worker/src/passes/mod.rs index 687c0fe21..5b4b65721 100644 --- a/services/worker/src/passes/mod.rs +++ b/services/worker/src/passes/mod.rs @@ -725,6 +725,7 @@ pub mod model_check; pub mod onboard; pub mod open_pr_choice; pub mod ops_tools; +pub mod outside_work; mod plan; pub mod plan_chat; pub(crate) mod plan_coverage; @@ -2280,7 +2281,9 @@ async fn run_passes( } } if fix_route == fix_location::Route::CodeAndOutside { - if let Some(decision) = plan_result.fix.as_ref() { + // A note with steps CoderHelm does after the pull request opens is + // posted once, then, with their results (see after_pr_outside_work). + if let Some(decision) = plan_result.fix.as_ref().filter(|d| !has_coderhelm_steps(d)) { report_also_outside(state, msg, run_id, &github, decision).await; } } else if fix_route == fix_location::Route::Code { @@ -4582,6 +4585,20 @@ async fn run_passes( } // Tasks the plan marks optional and the change left out, named. gaps.extend(plan_optional); + // What the ticket needs that this run didn't do: a person's step (with + // why and how to grant it), or one handed out with no reason (not + // covered). + let outside_open = plan_coverage::outside_findings(plan_result.fix.as_ref()); + if !outside_open.is_empty() { + add_progress_note( + state, + &msg.team_id, + run_id, + &plan_coverage::outside_note(plan_result.fix.as_ref()), + ) + .await; + } + gaps.extend(outside_open); // Security issues not fixed yet, or a check that didn't run. gaps.extend(security_items); // Every redirect and link the change adds or changes, checked live; a @@ -4710,6 +4727,22 @@ async fn run_passes( ) .await; info!(run_id, pr_url = %pr_result.pr_url, "Draft PR created"); + // The outside steps CoderHelm does itself (a deploy, the end-to-end + // run, the confirm query) run now; the ticket and the PR report results. + if fix_route == fix_location::Route::CodeAndOutside { + if let Some(decision) = plan_result.fix.as_mut() { + after_pr_outside_work( + state, + msg, + run_id, + &github, + decision, + &branch_name, + pr_result.pr_number, + ) + .await; + } + } // The plan moved the change out of a repo an earlier run of this ticket // opened a PR in: that PR is closed with a note naming this one. supersede_moved_prs( @@ -6073,6 +6106,98 @@ async fn report_also_outside( .await; } +/// Whether the decision's outside note has steps CoderHelm does itself. +fn has_coderhelm_steps(d: &fix_location::FixDecision) -> bool { + d.outside_change.as_ref().is_some_and(|c| { + c.steps + .iter() + .any(|s| s.doer() == outside_work::Doer::CoderHelm) + }) +} + +/// After the pull request opens: does the outside steps that are +/// CoderHelm's (starting the deploy or end-to-end workflow, running the +/// confirm query) and reports their results once, on the ticket (the same +/// notice, edited on GitHub) and in the PR's Not in this PR section, both +/// rendered from the same steps. +#[allow(clippy::too_many_arguments)] +async fn after_pr_outside_work( + state: &WorkerState, + msg: &TicketMessage, + run_id: &str, + github: &GitHubClient, + decision: &mut fix_location::FixDecision, + branch: &str, + pr_number: u64, +) { + if !has_coderhelm_steps(decision) { + return; + } + let repo = format!("{}/{}", msg.repo_owner, msg.repo_name); + let env = load_codegen_env(state, &msg.team_id, &msg.repo_owner, &msg.repo_name).await; + let staging_write = feature::load_connection(state, &msg.team_id, &repo) + .await + .is_some(); + let abilities = outside_work::load( + github, + &msg.repo_owner, + &msg.repo_name, + branch, + &env, + staging_write, + ) + .await; + if let Some(c) = decision.outside_change.as_mut() { + outside_work::perform( + github, + &state.http, + &msg.repo_owner, + &msg.repo_name, + branch, + pr_number, + &abilities, + c, + ) + .await; + let done = c.steps.iter().filter(|s| s.done).count(); + add_progress_note( + state, + &msg.team_id, + run_id, + &format!( + "Outside steps CoderHelm did after the PR opened: {done} of {}", + c.steps + .iter() + .filter(|s| s.doer() == outside_work::Doer::CoderHelm) + .count() + ), + ) + .await; + } + report_also_outside(state, msg, run_id, github, decision).await; + // The PR lists the same steps, with the same results. + if let Ok(pr) = github + .get_pull_request(&msg.repo_owner, &msg.repo_name, pr_number) + .await + { + let body = pr["body"].as_str().unwrap_or(""); + let (mut owned, rest) = completeness::take_owned(body); + owned.not_in_pr = completeness::for_people_section( + &fix_location::not_in_pr_steps(Some(decision), &repo), + &decision.questions, + ); + let updated = completeness::assemble(&rest, &owned); + if updated != body { + if let Err(e) = github + .update_pr_body(&msg.repo_owner, &msg.repo_name, pr_number, &updated) + .await + { + warn!(run_id, error = %e, "Could not update the PR's Not in this PR section"); + } + } + } +} + /// Notice key: the part of the fix outside the code, one comment per ticket. const NOTICE_ALSO_OUTSIDE: &str = "also_outside"; /// Notice key prefix: one question the plan asks, by its compared text. diff --git a/services/worker/src/passes/outside_work.rs b/services/worker/src/passes/outside_work.rs new file mode 100644 index 000000000..3b00730f3 --- /dev/null +++ b/services/worker/src/passes/outside_work.rs @@ -0,0 +1,1766 @@ +//! Work a ticket needs beyond the code: done by the run, not handed out. +//! +//! A run should do everything the ticket needs. Before a step goes to a +//! person, it is decided in this order, in code, with the reason recorded +//! on the step ([`Doer`]): +//! +//! 1. the change itself does it (`this_pr`): a content or data migration in +//! the repository's migrations folder that replaces the existing item by +//! key, test fixtures or end-to-end setup that create their own content, +//! config or registration files; +//! 2. CoderHelm does it after the pull request opens, with access it already +//! has (`coderhelm`): dispatching the repository's studio or staging +//! deploy workflow (or adding the label its CI deploys on), re-running the +//! end-to-end workflow, running the confirm query ([`perform`]). The +//! results (the run link, the query output) go into the note; +//! 3. a person (`person`) only for what is left: production content, an +//! action that needs a person's approval, access CoderHelm doesn't have. +//! Such a step names why CoderHelm couldn't (`why_person`) and how to +//! grant it so it can next time (`grant`). +//! +//! What the repository offers is read from its files ([`Abilities`]): the +//! CMS project and dataset its config or codegen environment names (so the +//! public read endpoint is known and the note's lookups can be made and +//! quoted), its deploy workflows and scripts, its migrations and fixtures +//! folders. [`gaps`] checks a planner's note against them: a note that says +//! it couldn't look while a readable source exists, a step without an owner +//! or an order that puts content before the deploy that makes its fields +//! exist, a person step the change or CoderHelm could do, a person step with +//! no reason, content without exact values, a confirm without a query. +//! Those go back to the planner. The ticket note and the pull request's +//! "Not in this PR" section are both rendered from the same steps +//! ([`note_markdown`], [`pr_lines`]). + +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +use super::fix_location::OutsideChange; + +/// The read API version the derived query endpoint uses. +pub const CMS_API_VERSION: &str = "v2021-10-21"; +/// Bytes of a query result quoted in a note. +const RESULT_QUOTE: usize = 600; + +// ── What the repository lets a run do itself ──────────────────────────────── + +/// A CMS whose read API the run can query: the project and dataset the +/// repository's config or codegen environment names. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct ContentSource { + pub project_id: String, + pub dataset: String, + /// Where the values came from (a file path, or "codegen environment"). + pub origin: String, +} + +impl ContentSource { + /// The project's public query endpoint. + pub fn query_url(&self) -> String { + format!( + "https://{}.api.sanity.io/{CMS_API_VERSION}/data/query/{}", + self.project_id, self.dataset + ) + } + + /// The labelled names a note uses: never one value under another's name. + pub fn labels(&self) -> String { + format!( + "CMS project id `{}`, dataset `{}` (from {})", + self.project_id, self.dataset, self.origin + ) + } +} + +/// How a deploy (or an end-to-end run) is started. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Trigger { + /// A workflow with `workflow_dispatch`: CoderHelm can start it. + Dispatch, + /// A workflow that runs when this label is added to a pull request: + /// CoderHelm can add it. + Label(String), + /// A workflow that runs on a push or merge: it runs when the PR merges. + OnMerge, + /// A package script a person runs (`npm run deploy`). + Script, +} + +/// A workflow or script that deploys the CMS studio or schema, or runs the +/// end-to-end tests. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Route { + /// The workflow's `name:` or the script's name. + pub name: String, + /// The workflow file (`.github/workflows/x.yml`) or the package file. + pub file: String, + /// The command it runs (`sanity deploy`, `npm run deploy:studio`). + pub command: String, + pub trigger: Trigger, +} + +impl Route { + /// The route in a note: its name, file and how it starts. + pub fn line(&self) -> String { + let how = match &self.trigger { + Trigger::Dispatch => "started with workflow_dispatch".to_string(), + Trigger::Label(l) => format!("runs when the `{l}` label is added to the PR"), + Trigger::OnMerge => "runs on merge".to_string(), + Trigger::Script => format!("run `{}`", self.command), + }; + format!("`{}` ({}, {how})", self.name, self.file) + } + + /// Whether CoderHelm can start it itself. + pub fn startable(&self) -> bool { + matches!(self.trigger, Trigger::Dispatch | Trigger::Label(_)) + } + + /// Whether a step's text names this route. + fn named_in(&self, text: &str) -> bool { + let t = text.to_lowercase(); + let file = self.file.rsplit('/').next().unwrap_or(&self.file); + [self.name.as_str(), file, self.command.as_str()] + .iter() + .filter(|s| s.len() >= 3) + .any(|s| t.contains(&s.to_lowercase())) + } +} + +/// What the repository lets a run do itself. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Abilities { + pub source: Option, + /// Studio or schema deploys, staging first. + pub deploys: Vec, + /// Workflows that run the end-to-end tests. + pub e2e: Vec, + /// The folder content or data migrations live in. + pub migrations_dir: Option, + /// The folder end-to-end fixtures or setup live in. + pub fixtures_dir: Option, + /// The repository has a staging CMS write connection CoderHelm may use. + pub staging_write: bool, +} + +fn valid_name(s: &str) -> bool { + !s.is_empty() + && s.len() <= 64 + && s.chars() + .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') +} + +fn unquote(s: &str) -> &str { + s.trim() + .trim_end_matches([',', ';']) + .trim() + .trim_matches(|c| c == '\'' || c == '"' || c == '`') +} + +/// The value of `key: value` or `key=value` on a line, when the line sets +/// that key (case-insensitive key match on the given names). +fn value_of<'a>(line: &'a str, keys: &[&str]) -> Option<&'a str> { + let l = line.trim().trim_start_matches("export "); + let (k, v) = l.split_once(['=', ':'])?; + let k = k.trim().trim_matches(|c| c == '\'' || c == '"'); + keys.iter() + .any(|want| k.eq_ignore_ascii_case(want)) + .then(|| unquote(v)) +} + +fn is_project_key(k: &str) -> bool { + let u = k.to_ascii_uppercase(); + u.contains("SANITY") && u.contains("PROJECT") && u.ends_with("ID") +} + +fn is_dataset_key(k: &str) -> bool { + let u = k.to_ascii_uppercase(); + u.contains("SANITY") && u.ends_with("DATASET") +} + +/// The CMS the repository reads, from its codegen environment (`env`) and +/// its files (`.env*` other than production ones, `sanity.config.*`, +/// `sanity.cli.*`). A dataset named for staging is preferred, since the +/// end-to-end tests read staging. `None` when no project id is named. +pub fn content_source( + files: &[(String, String)], + env: &[(String, String)], +) -> Option { + let mut project: Option<(String, String)> = None; + let mut datasets: Vec<(String, String)> = Vec::new(); + let mut env_map: Vec<(String, String)> = env.to_vec(); + for (k, v) in env { + if is_project_key(k) && valid_name(v) && project.is_none() { + project = Some((v.clone(), "the codegen environment".into())); + } + if is_dataset_key(k) && valid_name(v) { + datasets.push((v.clone(), format!("the codegen environment ({k})"))); + } + } + for (path, text) in files { + let base = path.rsplit('/').next().unwrap_or(path); + let is_env = base.starts_with(".env") && !base.contains("prod"); + let is_cfg = base.starts_with("sanity.config.") || base.starts_with("sanity.cli."); + if !is_env && !is_cfg { + continue; + } + for line in text.lines() { + if is_env { + if let Some((k, v)) = line.trim().split_once('=') { + let (k, v) = (k.trim().trim_start_matches("export ").trim(), unquote(v)); + env_map.push((k.to_string(), v.to_string())); + if is_project_key(k) && valid_name(v) && project.is_none() { + project = Some((v.to_string(), path.clone())); + } + if is_dataset_key(k) && valid_name(v) { + let from = if base.contains("staging") { + format!("{path} (staging)") + } else { + path.clone() + }; + datasets.push((v.to_string(), from)); + } + } + continue; + } + for (keys, is_project) in [(&["projectId"][..], true), (&["dataset"][..], false)] { + let Some(raw) = value_of(line, keys) else { + continue; + }; + // `process.env.X` (or `import.meta.env.X`) resolves through + // the environment values read so far. + let v = match raw.rsplit_once("env.") { + Some((_, var)) => { + let var = + var.trim_matches(|c: char| !c.is_ascii_alphanumeric() && c != '_'); + let var = var + .split(|c: char| !c.is_ascii_alphanumeric() && c != '_') + .next() + .unwrap_or(""); + match env_map.iter().find(|(k, _)| k == var) { + Some((_, v)) => v.clone(), + None => continue, + } + } + None => raw.to_string(), + }; + if !valid_name(&v) { + continue; + } + if is_project && project.is_none() { + project = Some((v, path.clone())); + } else if !is_project { + datasets.push((v, path.clone())); + } + } + } + } + let (project_id, origin) = project?; + let (dataset, dataset_from) = datasets + .iter() + .find(|(d, from)| d.contains("staging") || from.contains("staging")) + .or_else(|| datasets.first()) + .cloned()?; + Some(ContentSource { + project_id, + dataset, + origin: if origin == dataset_from { + origin + } else { + format!("{origin}; dataset from {dataset_from}") + }, + }) +} + +/// The top-level `name:` of a workflow file. +fn workflow_name(text: &str) -> Option { + text.lines() + .find(|l| l.starts_with("name:")) + .map(|l| unquote(&l[5..]).to_string()) + .filter(|n| !n.is_empty()) +} + +/// The label a workflow runs on, when it runs on a labelled pull request. +fn workflow_label(text: &str) -> Option { + if !text.contains("labeled") { + return None; + } + for pat in ["github.event.label.name == ", "labels.*.name, "] { + if let Some(i) = text.find(pat) { + let rest = &text[i + pat.len()..]; + let q = rest.chars().next()?; + if q == '\'' || q == '"' { + let inner = &rest[1..]; + let end = inner.find(q)?; + return Some(inner[..end].to_string()); + } + } + } + None +} + +fn deploys_studio(text: &str) -> bool { + let l = text.to_lowercase(); + l.contains("sanity deploy") + || l.contains("sanity schema deploy") + || (l.contains("deploy") && l.contains("studio")) +} + +fn runs_e2e(text: &str) -> bool { + let l = text.to_lowercase(); + l.contains("playwright test") || l.contains("cypress run") || l.contains("test:e2e") +} + +fn route_for(path: &str, text: &str, command: &str) -> Route { + let trigger = if text.contains("workflow_dispatch") { + Trigger::Dispatch + } else if let Some(l) = workflow_label(text) { + Trigger::Label(l) + } else { + Trigger::OnMerge + }; + Route { + name: workflow_name(text) + .unwrap_or_else(|| path.rsplit('/').next().unwrap_or(path).to_string()), + file: path.to_string(), + command: command.to_string(), + trigger, + } +} + +/// The repository's deploy and end-to-end routes, migrations and fixtures +/// folders, from its tree and files (workflows, package files). +pub fn routes(tree: &[String], files: &[(String, String)]) -> (Vec, Vec) { + let mut deploys = Vec::new(); + let mut e2e = Vec::new(); + for (path, text) in files { + let workflow = path.starts_with(".github/workflows/") + && (path.ends_with(".yml") || path.ends_with(".yaml")); + if workflow { + if deploys_studio(text) { + let cmd = text + .lines() + .map(str::trim) + .map(|l| l.trim_start_matches("- ").trim_start_matches("run:").trim()) + .find(|l| l.contains("deploy") && !l.starts_with('#') && !l.starts_with("name")) + .unwrap_or("") + .to_string(); + deploys.push(route_for(path, text, &cmd)); + } + if runs_e2e(text) { + e2e.push(route_for(path, text, "")); + } + continue; + } + if path.rsplit('/').next() == Some("package.json") { + let Ok(v) = serde_json::from_str::(text) else { + continue; + }; + let dir = path.rsplit_once('/').map(|(d, _)| d).unwrap_or(""); + for (name, cmd) in v["scripts"].as_object().into_iter().flatten() { + let cmd = cmd.as_str().unwrap_or(""); + if cmd.contains("sanity deploy") || cmd.contains("sanity schema deploy") { + let run = if dir.is_empty() { + format!("npm run {name}") + } else { + format!("cd {dir} && npm run {name}") + }; + deploys.push(Route { + name: name.clone(), + file: path.clone(), + command: run, + trigger: Trigger::Script, + }); + } + } + } + } + let _ = tree; + // Staging first, then what CoderHelm can start, then the rest. + let rank = |r: &Route| { + let staging = format!("{} {}", r.name, r.file) + .to_lowercase() + .contains("staging"); + (!staging, !r.startable()) + }; + deploys.sort_by_key(rank); + e2e.sort_by_key(rank); + (deploys, e2e) +} + +/// The folder that holds `marker` (`migrations`, `fixtures`), shallowest +/// first, among the tree's files (not under node_modules). +fn folder_with(tree: &[String], markers: &[&str]) -> Option { + let mut found: Vec = tree + .iter() + .filter(|p| !p.contains("node_modules/")) + .filter_map(|p| { + let parts: Vec<&str> = p.split('/').collect(); + let at = parts[..parts.len().saturating_sub(1)] + .iter() + .position(|s| markers.contains(&s.to_lowercase().as_str()))?; + Some(parts[..=at].join("/")) + }) + .collect(); + found.sort_by_key(|d| (d.matches('/').count(), d.clone())); + found.into_iter().next() +} + +/// What the repository lets a run do itself. +pub fn abilities( + tree: &[String], + files: &[(String, String)], + env: &[(String, String)], + staging_write: bool, +) -> Abilities { + let (deploys, e2e) = routes(tree, files); + Abilities { + source: content_source(files, env), + deploys, + e2e, + migrations_dir: folder_with(tree, &["migrations"]), + fixtures_dir: folder_with(tree, &["fixtures", "seed", "seeds"]), + staging_write, + } +} + +/// The files [`abilities`] reads from a tree. +pub fn files_to_read(tree: &[String]) -> Vec { + tree.iter() + .filter(|p| !p.contains("node_modules/") && p.matches('/').count() <= 3) + .filter(|p| { + let b = p.rsplit('/').next().unwrap_or(p); + (p.starts_with(".github/workflows/") && (b.ends_with(".yml") || b.ends_with(".yaml"))) + || b == "package.json" + || b.starts_with("sanity.config.") + || b.starts_with("sanity.cli.") + || (b.starts_with(".env") && !b.contains("prod")) + }) + .take(40) + .cloned() + .collect() +} + +/// Reads what the repository lets a run do itself at `git_ref`. +pub async fn load( + github: &crate::clients::github::GitHubClient, + owner: &str, + repo: &str, + git_ref: &str, + env: &[(String, String)], + staging_write: bool, +) -> Abilities { + let tree: Vec = match github.get_tree(owner, repo, git_ref).await { + Ok(t) => t + .into_iter() + .filter(|e| e.entry_type == "blob") + .map(|e| e.path) + .collect(), + Err(_) => return Abilities::default(), + }; + let mut files = Vec::new(); + for p in files_to_read(&tree) { + if let Ok(t) = github.read_file(owner, repo, &p, git_ref).await { + files.push((p, t)); + } + } + abilities(&tree, &files, env, staging_write) +} + +impl Abilities { + /// What the planner is told: what it can query, what the change and + /// CoderHelm can do, in the order steps are decided. + pub fn prompt_section(&self) -> String { + let mut out = String::from( + "\n\n## What this repository lets the run do itself (decide every step in this order)\n\ + 1. The change does it (`by: this_pr`, a task in tasks.md): a content or data migration \ + that replaces the existing item by key, test fixtures or end-to-end setup that create \ + their own content (the test then never depends on editors), config or registration.\n\ + 2. CoderHelm does it after the PR opens (`by: coderhelm`): start a deploy or end-to-end \ + workflow listed below, run the confirm query.\n\ + 3. Only what is left goes to a person (`by: person`), each with `why_person` (the \ + exact permission or connection CoderHelm lacks, or why a person must decide) and \ + `grant` (how to give CoderHelm that access so it can do it next time).\n", + ); + match &self.source { + Some(s) => out.push_str(&format!( + "- Content: {}. Its read API is public: query it with `query_content` (a GROQ query; endpoint `{}`) \ + before you write anything about existing content, and quote the query and its \ + result in `outside_change.lookup`. Never write that you could not look up or \ + that none exists without a failed attempt's error.\n", + s.labels(), + s.query_url() + )), + None => out.push_str("- Content: no CMS project id and dataset found in the repository's config or codegen environment.\n"), + } + for r in &self.deploys { + out.push_str(&format!("- Studio/schema deploy: {}\n", r.line())); + } + for r in &self.e2e { + out.push_str(&format!("- End-to-end workflow: {}\n", r.line())); + } + if let Some(d) = &self.migrations_dir { + out.push_str(&format!( + "- Migrations folder: `{d}` (content changes go here as a task)\n" + )); + } + if let Some(d) = &self.fixtures_dir { + out.push_str(&format!("- Fixtures folder: `{d}`\n")); + } + out.push_str(if self.staging_write { + "- Staging CMS write connection: yes (CoderHelm can change staging content, never production)\n" + } else { + "- Staging CMS write connection: none for this repository\n" + }); + out + } + + fn startable_deploy(&self) -> Option<&Route> { + self.deploys.iter().find(|r| r.startable()) + } + + fn startable_e2e(&self) -> Option<&Route> { + self.e2e.iter().find(|r| r.startable()) + } +} + +// ── The note ──────────────────────────────────────────────────────────────── + +/// Who does a step. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Doer { + ThisPr, + CoderHelm, + Person, +} + +impl Doer { + pub fn parse(s: &str) -> Doer { + match s + .trim() + .to_ascii_lowercase() + .replace(['-', ' '], "_") + .as_str() + { + "this_pr" | "pr" | "change" => Doer::ThisPr, + "coderhelm" | "run" | "agent" => Doer::CoderHelm, + _ => Doer::Person, + } + } +} + +/// One step of the work outside the code, in the order it must happen. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct NoteStep { + /// What has to happen. + #[serde(default)] + pub step: String, + /// `this_pr`, `coderhelm` or `person`. + #[serde(default)] + pub by: String, + /// Who does it ("the engineer merging", "a content editor"). + #[serde(default)] + pub owner: String, + /// The workflow, command or screen it is done with. + #[serde(default)] + pub how: String, + /// For a person: why CoderHelm couldn't (the missing permission or + /// connection, by name, or why a person must decide). + #[serde(default)] + pub why_person: String, + /// For a person: how to grant CoderHelm that access next time. + #[serde(default)] + pub grant: String, + /// Exact values to set (field = value, reference targets, settings). + #[serde(default, deserialize_with = "super::fix_location::string_or_list")] + pub values: Vec, + /// Fields to remove. + #[serde(default, deserialize_with = "super::fix_location::string_or_list")] + pub remove: Vec, + /// The minimum the feature and its tests need to be meaningful. + #[serde(default)] + pub minimum: String, + /// What happened when CoderHelm did it (a run link, a query result). + #[serde(default)] + pub result: String, + /// CoderHelm did it and it worked. + #[serde(default)] + pub done: bool, +} + +impl NoteStep { + pub fn doer(&self) -> Doer { + Doer::parse(&self.by) + } + + fn text(&self) -> String { + format!("{} {}", self.step, self.how).to_lowercase() + } + + fn is_deploy(&self) -> bool { + let t = self.text(); + t.contains("deploy") && (t.contains("studio") || t.contains("schema") || t.contains("cms")) + } + + fn is_content(&self) -> bool { + let t = self.text(); + let verb = [ + "add", "create", "replace", "update", "set", "fill", "enter", "populate", + ] + .iter() + .any(|v| words(&t).any(|w| w == *v)); + let noun = [ + "content", "block", "document", "entry", "item", "tab", "card", "record", "page", + ] + .iter() + .any(|n| words(&t).any(|w| w.trim_end_matches('s') == *n)); + verb && noun && !self.is_deploy() + } + + fn is_production(&self) -> bool { + let t = self.text(); + t.contains("production") || words(&t).any(|w| w == "prod") + } + + fn is_e2e_rerun(&self) -> bool { + let t = self.text(); + (t.contains("e2e") || t.contains("end-to-end") || t.contains("end to end")) + && (t.contains("run") || t.contains("rerun")) + && !self.is_content() + } + + fn is_verify(&self) -> bool { + let t = self.text(); + (t.contains("confirm") || t.contains("verify") || t.contains("query")) + && !self.is_content() + && !self.is_deploy() + } +} + +fn words(t: &str) -> impl Iterator { + t.split(|c: char| !c.is_ascii_alphanumeric()) + .filter(|w| !w.is_empty()) +} + +/// The lookup behind the note: the query, where it ran and what came back +/// (or the error of a failed attempt). +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct ContentLookup { + #[serde(default)] + pub endpoint: String, + #[serde(default)] + pub query: String, + #[serde(default)] + pub result: String, + #[serde(default)] + pub error: String, +} + +/// An item already there that the work replaces. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct ExistingItem { + /// Its document id. + #[serde(default)] + pub id: String, + /// Its array key, for a block inside a document. + #[serde(default)] + pub key: String, + #[serde(default)] + pub title: String, + /// Fields it has that the new schema doesn't know. + #[serde(default, deserialize_with = "super::fix_location::string_or_list")] + pub old_fields: Vec, + /// Fields the new schema requires that it lacks. + #[serde(default, deserialize_with = "super::fix_location::string_or_list")] + pub missing_fields: Vec, + /// The earlier pull request or branch that left it, or where that was + /// searched for. + #[serde(default)] + pub left_by: String, +} + +/// How to confirm the work is done. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct Confirm { + /// The exact read query. + #[serde(default)] + pub query: String, + /// The result shape it must return. + #[serde(default)] + pub expect: String, + /// The page to look at. + #[serde(default)] + pub page: String, + /// The end-to-end test that must run and pass, not skip. + #[serde(default)] + pub e2e: String, + /// What the query returned when CoderHelm ran it. + #[serde(default)] + pub result: String, +} + +/// A labelled identifier (project id, dataset, environment). +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct Labelled { + #[serde(default)] + pub label: String, + #[serde(default)] + pub value: String, +} + +/// Phrases a note uses when it didn't look. +const NO_LOOK: &[&str] = &[ + "could not look", + "couldn't look", + "cannot look", + "can't look", + "unable to look", + "did not look", + "didn't look", + "not looked up", + "assumes none", + "assume none", + "assuming none", + "assumes no ", + "assuming no ", + "not checked", +]; + +fn says_no_look(text: &str) -> bool { + let t = text.to_lowercase(); + NO_LOOK.iter().any(|p| t.contains(p)) +} + +/// Whether a lookup's result lists at least one item. +fn result_has_items(result: &str) -> bool { + let r = result.trim(); + if let Ok(v) = serde_json::from_str::(r) { + let v = v.get("result").cloned().unwrap_or(v); + return match v { + Value::Array(a) => !a.is_empty(), + Value::Object(o) => !o.is_empty(), + Value::Null => false, + _ => true, + }; + } + r.contains("\"_id\"") || r.contains("_id:") +} + +/// What the plan says about the work, for [`gaps`]. +#[derive(Debug, Clone, Copy, Default)] +pub struct PlanFacts { + /// The plan changes the CMS schema. + pub schema_changed: bool, + /// The plan adds or changes an end-to-end spec. + pub e2e: bool, +} + +/// What a planner's outside-change note lacks, as instructions to the +/// planner (empty when it is complete). See the module docs. +pub fn gaps(c: &OutsideChange, a: &Abilities, plan: PlanFacts) -> Vec { + let mut out = Vec::new(); + let all_text = format!( + "{} {} {} {}", + c.change, + c.existing, + c.verify, + c.steps + .iter() + .map(|s| s.step.as_str()) + .collect::>() + .join(" ") + ); + let content = c.adds_content() || c.steps.iter().any(NoteStep::is_content); + let lookup = c.lookup.as_ref(); + let attempted = lookup.is_some_and(|l| !l.query.trim().is_empty()); + let failed = lookup.is_some_and(|l| !l.error.trim().is_empty()); + // Query before claiming. + if let Some(s) = &a.source { + if says_no_look(&all_text) && !failed { + out.push(format!( + "The note says it could not look up the content (or assumes none exists), but \ + the CMS read API is public: {}. Query it with `query_content` ({}), quote \ + the query and its result in `outside_change.lookup`, and write the note from \ + what came back. Only a failed attempt may say it couldn't look, quoting the error.", + s.labels(), + s.query_url() + )); + } else if content && !attempted { + out.push(format!( + "The note changes content but quotes no lookup. Query it with `query_content` ({}) for \ + items of that kind first and put the query, endpoint and result in \ + `outside_change.lookup` ({}).", + s.query_url(), + s.labels() + )); + } + } + // What is already there. + if let Some(l) = lookup.filter(|l| result_has_items(&l.result)) { + if c.existing_items.is_empty() { + out.push(format!( + "Your lookup `{}` returned items, so the note must describe each in \ + `outside_change.existing_items`: id (and key), title, fields the new schema \ + doesn't know (`old_fields`), new required fields it lacks (`missing_fields`), \ + and the earlier pull request or branch that left it (`left_by`, from the \ + ticket's earlier PRs or git history).", + one_line(&l.query) + )); + } + let adds = c.steps.iter().any(|s| { + let t = s.step.to_lowercase(); + (words(&t).any(|w| w == "add" || w == "create")) + && !t.contains("replace") + && !t.contains("update") + }); + if adds { + out.push("Content of that kind is already there: the step replaces that item's content (by its id and key), never adds a second one.".to_string()); + } + } + for e in &c.existing_items { + if e.id.trim().is_empty() || e.title.trim().is_empty() || e.left_by.trim().is_empty() { + out.push(format!( + "Describe the existing item `{}` fully: id, title and `left_by` (which earlier PR or branch left it, or where you searched).", + if e.id.is_empty() { "?" } else { &e.id } + )); + } + } + // Ordered steps with owners. + if c.steps.is_empty() { + out.push("Write the outside work as `outside_change.steps`, in the order it must happen, each with `by` (this_pr, coderhelm or person) and `owner`.".to_string()); + } + for (i, s) in c.steps.iter().enumerate() { + let n = i + 1; + if s.doer() != Doer::ThisPr && s.owner.trim().is_empty() { + out.push(format!( + "Step {n} (`{}`) names no owner.", + one_line(&s.step) + )); + } + if s.doer() == Doer::Person { + if s.why_person.trim().is_empty() || s.grant.trim().is_empty() { + out.push(format!( + "Step {n} (`{}`) goes to a person without saying why CoderHelm can't do it \ + (`why_person`: the exact permission or connection it lacks) and how to grant \ + it (`grant`). Do it in the change or as a CoderHelm step instead when you can.", + one_line(&s.step) + )); + } + // A person step the change or CoderHelm could do. + if s.is_deploy() { + if let Some(r) = a.startable_deploy() { + out.push(format!( + "Step {n} deploys the studio/schema, and CoderHelm can start {}: make it \ + `by: coderhelm` with that workflow in `how`.", + r.line() + )); + } + } else if s.is_content() && !s.is_production() { + if let Some(d) = &a.migrations_dir { + out.push(format!( + "Step {n} changes staging content by hand, but the repository keeps \ + migrations in `{d}`: write a migration there as a task (it replaces the \ + existing item by key) and make the step `by: this_pr`." + )); + } else if plan.e2e { + out.push(format!( + "Step {n} has an editor create content an end-to-end test reads: have \ + the test's own setup or fixtures create it{} (a task, `by: this_pr`), \ + so the test never depends on editors; keep a person step only for \ + production content.", + a.fixtures_dir + .as_ref() + .map(|d| format!(" in `{d}`")) + .unwrap_or_default() + )); + } else if a.staging_write { + out.push(format!( + "Step {n} changes staging content, and CoderHelm has this repository's \ + staging CMS write connection: make it `by: coderhelm`." + )); + } + } else if s.is_e2e_rerun() { + if let Some(r) = a.startable_e2e() { + out.push(format!( + "Step {n} re-runs the end-to-end tests, and CoderHelm can start {}: make it `by: coderhelm`.", + r.line() + )); + } + } else if s.is_verify() && a.source.is_some() { + out.push(format!( + "Step {n} is a read query CoderHelm can run itself: make it `by: coderhelm`." + )); + } + } + if s.is_content() && s.values.is_empty() { + out.push(format!( + "Step {n} (`{}`) changes content without exact values: list each field and value \ + (reference targets, tab definitions with their settings) in `values` and fields \ + to remove in `remove`.", + one_line(&s.step) + )); + } + if s.is_content() && plan.e2e && s.minimum.trim().is_empty() { + out.push(format!( + "Step {n} feeds an end-to-end test: say in `minimum` the least content the \ + feature and its test need to be meaningful (for example how many cards a tab \ + needs before the carousel can scroll), from the test and component code." + )); + } + } + // A schema change deploys before content. + if plan.schema_changed && !a.deploys.is_empty() { + let first_outside = c.steps.iter().find(|s| s.doer() != Doer::ThisPr); + let names_route = |s: &NoteStep| { + a.deploys + .iter() + .any(|r| r.named_in(&format!("{} {}", s.step, s.how))) + }; + if first_outside.is_some_and(|s| !(s.is_deploy() && names_route(s))) + || (c.steps.iter().any(NoteStep::is_content) && first_outside.is_none()) + { + out.push(format!( + "The change edits the CMS schema, and its fields reach the editor only after the \ + studio/schema is deployed. Make step 1 that deploy, naming {} and its owner \ + (the engineer merging, or CoderHelm when it can start it); content steps come after.", + a.deploys[0].line() + )); + } + } + // How to confirm. + if content && a.source.is_some() { + let ok = c + .confirm + .as_ref() + .is_some_and(|f| !f.query.trim().is_empty() && !f.expect.trim().is_empty()); + if !ok { + out.push("Say how to confirm in `outside_change.confirm`: the exact read query and the result shape it must return (for example exactly one item, count of tabs >= 2, the reference set, old fields not defined).".to_string()); + } + if plan.e2e && c.confirm.as_ref().is_none_or(|f| f.e2e.trim().is_empty()) { + out.push("Name in `confirm.e2e` the end-to-end test that must run and pass (not skip) once the content is in.".to_string()); + } + } + // Labelled names. + if let Some(s) = &a.source { + for n in &c.names { + let l = n.label.to_lowercase(); + if n.value == s.project_id && !l.contains("project") { + out.push(format!( + "`{}` is the CMS project id, labelled `{}`.", + n.value, n.label + )); + } + if n.value == s.dataset && !l.contains("dataset") { + out.push(format!( + "`{}` is the CMS dataset, labelled `{}`.", + n.value, n.label + )); + } + } + } + out +} + +/// The outside-step gaps of a plan: its decision's note checked against +/// what the repository offers, with what the plan's tasks change. +pub fn plan_gaps(plan: &super::plan::PlanResult, a: &Abilities) -> Vec { + let Some(c) = plan.fix.as_ref().and_then(|d| d.outside_change.as_ref()) else { + return Vec::new(); + }; + if c.change.trim().is_empty() && c.steps.is_empty() { + return Vec::new(); + } + let tasks = super::plan_coverage::plan_tasks(&plan.tasks); + let facts = PlanFacts { + schema_changed: tasks.iter().any(|t| { + let p = t.path.to_lowercase(); + p.contains("schema") + && (p.contains("studio") || p.contains("sanity") || p.contains("cms")) + }), + e2e: tasks + .iter() + .any(|t| super::feature::content::is_e2e_spec(&t.path)), + }; + gaps(c, a, facts) +} + +fn one_line(s: &str) -> String { + s.split_whitespace().collect::>().join(" ") +} + +fn quote_result(s: &str) -> String { + let s = one_line(s); + if s.len() <= RESULT_QUOTE { + return s; + } + let mut end = RESULT_QUOTE; + while !s.is_char_boundary(end) { + end -= 1; + } + format!("{}…", &s[..end]) +} + +// ── Rendering: one source for the ticket note and the PR ──────────────────── + +/// One step as both the ticket note and the PR list it, with its owner. +pub fn step_line(n: usize, s: &NoteStep) -> (String, String) { + let mut t = format!("{n}. {}", one_line(&s.step)); + if !s.how.trim().is_empty() { + t.push_str(&format!( + " How: {}.", + one_line(&s.how).trim_end_matches('.') + )); + } + if !s.values.is_empty() { + t.push_str(&format!( + " Values: {}.", + s.values + .iter() + .map(|v| one_line(v)) + .collect::>() + .join("; ") + )); + } + if !s.remove.is_empty() { + t.push_str(&format!( + " Remove: {}.", + s.remove + .iter() + .map(|v| format!("`{}`", one_line(v))) + .collect::>() + .join(", ") + )); + } + if !s.minimum.trim().is_empty() { + t.push_str(&format!( + " At least: {}.", + one_line(&s.minimum).trim_end_matches('.') + )); + } + match s.doer() { + Doer::CoderHelm if !s.result.trim().is_empty() => { + let state = if s.done { + "Done by CoderHelm" + } else { + "CoderHelm could not do it" + }; + t.push_str(&format!( + " {state}: {}.", + one_line(&s.result).trim_end_matches('.') + )); + } + Doer::CoderHelm => t.push_str(" CoderHelm does this after the pull request opens."), + Doer::Person => { + if !s.why_person.trim().is_empty() { + t.push_str(&format!( + " Why a person: {}.", + one_line(&s.why_person).trim_end_matches('.') + )); + } + if !s.grant.trim().is_empty() { + t.push_str(&format!( + " So CoderHelm can do it next time: {}.", + one_line(&s.grant).trim_end_matches('.') + )); + } + } + Doer::ThisPr => {} + } + let owner = match s.doer() { + Doer::ThisPr => "this pull request".to_string(), + Doer::CoderHelm => "CoderHelm".to_string(), + Doer::Person => match one_line(&s.owner) { + o if o.is_empty() => "not named yet".to_string(), + o => o, + }, + }; + (t, owner) +} + +/// The lines before the steps: the names, the lookup and what is there. +pub fn context_lines(c: &OutsideChange) -> Vec { + let mut out = Vec::new(); + if !c.names.is_empty() { + out.push(format!( + "Names: {}", + c.names + .iter() + .map(|n| format!("{} `{}`", one_line(&n.label), one_line(&n.value))) + .collect::>() + .join(", ") + )); + } + if let Some(l) = &c.lookup { + if !l.error.trim().is_empty() { + out.push(format!( + "Looked up `{}` at {}: the lookup failed ({})", + one_line(&l.query), + one_line(&l.endpoint), + one_line(&l.error) + )); + } else if !l.query.trim().is_empty() { + out.push(format!( + "Looked up `{}` at {}: {}", + one_line(&l.query), + one_line(&l.endpoint), + quote_result(&l.result) + )); + } + } + for e in &c.existing_items { + let mut t = format!("Already there: `{}`", one_line(&e.id)); + if !e.key.trim().is_empty() { + t.push_str(&format!(" (key `{}`)", one_line(&e.key))); + } + if !e.title.trim().is_empty() { + t.push_str(&format!(", \"{}\"", one_line(&e.title))); + } + if !e.old_fields.is_empty() { + t.push_str(&format!( + "; fields the new schema doesn't know: {}", + e.old_fields + .iter() + .map(|f| format!("`{f}`")) + .collect::>() + .join(", ") + )); + } + if !e.missing_fields.is_empty() { + t.push_str(&format!( + "; new required fields it lacks: {}", + e.missing_fields + .iter() + .map(|f| format!("`{f}`")) + .collect::>() + .join(", ") + )); + } + if !e.left_by.trim().is_empty() { + t.push_str(&format!("; left by {}", one_line(&e.left_by))); + } + t.push_str(". Its content is replaced, not added to."); + out.push(t); + } + out +} + +/// The confirm line. +pub fn confirm_line(c: &OutsideChange) -> Option { + let f = c.confirm.as_ref()?; + if f.query.trim().is_empty() && f.page.trim().is_empty() && f.e2e.trim().is_empty() { + return None; + } + let mut t = String::from("How to confirm:"); + if !f.query.trim().is_empty() { + t.push_str(&format!(" query `{}`", one_line(&f.query))); + if !f.expect.trim().is_empty() { + t.push_str(&format!( + ", expect {}", + one_line(&f.expect).trim_end_matches('.') + )); + } + t.push(';'); + } + if !f.page.trim().is_empty() { + t.push_str(&format!( + " look at {};", + one_line(&f.page).trim_end_matches('.') + )); + } + if !f.e2e.trim().is_empty() { + t.push_str(&format!( + " `{}` must run and pass, not skip;", + one_line(&f.e2e) + )); + } + let mut t = t.trim_end_matches(';').to_string(); + t.push('.'); + if !f.result.trim().is_empty() { + t.push_str(&format!( + " CoderHelm ran the query: {}", + quote_result(&f.result) + )); + } + Some(t) +} + +/// The steps outside this pull request, in order, each with its owner: what +/// the PR's "Not in this PR" lists, from the same steps as the ticket note. +pub fn pr_lines(c: &OutsideChange) -> Vec<(String, String)> { + let mut out: Vec<(String, String)> = c + .steps + .iter() + .enumerate() + .filter(|(_, s)| s.doer() != Doer::ThisPr) + .map(|(i, s)| step_line(i + 1, s)) + .collect(); + if let Some(l) = confirm_line(c) { + out.push((l, "the owner of the last step".to_string())); + } + out +} + +/// The ticket note's body: the names and lookup, then every step in order +/// with its owner, then how to confirm. +pub fn note_markdown(c: &OutsideChange) -> String { + let mut out = String::new(); + for l in context_lines(c) { + out.push_str(&format!("- {l}\n")); + } + if !c.steps.is_empty() { + out.push_str("\n**Steps, in order**\n"); + for (i, s) in c.steps.iter().enumerate() { + let (t, owner) = step_line(i + 1, s); + out.push_str(&format!("{t} (owner: {owner})\n")); + } + } + if let Some(l) = confirm_line(c) { + out.push_str(&format!("\n{l}\n")); + } + out +} + +// ── Requirements a person holds, for the plan check ───────────────────────── + +/// What the plan check counts for the outside steps. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Held { + /// Steps a person does, with the recorded reason: not done by the run. + pub by_person: Vec, + /// Steps given to a person with no reason: not covered. + pub unjustified: Vec, + /// CoderHelm steps that did not work: not done. + pub not_done: Vec, +} + +/// The outside steps the run did not do, sorted by what the plan check +/// makes of them. +pub fn held(c: &OutsideChange) -> Held { + let mut h = Held::default(); + for (i, s) in c.steps.iter().enumerate() { + let (line, owner) = step_line(i + 1, s); + match s.doer() { + Doer::ThisPr => {} + Doer::CoderHelm => { + if !s.done && !s.result.trim().is_empty() { + h.not_done.push(line); + } + } + Doer::Person => { + if s.why_person.trim().is_empty() || s.grant.trim().is_empty() { + h.unjustified.push(format!("{line} (owner: {owner})")); + } else { + h.by_person.push(format!("{line} (owner: {owner})")); + } + } + } + } + h +} + +// ── Doing the CoderHelm steps ─────────────────────────────────────────────── + +/// Runs a read query on the public endpoint (no token). Returns the +/// `result`, or the error a failed attempt gave. +pub async fn public_query( + http: &reqwest::Client, + s: &ContentSource, + query: &str, +) -> Result { + let r = http + .get(s.query_url()) + .query(&[("query", query)]) + .timeout(std::time::Duration::from_secs(10)) + .send() + .await + .map_err(|e| format!("request failed: {e}"))?; + let status = r.status(); + if !status.is_success() { + return Err(format!("the read API returned {status}")); + } + let v: Value = r + .json() + .await + .map_err(|e| format!("unreadable result: {e}"))?; + Ok(v["result"].clone()) +} + +/// The read-only content query tool the planner gets when the repository +/// names its CMS project and dataset. +pub const TOOL: &str = "query_content"; + +pub fn tool(s: &ContentSource) -> crate::agent::llm::ToolDefinition { + crate::agent::llm::ToolDefinition { + name: TOOL.to_string(), + description: format!( + "Run a read-only GROQ query on the repository's CMS ({}) through its public read \ + API, without a token, and get the result. Use it before writing anything about \ + existing content (what items of a type exist, their ids, keys, titles and fields), \ + and quote the query and result in the note.", + s.labels() + ), + input_schema: serde_json::json!({ + "type": "object", + "properties": { + "query": {"type": "string", "description": "A GROQ query, for example *[_type == \"page\" && count(sections[_type == \"hero\"]) > 0][0...5]{_id, title, \"items\": sections[_type == \"hero\"]}"} + }, + "required": ["query"] + }), + } +} + +/// Answers a [`TOOL`] call: the query, the endpoint and the result (or the +/// error, quoted). `None` for another tool. +pub async fn handle( + http: &reqwest::Client, + a: &Abilities, + name: &str, + input: &Value, +) -> Option { + if name != TOOL { + return None; + } + let Some(s) = a.source.as_ref() else { + return Some("No CMS project id and dataset are named in this repository.".into()); + }; + let q = input["query"].as_str().unwrap_or("").trim(); + if q.is_empty() || q.len() > 2_000 { + return Some("Give a GROQ query (up to 2000 characters).".into()); + } + Some(match public_query(http, s, q).await { + Ok(v) => { + let mut out = v.to_string(); + if out.len() > 8_000 { + let mut end = 8_000; + while !out.is_char_boundary(end) { + end -= 1; + } + out.truncate(end); + out.push_str("… (cut)"); + } + format!("endpoint: {}\nquery: {q}\nresult: {out}", s.query_url()) + } + Err(e) => format!("endpoint: {}\nquery: {q}\nerror: {e}", s.query_url()), + }) +} + +/// What one CoderHelm step will do, from its text and the repository. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Action { + /// Start this workflow on the PR branch. + Dispatch(Route), + /// Add this label to the PR. + Label(String, Route), + /// Run the confirm query. + Query, + /// CoderHelm can't do it: why, and how to grant it. + Cannot(String), +} + +/// The action for a CoderHelm step. +pub fn action_for(s: &NoteStep, a: &Abilities, c: &OutsideChange) -> Action { + let pick = |routes: &[Route]| -> Option { + let r = routes + .iter() + .filter(|r| r.startable()) + .find(|r| r.named_in(&format!("{} {}", s.step, s.how))) + .or_else(|| routes.iter().find(|r| r.startable()))? + .clone(); + Some(match &r.trigger { + Trigger::Label(l) => Action::Label(l.clone(), r.clone()), + _ => Action::Dispatch(r), + }) + }; + if s.is_deploy() { + return pick(&a.deploys).unwrap_or_else(|| { + Action::Cannot("the repository has no studio/schema deploy workflow CoderHelm can start (one with workflow_dispatch, or one that runs on a pull request label); add `workflow_dispatch:` to the deploy workflow's triggers".to_string()) + }); + } + if s.is_e2e_rerun() { + return pick(&a.e2e).unwrap_or_else(|| { + Action::Cannot("the end-to-end workflow has no workflow_dispatch trigger; add `workflow_dispatch:` to it so CoderHelm can start it".to_string()) + }); + } + if s.is_verify() || (!s.is_content() && c.confirm.as_ref().is_some_and(|f| !f.query.is_empty())) + { + return if a.source.is_some() { + Action::Query + } else { + Action::Cannot("no CMS project id and dataset are named in the repository's config or codegen environment; add them to the codegen environment (Settings, repository, codegen environment)".to_string()) + }; + } + if s.is_content() { + return Action::Cannot(if s.is_production() { + "CoderHelm never changes production content".to_string() + } else if a.staging_write { + "content beyond removing dropped fields and setting schema defaults is not written by CoderHelm on staging; write it as a migration in the change".to_string() + } else { + "this repository has no staging CMS write connection; add one in Settings, repository, Staging CMS (a staging-only write token)".to_string() + }); + } + Action::Cannot("CoderHelm has no action for this step".to_string()) +} + +/// Does the CoderHelm steps after the pull request opens, recording each +/// result on its step (and the confirm query's on `confirm`). Returns +/// whether anything was done. +#[allow(clippy::too_many_arguments)] +pub async fn perform( + github: &crate::clients::github::GitHubClient, + http: &reqwest::Client, + owner: &str, + repo: &str, + branch: &str, + pr_number: u64, + a: &Abilities, + c: &mut OutsideChange, +) -> bool { + let mut any = false; + let snapshot = c.clone(); + for s in c + .steps + .iter_mut() + .filter(|s| s.doer() == Doer::CoderHelm && !s.done) + { + any = true; + match action_for(s, a, &snapshot) { + Action::Dispatch(r) => { + match github + .dispatch_workflow( + owner, + repo, + r.file.rsplit('/').next().unwrap_or(&r.file), + branch, + &serde_json::json!({}), + ) + .await + { + Ok(()) => { + let link = github + .list_workflow_runs( + owner, + repo, + r.file.rsplit('/').next().unwrap_or(&r.file), + branch, + ) + .await + .ok() + .and_then(|runs| { + runs.first() + .and_then(|x| x["html_url"].as_str().map(String::from)) + }) + .unwrap_or_else(|| { + format!("https://github.com/{owner}/{repo}/actions") + }); + s.result = format!("started {} on `{branch}`: {link}", r.line()); + s.done = true; + } + Err(e) => s.result = format!("starting {} failed: {e}", r.line()), + } + } + Action::Label(l, r) => match github.add_label(owner, repo, pr_number, &l).await { + Ok(()) => { + s.result = format!( + "added the `{l}` label to #{pr_number}, which starts {}", + r.line() + ); + s.done = true; + } + Err(e) => s.result = format!("adding the `{l}` label failed: {e}"), + }, + Action::Query => { + let (Some(src), Some(q)) = ( + a.source.as_ref(), + snapshot + .confirm + .as_ref() + .map(|f| f.query.clone()) + .filter(|q| !q.trim().is_empty()), + ) else { + s.result = "no confirm query to run".to_string(); + continue; + }; + match public_query(http, src, &q).await { + Ok(v) => { + let out = v.to_string(); + s.result = format!( + "ran `{}` on {}: {}", + one_line(&q), + src.labels(), + quote_result(&out) + ); + s.done = true; + } + Err(e) => s.result = format!("ran `{}` on {}: {e}", one_line(&q), src.labels()), + } + } + Action::Cannot(why) => s.result = format!("not done: {why}"), + } + } + // The confirm query's own result, whenever it can be read. + if let (Some(src), Some(f)) = (a.source.as_ref(), c.confirm.as_mut()) { + if !f.query.trim().is_empty() && f.result.is_empty() { + f.result = match public_query(http, src, &f.query).await { + Ok(v) => v.to_string(), + Err(e) => format!("the query failed: {e}"), + }; + any = true; + } + } + any +} + +#[cfg(test)] +mod tests { + use super::*; + + fn f(p: &str, t: &str) -> (String, String) { + (p.to_string(), t.to_string()) + } + + fn repo() -> (Vec, Vec<(String, String)>) { + let files = vec![ + f(".env.staging", "NEXT_PUBLIC_SANITY_PROJECT_ID=abc123\nNEXT_PUBLIC_SANITY_DATASET=staging\n"), + f(".env.production", "NEXT_PUBLIC_SANITY_DATASET=production\n"), + f( + ".github/workflows/studio-staging.yml", + "name: Deploy studio (staging)\non:\n workflow_dispatch:\n push:\n branches: [main]\njobs:\n d:\n steps:\n - run: npx sanity deploy\n", + ), + f( + ".github/workflows/e2e.yml", + "name: E2E\non:\n pull_request:\n types: [labeled]\njobs:\n e:\n if: contains(github.event.pull_request.labels.*.name, 'run-e2e')\n steps:\n - run: npx playwright test\n", + ), + f("studio/package.json", r#"{"scripts":{"deploy":"sanity deploy"}}"#), + ]; + let tree = vec![ + "studio/migrations/001-hero/index.ts".to_string(), + "e2e/fixtures/page.json".to_string(), + "src/app/page.tsx".to_string(), + ]; + (tree, files) + } + + #[test] + fn reads_the_cms_deploys_and_folders_from_the_repository() { + let (tree, files) = repo(); + let a = abilities(&tree, &files, &[], false); + let s = a.source.clone().unwrap(); + assert_eq!(s.project_id, "abc123"); + assert_eq!(s.dataset, "staging"); + assert_eq!( + s.query_url(), + "https://abc123.api.sanity.io/v2021-10-21/data/query/staging" + ); + assert!(s + .labels() + .contains("CMS project id `abc123`, dataset `staging`")); + assert_eq!(a.deploys[0].file, ".github/workflows/studio-staging.yml"); + assert_eq!(a.deploys[0].trigger, Trigger::Dispatch); + assert_eq!(a.deploys[0].command, "npx sanity deploy"); + assert!(a + .deploys + .iter() + .any(|r| r.command == "cd studio && npm run deploy")); + assert_eq!(a.e2e[0].trigger, Trigger::Label("run-e2e".into())); + assert_eq!(a.migrations_dir.as_deref(), Some("studio/migrations")); + assert_eq!(a.fixtures_dir.as_deref(), Some("e2e/fixtures")); + // The codegen environment and a config that reads it. + let s = content_source( + &[f("studio/sanity.config.ts", "export default defineConfig({\n projectId: process.env.SANITY_STUDIO_PROJECT_ID!,\n dataset: 'staging',\n})")], + &[("SANITY_STUDIO_PROJECT_ID".into(), "zz9".into())], + ) + .unwrap(); + assert_eq!( + (s.project_id.as_str(), s.dataset.as_str()), + ("zz9", "staging") + ); + assert!(content_source(&[f("README.md", "projectId: x")], &[]).is_none()); + assert!(a.prompt_section().contains("query_content")); + } + + fn step(by: &str, step: &str) -> NoteStep { + NoteStep { + step: step.into(), + by: by.into(), + owner: "a content editor".into(), + ..Default::default() + } + } + + /// The reviewed case: a note that said it couldn't look up the content + /// while the read API was public, added a block that was already there, + /// put content before the studio deploy and handed everything to people. + #[test] + fn the_reviewed_note_goes_back_with_every_rule() { + let (tree, files) = repo(); + let a = abilities(&tree, &[files[0].clone(), files[2].clone()], &[], false); + let c = OutsideChange { + system: "the CMS".into(), + change: "Add a carousel block to the events page".into(), + existing: "I could not look up existing content; this assumes none exists.".into(), + steps: vec![ + step("person", "Add a carousel block to the events page"), + NoteStep { + owner: "the engineer merging".into(), + ..step("person", "Deploy the studio") + }, + ], + names: vec![Labelled { + label: "dataset".into(), + value: "abc123".into(), + }], + ..Default::default() + }; + let g = gaps( + &c, + &a, + PlanFacts { + schema_changed: true, + e2e: true, + }, + ) + .join("\n"); + assert!(g.contains("the CMS read API is public"), "{g}"); + assert!(g.contains("https://abc123.api.sanity.io/v2021-10-21/data/query/staging")); + assert!(g.contains("without saying why CoderHelm can't do it")); + assert!( + g.contains("CoderHelm can start `Deploy studio (staging)`"), + "{g}" + ); + assert!(g.contains("migrations in `studio/migrations`"), "{g}"); + assert!(g.contains("Make step 1 that deploy"), "{g}"); + assert!(g.contains("without exact values")); + assert!(g.contains("`minimum`")); + assert!(g.contains("outside_change.confirm")); + assert!(g.contains("`abc123` is the CMS project id, labelled `dataset`")); + } + + #[test] + fn a_lookup_that_found_items_needs_them_described_and_replaced() { + let a = Abilities { + source: Some(ContentSource { + project_id: "p1".into(), + dataset: "staging".into(), + origin: ".env".into(), + }), + ..Default::default() + }; + let mut c = OutsideChange { + system: "the CMS".into(), + change: "Add a carousel".into(), + lookup: Some(ContentLookup { + endpoint: "x".into(), + query: "*[_type == \"carousel\"]{_id}".into(), + result: r#"[{"_id":"page-events"}]"#.into(), + error: String::new(), + }), + steps: vec![NoteStep { + values: vec!["title = Classes".into()], + why_person: "only an editor picks the classes shown".into(), + grant: "none: a content decision".into(), + ..step("person", "Add a carousel block on production") + }], + confirm: Some(Confirm { + query: "count(*[_type == \"carousel\"])".into(), + expect: "exactly 1".into(), + ..Default::default() + }), + ..Default::default() + }; + let g = gaps(&c, &a, PlanFacts::default()).join("\n"); + assert!(g.contains("existing_items"), "{g}"); + assert!(g.contains("never adds a second"), "{g}"); + c.existing_items = vec![ExistingItem { + id: "page-events".into(), + key: "k1".into(), + title: "Events".into(), + old_fields: vec!["image".into()], + missing_fields: vec!["tabs".into()], + left_by: "#41 (branch feature/carousel)".into(), + }]; + c.steps[0].step = "Replace the carousel block's content on production".into(); + let g = gaps(&c, &a, PlanFacts::default()); + assert!(g.is_empty(), "{g:?}"); + let note = note_markdown(&c); + assert!(note.contains("Already there: `page-events` (key `k1`), \"Events\"; fields the new schema doesn't know: `image`; new required fields it lacks: `tabs`; left by #41"), "{note}"); + assert!(note.contains("1. Replace the carousel block's content on production Values: title = Classes. Why a person: only an editor picks the classes shown.")); + assert!(note.contains( + "How to confirm: query `count(*[_type == \"carousel\"])`, expect exactly 1." + )); + // The PR lists the same steps. + let pr = pr_lines(&c); + assert!(note.contains(&pr[0].0)); + assert_eq!(pr[0].1, "a content editor"); + // A failed attempt may say so, quoting the error. + let failed = OutsideChange { + existing: "could not look up: the lookup failed".into(), + lookup: Some(ContentLookup { + query: "*[_type == \"carousel\"]".into(), + error: "the read API returned 401 Unauthorized".into(), + ..Default::default() + }), + ..c.clone() + }; + assert!(!gaps(&failed, &a, PlanFacts::default()) + .iter() + .any(|g| g.contains("read API is public"))); + assert!(context_lines(&failed)[0] + .contains("the lookup failed (the read API returned 401 Unauthorized)")); + } + + #[test] + fn the_plan_check_counts_a_person_step_without_a_reason_as_not_covered() { + let c = OutsideChange { + steps: vec![ + step("this_pr", "Write the migration"), + step("person", "Publish on production"), + NoteStep { + why_person: "production content needs an editor's approval".into(), + grant: "none: CoderHelm never writes production".into(), + ..step("person", "Approve the production content") + }, + NoteStep { + result: "not done: the end-to-end workflow has no workflow_dispatch trigger" + .into(), + ..step("coderhelm", "Re-run the e2e tests") + }, + ], + ..Default::default() + }; + let h = held(&c); + assert_eq!(h.unjustified.len(), 1); + assert!(h.unjustified[0].starts_with("2. Publish on production")); + assert_eq!(h.by_person.len(), 1); + assert_eq!(h.not_done.len(), 1); + assert!( + h.not_done[0].contains("CoderHelm could not do it: not done: the end-to-end workflow") + ); + } + + #[test] + fn each_coderhelm_step_gets_its_action() { + let (tree, files) = repo(); + let a = abilities(&tree, &files, &[], false); + let c = OutsideChange::default(); + assert!(matches!( + action_for(&step("coderhelm", "Deploy the studio schema to staging"), &a, &c), + Action::Dispatch(r) if r.file.ends_with("studio-staging.yml") + )); + assert!(matches!( + action_for(&step("coderhelm", "Re-run the e2e tests on staging"), &a, &c), + Action::Label(l, _) if l == "run-e2e" + )); + assert_eq!( + action_for(&step("coderhelm", "Confirm with the query"), &a, &c), + Action::Query + ); + assert!(matches!( + action_for(&step("coderhelm", "Replace the block content on production"), &a, &c), + Action::Cannot(w) if w.contains("never changes production") + )); + let none = Abilities::default(); + assert!(matches!( + action_for(&step("coderhelm", "Deploy the studio"), &none, &c), + Action::Cannot(w) if w.contains("workflow_dispatch") + )); + } +} diff --git a/services/worker/src/passes/plan.rs b/services/worker/src/passes/plan.rs index 4ace10be2..5258fc2e6 100644 --- a/services/worker/src/passes/plan.rs +++ b/services/worker/src/passes/plan.rs @@ -537,6 +537,34 @@ Then the ```fix_location.json block described above."#, &mut tools, &mut full_system, ); + // What the repository lets the run do itself (its CMS read API, deploy + // and end-to-end workflows, migrations and fixtures folders): the + // planner decides each outside step against it, and queries content + // before writing about it. + let abilities = { + let env = + super::load_codegen_env(state, &msg.team_id, &msg.repo_owner, &msg.repo_name).await; + let staging_write = super::feature::load_connection( + state, + &msg.team_id, + &format!("{}/{}", msg.repo_owner, msg.repo_name), + ) + .await + .is_some(); + super::outside_work::load( + github, + &msg.repo_owner, + &msg.repo_name, + &msg.base_branch, + &env, + staging_write, + ) + .await + }; + if let Some(s) = &abilities.source { + tools.push(super::outside_work::tool(s)); + } + full_system.push_str(&abilities.prompt_section()); // `check_url` on the team's own public pages. let web = super::web_check::WebCheck::load( state, @@ -638,6 +666,7 @@ Then the ```fix_location.json block described above."#, skills: skills.as_ref(), knowledge: knowledge.as_ref(), web: Some(&web), + abilities: Some(&abilities), state, }; @@ -738,6 +767,10 @@ Then the ```fix_location.json block described above."#, // reuse and the closest sibling. let feature_gaps = super::feature::plan_gaps(&draft, intent, &ticket_history.prior); back.extend(feature_gaps.iter().cloned()); + // The outside steps: decided in order (the change, CoderHelm, a person + // with a reason), looked up before claimed, ordered, valued, confirmable. + let note_gaps = super::outside_work::plan_gaps(&draft, &abilities); + back.extend(note_gaps.iter().cloned()); if !back.is_empty() { info!( ticket_id = %msg.ticket_id, @@ -939,6 +972,22 @@ Then the ```fix_location.json block described above."#, } }); } + if !note_gaps.is_empty() { + let left = super::outside_work::plan_gaps(&files, &abilities); + if !left.is_empty() { + warn!(ticket_id = %msg.ticket_id, gaps = ?left, "Outside steps still incomplete after one send-back"); + super::add_progress_note( + state, + &msg.team_id, + run_id, + &format!("Plan check (outside steps): {}", left.join(" ")), + ) + .await; + if let Some(d) = files.fix.as_mut() { + d.open_gaps.extend(left); + } + } + } if !feature_gaps.is_empty() { let left = super::feature::plan_gaps(&files, intent, &ticket_history.prior); if !left.is_empty() { @@ -1622,6 +1671,8 @@ struct CombinedToolExecutor<'a> { live: Option<&'a super::live_sources::LiveSources>, /// `check_url` on the team's own public pages. web: Option<&'a super::web_check::WebCheck>, + /// `query_content` on the repository's CMS read API. + abilities: Option<&'a super::outside_work::Abilities>, /// The team's written skills, read-only. skills: Option<&'a super::skills::TeamSkills>, /// Knowledge bases attached to the planner. @@ -1654,6 +1705,11 @@ impl<'a> ToolExecutor for CombinedToolExecutor<'a> { return Ok(serde_json::json!(out)); } } + if let Some(a) = self.abilities { + if let Some(out) = super::outside_work::handle(&self.state.http, a, name, input).await { + return Ok(serde_json::json!(out)); + } + } if let Some(web) = self.web { if let Some(out) = web .handle(self.state, self.read_only.github, name, input) diff --git a/services/worker/src/passes/plan_coverage.rs b/services/worker/src/passes/plan_coverage.rs index 798616646..27301b838 100644 --- a/services/worker/src/passes/plan_coverage.rs +++ b/services/worker/src/passes/plan_coverage.rs @@ -375,6 +375,55 @@ pub fn optional_findings(cov: &Coverage) -> Vec { .collect() } +/// The outside steps the plan check counts: none of them is done by the +/// branch. A step handed to a person counts as not done, with its recorded +/// reason; one handed out with no reason is not covered at all. +pub fn outside_findings(d: Option<&super::fix_location::FixDecision>) -> Vec { + let Some(c) = d + .filter(|d| d.effective_location() == super::fix_location::FixLocation::Both) + .and_then(|d| d.outside_change.as_ref()) + else { + return Vec::new(); + }; + let h = super::outside_work::held(c); + let mut out = Vec::new(); + for l in h.unjustified { + out.push(Finding { + point: "planned_work".to_string(), + detail: format!("Not covered: {l}"), + settle: "The plan gives this to a person without saying why CoderHelm couldn't do \ + it or how to grant the access: do it in the change or let CoderHelm do it, \ + or record the missing permission and how to grant it." + .to_string(), + ..Default::default() + }); + } + for l in h.by_person { + out.push(Finding { + point: "planned_work".to_string(), + detail: format!("Not done by this run (a person's step): {l}"), + settle: "Done when its owner makes it and the confirm query returns what the note \ + expects." + .to_string(), + ..Default::default() + }); + } + out +} + +/// A progress note counting the outside steps. +pub fn outside_note(d: Option<&super::fix_location::FixDecision>) -> String { + let Some(c) = d.and_then(|d| d.outside_change.as_ref()) else { + return String::new(); + }; + let h = super::outside_work::held(c); + format!( + "Plan check (outside the code): {} step(s) for a person with a recorded reason, {} handed out with no reason (not covered)", + h.by_person.len(), + h.unjustified.len() + ) +} + /// The plan's checklist with only the done tasks checked: what the run page /// and a retry read, instead of every task ticked when the pass ended. pub fn checked_tasks_md(tasks_md: &str, cov: &Coverage) -> String { @@ -721,4 +770,45 @@ mod tests { assert!(md.contains("- [ ] `src/blocks/gallery/schema.ts`")); assert!(md.contains("- [ ] Run the type-check")); } + + #[test] + fn an_outside_step_without_a_reason_is_not_covered() { + use crate::passes::fix_location::{FixDecision, FixLocation, OutsideChange}; + use crate::passes::outside_work::NoteStep; + let d = FixDecision { + fix_location: FixLocation::Both, + outside_change: Some(OutsideChange { + system: "the CMS".into(), + change: "Publish the block".into(), + steps: vec![ + NoteStep { + step: "Publish on production".into(), + by: "person".into(), + owner: "an editor".into(), + ..Default::default() + }, + NoteStep { + step: "Approve the production copy".into(), + by: "person".into(), + owner: "an editor".into(), + why_person: "production content needs an editor's approval".into(), + grant: "none: CoderHelm never writes production".into(), + ..Default::default() + }, + ], + ..Default::default() + }), + ..Default::default() + }; + let f = outside_findings(Some(&d)); + assert_eq!(f.len(), 2); + assert!(f[0] + .detail + .starts_with("Not covered: 1. Publish on production")); + assert!(f[1] + .detail + .contains("Why a person: production content needs an editor's approval")); + assert!(outside_note(Some(&d)).contains("1 handed out with no reason")); + assert!(outside_findings(None).is_empty()); + } }