agent-air-runtime 0.7.0

Core runtime for agent-air - LLM orchestration, tools, and permissions (no TUI dependencies)
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! MarkdownPlan tool implementation.
//!
//! Creates or updates a durable markdown plan file in `.agent-air/plans/`.
//! Each plan has a sequential ID, a title, an overall status, and numbered
//! steps with optional notes. All steps start as pending (`[ ]`).
//!
//! Plan files are internal agent artifacts so this tool handles its own
//! permissions and never prompts the user.

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use chrono::Utc;
use tokio::fs;

use super::plan_store::PlanStore;
use super::types::{
    DisplayConfig, DisplayResult, Executable, ResultContentType, ToolContext, ToolType,
};

/// MarkdownPlan tool name constant.
pub const MARKDOWN_PLAN_TOOL_NAME: &str = "markdown_plan";

/// MarkdownPlan tool description constant.
pub const MARKDOWN_PLAN_TOOL_DESCRIPTION: &str = r#"Creates or updates a durable markdown plan file in the workspace.

Usage:
- Omit plan_id to create a new plan (an ID will be generated automatically)
- Provide plan_id to overwrite an existing plan
- Each step starts as pending ([ ])
- The plan file is written to .agent-air/plans/ in the workspace root

Returns:
- The plan ID, file path, and rendered markdown content"#;

/// MarkdownPlan tool JSON schema constant.
pub const MARKDOWN_PLAN_TOOL_SCHEMA: &str = r#"{
    "type": "object",
    "properties": {
        "plan_id": {
            "type": "string",
            "description": "Plan ID to update. Omit to create a new plan."
        },
        "title": {
            "type": "string",
            "description": "Plan title"
        },
        "steps": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "description": {
                        "type": "string"
                    },
                    "notes": {
                        "type": "string"
                    }
                },
                "required": ["description"]
            }
        },
        "status": {
            "type": "string",
            "enum": ["draft", "active", "completed", "abandoned"]
        }
    },
    "required": ["title", "steps"]
}"#;

/// Tool that creates or updates markdown plan files.
///
/// Plan files live in `.agent-air/plans/` and are internal agent artifacts,
/// so no user permission is required.
pub struct MarkdownPlanTool {
    /// Shared plan store for directory paths and file locking.
    plan_store: Arc<PlanStore>,
}

impl MarkdownPlanTool {
    /// Create a new MarkdownPlanTool.
    pub fn new(plan_store: Arc<PlanStore>) -> Self {
        Self { plan_store }
    }

    /// Renders the markdown content for a plan.
    fn generate_markdown(title: &str, plan_id: &str, status: &str, steps: &[PlanStep]) -> String {
        let date = Utc::now().format("%Y-%m-%d");
        let mut md = format!(
            "# Plan: {}\n\n**ID**: {}\n**Status**: {}\n**Created**: {}\n\n## Steps\n\n",
            title, plan_id, status, date
        );

        for (i, step) in steps.iter().enumerate() {
            md.push_str(&format!("{}. [ ] {}\n", i + 1, step.description));
            if let Some(ref notes) = step.notes {
                md.push_str(&format!("   Notes: {}\n", notes));
            }
        }

        md
    }
}

/// A single step in a plan, parsed from input.
struct PlanStep {
    description: String,
    notes: Option<String>,
}

impl Executable for MarkdownPlanTool {
    fn name(&self) -> &str {
        MARKDOWN_PLAN_TOOL_NAME
    }

    fn description(&self) -> &str {
        MARKDOWN_PLAN_TOOL_DESCRIPTION
    }

    fn input_schema(&self) -> &str {
        MARKDOWN_PLAN_TOOL_SCHEMA
    }

    fn tool_type(&self) -> ToolType {
        ToolType::Custom
    }

