Skip to content

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

refactor(wallet): thread dispatch context through wallet and x402 services

Steven Enamakelc2786b71c3e3
diff --git a/crates/openhuman-core/src/modules/site_memory_host.rs b/crates/openhuman-core/src/modules/site_memory_host.rs
index 8e8497ea5c..f0bb7c5eed 100644
--- a/crates/openhuman-core/src/modules/site_memory_host.rs
+++ b/crates/openhuman-core/src/modules/site_memory_host.rs
@@ -177,7 +177,8 @@ async fn write_private(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
struct SiteMemoryHost;
async fn config() -> tinybus::Result<crate::config::Config> {
- crate::config::rpc::load_config_with_timeout()
+ crate::core::runtime::DispatchCtx::host_default()
+ .load_config()
.await
.map_err(|e| method_error(format!("config unavailable: {e}")))
}
diff --git a/crates/openhuman-core/src/modules/wallet_host.rs b/crates/openhuman-core/src/modules/wallet_host.rs
index daf9f43c85..ea0cfb0d72 100644
--- a/crates/openhuman-core/src/modules/wallet_host.rs
+++ b/crates/openhuman-core/src/modules/wallet_host.rs
@@ -162,8 +162,9 @@ pub(super) fn has_endpoint_reference_for_test(reference: &str) -> bool {
}
pub(crate) async fn wallet_accounts_snapshot(
+ ctx: &DispatchCtx,
) -> Result<tinywallet_bus::wire::WalletAccountsResponse, String> {
- let status = crate::web3::wallet::status(&DispatchCtx::ambient())
+ let status = crate::web3::wallet::status(ctx)
.await
.map_err(|error| error.to_string())?
.value;
@@ -516,7 +517,9 @@ impl WalletHost {
/// the host's custody store through this callback.
async fn wallet_accounts(&self) -> tinybus::Result<HostResponse> {
Ok(HostResponse::WalletAccounts(
- wallet_accounts_snapshot().await.map_err(callback_error)?,
+ wallet_accounts_snapshot(&DispatchCtx::host_default())
+ .await
+ .map_err(callback_error)?,
))
}
@@ -634,7 +637,8 @@ impl WalletHost {
/// Perform only the named authenticated wallet-backend request.
async fn wallet_backend(&self, request: WalletBackendRequest) -> tinybus::Result<HostResponse> {
- let config = crate::config::rpc::load_config_with_timeout()
+ let config = DispatchCtx::host_default()
+ .load_config()
.await
.map_err(callback_error)?;
let client =
diff --git a/crates/openhuman-core/src/modules/wallet_service.rs b/crates/openhuman-core/src/modules/wallet_service.rs
index fc6076c2b3..10c4c39bb0 100644
--- a/crates/openhuman-core/src/modules/wallet_service.rs
+++ b/crates/openhuman-core/src/modules/wallet_service.rs
@@ -39,6 +39,7 @@ fn native_wallet_service() -> &'static tokio::sync::Mutex<Option<NativeWalletSer
/// the complete bus operation so a user or endpoint change cannot replace the
/// module's quote store while an earlier request is still using it.
pub(crate) async fn with_native_wallet<R, F, Fut>(
+ ctx: &DispatchCtx,
config: &Config,
operation: F,
) -> Result<R, WalletCallError>
@@ -49,7 +50,7 @@ where
{
let mut active = native_wallet_service().lock().await;
register_native_wallet_shutdown_hook();
- let accounts = super::wallet_host::wallet_accounts_snapshot()
+ let accounts = super::wallet_host::wallet_accounts_snapshot(ctx)
.await
.map_err(WalletCallError::Failed)?;
let configuration = native_wallet_config_request();
@@ -121,13 +122,14 @@ async fn ensure_native_wallet(
/// Prepare a native transfer through the module and retain its exact quote so
/// the host can bind approval and signer selection to the same immutable facts.
pub(crate) async fn prepare_native_transfer(
+ ctx: &DispatchCtx,
config: &Config,
owner: Option<tinywallet_bus::wire::QuoteOwner>,
params: tinywallet_bus::wire::PrepareTransferParams,
) -> Result<tinywallet_bus::wire::PreparedTransaction, WalletCallError> {
let mut active = native_wallet_service().lock().await;
register_native_wallet_shutdown_hook();
- let accounts = super::wallet_host::wallet_accounts_snapshot()
+ let accounts = super::wallet_host::wallet_accounts_snapshot(ctx)
.await
.map_err(WalletCallError::Failed)?;
ensure_native_wallet(
@@ -165,6 +167,7 @@ pub(crate) async fn prepare_native_transfer(
/// matching account secret and delivers it over the attested confidential bus
/// call; the module owns quote validation, approval facts, signing and broadcast.
pub(crate) async fn execute_native_transfer(
+ ctx: &DispatchCtx,
config: &Config,
owner: Option<tinywallet_bus::wire::QuoteOwner>,
params: tinywallet_bus::wire::ExecutePreparedParams,
@@ -177,7 +180,7 @@ pub(crate) async fn execute_native_transfer(
let mut active = native_wallet_service().lock().await;
register_native_wallet_shutdown_hook();
- let accounts = super::wallet_host::wallet_accounts_snapshot()
+ let accounts = super::wallet_host::wallet_accounts_snapshot(ctx)
.await
.map_err(WalletCallError::Failed)?;
ensure_native_wallet(
@@ -199,7 +202,7 @@ pub(crate) async fn execute_native_transfer(
.ok_or_else(|| {
WalletCallError::InvalidInput("prepared wallet quote is unavailable".into())
})?;
- let secret = signing_secret(config, quote.chain).await?;
+ let secret = signing_secret(ctx, config, quote.chain).await?;
let mut request = tinywallet_bus::wire::NativeWalletExecuteRequest {
service: service.response.service.clone(),
owner,
@@ -223,6 +226,7 @@ pub(crate) async fn execute_native_transfer(
}
async fn signing_secret(
+ ctx: &DispatchCtx,
config: &Config,
chain: tinywallet_bus::wire::WalletChain,
) -> Result<tinywallet_bus::wire::SecretMaterial, WalletCallError> {
@@ -232,7 +236,7 @@ async fn signing_secret(
tinywallet_bus::wire::WalletChain::Solana => crate::web3::wallet::WalletChain::Solana,
tinywallet_bus::wire::WalletChain::Tron => crate::web3::wallet::WalletChain::Tron,
};
- let stored = crate::web3::wallet::secret_material(&DispatchCtx::ambient(), local_chain)
+ let stored = crate::web3::wallet::secret_material(ctx, local_chain)
.await
.map_err(WalletCallError::Failed)?;
let mnemonic =
diff --git a/crates/openhuman-core/src/modules/web3_service.rs b/crates/openhuman-core/src/modules/web3_service.rs
index 4c71f7197d..bfa29febb1 100644
--- a/crates/openhuman-core/src/modules/web3_service.rs
+++ b/crates/openhuman-core/src/modules/web3_service.rs
@@ -33,11 +33,12 @@ fn service() -> &'static tokio::sync::Mutex<Option<Web3Service>> {
}
async fn ensure_service(
+ ctx: &DispatchCtx,
config: &Config,
active: &mut Option<Web3Service>,
) -> Result<(), wallet::WalletCallError> {
register_shutdown_hook();
- let accounts = wallet_host::wallet_accounts_snapshot()
+ let accounts = wallet_host::wallet_accounts_snapshot(ctx)
.await
.map_err(wallet::WalletCallError::Failed)?;
let workspace = config.workspace_dir.clone();
@@ -139,7 +140,8 @@ fn register_shutdown_hook() {
let Some(current) = active.as_ref() else {
return;
};
- let config = crate::config::rpc::load_config_with_timeout().await;
+ // A shutdown hook carries no context of its own.
+ let config = DispatchCtx::host_default().load_config().await;
let Ok(config) = config else {
tracing::warn!(
"[modules:web3] could not load configuration to close service at shutdown"
@@ -175,13 +177,14 @@ pub(crate) struct ExecuteParams {
/// Prepare one implementation-owned Web3 quote.
pub(crate) async fn quote(
+ ctx: &DispatchCtx,
config: &Config,
owner: Option<QuoteOwner>,
member: &'static str,
params: serde_json::Value,
) -> Result<Web3QuoteResponse, wallet::WalletCallError> {
let mut active = service().lock().await;
- ensure_service(config, &mut active).await?;
+ ensure_service(ctx, config, &mut active).await?;
let service_handle = active
.as_ref()
.expect("ensure_service installs a handle")
@@ -207,14 +210,18 @@ pub(crate) async fn quote(
}
/// Call the module-owned authenticated-backend route operation.
-pub(crate) async fn routes(config: &Config) -> Result<serde_json::Value, wallet::WalletCallError> {
+pub(crate) async fn routes(
+ ctx: &DispatchCtx,
+ config: &Config,
+) -> Result<serde_json::Value, wallet::WalletCallError> {
let mut active = service().lock().await;
- ensure_service(config, &mut active).await?;
+ ensure_service(ctx, config, &mut active).await?;
wallet::call(config, methods::WEB3_ROUTES, ()).await
}
/// Execute one explicitly confirmed quote using its module-declared signer chain.
pub(crate) async fn execute(
+ ctx: &DispatchCtx,
config: &Config,
owner: Option<QuoteOwner>,
quote_id: String,
@@ -226,7 +233,7 @@ pub(crate) async fn execute(
));
}
let mut active = service().lock().await;
- ensure_service(config, &mut active).await?;
+ ensure_service(ctx, config, &mut active).await?;
let service_handle = active
.as_ref()
.expect("ensure_service installs a handle")
@@ -246,7 +253,7 @@ pub(crate) async fn execute(
WalletChain::Solana => crate::web3::wallet::WalletChain::Solana,
WalletChain::Tron => crate::web3::wallet::WalletChain::Tron,
};
- let stored = crate::web3::wallet::secret_material(&DispatchCtx::ambient(), chain)
+ let stored = crate::web3::wallet::secret_material(ctx, chain)
.await
.map_err(|error| wallet::WalletCallError::Failed(error.to_string()))?;
let mnemonic =
diff --git a/crates/openhuman-core/src/modules/x402_service.rs b/crates/openhuman-core/src/modules/x402_service.rs
index f5b58a4220..67e0881fda 100644
--- a/crates/openhuman-core/src/modules/x402_service.rs
+++ b/crates/openhuman-core/src/modules/x402_service.rs
@@ -22,21 +22,22 @@ use super::wallet::{self, WalletCallError};
/// secret material is sent only through the attested confidential call; the
/// module parses the challenge and selects the matching chain.
pub(crate) async fn prepare_payment(
+ ctx: &DispatchCtx,
config: &Config,
url: String,
payment_required: String,
owner: Option<tinywallet_bus::wire::QuoteOwner>,
) -> Result<(X402ServiceHandle, X402PreparePaymentResponse), WalletCallError> {
let mut active = service().lock().await;
- ensure_service(config, &mut active).await?;
+ ensure_service(ctx, config, &mut active).await?;
let service_handle = active
.as_ref()
.ok_or_else(|| WalletCallError::Failed("x402 service is unavailable".into()))?
.handle
.clone();
- let evm = signing_secret(config, crate::web3::wallet::WalletChain::Evm).await?;
- let solana = signing_secret(config, crate::web3::wallet::WalletChain::Solana).await?;
+ let evm = signing_secret(ctx, config, crate::web3::wallet::WalletChain::Evm).await?;
+ let solana = signing_secret(ctx, config, crate::web3::wallet::WalletChain::Solana).await?;
let mut request = X402PreparePaymentRequest {
service: service_handle.clone(),
url,
@@ -75,10 +76,11 @@ pub(crate) async fn record_payment(
}
async fn signing_secret(
+ ctx: &DispatchCtx,
config: &Config,
chain: crate::web3::wallet::WalletChain,
) -> Result<tinywallet_bus::wire::SecretMaterial, WalletCallError> {
- let stored = crate::web3::wallet::secret_material(&DispatchCtx::ambient(), chain)
+ let stored = crate::web3::wallet::secret_material(ctx, chain)
.await
.map_err(WalletCallError::Failed)?;
let phrase =
@@ -108,12 +110,13 @@ fn service() -> &'static tokio::sync::Mutex<Option<X402Service>> {
}
async fn ensure_service(
+ ctx: &DispatchCtx,
config: &Config,
active: &mut Option<X402Service>,
) -> Result<(), WalletCallError> {
register_shutdown_hook();
let workspace = config.workspace_dir.clone();
- let accounts = super::wallet_host::wallet_accounts_snapshot()
+ let accounts = super::wallet_host::wallet_accounts_snapshot(ctx)
.await
.map_err(WalletCallError::Failed)?;
let budget = budget_from(|name| std::env::var(name).ok());
@@ -204,7 +207,8 @@ fn register_shutdown_hook() {
let Some(current) = active.as_ref() else {
return;
};
- let Ok(config) = crate::config::rpc::load_config_with_timeout().await else {
+ // A shutdown hook carries no context of its own.
+ let Ok(config) = DispatchCtx::host_default().load_config().await else {
tracing::warn!(
"[modules:x402] could not load configuration to close service at shutdown"
);
@@ -227,13 +231,17 @@ fn register_shutdown_hook() {
});
}
-async fn with_service<R, F, Fut>(config: &Config, operation: F) -> Result<R, WalletCallError>
+async fn with_service<R, F, Fut>(
+ ctx: &DispatchCtx,
+ config: &Config,
+ operation: F,
+) -> Result<R, WalletCallError>
where
F: FnOnce(X402ServiceHandle) -> Fut,
Fut: std::future::Future<Output = Result<R, WalletCallError>>,
{
let mut active = service().lock().await;
- ensure_service(config, &mut active).await?;
+ ensure_service(ctx, config, &mut active).await?;
let handle = active
.as_ref()
.ok_or_else(|| WalletCallError::Failed("x402 service is unavailable".into()))?
@@ -244,11 +252,12 @@ where
/// Read the module-owned spending totals and budgets.
pub(crate) async fn summary(
+ ctx: &DispatchCtx,
config: &Config,
) -> Result<X402DetailedSpendingSummary, WalletCallError> {
let config = config.clone();
let operation_config = config.clone();
- with_service(&config, move |service| async move {
+ with_service(ctx, &config, move |service| async move {
wallet::call(
&operation_config,
methods::X402_SPENDING_SUMMARY_DETAILED,
@@ -261,12 +270,13 @@ pub(crate) async fn summary(
/// Read recent module-owned payment records.
pub(crate) async fn list_payments(
+ ctx: &DispatchCtx,
config: &Config,
limit: u64,
) -> Result<X402ListPaymentsResponse, WalletCallError> {
let config = config.clone();
let operation_config = config.clone();
- with_service(&config, move |service| async move {
+ with_service(ctx, &config, move |service| async move {
wallet::call(
&operation_config,
methods::X402_LIST_PAYMENTS,
@@ -279,13 +289,14 @@ pub(crate) async fn list_payments(
/// Update live module-owned spending limits without replacing the ledger.
pub(crate) async fn update_budget(
+ ctx: &DispatchCtx,
config: &Config,
per_request_max_atomic: Option<u64>,
daily_max_atomic: Option<u64>,
monthly_max_atomic: Option<u64>,
) -> Result<tinywallet_bus::wire::X402SpendingSummary, WalletCallError> {
let mut active = service().lock().await;
- ensure_service(config, &mut active).await?;
+ ensure_service(ctx, config, &mut active).await?;
let current = active
.as_mut()
.ok_or_else(|| WalletCallError::Failed("x402 service is unavailable".into()))?;
diff --git a/crates/openhuman-core/src/web3/wallet/execution.rs b/crates/openhuman-core/src/web3/wallet/execution.rs
index 0b8fe6eff4..62362d7dfe 100644
--- a/crates/openhuman-core/src/web3/wallet/execution.rs
+++ b/crates/openhuman-core/src/web3/wallet/execution.rs
@@ -24,7 +24,7 @@ pub async fn network_defaults(
ctx: &DispatchCtx,
) -> Result<Outcome<Vec<WalletNetworkDefaults>>, String> {
let config = ctx.load_config().await?;
- let result = wallet_service::with_native_wallet(&config, |service| async {
+ let result = wallet_service::with_native_wallet(ctx, &config, |service| async {
wallet_module::call(
&config,
methods::NATIVE_WALLET_NETWORK_DEFAULTS,
@@ -43,7 +43,7 @@ pub async fn network_defaults(
/// Every asset the wallet catalogues.
pub async fn supported_assets(ctx: &DispatchCtx) -> Result<Outcome<Vec<SupportedAsset>>, String> {
let config = ctx.load_config().await?;
- let result = wallet_service::with_native_wallet(&config, |service| async {
+ let result = wallet_service::with_native_wallet(ctx, &config, |service| async {
wallet_module::call(
&config,
methods::NATIVE_WALLET_SUPPORTED_ASSETS,
@@ -62,7 +62,7 @@ pub async fn supported_assets(ctx: &DispatchCtx) -> Result<Outcome<Vec<Supported
/// Which chains have an account and a provider.
pub async fn chain_status(ctx: &DispatchCtx) -> Result<Outcome<Vec<ChainStatus>>, String> {
let config = ctx.load_config().await?;
- let result = wallet_service::with_native_wallet(&config, |service| async {
+ let result = wallet_service::with_native_wallet(ctx, &config, |service| async {
wallet_module::call(
&config,
methods::NATIVE_WALLET_CHAIN_STATUS,
@@ -81,7 +81,7 @@ pub async fn chain_status(ctx: &DispatchCtx) -> Result<Outcome<Vec<ChainStatus>>
/// Live native balances.
pub async fn balances(ctx: &DispatchCtx) -> Result<Outcome<Vec<BalanceInfo>>, String> {
let config = ctx.load_config().await?;
- let result = wallet_service::with_native_wallet(&config, |service| async {
+ let result = wallet_service::with_native_wallet(ctx, &config, |service| async {
wallet_module::call(
&config,
methods::NATIVE_WALLET_BALANCES,
@@ -104,7 +104,7 @@ pub async fn prepare_transfer(
owner: Option<tinywallet_bus::wire::QuoteOwner>,
) -> Result<Outcome<PreparedTransaction>, String> {
let config = ctx.load_config().await?;
- let quote = wallet_service::prepare_native_transfer(&config, owner, params)
+ let quote = wallet_service::prepare_native_transfer(ctx, &config, owner, params)
.await
.map_err(|error| error.to_string())?;
Ok(Outcome::new(
@@ -120,7 +120,7 @@ pub async fn execute_prepared(
owner: Option<tinywallet_bus::wire::QuoteOwner>,
) -> Result<Outcome<ExecutionResult>, String> {
let config = ctx.load_config().await?;
- let result = wallet_service::execute_native_transfer(&config, owner, params)
+ let result = wallet_service::execute_native_transfer(ctx, &config, owner, params)
.await
.map_err(|error| error.to_string())?;
Ok(Outcome::new(
@@ -137,7 +137,7 @@ pub async fn tx_status(
hash: &str,
) -> Result<Outcome<TxStatusInfo>, String> {
let config = ctx.load_config().await?;
- let result = wallet_service::with_native_wallet(&config, |service| async {
+ let result = wallet_service::with_native_wallet(ctx, &config, |service| async {
wallet_module::call(
&config,
methods::NATIVE_WALLET_TX_STATUS,
@@ -166,7 +166,7 @@ pub async fn tx_receipt(
hash: &str,
) -> Result<Outcome<TxReceiptInfo>, String> {
let config = ctx.load_config().await?;
- let result = wallet_service::with_native_wallet(&config, |service| async {
+ let result = wallet_service::with_native_wallet(ctx, &config, |service| async {
wallet_module::call(
&config,
methods::NATIVE_WALLET_TX_RECEIPT,
@@ -195,7 +195,7 @@ pub async fn lookup_tx(
hash: &str,
) -> Result<Outcome<TxLookupInfo>, String> {
let config = ctx.load_config().await?;
- let result = wallet_service::with_native_wallet(&config, |service| async {
+ let result = wallet_service::with_native_wallet(ctx, &config, |service| async {
wallet_module::call(
&config,
methods::NATIVE_WALLET_LOOKUP_TX,