awaken-runtime 0.6.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
use super::*;
use crate::state::StateStore;
use awaken_runtime_contract::contract::message::Message;

#[test]
fn compaction_state_record_boundary() {
    let mut state = CompactionState::default();
    assert_eq!(state.total_compactions, 0);
    assert!(state.boundaries.is_empty());

    state.reduce(CompactionAction::RecordBoundary(CompactionBoundary {
        summary: "User asked to implement feature X.".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 5000,
        post_tokens: 200,
        timestamp_ms: 1234567890,
    }));

    assert_eq!(state.total_compactions, 1);
    assert_eq!(state.boundaries.len(), 1);
    assert_eq!(
        state.latest_boundary().unwrap().summary,
        "User asked to implement feature X."
    );
}

#[test]
fn compaction_state_multiple_boundaries() {
    let mut state = CompactionState::default();

    for i in 0..3 {
        state.reduce(CompactionAction::RecordBoundary(CompactionBoundary {
            summary: format!("summary {i}"),
            task_id: None,
            boundary_message_id: None,
            pre_tokens: 1000 * (i + 1),
            post_tokens: 100 * (i + 1),
            timestamp_ms: 1000 + i as u64,
        }));
    }

    assert_eq!(state.total_compactions, 3);
    assert_eq!(state.boundaries.len(), 3);
    assert_eq!(state.latest_boundary().unwrap().summary, "summary 2");
}

#[test]
fn compaction_state_clear() {
    let mut state = CompactionState {
        boundaries: vec![CompactionBoundary {
            summary: "old".into(),
            task_id: None,
            boundary_message_id: None,
            pre_tokens: 100,
            post_tokens: 10,
            timestamp_ms: 1,
        }],
        failures: vec![CompactionFailure {
            task_id: Some("bg_old".into()),
            boundary_message_id: "msg-old".into(),
            error: "old error".into(),
            timestamp_ms: 2,
        }],
        skipped: Vec::new(),
        total_compactions: 1,
        in_flight: None,
    };

    state.reduce(CompactionAction::Clear);
    assert!(state.boundaries.is_empty());
    assert!(state.failures.is_empty());
    assert!(state.skipped.is_empty());
    assert_eq!(state.total_compactions, 0);
}

#[test]
fn compaction_state_latest_boundary_empty() {
    let state = CompactionState::default();
    assert!(state.latest_boundary().is_none());
}

#[test]
fn compaction_state_serde_roundtrip() {
    let state = CompactionState {
        boundaries: vec![
            CompactionBoundary {
                summary: "first".into(),
                task_id: None,
                boundary_message_id: None,
                pre_tokens: 5000,
                post_tokens: 200,
                timestamp_ms: 1000,
            },
            CompactionBoundary {
                summary: "second".into(),
                task_id: None,
                boundary_message_id: None,
                pre_tokens: 3000,
                post_tokens: 150,
                timestamp_ms: 2000,
            },
        ],
        failures: vec![CompactionFailure {
            task_id: Some("bg_failed".into()),
            boundary_message_id: "msg-failed".into(),
            error: "summarizer failed".into(),
            timestamp_ms: 2500,
        }],
        skipped: Vec::new(),
        total_compactions: 2,
        in_flight: None,
    };

    let json = serde_json::to_string(&state).unwrap();
    let parsed: CompactionState = serde_json::from_str(&json).unwrap();
    assert_eq!(parsed, state);
}

#[test]
fn compaction_plugin_registers_key() {
    let store = StateStore::new();
    store.install_plugin(CompactionPlugin::default()).unwrap();
    let registry = store.registry.lock();
    assert!(registry.keys_by_name.contains_key("__context_compaction"));
}

#[test]
fn compaction_plugin_state_via_store() {
    let store = StateStore::new();
    store.install_plugin(CompactionPlugin::default()).unwrap();

    let mut patch = store.begin_mutation();
    patch.update::<CompactionStateKey>(super::super::record_compaction_boundary(
        CompactionBoundary {
            summary: "test summary".into(),
            task_id: None,
            boundary_message_id: None,
            pre_tokens: 4000,
            post_tokens: 180,
            timestamp_ms: 9999,
        },
    ));
    store.commit(patch).unwrap();

    let state = store.read::<CompactionStateKey>().unwrap();
    assert_eq!(state.total_compactions, 1);
    assert_eq!(state.boundaries[0].summary, "test summary");
}

#[test]
fn record_compaction_boundary_constructor() {
    let action = super::super::record_compaction_boundary(CompactionBoundary {
        summary: "s".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 100,
        post_tokens: 10,
        timestamp_ms: 0,
    });
    assert!(matches!(action, CompactionAction::RecordBoundary(_)));
}

