isb-apps 1.8.0

isb's apps over stacks: deployments, builds, jobs, backups, templates and notifications. Use the `isb` crate.
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
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
//! The app service: records on disk, the deploy queue and pipeline, and
//! webhooks.
//!
//! A deploy is a record ([`Deployment`]) that moves `queued` → `building` →
//! `deploying` → `done` | `failed`. One runs at a time per app; a deploy
//! asked for while one runs waits behind it, and a newer request
//! supersedes a waiting one that has not started (`superseded`), so a burst
//! of pushes builds the latest commit once.
//!
//! The pipeline: resolve the image (an image source's digest, or fetch the
//! git source and build it with [`crate::build::run`]), render the app into
//! its project environment's stack in place of its old service, hand the
//! stack to the controller, and wait for that service to converge.

use std::collections::{BTreeMap, VecDeque};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use super::git::{self, Credentials, GitAuth};
use super::{App, AppSpec, Rendered, Source, webhook};
use crate::build::{BuildRequest, BuiltImage};
use crate::client::Client;
use crate::error::{Error, Result};
use crate::org::OrgId;
use crate::secrets::Secrets;
use crate::stack::{Controller, StackDef};

#[path = "db_secrets.rs"]
mod db_secrets;

/// Turns a checkout into an image: [`crate::build::run`], or a stand-in.
pub type BuildFn =
    Arc<dyn Fn(&Client, &BuildRequest, &mut dyn FnMut(&str)) -> Result<BuiltImage> + Send + Sync>;

/// Asks an image's registry about it, waiting at most the given time:
/// whether it exists, and its manifest digest
/// ([`crate::image_check::probe`], or a stand-in).
pub type ImageProbe = Arc<dyn Fn(&str, Duration) -> crate::image_check::Probe + Send + Sync>;

/// Deployment records kept per app (older ones and their logs are pruned).
const KEEP_DEPLOYMENTS: usize = 30;

/// How long a deploy waits for its service to converge.
const DEPLOY_TIMEOUT: Duration = Duration::from_secs(900);

/// Who asked for a deployment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Trigger {
    /// The local CLI.
    Manual,
    /// A tool call over MCP or REST.
    Api,
    Webhook,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Status {
    Queued,
    Building,
    Deploying,
    Done,
    Failed,
    /// A newer deploy replaced it before it started.
    Superseded,
    /// Closed before it started, because what queued it stopped.
    Cancelled,
}

impl Status {
    pub fn finished(self) -> bool {
        !matches!(self, Status::Queued | Status::Building | Status::Deploying)
    }

