~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
2270 lines · 96.3 KB Raw History
//! The tools that write, with sign-in on: `create_record`, `apply_writes`
//! and `delete_record`, as the signed-in account, in its own repository at
//! its PDS (`com.atproto.repo.createRecord`, `applyWrites`,
//! `deleteRecord`), of the collections HUD asks to write alone
//! (`scope::written`). A record is held to its collection's lexicon first,
//! which must resolve, with the `atproto` crate's validation; an update
//! (`apply_writes`') reads the record it replaces from the account's PDS
//! first (`getRecord`); only the account's own records are deleted.
//! Called with no account, each asks for sign-in.

use crate::Hud;
use crate::net::Fetched;
use crate::oauth::Account;
use crate::pds::{error_name, error_said};
use crate::tools::{Args, Output, Refusal};
use crate::uri::AtUri;
use atproto::lexicon::Lexicons;
use serde_json::{Value, json};
use std::sync::Arc;

/// The three tools' definitions, after those that read, naming the
/// collections `asked`, the scope sign-in asks for, writes.
pub fn definitions(asked: &str) -> Vec<Value> {
    let written: Vec<&str> = crate::scope::written(asked).collect();
    let written = written.join(", ");
    let schema = |properties: Value, required: &[&str]| {
        json!({ "type": "object", "properties": properties, "required": required,
                "additionalProperties": false })
    };
    let annotations = |title: &str, destructive: bool, idempotent: bool| {
        json!({ "title": title, "readOnlyHint": false, "destructiveHint": destructive,
                "idempotentHint": idempotent, "openWorldHint": true })
    };
    vec![
        json!({
            "name": "create_record",
            "title": "Create record",
            "description": format!("Write a record to the signed-in account's own repository, as app.bsky.feed.like with {{ subject: {{ uri, cid }}, createdAt }}. Only these collections, the ones HUD asks to write: {written}. The collection's lexicon must resolve, and the record is checked against it first. Needs sign-in."),
            "inputSchema": schema(json!({
                "collection": { "type": "string", "description": "The record's collection (NSID), as app.bsky.feed.like." },
                "record": { "type": "object", "description": "The record, as its lexicon describes it; $type is set to the collection. A field {\"$facets\": key} is made the facets (app.bsky.richtext.facet) of the text in the record's field key beside it, as a post's \"facets\": { \"$facets\": \"text\" }: its links, #tags and @mentions, each handle made its DID; left out where the text has none." },
                "rkey": { "type": "string", "description": "Its record key; by default the PDS makes one." },
            }), &["collection", "record"]),
            "annotations": annotations("Create record", false, false),
        }),
        json!({
            "name": "apply_writes",
            "title": "Apply writes",
            "description": "Write several records to the signed-in account's own repository at once, all or none, as Bluesky's and Grain's apps do (a post and its threadgate; a gallery, its photos and their items; a profile). writes is a template (crate::compose): each { collection, record, as?, each?, when?, whenItem?, rkey?, update? }, its record's values literal or {\"$value\": name}, {\"$item\": path}, {\"$index\": true}, {\"$each\": name, \"value\": …}, {\"$when\": [names], \"value\": …}, {\"$first\": […]}, {\"$merge\": […]}, {\"$uri\": as}, {\"$rkey\": as}, {\"$stored\": path}, {\"$now\": true} or {\"$facets\": key}; values fills it. update, a record key (as social.grain.actor.profile's self) or {\"$value\": name} holding one (a gallery's), replaces the one record there: it is read first, {\"$stored\": path} reads it (an avatar's blob ref, kept), and the fields the record names are set on it, those made of nothing taken out, the rest kept; created where there is none. A view's Compose gives the template. Every record is checked against its lexicon first. Needs sign-in.",
            "inputSchema": schema(json!({
                "writes": { "type": "array", "items": { "type": "object" }, "description": "The writes, a template." },
                "values": { "type": "object", "description": "What the template's {\"$value\": name} reads." },
            }), &["writes"]),
            "annotations": annotations("Apply writes", false, false),
        }),
        json!({
            "name": "delete_record",
            "title": "Delete record",
            "description": "Delete one of the signed-in account's own records by its at-uri. Needs sign-in.",
            "inputSchema": schema(json!({
                "uri": { "type": "string", "description": "The record's at-uri, in the account's own repository." },
            }), &["uri"]),
            "annotations": annotations("Delete record", true, true),
        }),
    ]
}

/// A record to create, checked: an object, its `$type` the collection
/// (set where left out), valid against the collection's lexicon among
/// `docs` (its own and those it refs).
pub fn checked_record(
    collection: &str,
    record: Option<&Value>,
    docs: &[Arc<Value>],
) -> Result<Value, String> {
    let Some(Value::Object(fields)) = record else {
        return Err("record must be an object".into());
    };
    let mut fields = fields.clone();
    match fields.get("$type") {
        None => {
            fields.insert("$type".into(), collection.into());
        }
        Some(t) if t == collection => {}
        Some(_) => return Err(format!("record.$type must be {collection}, or left out")),
    }
    let is_record = docs
        .iter()
        .find(|d| d["id"] == collection)
        .is_some_and(|d| d["defs"]["main"]["type"] == "record");
    if !is_record {
        return Err(format!("{collection} is not a record type"));
    }
    let record = Value::Object(fields);
    Lexicons::new(docs.iter().map(|d| (**d).clone()))
        .validate(collection, &record)
        .map_err(|e| format!("The record does not match {collection}: {e}"))?;
    Ok(record)
}

/// The record a delete names, if it is in the account's own repository:
/// its DID, or its handle.
pub fn own_uri(uri: &str, did: &str, handle: Option<&str>) -> Result<AtUri, String> {
    let u = AtUri::parse(uri.trim())?;
    let own = u.authority == did || handle.is_some_and(|h| h.eq_ignore_ascii_case(&u.authority));
    if !own {
        return Err(format!(
            "Only your own records can be deleted, and {} is not your repository ({})",
            u.authority,
            handle.unwrap_or(did)
        ));
    }
    Ok(u)
}

impl Hud {
    /// A PDS's refusal of a write as `account`, in one line: its error's
    /// name and status, and what it says of why (`error_said`), quoted as
    /// its words; a refusal for want of `scope`, as `scope_refused` answers
    /// it.
    async fn write_refused(
        &self,
        account: &Account,
        got: &Fetched,
        what: &str,
        scope: &str,
    ) -> Refusal {
        let said = error_said(&got.body)
            .map(|m| format!(", saying \"{m}\""))
            .unwrap_or_default();
        match error_name(&got.body).as_deref() {
            Some("ScopeMissingError") => self.scope_refused(account, scope).await,
            Some(e) => Refusal::Error(format!(
                "Your PDS refused to {what}: {e} ({}){said}",
                got.status
            )),
            None => Refusal::Error(format!("Your PDS refused to {what} ({}){said}", got.status)),
        }
    }

    /// Nothing if HUD asks to write `collection`'s records
    /// (`scope::writes`), the collections its views write; else why it
    /// writes none, before asking the PDS.
    fn asks_to_write(&self, collection: &str) -> Result<(), Refusal> {
        let asked = self.oauth.as_ref().map_or("", |o| o.scope.as_str());
        match crate::scope::writes(asked, collection) {
            true => Ok(()),
            false => Err(format!(
                "HUD does not write {collection} records: it asks to write only those its views write"
            )
            .into()),
        }
    }

    /// Nothing if HUD asks to delete the records of every one of
    /// `collections` (`scope::deletes`): to write them, or to delete them
    /// alone, as a view's `deletes` names them; else, naming those it does
    /// not, why it deletes none of them: a person deleting their account
    /// learns what is left, and that nothing more went.
    fn asks_to_delete<'a>(
        &self,
        collections: impl Iterator<Item = &'a str>,
    ) -> Result<(), Refusal> {
        let asked = self.oauth.as_ref().map_or("", |o| o.scope.as_str());
        let mut left: Vec<&str> = collections
            .filter(|c| !crate::scope::deletes(asked, c))
            .collect();
        if left.is_empty() {
            return Ok(());
        }
        left.sort_unstable();
        left.dedup();
        Err(format!(
            "Some records can't be deleted here yet: {}. Signing in doesn't ask to delete them, so nothing more was deleted.",
            left.join(", ")
        )
        .into())
    }
}

/// A write's answer, as JSON.
fn answered(got: &Fetched) -> Result<Value, Refusal> {
    serde_json::from_slice(&got.body)
        .ok()
        .filter(Value::is_object)
        .ok_or_else(|| Refusal::Error("Your PDS answered with something unreadable".into()))
}

impl Hud {
    /// A record's fields `{"$facets": key}` made the facets of the text in
    /// its field `key` beside them (`richtext`): its links, tags, and
    /// mentions whose handles resolve, each to its DID; a field with none
    /// left out, as a text with no facets is written.
    async fn facets_made(&self, record: &mut Value) {
        fn marked(v: &Value, out: &mut Vec<String>) {
            match v {
                Value::Object(o) => {
                    for (k, x) in o {
                        if let Some(text) = x["$facets"]
                            .as_str()
                            .and_then(|f| o.get(f))
                            .and_then(Value::as_str)
                        {
                            out.extend(crate::richtext::marks(text).into_iter().filter_map(|m| {
                                match m.kind {
                                    crate::richtext::Kind::Mention(h) => Some(h),
                                    _ => None,
                                }
                            }));
                        } else if k != "$facets" {
                            marked(x, out);
                        }
                    }
                }
                Value::Array(a) => a.iter().for_each(|x| marked(x, out)),
                _ => {}
            }
        }
        fn made(v: &mut Value, dids: &std::collections::HashMap<String, String>) {
            match v {
                Value::Object(o) => {
                    let keys: Vec<String> = o.keys().cloned().collect();
                    for k in keys {
                        let of = o[&k]
                            .as_object()
                            .filter(|x| x.len() == 1)
                            .and_then(|x| x.get("$facets"));
                        if let Some(f) = of.and_then(Value::as_str).map(str::to_string) {
                            let text = o.get(&f).and_then(Value::as_str).unwrap_or("");
                            let facets: Vec<Value> = crate::richtext::marks(text)
                                .iter()
                                .filter_map(|m| {
                                    let did = match &m.kind {
                                        crate::richtext::Kind::Mention(h) => {
                                            Some(dids.get(h)?.as_str())
                                        }
                                        _ => None,
                                    };
                                    crate::richtext::facet(m, did)
                                })
                                .collect();
                            if facets.is_empty() {
                                o.remove(&k);
                            } else {
                                o.insert(k, Value::Array(facets));
                            }
                        } else {
                            made(o.get_mut(&k).expect("a key of o's"), dids);
                        }
                    }
                }
                Value::Array(a) => a.iter_mut().for_each(|x| made(x, dids)),
                _ => {}
            }
        }
        let mut handles = Vec::new();
        marked(record, &mut handles);
        handles.sort();
        handles.dedup();
        let found = futures::future::join_all(handles.iter().map(|h| self.handle_did(h))).await;
        let dids = handles
            .into_iter()
            .zip(found)
            .filter_map(|(h, d)| Some((h, d.ok()?)))
            .collect();
        made(record, &dids);
    }

