oy-cli 0.13.5

Focused OpenCode agent with repeatable audits, code reviews, and one-finding fixes
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! OpenCode package setup, namespace migration, backup, and integration validation.

mod backup;
mod config_file;

#[cfg(test)]
use super::OY_AGENT;
use super::{OpenCodeHost, api};
use anyhow::{Context, Result, bail};
use backup::{create_backup_dir, move_path, restore_moved_paths};
use config_file::{
    config_body, config_has_all_oy_entries, config_has_oy_entries, opencode_plugin_spec,
    parse_opencode_config, remove_owned_config,
};
use serde_json::json;
use std::fs;
use std::io::{IsTerminal as _, Write as _};
use std::path::{Path, PathBuf};

use crate::{config, ui};

#[derive(Debug)]
struct SetupOutcome {
    config_path: PathBuf,
    backup: Option<PathBuf>,
}

struct ConfigUpdate {
    path: PathBuf,
    body: String,
    current: Option<Vec<u8>>,
}

impl ConfigUpdate {
    fn new(path: PathBuf, body: String) -> Result<Self> {
        let current = match fs::read(&path) {
            Ok(bytes) => Some(bytes),
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
            Err(error) => {
                return Err(error).with_context(|| format!("failed reading {}", path.display()));
            }
        };
        Ok(Self {
            path,
            body,
            current,
        })
    }

    fn changed(&self) -> bool {
        self.current.as_deref() != Some(self.body.as_bytes())
    }
}

pub(crate) fn setup_command(workspace: bool, dry_run: bool, remove: bool) -> Result<i32> {
    let scope = SetupScope::from_workspace_flag(workspace);
    if remove {
        remove_opencode(scope, dry_run)
    } else {
        setup_opencode(scope, true, dry_run)
    }
}

pub(crate) fn global_config_path() -> Result<PathBuf> {
    Ok(config_path_in(&global_opencode_dir()?))
}

pub(crate) fn workspace_config_path() -> Result<PathBuf> {
    let root = config::oy_root()?;
    Ok(config_path_in(&root.join(".opencode")))
}

fn global_opencode_dir() -> Result<PathBuf> {
    if let Some(value) = std::env::var_os("OPENCODE_CONFIG_DIR") {
        if value.is_empty() {
            bail!("OPENCODE_CONFIG_DIR must not be empty");
        }
        let path = PathBuf::from(value);
        return Ok(if path.is_absolute() {
            path
        } else {
            config::oy_root()?.join(path)
        });
    }
    dirs::config_dir()
        .context("failed to find user config directory")
        .map(|dir| dir.join("opencode"))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SetupScope {
    Global,
    Workspace,
}

impl SetupScope {
    fn from_workspace_flag(workspace: bool) -> Self {
        if workspace {
            Self::Workspace
        } else {
            Self::Global
        }
    }

    fn dir(self) -> Result<PathBuf> {
        match self {
            Self::Global => global_opencode_dir(),
            Self::Workspace => {
                let root = config::oy_root()?;
                Ok(root.join(".opencode"))
            }
        }
    }

    fn label(self) -> &'static str {
        match self {
            Self::Global => "global",
            Self::Workspace => "workspace",
        }
    }
}

fn setup_opencode(scope: SetupScope, report: bool, dry_run: bool) -> Result<i32> {
    let dir = scope.dir()?;
    if dry_run {
        return preview_setup(scope, &dir);
    }
    let _lock = SetupLock::acquire(&dir)?;
    let files = legacy_oy_paths(&dir)?;
    let updates = setup_config_updates(&dir)?;
    let changed = !files.is_empty() || updates.iter().any(ConfigUpdate::changed);
    let backup = apply_integration_update(&dir, &files, &updates)?;
    if changed && let Ok(root) = config::oy_root() {
        let host = OpenCodeHost::selected_in(&root);
        if host.supported() {
            let _ = api::OpenCodeApi::new(&host).evict_location(&root);
        }
    }

    if report {
        report_setup(
            "installed",
            scope,
            &SetupOutcome {
                config_path: config_path_in(&dir),
                backup,
            },
        )?;
    }
    Ok(0)
}

