Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/src/domain/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ pub struct Page {
pub diff_id: Option<i64>,
}

/// How many pages each side has, `(base, head)`, from the pages' indexes. A comparison's rows are aligned positions: a
/// reflowed document with pages inserted and removed has more rows than either side has pages (34 base pages and 32
/// head pages aligned over 35 rows), so a page count is never the row count (sc-9184).
pub fn side_counts(indexes: impl IntoIterator<Item = (Option<i32>, Option<i32>)>) -> (usize, usize) {
let (mut base, mut head) = (std::collections::BTreeSet::new(), std::collections::BTreeSet::new());
for (b, h) in indexes {
base.extend(b);
head.extend(h);
}
(base.len(), head.len())
}

/// The page count to show for a snapshot: its head's, or its base's when it has no head (a removed snapshot).
pub const fn shown_count((base, head): (usize, usize)) -> usize {
if head == 0 { base } else { head }
}

struct Row {
snapshot_id: i64,
position: i32,
Expand Down
17 changes: 15 additions & 2 deletions app/src/domain/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ async fn describe(s: &Snapshot) -> laravel::Result<String> {
}
}
let mut out = Vec::new();
// A document whose page count changed leads with both counts: its rows are aligned positions, so "1 page added,
// 3 pages removed" for 34 pages becoming 32 reads as a strange change (sc-9184). Pages added and removed are then
// that alignment's detail, left to the snapshot's page.
let (base_count, head_count) = super::pages::side_counts(pages.iter().map(|p| (p.base_index, p.head_index)));
let recounted = s.kind != "image" && base_count > 0 && head_count > 0 && base_count != head_count;
if recounted {
let (more, word) = if head_count > base_count {
(head_count - base_count, "more")
} else {
(base_count - head_count, "fewer")
};
out.push(format!("{base_count} → {head_count} pages ({more} {word})"));
}
if !changed_pages.is_empty() {
let which = if s.kind == "image" {
"changed".to_string()
Expand All @@ -116,10 +129,10 @@ async fn describe(s: &Snapshot) -> laravel::Result<String> {
which
});
}
if inserted > 0 {
if inserted > 0 && !recounted {
out.push(format!("{inserted} {} added", plural(inserted, "page", "pages")));
}
if deleted > 0 {
if deleted > 0 && !recounted {
out.push(format!("{deleted} {} removed", plural(deleted, "page", "pages")));
}
Ok(if out.is_empty() { "changed".to_string() } else { out.join(", ") })
Expand Down
6 changes: 5 additions & 1 deletion app/src/http/ui/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ pub async fn show_build(auth: Auth<User>, Params((org, project, number)): Params
for s in &list {
let pages = pages_of.get(&s.id).map(Vec::as_slice).unwrap_or_default();
let changed = pages.iter().filter(|p| p.state == "changed").count();
let counts = crate::domain::pages::side_counts(pages.iter().map(|p| (p.base_index, p.head_index)));
let thumb =
thumb_of(s).filter(|sha| stored.contains(sha)).and_then(|sha| store.download_url(&sha).ok());
// Text snapshots: lines added and removed by the change, for the build's list.
Expand All @@ -174,7 +175,10 @@ pub async fn show_build(auth: Auth<User>, Params((org, project, number)): Params
"state": s.state,
"review": s.review,
"error": s.error,
"pages": pages.len(),
// The head's page count (the base's for a removed snapshot), and the base's: a comparison's rows
// are aligned positions, more than either side's pages when pages were added and removed.
"pages": crate::domain::pages::shown_count(counts),
"base_pages": counts.0,
"changed_pages": changed,
"change": sizes.get(&s.id),
"approved_elsewhere": elsewhere.remove(&s.id),
Expand Down
15 changes: 14 additions & 1 deletion app/src/mcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,10 @@ impl VisualCi {
v["url"] = json!(snapshot_url(e));
v
}),
"pages": pages.map(Vec::len),
// The head's page count (the base's for a removed snapshot), and the base's: the comparison's
// positions outnumber both when a reflow added and removed pages (sc-9184).
"pages": pages.map(|p| crate::domain::pages::shown_count(page_counts(p))),
"base_pages": pages.map(|p| page_counts(p).0),
"changed_pages": pages.map(|p| p.iter().filter(|p| p.state == "changed").count()),
"moved_lines": review_detail.as_ref().map_or(0, |d| d.moved.len()),
});
Expand Down Expand Up @@ -1457,6 +1460,7 @@ impl VisualCi {
return Ok(CallToolResult::success(content));
}
let (pages, moved) = (review_detail.pages, review_detail.moved);
let (base_pages, head_pages) = page_counts(&pages);

// Changed pages first; with none (an unchanged snapshot), the first pages, so any snapshot can be looked at.
let wanted: Vec<i32> = match &args.pages {
Expand Down Expand Up @@ -1511,6 +1515,10 @@ impl VisualCi {
"url": url,
"compared_with": compared_with,
"open_comment_threads": open_threads,
// Each side's page count: `pages` lists aligned positions, more than either side has when a reflow added
// and removed pages (sc-9184).
"base_pages": base_pages,
"head_pages": head_pages,
"pages": summary,
"moved_lines": moved.iter().map(|m| json!({ "text": m.text, "from_page": m.from_page + 1, "to_page": m.to_page + 1 })).collect::<Vec<_>>(),
});
Expand Down Expand Up @@ -2362,6 +2370,11 @@ pub async fn handle(principal: Result<Principal, laravel::Error>, request: axum:
}

/// The bearer token of a request, if it sent one.
/// Each side's page count, `(base, head)` ([`crate::domain::pages::side_counts`]).
fn page_counts(pages: &[crate::domain::detail::PageDetail]) -> (usize, usize) {
crate::domain::pages::side_counts(pages.iter().map(|p| (p.base_index, p.head_index)))
}

fn bearer(request: &axum::extract::Request) -> Option<&str> {
oauth_provider::bearer(request.headers().get(axum::http::header::AUTHORIZATION).and_then(|v| v.to_str().ok()))
}
Expand Down
1 change: 1 addition & 0 deletions app/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ mod staff;
mod staff_commands;
mod storage;
mod storage_blobs;
mod summaries;
mod sweep;
mod tokens;
mod ui;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"result": {
"content": [
{
"text": "{\"blocked\":null,\"branch\":\"feat\",\"ci_mode\":\"review\",\"commit\":\"abc\",\"conclusion\":null,\"config\":null,\"created_at\":\"<time:1>\",\"environment\":\"ci\",\"environment_info\":{},\"error\":null,\"id\":\"<uuid:1>\",\"mode\":\"ci\",\"name\":\"default\",\"number\":1,\"open_comment_threads\":[{\"anchor\":{\"kind\":\"point\",\"x\":20.0,\"y\":30.0},\"comments\":[{\"author\":{\"agent\":\"rmcp 3.12.0\",\"kind\":\"agent\",\"name\":null},\"body\":\"Is this change meant?\",\"created_at\":\"<time:2>\",\"id\":\"<uuid:2>\",\"updated_at\":\"<time:3>\"},{\"author\":{\"agent\":\"rmcp 3.12.0\",\"kind\":\"agent\",\"name\":null},\"body\":\"Yes.\",\"created_at\":\"<time:4>\",\"id\":\"<uuid:3>\",\"updated_at\":\"<time:4>\"}],\"id\":\"<uuid:4>\",\"page\":0,\"platform\":\"\",\"resolved\":false,\"resolved_at\":null,\"side\":\"head\",\"snapshot\":\"greeting\",\"started_in_build\":1,\"started_in_snapshot\":\"<uuid:5>\"}],\"passed\":null,\"pr_number\":null,\"project\":\"<org:1>/pdfs\",\"review_state\":\"none\",\"reviewable\":false,\"reviews\":[],\"snapshots\":[{\"approved_elsewhere\":null,\"change\":null,\"changed_pages\":null,\"environment\":\"\",\"error\":null,\"format\":\"text\",\"id\":\"<uuid:5>\",\"kind\":\"text\",\"matched_environment\":null,\"moved_lines\":0,\"name\":\"greeting\",\"pages\":null,\"platform\":\"\",\"review\":null,\"state\":\"pending\",\"url\":\"https://app.visualci.test/<org:1>/pdfs/builds/1/snapshots/<uuid:5>\"}],\"state\":\"receiving\",\"stats\":{\"added\":0,\"changed\":0,\"config\":0,\"failed\":0,\"ignored\":0,\"pending\":0,\"removed\":0,\"total\":0,\"unchanged\":0},\"summary\":null,\"url\":\"https://app.visualci.test/<org:1>/pdfs/builds/1\",\"warnings\":[]}",
"text": "{\"blocked\":null,\"branch\":\"feat\",\"ci_mode\":\"review\",\"commit\":\"abc\",\"conclusion\":null,\"config\":null,\"created_at\":\"<time:1>\",\"environment\":\"ci\",\"environment_info\":{},\"error\":null,\"id\":\"<uuid:1>\",\"mode\":\"ci\",\"name\":\"default\",\"number\":1,\"open_comment_threads\":[{\"anchor\":{\"kind\":\"point\",\"x\":20.0,\"y\":30.0},\"comments\":[{\"author\":{\"agent\":\"rmcp 3.12.0\",\"kind\":\"agent\",\"name\":null},\"body\":\"Is this change meant?\",\"created_at\":\"<time:2>\",\"id\":\"<uuid:2>\",\"updated_at\":\"<time:3>\"},{\"author\":{\"agent\":\"rmcp 3.12.0\",\"kind\":\"agent\",\"name\":null},\"body\":\"Yes.\",\"created_at\":\"<time:4>\",\"id\":\"<uuid:3>\",\"updated_at\":\"<time:4>\"}],\"id\":\"<uuid:4>\",\"page\":0,\"platform\":\"\",\"resolved\":false,\"resolved_at\":null,\"side\":\"head\",\"snapshot\":\"greeting\",\"started_in_build\":1,\"started_in_snapshot\":\"<uuid:5>\"}],\"passed\":null,\"pr_number\":null,\"project\":\"<org:1>/pdfs\",\"review_state\":\"none\",\"reviewable\":false,\"reviews\":[],\"snapshots\":[{\"approved_elsewhere\":null,\"base_pages\":null,\"change\":null,\"changed_pages\":null,\"environment\":\"\",\"error\":null,\"format\":\"text\",\"id\":\"<uuid:5>\",\"kind\":\"text\",\"matched_environment\":null,\"moved_lines\":0,\"name\":\"greeting\",\"pages\":null,\"platform\":\"\",\"review\":null,\"state\":\"pending\",\"url\":\"https://app.visualci.test/<org:1>/pdfs/builds/1/snapshots/<uuid:5>\"}],\"state\":\"receiving\",\"stats\":{\"added\":0,\"changed\":0,\"config\":0,\"failed\":0,\"ignored\":0,\"pending\":0,\"removed\":0,\"total\":0,\"unchanged\":0},\"summary\":null,\"url\":\"https://app.visualci.test/<org:1>/pdfs/builds/1\",\"warnings\":[]}",
"type": "text"
}
],
Expand Down Expand Up @@ -77,6 +77,7 @@
"snapshots": [
{
"approved_elsewhere": null,
"base_pages": null,
"change": null,
"changed_pages": null,
"environment": "",
Expand Down
Loading
Loading