iron-core 0.1.37

Core AgentIron loop, session state, and tool registry
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
//! Linux crontab host scheduler adapter.
//!
//! Uses the `crontab` command to read and write the user's crontab. Owned
//! entries are managed via marker-delimited blocks (see `crontab` module).
//! Non-owned content is preserved.
//!
//! The cron expression maps directly to crontab's native five-field syntax,
//! so no compilation or expansion is needed — the schedule text is used
//! as-is.

use async_trait::async_trait;
use tokio::sync::Mutex;

use crate::scheduled_task::host::{
    render_cron_command, CommandRunner, HostInstallRequest, HostScheduler, HostSchedulerError,
    ObservedHostEntry,
};
use crate::scheduled_task::platform::crontab::{make_block, CronOwnedBlock, ParsedCrontab};

/// Linux crontab host scheduler.
pub struct CronHostScheduler {
    runner: Box<dyn CommandRunner>,
    lock: Mutex<()>,
}

impl CronHostScheduler {
    /// Create a new cron adapter with the given command runner.
    pub fn new(runner: Box<dyn CommandRunner>) -> Self {
        Self {
            runner,
            lock: Mutex::new(()),
        }
    }

    /// Read the current crontab text. Returns empty string if no crontab.
    async fn read_crontab(&self) -> Result<String, HostSchedulerError> {
        let output = self
            .runner
            .run("crontab", &["-l"])
            .await
            .map_err(|e| HostSchedulerError::Io(e.to_string()))?;

        if output.exit_code == 0 {
            return Ok(output.stdout);
        }

        // crontab -l exits 1 with "no crontab" on stderr when the user has no
        // crontab. Only that case is treated as an empty crontab. Any other
        // non-zero exit (permission denied, command not found, etc.) is a real
        // failure and must propagate rather than silently erasing the crontab.
        if output.exit_code == 1 && output.stderr.contains("no crontab") {
            return Ok(String::new());
        }

        Err(HostSchedulerError::Io(format!(
            "crontab -l failed (exit {}): {}",
            output.exit_code, output.stderr
        )))
    }

    /// Write the full crontab text via stdin.
    async fn write_crontab(&self, content: &str) -> Result<(), HostSchedulerError> {
        let output = self
            .runner
            .run_with_stdin("crontab", &["-"], content)
            .await
            .map_err(|e| HostSchedulerError::Io(e.to_string()))?;

        if output.exit_code != 0 {
            return Err(HostSchedulerError::Io(format!(
                "crontab install failed (exit {}): {}",
                output.exit_code, output.stderr
            )));
        }

        Ok(())
    }

    /// Parse the current crontab.
    async fn parse_current(&self) -> Result<ParsedCrontab, HostSchedulerError> {
        let raw = self.read_crontab().await?;
        Ok(ParsedCrontab::parse(&raw))
    }

    /// Convert an owned block to an observed host entry.
    fn block_to_observed(block: &CronOwnedBlock) -> ObservedHostEntry {
        ObservedHostEntry {
            schedule_id: block.schedule_id.clone(),
            enabled: block.enabled,
            corrupt: false,
            raw_schedule: Some(block.cron_schedule.clone()),
            observed_command: Some(block.command.clone()),
            metadata: None,
        }
    }
}

#[async_trait]
impl HostScheduler for CronHostScheduler {
    fn platform(&self) -> &'static str {
        "cron"
    }

    async fn install(&self, request: &HostInstallRequest) -> Result<(), HostSchedulerError> {
        let _guard = self.lock.lock().await;
        let mut parsed = self.parse_current().await?;

        // The cron expression maps directly to crontab syntax. Escape `%`
        // for cron's metacharacter handling.
        let command = render_cron_command(&request.program, &request.args);
        let block = make_block(
            &request.schedule_id,
            request.cron.as_str(),
            &command,
            request.enabled,
        );

        parsed.upsert(block);

        let rendered = parsed.render();
        self.write_crontab(&rendered).await
    }

    async fn remove(&self, schedule_id: &str) -> Result<(), HostSchedulerError> {
        let _guard = self.lock.lock().await;
        let mut parsed = self.parse_current().await?;
        parsed.remove(schedule_id);
        let rendered = parsed.render();
        self.write_crontab(&rendered).await
    }

    async fn list_owned(&self) -> Result<Vec<ObservedHostEntry>, HostSchedulerError> {
        let parsed = self.parse_current().await?;
        let mut entries: Vec<ObservedHostEntry> = parsed
            .owned_blocks()
            .iter()
            .map(|b| Self::block_to_observed(b))
            .collect();
        // Expose malformed owned blocks so reconciliation can discover
        // corrupted AgentIron entries.
        for (schedule_id, _raw) in parsed.malformed_blocks() {
            entries.push(ObservedHostEntry {
                schedule_id: schedule_id.clone(),
                enabled: false,
                corrupt: true,
                raw_schedule: None,
                observed_command: None,
                metadata: None,
            });
        }
        Ok(entries)
    }

    async fn inspect(
        &self,
        schedule_id: &str,
    ) -> Result<Option<ObservedHostEntry>, HostSchedulerError> {
        let parsed = self.parse_current().await?;

        if let Some(block) = parsed.find_owned(schedule_id) {
            return Ok(Some(Self::block_to_observed(block)));
        }

        // Check for malformed blocks.
        if parsed.contains_id(schedule_id) {
            return Ok(Some(ObservedHostEntry {
                schedule_id: schedule_id.to_string(),
                enabled: false,
                corrupt: true,
                raw_schedule: None,
                observed_command: None,
                metadata: None,
            }));
        }

        Ok(None)
    }
}

