bamboo-domain 2026.10.21

Domain models and shared types 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
//! Finite Host installation data. Serialized observations never grant access.

use std::{
    collections::{BTreeMap, BTreeSet},
    path::{Component, Path},
    sync::Arc,
};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::{
    ActorAncestorObservation, ActorSession, ProjectId, Session, SessionAuthorityIdentity,
    SessionCallerReference, SessionKind,
};

/// The existing Supervisor logical operation address. Scope installation or
/// a new delivery intent cannot allocate another message for the same call.
/// This data is not authorization; the final writer still validates birth,
/// current grants and the complete original caller under physical guards.
pub fn supervisor_followup_message_id(
    supervisor: &crate::SupervisorReference,
    target_session_id: &str,
    operation_id: &str,
) -> crate::SessionMessageId {
    crate::SessionMessageId::stable(
        "supervisor_followup",
        &serde_json::json!({
            "supervisor": supervisor,
            "target_session_id": target_session_id,
            "operation_id": operation_id,
        }),
    )
}

#[derive(Debug, Clone, Copy)]
enum HostSessionPeerProtocol {
    PeerV1,
    SupervisorFollowup,
}

/// Pure wire/idempotency projection, never a grant. The physical writer must
/// recompute it from the original caller whose canonical identity it checks.
#[derive(Debug, Clone)]
pub struct HostSessionPeerIdentity {
    pub message_id: crate::SessionMessageId,
    pub thread_id: Option<String>,
    protocol: HostSessionPeerProtocol,
}

impl HostSessionPeerIdentity {
    pub fn for_caller(
        caller: &SessionCallerReference,
        target_id: &str,
        target_created_at: DateTime<Utc>,
        operation_id: &str,
    ) -> Self {
        let (message_id, protocol) = match &caller.authority_identity {
            SessionAuthorityIdentity::Supervisor { incarnation_id } => (
                supervisor_followup_message_id(
                    &crate::SupervisorReference {
                        session_id: caller.session_id.clone(),
                        incarnation_id: *incarnation_id,
                    },
                    target_id,
                    operation_id,
                ),
                HostSessionPeerProtocol::SupervisorFollowup,
            ),
            SessionAuthorityIdentity::Ordinary => (
                crate::session_peer_message_id(
                    &caller.session_id,
                    caller.created_at,
                    target_id,
                    target_created_at,
                    operation_id,
                ),
                HostSessionPeerProtocol::PeerV1,
            ),
        };
        let thread_id = match protocol {
            HostSessionPeerProtocol::PeerV1 => Some(message_id.to_string()),
            HostSessionPeerProtocol::SupervisorFollowup => None,
        };
        Self {
            message_id,
            thread_id,
            protocol,
        }
    }

    pub fn activation_intent(
        &self,
        delivery: crate::SessionMessageDeliveryIntent,
    ) -> crate::SessionInboxActivationIntent {
        match (self.protocol, delivery) {
            // Retain the existing Supervisor trigger's exact durable wire.
            // QueueOnly still has its own inert intent at this same address,
            // so changing it later is a conflict rather than a second send.
            (
                HostSessionPeerProtocol::SupervisorFollowup,
                crate::SessionMessageDeliveryIntent::TriggerTurn,
            ) => crate::SessionInboxActivationIntent::new(
                crate::SessionActivationPolicy::RespectSpecificWait,
            ),
            _ => crate::SessionInboxActivationIntent::with_delivery(delivery),
        }
    }
}

pub const HOST_SESSION_SCOPE_VERSION: u32 = 1;
pub const MAX_HOST_SESSION_TARGETS: usize = 128;
pub const MAX_HOST_SESSION_CREATES: usize = 32;
pub const MAX_HOST_SESSION_ANCESTORS: usize = 64;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HostSessionRight {
    ListMetadata,
    InspectBoundedProgress,
    /// Fixed immutable status/brief views, including bounded Task previews.
    /// Separate from the original progress/assistant-excerpt projection.
    ReadContextView,
    SendPeer,
}

/// Stable scope identity, independent of one conversation or tool invocation.
/// Keep this explicit projection separate from `SessionCallerReference`: later
/// execution observations may add generation fields, which must not revoke a
/// Host grant when an idle conversation resets without changing its identity.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HostSessionScopeIdentity {
    pub session_id: String,
    pub created_at: DateTime<Utc>,
    pub kind: SessionKind,
    pub root_session_id: String,
    pub parent_session_id: Option<String>,
    pub parent_created_at: Option<DateTime<Utc>>,
    pub spawn_depth: u32,
    pub authority_identity: SessionAuthorityIdentity,
    pub root_tool_authority_revision: u64,
    pub session_version: Option<u32>,
    pub workspace: Option<String>,
}

