meerkat-core 0.7.2

Core agent logic for Meerkat (no I/O deps)
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
//! Auth lifecycle publication helpers.
//!
//! TokenStore owns credential material. AuthMachine owns lifecycle state.
//! Surfaces that write or clear credentials use these helpers so the public
//! status path observes the machine-owned lease instead of deriving phase from
//! persisted token bytes.

use chrono::{DateTime, Utc};
use thiserror::Error;

#[cfg(not(target_arch = "wasm32"))]
use std::{
    collections::HashMap,
    sync::{Arc, OnceLock, Weak},
};

use super::status::AuthStatusPhase;
use super::token_store::{
    PersistedAuthMode, PersistedTokens, TokenKey, TokenStore, TokenStoreError,
};
use crate::connection::AuthBindingRef;
use crate::generated::auth_lease_durable_lifecycle_marker as durable_marker;
use crate::handles::{
    AuthLeasePhase, AuthLeaseRestoreSnapshot, AuthLeaseSnapshot, AuthLeaseTransition,
    DslTransitionError, GeneratedAuthLeaseHandle, LeaseKey,
};

#[cfg(not(target_arch = "wasm32"))]
type LoginLifecycleLockMap = parking_lot::Mutex<HashMap<LeaseKey, Weak<tokio::sync::Mutex<()>>>>;

#[cfg(not(target_arch = "wasm32"))]
static LOGIN_LIFECYCLE_LOCKS: OnceLock<LoginLifecycleLockMap> = OnceLock::new();

#[cfg(not(target_arch = "wasm32"))]
fn login_lifecycle_locks() -> &'static LoginLifecycleLockMap {
    LOGIN_LIFECYCLE_LOCKS.get_or_init(|| parking_lot::Mutex::new(HashMap::new()))
}

/// Process-local guard that serializes credential commit, terminal OAuth
/// consume, and compensating rollback for one auth binding.
#[cfg(not(target_arch = "wasm32"))]
pub struct AuthLoginLifecycleGuard {
    _lease_key: LeaseKey,
    _guard: tokio::sync::OwnedMutexGuard<()>,
}

#[cfg(not(target_arch = "wasm32"))]
pub async fn acquire_auth_login_lifecycle_guard(lease_key: &LeaseKey) -> AuthLoginLifecycleGuard {
    let lock = {
        let mut locks = login_lifecycle_locks().lock();
        locks.retain(|_, lock| lock.strong_count() > 0);
        if let Some(lock) = locks.get(lease_key).and_then(Weak::upgrade) {
            lock
        } else {
            let lock = Arc::new(tokio::sync::Mutex::new(()));
            locks.insert(lease_key.clone(), Arc::downgrade(&lock));
            lock
        }
    };
    AuthLoginLifecycleGuard {
        _lease_key: lease_key.clone(),
        _guard: lock.lock_owned().await,
    }
}

pub fn persisted_token_expires_at_epoch_secs(tokens: &PersistedTokens) -> u64 {
    tokens
        .expires_at
        .map(|ts| ts.timestamp().max(0) as u64)
        .unwrap_or(u64::MAX)
}

pub fn persisted_auth_mode_uses_oauth_login_lifecycle(mode: PersistedAuthMode) -> bool {
    matches!(
        mode,
        PersistedAuthMode::ChatgptOauth
            | PersistedAuthMode::ExternalTokens
            | PersistedAuthMode::ClaudeAiOauth
            | PersistedAuthMode::OauthToApiKey
            | PersistedAuthMode::GoogleOauth
    )
}

/// Whether a credential of this mode can be created directly by writing a
/// secret at the `auth/profile/create` surfaces (RPC/REST). Direct-secret
/// modes (api key / static bearer) are creatable; OAuth-login-lifecycle modes
/// must use the `auth/login/start` + `auth/login/complete` flow instead.
///
/// Single owner of the "direct-secret only at create surfaces" predicate over
/// the typed [`PersistedAuthMode`].
pub fn persisted_auth_mode_is_directly_creatable(mode: PersistedAuthMode) -> bool {
    !persisted_auth_mode_uses_oauth_login_lifecycle(mode)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TokenLifecyclePublication {
    pub phase: Option<AuthLeasePhase>,
    pub generation: Option<u64>,
    pub expires_at: u64,
    pub credential_published_at_millis: Option<u64>,
}

#[derive(Debug, Error)]
pub enum TokenLifecycleMarkerError {
    #[error(
        "AuthMachine lifecycle transition belongs to `{transition_key}`, not token key `{token_key}`"
    )]
    LeaseKeyMismatch {
        token_key: String,
        transition_key: String,
    },
    #[error(
        "AuthMachine lifecycle transition expires_at {transition_expires_at} does not match token expires_at {token_expires_at}"
    )]
    ExpiresAtMismatch {
        token_expires_at: u64,
        transition_expires_at: u64,
    },
    #[error("AuthMachine lifecycle transition did not carry a credential publication time")]
    CredentialPublicationTimeMissing,
}

