agix-base 0.4.4

Agix — foundation types, traits, and context
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! `RecoveryPolicy` — what to do when a model call goes wrong.
//!
//! The engine's own answers ([`DefaultRecovery`]): a request refused for size
//! is compacted and retried once. A transient
//! failure after the stream had started — a stream that broke off, went
//! silent, or ended in an overload event — is sent again as it was, twice at
//! most, after 2s and then 8s, or after the provider's own `retry-after` —
//! unless it asks for longer than the client's ladder would wait
//! ([`LadderBackoff::MAX_SERVER_HINT`]), which fails instead. Anything else
//! fails. A response cut off at the
//! output limit escalates the limit — to 64K the first time, 8K after that,
//! three attempts, each time with a user message saying the reply was cut off
//! and asking it to go on; the engine holds the raised limit to what the model
//! accepts. A response the provider paused is sent back to be continued.
//!
//! Every one of those is a reasonable answer and none of them is the only
//! one. One paying per token would rather fail than escalate to 64K. Moving
//! to another provider is not among them: the router does that below the
//! turn, before a failure reaches this policy.
//!
//! # Which failures reach it
//!
//! A failure is put to the policy when nothing of the call has been kept: the
//! provider refused the request, or its stream broke off — an error event, or
//! an end before the message's — before the reply asked for a tool. The
//! partial reply is dropped, so what a recovery retries is the request that
//! failed. Once a tool has run, the reply stays in the transcript with its
//! results and the turn ends with the error: sending it again would run the
//! tool again.
//!
//! A failure before the first byte has already been through the client's own
//! backoff ([`BackoffPolicy`](crate::interface::backoff::BackoffPolicy)), so
//! sending the same request again is not this policy's to decide — that
//! would multiply one ladder by another. [`ModelFailure::after_first_byte`]
//! says which side of that line a failure is on.
//!
//! What one call has already spent is handed in as [`CallRecovery`]. However
//! the policy answers, the loop recovers one call at most
//! [`MAX_RECOVERIES_PER_CALL`] times.
//!
//! # The policy decides; the loop acts
//!
//! Recovery *mechanisms* — compacting, rebuilding a request, sending it —
//! belong to the turn: they touch the session, the compactor and the model in
//! ways that only the loop can sequence correctly. What was missing is the
//! choice between them. So this contract answers "what should happen", in a
//! vocabulary the loop already implements, and nothing else. That is what
//! keeps the skeleton closed while the decision opens.
//!
//! # Attempt counts are the policy's memory
//!
//! Both recovery paths bound themselves with a counter the loop kept. The
//! counters are still the loop's — they belong to one turn, and a policy is
//! shared across turns — but they are *given* to the policy, so a policy that
//! wants to allow four escalations rather than three needs no state of its
//! own.

use crate::interface::backoff::LadderBackoff;
use crate::interface::model::ModelErrorKind;
use std::time::Duration;

/// Why a model call failed: the protocol client's classification, carried
/// over as it stands.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelFailure<'a> {
    pub kind: ModelErrorKind,
    pub message: &'a str,
    /// How long the provider asked to be left alone, when it said.
    pub retry_after: Option<Duration>,
    /// The provider had started answering: the client's backoff no longer
    /// covered this call.
    pub after_first_byte: bool,
}

impl<'a> ModelFailure<'a> {
    pub fn new(kind: ModelErrorKind, message: &'a str) -> Self {
        Self {
            kind,
            message,
            retry_after: None,
            after_first_byte: false,
        }
    }

    pub fn with_retry_after(mut self, retry_after: Option<Duration>) -> Self {
        self.retry_after = retry_after;
        self
    }

    pub fn with_after_first_byte(mut self, after_first_byte: bool) -> Self {
        self.after_first_byte = after_first_byte;
        self
    }
}

/// What one model call has already spent on recovering.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CallRecovery {
    pub compacted: bool,
    /// [`Recovery::RetrySame`]s so far.
    pub same_retries: u32,
}

/// The most recoveries the loop gives one call, whatever the policy answers.
pub const MAX_RECOVERIES_PER_CALL: u32 = 4;

