awaken-runtime 0.4.0

Phase-based execution engine, plugin system, and agent loop for Awaken
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
//! Compaction boundary discovery, plan/apply helpers, and load-time trimming.

use std::collections::HashSet;
use std::sync::Arc;

use awaken_contract::contract::inference::ContextWindowPolicy;
use awaken_contract::contract::message::{Message, Role, Visibility};
use awaken_contract::contract::transform::estimate_message_tokens;

use super::plugin::{CompactionAction, CompactionBoundary, CompactionInFlight, CompactionStateKey};
use super::summarizer::{MIN_COMPACTION_GAIN_TOKENS, extract_previous_summary, render_transcript};
use crate::state::{MutationBatch, StateStore};

/// Custom event type emitted by a successful background compaction task.
pub const COMPACTION_COMPLETED_EVENT: &str = "context.compacted";
/// Custom event type emitted when a background compaction task fails.
pub const COMPACTION_FAILED_EVENT: &str = "context.compaction_failed";

/// Find a safe compaction boundary in the message history.
///
/// Returns the index of the last message that can be safely compacted
/// (all tool call/result pairs are complete before this point).
pub fn find_compaction_boundary(
    messages: &[Arc<Message>],
    start: usize,
    end: usize,
) -> Option<usize> {
    let mut open_calls = HashSet::<String>::new();
    let mut best_boundary = None;

    for (idx, msg) in messages.iter().enumerate().skip(start).take(end - start) {
        if let Some(ref calls) = msg.tool_calls {
            for call in calls {
                open_calls.insert(call.id.clone());
            }
        }

        if msg.role == Role::Tool
            && let Some(ref call_id) = msg.tool_call_id
        {
            open_calls.remove(call_id);
        }

        // Safe boundary: all tool calls resolved and next isn't a tool result
        let next_is_tool = messages
            .get(idx + 1)
            .is_some_and(|next| next.role == Role::Tool);

        if open_calls.is_empty() && !next_is_tool {
            best_boundary = Some(idx);
        }
    }

    best_boundary
}

/// Trim loaded messages to the latest compaction boundary.
///
/// If the message list contains a `<conversation-summary>` internal_system message,
/// all messages before it are dropped. The summary message becomes the first message.
/// This avoids loading already-summarized history into the context window.
///
/// Idempotent: if no summary exists or messages are already trimmed, this is a no-op.
pub fn trim_to_compaction_boundary(messages: &mut Vec<Arc<Message>>) {
    // Find the last summary message (in case of multiple compactions)
    let last_summary_idx = messages.iter().rposition(|m| {
        m.role == Role::System
            && m.visibility == Visibility::Internal
            && m.text().contains("<conversation-summary>")
    });

    if let Some(idx) = last_summary_idx
        && idx > 0
    {
        messages.drain(..idx);
    }
}

/// Record a compaction boundary in the state store.
pub fn record_compaction_boundary(
    boundary: super::plugin::CompactionBoundary,
) -> super::plugin::CompactionAction {
    super::plugin::CompactionAction::RecordBoundary(boundary)
}

/// Inputs needed to run a compaction off the main thread. Snapshotted at
/// trigger time so the background task does not race with the live
/// `messages` list (which keeps growing during summarization).
#[derive(Debug, Clone)]
pub struct CompactionPlan {
    /// Pre-rendered transcript to feed the summarizer (Internal messages
    /// already filtered).
    pub transcript: String,
    /// Previous cumulative summary, if any, for incremental updates.
    pub previous_summary: Option<String>,
    /// Stable id of the last message included in the summary. The swap
    /// path locates the cut point against the current message list by
    /// this id, so it survives any new messages appended in the window.
    pub boundary_message_id: String,
    /// Token estimate of the messages that the summary will replace.
    /// Used for the `pre_tokens` field of the recorded boundary.
    pub pre_tokens: usize,
}

