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
412
413
414
415
//! Curated list of Claude models known to work with this CLI.
//! Centralized so the settings dropdown, defaults, and subagent hints agree.

pub const KNOWN_MODELS: &[(&str, &str)] = &[
    ("claude-sonnet-4-6", "Sonnet 4.6 — balanced (default)"),
    ("claude-fable-5-1", "Fable 5.1"),
    ("claude-fable-5", "Fable 5"),
    ("claude-opus-5-5", "Opus 5.5 — most capable Opus"),
    ("claude-opus-4-7", "Opus 4.7"),
    ("claude-opus-4-6", "Opus 4.6 — previous flagship"),
    ("claude-haiku-4-5-20251001", "Haiku 4.5 — fast"),
];

pub fn default_model() -> &'static str {
    KNOWN_MODELS[0].0
}

/// Returns true for models that support (and require) adaptive thinking:
/// `thinking: {type: "adaptive"}` with NO `budget_tokens` field.
///
/// Per Anthropic's docs as of 2026-04: Opus 4.6+/Sonnet 4.6+ deprecated
/// the fixed-budget `{type: "enabled", budget_tokens: N}` shape. On those
/// models the deprecated shape is silently accepted but returns no
/// thinking content (observed S172 on Opus 4.7). Older models (Opus 4.5,
/// Sonnet 4.5, Haiku, Opus 3.x) still use the enabled+budget shape.
///
/// Adaptive thinking also auto-enables interleaved thinking — no beta
/// header required.
/// Whether an Anthropic model supports tool search, i.e. `defer_loading` tool
/// definitions expanded by `tool_reference` blocks. Anthropic's compatibility
/// table lists Haiku 4.5, Sonnet 4.5+, Opus 4.5+ and every 5.x family; Opus 4.1
/// and earlier (and Sonnet 4 / the 3.x line) do not support it. Unknown future
/// `claude-*` ids are assumed to support it.
pub fn model_supports_tool_search(model: &str) -> bool {
    let m = model.to_ascii_lowercase();
    let m = m.strip_prefix("anthropic/").unwrap_or(&m);
    if !m.starts_with("claude-") {
        return false;
    }
    const UNSUPPORTED: &[&str] = &[
        "claude-3",
        "claude-opus-4-0",
        "claude-opus-4-1",
        "claude-opus-4-2025",
        "claude-sonnet-4-0",
        "claude-sonnet-4-2025",
        "claude-instant",
    ];
    !UNSUPPORTED.iter().any(|p| m.starts_with(p))
}

pub fn model_supports_adaptive_thinking(model: &str) -> bool {
    let m = model.to_ascii_lowercase();
    // Only Opus 4.7+ REQUIRES adaptive. Opus 4.6 supports it optionally
    // but works fine with enabled+budget_tokens (and doesn't support the
    // xhigh effort level that adaptive users expect). Keep 4.6 on the
    // legacy path to avoid effort-mapping headaches.
    if m.contains("opus-4-7") || m.contains("opus-4-8") || m.contains("opus-4-9") {
        return true;
    }
    if m.contains("sonnet-4-7") || m.contains("sonnet-4-8") || m.contains("sonnet-4-9") {
        return true;
    }
    // 5.x and beyond — assume adaptive by default.
    if m.contains("opus-5")
        || m.contains("sonnet-5")
        || m.contains("haiku-5")
        || m.contains("fable-5")
    {
        return true;
    }
    false
}

/// Maps a SynapsCLI thinking level to an Anthropic `effort` value for models
/// that use adaptive thinking (Opus 4.6+/Sonnet 4.6+). Effort controls
/// thinking depth when `budget_tokens` is unavailable/deprecated.
///
/// "adaptive" as input means "let the model decide" — returns None so the
/// caller omits `output_config.effort` entirely.
pub fn effort_for_thinking_level(level: &str) -> Option<&'static str> {
    match level {
        "low" => Some("low"),
        "medium" | "med" => Some("medium"),
        "high" => Some("high"),
        "xhigh" => Some("xhigh"),
        "adaptive" => None, // model decides
        _ => Some("high"),  // safe default
    }
}

