golem-rust 3.0.0

Golem Rust tooling library that facilitates writing Golem backends in Rust
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
// Copyright 2026 Golem Cloud
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

//! External Durable Streams clients. The host owns HTTP, framing and authentication;
//! these clients retain only replay-reconstructed checkpoints, buffers and producer state.
//! Each client owns one `golem:agent/durable-streams@2.0.0` resource. Construction
//! journals its immutable descriptor without HTTP; dropping the client releases it.
//!
//! Appends use the current Golem idempotence policy unchanged. With idempotence disabled,
//! recovery of an interrupted POST can fail closed. Exactly-once effects require the peer
//! to atomically retain producer deduplication state. Golem forks copy producer identity
//! and pending data; divergent branches are not independent external producers.

use crate::bindings::golem::agent::durable_streams as wire;
use crate::schema::wit::wire::Secret;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::value::RawValue;
use std::sync::Arc;

#[cfg(test)]
use tests::resources as calls;
#[cfg(not(test))]
use wire as calls;

pub use wire::{
    DurableStreamAppendReceipt as AppendReceipt, DurableStreamCheckpoint as Checkpoint,
    DurableStreamErrorKind as ErrorKind, DurableStreamTransport as Transport,
};

/// Producer identity and acknowledged sequence progress, reconstructed by replay.
#[derive(Clone, Debug)]
pub struct Producer {
    pub id: String,
    pub epoch: u64,
    pub sequence: u64,
}

/// A host protocol failure or a local codec/state error. Errors never mean EOF.
#[derive(Debug)]
pub struct Error {
    pub kind: ErrorKind,
    pub message: String,
    pub retry_after_ms: Option<u64>,
    pub producer_epoch: Option<u64>,
    pub expected_sequence: Option<u64>,
}

impl Error {
    fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),
            retry_after_ms: None,
            producer_epoch: None,
            expected_sequence: None,
        }
    }
}

impl From<wire::DurableStreamError> for Error {
    fn from(value: wire::DurableStreamError) -> Self {
        Self {
            kind: value.kind,
            message: value.message,
            retry_after_ms: value.retry_after_ms,
            producer_epoch: value.producer_epoch,
            expected_sequence: value.expected_sequence,
        }
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}: {}", self.kind, self.message)
    }
}
impl std::error::Error for Error {}

/// Consecutive failure budget, independent of each HTTP attempt's deadline.
#[derive(Clone, Debug)]
pub struct RetryOptions {
    pub max_retries: u32,
    pub initial_delay_ms: u64,
    pub max_delay_ms: u64,
}

impl Default for RetryOptions {
    fn default() -> Self {
        Self {
            max_retries: 8,
            initial_delay_ms: 100,
            max_delay_ms: 30_000,
        }
    }
}

impl RetryOptions {
    fn delay(&self, error: &Error, failures: u32) -> Option<u64> {
        if failures >= self.max_retries
            || !matches!(
                error.kind,
                ErrorKind::Timeout
                    | ErrorKind::Transport
                    | ErrorKind::RateLimited
                    | ErrorKind::Unavailable
            )
        {
            return None;
        }
        let backoff = self
            .initial_delay_ms
            .saturating_mul(1u64 << failures.min(63))
            .min(self.max_delay_ms);
        Some(backoff.max(error.retry_after_ms.unwrap_or(0)))
    }
}

async fn wait(delay_ms: u64) {
    #[cfg(not(test))]
    crate::wasip3::clocks::monotonic_clock::wait_for(delay_ms.saturating_mul(1_000_000)).await;
    #[cfg(test)]
    tests::WAITS.with_borrow_mut(|waits| waits.push(delay_ms));
}

#[derive(Clone, Debug)]
pub struct ReadOptions {
    pub checkpoint: Checkpoint,
    /// Transport used after catch-up reaches the tail. The initial call is always catch-up.
    pub transport: Transport,
    pub timeout_ms: u64,
    pub idle_delay_ms: u64,
    pub retry: RetryOptions,
    /// A secret capability, never its revealed value. Shared between independent readers/writers.
    pub auth: Option<Arc<Secret>>,
}

impl Default for ReadOptions {
    fn default() -> Self {
        Self {
            checkpoint: Checkpoint {
                offset: "-1".into(),
                cursor: None,
            },
            transport: Transport::LongPoll,
            timeout_ms: 30_000,
            idle_delay_ms: 100,
            retry: RetryOptions::default(),
            auth: None,
        }
    }
}

