eidetic-engine 0.15.1

Durable, local-first, explainable memory for coding agents.
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
//! SRR6.46.8 — Identity-change guard for cross-tailnet / cross-machine refusal.
//!
//! When a workspace has a materialized auto-enrollment peer-group bound to
//! a specific tailscale identity, this module detects whether the current
//! `tailscaled` posture still matches that identity in two dimensions:
//!
//! 1. **Tailnet change**: bound `tailnetId` differs from current
//!    `tailnetId` (user logged out of tailnet A and into tailnet B on the
//!    same machine).
//! 2. **Node-key change**: bound `materializedOnNodeKey` differs from
//!    current `selfNodeKey` (db restored from a backup taken on machine
//!    A onto machine B; or tailscale was reinstalled and reissued a node
//!    key).
//!
//! Either detection produces a `medium`-severity refusal classification
//! that SRR6.46.3 auto-enrollment consumes to abort cleanly without
//! touching the peer-group table, and that SRR6.46.4 status view
//! surfaces as `drift.tailnetChanged` / `drift.nodeKeyChanged` with a
//! literal `ee mesh disable && ee mesh auto-enroll` repair command.
//!
//! Tailnet DISPLAY-NAME drift (admin renamed the tailnet but the
//! stable id is unchanged) is informational, NOT a refusal: a
//! `warning`-severity classification with a rename-notice message.
//!
//! This module is pure decision logic — no I/O, no DB writes. Callers
//! provide the bound + current values; the module returns a verdict
//! enum. The actual probe (SRR6.46.1) and the peer-group bound-state
//! reader (SRR6.30) live in their own modules.

use serde::{Deserialize, Serialize};

/// Degraded code emitted on tailnet-id mismatch. Severity: `medium`.
/// SRR6.46.3 auto-enrollment refuses to materialize when this fires.
/// Symmetric on the SRR6.46.4 status surface and the SRR6.46.14
/// steward drift reconciliation.
///
/// bd-3e6fq: re-exported from `super::auto_enrollment` (the canonical
/// declaration site that matches the const's `AUTO_ENROLLMENT_*`
/// prefix). Two byte-identical `pub const` declarations previously
/// existed here and in `auto_enrollment.rs`; consolidating to a
/// single source-of-truth eliminates a silent drift hazard (a future
/// rename or value tweak on one site that the other doesn't pick up
/// would have looked like one constant emitting two different code
/// strings).
pub use super::auto_enrollment::AUTO_ENROLLMENT_TAILNET_CHANGED_CODE;

/// Degraded code emitted on node-key mismatch (backup-restored-to-different-
/// machine class). Severity: `medium`. Same caller semantics as the tailnet
/// code — SRR6.46.3 refuses; SRR6.46.4 surfaces; SRR6.46.14 honors.
pub const AUTO_ENROLLMENT_NODE_KEY_CHANGED_CODE: &str = "auto_enrollment_node_key_changed";

/// Degraded code emitted by the SRR6.46.12 hello-responder daemon job
/// at bind time when the materialized peer-group's
/// `materializedOnNodeKey` does not match the current `selfNodeKey`.
/// Severity: `high`. The daemon refuses to bind, preventing peers from
/// reaching a stale-identity responder.
pub const HELLO_RESPONDER_NODE_KEY_MISMATCH_CODE: &str = "hello_responder_node_key_mismatch";

/// Workspace-config opt-in for surfacing the identity-change check on
/// every `ee status --json` (default off; v4 design decision per
/// ADR 0038 / `bd-36bbk.1.8`).
pub const EE_MESH_CHANGE_GUARD_CHECK_ON_STATUS_ENV: &str = "EE_MESH_CHANGE_GUARD_CHECK_ON_STATUS";

/// The bound identity that a materialized auto-enrollment peer-group
/// captured at materialization time. All fields are owned strings so
/// the caller can construct this from a DB row read without lifetime
/// gymnastics.
///
/// When the materialized peer-group is absent (clean state), pass
/// `None` to the verdict functions — the guard returns a no-change
/// verdict and SRR6.46.3 proceeds.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct BoundIdentity {
    pub tailnet_id: String,
    pub tailnet_display_name: Option<String>,
    pub materialized_on_node_key: String,
}

