cordis-rs 0.4.2

Runtime-agnostic Rust port of Cordis, the plugin framework at the core of DeepSeek Harness
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! Disposal-aware event bus and Cordis dispatch strategies.

use crate::context::{Context, ContextMeta, RootInner};
use crate::effect::{AsyncDisposer, EffectHandle};
use crate::fiber::FiberInner;
use crate::utils::{BoxFuture, block_on, join_all, lock};
use crate::{CordisError, ErrorCode, Result, Value};
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};
use std::future::Future;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, Weak};

/// Event argument and listener bail value.
pub type EventValue = Value;
/// Listener result. `Some(value)` bails; `None` continues dispatch.
pub type EventResult = Result<Option<EventValue>>;

/// Return whether a Rust event result should stop bail-style dispatch.
///
/// Rust uses `Option` instead of JavaScript truthiness: every `Some` value
/// bails, while `None` continues.
pub const fn is_bailed(value: &Option<EventValue>) -> bool {
    value.is_some()
}

/// Event dispatch strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DispatchMode {
    /// Synchronous observation.
    Emit,
    /// Concurrent futures.
    Parallel,
    /// Ordered asynchronous bail.
    Serial,
    /// Ordered synchronous bail.
    Bail,
    /// Middleware continuation chain.
    Waterfall,
}

/// Registered listener options.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct EventOptions {
    /// Insert before existing listeners.
    pub prepend: bool,
    /// Ignore dispatch-context filter checks.
    pub global: bool,
}

/// Continuation supplied during waterfall dispatch.
pub type Next = Arc<dyn Fn() -> BoxFuture<EventResult> + Send + Sync + 'static>;

/// Owned event delivered to callbacks.
#[derive(Clone)]
pub struct Event {
    name: Arc<str>,
    args: Arc<[EventValue]>,
    target: Option<Context>,
    next: Option<Next>,
}

impl Event {
    /// Construct an event without a dispatch target.
    pub fn new(name: impl Into<String>, args: impl IntoIterator<Item = EventValue>) -> Self {
        Self {
            name: Arc::from(name.into()),
            args: args.into_iter().collect::<Vec<_>>().into(),
            target: None,
            next: None,
        }
    }

    /// Event name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Type-erased event arguments.
    pub fn args(&self) -> &[EventValue] {
        &self.args
    }

    /// Downcast one argument, returning `None` when out of bounds.
    pub fn arg<T>(&self, index: usize) -> Result<Option<Arc<T>>>
    where
        T: Send + Sync + 'static,
    {
        self.args.get(index).map(Value::downcast).transpose()
    }

    /// Explicit dispatch target used for context filtering.
    pub fn target(&self) -> Option<&Context> {
        self.target.as_ref()
    }

    /// Whether this callback is part of a waterfall chain.
    pub fn has_next(&self) -> bool {
        self.next.is_some()
    }

    /// Invoke the next waterfall listener or innermost behavior.
    pub fn next(&self) -> BoxFuture<EventResult> {
        match self.next.clone() {
            Some(next) => next(),
            None => Box::pin(async { Ok(None) }),
        }
    }

    fn with_target(mut self, target: Context) -> Self {
        self.target = Some(target);
        self
    }

    fn with_next(mut self, next: Next) -> Self {
        self.next = Some(next);
        self
    }
}

impl Debug for Event {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("Event")
            .field("name", &self.name)
            .field("args", &self.args)
            .field("target", &self.target)
            .field("has_next", &self.next.is_some())
            .finish()
    }
}

type Callback = Arc<dyn Fn(Event) -> BoxFuture<EventResult> + Send + Sync + 'static>;

struct HookContext {
    root: Weak<RootInner>,
    fiber: Weak<FiberInner>,
    meta: ContextMeta,
}

impl HookContext {
    fn capture(ctx: &Context) -> Self {
        Self {
            root: Arc::downgrade(&ctx.root),
            fiber: ctx.fiber.clone(),
            meta: ctx.meta.clone(),
        }
    }

