diff --git a/.crumb b/.crumb index 7c194842..aa90c504 100644 --- a/.crumb +++ b/.crumb @@ -77,14 +77,14 @@ } ], "children_root": "sha256:285ca7eafbe46d808ebe5e4fb533e464bfdf0fedaf3eef486ac2e2c8826baf6f", - "compiled_at": "2026-10-08T17:10:56Z", + "compiled_at": "2026-10-08T17:37:50Z", "compiler": "crumb-compile/1", - "digest": "sha256:6ccb598103b020ee3061a3998c4c033e3a85dec4a9c4fdd8d101f5cd07148596", + "digest": "sha256:370cd0f9db4c0b88bbe10fb0b5b4de2a546ba8effc132b69b1c3cc73a36dfa40", "evidence_files": 187, "evidence_root": "sha256:27ee0353cb4d4aae2b7dbce2bfe0d9f4ef2a94cd7a4555b84684d59372503280", - "files": 1607, - "generated_at_commit": "0d778066b180ca6e6e9309c98aabf1a93f6ddc65", - "source_tree": "sha256:523400168fdd2bebcc29a30140a7523b82cd275e002085b4fdd2c7b12f573300" + "files": 1608, + "generated_at_commit": "f5dffb632270538ba940d6545d3ac88158104d0b", + "source_tree": "sha256:631aa08a805bc622dde948c74794eb683f219cc984e80dbb51df69730478186a" } }, "history": [], diff --git a/crates/aien-runtime/src/requirements.rs b/crates/aien-runtime/src/requirements.rs index a28d16ec..9710a61e 100644 --- a/crates/aien-runtime/src/requirements.rs +++ b/crates/aien-runtime/src/requirements.rs @@ -106,6 +106,8 @@ pub enum Requirement { MinWords(usize), MaxWords(usize), MinItems(ItemKind, usize), + /// At least this many paragraphs (see [`count_paragraphs`]). + MinParagraphs(usize), /// At least `n` lines (outside fenced code, leading whitespace ignored) that /// start with exactly `prefix`, e.g. the `- [ ]` of a checklist. MinPrefixedLines { @@ -167,6 +169,7 @@ impl Requirement { Requirement::MinWords(n) => format!("at least {n} words"), Requirement::MaxWords(n) => format!("at most {n} words"), Requirement::MinItems(k, n) => format!("at least {n} {}", k.noun()), + Requirement::MinParagraphs(n) => format!("at least {n} paragraphs"), Requirement::MinPrefixedLines { prefix, n } => { format!("at least {n} lines starting with \"{prefix}\"") } @@ -225,6 +228,12 @@ impl Requirement { let c = count_items(*k, content); (c < *n).then(|| format!("{label}, found {c}")) } + Requirement::MinParagraphs(n) => { + let c = count_paragraphs(content); + (c < *n).then(|| { + format!("{label}, found {c} (a paragraph is a block of prose lines; separate paragraphs with a blank line)") + }) + } Requirement::MinPrefixedLines { prefix, n } => { let c = prose_lines(content) .into_iter() @@ -570,6 +579,94 @@ fn prose_lines(s: &str) -> Vec<&str> { out } +/// Paragraphs: maximal runs of consecutive prose lines, following CommonMark +/// closely enough that a document cannot reach the count with lines a reader +/// would not call paragraphs. +/// - A blank line ends a run (whitespace-only lines are blank). +/// - Not prose, and the end of a run: a fence and everything inside it, an ATX +/// heading (`#` to `######`), a thematic break (`---`, `***`, `___`, also +/// spaced). A run directly above a line of only `=` or only `-` is a setext +/// heading and does not count. +/// - A table row (starts with `|`) or an HTML line (starts with `<`) is not +/// prose when it starts a block; inside a run it continues the run. +/// - A list item (`- `, `* `, `+ `, a numbered marker, or a bare `1.`) is not +/// prose, and neither is the text that belongs to it: the following lines up +/// to a blank line, and after a blank line any line indented 2+ spaces. +/// - An indented line (a tab or 4+ spaces) continues a run but never starts +/// one (indented code). +/// - A line with no letter or digit never starts a run. +/// - Everything else is prose, including blockquote lines (`> text`). +pub fn count_paragraphs(content: &str) -> usize { + let mut open: Option<(char, usize)> = None; + // `in_item`: inside a list item; `blank`: a blank line came since the last line. + let (mut count, mut inside, mut in_item, mut blank) = (0, false, false, false); + for l in content.lines() { + let fence = fence_marker(l); + if let Some((c, n)) = open { + if matches!(fence, Some((c2, n2, info)) if c2 == c && n2 >= n && !info) { + open = None; + } + continue; + } + if let Some((c, n, _)) = fence { + open = Some((c, n)); + (inside, in_item) = (false, false); + continue; + } + let t = l.trim(); + if t.is_empty() { + (inside, blank) = (false, true); + continue; + } + let after_blank = std::mem::replace(&mut blank, false); + // A run right above a line of only `=` or only `-` is a setext heading. + let setext = t.chars().all(|x| x == '=') || t.chars().all(|x| x == '-'); + if setext && inside { + count -= 1; + (inside, in_item) = (false, false); + continue; + } + let h = t.chars().take_while(|&c| c == '#').count(); + let heading = (1..=6).contains(&h) && (t.len() == h || t[h..].starts_with([' ', '\t'])); + let rule = t.len() >= 3 + && ['-', '*', '_'] + .iter() + .any(|&c| t.starts_with(c) && t.chars().all(|x| x == c || x == ' ')); + // HTML and table lines start a block, but do not interrupt a paragraph. + let block = !inside && (t.starts_with('|') || t.starts_with('<')); + if heading || rule || setext || block { + (inside, in_item) = (false, false); + continue; + } + let empty_marker = t.len() >= 2 + && t.ends_with(['.', ')']) + && t[..t.len() - 1].chars().all(|c| c.is_ascii_digit()); + if t.starts_with("- ") + || t.starts_with("* ") + || t.starts_with("+ ") + || numbered(t) + || empty_marker + { + (inside, in_item) = (false, true); + continue; + } + // Text right under an item, or indented 2+ after a blank line, belongs to it. + if in_item && (!after_blank || l.starts_with(" ") || l.starts_with('\t')) { + continue; + } + in_item = false; + let indented = l.starts_with('\t') || l.starts_with(" "); + if !inside && (indented || !t.chars().any(char::is_alphanumeric)) { + continue; + } + if !inside { + count += 1; + } + inside = true; + } + count +} + fn count_items(k: ItemKind, s: &str) -> usize { if k == ItemKind::CodeBlocks { return count_code_blocks(s); diff --git a/crates/aien-runtime/src/requirements_extract.rs b/crates/aien-runtime/src/requirements_extract.rs index 75b340a3..d5e7d2e0 100644 --- a/crates/aien-runtime/src/requirements_extract.rs +++ b/crates/aien-runtime/src/requirements_extract.rs @@ -14,7 +14,7 @@ //! | `at most / no more than / not more than / a maximum of / maximum of / max / up to N `, `N or fewer|less ` | `MaxX(N)` | //! | `more than N `, `fewer|less than N ` | `MinX(N+1)`, `MaxX(N-1)` | //! | `N or more|fewer|less`, `N minimum|maximum`, `no longer|shorter than N ` | `MinX(N)` or `MaxX(N)` | -//! | `` = `lines`, `words`; `items`, `steps`, `sections`, `headings`, `questions` (Min only; singular too) | lines, words, list items, numbered steps, headings, questions | +//! | `` = `lines`, `words`; `items`, `steps`, `sections`, `headings`, `questions`, `paragraphs` (Min only; singular too) | lines, words, list items, numbered steps, headings, questions, paragraphs (`MinParagraphs`: runs of prose lines, see `requirements::count_paragraphs`) | //! | `N sentences in|per|for|within every|each section` (or `in every section, ... N sentences`) | `MinSentencesPerSection(N)` | //! | `sections|headings titled|named|called A, B and C`, `sections: A, B and C` | `RequiredHeadings` | //! | `covers|covering A, B and C` (and `to|should|must|will|can cover ...`) | `RequiredTopics` | @@ -73,7 +73,7 @@ //! - emphasis or quotes around the wording (`**at least 20 lines**`) are not //! recognized and the span is UNCERTAIN. //! - a bound word with a number and a noun that is not supported (`at most 5 -//! items`, `at least 3 paragraphs`, `exactly 20 lines`, `about 30 lines`, +//! items`, `at most 3 paragraphs`, `exactly 20 lines`, `about 30 lines`, //! `between 10 and 20 lines`, `35-line` with no verb before it) is UNCERTAIN. //! - a title list whose parsed length differs from a declared count ("6 //! sections titled A, B, C") is UNCERTAIN; so is a list item that is not a @@ -969,6 +969,7 @@ pub fn analyze(goal: &str) -> Extraction { Requirement::MinItems(ItemKind::Sections, v) } (Dir::Min, "questions" | "question") => Requirement::MinItems(ItemKind::Questions, v), + (Dir::Min, "paragraphs" | "paragraph") => Requirement::MinParagraphs(v), _ => return None, }) }; diff --git a/crates/aien-runtime/tests/requirements_enforce_test.rs b/crates/aien-runtime/tests/requirements_enforce_test.rs index 7e550785..ce4e08ab 100644 --- a/crates/aien-runtime/tests/requirements_enforce_test.rs +++ b/crates/aien-runtime/tests/requirements_enforce_test.rs @@ -193,7 +193,7 @@ fn uncertain_spans_are_reported_not_dropped() { "at least 5 items per category", ), ("at most 5 items", "at most 5 items"), - ("at least 3 paragraphs", "at least 3 paragraphs"), + ("at most 3 paragraphs", "at most 3 paragraphs"), ("exactly 20 lines", "exactly 20 lines"), ("about 30 lines", "about 30 lines"), ("between 10 and 20 lines", "between 10 and 20 lines"), diff --git a/crates/aien-runtime/tests/requirements_paragraphs_test.rs b/crates/aien-runtime/tests/requirements_paragraphs_test.rs new file mode 100644 index 00000000..48bfa06f --- /dev/null +++ b/crates/aien-runtime/tests/requirements_paragraphs_test.rs @@ -0,0 +1,137 @@ +//! sc#335: a paragraph count ("at least twelve paragraphs") was refused as +//! uncertain before any model call: there was no paragraph requirement. It is +//! now read as `MinParagraphs` for a minimum bound; an upper bound, an exact or +//! approximate count and a bare count stay UNCERTAIN. +use aien_runtime::requirements::{analyze, count_paragraphs, refusal_reason, Requirement}; + +use Requirement::*; + +/// tasks-oq3-v4 N2, verbatim. +const V4_N2: &str = "Create the file docs/HISTORY.md with a long account of the project history that has at least twelve paragraphs."; +/// tasks-oq3-v3 N2, verbatim. +const V3_N2: &str = "Create the file docs/OVERVIEW.md with a detailed overview of the project that has at least ten paragraphs."; +/// v5 N2 (sc#339), verbatim. +const V5_N2: &str = "I would like a long, detailed history of the marathon as a race, from the legend of ancient Greece to modern city events, saved as docs/marathon-story.md. Please make it at least twelve paragraphs long."; +/// v5 D6 (sc#339), the sentence under test up to its comma (the section clause is sc#331). +const V5_D6: &str = "Please write a warm, longer essay about adopting a rescue cat and save it as docs/rescue-cat-essay.md. It should run to at least 7 paragraphs."; + +fn reqs(goal: &str) -> Vec { + let ex = analyze(goal); + assert!( + ex.uncertain.is_empty(), + "{goal:?} should be read, uncertain: {:?}", + ex.uncertain + ); + ex.requirements +} + +#[test] +fn the_campaign_paragraph_goals_are_read() { + assert_eq!(reqs(V4_N2), [MinParagraphs(12)]); + assert_eq!(reqs(V3_N2), [MinParagraphs(10)]); + assert_eq!(reqs(V5_N2), [MinParagraphs(12)]); + assert_eq!(reqs(V5_D6), [MinParagraphs(7)]); +} + +#[test] +fn the_usual_minimum_wordings_are_read() { + for (goal, n) in [ + ("Write at least 3 paragraphs about bees.", 3), + ("Write 4 or more paragraphs about bees.", 4), + ("Write 5+ paragraphs about bees.", 5), + ("Use a minimum of six paragraphs.", 6), + ("Use no fewer than 2 paragraphs.", 2), + ("Write more than 3 paragraphs.", 4), + ("It must be 8 paragraphs or more.", 8), + ] { + assert_eq!(reqs(goal), [MinParagraphs(n)], "{goal}"); + } +} + +#[test] +fn other_paragraph_wordings_stay_uncertain() { + for goal in [ + "Write at most 3 paragraphs.", + "Write no more than 3 paragraphs.", + "Write exactly 3 paragraphs.", + "Write about 5 paragraphs.", + "Write between 3 and 5 paragraphs.", + "Write three paragraphs.", + "Write at least 3 paragraphs per section.", + "Write at least 3 paragraphs of code.", + "Do not write at least 3 paragraphs.", + "Write 2-3 paragraphs.", + ] { + let ex = analyze(goal); + assert!( + !ex.uncertain.is_empty(), + "{goal:?} should stay uncertain, got {:?}", + ex.requirements + ); + assert!( + !ex.requirements + .iter() + .any(|r| matches!(r, MinParagraphs(_))), + "{goal:?}: {ex:?}" + ); + } +} + +#[test] +fn paragraphs_are_counted_by_the_documented_rule() { + for (doc, n) in [ + ("", 0), + ("one line\n", 1), + ("one\ntwo lines, still one paragraph\n", 1), + ("first\n\nsecond\n\n\nthird\n", 3), + // a heading ends a paragraph and is not one + ("# Title\nfirst\n## Next\nsecond\n", 2), + // list items, table rows and breaks are not prose and end a paragraph + ("intro\n- a\n- b\n\nafter the list\n", 2), + // a line right after an item continues the item (sc#341 review) + ("intro\n- a\n- b\nafter the list\n", 1), + ("- a\n more\n- b\n more\n- c\n more\n", 0), + // indented code never starts a paragraph, but an indented line continues one + (" code1\n code2\n\n code3\n", 0), + ("text\n still the same paragraph\n", 1), + // HTML, setext underlines and lines without letters or digits + ("
\n\n
\n", 0), + ("Title\n=====\nbody\n", 1), + // round 2: a `---` underline is a setext heading too + ("Title\n---\n\nTitle2\n---\n\nTitle3\n---\n", 0), + ("Title\n-\nbody\n", 1), + ("above\n\n---\n\nbelow\n", 2), + // round 2: text indented under an item after a blank line belongs to it + ("- item\n\n indented para\n\n another\n", 0), + ("1. a\n\n para under item\n", 0), + ("- item\n\nnot indented, a paragraph\n", 1), + // round 2: HTML or a pipe inside a run does not split it + ("one\nx\ntwo\n", 1), + ("one\n|two\nthree\n", 1), + ("-\n\n1.\n\n***\n\n* * *\n\n...\n", 0), + // blockquotes are prose; CRLF and non-ASCII text are fine + ("> q1\n\n> q2\n", 2), + ("a\r\n\r\nb\r\n", 2), + ("# h\u{e9}llo\n\n\u{fc}\u{fc}\n", 1), + // an unclosed fence hides the rest of the file + ("before\n\n```\nafter\n\nmore\n", 1), + ("1. step\n2. step\n", 0), + ("| a | b |\n|---|---|\n| 1 | 2 |\n", 0), + ("above\n***\nbelow\n", 2), + // fenced code is not prose and ends a paragraph + ("before\n```\ncode\n\nmore code\n```\nafter\n", 2), + ("~~~\nonly code\n~~~\n", 0), + // "#tag" without a space is text, not a heading + ("#tag line\n", 1), + ] { + assert_eq!(count_paragraphs(doc), n, "{doc:?}"); + } +} + +#[test] +fn the_check_reports_what_it_found() { + let r = [MinParagraphs(3)]; + assert!(refusal_reason(&r, "a\n\nb\n\nc\n").is_none()); + let why = refusal_reason(&r, "a\nb\nc\n").expect("one paragraph only"); + assert!(why.contains("at least 3 paragraphs, found 1"), "{why}"); +} diff --git a/crates/aien-runtime/tests/requirements_test.rs b/crates/aien-runtime/tests/requirements_test.rs index 0c730eeb..31e45268 100644 --- a/crates/aien-runtime/tests/requirements_test.rs +++ b/crates/aien-runtime/tests/requirements_test.rs @@ -62,7 +62,7 @@ fn unsupported_phrasing_is_not_extracted() { for g in [ "write a long document", "at least 20 long lines", - "at least 20 paragraphs", + "at most 20 paragraphs", "at most 5 items", "at least 0 lines", "at least -3 lines", diff --git a/crates/aien-runtime/tests/requirements_usability_test.rs b/crates/aien-runtime/tests/requirements_usability_test.rs index c956e9bf..9b615476 100644 --- a/crates/aien-runtime/tests/requirements_usability_test.rs +++ b/crates/aien-runtime/tests/requirements_usability_test.rs @@ -268,7 +268,7 @@ const SILENT: [&str; 12] = [ ]; /// Goals whose explicit requirements are all read and enforced. -const RECOGNIZED: [&str; 16] = [ +const RECOGNIZED: [&str; 18] = [ "dryrun-tasks-v2/N1", "tasks-oq3-v3/N1", "tasks-oq3-v4/N1", @@ -276,6 +276,9 @@ const RECOGNIZED: [&str; 16] = [ "dryrun-tasks-v2/T4", "tasks-oq3-v3/G2", "tasks-oq3-v3/G3", + // sc#335: "at least ten|twelve paragraphs" is MinParagraphs. + "tasks-oq3-v3/N2", + "tasks-oq3-v4/N2", "tasks-oq3-v4/RT4", "tasks-oq3-v4/RT6", "tasks-oq3-v4/W1", @@ -289,7 +292,7 @@ const RECOGNIZED: [&str; 16] = [ ]; /// Goals still refused as uncertain, each with the span that is reported and why. -const REFUSED: [(&str, &str, &str); 8] = [ +const REFUSED: [(&str, &str, &str); 6] = [ ( "dryrun-tasks-v2/T6", "one section per symptom", @@ -300,21 +303,11 @@ const REFUSED: [(&str, &str, &str); 8] = [ "one fenced shell code example in each of them", "a per-section code example count", ), - ( - "tasks-oq3-v3/N2", - "at least ten paragraphs", - "paragraph counts are not supported", - ), ( "tasks-oq3-v3/R1", "one-sentence greeting", "sentence counts over the whole document are not verified", ), - ( - "tasks-oq3-v4/N2", - "at least twelve paragraphs", - "paragraph counts are not supported", - ), ( "tasks-oq3-v4/R1", "one-sentence thank-you note",