/// The current identity reported by SRR6.46.1's local probe. Same
/// owned-string contract.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct CurrentIdentity {
    pub tailnet_id: String,
    pub tailnet_display_name: Option<String>,
    pub self_node_key: String,
}

/// The verdict the guard returns. Variants are sorted by severity in
/// the documented evaluation order (tailnet > node-key > rename >
/// no_change), so a caller switching on this enum can handle the most
/// severe class first and fall through to less-severe.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum IdentityGuardVerdict {
    /// Bound `tailnetId` differs from current. Refuse.
    TailnetChanged {
        bound_tailnet_id: String,
        bound_tailnet_display_name: Option<String>,
        current_tailnet_id: String,
        current_tailnet_display_name: Option<String>,
    },
    /// Bound `materializedOnNodeKey` differs from current `selfNodeKey`.
    /// Refuse. (The bound `tailnetId` matches at this point.)
    NodeKeyChanged {
        bound_node_key: String,
        current_node_key: String,
    },
    /// Tailnet id matches; display name changed (admin renamed the
    /// tailnet). Informational; auto-enrollment proceeds and status
    /// view surfaces with severity `warning`.
    TailnetRenamed {
        tailnet_id: String,
        bound_display_name: Option<String>,
        current_display_name: Option<String>,
    },
    /// All checks pass. Auto-enrollment proceeds normally.
    NoChange,
    /// No materialized peer-group exists yet; the guard has nothing to
    /// check. Auto-enrollment proceeds normally.
    NoBoundIdentity,
}

impl IdentityGuardVerdict {
    /// Whether SRR6.46.3 should REFUSE auto-enrollment based on this
    /// verdict. Only `TailnetChanged` and `NodeKeyChanged` are refusal
    /// classes; rename + no-change + no-bound-identity all proceed.
    #[must_use]
    pub fn refuses_auto_enrollment(&self) -> bool {
        matches!(
            self,
            Self::TailnetChanged { .. } | Self::NodeKeyChanged { .. }
        )
    }

    /// Maps the verdict to the canonical degraded code, when applicable.
    /// Returns `None` for non-refusal verdicts.
    #[must_use]
    pub fn refusal_code(&self) -> Option<&'static str> {
        match self {
            Self::TailnetChanged { .. } => Some(AUTO_ENROLLMENT_TAILNET_CHANGED_CODE),
            Self::NodeKeyChanged { .. } => Some(AUTO_ENROLLMENT_NODE_KEY_CHANGED_CODE),
            _ => None,
        }
    }

    /// Build the canonical repair command string for the verdict. The
    /// command is the literal copy-paste an operator can run to clear
    /// the refusal — `ee mesh disable && ee mesh auto-enroll`, with
    /// the `--workspace` flag substituted and a `--reason` annotation
    /// for the node-key case so the disable audit row records the
    /// trigger.
    ///
    /// Returns `None` for non-refusal verdicts.
    #[must_use]
    pub fn repair_command(&self, workspace_path: &str) -> Option<String> {
        let workspace_arg = shell_quote_command_arg(workspace_path);
        let reason_arg = shell_quote_command_arg("restored from different machine");
        match self {
            Self::TailnetChanged { .. } => Some(format!(
                "ee mesh disable --workspace {workspace_arg} && ee mesh auto-enroll --workspace {workspace_arg}"
            )),
            Self::NodeKeyChanged { .. } => Some(format!(
                "ee mesh disable --workspace {workspace_arg} --reason {reason_arg} && ee mesh auto-enroll --workspace {workspace_arg}"
            )),
            _ => None,
        }
    }

    /// Stable string tag for log/audit emission. Matches the serde
    /// `kind` field's value for each variant.
    #[must_use]
    pub fn kind_str(&self) -> &'static str {
        match self {
            Self::TailnetChanged { .. } => "tailnet_changed",
            Self::NodeKeyChanged { .. } => "node_key_changed",
            Self::TailnetRenamed { .. } => "tailnet_renamed",
            Self::NoChange => "no_change",
            Self::NoBoundIdentity => "no_bound_identity",
        }
    }
}

