| 1 | //! Context compaction (`/compact`), mirroring pi's coding agent. |
| 2 | //! |
| 3 | //! Older turns are summarized by the model into a structured context |
| 4 | //! checkpoint (`Goal`, `Progress`, `Next Steps`, …) that replaces them in the |
| 5 | //! context sent to the model, while the most recent ~`KEEP_RECENT_TOKENS` stay |
| 6 | //! verbatim. The summary is stored as a `compaction` message row (with |
| 7 | //! `cut_msg_id` pointing at the first kept message) so the agent loop can |
| 8 | //! rebuild the context without the summarized turns. |
| 9 | |
| 10 | use std::collections:: |
| 11 | use std::sync:: |
| 12 | |
| 13 | use anyhow::{Result |
| 14 | use rusqlite::Connection |
| 15 | use serde_json::Value |
| 16 | |
| 17 | use crate::db::{self |
| 18 | use crate::llm::{self |
| 19 | |
| 20 | /// Recent context (estimated tokens) that compaction always keeps verbatim. |
| 21 | const KEEP_RECENT_TOKENS: usize |
| 22 | /// Reserved for the summary + provider overhead when auto-compacting. Manual |
| 23 | /// compaction uses it to size the summarization request. |
| 24 | const RESERVE_TOKENS: usize |
| 25 | /// Conservative chars-per-token heuristic (pi uses chars/4). |
| 26 | const CHARS_PER_TOKEN: usize |
| 27 | /// Token weight of a pasted image in a user message. |
| 28 | const ESTIMATED_IMAGE_CHARS: usize |
| 29 | /// Tool results are truncated to this many chars when serialized for |
| 30 | /// summarization — the full content isn't needed for a summary. |
| 31 | const TOOL_RESULT_MAX_CHARS: usize |
| 32 | |
| 33 | /// Wrap the stored summary in these tags when it is injected back into the |
| 34 | /// model context (pi's `COMPACTION_SUMMARY_PREFIX` / `_SUFFIX`). |
| 35 | pub const COMPACTION_SUMMARY_PREFIX: |
| 36 | pub const COMPACTION_SUMMARY_SUFFIX: |
| 37 | |
| 38 | const SUMMARIZATION_SYSTEM_PROMPT: &str |
| 39 | |
| 40 | Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary."; |
| 41 | |
| 42 | const SUMMARIZATION_PROMPT: &str |
| 43 | |
| 44 | Use this EXACT format: |
| 45 | |
| 46 | ## Goal |
| 47 | [What is the user trying to accomplish? Can be multiple items if the session covers different tasks.] |
| 48 | |
| 49 | ## Constraints & Preferences |
| 50 | - [Any constraints, preferences, or requirements mentioned by user] |
| 51 | - [Or \"(none)\" if none were mentioned] |
| 52 | |
| 53 | ## Progress |
| 54 | ### Done |
| 55 | - [x] [Completed tasks/changes] |
| 56 | |
| 57 | ### In Progress |
| 58 | - [ ] [Current work] |
| 59 | |
| 60 | ### Blocked |
| 61 | - [Issues preventing progress, if any] |
| 62 | |
| 63 | ## Key Decisions |
| 64 | - **[Decision]**: [Brief rationale] |
| 65 | |
| 66 | ## Next Steps |
| 67 | 1. [Ordered list of what should happen next] |
| 68 | |
| 69 | ## Critical Context |
| 70 | - [Any data, examples, or references needed to continue] |
| 71 | - [Or \"(none)\" if not applicable] |
| 72 | |
| 73 | Keep each section concise. Preserve exact file paths, function names, and error messages."; |
| 74 | |
| 75 | const UPDATE_SUMMARIZATION_PROMPT: &str |
| 76 | |
| 77 | Update the existing structured summary with new information. RULES: |
| 78 | - PRESERVE all existing information from the previous summary |
| 79 | - ADD new progress, decisions, and context from the new messages |
| 80 | - UPDATE the Progress section: move items from \"In Progress\" to \"Done |
| 81 | - UPDATE \"Next Steps\" based on what was accomplished |
| 82 | - PRESERVE exact file paths, function names, and error messages |
| 83 | - If something is no longer relevant, you may remove it |
| 84 | |
| 85 | Use this EXACT format: |
| 86 | |
| 87 | ## Goal |
| 88 | [Preserve existing goals, add new ones if the task expanded] |
| 89 | |
| 90 | ## Constraints & Preferences |
| 91 | - [Preserve existing, add new ones discovered] |
| 92 | |
| 93 | ## Progress |
| 94 | ### Done |
| 95 | - [x] [Include previously done items AND newly completed items] |
| 96 | |
| 97 | ### In Progress |
| 98 | - [ ] [Current work - update based on progress] |
| 99 | |
| 100 | ### Blocked |
| 101 | - [Current blockers - remove if resolved] |
| 102 | |
| 103 | ## Key Decisions |
| 104 | - **[Decision]**: [Brief rationale] (preserve all previous, add new) |
| 105 | |
| 106 | ## Next Steps |
| 107 | 1. [Update based on current state] |
| 108 | |
| 109 | ## Critical Context |
| 110 | - [Preserve important context, add new if needed] |
| 111 | |
| 112 | Keep each section concise. Preserve exact file paths, function names, and error messages."; |
| 113 | |
| 114 | const TURN_PREFIX_SUMMARIZATION_PROMPT: &str |
| 115 | "This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) is retained. |
| 116 | |
| 117 | Summarize the prefix to provide context for the retained suffix: |
| 118 | |
| 119 | ## Original Request |
| 120 | [What did the user ask for in this turn?] |
| 121 | |
| 122 | ## Early Progress |
| 123 | - [Key decisions and work done in the prefix] |
| 124 | |
| 125 | ## Context for Suffix |
| 126 | - [Information needed to understand the retained recent work] |
| 127 | |
| 128 | Be concise. Focus on what's needed to understand the kept suffix."; |
| 129 | |
| 130 | /// Result of a manual compaction run. |
| 131 | pub struct CompactionResult |
| 132 | /// Estimated total context tokens before compaction. |
| 133 | pub tokens_before: usize |
| 134 | /// Number of message rows folded into the summary. |
| 135 | pub dropped: usize |
| 136 | /// Number of message rows kept verbatim. |
| 137 | pub kept: usize |
| 138 | } |
| 139 | |
| 140 | /// Estimate how many context tokens a stored message contributes, using the |
| 141 | /// same chars/4 heuristic pi uses (deliberately conservative / an overestimate). |
| 142 | pub fn estimate_tokens(m |
| 143 | let mut chars |
| 144 | match m.role.as_str |
| 145 | "user" => { |
| 146 | if let Some |
| 147 | && let Ok |
| 148 | { |
| 149 | chars += parts. |
| 150 | } |
| 151 | } |
| 152 | "assistant" => { |
| 153 | chars += m |
| 154 | .tool_calls |
| 155 | .as_deref() |
| 156 | .map(|s| |
| 157 | .unwrap_or(0); |
| 158 | } |
| 159 | _ => {} |
| 160 | } |
| 161 | chars.div_ceil(CHARS_PER_TOKEN |
| 162 | } |
| 163 | |
| 164 | /// Estimate how many context tokens a resolved, ready-to-send message |
| 165 | /// contributes (same chars/4 heuristic as `estimate_tokens`). Counts the |
| 166 | /// content plus any tool call JSON. Used for the threshold check so the |
| 167 | /// estimate reflects what actually goes to the provider — folded history that |
| 168 | /// `resolve_compaction` drops must not keep pushing the estimate over the |
| 169 | /// threshold (that made every round after a compaction re-compact). |
| 170 | pub fn estimate_llm_tokens(m |
| 171 | let mut chars |
| 172 | for t in & |
| 173 | chars += t. |
| 174 | chars += t. |
| 175 | chars += t. |
| 176 | } |
| 177 | if let Some |
| 178 | chars += name. |
| 179 | } |
| 180 | chars.div_ceil(CHARS_PER_TOKEN |
| 181 | } |
| 182 | |
| 183 | /// Total estimated context tokens of a resolved message list (the actual |
| 184 | /// request payload, per pi's `estimateContextTokens`). |
| 185 | pub fn estimate_llm_context_tokens(messages |
| 186 | messages.iter().map |
| 187 | } |
| 188 | |
| 189 | /// Messages that may end up on the summary/keep boundary. Tool results are |
| 190 | /// never cut points — they must follow their assistant tool call. |
| 191 | fn is_cut_point(m: |
| 192 | matches!(m.role |
| 193 | } |
| 194 | |
| 195 | /// Estimate the total context tokens of the stored conversation (pi's |
| 196 | /// `estimateContextTokens`), using the same chars/4 heuristic as |
| 197 | /// `estimate_tokens`. Production compaction estimates the resolved LLM payload |
| 198 | /// (`estimate_llm_context_tokens`); this raw-row helper is kept for tests that |
| 199 | /// assert the folded-history regression. |
| 200 | #[cfg(test)] |
| 201 | pub fn estimate_context_tokens(messages |
| 202 | messages.iter().map |
| 203 | } |
| 204 | |
| 205 | /// Whether the context has grown within `RESERVE_TOKENS` of the provider's |
| 206 | /// context window and should be proactively compacted before the next request |
| 207 | /// (pi's `shouldCompact`: `contextTokens > contextWindow - reserveTokens`). |
| 208 | pub fn should_compact(context_tokens |
| 209 | context_tokens > context_window. |
| 210 | } |
| 211 | |
| 212 | /// Messages that start a turn (cutting before these is always clean). |
| 213 | fn is_turn_start(m: |
| 214 | matches!(m.role |
| 215 | } |
| 216 | |
| 217 | struct CutPoint { |
| 218 | /// Index of the first entry to keep. |
| 219 | first_kept_index: usize |
| 220 | /// Index of the user message that starts a split turn, if cutting mid-turn. |
| 221 | turn_start_index: Option |
| 222 | /// Whether the cut lands mid-turn (cut point is not a user message). |
| 223 | is_split_turn: bool |
| 224 | } |
| 225 | |
| 226 | /// Find the cut point that keeps approximately `KEEP_RECENT_TOKENS`: walk |
| 227 | /// backwards from the newest message accumulating estimated sizes, then cut at |
| 228 | /// the closest valid cut point. Only considers messages at/after `start`. |
| 229 | fn find_cut_point(messages: |
| 230 | let end = |
| 231 | let cut_points: Vec |
| 232 | .filter(|&i |
| 233 | .collect(); |
| 234 | if cut_points.is_empty() |
| 235 | return CutPoint { |
| 236 | first_kept_index: start, |
| 237 | turn_start_index: None |
| 238 | is_split_turn: false, |
| 239 | }; |
| 240 | } |
| 241 | |
| 242 | let mut accumulated |
| 243 | let mut cut_index |
| 244 | for i in ( |
| 245 | let tokens = |
| 246 | if tokens == 0 |
| 247 | continue; |
| 248 | } |
| 249 | accumulated += tokens; |
| 250 | if accumulated >= KEEP_RECENT_TOKENS |
| 251 | for &c in |
| 252 | if c >= i |
| 253 | cut_index = c; |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | let starts_turn = |
| 262 | let turn_start_index = |
| 263 | None |
| 264 | } else { |
| 265 | (start..=cut_index) |
| 266 | .rev() |
| 267 | .find(|&i |
| 268 | }; |
| 269 | |
| 270 | CutPoint { |
| 271 | first_kept_index: cut_index, |
| 272 | turn_start_index, |
| 273 | is_split_turn: turn_start_index.is_some |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /// Serialize messages to plain text so the summarizer treats them as a |
| 278 | /// document, not a conversation to continue (pi's `serializeConversation`). |
| 279 | fn serialize_conversation(messages: |
| 280 | let mut parts |
| 281 | for m in messages |
| 282 | match m.role.as_str |
| 283 | "user" => { |
| 284 | if let Some |
| 285 | && !c.is_empty |
| 286 | { |
| 287 | parts.push(format! |
| 288 | } |
| 289 | } |
| 290 | "assistant" => { |
| 291 | let mut calls |
| 292 | if let Some |
| 293 | && let Ok |
| 294 | { |
| 295 | for t in & |
| 296 | let name = |
| 297 | let args = |
| 298 | calls.push(format! |
| 299 | } |
| 300 | } |
| 301 | if let Some |
| 302 | && !c.is_empty |
| 303 | { |
| 304 | parts.push(format! |
| 305 | } |
| 306 | if !calls.is_empty |
| 307 | parts.push(format! |
| 308 | } |
| 309 | } |
| 310 | "tool" => { |
| 311 | if let Some |
| 312 | && !c.is_empty |
| 313 | { |
| 314 | parts.push(format! |
| 315 | } |
| 316 | } |
| 317 | _ => {} |
| 318 | } |
| 319 | } |
| 320 | parts.join("\ |
| 321 | } |
| 322 | |
| 323 | fn truncate_for_summary(text: |
| 324 | let max_chars = |
| 325 | if text.chars() |
| 326 | return text.to_string(); |
| 327 | } |
| 328 | let kept: String |
| 329 | let truncated = |
| 330 | format!("{kept} |
| 331 | } |
| 332 | |
| 333 | /// Extract read/write/edit file paths from tool calls (pi's file-op tracking). |
| 334 | fn extract_file_ops(messages: |
| 335 | let mut read |
| 336 | let mut written |
| 337 | let mut edited |
| 338 | |
| 339 | for m in messages |
| 340 | if m.role != |
| 341 | continue; |
| 342 | } |
| 343 | let Some( |
| 344 | continue; |
| 345 | }; |
| 346 | let Ok( |
| 347 | continue; |
| 348 | }; |
| 349 | for t in & |
| 350 | let Some( |
| 351 | continue; |
| 352 | }; |
| 353 | match t["name" |
| 354 | Some("read_file") |
| 355 | read.insert(path |
| 356 | } |
| 357 | Some("write_file") |
| 358 | written.insert(path |
| 359 | } |
| 360 | Some("edit_file") |
| 361 | edited.insert(path |
| 362 | } |
| 363 | _ => {} |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | let modified: BTreeSet |
| 369 | let read_only: Vec |
| 370 | (read_only, modified. |
| 371 | } |
| 372 | |
| 373 | /// Append `<read-files>` / `<modified-files>` sections to the summary (pi's |
| 374 | /// `formatFileOperations`). |
| 375 | fn format_file_operations(read_files: |
| 376 | let mut sections |
| 377 | if !read_files.is_empty |
| 378 | sections.push(format! |
| 379 | "<read-files>\n{}\n</read-files>" |
| 380 | read_files.join("\ |
| 381 | )); |
| 382 | } |
| 383 | if !modified_files.is_empty |
| 384 | sections.push(format! |
| 385 | "<modified-files>\n{}\n</modified-files>" |
| 386 | modified_files.join("\ |
| 387 | )); |
| 388 | } |
| 389 | if sections.is_empty() |
| 390 | return String::new |
| 391 | } |
| 392 | format!("\n\ |
| 393 | } |
| 394 | |
| 395 | /// One LLM call that turns `messages` into the structured summary. If a |
| 396 | /// previous summary exists, the update prompt merges it in. |
| 397 | fn summarize( |
| 398 | messages: &[StoredMessage |
| 399 | previous_summary: Option |
| 400 | cfg: &ProviderConfig |
| 401 | model: &str |
| 402 | custom_instructions: Option |
| 403 | cancel: &AtomicBool |
| 404 | ) -> Result<String |
| 405 | let conversation_text = |
| 406 | let mut prompt_text |
| 407 | if let Some |
| 408 | prompt_text.push_str(&format! |
| 409 | "<previous-summary>\n{prev}\n |
| 410 | )); |
| 411 | } |
| 412 | prompt_text.push_str(if |
| 413 | UPDATE_SUMMARIZATION_PROMPT |
| 414 | } else { |
| 415 | SUMMARIZATION_PROMPT |
| 416 | }); |
| 417 | if let Some |
| 418 | && !ci.trim |
| 419 | { |
| 420 | prompt_text.push_str(&format! |
| 421 | } |
| 422 | |
| 423 | let msg = |
| 424 | role: "user". |
| 425 | content: Some |
| 426 | tool_call_id: None |
| 427 | name: None |
| 428 | tool_calls: vec![], |
| 429 | reasoning: None |
| 430 | images: vec![], |
| 431 | }; |
| 432 | // Summaries are standalone requests: cap output at a fraction of the |
| 433 | // reserved budget (pi: min(0.8 * reserveTokens, model max)), but never less |
| 434 | // than the provider's configured max_tokens. |
| 435 | let mut sum_cfg |
| 436 | sum_cfg.max_tokens = sum_cfg |
| 437 | let mut retry_sink |
| 438 | eprintln!( |
| 439 | " ⟳ summarization retry ({attempt}/{max} |
| 440 | wait_ms / 1000 |
| 441 | ); |
| 442 | }; |
| 443 | let resp = |
| 444 | &sum_cfg, |
| 445 | model, |
| 446 | SUMMARIZATION_SYSTEM_PROMPT, |
| 447 | &[msg], |
| 448 | &[], |
| 449 | None, |
| 450 | Some(&mut |
| 451 | cancel, |
| 452 | )?; |
| 453 | Ok(resp. |
| 454 | } |
| 455 | |
| 456 | /// Summarize the prefix of a turn that was too large to keep whole. |
| 457 | fn summarize_turn_prefix( |
| 458 | messages: &[StoredMessage |
| 459 | cfg: &ProviderConfig |
| 460 | model: &str |
| 461 | cancel: &AtomicBool |
| 462 | ) -> Result<String |
| 463 | let conversation_text = |
| 464 | let prompt_text = |
| 465 | "<conversation>\n{conversation_text}\n |
| 466 | ); |
| 467 | let msg = |
| 468 | role: "user". |
| 469 | content: Some |
| 470 | tool_call_id: None |
| 471 | name: None |
| 472 | tool_calls: vec![], |
| 473 | reasoning: None |
| 474 | images: vec![], |
| 475 | }; |
| 476 | let mut sum_cfg |
| 477 | // Smaller budget for the turn prefix (pi: 0.5 * reserveTokens). |
| 478 | sum_cfg.max_tokens = sum_cfg |
| 479 | let mut retry_sink |
| 480 | eprintln!( |
| 481 | " ⟳ turn-prefix summarization retry ({attempt}/{max} |
| 482 | wait_ms / 1000 |
| 483 | ); |
| 484 | }; |
| 485 | let resp = |
| 486 | &sum_cfg, |
| 487 | model, |
| 488 | SUMMARIZATION_SYSTEM_PROMPT, |
| 489 | &[msg], |
| 490 | &[], |
| 491 | None, |
| 492 | Some(&mut |
| 493 | cancel, |
| 494 | )?; |
| 495 | Ok(resp. |
| 496 | } |
| 497 | |
| 498 | /// Whether an in-memory message list (sub-agent history) is a clean cut point. |
| 499 | fn is_llm_cut_point(m: |
| 500 | matches!(m.role |
| 501 | } |
| 502 | |
| 503 | fn is_llm_turn_start(m: |
| 504 | m.role == "user |
| 505 | } |
| 506 | |
| 507 | /// Same cut-point walk as session compaction, but over the sub-agent's live |
| 508 | /// `LlmMessage` list (no DB rows, no compaction markers). |
| 509 | fn find_llm_cut_point(messages: |
| 510 | let end = |
| 511 | let cut_points: Vec |
| 512 | .filter(|&i |
| 513 | .collect(); |
| 514 | if cut_points.is_empty() |
| 515 | return CutPoint { |
| 516 | first_kept_index: start, |
| 517 | turn_start_index: None |
| 518 | is_split_turn: false, |
| 519 | }; |
| 520 | } |
| 521 | |
| 522 | let mut accumulated |
| 523 | let mut cut_index |
| 524 | for i in ( |
| 525 | let tokens = |
| 526 | if tokens == 0 |
| 527 | continue; |
| 528 | } |
| 529 | accumulated += tokens; |
| 530 | if accumulated >= KEEP_RECENT_TOKENS |
| 531 | for &c in |
| 532 | if c >= i |
| 533 | cut_index = c; |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | let starts_turn = |
| 542 | let turn_start_index = |
| 543 | None |
| 544 | } else { |
| 545 | (start..=cut_index) |
| 546 | .rev() |
| 547 | .find(|&i |
| 548 | }; |
| 549 | |
| 550 | CutPoint { |
| 551 | first_kept_index: cut_index, |
| 552 | turn_start_index, |
| 553 | is_split_turn: turn_start_index.is_some |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /// Serialize an in-memory agent transcript for the summarizer (same shape as |
| 558 | /// `serialize_conversation`, without needing stored DB rows). |
| 559 | fn serialize_llm_conversation(messages: |
| 560 | let mut parts |
| 561 | for m in messages |
| 562 | match m.role.as_str |
| 563 | "user" => { |
| 564 | if let Some |
| 565 | && !c.is_empty |
| 566 | { |
| 567 | // Don't re-summarize an earlier compaction envelope as a |
| 568 | // user turn — pass the inner summary text through. |
| 569 | let body = |
| 570 | .strip_prefix(COMPACTION_SUMMARY_PREFIX) |
| 571 | .and_then(|s| |
| 572 | .unwrap_or(c); |
| 573 | parts.push(format! |
| 574 | } |
| 575 | } |
| 576 | "assistant" => { |
| 577 | if let Some |
| 578 | && !c.is_empty |
| 579 | { |
| 580 | parts.push(format! |
| 581 | } |
| 582 | if !m.tool_calls. |
| 583 | let calls: Vec |
| 584 | .tool_calls |
| 585 | .iter() |
| 586 | .map(|t| |
| 587 | .collect(); |
| 588 | parts.push(format! |
| 589 | } |
| 590 | } |
| 591 | "tool" => { |
| 592 | if let Some |
| 593 | && !c.is_empty |
| 594 | { |
| 595 | parts.push(format! |
| 596 | } |
| 597 | } |
| 598 | _ => {} |
| 599 | } |
| 600 | } |
| 601 | parts.join("\ |
| 602 | } |
| 603 | |
| 604 | fn extract_llm_file_ops(messages: |
| 605 | let mut read |
| 606 | let mut written |
| 607 | let mut edited |
| 608 | for m in messages |
| 609 | if m.role != |
| 610 | continue; |
| 611 | } |
| 612 | for t in & |
| 613 | let Some( |
| 614 | continue; |
| 615 | }; |
| 616 | let path = |
| 617 | match t.name.as_str |
| 618 | "read_file" => { |
| 619 | read.insert(path |
| 620 | } |
| 621 | "write_file" => { |
| 622 | written.insert(path |
| 623 | } |
| 624 | "edit_file" => { |
| 625 | edited.insert(path |
| 626 | } |
| 627 | _ => {} |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | let modified: BTreeSet |
| 632 | let read_only: Vec |
| 633 | (read_only, modified. |
| 634 | } |
| 635 | |
| 636 | fn previous_in_memory_summary(messages: |
| 637 | let Some( |
| 638 | return (None, |
| 639 | }; |
| 640 | if first.role != |
| 641 | return (None, |
| 642 | } |
| 643 | let Some( |
| 644 | return (None, |
| 645 | }; |
| 646 | let Some( |
| 647 | .strip_prefix(COMPACTION_SUMMARY_PREFIX) |
| 648 | .and_then(|s| |
| 649 | else { |
| 650 | return (None, |
| 651 | }; |
| 652 | (Some(inner |
| 653 | } |
| 654 | |
| 655 | /// Compact a sub-agent's in-memory history the same way the main agent |
| 656 | /// compacts a session: summarize everything before a cut that keeps |
| 657 | /// ~`KEEP_RECENT_TOKENS`, then return `[summary user msg] + kept tail`. |
| 658 | /// |
| 659 | /// No database write — sub-agents never persist their intermediate turns. |
| 660 | /// Callers should skip the next threshold check (like the main loop skips |
| 661 | /// when the newest row is already a compaction) so a compact can't loop. |
| 662 | pub fn compact_messages( |
| 663 | messages: Vec |
| 664 | cfg: &ProviderConfig |
| 665 | model: &str |
| 666 | cancel: &AtomicBool |
| 667 | ) -> Result<( |
| 668 | if messages.is_empty() |
| 669 | bail!("Nothing to compact (sub-agent history is empty)"); |
| 670 | } |
| 671 | let (previous_summary, |
| 672 | // Just-compacted history: first message is the prior summary and the |
| 673 | // remainder is still under the keep budget — refuse so we don't thrash. |
| 674 | if boundary_start > 0 |
| 675 | && !should_compact( |
| 676 | estimate_llm_context_tokens(&messages), |
| 677 | cfg.context_window as usize |
| 678 | ) |
| 679 | { |
| 680 | bail!("Already compacted — continue before compacting again"); |
| 681 | } |
| 682 | |
| 683 | let tokens_before = |
| 684 | let cut = |
| 685 | let history_end = |
| 686 | if history_end <= boundary_start |
| 687 | bail!("Nothing to compact (sub-agent history is small enough)"); |
| 688 | } |
| 689 | let to_summarize = |
| 690 | let turn_prefix: & |
| 691 | Some(ts) |
| 692 | _ => &[], |
| 693 | }; |
| 694 | if to_summarize.is_empty() |
| 695 | bail!("Nothing to compact (sub-agent history is small enough)"); |
| 696 | } |
| 697 | |
| 698 | let conversation_text = |
| 699 | let mut text |
| 700 | if !turn_prefix.is_empty |
| 701 | if !text.is_empty |
| 702 | text.push_str("\ |
| 703 | } |
| 704 | text.push_str(&serialize_llm_conversation |
| 705 | } |
| 706 | text |
| 707 | }; |
| 708 | let mut prompt_text |
| 709 | if let Some |
| 710 | prompt_text.push_str(&format! |
| 711 | "<previous-summary>\n{prev}\n |
| 712 | )); |
| 713 | prompt_text.push_str(UPDATE_SUMMARIZATION_PROMPT |
| 714 | } else { |
| 715 | prompt_text.push_str(SUMMARIZATION_PROMPT |
| 716 | } |
| 717 | let msg = |
| 718 | role: "user". |
| 719 | content: Some |
| 720 | tool_call_id: None |
| 721 | name: None |
| 722 | tool_calls: vec![], |
| 723 | reasoning: None |
| 724 | images: vec![], |
| 725 | }; |
| 726 | let mut sum_cfg |
| 727 | sum_cfg.max_tokens = sum_cfg |
| 728 | let mut retry_sink |
| 729 | eprintln!( |
| 730 | " ⟳ sub-agent summarization retry ({attempt}/{max} |
| 731 | wait_ms / 1000 |
| 732 | ); |
| 733 | }; |
| 734 | let resp = |
| 735 | &sum_cfg, |
| 736 | model, |
| 737 | SUMMARIZATION_SYSTEM_PROMPT, |
| 738 | &[msg], |
| 739 | &[], |
| 740 | None, |
| 741 | Some(&mut |
| 742 | cancel, |
| 743 | )?; |
| 744 | let mut summary |
| 745 | let mut op_sources |
| 746 | op_sources.extend_from_slice(turn_prefix |
| 747 | let (read_files, |
| 748 | summary.push_str(&format_file_operations |
| 749 | |
| 750 | let dropped = |
| 751 | let kept_tail = |
| 752 | let kept = |
| 753 | let mut out |
| 754 | out.push(LlmMessage |
| 755 | role: "user". |
| 756 | content: Some |
| 757 | "{COMPACTION_SUMMARY_PREFIX}{summary}{COMPACTION_SUMMARY_SUFFIX} |
| 758 | )), |
| 759 | tool_call_id: None |
| 760 | name: None |
| 761 | tool_calls: vec![], |
| 762 | reasoning: None |
| 763 | images: vec![], |
| 764 | }); |
| 765 | out.extend(kept_tail |
| 766 | Ok(( |
| 767 | out, |
| 768 | CompactionResult { |
| 769 | tokens_before, |
| 770 | dropped, |
| 771 | kept, |
| 772 | }, |
| 773 | )) |
| 774 | } |
| 775 | |
| 776 | /// Manually compact the session: summarize everything before a cut point that |
| 777 | /// keeps ~`KEEP_RECENT_TOKENS`, then record a `compaction` message row that |
| 778 | /// replaces the summarized turns in future context. Mirrors pi's `/compact`. |
| 779 | pub fn run( |
| 780 | conn: &Connection |
| 781 | session_id: i64 |
| 782 | cfg: &ProviderConfig |
| 783 | model: &str |
| 784 | custom_instructions: Option |
| 785 | cancel: &AtomicBool |
| 786 | ) -> Result<CompactionResult |
| 787 | // Session-global thinking effort (see `/effort`) applies to the summarizer |
| 788 | // call too; same rule as agent runs. |
| 789 | let mut cfg |
| 790 | if let Some |
| 791 | cfg.effort = Some |
| 792 | } |
| 793 | let messages = |
| 794 | if messages.is_empty() |
| 795 | bail!("Nothing to compact (session is empty)"); |
| 796 | } |
| 797 | if messages.last() |
| 798 | bail!("Already compacted — continue chatting before compacting again"); |
| 799 | } |
| 800 | |
| 801 | // Find the previous compaction (if any): its summary becomes the base for |
| 802 | // the update prompt, and its cut marks the earliest entry worth re-reading. |
| 803 | let mut previous_summary |
| 804 | let mut boundary_start |
| 805 | for (i, m |
| 806 | if m.role == |
| 807 | previous_summary = m. |
| 808 | boundary_start = messages |
| 809 | .iter() |
| 810 | .position(|x| |
| 811 | .unwrap_or(i + |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | let tokens_before = |
| 816 | |
| 817 | let cut = |
| 818 | let history_end = |
| 819 | let to_summarize = |
| 820 | let turn_prefix: & |
| 821 | Some(ts) |
| 822 | None => &[], |
| 823 | }; |
| 824 | |
| 825 | if to_summarize.is_empty() |
| 826 | bail!("Nothing to compact (session is small enough)"); |
| 827 | } |
| 828 | |
| 829 | let summary = |
| 830 | let history_text = |
| 831 | "No prior history.".to_string() |
| 832 | } else { |
| 833 | summarize( |
| 834 | to_summarize, |
| 835 | previous_summary.as_deref(), |
| 836 | &cfg, |
| 837 | model, |
| 838 | custom_instructions, |
| 839 | cancel, |
| 840 | )? |
| 841 | }; |
| 842 | let prefix_text = |
| 843 | format!("{history_text} |
| 844 | } else { |
| 845 | summarize( |
| 846 | to_summarize, |
| 847 | previous_summary.as_deref(), |
| 848 | &cfg, |
| 849 | model, |
| 850 | custom_instructions, |
| 851 | cancel, |
| 852 | )? |
| 853 | }; |
| 854 | |
| 855 | // File operation tracking survives the summarization (pi appends these to |
| 856 | // every compaction entry so future summaries can stay path-accurate). |
| 857 | let mut op_sources |
| 858 | op_sources.extend(turn_prefix |
| 859 | let (read_files, |
| 860 | let summary = |
| 861 | "{summary}{}", |
| 862 | format_file_operations(&read_files, |
| 863 | ); |
| 864 | |
| 865 | let first_kept_id = |
| 866 | db::add_compaction(conn |
| 867 | db::touch_session(conn |
| 868 | |
| 869 | let dropped = |
| 870 | Ok(CompactionResult |
| 871 | tokens_before, |
| 872 | dropped, |
| 873 | kept: messages.len |
| 874 | }) |
| 875 | } |
| 876 | |
| 877 | #[cfg(test)] |
| 878 | mod tests { |
| 879 | use super::*; |
| 880 | |
| 881 | fn msg(id: |
| 882 | StoredMessage { |
| 883 | id, |
| 884 | session_id: 1, |
| 885 | role: role.into |
| 886 | content: Some |
| 887 | tool_call_id: None |
| 888 | name: None |
| 889 | tool_calls: None |
| 890 | reasoning: None |
| 891 | images: None |
| 892 | usage: None |
| 893 | cut_msg_id: None |
| 894 | created_at: id, |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | #[test] |
| 899 | fn estimate_tokens_is_chars_over_four() { |
| 900 | assert_eq!(estimate_tokens( |
| 901 | assert_eq!(estimate_tokens( |
| 902 | assert_eq!(estimate_tokens( |
| 903 | } |
| 904 | |
| 905 | #[test] |
| 906 | fn threshold_triggers_within_reserve_of_window() { |
| 907 | // pi's shouldCompact: contextTokens > contextWindow - reserveTokens. |
| 908 | assert!(should_compact( |
| 909 | assert!(should_compact( |
| 910 | assert!(!should_compact |
| 911 | assert!(!should_compact |
| 912 | // A window smaller than the reserve saturates to zero: only a truly |
| 913 | // empty context avoids tripping. |
| 914 | assert!(!should_compact |
| 915 | assert!(should_compact( |
| 916 | |
| 917 | // estimate_context_tokens is the sum of the per-message estimates. |
| 918 | let msgs = |
| 919 | assert_eq!(estimate_context_tokens( |
| 920 | } |
| 921 | |
| 922 | #[test] |
| 923 | fn cut_point_keeps_recent_tokens() { |
| 924 | // 40 user messages of ~1000 chars (~250 tokens each) = ~10k tokens: |
| 925 | // small enough that nothing gets cut. |
| 926 | let msgs: Vec |
| 927 | .map(|i| |
| 928 | .collect(); |
| 929 | let cut = |
| 930 | assert_eq!(cut.first_kept_index |
| 931 | assert!(!cut. |
| 932 | |
| 933 | // 200 messages of ~1000 chars (~250 tokens) = ~50k tokens: the cut |
| 934 | // should land somewhere after the 80-most-recent (~20k) and keep a |
| 935 | // clean user boundary. |
| 936 | let msgs: Vec |
| 937 | .map(|i| |
| 938 | .collect(); |
| 939 | let cut = |
| 940 | assert!(cut.first_kept_index |
| 941 | assert!(!cut. |
| 942 | assert!(msgs[cut |
| 943 | } |
| 944 | |
| 945 | #[test] |
| 946 | fn cut_point_never_lands_on_tool_result() { |
| 947 | let mut msgs |
| 948 | msgs.push(msg |
| 949 | for i in 0 |
| 950 | let mut call |
| 951 | call.tool_calls = Some |
| 952 | serde_json::to_string(&vec! |
| 953 | "id": format! |
| 954 | })]) |
| 955 | .unwrap(), |
| 956 | ); |
| 957 | msgs.push(call |
| 958 | msgs.push(msg |
| 959 | } |
| 960 | let cut = |
| 961 | assert!(msgs[cut |
| 962 | } |
| 963 | |
| 964 | #[test] |
| 965 | fn split_turn_when_cut_lands_mid_turn() { |
| 966 | // A single huge turn: the cut must fall inside it and report the turn |
| 967 | // start (the user message at index 0). |
| 968 | let mut msgs |
| 969 | for i in 0 |
| 970 | let mut call |
| 971 | call.tool_calls = Some |
| 972 | serde_json::to_string(&vec! |
| 973 | "id": format! |
| 974 | "arguments": {"path" |
| 975 | })]) |
| 976 | .unwrap(), |
| 977 | ); |
| 978 | msgs.push(call |
| 979 | msgs.push(msg |
| 980 | } |
| 981 | let cut = |
| 982 | assert!(cut.is_split_turn |
| 983 | assert_eq!(cut.turn_start_index |
| 984 | assert!(msgs[cut |
| 985 | } |
| 986 | |
| 987 | #[test] |
| 988 | fn serialize_conversation_formats_roles() { |
| 989 | let mut call |
| 990 | call.tool_calls = Some |
| 991 | serde_json::to_string(&vec! |
| 992 | "id": "c1", |
| 993 | })]) |
| 994 | .unwrap(), |
| 995 | ); |
| 996 | let msgs = |
| 997 | msg(1, "user |
| 998 | call, |
| 999 | msg(3, "tool |
| 1000 | ]; |
| 1001 | let out = |
| 1002 | assert!(out.contains |
| 1003 | assert!(out.contains |
| 1004 | assert!(out.contains |
| 1005 | assert!(out.contains |
| 1006 | assert!(out.contains |
| 1007 | } |
| 1008 | |
| 1009 | #[test] |
| 1010 | fn file_ops_are_deduplicated_and_split() { |
| 1011 | let mut a |
| 1012 | a.tool_calls = Some |
| 1013 | serde_json::json!({ |
| 1014 | serde_json::json!({ |
| 1015 | serde_json::json!({ |
| 1016 | serde_json::json!({ |
| 1017 | serde_json::json!({ |
| 1018 | ]) |
| 1019 | .unwrap()); |
| 1020 | let (read, |
| 1021 | assert_eq!(read, |
| 1022 | assert_eq!(modified, |
| 1023 | |
| 1024 | let formatted = |
| 1025 | assert!(formatted. |
| 1026 | assert!(formatted. |
| 1027 | } |
| 1028 | } |