phi-core 0.10.0

Simple, effective agent loop with tool execution and event streaming
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
//! Tests for the non-destructive compaction overlay system.

use chrono::Utc;
use phi_core::context::*;
use phi_core::session::{LoopRecord, LoopStatus, Session, SessionFormation, SessionScope};
use phi_core::ContinuationKind;
use phi_core::*;

// ---------------------------------------------------------------------------
// Helper: create a simple LoopRecord with N turns of user+assistant pairs
// ---------------------------------------------------------------------------
fn make_loop_record(loop_id: &str, num_turns: u32, parent: Option<&str>) -> LoopRecord {
    let mut messages = Vec::new();
    for t in 0..num_turns {
        let tid = Some(TurnId {
            loop_id: loop_id.to_string(),
            turn_index: t,
        });
        messages.push(
            AgentMessage::from(Message::user(format!("Turn {} question", t)))
                .with_turn_id(tid.clone()),
        );
        messages.push(
            AgentMessage::from(Message::Assistant {
                content: vec![Content::Text {
                    text: format!("Turn {} answer with some content that is meaningful", t),
                }],
                stop_reason: StopReason::Stop,
                model: "test".into(),
                provider: "test".into(),
                usage: Usage::default(),
                timestamp: 0,
                error_message: None,
            })
            .with_turn_id(tid.clone()),
        );
    }
    LoopRecord {
        loop_id: loop_id.to_string(),
        session_id: "test-session".to_string(),
        agent_id: "test-agent".to_string(),
        parent_loop_id: parent.map(|s| s.to_string()),
        continuation_kind: ContinuationKind::Initial,
        started_at: Utc::now(),
        ended_at: Some(Utc::now()),
        status: LoopStatus::Completed,
        rejection: None,
        config: None,
        messages,
        turns: Vec::new(),
        usage: Usage::default(),
        metadata: None,
        events: Vec::new(),
        children_loop_ids: Vec::new(),
        child_loop_refs: Vec::new(),
        parallel_group: None,
        compaction_block: None,
    }
}

fn make_session(loops: Vec<LoopRecord>) -> Session {
    let now = Utc::now();
    Session {
        session_id: "test-session".to_string(),
        agent_id: "test-agent".to_string(),
        created_at: now,
        last_active_at: now,
        formation: SessionFormation::Explicit { timestamp: now },
        parent_spawn_ref: None,
        scope: SessionScope::Ephemeral,
        loops,
    }
}

// ---------------------------------------------------------------------------
// TurnMap tests
// ---------------------------------------------------------------------------

#[test]
fn test_turn_map_from_messages_groups_by_turn_id() {
    let loop_id = "test.model.1";
    let mut messages = Vec::new();
    // Turn 0: user + assistant + tool_result (3 messages)
    for _ in 0..3 {
        messages.push(
            AgentMessage::from(Message::user("hi")).with_turn_id(Some(TurnId {
                loop_id: loop_id.into(),
                turn_index: 0,
            })),
        );
    }
    // Turn 1: user + assistant (2 messages)
    for _ in 0..2 {
        messages.push(
            AgentMessage::from(Message::user("hello")).with_turn_id(Some(TurnId {
                loop_id: loop_id.into(),
                turn_index: 1,
            })),
        );
    }

    let tm = TurnMap::from_messages(&messages);
    assert_eq!(tm.turn_count(), 2);

    let range0 = TurnRange {
        start_turn: 0,
        end_turn: 0,
    };
    assert_eq!(tm.messages_for_range(&range0, &messages).len(), 3);

    let range1 = TurnRange {
        start_turn: 1,
        end_turn: 1,
    };
    assert_eq!(tm.messages_for_range(&range1, &messages).len(), 2);

    let full_range = TurnRange {
        start_turn: 0,
        end_turn: 1,
    };
    assert_eq!(tm.messages_for_range(&full_range, &messages).len(), 5);
}

