kian-plugin 0.1.1

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

use std::collections::VecDeque;
use std::io::Read;
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use futures::channel::mpsc::Sender;
use futures::channel::{mpsc, oneshot};
use futures::{FutureExt, StreamExt};

use super::super::failure::Cause;
use super::super::registry::{HookFn, HookPayload, HookReply, ReplyErrCode};
use super::super::wire::sha256_hex;
use super::frame::{
    DecodeError, Hello, HookFrame, ReplyFrame, decode_ready, decode_reply, encode_bye,
};
use super::line::{BoundedLineReader, LineError, write_line};
use super::manifest::{LimitsSection, Manifest, Refusal, RefusalCode, Required};

/// §7.5's graceful-stop grace before the group kill.
const STOP_GRACE: Duration = Duration::from_millis(2_000);
/// Bounded reap after a kill: a stuck group must not hang the reaper
/// (§10.3 step 5).
const REAP_BOUND: Duration = Duration::from_millis(2_000);

// ---------------------------------------------------------------------
// Failure window and quarantine (§6.4, §6.5)
// ---------------------------------------------------------------------

/// The kind tag every window entry carries — the deque is kind-tagged
/// so a tripping window can say whether it contains only stderr-rate
/// entries (§6.5).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FailureKind {
    /// `err:deadline` or a deadline kill.
    Deadline,
    /// A crash: process exit while a dispatch or handshake was pending.
    Crash,
    /// An `err:malformed` / `err:unauthorized` reply, or an unparseable
    /// line (§6.4's window-counted speech failures).
    ReplyDefect,
    /// The retained stderr-rate bound (§10.5).
    StderrRate,
}

/// §6.5's quarantine reasons — each names the mechanism that produced
/// it (window trip vs. immediate one strike).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuarantineReason {
    /// The §6.4 window tripped with at least one non-stderr entry.
    RestartsExhausted { failures: usize, window_ms: u64 },
    /// The window tripped on stderr-rate entries only.
    StderrRate { failures: usize, window_ms: u64 },
    /// Unknown / duplicate / unsolicited frame id — immediate (§6.5).
    ProtocolViolation { detail: String },
    /// A line over `max_payload_kib` in either direction — immediate.
    FrameOverflow { direction: &'static str },
    /// The binary's digest no longer matches at respawn — immediate
    /// (at boot the same condition is a `plugin/refuse`).
    DigestMismatch,
}

/// The §6.4 state machine: one kind-tagged window plus a reported (not
/// thresholded) respawn counter.
#[derive(Debug, Default)]
pub struct Counters {
    failures: Mutex<VecDeque<(Instant, FailureKind)>>,
    restarts: AtomicU64,
}

impl Counters {
    /// Records a window-counted failure: push `now()`, drop entries
    /// older than the window, trip quarantine at the threshold. A
    /// success (any well-formed reply) clears the deque — the failures
    /// are consecutive by definition (§6.4).
    pub fn record_failure(
        &self,
        kind: FailureKind,
        limits: &LimitsSection,
    ) -> Option<QuarantineReason> {
        let mut failures = self
            .failures
            .lock()
            .expect("kian-plugin: sidecar counters lock poisoned");
        failures.push_back((Instant::now(), kind));
        let window = Duration::from_millis(limits.restart_window_ms);
        while failures
            .front()
            .is_some_and(|(at, _)| at.elapsed() > window)
        {
            failures.pop_front();
        }
        if failures.len() as u64 >= limits.restarts_per_window {
            let only_stderr = failures.iter().all(|(_, k)| *k == FailureKind::StderrRate);
            let count = failures.len();
            return Some(if only_stderr {
                QuarantineReason::StderrRate {
                    failures: count,
                    window_ms: limits.restart_window_ms,
                }
            } else {
                QuarantineReason::RestartsExhausted {
                    failures: count,
                    window_ms: limits.restart_window_ms,
                }
            });
        }
        None
    }

    /// A well-formed reply clears the consecutive-failure window.
    pub fn record_success(&self) {
        self.failures
            .lock()
            .expect("kian-plugin: sidecar counters lock poisoned")
            .clear();
    }

    /// Counts an actual respawn — reported, never thresholded (§6.4).
    pub fn record_restart(&self) -> u64 {
        self.restarts.fetch_add(1, Ordering::Relaxed) + 1
    }

    pub fn restarts(&self) -> u64 {
        self.restarts.load(Ordering::Relaxed)
    }

    pub fn window_len(&self) -> usize {
        self.failures
            .lock()
            .expect("kian-plugin: sidecar counters lock poisoned")
            .len()
    }
}

// ---------------------------------------------------------------------
// Jobs and outcomes
// ---------------------------------------------------------------------

enum Job {
    Dispatch {
        frame: HookFrame,
        reply: oneshot::Sender<ReplyOutcome>,
    },
    Stop {
        done: oneshot::Sender<()>,
    },
}

/// What one dispatch resolved to, before the adapter shapes it for the
/// bus's classifier.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReplyOutcome {
    /// A decoded reply frame (including `err` replies — the bus
    /// classifies those, not the runner).
    Reply(HookReply),
    /// A protocol violation whose dispatch outcome is as-if-absent
    /// (§7.4): quarantining is the runner's job, deciding is not.
    AsIfAbsent { detail: String },
    /// §7.5's fail-fast: one dispatch is in flight; never queued.
    Busy,
    /// The handler cannot answer — the adapter maps this to the
    /// self-declared unreachability reply so the §11.1 grant-keyed
    /// class default applies.
    Unreachable { cause: Cause, detail: String },
}

// ---------------------------------------------------------------------
// The runner
// ---------------------------------------------------------------------