enum Items {
    Json(Vec<Box<RawValue>>),
    Bytes(Vec<u8>),
}

impl Items {
    fn len(&self) -> usize {
        match self {
            Self::Json(items) => items.len(),
            Self::Bytes(items) => items.len(),
        }
    }
}

struct Batch {
    items: Items,
    index: usize,
    next: Checkpoint,
    up_to_date: bool,
    closed: bool,
}

/// A single-reader, one-batch-buffered external source. `&mut self` serializes reads.
/// A delivered batch is drained before its opaque checkpoint is promoted.
/// Dropping a pending `next` cancels that attempt without advancing the checkpoint.
/// Live readers are reconstructed by replay, not serialized as native stream handles.
pub struct ExternalDurableStream<T> {
    reader: calls::DurableStreamReader,
    mode: wire::DurableStreamMode,
    request: wire::DurableStreamReadRequest,
    options: ReadOptions,
    decode: fn(&Items, usize) -> Result<T, Error>,
    pending: Option<Batch>,
    closed: bool,
    failures: u32,
    delay_ms: Option<u64>,
}

impl<T: DeserializeOwned> ExternalDurableStream<T> {
    pub fn json(url: impl Into<String>, options: ReadOptions) -> Self {
        Self::new(
            url.into(),
            options,
            wire::DurableStreamMode::Json,
            |items, index| {
                let Items::Json(items) = items else {
                    unreachable!()
                };
                serde_json::from_str(items[index].get()).map_err(|e| {
                    Error::new(
                        ErrorKind::ProtocolError,
                        format!("JSON item decode failed: {e}"),
                    )
                })
            },
        )
    }
}

impl ExternalDurableStream<u8> {
    /// Bytes are a sequence, not the original append chunk boundaries.
    pub fn bytes(url: impl Into<String>, options: ReadOptions) -> Self {
        Self::new(
            url.into(),
            options,
            wire::DurableStreamMode::Bytes,
            |items, index| {
                let Items::Bytes(items) = items else {
                    unreachable!()
                };
                Ok(items[index])
            },
        )
    }
}

impl<T> ExternalDurableStream<T> {
    fn new(
        url: String,
        mut options: ReadOptions,
        mode: wire::DurableStreamMode,
        decode: fn(&Items, usize) -> Result<T, Error>,
    ) -> Self {
        let reader = calls::DurableStreamReader::new(
            &wire::DurableStreamReaderOptions {
                url,
                mode,
                timeout_ms: options.timeout_ms,
            },
            options.auth.as_deref(),
        );
        options.auth = None;
        Self {
            reader,
            mode,
            request: wire::DurableStreamReadRequest {
                checkpoint: options.checkpoint.clone(),
                transport: Transport::CatchUp,
                content_type: None,
            },
            options,
            decode,
            pending: None,
            closed: false,
            failures: 0,
            delay_ms: None,
        }
    }

    /// Last fully drained checkpoint. A partially consumed batch retains its original offset.
    pub fn checkpoint(&self) -> &Checkpoint {
        &self.request.checkpoint
    }

    /// Stop pulling and discard buffered items. To cancel an active pull, drop its future first.
    pub fn close(&mut self) {
        self.closed = true;
        self.pending = None;
    }

    fn install(&mut self, batch: wire::DurableStreamBatch) -> Result<(), Error> {
        if batch.next.offset == "now" {
            return Err(Error::new(
                ErrorKind::ProtocolError,
                "server did not resolve now",
            ));
        }
        let items = match self.mode {
            wire::DurableStreamMode::Json => Items::Json(if batch.payload.is_empty() {
                Vec::new()
            } else {
                serde_json::from_slice(&batch.payload).map_err(|e| {
                    Error::new(
                        ErrorKind::ProtocolError,
                        format!("JSON batch decode failed: {e}"),
                    )
                })?
            }),
            wire::DurableStreamMode::Bytes => Items::Bytes(batch.payload),
        };
        // The host validates MIME equivalence against the original pin.
        self.request.content_type.get_or_insert(batch.content_type);
        self.pending = Some(Batch {
            items,
            index: 0,
            next: batch.next,
            up_to_date: batch.up_to_date,
            closed: batch.closed,
        });
        self.failures = 0;
        Ok(())
    }

