Skip to main content

hyprstream_core/services/
public_repo.rs

1//! Durable public AT Protocol repository transactions.
2//!
3//! This module is the storage boundary between native authorization and the
4//! public repository codec. It keeps public record bytes and public commit
5//! bytes in a separate RocksDB key namespace, and writes the record, signed
6//! head and publication intent in one batch. It deliberately does not mint
7//! identities, evaluate native policy itself, or expose native signing keys.
8//! A caller must supply the native authorization gate for every request.
9
10use std::collections::BTreeMap;
11use std::path::Path;
12use std::sync::Arc;
13
14use anyhow::{anyhow, ensure, Context as _, Result};
15use base64::{
16    engine::general_purpose::{STANDARD, STANDARD_NO_PAD},
17    Engine as _,
18};
19use hyprstream_pds::atproto_cbor::{AtprotoRecord, AtprotoRecordKey};
20use hyprstream_pds::commit::{Commit, UnsignedCommit};
21use hyprstream_pds::dag_cbor::DagCbor;
22#[cfg(feature = "rocksdb")]
23use hyprstream_pds::mst::Node;
24use hyprstream_pds::repo_authority::accept_repo_authority;
25use hyprstream_pds::tid::Tid;
26use hyprstream_pds::Cid;
27use parking_lot::Mutex;
28use serde::{Deserialize, Serialize};
29
30const RECORD_PREFIX: &str = "public-rk\0";
31const GENESIS_PREFIX: &str = "public-genesis\0";
32const COMMIT_PREFIX: &str = "public-commit\0";
33const COMMIT_BLOCK_PREFIX: &str = "public-commit-block\0";
34const INTENT_PREFIX: &str = "public-intent\0";
35const MAX_REQUEST_ID: usize = 128;
36const MAX_PRINCIPAL: usize = 512;
37/// Deployment ceiling on complete canonical DAG-CBOR record bytes (64 KiB).
38/// A conservative storage policy, not a universal protocol maximum; large
39/// payloads belong in linked blobs. Includes CBOR map/type/length overhead.
40/// See https://atproto.com/specs/repository#security-considerations.
41pub const MAX_PUBLIC_RECORD_BYTES: usize = 64 * 1024;
42
43/// MVP repository capacity: bounds raw records, decoded trees and MST work for
44/// every snapshot caller. Key bytes count too; tiny records also have a limit.
45pub const MAX_PUBLIC_SNAPSHOT_BYTES: usize = 1024 * 1024;
46pub const MAX_PUBLIC_SNAPSHOT_RECORDS: usize = 256;
47
48#[derive(Default)]
49struct SnapshotBudget {
50    bytes: usize,
51    records: usize,
52}
53
54impl SnapshotBudget {
55    fn charge(&mut self, key_bytes: usize, record_bytes: usize) -> Result<()> {
56        ensure!(
57            record_bytes <= MAX_PUBLIC_RECORD_BYTES,
58            "public snapshot record exceeds byte budget"
59        );
60        let bytes = self
61            .bytes
62            .checked_add(key_bytes)
63            .and_then(|bytes| bytes.checked_add(record_bytes));
64        ensure!(
65            self.records < MAX_PUBLIC_SNAPSHOT_RECORDS
66                && bytes.is_some_and(|bytes| bytes <= MAX_PUBLIC_SNAPSHOT_BYTES),
67            "public snapshot exceeds aggregate budget"
68        );
69        self.bytes = bytes.ok_or_else(|| anyhow!("public snapshot byte count overflow"))?;
70        self.records += 1;
71        Ok(())
72    }
73}
74
75/// Blob storage is not wired to this public writer. Never accept caller-supplied
76/// blob claims as proof of account ownership, bytes, MIME type or size. Scan
77/// every object, including open unions/extensions and legacy blob objects.
78pub(crate) fn reject_unverified_blobs(value: &DagCbor) -> Result<()> {
79    match value {
80        DagCbor::Map(entries) => {
81            let field = |name: &str| {
82                entries.iter().find_map(|(key, value)| {
83                    matches!(key, DagCbor::Text(key) if key == name).then_some(value)
84                })
85            };
86            ensure!(
87                !matches!(field("$type"), Some(DagCbor::Text(kind)) if kind == "blob")
88                    && !(field("cid").is_some() && field("mimeType").is_some()),
89                "blob storage verification is unavailable"
90            );
91            for (_, value) in entries {
92                reject_unverified_blobs(value)?;
93            }
94        }
95        DagCbor::List(values) => {
96            for value in values {
97                reject_unverified_blobs(value)?;
98            }
99        }
100        _ => {}
101    }
102    Ok(())
103}
104
105fn record_prefix(did: &str) -> Vec<u8> {
106    format!("{RECORD_PREFIX}{did}\0").into_bytes()
107}
108
109fn record_key(did: &str, collection: &str, rkey: &str) -> Vec<u8> {
110    format!("{RECORD_PREFIX}{did}\0{collection}\0{rkey}").into_bytes()
111}
112
113fn commit_key(did: &str) -> Vec<u8> {
114    format!("{COMMIT_PREFIX}{did}").into_bytes()
115}
116
117fn commit_block_key(did: &str, cid: &str) -> Vec<u8> {
118    format!("{COMMIT_BLOCK_PREFIX}{did}\0{cid}").into_bytes()
119}
120
121fn intent_key(did: &str, request_id: &str) -> Vec<u8> {
122    format!("{INTENT_PREFIX}{did}\0{request_id}").into_bytes()
123}
124
125fn validate_did(did: &str) -> Result<()> {
126    let authority = accept_repo_authority(did)?;
127    ensure!(
128        authority.is_publicly_publishable(),
129        "public repo requires did:web or did:plc authority"
130    );
131    ensure!(!did.contains('\0'), "repo DID contains a NUL byte");
132    Ok(())
133}
134
135fn validate_request_id(request_id: &str) -> Result<()> {
136    ensure!(
137        !request_id.is_empty() && request_id.len() <= MAX_REQUEST_ID,
138        "invalid publication request id"
139    );
140    ensure!(
141        request_id
142            .bytes()
143            .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_' || b == b'.'),
144        "publication request id contains unsupported characters"
145    );
146    Ok(())
147}
148
149fn validate_principal(principal: &str) -> Result<()> {
150    ensure!(
151        !principal.is_empty() && principal.len() <= MAX_PRINCIPAL,
152        "invalid native principal"
153    );
154    ensure!(
155        !principal.contains('\0'),
156        "native principal contains a NUL byte"
157    );
158    Ok(())
159}
160
161/// Native policy gate required by PublicRepoWriter.
162pub trait PublicPublicationAuthorizer: Send + Sync {
163    /// Authorize one direct publication before any public repository state is
164    /// read or written. Implementations should apply native identity,
165    /// assurance, account, collection, tenant, expiry and revocation policy.
166    fn authorize(&self, principal: &str, account: &str, collection: &str) -> Result<()>;
167
168    /// Separate account-owner authority: permission to publish a record never
169    /// implicitly permits key replacement. Existing authorizers fail closed.
170    fn authorize_key_promotion(&self, _principal: &str, _account: &str) -> Result<()> {
171        Err(anyhow!(
172            "repository signing key promotion is not authorized"
173        ))
174    }
175}
176
177/// A request to create one immutable public record under an owned repo.
178#[derive(Clone, Debug)]
179pub struct PublicCreateRequest {
180    pub request_id: String,
181    pub principal: String,
182    pub did: String,
183    pub collection: String,
184    /// Explicit general AT record key, or None to allocate a fresh TID under
185    /// the transaction lock. Retries recover a generated key from the intent.
186    pub rkey: Option<AtprotoRecordKey>,
187    pub value: DagCbor,
188    /// Required repo-head CAS value. None means this must create the genesis
189    /// record; retries use the request id and never silently fork a head.
190    pub expected_prev: Option<Cid>,
191}
192
193/// Durable result of a successful public repository transaction.
194#[derive(Clone, Debug, Eq, PartialEq)]
195pub struct PublicCommitResult {
196    pub uri: String,
197    pub cid: Cid,
198    pub commit_cid: Cid,
199}
200
201#[derive(Clone, Debug)]
202pub struct PublicRepoSnapshot {
203    pub did: String,
204    pub records: BTreeMap<(String, AtprotoRecordKey), AtprotoRecord>,
205    pub commit: Commit,
206}
207
208#[derive(Clone, Debug, Deserialize, Serialize)]
209struct PublicationIntent {
210    request_id: String,
211    principal: String,
212    did: String,
213    collection: String,
214    rkey: String,
215    /// Old intents always had an explicit key. Preserve their retry behavior.
216    #[serde(default)]
217    generated_rkey: bool,
218    cid: String,
219    commit_cid: String,
220    /// Missing on older receipts: condition identity cannot safely be inferred.
221    #[serde(default)]
222    head_condition: Option<String>,
223}
224
225#[derive(Default)]
226struct AccountSigningState {
227    // Owned async admission spans hosted prepare/sign/finish. Cancellation
228    // releases it; the synchronous guard below still protects detached work.
229    external_write: Arc<tokio::sync::Mutex<()>>,
230    // Shared transaction guard for BOTH local and external authority modes,
231    // genesis initialization, snapshots, commit preparation/finalization and
232    // promotion. External signing releases it across the await and checks the
233    // exact prepared head again before commit. Always lock before external_key.
234    active_key: Mutex<Option<p256::ecdsa::SigningKey>>,
235    // External hosted-account authority. It is mutually exclusive with the
236    // local key so local and keyless writers cannot silently share a DID.
237    external_key: Mutex<Option<p256::ecdsa::VerifyingKey>>,
238}
239
240pub enum PublicCreatePreparation {
241    Existing(PublicCommitResult),
242    Pending(Box<PublicPendingCreate>),
243}
244
245pub struct PublicPendingCreate {
246    account: Arc<AccountSigningState>,
247    request: PublicCreateRequest,
248    record: AtprotoRecord,
249    unsigned: UnsignedCommit,
250    active_verifying_key: p256::ecdsa::VerifyingKey,
251    head_condition: PublicHeadCondition,
252    generated_rkey: bool,
253}
254
255impl PublicPendingCreate {
256    pub fn signing_bytes(&self) -> Result<Vec<u8>> {
257        self.unsigned.to_atproto_dag_cbor()
258    }
259}
260
261/// Native callers retain genesis-or-exact CAS; XRPC may omit its condition.
262#[derive(Clone)]
263enum PublicHeadCondition {
264    Unconditional,
265    Exact(Option<Cid>),
266}
267
268impl PublicHeadCondition {
269    fn identity(&self) -> String {
270        match self {
271            Self::Unconditional => "unconditional".to_owned(),
272            Self::Exact(None) => "exact:genesis".to_owned(),
273            Self::Exact(Some(cid)) => format!("exact:{cid}"),
274        }
275    }
276
277    fn matches(&self, actual: Option<Cid>) -> bool {
278        match self {
279            Self::Unconditional => true,
280            Self::Exact(expected) => *expected == actual,
281        }
282    }
283}
284
285/// Durable public repository storage. It uses a distinct RocksDB directory
286/// and key namespace so native signed artifacts are never re-encoded as public
287/// bytes.
288pub struct PublicRepoStore {
289    #[cfg(feature = "rocksdb")]
290    db: Arc<rocksdb::DB>,
291    // Held only to find/insert a shared account state, never during account
292    // locking, cryptography or database access. Unrelated DIDs can progress.
293    accounts: Mutex<BTreeMap<String, Arc<AccountSigningState>>>,
294}
295
296impl std::fmt::Debug for PublicRepoStore {
297    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
298        f.debug_struct("PublicRepoStore").finish_non_exhaustive()
299    }
300}
301
302impl PublicRepoStore {
303    #[cfg(all(test, feature = "rocksdb"))]
304    pub(crate) fn insert_snapshot_budget_fixture_for_test(
305        &self,
306        did: &str,
307        count: usize,
308        payload_bytes: usize,
309    ) -> Result<()> {
310        let value = DagCbor::str_map([
311            ("$type", DagCbor::Text("app.bsky.feed.post".into())),
312            ("text", DagCbor::Text("x".repeat(payload_bytes))),
313        ]);
314        let record = AtprotoRecord::new(
315            "app.bsky.feed.post",
316            AtprotoRecordKey::new("fixture")?,
317            value,
318        )?;
319        let mut batch = rocksdb::WriteBatch::default();
320        for index in 0..count {
321            let key = record_key(did, "app.bsky.feed.post", &format!("{index:04}"));
322            if index + 1 == count {
323                // Same size, invalid CBOR: capacity must fail BEFORE decoding it.
324                batch.put(key, vec![0xff; record.bytes().len()]);
325            } else {
326                batch.put(key, record.bytes());
327            }
328        }
329        self.db.write(batch)?;
330        Ok(())
331    }
332
333    #[cfg(all(test, feature = "rocksdb"))]
334    pub(crate) fn insert_unverified_blob_for_test(&self, did: &str) -> Result<()> {
335        let account = self
336            .accounts
337            .lock()
338            .get(did)
339            .cloned()
340            .ok_or_else(|| anyhow!("test writer account missing"))?;
341        let key = account.active_key.lock();
342        let record = AtprotoRecord::new(
343            "app.bsky.feed.post",
344            AtprotoRecordKey::new("legacy")?,
345            DagCbor::str_map([
346                ("$type", DagCbor::Text("app.bsky.feed.post".into())),
347                (
348                    "extension",
349                    DagCbor::str_map([
350                        ("$type", DagCbor::Text("blob".into())),
351                        ("ref", DagCbor::Link(Cid::from_raw(b"absent-content"))),
352                        ("mimeType", DagCbor::Text("image/png".into())),
353                        ("size", DagCbor::Unsigned(14)),
354                    ]),
355                ),
356            ]),
357        )?;
358        let keyed = BTreeMap::from([("app.bsky.feed.post/legacy".to_owned(), record.cid())]);
359        let (root, _) = Node::from_keyed_records(&keyed).to_node_data_with_blocks_atproto()?;
360        let commit = Commit::sign_atproto(
361            &UnsignedCommit::new(did.to_owned(), root.cid_atproto()?, Tid::from_raw(7), None),
362            key.as_ref()
363                .ok_or_else(|| anyhow!("test signing authority missing"))?,
364        )?;
365        let mut batch = rocksdb::WriteBatch::default();
366        batch.put(
367            record_key(did, record.collection(), record.rkey().as_str()),
368            record.bytes(),
369        );
370        batch.put(commit_key(did), commit.to_atproto_dag_cbor()?);
371        self.db.write(batch)?;
372        Ok(())
373    }
374
375    #[cfg(all(test, feature = "rocksdb"))]
376    pub(crate) fn with_account_lock_for_test(&self, did: &str, held: impl FnOnce()) -> Result<()> {
377        let account = self
378            .accounts
379            .lock()
380            .get(did)
381            .cloned()
382            .ok_or_else(|| anyhow!("test writer account missing"))?;
383        let _guard = account.active_key.lock();
384        held();
385        Ok(())
386    }
387
388    #[cfg(all(test, feature = "rocksdb"))]
389    pub(crate) fn insert_malformed_record_for_test(
390        &self,
391        did: &str,
392        collection: &str,
393        rkey: &str,
394    ) -> Result<()> {
395        self.db
396            .put(record_key(did, collection, rkey), b"invalid CBOR")?;
397        Ok(())
398    }
399
400    #[cfg(feature = "rocksdb")]
401    pub fn open(path: &Path) -> Result<Self> {
402        std::fs::create_dir_all(path)
403            .with_context(|| format!("failed to create public repo store at {path:?}"))?;
404        let mut opts = rocksdb::Options::default();
405        opts.create_if_missing(true);
406        let db = rocksdb::DB::open(&opts, path)
407            .with_context(|| format!("failed to open public repo store at {path:?}"))?;
408        Ok(Self {
409            db: Arc::new(db),
410            accounts: Mutex::new(BTreeMap::new()),
411        })
412    }
413
414    /// Fail-closed twin: no durable public repository store without rocksdb.
415    #[cfg(not(feature = "rocksdb"))]
416    pub fn open(_path: &Path) -> Result<Self> {
417        anyhow::bail!("public repository store requires the `rocksdb` feature")
418    }
419
420    #[cfg(feature = "rocksdb")]
421    pub fn snapshot(&self, did: &str) -> Result<Option<PublicRepoSnapshot>> {
422        validate_did(did)?;
423        // Iteration and the head lookup must observe the same database
424        // sequence, even when a writer commits a batch during the scan.
425        let snapshot = self.db.snapshot();
426        let prefix = record_prefix(did);
427        let mut records = BTreeMap::new();
428        let mut budget = SnapshotBudget::default();
429        // Borrow RocksDB's current key/value so oversized values are rejected
430        // before the iterator or decoder copies them into Rust-owned buffers.
431        let mut iterator = snapshot.raw_iterator();
432        iterator.seek(&prefix);
433        while iterator.valid() {
434            let key = iterator
435                .key()
436                .ok_or_else(|| anyhow!("public record key unavailable"))?;
437            if !key.starts_with(&prefix) {
438                break;
439            }
440            let bytes = iterator
441                .value()
442                .ok_or_else(|| anyhow!("public record bytes unavailable"))?;
443            budget.charge(key.len(), bytes.len())?;
444            let suffix = std::str::from_utf8(&key[prefix.len()..])
445                .context("public repo record key is not UTF-8")?;
446            let (collection, rkey) = suffix
447                .split_once('\0')
448                .ok_or_else(|| anyhow!("public repo record key missing separator"))?;
449            let rkey = AtprotoRecordKey::new(rkey)?;
450            let record =
451                AtprotoRecord::from_bytes(collection, &rkey, bytes).with_context(|| {
452                    format!("invalid public record {did}/{collection}/{}", rkey.as_str())
453                })?;
454            reject_unverified_blobs(record.value())?;
455            records.insert((collection.to_owned(), rkey), record);
456            iterator.next();
457        }
458        iterator
459            .status()
460            .context("public repo record scan failed")?;
461        let bytes = snapshot
462            .get_pinned(commit_key(did))
463            .context("public repo commit read failed")?;
464        let Some(bytes) = bytes else {
465            ensure!(
466                records.is_empty(),
467                "public repo has records but no signed commit"
468            );
469            return Ok(None);
470        };
471        ensure!(
472            bytes.len() <= MAX_PUBLIC_RECORD_BYTES,
473            "public commit exceeds byte budget"
474        );
475        let commit = Commit::from_atproto_dag_cbor(&bytes)
476            .context("public repo signed commit is invalid")?;
477        ensure!(commit.did == did, "public repo commit DID mismatch");
478        let keyed = records
479            .iter()
480            .map(|((collection, rkey), record)| {
481                (format!("{collection}/{}", rkey.as_str()), record.cid())
482            })
483            .collect();
484        let tree = Node::from_keyed_records(&keyed);
485        let (root_data, _) = tree.to_node_data_with_blocks_atproto()?;
486        ensure!(
487            commit.data == root_data.cid_atproto()?,
488            "public repo commit root does not cover stored records"
489        );
490        Ok(Some(PublicRepoSnapshot {
491            did: did.to_owned(),
492            records,
493            commit,
494        }))
495    }
496
497    #[cfg(not(feature = "rocksdb"))]
498    pub fn snapshot(&self, _did: &str) -> Result<Option<PublicRepoSnapshot>> {
499        anyhow::bail!("public repository store requires the `rocksdb` feature")
500    }
501
502    #[cfg(feature = "rocksdb")]
503    fn public_genesis_bytes(&self, did: &str) -> Result<Option<Vec<u8>>> {
504        let bytes = self.db.get_pinned(format!("{GENESIS_PREFIX}{did}"))?;
505        if let Some(bytes) = &bytes {
506            ensure!(
507                bytes.len() <= MAX_PUBLIC_RECORD_BYTES,
508                "public genesis exceeds byte budget"
509            );
510        }
511        Ok(bytes.map(|bytes| bytes.to_vec()))
512    }
513
514    #[cfg(not(feature = "rocksdb"))]
515    fn public_genesis_bytes(&self, _did: &str) -> Result<Option<Vec<u8>>> {
516        anyhow::bail!("public repository store requires the `rocksdb` feature")
517    }
518
519    #[cfg(feature = "rocksdb")]
520    pub fn seed_public_genesis(
521        &self,
522        did: &str,
523        commit: Commit,
524        verifying_key: &p256::ecdsa::VerifyingKey,
525    ) -> Result<()> {
526        let account = {
527            let mut accounts = self.accounts.lock();
528            Arc::clone(accounts.entry(did.to_owned()).or_default())
529        };
530        let transaction = account.active_key.lock();
531        if let Some(local) = transaction.as_ref() {
532            ensure!(
533                local.verifying_key() == verifying_key,
534                "genesis differs from local authority"
535            );
536        }
537        if let Some(external) = account.external_key.lock().as_ref() {
538            ensure!(
539                external == verifying_key,
540                "genesis differs from external authority"
541            );
542        }
543        validate_did(did)?;
544        ensure!(
545            commit.did == did && commit.prev.is_none(),
546            "public genesis identity is invalid"
547        );
548        commit.verify_atproto(verifying_key)?;
549        let empty = Node::empty();
550        let (root, _) = empty.to_node_data_with_blocks_atproto()?;
551        ensure!(
552            commit.data == root.cid_atproto()?,
553            "public genesis root is not empty"
554        );
555        let bytes = commit.to_atproto_dag_cbor()?;
556        let cid = commit.cid_atproto()?;
557        let cached = self.public_genesis_bytes(did)?;
558        if let Some(cached) = &cached {
559            ensure!(
560                cached == &bytes,
561                "public genesis cache differs from immutable archive"
562            );
563        }
564        if let Some(existing) = self.snapshot(did)? {
565            existing.commit.verify_atproto(verifying_key)?;
566            let archived = self.db.get(commit_block_key(did, &cid.to_string()))?;
567            ensure!(
568                archived.as_deref() == Some(bytes.as_slice()),
569                "public repository has no matching immutable genesis"
570            );
571            if cached.is_none() {
572                // Upgrade stores seeded before the explicit genesis locator existed.
573                let mut options = rocksdb::WriteOptions::default();
574                options.set_sync(true);
575                self.db
576                    .put_opt(format!("{GENESIS_PREFIX}{did}"), &bytes, &options)?;
577            }
578            return Ok(());
579        }
580        ensure!(
581            self.db.get(commit_key(did))?.is_none(),
582            "public repository has an unreadable head"
583        );
584        let mut batch = rocksdb::WriteBatch::default();
585        batch.put(commit_block_key(did, &cid.to_string()), &bytes);
586        batch.put(format!("{GENESIS_PREFIX}{did}"), &bytes);
587        batch.put(commit_key(did), bytes);
588        let mut options = rocksdb::WriteOptions::default();
589        options.set_sync(true);
590        self.db
591            .write_opt(batch, &options)
592            .context("public repo genesis transaction failed")?;
593        Ok(())
594    }
595
596    #[cfg(not(feature = "rocksdb"))]
597    pub fn seed_public_genesis(
598        &self,
599        _did: &str,
600        _commit: Commit,
601        _verifying_key: &p256::ecdsa::VerifyingKey,
602    ) -> Result<()> {
603        anyhow::bail!("public repository store requires the `rocksdb` feature")
604    }
605
606    #[cfg(feature = "rocksdb")]
607    fn intent(&self, did: &str, request_id: &str) -> Result<Option<PublicationIntent>> {
608        let Some(bytes) = self.db.get(intent_key(did, request_id))? else {
609            return Ok(None);
610        };
611        Ok(Some(
612            serde_json::from_slice(&bytes).context("public publication intent is invalid")?,
613        ))
614    }
615
616    #[cfg(not(feature = "rocksdb"))]
617    fn intent(&self, _did: &str, _request_id: &str) -> Result<Option<PublicationIntent>> {
618        anyhow::bail!("public repository store requires the `rocksdb` feature")
619    }
620
621    #[cfg(feature = "rocksdb")]
622    fn write_transaction(
623        &self,
624        did: &str,
625        record: &AtprotoRecord,
626        commit: &Commit,
627        intent: &PublicationIntent,
628    ) -> Result<()> {
629        let commit_bytes = commit.to_atproto_dag_cbor()?;
630        let intent_bytes = serde_json::to_vec(intent).context("encode publication intent")?;
631        let mut batch = rocksdb::WriteBatch::default();
632        batch.put(
633            record_key(did, record.collection(), record.rkey().as_str()),
634            record.bytes(),
635        );
636        // The mutable head may advance before a caller retries. Retain the
637        // exact commit block that backs each durable publication receipt.
638        batch.put(
639            commit_block_key(did, &commit.cid_atproto()?.to_string()),
640            &commit_bytes,
641        );
642        batch.put(commit_key(did), commit_bytes);
643        batch.put(intent_key(did, &intent.request_id), intent_bytes);
644        let mut options = rocksdb::WriteOptions::default();
645        options.set_sync(true);
646        self.db
647            .write_opt(batch, &options)
648            .context("public repo transaction failed")?;
649        Ok(())
650    }
651
652    #[cfg(not(feature = "rocksdb"))]
653    fn write_transaction(
654        &self,
655        _did: &str,
656        _record: &AtprotoRecord,
657        _commit: &Commit,
658        _intent: &PublicationIntent,
659    ) -> Result<()> {
660        anyhow::bail!("public repository store requires the `rocksdb` feature")
661    }
662}
663
664/// Write failures are classified at their origin. HTTP callers must use these
665/// variants and fixed public messages; sources are for local diagnostics only.
666#[derive(Debug, thiserror::Error)]
667pub enum PublicRepoWriteError {
668    #[error("invalid publication request: {0}")]
669    InvalidRequest(#[source] anyhow::Error),
670    #[error("publication authorization failed: {0}")]
671    Authorization(#[source] anyhow::Error),
672    #[error("record key already exists")]
673    RecordAlreadyExists,
674    #[error("public repo head CAS conflict")]
675    InvalidSwap,
676    #[error("internal public repository failure: {0}")]
677    Internal(#[from] anyhow::Error),
678}
679
680/// Public repository writer with an explicit native authorization gate.
681pub struct PublicRepoWriter {
682    store: Arc<PublicRepoStore>,
683    did: String,
684    account: Arc<AccountSigningState>,
685    authorizer: Arc<dyn PublicPublicationAuthorizer>,
686    external: bool,
687}
688
689impl std::fmt::Debug for PublicRepoWriter {
690    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
691        f.debug_struct("PublicRepoWriter")
692            .field("did", &self.did)
693            .finish_non_exhaustive()
694    }
695}
696
697impl PublicRepoWriter {
698    pub fn did(&self) -> &str {
699        &self.did
700    }
701
702    /// Blocking public read boundary. The account guard keeps the snapshot and
703    /// published verifying key consistent with concurrent writes/key promotion.
704    pub(crate) fn public_snapshot(
705        &self,
706    ) -> Result<Option<(PublicRepoSnapshot, p256::ecdsa::VerifyingKey)>> {
707        let state = self.account.active_key.lock();
708        let key = if self.external {
709            self.account.external_key.lock().as_ref().copied()
710        } else {
711            state.as_ref().map(|key| *key.verifying_key())
712        }
713        .ok_or_else(|| anyhow!("public signing authority is unavailable"))?;
714        let Some(snapshot) = self.store.snapshot(&self.did)? else {
715            return Ok(None);
716        };
717        snapshot.commit.verify_atproto(&key)?;
718        Ok(Some((snapshot, key)))
719    }
720
721    /// Bind a trusted account key to this store. All writer handles for the
722    /// same DID share one active authority; a different key requires promotion.
723    pub fn new(
724        store: Arc<PublicRepoStore>,
725        did: impl Into<String>,
726        signing_key: p256::ecdsa::SigningKey,
727        authorizer: Arc<dyn PublicPublicationAuthorizer>,
728    ) -> Result<Self> {
729        let did = did.into();
730        validate_did(&did)?;
731        let account = {
732            let mut accounts = store.accounts.lock();
733            Arc::clone(accounts.entry(did.clone()).or_default())
734        };
735        {
736            let mut state = account.active_key.lock();
737            ensure!(
738                account.external_key.lock().is_none(),
739                "repo already has an external signing authority"
740            );
741            if let Some(active) = state.as_ref() {
742                ensure!(
743                    active.verifying_key() == signing_key.verifying_key(),
744                    "repo signing key differs from active authority; use explicit promotion"
745                );
746            } else {
747                // On reopen, never silently adopt a key that disagrees with
748                // the durable head. Recover/promote through the account owner.
749                if let Some(repo) = store.snapshot(&did)? {
750                    repo.commit
751                        .verify_atproto(signing_key.verifying_key())
752                        .context("repo head does not match supplied active signing authority")?;
753                }
754                *state = Some(signing_key);
755            }
756        }
757        Ok(Self {
758            store,
759            did,
760            account,
761            authorizer,
762            external: false,
763        })
764    }
765
766    /// Bind a hosted account's public authority without importing its private
767    /// signing key into this repository boundary.
768    pub fn new_external(
769        store: Arc<PublicRepoStore>,
770        did: impl Into<String>,
771        verifying_key: p256::ecdsa::VerifyingKey,
772        authorizer: Arc<dyn PublicPublicationAuthorizer>,
773    ) -> Result<Self> {
774        let did = did.into();
775        validate_did(&did)?;
776        let account = {
777            let mut accounts = store.accounts.lock();
778            Arc::clone(accounts.entry(did.clone()).or_default())
779        };
780        {
781            let local = account.active_key.lock();
782            ensure!(
783                local.is_none(),
784                "repo already has a local signing authority"
785            );
786            let mut external = account.external_key.lock();
787            if let Some(active) = external.as_ref() {
788                ensure!(
789                    *active == verifying_key,
790                    "repo verifying key differs from active authority"
791                );
792            } else if let Some(repo) = store.snapshot(&did)? {
793                repo.commit
794                    .verify_atproto(&verifying_key)
795                    .context("repo head does not match supplied active signing authority")?;
796            }
797            *external = Some(verifying_key);
798        }
799        Ok(Self {
800            store,
801            did,
802            account,
803            authorizer,
804            external: true,
805        })
806    }
807
808    /// Resolve this account's active public key from shared transaction state.
809    /// DID-document publication must use this authority after promotion returns.
810    pub fn active_verifying_key(&self) -> Result<p256::ecdsa::VerifyingKey> {
811        if self.external {
812            self.account
813                .external_key
814                .lock()
815                .as_ref()
816                .copied()
817                .ok_or_else(|| anyhow!("no active signing authority for repository"))
818        } else {
819            self.account
820                .active_key
821                .lock()
822                .as_ref()
823                .map(|key| *key.verifying_key())
824                .ok_or_else(|| anyhow!("no active signing authority for repository"))
825        }
826    }
827
828    pub fn prepare_external_record_with_expected_prev_text(
829        &self,
830        request: PublicCreateRequest,
831        expected_prev: Option<&str>,
832    ) -> Result<PublicCreatePreparation, PublicRepoWriteError> {
833        if !self.external {
834            return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
835                "external preparation requires an external writer"
836            )));
837        }
838        let condition = match expected_prev {
839            None => PublicHeadCondition::Unconditional,
840            Some(value) => PublicHeadCondition::Exact(Some(
841                parse_atproto_json_cid(value).map_err(PublicRepoWriteError::InvalidRequest)?,
842            )),
843        };
844        let _transaction = self.account.active_key.lock();
845        self.prepare_external_record_locked(request, condition, None)
846    }
847
848    #[cfg(feature = "rocksdb")]
849    fn prepare_external_record_locked(
850        &self,
851        request: PublicCreateRequest,
852        condition: PublicHeadCondition,
853        proposed_rkey: Option<AtprotoRecordKey>,
854    ) -> Result<PublicCreatePreparation, PublicRepoWriteError> {
855        if request.did != self.did {
856            return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
857                "public request account does not match writer"
858            )));
859        }
860        validate_request_id(&request.request_id).map_err(PublicRepoWriteError::InvalidRequest)?;
861        validate_principal(&request.principal).map_err(PublicRepoWriteError::InvalidRequest)?;
862        self.authorizer
863            .authorize(&request.principal, &self.did, &request.collection)
864            .map_err(PublicRepoWriteError::Authorization)?;
865        reject_unverified_blobs(&request.value).map_err(PublicRepoWriteError::InvalidRequest)?;
866        let verifying_key = self
867            .active_verifying_key()
868            .map_err(PublicRepoWriteError::Internal)?;
869        let state = self.account.external_key.lock();
870        if state.as_ref() != Some(&verifying_key) {
871            return Err(PublicRepoWriteError::Internal(anyhow!(
872                "external signing authority changed"
873            )));
874        }
875        if let Some(intent) = self
876            .store
877            .intent(&self.did, &request.request_id)
878            .map_err(PublicRepoWriteError::Internal)?
879        {
880            if intent.request_id != request.request_id
881                || intent.principal != request.principal
882                || intent.generated_rkey != request.rkey.is_none()
883            {
884                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
885                    "publication request identity or key mode changed"
886                )));
887            }
888            let rkey = request
889                .rkey
890                .clone()
891                .or_else(|| AtprotoRecordKey::new(intent.rkey.clone()).ok());
892            let Some(rkey) = rkey else {
893                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
894                    "stored record key is invalid"
895                )));
896            };
897            let record =
898                AtprotoRecord::new(request.collection.clone(), &rkey, request.value.clone())
899                    .map_err(PublicRepoWriteError::InvalidRequest)?;
900            if intent.head_condition.as_deref() != Some(&condition.identity()) {
901                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
902                    "publication request condition changed"
903                )));
904            }
905            if intent.cid != record.cid().to_string()
906                || intent.did != self.did
907                || intent.collection != record.collection()
908                || intent.rkey != record.rkey().as_str()
909            {
910                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
911                    "publication request id was reused with different content"
912                )));
913            }
914            let snapshot = self
915                .store
916                .snapshot(&self.did)
917                .map_err(PublicRepoWriteError::Internal)?
918                .ok_or_else(|| anyhow!("publication intent exists without a repository"))?;
919            if !snapshot
920                .records
921                .get(&(record.collection().to_owned(), record.rkey().clone()))
922                .is_some_and(|stored| stored.bytes() == record.bytes())
923            {
924                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
925                    "publication intent record does not match repository"
926                )));
927            }
928            let bytes = self
929                .store
930                .db
931                .get(commit_block_key(&self.did, &intent.commit_cid))
932                .map_err(|error| PublicRepoWriteError::Internal(error.into()))?
933                .ok_or_else(|| anyhow!("publication intent commit block is missing"))?;
934            let commit =
935                Commit::from_atproto_dag_cbor(&bytes).map_err(PublicRepoWriteError::Internal)?;
936            let commit_cid = commit
937                .cid_atproto()
938                .map_err(PublicRepoWriteError::Internal)?;
939            if commit.did != self.did || intent.commit_cid != commit_cid.to_string() {
940                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
941                    "publication intent commit does not match stored block"
942                )));
943            }
944            return Ok(PublicCreatePreparation::Existing(PublicCommitResult {
945                uri: record.uri(&self.did),
946                cid: record.cid(),
947                commit_cid,
948            }));
949        }
950        let existing = self
951            .store
952            .snapshot(&self.did)
953            .map_err(PublicRepoWriteError::Internal)?;
954        let mut budget = SnapshotBudget::default();
955        let (mut keyed, previous, previous_rev) = match existing {
956            Some(snapshot) => {
957                let previous = snapshot
958                    .commit
959                    .cid_atproto()
960                    .map_err(PublicRepoWriteError::Internal)?;
961                if !condition.matches(Some(previous)) {
962                    return Err(PublicRepoWriteError::InvalidSwap);
963                }
964                let previous_rev = snapshot.commit.rev;
965                for ((collection, rkey), record) in &snapshot.records {
966                    budget
967                        .charge(
968                            record_key(&self.did, collection, rkey.as_str()).len(),
969                            record.bytes().len(),
970                        )
971                        .map_err(PublicRepoWriteError::InvalidRequest)?;
972                }
973                let keyed = snapshot
974                    .records
975                    .into_iter()
976                    .map(|((collection, rkey), record)| {
977                        (format!("{collection}/{}", rkey.as_str()), record.cid())
978                    })
979                    .collect();
980                (keyed, Some(previous), Some(previous_rev))
981            }
982            None => {
983                if !condition.matches(None) {
984                    return Err(PublicRepoWriteError::InvalidSwap);
985                }
986                (BTreeMap::new(), None, None)
987            }
988        };
989        let generated_rkey = request.rkey.is_none();
990        let rkey = match request.rkey.clone().or(proposed_rkey) {
991            Some(key) => key,
992            None => allocate_record_key(&request.collection, &keyed, previous_rev)
993                .map_err(PublicRepoWriteError::InvalidRequest)?,
994        };
995        let record = AtprotoRecord::new(request.collection.clone(), &rkey, request.value.clone())
996            .map_err(PublicRepoWriteError::InvalidRequest)?;
997        if record.bytes().len() > MAX_PUBLIC_RECORD_BYTES {
998            return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
999                "public record exceeds {MAX_PUBLIC_RECORD_BYTES}-byte canonical encoded limit"
1000            )));
1001        }
1002        let key = format!("{}/{}", record.collection(), record.rkey().as_str());
1003        if keyed.contains_key(&key) {
1004            return Err(PublicRepoWriteError::RecordAlreadyExists);
1005        }
1006        budget
1007            .charge(
1008                record_key(&self.did, record.collection(), record.rkey().as_str()).len(),
1009                record.bytes().len(),
1010            )
1011            .map_err(PublicRepoWriteError::InvalidRequest)?;
1012        keyed.insert(key, record.cid());
1013        let tree = Node::from_keyed_records(&keyed);
1014        let (root, _) = tree
1015            .to_node_data_with_blocks_atproto()
1016            .map_err(PublicRepoWriteError::Internal)?;
1017        let unsigned = UnsignedCommit::new(
1018            self.did.clone(),
1019            root.cid_atproto().map_err(PublicRepoWriteError::Internal)?,
1020            next_revision(previous_rev),
1021            previous,
1022        );
1023        Ok(PublicCreatePreparation::Pending(Box::new(
1024            PublicPendingCreate {
1025                account: Arc::clone(&self.account),
1026                request,
1027                record,
1028                unsigned,
1029                active_verifying_key: verifying_key,
1030                head_condition: condition,
1031                generated_rkey,
1032            },
1033        )))
1034    }
1035
1036    #[cfg(not(feature = "rocksdb"))]
1037    fn prepare_external_record_locked(
1038        &self,
1039        _request: PublicCreateRequest,
1040        _condition: PublicHeadCondition,
1041        _proposed_rkey: Option<AtprotoRecordKey>,
1042    ) -> Result<PublicCreatePreparation, PublicRepoWriteError> {
1043        Err(PublicRepoWriteError::Internal(anyhow::anyhow!(
1044            "public repository store requires the `rocksdb` feature",
1045        )))
1046    }
1047
1048    pub fn finish_external_record(
1049        &self,
1050        preparation: PublicPendingCreate,
1051        signature: Vec<u8>,
1052    ) -> Result<PublicCommitResult, PublicRepoWriteError> {
1053        if !self.external {
1054            return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
1055                "external finish requires an external writer"
1056            )));
1057        }
1058        if !Arc::ptr_eq(&self.account, &preparation.account)
1059            || preparation.request.did != self.did
1060            || preparation.unsigned.did != self.did
1061        {
1062            return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
1063                "prepared publication belongs to another writer"
1064            )));
1065        }
1066        let _transaction = self.account.active_key.lock();
1067        // Re-run authorization, strict receipt matching, the prospective budget
1068        // and head selection after the external signer returned. Identical
1069        // concurrent retries recover the original receipt before CAS checks.
1070        let current = match self.prepare_external_record_locked(
1071            preparation.request.clone(),
1072            preparation.head_condition.clone(),
1073            Some(preparation.record.rkey().clone()),
1074        )? {
1075            PublicCreatePreparation::Existing(result) => return Ok(result),
1076            PublicCreatePreparation::Pending(current) => current,
1077        };
1078        let verifying_key = current.active_verifying_key;
1079        if verifying_key != preparation.active_verifying_key
1080            || current.unsigned.did != preparation.unsigned.did
1081            || current.unsigned.prev != preparation.unsigned.prev
1082            || current.unsigned.data != preparation.unsigned.data
1083        {
1084            return Err(PublicRepoWriteError::InvalidSwap);
1085        }
1086        let commit =
1087            Commit::from_atproto_signature(&preparation.unsigned, signature, &verifying_key)
1088                .map_err(PublicRepoWriteError::Internal)?;
1089        let commit_cid = commit
1090            .cid_atproto()
1091            .map_err(PublicRepoWriteError::Internal)?;
1092        let intent = PublicationIntent {
1093            request_id: preparation.request.request_id.clone(),
1094            principal: preparation.request.principal.clone(),
1095            did: self.did.clone(),
1096            collection: preparation.record.collection().to_owned(),
1097            rkey: preparation.record.rkey().as_str().to_owned(),
1098            generated_rkey: preparation.generated_rkey,
1099            cid: preparation.record.cid().to_string(),
1100            commit_cid: commit_cid.to_string(),
1101            head_condition: Some(preparation.head_condition.identity()),
1102        };
1103        self.store
1104            .write_transaction(&self.did, &preparation.record, &commit, &intent)
1105            .map_err(PublicRepoWriteError::Internal)?;
1106        Ok(PublicCommitResult {
1107            uri: preparation.record.uri(&self.did),
1108            cid: preparation.record.cid(),
1109            commit_cid,
1110        })
1111    }
1112
1113    /// Explicit account-owner promotion boundary. The trusted caller must
1114    /// validate/secure the candidate and publish it in the account DID document
1115    /// only after this succeeds. This is not the node-global OAuth key store.
1116    ///
1117    /// Serializes with all writers for this store/account. Re-signs an idle
1118    /// head over identical canonical unsigned bytes, persists the new head and
1119    /// immutable block synchronously, then exposes the candidate to surviving
1120    /// writers and public-key readers. Errors leave the active key unchanged.
1121    /// Old blocks and publication intents remain immutable for original retries.
1122    /// This in-process boundary does not subscribe to external key rotations;
1123    /// account lifecycle wiring must route promotion through it.
1124    #[cfg(feature = "rocksdb")]
1125    pub fn promote_signing_key(
1126        &self,
1127        principal: &str,
1128        expected_active: &p256::ecdsa::VerifyingKey,
1129        candidate: p256::ecdsa::SigningKey,
1130    ) -> Result<Option<Cid>> {
1131        validate_principal(principal)?;
1132        self.authorizer
1133            .authorize_key_promotion(principal, &self.did)?;
1134        let mut state = self.account.active_key.lock();
1135        let active = state
1136            .as_mut()
1137            .ok_or_else(|| anyhow!("no active signing authority for repository"))?;
1138        ensure!(
1139            active.verifying_key() == expected_active,
1140            "repo signing authority CAS conflict"
1141        );
1142        let head = if let Some(repo) = self.store.snapshot(&self.did)? {
1143            repo.commit
1144                .verify_atproto(active.verifying_key())
1145                .context("current repo head does not verify under active authority")?;
1146            let resigned = Commit::sign_atproto(&repo.commit.unsigned(), &candidate)?;
1147            let bytes = resigned.to_atproto_dag_cbor()?;
1148            let cid = resigned.cid_atproto()?;
1149            let mut batch = rocksdb::WriteBatch::default();
1150            batch.put(commit_block_key(&self.did, &cid.to_string()), &bytes);
1151            batch.put(commit_key(&self.did), bytes);
1152            let mut options = rocksdb::WriteOptions::default();
1153            options.set_sync(true);
1154            self.store
1155                .db
1156                .write_opt(batch, &options)
1157                .context("public repo signing authority reconciliation failed")?;
1158            Some(cid)
1159        } else {
1160            None
1161        };
1162        *active = candidate;
1163        Ok(head)
1164    }
1165
1166    #[cfg(not(feature = "rocksdb"))]
1167    pub fn promote_signing_key(
1168        &self,
1169        _principal: &str,
1170        _expected_active: &p256::ecdsa::VerifyingKey,
1171        _candidate: p256::ecdsa::SigningKey,
1172    ) -> Result<Option<Cid>> {
1173        anyhow::bail!("public repository store requires the `rocksdb` feature")
1174    }
1175
1176    pub fn create_record(
1177        &self,
1178        request: PublicCreateRequest,
1179    ) -> Result<PublicCommitResult, PublicRepoWriteError> {
1180        let condition = PublicHeadCondition::Exact(request.expected_prev);
1181        self.create_record_with_condition(request, condition)
1182    }
1183
1184    #[cfg(feature = "rocksdb")]
1185    fn create_record_with_condition(
1186        &self,
1187        request: PublicCreateRequest,
1188        condition: PublicHeadCondition,
1189    ) -> Result<PublicCommitResult, PublicRepoWriteError> {
1190        if request.did != self.did {
1191            return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
1192                "public request account does not match writer"
1193            )));
1194        }
1195        validate_request_id(&request.request_id).map_err(PublicRepoWriteError::InvalidRequest)?;
1196        validate_principal(&request.principal).map_err(PublicRepoWriteError::InvalidRequest)?;
1197        self.authorizer
1198            .authorize(&request.principal, &self.did, &request.collection)
1199            .map_err(PublicRepoWriteError::Authorization)?;
1200
1201        reject_unverified_blobs(&request.value).map_err(PublicRepoWriteError::InvalidRequest)?;
1202        let state = self.account.active_key.lock();
1203        let signing_key = state
1204            .as_ref()
1205            .ok_or_else(|| anyhow!("no active signing authority for repository"))?;
1206        if let Some(intent) = self.store.intent(&self.did, &request.request_id)? {
1207            if intent.head_condition.as_deref() != Some(condition.identity().as_str()) {
1208                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
1209                    "publication request condition changed or is not bound"
1210                )));
1211            }
1212            if intent.generated_rkey != request.rkey.is_none() {
1213                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
1214                    "publication request id was reused with a different key mode"
1215                )));
1216            }
1217            let rkey = match request.rkey.as_ref() {
1218                Some(rkey) => rkey.clone(),
1219                None => AtprotoRecordKey::new(intent.rkey.clone())?,
1220            };
1221            let record = AtprotoRecord::new(request.collection.clone(), &rkey, request.value)
1222                .map_err(PublicRepoWriteError::InvalidRequest)?;
1223            if record.bytes().len() > MAX_PUBLIC_RECORD_BYTES {
1224                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
1225                    "public record exceeds {MAX_PUBLIC_RECORD_BYTES}-byte canonical encoded limit"
1226                )));
1227            }
1228            if !(intent.request_id == request.request_id
1229                && intent.principal == request.principal
1230                && intent.did == self.did
1231                && intent.collection == record.collection()
1232                && intent.rkey == record.rkey().as_str()
1233                && intent.cid == record.cid().to_string())
1234            {
1235                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
1236                    "publication request id was reused with different content"
1237                )));
1238            }
1239            let snapshot = self
1240                .store
1241                .snapshot(&self.did)?
1242                .ok_or_else(|| anyhow!("publication intent exists without a repository"))?;
1243            if !snapshot
1244                .records
1245                .get(&(request.collection.clone(), rkey))
1246                .is_some_and(|stored| stored.bytes() == record.bytes())
1247            {
1248                return Err(anyhow!("publication intent record does not match repository").into());
1249            }
1250            let commit_bytes = self
1251                .store
1252                .db
1253                .get(commit_block_key(&self.did, &intent.commit_cid))
1254                .context("publication intent commit block read failed")?
1255                .ok_or_else(|| anyhow!("publication intent commit block is missing"))?;
1256            let commit = Commit::from_atproto_dag_cbor(&commit_bytes)
1257                .context("publication intent commit is invalid")?;
1258            let commit_cid = commit.cid_atproto()?;
1259            if commit.did != self.did || intent.commit_cid != commit_cid.to_string() {
1260                return Err(
1261                    anyhow!("publication intent commit does not match stored block").into(),
1262                );
1263            }
1264            return Ok(PublicCommitResult {
1265                uri: record.uri(&self.did),
1266                cid: record.cid(),
1267                commit_cid,
1268            });
1269        }
1270
1271        let existing = self.store.snapshot(&self.did)?;
1272        let mut budget = SnapshotBudget::default();
1273        let (mut keyed, previous, previous_rev) = match existing {
1274            Some(snapshot) => {
1275                let previous = snapshot.commit.cid_atproto()?;
1276                let previous_rev = snapshot.commit.rev;
1277                if !condition.matches(Some(previous)) {
1278                    return Err(PublicRepoWriteError::InvalidSwap);
1279                }
1280                for ((collection, rkey), record) in &snapshot.records {
1281                    budget.charge(
1282                        record_key(&self.did, collection, rkey.as_str()).len(),
1283                        record.bytes().len(),
1284                    )?;
1285                }
1286                let keyed = snapshot
1287                    .records
1288                    .into_iter()
1289                    .map(|((collection, rkey), record)| {
1290                        (format!("{collection}/{}", rkey.as_str()), record.cid())
1291                    })
1292                    .collect();
1293                (keyed, Some(previous), Some(previous_rev))
1294            }
1295            None => {
1296                if !condition.matches(None) {
1297                    return Err(PublicRepoWriteError::InvalidSwap);
1298                }
1299                (BTreeMap::new(), None, None)
1300            }
1301        };
1302        let generated_rkey = request.rkey.is_none();
1303        let rkey = match request.rkey {
1304            Some(rkey) => rkey,
1305            None => allocate_record_key(&request.collection, &keyed, previous_rev)?,
1306        };
1307        let record = AtprotoRecord::new(request.collection.clone(), rkey, request.value)
1308            .map_err(PublicRepoWriteError::InvalidRequest)?;
1309        if record.bytes().len() > MAX_PUBLIC_RECORD_BYTES {
1310            return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
1311                "public record exceeds {MAX_PUBLIC_RECORD_BYTES}-byte canonical encoded limit"
1312            )));
1313        }
1314        let record_key = format!("{}/{}", record.collection(), record.rkey().as_str());
1315        if keyed.contains_key(&record_key) {
1316            return Err(PublicRepoWriteError::RecordAlreadyExists);
1317        }
1318        budget
1319            .charge(
1320                self::record_key(&self.did, record.collection(), record.rkey().as_str()).len(),
1321                record.bytes().len(),
1322            )
1323            .map_err(PublicRepoWriteError::InvalidRequest)?;
1324        keyed.insert(record_key, record.cid());
1325
1326        let tree = Node::from_keyed_records(&keyed);
1327        let (root_data, _) = tree.to_node_data_with_blocks_atproto()?;
1328        let unsigned = UnsignedCommit::new(
1329            self.did.clone(),
1330            root_data.cid_atproto()?,
1331            next_revision(previous_rev),
1332            previous,
1333        );
1334        let commit = Commit::sign_atproto(&unsigned, signing_key)?;
1335        let commit_cid = commit.cid_atproto()?;
1336        let intent = PublicationIntent {
1337            request_id: request.request_id,
1338            principal: request.principal,
1339            did: self.did.clone(),
1340            collection: record.collection().to_owned(),
1341            rkey: record.rkey().as_str().to_owned(),
1342            generated_rkey,
1343            cid: record.cid().to_string(),
1344            commit_cid: commit_cid.to_string(),
1345            head_condition: Some(condition.identity()),
1346        };
1347        self.store
1348            .write_transaction(&self.did, &record, &commit, &intent)?;
1349        Ok(PublicCommitResult {
1350            uri: record.uri(&self.did),
1351            cid: record.cid(),
1352            commit_cid,
1353        })
1354    }
1355
1356    #[cfg(not(feature = "rocksdb"))]
1357    fn create_record_with_condition(
1358        &self,
1359        _request: PublicCreateRequest,
1360        _condition: PublicHeadCondition,
1361    ) -> Result<PublicCommitResult, PublicRepoWriteError> {
1362        Err(PublicRepoWriteError::Internal(anyhow::anyhow!(
1363            "public repository store requires the `rocksdb` feature",
1364        )))
1365    }
1366
1367    /// XRPC creation with an optional base32 CID head condition. Unlike the
1368    /// native typed API, omission permits a new write on any current head.
1369    /// Authorization, retry lookup, head comparison and the write all use the
1370    /// shared transaction path; no repository state is read by this adapter.
1371    /// This argument supersedes `request.expected_prev`.
1372    pub fn create_record_with_expected_prev_text(
1373        &self,
1374        request: PublicCreateRequest,
1375        expected_prev: Option<&str>,
1376    ) -> Result<PublicCommitResult, PublicRepoWriteError> {
1377        let condition = match expected_prev {
1378            None => PublicHeadCondition::Unconditional,
1379            Some(expected) => PublicHeadCondition::Exact(Some(
1380                parse_atproto_json_cid(expected)
1381                    .context("invalid swapCommit CID")
1382                    .map_err(PublicRepoWriteError::InvalidRequest)?,
1383            )),
1384        };
1385        self.create_record_with_condition(request, condition)
1386    }
1387}
1388
1389pub struct HostedAccountSelfAuthorizer;
1390
1391impl PublicPublicationAuthorizer for HostedAccountSelfAuthorizer {
1392    fn authorize(&self, principal: &str, account: &str, collection: &str) -> Result<()> {
1393        validate_principal(principal)?;
1394        ensure!(
1395            principal == account,
1396            "direct publication principal is not repo owner"
1397        );
1398        ensure!(
1399            matches!(collection, "app.bsky.feed.post" | "app.bsky.actor.profile"),
1400            "collection is outside the enabled posting slice"
1401        );
1402        let host = account
1403            .strip_prefix("did:web:")
1404            .filter(|host| !host.is_empty() && !host.contains(['/', ':', '%']));
1405        ensure!(
1406            host.is_some(),
1407            "direct publication requires a hosted did:web account"
1408        );
1409        Ok(())
1410    }
1411}
1412
1413pub struct HostedAccountPublicRepoWriter {
1414    store: Arc<PublicRepoStore>,
1415    account_store: Arc<hyprstream_pds_service::AccountRecordStore>,
1416    authority: hyprstream_rpc::Subject,
1417    authorizer: Arc<dyn PublicPublicationAuthorizer>,
1418}
1419
1420impl std::fmt::Debug for HostedAccountPublicRepoWriter {
1421    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1422        f.debug_struct("HostedAccountPublicRepoWriter")
1423            .finish_non_exhaustive()
1424    }
1425}
1426
1427impl HostedAccountPublicRepoWriter {
1428    pub fn new(
1429        store: Arc<PublicRepoStore>,
1430        account_store: Arc<hyprstream_pds_service::AccountRecordStore>,
1431        authority: hyprstream_rpc::Subject,
1432        authorizer: Arc<dyn PublicPublicationAuthorizer>,
1433    ) -> Self {
1434        Self {
1435            store,
1436            account_store,
1437            authority,
1438            authorizer,
1439        }
1440    }
1441
1442    pub(crate) async fn has_account(&self, did: &str) -> Result<bool> {
1443        Ok(self
1444            .account_store
1445            .verifying_key_for_hosted_did(&self.authority, did)
1446            .await?
1447            .is_some())
1448    }
1449
1450    // Called while holding the per-DID external transaction admission. The
1451    // locator avoids custody signing on every legacy read; the authority still
1452    // verifies it against native genesis and never hides a corrupt sidecar.
1453    async fn genesis_bytes(
1454        &self,
1455        did: &str,
1456    ) -> Result<Option<(Vec<u8>, Vec<u8>)>, PublicRepoWriteError> {
1457        let store = Arc::clone(&self.store);
1458        let accounts = Arc::clone(&self.account_store);
1459        let authority = self.authority.clone();
1460        let did = did.to_owned();
1461        let runtime = tokio::runtime::Handle::current();
1462        blocking_public_work(move || {
1463            let cached = store.public_genesis_bytes(&did)?;
1464            // Account reads are async, but verification and optional custody
1465            // signing must not consume a Tokio request worker.
1466            runtime
1467                .block_on(accounts.hosted_repo_genesis_with_cached_public(
1468                    &authority,
1469                    &did,
1470                    cached.as_deref(),
1471                ))
1472                .map_err(|error| PublicRepoWriteError::Internal(anyhow!(error)))
1473        })
1474        .await
1475    }
1476
1477    pub async fn create_record(
1478        &self,
1479        request: PublicCreateRequest,
1480        expected_prev: Option<&str>,
1481    ) -> Result<PublicCommitResult, PublicRepoWriteError> {
1482        // Admission and authorization precede all authority reads and storage.
1483        let authorizer = Arc::clone(&self.authorizer);
1484        let admission_store = Arc::clone(&self.store);
1485        let validated = request.clone();
1486        let expected_prev = expected_prev.map(str::to_owned);
1487        let expected = expected_prev.clone();
1488        let admission = blocking_public_work(move || {
1489            validate_request_id(&validated.request_id)
1490                .map_err(PublicRepoWriteError::InvalidRequest)?;
1491            validate_principal(&validated.principal)
1492                .map_err(PublicRepoWriteError::InvalidRequest)?;
1493            validate_did(&validated.did).map_err(PublicRepoWriteError::InvalidRequest)?;
1494            if let Some(expected) = expected {
1495                parse_atproto_json_cid(&expected).map_err(PublicRepoWriteError::InvalidRequest)?;
1496            }
1497            authorizer
1498                .authorize(&validated.principal, &validated.did, &validated.collection)
1499                .map_err(PublicRepoWriteError::Authorization)?;
1500            reject_unverified_blobs(&validated.value)
1501                .map_err(PublicRepoWriteError::InvalidRequest)?;
1502            let key = validated
1503                .rkey
1504                .clone()
1505                .unwrap_or(AtprotoRecordKey::new("self")?);
1506            let record = AtprotoRecord::new(validated.collection, &key, validated.value)
1507                .map_err(PublicRepoWriteError::InvalidRequest)?;
1508            if record.bytes().len() > MAX_PUBLIC_RECORD_BYTES {
1509                return Err(PublicRepoWriteError::InvalidRequest(anyhow!(
1510                    "public record exceeds canonical byte limit"
1511                )));
1512            }
1513            let mut accounts = admission_store.accounts.lock();
1514            Ok(Arc::clone(
1515                &accounts.entry(validated.did).or_default().external_write,
1516            ))
1517        })
1518        .await?;
1519        let _admission = admission.lock_owned().await;
1520        let verifying_key = self
1521            .account_store
1522            .verifying_key_for_hosted_did(&self.authority, &request.did)
1523            .await
1524            .map_err(|error| PublicRepoWriteError::Internal(anyhow!(error)))?
1525            .ok_or_else(|| {
1526                PublicRepoWriteError::Authorization(anyhow!("hosted account is not locally owned"))
1527            })?;
1528        let (native_bytes, public_bytes) =
1529            self.genesis_bytes(&request.did).await?.ok_or_else(|| {
1530                PublicRepoWriteError::Authorization(anyhow!(
1531                    "hosted account has no repository genesis"
1532                ))
1533            })?;
1534        let store = Arc::clone(&self.store);
1535        let authorizer = Arc::clone(&self.authorizer);
1536        let (writer, prepared) = blocking_public_work(move || {
1537            let native = Commit::from_dag_cbor(&native_bytes)?;
1538            native.verify(&verifying_key)?;
1539            let public = Commit::from_atproto_dag_cbor(&public_bytes)?;
1540            public.verify_atproto(&verifying_key)?;
1541            if !(native.did == request.did
1542                && native.prev.is_none()
1543                && public.did == native.did
1544                && public.data == native.data
1545                && public.rev == native.rev
1546                && public.prev == native.prev)
1547            {
1548                return Err(PublicRepoWriteError::Internal(anyhow!(
1549                    "hosted dual-format repository genesis differs"
1550                )));
1551            }
1552            // Recheck policy immediately before initialization; no private key
1553            // crosses this boundary, only authority-verified public artifacts.
1554            authorizer
1555                .authorize(&request.principal, &request.did, &request.collection)
1556                .map_err(PublicRepoWriteError::Authorization)?;
1557            let writer = Arc::new(PublicRepoWriter::new_external(
1558                Arc::clone(&store),
1559                request.did.clone(),
1560                verifying_key,
1561                authorizer,
1562            )?);
1563            store.seed_public_genesis(&request.did, public, &verifying_key)?;
1564            let prepared = writer.prepare_external_record_with_expected_prev_text(
1565                request,
1566                expected_prev.as_deref(),
1567            )?;
1568            Ok((writer, prepared))
1569        })
1570        .await?;
1571        match prepared {
1572            PublicCreatePreparation::Existing(result) => Ok(result),
1573            PublicCreatePreparation::Pending(pending) => {
1574                let signature = self
1575                    .account_store
1576                    .sign_for_hosted_did(
1577                        &self.authority,
1578                        &pending.request.did,
1579                        &pending.signing_bytes()?,
1580                    )
1581                    .await
1582                    .map_err(|error| PublicRepoWriteError::Internal(anyhow!(error)))?
1583                    .ok_or_else(|| {
1584                        PublicRepoWriteError::Authorization(anyhow!(
1585                            "hosted account is not locally owned"
1586                        ))
1587                    })?;
1588                blocking_public_work(move || writer.finish_external_record(*pending, signature))
1589                    .await
1590            }
1591        }
1592    }
1593}
1594
1595/// A durable read source. Resolving authority and reading/encoding happen only
1596/// after the route acquires snapshot/body admission. Hosted failures never
1597/// fall back to the unrelated model snapshot cache.
1598pub(crate) enum PublicRepoReader {
1599    Local(Arc<PublicRepoWriter>),
1600    Hosted {
1601        writer: Arc<HostedAccountPublicRepoWriter>,
1602        did: String,
1603    },
1604}
1605
1606impl PublicRepoReader {
1607    pub(crate) fn did(&self) -> &str {
1608        match self {
1609            Self::Local(writer) => writer.did(),
1610            Self::Hosted { did, .. } => did,
1611        }
1612    }
1613
1614    pub(crate) async fn read<T: Send + 'static>(
1615        self,
1616        encode: impl FnOnce(Option<(PublicRepoSnapshot, p256::ecdsa::VerifyingKey)>) -> Result<T>
1617            + Send
1618            + 'static,
1619    ) -> Result<T> {
1620        match self {
1621            Self::Local(writer) => {
1622                tokio::task::spawn_blocking(move || encode(writer.public_snapshot()?)).await?
1623            }
1624            Self::Hosted { writer, did } => {
1625                let store = Arc::clone(&writer.store);
1626                let lock_did = did.clone();
1627                let admission = blocking_public_work(move || {
1628                    let mut accounts = store.accounts.lock();
1629                    Ok(Arc::clone(
1630                        &accounts.entry(lock_did).or_default().external_write,
1631                    ))
1632                })
1633                .await?;
1634                let _admission = admission.lock_owned().await;
1635                let key = writer
1636                    .account_store
1637                    .verifying_key_for_hosted_did(&writer.authority, &did)
1638                    .await?;
1639                let genesis = if key.is_some() {
1640                    writer.genesis_bytes(&did).await?
1641                } else {
1642                    None
1643                };
1644                tokio::task::spawn_blocking(move || {
1645                    let (Some(key), Some((_, public))) = (key, genesis) else {
1646                        return encode(None);
1647                    };
1648                    // Seed validates both genesis and any existing immutable
1649                    // archive under active_key; it never rewinds a later head.
1650                    writer.store.seed_public_genesis(
1651                        &did,
1652                        Commit::from_atproto_dag_cbor(&public)?,
1653                        &key,
1654                    )?;
1655                    let account = {
1656                        let mut accounts = writer.store.accounts.lock();
1657                        Arc::clone(accounts.entry(did.clone()).or_default())
1658                    };
1659                    let _transaction = account.active_key.lock();
1660                    let snapshot = writer.store.snapshot(&did)?;
1661                    if let Some(snapshot) = &snapshot {
1662                        snapshot.commit.verify_atproto(&key)?;
1663                    }
1664                    encode(snapshot.map(|snapshot| (snapshot, key)))
1665                })
1666                .await?
1667            }
1668        }
1669    }
1670}
1671
1672async fn blocking_public_work<T: Send + 'static>(
1673    work: impl FnOnce() -> Result<T, PublicRepoWriteError> + Send + 'static,
1674) -> Result<T, PublicRepoWriteError> {
1675    tokio::task::spawn_blocking(work)
1676        .await
1677        .map_err(|error| PublicRepoWriteError::Internal(error.into()))?
1678}
1679
1680/// Allocate after authorization, retry lookup and head selection, while the
1681/// writer lock is held. Advancing from the durable revision survives restarts
1682/// and clock rollback; collision checks also cover explicitly supplied keys.
1683fn allocate_record_key(
1684    collection: &str,
1685    keyed: &BTreeMap<String, Cid>,
1686    previous_rev: Option<Tid>,
1687) -> Result<AtprotoRecordKey> {
1688    let mut candidate = next_revision(previous_rev);
1689    loop {
1690        let rkey = AtprotoRecordKey::from(candidate);
1691        if !keyed.contains_key(&format!("{collection}/{}", rkey.as_str())) {
1692            return Ok(rkey);
1693        }
1694        candidate = Tid::from_raw(
1695            candidate
1696                .to_raw()
1697                .checked_add(1)
1698                .ok_or_else(|| anyhow!("public record key space exhausted"))?,
1699        );
1700    }
1701}
1702
1703/// Decode a canonical AT JSON CID link without accepting other codecs,
1704/// hashes, overlong varints, or noncanonical base32 spellings.
1705fn parse_atproto_json_cid(text: &str) -> Result<Cid> {
1706    // CIDv1, one-byte raw/DAG-CBOR codec, sha2-256, and 32 digest bytes.
1707    // These 36 bytes occupy 58 base32 digits plus the multibase prefix.
1708    ensure!(
1709        text.len() == 59 && text.starts_with('b'),
1710        "invalid AT CID text"
1711    );
1712    let mut raw = Vec::with_capacity(36);
1713    let mut pending = 0u16;
1714    let mut bits = 0u32;
1715    for digit in text.bytes().skip(1) {
1716        let digit = match digit {
1717            b'a'..=b'z' => digit - b'a',
1718            b'2'..=b'7' => digit - b'2' + 26,
1719            _ => return Err(anyhow!("AT CID must use lowercase base32")),
1720        };
1721        pending = (pending << 5) | u16::from(digit);
1722        bits += 5;
1723        if bits >= 8 {
1724            bits -= 8;
1725            raw.push((pending >> bits) as u8);
1726            pending &= (1 << bits) - 1;
1727        }
1728    }
1729    ensure!(
1730        pending == 0
1731            && raw.len() == 36
1732            && matches!(raw.as_slice(), [1, 0x55 | 0x71, 0x12, 0x20, ..]),
1733        "AT CID requires canonical raw or DAG-CBOR SHA-256 bytes"
1734    );
1735    Cid::from_bytes(&raw)
1736}
1737
1738/// Convert AT JSON into the value type used by the public codec. Reserved
1739/// single-key `$link` and `$bytes` objects become links and byte strings,
1740/// never ordinary maps. Invalid wrappers, floats and integers outside signed
1741/// 64-bit range are rejected before serialization.
1742pub fn json_to_dag_cbor(value: &serde_json::Value) -> Result<DagCbor> {
1743    Ok(match value {
1744        serde_json::Value::Null => DagCbor::Null,
1745        serde_json::Value::Bool(value) => DagCbor::Bool(*value),
1746        serde_json::Value::Number(number) => {
1747            if let Some(value) = number.as_i64() {
1748                if value >= 0 {
1749                    DagCbor::Unsigned(value as u64)
1750                } else {
1751                    DagCbor::Negative(value as i128)
1752                }
1753            } else {
1754                return Err(anyhow!("AT record numbers must be signed integers"));
1755            }
1756        }
1757        serde_json::Value::String(value) => DagCbor::Text(value.clone()),
1758        serde_json::Value::Array(values) => DagCbor::List(
1759            values
1760                .iter()
1761                .map(json_to_dag_cbor)
1762                .collect::<Result<Vec<_>>>()?,
1763        ),
1764        serde_json::Value::Object(values) if values.contains_key("$link") => {
1765            ensure!(values.len() == 1, "AT JSON link must contain only $link");
1766            let text = values
1767                .get("$link")
1768                .and_then(serde_json::Value::as_str)
1769                .ok_or_else(|| anyhow!("AT JSON $link must be a CID string"))?;
1770            DagCbor::Link(parse_atproto_json_cid(text)?)
1771        }
1772        serde_json::Value::Object(values) if values.contains_key("$bytes") => {
1773            ensure!(values.len() == 1, "AT JSON bytes must contain only $bytes");
1774            let text = values
1775                .get("$bytes")
1776                .and_then(serde_json::Value::as_str)
1777                .ok_or_else(|| anyhow!("AT JSON $bytes must be a base64 string"))?;
1778            let bytes = STANDARD
1779                .decode(text)
1780                .or_else(|_| STANDARD_NO_PAD.decode(text))
1781                .context("invalid AT JSON base64 bytes")?;
1782            DagCbor::Bytes(bytes)
1783        }
1784        serde_json::Value::Object(values) => DagCbor::Map(
1785            values
1786                .iter()
1787                .map(|(key, value)| Ok((DagCbor::Text(key.clone()), json_to_dag_cbor(value)?)))
1788                .collect::<Result<Vec<_>>>()?,
1789        ),
1790    })
1791}
1792
1793fn next_revision(previous: Option<Tid>) -> Tid {
1794    let now = Tid::now();
1795    match previous {
1796        Some(previous) if now <= previous => Tid::from_raw(previous.to_raw().saturating_add(1)),
1797        _ => now,
1798    }
1799}
1800
1801#[cfg(all(test, feature = "rocksdb"))]
1802mod tests {
1803    #![allow(clippy::expect_used, clippy::unwrap_used, clippy::indexing_slicing)]
1804
1805    use super::*;
1806    use std::sync::atomic::{AtomicBool, Ordering};
1807
1808    struct Gate {
1809        allow: AtomicBool,
1810    }
1811
1812    impl PublicPublicationAuthorizer for Gate {
1813        fn authorize(&self, _: &str, _: &str, _: &str) -> Result<()> {
1814            ensure!(self.allow.load(Ordering::Acquire), "publication denied");
1815            Ok(())
1816        }
1817
1818        fn authorize_key_promotion(&self, _: &str, _: &str) -> Result<()> {
1819            ensure!(self.allow.load(Ordering::Acquire), "promotion denied");
1820            Ok(())
1821        }
1822    }
1823
1824    fn post() -> DagCbor {
1825        DagCbor::str_map([
1826            ("$type", DagCbor::Text("app.bsky.feed.post".into())),
1827            ("text", DagCbor::Text("durable".into())),
1828        ])
1829    }
1830
1831    fn create_request(rkey: u64, expected_prev: Option<Cid>) -> PublicCreateRequest {
1832        PublicCreateRequest {
1833            request_id: format!("req-{rkey}"),
1834            principal: "did:at9p:agent".into(),
1835            did: "did:web:tormentnexus.social".into(),
1836            collection: "app.bsky.feed.post".into(),
1837            rkey: Some(Tid::from_raw(rkey).into()),
1838            value: post(),
1839            expected_prev,
1840        }
1841    }
1842
1843    #[test]
1844    fn public_repo_account_locks_allow_independent_dids_and_serialize_same_did() {
1845        use std::sync::mpsc;
1846        use std::time::Duration;
1847
1848        let dir = tempfile::tempdir().unwrap();
1849        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
1850        let key_a = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
1851        let key_b = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
1852        let next_b = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
1853        let next_b_vk = *next_b.verifying_key();
1854        let gate = Arc::new(Gate {
1855            allow: AtomicBool::new(true),
1856        });
1857        let request_a = create_request(1, None);
1858        let writer_a =
1859            PublicRepoWriter::new(store.clone(), &request_a.did, key_a.clone(), gate.clone())
1860                .unwrap();
1861        let same_did =
1862            PublicRepoWriter::new(store.clone(), &request_a.did, key_a.clone(), gate.clone())
1863                .unwrap();
1864        assert!(Arc::ptr_eq(&writer_a.account, &same_did.account));
1865        let mut request_b = create_request(1, None);
1866        request_b.did = "did:web:independent.example.com".into();
1867        let did_b = request_b.did.clone();
1868
1869        // Model a long account-A transaction/promotion without sleeping or
1870        // filling a large repo. Another A handle must wait; B must finish its
1871        // constructor, create and key promotion while A is still locked.
1872        let held = writer_a.account.active_key.lock();
1873        let (started_tx, started_rx) = mpsc::channel();
1874        let (same_tx, same_rx) = mpsc::channel();
1875        let same_thread = std::thread::spawn(move || {
1876            let _ = started_tx.send(());
1877            let _ = same_tx.send(same_did.create_record(request_a));
1878        });
1879        let other_store = store.clone();
1880        let (other_tx, other_rx) = mpsc::channel();
1881        let other_thread = std::thread::spawn(move || {
1882            let result = (|| -> Result<()> {
1883                let other = PublicRepoWriter::new(
1884                    other_store.clone(),
1885                    &request_b.did,
1886                    key_b.clone(),
1887                    gate,
1888                )?;
1889                let first = other.create_record(request_b.clone())?;
1890                let head =
1891                    other.promote_signing_key("did:at9p:agent", key_b.verifying_key(), next_b)?;
1892                ensure!(
1893                    head.is_some() && head != Some(first.commit_cid),
1894                    "B promotion must reconcile its head"
1895                );
1896                ensure!(
1897                    other.create_record(request_b)? == first,
1898                    "B retry must retain original receipt"
1899                );
1900                Ok(())
1901            })();
1902            let _ = other_tx.send(result);
1903        });
1904        let started = started_rx.recv_timeout(Duration::from_secs(10));
1905        let independent = other_rx.recv_timeout(Duration::from_secs(10));
1906        let same_while_locked = same_rx.try_recv();
1907        // Always release before assertions/joins: a regression must fail the
1908        // bounded timeout rather than leave worker threads deadlocked.
1909        drop(held);
1910        same_thread.join().unwrap();
1911        other_thread.join().unwrap();
1912        started.unwrap();
1913        independent
1914            .expect("unrelated DID blocked behind account A")
1915            .unwrap();
1916        assert!(
1917            matches!(same_while_locked, Err(mpsc::TryRecvError::Empty)),
1918            "same-DID write bypassed its transaction lock"
1919        );
1920        let result_a = same_rx
1921            .recv_timeout(Duration::from_secs(10))
1922            .unwrap()
1923            .unwrap();
1924        let repo_a = store
1925            .snapshot("did:web:tormentnexus.social")
1926            .unwrap()
1927            .unwrap();
1928        assert_eq!(repo_a.commit.cid_atproto().unwrap(), result_a.commit_cid);
1929        repo_a.commit.verify_atproto(key_a.verifying_key()).unwrap();
1930        assert_eq!(
1931            writer_a.active_verifying_key().unwrap(),
1932            *key_a.verifying_key()
1933        );
1934        store
1935            .snapshot(&did_b)
1936            .unwrap()
1937            .unwrap()
1938            .commit
1939            .verify_atproto(&next_b_vk)
1940            .unwrap();
1941    }
1942
1943    #[test]
1944    fn public_repo_rotation_reconciles_idle_head_and_surviving_writers() {
1945        let dir = tempfile::tempdir().unwrap();
1946        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
1947        let old = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
1948        let candidate = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
1949        let gate = Arc::new(Gate {
1950            allow: AtomicBool::new(true),
1951        });
1952        let request = create_request(1, None);
1953        let writer =
1954            PublicRepoWriter::new(store.clone(), &request.did, old.clone(), gate.clone()).unwrap();
1955        let survivor =
1956            PublicRepoWriter::new(store.clone(), &request.did, old.clone(), gate.clone()).unwrap();
1957        let first = writer.create_record(request.clone()).unwrap();
1958        let original = store.snapshot(&request.did).unwrap().unwrap().commit;
1959        let original_block = store
1960            .db
1961            .get(commit_block_key(
1962                &request.did,
1963                &first.commit_cid.to_string(),
1964            ))
1965            .unwrap()
1966            .unwrap();
1967        let reconciled = writer
1968            .promote_signing_key("did:at9p:agent", old.verifying_key(), candidate.clone())
1969            .unwrap()
1970            .unwrap();
1971        assert_ne!(reconciled, first.commit_cid);
1972        assert_eq!(
1973            survivor.active_verifying_key().unwrap(),
1974            *candidate.verifying_key()
1975        );
1976        let idle = store.snapshot(&request.did).unwrap().unwrap().commit;
1977        assert_eq!(idle.unsigned(), original.unsigned());
1978        idle.verify_atproto(candidate.verifying_key()).unwrap();
1979        assert!(idle.verify_atproto(old.verifying_key()).is_err());
1980        assert_eq!(survivor.create_record(request.clone()).unwrap(), first);
1981        assert_eq!(
1982            store
1983                .db
1984                .get(commit_block_key(
1985                    &request.did,
1986                    &first.commit_cid.to_string()
1987                ))
1988                .unwrap()
1989                .unwrap(),
1990            original_block
1991        );
1992        assert!(
1993            PublicRepoWriter::new(store.clone(), &request.did, old.clone(), gate.clone()).is_err()
1994        );
1995        let second = survivor
1996            .create_record(create_request(2, Some(reconciled)))
1997            .unwrap();
1998        let head = store.snapshot(&request.did).unwrap().unwrap().commit;
1999        head.verify_atproto(candidate.verifying_key()).unwrap();
2000        assert!(head.verify_atproto(old.verifying_key()).is_err());
2001        assert_eq!(head.prev, Some(reconciled));
2002        assert_eq!(head.cid_atproto().unwrap(), second.commit_cid);
2003        drop(survivor);
2004        drop(writer);
2005        drop(store);
2006        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2007        assert!(PublicRepoWriter::new(store.clone(), &request.did, old, gate.clone()).is_err());
2008        let reopened = PublicRepoWriter::new(store, &request.did, candidate, gate).unwrap();
2009        assert_eq!(reopened.create_record(request).unwrap(), first);
2010    }
2011
2012    #[test]
2013    fn public_repo_rotation_failure_keeps_active_authority_and_durable_state() {
2014        let dir = tempfile::tempdir().unwrap();
2015        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2016        let old = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2017        let candidate = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2018        let gate = Arc::new(Gate {
2019            allow: AtomicBool::new(true),
2020        });
2021        let request = create_request(1, None);
2022        let writer = PublicRepoWriter::new(store.clone(), &request.did, old.clone(), gate).unwrap();
2023        writer.create_record(request.clone()).unwrap();
2024        let all_bytes = || {
2025            store
2026                .db
2027                .iterator(rocksdb::IteratorMode::Start)
2028                .map(|entry| entry.unwrap())
2029                .collect::<Vec<_>>()
2030        };
2031        let before = all_bytes();
2032        struct RecordOnly;
2033        impl PublicPublicationAuthorizer for RecordOnly {
2034            fn authorize(&self, _: &str, _: &str, _: &str) -> Result<()> {
2035                Ok(())
2036            }
2037        }
2038        let unprivileged = PublicRepoWriter::new(
2039            store.clone(),
2040            &request.did,
2041            old.clone(),
2042            Arc::new(RecordOnly),
2043        )
2044        .unwrap();
2045        assert!(unprivileged
2046            .promote_signing_key("did:at9p:agent", old.verifying_key(), candidate.clone())
2047            .is_err());
2048        assert_eq!(writer.active_verifying_key().unwrap(), *old.verifying_key());
2049        assert_eq!(all_bytes(), before);
2050        assert!(writer
2051            .promote_signing_key(
2052                "did:at9p:agent",
2053                candidate.verifying_key(),
2054                candidate.clone()
2055            )
2056            .is_err());
2057        assert_eq!(writer.active_verifying_key().unwrap(), *old.verifying_key());
2058        assert_eq!(all_bytes(), before);
2059        // A validly encoded head signed by the wrong key must not be blessed
2060        // by rotation. Reconciliation fails without exposing the candidate.
2061        let head = store.snapshot(&request.did).unwrap().unwrap().commit;
2062        let substituted = Commit::sign_atproto(&head.unsigned(), &candidate).unwrap();
2063        store
2064            .db
2065            .put(
2066                commit_key(&request.did),
2067                substituted.to_atproto_dag_cbor().unwrap(),
2068            )
2069            .unwrap();
2070        let before = all_bytes();
2071        assert!(writer
2072            .promote_signing_key("did:at9p:agent", old.verifying_key(), candidate)
2073            .is_err());
2074        assert_eq!(writer.active_verifying_key().unwrap(), *old.verifying_key());
2075        assert_eq!(all_bytes(), before);
2076    }
2077
2078    #[test]
2079    fn public_repo_oauth_port_authority_survives_commit_reopen_and_retry() {
2080        let dir = tempfile::tempdir().unwrap();
2081        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2082        let gate = Arc::new(Gate {
2083            allow: AtomicBool::new(true),
2084        });
2085        let key = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2086        let mut request = create_request(1, None);
2087        request.did = "did:web:pds.example.test%3A8443".into();
2088        let writer =
2089            PublicRepoWriter::new(store.clone(), &request.did, key.clone(), gate.clone()).unwrap();
2090        let result = writer.create_record(request.clone()).unwrap();
2091        assert!(result
2092            .uri
2093            .starts_with("at://did:web:pds.example.test%3A8443/"));
2094        drop(writer);
2095        drop(store);
2096        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2097        let snapshot = store.snapshot(&request.did).unwrap().unwrap();
2098        assert_eq!(snapshot.commit.did, request.did);
2099        snapshot.commit.verify_atproto(key.verifying_key()).unwrap();
2100        let writer = PublicRepoWriter::new(store, &request.did, key, gate).unwrap();
2101        assert_eq!(writer.create_record(request).unwrap(), result);
2102    }
2103
2104    #[test]
2105    fn public_repo_encoded_size_boundary_rejects_without_writes() {
2106        let dir = tempfile::tempdir().unwrap();
2107        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2108        let gate = Arc::new(Gate {
2109            allow: AtomicBool::new(true),
2110        });
2111        let writer = PublicRepoWriter::new(
2112            store.clone(),
2113            "did:web:tormentnexus.social",
2114            p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng),
2115            gate,
2116        )
2117        .unwrap();
2118        let sized_request = |rkey, prev, size| {
2119            let mut request = create_request(rkey, prev);
2120            let value = |payload| {
2121                DagCbor::str_map([
2122                    ("$type", DagCbor::Text(request.collection.clone())),
2123                    ("text", DagCbor::Text("x".repeat(payload))),
2124                ])
2125            };
2126            let overhead = AtprotoRecord::new(
2127                &request.collection,
2128                request.rkey.as_ref().unwrap(),
2129                value(1024),
2130            )
2131            .unwrap()
2132            .bytes()
2133            .len()
2134                - 1024;
2135            request.value = value(size - overhead);
2136            assert_eq!(
2137                AtprotoRecord::new(
2138                    &request.collection,
2139                    request.rkey.as_ref().unwrap(),
2140                    request.value.clone()
2141                )
2142                .unwrap()
2143                .bytes()
2144                .len(),
2145                size
2146            );
2147            request
2148        };
2149        let oversized = sized_request(1, None, MAX_PUBLIC_RECORD_BYTES + 1);
2150        assert!(writer
2151            .create_record(oversized)
2152            .unwrap_err()
2153            .to_string()
2154            .contains("canonical encoded limit"));
2155        assert!(store
2156            .db
2157            .iterator(rocksdb::IteratorMode::Start)
2158            .next()
2159            .is_none());
2160        let at_limit = sized_request(1, None, MAX_PUBLIC_RECORD_BYTES);
2161        let first = writer.create_record(at_limit.clone()).unwrap();
2162        assert_eq!(writer.create_record(at_limit).unwrap(), first);
2163        let all_bytes = || {
2164            store
2165                .db
2166                .iterator(rocksdb::IteratorMode::Start)
2167                .map(|item| item.unwrap())
2168                .collect::<Vec<_>>()
2169        };
2170        let before = all_bytes();
2171        assert!(writer
2172            .create_record(sized_request(
2173                2,
2174                Some(first.commit_cid),
2175                MAX_PUBLIC_RECORD_BYTES + 1
2176            ))
2177            .unwrap_err()
2178            .to_string()
2179            .contains("canonical encoded limit"));
2180        assert_eq!(
2181            all_bytes(),
2182            before,
2183            "oversize rejection must not modify record/head/intent/block state"
2184        );
2185        let snapshot = store
2186            .snapshot("did:web:tormentnexus.social")
2187            .unwrap()
2188            .unwrap();
2189        assert_eq!(snapshot.commit.cid_atproto().unwrap(), first.commit_cid);
2190        assert_eq!(snapshot.records.len(), 1);
2191    }
2192
2193    #[test]
2194    fn public_repo_rejects_invalid_authorities_before_writing() {
2195        let dir = tempfile::tempdir().unwrap();
2196        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2197        let gate = Arc::new(Gate {
2198            allow: AtomicBool::new(true),
2199        });
2200        let key = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2201        for did in [
2202            "did:plc:abc",
2203            "did:plc:ewvi7nxzyoun6zhxrhs64oiz#key",
2204            "did:web:example.com#fragment",
2205            "did:web:example.com?query",
2206            "did:web:example.com/path",
2207            "did:web:example..com",
2208            "did:web:example.com%2Fpath",
2209            "did:web:example.arpa",
2210            "did:web:example.onion",
2211            "did:web:example.123",
2212            "did:web:example.1com",
2213            "did:web:127.0.0.1",
2214        ] {
2215            assert!(PublicRepoWriter::new(store.clone(), did, key.clone(), gate.clone()).is_err());
2216            assert!(store.snapshot(did).is_err());
2217        }
2218        assert!(store
2219            .db
2220            .iterator(rocksdb::IteratorMode::Start)
2221            .next()
2222            .is_none());
2223        let did = "did:plc:ewvi7nxzyoun6zhxrhs64oiz";
2224        let writer = PublicRepoWriter::new(store.clone(), did, key, gate).unwrap();
2225        let mut request = create_request(1, None);
2226        request.did = did.to_owned();
2227        writer.create_record(request).unwrap();
2228        assert_eq!(store.snapshot(did).unwrap().unwrap().commit.did, did);
2229    }
2230
2231    #[test]
2232    fn public_repo_general_keys_preserve_profile_self_through_reopen_and_retry() {
2233        let dir = tempfile::tempdir().unwrap();
2234        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2235        let gate = Arc::new(Gate {
2236            allow: AtomicBool::new(true),
2237        });
2238        let key = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2239        let mut profile = create_request(1, None);
2240        profile.collection = "app.bsky.actor.profile".into();
2241        profile.rkey = Some(AtprotoRecordKey::new("self").unwrap());
2242        profile.value = DagCbor::str_map([
2243            ("$type", DagCbor::Text(profile.collection.clone())),
2244            ("displayName", DagCbor::Text("Profile".into())),
2245        ]);
2246        let writer =
2247            PublicRepoWriter::new(store.clone(), &profile.did, key.clone(), gate.clone()).unwrap();
2248        let first = writer.create_record(profile.clone()).unwrap();
2249        assert_eq!(
2250            first.uri,
2251            format!("at://{}/app.bsky.actor.profile/self", profile.did)
2252        );
2253        let mut post = create_request(2, Some(first.commit_cid));
2254        post.rkey = Some(AtprotoRecordKey::new("custom-key:~").unwrap());
2255        let second = writer.create_record(post.clone()).unwrap();
2256        assert_eq!(writer.create_record(profile.clone()).unwrap(), first);
2257        let mut duplicate = profile.clone();
2258        duplicate.request_id = "duplicate-profile".into();
2259        duplicate.expected_prev = Some(second.commit_cid);
2260        assert!(writer
2261            .create_record(duplicate)
2262            .unwrap_err()
2263            .to_string()
2264            .contains("already exists"));
2265        drop(writer);
2266        drop(store);
2267
2268        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2269        let snapshot = store.snapshot(&profile.did).unwrap().unwrap();
2270        assert_eq!(snapshot.records.len(), 2);
2271        let stored =
2272            &snapshot.records[&(profile.collection.clone(), profile.rkey.clone().unwrap())];
2273        assert_eq!(stored.rkey().as_str(), "self");
2274        assert_eq!(stored.cid(), first.cid);
2275        assert_eq!(stored.value(), &profile.value);
2276        assert!(snapshot
2277            .records
2278            .contains_key(&(post.collection, post.rkey.unwrap())));
2279        snapshot.commit.verify_atproto(key.verifying_key()).unwrap();
2280        assert_eq!(snapshot.commit.cid_atproto().unwrap(), second.commit_cid);
2281        let writer = PublicRepoWriter::new(store.clone(), &profile.did, key, gate).unwrap();
2282        assert_eq!(writer.create_record(profile).unwrap(), first);
2283
2284        // Storage reconstruction validates keys instead of treating an
2285        // arbitrary path or separator as an ordinary record key.
2286        store
2287            .db
2288            .put(
2289                record_key(writer.did.as_str(), "app.bsky.actor.profile", "bad/key"),
2290                stored.bytes(),
2291            )
2292            .unwrap();
2293        assert!(store.snapshot(&writer.did).is_err());
2294    }
2295
2296    #[test]
2297    fn authorized_create_is_atomic_durable_and_idempotent() {
2298        let dir = tempfile::tempdir().expect("tempdir");
2299        let store = Arc::new(PublicRepoStore::open(dir.path()).expect("store"));
2300        let gate = Arc::new(Gate {
2301            allow: AtomicBool::new(true),
2302        });
2303        let key = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2304        let writer = PublicRepoWriter::new(
2305            store.clone(),
2306            "did:web:tormentnexus.social",
2307            key.clone(),
2308            gate.clone(),
2309        )
2310        .expect("writer");
2311        let request = PublicCreateRequest {
2312            request_id: "req-1".into(),
2313            principal: "did:at9p:agent".into(),
2314            did: "did:web:tormentnexus.social".into(),
2315            collection: "app.bsky.feed.post".into(),
2316            rkey: Some(Tid::from_raw(7).into()),
2317            value: post(),
2318            expected_prev: None,
2319        };
2320        let first = writer.create_record(request.clone()).expect("create");
2321        let retry = writer.create_record(request.clone()).expect("retry");
2322        assert_eq!(first, retry);
2323        let second = writer
2324            .create_record(PublicCreateRequest {
2325                request_id: "req-2".into(),
2326                principal: "did:at9p:agent".into(),
2327                did: "did:web:tormentnexus.social".into(),
2328                collection: "app.bsky.feed.post".into(),
2329                rkey: Some(Tid::from_raw(8).into()),
2330                value: post(),
2331                expected_prev: Some(first.commit_cid),
2332            })
2333            .expect("second create");
2334        assert_ne!(first.commit_cid, second.commit_cid);
2335        assert_eq!(
2336            writer
2337                .create_record(request.clone())
2338                .expect("retry after head advance"),
2339            first
2340        );
2341        let snapshot = store
2342            .snapshot("did:web:tormentnexus.social")
2343            .expect("snapshot")
2344            .expect("repo");
2345        assert_eq!(snapshot.records.len(), 2);
2346        assert_eq!(snapshot.commit.cid_atproto().unwrap(), second.commit_cid);
2347        snapshot
2348            .commit
2349            .verify_atproto(&writer.active_verifying_key().unwrap())
2350            .expect("commit signature");
2351        drop(writer);
2352        drop(store);
2353        let reopened = Arc::new(PublicRepoStore::open(dir.path()).expect("reopen"));
2354        assert_eq!(
2355            reopened
2356                .snapshot("did:web:tormentnexus.social")
2357                .expect("snapshot")
2358                .expect("repo")
2359                .records
2360                .len(),
2361            2
2362        );
2363        let writer = PublicRepoWriter::new(
2364            reopened.clone(),
2365            "did:web:tormentnexus.social",
2366            key,
2367            gate.clone(),
2368        )
2369        .expect("reopened writer");
2370        assert_eq!(
2371            writer
2372                .create_record(request.clone())
2373                .expect("durable retry"),
2374            first
2375        );
2376        assert_eq!(
2377            reopened
2378                .snapshot(&request.did)
2379                .unwrap()
2380                .unwrap()
2381                .commit
2382                .cid_atproto()
2383                .unwrap(),
2384            second.commit_cid
2385        );
2386        gate.allow.store(false, Ordering::Release);
2387        assert!(writer
2388            .create_record(request)
2389            .unwrap_err()
2390            .to_string()
2391            .contains("publication denied"));
2392    }
2393
2394    #[test]
2395    fn retry_checks_intent_record_and_commit_integrity() {
2396        let dir = tempfile::tempdir().unwrap();
2397        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2398        let gate = Arc::new(Gate {
2399            allow: AtomicBool::new(true),
2400        });
2401        let key = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2402        let request = create_request(1, None);
2403        let writer = PublicRepoWriter::new(store.clone(), &request.did, key, gate).unwrap();
2404        let first = writer.create_record(request.clone()).unwrap();
2405        let mut changed = request.clone();
2406        changed.value = DagCbor::str_map([
2407            ("$type", DagCbor::Text(request.collection.clone())),
2408            ("text", DagCbor::Text("changed".into())),
2409        ]);
2410        assert!(writer
2411            .create_record(changed)
2412            .unwrap_err()
2413            .to_string()
2414            .contains("different content"));
2415        let mut changed = request.clone();
2416        changed.principal = "another-agent".into();
2417        assert!(writer
2418            .create_record(changed)
2419            .unwrap_err()
2420            .to_string()
2421            .contains("different content"));
2422
2423        let block_key = commit_block_key(&request.did, &first.commit_cid.to_string());
2424        let original = store.db.get(&block_key).unwrap().unwrap();
2425        let mut tampered = Commit::from_atproto_dag_cbor(&original).unwrap();
2426        tampered.rev = Tid::from_raw(tampered.rev.to_raw() + 1);
2427        store
2428            .db
2429            .put(&block_key, tampered.to_atproto_dag_cbor().unwrap())
2430            .unwrap();
2431        assert!(writer
2432            .create_record(request.clone())
2433            .unwrap_err()
2434            .to_string()
2435            .contains("does not match stored block"));
2436        store.db.delete(&block_key).unwrap();
2437        assert!(writer
2438            .create_record(request.clone())
2439            .unwrap_err()
2440            .to_string()
2441            .contains("commit block is missing"));
2442        store.db.put(&block_key, original).unwrap();
2443
2444        // A valid repository containing another record must not satisfy an
2445        // intent whose record path has disappeared.
2446        let other_request = create_request(2, Some(first.commit_cid));
2447        writer.create_record(other_request).unwrap();
2448        let mut intent = store
2449            .intent(&request.did, &request.request_id)
2450            .unwrap()
2451            .unwrap();
2452        let missing = create_request(3, None);
2453        intent.rkey = missing.rkey.as_ref().unwrap().as_str().to_owned();
2454        store
2455            .db
2456            .put(
2457                intent_key(&request.did, &request.request_id),
2458                serde_json::to_vec(&intent).unwrap(),
2459            )
2460            .unwrap();
2461        let mut missing = missing;
2462        missing.request_id = request.request_id;
2463        assert!(writer
2464            .create_record(missing)
2465            .unwrap_err()
2466            .to_string()
2467            .contains("record does not match repository"));
2468    }
2469
2470    #[test]
2471    fn snapshots_remain_consistent_during_concurrent_publications() {
2472        let dir = tempfile::tempdir().unwrap();
2473        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2474        let gate = Arc::new(Gate {
2475            allow: AtomicBool::new(true),
2476        });
2477        let key = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2478        let verifying_key = *key.verifying_key();
2479        let writer =
2480            PublicRepoWriter::new(store.clone(), "did:web:tormentnexus.social", key, gate).unwrap();
2481        let first = writer.create_record(create_request(1, None)).unwrap();
2482        let start = std::sync::Barrier::new(4);
2483        let done = AtomicBool::new(false);
2484        std::thread::scope(|scope| {
2485            for _ in 0..3 {
2486                scope.spawn(|| {
2487                    start.wait();
2488                    let mut reads = 0;
2489                    while !done.load(Ordering::Acquire) || reads == 0 {
2490                        let snapshot = store
2491                            .snapshot("did:web:tormentnexus.social")
2492                            .unwrap()
2493                            .unwrap();
2494                        assert!(!snapshot.records.is_empty());
2495                        snapshot.commit.verify_atproto(&verifying_key).unwrap();
2496                        reads += 1;
2497                    }
2498                });
2499            }
2500            start.wait();
2501            let mut head = first.commit_cid;
2502            for rkey in 2..=64 {
2503                head = writer
2504                    .create_record(create_request(rkey, Some(head)))
2505                    .unwrap()
2506                    .commit_cid;
2507            }
2508            done.store(true, Ordering::Release);
2509        });
2510        assert_eq!(
2511            store
2512                .snapshot("did:web:tormentnexus.social")
2513                .unwrap()
2514                .unwrap()
2515                .records
2516                .len(),
2517            64
2518        );
2519    }
2520
2521    #[test]
2522    fn authorization_and_head_cas_fail_closed() {
2523        let dir = tempfile::tempdir().expect("tempdir");
2524        let store = Arc::new(PublicRepoStore::open(dir.path()).expect("store"));
2525        let gate = Arc::new(Gate {
2526            allow: AtomicBool::new(false),
2527        });
2528        let key = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2529        let writer =
2530            PublicRepoWriter::new(store, "did:web:tormentnexus.social", key, gate).expect("writer");
2531        let err = writer
2532            .create_record(PublicCreateRequest {
2533                request_id: "req-1".into(),
2534                principal: "agent".into(),
2535                did: "did:web:tormentnexus.social".into(),
2536                collection: "app.bsky.feed.post".into(),
2537                rkey: Some(Tid::from_raw(1).into()),
2538                value: post(),
2539                expected_prev: None,
2540            })
2541            .expect_err("denied");
2542        assert!(err.to_string().contains("publication denied"));
2543    }
2544
2545    #[test]
2546    fn at9p_authority_cannot_be_exported_without_bridge() {
2547        let dir = tempfile::tempdir().expect("tempdir");
2548        let store = Arc::new(PublicRepoStore::open(dir.path()).expect("store"));
2549        let gate = Arc::new(Gate {
2550            allow: AtomicBool::new(true),
2551        });
2552        let key = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2553        assert!(PublicRepoWriter::new(store, "did:at9p:agent", key, gate).is_err());
2554    }
2555
2556    fn transaction_fixture() -> (
2557        tempfile::TempDir,
2558        Arc<PublicRepoStore>,
2559        Arc<Gate>,
2560        PublicRepoWriter,
2561    ) {
2562        let dir = tempfile::tempdir().expect("tempdir");
2563        let store = Arc::new(PublicRepoStore::open(dir.path()).expect("store"));
2564        let gate = Arc::new(Gate {
2565            allow: AtomicBool::new(true),
2566        });
2567        let key = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2568        let writer = PublicRepoWriter::new(
2569            store.clone(),
2570            "did:web:tormentnexus.social",
2571            key,
2572            gate.clone(),
2573        )
2574        .expect("writer");
2575        (dir, store, gate, writer)
2576    }
2577
2578    fn transaction_request(id: u64) -> PublicCreateRequest {
2579        PublicCreateRequest {
2580            request_id: format!("req-{id}"),
2581            principal: "did:at9p:agent".into(),
2582            did: "did:web:tormentnexus.social".into(),
2583            collection: "app.bsky.feed.post".into(),
2584            rkey: Some(Tid::from_raw(id).into()),
2585            value: post(),
2586            expected_prev: None,
2587        }
2588    }
2589
2590    #[test]
2591    fn general_keys_survive_restart_and_legacy_intents_remain_retriable() {
2592        let (dir, store, gate, writer) = transaction_fixture();
2593        let first = writer.create_record(transaction_request(7)).unwrap();
2594        // Model an intent written before generated_rkey existed.
2595        let mut legacy =
2596            serde_json::to_value(store.intent(writer.did(), "req-7").unwrap().unwrap()).unwrap();
2597        legacy.as_object_mut().unwrap().remove("generated_rkey");
2598        store
2599            .db
2600            .put(
2601                intent_key(writer.did(), "req-7"),
2602                serde_json::to_vec(&legacy).unwrap(),
2603            )
2604            .unwrap();
2605        let mut profile = transaction_request(8);
2606        profile.collection = "app.bsky.actor.profile".into();
2607        profile.rkey = Some(AtprotoRecordKey::new("self").unwrap());
2608        profile.value = DagCbor::str_map([("$type", DagCbor::Text(profile.collection.clone()))]);
2609        profile.expected_prev = Some(first.commit_cid);
2610        let second = writer.create_record(profile.clone()).unwrap();
2611        let mut general = transaction_request(9);
2612        general.collection = "com.example.record".into();
2613        general.rkey = Some(AtprotoRecordKey::new("literal:key~one").unwrap());
2614        general.value = DagCbor::str_map([("$type", DagCbor::Text(general.collection.clone()))]);
2615        general.expected_prev = Some(second.commit_cid);
2616        let third = writer.create_record(general.clone()).unwrap();
2617        let signing_key = writer.account.active_key.lock().as_ref().unwrap().clone();
2618        drop(writer);
2619        drop(store);
2620        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2621        let writer = PublicRepoWriter::new(
2622            store.clone(),
2623            "did:web:tormentnexus.social",
2624            signing_key,
2625            gate,
2626        )
2627        .unwrap();
2628        assert_eq!(writer.create_record(transaction_request(7)).unwrap(), first);
2629        assert_eq!(writer.create_record(profile).unwrap(), second);
2630        assert_eq!(writer.create_record(general).unwrap(), third);
2631        let snapshot = store.snapshot(writer.did()).unwrap().unwrap();
2632        assert_eq!(snapshot.records.len(), 3);
2633        assert!(snapshot.records.contains_key(&(
2634            "app.bsky.actor.profile".into(),
2635            AtprotoRecordKey::new("self").unwrap()
2636        )));
2637        assert!(snapshot.records.contains_key(&(
2638            "com.example.record".into(),
2639            AtprotoRecordKey::new("literal:key~one").unwrap()
2640        )));
2641        snapshot
2642            .commit
2643            .verify_atproto(&writer.active_verifying_key().unwrap())
2644            .unwrap();
2645    }
2646
2647    #[test]
2648    fn generated_key_retry_survives_account_key_promotion_and_reopen() {
2649        let (dir, store, gate, writer) = transaction_fixture();
2650        let mut request = transaction_request(7);
2651        request.rkey = None;
2652        let first = writer
2653            .create_record_with_expected_prev_text(request.clone(), None)
2654            .unwrap();
2655        let candidate = p256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng);
2656        let promoted = writer
2657            .promote_signing_key(
2658                "did:at9p:agent",
2659                &writer.active_verifying_key().unwrap(),
2660                candidate.clone(),
2661            )
2662            .unwrap()
2663            .unwrap();
2664        let mut later = transaction_request(8);
2665        later.rkey = None;
2666        writer
2667            .create_record_with_expected_prev_text(later, Some(&promoted.to_string()))
2668            .unwrap();
2669        assert_eq!(
2670            writer
2671                .create_record_with_expected_prev_text(request.clone(), None)
2672                .unwrap(),
2673            first
2674        );
2675        drop(writer);
2676        drop(store);
2677        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2678        let writer =
2679            PublicRepoWriter::new(store.clone(), &request.did, candidate.clone(), gate).unwrap();
2680        assert_eq!(
2681            writer
2682                .create_record_with_expected_prev_text(request, None)
2683                .unwrap(),
2684            first
2685        );
2686        let snapshot = store.snapshot(writer.did()).unwrap().unwrap();
2687        assert_eq!(snapshot.records.len(), 2);
2688        snapshot
2689            .commit
2690            .verify_atproto(candidate.verifying_key())
2691            .unwrap();
2692    }
2693
2694    #[test]
2695    fn generated_keys_recover_after_restart_and_bind_request_shape() {
2696        let (dir, store, gate, writer) = transaction_fixture();
2697        let mut request = transaction_request(7);
2698        request.rkey = None;
2699        let first = writer
2700            .create_record_with_expected_prev_text(request.clone(), None)
2701            .unwrap();
2702        let first_key = first.uri.rsplit('/').next().unwrap();
2703        assert!(Tid::parse(first_key).is_ok());
2704        let mut second_request = transaction_request(8);
2705        second_request.rkey = None;
2706        let second = writer
2707            .create_record_with_expected_prev_text(second_request, None)
2708            .unwrap();
2709        assert_ne!(first.uri, second.uri);
2710        let signing_key = writer.account.active_key.lock().as_ref().unwrap().clone();
2711        drop(writer);
2712        drop(store);
2713        let store = Arc::new(PublicRepoStore::open(dir.path()).unwrap());
2714        let writer = PublicRepoWriter::new(
2715            store.clone(),
2716            "did:web:tormentnexus.social",
2717            signing_key,
2718            gate.clone(),
2719        )
2720        .unwrap();
2721        assert_eq!(
2722            writer
2723                .create_record_with_expected_prev_text(request.clone(), None)
2724                .unwrap(),
2725            first
2726        );
2727        let mut explicit_retry = request.clone();
2728        explicit_retry.rkey = Some(AtprotoRecordKey::new(first_key).unwrap());
2729        assert!(writer
2730            .create_record_with_expected_prev_text(explicit_retry, None)
2731            .unwrap_err()
2732            .to_string()
2733            .contains("key mode"));
2734        let mut changed = request.clone();
2735        changed.value = DagCbor::str_map([
2736            ("$type", DagCbor::Text(changed.collection.clone())),
2737            ("text", DagCbor::Text("different".into())),
2738        ]);
2739        assert!(writer
2740            .create_record_with_expected_prev_text(changed, None)
2741            .is_err());
2742        let mut other_principal = request.clone();
2743        other_principal.principal = "did:at9p:other".into();
2744        assert!(writer
2745            .create_record_with_expected_prev_text(other_principal, None)
2746            .is_err());
2747        let explicit = transaction_request(9);
2748        writer
2749            .create_record_with_expected_prev_text(explicit.clone(), None)
2750            .unwrap();
2751        let mut omitted = explicit;
2752        omitted.rkey = None;
2753        assert!(writer
2754            .create_record_with_expected_prev_text(omitted, None)
2755            .unwrap_err()
2756            .to_string()
2757            .contains("key mode"));
2758        gate.allow.store(false, Ordering::Release);
2759        assert!(writer
2760            .create_record_with_expected_prev_text(request, None)
2761            .unwrap_err()
2762            .to_string()
2763            .contains("publication denied"));
2764        assert_eq!(
2765            store.snapshot(writer.did()).unwrap().unwrap().records.len(),
2766            3
2767        );
2768    }
2769
2770    #[test]
2771    fn generated_keys_are_unique_or_recovered_under_concurrent_requests() {
2772        for same_request in [false, true] {
2773            let (_dir, store, _gate, writer) = transaction_fixture();
2774            let writer = Arc::new(writer);
2775            let start = Arc::new(std::sync::Barrier::new(2));
2776            let results = std::thread::scope(|scope| {
2777                let mut workers = Vec::new();
2778                for id in [7, if same_request { 7 } else { 8 }] {
2779                    let writer = writer.clone();
2780                    let start = start.clone();
2781                    workers.push(scope.spawn(move || {
2782                        let mut request = transaction_request(id);
2783                        request.rkey = None;
2784                        start.wait();
2785                        writer
2786                            .create_record_with_expected_prev_text(request, None)
2787                            .unwrap()
2788                    }));
2789                }
2790                workers
2791                    .into_iter()
2792                    .map(|worker| worker.join().unwrap())
2793                    .collect::<Vec<_>>()
2794            });
2795            assert_eq!(results[0] == results[1], same_request);
2796            let expected = if same_request { 1 } else { 2 };
2797            assert_eq!(
2798                store.snapshot(writer.did()).unwrap().unwrap().records.len(),
2799                expected
2800            );
2801        }
2802    }
2803
2804    #[test]
2805    fn generated_key_skips_collisions_after_a_durable_future_revision() {
2806        let previous = Tid::from_raw(1 << 62);
2807        let collection = "app.bsky.feed.post";
2808        let mut keyed = BTreeMap::new();
2809        for offset in [1, 2] {
2810            keyed.insert(
2811                format!(
2812                    "{collection}/{}",
2813                    Tid::from_raw(previous.to_raw() + offset).encode()
2814                ),
2815                Cid::from_dag_cbor(b"occupied"),
2816            );
2817        }
2818        let key = allocate_record_key(collection, &keyed, Some(previous)).unwrap();
2819        assert_eq!(key.as_str(), Tid::from_raw(previous.to_raw() + 3).encode());
2820    }
2821
2822    #[test]
2823    fn xrpc_omitted_swap_allows_subsequent_creates_without_weakening_native_cas() {
2824        let (_dir, store, _gate, writer) = transaction_fixture();
2825        let first = writer
2826            .create_record_with_expected_prev_text(transaction_request(7), None)
2827            .expect("genesis");
2828        let native_none = writer
2829            .create_record(transaction_request(8))
2830            .expect_err("native genesis only");
2831        assert!(native_none.to_string().contains("CAS conflict"));
2832        let second = writer
2833            .create_record_with_expected_prev_text(transaction_request(8), None)
2834            .expect("unconditional second write");
2835        let mut third = transaction_request(9);
2836        third.expected_prev = Some(first.commit_cid);
2837        assert!(
2838            writer.create_record(third.clone()).is_err(),
2839            "native stale CAS"
2840        );
2841        third.expected_prev = Some(second.commit_cid);
2842        writer.create_record(third).expect("native exact CAS");
2843        assert_eq!(
2844            store.snapshot(writer.did()).unwrap().unwrap().records.len(),
2845            3
2846        );
2847    }
2848
2849    #[test]
2850    fn publication_capacity_rejects_before_commit_and_keeps_existing_retries_valid() {
2851        for payload_bytes in [0, 60_000] {
2852            let (_dir, store, _gate, writer) = transaction_fixture();
2853            let mut last = None;
2854            for id in 1..=MAX_PUBLIC_SNAPSHOT_RECORDS + 1 {
2855                let mut request = transaction_request(id as u64);
2856                request.value = DagCbor::str_map([
2857                    ("$type", DagCbor::Text(request.collection.clone())),
2858                    ("text", DagCbor::Text("x".repeat(payload_bytes))),
2859                ]);
2860                let result = writer.create_record_with_expected_prev_text(request.clone(), None);
2861                if let Ok(result) = result {
2862                    last = Some((request, result));
2863                } else {
2864                    assert!(matches!(
2865                        result,
2866                        Err(PublicRepoWriteError::InvalidRequest(_))
2867                    ));
2868                    let (request, original) = last.as_ref().unwrap();
2869                    let snapshot = store.snapshot(writer.did()).unwrap().unwrap();
2870                    assert_eq!(snapshot.records.len(), id - 1);
2871                    assert_eq!(snapshot.commit.cid_atproto().unwrap(), original.commit_cid);
2872                    assert_eq!(
2873                        writer
2874                            .create_record_with_expected_prev_text(request.clone(), None)
2875                            .unwrap(),
2876                        *original
2877                    );
2878                    if payload_bytes == 0 {
2879                        assert_eq!(snapshot.records.len(), MAX_PUBLIC_SNAPSHOT_RECORDS);
2880                    } else {
2881                        assert!(snapshot.records.len() < MAX_PUBLIC_SNAPSHOT_RECORDS);
2882                    }
2883                    break;
2884                }
2885                assert!(id <= MAX_PUBLIC_SNAPSHOT_RECORDS);
2886            }
2887        }
2888    }
2889
2890    #[test]
2891    fn snapshot_budget_fails_before_decoding_over_limit_records() {
2892        for (count, bytes) in [
2893            (MAX_PUBLIC_SNAPSHOT_RECORDS + 1, 1),
2894            (18, 60_000),
2895            (1, MAX_PUBLIC_RECORD_BYTES + 1),
2896        ] {
2897            let (_dir, store, _gate, writer) = transaction_fixture();
2898            store
2899                .insert_snapshot_budget_fixture_for_test(writer.did(), count, bytes)
2900                .unwrap();
2901            // Last record is intentionally invalid; budget wins before decode.
2902            let error = store.snapshot(writer.did()).unwrap_err().to_string();
2903            assert!(error.contains("budget"), "{error}");
2904            assert!(writer
2905                .public_snapshot()
2906                .unwrap_err()
2907                .to_string()
2908                .contains("budget"));
2909        }
2910    }
2911
2912    #[test]
2913    fn retries_bind_original_condition_under_race_and_legacy_receipts_fail_closed() {
2914        let (_dir, store, _gate, writer) = transaction_fixture();
2915        let first = writer
2916            .create_record_with_expected_prev_text(transaction_request(7), None)
2917            .unwrap();
2918        let previous = first.commit_cid.to_string();
2919        let barrier = std::sync::Barrier::new(2);
2920        let outcomes = std::thread::scope(|scope| {
2921            let workers: Vec<_> = [None, Some(previous.as_str())]
2922                .into_iter()
2923                .map(|condition| {
2924                    let writer = &writer;
2925                    let barrier = &barrier;
2926                    scope.spawn(move || {
2927                        barrier.wait();
2928                        (
2929                            condition,
2930                            writer.create_record_with_expected_prev_text(
2931                                transaction_request(8),
2932                                condition,
2933                            ),
2934                        )
2935                    })
2936                })
2937                .collect();
2938            workers
2939                .into_iter()
2940                .map(|worker| worker.join().unwrap())
2941                .collect::<Vec<_>>()
2942        });
2943        assert_eq!(
2944            outcomes.iter().filter(|(_, result)| result.is_ok()).count(),
2945            1
2946        );
2947        assert!(outcomes
2948            .iter()
2949            .any(|(_, result)| matches!(result, Err(PublicRepoWriteError::InvalidRequest(_)))));
2950        let (condition, result) = outcomes.iter().find(|(_, result)| result.is_ok()).unwrap();
2951        assert_eq!(
2952            &writer
2953                .create_record_with_expected_prev_text(transaction_request(8), *condition)
2954                .unwrap(),
2955            result.as_ref().unwrap()
2956        );
2957        assert_eq!(
2958            store.snapshot(writer.did()).unwrap().unwrap().records.len(),
2959            2
2960        );
2961        let mut legacy =
2962            serde_json::to_value(store.intent(writer.did(), "req-8").unwrap().unwrap()).unwrap();
2963        legacy.as_object_mut().unwrap().remove("head_condition");
2964        store
2965            .db
2966            .put(
2967                intent_key(writer.did(), "req-8"),
2968                serde_json::to_vec(&legacy).unwrap(),
2969            )
2970            .unwrap();
2971        assert!(matches!(
2972            writer.create_record_with_expected_prev_text(transaction_request(8), *condition),
2973            Err(PublicRepoWriteError::InvalidRequest(_))
2974        ));
2975    }
2976
2977    #[test]
2978    fn native_publication_rejects_unverified_blob_extensions_without_mutation() {
2979        let (_dir, store, _gate, writer) = transaction_fixture();
2980        let mut request = transaction_request(7);
2981        request.value = DagCbor::str_map([
2982            ("$type", DagCbor::Text(request.collection.clone())),
2983            (
2984                "extension",
2985                DagCbor::List(vec![DagCbor::str_map([(
2986                    "$type",
2987                    DagCbor::Text("blob".into()),
2988                )])]),
2989            ),
2990        ]);
2991        assert!(matches!(
2992            writer.create_record(request),
2993            Err(PublicRepoWriteError::InvalidRequest(_))
2994        ));
2995        assert!(store.snapshot(writer.did()).unwrap().is_none());
2996    }
2997
2998    #[test]
2999    fn xrpc_retries_return_original_result_after_later_commit_and_reopen() {
3000        let (dir, store, gate, writer) = transaction_fixture();
3001        let first = writer
3002            .create_record_with_expected_prev_text(transaction_request(7), None)
3003            .expect("genesis");
3004        let previous = first.commit_cid.to_string();
3005        let second = writer
3006            .create_record_with_expected_prev_text(transaction_request(8), Some(&previous))
3007            .expect("conditional second write");
3008        assert_eq!(
3009            writer
3010                .create_record_with_expected_prev_text(transaction_request(8), Some(&previous))
3011                .unwrap(),
3012            second,
3013            "immediate retry must not compare against its own new head"
3014        );
3015        let third = writer
3016            .create_record_with_expected_prev_text(transaction_request(9), None)
3017            .expect("head advances again");
3018        let key = writer.account.active_key.lock().as_ref().unwrap().clone();
3019        drop(writer);
3020        drop(store);
3021        let store = Arc::new(PublicRepoStore::open(dir.path()).expect("reopen"));
3022        let writer = PublicRepoWriter::new(
3023            store.clone(),
3024            "did:web:tormentnexus.social",
3025            key,
3026            gate.clone(),
3027        )
3028        .unwrap();
3029        assert_eq!(
3030            writer
3031                .create_record_with_expected_prev_text(transaction_request(7), None)
3032                .unwrap(),
3033            first
3034        );
3035        assert_eq!(
3036            writer
3037                .create_record_with_expected_prev_text(transaction_request(8), Some(&previous))
3038                .unwrap(),
3039            second,
3040            "durable retry returns original commit, not the latest head"
3041        );
3042        for changed in [None, Some(third.commit_cid.to_string().as_str())] {
3043            assert!(matches!(
3044                writer.create_record_with_expected_prev_text(transaction_request(8), changed),
3045                Err(PublicRepoWriteError::InvalidRequest(_))
3046            ));
3047        }
3048        let mut changed = transaction_request(8);
3049        changed.value = DagCbor::str_map([
3050            ("$type", DagCbor::Text("app.bsky.feed.post".into())),
3051            ("text", DagCbor::Text("changed".into())),
3052        ]);
3053        assert!(writer
3054            .create_record_with_expected_prev_text(changed, Some(&previous))
3055            .is_err());
3056        let mut different_principal = transaction_request(8);
3057        different_principal.principal = "did:at9p:other".into();
3058        assert!(writer
3059            .create_record_with_expected_prev_text(different_principal, Some(&previous))
3060            .is_err());
3061        gate.allow.store(false, Ordering::Release);
3062        let denied = writer
3063            .create_record_with_expected_prev_text(transaction_request(8), Some(&previous))
3064            .unwrap_err();
3065        assert!(
3066            denied.to_string().contains("publication denied"),
3067            "retries reauthorize"
3068        );
3069        let snapshot = store.snapshot(writer.did()).unwrap().unwrap();
3070        assert_eq!(snapshot.records.len(), 3);
3071        assert_eq!(snapshot.commit.cid_atproto().unwrap(), third.commit_cid);
3072    }
3073
3074    #[test]
3075    fn xrpc_cas_and_unconditional_writes_share_one_atomic_head_selection() {
3076        for conditional in [true, false] {
3077            let (_dir, store, _gate, writer) = transaction_fixture();
3078            let first = writer.create_record(transaction_request(7)).unwrap();
3079            let writer = Arc::new(writer);
3080            let start = Arc::new(std::sync::Barrier::new(2));
3081            let results = std::thread::scope(|scope| {
3082                let mut workers = Vec::new();
3083                for id in [8, 9] {
3084                    let writer = writer.clone();
3085                    let start = start.clone();
3086                    let expected = conditional.then(|| first.commit_cid.to_string());
3087                    workers.push(scope.spawn(move || {
3088                        start.wait();
3089                        writer.create_record_with_expected_prev_text(
3090                            transaction_request(id),
3091                            expected.as_deref(),
3092                        )
3093                    }));
3094                }
3095                workers
3096                    .into_iter()
3097                    .map(|worker| worker.join().expect("writer thread"))
3098                    .collect::<Vec<_>>()
3099            });
3100            let expected_successes = if conditional { 1 } else { 2 };
3101            assert_eq!(
3102                results.iter().filter(|result| result.is_ok()).count(),
3103                expected_successes
3104            );
3105            for error in results.iter().filter_map(|result| result.as_ref().err()) {
3106                assert!(error.to_string().contains("CAS conflict"));
3107            }
3108            let snapshot = store.snapshot(writer.did()).unwrap().unwrap();
3109            assert_eq!(snapshot.records.len(), 1 + expected_successes);
3110            snapshot
3111                .commit
3112                .verify_atproto(&writer.active_verifying_key().unwrap())
3113                .unwrap();
3114        }
3115    }
3116
3117    #[test]
3118    fn xrpc_swap_authorizes_before_reading_repository_state() {
3119        let (_dir, store, gate, writer) = transaction_fixture();
3120        // A read would fail decoding this record before reaching the gate in
3121        // the old text adapter. A denied request must not inspect it at all.
3122        let key = record_key(
3123            writer.did(),
3124            "app.bsky.feed.post",
3125            Tid::from_raw(7).encode().as_str(),
3126        );
3127        store.db.put(&key, b"malformed record").unwrap();
3128        gate.allow.store(false, Ordering::Release);
3129        let expected = Cid::from_dag_cbor(b"any head").to_string();
3130        let error = writer
3131            .create_record_with_expected_prev_text(transaction_request(8), Some(&expected))
3132            .unwrap_err();
3133        assert!(error.to_string().contains("publication denied"));
3134        assert_eq!(store.db.get(key).unwrap().unwrap(), b"malformed record");
3135        assert!(store.db.get(commit_key(writer.did())).unwrap().is_none());
3136        assert!(store.intent(writer.did(), "req-8").unwrap().is_none());
3137    }
3138
3139    #[test]
3140    fn json_links_and_bytes_match_independent_public_record_fixture() {
3141        // Independently encoded using Python hashlib/base64 and a minimal
3142        // RFC 8949 encoder with public map ordering and tag-42 links.
3143        let json = serde_json::json!({
3144            "$type": "app.bsky.feed.post",
3145            "text": "json",
3146            "payload": {"$bytes": "AQI="},
3147            "links": [{"$link": "bafyreifqwkmiw256ojf2zws6tzjeonw6bpd5vza4i22ccpcq4hjv2ts7cm"}],
3148            "blob": {
3149                "$type": "blob",
3150                "ref": {"$link": "bafkreifbfby75yqq7odbski6v2qziwa4xustdzfsg5m5ejpwqbush5rsei"},
3151                "mimeType": "image/png",
3152                "size": 2
3153            }
3154        });
3155        let value = json_to_dag_cbor(&json).expect("AT JSON");
3156        let record = AtprotoRecord::new("app.bsky.feed.post", Tid::from_raw(7), value)
3157            .expect("public record");
3158        let fixture = hex::decode(concat!(
3159            "a564626c6f62a463726566d82a58250001551220a12871fee210fb8619291eaea194581cbd2531e4b23759d225f6806923f63222",
3160            "6473697a650265247479706564626c6f62686d696d655479706569696d6167652f706e676474657874646a736f6e",
3161            "652474797065726170702e62736b792e666565642e706f7374656c696e6b7381d82a58250001711220b0b2988b6bbe724bacda5e9e524736de0bc7dae41c46b4213c50e1d35d4e5f13",
3162            "677061796c6f6164420102"
3163        )).expect("fixture");
3164        assert_eq!(record.bytes(), fixture);
3165        assert_eq!(
3166            record.cid().to_string(),
3167            "bafyreie3irn4tnywq3hxjm3knxuq7ovhni7cjqxa7weo7dncxsw6rj3s4m"
3168        );
3169        assert!(
3170            AtprotoRecord::from_bytes("app.bsky.feed.post", Tid::from_raw(7), &fixture).is_ok()
3171        );
3172    }
3173
3174    #[test]
3175    fn json_bytes_accept_standard_base64_with_optional_padding() {
3176        for text in ["", "AQI", "AQI=", "+/8=", "+/8"] {
3177            let value = json_to_dag_cbor(&serde_json::json!({"$bytes": text})).expect("bytes");
3178            let expected = match text {
3179                "" => vec![],
3180                "AQI" | "AQI=" => vec![1, 2],
3181                _ => vec![251, 255],
3182            };
3183            assert_eq!(value, DagCbor::Bytes(expected));
3184        }
3185    }
3186
3187    #[test]
3188    fn json_rejects_malformed_reserved_wrappers_at_any_depth() {
3189        let cid = Cid::from_raw(b"blob").to_string();
3190        let malformed = [
3191            serde_json::json!({"$link": false}),
3192            serde_json::json!({"$link": "not-a-cid"}),
3193            serde_json::json!({"$link": cid, "extra": 1}),
3194            serde_json::json!({"$link": cid, "$bytes": "AQI="}),
3195            serde_json::json!({"$bytes": 12}),
3196            serde_json::json!({"$bytes": "AQI=", "extra": 1}),
3197            serde_json::json!({"$bytes": "-_8="}),
3198            serde_json::json!({"$bytes": "AQJ="}),
3199            serde_json::json!({"$bytes": "AQI=="}),
3200        ];
3201        for value in malformed {
3202            assert!(json_to_dag_cbor(&value).is_err(), "{value}");
3203            assert!(json_to_dag_cbor(&serde_json::json!({"nested": [value]})).is_err());
3204        }
3205    }
3206
3207    #[test]
3208    fn json_links_reject_noncanonical_and_unsupported_cids() {
3209        let cid = Cid::from_raw(b"blob");
3210        let text = cid.to_string();
3211        assert_eq!(parse_atproto_json_cid(&text).expect("canonical"), cid);
3212        assert!(parse_atproto_json_cid(&text.to_uppercase()).is_err());
3213        assert!(parse_atproto_json_cid(&format!("{text}=")).is_err());
3214        let mut noncanonical = text.into_bytes();
3215        let last = noncanonical.last_mut().expect("last digit");
3216        // The last base32 digit has two zero padding bits. Setting one leaves
3217        // the decoded CID bytes unchanged but must not be accepted.
3218        let alphabet = b"abcdefghijklmnopqrstuvwxyz234567";
3219        let position = alphabet.iter().position(|c| c == last).expect("base32");
3220        *last = alphabet[position + 1];
3221        assert!(parse_atproto_json_cid(&String::from_utf8(noncanonical).unwrap()).is_err());
3222        for (position, replacement) in [(1, 0x70), (2, 0x13), (3, 0x1f)] {
3223            let mut raw = cid.as_bytes().to_vec();
3224            raw[position] = replacement;
3225            // Encode invalid bytes independently of the production parser.
3226            let encoded = raw
3227                .iter()
3228                .flat_map(|byte| (0..8).rev().map(move |bit| (byte >> bit) & 1))
3229                .collect::<Vec<_>>();
3230            let mut text = String::from("b");
3231            for chunk in encoded.chunks(5) {
3232                let n = chunk.iter().fold(0u8, |n, bit| (n << 1) | bit) << (5 - chunk.len());
3233                text.push(alphabet[n as usize] as char);
3234            }
3235            assert!(parse_atproto_json_cid(&text).is_err());
3236        }
3237    }
3238}
3239
3240#[cfg(all(test, feature = "rocksdb"))]
3241pub(crate) mod hosted_tests;