yana-rt 1.4.0

Yana AI Runtime — safety CLI for AI agents: scan, graph, vault, hunt, ci, map, fix, doctor
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
//! Cross-platform install/start/stop/restart/status/uninstall for a
//! resident always-on service definition.
//!
//! Platform-specific service-definition rendering lives in `launchd`,
//! `systemd`, and `windows`; this file holds the shared, OS-agnostic
//! orchestration (atomic file writes, external-command invocation) and
//! the public API, following the exact atomic-write/no-symlink-follow
//! discipline already established by `os::monitor_service`.

use anyhow::{bail, Context, Result};
use serde::Serialize;
use sha2::{Digest, Sha256};
use std::env;
use std::fs::{self, OpenOptions};
use std::io::Write;
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Debug, Clone)]
pub struct ServiceDefinition {
    pub name: String,
    pub description: String,
    pub program: PathBuf,
    pub args: Vec<String>,
    pub working_directory: PathBuf,
}

#[derive(Debug, Clone, Serialize)]
pub struct ServiceStatus {
    pub schema_version: u32,
    pub runtime_version: String,
    pub service_id: String,
    pub platform: String,
    pub installed: bool,
    pub registered: Option<bool>,
    pub running: Option<bool>,
    pub definition_paths: Vec<String>,
    pub detail: String,
}

#[derive(Debug, Clone)]
pub(crate) struct RuntimeInspection {
    pub registered: Option<bool>,
    pub running: Option<bool>,
    pub detail: String,
}

/// Everything a platform installer needs to install/uninstall a service
/// definition: where the definition file(s) live, their rendered
/// contents, and the external-command invocations that start/stop it.
pub(crate) struct PlatformPlan {
    pub paths: Vec<PathBuf>,
    pub contents: Vec<String>,
    pub start: Vec<Invocation>,
    pub stop: Vec<Invocation>,
    pub remove: Vec<Invocation>,
}

pub(crate) struct Invocation {
    pub program: String,
    pub args: Vec<String>,
    pub tolerate_failure: bool,
}

pub struct ServiceManager {
    definition: ServiceDefinition,
}

impl ServiceManager {
    pub fn new(definition: ServiceDefinition) -> Self {
        Self { definition }
    }

    pub fn install(&self) -> Result<ServiceStatus> {
        let plan = platform_plan(&self.definition)?;
        let previous = plan
            .paths
            .iter()
            .map(read_definition)
            .collect::<Result<Vec<_>>>()?;
        let installation = (|| -> Result<()> {
            for (path, content) in plan.paths.iter().zip(&plan.contents) {
                write_definition(path, content)?;
            }
            invoke_all(&plan.start)
        })();
        if let Err(error) = installation {
            let _ = invoke_all(&plan.remove);
            restore_definitions(&plan.paths, &previous);
            let _ = refresh_after_remove();
            return Err(error).context("activating resident service; installation rolled back");
        }
        let status = self.status()?;
        if !activation_verified(&status, env::consts::OS) {
            let _ = invoke_all(&plan.remove);
            restore_definitions(&plan.paths, &previous);
            let _ = refresh_after_remove();
            bail!(
                "resident service activation could not be verified; installation rolled back: {}",
                status.detail
            );
        }
        Ok(status)
    }

    pub fn start(&self) -> Result<ServiceStatus> {
        let plan = platform_plan(&self.definition)?;
        if !plan.paths.iter().all(|path| path.is_file()) {
            bail!("service is not installed; run install first");
        }
        invoke_all(&plan.start)?;
        self.status()
    }

    pub fn stop(&self) -> Result<ServiceStatus> {
        let plan = platform_plan(&self.definition)?;
        invoke_all(&plan.stop)?;
        self.status()
    }

    pub fn restart(&self) -> Result<ServiceStatus> {
        self.stop()?;
        self.start()
    }

    pub fn status(&self) -> Result<ServiceStatus> {
        let plan = platform_plan(&self.definition)?;
        let installed = plan.paths.iter().all(|path| path.is_file());
        let runtime = if installed {
            inspect_runtime(&self.definition)
        } else {
            RuntimeInspection {
                registered: Some(false),
                running: Some(false),
                detail: "service definition is absent".into(),
            }
        };
        Ok(ServiceStatus {
            schema_version: 1,
            runtime_version: env!("CARGO_PKG_VERSION").into(),
            service_id: identity(&self.definition),
            platform: env::consts::OS.into(),
            installed,
            registered: runtime.registered,
            running: runtime.running,
            definition_paths: plan
                .paths
                .iter()
                .map(|path| path.display().to_string())
                .collect(),
            detail: runtime.detail,
        })
    }

