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
//! Message truncation: find split points that respect token budgets and tool-call pairing.

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

/// Find the split point in history that fits the token budget.
///
/// Always keeps at least `min_recent` messages from the end.
/// Adjusts boundaries to avoid splitting tool call/result pairs.
pub fn find_split_point(history: &[Message], budget_tokens: usize, min_recent: usize) -> usize {
    if history.is_empty() {
        return 0;
    }

    let must_keep = min_recent.min(history.len());
    let must_keep_start = history.len().saturating_sub(must_keep);

    let mut used_tokens = 0usize;
    let mut candidate_split = history.len();

    for i in (0..history.len()).rev() {
        let msg_tokens = estimate_message_tokens(&history[i]);
        let new_total = used_tokens + msg_tokens;

        if i >= must_keep_start {
            used_tokens = new_total;
            candidate_split = i;
            continue;
        }

        if new_total > budget_tokens {
            break;
        }

        used_tokens = new_total;
        candidate_split = i;
    }

    adjust_split_for_tool_pairs(history, candidate_split)
}

/// Adjust split to avoid orphaning tool call/result pairs.
pub fn adjust_split_for_tool_pairs(history: &[Message], mut split: usize) -> usize {
    if split == 0 || split >= history.len() {
        return split;
    }

    // If first kept message is Tool, move split backward to include paired Assistant
    while split > 0 && history[split].role == Role::Tool {
        split -= 1;
    }

    // If last dropped message is Assistant with tool_calls,
    // move split forward to drop orphaned tool results
    if split > 0 {
        let last_dropped = &history[split - 1];
        if last_dropped.role == Role::Assistant && last_dropped.tool_calls.is_some() {
            while split < history.len() && history[split].role == Role::Tool {
                split += 1;
            }
        }
    }

    split
}

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

    #[test]
    fn find_split_empty_history() {
        let split = find_split_point(&[], 1000, 5);
        assert_eq!(split, 0);
    }

    #[test]
    fn find_split_all_within_budget() {
        let history = vec![Message::user("Hi"), Message::assistant("Hello!")];
        let split = find_split_point(&history, 100_000, 2);
        assert_eq!(split, 0, "everything fits, nothing to drop");
    }

    #[test]
    fn find_split_drops_oldest_when_tight() {
        let history: Vec<Message> = (0..20)
            .map(|i| {
                if i % 2 == 0 {
                    Message::user(format!("msg {i}"))
                } else {
                    Message::assistant(format!("reply {i}"))
                }
            })
            .collect();
        let split = find_split_point(&history, 30, 2);
        assert!(split > 0, "some messages should be dropped");
        let kept = history.len() - split;
        assert!(kept >= 2, "must keep at least min_recent=2");
    }

    #[test]
    fn find_split_respects_min_recent_even_beyond_budget() {
        let history: Vec<Message> = (0..10)
            .map(|i| Message::user(format!("message {i} with padding")))
            .collect();
        let split = find_split_point(&history, 1, 5);
        let kept = history.len() - split;
        assert!(kept >= 5, "must keep at least min_recent=5, kept={kept}");
    }

    #[test]
    fn find_split_min_recent_exceeds_history_len() {
        let history = vec![Message::user("a"), Message::assistant("b")];
        let split = find_split_point(&history, 1, 100);
        assert_eq!(split, 0, "min_recent > len means keep all");
    }

    #[test]
    fn adjust_split_moves_back_for_orphaned_tool_result() {
        let history = vec![
            Message::user("a"),
            Message::assistant_with_tool_calls("b", vec![ToolCall::new("c1", "t", json!({}))]),
            Message::tool("c1", "result"),
            Message::user("c"),
        ];
        let adjusted = adjust_split_for_tool_pairs(&history, 2);
        assert_eq!(adjusted, 1, "should include the assistant with tool calls");
    }

    #[test]
    fn adjust_split_drops_orphaned_results_after_dropped_assistant() {
        let history = vec![
            Message::user("a"),
            Message::assistant_with_tool_calls("b", vec![ToolCall::new("c1", "t", json!({}))]),
            Message::tool("c1", "result"),
            Message::user("c"),
            Message::assistant("answer"),
        ];
        let adjusted = adjust_split_for_tool_pairs(&history, 3);
        assert_eq!(adjusted, 3, "split at user boundary should be stable");
    }

    #[test]
    fn adjust_split_handles_multiple_consecutive_tool_results() {
        let history = vec![
            Message::user("start"),
            Message::assistant_with_tool_calls(
                "calling two",
                vec![
                    ToolCall::new("c1", "t1", json!({})),
                    ToolCall::new("c2", "t2", json!({})),
                ],
            ),
            Message::tool("c1", "r1"),
            Message::tool("c2", "r2"),
            Message::user("continue"),
        ];
        assert_eq!(adjust_split_for_tool_pairs(&history, 2), 1);
        assert_eq!(adjust_split_for_tool_pairs(&history, 3), 1);
    }

    #[test]
    fn adjust_split_at_zero_is_noop() {
        let history = vec![Message::user("a"), Message::assistant("b")];
        assert_eq!(adjust_split_for_tool_pairs(&history, 0), 0);
    }

    #[test]
    fn adjust_split_at_len_minus_one_non_tool() {
        // When split is at the last element and it's not a Tool, no adjustment needed
        let history = vec![Message::user("a"), Message::assistant("b")];
        assert_eq!(adjust_split_for_tool_pairs(&history, 1), 1);
    }

    #[test]
    fn adjust_split_at_end_returns_len() {
        let history = vec![Message::user("a"), Message::assistant("b")];
        // split == history.len() means keep nothing; guard returns early
        assert_eq!(adjust_split_for_tool_pairs(&history, 2), 2);
    }

    #[test]
    fn adjust_split_backward_then_no_forward() {
        let history = vec![
            Message::user("start"),
            Message::assistant_with_tool_calls(
                "calling",
                vec![ToolCall::new("c1", "t", json!({}))],
            ),
            Message::tool("c1", "result"),
            Message::user("next"),
        ];
        let adjusted = adjust_split_for_tool_pairs(&history, 2);
        assert_eq!(adjusted, 1);
    }

    // -----------------------------------------------------------------------
    // Additional truncation tests
    // -----------------------------------------------------------------------

    #[test]
    fn find_split_with_various_history_lengths() {
        // Verify split works correctly for history lengths 1 through 30
        for len in 1..=30 {
            let history: Vec<Message> = (0..len)
                .map(|i| {
                    if i % 2 == 0 {
                        Message::user(format!("u{i}"))
                    } else {
                        Message::assistant(format!("a{i}"))
                    }
                })
                .collect();
            let split = find_split_point(&history, 100_000, 2);
            assert!(split <= history.len(), "split out of range for len={len}");
            let kept = history.len() - split;
            assert!(
                kept >= 2.min(history.len()),
                "min_recent not honored for len={len}: kept={kept}"
            );
        }
    }

    #[test]
    fn token_estimation_accuracy_relative_to_content_size() {
        // Longer messages should estimate more tokens
        let short = Message::user("hi");
        let long = Message::user("x".repeat(400));
        let short_tokens = estimate_message_tokens(&short);
        let long_tokens = estimate_message_tokens(&long);
        assert!(
            long_tokens > short_tokens,
            "longer message should estimate more tokens: short={short_tokens}, long={long_tokens}"
        );
        // The 400-char message should estimate roughly 100 content tokens + 4 overhead = 104
        assert!(
            long_tokens >= 100,
            "400-char message should be >= 100 tokens, got {long_tokens}"
        );
    }

    #[test]
    fn find_split_edge_single_message() {
        let history = vec![Message::user("only one")];
        // Budget is huge, min_recent=1 => keep everything
        assert_eq!(find_split_point(&history, 100_000, 1), 0);
        // Budget is tiny, min_recent=1 => still keep 1
        assert_eq!(find_split_point(&history, 1, 1), 0);
        // Budget is tiny, min_recent=0 => can drop everything
        let split = find_split_point(&history, 1, 0);
        // With min_recent=0 and tiny budget, the message may exceed budget
        assert!(split <= 1);
    }

    #[test]
    fn tool_pair_preserved_across_truncation_boundary() {
        // Tool call + multiple results should never be split
        let history = vec![
            Message::user("old stuff with lots of padding text to consume budget"),
            Message::assistant("old reply with lots of padding text to consume budget"),
            Message::user("trigger"),
            Message::assistant_with_tool_calls(
                "calling tools",
                vec![
                    ToolCall::new("c1", "search", json!({})),
                    ToolCall::new("c2", "read", json!({})),
                    ToolCall::new("c3", "write", json!({})),
                ],
            ),
            Message::tool("c1", "result1"),
            Message::tool("c2", "result2"),
            Message::tool("c3", "result3"),
            Message::user("final"),
        ];
        // Try various split points and verify tool pairs stay intact
        for candidate in 0..history.len() {
            let adjusted = adjust_split_for_tool_pairs(&history, candidate);
            if adjusted > 0 && adjusted < history.len() {
                assert_ne!(
                    history[adjusted].role,
                    Role::Tool,
                    "adjusted split at {adjusted} should not start with Tool"
                );
            }
        }
    }

    #[test]
    fn context_window_policy_threshold_triggers_truncation() {
        // Verify that when total tokens exceed (max_context - max_output), truncation occurs
        let history: Vec<Message> = (0..50)
            .map(|i| Message::user(format!("message number {i} with some padding text")))
            .collect();
        let total_tokens: usize = history.iter().map(estimate_message_tokens).sum();
        // Set budget to half the total
        let budget = total_tokens / 2;
        let split = find_split_point(&history, budget, 2);
        assert!(split > 0, "should truncate when over budget");
        // Verify kept messages fit in budget (approximately)
        let kept_tokens: usize = history[split..].iter().map(estimate_message_tokens).sum();
        assert!(
            kept_tokens <= budget || (history.len() - split) <= 2,
            "kept tokens {kept_tokens} should be <= budget {budget} (unless forced by min_recent)"
        );
    }

    #[test]
    fn truncation_with_only_system_prompt_no_history() {
        // If the split point function receives an empty history slice, it should return 0
        let split = find_split_point(&[], 1000, 5);
        assert_eq!(split, 0);
        let adjusted = adjust_split_for_tool_pairs(&[], 0);
        assert_eq!(adjusted, 0);
    }

    #[test]
    fn truncation_preserves_recent_n_messages_exactly() {
        let history: Vec<Message> = (0..10)
            .map(|i| {
                if i % 2 == 0 {
                    Message::user(format!("u{i}"))
                } else {
                    Message::assistant(format!("a{i}"))
                }
            })
            .collect();
        // Set budget to 1 token (impossibly small) but min_recent=6
        let split = find_split_point(&history, 1, 6);
        let kept = history.len() - split;
        assert!(kept >= 6, "should keep at least min_recent=6, got {kept}");
    }

    #[test]
    fn mixed_message_types_truncation() {
        // Mix of user, assistant, tool call, tool result messages
        let history = vec![
            Message::user("old user msg"),
            Message::assistant("old assistant msg"),
            Message::user("mid user msg"),
            Message::assistant_with_tool_calls(
                "mid tool call",
                vec![ToolCall::new("c1", "search", json!({}))],
            ),
            Message::tool("c1", "mid tool result"),
            Message::user("recent user"),
            Message::assistant("recent assistant"),
            Message::user("latest user"),
            Message::assistant("latest reply"),
        ];
        let split = find_split_point(&history, 40, 2);
        assert!(split > 0, "tight budget should force truncation");
        let kept = &history[split..];
        assert!(kept.len() >= 2, "must keep min_recent=2");
        // First kept message should not be orphaned Tool
        assert_ne!(
            kept[0].role,
            Role::Tool,
            "first kept message must not be orphaned tool result"
        );
    }

    #[test]
    fn very_large_message_handling() {
        // A single very large message should be handled without panic
        let huge_text = "x".repeat(100_000);
        let history = vec![
            Message::user(&huge_text),
            Message::assistant("small reply"),
            Message::user("another"),
            Message::assistant("end"),
        ];
        let huge_tokens = estimate_message_tokens(&history[0]);
        assert!(huge_tokens > 20_000, "huge message should have many tokens");
        // With a small budget, the huge message should be dropped
        let split = find_split_point(&history, 100, 2);
        assert!(split >= 1, "should drop the huge message");
    }

    #[test]
    fn adjust_split_forward_drops_orphaned_results() {
        // When the last dropped message is an assistant with tool_calls,
        // all following tool results should also be dropped
        let history = vec![
            Message::user("a"),
            Message::assistant_with_tool_calls(
                "calling",
                vec![
                    ToolCall::new("c1", "t1", json!({})),
                    ToolCall::new("c2", "t2", json!({})),
                ],
            ),
            Message::tool("c1", "r1"),
            Message::tool("c2", "r2"),
            Message::user("keep this"),
            Message::assistant("keep reply"),
        ];
        // Split at 2 means we'd keep from index 2 onward (starting with Tool).
        // adjust_split should move backward to include the assistant, or forward past tools.
        let adjusted = adjust_split_for_tool_pairs(&history, 2);
        // Should move back to 1 (include assistant) since first kept is Tool
        assert_eq!(
            adjusted, 1,
            "should move back to include assistant with tool_calls"
        );
    }

    #[test]
    fn find_split_tool_pair_not_broken() {
        let history = vec![
            Message::user("old1"),
            Message::assistant("old_reply"),
            Message::user("Do something"),
            Message::assistant_with_tool_calls(
                "Using tool",
                vec![ToolCall::new("c1", "search", json!({"q": "x"}))],
            ),
            Message::tool("c1", "found it"),
            Message::assistant("Here is the answer."),
            Message::user("Thanks"),
            Message::assistant("Welcome!"),
        ];
        let split = find_split_point(&history, 60, 2);
        if split < history.len() {
            assert_ne!(
                history[split].role,
                Role::Tool,
                "first kept message must not be an orphaned tool result"
            );
        }
    }
}