contextvm-sdk 0.2.0

Rust SDK for the ContextVM protocol — MCP over Nostr
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Producer-side writer for a CEP-41 open stream.
//!
//! Ports `sdk/src/transport/open-stream/writer.ts`. A tool emits an ordered
//! sequence of `chunk` frames (plus keepalive `ping`/`pong` and a terminal
//! `close`/`abort`) through an injected publish closure.
//!
//! Serialization design: `write`/`close`/`ping`/`pong`
//! hold a `tokio::sync::Mutex` across their publish `.await`, so **call order ==
//! wire order** natively (each op increments `progress`/`chunkIndex` under the
//! lock). The liveness flag lives in a **separate `AtomicBool` outside that
//! lock**, so [`abort`](OpenStreamWriter::abort) can claim the terminal
//! transition and publish without queueing behind a stuck `write`.
//!
//! The handle is `Arc`-backed and `Clone` so it can be inserted into the rmcp
//! request `extensions` typemap (`T: Clone + Send + Sync + 'static`) when the
//! server transport wiring lands.

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;

use futures::future::BoxFuture;
use tokio::sync::Mutex;

use super::frame::OpenStreamFrame;
use super::session::PublishFrame;

/// Lifecycle hook fired after a terminal `close` frame is published.
///
/// Currently inert; the server transport will wire it to flush a deferred final response.
pub type OnCloseHook = Arc<dyn Fn() -> BoxFuture<'static, ()> + Send + Sync>;

/// Lifecycle hook fired after a terminal `abort` frame is published (with the
/// advisory reason).
///
/// Currently inert; the server transport will wire it to flush a deferred final response.
pub type OnAbortHook = Arc<dyn Fn(Option<String>) -> BoxFuture<'static, ()> + Send + Sync>;

/// Construction options for an [`OpenStreamWriter`].
pub struct OpenStreamWriterOptions {
    /// The stream id (stringified `progressToken`).
    pub progress_token: String,
    /// Outbound publisher (same seam as the session).
    pub publish_frame: PublishFrame,
    /// Optional advisory `start` content type (writer-settable, receiver-ignored).
    pub content_type: Option<String>,
    /// Fired after a terminal `close` frame is published.
    pub on_close: Option<OnCloseHook>,
    /// Fired after a terminal `abort` frame is published.
    pub on_abort: Option<OnAbortHook>,
}

/// State mutated only under the op `Mutex` (serialized publishes).
struct WriterOpState {
    /// Next `chunkIndex` to assign (touched only by `write`).
    chunk_index: u64,
    /// Control-frame nonce counter (touched only by `ping`).
    control_nonce: u64,
}

struct WriterInner {
    progress_token: String,
    content_type: Option<String>,
    publish_frame: PublishFrame,
    on_close: Option<OnCloseHook>,
    on_abort: Option<OnAbortHook>,
    /// Serializes `write`/`close`/`ping`/`pong` so call order == wire order.
    op: Mutex<WriterOpState>,
    /// Monotonic outer `progress`, shared so `abort` can mint one without the op
    /// lock. Frames use `fetch_add(1) + 1` → 1, 2, 3, …
    progress: AtomicU64,
    /// Liveness flag, **outside** the op lock so `abort` never blocks on a stuck
    /// write. `false` once closed or aborted.
    active: AtomicBool,
    /// Whether the lazy `start` frame has been published.
    started: AtomicBool,
}

/// Minimal CEP-41 producer/writer.
#[derive(Clone)]
pub struct OpenStreamWriter {
    inner: Arc<WriterInner>,
}

impl OpenStreamWriter {
    /// Create a new writer from explicit options.
    pub fn new(options: OpenStreamWriterOptions) -> Self {
        Self {
            inner: Arc::new(WriterInner {
                progress_token: options.progress_token,
                content_type: options.content_type,
                publish_frame: options.publish_frame,
                on_close: options.on_close,
                on_abort: options.on_abort,
                op: Mutex::new(WriterOpState {
                    chunk_index: 0,
                    control_nonce: 0,
                }),
                progress: AtomicU64::new(0),
                active: AtomicBool::new(true),
                started: AtomicBool::new(false),
            }),
        }
    }

