Skip to main content

ast_doc_core/scheduler/
mod.rs

1//! Phase 3: Token budget scheduling.
2//!
3//! Pure mathematical optimizer that selects strategies to fit within
4//! the token budget while protecting core files.
5
6pub mod optimizer;
7
8use std::collections::HashMap;
9
10use crate::{
11    config::{AstDocConfig, OutputStrategy},
12    error::AstDocError,
13    parser::ParsedFile,
14};
15
16/// A file with its assigned strategy and token counts.
17#[derive(Debug, Clone)]
18pub struct ScheduledFile {
19    /// The parsed file data.
20    pub parsed: ParsedFile,
21    /// The assigned output strategy.
22    pub strategy: OutputStrategy,
23    /// Token count for the assigned strategy.
24    pub rendered_tokens: usize,
25    /// Tokens saved compared to Full strategy.
26    pub saved_tokens: usize,
27}
28
29/// Result of the scheduling phase.
30#[derive(Debug)]
31pub struct ScheduleResult {
32    /// All scheduled files.
33    pub files: Vec<ScheduledFile>,
34    /// Total tokens after scheduling.
35    pub total_tokens: usize,
36    /// Raw tokens before scheduling (all files at Full).
37    pub raw_tokens: usize,
38    /// Count of files per strategy.
39    pub strategy_counts: HashMap<OutputStrategy, usize>,
40}
41
42/// Run the token scheduling phase.
43///
44/// `base_overhead_tokens` is the token cost of non-file content
45/// (directory tree + git context) computed during ingestion.
46///
47/// # Errors
48///
49/// Returns `AstDocError::BudgetExceeded` if even minimum strategies
50/// exceed the token budget.
51#[cfg_attr(feature = "hotpath", allow(missing_docs))]
52#[cfg_attr(feature = "hotpath", hotpath::measure)]
53pub fn run_scheduler(
54    parsed: &[ParsedFile],
55    config: &AstDocConfig,
56    base_overhead_tokens: usize,
57) -> Result<ScheduleResult, AstDocError> {
58    optimizer::optimize(parsed, config, base_overhead_tokens)
59}