    fn get(&self) -> Option<Context> {
        Some(Context {
            root: self.root.upgrade()?,
            fiber: self.fiber.clone(),
            meta: self.meta.clone(),
        })
    }
}

struct Hook {
    id: u64,
    ctx: HookContext,
    callback: Callback,
    options: EventOptions,
}

#[derive(Default)]
struct EventsState {
    hooks: HashMap<String, Vec<Hook>>,
}

pub(crate) struct EventsRoot {
    state: Mutex<EventsState>,
    next_hook: AtomicU64,
}

impl EventsRoot {
    pub(crate) fn new() -> Self {
        Self {
            state: Mutex::new(EventsState::default()),
            next_hook: AtomicU64::new(0),
        }
    }

    fn next_id(&self) -> u64 {
        self.next_hook.fetch_add(1, Ordering::Relaxed) + 1
    }

    fn remove(&self, name: &str, id: u64) -> bool {
        let mut state = lock(&self.state);
        let Some(hooks) = state.hooks.get_mut(name) else {
            return false;
        };
        let before = hooks.len();
        hooks.retain(|hook| hook.id != id);
        let removed = hooks.len() != before;
        if hooks.is_empty() {
            state.hooks.remove(name);
        }
        removed
    }
}

/// Event bus bound to a context.
#[derive(Clone, Debug)]
pub struct EventsService {
    ctx: Context,
}

impl EventsService {
    pub(crate) fn new(ctx: Context) -> Self {
        Self { ctx }
    }

    fn register_callback(
        &self,
        name: String,
        callback: Callback,
        options: EventOptions,
        once: bool,
    ) -> Result<EffectHandle> {
        let fiber = self.ctx.fiber()?;
        fiber.assert_active()?;
        let id = self.ctx.root.events.next_id();

        // Register the owning effect first: failure leaves no hook behind,
        // and a `once` wrapper can capture the effect's weak handle up front.
        let root = Arc::downgrade(&self.ctx.root);
        let effect_name = name.clone();
        let effect = fiber.register_effect(
            format!("ctx.on({name:?})"),
            AsyncDisposer::from_sync(move || {
                if let Some(root) = root.upgrade() {
                    root.events.remove(&effect_name, id);
                }
                Ok(())
            }),
        )?;

        let callback = if once {
            let fired = AtomicBool::new(false);
            let effect = Arc::downgrade(&effect.cell);
            let listener = callback;
            Arc::new(move |event| {
                // Upstream parity: a once listener removes itself when it is
                // invoked, not when it is dispatched. Listeners skipped by an
                // earlier bail or error stay registered, and the atomic claim
                // serializes concurrent dispatches without any list mutation.
                if fired.swap(true, Ordering::SeqCst) {
                    return Box::pin(async { Ok(None) }) as BoxFuture<EventResult>;
                }
                if let Some(cell) = effect.upgrade() {
                    let _ = EffectHandle::new(cell).dispose();
                }
                listener(event)
            }) as Callback
        } else {
            callback
        };

        {
            let mut state = lock(&self.ctx.root.events.state);
            let hooks = state.hooks.entry(name.clone()).or_default();
            let hook = Hook {
                id,
                ctx: HookContext::capture(&self.ctx),
                callback,
                options,
            };
            if options.prepend {
                hooks.insert(0, hook);
            } else {
                hooks.push(hook);
            }
        }

        // The fiber may have unloaded between effect registration and hook
        // insertion; its disposer then never runs, so roll back by hand
        // instead of leaking a live hook for a dead plugin.
        if fiber.uid().is_none() {
            self.ctx.root.events.remove(&name, id);
            effect.dispose()?;
            return Err(CordisError::new(ErrorCode::InactiveEffect));
        }

        Ok(effect)
    }

