Skip to content

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

feat(modules): add tinyjuice storage import module

Steven Enamakel0e8c569277b0
diff --git a/crates/openhuman-core/src/modules/import_once_tests.rs b/crates/openhuman-core/src/modules/import_once_tests.rs
index 98aa652889..0a20604e47 100644
--- a/crates/openhuman-core/src/modules/import_once_tests.rs
+++ b/crates/openhuman-core/src/modules/import_once_tests.rs
@@ -23,7 +23,8 @@ async fn a_concurrent_caller_waits_for_the_running_import() {
let second = async {
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
import_once("test-concurrent", MODULE_FOR_TEST, &dir, async {
- // Only reached if the first import did not settle.
+ panic!("a settled import must not run a second time");
+ #[allow(unreachable_code)]
Ok(Done(true))
})
.await;
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index f715e67d4f..9d92f25e3b 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -260,10 +260,12 @@ pub async fn import_legacy_connectors(state_dir: &std::path::Path) {
/// Imports the x402 ledger's legacy `payments.jsonl` under `workspace_dir`
/// into the wallet module's storage, once per workspace. Never fails the
-/// caller: an error is logged and the import is retried at the next call.
-pub async fn import_legacy_x402(workspace_dir: &std::path::Path) {
+/// caller: an error is logged and the import is retried at the next call. Returns
+/// false when the import failed, so the caller can refuse to open a ledger that
+/// would ignore the legacy payments.
+pub async fn import_legacy_x402(workspace_dir: &std::path::Path) -> bool {
#[cfg(all(feature = "modules", feature = "web3"))]
- import_once(
+ return import_once(
"x402",
storage_grant_tinywallet::MODULE_ID,
workspace_dir,
@@ -271,7 +273,10 @@ pub async fn import_legacy_x402(workspace_dir: &std::path::Path) {
)
.await;
#[cfg(not(all(feature = "modules", feature = "web3")))]
- let _ = workspace_dir;
+ {
+ let _ = workspace_dir;
+ true
+ }
}
/// Imports the legacy jail registry index under `<workspace_dir>/jails` into
diff --git a/crates/openhuman-core/src/modules/storage_import_tinyjuice.rs b/crates/openhuman-core/src/modules/storage_import_tinyjuice.rs
index 5cbfdf9252..e24dd85848 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinyjuice.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinyjuice.rs
@@ -150,6 +150,48 @@ fn is_token(name: &str) -> bool {
name.len() == TOKEN_LEN && name.bytes().all(|byte| byte.is_ascii_hexdigit())
}
+/// The modified time and bytes of a regular file, or `None` for a link,
+/// directory or anything else; the file is never reached through a symlink.
+fn read_regular(path: &Path) -> Result<Option<(SystemTime, Vec<u8>)>> {
+ use std::io::Read;
+ let mut options = std::fs::OpenOptions::new();
+ options.read(true);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::OpenOptionsExt;
+ options.custom_flags(libc::O_NOFOLLOW);
+ }
+ let mut file = match options.open(path) {
+ Ok(file) => file,
+ // O_NOFOLLOW refuses a symlink with ELOOP.
+ Err(error) if is_link_refusal(&error) => return Ok(None),
+ Err(error) => return Err(error).with_context(|| format!("open {}", path.display())),
+ };
+ let meta = file
+ .metadata()
+ .with_context(|| format!("stat {}", path.display()))?;
+ if !meta.file_type().is_file() {
+ return Ok(None);
+ }
+ let modified = meta
+ .modified()
+ .with_context(|| format!("stat {}", path.display()))?;
+ let mut bytes = Vec::new();
+ file.read_to_end(&mut bytes)
+ .with_context(|| format!("read {}", path.display()))?;
+ Ok(Some((modified, bytes)))
+}
+
+#[cfg(unix)]
+fn is_link_refusal(error: &std::io::Error) -> bool {
+ error.raw_os_error() == Some(libc::ELOOP)
+}
+
+#[cfg(not(unix))]
+fn is_link_refusal(_: &std::io::Error) -> bool {
+ false
+}
+
fn read_originals(root: &Path, now: SystemTime) -> Result<(Vec<LegacyOriginal>, usize)> {
let mut originals = Vec::new();
let mut skipped = 0usize;
@@ -165,17 +207,12 @@ fn read_originals(root: &Path, now: SystemTime) -> Result<(Vec<LegacyOriginal>,
continue;
};
let path = entry.path();
- // Never follow a link: it could point outside the CCR directory.
- let meta =
- std::fs::symlink_metadata(&path).with_context(|| format!("stat {}", path.display()))?;
- if !meta.file_type().is_file() {
+ // Open without following a link (it could point outside the CCR
+ // directory) and read both metadata and bytes from that one handle.
+ let Some((modified, bytes)) = read_regular(&path)? else {
skipped += 1;
continue;
- }
- let modified = meta
- .modified()
- .with_context(|| format!("stat {}", path.display()))?;
- let bytes = std::fs::read(&path).with_context(|| format!("read {}", path.display()))?;
+ };
let Ok(text) = String::from_utf8(bytes) else {
skipped += 1;
continue;
diff --git a/crates/openhuman-core/src/modules/x402_service.rs b/crates/openhuman-core/src/modules/x402_service.rs
index 4f05df4918..21c917b344 100644
--- a/crates/openhuman-core/src/modules/x402_service.rs
+++ b/crates/openhuman-core/src/modules/x402_service.rs
@@ -140,7 +140,11 @@ async fn ensure_service(
if active.is_none() {
// The module's ledger opens on its storage: bring the payments file of
// an earlier install across first (once per workspace, best effort).
- super::import_legacy_x402(&workspace).await;
+ if !super::import_legacy_x402(&workspace).await {
+ // Opening the ledger without the earlier payments would restore the
+ // spending allowance; stay inactive and retry on the next call.
+ anyhow::bail!("x402 legacy payment import failed; the service is not opened");
+ }
let service_config = X402ConfigResponse {
workspace_dir: workspace.display().to_string(),
session_id: format!("x402-{}", uuid::Uuid::new_v4()),