bamboo-storage 2026.10.21

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
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
//! Ordinary Root input uses the existing full writer and owned Maildir lease.
use super::root_actor_runtime::{conflict, RootActorWriteProof};
use super::*;
use crate::session_inbox::{FileSessionInbox, InboxAuthority, OwnedFilesystem};
use bamboo_domain::{
    RootActorRuntimePublisher, RootActorRuntimeWrite, SessionInboxOwnedClaim, SessionInboxPort,
    SessionMessageEnvelope,
};

/// A detached physical job can outlive the async waiter. Dropping the waiter
/// cancels the same gate that the final synchronous effect must commit.
struct CancelPendingOnDrop {
    gate: bamboo_domain::AdmissionGate,
    armed: bool,
}

impl CancelPendingOnDrop {
    fn new(gate: bamboo_domain::AdmissionGate) -> Self {
        Self { gate, armed: true }
    }

    fn disarm(&mut self) {
        self.armed = false;
    }
}

impl Drop for CancelPendingOnDrop {
    fn drop(&mut self) {
        if self.armed {
            self.gate.cancel_if_pending();
        }
    }
}

/// Nonserializable, process-local observation. It is not provider authority.
struct LiveBootstrapCapture {
    owner: RootActorRuntimeWrite,
    committed: serde_json::Value,
    inbox_directory: PathBuf,
    inbox_generation: u64,
    consumed: AtomicBool,
}

impl LiveBootstrapCapture {
    fn matches_owner(&self, owner: &RootActorRuntimeWrite) -> bool {
        self.owner.fence == owner.fence
            && self.owner.created_at == owner.created_at
            && self.owner.conversation_generation == owner.conversation_generation
    }
}

fn ensure_live_bootstrap_canonical_file_sizes(directory: &Path) -> io::Result<()> {
    for name in [
        "session.json",
        RUNTIME_SIDECAR_FILE,
        "actor-authority.json",
        "actor-authority.initialized.json",
    ] {
        let meta = std::fs::symlink_metadata(directory.join(name))?;
        if !meta.file_type().is_file()
            || meta.len() > crate::session_merge::LIVE_BOOTSTRAP_SESSION_JSON_LIMIT as u64
        {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "live bootstrap canonical file exceeds limit or is invalid",
            ));
        }
    }
    Ok(())
}

#[derive(Clone)]
pub(super) struct RootActorInputProof {
    root: RootActorWriteProof,
    inbox: FileSessionInbox,
    directory: PathBuf,
    claim: SessionInboxOwnedClaim,
}

impl RootActorInputProof {
    pub(super) fn validate(&self, allow_terminal: bool) -> io::Result<()> {
        self.root.validate_deadline(self.claim.lease.expires_at)?;
        self.inbox
            .locked_owned_claim(
                &self.directory,
                &self.claim.claim.envelope.target_session_id,
                &self.claim,
                Utc::now(),
                allow_terminal,
            )
            .map(|_| ())
            .map_err(conflict)
    }

    pub(super) fn validate_transcript(&self, session: &Session) -> io::Result<()> {
        if !session.messages.iter().any(|message| {
            bamboo_domain::is_matching_session_message(message, &self.claim.claim.envelope)
        }) || !session.session_inbox_admission().is_some_and(|cursor| {
            cursor.contains(&self.claim.claim.envelope.id)
                && cursor.last_admitted_sequence >= self.claim.claim.generation
        }) {
            return Err(conflict(
                "Root input checkpoint lacks typed message and admission proof",
            ));
        }
        Ok(())
    }
}

impl SessionStoreV2 {
    pub(crate) fn root_sessions_directory(&self) -> &Path {
        &self.sessions_dir
    }