    /// The moves the pipeline makes; anything else is a bug.
    pub fn can_become(self, next: Status) -> bool {
        use Status::*;
        matches!(
            (self, next),
            (Queued, Building)
                | (Queued, Superseded)
                | (Queued, Cancelled)
                | (Queued, Failed)
                | (Building, Deploying)
                | (Building, Failed)
                | (Deploying, Done)
                | (Deploying, Failed)
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Commit {
    pub sha: String,
    pub message: String,
}

/// One deploy of an app.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Deployment {
    pub id: u64,
    pub app: String,
    pub trigger: Trigger,
    /// The caller (a user, an API token, `local`, `webhook:github`).
    pub by: String,
    pub status: Status,
    /// What a webhook said was pushed (`refs/heads/main abc123`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub requested: Option<String>,
    /// A rollback: the deployment whose image and settings it restores.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rollback_of: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub commit: Option<Commit>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub image: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub digest: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    /// Unix milliseconds.
    pub created_at: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started_at: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub finished_at: Option<u64>,
    /// The service as deployed: what a rollback puts back.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rendered: Option<Rendered>,
}

impl Deployment {
    /// Move to `next`, refusing a move the state machine does not have.
    pub fn advance(&mut self, next: Status) -> Result<()> {
        if !self.status.can_become(next) {
            return Err(Error::invalid(format!(
                "deployment {}: cannot go from {:?} to {next:?}",
                self.id, self.status
            )));
        }
        self.status = next;
        let now = crate::stack::controller::now_ms();
        match next {
            Status::Building => self.started_at = Some(now),
            s if s.finished() => self.finished_at = Some(now),
            _ => {}
        }
        Ok(())
    }

    /// The record without the rendered service, for listings.
    pub fn summary(&self) -> Value {
        let mut v = serde_json::to_value(self).unwrap_or_default();
        if let Some(o) = v.as_object_mut() {
            o.remove("rendered");
        }
        v
    }
}

#[derive(Default)]
pub(super) struct Queue {
    pub(super) running: bool,
    pub(super) next: Option<u64>,
}

pub(super) struct Inner {
    pub(super) state: PathBuf,
    pub(super) client: Client,
    pub(super) ctl: Controller,
    pub(super) secrets: Arc<Secrets>,
    pub(super) build: BuildFn,
    pub(super) probe: ImageProbe,
    pub(super) timeout: Duration,
    /// Held across read-modify-write of app and project records.
    pub(super) edit: Mutex<()>,
    /// Held across read-splice-deploy of a stack, so two apps deploying
    /// into one environment never drop each other's service.
    pub(super) stacks: Mutex<()>,
    /// Keyed by app, or `<app>#pr-<n>` for a preview.
    pub(super) queues: Mutex<BTreeMap<(OrgId, String), Queue>>,
    /// Recent webhook delivery ids, to ignore a replayed delivery.
    deliveries: Mutex<VecDeque<String>>,
    /// Told when a secret gets a new value outside `secret_set` (a
    /// database's URL secrets, written at deploy), for what stacks don't
    /// cover: the workspaces.
    pub(super) secret_hook: OnceLock<SecretHook>,
    /// Compose stack owners per org, read from the project records.
    pub(super) owners: Mutex<BTreeMap<OrgId, Arc<super::compose::Owners>>>,
}

/// Called with an org and a secret's name after it got a new value.
pub type SecretHook = Arc<dyn Fn(&OrgId, &str) + Send + Sync>;

/// Projects, environments, apps and their deployments, for every org.
#[derive(Clone)]
pub struct Apps {
    pub(super) inner: Arc<Inner>,
}

impl Apps {
    /// Open the app store under `state`. Deployments a stopped daemon left
    /// unfinished are marked failed.
    pub fn new(state: &Path, client: Client, ctl: Controller, secrets: Arc<Secrets>) -> Apps {
        let a = Apps {
            inner: Arc::new(Inner {
                state: state.to_path_buf(),
                client,
                ctl,
                secrets,
                build: Arc::new(|c: &Client, r: &BuildRequest, l: &mut dyn FnMut(&str)| {
                    crate::build::run(c, r, l)
                }),
                probe: Arc::new(crate::image_check::probe),
                timeout: DEPLOY_TIMEOUT,
                edit: Mutex::new(()),
                stacks: Mutex::new(()),
                queues: Mutex::new(BTreeMap::new()),
                deliveries: Mutex::new(VecDeque::new()),
                secret_hook: OnceLock::new(),
                owners: Mutex::new(BTreeMap::new()),
            }),
        };
        a.recover();
        a
    }

    fn with(self, f: impl FnOnce(&mut Inner)) -> Apps {
        let mut inner = Arc::try_unwrap(self.inner)
            .unwrap_or_else(|_| panic!("Apps::with_* before the Apps is shared"));
        f(&mut inner);
        Apps {
            inner: Arc::new(inner),
        }
    }

    /// Use `f` instead of [`crate::build::run`] (tests).
    pub fn with_build(self, f: BuildFn) -> Apps {
        self.with(|i| i.build = f)
    }

    /// Use `f` to ask registries about images instead of `skopeo inspect`.
    pub fn with_probe(self, f: ImageProbe) -> Apps {
        self.with(|i| i.probe = f)
    }

    pub fn with_timeout(self, t: Duration) -> Apps {
        self.with(|i| i.timeout = t)
    }

    // --- paths -----------------------------------------------------------

    pub(super) fn apps_dir(&self, org: &OrgId) -> PathBuf {
        super::org_root(&self.inner.state, org).join("apps")
    }

    pub(super) fn app_dir(&self, org: &OrgId, app: &str) -> PathBuf {
        self.apps_dir(org).join(app)
    }

    fn deployments_dir(&self, org: &OrgId, app: &str) -> PathBuf {
        self.app_dir(org, app).join("deployments")
    }

    /// Where an app's git checkout lives.
    pub fn source_dir(&self, org: &OrgId, app: &str) -> PathBuf {
        super::org_root(&self.inner.state, org)
            .join("sources")
            .join(app)
    }

    // --- apps ------------------------------------------------------------

    pub fn get(&self, org: &OrgId, name: &str) -> Result<App> {
        super::validate_app_name(name)?;
        match std::fs::read(self.app_dir(org, name).join("app.json")) {
            Ok(b) => Ok(serde_json::from_slice(&b)?),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                Err(Error::NotFound(format!("app {name} in org {org}")))
            }
            Err(e) => Err(e.into()),
        }
    }

    pub fn list(&self, org: &OrgId) -> Result<Vec<App>> {
        let mut out = Vec::new();
        let Ok(rd) = std::fs::read_dir(self.apps_dir(org)) else {
            return Ok(out);
        };
        for e in rd.flatten() {
            let p = e.path().join("app.json");
            if !p.is_file() {
                continue;
            }
            match serde_json::from_slice::<App>(&std::fs::read(&p)?) {
                Ok(a) => out.push(a),
                Err(e) => eprintln!("isb serve: skipping {}: {e}", p.display()),
            }
        }
        out.sort_by(|a, b| a.spec.name.cmp(&b.spec.name));
        Ok(out)
    }

    fn save(&self, org: &OrgId, app: &App) -> Result<()> {
        super::write_atomic(
            &self.app_dir(org, &app.spec.name).join("app.json"),
            &serde_json::to_vec_pretty(app)?,
        )
    }

    /// Create an app (not deployed yet) and its webhook secret. Returns the
    /// app and that secret.
    pub fn create(&self, org: &OrgId, mut spec: AppSpec) -> Result<(App, String)> {
        if let Source::Database(db) = &mut spec.source {
            db.normalize(&spec.name);
        }
        spec.validate()?;
        let _g = self.inner.edit.lock().unwrap();
        let proj = self.project_get(org, &spec.project)?;
        if !proj.environments.contains(&spec.environment) {
            return Err(Error::NotFound(format!(
                "environment {} in project {} (it has {})",
                spec.environment,
                spec.project,
                proj.environments.join(", ")
            )));
        }
        if self.app_dir(org, &spec.name).join("app.json").exists() {
            return Err(Error::AlreadyExists(format!("app {}", spec.name)));
        }
        self.check_app_name_free(org, &spec.project, &spec.environment, &spec.name)?;
        self.check_secrets(org, &spec)?;
        let now = crate::stack::now_secs();
        let app = App {
            spec,
            created_at: now,
            updated_at: now,
            next_deployment: 1,
            current: None,
        };
        if let Source::Database(db) = &app.spec.source {
            self.database_credentials(org, &app.spec, db)?;
        }
        let secret = git::random_hex(32);
        self.inner
            .secrets
            .set(org, &app.spec.webhook_secret(), secret.as_bytes())?;
        self.save(org, &app)?;
        Ok((app, secret))
    }

    /// The org's secret store (backups read destination credentials and
    /// database passwords from it).
    pub fn secrets(&self) -> &Arc<Secrets> {
        &self.inner.secrets
    }

    /// The incus client the apps run on.
    pub fn client(&self) -> &Client {
        &self.inner.client
    }

    /// The stack controller (events, definitions).
    pub fn controller(&self) -> &Controller {
        &self.inner.ctl
    }

    /// Wait (up to the deploy timeout) for an app's service to converge at
    /// its current revision: `(converged, why not)`.
    pub fn wait_converged(&self, org: &OrgId, name: &str) -> Result<(bool, String)> {
        let app = self.get(org, name)?;
        let q = crate::stack::qualified(org, &app.spec.stack()?);
        self.wait_service(&q, name)
    }

    /// Secrets an app names must exist (a typo is better caught now than
    /// at deploy).
    fn check_secrets(&self, org: &OrgId, spec: &AppSpec) -> Result<()> {
        let mut names = spec.env.secret_names();
        if let Some(p) = &spec.previews {
            names.extend(p.secret_names());
        }
        names.extend(spec.files.iter().map(|f| f.secret.clone()));
        if let Source::Git(g) = &spec.source {
            names.extend(g.auth.secret().map(String::from));
        }
        for n in names {
            self.inner.secrets.inspect(org, &n).map_err(|e| match e {
                Error::NotFound(_) => Error::invalid(format!(
                    "secret {n} does not exist in org {org}; create it with `isb secret create {n} --org {org}`"
                )),
                e => e,
            })?;
        }
        Ok(())
    }

    /// What `create` and `update` check about the secrets a spec names,
    /// for callers that check without writing (`app_apply` with
    /// `dry_run`).
    pub fn check_spec(&self, org: &OrgId, spec: &AppSpec) -> Result<()> {
        self.check_secrets(org, spec)
    }

    /// Change an app's settings with a JSON merge patch (`null` clears a
    /// setting). Name, project and environment are fixed. Takes effect at
    /// the next deploy.
    pub fn update(&self, org: &OrgId, name: &str, patch: &Value) -> Result<App> {
        let _g = self.inner.edit.lock().unwrap();
        let mut app = self.get(org, name)?;
        let mut v = serde_json::to_value(&app.spec)?;
        super::merge_patch(&mut v, patch);
        let mut spec: AppSpec =
            serde_json::from_value(v).map_err(|e| Error::invalid(format!("app {name}: {e}")))?;
        super::manifest::check_update(&app.spec, &mut spec)?;
        spec.validate()?;
        self.check_secrets(org, &spec)?;
        app.spec = spec;
        app.updated_at = crate::stack::now_secs();
        self.save(org, &app)?;
        Ok(app)
    }

    pub fn env_get(&self, org: &OrgId, name: &str) -> Result<String> {
        Ok(self.get(org, name)?.spec.env.render())
    }

    /// Replace an app's environment with `.env` text.
    pub fn env_set(&self, org: &OrgId, name: &str, text: &str) -> Result<App> {
        let env = super::EnvFile::parse(text)?;
        self.update(org, name, &json!({"env": env.render()}))
    }

    /// The app's webhook secret; with `rotate`, a new one first.
    pub fn webhook_secret(&self, org: &OrgId, name: &str, rotate: bool) -> Result<String> {
        let app = self.get(org, name)?;
        let key = app.spec.webhook_secret();
        if !rotate {
            if let Ok((v, _)) = self.inner.secrets.get(org, &key) {
                return String::from_utf8(v)
                    .map_err(|_| Error::invalid("webhook secret is not text"));
            }
        }
        let s = git::random_hex(32);
        self.inner.secrets.set(org, &key, s.as_bytes())?;
        Ok(s)
    }

    /// A new ed25519 deploy key for a git app over SSH: stored as the org
    /// secret `app.<app>.deploy-key`, made the app's credential, and its
    /// public half returned to paste into the repository's deploy keys.
    pub fn deploy_key(&self, org: &OrgId, name: &str) -> Result<String> {
        let app = self.get(org, name)?;
        let Source::Git(g) = &app.spec.source else {
            return Err(Error::invalid(format!("app {name} has no git source")));
        };
        if git::transport(&g.url)? != git::Transport::Ssh {
            return Err(Error::invalid(format!(
                "a deploy key needs an SSH URL; {} is not one (git@host:owner/repo)",
                g.url
            )));
        }
        let (private, public) =
            git::generate_deploy_key(&self.apps_dir(org), &format!("isb-deploy-{org}-{name}"))?;
        let key = super::deploy_key_secret(name);
        self.inner.secrets.set(org, &key, &private)?;
        self.update(
            org,
            name,
            &json!({"source": {"git": {"auth": {"ssh_key_secret": key}}}}),
        )?;
        Ok(public)
    }

    /// Delete an app: its service leaves the stack (the stack goes when it
    /// was the last), then its records, checkout and the secrets isb made
    /// for it. Named volumes are kept.
    pub fn delete(&self, org: &OrgId, name: &str) -> Result<()> {
        let app = self.get(org, name)?;
        if self
            .inner
            .queues
            .lock()
            .unwrap()
            .contains_key(&(org.clone(), name.to_string()))
        {
            return Err(Error::invalid(format!(
                "app {name} is deploying; delete it once that finishes"
            )));
        }
        // Its previews go first, with their stacks, volumes and images.
        self.previews_remove_all(org, name)?;
        let stack = app.spec.stack()?;
        let q = crate::stack::qualified(org, &stack);
        {
            let _s = self.inner.stacks.lock().unwrap();
            if let Ok(cur) = self.inner.ctl.definition(&q) {
                if cur.file.services.contains_key(name) {
                    let file = super::splice(Some(&cur.file), &stack, name, None);
                    if file.services.is_empty() {
                        self.inner.ctl.remove(&q, false, Duration::from_secs(300))?;
                    } else {
                        let def = self.stack_def(org, &stack, file, "app delete")?;
                        self.inner.ctl.deploy(def)?;
                    }
                }
            }
        }
        let _g = self.inner.edit.lock().unwrap();
        for s in [super::webhook_secret(name), super::deploy_key_secret(name)] {
            match self.inner.secrets.delete(org, &s) {
                Ok(()) => {}
                Err(e) if e.is_not_found() => {}
                Err(e) => return Err(e),
            }
        }
        let _ = std::fs::remove_dir_all(self.source_dir(org, name));
        std::fs::remove_dir_all(self.app_dir(org, name))?;
        Ok(())
    }

    // --- deployments -----------------------------------------------------

    fn dep_path(&self, org: &OrgId, app: &str, id: u64) -> PathBuf {
        self.deployments_dir(org, app).join(format!("{id}.json"))
    }

    fn log_path(&self, org: &OrgId, app: &str, id: u64) -> PathBuf {
        self.deployments_dir(org, app).join(format!("{id}.log"))
    }

    pub(super) fn save_dep(&self, org: &OrgId, d: &Deployment) -> Result<()> {
        super::write_atomic(
            &self.dep_path(org, &d.app, d.id),
            &serde_json::to_vec_pretty(d)?,
        )
    }

    pub fn deployment(&self, org: &OrgId, app: &str, id: u64) -> Result<Deployment> {
        super::validate_app_name(app)?;
        match std::fs::read(self.dep_path(org, app, id)) {
            Ok(b) => Ok(serde_json::from_slice(&b)?),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                Err(Error::NotFound(format!("deployment {id} of app {app}")))
            }
            Err(e) => Err(e.into()),
        }
    }