#[test]
fn compaction_state_record_then_clear_then_record() {
    let mut state = CompactionState::default();

    state.reduce(CompactionAction::RecordBoundary(CompactionBoundary {
        summary: "first".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 1000,
        post_tokens: 100,
        timestamp_ms: 1,
    }));
    assert_eq!(state.total_compactions, 1);

    state.reduce(CompactionAction::Clear);
    assert_eq!(state.total_compactions, 0);
    assert!(state.boundaries.is_empty());
    assert!(state.latest_boundary().is_none());

    state.reduce(CompactionAction::RecordBoundary(CompactionBoundary {
        summary: "after clear".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 2000,
        post_tokens: 150,
        timestamp_ms: 2,
    }));
    assert_eq!(state.total_compactions, 1);
    assert_eq!(state.latest_boundary().unwrap().summary, "after clear");
}

#[test]
fn compaction_state_key_properties() {
    assert_eq!(CompactionStateKey::KEY, "__context_compaction");
}

#[test]
fn compaction_state_key_apply() {
    let mut state = CompactionState::default();
    CompactionStateKey::apply(
        &mut state,
        CompactionAction::RecordBoundary(CompactionBoundary {
            summary: "via apply".into(),
            task_id: None,
            boundary_message_id: None,
            pre_tokens: 500,
            post_tokens: 50,
            timestamp_ms: 42,
        }),
    );
    assert_eq!(state.total_compactions, 1);
    assert_eq!(state.boundaries[0].summary, "via apply");
}

#[test]
fn compaction_plugin_descriptor_name() {
    let plugin = CompactionPlugin::default();
    assert_eq!(plugin.descriptor().name, CONTEXT_COMPACTION_PLUGIN_ID);
}

#[test]
fn compaction_plugin_new_with_config() {
    let config = CompactionConfig {
        min_savings_ratio: 0.8,
        ..Default::default()
    };
    let plugin = CompactionPlugin::new(config);
    assert!((plugin.config.min_savings_ratio - 0.8).abs() < f64::EPSILON);
}

#[test]
fn compaction_boundary_equality() {
    let a = CompactionBoundary {
        summary: "s".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 100,
        post_tokens: 10,
        timestamp_ms: 0,
    };
    let b = a.clone();
    assert_eq!(a, b);
}

#[test]
fn compaction_boundary_serde_roundtrip() {
    let boundary = CompactionBoundary {
        summary: "test summary".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 3000,
        post_tokens: 200,
        timestamp_ms: 1234567890,
    };
    let json = serde_json::to_string(&boundary).unwrap();
    let parsed: CompactionBoundary = serde_json::from_str(&json).unwrap();
    assert_eq!(parsed, boundary);
}

// -----------------------------------------------------------------------
// Migrated from uncarve: additional compaction state tests
// -----------------------------------------------------------------------

#[test]
fn compaction_state_default_is_empty() {
    let state = CompactionState::default();
    assert!(state.boundaries.is_empty());
    assert!(state.failures.is_empty());
    assert_eq!(state.total_compactions, 0);
    assert!(state.latest_boundary().is_none());
}

#[test]
fn compaction_state_records_failures_without_incrementing_total() {
    let mut state = CompactionState::default();
    state.reduce(CompactionAction::RecordFailure(CompactionFailure {
        task_id: Some("bg_1".into()),
        boundary_message_id: "msg-1".into(),
        error: "boom".into(),
        timestamp_ms: 42,
    }));

    assert_eq!(state.total_compactions, 0);
    assert!(state.boundaries.is_empty());
    assert_eq!(state.failures.len(), 1);
    assert_eq!(state.failures[0].task_id.as_deref(), Some("bg_1"));
    assert_eq!(state.failures[0].error, "boom");
}

#[test]
fn compaction_state_boundary_ordering_preserved() {
    let mut state = CompactionState::default();
    for i in 0..5 {
        state.reduce(CompactionAction::RecordBoundary(CompactionBoundary {
            summary: format!("boundary_{i}"),
            task_id: None,
            boundary_message_id: None,
            pre_tokens: 1000,
            post_tokens: 100,
            timestamp_ms: i as u64,
        }));
    }
    assert_eq!(state.boundaries.len(), 5);
    assert_eq!(state.total_compactions, 5);
    for (i, b) in state.boundaries.iter().enumerate() {
        assert_eq!(b.summary, format!("boundary_{i}"));
        assert_eq!(b.timestamp_ms, i as u64);
    }
}

