Skip to main content

agent_sessions/reader/
options.rs

1use crate::{CodexUsageMode, EventKinds, ReadError};
2use serde::Serialize;
3use std::collections::BTreeMap;
4
5#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum TailMode {
8    #[default]
9    Strict,
10    AllowIncomplete,
11}
12
13#[derive(Debug, Clone)]
14pub struct ReadOptions {
15    pub accounting: crate::AccountingPolicy,
16    /// Byte budget from the start of the source. With stop_at_byte, only that
17    /// captured prefix must fit; bytes beyond it are not read or size-checked.
18    pub max_file_bytes: Option<u64>,
19    /// Includes the line delimiter. Enforced before allocating the whole line.
20    pub max_line_bytes: Option<usize>,
21    pub stop_at_byte: Option<u64>,
22    pub include: EventKinds,
23    pub tail: TailMode,
24    pub codex_usage: CodexUsageMode,
25}
26impl Default for ReadOptions {
27    fn default() -> Self {
28        Self {
29            max_file_bytes: Some(200 * 1024 * 1024),
30            max_line_bytes: Some(8 * 1024 * 1024),
31            stop_at_byte: None,
32            include: EventKinds::ALL,
33            tail: TailMode::Strict,
34            codex_usage: CodexUsageMode::TokenCount,
35            accounting: crate::AccountingPolicy::Strict,
36        }
37    }
38}
39impl ReadOptions {
40    pub(crate) fn validate(&self) -> Result<(), ReadError> {
41        if matches!((self.stop_at_byte,self.max_file_bytes),(Some(s),Some(m)) if s>m) {
42            return Err(ReadError::InvalidOptions("snapshot exceeds byte budget"));
43        }
44        Ok(())
45    }
46}
47
48#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
49#[non_exhaustive]
50pub enum ReadStatus {
51    Complete,
52    CompleteWithErrors,
53    IncompleteTail,
54    #[default]
55    StoppedEarly,
56    Failed,
57}
58
59#[derive(Debug, Clone, Default, Serialize)]
60pub struct ReadSummary {
61    pub status: ReadStatus,
62    pub lines: u64,
63    pub bytes_read: u64,
64    /// End of the contiguous, error-free, fully delivered record prefix.
65    pub last_complete_byte: u64,
66    pub truncated_tail: bool,
67    pub line_errors: u64,
68    pub unknown_types: BTreeMap<String, u64>,
69    pub ignored_types: BTreeMap<String, u64>,
70}
71impl ReadSummary {
72    pub fn is_complete(&self) -> bool {
73        self.status == ReadStatus::Complete
74    }
75}