    /// Newest first.
    pub fn deployments(&self, org: &OrgId, app: &str) -> Result<Vec<Deployment>> {
        self.get(org, app)?;
        let mut ids: Vec<u64> = match std::fs::read_dir(self.deployments_dir(org, app)) {
            Ok(rd) => rd
                .flatten()
                .filter_map(|e| {
                    let n = e.file_name().into_string().ok()?;
                    n.strip_suffix(".json")?.parse().ok()
                })
                .collect(),
            Err(_) => vec![],
        };
        ids.sort_unstable_by(|a, b| b.cmp(a));
        ids.into_iter()
            .map(|id| self.deployment(org, app, id))
            .collect()
    }

    /// A deployment's log from byte `offset`: `(text, next offset, finished)`.
    pub fn log(&self, org: &OrgId, app: &str, id: u64, offset: u64) -> Result<(String, u64, bool)> {
        let (text, next, d) = self.log_and_record(org, app, id, offset)?;
        Ok((text, next, d.status.finished()))
    }

    /// [`Apps::log`] with the deployment record read just before the log,
    /// so a client follows the text and the status in one call: a record
    /// that says finished means the text holds every line.
    pub fn log_and_record(
        &self,
        org: &OrgId,
        app: &str,
        id: u64,
        offset: u64,
    ) -> Result<(String, u64, Deployment)> {
        let d = self.deployment(org, app, id)?;
        let b = std::fs::read(self.log_path(org, app, id)).unwrap_or_default();
        let start = (offset as usize).min(b.len());
        Ok((
            String::from_utf8_lossy(&b[start..]).into_owned(),
            b.len() as u64,
            d,
        ))
    }

    /// Wait until a deployment finishes, or `timeout`.
    pub fn wait(&self, org: &OrgId, app: &str, id: u64, timeout: Duration) -> Result<Deployment> {
        let started = Instant::now();
        loop {
            let d = self.deployment(org, app, id)?;
            if d.status.finished() || started.elapsed() >= timeout {
                return Ok(d);
            }
            std::thread::sleep(Duration::from_millis(500));
        }
    }

    /// Queue a deploy of the app's current settings.
    pub fn deploy(
        &self,
        org: &OrgId,
        name: &str,
        trigger: Trigger,
        by: &str,
        requested: Option<String>,
    ) -> Result<Deployment> {
        self.enqueue(org, name, trigger, by, requested, None)
    }

    /// Queue a rollback to deployment `to` (default: the one before the
    /// current): its image and settings, without building.
    pub fn rollback(
        &self,
        org: &OrgId,
        name: &str,
        to: Option<u64>,
        trigger: Trigger,
        by: &str,
    ) -> Result<Deployment> {
        let app = self.get(org, name)?;
        let target = match to {
            Some(id) => self.deployment(org, name, id)?,
            None => self
                .deployments(org, name)?
                .into_iter()
                .find(|d| d.status == Status::Done && Some(d.id) != app.current)
                .ok_or_else(|| {
                    Error::invalid(format!(
                        "app {name} has no earlier successful deployment to roll back to"
                    ))
                })?,
        };
        if target.status != Status::Done || target.rendered.is_none() {
            return Err(Error::invalid(format!(
                "deployment {} did not finish done; only those can be rolled back to",
                target.id
            )));
        }
        self.enqueue(org, name, trigger, by, None, Some(target.id))
    }

    fn enqueue(
        &self,
        org: &OrgId,
        name: &str,
        trigger: Trigger,
        by: &str,
        requested: Option<String>,
        rollback_of: Option<u64>,
    ) -> Result<Deployment> {
        let dep = {
            let _g = self.inner.edit.lock().unwrap();
            let mut app = self.get(org, name)?;
            let id = app.next_deployment;
            app.next_deployment += 1;
            self.save(org, &app)?;
            let d = Deployment {
                id,
                app: name.into(),
                trigger,
                by: by.into(),
                status: Status::Queued,
                requested,
                rollback_of,
                commit: None,
                image: None,
                digest: None,
                error: None,
                created_at: crate::stack::controller::now_ms(),
                started_at: None,
                finished_at: None,
                rendered: None,
            };
            self.save_dep(org, &d)?;
            self.prune(org, &app);
            d
        };
        self.event(
            org,
            name,
            "info",
            format!("deployment {} queued by {by}", dep.id),
        );
        let start = {
            let mut qs = self.inner.queues.lock().unwrap();
            let q = qs.entry((org.clone(), name.to_string())).or_default();
            if let Some(old) = q.next.replace(dep.id) {
                if let Ok(mut d) = self.deployment(org, name, old) {
                    if d.advance(Status::Superseded).is_ok() {
                        d.error = Some(format!("superseded by deployment {}", dep.id));
                        let _ = self.save_dep(org, &d);
                    }
                }
            }
            !std::mem::replace(&mut q.running, true)
        };
        if start {
            let me = self.clone();
            let (org, name) = (org.clone(), name.to_string());
            std::thread::spawn(move || me.drain(&org, &name));
        }
        Ok(dep)
    }

