kian-plugin 0.1.0

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
//! Failure classification and class defaults (PLUGIN-SPEC §11.1, §5.2).
//!
//! Two failure classes, disjoint by construction:
//!
//! - **Unreachability** — the handler cannot answer (deadline, crash,
//!   quarantine, busy, handshake incomplete, EOF without a reply, or a
//!   self-declared `err:deadline`/`err:crashed`). Resolves to the
//!   **grant-keyed class default**.
//! - **Speech defect** — the handler answered wrongly (out-of-grant,
//!   wrong-mode, unparseable, or an `err` whose code is not a
//!   self-declared unreachability). The reply is **discarded as an
//!   opinion**: the outcome is exactly the as-if-absent one, never the
//!   class default.
//!
//! [`resolve_outcome`] is the *only* place a reply becomes a verdict
//! (K6): the bus and both tiers test through it, so the escalation bug of
//! §5.2 (a draft resolving a defect to the fail-closed default) cannot
//! reappear in a second code path.

use super::grants::{Grant, GrantSet, ReplyShape, veto_grant_for_class};
use super::ids::HookId;
use super::registry::{HookReply, ReplyErrCode};
use super::views::Origin;

/// Why a handler could not answer (§11.1's unreachability members).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Cause {
    Deadline,
    Crash,
    Quarantine,
    Busy,
    HandshakeIncomplete,
    EofWithoutReply,
}

impl Cause {
    pub fn as_str(self) -> &'static str {
        match self {
            Cause::Deadline => "deadline",
            Cause::Crash => "crash",
            Cause::Quarantine => "quarantine",
            Cause::Busy => "busy",
            Cause::HandshakeIncomplete => "handshake_incomplete",
            Cause::EofWithoutReply => "eof_without_reply",
        }
    }
}

/// Which speech defect a discarded reply committed. The first five are
/// the ABI's non-unreachability `err` codes (§7.2); `Unparseable` arises
/// only at the wire (the tier-0 carrier is typed, so it cannot occur
/// in-process); `OriginObserveOnly` classifies the one in-process defect
/// the wire never sees — a stop answered at an observe-only admission
/// origin (§3.4.3).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpeechCode {
    Unauthorized,
    Malformed,
    UnsupportedHook,
    ProtocolViolation,
    Unparseable,
    OriginObserveOnly,
}

impl SpeechCode {
    pub fn as_str(self) -> &'static str {
        match self {
            SpeechCode::Unauthorized => "unauthorized",
            SpeechCode::Malformed => "malformed",
            SpeechCode::UnsupportedHook => "unsupported_hook",
            SpeechCode::ProtocolViolation => "protocol_violation",
            SpeechCode::Unparseable => "unparseable",
            SpeechCode::OriginObserveOnly => "origin_observe_only",
        }
    }
}

/// The two failure classes (§11.1).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FailureClass {
    Unreachable(Cause),
    Speech(SpeechCode),
}

/// The class default an unreachability resolves to — **a property of a
/// `veto:*` grant, not of a hook id** (§11.1):
///
/// - `Deny` — `veto:tool` holder at `tool/pre`: fail closed.
/// - `RefuseAdmission` — `veto:admit` holder at a channel-borne
///   `admit/pre`: fail closed for every target.
/// - `Abstain` — `veto:settle` holder at `settle/pre`: fail open; a
///   wedged quality gate must not hold a session hostage.
/// - `Lapse` — an observe-only subscriber anywhere: the observation is
///   lost and nothing else happens.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClassDefault {
    Deny { cause: Cause },
    RefuseAdmission { cause: Cause },
    Abstain,
    Lapse,
}

/// What one subscriber's reply resolves to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
    /// An observation (ack / next / abstain), or a defect whose reply was
    /// discarded: this subscriber contributes no verdict, exactly as if
    /// absent (§5.2).
    AsIfAbsent,
    /// A valid veto reply — the subscriber holds the grant and answered
    /// in the hook's mode.
    Denied { reason: String },
    /// Unreachability resolved to the grant-keyed default (§11.1).
    Default(ClassDefault),
    /// A speech defect: discarded, but recorded for the `hook-discard`
    /// audit line (§10.6) — audited as a defection, never as a lapse.
    Discarded {
        code: SpeechCode,
        grant: Option<Grant>,
    },
}