/// What the turn should do about it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Recovery {
    /// Give up. The turn fails and the message reaches the host.
    Fail,
    /// Shorten the conversation, then retry against the same model. Failing to
    /// shorten it is a failure: retrying an unshortened request that was
    /// refused for size just spends another call to be told the same thing.
    CompactAndRetry,
    /// Wait, then send the same request again. The host is told with a
    /// `model_retry` event, and the step starts over.
    RetrySame { after: Duration },
}

/// What to do about a response that stopped early.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StopRecovery {
    /// Accept the response as it is.
    Accept,
    /// Send the conversation again as it stands, the response included, so
    /// the model picks up where it stopped — what a provider asks for with
    /// `pause_turn`. Each continuation is another call of the turn, bounded
    /// by the turn policy's ceiling on calls.
    Continue,
    /// Raise the output limit and ask again.
    RaiseOutputLimit {
        /// Held to the model's own output limit by the engine.
        max_tokens: u32,
        /// Text to append as a user message before retrying, if any. Without
        /// one the retry ends on the cut-off reply, which the provider takes
        /// as a prefill to continue — and several refuse: Anthropic with
        /// thinking on, or when the reply ends in whitespace.
        nudge: Option<String>,
    },
}

/// How many times this turn has already tried to recover.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RecoveryAttempt {
    /// Output-limit escalations so far in this turn.
    pub output_limit_escalations: u32,
    /// The limit currently in force.
    pub current_max_tokens: u32,
}

/// What to do when a model call goes wrong.
pub trait RecoveryPolicy: Send + Sync {
    /// The call failed, after `so_far` was already spent on it.
    fn on_failure(&self, failure: &ModelFailure<'_>, so_far: &CallRecovery) -> Recovery;

    /// The call succeeded but stopped early — `reason` is the model's own
    /// stop reason.
    ///
    /// Asked only for a reply that called no tools, after
    /// [`TurnPolicy::after_model_call`] let the turn go on. A retry decided
    /// here comes before the `Stop` hook and before
    /// [`BudgetPolicy::on_output_target`], which see the reply only when this
    /// gives up.
    ///
    /// [`TurnPolicy::after_model_call`]: crate::interface::turn_policy::TurnPolicy::after_model_call
    /// [`BudgetPolicy::on_output_target`]: crate::interface::budget_policy::BudgetPolicy::on_output_target
    fn on_early_stop(&self, reason: &str, attempt: &RecoveryAttempt) -> StopRecovery;
}

/// The engine's own answers.
pub struct DefaultRecovery {
    /// How many output-limit escalations a turn may spend.
    pub max_output_escalations: u32,
}

impl DefaultRecovery {
    /// First escalation goes straight to 64K on the theory that a response cut
    /// off once will be cut off again at a smaller bump; later ones settle at
    /// 8K. The engine holds either to the model's own limit.
    pub const FIRST_ESCALATION: u32 = 64_000;
    pub const LATER_ESCALATION: u32 = 8_000;

    pub const NUDGE: &'static str = "Your previous reply was cut off at the output token \
                                     limit. Continue from exactly where it stopped — no \
                                     apology, no recap of what you were doing. Pick up \
                                     mid-thought if that is where the cut happened. Break \
                                     remaining work into smaller pieces.";

    /// How long to wait before each [`Recovery::RetrySame`] when the provider
    /// named no delay. Its length is the budget.
    pub const SAME_RETRY_DELAYS: &'static [Duration] =
        &[Duration::from_secs(2), Duration::from_secs(8)];
}

impl Default for DefaultRecovery {
    fn default() -> Self {
        Self {
            max_output_escalations: 3,
        }
    }
}

impl RecoveryPolicy for DefaultRecovery {
    fn on_failure(&self, failure: &ModelFailure<'_>, so_far: &CallRecovery) -> Recovery {
        if failure.kind == ModelErrorKind::ContextTooLong && !so_far.compacted {
            return Recovery::CompactAndRetry;
        }
        if failure.after_first_byte && failure.kind.transient() {
            if let Some(delay) = Self::SAME_RETRY_DELAYS.get(so_far.same_retries as usize) {
                return match failure.retry_after {
                    Some(hint) if hint > LadderBackoff::MAX_SERVER_HINT => Recovery::Fail,
                    hint => Recovery::RetrySame {
                        after: hint.unwrap_or(*delay),
                    },
                };
            }
        }
        Recovery::Fail
    }