fn remove_opencode(scope: SetupScope, dry_run: bool) -> Result<i32> {
    let dir = scope.dir()?;
    let _lock = if dry_run {
        None
    } else {
        Some(SetupLock::acquire(&dir)?)
    };
    let files = legacy_oy_paths(&dir)?;
    let updates = removal_config_updates(&dir)?;
    if dry_run {
        ui::section(format!("{} oy integration removal dry run", scope.label()).as_str());
        for path in &files {
            ui::kv("move", path.display());
        }
        preview_config_updates(&updates);
        return Ok(0);
    }
    let backup = apply_integration_update(&dir, &files, &updates)?;
    report_setup(
        "removed",
        scope,
        &SetupOutcome {
            config_path: config_path_in(&dir),
            backup,
        },
    )?;
    Ok(0)
}

fn report_setup(status: &str, scope: SetupScope, outcome: &SetupOutcome) -> Result<()> {
    if ui::is_json() {
        ui::line(serde_json::to_string_pretty(&json!({
            "status": status,
            "scope": scope.label(),
            "config": outcome.config_path,
            "backup": outcome.backup,
        }))?);
        return Ok(());
    }
    ui::success(format_args!("{status} {} oy integration", scope.label()));
    if let Some(backup) = &outcome.backup {
        ui::line(format_args!(
            "Previous oy integration files were moved to {}.",
            backup.display()
        ));
    }
    if status == "installed" {
        ui::line(format_args!(
            "Restart opencode for {} to install and load.",
            opencode_plugin_spec()
        ));
    }
    Ok(())
}

struct SetupLock {
    path: PathBuf,
}

impl SetupLock {
    fn acquire(dir: &Path) -> Result<Self> {
        let parent = dir.parent().unwrap_or(dir);
        fs::create_dir_all(parent)?;
        let path = parent.join(".oy-opencode-setup.lock");
        for attempt in 0..2 {
            match fs::OpenOptions::new()
                .write(true)
                .create_new(true)
                .open(&path)
            {
                Ok(mut file) => {
                    use std::io::Write as _;
                    writeln!(file, "{}", std::process::id())?;
                    return Ok(Self { path });
                }
                Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists && attempt == 0 => {
                    if stale_setup_lock(&path) {
                        fs::remove_file(&path)?;
                        continue;
                    }
                    return Err(error).with_context(|| {
                        format!("another oy setup/remove may be running: {}", path.display())
                    });
                }
                Err(error) => {
                    return Err(error).with_context(|| {
                        format!("failed acquiring setup lock: {}", path.display())
                    });
                }
            }
        }
        unreachable!("setup lock loop always returns")
    }
}

fn stale_setup_lock(path: &Path) -> bool {
    let pid = fs::read_to_string(path)
        .ok()
        .and_then(|value| value.trim().parse::<u32>().ok());
    let Some(pid) = pid else {
        return fs::metadata(path)
            .and_then(|metadata| metadata.modified())
            .ok()
            .and_then(|modified| modified.elapsed().ok())
            .is_some_and(|age| age > std::time::Duration::from_secs(30));
    };
    let result = unsafe { libc::kill(pid as i32, 0) };
    result != 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH)
}

impl Drop for SetupLock {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.path);
    }
}

fn preview_setup(scope: SetupScope, dir: &Path) -> Result<i32> {
    ui::section(format!("{} oy integration dry run", scope.label()).as_str());
    for path in legacy_oy_paths(dir)? {
        ui::kv("move", path.display());
    }
    preview_config_updates(&setup_config_updates(dir)?);
    Ok(0)
}