    /// Run queued deployments of one app until none is left.
    fn drain(&self, org: &OrgId, name: &str) {
        loop {
            let next = {
                let mut qs = self.inner.queues.lock().unwrap();
                let key = (org.clone(), name.to_string());
                let q = qs.entry(key.clone()).or_default();
                match q.next.take() {
                    Some(id) => id,
                    None => {
                        qs.remove(&key);
                        return;
                    }
                }
            };
            self.run(org, name, next);
        }
    }

    fn run(&self, org: &OrgId, name: &str, id: u64) {
        let Ok(mut dep) = self.deployment(org, name, id) else {
            return;
        };
        let mut log = match DeployLog::open(self, org, name, id) {
            Ok(l) => l,
            Err(e) => {
                eprintln!("isb serve: app {name}: deployment {id}: no log: {e}");
                return;
            }
        };
        let r = self.pipeline(org, name, &mut dep, &mut log);
        let (kind, level, msg) = match &r {
            Ok(()) => ("deploy.succeeded", "info", format!("deployment {id} done")),
            Err(e) => (
                "deploy.failed",
                "error",
                format!("deployment {id} failed: {e}"),
            ),
        };
        log.line(&msg);
        if let Err(e) = r {
            dep.error = Some(e.to_string());
            let _ = dep.advance(Status::Failed);
        }
        if let Err(e) = self.save_dep(org, &dep) {
            eprintln!("isb serve: app {name}: deployment {id}: {e}");
        }
        self.kind_event(org, name, kind, level, msg);
    }

    fn set_status(&self, org: &OrgId, dep: &mut Deployment, s: Status) -> Result<()> {
        dep.advance(s)?;
        self.save_dep(org, dep)?;
        self.event(
            org,
            &dep.app,
            "info",
            format!("deployment {}: {s:?}", dep.id).to_lowercase(),
        );
        Ok(())
    }

