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
//! Compaction boundary discovery, plan/apply helpers, and load-time trimming.

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

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

use super::plugin::{
    CompactionAction, CompactionBoundary, CompactionFailure, CompactionInFlight, CompactionSkipped,
    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";
/// Custom event type emitted when a background compaction task starts.
pub const COMPACTION_STARTED_EVENT: &str = "compaction.started";
/// Custom event type emitted when a background compaction task is not applied.
pub const COMPACTION_SKIPPED_EVENT: &str = "compaction.skipped";

pub const COMPACTION_SKIP_REASON_MIN_SAVINGS_RATIO: &str = "min_savings_ratio";
const RATIO_PPM: f64 = 1_000_000.0;

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

/// Record a failed background compaction attempt in the state store.
pub fn record_compaction_failure(
    failure: super::plugin::CompactionFailure,
) -> super::plugin::CompactionAction {
    super::plugin::CompactionAction::RecordFailure(failure)
}

/// Record a skipped background compaction attempt in the state store.
pub fn record_compaction_skipped(skipped: CompactionSkipped) -> CompactionAction {
    CompactionAction::RecordSkipped(skipped)
}

/// 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 = compaction_keep_suffix(messages, policy);
    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,
    })
}

fn compaction_keep_suffix(messages: &[Arc<Message>], policy: &ContextWindowPolicy) -> usize {
    let configured = match policy.compaction_mode {
        ContextCompactionMode::KeepRecentRawSuffix => policy.compaction_raw_suffix_messages,
        ContextCompactionMode::CompactToSafeFrontier => policy.min_recent_messages,
    };
    let latest_user_suffix = messages
        .iter()
        .rposition(|message| {
            message.role == Role::User && message.visibility != Visibility::Internal
        })
        .map_or(0, |idx| messages.len().saturating_sub(idx));
    configured.max(latest_user_suffix).min(messages.len())
}

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

/// Estimate the token cost of the summary message that would be inserted.
pub fn summary_message_tokens(summary_text: &str) -> usize {
    estimate_message_tokens(&summary_message(summary_text))
}

/// Calculate a deterministic savings ratio in parts per million.
pub fn compaction_savings_ratio_ppm(pre_tokens: usize, post_tokens: usize) -> u32 {
    if pre_tokens == 0 || post_tokens >= pre_tokens {
        return 0;
    }
    let ratio = (pre_tokens - post_tokens) as f64 / pre_tokens as f64;
    ratio_to_ppm(ratio)
}

