moadim 1.7.4

Loop engine for AI agents — routines over REST, MCP, and a built-in web UI
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
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
{
  "openapi": "3.1.0",
  "info": {
    "title": "Moadim Server API",
    "description": "REST API for managing routines",
    "license": {
      "name": "MIT",
      "identifier": "MIT"
    },
    "version": "1.7.4"
  },
  "servers": [
    {
      "url": "/api/v1",
      "description": "This server"
    }
  ],
  "paths": {
    "/agents": {
      "get": {
        "tags": [
          "crate::routes::list_agents"
        ],
        "summary": "`GET /agents` — list the agent registry keys a routine may target.",
        "operationId": "list_agents",
        "responses": {
          "200": {
            "description": "Available agent names",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/config/max-concurrent-runs": {
      "get": {
        "tags": [
          "crate::routes::http"
        ],
        "summary": "`GET /config/max-concurrent-runs` — the global routine concurrency cap (issue #1155).",
        "operationId": "get_max_concurrent_runs",
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MaxConcurrentRunsResponse"
                }
              }
            }
          }
        }
      },
      "put": {
        "tags": [
          "crate::routes::http"
        ],
        "summary": "`PUT /config/max-concurrent-runs` — set or clear the persisted concurrency-cap override.",
        "description": "Writes to `machine.local.toml`. The `MOADIM_MAX_CONCURRENT_RUNS` env var still takes\nprecedence at runtime when set; this only takes effect while the env var is unset. Takes\neffect on the next trigger check (read live from disk, like the env var today) — no restart\nrequired. Returns `500` if the write fails.",
        "operationId": "put_max_concurrent_runs",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SetMaxConcurrentRunsRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MaxConcurrentRunsResponse"
                }
              }
            }
          },
          "500": {
            "description": "Write failed"
          }
        }
      }
    },
    "/config/user-prompt": {
      "get": {
        "tags": [
          "crate::routes::http"
        ],
        "summary": "`GET /config/user-prompt` — the persistent system prompt appended to every routine's agent\ninstructions file (see [`crate::paths::user_prompt_path`]), as plain text. Empty (not an\nerror) when nothing has been saved yet.",
        "operationId": "get_user_prompt",
        "responses": {
          "200": {
            "description": "User prompt contents as plain text"
          }
        }
      },
      "put": {
        "tags": [
          "crate::routes::http"
        ],
        "summary": "`PUT /config/user-prompt` — replace the persistent system prompt.",
        "description": "Creates the config directory if absent. Every routine's next run picks up the change (the\nlaunch command re-reads this file each time — see `command::system_prompt_stmts`); already\nrunning agents are unaffected.",
        "operationId": "put_user_prompt",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SetUserPromptRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "204": {
            "description": "Saved"
          },
          "500": {
            "description": "Write failed"
          }
        }
      }
    },
    "/health": {
      "get": {
        "tags": [
          "crate::routes::health"
        ],
        "summary": "`GET /health` — health check with uptime.",
        "operationId": "health",
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HealthResponse"
                }
              }
            }
          }
        }
      }
    },
    "/machine": {
      "get": {
        "tags": [
          "crate::routes::http"
        ],
        "summary": "`GET /machine` — the current machine's resolved identity.",
        "description": "Returns the name this daemon uses to match `machines[]` targeting lists on routines. Useful for\nclients (e.g. the UI) that want to default their views to local entries only.",
        "operationId": "get_current_machine",
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MachineResponse"
                }
              }
            }
          }
        }
      },
      "put": {
        "tags": [
          "crate::routes::http"
        ],
        "summary": "`PUT /machine` — rename this machine's identity.",
        "description": "Writes the new name to `machine.local.toml` and returns it trimmed. Returns `400` if the name\nis empty, `500` if the write fails. The `MOADIM_MACHINE` env var takes precedence at runtime;\nsetting the name here persists it for when the env var is absent.\n\nAs a side-effect, every routine whose `machines` list contained the old name is updated in\nmemory, on disk, and in the crontab so that the rename propagates atomically.",
        "operationId": "put_machine",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SetMachineRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MachineResponse"
                }
              }
            }
          },
          "400": {
            "description": "Empty name"
          },
          "500": {
            "description": "Write failed"
          }
        }
      }
    },
    "/machines": {
      "get": {
        "tags": [
          "crate::routes::http"
        ],
        "summary": "`GET /machines` — distinct machine names this daemon knows about.",
        "description": "There is no central machine registry, so the \"known\" set is the union of every `machines`\ntargeting list declared by a routine, plus this machine's own resolved identity\n([`crate::machine::current_machine`]) so the local machine is always pickable even before\nanything targets it. Sorted and de-duplicated. Backs the UI machine picker; mirrors the\n`moadim machine list` CLI but reads the live in-memory store instead of disk.",
        "operationId": "list_machines",
        "responses": {
          "200": {
            "description": "Known machine names, sorted",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/metrics": {
      "get": {
        "tags": [
          "crate::routes::metrics"
        ],
        "summary": "`GET /api/v1/metrics` — Prometheus text-exposition metrics. See the module doc for how each\nseries is derived; `/health` remains the cheap liveness probe, this is the richer surface.",
        "operationId": "metrics",
        "responses": {
          "200": {
            "description": "Prometheus text exposition format (version 0.0.4)",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },
    "/restart": {
      "post": {
        "tags": [
          "crate::routes::restart"
        ],
        "summary": "`POST /restart` — stop this server and start a fresh instance.",
        "description": "The running server cannot rebind its own port, so it spawns a detached `moadim restart` helper\nthat stops it and starts a new process, mirroring the `moadim restart` CLI command and the\n`restart` MCP tool. Responds with the helper's PID before the restart completes.",
        "operationId": "restart",
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RestartResponse"
                }
              }
            }
          },
          "500": {
            "description": "could not spawn the restart helper"
          }
        }
      }
    },
    "/routines": {
      "get": {
        "tags": [
          "crate::routes::list_routines"
        ],
        "summary": "`GET /routines` — list routines, optionally filtered and sorted by repository.",
        "operationId": "list_routines",
        "parameters": [
          {
            "name": "repository",
            "in": "query",
            "description": "Keep only routines with at least one repository whose URL contains this\nsubstring (case-insensitive). Empty or absent keeps every routine.",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "sort",
            "in": "query",
            "description": "Field to sort by (default: creation time).",
            "required": false,
            "schema": {
              "$ref": "#/components/schemas/RoutineSort"
            }
          },
          {
            "name": "order",
            "in": "query",
            "description": "Sort direction (default: ascending).",
            "required": false,
            "schema": {
              "$ref": "#/components/schemas/SortOrder"
            }
          },
          {
            "name": "local_only",
            "in": "query",
            "description": "When `true`, only return routines whose `machines` list includes the current machine.\nDefaults to `false` (return all routines, preserving backwards compatibility).",
            "required": false,
            "schema": {
              "type": "boolean"
            }
          },
          {
            "name": "include_prompts",
            "in": "query",
            "description": "When `true`, include each routine's `prompt` in the response. Defaults to `false`:\nthe prompt (often the largest field) is omitted so listings stay compact. Fetch a\nsingle routine with `svc_get` / `GET /routines/{id}` to always see its prompt.",
            "required": false,
            "schema": {
              "type": "boolean"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/RoutineResponse"
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": [
          "crate::routes::create_routine"
        ],
        "summary": "`POST /routines` — create a new routine.",
        "operationId": "create_routine",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateRoutineRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RoutineResponse"
                }
              }
            }
          },
          "400": {
            "description": "Invalid cron expression"
          }
        }
      }
    },
    "/routines.ics": {
      "get": {
        "tags": [
          "crate::routines"
        ],
        "summary": "`GET /routines.ics` — iCalendar feed of every enabled routine's upcoming fire times.",
        "description": "Returns a `text/calendar` body suitable for subscribing to in an external calendar\n(Google Calendar, Apple Calendar, …) so upcoming runs show up alongside other events.\nWith `?routine=<id>` the feed is scoped to a single routine (named after it); without\nit every enabled routine is rendered (issue #263).",
        "operationId": "ical_feed",
        "parameters": [
          {
            "name": "routine",
            "in": "query",
            "description": "Render only the fire times of the routine with this UUID. Absent (the default)\nrenders every enabled routine. An unknown or disabled id yields a well-formed\nempty calendar.",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "iCalendar (text/calendar) feed of upcoming routine fire times"
          }
        }
      }
    },
    "/routines/cleanup": {
      "post": {
        "tags": [
          "crate::routes::cleanup_workbenches"
        ],
        "summary": "`POST /routines/cleanup` — reap finished, expired run workbenches on demand.",
        "operationId": "cleanup_workbenches",
        "responses": {
          "200": {
            "description": "Workbenches removed and bytes freed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CleanupResponse"
                }
              }
            }
          }
        }
      }
    },
    "/routines/lock": {
      "get": {
        "tags": [
          "crate::routes::get_lock_status"
        ],
        "summary": "`GET /routines/lock` — return the current global lock status.",
        "operationId": "get_lock_status",
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/LockStatus"
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": [
          "crate::routes::lock_routines"
        ],
        "summary": "`POST /routines/lock` — create a lock sentinel, halting all routine scheduling and triggers.",
        "operationId": "lock_routines",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/LockRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/LockStatus"
                }
              }
            }
          },
          "400": {
            "description": "Unknown scope"
          },
          "500": {
            "description": "IO error"
          }
        }
      },
      "delete": {
        "tags": [
          "crate::routes::unlock_routines"
        ],
        "summary": "`DELETE /routines/lock` — remove lock sentinel(s), restoring routine scheduling.",
        "operationId": "unlock_routines",
        "parameters": [
          {
            "name": "scope",
            "in": "query",
            "description": "Which sentinel(s) to remove: `\"shared\"`, `\"local\"`, or `\"all\"`.",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/LockStatus"
                }
              }
            }
          },
          "400": {
            "description": "Unknown scope"
          },
          "500": {
            "description": "IO error"
          }
        }
      }
    },
    "/routines/runs": {
      "get": {
        "tags": [
          "crate::routines"
        ],
        "summary": "`GET /routines/runs` — the most recent runs across every routine, newest first. Backs the\noverview \"recent runs\" panel with a single workbench scan instead of one request per routine.",
        "operationId": "get_all_runs",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "description": "Cap on the number of runs returned (default: `DEFAULT_FLEET_RUNS_LIMIT`).",
            "required": false,
            "schema": {
              "type": [
                "integer",
                "null"
              ],
              "minimum": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/FleetRunSummary"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/routines/{id}": {
      "get": {
        "tags": [
          "crate::routes::get_routine"
        ],
        "summary": "`GET /routines/{id}` — retrieve a single routine by UUID.",
        "operationId": "get_routine",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RoutineResponse"
                }
              }
            }
          },
          "404": {
            "description": "Not found"
          }
        }
      },
      "put": {
        "tags": [
          "crate::routes::update_routine"
        ],
        "summary": "`PUT /routines/{id}` — alias for `PATCH`: a partial-merge update, not a full replace.",
        "description": "Fields omitted from the body are retained from the existing record, exactly as with `PATCH`.\nA client expecting RFC 7231 full-resource-replacement semantics (omitted fields reset to\ndefault) should not rely on this route for that; use `PATCH` and set every field explicitly.",
        "operationId": "replace",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/UpdateRoutineRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RoutineResponse"
                }
              }
            }
          },
          "400": {
            "description": "Invalid"
          },
          "404": {
            "description": "Not found"
          }
        }
      },
      "delete": {
        "tags": [
          "crate::routes::delete_routine"
        ],
        "summary": "`DELETE /routines/{id}` — delete a routine by UUID.",
        "operationId": "delete_routine",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RoutineResponse"
                }
              }
            }
          },
          "404": {
            "description": "Not found"
          }
        }
      },
      "patch": {
        "tags": [
          "crate::routes::update_routine"
        ],
        "summary": "`PATCH /routines/{id}` — partially update a routine.",
        "operationId": "update_routine",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/UpdateRoutineRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RoutineResponse"
                }
              }
            }
          },
          "400": {
            "description": "Invalid"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/routines/{id}/flags": {
      "get": {
        "tags": [
          "crate::routes::list_flags"
        ],
        "summary": "`GET /routines/{id}/flags` — list open flags raised against a routine.",
        "operationId": "list_flags",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Flag"
                  }
                }
              }
            }
          },
          "404": {
            "description": "Not found"
          }
        }
      },
      "post": {
        "tags": [
          "crate::routes::create_flag"
        ],
        "summary": "`POST /routines/{id}/flags` — raise a new flag against a routine.",
        "operationId": "create_flag",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateFlagRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Flag"
                }
              }
            }
          },
          "400": {
            "description": "Invalid type/description/scope"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/routines/{id}/flags/{filename}": {
      "delete": {
        "tags": [
          "crate::routes::resolve_flag"
        ],
        "summary": "`DELETE /routines/{id}/flags/{filename}` — resolve (delete) a flag.",
        "operationId": "resolve_flag",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "filename",
            "in": "path",
            "description": "Flag filename, as returned by create/list",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Resolved"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/routines/{id}/logs": {
      "get": {
        "tags": [
          "crate::routines"
        ],
        "summary": "`GET /routines/{id}/logs` — return the newest workbench `agent.log` as plain text.",
        "operationId": "get_logs",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Log file contents as plain text"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/routines/{id}/prompt-preview": {
      "get": {
        "tags": [
          "crate::routines"
        ],
        "summary": "`GET /routines/{id}/prompt-preview` — the exact prompt body a run would receive, computed\nin-memory with no workbench, `prompt.md` write, or agent launch (issue #391). Includes the\nroutine-origin disclosure because it now lives in the composed prompt.",
        "operationId": "get_prompt_preview",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Composed prompt body as plain text"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/routines/{id}/runs": {
      "get": {
        "tags": [
          "crate::routes::list_routine_runs"
        ],
        "summary": "`GET /routines/{id}/runs` — list every run workbench for the routine, newest first.",
        "operationId": "list_routine_runs",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/RunSummary"
                  }
                }
              }
            }
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/routines/{id}/runs/{workbench}/log": {
      "get": {
        "tags": [
          "crate::routines"
        ],
        "summary": "`GET /routines/{id}/runs/{workbench}/log` — return one specific run's `agent.log` as plain text.",
        "operationId": "get_run_log",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "workbench",
            "in": "path",
            "description": "Workbench directory name (`{slug}-{unix_secs}`), from `GET /routines/{id}/runs`",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Log file contents as plain text"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/routines/{id}/runs/{workbench}/summary": {
      "get": {
        "tags": [
          "crate::routines"
        ],
        "summary": "`GET /routines/{id}/runs/{workbench}/summary` — return one specific run's agent-authored\n`summary.md` as plain text (empty when the agent hasn't written one).",
        "operationId": "get_run_summary",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "workbench",
            "in": "path",
            "description": "Workbench directory name (`{slug}-{unix_secs}`), from `GET /routines/{id}/runs`",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Summary file contents as plain text"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/routines/{id}/scheduled-trigger": {
      "post": {
        "tags": [
          "crate::routines"
        ],
        "summary": "`POST /routines/{id}/scheduled-trigger` — run a routine on its schedule.",
        "description": "The daemon-side endpoint the generated crontab line invokes (`moadim schedule trigger <id>`).\nUnlike [`crate::routes::trigger_routine::trigger_routine`] it does not record a manual\ntrigger; the spawned command records the scheduled timestamp itself. See\n[`svc_trigger_scheduled`].",
        "operationId": "scheduled_trigger",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Routine"
                }
              }
            }
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/routines/{id}/trigger": {
      "post": {
        "tags": [
          "crate::routes::trigger_routine"
        ],
        "summary": "`POST /routines/{id}/trigger` — manually trigger a routine outside its schedule.",
        "description": "Refuses (423, distinct message) when the routine is disabled or in power-saving mode. See\n[`crate::routines::svc_trigger`].",
        "operationId": "trigger_routine",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Routine UUID",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Routine"
                }
              }
            }
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/shutdown": {
      "post": {
        "tags": [
          "crate::routes::shutdown"
        ],
        "summary": "`POST /shutdown` — ask the server to stop gracefully.",
        "description": "Used by the UI \"STOP\" button (and the `moadim stop` command) to kill a backgrounded server that\nhas no controlling terminal. The response is sent before the graceful shutdown completes.",
        "operationId": "shutdown",
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ShutdownResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "CleanupResponse": {
        "type": "object",
        "description": "Result of an on-demand workbench cleanup sweep.",
        "required": [
          "removed",
          "freed_bytes"
        ],
        "properties": {
          "freed_bytes": {
            "type": "integer",
            "format": "int64",
            "description": "Total disk space reclaimed, in bytes, summed across the removed workbench trees. Additive\nfield: existing `{\"removed\": N}` consumers are unaffected.",
            "minimum": 0
          },
          "removed": {
            "type": "integer",
            "description": "Number of finished, expired run workbenches removed by this sweep.",
            "minimum": 0
          }
        }
      },
      "CreateFlagRequest": {
        "type": "object",
        "description": "Request body for `POST /routines/{id}/flags`.",
        "required": [
          "type",
          "description",
          "scope"
        ],
        "properties": {
          "description": {
            "type": "string",
            "description": "Free-text description of what's unclear."
          },
          "scope": {
            "type": "string",
            "description": "`\"general\"` (committed, shared via git) or `\"local\"` (gitignored, machine-local)."
          },
          "type": {
            "type": "string",
            "description": "Free-text flag category. Common examples: `\"bug\"`, `\"gap\"`, `\"edge_case\"`, `\"question\"`,\n`\"blocker\"` — any string is accepted."
          }
        }
      },
      "CreateRoutineRequest": {
        "type": "object",
        "description": "Request body for creating a new routine.",
        "required": [
          "schedule",
          "title",
          "agent",
          "prompt"
        ],
        "properties": {
          "agent": {
            "type": "string",
            "description": "Agent registry key to launch."
          },
          "enabled": {
            "type": "boolean",
            "description": "Whether to create the routine enabled (defaults to `true`)."
          },
          "env": {
            "type": "object",
            "description": "Non-secret environment variables to inject into the agent's shell session at launch,\nwritten to `routine.toml`'s `[env]` table (defaults to empty). Keys must match\n`[A-Za-z_][A-Za-z0-9_]*`; values must not contain newlines (#408). For secrets, edit the\ngitignored `routine.local.toml` sidecar directly on disk instead — it is never accepted\nover the API.",
            "additionalProperties": {
              "type": "string"
            },
            "propertyNames": {
              "type": "string"
            }
          },
          "goal": {
            "type": [
              "string",
              "null"
            ],
            "description": "A very short (at most 5 lines) statement of the routine's goal. Optional; `None` leaves it\nunset. When present it is rendered into the agent's `prompt.md` as a `## Goal` preamble."
          },
          "machines": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Machines to run this routine on (defaults to empty = runs nowhere until assigned)."
          },
          "max_runtime_secs": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Max wall-clock seconds a run may execute before the watchdog kills its hung\nsession. `None` uses the default cap (`MAX_RUNTIME_SECS`). Must be greater\nthan zero when set; `0` is rejected (#233).",
            "minimum": 0
          },
          "model": {
            "type": [
              "string",
              "null"
            ],
            "description": "Model ID to run the agent with, or `None` to use the agent's own default. A\nblank/whitespace-only value is treated the same as `None`."
          },
          "prompt": {
            "type": "string",
            "description": "Task prompt."
          },
          "repositories": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Repository"
            },
            "description": "Repositories to list as context (defaults to empty)."
          },
          "schedule": {
            "type": "string",
            "description": "Cron expression for the new routine. Evaluated in the host's local system\ntimezone (the OS crontab timezone), not UTC."
          },
          "tags": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Free-form labels for the routine (defaults to empty). Each entry is trimmed\nand must be non-blank."
          },
          "title": {
            "type": "string",
            "description": "Human name for the routine."
          },
          "ttl_secs": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Workbench retention in seconds for finished runs; caps the cron-derived\nretention lower. `None` uses `min(MAX_TTL_SECS, cron interval)`. Must be\ngreater than zero when set; `0` is rejected (#233).",
            "minimum": 0
          }
        }
      },
      "DependencyHealth": {
        "type": "object",
        "description": "External-binary dependencies the daemon relies on at runtime, and whether each is resolvable on\nthe daemon's `PATH`. Surfaced in [`HealthResponse`] so the UI/CLI can flag a missing dependency\ninstead of having routine runs silently no-op.",
        "required": [
          "tmux",
          "python3"
        ],
        "properties": {
          "python3": {
            "type": "boolean",
            "description": "Whether `python3` resolves on the daemon's `PATH`. The built-in `claude` agent's `setup`\nstep runs a `python3` snippet to pre-seed workspace-trust state; when it is missing that\nstep fails silently and the routine still shows a healthy status (issue #404)."
          },
          "tmux": {
            "type": "boolean",
            "description": "Whether `tmux` (used to launch every routine agent) resolves on the daemon's `PATH`."
          }
        }
      },
      "Flag": {
        "type": "object",
        "description": "A single flag raised against a routine.",
        "required": [
          "filename",
          "type",
          "description",
          "scope",
          "created_at"
        ],
        "properties": {
          "created_at": {
            "type": "integer",
            "format": "int64",
            "description": "Unix timestamp (seconds) the flag was created.",
            "minimum": 0
          },
          "description": {
            "type": "string",
            "description": "Free-text body describing what's unclear."
          },
          "filename": {
            "type": "string",
            "description": "Filename on disk under the routine's `flags/` dir; the handle used to resolve it."
          },
          "scope": {
            "$ref": "#/components/schemas/FlagScope",
            "description": "Whether this flag is committed (general) or machine-local."
          },
          "type": {
            "type": "string",
            "description": "Free-text category, e.g. `\"bug\"`, `\"gap\"`, `\"edge_case\"`, `\"question\"`."
          }
        }
      },
      "FlagScope": {
        "type": "string",
        "description": "Whether a flag file is committed to version control or kept machine-local.",
        "enum": [
          "general",
          "local"
        ]
      },
      "FleetRunSummary": {
        "type": "object",
        "description": "One past (or in-progress) run, across every routine, listed newest-first — the fleet-wide\ncounterpart to [`RunSummary`] backing an overview \"recent runs\" view.",
        "required": [
          "routine_id",
          "routine_title",
          "workbench",
          "started_at",
          "started_at_local",
          "status"
        ],
        "properties": {
          "exit_code": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int32",
            "description": "Process exit code, when recorded."
          },
          "finished_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Unix seconds the run finished (`exit_code` file's mtime), `None` while running or unknown.",
            "minimum": 0
          },
          "finished_at_local": {
            "type": [
              "string",
              "null"
            ],
            "description": "`finished_at` as a human-readable local timestamp, when finished."
          },
          "routine_id": {
            "type": "string",
            "description": "The routine this run belongs to."
          },
          "routine_title": {
            "type": "string",
            "description": "The routine's title, at the time of this call (not snapshotted per-run)."
          },
          "started_at": {
            "type": "integer",
            "format": "int64",
            "description": "Unix seconds the run was triggered.",
            "minimum": 0
          },
          "started_at_local": {
            "type": "string",
            "description": "`started_at` as a human-readable local (daemon machine's timezone) timestamp."
          },
          "status": {
            "$ref": "#/components/schemas/RunStatus",
            "description": "Success/failure/running/unknown, derived from the exit-code file and tmux session liveness."
          },
          "workbench": {
            "type": "string",
            "description": "Workbench directory name (`{slug}-{unix_secs}`)."
          }
        }
      },
      "HealthResponse": {
        "type": "object",
        "description": "Response body for `GET /health`.",
        "required": [
          "status",
          "uptime_secs",
          "running",
          "machine",
          "dependencies",
          "version",
          "git_sha",
          "build_date"
        ],
        "properties": {
          "build_date": {
            "type": "string",
            "description": "Committer date (`YYYY-MM-DD`) of the build commit, or `\"unknown\"` outside a git checkout."
          },
          "dependencies": {
            "$ref": "#/components/schemas/DependencyHealth",
            "description": "Presence of required external binaries on the daemon's `PATH`."
          },
          "git_sha": {
            "type": "string",
            "description": "Short git commit SHA the daemon was built from, or `\"unknown\"` outside a git checkout."
          },
          "machine": {
            "type": "string",
            "description": "Resolved name of this machine (from `MOADIM_MACHINE`, `~/.config/moadim/machine.local.toml`, or hostname)."
          },
          "running": {
            "type": "boolean",
            "description": "Whether the server is running."
          },
          "server_exe_dir": {
            "type": [
              "string",
              "null"
            ],
            "description": "Absolute path of the directory containing the server executable, or `None` if unresolvable."
          },
          "server_root": {
            "type": [
              "string",
              "null"
            ],
            "description": "Absolute path of the server's working directory, or `None` if unresolvable."
          },
          "status": {
            "type": "string",
            "description": "Health status string (always `\"ok\"` when reachable)."
          },
          "uptime_secs": {
            "type": "integer",
            "format": "int64",
            "description": "Seconds elapsed since the server started.",
            "minimum": 0
          },
          "version": {
            "type": "string",
            "description": "Daemon version (from `CARGO_PKG_VERSION`)."
          }
        }
      },
      "LockRequest": {
        "type": "object",
        "description": "Request body for `POST /routines/lock`.",
        "required": [
          "scope"
        ],
        "properties": {
          "scope": {
            "type": "string",
            "description": "Which sentinel to create: `\"shared\"` (committed `.lock`) or `\"local\"` (gitignored `.local.lock`)."
          }
        }
      },
      "LockStatus": {
        "type": "object",
        "description": "Current state of both lock sentinels.",
        "required": [
          "shared",
          "local",
          "locked"
        ],
        "properties": {
          "local": {
            "type": "boolean",
            "description": "Whether the gitignored `.local.lock` file is present."
          },
          "locked": {
            "type": "boolean",
            "description": "`true` if either sentinel is present (all routine scheduling and triggers halted)."
          },
          "shared": {
            "type": "boolean",
            "description": "Whether the committed `.lock` file is present."
          }
        }
      },
      "MachineResponse": {
        "type": "object",
        "description": "Response body for `GET /machine`.",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string",
            "description": "Resolved name of this machine (from `MOADIM_MACHINE`, `~/.config/moadim/machine.local.toml`, or hostname)."
          }
        }
      },
      "MaxConcurrentRunsResponse": {
        "type": "object",
        "description": "Response body for `GET /config/max-concurrent-runs` and `PUT /config/max-concurrent-runs`.",
        "required": [
          "value"
        ],
        "properties": {
          "override_value": {
            "type": [
              "integer",
              "null"
            ],
            "description": "The persisted UI/REST override on its own, independent of the env var — `None` if never\nset. The UI should populate its input from this field (not `value`), so an env-var\noverride in effect doesn't get echoed back as though it were a saved setting.",
            "minimum": 0
          },
          "value": {
            "type": "integer",
            "description": "Effective cap: the `MOADIM_MAX_CONCURRENT_RUNS` env var if set, else the persisted\nUI/REST override, else the built-in default. `0` means unbounded.",
            "minimum": 0
          }
        }
      },
      "Repository": {
        "type": "object",
        "description": "A git repository made available to a routine's agent as prompt context (not cloned by moadim).",
        "required": [
          "repository"
        ],
        "properties": {
          "branch": {
            "type": [
              "string",
              "null"
            ],
            "description": "Branch to use, or `None` for the remote default branch."
          },
          "repository": {
            "type": "string",
            "description": "Git remote URL."
          }
        }
      },
      "RestartResponse": {
        "type": "object",
        "description": "Response body for `POST /restart` and the `restart` MCP tool.",
        "required": [
          "status",
          "helper_pid"
        ],
        "properties": {
          "helper_pid": {
            "type": "integer",
            "format": "int32",
            "description": "PID of the detached helper process performing the stop-old-then-start-new restart.",
            "minimum": 0
          },
          "status": {
            "type": "string",
            "description": "Acknowledgement status (always `\"restarting\"`)."
          }
        }
      },
      "Routine": {
        "type": "object",
        "description": "A persisted routine: a scheduled AI-agent task.",
        "required": [
          "id",
          "schedule",
          "title",
          "agent",
          "enabled",
          "source",
          "created_at",
          "updated_at"
        ],
        "properties": {
          "agent": {
            "type": "string",
            "description": "Agent registry key (e.g. `\"claude\"`) resolved from `~/.config/moadim/agents/`."
          },
          "created_at": {
            "type": "integer",
            "format": "int64",
            "description": "Unix timestamp (seconds) when the routine was created.",
            "minimum": 0
          },
          "enabled": {
            "type": "boolean",
            "description": "Whether the routine is active."
          },
          "goal": {
            "type": [
              "string",
              "null"
            ],
            "description": "A very short (at most 5 lines) statement of the routine's goal — the \"why\" behind the\nprompt. Rendered into the agent's `prompt.md` as a `## Goal` preamble. `None` when unset."
          },
          "id": {
            "type": "string",
            "description": "Unique identifier (UUID v4)."
          },
          "last_manual_trigger_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Unix timestamp (seconds) when the routine was last manually triggered, if ever.\n\nOnly manual triggers (`trigger_routine`) update this; scheduled cron firings run the built\ncommand directly and do not. Accepts the legacy `last_triggered_at` key on deserialize.",
            "minimum": 0
          },
          "last_scheduled_trigger_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Unix timestamp (seconds) when the routine was last fired by its cron schedule, if ever.\n\nThe mirror of [`Routine::last_manual_trigger_at`] for scheduled runs: a manual trigger\nupdates only the manual field, a scheduled firing updates only this one. The host OS crontab\nline runs `moadim schedule trigger <id>`, and the launch command the daemon spawns appends\nthe Unix timestamp to the gitignored `scheduled.log` at fire time; the daemon reads the last\nline back on load. The daemon never writes this field directly (it is absent from\n`routine.toml` and the daemon-owned `state.local.toml`), so re-persisting a routine can't\nclobber the log.",
            "minimum": 0
          },
          "machines": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Machines this routine runs on. Each daemon schedules a routine only when this list names its\nown machine identity ([`crate::machine::current_machine`]); an **empty list runs nowhere**, so\na routine is dormant until explicitly assigned. Lets one shared config repo drive different\nroutines on different machines."
          },
          "max_runtime_secs": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Maximum wall-clock seconds a single run may execute before the cleanup watchdog force-kills\nits (hung) tmux session, after which the workbench is reaped under the normal TTL rules.\n`None` uses `min(MAX_RUNTIME_SECS, cron interval)`; an explicit value can only lower that. A\nsession still within this bound is never touched. The cap and\n[`Routine::effective_max_runtime_secs`] live in the cleanup module. Must be greater than\nzero when set; `0` is rejected by `svc_create`/`svc_update` (#233).",
            "minimum": 0
          },
          "model": {
            "type": [
              "string",
              "null"
            ],
            "description": "Model ID to run the agent with (e.g. `\"claude-sonnet-4-6\"`), passed as `--model` on the\nagent invocation. `None` uses the agent's own default."
          },
          "power_saving": {
            "type": "boolean",
            "description": "Whether scheduled and manual firing is paused to conserve resources, independent of\n[`Routine::enabled`].\n\n`enabled` is user-owned intent (\"I want this routine on/off\"); `power_saving` is a\nsystem/policy throttle layered on top — both must hold for a firing to launch an agent\n(`enabled && !power_saving`). Never mutated by `svc_create`/`svc_update` (set via\n[`crate::routines::svc_set_power_saving`] instead), so it survives a config edit the same\nway `snoozed_until` and `skip_runs` do. Daemon-owned runtime state: persisted in the\ngitignored `state.local.toml` sidecar, not the version-controlled `routine.toml`."
          },
          "prompt": {
            "type": "string",
            "description": "The task prompt handed to the agent.\n\nOmitted from serialized output when empty. A persisted routine always has a\nnon-blank prompt (enforced by `validate_prompt`), so this never affects\n`routine.toml` persistence; it lets list responses drop the prompt by blanking\nit in-memory (see [`RoutineListQuery::include_prompts`] / `svc_list`)."
          },
          "repositories": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Repository"
            },
            "description": "Repositories listed in the prompt as context."
          },
          "schedule": {
            "type": "string",
            "description": "Cron expression defining when the routine runs, evaluated in the host's local\nsystem timezone (the OS crontab timezone), not UTC."
          },
          "skip_runs": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int32",
            "description": "Number of upcoming scheduled fires still to skip, or `None`.\n\nDecremented (and cleared once it reaches zero) on each skipped scheduled fire; manual\ntriggers do not consume it. Mutually exclusive with `snoozed_until`.",
            "minimum": 0
          },
          "snoozed_until": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Unix timestamp (seconds) until which scheduled (cron) fires are skipped, or `None`.\n\nCleared automatically the first time a scheduled fire observes `now >= snoozed_until`, which\nalso runs that fire. Manual triggers ([`crate::routines::svc_trigger`]) ignore this entirely.\nSet via the `snooze_routine` MCP tool; mutually exclusive with `skip_runs`.",
            "minimum": 0
          },
          "source": {
            "type": "string",
            "description": "`\"managed\"` for routines owned by this server."
          },
          "tags": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Free-form labels for grouping and filtering routines (e.g. `\"triage\"`, `\"nightly\"`).\nDefaults to empty; each entry is trimmed and must be non-blank."
          },
          "title": {
            "type": "string",
            "description": "Human name; slugified to name the workbench and tmux session."
          },
          "ttl_secs": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "How long (seconds) a finished run's workbench is retained before auto-cleanup removes it.\nCaps the cron-derived retention (`min(MAX_TTL_SECS, cron interval)`) lower; it can only\nshorten, never extend it. `None` uses the cron-derived value. Sessions still running are\nnever reaped. The cap and [`Routine::effective_ttl_secs`] live in the cleanup module. Must\nbe greater than zero when set; `0` is rejected by `svc_create`/`svc_update` (#233).",
            "minimum": 0
          },
          "updated_at": {
            "type": "integer",
            "format": "int64",
            "description": "Unix timestamp (seconds) when the routine was last updated.",
            "minimum": 0
          }
        }
      },
      "RoutineResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Routine",
            "description": "The underlying routine."
          },
          {
            "type": "object",
            "required": [
              "agent_registered",
              "agent_command_available",
              "agent_setup_available",
              "file_path",
              "flag_count",
              "is_running",
              "env_keys"
            ],
            "properties": {
              "agent_command_available": {
                "type": "boolean",
                "description": "`true` if the agent config's `command` (e.g. `claude`, `codex`) resolves to an executable\non the daemon's `PATH`. Distinct from [`Self::agent_registered`]: a routine can have a\npresent, well-formed agent config yet reference a binary that isn't installed, in which\ncase the cron firing launches a tmux session that dies immediately with \"command not\nfound\" — a silent no-op indistinguishable from a healthy routine by `agent_registered`\nalone. `false` whenever the agent config is missing, unreadable, or malformed, since no\n`command` can be resolved in that case either."
              },
              "agent_registered": {
                "type": "boolean",
                "description": "`true` if an agent config exists at `~/.config/moadim/agents/<agent>.toml` *and* parses\nsuccessfully. A present-but-malformed config is silently dropped at crontab-sync time, so\nit reports `false` here too — file existence alone is not \"registered\"."
              },
              "agent_setup_available": {
                "type": "boolean",
                "description": "`true` if the agent config has no `setup` step, or that step's first whitespace-delimited\ntoken (the interpreter/binary it shells out to, e.g. `python3` for the built-in `claude`\nagent's workspace-trust seeding) resolves on the daemon's `PATH`. Distinct from\n[`Self::agent_command_available`]: the agent's own `command` can be installed while its\n`setup` step still shells out to something that isn't — in which case the launch command's\nfail-fast guard (`build_routine_command`) aborts the run before the agent ever starts, a\nfailure otherwise invisible to anything checking only `agent_command_available`. `false`\nhere means the run is expected to abort in `setup` rather than actually launch the agent.\n`false` whenever the agent config is missing, unreadable, or malformed too (mirrors\n`agent_command_available`'s pessimistic default) — `agent_registered` is the field that\ndistinguishes that case. See issue #404."
              },
              "env_keys": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "description": "Names (never values) of every environment variable set for this routine, merging the\ntracked `routine.toml` `[env]` table with the untracked `routine.local.toml` sidecar\n(secrets), deduplicated and sorted. Lets a client show *what* is configured without ever\nexposing a secret value over the API (issue #408)."
              },
              "file_path": {
                "type": "string",
                "description": "Absolute path to the routine's `routine.toml` file on disk."
              },
              "flag_count": {
                "type": "integer",
                "description": "Number of open flags raised against this routine (see [`super::flags`]). Surfaced here so\nlistings can badge it without a separate `list_flags` round-trip per routine.",
                "minimum": 0
              },
              "is_running": {
                "type": "boolean",
                "description": "`true` if any fire of this routine currently has a live tmux session — i.e. an agent is\nrunning right now. Derived by probing for a session under the routine's\n`moadim-{slug}-` prefix (the same overlap-guard check `svc_trigger` uses, #514), not\npersisted. `false` whenever no `tmux` binary is available, mirroring the probe's existing\nbest-effort \"no tmux, nothing running\" stance. See issue #438."
              },
              "next_run_at": {
                "type": [
                  "integer",
                  "null"
                ],
                "format": "int64",
                "description": "Unix epoch seconds of this routine's next scheduled fire, in the host's local timezone\n(matching crontab semantics) — the future counterpart to `last_scheduled_trigger_at`.\n`None` when disabled, globally locked, or `schedule` is unparseable or has no upcoming\nfire (e.g. `@reboot`). See issue #369.",
                "minimum": 0
              },
              "schedule_description": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "Human-readable description of the schedule, including the timezone the\ncron expression is interpreted in, or `null` if it cannot be parsed."
              },
              "timezone": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "IANA name of the local timezone the schedule is interpreted in (e.g.\n`\"Asia/Jerusalem\"`), or `null` if it cannot be determined. Cron\nexpressions are evaluated in this timezone, **not** UTC."
              }
            }
          }
        ],
        "description": "A [`Routine`] enriched with derived, non-persisted fields for API responses."
      },
      "RoutineSort": {
        "type": "string",
        "description": "Field to sort a routine listing by.",
        "enum": [
          "created",
          "updated",
          "title",
          "repository"
        ]
      },
      "RunStatus": {
        "type": "string",
        "description": "Outcome of a single past run, derived from its workbench on disk.",
        "enum": [
          "running",
          "success",
          "failed",
          "unknown"
        ]
      },
      "RunSummary": {
        "type": "object",
        "description": "One past (or in-progress) run of a routine, listed newest-first.",
        "required": [
          "workbench",
          "started_at",
          "started_at_local",
          "status"
        ],
        "properties": {
          "exit_code": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int32",
            "description": "Process exit code, when recorded."
          },
          "finished_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Unix seconds the run finished (`exit_code` file's mtime), `None` while running or unknown.",
            "minimum": 0
          },
          "finished_at_local": {
            "type": [
              "string",
              "null"
            ],
            "description": "`finished_at` as a human-readable local timestamp, when finished."
          },
          "retention_expires_at": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Unix seconds this run's workbench is due to be reaped (`finished_at` +\n[`Routine::effective_ttl_secs`]). `None` while the run hasn't finished, or once its\nworkbench is already gone (a run restored from `runs.log` after TTL reaping).",
            "minimum": 0
          },
          "started_at": {
            "type": "integer",
            "format": "int64",
            "description": "Unix seconds the run was triggered.",
            "minimum": 0
          },
          "started_at_local": {
            "type": "string",
            "description": "`started_at` as a human-readable local (daemon machine's timezone) timestamp."
          },
          "status": {
            "$ref": "#/components/schemas/RunStatus",
            "description": "Success/failure/running/unknown, derived from the exit-code file and tmux session liveness."
          },
          "workbench": {
            "type": "string",
            "description": "Workbench directory name (`{slug}-{unix_secs}`); pass to `GET /routines/{id}/runs/{workbench}/log`."
          }
        }
      },
      "SetMachineRequest": {
        "type": "object",
        "description": "Request body for `PUT /machine`.",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string",
            "description": "New machine name. Trimmed; must be non-empty."
          }
        }
      },
      "SetMaxConcurrentRunsRequest": {
        "type": "object",
        "description": "Request body for `PUT /config/max-concurrent-runs`.",
        "properties": {
          "value": {
            "type": [
              "integer",
              "null"
            ],
            "description": "New override value, or `null`/omitted to clear it and fall back to the env var/default.",
            "minimum": 0
          }
        }
      },
      "SetUserPromptRequest": {
        "type": "object",
        "description": "Request body for `PUT /config/user-prompt`.",
        "required": [
          "content"
        ],
        "properties": {
          "content": {
            "type": "string",
            "description": "New persistent prompt contents. An empty string clears it."
          }
        }
      },
      "ShutdownResponse": {
        "type": "object",
        "description": "Response body for `POST /shutdown`.",
        "required": [
          "status"
        ],
        "properties": {
          "status": {
            "type": "string",
            "description": "Acknowledgement status (always `\"shutting down\"`)."
          }
        }
      },
      "SortOrder": {
        "type": "string",
        "description": "Sort direction for a routine listing.",
        "enum": [
          "asc",
          "desc"
        ]
      },
      "UpdateRoutineRequest": {
        "type": "object",
        "description": "Request body for partially updating an existing routine.",
        "properties": {
          "agent": {
            "type": [
              "string",
              "null"
            ],
            "description": "New agent key, or `None` to keep the existing value."
          },
          "enabled": {
            "type": [
              "boolean",
              "null"
            ],
            "description": "New enabled state, or `None` to keep the existing value."
          },
          "env": {
            "type": [
              "object",
              "null"
            ],
            "description": "New tracked `[env]` map, or `None` to keep the existing value. Replaces the whole map (not\nmerged); send the full desired set. See [`CreateRoutineRequest::env`] for validation rules\nand the `routine.local.toml` secrets sidecar.",
            "additionalProperties": {
              "type": "string"
            },
            "propertyNames": {
              "type": "string"
            }
          },
          "goal": {
            "type": [
              "string",
              "null"
            ],
            "description": "New goal, or `None` to keep the existing value. Send an empty string to clear it."
          },
          "machines": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "type": "string"
            },
            "description": "New machines targeting list, or `None` to keep the existing value."
          },
          "max_runtime_secs": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "New max runtime (seconds) for a single run, or `None` to keep the existing value. Must be\ngreater than zero when set; `0` is rejected (#233).",
            "minimum": 0
          },
          "model": {
            "type": [
              "string",
              "null"
            ],
            "description": "New model ID, or `None` to keep the existing value. A blank/whitespace-only value clears\nthe model back to the agent's own default."
          },
          "prompt": {
            "type": [
              "string",
              "null"
            ],
            "description": "New prompt, or `None` to keep the existing value."
          },
          "repositories": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "$ref": "#/components/schemas/Repository"
            },
            "description": "New repositories list, or `None` to keep the existing value."
          },
          "schedule": {
            "type": [
              "string",
              "null"
            ],
            "description": "New cron expression, or `None` to keep the existing value. Evaluated in the\nhost's local system timezone (the OS crontab timezone), not UTC."
          },
          "tags": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "type": "string"
            },
            "description": "New tags list, or `None` to keep the existing value."
          },
          "title": {
            "type": [
              "string",
              "null"
            ],
            "description": "New title, or `None` to keep the existing value."
          },
          "ttl_secs": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "New workbench TTL (seconds), or `None` to keep the existing value. Must be\ngreater than zero when set; `0` is rejected (#233).",
            "minimum": 0
          }
        }
      }
    }
  }
}