solid-pod-rs-forge 0.5.0-alpha.6

Clean-room Rust Git forge (Gogs/Gitea slice) composed on solid-pod-rs primitives: pod-native issues/PRs, forge push tokens, hosted did:nostr bodies, and Blocktrails anchoring. Cites JavaScriptSolidServer `forge` behaviour by function name only; all code original.
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
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
//! # solid-pod-rs-forge
//!
//! A clean-room Rust reimplementation of the JavaScriptSolidServer
//! `forge` plugin — a zero-build, server-rendered Git forge (a
//! Gogs/Gitea slice) composed almost entirely from primitives
//! [`solid-pod-rs`](solid_pod_rs) already ships: git smart-HTTP
//! ([`solid_pod_rs_git`]), write-as-commit provenance and Bitcoin
//! anchoring ([`solid_pod_rs::provenance`], [`solid_pod_rs::mrc20`]),
//! NIP-98 auth ([`solid_pod_rs::auth::nip98`]), `did:nostr` identity
//! ([`solid_pod_rs::did_nostr_types`]), and WAC gating (enforced by the
//! embedding server before [`ForgeService::handle`] is reached).
//!
//! ## IP posture
//!
//! Everything here is derived from the forge plugin's *published
//! behaviour* and expressed as fresh Rust on solid-pod-rs's own
//! primitives. JSS is cited by function/section name only — no upstream
//! JavaScript is transcribed. This mirrors how [`solid_pod_rs::bitcoin_tx`]
//! and [`solid_pod_rs::auth::nip98`] cite JSS while carrying an
//! independent implementation.
//!
//! ## Architecture rule — "words in pods, metadata in the forge"
//!
//! Issue/PR/comment *bodies* live in the author's own pod (WAC-governed,
//! author-owned); the forge keeps only a **spine index** of pointers.
//! Bodies are re-fetched at read time, bounded. Podless `did:nostr`
//! agents store bodies in forge-hosted storage instead.
//!
//! ## Native-only
//!
//! The forge shells to `git`/`git-http-backend`, touches the
//! filesystem, and runs a loopback HTTP client. It is never part of a
//! `core`/wasm build and adds zero dependencies to the core crate.
//!
//! ## Entry point
//!
//! The crate is framework-agnostic: [`ForgeService::handle`] consumes a
//! [`ForgeRequest`] and produces a [`ForgeResponse`]; the embedding
//! server (actix/axum/hyper) translates its native types at the edge and
//! WAC-gates the forge scope *before* dispatch — exactly as the server's
//! `handle_git` gates before invoking [`solid_pod_rs_git::GitHttpService`].

#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]

pub mod auth;
pub mod bodies;
pub mod config;
pub mod error;
pub mod hosted;
pub mod html;
pub mod ownership;
pub mod repo;
pub mod request;
pub mod router;
pub mod spine;
pub mod token;

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use solid_pod_rs_git::service::{GitHttpService, GitRequest};

pub use bodies::{HostedReader, LoopbackFetch};
pub use config::ForgeConfig;
pub use error::ForgeError;
pub use hosted::HostedStore;
pub use ownership::ForgeAgent;
pub use request::{esc, ForgeRequest, ForgeResponse};
pub use router::{parse_route, Route};
pub use spine::{FsSpineStore, SpineStore};
pub use token::TokenError;

/// Convenience import surface.
pub mod prelude {
    pub use crate::config::ForgeConfig;
    pub use crate::error::ForgeError;
    pub use crate::ownership::ForgeAgent;
    pub use crate::request::{ForgeRequest, ForgeResponse};
    pub use crate::ForgeService;
}

/// Current wall-clock time in Unix seconds.
fn now_secs() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

/// Load the persisted forge instance HMAC key, or generate + persist a
/// fresh one (`0600`). The key is 32 bytes of OS randomness (two v4
/// UUIDs, each 122 bits of entropy from the platform RNG). Persisting it
/// keeps minted tokens valid across restarts.
fn load_or_create_token_key(plugin_dir: &Path) -> Result<[u8; 32], ForgeError> {
    let path = plugin_dir.join(".forge-token-key");
    if let Ok(bytes) = std::fs::read(&path) {
        if bytes.len() == 32 {
            let mut key = [0u8; 32];
            key.copy_from_slice(&bytes);
            return Ok(key);
        }
    }
    let mut key = [0u8; 32];
    key[..16].copy_from_slice(uuid::Uuid::new_v4().as_bytes());
    key[16..].copy_from_slice(uuid::Uuid::new_v4().as_bytes());
    std::fs::write(&path, key)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600));
    }
    Ok(key)
}

/// The forge service. One instance is built at server startup and shared
/// (`Arc`) across requests. All durable state lives under `plugin_dir`.
#[derive(Clone)]
pub struct ForgeService {
    cfg: ForgeConfig,
    plugin_dir: PathBuf,
    repo_root: PathBuf,
    git: Arc<GitHttpService>,
    spine: Arc<dyn SpineStore>,
    /// Loopback GET for verifying/re-fetching pod-hosted bodies. `None`
    /// until the server injects its reqwest-backed client; without it,
    /// pod-hosted issue writes are refused (fail-closed).
    loopback: Option<Arc<dyn LoopbackFetch>>,
    /// Forge-hosted body store for podless `did:nostr` agents (always
    /// available — it is local `0600` storage under the plugin dir).
    hosted: Arc<HostedStore>,
    /// Forge instance HMAC key for push tokens (persisted `0600`).
    token_key: Arc<[u8; 32]>,
}

impl std::fmt::Debug for ForgeService {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ForgeService")
            .field("prefix", &self.cfg.normalized_prefix())
            .field("plugin_dir", &self.plugin_dir)
            .finish()
    }
}

impl ForgeService {
    /// Build a forge rooted at `plugin_dir`. Creates the on-disk layout
    /// (`repos/`, `issues/`, `pulls/`, `hosted/`, `marks/`) if absent.
    /// The git CGI service is rooted at `plugin_dir/repos`.
    pub fn new(cfg: ForgeConfig, plugin_dir: impl Into<PathBuf>) -> Result<Self, ForgeError> {
        let plugin_dir = plugin_dir.into();
        let repo_root = plugin_dir.join("repos");
        for sub in ["repos", "issues", "pulls", "hosted", "marks"] {
            std::fs::create_dir_all(plugin_dir.join(sub))?;
        }
        let git = Arc::new(GitHttpService::new(repo_root.clone()));
        let spine: Arc<dyn SpineStore> = Arc::new(FsSpineStore::new(plugin_dir.clone()));
        let hosted = Arc::new(HostedStore::new(plugin_dir.clone()));
        let token_key = Arc::new(load_or_create_token_key(&plugin_dir)?);
        Ok(Self {
            cfg,
            plugin_dir,
            repo_root,
            git,
            spine,
            loopback: None,
            hosted,
            token_key,
        })
    }

