Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
0.9.0 G1: line notes on a diff, sent to the author bot as one batch (#491)
Oleksandr Ivanchenko8540668be368
diff --git a/src/db/dev_line_notes.rs b/src/db/dev_line_notes.rsnew file mode 100644index 00000000..e7671a75--- /dev/null+++ b/src/db/dev_line_notes.rs@@ -0,0 +1,299 @@+//! G1 line notes (0.9.0 wave 1, Juno; `server::dev::line_notes`, release-090/g1-api.md).+//!+//! `dev_line_notes` holds the person's notes on lines of a review card's diff, per PR (`repo`,+//! `ref` = `pr:N`), so they survive reloads and show on the phone. A note is written on one head+//! (`head_sha`) and keeps the lines it was written on (`code`); the server works out where they+//! sit in a newer head. `deleted_at` is a soft delete the person can undo for+//! [`RESTORE_SECS`]; after that the row is purged. `dev_line_note_sends` remembers each send by the+//! client's `send_id` for 24 h, so a retry never posts the batch twice. Kept outside exports like+//! the other Dev mode tables. Add-only.++use super::{Database, DbError};+use sqlx::FromRow;++const TABLES: &[&str] = &[+ "CREATE TABLE IF NOT EXISTS dev_line_notes (+ id TEXT PRIMARY KEY,+ space_id TEXT NOT NULL,+ repo TEXT NOT NULL,+ ref TEXT NOT NULL,+ card_id TEXT NOT NULL,+ path TEXT NOT NULL,+ start_side TEXT NOT NULL,+ start_line INTEGER NOT NULL,+ end_side TEXT NOT NULL,+ end_line INTEGER NOT NULL,+ code TEXT NOT NULL,+ text TEXT NOT NULL,+ state TEXT NOT NULL,+ head_sha TEXT NOT NULL,+ created_at INTEGER NOT NULL,+ updated_at INTEGER NOT NULL,+ sent_at INTEGER,+ message_id TEXT,+ checked_sha TEXT,+ deleted_at INTEGER+ )",+ "CREATE INDEX IF NOT EXISTS dev_line_notes_pr ON dev_line_notes (space_id, repo, ref, created_at)",+ "CREATE TABLE IF NOT EXISTS dev_line_note_sends (+ send_id TEXT PRIMARY KEY,+ space_id TEXT NOT NULL,+ repo TEXT NOT NULL,+ ref TEXT NOT NULL,+ ids TEXT,+ result TEXT NOT NULL,+ created_at INTEGER NOT NULL+ )",+];++/// The tables [`Database::delete_dev_mode_of_space`] clears with the rest of Dev mode.+pub const LINE_NOTE_TABLES: [&str; 2] = ["dev_line_notes", "dev_line_note_sends"];++/// Only the person sees it; it can be edited, deleted and sent.+pub const DRAFT: &str = "draft";+/// Sent to the author bot; waiting for its next push.+pub const SENT: &str = "sent";+/// The next push changed or deleted at least one of its lines.+pub const RESOLVED: &str = "resolved";+/// The next push left all its lines as they were.+pub const OPEN: &str = "open";++/// How long a deleted draft can be restored (the UI's Undo needs 10 s; slow phones get more).+pub const RESTORE_SECS: i64 = 60;+/// How long a send is remembered by its `send_id`.+pub const SEND_ID_HOURS: i64 = 24;++#[derive(Debug, Clone, FromRow, PartialEq)]+pub struct LineNoteRow {+ pub id: String,+ pub space_id: String,+ /// The repo key, as on the card.+ pub repo: String,+ /// The card's ref, `pr:N`.+ #[sqlx(rename = "ref")]+ pub ref_: String,+ /// The card it was written on (that head's card).+ pub card_id: String,+ pub path: String,+ /// `new` or `old`.+ pub start_side: String,+ pub start_line: i64,+ pub end_side: String,+ pub end_line: i64,+ /// The noted lines as written on, `[{kind, old, new, text}]`.+ pub code: String,+ pub text: String,+ /// [`DRAFT`], [`SENT`], [`RESOLVED`] or [`OPEN`].+ pub state: String,+ /// The head it was written on.+ pub head_sha: String,+ pub created_at: i64,+ pub updated_at: i64,+ pub sent_at: Option<i64>,+ pub message_id: Option<String>,+ /// The push a sent note was checked against.+ pub checked_sha: Option<String>,+ pub deleted_at: Option<i64>,+}++const COLS: &str = "id, space_id, repo, ref, card_id, path, start_side, start_line, end_side, end_line, code, text, state, head_sha, \+ created_at, updated_at, sent_at, message_id, checked_sha, deleted_at";++#[derive(Debug, Clone, FromRow, PartialEq)]+pub struct LineNoteSendRow {+ pub send_id: String,+ pub space_id: String,+ pub repo: String,+ #[sqlx(rename = "ref")]+ pub ref_: String,+ /// The ids the request named, sorted, as JSON; none when it sent every draft.+ pub ids: Option<String>,+ /// The response the first send returned.+ pub result: String,+ pub created_at: i64,+}++fn now_ms() -> i64 {+ chrono::Utc::now().timestamp_millis()+}++impl Database {+ pub(super) async fn migrate_line_notes(&self) -> Result<(), DbError> {+ for sql in TABLES {+ sqlx::query(sql).execute(&self.pool).await?;+ }+ Ok(())+ }++ pub async fn insert_line_note(&self, row: &LineNoteRow) -> Result<(), DbError> {+ sqlx::query(&format!("INSERT INTO dev_line_notes ({COLS}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))+ .bind(&row.id)+ .bind(&row.space_id)+ .bind(&row.repo)+ .bind(&row.ref_)+ .bind(&row.card_id)+ .bind(&row.path)+ .bind(&row.start_side)+ .bind(row.start_line)+ .bind(&row.end_side)+ .bind(row.end_line)+ .bind(&row.code)+ .bind(&row.text)+ .bind(&row.state)+ .bind(&row.head_sha)+ .bind(row.created_at)+ .bind(row.updated_at)+ .bind(row.sent_at)+ .bind(&row.message_id)+ .bind(&row.checked_sha)+ .bind(row.deleted_at)+ .execute(&self.pool)+ .await?;+ Ok(())+ }++ /// One note, deleted or not.+ pub async fn line_note(&self, id: &str) -> Result<Option<LineNoteRow>, DbError> {+ Ok(sqlx::query_as(&format!("SELECT {COLS} FROM dev_line_notes WHERE id = ?")).bind(id).fetch_optional(&self.pool).await?)+ }++ /// A PR's notes that aren't deleted, oldest first.+ pub async fn line_notes_of_pr(&self, space_id: &str, repo: &str, ref_: &str) -> Result<Vec<LineNoteRow>, DbError> {+ Ok(sqlx::query_as(&format!(+ "SELECT {COLS} FROM dev_line_notes WHERE space_id = ? AND repo = ? AND ref = ? AND deleted_at IS NULL ORDER BY created_at, rowid"+ ))+ .bind(space_id)+ .bind(repo)+ .bind(ref_)+ .fetch_all(&self.pool)+ .await?)+ }++ /// `(repo, ref, drafts, total)` for every PR of a space with notes that aren't deleted.+ pub async fn line_note_counts(&self, space_id: &str) -> Result<Vec<(String, String, i64, i64)>, DbError> {+ Ok(sqlx::query_as(+ "SELECT repo, ref, SUM(CASE WHEN state = 'draft' THEN 1 ELSE 0 END), COUNT(*) FROM dev_line_notes+ WHERE space_id = ? AND deleted_at IS NULL GROUP BY repo, ref",+ )+ .bind(space_id)+ .fetch_all(&self.pool)+ .await?)+ }++ /// Sets a draft's text. False when it isn't a draft that's not deleted.+ pub async fn set_line_note_text(&self, id: &str, text: &str) -> Result<bool, DbError> {+ let r = sqlx::query("UPDATE dev_line_notes SET text = ?, updated_at = ? WHERE id = ? AND state = 'draft' AND deleted_at IS NULL")+ .bind(text)+ .bind(now_ms())+ .bind(id)+ .execute(&self.pool)+ .await?;+ Ok(r.rows_affected() == 1)+ }++ /// Soft-deletes a draft. False when it isn't a draft that's not deleted.+ pub async fn delete_line_note(&self, id: &str) -> Result<bool, DbError> {+ let now = now_ms();+ let r = sqlx::query("UPDATE dev_line_notes SET deleted_at = ?, updated_at = ? WHERE id = ? AND state = 'draft' AND deleted_at IS NULL")+ .bind(now)+ .bind(now)+ .bind(id)+ .execute(&self.pool)+ .await?;+ Ok(r.rows_affected() == 1)+ }++ /// Soft-deletes every draft of a PR: how many.+ pub async fn discard_line_note_drafts(&self, space_id: &str, repo: &str, ref_: &str) -> Result<u64, DbError> {+ let now = now_ms();+ let r = sqlx::query(+ "UPDATE dev_line_notes SET deleted_at = ?, updated_at = ? WHERE space_id = ? AND repo = ? AND ref = ? AND state = 'draft' AND deleted_at IS NULL",+ )+ .bind(now)+ .bind(now)+ .bind(space_id)+ .bind(repo)+ .bind(ref_)+ .execute(&self.pool)+ .await?;+ Ok(r.rows_affected())+ }++ /// Undoes a soft delete made in the last [`RESTORE_SECS`]. False when there's none.+ pub async fn restore_line_note(&self, id: &str) -> Result<bool, DbError> {+ let now = now_ms();+ let r = sqlx::query("UPDATE dev_line_notes SET deleted_at = NULL, updated_at = ? WHERE id = ? AND state = 'draft' AND deleted_at >= ?")+ .bind(now)+ .bind(id)+ .bind(now - RESTORE_SECS * 1000)+ .execute(&self.pool)+ .await?;+ Ok(r.rows_affected() == 1)+ }++ /// Removes deleted notes past the restore window and sends past [`SEND_ID_HOURS`].+ pub async fn purge_line_notes(&self) -> Result<(), DbError> {+ let now = now_ms();+ sqlx::query("DELETE FROM dev_line_notes WHERE deleted_at IS NOT NULL AND deleted_at < ?").bind(now - RESTORE_SECS * 1000).execute(&self.pool).await?;+ sqlx::query("DELETE FROM dev_line_note_sends WHERE created_at < ?").bind(now - SEND_ID_HOURS * 3_600_000).execute(&self.pool).await?;+ Ok(())+ }++ /// Marks drafts sent in message `message_id`. Only drafts that aren't deleted change: the ids+ /// that did.+ pub async fn mark_line_notes_sent(&self, ids: &[String], message_id: &str, sent_at: i64) -> Result<Vec<String>, DbError> {+ let mut tx = self.pool.begin().await?;+ let mut done = Vec::new();+ for id in ids {+ let r = sqlx::query(+ "UPDATE dev_line_notes SET state = 'sent', sent_at = ?, message_id = ?, updated_at = ? WHERE id = ? AND state = 'draft' AND deleted_at IS NULL",+ )+ .bind(sent_at)+ .bind(message_id)+ .bind(sent_at)+ .bind(id)+ .execute(&mut *tx)+ .await?;+ if r.rows_affected() == 1 {+ done.push(id.clone());+ }+ }+ tx.commit().await?;+ Ok(done)+ }++ /// Sets a sent note's state after a push (`resolved` or `open`), once: false when it was+ /// already checked or isn't sent.+ pub async fn check_line_note(&self, id: &str, state: &str, checked_sha: &str) -> Result<bool, DbError> {+ let r = sqlx::query("UPDATE dev_line_notes SET state = ?, checked_sha = ?, updated_at = ? WHERE id = ? AND state = 'sent'")+ .bind(state)+ .bind(checked_sha)+ .bind(now_ms())+ .bind(id)+ .execute(&self.pool)+ .await?;+ Ok(r.rows_affected() == 1)+ }++ /// A send remembered by its `send_id` (any age; the caller checks it).+ pub async fn line_note_send(&self, send_id: &str) -> Result<Option<LineNoteSendRow>, DbError> {+ Ok(sqlx::query_as("SELECT send_id, space_id, repo, ref, ids, result, created_at FROM dev_line_note_sends WHERE send_id = ?")+ .bind(send_id)+ .fetch_optional(&self.pool)+ .await?)+ }++ pub async fn insert_line_note_send(&self, row: &LineNoteSendRow) -> Result<(), DbError> {+ sqlx::query("INSERT OR REPLACE INTO dev_line_note_sends (send_id, space_id, repo, ref, ids, result, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)")+ .bind(&row.send_id)+ .bind(&row.space_id)+ .bind(&row.repo)+ .bind(&row.ref_)+ .bind(&row.ids)+ .bind(&row.result)+ .bind(row.created_at)+ .execute(&self.pool)+ .await?;+ Ok(())+ }+}diff --git a/src/db/dev_mode.rs b/src/db/dev_mode.rsindex 8202ffae..96228c1c 100644--- a/src/db/dev_mode.rs+++ b/src/db/dev_mode.rs@@ -327,6 +327,8 @@ impl Database {self.migrate_prepush().await?;// 0.9.0: CI and review comments back to the author bot (dev_fix_it.rs)self.migrate_fix_it().await?;+ // 0.9.0: G1 line notes on a review's diff (dev_line_notes.rs)+ self.migrate_line_notes().await?;Ok(())}@@ -650,7 +652,7 @@ impl Database {async fn delete_dev_rows(&self, space_id: &str) -> Result<(), DbError> {let mut tx = self.pool.begin().await?;let tables = ["dev_mode_spaces", "dev_repos", "dev_cards", "dev_backups", "dev_model_calls", "dev_prod_connections"];- for t in tables.into_iter().chain(super::dev_ticket_briefs::BRIEF_TABLES).chain(super::dev_prepush::PREPUSH_TABLES).chain(super::dev_fix_it::FIX_TABLES) {+ for t in tables.into_iter().chain(super::dev_ticket_briefs::BRIEF_TABLES).chain(super::dev_prepush::PREPUSH_TABLES).chain(super::dev_fix_it::FIX_TABLES).chain(super::dev_line_notes::LINE_NOTE_TABLES) {sqlx::query(&format!("DELETE FROM {t} WHERE space_id = ?")).bind(space_id).execute(&mut *tx).await?;}tx.commit().await?;diff --git a/src/db/mod.rs b/src/db/mod.rsindex 1c330225..d2ecb40f 100644--- a/src/db/mod.rs+++ b/src/db/mod.rs@@ -48,6 +48,7 @@ pub mod dev_mode;pub mod dev_ticket_briefs;pub mod dev_fix_it;pub mod dev_prepush;+pub mod dev_line_notes;pub mod spend;pub mod agent_connections;pub mod dev_jira;diff --git a/src/server/dev/line_notes.rs b/src/server/dev/line_notes.rsnew file mode 100644index 00000000..61292748--- /dev/null+++ b/src/server/dev/line_notes.rs@@ -0,0 +1,1162 @@+//! G1 line notes on a review's diff, sent to the author bot as one message (0.9.0 wave 1, Juno;+//! release-090/g1-api.md, Mira's designs/g1-line-notes.md; Iris builds the UI).+//!+//! * `GET /api/dev/cards/:id/changes` serves the diff of the PR's newest review card as parsed+//! hunks. It is the card's own files: the patches the poller read for that head, after the+//! secret scan hid what it found and the 60k cut. A file the cut dropped shows as+//! `omitted: "too_large"` (the UI links to the forge); nothing here reads the forge, so a line the+//! scan hid never comes back through this route.+//! * Notes belong to the PR (space, repo, `pr:N`), not to one head. A note keeps the head it was+//! written on and the lines as they were (`code`); [`locate`] finds them in a newer head by+//! their text (`at_head`).+//! * Send posts ONE message from the person in the author bot's 1:1, the same path as a chat+//! message (it queues when the bot is busy), with `meta.kind = "pr_notes"`. A repeat `send_id`+//! returns the first result and posts nothing.+//! * On the next push (a new review card for the PR), each sent note becomes `resolved` when at+//! least one of its lines was edited or deleted, else `open` ([`after_push`]).+//! * Every change fires the card's `dev_card` event, so an open desk refetches.+//!+//! Only the person reads or writes notes (agent tokens are refused), and nothing here writes to the+//! forge.++// Route helpers return the refusal `Response` itself, as in `continuity.rs`.+#![allow(clippy::result_large_err)]++use super::facts::{DevFacts, FileDiff};+use crate::drivers::events::RuntimeEvent;+use super::routes::REFUSAL;+use crate::db::dev_line_notes::{LineNoteRow, LineNoteSendRow, DRAFT, OPEN, RESOLVED, SEND_ID_HOURS, SENT};+use crate::db::dev_mode::{DevCardRow, LANDED, REVIEW, STALE};+use crate::db::models::Kwant;+use crate::db::spaces::SpaceId;+use crate::server::spaces::active_space;+use crate::server::AppState;+use axum::extract::{Path, State};+use axum::http::{HeaderMap, StatusCode};+use axum::response::{IntoResponse, Response};+use axum::Json;+use chrono::{TimeZone, Utc};+use serde::{Deserialize, Serialize};+use serde_json::{json, Value};+use std::collections::{HashMap, HashSet};+use std::sync::{Arc, Mutex, OnceLock};++/// Most characters in a note.+pub const TEXT_CHARS: usize = 4_000;+/// Most diff lines one note covers.+pub const RANGE_LINES: usize = 200;+/// Most notes in one send.+pub const SEND_MAX: usize = 20;+/// A code line in the message is cut at this many characters.+pub const CODE_CHARS: usize = 160;+/// A range over this many lines sends its first [`HEAD_LINES`] and last [`TAIL_LINES`].+pub const RANGE_SHOWN: usize = 12;+const HEAD_LINES: usize = 6;+const TAIL_LINES: usize = 3;+/// A file with more changed lines than this starts collapsed.+pub const BIG_LINES: u64 = 400;++const LOCKFILES: [&str; 10] = [+ "Cargo.lock",+ "package-lock.json",+ "npm-shrinkwrap.json",+ "yarn.lock",+ "pnpm-lock.yaml",+ "Gemfile.lock",+ "poetry.lock",+ "composer.lock",+ "go.sum",+ "bun.lockb",+];++// ---------------------------------------------------------------------------------------------+// The diff: pure, and tested below++/// One line of a unified diff: `ctx`, `add` or `del`, its old and new numbers, its text.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct DLine {+ pub kind: String,+ pub old: Option<u32>,+ pub new: Option<u32>,+ pub text: String,+}++#[derive(Debug, Clone, PartialEq, Eq, Serialize)]+pub struct DHunk {+ pub header: String,+ pub lines: Vec<DLine>,+}++/// A note's first or last line: the side (`new` or `old`) and its number on that side.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct End {+ pub side: String,+ pub line: u32,+}++/// The `@@ -a,b +c,d @@` numbers: where the old and new sides start.+fn hunk_starts(header: &str) -> Option<(u32, u32)> {+ let rest = header.strip_prefix("@@ -")?;+ let (old, rest) = rest.split_once(' ')?;+ let new = rest.strip_prefix('+')?.split(' ').next()?;+ let first = |s: &str| s.split(',').next()?.parse::<u32>().ok();+ Some((first(old)?, first(new)?))+}++/// The hunks of a unified diff patch (GitHub's `patch` field), with old and new line numbers.+/// Lines before the first `@@`, and `\ No newline at end of file`, are left out.+pub fn parse_patch(patch: &str) -> Vec<DHunk> {+ let mut out: Vec<DHunk> = Vec::new();+ let (mut old, mut new) = (0u32, 0u32);+ for raw in patch.lines() {+ if raw.starts_with("@@") {+ let Some((o, n)) = hunk_starts(raw) else { continue };+ (old, new) = (o, n);+ out.push(DHunk { header: raw.to_string(), lines: Vec::new() });+ continue;+ }+ let Some(h) = out.last_mut() else { continue };+ let (kind, text) = match raw.as_bytes().first() {+ Some(b'+') => ("add", &raw[1..]),+ Some(b'-') => ("del", &raw[1..]),+ Some(b' ') => ("ctx", &raw[1..]),+ Some(b'\\') => continue,+ _ => ("ctx", raw),+ };+ let line = match kind {+ "add" => DLine { kind: kind.into(), old: None, new: Some(new), text: text.into() },+ "del" => DLine { kind: kind.into(), old: Some(old), new: None, text: text.into() },+ _ => DLine { kind: kind.into(), old: Some(old), new: Some(new), text: text.into() },+ };+ if line.old.is_some() {+ old += 1;+ }+ if line.new.is_some() {+ new += 1;+ }+ h.lines.push(line);+ }+ out+}++/// Where `e` is in a hunk: the line with that number on that side.+fn find(h: &DHunk, e: &End) -> Option<usize> {+ h.lines.iter().position(|l| is_at(l, e))+}++/// Why a range can't be noted.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum RangeError {+ NotInDiff,+ CrossHunk,+ TooBig,+}++/// The unified lines from `start` to `end` (either order), inclusive, in one hunk.+pub fn select(hunks: &[DHunk], start: &End, end: &End) -> Result<Vec<DLine>, RangeError> {+ let at = |e: &End| hunks.iter().enumerate().find_map(|(i, h)| find(h, e).map(|p| (i, p)));+ let (Some((hs, ps)), Some((he, pe))) = (at(start), at(end)) else { return Err(RangeError::NotInDiff) };+ if hs != he {+ return Err(RangeError::CrossHunk);+ }+ let (a, b) = (ps.min(pe), ps.max(pe));+ if b - a + 1 > RANGE_LINES {+ return Err(RangeError::TooBig);+ }+ Ok(hunks[hs].lines[a..=b].to_vec())+}++/// The end of a line on the side the note prefers, else the side it has.+fn end_of(l: &DLine, prefer: &str) -> End {+ match (prefer, l.new, l.old) {+ ("new", Some(n), _) => End { side: "new".into(), line: n },+ ("old", _, Some(o)) => End { side: "old".into(), line: o },+ (_, Some(n), _) if l.kind != "del" => End { side: "new".into(), line: n },+ (_, _, Some(o)) => End { side: "old".into(), line: o },+ (_, Some(n), _) => End { side: "new".into(), line: n },+ _ => End { side: prefer.into(), line: 0 },+ }+}++/// Where the noted lines `code` are in `hunks`: the same lines (kind and text) one after another,+/// the place nearest line `near` when there are several. Their first and last ends, on the sides+/// the note named. None when they aren't there: at least one was edited or deleted.+pub fn locate(hunks: &[DHunk], code: &[DLine], start_side: &str, end_side: &str, near: u32) -> Option<(End, End)> {+ if code.is_empty() {+ return None;+ }+ let same = |a: &DLine, b: &DLine| a.kind == b.kind && a.text == b.text;+ let mut best: Option<(u32, End, End)> = None;+ for h in hunks {+ if h.lines.len() < code.len() {+ continue;+ }+ for i in 0..=(h.lines.len() - code.len()) {+ if !h.lines[i..i + code.len()].iter().zip(code).all(|(a, b)| same(a, b)) {+ continue;+ }+ let s = end_of(&h.lines[i], start_side);+ let e = end_of(&h.lines[i + code.len() - 1], end_side);+ let d = s.line.abs_diff(near);+ if best.as_ref().is_none_or(|(bd, _, _)| d < *bd) {+ best = Some((d, s, e));+ }+ }+ }+ best.map(|(_, s, e)| (s, e))+}++/// `src/a.tsx:40-41` for the noted lines: new-side numbers when any line has one (removed lines+/// have none), else old.+pub fn at_text(path: &str, code: &[DLine]) -> String {+ let news: Vec<u32> = code.iter().filter(|l| l.kind != "del").filter_map(|l| l.new).collect();+ let nums = if news.is_empty() { code.iter().filter_map(|l| l.old).collect() } else { news };+ match (nums.iter().min(), nums.iter().max()) {+ (Some(a), Some(b)) if a != b => format!("{path}:{a}-{b}"),+ (Some(a), _) => format!("{path}:{a}"),+ _ => path.to_string(),+ }+}++/// What a file's patch shows: `binary` (no patch, no line counts), `too_large` (no patch but+/// changed lines: GitHub's limit or the card's 60k cut), or none.+pub fn omitted(f: &FileDiff) -> Option<&'static str> {+ if f.patch.is_some() || (f.status == "renamed" && f.additions + f.deletions == 0) {+ return None;+ }+ Some(if f.additions + f.deletions == 0 { "binary" } else { "too_large" })+}++/// A file that starts collapsed: many changed lines, or a lockfile.+pub fn is_big(f: &FileDiff) -> bool {+ let name = f.path.rsplit('/').next().unwrap_or(&f.path);+ f.additions + f.deletions > BIG_LINES || LOCKFILES.contains(&name)+}++/// One file of `/changes`.+pub fn file_json(f: &FileDiff, notes: usize) -> Value {+ json!({+ "path": f.path,+ "old_path": f.previous_path,+ "status": f.status,+ "additions": f.additions,+ "deletions": f.deletions,+ "omitted": omitted(f),+ "big": is_big(f),+ "notes": notes,+ "hunks": f.patch.as_deref().map(parse_patch).unwrap_or_default(),+ })+}++fn cut_line(text: &str) -> String {+ if text.chars().count() <= CODE_CHARS {+ return text.to_string();+ }+ let mut s: String = text.chars().take(CODE_CHARS).collect();+ s.push('…');+ s+}++/// The noted lines as the message quotes them: ` 40 | code`, a removed line as+/// ` 38 (removed) | code`; a range over [`RANGE_SHOWN`] lines keeps its first and last lines.+fn quoted(code: &[DLine]) -> Vec<String> {+ let line = |l: &DLine| match (l.kind.as_str(), l.new, l.old) {+ ("del", _, Some(o)) => format!(" {o} (removed) | {}", cut_line(&l.text)),+ (_, Some(n), _) => format!(" {n} | {}", cut_line(&l.text)),+ (_, _, Some(o)) => format!(" {o} | {}", cut_line(&l.text)),+ _ => format!(" | {}", cut_line(&l.text)),+ };+ if code.len() <= RANGE_SHOWN {+ return code.iter().map(line).collect();+ }+ let mut out: Vec<String> = code[..HEAD_LINES].iter().map(line).collect();+ out.push(" …".into());+ out.extend(code[code.len() - TAIL_LINES..].iter().map(line));+ out+}++/// One note as it goes out.+#[derive(Debug, Clone)]+pub struct Outgoing {+ pub path: String,+ pub code: Vec<DLine>,+ pub text: String,+}++/// The message the author bot gets (Mira's block).+pub fn message_text(pr: Option<u64>, branch: Option<&str>, head: &str, notes: &[Outgoing]) -> String {+ let n = notes.len();+ let mut on = match pr {+ Some(p) => format!("#{p}"),+ None => "the branch".to_string(),+ };+ if let Some(b) = branch.filter(|b| !b.is_empty()) {+ on.push(' ');+ on.push_str(b);+ }+ let head7: String = head.chars().take(7).collect();+ let mut out = format!("{n} {} on {on} (at {head7}), from the review:\n", if n == 1 { "note" } else { "notes" });+ for (i, note) in notes.iter().enumerate() {+ out.push_str(&format!("\n{}. {}\n", i + 1, at_text(¬e.path, ¬e.code)));+ for l in quoted(¬e.code) {+ out.push_str(&l);+ out.push('\n');+ }+ let mut lines = note.text.trim().lines();+ out.push_str(&format!(" → {}\n", lines.next().unwrap_or("")));+ for l in lines {+ out.push_str(&format!(" {l}\n"));+ }+ }+ out.push_str("\nPush to the same branch when done; kwant marks each note resolved when its lines change.");+ out+}++/// A note as the routes return it. `current` is the PR's newest head and its files (when known).+pub fn note_json(row: &LineNoteRow, current: Option<(&str, &HashMap<String, Vec<DHunk>>)>) -> Value {+ let code: Vec<DLine> = serde_json::from_str(&row.code).unwrap_or_default();+ let start = End { side: row.start_side.clone(), line: row.start_line as u32 };+ let end = End { side: row.end_side.clone(), line: row.end_line as u32 };+ let (at_head, seen_sha) = match current {+ Some((head, _)) if head == row.head_sha => (Some(json!({"start": start, "end": end})), None),+ Some((head, files)) => {+ let at = files.get(&row.path).and_then(|h| locate(h, &code, &row.start_side, &row.end_side, row.start_line as u32));+ (at.map(|(s, e)| json!({"start": s, "end": e})), Some(head.to_string()))+ }+ None => (None, None),+ };+ let ms = |v: Option<i64>| v.and_then(|t| Utc.timestamp_millis_opt(t).single());+ json!({+ "id": row.id,+ "path": row.path,+ "start": start,+ "end": end,+ "code": code,+ "text": row.text,+ "state": row.state,+ "head_sha": row.head_sha,+ "created_at": ms(Some(row.created_at)),+ "updated_at": ms(Some(row.updated_at)),+ "sent_at": ms(row.sent_at),+ "message_id": row.message_id,+ "checked_sha": row.checked_sha,+ "at_head": at_head,+ "seen_sha": seen_sha,+ })+}++/// The review summary line's numbers: `{total, changed, open, sha}` once a push checked any sent+/// note, else none. `sha` is the newest push that checked one.+pub fn summary(rows: &[LineNoteRow]) -> Option<Value> {+ let checked: Vec<&LineNoteRow> = rows.iter().filter(|r| r.state == RESOLVED || r.state == OPEN).collect();+ let newest = checked.iter().max_by_key(|r| r.updated_at)?;+ let sha = newest.checked_sha.clone();+ let of_sha: Vec<&&LineNoteRow> = checked.iter().filter(|r| r.checked_sha == sha).collect();+ Some(json!({+ "total": of_sha.len(),+ "changed": of_sha.iter().filter(|r| r.state == RESOLVED).count(),+ "open": of_sha.iter().filter(|r| r.state == OPEN).count(),+ "sha": sha,+ }))+}++/// Whether a sent note's lines changed in a newer head's files: none when that file's diff isn't+/// known there (cut), so it's checked on a later push instead. A file no longer in the PR's diff+/// lost the noted change, so that counts as changed.+pub fn changed(row: &LineNoteRow, files: &[FileDiff]) -> Option<bool> {+ let code: Vec<DLine> = serde_json::from_str(&row.code).ok()?;+ let Some(f) = files.iter().find(|f| f.path == row.path) else { return Some(true) };+ let patch = f.patch.as_deref()?;+ Some(locate(&parse_patch(patch), &code, &row.start_side, &row.end_side, row.start_line as u32).is_none())+}++// ---------------------------------------------------------------------------------------------+// The PR a card is about++/// A card's PR: its newest review card and that card's facts.+struct Pr {+ space: SpaceId,+ current: DevCardRow,+ facts: DevFacts,+}++impl Pr {+ fn files(&self) -> HashMap<String, Vec<DHunk>> {+ self.facts.files.iter().filter_map(|f| Some((f.path.clone(), parse_patch(f.patch.as_deref()?)))).collect()+ }+}++fn err(status: StatusCode, code: &str, message: &str) -> Response {+ (status, Json(json!({"error": message, "code": code}))).into_response()+}++fn internal(e: impl std::fmt::Display) -> Response {+ tracing::warn!("dev: line notes: {e}");+ err(StatusCode::INTERNAL_SERVER_ERROR, "internal", "Dev mode could not read or save that")+}++/// The review card `card_id` and its PR's newest card, for the person only.+async fn pr_of(state: &AppState, headers: &HeaderMap, card_id: &str) -> Result<Pr, Response> {+ super::super::mcp_suggest::refuse_agent_tokens(state, headers, REFUSAL).await?;+ let row = match state.db.dev_card(card_id.trim()).await {+ Ok(Some(r)) => r,+ Ok(None) => return Err(err(StatusCode::NOT_FOUND, "not_found", "no such card")),+ Err(e) => return Err(internal(e)),+ };+ let space = active_space(state, &row.space_id).await?;+ if row.kind != REVIEW {+ return Err(err(StatusCode::NOT_FOUND, "not_review_card", "only a review card has changes and notes"));+ }+ let current = state.db.dev_cards_of_ref_in(&row.space_id, &row.repo, REVIEW, &row.ref_).await.map_err(internal)?.into_iter().next().unwrap_or(row);+ let facts: DevFacts = serde_json::from_str(¤t.facts).map_err(|e| internal(format!("card {} facts: {e}", current.id)))?;+ Ok(Pr { space, current, facts })+}++/// `open`, `merged` (a landed card of the same repo came from this PR) or `closed` (its newest+/// card went stale with no landed card: closed, gone back to draft or quiet).+async fn pr_state(state: &AppState, pr: &Pr) -> &'static str {+ if pr.current.status != STALE {+ return "open";+ }+ let landed = state.db.dev_cards_in(&pr.current.space_id, Some(pr.current.repo.as_str()), LANDED, None, 200).await.unwrap_or_default();+ let merged = pr.facts.pr.is_some()+ && landed.iter().any(|c| serde_json::from_str::<DevFacts>(&c.facts).is_ok_and(|f| f.pr == pr.facts.pr));+ if merged {+ "merged"+ } else {+ "closed"+ }+}++/// The bot the notes go to: the review's author bot, else the bot the head branch names+/// (`pixel/…`), when it is a bot of the space now.+fn author_bot<'a>(facts: &DevFacts, kwants: &'a [Kwant]) -> Option<&'a Kwant> {+ if let Some(b) = &facts.author.bot {+ if let Some(k) = kwants.iter().find(|k| k.id == b.id) {+ return Some(k);+ }+ }+ let (prefix, _) = facts.head_branch.as_deref()?.split_once('/')?;+ let prefix = prefix.to_lowercase();+ let slug = |n: &str| n.trim().to_lowercase().split_whitespace().collect::<Vec<_>>().join("-");+ kwants.iter().find(|k| !k.name.trim().is_empty() && slug(&k.name) == prefix)+}++fn card_lock(key: &str) -> Arc<tokio::sync::Mutex<()>> {+ static LOCKS: OnceLock<Mutex<HashMap<String, Arc<tokio::sync::Mutex<()>>>>> = OnceLock::new();+ let mut map = LOCKS.get_or_init(Default::default).lock().unwrap_or_else(|e| e.into_inner());+ map.retain(|_, l| Arc::strong_count(l) > 1);+ map.entry(key.to_string()).or_default().clone()+}++fn pr_key(row: &DevCardRow) -> String {+ format!("{}\u{1f}{}\u{1f}{}", row.space_id, row.repo, row.ref_)+}++/// Fires the PR's newest card's `dev_card` event, so an open desk refetches. Only the event:+/// [`super::notify_card`]'s ticket sync is about the card, which a note doesn't change.+fn changed_event(state: &AppState, pr: &Pr) {+ let _ = state.event_sender.send(RuntimeEvent::DevCard {+ space_id: pr.current.space_id.clone(),+ id: pr.current.id.clone(),+ kind: pr.current.kind.clone(),+ status: pr.current.status.clone(),+ });+}++fn new_id() -> String {+ format!("ln_{}", uuid::Uuid::new_v4().simple())+}++fn now_ms() -> i64 {+ Utc::now().timestamp_millis()+}++// ---------------------------------------------------------------------------------------------+// Routes++/// `GET /api/dev/cards/:id/changes`+pub async fn changes(State(state): State<AppState>, headers: HeaderMap, Path(card_id): Path<String>) -> Response {+ let pr = match pr_of(&state, &headers, &card_id).await {+ Ok(p) => p,+ Err(r) => return r,+ };+ let notes = match state.db.line_notes_of_pr(&pr.current.space_id, &pr.current.repo, &pr.current.ref_).await {+ Ok(n) => n,+ Err(e) => return internal(e),+ };+ let kwants = state.db.list_kwants(&pr.space).await.unwrap_or_default();+ let author = author_bot(&pr.facts, &kwants);+ let files: Vec<Value> = pr.facts.files.iter().map(|f| file_json(f, notes.iter().filter(|n| n.path == f.path).count())).collect();+ let unread = pr.facts.files.iter().filter(|f| omitted(f) == Some("too_large")).count();+ let cut = pr.facts.cut.as_ref().map(|_| json!({"files_not_read": unread}));+ Json(json!({+ "card_id": pr.current.id,+ "repo": pr.facts.repo,+ "pr": pr.facts.pr,+ "head_sha": pr.current.head_sha,+ "pr_url": pr.facts.links.pr,+ "pr_state": pr_state(&state, &pr).await,+ "author_bot": author.map(|k| json!({"id": k.id, "name": k.name})),+ "author_login": pr.facts.author.login,+ "files": files,+ "cut": cut,+ }))+ .into_response()+}++/// `GET /api/dev/cards/:id/notes`+pub async fn list(State(state): State<AppState>, headers: HeaderMap, Path(card_id): Path<String>) -> Response {+ let pr = match pr_of(&state, &headers, &card_id).await {+ Ok(p) => p,+ Err(r) => return r,+ };+ // A push the poller saw before this build ran: check it now+ check_sent(&state, &pr).await;+ notes_response(&state, &pr).await+}++async fn notes_response(state: &AppState, pr: &Pr) -> Response {+ let rows = match state.db.line_notes_of_pr(&pr.current.space_id, &pr.current.repo, &pr.current.ref_).await {+ Ok(n) => n,+ Err(e) => return internal(e),+ };+ let files = pr.files();+ let notes: Vec<Value> = rows.iter().map(|r| note_json(r, Some((&pr.current.head_sha, &files)))).collect();+ Json(json!({"notes": notes, "summary": summary(&rows)})).into_response()+}++#[derive(Deserialize)]+pub struct NewNote {+ pub path: String,+ pub start: End,+ pub end: End,+ pub head_sha: String,+ pub text: String,+}++fn check_text(text: &str) -> Result<String, Response> {+ let t = text.trim();+ if t.is_empty() {+ return Err(err(StatusCode::UNPROCESSABLE_ENTITY, "empty", "Write the note first"));+ }+ if t.chars().count() > TEXT_CHARS {+ return Err(err(StatusCode::UNPROCESSABLE_ENTITY, "too_long", "A note is at most 4000 characters"));+ }+ Ok(t.to_string())+}++/// Notes can't be added or sent once the PR is merged or closed.+async fn writable(state: &AppState, pr: &Pr) -> Result<(), Response> {+ match pr_state(state, pr).await {+ "merged" => Err(err(StatusCode::CONFLICT, "pr_merged", "This PR is merged. Notes are read-only.")),+ "closed" => Err(err(StatusCode::CONFLICT, "pr_closed", "This PR is closed. Notes are read-only.")),+ _ => Ok(()),+ }+}++/// `POST /api/dev/cards/:id/notes`+pub async fn create(State(state): State<AppState>, headers: HeaderMap, Path(card_id): Path<String>, body: Option<Json<Value>>) -> Response {+ let Some(Ok(b)) = body.map(|Json(v)| serde_json::from_value::<NewNote>(v)) else {+ return err(StatusCode::BAD_REQUEST, "bad_request", "send {path, start, end, head_sha, text}");+ };+ let pr = match pr_of(&state, &headers, &card_id).await {+ Ok(p) => p,+ Err(r) => return r,+ };+ if let Err(r) = writable(&state, &pr).await {+ return r;+ }+ let text = match check_text(&b.text) {+ Ok(t) => t,+ Err(r) => return r,+ };+ if b.head_sha.trim() != pr.current.head_sha {+ return (+ StatusCode::CONFLICT,+ Json(json!({"error": "The PR has a newer push. Load the changes again.", "code": "stale_head", "head_sha": pr.current.head_sha})),+ )+ .into_response();+ }+ let Some(file) = pr.facts.files.iter().find(|f| f.path == b.path) else {+ return err(StatusCode::UNPROCESSABLE_ENTITY, "not_in_diff", "Those lines aren't in this diff");+ };+ let hunks = parse_patch(file.patch.as_deref().unwrap_or(""));+ let code = match select(&hunks, &b.start, &b.end) {+ Ok(c) => c,+ Err(RangeError::NotInDiff) => return err(StatusCode::UNPROCESSABLE_ENTITY, "not_in_diff", "Those lines aren't in this diff"),+ Err(RangeError::CrossHunk) => return err(StatusCode::UNPROCESSABLE_ENTITY, "cross_hunk", "A note covers lines of one hunk"),+ Err(RangeError::TooBig) => return err(StatusCode::UNPROCESSABLE_ENTITY, "range_too_big", "A note covers at most 200 lines"),+ };+ // Store the ends in diff order, each on the side the person picked for it+ let (first, last) = if is_at(&code[0], &b.start) { (b.start.clone(), b.end.clone()) } else { (b.end.clone(), b.start.clone()) };+ let now = now_ms();+ let row = LineNoteRow {+ id: new_id(),+ space_id: pr.current.space_id.clone(),+ repo: pr.current.repo.clone(),+ ref_: pr.current.ref_.clone(),+ card_id: pr.current.id.clone(),+ path: b.path.clone(),+ start_side: first.side,+ start_line: first.line as i64,+ end_side: last.side,+ end_line: last.line as i64,+ code: serde_json::to_string(&code).unwrap_or_else(|_| "[]".into()),+ text,+ state: DRAFT.into(),+ head_sha: pr.current.head_sha.clone(),+ created_at: now,+ updated_at: now,+ sent_at: None,+ message_id: None,+ checked_sha: None,+ deleted_at: None,+ };+ if let Err(e) = state.db.insert_line_note(&row).await {+ return internal(e);+ }+ let _ = state.db.purge_line_notes().await;+ changed_event(&state, &pr);+ let files = pr.files();+ (StatusCode::CREATED, Json(note_json(&row, Some((&pr.current.head_sha, &files))))).into_response()+}++/// Whether `l` is the line `e` names.+fn is_at(l: &DLine, e: &End) -> bool {+ match e.side.as_str() {+ "new" => l.new == Some(e.line),+ "old" => l.old == Some(e.line),+ _ => false,+ }+}++/// A note of a card's PR, with the PR.+async fn note_of(state: &AppState, headers: &HeaderMap, note_id: &str) -> Result<(LineNoteRow, Pr), Response> {+ super::super::mcp_suggest::refuse_agent_tokens(state, headers, REFUSAL).await?;+ let row = match state.db.line_note(note_id.trim()).await {+ Ok(Some(r)) => r,+ Ok(None) => return Err(err(StatusCode::NOT_FOUND, "not_found", "no such note")),+ Err(e) => return Err(internal(e)),+ };+ let pr = pr_of(state, headers, &row.card_id).await?;+ Ok((row, pr))+}++#[derive(Deserialize)]+pub struct EditNote {+ pub text: String,+}++/// `PATCH /api/dev/notes/:note_id`+pub async fn edit(State(state): State<AppState>, headers: HeaderMap, Path(note_id): Path<String>, body: Option<Json<Value>>) -> Response {+ let Some(Ok(b)) = body.map(|Json(v)| serde_json::from_value::<EditNote>(v)) else {+ return err(StatusCode::BAD_REQUEST, "bad_request", "send {text}");+ };+ let (row, pr) = match note_of(&state, &headers, ¬e_id).await {+ Ok(x) => x,+ Err(r) => return r,+ };+ if row.deleted_at.is_some() {+ return err(StatusCode::NOT_FOUND, "not_found", "no such note");+ }+ let text = match check_text(&b.text) {+ Ok(t) => t,+ Err(r) => return r,+ };+ match state.db.set_line_note_text(&row.id, &text).await {+ Ok(true) => {}+ Ok(false) => return err(StatusCode::CONFLICT, "not_draft", "Only a draft can be changed"),+ Err(e) => return internal(e),+ }+ changed_event(&state, &pr);+ one_note(&state, &pr, &row.id).await+}++async fn one_note(state: &AppState, pr: &Pr, id: &str) -> Response {+ match state.db.line_note(id).await {+ Ok(Some(r)) => {+ let files = pr.files();+ Json(note_json(&r, Some((&pr.current.head_sha, &files)))).into_response()+ }+ Ok(None) => err(StatusCode::NOT_FOUND, "not_found", "no such note"),+ Err(e) => internal(e),+ }+}++/// `DELETE /api/dev/notes/:note_id`: a soft delete, restorable for 60 s.+pub async fn delete(State(state): State<AppState>, headers: HeaderMap, Path(note_id): Path<String>) -> Response {+ let (row, pr) = match note_of(&state, &headers, ¬e_id).await {+ Ok(x) => x,+ Err(r) => return r,+ };+ if row.deleted_at.is_some() {+ return err(StatusCode::NOT_FOUND, "not_found", "no such note");+ }+ match state.db.delete_line_note(&row.id).await {+ Ok(true) => {}+ Ok(false) => return err(StatusCode::CONFLICT, "not_draft", "Only a draft can be deleted"),+ Err(e) => return internal(e),+ }+ changed_event(&state, &pr);+ Json(json!({"id": row.id, "deleted": true})).into_response()+}++/// `POST /api/dev/notes/:note_id/restore`+pub async fn restore(State(state): State<AppState>, headers: HeaderMap, Path(note_id): Path<String>) -> Response {+ let (row, pr) = match note_of(&state, &headers, ¬e_id).await {+ Ok(x) => x,+ Err(r) => return r,+ };+ if row.state != DRAFT {+ return err(StatusCode::CONFLICT, "not_draft", "This note was sent, so there's nothing to restore");+ }+ if row.deleted_at.is_some() {+ match state.db.restore_line_note(&row.id).await {+ Ok(true) => changed_event(&state, &pr),+ Ok(false) => {+ let _ = state.db.purge_line_notes().await;+ return err(StatusCode::NOT_FOUND, "not_found", "That note can't be restored any more");+ }+ Err(e) => return internal(e),+ }+ }+ one_note(&state, &pr, &row.id).await+}++/// `DELETE /api/dev/cards/:id/notes`: soft-deletes every draft of the PR.+pub async fn discard(State(state): State<AppState>, headers: HeaderMap, Path(card_id): Path<String>) -> Response {+ let pr = match pr_of(&state, &headers, &card_id).await {+ Ok(p) => p,+ Err(r) => return r,+ };+ match state.db.discard_line_note_drafts(&pr.current.space_id, &pr.current.repo, &pr.current.ref_).await {+ Ok(n) => {+ if n > 0 {+ changed_event(&state, &pr);+ }+ Json(json!({"discarded": n})).into_response()+ }+ Err(e) => internal(e),+ }+}++#[derive(Deserialize)]+pub struct SendBody {+ #[serde(default)]+ pub ids: Option<Vec<String>>,+ pub send_id: String,+}++/// The ids a send names, sorted, as stored to compare a repeat.+fn ids_key(ids: &Option<Vec<String>>) -> Option<String> {+ ids.as_ref().map(|v| {+ let mut v: Vec<&String> = v.iter().collect::<HashSet<_>>().into_iter().collect();+ v.sort();+ serde_json::to_string(&v).unwrap_or_default()+ })+}++/// `POST /api/dev/cards/:id/notes/send`+pub async fn send(State(state): State<AppState>, headers: HeaderMap, Path(card_id): Path<String>, body: Option<Json<Value>>) -> Response {+ let Some(Ok(b)) = body.map(|Json(v)| serde_json::from_value::<SendBody>(v)) else {+ return err(StatusCode::BAD_REQUEST, "bad_request", "send {send_id, ids?}");+ };+ let send_id = b.send_id.trim().to_string();+ if send_id.is_empty() || send_id.len() > 100 {+ return err(StatusCode::BAD_REQUEST, "bad_request", "send_id is the client's UUID");+ }+ let pr = match pr_of(&state, &headers, &card_id).await {+ Ok(p) => p,+ Err(r) => return r,+ };+ let lock = card_lock(&pr_key(&pr.current));+ let _held = lock.lock().await;+ // A repeat: the first result, nothing posted+ match state.db.line_note_send(&send_id).await {+ Ok(Some(s)) if s.created_at >= now_ms() - SEND_ID_HOURS * 3_600_000 => {+ if s.ids != ids_key(&b.ids) || s.space_id != pr.current.space_id || s.repo != pr.current.repo || s.ref_ != pr.current.ref_ {+ return err(StatusCode::CONFLICT, "send_id_reused", "That send_id was used for other notes");+ }+ let first: Value = serde_json::from_str(&s.result).unwrap_or(Value::Null);+ return Json(first).into_response();+ }+ Ok(_) => {}+ Err(e) => return internal(e),+ }+ if let Err(r) = writable(&state, &pr).await {+ return r;+ }+ let kwants = state.db.list_kwants(&pr.space).await.unwrap_or_default();+ let Some(bot) = author_bot(&pr.facts, &kwants).cloned() else {+ return err(StatusCode::CONFLICT, "no_author", "Notes go to bots in this space, and this PR isn't by one");+ };+ let rows = match state.db.line_notes_of_pr(&pr.current.space_id, &pr.current.repo, &pr.current.ref_).await {+ Ok(r) => r,+ Err(e) => return internal(e),+ };+ let drafts: Vec<&LineNoteRow> = match &b.ids {+ Some(ids) => {+ let want: HashSet<&str> = ids.iter().map(String::as_str).collect();+ let found: Vec<&LineNoteRow> = rows.iter().filter(|r| want.contains(r.id.as_str())).collect();+ if found.len() != want.len() {+ return err(StatusCode::NOT_FOUND, "not_found", "One of those notes doesn't exist on this PR");+ }+ if found.iter().any(|r| r.state != DRAFT) {+ return err(StatusCode::CONFLICT, "not_draft", "One of those notes was already sent");+ }+ found+ }+ None => rows.iter().filter(|r| r.state == DRAFT).collect(),+ };+ if drafts.is_empty() {+ return err(StatusCode::UNPROCESSABLE_ENTITY, "empty", "There are no drafts to send");+ }+ if drafts.len() > SEND_MAX {+ return err(StatusCode::UNPROCESSABLE_ENTITY, "too_many", "Up to 20 notes per send");+ }+ let outgoing: Vec<Outgoing> =+ drafts.iter().map(|r| Outgoing { path: r.path.clone(), code: serde_json::from_str(&r.code).unwrap_or_default(), text: r.text.clone() }).collect();+ let text = message_text(pr.facts.pr, pr.facts.head_branch.as_deref(), &pr.current.head_sha, &outgoing);+ let meta_notes: Vec<Value> = drafts+ .iter()+ .zip(&outgoing)+ .map(|(r, o)| {+ json!({+ "id": r.id, "at": at_text(&o.path, &o.code), "path": o.path,+ "start": End { side: r.start_side.clone(), line: r.start_line as u32 },+ "end": End { side: r.end_side.clone(), line: r.end_line as u32 },+ "code": o.code, "text": o.text,+ })+ })+ .collect();+ let meta = json!({+ "kind": "pr_notes",+ "notes": meta_notes,+ "repo": pr.facts.repo,+ "pr": pr.facts.pr,+ "head_sha": pr.current.head_sha,+ "card_id": pr.current.id,+ "pr_url": pr.facts.links.pr,+ "author_bot_id": bot.id,+ });+ let payload = crate::server::routes::SendChatPayload {+ channel_id: None,+ kwant_id: Some(bot.id.clone()),+ team_id: None,+ thread_id: None,+ prompt: text,+ sender_id: "user".into(),+ attachment_ids: Vec::new(),+ reply_to_message_id: None,+ source: None,+ meta: Some(meta),+ };+ let sent = match crate::server::routes::send_chat_inner(state.clone(), payload, true).await {+ Ok(Json(v)) => v,+ Err(status) => {+ tracing::warn!(card = %pr.current.id, %status, "dev: line notes: the message didn't go");+ return err(StatusCode::BAD_GATEWAY, "send_failed", "The notes didn't send. They're still here.");+ }+ };+ let message_id = sent["user_message_id"].as_str().unwrap_or_default().to_string();+ let sent_at = now_ms();+ let ids: Vec<String> = drafts.iter().map(|r| r.id.clone()).collect();+ if let Err(e) = state.db.mark_line_notes_sent(&ids, &message_id, sent_at).await {+ return internal(e);+ }+ let rows = state.db.line_notes_of_pr(&pr.current.space_id, &pr.current.repo, &pr.current.ref_).await.unwrap_or_default();+ let files = pr.files();+ let notes: Vec<Value> = rows.iter().filter(|r| ids.contains(&r.id)).map(|r| note_json(r, Some((&pr.current.head_sha, &files)))).collect();+ let result = json!({+ "sent": ids.len(),+ "message_id": message_id,+ "kwant_id": bot.id,+ "kwant_name": bot.name,+ "sent_at": Utc.timestamp_millis_opt(sent_at).single(),+ "notes": notes,+ });+ let remembered = LineNoteSendRow {+ send_id,+ space_id: pr.current.space_id.clone(),+ repo: pr.current.repo.clone(),+ ref_: pr.current.ref_.clone(),+ ids: ids_key(&b.ids),+ result: result.to_string(),+ created_at: sent_at,+ };+ if let Err(e) = state.db.insert_line_note_send(&remembered).await {+ tracing::warn!("dev: line notes: send not remembered: {e}");+ }+ changed_event(&state, &pr);+ Json(result).into_response()+}++// ---------------------------------------------------------------------------------------------+// After a push++/// Checks the PR's sent notes against its newest head, when that head is newer than the one they+/// were written on. True when any changed state.+async fn check_sent(state: &AppState, pr: &Pr) -> bool {+ let Ok(rows) = state.db.line_notes_of_pr(&pr.current.space_id, &pr.current.repo, &pr.current.ref_).await else { return false };+ let mut any = false;+ for r in rows.iter().filter(|r| r.state == SENT && r.head_sha != pr.current.head_sha) {+ let Some(c) = changed(r, &pr.facts.files) else { continue };+ let to = if c { RESOLVED } else { OPEN };+ if state.db.check_line_note(&r.id, to, &pr.current.head_sha).await.unwrap_or(false) {+ any = true;+ }+ }+ any+}++/// A new review card for a PR (the poller, after it saved it): the PR's sent notes are checked+/// against it, and the card's event fires when any changed.+pub async fn after_push(state: AppState, row: DevCardRow) {+ if row.kind != REVIEW {+ return;+ }+ let Ok(facts) = serde_json::from_str::<DevFacts>(&row.facts) else { return };+ let Ok(space) = active_space(&state, &row.space_id).await else { return };+ let pr = Pr { space, current: row, facts };+ if check_sent(&state, &pr).await {+ changed_event(&state, &pr);+ }+}++/// The review card payload's `notes: {drafts, total} | null` (g1-api.md, Iris ask 1), for the+/// `rows` of `space` whose JSON is `cards` (same order). Notes are the PR's, whichever of its cards+/// this is; soft-deleted notes don't count.+pub async fn add_counts(state: &AppState, space: &str, rows: &[DevCardRow], cards: &mut [Value]) {+ if !rows.iter().any(|r| r.kind == REVIEW) {+ return;+ }+ let Ok(counts) = state.db.line_note_counts(space).await else { return };+ let by: HashMap<(String, String), (i64, i64)> = counts.into_iter().map(|(repo, r, d, t)| ((repo, r), (d, t))).collect();+ for (row, c) in rows.iter().zip(cards.iter_mut()).filter(|(r, _)| r.kind == REVIEW) {+ let v = by.get(&(row.repo.clone(), row.ref_.clone())).map(|(d, t)| json!({"drafts": d, "total": t})).unwrap_or(Value::Null);+ if let Some(m) = c.as_object_mut() {+ m.insert("notes".into(), v);+ }+ }+}++/// The review card payload's `changes: {files, additions, deletions} | null`: from the files the+/// review read for its head; null when it read none.+pub fn changes_stat(files: &[FileDiff]) -> Value {+ if files.is_empty() {+ return Value::Null;+ }+ json!({+ "files": files.len(),+ "additions": files.iter().map(|f| f.additions).sum::<u64>(),+ "deletions": files.iter().map(|f| f.deletions).sum::<u64>(),+ })+}++#[cfg(test)]+mod tests {+ use super::*;++ const PATCH: &str = "@@ -38,6 +38,7 @@ export function InboxEmpty\n const x = 1;\n- return (\n+ return (\n+ <section aria-label=\"Inbox\">\n <div>\n </div>\n@@ -80,3 +81,3 @@\n a\n-b\n+B\n c\n\\ No newline at end of file";++ fn new(line: u32) -> End {+ End { side: "new".into(), line }+ }+ fn old(line: u32) -> End {+ End { side: "old".into(), line }+ }++ #[test]+ fn a_patch_parses_into_numbered_lines() {+ let h = parse_patch(PATCH);+ assert_eq!(h.len(), 2);+ assert_eq!(h[0].header, "@@ -38,6 +38,7 @@ export function InboxEmpty");+ let l = &h[0].lines;+ assert_eq!(l[0], DLine { kind: "ctx".into(), old: Some(38), new: Some(38), text: " const x = 1;".into() });+ assert_eq!(l[1], DLine { kind: "del".into(), old: Some(39), new: None, text: " return (".into() });+ assert_eq!(l[2], DLine { kind: "add".into(), old: None, new: Some(39), text: " return (".into() });+ assert_eq!(l[3].new, Some(40));+ assert_eq!((l[4].old, l[4].new), (Some(40), Some(41)));+ assert_eq!(h[1].lines.len(), 4, "the no-newline marker isn't a line");+ assert_eq!((h[1].lines[1].old, h[1].lines[2].new), (Some(81), Some(82)));+ }++ #[test]+ fn a_range_is_every_line_between_its_ends_in_one_hunk() {+ let h = parse_patch(PATCH);+ // del 39 to add 40: a mixed range+ let r = select(&h, &old(39), &new(40)).unwrap();+ assert_eq!(r.iter().map(|l| l.kind.as_str()).collect::<Vec<_>>(), ["del", "add", "add"]);+ // dragged upwards: same lines+ assert_eq!(select(&h, &new(40), &old(39)).unwrap(), r);+ // one line+ assert_eq!(select(&h, &new(38), &new(38)).unwrap().len(), 1);+ assert_eq!(select(&h, &new(38), &new(82)), Err(RangeError::CrossHunk));+ assert_eq!(select(&h, &new(500), &new(500)), Err(RangeError::NotInDiff));+ // a removed line has no new number+ assert_eq!(select(&h, &new(39), &new(39)).unwrap()[0].kind, "add");+ assert_eq!(select(&h, &old(39), &old(39)).unwrap()[0].kind, "del");+ }++ #[test]+ fn a_range_over_200_lines_is_refused() {+ let mut p = String::from("@@ -1,0 +1,201 @@\n");+ for i in 0..201 {+ p.push_str(&format!("+l{i}\n"));+ }+ let h = parse_patch(&p);+ assert_eq!(select(&h, &new(1), &new(201)), Err(RangeError::TooBig));+ assert_eq!(select(&h, &new(1), &new(200)).unwrap().len(), 200);+ }++ #[test]+ fn noted_lines_are_found_in_a_newer_push() {+ let h = parse_patch(PATCH);+ let code = select(&h, &new(40), &new(41)).unwrap();+ // Two lines added above them: they moved down by 2+ let moved = PATCH.replace("@@ -38,6 +38,7 @@ export function InboxEmpty\n", "@@ -38,6 +38,9 @@ export function InboxEmpty\n+// one\n+// two\n");+ let (s, e) = locate(&parse_patch(&moved), &code, "new", "new", 40).unwrap();+ assert_eq!((s, e), (new(42), new(43)));+ // One of them edited: gone+ let edited = PATCH.replace("aria-label=\"Inbox\"", "aria-labelledby=\"h\"");+ assert_eq!(locate(&parse_patch(&edited), &code, "new", "new", 40), None);+ }++ #[test]+ fn of_two_copies_the_nearest_is_picked() {+ let p = "@@ -1,0 +1,2 @@\n+x\n+y\n@@ -50,0 +50,2 @@\n+x\n+y\n";+ let code = vec![DLine { kind: "add".into(), old: None, new: Some(1), text: "x".into() }];+ assert_eq!(locate(&parse_patch(p), &code, "new", "new", 48).unwrap().0, new(50));+ assert_eq!(locate(&parse_patch(p), &code, "new", "new", 3).unwrap().0, new(1));+ }++ #[test]+ fn at_names_new_lines_else_old() {+ let h = parse_patch(PATCH);+ assert_eq!(at_text("a.tsx", &select(&h, &old(39), &new(40)).unwrap()), "a.tsx:39-40");+ assert_eq!(at_text("a.tsx", &select(&h, &old(39), &old(39)).unwrap()), "a.tsx:39");+ assert_eq!(at_text("a.tsx", &select(&h, &new(41), &new(41)).unwrap()), "a.tsx:41");+ }++ fn file(path: &str, adds: u64, dels: u64, patch: Option<&str>, status: &str) -> FileDiff {+ FileDiff { path: path.into(), status: status.into(), additions: adds, deletions: dels, patch: patch.map(str::to_string), previous_path: None }+ }++ #[test]+ fn omitted_files_say_why() {+ assert_eq!(omitted(&file("a.png", 0, 0, None, "added")), Some("binary"));+ assert_eq!(omitted(&file("a.rs", 900, 2, None, "modified")), Some("too_large"));+ assert_eq!(omitted(&file("a.rs", 0, 0, None, "renamed")), None);+ assert_eq!(omitted(&file("a.rs", 1, 0, Some("@@ -1,0 +1 @@\n+a"), "modified")), None);+ assert!(is_big(&file("x/Cargo.lock", 3, 1, Some(""), "modified")));+ assert!(is_big(&file("a.rs", 300, 101, Some(""), "modified")));+ assert!(!is_big(&file("a.rs", 300, 100, Some(""), "modified")));+ }++ #[test]+ fn the_message_is_mira_s_block() {+ let h = parse_patch(PATCH);+ let notes = vec![+ Outgoing { path: "src/a.tsx".into(), code: select(&h, &new(40), &new(41)).unwrap(), text: "Use aria-labelledby.".into() },+ Outgoing { path: "src/a.tsx".into(), code: select(&h, &old(39), &old(39)).unwrap(), text: "Why remove this?\nIt was fine.".into() },+ ];+ let m = message_text(Some(212), Some("pixel/inbox-empty"), "9f3c2e1abcdef", ¬es);+ assert_eq!(+ m,+ "2 notes on #212 pixel/inbox-empty (at 9f3c2e1), from the review:\n\+ \n1. src/a.tsx:40-41\n 40 | <section aria-label=\"Inbox\">\n 41 | <div>\n → Use aria-labelledby.\n\+ \n2. src/a.tsx:39\n 39 (removed) | return (\n → Why remove this?\n It was fine.\n\+ \nPush to the same branch when done; kwant marks each note resolved when its lines change."+ );+ let one = message_text(None, None, "abc", ¬es[..1]);+ assert!(one.starts_with("1 note on the branch (at abc), from the review:\n"), "{one}");+ }++ #[test]+ fn long_lines_and_long_ranges_are_cut() {+ let long = "x".repeat(200);+ let code: Vec<DLine> = (1..=20).map(|i| DLine { kind: "add".into(), old: None, new: Some(i), text: if i == 1 { long.clone() } else { format!("l{i}") } }).collect();+ let q = quoted(&code);+ assert_eq!(q.len(), 6 + 1 + 3);+ assert_eq!(q[0], format!(" 1 | {}…", "x".repeat(160)));+ assert_eq!(q[6], " …");+ assert_eq!(q[7], " 18 | l18");+ assert_eq!(q[9], " 20 | l20");+ assert_eq!(quoted(&code[..12]).len(), 12);+ }++ fn row(state: &str, head: &str, code: &[DLine], start: u32, end: u32) -> LineNoteRow {+ LineNoteRow {+ id: "ln_1".into(),+ space_id: "s".into(),+ repo: "r".into(),+ ref_: "pr:1".into(),+ card_id: "c".into(),+ path: "src/a.tsx".into(),+ start_side: "new".into(),+ start_line: start as i64,+ end_side: "new".into(),+ end_line: end as i64,+ code: serde_json::to_string(code).unwrap(),+ text: "t".into(),+ state: state.into(),+ head_sha: head.into(),+ created_at: 1,+ updated_at: 1,+ sent_at: None,+ message_id: None,+ checked_sha: None,+ deleted_at: None,+ }+ }++ #[test]+ fn a_push_changes_a_note_when_any_of_its_lines_changed() {+ let h = parse_patch(PATCH);+ let code = select(&h, &new(40), &new(41)).unwrap();+ let r = row(SENT, "aaa", &code, 40, 41);+ let same = [file("src/a.tsx", 2, 1, Some(PATCH), "modified")];+ assert_eq!(changed(&r, &same), Some(false));+ let edited = PATCH.replace("<div>", "<main>");+ assert_eq!(changed(&r, &[file("src/a.tsx", 2, 1, Some(&edited), "modified")]), Some(true));+ // The file's diff wasn't read for that head: can't tell yet+ assert_eq!(changed(&r, &[file("src/a.tsx", 2000, 1, None, "modified")]), None);+ // The file left the PR: the change is gone+ assert_eq!(changed(&r, &[file("src/b.tsx", 1, 0, Some("@@ -1,0 +1 @@\n+a"), "modified")]), Some(true));+ }++ #[test]+ fn a_note_says_where_it_is_in_the_newest_head() {+ let h = parse_patch(PATCH);+ let code = select(&h, &new(40), &new(41)).unwrap();+ let r = row(DRAFT, "aaa", &code, 40, 41);+ let files: HashMap<String, Vec<DHunk>> = [("src/a.tsx".to_string(), h.clone())].into();+ let v = note_json(&r, Some(("aaa", &files)));+ assert_eq!(v["at_head"]["start"], json!({"side": "new", "line": 40}));+ assert_eq!(v["seen_sha"], Value::Null);+ let moved = PATCH.replace("@@ -38,6 +38,7 @@ export function InboxEmpty\n", "@@ -38,6 +38,8 @@ export function InboxEmpty\n+// one\n");+ let files: HashMap<String, Vec<DHunk>> = [("src/a.tsx".to_string(), parse_patch(&moved))].into();+ let v = note_json(&r, Some(("bbb", &files)));+ assert_eq!(v["at_head"], json!({"start": {"side": "new", "line": 41}, "end": {"side": "new", "line": 42}}));+ assert_eq!(v["seen_sha"], "bbb");+ let files: HashMap<String, Vec<DHunk>> = HashMap::new();+ let v = note_json(&r, Some(("bbb", &files)));+ assert_eq!(v["at_head"], Value::Null);+ assert_eq!(v["seen_sha"], "bbb");+ assert_eq!(v["code"][0]["kind"], "add");+ }++ #[test]+ fn the_summary_counts_the_newest_check() {+ let mut a = row(RESOLVED, "a", &[], 1, 1);+ a.checked_sha = Some("b".into());+ a.updated_at = 5;+ let mut b = row(OPEN, "a", &[], 1, 1);+ b.checked_sha = Some("b".into());+ b.updated_at = 6;+ let c = row(DRAFT, "b", &[], 1, 1);+ assert_eq!(summary(&[a, b, c.clone()]).unwrap(), json!({"total": 2, "changed": 1, "open": 1, "sha": "b"}));+ assert_eq!(summary(&[c]), None);+ }+}diff --git a/src/server/dev/mod.rs b/src/server/dev/mod.rsindex 87d188b4..f83c721f 100644--- a/src/server/dev/mod.rs+++ b/src/server/dev/mod.rs@@ -17,6 +17,7 @@ pub mod forge;pub mod git;pub mod generate;pub mod landed;+pub mod line_notes;pub mod notes;pub mod poller;pub mod post;diff --git a/src/server/dev/poller.rs b/src/server/dev/poller.rsindex 09ac0191..76caa18e 100644--- a/src/server/dev/poller.rs+++ b/src/server/dev/poller.rs@@ -586,6 +586,10 @@ async fn insert(state: &AppState, mode: &DevRepoRow, kind: &str, ref_: &str, heamatch state.db.insert_dev_card_in(space_id, &mode.key, kind, ref_, head, &json).await {Ok(Some(row)) => {super::notify_card(state, &row);+ // G1: a new head for a PR checks the notes sent on an older one+ if row.kind == REVIEW {+ tokio::spawn(super::line_notes::after_push(state.clone(), row.clone()));+ }tokio::spawn(super::generate(state.clone(), row.id));}Ok(None) => {}diff --git a/src/server/dev/routes.rs b/src/server/dev/routes.rsindex 70dcfb17..b5a818be 100644--- a/src/server/dev/routes.rs+++ b/src/server/dev/routes.rs@@ -619,6 +619,8 @@ pub fn card_json_in(row: &DevCardRow, teams: &HashMap<String, String>) -> Valueout.insert("branch".into(), json!(f.head_branch));out.insert("base".into(), json!(f.base_branch));out.insert("draft".into(), json!(f.draft));+ // G1: the diff stat the Show button needs before `/changes` is fetched+ out.insert("changes".into(), super::line_notes::changes_stat(&f.files));}}}@@ -645,6 +647,8 @@ pub fn card_json_in(row: &DevCardRow, teams: &HashMap<String, String>) -> Value}out.insert("posted".into(), stored(&row.posted));out.insert("post_error".into(), stored(&row.post_error));+ // G1: the head the card is about, refetched by the diff view when it moves+ out.insert("head_sha".into(), json!(row.head_sha));}Value::Object(out)}@@ -821,7 +825,11 @@ pub async fn reviews(State(state): State<AppState>, headers: HeaderMap, Path(id)let only = repo_filter(q.repo.as_deref());let teams = team_names(&state, &space).await;match open_reviews(&state, &space, only.as_deref()).await {- Ok(rows) => Json(json!({"cards": rows.iter().map(|r| card_json_in(r, &teams)).collect::<Vec<_>>()})).into_response(),+ Ok(rows) => {+ let mut cards: Vec<Value> = rows.iter().map(|r| card_json_in(r, &teams)).collect();+ super::line_notes::add_counts(&state, space.as_str(), &rows, &mut cards).await;+ Json(json!({"cards": cards})).into_response()+ }Err(e) => internal(e),}}@@ -843,7 +851,10 @@ pub async fn card(State(state): State<AppState>, headers: HeaderMap, Path(card_iErr(r) => return r,};let teams = team_names(&state, &space).await;- Json(card_json_in(&row, &teams)).into_response()+ let mut card = [card_json_in(&row, &teams)];+ super::line_notes::add_counts(&state, space.as_str(), std::slice::from_ref(&row), &mut card).await;+ let [card] = card;+ Json(card).into_response()}// ---------------------------------------------------------------------------------------------diff --git a/src/server/mod.rs b/src/server/mod.rsindex f6756b73..830e5f77 100644--- a/src/server/mod.rs+++ b/src/server/mod.rs@@ -304,6 +304,11 @@ pub fn create_router(state: AppState) -> Router {.route("/api/dev/cards/:id", get(dev::routes::card)).route("/api/dev/cards/:id/findings/:fid", post(dev::routes::finding)).route("/api/dev/cards/:id/post", post(dev::post::post))+ .route("/api/dev/cards/:id/changes", get(dev::line_notes::changes))+ .route("/api/dev/cards/:id/notes", get(dev::line_notes::list).post(dev::line_notes::create).delete(dev::line_notes::discard))+ .route("/api/dev/cards/:id/notes/send", post(dev::line_notes::send))+ .route("/api/dev/notes/:note_id", patch(dev::line_notes::edit).delete(dev::line_notes::delete))+ .route("/api/dev/notes/:note_id/restore", post(dev::line_notes::restore)).route("/api/kwants/:id/approvals", get(approval_modes::get_kwant).put(approval_modes::put_kwant)).route("/api/settings", get(routes::get_settings).post(routes::save_settings)).route("/api/settings/computer", get(computer::get_computer).put(computer::put_computer))diff --git a/src/server/routes.rs b/src/server/routes.rsindex 5e532cfd..71e94e4b 100644--- a/src/server/routes.rs+++ b/src/server/routes.rs@@ -710,6 +710,10 @@ pub struct SendChatPayload {/// `meta.source`. Set only by the server, never read from a request.#[serde(skip)]pub source: Option<String>,+ /// Keys merged into the message's `meta` (line notes' `{kind: "pr_notes", …}`). Set only by+ /// the server, never read from a request.+ #[serde(skip)]+ pub meta: Option<serde_json::Value>,}async fn get_configured_key(db: &crate::db::Database, target_model: &str) -> Option<String> {@@ -3703,6 +3707,17 @@ async fn idea_from_chat(}}+/// Adds the server-set `extra` keys to a message's meta.+fn merge_meta(meta: &mut Option<serde_json::Value>, extra: Option<&serde_json::Value>) {+ let Some(serde_json::Value::Object(extra)) = extra else { return };+ let m = meta.get_or_insert_with(|| json!({}));+ if let Some(m) = m.as_object_mut() {+ for (k, v) in extra {+ m.insert(k.clone(), v.clone());+ }+ }+}+pub(crate) async fn send_chat_inner(state: AppState,mut payload: SendChatPayload,@@ -3924,6 +3939,7 @@ pub(crate) async fn send_chat_inner(if let Some(source) = &payload.source {user_meta.get_or_insert_with(|| json!({}))["source"] = json!(source);}+ merge_meta(&mut user_meta, payload.meta.as_ref());if !from_person {user_meta.get_or_insert_with(|| json!({}))[SENT_BY_BOT] = json!(true);}@@ -4357,6 +4373,7 @@ async fn send_to_thread(tokens_used: payload.prompt.len() as i64 / 4,};let mut meta = (!uploads.is_empty()).then(|| json!({"attachments": super::attachments::cards(&uploads)}));+ merge_meta(&mut meta, payload.meta.as_ref());if !from_person {meta.get_or_insert_with(|| json!({}))[SENT_BY_BOT] = json!(true);}diff --git a/src/server/telegram/bridge.rs b/src/server/telegram/bridge.rsindex fdc42770..36c8e331 100644--- a/src/server/telegram/bridge.rs+++ b/src/server/telegram/bridge.rs@@ -235,6 +235,7 @@ async fn person_message(state: &AppState, runner: &Runner, link: &TelegramLink,attachment_ids: Vec::new(),reply_to_message_id: reply_to,source: Some("telegram".into()),+ meta: None,};match crate::server::routes::send_chat_inner(state.clone(), payload, true).await {Ok(axum::Json(v)) => {diff --git a/tests/dev_line_notes_test.rs b/tests/dev_line_notes_test.rsnew file mode 100644index 00000000..ed12eae1--- /dev/null+++ b/tests/dev_line_notes_test.rs@@ -0,0 +1,259 @@+//! G1 line notes (release-090/g1-api.md): the diff of a review card, notes on its lines, one+//! message to the author bot, and the check on the next push.++mod common;++use common::*;+use kwnt::db::dev_mode::{LANDED, REVIEW, STALE};+use kwnt::db::spaces::SpaceId;+use kwnt::server::dev::line_notes;+use serde_json::{json, Value};+use std::time::Duration;++const REPO: &str = "github.com/demo-org/app";++const PATCH: &str = "@@ -38,6 +38,7 @@ export function InboxEmpty\n const x = 1;\n- return (\n+ return (\n+ <section aria-label=\"Inbox\">\n <div>\n </div>\n@@ -80,3 +81,3 @@\n a\n-b\n+B\n c";++fn facts(head: &str, bot: Option<(&str, &str)>, branch: &str, patch: &str) -> String {+ json!({+ "repo": "demo-org/app", "kind": "review", "pr": 212, "title": "Inbox empty state", "body": "",+ "head_sha": head, "base_branch": "main", "merged_at": null, "merged_by": null,+ "author": {"login": "pixel-bot", "name": "Pixel", "email": null,+ "bot": bot.map(|(id, name)| json!({"id": id, "name": name, "engine": "claude", "model": "x"}))},+ "files": [+ {"path": "src/InboxEmpty.tsx", "status": "modified", "additions": 3, "deletions": 2, "patch": patch, "previous_path": null},+ {"path": "assets/logo.png", "status": "added", "additions": 0, "deletions": 0, "patch": null, "previous_path": null},+ {"path": "Cargo.lock", "status": "modified", "additions": 900, "deletions": 3, "patch": null, "previous_path": null}+ ],+ "cut": "diff cut, 1 files not read", "checks": [], "ci": "green", "task": null, "first_edit_files": [],+ "head_branch": branch, "links": {"pr": "https://github.com/demo-org/app/pull/212", "commit": null, "ci": null}+ })+ .to_string()+}++async fn review(env: &Env, head: &str, patch: &str) -> kwnt::db::dev_mode::DevCardRow {+ tokio::time::sleep(Duration::from_millis(5)).await;+ let space = SpaceId::default_space();+ let bot = env.state.db.get_kwant(&env.a).await.unwrap().unwrap();+ env.state+ .db+ .insert_dev_card_in(space.as_str(), REPO, REVIEW, "pr:212", head, &facts(head, Some((&bot.id, &bot.name)), "pixel/inbox-empty", patch))+ .await+ .unwrap()+ .unwrap()+}++fn note(head: &str, start: Value, end: Value, text: &str) -> Value {+ json!({"path": "src/InboxEmpty.tsx", "start": start, "end": end, "head_sha": head, "text": text})+}++#[tokio::test]+async fn the_diff_is_the_card_s_own_files_as_hunks() {+ let env = setup().await;+ let card = review(&env, "aaa1111", PATCH).await;+ let (s, v) = call(&env.app, "GET", &format!("/api/dev/cards/{}/changes", card.id), None).await;+ assert_eq!(s.as_u16(), 200, "{v}");+ assert_eq!((v["pr"].clone(), v["head_sha"].clone(), v["pr_state"].clone()), (json!(212), json!("aaa1111"), json!("open")));+ assert_eq!(v["author_bot"]["id"], env.a.as_str());+ assert_eq!(v["cut"], json!({"files_not_read": 1}));+ let f = &v["files"];+ assert_eq!(f[0]["hunks"][0]["lines"][1], json!({"kind": "del", "old": 39, "new": null, "text": " return ("}));+ assert_eq!(f[0]["hunks"][1]["header"], "@@ -80,3 +81,3 @@");+ assert_eq!((f[0]["omitted"].clone(), f[0]["big"].clone()), (Value::Null, json!(false)));+ assert_eq!((f[1]["omitted"].clone(), f[1]["hunks"].clone()), (json!("binary"), json!([])));+ assert_eq!((f[2]["omitted"].clone(), f[2]["big"].clone()), (json!("too_large"), json!(true)));++ // The card payload carries the stat, the head and (no notes yet) null counts+ let (_, c) = call(&env.app, "GET", &format!("/api/dev/cards/{}", card.id), None).await;+ assert_eq!(c["changes"], json!({"files": 3, "additions": 903, "deletions": 5}), "{c}");+ assert_eq!((c["head_sha"].clone(), c["notes"].clone()), (json!("aaa1111"), Value::Null));++ // Only a review card has a diff; agents are refused+ let (s, v) = call(&env.app, "GET", "/api/dev/cards/dvc_nope/changes", None).await;+ assert_eq!((s.as_u16(), v["code"].clone()), (404, json!("not_found")));+ let (s, _) = call_with(&env.app, "GET", &format!("/api/dev/cards/{}/changes", card.id), None, &[("authorization", "Bearer not-the-person")]).await;+ assert_eq!(s.as_u16(), 403);+}++#[tokio::test]+async fn notes_are_written_edited_deleted_and_restored() {+ let env = setup().await;+ let card = review(&env, "aaa1111", PATCH).await;+ let notes = format!("/api/dev/cards/{}/notes", card.id);+ let new = |l: u32| json!({"side": "new", "line": l});+ let old = |l: u32| json!({"side": "old", "line": l});++ // A mixed range, dragged upwards: the removed line and both added lines+ let (s, n) = call(&env.app, "POST", ¬es, Some(note("aaa1111", new(40), old(39), "Use aria-labelledby."))).await;+ assert_eq!(s.as_u16(), 201, "{n}");+ assert_eq!((n["start"].clone(), n["end"].clone()), (old(39), new(40)), "ends in diff order");+ assert_eq!(n["code"].as_array().unwrap().iter().map(|l| l["kind"].as_str().unwrap()).collect::<Vec<_>>(), ["del", "add", "add"]);+ assert_eq!((n["state"].clone(), n["at_head"]["start"].clone(), n["seen_sha"].clone()), (json!("draft"), old(39), Value::Null));+ let id = n["id"].as_str().unwrap().to_string();++ // Refusals, each with its code+ for (body, status, code) in [+ (note("bbb", new(40), new(40), "x"), 409, "stale_head"),+ (note("aaa1111", new(38), new(82), "x"), 422, "cross_hunk"),+ (note("aaa1111", new(500), new(500), "x"), 422, "not_in_diff"),+ (note("aaa1111", new(40), new(40), " "), 422, "empty"),+ (note("aaa1111", new(40), new(40), &"x".repeat(4001)), 422, "too_long"),+ (json!({"text": "no path"}), 400, "bad_request"),+ ] {+ let (s, v) = call(&env.app, "POST", ¬es, Some(body)).await;+ assert_eq!((s.as_u16(), v["code"].as_str().unwrap()), (status, code), "{v}");+ assert!(v["error"].is_string());+ if code == "stale_head" {+ assert_eq!(v["head_sha"], "aaa1111");+ }+ }++ // Edit, then the counts on the card+ let (s, e) = call(&env.app, "PATCH", &format!("/api/dev/notes/{id}"), Some(json!({"text": "Use aria-labelledby, please."}))).await;+ assert_eq!((s.as_u16(), e["text"].clone()), (200, json!("Use aria-labelledby, please.")));+ let (s, _) = call(&env.app, "POST", ¬es, Some(note("aaa1111", new(82), new(82), "Why B?"))).await;+ assert_eq!(s.as_u16(), 201);+ let (_, list) = call(&env.app, "GET", &format!("/api/spaces/{}/dev/reviews", SpaceId::default_space().as_str()), None).await;+ assert_eq!(list["cards"][0]["notes"], json!({"drafts": 2, "total": 2}), "{list}");++ // Delete and restore within 60 s; a deleted note isn't listed or counted+ let (s, d) = call(&env.app, "DELETE", &format!("/api/dev/notes/{id}"), None).await;+ assert_eq!((s.as_u16(), d), (200, json!({"id": id, "deleted": true})));+ let (_, l) = call(&env.app, "GET", ¬es, None).await;+ assert_eq!(l["notes"].as_array().unwrap().len(), 1);+ let (_, c) = call(&env.app, "GET", &format!("/api/dev/cards/{}", card.id), None).await;+ assert_eq!(c["notes"], json!({"drafts": 1, "total": 1}));+ let (s, r) = call(&env.app, "POST", &format!("/api/dev/notes/{id}/restore"), None).await;+ assert_eq!((s.as_u16(), r["id"].clone()), (200, json!(id)));+ let (_, l) = call(&env.app, "GET", ¬es, None).await;+ assert_eq!(l["notes"].as_array().unwrap().len(), 2);++ // Discard all drafts+ let (s, v) = call(&env.app, "DELETE", ¬es, None).await;+ assert_eq!((s.as_u16(), v["discarded"].clone()), (200, json!(2)));+ let (_, l) = call(&env.app, "GET", ¬es, None).await;+ assert_eq!(l["notes"], json!([]));+ let (s, v) = call(&env.app, "PATCH", "/api/dev/notes/ln_nope", Some(json!({"text": "x"}))).await;+ assert_eq!((s.as_u16(), v["code"].clone()), (404, json!("not_found")));+}++#[tokio::test]+async fn send_posts_one_message_to_the_author_bot_once() {+ let env = setup().await;+ let card = review(&env, "aaa1111", PATCH).await;+ let notes = format!("/api/dev/cards/{}/notes", card.id);+ let new = |l: u32| json!({"side": "new", "line": l});+ let (_, a) = call(&env.app, "POST", ¬es, Some(note("aaa1111", new(40), new(41), "Use aria-labelledby."))).await;+ let (_, b) = call(&env.app, "POST", ¬es, Some(note("aaa1111", new(82), new(82), "Why B?"))).await;+ let send = format!("{notes}/send");++ let (s, v) = call(&env.app, "POST", &send, Some(json!({"send_id": "s-1"}))).await;+ assert_eq!(s.as_u16(), 200, "{v}");+ assert_eq!((v["sent"].clone(), v["kwant_id"].clone()), (json!(2), json!(env.a)));+ assert!(v["notes"].as_array().unwrap().iter().all(|n| n["state"] == "sent" && n["message_id"] == v["message_id"]), "{v}");++ // One message from the person in the bot's 1:1, Mira's block, meta on the message object+ let (_, msgs) = call(&env.app, "GET", &format!("/api/channels/{}/messages", env.a), None).await;+ let mine: Vec<&Value> = msgs.as_array().unwrap().iter().filter(|m| m["text"].as_str().is_some_and(|t| t.contains("from the review"))).collect();+ assert_eq!(mine.len(), 1, "{msgs}");+ let m = mine[0];+ assert_eq!((m["sender_role"].clone(), m["id"].clone()), (json!("user"), v["message_id"].clone()));+ let text = m["text"].as_str().unwrap();+ assert!(text.starts_with("2 notes on #212 pixel/inbox-empty (at aaa1111), from the review:\n"), "{text}");+ assert!(text.contains("\n1. src/InboxEmpty.tsx:40-41\n 40 | <section aria-label=\"Inbox\">\n 41 | <div>\n → Use aria-labelledby.\n"), "{text}");+ assert!(text.contains("\n2. src/InboxEmpty.tsx:82\n 82 | B\n → Why B?\n"), "{text}");+ let meta: Value = serde_json::from_str(m["meta"].as_str().unwrap()).unwrap();+ assert_eq!(meta["kind"], "pr_notes");+ assert_eq!((meta["pr"].clone(), meta["card_id"].clone(), meta["author_bot_id"].clone()), (json!(212), json!(card.id), json!(env.a)));+ assert_eq!(meta["pr_url"], "https://github.com/demo-org/app/pull/212");+ assert_eq!(meta["notes"][0]["id"], a["id"]);+ assert_eq!(meta["notes"][0]["at"], "src/InboxEmpty.tsx:40-41");+ assert_eq!(meta["notes"][1]["id"], b["id"]);++ // The same send_id: the first result, nothing posted+ let (s, again) = call(&env.app, "POST", &send, Some(json!({"send_id": "s-1"}))).await;+ assert_eq!((s.as_u16(), again["message_id"].clone()), (200, v["message_id"].clone()));+ let (s, r) = call(&env.app, "POST", &send, Some(json!({"send_id": "s-1", "ids": [a["id"]]}))).await;+ assert_eq!((s.as_u16(), r["code"].clone()), (409, json!("send_id_reused")));+ let (_, msgs) = call(&env.app, "GET", &format!("/api/channels/{}/messages", env.a), None).await;+ assert_eq!(msgs.as_array().unwrap().iter().filter(|m| m["text"].as_str().is_some_and(|t| t.contains("from the review"))).count(), 1);++ // Nothing left to send; a sent note can't change+ let (s, r) = call(&env.app, "POST", &send, Some(json!({"send_id": "s-2"}))).await;+ assert_eq!((s.as_u16(), r["code"].clone()), (422, json!("empty")));+ let (s, r) = call(&env.app, "PATCH", &format!("/api/dev/notes/{}", a["id"].as_str().unwrap()), Some(json!({"text": "y"}))).await;+ assert_eq!((s.as_u16(), r["code"].clone()), (409, json!("not_draft")));+ let (s, r) = call(&env.app, "POST", &format!("/api/dev/notes/{}/restore", a["id"].as_str().unwrap()), None).await;+ assert_eq!((s.as_u16(), r["code"].clone()), (409, json!("not_draft")));+}++#[tokio::test]+async fn a_push_resolves_the_notes_whose_lines_changed() {+ let env = setup().await;+ let card = review(&env, "aaa1111", PATCH).await;+ let notes = format!("/api/dev/cards/{}/notes", card.id);+ let new = |l: u32| json!({"side": "new", "line": l});+ let (_, a) = call(&env.app, "POST", ¬es, Some(note("aaa1111", new(40), new(41), "label"))).await;+ let (_, b) = call(&env.app, "POST", ¬es, Some(note("aaa1111", new(82), new(82), "B"))).await;+ let (s, _) = call(&env.app, "POST", &format!("{notes}/send"), Some(json!({"send_id": "s-1"}))).await;+ assert_eq!(s.as_u16(), 200);++ // The bot pushes: a line added above the first note, the second note's line edited+ let pushed = PATCH.replace("@@ -38,6 +38,7 @@ export function InboxEmpty\n", "@@ -38,6 +38,8 @@ export function InboxEmpty\n+// moved\n").replace("+B\n", "+Bee\n");+ let old_card = env.state.db.set_dev_card(&card.id, STALE, None).await.unwrap().unwrap();+ let newer = review(&env, "bbb2222", &pushed).await;+ line_notes::after_push(env.state.clone(), newer.clone()).await;++ // Read through the old card's id too: the notes belong to the PR+ for id in [&old_card.id, &newer.id] {+ let (_, l) = call(&env.app, "GET", &format!("/api/dev/cards/{id}/notes"), None).await;+ let by = |nid: &Value| l["notes"].as_array().unwrap().iter().find(|n| n["id"] == *nid).unwrap().clone();+ let (na, nb) = (by(&a["id"]), by(&b["id"]));+ assert_eq!((na["state"].clone(), na["checked_sha"].clone()), (json!("open"), json!("bbb2222")), "{l}");+ assert_eq!(na["at_head"], json!({"start": {"side": "new", "line": 41}, "end": {"side": "new", "line": 42}}));+ assert_eq!(na["seen_sha"], "bbb2222");+ assert_eq!((nb["state"].clone(), nb["at_head"].clone()), (json!("resolved"), Value::Null));+ assert_eq!(l["summary"], json!({"total": 2, "changed": 1, "open": 1, "sha": "bbb2222"}));+ }++ // A new note now anchors to the new head only+ let (s, v) = call(&env.app, "POST", &format!("/api/dev/cards/{}/notes", old_card.id), Some(note("aaa1111", new(40), new(40), "x"))).await;+ assert_eq!((s.as_u16(), v["code"].clone(), v["head_sha"].clone()), (409, json!("stale_head"), json!("bbb2222")));+}++#[tokio::test]+async fn a_merged_or_closed_pr_is_read_only_and_a_person_s_pr_has_no_author() {+ let env = setup().await;+ let space = SpaceId::default_space();+ // A PR by the person: no bot, a branch with no bot's name+ let row = env+ .state+ .db+ .insert_dev_card_in(space.as_str(), REPO, REVIEW, "pr:7", "ccc", &facts("ccc", None, "oleks/tweak", PATCH).replace("212", "7"))+ .await+ .unwrap()+ .unwrap();+ let notes = format!("/api/dev/cards/{}/notes", row.id);+ let (s, _) = call(&env.app, "POST", ¬es, Some(note("ccc", json!({"side": "new", "line": 40}), json!({"side": "new", "line": 40}), "x"))).await;+ assert_eq!(s.as_u16(), 201);+ let (s, v) = call(&env.app, "POST", &format!("{notes}/send"), Some(json!({"send_id": "p-1"}))).await;+ assert_eq!((s.as_u16(), v["code"].clone()), (409, json!("no_author")), "{v}");++ // Closed: its newest card went stale with no landed card+ env.state.db.set_dev_card(&row.id, STALE, None).await.unwrap();+ let (_, c) = call(&env.app, "GET", &format!("/api/dev/cards/{}/changes", row.id), None).await;+ assert_eq!(c["pr_state"], "closed");+ let (s, v) = call(&env.app, "POST", ¬es, Some(note("ccc", json!({"side": "new", "line": 41}), json!({"side": "new", "line": 41}), "x"))).await;+ assert_eq!((s.as_u16(), v["code"].clone()), (409, json!("pr_closed")));++ // Merged: a landed card came from it+ let landed = facts("ddd", None, "", "").replace("\"kind\":\"review\"", "\"kind\":\"landed\"").replace("212", "7");+ env.state.db.insert_dev_card_in(space.as_str(), REPO, LANDED, "ddd", "ddd", &landed).await.unwrap().unwrap();+ let (_, c) = call(&env.app, "GET", &format!("/api/dev/cards/{}/changes", row.id), None).await;+ assert_eq!(c["pr_state"], "merged");+ let (s, v) = call(&env.app, "POST", &format!("{notes}/send"), Some(json!({"send_id": "p-2"}))).await;+ assert_eq!((s.as_u16(), v["code"].clone()), (409, json!("pr_merged")));+ // Reading still works+ let (s, l) = call(&env.app, "GET", ¬es, None).await;+ assert_eq!((s.as_u16(), l["notes"].as_array().unwrap().len()), (200, 1));+}