heddle-thread-api 0.25.4

Native Thread clients and durable peer replication over Iroh
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
// SPDX-License-Identifier: Apache-2.0
//! Deliver bounded, committed changes; never expose half a snapshot as a view.
use api::v2::{
    ObservationAction, ObservationState, StreamProtocolError,
    client::{ClientError, MessageReader, Messages},
};

use crate::{contract::*, reopen::ReopenRetryable, transport};

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    Client(#[from] ClientError<transport::Error>),
    #[error(transparent)]
    Stream(#[from] StreamProtocolError),
    #[error("invalid observation: {0}")]
    Invalid(&'static str),
    #[error("observation interrupted; resume only from the last committed batch")]
    Interrupted,
    #[error("observation reset ({0}); start a replacement snapshot")]
    Reset(i32),
}

impl ReopenRetryable for Error {
    fn is_reopen_retryable(&self) -> bool {
        match self {
            Error::Client(error) => crate::reopen::client_error_is_reopen_retryable(error),
            _ => false,
        }
    }
}

/// Store this atomically with the view changed by its committed batch. A resume
/// token belongs to one authenticated endpoint and one exact request projection.
#[derive(Clone, Debug)]
pub struct Resume {
    pub(crate) cursor: Vec<u8>,
    binding: [u8; 32],
    source: EndpointRef,
    query: Vec<u8>,
}

// Local bookmark format, not a server cursor or a signed authority record.
#[derive(prost::Message)]
struct StoredResume {
    #[prost(uint32, tag = "1")]
    version: u32,
    #[prost(message, optional, tag = "2")]
    source: Option<EndpointRef>,
    #[prost(bytes = "vec", tag = "3")]
    binding: Vec<u8>,
    #[prost(bytes = "vec", tag = "4")]
    query: Vec<u8>,
    #[prost(bytes = "vec", tag = "5")]
    cursor: Vec<u8>,
}

impl Resume {
    pub fn encode(&self) -> Vec<u8> {
        prost::Message::encode_to_vec(&StoredResume {
            version: 1,
            source: Some(self.source.clone()),
            binding: self.binding.to_vec(),
            query: self.query.clone(),
            cursor: self.cursor.clone(),
        })
    }

    pub fn decode(bytes: &[u8]) -> Result<Self, Error> {
        if bytes.len() > 512 * 1024 {
            return Err(Error::Invalid("oversized observation bookmark"));
        }
        let stored: StoredResume = prost::Message::decode(bytes)
            .map_err(|_| Error::Invalid("malformed observation bookmark"))?;
        let source = stored
            .source
            .ok_or(Error::Invalid("missing bookmark source"))?;
        if stored.version != 1
            || source.public_key.len() != 32
            || !matches!(
                EndpointKind::try_from(source.kind),
                Ok(EndpointKind::Weft | EndpointKind::Device)
            )
            || stored.cursor.is_empty()
            || stored.cursor.len() > api::v2::MAX_CURSOR_BYTES
            || stored.query.is_empty()
        {
            return Err(Error::Invalid("invalid observation bookmark"));
        }
        let binding = stored
            .binding
            .try_into()
            .map_err(|_| Error::Invalid("invalid bookmark binding"))?;
        Ok(Self {
            cursor: stored.cursor,
            binding,
            source,
            query: stored.query,
        })
    }
}

pub type CommittedThreadBatch = CommittedBatch<thread_event::Payload>;
pub type CommittedAnalysisBatch = CommittedBatch<analysis_event::Payload>;

pub struct CommittedBatch<P> {
    pub replace: bool,
    pub changes: Vec<P>,
    pub page: Option<PageInfo>,
    pub resume: Resume,
}

pub(crate) fn validate_resume(
    resume: &Option<Resume>,
    description: &DescribeEndpointResponse,
    query: &[u8],
) -> Result<(), Error> {
    if resume
        .as_ref()
        .is_some_and(|r| Some(&r.source) != description.endpoint.as_ref() || r.query != query)
    {
        return Err(Error::Invalid(
            "resume belongs to a different source or projection",
        ));
    }
    Ok(())
}

pub fn budget(description: &DescribeEndpointResponse) -> Result<ReadBudget, Error> {
    let budget = description
        .default_read_budget
        .ok_or(Error::Invalid("missing default read budget"))?;
    if budget.max_items == 0
        || budget.max_frame_bytes == 0
        || budget.max_snapshot_bytes == 0
        || description.max_pending_batch_bytes == 0
    {
        return Err(Error::Invalid("unbounded endpoint budget"));
    }
    // Local ceilings apply even when a peer advertises excessive defaults.
    Ok(ReadBudget {
        max_items: budget.max_items.min(1000),
        max_frame_bytes: budget.max_frame_bytes.min(256 * 1024),
        max_snapshot_bytes: budget.max_snapshot_bytes.min(4 * 1024 * 1024),
    })
}

pub type ThreadObservation<R> = Observation<R, ThreadEvent>;
pub type AnalysisObservation<R> = Observation<R, AnalysisEvent>;

/// Typed payload access; the checkpoint/budget state machine is shared.
pub trait ObservedEvent: prost::Message + Default {
    type Payload;
    fn frame(&self) -> Option<&StreamFrame>;
    fn has_payload(&self) -> bool;
    fn take_payload(&mut self) -> Option<Self::Payload>;
    fn is_removal(&self) -> bool;
}

impl ObservedEvent for ThreadEvent {
    type Payload = thread_event::Payload;
    fn frame(&self) -> Option<&StreamFrame> {
        self.frame.as_ref()
    }
    fn has_payload(&self) -> bool {
        self.payload.is_some()
    }
    fn take_payload(&mut self) -> Option<Self::Payload> {
        self.payload.take()
    }
    fn is_removal(&self) -> bool {
        matches!(self.payload, Some(thread_event::Payload::Removal(_)))
    }
}
impl ObservedEvent for AnalysisEvent {
    type Payload = analysis_event::Payload;
    fn frame(&self) -> Option<&StreamFrame> {
        self.frame.as_ref()
    }
    fn has_payload(&self) -> bool {
        self.payload.is_some()
    }
    fn take_payload(&mut self) -> Option<Self::Payload> {
        self.payload.take()
    }
    fn is_removal(&self) -> bool {
        matches!(
            self.payload,
            Some(analysis_event::Payload::Removal(_) | analysis_event::Payload::BehaviorRemoval(_))
        )
    }
}

/// Request shapes with the common observation controls. Typed RPC selection still
/// comes from the contract; this trait never guesses a method from a payload.
pub trait ObservationRequest: prost::Message {
    fn options_mut(&mut self) -> &mut ObserveOptions;
}
macro_rules! observation_requests {
    ($($request:ty),+ $(,)?) => { $(
        impl ObservationRequest for $request {
            fn options_mut(&mut self) -> &mut ObserveOptions {
                self.observe.get_or_insert_default()
            }
        }
    )+ };
}
observation_requests!(
    ObserveThreadRequest,
    ObserveThreadsRequest,
    ObserveAnalysisRequest,
    ObserveIdentityRequest,
    ObservePairingRequest,
    ObserveOwnershipRequest,
    ObserveWorkspaceRequest,
    ObserveCatalogRequest,
    ObserveSpoolRequest,
    ObserveCollaborationRequest,
    ObserveCheckoutsRequest,
    ObserveRunsRequest,
    ObserveAttentionRequest,
    ObserveNotificationsRequest,
    ObserveOperationsRequest,
    ObserveIntegrationsRequest,
);

macro_rules! observed_events {
    ($($event:ty => $module:ident [$($removal:ident),*]),+ $(,)?) => { $(
        impl ObservedEvent for $event {
            type Payload = $module::Payload;
            fn frame(&self) -> Option<&StreamFrame> { self.frame.as_ref() }
            fn has_payload(&self) -> bool { self.payload.is_some() }
            fn take_payload(&mut self) -> Option<Self::Payload> { self.payload.take() }
            fn is_removal(&self) -> bool {
                match &self.payload {
                    $(Some($module::Payload::$removal(_)) => true,)*
                    _ => false,
                }
            }
        }
    )+ };
}
observed_events!(
    IdentityEvent => identity_event [Removal],
    PairingEvent => pairing_event [],
    OwnershipEvent => ownership_event [],
    WorkspaceEvent => workspace_event [Removal],
    CatalogEvent => catalog_event [Removal],
    SpoolEvent => spool_event [Removal],
    ThreadListEvent => thread_list_event [Removal],
    CollaborationEvent => collaboration_event [Removal],
    CheckoutEvent => checkout_event [Removal],
    RunEvent => run_event [Removal],
    AttentionEvent => attention_event [Removal],
    NotificationEvent => notification_event [Removal],
    OperationEvent => operation_event [Removal],
    IntegrationEvent => integration_event [Removal],
);

pub struct Observation<R: MessageReader<Error = transport::Error>, E: ObservedEvent> {
    messages: Messages<R, E>,
    state: Option<ObservationState>,
    binding: Option<[u8; 32]>,
    source: EndpointRef,
    requested: ReadBudget,
    accepted: ReadBudget,
    max_batch_bytes: u64,
    resume: Option<Resume>,
    query: Vec<u8>,
    pending: Vec<E::Payload>,
    pending_bytes: u64,
    snapshot: bool,
    done: bool,
    primed_error: Option<Error>,
}

impl<R: MessageReader<Error = transport::Error>, E: ObservedEvent> Observation<R, E> {
    pub(crate) fn new(
        messages: Messages<R, E>,
        description: &DescribeEndpointResponse,
        requested: ReadBudget,
        resume: Option<Resume>,
        query: Vec<u8>,
    ) -> Result<Self, Error> {
        let source = description
            .endpoint
            .clone()
            .ok_or(Error::Invalid("missing endpoint"))?;
        if resume
            .as_ref()
            .is_some_and(|r| r.source != source || r.query != query)
        {
            return Err(Error::Invalid(
                "resume belongs to a different source or projection",
            ));
        }
        Ok(Self {
            messages,
            state: None,
            binding: None,
            source,
            accepted: requested,
            requested,
            max_batch_bytes: u64::from(description.max_pending_batch_bytes).min(4 * 1024 * 1024),
            resume,
            query,
            pending: vec![],
            pending_bytes: 0,
            snapshot: true,
            done: false,
            primed_error: None,
        })
    }

    pub(crate) fn prime_error(&mut self, error: Error) {
        self.primed_error = Some(error);
    }

    /// Admit the stream Open (or surface a terminal opening failure) so a
    /// retryable authority-change can reopen a fresh exact selection.
    pub(crate) async fn consume_open(&mut self) -> Result<(), Error> {
        if self.state.is_some() || self.done {
            return Ok(());
        }
        let event = self.messages.next().await?.ok_or(Error::Interrupted)?;
        let size = prost::Message::encoded_len(&event) as u64;
        if size > u64::from(self.accepted.max_frame_bytes) {
            return Err(Error::Invalid("frame budget exceeded"));
        }
        let frame = event.frame().ok_or(Error::Invalid("missing frame"))?;
        if let Some(stream_frame::Body::Reset(reset)) = &frame.body {
            if frame.sequence != 1 || event.has_payload() {
                return Err(Error::Invalid("malformed initial reset"));
            }
            return Err(Error::Reset(reset.reason));
        }
        let Some(stream_frame::Body::Open(open)) = &frame.body else {
            return Err(Error::Invalid("missing Open"));
        };
        if open.source.as_ref() != Some(&self.source) {
            return Err(Error::Invalid("stream source mismatch"));
        }
        let binding: [u8; 32] = open
            .binding_digest
            .as_slice()
            .try_into()
            .map_err(|_| Error::Invalid("invalid binding"))?;
        let accepted = open
            .accepted_budget
            .ok_or(Error::Invalid("missing accepted budget"))?;
        if accepted.max_items == 0
            || accepted.max_items > self.requested.max_items
            || accepted.max_frame_bytes == 0
            || accepted.max_frame_bytes > self.requested.max_frame_bytes
            || accepted.max_snapshot_bytes == 0
            || accepted.max_snapshot_bytes > self.requested.max_snapshot_bytes
        {
            return Err(Error::Invalid("server widened or omitted the read budget"));
        }
        self.accepted = accepted;
        self.binding = Some(binding);
        self.state = Some(ObservationState::new(
            self.resume.as_ref().map(|r| r.binding).unwrap_or(binding),
            self.resume
                .as_ref()
                .map(|r| r.cursor.clone())
                .unwrap_or_default(),
        ));
        let state = self
            .state
            .as_mut()
            .ok_or(Error::Invalid("missing observation state"))?;
        match state.accept(frame, event.has_payload())? {
            ObservationAction::BeginSnapshot => self.snapshot = true,
            ObservationAction::Resumed => self.snapshot = false,
            ObservationAction::Heartbeat => {}
            _ => return Err(Error::Invalid("unexpected opening frame")),
        }
        Ok(())
    }

    pub fn cancel(&mut self) {
        self.messages.cancel();
        self.pending.clear();
        self.done = true;
    }

    pub async fn next_commit(&mut self) -> Result<Option<CommittedBatch<E::Payload>>, Error> {
        let result = self.next_inner().await;
        if result.is_err() {
            self.cancel();
        }
        result
    }

    async fn next_inner(&mut self) -> Result<Option<CommittedBatch<E::Payload>>, Error> {
        if let Some(error) = self.primed_error.take() {
            return Err(error);
        }
        if self.done {
            return Ok(None);
        }
        loop {
            let mut event = self.messages.next().await?.ok_or(Error::Interrupted)?;
            let size = prost::Message::encoded_len(&event) as u64;
            if size > u64::from(self.accepted.max_frame_bytes) {
                return Err(Error::Invalid("frame budget exceeded"));
            }
            let frame = event.frame().ok_or(Error::Invalid("missing frame"))?;
            if self.state.is_none() {
                if let Some(stream_frame::Body::Reset(reset)) = &frame.body {
                    if frame.sequence != 1 || event.has_payload() {
                        return Err(Error::Invalid("malformed initial reset"));
                    }
                    return Err(Error::Reset(reset.reason));
                }
                let Some(stream_frame::Body::Open(open)) = &frame.body else {
                    return Err(Error::Invalid("missing Open"));
                };
                if open.source.as_ref() != Some(&self.source) {
                    return Err(Error::Invalid("stream source mismatch"));
                }
                let binding: [u8; 32] = open
                    .binding_digest
                    .as_slice()
                    .try_into()
                    .map_err(|_| Error::Invalid("invalid binding"))?;
                let accepted = open
                    .accepted_budget
                    .ok_or(Error::Invalid("missing accepted budget"))?;
                if accepted.max_items == 0
                    || accepted.max_items > self.requested.max_items
                    || accepted.max_frame_bytes == 0
                    || accepted.max_frame_bytes > self.requested.max_frame_bytes
                    || accepted.max_snapshot_bytes == 0
                    || accepted.max_snapshot_bytes > self.requested.max_snapshot_bytes
                {
                    return Err(Error::Invalid("server widened or omitted the read budget"));
                }
                self.accepted = accepted;
                self.binding = Some(binding);
                self.state = Some(ObservationState::new(
                    self.resume.as_ref().map(|r| r.binding).unwrap_or(binding),
                    self.resume
                        .as_ref()
                        .map(|r| r.cursor.clone())
                        .unwrap_or_default(),
                ));
            }
            let state = self
                .state
                .as_mut()
                .ok_or(Error::Invalid("missing observation state"))?;
            match state.accept(frame, event.has_payload())? {
                ObservationAction::BeginSnapshot => self.snapshot = true,
                ObservationAction::Resumed => self.snapshot = false,
                ObservationAction::Stage(kind) => {
                    if event.is_removal() != (kind == StreamDataKind::Remove) {
                        return Err(Error::Invalid("removal payload/kind mismatch"));
                    }
                    let ceiling = if self.snapshot {
                        self.accepted.max_snapshot_bytes
                    } else {
                        self.max_batch_bytes
                    };
                    if self.pending.len() >= self.accepted.max_items as usize
                        || size > ceiling.saturating_sub(self.pending_bytes)
                    {
                        return Err(Error::Invalid("uncommitted batch budget exceeded"));
                    }
                    self.pending_bytes += size;
                    self.pending.push(
                        event
                            .take_payload()
                            .ok_or(Error::Invalid("missing data payload"))?,
                    );
                }
                ObservationAction::Commit => {
                    let binding = self
                        .binding
                        .ok_or(Error::Invalid("missing opening binding"))?;
                    let resume = Resume {
                        cursor: state.cursor().to_vec(),
                        binding,
                        source: self.source.clone(),
                        query: self.query.clone(),
                    };
                    let batch = CommittedBatch {
                        page: match &frame.body {
                            Some(stream_frame::Body::Checkpoint(c)) => c.page.clone(),
                            _ => None,
                        },
                        replace: self.snapshot,
                        changes: std::mem::take(&mut self.pending),
                        resume: resume.clone(),
                    };
                    self.resume = Some(resume);
                    self.pending_bytes = 0;
                    self.snapshot = false;
                    return Ok(Some(batch));
                }
                ObservationAction::Complete => {
                    self.done = true;
                    self.messages.cancel();
                    return Ok(None);
                }
                ObservationAction::Reset => {
                    let Some(stream_frame::Body::Reset(reset)) = &frame.body else {
                        return Err(Error::Invalid("missing Reset"));
                    };
                    return Err(Error::Reset(reset.reason));
                }
                ObservationAction::Heartbeat => {}
            }
        }
    }
}