aither-agent 0.17.0

AI agent framework with planning, memory, and tool execution for aither
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! Asynchronous compaction into a typed handoff document.
//!
//! Compaction used to be a stop-the-world event: the run halted, the model
//! summarized its own transcript, and only then did work resume. That is the
//! worst possible moment to spend a full request, because the window is
//! already near its limit and the user is already waiting.
//!
//! Here it is forked instead. At a request boundary the window is snapshotted
//! together with the sequence number of the next item — the *fork point* —
//! and a handoff request goes out alongside the normal turns. The fork
//! reproduces the main request's prefix byte for byte, so the provider serves
//! it from the same cache the run just paid for. When the handoff lands, the
//! next request boundary applies it: the conversation is replaced by the
//! document, and every item numbered at or above the fork point is kept
//! verbatim because the document cannot possibly describe it.
//!
//! The result is a *typed* document rather than prose. Structured output is
//! tried first; a model that ignores the schema gets exactly one corrective
//! retry quoting its own failure, and then the compaction is reported as
//! failed instead of silently storing something unusable.

use std::future::Future;
use std::pin::Pin;

use aither_core::llm::{Classify, Event, Failure, LLMRequest, Message, Role};
use aither_core::{LanguageModel, llm::GenerateError};
use askama::Template;
use futures_lite::StreamExt as _;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use sync_wrapper::SyncWrapper;

/// Structured handoff document used to recover work after compaction.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct HandoffDocument {
    /// What the user asked for, and how far it has got.
    pub current_task: String,
    /// Decisions, constraints and patterns that must survive.
    pub key_decisions: Vec<String>,
    /// File ranges the next turns need in view, restored verbatim on apply.
    #[serde(default)]
    pub context_files: Vec<ContextFile>,
    /// Errors encountered and how they were resolved.
    pub errors_fixes: Vec<String>,
    /// Remaining work, in order, starting with the next action.
    pub pending_work: Vec<String>,
    /// Where the rest of the state can be recovered from.
    pub recovery_hints: Vec<String>,
    /// Session skills whose instructions the next turns need in view,
    /// chosen from the catalog the instruction lists.
    #[serde(default)]
    pub skills: Vec<String>,
    /// Mechanical index of on-disk materials produced during the compacted
    /// conversation (`outputs/`, `artifacts/`), each annotated with the
    /// originating command's description.
    ///
    /// Built by the framework, not the model: asking the model for it is
    /// unreliable, and a missing entry directly causes work to be redone.
    #[serde(default)]
    pub file_index: Vec<String>,
}

/// A file range the next turns need in view, named by the handoff.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct ContextFile {
    /// Path to the file, as it appears on disk.
    pub path: String,
    /// The 1-based inclusive line range to restore; absent restores the
    /// whole file.
    pub lines: Option<LineRange>,
    /// Why the next turns need this file in view.
    pub why: String,
}

/// A 1-based inclusive line range inside a file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct LineRange {
    /// First line to restore.
    pub start: u32,
    /// Last line to restore.
    pub end: u32,
}

/// A skill the handoff may name for restoration.
///
/// The session's skill catalog is offered to the summarising model so it can
/// name skills by their registry name; on apply, only names found in the
/// catalog are restored, and the rest are reported.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SkillCatalogEntry {
    /// The name the handoff names.
    pub name: String,
    /// What the skill is for, shown in the compaction prompt.
    pub description: String,
    /// The text restored into `<restored_context>`.
    pub instructions: String,
}

#[derive(Template)]
#[template(path = "handoff_document.md", escape = "none")]
struct HandoffDocumentTemplate<'a> {
    handoff: &'a HandoffDocument,
}

#[derive(Template)]
#[template(path = "compaction_instruction.txt", escape = "none")]
struct CompactionInstruction<'a> {
    focus: Option<&'a str>,
    skills: &'a [SkillCatalogEntry],
}

#[derive(Template)]
#[template(path = "compaction_retry.txt", escape = "none")]
struct CompactionRetry<'a> {
    reason: &'a str,
}

impl HandoffDocument {
    /// Validates required handoff fields.
    ///
    /// # Errors
    ///
    /// Returns [`CompactionError::Invalid`] when `current_task` is empty: a
    /// handoff without it tells the next instance nothing about what it is
    /// supposed to be doing, and is worse than no handoff at all.
    pub fn validate(self) -> Result<Self, CompactionError> {
        if self.current_task.trim().is_empty() {
            return Err(CompactionError::Invalid {
                reason: "current_task must not be empty".to_string(),
            });
        }
        Ok(self)
    }

