bamboo-engine 2026.10.22

Execution engine and orchestration 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
//! Host-only Ticket dispatch over canonical SessionInbox activation. Runtime
//! evidence lives in the existing Child control plane, never in Worker JSON.
use std::sync::Arc;

use bamboo_domain::{Session, SessionAuthorityIdentity, SessionKind, Storage};
use bamboo_tickets::{
    canonical_bytes, content_hash, AssignmentState, Authority, DispatchSpec, Error, Operation,
    Principal, Result, RuntimeReceipt, ScopeBinding, TicketService, WorkState,
};
use serde::{Deserialize, Serialize};

use crate::execution::spawn::SessionInboxRuntimeBinding;

pub const TICKET_DISPATCH_KEY: &str = "ticket.runtime.dispatch.v1";
mod result;
pub use result::{
    checkpoint_owned_task_stop, checkpoint_owned_ticket_stop, checkpoint_ticket_result,
    question_terminal_call_id,
};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TicketDispatchBinding {
    pub dispatch_key: String,
    pub spec_hash: String,
    pub assignment_id: String,
    pub binding: ScopeBinding,
    /// Immutable Host request ceiling, retained in canonical Child metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_rounds: Option<u32>,
    pub receipt: Option<RuntimeReceipt>,
    pub receipt_created_at: Option<chrono::DateTime<chrono::Utc>>,
}

#[derive(Clone, Debug, Serialize)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum DispatchObservation {
    Missing,
    Pending {
        session_id: String,
    },
    Admitted {
        receipt: RuntimeReceipt,
    },
    Terminal {
        receipt: RuntimeReceipt,
        terminal_status: String,
    },
    OutcomeUnknown {
        receipt: Option<RuntimeReceipt>,
        reason: String,
    },
}

pub fn ticket_child_id(dispatch_key: &str) -> String {
    format!("ticket-{}", &content_hash(dispatch_key.as_bytes())[..32])
}

/// Storage's strict Root reader validates its Supervisor proof/runtime sidecar.
/// No model/client scope token is accepted as authority.
pub async fn verified_scope_binding(
    storage: &dyn Storage,
    supervisor_id: &str,
) -> Result<(ScopeBinding, Session)> {
    let supervisor = storage
        .load_root_authority(supervisor_id)
        .await?
        .ok_or_else(|| Error::ScopeDenied("Supervisor missing".into()))?;
    let SessionAuthorityIdentity::Supervisor { incarnation_id } = supervisor.authority_identity
    else {
        return Err(Error::ScopeDenied("canonical Supervisor required".into()));
    };
    let Some(orchestration_revision) = supervisor.supervisor_orchestration_revision() else {
        return Err(Error::ScopeDenied(
            "Supervisor orchestration authority required".into(),
        ));
    };
    let binding = ScopeBinding {
        scope_id: format!("supervisor/{incarnation_id}"),
        supervisor_session_id: supervisor.id.clone(),
        binding_revision: orchestration_revision,
    };
    Ok((binding, supervisor))
}

/// Existing Work keeps its sealed binding revision across thinking-mode changes.
/// Only the strict canonical Supervisor reader can authorize that same namespace;
/// a different incarnation, Supervisor id or a zero revision remains denied.
pub async fn verified_scope_binding_for_service(
    storage: &dyn Storage,
    service: &TicketService,
    supervisor_id: &str,
) -> Result<(ScopeBinding, Session)> {
    let (expected, supervisor) = verified_scope_binding(storage, supervisor_id).await?;
    let actual = service.published()?.1.binding;
    if actual.scope_id != expected.scope_id
        || actual.supervisor_session_id != expected.supervisor_session_id
        || actual.binding_revision == 0
    {
        return Err(Error::ScopeDenied(
            "Supervisor scope binding changed".into(),
        ));
    }
    Ok((actual, supervisor))
}

pub fn dispatch_binding(
    service: &TicketService,
    key: &str,
    spec: &DispatchSpec,
) -> Result<TicketDispatchBinding> {
    let (_, snapshot) = service.published()?;
    let intent = snapshot
        .intents
        .get(key)
        .ok_or_else(|| Error::InvalidTransition("dispatch intent missing".into()))?;
    let spec_hash = content_hash(&canonical_bytes(spec)?);
    if snapshot.binding != spec.binding || intent.spec_hash != spec_hash {
        return Err(Error::IdempotencyConflict);
    }
    Ok(TicketDispatchBinding {
        dispatch_key: key.into(),
        spec_hash,
        assignment_id: spec.assignment_id.clone(),
        binding: snapshot.binding,
        max_rounds: None,
        receipt: None,
        receipt_created_at: None,
    })
}