    pub(super) fn bound_root_inbox(
        &self,
        owner: &RootActorRuntimeWrite,
        inbox: &Arc<dyn SessionInboxPort>,
    ) -> io::Result<FileSessionInbox> {
        inbox
            .as_any()
            .and_then(|any| any.downcast_ref::<FileSessionInbox>())
            .ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::Unsupported,
                    "Root admission requires the concrete owned Inbox",
                )
            })?
            .bind_root_owner(owner, &self.sessions_dir)
    }

    async fn root_input_guards(
        &self,
        target: &str,
        owner: &RootActorRuntimeWrite,
    ) -> io::Result<Arc<DefaultWriterGuards>> {
        self.root_input_guards_inner(target, owner, false).await
    }

    async fn root_input_guards_inner(
        &self,
        target: &str,
        owner: &RootActorRuntimeWrite,
        limit_recovery_files: bool,
    ) -> io::Result<Arc<DefaultWriterGuards>> {
        if target != owner.fence.actor_id {
            return Err(conflict("Root Inbox target changed"));
        }
        validate_session_id(target)?;
        let lifecycle = self.lock_default_writer_lifecycle().await?;
        let task = self.lock_runtime_task_sidecar_shared().await?;
        let session = self
            .acquire_session_write_lock(target, SaveKind::Full)
            .await?;
        let directory = self.sessions_dir.join(target);
        let proof = RootActorWriteProof::new(directory.clone(), owner.clone());
        let guards = DefaultWriterGuards::shared_with_root_actor(
            lifecycle,
            task,
            session,
            Some(proof.clone()),
        );
        Self::default_writer_job(&guards, move || {
            if limit_recovery_files {
                ensure_live_bootstrap_canonical_file_sizes(&directory)?;
            }
            proof.validate().map(|_| ())
        })
        .await?;
        Ok(guards)
    }

    pub(crate) async fn root_owned_inbox_filesystem(
        &self,
        inbox: &FileSessionInbox,
        target: &str,
        owner: &RootActorRuntimeWrite,
    ) -> io::Result<(PathBuf, OwnedFilesystem)> {
        let guards = self.root_input_guards(target, owner).await?;
        let proof = guards.root_actor.clone().expect("bound Root proof");
        let (directory, filesystem) = inbox
            .filesystem_with_authority(
                target,
                InboxAuthority::Root {
                    _guard: guards.physical.clone(),
                },
            )
            .await
            .map_err(conflict)?;
        Ok((
            directory,
            filesystem.with_check(Arc::new(move || proof.validate().map(|_| ()))),
        ))
    }

    pub(crate) fn root_inbox_lease_check(
        &self,
        owner: &RootActorRuntimeWrite,
        deadline: DateTime<Utc>,
    ) -> Arc<dyn Fn() -> io::Result<()> + Send + Sync> {
        let proof =
            RootActorWriteProof::new(self.sessions_dir.join(&owner.fence.actor_id), owner.clone());
        Arc::new(move || proof.validate_deadline(deadline))
    }

    pub(crate) fn root_inbox_terminal_check(
        &self,
        owner: &RootActorRuntimeWrite,
        envelope: &SessionMessageEnvelope,
    ) -> Arc<dyn Fn() -> io::Result<()> + Send + Sync> {
        let proof =
            RootActorWriteProof::new(self.sessions_dir.join(&owner.fence.actor_id), owner.clone());
        let envelope = envelope.clone();
        Arc::new(move || {
            let durable = proof.validate()?;
            if (bamboo_domain::is_scoped_loop_control(&envelope)
                && bamboo_domain::has_scoped_loop_consumption(&durable, &envelope)
                && durable
                    .session_inbox_admission()
                    .is_some_and(|cursor| cursor.contains(&envelope.id)))
                || (!bamboo_domain::is_scoped_loop_control(&envelope)
                    && durable.messages.iter().any(|message| {
                        bamboo_domain::is_matching_session_message(message, &envelope)
                    }))
            {
                Ok(())
            } else {
                Err(conflict(
                    "terminal Inbox receipt lacks typed canonical proof",
                ))
            }
        })
    }

    pub(super) async fn checkpoint_root_live_bootstrap_impl(
        &self,
        owner: &RootActorRuntimeWrite,
        session: &Session,
        inbox: Arc<dyn SessionInboxPort>,
        gate: bamboo_domain::AdmissionGate,
        publish: RootActorRuntimePublisher,
    ) -> io::Result<bamboo_domain::LiveBootstrapProof> {
        let mut cancel_on_drop = CancelPendingOnDrop::new(gate.clone());
        if gate.is_cancelled() || gate.is_committed() {
            return Err(conflict("live bootstrap gate unavailable"));
        }
        let started = Instant::now();
        let inbox = self.bound_root_inbox(owner, &inbox)?;
        let mut guards = self
            .root_input_guards_inner(&session.id, owner, true)
            .await?;
        let root = guards.root_actor.clone().expect("bound Root proof");
        let (directory, filesystem) = inbox
            .filesystem_with_authority(
                &session.id,
                InboxAuthority::Root {
                    _guard: guards.physical.clone(),
                },
            )
            .await
            .map_err(conflict)?;
        if directory != self.sessions_dir.join(&session.id).join("inbox") {
            return Err(conflict("Root Inbox directory changed"));
        }
        // The started physical writer retains Inbox as well as Session/Actor
        // even if its awaiting task is cancelled during a blocking write.
        Arc::get_mut(&mut guards)
            .expect("new Root guards are private")
            ._input_filesystem = Some(filesystem.clone());
        let expected = crate::session_merge::bounded_live_bootstrap_value(session)?;
        let candidate = session.clone();
        let check_root = root.clone();
        let check_session_directory = self.sessions_dir.join(&session.id);
        let check_dir = directory.clone();
        let check_filesystem = filesystem.clone();
        let check_gate = gate.clone();
        let before = Self::default_writer_job(&guards, move || {
            if check_gate.is_cancelled() || check_gate.is_committed() {
                return Err(conflict("live bootstrap gate unavailable"));
            }
            ensure_live_bootstrap_canonical_file_sizes(&check_session_directory)?;
            check_root.validate_candidate(&candidate)?;
            let current = check_root.validate()?;
            crate::session_merge::validate_live_bootstrap_candidate(&candidate, &current)?;
            crate::session_inbox::empty_live_bootstrap_generation_locked(
                &check_dir,
                &check_filesystem,
            )
            .map_err(conflict)
        })
        .await?;
        self.save_session_after_lock(session, started, &guards, None)
            .await?;
        let check_root = root.clone();
        let check_session_directory = self.sessions_dir.join(&session.id);
        let check_dir = directory.clone();
        let check_filesystem = filesystem.clone();
        let check_expected = expected.clone();
        let check_gate = gate.clone();
        Self::default_writer_job(&guards, move || {
            if check_gate.is_cancelled() || check_gate.is_committed() {
                return Err(conflict("live bootstrap gate unavailable"));
            }
            ensure_live_bootstrap_canonical_file_sizes(&check_session_directory)?;
            let saved = check_root.validate()?;
            if crate::session_merge::bounded_live_bootstrap_value(&saved)? != check_expected {
                return Err(conflict("live bootstrap Session readback changed"));
            }
            let after = crate::session_inbox::empty_live_bootstrap_generation_locked(
                &check_dir,
                &check_filesystem,
            )
            .map_err(conflict)?;
            if after != before {
                return Err(conflict("live bootstrap Inbox changed"));
            }
            Ok(())
        })
        .await?;
        // Reuse the existing guarded cache publication, not an event or grant.
        self.publish_root_actor_runtime(&guards, publish).await?;
        if gate.is_cancelled() || gate.is_committed() {
            return Err(conflict("live bootstrap gate unavailable"));
        }
        let proof = bamboo_domain::LiveBootstrapProof::from_storage(
            LiveBootstrapCapture {
                owner: owner.clone(),
                committed: expected,
                inbox_directory: directory,
                inbox_generation: before,
                consumed: AtomicBool::new(false),
            },
            gate,
        );
        cancel_on_drop.disarm();
        Ok(proof)
    }

    pub(super) async fn confirm_root_live_bootstrap_impl(
        &self,
        owner: &RootActorRuntimeWrite,
        proof: &bamboo_domain::LiveBootstrapProof,
        inbox: Arc<dyn SessionInboxPort>,
        publish: bamboo_domain::RootActorRuntimeEventPublisher,
    ) -> io::Result<()> {
        let captured = proof
            .storage_ref::<LiveBootstrapCapture>()
            .ok_or_else(|| conflict("wrong live bootstrap proof"))?;
        let mut cancel_on_drop = CancelPendingOnDrop::new(proof.storage_gate());
        // Storage validates the actor and commits a supplied synchronous
        // effect once; it cannot create a provider continuation. The private
        // Host must supply that effect only for a live provider-created offer
        // whose gate matches this proof.
        if !captured.matches_owner(owner) {
            return Err(conflict("live bootstrap owner unavailable"));
        }
        let inbox = self.bound_root_inbox(owner, &inbox)?;
        let mut guards = self
            .root_input_guards_inner(&owner.fence.actor_id, owner, true)
            .await?;
        let root = guards.root_actor.clone().expect("bound Root proof");
        let (directory, filesystem) = inbox
            .filesystem_with_authority(
                &owner.fence.actor_id,
                InboxAuthority::Root {
                    _guard: guards.physical.clone(),
                },
            )
            .await
            .map_err(conflict)?;
        if directory != self.sessions_dir.join(&owner.fence.actor_id).join("inbox")
            || directory != captured.inbox_directory
        {
            return Err(conflict("Root Inbox directory changed"));
        }
        Arc::get_mut(&mut guards)
            .expect("new Root guards are private")
            ._input_filesystem = Some(filesystem.clone());
        let held = proof.clone();
        let session_directory = self.sessions_dir.join(&owner.fence.actor_id);
        let result = Self::default_writer_job(&guards, move || {
            let captured = held
                .storage_ref::<LiveBootstrapCapture>()
                .ok_or_else(|| conflict("wrong live bootstrap proof"))?;
            let check = || -> io::Result<()> {
                ensure_live_bootstrap_canonical_file_sizes(&session_directory)?;
                let current = root.validate()?;
                if crate::session_merge::bounded_live_bootstrap_value(&current)?
                    != captured.committed
                {
                    return Err(conflict("live bootstrap Session changed"));
                }
                let generation = crate::session_inbox::empty_live_bootstrap_generation_locked(
                    &directory,
                    &filesystem,
                )
                .map_err(conflict)?;
                if generation != captured.inbox_generation {
                    return Err(conflict("live bootstrap Inbox changed"));
                }
                Ok(())
            };
            check()?;
            match held.storage_gate().commit(|| {
                check()?;
                // Burn before invoking the callback: even a lost ACK after an
                // effect must not let a retry invoke the decision twice.
                if captured.consumed.swap(true, Ordering::AcqRel) {
                    return Err(conflict("live bootstrap proof already consumed"));
                }
                publish(&check)
            })? {
                bamboo_domain::AdmissionCommit::Committed(()) => Ok(()),
                bamboo_domain::AdmissionCommit::Cancelled => {
                    Err(conflict("live bootstrap cancelled"))
                }
                bamboo_domain::AdmissionCommit::AlreadyCommitted => {
                    Err(conflict("live bootstrap already committed"))
                }
            }
        })
        .await;
        if result.is_ok() {
            cancel_on_drop.disarm();
        }
        result
    }

    pub(super) async fn save_root_actor_input_impl(
        &self,
        owner: &RootActorRuntimeWrite,
        session: &mut Session,
        inbox: Arc<dyn SessionInboxPort>,
        claim: &SessionInboxOwnedClaim,
        publish: RootActorRuntimePublisher,
        inherited: Option<&bamboo_domain::InheritedChildWait>,
    ) -> io::Result<()> {
        let total_started = Instant::now();
        let inbox = self.bound_root_inbox(owner, &inbox)?;
        let mut guards = self.root_input_guards(&session.id, owner).await?;
        let root_proof = guards.root_actor.clone().expect("bound Root proof");
        let (directory, filesystem) = inbox
            .filesystem_with_authority(
                &session.id,
                InboxAuthority::Root {
                    _guard: guards.physical.clone(),
                },
            )
            .await
            .map_err(conflict)?;
        let input = RootActorInputProof {
            root: root_proof.clone(),
            inbox: inbox.clone(),
            directory: directory.clone(),
            claim: claim.clone(),
        };
        let ack_input = input.clone();
        let filesystem = filesystem.with_check(Arc::new(move || {
            let durable = root_proof.validate()?;
            ack_input.validate(true)?;
            ack_input.validate_transcript(&durable)
        }));
        let mutable = Arc::get_mut(&mut guards).expect("new Root writer has no borrowers");
        mutable.input = Some(input);
        mutable._input_filesystem = Some(filesystem.clone());
        let reconciled = match inherited {
            Some(inherited) => Some(
                self.reconcile_inherited_runtime_snapshot(session, inherited, false)
                    .await?,
            ),
            None => None,
        };
        self.save_session_after_lock(
            reconciled.as_ref().unwrap_or(session),
            total_started,
            &guards,
            None,
        )
        .await?;
        self.publish_root_actor_runtime(&guards, publish).await?;
        if let Some(reconciled) = reconciled {
            *session = reconciled;
        }
        // No provider boundary or new lease driver between Main and ACK.
        inbox
            .ack_owned_with_filesystem(&directory, &session.id, claim, Utc::now(), filesystem)
            .await
            .map_err(conflict)
    }
}