oxi-cli 0.62.0

Terminal-based AI coding assistant — multi-provider, streaming-first, extensible
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! `ProjectPluginOverrides` — project-scoped force on/off for resources.
//!
//! Stored under `<project>/.oxi/plugin-overrides.json`. Distinct from the
//! user-level `RuntimeConfig`: project overrides take precedence so that
//! `oxi` policy can force a resource ON or OFF regardless of what the user
//! toggled globally. The combined resolution order is:
//!
//! 1. Project `Forced::On` / `Forced::Off` (highest, applies to both
//!    installed and would-be-disabled resources)
//! 2. User-level `RuntimeConfig::disabled`
//! 3. Default — resource is enabled
//!
//! File format:
//! ```json
//! {
//!   "version": 1,
//!   "forced": {
//!     "@foo/oxi-tools": {
//!       "extension": "off",
//!       "skill": "on"
//!     },
//!     "lodash": { "prompt": "off" }
//!   }
//! }
//! ```

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use super::types::ResourceKind;

/// Schema version for `ProjectPluginOverrides`.
pub const OVERRIDES_VERSION: u32 = 1;

/// Default filename under `<project>/.oxi/`.
pub const OVERRIDES_FILE: &str = "plugin-overrides.json";

/// Tri-state project force for a single (package, kind) pair.
///
/// `On` / `Off` take precedence over user-level `RuntimeConfig.disabled`
/// and over the default (`enabled`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ForceState {
    /// Force the resource enabled (project overrides user-disable).
    On,
    /// Force the resource disabled (project overrides default-on).
    Off,
}

impl std::fmt::Display for ForceState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ForceState::On => write!(f, "on"),
            ForceState::Off => write!(f, "off"),
        }
    }
}

/// Map from package name to per-kind `ForceState`. Keys present in the
/// map override anything below them in the precedence chain.
pub type ForceMap = BTreeMap<String, BTreeMap<ResourceKind, ForceState>>;

/// Per-project plugin override file.
///
/// A project is "the directory containing `.oxi/`". Path is supplied by
/// the caller (typically `PackageManager::project_dir`); the file itself
/// is optional — missing-file is the no-overrides case.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProjectPluginOverrides {
    /// Schema version.
    #[serde(default = "default_version")]
    pub version: u32,
    /// Per-package, per-kind force map.
    #[serde(default)]
    pub forced: ForceMap,
}

fn default_version() -> u32 {
    OVERRIDES_VERSION
}

impl ProjectPluginOverrides {
    /// Empty overrides (everything follows `RuntimeConfig`/default).
    pub fn new() -> Self {
        Self {
            version: OVERRIDES_VERSION,
            forced: BTreeMap::new(),
        }
    }

    /// Canonical path: `<project_dir>/.oxi/<OVERRIDES_FILE>`.
    pub fn project_path(project_dir: &Path) -> PathBuf {
        project_dir.join(".oxi").join(OVERRIDES_FILE)
    }

    /// Strict read — missing file is `Ok(Empty)`, corrupt file is an
    /// error so Doctor can surface it (callers wanting best-effort use
    /// `read_or_default`).
    pub fn read(path: &Path) -> Result<Self> {
        if !path.exists() {
            return Ok(Self::new());
        }
        let content = fs::read_to_string(path)
            .with_context(|| format!("Failed to read overrides at {}", path.display()))?;
        let cfg: Self = serde_json::from_str(&content)
            .with_context(|| format!("Failed to parse overrides at {}", path.display()))?;
        Ok(cfg)
    }

    /// Lenient read: missing or corrupt -> empty overrides. Doctor is
    /// the structured channel for surfacing corruption.
    pub fn read_or_default(path: &Path) -> Self {
        Self::read(path).unwrap_or_default()
    }

