toktier 0.2.7

Rust API preview for TokTier's exact, stateful tokenization runtime
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
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, Instant};

use toktier_routing_core::{NativeRouter, ReferenceEngine};
use toktier_store_core::{ContentDigest, KeyId, SessionHandle, SessionStore, StoreConfig};
#[cfg(feature = "sqlite")]
use toktier_store_sqlite::{NamedSessionRef, RecoveredNamedSession, SingleEngine, StoreDb};

use crate::manifest::LocalArtifact;
use crate::{
    Backend, Certification, Encoding, Error, ErrorCode, ExecutionFacts, ReasonCode, Result,
    RoutePlan, TokenBuffer, TokenPatch,
};

#[derive(Debug)]
pub(crate) struct TokenizerInner {
    pub(crate) artifact: LocalArtifact,
    pub(crate) reference: Arc<ReferenceEngine>,
    pub(crate) router: Arc<NativeRouter>,
    pub(crate) plan: RoutePlan,
    pub(crate) store: std::sync::Mutex<SessionStoreState>,
    pub(crate) gpu_detail: Option<(String, i32)>,
    /// What a local verification of this route would be filed under,
    /// when the route is one a campaign has not judged.
    pub(crate) gpu_verification_key: Option<crate::verify_local::VerificationKey>,
    #[cfg(feature = "jit")]
    pub(crate) jit_detail: Option<crate::JitArtifact>,
}

pub(crate) struct SessionStoreState {
    pub(crate) store: SessionStore,
    pub(crate) key: KeyId,
    names: HashMap<String, SessionHandle>,
    leased: HashSet<String>,
    /// Full plaintext is retained only for durable named sessions. Store-v1
    /// intentionally omits sealed-prefix text, so restart-safe delta appends
    /// need this caller-owned recovery input to revalidate TKFR-v1.
    transcripts: Option<HashMap<String, String>>,
    /// A durable write failed after the in-memory mutation. The process may
    /// continue serving stateless requests, but this session store is sealed
    /// until restart reloads the last atomic SQLite commit.
    persistence_fault: Option<String>,
    #[cfg(feature = "sqlite")]
    database: Option<StoreDb>,
}

impl std::fmt::Debug for SessionStoreState {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("SessionStoreState")
            .field("names", &self.names.len())
            .field("leased", &self.leased.len())
            .finish_non_exhaustive()
    }
}

impl SessionStoreState {
    pub(crate) fn new(
        router: Arc<NativeRouter>,
        fingerprint: [u8; 32],
        seal_guard: u64,
        database_path: Option<&Path>,
        seed_digest_overlap: bool,
    ) -> Result<Self> {
        #[cfg(not(feature = "sqlite"))]
        let _ = &router;
        #[cfg(feature = "sqlite")]
        if let Some(path) = database_path {
            if let Some(parent) = path.parent() {
                ensure_store_home(parent)?;
            }
            let populated = path.metadata().is_ok_and(|metadata| metadata.len() > 0);
            let database = StoreDb::open(path)?;
            if populated {
                let (mut store, recovered) =
                    database.load_named_recoverable(&SingleEngine(router.as_ref()))?;
                set_seed_overlap(&mut store, seed_digest_overlap);
                let key = store
                    .export_fingerprints()
                    .into_iter()
                    .find_map(|(id, candidate, _)| (candidate == fingerprint).then_some(KeyId(id)))
                    .ok_or_else(|| {
                        Error::new(
                            ErrorCode::SessionStateMismatch,
                            "persistent store does not contain this tokenizer fingerprint",
                        )
                    })?;
                let names = recovered
                    .iter()
                    .map(|session| (session.name.clone(), session.handle))
                    .collect();
                let transcripts = recovered
                    .into_iter()
                    .map(
                        |RecoveredNamedSession {
                             name, transcript, ..
                         }| (name, transcript),
                    )
                    .collect();
                return Ok(Self {
                    store,
                    key,
                    names,
                    leased: HashSet::new(),
                    transcripts: Some(transcripts),
                    persistence_fault: None,
                    database: Some(database),
                });
            }
            let mut store = new_store(true, seed_digest_overlap)?;
            let key = store.register_fingerprint(fingerprint, seal_guard)?;
            let mut state = Self {
                store,
                key,
                names: HashMap::new(),
                leased: HashSet::new(),
                transcripts: Some(HashMap::new()),
                persistence_fault: None,
                database: Some(database),
            };
            state.persist()?;
            return Ok(state);
        }
        #[cfg(not(feature = "sqlite"))]
        if database_path.is_some() {
            return Err(Error::new(
                ErrorCode::ConfigInvalid,
                "persistent sessions require the sqlite feature",
            ));
        }

        let mut store = new_store(false, seed_digest_overlap)?;
        let key = store.register_fingerprint(fingerprint, seal_guard)?;
        Ok(Self {
            store,
            key,
            names: HashMap::new(),
            leased: HashSet::new(),
            transcripts: None,
            persistence_fault: None,
            #[cfg(feature = "sqlite")]
            database: None,
        })
    }