    /// Renders this handoff as Markdown.
    ///
    /// # Panics
    ///
    /// Panics if the bundled handoff template cannot be rendered, which is a
    /// build-time error in the template rather than a runtime condition.
    #[must_use]
    pub fn render_markdown(&self) -> String {
        HandoffDocumentTemplate { handoff: self }
            .render()
            .expect("handoff document template must render")
    }
}

/// Why a compaction could not produce a usable handoff document.
#[derive(Debug, Clone, thiserror::Error)]
pub enum CompactionError {
    /// The provider failed before producing a response.
    #[error("compaction request failed: {message}")]
    Provider {
        /// The provider's error message.
        message: String,
        /// How the provider error classifies.
        failure: Failure,
    },

    /// The model answered, but not with a usable handoff document.
    #[error("compaction produced an invalid handoff document: {reason}")]
    Invalid {
        /// What was wrong with the document.
        reason: String,
    },

    /// There was nothing to compact.
    #[error("compaction found no conversation to summarize")]
    Empty,
}

/// Joins an index entry's path to its description.
const INDEX_SEPARATOR: &str = " — ";

/// The handoff a [`Context`](crate::context::Context) carries between
/// compactions: the structured document a compaction produced, or the plain
/// text a host set directly.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Handoff {
    /// Free text set by the host; it has no file index.
    Text(String),
    /// A document produced by a compaction.
    Document(HandoffDocument),
}

impl Handoff {
    /// The file index entries this handoff carries.
    #[must_use]
    pub fn file_index(&self) -> &[String] {
        match self {
            Self::Text(_) => &[],
            Self::Document(document) => &document.file_index,
        }
    }
}

/// Builds the mechanical file index for a handoff from conversation messages.
///
/// Scans tool results for `outputs/` URLs and `artifacts/` paths and pairs
/// each with the description of the tool call that produced it. `previous` is
/// the file index of the handoff being replaced: its entries are kept, so a
/// file indexed by an earlier compaction stays indexed. Later mentions of the
/// same path win.
#[must_use]
pub fn build_file_index(previous: &[String], messages: &[Message]) -> Vec<String> {
    use std::collections::HashMap;

    let mut call_description: HashMap<&str, &str> = HashMap::new();
    for message in messages {
        for call in message.tool_calls() {
            if let Some(description) = call.arguments.get("description").and_then(|v| v.as_str()) {
                call_description.insert(call.id.as_str(), description);
            }
        }
    }

    let mut index: indexmap::IndexMap<String, Option<String>> = indexmap::IndexMap::new();
    for entry in previous {
        match entry.split_once(INDEX_SEPARATOR) {
            Some((path, description)) => {
                index.insert(path.to_string(), Some(description.to_string()));
            }
            None => {
                index.insert(entry.clone(), None);
            }
        }
    }
    for message in messages {
        let Message::Tool {
            content,
            tool_call_id,
        } = message
        else {
            continue;
        };
        let description = call_description
            .get(tool_call_id.as_str())
            .map(|description| (*description).to_string());
        for url in crate::output_urls::extract_referenced_urls(content) {
            index.insert(url, description.clone());
        }
        for word in content.split_whitespace() {
            let word = word.trim_matches(|c: char| {
                c == '"' || c == '\'' || c == '`' || c == ',' || c == ')' || c == ']' || c == ';'
            });
            if word.starts_with("artifacts/") && word.len() > "artifacts/".len() {
                index.insert(word.to_string(), description.clone());
            }
        }
    }

    index
        .into_iter()
        .map(|(path, description)| match description {
            Some(description) => format!("{path}{INDEX_SEPARATOR}{description}"),
            None => path,
        })
        .collect()
}

/// A compaction snapshot taken at a request boundary.
#[derive(Debug, Clone)]
pub struct CompactionFork {
    /// Sequence number of the first item the handoff will *not* describe.
    fork_point: u64,
    /// The file index of the handoff this compaction replaces.
    previous_file_index: Vec<String>,
    /// The main request, source blocks, tools and cache parameters included,
    /// with the handoff instruction appended to its messages. Replaying it is
    /// what lets the provider serve the compaction from the same prefix
    /// cache the run just paid for.
    request: LLMRequest,
}