    /// Atomic write matching the `RuntimeConfig::write` policy.
    pub fn write(&self, path: &Path) -> Result<()> {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("Failed to create parent dir for {}", path.display()))?;
        }
        let content =
            serde_json::to_string_pretty(self).context("Failed to serialize overrides")?;
        let tmp = path.with_extension(format!(
            "tmp.{}.{}",
            std::process::id(),
            uuid::Uuid::new_v4().simple()
        ));
        fs::write(&tmp, content)
            .with_context(|| format!("Failed to write tmp overrides {}", tmp.display()))?;
        match fs::rename(&tmp, path) {
            Ok(()) => Ok(()),
            Err(e) => {
                let _ = fs::remove_file(&tmp);
                Err(e).with_context(|| format!("Failed to rename overrides to {}", path.display()))
            }
        }
    }

    /// Number of (package, kind) override entries.
    pub fn entry_count(&self) -> usize {
        self.forced.values().map(|m| m.len()).sum()
    }

    /// True when no overrides are set.
    pub fn is_empty(&self) -> bool {
        self.forced.values().all(|m| m.is_empty())
    }

    /// The forced state for `(package, kind)`, if any.
    pub fn forced_state(&self, package: &str, kind: ResourceKind) -> Option<ForceState> {
        self.forced.get(package).and_then(|m| m.get(&kind)).copied()
    }

    /// Force a kind on under a package (project layer can rescue a
    /// resource the user disabled globally).
    pub fn force_on(&mut self, package: impl Into<String>, kind: ResourceKind) {
        self.forced
            .entry(package.into())
            .or_default()
            .insert(kind, ForceState::On);
    }

    /// Force a kind off under a package.
    pub fn force_off(&mut self, package: impl Into<String>, kind: ResourceKind) {
        self.forced
            .entry(package.into())
            .or_default()
            .insert(kind, ForceState::Off);
    }

    /// Drop the override for `(package, kind)`. Keys with empty inner
    /// maps are also removed so the persisted file stays minimal.
    pub fn clear(&mut self, package: &str, kind: ResourceKind) {
        if let Some(inner) = self.forced.get_mut(package) {
            inner.remove(&kind);
            if inner.is_empty() {
                self.forced.remove(package);
            }
        }
    }
}

/// Resolve the enabled flag for `(package, kind)` under the layered
/// precedence: project-force > runtime-disable > default(true).
///
/// Pure, single-purpose helper used by `PackageManager::resolve_with_config`
/// and the Doctor summary views. Pulled out so it's testable without
/// touching the filesystem.
pub fn resolve_enabled(
    package: &str,
    kind: ResourceKind,
    overrides: Option<&ProjectPluginOverrides>,
    runtime: Option<&RuntimeConfig>,
) -> bool {
    if let Some(o) = overrides
        && let Some(state) = o.forced_state(package, kind)
    {
        return match state {
            ForceState::On => true,
            ForceState::Off => false,
        };
    }
    if let Some(r) = runtime
        && r.is_disabled(package, kind)
    {
        return false;
    }
    true
}