    #[expect(
        clippy::too_many_lines,
        reason = "predates the lint ratchet; split it when next changed"
    )]
    fn pipeline(
        &self,
        org: &OrgId,
        name: &str,
        dep: &mut Deployment,
        log: &mut DeployLog,
    ) -> Result<()> {
        self.set_status(org, dep, Status::Building)?;
        let app = self.get(org, name)?;
        let mut notes = Vec::new();
        let rendered = match dep.rollback_of {
            Some(of) => {
                let t = self.deployment(org, name, of)?;
                log.line(&format!(
                    "rolling back to deployment {of} ({})",
                    t.image.as_deref().unwrap_or("?")
                ));
                dep.image = t.image;
                dep.digest = t.digest;
                dep.commit = t.commit;
                t.rendered
                    .ok_or_else(|| Error::invalid(format!("deployment {of} kept no settings")))?
            }
            None => {
                let (image, digest) = match &app.spec.source {
                    Source::Image(i) => {
                        log.line(&format!("image {i}"));
                        self.resolve(i, log)?
                    }
                    Source::Database(db) => {
                        let i = db.image();
                        log.line(&format!(
                            "database {} {}: image {i}",
                            db.engine,
                            db.version()
                        ));
                        // A `urls` entry added since the last deploy is
                        // written now, not at the next password change, and
                        // what uses a value that moved follows it.
                        for n in self.write_urls(org, &app.spec, db)? {
                            log.line(&format!("secret {n}: the connection URL"));
                            self.url_secret_moved(org, &n, log);
                        }
                        self.resolve(&i, log)?
                    }
                    Source::Git(g) => {
                        let creds = self.credentials(org, &g.auth)?;
                        let co = git::fetch(g, &creds, &self.source_dir(org, name), &mut |l| {
                            log.line(l)
                        })?;
                        dep.commit = Some(Commit {
                            sha: co.sha.clone(),
                            message: co.message.clone(),
                        });
                        self.save_dep(org, dep)?;
                        let b =
                            app.spec.build.as_ref().ok_or_else(|| {
                                Error::invalid("a git source needs build settings")
                            })?;
                        let req = BuildRequest {
                            org: org.clone(),
                            app: name.into(),
                            context: co.dir.clone(),
                            subdir: g.subdir.clone(),
                            builder: b.builder.clone(),
                            args: b.args.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
                            tag: co.sha.clone(),
                            untrusted: b.untrusted,
                            cache: None,
                        };
                        log.line(&format!("building {} with {:?}", co.sha, b.builder));
                        let built =
                            (self.inner.build)(&self.inner.client, &req, &mut |l| log.line(l))?;
                        log.line(&format!("built {} ({})", built.image, built.digest));
                        (built.image, Some(built.digest))
                    }
                };
                dep.image = Some(image.clone());
                dep.digest = digest;
                super::render(&app.spec, &image, &mut notes)?
            }
        };
        for n in &notes {
            log.line(&format!("note: {n}"));
        }
        dep.rendered = Some(rendered.clone());
        self.set_status(org, dep, Status::Deploying)?;
        let stack = app.spec.stack()?;
        let q = crate::stack::qualified(org, &stack);
        let mark = self.event_mark();
        {
            let _s = self.inner.stacks.lock().unwrap();
            let cur = self.inner.ctl.definition(&q).ok();
            let file = super::splice(cur.as_ref().map(|d| &d.file), &stack, name, Some(&rendered));
            let def = self.stack_def(org, &stack, file, &dep.by)?;
            let changes = self.inner.ctl.deploy(def)?;
            let mine = changes.iter().find(|c| c.service == name);
            match mine {
                Some(c) if c.change == "unchanged" => {
                    // Deploy means fresh instances, as in Dokploy: a moved
                    // tag or a restart the settings do not show.
                    log.line("settings unchanged: replacing the instances anyway");
                    self.inner.ctl.redeploy(&q, name)?;
                }
                Some(c) => log.line(&format!(
                    "stack {stack}: {} {} (rev {}, {} replicas)",
                    c.service, c.change, c.rev, c.replicas
                )),
                None => {}
            }
        }
        let (ok, msg) = self.wait_service_with(&q, name, Some(mark), |m| log.line(m))?;
        if !ok {
            return Err(Error::invalid(format!("service {name}: {msg}")));
        }
        log.line(&format!("service {name}.{stack} converged"));
        self.set_status(org, dep, Status::Done)?;
        let _g = self.inner.edit.lock().unwrap();
        let mut app = self.get(org, name)?;
        app.current = Some(dep.id);
        self.save(org, &app)?;
        Ok(())
    }

    /// The stack definition for `file`, with its secrets bound.
    pub(super) fn stack_def(
        &self,
        org: &OrgId,
        stack: &str,
        file: crate::spec::ComposeFile,
        by: &str,
    ) -> Result<StackDef> {
        let base = self.apps_dir(org);
        std::fs::create_dir_all(&base)?;
        let secrets = crate::stack::secrets::bind(
            &self.inner.secrets,
            org,
            stack,
            &file,
            &BTreeMap::new(),
            false,
        )?;
        Ok(StackDef {
            source: None,
            domains: Default::default(),
            name: stack.into(),
            org: org.clone(),
            file,
            base_dir: base,
            secrets,
            force: BTreeMap::new(),
            images: BTreeMap::new(),
            deployed_at: crate::stack::now_secs(),
            deployed_by: by.into(),
            previous: None,
        })
    }

    /// Wait for one service of a stack to settle at its current revision:
    /// `(converged, why not)`.
    pub(super) fn wait_service(&self, q: &str, svc: &str) -> Result<(bool, String)> {
        self.wait_service_with(q, svc, None, |_| {})
    }

    /// The controller's latest event number: rollout events after it
    /// belong to a deploy that starts now.
    pub(super) fn event_mark(&self) -> u64 {
        self.inner.ctl.events(u64::MAX, 0).0
    }

    /// [`Apps::wait_service`], handing each controller event about the
    /// service after `since` (the rollout: slots created, probed, serving,
    /// old ones drained) to `seen` as it happens, so a deployment's log
    /// tells the whole story. App events (`app NAME: ...`) are skipped:
    /// they are the log's own lines.
    pub(super) fn wait_service_with(
        &self,
        q: &str,
        svc: &str,
        mut since: Option<u64>,
        mut seen: impl FnMut(&str),
    ) -> Result<(bool, String)> {
        let started = Instant::now();
        let own = format!("app {svc}");
        let mut relay = |since: &mut Option<u64>| {
            let Some(after) = *since else { return };
            let (last, evs) = self.inner.ctl.events(after, 500);
            for e in evs.iter().filter(|e| e.stack == q && e.service == svc) {
                if !e.message.starts_with(&own) {
                    seen(&e.message);
                }
            }
            *since = Some(last.max(after));
        };
        loop {
            relay(&mut since);
            let def = self.inner.ctl.definition(q)?;
            let rev = def.revision(svc)?;
            let replicas = def.service(svc)?.replicas();
            let st = self.inner.ctl.status(q)?;
            if let Some(s) = st.services.iter().find(|s| s.service == svc) {
                if s.rev == rev && s.replicas == replicas {
                    match s.state.as_str() {
                        "converged" => {
                            relay(&mut since);
                            return Ok((true, String::new()));
                        }
                        "paused" | "failing" => {
                            relay(&mut since);
                            return Ok((
                                false,
                                format!("{}: {}", s.state, s.message.clone().unwrap_or_default()),
                            ));
                        }
                        _ => {}
                    }
                }
            }
            if started.elapsed() >= self.inner.timeout {
                return Ok((
                    false,
                    format!("not converged after {:?}", self.inner.timeout),
                ));
            }
            std::thread::sleep(Duration::from_millis(500));
        }
    }

    pub(super) fn credentials(&self, org: &OrgId, auth: &GitAuth) -> Result<Credentials> {
        let read = |n: &str| {
            self.inner
                .secrets
                .get(org, n)
                .map(|(v, _)| v)
                .map_err(|e| Error::invalid(format!("git credential {n}: {e}")))
        };
        Ok(match auth {
            GitAuth::None => Credentials::None,
            GitAuth::Token {
                token_secret,
                username,
            } => Credentials::Token {
                username: username.clone().unwrap_or_else(|| "x-access-token".into()),
                token: String::from_utf8(read(token_secret)?)
                    .map_err(|_| Error::invalid("the git token is not text"))?
                    .trim()
                    .to_string(),
            },
            GitAuth::SshKey { ssh_key_secret } => Credentials::SshKey {
                private_key: read(ssh_key_secret)?,
            },
        })
    }

    /// Drop the oldest records beyond [`KEEP_DEPLOYMENTS`], never the
    /// current one.
    fn prune(&self, org: &OrgId, app: &App) {
        let Ok(rd) = std::fs::read_dir(self.deployments_dir(org, &app.spec.name)) else {
            return;
        };
        let mut ids: Vec<u64> = rd
            .flatten()
            .filter_map(|e| {
                e.file_name()
                    .into_string()
                    .ok()?
                    .strip_suffix(".json")?
                    .parse()
                    .ok()
            })
            .collect();
        ids.sort_unstable();
        let excess = ids.len().saturating_sub(KEEP_DEPLOYMENTS);
        for id in ids.into_iter().take(excess) {
            if Some(id) == app.current {
                continue;
            }
            let _ = std::fs::remove_file(self.dep_path(org, &app.spec.name, id));
            let _ = std::fs::remove_file(self.log_path(org, &app.spec.name, id));
        }
    }

    /// An event about an app on the daemon's feed, under its stack.
    pub(super) fn event(&self, org: &OrgId, app: &str, level: &str, message: String) {
        let stack = self
            .get(org, app)
            .ok()
            .and_then(|a| a.spec.stack().ok())
            .unwrap_or_default();
        let q = crate::stack::qualified(org, &stack);
        self.inner
            .ctl
            .note_service(level, &q, app, format!("app {app}: {message}"));
    }

    /// [`Apps::event`] with a kind ([`crate::stack::controller::Event::kind`]).
    fn kind_event(&self, org: &OrgId, app: &str, kind: &str, level: &str, message: String) {
        let stack = self
            .get(org, app)
            .ok()
            .and_then(|a| a.spec.stack().ok())
            .unwrap_or_default();
        let q = crate::stack::qualified(org, &stack);
        self.inner
            .ctl
            .event(kind, level, &q, app, format!("app {app}: {message}"));
    }

    // --- webhooks --------------------------------------------------------

    /// Answer `POST /api/v1/webhooks/<org>/<app>`: `(status, body)`. Nothing
    /// happens without a valid signature or token; an unknown org or app
    /// answers as a bad signature does, so the endpoint maps nothing.
    pub fn webhook(
        &self,
        org: &str,
        app: &str,
        header: &dyn Fn(&str) -> Option<String>,
        token: Option<&str>,
        body: &[u8],
    ) -> (u16, Value) {
        let refuse = |why: &str| (401, json!({"error": "unauthorized", "message": why}));
        let (Ok(org), Ok(())) = (OrgId::new(org), super::validate_app_name(app)) else {
            return refuse("invalid signature");
        };
        let Ok(found) = self.get(&org, app) else {
            return refuse("invalid signature");
        };
        let Ok((secret, _)) = self.inner.secrets.get(&org, &found.spec.webhook_secret()) else {
            return refuse("invalid signature");
        };
        let provider = match webhook::verify(trim_ascii(&secret), header, token, body) {
            Ok(p) => p,
            Err(webhook::Refusal::Missing) => {
                return refuse(
                    "sign the request (X-Hub-Signature-256, X-Gitea-Signature, X-Gitlab-Token) or pass ?token=",
                );
            }
            Err(webhook::Refusal::Invalid) => return refuse("invalid signature"),
        };
        if let Some(id) = webhook::delivery_id(header) {
            let key = format!("{org}/{app}/{id}");
            let mut seen = self.inner.deliveries.lock().unwrap();
            if seen.contains(&key) {
                return (200, json!({"ignored": "delivery already received"}));
            }
            if seen.len() >= 1000 {
                seen.pop_front();
            }
            seen.push_back(key);
        }
        let by = format!("webhook:{provider:?}").to_lowercase();
        let requested = match webhook::event(provider, header, body) {
            webhook::Event::Ping => return (200, json!({"ok": true, "ping": true})),
            webhook::Event::Other(kind) => {
                return (200, json!({"ignored": format!("event {kind}")}));
            }
            webhook::Event::Trigger => None,
            webhook::Event::PullRequest(pr) => {
                return self.preview_webhook(&org, &found, provider, &by, &pr);
            }
            webhook::Event::Push {
                reference,
                after,
                message,
                deleted,
            } => {
                if deleted {
                    return (200, json!({"ignored": format!("{reference} was deleted")}));
                }
                if let Source::Git(g) = &found.spec.source {
                    if !g.matches_push(&reference) {
                        return (
                            200,
                            json!({"ignored": format!("{reference} is not {}", g.reference)}),
                        );
                    }
                }
                let mut r = reference;
                if let Some(a) = after {
                    r = format!("{r} {a}");
                }
                if let Some(m) = message {
                    r = format!("{r}: {m}");
                }
                Some(r)
            }
        };
        match self.deploy(&org, app, Trigger::Webhook, &by, requested) {
            Ok(d) => (202, json!({"deployment": d.id, "status": d.status})),
            Err(e) => (500, json!({"error": "deploy", "message": e.to_string()})),
        }
    }
}

