返回 CodeWhale
config_policy.rs
根目录 / crates / command-contract / src / config_policy.rs
1 //! Policy/status observations only. Hosts own persistence, runtime services and I/O.
2 //! These values do not grant authority to edit configuration or operate a session.
3 use crate::types::{CommandApprovalMode, CommandCurrency, CommandMode};
4 use codewhale_protocol::cloud_facts::CloudFactsState;
5 use std::path::PathBuf;
6 use std::time::Duration;
7
8 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
9 pub enum CommandPermissionAction {
10 Allow,
11 Ask,
12 Deny,
13 }
14 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
15 pub enum CommandPermissionsFileState {
16 Missing,
17 Empty,
18 Present,
19 }
20
21 #[derive(Debug, Clone, PartialEq, Eq)]
22 pub struct PermissionRule {
23 pub action: CommandPermissionAction,
24 pub tool: String,
25 pub command: Option<String>,
26 pub command_exact: bool,
27 pub path: Option<String>,
28 pub workspace: Option<String>,
29 pub applies_here: bool,
30 pub warning: Option<String>,
31 /// Opaque host-generated token, kept with the rule it confirms.
32 pub removal_token: String,
33 }
34 #[derive(Debug, Clone, PartialEq, Eq)]
35 pub struct PermissionsView {
36 pub path: PathBuf,
37 pub file_state: CommandPermissionsFileState,
38 pub rules: Vec<PermissionRule>,
39 pub approval_mode: CommandApprovalMode,
40 pub audit_path: Option<PathBuf>,
41 }
42 #[derive(Debug, Clone, PartialEq, Eq)]
43 pub struct RemovedPermissionRule {
44 pub action: CommandPermissionAction,
45 pub tool: String,
46 }
47 #[derive(Debug, Clone, PartialEq, Eq)]
48 pub struct BlockedPermissionAction {
49 pub receipt_id: Option<String>,
50 pub tool_name: String,
51 pub rule: String,
52 pub reason: String,
53 pub action_summary: String,
54 pub approvable: bool,
55 pub approved: bool,
56 }
57
58 pub trait CommandPermissionsContext {
59 fn snapshot(&self) -> Result<PermissionsView, String>;
60 /// The host must lock, re-read and verify this token before atomic removal.
61 /// Index is zero-based; failure must not mutate the file.
62 fn remove_rule(
63 &mut self,
64 index: usize,
65 expected_token: &str,
66 ) -> Result<RemovedPermissionRule, String>;
67 fn blocked_actions(&self) -> Vec<BlockedPermissionAction> {
68 Vec::new()
69 }
70 }
71
72 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
73 pub enum StatusSafety {
74 ReadOnly {
75 enforced: bool,
76 },
77 WorkspaceWrite {
78 enforced: bool,
79 network_access: bool,
80 },
81 FullAccess {
82 no_new_privs: Option<bool>,
83 },
84 External,
85 }
86 #[derive(Debug, Clone, PartialEq, Eq)]
87 pub struct StatusFleetDrift {
88 pub name: String,
89 pub ids: Vec<String>,
90 }
91 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
92 pub enum StatusSnapshotScope {
93 /// Snapshot-eligible content exceeds the configured workspace size cap.
94 WorkspaceTooLarge,
95 /// The entry ceiling is independent from the configurable size cap.
96 TooManyFiles,
97 /// Home/root locations remain refused regardless of the size setting.
98 UnsafeLocation,
99 /// Missing history was restarted; earlier restore points are gone.
100 HistoryRepaired,
101 /// A real git/disk failure; the notice limit field carries the error.
102 Failing,
103 }
104 #[derive(Debug, Clone, PartialEq, Eq)]
105 /// Retained semantic observation shared by status and transient host notices.
106 pub struct StatusSnapshotNotice {
107 pub workspace: String,
108 pub scope: StatusSnapshotScope,
109 pub limit: String,
110 }
111 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
112 pub enum StatusContextSource {
113 Configured,
114 UserDeclared,
115 ConfiguredModel,
116 ProviderReported,
117 StaticKimiCodeSafeFloor,
118 Catalog,
119 NameSuffixHint,
120 Fallback,
121 }
122 #[derive(Debug, Clone, PartialEq, Eq)]
123 pub enum StatusWindowOverride {
124 Provider(String),
125 ActiveProvider,
126 }
127 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
128 pub enum StatusCatalogFreshness {
129 Bundled,
130 Live,
131 Stale,
132 Failed,
133 }
134 #[derive(Debug, Clone, PartialEq, Eq)]
135 pub struct StatusCatalog {
136 pub freshness: StatusCatalogFreshness,
137 pub offering_count: usize,
138 pub fetched_at: Option<u64>,
139 pub last_error: Option<String>,
140 }
141 /// Same semantic inputs as the existing session metrics strip; no renderer dependency.
142 #[derive(Debug, Clone, Copy, Default, PartialEq)]
143 pub struct StatusMetrics {
144 pub turns: u64,
145 pub steps: u64,
146 pub llm_time: Duration,
147 pub tool_time: Duration,
148 pub ttft_avg: Option<Duration>,
149 pub tokens_per_second: Option<f64>,
150 pub cache_hit_percent: Option<u8>,
151 pub input_tokens: u64,
152 }
153 impl StatusMetrics {
154 #[must_use]
155 pub fn is_empty(&self) -> bool {
156 self.turns == 0 && self.steps == 0 && self.input_tokens == 0
157 }
158 }
159 #[derive(Debug, Clone, Default, PartialEq, Eq)]
160 pub struct StatusToolOutputs {
161 pub raw_large_count: usize,
162 pub raw_large_chars: usize,
163 pub receipt_count: usize,
164 pub artifact_count: usize,
165 pub artifact_bytes: u64,
166 }
167
168 #[derive(Debug, Clone, PartialEq)]
169 pub struct ConfigStatusView {
170 pub version: String,
171 pub provider: String,
172 pub model: String,
173 pub reasoning: String,
174 pub workspace: PathBuf,
175 pub home: Option<PathBuf>,
176 pub project_docs: Vec<String>,
177 pub mode: CommandMode,
178 pub approval_mode: CommandApprovalMode,
179 pub trusted: bool,
180 pub allow_shell: bool,
181 pub safety: StatusSafety,
182 pub mcp_configured_count: usize,
183 /// The raw missing pin, which may differ from the display model label.
184 pub model_pin_drift: Option<String>,
185 pub fleet_drift: Option<StatusFleetDrift>,
186 pub snapshot_notice: Option<StatusSnapshotNotice>,
187 pub context_used: usize,
188 pub context_window: u32,
189 pub context_source: StatusContextSource,
190 pub window_override: Option<StatusWindowOverride>,
191 pub catalog: StatusCatalog,
192 pub cloud_facts: CloudFactsState,
193 pub observed_at: u64,
194 pub session_id: Option<String>,
195 pub history_count: usize,
196 pub message_count: usize,
197 pub input_tokens: u32,
198 pub output_tokens: u32,
199 pub total_tokens: u32,
200 pub cache_hit_tokens: u32,
201 pub cache_miss_tokens: u32,
202 pub cost: f64,
203 pub currency: CommandCurrency,
204 pub cost_label: Option<String>,
205 pub metrics: StatusMetrics,
206 pub ascii_safe: bool,
207 pub tool_outputs: StatusToolOutputs,
208 }
209
210 /// Read-only observations; optional failures are represented by absent fields.
211 /// No config/session mutation, provider refresh or rendered-report callback.
212 pub trait CommandConfigStatusContext {
213 fn snapshot(&self) -> ConfigStatusView;
214 }
215
216 /// The size-cap remedy is shared by the status report and host toast.
217 pub const SNAPSHOTS_CAP_CONFIG_KEY: &str = "[snapshots] max_workspace_gb";
218 impl StatusSnapshotNotice {
219 pub fn render(&self, template: &str) -> String {
220 codewhale_protocol::display::interpolate(
221 template,
222 &[
223 ("{workspace}", &self.workspace),
224 ("{limit}", &self.limit),
225 ("{config_key}", SNAPSHOTS_CAP_CONFIG_KEY),
226 ],
227 )
228 }
229 }
230
230 lines RUST