/// Result of a successful in-place swap.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AppliedCompaction {
    /// Index in the original list where the cut happened.
    pub boundary_index: usize,
    /// Tokens that were dropped from the head of the message list.
    pub pre_tokens: usize,
    /// Tokens used by the inserted summary message.
    pub post_tokens: usize,
}

/// Decide whether compaction should run right now and, if so, capture the
/// inputs needed by the background summarization task. Returns `None` if
/// compaction is not feasible (no safe boundary, savings below threshold,
/// boundary message has no stable id, transcript is empty, etc.).
pub fn plan_compaction(
    messages: &[Arc<Message>],
    policy: &ContextWindowPolicy,
) -> Option<CompactionPlan> {
    if messages.len() < 2 {
        return None;
    }
    let keep_suffix = policy.compaction_raw_suffix_messages.min(messages.len());
    let search_end = messages.len().saturating_sub(keep_suffix);
    if search_end < 2 {
        return None;
    }
    let boundary = find_compaction_boundary(messages, 0, search_end)?;
    let boundary_message_id = messages[boundary].id.clone()?;
    let pre_tokens: usize = messages[..=boundary]
        .iter()
        .map(|m| estimate_message_tokens(m))
        .sum();
    if pre_tokens < MIN_COMPACTION_GAIN_TOKENS {
        return None;
    }
    let transcript = render_transcript(&messages[..=boundary]);
    if transcript.is_empty() {
        return None;
    }
    let previous_summary = extract_previous_summary(messages);
    Some(CompactionPlan {
        transcript,
        previous_summary,
        boundary_message_id,
        pre_tokens,
    })
}

/// Mark a background compaction pass as in-flight in the state store.
/// Used by the spawn helper after a task has been queued.
pub fn record_compaction_in_flight(in_flight: CompactionInFlight) -> CompactionAction {
    CompactionAction::SetInFlight(in_flight)
}

/// Clear the in-flight marker. Called from the inbox-event router on both
/// success and failure of the background pass.
pub fn clear_compaction_in_flight() -> CompactionAction {
    CompactionAction::ClearInFlight
}

/// Detect and handle a context-compaction event arriving via the inbox.
///
/// Returns `true` when `payload` was a compaction event — in that case the
/// caller MUST NOT also push it as a regular Internal user message. The
/// success path performs the message swap by stable id and records the
/// boundary; both success and failure paths clear the in-flight marker
/// so future compactions can be triggered.
///
/// Returns `false` for any payload that is not a compaction event; the
/// caller should fall back to the normal inbox handling.
pub fn try_consume_compaction_event(
    messages: &mut Vec<Arc<Message>>,
    payload: &serde_json::Value,
    store: &StateStore,
) -> bool {
    let Some(event_type) = compaction_event_type(payload) else {
        return false;
    };
    let inner = payload.get("payload");

    match event_type {
        e if e == COMPACTION_COMPLETED_EVENT => {
            let boundary_id = inner
                .and_then(|p| p.get("boundary_message_id"))
                .and_then(|v| v.as_str())
                .unwrap_or_default();
            let summary = inner
                .and_then(|p| p.get("summary"))
                .and_then(|v| v.as_str())
                .unwrap_or_default();
            let reported_pre_tokens = inner
                .and_then(|p| p.get("pre_tokens"))
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as usize;

            let mut batch = MutationBatch::new();
            if !boundary_id.is_empty()
                && !summary.is_empty()
                && let Some(applied) = apply_summary(messages, boundary_id, summary)
            {
                batch.update::<CompactionStateKey>(CompactionAction::RecordBoundary(
                    CompactionBoundary {
                        summary: summary.to_string(),
                        pre_tokens: applied.pre_tokens.max(reported_pre_tokens),
                        post_tokens: applied.post_tokens,
                        timestamp_ms: now_ms(),
                    },
                ));
                tracing::info!(
                    pre_tokens = applied.pre_tokens,
                    post_tokens = applied.post_tokens,
                    boundary_index = applied.boundary_index,
                    "background_compaction_swap_applied"
                );
            } else {
                tracing::warn!(
                    boundary_message_id = boundary_id,
                    "background compaction completed but boundary message no longer present; skipping swap"
                );
            }
            batch.update::<CompactionStateKey>(CompactionAction::ClearInFlight);
            if let Err(error) = store.commit(batch) {
                tracing::warn!(
                    error = %error,
                    "failed to commit compaction completion state"
                );
            }
        }
        e if e == COMPACTION_FAILED_EVENT => {
            let error_text = inner
                .and_then(|p| p.get("error"))
                .and_then(|v| v.as_str())
                .unwrap_or("unknown error");
            tracing::warn!(
                error = error_text,
                "background compaction failed; clearing in-flight marker"
            );
            let mut batch = MutationBatch::new();
            batch.update::<CompactionStateKey>(CompactionAction::ClearInFlight);
            if let Err(error) = store.commit(batch) {
                tracing::warn!(
                    error = %error,
                    "failed to clear in-flight marker after compaction failure"
                );
            }
        }
        _ => {}
    }
    true
}

