refactor: extract context window building into context.rs

Move context window construction (build_context_window, plan_context,
render_journal_text, assemble_context), token counting, error
classification, and related helpers from agent.rs into context.rs.

All extracted functions are pure — they take inputs and return values
with no mutable state access. State mutation stays in agent.rs
(compact, restore_from_log, load_startup_journal).

agent.rs: 1504 → 987 lines (-517)
context.rs: 365 lines (new)
Net: -152 lines (duplicate comments removed)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kent Overstreet 2026-03-21 15:55:30 -04:00
parent d04d41e993
commit db48d57917
3 changed files with 206 additions and 488 deletions

View file

@ -37,6 +37,7 @@ mod agent;
mod api;
mod cli;
mod config;
mod context;
mod dmn;
mod journal;
mod log;
@ -66,13 +67,13 @@ use crate::ui_channel::{ContextInfo, StatusInfo, StreamTarget, UiMessage};
/// Hard compaction threshold — context is rebuilt immediately.
/// Uses config percentage of model context window.
fn compaction_threshold(model: &str, app: &AppConfig) -> u32 {
(agent::model_context_window(model) as u32) * app.compaction.hard_threshold_pct / 100
(context::model_context_window(model) as u32) * app.compaction.hard_threshold_pct / 100
}
/// Soft threshold — nudge the model to journal before compaction.
/// Fires once; the hard threshold handles the actual rebuild.
fn pre_compaction_threshold(model: &str, app: &AppConfig) -> u32 {
(agent::model_context_window(model) as u32) * app.compaction.soft_threshold_pct / 100
(context::model_context_window(model) as u32) * app.compaction.soft_threshold_pct / 100
}
#[tokio::main]