#[test]
fn compaction_state_clear_twice_is_idempotent() {
    let mut state = CompactionState::default();
    state.reduce(CompactionAction::RecordBoundary(CompactionBoundary {
        summary: "s".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 1,
        post_tokens: 1,
        timestamp_ms: 0,
    }));
    state.reduce(CompactionAction::Clear);
    state.reduce(CompactionAction::Clear);
    assert!(state.boundaries.is_empty());
    assert_eq!(state.total_compactions, 0);
}

#[test]
fn compaction_config_default_has_sane_values() {
    let config = CompactionConfig::default();
    assert!(!config.summarizer_system_prompt.is_empty());
    assert!(config.summarizer_user_prompt.contains("{messages}"));
    assert!(config.min_savings_ratio > 0.0);
    assert!(config.min_savings_ratio < 1.0);
    assert!(config.summary_max_tokens.is_none());
    assert!(config.summary_model.is_none());
}

#[test]
fn compaction_config_serde_roundtrip() {
    let config = CompactionConfig {
        summarizer_system_prompt: "custom system".into(),
        summarizer_user_prompt: "custom user: {messages}".into(),
        summary_max_tokens: Some(512),
        summary_model: Some("claude-3-haiku".into()),
        min_savings_ratio: 0.5,
        ..Default::default()
    };
    let json = serde_json::to_string(&config).unwrap();
    let parsed: CompactionConfig = serde_json::from_str(&json).unwrap();
    assert_eq!(
        parsed.summarizer_system_prompt,
        config.summarizer_system_prompt
    );
    assert_eq!(parsed.summary_max_tokens, Some(512));
    assert_eq!(parsed.summary_model.as_deref(), Some("claude-3-haiku"));
}

#[test]
fn compaction_state_pre_post_tokens_preserved() {
    let mut state = CompactionState::default();
    state.reduce(CompactionAction::RecordBoundary(CompactionBoundary {
        summary: "test".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 10_000,
        post_tokens: 500,
        timestamp_ms: 99,
    }));
    let b = state.latest_boundary().unwrap();
    assert_eq!(b.pre_tokens, 10_000);
    assert_eq!(b.post_tokens, 500);
    assert_eq!(b.timestamp_ms, 99);
}

#[test]
fn context_transform_plugin_descriptor_name() {
    let policy = awaken_runtime_contract::contract::inference::ContextWindowPolicy::default();
    let plugin = ContextTransformPlugin::new(policy);
    assert_eq!(plugin.descriptor().name, CONTEXT_TRANSFORM_PLUGIN_ID);
}

// -----------------------------------------------------------------------
// Additional compaction tests
// -----------------------------------------------------------------------

#[test]
fn compaction_fires_at_threshold() {
    // Verify savings ratio check: only accept compaction when savings >= min_savings_ratio
    let config = CompactionConfig {
        min_savings_ratio: 0.5,
        ..Default::default()
    };
    let boundary_good = CompactionBoundary {
        summary: "good".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 1000,
        post_tokens: 400, // 60% savings > 50% threshold
        timestamp_ms: 1,
    };
    let savings_good = 1.0 - (boundary_good.post_tokens as f64 / boundary_good.pre_tokens as f64);
    assert!(
        savings_good >= config.min_savings_ratio,
        "60% savings should meet 50% threshold"
    );

    let boundary_bad = CompactionBoundary {
        summary: "bad".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 1000,
        post_tokens: 600, // 40% savings < 50% threshold
        timestamp_ms: 2,
    };
    let savings_bad = 1.0 - (boundary_bad.post_tokens as f64 / boundary_bad.pre_tokens as f64);
    assert!(
        savings_bad < config.min_savings_ratio,
        "40% savings should not meet 50% threshold"
    );
}

#[test]
fn compaction_state_tracks_across_multiple_rounds() {
    let mut state = CompactionState::default();
    // Simulate 5 compaction rounds with increasing pre-token counts
    for round in 1..=5u64 {
        state.reduce(CompactionAction::RecordBoundary(CompactionBoundary {
            summary: format!("round {round}"),
            task_id: None,
            boundary_message_id: None,
            pre_tokens: 1000 * round as usize,
            post_tokens: 100 * round as usize,
            timestamp_ms: round * 1000,
        }));
        assert_eq!(state.total_compactions, round);
        assert_eq!(state.boundaries.len(), round as usize);
    }
    // Latest boundary should be the last round
    assert_eq!(state.latest_boundary().unwrap().summary, "round 5");
    assert_eq!(state.latest_boundary().unwrap().pre_tokens, 5000);
}