#[test]
fn test_turn_map_legacy_messages_without_turn_id() {
    let messages = vec![
        AgentMessage::from(Message::user("no turn id 1")),
        AgentMessage::from(Message::user("no turn id 2")),
    ];
    let tm = TurnMap::from_messages(&messages);
    // Each legacy message is its own group
    assert_eq!(tm.turn_count(), 2);
}

// ---------------------------------------------------------------------------
// CompactionBlock creation tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_compaction_block_creation_most_recent() {
    let record = make_loop_record("test.model.1", 10, None);
    let config = CompactionConfig::default(); // keep_first=2, keep_recent=10, max_summary=2000

    let block = DefaultBlockCompaction.compact(&record, &config, true).await;

    // With 10 turns and keep_first=2, keep_recent=10, there's no middle
    // (2 + 10 > 10), so keep_compacted should be None
    assert!(block.keep_first.is_some());
    assert!(block.keep_compacted.is_none()); // No room for middle
}

#[tokio::test]
async fn test_compaction_block_creation_with_middle() {
    let record = make_loop_record("test.model.1", 20, None);
    let config = CompactionConfig {
        keep_first_turns: 2,
        keep_recent_turns: 5,
        max_summary_tokens: 2_000,
        ..CompactionConfig::default()
    };

    let block = DefaultBlockCompaction.compact(&record, &config, true).await;

    assert!(block.keep_first.is_some());
    let kf = block.keep_first.unwrap();
    assert_eq!(kf.start_turn, 0);
    assert_eq!(kf.end_turn, 1); // turns 0-1

    assert!(block.keep_compacted.is_some());
    let kc = block.keep_compacted.unwrap();
    assert_eq!(kc.range.start_turn, 2);
    assert_eq!(kc.range.end_turn, 14); // turns 2-14

    assert!(block.keep_recent.is_some());
    let kr = block.keep_recent.unwrap();
    assert_eq!(kr.range.start_turn, 15);
    assert_eq!(kr.range.end_turn, 19); // turns 15-19
}

#[tokio::test]
async fn test_compaction_block_creation_earlier_loop() {
    let record = make_loop_record("test.model.1", 10, None);
    let config = CompactionConfig::default();

    let block = DefaultBlockCompaction
        .compact(&record, &config, false)
        .await;

    // Earlier loops: only keep_compacted, no keep_first or keep_recent
    assert!(block.keep_first.is_none());
    assert!(block.keep_recent.is_none());
    assert!(block.keep_compacted.is_some());
}

// ---------------------------------------------------------------------------
// build_context_from_session tests
// ---------------------------------------------------------------------------

#[test]
fn test_build_context_falls_back_to_raw() {
    let record = make_loop_record("test.model.1", 3, None);
    let session = make_session(vec![record]);
    let config = CompactionConfig::default();

    let context = build_context_from_session(&session, "test.model.1", &config, 100_000, None);

    // No compaction block → raw messages loaded (3 turns × 2 msgs = 6)
    assert_eq!(context.len(), 6);
}

#[tokio::test]
async fn test_build_context_from_session_with_blocks() {
    let mut record = make_loop_record("test.model.1", 20, None);
    let config = CompactionConfig {
        keep_first_turns: 2,
        keep_recent_turns: 5,
        max_summary_tokens: 2_000,
        ..CompactionConfig::default()
    };

    // Create a compaction block
    record.compaction_block = Some(DefaultBlockCompaction.compact(&record, &config, true).await);

    let session = make_session(vec![record]);
    let context = build_context_from_session(&session, "test.model.1", &config, 100_000, None);

    // Should have: keep_first messages + keep_compacted summaries + keep_recent messages
    // Much fewer than the original 40 messages (20 turns × 2)
    assert!(context.len() < 40);
    assert!(!context.is_empty());
}

