edgecrab-core 0.4.0

Agent core: conversation loop, prompt builder, context compression, model routing
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! # Tool Result Spill-to-Artifact
//!
//! When a tool result exceeds a configurable byte threshold, the full
//! output is written to a session-scoped artifact file on disk and only
//! a compact **head preview + metadata stub** is injected into the
//! conversation message history.
//!
//! ## Design invariants
//!
//! 1. **Nothing is lost** — full result persisted to disk.
//! 2. **Agent retains access** — artifact under `cwd`, readable via `read_file`.
//! 3. **Zero breaking changes** — `ToolHandler` trait signature unchanged.
//! 4. **Compression-friendly** — stub is ~200 bytes vs 50KB+ originals.
//! 5. **Feature-gated** — `tools.result_spill` config flag, on by default.

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, Ordering};

/// Configuration for tool result spilling.
#[derive(Debug, Clone)]
pub struct SpillConfig {
    /// Whether spilling is enabled (gated by `tools.result_spill`).
    pub enabled: bool,
    /// Byte threshold — results strictly larger than this are spilled.
    pub threshold: usize,
    /// Number of lines to include in the preview stub.
    pub preview_lines: usize,
}

impl Default for SpillConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            threshold: 16_384, // 16 KB
            preview_lines: 80,
        }
    }
}

/// Outcome of a spill attempt.
#[derive(Debug)]
pub enum SpillOutcome {
    /// Result was small enough — use it as-is.
    Inline(String),
    /// Result was spilled to an artifact file.
    Spilled {
        /// The stub message to inject into session.messages.
        stub: String,
        /// Absolute path of the artifact file on disk.
        artifact_path: PathBuf,
        /// Original byte length of the full result.
        original_bytes: usize,
        /// Original line count of the full result.
        original_lines: usize,
        /// Number of preview lines included in the stub.
        preview_line_count: usize,
    },
}

/// Per-session atomic sequence counter for artifact filenames.
///
/// Thread-safe across parallel tool calls within one conversation.
pub struct SpillSequence(AtomicU32);

impl SpillSequence {
    pub fn new() -> Self {
        Self(AtomicU32::new(1))
    }

    pub fn next(&self) -> u32 {
        self.0.fetch_add(1, Ordering::Relaxed)
    }
}

impl Default for SpillSequence {
    fn default() -> Self {
        Self::new()
    }
}

/// Attempt to spill a tool result to an artifact file.
///
/// Returns `SpillOutcome::Inline` if spilling is disabled, the result is
/// small enough, or writing fails (graceful degradation).
pub fn maybe_spill(
    tool_name: &str,
    _tool_call_id: &str,
    result: String,
    session_id: &str,
    cwd: &Path,
    config: &SpillConfig,
    seq: &SpillSequence,
) -> SpillOutcome {
    // Gate check
    if !config.enabled {
        return SpillOutcome::Inline(result);
    }

    // Size check — strictly greater than threshold
    if result.len() <= config.threshold {
        return SpillOutcome::Inline(result);
    }

    // Compute artifact directory and path
    let safe_name = sanitize_tool_name(tool_name);
    let seq_num = seq.next();
    let artifact_dir = artifact_dir_for_session(cwd, session_id);
    let filename = format!("{safe_name}_{seq_num:03}.md");
    let artifact_path = artifact_dir.join(&filename);

    // Attempt to write — on failure, fall back to inline
    if let Err(e) = write_artifact(&artifact_dir, &artifact_path, &result) {
        tracing::warn!(
            tool = %tool_name,
            path = %artifact_path.display(),
            error = %e,
            "tool result spill failed, falling back to inline"
        );
        return SpillOutcome::Inline(result);
    }

    // Ensure .gitignore covers the artifact directory
    ensure_gitignore(cwd);

    // Build the stub
    let original_bytes = result.len();
    let original_lines = result.lines().count().max(1);
    let (preview, preview_line_count) = build_preview(&result, config.preview_lines);

    // Compute relative path for display (agent uses read_file with relative paths)
    let rel_path = artifact_path.strip_prefix(cwd).unwrap_or(&artifact_path);

    let pct = if original_lines > 0 {
        (preview_line_count as f64 / original_lines as f64 * 100.0).round() as usize
    } else {
        100
    };

    let stub = format!(
        "[tool_result_spill]\n\
         tool: {tool_name}\n\
         lines: {original_lines}\n\
         bytes: {original_bytes}\n\
         artifact: {rel}\n\
         showing: {preview_line_count}/{original_lines} lines (first {pct}%)\n\
         \n\
         --- BEGIN PREVIEW ({preview_line_count} lines) ---\n\
         {preview}\n\
         --- END PREVIEW ---\n\
         \n\
         Full result saved to: {rel}\n\
         Use read_file or file_search to explore the full content.",
        rel = rel_path.display(),
    );

    tracing::info!(
        tool = %tool_name,
        original_bytes,
        original_lines,
        preview_lines = preview_line_count,
        artifact = %rel_path.display(),
        "tool result spilled to artifact"
    );

    SpillOutcome::Spilled {
        stub,
        artifact_path,
        original_bytes,
        original_lines,
        preview_line_count,
    }
}