    pub async fn next(&mut self) -> Result<Option<T>, Error> {
        loop {
            if self.closed {
                return Ok(None);
            }
            if let Some(batch) = &mut self.pending {
                if batch.index < batch.items.len() {
                    let item = (self.decode)(&batch.items, batch.index)?;
                    batch.index += 1;
                    return Ok(Some(item));
                }
                let batch = self.pending.take().unwrap();
                self.request.checkpoint = batch.next;
                self.closed = batch.closed;
                self.request.transport = if batch.up_to_date {
                    self.options.transport
                } else {
                    Transport::CatchUp
                };
                if batch.items.len() == 0 && batch.up_to_date && !batch.closed {
                    self.delay_ms = Some(self.options.idle_delay_ms);
                }
                continue;
            }
            if let Some(delay) = self.delay_ms {
                wait(delay).await;
                self.delay_ms = None;
            }
            match self.reader.read(self.request.clone()).await {
                Ok(batch) => self.install(batch)?,
                Err(error) => {
                    let error = Error::from(error);
                    let Some(delay) = self.options.retry.delay(&error, self.failures) else {
                        return Err(error);
                    };
                    self.failures += 1;
                    self.delay_ms = Some(delay);
                }
            }
        }
    }

    pub async fn collect(mut self) -> Result<Vec<T>, Error> {
        let mut items = Vec::new();
        while let Some(item) = self.next().await? {
            items.push(item);
        }
        Ok(items)
    }
}

#[cfg(feature = "export_golem_agentic")]
impl<T: crate::IntoSchema + crate::FromSchema + Send + 'static> ExternalDurableStream<T> {
    /// Preserve ordinary native stream schemas, including nested values and RPC inputs.
    /// Local native reads return String errors; a forwarded producer failure traps, never EOF.
    pub fn into_agent_stream(self) -> crate::agentic::AgentStream<T> {
        crate::agentic::AgentStream::from_schema_stream(
            crate::schema::SchemaValueStream::from_source(self),
            |value| T::from_value(&value).map_err(|e| e.to_string()),
        )
    }
}

#[cfg(feature = "export_golem_agentic")]
impl<T: crate::IntoSchema + Send + 'static> crate::schema::stream::SchemaValueStreamSource
    for ExternalDurableStream<T>
{
    fn next(
        &mut self,
    ) -> std::pin::Pin<
        Box<
            dyn std::future::Future<
                    Output = Result<Option<crate::schema::wit::wire::SchemaValueTree>, String>,
                > + Send
                + '_,
        >,
    > {
        Box::pin(async move {
            match ExternalDurableStream::next(self)
                .await
                .map_err(|e| e.to_string())?
            {
                Some(value) => crate::schema::wit::encode_value_async(&value.to_value())
                    .await
                    .map(Some)
                    .map_err(|e| e.to_string()),
                None => Ok(None),
            }
        })
    }
}

#[derive(Clone, Debug)]
pub struct WriteOptions {
    /// None generates one durable identity at writer construction.
    pub producer_id: Option<String>,
    pub epoch: u64,
    pub timeout_ms: u64,
    pub retry: RetryOptions,
    pub auth: Option<Arc<Secret>>,
}

impl Default for WriteOptions {
    fn default() -> Self {
        Self {
            producer_id: None,
            epoch: 0,
            timeout_ms: 30_000,
            retry: RetryOptions::default(),
            auth: None,
        }
    }
}

struct PendingAppend {
    request: wire::DurableStreamAppendRequest,
    failures: u32,
    delay_ms: Option<u64>,
}

/// A non-pipelining producer. A cancelled or failed append remains pending;
/// call `retry_pending` to resolve that exact payload before supplying new data.
/// Dropping this writer does not undo an external write.
pub struct DurableStreamWriter {
    writer: calls::DurableStreamWriter,
    producer: Producer,
    retry: RetryOptions,
    pending: Option<PendingAppend>,
    closed: bool,
}

const MAX_PRODUCER_NUMBER: u64 = (1u64 << 53) - 1;

impl DurableStreamWriter {
    pub fn new(
        url: impl Into<String>,
        content_type: impl Into<String>,
        options: WriteOptions,
    ) -> Result<Self, Error> {
        if options.epoch > MAX_PRODUCER_NUMBER {
            return Err(Error::new(
                ErrorKind::InvalidRequest,
                "producer epoch exceeds 2^53-1",
            ));
        }
        let id = options
            .producer_id
            .clone()
            .unwrap_or_else(|| crate::generate_idempotency_key().to_string());
        Ok(Self {
            writer: calls::DurableStreamWriter::new(
                &wire::DurableStreamWriterOptions {
                    url: url.into(),
                    content_type: content_type.into(),
                    producer_id: id.clone(),
                    producer_epoch: options.epoch,
                    timeout_ms: options.timeout_ms,
                },
                options.auth.as_deref(),
            ),
            producer: Producer {
                id,
                epoch: options.epoch,
                sequence: 0,
            },
            retry: options.retry,
            pending: None,
            closed: false,
        })
    }