// ============================================================================
// Mock command runner for testing
// ============================================================================

/// Mock command runner that simulates crontab interactions.
#[cfg(test)]
struct MockCrontabRunner {
    state: parking_lot::Mutex<MockCrontabState>,
}

/// Simulated crontab state for the mock runner.
#[cfg(test)]
#[derive(Clone)]
enum MockCrontabState {
    /// Active crontab with content.
    Content(String),
    /// No crontab for the user (exit 1, "no crontab").
    Absent,
    /// `crontab -l` fails with the given exit code and stderr. Used to
    /// simulate permission errors and other non-"no crontab" failures.
    ReadError { exit_code: i32, stderr: String },
}

#[cfg(test)]
use crate::scheduled_task::cron::CronExpression;
#[cfg(test)]
use crate::scheduled_task::host::CommandOutput;

#[cfg(test)]
impl MockCrontabRunner {
    fn new(initial: Option<&str>) -> Self {
        let state = match initial {
            Some(text) => MockCrontabState::Content(text.to_string()),
            None => MockCrontabState::Absent,
        };
        Self {
            state: parking_lot::Mutex::new(state),
        }
    }

    /// Create a runner where `crontab -l` fails with the given exit code
    /// and stderr (e.g. a permission error with exit code 2).
    fn with_read_error(exit_code: i32, stderr: &str) -> Self {
        Self {
            state: parking_lot::Mutex::new(MockCrontabState::ReadError {
                exit_code,
                stderr: stderr.to_string(),
            }),
        }
    }
}

#[cfg(test)]
#[async_trait]
impl CommandRunner for MockCrontabRunner {
    async fn run(&self, program: &str, args: &[&str]) -> Result<CommandOutput, std::io::Error> {
        assert_eq!(program, "crontab");

        match args {
            ["-l"] => {
                let state = self.state.lock().clone();
                match state {
                    MockCrontabState::Content(text) => Ok(CommandOutput {
                        exit_code: 0,
                        stdout: text,
                        stderr: String::new(),
                    }),
                    MockCrontabState::Absent => Ok(CommandOutput {
                        exit_code: 1,
                        stdout: String::new(),
                        stderr: "no crontab for user".to_string(),
                    }),
                    MockCrontabState::ReadError { exit_code, stderr } => Ok(CommandOutput {
                        exit_code,
                        stdout: String::new(),
                        stderr,
                    }),
                }
            }
            _ => Ok(CommandOutput {
                exit_code: 1,
                stdout: String::new(),
                stderr: "unknown args".to_string(),
            }),
        }
    }