// ─── Internal helpers ────────────────────────────────────────────────

/// Sanitize a tool name for use as a filename component.
/// Only allow alphanumeric chars and underscores; replace everything else.
fn sanitize_tool_name(name: &str) -> String {
    name.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

/// Compute the artifact directory path for a session.
fn artifact_dir_for_session(cwd: &Path, session_id: &str) -> PathBuf {
    // Sanitize session_id for directory name safety
    let safe_session: String = session_id
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect();
    cwd.join(".edgecrab-artifacts").join(safe_session)
}

/// Write the full result to an artifact file atomically.
fn write_artifact(dir: &Path, path: &Path, content: &str) -> std::io::Result<()> {
    std::fs::create_dir_all(dir)?;
    std::fs::write(path, content)?;
    Ok(())
}

/// Build a preview from the first N lines of the result.
///
/// Returns (preview_text, actual_line_count_shown).
fn build_preview(result: &str, max_lines: usize) -> (String, usize) {
    let lines: Vec<&str> = result.lines().take(max_lines).collect();
    let count = lines.len();
    (lines.join("\n"), count)
}

/// Ensure `.gitignore` in `cwd` contains `.edgecrab-artifacts/`.
///
/// Best-effort: if the file can't be read or written, we silently skip.
fn ensure_gitignore(cwd: &Path) {
    let gitignore_path = cwd.join(".gitignore");
    let entry = ".edgecrab-artifacts/";

    // Read existing content (or empty if no .gitignore)
    let existing = std::fs::read_to_string(&gitignore_path).unwrap_or_default();

    // Check if already present (line-by-line to avoid substring false positives)
    if existing.lines().any(|line| line.trim() == entry) {
        return;
    }

    // Append the entry
    let addition = if existing.is_empty() || existing.ends_with('\n') {
        format!("{entry}\n")
    } else {
        format!("\n{entry}\n")
    };

    if let Err(e) = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&gitignore_path)
        .and_then(|mut f| std::io::Write::write_all(&mut f, addition.as_bytes()))
    {
        tracing::debug!(error = %e, "could not update .gitignore with artifact entry");
    }
}