    fn execute(
        &self,
        _context: ToolContext,
        input: HashMap<String, serde_json::Value>,
    ) -> Pin<Box<dyn Future<Output = Result<String, String>> + Send>> {
        let plan_store = self.plan_store.clone();

        Box::pin(async move {
            // Step 1: Extract and validate parameters.
            let title = input
                .get("title")
                .and_then(|v| v.as_str())
                .ok_or_else(|| "Missing required 'title' parameter".to_string())?;

            let steps_value = input
                .get("steps")
                .and_then(|v| v.as_array())
                .ok_or_else(|| "Missing required 'steps' parameter".to_string())?;

            if steps_value.is_empty() {
                return Err("'steps' array must not be empty".to_string());
            }

            let status = input
                .get("status")
                .and_then(|v| v.as_str())
                .unwrap_or("draft");

            // Parse steps from JSON array.
            let mut steps = Vec::with_capacity(steps_value.len());
            for (i, step_val) in steps_value.iter().enumerate() {
                let description = step_val
                    .get("description")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| {
                        format!("Step {} is missing required 'description' field", i + 1)
                    })?;
                let notes = step_val
                    .get("notes")
                    .and_then(|v| v.as_str())
                    .map(String::from);
                steps.push(PlanStep {
                    description: description.to_string(),
                    notes,
                });
            }

            // Step 2: Determine plan ID.
            let plan_id = match input.get("plan_id").and_then(|v| v.as_str()) {
                Some(id) => id.to_string(),
                None => plan_store.get_next_plan_id().await?,
            };

            // Step 3: Build file path.
            let plans_dir = plan_store.plans_dir();
            let file_name = format!("{}.md", plan_id);
            let file_path = plans_dir.join(&file_name);
            let file_path_str = file_path.to_string_lossy().to_string();

            // Step 4: Acquire per-file lock.
            let lock = plan_store.acquire_lock(&file_path).await;
            let _guard = lock.lock().await;

            // Step 5: Ensure the plans directory exists.
            fs::create_dir_all(plans_dir)
                .await
                .map_err(|e| format!("Failed to create plans directory: {}", e))?;

            // Step 6: Generate and write the markdown.
            let markdown = Self::generate_markdown(title, &plan_id, status, &steps);
            fs::write(&file_path, &markdown)
                .await
                .map_err(|e| format!("Failed to write plan file: {}", e))?;

            Ok(format!(
                "Plan '{}' saved to {}\n\n{}",
                plan_id, file_path_str, markdown
            ))
        })
    }

    fn handles_own_permissions(&self) -> bool {
        true
    }

    fn display_config(&self) -> DisplayConfig {
        DisplayConfig {
            display_name: "Markdown Plan".to_string(),
            display_title: Box::new(|input| {
                input
                    .get("plan_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("new plan")
                    .to_string()
            }),
            display_content: Box::new(|_input, result| {
                let lines: Vec<&str> = result.lines().take(15).collect();
                let total_lines = result.lines().count();
                let truncated = total_lines > 15;
                let content = if truncated {
                    format!("{}...\n[truncated]", lines.join("\n"))
                } else {
                    lines.join("\n")
                };

                DisplayResult {
                    content,
                    content_type: ResultContentType::Markdown,
                    is_truncated: truncated,
                    full_length: total_lines,
                }
            }),
        }
    }

    fn compact_summary(&self, input: &HashMap<String, serde_json::Value>, _result: &str) -> String {
        let plan_id = input
            .get("plan_id")
            .and_then(|v| v.as_str())
            .unwrap_or("new");

        let step_count = input
            .get("steps")
            .and_then(|v| v.as_array())
            .map(|a| a.len())
            .unwrap_or(0);

        let status = input
            .get("status")
            .and_then(|v| v.as_str())
            .unwrap_or("draft");

        format!(
            "[MarkdownPlan: {} ({} steps, {})]",
            plan_id, step_count, status
        )
    }
}

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

    fn make_steps(descriptions: &[&str]) -> serde_json::Value {
        let steps: Vec<serde_json::Value> = descriptions
            .iter()
            .map(|d| serde_json::json!({ "description": d }))
            .collect();
        serde_json::Value::Array(steps)
    }

    fn make_steps_with_notes(items: &[(&str, Option<&str>)]) -> serde_json::Value {
        let steps: Vec<serde_json::Value> = items
            .iter()
            .map(|(desc, notes)| {
                let mut step = serde_json::json!({ "description": desc });
                if let Some(n) = notes {
                    step.as_object_mut()
                        .unwrap()
                        .insert("notes".to_string(), serde_json::json!(n));
                }
                step
            })
            .collect();
        serde_json::Value::Array(steps)
    }

    fn make_context(tool_use_id: &str) -> ToolContext {
        ToolContext {
            session_id: 1,
            tool_use_id: tool_use_id.to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        }
    }

    #[tokio::test]
    async fn test_create_new_plan() {
        let temp_dir = TempDir::new().unwrap();
        let plan_store = Arc::new(PlanStore::new(temp_dir.path().to_path_buf()));
        let tool = MarkdownPlanTool::new(plan_store);

        let mut input = HashMap::new();
        input.insert(
            "title".to_string(),
            serde_json::Value::String("My Test Plan".to_string()),
        );
        input.insert("steps".to_string(), make_steps(&["Step one", "Step two"]));

        let result = tool.execute(make_context("test-create"), input).await;
        assert!(result.is_ok());

        let output = result.unwrap();
        assert!(output.contains("plan-001"));
        assert!(output.contains("My Test Plan"));
        assert!(output.contains("1. [ ] Step one"));
        assert!(output.contains("2. [ ] Step two"));

        // Verify the file was created on disk.
        let plan_file = temp_dir.path().join(".agent-air/plans/plan-001.md");
        assert!(plan_file.exists());
    }

    #[tokio::test]
    async fn test_upsert_existing_plan() {
        let temp_dir = TempDir::new().unwrap();
        let plan_store = Arc::new(PlanStore::new(temp_dir.path().to_path_buf()));

        // Create the plans directory and an existing plan file.
        let plans_dir = temp_dir.path().join(".agent-air/plans");
        fs::create_dir_all(&plans_dir).await.unwrap();
        fs::write(plans_dir.join("plan-001.md"), "# Old plan")
            .await
            .unwrap();

        let tool = MarkdownPlanTool::new(plan_store);

        let mut input = HashMap::new();
        input.insert(
            "plan_id".to_string(),
            serde_json::Value::String("plan-001".to_string()),
        );
        input.insert(
            "title".to_string(),
            serde_json::Value::String("Updated Plan".to_string()),
        );
        input.insert("steps".to_string(), make_steps(&["New step"]));
        input.insert(
            "status".to_string(),
            serde_json::Value::String("active".to_string()),
        );

        let result = tool.execute(make_context("test-upsert"), input).await;
        assert!(result.is_ok());

        let output = result.unwrap();
        assert!(output.contains("plan-001"));
        assert!(output.contains("Updated Plan"));
        assert!(output.contains("active"));
        assert!(output.contains("1. [ ] New step"));
    }

    #[tokio::test]
    async fn test_sequential_id_generation() {
        let temp_dir = TempDir::new().unwrap();
        let plan_store = Arc::new(PlanStore::new(temp_dir.path().to_path_buf()));

        // Create plan-001.md and plan-002.md.
        let plans_dir = temp_dir.path().join(".agent-air/plans");
        fs::create_dir_all(&plans_dir).await.unwrap();
        fs::write(plans_dir.join("plan-001.md"), "").await.unwrap();
        fs::write(plans_dir.join("plan-002.md"), "").await.unwrap();

        let next_id = plan_store.get_next_plan_id().await.unwrap();
        assert_eq!(next_id, "plan-003");
    }

    #[tokio::test]
    async fn test_missing_required_fields() {
        let temp_dir = TempDir::new().unwrap();
        let plan_store = Arc::new(PlanStore::new(temp_dir.path().to_path_buf()));
        let tool = MarkdownPlanTool::new(plan_store);

        // Missing title.
        let mut input = HashMap::new();
        input.insert("steps".to_string(), make_steps(&["A step"]));

        let result = tool.execute(make_context("test-no-title"), input).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Missing required 'title'"));

        // Missing steps.
        let mut input = HashMap::new();
        input.insert(
            "title".to_string(),
            serde_json::Value::String("A Title".to_string()),
        );

        let result = tool.execute(make_context("test-no-steps"), input).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Missing required 'steps'"));
    }

    #[tokio::test]
    async fn test_default_status_is_draft() {
        let temp_dir = TempDir::new().unwrap();
        let plan_store = Arc::new(PlanStore::new(temp_dir.path().to_path_buf()));
        let tool = MarkdownPlanTool::new(plan_store);

        let mut input = HashMap::new();
        input.insert(
            "title".to_string(),
            serde_json::Value::String("Draft Plan".to_string()),
        );
        input.insert("steps".to_string(), make_steps(&["Step A"]));

        let result = tool.execute(make_context("test-draft"), input).await;
        assert!(result.is_ok());
        assert!(result.unwrap().contains("**Status**: draft"));
    }

    #[test]
    fn test_generate_markdown_format() {
        let steps = vec![
            PlanStep {
                description: "First step".to_string(),
                notes: None,
            },
            PlanStep {
                description: "Second step".to_string(),
                notes: None,
            },
        ];

        let md = MarkdownPlanTool::generate_markdown("Test Plan", "plan-001", "draft", &steps);

        assert!(md.starts_with("# Plan: Test Plan\n"));
        assert!(md.contains("**ID**: plan-001"));
        assert!(md.contains("**Status**: draft"));
        assert!(md.contains("**Created**:"));
        assert!(md.contains("## Steps"));
        assert!(md.contains("1. [ ] First step"));
        assert!(md.contains("2. [ ] Second step"));
    }

    #[test]
    fn test_steps_with_notes() {
        let steps = vec![
            PlanStep {
                description: "Step with notes".to_string(),
                notes: Some("Important context here".to_string()),
            },
            PlanStep {
                description: "Step without notes".to_string(),
                notes: None,
            },
        ];

        let md = MarkdownPlanTool::generate_markdown("Noted Plan", "plan-005", "active", &steps);

        assert!(md.contains("1. [ ] Step with notes\n   Notes: Important context here"));
        assert!(md.contains("2. [ ] Step without notes\n"));
        // The step without notes should not have a Notes: line.
        assert!(!md.contains("2. [ ] Step without notes\n   Notes:"));
    }

    #[test]
    fn test_compact_summary() {
        let plan_store = Arc::new(PlanStore::new(std::path::PathBuf::from("/tmp")));
        let tool = MarkdownPlanTool::new(plan_store);

        let mut input = HashMap::new();
        input.insert(
            "plan_id".to_string(),
            serde_json::Value::String("plan-001".to_string()),
        );
        input.insert("steps".to_string(), make_steps(&["A", "B", "C"]));
        input.insert(
            "status".to_string(),
            serde_json::Value::String("active".to_string()),
        );

        let summary = tool.compact_summary(&input, "");
        assert_eq!(summary, "[MarkdownPlan: plan-001 (3 steps, active)]");
    }

    #[test]
    fn test_compact_summary_defaults() {
        let plan_store = Arc::new(PlanStore::new(std::path::PathBuf::from("/tmp")));
        let tool = MarkdownPlanTool::new(plan_store);

        let input = HashMap::new();
        let summary = tool.compact_summary(&input, "");
        assert_eq!(summary, "[MarkdownPlan: new (0 steps, draft)]");
    }

    #[tokio::test]
    async fn test_empty_steps_rejected() {
        let temp_dir = TempDir::new().unwrap();
        let plan_store = Arc::new(PlanStore::new(temp_dir.path().to_path_buf()));
        let tool = MarkdownPlanTool::new(plan_store);

        let mut input = HashMap::new();
        input.insert(
            "title".to_string(),
            serde_json::Value::String("Empty".to_string()),
        );
        input.insert("steps".to_string(), serde_json::Value::Array(vec![]));

        let result = tool.execute(make_context("test-empty"), input).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("must not be empty"));
    }

    #[tokio::test]
    async fn test_steps_with_notes_rendered() {
        let temp_dir = TempDir::new().unwrap();
        let plan_store = Arc::new(PlanStore::new(temp_dir.path().to_path_buf()));
        let tool = MarkdownPlanTool::new(plan_store);

        let mut input = HashMap::new();
        input.insert(
            "title".to_string(),
            serde_json::Value::String("Noted".to_string()),
        );
        input.insert(
            "steps".to_string(),
            make_steps_with_notes(&[
                ("Do something", Some("Watch out for edge cases")),
                ("Do another thing", None),
            ]),
        );

        let result = tool.execute(make_context("test-notes"), input).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("Notes: Watch out for edge cases"));
    }

    #[test]
    fn test_handles_own_permissions() {
        let plan_store = Arc::new(PlanStore::new(std::path::PathBuf::from("/tmp")));
        let tool = MarkdownPlanTool::new(plan_store);
        assert!(tool.handles_own_permissions());
    }
}