feather-reader 0.4.5

A minimalist, atproto-native RSS/Atom reader in Rust — your feed subscriptions live in your own PDS.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
//! DPoP-bound `com.atproto.repo.*` calls against the user's PDS.
//!
//! The layer the 31-method reader surface is built on. It returns the same
//! [`RecordEntry`] / [`WriteResult`] / [`WriteOp`] types the existing
//! [`crate::atproto`] client does, so the typed wrappers above it (subscriptions,
//! folders, saved, read-state) transfer at cutover rather than being rewritten.
//!
//! Two properties that are not obvious from the call shapes:
//!
//! * **The PDS comes from the session's `aud`**, never re-derived per call. It
//!   is a property of the token set — a token is valid at one PDS — and it is
//!   AAD-bound in the session row precisely so it cannot be repointed.
//! * **Every request carries both the proof and the token.** A resource request
//!   is `Authorization: DPoP <token>` plus a `DPoP` proof whose `ath` binds to
//!   that token; either alone is useless.

use anyhow::{Context as _, Result};
use reqwest::Client;
use serde_json::{json, Value};
use sqlx::SqlitePool;

use crate::atproto::{RecordEntry, WriteOp, WriteResult};

use super::dpop::Endpoint;
use super::keys::SigningKey;
use super::request::{self, DpopBody, DpopRequest, Retry};
use super::store::OAuthSession;

/// Cap on how many pages `list_all_records` will walk.
///
/// The same bound the existing client uses: a repo is user-controlled, and an
/// unbounded walk is a denial-of-service against ourselves.
const MAX_LIST_PAGES: usize = 50;

/// Hard cap on records accumulated by one walk — 50 pages x the 100 we request.
/// See [`crate::atproto::extend_bounded`] for why exceeding it is an error
/// rather than a truncation.
const MAX_LIST_RECORDS: usize = 5_000;

/// An authenticated handle on one account's repo.
pub struct Repo<'a> {
    pub http: &'a Client,
    pub pool: &'a SqlitePool,
    pub session: &'a OAuthSession,
    /// The session's DPoP key, already unsealed.
    pub key: &'a SigningKey,
}