    /// Inject the loopback fetcher (the server's reqwest client) used to
    /// verify and re-fetch pod-hosted bodies.
    #[must_use]
    pub fn with_loopback(mut self, lb: Arc<dyn LoopbackFetch>) -> Self {
        self.loopback = Some(lb);
        self
    }

    /// Override the spine store (tests inject an in-memory store).
    #[must_use]
    pub fn with_spine(mut self, spine: Arc<dyn SpineStore>) -> Self {
        self.spine = spine;
        self
    }

    /// The forge instance HMAC key (for token minting/verification).
    #[must_use]
    pub fn token_key(&self) -> &[u8; 32] {
        &self.token_key
    }

    /// Resolve the caller identity from `req`'s `Authorization` header
    /// (forge token or NIP-98). The server may instead pass a
    /// [`ForgeAgent::Pod`] directly to [`Self::handle`] when it has a pod
    /// session; this helper is for the token/NIP-98 schemes.
    #[must_use]
    pub fn resolve_agent(&self, req: &ForgeRequest) -> ForgeAgent {
        auth::resolve_agent(req, self.token_key.as_ref(), now_secs())
    }

    /// The configured, normalised URL prefix (`/forge`).
    #[must_use]
    pub fn prefix(&self) -> String {
        self.cfg.normalized_prefix()
    }

    /// The plugin data directory.
    #[must_use]
    pub fn plugin_dir(&self) -> &Path {
        &self.plugin_dir
    }

    /// Single entry point. The embedding server maps its native request
    /// to a [`ForgeRequest`], WAC-gates the scope, and calls this.
    pub async fn handle(
        &self,
        req: ForgeRequest,
        agent: ForgeAgent,
    ) -> Result<ForgeResponse, ForgeError> {
        // CORS preflight.
        if req.method.eq_ignore_ascii_case("OPTIONS") {
            return Ok(ForgeResponse {
                status: 204,
                headers: vec![
                    ("access-control-allow-origin".into(), "*".into()),
                    (
                        "access-control-allow-methods".into(),
                        "GET, POST, PUT, DELETE, OPTIONS".into(),
                    ),
                    (
                        "access-control-allow-headers".into(),
                        "Content-Type, Authorization".into(),
                    ),
                ],
                body: bytes::Bytes::new(),
            });
        }

        let prefix = self.cfg.normalized_prefix();
        let Some(rel) = router::strip_prefix(&prefix, &req.path) else {
            return Ok(ForgeResponse::error(404, "not under forge prefix"));
        };
        let route = parse_route(&rel);

        let result = self.dispatch(route, &req, &agent).await;
        Ok(result.unwrap_or_else(|e| e.to_response()))
    }

    /// Route → handler. Handlers return `Result` and errors are mapped to
    /// responses by the caller.
    async fn dispatch(
        &self,
        route: Route,
        req: &ForgeRequest,
        _agent: &ForgeAgent,
    ) -> Result<ForgeResponse, ForgeError> {
        match route {
            Route::Index => self.h_index().await,
            Route::OwnerIndex { owner } => self.h_owner(&owner).await,
            Route::GitSmart { rel_path } => self.h_git_smart(req, _agent, &rel_path).await,
            Route::RepoOverview { owner, repo } => self.h_overview(&owner, &repo).await,
            Route::Tree {
                owner,
                repo,
                rev,
                path,
            } => self.h_tree(&owner, &repo, &rev, &path).await,
            Route::Blob {
                owner,
                repo,
                rev,
                path,
            } => self.h_blob(&owner, &repo, &rev, &path).await,
            Route::Raw {
                owner,
                repo,
                rev,
                path,
            } => self.h_raw(&owner, &repo, &rev, &path).await,
            Route::Commits { owner, repo, rev } => self.h_commits(req, &owner, &repo, &rev).await,
            Route::Commit { owner, repo, sha } => self.h_commit(&owner, &repo, &sha).await,
            Route::Branches { owner, repo } => self.h_branches(&owner, &repo).await,
            Route::Tags { owner, repo } => self.h_tags(&owner, &repo).await,
            Route::Issues { owner, repo } => {
                if req.method.eq_ignore_ascii_case("POST") {
                    self.h_issue_create(req, _agent, &owner, &repo).await
                } else {
                    self.h_issues_list(req, &owner, &repo).await
                }
            }
            Route::IssueNew { owner, repo } => self.h_issue_new_form(&owner, &repo).await,
            Route::IssueDetail { owner, repo, num } => {
                if req.method.eq_ignore_ascii_case("POST") {
                    self.h_issue_comment(req, _agent, &owner, &repo, num).await
                } else {
                    self.h_issue_detail(&owner, &repo, num).await
                }
            }
            Route::ApiToken => self.h_api_token(req, _agent).await,
            Route::ApiHosted { hex, id } => self.h_api_hosted(req, _agent, &hex, &id).await,
            // The remaining routes are implemented in later phases.
            _ => Err(ForgeError::NotFound("not implemented".into())),
        }
    }

    /// Resolve `<owner>/<repo>` to its bare git-dir, 404ing when the repo
    /// does not exist. Validates both segments against traversal.
    async fn resolve_repo(&self, owner: &str, repo: &str) -> Result<PathBuf, ForgeError> {
        let dir = repo::repo_git_dir(&self.repo_root, owner, repo)?;
        if !tokio::fs::metadata(&dir)
            .await
            .map(|m| m.is_dir())
            .unwrap_or(false)
        {
            return Err(ForgeError::NotFound(format!("repo {owner}/{repo}")));
        }
        Ok(dir)
    }

    // ---- Tier 1: index + git smart-HTTP ---------------------------------

    async fn h_index(&self) -> Result<ForgeResponse, ForgeError> {
        let repos = repo::list_all(&self.repo_root).await;
        Ok(ForgeResponse::html(
            200,
            html::index_page(&self.prefix(), &repos),
        ))
    }

    async fn h_owner(&self, owner: &str) -> Result<ForgeResponse, ForgeError> {
        let repos = repo::list_owner(&self.repo_root, owner).await;
        if repos.is_empty() {
            return Err(ForgeError::NotFound(format!("owner {owner}")));
        }
        Ok(ForgeResponse::html(
            200,
            html::owner_page(&self.prefix(), owner, &repos),
        ))
    }