// ─── Tests ───────────────────────────────────────────────────────────

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

    fn test_config(enabled: bool, threshold: usize, preview_lines: usize) -> SpillConfig {
        SpillConfig {
            enabled,
            threshold,
            preview_lines,
        }
    }

    #[test]
    fn inline_when_disabled() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(false, 10, 5);
        let result = "a".repeat(1000);

        match maybe_spill(
            "test",
            "tc1",
            result.clone(),
            "ses1",
            tmp.path(),
            &config,
            &seq,
        ) {
            SpillOutcome::Inline(s) => assert_eq!(s, result),
            SpillOutcome::Spilled { .. } => panic!("should not spill when disabled"),
        }
    }

    #[test]
    fn inline_when_under_threshold() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 1000, 5);
        let result = "small result".to_string();

        match maybe_spill(
            "test",
            "tc1",
            result.clone(),
            "ses1",
            tmp.path(),
            &config,
            &seq,
        ) {
            SpillOutcome::Inline(s) => assert_eq!(s, result),
            SpillOutcome::Spilled { .. } => panic!("should not spill under threshold"),
        }
    }

    #[test]
    fn inline_when_exactly_at_threshold() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 100, 5);
        let result = "x".repeat(100); // exactly at threshold — no spill

        match maybe_spill(
            "test",
            "tc1",
            result.clone(),
            "ses1",
            tmp.path(),
            &config,
            &seq,
        ) {
            SpillOutcome::Inline(s) => assert_eq!(s, result),
            SpillOutcome::Spilled { .. } => panic!("should not spill at exact threshold"),
        }
    }

    #[test]
    fn spills_when_over_threshold() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 100, 5);
        let lines: Vec<String> = (1..=50).map(|i| format!("line {i}")).collect();
        let result = lines.join("\n");
        assert!(result.len() > 100);

        match maybe_spill(
            "file_search",
            "tc1",
            result.clone(),
            "ses1",
            tmp.path(),
            &config,
            &seq,
        ) {
            SpillOutcome::Spilled {
                stub,
                artifact_path,
                original_bytes,
                original_lines,
                preview_line_count,
            } => {
                assert_eq!(original_bytes, result.len());
                assert_eq!(original_lines, 50);
                assert_eq!(preview_line_count, 5);
                assert!(stub.contains("[tool_result_spill]"));
                assert!(stub.contains("tool: file_search"));
                assert!(stub.contains("lines: 50"));
                assert!(stub.contains("showing: 5/50 lines"));
                assert!(stub.contains("line 1"));
                assert!(stub.contains("line 5"));
                assert!(!stub.contains("line 6"));
                assert!(stub.contains("--- BEGIN PREVIEW"));
                assert!(stub.contains("--- END PREVIEW"));

                // Verify artifact file was written
                assert!(artifact_path.exists());
                let written = std::fs::read_to_string(&artifact_path).expect("read artifact");
                assert_eq!(written, result);
            }
            SpillOutcome::Inline(_) => panic!("should spill over threshold"),
        }
    }

    #[test]
    fn artifact_path_structure() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 10, 5);
        let result = "a".repeat(20);

        match maybe_spill(
            "my_tool",
            "tc1",
            result,
            "ses-abc123",
            tmp.path(),
            &config,
            &seq,
        ) {
            SpillOutcome::Spilled { artifact_path, .. } => {
                let rel = artifact_path
                    .strip_prefix(tmp.path())
                    .expect("relative path");
                assert_eq!(
                    rel,
                    Path::new(".edgecrab-artifacts/ses-abc123/my_tool_001.md")
                );
            }
            SpillOutcome::Inline(_) => panic!("should spill"),
        }
    }

    #[test]
    fn sequence_counter_increments() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 10, 5);

        for i in 1..=3 {
            let result = "a".repeat(20);
            match maybe_spill("tool", "tc1", result, "ses1", tmp.path(), &config, &seq) {
                SpillOutcome::Spilled { artifact_path, .. } => {
                    let name = artifact_path
                        .file_name()
                        .expect("filename")
                        .to_str()
                        .expect("utf8");
                    assert_eq!(name, format!("tool_{i:03}.md"));
                }
                SpillOutcome::Inline(_) => panic!("should spill"),
            }
        }
    }

    #[test]
    fn sanitize_tool_name_handles_special_chars() {
        assert_eq!(sanitize_tool_name("file_read"), "file_read");
        assert_eq!(sanitize_tool_name("mcp-server:tool"), "mcp_server_tool");
        assert_eq!(sanitize_tool_name("my.tool/v2"), "my_tool_v2");
        assert_eq!(sanitize_tool_name(""), "");
    }

    #[test]
    fn sanitize_session_id_in_path() {
        let dir = artifact_dir_for_session(Path::new("/tmp"), "ses/../../etc/passwd");
        // No path traversal — slashes become underscores
        assert!(dir.to_str().expect("utf8").contains("ses____"));
        assert!(!dir.to_str().expect("utf8").contains("etc/passwd"));
    }

    #[test]
    fn preview_with_fewer_lines_than_max() {
        let (preview, count) = build_preview("line 1\nline 2\nline 3", 10);
        assert_eq!(count, 3);
        assert_eq!(preview, "line 1\nline 2\nline 3");
    }

    #[test]
    fn preview_limits_to_max_lines() {
        let input: String = (1..=100)
            .map(|i| format!("line {i}"))
            .collect::<Vec<_>>()
            .join("\n");
        let (preview, count) = build_preview(&input, 5);
        assert_eq!(count, 5);
        assert!(preview.contains("line 1"));
        assert!(preview.contains("line 5"));
        assert!(!preview.contains("line 6"));
    }

    #[test]
    fn single_line_result_spills() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 10, 5);
        let result = "a".repeat(50); // single long line, no newlines

        match maybe_spill(
            "tool",
            "tc1",
            result.clone(),
            "ses1",
            tmp.path(),
            &config,
            &seq,
        ) {
            SpillOutcome::Spilled {
                original_lines,
                preview_line_count,
                stub,
                ..
            } => {
                assert_eq!(original_lines, 1);
                assert_eq!(preview_line_count, 1);
                assert!(stub.contains("showing: 1/1 lines"));
            }
            SpillOutcome::Inline(_) => panic!("should spill"),
        }
    }

    #[test]
    fn gitignore_is_created_on_first_spill() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 10, 5);
        let result = "a".repeat(20);

        let _ = maybe_spill("tool", "tc1", result, "ses1", tmp.path(), &config, &seq);

        let gitignore =
            std::fs::read_to_string(tmp.path().join(".gitignore")).expect("read .gitignore");
        assert!(gitignore.contains(".edgecrab-artifacts/"));
    }

    #[test]
    fn gitignore_not_duplicated() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 10, 5);

        // Write initial .gitignore with existing entry
        std::fs::write(tmp.path().join(".gitignore"), ".edgecrab-artifacts/\n").expect("write");

        let result = "a".repeat(20);
        let _ = maybe_spill("tool", "tc1", result, "ses1", tmp.path(), &config, &seq);

        let gitignore = std::fs::read_to_string(tmp.path().join(".gitignore")).expect("read");
        // Should appear exactly once
        assert_eq!(gitignore.matches(".edgecrab-artifacts/").count(), 1);
    }

    #[test]
    fn unicode_result_spills_safely() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 10, 5);
        // Multi-byte chars: each emoji is 4 bytes
        let result = "🦀".repeat(10); // 40 bytes

        match maybe_spill(
            "tool",
            "tc1",
            result.clone(),
            "ses1",
            tmp.path(),
            &config,
            &seq,
        ) {
            SpillOutcome::Spilled { artifact_path, .. } => {
                let written = std::fs::read_to_string(&artifact_path).expect("read");
                assert_eq!(written, result);
            }
            SpillOutcome::Inline(_) => panic!("should spill"),
        }
    }

    #[test]
    fn empty_result_never_spills() {
        let tmp = TempDir::new().expect("tempdir");
        let seq = SpillSequence::new();
        let config = test_config(true, 0, 5); // threshold=0, but empty string has len 0

        match maybe_spill(
            "tool",
            "tc1",
            String::new(),
            "ses1",
            tmp.path(),
            &config,
            &seq,
        ) {
            SpillOutcome::Inline(s) => assert!(s.is_empty()),
            SpillOutcome::Spilled { .. } => panic!("empty should not spill"),
        }
    }

    #[test]
    fn concurrent_sequence_numbers_are_unique() {
        let seq = SpillSequence::new();
        let mut seen = std::collections::HashSet::new();
        for _ in 0..100 {
            let n = seq.next();
            assert!(seen.insert(n), "duplicate sequence number: {n}");
        }
    }
}