impl From<&SessionCallerReference> for HostSessionScopeIdentity {
    fn from(caller: &SessionCallerReference) -> Self {
        Self {
            session_id: caller.session_id.clone(),
            created_at: caller.created_at,
            kind: caller.kind,
            root_session_id: caller.root_session_id.clone(),
            parent_session_id: caller.parent_session_id.clone(),
            parent_created_at: caller.parent_created_at,
            spawn_depth: caller.spawn_depth,
            authority_identity: caller.authority_identity.clone(),
            root_tool_authority_revision: caller.root_tool_authority_revision,
            session_version: caller.session_version,
            workspace: caller.workspace.clone(),
        }
    }
}

impl HostSessionScopeIdentity {
    /// Stable identity only. The final operation must separately validate the
    /// original caller's complete conversation generation under its own locks.
    pub fn matches_caller(&self, caller: &SessionCallerReference) -> bool {
        self == &Self::from(caller)
    }
}

/// Original scope/lineage data, without conversation state or tool arguments.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HostScopeSessionObservation {
    pub identity: HostSessionScopeIdentity,
    pub metadata_version: u64,
    pub project_id: Option<ProjectId>,
    /// Direct parent first, Root last, just like the canonical Actor directory.
    pub ancestors: Vec<ActorAncestorObservation>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum HostSessionScopeError {
    #[error("host_session_scope_invalid: explicit finite scope is malformed")]
    Invalid,
    #[error("host_session_scope_stale: original Session identity/Project changed")]
    Stale,
    #[error("host_session_scope_denied: this exact operation is not granted")]
    Denied,
}

fn valid_id(id: &str) -> bool {
    !id.is_empty() && id.trim() == id && !id.contains(['/', '\\']) && !id.contains("..")
}

impl HostScopeSessionObservation {
    /// A Host supplies the complete canonically read ancestor observations.
    /// This captures data only; final storage still checks the complete chain.
    pub fn capture(
        session: &Session,
        ancestors: Vec<ActorAncestorObservation>,
    ) -> Result<Self, HostSessionScopeError> {
        session
            .validate_conversation_state()
            .map_err(|_| HostSessionScopeError::Invalid)?;
        let actor =
            ActorSession::from_session(session).map_err(|_| HostSessionScopeError::Invalid)?;
        let observation = Self {
            identity: HostSessionScopeIdentity::from(&SessionCallerReference::from(session)),
            metadata_version: session.metadata_version,
            project_id: actor
                .project_id
                .map(|id| id.parse().map_err(|_| HostSessionScopeError::Invalid))
                .transpose()?,
            ancestors,
        };
        observation.validate()?;
        Ok(observation)
    }

    pub fn validate(&self) -> Result<(), HostSessionScopeError> {
        let caller = &self.identity;
        if !valid_id(&caller.session_id)
            || !valid_id(&caller.root_session_id)
            || self.ancestors.len() > MAX_HOST_SESSION_ANCESTORS
            || self.ancestors.len() != caller.spawn_depth as usize
        {
            return Err(HostSessionScopeError::Invalid);
        }
        let mut ids = BTreeSet::from([caller.session_id.as_str()]);
        if self.ancestors.iter().any(|a| {
            !valid_id(&a.actor_id)
                || !ids.insert(&a.actor_id)
                || a.session_created_at > caller.created_at
        }) {
            return Err(HostSessionScopeError::Invalid);
        }
        match caller.kind {
            SessionKind::Root
                if caller.root_session_id == caller.session_id
                    && caller.parent_session_id.is_none()
                    && caller.parent_created_at.is_none()
                    && caller.spawn_depth == 0 => {}
            SessionKind::Child
                if caller.spawn_depth > 0
                    && self.ancestors.first().is_some_and(|p| {
                        Some(p.actor_id.as_str()) == caller.parent_session_id.as_deref()
                            && Some(p.session_created_at) == caller.parent_created_at
                    })
                    && self
                        .ancestors
                        .last()
                        .is_some_and(|root| root.actor_id == caller.root_session_id) => {}
            _ => return Err(HostSessionScopeError::Invalid),
        }
        Ok(())
    }