    pub fn uninstall(&self) -> Result<ServiceStatus> {
        let plan = platform_plan(&self.definition)?;
        invoke_all(&plan.remove)?;
        for path in &plan.paths {
            match fs::remove_file(path) {
                Ok(()) => {}
                Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
                Err(error) => {
                    return Err(error).with_context(|| format!("removing {}", path.display()))
                }
            }
        }
        refresh_after_remove()?;
        Ok(ServiceStatus {
            schema_version: 1,
            runtime_version: env!("CARGO_PKG_VERSION").into(),
            service_id: identity(&self.definition),
            platform: env::consts::OS.into(),
            installed: false,
            registered: Some(false),
            running: Some(false),
            definition_paths: plan
                .paths
                .iter()
                .map(|path| path.display().to_string())
                .collect(),
            detail: "service definitions removed".into(),
        })
    }
}

fn activation_verified(status: &ServiceStatus, platform: &str) -> bool {
    status.installed
        && status.registered == Some(true)
        && (platform == "windows" || status.running == Some(true))
}

pub fn print(status: &ServiceStatus, json: bool) -> Result<()> {
    if json {
        println!("{}", serde_json::to_string_pretty(status)?);
    } else {
        println!("Yana always-on service");
        println!("  schema      {}", status.schema_version);
        println!("  runtime     {}", status.runtime_version);
        println!("  identity    {}", status.service_id);
        println!("  platform    {}", status.platform);
        println!("  installed   {}", status.installed);
        println!(
            "  registered  {}",
            status
                .registered
                .map_or("", |value| if value { "yes" } else { "no" })
        );
        println!(
            "  running     {}",
            status
                .running
                .map_or("", |value| if value { "yes" } else { "no" })
        );
        println!("  detail      {}", status.detail);
        for path in &status.definition_paths {
            println!("  definition  {path}");
        }
    }
    Ok(())
}

/// Stable, project- and service-name-specific identity used to derive
/// definition file/label/task names, matching `os::monitor_service`'s own
/// `project_id` convention.
pub(crate) fn identity(def: &ServiceDefinition) -> String {
    let digest = Sha256::digest(def.working_directory.to_string_lossy().as_bytes());
    let short: String = digest
        .iter()
        .take(5)
        .map(|byte| format!("{byte:02x}"))
        .collect();
    format!("{}-{short}", def.name)
}

pub(crate) fn home() -> Result<PathBuf> {
    env::var_os("HOME")
        .or_else(|| env::var_os("USERPROFILE"))
        .map(PathBuf::from)
        .ok_or_else(|| {
            anyhow::anyhow!("HOME/USERPROFILE is required to install a per-user service")
        })
}

pub(crate) fn xml_escape(value: &str) -> String {
    value
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

#[cfg(any(test, target_os = "linux"))]
pub(crate) fn systemd_escape(value: &Path) -> String {
    format!(
        "\"{}\"",
        value
            .display()
            .to_string()
            .replace('\\', "\\\\")
            .replace('"', "\\\"")
    )
}

#[cfg(target_os = "linux")]
use crate::os::platform::linux::service::plan as platform_plan;
#[cfg(target_os = "macos")]
use crate::os::platform::macos::service::plan as platform_plan;
#[cfg(target_os = "windows")]
use crate::os::platform::windows::service::plan as platform_plan;

#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
fn platform_plan(_def: &ServiceDefinition) -> Result<PlatformPlan> {
    bail!("always-on service management is supported on macOS, Linux, and Windows")
}

#[cfg(target_os = "macos")]
fn inspect_runtime(def: &ServiceDefinition) -> RuntimeInspection {
    crate::os::platform::macos::service::inspect(&identity(def))
}

#[cfg(target_os = "linux")]
fn inspect_runtime(def: &ServiceDefinition) -> RuntimeInspection {
    crate::os::platform::linux::service::inspect(&identity(def))
}

#[cfg(target_os = "windows")]
fn inspect_runtime(def: &ServiceDefinition) -> RuntimeInspection {
    crate::os::platform::windows::service::inspect(&identity(def))
}

#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
fn inspect_runtime(_def: &ServiceDefinition) -> RuntimeInspection {
    RuntimeInspection {
        registered: None,
        running: None,
        detail: "service-manager status is unavailable on this platform".into(),
    }
}

#[cfg(target_os = "linux")]
fn refresh_after_remove() -> Result<()> {
    invoke(&Invocation {
        program: "systemctl".into(),
        args: vec!["--user".into(), "daemon-reload".into()],
        tolerate_failure: true,
    })
}

#[cfg(not(target_os = "linux"))]
fn refresh_after_remove() -> Result<()> {
    Ok(())
}

pub(crate) fn invoke(invocation: &Invocation) -> Result<()> {
    let status = Command::new(&invocation.program)
        .args(&invocation.args)
        .status();
    match status {
        Ok(status) if status.success() || invocation.tolerate_failure => Ok(()),
        Ok(status) => bail!(
            "{} exited with {}",
            invocation.program,
            status.code().unwrap_or(-1)
        ),
        Err(_) if invocation.tolerate_failure => Ok(()),
        Err(error) => Err(error).with_context(|| format!("starting {}", invocation.program)),
    }
}

fn invoke_all(invocations: &[Invocation]) -> Result<()> {
    for invocation in invocations {
        invoke(invocation)?;
    }
    Ok(())
}

fn read_definition(path: &PathBuf) -> Result<Option<Vec<u8>>> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => bail!(
            "refusing non-regular service definition: {}",
            path.display()
        ),
        Ok(_) => fs::read(path)
            .map(Some)
            .with_context(|| format!("reading {}", path.display())),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(error).with_context(|| format!("inspecting {}", path.display())),
    }
}