/// The one place a reply becomes a verdict (K6).
///
/// `hook` fixes the mode (wrong-mode check) and the class (grant-keyed
/// default); `origin` carries §11.1's per-origin split for the admission
/// port — the only veto-capable one is `channel`.
pub fn resolve_outcome(
    reply: &HookReply,
    grants: &GrantSet,
    hook: &HookId,
    origin: Origin,
) -> Outcome {
    // Self-declared unreachability first (§11.1): an honest plugin that
    // knows it cannot decide must not be mistaken for one speaking out of
    // turn.
    if let HookReply::Err { code, .. } = reply {
        return match code {
            ReplyErrCode::Deadline => {
                Outcome::Default(grant_keyed_default(grants, hook, origin, Cause::Deadline))
            }
            ReplyErrCode::Crashed => {
                Outcome::Default(grant_keyed_default(grants, hook, origin, Cause::Crash))
            }
            ReplyErrCode::Malformed => Outcome::Discarded {
                code: SpeechCode::Malformed,
                grant: None,
            },
            ReplyErrCode::Unauthorized => Outcome::Discarded {
                code: SpeechCode::Unauthorized,
                grant: None,
            },
            ReplyErrCode::UnsupportedHook => Outcome::Discarded {
                code: SpeechCode::UnsupportedHook,
                grant: None,
            },
            ReplyErrCode::ProtocolViolation => Outcome::Discarded {
                code: SpeechCode::ProtocolViolation,
                grant: None,
            },
        };
    }

    let shape = reply_shape(reply);
    let mode = hook.mode();
    if !shape.legal_at(mode) {
        // Wrong-mode reply: `err:malformed`-shaped speech defect (§5.2).
        return Outcome::Discarded {
            code: SpeechCode::Malformed,
            grant: None,
        };
    }
    match shape {
        ReplyShape::Ack | ReplyShape::FlowNext | ReplyShape::BailNone => Outcome::AsIfAbsent,
        ReplyShape::FlowStop => {
            // Only `admit/pre` is a flow hook (the mode check above has
            // already refused a stop anywhere else).
            match hook {
                HookId::AdmitPre if origin != Origin::Channel => {
                    // §3.4.3: the API-borne port is observe-only in v1 —
                    // there is no verdict to fail closed and no wire to
                    // express a refusal; the stop is discarded.
                    Outcome::Discarded {
                        code: SpeechCode::OriginObserveOnly,
                        grant: Some(Grant::VetoAdmit),
                    }
                }
                HookId::AdmitPre if !grants.contains(Grant::VetoAdmit) => Outcome::Discarded {
                    code: SpeechCode::Unauthorized,
                    grant: Some(Grant::VetoAdmit),
                },
                HookId::AdmitPre => Outcome::Denied {
                    reason: match reply {
                        HookReply::FlowStop { reason } => reason.clone(),
                        _ => unreachable!("shape checked above"),
                    },
                },
                _ => Outcome::Discarded {
                    code: SpeechCode::Malformed,
                    grant: None,
                },
            }
        }
        ReplyShape::BailDeny => {
            let required = veto_grant_for_class(hook.class());
            match required {
                Some(grant) if grants.contains(grant) => Outcome::Denied {
                    reason: match reply {
                        HookReply::BailDeny { reason } => reason.clone(),
                        _ => unreachable!("shape checked above"),
                    },
                },
                Some(grant) => Outcome::Discarded {
                    code: SpeechCode::Unauthorized,
                    grant: Some(grant),
                },
                // A deny at a hook with no veto-capable class: malformed.
                None => Outcome::Discarded {
                    code: SpeechCode::Malformed,
                    grant: None,
                },
            }
        }
    }
}