struct Inner {
    name: String,
    manifest: Manifest,
    canonical_path: String,
    /// The job slot (§7.6: bounded internal channels — one per
    /// sidecar). Mutex'd because the futures sender's `try_send`
    /// needs `&mut`; the in-flight flag below is what actually
    /// serializes dispatches.
    tx: Mutex<Sender<Job>>,
    next_dispatch_id: AtomicU64,
    counters: Counters,
    quarantine: Mutex<Option<QuarantineReason>>,
    /// §7.5's single in-flight rule, enforced before the job channel:
    /// while a dispatch is unanswered the runner is busy, and a second
    /// dispatch **never queues** — the channel alone could not express
    /// that (a buffered slot is a queue).
    in_flight: AtomicBool,
}

/// Resets the in-flight flag when dropped — cancellation-safe across
/// the await point.
struct InFlightGuard<'a>(&'a AtomicBool);

impl Drop for InFlightGuard<'_> {
    fn drop(&mut self) {
        self.0.store(false, Ordering::SeqCst);
    }
}

/// A running sidecar: one child process, one io thread, one in-flight
/// dispatch. Cheap to clone (an `Arc` inside); [`SidecarRunner::stop`]
/// is the graceful shutdown (`bye`, half-close, bounded grace, group
/// kill — §7.5).
#[derive(Clone)]
pub struct SidecarRunner {
    inner: Arc<Inner>,
}

impl std::fmt::Debug for SidecarRunner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SidecarRunner")
            .field("name", &self.inner.name)
            .field("quarantined", &self.quarantined())
            .finish_non_exhaustive()
    }
}

/// A watchdog: sleeps the deadline, then — only if the awaited event
/// has not arrived — records the deadline cause and kills the child's
/// process group (§10.3 step 5). The read side consults the cause when
/// its blocking read unblocks, which distinguishes a deadline kill
/// from a crash.
#[derive(Clone)]
struct WatchdogHandle {
    answered: Arc<AtomicBool>,
    deadline_fired: Arc<Mutex<bool>>,
    pid: Arc<AtomicU64>,
}

impl WatchdogHandle {
    fn arm(&self, timeout: Duration) {
        let handle = self.clone();
        std::thread::spawn(move || {
            std::thread::sleep(timeout);
            if !handle.answered.load(Ordering::SeqCst) {
                // Set the cause *before* the kill so the reader that
                // unblocks on the dying pipe sees it (§11.2: the child
                // is killed for a timeout, never left running).
                *handle.deadline_fired.lock().unwrap() = true;
                kill_group(handle.pid.load(Ordering::SeqCst) as u32);
            }
        });
    }

    fn mark_answered(&self) -> bool {
        self.answered.swap(true, Ordering::SeqCst)
    }
}

/// One live child plus its carrier halves.
struct Session {
    child: Child,
    /// `Option` so the graceful stop can half-close (§7.5); `None`
    /// after that is a crash-class unreachability for any late write.
    stdin: Option<ChildStdin>,
    stdout: BoundedLineReader<ChildStdout>,
    pid: u32,
    /// Cleared when the session must not be reused (killed, crashed,
    /// overflowed); the io loop respawns then.
    alive: bool,
}

/// Why a spawn+handshake failed — mapped to boot refusals (§6.2 rules
/// 6/12/13) on the first spawn and to window failures on respawns.
enum HandshakeFailure {
    DigestMismatch,
    Spawn(String),
    Timeout,
    ReadyMismatch(String),
    AbiMismatch,
    Crashed(String),
}

impl SidecarRunner {
    /// Spawns and handshakes one sidecar instance (§6.3's
    /// `configured → spawning → handshaking → ready`). Blocks for at
    /// most the manifest's `startup_timeout_ms` plus slack; a failure
    /// is the typed per-instance refusal the caller audits
    /// (`plugin/refuse`, fatal iff `required` — the caller's policy,
    /// §6.2).
    pub fn start(manifest: Manifest, canonical_path: &std::path::Path) -> Result<Self, Refusal> {
        let canonical =
            std::fs::canonicalize(canonical_path).unwrap_or_else(|_| canonical_path.to_path_buf());
        let name = manifest.plugin.name.clone();
        let (tx, rx) = mpsc::channel(1);
        let inner = Arc::new(Inner {
            name: name.clone(),
            manifest,
            canonical_path: canonical.display().to_string(),
            tx: Mutex::new(tx),
            next_dispatch_id: AtomicU64::new(0),
            counters: Counters::default(),
            quarantine: Mutex::new(None),
            in_flight: AtomicBool::new(false),
        });
        let (handshake_tx, handshake_rx) = std::sync::mpsc::channel::<Result<(), Refusal>>();
        let thread_inner = Arc::clone(&inner);
        let spawn_result = std::thread::Builder::new()
            .name(format!("kian-sidecar-{name}"))
            .spawn(move || io_thread(thread_inner, rx, handshake_tx));
        if spawn_result.is_err() {
            return Err(Refusal {
                code: RefusalCode::SpawnFailed,
                message: format!("could not spawn the io thread for `{name}`"),
                required: inner.manifest.required(),
            });
        }
        let slack = Duration::from_millis(2_000);
        let bound = Duration::from_millis(inner.manifest.process.startup_timeout_ms) + slack;
        match handshake_rx.recv_timeout(bound) {
            Ok(Ok(())) => Ok(Self { inner }),
            Ok(Err(refusal)) => {
                audit_refuse(&refusal);
                Err(refusal)
            }
            Err(_) => Err(Refusal {
                code: RefusalCode::SpawnFailed,
                message: format!(
                    "`{name}`: hello→ready did not complete within startup_timeout_ms"
                ),
                required: inner.manifest.required(),
            }),
        }
    }

    /// The instance's plugin name (audit identity).
    pub fn name(&self) -> &str {
        &self.inner.name
    }