impl Repo<'_> {
    /// The XRPC endpoint for a method, on THIS session's PDS.
    fn url(&self, nsid: &str) -> String {
        format!("{}/xrpc/{nsid}", self.session.aud.trim_end_matches('/'))
    }

    /// Send, and fail loudly on a non-2xx with the XRPC error if there is one.
    async fn send_raw(
        &self,
        url: &str,
        body: DpopBody<'_>,
        nsid: &str,
    ) -> Result<request::PostOutcome> {
        let outcome = request::send_with_dpop(
            self.http,
            self.pool,
            &DpopRequest {
                endpoint: Endpoint::ResourceServer,
                url,
                key: self.key,
                access_token: Some(&self.session.access_token),
                body,
                // A repo call is repeated on a nonce challenge, on the
                // ASSUMPTION that a PDS answering with a challenge has not acted
                // on the request. That is how conformant servers behave and what
                // the reference relies on, but it is not a guarantee we can
                // verify: a PDS or intermediary that emitted `use_dpop_nonce`
                // after a write committed would yield a duplicate record from
                // `createRecord` or `applyWrites`, both of which are
                // non-idempotent here.
                retry: Retry::Allowed,
            },
        )
        .await?;

        if !outcome.is_success() {
            // **The rejection as a value, under the same sentence as before.**
            // The sidecar client already surfaces an `AtProtoError::Xrpc`; this
            // one only said it in a string, so a caller that has to tell "the
            // PDS refused this" from "the network broke" — the read-state
            // reconcile (#241) — could match on nothing sturdier than wording.
            // The context keeps `Display` byte-for-byte what it was.
            let rejection = Self::rejection(&outcome.body, outcome.status);
            return Err(anyhow::Error::new(rejection).context(format!(
                "{nsid} failed: {}",
                xrpc_error(&outcome.body, outcome.status)
            )));
        }
        Ok(outcome)
    }

    /// A non-2xx answer as the [`crate::atproto::AtProtoError::Xrpc`] the
    /// sidecar client produces for the same answer, so one matcher serves both.
    ///
    /// The same length bound as [`Self::error_fields`] before any parse, and the
    /// same `"Unknown"` fallback `atproto::xrpc_error_from` uses for a body that
    /// is not an error document.
    fn rejection(body: &[u8], status: u16) -> crate::atproto::AtProtoError {
        #[derive(serde::Deserialize)]
        struct Fields {
            error: Option<String>,
            message: Option<String>,
        }
        let fields = super::error_body_worth_parsing(body)
            .then(|| serde_json::from_slice::<Fields>(body).ok())
            .flatten();
        let (error, message) = match fields {
            Some(Fields {
                error: Some(error),
                message,
            }) => (error, message),
            _ => ("Unknown".to_string(), None),
        };
        crate::atproto::AtProtoError::Xrpc {
            status: reqwest::StatusCode::from_u16(status)
                // Unreachable: reqwest already parsed this status. Not a 500,
                // so it can never read as the reference PDS's mismatch.
                .unwrap_or(reqwest::StatusCode::BAD_GATEWAY),
            error,
            message,
        }
    }

    /// [`send_raw`](Self::send_raw), then the body as JSON.
    ///
    /// Every call that wants a `Value` goes through here; `listRecords` does
    /// not, because turning its body into a `Value` and then into a page
    /// materialises the records twice.
    async fn send(&self, url: &str, body: DpopBody<'_>, nsid: &str) -> Result<Value> {
        let outcome = self.send_raw(url, body, nsid).await?;
        // A 200 with an empty body is legitimate for deleteRecord/applyWrites.
        if outcome.body.is_empty() {
            return Ok(Value::Null);
        }
        outcome.json()
    }

    /// Render an XRPC error body for a message.
    ///
    /// Only ever called on a NON-success, where the body is an error document
    /// rather than a token or a record — and even then only the `error` and
    /// `message` fields, never the raw bytes.
    ///
    /// **Bounded by length before it is parsed.** This is the error twin of
    /// `send`, whose success branch goes through `PostOutcome::json` and its node
    /// guard — so without this a hostile PDS answering **500** instead of 200 got
    /// the whole amplification the guard exists to stop, on every write and on
    /// every listing failure. An error document is a few dozen bytes; see
    /// [`super::MAX_ERROR_BODY`].
    fn error_fields(body: &[u8]) -> Option<String> {
        if !super::error_body_worth_parsing(body) {
            return None;
        }
        let value: Value = serde_json::from_slice(body).ok()?;
        let kind = value.get("error").and_then(Value::as_str)?;
        match value.get("message").and_then(Value::as_str) {
            Some(message) => Some(format!("{kind}: {message}")),
            None => Some(kind.to_string()),
        }
    }

    /// One page of a collection.
    pub async fn list_records(
        &self,
        collection: &str,
        limit: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<(Vec<RecordEntry>, Option<String>)> {
        let page = self.list_records_page(collection, limit, cursor).await?;
        Ok((page.records, page.cursor))
    }

    /// [`Self::list_records`] with the whole page, including how many records
    /// were malformed and left out (#177) — which the tuple form cannot carry.
    pub(crate) async fn list_records_page(
        &self,
        collection: &str,
        limit: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<crate::atproto::ListRecordsResponse> {
        let mut url = url::Url::parse(&self.url("com.atproto.repo.listRecords"))
            .context("building the listRecords URL")?;
        {
            let mut query = url.query_pairs_mut();
            query.append_pair("repo", &self.session.sub);
            query.append_pair("collection", collection);
            if let Some(limit) = limit {
                query.append_pair("limit", &limit.to_string());
            }
            if let Some(cursor) = cursor {
                query.append_pair("cursor", cursor);
            }
        }

        // **Raw bytes, parsed once.** This is the live backend's list walk, the
        // largest body this client reads, and routing it through `Value` first
        // held two copies of every page at the same time.
        let outcome = self
            .send_raw(
                url.as_str(),
                DpopBody::Query,
                "com.atproto.repo.listRecords",
            )
            .await?;
        // A 2xx carrying an error envelope is NOT an empty page: `records`
        // defaulting to `[]` turned a PDS failure into `Ok(vec![])`, which
        // `resolve_subscriptions` reads as "this DID follows nothing" and
        // `sync_sub_refs` then writes through, revoking every `sub_ref`.
        // Both invariants, through the one function every listRecords caller
        // shares — this check was added here first and had to be fitted to the
        // other two clients a round later. It reads bytes now rather than a
        // `Value`; the invariants are the same ones, expressed as fields.
        crate::atproto::parse_list_records(&outcome.body)
    }

    /// Every record in a collection, following the cursor.
    ///
    /// Bounded at `MAX_LIST_PAGES`: the repo is user-controlled, so an
    /// unbounded walk is a denial-of-service against ourselves. A cursor that
    /// does not advance also terminates the walk rather than spinning.
    pub async fn list_all_records(&self, collection: &str) -> Result<Vec<RecordEntry>> {
        self.list_all_records_within(
            collection,
            &mut crate::atproto::ByteBudget::new(crate::atproto::MAX_LIST_BYTES),
        )
        .await
    }

    /// [`list_all_records`](Self::list_all_records) against a caller's budget.
    pub(crate) async fn list_all_records_within(
        &self,
        collection: &str,
        budget: &mut crate::atproto::ByteBudget,
    ) -> Result<Vec<RecordEntry>> {
        let mut out = Vec::new();
        let max_bytes = budget.max();
        let mut cursor: Option<String> = None;
        let mut more_offered = false;

        for _ in 0..MAX_LIST_PAGES {
            let listed = self
                .list_records_page(collection, Some(100), cursor.as_deref())
                .await?;
            // This walk is the live backend's, and its result reaches
            // `replace_sub_refs`: a skipped record is a dropped subscription.
            crate::atproto::refuse_malformed(&listed, collection)?;
            let (page, next) = (listed.records, listed.cursor);
            let got = page.len();
            // Same cap, same reason as `atproto::extend_bounded`: MAX_LIST_PAGES
            // bounds requests, not memory, unless the server honours our limit.
            // This is the LIVE walk on `backend=rust` — its result reaches
            // `replace_sub_refs`, so a truncation here is revoked access.
            if !budget.admit(&page) {
                anyhow::bail!(
                    "listRecords for {collection} exceeded the {max_bytes}-byte cap \
                     ({} held, {} bytes charged) — refusing to accumulate further",
                    out.len(),
                    budget.used(),
                );
            }
            crate::atproto::extend_bounded(&mut out, page, MAX_LIST_RECORDS, collection)?;
            match next {
                // `got > 0` is not defensive tidiness -- it is a whole round
                // trip. This project's own PDS returns a cursor ALONGSIDE a
                // short page, so without it every list fetches a second, empty
                // page before stopping. Both of the other clients have this
                // guard; measured, its absence was most of the remaining gap
                // against the sidecar.
                //
                // The cursor-repeat check is the separate concern: a server that
                // hands back the same cursor forever would otherwise loop.
                Some(next) if got > 0 && Some(&next) != cursor.as_ref() => {
                    cursor = Some(next);
                    more_offered = true;
                }
                _ => {
                    // `break` with the flag cleared, rather than an early
                    // `return`: an early return makes the post-loop check
                    // unreachable, so the flag reads as dead state and a
                    // reviewer hunts for the case that clears it. It also left
                    // the "every walk refuses" mutation alive here.
                    more_offered = false;
                    break;
                }
            }
        }
        // **Running out of pages is a refusal, not a short answer.** Falling out
        // of the loop used to return `Ok(out)`, so a repo bigger than the page
        // budget produced a truncated list indistinguishable from a complete
        // one — and `resolve_subscriptions` needs an `Err` for its fail-closed
        // branch. Given `Ok`, it hands the short list to `replace_sub_refs`,
        // which DELETEs the reader's whole `sub_ref` projection and reinserts
        // only what it was given. `extend_bounded` cannot catch this either:
        // `MAX_LIST_PAGES` x the 100 we request is `MAX_LIST_RECORDS`, so against
        // a server that honours our limit the page budget runs out first.
        //
        // **The cap is on REQUESTS, though, so the record count it bites at is
        // the server's page size x the budget — not a number our constants fix.**
        // A PDS answering 50 a page reaches half as far; one answering more than
        // asked trips `extend_bounded` instead. And the last allowed page is a
        // FALSE refusal: terminating costs one extra request when a short page
        // still carries a cursor, so a walk holding every record it will ever
        // hold still refuses on a cursor it never followed.
        //
        // This client's caps are a QUARTER of the direct client's (50 pages,
        // 5 000 records), so with `limit=100` honoured the same reader refuses
        // here at ~4 900 records and works to ~19 900 on the sidecar. `Saved`
        // walks this too, one record per starred article, where 4 900 is a
        // plausible number for a real reader.
        if more_offered {
            anyhow::bail!(
                "listRecords for {collection} did not finish within {MAX_LIST_PAGES} pages \
                 ({} held, and the PDS still offered more) — refusing a short list",
                out.len(),
            );
        }
        Ok(out)
    }

    /// Create a record, letting the PDS assign the key.
    pub async fn create_record<T: crate::vetted::WritableRecord>(
        &self,
        collection: &str,
        record: &T,
    ) -> Result<WriteResult> {
        self.write(
            "com.atproto.repo.createRecord",
            json!({ "repo": self.session.sub, "collection": collection, "record": record }),
        )
        .await
    }

    /// Create or replace a record at a known key.
    pub async fn put_record<T: crate::vetted::WritableRecord>(
        &self,
        collection: &str,
        rkey: &str,
        record: &T,
    ) -> Result<WriteResult> {
        self.write(
            "com.atproto.repo.putRecord",
            json!({
                "repo": self.session.sub,
                "collection": collection,
                "rkey": rkey,
                "record": record,
            }),
        )
        .await
    }

    pub async fn delete_record(&self, collection: &str, rkey: &str) -> Result<()> {
        let body = json!({ "repo": self.session.sub, "collection": collection, "rkey": rkey });
        self.send(
            &self.url("com.atproto.repo.deleteRecord"),
            DpopBody::Json(serde_json::to_vec(&body)?),
            "com.atproto.repo.deleteRecord",
        )
        .await
        .and_then(|v| crate::atproto::reject_error_envelope(&v))?;
        Ok(())
    }

    /// A batch of writes, in as few round trips as the PDS's limits allow.
    ///
    /// Chunked by `crate::atproto::apply_writes_chunked`, which says what a
    /// failure part-way means: the batch is atomic per CALL, not as a whole.
    pub async fn apply_writes(&self, writes: &[WriteOp]) -> Result<()> {
        crate::atproto::apply_writes_chunked(writes, |chunk| self.apply_writes_once(chunk)).await
    }

    /// One `applyWrites` call, unchunked. Private: an oversized call is a
    /// refused call, so nothing reaches this except through
    /// [`apply_writes`](Self::apply_writes).
    async fn apply_writes_once(&self, writes: &[WriteOp]) -> Result<()> {
        let ops: Vec<Value> = writes.iter().map(WriteOp::to_json).collect();
        let body = json!({ "repo": self.session.sub, "writes": ops });
        self.send(
            &self.url("com.atproto.repo.applyWrites"),
            DpopBody::Json(serde_json::to_vec(&body)?),
            "com.atproto.repo.applyWrites",
        )
        .await
        .and_then(|v| crate::atproto::reject_error_envelope(&v))?;
        Ok(())
    }

    async fn write(&self, nsid: &str, body: Value) -> Result<WriteResult> {
        let value = self
            .send(
                &self.url(nsid),
                DpopBody::Json(serde_json::to_vec(&body)?),
                nsid,
            )
            .await?;
        serde_json::from_value(value).with_context(|| format!("{nsid} returned no usable result"))
    }
}

/// Format an XRPC failure for an error message.
fn xrpc_error(body: &[u8], status: u16) -> String {
    match Repo::error_fields(body) {
        Some(detail) => format!("status {status} ({detail})"),
        None => format!("status {status}"),
    }
}

/// The reader's typed surface, over [`Repo`].
///
/// Deliberately thin: each method is one repo call plus a parse, and the
/// orderings come from [`crate::lexicon::sort`], SHARED with the sidecar client
/// so the two cannot disagree across the cutover. A divergence there would not
/// be subtle — it would reorder the user's feed list the moment the
/// implementation swapped.
impl Repo<'_> {
    /// List a collection and parse each record into `T`, paired with its rkey.
    ///
    /// An unparseable record is SKIPPED with a warning rather than failing the
    /// list. Records are written by other clients and by future versions of this
    /// one; one record this build cannot read must not black out the whole feed
    /// list.
    async fn list_typed<T: serde::de::DeserializeOwned>(
        &self,
        collection: &str,
    ) -> Result<Vec<(String, T)>> {
        let records = self.list_all_records(collection).await?;
        let mut out = Vec::with_capacity(records.len());
        for record in records {
            let rkey = record.rkey().unwrap_or_default().to_string();
            match record.parse::<T>() {
                Ok(value) => out.push((rkey, value)),
                Err(err) => tracing::warn!(
                    collection,
                    uri = %record.uri,
                    error = %err,
                    "skipping unparseable record in collection"
                ),
            }
        }
        Ok(out)
    }

    // ── subscriptions ────────────────────────────────────────────────────────

    pub async fn list_subscriptions(&self) -> Result<Vec<(String, crate::lexicon::Subscription)>> {
        self.list_typed(crate::lexicon::nsid::SUBSCRIPTION).await
    }

    /// Every subscription, in the reader's deterministic order.
    pub async fn list_subscriptions_sorted(
        &self,
    ) -> Result<Vec<(String, crate::lexicon::Subscription)>> {
        let mut subs = self.list_subscriptions().await?;
        subs.sort_by(crate::lexicon::sort::subscriptions);
        Ok(subs)
    }

    /// Subscribe to a feed. Returns the new record's rkey so the caller can
    /// address it (rename, delete) without re-listing.
    pub async fn add_subscription(
        &self,
        sub: &crate::vetted::VettedSubscription,
    ) -> Result<String> {
        Ok(self
            .create_record(crate::lexicon::nsid::SUBSCRIPTION, sub)
            .await?
            .into_rkey())
    }

    pub async fn remove_subscription(&self, rkey: &str) -> Result<()> {
        self.delete_record(crate::lexicon::nsid::SUBSCRIPTION, rkey)
            .await
    }

    /// Replace a subscription in place — retitle, refile, change cadence.
    /// Returns the [`WriteResult`] rather than discarding it: the caller logs
    /// the resulting record URI, and a `()` here would have silently dropped
    /// that field from the log line after the cutover.
    pub async fn update_subscription(
        &self,
        rkey: &str,
        sub: &crate::vetted::VettedSubscription,
    ) -> Result<WriteResult> {
        self.put_record(crate::lexicon::nsid::SUBSCRIPTION, rkey, sub)
            .await
    }

    /// Batch-add many subscriptions via `applyWrites` (chunked) — the OPML-import path.
    ///
    /// One round trip per 200 feeds rather than one per feed: an import of
    /// several hundred feeds is the case this exists for. More than one call
    /// means the import can part-land; on an error,
    /// [`crate::atproto::ApplyWritesIncomplete::of`] gives `landed`, and the
    /// first `landed` of `subs` are in the repo.
    /// Returns the new rkeys, which are assigned HERE rather than by the server:
    /// client-side TIDs keep the imported feeds in input order and make the
    /// batch reproducible. The sidecar client does the same, with the same
    /// generator.
    pub async fn add_subscriptions_bulk(
        &self,
        subs: &[crate::vetted::VettedSubscription],
    ) -> Result<Vec<String>> {
        let mut gen = crate::atproto::TidGenerator::new();
        let mut rkeys = Vec::with_capacity(subs.len());
        let mut writes = Vec::with_capacity(subs.len());
        for sub in subs {
            let rkey = gen.next();
            writes.push(WriteOp::Create {
                collection: crate::lexicon::nsid::SUBSCRIPTION.to_string(),
                rkey: Some(rkey.clone()),
                // Propagated, NOT defaulted: `unwrap_or(Value::Null)` here would
                // write a null record into the user's repo on a serialization
                // failure rather than failing the import.
                value: serde_json::to_value(sub)?,
            });
            rkeys.push(rkey);
        }
        self.apply_writes(&writes).await?;
        Ok(rkeys)
    }

    // ── folders ──────────────────────────────────────────────────────────────

    pub async fn list_folders(&self) -> Result<Vec<(String, crate::lexicon::Folder)>> {
        self.list_typed(crate::lexicon::nsid::FOLDER).await
    }

    pub async fn list_folders_sorted(&self) -> Result<Vec<(String, crate::lexicon::Folder)>> {
        let mut folders = self.list_folders().await?;
        folders.sort_by(crate::lexicon::sort::folders);
        Ok(folders)
    }

    pub async fn add_folder(&self, folder: &crate::lexicon::Folder) -> Result<String> {
        Ok(self
            .create_record(crate::lexicon::nsid::FOLDER, folder)
            .await?
            .into_rkey())
    }

    /// Delete a folder. Subscriptions referencing it are left alone; a dangling
    /// reference reads as "unfiled", which is the same behaviour the sidecar
    /// client has.
    pub async fn remove_folder(&self, rkey: &str) -> Result<()> {
        self.delete_record(crate::lexicon::nsid::FOLDER, rkey).await
    }

    /// Returns the [`WriteResult`], matching the sidecar client — the caller
    /// logs the record URI from it.
    pub async fn rename_folder(
        &self,
        rkey: &str,
        folder: &crate::lexicon::Folder,
    ) -> Result<WriteResult> {
        self.put_record(crate::lexicon::nsid::FOLDER, rkey, folder)
            .await
    }

    // ── saved ────────────────────────────────────────────────────────────────

    pub async fn list_saved(&self) -> Result<Vec<(String, crate::lexicon::Saved)>> {
        self.list_typed(crate::lexicon::nsid::SAVED).await
    }

    /// Saved entries, newest first.
    pub async fn list_saved_sorted(&self) -> Result<Vec<(String, crate::lexicon::Saved)>> {
        let mut saved = self.list_saved().await?;
        saved.sort_by(crate::lexicon::sort::saved);
        Ok(saved)
    }

    pub async fn add_saved(&self, saved: &crate::vetted::VettedSaved) -> Result<String> {
        Ok(self
            .create_record(crate::lexicon::nsid::SAVED, saved)
            .await?
            .into_rkey())
    }

    pub async fn remove_saved(&self, rkey: &str) -> Result<()> {
        self.delete_record(crate::lexicon::nsid::SAVED, rkey).await
    }

    // ── read state ───────────────────────────────────────────────────────────

    pub async fn list_read_states(&self) -> Result<Vec<(String, crate::lexicon::ReadState)>> {
        self.list_typed(crate::lexicon::nsid::READ_STATE).await
    }

    /// Upsert one read cursor at its feed-derived rkey.
    pub async fn put_read_state(
        &self,
        rkey: &str,
        state: &crate::lexicon::ReadState,
    ) -> Result<()> {
        self.put_record(crate::lexicon::nsid::READ_STATE, rkey, state)
            .await?;
        Ok(())
    }

    /// Flush many dirty read cursors via `applyWrites` (chunked).
    ///
    /// Read state changes on nearly every page view, so this is the hottest
    /// write path in the app; one round trip per flush rather than per feed is
    /// the whole point. A flush past the op or byte bound is several calls,
    /// and a failure part-way leaves the first
    /// [`landed`](crate::atproto::ApplyWritesIncomplete::landed) cursors
    /// written — see `crate::atproto::apply_writes_chunked`.
    /// The `bool` is whether the record already exists in the PDS. It is not
    /// optional bookkeeping: an `#update` on a missing record ERRORS, and
    /// `applyWrites` is atomic per repo, so one not-yet-created cursor in the
    /// batch would drop the whole DID's flush. The op builder is shared with the
    /// sidecar client so the two cannot decide create-vs-update differently.
    pub async fn flush_read_states(
        &self,
        cursors: &[(String, crate::lexicon::ReadState, bool)],
    ) -> Result<()> {
        if cursors.is_empty() {
            return Ok(());
        }
        let writes = crate::atproto::read_state_write_ops(cursors)?;
        self.apply_writes(&writes).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// **Both clients must build the SAME write ops.**
    ///
    /// `flush_read_states` is the hottest write path in the app, and the
    /// create-vs-update choice is the one part of it that cannot be got wrong
    /// quietly: an `#update` on a record that does not exist errors, and
    /// `applyWrites` is atomic per repo, so a single first-flush cursor in the
    /// batch takes the whole DID's flush down with it.
    ///
    /// The first version of this method here ignored the flag and emitted
    /// `#create` unconditionally, which would have broken every feed's first
    /// flush after the cutover. Sharing the builder is what makes that
    /// impossible rather than merely fixed.
    #[test]
    fn read_state_writes_choose_create_or_update_per_cursor() {
        let state = crate::lexicon::ReadState::new(
            "https://example.com/feed",
            Some("2026-01-01T00:00:00Z".to_string()),
            "2026-01-01T00:00:00Z",
        );
        let cursors = vec![
            ("existing".to_string(), state.clone(), true),
            ("brand-new".to_string(), state.clone(), false),
        ];

        let ops = crate::atproto::read_state_write_ops(&cursors).expect("ops build");
        assert_eq!(ops.len(), 2);

        let rendered: Vec<Value> = ops.iter().map(|op| op.to_json()).collect();
        assert_eq!(
            rendered[0]["$type"], "com.atproto.repo.applyWrites#update",
            "an existing record must be UPDATED, not re-created"
        );
        assert_eq!(
            rendered[1]["$type"], "com.atproto.repo.applyWrites#create",
            "a first flush must CREATE, or the whole atomic batch fails"
        );
    }

    /// **Bulk import, through the real `Repo`, asserted on the bytes.** The
    /// test this replaces exercised `TidGenerator` directly and never called
    /// `add_subscriptions_bulk`; the function could stop assigning rkeys and
    /// return garbage with the suite green.
    #[tokio::test]
    async fn bulk_subscribe_writes_client_assigned_ordered_rkeys_to_the_right_collection() {
        let (base, log) = crate::net::tests::serve_json_capturing(b"{}".to_vec()).await;
        let port: u16 = base.rsplit(':').next().unwrap().parse().unwrap();
        crate::net::test_host_override(
            "bulk-pds.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://bulk-pds.test:{port}");
        let repo = repo(&http, &pool, &s, &key);
        let subs: Vec<crate::vetted::VettedSubscription> = (0..3)
            .map(|i| {
                crate::vetted::VettedSubscription::new(&crate::lexicon::Subscription::new(
                    format!("https://f{i}.example/feed.xml"),
                    "2026-07-12T00:00:00.000Z",
                ))
            })
            .collect();

        let rkeys = repo
            .add_subscriptions_bulk(&subs)
            .await
            .expect("bulk write failed");

        let sent = log.lock().unwrap().clone();
        assert_eq!(
            sent.len(),
            1,
            "expected one applyWrites request, got {sent:?}"
        );
        let body: Value = serde_json::from_str(sent[0].split("\r\n\r\n").nth(1).unwrap())
            .expect("request body is JSON");
        let writes = body["writes"].as_array().expect("writes array");
        assert_eq!(writes.len(), 3);
        for (i, w) in writes.iter().enumerate() {
            assert_eq!(w["collection"], crate::lexicon::nsid::SUBSCRIPTION);
            assert_eq!(w["rkey"].as_str(), Some(rkeys[i].as_str()));
        }
        let mut sorted = rkeys.clone();
        sorted.sort();
        assert_eq!(rkeys, sorted, "client-assigned rkeys must ascend");
    }

    fn session() -> OAuthSession {
        OAuthSession {
            sub: "did:plc:ewvi7nxzyoun6zhxrhs64oiz".into(),
            issuer: "https://pds.example.com".into(),
            aud: "https://pds.example.com".into(),
            dpop_key_jwk: "{}".into(),
            access_token: "tok".into(),
            refresh_token: "ref".into(),
            token_type: "DPoP".into(),
            granted_scope: "atproto".into(),
            expires_at: None,
        }
    }

    fn repo<'a>(
        http: &'a Client,
        pool: &'a SqlitePool,
        session: &'a OAuthSession,
        key: &'a SigningKey,
    ) -> Repo<'a> {
        Repo {
            http,
            pool,
            session,
            key,
        }
    }

    /// **The PDS comes from the session**, so a call cannot be pointed at
    /// another host by a caller who passes the wrong base.
    #[tokio::test]
    async fn endpoints_are_built_from_the_sessions_audience() {
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = "https://pds.example.com/".into();
        let repo = repo(&http, &pool, &s, &key);
        assert_eq!(
            repo.url("com.atproto.repo.listRecords"),
            "https://pds.example.com/xrpc/com.atproto.repo.listRecords",
            "a trailing slash on the audience must not double the separator"
        );
    }

    /// An XRPC error document is summarised by its `error`/`message` fields
    /// only — never by echoing the raw body, which on other paths holds tokens.
    #[test]
    fn an_xrpc_error_is_summarised_not_echoed() {
        let body = br#"{"error":"InvalidRequest","message":"unknown collection"}"#;
        let rendered = xrpc_error(body, 400);
        assert!(rendered.contains("InvalidRequest"));
        assert!(rendered.contains("unknown collection"));

        // A body that is not an XRPC error contributes nothing but the status.
        let opaque = xrpc_error(br#"{"access_token":"SECRET"}"#, 500);
        assert_eq!(opaque, "status 500");
        assert!(!opaque.contains("SECRET"));
        assert_eq!(xrpc_error(b"<html>oops</html>", 502), "status 502");
    }

    /// Every repo call must fail closed on an internal target, like every other
    /// outbound path — asserted on the guard's own error.
    #[tokio::test]
    async fn repo_calls_fail_closed_on_an_internal_pds() {
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        super::super::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = "http://127.0.0.1:2583".into();
        let repo = repo(&http, &pool, &s, &key);

        let err = repo
            .list_records("app.feather.subscription", None, None)
            .await
            .expect_err("must refuse a loopback PDS");
        assert!(
            format!("{err:#}").contains("forbidden (internal) address"),
            "failed for the wrong reason: {err:#}"
        );
    }

    /// **A 200 carrying an error envelope must not read as an empty repo.**
    ///
    /// `records` was taken off the JSON with `unwrap_or(Array([]))`, so a PDS
    /// answering `200 {"error": …}` produced `Ok(vec![])`. That is not the
    /// fail-closed branch in `web::resolve_subscriptions`: `sync_sub_refs`
    /// writes the empty set through and `replace_sub_refs` DELETEs the DID's
    /// entire `sub_ref` projection — one bad response revokes the reader's
    /// access to every feed they have. Driven through the real client against
    /// a real server, because the bug was the missing CALL, not the check.
    #[tokio::test]
    async fn a_200_error_envelope_is_not_an_empty_repo() {
        let base = crate::net::tests::serve_body(
            br#"{"error":"InvalidRequest","message":"bad cursor"}"#.to_vec(),
        )
        .await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "envelope-pds.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );

        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://envelope-pds.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        let err = repo
            .list_records("app.feather.subscription", None, None)
            .await
            .expect_err("an error envelope was read as an empty page");
        assert!(
            format!("{err:#}").contains("InvalidRequest"),
            "failed for the wrong reason: {err:#}"
        );
    }

    /// **The live walk spends its budget across pages too.**
    ///
    /// This is the `backend=rust` walk whose result reaches `replace_sub_refs`,
    /// so a bound that silently failed to accumulate here would revoke a
    /// reader's access to every feed past the cut. Three pages, a two-page
    /// budget: the walk must refuse, and must say it kept two.
    #[tokio::test]
    async fn the_live_walk_spends_its_budget_across_pages() {
        let (bodies, per_page) = crate::atproto::tests::paged_bodies(3, 4096, false);
        let base = crate::net::tests::serve_bodies_in_sequence(bodies).await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "live-budget-pages.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );

        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://live-budget-pages.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        let err = repo
            .list_all_records_within(
                "app.feather.subscription",
                &mut crate::atproto::ByteBudget::new(per_page * 2),
            )
            .await
            .expect_err("three pages cannot fit in a two-page budget");
        let msg = format!("{err:#}");
        assert!(msg.contains("byte cap"), "wrong bound reported: {msg}");
        assert!(
            msg.contains("2 held"),
            "the live walk did not accumulate across pages: {msg}"
        );
    }

    /// **The live walk refuses a short list.** Its result reaches
    /// `replace_sub_refs`, so returning a truncated list as a complete one
    /// deletes every subscription past the page budget.
    #[tokio::test]
    async fn the_live_walk_that_runs_out_of_pages_refuses() {
        let bodies: Vec<Vec<u8>> = (0..MAX_LIST_PAGES + 1)
            .map(|i| {
                serde_json::json!({
                    "records": [{ "uri": format!("at://did:plc:x/c/3lab{i}"), "value": {} }],
                    "cursor": format!("p{}", i + 1),
                })
                .to_string()
                .into_bytes()
            })
            .collect();
        let base = crate::net::tests::serve_bodies_in_sequence(bodies).await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "pages-exhausted-live.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://pages-exhausted-live.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        let err = repo
            .list_all_records("app.feather.subscription")
            .await
            .expect_err("a truncated list was returned as a complete one");
        assert!(
            format!("{err:#}").contains("did not finish"),
            "failed for the wrong reason: {err:#}"
        );
    }

    /// #177 on the live backend: a malformed record in the reader's own repo
    /// refuses the walk, by type, rather than dropping that subscription.
    #[tokio::test]
    async fn the_live_walk_refuses_a_page_with_a_malformed_record() {
        let body = serde_json::json!({ "records": [
            { "uri": "at://did:plc:x/c/3labGOOD", "value": {} },
            { "cid": "bafy", "value": {} },
        ]})
        .to_string()
        .into_bytes();
        let base = crate::net::tests::serve_bodies_in_sequence(vec![body]).await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "malformed-live.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://malformed-live.test:{port}");
        let repo = repo(&http, &pool, &s, &key);
        let err = repo
            .list_all_records("app.feather.subscription")
            .await
            .expect_err("a page with a malformed record was accepted");
        assert!(
            err.downcast_ref::<crate::atproto::MalformedRecords>()
                .is_some(),
            "refused for the wrong reason: {err:#}"
        );
    }

    /// The live walk's clean finish must still be a success — see the sidecar's
    /// twin for why this direction is the dangerous one.
    #[tokio::test]
    async fn the_live_walk_that_finishes_cleanly_returns_the_records() {
        let mut bodies: Vec<Vec<u8>> = (0..3)
            .map(|i| {
                serde_json::json!({
                    "records": [{ "uri": format!("at://did:plc:x/c/3lab{i}"), "value": {} }],
                    "cursor": format!("p{}", i + 1),
                })
                .to_string()
                .into_bytes()
            })
            .collect();
        // **The terminator CARRIES a record**, because a real PDS ends on a
        // partial page and those are the records an off-by-one loses. Ending on
        // an empty page kept "drop the last page's records" alive: the count
        // below was right either way.
        bodies.push(
            serde_json::json!({
                "records": [{ "uri": "at://did:plc:x/c/3labLAST", "value": {} }]
            })
            .to_string()
            .into_bytes(),
        );
        let base = crate::net::tests::serve_bodies_in_sequence(bodies).await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "clean-finish-live.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://clean-finish-live.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        let records = repo
            .list_all_records("app.feather.subscription")
            .await
            .expect("a walk that ran out of records is not a short list");
        assert_eq!(records.len(), 4);
        assert!(
            records.iter().any(|r| r.uri.ends_with("3labLAST")),
            "the LAST page's records were dropped: {:?}",
            records.iter().map(|r| r.uri.as_str()).collect::<Vec<_>>(),
        );
    }

    /// **The page cap is pinned exactly, not to within one.**
    ///
    /// `the_live_walk_that_runs_out_of_pages_refuses` serves `MAX_LIST_PAGES + 1`
    /// pages, so a budget one page SHORT refuses too and that mutation survives
    /// it. A walk whose last allowed request is the terminating one must come
    /// back `Ok` — and on this backend an `Err` is `replace_sub_refs` never
    /// running, which is the direction that costs a reader their subscriptions.
    #[tokio::test]
    async fn a_live_walk_that_terminates_on_its_last_allowed_page_succeeds() {
        let mut bodies: Vec<Vec<u8>> = (0..MAX_LIST_PAGES - 1)
            .map(|i| {
                serde_json::json!({
                    "records": [{ "uri": format!("at://did:plc:x/c/3lab{i}"), "value": {} }],
                    "cursor": format!("p{}", i + 1),
                })
                .to_string()
                .into_bytes()
            })
            .collect();
        bodies.push(
            serde_json::json!({
                "records": [{ "uri": "at://did:plc:x/c/3labLAST", "value": {} }]
            })
            .to_string()
            .into_bytes(),
        );
        assert_eq!(bodies.len(), MAX_LIST_PAGES);
        let base = crate::net::tests::serve_bodies_in_sequence(bodies).await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "last-allowed-page-live.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://last-allowed-page-live.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        let records = repo
            .list_all_records("app.feather.subscription")
            .await
            .expect("a walk that terminated inside its budget is not a short list");
        assert_eq!(
            records.len(),
            MAX_LIST_PAGES,
            "a walk that used its whole page budget and finished lost records",
        );
    }

    /// **The ERROR twin of `send`, which a review found unguarded.**
    ///
    /// `send`'s success branch goes through `PostOutcome::json` and its cap; its
    /// failure branch goes to `xrpc_error` → `error_fields`, which deserialised the
    /// whole body. So a hostile PDS answering **500** instead of 200 with the same
    /// explosion got the full amplification on every write and on every listing
    /// failure — the guard bypassed by a status code.
    ///
    /// Both directions: a small error body still yields its reason, because that
    /// string is what a reader's log needs to tell "your PDS said no" from "we
    /// broke".
    #[test]
    fn an_oversized_error_body_is_not_parsed_for_its_reason() {
        let small = br#"{"error":"InvalidSwap","message":"record changed"}"#;
        assert_eq!(
            Repo::error_fields(small).as_deref(),
            Some("InvalidSwap: record changed"),
            "a real error body must still render its reason",
        );

        let mut huge = String::from(r#"{"error":"InvalidSwap","pad":["#);
        while huge.len() < crate::oauth::MAX_ERROR_BODY + 1_024 {
            huge.push_str("{},");
        }
        huge.push_str("{}]}");
        assert!(huge.len() > crate::oauth::MAX_ERROR_BODY);
        assert_eq!(
            Repo::error_fields(huge.as_bytes()),
            None,
            "an oversized error body was deserialised to fish out one string",
        );
    }

    /// **A PDS rejection is structured, not only a sentence (#241).**
    ///
    /// The read-state flusher has to tell "the PDS refused this batch" from "the
    /// network broke", and the sidecar client already said so with
    /// [`crate::atproto::AtProtoError::Xrpc`]. This client only said it in a
    /// string, so the one backend production runs could be matched on nothing
    /// sturdier than its wording. The rendered message must not change: it is
    /// what every existing log line and assertion reads.
    #[tokio::test]
    async fn a_rejected_write_carries_the_status_and_error_name() {
        use axum::response::IntoResponse as _;
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let host = format!("rejecting-{}.xrpc.test", addr.port());
        crate::net::test_host_override(&host, addr);
        let app = axum::Router::new().fallback(|| async {
            (
                axum::http::StatusCode::INTERNAL_SERVER_ERROR,
                axum::Json(
                    json!({ "error": "InternalServerError", "message": "Internal Server Error" }),
                ),
            )
                .into_response()
        });
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });

        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://{host}:{}", addr.port());
        let repo = repo(&http, &pool, &s, &key);

        let err = repo
            .apply_writes(&[WriteOp::Delete {
                collection: crate::lexicon::nsid::READ_STATE.into(),
                rkey: "rs-0".into(),
            }])
            .await
            .expect_err("a 500 is a failure");
        assert_eq!(
            err.to_string(),
            "com.atproto.repo.applyWrites failed: status 500 \
             (InternalServerError: Internal Server Error)",
            "the rendered message changed",
        );
        let xrpc = err
            .chain()
            .find_map(|cause| cause.downcast_ref::<crate::atproto::AtProtoError>());
        match xrpc {
            Some(crate::atproto::AtProtoError::Xrpc { status, error, .. }) => {
                assert_eq!(status.as_u16(), 500);
                assert_eq!(error, "InternalServerError");
            }
            other => panic!("no structured XRPC error in the chain: {other:?}"),
        }
    }

    /// **The write path parses a PDS body too, and it had no bound before the
    /// parse.**
    ///
    /// `listRecords` is guarded by `parse_list_records`; every OTHER body this
    /// client turns into a `Value` goes through `PostOutcome::json` — the repo
    /// writers' responses, the PAR response, the token response, the session
    /// refresh. `read_capped` bounds the WIRE at 8 MB, which is the *input* to the
    /// amplification rather than a limit on it: 8 MB of the cheapest node shape
    /// measured 824 MB retained on a 512 MB box.
    ///
    /// Drives `delete_record`, which is the shortest route from a handler to
    /// `send` → `json`.
    #[tokio::test]
    async fn the_live_write_path_refuses_a_node_explosion() {
        let mut body = String::from(r#"{"uri":"at://d/c/r","value":["#);
        for _ in 0..1_200_000 {
            body.push_str("{},");
        }
        body.push_str("{}]}");
        assert!(
            crate::atproto::count_structural_chars(body.as_bytes())
                > crate::atproto::MAX_LIST_STRUCTURAL_CHARS,
            "the probe body is not over the cap, so this test proves nothing",
        );
        let base = crate::net::tests::serve_body(body.into_bytes()).await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "write-explosion.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://write-explosion.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        let err = repo
            .delete_record("c", "r")
            .await
            .expect_err("a node explosion on the write path was parsed rather than refused");
        assert!(
            format!("{err:#}").contains("structural characters"),
            "failed for the wrong reason: {err:#}"
        );
    }

    /// The other direction: an ordinary write response still parses. Without this
    /// a guard that refused every body would pass the test above.
    #[tokio::test]
    async fn the_live_write_path_accepts_an_ordinary_response() {
        let base =
            crate::net::tests::serve_body(br#"{"commit":{"cid":"bafy","rev":"3lab"}}"#.to_vec())
                .await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "write-ordinary.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://write-ordinary.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        repo.delete_record("c", "r")
            .await
            .expect("an ordinary write response was refused");
    }

    /// **A duplicated `records` key must not be able to empty a page.**
    ///
    /// This is the live `backend=rust` walk, so an empty page here reaches
    /// `replace_sub_refs` and deletes the reader's subscriptions. serde refuses
    /// a repeated field outright; a `serde_json::Value` takes the last one
    /// silently, so routing this body through a `Value` first turns a smuggled
    /// second key into a successful, empty listing. The test exists to pin
    /// which of the two this client uses.
    #[tokio::test]
    async fn the_live_walk_refuses_a_duplicated_records_key() {
        let base = crate::net::tests::serve_body(
            br#"{"records":[{"uri":"at://d/c/r","value":{}}],"records":[]}"#.to_vec(),
        )
        .await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "dup-records.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );

        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://dup-records.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        let err = repo
            .list_records("app.feather.subscription", None, None)
            .await
            .expect_err("a duplicated records key was read as an empty page");
        assert!(
            format!("{err:#}").contains("duplicate"),
            "failed for the wrong reason: {err:#}"
        );
    }

    /// **An empty 2xx body is not an empty repo either.** `send` maps a
    /// zero-length 2xx to `Value::Null` — deliberately, for `deleteRecord` and
    /// `applyWrites` — and while `listRecords` still went through it, that
    /// slipped past the envelope guard
    /// and `records.unwrap_or(Array([]))` produced `Ok(vec![])`: the same
    /// `sub_ref` wipe the guard was added to prevent, through the sibling door.
    #[tokio::test]
    async fn an_empty_200_body_is_not_an_empty_repo() {
        let base = crate::net::tests::serve_body(Vec::new()).await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "empty-body.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://empty-body.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        let err = repo
            .list_records("app.feather.subscription", None, None)
            .await
            .expect_err("an empty body was read as an empty repo");
        assert!(
            format!("{err:#}").contains("no records"),
            "failed for the wrong reason: {err:#}"
        );
    }

    /// The write paths discarded the body too: `delete_record` and
    /// `apply_writes` are `send(..).await?; Ok(())`, so a 200 carrying an
    /// error envelope reported success for a delete that did not happen.
    #[tokio::test]
    async fn a_200_error_envelope_is_not_a_successful_write() {
        let base = crate::net::tests::serve_body(
            br#"{"error":"InvalidRequest","message":"nope"}"#.to_vec(),
        )
        .await;
        let port: u16 = base
            .trim_end_matches('/')
            .rsplit(':')
            .next()
            .unwrap()
            .parse()
            .unwrap();
        crate::net::test_host_override(
            "envelope-write.test",
            std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        );
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        s.aud = format!("http://envelope-write.test:{port}");
        let repo = repo(&http, &pool, &s, &key);

        let err = repo
            .delete_record("app.feather.subscription", "rk1")
            .await
            .expect_err("a failed delete was reported as success");
        assert!(format!("{err:#}").contains("InvalidRequest"), "{err:#}");

        let err = repo
            .apply_writes(&[crate::atproto::WriteOp::Delete {
                collection: "app.feather.subscription".to_string(),
                rkey: "rk1".to_string(),
            }])
            .await
            .expect_err("a failed batch was reported as success");
        assert!(format!("{err:#}").contains("InvalidRequest"), "{err:#}");
    }

    /// An empty batch must not produce a request at all — an `applyWrites` with
    /// no writes is a round trip that can only fail.
    #[tokio::test]
    async fn an_empty_batch_is_not_sent() {
        let http = Client::new();
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        let key = SigningKey::generate("k");
        let mut s = session();
        // A target that would fail loudly if it were ever contacted.
        s.aud = "http://127.0.0.1:2583".into();
        let repo = repo(&http, &pool, &s, &key);
        assert!(repo.apply_writes(&[]).await.is_ok());
    }

    // ── applyWrites chunking (#240) ──────────────────────────────────────────
    //
    // This is the production backend, so the chunking is asserted here on the
    // bytes it sends to a fake PDS that refuses what the reference PDS refuses
    // (see `crate::atproto::tests::serve_apply_writes`).

    /// A session pointed at the strict fake, reached through a per-port host
    /// override (the override table is process-wide and tests run in parallel).
    async fn strict_pds(
        fail_call: Option<usize>,
    ) -> (
        OAuthSession,
        SqlitePool,
        crate::atproto::tests::ApplyWritesLog,
    ) {
        let (base, log) = crate::atproto::tests::serve_apply_writes(fail_call).await;
        let port: u16 = base.rsplit(':').next().unwrap().parse().unwrap();
        let host = format!("chunk-oauth-{port}.test");
        crate::net::test_host_override(&host, std::net::SocketAddr::from(([127, 0, 0, 1], port)));
        let pool = crate::store::init_url("sqlite::memory:").await.unwrap();
        crate::store::init_schema(&pool).await.unwrap();
        let mut s = session();
        s.aud = format!("http://{host}:{port}");
        (s, pool, log)
    }

    fn subs(n: usize) -> Vec<crate::vetted::VettedSubscription> {
        (0..n)
            .map(|i| {
                crate::vetted::VettedSubscription::new(&crate::lexicon::Subscription::new(
                    format!("https://f{i}.example/feed.xml"),
                    "2026-07-12T00:00:00.000Z",
                ))
            })
            .collect()
    }

    /// **An OPML import of 201 feeds is two calls, 200 then 1, in order** — on
    /// the backend production runs. One call of 201 is what the reference PDS
    /// refuses with `Too many writes. Max: 200`.
    #[tokio::test]
    async fn bulk_subscribe_of_201_is_two_calls_in_order() {
        let (s, pool, log) = strict_pds(None).await;
        let (http, key) = (Client::new(), SigningKey::generate("k"));
        let rkeys = repo(&http, &pool, &s, &key)
            .add_subscriptions_bulk(&subs(201))
            .await
            .expect("a 201-feed import must succeed against a PDS that caps at 200");
        assert_eq!(crate::atproto::tests::call_sizes(&log), vec![200, 1]);
        assert_eq!(
            crate::atproto::tests::sent_rkeys(&log),
            rkeys,
            "every feed, once, in input order"
        );
    }

    /// 500 feeds — the default per-DID cap — are three calls; exactly 200 is one.
    #[tokio::test]
    async fn bulk_subscribe_splits_at_200_and_not_before() {
        for (n, want) in [(500, vec![200, 200, 100]), (200, vec![200])] {
            let (s, pool, log) = strict_pds(None).await;
            let (http, key) = (Client::new(), SigningKey::generate("k"));
            repo(&http, &pool, &s, &key)
                .add_subscriptions_bulk(&subs(n))
                .await
                .expect("bulk write");
            assert_eq!(crate::atproto::tests::call_sizes(&log), want, "{n} feeds");
        }
    }

    /// **A failed chunk stops the run**, and chunk 3 is never sent.
    #[tokio::test]
    async fn bulk_subscribe_stops_at_the_first_failed_chunk() {
        let (s, pool, log) = strict_pds(Some(2)).await;
        let (http, key) = (Client::new(), SigningKey::generate("k"));
        let err = repo(&http, &pool, &s, &key)
            .add_subscriptions_bulk(&subs(500))
            .await
            .expect_err("a failed chunk must fail the call");
        assert_eq!(
            crate::atproto::tests::call_sizes(&log),
            vec![200, 200],
            "chunk 3 must NOT be sent"
        );
        assert!(format!("{err:#}").contains("boom"), "{err:#}");
    }

    /// **The byte bound splits a read-state flush well under 200 ops.** Ten
    /// cursors at the lexicon's 1,000-id cap are ~500 KB — one call of that is
    /// refused by every reference PDS older than atproto#4989.
    #[tokio::test]
    async fn read_state_flush_splits_on_bytes_under_200_ops() {
        let (s, pool, log) = strict_pds(None).await;
        let (http, key) = (Client::new(), SigningKey::generate("k"));
        let cursors: Vec<(String, crate::lexicon::ReadState, bool)> = (0..10)
            .map(|i| {
                let mut state = crate::lexicon::ReadState::new(
                    format!("https://f{i}.example/feed.xml"),
                    None,
                    "2026-07-12T00:00:00.000Z",
                );
                state.read_ids = (0..crate::lexicon::ReadState::MAX_IDS)
                    .map(|j| format!("https://f{i}.example/posts/{j:04}/an-entry-permalink"))
                    .collect();
                (format!("rk{i:04}"), state, false)
            })
            .collect();
        repo(&http, &pool, &s, &key)
            .flush_read_states(&cursors)
            .await
            .expect("a byte-heavy flush must succeed in chunks");
        let sizes = crate::atproto::tests::call_sizes(&log);
        assert!(sizes.len() > 1, "one call for ~500 KB: {sizes:?}");
        let want: Vec<String> = cursors.iter().map(|(rkey, _, _)| rkey.clone()).collect();
        assert_eq!(crate::atproto::tests::sent_rkeys(&log), want);
    }
}