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
//! Context-budget enforcement (Tier 1 + Tier 2 compaction).
//!
//! Extracted from `tool_loop.rs` (was lines 134-330) as part of the
//! 2026-05-04 Linor-flagged refactor: `tool_loop.rs` was 4,047 lines.
//! Compaction logic is cohesive — one async method that decides between
//! the soft 65 % LLM-summarisation tier, the 90 % hard-truncate floor,
//! and the safety-net truncation when all attempts fail. Lives next to
//! the rest of `impl AgentService` in the same crate; callers still
//! invoke it as `self.enforce_context_budget(...)` exactly as before.
//!
//! Behaviour is unchanged from the pre-extraction version. The
//! exhaustive comments inside the function were preserved verbatim
//! because they document the regression history (pre-0f052250 shape,
//! cancellation race details, the failed async-spawn variant).
use super::builder::AgentService;
use super::types::{ProgressCallback, ProgressEvent};
use crate::brain::agent::context::AgentContext;
use uuid::Uuid;
impl AgentService {
/// Enforce context budget with non-blocking compaction.
///
/// Tier 1 — soft trigger at 65%: spawns an async LLM compaction task in
/// the background and returns immediately. The agent keeps processing
/// turns. Subsequent visits to this function check whether the spawned
/// task has finished and atomically swap the summary in when it has.
///
/// Tier 2 — hard floor at 90%: if context grows past 90% (because growth
/// outran compaction or compaction failed), emergency truncation cuts
/// older messages back to 80%. This path NEVER fails. It also cancels
/// any in-flight async compaction so a stale snapshot summary cannot
/// later overwrite the now-truncated context.
///
/// NOTE: 65% (~130k of 200k) is chosen because MiniMax (and likely other
/// providers) start returning `400 Prompt exceeds max length` well below
/// the documented limit, around 75-80% in practice. 65% gives enough
/// headroom to summarise without bumping into the actual ceiling.
///
/// Returns `Some(summary)` only on the visit where a previously-spawned
/// async task finished AND its summary was applied; otherwise `None`.
pub(super) async fn enforce_context_budget(
&self,
session_id: Uuid,
context: &mut AgentContext,
model_name: &str,
cancel_token: Option<&tokio_util::sync::CancellationToken>,
progress_callback: &Option<ProgressCallback>,
) -> Option<String> {
// Restored to the pre-0f052250 shape (the version that ran fine for
// months before the async-compaction refactor). Logic, in order:
//
// Tier 2 (90% hard floor): truncate to 80% first, then FALL THROUGH
// to Tier 1. Doing the truncation first means the compaction
// summarizer below sees ≤80% of the window — well within tokenizer
// headroom — so it doesn't hit `400 Prompt exceeds max length`
// and there's no failed-summarizer-then-truncate cascade.
//
// Tier 1 (65% soft trigger): up to 3 sync compact_context attempts.
// If any succeed, summary lands and the marker gets persisted by
// the caller. If still over 65% target after success, re-compact
// once more with the now-tighter budget.
//
// Safety net: only if all 3 attempts totally failed AND we're still
// above 80%, hard-truncate to 80%. This is the LAST RESORT — it
// drops messages without a summary marker, but only fires when
// the LLM compaction path is entirely unavailable.
//
// No async spawn/swap, no cancel-pending-on-90%, no per-call hard-
// truncate fallback in the error arm — those were the additions that
// produced the cascade-and-loop behaviour.
let effective_max = context.max_tokens;
let usage_pct = if effective_max > 0 {
(context.token_count as f64 / effective_max as f64) * 100.0
} else {
100.0
};
tracing::trace!(
"Context budget: {} tokens / {} max = {:.1}%",
context.token_count,
effective_max,
usage_pct,
);
// ── Tier 2: 90% hard floor — truncate to 80%, then fall through to Tier 1 compaction ──
if usage_pct >= 90.0 {
tracing::warn!(
"Context at {:.0}% ({} tokens) — hard truncating to 80%",
usage_pct,
context.token_count,
);
let target = (effective_max as f64 * 0.80) as usize;
context.hard_truncate_to(target);
context.trim_to_fit(0);
if let Some(cb) = progress_callback {
cb(session_id, ProgressEvent::TokenCount(context.token_count));
}
tracing::info!(
"Hard truncation complete: {} messages, {} tokens ({:.0}%)",
context.messages.len(),
context.token_count,
context.token_count as f64 / effective_max as f64 * 100.0,
);
let usage_pct_now = if effective_max > 0 {
(context.token_count as f64 / effective_max as f64) * 100.0
} else {
100.0
};
tracing::debug!(
"Post-truncation: {:.0}% — falling through to auto-compaction",
usage_pct_now,
);
}
// ── Tier 1: soft trigger at 65% - LLM compaction ──
let usage_pct = if effective_max > 0 {
(context.token_count as f64 / effective_max as f64) * 100.0
} else {
100.0
};
if usage_pct <= 65.0 {
// Below the compaction trigger. Try to emit the pre-compaction
// pressure warning if usage is in the 55-64% band (#909), and
// re-arm the throttle when usage has dropped below the floor.
self.maybe_emit_pressure_warning(session_id, context, usage_pct);
return None;
}
tracing::warn!(
"Context at {:.0}% (>65%) — triggering LLM compaction",
usage_pct
);
self.record_provider_feedback(
session_id,
"context_compaction",
model_name,
Some(&format!("proactive_65pct tokens={}", context.token_count)),
);
// Signal channels that the next 10-60s will produce zero
// streaming chunks so their typing-indicator pingers can keep
// firing. Carries no user-visible text — the event is consumed
// for typing/spinner refresh only.
if let Some(cb) = progress_callback {
cb(session_id, ProgressEvent::Compacting);
}
// Up to 3 attempts — transient summarizer errors (network blip,
// tokenizer-edge 400) usually self-resolve on retry.
let mut summary_result = None;
const MAX_ATTEMPTS: u32 = 3;
for attempt in 1..=MAX_ATTEMPTS {
match self
.compact_context(session_id, context, model_name, cancel_token)
.await
{
Ok(summary) => {
summary_result = Some(summary);
break;
}
Err(e) => {
tracing::error!(
"LLM compaction failed (attempt {}/{}): {}",
attempt,
MAX_ATTEMPTS,
e
);
}
}
}
// If still over the 65% target after a successful compaction, run one
// more pass with the tighter post-summary budget.
let target_tokens = (effective_max as f64 * 0.65) as usize;
if context.token_count > target_tokens && summary_result.is_some() {
tracing::warn!(
"Still at {} tokens after compaction (target {}), re-compacting",
context.token_count,
target_tokens,
);
if let Ok(summary) = self
.compact_context(session_id, context, model_name, cancel_token)
.await
{
summary_result = Some(summary);
}
}
// Last resort: every compaction attempt failed AND we're still over
// 80%. Truncate to keep the next request from going out at 200%+. No
// marker is persisted in this branch; the caller sees None back.
if summary_result.is_none() {
let safety_target = (effective_max as f64 * 0.80) as usize;
if context.token_count > safety_target {
tracing::warn!(
"Compaction exhausted, context at {} tokens (>{:.0}%) — safety truncation to 80%",
context.token_count,
usage_pct,
);
context.hard_truncate_to(safety_target);
context.trim_to_fit(0);
}
}
// Emit the token count the NEXT request will start with.
if let Some(cb) = progress_callback {
if let Some(ref summary) = summary_result {
let marker_tokens = AgentContext::estimate_tokens(summary) + 100;
let brain_tokens = self
.default_system_brain
.as_deref()
.map(AgentContext::estimate_tokens)
.unwrap_or(0);
cb(
session_id,
ProgressEvent::TokenCount(marker_tokens + brain_tokens),
);
} else {
cb(session_id, ProgressEvent::TokenCount(context.token_count));
}
}
summary_result
}
/// Pre-compaction pressure-warning gate (#909).
///
/// Called from `enforce_context_budget` when usage is at or below 65% (i.e.
/// compaction is NOT firing this turn). Decides whether to append the
/// behavioural "persist your state" nudge to the system brain:
///
/// - usage **below** the 55% floor: clear the per-session throttle flag so
/// the next entry into the band warns again. No nudge.
/// - usage **in** the 55-64% band AND not yet emitted this entry: append the
/// nudge to `system_brain`, count its tokens, set the throttle flag.
/// - usage **in** the band but already emitted: no-op (once-per-entry).
///
/// The nudge is transient: `system_brain` is rebuilt from disk every turn
/// (`live_system_brain_for_session`), so the suffix never persists to the
/// DB and cannot compound across turns.
pub(super) fn maybe_emit_pressure_warning(
&self,
session_id: Uuid,
context: &mut AgentContext,
usage_pct: f64,
) {
use super::nudge::should_emit_pressure_warning;
// Re-arm the throttle once usage drops below the band floor so the
// next climb back into the band warns again.
if usage_pct < super::nudge::PRESSURE_WARN_FLOOR {
if let Ok(mut map) = self.session_pressure_warned.write() {
map.insert(session_id, false);
}
return;
}
let already_emitted = self
.session_pressure_warned
.read()
.ok()
.and_then(|map| map.get(&session_id).copied())
.unwrap_or(false);
if let Some(warning) = should_emit_pressure_warning(usage_pct, already_emitted) {
if let Some(ref mut brain) = context.system_brain {
tracing::info!(
"Context at {:.0}% - emitting pre-compaction pressure warning",
usage_pct
);
brain.push_str(warning);
context.token_count += AgentContext::estimate_tokens(warning);
}
// Mark emitted regardless of whether a brain existed, so a
// missing brain doesn't retry every turn until the band ends.
if let Ok(mut map) = self.session_pressure_warned.write() {
map.insert(session_id, true);
}
}
// else: in-band but already emitted -> no-op (once-per-entry).
}
}