clawft-types 0.6.4

Core types for the clawft AI assistant framework
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
//! Cron scheduling types.
//!
//! Defines the data model for scheduled jobs: [`CronJob`], its
//! [`CronSchedule`], [`CronPayload`], and runtime [`CronJobState`].
//! The [`CronStore`] is the top-level container persisted to disk.
//!
//! All timestamps use `DateTime<Utc>` for type safety. For backward
//! compatibility, the serde layer accepts both RFC 3339 strings and
//! millisecond-since-epoch integers via custom deserializers.

use chrono::{DateTime, TimeZone, Utc};
use serde::{Deserialize, Serialize};

/// How a cron job is scheduled.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScheduleKind {
    /// Fire once at a specific timestamp.
    At,
    /// Fire repeatedly at a fixed interval.
    Every,
    /// Fire according to a cron expression.
    Cron,
}

/// Schedule definition for a cron job.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CronSchedule {
    /// The type of schedule.
    pub kind: ScheduleKind,

    /// For [`ScheduleKind::At`]: timestamp in milliseconds since epoch.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub at_ms: Option<i64>,

    /// For [`ScheduleKind::Every`]: interval in milliseconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub every_ms: Option<i64>,

    /// For [`ScheduleKind::Cron`]: cron expression (e.g. `"0 9 * * *"`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expr: Option<String>,

    /// Timezone for cron expressions (e.g. `"UTC"`, `"Asia/Shanghai"`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tz: Option<String>,
}

impl Default for CronSchedule {
    fn default() -> Self {
        Self {
            kind: ScheduleKind::Every,
            at_ms: None,
            every_ms: None,
            expr: None,
            tz: None,
        }
    }
}

/// What action to perform when a cron job fires.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PayloadKind {
    /// Emit a system-level event.
    SystemEvent,
    /// Trigger an agent turn with a message.
    AgentTurn,
}

/// Payload executed when a cron job fires.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CronPayload {
    /// The type of payload.
    #[serde(default = "default_payload_kind")]
    pub kind: PayloadKind,

    /// Message to deliver or use as agent prompt.
    #[serde(default)]
    pub message: String,

    /// Whether to deliver the response to a channel.
    #[serde(default)]
    pub deliver: bool,

    /// Target channel name (e.g. `"whatsapp"`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub channel: Option<String>,

    /// Target recipient (e.g. phone number, user ID).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub to: Option<String>,
}

fn default_payload_kind() -> PayloadKind {
    PayloadKind::AgentTurn
}

impl Default for CronPayload {
    fn default() -> Self {
        Self {
            kind: PayloadKind::AgentTurn,
            message: String::new(),
            deliver: false,
            channel: None,
            to: None,
        }
    }
}

/// Outcome of the last job execution.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum JobStatus {
    /// The job completed successfully.
    Ok,
    /// The job encountered an error.
    Error,
    /// The job was skipped (e.g. already running).
    Skipped,
}

/// Runtime state of a cron job.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CronJobState {
    /// Next scheduled run time (UTC).
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_datetime_or_ms"
    )]
    pub next_run_at: Option<DateTime<Utc>>,

    /// Last actual run time (UTC).
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_datetime_or_ms"
    )]
    pub last_run_at: Option<DateTime<Utc>>,

    /// Outcome of the last run.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_status: Option<JobStatus>,

    /// Error message from the last failed run.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_error: Option<String>,
}

/// A scheduled job.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CronJob {
    /// Unique job identifier.
    pub id: String,

    /// Human-readable job name.
    pub name: String,

    /// Whether the job is active.
    #[serde(default = "default_true")]
    pub enabled: bool,

    /// When and how often to run.
    #[serde(default)]
    pub schedule: CronSchedule,

    /// What to do when the job fires.
    #[serde(default)]
    pub payload: CronPayload,

    /// Runtime state (next run, last run, etc.).
    #[serde(default)]
    pub state: CronJobState,

    /// Creation timestamp (UTC).
    #[serde(default = "default_epoch", deserialize_with = "deserialize_datetime_or_ms")]
    pub created_at: DateTime<Utc>,

    /// Last update timestamp (UTC).
    #[serde(default = "default_epoch", deserialize_with = "deserialize_datetime_or_ms")]
    pub updated_at: DateTime<Utc>,

    /// If true, delete the job after its next successful run.
    #[serde(default)]
    pub delete_after_run: bool,
}