    pub(crate) async fn create_record_tool(
        &self,
        arguments: Option<&Value>,
        account: Option<&Account>,
    ) -> Result<Output, Refusal> {
        let Some(account) = account else {
            return Err(Refusal::SignIn);
        };
        let args = Args::of(arguments, &["collection", "record", "rkey"])?;
        let collection = args.required("collection")?.trim();
        if !atproto::syntax::is_nsid(collection) {
            return Err(format!("Not a collection's NSID: {collection}").into());
        }
        self.asks_to_write(collection)?;
        let rkey = args.text("rkey")?.map(str::trim).filter(|k| !k.is_empty());
        if let Some(k) = rkey
            && !atproto::syntax::is_record_key(k)
        {
            return Err(format!("Not a record key: {k}").into());
        }
        if self.lexicon_doc(collection).await.is_none() {
            return Err(format!(
                "No lexicon for {collection}: HUD writes only records whose lexicon resolves"
            )
            .into());
        }
        let docs = self.lexicon_docs(collection).await;
        let mut record = args.0.get("record").cloned().unwrap_or(Value::Null);
        self.facets_made(&mut record).await;
        let record = checked_record(collection, Some(&record), &docs)?;
        let mut input = json!({ "repo": account.did, "collection": collection, "record": record });
        if let Some(k) = rkey {
            input["rkey"] = k.into();
        }
        let got = self
            .pds_write(account, "com.atproto.repo.createRecord", &input, None)
            .await?;
        if got.status != 200 {
            let what = format!("create a {collection} record");
            let scope = format!("repo:{collection}?action=create");
            return Err(self.write_refused(account, &got, &what, &scope).await);
        }
        let v = answered(&got)?;
        let uri = v["uri"]
            .as_str()
            .and_then(|u| AtUri::parse(u).ok())
            .filter(|u| u.authority == account.did && u.collection == collection)
            .map(|u| u.with_did(&account.did));
        let (Some(uri), Some(cid)) = (uri, v["cid"].as_str()) else {
            return Err("Your PDS answered without the record's uri and cid".into());
        };
        Ok(Output {
            text: format!("Created {uri} ({collection})."),
            structured: json!({ "uri": uri, "cid": cid }),
            meta: json!({}),
        })
    }

    /// The record at `collection`/`rkey` in the account's own repository as
    /// its PDS keeps it (its blobs' refs as they are, not the URLs an
    /// AppView gives), for an update to lay its fields over; none where
    /// there is none yet.
    async fn stored(
        &self,
        account: &Account,
        collection: &str,
        rkey: &str,
    ) -> Result<Option<Value>, Refusal> {
        let query = [
            ("repo", account.did.as_str()),
            ("collection", collection),
            ("rkey", rkey),
        ]
        .map(|(k, v)| (k.to_string(), v.to_string()));
        let got = self
            .pds_call(account, "com.atproto.repo.getRecord", &query, None, None)
            .await?;
        let uri = format!("at://{}/{collection}/{rkey}", account.did);
        match (got.status, error_name(&got.body)) {
            (200, _) => answered(&got)?
                .get("value")
                .filter(|v| v.is_object())
                .map(|v| Some(v.clone()))
                .ok_or_else(|| format!("Your PDS answered {uri} without its record").into()),
            (_, Some(e)) if e == "RecordNotFound" => Ok(None),
            (status, e) => Err(format!(
                "Your PDS refused to read {uri}{} ({status})",
                e.map_or(String::new(), |e| format!(": {e}"))
            )
            .into()),
        }
    }

    /// `apply_writes`: a template's records (`compose::expand`) made with
    /// their record keys, each's facets made and checked against its
    /// lexicon, then written at once by the account's PDS
    /// (`com.atproto.repo.applyWrites`), all or none: each created, or an
    /// update's laid over the record it replaces, read first.
    pub(crate) async fn apply_writes_tool(
        &self,
        arguments: Option<&Value>,
        account: Option<&Account>,
    ) -> Result<Output, Refusal> {
        static CLOCK: std::sync::LazyLock<atproto::tid::TidClock> =
            std::sync::LazyLock::new(|| {
                let bytes = atproto::jose::unb64(&atproto::jose::random(2)).unwrap_or_default();
                atproto::tid::TidClock::new(bytes.iter().fold(0, |n, b| (n << 8) | u64::from(*b)))
            });
        let Some(account) = account else {
            return Err(Refusal::SignIn);
        };
        let args = Args::of(arguments, &["writes", "values"])?;
        let values = match args.0.get("values") {
            None | Some(Value::Null) => serde_json::Map::new(),
            Some(Value::Object(o)) => o.clone(),
            Some(_) => return Err("values must be an object".into()),
        };
        let writes = args.0.get("writes").cloned().unwrap_or(Value::Null);
        let mut stored = Vec::new();
        for read in crate::compose::updated(&writes, &values)? {
            stored.push(match read {
                Some((collection, rkey)) => self.stored(account, &collection, &rkey).await?,
                None => None,
            });
        }
        let now = atproto::time::iso(atproto::time::now_millis());
        // `updated` refused what `expand` would: its `?` shares its line.
        let tid = || CLOCK.next();
        let made = crate::compose::expand(&writes, &values, &stored, &account.did, &now, tid)?;
        if made.is_empty() {
            return Err("Nothing to write: no write's when holds".into());
        }
        let (mut creates, mut kinds) = (Vec::new(), Vec::new());
        for m in made {
            self.asks_to_write(&m.collection)?;
            if self.lexicon_doc(&m.collection).await.is_none() {
                return Err(format!(
                    "No lexicon for {}: HUD writes only records whose lexicon resolves",
                    m.collection
                )
                .into());
            }
            let docs = self.lexicon_docs(&m.collection).await;
            let mut record = m.record;
            self.facets_made(&mut record).await;
            // An update over the record stored; where there is none, a create.
            let (kind, record) = match m.update {
                Some(u) if u.stored.is_some() => ("update", u.over(record)),
                _ => ("create", record),
            };
            let record = checked_record(&m.collection, Some(&record), &docs)?;
            kinds.push(kind);
            creates.push(
                json!({ "$type": format!("com.atproto.repo.applyWrites#{kind}"),
                "collection": m.collection, "rkey": m.rkey, "value": record }),
            );
        }
        // What it asks of the PDS: create, or write where it updates too;
        // and the permission the first write needs.
        let (verb, done) = match kinds.contains(&"update") {
            true => ("write", "Wrote"),
            false => ("create", "Created"),
        };
        let scope = format!(
            "repo:{}?action={}",
            creates[0]["collection"].as_str().unwrap_or_default(),
            kinds[0]
        );
        // No `validate`: true would have the PDS refuse every record whose
        // lexicon it does not know (a Grain comment: "Unknown lexicon
        // type"), as createRecord without it does not. It still checks
        // those it knows; HUD checked each against its own lexicon above.
        let input = json!({ "repo": account.did, "writes": creates });
        let got = self
            .pds_write(account, "com.atproto.repo.applyWrites", &input, None)
            .await?;
        if got.status != 200 {
            let what = format!("{verb} {} records", creates.len());
            return Err(self.write_refused(account, &got, &what, &scope).await);
        }
        let v = answered(&got)?;
        let results: Vec<Value> = v["results"]
            .as_array()
            .into_iter()
            .flatten()
            .filter_map(|r| {
                let uri = AtUri::parse(r["uri"].as_str()?).ok()?;
                (uri.authority == account.did)
                    .then(|| json!({ "uri": uri.with_did(&account.did), "cid": r["cid"] }))
            })
            .collect();
        let first = match results.first() {
            Some(r) if results.len() == creates.len() => r["uri"].as_str().unwrap_or_default(),
            _ => return Err("Your PDS answered without each record's uri and cid".into()),
        };
        Ok(Output {
            text: match results.len() {
                1 => format!("{done} {first}."),
                n => format!("{done} {n} records, the first {first}."),
            },
            structured: json!({ "results": results }),
            meta: json!({}),
        })
    }

    pub(crate) async fn delete_record_tool(
        &self,
        arguments: Option<&Value>,
        account: Option<&Account>,
    ) -> Result<Output, Refusal> {
        let Some(account) = account else {
            return Err(Refusal::SignIn);
        };
        let args = Args::of(arguments, &["uri"])?;
        let u = own_uri(
            args.required("uri")?,
            &account.did,
            account.handle.as_deref(),
        )?;
        self.asks_to_write(&u.collection)?;
        let input = json!({ "repo": account.did, "collection": u.collection, "rkey": u.rkey });
        let got = self
            .pds_write(account, "com.atproto.repo.deleteRecord", &input, None)
            .await?;
        let uri = u.with_did(&account.did);
        if got.status != 200 {
            let what = format!("delete {uri}");
            let scope = format!("repo:{}?action=delete", u.collection);
            return Err(self.write_refused(account, &got, &what, &scope).await);
        }
        Ok(Output {
            text: format!("Deleted {uri}."),
            structured: json!({ "uri": uri }),
            meta: json!({}),
        })
    }
}

/// A delete's `$type` in `com.atproto.repo.applyWrites`.
const DELETE: &str = "com.atproto.repo.applyWrites#delete";

/// The most writes one `com.atproto.repo.applyWrites` takes, as the
/// reference PDS takes them.
const MOST_WRITES: usize = 200;

/// The deletes a deletion plan, `method`'s answer, names (`{ writes: [{
/// collection, rkey }] }`, each as `com.atproto.repo.applyWrites#delete`
/// names one, its `$type` that or left out), each its collection and
/// record key: at least one, at most `MOST_WRITES`, as one commit takes
/// them; else why it is no plan to commit.
pub fn planned_deletes(method: &str, answer: &Value) -> Result<Vec<(String, String)>, String> {
    let Some(writes) = answer["writes"].as_array() else {
        return Err(format!("{method} answered no writes to delete"));
    };
    if writes.is_empty() {
        return Err(format!("{method} planned nothing to delete"));
    }
    if writes.len() > MOST_WRITES {
        return Err(format!(
            "{method} planned {} deletes, more than the {MOST_WRITES} one commit takes",
            writes.len()
        ));
    }
    let one = |(i, w): (usize, &Value)| {
        let (kind, c, k) = (
            w["$type"].as_str(),
            w["collection"].as_str(),
            w["rkey"].as_str(),
        );
        match (kind, c, k) {
            (None | Some(DELETE), Some(c), Some(k))
                if atproto::syntax::is_nsid(c) && atproto::syntax::is_record_key(k) =>
            {
                Ok((c.to_string(), k.to_string()))
            }
            _ => Err(format!(
                "{method} planned, as its write {i}, something other than a record's delete"
            )),
        }
    };
    writes.iter().enumerate().map(one).collect()
}

/// A `then`'s input with each `{"$did": true}` in it made `did`.
fn stamped(v: &Value, did: &str) -> Value {
    match v {
        _ if *v == json!({ "$did": true }) => did.into(),
        Value::Object(o) => o
            .iter()
            .map(|(k, x)| (k.clone(), stamped(x, did)))
            .collect(),
        Value::Array(a) => a.iter().map(|x| stamped(x, did)).collect(),
        v => v.clone(),
    }
}