    fn persist(&mut self) -> Result<()> {
        self.ensure_healthy()?;
        #[cfg(feature = "sqlite")]
        if let Some(database) = &mut self.database {
            let transcripts = self.transcripts.as_ref().ok_or_else(|| {
                Error::new(
                    ErrorCode::Internal,
                    "persistent session state lost its recovery transcripts",
                )
            })?;
            let sessions = self
                .names
                .iter()
                .map(|(name, handle)| {
                    let transcript = transcripts.get(name).ok_or_else(|| {
                        Error::new(
                            ErrorCode::SessionStateMismatch,
                            format!("persistent session {name:?} has no recovery transcript"),
                        )
                    })?;
                    Ok(NamedSessionRef {
                        name,
                        handle: *handle,
                        transcript,
                    })
                })
                .collect::<Result<Vec<_>>>()?;
            if let Err(error) = database.save_named_recoverable(&self.store, &sessions) {
                let error = Error::from(error);
                self.persistence_fault = Some(error.to_string());
                return Err(error);
            }
        }
        Ok(())
    }

    pub(crate) fn ensure_healthy(&self) -> Result<()> {
        match &self.persistence_fault {
            Some(cause) => Err(Error::new(
                ErrorCode::SessionStateMismatch,
                format!(
                    "persistent session state is sealed after a failed atomic save; restart to reload the last committed state: {cause}"
                ),
            )),
            None => Ok(()),
        }
    }
}

#[cfg(feature = "sqlite")]
fn ensure_store_home(path: &Path) -> Result<()> {
    // The store home is created the way every other private directory in
    // this crate is: one component at a time, each one owner-only.
    crate::fsutil::create_private_dir_all(path)
}

fn new_store(persistent: bool, seed_digest_overlap: bool) -> Result<SessionStore> {
    let mut store = SessionStore::new(StoreConfig::default())?;
    if persistent {
        store.enable_recovery_tracking()?;
    }
    store.enable_content_tracking()?;
    set_seed_overlap(&mut store, seed_digest_overlap);
    Ok(store)
}

/// Install the bounded-pool overlap runner when the runtime opted in
/// (PLAN/162 WP5/WP6). Recovery hashing and durable serialization stay
/// serial in the durable tier; only the seed content-digest scan moves
/// next to the seed encode.
fn set_seed_overlap(store: &mut SessionStore, enabled: bool) {
    if enabled {
        store.set_seed_overlap(Some(Arc::new(toktier_routing_core::RayonSeedOverlap)));
    }
}

/// Compact observability snapshot for one named session.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionStats {
    pub name: String,
    pub revision: Option<u64>,
    pub token_count: u64,
    pub closed: bool,
}

/// One non-cloneable, single-writer agent session.
///
/// Mutation requires one exclusive Rust borrow, so duplicate in-process
/// writers are rejected by the type system before the runtime revision gate:
///
/// ```compile_fail
/// use toktier::Session;
/// fn invalid(session: &mut Session) {
///     let first = &mut *session;
///     let second = &mut *session;
///     let _ = first.append("a");
///     let _ = second.append("b");
/// }
/// ```
pub struct Session {
    tokenizer: Arc<TokenizerInner>,
    name: String,
    handle: Option<SessionHandle>,
    revision: Option<u64>,
    closed: bool,
}

impl std::fmt::Debug for Session {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Session")
            .field("name", &self.name)
            .field("revision", &self.revision)
            .field("closed", &self.closed)
            .finish_non_exhaustive()
    }
}