    /// Forward a git smart-HTTP request to the reused CGI service. The
    /// server has already WAC-gated the scope; auth for the CGI itself is
    /// the pod's existing `Basic nostr:`/NIP-98 path inside the git crate.
    async fn h_git_smart(
        &self,
        req: &ForgeRequest,
        agent: &ForgeAgent,
        rel_path: &str,
    ) -> Result<ForgeResponse, ForgeError> {
        let git_req = GitRequest {
            method: req.method.clone(),
            path: rel_path.to_string(),
            query: req.query.clone(),
            headers: req.headers.clone(),
            body: req.raw_body.clone(),
            host_url: req.host_url.clone(),
        };
        // Namespace push guard: a push (receive-pack) may only target the
        // caller's own namespace. Pushing into another owner's namespace
        // is `403` (JSS forge namespace guard). Reads are unaffected here;
        // the server's WAC gate governs private-repo read access.
        if git_req.is_write() {
            let owner = rel_path
                .trim_start_matches('/')
                .split('/')
                .next()
                .unwrap_or("");
            if !agent.can_write_namespace(owner) {
                return Err(ForgeError::Forbidden(format!(
                    "cannot push into namespace '{owner}'"
                )));
            }
        }
        let resp = self.git.handle(git_req).await?;
        Ok(ForgeResponse {
            status: resp.status,
            headers: resp.headers,
            body: resp.body,
        })
    }

    // ---- Tier 1: browse -------------------------------------------------

    async fn h_overview(&self, owner: &str, repo: &str) -> Result<ForgeResponse, ForgeError> {
        let dir = self.resolve_repo(owner, repo).await?;
        let prefix = self.prefix();
        if !repo::browse::has_commits(&dir).await {
            return Ok(ForgeResponse::html(
                200,
                html::repo_overview_page(&prefix, owner, repo, "main", &[], None),
            ));
        }
        let branch = repo::browse::default_branch(&dir).await;
        let entries = repo::browse::list_tree(&dir, &branch, "").await?;
        // README detection (case-insensitive common names).
        let readme = self.find_readme(&dir, &branch, &entries).await;
        Ok(ForgeResponse::html(
            200,
            html::repo_overview_page(&prefix, owner, repo, &branch, &entries, readme.as_deref()),
        ))
    }

    /// Read a bounded, textual README from the root tree if one exists.
    async fn find_readme(
        &self,
        dir: &Path,
        rev: &str,
        entries: &[repo::browse::TreeEntry],
    ) -> Option<String> {
        let candidate = entries.iter().find(|e| {
            e.kind == repo::browse::EntryKind::File && {
                let n = e.name.to_ascii_lowercase();
                n == "readme" || n == "readme.md" || n == "readme.txt"
            }
        })?;
        let bytes =
            repo::browse::read_blob(dir, rev, &candidate.name, self.cfg.max_body_bytes as u64)
                .await
                .ok()?;
        if request::looks_textual(&bytes) {
            Some(String::from_utf8_lossy(&bytes).into_owned())
        } else {
            None
        }
    }

    async fn h_tree(
        &self,
        owner: &str,
        repo: &str,
        rev: &str,
        path: &str,
    ) -> Result<ForgeResponse, ForgeError> {
        let dir = self.resolve_repo(owner, repo).await?;
        let entries = repo::browse::list_tree(&dir, rev, path).await?;
        Ok(ForgeResponse::html(
            200,
            html::tree_page(&self.prefix(), owner, repo, rev, path, &entries),
        ))
    }

    async fn h_blob(
        &self,
        owner: &str,
        repo: &str,
        rev: &str,
        path: &str,
    ) -> Result<ForgeResponse, ForgeError> {
        let dir = self.resolve_repo(owner, repo).await?;
        // Cap the in-browser render; oversized/binary blobs get a download
        // link instead of buffering into the page.
        const BLOB_VIEW_MAX: u64 = 1024 * 1024;
        let content = match repo::browse::read_blob(&dir, rev, path, BLOB_VIEW_MAX).await {
            Ok(bytes) => {
                if request::looks_textual(&bytes) {
                    Some(String::from_utf8_lossy(&bytes).into_owned())
                } else {
                    None
                }
            }
            // Too large to inline — offer the raw download.
            Err(ForgeError::BadRequest(_)) => None,
            Err(e) => return Err(e),
        };
        Ok(ForgeResponse::html(
            200,
            html::blob_page(&self.prefix(), owner, repo, rev, path, content.as_deref()),
        ))
    }

    async fn h_raw(
        &self,
        owner: &str,
        repo: &str,
        rev: &str,
        path: &str,
    ) -> Result<ForgeResponse, ForgeError> {
        let dir = self.resolve_repo(owner, repo).await?;
        // Generous cap for raw downloads; still bounded to avoid OOM.
        const RAW_MAX: u64 = 25 * 1024 * 1024;
        let bytes = repo::browse::read_blob(&dir, rev, path, RAW_MAX).await?;
        let is_text = request::looks_textual(&bytes);
        let filename = path.rsplit('/').next().unwrap_or("download");
        Ok(ForgeResponse::raw_bytes(
            bytes::Bytes::from(bytes),
            is_text,
            filename,
        ))
    }

    async fn h_commits(
        &self,
        req: &ForgeRequest,
        owner: &str,
        repo: &str,
        rev: &str,
    ) -> Result<ForgeResponse, ForgeError> {
        let dir = self.resolve_repo(owner, repo).await?;
        let page: u32 = req
            .query_param("page")
            .and_then(|p| p.parse().ok())
            .unwrap_or(1)
            .max(1);
        let (commits, has_next) = repo::browse::commit_log(&dir, rev, page, 50).await?;
        Ok(ForgeResponse::html(
            200,
            html::commits_page(&self.prefix(), owner, repo, rev, &commits, page, has_next),
        ))
    }

    async fn h_commit(
        &self,
        owner: &str,
        repo: &str,
        sha: &str,
    ) -> Result<ForgeResponse, ForgeError> {
        let dir = self.resolve_repo(owner, repo).await?;
        let meta = solid_pod_rs_git::api::resolve_commit(&dir, sha).await?;
        let patch = repo::browse::commit_patch(&dir, &meta.hash).await?;
        Ok(ForgeResponse::html(
            200,
            html::commit_page(&self.prefix(), owner, repo, &meta, &patch),
        ))
    }

    async fn h_branches(&self, owner: &str, repo: &str) -> Result<ForgeResponse, ForgeError> {
        let dir = self.resolve_repo(owner, repo).await?;
        let info = solid_pod_rs_git::api::git_branches(&dir).await?;
        Ok(ForgeResponse::html(
            200,
            html::refs_page(
                &self.prefix(),
                owner,
                repo,
                "Branches",
                Some(&info.current),
                &info.local,
                "commits",
            ),
        ))
    }

    async fn h_tags(&self, owner: &str, repo: &str) -> Result<ForgeResponse, ForgeError> {
        let dir = self.resolve_repo(owner, repo).await?;
        let tags = repo::browse::list_tags(&dir).await?;
        Ok(ForgeResponse::html(
            200,
            html::refs_page(&self.prefix(), owner, repo, "Tags", None, &tags, "tree"),
        ))
    }