    /// The counters handle (§6.4) — reported, never thresholded
    /// outside the runner.
    pub fn counters(&self) -> &Counters {
        &self.inner.counters
    }

    /// The quarantine reason, if the instance is quarantined (§6.5:
    /// one-way for the process lifetime).
    pub fn quarantined(&self) -> Option<QuarantineReason> {
        self.inner
            .quarantine
            .lock()
            .expect("kian-plugin: quarantine lock poisoned")
            .clone()
    }

    /// Dispatches one frame to the sidecar: fails fast to
    /// [`ReplyOutcome::Busy`] while a dispatch is in flight (§7.5 —
    /// never queued), resolves unreachability to the cause the adapter
    /// turns into the §11.1 class default.
    pub async fn dispatch(&self, frame: HookFrame) -> ReplyOutcome {
        if let Some(reason) = self.quarantined() {
            return ReplyOutcome::Unreachable {
                cause: Cause::Quarantine,
                detail: format!("quarantined: {reason:?}"),
            };
        }
        if self.inner.in_flight.swap(true, Ordering::SeqCst) {
            return ReplyOutcome::Busy;
        }
        let _in_flight = InFlightGuard(&self.inner.in_flight);
        let (reply_tx, reply_rx) = oneshot::channel();
        let sent = self
            .inner
            .tx
            .lock()
            .expect("kian-plugin: sidecar job-slot lock poisoned")
            .try_send(Job::Dispatch {
                frame,
                reply: reply_tx,
            });
        match sent {
            Ok(()) => match reply_rx.await {
                Ok(outcome) => outcome,
                Err(_) => ReplyOutcome::Unreachable {
                    cause: Cause::Crash,
                    detail: "sidecar stopped".to_string(),
                },
            },
            // With the in-flight flag held, a send failure means the
            // receiver is gone — quarantined or stopped (the futures
            // `TrySendError` does not distinguish; the quarantine
            // check above already reported the reason if there is
            // one).
            Err(_) => ReplyOutcome::Unreachable {
                cause: Cause::Quarantine,
                detail: "sidecar io thread exited (quarantined or stopped)".to_string(),
            },
        }
    }

    /// Graceful stop (§7.5): `bye`, close stdin (half-close), wait a
    /// bounded grace period, then kill the whole process group. Blocks
    /// for at most the grace; call at shutdown, not from async code.
    pub fn stop(&self) {
        let (done_tx, done_rx) = oneshot::channel();
        let sent = self
            .inner
            .tx
            .lock()
            .expect("kian-plugin: sidecar job-slot lock poisoned")
            .try_send(Job::Stop { done: done_tx });
        if sent.is_ok() {
            let _ = futures::executor::block_on(done_rx);
        }
    }

    /// A fresh dispatch id. Process-unique per sidecar (§7.3 wants a
    /// fresh uuid; K10 forbids the `uuid` edge this round, so the id
    /// is the sidecar-tagged counter the same way the tier-0 bus ids
    /// are — uniqueness and reply attribution are what the protocol
    /// needs).
    fn next_dispatch_id(&self) -> String {
        format!(
            "sc-{}-{}",
            self.inner.name,
            self.inner.next_dispatch_id.fetch_add(1, Ordering::Relaxed)
        )
    }

    /// Tier-0-shaped handlers for the bus: one per subscribed hook,
    /// carrying the manifest's grants and order. The plugin name is
    /// leaked for the `&'static str` the tier-0 `HandlerId` requires —
    /// one process per plugin per server lifetime (R11), so the leak
    /// is bounded by the instance count.
    pub fn hook_fns(self: &Arc<Self>) -> Vec<(super::super::registry::HandlerId, HookFn)> {
        let mut out = Vec::new();
        for hook in self.inner.manifest.grants.hooks.clone() {
            let plugin: &'static str = Box::leak(self.inner.name.clone().into_boxed_str());
            let runner = Arc::clone(self);
            let hook_for_closure = hook.clone();
            let deadline_ms = self.inner.manifest.limits.deadline_ms;
            let handler = super::super::registry::HandlerId {
                plugin,
                hook,
                grants: self.inner.manifest.grants.grants.clone(),
                order: self.inner.manifest.plugin.order,
            };
            let f: HookFn = Arc::new(move |payload: &HookPayload| {
                let runner = Arc::clone(&runner);
                // Extract everything borrowed before the async block.
                let frame_info = payload.frame().cloned();
                let payload_value = payload_value(payload);
                let dispatch_id = runner.next_dispatch_id();
                let hook = hook_for_closure.clone();
                async move {
                    let Some(frame_info) = frame_info else {
                        tracing::warn!(
                            target: "kian::hook",
                            hook = %hook,
                            plugin = plugin,
                            "sidecar dispatch without frame identity — skipped (R11: no implicit identity)"
                        );
                        return HookReply::Ack { note: None };
                    };
                    let hook_for_log = hook.clone();
                    let frame = match HookFrame::new(
                        hook,
                        &frame_info,
                        dispatch_id,
                        deadline_ms,
                        payload_value,
                    ) {
                        Ok(frame) => frame,
                        Err(e) => {
                            // The epoch rule's local guard (§7.2): the
                            // call site built an incoherent frame. Skip
                            // the sidecar rather than deny on a
                            // harness-side defect.
                            tracing::error!(
                                target: "kian::hook",
                                hook = %hook_for_log,
                                plugin = plugin,
                                error = %e.message,
                                "sidecar frame rejected — dispatch skipped"
                            );
                            return HookReply::Ack { note: None };
                        }
                    };
                    match runner.dispatch(frame).await {
                        ReplyOutcome::Reply(reply) => reply,
                        ReplyOutcome::AsIfAbsent { .. } => {
                            tracing::info!(
                                target: "kian::hook",
                                hook = %hook_for_log,
                                plugin = plugin,
                                outcome = "discarded",
                                "hook-discard"
                            );
                            HookReply::Ack { note: None }
                        }
                        ReplyOutcome::Busy => {
                            // §7.5's busy audit line — never queued.
                            tracing::info!(
                                target: "kian::hook",
                                hook = %hook_for_log,
                                plugin = plugin,
                                busy = true,
                                "hook-default"
                            );
                            HookReply::Err {
                                code: ReplyErrCode::Crashed,
                                message: "busy: one dispatch in flight".to_string(),
                            }
                        }
                        ReplyOutcome::Unreachable { cause, detail } => HookReply::Err {
                            code: match cause {
                                Cause::Deadline => ReplyErrCode::Deadline,
                                _ => ReplyErrCode::Crashed,
                            },
                            message: format!("{detail} ({})", cause.as_str()),
                        },
                    }
                }
                .boxed()
            });
            out.push((handler, f));
        }
        out
    }
}