pub fn require_dispatch_permission(
    service: &TicketService,
    dispatch: &TicketDispatchBinding,
) -> Result<()> {
    if service.health() != bamboo_tickets::Health::Writable {
        return Err(Error::AuthorityUnavailable(
            "dispatch requires writable authority".into(),
        ));
    }
    let (_, snapshot) = service.published()?;
    let assignment = snapshot
        .assignments
        .get(&dispatch.assignment_id)
        .ok_or_else(|| Error::ScopeDenied("Assignment missing".into()))?;
    let work = &snapshot.tickets[&assignment.work_id];
    if assignment.process_stopped
        || snapshot.binding != dispatch.binding
        || snapshot
            .intents
            .get(&dispatch.dispatch_key)
            .is_none_or(|i| i.spec_hash != dispatch.spec_hash)
        || assignment.dispatch_key != dispatch.dispatch_key
        || assignment.authority_epoch != snapshot.authority_epoch
        || work.generation != assignment.generation
        || work.contract_revision != assignment.contract_revision
        || work.active_assignment.as_deref() != Some(&assignment.id)
        || work.state != WorkState::Active
        || !matches!(
            assignment.state,
            AssignmentState::DispatchPending | AssignmentState::Admitted | AssignmentState::Running
        )
    {
        return Err(Error::ScopeDenied(
            "dispatch permission is stale or inactive".into(),
        ));
    }
    Ok(())
}

pub fn read_dispatch(session: &Session) -> Result<Option<TicketDispatchBinding>> {
    session
        .metadata
        .get(TICKET_DISPATCH_KEY)
        .map(|raw| {
            if raw.len() > 4096 {
                return Err(Error::ScopeDenied("dispatch binding exceeds limit".into()));
            }
            Ok(serde_json::from_str(raw)?)
        })
        .transpose()
}

/// Ticket owns this child's result/recovery lifecycle. A short Supervisor turn
/// must not acquire the ordinary SubAgent orphan wait on its behalf. Inspect
/// the canonical Host control plane; a model label or a cached replica cannot
/// confer this exemption. Explicit SubAgent waits keep their existing policy.
/// This classifies the durable owner, not a model action: parent thinking-mode
/// changes must not transfer an independent Work child into orphan cleanup.
/// Final model operations still validate their complete original generation.
pub(crate) async fn is_independent_ticket_child(
    storage: &dyn Storage,
    parent: &Session,
    child_id: &str,
) -> bool {
    let Ok((binding, canonical_parent)) = verified_scope_binding(storage, &parent.id).await else {
        return false;
    };
    if parent.kind != SessionKind::Root
        || parent.parent_session_id.is_some()
        || parent.root_session_id != parent.id
        || canonical_parent.created_at != parent.created_at
        || canonical_parent.authority_identity != parent.authority_identity
    {
        return false;
    }
    let Ok(Some(child)) = storage.load_runtime_control_plane(child_id).await else {
        return false;
    };
    let Ok(Some(dispatch)) = read_dispatch(&child) else {
        return false;
    };
    child.kind == SessionKind::Child
        && child.parent_session_id.as_deref() == Some(parent.id.as_str())
        && child.parent_created_at == Some(parent.created_at)
        && child.root_session_id == parent.id
        && child.id == ticket_child_id(&dispatch.dispatch_key)
        && !dispatch.dispatch_key.is_empty()
        && !dispatch.assignment_id.is_empty()
        && dispatch.binding.scope_id == binding.scope_id
        && dispatch.binding.supervisor_session_id == binding.supervisor_session_id
        && dispatch.binding.binding_revision > 0
        && child
            .metadata
            .get(crate::ticket_worker_plan::TICKET_LOCAL_PLAN_KEY)
            == Some(&dispatch.assignment_id)
}