fn compaction_event_type(payload: &serde_json::Value) -> Option<&str> {
    if payload.get("kind").and_then(|k| k.as_str()) != Some("custom") {
        return None;
    }
    payload
        .get("event_type")
        .and_then(|t| t.as_str())
        .filter(|t| *t == COMPACTION_COMPLETED_EVENT || *t == COMPACTION_FAILED_EVENT)
}

fn now_ms() -> u64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

/// Apply a freshly produced summary to the live message list. Locates the
/// boundary message by id (not by index) so it is safe against any
/// messages appended between trigger and completion. Returns `None` when
/// the boundary message is no longer present (already trimmed by an
/// earlier compaction or rewritten by another path); callers should treat
/// that as a benign skip.
pub fn apply_summary(
    messages: &mut Vec<Arc<Message>>,
    boundary_message_id: &str,
    summary_text: &str,
) -> Option<AppliedCompaction> {
    let idx = messages
        .iter()
        .position(|m| m.id.as_deref() == Some(boundary_message_id))?;
    let pre_tokens: usize = messages[..=idx]
        .iter()
        .map(|m| estimate_message_tokens(m))
        .sum();
    messages.drain(..=idx);
    let summary_message = Arc::new(Message::internal_system(format!(
        "<conversation-summary>\n{summary_text}\n</conversation-summary>"
    )));
    let post_tokens = estimate_message_tokens(&summary_message);
    messages.insert(0, summary_message);
    Some(AppliedCompaction {
        boundary_index: idx,
        pre_tokens,
        post_tokens,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use awaken_contract::contract::message::ToolCall;
    use serde_json::json;

    fn long_user(text: &str, copies: usize) -> Arc<Message> {
        Arc::new(Message::user(text.repeat(copies)))
    }

    fn store_with_compaction_plugin() -> StateStore {
        let store = StateStore::new();
        store
            .install_plugin(super::super::plugin::CompactionPlugin::default())
            .unwrap();
        store
    }

    fn completed_event(boundary_id: &str, summary: &str, pre_tokens: u64) -> serde_json::Value {
        json!({
            "kind": "custom",
            "task_id": "bg_99",
            "event_type": COMPACTION_COMPLETED_EVENT,
            "payload": {
                "boundary_message_id": boundary_id,
                "summary": summary,
                "pre_tokens": pre_tokens,
            },
        })
    }

    fn failed_event(boundary_id: &str, error_text: &str) -> serde_json::Value {
        json!({
            "kind": "custom",
            "task_id": "bg_99",
            "event_type": COMPACTION_FAILED_EVENT,
            "payload": {
                "boundary_message_id": boundary_id,
                "error": error_text,
            },
        })
    }

    fn mark_in_flight(store: &StateStore, boundary_id: &str) {
        let mut batch = MutationBatch::new();
        batch.update::<CompactionStateKey>(record_compaction_in_flight(CompactionInFlight {
            task_id: "bg_99".into(),
            boundary_message_id: boundary_id.into(),
            started_at_ms: 1,
        }));
        store.commit(batch).unwrap();
    }

    #[test]
    fn try_consume_compaction_event_swaps_messages_and_records_boundary() {
        let store = store_with_compaction_plugin();
        let mut messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("OLD-1")),
            Arc::new(Message::assistant("OLD-2")),
            Arc::new(Message::user("BOUNDARY")),
            Arc::new(Message::assistant("AFTER")),
            // simulates a user message that arrived during the compaction window
            Arc::new(Message::user("RACE-NEW")),
        ];
        let boundary_id = messages[2].id.clone().unwrap();
        mark_in_flight(&store, &boundary_id);

        let consumed = try_consume_compaction_event(
            &mut messages,
            &completed_event(&boundary_id, "the summary", 4321),
            &store,
        );
        assert!(consumed, "must report the event was consumed");

        // Swap happened: summary at front, race-new message preserved.
        assert!(
            messages[0]
                .text()
                .contains("<conversation-summary>\nthe summary"),
            "summary not at front: {}",
            messages[0].text()
        );
        assert_eq!(messages[1].text(), "AFTER");
        assert_eq!(messages[2].text(), "RACE-NEW");
        assert_eq!(messages.len(), 3);

        let state = store.read::<CompactionStateKey>().unwrap();
        assert!(!state.is_compacting(), "in-flight must be cleared");
        assert_eq!(state.boundaries.len(), 1, "boundary must be recorded");
        assert_eq!(state.boundaries[0].summary, "the summary");
    }

    #[test]
    fn try_consume_compaction_event_skips_swap_when_boundary_no_longer_present() {
        let store = store_with_compaction_plugin();
        let mut messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("only-msg")),
            Arc::new(Message::assistant("only-reply")),
        ];
        mark_in_flight(&store, "ghost-boundary-id");

        let consumed = try_consume_compaction_event(
            &mut messages,
            &completed_event("ghost-boundary-id", "irrelevant", 0),
            &store,
        );
        assert!(consumed);

        // No mutation: skip is benign.
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].text(), "only-msg");

        let state = store.read::<CompactionStateKey>().unwrap();
        assert!(
            !state.is_compacting(),
            "in-flight must clear even on benign skip"
        );
        assert!(
            state.boundaries.is_empty(),
            "no boundary should be recorded when swap was skipped"
        );
    }

    #[test]
    fn try_consume_compaction_event_clears_in_flight_on_failure() {
        let store = store_with_compaction_plugin();
        let mut messages: Vec<Arc<Message>> = vec![Arc::new(Message::user("x"))];
        mark_in_flight(&store, "any");

        let consumed =
            try_consume_compaction_event(&mut messages, &failed_event("any", "boom"), &store);
        assert!(consumed);

        let state = store.read::<CompactionStateKey>().unwrap();
        assert!(!state.is_compacting());
        assert!(
            state.boundaries.is_empty(),
            "failure must not record a boundary"
        );
    }

    #[test]
    fn try_consume_compaction_event_passes_through_unrelated_payloads() {
        let store = store_with_compaction_plugin();
        let mut messages: Vec<Arc<Message>> = vec![Arc::new(Message::user("x"))];

        // Other Custom event: not for compaction.
        let other = json!({
            "kind": "custom",
            "task_id": "bg_42",
            "event_type": "task.heartbeat",
            "payload": {"pct": 50},
        });
        assert!(!try_consume_compaction_event(&mut messages, &other, &store));

        // Plain non-Custom payload.
        let task_completed = json!({
            "kind": "completed",
            "task_id": "bg_43",
            "result": null,
        });
        assert!(!try_consume_compaction_event(
            &mut messages,
            &task_completed,
            &store
        ));
    }

    #[test]
    fn plan_compaction_returns_none_when_savings_below_threshold() {
        let messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("hi")),
            Arc::new(Message::assistant("hello")),
            Arc::new(Message::user("how are you?")),
            Arc::new(Message::assistant("fine")),
        ];
        let policy = ContextWindowPolicy {
            compaction_raw_suffix_messages: 1,
            ..Default::default()
        };
        assert!(plan_compaction(&messages, &policy).is_none());
    }

    #[test]
    fn plan_compaction_captures_boundary_message_id() {
        // Pad the head with enough tokens to clear MIN_COMPACTION_GAIN_TOKENS.
        let mut messages: Vec<Arc<Message>> = (0..6)
            .map(|i| {
                if i % 2 == 0 {
                    long_user("filler ", 600)
                } else {
                    Arc::new(Message::assistant("ack"))
                }
            })
            .collect();
        messages.push(Arc::new(Message::user("recent")));
        let policy = ContextWindowPolicy {
            compaction_raw_suffix_messages: 1,
            ..Default::default()
        };
        let plan = plan_compaction(&messages, &policy).expect("plan");
        // Boundary id must reference an actual message in the snapshot.
        assert!(
            messages
                .iter()
                .any(|m| m.id.as_deref() == Some(plan.boundary_message_id.as_str()))
        );
        assert!(plan.pre_tokens >= MIN_COMPACTION_GAIN_TOKENS);
        assert!(!plan.transcript.is_empty());
    }

    #[test]
    fn apply_summary_swaps_when_boundary_present() {
        let mut messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("old1")),
            Arc::new(Message::assistant("old2")),
            Arc::new(Message::user("BOUNDARY")),
            Arc::new(Message::assistant("after-boundary")),
            Arc::new(Message::user("appended-during-window")),
        ];
        let boundary_id = messages[2].id.clone().unwrap();

        let applied = apply_summary(&mut messages, &boundary_id, "synthetic summary").unwrap();
        assert_eq!(applied.boundary_index, 2);
        assert!(applied.pre_tokens > 0);
        assert!(applied.post_tokens > 0);

        // First message must now be the summary; messages after the boundary
        // (including ones appended during the compaction window) are kept.
        assert!(
            messages[0]
                .text()
                .contains("<conversation-summary>\nsynthetic summary"),
            "summary missing or malformed: {}",
            messages[0].text()
        );
        assert_eq!(messages[1].text(), "after-boundary");
        assert_eq!(messages[2].text(), "appended-during-window");
        assert_eq!(messages.len(), 3);
    }

    #[test]
    fn apply_summary_returns_none_when_boundary_already_gone() {
        let mut messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("a")),
            Arc::new(Message::assistant("b")),
        ];
        let original = messages.clone();
        assert!(apply_summary(&mut messages, "non-existent-id", "any").is_none());
        // Skip must be benign: the live list is unchanged.
        assert_eq!(messages.len(), original.len());
        for (a, b) in messages.iter().zip(original.iter()) {
            assert_eq!(a.text(), b.text());
        }
    }

    #[test]
    fn find_compaction_boundary_respects_tool_pairs() {
        let messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("start")),
            Arc::new(Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new("c1", "search", json!({}))],
            )),
            Arc::new(Message::tool("c1", "found")),
            Arc::new(Message::user("next")), // safe boundary here (idx 3)
            Arc::new(Message::assistant("reply")),
        ];

        let boundary = find_compaction_boundary(&messages, 0, messages.len());
        // Should be at idx 3 or 4 (after tool pair is complete)
        assert!(boundary.is_some());
        let b = boundary.unwrap();
        assert!(b >= 3);
    }

    #[test]
    fn trim_to_compaction_boundary_drops_pre_summary() {
        let mut messages = vec![
            Arc::new(Message::user("old msg 1")),
            Arc::new(Message::assistant("old reply")),
            Arc::new(Message::internal_system(
                "<conversation-summary>\nSummary of old messages\n</conversation-summary>",
            )),
            Arc::new(Message::user("new msg")),
            Arc::new(Message::assistant("new reply")),
        ];

        trim_to_compaction_boundary(&mut messages);
        assert_eq!(messages.len(), 3);
        assert!(messages[0].text().contains("conversation-summary"));
        assert_eq!(messages[1].text(), "new msg");
    }

    #[test]
    fn trim_to_compaction_boundary_noop_without_summary() {
        let mut messages = vec![
            Arc::new(Message::user("hello")),
            Arc::new(Message::assistant("hi")),
        ];
        let len_before = messages.len();
        trim_to_compaction_boundary(&mut messages);
        assert_eq!(messages.len(), len_before);
    }

    #[test]
    fn find_compaction_boundary_does_not_cut_open_tool_round() {
        let messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("start")),
            Arc::new(Message::assistant("reply")),
            Arc::new(Message::user("next")),
            Arc::new(Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new("c1", "search", json!({}))],
            )),
            // c1 has no result yet — open tool round
        ];

        let boundary = find_compaction_boundary(&messages, 0, messages.len());
        // Boundary should be before the open tool round (idx 2 at latest)
        if let Some(b) = boundary {
            assert!(b <= 2, "boundary should not include open tool round");
        }
    }

    #[test]
    fn trim_to_compaction_boundary_idempotent() {
        let mut messages = vec![
            Arc::new(Message::user("old")),
            Arc::new(Message::internal_system(
                "<conversation-summary>\nSummary\n</conversation-summary>",
            )),
            Arc::new(Message::user("new")),
        ];

        trim_to_compaction_boundary(&mut messages);
        let len_after_first = messages.len();

        trim_to_compaction_boundary(&mut messages);
        assert_eq!(
            messages.len(),
            len_after_first,
            "second trim should be noop"
        );
    }

    #[test]
    fn find_boundary_skips_open_tool_rounds() {
        let messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("start")),
            Arc::new(Message::assistant("ok")),
            Arc::new(Message::user("do something")),
            Arc::new(Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new("c1", "search", json!({}))],
            )),
            // c1 result is missing — open tool round
        ];

        let boundary = find_compaction_boundary(&messages, 0, messages.len());
        // Must not place boundary at or after the open tool call (idx 3)
        if let Some(b) = boundary {
            assert!(b < 3, "boundary {b} must be before open tool call at idx 3");
        }
    }

    #[test]
    fn find_boundary_respects_suffix_messages() {
        // Search only within a sub-range, leaving suffix messages untouched
        let messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("old1")),
            Arc::new(Message::assistant("reply1")),
            Arc::new(Message::user("old2")),
            Arc::new(Message::assistant("reply2")),
            // suffix: last 2 messages are "raw suffix"
            Arc::new(Message::user("recent")),
            Arc::new(Message::assistant("recent_reply")),
        ];

        let suffix_count = 2;
        let search_end = messages.len().saturating_sub(suffix_count);
        let boundary = find_compaction_boundary(&messages, 0, search_end);
        // Boundary must be within the searched range, not touching suffix
        if let Some(b) = boundary {
            assert!(
                b < search_end,
                "boundary {b} must be before suffix start {search_end}"
            );
        }
    }

    #[test]
    fn find_boundary_returns_none_when_too_few_messages() {
        // Single message — no safe compaction point
        let messages: Vec<Arc<Message>> = vec![Arc::new(Message::user("only message"))];
        // Search range is empty (start == end)
        let boundary = find_compaction_boundary(&messages, 0, 0);
        assert!(boundary.is_none(), "empty range should yield no boundary");

        // Range with only an open tool call — no safe boundary
        let messages2: Vec<Arc<Message>> = vec![Arc::new(Message::assistant_with_tool_calls(
            "",
            vec![ToolCall::new("c1", "fn", json!({}))],
        ))];
        let boundary2 = find_compaction_boundary(&messages2, 0, messages2.len());
        assert!(
            boundary2.is_none(),
            "single open tool call should yield no boundary"
        );
    }

    #[test]
    fn find_compaction_boundary_multiple_complete_tool_rounds() {
        let messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("start")),
            Arc::new(Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new("c1", "search", json!({}))],
            )),
            Arc::new(Message::tool("c1", "found it")),
            Arc::new(Message::user("next")),
            Arc::new(Message::assistant_with_tool_calls(
                "",
                vec![ToolCall::new("c2", "read", json!({}))],
            )),
            Arc::new(Message::tool("c2", "content")),
            Arc::new(Message::user("last")),
            Arc::new(Message::assistant("done")),
        ];

        let boundary = find_compaction_boundary(&messages, 0, messages.len());
        assert!(boundary.is_some());
        // Should be at or after idx 6 (after second tool round)
        let b = boundary.unwrap();
        assert!(
            b >= 6,
            "boundary should be after all tool rounds: got {}",
            b
        );
    }

    #[test]
    fn find_compaction_boundary_empty_range() {
        let messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("hello")),
            Arc::new(Message::assistant("hi")),
        ];
        let boundary = find_compaction_boundary(&messages, 0, 0);
        assert!(boundary.is_none(), "empty range should yield no boundary");
    }

    #[test]
    fn find_compaction_boundary_range_start_equals_end() {
        let messages: Vec<Arc<Message>> = vec![Arc::new(Message::user("only"))];
        let boundary = find_compaction_boundary(&messages, 1, 1);
        assert!(boundary.is_none());
    }

    #[test]
    fn trim_to_compaction_boundary_uses_last_summary() {
        let mut messages = vec![
            Arc::new(Message::user("old msg 1")),
            Arc::new(Message::internal_system(
                "<conversation-summary>\nFirst summary\n</conversation-summary>",
            )),
            Arc::new(Message::user("mid msg")),
            Arc::new(Message::internal_system(
                "<conversation-summary>\nSecond summary\n</conversation-summary>",
            )),
            Arc::new(Message::user("new msg")),
        ];

        trim_to_compaction_boundary(&mut messages);
        // Should trim to the LAST summary (index 3)
        assert_eq!(messages.len(), 2);
        assert!(messages[0].text().contains("Second summary"));
        assert_eq!(messages[1].text(), "new msg");
    }

    #[test]
    fn find_compaction_boundary_with_multiple_tool_calls_in_one_round() {
        let messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("do things")),
            Arc::new(Message::assistant_with_tool_calls(
                "",
                vec![
                    ToolCall::new("c1", "search", json!({})),
                    ToolCall::new("c2", "read", json!({})),
                ],
            )),
            Arc::new(Message::tool("c1", "found")),
            Arc::new(Message::tool("c2", "content")),
            Arc::new(Message::user("thanks")),
        ];

        let boundary = find_compaction_boundary(&messages, 0, messages.len());
        assert!(boundary.is_some());
        // Both tool results are present, so boundary can be after them
        let b = boundary.unwrap();
        assert!(
            b >= 3,
            "boundary should be after all tool results: got {}",
            b
        );
    }

    #[test]
    fn find_compaction_boundary_partial_tool_results() {
        // Two tool calls but only one result
        let messages: Vec<Arc<Message>> = vec![
            Arc::new(Message::user("start")),
            Arc::new(Message::assistant_with_tool_calls(
                "",
                vec![
                    ToolCall::new("c1", "search", json!({})),
                    ToolCall::new("c2", "read", json!({})),
                ],
            )),
            Arc::new(Message::tool("c1", "found")),
            // c2 result missing
        ];

        let boundary = find_compaction_boundary(&messages, 0, messages.len());
        // Should not place boundary after the incomplete tool round
        if let Some(b) = boundary {
            assert!(b < 1, "boundary should not include incomplete tool round");
        }
    }
}