impl Hud {
    /// An `Action`'s or select's `deletes` (`call_procedure`, as a trusted
    /// view deletes what `method` of `service` plans): the query asked as
    /// the account, now, with `params`, held to its lexicon as the `query`
    /// tool holds one; its plan (`planned_deletes`) committed in the
    /// account's own repository in one `com.atproto.repo.applyWrites`, all
    /// or none, of collections HUD asks to write or delete alone; then read
    /// back by the app's `refresh`, as every write is (`pds_write`). One the
    /// view says repeats (`view::Plan`) is asked again by the client until a
    /// plan comes back empty, so an account's data goes a part at a time and
    /// a run cut short carries on, answered `done` once nothing is left;
    /// only the operator's own clients delete so (`Auth::from_own_client`),
    /// never another MCP client the person connected. Its `then` is called
    /// once nothing is left (one that does not repeat, once committed), as
    /// the account, its input's `{"$did": true}` their DID: the app's
    /// server forgetting them.
    pub(crate) async fn delete_planned(
        &self,
        account: &Account,
        method: &str,
        service: &crate::xrpc::Service,
        params: serde_json::Map<String, Value>,
    ) -> Result<Output, Refusal> {
        let plan = crate::view::views().plan(method, &service.name());
        self.delete_by(account, method, plan, service, params).await
    }

    /// `delete_planned` with what the view says beyond its query, `plan`.
    async fn delete_by(
        &self,
        account: &Account,
        method: &str,
        plan: Option<&crate::view::Plan>,
        service: &crate::xrpc::Service,
        mut params: serde_json::Map<String, Value>,
    ) -> Result<Output, Refusal> {
        let repeat = plan.is_some_and(|p| p.repeat);
        let then = plan.and_then(|p| p.then.as_ref());
        if repeat && !self.auth()?.from_own_client(account).await {
            return Err(
                "An account's data is deleted from the app's own pages or app alone, not another MCP client"
                    .into(),
            );
        }
        let docs = self.lexicon_docs(method).await;
        let caller = crate::xrpc::Caller::Account(account);
        let asker = crate::xrpc::As { hud: self, caller };
        let asked =
            crate::xrpc::ask_with(&asker, &docs, service, method, &mut params, true).await?;
        if repeat
            && asked.response["writes"]
                .as_array()
                .is_some_and(Vec::is_empty)
        {
            if let Some(then) = then {
                self.then_call(account, then).await?;
            }
            tracing::info!(%method, service = %service.name(), "deleted all that was planned");
            return Ok(Output {
                text: "Nothing is left to delete.".into(),
                structured: json!({ "output": { "deleted": [], "done": true } }),
                meta: json!({}),
            });
        }
        let planned = planned_deletes(method, &asked.response)?;
        self.asks_to_delete(planned.iter().map(|(c, _)| c.as_str()))?;
        let writes: Vec<Value> = planned
            .iter()
            .map(|(c, k)| json!({ "$type": DELETE, "collection": c, "rkey": k }))
            .collect();
        let input = json!({ "repo": account.did, "writes": writes });
        let got = self
            .pds_write(account, "com.atproto.repo.applyWrites", &input, None)
            .await?;
        let n = planned.len();
        if got.status != 200 {
            let what = format!("delete {n} records at once, so it deleted none of them");
            let scope = format!("repo:{}?action=delete", planned[0].0);
            return Err(self.write_refused(account, &got, &what, &scope).await);
        }
        let deleted: Vec<String> = planned
            .iter()
            .map(|(c, k)| format!("at://{}/{c}/{k}", account.did))
            .collect();
        tracing::info!(%method, service = %service.name(), records = n, "deleted as planned");
        let mut output = json!({ "deleted": deleted });
        match (repeat, then) {
            (true, _) => output["done"] = false.into(),
            (false, Some(then)) => self.then_call(account, then).await?,
            (false, None) => {}
        }
        Ok(Output {
            text: match n {
                1 => format!("Deleted {}.", deleted[0]),
                n => format!("Deleted {n} records, as {method} planned."),
            },
            structured: json!({ "output": output }),
            meta: json!({}),
        })
    }

    /// A plan's `then`, called as the account through its PDS once nothing
    /// is left: its input's `{"$did": true}` their DID.
    async fn then_call(&self, account: &Account, then: &crate::view::Call) -> Result<(), Refusal> {
        let input = stamped(&then.input, &account.did);
        let (method, service) = (then.method.as_str(), then.service.as_str());
        let got = self
            .pds_write(account, method, &input, Some(service))
            .await?;
        match got.status {
            200 => Ok(()),
            _ => Err(self.service_refused(account, &got, method, service).await),
        }
    }
}

/// The records a write of `nsid` with `input`, answered `body`, made,
/// changed or deleted, each by its collection and URI: a record created or
/// put, by the URI its PDS answered; those `applyWrites` wrote and the one
/// `deleteRecord` deleted, by the keys their input names. None for any
/// other method.
pub(crate) fn written_uris(nsid: &str, input: &Value, body: &[u8]) -> Vec<(String, String)> {
    let repo = input["repo"].as_str().unwrap_or_default();
    let at = |w: &Value| {
        let (c, k) = (w["collection"].as_str()?, w["rkey"].as_str()?);
        Some((c.to_string(), format!("at://{repo}/{c}/{k}")))
    };
    match nsid {
        "com.atproto.repo.createRecord" | "com.atproto.repo.putRecord" => {
            let answered: Value = serde_json::from_slice(body).unwrap_or_default();
            let uri = answered["uri"].as_str().and_then(|u| AtUri::parse(u).ok());
            uri.map(|u| (u.collection.clone(), u.with_did(&u.authority)))
                .into_iter()
                .collect()
        }
        "com.atproto.repo.applyWrites" => {
            let writes = input["writes"].as_array().into_iter().flatten();
            writes.filter_map(at).collect()
        }
        "com.atproto.repo.deleteRecord" => at(input).into_iter().collect(),
        _ => Vec::new(),
    }
}

impl Hud {
    /// Have each app whose own collections `written` holds a record of
    /// read those records back from the account's PDS at once, by calling
    /// its `refresh` (`view::Refresh`) with `{"uris": [...]}`, at most
    /// `REFRESH_URIS` a call, as the account, through its PDS: so what its
    /// views read next shows the write, rather than when the network
    /// brings it to the app's server. The calls go out together, so a
    /// gallery of many photos waits for one round trip, not one a call. One
    /// that fails is logged, and the write stands.
    pub(crate) async fn refresh_written(&self, account: &Account, written: &[(String, String)]) {
        let refreshes = crate::view::views().refreshes();
        let calls = refreshes.iter().flat_map(|r| {
            let uris: Vec<&str> = written
                .iter()
                .filter(|(c, _)| r.collections.contains(c))
                .map(|(_, uri)| uri.as_str())
                .collect();
            let chunks: Vec<Value> = uris
                .chunks(REFRESH_URIS)
                .map(|uris| json!({ "uris": uris }))
                .collect();
            chunks.into_iter().map(move |input| async move {
                let got = self
                    .pds_call(account, &r.method, &[], Some(&input), Some(&r.service))
                    .await;
                let status = got.as_ref().map(|f| f.status);
                if status != Ok(200) {
                    let method = &r.method;
                    tracing::warn!(method, ?status, "an app's refresh after a write failed");
                }
            })
        });
        futures::future::join_all(calls).await;
    }
}