    /// The stream id (stringified `progressToken`).
    pub fn progress_token(&self) -> &str {
        &self.inner.progress_token
    }

    /// Whether the writer is still live (not yet closed/aborted).
    ///
    /// `true` for any freshly-created writer; see [`has_started`](Self::has_started).
    pub fn is_active(&self) -> bool {
        self.inner.active.load(Ordering::SeqCst)
    }

    /// Whether the writer has begun streaming by publishing its `start` frame.
    ///
    /// Distinct from [`is_active`](Self::is_active): used to tell apart writers a
    /// tool actually streams through from ones created only because the request
    /// carried a progress token (the response-deferral guard).
    pub fn has_started(&self) -> bool {
        self.inner.started.load(Ordering::SeqCst)
    }

    /// Mint the next monotonic outer `progress` value (1, 2, 3, …).
    fn next_progress(&self) -> u64 {
        self.inner.progress.fetch_add(1, Ordering::SeqCst) + 1
    }

    /// Publish the lazy `start` frame on first use. Caller MUST hold the op lock.
    /// On a publish failure `started` stays `false`, so a later op retries.
    async fn start_internal(&self) -> crate::Result<()> {
        if self.inner.started.load(Ordering::SeqCst) || !self.inner.active.load(Ordering::SeqCst) {
            return Ok(());
        }
        let notification = OpenStreamFrame::Start {
            content_type: self.inner.content_type.clone(),
        }
        .into_progress_notification(
            &self.inner.progress_token,
            self.next_progress(),
            None,
        )?;
        (self.inner.publish_frame)(notification).await?;
        self.inner.started.store(true, Ordering::SeqCst);
        Ok(())
    }

    /// Explicitly publish the `start` frame (idempotent; lazy on first `write`).
    pub async fn start(&self) -> crate::Result<()> {
        let _op = self.inner.op.lock().await;
        self.start_internal().await
    }

    /// Publish one ordered `chunk` frame, starting the stream lazily.
    pub async fn write(&self, data: String) -> crate::Result<()> {
        let mut op = self.inner.op.lock().await;
        self.start_internal().await?;
        if !self.inner.active.load(Ordering::SeqCst) {
            return Ok(());
        }
        let progress = self.next_progress();
        let chunk_index = op.chunk_index;
        let notification = OpenStreamFrame::Chunk { chunk_index, data }
            .into_progress_notification(&self.inner.progress_token, progress, None)?;
        (self.inner.publish_frame)(notification).await?;
        op.chunk_index += 1;
        Ok(())
    }

    /// Publish a keepalive `ping` carrying a fresh `{token}:{n}` nonce.
    pub async fn ping(&self) -> crate::Result<()> {
        let mut op = self.inner.op.lock().await;
        if !self.inner.active.load(Ordering::SeqCst) {
            return Ok(());
        }
        op.control_nonce += 1;
        let nonce = format!("{}:{}", self.inner.progress_token, op.control_nonce);
        let progress = self.next_progress();
        let notification = OpenStreamFrame::Ping { nonce }.into_progress_notification(
            &self.inner.progress_token,
            progress,
            None,
        )?;
        (self.inner.publish_frame)(notification).await?;
        Ok(())
    }

    /// Publish a `pong` echoing the peer's `ping` nonce.
    pub async fn pong(&self, nonce: String) -> crate::Result<()> {
        let _op = self.inner.op.lock().await;
        if !self.inner.active.load(Ordering::SeqCst) {
            return Ok(());
        }
        let progress = self.next_progress();
        let notification = OpenStreamFrame::Pong { nonce }.into_progress_notification(
            &self.inner.progress_token,
            progress,
            None,
        )?;
        (self.inner.publish_frame)(notification).await?;
        Ok(())
    }

