nexil 0.9.0

Provider-agnostic LLM toolkit — streaming, tool calls, tape storage, OAuth
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
//! Spill large tool results to disk, keeping only head + tail in tape.

use std::cmp::min;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

/// Configuration for spilling large tool outputs to disk.
pub struct SpillConfig {
    /// Character count threshold (Unicode scalar values, not bytes).
    pub threshold_chars: usize,
    pub head_lines: usize,
    pub tail_lines: usize,
}

/// Default spill configuration.
pub const DEFAULT_SPILL: SpillConfig = SpillConfig {
    threshold_chars: 500,
    head_lines: 15,
    tail_lines: 5,
};

/// If `content` exceeds the threshold (in Unicode chars), write full content to
/// a spill file under `{spill_dir}/{call_id}.txt` and return a truncated
/// version with head + tail + file reference. Otherwise return `None`.
pub fn spill_if_needed(
    content: &str,
    call_id: &str,
    spill_dir: &Path,
    config: &SpillConfig,
) -> io::Result<Option<String>> {
    if content.chars().count() <= config.threshold_chars {
        return Ok(None);
    }

    fs::create_dir_all(spill_dir)?;
    let spill_path = spill_dir.join(format!("{call_id}.txt"));
    fs::write(&spill_path, content)?;

    // Canonicalize to absolute path so the model can read the file.
    // Bug F: log a warning when canonicalize fails (e.g. the spill dir lives on
    // a path with a symlink that was removed) so operators can diagnose missing
    // spill files instead of silently getting a relative/broken path.
    let absolute = match spill_path.canonicalize() {
        Ok(p) => p,
        Err(e) => {
            tracing::warn!(
                path = %spill_path.display(),
                error = %e,
                "spill: canonicalize failed, using original path (model may not be able to read the file)"
            );
            spill_path
        }
    };

    Ok(Some(build_truncated(content, &absolute, config)))
}

/// Build the spill directory path for a given tape name.
/// Convention: `{tapes_dir}/{tape_name}.d/`
pub fn spill_dir_for_tape(tapes_dir: &Path, tape_name: &str) -> PathBuf {
    tapes_dir.join(format!("{tape_name}.d"))
}

fn build_truncated(content: &str, path: &Path, config: &SpillConfig) -> String {
    let lines: Vec<&str> = content.lines().collect();
    let total_lines = lines.len();
    let total_chars = content.chars().count();

    let head_end = min(config.head_lines, total_lines);
    let tail_start = total_lines.saturating_sub(config.tail_lines);

    // If line-based truncation doesn't actually remove anything (e.g. a single
    // long line, or fewer lines than head+tail), fall back to char-level split.
    let line_based_keeps_all = tail_start <= head_end;
    if line_based_keeps_all {
        return char_level_truncate(content, total_chars, path, config);
    }

    let head = lines[..head_end].join("\n");
    let tail = lines[tail_start..].join("\n");
    let omitted_lines = tail_start - head_end;
    let omitted_chars = total_chars.saturating_sub(head.chars().count() + tail.chars().count());

    format!(
        "{head}\n\n[{omitted_lines} lines, {omitted_chars} chars omitted — full output: {}]\n\n{tail}",
        path.display()
    )
}

/// Fall back to character-level truncation when line-based splitting can't
/// remove content (single long line, minified JSON, etc.).
///
/// Takes first `budget / 2` chars and last `budget / 4` chars, where
/// `budget = threshold_chars`. Always cuts at char boundaries.
fn char_level_truncate(
    content: &str,
    total_chars: usize,
    path: &Path,
    config: &SpillConfig,
) -> String {
    let head_chars = config.threshold_chars / 2;
    let tail_chars = config.threshold_chars / 4;

    let head_byte_end = char_offset_to_byte(content, head_chars);
    let tail_byte_start = char_offset_from_end(content, tail_chars);

    let head = &content[..head_byte_end];
    let tail = if tail_byte_start > head_byte_end {
        &content[tail_byte_start..]
    } else {
        ""
    };

    let kept_chars = head.chars().count() + tail.chars().count();
    let omitted = total_chars.saturating_sub(kept_chars);

    if tail.is_empty() {
        format!(
            "{head}\n\n[{omitted} chars omitted — full output: {}]",
            path.display()
        )
    } else {
        format!(
            "{head}\n\n[{omitted} chars omitted — full output: {}]\n\n{tail}",
            path.display()
        )
    }
}