// ---------------------------------------------------------------------------
// compact_session_loops tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_compact_session_loops_writes_earlier() {
    let loop1 = make_loop_record("test.model.1", 5, None);
    let loop2 = make_loop_record("test.model.2", 5, Some("test.model.1"));
    let loop3 = make_loop_record("test.model.3", 10, Some("test.model.2"));
    let mut session = make_session(vec![loop1, loop2, loop3]);

    let config = CompactionConfig {
        compaction_scope: CompactionScope::FixedCount(2),
        keep_first_turns: 1,
        keep_recent_turns: 3,
        max_summary_tokens: 2_000,
        ..CompactionConfig::default()
    };

    compact_session_loops(
        &mut session,
        "test.model.3",
        &DefaultBlockCompaction,
        &config,
        100_000,
        None,
    )
    .await;

    // Current loop should have a block
    assert!(session
        .get_loop("test.model.3")
        .unwrap()
        .compaction_block
        .is_some());
    // Earlier loop should also have a block (compact_earlier_loops = 2)
    assert!(session
        .get_loop("test.model.2")
        .unwrap()
        .compaction_block
        .is_some());
    assert!(session
        .get_loop("test.model.1")
        .unwrap()
        .compaction_block
        .is_some());
}

// ---------------------------------------------------------------------------
// Percentage threshold test
// ---------------------------------------------------------------------------

#[test]
fn test_pct_threshold_calculation() {
    let config = CompactionConfig::default(); // 90%, 5%
    let max_tokens = 100_000usize;
    let system_tokens = 4_000usize;

    // At 80k tokens: headroom = 0.90 - 0.04 - 0.80 = 0.06 > 0.05 → no compaction
    let system_frac = system_tokens as f64 / max_tokens as f64;
    let current_frac = 80_000f64 / max_tokens as f64;
    let headroom = config.compact_at_pct - system_frac - current_frac;
    assert!(headroom >= config.compact_budget_threshold_pct);

    // At 82k tokens: headroom = 0.90 - 0.04 - 0.82 = 0.04 < 0.05 → compaction fires
    let current_frac = 82_000f64 / max_tokens as f64;
    let headroom = config.compact_at_pct - system_frac - current_frac;
    assert!(headroom < config.compact_budget_threshold_pct);
}

// ---------------------------------------------------------------------------
// Serialization round-trip test
// ---------------------------------------------------------------------------

#[test]
fn test_compaction_block_serialization_roundtrip() {
    let block = CompactionBlock {
        keep_first: Some(TurnRange {
            start_turn: 0,
            end_turn: 1,
        }),
        keep_compacted: Some(CompactedSection {
            range: TurnRange {
                start_turn: 2,
                end_turn: 5,
            },
            messages: vec![AgentMessage::from(Message::user("[Summary] test"))],
        }),
        keep_recent: None,
        created_at: Utc::now(),
    };

    let json = serde_json::to_string(&block).unwrap();
    let deserialized: CompactionBlock = serde_json::from_str(&json).unwrap();

    assert_eq!(block.keep_first, deserialized.keep_first);
    assert!(deserialized.keep_compacted.is_some());
    assert!(deserialized.keep_recent.is_none());
}

#[test]
fn test_turn_id_serialization_roundtrip() {
    let msg = AgentMessage::from(Message::user("hello")).with_turn_id(Some(TurnId {
        loop_id: "test.model.1".into(),
        turn_index: 3,
    }));

    let json = serde_json::to_string(&msg).unwrap();
    assert!(json.contains("turnId"));
    assert!(json.contains("turnIndex"));

    let deserialized: AgentMessage = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized.turn_id().unwrap().turn_index, 3);
}

#[test]
fn test_turn_id_backward_compat_deserialization() {
    // Old format: no turnId field
    let json = r#"{"role":"user","content":[{"type":"text","text":"hello"}],"timestamp":0}"#;
    let msg: AgentMessage = serde_json::from_str(json).unwrap();
    assert!(msg.turn_id().is_none());
    assert_eq!(msg.role(), "user");
}