fn reply_shape(reply: &HookReply) -> ReplyShape {
    match reply {
        HookReply::Ack { .. } => ReplyShape::Ack,
        HookReply::FlowNext => ReplyShape::FlowNext,
        HookReply::FlowStop { .. } => ReplyShape::FlowStop,
        HookReply::BailNone => ReplyShape::BailNone,
        HookReply::BailDeny { .. } => ReplyShape::BailDeny,
        HookReply::Err { .. } => unreachable!("err handled before shaping"),
    }
}

/// The grant-keyed unreachability default (§11.1's table).
/// `pub(crate)`: the bus's session-quarantine path (§9.3) synthesizes
/// the default directly — a quarantined sidecar is unreachable, and
/// the cause must read `quarantine`, not `crashed`.
pub(crate) fn grant_keyed_default(
    grants: &GrantSet,
    hook: &HookId,
    origin: Origin,
    cause: Cause,
) -> ClassDefault {
    match hook {
        HookId::ToolPre if grants.contains(Grant::VetoTool) => ClassDefault::Deny { cause },
        HookId::AdmitPre if origin == Origin::Channel && grants.contains(Grant::VetoAdmit) => {
            ClassDefault::RefuseAdmission { cause }
        }
        HookId::SettlePre if grants.contains(Grant::VetoSettle) => ClassDefault::Abstain,
        // An observe-only subscriber — at any hook, including a
        // veto-capable one, and at an observe-only port — lapses (§11.1
        // row 4): an observer has no decision to lose.
        _ => ClassDefault::Lapse,
    }
}

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

    fn grants(names: &[&str]) -> GrantSet {
        GrantSet::from_names(names).unwrap()
    }

    #[test]
    fn class_default_is_keyed_by_grant() {
        let tool_pre = HookId::parse("tool/pre").unwrap();
        let admit_pre = HookId::parse("admit/pre").unwrap();
        let settle_pre = HookId::parse("settle/pre").unwrap();

        let unreachable = HookReply::Err {
            code: ReplyErrCode::Crashed,
            message: "gone".to_string(),
        };

        // Row 1: `veto:tool` at `tool/pre` fails CLOSED.
        assert_eq!(
            resolve_outcome(
                &unreachable,
                &grants(&["observe:tool", "veto:tool"]),
                &tool_pre,
                Origin::Api
            ),
            Outcome::Default(ClassDefault::Deny {
                cause: Cause::Crash
            })
        );
        // Row 2: `veto:admit` at a channel-borne `admit/pre` fails CLOSED.
        assert_eq!(
            resolve_outcome(
                &unreachable,
                &grants(&["observe:admit", "veto:admit"]),
                &admit_pre,
                Origin::Channel
            ),
            Outcome::Default(ClassDefault::RefuseAdmission {
                cause: Cause::Crash
            })
        );
        // The same holder at an API-borne origin lapses: the port is
        // observe-only there, so there is no verdict to fail closed.
        assert_eq!(
            resolve_outcome(
                &unreachable,
                &grants(&["observe:admit", "veto:admit"]),
                &admit_pre,
                Origin::Api
            ),
            Outcome::Default(ClassDefault::Lapse)
        );
        // Row 3: `veto:settle` at `settle/pre` fails OPEN.
        assert_eq!(
            resolve_outcome(
                &unreachable,
                &grants(&["observe:settle", "veto:settle"]),
                &settle_pre,
                Origin::Api
            ),
            Outcome::Default(ClassDefault::Abstain)
        );
        // Row 4: observe-only lapses — even at a veto-capable hook.
        assert_eq!(
            resolve_outcome(
                &unreachable,
                &grants(&["observe:tool"]),
                &tool_pre,
                Origin::Api
            ),
            Outcome::Default(ClassDefault::Lapse)
        );
        // Deadline is the same default with a different cause.
        let overdue = HookReply::Err {
            code: ReplyErrCode::Deadline,
            message: "slow".to_string(),
        };
        assert_eq!(
            resolve_outcome(
                &overdue,
                &grants(&["observe:tool", "veto:tool"]),
                &tool_pre,
                Origin::Api
            ),
            Outcome::Default(ClassDefault::Deny {
                cause: Cause::Deadline
            })
        );
    }

    #[test]
    fn unreachable_veto_denies_at_any_order_position() {
        // §11.1 consequence 1: "not reachable" cannot be read as
        // "abstained". `resolve_outcome` is position-free — the ordering
        // guarantee is exercised end-to-end in the bus tests, where the
        // same reply placed *after* an abstaining subscriber still denies.
        let tool_pre = HookId::parse("tool/pre").unwrap();
        let abstain = HookReply::BailNone;
        let unreachable = HookReply::Err {
            code: ReplyErrCode::Deadline,
            message: "over deadline".to_string(),
        };
        let veto_holder = grants(&["observe:tool", "veto:tool"]);

        assert_eq!(
            resolve_outcome(&abstain, &veto_holder, &tool_pre, Origin::Api),
            Outcome::AsIfAbsent
        );
        assert_eq!(
            resolve_outcome(&unreachable, &veto_holder, &tool_pre, Origin::Api),
            Outcome::Default(ClassDefault::Deny {
                cause: Cause::Deadline
            })
        );
    }

    #[test]
    fn speech_defects_are_discarded_never_defaulted() {
        // §5.2: an out-of-grant or wrong-mode reply is not the class
        // default — the outcome is exactly as-if-absent.
        let tool_pre = HookId::parse("tool/pre").unwrap();
        let observe_only = grants(&["observe:tool"]);

        // Out-of-grant deny from an observe-only subscriber.
        assert_eq!(
            resolve_outcome(
                &HookReply::BailDeny {
                    reason: "no".to_string()
                },
                &observe_only,
                &tool_pre,
                Origin::Api
            ),
            Outcome::Discarded {
                code: SpeechCode::Unauthorized,
                grant: Some(Grant::VetoTool)
            }
        );
        // Wrong-mode: an `ack` at a bail hook.
        assert_eq!(
            resolve_outcome(
                &HookReply::Ack { note: None },
                &observe_only,
                &tool_pre,
                Origin::Api
            ),
            Outcome::Discarded {
                code: SpeechCode::Malformed,
                grant: None
            }
        );
        // A flow stop at a bail hook is wrong-mode too.
        assert_eq!(
            resolve_outcome(
                &HookReply::FlowStop {
                    reason: "no".to_string()
                },
                &observe_only,
                &tool_pre,
                Origin::Api
            ),
            Outcome::Discarded {
                code: SpeechCode::Malformed,
                grant: None
            }
        );
        // A stop at an API-borne admission is discarded (observe-only
        // origin), even with the grant held.
        let admit_pre = HookId::parse("admit/pre").unwrap();
        assert_eq!(
            resolve_outcome(
                &HookReply::FlowStop {
                    reason: "no".to_string()
                },
                &grants(&["observe:admit", "veto:admit"]),
                &admit_pre,
                Origin::Api
            ),
            Outcome::Discarded {
                code: SpeechCode::OriginObserveOnly,
                grant: Some(Grant::VetoAdmit)
            }
        );
        // A valid stop at a channel-borne admission with the grant is a
        // verdict, not a defect.
        assert_eq!(
            resolve_outcome(
                &HookReply::FlowStop {
                    reason: "off-hours".to_string()
                },
                &grants(&["observe:admit", "veto:admit"]),
                &admit_pre,
                Origin::Channel
            ),
            Outcome::Denied {
                reason: "off-hours".to_string()
            }
        );
        // Valid deny and abstain at bail hooks with the grant held.
        assert_eq!(
            resolve_outcome(
                &HookReply::BailDeny {
                    reason: "blocked".to_string()
                },
                &grants(&["observe:tool", "veto:tool"]),
                &tool_pre,
                Origin::Api
            ),
            Outcome::Denied {
                reason: "blocked".to_string()
            }
        );
        assert_eq!(
            resolve_outcome(
                &HookReply::BailNone,
                &grants(&["observe:settle", "veto:settle"]),
                &HookId::parse("settle/pre").unwrap(),
                Origin::Api
            ),
            Outcome::AsIfAbsent
        );
    }
}