    pub fn producer(&self) -> &Producer {
        &self.producer
    }
    pub fn has_pending(&self) -> bool {
        self.pending.is_some()
    }

    fn prepare(
        &mut self,
        payload: wire::DurableStreamAppendPayload,
        close: bool,
    ) -> Result<(), Error> {
        if self.pending.is_some() {
            return Err(Error::new(
                ErrorKind::SequenceConflict,
                "resolve the pending append before supplying new data",
            ));
        }
        if self.closed {
            return Err(Error::new(ErrorKind::Closed, "writer is closed"));
        }
        if self.producer.sequence > MAX_PRODUCER_NUMBER {
            return Err(Error::new(
                ErrorKind::InvalidRequest,
                "producer sequence exhausted",
            ));
        }
        let empty = match &payload {
            wire::DurableStreamAppendPayload::Json(v) => v.is_empty(),
            wire::DurableStreamAppendPayload::Bytes(v) => v.is_empty(),
        };
        if empty && !close {
            return Err(Error::new(
                ErrorKind::InvalidRequest,
                "empty append requires close",
            ));
        }
        self.pending = Some(PendingAppend {
            request: wire::DurableStreamAppendRequest {
                payload,
                sequence: self.producer.sequence,
                close,
            },
            failures: 0,
            delay_ms: None,
        });
        Ok(())
    }

    /// Each element is encoded separately; an array-valued element stays one message.
    pub async fn append_json<T: Serialize>(
        &mut self,
        values: &[T],
        close: bool,
    ) -> Result<AppendReceipt, Error> {
        let encoded = values
            .iter()
            .map(serde_json::to_string)
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| {
                Error::new(
                    ErrorKind::InvalidRequest,
                    format!("JSON encode failed: {e}"),
                )
            })?;
        self.prepare(wire::DurableStreamAppendPayload::Json(encoded), close)?;
        self.retry_pending().await
    }

    pub async fn append_bytes(
        &mut self,
        bytes: &[u8],
        close: bool,
    ) -> Result<AppendReceipt, Error> {
        self.prepare(
            wire::DurableStreamAppendPayload::Bytes(bytes.to_vec()),
            close,
        )?;
        self.retry_pending().await
    }

    /// Close-only consumes a sequence just like an append-and-close.
    pub async fn close(&mut self) -> Result<AppendReceipt, Error> {
        self.prepare(wire::DurableStreamAppendPayload::Bytes(Vec::new()), true)?;
        self.retry_pending().await
    }

    fn acknowledge(&mut self, receipt: &AppendReceipt) -> Result<(), Error> {
        let pending = self
            .pending
            .as_ref()
            .expect("acknowledgement requires a pending request");
        if receipt.epoch != self.producer.epoch || receipt.sequence < pending.request.sequence {
            return Err(Error::new(
                ErrorKind::ProtocolError,
                "invalid producer acknowledgement",
            ));
        }
        if receipt.sequence > pending.request.sequence {
            return Err(Error::new(
                ErrorKind::ProducerDiverged,
                "peer acknowledged a later producer sequence",
            ));
        }
        if pending.request.close && !receipt.closed {
            return Err(Error::new(
                ErrorKind::ProtocolError,
                "peer did not acknowledge closure",
            ));
        }
        self.producer.sequence = pending.request.sequence + 1;
        self.closed = receipt.closed;
        self.pending = None;
        Ok(())
    }

    pub async fn retry_pending(&mut self) -> Result<AppendReceipt, Error> {
        loop {
            let pending = self
                .pending
                .as_mut()
                .ok_or_else(|| Error::new(ErrorKind::InvalidRequest, "no pending append"))?;
            if let Some(delay) = pending.delay_ms {
                wait(delay).await;
                pending.delay_ms = None;
            }
            match self.writer.append(pending.request.clone()).await {
                Ok(receipt) => {
                    self.acknowledge(&receipt)?;
                    return Ok(receipt);
                }
                Err(error) => {
                    let error = Error::from(error);
                    let Some(delay) = self.retry.delay(&error, pending.failures) else {
                        return Err(error);
                    };
                    pending.failures += 1;
                    pending.delay_ms = Some(delay);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests;