| 1 | use serde::Deserialize; |
| 2 | |
| 3 | /// Absolute per-scope default: 100k estimated tokens. |
| 4 | pub const DEFAULT_SCOPE_BUDGET_TOKENS: usize = 100_000; |
| 5 | /// Per-scope default as a share of the usable window. |
| 6 | pub const DEFAULT_SCOPE_BUDGET_PERCENT: u8 = 25; |
| 7 | /// Default ceiling for the whole static prefix, as a share of the usable window. |
| 8 | pub const DEFAULT_STATIC_PREFIX_GUARD_PERCENT: u8 = 80; |
| 9 | /// Hard memory-safety ceiling for one instruction file (8 MiB). |
| 10 | pub const DEFAULT_FILE_MAX_BYTES: u64 = 8 * 1024 * 1024; |
| 11 | const MIN_STATIC_RESIDUAL_PERCENT: u8 = 10; |
| 12 | |
| 13 | /// What to do when an instruction scope does not fit. |
| 14 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, Deserialize, Default)] |
| 15 | #[serde(rename_all = "snake_case")] |
| 16 | pub enum InstructionBudgetPolicy { |
| 17 | /// Trim the scope and continue with a warning (default). |
| 18 | #[default] |
| 19 | Trim, |
| 20 | /// Stop the session instead of trimming or running an unsatisfiable guard. |
| 21 | Refuse, |
| 22 | } |
| 23 | |
| 24 | impl InstructionBudgetPolicy { |
| 25 | #[must_use] |
| 26 | pub fn as_str(self) -> &'static str { |
| 27 | match self { |
| 28 | Self::Trim => "trim", |
| 29 | Self::Refuse => "refuse", |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// The `[instruction_budget]` config table. |
| 35 | /// |
| 36 | /// Every field is optional so the table only states what the user changed and |
| 37 | /// the defaults stay in one place. `deny_unknown_fields` makes a typo inside |
| 38 | /// the table fail loudly — the top-level config is deliberately tolerant of |
| 39 | /// unknown *tables* (#6516), so silently accepting `global_budget_token` |
| 40 | /// would hide the mistake forever. |
| 41 | #[derive(Debug, Clone, Default, PartialEq, Eq, Hash, serde::Serialize, Deserialize)] |
| 42 | #[serde(deny_unknown_fields)] |
| 43 | pub struct InstructionBudgetConfig { |
| 44 | /// Absolute ceiling for the global scope, in estimated tokens. |
| 45 | pub global_budget_tokens: Option<usize>, |
| 46 | /// Absolute ceiling for the project scope, in estimated tokens. |
| 47 | pub project_budget_tokens: Option<usize>, |
| 48 | /// Global scope ceiling as a share of the usable window (1–100). |
| 49 | pub global_budget_percent: Option<u8>, |
| 50 | /// Project scope ceiling as a share of the usable window (1–100). |
| 51 | pub project_budget_percent: Option<u8>, |
| 52 | /// Ceiling for the whole static prefix as a share of the usable window. |
| 53 | pub static_prefix_guard_percent: Option<u8>, |
| 54 | /// Hard per-file ceiling in bytes; a larger file is skipped, not trimmed. |
| 55 | pub file_max_bytes: Option<u64>, |
| 56 | /// `trim` (default) or `refuse`. |
| 57 | pub policy: Option<InstructionBudgetPolicy>, |
| 58 | /// Read `CLAUDE.md` when no `AGENTS.md`/codewhale file is present. |
| 59 | pub claude_fallback: Option<bool>, |
| 60 | } |
| 61 | |
| 62 | impl InstructionBudgetConfig { |
| 63 | /// Reject values that cannot mean anything. |
| 64 | /// |
| 65 | /// Called while config is applied, so a broken table stops the launch |
| 66 | /// instead of silently producing a nonsensical budget. |
| 67 | pub fn validate(&self) -> Result<(), String> { |
| 68 | for (name, value) in [ |
| 69 | ("global_budget_tokens", self.global_budget_tokens), |
| 70 | ("project_budget_tokens", self.project_budget_tokens), |
| 71 | ] { |
| 72 | if value == Some(0) { |
| 73 | return Err(format!( |
| 74 | "instruction_budget.{name} must be positive (0 is not an unlimited budget)" |
| 75 | )); |
| 76 | } |
| 77 | } |
| 78 | if self |
| 79 | .file_max_bytes |
| 80 | .is_some_and(|value| value == 0 || value > DEFAULT_FILE_MAX_BYTES) |
| 81 | { |
| 82 | return Err( |
| 83 | "instruction_budget.file_max_bytes must be between 1 and 8388608 (the hard ceiling cannot be raised)" |
| 84 | .to_string(), |
| 85 | ); |
| 86 | } |
| 87 | for (name, value) in [ |
| 88 | ("global_budget_percent", self.global_budget_percent), |
| 89 | ("project_budget_percent", self.project_budget_percent), |
| 90 | ( |
| 91 | "static_prefix_guard_percent", |
| 92 | self.static_prefix_guard_percent, |
| 93 | ), |
| 94 | ] { |
| 95 | if let Some(value) = value |
| 96 | && !(1..=100).contains(&value) |
| 97 | { |
| 98 | return Err(format!( |
| 99 | "instruction_budget.{name} must be between 1 and 100 (got {value})" |
| 100 | )); |
| 101 | } |
| 102 | } |
| 103 | let global_percent = self |
| 104 | .global_budget_percent |
| 105 | .unwrap_or(DEFAULT_SCOPE_BUDGET_PERCENT); |
| 106 | let project_percent = self |
| 107 | .project_budget_percent |
| 108 | .unwrap_or(DEFAULT_SCOPE_BUDGET_PERCENT); |
| 109 | let guard = self |
| 110 | .static_prefix_guard_percent |
| 111 | .unwrap_or(DEFAULT_STATIC_PREFIX_GUARD_PERCENT); |
| 112 | if guard < global_percent.saturating_add(project_percent) { |
| 113 | return Err(format!( |
| 114 | "instruction_budget.static_prefix_guard_percent ({guard}) must be at least the sum of the scope percents ({global_percent} + {project_percent})" |
| 115 | )); |
| 116 | } |
| 117 | if guard - global_percent.saturating_add(project_percent) < MIN_STATIC_RESIDUAL_PERCENT { |
| 118 | return Err(format!( |
| 119 | "instruction_budget.static_prefix_guard_percent ({guard}) must leave at least {MIN_STATIC_RESIDUAL_PERCENT} points above the scope percents ({global_percent} + {project_percent}) for non-instruction static content" |
| 120 | )); |
| 121 | } |
| 122 | Ok(()) |
| 123 | } |
| 124 | } |
| 125 |