// ---------------------------------------------------------------------
// The io thread
// ---------------------------------------------------------------------

fn io_thread(
    inner: Arc<Inner>,
    mut rx: mpsc::Receiver<Job>,
    handshake: std::sync::mpsc::Sender<Result<(), Refusal>>,
) {
    let mut session = match spawn_session(&inner, true) {
        Ok(session) => {
            let _ = handshake.send(Ok(()));
            session
        }
        Err(failure) => {
            let refusal = boot_refusal(&inner, failure);
            let _ = handshake.send(Err(refusal.clone()));
            audit_refuse(&refusal);
            return;
        }
    };
    loop {
        if inner.quarantine_locked().is_some() {
            // §6.5: a quarantined instance is skipped at the bus and
            // never respawns; the io thread retires.
            return;
        }
        let job = match futures::executor::block_on(rx.next()) {
            Some(job) => job,
            None => {
                graceful_stop(&mut session);
                return;
            }
        };
        match job {
            Job::Stop { done } => {
                graceful_stop(&mut session);
                let _ = done.send(());
                return;
            }
            Job::Dispatch { frame, reply } => {
                let outcome = serve_dispatch(&inner, &mut session, frame);
                let _ = reply.send(outcome);
                if !session.alive && inner.quarantine_locked().is_none() {
                    // §6.3: respawn after a crash or deadline kill,
                    // inside the failure window, digest re-verified,
                    // re-handshaked before the next dispatch.
                    session = match respawn_until_ready(&inner) {
                        Some(session) => session,
                        None => return, // quarantined (or window tripped)
                    };
                }
            }
        }
    }
}

/// Serves exactly one dispatch on a live session (§7.5: the io thread
/// is the serialization point; nothing else touches the child).
fn serve_dispatch(inner: &Arc<Inner>, session: &mut Session, frame: HookFrame) -> ReplyOutcome {
    let max = inner.manifest.limits.max_frame_bytes();
    let line = frame.encode();
    let Some(stdin) = session.stdin.as_mut() else {
        record_window_failure(inner, FailureKind::Crash);
        kill_session(session);
        return ReplyOutcome::Unreachable {
            cause: Cause::Crash,
            detail: "stdin already closed (stop in progress)".to_string(),
        };
    };
    match write_line(stdin, &line, max) {
        Err(LineError::Overflow { scanned, max }) => {
            quarantine(
                inner,
                QuarantineReason::FrameOverflow {
                    direction: "outbound",
                },
            );
            kill_session(session);
            return ReplyOutcome::Unreachable {
                cause: Cause::Crash,
                detail: format!("frame_overflow: outbound line {scanned} > {max} bytes"),
            };
        }
        Err(LineError::Io(e)) => {
            // The write itself failed: a crash.
            if let Some(reason) = record_window_failure(inner, FailureKind::Crash) {
                quarantine(inner, reason);
            }
            kill_session(session);
            return ReplyOutcome::Unreachable {
                cause: Cause::Crash,
                detail: format!("stdin write failed: {e}"),
            };
        }
        Ok(()) => {}
    }

    let watchdog = WatchdogHandle {
        answered: Arc::new(AtomicBool::new(false)),
        deadline_fired: Arc::new(Mutex::new(false)),
        pid: Arc::new(AtomicU64::new(u64::from(session.pid))),
    };
    watchdog.arm(Duration::from_millis(inner.manifest.limits.deadline_ms));

    match session.stdout.read_line(max) {
        Err(LineError::Overflow { scanned, max }) => {
            // §7.6: immediate one-strike quarantine, and the class
            // default for this dispatch — never a window increment.
            quarantine(
                inner,
                QuarantineReason::FrameOverflow {
                    direction: "inbound",
                },
            );
            tracing::info!(
                target: "kian::hook",
                plugin = %inner.name,
                direction = "inbound",
                scanned = scanned,
                max = max,
                "hook-quarantine-overflow"
            );
            kill_session(session);
            ReplyOutcome::Unreachable {
                cause: Cause::Crash,
                detail: format!("frame_overflow: inbound line {scanned} > {max} bytes"),
            }
        }
        Err(LineError::Io(e)) => {
            let deadline = *watchdog.deadline_fired.lock().unwrap();
            kill_session(session);
            let kind = if deadline {
                FailureKind::Deadline
            } else {
                // EOF without the expected reply is a crash, never an
                // empty allow (§7.5).
                FailureKind::Crash
            };
            if let Some(reason) = record_window_failure(inner, kind) {
                // The window tripped with this very failure (§6.4).
                quarantine(inner, reason);
            }
            if deadline {
                ReplyOutcome::Unreachable {
                    cause: Cause::Deadline,
                    detail: "deadline: child killed over the dispatch deadline".to_string(),
                }
            } else {
                ReplyOutcome::Unreachable {
                    cause: Cause::Crash,
                    detail: format!("crash: child pipe closed ({e})"),
                }
            }
        }
        Ok(line) => {
            watchdog.mark_answered();
            match decode_reply(&line) {
                Err(DecodeError::Malformed(detail)) => {
                    // An unparseable reply is the err:malformed class
                    // — discarded as an opinion at the bus and
                    // window-counted here (§6.4, §11.1).
                    record_window_failure(inner, FailureKind::ReplyDefect);
                    ReplyOutcome::Reply(HookReply::Err {
                        code: ReplyErrCode::Malformed,
                        message: detail,
                    })
                }
                Ok((reply_id, reply_frame)) => {
                    let id_ok = reply_id.as_ref().map(|id| id == &frame.id).unwrap_or(true); // id-less err: the one in-flight
                    if !id_ok {
                        // §7.4: a second/unsolicited reply is an
                        // immediate quarantine, and this dispatch is
                        // as-if-absent.
                        quarantine(
                            inner,
                            QuarantineReason::ProtocolViolation {
                                detail: format!(
                                    "reply id `{reply_id:?}` ≠ dispatch id `{}`",
                                    frame.id
                                ),
                            },
                        );
                        kill_session(session);
                        return ReplyOutcome::AsIfAbsent {
                            detail: "protocol_violation: reply for another dispatch id".to_string(),
                        };
                    }
                    match reply_frame {
                        ReplyFrame::Err { code, message }
                            if matches!(
                                code,
                                ReplyErrCode::Deadline
                                    | ReplyErrCode::Malformed
                                    | ReplyErrCode::Unauthorized
                            ) =>
                        {
                            // §6.4's window-counted err replies.
                            let kind = if code == ReplyErrCode::Deadline {
                                FailureKind::Deadline
                            } else {
                                FailureKind::ReplyDefect
                            };
                            if let Some(reason) = record_window_failure(inner, kind) {
                                quarantine(inner, reason);
                                kill_session(session);
                            }
                            ReplyOutcome::Reply(HookReply::Err { code, message })
                        }
                        other => {
                            // Any well-formed reply is a success — it
                            // clears the consecutive window (§6.4).
                            inner.counters.record_success();
                            ReplyOutcome::Reply(frame_to_reply(other))
                        }
                    }
                }
            }
        }
    }
}