fn restore_definitions(paths: &[PathBuf], previous: &[Option<Vec<u8>>]) {
    for (path, content) in paths.iter().zip(previous) {
        match content {
            Some(bytes) => {
                let _ = write_definition(path, &String::from_utf8_lossy(bytes));
            }
            None => {
                let _ = fs::remove_file(path);
            }
        }
    }
}

fn write_definition(path: &Path, content: &str) -> Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => bail!(
            "refusing to replace non-regular service definition: {}",
            path.display()
        ),
        Ok(_) => {}
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => return Err(error).with_context(|| format!("inspecting {}", path.display())),
    }
    let nonce = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    let temporary = path.with_extension(format!("tmp.{}.{}", std::process::id(), nonce));
    let mut options = OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    options.mode(0o600).custom_flags(libc::O_NOFOLLOW);
    let mut file = options.open(&temporary)?;
    file.write_all(content.as_bytes())?;
    file.sync_all()?;
    let result = (|| -> Result<()> {
        #[cfg(target_os = "windows")]
        if path.exists() {
            fs::remove_file(path)?;
        }
        fs::rename(&temporary, path)?;
        Ok(())
    })();
    if result.is_err() {
        let _ = fs::remove_file(&temporary);
    }
    result
}

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

    fn definition() -> ServiceDefinition {
        ServiceDefinition {
            name: "yana-rt-service".into(),
            description: "Yana always-on service (test)".into(),
            program: PathBuf::from("/usr/local/bin/yana-rt"),
            args: vec!["os".into(), "service".into(), "run".into()],
            working_directory: PathBuf::from("/tmp/some project & one"),
        }
    }

    #[test]
    fn identity_is_stable_and_definition_specific() {
        let a = identity(&definition());
        let b = identity(&definition());
        assert_eq!(a, b);
        let mut other = definition();
        other.working_directory = PathBuf::from("/tmp/other");
        assert_ne!(a, identity(&other));
    }

    #[test]
    fn xml_escape_neutralizes_metacharacters() {
        assert_eq!(xml_escape("a & b < c"), "a &amp; b &lt; c");
    }

    #[test]
    fn definition_write_is_atomic_and_replaces_regular_content() {
        let root = std::env::temp_dir().join(format!("yana-service-write-{}", Uuid::new_v4()));
        let path = root.join("service definition.conf");
        write_definition(&path, "first").unwrap();
        write_definition(&path, "second").unwrap();
        assert_eq!(fs::read_to_string(&path).unwrap(), "second");
        assert_eq!(
            fs::read_dir(&root).unwrap().count(),
            1,
            "temporary definition must not remain"
        );
        fs::remove_dir_all(root).unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn definition_write_refuses_symlink_replacement() {
        use std::os::unix::fs::symlink;

        let root = std::env::temp_dir().join(format!("yana-service-link-{}", Uuid::new_v4()));
        fs::create_dir_all(&root).unwrap();
        let target = root.join("target");
        let path = root.join("service.conf");
        fs::write(&target, "do not touch").unwrap();
        symlink(&target, &path).unwrap();
        assert!(write_definition(&path, "replacement").is_err());
        assert_eq!(fs::read_to_string(target).unwrap(), "do not touch");
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn activation_never_treats_definition_only_or_unknown_as_healthy() {
        let mut status = ServiceStatus {
            schema_version: 1,
            runtime_version: "test".into(),
            service_id: "test".into(),
            platform: "linux".into(),
            installed: true,
            registered: Some(false),
            running: Some(false),
            definition_paths: vec![],
            detail: "definition only".into(),
        };
        assert!(!activation_verified(&status, "linux"));
        status.registered = Some(true);
        status.running = None;
        assert!(!activation_verified(&status, "linux"));
        status.running = Some(true);
        assert!(activation_verified(&status, "linux"));
        status.running = None;
        assert!(activation_verified(&status, "windows"));
    }
}