fn shell_quote_command_arg(value: &str) -> String {
    let mut quoted = String::with_capacity(value.len() + 2);
    quoted.push('"');
    for ch in value.chars() {
        if matches!(ch, '"' | '$' | '`' | '\\') {
            quoted.push('\\');
        }
        quoted.push(ch);
    }
    quoted.push('"');
    quoted
}

/// Evaluate the identity guard.
///
/// Evaluation order (first-match wins):
///
/// 1. `bound` is `None` → [`NoBoundIdentity`]. Clean state, proceed.
/// 2. `bound.tailnet_id != current.tailnet_id` → [`TailnetChanged`].
///    Refuse.
/// 3. `bound.materialized_on_node_key != current.self_node_key` →
///    [`NodeKeyChanged`]. Refuse.
/// 4. `bound.tailnet_display_name != current.tailnet_display_name`
///    (with both non-None) → [`TailnetRenamed`]. Informational; proceed.
/// 5. Otherwise → [`NoChange`]. Proceed normally.
///
/// Note: tailnet rename is only detected when BOTH display names are
/// present and differ. A transition from `Some(x)` to `None` (or vice
/// versa) is silently treated as no-change because absent display names
/// are an artifact of the probe's optional field handling — not a
/// meaningful identity change.
#[must_use]
pub fn evaluate_identity_guard(
    bound: Option<&BoundIdentity>,
    current: &CurrentIdentity,
) -> IdentityGuardVerdict {
    let Some(bound) = bound else {
        return IdentityGuardVerdict::NoBoundIdentity;
    };
    if bound.tailnet_id != current.tailnet_id {
        return IdentityGuardVerdict::TailnetChanged {
            bound_tailnet_id: bound.tailnet_id.clone(),
            bound_tailnet_display_name: bound.tailnet_display_name.clone(),
            current_tailnet_id: current.tailnet_id.clone(),
            current_tailnet_display_name: current.tailnet_display_name.clone(),
        };
    }
    if bound.materialized_on_node_key != current.self_node_key {
        return IdentityGuardVerdict::NodeKeyChanged {
            bound_node_key: bound.materialized_on_node_key.clone(),
            current_node_key: current.self_node_key.clone(),
        };
    }
    match (
        bound.tailnet_display_name.as_deref(),
        current.tailnet_display_name.as_deref(),
    ) {
        (Some(bound_name), Some(current_name)) if bound_name != current_name => {
            IdentityGuardVerdict::TailnetRenamed {
                tailnet_id: bound.tailnet_id.clone(),
                bound_display_name: Some(bound_name.to_owned()),
                current_display_name: Some(current_name.to_owned()),
            }
        }
        _ => IdentityGuardVerdict::NoChange,
    }
}

/// Convenience verdict for the SRR6.46.12 hello responder's bind-time
/// check: a strictly node-key-focused variant of the verdict tree that
/// ignores tailnet + display-name. The responder cares only about
/// "am I the same machine that materialized this binding?" because a
/// peer reaching us under a stale identity is the safety concern.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ResponderBindVerdict {
    /// Bind safely. Bound nodeKey matches current self.
    Bind,
    /// Refuse to bind. Bound nodeKey differs from current.
    RefuseNodeKeyMismatch {
        bound_node_key: String,
        current_node_key: String,
    },
    /// No bound peer-group exists. Bind safely (nothing to protect).
    BindNoBoundIdentity,
}

impl ResponderBindVerdict {
    #[must_use]
    pub fn refuses_bind(&self) -> bool {
        matches!(self, Self::RefuseNodeKeyMismatch { .. })
    }

    /// The literal degraded code SRR6.46.12 emits on refusal.
    #[must_use]
    pub fn refusal_code(&self) -> Option<&'static str> {
        match self {
            Self::RefuseNodeKeyMismatch { .. } => Some(HELLO_RESPONDER_NODE_KEY_MISMATCH_CODE),
            _ => None,
        }
    }
}

