| 1 | pub mod context_reference; |
| 2 | pub mod fragments; |
| 3 | pub mod prefix_cache; |
| 4 | pub mod request; |
| 5 | pub mod role; |
| 6 | pub mod secret_eq; |
| 7 | pub mod tool_parser; |
| 8 | |
| 9 | pub use context_reference::{ |
| 10 | ContextReference, ContextReferenceKind, ContextReferenceSource, MediaAttachmentReference, |
| 11 | media_attachment_references, |
| 12 | }; |
| 13 | |
| 14 | use codewhale_config::{ConfigToml, ProviderKind}; |
| 15 | use codewhale_state::StateStore; |
| 16 | |
| 17 | /// Compatibility configuration. |
| 18 | /// |
| 19 | /// Conversation execution and durable history belong to the held canonical |
| 20 | /// RuntimeThreadManager/Engine. Legacy State data is retained for read-only |
| 21 | /// recovery and bound alias publication, never as a second transcript writer. |
| 22 | pub struct Runtime { |
| 23 | pub config: ConfigToml, |
| 24 | state: StateStore, |
| 25 | } |
| 26 | |
| 27 | impl Runtime { |
| 28 | pub fn new(config: ConfigToml, state: StateStore) -> Self { |
| 29 | Self { config, state } |
| 30 | } |
| 31 | |
| 32 | /// Existing compatibility data and receipts; no transcript controller. |
| 33 | pub fn state_store(&self) -> &StateStore { |
| 34 | &self.state |
| 35 | } |
| 36 | |
| 37 | pub fn update_config(&mut self, config: ConfigToml) { |
| 38 | self.config = config; |
| 39 | } |
| 40 | |
| 41 | /// Returns the default model provider from the resolved configuration. |
| 42 | pub fn provider_default(&self) -> ProviderKind { |
| 43 | self.config.provider |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// How many leading entries of `offered` repeat the end of `persisted`: the |
| 48 | /// longest suffix of `persisted` that is also a prefix of `offered`. |
| 49 | /// Longest persisted suffix repeated by an offered history prefix. Linear |
| 50 | /// work preserves genuine repeated messages without an unbounded nested scan. |
| 51 | pub fn persisted_overlap<T: PartialEq>(persisted: &[T], offered: &[T]) -> usize { |
| 52 | if offered.is_empty() { |
| 53 | return 0; |
| 54 | } |
| 55 | let mut failure = vec![0; offered.len()]; |
| 56 | for index in 1..offered.len() { |
| 57 | let mut matched = failure[index - 1]; |
| 58 | while matched > 0 && offered[index] != offered[matched] { |
| 59 | matched = failure[matched - 1]; |
| 60 | } |
| 61 | if offered[index] == offered[matched] { |
| 62 | matched += 1; |
| 63 | } |
| 64 | failure[index] = matched; |
| 65 | } |
| 66 | let mut matched = 0; |
| 67 | for item in persisted { |
| 68 | if matched == offered.len() { |
| 69 | matched = failure[matched - 1]; |
| 70 | } |
| 71 | while matched > 0 && *item != offered[matched] { |
| 72 | matched = failure[matched - 1]; |
| 73 | } |
| 74 | if *item == offered[matched] { |
| 75 | matched += 1; |
| 76 | } |
| 77 | } |
| 78 | matched |
| 79 | } |
| 80 | |
| 81 | #[cfg(test)] |
| 82 | mod tests { |
| 83 | use super::*; |
| 84 | use uuid::Uuid; |
| 85 | |
| 86 | fn temp_core_state(name: &str) -> StateStore { |
| 87 | let dir = |
| 88 | std::env::temp_dir().join(format!("codewhale-core-{name}-{}", Uuid::new_v4().simple())); |
| 89 | std::fs::create_dir_all(&dir).expect("create temp state dir"); |
| 90 | StateStore::open(Some(dir.join("state.db"))).expect("open state store") |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn runtime_bookkeeping_keeps_the_legacy_store_read_only() { |
| 95 | let store = temp_core_state("read-only-runtime"); |
| 96 | let before = store.list_threads(Default::default()).unwrap(); |
| 97 | let mut runtime = Runtime::new(ConfigToml::default(), store); |
| 98 | runtime.update_config(ConfigToml::default()); |
| 99 | assert_eq!( |
| 100 | runtime |
| 101 | .state_store() |
| 102 | .list_threads(Default::default()) |
| 103 | .unwrap() |
| 104 | .len(), |
| 105 | before.len() |
| 106 | ); |
| 107 | let reopened = Runtime::new(runtime.config.clone(), runtime.state_store().clone()); |
| 108 | assert!( |
| 109 | reopened |
| 110 | .state_store() |
| 111 | .list_threads(Default::default()) |
| 112 | .unwrap() |
| 113 | .is_empty() |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 |