pub async fn query_dispatch(
    storage: &dyn Storage,
    service: &TicketService,
    key: &str,
    spec: &DispatchSpec,
) -> Result<DispatchObservation> {
    let expected = dispatch_binding(service, key, spec)?;
    let (binding, _) = verified_scope_binding_for_service(
        storage,
        service,
        &expected.binding.supervisor_session_id,
    )
    .await?;
    if binding != expected.binding {
        return Err(Error::ScopeDenied("Supervisor binding changed".into()));
    }
    let child = storage
        .load_runtime_control_plane(&ticket_child_id(key))
        .await?;
    let Some(child) = child else {
        return Ok(
            if require_dispatch_permission(service, &expected).is_ok()
                && service.published()?.1.assignments[&spec.assignment_id]
                    .runtime
                    .is_none()
            {
                DispatchObservation::Missing
            } else {
                DispatchObservation::OutcomeUnknown {
                    receipt: None,
                    reason: "canonical dispatch evidence unavailable".into(),
                }
            },
        );
    };
    let actual = read_dispatch(&child)?
        .ok_or_else(|| Error::ScopeDenied("Child lacks dispatch binding".into()))?;
    let mut comparable = actual.clone();
    comparable.receipt = None;
    comparable.receipt_created_at = None;
    comparable.max_rounds = None;
    if comparable != expected
        || child.kind != SessionKind::Child
        || child.parent_session_id.as_deref() != Some(&binding.supervisor_session_id)
        || child
            .metadata
            .get(crate::ticket_worker_plan::TICKET_LOCAL_PLAN_KEY)
            != Some(&expected.assignment_id)
    {
        return Err(Error::ScopeDenied(
            "canonical Child dispatch changed".into(),
        ));
    }
    if let Some(receipt) = actual.receipt {
        if receipt.dispatch_key != key
            || receipt.spec_hash != actual.spec_hash
            || receipt.session_id != child.id
            || receipt.run_id.is_empty()
            || actual.receipt_created_at != Some(child.created_at)
        {
            return Err(Error::ScopeDenied(
                "canonical receipt identity changed".into(),
            ));
        }
        let snapshot = service.published()?.1;
        if snapshot.assignments[&spec.assignment_id].runtime.as_ref() != Some(&receipt) {
            return Ok(DispatchObservation::OutcomeUnknown {
                receipt: Some(receipt),
                reason: "Runtime receipt prepared but Ticket admission not confirmed".into(),
            });
        }
        if snapshot.assignments[&spec.assignment_id].state == AssignmentState::OutcomeUnknown {
            // Startup may mark the Session's abandoned runner as an error.
            // That display status cannot prove the old OS process stopped or
            // reconcile its effects, so preserve the authoritative quarantine.
            return Ok(DispatchObservation::OutcomeUnknown {
                receipt: Some(receipt),
                reason: "Ticket attempt requires Runtime stop/result reconciliation".into(),
            });
        }
        let status = child.last_run_status();
        if let Some(status @ ("completed" | "error" | "timeout" | "cancelled" | "skipped")) =
            status.as_deref()
        {
            return Ok(DispatchObservation::Terminal {
                receipt,
                terminal_status: status.into(),
            });
        }
        if require_dispatch_permission(service, &expected).is_err() {
            return Ok(DispatchObservation::OutcomeUnknown {
                receipt: Some(receipt),
                reason: "old writer or revoked execution; reconciliation required".into(),
            });
        }
        Ok(DispatchObservation::Admitted { receipt })
    } else if require_dispatch_permission(service, &expected).is_ok()
        && child.last_run_status().as_deref() == Some("pending")
    {
        Ok(DispatchObservation::Pending {
            session_id: child.id,
        })
    } else {
        Ok(DispatchObservation::OutcomeUnknown {
            receipt: None,
            reason: "dispatch has no confirmed run receipt".into(),
        })
    }
}

fn actual_with_receipt(
    expected: &TicketDispatchBinding,
    receipt: &RuntimeReceipt,
    created_at: chrono::DateTime<chrono::Utc>,
) -> TicketDispatchBinding {
    TicketDispatchBinding {
        receipt: Some(receipt.clone()),
        receipt_created_at: Some(created_at),
        ..expected.clone()
    }
}