/// Maps a raw `thinking_budget` value to the user-facing level name.
///
/// `0` is the sentinel for "adaptive" (model decides). Positive values
/// bucket into the four fixed tiers. Single source of truth — consumed by
/// Runtime::thinking_level, the request builders in runtime/api.rs, and
/// the status display.
pub fn thinking_level_for_budget(budget: u32) -> &'static str {
    match budget {
        0 => "adaptive",
        1..=2048 => "low",
        2049..=4096 => "medium",
        4097..=16384 => "high",
        _ => "xhigh",
    }
}

/// Inverse of `thinking_level_for_budget` — maps a level name back to its
/// canonical budget. Returns None for unrecognized labels (callers should
/// leave the current budget untouched). Used by /resume to restore the
/// session's serialized thinking level.
pub fn budget_for_thinking_level(level: &str) -> Option<u32> {
    match level {
        "adaptive" | "off" => Some(0),
        "low" => Some(2048),
        "medium" => Some(4096),
        "high" => Some(16384),
        "xhigh" => Some(32768),
        _ => None,
    }
}

/// Default legacy-model thinking budget used when the "adaptive" sentinel
/// (0) leaks into the non-adaptive request path. Matches the "high" tier.
pub const DEFAULT_LEGACY_ADAPTIVE_FALLBACK: u32 = 16384;

/// Returns true for models that *support* opting into the 1M context window
/// via the `context-1m-2025-08-07` beta header.
///
/// Without that header, all current Claude models operate at the default
/// 200k window — including those documented as "1M tokens", because the
/// extended window is gated behind the beta opt-in. This matches Anthropic's
/// own claude-code logic (`modelSupports1M` in src/utils/context.ts).
pub fn model_supports_1m(model: &str) -> bool {
    let m = model.to_ascii_lowercase();
    // Opus 4.6+, Sonnet 4 family. Newer models (5.x) assumed supported.
    m.contains("opus-4-6")
        || m.contains("opus-4-7")
        || m.contains("opus-4-8")
        || m.contains("opus-4-9")
        || m.contains("sonnet-4")
        || m.contains("opus-5")
        || m.contains("sonnet-5")
        || m.contains("fable-5")
}

/// The extended context window a 1M-capable model opens ([`model_supports_1m`]).
pub const EXTENDED_CONTEXT_WINDOW: u64 = 1_000_000;

/// Whether the Messages API rejects a request whose input plus `max_tokens`
/// exceeds the model's context window.
///
/// Anthropic's context-window docs ("Context window overflow behavior",
/// <https://platform.claude.com/docs/en/build-with-claude/context-windows#context-window-overflow-behavior>):
/// on Claude 4.5 models and newer such a request is accepted, and generation
/// that reaches the window stops with `stop_reason:
/// "model_context_window_exceeded"`; earlier models return a validation
/// error instead (unless the `model-context-window-exceeded-2025-08-26` beta
/// is sent, which Synaps does not). Input alone over the window is a 400 on
/// every model. So `true` only for the pre-4.5 line: Claude 2/Instant, the
/// 3.x line, Opus 4 / 4.1 and Sonnet 4. Haiku 4.5, Sonnet/Opus 4.5 and every
/// later or unknown future `claude-*` id accept overflow, and so does any
/// non-Claude id: no route but the native Anthropic one sends an output cap.
pub fn model_rejects_output_overflow(model: &str) -> bool {
    let m = model.to_ascii_lowercase();
    let m = m.strip_prefix("anthropic/").unwrap_or(&m);
    const BEFORE_4_5: &[&str] = &[
        "claude-2",
        "claude-instant",
        "claude-3",
        "claude-opus-4-0",
        "claude-opus-4-1",
        "claude-opus-4-2025",
        "claude-sonnet-4-0",
        "claude-sonnet-4-2025",
    ];
    matches!(m, "claude-opus-4" | "claude-sonnet-4") || BEFORE_4_5.iter().any(|p| m.starts_with(p))
}