// Reference `RuntimeConfig` for callers using `resolve_enabled` from
// sibling modules — keeps the import surface obvious without creating a
// cycle.
use super::runtime_config::RuntimeConfig;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn defaults_are_enabled() {
        let o = ProjectPluginOverrides::new();
        assert!(o.is_empty());
        assert_eq!(o.entry_count(), 0);
    }

    #[test]
    fn force_on_and_force_off_track_separately() {
        let mut o = ProjectPluginOverrides::new();
        o.force_on("@foo/oxi-tools", ResourceKind::Extension);
        o.force_off("@foo/oxi-tools", ResourceKind::Skill);
        o.force_off("lodash", ResourceKind::Prompt);

        assert_eq!(o.entry_count(), 3);
        assert_eq!(
            o.forced_state("@foo/oxi-tools", ResourceKind::Extension),
            Some(ForceState::On)
        );
        assert_eq!(
            o.forced_state("@foo/oxi-tools", ResourceKind::Skill),
            Some(ForceState::Off)
        );
        assert_eq!(
            o.forced_state("lodash", ResourceKind::Prompt),
            Some(ForceState::Off)
        );
        assert_eq!(o.forced_state("lodash", ResourceKind::Theme), None);
    }

    #[test]
    fn clear_drops_empty_outer_keys() {
        let mut o = ProjectPluginOverrides::new();
        o.force_on("pkg", ResourceKind::Skill);
        o.clear("pkg", ResourceKind::Skill);
        assert!(o.is_empty());
        assert_eq!(o.entry_count(), 0);
    }

    #[test]
    fn write_then_read_round_trips() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("plugin-overrides.json");
        let mut o = ProjectPluginOverrides::new();
        o.force_on("lodash", ResourceKind::Skill);
        o.force_off("@scope/x", ResourceKind::Extension);
        o.write(&path).unwrap();

        let loaded = ProjectPluginOverrides::read(&path).unwrap();
        assert_eq!(loaded.entry_count(), 2);
        assert_eq!(
            loaded.forced_state("lodash", ResourceKind::Skill),
            Some(ForceState::On)
        );
        assert_eq!(
            loaded.forced_state("@scope/x", ResourceKind::Extension),
            Some(ForceState::Off)
        );
    }

    #[test]
    fn missing_file_reads_as_empty() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nope.json");
        let o = ProjectPluginOverrides::read(&path).unwrap();
        assert!(o.is_empty());
    }

    #[test]
    fn corrupt_file_reads_as_empty_via_lenient_helper() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("plugin-overrides.json");
        fs::write(&path, b"{ not valid").unwrap();
        assert!(ProjectPluginOverrides::read(&path).is_err());
        let o = ProjectPluginOverrides::read_or_default(&path);
        assert!(o.is_empty());
    }

    // ── Precedence tests ─────────────────────────────────────────────

    fn build_layers() -> (ProjectPluginOverrides, RuntimeConfig) {
        let mut o = ProjectPluginOverrides::new();
        let mut r = RuntimeConfig::new();
        // pkg-A: project forces Off on extension; user also disabled (project wins → off)
        o.force_off("pkg-A", ResourceKind::Extension);
        r.disable("pkg-A", ResourceKind::Extension);
        // pkg-B: project forces On on skill; user disabled (project wins → on)
        o.force_on("pkg-B", ResourceKind::Skill);
        r.disable("pkg-B", ResourceKind::Skill);
        // pkg-C: project has no entry; user disabled (only runtime wins → off)
        r.disable("pkg-C", ResourceKind::Prompt);
        // pkg-D: project forces Off on extension; user has no entry (project wins → off)
        o.force_off("pkg-D", ResourceKind::Extension);
        // pkg-E: project forces On; user has no entry (project wins → on)
        o.force_on("pkg-E", ResourceKind::Theme);
        // pkg-F: project && user say nothing (default on)
        (o, r)
    }

    #[test]
    fn precedence_project_force_off_overrides_user_disable() {
        let (o, r) = build_layers();
        // pkg-A: both layers disable; project-force off sticks.
        assert!(!resolve_enabled(
            "pkg-A",
            ResourceKind::Extension,
            Some(&o),
            Some(&r),
        ));
    }

    #[test]
    fn precedence_project_force_on_overrides_user_disable() {
        let (o, r) = build_layers();
        // pkg-B: user disables, project forces on. Project wins — enabled.
        assert!(resolve_enabled(
            "pkg-B",
            ResourceKind::Skill,
            Some(&o),
            Some(&r),
        ));
    }

    #[test]
    fn precedence_user_disable_holds_when_no_project_entry() {
        let (o, r) = build_layers();
        // pkg-C has no project entry; runtime disables prompt.
        assert!(!resolve_enabled(
            "pkg-C",
            ResourceKind::Prompt,
            Some(&o),
            Some(&r),
        ));
    }

    #[test]
    fn precedence_project_force_off_without_user_entry() {
        let (o, r) = build_layers();
        assert!(!resolve_enabled(
            "pkg-D",
            ResourceKind::Extension,
            Some(&o),
            Some(&r),
        ));
    }

    #[test]
    fn precedence_project_force_on_without_user_entry() {
        let (o, r) = build_layers();
        assert!(resolve_enabled(
            "pkg-E",
            ResourceKind::Theme,
            Some(&o),
            Some(&r),
        ));
    }

    #[test]
    fn precedence_default_enabled_when_neither_layer_speaks() {
        let (o, r) = build_layers();
        assert!(resolve_enabled(
            "pkg-F",
            ResourceKind::Skill,
            Some(&o),
            Some(&r),
        ));
    }

    #[test]
    fn precedence_falls_back_to_runtime_when_no_overrides() {
        let mut r = RuntimeConfig::new();
        r.disable("only-user", ResourceKind::Theme);
        assert!(!resolve_enabled(
            "only-user",
            ResourceKind::Theme,
            None,
            Some(&r),
        ));
        // Different (package, kind) pair, no override, no disable: default on.
        assert!(resolve_enabled(
            "only-user",
            ResourceKind::Skill,
            None,
            Some(&r),
        ));
    }

    #[test]
    fn precedence_default_enabled_when_no_layers_at_all() {
        assert!(resolve_enabled("any", ResourceKind::Skill, None, None));
    }
}