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
//! The control-plane client the CLI and other surfaces use (spec 3.2 §7.11, §8.1).
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::time::Duration;
use adk_core::{AdkError, ApprovalAction, ErrorCategory, ErrorComponent};
use futures::stream::{self, BoxStream, StreamExt};
use hyper::body::Bytes;
use hyper::{Method, StatusCode};
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use super::api::{
AgendaItemResponse, AgendaQuery, AgendaResponse, ApprovalItem, AttachRequest, AttachResponse,
FreezeRequest, FreezeResponse, KillRequest, NiceRequest, ProcessResponse, ProcessesQuery,
ProcessesResponse, RejectRequest, RunNowRequest, ScheduleRequest, SpawnRequest, SteerRequest,
SteerResponse,
};
use super::{ControlPlaneConfig, paths};
use crate::agenda::AgendaItem;
use crate::bus::EventRecord;
use crate::error::{self, KernelError, Result};
use crate::kernel::KernelHealth;
use crate::process::Process;
use crate::sched::QueueClass;
use crate::wake::WakeSet;
/// The approvals inbox, beside the kernel routes under `/api`.
const APPROVALS: &str = "/approvals";
/// How long a call other than [`events`](ControlClient::events) waits for the kernel.
const CALL_TIMEOUT: Duration = Duration::from_secs(30);
/// A client of a running kernel's control plane (spec 3.2 §7.11), over its Unix socket, its
/// named pipe on Windows (§10 W2) or HTTP. Surfaces hold no state (K-I17): every call reads
/// the kernel.
///
/// # Example
///
/// ```rust,no_run
/// use adk_kernel::control::ControlClient;
///
/// # async fn example(home: &std::path::Path) -> adk_kernel::Result<()> {
/// let client = match ControlClient::connect_local(home).await? {
/// Some(client) => client,
/// None => ControlClient::http("https://kernel.example.com").with_bearer_token("token"),
/// };
/// for process in client.processes(&Default::default()).await? {
/// println!("{} {:?}", process.meta.pid, process.run.state);
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct ControlClient {
transport: Transport,
token: Option<String>,
}
#[derive(Debug, Clone)]
enum Transport {
Socket(PathBuf),
#[cfg(windows)]
Pipe(String),
Http {
base: String,
client: reqwest::Client,
},
}
/// A response's status and body.
struct Answer {
status: StatusCode,
body: BoxStream<'static, Result<Bytes>>,
}
impl Answer {
async fn bytes(self) -> Result<Vec<u8>> {
let mut body = self.body;
let mut bytes = Vec::new();
while let Some(chunk) = body.next().await {
bytes.extend_from_slice(&chunk?);
}
Ok(bytes)
}
}
impl ControlClient {
/// A client of the kernel listening on the Unix socket at `path`.
pub fn socket(path: impl Into<PathBuf>) -> Self {
Self { transport: Transport::Socket(path.into()), token: None }
}
/// A client of the kernel listening on the named pipe `name` (Windows; spec 3.2 §10 W2),
/// as [`ControlPlaneConfig::pipe_name`] names it. The client talks to the pipe only when
/// its owner is the user running the client.
#[cfg(windows)]
pub fn pipe(name: impl Into<String>) -> Self {
Self { transport: Transport::Pipe(name.into()), token: None }
}
/// A client of the kernel serving HTTP at `base_url`, the routes under its `/api`.
pub fn http(base_url: impl Into<String>) -> Self {
let base = base_url.into().trim_end_matches('/').to_string();
Self { transport: Transport::Http { base, client: reqwest::Client::new() }, token: None }
}
/// Sends `token` as a bearer token.
#[must_use]
pub fn with_bearer_token(mut self, token: impl Into<String>) -> Self {
self.token = Some(token.into());
self
}
/// The bearer token the client sends, so a caller of other routes on the same control
/// plane (the approvals inbox, spec 3.3 §6) sends the same one.
///
/// # Example
///
/// ```rust
/// use adk_kernel::control::ControlClient;
///
/// let client = ControlClient::http("https://kernel.example.com").with_bearer_token("token");
/// assert_eq!(client.bearer_token(), Some("token"));
/// assert_eq!(ControlClient::socket("/run/kernel.sock").bearer_token(), None);
/// ```
pub fn bearer_token(&self) -> Option<&str> {
self.token.as_deref()
}
/// Where the client connects: `unix:<path>`, `pipe:<name>` or the base URL.
pub fn endpoint(&self) -> String {
match &self.transport {
Transport::Socket(path) => format!("unix:{}", path.display()),
#[cfg(windows)]
Transport::Pipe(name) => format!("pipe:{name}"),
Transport::Http { base, .. } => base.clone(),
}
}
/// A client of the kernel running at `home`, or `None` when none answers on its socket,
/// or on Windows its named pipe (spec 3.2 §10 W2).
///
/// A socket left by a kernel that did not stop cleanly counts as none; a pipe goes away
/// with its kernel.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the socket or pipe exists but this user may
/// not use it: another account owns it, or its permissions refuse this user; or when the
/// directory of its own that a socket too long for the home sits in is a symlink, another
/// user's, or open to other users (spec 3.3 G18).
pub async fn connect_local(home: &Path) -> Result<Option<Self>> {
#[cfg(windows)]
{
let Some(name) = ControlPlaneConfig::default().pipe_name(home) else {
return Ok(None);
};
match super::pipe::answers(&name).await {
Ok(true) => Ok(Some(Self::pipe(name))),
Ok(false) => Ok(None),
Err(error) => Err(KernelError::ControlPlane {
reason: format!(
"the kernel pipe {name} exists but cannot be used: {error}; it admits \
only the OS user that runs the kernel"
),
}),
}
}
#[cfg(not(windows))]
{
let Some(path) = ControlPlaneConfig::default().socket_path(home) else {
return Ok(None);
};
connect_local_socket(path, &home.join(ControlPlaneConfig::DEFAULT_SOCKET)).await
}
}
/// `GET /kernel/health`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn health(&self) -> Result<KernelHealth> {
self.call(Method::GET, paths::HEALTH.to_string(), None::<&()>).await
}
/// `GET /kernel/inventory`: what the kernel runs, and a warning for each thing it would
/// silently not do (`Kernel::inventory`); for the `operator` scope.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// kernel's error for a caller without the `operator` scope.
pub async fn inventory(&self) -> Result<crate::inventory::KernelInventory> {
self.call(Method::GET, paths::INVENTORY.to_string(), None::<&()>).await
}
/// `GET /kernel/partitions`: the partition table (spec 3.2 §7.8).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn partitions(&self) -> Result<Vec<crate::partition::Partition>> {
let answer: super::api::PartitionsResponse =
self.call(Method::GET, paths::PARTITIONS.to_string(), None::<&()>).await?;
Ok(answer.partitions)
}
/// `GET /kernel/agents`: the agents a person added, but tombstones and refused
/// suggestions (spec 3.3 §10 K7).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn agents(&self) -> Result<Vec<crate::agents::AgentView>> {
let answer: super::api::AgentsResponse =
self.call(Method::GET, paths::AGENTS.to_string(), None::<&()>).await?;
Ok(answer.agents)
}
/// `GET /kernel/agents/{id}`: one agent a person added, tombstones included (spec 3.3
/// §10 K7).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `kernel.agent.not_found` for an unknown id.
pub async fn agent(&self, id: &str) -> Result<crate::agents::AgentView> {
let path = paths::AGENT.replace("{id}", &encode(id));
self.call(Method::GET, path, None::<&()>).await
}
/// `POST /kernel/partitions`: creates or replaces `partition` (spec 3.2 §7.8).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn put_partition(
&self,
partition: &crate::partition::Partition,
) -> Result<crate::partition::Partition> {
let answer: super::api::PartitionResponse =
self.call(Method::POST, paths::PARTITIONS.to_string(), Some(partition)).await?;
Ok(answer.partition)
}
/// `POST /kernel/partitions/{id}/mode`: sets partition `id`'s mode (spec 3.2 §8.1).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn set_partition_mode(
&self,
id: &str,
mode: crate::partition::PartitionMode,
) -> Result<crate::partition::Partition> {
let path = paths::PARTITION_MODE.replace("{id}", &encode(id));
let body = super::api::PartitionModeRequest { mode };
let answer: super::api::PartitionResponse =
self.call(Method::POST, path, Some(&body)).await?;
Ok(answer.partition)
}
/// `GET /kernel/settings`: every area's quiet time, daily spending limit and mode in
/// force (spec 3.3 §10 K3).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `403` without the `operator` scope or a device's `read`.
pub async fn settings(&self) -> Result<Vec<super::api::AreaView>> {
let answer: super::api::SettingsResponse =
self.call(Method::GET, paths::SETTINGS.to_string(), None::<&()>).await?;
Ok(answer.areas)
}
/// `GET /kernel/settings/{partition}`: one area's settings in force (spec 3.3 §10 K3).
///
/// # Errors
///
/// As [`settings`](Self::settings), and `404` for an unknown area.
pub async fn area_settings(&self, partition: &str) -> Result<super::api::AreaView> {
let path = paths::AREA_SETTINGS.replace("{partition}", &encode(partition));
let answer: super::api::AreaResponse = self.call(Method::GET, path, None::<&()>).await?;
Ok(answer.area)
}
/// `POST /kernel/settings/{partition}/quiet-time`: sets the area's quiet time, or turns
/// it off with `None` (spec 3.3 §10 K3).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `403` for a device token or without the `operator` scope,
/// `404` for an unknown area.
pub async fn set_quiet_time(
&self,
partition: &str,
window: Option<crate::sched::QuietHours>,
) -> Result<super::api::AreaView> {
let path = paths::QUIET_TIME.replace("{partition}", &encode(partition));
let body = super::api::QuietTimeRequest { window };
let answer: super::api::AreaResponse = self.call(Method::POST, path, Some(&body)).await?;
Ok(answer.area)
}
/// `POST /kernel/settings/{partition}/spending-limit`: sets the area's daily spending
/// limit in micro-USD, or removes it with `None` (spec 3.3 §10 K3).
///
/// # Errors
///
/// As [`set_quiet_time`](Self::set_quiet_time).
pub async fn set_spending_limit(
&self,
partition: &str,
micro_usd: Option<u64>,
) -> Result<super::api::AreaView> {
let path = paths::SPENDING_LIMIT.replace("{partition}", &encode(partition));
let body = super::api::SpendingLimitRequest { micro_usd };
let answer: super::api::AreaResponse = self.call(Method::POST, path, Some(&body)).await?;
Ok(answer.area)
}
/// `POST /kernel/settings/{partition}/mode`: sets how much the area's agents act alone
/// (spec 3.3 §10 K3).
///
/// # Errors
///
/// As [`set_quiet_time`](Self::set_quiet_time).
pub async fn set_area_mode(
&self,
partition: &str,
mode: crate::partition::PartitionMode,
) -> Result<super::api::AreaView> {
let path = paths::AREA_MODE.replace("{partition}", &encode(partition));
let body = super::api::PartitionModeRequest { mode };
let answer: super::api::AreaResponse = self.call(Method::POST, path, Some(&body)).await?;
Ok(answer.area)
}
/// `GET /kernel/away`: "While you were away" after the viewer's cursor (spec 3.3 §10
/// K4). A paired device reads as itself; `query.viewer` names any other surface.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
#[cfg(feature = "proc")]
pub async fn away(&self, query: &super::api::AwayQuery) -> Result<super::api::AwayResponse> {
let mut pairs = Vec::new();
if let Some(viewer) = &query.viewer {
pairs.push(format!("viewer={}", encode(viewer)));
}
if let Some(after) = query.after {
pairs.push(format!("after={after}"));
}
if let Some(limit) = query.limit {
pairs.push(format!("limit={limit}"));
}
let path = match pairs.is_empty() {
true => paths::AWAY.to_string(),
false => format!("{}?{}", paths::AWAY, pairs.join("&")),
};
self.call(Method::GET, path, None::<&()>).await
}
/// `POST /kernel/away/seen`: marks "While you were away" seen through row `through`, a
/// page's `through` (spec 3.3 §10 K4); returns the cursor, which never moves back.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `400` for a row the ledger does not hold yet.
#[cfg(feature = "proc")]
pub async fn mark_away_seen(
&self,
viewer: Option<&str>,
through: u64,
) -> Result<super::AwayCursor> {
let body = super::api::SeenRequest { viewer: viewer.map(str::to_string), through };
let answer: super::api::SeenResponse =
self.call(Method::POST, paths::AWAY_SEEN.to_string(), Some(&body)).await?;
Ok(answer.cursor)
}
/// `GET /kernel/away/lines/{seq}`: one line of "While you were away" with its ledger
/// row, the line's record (spec 3.3 §10 K4).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `404` for a row the ledger does not hold.
#[cfg(feature = "proc")]
pub async fn away_line(&self, seq: u64) -> Result<super::api::AwayLineResponse> {
let path = paths::AWAY_LINE.replace("{seq}", &seq.to_string());
self.call(Method::GET, path, None::<&()>).await
}
/// `GET /kernel/memory`: what the agents remember about the caller, with provenance, and
/// the overnight tidy-ups (spec 3.3 §10 K5).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `400` when the kernel shows no memory.
pub async fn memory(&self, area: Option<&str>) -> Result<super::api::MemoryResponse> {
let path = match area {
Some(area) => format!("{}?area={}", paths::MEMORY, encode(area)),
None => paths::MEMORY.to_string(),
};
self.call(Method::GET, path, None::<&()>).await
}
/// `POST /kernel/memory/facts/{id}/confirm`: "That's right" (spec 3.3 §8.2, §10 K5).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `403` for a device token, `404` for a fact that is not
/// the caller's.
pub async fn confirm_fact(&self, id: &str) -> Result<super::RememberedFact> {
let path = paths::FACT_CONFIRM.replace("{id}", &encode(id));
let answer: super::api::FactResponse = self.call(Method::POST, path, Some(&())).await?;
Ok(answer.fact)
}
/// `POST /kernel/memory/facts/{id}/fix`: "Fix it" with the person's `text` (spec 3.3
/// §8.2, §10 K5).
///
/// # Errors
///
/// As [`confirm_fact`](Self::confirm_fact), and `400` for a text the harness refuses.
pub async fn fix_fact(&self, id: &str, text: &str) -> Result<super::RememberedFact> {
let path = paths::FACT_FIX.replace("{id}", &encode(id));
let body = super::api::FixRequest { text: text.to_string() };
let answer: super::api::FactResponse = self.call(Method::POST, path, Some(&body)).await?;
Ok(answer.fact)
}
/// `POST /kernel/memory/facts/{id}/forget`: "Forget it" (spec 3.3 §8.2, §10 K5).
///
/// # Errors
///
/// As [`confirm_fact`](Self::confirm_fact).
pub async fn forget_fact(&self, id: &str) -> Result<()> {
let path = paths::FACT_FORGET.replace("{id}", &encode(id));
let _: super::api::ForgetResponse = self.call(Method::POST, path, Some(&())).await?;
Ok(())
}
/// `POST /kernel/memory/undo`: "Undo" the tidy-up `id`, or the latest one that can be
/// undone (spec 3.3 §8.2, §10 K5).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `403` for a device token, `404` when nothing is left to
/// undo, `409` for a tidy-up unfinished or undone.
pub async fn undo_tidy_up(&self, id: Option<&str>) -> Result<super::TidyUpUndo> {
let body = super::api::UndoRequest { tidy_up: id.map(str::to_string) };
self.call(Method::POST, paths::MEMORY_UNDO.to_string(), Some(&body)).await
}
/// `GET /kernel/record`: the latest `limit` actions on record, newest first (spec 3.3
/// §10 K5).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `400` when the kernel shows no record.
pub async fn record(&self, limit: Option<usize>) -> Result<Vec<super::RecordLine>> {
let path = match limit {
Some(limit) => format!("{}?limit={limit}", paths::RECORD),
None => paths::RECORD.to_string(),
};
let answer: super::api::RecordResponse = self.call(Method::GET, path, None::<&()>).await?;
Ok(answer.lines)
}
/// `GET /kernel/record/check`: whether every action is on record and nothing was
/// changed or deleted, as `adk-rust audit verify` answers (spec 3.3 §10 K5).
///
/// # Errors
///
/// As [`record`](Self::record).
pub async fn check_record(&self) -> Result<super::RecordCheck> {
self.call(Method::GET, paths::RECORD_CHECK.to_string(), None::<&()>).await
}
/// `GET /kernel/processes`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn processes(&self, query: &ProcessesQuery) -> Result<Vec<Process>> {
let mut pairs = Vec::new();
if let Some(partition) = &query.partition {
pairs.push(format!("partition={}", encode(partition.as_str())));
}
if let Some(state) = query.state {
let state = serde_json::to_value(state)?;
pairs.push(format!("state={}", encode(state.as_str().unwrap_or_default())));
}
if let Some(limit) = query.limit {
pairs.push(format!("limit={limit}"));
}
let path = match pairs.is_empty() {
true => paths::PROCESSES.to_string(),
false => format!("{}?{}", paths::PROCESSES, pairs.join("&")),
};
let answer: ProcessesResponse = self.call(Method::GET, path, None::<&()>).await?;
Ok(answer.processes)
}
/// `POST /kernel/processes`: starts a process of a registered target as the caller.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `404` for an unknown target, `403` without the `operator`
/// scope.
pub async fn spawn(&self, request: &SpawnRequest) -> Result<Process> {
let answer: ProcessResponse =
self.call(Method::POST, paths::PROCESSES.to_string(), Some(request)).await?;
Ok(answer.process)
}
/// `GET /approvals`: the held calls the caller may decide, pending first page.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `404` when it serves no approvals inbox.
pub async fn approvals(&self) -> Result<Vec<ApprovalItem>> {
#[derive(serde::Deserialize)]
struct Page {
items: Vec<ApprovalItem>,
}
let page: Page =
self.call(Method::GET, format!("{APPROVALS}?limit=200"), None::<&()>).await?;
Ok(page.items)
}
/// `POST /approvals/{id}/decide`: decides a held call, at the `version` the caller read.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `409` for a stale version, `403` for a caller who may not
/// decide it.
pub async fn decide(
&self,
id: &str,
version: u64,
action: ApprovalAction,
reason: Option<&str>,
) -> Result<ApprovalItem> {
let body = serde_json::json!({
"expectedVersion": version,
"action": action,
"reason": reason,
});
let path = format!("{APPROVALS}/{}/decide", encode(id));
self.call(Method::POST, path, Some(&body)).await
}
/// `POST /kernel/processes/{pid}/kill`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn kill(&self, pid: &str, reason: Option<&str>) -> Result<Process> {
let body = KillRequest { reason: reason.map(str::to_string) };
self.process_action(paths::KILL, pid, &body).await
}
/// `POST /kernel/processes/{pid}/nice`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn nice(&self, pid: &str, queue: QueueClass) -> Result<Process> {
self.process_action(paths::NICE, pid, &NiceRequest { queue }).await
}
/// `POST /kernel/processes/{pid}/suspend`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn suspend(&self, pid: &str) -> Result<Process> {
self.process_action(paths::SUSPEND, pid, &()).await
}
/// `POST /kernel/processes/{pid}/resume`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn resume(&self, pid: &str) -> Result<Process> {
self.process_action(paths::RESUME, pid, &()).await
}
/// `POST /kernel/processes/{pid}/run-now`: runs a cron sleeper now, outside its schedule
/// ([`Kernel::run_now`](crate::Kernel::run_now)); a retry with the same `request_id`
/// queues nothing new.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered, such as `kernel.frozen`.
pub async fn run_now(
&self,
pid: &str,
request_id: Option<&str>,
) -> Result<crate::kernel::RunNow> {
let path = paths::RUN_NOW.replace("{pid}", &encode(pid));
self.call(Method::POST, path, Some(&run_now_request(request_id))).await
}
/// `POST /kernel/triggers/{name}/run-now`: runs a standing cron trigger now, outside its
/// schedule ([`Kernel::run_now`](crate::Kernel::run_now)).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn run_trigger_now(
&self,
name: &str,
request_id: Option<&str>,
) -> Result<crate::kernel::RunNow> {
let path = paths::TRIGGER_RUN_NOW.replace("{name}", &encode(name));
self.call(Method::POST, path, Some(&run_now_request(request_id))).await
}
/// `GET /kernel/drivers`: the mounted drivers (spec 3.2 §7.7).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
#[cfg(feature = "drivers")]
pub async fn drivers(&self) -> Result<Vec<crate::drivers::DriverView>> {
let answer: super::api::DriversResponse =
self.call(Method::GET, paths::DRIVERS.to_string(), None::<&()>).await?;
Ok(answer.drivers)
}
/// `POST /kernel/drivers/{id}/mount`: what mounting `manifest` (its TOML) approves,
/// and with `confirm` the mount itself.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
#[cfg(feature = "drivers")]
pub async fn mount_driver(
&self,
id: &str,
manifest: &str,
confirm: bool,
) -> Result<super::api::MountDriverResponse> {
let mut body = super::api::MountDriverRequest::new(manifest);
body.confirm = confirm;
let path = paths::DRIVER_MOUNT.replace("{id}", &encode(id));
self.call(Method::POST, path, Some(&body)).await
}
/// `POST /kernel/drivers/{id}/unmount`; `false` when no such driver was mounted.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
#[cfg(feature = "drivers")]
pub async fn unmount_driver(&self, id: &str) -> Result<bool> {
let path = paths::DRIVER_UNMOUNT.replace("{id}", &encode(id));
let answer: super::api::UnmountDriverResponse =
self.call(Method::POST, path, Some(&())).await?;
Ok(answer.unmounted)
}
/// `POST /kernel/drivers/{id}/restart`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
#[cfg(feature = "drivers")]
pub async fn restart_driver(&self, id: &str) -> Result<crate::drivers::DriverView> {
let path = paths::DRIVER_RESTART.replace("{id}", &encode(id));
let answer: super::api::DriverResponse = self.call(Method::POST, path, Some(&())).await?;
Ok(answer.driver)
}
/// `POST /kernel/drivers/{id}/reapprove`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
#[cfg(feature = "drivers")]
pub async fn reapprove_driver(&self, id: &str) -> Result<crate::drivers::DriverView> {
let path = paths::DRIVER_REAPPROVE.replace("{id}", &encode(id));
let answer: super::api::DriverResponse = self.call(Method::POST, path, Some(&())).await?;
Ok(answer.driver)
}
/// `POST /kernel/freeze`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn freeze(&self, reason: Option<&str>) -> Result<FreezeResponse> {
let body = FreezeRequest { reason: reason.map(str::to_string) };
self.call(Method::POST, paths::FREEZE.to_string(), Some(&body)).await
}
/// `POST /kernel/unfreeze`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn unfreeze(&self) -> Result<FreezeResponse> {
self.call(Method::POST, paths::UNFREEZE.to_string(), Some(&())).await
}
/// `GET /kernel/proc/ledger` as JSON: one page of the activity ledger (spec 3.2 §7.10).
/// Page on with `params.after` set to the answer's `next`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `403` without the `operator` scope.
#[cfg(feature = "proc")]
pub async fn ledger(
&self,
params: &super::api::LedgerParams,
) -> Result<super::api::LedgerResponse> {
let mut pairs = Vec::new();
let mut pair = |key: &str, value: Option<String>| {
if let Some(value) = value {
pairs.push(format!("{key}={}", encode(&value)));
}
};
pair("partition", params.partition.as_ref().map(|p| p.as_str().to_string()));
pair("kind", params.kind.clone());
pair("agent", params.agent.clone());
pair("from", params.from.map(|from| from.to_rfc3339()));
pair("to", params.to.map(|to| to.to_rfc3339()));
pair("after", params.after.map(|after| after.to_string()));
pair("limit", params.limit.map(|limit| limit.to_string()));
let path = match pairs.is_empty() {
true => paths::PROC_LEDGER.to_string(),
false => format!("{}?{}", paths::PROC_LEDGER, pairs.join("&")),
};
self.call(Method::GET, path, None::<&()>).await
}
/// `GET /kernel/proc/baselines`: each partition's last baseline pass (spec 3.2 §7.10).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
#[cfg(feature = "proc")]
pub async fn baselines(&self) -> Result<Vec<crate::proc::PassState>> {
let answer: super::api::BaselinesResponse =
self.call(Method::GET, paths::PROC_BASELINES.to_string(), None::<&()>).await?;
Ok(answer.partitions)
}
/// `GET /kernel/proc/observations`: the observations the kernel offered, newest first
/// (spec 3.2 §7.10).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
#[cfg(feature = "proc")]
pub async fn observations(
&self,
params: &super::api::ObservationsParams,
) -> Result<Vec<crate::proc::Observation>> {
let mut pairs = Vec::new();
if let Some(partition) = ¶ms.partition {
pairs.push(format!("partition={}", encode(partition.as_str())));
}
if let Some(since) = params.since {
pairs.push(format!("since={since}"));
}
if let Some(limit) = params.limit {
pairs.push(format!("limit={limit}"));
}
let path = match pairs.is_empty() {
true => paths::PROC_OBSERVATIONS.to_string(),
false => format!("{}?{}", paths::PROC_OBSERVATIONS, pairs.join("&")),
};
let answer: super::api::ObservationsResponse =
self.call(Method::GET, path, None::<&()>).await?;
Ok(answer.observations)
}
/// `GET /kernel/events`: the event bus from just after `after`, then live.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached; a broken
/// stream ends with the error as its last item.
pub async fn events(
&self,
after: Option<u64>,
) -> Result<BoxStream<'static, Result<EventRecord>>> {
let path = match after {
Some(cursor) => format!("{}?after={cursor}", paths::EVENTS),
None => paths::EVENTS.to_string(),
};
let answer = self.send(Method::GET, &path, None, None).await?;
if !answer.status.is_success() {
let status = answer.status;
return Err(answer_error(status, &answer.bytes().await?));
}
Ok(server_sent_records(answer.body))
}
/// `POST /kernel/attach/{session}`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn attach(
&self,
session_id: &str,
request: &AttachRequest,
) -> Result<AttachResponse> {
let path = paths::ATTACH.replace("{session}", &encode(session_id));
self.call(Method::POST, path, Some(request)).await
}
/// `POST /kernel/sessions/{session}/steer`: sends `text` to the running turn of the
/// caller's session `session_id` (spec 3.3 §6 "steer").
///
/// [`SteerResponse::Accepted`] means the turn holds the message, not that it took it: the
/// message is delivered once [`attach`](Self::attach) shows an event whose steering marker
/// carries the answer's steer id, and handed back once the process leaves the answer's
/// claim without one. On [`SteerResponse::Returned`], [`spawn`](Self::spawn) it as the
/// session's next message.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the error
/// the kernel answered: `403` without the `operator` scope or for a device token, `503`
/// while everything is stopped, `400` for a blank message, or an input guardrail's
/// refusal.
///
/// # Example
///
/// ```rust,no_run
/// use adk_kernel::control::ControlClient;
/// use adk_kernel::control::api::{SpawnRequest, SteerResponse};
///
/// # async fn example(client: ControlClient) -> adk_kernel::Result<()> {
/// let text = "use the staging database";
/// if client.steer("s-1", text).await? == SteerResponse::Returned {
/// let next = SpawnRequest::new("adk-rust", serde_json::json!({ "text": text }));
/// client.spawn(&next.with_session_id("s-1")).await?;
/// }
/// # Ok(())
/// # }
/// ```
pub async fn steer(&self, session_id: &str, text: &str) -> Result<SteerResponse> {
let path = paths::SESSION_STEER.replace("{session}", &encode(session_id));
self.call(Method::POST, path, Some(&SteerRequest::new(text))).await
}
/// `GET /kernel/agenda` (spec 3.2 §7.5, §8.1 `adk-rust agenda list`).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn agenda(&self, query: &AgendaQuery) -> Result<Vec<AgendaItem>> {
let mut pairs = Vec::new();
if let Some(partition) = &query.partition {
pairs.push(format!("partition={}", encode(partition.as_str())));
}
if let Some(state) = query.state {
pairs.push(format!("state={}", encode(state.as_str())));
}
if let Some(limit) = query.limit {
pairs.push(format!("limit={limit}"));
}
if query.newest_first {
pairs.push("newestFirst=true".to_string());
}
if let Some(after) = &query.after {
pairs.push(format!("after={}", encode(after)));
}
let path = match pairs.is_empty() {
true => paths::AGENDA.to_string(),
false => format!("{}?{}", paths::AGENDA, pairs.join("&")),
};
let answer: AgendaResponse = self.call(Method::GET, path, None::<&()>).await?;
Ok(answer.items)
}
/// `POST /kernel/agenda/{id}/approve`: approves the item as the caller, then schedules it.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `404` for an unknown item, `409` for one not proposed.
pub async fn approve_item(&self, id: &str) -> Result<AgendaItem> {
self.agenda_action(paths::AGENDA_APPROVE, id, &()).await
}
/// `POST /kernel/agenda/{id}/reject`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn reject_item(&self, id: &str, reason: Option<&str>) -> Result<AgendaItem> {
let body = RejectRequest { reason: reason.map(str::to_string) };
self.agenda_action(paths::AGENDA_REJECT, id, &body).await
}
/// `POST /kernel/agenda/{id}/schedule`: puts the approved item's process to sleep on
/// `wake`.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn schedule_item(&self, id: &str, wake: WakeSet) -> Result<AgendaItem> {
self.agenda_action(paths::AGENDA_SCHEDULE, id, &ScheduleRequest { wake }).await
}
/// `POST /kernel/pairing`: issues a pairing code for a device acting for the caller
/// (spec 3.3 §10 K2, `adk-rust pair`).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `403` without the `operator` scope, `400` when the kernel
/// pairs no devices.
pub async fn issue_pairing_code(
&self,
request: &super::api::IssueRequest,
) -> Result<crate::pairing::IssuedCode> {
self.call(Method::POST, paths::PAIRING.to_string(), Some(request)).await
}
/// `POST /kernel/pairing/claim`: pairs this client's device with `code` and returns its
/// token, once (spec 3.3 §10 K2). The call needs no token and sends none, so a client
/// whose old token was revoked or expired re-pairs; send the one it returns with
/// [`with_bearer_token`](Self::with_bearer_token) from then on.
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `401` for a wrong, used or expired code.
pub async fn claim_pairing(
&self,
code: &str,
name: Option<&str>,
) -> Result<crate::pairing::ClaimedDevice> {
let body =
super::api::ClaimRequest { code: code.to_string(), name: name.map(str::to_string) };
let anonymous = Self { transport: self.transport.clone(), token: None };
anonymous.call(Method::POST, paths::PAIRING_CLAIM.to_string(), Some(&body)).await
}
/// `GET /kernel/devices`: every paired device (spec 3.3 §10 K2, `adk-rust pair list`).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered.
pub async fn devices(&self) -> Result<Vec<crate::pairing::DeviceView>> {
let answer: super::api::DevicesResponse =
self.call(Method::GET, paths::DEVICES.to_string(), None::<&()>).await?;
Ok(answer.devices)
}
/// `POST /kernel/devices/{id}/revoke`: removes a device; its token stops working at once
/// (spec 3.3 §10 K2, `adk-rust pair revoke`).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `404` for an unknown device.
pub async fn revoke_device(&self, id: &str) -> Result<crate::pairing::DeviceView> {
let path = paths::DEVICE_REVOKE.replace("{id}", &encode(id));
let answer: super::api::DeviceResponse = self.call(Method::POST, path, Some(&())).await?;
Ok(answer.device)
}
/// `GET /kernel/channels`: the paired chat accounts and the ones waiting for their code
/// to be accepted (spec 3.3 §10 K1).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered; a kernel without chat channels answers none.
pub async fn channel_accounts(&self) -> Result<super::api::ChannelAccountsResponse> {
self.call(Method::GET, paths::CHANNELS.to_string(), None::<&()>).await
}
/// `POST /kernel/channels/pair`: accepts the code a chat account was sent, so its
/// messages reach the caller's agents (spec 3.3 §10 K1).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `401` for a wrong or expired code, `400` when the kernel
/// has no chat channels.
pub async fn accept_channel(&self, code: &str) -> Result<super::api::ChannelAccountView> {
let body = super::api::AcceptChannelRequest { code: code.to_string() };
let answer: super::api::ChannelAccountResponse =
self.call(Method::POST, paths::CHANNEL_PAIR.to_string(), Some(&body)).await?;
Ok(answer.account)
}
/// `POST /kernel/channels/{id}/revoke`: removes a chat account (spec 3.3 §10 K1).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `404` for an unknown account.
pub async fn revoke_channel_account(&self, id: &str) -> Result<super::api::ChannelAccountView> {
let path = paths::CHANNEL_REVOKE.replace("{id}", &encode(id));
let answer: super::api::ChannelAccountResponse =
self.call(Method::POST, path, Some(&())).await?;
Ok(answer.account)
}
/// `POST /kernel/channels/waiting/{id}/dismiss`: dismisses a chat account waiting to be
/// paired; its code stops working (spec 3.3 §10 K1).
///
/// # Errors
///
/// Returns [`KernelError::ControlPlane`] when the kernel cannot be reached, or the
/// error the kernel answered: `404` for an account that is not waiting.
pub async fn dismiss_channel_request(
&self,
id: &str,
) -> Result<super::api::ChannelPairingView> {
let path = paths::CHANNEL_DISMISS.replace("{id}", &encode(id));
let answer: super::api::ChannelPairingResponse =
self.call(Method::POST, path, Some(&())).await?;
Ok(answer.waiting)
}
async fn agenda_action(
&self,
route: &str,
id: &str,
body: &impl Serialize,
) -> Result<AgendaItem> {
let path = route.replace("{id}", &encode(id));
let answer: AgendaItemResponse = self.call(Method::POST, path, Some(body)).await?;
Ok(answer.item)
}
async fn process_action(
&self,
route: &str,
pid: &str,
body: &impl Serialize,
) -> Result<Process> {
let path = route.replace("{pid}", &encode(pid));
let answer: ProcessResponse = self.call(Method::POST, path, Some(body)).await?;
Ok(answer.process)
}
/// Sends a JSON call and decodes the JSON answer, or the error the kernel answered.
pub(super) async fn call<T: DeserializeOwned>(
&self,
method: Method,
path: String,
body: Option<&impl Serialize>,
) -> Result<T> {
let body = body.map(serde_json::to_vec).transpose()?;
let answer = self.send(method, &path, body, Some(CALL_TIMEOUT)).await?;
let status = answer.status;
let bytes = answer.bytes().await?;
if !status.is_success() {
return Err(answer_error(status, &bytes));
}
serde_json::from_slice(&bytes).map_err(|error| KernelError::ControlPlane {
reason: format!(
"the kernel at {} answered {path} with a body this client cannot read ({error}); \
are the CLI and the kernel the same version?",
self.endpoint()
),
})
}
async fn send(
&self,
method: Method,
path: &str,
body: Option<Vec<u8>>,
timeout: Option<Duration>,
) -> Result<Answer> {
let path = format!("{}{path}", paths::API_PREFIX);
let unreachable = |error: &dyn std::fmt::Display| KernelError::ControlPlane {
reason: format!(
"could not reach the kernel at {}: {error}; start it with `adk-rust up`",
self.endpoint()
),
};
match &self.transport {
Transport::Http { base, client } => {
let mut request = client.request(method, format!("{base}{path}"));
if let Some(token) = &self.token {
request = request.bearer_auth(token);
}
if let Some(body) = body {
request = request.header("content-type", "application/json").body(body);
}
if let Some(timeout) = timeout {
request = request.timeout(timeout);
}
let response = request.send().await.map_err(|error| unreachable(&error))?;
let status = StatusCode::from_u16(response.status().as_u16())
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
let body = response
.bytes_stream()
.map(|chunk| {
chunk.map_err(|error| KernelError::ControlPlane {
reason: format!("the kernel's answer broke off: {error}"),
})
})
.boxed();
Ok(Answer { status, body })
}
local => {
let send = local_request(local, method, &path, self.token.as_deref(), body);
let answer = match timeout {
Some(timeout) => tokio::time::timeout(timeout, send)
.await
.map_err(|_| unreachable(&"the kernel did not answer in time"))?,
None => send.await,
};
answer.map_err(|error| unreachable(&error))
}
}
}
}
#[cfg(unix)]
async fn connect_local_socket(path: PathBuf, configured: &Path) -> Result<Option<ControlClient>> {
// A socket standing in for one too long for the home sits in a directory of its own,
// checked first, so no socket another user placed is reached (spec 3.3 G18).
if path != configured
&& let Some(dir) = path.parent()
&& !super::socket::check_dir(dir)?
{
return Ok(None);
}
match tokio::fs::symlink_metadata(&path).await {
Ok(_) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(error) => {
return Err(KernelError::ControlPlane {
reason: format!("could not read the kernel socket {}: {error}", path.display()),
});
}
}
match tokio::net::UnixStream::connect(&path).await {
Ok(_) => Ok(Some(ControlClient::socket(path))),
Err(error)
if matches!(
error.kind(),
std::io::ErrorKind::ConnectionRefused | std::io::ErrorKind::NotFound
) =>
{
Ok(None)
}
Err(error) => Err(KernelError::ControlPlane {
reason: format!(
"the kernel socket {} exists but cannot be opened: {error}; it admits only the \
OS user that runs the kernel",
path.display()
),
}),
}
}
#[cfg(all(not(unix), not(windows)))]
async fn connect_local_socket(_path: PathBuf, _configured: &Path) -> Result<Option<ControlClient>> {
Ok(None)
}
/// One HTTP/1.1 exchange over the socket at `socket`.
#[cfg(unix)]
async fn socket_exchange(
socket: &Path,
request: hyper::Request<http_body_util::Full<Bytes>>,
) -> std::result::Result<hyper::Response<hyper::body::Incoming>, String> {
let stream =
tokio::net::UnixStream::connect(socket).await.map_err(|error| error.to_string())?;
let (mut sender, connection) =
hyper::client::conn::http1::handshake(hyper_util::rt::TokioIo::new(stream))
.await
.map_err(|error| error.to_string())?;
tokio::spawn(async move {
if let Err(error) = connection.await {
tracing::debug!(error = %error, "kernel socket connection closed");
}
});
sender.send_request(request).await.map_err(|error| error.to_string())
}
/// One HTTP/1.1 exchange over the local transport `local`: the socket, or the pipe.
async fn local_request(
local: &Transport,
method: Method,
path: &str,
token: Option<&str>,
body: Option<Vec<u8>>,
) -> std::result::Result<Answer, String> {
use http_body_util::{BodyStream, Full};
let mut request =
hyper::Request::builder().method(method).uri(path).header(hyper::header::HOST, "localhost");
if let Some(token) = token {
request = request.header(hyper::header::AUTHORIZATION, format!("Bearer {token}"));
}
if body.is_some() {
request = request.header(hyper::header::CONTENT_TYPE, "application/json");
}
let request = request
.body(Full::new(Bytes::from(body.unwrap_or_default())))
.map_err(|error| error.to_string())?;
let response = match local {
#[cfg(unix)]
Transport::Socket(socket) => socket_exchange(socket, request).await?,
#[cfg(not(unix))]
Transport::Socket(socket) => {
let _ = request;
return Err(format!(
"the local control socket {} needs a Unix platform; on Windows the kernel \
serves a named pipe, which ControlClient::connect_local finds",
socket.display()
));
}
#[cfg(windows)]
Transport::Pipe(name) => super::pipe::request(name, request).await?,
Transport::Http { base, .. } => {
return Err(format!("{base} is reached over HTTP, not over a local transport"));
}
};
let status = response.status();
let body = BodyStream::new(response.into_body())
.filter_map(|frame| async move {
match frame {
Ok(frame) => frame.into_data().ok().map(Ok),
Err(error) => Some(Err(KernelError::ControlPlane {
reason: format!("the kernel's answer broke off: {error}"),
})),
}
})
.boxed();
Ok(Answer { status, body })
}
/// The error a kernel's non-success answer carries: its problem document, the server's
/// `{"error": "..."}` refusal, or the status alone.
fn answer_error(status: StatusCode, body: &[u8]) -> KernelError {
let value: Option<Value> = serde_json::from_slice(body).ok();
let error = value.as_ref().and_then(|value| value.get("error"));
if let Some(problem) = error.and_then(Value::as_object) {
let message = problem.get("message").and_then(Value::as_str).unwrap_or("").to_string();
let code = problem.get("code").and_then(Value::as_str).unwrap_or("");
let category = problem
.get("category")
.and_then(|category| serde_json::from_value(category.clone()).ok())
.unwrap_or_else(|| category_of(status));
return KernelError::Core(AdkError::new(
ErrorComponent::Server,
category,
known_code(code),
message,
));
}
let message = match error.and_then(Value::as_str) {
Some(message) => message.to_string(),
None => String::from_utf8_lossy(body).trim().chars().take(300).collect(),
};
match status {
StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => KernelError::Unauthorized {
reason: format!(
"{message}; pass the kernel's bearer token (--token or ADK_SERVE_TOKEN), or run \
the command as the user that runs the kernel"
),
},
_ => {
KernelError::ControlPlane { reason: format!("the kernel answered {status}: {message}") }
}
}
}
/// The category a status stands for, when an answer names none.
fn category_of(status: StatusCode) -> ErrorCategory {
match status.as_u16() {
400 => ErrorCategory::InvalidInput,
401 => ErrorCategory::Unauthorized,
403 => ErrorCategory::Forbidden,
404 => ErrorCategory::NotFound,
409 => ErrorCategory::Conflict,
429 => ErrorCategory::RateLimited,
501 => ErrorCategory::Unsupported,
503 => ErrorCategory::Unavailable,
_ => ErrorCategory::Internal,
}
}
/// The kernel code `code` names, or the control-plane code for one this client does not know.
fn known_code(code: &str) -> &'static str {
error::CODES.iter().copied().find(|known| *known == code).unwrap_or(error::CONTROL_PLANE_CODE)
}
/// Percent-encodes `text` for a path segment or a query value.
/// The body of a run now with `request_id`, when one is given.
fn run_now_request(request_id: Option<&str>) -> RunNowRequest {
match request_id {
Some(id) => RunNowRequest::default().with_request_id(id),
None => RunNowRequest::default(),
}
}
pub(super) fn encode(text: &str) -> String {
let mut encoded = String::with_capacity(text.len());
for byte in text.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
encoded.push(byte as char);
}
_ => encoded.push_str(&format!("%{byte:02X}")),
}
}
encoded
}
/// The event records of a server-sent event stream: the `data` of each event, keep-alive
/// comments skipped.
fn server_sent_records(
body: BoxStream<'static, Result<Bytes>>,
) -> BoxStream<'static, Result<EventRecord>> {
struct Reader {
body: BoxStream<'static, Result<Bytes>>,
buffer: Vec<u8>,
ready: VecDeque<Result<EventRecord>>,
done: bool,
}
let reader = Reader { body, buffer: Vec::new(), ready: VecDeque::new(), done: false };
stream::unfold(reader, |mut reader| async move {
loop {
if let Some(record) = reader.ready.pop_front() {
return Some((record, reader));
}
if reader.done {
return None;
}
match reader.body.next().await {
Some(Ok(chunk)) => {
reader.buffer.extend_from_slice(&chunk);
while let Some(end) = event_end(&reader.buffer) {
let block: Vec<u8> = reader.buffer.drain(..end.0).collect();
reader.buffer.drain(..end.1);
if let Some(record) = parse_event(&block) {
reader.ready.push_back(record);
}
}
}
Some(Err(error)) => {
reader.done = true;
reader.ready.push_back(Err(error));
}
None => reader.done = true,
}
}
})
.boxed()
}
/// Where the first event in `buffer` ends: its length and the separator's.
fn event_end(buffer: &[u8]) -> Option<(usize, usize)> {
let lf = buffer.windows(2).position(|window| window == b"\n\n").map(|at| (at, 2));
let crlf = buffer.windows(4).position(|window| window == b"\r\n\r\n").map(|at| (at, 4));
match (lf, crlf) {
(Some(lf), Some(crlf)) => Some(if lf.0 <= crlf.0 { lf } else { crlf }),
(found, None) | (None, found) => found,
}
}
/// The record in one event's lines, or `None` for an event with no data.
fn parse_event(block: &[u8]) -> Option<Result<EventRecord>> {
let text = String::from_utf8_lossy(block);
let data: Vec<&str> = text
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(|data| data.strip_prefix(' ').unwrap_or(data))
.collect();
if data.is_empty() {
return None;
}
Some(serde_json::from_str(&data.join("\n")).map_err(KernelError::from))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn segments_are_percent_encoded() {
assert_eq!(encode("run-1_a.b~c"), "run-1_a.b~c");
assert_eq!(encode("a/b c?d"), "a%2Fb%20c%3Fd");
}
#[tokio::test]
async fn server_sent_events_split_on_blank_lines_across_chunks() {
let record = |cursor: u64| {
serde_json::to_string(&EventRecord {
cursor,
at: chrono::Utc::now(),
event: crate::KernelEvent::Governance { frozen: true, reason: None },
})
.unwrap()
};
let text = format!(
": keep-alive\n\nid: 1\nevent: governance\ndata: {}\n\nid: 2\r\ndata: {}\r\n\r\n",
record(1),
record(2)
);
let (head, tail) = text.split_at(40);
let chunks = vec![Ok(Bytes::from(head.to_string())), Ok(Bytes::from(tail.to_string()))];
let records: Vec<_> = server_sent_records(stream::iter(chunks).boxed())
.map(|record| record.unwrap().cursor)
.collect()
.await;
assert_eq!(records, vec![1, 2]);
}
#[test]
fn answers_map_to_kernel_errors() {
let problem = AdkError::from(KernelError::NotFound { pid: "run-9".into() });
let body = serde_json::to_vec(&problem.to_problem_json()).unwrap();
let error = AdkError::from(answer_error(StatusCode::NOT_FOUND, &body));
assert_eq!((error.code, error.category), (error::NOT_FOUND_CODE, ErrorCategory::NotFound));
assert!(error.message.contains("run-9"));
let refused =
answer_error(StatusCode::UNAUTHORIZED, br#"{"error":"missing authorization"}"#);
assert!(matches!(refused, KernelError::Unauthorized { .. }), "{refused}");
let other = answer_error(StatusCode::BAD_GATEWAY, b"upstream down");
assert!(other.to_string().contains("upstream down"), "{other}");
}
#[test]
fn every_kernel_code_reaches_the_client() {
let refusals = [
KernelError::AgendaNotFound { item: "agenda-1".into() },
KernelError::AgendaState {
item: "agenda-1".into(),
state: crate::agenda::AgendaStateKind::Rejected,
action: "approved",
},
KernelError::Quota { partition: "work".into(), reason: "max running 2".into() },
];
for refusal in refusals {
let problem = AdkError::from(refusal);
let body = serde_json::to_vec(&problem.to_problem_json()).unwrap();
let status = StatusCode::from_u16(problem.http_status_code()).unwrap();
let error = AdkError::from(answer_error(status, &body));
assert_eq!((error.code, error.category), (problem.code, problem.category));
}
for code in error::CODES {
let problem =
AdkError::new(ErrorComponent::Server, ErrorCategory::Conflict, code, "refused");
let body = serde_json::to_vec(&problem.to_problem_json()).unwrap();
assert_eq!(AdkError::from(answer_error(StatusCode::CONFLICT, &body)).code, *code);
}
// A code from a crate the kernel composes, which this client does not list.
let foreign = AdkError::new(
ErrorComponent::Server,
ErrorCategory::Conflict,
"session.not_found",
"refused",
);
let body = serde_json::to_vec(&foreign.to_problem_json()).unwrap();
let error = AdkError::from(answer_error(StatusCode::CONFLICT, &body));
assert_eq!((error.code, error.message.as_str()), (error::CONTROL_PLANE_CODE, "refused"));
}
}