/// Returns the input context window size for a given model, in tokens.
/// Used as the denominator for the chatui context-usage bar and anywhere
/// else the client needs to know how much prompt the model will accept.
///
/// **All models default to 200k** — matching Anthropic's claude-code
/// behavior. Models that support a larger window (Opus 4.6+, Sonnet 4)
/// require explicit opt-in via the `context-1m-2025-08-07` beta header.
/// Without the header, the model operates in 200k mode (better inference
/// quality — context rot at long contexts is real).
///
/// Use [`model_supports_1m`] to check whether a model *can* opt into 1M;
/// the actual decision happens at request time based on the user's
/// `context_window` setting.
pub fn context_window_for_model(_model: &str) -> u64 {
    200_000
}

#[cfg(test)]
mod tests {
    #[test]
    fn tool_search_support_follows_anthropic_compatibility_table() {
        for m in [
            "claude-opus-5-5",
            "anthropic/claude-sonnet-5-5",
            "claude-fable-5-1",
            "claude-opus-4-5-20251101",
            "claude-sonnet-4-5-20250929",
            "claude-haiku-4-5-20251001",
            "claude-opus-4-8",
        ] {
            assert!(super::model_supports_tool_search(m), "{m}");
        }
        for m in [
            "claude-opus-4-1-20250805",
            "claude-opus-4-20250514",
            "claude-sonnet-4-20250514",
            "claude-3-7-sonnet-latest",
            "gpt-5.6-sol",
            "groq/llama-3.3-70b-versatile",
        ] {
            assert!(!super::model_supports_tool_search(m), "{m}");
        }
    }

    use super::*;

    #[test]
    fn default_model_is_first_entry() {
        assert_eq!(default_model(), KNOWN_MODELS[0].0);
    }

    #[test]
    fn known_models_has_expected_ids() {
        let ids: Vec<&str> = KNOWN_MODELS.iter().map(|(id, _)| *id).collect();
        assert!(ids.contains(&"claude-fable-5-1"));
        assert!(ids.contains(&"claude-fable-5"));
        assert!(ids.contains(&"claude-opus-5-5"));
        assert!(ids.contains(&"claude-opus-4-7"));
        assert!(ids.contains(&"claude-sonnet-4-6"));
        assert!(ids.contains(&"claude-haiku-4-5-20251001"));
    }

    #[test]
    fn descriptions_are_non_empty() {
        for (_, desc) in KNOWN_MODELS {
            assert!(!desc.is_empty(), "empty description");
        }
    }

    #[test]
    fn budget_for_thinking_level_round_trips() {
        for budget in [0u32, 2048, 4096, 16384, 32768] {
            let level = thinking_level_for_budget(budget);
            assert_eq!(
                budget_for_thinking_level(level),
                Some(budget),
                "level {}",
                level
            );
        }
        assert_eq!(budget_for_thinking_level("off"), Some(0));
        assert_eq!(budget_for_thinking_level("custom(8192)"), None);
    }

    #[test]
    fn only_models_before_claude_4_5_reject_output_overflow() {
        for m in [
            "claude-opus-4-1-20250805",
            "claude-opus-4-20250514",
            "claude-opus-4-0",
            "claude-sonnet-4-20250514",
            "anthropic/claude-sonnet-4-0",
            "claude-sonnet-4",
            "claude-3-7-sonnet-latest",
            "claude-3-5-haiku-20241022",
            "CLAUDE-3-OPUS-20240229",
            "claude-2.1",
            "claude-instant-1.2",
        ] {
            assert!(model_rejects_output_overflow(m), "{m}");
        }
        for m in [
            "claude-haiku-4-5-20251001",
            "claude-sonnet-4-5-20250929",
            "claude-opus-4-5-20251101",
            "claude-sonnet-4-6",
            "claude-opus-4-7",
            "anthropic/claude-opus-5-5",
            "claude-fable-5-1",
            "claude-sonnet-9-0",
            "gpt-5.5",
            "google-gemini/gemini-2.5-pro",
        ] {
            assert!(!model_rejects_output_overflow(m), "{m}");
        }
    }

    #[test]
    fn context_window_default_is_200k_for_all_models() {
        // Without explicit 1M opt-in (context-1m beta), every model is 200k.
        assert_eq!(context_window_for_model("claude-opus-4-7"), 200_000);
        assert_eq!(context_window_for_model("claude-opus-4-6"), 200_000);
        assert_eq!(context_window_for_model("claude-sonnet-4-6"), 200_000);
        assert_eq!(
            context_window_for_model("claude-opus-4-5-20251101"),
            200_000
        );
        assert_eq!(
            context_window_for_model("claude-haiku-4-5-20251001"),
            200_000
        );
    }