fn frame_to_reply(frame: ReplyFrame) -> HookReply {
    match frame {
        ReplyFrame::Ack { note } => HookReply::Ack { note },
        ReplyFrame::FlowNext => HookReply::FlowNext,
        ReplyFrame::FlowStop { reason } => HookReply::FlowStop { reason },
        ReplyFrame::BailNone => HookReply::BailNone,
        ReplyFrame::BailDeny { reason } => HookReply::BailDeny { reason },
        ReplyFrame::Err { code, message } => HookReply::Err { code, message },
    }
}

/// Respawns after a crash/deadline kill until the child is ready again
/// or the failure window trips quarantine (§6.3, §6.4) — each failed
/// attempt counts, so the loop is bounded by `restarts_per_window`.
fn respawn_until_ready(inner: &Arc<Inner>) -> Option<Session> {
    loop {
        match spawn_session(inner, false) {
            Ok(session) => {
                let restarts = inner.counters.record_restart();
                tracing::info!(
                    target: "kian::hook",
                    plugin = %inner.name,
                    restarts = restarts,
                    digest_ok = true,
                    "hook-respawn"
                );
                return Some(session);
            }
            Err(HandshakeFailure::DigestMismatch) => {
                // §6.5: digest mismatch at respawn — immediate.
                quarantine(inner, QuarantineReason::DigestMismatch);
                return None;
            }
            Err(_) => {
                if let Some(reason) = record_window_failure(inner, FailureKind::Crash) {
                    quarantine(inner, reason);
                    return None;
                }
            }
        }
    }
}

// ---------------------------------------------------------------------
// Spawn + handshake (§10.3, §7.3)
// ---------------------------------------------------------------------