#[test]
fn compaction_config_serialization_omits_none_fields() {
    let config = CompactionConfig::default();
    let json = serde_json::to_value(&config).unwrap();
    // summary_max_tokens and summary_model are None, should be omitted via skip_serializing_if
    assert!(
        !json.as_object().unwrap().contains_key("summary_max_tokens"),
        "None fields should be omitted"
    );
    assert!(
        !json.as_object().unwrap().contains_key("summary_model"),
        "None fields should be omitted"
    );
}

#[test]
fn compaction_config_serialization_includes_some_fields() {
    let config = CompactionConfig {
        summary_max_tokens: Some(1024),
        summary_model: Some("claude-3-sonnet".into()),
        ..Default::default()
    };
    let json = serde_json::to_value(&config).unwrap();
    assert_eq!(json["summary_max_tokens"], 1024);
    assert_eq!(json["summary_model"], "claude-3-sonnet");
}

#[test]
fn compaction_with_tool_messages_records_correctly() {
    // Simulate compaction that includes tool messages in the summarized range
    let store = StateStore::new();
    store.install_plugin(CompactionPlugin::default()).unwrap();

    // Record a boundary representing a range that included tool messages
    let mut patch = store.begin_mutation();
    patch.update::<CompactionStateKey>(super::super::record_compaction_boundary(
            CompactionBoundary {
                summary: "User asked to search files. Tool search returned 3 results. Assistant presented findings.".into(),
                task_id: None,
                boundary_message_id: None,
                pre_tokens: 8000,
                post_tokens: 200,
                timestamp_ms: 1000,
            },
        ));
    store.commit(patch).unwrap();

    let state = store.read::<CompactionStateKey>().unwrap();
    assert_eq!(state.total_compactions, 1);
    assert!(state.boundaries[0].summary.contains("Tool search"));
    assert_eq!(state.boundaries[0].pre_tokens, 8000);
}

#[test]
fn context_transform_plugin_registers_transform() {
    use crate::plugins::PluginRegistrar;
    let policy = awaken_runtime_contract::contract::inference::ContextWindowPolicy::default();
    let plugin = ContextTransformPlugin::new(policy);
    let mut registrar = PluginRegistrar::new();
    plugin.register(&mut registrar).unwrap();
    assert_eq!(
        registrar.request_transforms.len(),
        1,
        "should have registered one transform"
    );
    assert_eq!(
        registrar.request_transforms[0].plugin_id,
        CONTEXT_TRANSFORM_PLUGIN_ID
    );
}

#[test]
fn transform_ordering_compaction_then_context() {
    use crate::plugins::PluginRegistrar;
    // Compaction plugin should register no transforms
    let mut reg_compaction = PluginRegistrar::new();
    CompactionPlugin::default()
        .register(&mut reg_compaction)
        .unwrap();
    assert!(
        reg_compaction.request_transforms.is_empty(),
        "CompactionPlugin should not register request transforms"
    );
    // ContextTransformPlugin should register exactly one transform
    let policy = awaken_runtime_contract::contract::inference::ContextWindowPolicy::default();
    let mut reg_transform = PluginRegistrar::new();
    ContextTransformPlugin::new(policy)
        .register(&mut reg_transform)
        .unwrap();
    assert_eq!(reg_transform.request_transforms.len(), 1);
}

#[test]
fn token_count_estimation_for_various_content_types() {
    use awaken_runtime_contract::contract::transform::estimate_message_tokens;

    // Text message
    let text_msg = Message::user("Hello, this is a test message with some content.");
    let text_tokens = estimate_message_tokens(&text_msg);
    assert!(
        text_tokens > 4,
        "text message should have tokens beyond overhead"
    );

    // Empty content message
    let empty_msg = Message::user("");
    let empty_tokens = estimate_message_tokens(&empty_msg);
    assert_eq!(
        empty_tokens, 4,
        "empty message should have only overhead tokens"
    );

    // Very long message
    let long_msg = Message::user("x".repeat(4000));
    let long_tokens = estimate_message_tokens(&long_msg);
    assert!(
        long_tokens >= 1000,
        "4000-char message should estimate >= 1000 tokens, got {long_tokens}"
    );
}