/// Called after canonical activation registration, before sending any RunSpec.
/// The Host receipt is committed first; an uncertain Ticket publication blocks
/// execution. No lease or Worker event can supply the Run identity.
pub async fn admit_ticket_run(
    service: &Arc<TicketService>,
    runtime: &SessionInboxRuntimeBinding,
    session: &mut Session,
    run_id: &str,
) -> Result<()> {
    let dispatch = read_dispatch(session)?
        .ok_or_else(|| Error::ScopeDenied("dispatch binding missing".into()))?;
    require_dispatch_permission(service, &dispatch)?;
    let (binding, _) = verified_scope_binding_for_service(
        runtime.storage.as_ref(),
        service,
        &dispatch.binding.supervisor_session_id,
    )
    .await?;
    if binding != dispatch.binding || !runtime.router.owns_run(&session.id, run_id).await {
        return Err(Error::ScopeDenied(
            "canonical activation ownership changed".into(),
        ));
    }
    let store = runtime.parent_question_lock.as_ref().ok_or_else(|| {
        Error::AuthorityUnavailable("canonical write coordinator unavailable".into())
    })?;
    let receipt = RuntimeReceipt {
        dispatch_key: dispatch.dispatch_key.clone(),
        spec_hash: dispatch.spec_hash.clone(),
        session_id: session.id.clone(),
        run_id: run_id.into(),
    };
    let saved = store
        .mutate_runtime_session_and_publish(
            &session.id,
            || None,
            |latest| {
                let current = read_dispatch(latest)?
                    .ok_or_else(|| Error::ScopeDenied("canonical dispatch missing".into()))?;
                let mut comparable = current.clone();
                comparable.receipt = None;
                comparable.receipt_created_at = None;
                let mut expected = dispatch.clone();
                expected.receipt = None;
                expected.receipt_created_at = None;
                if latest.created_at != session.created_at
                    || latest.parent_session_id != session.parent_session_id
                    || latest.kind != SessionKind::Child
                    || comparable != expected
                    || current.receipt.as_ref().is_some_and(|r| r != &receipt)
                    || current.receipt.is_some()
                        && current.receipt_created_at != Some(latest.created_at)
                {
                    return Err(Error::ScopeDenied(
                        "dispatch birth or receipt changed".into(),
                    ));
                }
                if current.receipt.is_none() {
                    latest.metadata.insert(
                        TICKET_DISPATCH_KEY.into(),
                        serde_json::to_string(&actual_with_receipt(
                            &dispatch,
                            &receipt,
                            latest.created_at,
                        ))?,
                    );
                    latest.metadata_version =
                        latest.metadata_version.checked_add(1).ok_or_else(|| {
                            Error::AuthorityUnavailable("metadata revision overflow".into())
                        })?;
                }
                Ok::<_, Error>(())
            },
            |_| {},
        )
        .await??
        .ok_or_else(|| Error::ScopeDenied("canonical Child missing".into()))?;
    // Preserve the same Host-owned receipt in the eventual final checkpoint.
    session.metadata.insert(
        TICKET_DISPATCH_KEY.into(),
        saved.metadata[TICKET_DISPATCH_KEY].clone(),
    );
    session.metadata_version = saved.metadata_version;
    let authority = Authority::from_verified_host(dispatch.binding, Principal::Runtime);
    execute_runtime_command(
        service,
        &authority,
        &format!("runtime-admit/{}", dispatch.dispatch_key),
        vec![
            Operation::Admitted {
                assignment_id: dispatch.assignment_id.clone(),
                receipt,
            },
            Operation::Running {
                assignment_id: dispatch.assignment_id.clone(),
            },
        ],
    )?;
    // A restarted writer may only replay a historical receipt. It must not
    // inherit the old Worker's run permission from that receipt.
    crate::ticket_worker_plan::TicketWorkerPlan::from_runtime_receipt(
        service.clone(),
        &dispatch.assignment_id,
    )?;
    Ok(())
}

pub(crate) fn execute_runtime_command(
    service: &TicketService,
    authority: &Authority,
    id: &str,
    operations: Vec<Operation>,
) -> Result<bamboo_tickets::OperationReceipt> {
    for attempt in 0..4 {
        let command = service.prepare_command(authority, id, operations.clone())?;
        match service.execute(authority, &command) {
            Err(Error::RevisionConflict) if attempt < 3 => continue,
            result => return result,
        }
    }
    unreachable!()
}

#[cfg(test)]
mod tests;