fn spawn_session(inner: &Arc<Inner>, first: bool) -> Result<Session, HandshakeFailure> {
    let manifest = &inner.manifest;
    // §10.3 step 6: digest verification at spawn and at every respawn.
    match digest_of(&manifest.process.argv[0]) {
        Some(digest) if digest == manifest.process.sha256 => {}
        Some(_) => return Err(HandshakeFailure::DigestMismatch),
        None => {
            return Err(HandshakeFailure::Spawn(format!(
                "argv[0] `{}` unreadable for digest verification",
                manifest.process.argv[0]
            )));
        }
    }

    let mut command = Command::new(&manifest.process.argv[0]);
    command
        .args(&manifest.process.argv[1..])
        // §10.3 step 2: env_clear first, then exactly env_allow with
        // the server's values — no PATH, HOME, proxies or credentials.
        .env_clear();
    for name in &manifest.process.env_allow {
        if let Ok(value) = std::env::var(name) {
            command.env(name, value);
        }
    }
    // §10.3 step 3: dedicated cwd (the manifest's, or a fresh empty
    // directory under the server's own temp — never an inherited one).
    let _owned_cwd;
    match &manifest.process.cwd {
        Some(cwd) => {
            command.current_dir(cwd);
        }
        None => {
            let dir = std::env::temp_dir().join(format!(
                "kian-sidecar-{}-{}",
                manifest.plugin.name,
                std::process::id()
            ));
            if std::fs::create_dir_all(&dir).is_err() {
                return Err(HandshakeFailure::Spawn(format!(
                    "could not create the runtime cwd `{}`",
                    dir.display()
                )));
            }
            _owned_cwd = dir;
            command.current_dir(&_owned_cwd);
        }
    }
    // §10.3 step 4: pipe exactly stdin/stdout/stderr; step 5: a
    // killable process group.
    command
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        command.process_group(0);
    }
    let mut child = command
        .spawn()
        .map_err(|e| HandshakeFailure::Spawn(format!("spawn failed: {e}")))?;
    let pid = child.id();
    let mut stdin = child
        .stdin
        .take()
        .ok_or_else(|| HandshakeFailure::Spawn("stdin pipe missing".to_string()))?;
    let stdout = child
        .stdout
        .take()
        .ok_or_else(|| HandshakeFailure::Spawn("stdout pipe missing".to_string()))?;
    let stderr = child
        .stderr
        .take()
        .ok_or_else(|| HandshakeFailure::Spawn("stderr pipe missing".to_string()))?;

    // §7.6/§10.5: stderr is drained concurrently, bounded, tagged,
    // never parsed, never logged with content.
    let stderr_inner = Arc::clone(inner);
    let _stderr_thread = std::thread::Builder::new()
        .name(format!("kian-sidecar-{}-stderr", manifest.plugin.name))
        .spawn(move || drain_stderr(stderr_inner, stderr));

    let mut reader = BoundedLineReader::new(stdout);
    let hello = Hello {
        manifest: inner.canonical_path.clone(),
        name: manifest.plugin.name.clone(),
        version: manifest.plugin.version.clone(),
        abi: manifest.plugin.abi,
        hooks: manifest
            .grants
            .hooks
            .iter()
            .map(|h| h.to_string())
            .collect(),
        capabilities: manifest
            .grants
            .grants
            .iter()
            .map(|g| g.as_str().to_string())
            .collect(),
        deadline_default_ms: manifest.limits.deadline_ms,
        max_frame_bytes: manifest.limits.max_frame_bytes(),
    };
    let max = manifest.limits.max_frame_bytes();
    match write_line(&mut stdin, &hello.encode(), max) {
        Err(LineError::Overflow { .. }) => {
            kill_child(&mut child);
            return Err(HandshakeFailure::ReadyMismatch(
                "hello over the frame bound".to_string(),
            ));
        }
        Err(LineError::Io(e)) => {
            kill_child(&mut child);
            return Err(HandshakeFailure::Crashed(format!(
                "hello write failed: {e}"
            )));
        }
        Ok(()) => {}
    }

    let watchdog = WatchdogHandle {
        answered: Arc::new(AtomicBool::new(false)),
        deadline_fired: Arc::new(Mutex::new(false)),
        pid: Arc::new(AtomicU64::new(u64::from(pid))),
    };
    // Rule 13 (§6.2): hello→ready within startup_timeout_ms.
    watchdog.arm(Duration::from_millis(manifest.process.startup_timeout_ms));

    let ready_line = match reader.read_line(max) {
        Ok(line) => {
            watchdog.mark_answered();
            line
        }
        Err(LineError::Overflow { .. }) => {
            kill_child(&mut child);
            return Err(HandshakeFailure::ReadyMismatch(
                "ready over the frame bound".to_string(),
            ));
        }
        Err(LineError::Io(e)) => {
            let deadline = *watchdog.deadline_fired.lock().unwrap();
            kill_child(&mut child);
            return Err(if deadline {
                HandshakeFailure::Timeout
            } else {
                HandshakeFailure::Crashed(format!("child closed stdout before ready ({e})"))
            });
        }
    };
    let ready = match decode_ready(&ready_line) {
        Ok(ready) => ready,
        Err(DecodeError::Malformed(detail)) => {
            kill_child(&mut child);
            return Err(HandshakeFailure::ReadyMismatch(detail));
        }
    };
    // §7.3 step 2: the ready must restate the identity exactly, with
    // set-equal hooks/capabilities — typed refusals otherwise.
    if ready.abi != manifest.plugin.abi {
        kill_child(&mut child);
        return Err(HandshakeFailure::AbiMismatch);
    }
    if ready.manifest != inner.canonical_path
        || ready.name != manifest.plugin.name
        || ready.version != manifest.plugin.version
        || !set_equal(&ready.hooks, &hello.hooks)
        || !set_equal(&ready.capabilities, &hello.capabilities)
    {
        kill_child(&mut child);
        return Err(HandshakeFailure::ReadyMismatch(format!(
            "ready restates a different identity (manifest={}, name={}, version={}, hooks/capabilities sets differ)",
            ready.manifest, ready.name, ready.version
        )));
    }
    let _ = first; // boot refusal mapping happens in the caller
    Ok(Session {
        child,
        stdin: Some(stdin),
        stdout: reader,
        pid,
        alive: true,
    })
}