impl CompactionFork {
    /// Forks `request` — the exact request the run is about to send — into a
    /// compaction request, optionally focused by `focus`. `previous_file_index`
    /// is the file index of the handoff the result will replace.
    ///
    /// The instruction is *appended* and the tool definitions and parameters
    /// are reused untouched: everything up to the instruction is byte-for-
    /// byte what the main request sent, so the provider's prefix cache still
    /// applies.
    ///
    /// # Errors
    ///
    /// Returns [`CompactionError::Empty`] when the request carries no
    /// conversation to summarize.
    ///
    /// # Panics
    ///
    /// Panics if the compiled-in instruction template fails to render, which
    /// would mean the binary was built against a broken template.
    pub fn new(
        request: &LLMRequest,
        fork_point: u64,
        previous_file_index: &[String],
        focus: Option<&str>,
        skills: &[SkillCatalogEntry],
    ) -> Result<Self, CompactionError> {
        if request
            .messages()
            .iter()
            .all(|message| message.role() == Role::System)
        {
            return Err(CompactionError::Empty);
        }
        let focus = focus.map(str::trim).filter(|focus| !focus.is_empty());
        let instruction = CompactionInstruction { focus, skills }
            .render()
            .expect("compaction instruction template must render");
        let mut messages = request.messages().to_vec();
        messages.push(Message::user(instruction));
        Ok(Self {
            fork_point,
            previous_file_index: previous_file_index.to_vec(),
            request: LLMRequest::new(messages)
                .with_parameters(request.parameters().clone())
                .with_tool_definitions(request.tool_definitions().to_vec()),
        })
    }

    /// The sequence number items must reach to survive this compaction.
    #[must_use]
    pub const fn fork_point(&self) -> u64 {
        self.fork_point
    }

    /// The request the fork replays, instruction included.
    #[must_use]
    pub const fn request(&self) -> &LLMRequest {
        &self.request
    }

    /// Runs the compaction against `model`.
    ///
    /// Structured output is attempted first. A schema mismatch — the common
    /// failure for models without native structured output — earns one
    /// corrective retry that quotes the parse failure back to the model.
    ///
    /// # Errors
    ///
    /// Returns [`CompactionError`] when the provider fails, or when the
    /// retry also fails to produce a valid document.
    ///
    /// # Panics
    ///
    /// Panics if the compiled-in retry template fails to render, which would
    /// mean the binary was built against a broken template.
    pub async fn run<M: LanguageModel>(self, model: M) -> Result<HandoffDocument, CompactionError> {
        let Self {
            request,
            previous_file_index,
            ..
        } = self;
        let conversation = request.messages().to_vec();
        let reason = match model.generate::<HandoffDocument>(request.clone()).await {
            Ok(document) => match document.validate() {
                Ok(mut document) => {
                    document.file_index = build_file_index(&previous_file_index, &conversation);
                    return Ok(document);
                }
                Err(error) => error.to_string(),
            },
            Err(GenerateError::Provider(error)) => {
                return Err(CompactionError::Provider {
                    message: error.to_string(),
                    failure: error.failure(),
                });
            }
            Err(error @ GenerateError::Parse { .. }) => error.to_string(),
        };

        tracing::warn!(reason = %reason, "retrying compaction with a corrective prompt");
        let retry = CompactionRetry {
            reason: reason.as_str(),
        }
        .render()
        .expect("compaction retry template must render");
        let (mut messages, parameters, definitions) = request.into_parts();
        messages.push(Message::user(retry));
        let retry_request = LLMRequest::new(messages)
            .with_parameters(parameters)
            .with_tool_definitions(definitions);
        let response = collect_text(&model, retry_request).await?;
        let mut document = parse_handoff(&response)?.validate()?;
        document.file_index = build_file_index(&previous_file_index, &conversation);
        Ok(document)
    }
}

/// A compaction that has landed but not yet been folded into the window.
///
/// The handoff is applied at the next request boundary rather than the moment
/// it arrives: mid-turn the conversation holds tool calls whose results have
/// not been appended yet, and replacing it there would strand them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompletedCompaction {
    /// Sequence number items must reach to survive the handoff.
    pub fork_point: u64,
    /// The document that replaces everything older than the fork point.
    pub handoff: HandoffDocument,
}