fn trim_ascii(b: &[u8]) -> &[u8] {
    let s = b
        .iter()
        .position(|c| !c.is_ascii_whitespace())
        .unwrap_or(b.len());
    let e = b
        .iter()
        .rposition(|c| !c.is_ascii_whitespace())
        .map_or(s, |i| i + 1);
    &b[s..e]
}

/// A deployment's log: a file, and each line on the events feed.
pub(super) struct DeployLog {
    file: std::fs::File,
    apps: Apps,
    org: OrgId,
    app: String,
    id: u64,
}

impl DeployLog {
    fn open(apps: &Apps, org: &OrgId, app: &str, id: u64) -> Result<DeployLog> {
        use std::os::unix::fs::OpenOptionsExt;
        let p = apps.log_path(org, app, id);
        if let Some(d) = p.parent() {
            std::fs::create_dir_all(d)?;
        }
        let file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .mode(0o600)
            .open(p)?;
        Ok(DeployLog {
            file,
            apps: apps.clone(),
            org: org.clone(),
            app: app.into(),
            id,
        })
    }

    pub(super) fn line(&mut self, l: &str) {
        let _ = writeln!(self.file, "{l}");
        self.apps
            .event(&self.org, &self.app, "log", format!("#{}: {l}", self.id));
    }
}

#[cfg(test)]
pub(super) mod tests {
    use super::*;
    use crate::app::EnvValue;
    use crate::image_check::Probe;

    #[test]
    fn state_machine() {
        use Status::*;
        let mut d = Deployment {
            id: 1,
            app: "web".into(),
            trigger: Trigger::Api,
            by: "t".into(),
            status: Queued,
            requested: None,
            rollback_of: None,
            commit: None,
            image: None,
            digest: None,
            error: None,
            created_at: 0,
            started_at: None,
            finished_at: None,
            rendered: None,
        };
        assert!(d.advance(Done).is_err(), "queued cannot jump to done");
        d.advance(Building).unwrap();
        assert!(d.started_at.is_some());
        assert!(
            d.advance(Superseded).is_err(),
            "only a waiting one is superseded"
        );
        d.advance(Deploying).unwrap();
        d.advance(Done).unwrap();
        assert!(d.finished_at.is_some());
        for s in [Queued, Building, Deploying, Done, Failed, Superseded] {
            assert!(!Done.can_become(s) && !Failed.can_become(s) && !Superseded.can_become(s));
        }
        assert!(Queued.can_become(Superseded));
        assert!(Building.can_become(Failed));
        let v = d.summary();
        assert!(v.get("rendered").is_none());
        assert_eq!(v["status"], "done");
        assert_eq!(v["trigger"], "api");
    }

    /// An `Apps` over a controller with no incusd behind it: everything up
    /// to the stack deploy works, which then fails.
    pub(in crate::app) fn apps(dir: &Path, gate: Arc<(Mutex<bool>, std::sync::Condvar)>) -> Apps {
        let k = crate::secrets::Keyring::new(age::x25519::Identity::generate(), vec![]);
        let secrets = Arc::new(Secrets::new(crate::secrets::LocalDriver::new(
            dir,
            Arc::new(k),
        )));
        let client = Client::with_socket("/nonexistent/isb-test/incus.sock");
        let store = crate::stack::Store::open(dir).unwrap();
        let ctl = Controller::start(
            client.clone(),
            store,
            Duration::from_secs(60),
            secrets.clone(),
        )
        .unwrap();
        // `docker:slow` holds its deploy in `building` until the gate opens.
        // `docker:traefik:whoami` is not on the registry; `docker:traefik/whoami` is.
        let probe: ImageProbe = Arc::new(move |image: &str, _| {
            if image == "docker:traefik:whoami" {
                return Probe::NotFound("manifest unknown".into());
            }
            if image == "docker:slow" {
                let (m, cv) = &*gate;
                let mut open = m.lock().unwrap();
                while !*open {
                    open = cv.wait(open).unwrap();
                }
            }
            Probe::Found(None)
        });
        Apps::new(dir, client, ctl, secrets).with_probe(probe)
    }

    fn spec(v: Value) -> AppSpec {
        serde_json::from_value(v).unwrap()
    }

