everruns-core 0.17.9

Core agent abstractions for Everruns - agent loop, events, tools, LLM providers
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
// Usage-Limit Auto-Continue capability
//
// When an LLM subscription/plan usage limit is hit — classified as
// `provider_usage_limit_reached` with a concrete `resets_at` timestamp (e.g. the
// ChatGPT/Codex `usage_limit_reached` body) — this capability makes the session
// resume on its own once the limit resets.
//
// The behavior is fully encapsulated behind the platform's `LlmErrorHook`
// seam (`crate::llm_error_hook`): the capability contributes no tools and no
// reason-atom special-casing. It supplies an error hook that, on the terminal
// error path, when the error code matches:
//
//   1. schedules a one-shot session continuation at `resets_at + delay_seconds`
//      that re-injects `prompt` to resume the interrupted work, and
//   2. returns an `auto_continue` error field so the user-facing copy promises
//      automatic resumption.
//
// (2) is returned only when (1) actually succeeded, so the copy never
// over-promises. The reason atom invokes this hook generically alongside any
// other capability's hook — new error-recovery extensions are built the same
// way, without touching the atom.

use super::{Capability, CapabilityLocalization, CapabilityStatus, RiskLevel};
use crate::capability_types::AgentCapabilityConfig;
use crate::llm_error_hook::{LlmErrorContext, LlmErrorHook, LlmErrorHookOutcome};
use async_trait::async_trait;
use serde_json::{Value, json};
use std::sync::Arc;

pub const USAGE_LIMIT_AUTO_CONTINUE_CAPABILITY_ID: &str = "usage_limit_auto_continue";

/// Default delay, in seconds, after the reported reset time before the
/// continuation fires. The reset is a lower bound; a small buffer avoids racing
/// the provider's own clock (the motivating Codex case wants reset + 2 min).
pub const DEFAULT_CONTINUATION_DELAY_SECS: i64 = 120;

/// Default prompt re-injected to resume work once the limit resets.
pub const DEFAULT_CONTINUATION_PROMPT: &str = "Continue tasks";

/// Upper bound on the configurable delay (24h) so a misconfiguration cannot park
/// a continuation arbitrarily far in the future.
const MAX_CONTINUATION_DELAY_SECS: i64 = 86_400;

/// Resolved auto-continue settings for a turn.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AutoContinueConfig {
    /// Seconds to wait after `resets_at` before firing the continuation.
    pub delay_seconds: i64,
    /// Prompt injected as the continuation turn's user message.
    pub prompt: String,
}

impl Default for AutoContinueConfig {
    fn default() -> Self {
        Self {
            delay_seconds: DEFAULT_CONTINUATION_DELAY_SECS,
            prompt: DEFAULT_CONTINUATION_PROMPT.to_string(),
        }
    }
}

impl AutoContinueConfig {
    /// Parse settings from a single capability config JSON value, falling back
    /// to defaults for any missing, blank, or out-of-range field.
    pub fn from_config_value(config: &Value) -> Self {
        let delay_seconds = config
            .get("delay_seconds")
            .and_then(Value::as_i64)
            .filter(|secs| (0..=MAX_CONTINUATION_DELAY_SECS).contains(secs))
            .unwrap_or(DEFAULT_CONTINUATION_DELAY_SECS);

        let prompt = config
            .get("prompt")
            .and_then(Value::as_str)
            .map(str::trim)
            .filter(|p| !p.is_empty())
            .unwrap_or(DEFAULT_CONTINUATION_PROMPT)
            .to_string();

        Self {
            delay_seconds,
            prompt,
        }
    }
}

/// Resolve the auto-continue settings for a turn, or `None` when the capability
/// is not enabled for the agent/session.
pub fn resolve_usage_limit_auto_continue(
    configs: &[AgentCapabilityConfig],
) -> Option<AutoContinueConfig> {
    configs
        .iter()
        .find(|config| config.capability_id() == USAGE_LIMIT_AUTO_CONTINUE_CAPABILITY_ID)
        .map(|config| AutoContinueConfig::from_config_value(&config.config))
}

pub struct UsageLimitAutoContinueCapability;

impl Capability for UsageLimitAutoContinueCapability {
    fn id(&self) -> &str {
        USAGE_LIMIT_AUTO_CONTINUE_CAPABILITY_ID
    }

