aither-agent 0.30.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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
//! 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, and the type is the
//! embedder's: a [`HandoffSchema`] gives the schema the model fills, the
//! instruction that asks for it, and the text it renders back into the
//! conversation. aither reads from it only what restoration needs, the
//! [`Restoration`]. [`HandoffDocument`] is the schema aither ships.
//!
//! 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::marker::PhantomData;
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::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sync_wrapper::SyncWrapper;

/// A handoff document type a compaction asks the model to fill.
///
/// The type's JSON schema is the structured output the compaction requests,
/// so its fields are what the model writes. The rest is what the embedder
/// decides about its handoff: the instruction that asks for it, the
/// corrective prompt after an unusable answer, what makes a document
/// unusable, and the text the conversation reads in place of everything the
/// handoff replaced.
///
/// aither needs only the [`Restoration`] from it — the file ranges and
/// skills to put back in view — and builds the index of on-disk materials
/// itself, passing it to [`render`](Self::render).
pub trait HandoffSchema: JsonSchema + DeserializeOwned + Send + 'static {
    /// The instruction appended to the conversation to ask for this
    /// document.
    fn instruction(prompt: &HandoffPrompt<'_>) -> String;

    /// The corrective prompt sent once after a response that did not parse
    /// or validate; `reason` says what was wrong with it.
    fn retry(reason: &str) -> String;

    /// Checks a parsed document before it is accepted.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidHandoff`] when the document is unusable; its reason
    /// is quoted to the model in the retry.
    fn validate(&self) -> Result<(), InvalidHandoff> {
        Ok(())
    }

    /// Renders the document as the text the conversation reads from now on.
    ///
    /// `file_index` is the mechanical index of on-disk materials the
    /// compacted conversation produced (`path — description` entries); the
    /// model does not write it, because a missing entry means redone work.
    fn render(&self, file_index: &[String]) -> String;

    /// Takes out what restoration puts back in view.
    fn into_restoration(self) -> Restoration;
}

/// What the instruction of a compaction is rendered from.
#[derive(Debug, Clone, Copy)]
pub struct HandoffPrompt<'a> {
    /// What the handoff should focus on, when a caller said; trimmed and
    /// never empty.
    pub focus: Option<&'a str>,
    /// The session's skills the handoff may name for restoration.
    pub skills: &'a [SkillCatalogEntry],
    /// The attachments of the user messages below the fork the handoff may
    /// name to keep.
    pub attachments: &'a [AttachmentCatalogEntry],
}

/// What a handoff asks to be put back in view after compaction.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Restoration {
    /// File ranges restored verbatim, in order.
    pub context_files: Vec<ContextFile>,
    /// Names of catalog skills whose instructions are restored; a name not
    /// in the catalog is reported instead.
    pub skills: Vec<String>,
    /// Ids of catalog attachments kept in their user messages; an id not
    /// in the catalog is reported instead.
    pub attachments: Vec<String>,
}

/// Why a parsed handoff document is unusable.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{reason}")]
pub struct InvalidHandoff {
    /// What is wrong with the document, as the retry quotes it.
    pub reason: String,
}

impl InvalidHandoff {
    /// An invalid handoff for `reason`.
    pub fn new(reason: impl Into<String>) -> Self {
        Self {
            reason: reason.into(),
        }
    }
}

impl From<InvalidHandoff> for CompactionError {
    fn from(error: InvalidHandoff) -> Self {
        Self::Invalid {
            reason: error.reason,
        }
    }
}

/// The handoff schema aither ships: the task, decisions, errors, pending
/// work and recovery hints, plus the files and skills to restore.
#[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>,
    /// Attachments of user messages the next turns must see again, by
    /// their id in the catalog the instruction lists; every other
    /// attachment below the fork is stripped on apply.
    #[serde(default)]
    pub attachments: Vec<KeptAttachment>,
}

/// 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,
}

/// An attachment the next turns must see again, named by the id the
/// instruction's catalog gave it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct KeptAttachment {
    /// The attachment's id in the catalog the instruction listed.
    pub id: String,
    /// What in it the remaining work must match or inspect.
    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,
}

/// A catalog attachment's place in the window.
///
/// The sequence number identifies the user message carrying it; the index
/// its position in that message's attachments.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AttachmentRef {
    /// Sequence number of the user message carrying the attachment.
    pub seq: u64,
    /// Index of the attachment within that message.
    pub index: usize,
}