    fn hdrs(h: &[(&str, &str)]) -> Box<webhook::Headers<'static>> {
        let h: Vec<(String, String)> = h
            .iter()
            .map(|(k, v)| (k.to_ascii_lowercase(), v.to_string()))
            .collect();
        Box::new(move |k: &str| {
            h.iter()
                .find(|(n, _)| *n == k.to_ascii_lowercase())
                .map(|(_, v)| v.clone())
        })
    }

    #[test]
    fn rollout_events_reach_the_deployment_log() {
        let dir = tempfile::tempdir().unwrap();
        let gate = Arc::new((Mutex::new(true), std::sync::Condvar::new()));
        let ap = apps(dir.path(), gate);
        let ctl = ap.controller();
        ctl.note_service(
            "info",
            "acme/shop-production",
            "web",
            "before the deploy".into(),
        );
        let mark = ap.event_mark();
        ctl.note_service(
            "info",
            "acme/shop-production",
            "web",
            "app web: #1: own line".into(),
        );
        ctl.note_service(
            "info",
            "acme/shop-production",
            "web",
            "rolling out rev 1 to 1 slot(s)".into(),
        );
        ctl.note_service(
            "info",
            "acme/shop-production",
            "api",
            "another service".into(),
        );
        ctl.note_service("info", "acme/other", "web", "another stack".into());
        let mut seen = Vec::new();
        // No definition here, so the wait itself fails, after relaying.
        let r = ap.wait_service_with("acme/shop-production", "web", Some(mark), |m| {
            seen.push(m.to_string())
        });
        assert!(r.is_err());
        assert_eq!(seen, ["rolling out rev 1 to 1 slot(s)"]);
        // Without a mark, nothing is relayed.
        let mut none = Vec::new();
        let _ = ap.wait_service_with("acme/shop-production", "web", None, |m| {
            none.push(m.to_string())
        });
        assert!(none.is_empty());
    }

    #[test]
    #[expect(
        clippy::too_many_lines,
        clippy::cognitive_complexity,
        reason = "predates the lint ratchet; split it when next changed"
    )]
    fn apps_records_webhooks_and_queue() {
        let dir = tempfile::tempdir().unwrap();
        let gate = Arc::new((Mutex::new(false), std::sync::Condvar::new()));
        let ap = apps(dir.path(), gate.clone());
        let org = OrgId::new("acme").unwrap();

        // Projects and environments.
        let p = ap.project_create(&org, "shop", "", &[]).unwrap();
        assert_eq!(p.environments, ["production"]);
        assert!(ap.project_create(&org, "shop", "", &[]).is_err());
        ap.environment_create(&org, "shop", "staging").unwrap();
        assert!(
            dir.path()
                .join("orgs/acme/apps/projects/shop.json")
                .is_file()
        );

        // An app naming a secret that does not exist is refused.
        let web = json!({
            "name": "web", "project": "shop",
            "source": {"image": "docker:traefik/whoami"},
            "env": "# greeting\nA=1\nT=${{secret.tok}}\n",
        });
        assert!(ap.create(&org, spec(web.clone())).is_err());
        ap.inner.secrets.set(&org, "tok", b"v").unwrap();
        let (app, secret) = ap.create(&org, spec(web.clone())).unwrap();
        assert_eq!(secret.len(), 64);
        assert_eq!(app.spec.environment, "production");
        assert!(ap.create(&org, spec(web)).is_err(), "names are unique");
        let mut bad = spec(
            json!({"name": "x", "project": "shop", "environment": "qa", "source": {"image": "x"}}),
        );
        assert!(ap.create(&org, bad.clone()).is_err(), "no such environment");
        bad.project = "nope".into();
        assert!(ap.create(&org, bad).is_err(), "no such project");

        // The env editor keeps comments and never shows a secret's value.
        let text = ap.env_get(&org, "web").unwrap();
        assert_eq!(text, "# greeting\nA=1\nT=${{secret.tok}}\n");
        ap.env_set(
            &org,
            "web",
            "# greeting\nA=2\nT=${{secret.tok}}\nB=\"two words\"\n",
        )
        .unwrap();
        assert!(
            ap.env_get(&org, "web")
                .unwrap()
                .contains("A=2\nT=${{secret.tok}}\nB=\"two words\"")
        );
        assert!(ap.env_set(&org, "web", "T=${{secret.missing}}\n").is_err());
        let u = ap
            .update(&org, "web", &json!({"replicas": 3, "port": 80}))
            .unwrap();
        assert_eq!(u.spec.replicas, 3);
        assert_eq!(u.spec.env.get("A"), Some(&EnvValue::Plain("2".into())));
        assert!(
            ap.update(&org, "web", &json!({"project": "other"}))
                .is_err()
        );
        assert!(
            ap.update(&org, "web", &json!({"port": null}))
                .unwrap()
                .spec
                .port
                .is_none()
        );
        assert!(ap.rollback(&org, "web", None, Trigger::Api, "t").is_err());

        // Webhooks: refused without the secret, whatever the app.
        let push = br#"{"ref":"refs/heads/main","after":"abc","head_commit":{"message":"m"}}"#;
        let none = hdrs(&[]);
        assert_eq!(ap.webhook("acme", "web", &none, None, push).0, 401);
        assert_eq!(ap.webhook("acme", "web", &none, Some("wrong"), push).0, 401);
        assert_eq!(
            ap.webhook("acme", "nope", &none, Some(&secret), push).0,
            401
        );
        assert_eq!(
            ap.webhook("Bad Org", "web", &none, Some(&secret), push).0,
            401
        );
        let forged = hdrs(&[
            ("X-GitHub-Event", "push"),
            (
                "X-Hub-Signature-256",
                &format!("sha256={}", webhook::sign(b"other", push)),
            ),
        ]);
        assert_eq!(ap.webhook("acme", "web", &forged, None, push).0, 401);
        assert!(ap.deployments(&org, "web").unwrap().is_empty());
        let signed = |event: &str, delivery: &str, body: &[u8]| {
            hdrs(&[
                ("X-GitHub-Event", event),
                ("X-GitHub-Delivery", delivery),
                (
                    "X-Hub-Signature-256",
                    &format!("sha256={}", webhook::sign(secret.as_bytes(), body)),
                ),
            ])
        };
        let (st, v) = ap.webhook("acme", "web", &signed("ping", "d0", b"{}"), None, b"{}");
        assert_eq!((st, v["ping"].as_bool()), (200, Some(true)));
        let (st, v) = ap.webhook("acme", "web", &signed("issues", "d1", push), None, push);
        assert_eq!(st, 200);
        assert!(v["ignored"].as_str().unwrap().contains("issues"), "{v}");
        let (st, v) = ap.webhook("acme", "web", &signed("push", "d2", push), None, push);
        assert_eq!(st, 202, "{v}");
        assert_eq!(v["deployment"], 1);
        // The same delivery again is not a second deploy.
        let (st, v) = ap.webhook("acme", "web", &signed("push", "d2", push), None, push);
        assert_eq!(st, 200, "{v}");
        let d = ap.wait(&org, "web", 1, Duration::from_secs(30)).unwrap();
        assert_eq!(d.trigger, Trigger::Webhook);
        assert_eq!(d.by, "webhook:github");
        assert_eq!(d.requested.as_deref(), Some("refs/heads/main abc: m"));
        // No incusd here: the stack deploy fails, and says so.
        assert_eq!(d.status, Status::Failed, "{d:?}");
        assert!(d.image.is_some());
        let (log, _, done) = ap.log(&org, "web", 1, 0).unwrap();
        assert!(done && log.contains("image docker:traefik/whoami"), "{log}");
        // The record comes with the text, so one call follows both.
        let (rest, next, rec) = ap.log_and_record(&org, "web", 1, 4).unwrap();
        assert_eq!((rec.id, rec.status), (1, Status::Failed));
        assert_eq!(next as usize, log.len());
        assert_eq!(rest, log[4..]);
        assert!(rec.summary().get("rendered").is_none());
        let ds = ap.deployments(&org, "web").unwrap();
        assert_eq!(ds.len(), 1);

        // A git app deploys only on pushes to its branch.
        ap.inner.secrets.set(&org, "gh", b"t").unwrap();
        let (_, gsecret) = ap
            .create(
                &org,
                spec(json!({
                    "name": "api", "project": "shop", "environment": "staging",
                    "source": {"git": {"url": "https://example.invalid/o/r.git", "ref": "main", "auth": {"token_secret": "gh"}}},
                    "build": {"builder": {"type": "railpack"}},
                })),
            )
            .unwrap();
        let dev = br#"{"ref":"refs/heads/dev","after":"abc"}"#;
        let gl = |body: &[u8]| {
            let _ = body;
            hdrs(&[
                ("X-Gitlab-Event", "Push Hook"),
                ("X-Gitlab-Token", &gsecret),
            ])
        };
        let (st, v) = ap.webhook("acme", "api", &gl(dev), None, dev);
        assert_eq!(st, 200);
        assert!(
            v["ignored"].as_str().unwrap().contains("refs/heads/dev"),
            "{v}"
        );
        assert!(ap.deployments(&org, "api").unwrap().is_empty());
        let (st, _) = ap.webhook("acme", "api", &gl(push), None, push);
        assert_eq!(st, 202);
        let d = ap.wait(&org, "api", 1, Duration::from_secs(60)).unwrap();
        assert_eq!(d.status, Status::Failed);
        assert!(d.error.as_deref().unwrap_or("").contains("git"), "{d:?}");
        // A rotated secret: the old one stops working.
        let s2 = ap.webhook_secret(&org, "api", true).unwrap();
        assert_ne!(s2, gsecret);
        assert_eq!(ap.webhook("acme", "api", &gl(push), None, push).0, 401);

        // The queue: one at a time; a newer request replaces a waiting one.
        ap.create(
            &org,
            spec(json!({"name": "slow", "project": "shop", "source": {"image": "docker:slow"}})),
        )
        .unwrap();
        let d1 = ap.deploy(&org, "slow", Trigger::Api, "t", None).unwrap();
        let started = Instant::now();
        while ap.deployment(&org, "slow", d1.id).unwrap().status != Status::Building {
            assert!(started.elapsed() < Duration::from_secs(10));
            std::thread::sleep(Duration::from_millis(20));
        }
        let d2 = ap.deploy(&org, "slow", Trigger::Api, "t", None).unwrap();
        let d3 = ap.deploy(&org, "slow", Trigger::Manual, "t", None).unwrap();
        assert_eq!(
            ap.deployment(&org, "slow", d2.id).unwrap().status,
            Status::Superseded
        );
        assert_eq!(
            ap.deployment(&org, "slow", d3.id).unwrap().status,
            Status::Queued
        );
        assert!(ap.delete(&org, "slow").is_err(), "not while deploying");
        {
            let (m, cv) = &*gate;
            *m.lock().unwrap() = true;
            cv.notify_all();
        }
        let d3 = ap
            .wait(&org, "slow", d3.id, Duration::from_secs(30))
            .unwrap();
        assert!(d3.status.finished());
        assert!(
            ap.wait(&org, "slow", d1.id, Duration::from_secs(1))
                .unwrap()
                .status
                .finished()
        );

        // Deleting: projects with apps stay; an app takes its secrets along.
        assert!(ap.project_delete(&org, "shop").is_err());
        assert!(ap.environment_delete(&org, "shop", "staging").is_err());
        let started = Instant::now();
        for a in ["web", "api", "slow"] {
            while let Err(e) = ap.delete(&org, a) {
                assert!(started.elapsed() < Duration::from_secs(10), "{a}: {e}");
                std::thread::sleep(Duration::from_millis(50));
            }
        }
        assert!(ap.inner.secrets.inspect(&org, "app.web.webhook").is_err());
        assert!(
            ap.inner.secrets.inspect(&org, "tok").is_ok(),
            "not isb's to delete"
        );
        ap.environment_delete(&org, "shop", "staging").unwrap();
        ap.project_delete(&org, "shop").unwrap();
        assert!(ap.project_list(&org).unwrap().is_empty());
    }

    #[test]
    #[expect(
        clippy::too_many_lines,
        clippy::cognitive_complexity,
        reason = "predates the lint ratchet; split it when next changed"
    )]
    fn previews_from_webhooks() {
        let dir = tempfile::tempdir().unwrap();
        let gate = Arc::new((Mutex::new(true), std::sync::Condvar::new()));
        let ap = apps(dir.path(), gate);
        let org = OrgId::new("acme").unwrap();
        ap.project_create(&org, "shop", "", &[]).unwrap();
        assert!(
            ap.environment_create(&org, "shop", "prod-pr-1").is_err(),
            "preview stack names are kept"
        );
        let (_, secret) = ap
            .create(
                &org,
                spec(json!({
                    "name": "web", "project": "shop",
                    "source": {"git": {"url": "https://example.invalid/acme/web.git", "ref": "main"}},
                    "build": {"builder": {"type": "dockerfile"}},
                    "port": 8080,
                })),
            )
            .unwrap();
        let pr = |action: &str, n: u64, head_repo: &str, base: &str| {
            format!(
                r#"{{"action":"{action}","number":{n},"pull_request":{{"title":"t{n}","merged":false,
                "head":{{"ref":"f{n}","sha":"{}","repo":{{"full_name":"{head_repo}"}}}},
                "base":{{"ref":"{base}","repo":{{"full_name":"acme/web"}}}}}}}}"#,
                "a".repeat(40)
            )
            .into_bytes()
        };
        let mut delivery = 0;
        let mut send = |body: &[u8]| {
            delivery += 1;
            let h = hdrs(&[
                ("X-Gitea-Event", "pull_request"),
                ("X-Gitea-Delivery", &format!("p{delivery}")),
                ("X-Gitea-Signature", &webhook::sign(secret.as_bytes(), body)),
            ]);
            ap.webhook("acme", "web", &h, None, body)
        };
        // Previews are opt-in.
        let (st, v) = send(&pr("opened", 1, "acme/web", "main"));
        assert_eq!(st, 200);
        assert!(v["ignored"].as_str().unwrap().contains("off"), "{v}");
        ap.update(
            &org,
            "web",
            &json!({"previews": {"enabled": true, "max": 2, "env": "MODE=preview\n"}}),
        )
        .unwrap();
        // Another base branch, a fork: ignored.
        let (st, v) = send(&pr("opened", 1, "acme/web", "dev"));
        assert_eq!(st, 200);
        assert!(
            v["ignored"].as_str().unwrap().contains("targets dev"),
            "{v}"
        );
        let (st, v) = send(&pr("opened", 1, "mallory/web", "main"));
        assert_eq!(st, 200);
        assert!(v["ignored"].as_str().unwrap().contains("fork"), "{v}");
        assert!(ap.preview_list(&org, "web").unwrap().is_empty());
        // Opened: a preview and a deploy (which fails here: no git host).
        let (st, v) = send(&pr("opened", 1, "acme/web", "main"));
        assert_eq!(st, 202, "{v}");
        assert_eq!(
            (v["preview"].as_u64(), v["deployment"].as_u64()),
            (Some(1), Some(1))
        );
        let p = ap.preview_get(&org, "web", 1).unwrap();
        assert_eq!(p.stack, "shop-production-pr-1");
        assert_eq!((p.provider.as_str(), p.fork), ("gitea", false));
        let d = ap
            .preview_wait(&org, "web", 1, 1, Duration::from_secs(60))
            .unwrap();
        assert_eq!(d.status, Status::Failed, "{d:?}");
        let (log, _, done) = ap.preview_log(&org, "web", 1, 1, 0).unwrap();
        assert!(done && log.contains("refs/pull/1/head"), "{log}");
        // Synchronize to the commit it has (Gitea sends one after opening):
        // nothing; to a new one: the same preview, a second deployment.
        let (st, v) = send(&pr("synchronize", 1, "acme/web", "main"));
        assert_eq!(st, 200, "{v}");
        assert!(v["ignored"].as_str().unwrap().contains("already"), "{v}");
        let moved = String::from_utf8(pr("synchronize", 1, "acme/web", "main"))
            .unwrap()
            .replace(&"a".repeat(40), &"b".repeat(40));
        let (st, v) = send(moved.as_bytes());
        assert_eq!((st, v["deployment"].as_u64()), (202, Some(2)), "{v}");
        ap.preview_wait(&org, "web", 1, 2, Duration::from_secs(60))
            .unwrap();
        // The limit: two at once.
        assert_eq!(send(&pr("opened", 2, "acme/web", "main")).0, 202);
        let (st, v) = send(&pr("opened", 3, "acme/web", "main"));
        assert_eq!(st, 200);
        assert!(v["ignored"].as_str().unwrap().contains("2 previews"), "{v}");
        assert!(ap.preview_get(&org, "web", 3).is_err());
        ap.preview_wait(&org, "web", 2, 1, Duration::from_secs(60))
            .unwrap();
        // Forks when allowed: marked, so they build in a VM without the
        // app's secrets.
        ap.update(&org, "web", &json!({"previews": {"forks": true, "max": 5}}))
            .unwrap();
        assert_eq!(send(&pr("opened", 4, "mallory/web", "main")).0, 202);
        assert!(ap.preview_get(&org, "web", 4).unwrap().fork);
        ap.preview_wait(&org, "web", 4, 1, Duration::from_secs(60))
            .unwrap();
        // Closed: removed with its records.
        let (st, v) = send(&pr("closed", 1, "acme/web", "main"));
        assert_eq!(st, 202, "{v}");
        let started = Instant::now();
        while ap.preview_get(&org, "web", 1).is_ok() {
            assert!(started.elapsed() < Duration::from_secs(30));
            std::thread::sleep(Duration::from_millis(50));
        }
        assert!(!dir.path().join("orgs/acme/apps/web/previews/1").exists());
        let (st, v) = send(&pr("closed", 1, "acme/web", "main"));
        assert_eq!(st, 200, "{v}");
        // The tools' path: redeploy, then delete.
        let d = ap
            .preview_redeploy(&org, "web", 2, Trigger::Api, "t")
            .unwrap();
        assert_eq!(d.id, 2);
        ap.preview_wait(&org, "web", 2, 2, Duration::from_secs(60))
            .unwrap();
        ap.preview_remove(&org, "web", 2, "test", true).unwrap();
        assert!(ap.preview_get(&org, "web", 2).is_err());
        // Deleting the app takes its previews along first; here the fork's
        // build cache cannot be checked without incusd, so it stops there.
        assert!(ap.delete(&org, "web").is_err());
        assert!(ap.get(&org, "web").is_ok());
        assert!(ap.preview_get(&org, "web", 4).unwrap().removing);
    }
}