fn setup_config_updates(dir: &Path) -> Result<Vec<ConfigUpdate>> {
    let primary = config_path_in(dir);
    let mut updates = vec![ConfigUpdate::new(primary.clone(), config_body(&primary)?)?];
    for path in config_paths_in(dir) {
        if path != primary && path.exists() {
            updates.push(ConfigUpdate::new(
                path.clone(),
                remove_owned_config(&path)?,
            )?);
        }
    }
    Ok(updates)
}

fn removal_config_updates(dir: &Path) -> Result<Vec<ConfigUpdate>> {
    config_paths_in(dir)
        .into_iter()
        .filter(|path| path.exists())
        .map(|path| {
            let body = remove_owned_config(&path)?;
            ConfigUpdate::new(path, body)
        })
        .collect()
}

fn config_paths_in(dir: &Path) -> [PathBuf; 2] {
    [dir.join("opencode.json"), dir.join("opencode.jsonc")]
}

fn preview_config_updates(updates: &[ConfigUpdate]) {
    for update in updates {
        let action = if update.current.is_none() {
            "create"
        } else if update.changed() {
            "backup+update"
        } else {
            "unchanged"
        };
        ui::kv(action, update.path.display());
    }
}

fn legacy_oy_paths(dir: &Path) -> Result<Vec<PathBuf>> {
    let mut paths = Vec::new();
    for namespace in ["agents", "commands", "skills"] {
        let parent = dir.join(namespace);
        let metadata = match fs::symlink_metadata(&parent) {
            Ok(metadata) => metadata,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
            Err(error) => {
                return Err(error).with_context(|| format!("failed reading {}", parent.display()));
            }
        };
        if metadata.file_type().is_symlink() {
            bail!(
                "refusing to scan symlinked OpenCode namespace {}",
                parent.display()
            );
        }
        if !metadata.is_dir() {
            bail!(
                "OpenCode namespace is not a directory: {}",
                parent.display()
            );
        }
        let entries = fs::read_dir(&parent)
            .with_context(|| format!("failed reading {}", parent.display()))?;
        for entry in entries {
            let entry = entry?;
            let name = entry.file_name();
            let Some(name) = name.to_str() else {
                continue;
            };
            if name == "oy" || name.starts_with("oy-") || name.starts_with("oy.") {
                paths.push(entry.path());
            }
        }
    }
    paths.sort();
    Ok(paths)
}

fn apply_integration_update(
    dir: &Path,
    old_paths: &[PathBuf],
    updates: &[ConfigUpdate],
) -> Result<Option<PathBuf>> {
    let changed = updates
        .iter()
        .filter(|update| update.changed())
        .collect::<Vec<_>>();
    let existing_configs = changed
        .iter()
        .filter(|update| update.current.is_some())
        .collect::<Vec<_>>();
    let backup = if old_paths.is_empty() && existing_configs.is_empty() {
        None
    } else {
        Some(create_backup_dir()?)
    };

    if let Some(backup) = &backup {
        let result = (|| -> Result<()> {
            for update in existing_configs {
                let relative = update.path.strip_prefix(dir).with_context(|| {
                    format!("{} is outside {}", update.path.display(), dir.display())
                })?;
                let destination = backup.join(relative);
                if let Some(parent) = destination.parent() {
                    fs::create_dir_all(parent)?;
                }
                fs::write(
                    &destination,
                    update.current.as_deref().expect("existing config bytes"),
                )
                .with_context(|| format!("failed backing up {}", update.path.display()))?;
                let permissions = fs::metadata(&update.path)?.permissions();
                fs::set_permissions(&destination, permissions).with_context(|| {
                    format!(
                        "failed preserving permissions for {}",
                        destination.display()
                    )
                })?;
            }
            Ok(())
        })();
        if let Err(error) = result {
            let _ = fs::remove_dir_all(backup);
            return Err(error);
        }
    }

    let mut moved = Vec::new();
    if let Some(backup) = &backup {
        for source in old_paths {
            let relative = source
                .strip_prefix(dir)
                .with_context(|| format!("{} is outside {}", source.display(), dir.display()))?;
            let destination = backup.join(relative);
            if let Some(parent) = destination.parent() {
                fs::create_dir_all(parent)?;
            }
            if let Err(error) = move_path(source, &destination) {
                if let Err(rollback) = restore_moved_paths(&moved) {
                    return Err(error).context(format!(
                        "failed moving {} to {}; rollback also failed: {rollback:#}",
                        source.display(),
                        destination.display()
                    ));
                }
                return Err(error).with_context(|| {
                    format!(
                        "failed moving {} to {}",
                        source.display(),
                        destination.display()
                    )
                });
            }
            moved.push((source.clone(), destination));
        }
    }

    let mutations = changed
        .iter()
        .map(|update| crate::config::FileMutation::Write {
            path: update.path.as_path(),
            bytes: update.body.as_bytes(),
        })
        .collect::<Vec<_>>();
    if let Err(error) = crate::config::apply_file_batch_in(dir, &mutations) {
        if let Err(rollback) = restore_moved_paths(&moved) {
            return Err(error).context(format!(
                "setup rollback failed; backup retained at {}: {rollback:#}",
                backup
                    .as_ref()
                    .map_or_else(|| Path::new("<none>"), PathBuf::as_path)
                    .display()
            ));
        }
        if let Some(backup) = &backup {
            return Err(error).context(format!(
                "config update failed; backup retained at {}",
                backup.display()
            ));
        }
        return Err(error);
    }

    for namespace in ["agents", "commands", "skills"] {
        let _ = fs::remove_dir(dir.join(namespace));
    }
    Ok(backup)
}