fn mark_tokens_lifecycle_published_inner(
    key: &TokenKey,
    tokens: &PersistedTokens,
    phase: AuthLeasePhase,
    generation: u64,
    credential_published_at_millis: u64,
) -> PersistedTokens {
    let mut marked = tokens.clone();
    marked.metadata = durable_marker::metadata_with_marker(
        &marked.metadata,
        durable_marker::DurableAuthLifecycleMarker {
            token_key: key.clone(),
            phase,
            expires_at: persisted_token_expires_at_epoch_secs(tokens),
            generation,
            credential_published_at_millis,
        },
    );
    marked
}

#[cfg(test)]
fn mark_tokens_lifecycle_published(key: &TokenKey, tokens: &PersistedTokens) -> PersistedTokens {
    mark_tokens_lifecycle_published_inner(key, tokens, AuthLeasePhase::Valid, 1, 1)
}

pub fn mark_tokens_lifecycle_published_for_transition(
    key: &TokenKey,
    tokens: &PersistedTokens,
    transition: &AuthLeaseTransition,
) -> Result<PersistedTokens, TokenLifecycleMarkerError> {
    let token_lease_key =
        LeaseKey::new(key.realm.clone(), key.binding.clone(), key.profile.clone());
    if transition.lease_key() != &token_lease_key {
        return Err(TokenLifecycleMarkerError::LeaseKeyMismatch {
            token_key: token_lease_key.to_string(),
            transition_key: transition.lease_key().to_string(),
        });
    }
    let token_expires_at = persisted_token_expires_at_epoch_secs(tokens);
    if transition.expires_at() != token_expires_at {
        return Err(TokenLifecycleMarkerError::ExpiresAtMismatch {
            token_expires_at,
            transition_expires_at: transition.expires_at(),
        });
    }
    let credential_published_at_millis = transition
        .credential_published_at_millis()
        .ok_or(TokenLifecycleMarkerError::CredentialPublicationTimeMissing)?;
    Ok(mark_tokens_lifecycle_published_inner(
        key,
        tokens,
        transition.phase(),
        transition.generation(),
        credential_published_at_millis,
    ))
}

pub fn tokens_lifecycle_published(tokens: &PersistedTokens) -> bool {
    durable_marker::metadata_has_valid_marker(&tokens.metadata)
}

pub fn tokens_lifecycle_published_generation(tokens: &PersistedTokens) -> Option<u64> {
    tokens_lifecycle_publication(tokens).and_then(|publication| publication.generation)
}

pub fn tokens_lifecycle_publication(tokens: &PersistedTokens) -> Option<TokenLifecyclePublication> {
    tokens_lifecycle_publication_inner(tokens, false)
}

pub fn tokens_lifecycle_publication_with_explicit_expiry(
    tokens: &PersistedTokens,
) -> Option<TokenLifecyclePublication> {
    tokens_lifecycle_publication_inner(tokens, true)
}

fn tokens_lifecycle_publication_inner(
    tokens: &PersistedTokens,
    _require_explicit_expiry: bool,
) -> Option<TokenLifecyclePublication> {
    let marker = durable_marker::read_marker_from_metadata(&tokens.metadata)?;
    Some(TokenLifecyclePublication {
        phase: Some(marker.phase),
        generation: Some(marker.generation),
        expires_at: marker.expires_at,
        credential_published_at_millis: Some(marker.credential_published_at_millis),
    })
}

pub fn tokens_lifecycle_published_credential_time(tokens: &PersistedTokens) -> Option<u64> {
    durable_marker::read_marker_from_metadata(&tokens.metadata)
        .map(|marker| marker.credential_published_at_millis)
}