    // ---- Tier 2: issues + spine ----------------------------------------

    async fn h_issues_list(
        &self,
        req: &ForgeRequest,
        owner: &str,
        repo: &str,
    ) -> Result<ForgeResponse, ForgeError> {
        self.resolve_repo(owner, repo).await?;
        let idx = spine::issues::load_issue_index(self.spine.as_ref(), owner, repo).await?;
        let filter = match req.query_param("state").as_deref() {
            Some("closed") => spine::issues::IssueState::Closed,
            _ => spine::issues::IssueState::Open,
        };
        let issues = idx.by_state(filter);
        Ok(ForgeResponse::html(
            200,
            html::issues_list_page(
                &self.prefix(),
                owner,
                repo,
                filter,
                idx.count(spine::issues::IssueState::Open),
                idx.count(spine::issues::IssueState::Closed),
                &issues,
            ),
        ))
    }

    async fn h_issue_new_form(&self, owner: &str, repo: &str) -> Result<ForgeResponse, ForgeError> {
        self.resolve_repo(owner, repo).await?;
        Ok(ForgeResponse::html(
            200,
            html::issue_new_page(&self.prefix(), owner, repo),
        ))
    }

    async fn h_issue_detail(
        &self,
        owner: &str,
        repo: &str,
        num: u64,
    ) -> Result<ForgeResponse, ForgeError> {
        self.resolve_repo(owner, repo).await?;
        let idx = spine::issues::load_issue_index(self.spine.as_ref(), owner, repo).await?;
        let entry = idx
            .issues
            .get(&num)
            .ok_or_else(|| ForgeError::NotFound(format!("issue #{num}")))?;
        let truncated = entry.thread.len() > self.cfg.thread_cap;
        let hosted: Arc<dyn HostedReader> = self.hosted.clone();
        let threads = bodies::render_thread(
            &entry.thread,
            self.loopback.clone(),
            Some(hosted),
            &self.cfg,
        )
        .await;
        Ok(ForgeResponse::html(
            200,
            html::issue_detail_page(&self.prefix(), owner, repo, entry, &threads, truncated),
        ))
    }

    /// Parse `{title?, resourceUrl?, body?}` from a JSON or form-encoded
    /// body. `resourceUrl` is the pod-hosted pointer (pod agents); `body`
    /// is the inline content (podless `did:nostr` agents).
    fn parse_issue_body(req: &ForgeRequest) -> (Option<String>, Option<String>, Option<String>) {
        let ct = req.header("content-type").unwrap_or("");
        let raw = String::from_utf8_lossy(&req.raw_body);
        let field_json = |v: &serde_json::Value, k: &str| {
            v.get(k).and_then(|x| x.as_str()).map(|s| s.to_string())
        };
        if ct.contains("application/json") {
            if let Ok(v) = serde_json::from_str::<serde_json::Value>(&raw) {
                return (
                    field_json(&v, "title"),
                    field_json(&v, "resourceUrl"),
                    field_json(&v, "body"),
                );
            }
        }
        let pairs = request::parse_form(&raw);
        let field = |k: &str| pairs.iter().find(|(kk, _)| kk == k).map(|(_, v)| v.clone());
        (field("title"), field("resourceUrl"), field("body"))
    }

    /// Build a verified [`ThreadPointer`] for `agent`'s contribution:
    /// - **Pod agent** → validate + loopback-verify the pod `resource_url`.
    /// - **Nostr agent** → store `body` inline in forge-hosted storage.
    /// - **Anonymous** → `401`.
    async fn make_pointer(
        &self,
        req: &ForgeRequest,
        agent: &ForgeAgent,
        owner: &str,
        repo: &str,
        resource_url: Option<&str>,
        body: Option<&str>,
    ) -> Result<spine::issues::ThreadPointer, ForgeError> {
        match agent {
            ForgeAgent::Anonymous => {
                Err(ForgeError::Unauthorised("authentication required".into()))
            }
            ForgeAgent::Nostr { pubkey_hex } => {
                // Podless: the body is submitted inline and hosted 0600.
                let body = body
                    .map(str::trim)
                    .filter(|b| !b.is_empty())
                    .ok_or_else(|| ForgeError::BadRequest("body required".into()))?;
                let rref = self.hosted.write(pubkey_hex, body.as_bytes()).await?;
                Ok(spine::issues::ThreadPointer {
                    author: agent.author_id(),
                    resource_url: rref,
                    at: now_secs(),
                    hosted: true,
                })
            }
            ForgeAgent::Pod { .. } => {
                let url = resource_url
                    .ok_or_else(|| ForgeError::BadRequest("resourceUrl required".into()))?;
                self.verify_pod_pointer(req, agent, owner, repo, url).await
            }
        }
    }

    /// Verify a pod-hosted body pointer: the URL must be in the caller's
    /// own forge area (SSRF guard) and unauthenticated-readable. Returns
    /// the validated `ThreadPointer` on success.
    async fn verify_pod_pointer(
        &self,
        req: &ForgeRequest,
        agent: &ForgeAgent,
        repo_owner: &str,
        repo: &str,
        resource_url: &str,
    ) -> Result<spine::issues::ThreadPointer, ForgeError> {
        let caller = agent
            .owner()
            .ok_or_else(|| ForgeError::Unauthorised("authentication required".into()))?;
        let host = req
            .host_url
            .as_deref()
            .ok_or_else(|| ForgeError::BadRequest("missing host".into()))?;
        // SSRF + own-area guard.
        bodies::own_area_ok(resource_url, host, caller, repo_owner, repo)?;
        // Confirm public readability with a single loopback GET.
        let lb = self
            .loopback
            .as_ref()
            .ok_or_else(|| ForgeError::Unsupported("pod body verification unavailable".into()))?;
        match lb
            .get(
                resource_url,
                self.cfg.max_body_bytes,
                self.cfg.fetch_timeout_secs,
            )
            .await
        {
            bodies::FetchResult::Body(_) => Ok(spine::issues::ThreadPointer {
                author: agent.author_id(),
                resource_url: resource_url.to_string(),
                at: now_secs(),
                hosted: false,
            }),
            bodies::FetchResult::Removed => Err(ForgeError::BadRequest(
                "pod body is not publicly readable".into(),
            )),
            bodies::FetchResult::TooLarge => {
                Err(ForgeError::BadRequest("pod body too large".into()))
            }
            bodies::FetchResult::Error(e) => {
                Err(ForgeError::Backend(format!("pod body fetch failed: {e}")))
            }
        }
    }