    fn on_early_stop(&self, reason: &str, attempt: &RecoveryAttempt) -> StopRecovery {
        if reason == "pause_turn" {
            return StopRecovery::Continue;
        }
        if reason != "max_tokens" || attempt.output_limit_escalations >= self.max_output_escalations
        {
            return StopRecovery::Accept;
        }
        let max_tokens = if attempt.output_limit_escalations == 0 {
            attempt.current_max_tokens.max(Self::FIRST_ESCALATION)
        } else {
            attempt.current_max_tokens.max(Self::LATER_ESCALATION)
        };
        StopRecovery::RaiseOutputLimit {
            max_tokens,
            nudge: Some(Self::NUDGE.to_string()),
        }
    }
}

/// Fails on everything and escalates nothing.
///
/// The second implementation, and a real configuration rather than a stub: a
/// deployment billed per token, or one running a batch where a surprise 64K
/// call is worse than a truncated answer, wants exactly this.
pub struct NeverRecover;

impl RecoveryPolicy for NeverRecover {
    fn on_failure(&self, _failure: &ModelFailure<'_>, _so_far: &CallRecovery) -> Recovery {
        Recovery::Fail
    }

    fn on_early_stop(&self, _reason: &str, _attempt: &RecoveryAttempt) -> StopRecovery {
        StopRecovery::Accept
    }
}

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