    #[test]
    fn model_supports_1m_for_opus_46_and_sonnet_4() {
        assert!(model_supports_1m("claude-opus-4-6"));
        assert!(model_supports_1m("claude-opus-4-7"));
        assert!(model_supports_1m("claude-sonnet-4-6"));
        assert!(model_supports_1m("claude-sonnet-4-5-20250929"));
    }

    #[test]
    fn model_supports_1m_rejects_older_and_haiku() {
        assert!(!model_supports_1m("claude-opus-4-5"));
        assert!(!model_supports_1m("claude-haiku-4-5-20251001"));
        assert!(!model_supports_1m("claude-opus-3-5"));
    }

    #[test]
    fn model_supports_1m_assumed_for_5x() {
        assert!(model_supports_1m("claude-opus-5-0"));
        assert!(model_supports_1m("claude-sonnet-5-1"));
        assert!(model_supports_1m("claude-fable-5"));
    }

    #[test]
    fn model_supports_1m_case_insensitive() {
        assert!(model_supports_1m("CLAUDE-OPUS-4-6"));
    }

    #[test]
    fn context_window_haiku_is_200k() {
        assert_eq!(
            context_window_for_model("claude-haiku-4-5-20251001"),
            200_000
        );
    }

    #[test]
    fn context_window_opus3_is_200k() {
        assert_eq!(
            context_window_for_model("claude-opus-3-5-20250101"),
            200_000
        );
    }

    #[test]
    fn context_window_unknown_defaults_200k() {
        assert_eq!(context_window_for_model("some-future-model"), 200_000);
        assert_eq!(context_window_for_model(""), 200_000);
    }

    #[test]
    fn adaptive_thinking_required_for_opus_4_7_plus() {
        assert!(model_supports_adaptive_thinking("claude-opus-4-7"));
        assert!(model_supports_adaptive_thinking("claude-opus-4-8"));
    }

    #[test]
    fn adaptive_thinking_not_for_opus_4_6() {
        // 4.6 uses enabled+budget_tokens (deprecated but functional)
        assert!(!model_supports_adaptive_thinking("claude-opus-4-6"));
    }

    #[test]
    fn adaptive_thinking_not_for_sonnet_4_6() {
        assert!(!model_supports_adaptive_thinking("claude-sonnet-4-6"));
    }

    #[test]
    fn adaptive_thinking_required_for_sonnet_4_7_plus() {
        assert!(model_supports_adaptive_thinking("claude-sonnet-4-7"));
    }

    #[test]
    fn adaptive_thinking_not_for_older_models() {
        assert!(!model_supports_adaptive_thinking("claude-opus-4-5"));
        assert!(!model_supports_adaptive_thinking("claude-sonnet-4-5"));
        assert!(!model_supports_adaptive_thinking(
            "claude-haiku-4-5-20251001"
        ));
        assert!(!model_supports_adaptive_thinking("claude-opus-3-5"));
    }

    #[test]
    fn adaptive_thinking_assumed_for_5x() {
        assert!(model_supports_adaptive_thinking("claude-opus-5-0"));
        assert!(model_supports_adaptive_thinking("claude-sonnet-5-1"));
    }

    #[test]
    fn opus_5_5_is_a_known_adaptive_1m_capable_model() {
        // Exact wire id is dash-separated, like every other Anthropic id.
        assert!(KNOWN_MODELS.iter().any(|(id, _)| *id == "claude-opus-5-5"));
        assert!(!KNOWN_MODELS.iter().any(|(id, _)| *id == "claude-opus-5.5"));
        assert!(model_supports_adaptive_thinking("claude-opus-5-5"));
        assert!(model_supports_1m("claude-opus-5-5"));
        // Not the default: adding a model must not silently change it.
        assert_ne!(default_model(), "claude-opus-5-5");
    }

    #[test]
    fn adaptive_thinking_case_insensitive() {
        assert!(model_supports_adaptive_thinking("CLAUDE-OPUS-4-7"));
    }
}