    async fn run_with_stdin(
        &self,
        program: &str,
        args: &[&str],
        stdin: &str,
    ) -> Result<CommandOutput, std::io::Error> {
        assert_eq!(program, "crontab");
        assert_eq!(args, &["-"]);

        *self.state.lock() = MockCrontabState::Content(stdin.to_string());

        Ok(CommandOutput {
            exit_code: 0,
            stdout: String::new(),
            stderr: String::new(),
        })
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::scheduled_task::host::SchedulerInstallContext;

    fn make_request(id: &str, cron: &str, enabled: bool) -> HostInstallRequest {
        let ctx = SchedulerInstallContext {
            runner_executable: std::path::PathBuf::from("/usr/local/bin/agent-iron"),
            config_store_path: std::path::PathBuf::from("/home/user/.config/agentiron/config.db"),
        };
        let (program, args) = ctx.generate_invocation("task-1");
        HostInstallRequest {
            schedule_id: id.to_string(),
            automation_task_id: "task-1".to_string(),
            cron: CronExpression::parse(cron).unwrap(),
            enabled,
            program,
            args,
        }
    }

    #[tokio::test]
    async fn install_into_empty_crontab() {
        let runner = MockCrontabRunner::new(None);
        let scheduler = CronHostScheduler::new(Box::new(runner));

        let request = make_request("s1", "0 9 * * *", true);
        scheduler.install(&request).await.unwrap();

        // Read back and verify.
        let entry = scheduler.inspect("s1").await.unwrap().unwrap();
        assert_eq!(entry.schedule_id, "s1");
        assert!(entry.enabled);
        assert_eq!(entry.raw_schedule.as_deref(), Some("0 9 * * *"));
        assert!(entry
            .observed_command
            .as_deref()
            .unwrap()
            .contains("run task-1"));
    }

    #[tokio::test]
    async fn install_beside_existing_entries() {
        let initial = "# My crontab\n0 0 * * * /usr/bin/cleanup\n";
        let runner = MockCrontabRunner::new(Some(initial));
        let scheduler = CronHostScheduler::new(Box::new(runner));

        let request = make_request("s1", "0 9 * * *", true);
        scheduler.install(&request).await.unwrap();

        // List should only show owned entries.
        let owned = scheduler.list_owned().await.unwrap();
        assert_eq!(owned.len(), 1);
        assert_eq!(owned[0].schedule_id, "s1");
    }

    #[tokio::test]
    async fn install_disabled_task() {
        let runner = MockCrontabRunner::new(None);
        let scheduler = CronHostScheduler::new(Box::new(runner));

        let request = make_request("s1", "0 9 * * *", false);
        scheduler.install(&request).await.unwrap();

        let entry = scheduler.inspect("s1").await.unwrap().unwrap();
        assert!(!entry.enabled);
    }

    #[tokio::test]
    async fn replace_existing_block() {
        let runner = MockCrontabRunner::new(None);
        let scheduler = CronHostScheduler::new(Box::new(runner));

        // Install initial.
        scheduler
            .install(&make_request("s1", "0 9 * * *", true))
            .await
            .unwrap();

        // Replace with different schedule.
        scheduler
            .install(&make_request("s1", "0 10 * * *", true))
            .await
            .unwrap();

        let entry = scheduler.inspect("s1").await.unwrap().unwrap();
        assert_eq!(entry.raw_schedule.as_deref(), Some("0 10 * * *"));
    }

    #[tokio::test]
    async fn remove_block() {
        let runner = MockCrontabRunner::new(None);
        let scheduler = CronHostScheduler::new(Box::new(runner));

        scheduler
            .install(&make_request("s1", "0 9 * * *", true))
            .await
            .unwrap();
        scheduler
            .install(&make_request("s2", "0 10 * * *", true))
            .await
            .unwrap();

        scheduler.remove("s1").await.unwrap();

        assert!(scheduler.inspect("s1").await.unwrap().is_none());
        assert!(scheduler.inspect("s2").await.unwrap().is_some());
    }

    #[tokio::test]
    async fn list_multiple_owned() {
        let runner = MockCrontabRunner::new(None);
        let scheduler = CronHostScheduler::new(Box::new(runner));

        for (id, cron) in &[("alpha", "0 9 * * *"), ("bravo", "0 10 * * *")] {
            scheduler
                .install(&make_request(id, cron, true))
                .await
                .unwrap();
        }

        let owned = scheduler.list_owned().await.unwrap();
        assert_eq!(owned.len(), 2);
    }

    #[tokio::test]
    async fn inspect_nonexistent_returns_none() {
        let runner = MockCrontabRunner::new(None);
        let scheduler = CronHostScheduler::new(Box::new(runner));

        let result = scheduler.inspect("nonexistent").await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn faithful_cron_compilation() {
        // On Linux, the cron expression maps directly to crontab syntax.
        // No transformation is needed.
        let runner = MockCrontabRunner::new(None);
        let scheduler = CronHostScheduler::new(Box::new(runner));

        let complex = "*/15 9-17/2 1,15 * 1-5";
        scheduler
            .install(&make_request("s1", complex, true))
            .await
            .unwrap();

        let entry = scheduler.inspect("s1").await.unwrap().unwrap();
        assert_eq!(entry.raw_schedule.as_deref(), Some(complex));
    }

    #[tokio::test]
    async fn enable_after_disabled() {
        let runner = MockCrontabRunner::new(None);
        let scheduler = CronHostScheduler::new(Box::new(runner));

        // Install disabled.
        scheduler
            .install(&make_request("s1", "0 9 * * *", false))
            .await
            .unwrap();

        // Replace with enabled.
        scheduler
            .install(&make_request("s1", "0 9 * * *", true))
            .await
            .unwrap();

        let entry = scheduler.inspect("s1").await.unwrap().unwrap();
        assert!(entry.enabled);
    }

    #[tokio::test]
    async fn read_permission_error_propagates() {
        // Exit code 2 (e.g. permission denied) must not be treated as an empty
        // crontab; it must propagate as an error.
        let runner = MockCrontabRunner::with_read_error(2, "crontab: permission denied");
        let scheduler = CronHostScheduler::new(Box::new(runner));

        let result = scheduler.list_owned().await;
        assert!(result.is_err());
        assert!(matches!(result, Err(HostSchedulerError::Io(_))));
    }
}