fn config_path_in(dir: &Path) -> PathBuf {
    let jsonc = dir.join("opencode.jsonc");
    if jsonc.exists() {
        jsonc
    } else {
        dir.join("opencode.json")
    }
}

pub(super) fn ensure_opencode_integration() -> Result<()> {
    let global = SetupScope::Global.dir()?;
    let workspace = SetupScope::Workspace.dir()?;
    if [&global, &workspace]
        .iter()
        .any(|dir| integration_complete(dir))
    {
        return Ok(());
    }
    if !std::io::stdin().is_terminal() || !std::io::stdout().is_terminal() || ui::is_json() {
        bail!(
            "oy integration is missing or incomplete; run `oy setup` (or `oy setup --workspace`) first"
        );
    }
    let scope = if integration_present(&workspace)? {
        SetupScope::Workspace
    } else {
        SetupScope::Global
    };
    ui::out(&format!(
        "oy is not set up. Set up the {} integration now? [Y/n] ",
        scope.label()
    ));
    std::io::stdout().flush()?;
    let mut answer = String::new();
    if std::io::stdin().read_line(&mut answer)? == 0 {
        bail!("oy setup was cancelled; run `oy setup` when ready");
    }
    if !setup_answer_is_yes(&answer) {
        bail!("oy setup was declined; run `oy setup` when ready");
    }
    setup_opencode(scope, true, false)?;
    if integration_complete(&scope.dir()?) {
        return Ok(());
    }
    bail!("oy setup completed, but the integration is still incomplete; run `oy doctor --check`")
}

fn setup_answer_is_yes(answer: &str) -> bool {
    matches!(
        answer.trim().to_ascii_lowercase().as_str(),
        "" | "y" | "yes"
    )
}

fn integration_present(dir: &Path) -> Result<bool> {
    if !legacy_oy_paths(dir)?.is_empty() {
        return Ok(true);
    }
    Ok(config_paths_in(dir).iter().any(|path| {
        fs::read_to_string(path)
            .ok()
            .and_then(|text| parse_opencode_config(&text).ok())
            .is_some_and(|config| config_has_oy_entries(&config))
    }))
}

fn integration_complete(dir: &Path) -> bool {
    config_has_all_oy_entries(&config_path_in(dir))
}

#[cfg(test)]
mod tests;