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    pub codex_content: crate::CodexContentMode,
26}
27impl Default for ReadOptions {
28    fn default() -> Self {
29        Self {
30            max_file_bytes: Some(200 * 1024 * 1024),
31            max_line_bytes: Some(8 * 1024 * 1024),
32            stop_at_byte: None,
33            include: EventKinds::ALL,
34            tail: TailMode::Strict,
35            codex_usage: CodexUsageMode::TokenCount,
36            codex_content: crate::CodexContentMode::Auto,
37            accounting: crate::AccountingPolicy::Strict,
38        }
39    }
40}
41impl ReadOptions {
42    pub(crate) fn validate(&self) -> Result<(), ReadError> {
43        if matches!((self.stop_at_byte,self.max_file_bytes),(Some(s),Some(m)) if s>m) {
44            return Err(ReadError::InvalidOptions("snapshot exceeds byte budget"));
45        }
46        Ok(())
47    }
48}
49
50#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
51#[non_exhaustive]
52pub enum ReadStatus {
53    Complete,
54    CompleteWithErrors,
55    IncompleteTail,
56    #[default]
57    StoppedEarly,
58    Failed,
59}
60
61#[derive(Debug, Clone, Default, Serialize)]
62pub struct ReadSummary {
63    pub status: ReadStatus,
64    pub lines: u64,
65    pub bytes_read: u64,
66    /// End of the contiguous, error-free, fully delivered record prefix.
67    pub last_complete_byte: u64,
68    pub truncated_tail: bool,
69    pub line_errors: u64,
70    pub unknown_types: BTreeMap<String, u64>,
71    pub ignored_types: BTreeMap<String, u64>,
72}
73impl ReadSummary {
74    pub fn is_complete(&self) -> bool {
75        self.status == ReadStatus::Complete
76    }
77    /// Clean source read with no unrecognized selected record/content types.
78    /// Deliberate omissions remain in ignored_types; consult the support contract.
79    pub fn is_supported(&self) -> bool {
80        self.is_complete() && self.unknown_types.is_empty()
81    }
82}