synaps-core 0.11.1

Foundation types, config, session, auth, protocol — leaf crate for agent-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
//! Typed named reasoning level — single source of truth across providers.
//!
//! `ReasoningLevel` is a closed enum with exact parsing, display, and explicit
//! conversion to legacy Anthropic token budgets. It lives in `synaps-core` so
//! every crate (engine, TUI) can depend on the same type without circular deps.
//!
//! ## Invariants
//! - `Max` must never alias `XHigh`.
//! - `Ultra` must never map through a numeric bucket.
//! - `Off` and `Adaptive` are distinct.
//! - Unknown strings from external sources parse to `None` / are rejected at
//!   the call-site, not silently coerced.

use std::fmt;

/// A named reasoning / thinking depth level.
///
/// Ordering is from least to most intensive; `Off` < `Adaptive` < `Low` < … < `Ultra`.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ReasoningLevel {
    /// No reasoning / thinking output.
    Off,
    /// Model decides reasoning depth automatically (Anthropic adaptive path).
    Adaptive,
    /// Low reasoning budget.
    Low,
    /// Medium reasoning budget.
    #[default]
    Medium,
    /// High reasoning budget.
    High,
    /// Extra-high reasoning budget (above high, below max).
    XHigh,
    /// Maximum named level (below ultra).
    Max,
    /// Ultra — highest intensity, only a subset of Codex models supports this.
    Ultra,
    /// Anthropic-only logical xhigh plus standing workflow mode.
    UltraCode,
}

impl ReasoningLevel {
    /// Parse a level from its canonical logical/config string.
    ///
    /// Returns `None` for unknown strings; callers must not silently fallback.
    pub fn parse(s: &str) -> Option<Self> {
        match s.trim().to_ascii_lowercase().as_str() {
            "off" | "none" => Some(Self::Off),
            "adaptive" => Some(Self::Adaptive),
            "low" => Some(Self::Low),
            "medium" | "med" => Some(Self::Medium),
            "high" => Some(Self::High),
            "xhigh" | "x-high" | "x_high" => Some(Self::XHigh),
            "max" => Some(Self::Max),
            "ultra" => Some(Self::Ultra),
            "ultracode" => Some(Self::UltraCode),
            _ => None,
        }
    }

    /// The canonical logical/config representation.
    ///
    /// Provider request planners own wire conversion. In particular, Codex
    /// logical Ultra uses the exact catalog's multi-agent wire effort (e.g.
    /// Astra `xhigh`, Sol `max`) plus foreground proactive mode.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Off => "off",
            Self::Adaptive => "adaptive",
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
            Self::XHigh => "xhigh",
            Self::Max => "max",
            Self::Ultra => "ultra",
            Self::UltraCode => "ultracode",
        }
    }

    /// Convert to the legacy Anthropic token budget used by the enabled+budget_tokens
    /// request shape. Returns `None` for levels that have no meaningful numeric
    /// mapping (Max and Ultra are Codex execution modes, not numeric budgets).
    pub fn to_legacy_budget(self) -> Option<u32> {
        match self {
            Self::Off | Self::Adaptive => Some(0),
            Self::Low => Some(2048),
            Self::Medium => Some(4096),
            Self::High => Some(16384),
            Self::XHigh => Some(32768),
            Self::Max | Self::Ultra | Self::UltraCode => None,
        }
    }

    /// Build from a legacy token budget value (Anthropic path).
    ///
    /// `0` → `Adaptive`, positive values → their tier. Does not produce `Max`
    /// or `Ultra` — those are provider-named levels, never inferred from a budget.
    pub fn from_legacy_budget(budget: u32) -> Self {
        match budget {
            0 => Self::Adaptive,
            1..=2048 => Self::Low,
            2049..=4096 => Self::Medium,
            4097..=16384 => Self::High,
            _ => Self::XHigh,
        }
    }

    /// All levels that are strictly above XHigh (require Codex-specific support).
    pub fn requires_codex_support(self) -> bool {
        matches!(self, Self::Ultra)
    }

    /// Whether this level requires exact Anthropic mode capability evidence.
    pub fn requires_anthropic_support(self) -> bool {
        matches!(self, Self::Max | Self::UltraCode)
    }
}

impl fmt::Display for ReasoningLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Parse the `thinking` config key into either a named level or a raw numeric budget.
///
/// This is the single parse path for config files, `/thinking` commands, and
/// session resumption. Named levels win over numeric-only parsing; numeric values
/// that don't match a known level are treated as raw budget tokens.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ThinkingSpec {
    /// A recognized named level.
    Named(ReasoningLevel),
    /// A raw numeric budget (legacy Anthropic path, user-specified token count).
    Budget(u32),
}

impl ThinkingSpec {
    /// Parse from a string (config value or command argument).
    ///
    /// Returns `None` for strings that are neither a known name nor a non-negative integer.
    pub fn parse(s: &str) -> Option<Self> {
        // Named levels take priority.
        if let Some(level) = ReasoningLevel::parse(s) {
            return Some(Self::Named(level));
        }
        // Raw numeric budget (any non-negative integer).
        if let Ok(n) = s.trim().parse::<u32>() {
            return Some(Self::Budget(n));
        }
        None
    }