/// Hello responder bind-time check. Pure-read; SRR6.46.12 consumes
/// the verdict to decide whether to register the supervised job.
#[must_use]
pub fn evaluate_responder_bind(
    bound: Option<&BoundIdentity>,
    current_self_node_key: &str,
) -> ResponderBindVerdict {
    let Some(bound) = bound else {
        return ResponderBindVerdict::BindNoBoundIdentity;
    };
    if bound.materialized_on_node_key == current_self_node_key {
        ResponderBindVerdict::Bind
    } else {
        ResponderBindVerdict::RefuseNodeKeyMismatch {
            bound_node_key: bound.materialized_on_node_key.clone(),
            current_node_key: current_self_node_key.to_owned(),
        }
    }
}

// ============================================================================
// Inline tests (AGENTS.md L300-302 / bd-3usjw.62 Rule 7)
// ============================================================================

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

    fn bound(tailnet: &str, display: Option<&str>, node: &str) -> BoundIdentity {
        BoundIdentity {
            tailnet_id: tailnet.to_owned(),
            tailnet_display_name: display.map(str::to_owned),
            materialized_on_node_key: node.to_owned(),
        }
    }

    fn current(tailnet: &str, display: Option<&str>, node: &str) -> CurrentIdentity {
        CurrentIdentity {
            tailnet_id: tailnet.to_owned(),
            tailnet_display_name: display.map(str::to_owned),
            self_node_key: node.to_owned(),
        }
    }

    // ---- evaluate_identity_guard -------------------------------------------

    #[test]
    fn guard_no_bound_identity_when_bound_is_none() {
        let verdict = evaluate_identity_guard(None, &current("tn_a", None, "nk_self"));
        assert_eq!(verdict, IdentityGuardVerdict::NoBoundIdentity);
        assert!(!verdict.refuses_auto_enrollment());
        assert_eq!(verdict.kind_str(), "no_bound_identity");
    }

    #[test]
    fn guard_no_change_when_both_identities_match_exactly() {
        let b = bound("tn_a", Some("team-a"), "nk_self");
        let c = current("tn_a", Some("team-a"), "nk_self");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        assert_eq!(verdict, IdentityGuardVerdict::NoChange);
        assert!(!verdict.refuses_auto_enrollment());
        assert_eq!(verdict.kind_str(), "no_change");
    }

    #[test]
    fn guard_no_change_when_bound_display_name_is_none_and_current_is_some() {
        // Optional-field drift on display name alone is NOT a rename
        // detection — display name might just become available after
        // an admin sets it. Lock the behavior here.
        let b = bound("tn_a", None, "nk_self");
        let c = current("tn_a", Some("team-a"), "nk_self");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        assert_eq!(verdict, IdentityGuardVerdict::NoChange);
    }

    #[test]
    fn guard_no_change_when_bound_display_name_is_some_and_current_is_none() {
        let b = bound("tn_a", Some("team-a"), "nk_self");
        let c = current("tn_a", None, "nk_self");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        assert_eq!(verdict, IdentityGuardVerdict::NoChange);
    }

    #[test]
    fn guard_tailnet_changed_takes_priority_over_node_key_change() {
        // When BOTH have changed (user moved to a different tailnet on
        // a different machine — rare but possible), the tailnet check
        // fires first. The repair command for tailnet-change is
        // sufficient: disabling clears the bound state entirely, so a
        // subsequent auto-enroll picks up both new identities.
        let b = bound("tn_old", None, "nk_old");
        let c = current("tn_new", None, "nk_new");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        assert!(matches!(
            verdict,
            IdentityGuardVerdict::TailnetChanged { .. }
        ));
        assert!(verdict.refuses_auto_enrollment());
        assert_eq!(
            verdict.refusal_code(),
            Some(AUTO_ENROLLMENT_TAILNET_CHANGED_CODE)
        );
    }

    #[test]
    fn guard_tailnet_changed_captures_both_bound_and_current_ids() {
        let b = bound("tn_old", Some("old-team"), "nk_self");
        let c = current("tn_new", Some("new-team"), "nk_self");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        let IdentityGuardVerdict::TailnetChanged {
            bound_tailnet_id,
            bound_tailnet_display_name,
            current_tailnet_id,
            current_tailnet_display_name,
        } = verdict
        else {
            panic!("expected TailnetChanged");
        };
        assert_eq!(bound_tailnet_id, "tn_old");
        assert_eq!(bound_tailnet_display_name.as_deref(), Some("old-team"));
        assert_eq!(current_tailnet_id, "tn_new");
        assert_eq!(current_tailnet_display_name.as_deref(), Some("new-team"));
    }

    #[test]
    fn guard_node_key_changed_when_only_node_key_differs() {
        let b = bound("tn_a", None, "nk_old");
        let c = current("tn_a", None, "nk_new");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        let IdentityGuardVerdict::NodeKeyChanged {
            bound_node_key,
            current_node_key,
        } = verdict
        else {
            panic!("expected NodeKeyChanged");
        };
        assert_eq!(bound_node_key, "nk_old");
        assert_eq!(current_node_key, "nk_new");
        assert_eq!(
            evaluate_identity_guard(Some(&b), &c).refusal_code(),
            Some(AUTO_ENROLLMENT_NODE_KEY_CHANGED_CODE)
        );
    }

    #[test]
    fn guard_tailnet_renamed_when_id_matches_but_display_name_differs() {
        let b = bound("tn_a", Some("old-name"), "nk_self");
        let c = current("tn_a", Some("new-name"), "nk_self");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        let IdentityGuardVerdict::TailnetRenamed {
            tailnet_id,
            bound_display_name,
            current_display_name,
        } = verdict
        else {
            panic!("expected TailnetRenamed");
        };
        assert_eq!(tailnet_id, "tn_a");
        assert_eq!(bound_display_name.as_deref(), Some("old-name"));
        assert_eq!(current_display_name.as_deref(), Some("new-name"));
        // Rename is informational — does NOT refuse auto-enrollment.
        assert!(!evaluate_identity_guard(Some(&b), &c).refuses_auto_enrollment());
    }

    #[test]
    fn guard_node_key_change_priority_when_tailnet_id_matches_but_node_key_differs() {
        // Confirms the second-tier check fires when tailnet matches.
        let b = bound("tn_a", Some("team-a"), "nk_old");
        let c = current("tn_a", Some("team-a"), "nk_new");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        assert!(matches!(
            verdict,
            IdentityGuardVerdict::NodeKeyChanged { .. }
        ));
    }

    // ---- repair_command ----------------------------------------------------

    #[test]
    fn repair_command_for_tailnet_change_includes_disable_and_auto_enroll() {
        let b = bound("tn_old", None, "nk_self");
        let c = current("tn_new", None, "nk_self");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        let cmd = verdict
            .repair_command("/Users/me/projects/foo")
            .expect("repair available");
        assert!(cmd.contains("ee mesh disable --workspace \"/Users/me/projects/foo\""));
        assert!(cmd.contains("ee mesh auto-enroll --workspace \"/Users/me/projects/foo\""));
        assert!(cmd.contains("&&"));
    }

    #[test]
    fn repair_command_for_node_key_change_includes_explicit_reason_flag() {
        let b = bound("tn_a", None, "nk_old");
        let c = current("tn_a", None, "nk_new");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        let cmd = verdict
            .repair_command("/Users/me/projects/foo")
            .expect("repair available");
        assert!(cmd.contains("--reason \"restored from different machine\""));
        assert!(cmd.contains("ee mesh disable"));
        assert!(cmd.contains("ee mesh auto-enroll"));
    }

    #[test]
    fn repair_command_shell_quotes_workspace_path() {
        let b = bound("tn_old", None, "nk_self");
        let c = current("tn_new", None, "nk_self");
        let verdict = evaluate_identity_guard(Some(&b), &c);
        let cmd = verdict
            .repair_command("/Users/me/projects/weird path/it's \"fine\"")
            .expect("repair available");
        assert!(cmd.contains("--workspace \"/Users/me/projects/weird path/it's \\\"fine\\\"\""));
        assert!(!cmd.contains("--workspace \"/Users/me/projects/weird path/it's \"fine\""));
    }

    #[test]
    fn repair_command_returns_none_for_non_refusal_verdicts() {
        assert!(
            IdentityGuardVerdict::NoChange
                .repair_command("/x")
                .is_none()
        );
        assert!(
            IdentityGuardVerdict::NoBoundIdentity
                .repair_command("/x")
                .is_none()
        );
        assert!(
            IdentityGuardVerdict::TailnetRenamed {
                tailnet_id: "tn_a".to_owned(),
                bound_display_name: Some("old".to_owned()),
                current_display_name: Some("new".to_owned()),
            }
            .repair_command("/x")
            .is_none()
        );
    }

    // ---- evaluate_responder_bind -------------------------------------------

    #[test]
    fn responder_bind_no_bound_identity_when_bound_is_none() {
        let verdict = evaluate_responder_bind(None, "nk_self");
        assert_eq!(verdict, ResponderBindVerdict::BindNoBoundIdentity);
        assert!(!verdict.refuses_bind());
        assert!(verdict.refusal_code().is_none());
    }

    #[test]
    fn responder_bind_succeeds_when_bound_node_key_matches_current() {
        let b = bound("tn_a", None, "nk_self");
        let verdict = evaluate_responder_bind(Some(&b), "nk_self");
        assert_eq!(verdict, ResponderBindVerdict::Bind);
        assert!(!verdict.refuses_bind());
    }

    #[test]
    fn responder_bind_refuses_on_node_key_mismatch() {
        let b = bound("tn_a", None, "nk_old");
        let verdict = evaluate_responder_bind(Some(&b), "nk_new");
        let ResponderBindVerdict::RefuseNodeKeyMismatch {
            bound_node_key,
            current_node_key,
        } = verdict.clone()
        else {
            panic!("expected RefuseNodeKeyMismatch");
        };
        assert_eq!(bound_node_key, "nk_old");
        assert_eq!(current_node_key, "nk_new");
        assert!(verdict.refuses_bind());
        assert_eq!(
            verdict.refusal_code(),
            Some(HELLO_RESPONDER_NODE_KEY_MISMATCH_CODE)
        );
    }

    #[test]
    fn responder_bind_ignores_tailnet_id_drift_when_node_key_matches() {
        // The hello responder bind check is strictly node-key focused.
        // A tailnet-only change still produces a Bind verdict here; the
        // SRR6.46.3 caller-side guard handles the tailnet-change refusal.
        let b = bound("tn_old", None, "nk_self");
        let verdict = evaluate_responder_bind(Some(&b), "nk_self");
        assert_eq!(verdict, ResponderBindVerdict::Bind);
    }

    // ---- Verdict serde + kind_str -----------------------------------------

    #[test]
    fn verdict_serializes_with_tagged_kind_field() {
        let v = IdentityGuardVerdict::TailnetChanged {
            bound_tailnet_id: "tn_old".to_owned(),
            bound_tailnet_display_name: None,
            current_tailnet_id: "tn_new".to_owned(),
            current_tailnet_display_name: None,
        };
        let json = serde_json::to_string(&v).expect("serialize");
        assert!(json.contains("\"kind\":\"tailnet_changed\""));
    }

    #[test]
    fn verdict_kind_str_matches_serde_tag_for_all_variants() {
        for v in [
            IdentityGuardVerdict::TailnetChanged {
                bound_tailnet_id: String::new(),
                bound_tailnet_display_name: None,
                current_tailnet_id: String::new(),
                current_tailnet_display_name: None,
            },
            IdentityGuardVerdict::NodeKeyChanged {
                bound_node_key: String::new(),
                current_node_key: String::new(),
            },
            IdentityGuardVerdict::TailnetRenamed {
                tailnet_id: String::new(),
                bound_display_name: None,
                current_display_name: None,
            },
            IdentityGuardVerdict::NoChange,
            IdentityGuardVerdict::NoBoundIdentity,
        ] {
            let json = serde_json::to_string(&v).expect("serialize");
            assert!(
                json.contains(&format!("\"kind\":\"{}\"", v.kind_str())),
                "kind_str ({}) did not match serde tag in JSON: {}",
                v.kind_str(),
                json
            );
        }
    }
}