    /// Register a synchronous event listener owned by the current fiber.
    pub fn on<F>(
        &self,
        name: impl Into<String>,
        listener: F,
        options: EventOptions,
    ) -> Result<EffectHandle>
    where
        F: Fn(Event) -> EventResult + Send + Sync + 'static,
    {
        let listener = Arc::new(listener);
        self.register_callback(
            name.into(),
            Arc::new(move |event| {
                let listener = listener.clone();
                Box::pin(async move { listener(event) })
            }),
            options,
            false,
        )
    }

    /// Register an asynchronous event listener.
    ///
    /// The future is driven by a small blocking executor, often while the
    /// owning fiber's transition mutex is held. Do not await futures that
    /// require the current thread to make progress (for example runtime
    /// blocking-task joins or channels filled by the calling thread); park
    /// only on work completing on other threads. The same caveat applies to
    /// async listeners dispatched through [`emit`](Self::emit) and
    /// [`bail`](Self::bail), which block on each listener in turn.
    pub fn on_async<F, Fut>(
        &self,
        name: impl Into<String>,
        listener: F,
        options: EventOptions,
    ) -> Result<EffectHandle>
    where
        F: Fn(Event) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = EventResult> + Send + 'static,
    {
        let listener = Arc::new(listener);
        self.register_callback(
            name.into(),
            Arc::new(move |event| Box::pin(listener(event))),
            options,
            false,
        )
    }

    /// Register a synchronous listener removed when it is first invoked.
    ///
    /// Matches upstream cordis: removal happens at invocation time, so a
    /// listener skipped because an earlier listener bailed or failed stays
    /// registered until it actually runs.
    pub fn once<F>(
        &self,
        name: impl Into<String>,
        listener: F,
        options: EventOptions,
    ) -> Result<EffectHandle>
    where
        F: Fn(Event) -> EventResult + Send + Sync + 'static,
    {
        let listener = Arc::new(listener);
        self.register_callback(
            name.into(),
            Arc::new(move |event| {
                let listener = listener.clone();
                Box::pin(async move { listener(event) })
            }),
            options,
            true,
        )
    }

    /// Number of listeners registered for `name`.
    pub fn listener_count(&self, name: &str) -> usize {
        lock(&self.ctx.root.events.state)
            .hooks
            .get(name)
            .map(Vec::len)
            .unwrap_or(0)
    }

    fn dispatch(&self, mode: DispatchMode, event: &Event) -> Vec<Callback> {
        // Upstream gates this meta-event on listener presence; skip the
        // argument construction entirely when nobody listens.
        if !event.name().starts_with("internal/") && self.listener_count("internal/dispatch") > 0 {
            let _ = self.emit(
                "internal/dispatch",
                [
                    Value::new(mode),
                    Value::new(event.name().to_owned()),
                    Value::new(event.args().to_vec()),
                ],
            );
        }

        // Snapshot under the lock; user filters run outside it. The hook list
        // is never mutated here: once listeners remove themselves on
        // invocation, so concurrent dispatches cannot double-fire them, and a
        // filter re-entering the events API cannot deadlock on the state lock.
        let filter = event.target().and_then(Context::filter);
        let snapshot = {
            let state = lock(&self.ctx.root.events.state);
            let Some(hooks) = state.hooks.get(event.name()) else {
                return Vec::new();
            };
            hooks
                .iter()
                .map(|hook| {
                    (
                        hook.options.global,
                        hook.callback.clone(),
                        if filter.is_some() {
                            hook.ctx.get()
                        } else {
                            None
                        },
                    )
                })
                .collect::<Vec<_>>()
        };

        let mut callbacks = Vec::with_capacity(snapshot.len());
        for (global, callback, owner) in snapshot {
            let accepted = global
                || match filter {
                    None => true,
                    Some(filter) => owner.as_ref().is_some_and(|owner| filter(owner)),
                };
            if accepted {
                callbacks.push(callback);
            }
        }
        callbacks
    }

    /// Emit synchronously, returning the first listener error.
    pub fn emit(
        &self,
        name: impl Into<String>,
        args: impl IntoIterator<Item = EventValue>,
    ) -> Result<()> {
        self.emit_event(Event::new(name, args))
    }