/// §10.5's stderr discipline: per-line bound (drop + one count per
/// drop-burst), retained-rate bound (one count per minute), all
/// content discarded — never parsed, never logged.
///
/// A *drop burst* is a run of oversized lines not separated by more
/// than a second: coalescing is meant to keep one fragmented huge
/// line from counting many times, not to make an endless flood count
/// once (§6.5's stderr-only window mechanism requires the flood to be
/// able to trip it).
fn drain_stderr(inner: Arc<Inner>, stderr: std::process::ChildStderr) {
    let limits = inner.manifest.limits;
    let line_max = (limits.max_stderr_line_kib as usize) * 1024;
    let rate_max = (limits.stderr_rate_kib_per_min as usize) * 1024;
    let mut reader = BoundedLineReader::new(stderr);
    let mut last_drop: Option<Instant> = None;
    let mut retained: VecDeque<(Instant, usize)> = VecDeque::new();
    let mut retained_bytes = 0usize;
    let mut last_rate_note: Option<Instant> = None;
    loop {
        match reader.read_line(line_max) {
            Ok(line) => {
                last_drop = None; // an in-bounds line ends any burst
                let now = Instant::now();
                retained.push_back((now, line.len()));
                retained_bytes = retained_bytes.saturating_add(line.len());
                while retained
                    .front()
                    .is_some_and(|(at, _)| now.duration_since(*at) > Duration::from_secs(60))
                {
                    let (_, size) = retained.pop_front().unwrap();
                    retained_bytes = retained_bytes.saturating_sub(size);
                }
                if retained_bytes > rate_max
                    && last_rate_note
                        .is_none_or(|at| now.duration_since(at) > Duration::from_secs(60))
                {
                    last_rate_note = Some(now);
                    tracing::info!(
                        target: "kian::hook",
                        plugin = %inner.name,
                        kind = "stderr_rate",
                        "stderr rate bound exceeded — excess dropped"
                    );
                    if let Some(reason) = record_window_failure(&inner, FailureKind::StderrRate) {
                        quarantine(&inner, reason);
                        return;
                    }
                }
            }
            Err(LineError::Overflow { scanned, max }) => {
                // Drop the oversized line; count at most one
                // stderr_overflow per second — a fragmented huge line
                // counts once, an endless flood counts once per second
                // and can trip the window (§6.4, §6.5).
                tracing::info!(
                    target: "kian::hook",
                    plugin = %inner.name,
                    kind = "stderr_line",
                    scanned = scanned,
                    max = max,
                    "stderr line over bound — dropped"
                );
                let now = Instant::now();
                if last_drop.is_none_or(|at| now.duration_since(at) >= Duration::from_secs(1)) {
                    // Stamp only when counting: a drop that does not
                    // count must not push the window forward.
                    last_drop = Some(now);
                    if let Some(reason) = record_window_failure(&inner, FailureKind::StderrRate) {
                        quarantine(&inner, reason);
                        return;
                    }
                }
                if !reader.discard_line() {
                    return;
                }
            }
            Err(LineError::Io(_)) => return, // EOF: the child is gone
        }
    }
}

fn set_equal(a: &[String], b: &[String]) -> bool {
    let mut left: Vec<&String> = a.iter().collect();
    let mut right: Vec<&String> = b.iter().collect();
    left.sort();
    right.sort();
    left == right
}

fn digest_of(path: &str) -> Option<String> {
    let mut file = std::fs::File::open(path).ok()?;
    let mut buf = Vec::new();
    file.read_to_end(&mut buf).ok()?;
    Some(sha256_hex(&buf))
}

// ---------------------------------------------------------------------
// Quarantine, kills, stops
// ---------------------------------------------------------------------

impl Inner {
    fn quarantine_locked(&self) -> Option<QuarantineReason> {
        self.quarantine
            .lock()
            .expect("kian-plugin: quarantine lock poisoned")
            .clone()
    }
}

/// Quarantines the instance (§6.5): one-way for the process lifetime,
/// one `hook-quarantine` audit line with name, reason, failure count
/// and window. There is no un-quarantine surface in v1 (D3).
fn quarantine(inner: &Arc<Inner>, reason: QuarantineReason) {
    let mut slot = inner
        .quarantine
        .lock()
        .expect("kian-plugin: quarantine lock poisoned");
    if slot.is_some() {
        return; // the first reason stands
    }
    let failures = inner.counters.window_len();
    let window_ms = inner.manifest.limits.restart_window_ms;
    tracing::info!(
        target: "kian::hook",
        plugin = %inner.name,
        reason = %reason.as_audit_str(),
        failures = failures,
        window_ms = window_ms,
        lifetime = "server-process",
        "hook-quarantine"
    );
    *slot = Some(reason);
}

impl QuarantineReason {
    /// The audit-line spelling.
    pub fn as_audit_str(&self) -> String {
        match self {
            QuarantineReason::RestartsExhausted {
                failures,
                window_ms,
            } => {
                format!("restarts_exhausted(failures={failures}, window_ms={window_ms})")
            }
            QuarantineReason::StderrRate {
                failures,
                window_ms,
            } => {
                format!("stderr_overflow(failures={failures}, window_ms={window_ms})")
            }
            QuarantineReason::ProtocolViolation { detail } => {
                format!("protocol_violation({detail})")
            }
            QuarantineReason::FrameOverflow { direction } => {
                format!("frame_overflow({direction})")
            }
            QuarantineReason::DigestMismatch => "digest_mismatch(respawn)".to_string(),
        }
    }
}

/// Records a window-counted failure and quarantines when the window
/// trips (§6.4).
fn record_window_failure(inner: &Arc<Inner>, kind: FailureKind) -> Option<QuarantineReason> {
    inner.counters.record_failure(kind, &inner.manifest.limits)
}

fn kill_session(session: &mut Session) {
    session.alive = false;
    kill_group(session.pid);
    let _ = session.child.kill();
    reap_bounded(&mut session.child);
}

fn kill_child(child: &mut Child) {
    let pid = child.id();
    kill_group(pid);
    let _ = child.kill();
    reap_bounded(child);
}

/// Kills the child's whole process group (§10.3 step 5). Without a
/// `libc` edge (K10) the group signal goes through the absolute-path
/// `/bin/kill` — a direct binary, never a shell; the leader itself is
/// always killed through std afterwards regardless.
fn kill_group(pid: u32) {
    if pid == 0 {
        return;
    }
    #[cfg(unix)]
    {
        let target = format!("-{pid}");
        for killer in ["/bin/kill", "/usr/bin/kill"] {
            if Command::new(killer)
                .arg("-9")
                .arg(&target)
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status()
                .is_ok()
            {
                return;
            }
        }
    }
    #[cfg(not(unix))]
    {
        let _ = pid;
    }
}

fn reap_bounded(child: &mut Child) {
    let deadline = Instant::now() + REAP_BOUND;
    while Instant::now() < deadline {
        match child.try_wait() {
            Ok(Some(_)) | Err(_) => return,
            Ok(None) => std::thread::sleep(Duration::from_millis(10)),
        }
    }
    tracing::warn!(
        target: "kian::hook",
        pid = child.id(),
        "sidecar child not reaped within the bounded grace"
    );
}