/// Record a credential acquisition in the AuthMachine lease (an in-memory DSL
/// transition; no durable I/O).
///
/// Acquire-first ordering contract (shared by the MCP OAuth login and the
/// REST/CLI/RPC token-commit surfaces): call this BEFORE any durable
/// `TokenStore::save`, stamp the durable lifecycle marker from the returned
/// transition via [`mark_tokens_lifecycle_published_for_transition`], and
/// persist the credential in that single marked write. The marker is the
/// durable proof-of-acquisition; an unmarked persisted token is dead data and
/// is rejected by `restore_marked_token_lifecycle` /
/// [`rehydrate_marked_tokens_for_status`] and the resolver admission gate. A
/// rejected acquisition mutates nothing, so the caller propagates the error
/// without compensation; if the durable save fails AFTER acquisition, the
/// caller must roll the acquired lease back so no half-state survives.
pub fn publish_token_lifecycle_acquired(
    handle: &GeneratedAuthLeaseHandle,
    auth_binding: &AuthBindingRef,
    tokens: &PersistedTokens,
) -> Result<AuthLeaseTransition, DslTransitionError> {
    let lease_key = LeaseKey::from_auth_binding(auth_binding);
    handle.acquire_lease(&lease_key, persisted_token_expires_at_epoch_secs(tokens))
}

pub fn publish_token_lifecycle_released(
    handle: &GeneratedAuthLeaseHandle,
    auth_binding: &AuthBindingRef,
) -> Result<(), DslTransitionError> {
    let lease_key = LeaseKey::from_auth_binding(auth_binding);
    handle.release_lease(&lease_key)
}

#[derive(Debug, Error)]
pub enum TokenLifecycleClearError {
    #[error("AuthMachine lifecycle release failed: {0}")]
    AuthMachineRelease(DslTransitionError),
    #[error("TokenStore clear failed: {0}")]
    TokenStoreClear(TokenStoreError),
    /// The durable clear failed AND rolling the staged lease release back to
    /// the pre-clear snapshot was rejected. Both typed faults are carried;
    /// durable truth still holds the credential, so the next status rehydrate
    /// restores the lease projection from the durable lifecycle marker.
    #[error("TokenStore clear failed ({clear}); staged lease release rollback failed ({restore})")]
    StagedReleaseRestore {
        clear: TokenStoreError,
        restore: DslTransitionError,
    },
}

/// Clear persisted token material and release the AuthMachine lifecycle.
///
/// The lease release is STAGED before the durable clear commits, so no lease
/// operation ever runs after the durable credential is gone — the
/// "resurrected live lease over a cleared credential" interleaving is
/// unrepresentable:
///
/// - If staging the release fails (a release-observer fault — which aborts
///   the release before its authoritative transition — or a machine
///   rejection), the typed
///   [`TokenLifecycleClearError::AuthMachineRelease`] fault propagates and
///   the durable clear NEVER runs. Durable truth retains the credential and
///   the lease projection still matches it; the clear is retryable.
/// - If the durable clear commit fails, the staged release is rolled back
///   from the pre-stage snapshot (legal: the durable record still holds the
///   credential, so lease truth re-aligns with durable truth) and the typed
///   [`TokenLifecycleClearError::TokenStoreClear`] fault propagates.
/// - Once the durable clear has committed, the operation is complete: the
///   lease was already released at staging, and nothing fallible follows the
///   commit.
pub async fn clear_tokens_and_publish_lifecycle_released(
    store: &dyn TokenStore,
    handle: &GeneratedAuthLeaseHandle,
    auth_binding: &AuthBindingRef,
) -> Result<(), TokenLifecycleClearError> {
    let key = TokenKey::from_auth_binding(auth_binding);
    let lease_key = LeaseKey::from_auth_binding(auth_binding);

    // Stage: release the lease BEFORE the durable commit, capturing the
    // pre-stage snapshot for rollback if the commit fails.
    let staged = handle.capture_auth_lifecycle_restore_snapshot(&lease_key);
    publish_token_lifecycle_released(handle, auth_binding)
        .map_err(TokenLifecycleClearError::AuthMachineRelease)?;

    // Commit: one durable mutation destroys the credential and its lifecycle
    // marker together.
    if let Err(clear_err) = store.clear(&key).await {
        // Pre-commit rollback: durable truth still holds the credential, so
        // restoring the staged release keeps the lease projection aligned.
        if let Err(restore_err) = restore_token_lifecycle_snapshot(handle, &staged) {
            return Err(TokenLifecycleClearError::StagedReleaseRestore {
                clear: clear_err,
                restore: restore_err,
            });
        }
        return Err(TokenLifecycleClearError::TokenStoreClear(clear_err));
    }
    Ok(())
}