// ---------------------------------------------------------------------------
// CompactionScope::TokenBudget tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_compaction_scope_token_budget_partial() {
    // Create 3 loops: loop1 has ~50 tokens, loop2 has ~50, loop3 (current) has ~50
    // With max_context_tokens = 80, only loop2 should be in scope (loop1 exceeds budget)
    let loop1 = make_loop_record("test.model.1", 5, None); // ~5 turns × 2 msgs
    let loop2 = make_loop_record("test.model.2", 3, Some("test.model.1"));
    let loop3 = make_loop_record("test.model.3", 3, Some("test.model.2"));
    let mut session = make_session(vec![loop1, loop2, loop3]);

    let config = CompactionConfig {
        compaction_scope: CompactionScope::TokenBudget,
        keep_first_turns: 1,
        keep_recent_turns: 2,
        max_summary_tokens: 2_000,
        ..CompactionConfig::default()
    };

    // Use a small token budget so not all loops fit
    let small_budget =
        phi_core::context::total_tokens(&session.get_loop("test.model.2").unwrap().messages) + 10; // Just enough for loop2's messages + a little buffer

    compact_session_loops(
        &mut session,
        "test.model.3",
        &DefaultBlockCompaction,
        &config,
        small_budget,
        None,
    )
    .await;

    // Current loop (3) always gets a block
    assert!(session
        .get_loop("test.model.3")
        .unwrap()
        .compaction_block
        .is_some());
    // Loop2 should be in scope (fits in budget)
    assert!(session
        .get_loop("test.model.2")
        .unwrap()
        .compaction_block
        .is_some());
    // Loop1 should NOT be compacted (exceeds remaining budget)
    assert!(session
        .get_loop("test.model.1")
        .unwrap()
        .compaction_block
        .is_none());
}

#[tokio::test]
async fn test_compaction_scope_token_budget_all_fit() {
    let loop1 = make_loop_record("test.model.1", 2, None);
    let loop2 = make_loop_record("test.model.2", 2, Some("test.model.1"));
    let mut session = make_session(vec![loop1, loop2]);

    let config = CompactionConfig {
        compaction_scope: CompactionScope::TokenBudget,
        ..CompactionConfig::default()
    };

    // Large budget — all loops fit
    compact_session_loops(
        &mut session,
        "test.model.2",
        &DefaultBlockCompaction,
        &config,
        1_000_000,
        None,
    )
    .await;

    assert!(session
        .get_loop("test.model.2")
        .unwrap()
        .compaction_block
        .is_some());
    assert!(session
        .get_loop("test.model.1")
        .unwrap()
        .compaction_block
        .is_some());
}

#[tokio::test]
async fn test_build_context_token_budget_scope() {
    let mut loop1 = make_loop_record("test.model.1", 5, None);
    let mut loop2 = make_loop_record("test.model.2", 5, Some("test.model.1"));
    let mut loop3 = make_loop_record("test.model.3", 5, Some("test.model.2"));

    let config = CompactionConfig {
        compaction_scope: CompactionScope::TokenBudget,
        keep_first_turns: 1,
        keep_recent_turns: 2,
        max_summary_tokens: 2_000,
        ..CompactionConfig::default()
    };

    // Create blocks on all loops
    loop1.compaction_block = Some(DefaultBlockCompaction.compact(&loop1, &config, false).await);
    loop2.compaction_block = Some(DefaultBlockCompaction.compact(&loop2, &config, false).await);
    loop3.compaction_block = Some(DefaultBlockCompaction.compact(&loop3, &config, true).await);

    let session = make_session(vec![loop1, loop2, loop3]);

    // With a small budget, only recent loops should be loaded
    let small_budget = 200;
    let context = build_context_from_session(&session, "test.model.3", &config, small_budget, None);

    // With a large budget, all loops should contribute
    let large_budget = 1_000_000;
    let context_large =
        build_context_from_session(&session, "test.model.3", &config, large_budget, None);

    // Large budget should load more messages than small budget
    assert!(context_large.len() >= context.len());
}