/// 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 prior_in_flight = store
                .read::<CompactionStateKey>()
                .and_then(|state| state.in_flight);
            let event_task_id = compaction_event_task_id(payload, inner);
            let Some(in_flight) =
                matching_in_flight(prior_in_flight, event_task_id.as_deref(), boundary_id)
            else {
                tracing::warn!(
                    task_id = event_task_id.as_deref().unwrap_or_default(),
                    boundary_message_id = boundary_id,
                    "ignoring stale compaction completion event"
                );
                return true;
            };

            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(),
                        task_id: Some(in_flight.task_id.clone()),
                        boundary_message_id: Some(boundary_id.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");
            let prior_in_flight = store
                .read::<CompactionStateKey>()
                .and_then(|state| state.in_flight);
            let boundary_message_id = inner
                .and_then(|p| p.get("boundary_message_id"))
                .and_then(|v| v.as_str())
                .filter(|value| !value.trim().is_empty())
                .map(ToOwned::to_owned)
                .or_else(|| {
                    prior_in_flight
                        .as_ref()
                        .map(|in_flight| in_flight.boundary_message_id.clone())
                })
                .unwrap_or_default();
            let event_task_id = compaction_event_task_id(payload, inner);
            let Some(in_flight) = matching_in_flight(
                prior_in_flight,
                event_task_id.as_deref(),
                &boundary_message_id,
            ) else {
                tracing::warn!(
                    task_id = event_task_id.as_deref().unwrap_or_default(),
                    boundary_message_id = boundary_message_id,
                    "ignoring stale compaction failure event"
                );
                return true;
            };
            tracing::warn!(
                error = error_text,
                "background compaction failed; clearing in-flight marker"
            );
            let mut batch = MutationBatch::new();
            batch.update::<CompactionStateKey>(CompactionAction::RecordFailure(
                CompactionFailure {
                    task_id: Some(in_flight.task_id),
                    boundary_message_id,
                    error: error_text.to_string(),
                    timestamp_ms: now_ms(),
                },
            ));
            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"
                );
            }
        }
        e if e == COMPACTION_SKIPPED_EVENT => {
            let prior_in_flight = store
                .read::<CompactionStateKey>()
                .and_then(|state| state.in_flight);
            let boundary_message_id = inner
                .and_then(|p| p.get("boundary_message_id"))
                .and_then(|v| v.as_str())
                .filter(|value| !value.trim().is_empty())
                .map(ToOwned::to_owned)
                .or_else(|| {
                    prior_in_flight
                        .as_ref()
                        .map(|in_flight| in_flight.boundary_message_id.clone())
                })
                .unwrap_or_default();
            let reason = inner
                .and_then(|p| p.get("reason"))
                .and_then(|v| v.as_str())
                .filter(|value| !value.trim().is_empty())
                .unwrap_or("unknown")
                .to_string();
            let pre_tokens = inner
                .and_then(|p| p.get("pre_tokens"))
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as usize;
            let post_tokens = inner
                .and_then(|p| p.get("post_tokens"))
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as usize;
            let savings_ratio_ppm = inner
                .and_then(|p| p.get("savings_ratio_ppm"))
                .and_then(|v| v.as_u64())
                .unwrap_or_else(|| {
                    inner
                        .and_then(|p| p.get("savings_ratio"))
                        .and_then(|v| v.as_f64())
                        .map(ratio_to_ppm)
                        .unwrap_or_else(|| compaction_savings_ratio_ppm(pre_tokens, post_tokens))
                        .into()
                }) as u32;
            let min_savings_ratio_ppm = inner
                .and_then(|p| p.get("min_savings_ratio_ppm"))
                .and_then(|v| v.as_u64())
                .unwrap_or_else(|| {
                    inner
                        .and_then(|p| p.get("min_savings_ratio"))
                        .and_then(|v| v.as_f64())
                        .map(ratio_to_ppm)
                        .unwrap_or(0)
                        .into()
                }) as u32;
            let event_task_id = compaction_event_task_id(payload, inner);
            let Some(in_flight) = matching_in_flight(
                prior_in_flight,
                event_task_id.as_deref(),
                &boundary_message_id,
            ) else {
                tracing::warn!(
                    task_id = event_task_id.as_deref().unwrap_or_default(),
                    boundary_message_id = boundary_message_id,
                    "ignoring stale compaction skipped event"
                );
                return true;
            };
            let mut batch = MutationBatch::new();
            batch.update::<CompactionStateKey>(CompactionAction::RecordSkipped(
                CompactionSkipped {
                    task_id: Some(in_flight.task_id),
                    boundary_message_id,
                    reason,
                    pre_tokens,
                    post_tokens,
                    savings_ratio_ppm,
                    min_savings_ratio_ppm,
                    timestamp_ms: now_ms(),
                },
            ));
            batch.update::<CompactionStateKey>(CompactionAction::ClearInFlight);
            if let Err(error) = store.commit(batch) {
                tracing::warn!(
                    error = %error,
                    "failed to clear in-flight marker after skipped compaction"
                );
            }
        }
        e if e == COMPACTION_STARTED_EVENT => {}
        _ => {}
    }
    true
}

fn compaction_event_task_id(
    payload: &serde_json::Value,
    inner: Option<&serde_json::Value>,
) -> Option<String> {
    payload
        .get("task_id")
        .and_then(|v| v.as_str())
        .or_else(|| {
            inner
                .and_then(|p| p.get("task_id"))
                .and_then(|v| v.as_str())
        })
        .filter(|value| !value.trim().is_empty())
        .map(ToOwned::to_owned)
}

fn matching_in_flight(
    prior_in_flight: Option<CompactionInFlight>,
    event_task_id: Option<&str>,
    boundary_message_id: &str,
) -> Option<CompactionInFlight> {
    let in_flight = prior_in_flight?;
    if event_task_id != Some(in_flight.task_id.as_str()) {
        return None;
    }
    if !boundary_message_id.is_empty()
        && boundary_message_id != in_flight.boundary_message_id.as_str()
    {
        return None;
    }
    Some(in_flight)
}

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
                || *t == COMPACTION_STARTED_EVENT
                || *t == COMPACTION_SKIPPED_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 = summary_message(summary_text);
    let post_tokens = estimate_message_tokens(&summary_message);
    messages.insert(0, summary_message);
    Some(AppliedCompaction {
        boundary_index: idx,
        pre_tokens,
        post_tokens,
    })
}

fn summary_message(summary_text: &str) -> Arc<Message> {
    Arc::new(Message::internal_system(format!(
        "<conversation-summary>\n{summary_text}\n</conversation-summary>"
    )))
}

fn ratio_to_ppm(ratio: f64) -> u32 {
    if !ratio.is_finite() {
        return 0;
    }
    ratio.clamp(0.0, 1.0).mul_add(RATIO_PPM, 0.0).round() as u32
}

#[cfg(test)]
mod tests;