    fn name(&self) -> &str {
        "Auto-Continue After Usage Limit"
    }

    fn description(&self) -> &str {
        "When an LLM usage limit is reached, automatically resume the interrupted work shortly after the limit resets."
    }

    fn localizations(&self) -> Vec<CapabilityLocalization> {
        vec![CapabilityLocalization::text(
            "uk",
            "Автопродовження після ліміту використання",
            "Коли досягнуто ліміт використання LLM, автоматично відновлює перервану роботу невдовзі після скидання ліміту.",
        )]
    }

    fn status(&self) -> CapabilityStatus {
        CapabilityStatus::Available
    }

    fn risk_level(&self) -> RiskLevel {
        RiskLevel::Low
    }

    fn icon(&self) -> Option<&str> {
        Some("clock")
    }

    fn category(&self) -> Option<&str> {
        Some("Core")
    }

    fn llm_error_hook(&self) -> Option<Arc<dyn LlmErrorHook>> {
        Some(Arc::new(UsageLimitAutoContinueHook))
    }

    fn config_schema(&self) -> Option<Value> {
        Some(json!({
            "type": "object",
            "properties": {
                "delay_seconds": {
                    "type": "integer",
                    "title": "Continuation delay (seconds)",
                    "description": "How long to wait after the reported reset time before resuming work. A small buffer avoids racing the provider's reset clock.",
                    "minimum": 0,
                    "maximum": MAX_CONTINUATION_DELAY_SECS,
                    "default": DEFAULT_CONTINUATION_DELAY_SECS
                },
                "prompt": {
                    "type": "string",
                    "title": "Continuation prompt",
                    "description": "Message injected to resume the interrupted work when the limit resets.",
                    "default": DEFAULT_CONTINUATION_PROMPT
                }
            },
            "additionalProperties": false
        }))
    }

    fn validate_config(&self, config: &Value) -> Result<(), String> {
        if config.is_null() {
            return Ok(());
        }
        if let Some(delay) = config.get("delay_seconds") {
            let secs = delay
                .as_i64()
                .ok_or_else(|| "delay_seconds must be an integer".to_string())?;
            if !(0..=MAX_CONTINUATION_DELAY_SECS).contains(&secs) {
                return Err(format!(
                    "delay_seconds must be between 0 and {MAX_CONTINUATION_DELAY_SECS}"
                ));
            }
        }
        if let Some(prompt) = config.get("prompt")
            && !prompt.is_string()
        {
            return Err("prompt must be a string".to_string());
        }
        Ok(())
    }
}

/// Error hook that encapsulates the auto-continue behavior. Invoked by the
/// reason atom through the generic `LlmErrorHook` seam on a terminal LLM
/// error; the reason atom itself has no knowledge of usage limits or scheduling.
struct UsageLimitAutoContinueHook;