    /// Emit with an explicit target context for listener filtering.
    pub fn emit_from(
        &self,
        target: Context,
        name: impl Into<String>,
        args: impl IntoIterator<Item = EventValue>,
    ) -> Result<()> {
        self.emit_event(Event::new(name, args).with_target(target))
    }

    fn emit_event(&self, event: Event) -> Result<()> {
        for callback in self.dispatch(DispatchMode::Emit, &event) {
            block_on(callback(event.clone()))?;
        }
        Ok(())
    }

    /// Run all listeners concurrently and aggregate failures.
    pub async fn parallel(
        &self,
        name: impl Into<String>,
        args: impl IntoIterator<Item = EventValue>,
    ) -> Result<()> {
        let event = Event::new(name, args);
        let futures = self
            .dispatch(DispatchMode::Parallel, &event)
            .into_iter()
            .map(|callback| callback(event.clone()))
            .collect();
        let errors = join_all(futures)
            .await
            .into_iter()
            .filter_map(|result| result.err())
            .collect::<Vec<_>>();
        if errors.is_empty() {
            Ok(())
        } else {
            Err(CordisError::with_message(
                ErrorCode::Event,
                format!(
                    "{} event listener(s) failed: {}",
                    errors.len(),
                    errors
                        .into_iter()
                        .map(|error| error.to_string())
                        .collect::<Vec<_>>()
                        .join("; ")
                ),
            ))
        }
    }

    /// Run listeners in order, awaiting each, until one bails.
    pub async fn serial(
        &self,
        name: impl Into<String>,
        args: impl IntoIterator<Item = EventValue>,
    ) -> EventResult {
        let event = Event::new(name, args);
        for callback in self.dispatch(DispatchMode::Serial, &event) {
            let result = callback(event.clone()).await?;
            if is_bailed(&result) {
                return Ok(result);
            }
        }
        Ok(None)
    }

    /// Synchronous ordered bail dispatch.
    pub fn bail(
        &self,
        name: impl Into<String>,
        args: impl IntoIterator<Item = EventValue>,
    ) -> EventResult {
        let event = Event::new(name, args);
        for callback in self.dispatch(DispatchMode::Bail, &event) {
            let result = block_on(callback(event.clone()))?;
            if is_bailed(&result) {
                return Ok(result);
            }
        }
        Ok(None)
    }

    /// Compose listeners around an innermost asynchronous callback.
    pub async fn waterfall_async<F, Fut>(
        &self,
        name: impl Into<String>,
        args: impl IntoIterator<Item = EventValue>,
        inner: F,
    ) -> EventResult
    where
        F: Fn() -> Fut + Send + Sync + 'static,
        Fut: Future<Output = EventResult> + Send + 'static,
    {
        let event = Event::new(name, args);
        let callbacks = self.dispatch(DispatchMode::Waterfall, &event);
        let inner = Arc::new(inner);
        let mut next: Next = Arc::new(move || Box::pin(inner()));
        for callback in callbacks.into_iter().rev() {
            let previous = next.clone();
            let event = event.clone();
            next = Arc::new(move || {
                let callback = callback.clone();
                let current = event.clone().with_next(previous.clone());
                callback(current)
            });
        }
        next().await
    }

    /// Synchronous waterfall convenience wrapper.
    pub fn waterfall<F>(
        &self,
        name: impl Into<String>,
        args: impl IntoIterator<Item = EventValue>,
        inner: F,
    ) -> EventResult
    where
        F: Fn() -> EventResult + Send + Sync + 'static,
    {
        block_on(self.waterfall_async(name, args, move || {
            let result = inner();
            async move { result }
        }))
    }

    /// Remove every event hook. Primarily useful for diagnostics/tests; normal
    /// cleanup should dispose the owning fiber.
    pub fn clear(&self) {
        lock(&self.ctx.root.events.state).hooks.clear();
    }
}