#[test]
fn enable_prompt_cache_flag_in_policy() {
    let policy_cached = awaken_runtime_contract::contract::inference::ContextWindowPolicy {
        enable_prompt_cache: true,
        ..Default::default()
    };
    assert!(policy_cached.enable_prompt_cache);

    let policy_uncached = awaken_runtime_contract::contract::inference::ContextWindowPolicy {
        enable_prompt_cache: false,
        ..Default::default()
    };
    assert!(!policy_uncached.enable_prompt_cache);

    // Both should create valid transform plugins
    let _ = ContextTransformPlugin::new(policy_cached);
    let _ = ContextTransformPlugin::new(policy_uncached);
}

#[test]
fn autocompact_threshold_check() {
    use awaken_runtime_contract::contract::transform::estimate_tokens;

    let policy_with_threshold = awaken_runtime_contract::contract::inference::ContextWindowPolicy {
        autocompact_threshold: Some(500),
        ..Default::default()
    };

    // Simulate checking if messages exceed autocompact threshold
    let messages = vec![Message::user("short"), Message::assistant("reply")];
    let total = estimate_tokens(&messages);
    assert!(
        total < policy_with_threshold.autocompact_threshold.unwrap(),
        "short conversation should be under threshold"
    );

    // Longer conversation should exceed threshold
    let long_messages: Vec<Message> = (0..100)
        .map(|i| Message::user(format!("message {i} with some filler text to add tokens")))
        .collect();
    let long_total = estimate_tokens(&long_messages);
    assert!(
        long_total > policy_with_threshold.autocompact_threshold.unwrap(),
        "100-message conversation should exceed threshold of 500, got {long_total}"
    );
}

#[test]
fn in_flight_set_and_clear_round_trip() {
    let mut state = CompactionState::default();
    assert!(!state.is_compacting());

    state.reduce(CompactionAction::SetInFlight(CompactionInFlight {
        task_id: "bg_42".into(),
        boundary_message_id: "01HZ-msg-01".into(),
        started_at_ms: 100,
    }));
    let live = state.in_flight.as_ref().expect("in-flight set");
    assert_eq!(live.task_id, "bg_42");
    assert_eq!(live.boundary_message_id, "01HZ-msg-01");
    assert!(state.is_compacting());

    state.reduce(CompactionAction::ClearInFlight);
    assert!(state.in_flight.is_none());
    assert!(!state.is_compacting());
}

#[test]
fn clear_action_resets_in_flight_too() {
    let mut state = CompactionState::default();
    state.reduce(CompactionAction::SetInFlight(CompactionInFlight {
        task_id: "bg_1".into(),
        boundary_message_id: "msg-id".into(),
        started_at_ms: 1,
    }));
    state.reduce(CompactionAction::Clear);
    assert!(state.in_flight.is_none());
    assert!(state.boundaries.is_empty());
}

#[test]
fn record_boundary_does_not_touch_in_flight() {
    let mut state = CompactionState::default();
    state.reduce(CompactionAction::SetInFlight(CompactionInFlight {
        task_id: "bg_99".into(),
        boundary_message_id: "msg".into(),
        started_at_ms: 1,
    }));
    state.reduce(CompactionAction::RecordBoundary(CompactionBoundary {
        summary: "s".into(),
        task_id: None,
        boundary_message_id: None,
        pre_tokens: 10,
        post_tokens: 1,
        timestamp_ms: 2,
    }));
    // RecordBoundary alone does not clear the marker — the inbox
    // event router is responsible for the explicit ClearInFlight.
    assert!(state.is_compacting());
    assert_eq!(state.boundaries.len(), 1);
}

#[test]
fn compaction_action_serde_roundtrip() {
    let actions = vec![
        CompactionAction::RecordBoundary(CompactionBoundary {
            summary: "s".into(),
            task_id: None,
            boundary_message_id: None,
            pre_tokens: 1,
            post_tokens: 1,
            timestamp_ms: 0,
        }),
        CompactionAction::RecordFailure(CompactionFailure {
            task_id: Some("bg".into()),
            boundary_message_id: "msg".into(),
            error: "boom".into(),
            timestamp_ms: 1,
        }),
        CompactionAction::Clear,
    ];
    for action in actions {
        let json = serde_json::to_string(&action).unwrap();
        let parsed: CompactionAction = serde_json::from_str(&json).unwrap();
        // Verify the action type roundtrips
        match (&action, &parsed) {
            (CompactionAction::Clear, CompactionAction::Clear) => {}
            (CompactionAction::RecordBoundary(a), CompactionAction::RecordBoundary(b)) => {
                assert_eq!(a.summary, b.summary);
            }
            (CompactionAction::RecordFailure(a), CompactionAction::RecordFailure(b)) => {
                assert_eq!(a, b);
            }
            _ => panic!("action type mismatch after serde roundtrip"),
        }
    }
}