| 1 | //! Recursive Language Model presentation and persistent Python context. |
| 2 | //! |
| 3 | //! The serving Engine captures one call-local route, policy, service and |
| 4 | //! cancellation receipt. `llm_query` and `rlm_query` submit bounded invocations |
| 5 | //! to the same `Engine::run_turn`; this module owns no provider/code loop. |
| 6 | //! |
| 7 | //! Long input stays in `_context` (`_ctx` / `content` compatibility aliases). |
| 8 | //! Core's session keeps initial metadata plus every code/result round until |
| 9 | //! FINAL, the strict partial-response refusal or the bounded deadline/cap. |
| 10 | //! Python kernels retain variables and pipes, never their borrowed dispatcher |
| 11 | //! or caller. The persistent `rlm` action surface is caller-session scoped; |
| 12 | //! `share_session=true` explicitly refuses. No HTTP sidecar is created. |
| 13 | |
| 14 | #[cfg(test)] |
| 15 | use codewhale_models::Usage; |
| 16 | |
| 17 | pub mod bridge; |
| 18 | pub(crate) mod kernel; |
| 19 | pub mod prompt; |
| 20 | pub mod session; |
| 21 | pub mod turn; |
| 22 | |
| 23 | pub use bridge::RlmBridge; |
| 24 | pub use prompt::rlm_system_prompt; |
| 25 | pub use turn::{RlmTermination, RlmTurnResult}; |
| 26 | |
| 27 | #[cfg(test)] |
| 28 | mod tests { |
| 29 | use super::*; |
| 30 | |
| 31 | #[test] |
| 32 | fn add_usage_with_prompt_cache_preserves_cache_counts() { |
| 33 | let mut total = Usage { |
| 34 | input_tokens: 100, |
| 35 | output_tokens: 10, |
| 36 | prompt_cache_hit_tokens: Some(80), |
| 37 | prompt_cache_miss_tokens: Some(20), |
| 38 | ..Usage::default() |
| 39 | }; |
| 40 | let delta = Usage { |
| 41 | input_tokens: 50, |
| 42 | output_tokens: 5, |
| 43 | prompt_cache_hit_tokens: Some(30), |
| 44 | prompt_cache_miss_tokens: Some(20), |
| 45 | reasoning_tokens: Some(4), |
| 46 | reasoning_replay_tokens: Some(3), |
| 47 | server_tool_use: Some(codewhale_models::ServerToolUsage { |
| 48 | code_execution_requests: Some(2), |
| 49 | tool_search_requests: Some(1), |
| 50 | ..Default::default() |
| 51 | }), |
| 52 | ..Usage::default() |
| 53 | }; |
| 54 | |
| 55 | crate::core::turn::add_usage_to(&mut total, &delta); |
| 56 | |
| 57 | assert_eq!(total.input_tokens, 150); |
| 58 | assert_eq!(total.output_tokens, 15); |
| 59 | assert_eq!(total.prompt_cache_hit_tokens, Some(110)); |
| 60 | assert_eq!(total.prompt_cache_miss_tokens, Some(40)); |
| 61 | assert_eq!(total.reasoning_tokens, Some(4)); |
| 62 | assert_eq!(total.reasoning_replay_tokens, Some(3)); |
| 63 | assert_eq!( |
| 64 | total.server_tool_use, |
| 65 | Some(codewhale_models::ServerToolUsage { |
| 66 | code_execution_requests: Some(2), |
| 67 | tool_search_requests: Some(1), |
| 68 | ..Default::default() |
| 69 | }) |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 |