/// The owned, `'static` future a forked compaction runs as.
type CompactionFuture =
    Pin<Box<dyn Future<Output = Result<HandoffDocument, CompactionError>> + Send>>;

/// A compaction that is in flight while the run continues.
///
/// The future is owned and `'static`, which is what lets it be carried across
/// turn boundaries and polled next to the model stream without an executor:
/// the agent races the two with [`futures_lite::future::or`] and keeps
/// whichever finishes.
pub struct PendingCompaction {
    fork_point: u64,
    /// The compaction in flight.
    ///
    /// A future built from provider futures is `Send` but says nothing about
    /// `Sync`, and the agent that owns this has to stay `Sync` to live inside
    /// a tool. [`SyncWrapper`] grants exactly that: `Sync` in exchange for
    /// `&mut`-only access, which is all polling ever needs.
    future: SyncWrapper<CompactionFuture>,
}

impl std::fmt::Debug for PendingCompaction {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("PendingCompaction")
            .field("fork_point", &self.fork_point)
            .finish_non_exhaustive()
    }
}

impl PendingCompaction {
    /// Starts `fork` against an owned `model`.
    #[must_use]
    pub fn start<M: LanguageModel + 'static>(fork: CompactionFork, model: M) -> Self {
        let fork_point = fork.fork_point();
        Self {
            fork_point,
            future: SyncWrapper::new(Box::pin(fork.run(model))),
        }
    }

    /// The sequence number items must reach to survive this compaction.
    #[must_use]
    pub const fn fork_point(&self) -> u64 {
        self.fork_point
    }

    /// Awaits the handoff document.
    pub fn wait(&mut self) -> impl Future<Output = Result<HandoffDocument, CompactionError>> + '_ {
        self.future.get_mut()
    }
}

/// Parses a handoff document from a raw model response.
///
/// Models wrap JSON in prose and code fences no matter what the prompt says,
/// so the outermost braces are located rather than trusting the whole body.
fn parse_handoff(response: &str) -> Result<HandoffDocument, CompactionError> {
    let trimmed = response.trim();
    if trimmed.is_empty() {
        return Err(CompactionError::Invalid {
            reason: "the model returned an empty response".to_string(),
        });
    }
    let json = match (trimmed.find('{'), trimmed.rfind('}')) {
        (Some(start), Some(end)) if end > start => &trimmed[start..=end],
        _ => trimmed,
    };
    serde_json::from_str(json).map_err(|error| CompactionError::Invalid {
        reason: error.to_string(),
    })
}