/// §7.5's graceful stop: `bye`, half-close stdin, bounded grace, then
/// kill the whole group. EOF without the expected reply was already
/// handled as a crash during dispatch; this is the orderly path.
fn graceful_stop(session: &mut Session) {
    if let Some(mut stdin) = session.stdin.take() {
        let _ = write_line(&mut stdin, &encode_bye(), 64);
    }
    // The take above already half-closed stdin when there was one.
    let deadline = Instant::now() + STOP_GRACE;
    while Instant::now() < deadline {
        match session.child.try_wait() {
            Ok(Some(_)) => return,
            Ok(None) => std::thread::sleep(Duration::from_millis(20)),
            Err(_) => return,
        }
    }
    kill_session(session);
}

fn boot_refusal(inner: &Arc<Inner>, failure: HandshakeFailure) -> Refusal {
    let required = inner.manifest.required();
    let refusal = |code, message| Refusal {
        code,
        message,
        required,
    };
    match failure {
        HandshakeFailure::DigestMismatch => refusal(
            RefusalCode::DigestMismatch,
            format!(
                "`{}`: argv[0] digest does not match the manifest sha256 at spawn",
                inner.name
            ),
        ),
        HandshakeFailure::Spawn(detail) => refusal(
            RefusalCode::SpawnFailed,
            format!("`{}`: {}", inner.name, detail),
        ),
        HandshakeFailure::Timeout => refusal(
            RefusalCode::SpawnFailed,
            format!(
                "`{}`: hello→ready exceeded startup_timeout_ms (rule 13)",
                inner.name
            ),
        ),
        HandshakeFailure::ReadyMismatch(detail) => refusal(
            RefusalCode::ReadyMismatch,
            format!("`{}`: {detail}", inner.name),
        ),
        HandshakeFailure::AbiMismatch => refusal(
            RefusalCode::AbiMismatch,
            format!(
                "`{}`: ready restates a different abi than the manifest",
                inner.name
            ),
        ),
        HandshakeFailure::Crashed(detail) => refusal(
            RefusalCode::SpawnFailed,
            format!("`{}`: {detail}", inner.name),
        ),
    }
}

/// §10.6's `hook-refuse` audit line.
fn audit_refuse(refusal: &Refusal) {
    tracing::info!(
        target: "kian::hook",
        code = refusal.code.as_str(),
        message = %refusal.message,
        required = match refusal.required {
            Required::True => "true",
            Required::False => "false",
            Required::Unknown => "unknown",
        },
        "hook-refuse"
    );
}

/// Serializes a payload view for the wire (§7.2's `payload` is the
/// §3.2 view; the in-process `frame` identity is skipped by serde and
/// rides the frame envelope instead).
fn payload_value(payload: &HookPayload) -> serde_json::Value {
    use HookPayload as P;
    let result = match payload {
        P::AdmitDraft(v) => serde_json::to_value(v),
        P::AdmitReceipt(v) => serde_json::to_value(v),
        P::TurnRequest(v) => serde_json::to_value(v),
        P::TurnResponse(v) => serde_json::to_value(v),
        P::ToolPre(v) => serde_json::to_value(v),
        P::ToolPost(v) => serde_json::to_value(v),
        P::Recovery(v) => serde_json::to_value(v),
        P::Settle(v) => serde_json::to_value(v),
    };
    result.unwrap_or(serde_json::Value::Null)
}

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

    fn limits() -> LimitsSection {
        LimitsSection {
            deadline_ms: 500,
            max_payload_kib: 256,
            restarts_per_window: 3,
            restart_window_ms: 60_000,
            max_settle_vetoes: 0,
            max_stderr_line_kib: 8,
            stderr_rate_kib_per_min: 1024,
        }
    }

    #[test]
    fn window_trips_at_the_threshold_and_tags_the_reason() {
        let counters = Counters::default();
        let limits = limits();
        for _ in 0..2 {
            assert!(
                counters
                    .record_failure(FailureKind::Crash, &limits)
                    .is_none()
            );
        }
        let reason = counters
            .record_failure(FailureKind::Deadline, &limits)
            .expect("third failure trips the window");
        assert_eq!(
            reason,
            QuarantineReason::RestartsExhausted {
                failures: 3,
                window_ms: 60_000
            }
        );
        // A success clears the window — the failures are consecutive.
        counters.record_success();
        for _ in 0..2 {
            assert!(
                counters
                    .record_failure(FailureKind::Crash, &limits)
                    .is_none()
            );
        }
    }

    #[test]
    fn stderr_only_window_names_stderr_overflow() {
        let counters = Counters::default();
        let limits = limits();
        for _ in 0..2 {
            assert!(
                counters
                    .record_failure(FailureKind::StderrRate, &limits)
                    .is_none()
            );
        }
        let reason = counters
            .record_failure(FailureKind::StderrRate, &limits)
            .expect("stderr-only window trips");
        assert!(matches!(
            reason,
            QuarantineReason::StderrRate { failures: 3, .. }
        ));
        // A mixed window names restarts_exhausted, not stderr.
        let counters = Counters::default();
        counters.record_failure(FailureKind::StderrRate, &limits);
        for _ in 0..2 {
            counters.record_failure(FailureKind::Crash, &limits);
        }
        // (3 total with at least one crash entry)
    }

    #[test]
    fn restarts_are_counted_not_thresholded() {
        let counters = Counters::default();
        assert_eq!(counters.record_restart(), 1);
        assert_eq!(counters.record_restart(), 2);
        assert_eq!(counters.restarts(), 2);
        assert_eq!(counters.window_len(), 0, "restarts never touch the window");
    }
}