    async fn h_issue_create(
        &self,
        req: &ForgeRequest,
        agent: &ForgeAgent,
        owner: &str,
        repo: &str,
    ) -> Result<ForgeResponse, ForgeError> {
        self.resolve_repo(owner, repo).await?;
        let (title, resource_url, body) = Self::parse_issue_body(req);
        let title = title
            .map(|t| t.trim().to_string())
            .filter(|t| !t.is_empty())
            .ok_or_else(|| ForgeError::BadRequest("title required".into()))?;

        // Pod agents supply a pod pointer; podless nostr agents supply an
        // inline body stored in forge-hosted storage.
        let pointer = self
            .make_pointer(
                req,
                agent,
                owner,
                repo,
                resource_url.as_deref(),
                body.as_deref(),
            )
            .await?;

        let mut idx = spine::issues::load_issue_index(self.spine.as_ref(), owner, repo).await?;
        let entry = spine::issues::IssueEntry {
            number: 0,
            title,
            state: spine::issues::IssueState::Open,
            author: agent.author_id(),
            created_at: now_secs(),
            thread: vec![pointer],
        };
        let num = idx.allocate(entry);
        spine::issues::save_issue_index(self.spine.as_ref(), owner, repo, &idx).await?;

        let location = format!("{}/{}/{}/issues/{}", self.prefix(), owner, repo, num);
        Ok(ForgeResponse::redirect(303, &location))
    }

    async fn h_issue_comment(
        &self,
        req: &ForgeRequest,
        agent: &ForgeAgent,
        owner: &str,
        repo: &str,
        num: u64,
    ) -> Result<ForgeResponse, ForgeError> {
        self.resolve_repo(owner, repo).await?;
        let (_title, resource_url, body) = Self::parse_issue_body(req);
        let pointer = self
            .make_pointer(
                req,
                agent,
                owner,
                repo,
                resource_url.as_deref(),
                body.as_deref(),
            )
            .await?;

        let mut idx = spine::issues::load_issue_index(self.spine.as_ref(), owner, repo).await?;
        let entry = idx
            .issues
            .get_mut(&num)
            .ok_or_else(|| ForgeError::NotFound(format!("issue #{num}")))?;
        entry.thread.push(pointer);
        spine::issues::save_issue_index(self.spine.as_ref(), owner, repo, &idx).await?;

        let location = format!("{}/{}/{}/issues/{}", self.prefix(), owner, repo, num);
        Ok(ForgeResponse::redirect(303, &location))
    }

    // ---- Tier 2.5: tokens + hosted storage -----------------------------

    /// `POST /api/token` — mint a forge push token for the (already
    /// authenticated) caller. The server resolves the caller via NIP-98 /
    /// pod session before calling `handle`; an anonymous caller is `401`.
    async fn h_api_token(
        &self,
        req: &ForgeRequest,
        agent: &ForgeAgent,
    ) -> Result<ForgeResponse, ForgeError> {
        if !req.method.eq_ignore_ascii_case("POST") {
            return Err(ForgeError::NotFound("token requires POST".into()));
        }
        let iat = now_secs();
        let ttl = self.cfg.token_ttl_secs;
        let token = token::mint(self.token_key.as_ref(), agent, iat, ttl).ok_or_else(|| {
            ForgeError::Unauthorised("authentication required to mint a token".into())
        })?;
        let v = serde_json::json!({
            "token": token,
            "tokenType": "Bearer",
            "expiresIn": ttl,
            "exp": iat + ttl,
            "agent": agent.author_id(),
        });
        Ok(ForgeResponse::json(200, &v))
    }