    /// Close the stream gracefully. Declares `lastChunkIndex` iff any chunks were
    /// written. Runs [`on_close`](OpenStreamWriterOptions::on_close) **after** the
    /// frame is published (even on publish failure); propagates the publish error.
    pub async fn close(&self) -> crate::Result<()> {
        let op = self.inner.op.lock().await;
        self.start_internal().await?;
        // Claim the terminal transition atomically; lose to a racing abort.
        if !self.inner.active.swap(false, Ordering::SeqCst) {
            return Ok(());
        }
        let last_chunk_index = if op.chunk_index > 0 {
            Some(op.chunk_index - 1)
        } else {
            None
        };
        let notification = OpenStreamFrame::Close { last_chunk_index }.into_progress_notification(
            &self.inner.progress_token,
            self.next_progress(),
            None,
        )?;
        let publish_result = (self.inner.publish_frame)(notification).await;
        if let Some(hook) = &self.inner.on_close {
            hook().await;
        }
        publish_result.map(|_| ())
    }

    /// Abort the stream (terminal). Claims the terminal transition without the op
    /// lock, so it never waits on a stuck `write`. Runs
    /// [`on_abort`](OpenStreamWriterOptions::on_abort) **after** the frame is
    /// published (even on publish failure); propagates the publish error.
    /// Idempotent: a second `abort` (or one after `close`) is a no-op.
    pub async fn abort(&self, reason: Option<String>) -> crate::Result<()> {
        // Claim the terminal transition; no op lock → never blocks on a write.
        if !self.inner.active.swap(false, Ordering::SeqCst) {
            return Ok(());
        }
        let notification = OpenStreamFrame::Abort {
            reason: reason.clone(),
        }
        .into_progress_notification(
            &self.inner.progress_token,
            self.next_progress(),
            None,
        )?;
        let publish_result = (self.inner.publish_frame)(notification).await;
        if let Some(hook) = &self.inner.on_abort {
            hook(reason).await;
        }
        publish_result.map(|_| ())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::types::JsonRpcNotification;
    use crate::Error;
    use nostr_sdk::prelude::EventId;
    use std::sync::Mutex as StdMutex;

    type FrameLog = Arc<StdMutex<Vec<JsonRpcNotification>>>;

    fn frame_log() -> FrameLog {
        Arc::new(StdMutex::new(Vec::new()))
    }

    /// A publish closure that records every frame and returns a dummy id.
    fn recording_publisher(log: FrameLog) -> PublishFrame {
        Arc::new(move |frame: JsonRpcNotification| {
            let log = log.clone();
            Box::pin(async move {
                log.lock().unwrap().push(frame);
                Ok(EventId::all_zeros())
            })
        })
    }

    fn frame_type(notification: &JsonRpcNotification) -> String {
        notification.params.as_ref().unwrap()["cvm"]["frameType"]
            .as_str()
            .unwrap()
            .to_string()
    }

    fn frame_types(log: &FrameLog) -> Vec<String> {
        log.lock().unwrap().iter().map(frame_type).collect()
    }

    fn progress_of(notification: &JsonRpcNotification) -> u64 {
        notification.params.as_ref().unwrap()["progress"]
            .as_u64()
            .unwrap()
    }

    fn cvm(notification: &JsonRpcNotification) -> &serde_json::Value {
        &notification.params.as_ref().unwrap()["cvm"]
    }

    fn writer_with(log: FrameLog) -> OpenStreamWriter {
        OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "tok".to_string(),
            publish_frame: recording_publisher(log),
            content_type: None,
            on_close: None,
            on_abort: None,
        })
    }

    #[tokio::test]
    async fn has_started_reflects_start_or_chunk_frame() {
        let log = frame_log();
        let writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-started".to_string(),
            publish_frame: recording_publisher(log),
            content_type: None,
            on_close: None,
            on_abort: None,
        });

        assert!(writer.is_active());
        assert!(!writer.has_started());

        // Control frames do not start the stream.
        writer.ping().await.unwrap();
        writer.pong("nonce".to_string()).await.unwrap();
        assert!(!writer.has_started());

        writer.write("hello".to_string()).await.unwrap();
        assert!(writer.has_started());
    }

    #[tokio::test]
    async fn has_started_after_explicit_start() {
        let log = frame_log();
        let writer = writer_with(log.clone());
        assert!(!writer.has_started());

        writer.start().await.unwrap();

        assert!(writer.has_started());
        assert_eq!(frame_types(&log), vec!["start"]);
    }

    #[tokio::test]
    async fn emits_ping_and_pong_with_matching_nonces() {
        let log = frame_log();
        let writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-keepalive".to_string(),
            publish_frame: recording_publisher(log.clone()),
            content_type: None,
            on_close: None,
            on_abort: None,
        });

        writer.start().await.unwrap();
        writer.ping().await.unwrap();
        writer.pong("keepalive-nonce".to_string()).await.unwrap();

        let frames = log.lock().unwrap();
        assert_eq!(frames.len(), 3);
        assert_eq!(progress_of(&frames[1]), 2);
        assert_eq!(cvm(&frames[1])["frameType"], "ping");
        assert_eq!(cvm(&frames[1])["nonce"], "token-keepalive:1");
        assert_eq!(progress_of(&frames[2]), 3);
        assert_eq!(cvm(&frames[2])["frameType"], "pong");
        assert_eq!(cvm(&frames[2])["nonce"], "keepalive-nonce");
    }

    #[tokio::test]
    async fn close_omits_last_chunk_index_when_no_chunks() {
        let log = frame_log();
        let writer = writer_with(log.clone());
        writer.close().await.unwrap();

        let frames = log.lock().unwrap();
        assert_eq!(frames.len(), 2);
        assert_eq!(frame_type(&frames[1]), "close");
        assert!(!cvm(&frames[1])
            .as_object()
            .unwrap()
            .contains_key("lastChunkIndex"));
    }

    #[tokio::test]
    async fn close_includes_last_chunk_index_after_chunks() {
        let log = frame_log();
        let writer = writer_with(log.clone());
        writer.write("hello".to_string()).await.unwrap();
        writer.write("world".to_string()).await.unwrap();
        writer.close().await.unwrap();

        let frames = log.lock().unwrap();
        assert_eq!(frames.len(), 4);
        assert_eq!(frame_type(&frames[3]), "close");
        assert_eq!(cvm(&frames[3])["lastChunkIndex"], 1);
    }

    #[tokio::test]
    async fn lifecycle_hooks_fire_after_terminal_frames() {
        let lifecycle = Arc::new(StdMutex::new(Vec::<String>::new()));
        let log = frame_log();

        let lc = lifecycle.clone();
        let on_close: OnCloseHook = Arc::new(move || {
            let lc = lc.clone();
            Box::pin(async move {
                lc.lock().unwrap().push("close".to_string());
            })
        });
        let writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-hooks".to_string(),
            publish_frame: recording_publisher(log.clone()),
            content_type: None,
            on_close: Some(on_close),
            on_abort: None,
        });
        writer.close().await.unwrap();
        assert_eq!(frame_type(log.lock().unwrap().last().unwrap()), "close");
        assert_eq!(*lifecycle.lock().unwrap(), vec!["close"]);

        let lc = lifecycle.clone();
        let on_abort: OnAbortHook = Arc::new(move |reason: Option<String>| {
            let lc = lc.clone();
            Box::pin(async move {
                lc.lock()
                    .unwrap()
                    .push(format!("abort:{}", reason.unwrap_or_default()));
            })
        });
        let abort_writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-hooks-abort".to_string(),
            publish_frame: recording_publisher(log.clone()),
            content_type: None,
            on_close: None,
            on_abort: Some(on_abort),
        });
        abort_writer.abort(Some("done".to_string())).await.unwrap();
        let frames = log.lock().unwrap();
        assert_eq!(frame_type(frames.last().unwrap()), "abort");
        assert_eq!(cvm(frames.last().unwrap())["reason"], "done");
        assert_eq!(*lifecycle.lock().unwrap(), vec!["close", "abort:done"]);
    }

    #[tokio::test]
    async fn publishes_abort_before_running_abort_hook() {
        let events = Arc::new(StdMutex::new(Vec::<String>::new()));

        let ev = events.clone();
        let publish: PublishFrame = Arc::new(move |frame: JsonRpcNotification| {
            let ev = ev.clone();
            Box::pin(async move {
                ev.lock()
                    .unwrap()
                    .push(format!("publish:{}", frame_type(&frame)));
                Ok(EventId::all_zeros())
            })
        });
        let ev = events.clone();
        let on_abort: OnAbortHook = Arc::new(move |reason: Option<String>| {
            let ev = ev.clone();
            Box::pin(async move {
                ev.lock()
                    .unwrap()
                    .push(format!("abort:{}", reason.unwrap_or_default()));
            })
        });
        let writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-abort-order".to_string(),
            publish_frame: publish,
            content_type: None,
            on_close: None,
            on_abort: Some(on_abort),
        });

        writer.abort(Some("ordered".to_string())).await.unwrap();
        assert_eq!(
            *events.lock().unwrap(),
            vec!["publish:abort", "abort:ordered"]
        );
    }

    #[tokio::test]
    async fn retries_start_when_first_start_publish_fails() {
        let log = frame_log();
        let fail_start = Arc::new(AtomicBool::new(true));

        let f = log.clone();
        let fs = fail_start.clone();
        let publish: PublishFrame = Arc::new(move |frame: JsonRpcNotification| {
            let f = f.clone();
            let fs = fs.clone();
            Box::pin(async move {
                if frame_type(&frame) == "start" && fs.swap(false, Ordering::SeqCst) {
                    return Err(Error::Transport("relay unavailable".to_string()));
                }
                f.lock().unwrap().push(frame);
                Ok(EventId::all_zeros())
            })
        });
        let writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-start-retry".to_string(),
            publish_frame: publish,
            content_type: None,
            on_close: None,
            on_abort: None,
        });

        assert!(writer.start().await.is_err());
        writer.write("hello".to_string()).await.unwrap();
        assert_eq!(frame_types(&log), vec!["start", "chunk"]);
    }

    #[tokio::test]
    async fn runs_close_cleanup_when_close_publish_fails() {
        let lifecycle = Arc::new(StdMutex::new(Vec::<String>::new()));
        let publish: PublishFrame = Arc::new(move |frame: JsonRpcNotification| {
            Box::pin(async move {
                if frame_type(&frame) == "close" {
                    return Err(Error::Transport("close publish failed".to_string()));
                }
                Ok(EventId::all_zeros())
            })
        });
        let lc = lifecycle.clone();
        let on_close: OnCloseHook = Arc::new(move || {
            let lc = lc.clone();
            Box::pin(async move {
                lc.lock().unwrap().push("close".to_string());
            })
        });
        let writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-close-fail".to_string(),
            publish_frame: publish,
            content_type: None,
            on_close: Some(on_close),
            on_abort: None,
        });

        assert!(writer.close().await.is_err());
        assert!(!writer.is_active());
        assert_eq!(*lifecycle.lock().unwrap(), vec!["close"]);
    }

    #[tokio::test]
    async fn runs_abort_cleanup_when_abort_publish_fails() {
        let lifecycle = Arc::new(StdMutex::new(Vec::<String>::new()));
        let publish: PublishFrame = Arc::new(move |frame: JsonRpcNotification| {
            Box::pin(async move {
                if frame_type(&frame) == "abort" {
                    return Err(Error::Transport("abort publish failed".to_string()));
                }
                Ok(EventId::all_zeros())
            })
        });
        let lc = lifecycle.clone();
        let on_abort: OnAbortHook = Arc::new(move |reason: Option<String>| {
            let lc = lc.clone();
            Box::pin(async move {
                lc.lock()
                    .unwrap()
                    .push(format!("abort:{}", reason.unwrap_or_default()));
            })
        });
        let writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-abort-fail".to_string(),
            publish_frame: publish,
            content_type: None,
            on_close: None,
            on_abort: Some(on_abort),
        });

        assert!(writer.abort(Some("cleanup".to_string())).await.is_err());
        assert!(!writer.is_active());
        assert_eq!(*lifecycle.lock().unwrap(), vec!["abort:cleanup"]);
    }

    #[tokio::test]
    async fn abort_deactivates_without_waiting_for_a_stuck_write() {
        let lifecycle = Arc::new(StdMutex::new(Vec::<String>::new()));
        let reached = Arc::new(tokio::sync::Notify::new());
        let gate = Arc::new(tokio::sync::Notify::new());

        let r = reached.clone();
        let g = gate.clone();
        let publish: PublishFrame = Arc::new(move |frame: JsonRpcNotification| {
            let r = r.clone();
            let g = g.clone();
            Box::pin(async move {
                if frame_type(&frame) == "chunk" {
                    // Signal that the write has reached the (stuck) chunk publish,
                    // then block until released.
                    r.notify_one();
                    g.notified().await;
                }
                Ok(EventId::all_zeros())
            })
        });
        let lc = lifecycle.clone();
        let on_abort: OnAbortHook = Arc::new(move |reason: Option<String>| {
            let lc = lc.clone();
            Box::pin(async move {
                lc.lock()
                    .unwrap()
                    .push(format!("abort:{}", reason.unwrap_or_default()));
            })
        });
        let writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-stuck".to_string(),
            publish_frame: publish,
            content_type: None,
            on_close: None,
            on_abort: Some(on_abort),
        });

        let writer2 = writer.clone();
        let write_handle = tokio::spawn(async move { writer2.write("hello".to_string()).await });

        // Wait until the write is parked inside the stuck chunk publish.
        reached.notified().await;

        // Abort must complete without acquiring the op lock the stuck write holds.
        writer
            .abort(Some("stuck publish".to_string()))
            .await
            .unwrap();
        assert!(!writer.is_active());
        assert_eq!(*lifecycle.lock().unwrap(), vec!["abort:stuck publish"]);

        // Release the stuck write so the spawned task can finish cleanly.
        gate.notify_one();
        write_handle.await.unwrap().unwrap();
    }

    #[tokio::test]
    async fn serializes_concurrent_writes_before_close() {
        let log = frame_log();
        let f = log.clone();
        let publish: PublishFrame = Arc::new(move |frame: JsonRpcNotification| {
            let f = f.clone();
            Box::pin(async move {
                // A small delay on chunks would expose any interleaving if the op
                // lock did not serialize build+publish.
                if frame_type(&frame) == "chunk" {
                    tokio::time::sleep(std::time::Duration::from_millis(5)).await;
                }
                f.lock().unwrap().push(frame);
                Ok(EventId::all_zeros())
            })
        });
        let writer = OpenStreamWriter::new(OpenStreamWriterOptions {
            progress_token: "token-concurrent".to_string(),
            publish_frame: publish,
            content_type: None,
            on_close: None,
            on_abort: None,
        });

        let (a, b, c) = tokio::join!(
            writer.write("hello".to_string()),
            writer.write("world".to_string()),
            writer.close()
        );
        a.unwrap();
        b.unwrap();
        c.unwrap();

        let frames = log.lock().unwrap();
        let types: Vec<String> = frames.iter().map(frame_type).collect();
        assert_eq!(types, vec!["start", "chunk", "chunk", "close"]);
        assert_eq!(progress_of(&frames[1]), 2);
        assert_eq!(cvm(&frames[1])["chunkIndex"], 0);
        assert_eq!(cvm(&frames[1])["data"], "hello");
        assert_eq!(progress_of(&frames[2]), 3);
        assert_eq!(cvm(&frames[2])["chunkIndex"], 1);
        assert_eq!(cvm(&frames[2])["data"], "world");
        assert_eq!(progress_of(&frames[3]), 4);
        assert_eq!(cvm(&frames[3])["lastChunkIndex"], 1);
    }
}