/// Returns the Unix epoch as a default `DateTime<Utc>` (for `#[serde(default)]`).
fn default_epoch() -> DateTime<Utc> {
    DateTime::UNIX_EPOCH
}

/// Deserialize a `DateTime<Utc>` from either:
/// - An RFC 3339 string (new format)
/// - An integer (milliseconds since epoch, legacy format)
fn deserialize_datetime_or_ms<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::de;

    let value: serde_json::Value = Deserialize::deserialize(deserializer)?;
    match &value {
        serde_json::Value::String(s) => DateTime::parse_from_rfc3339(s)
            .map(|dt| dt.with_timezone(&Utc))
            .or_else(|_| s.parse::<DateTime<Utc>>())
            .map_err(de::Error::custom),
        serde_json::Value::Number(n) => {
            let ms = n.as_i64().ok_or_else(|| de::Error::custom("expected i64"))?;
            Utc.timestamp_millis_opt(ms)
                .single()
                .ok_or_else(|| de::Error::custom(format!("invalid ms timestamp: {ms}")))
        }
        serde_json::Value::Null => Ok(DateTime::UNIX_EPOCH),
        _ => Err(de::Error::custom("expected string, integer, or null")),
    }
}

/// Deserialize an `Option<DateTime<Utc>>` from either:
/// - An RFC 3339 string (new format)
/// - An integer (milliseconds since epoch, legacy format)
/// - `null` / missing -> `None`
fn deserialize_optional_datetime_or_ms<'de, D>(
    deserializer: D,
) -> Result<Option<DateTime<Utc>>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::de;

    let value: Option<serde_json::Value> = Option::deserialize(deserializer)?;
    match value {
        None | Some(serde_json::Value::Null) => Ok(None),
        Some(serde_json::Value::String(s)) => DateTime::parse_from_rfc3339(&s)
            .map(|dt| Some(dt.with_timezone(&Utc)))
            .or_else(|_| s.parse::<DateTime<Utc>>().map(Some))
            .map_err(de::Error::custom),
        Some(serde_json::Value::Number(n)) => {
            let ms = n.as_i64().ok_or_else(|| de::Error::custom("expected i64"))?;
            Ok(Utc.timestamp_millis_opt(ms).single())
        }
        _ => Err(de::Error::custom("expected string, integer, or null")),
    }
}

fn default_true() -> bool {
    true
}

/// Persistent store for cron jobs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CronStore {
    /// Schema version for forward compatibility.
    #[serde(default = "default_version")]
    pub version: u32,

    /// All registered cron jobs.
    #[serde(default)]
    pub jobs: Vec<CronJob>,
}

fn default_version() -> u32 {
    1
}

