pub mod catalog;
#[cfg(test)]
mod copy_tests;
mod creation;
mod dependencies;
mod exchange;
mod hermes_bridge;
mod layout;
mod lifecycle;
mod migration;
mod privileged;
mod registration;
mod relocation;
mod storage;
#[cfg(test)]
mod tests;
mod update;
pub(super) use exchange::normalize;
pub use exchange::{
export_package, import_definition, import_package, preview_package, AutomationPackage,
};
#[cfg(any(feature = "cloud-sync", test))]
pub(crate) use exchange::{read_backup_directory, write_backup_directory};
pub(crate) use layout::{manifest_directory, manifest_path};
pub(super) use lifecycle::{prepare_at, remove};
pub use migration::migrate;
pub(super) use storage::fingerprint as storage_fingerprint;
pub(super) use storage::{discover, read, regular_directory, write_receipt};
pub(super) use update::save;
pub fn read_definition(path: &std::path::Path) -> Result<AutomationDraft, String> {
storage::json(path)
}
use super::{definition::valid_id, store, AutomationDraft, ManagedAutomation};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum Dependency {
Command { name: String },
Library { name: String },
Python { packages: Vec<String> },
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct HermesScripts {
pub script: Option<String>,
pub monitor_script: Option<String>,
#[serde(default)]
pub no_agent: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct PackageOptions {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(default)]
pub dependencies: Vec<Dependency>,
pub hermes: Option<HermesScripts>,
}
pub(super) fn validate_options(options: &PackageOptions) -> Result<(), String> {
validate_version(options.version.as_deref())?;
dependencies::validate(&options.dependencies)
}
pub(super) fn validate_version(version: Option<&str>) -> Result<(), String> {
if let Some(version) = version {
if version.len() > 128
|| version.trim() != version
|| semver::Version::parse(version).is_err()
{
return Err("Task version must be a SemVer value such as 1.0.0".into());
}
}
Ok(())
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct Manifest {
#[serde(default, skip_serializing_if = "Option::is_none")]
version: Option<String>,
schema_version: u32,
#[serde(default)]
instance_id: String,
shell: String,
draft: AutomationDraft,
files: Vec<String>,
#[serde(default)]
dependencies: Vec<Dependency>,
#[serde(default, skip_serializing_if = "Option::is_none")]
hermes: Option<HermesScripts>,
}
#[derive(Clone, Serialize, Deserialize)]
pub(super) struct Receipt {
pub revision: u64,
pub source: String,
pub identifier: String,
pub location: String,
#[serde(deserialize_with = "read_runtime_state")]
pub state: String,
pub error: Option<String>,
pub fingerprint: String,
pub pending_backup: bool,
#[serde(default)]
pub removal_pending: bool,
#[serde(default)]
pub archived: bool,
}
#[derive(Clone, Serialize, Deserialize)]
pub(super) struct Binding {
pub workdir: Option<String>,
}
#[derive(Clone, Serialize, Deserialize)]
pub(super) struct UpdateSnapshot {
pub record: ManagedAutomation,
pub receipt: Receipt,
pub manifest: Manifest,
}
pub(super) fn root(state: &Path) -> PathBuf {
#[cfg(test)]
{
state.join("workspaces")
}
#[cfg(not(test))]
{
if state == store::root() {
super::workspace::workspace_root()
} else {
state.join("workspaces")
}
}
}
pub(super) fn directory(record: &ManagedAutomation) -> Option<PathBuf> {
let path = PathBuf::from(record.draft.script_dir.as_ref()?);
let identity = layout::read_manifest(&path)
.map(|m| m.instance_id)
.or_else(|_| {
storage::json::<UpdateSnapshot>(&path.join(".local/operation.json"))
.map(|s| s.record.id)
})
.ok()?;
(valid_id(&record.id) && identity == record.id).then_some(path)
}
pub(super) fn checked(state: &Path, record: &ManagedAutomation) -> Result<PathBuf, String> {
let path = directory(record).ok_or("Missing or invalid task package")?;
let root = root(state);
if path.parent() != Some(root.as_path()) {
return Err("Task package is outside its managed root".into());
}
crate::core::path_guard::ensure_dir_within_root(&path, &root).map_err(|e| e.to_string())?;
storage::regular_directory(&path)?;
storage::regular_directory(&path.join(".local"))?;
super::managed::verify(state, record)?;
Ok(path)
}
pub(super) fn receipt(record: &ManagedAutomation) -> Result<Receipt, String> {
storage::json(
&directory(record)
.ok_or("Missing task package")?
.join(".local/receipt.json"),
)
}
pub(super) fn lock(state: &Path, id: &str) -> Result<store::Lock, String> {
if !valid_id(id) {
return Err("Invalid task instance ID".into());
}
store::Lock::acquire(
&state
.join("operations")
.join(id.trim_start_matches("agent_hub:")),
)
}
pub(super) fn find(state: &Path, id: &str) -> Result<ManagedAutomation, String> {
discover(state)
.0
.into_iter()
.find(|record| record.id == id)
.ok_or_else(|| "Task package not found or damaged; inspect the automation list".into())
}
pub(super) fn json_write(path: &Path, value: &impl Serialize) -> Result<(), String> {
store::write(
path,
&serde_json::to_vec_pretty(value).map_err(|e| e.to_string())?,
)
}
pub(super) fn state(record: &ManagedAutomation) -> Option<String> {
directory(record)?;
Some(
receipt(record)
.map(|r| r.state)
.unwrap_or_else(|_| "prepare_failed".into()),
)
}
#[cfg(any(feature = "cloud-sync", test))]
pub(super) fn is_package_draft(draft: &AutomationDraft) -> bool {
draft
.script_dir
.as_ref()
.is_some_and(|path| manifest_path(Path::new(path)).is_file())
}
fn read_runtime_state<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<String, D::Error> {
let state = String::deserialize(deserializer)?;
Ok(match state.as_str() {
"installed" => "ready".into(),
"uninstalled" => "legacy_uninstalled".into(),
"installing" => "preparing".into(),
"install_failed" => "prepare_failed".into(),
"uninstalling" => "removing".into(),
"uninstall_failed" => "remove_failed".into(),
_ => state,
})
}