/// Collects the text of a non-streaming completion.
async fn collect_text<M: LanguageModel>(
    model: &M,
    request: LLMRequest,
) -> Result<String, CompactionError> {
    let stream = model.respond(request);
    futures_lite::pin!(stream);
    let mut text = String::new();
    while let Some(event) = stream.next().await {
        match event {
            Ok(Event::Text(chunk)) => text.push_str(&chunk),
            Ok(_) => {}
            Err(error) => {
                return Err(CompactionError::Provider {
                    message: error.to_string(),
                    failure: error.failure(),
                });
            }
        }
    }
    Ok(text)
}

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

    #[test]
    fn build_file_index_pairs_outputs_with_call_descriptions() {
        let messages = vec![
            Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new(
                    "call_1",
                    "terminal",
                    serde_json::json!({
                        "description": "Fetch WebGPU status pages",
                        "script": "webfetch https://caniuse.com"
                    }),
                )],
            ),
            Message::tool(
                "call_1",
                "Output saved to outputs/amber-oak-swift-river.txt (100 lines, 5000 bytes)",
            ),
            Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new(
                    "call_2",
                    "terminal",
                    serde_json::json!({"description": "Write deck", "script": "tee artifacts/slides.md"}),
                )],
            ),
            Message::tool("call_2", "wrote artifacts/slides.md"),
        ];

        let index = build_file_index(&[], &messages);
        assert_eq!(index.len(), 2);
        assert!(
            index[0].contains("outputs/amber-oak-swift-river.txt")
                && index[0].contains("Fetch WebGPU status pages"),
            "index entry should pair path with description: {index:?}"
        );
        assert!(index[1].contains("artifacts/slides.md") && index[1].contains("Write deck"));
    }

    #[test]
    fn build_file_index_keeps_latest_description_per_path() {
        let messages = vec![
            Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new(
                    "call_1",
                    "terminal",
                    serde_json::json!({"description": "first pass"}),
                )],
            ),
            Message::tool("call_1", "see artifacts/report.md"),
            Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new(
                    "call_2",
                    "terminal",
                    serde_json::json!({"description": "final revision"}),
                )],
            ),
            Message::tool("call_2", "updated artifacts/report.md"),
        ];

        let index = build_file_index(&[], &messages);
        assert_eq!(index.len(), 1);
        assert!(index[0].contains("final revision"));
    }

    fn read_file_messages(id: &str, description: &str, path: &str) -> Vec<Message> {
        vec![
            Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new(
                    id,
                    "terminal",
                    serde_json::json!({"description": description}),
                )],
            ),
            Message::tool(id, format!("saved {path}")),
        ]
    }

    /// Compacts `context` the way a run does: indexes the conversation seeded
    /// with the current handoff's index, then folds the result into the window.
    fn compact(context: &mut crate::context::Context) -> HandoffDocument {
        let handoff = HandoffDocument {
            current_task: "task".to_string(),
            file_index: build_file_index(context.handoff_file_index(), &context.build_request(&[])),
            ..HandoffDocument::default()
        };
        context.apply_handoff(&handoff, u64::MAX, 0);
        handoff
    }

    #[test]
    fn the_second_handoff_keeps_files_indexed_by_the_first() {
        let mut context = crate::context::Context::new();
        for message in read_file_messages("call_1", "Fetch notes", "artifacts/notes.md") {
            context.push(message);
        }
        let first = compact(&mut context);
        assert_eq!(first.file_index, ["artifacts/notes.md — Fetch notes"]);

        for message in read_file_messages("call_2", "Write deck", "artifacts/slides.md") {
            context.push(message);
        }
        let second = compact(&mut context);
        assert_eq!(
            second.file_index,
            [
                "artifacts/notes.md — Fetch notes",
                "artifacts/slides.md — Write deck"
            ]
        );

        for message in read_file_messages("call_3", "Revised notes", "artifacts/notes.md") {
            context.push(message);
        }
        let third = compact(&mut context);
        assert_eq!(
            third.file_index,
            [
                "artifacts/notes.md — Revised notes",
                "artifacts/slides.md — Write deck"
            ]
        );
    }

    #[test]
    fn handoff_markdown_renders_file_index_section() {
        let handoff = HandoffDocument {
            current_task: "task".to_string(),
            file_index: vec!["outputs/a-b-c-d.txt — research notes".to_string()],
            ..HandoffDocument::default()
        };
        let markdown = handoff.render_markdown();
        assert!(markdown.contains("Materials Already On Disk"));
        assert!(markdown.contains("outputs/a-b-c-d.txt — research notes"));
        assert!(markdown.contains("Read the file instead of redoing the work"));
    }

    #[test]
    fn a_handoff_without_a_current_task_is_rejected() {
        let error = HandoffDocument::default()
            .validate()
            .expect_err("empty current_task must not validate");
        assert!(matches!(error, CompactionError::Invalid { .. }));
    }

    #[test]
    fn handoff_parsing_tolerates_fences_and_commentary() {
        let response = "Sure! Here you go:\n```json\n{\"current_task\": \"ship it\", \
            \"key_decisions\": [], \"files_paths\": [], \"errors_fixes\": [], \
            \"pending_work\": [], \"recovery_hints\": []}\n```\nHope that helps.";
        let document = parse_handoff(response).expect("handoff parses");
        assert_eq!(document.current_task, "ship it");
    }

    #[test]
    fn empty_responses_are_reported_as_invalid() {
        let error = parse_handoff("   ").expect_err("empty response must not parse");
        assert!(matches!(error, CompactionError::Invalid { .. }));
    }

    #[test]
    fn forking_an_empty_context_is_an_error() {
        let request = LLMRequest::new(vec![Message::system("only the prefix")]);
        assert!(matches!(
            CompactionFork::new(&request, 0, &[], None, &[]),
            Err(CompactionError::Empty)
        ));
    }

    #[test]
    fn the_fork_reproduces_the_main_request_and_appends_the_instruction() {
        use aither_core::llm::model::Parameters;

        let parameters = Parameters {
            temperature: Some(0.3),
            ..Parameters::default()
        };
        let main = LLMRequest::new(vec![
            Message::system("be useful"),
            Message::system("<tool-outputs>tail block</tool-outputs>"),
            Message::user("do the thing"),
        ])
        .with_parameters(parameters);

        let skills = [SkillCatalogEntry {
            name: "pdf".to_string(),
            description: "read PDFs".to_string(),
            instructions: "how to read PDFs".to_string(),
        }];
        let fork =
            CompactionFork::new(&main, 7, &[], Some("the failing test"), &skills).expect("fork");

        assert_eq!(fork.fork_point(), 7);
        assert_eq!(
            fork.request().parameters().temperature,
            main.parameters().temperature,
            "the compaction request reuses the main parameters"
        );
        assert_eq!(
            fork.request().tool_definitions().len(),
            main.tool_definitions().len(),
            "the compaction request carries the same tools"
        );
        assert_eq!(fork.request().messages().len(), main.messages().len() + 1);
        for (index, message) in main.messages().iter().enumerate() {
            assert_eq!(
                message.content(),
                fork.request().messages()[index].content(),
                "fork perturbed prefix message {index}"
            );
        }
        let instruction = fork
            .request()
            .messages()
            .last()
            .expect("instruction")
            .content();
        assert!(instruction.contains("Focus the handoff on: the failing test"));
        assert!(instruction.contains("pdf"), "the skill catalog is offered");
    }

    #[derive(Debug)]
    struct NoError;

    impl std::fmt::Display for NoError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_str("never fails")
        }
    }

    impl std::error::Error for NoError {}

    impl aither_core::llm::Classify for NoError {
        fn failure(&self) -> aither_core::llm::Failure {
            aither_core::llm::Failure::permanent()
        }
    }

    /// Answers every request with a handoff and records it.
    struct CapturingModel {
        seen: std::sync::Mutex<Vec<LLMRequest>>,
    }

    impl LanguageModel for CapturingModel {
        type Error = NoError;

        fn respond(
            &self,
            request: LLMRequest,
        ) -> impl futures_lite::Stream<Item = Result<Event, Self::Error>> + Send {
            self.seen.lock().unwrap().push(request);
            futures_lite::stream::iter([Ok(Event::Text(
                r#"{"current_task":"keep going","key_decisions":[],"context_files":[],"errors_fixes":[],"pending_work":["continue"],"recovery_hints":[],"skills":[]}"#
                    .to_string(),
            ))])
        }

        fn profile(
            &self,
        ) -> impl std::future::Future<
            Output = Result<aither_core::llm::model::Profile, Self::Error>,
        > + Send {
            std::future::ready(Ok(aither_core::llm::model::Profile::new(
                "capture",
                "test",
                "capture",
                "capturing model",
                1_000_000,
            )))
        }
    }

    /// The compaction request must reach the provider with the session's
    /// cache controls and the structured-output flag `generate` sets.
    #[tokio::test]
    async fn the_compaction_request_keeps_the_cache_controls_beside_structured_outputs() {
        use aither_core::llm::model::{
            ClaudeCacheBreakpointTarget, ClaudePromptCacheStrategy, OpenAIPromptCacheRetention,
            Parameters,
        };

        let messages = vec![
            Message::system("prefix"),
            Message::user("hello"),
            Message::assistant("hi"),
            Message::system("live tail"),
        ];
        let parameters = crate::context::PromptCache::new("session-1").apply(
            Parameters::default(),
            &messages,
            false,
        );
        let main = LLMRequest::new(messages).with_parameters(parameters);
        let fork = CompactionFork::new(&main, 0, &[], None, &[]).expect("fork");
        let model = CapturingModel {
            seen: std::sync::Mutex::default(),
        };
        fork.run(&model).await.expect("compaction");

        let request = model.seen.lock().unwrap().remove(0);
        let parameters = request.parameters();
        assert!(parameters.structured_outputs);
        let openai = parameters.cache.openai.as_ref().expect("openai cache");
        assert_eq!(openai.key.as_deref(), Some("session-1:0"));
        assert_eq!(openai.retention, Some(OpenAIPromptCacheRetention::Hours24));
        let ClaudePromptCacheStrategy::Explicit(breakpoints) =
            parameters.cache.claude.expect("claude cache").strategy
        else {
            panic!("expected explicit claude breakpoints");
        };
        assert_eq!(
            breakpoints.first.target,
            ClaudeCacheBreakpointTarget::LastSystem
        );
    }
}