#[async_trait]
impl LlmErrorHook for UsageLimitAutoContinueHook {
    async fn on_llm_error(&self, ctx: &LlmErrorContext<'_>) -> LlmErrorHookOutcome {
        // Only act on the usage-limit error this capability recovers from.
        if ctx.error_code != crate::user_facing_error_codes::PROVIDER_USAGE_LIMIT_REACHED {
            return LlmErrorHookOutcome::noop();
        }
        // Best-effort: without a schedule store or a concrete reset time there
        // is nothing to schedule, so make no auto-resume promise.
        let Some(store) = &ctx.services.schedule_store else {
            return LlmErrorHookOutcome::noop();
        };
        let Some(resets_at) = ctx.error_fields.get("resets_at").and_then(|v| v.as_i64()) else {
            return LlmErrorHookOutcome::noop();
        };
        let Some(reset_time) = chrono::DateTime::from_timestamp(resets_at, 0) else {
            return LlmErrorHookOutcome::noop();
        };

        let cfg = AutoContinueConfig::from_config_value(ctx.config);

        // Fire `delay_seconds` after the reset. Clamp into the future so a
        // stale/past reset (clock skew, limit already cleared) still fires
        // instead of being dropped by the store's past-time next-trigger rule.
        let now = chrono::Utc::now();
        let delay = chrono::Duration::seconds(cfg.delay_seconds);
        let scheduled_at = (reset_time + delay).max(now + delay);

        match store
            .create_schedule(
                ctx.session_id,
                cfg.prompt.clone(),
                None,
                Some(scheduled_at),
                "UTC".to_string(),
            )
            .await
        {
            Ok(schedule) => {
                tracing::info!(
                    session_id = %ctx.session_id,
                    schedule_id = %schedule.id,
                    scheduled_at = %scheduled_at,
                    "usage_limit_auto_continue: scheduled continuation after usage-limit reset"
                );
                // Unlock the "we'll continue automatically" message copy only
                // now that a continuation was actually scheduled.
                LlmErrorHookOutcome::noop().with_error_field("auto_continue", true)
            }
            Err(e) => {
                tracing::warn!(
                    session_id = %ctx.session_id,
                    error = %e,
                    "usage_limit_auto_continue: failed to schedule continuation"
                );
                LlmErrorHookOutcome::noop()
            }
        }
    }
}

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

    fn cap_config(config: Value) -> AgentCapabilityConfig {
        AgentCapabilityConfig {
            capability_ref: USAGE_LIMIT_AUTO_CONTINUE_CAPABILITY_ID.into(),
            config,
        }
    }

    #[test]
    fn resolve_returns_none_without_capability() {
        assert_eq!(resolve_usage_limit_auto_continue(&[]), None);
    }

    #[test]
    fn resolve_uses_defaults_for_empty_config() {
        let resolved = resolve_usage_limit_auto_continue(&[cap_config(json!({}))]).unwrap();
        assert_eq!(resolved, AutoContinueConfig::default());
    }

    #[test]
    fn resolve_reads_custom_delay_and_prompt() {
        let resolved = resolve_usage_limit_auto_continue(&[cap_config(json!({
            "delay_seconds": 300,
            "prompt": "  Resume the migration  "
        }))])
        .unwrap();
        assert_eq!(resolved.delay_seconds, 300);
        assert_eq!(resolved.prompt, "Resume the migration");
    }

    #[test]
    fn resolve_falls_back_on_out_of_range_or_blank_fields() {
        let resolved = resolve_usage_limit_auto_continue(&[cap_config(json!({
            "delay_seconds": -5,
            "prompt": "   "
        }))])
        .unwrap();
        assert_eq!(resolved.delay_seconds, DEFAULT_CONTINUATION_DELAY_SECS);
        assert_eq!(resolved.prompt, DEFAULT_CONTINUATION_PROMPT);
    }

    #[test]
    fn validate_rejects_bad_types_and_ranges() {
        let cap = UsageLimitAutoContinueCapability;
        assert!(
            cap.validate_config(&json!({ "delay_seconds": 120 }))
                .is_ok()
        );
        assert!(
            cap.validate_config(&json!({ "delay_seconds": "x" }))
                .is_err()
        );
        assert!(
            cap.validate_config(&json!({ "delay_seconds": 999999999 }))
                .is_err()
        );
        assert!(cap.validate_config(&json!({ "prompt": 5 })).is_err());
    }

    // ------------------------------------------------------------------------
    // Handler tests (the encapsulated behavior, exercised via the seam)
    // ------------------------------------------------------------------------

    use crate::llm_error_hook::{LlmErrorContext, LlmErrorHook, LlmErrorHookServices};
    use crate::session_schedule::SessionSchedule;
    use crate::traits::SessionScheduleStore;
    use crate::typed_id::{PrincipalId, ScheduleId, SessionId};
    use crate::user_facing_error::UserFacingErrorFields;
    use crate::user_facing_error_codes;
    use std::sync::Mutex;

    /// Records the one-shot schedules the handler creates so tests can assert on
    /// the description and fire time.
    #[derive(Default)]
    struct RecordingScheduleStore {
        created: Mutex<Vec<(String, Option<chrono::DateTime<chrono::Utc>>)>>,
    }

    #[async_trait]
    impl SessionScheduleStore for RecordingScheduleStore {
        async fn create_schedule(
            &self,
            session_id: SessionId,
            description: String,
            cron_expression: Option<String>,
            scheduled_at: Option<chrono::DateTime<chrono::Utc>>,
            timezone: String,
        ) -> crate::error::Result<SessionSchedule> {
            self.created
                .lock()
                .unwrap()
                .push((description.clone(), scheduled_at));
            Ok(SessionSchedule {
                id: ScheduleId::new(),
                session_id,
                owner_principal_id: PrincipalId::new(),
                resolved_owner_user_id: None,
                owner: None,
                effective_owner: None,
                description,
                cron_expression: cron_expression.clone(),
                scheduled_at,
                timezone,
                enabled: true,
                schedule_type: SessionSchedule::derive_type(&cron_expression),
                next_trigger_at: scheduled_at,
                last_triggered_at: None,
                trigger_count: 0,
                created_at: chrono::Utc::now(),
                updated_at: chrono::Utc::now(),
            })
        }

        async fn cancel_schedule(
            &self,
            _session_id: SessionId,
            _schedule_id: ScheduleId,
        ) -> crate::error::Result<SessionSchedule> {
            unimplemented!("not used by these tests")
        }

        async fn list_schedules(
            &self,
            _session_id: SessionId,
        ) -> crate::error::Result<Vec<SessionSchedule>> {
            Ok(vec![])
        }

        async fn count_active_schedules(
            &self,
            _session_id: SessionId,
        ) -> crate::error::Result<u32> {
            Ok(0)
        }

        async fn count_active_org_schedules(&self) -> crate::error::Result<u32> {
            Ok(0)
        }
    }

    fn usage_limit_fields(resets_at: i64) -> UserFacingErrorFields {
        let mut fields = UserFacingErrorFields::new();
        fields.insert("resets_at".to_string(), json!(resets_at));
        fields
    }

    #[tokio::test]
    async fn handler_schedules_continuation_and_flags_auto_continue() {
        let store = Arc::new(RecordingScheduleStore::default());
        let services = LlmErrorHookServices {
            schedule_store: Some(store.clone()),
        };
        let fields = usage_limit_fields(1_783_767_823);
        let config = json!({ "prompt": "Resume the migration" });
        let ctx = LlmErrorContext {
            session_id: SessionId::new(),
            error_code: user_facing_error_codes::PROVIDER_USAGE_LIMIT_REACHED,
            error_fields: &fields,
            config: &config,
            services: &services,
        };

        let outcome = UsageLimitAutoContinueHook.on_llm_error(&ctx).await;

        // Continuation scheduled with the configured prompt.
        let created = store.created.lock().unwrap();
        assert_eq!(created.len(), 1);
        assert_eq!(created[0].0, "Resume the migration");
        assert!(created[0].1.is_some(), "continuation must have a fire time");

        // Only now is the auto-continue message copy unlocked.
        assert_eq!(
            outcome.extra_error_fields.get("auto_continue"),
            Some(&json!(true))
        );
    }

    #[tokio::test]
    async fn handler_ignores_non_usage_limit_errors() {
        let store = Arc::new(RecordingScheduleStore::default());
        let services = LlmErrorHookServices {
            schedule_store: Some(store.clone()),
        };
        let fields = usage_limit_fields(1_783_767_823);
        let config = json!({});
        let ctx = LlmErrorContext {
            session_id: SessionId::new(),
            error_code: user_facing_error_codes::PROVIDER_RATE_LIMITED,
            error_fields: &fields,
            config: &config,
            services: &services,
        };

        let outcome = UsageLimitAutoContinueHook.on_llm_error(&ctx).await;

        assert!(store.created.lock().unwrap().is_empty());
        assert_eq!(outcome, LlmErrorHookOutcome::noop());
    }

    #[tokio::test]
    async fn handler_makes_no_promise_without_schedule_store() {
        let services = LlmErrorHookServices {
            schedule_store: None,
        };
        let fields = usage_limit_fields(1_783_767_823);
        let config = json!({});
        let ctx = LlmErrorContext {
            session_id: SessionId::new(),
            error_code: user_facing_error_codes::PROVIDER_USAGE_LIMIT_REACHED,
            error_fields: &fields,
            config: &config,
            services: &services,
        };

        let outcome = UsageLimitAutoContinueHook.on_llm_error(&ctx).await;
        assert_eq!(outcome, LlmErrorHookOutcome::noop());
    }
}