/// The most URIs one call of an app's `refresh` names, as opensocial's
/// `refreshRecords` and Grain's `indexRecords` take them.
const REFRESH_URIS: usize = 25;

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use std::sync::Mutex;

    fn like_docs() -> Vec<Arc<Value>> {
        ["app.bsky.feed.like", "com.atproto.repo.strongRef"]
            .iter()
            .map(|n| crate::lexicon::bundled(n).unwrap())
            .collect()
    }

    #[test]
    fn records_checked_against_their_lexicon() {
        let docs = like_docs();
        let subject = json!({ "uri": "at://did:plc:a/app.bsky.feed.post/3k2a",
            "cid": "bafyreie5737gdxlw5i64vzichcalba3z2v5n6icifvx5xytvske7mr3hpm" });
        let ok = checked_record(
            "app.bsky.feed.like",
            Some(&json!({ "subject": subject, "createdAt": "2026-11-14T12:00:00.000Z" })),
            &docs,
        )
        .unwrap();
        assert_eq!(ok["$type"], "app.bsky.feed.like");
        // createdAt is left as given; a record missing what its lexicon requires is refused.
        assert_eq!(ok["createdAt"], "2026-11-14T12:00:00.000Z");
        let err = checked_record(
            "app.bsky.feed.like",
            Some(&json!({ "createdAt": "2026-11-14T12:00:00.000Z" })),
            &docs,
        )
        .unwrap_err();
        assert!(
            err.starts_with("The record does not match app.bsky.feed.like: "),
            "{err}"
        );
        assert!(err.contains("subject"), "{err}");
        let err = checked_record(
            "app.bsky.feed.like",
            Some(&json!({ "subject": { "uri": "not a uri", "cid": "x" },
                          "createdAt": "2026-11-14T12:00:00.000Z" })),
            &docs,
        )
        .unwrap_err();
        assert!(err.contains("subject"), "{err}");
        assert!(
            checked_record(
                "app.bsky.feed.like",
                Some(&json!({ "$type": "app.bsky.feed.post" })),
                &docs
            )
            .unwrap_err()
            .contains("$type")
        );
        assert_eq!(
            checked_record("app.bsky.feed.like", Some(&json!([])), &docs).unwrap_err(),
            "record must be an object"
        );
        let strong = vec![crate::lexicon::bundled("com.atproto.repo.strongRef").unwrap()];
        assert_eq!(
            checked_record("com.atproto.repo.strongRef", Some(&json!({})), &strong).unwrap_err(),
            "com.atproto.repo.strongRef is not a record type"
        );
    }

    #[test]
    fn deletes_only_your_own() {
        let did = "did:plc:alice";
        assert!(own_uri("at://did:plc:alice/app.bsky.feed.like/3k", did, None).is_ok());
        assert!(
            own_uri(
                "at://Alice.Test/app.bsky.feed.like/3k",
                did,
                Some("alice.test")
            )
            .is_ok()
        );
        let err = own_uri(
            "at://did:plc:bob/app.bsky.feed.like/3k",
            did,
            Some("alice.test"),
        )
        .unwrap_err();
        assert!(err.starts_with("Only your own records"), "{err}");
        assert!(
            own_uri(
                "at://bob.test/app.bsky.feed.like/3k",
                did,
                Some("alice.test")
            )
            .is_err()
        );
        assert!(own_uri("at://did:plc:alice", did, None).is_err());
    }

    /// The account the fake signs in, and another's DID.
    pub(crate) const DID: &str = "did:plc:alice";
    const BOB: &str = "did:plc:bob";
    pub(crate) const CID: &str = "bafyreie5737gdxlw5i64vzichcalba3z2v5n6icifvx5xytvske7mr3hpm";

    /// A request the fake was sent.
    pub(crate) struct Asked {
        pub path: String,
        pub query: String,
        pub headers: axum::http::HeaderMap,
        pub body: Vec<u8>,
    }

    impl Asked {
        pub(crate) fn json(&self) -> Value {
            serde_json::from_slice(&self.body).unwrap()
        }
    }

    /// What the fake answers: its status, its content type (none if
    /// empty) and its body.
    pub(crate) type Answer = (u16, &'static str, Vec<u8>);

    pub(crate) fn json_answer(status: u16, v: Value) -> Answer {
        (status, "application/json", v.to_string().into_bytes())
    }

    /// HUD with sign-in on, signed in offline as `DID` (alice.test) at a
    /// fake on a loopback port, which is its PDS, handle resolver and
    /// anything else asked of it, each answered by `answer`; and what it
    /// was asked.
    pub(crate) struct Served {
        pub hud: Arc<Hud>,
        pub account: Account,
        pub url: String,
        pub asked: Arc<Mutex<Vec<Asked>>>,
    }

    impl Served {
        /// The requests made to `path`.
        pub(crate) fn sent(&self, path: &str) -> Vec<Value> {
            self.asked
                .lock()
                .unwrap()
                .iter()
                .filter(|a| a.path == path)
                .map(Asked::json)
                .collect()
        }
    }

    pub(crate) async fn served(
        answer: impl Fn(&Asked) -> Answer + Send + Sync + 'static,
    ) -> Served {
        type Shared = (
            Arc<dyn Fn(&Asked) -> Answer + Send + Sync>,
            Arc<Mutex<Vec<Asked>>>,
        );
        async fn handle(
            axum::extract::State((answer, asked)): axum::extract::State<Shared>,
            uri: axum::http::Uri,
            headers: axum::http::HeaderMap,
            body: bytes::Bytes,
        ) -> axum::response::Response {
            let a = Asked {
                path: uri.path().into(),
                query: uri.query().unwrap_or_default().into(),
                headers,
                body: body.to_vec(),
            };
            let (status, kind, bytes) = answer(&a);
            asked.lock().unwrap().push(a);
            let mut r = axum::response::Response::builder().status(status);
            if !kind.is_empty() {
                r = r.header("content-type", kind);
            }
            r.body(axum::body::Body::from(bytes)).unwrap()
        }
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let url = format!("http://{}", listener.local_addr().unwrap());
        let asked = Arc::new(Mutex::new(Vec::new()));
        let app = axum::Router::new().fallback(handle).with_state((
            Arc::new(answer) as Arc<dyn Fn(&Asked) -> Answer + Send + Sync>,
            asked.clone(),
        ));
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        let dir = std::env::temp_dir().join(format!("hud-writes-{}", atproto::jose::random(6)));
        let hud = Hud::new(crate::Config {
            port: 0,
            bind: "::".parse().unwrap(),
            public_origin: "https://hud.test".into(),
            cache_dir: dir.join("cache"),
            blob_cache_bytes: 0,
            cache_memory_bytes: 1 << 24,
            data_dir: dir,
            oauth: true,
            // And test.asked.thing, a collection asked for whose lexicon
            // does not resolve.
            oauth_scope: format!(
                "{} repo:test.asked.thing",
                crate::scope::asked(crate::view::views(), true, false)
            ),
            oauth_include: None,
            workspace: true,
            sign_up_pds: crate::config::SIGN_UP_PDS.into(),
            client: Default::default(),
            operators: Vec::new(),
            logs: false,
            logs_keep: crate::logs::KEEP,
            spaces: false,
            plc_url: url.clone(),
            handle_resolver: Some(url.clone()),
            allow_private: true,
            backlinks: crate::backlinks::Backlinks::None,
            favicons: None,
            image_cdn: None,
            relay: None,
        })
        .unwrap();
        let auth = hud.auth().unwrap();
        let token = auth.signed_in_offline_at(DID, Some("alice.test"), false, &url);
        let account = auth.account(&token).unwrap();
        Served {
            hud,
            account,
            url,
            asked,
        }
    }

    /// A port nothing listens on.
    pub(crate) async fn closed_port() -> u16 {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        listener.local_addr().unwrap().port()
    }

    /// `nsid` kept as resolving to no lexicon, as though its resolution
    /// had answered none: no lookup made.
    fn unresolved(hud: &Hud, nsid: &str) {
        let dir = hud.config.cache_dir.join("lexicons");
        std::fs::create_dir_all(&dir).unwrap();
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let kept = json!({ "nsid": nsid, "fetchedAt": now, "doc": null });
        std::fs::write(dir.join(format!("{nsid}.json")), kept.to_string()).unwrap();
    }

    /// The fake's answers: `bob.example.com` resolves to Bob, no other
    /// handle; a write is answered by `next` where it is set, else as a
    /// PDS does, its records in the account's repository.
    fn pds(next: Arc<Mutex<Option<Answer>>>) -> impl Fn(&Asked) -> Answer + Send + Sync {
        move |a| match a.path.as_str() {
            "/xrpc/com.atproto.identity.resolveHandle" => match a.query.as_str() {
                "handle=bob.example.com" => json_answer(200, json!({ "did": BOB })),
                _ => json_answer(400, json!({ "error": "InvalidRequest" })),
            },
            _ if next.lock().unwrap().is_some() => next.lock().unwrap().clone().unwrap(),
            "/xrpc/com.atproto.repo.createRecord" => {
                let input = a.json();
                let rkey = input["rkey"].as_str().unwrap_or("3kmade");
                let uri = format!(
                    "at://{}/{}/{rkey}",
                    input["repo"].as_str().unwrap(),
                    input["collection"].as_str().unwrap()
                );
                json_answer(200, json!({ "uri": uri, "cid": CID }))
            }
            "/xrpc/com.atproto.repo.applyWrites" => {
                let input = a.json();
                let results: Vec<Value> = input["writes"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|w| {
                        let uri = format!("at://{}/{}/{}", input["repo"].as_str().unwrap(), w["collection"].as_str().unwrap(), w["rkey"].as_str().unwrap());
                        json!({ "$type": "com.atproto.repo.applyWrites#createResult", "uri": uri, "cid": CID })
                    })
                    .collect();
                json_answer(200, json!({ "results": results }))
            }
            _ => json_answer(200, json!({})),
        }
    }

    /// What a refused call tells the person: its error, or to sign in.
    pub(crate) fn refused(r: Result<Output, Refusal>) -> String {
        match r.unwrap_err() {
            Refusal::Error(e) => e,
            Refusal::SignIn => crate::tools::SIGN_IN.into(),
        }
    }

    #[tokio::test]
    async fn creates_a_record_as_the_account() {
        let next = Arc::new(Mutex::new(None));
        let s = served(pds(next.clone())).await;
        let set = |a: Option<Answer>| *next.lock().unwrap() = a;
        let create = async |args: Value| {
            s.hud
                .create_record_tool(Some(&args), Some(&s.account))
                .await
        };
        let subject = json!({ "uri": format!("at://{BOB}/app.bsky.feed.post/3k2a"), "cid": CID });
        let like = json!({ "collection": "app.bsky.feed.like",
            "record": { "subject": subject, "createdAt": "2026-11-14T12:00:00.000Z" } });
        // Signed out: sign in.
        assert_eq!(
            refused(s.hud.create_record_tool(Some(&like), None).await),
            crate::tools::SIGN_IN
        );
        // With a record key of its own, sent trimmed.
        let mut keyed = like.clone();
        keyed["rkey"] = " 3kself ".into();
        let out = create(keyed).await.unwrap();
        let uri = format!("at://{DID}/app.bsky.feed.like/3kself");
        assert_eq!(out.text, format!("Created {uri} (app.bsky.feed.like)."));
        assert_eq!(out.structured, json!({ "uri": uri, "cid": CID }));
        let sent = s.sent("/xrpc/com.atproto.repo.createRecord");
        assert_eq!(sent[0]["rkey"], "3kself");
        assert_eq!(sent[0]["repo"], DID);
        assert_eq!(sent[0]["record"]["$type"], "app.bsky.feed.like");
        // Refused before it is sent: not an NSID, not a record key, a
        // collection HUD's views don't write, one whose lexicon does not
        // resolve.
        let mut bad = like.clone();
        bad["collection"] = "not an nsid".into();
        assert_eq!(
            refused(create(bad).await),
            "Not a collection's NSID: not an nsid"
        );
        let mut bad = like.clone();
        bad["rkey"] = "a/b".into();
        assert_eq!(refused(create(bad).await), "Not a record key: a/b");
        let mut bad = like.clone();
        bad["collection"] = "test.nowhere.thing".into();
        assert_eq!(
            refused(create(bad).await),
            "HUD does not write test.nowhere.thing records: it asks to write only those its views write"
        );
        unresolved(&s.hud, "test.asked.thing");
        let mut bad = like.clone();
        bad["collection"] = "test.asked.thing".into();
        assert_eq!(
            refused(create(bad).await),
            "No lexicon for test.asked.thing: HUD writes only records whose lexicon resolves"
        );
        assert_eq!(s.sent("/xrpc/com.atproto.repo.createRecord").len(), 1);
        // The PDS's refusals: its error's name and what it says of why;
        // for want of scope, what to do.
        set(Some(json_answer(
            400,
            json!({ "error": "InvalidRecord", "message": "Invalid app.bsky.feed.like record:\nsubject must be an object" }),
        )));
        assert_eq!(
            refused(create(like.clone()).await),
            "Your PDS refused to create a app.bsky.feed.like record: InvalidRecord (400), saying \"Invalid app.bsky.feed.like record: subject must be an object\""
        );
        set(Some(json_answer(500, json!({ "message": "overloaded" }))));
        assert_eq!(
            refused(create(like.clone()).await),
            "Your PDS refused to create a app.bsky.feed.like record (500), saying \"overloaded\""
        );
        set(Some(json_answer(
            403,
            json!({ "error": "ScopeMissingError" }),
        )));
        assert_eq!(
            refused(create(like.clone()).await),
            "Your sign-in does not allow repo:app.bsky.feed.like?action=create"
        );
        set(Some((502, "text/html", b"<h1>Bad gateway</h1>".to_vec())));
        assert_eq!(
            refused(create(like.clone()).await),
            "Your PDS refused to create a app.bsky.feed.like record (502)"
        );
        // Answers it cannot use: unreadable, or not naming the record made
        // in the account's own repository.
        for (answer, why) in [
            (
                (200, "application/json", b"not json".to_vec()),
                "Your PDS answered with something unreadable",
            ),
            (
                json_answer(200, json!([])),
                "Your PDS answered with something unreadable",
            ),
            (
                json_answer(
                    200,
                    json!({ "uri": format!("at://{BOB}/app.bsky.feed.like/1"), "cid": CID }),
                ),
                "Your PDS answered without the record's uri and cid",
            ),
            (
                json_answer(
                    200,
                    json!({ "uri": format!("at://{DID}/app.bsky.feed.post/1"), "cid": CID }),
                ),
                "Your PDS answered without the record's uri and cid",
            ),
            (
                json_answer(
                    200,
                    json!({ "uri": format!("at://{DID}/app.bsky.feed.like/1") }),
                ),
                "Your PDS answered without the record's uri and cid",
            ),
        ] {
            set(Some(answer));
            assert_eq!(refused(create(like.clone()).await), why);
        }
    }

    /// A text's facets made wherever a record marks one, at any depth: a
    /// mention whose handle resolves made its DID, one that does not left
    /// unmarked; a mark naming no text, left out.
    #[tokio::test]
    async fn facets_made_where_marked() {
        let s = served(pds(Arc::new(Mutex::new(None)))).await;
        let text = "hi @bob.example.com and @nobody.example.com";
        let post = json!({ "collection": "app.bsky.feed.post", "record": {
            "text": text, "facets": { "$facets": "text" }, "langs": ["en"],
            "createdAt": "2026-11-14T12:00:00.000Z",
            "embed": { "$type": "app.bsky.embed.images", "images": [{
                "alt": "@bob.example.com", "facets": { "$facets": "nothing" },
                "image": { "$type": "blob", "ref": { "$link": CID }, "mimeType": "image/png", "size": 10 } }] } } });
        s.hud
            .create_record_tool(Some(&post), Some(&s.account))
            .await
            .unwrap();
        let record = &s.sent("/xrpc/com.atproto.repo.createRecord")[0]["record"];
        let at = text.find("@bob").unwrap();
        assert_eq!(
            record["facets"],
            json!([{ "index": { "byteStart": at, "byteEnd": at + "@bob.example.com".len() },
                     "features": [{ "$type": "app.bsky.richtext.facet#mention", "did": BOB }] }])
        );
        assert_eq!(record["langs"], json!(["en"]));
        assert_eq!(record["embed"]["images"][0].get("facets"), None, "{record}");
    }

    #[tokio::test]
    async fn applies_a_composers_writes() {
        let next = Arc::new(Mutex::new(None));
        let s = served(pds(next.clone())).await;
        let set = |a: Option<Answer>| *next.lock().unwrap() = a;
        let apply =
            async |args: Value| s.hud.apply_writes_tool(Some(&args), Some(&s.account)).await;
        let writes = json!([
            { "as": "post", "collection": "app.bsky.feed.post", "record": {
                "text": { "$value": "text" }, "createdAt": { "$now": true } } },
            { "when": ["gate"], "rkey": { "$rkey": "post" }, "collection": "app.bsky.feed.threadgate", "record": {
                "post": { "$uri": "post" }, "allow": { "$value": "gate" }, "createdAt": { "$now": true } } },
        ]);
        let args = |values: Value| json!({ "writes": writes, "values": values });
        assert_eq!(
            refused(
                s.hud
                    .apply_writes_tool(Some(&args(json!({ "text": "hi" }))), None)
                    .await
            ),
            crate::tools::SIGN_IN
        );
        // One record: its key a TID, made now, sent without `validate`, so
        // the PDS checks it only where it knows its lexicon.
        let out = apply(args(json!({ "text": "hi" }))).await.unwrap();
        let sent = s.sent("/xrpc/com.atproto.repo.applyWrites");
        assert_eq!(sent[0]["repo"], DID);
        assert_eq!(sent[0].get("validate"), None);
        let create = &sent[0]["writes"][0];
        let rkey = create["rkey"].as_str().unwrap();
        assert!(
            atproto::syntax::is_record_key(rkey) && rkey.len() == 13,
            "{rkey}"
        );
        assert_eq!(create["$type"], "com.atproto.repo.applyWrites#create");
        assert_eq!(create["value"]["$type"], "app.bsky.feed.post");
        assert_eq!(create["value"]["text"], "hi");
        assert!(
            create["value"]["createdAt"]
                .as_str()
                .unwrap()
                .ends_with('Z')
        );
        let post = format!("at://{DID}/app.bsky.feed.post/{rkey}");
        assert_eq!(out.text, format!("Created {post}."));
        assert_eq!(
            out.structured,
            json!({ "results": [{ "uri": post, "cid": CID }] })
        );
        // Two: the gate keyed as its post, naming it.
        let out = apply(args(json!({ "text": "hi", "gate": [] })))
            .await
            .unwrap();
        let sent = &s.sent("/xrpc/com.atproto.repo.applyWrites")[1]["writes"];
        assert_eq!(sent[1]["rkey"], sent[0]["rkey"]);
        let post = format!(
            "at://{DID}/app.bsky.feed.post/{}",
            sent[0]["rkey"].as_str().unwrap()
        );
        assert_eq!(sent[1]["value"]["post"], post);
        assert_eq!(out.text, format!("Created 2 records, the first {post}."));
        // No values at all: a template of literals.
        let literal = json!({ "writes": [{ "collection": "app.bsky.feed.post",
            "record": { "text": "fixed", "createdAt": { "$now": true } } }] });
        apply(literal).await.unwrap();
        assert_eq!(
            s.sent("/xrpc/com.atproto.repo.applyWrites")[2]["writes"][0]["value"]["text"],
            "fixed"
        );
        // Refused before anything is sent.
        assert_eq!(
            refused(apply(args(json!("hi"))).await),
            "values must be an object"
        );
        assert_eq!(
            refused(apply(json!({ "writes": [], "values": {} })).await),
            "writes: must be an array of 1 to 8 writes"
        );
        let err = refused(apply(args(json!({ "text": 5 }))).await);
        assert!(
            err.starts_with("The record does not match app.bsky.feed.post: "),
            "{err}"
        );
        unresolved(&s.hud, "test.asked.thing");
        let nowhere =
            json!({ "writes": [{ "collection": "test.asked.thing", "record": { "a": 1 } }] });
        assert_eq!(
            refused(apply(nowhere).await),
            "No lexicon for test.asked.thing: HUD writes only records whose lexicon resolves"
        );
        // A template whose every write's `when` is unmet makes nothing: it
        // is refused, not sent as an empty applyWrites.
        let gated = json!({ "writes": [{ "when": ["gate"], "collection": "app.bsky.feed.threadgate",
            "record": { "post": "at://did:plc:alice/app.bsky.feed.post/1", "createdAt": { "$now": true } } }] });
        assert_eq!(
            refused(apply(gated).await),
            "Nothing to write: no write's when holds"
        );
        assert_eq!(s.sent("/xrpc/com.atproto.repo.applyWrites").len(), 3);
        // The PDS's refusals, all or none written.
        let two = || args(json!({ "text": "hi", "gate": [] }));
        set(Some(json_answer(
            400,
            json!({ "error": "InvalidRequest", "message": "Unknown lexicon type: app.bsky.feed.threadgate" }),
        )));
        assert_eq!(
            refused(apply(two()).await),
            "Your PDS refused to create 2 records: InvalidRequest (400), saying \"Unknown lexicon type: app.bsky.feed.threadgate\""
        );
        set(Some(json_answer(
            403,
            json!({ "error": "ScopeMissingError" }),
        )));
        assert_eq!(
            refused(apply(two()).await),
            "Your sign-in does not allow repo:app.bsky.feed.post?action=create"
        );
        // Answers it cannot use: unreadable, or without each record made in
        // the account's own repository.
        for (answer, why) in [
            (
                (200, "application/json", b"{".to_vec()),
                "Your PDS answered with something unreadable",
            ),
            (
                json_answer(200, json!({})),
                "Your PDS answered without each record's uri and cid",
            ),
            (
                json_answer(
                    200,
                    json!({ "results": [{ "uri": format!("at://{BOB}/app.bsky.feed.post/1"), "cid": CID }] }),
                ),
                "Your PDS answered without each record's uri and cid",
            ),
            (
                json_answer(
                    200,
                    json!({ "results": [{ "uri": "not a uri", "cid": CID }] }),
                ),
                "Your PDS answered without each record's uri and cid",
            ),
        ] {
            set(Some(answer));
            assert_eq!(refused(apply(args(json!({ "text": "hi" }))).await), why);
        }
        // Fewer results than records sent.
        set(Some(json_answer(
            200,
            json!({ "results": [{ "uri": format!("at://{DID}/app.bsky.feed.post/1"), "cid": CID }] }),
        )));
        assert_eq!(
            refused(apply(two()).await),
            "Your PDS answered without each record's uri and cid"
        );
    }

    /// A comment on a Grain gallery, as its page's Compose writes it: a
    /// record of a lexicon a PDS need not know, so sent without
    /// `validate`, which made a PDS refuse it ("Unknown lexicon type:
    /// social.grain.comment"); checked by HUD against its lexicon.
    #[tokio::test]
    async fn writes_a_grain_comment() {
        let s = served(pds(Arc::new(Mutex::new(None)))).await;
        let gallery = format!("at://{BOB}/social.grain.gallery/3mgallery2222");
        let args = json!({ "writes": [{ "collection": "social.grain.comment", "record": {
            "subject": { "$value": "subject" }, "text": { "$value": "text" },
            "facets": { "$facets": "text" }, "createdAt": { "$now": true } } }],
            "values": { "subject": gallery, "text": "so nice!" } });
        let out = s
            .hud
            .apply_writes_tool(Some(&args), Some(&s.account))
            .await
            .unwrap();
        let sent = &s.sent("/xrpc/com.atproto.repo.applyWrites")[0];
        assert_eq!(sent.get("validate"), None);
        let value = &sent["writes"][0]["value"];
        assert_eq!(
            (&value["$type"], &value["subject"], &value["text"]),
            (
                &json!("social.grain.comment"),
                &json!(gallery),
                &json!("so nice!")
            )
        );
        // A text with no facets has none, not an empty list.
        assert_eq!(value.get("facets"), None);
        assert!(
            out.text
                .starts_with(&format!("Created at://{DID}/social.grain.comment/"))
        );
    }

    /// Grain's profile edited: the record stored read first from the
    /// account's own PDS, as it is (its avatar a blob ref), and the update
    /// sent over it, the avatar kept where no new one was picked; created
    /// where there is none yet; the PDS's refusals of either, said.
    #[tokio::test]
    async fn updates_a_record_over_the_one_stored() {
        let read: Arc<Mutex<Answer>> = Arc::new(Mutex::new(json_answer(200, json!({}))));
        let next = Arc::new(Mutex::new(None));
        let s = served({
            let (read, base) = (read.clone(), pds(next.clone()));
            move |a| match a.path.as_str() {
                "/xrpc/com.atproto.repo.getRecord" => read.lock().unwrap().clone(),
                _ => base(a),
            }
        })
        .await;
        let stored = |a: Answer| *read.lock().unwrap() = a;
        let apply =
            async |args: Value| s.hud.apply_writes_tool(Some(&args), Some(&s.account)).await;
        let writes = json!([{ "collection": "social.grain.actor.profile", "update": "self", "record": {
            "displayName": { "$value": "name" },
            "description": { "$value": "description" },
            "avatar": { "$first": [{ "$value": "avatar.0.blob" }, { "$stored": "avatar" }] },
            "createdAt": { "$first": [{ "$stored": "createdAt" }, { "$now": true }] } } }]);
        let args = |values: Value| json!({ "writes": writes, "values": values });
        let blob = |link: &str| json!({ "$type": "blob", "ref": { "$link": link }, "mimeType": "image/jpeg", "size": 9 });
        let old = blob(CID);
        let uri = format!("at://{DID}/social.grain.actor.profile/self");
        stored(json_answer(
            200,
            json!({ "uri": uri, "cid": CID, "value": {
                "$type": "social.grain.actor.profile", "displayName": "Alice", "description": "photos",
                "avatar": old, "createdAt": "2025-01-01T00:00:00.000Z" } }),
        ));
        // A new name, the description cleared, no new picture: an update,
        // the stored avatar's blob ref and createdAt kept.
        let out = apply(args(json!({ "name": "Alice B" }))).await.unwrap();
        assert_eq!(out.text, format!("Wrote {uri}."));
        let read_at = s
            .asked
            .lock()
            .unwrap()
            .iter()
            .find(|a| a.path == "/xrpc/com.atproto.repo.getRecord")
            .map(|a| a.query.clone());
        assert_eq!(
            read_at.as_deref(),
            Some("repo=did%3Aplc%3Aalice&collection=social.grain.actor.profile&rkey=self")
        );
        let sent = s.sent("/xrpc/com.atproto.repo.applyWrites");
        assert_eq!(
            sent[0]["writes"],
            json!([{ "$type": "com.atproto.repo.applyWrites#update", "collection": "social.grain.actor.profile",
                "rkey": "self", "value": { "$type": "social.grain.actor.profile", "displayName": "Alice B",
                    "avatar": old, "createdAt": "2025-01-01T00:00:00.000Z" } }])
        );
        // A new picture, uploaded: it replaces the stored one.
        let new = blob("bafkreie5737gdxlw5i64vzichcalba3z2v5n6icifvx5xytvske7mr3hpm");
        apply(args(
            json!({ "name": "Alice", "avatar": [{ "blob": new, "alt": "" }] }),
        ))
        .await
        .unwrap();
        assert_eq!(
            s.sent("/xrpc/com.atproto.repo.applyWrites")[1]["writes"][0]["value"]["avatar"],
            new
        );
        // None stored yet: created, under its own key, now.
        stored(json_answer(400, json!({ "error": "RecordNotFound" })));
        let out = apply(args(json!({ "description": "hi" }))).await.unwrap();
        assert_eq!(out.text, format!("Created {uri}."));
        let create = &s.sent("/xrpc/com.atproto.repo.applyWrites")[2]["writes"][0];
        assert_eq!(
            (
                &create["$type"],
                &create["rkey"],
                &create["value"]["description"]
            ),
            (
                &json!("com.atproto.repo.applyWrites#create"),
                &json!("self"),
                &json!("hi")
            )
        );
        assert!(
            create["value"]["createdAt"]
                .as_str()
                .unwrap()
                .ends_with('Z')
        );
        assert_eq!(create["value"].get("avatar"), None);
        // The read refused or unusable: nothing written.
        for (answer, why) in [
            (
                json_answer(
                    400,
                    json!({ "error": "InvalidRequest", "message": "secret" }),
                ),
                format!("Your PDS refused to read {uri}: InvalidRequest (400)"),
            ),
            (
                (502, "text/html", b"<h1>Bad gateway</h1>".to_vec()),
                format!("Your PDS refused to read {uri} (502)"),
            ),
            (
                json_answer(200, json!({ "uri": uri, "value": "nothing" })),
                format!("Your PDS answered {uri} without its record"),
            ),
            (
                (200, "application/json", b"{".to_vec()),
                "Your PDS answered with something unreadable".into(),
            ),
        ] {
            stored(answer);
            assert_eq!(refused(apply(args(json!({ "name": "x" }))).await), why);
        }
        assert_eq!(s.sent("/xrpc/com.atproto.repo.applyWrites").len(), 3);
        // The update refused: by its error's name; for want of scope, what to do.
        stored(json_answer(
            200,
            json!({ "uri": uri, "value": { "displayName": "Alice" } }),
        ));
        *next.lock().unwrap() = Some(json_answer(400, json!({ "error": "InvalidSwap" })));
        assert_eq!(
            refused(apply(args(json!({ "name": "x" }))).await),
            "Your PDS refused to write 1 records: InvalidSwap (400)"
        );
        *next.lock().unwrap() = Some(json_answer(403, json!({ "error": "ScopeMissingError" })));
        assert_eq!(
            refused(apply(args(json!({ "name": "x" }))).await),
            "Your sign-in does not allow repo:social.grain.actor.profile?action=update"
        );
    }

    /// A gallery edited, its record key given: the gallery read at that
    /// key, its title and description set, its facets made, its place and
    /// createdAt kept; a given value that is no record key refused before
    /// anything is read.
    #[tokio::test]
    async fn updates_a_record_whose_key_is_given() {
        let s = served({
            let base = pds(Arc::new(Mutex::new(None)));
            move |a| match a.path.as_str() {
                "/xrpc/com.atproto.repo.getRecord" => json_answer(
                    200,
                    json!({ "value": { "$type": "social.grain.gallery", "title": "Old", "description": "was",
                        "createdAt": "2025-01-01T00:00:00.000Z", "location": { "name": "Lisbon", "value": "8a2a1072b59ffff" } } }),
                ),
                _ => base(a),
            }
        })
        .await;
        let writes = json!([{ "collection": "social.grain.gallery", "update": { "$value": "rkey" }, "record": {
            "title": { "$value": "title" }, "description": { "$value": "description" },
            "facets": { "$facets": "description" }, "updatedAt": { "$now": true } } }]);
        let apply = async |values: Value| {
            s.hud
                .apply_writes_tool(
                    Some(&json!({ "writes": writes, "values": values })),
                    Some(&s.account),
                )
                .await
        };
        let text = "for @bob.example.com";
        let out = apply(json!({ "rkey": "3kgallery", "title": "Lisbon", "description": text }))
            .await
            .unwrap();
        let uri = format!("at://{DID}/social.grain.gallery/3kgallery");
        assert_eq!(out.text, format!("Wrote {uri}."));
        let read = s
            .asked
            .lock()
            .unwrap()
            .iter()
            .find(|a| a.path == "/xrpc/com.atproto.repo.getRecord")
            .map(|a| a.query.clone());
        assert_eq!(
            read.as_deref(),
            Some("repo=did%3Aplc%3Aalice&collection=social.grain.gallery&rkey=3kgallery")
        );
        let write = &s.sent("/xrpc/com.atproto.repo.applyWrites")[0]["writes"][0];
        assert_eq!(
            (&write["$type"], &write["rkey"]),
            (
                &json!("com.atproto.repo.applyWrites#update"),
                &json!("3kgallery")
            )
        );
        let value = &write["value"];
        assert_eq!(
            (&value["title"], &value["description"], &value["createdAt"]),
            (
                &json!("Lisbon"),
                &json!(text),
                &json!("2025-01-01T00:00:00.000Z")
            )
        );
        assert_eq!(value["location"]["name"], "Lisbon");
        assert_eq!(
            value["facets"][0]["features"][0]["did"], "did:plc:bob",
            "{value}"
        );
        assert_eq!(
            refused(apply(json!({ "rkey": "a/b", "title": "x" })).await),
            "writes[0].update: the value it names holds no record key"
        );
        let reads = s
            .asked
            .lock()
            .unwrap()
            .iter()
            .filter(|a| a.path == "/xrpc/com.atproto.repo.getRecord")
            .count();
        assert_eq!(reads, 1);
    }

    #[tokio::test]
    async fn deletes_the_accounts_own_record() {
        let next = Arc::new(Mutex::new(None));
        let s = served(pds(next.clone())).await;
        let set = |a: Option<Answer>| *next.lock().unwrap() = a;
        let delete = async |uri: &str| {
            s.hud
                .delete_record_tool(Some(&json!({ "uri": uri })), Some(&s.account))
                .await
        };
        assert_eq!(
            refused(
                s.hud
                    .delete_record_tool(Some(&json!({ "uri": "at://did:plc:alice/a.b.c/1" })), None)
                    .await
            ),
            crate::tools::SIGN_IN
        );
        // By its handle, deleted by its DID.
        let out = delete("at://alice.test/app.bsky.feed.like/3k")
            .await
            .unwrap();
        let uri = format!("at://{DID}/app.bsky.feed.like/3k");
        assert_eq!(out.text, format!("Deleted {uri}."));
        assert_eq!(out.structured, json!({ "uri": uri }));
        assert_eq!(
            s.sent("/xrpc/com.atproto.repo.deleteRecord"),
            [json!({ "repo": DID, "collection": "app.bsky.feed.like", "rkey": "3k" })]
        );
        // Another's, or none: nothing sent.
        let err = refused(delete(&format!("at://{BOB}/app.bsky.feed.like/3k")).await);
        assert!(err.starts_with("Only your own records"), "{err}");
        assert_eq!(refused(delete(" ").await), "uri is required");
        assert_eq!(s.sent("/xrpc/com.atproto.repo.deleteRecord").len(), 1);
        // The PDS's refusals.
        set(Some(json_answer(400, json!({ "error": "RecordNotFound" }))));
        assert_eq!(
            refused(delete(&uri).await),
            format!("Your PDS refused to delete {uri}: RecordNotFound (400)")
        );
        set(Some(json_answer(
            403,
            json!({ "error": "ScopeMissingError" }),
        )));
        assert_eq!(
            refused(delete(&uri).await),
            "Your sign-in does not allow repo:app.bsky.feed.like?action=delete"
        );
    }

    /// Each write through HUD forgets the answers kept for its account, so
    /// what it reads next shows what it wrote; a write that fails too, as
    /// it may have landed.
    #[tokio::test]
    async fn a_write_forgets_its_accounts_answers() {
        let next = Arc::new(Mutex::new(None));
        let answer = pds(next.clone());
        // A service of its own, did:plc:svc, at the fake.
        let s = served(move |a| match a.path.as_str() {
            "/did:plc:svc" => {
                let host = a.headers["host"].to_str().unwrap();
                json_answer(
                    200,
                    json!({ "id": "did:plc:svc", "service": [
                    { "id": "#svc", "type": "X", "serviceEndpoint": format!("http://{host}") }] }),
                )
            }
            _ => answer(a),
        })
        .await;
        let svc = crate::xrpc::Service::parse("did:plc:svc#svc").unwrap();
        let asker = crate::xrpc::As {
            hud: &s.hud,
            caller: crate::xrpc::Caller::Account(&s.account),
        };
        let read = async || {
            use crate::xrpc::Asker;
            asker
                .call(&svc, "com.example.q", vec![], false)
                .await
                .unwrap();
            let asked = s.asked.lock().unwrap();
            asked
                .iter()
                .filter(|a| a.path == "/xrpc/com.example.q")
                .count()
        };
        assert_eq!(read().await, 1);
        assert_eq!(read().await, 1);
        let like = json!({ "collection": "app.bsky.feed.like", "record": {
            "subject": { "uri": format!("at://{BOB}/app.bsky.feed.post/3k2a"), "cid": CID },
            "createdAt": "2026-11-14T12:00:00.000Z" } });
        let hud = &s.hud;
        hud.create_record_tool(Some(&like), Some(&s.account))
            .await
            .unwrap();
        assert_eq!(read().await, 2);
        let writes = json!({ "writes": [{ "as": "post", "collection": "app.bsky.feed.post",
            "record": { "text": { "$value": "text" }, "createdAt": { "$now": true } } }],
            "values": { "text": "hi" } });
        hud.apply_writes_tool(Some(&writes), Some(&s.account))
            .await
            .unwrap();
        assert_eq!(read().await, 3);
        let uri = json!({ "uri": format!("at://{DID}/app.bsky.feed.like/3k") });
        hud.delete_record_tool(Some(&uri), Some(&s.account))
            .await
            .unwrap();
        assert_eq!(read().await, 4);
        *next.lock().unwrap() = Some(json_answer(400, json!({ "error": "RecordNotFound" })));
        let failed = hud.delete_record_tool(Some(&uri), Some(&s.account)).await;
        *next.lock().unwrap() = None;
        assert!(failed.is_err());
        assert_eq!(read().await, 5);
        assert_eq!(read().await, 5);
    }

    /// Each write through HUD of an app's own records has the app's server
    /// read them back at once (its `refresh`), as the account through its
    /// PDS, before the write answers: a create by the URI its PDS
    /// answered, an apply's and a delete's by their keys. A write of no
    /// app's records calls none, and a refresh that fails leaves the write
    /// standing.
    #[tokio::test]
    async fn a_write_refreshes_its_apps_records() {
        const REFRESH: &str = "/xrpc/social.grain.sync.indexRecords";
        let refresh: Arc<Mutex<Answer>> = Arc::new(Mutex::new(json_answer(200, json!({}))));
        let answer = pds(Arc::new(Mutex::new(None)));
        let s = served({
            let refresh = refresh.clone();
            move |a| match a.path.as_str() {
                REFRESH => refresh.lock().unwrap().clone(),
                _ => answer(a),
            }
        })
        .await;
        let (hud, me) = (&s.hud, Some(&s.account));
        let refreshed = |n: usize| {
            let asked = s.asked.lock().unwrap();
            let sent: Vec<&Asked> = asked.iter().filter(|a| a.path == REFRESH).collect();
            assert_eq!(
                sent[n].headers["atproto-proxy"],
                "did:web:grain.social#grain_appview"
            );
            (sent.len(), sent[n].json())
        };
        let named = |uris: &[&str]| json!({ "uris": uris });
        let gallery = format!("at://{BOB}/social.grain.gallery/3mgallery2222");
        let favorite = json!({ "collection": "social.grain.favorite", "rkey": "3kfav",
            "record": { "subject": gallery, "createdAt": "2026-11-20T12:00:00.000Z" } });
        hud.create_record_tool(Some(&favorite), me).await.unwrap();
        let uri = format!("at://{DID}/social.grain.favorite/3kfav");
        assert_eq!(refreshed(0), (1, named(&[&uri])));

        let comment = json!({ "writes": [{ "collection": "social.grain.comment",
            "record": { "subject": { "$value": "subject" }, "text": { "$value": "text" },
                "createdAt": { "$now": true } } }],
            "values": { "subject": gallery, "text": "so nice!" } });
        hud.apply_writes_tool(Some(&comment), me).await.unwrap();
        let (n, sent) = refreshed(1);
        assert_eq!(n, 2);
        let made = sent["uris"][0].as_str().unwrap();
        assert!(
            made.starts_with(&format!("at://{DID}/social.grain.comment/")),
            "{made}"
        );

        let deleted = json!({ "uri": uri });
        hud.delete_record_tool(Some(&deleted), me).await.unwrap();
        assert_eq!(refreshed(2), (3, named(&[&uri])));

        // A Bluesky like is no app's: no refresh.
        let like = json!({ "collection": "app.bsky.feed.like", "record": {
            "subject": { "uri": format!("at://{BOB}/app.bsky.feed.post/3k2a"), "cid": CID },
            "createdAt": "2026-11-20T12:00:00.000Z" } });
        hud.create_record_tool(Some(&like), me).await.unwrap();
        assert_eq!(refreshed(2).0, 3);

        // Grain's server failing to refresh: the write stands.
        *refresh.lock().unwrap() = json_answer(502, json!({ "error": "Unreachable" }));
        let out = hud.create_record_tool(Some(&favorite), me).await.unwrap();
        assert_eq!(out.structured["uri"], uri);
        assert_eq!(refreshed(3).0, 4);
    }

    /// A write of more records than one refresh call names (a gallery of
    /// many photos) has them all read back, in calls of at most
    /// `REFRESH_URIS`, each URI named once.
    #[tokio::test]
    async fn many_records_are_refreshed_in_calls_of_at_most_25() {
        const REFRESH: &str = "/xrpc/social.grain.sync.indexRecords";
        // Only the refreshes are asked: the records were written already.
        let s = served(|_| json_answer(200, json!({}))).await;
        let written: Vec<(String, String)> = (0..31)
            .map(|i| {
                let collection = if i == 0 {
                    "social.grain.gallery"
                } else {
                    "social.grain.photo"
                };
                (
                    collection.to_string(),
                    format!("at://{DID}/{collection}/3k{i:02}"),
                )
            })
            .collect();
        s.hud.refresh_written(&s.account, &written).await;
        let asked = s.asked.lock().unwrap();
        let mut sizes = Vec::new();
        let mut named = Vec::new();
        for a in asked.iter().filter(|a| a.path == REFRESH) {
            let uris = a.json()["uris"].as_array().unwrap().clone();
            sizes.push(uris.len());
            named.extend(uris.into_iter().map(|u| u.as_str().unwrap().to_string()));
        }
        sizes.sort_unstable();
        assert_eq!(sizes, [6, 25]);
        named.sort();
        let mut all: Vec<String> = written.into_iter().map(|(_, uri)| uri).collect();
        all.sort();
        assert_eq!(named, all);
    }

    /// What each write names as written, from its input and answer.
    #[test]
    fn written_uris_of_each_write() {
        let input = json!({ "repo": DID, "collection": "a.b.c", "rkey": "k" });
        let answered = json!({ "uri": format!("at://{DID}/a.b.c/k"), "cid": CID }).to_string();
        let one = vec![("a.b.c".to_string(), format!("at://{DID}/a.b.c/k"))];
        let put = written_uris("com.atproto.repo.putRecord", &input, answered.as_bytes());
        assert_eq!(put, one);
        let deleted = written_uris("com.atproto.repo.deleteRecord", &input, b"{}");
        assert_eq!(deleted, one);
        assert!(written_uris("com.atproto.repo.createRecord", &input, b"not json").is_empty());
        assert!(written_uris("social.grain.graph.muteActor", &input, b"{}").is_empty());
    }

    /// A deletion plan is the deletes of one commit: each a collection and
    /// a record key, `$type` a delete's or left out, at least one and at
    /// most as many as one commit takes.
    #[test]
    fn plans_are_deletes_one_commit_takes() {
        let m = "a.b.getPlan";
        let write = json!({ "collection": "a.b.thing", "rkey": "3k" });
        let typed = json!({ "$type": DELETE, "collection": "a.b.other", "rkey": "self" });
        assert_eq!(
            planned_deletes(m, &json!({ "writes": [write, typed] })),
            Ok(vec![
                ("a.b.thing".into(), "3k".into()),
                ("a.b.other".into(), "self".into())
            ])
        );
        let refused = |v: Value| planned_deletes(m, &v).unwrap_err();
        assert_eq!(
            refused(json!({})),
            "a.b.getPlan answered no writes to delete"
        );
        assert_eq!(
            refused(json!({ "writes": [] })),
            "a.b.getPlan planned nothing to delete"
        );
        let many = vec![write.clone(); MOST_WRITES + 1];
        assert_eq!(
            refused(json!({ "writes": many })),
            "a.b.getPlan planned 201 deletes, more than the 200 one commit takes"
        );
        let other = "a.b.getPlan planned, as its write 1, something other than a record's delete";
        for odd in [
            json!({ "$type": "com.atproto.repo.applyWrites#create", "collection": "a.b.c", "rkey": "k" }),
            json!({ "collection": "not an nsid", "rkey": "k" }),
            json!({ "collection": "a.b.c", "rkey": "a/b" }),
            json!({ "collection": "a.b.c" }),
        ] {
            assert_eq!(refused(json!({ "writes": [write, odd] })), other);
        }
    }

    /// A trusted view's `deletes` through `call_procedure`: the plan asked
    /// of Grain as the account, through its PDS, now; committed in one
    /// applyWrites of the account's own repository, all or none; read back
    /// by Grain's refresh. A plan Grain refuses, one of a collection HUD
    /// does not write, or a commit the PDS refuses deletes nothing.
    #[tokio::test]
    async fn a_plan_is_deleted_in_one_commit() {
        const PLAN: &str = "/xrpc/social.grain.gallery.getDeletionPlan";
        const COMMIT: &str = "/xrpc/com.atproto.repo.applyWrites";
        const REFRESH: &str = "/xrpc/social.grain.sync.indexRecords";
        let grain = "did:web:grain.social#grain_appview";
        let gallery = format!("at://{DID}/social.grain.gallery/3kgal");
        let writes = json!([
            { "collection": "social.grain.gallery.item", "rkey": "3kitem" },
            { "collection": "social.grain.photo", "rkey": "3kphoto" },
            { "collection": "social.grain.gallery", "rkey": "3kgal" },
        ]);
        let plan = Arc::new(Mutex::new(json_answer(200, json!({ "writes": writes }))));
        let commit = Arc::new(Mutex::new(json_answer(200, json!({}))));
        let (p, c) = (plan.clone(), commit.clone());
        let s = served(move |a| match a.path.as_str() {
            PLAN => p.lock().unwrap().clone(),
            COMMIT => c.lock().unwrap().clone(),
            _ => json_answer(200, json!({})),
        })
        .await;
        let doc = json!({ "id": "did:web:grain.social", "service": [
            { "id": "#grain_appview", "type": "AppView", "serviceEndpoint": s.url }] });
        s.hud.docs.put(
            "did:web:grain.social".into(),
            Ok(Arc::new(doc)),
            std::time::Duration::from_secs(600),
        );
        let args = json!({ "method": "social.grain.gallery.getDeletionPlan", "service": grain,
                           "input": { "gallery": gallery } });
        let delete = async || {
            s.hud
                .call_procedure_tool(Some(&args), Some(&s.account))
                .await
        };
        let asked = |path: &str| {
            let asked = s.asked.lock().unwrap();
            asked.iter().filter(|a| a.path == path).count()
        };

        let out = delete().await.unwrap();
        let deleted = [
            format!("at://{DID}/social.grain.gallery.item/3kitem"),
            format!("at://{DID}/social.grain.photo/3kphoto"),
            gallery.clone(),
        ];
        assert_eq!(
            out.text,
            "Deleted 3 records, as social.grain.gallery.getDeletionPlan planned."
        );
        assert_eq!(out.structured, json!({ "output": { "deleted": deleted } }));
        {
            let asked = s.asked.lock().unwrap();
            let planned = asked.iter().find(|a| a.path == PLAN).unwrap();
            assert_eq!(planned.headers["atproto-proxy"], grain);
            assert!(planned.query.starts_with("gallery=at"), "{}", planned.query);
        }
        let sent = s.sent(COMMIT);
        let deletes: Vec<Value> = writes
            .as_array()
            .unwrap()
            .iter()
            .map(|w| json!({ "$type": DELETE, "collection": w["collection"], "rkey": w["rkey"] }))
            .collect();
        assert_eq!(sent, [json!({ "repo": DID, "writes": deletes })]);
        let refreshed: Vec<Value> = s.sent(REFRESH);
        assert_eq!(refreshed, [json!({ "uris": deleted })]);

        // A plan of one, said by its record.
        *plan.lock().unwrap() = json_answer(200, json!({ "writes": [writes[2]] }));
        assert_eq!(delete().await.unwrap().text, format!("Deleted {gallery}."));
        assert_eq!(asked(COMMIT), 2);

        // A plan refused, or of a collection HUD does not write: no commit.
        *plan.lock().unwrap() = json_answer(400, json!({ "error": "NotFound" }));
        let host = s
            .url
            .trim_start_matches("http://")
            .split(':')
            .next()
            .unwrap()
            .to_string();
        assert_eq!(
            refused(delete().await),
            format!("{host} refused social.grain.gallery.getDeletionPlan: NotFound (400)")
        );
        let follow = json!({ "writes": [writes[0], { "collection": "app.bsky.graph.follow", "rkey": "3kf" }] });
        *plan.lock().unwrap() = json_answer(200, follow);
        assert_eq!(
            refused(delete().await),
            "Some records can't be deleted here yet: app.bsky.graph.follow. Signing in doesn't ask to delete them, so nothing more was deleted."
        );
        assert_eq!(asked(COMMIT), 2);

        // A commit refused: none deleted, none refreshed.
        *plan.lock().unwrap() = json_answer(200, json!({ "writes": writes }));
        *commit.lock().unwrap() =
            json_answer(400, json!({ "error": "InvalidRequest", "message": "no" }));
        assert_eq!(
            refused(delete().await),
            "Your PDS refused to delete 3 records at once, so it deleted none of them: InvalidRequest (400), saying \"no\""
        );
        assert_eq!((asked(COMMIT), asked(REFRESH)), (3, 2));
    }

    /// A `then`'s input: each `{"$did": true}` the account's DID, the rest
    /// as it is.
    #[test]
    fn a_thens_did_is_stamped() {
        let input = json!({ "did": { "$did": true }, "also": [{ "$did": true }, 1], "n": "x" });
        assert_eq!(
            stamped(&input, DID),
            json!({ "did": DID, "also": [DID, 1], "n": "x" })
        );
    }

    const PLAN: &str = "social.grain.server.getAccountDeletionPlan";
    const GRAIN: &str = "did:web:grain.social#grain_appview";
    const FORGET: &str = "/xrpc/social.grain.server.deleteAccount";
    const COMMIT: &str = "/xrpc/com.atproto.repo.applyWrites";

    /// A plan of `writes`, each `(collection, rkey)`.
    fn plan_of(writes: &[(&str, &str)]) -> Answer {
        let writes: Vec<Value> = writes
            .iter()
            .map(|(c, k)| json!({ "collection": c, "rkey": k }))
            .collect();
        json_answer(200, json!({ "writes": writes }))
    }

    /// A PDS and Grain's AppView behind it, Grain's DID document cached:
    /// plans answered from `plans` in turn (the last again once they run
    /// out), a commit by `commit`, the AppView's forgetting by `forget`,
    /// all else 200.
    async fn grain(
        plans: Arc<Mutex<Vec<Answer>>>,
        commit: Arc<Mutex<Answer>>,
        forget: Arc<Mutex<Answer>>,
    ) -> Served {
        let s = served(move |a| match a.path.as_str() {
            p if p == format!("/xrpc/{PLAN}") => {
                let mut plans = plans.lock().unwrap();
                match plans.len() {
                    1 => plans[0].clone(),
                    _ => plans.remove(0),
                }
            }
            COMMIT => commit.lock().unwrap().clone(),
            FORGET => forget.lock().unwrap().clone(),
            _ => json_answer(200, json!({})),
        })
        .await;
        let doc = json!({ "id": "did:web:grain.social", "service": [
            { "id": "#grain_appview", "type": "AppView", "serviceEndpoint": s.url }] });
        s.hud.docs.put(
            "did:web:grain.social".into(),
            Ok(Arc::new(doc)),
            std::time::Duration::from_secs(600),
        );
        s
    }

    fn ok() -> Arc<Mutex<Answer>> {
        Arc::new(Mutex::new(json_answer(200, json!({ "results": [] }))))
    }

    /// Grain's Delete account, as its settings view deletes: through
    /// `call_procedure`, a plan at a time, each committed in one
    /// applyWrites and read back by Grain's refresh, until a plan comes
    /// back empty; then Grain's AppView told to forget the account, its
    /// DID stamped.
    #[tokio::test]
    async fn an_accounts_data_is_deleted_a_plan_at_a_time() {
        let plans = Arc::new(Mutex::new(vec![
            plan_of(&[
                ("social.grain.photo.exif", "3ke1"),
                ("social.grain.gallery.item", "3ki1"),
            ]),
            plan_of(&[("social.grain.actor.profile", "self")]),
            plan_of(&[]),
        ]));
        let forget = Arc::new(Mutex::new(json_answer(
            200,
            json!({ "deleted": 0, "done": true }),
        )));
        let s = grain(plans, ok(), forget).await;
        let auth = s.hud.auth().unwrap();
        let token = auth.signed_in_offline_app(DID, Some("alice.test"), &s.url);
        let app = auth.account(&token).unwrap();
        let args = json!({ "method": PLAN, "service": GRAIN });
        let delete = async || {
            let out = s
                .hud
                .call_tool("call_procedure", Some(&args), Some(&app))
                .await
                .unwrap()
                .unwrap();
            (out.structured, out.text)
        };
        let (exif, item) = (
            format!("at://{DID}/social.grain.photo.exif/3ke1"),
            format!("at://{DID}/social.grain.gallery.item/3ki1"),
        );
        assert_eq!(
            delete().await,
            (
                json!({ "output": { "deleted": [exif, item], "done": false } }),
                format!("Deleted 2 records, as {PLAN} planned.")
            )
        );
        let profile = format!("at://{DID}/social.grain.actor.profile/self");
        assert_eq!(
            delete().await,
            (
                json!({ "output": { "deleted": [profile], "done": false } }),
                format!("Deleted {profile}.")
            )
        );
        assert!(s.sent(FORGET).is_empty(), "not before nothing is left");
        assert_eq!(
            delete().await,
            (
                json!({ "output": { "deleted": [], "done": true } }),
                "Nothing is left to delete.".to_string()
            )
        );
        // Each plan asked through the PDS of Grain's AppView, as the account.
        {
            let asked = s.asked.lock().unwrap();
            let planned: Vec<&Asked> = asked
                .iter()
                .filter(|a| a.path == format!("/xrpc/{PLAN}"))
                .collect();
            assert_eq!(planned.len(), 3);
            assert_eq!(planned[0].headers["atproto-proxy"], GRAIN);
        }
        let delete = |c: &str, k: &str| json!({ "$type": DELETE, "collection": c, "rkey": k });
        assert_eq!(
            s.sent(COMMIT),
            [
                json!({ "repo": DID, "writes": [
                    delete("social.grain.photo.exif", "3ke1"),
                    delete("social.grain.gallery.item", "3ki1")] }),
                json!({ "repo": DID, "writes": [delete("social.grain.actor.profile", "self")] }),
            ]
        );
        // Grain's AppView told of the records that went.
        let refreshed = s.sent("/xrpc/social.grain.sync.indexRecords");
        assert_eq!(refreshed[0], json!({ "uris": [exif, item] }));
        assert_eq!(s.sent(FORGET), [json!({ "did": DID })]);
    }

    /// A plan of one go (a gallery's) is done once committed, then calls
    /// its `then`, if any; it must plan something.
    #[tokio::test]
    async fn a_plan_that_does_not_repeat_is_done_at_once() {
        let plans = Arc::new(Mutex::new(vec![plan_of(&[(
            "social.grain.gallery",
            "3kg",
        )])]));
        let s = grain(plans.clone(), ok(), ok()).await;
        let service = crate::xrpc::Service::parse(GRAIN).unwrap();
        let mut once = crate::view::views().plan(PLAN, GRAIN).unwrap().clone();
        once.repeat = false;
        let delete = async |plan: Option<&crate::view::Plan>| {
            s.hud
                .delete_by(&s.account, PLAN, plan, &service, serde_json::Map::new())
                .await
        };
        let gallery = format!("at://{DID}/social.grain.gallery/3kg");
        let out = delete(Some(&once)).await.unwrap();
        assert_eq!(
            (out.structured, out.text),
            (
                json!({ "output": { "deleted": [gallery] } }),
                format!("Deleted {gallery}.")
            )
        );
        assert_eq!(s.sent(FORGET).len(), 1);
        assert!(delete(None).await.is_ok());
        assert_eq!(s.sent(FORGET).len(), 1, "no then, nothing called");
        *plans.lock().unwrap() = vec![plan_of(&[])];
        assert_eq!(
            refused(delete(None).await),
            format!("{PLAN} planned nothing to delete")
        );
        assert_eq!(s.sent(COMMIT).len(), 2);
        // One that repeats with no then: done once its plan comes back empty.
        let auth = s.hud.auth().unwrap();
        let app = auth
            .account(&auth.signed_in_offline_app(DID, None, &s.url))
            .unwrap();
        let repeats = crate::view::Plan {
            repeat: true,
            then: None,
            ..once
        };
        let out = s
            .hud
            .delete_by(&app, PLAN, Some(&repeats), &service, serde_json::Map::new())
            .await
            .unwrap();
        assert_eq!(
            out.structured,
            json!({ "output": { "deleted": [], "done": true } })
        );
        assert_eq!(s.sent(FORGET).len(), 1);
    }

    /// From another MCP client than the operator's own for a plan that
    /// repeats, a delete HUD doesn't ask for, or what the PDS or the
    /// AppView refuses: refused, said why, nothing more sent.
    #[tokio::test]
    async fn deleting_as_planned_is_refused() {
        let plans = Arc::new(Mutex::new(vec![plan_of(&[])]));
        let (commit, forget) = (ok(), ok());
        let s = grain(plans.clone(), commit.clone(), forget.clone()).await;
        let auth = s.hud.auth().unwrap();
        let app = auth
            .account(&auth.signed_in_offline_app(DID, None, &s.url))
            .unwrap();
        let args = json!({ "method": PLAN, "service": GRAIN });
        let call = async |who: &Account| {
            s.hud
                .call_tool("call_procedure", Some(&args), Some(who))
                .await
                .unwrap()
        };
        // Another MCP client of hers: never her whole account's data.
        let other = refused(call(&s.account).await);
        assert!(
            other.starts_with("An account's data is deleted from the app's own"),
            "{other}"
        );
        // The operator's web app, and a workspace page, may.
        let web = auth
            .account(&auth.signed_in_offline_client(DID, None, &s.url, &auth.web_client_id()))
            .unwrap();
        let page = auth
            .web_account(&auth.signed_in_offline_at(DID, None, true, &s.url))
            .unwrap();
        for who in [&web, &page] {
            assert!(call(who).await.is_ok());
        }
        // A plan with records of collections HUD does not ask to delete
        // (an account's group records, which Grain's sign-in leaves out):
        // each named once, nothing of the plan committed, never done.
        *plans.lock().unwrap() = vec![plan_of(&[
            ("social.grain.photo", "3kp"),
            ("social.grain.group.submission", "3ks"),
            ("social.grain.group.item", "3ki"),
            ("social.grain.group.submission", "3kt"),
        ])];
        let committed = s.sent(COMMIT).len();
        assert_eq!(
            refused(call(&app).await),
            "Some records can't be deleted here yet: social.grain.group.item, social.grain.group.submission. Signing in doesn't ask to delete them, so nothing more was deleted."
        );
        assert_eq!(s.sent(COMMIT).len(), committed);
        // The PDS refusing the commit: none deleted.
        *plans.lock().unwrap() = vec![plan_of(&[("social.grain.photo", "3kp")])];
        let set = |m: &Arc<Mutex<Answer>>, a: Answer| *m.lock().unwrap() = a;
        set(
            &commit,
            json_answer(400, json!({ "error": "InvalidRequest", "message": "no" })),
        );
        assert_eq!(
            refused(call(&app).await),
            "Your PDS refused to delete 1 records at once, so it deleted none of them: InvalidRequest (400), saying \"no\""
        );
        // The AppView refusing to forget, by name or not, or for want of scope.
        set(&commit, json_answer(200, json!({ "results": [] })));
        *plans.lock().unwrap() = vec![plan_of(&[])];
        for (answer, want) in [
            (
                json_answer(400, json!({ "error": "RecordsRemain" })),
                "did:web:grain.social refused social.grain.server.deleteAccount: RecordsRemain (400)".to_string(),
            ),
            (
                (502, "", Vec::new()),
                "did:web:grain.social refused social.grain.server.deleteAccount (502)".to_string(),
            ),
            (
                json_answer(403, json!({ "error": "ScopeMissingError" })),
                "Your sign-in does not allow rpc:social.grain.server.deleteAccount?aud=did:web:grain.social%23grain_appview".to_string(),
            ),
        ] {
            set(&forget, answer);
            assert_eq!(refused(call(&app).await), want);
        }
        assert_eq!(s.sent(COMMIT).len(), 1);
    }
}