/// An attachment of a user message below the fork, offered to the
/// compaction model so the handoff can name it to be kept.
///
/// The catalog is built when the fork is taken and the instruction renders
/// exactly it, so on apply a named id resolves against what the model saw
/// and the attachment stays in the message it came with.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AttachmentCatalogEntry {
    /// The id the handoff names, `attachment-N` in conversation order.
    pub id: String,
    /// The attachment's media type.
    pub media_type: String,
    /// The beginning of the user message the attachment came with, empty
    /// when the message has no text.
    pub excerpt: String,
    /// Where the attachment sits in the window.
    pub reference: AttachmentRef,
}

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

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

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

impl HandoffSchema for HandoffDocument {
    /// Asks for every field, offering the skill catalog by name.
    ///
    /// # Panics
    ///
    /// Panics if the bundled instruction template cannot be rendered, which
    /// is a build-time error in the template rather than a runtime condition.
    fn instruction(prompt: &HandoffPrompt<'_>) -> String {
        CompactionInstruction {
            focus: prompt.focus,
            skills: prompt.skills,
            attachments: prompt.attachments,
        }
        .render()
        .expect("compaction instruction template must render")
    }

    /// Quotes `reason` and restates the keys.
    ///
    /// # Panics
    ///
    /// Panics if the bundled retry template cannot be rendered.
    fn retry(reason: &str) -> String {
        CompactionRetry { reason }
            .render()
            .expect("compaction retry template must render")
    }

    /// Rejects an empty `current_task`: 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.
    fn validate(&self) -> Result<(), InvalidHandoff> {
        if self.current_task.trim().is_empty() {
            return Err(InvalidHandoff::new("current_task must not be empty"));
        }
        Ok(())
    }

    /// Renders the document as Markdown, one section per field.
    ///
    /// # Panics
    ///
    /// Panics if the bundled handoff template cannot be rendered.
    fn render(&self, file_index: &[String]) -> String {
        HandoffDocumentTemplate {
            handoff: self,
            file_index,
        }
        .render()
        .expect("handoff document template must render")
    }

    fn into_restoration(self) -> Restoration {
        Restoration {
            context_files: self.context_files,
            skills: self.skills,
            attachments: self.attachments.into_iter().map(|kept| kept.id).collect(),
        }
    }
}

/// 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 text the conversation reads in place of what it
/// replaced, and the file index the next compaction extends.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Handoff {
    /// The rendered handoff, as the conversation reads it.
    text: String,
    /// The mechanical index of on-disk materials, carried into the next
    /// compaction so a file indexed once stays indexed.
    file_index: Vec<String>,
}