    /// `GET/DELETE /api/hosted/<hex>/<uuid>` — the podless body store.
    /// GET is public (bodies are public by construction); DELETE is
    /// owner-only (`agent` hex must equal `hex`).
    async fn h_api_hosted(
        &self,
        req: &ForgeRequest,
        agent: &ForgeAgent,
        hex: &str,
        id: &str,
    ) -> Result<ForgeResponse, ForgeError> {
        if req.method.eq_ignore_ascii_case("DELETE") {
            // Only the owning nostr agent may delete their hosted body.
            if agent.owner() != Some(hex) {
                return Err(ForgeError::Forbidden(
                    "only the owner may delete a hosted body".into(),
                ));
            }
            let removed = self.hosted.delete(hex, id).await?;
            if removed {
                return Ok(ForgeResponse::with_type(
                    204,
                    "application/json",
                    bytes::Bytes::new(),
                ));
            }
            return Err(ForgeError::NotFound(format!("hosted {hex}/{id}")));
        }

        // GET (default).
        match self.hosted.read(hex, id, self.cfg.max_body_bytes).await? {
            Some(bytes) => Ok(ForgeResponse {
                status: 200,
                headers: vec![
                    ("content-type".into(), "application/json".into()),
                    ("x-content-type-options".into(), "nosniff".into()),
                    ("access-control-allow-origin".into(), "*".into()),
                ],
                body: bytes::Bytes::from(bytes),
            }),
            None => Err(ForgeError::NotFound(format!("hosted {hex}/{id}"))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use tempfile::TempDir;

    fn req(method: &str, path: &str) -> ForgeRequest {
        ForgeRequest {
            method: method.into(),
            path: path.into(),
            query: String::new(),
            headers: vec![],
            raw_body: Bytes::new(),
            host_url: Some("https://pod.example".into()),
        }
    }

    fn service() -> (TempDir, ForgeService) {
        let td = TempDir::new().unwrap();
        let svc = ForgeService::new(ForgeConfig::default(), td.path()).unwrap();
        (td, svc)
    }

    #[tokio::test]
    async fn new_creates_layout() {
        let (td, _svc) = service();
        for sub in ["repos", "issues", "pulls", "hosted", "marks"] {
            assert!(td.path().join(sub).is_dir(), "{sub} dir must exist");
        }
    }

    #[tokio::test]
    async fn index_renders_empty() {
        let (_td, svc) = service();
        let r = svc
            .handle(req("GET", "/forge"), ForgeAgent::Anonymous)
            .await
            .unwrap();
        assert_eq!(r.status, 200);
        let body = String::from_utf8(r.body.to_vec()).unwrap();
        assert!(body.contains("No repositories yet"));
    }

    #[tokio::test]
    async fn index_lists_pushed_repo() {
        let (td, svc) = service();
        tokio::fs::create_dir_all(td.path().join("repos/alice/demo.git"))
            .await
            .unwrap();
        let r = svc
            .handle(req("GET", "/forge"), ForgeAgent::Anonymous)
            .await
            .unwrap();
        let body = String::from_utf8(r.body.to_vec()).unwrap();
        assert!(body.contains("/forge/alice/demo"));

        // Owner page too.
        let r2 = svc
            .handle(req("GET", "/forge/alice"), ForgeAgent::Anonymous)
            .await
            .unwrap();
        assert_eq!(r2.status, 200);
    }

    #[tokio::test]
    async fn unknown_owner_is_404() {
        let (_td, svc) = service();
        let r = svc
            .handle(req("GET", "/forge/ghost"), ForgeAgent::Anonymous)
            .await
            .unwrap();
        assert_eq!(r.status, 404);
    }

    #[tokio::test]
    async fn options_preflight() {
        let (_td, svc) = service();
        let r = svc
            .handle(req("OPTIONS", "/forge/x"), ForgeAgent::Anonymous)
            .await
            .unwrap();
        assert_eq!(r.status, 204);
        assert!(r
            .headers
            .iter()
            .any(|(k, _)| k.eq_ignore_ascii_case("access-control-allow-methods")));
    }

    #[tokio::test]
    async fn outside_prefix_is_404() {
        let (_td, svc) = service();
        let r = svc
            .handle(req("GET", "/other/thing"), ForgeAgent::Anonymous)
            .await
            .unwrap();
        assert_eq!(r.status, 404);
    }

    #[tokio::test]
    async fn unimplemented_route_is_404_for_now() {
        let (_td, svc) = service();
        let r = svc
            .handle(
                req("GET", "/forge/alice/repo/issues"),
                ForgeAgent::Anonymous,
            )
            .await
            .unwrap();
        // Issues is implemented in Phase 2; before that it is a 404. Once
        // Phase 2 lands, an anonymous GET on a missing repo still 404s.
        assert_eq!(r.status, 404);
    }

    // ---- Phase 1 browse integration (needs the `git` binary) -----------

    fn git_available() -> bool {
        std::process::Command::new("git")
            .arg("--version")
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    fn run_git(dir: &std::path::Path, args: &[&str]) {
        let ok = std::process::Command::new("git")
            .args(args)
            .current_dir(dir)
            .env("GIT_CONFIG_NOSYSTEM", "1")
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false);
        assert!(ok, "git {args:?} failed in {}", dir.display());
    }

    /// Create `repos/<owner>/<name>.git` as a bare repo containing a
    /// README, a `src/lib.rs`, and a `v1.0` tag, via a working clone.
    fn seed_repo(td: &TempDir, owner: &str, name: &str) {
        let work = td.path().join(format!("work-{owner}-{name}"));
        std::fs::create_dir_all(&work).unwrap();
        run_git(&work, &["init", "-b", "main"]);
        std::fs::write(work.join("README.md"), "# Demo\nhello forge\n").unwrap();
        std::fs::create_dir_all(work.join("src")).unwrap();
        std::fs::write(work.join("src/lib.rs"), "pub fn f() -> u8 { 42 }\n").unwrap();
        run_git(&work, &["add", "-A"]);
        run_git(
            &work,
            &[
                "-c",
                "user.email=t@e.st",
                "-c",
                "user.name=Tester",
                "commit",
                "-m",
                "initial commit",
            ],
        );
        run_git(&work, &["tag", "v1.0"]);
        let bare = td
            .path()
            .join("repos")
            .join(owner)
            .join(format!("{name}.git"));
        std::fs::create_dir_all(bare.parent().unwrap()).unwrap();
        run_git(
            td.path(),
            &[
                "clone",
                "--bare",
                work.to_str().unwrap(),
                bare.to_str().unwrap(),
            ],
        );
    }

    async fn body(svc: &ForgeService, path: &str) -> (u16, String) {
        let r = svc
            .handle(req("GET", path), ForgeAgent::Anonymous)
            .await
            .unwrap();
        (r.status, String::from_utf8_lossy(&r.body).into_owned())
    }

    #[tokio::test]
    async fn overview_lists_tree_and_readme() {
        if !git_available() {
            return;
        }
        let (td, svc) = service();
        seed_repo(&td, "alice", "demo");
        let (status, html) = body(&svc, "/forge/alice/demo").await;
        assert_eq!(status, 200);
        assert!(html.contains("alice/demo"));
        // Root tree entries.
        assert!(html.contains("README.md"));
        assert!(html.contains("src"));
        // README rendered.
        assert!(html.contains("hello forge"));
    }

    #[tokio::test]
    async fn tree_blob_raw_roundtrip() {
        if !git_available() {
            return;
        }
        let (td, svc) = service();
        seed_repo(&td, "alice", "demo");

        // Subdirectory listing.
        let (s1, tree) = body(&svc, "/forge/alice/demo/tree/main/src").await;
        assert_eq!(s1, 200);
        assert!(tree.contains("lib.rs"));

        // Blob view renders textual content (escaped).
        let (s2, blob) = body(&svc, "/forge/alice/demo/blob/main/src/lib.rs").await;
        assert_eq!(s2, 200);
        assert!(blob.contains("pub fn f"));

        // Raw serves text/plain with nosniff.
        let r = svc
            .handle(
                req("GET", "/forge/alice/demo/raw/main/src/lib.rs"),
                ForgeAgent::Anonymous,
            )
            .await
            .unwrap();
        assert_eq!(r.status, 200);
        let ct = r
            .headers
            .iter()
            .find(|(k, _)| k == "content-type")
            .map(|(_, v)| v.clone())
            .unwrap();
        assert!(ct.starts_with("text/plain"));
        assert_eq!(
            String::from_utf8_lossy(&r.body),
            "pub fn f() -> u8 { 42 }\n"
        );
    }

    #[tokio::test]
    async fn commits_branches_tags_commit_view() {
        if !git_available() {
            return;
        }
        let (td, svc) = service();
        seed_repo(&td, "alice", "demo");

        let (s1, commits) = body(&svc, "/forge/alice/demo/commits/main").await;
        assert_eq!(s1, 200);
        assert!(commits.contains("initial commit"));

        let (s2, branches) = body(&svc, "/forge/alice/demo/branches").await;
        assert_eq!(s2, 200);
        assert!(branches.contains("main"));
        assert!(branches.contains("default"));

        let (s3, tags) = body(&svc, "/forge/alice/demo/tags").await;
        assert_eq!(s3, 200);
        assert!(tags.contains("v1.0"));

        // Resolve the HEAD sha, then view that commit.
        let dir = td.path().join("repos/alice/demo.git");
        let out = std::process::Command::new("git")
            .args(["rev-parse", "HEAD"])
            .current_dir(&dir)
            .output()
            .unwrap();
        let sha = String::from_utf8_lossy(&out.stdout).trim().to_string();
        let (s4, commit) = body(&svc, &format!("/forge/alice/demo/commit/{sha}")).await;
        assert_eq!(s4, 200);
        assert!(commit.contains("initial commit"));
        assert!(commit.contains("README.md"));
    }

    #[tokio::test]
    async fn browse_rejects_bad_ref_and_missing_repo() {
        if !git_available() {
            return;
        }
        let (td, svc) = service();
        seed_repo(&td, "alice", "demo");

        // Missing repo → 404.
        let (s1, _) = body(&svc, "/forge/alice/ghost").await;
        assert_eq!(s1, 404);

        // Flag-injection ref → 400.
        let (s2, _) = body(&svc, "/forge/alice/demo/tree/--upload-pack/x").await;
        assert_eq!(s2, 400);

        // Unknown path in a valid ref → 404.
        let (s3, _) = body(&svc, "/forge/alice/demo/blob/main/nope.txt").await;
        assert_eq!(s3, 404);
    }

    #[tokio::test]
    async fn empty_repo_overview_renders() {
        if !git_available() {
            return;
        }
        let (td, svc) = service();
        // A bare repo with no commits.
        let bare = td.path().join("repos/bob/empty.git");
        std::fs::create_dir_all(&bare).unwrap();
        run_git(
            td.path(),
            &["init", "--bare", "-b", "main", bare.to_str().unwrap()],
        );
        let (status, html) = body(&svc, "/forge/bob/empty").await;
        assert_eq!(status, 200);
        assert!(html.contains("empty"));
    }

    // ---- Phase 2 issues + spine ----------------------------------------

    use std::collections::HashMap;
    use std::sync::Mutex;

    struct MockLb {
        present: Mutex<HashMap<String, Vec<u8>>>,
    }
    impl MockLb {
        fn new() -> Self {
            Self {
                present: Mutex::new(HashMap::new()),
            }
        }
        fn put(&self, url: &str, body: &[u8]) {
            self.present
                .lock()
                .unwrap()
                .insert(url.into(), body.to_vec());
        }
        fn remove(&self, url: &str) {
            self.present.lock().unwrap().remove(url);
        }
    }
    #[async_trait::async_trait]
    impl bodies::LoopbackFetch for MockLb {
        async fn get(&self, url: &str, _max: usize, _to: u64) -> bodies::FetchResult {
            match self.present.lock().unwrap().get(url) {
                Some(b) => bodies::FetchResult::Body(b.clone()),
                None => bodies::FetchResult::Removed,
            }
        }
    }

    fn pod_agent(user: &str) -> ForgeAgent {
        ForgeAgent::Pod {
            webid: format!("https://pod.example/{user}/profile/card#me"),
            username: user.to_string(),
        }
    }

    fn post_form(path: &str, form: &str) -> ForgeRequest {
        ForgeRequest {
            method: "POST".into(),
            path: path.into(),
            query: String::new(),
            headers: vec![(
                "content-type".into(),
                "application/x-www-form-urlencoded".into(),
            )],
            raw_body: Bytes::from(form.to_string()),
            host_url: Some("https://pod.example".into()),
        }
    }

    /// Build a git-seeded service with a mock loopback injected.
    fn issues_service() -> (TempDir, ForgeService, Arc<MockLb>) {
        let td = TempDir::new().unwrap();
        let mock = Arc::new(MockLb::new());
        let svc = ForgeService::new(ForgeConfig::default(), td.path())
            .unwrap()
            .with_loopback(mock.clone() as Arc<dyn bodies::LoopbackFetch>);
        (td, svc, mock)
    }

    #[tokio::test]
    async fn issue_create_list_detail_and_deletion() {
        if !git_available() {
            return;
        }
        let (td, svc, mock) = issues_service();
        seed_repo(&td, "alice", "demo");

        let url = "https://pod.example/alice/public/forge/alice--demo/issue-1.jsonld";
        mock.put(url, b"the issue body text");

        // Create.
        let r = svc
            .handle(
                post_form(
                    "/forge/alice/demo/issues",
                    &format!(
                        "title=First+bug&resourceUrl={}",
                        url.replace(':', "%3A").replace('/', "%2F")
                    ),
                ),
                pod_agent("alice"),
            )
            .await
            .unwrap();
        assert_eq!(r.status, 303);
        let loc = r
            .headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case("location"))
            .map(|(_, v)| v.clone())
            .unwrap();
        assert_eq!(loc, "/forge/alice/demo/issues/1");

        // List shows it.
        let (s, list) = body(&svc, "/forge/alice/demo/issues").await;
        assert_eq!(s, 200);
        assert!(list.contains("First bug"));
        assert!(list.contains("1 open"));

        // Detail re-fetches the body.
        let (s2, detail) = body(&svc, "/forge/alice/demo/issues/1").await;
        assert_eq!(s2, 200);
        assert!(detail.contains("the issue body text"));

        // Author deletes the pod body → detail shows the removed notice.
        mock.remove(url);
        let (_s3, detail2) = body(&svc, "/forge/alice/demo/issues/1").await;
        assert!(detail2.contains("content removed by its author"));
    }

    #[tokio::test]
    async fn issue_create_rejects_foreign_area_and_ssrf() {
        if !git_available() {
            return;
        }
        let (td, svc, mock) = issues_service();
        seed_repo(&td, "alice", "demo");
        mock.put(
            "https://pod.example/bob/public/forge/alice--demo/x.jsonld",
            b"body",
        );

        // alice pointing at bob's pod area → 403 (own-area guard).
        let bad = "https://pod.example/bob/public/forge/alice--demo/x.jsonld";
        let r = svc
            .handle(
                post_form(
                    "/forge/alice/demo/issues",
                    &format!(
                        "title=x&resourceUrl={}",
                        bad.replace(':', "%3A").replace('/', "%2F")
                    ),
                ),
                pod_agent("alice"),
            )
            .await
            .unwrap();
        assert_eq!(r.status, 403);

        // A cross-origin SSRF target → rejected before any fetch.
        let ssrf = "http://169.254.169.254/alice/public/forge/alice--demo/x.jsonld";
        let r2 = svc
            .handle(
                post_form(
                    "/forge/alice/demo/issues",
                    &format!(
                        "title=x&resourceUrl={}",
                        ssrf.replace(':', "%3A").replace('/', "%2F")
                    ),
                ),
                pod_agent("alice"),
            )
            .await
            .unwrap();
        assert!(r2.status == 403 || r2.status == 400);
    }

    #[tokio::test]
    async fn comment_appends_to_thread() {
        if !git_available() {
            return;
        }
        let (td, svc, mock) = issues_service();
        seed_repo(&td, "alice", "demo");

        let open = "https://pod.example/alice/public/forge/alice--demo/i1.jsonld";
        mock.put(open, b"opening");
        svc.handle(
            post_form(
                "/forge/alice/demo/issues",
                &format!(
                    "title=t&resourceUrl={}",
                    open.replace(':', "%3A").replace('/', "%2F")
                ),
            ),
            pod_agent("alice"),
        )
        .await
        .unwrap();

        // bob comments — his body lives in HIS pod, namespaced by the repo.
        let c = "https://pod.example/bob/public/forge/alice--demo/c1.jsonld";
        mock.put(c, b"a helpful comment");
        let r = svc
            .handle(
                post_form(
                    "/forge/alice/demo/issues/1",
                    &format!("resourceUrl={}", c.replace(':', "%3A").replace('/', "%2F")),
                ),
                pod_agent("bob"),
            )
            .await
            .unwrap();
        assert_eq!(r.status, 303);

        let (_s, detail) = body(&svc, "/forge/alice/demo/issues/1").await;
        assert!(detail.contains("opening"));
        assert!(detail.contains("a helpful comment"));
        assert!(detail.contains("commented"));
    }

    #[tokio::test]
    async fn issue_create_without_loopback_fails_closed() {
        if !git_available() {
            return;
        }
        // No loopback injected → pod-body verification unavailable (501).
        let (td, svc) = service();
        seed_repo(&td, "alice", "demo");
        let url = "https://pod.example/alice/public/forge/alice--demo/x.jsonld";
        let r = svc
            .handle(
                post_form(
                    "/forge/alice/demo/issues",
                    &format!(
                        "title=x&resourceUrl={}",
                        url.replace(':', "%3A").replace('/', "%2F")
                    ),
                ),
                pod_agent("alice"),
            )
            .await
            .unwrap();
        assert_eq!(r.status, 501);
    }

    // ---- Phase 3 tokens + hosted + namespace guard ---------------------

    fn hex64() -> String {
        "d".repeat(64)
    }

    fn nostr_agent(hex: &str) -> ForgeAgent {
        ForgeAgent::Nostr {
            pubkey_hex: hex.to_string(),
        }
    }

    fn post_json(path: &str, json: &str) -> ForgeRequest {
        ForgeRequest {
            method: "POST".into(),
            path: path.into(),
            query: String::new(),
            headers: vec![("content-type".into(), "application/json".into())],
            raw_body: Bytes::from(json.to_string()),
            host_url: Some("https://pod.example".into()),
        }
    }

    #[tokio::test]
    async fn token_mint_then_resolves() {
        let (_td, svc) = service();
        // Mint a token for a NIP-98-authenticated nostr caller.
        let agent = nostr_agent(&hex64());
        let r = svc
            .handle(post_form("/forge/api/token", ""), agent.clone())
            .await
            .unwrap();
        assert_eq!(r.status, 200);
        let v: serde_json::Value = serde_json::from_slice(&r.body).unwrap();
        let token = v["token"].as_str().unwrap().to_string();
        assert!(token.starts_with("f1."));

        // Present the token on a later request → same identity resolves.
        let follow = ForgeRequest {
            method: "GET".into(),
            path: "/forge/x".into(),
            query: String::new(),
            headers: vec![("authorization".into(), format!("Bearer {token}"))],
            raw_body: Bytes::new(),
            host_url: Some("https://pod.example".into()),
        };
        assert_eq!(svc.resolve_agent(&follow), agent);
    }

    #[tokio::test]
    async fn token_mint_anonymous_is_401() {
        let (_td, svc) = service();
        let r = svc
            .handle(post_form("/forge/api/token", ""), ForgeAgent::Anonymous)
            .await
            .unwrap();
        assert_eq!(r.status, 401);
    }

    #[tokio::test]
    async fn podless_nostr_issue_uses_hosted_store() {
        if !git_available() {
            return;
        }
        let (td, svc) = service();
        let hex = hex64();
        seed_repo(&td, &hex, "proj");

        // No pod: submit the body inline; the forge hosts it.
        let r = svc
            .handle(
                post_json(
                    &format!("/forge/{hex}/proj/issues"),
                    "{\"title\":\"podless bug\",\"body\":\"no pod here\"}",
                ),
                nostr_agent(&hex),
            )
            .await
            .unwrap();
        assert_eq!(r.status, 303, "podless issue create should redirect");

        // Detail re-fetches from the hosted store (no loopback needed).
        let (s, detail) = body(&svc, &format!("/forge/{hex}/proj/issues/1")).await;
        assert_eq!(s, 200);
        assert!(detail.contains("no pod here"));
        assert!(detail.contains("podless bug"));
    }

    #[tokio::test]
    async fn hosted_api_get_and_owner_only_delete() {
        let (td, svc) = service();
        let hex = hex64();
        // Write a hosted body directly through a store on the same dir.
        let store = HostedStore::new(td.path());
        let rref = store.write(&hex, b"{\"body\":\"hi\"}").await.unwrap();
        let (h, u) = HostedStore::parse_ref(&rref).unwrap();

        // Public GET.
        let (s, got) = body(&svc, &format!("/forge/api/hosted/{h}/{u}")).await;
        assert_eq!(s, 200);
        assert!(got.contains("hi"));

        // A non-owner DELETE is 403.
        let del_other = ForgeRequest {
            method: "DELETE".into(),
            path: format!("/forge/api/hosted/{h}/{u}"),
            query: String::new(),
            headers: vec![],
            raw_body: Bytes::new(),
            host_url: Some("https://pod.example".into()),
        };
        let r1 = svc
            .handle(del_other.clone(), nostr_agent(&"e".repeat(64)))
            .await
            .unwrap();
        assert_eq!(r1.status, 403);

        // The owner DELETE succeeds (204), then the body is gone (404).
        let r2 = svc.handle(del_other, nostr_agent(&hex)).await.unwrap();
        assert_eq!(r2.status, 204);
        let (s2, _) = body(&svc, &format!("/forge/api/hosted/{h}/{u}")).await;
        assert_eq!(s2, 404);
    }

    #[tokio::test]
    async fn namespace_push_guard_blocks_foreign_push() {
        let (_td, svc) = service();
        // A receive-pack (push) into alice's namespace by bob → 403,
        // before the CGI is ever invoked.
        let push = ForgeRequest {
            method: "POST".into(),
            path: "/forge/alice/demo.git/git-receive-pack".into(),
            query: String::new(),
            headers: vec![],
            raw_body: Bytes::new(),
            host_url: Some("https://pod.example".into()),
        };
        let r = svc
            .handle(push.clone(), nostr_agent(&hex64()))
            .await
            .unwrap();
        assert_eq!(r.status, 403);

        // Anonymous push is also blocked.
        let r_anon = svc.handle(push, ForgeAgent::Anonymous).await.unwrap();
        assert_eq!(r_anon.status, 403);
    }

    #[tokio::test]
    async fn namespace_push_guard_allows_own_push() {
        // A push into the caller's OWN namespace passes the guard (it then
        // reaches the CGI, which is absent here → a non-403 backend error).
        let (_td, svc) = service();
        let hex = hex64();
        let push = ForgeRequest {
            method: "POST".into(),
            path: format!("/forge/{hex}/demo.git/git-receive-pack"),
            query: String::new(),
            headers: vec![],
            raw_body: Bytes::new(),
            host_url: Some("https://pod.example".into()),
        };
        let r = svc.handle(push, nostr_agent(&hex)).await.unwrap();
        assert_ne!(r.status, 403, "own-namespace push must pass the guard");
    }
}