    pub fn check_session(&self, current: &Session) -> Result<(), HostSessionScopeError> {
        self.validate()?;
        current
            .validate_conversation_state()
            .map_err(|_| HostSessionScopeError::Stale)?;
        let project = ActorSession::from_session(current)
            .map_err(|_| HostSessionScopeError::Stale)?
            .project_id;
        if !self
            .identity
            .matches_caller(&SessionCallerReference::from(current))
            || self.metadata_version != current.metadata_version
            || self.project_id.as_ref().map(ProjectId::as_str) != project.as_deref()
        {
            return Err(HostSessionScopeError::Stale);
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HostSessionTargetScope {
    pub observation: HostScopeSessionObservation,
    pub rights: BTreeSet<HostSessionRight>,
}

/// One explicit creation operation. Workspace bytes are already bound by Host;
/// the existing canonical workspace resolver must recheck them before creation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HostSessionCreateScope {
    pub operation_id: Uuid,
    pub project_id: ProjectId,
    pub workspace_path: String,
    /// Empty by default. Creation/receipts do not imply follow-up permission.
    #[serde(default)]
    pub created_target_rights: BTreeSet<HostSessionRight>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HostSessionScopeBinding {
    pub schema_version: u32,
    pub grant_id: Uuid,
    pub revision: u64,
    pub caller: HostScopeSessionObservation,
    pub targets: BTreeMap<String, HostSessionTargetScope>,
    pub creates: BTreeMap<Uuid, HostSessionCreateScope>,
}

impl HostSessionScopeBinding {
    pub fn validate(&self) -> Result<(), HostSessionScopeError> {
        self.caller.validate()?;
        if self.schema_version != HOST_SESSION_SCOPE_VERSION
            || self.grant_id.is_nil()
            || self.revision == 0
            || self.targets.len() > MAX_HOST_SESSION_TARGETS
            || self.creates.len() > MAX_HOST_SESSION_CREATES
            || (self.targets.is_empty() && self.creates.is_empty())
        {
            return Err(HostSessionScopeError::Invalid);
        }
        for (id, target) in &self.targets {
            target.observation.validate()?;
            if id != &target.observation.identity.session_id
                || id == &self.caller.identity.session_id
                || target.observation.project_id.is_none()
                || target.rights.is_empty()
            {
                return Err(HostSessionScopeError::Invalid);
            }
        }
        for (id, create) in &self.creates {
            let path = Path::new(&create.workspace_path);
            if id.is_nil()
                || id != &create.operation_id
                || !path.is_absolute()
                || create.workspace_path.trim() != create.workspace_path
                || path
                    .components()
                    .any(|c| matches!(c, Component::ParentDir | Component::CurDir))
            {
                return Err(HostSessionScopeError::Invalid);
            }
        }
        Ok(())
    }

    pub fn target(
        &self,
        id: &str,
        right: HostSessionRight,
    ) -> Result<&HostSessionTargetScope, HostSessionScopeError> {
        self.validate()?;
        self.targets
            .get(id)
            .filter(|target| target.rights.contains(&right))
            .ok_or(HostSessionScopeError::Denied)
    }

    pub fn create(
        &self,
        operation_id: Uuid,
        project: &ProjectId,
        workspace: &str,
    ) -> Result<&HostSessionCreateScope, HostSessionScopeError> {
        self.validate()?;
        self.creates
            .get(&operation_id)
            .filter(|scope| &scope.project_id == project && scope.workspace_path == workspace)
            .ok_or(HostSessionScopeError::Denied)
    }
}

/// Non-deserializable Host installation identity. Another issuer cannot mint a
/// token for a store's installed authority, even by copying all serialized data.
#[derive(Debug, Clone)]
pub struct HostSessionScopeIssuer(Arc<Uuid>);

#[derive(Debug, Clone)]
pub struct HostSessionScopeToken {
    issuer: Arc<Uuid>,
    binding: Arc<HostSessionScopeBinding>,
}

impl Default for HostSessionScopeIssuer {
    fn default() -> Self {
        Self(Arc::new(Uuid::new_v4()))
    }
}

impl HostSessionScopeIssuer {
    /// Trusted in-process Host API only. A registry must install the returned
    /// token and retain its publication fence through every final operation.
    pub fn issue(
        &self,
        binding: HostSessionScopeBinding,
    ) -> Result<HostSessionScopeToken, HostSessionScopeError> {
        binding.validate()?;
        Ok(HostSessionScopeToken {
            issuer: Arc::clone(&self.0),
            binding: Arc::new(binding),
        })
    }
    pub fn recognizes(&self, token: &HostSessionScopeToken) -> bool {
        Arc::ptr_eq(&self.0, &token.issuer)
    }
}

impl HostSessionScopeToken {
    pub fn binding(&self) -> &HostSessionScopeBinding {
        &self.binding
    }
    pub fn same_installation(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.issuer, &other.issuer) && Arc::ptr_eq(&self.binding, &other.binding)
    }
}

#[cfg(test)]
#[path = "host_session_scope_tests.rs"]
mod tests;