/// Restore an AuthMachine lease projection from a previously captured
/// snapshot.
///
/// Callers that need to compensate a token write after a later step fails can
/// use this with the token snapshot captured before the write. If the previous
/// snapshot had no credential-backed active phase, this helper is a no-op; it
/// must not recreate credential authority from token bytes alone.
pub fn restore_token_lifecycle_snapshot(
    handle: &GeneratedAuthLeaseHandle,
    captured: &AuthLeaseRestoreSnapshot,
) -> Result<Option<AuthLeaseTransition>, DslTransitionError> {
    let snapshot = captured.snapshot();
    if !snapshot.credential_present {
        return Ok(None);
    }
    let Some(phase) = snapshot.phase else {
        return Ok(None);
    };
    if phase == AuthLeasePhase::Released {
        return Ok(None);
    }

    handle.restore_auth_lifecycle_snapshot(captured)
}

pub fn lease_snapshot_expires_at_datetime(snapshot: &AuthLeaseSnapshot) -> Option<DateTime<Utc>> {
    snapshot
        .expires_at
        .and_then(|secs| i64::try_from(secs).ok())
        .and_then(|secs| DateTime::<Utc>::from_timestamp(secs, 0))
}

#[derive(Debug)]
pub struct PublishedAuthStatus<'a> {
    pub phase: AuthStatusPhase,
    pub expires_at: Option<DateTime<Utc>>,
    pub tokens: Option<&'a PersistedTokens>,
}

#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Error)]
pub enum AuthStatusRehydrateError {
    #[error("token store error: {0}")]
    TokenStore(#[from] TokenStoreError),
    #[error("AuthMachine lifecycle acquire failed: {0}")]
    LifecycleAcquire(DslTransitionError),
    #[error("AuthMachine lifecycle restore failed: {0}")]
    LifecycleRestore(DslTransitionError),
    #[error("AuthMachine lifecycle rollback failed after token marker save failure: {0}")]
    LifecycleRollback(DslTransitionError),
    #[error("TokenStore lifecycle marker save failed: {0}")]
    MarkerSave(TokenStoreError),
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn restore_marked_token_lifecycle(
    auth_lease: &GeneratedAuthLeaseHandle,
    auth_binding: &AuthBindingRef,
    tokens: &PersistedTokens,
) -> Result<Option<PersistedTokens>, AuthStatusRehydrateError> {
    let key = TokenKey::from_auth_binding(auth_binding);
    let Some(publication) =
        durable_marker::restore_publication_from_metadata(&tokens.metadata, &key)
    else {
        return Ok(None);
    };
    if publication.expires_at() != persisted_token_expires_at_epoch_secs(tokens) {
        return Ok(None);
    }
    let lease_key = LeaseKey::from_auth_binding(auth_binding);
    auth_lease
        .restore_published_credential_lifecycle(&lease_key, &publication)
        .map_err(AuthStatusRehydrateError::LifecycleRestore)?;
    Ok(Some(tokens.clone()))
}

#[cfg(not(target_arch = "wasm32"))]
pub async fn rehydrate_marked_tokens_for_status(
    token_store: &dyn TokenStore,
    auth_lease: &GeneratedAuthLeaseHandle,
    auth_binding: &AuthBindingRef,
    expected_mode: PersistedAuthMode,
    now: DateTime<Utc>,
) -> Result<Option<PersistedTokens>, AuthStatusRehydrateError> {
    let key = TokenKey::from_auth_binding(auth_binding);
    let Some(tokens) = token_store.load(&key).await? else {
        // Durable truth holds no credential for this binding. The in-process
        // lease is a projection of durable truth: a credential-bearing lease
        // that outlived its durable record (e.g. a clear whose release
        // transition was rejected) is released here so public status cannot
        // keep projecting a credential the store no longer holds.
        let lease_key = LeaseKey::from_auth_binding(auth_binding);
        let captured = auth_lease.capture_auth_lifecycle_restore_snapshot(&lease_key);
        let snapshot = captured.snapshot();
        let lease_is_live = snapshot.credential_present
            && snapshot
                .phase
                .is_some_and(|phase| phase != AuthLeasePhase::Released);
        if lease_is_live {
            auth_lease
                .release_lease(&lease_key)
                .map_err(AuthStatusRehydrateError::LifecycleRestore)?;
        }
        return Ok(None);
    };
    if tokens.auth_mode != expected_mode {
        return Ok(None);
    }
    let restored = restore_marked_token_lifecycle(auth_lease, auth_binding, &tokens)?;
    if restored.is_some() {
        let lease_key = LeaseKey::from_auth_binding(auth_binding);
        auth_lease
            .observe_credential_freshness(
                &lease_key,
                now.timestamp().max(0) as u64,
                crate::handles::AUTH_LEASE_TTL_REFRESH_WINDOW_SECS,
            )
            .map_err(AuthStatusRehydrateError::LifecycleRestore)?;
    }
    Ok(restored)
}

pub fn project_published_auth_status<'a>(
    now: DateTime<Utc>,
    stored: Option<&'a PersistedTokens>,
    snapshot: &AuthLeaseSnapshot,
) -> PublishedAuthStatus<'a> {
    let phase = AuthStatusPhase::from_lease_snapshot(now, snapshot);
    if phase.is_no_live_lease() {
        return PublishedAuthStatus {
            phase,
            expires_at: None,
            tokens: None,
        };
    }
    PublishedAuthStatus {
        phase,
        expires_at: lease_snapshot_expires_at_datetime(snapshot)
            .or_else(|| stored.and_then(|tokens| tokens.expires_at)),
        tokens: stored,
    }
}