impl Session {
    pub(crate) fn open(tokenizer: Arc<TokenizerInner>, name: String) -> Result<Self> {
        validate_name(&name)?;
        let (handle, revision) = {
            let mut state = tokenizer
                .store
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            state.ensure_healthy()?;
            if !state.leased.insert(name.clone()) {
                return Err(Error::new(
                    ErrorCode::SessionRevisionConflict,
                    format!("session {name:?} already has a writer in this process"),
                ));
            }
            let handle = state.names.get(&name).copied();
            let revision = handle
                .map(|handle| state.store.revision(handle))
                .transpose()?;
            (handle, revision)
        };
        Ok(Self {
            tokenizer,
            name,
            handle,
            revision,
            closed: false,
        })
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn revision(&self) -> Option<u64> {
        self.revision
    }

    /// Seed a newly named session and return its initial complete stream.
    pub fn seed(&mut self, text: &str) -> Result<Encoding> {
        self.seed_observed(text).map(|(encoding, _)| encoding)
    }

    pub(crate) fn seed_observed(&mut self, text: &str) -> Result<(Encoding, Duration)> {
        self.require_open()?;
        if self.handle.is_some() {
            return Err(Error::new(
                ErrorCode::SessionRevisionConflict,
                "seed requires a new session; use overwrite for an existing name",
            ));
        }
        let (handle, revision, ids, persistence) = {
            let mut state = self.lock_store();
            state.ensure_healthy()?;
            let key = state.key;
            let put = state.store.put(key, text, self.tokenizer.router.as_ref())?;
            state.names.insert(self.name.clone(), put.handle);
            if let Some(transcripts) = &mut state.transcripts {
                transcripts.insert(self.name.clone(), text.to_owned());
            }
            // The returned Encoding shares the store's adopted allocation
            // (or its one cached materialization) instead of copying the
            // complete row again.
            let ids = state.store.shared_all_ids(put.handle)?;
            let persistence_started = Instant::now();
            state.persist()?;
            (put.handle, put.revision, ids, persistence_started.elapsed())
        };
        self.handle = Some(handle);
        self.revision = Some(revision);
        Ok((
            Encoding::from_buffer(
                TokenBuffer::from_shared(ids),
                session_facts(&self.tokenizer, "session_seed", text.len()),
            ),
            persistence,
        ))
    }

    /// Append only new text and return the exact BPE suffix replacement.
    pub fn append(&mut self, delta: &str) -> Result<TokenPatch> {
        self.append_observed(delta).map(|(patch, _)| patch)
    }

    pub(crate) fn append_observed(&mut self, delta: &str) -> Result<(TokenPatch, Duration)> {
        self.require_open()?;
        let handle = self.handle.ok_or_else(|| {
            Error::new(ErrorCode::InvalidArgument, "seed the session before append")
        })?;
        let expected_revision = self
            .revision
            .ok_or_else(|| Error::new(ErrorCode::Internal, "seeded session has no revision"))?;
        let (patch, persistence) = {
            let mut state = self.lock_store();
            state.ensure_healthy()?;
            let patch = state.store.append_patch(
                handle,
                delta,
                expected_revision,
                self.tokenizer.router.as_ref(),
            )?;
            if let Some(transcripts) = &mut state.transcripts {
                let transcript = transcripts.get_mut(&self.name).ok_or_else(|| {
                    Error::new(
                        ErrorCode::SessionStateMismatch,
                        "persistent session is missing its recovery transcript",
                    )
                })?;
                transcript.push_str(delta);
            }
            let persistence_started = Instant::now();
            state.persist()?;
            (patch, persistence_started.elapsed())
        };
        self.revision = Some(patch.revision);
        let execution = ExecutionFacts {
            backend: backend_for_path(&patch.path),
            path: patch.path.clone(),
            source: Some("native_session".to_owned()),
            input_bytes: delta.len() as u64,
            certification: self.tokenizer.plan.certification,
            reason: reason_for_repair_path(&patch.path),
        };
        Ok((
            TokenPatch::new(
                patch.replace_from,
                patch.replacement_ids,
                patch.revision,
                patch.token_count,
                execution,
            ),
            persistence,
        ))
    }

    /// Compatibility operation for callers that still submit the complete
    /// transcript. Prefix identity is checked with the store's native content
    /// digest before only the suffix is repaired.
    pub fn encode_transcript(&mut self, complete_text: &str) -> Result<Encoding> {
        self.require_open()?;
        let handle = self.handle.ok_or_else(|| {
            Error::new(
                ErrorCode::InvalidArgument,
                "seed the session before compatibility encode",
            )
        })?;
        let prefix_len = {
            let state = self.lock_store();
            state.ensure_healthy()?;
            let expected = state.store.content_index_entry(handle)?.ok_or_else(|| {
                Error::new(
                    ErrorCode::SessionStateMismatch,
                    "session has no recoverable content binding; seed or reopen with history",
                )
            })?;
            let prefix_len = usize::try_from(expected.byte_length).map_err(|_| {
                Error::new(
                    ErrorCode::InvalidArgument,
                    "session text length exceeds usize",
                )
            })?;
            let prefix = complete_text.as_bytes().get(..prefix_len).ok_or_else(|| {
                Error::new(
                    ErrorCode::SessionStateMismatch,
                    "complete transcript is shorter than the stored session",
                )
            })?;
            std::str::from_utf8(prefix).map_err(|_| {
                Error::new(
                    ErrorCode::SessionStateMismatch,
                    "stored byte length is not a UTF-8 boundary in the candidate transcript",
                )
            })?;
            let observed = ContentDigest::from_bytes(prefix)?.entry();
            if observed != expected {
                return Err(Error::new(
                    ErrorCode::SessionStateMismatch,
                    "complete transcript does not extend the stored session exactly",
                ));
            }
            prefix_len
        };
        let delta = &complete_text[prefix_len..];
        self.append(delta)?;
        self.snapshot()
    }

    /// Materialize the complete token stream explicitly.
    pub fn snapshot(&self) -> Result<Encoding> {
        self.require_open()?;
        let handle = self.handle.ok_or_else(|| {
            Error::new(
                ErrorCode::InvalidArgument,
                "seed the session before snapshot",
            )
        })?;
        let mut state = self.lock_store();
        state.ensure_healthy()?;
        // Explicit materialization point: the complete row is served from
        // the generation-keyed shared snapshot, so repeated snapshots of
        // an unchanged session return the same immutable allocation.
        let ids = state.store.shared_all_ids(handle)?;
        Ok(Encoding::from_buffer(
            TokenBuffer::from_shared(ids),
            session_facts(&self.tokenizer, "session_snapshot", 0),
        ))
    }

    /// Replace an existing named session with a new genesis stream.
    pub fn overwrite(&mut self, text: &str) -> Result<Encoding> {
        self.require_open()?;
        let (handle, revision, ids) = {
            let mut state = self.lock_store();
            state.ensure_healthy()?;
            if let Some(old) = self.handle {
                state.store.evict(old);
            }
            let key = state.key;
            let put = state.store.put(key, text, self.tokenizer.router.as_ref())?;
            state.names.insert(self.name.clone(), put.handle);
            if let Some(transcripts) = &mut state.transcripts {
                transcripts.insert(self.name.clone(), text.to_owned());
            }
            let ids = state.store.shared_all_ids(put.handle)?;
            state.persist()?;
            (put.handle, put.revision, ids)
        };
        self.handle = Some(handle);
        self.revision = Some(revision);
        Ok(Encoding::from_buffer(
            TokenBuffer::from_shared(ids),
            session_facts(&self.tokenizer, "session_overwrite", text.len()),
        ))
    }

    /// Fork this session under a new stable name. Sealed content-addressed
    /// nodes are shared; the new revision lineage starts at zero.
    pub fn fork(&self, new_name: impl Into<String>) -> Result<Session> {
        self.require_open()?;
        let new_name = new_name.into();
        validate_name(&new_name)?;
        let handle = self.handle.ok_or_else(|| {
            Error::new(ErrorCode::InvalidArgument, "seed the session before fork")
        })?;
        let forked = {
            let mut state = self.lock_store();
            state.ensure_healthy()?;
            if state.names.contains_key(&new_name) || state.leased.contains(&new_name) {
                return Err(Error::new(
                    ErrorCode::SessionRevisionConflict,
                    format!("session {new_name:?} already exists"),
                ));
            }
            let forked = state.store.fork(handle)?;
            state.names.insert(new_name.clone(), forked);
            let forked_transcript = state
                .transcripts
                .as_ref()
                .and_then(|transcripts| transcripts.get(&self.name).cloned());
            if let Some(transcripts) = &mut state.transcripts {
                let transcript = forked_transcript.ok_or_else(|| {
                    Error::new(
                        ErrorCode::SessionStateMismatch,
                        "persistent source session has no recovery transcript",
                    )
                })?;
                transcripts.insert(new_name.clone(), transcript);
            }
            state.leased.insert(new_name.clone());
            state.persist()?;
            forked
        };
        Ok(Session {
            tokenizer: Arc::clone(&self.tokenizer),
            name: new_name,
            handle: Some(forked),
            revision: Some(0),
            closed: false,
        })
    }

    /// Delete this session's persistent/in-memory state.
    pub fn delete(mut self) -> Result<()> {
        self.require_open()?;
        {
            let mut state = self.lock_store();
            state.ensure_healthy()?;
            if let Some(handle) = self.handle {
                state.store.evict(handle);
            }
            state.names.remove(&self.name);
            if let Some(transcripts) = &mut state.transcripts {
                transcripts.remove(&self.name);
            }
            state.leased.remove(&self.name);
            state.persist()?;
        }
        self.closed = true;
        Ok(())
    }

    /// Release the single-writer lease while retaining reopenable state.
    pub fn close(mut self) -> Result<()> {
        self.release_lease();
        self.closed = true;
        Ok(())
    }

    pub fn stats(&self) -> Result<SessionStats> {
        let token_count = match self.handle {
            Some(handle) if !self.closed => {
                let state = self.lock_store();
                state.ensure_healthy()?;
                state.store.session_info(handle)?.token_count
            }
            _ => 0,
        };
        Ok(SessionStats {
            name: self.name.clone(),
            revision: self.revision,
            token_count,
            closed: self.closed,
        })
    }

    fn lock_store(&self) -> std::sync::MutexGuard<'_, SessionStoreState> {
        self.tokenizer
            .store
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    fn require_open(&self) -> Result<()> {
        if self.closed {
            Err(Error::new(
                ErrorCode::InvalidArgument,
                format!("session {:?} is closed", self.name),
            ))
        } else {
            Ok(())
        }
    }

    fn release_lease(&self) {
        self.lock_store().leased.remove(&self.name);
    }
}

impl Drop for Session {
    fn drop(&mut self) {
        if !self.closed {
            self.release_lease();
            self.closed = true;
        }
    }
}

fn validate_name(name: &str) -> Result<()> {
    if name.is_empty() || name.len() > 1024 || name.contains('\0') {
        Err(Error::new(
            ErrorCode::InvalidArgument,
            "session name must contain 1..=1024 non-NUL UTF-8 bytes",
        ))
    } else {
        Ok(())
    }
}

fn session_facts(tokenizer: &TokenizerInner, path: &str, input_bytes: usize) -> ExecutionFacts {
    ExecutionFacts {
        backend: if tokenizer.plan.backends.contains(&Backend::FastCpu) {
            Backend::FastCpu
        } else {
            Backend::HuggingFace
        },
        path: path.to_owned(),
        source: Some("native_session".to_owned()),
        input_bytes: input_bytes as u64,
        certification: tokenizer.plan.certification,
        reason: reason_for_repair_path(path),
    }
}

/// Why a session repair ran where it did.
///
/// The repair engine does not carry a ledger code the way the request
/// router does; it names its outcome in the path itself, and each name
/// has exactly one meaning. `engine_guard` is the `R_INPUT_GUARD_ROUTED`
/// stage of that name in `docs/contracts/routing.md` Section 5.2, and
/// both full re-encodes that follow an unusable cut are that section's
/// `R_SESSION_NO_SAFE_CUT`. An accelerated or repaired path took the
/// admitted route and has nothing to record.
fn reason_for_repair_path(path: &str) -> Option<ReasonCode> {
    match path {
        "hf_full_engine_guard" => Some(ReasonCode::InputGuardRouted),
        "hf_full_no_safe_cut" | "hf_full_window_covers_all" => Some(ReasonCode::SessionNoSafeCut),
        // The stored tail did not describe itself consistently, so the
        // accumulated text was re-encoded from the reference. Registered
        // in the frozen namespace as `R_INVALID_PRIOR_STATE` in 0.2.7;
        // it used to travel as an `Other` string that appeared in
        // neither face's vocabulary.
        "hf_full_invalid_prior_state" => Some(ReasonCode::InvalidPriorState),
        _ => None,
    }
}

fn backend_for_path(path: &str) -> Backend {
    if path.contains("gpu") {
        Backend::Gpu
    } else if path.contains("hf") || path.contains("fallback") {
        Backend::HuggingFace
    } else {
        Backend::FastCpu
    }
}

#[allow(dead_code)]
fn _assert_session_send() {
    fn assert_send<T: Send>() {}
    assert_send::<Session>();
    let _ = Certification::Reference;
}

#[cfg(all(test, feature = "sqlite", unix))]
mod tests {
    use super::*;
    use std::fs;
    use std::os::unix::fs::PermissionsExt;
    use std::path::PathBuf;
    use toktier_routing_core::BACKEND_REFERENCE;

