Read-only mirror of github.com/ondeinference/ed.
Mirrored from https://github.com/ondeinference/ed.git
Synced Nov 23, 01:00 UTC
Relax thread-safety bounds only on wasm32-unknown-unknown
target_arch = "wasm32" also matches wasm32-wasip1-threads and Emscripten with pthreads, where the single JS thread premise does not hold and the blanket MaybeSend/MaybeSync impls would let a !Send host type cross threads. Gate the relaxation, and the matching async_trait(?Send) attributes, on wasm32 with target_os = "unknown", the browser target the docs justify. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Co-Authored-By: siGit Code v1.7.6 <noreply@sigit.si>
Seto Elkahfi committed
Nov 10, 2026 at 14:05 UTC
f54d4fe5b43497d1c7c2b88b61fa21ae537492c3
4 files changed
+43
-23
crates/ed-agent/src/maybe.rs
+12
-10
@@ -1,4 +1,4 @@
1
-//! Thread-safety bounds that relax on `wasm32`.
1
+//! Thread-safety bounds that relax on browser wasm.
2
//!
3
//! The host-facing seams ([`ToolExecutor`](crate::ToolExecutor),
4
//! [`ApprovalHandler`](crate::ApprovalHandler), [`EventSink`](crate::EventSink),
@@ -6,29 +6,31 @@
6
//! targets, where a turn can hop between runtime threads. In a browser the
7
//! agent lives on one JS thread and its implementations hold `JsValue`s, which
8
//! are `!Send`. These aliases resolve to exactly `Send` / `Sync` everywhere
9
-//! except `wasm32`, so native signatures are unchanged. It is the same pattern
9
+//! except `wasm32-unknown-unknown`, so native signatures are unchanged. Other
10
+//! wasm targets (`wasm32-wasip1-threads`, Emscripten with pthreads) can run
11
+//! threads, so they keep the real bounds. It is the same pattern
12
//! wgpu uses (`WasmNotSend`).
13
//!
14
//! Soundness rests on the single-thread guarantee: nothing holding a `JsValue`
15
//! may be handed to another thread. Do not paper over a `!Send` type with
16
//! `unsafe impl Send`.
17
16
-#[cfg(not(target_arch = "wasm32"))]
18
+#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
19
pub trait MaybeSend: Send {}
18
-#[cfg(not(target_arch = "wasm32"))]
20
+#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
21
impl<T: Send + ?Sized> MaybeSend for T {}
22
21
-#[cfg(target_arch = "wasm32")]
23
+#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
24
pub trait MaybeSend {}
23
-#[cfg(target_arch = "wasm32")]
25
+#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
26
impl<T: ?Sized> MaybeSend for T {}
27
26
-#[cfg(not(target_arch = "wasm32"))]
28
+#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
29
pub trait MaybeSync: Sync {}
28
-#[cfg(not(target_arch = "wasm32"))]
30
+#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
31
impl<T: Sync + ?Sized> MaybeSync for T {}
32
31
-#[cfg(target_arch = "wasm32")]
33
+#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
34
pub trait MaybeSync {}
33
-#[cfg(target_arch = "wasm32")]
35
+#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
36
impl<T: ?Sized> MaybeSync for T {}
crates/ed-agent/src/session.rs
+10
-4
@@ -34,8 +34,11 @@ pub struct SessionError(pub String);
34
/// messages if it does.
35
/// - `delete` of an unknown id succeeds.
36
/// - `list` is ordered most recently updated first.
37
-#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
38
-#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
37
+#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))]
38
+#[cfg_attr(
39
+ not(all(target_arch = "wasm32", target_os = "unknown")),
40
+ async_trait::async_trait
41
+)]
42
pub trait SessionStore: MaybeSend + MaybeSync + 'static {
43
async fn list(&self) -> Result<Vec<SessionMeta>, SessionError>;
44
async fn load(&self, id: &str) -> Result<Option<Vec<ChatMessage>>, SessionError>;
@@ -71,8 +74,11 @@ mod tests {
74
}
75
}
76
74
- #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
75
- #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
77
+ #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))]
78
+ #[cfg_attr(
79
+ not(all(target_arch = "wasm32", target_os = "unknown")),
80
+ async_trait::async_trait
81
+ )]
82
impl SessionStore for MemoryStore {
83
async fn list(&self) -> Result<Vec<SessionMeta>, SessionError> {
84
let mut metas: Vec<_> = self
crates/ed-agent/src/sink.rs
+1
-1
@@ -16,7 +16,7 @@ use crate::{AgentReply, ApprovalRequest, ChatReply, ToolCall, ToolExecutionResul
16
///
17
/// Both methods are synchronous and should stay cheap — they are called while
18
/// Ed is mid-operation. Hand off to a channel or an event bus rather than
19
-/// blocking. Implementations must be `Send + Sync` (on `wasm32`, where the agent is single-threaded, that relaxes to nothing; see [`maybe`](crate::maybe)) because a load or a send
19
+/// blocking. Implementations must be `Send + Sync` (on `wasm32-unknown-unknown`, where the agent is single-threaded, that relaxes to nothing; see [`maybe`](crate::maybe)) because a load or a send
20
/// may be driven from any task.
21
pub trait EventSink: MaybeSend + MaybeSync + 'static {
22
/// The engine moved to a new lifecycle state.
crates/ed-agent/src/tool.rs
+20
-8
@@ -105,14 +105,20 @@ pub enum ApprovalDecision {
105
Deny,
106
}
107
108
-#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
109
-#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
108
+#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))]
109
+#[cfg_attr(
110
+ not(all(target_arch = "wasm32", target_os = "unknown")),
111
+ async_trait::async_trait
112
+)]
113
pub trait ToolExecutor: MaybeSend + MaybeSync + 'static {
114
async fn execute(&self, tool_name: &str, arguments: &str) -> Result<String, String>;
115
}
116
114
-#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
115
-#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
117
+#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))]
118
+#[cfg_attr(
119
+ not(all(target_arch = "wasm32", target_os = "unknown")),
120
+ async_trait::async_trait
121
+)]
122
pub trait ApprovalHandler: MaybeSend + MaybeSync + 'static {
123
async fn approve(&self, request: ApprovalRequest) -> ApprovalDecision;
124
}
@@ -120,8 +126,11 @@ pub trait ApprovalHandler: MaybeSend + MaybeSync + 'static {
126
#[derive(Debug, Default)]
127
pub struct RejectingExecutor;
128
123
-#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
124
-#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
129
+#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))]
130
+#[cfg_attr(
131
+ not(all(target_arch = "wasm32", target_os = "unknown")),
132
+ async_trait::async_trait
133
+)]
134
impl ToolExecutor for RejectingExecutor {
135
async fn execute(&self, tool_name: &str, _: &str) -> Result<String, String> {
136
Err(format!("no executor is registered for tool {tool_name:?}"))
@@ -131,8 +140,11 @@ impl ToolExecutor for RejectingExecutor {
140
#[derive(Debug, Default)]
141
pub struct DenyApprovals;
142
134
-#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
135
-#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
143
+#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), async_trait::async_trait(?Send))]
144
+#[cfg_attr(
145
+ not(all(target_arch = "wasm32", target_os = "unknown")),
146
+ async_trait::async_trait
147
+)]
148
impl ApprovalHandler for DenyApprovals {
149
async fn approve(&self, _: ApprovalRequest) -> ApprovalDecision {
150
ApprovalDecision::Deny