impl Default for CronStore {
    fn default() -> Self {
        Self {
            version: 1,
            jobs: Vec::new(),
        }
    }
}

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

    #[test]
    fn schedule_default() {
        let s = CronSchedule::default();
        assert_eq!(s.kind, ScheduleKind::Every);
        assert!(s.at_ms.is_none());
        assert!(s.every_ms.is_none());
    }

    #[test]
    fn payload_default() {
        let p = CronPayload::default();
        assert_eq!(p.kind, PayloadKind::AgentTurn);
        assert!(p.message.is_empty());
        assert!(!p.deliver);
    }

    #[test]
    fn cron_store_default() {
        let store = CronStore::default();
        assert_eq!(store.version, 1);
        assert!(store.jobs.is_empty());
    }

    #[test]
    fn cron_job_serde_roundtrip() {
        let now = Utc::now();
        let job = CronJob {
            id: "job-1".into(),
            name: "daily check".into(),
            enabled: true,
            schedule: CronSchedule {
                kind: ScheduleKind::Cron,
                at_ms: None,
                every_ms: None,
                expr: Some("0 9 * * *".into()),
                tz: Some("UTC".into()),
            },
            payload: CronPayload {
                kind: PayloadKind::AgentTurn,
                message: "run daily report".into(),
                deliver: true,
                channel: Some("slack".into()),
                to: Some("C123".into()),
            },
            state: CronJobState::default(),
            created_at: now,
            updated_at: now,
            delete_after_run: false,
        };
        let json = serde_json::to_string(&job).unwrap();
        let restored: CronJob = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.id, "job-1");
        assert_eq!(restored.schedule.kind, ScheduleKind::Cron);
        assert_eq!(restored.schedule.expr.as_deref(), Some("0 9 * * *"));
        assert_eq!(restored.payload.channel.as_deref(), Some("slack"));
    }

    #[test]
    fn cron_store_serde_roundtrip() {
        let store = CronStore {
            version: 1,
            jobs: vec![CronJob {
                id: "j1".into(),
                name: "test".into(),
                enabled: true,
                schedule: CronSchedule::default(),
                payload: CronPayload::default(),
                state: CronJobState::default(),
                created_at: DateTime::UNIX_EPOCH,
                updated_at: DateTime::UNIX_EPOCH,
                delete_after_run: true,
            }],
        };
        let json = serde_json::to_string(&store).unwrap();
        let restored: CronStore = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.version, 1);
        assert_eq!(restored.jobs.len(), 1);
        assert!(restored.jobs[0].delete_after_run);
    }

    #[test]
    fn schedule_kind_serde() {
        let kinds = [
            (ScheduleKind::At, "\"at\""),
            (ScheduleKind::Every, "\"every\""),
            (ScheduleKind::Cron, "\"cron\""),
        ];
        for (kind, expected) in &kinds {
            let json = serde_json::to_string(kind).unwrap();
            assert_eq!(&json, expected);
            let restored: ScheduleKind = serde_json::from_str(&json).unwrap();
            assert_eq!(restored, *kind);
        }
    }

    #[test]
    fn job_status_serde() {
        let statuses = [
            (JobStatus::Ok, "\"ok\""),
            (JobStatus::Error, "\"error\""),
            (JobStatus::Skipped, "\"skipped\""),
        ];
        for (status, expected) in &statuses {
            let json = serde_json::to_string(status).unwrap();
            assert_eq!(&json, expected);
        }
    }

    #[test]
    fn cron_job_defaults_on_missing_fields() {
        let json = r#"{"id": "j1", "name": "test"}"#;
        let job: CronJob = serde_json::from_str(json).unwrap();
        assert!(job.enabled); // default true
        assert_eq!(job.schedule.kind, ScheduleKind::Every);
        assert_eq!(job.payload.kind, PayloadKind::AgentTurn);
        assert!(!job.delete_after_run);
    }

    #[test]
    fn job_state_with_error() {
        let now = Utc::now();
        let state = CronJobState {
            next_run_at: Some(now),
            last_run_at: Some(now),
            last_status: Some(JobStatus::Error),
            last_error: Some("connection refused".into()),
        };
        let json = serde_json::to_string(&state).unwrap();
        let restored: CronJobState = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.last_status, Some(JobStatus::Error));
        assert_eq!(restored.last_error.as_deref(), Some("connection refused"));
    }

    #[test]
    fn backward_compat_ms_timestamps() {
        // Legacy format: millisecond integers.
        let json = r#"{
            "id": "legacy-1",
            "name": "old-job",
            "created_at": 1700000000000,
            "updated_at": 1700000000000,
            "state": {
                "next_run_at": 1700000100000,
                "last_run_at": 1700000000000
            }
        }"#;
        let job: CronJob = serde_json::from_str(json).unwrap();
        assert_eq!(job.id, "legacy-1");
        assert_eq!(job.created_at.timestamp_millis(), 1_700_000_000_000);
        assert!(job.state.next_run_at.is_some());
        assert!(job.state.last_run_at.is_some());
    }

    #[test]
    fn backward_compat_legacy_field_names() {
        // Legacy JSONL may have old field names with _ms suffix.
        // These will be ignored by the new struct (fields renamed).
        // This test verifies the new fields parse from their new names.
        let json = r#"{
            "id": "j1",
            "name": "test",
            "created_at": "2026-01-01T00:00:00Z",
            "updated_at": "2026-01-01T00:00:00Z"
        }"#;
        let job: CronJob = serde_json::from_str(json).unwrap();
        assert_eq!(job.created_at, Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap());
    }
}