    const TOKENIZER_JSON: &[u8] = br#"{"version":"1.0","truncation":null,"padding":null,"added_tokens":[],"normalizer":null,"pre_tokenizer":null,"post_processor":null,"decoder":null,"model":{"type":"BPE","dropout":null,"unk_token":null,"continuing_subword_prefix":null,"end_of_word_suffix":null,"fuse_unk":false,"byte_fallback":false,"ignore_merges":false,"vocab":{"a":0},"merges":[]}}"#;

    fn reference_router() -> Arc<NativeRouter> {
        let reference = Arc::new(ReferenceEngine::from_bytes(TOKENIZER_JSON).unwrap());
        Arc::new(
            NativeRouter::new(
                vec![BACKEND_REFERENCE.to_owned()],
                vec![0],
                reference,
                None,
                false,
                None,
                false,
                true,
            )
            .unwrap(),
        )
    }

    fn permission_bits(path: &Path) -> u32 {
        fs::metadata(path).unwrap().permissions().mode() & 0o777
    }

    #[test]
    fn fresh_rust_store_home_and_database_are_owner_only() {
        let temporary = tempfile::tempdir().unwrap();
        let existing = temporary.path().join("caller-owned");
        fs::create_dir(&existing).unwrap();
        fs::set_permissions(&existing, fs::Permissions::from_mode(0o750)).unwrap();
        ensure_store_home(&existing).unwrap();
        assert_eq!(permission_bits(&existing), 0o750);

        let store_home = temporary.path().join("runtime-home/sessions");
        let database_path = store_home.join("test.sqlite3");
        let _state =
            SessionStoreState::new(reference_router(), [7; 32], 0, Some(&database_path), false)
                .unwrap();

        assert_eq!(permission_bits(&store_home), 0o700);
        assert_eq!(permission_bits(&database_path), 0o600);
        for suffix in ["-wal", "-shm"] {
            let mut sidecar = database_path.as_os_str().to_os_string();
            sidecar.push(suffix);
            let sidecar = PathBuf::from(sidecar);
            if sidecar.exists() {
                assert_eq!(permission_bits(&sidecar), 0o600);
            }
        }
    }

    /// The repair engine names its outcome in the path, so this crate's
    /// reading of it has to stay tied to the paths the engine emits: the
    /// two full re-encodes that follow an unusable cut, the guard route,
    /// the one outcome with no frozen code, and the repaired paths that
    /// have nothing to explain.
    #[test]
    fn a_repair_path_reads_as_the_reason_the_engine_meant() {
        assert_eq!(
            reason_for_repair_path("hf_full_engine_guard"),
            Some(ReasonCode::InputGuardRouted)
        );
        for path in ["hf_full_no_safe_cut", "hf_full_window_covers_all"] {
            assert_eq!(
                reason_for_repair_path(path),
                Some(ReasonCode::SessionNoSafeCut),
                "for {path}"
            );
        }
        assert_eq!(
            reason_for_repair_path("hf_full_invalid_prior_state"),
            Some(ReasonCode::InvalidPriorState)
        );
        for path in [
            "gigatoken_repair",
            "gigatoken_repair_noop",
            "accelerated_with_reconstructed_spans",
            "accelerated_with_lazy_span_checkpoints",
        ] {
            assert_eq!(reason_for_repair_path(path), None, "for {path}");
        }
    }
}