    fn failure(kind: ModelErrorKind) -> ModelFailure<'static> {
        ModelFailure::new(kind, "")
    }

    fn streamed(kind: ModelErrorKind) -> ModelFailure<'static> {
        ModelFailure::new(kind, "").with_after_first_byte(true)
    }

    fn fresh() -> CallRecovery {
        CallRecovery::default()
    }

    fn attempt(escalations: u32, max_tokens: u32) -> RecoveryAttempt {
        RecoveryAttempt {
            output_limit_escalations: escalations,
            current_max_tokens: max_tokens,
        }
    }

    #[test]
    fn an_overload_before_the_first_byte_fails() {
        assert_eq!(
            DefaultRecovery::default().on_failure(&failure(ModelErrorKind::Overloaded), &fresh()),
            Recovery::Fail,
            "before the first byte the client's ladder already waited"
        );
    }

    #[test]
    fn a_request_refused_for_size_is_shortened_rather_than_resent() {
        assert_eq!(
            DefaultRecovery::default()
                .on_failure(&failure(ModelErrorKind::ContextTooLong), &fresh()),
            Recovery::CompactAndRetry
        );
    }

    #[test]
    fn anything_else_fails() {
        assert_eq!(
            DefaultRecovery::default().on_failure(&failure(ModelErrorKind::Transport), &fresh()),
            Recovery::Fail
        );
    }

    #[test]
    fn a_request_is_compacted_once() {
        let compacted = CallRecovery {
            compacted: true,
            ..fresh()
        };
        assert_eq!(
            DefaultRecovery::default()
                .on_failure(&failure(ModelErrorKind::ContextTooLong), &compacted),
            Recovery::Fail
        );
    }

    #[test]
    fn a_stream_that_broke_off_is_sent_again_twice() {
        let p = DefaultRecovery::default();
        let after = |same_retries| {
            p.on_failure(
                &streamed(ModelErrorKind::StreamInterrupted),
                &CallRecovery {
                    same_retries,
                    ..fresh()
                },
            )
        };
        assert_eq!(
            after(0),
            Recovery::RetrySame {
                after: Duration::from_secs(2)
            }
        );
        assert_eq!(
            after(1),
            Recovery::RetrySame {
                after: Duration::from_secs(8)
            }
        );
        assert_eq!(after(2), Recovery::Fail);
    }

    #[test]
    fn the_providers_delay_wins_over_the_default() {
        let hinted =
            streamed(ModelErrorKind::Overloaded).with_retry_after(Some(Duration::from_secs(5)));
        assert_eq!(
            DefaultRecovery::default().on_failure(&hinted, &fresh()),
            Recovery::RetrySame {
                after: Duration::from_secs(5)
            }
        );
    }

    #[test]
    fn a_provider_delay_beyond_the_ladders_cap_is_not_waited_for() {
        let p = DefaultRecovery::default();
        let asking = |delay| streamed(ModelErrorKind::Overloaded).with_retry_after(Some(delay));
        assert_eq!(
            p.on_failure(&asking(LadderBackoff::MAX_SERVER_HINT), &fresh()),
            Recovery::RetrySame {
                after: LadderBackoff::MAX_SERVER_HINT
            }
        );
        assert_eq!(
            p.on_failure(
                &asking(LadderBackoff::MAX_SERVER_HINT + Duration::from_secs(1)),
                &fresh()
            ),
            Recovery::Fail
        );
        assert_eq!(
            p.on_failure(&asking(Duration::from_secs(u64::MAX)), &fresh()),
            Recovery::Fail
        );
    }

    #[test]
    fn only_a_transient_failure_after_the_first_byte_is_sent_again() {
        let p = DefaultRecovery::default();
        for kind in [ModelErrorKind::Timeout, ModelErrorKind::Transport] {
            assert_eq!(
                p.on_failure(&failure(kind), &fresh()),
                Recovery::Fail,
                "{kind}: before the first byte"
            );
            assert!(matches!(
                p.on_failure(&streamed(kind), &fresh()),
                Recovery::RetrySame { .. }
            ));
        }
        for kind in [
            ModelErrorKind::Auth,
            ModelErrorKind::InvalidRequest,
            ModelErrorKind::Protocol,
            ModelErrorKind::Cancelled,
        ] {
            assert_eq!(
                p.on_failure(&streamed(kind), &fresh()),
                Recovery::Fail,
                "{kind}"
            );
        }
    }

    /// The escalation ladder, which is three numbers and an ordering that a
    /// reader has no way to check against intent — so it is checked here.
    #[test]
    fn the_escalation_ladder_is_64k_then_8k_each_with_a_nudge() {
        let p = DefaultRecovery::default();
        assert_eq!(
            p.on_early_stop("max_tokens", &attempt(0, 2000)),
            StopRecovery::RaiseOutputLimit {
                max_tokens: 64_000,
                nudge: Some(DefaultRecovery::NUDGE.to_string())
            },
            "the first retry must not end on the cut-off reply"
        );
        assert_eq!(
            p.on_early_stop("max_tokens", &attempt(1, 64_000)),
            StopRecovery::RaiseOutputLimit {
                max_tokens: 64_000,
                nudge: Some(DefaultRecovery::NUDGE.to_string())
            },
            "a limit already above 8K is not lowered to it"
        );
        assert_eq!(
            p.on_early_stop("max_tokens", &attempt(3, 64_000)),
            StopRecovery::Accept,
            "three escalations is the bound"
        );
    }

    #[test]
    fn a_paused_response_is_continued_whatever_the_escalations() {
        let p = DefaultRecovery::default();
        for escalations in [0, 3] {
            assert_eq!(
                p.on_early_stop("pause_turn", &attempt(escalations, 2000)),
                StopRecovery::Continue
            );
        }
        assert_eq!(
            NeverRecover.on_early_stop("pause_turn", &attempt(0, 2000)),
            StopRecovery::Accept
        );
    }

    #[test]
    fn an_ordinary_stop_reason_is_not_a_recovery() {
        assert_eq!(
            DefaultRecovery::default().on_early_stop("end_turn", &attempt(0, 2000)),
            StopRecovery::Accept
        );
    }

    #[test]
    fn a_deployment_can_refuse_to_spend_anything_on_recovery() {
        assert_eq!(
            NeverRecover.on_failure(&streamed(ModelErrorKind::Overloaded), &fresh()),
            Recovery::Fail
        );
        assert_eq!(
            NeverRecover.on_early_stop("max_tokens", &attempt(0, 2000)),
            StopRecovery::Accept
        );
    }
}