Skip to content

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

refactor(agent): extract artifact store helpers

Steven Enamakel317dd3187e95
diff --git a/crates/openhuman-core/src/agent/artifacts/store.rs b/crates/openhuman-core/src/agent/artifacts/store.rs
index 8d18b08158..4754429da7 100644
--- a/crates/openhuman-core/src/agent/artifacts/store.rs
+++ b/crates/openhuman-core/src/agent/artifacts/store.rs
@@ -302,8 +302,8 @@ fn sanitize_filename_stem(title: &str) -> String {
/// matching [`finalize_artifact`] / [`fail_artifact`] later fires it
/// reuses the same `artifact_id`, so the card swaps in place without
/// flicker. Same chat-context routing rules as the Ready/Failed pair —
-/// `thread_id` / `client_id` come from the
-/// [`crate::security::approval::ApprovalChatContext`] task-local and
+/// `thread_id` / `client_id` come from the `chat` argument (the producing
+/// turn's [`crate::security::approval::ApprovalChatContext`]) and
/// are `None` for CLI / cron / sub-agent paths, in which case the web
/// bridge silently drops the event for lack of a routing target.
pub async fn create_artifact(
@@ -312,8 +312,9 @@ pub async fn create_artifact(
kind: super::types::ArtifactKind,
title: &str,
extension: &str,
+ chat: Option<&ApprovalChatContext>,
) -> Result<(ArtifactMeta, PathBuf), String> {
- create_artifact_for_call(workspace_dir, files, kind, title, extension, None).await
+ create_artifact_for_call(workspace_dir, files, kind, title, extension, None, chat).await
}
/// As [`create_artifact`], but also records the provider-assigned
@@ -329,6 +330,7 @@ pub async fn create_artifact_for_call(
title: &str,
extension: &str,
tool_call_id: Option<&str>,
+ chat: Option<&ApprovalChatContext>,
) -> Result<(ArtifactMeta, PathBuf), String> {
create_artifact_regenerating(
workspace_dir,
@@ -338,6 +340,7 @@ pub async fn create_artifact_for_call(
extension,
tool_call_id,
None,
+ chat,
)
.await
}
@@ -357,6 +360,7 @@ pub async fn create_artifact_regenerating(
extension: &str,
tool_call_id: Option<&str>,
regenerate_target: Option<&str>,
+ chat: Option<&ApprovalChatContext>,
) -> Result<(ArtifactMeta, PathBuf), String> {
let roots = files.into();
let files_dir = roots.current();
@@ -395,9 +399,9 @@ pub async fn create_artifact_regenerating(
// Capture the originating chat thread (if any) at create-time so the
// panel can repopulate from disk after a redux-persist purge — see
// #3226. `finalize_artifact` / `fail_artifact` already read the same
- // task-local for event publication; persisting it here means the
+ // chat context for event publication; persisting it here means the
// routing target survives a process restart.
- let (thread_id, _, _) = current_chat_context();
+ let (thread_id, _, _) = chat_route(chat);
// On a regenerate the id is reused in place, so preserve the original
// `created_at` — bumping it to now would reorder the artifact to the
@@ -464,7 +468,7 @@ pub async fn create_artifact_regenerating(
// (#3162). When `finalize_artifact` / `fail_artifact` later fires the
// matching Ready/Failed event with the same `artifact_id`, the
// frontend can swap the card in place.
- let (thread_id, client_id, request_id) = current_chat_context();
+ let (thread_id, client_id, request_id) = chat_route(chat);
crate::core::bus::BUS.publish(crate::core::events::DomainEvent::ArtifactPending {
artifact_id: meta.id.clone(),
kind: meta.kind.as_str().to_string(),
@@ -487,7 +491,7 @@ pub async fn create_artifact_regenerating(
/// On a real transition (Pending → Ready), publishes
/// [`DomainEvent::ArtifactReady`] on the global bus so the web
/// channel can surface a download card to the originating thread.
-/// When the calling task carries no
+/// When the caller passes no
/// [`ApprovalChatContext`](crate::security::approval::ApprovalChatContext)
/// (CLI / cron / sub-agent paths), the event is still published but
/// `thread_id` / `client_id` are `None` so the socket bridge silently
@@ -497,6 +501,7 @@ pub async fn finalize_artifact(
workspace_dir: &Path,
artifact_id: &str,
size_bytes: u64,
+ chat: Option<&ApprovalChatContext>,
) -> Result<ArtifactMeta, String> {
let mut meta = get_artifact(workspace_dir, artifact_id).await?;
if matches!(meta.status, ArtifactStatus::Ready) && meta.size_bytes == size_bytes {
@@ -509,7 +514,7 @@ pub async fn finalize_artifact(
save_artifact_meta(workspace_dir, &meta).await?;
log::debug!("[artifacts] finalize_artifact: id={artifact_id} -> Ready size={size_bytes}");
- let (thread_id, client_id, request_id) = current_chat_context();
+ let (thread_id, client_id, request_id) = chat_route(chat);
crate::core::bus::BUS.publish(crate::core::events::DomainEvent::ArtifactReady {
artifact_id: meta.id.clone(),
kind: meta.kind.as_str().to_string(),
@@ -538,6 +543,7 @@ pub async fn fail_artifact(
roots: impl Into<FileRoots>,
artifact_id: &str,
reason: &str,
+ chat: Option<&ApprovalChatContext>,
) -> Result<ArtifactMeta, String> {
let mut meta = get_artifact(workspace_dir, artifact_id).await?;
meta.status = ArtifactStatus::Failed;
@@ -553,7 +559,7 @@ pub async fn fail_artifact(
reason.len()
);
- let (thread_id, client_id, request_id) = current_chat_context();
+ let (thread_id, client_id, request_id) = chat_route(chat);
crate::core::bus::BUS.publish(crate::core::events::DomainEvent::ArtifactFailed {
artifact_id: meta.id.clone(),
kind: meta.kind.as_str().to_string(),
@@ -568,23 +574,20 @@ pub async fn fail_artifact(
Ok(meta)
}
-/// Read the active [`ApprovalChatContext`] task-local (set by
-/// `web_chat` around each chat turn) and return its thread id, client
-/// id, and the originating turn's `request_id`. Returns `(None, None,
-/// None)` for non-chat callers (CLI, cron, sub-agent runners) so
-/// artifact emit hooks degrade gracefully — the event is still
-/// published but the web subscriber drops it for lack of a routing
-/// target.
-fn current_chat_context() -> (Option<String>, Option<String>, Option<String>) {
- crate::security::approval::APPROVAL_CHAT_CONTEXT
- .try_with(|ctx| {
- (
- Some(ctx.thread_id.clone()),
- Some(ctx.client_id.clone()),
- ctx.request_id.clone(),
- )
- })
- .unwrap_or((None, None, None))
+/// The routing triple of the producing turn's [`ApprovalChatContext`]: its
+/// thread id, client id and originating `request_id`. `(None, None, None)` for
+/// non-chat callers (CLI, cron, sub-agent runners) so artifact emit hooks
+/// degrade gracefully: the event is still published but the web subscriber
+/// drops it for lack of a routing target.
+fn chat_route(chat: Option<&ApprovalChatContext>) -> (Option<String>, Option<String>, Option<String>) {
+ match chat {
+ Some(ctx) => (
+ Some(ctx.thread_id.clone()),
+ Some(ctx.client_id.clone()),
+ ctx.request_id.clone(),
+ ),
+ None => (None, None, None),
+ }
}
#[cfg(test)]