impl Handoff {
    /// A handoff of plain `text`, with no file index.
    #[must_use]
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            file_index: Vec::new(),
        }
    }

    /// A handoff of `text` that carries `file_index`.
    #[must_use]
    pub const fn with_file_index(text: String, file_index: Vec<String>) -> Self {
        Self { text, file_index }
    }

    /// The handoff text, as the conversation reads it.
    #[must_use]
    pub fn text(&self) -> &str {
        &self.text
    }

    /// The file index entries this handoff carries.
    #[must_use]
    pub fn file_index(&self) -> &[String] {
        &self.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. `outputs_dir` is the session's outputs directory, whose
/// absolute paths saved-output notices print.
#[must_use]
pub fn build_file_index(
    previous: &[String],
    messages: &[Message],
    outputs_dir: Option<&std::path::Path>,
) -> 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, outputs_dir) {
            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, asking for a `D`.
pub struct CompactionFork<D> {
    /// 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 session's outputs directory, for the file index.
    outputs_dir: Option<std::path::PathBuf>,
    /// The attachment catalog the instruction renders, carried to the
    /// completed compaction so apply resolves the handoff's names against
    /// exactly what the model saw.
    attachment_catalog: Vec<AttachmentCatalogEntry>,
    /// 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,
    /// The schema the model fills; no `D` is held.
    schema: PhantomData<fn() -> D>,
}

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

impl<D: HandoffSchema> CompactionFork<D> {
    /// Forks `request` — the exact request the run is about to send — into a
    /// compaction request for a `D`, optionally focused by `focus`.
    /// `previous_file_index` is the file index of the handoff the result
    /// will replace, and `attachments` the catalog of attachments the user
    /// messages below `fork_point` carry, which the instruction offers for
    /// the handoff to name.
    ///
    /// The instruction [`D::instruction`](HandoffSchema::instruction)
    /// renders 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.
    pub fn new(
        request: &LLMRequest,
        fork_point: u64,
        previous_file_index: &[String],
        focus: Option<&str>,
        skills: &[SkillCatalogEntry],
        attachments: &[AttachmentCatalogEntry],
    ) -> 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 = D::instruction(&HandoffPrompt {
            focus,
            skills,
            attachments,
        });
        let mut messages = request.messages().to_vec();
        messages.push(Message::user(instruction));
        Ok(Self {
            fork_point,
            previous_file_index: previous_file_index.to_vec(),
            outputs_dir: None,
            attachment_catalog: attachments.to_vec(),
            request: LLMRequest::new(messages)
                .with_parameters(request.parameters().clone())
                .with_tool_definitions(request.tool_definitions().to_vec()),
            schema: PhantomData,
        })
    }

    /// Sets the session's outputs directory, so the file index also finds
    /// saved outputs the conversation names by absolute path.
    #[must_use]
    pub fn with_outputs_dir(mut self, outputs_dir: Option<std::path::PathBuf>) -> Self {
        self.outputs_dir = outputs_dir;
        self
    }

    /// 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 — or a document
    /// [`D::validate`](HandoffSchema::validate) rejects earns one corrective
    /// retry, [`D::retry`](HandoffSchema::retry), that quotes the failure
    /// back to the model. The accepted document is rendered with the file
    /// index and split into the handoff and its restoration.
    ///
    /// # Errors
    ///
    /// Returns [`CompactionError`] when the provider fails, or when the
    /// retry also fails to produce a valid document.
    pub async fn run<M: LanguageModel>(
        self,
        model: M,
    ) -> Result<CompletedCompaction, CompactionError> {
        let Self {
            fork_point,
            request,
            previous_file_index,
            outputs_dir,
            attachment_catalog,
            ..
        } = self;
        let file_index = build_file_index(
            &previous_file_index,
            request.messages(),
            outputs_dir.as_deref(),
        );
        let reason = match model.generate::<D>(request.clone()).await {
            Ok(document) => match document.validate() {
                Ok(()) => {
                    return Ok(CompletedCompaction::new(
                        fork_point,
                        document,
                        file_index,
                        attachment_catalog,
                    ));
                }
                Err(error) => error.reason,
            },
            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 (mut messages, parameters, definitions) = request.into_parts();
        messages.push(Message::user(D::retry(&reason)));
        let retry_request = LLMRequest::new(messages)
            .with_parameters(parameters)
            .with_tool_definitions(definitions);
        let response = collect_text(&model, retry_request).await?;
        let document = parse_handoff::<D>(&response)?;
        document.validate()?;
        Ok(CompletedCompaction::new(
            fork_point,
            document,
            file_index,
            attachment_catalog,
        ))
    }
}

impl CompletedCompaction {
    /// Splits an accepted `document` into the handoff it renders to, with
    /// `file_index`, and its restoration; `attachment_catalog` is the
    /// catalog the instruction offered the document's attachments against.
    pub(crate) fn new<D: HandoffSchema>(
        fork_point: u64,
        document: D,
        file_index: Vec<String>,
        attachment_catalog: Vec<AttachmentCatalogEntry>,
    ) -> Self {
        let text = document.render(&file_index);
        Self {
            fork_point,
            handoff: Handoff::with_file_index(text, file_index),
            restoration: document.into_restoration(),
            attachment_catalog,
        }
    }
}

/// 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 handoff that replaces everything older than the fork point.
    pub handoff: Handoff,
    /// What the handoff asked to put back in view.
    pub restoration: Restoration,
    /// The attachment catalog the instruction offered, so the ids the
    /// handoff named resolve against what the model saw.
    pub attachment_catalog: Vec<AttachmentCatalogEntry>,
}

