bamboo-storage 2026.10.13

Session storage backends for the Bamboo agent framework
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
//! Durable WorkerHost observations and atomic slot reservations.
//!
//! The broker supplies connection identity; this file is control-plane state,
//! not a broker mailbox. All readers and writers take one physical file lock.

use std::collections::BTreeMap;
use std::fs::OpenOptions;
use std::io;
use std::path::{Path, PathBuf};

use bamboo_domain::{
    select_worker_host, HostPlacementRequest, HostRegistryError, RegisteredWorkerHost,
    WorkerHostRegistration, WorkerRunReservation, WorkerRunReservationCheck, WorkerSlotLease,
    HOST_REGISTRY_SCHEMA_VERSION, MAX_REGISTERED_HOSTS,
};
use chrono::{DateTime, Utc};
use fs2::FileExt;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::durable_atomic_write_blocking;

const RECORD: &str = "worker-host-registry.json";
const MARKER: &str = "worker-host-registry.initialized";
const LOCK: &str = "worker-host-registry.lock";
const MARKER_BYTES: &[u8] = b"worker-host-registry-v1\n";

fn storage(error: io::Error) -> HostRegistryError {
    HostRegistryError::Storage(error.to_string())
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct RegistryState {
    schema_version: u32,
    revision: u64,
    hosts: BTreeMap<String, RegisteredWorkerHost>,
}

impl Default for RegistryState {
    fn default() -> Self {
        Self {
            schema_version: HOST_REGISTRY_SCHEMA_VERSION,
            revision: 0,
            hosts: BTreeMap::new(),
        }
    }
}

impl RegistryState {
    fn validate(&self) -> Result<(), HostRegistryError> {
        if self.schema_version != HOST_REGISTRY_SCHEMA_VERSION
            || self.revision == 0
            || self.hosts.len() > MAX_REGISTERED_HOSTS
            || self
                .hosts
                .iter()
                .any(|(key, host)| key != &host.host_ref || host.validate().is_err())
        {
            return Err(HostRegistryError::Corrupt);
        }
        Ok(())
    }
}

fn read_state(home: &Path) -> Result<RegistryState, HostRegistryError> {
    let marker = std::fs::read(home.join(MARKER));
    let record = std::fs::read(home.join(RECORD));
    match (marker, record) {
        (Err(marker), Err(record))
            if marker.kind() == io::ErrorKind::NotFound
                && record.kind() == io::ErrorKind::NotFound =>
        {
            Ok(RegistryState::default())
        }
        (Ok(marker), Ok(record)) if marker == MARKER_BYTES => {
            let state: RegistryState =
                serde_json::from_slice(&record).map_err(|_| HostRegistryError::Corrupt)?;
            state.validate()?;
            Ok(state)
        }
        (Err(error), _) if error.kind() != io::ErrorKind::NotFound => Err(storage(error)),
        (_, Err(error)) if error.kind() != io::ErrorKind::NotFound => Err(storage(error)),
        _ => Err(HostRegistryError::Corrupt),
    }
}

fn publish_state(home: &Path, state: &RegistryState) -> Result<(), HostRegistryError> {
    let marker = home.join(MARKER);
    if !marker.exists() {
        // Publish the immutable witness first. Failure before the registry
        // record then fails closed on restart rather than resetting epochs.
        durable_atomic_write_blocking(&marker, MARKER_BYTES, |_| Ok(())).map_err(storage)?;
    }
    let encoded = serde_json::to_vec(state).map_err(|_| HostRegistryError::Corrupt)?;
    durable_atomic_write_blocking(&home.join(RECORD), &encoded, |_| Ok(())).map_err(storage)
}

/// A shared-home registry. This is safe across independent processes using the
/// same physical filesystem locking semantics; it is not a distributed DB.
#[derive(Debug, Clone)]
pub struct FileHostRegistry {
    home: PathBuf,
}

impl FileHostRegistry {
    pub async fn new(home: PathBuf) -> io::Result<Self> {
        if !home.is_absolute() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "WorkerHost registry home must be absolute",
            ));
        }
        tokio::fs::create_dir_all(&home).await?;
        Ok(Self { home })
    }

    async fn transact<T, F>(&self, operation: F) -> Result<T, HostRegistryError>
    where
        T: Send + 'static,
        F: FnOnce(&mut RegistryState) -> Result<(T, bool), HostRegistryError> + Send + 'static,
    {
        let home = self.home.clone();
        tokio::task::spawn_blocking(move || {
            let lock = OpenOptions::new()
                .create(true)
                .truncate(false)
                .read(true)
                .write(true)
                .open(home.join(LOCK))
                .map_err(storage)?;
            lock.lock_exclusive().map_err(storage)?;
            let mut state = read_state(&home)?;
            let (result, changed) = operation(&mut state)?;
            if changed {
                state.revision = state
                    .revision
                    .checked_add(1)
                    .ok_or(HostRegistryError::Corrupt)?;
                state.validate()?;
                publish_state(&home, &state)?;
            }
            Ok(result)
        })
        .await
        .map_err(|error| {
            HostRegistryError::Storage(format!("join registry transaction: {error}"))
        })?
    }

    pub async fn inspect_host(
        &self,
        host_ref: &str,
    ) -> Result<Option<RegisteredWorkerHost>, HostRegistryError> {
        let host_ref = host_ref.to_owned();
        self.transact(move |state| Ok((state.hosts.get(&host_ref).cloned(), false)))
            .await
    }

    pub async fn observe_host(
        &self,
        request: WorkerHostRegistration,
    ) -> Result<RegisteredWorkerHost, HostRegistryError> {
        request.validate()?;
        self.transact(move |state| {
            let existing = state.hosts.get(&request.host_ref);
            if existing.map(|host| host.connection_generation.as_str())
                != request.expected_connection_generation.as_deref()
            {
                return Err(HostRegistryError::StaleGeneration);
            }
            if existing.is_none() && state.hosts.len() >= MAX_REGISTERED_HOSTS {
                return Err(HostRegistryError::Busy);
            }
            let mut slots = BTreeMap::new();
            let mut next_slot_epoch = 0;
            if let Some(old) = existing {
                next_slot_epoch = old.next_slot_epoch;
                if old.connection_generation == request.connection_generation {
                    if request.observed_at < old.observed_at
                        || old.mailbox != request.mailbox
                        || old.role != request.role
                    {
                        return Err(HostRegistryError::StaleGeneration);
                    }
                    slots = old.slots.clone();
                    slots.retain(|_, lease| lease.expires_at > request.observed_at);
                    if !slots.is_empty()
                        && (old.capabilities != request.capabilities
                            || slots.keys().any(|slot| *slot >= request.max_slots)
                            || slots
                                .values()
                                .any(|lease| lease.expires_at > request.lease_expires_at))
                    {
                        return Err(HostRegistryError::Busy);
                    }
                }
                // A different authenticated connection fences every slot of
                // the predecessor. Its epoch counter remains monotonic.
            }
            let host = RegisteredWorkerHost {
                host_ref: request.host_ref.clone(),
                mailbox: request.mailbox,
                role: request.role,
                connection_generation: request.connection_generation,
                credential_expires_at: request.credential_expires_at,
                observed_at: request.observed_at,
                lease_expires_at: request.lease_expires_at,
                max_slots: request.max_slots,
                capabilities: request.capabilities,
                next_slot_epoch,
                slots,
            };
            state.hosts.insert(host.host_ref.clone(), host.clone());
            Ok((host, true))
        })
        .await
    }

    /// Select and reserve under one file lock. No caller can claim capacity
    /// merely by observing a broker connection or reading this registry.
    pub async fn reserve_slot(
        &self,
        request: HostPlacementRequest,
    ) -> Result<WorkerSlotLease, HostRegistryError> {
        request.validate()?;
        self.transact(move |state| {
            // ActorSession identity may not occupy two WorkerHosts or slots at
            // once, including a retry with a different Run ID. This check and
            // slot selection share the same cross-process file lock.
            if state.hosts.values().any(|host| {
                host.is_live(request.now)
                    && host.slots.values().any(|lease| {
                        lease.actor_id == request.actor_id && lease.expires_at > request.now
                    })
            }) {
                return Err(HostRegistryError::Busy);
            }
            let selected = select_worker_host(&state.hosts, &request)?;
            let host_ref = selected.host_ref.clone();
            let slot = selected
                .first_free_slot(request.now)
                .ok_or(HostRegistryError::NoEligibleHost)?;
            let host = state
                .hosts
                .get_mut(&host_ref)
                .ok_or(HostRegistryError::Corrupt)?;
            let epoch = host
                .next_slot_epoch
                .checked_add(1)
                .ok_or(HostRegistryError::Corrupt)?;
            let expires_at = request
                .lease_expires_at
                .min(host.lease_expires_at)
                .min(host.credential_expires_at);
            if expires_at <= request.now {
                return Err(HostRegistryError::NoEligibleHost);
            }
            let lease = WorkerSlotLease {
                lease_id: Uuid::new_v4().to_string(),
                host_ref,
                connection_generation: host.connection_generation.clone(),
                slot,
                epoch,
                actor_id: request.actor_id,
                run_id: request.run_id,
                expires_at,
            };
            host.next_slot_epoch = epoch;
            host.slots.insert(slot, lease.clone());
            Ok((lease, true))
        })
        .await
    }

    pub async fn validate_slot(
        &self,
        lease: &WorkerSlotLease,
        now: DateTime<Utc>,
    ) -> Result<(), HostRegistryError> {
        let lease = lease.clone();
        self.transact(move |state| {
            let host = state
                .hosts
                .get(&lease.host_ref)
                .ok_or(HostRegistryError::StaleLease)?;
            if !host.is_live(now)
                || host.connection_generation != lease.connection_generation
                || host.slots.get(&lease.slot) != Some(&lease)
                || lease.expires_at <= now
            {
                return Err(HostRegistryError::StaleLease);
            }
            Ok(((), false))
        })
        .await
    }

    /// Resolve the opaque ActorActivation placement ID to the exact live slot
    /// and delivery identity under one cross-process registry lock. The
    /// `observed_*` fields must come from a fresh authenticated broker query;
    /// this is a preflight only, not an atomic broker-delivery fence.
    pub async fn validate_run_reservation(
        &self,
        check: WorkerRunReservationCheck,
    ) -> Result<WorkerRunReservation, HostRegistryError> {
        check.validate()?;
        self.transact(move |state| {
            let host = state
                .hosts
                .get(&check.observed_host_ref)
                .ok_or(HostRegistryError::StaleLease)?;
            if !host.is_live(check.now)
                || host.connection_generation != check.observed_connection_generation
                || host.capabilities.placement_class != check.placement_ref.class
            {
                return Err(HostRegistryError::StaleLease);
            }
            let mut matching = host
                .slots
                .values()
                .filter(|lease| lease.lease_id == check.placement_ref.lease_id);
            let lease = matching.next().ok_or(HostRegistryError::StaleLease)?;
            if matching.next().is_some() {
                return Err(HostRegistryError::Corrupt);
            }
            if lease.actor_id != check.actor_id
                || lease.run_id != check.run_id
                || Some(lease.epoch) != check.placement_ref.slot_epoch
                || lease.connection_generation != check.observed_connection_generation
                || lease.expires_at <= check.now
            {
                return Err(HostRegistryError::StaleLease);
            }
            Ok((
                WorkerRunReservation {
                    lease: lease.clone(),
                    mailbox: host.mailbox.clone(),
                    role: host.role.clone(),
                },
                false,
            ))
        })
        .await
    }

    /// Recover a lease by the opaque ID persisted in ActorActivation's
    /// placement_ref. Callers must also validate the ActorDirectory fence.
    pub async fn inspect_slot_by_lease_id(
        &self,
        lease_id: &str,
        actor_id: &str,
        run_id: &str,
        now: DateTime<Utc>,
    ) -> Result<WorkerSlotLease, HostRegistryError> {
        let lease_id = lease_id.to_owned();
        let actor_id = actor_id.to_owned();
        let run_id = run_id.to_owned();
        self.transact(move |state| {
            let lease = state
                .hosts
                .values()
                .filter(|host| host.is_live(now))
                .flat_map(|host| host.slots.values())
                .find(|lease| lease.lease_id == lease_id)
                .filter(|lease| {
                    lease.actor_id == actor_id && lease.run_id == run_id && lease.expires_at > now
                })
                .cloned()
                .ok_or(HostRegistryError::StaleLease)?;
            Ok((lease, false))
        })
        .await
    }

    pub async fn renew_slot(
        &self,
        lease: &WorkerSlotLease,
        now: DateTime<Utc>,
        new_expires_at: DateTime<Utc>,
    ) -> Result<WorkerSlotLease, HostRegistryError> {
        let lease = lease.clone();
        self.transact(move |state| {
            let host = state
                .hosts
                .get_mut(&lease.host_ref)
                .ok_or(HostRegistryError::StaleLease)?;
            if !host.is_live(now)
                || host.connection_generation != lease.connection_generation
                || host.slots.get(&lease.slot) != Some(&lease)
                || lease.expires_at <= now
            {
                return Err(HostRegistryError::StaleLease);
            }
            if new_expires_at <= lease.expires_at
                || new_expires_at - now > chrono::Duration::minutes(5)
                || new_expires_at > host.lease_expires_at
                || new_expires_at > host.credential_expires_at
            {
                return Err(HostRegistryError::Invalid);
            }
            let mut renewed = lease;
            renewed.expires_at = new_expires_at;
            host.slots.insert(renewed.slot, renewed.clone());
            Ok((renewed, true))
        })
        .await
    }

    pub async fn release_slot(&self, lease: &WorkerSlotLease) -> Result<(), HostRegistryError> {
        let lease = lease.clone();
        self.transact(move |state| {
            let host = state
                .hosts
                .get_mut(&lease.host_ref)
                .ok_or(HostRegistryError::StaleLease)?;
            if host.connection_generation != lease.connection_generation
                || host.slots.get(&lease.slot) != Some(&lease)
            {
                return Err(HostRegistryError::StaleLease);
            }
            host.slots.remove(&lease.slot);
            Ok(((), true))
        })
        .await
    }

    /// Immediate disconnect or policy revocation. An old generation cannot
    /// revoke a successor's slots.
    pub async fn revoke_host(
        &self,
        host_ref: &str,
        connection_generation: &str,
        now: DateTime<Utc>,
    ) -> Result<(), HostRegistryError> {
        let host_ref = host_ref.to_owned();
        let connection_generation = connection_generation.to_owned();
        self.transact(move |state| {
            let host = state
                .hosts
                .get_mut(&host_ref)
                .ok_or(HostRegistryError::StaleGeneration)?;
            if host.connection_generation != connection_generation {
                return Err(HostRegistryError::StaleGeneration);
            }
            host.lease_expires_at = now.min(host.credential_expires_at);
            host.slots.clear();
            Ok(((), true))
        })
        .await
    }
}