/// Return the byte offset of the n-th char (or content.len() if n >= char count).
fn char_offset_to_byte(s: &str, n: usize) -> usize {
    s.char_indices()
        .nth(n)
        .map(|(byte_idx, _)| byte_idx)
        .unwrap_or(s.len())
}

/// Return the byte offset such that the last `n` chars start there.
fn char_offset_from_end(s: &str, n: usize) -> usize {
    let total = s.chars().count();
    if n >= total {
        return 0;
    }
    char_offset_to_byte(s, total - n)
}

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

    #[test]
    fn below_threshold_returns_none() {
        let dir = tempdir().unwrap();
        let result = spill_if_needed("short", "call1", dir.path(), &DEFAULT_SPILL).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn above_threshold_spills_to_file() {
        let dir = tempdir().unwrap();
        let content: String = (0..100)
            .map(|i| format!("line {i}"))
            .collect::<Vec<_>>()
            .join("\n");
        let result = spill_if_needed(&content, "call_abc", dir.path(), &DEFAULT_SPILL).unwrap();
        assert!(result.is_some());

        // Spill file exists with full content
        let spill_path = dir.path().join("call_abc.txt");
        assert!(spill_path.exists());
        assert_eq!(fs::read_to_string(&spill_path).unwrap(), content);

        // Truncated output contains head, tail, and reference
        let truncated = result.unwrap();
        assert!(truncated.contains("line 0"));
        assert!(truncated.contains("line 14")); // last head line
        assert!(truncated.contains("line 99")); // last tail line
        assert!(truncated.contains("lines,"));
        assert!(truncated.contains("chars omitted"));
        assert!(truncated.contains("call_abc.txt"));
    }

    #[test]
    fn head_tail_no_overlap() {
        let dir = tempdir().unwrap();
        // 25 lines, head=15, tail=5 → omit lines 15..20
        let content: String = (0..25)
            .map(|i| format!("L{i}"))
            .collect::<Vec<_>>()
            .join("\n");
        let config = SpillConfig {
            threshold_chars: 10,
            head_lines: 15,
            tail_lines: 5,
        };
        let truncated = spill_if_needed(&content, "c1", dir.path(), &config)
            .unwrap()
            .unwrap();
        assert!(truncated.contains("L0"));
        assert!(truncated.contains("L14"));
        assert!(truncated.contains("L20"));
        assert!(truncated.contains("L24"));
        assert!(truncated.contains("5 lines,"));
    }

    // -- Threshold uses char count, not byte count --

    #[test]
    fn threshold_counts_chars_not_bytes() {
        let dir = tempdir().unwrap();
        // 200 Chinese chars = 600 bytes. Threshold=500 chars → should NOT spill.
        let content: String = "".repeat(200);
        assert_eq!(content.len(), 600); // 600 bytes
        assert_eq!(content.chars().count(), 200); // 200 chars
        let result = spill_if_needed(&content, "cjk", dir.path(), &DEFAULT_SPILL).unwrap();
        assert!(
            result.is_none(),
            "200 chars < 500 threshold, should not spill"
        );
    }

    #[test]
    fn threshold_spills_cjk_over_limit() {
        let dir = tempdir().unwrap();
        // 501 Chinese chars → should spill
        let content: String = "".repeat(501);
        assert_eq!(content.chars().count(), 501);
        let result = spill_if_needed(&content, "cjk_over", dir.path(), &DEFAULT_SPILL).unwrap();
        assert!(result.is_some(), "501 chars > 500 threshold, should spill");

        // Reversible
        let recovered = fs::read_to_string(dir.path().join("cjk_over.txt")).unwrap();
        assert_eq!(recovered, content);
    }

    // -- Single-line large content (no newlines) --

    #[test]
    fn single_line_large_content_uses_char_level_truncation() {
        let dir = tempdir().unwrap();
        // One long line, no newlines — 1000 chars
        let content: String = "x".repeat(1000);
        let config = SpillConfig {
            threshold_chars: 100,
            head_lines: 5,
            tail_lines: 2,
        };
        let truncated = spill_if_needed(&content, "single", dir.path(), &config)
            .unwrap()
            .unwrap();

        // Should be significantly shorter than original
        assert!(
            truncated.chars().count() < content.chars().count(),
            "truncated ({}) should be shorter than original ({})",
            truncated.chars().count(),
            content.chars().count()
        );
        assert!(truncated.contains("chars omitted"));
        assert!(truncated.contains("single.txt"));

        // Head is threshold/2 = 50 chars of 'x'
        assert!(truncated.starts_with(&"x".repeat(50)));

        // Reversible
        let recovered = fs::read_to_string(dir.path().join("single.txt")).unwrap();
        assert_eq!(recovered, content);
    }

    #[test]
    fn single_line_json_blob_truncates() {
        let dir = tempdir().unwrap();
        // Minified JSON — no newlines, very long
        let json_blob = format!(
            r#"{{"data":[{}]}}"#,
            (0..200)
                .map(|i| format!(r#"{{"id":{i},"value":"item_{i}"}}"#))
                .collect::<Vec<_>>()
                .join(",")
        );
        assert!(json_blob.chars().count() > 500);
        assert_eq!(json_blob.lines().count(), 1);

        let truncated = spill_if_needed(&json_blob, "json", dir.path(), &DEFAULT_SPILL)
            .unwrap()
            .unwrap();

        assert!(truncated.chars().count() < json_blob.chars().count());
        assert!(truncated.contains("chars omitted"));

        // Reversible
        let recovered = fs::read_to_string(dir.path().join("json.txt")).unwrap();
        assert_eq!(recovered, json_blob);
    }

    // -- CJK char-level truncation --

    #[test]
    fn cjk_single_line_splits_at_char_boundary() {
        let dir = tempdir().unwrap();
        // 600 Chinese chars, no newlines
        let content: String = (0..600)
            .map(|i| char::from_u32('' as u32 + (i % 100)).unwrap_or(''))
            .collect();
        let config = SpillConfig {
            threshold_chars: 100,
            head_lines: 5,
            tail_lines: 2,
        };
        let truncated = spill_if_needed(&content, "cjk_line", dir.path(), &config)
            .unwrap()
            .unwrap();

        // Head should be exactly 50 chars (threshold/2)
        let head_part: &str = truncated.split("\n\n[").next().unwrap();
        assert_eq!(head_part.chars().count(), 50);

        // Verify it's valid UTF-8 (no broken chars)
        assert!(std::str::from_utf8(truncated.as_bytes()).is_ok());

        // Reversible
        let recovered = fs::read_to_string(dir.path().join("cjk_line.txt")).unwrap();
        assert_eq!(recovered, content);
    }

    #[test]
    fn emoji_content_splits_correctly() {
        let dir = tempdir().unwrap();
        // Emoji are 4 bytes each
        let content: String = "🎉".repeat(200);
        assert_eq!(content.len(), 800); // 800 bytes
        assert_eq!(content.chars().count(), 200); // 200 chars
        let config = SpillConfig {
            threshold_chars: 50,
            head_lines: 5,
            tail_lines: 2,
        };
        let truncated = spill_if_needed(&content, "emoji", dir.path(), &config)
            .unwrap()
            .unwrap();

        // Valid UTF-8
        assert!(std::str::from_utf8(truncated.as_bytes()).is_ok());
        // Head = 25 emoji (threshold/2)
        let head_part: &str = truncated.split("\n\n[").next().unwrap();
        assert_eq!(head_part.chars().count(), 25);
        // Each char in head is the full emoji, not broken
        assert!(head_part.chars().all(|c| c == '🎉'));

        // Reversible
        let recovered = fs::read_to_string(dir.path().join("emoji.txt")).unwrap();
        assert_eq!(recovered, content);
    }

    // -- Mixed content --

    #[test]
    fn mixed_ascii_and_cjk_multiline() {
        let dir = tempdir().unwrap();
        let content: String = (0..100)
            .map(|i| {
                if i % 2 == 0 {
                    format!("english line {i}")
                } else {
                    format!("中文行 {i} 内容测试")
                }
            })
            .collect::<Vec<_>>()
            .join("\n");
        let truncated = spill_if_needed(&content, "mixed", dir.path(), &DEFAULT_SPILL)
            .unwrap()
            .unwrap();

        // Line-based truncation should work (many lines)
        assert!(truncated.contains("english line 0"));
        assert!(truncated.contains("中文行 1 内容测试"));
        assert!(truncated.contains("lines,"));
        assert!(truncated.contains("chars omitted"));

        // Reversible
        let recovered = fs::read_to_string(dir.path().join("mixed.txt")).unwrap();
        assert_eq!(recovered, content);
    }

    // -- Exact threshold boundary --

    #[test]
    fn exact_threshold_does_not_spill() {
        let dir = tempdir().unwrap();
        let config = SpillConfig {
            threshold_chars: 10,
            head_lines: 5,
            tail_lines: 2,
        };
        let content = "0123456789"; // exactly 10 chars
        let result = spill_if_needed(content, "exact", dir.path(), &config).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn one_over_threshold_spills() {
        let dir = tempdir().unwrap();
        let config = SpillConfig {
            threshold_chars: 10,
            head_lines: 5,
            tail_lines: 2,
        };
        let content = "01234567890"; // 11 chars
        let result = spill_if_needed(content, "over", dir.path(), &config).unwrap();
        assert!(result.is_some());

        let recovered = fs::read_to_string(dir.path().join("over.txt")).unwrap();
        assert_eq!(recovered, content);
    }

    // -- Previously existing tests kept --

    #[test]
    fn head_covers_all_lines_falls_back_to_char_level() {
        let dir = tempdir().unwrap();
        // Many chars on few lines — line-based can't truncate, char-level kicks in
        let content: String = (0..5)
            .map(|i| format!("line {i}: {}", "x".repeat(200)))
            .collect::<Vec<_>>()
            .join("\n");
        let config = SpillConfig {
            threshold_chars: 100,
            head_lines: 10, // more than 5 lines → covers all
            tail_lines: 3,
        };
        let truncated = spill_if_needed(&content, "c2", dir.path(), &config)
            .unwrap()
            .unwrap();
        // Falls back to char-level since head_lines covers all lines
        assert!(truncated.contains("chars omitted"));
        // Head is threshold/2 = 50 chars
        let head_part: &str = truncated.split("\n\n[").next().unwrap();
        assert_eq!(head_part.chars().count(), 50);

        // Reversible
        let recovered = fs::read_to_string(dir.path().join("c2.txt")).unwrap();
        assert_eq!(recovered, content);
    }

    // -- Path is absolute and file is readable --

    #[test]
    fn spill_path_in_output_is_absolute() {
        let dir = tempdir().unwrap();
        let content = "x".repeat(600);
        let truncated = spill_if_needed(&content, "abs_test", dir.path(), &DEFAULT_SPILL)
            .unwrap()
            .unwrap();

        // Extract path from the omission notice
        let path_str = truncated
            .split("full output: ")
            .nth(1)
            .unwrap()
            .split(']')
            .next()
            .unwrap();
        let path = std::path::Path::new(path_str);

        assert!(path.is_absolute(), "path should be absolute: {path_str}");
        assert!(path.exists(), "file should exist at: {path_str}");
        assert_eq!(
            fs::read_to_string(path).unwrap(),
            content,
            "file should be readable with full content"
        );
    }

    #[test]
    fn spill_path_in_line_based_output_is_absolute() {
        let dir = tempdir().unwrap();
        let content: String = (0..100)
            .map(|i| format!("line {i}"))
            .collect::<Vec<_>>()
            .join("\n");
        let truncated = spill_if_needed(&content, "lines_abs", dir.path(), &DEFAULT_SPILL)
            .unwrap()
            .unwrap();

        let path_str = truncated
            .split("full output: ")
            .nth(1)
            .unwrap()
            .split(']')
            .next()
            .unwrap();
        let path = std::path::Path::new(path_str);

        assert!(path.is_absolute(), "path should be absolute: {path_str}");
        assert!(path.exists(), "file should exist at: {path_str}");
        assert_eq!(fs::read_to_string(path).unwrap(), content);
    }

    #[test]
    fn spill_path_readable_by_model_tool() {
        // Simulate what the model would do: parse the path from the truncated
        // output and read the file to get the full content.
        let dir = tempdir().unwrap();
        let original = "重要内容\n".repeat(200);
        let truncated = spill_if_needed(&original, "model_read", dir.path(), &DEFAULT_SPILL)
            .unwrap()
            .unwrap();

        // Model sees truncated output, extracts path
        assert!(truncated.contains("full output:"));
        let path_str = truncated
            .split("full output: ")
            .nth(1)
            .unwrap()
            .split(']')
            .next()
            .unwrap();

        // Model calls fs.read with the path — should get exact original
        let recovered = fs::read_to_string(path_str).unwrap();
        assert_eq!(recovered, original);
    }
}