    /// The canonical level this spec resolves to, if deterministic.
    /// `Budget` values are bucketized via `ReasoningLevel::from_legacy_budget`.
    pub fn to_level(&self) -> ReasoningLevel {
        match self {
            Self::Named(l) => *l,
            Self::Budget(n) => ReasoningLevel::from_legacy_budget(*n),
        }
    }

    /// The numeric budget this spec resolves to for Anthropic requests.
    /// Named Max/Ultra return `None` because they have no valid numeric budget.
    pub fn to_budget(&self) -> Option<u32> {
        match self {
            Self::Named(l) => l.to_legacy_budget(),
            Self::Budget(n) => Some(*n),
        }
    }
}

/// Named worker effort levels shared by config and tool arguments. Execution
/// modes (ultra/ultracode), disabling thinking and numeric budgets are not
/// subagent effort overrides.
pub fn parse_subagent_thinking(value: &str) -> Option<ReasoningLevel> {
    match ReasoningLevel::parse(value)? {
        level @ (ReasoningLevel::Low | ReasoningLevel::Medium | ReasoningLevel::High
        | ReasoningLevel::XHigh | ReasoningLevel::Max) => Some(level),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ultracode_is_a_distinct_canonical_non_numeric_level() {
        let level = ReasoningLevel::parse("ultracode").expect("canonical ultracode");
        assert_eq!(level, ReasoningLevel::UltraCode);
        assert_eq!(level.to_string(), "ultracode");
        assert_ne!(level, ReasoningLevel::Ultra);
        assert_ne!(level, ReasoningLevel::Max);
        assert_ne!(level, ReasoningLevel::XHigh);
        assert_eq!(level.to_legacy_budget(), None);
        assert!(!level.requires_codex_support());
        assert!(level.requires_anthropic_support());
    }

    // ── Parse / round-trip ────────────────────────────────────────────────────

    #[test]
    fn all_canonical_strings_parse() {
        for (s, expected) in [
            ("off", ReasoningLevel::Off),
            ("none", ReasoningLevel::Off),
            ("adaptive", ReasoningLevel::Adaptive),
            ("low", ReasoningLevel::Low),
            ("medium", ReasoningLevel::Medium),
            ("med", ReasoningLevel::Medium),
            ("high", ReasoningLevel::High),
            ("xhigh", ReasoningLevel::XHigh),
            ("max", ReasoningLevel::Max),
            ("ultra", ReasoningLevel::Ultra),
            ("ultracode", ReasoningLevel::UltraCode),
        ] {
            assert_eq!(
                ReasoningLevel::parse(s),
                Some(expected),
                "failed to parse {s:?}"
            );
        }
    }

    #[test]
    fn case_insensitive_parse() {
        assert_eq!(ReasoningLevel::parse("MAX"), Some(ReasoningLevel::Max));
        assert_eq!(ReasoningLevel::parse("Ultra"), Some(ReasoningLevel::Ultra));
        assert_eq!(ReasoningLevel::parse("XHIGH"), Some(ReasoningLevel::XHigh));
        assert_eq!(ReasoningLevel::parse("  Low  "), Some(ReasoningLevel::Low));
    }

    #[test]
    fn unknown_strings_return_none() {
        assert_eq!(ReasoningLevel::parse(""), None);
        assert_eq!(ReasoningLevel::parse("bogus"), None);
        assert_eq!(ReasoningLevel::parse("maximum"), None);
        assert_eq!(ReasoningLevel::parse("hyper"), None);
        assert_eq!(ReasoningLevel::parse("xhigh+"), None);
    }

    #[test]
    fn as_str_round_trips() {
        for level in [
            ReasoningLevel::Off,
            ReasoningLevel::Adaptive,
            ReasoningLevel::Low,
            ReasoningLevel::Medium,
            ReasoningLevel::High,
            ReasoningLevel::XHigh,
            ReasoningLevel::Max,
            ReasoningLevel::Ultra,
            ReasoningLevel::UltraCode,
        ] {
            let s = level.as_str();
            assert_eq!(
                ReasoningLevel::parse(s),
                Some(level),
                "as_str() → parse() failed for {:?}",
                level
            );
        }
    }

    #[test]
    fn display_matches_as_str() {
        assert_eq!(ReasoningLevel::Max.to_string(), "max");
        assert_eq!(ReasoningLevel::Ultra.to_string(), "ultra");
        assert_eq!(ReasoningLevel::XHigh.to_string(), "xhigh");
    }

    // ── Max must never alias XHigh ─────────────────────────────────────────────

    #[test]
    fn max_is_distinct_from_xhigh() {
        assert_ne!(ReasoningLevel::Max, ReasoningLevel::XHigh);
        assert_ne!(ReasoningLevel::Max.as_str(), ReasoningLevel::XHigh.as_str());
    }

    // ── Ultra must not map through a numeric budget bucket ────────────────────

    #[test]
    fn ultra_has_no_legacy_budget() {
        assert_eq!(ReasoningLevel::Ultra.to_legacy_budget(), None);
    }

    #[test]
    fn max_has_no_legacy_budget() {
        assert_eq!(ReasoningLevel::Max.to_legacy_budget(), None);
    }

    // ── Legacy budget conversion ──────────────────────────────────────────────

    #[test]
    fn known_legacy_budgets_convert_correctly() {
        assert_eq!(ReasoningLevel::Off.to_legacy_budget(), Some(0));
        assert_eq!(ReasoningLevel::Adaptive.to_legacy_budget(), Some(0));
        assert_eq!(ReasoningLevel::Low.to_legacy_budget(), Some(2048));
        assert_eq!(ReasoningLevel::Medium.to_legacy_budget(), Some(4096));
        assert_eq!(ReasoningLevel::High.to_legacy_budget(), Some(16384));
        assert_eq!(ReasoningLevel::XHigh.to_legacy_budget(), Some(32768));
    }

    #[test]
    fn from_legacy_budget_never_returns_max_or_ultra() {
        for budget in [0u32, 2048, 4096, 16384, 32768, 65536, u32::MAX] {
            let level = ReasoningLevel::from_legacy_budget(budget);
            assert!(
                !matches!(level, ReasoningLevel::Max | ReasoningLevel::Ultra),
                "from_legacy_budget({budget}) returned {level:?} which must not be Max/Ultra"
            );
        }
    }

    #[test]
    fn from_legacy_budget_round_trips_for_known_values() {
        for (budget, expected) in [
            (0u32, ReasoningLevel::Adaptive),
            (2048, ReasoningLevel::Low),
            (4096, ReasoningLevel::Medium),
            (16384, ReasoningLevel::High),
            (32768, ReasoningLevel::XHigh),
        ] {
            assert_eq!(
                ReasoningLevel::from_legacy_budget(budget),
                expected,
                "budget {budget}"
            );
        }
    }

    // ── Ordering ──────────────────────────────────────────────────────────────

    #[test]
    fn ordering_is_intensity() {
        let levels = [
            ReasoningLevel::Off,
            ReasoningLevel::Adaptive,
            ReasoningLevel::Low,
            ReasoningLevel::Medium,
            ReasoningLevel::High,
            ReasoningLevel::XHigh,
            ReasoningLevel::Max,
            ReasoningLevel::Ultra,
        ];
        for w in levels.windows(2) {
            assert!(w[0] < w[1], "{:?} should be less than {:?}", w[0], w[1]);
        }
    }

    // ── requires_codex_support ────────────────────────────────────────────────

    #[test]
    fn only_ultra_requires_categorically_codex_support() {
        assert!(!ReasoningLevel::Max.requires_codex_support());
        assert!(ReasoningLevel::Ultra.requires_codex_support());
        assert!(!ReasoningLevel::UltraCode.requires_codex_support());
        assert!(!ReasoningLevel::XHigh.requires_codex_support());
        assert!(!ReasoningLevel::High.requires_codex_support());
        assert!(!ReasoningLevel::Adaptive.requires_codex_support());
    }

    // ── ThinkingSpec ──────────────────────────────────────────────────────────

    #[test]
    fn thinking_spec_named_levels_parse() {
        assert_eq!(
            ThinkingSpec::parse("max"),
            Some(ThinkingSpec::Named(ReasoningLevel::Max))
        );
        assert_eq!(
            ThinkingSpec::parse("ultra"),
            Some(ThinkingSpec::Named(ReasoningLevel::Ultra))
        );
        assert_eq!(
            ThinkingSpec::parse("xhigh"),
            Some(ThinkingSpec::Named(ReasoningLevel::XHigh))
        );
    }

    #[test]
    fn thinking_spec_numeric_budget_parse() {
        assert_eq!(
            ThinkingSpec::parse("8192"),
            Some(ThinkingSpec::Budget(8192))
        );
        assert_eq!(ThinkingSpec::parse("0"), Some(ThinkingSpec::Budget(0)));
    }

    #[test]
    fn thinking_spec_parse_invalid() {
        assert_eq!(ThinkingSpec::parse("bogus"), None);
        assert_eq!(ThinkingSpec::parse(""), None);
    }

    #[test]
    fn thinking_spec_max_has_no_budget() {
        let spec = ThinkingSpec::Named(ReasoningLevel::Max);
        assert_eq!(spec.to_budget(), None);
        assert_eq!(spec.to_level(), ReasoningLevel::Max);
    }

    #[test]
    fn thinking_spec_budget_round_trips() {
        let spec = ThinkingSpec::Budget(8192);
        assert_eq!(spec.to_budget(), Some(8192));
        // 8192 is in the high tier (4097–16384)
        assert_eq!(spec.to_level(), ReasoningLevel::High);
    }
}