Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore: files changed crates/openhuman-core/src/skills/bundled/mod.rs,crates/openhuman-core/src/skill

Steven Enamakel48e2d2e943ed
diff --git a/crates/openhuman-core/src/skills/bundled/flow_authoring.rs b/crates/openhuman-core/src/skills/bundled/flow_authoring.rs
new file mode 100644
index 0000000000..deb5e2aaf1
--- /dev/null
+++ b/crates/openhuman-core/src/skills/bundled/flow_authoring.rs
@@ -0,0 +1,301 @@
+//! Bus-backed materialization for the flow-authoring builtin skill.
+//!
+//! TinyFlows owns the resource bytes. This adapter asks the published Copilot
+//! interface for its current files, lets TinySkills own their safe atomic
+//! materialization, and keeps a process snapshot so synchronous discovery can
+//! verify the disk tree without reaching across the async bus boundary.
+
+use std::collections::{BTreeSet, HashMap};
+use std::path::{Path, PathBuf};
+use std::sync::atomic::{AtomicU64, Ordering};
+use std::sync::{LazyLock, RwLock};
+use std::time::Duration;
+
+use crate::config::Config;
+use crate::core::runtime::{DispatchCtx, DomainSet};
+use tinyskills::{MaterializeEntry, MaterializeFile, MaterializeSource, Skill, SkillScope};
+use tinyflows_bus::copilot::ResourceFile;
+
+use super::super::ops_discover::api::DiscoveryGate;
+
+const FLOW_SKILL_ID: &str = "flow-authoring";
+const FLOW_ROOT_NAME: &str = "builtin-flow-skills";
+const COPILOT_RESOURCE_DEADLINE: Duration = Duration::from_secs(60);
+
+#[derive(Clone, Debug)]
+struct Snapshot {
+ generation: u64,
+ files: Vec<ResourceFile>,
+}
+
+static SNAPSHOTS: LazyLock<RwLock<HashMap<PathBuf, Snapshot>>> =
+ LazyLock::new(|| RwLock::new(HashMap::new()));
+static NEXT_GENERATION: AtomicU64 = AtomicU64::new(1);
+
+/// Product-owned root for the dynamic skill, separate from static bundles.
+pub(crate) fn root(workspace: &Path) -> PathBuf {
+ workspace.join(".openhuman").join(FLOW_ROOT_NAME)
+}
+
+/// Fetch and materialize Copilot resources when every owning capability is
+/// enabled. Failures clear the snapshot and return normally so optional module
+/// availability never blocks ordinary chat startup.
+pub(crate) async fn prewarm(
+ ctx: &DispatchCtx,
+ config: &Config,
+ domains: Option<DomainSet>,
+) {
+ let gate = DiscoveryGate::from_config(ctx, config, domains);
+ if !gate.flow_authoring_enabled() {
+ tracing::debug!(
+ skills = config.components.skills,
+ flows = config.components.flows,
+ tinyflows = config.components.tinyflows,
+ "[skills][bundled] flow-authoring prewarm skipped by capability gates"
+ );
+ return;
+ }
+
+ let workspace = config.workspace_dir.as_path();
+ let fetched = tokio::time::timeout(COPILOT_RESOURCE_DEADLINE, async {
+ let proxy = crate::modules::tinyflows::copilot_proxy(config).await?;
+ let member = tinyflows_bus::copilot::METHODS
+ .iter()
+ .copied()
+ .find(|member| *member == "ListResources")
+ .ok_or_else(|| "TinyFlows Copilot contract has no ListResources member".to_string())?;
+ proxy
+ .call::<Vec<ResourceFile>>(member, ())
+ .await
+ .map_err(|error| error.wire_message())
+ })
+ .await;
+
+ let resources = match fetched {
+ Ok(Ok(files)) => files,
+ Ok(Err(error)) => {
+ tracing::warn!(%error, "[skills][bundled] flow-authoring resources unavailable; hiding builtin");
+ clear_snapshot(workspace);
+ return;
+ }
+ Err(_) => {
+ tracing::warn!(
+ timeout_seconds = COPILOT_RESOURCE_DEADLINE.as_secs(),
+ "[skills][bundled] flow-authoring resource prewarm timed out; hiding builtin"
+ );
+ clear_snapshot(workspace);
+ return;
+ }
+ };
+
+ if !valid_resource_manifest(&resources) {
+ tracing::warn!("[skills][bundled] TinyFlows returned an invalid resource manifest; hiding builtin");
+ clear_snapshot(workspace);
+ return;
+ }
+ if !module_is_serving() {
+ tracing::warn!("[skills][bundled] TinyFlows module is not serving after ListResources; hiding builtin");
+ clear_snapshot(workspace);
+ return;
+ }
+
+ let previous = SNAPSHOTS
+ .read()
+ .ok()
+ .and_then(|snapshots| snapshots.get(workspace).cloned());
+ if let Some(previous) = &previous {
+ if !materialization_matches(workspace, previous) {
+ tracing::warn!("[skills][bundled] flow-authoring disk tree changed; hiding builtin");
+ clear_snapshot(workspace);
+ return;
+ }
+ if previous.files == resources {
+ return;
+ }
+ }
+
+ let generation = NEXT_GENERATION.fetch_add(1, Ordering::SeqCst);
+ if let Err(error) = materialize(workspace, &resources) {
+ tracing::warn!(%error, "[skills][bundled] flow-authoring materialization failed; hiding builtin");
+ clear_snapshot(workspace);
+ return;
+ }
+ if let Ok(mut snapshots) = SNAPSHOTS.write() {
+ snapshots.insert(
+ workspace.to_path_buf(),
+ Snapshot {
+ generation,
+ files: resources,
+ },
+ );
+ }
+ crate::skills::ops_discover::invalidate_workflow_metadata_cache();
+}
+
+/// Return the current resource generation only while the owning module and
+/// TinySkills-validated on-disk tree still agree.
+pub(crate) fn generation_if_current(workspace: &Path) -> Option<u64> {
+ if !module_is_serving() {
+ clear_snapshot(workspace);
+ return None;
+ }
+ let snapshot = SNAPSHOTS
+ .read()
+ .ok()
+ .and_then(|snapshots| snapshots.get(workspace).cloned())?;
+ if !materialization_matches(workspace, &snapshot) {
+ tracing::warn!("[skills][bundled] cached flow-authoring materialization changed; hiding builtin");
+ clear_snapshot(workspace);
+ return None;
+ }
+ Some(snapshot.generation)
+}
+
+/// Load the dynamic skill for synchronous discovery after the metadata path
+/// has checked the expected snapshot generation.
+pub(crate) fn materialized_skill(workspace: &Path, generation: u64) -> Option<Skill> {
+ let snapshot = snapshot_for(workspace, generation)?;
+ if !module_is_serving() || !materialization_matches(workspace, &snapshot) {
+ clear_snapshot(workspace);
+ return None;
+ }
+ let skill = tinyskills::load_skill_dir(
+ &root(workspace).join(FLOW_SKILL_ID),
+ SkillScope::Builtin,
+ )?;
+ (skill.id() == FLOW_SKILL_ID).then_some(skill)
+}
+
+/// Read one resource only when the module lifecycle, complete resource
+/// inventory, and returned bytes all still match the retained bus snapshot.
+pub(crate) fn read_resource(
+ workspace: &Path,
+ generation: u64,
+ skill: &Skill,
+ relative_path: &Path,
+) -> Result<String, String> {
+ let snapshot = snapshot_for(workspace, generation)
+ .ok_or_else(|| "flow-authoring resources are unavailable".to_string())?;
+ if !module_is_serving() || !materialization_matches(workspace, &snapshot) {
+ clear_snapshot(workspace);
+ return Err("flow-authoring resources are stale or changed".to_string());
+ }
+ let requested = relative_path
+ .to_str()
+ .ok_or_else(|| "resource path is not UTF-8".to_string())?;
+ let expected = snapshot
+ .files
+ .iter()
+ .find(|file| file.path == requested)
+ .map(|file| file.contents.as_str())
+ .ok_or_else(|| "resource is not present in the current TinyFlows snapshot".to_string())?;
+ let content = tinyskills::read_resource(skill, relative_path)
+ .map_err(|error| error.to_string())?;
+ if content != expected {
+ clear_snapshot(workspace);
+ return Err("flow-authoring resource bytes changed".to_string());
+ }
+ Ok(content)
+}
+
+fn snapshot_for(workspace: &Path, generation: u64) -> Option<Snapshot> {
+ SNAPSHOTS
+ .read()
+ .ok()?
+ .get(workspace)
+ .filter(|snapshot| snapshot.generation == generation)
+ .cloned()
+}
+
+fn module_is_serving() -> bool {
+ crate::modules::ops::state_of(crate::modules::tinyflows::MODULE_ID)
+ == crate::modules::types::ModuleState::Ready
+ && crate::modules::runtime::is_module_serving(crate::modules::tinyflows::MODULE_ID)
+}
+
+fn valid_resource_manifest(files: &[ResourceFile]) -> bool {
+ if files.is_empty() {
+ return false;
+ }
+ let mut paths = BTreeSet::new();
+ if files.iter().any(|file| !paths.insert(file.path.as_str())) {
+ return false;
+ }
+ files.iter().any(|file| {
+ file.path == tinyskills::WORKFLOW_MD || file.path == tinyskills::SKILL_MD
+ })
+}
+
+fn materialize(workspace: &Path, resources: &[ResourceFile]) -> Result<(), String> {
+ let parent_path = workspace.join(".openhuman");
+ std::fs::create_dir_all(&parent_path)
+ .map_err(|error| format!("creating builtin skill parent failed: {error}"))?;
+ let parent = tinyskills::cap_std::fs::Dir::open_ambient_dir(
+ &parent_path,
+ tinyskills::cap_std::ambient_authority(),
+ )
+ .map_err(|error| format!("opening builtin skill parent failed: {error}"))?;
+ let files = resources
+ .iter()
+ .map(|file| MaterializeFile::new(file.path.clone(), file.contents.as_bytes().to_vec()))
+ .collect();
+ let entry = MaterializeEntry {
+ dir_name: FLOW_SKILL_ID.to_string(),
+ source: MaterializeSource::Files(files),
+ };
+ tinyskills::materialize_tree(&parent, FLOW_ROOT_NAME, &[entry])
+ .map(|_| ())
+ .map_err(|error| error.to_string())
+}
+
+fn materialization_matches(workspace: &Path, snapshot: &Snapshot) -> bool {
+ let directory = root(workspace).join(FLOW_SKILL_ID);
+ let Some(skill) = tinyskills::load_skill_dir(&directory, SkillScope::Builtin) else {
+ return false;
+ };
+ if skill.id() != FLOW_SKILL_ID || skill.scope != SkillScope::Builtin {
+ return false;
+ }
+
+ let Some(manifest) = snapshot.files.iter().find(|file| {
+ file.path == tinyskills::WORKFLOW_MD || file.path == tinyskills::SKILL_MD
+ }) else {
+ return false;
+ };
+ let Some(location) = skill.location.as_deref() else {
+ return false;
+ };
+ if tinyskills::read_document(location).ok().as_deref() != Some(manifest.contents.as_str()) {
+ return false;
+ }
+
+ let expected_resources: Vec<PathBuf> = snapshot
+ .files
+ .iter()
+ .filter(|file| file.path != manifest.path)
+ .map(|file| PathBuf::from(&file.path))
+ .collect();
+ let mut actual_resources = skill.resources.clone();
+ actual_resources.sort();
+ if actual_resources != expected_resources {
+ return false;
+ }
+ expected_resources.iter().all(|path| {
+ let Some(expected) = snapshot
+ .files
+ .iter()
+ .find(|file| PathBuf::from(&file.path) == *path)
+ .map(|file| file.contents.as_str())
+ else {
+ return false;
+ };
+ tinyskills::read_resource(&skill, path).ok().as_deref() == Some(expected)
+ })
+}
+
+fn clear_snapshot(workspace: &Path) {
+ if let Ok(mut snapshots) = SNAPSHOTS.write() {
+ snapshots.remove(workspace);
+ }
+ crate::skills::ops_discover::invalidate_workflow_metadata_cache();
+}
diff --git a/crates/openhuman-core/src/skills/bundled/mod.rs b/crates/openhuman-core/src/skills/bundled/mod.rs
index d32824ada2..586f997dab 100644
--- a/crates/openhuman-core/src/skills/bundled/mod.rs
+++ b/crates/openhuman-core/src/skills/bundled/mod.rs
@@ -4,6 +4,9 @@ use std::path::{Path, PathBuf};
use tinyskills::{BundledFile, BundledSkill, InstallReport};
+#[cfg(feature = "flows")]
+pub(crate) mod flow_authoring;
+
#[cfg(test)]
#[path = "mod_tests.rs"]
mod mod_tests;
@@ -18,8 +21,6 @@ pub const BUNDLED: &[BundledSkill] = &[
contents: include_str!("../../desktop/control/WORKFLOW.md"),
}],
},
- #[cfg(feature = "flows")]
- crate::flows::skills::FLOW_AUTHORING,
];
/// Product-owned root for materialized builtin skills.
diff --git a/crates/openhuman-core/src/skills/ops.rs b/crates/openhuman-core/src/skills/ops.rs
index 4d2a3d9b05..3493d8c35f 100644
--- a/crates/openhuman-core/src/skills/ops.rs
+++ b/crates/openhuman-core/src/skills/ops.rs
@@ -33,7 +33,8 @@ pub use super::bundled::install_bundled_skills;
pub use super::ops_create::{create_workflow, CreateWorkflowParams, WorkflowCreateInputDef};
pub use super::ops_discover::{
discover_automations, discover_workflows, init_workflows_dir, is_workspace_trusted,
- load_workflow_metadata, read_workflow_resource,
+ load_workflow_metadata, load_workflow_metadata_for, read_workflow_resource,
+ read_workflow_resource_for,
};
pub use super::ops_install::{
install_workflow_from_url, uninstall_workflow, validate_install_url, validate_resolved_host,
diff --git a/crates/openhuman-core/src/skills/ops_discover.rs b/crates/openhuman-core/src/skills/ops_discover.rs
index b138d78bab..cbf7b19001 100644
--- a/crates/openhuman-core/src/skills/ops_discover.rs
+++ b/crates/openhuman-core/src/skills/ops_discover.rs
@@ -20,9 +20,9 @@ use std::path::Path;
pub use api::{
discover_automations, discover_workflows, discovery_home_dir, init_workflows_dir,
- is_workspace_trusted, load_workflow_metadata,
+ is_workspace_trusted, load_workflow_metadata, load_workflow_metadata_for,
};
-pub use resource::read_workflow_resource;
+pub use resource::{read_workflow_resource, read_workflow_resource_for};
pub(crate) use api::discover_workflows_inner;
pub(crate) use api::invalidate_workflow_metadata_cache;
diff --git a/crates/openhuman-core/src/skills/ops_discover/api.rs b/crates/openhuman-core/src/skills/ops_discover/api.rs
index 66618d782f..26a5bc70c0 100644
--- a/crates/openhuman-core/src/skills/ops_discover/api.rs
+++ b/crates/openhuman-core/src/skills/ops_discover/api.rs
@@ -24,7 +24,7 @@ pub(crate) struct DiscoveryGate {
}
impl DiscoveryGate {
- fn from_dispatch(ctx: &crate::core::runtime::DispatchCtx) -> Self {
+ pub(crate) fn from_dispatch(ctx: &crate::core::runtime::DispatchCtx) -> Self {
let components = ctx.components();
let domains = ctx.domains();
Self {
@@ -37,7 +37,7 @@ impl DiscoveryGate {
}
}
- fn from_config(
+ pub(crate) fn from_config(
ctx: &crate::core::runtime::DispatchCtx,
config: &crate::config::Config,
domains: Option<crate::core::runtime::DomainSet>,
@@ -178,10 +178,11 @@ pub(crate) fn load_workflow_metadata_for_domains(
config: &crate::config::Config,
domains: Option<crate::core::runtime::DomainSet>,
) -> Vec<Workflow> {
- let home = ctx
- .resolved()
- .filter(|core| core.user_skill_roots())
- .and_then(|_| dirs::home_dir());
+ let home = match ctx.resolved() {
+ Some(core) if core.user_skill_roots() => dirs::home_dir(),
+ Some(_) => None,
+ None => discovery_home_dir(),
+ };
load_workflow_metadata_with_gate(
&config.workspace_dir,
home,
@@ -189,7 +190,7 @@ pub(crate) fn load_workflow_metadata_for_domains(
)
}
-fn load_workflow_metadata_with_gate(
+pub(super) fn load_workflow_metadata_with_gate(
workspace_dir: &Path,
home: Option<PathBuf>,
gate: DiscoveryGate,
@@ -217,7 +218,7 @@ fn load_workflow_metadata_with_gate(
}
}
let generation = METADATA_GENERATION.load(Ordering::SeqCst);
- let workflows = discover_workflows_inner(
+ let workflows = discover_workflows_inner_with_gate(
home.as_deref(),
Some(workspace_dir),
trusted,
@@ -253,7 +254,7 @@ pub fn discover_workflows(
let generation = workspace_dir
.filter(|_| gate.flow_authoring_enabled())
.and_then(crate::skills::bundled::flow_authoring::generation_if_current);
- discover_workflows_inner(home_dir, workspace_dir, trusted, &gate, generation)
+ discover_workflows_inner_with_gate(home_dir, workspace_dir, trusted, &gate, generation)
}
#[cfg(test)]
@@ -277,6 +278,18 @@ pub(crate) fn discover_workflows_inner(
home_dir: Option<&Path>,
workspace_dir: Option<&Path>,
trusted: bool,
+) -> Vec<Workflow> {
+ let gate = DiscoveryGate::from_dispatch(&crate::core::runtime::DispatchCtx::ambient());
+ let generation = workspace_dir
+ .filter(|_| gate.flow_authoring_enabled())
+ .and_then(crate::skills::bundled::flow_authoring::generation_if_current);
+ discover_workflows_inner_with_gate(home_dir, workspace_dir, trusted, &gate, generation)
+}
+
+pub(crate) fn discover_workflows_inner_with_gate(
+ home_dir: Option<&Path>,
+ workspace_dir: Option<&Path>,
+ trusted: bool,
gate: &DiscoveryGate,
flow_generation: Option<u64>,
) -> Vec<Workflow> {
@@ -308,5 +321,13 @@ pub fn discover_automations(
has_workspace = workspace_dir.is_some(),
"[workflows] discover:automations:enter"
);
- discover_filtered(home_dir, workspace_dir, trusted, WORKFLOW_ROOT_KINDS)
+ let gate = DiscoveryGate::from_dispatch(&crate::core::runtime::DispatchCtx::ambient());
+ discover_filtered(
+ home_dir,
+ workspace_dir,
+ trusted,
+ WORKFLOW_ROOT_KINDS,
+ &gate,
+ None,
+ )
}
diff --git a/crates/openhuman-core/src/skills/ops_discover/resource.rs b/crates/openhuman-core/src/skills/ops_discover/resource.rs
index c362d97bf6..de202ff103 100644
--- a/crates/openhuman-core/src/skills/ops_discover/resource.rs
+++ b/crates/openhuman-core/src/skills/ops_discover/resource.rs
@@ -1,16 +1,77 @@
//! OpenHuman discovery context around `tinyskills` resource reads.
-use super::api::load_workflow_metadata;
+use super::api::{DiscoveryGate, load_workflow_metadata_with_gate};
use std::path::Path;
pub fn read_workflow_resource(
workspace_dir: &Path,
skill_id: &str,
relative_path: &Path,
+) -> Result<String, String> {
+ let ctx = crate::core::runtime::DispatchCtx::ambient();
+ read_workflow_resource_with_gate(
+ workspace_dir,
+ skill_id,
+ relative_path,
+ &ctx,
+ DiscoveryGate::from_dispatch(&ctx),
+ super::api::discovery_home_dir(),
+ )
+}
+
+/// Read a skill resource under the caller's explicit Config and dispatch
+/// identity. Host adapters that already own these inputs use this path so a
+/// workspace shared by several profiles cannot reuse another profile's view.
+pub fn read_workflow_resource_for(
+ ctx: &crate::core::runtime::DispatchCtx,
+ config: &crate::config::Config,
+ skill_id: &str,
+ relative_path: &Path,
+) -> Result<String, String> {
+ let home = match ctx.resolved() {
+ Some(core) if core.user_skill_roots() => dirs::home_dir(),
+ Some(_) => None,
+ None => super::api::discovery_home_dir(),
+ };
+ read_workflow_resource_with_gate(
+ &config.workspace_dir,
+ skill_id,
+ relative_path,
+ ctx,
+ DiscoveryGate::from_config(ctx, config, None),
+ home,
+ )
+}
+
+fn read_workflow_resource_with_gate(
+ workspace_dir: &Path,
+ skill_id: &str,
+ relative_path: &Path,
+ ctx: &crate::core::runtime::DispatchCtx,
+ gate: DiscoveryGate,
+ home: Option<std::path::PathBuf>,
) -> Result<String, String> {
if skill_id.trim().is_empty() {
return Err("skill_id must not be empty".to_string());
}
- let skill = tinyskills::resolve_skill(load_workflow_metadata(workspace_dir), skill_id)?;
+ let skills = load_workflow_metadata_with_gate(workspace_dir, home, gate.clone());
+ let skill = tinyskills::resolve_skill(skills, skill_id)?;
+ #[cfg(feature = "flows")]
+ if skill.scope == tinyskills::SkillScope::Builtin && skill.id() == "flow-authoring" {
+ if !gate.flow_authoring_enabled() {
+ return Err("flow-authoring is unavailable in this context".to_string());
+ }
+ let generation = crate::skills::bundled::flow_authoring::generation_if_current(
+ workspace_dir,
+ )
+ .ok_or_else(|| "flow-authoring resources are unavailable".to_string())?;
+ return crate::skills::bundled::flow_authoring::read_resource(
+ workspace_dir,
+ generation,
+ &skill,
+ relative_path,
+ );
+ }
+ let _ = ctx;
tinyskills::read_resource(&skill, relative_path).map_err(|error| error.to_string())
}
diff --git a/crates/openhuman-core/src/skills/ops_discover/scan.rs b/crates/openhuman-core/src/skills/ops_discover/scan.rs
index 5043063987..c71518ae1a 100644
--- a/crates/openhuman-core/src/skills/ops_discover/scan.rs
+++ b/crates/openhuman-core/src/skills/ops_discover/scan.rs
@@ -9,6 +9,8 @@ use std::path::Path;
use tinyskills::{discover_layout, LayoutQuery, RootKind};
+use super::api::DiscoveryGate;
+
pub(super) use tinyskills::{ALL_ROOT_KINDS, WORKFLOW_ROOT_KINDS};
use {tinyskills::Skill as Workflow, tinyskills::SkillScope as WorkflowScope};
@@ -22,7 +24,11 @@ pub(super) fn discover_filtered(
workspace_dir: Option<&Path>,
trusted: bool,
kinds: &[RootKind],
+ gate: &DiscoveryGate,
+ flow_generation: Option<u64>,
) -> Vec<Workflow> {
+ #[cfg(not(feature = "flows"))]
+ let _ = (gate, flow_generation);
tracing::debug!(
trusted,
has_home = home_dir.is_some(),
@@ -36,12 +42,21 @@ pub(super) fn discover_filtered(
// first and at the lowest precedence. No trust marker is consulted: the
// directory is core-managed and its contents were written from constants
// compiled into this binary. See `skills::bundled`.
- let builtin = match workspace_dir {
+ let mut builtin = match workspace_dir {
Some(ws) if kinds.contains(&RootKind::Skill) => {
scan_bundled_root(&crate::skills::bundled::builtin_root(ws))
}
_ => Vec::new(),
};
+ #[cfg(feature = "flows")]
+ if gate.flow_authoring_enabled()
+ && kinds.contains(&RootKind::Skill)
+ && let (Some(workspace), Some(generation)) = (workspace_dir, flow_generation)
+ && let Some(flow_authoring) =
+ crate::skills::bundled::flow_authoring::materialized_skill(workspace, generation)
+ {
+ builtin.push(flow_authoring);
+ }
let extra_user_roots = workspace_dir
.and_then(crate::skills::write_root::agent_skill_home)