pub fn oauth_status_projection_snapshot_from_newer_marker(
    snapshot: &AuthLeaseSnapshot,
    tokens: &PersistedTokens,
) -> Option<AuthLeaseSnapshot> {
    let _ = (snapshot, tokens);
    None
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    use crate::auth::PersistedAuthMode;
    use crate::handles::AuthLeasePhase;

    fn oauth_tokens_with_metadata(metadata: serde_json::Value) -> PersistedTokens {
        PersistedTokens {
            auth_mode: PersistedAuthMode::ChatgptOauth,
            primary_secret: Some("access".into()),
            refresh_token: Some("refresh".into()),
            id_token: None,
            expires_at: None,
            last_refresh: None,
            scopes: Vec::new(),
            account_id: None,
            metadata,
        }
    }

    fn marker_test_key() -> TokenKey {
        TokenKey::parse("dev", "default_openai").unwrap()
    }

    #[test]
    fn direct_secret_modes_are_creatable_oauth_login_modes_are_not() {
        // Direct-secret persisted modes are creatable at the auth/profile/create
        // surfaces; OAuth-login-lifecycle modes are not.
        for mode in [PersistedAuthMode::ApiKey, PersistedAuthMode::StaticBearer] {
            assert!(
                persisted_auth_mode_is_directly_creatable(mode),
                "{mode:?} must be directly creatable"
            );
        }
        for mode in [
            PersistedAuthMode::ChatgptOauth,
            PersistedAuthMode::ExternalTokens,
            PersistedAuthMode::ClaudeAiOauth,
            PersistedAuthMode::OauthToApiKey,
            PersistedAuthMode::GoogleOauth,
        ] {
            assert!(
                !persisted_auth_mode_is_directly_creatable(mode),
                "{mode:?} (oauth-login lifecycle) must NOT be directly creatable"
            );
        }
    }

    #[test]
    fn lifecycle_marker_is_added_to_managed_store_tokens() {
        let key = marker_test_key();
        let api_key = PersistedTokens::api_key("sk-test");
        let marked_api_key = mark_tokens_lifecycle_published(&key, &api_key);
        assert!(tokens_lifecycle_published(&marked_api_key));

        let oauth = oauth_tokens_with_metadata(serde_json::Value::Null);
        let marked = mark_tokens_lifecycle_published(&key, &oauth);
        assert!(tokens_lifecycle_published(&marked));
        assert!(!tokens_lifecycle_published(&oauth));
    }

    #[test]
    fn lifecycle_marker_preserves_existing_object_metadata() {
        let key = marker_test_key();
        let oauth = oauth_tokens_with_metadata(serde_json::json!({
            "provider": "openai",
        }));

        let marked = mark_tokens_lifecycle_published(&key, &oauth);

        assert!(tokens_lifecycle_published(&marked));
        assert_eq!(marked.metadata["provider"], "openai");
    }

    #[test]
    fn unmarked_orphan_tokens_are_dead_data_for_lifecycle_restore() {
        // Vault property: a persisted token WITHOUT the durable lifecycle
        // marker (the on-disk shape an interrupted save-then-acquire commit
        // used to leave behind) carries no proof-of-acquisition. Every
        // restore/rehydration entry point must treat it as dead data.
        let key = marker_test_key();
        let orphan = oauth_tokens_with_metadata(serde_json::Value::Null);

        assert!(!tokens_lifecycle_published(&orphan));
        assert!(tokens_lifecycle_publication(&orphan).is_none());
        assert!(
            durable_marker::restore_publication_from_metadata(&orphan.metadata, &key).is_none()
        );

        // Arbitrary non-marker metadata is equally dead.
        let decorated = oauth_tokens_with_metadata(serde_json::json!({
            "provider": "openai",
            "source": "legacy-import",
        }));
        assert!(!tokens_lifecycle_published(&decorated));
        assert!(
            durable_marker::restore_publication_from_metadata(&decorated.metadata, &key).is_none()
        );
    }

    #[test]
    fn lifecycle_marker_restore_is_bound_to_token_identity() {
        let key = marker_test_key();
        let wrong_key = TokenKey::parse("dev", "other_openai").unwrap();
        let oauth = oauth_tokens_with_metadata(serde_json::Value::Null);
        let marked = mark_tokens_lifecycle_published(&key, &oauth);

        assert!(
            durable_marker::restore_publication_from_metadata(&marked.metadata, &key).is_some()
        );
        assert!(
            durable_marker::restore_publication_from_metadata(&marked.metadata, &wrong_key)
                .is_none()
        );
    }

    #[test]
    fn lifecycle_marker_relation_is_bound_to_token_identity() {
        let key = marker_test_key();
        let wrong_key = TokenKey::parse("dev", "other_openai").unwrap();
        let oauth = oauth_tokens_with_metadata(serde_json::Value::Null);
        let marked = mark_tokens_lifecycle_published(&key, &oauth);
        let snapshot = AuthLeaseSnapshot {
            phase: Some(AuthLeasePhase::Valid),
            expires_at: Some(u64::MAX),
            credential_present: true,
            generation: 1,
            credential_published_at_millis: Some(1),
        };

        assert!(durable_marker::marker_payload_valid_for_tokens(
            &marked, &key
        ));
        assert!(!durable_marker::marker_payload_valid_for_tokens(
            &marked, &wrong_key
        ));
        assert_eq!(
            durable_marker::marker_relation_for_tokens_and_snapshot(&marked, &snapshot, &key),
            durable_marker::AuthLeaseDurableMarkerRelation::Matches
        );
        assert_eq!(
            durable_marker::marker_relation_for_tokens_and_snapshot(&marked, &snapshot, &wrong_key),
            durable_marker::AuthLeaseDurableMarkerRelation::Invalid
        );
    }

    #[test]
    fn lifecycle_marker_rejects_wrong_schema_version() {
        let oauth = oauth_tokens_with_metadata(serde_json::json!({
            "meerkat_auth_lifecycle": {
                "published": true,
                "version": 2,
                "authority": "auth_machine",
                "protocol": "auth_lease_lifecycle_publication",
                "phase": "valid",
                "generation": 1,
                "expires_at": u64::MAX,
                "credential_published_at_millis": 1,
            }
        }));

        assert!(!tokens_lifecycle_published(&oauth));
        assert!(tokens_lifecycle_publication(&oauth).is_none());
    }

    #[test]
    fn published_status_projects_lease_phase_without_token_material() {
        let now = Utc::now();
        let snapshot = AuthLeaseSnapshot {
            phase: Some(AuthLeasePhase::Valid),
            expires_at: Some((now + chrono::Duration::hours(1)).timestamp() as u64),
            credential_present: true,
            generation: 1,
            credential_published_at_millis: None,
        };

        let status = project_published_auth_status(now, None, &snapshot);

        assert_eq!(status.phase, AuthStatusPhase::Valid);
        assert!(status.expires_at.is_some());
        assert!(status.tokens.is_none());
    }
}