/// The owned, `'static` future a forked compaction runs as.
type CompactionFuture =
    Pin<Box<dyn Future<Output = Result<CompletedCompaction, 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<D: HandoffSchema, M: LanguageModel + 'static>(
        fork: CompactionFork<D>,
        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 completed compaction.
    pub fn wait(
        &mut self,
    ) -> impl Future<Output = Result<CompletedCompaction, 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<D: DeserializeOwned>(response: &str) -> Result<D, 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/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, None);
        assert_eq!(index.len(), 2);
        assert!(
            index[0].contains("outputs/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_finds_the_absolute_path_a_saved_output_notice_prints() {
        let dir = std::path::Path::new("/work/session/outputs");
        // Joined the way the sandbox names the file, so the separator is the
        // platform's.
        let path = dir.join("river.txt").display().to_string();
        let path = path.as_str();
        let messages = vec![
            Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new(
                    "call_1",
                    "terminal",
                    serde_json::json!({"description": "Run the suite"}),
                )],
            ),
            Message::tool(
                "call_1",
                aither_sandbox::output_saved_text("head", path, 100, 5000),
            ),
        ];
        let index = build_file_index(&[], &messages, Some(dir));
        assert_eq!(index.len(), 1, "{index:?}");
        assert!(index[0].contains(path) && index[0].contains("Run the suite"));
    }

    #[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, None);
        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) -> Vec<String> {
        let file_index = build_file_index(
            context.handoff_file_index(),
            &context.build_request(&[]),
            None,
        );
        context.apply_handoff(
            Handoff::with_file_index("task".to_string(), file_index.clone()),
            u64::MAX,
            0,
            &std::collections::HashSet::new(),
        );
        file_index
    }

    #[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, ["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,
            [
                "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,
            [
                "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(),
            ..HandoffDocument::default()
        };
        let markdown = handoff.render(&["outputs/a-b-c-d.txt — research notes".to_string()]);
        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!(error.reason.contains("current_task"));
    }

    /// A PNG screenshot the way a host attaches one to a user message.
    fn screenshot(name: &str) -> aither_core::llm::Attachment {
        aither_core::llm::Attachment::new(
            format!("https://files.example.com/{name}.png")
                .parse()
                .expect("url"),
            "image/png".parse().expect("media type"),
        )
    }

    /// The instruction offers the catalog the window built: each entry's
    /// id, media type and the excerpt of its message, cut to a character
    /// boundary on Chinese, Japanese and Korean text.
    #[test]
    fn the_instruction_renders_the_attachment_catalog() {
        let mut context = crate::context::Context::new();
        let cjk = "把这张截图里的页眉配色套用到报表页,このスクリーンショットのヘッダー配色をレポートページに適用して,이 스크린샷의 헤더 색상을 보고서 페이지에 적용해 주세요";
        context.push(Message::user(cjk).with_attachment(screenshot("header-colors")));
        context.push(Message::user("").with_attachment(screenshot("bare")));
        let catalog = context.attachment_catalog(context.fork_point());

        let instruction = HandoffDocument::instruction(&HandoffPrompt {
            focus: None,
            skills: &[],
            attachments: &catalog,
        });

        assert!(instruction.contains("`attachments`: {`id`: string, `why`: string}[]"));
        assert!(
            instruction.contains("`attachment-1`: image/png, sent with the user message beginning"),
            "{instruction}"
        );
        let excerpt = catalog[0].excerpt.clone();
        assert!(excerpt.chars().count() <= 80 && excerpt.starts_with('把'));
        assert!(instruction.contains(&format!("\"{excerpt}\"")));
        assert!(
            instruction
                .contains("`attachment-2`: image/png, sent with a user message that has no text"),
            "{instruction}"
        );
    }

    #[test]
    fn the_instruction_marks_an_empty_attachment_catalog() {
        let instruction = HandoffDocument::instruction(&HandoffPrompt {
            focus: None,
            skills: &[],
            attachments: &[],
        });

        assert!(
            instruction.contains("(the conversation has no attachments; leave the list empty)")
        );
    }

    #[test]
    fn a_stored_handoff_without_attachments_still_deserializes() {
        let json = r#"{"current_task":"restyle the header","key_decisions":[],"context_files":[],"errors_fixes":[],"pending_work":["update the palette"],"recovery_hints":[],"skills":[]}"#;
        let document: HandoffDocument = serde_json::from_str(json).expect("stored handoff parses");
        assert_eq!(document.attachments, Vec::new());
    }

    #[test]
    fn handoff_markdown_renders_the_attachments_section() {
        let handoff = HandoffDocument {
            current_task: "restyle the header".to_string(),
            attachments: vec![KeptAttachment {
                id: "attachment-2".to_string(),
                why: "the palette the header must match".to_string(),
            }],
            ..HandoffDocument::default()
        };

        let markdown = handoff.render(&[]);
        assert!(markdown.contains("## Attachments"));
        assert!(markdown.contains("- the palette the header must match"));
        assert!(
            !markdown.contains("attachment-2"),
            "the catalog id means nothing after apply"
        );

        let markdown = HandoffDocument {
            current_task: "restyle the header".to_string(),
            ..HandoffDocument::default()
        }
        .render(&[]);
        assert!(
            !markdown.contains("## Attachments"),
            "the section is omitted when the handoff names none"
        );
    }

    #[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::<HandoffDocument>(response).expect("handoff parses");
        assert_eq!(document.current_task, "ship it");
    }

    #[test]
    fn empty_responses_are_reported_as_invalid() {
        let error =
            parse_handoff::<HandoffDocument>("   ").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::<HandoffDocument>::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::<HandoffDocument>::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,
            false,
        );
        let main = LLMRequest::new(messages).with_parameters(parameters);
        let fork =
            CompactionFork::<HandoffDocument>::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
        );
    }
}