Skip to main content

xchecker_packet/
lib.rs

1//! Packet construction system for token-efficient context management.
2//!
3//! This crate implements the packet building system that selects and organizes
4//! content for Claude CLI invocations while respecting budget constraints and
5//! maintaining evidence for auditability.
6
7use xchecker_utils::types::PacketEvidence;
8
9mod budget;
10mod builder;
11mod model;
12mod render;
13mod selectors;
14
15/// A packet of content prepared for Claude CLI consumption.
16#[derive(Debug, Clone)]
17pub struct Packet {
18    /// The actual content to send to Claude.
19    pub content: String,
20    /// BLAKE3 hash of the packet content (after redaction).
21    pub blake3_hash: String,
22    /// Evidence of what went into the packet for auditability.
23    pub evidence: PacketEvidence,
24    /// Information about budget usage.
25    pub budget_used: BudgetUsage,
26}
27
28impl Packet {
29    /// Create a new packet.
30    #[must_use]
31    pub const fn new(
32        content: String,
33        blake3_hash: String,
34        evidence: PacketEvidence,
35        budget_used: BudgetUsage,
36    ) -> Self {
37        Self {
38            content,
39            blake3_hash,
40            evidence,
41            budget_used,
42        }
43    }
44
45    /// Get the packet content.
46    #[must_use]
47    #[allow(dead_code)] // Public API for packet inspection
48    pub fn content(&self) -> &str {
49        &self.content
50    }
51
52    /// Get the packet hash.
53    #[must_use]
54    pub fn hash(&self) -> &str {
55        &self.blake3_hash
56    }
57
58    /// Get the packet evidence.
59    #[must_use]
60    #[allow(dead_code)] // Public API for evidence inspection
61    pub const fn evidence(&self) -> &PacketEvidence {
62        &self.evidence
63    }
64
65    /// Get budget usage information.
66    #[must_use]
67    pub const fn budget_usage(&self) -> &BudgetUsage {
68        &self.budget_used
69    }
70
71    /// Check if packet is within budget limits.
72    #[must_use]
73    #[allow(dead_code)] // Public API for budget validation
74    pub const fn is_within_budget(&self) -> bool {
75        !self.budget_used.is_exceeded()
76    }
77}
78
79/// Information about packet budget usage.
80#[derive(Debug, Clone)]
81pub struct BudgetUsage {
82    /// Current bytes used.
83    pub bytes_used: usize,
84    /// Current lines used.
85    pub lines_used: usize,
86    /// Maximum bytes allowed.
87    pub max_bytes: usize,
88    /// Maximum lines allowed.
89    pub max_lines: usize,
90}
91
92impl BudgetUsage {
93    /// Create a new budget tracker.
94    #[must_use]
95    pub const fn new(max_bytes: usize, max_lines: usize) -> Self {
96        Self {
97            bytes_used: 0,
98            lines_used: 0,
99            max_bytes,
100            max_lines,
101        }
102    }
103
104    /// Check if adding content would exceed budget.
105    #[must_use]
106    pub const fn would_exceed(&self, bytes: usize, lines: usize) -> bool {
107        self.bytes_used + bytes > self.max_bytes || self.lines_used + lines > self.max_lines
108    }
109
110    /// Add content to budget tracking.
111    pub const fn add_content(&mut self, bytes: usize, lines: usize) {
112        self.bytes_used += bytes;
113        self.lines_used += lines;
114    }
115
116    /// Check if budget is exceeded.
117    #[must_use]
118    pub const fn is_exceeded(&self) -> bool {
119        self.bytes_used > self.max_bytes || self.lines_used > self.max_lines
120    }
121}
122
123pub use builder::{DEFAULT_PACKET_MAX_BYTES, DEFAULT_PACKET_MAX_LINES, PacketBuilder};
124pub use model::{PriorityRules, SelectedFile};
125pub use selectors::ContentSelector;