tea-session 0.1.0

Append-only session log, replay, and storage contracts for tea-rs
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use std::collections::BTreeMap;

use serde_json::Value;
use tea_protocol::{
    ApprovalDecision, ApprovalId, BranchId, CanonicalMessage, ContentBlock, ExecutionTarget,
    MessageId, ModelRef, PolicyDecision, ProfileId, ProtocolMetadata, ProtocolTimestamp,
    ReasoningEffort, RecordId, RunId, SessionId, SessionSequence, ToolCallId, ToolFailure,
    ToolIdempotency, ToolPresentation, TurnId,
};

/// Active model and product-profile configuration derived from durable records.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionConfiguration {
    model: Option<ModelRef>,
    profile_id: ProfileId,
    reasoning_effort: Option<ReasoningEffort>,
}

impl SessionConfiguration {
    /// Returns the selected model, when one has been configured.
    #[must_use]
    pub const fn model_ref(&self) -> Option<&ModelRef> {
        self.model.as_ref()
    }

    /// Returns the provider-local model selector, when one is configured.
    #[must_use]
    pub const fn model_id(&self) -> Option<&tea_protocol::ModelId> {
        match &self.model {
            Some(model) => Some(model.model_id()),
            None => None,
        }
    }

    /// Returns the selected provider, when one is configured.
    #[must_use]
    pub const fn provider_id(&self) -> Option<&tea_protocol::ProviderId> {
        match &self.model {
            Some(model) => Some(model.provider_id()),
            None => None,
        }
    }

    /// Returns the selected product profile.
    #[must_use]
    pub const fn profile_id(&self) -> &ProfileId {
        &self.profile_id
    }

    /// Returns the explicit session reasoning effort, when configured.
    #[must_use]
    pub const fn reasoning_effort(&self) -> Option<ReasoningEffort> {
        self.reasoning_effort
    }
}

/// Durable pending approval reconstructed from canonical records.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PendingApproval {
    approval_id: ApprovalId,
    tool_call_id: ToolCallId,
    expires_at: ProtocolTimestamp,
    requested_at: ProtocolTimestamp,
}

impl PendingApproval {
    /// Returns the approval identity.
    #[must_use]
    pub const fn approval_id(&self) -> ApprovalId {
        self.approval_id
    }

    /// Returns the tool call awaiting a decision.
    #[must_use]
    pub const fn tool_call_id(&self) -> ToolCallId {
        self.tool_call_id
    }

    /// Returns the caller-clock expiry boundary.
    #[must_use]
    pub const fn expires_at(&self) -> ProtocolTimestamp {
        self.expires_at
    }

    /// Returns when the canonical request became durable.
    #[must_use]
    pub const fn requested_at(&self) -> ProtocolTimestamp {
        self.requested_at
    }
}

/// Durable execution/recovery state for a tool call.
#[derive(Debug, Clone, PartialEq)]
pub enum ToolExecutionState {
    /// The tool call exists but execution has not started.
    NotStarted,
    /// Execution crossed its durable start boundary.
    Started {
        /// Selected execution boundary.
        execution_target: ExecutionTarget,
        /// Recovery semantics declared at start.
        idempotency: ToolIdempotency,
    },
    /// Execution reached a durable terminal result.
    Finished {
        /// Whether the terminal result is an error.
        is_error: bool,
        /// Canonical terminal content committed before a tool-result message.
        content: Vec<ContentBlock>,
        /// Machine-readable failure when terminal result is an error.
        error: Option<ToolFailure>,
        /// Optional UI-only presentation retained out of model context.
        presentation: Option<ToolPresentation>,
    },
    /// Execution was interrupted after start and has uncertain outcome.
    Interrupted {
        /// English technical recovery diagnostic.
        reason: String,
        /// Selected execution boundary.
        execution_target: ExecutionTarget,
        /// Recovery semantics declared at start.
        idempotency: ToolIdempotency,
    },
}

/// Materialized lifecycle of one requested tool call.
#[derive(Debug, Clone, PartialEq)]
pub struct ToolCallState {
    tool_call_id: ToolCallId,
    tool_name: String,
    arguments: Value,
    policy_decision: Option<PolicyDecision>,
    approval_id: Option<ApprovalId>,
    approval_decision: Option<ApprovalDecision>,
    execution: ToolExecutionState,
    result_message_id: Option<MessageId>,
}

impl ToolCallState {
    /// Returns the stable tool-call identity.
    #[must_use]
    pub const fn tool_call_id(&self) -> ToolCallId {
        self.tool_call_id
    }

    /// Returns the registered tool name.
    #[must_use]
    pub fn tool_name(&self) -> &str {
        &self.tool_name
    }

    /// Returns validated provider-neutral arguments.
    #[must_use]
    pub const fn arguments(&self) -> &Value {
        &self.arguments
    }

    /// Returns the durable policy decision, when evaluated.
    #[must_use]
    pub const fn policy_decision(&self) -> Option<PolicyDecision> {
        self.policy_decision
    }

    /// Returns the associated approval identity, when requested.
    #[must_use]
    pub const fn approval_id(&self) -> Option<ApprovalId> {
        self.approval_id
    }

    /// Returns the terminal approval decision, when resolved.
    #[must_use]
    pub const fn approval_decision(&self) -> Option<ApprovalDecision> {
        self.approval_decision
    }

    /// Returns durable execution/recovery state.
    #[must_use]
    pub const fn execution(&self) -> &ToolExecutionState {
        &self.execution
    }

    /// Returns the sole committed result message, when present.
    #[must_use]
    pub const fn result_message_id(&self) -> Option<MessageId> {
        self.result_message_id
    }
}

/// Provider-run state required for restart diagnostics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RunRecoveryState {
    /// Provider streaming was interrupted before durable terminal output.
    Interrupted {
        /// Active turn at interruption.
        turn_id: TurnId,
        /// English technical diagnostic.
        reason: String,
    },
    /// The run was explicitly cancelled.
    Cancelled,
}

/// Durable turn boundary reconstructed during replay.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TurnCheckpoint {
    run_id: RunId,
    turn_id: TurnId,
    record_id: RecordId,
    sequence: SessionSequence,
    next_action: tea_protocol::NextTurnAction,
}

impl TurnCheckpoint {
    pub(crate) const fn new(
        run_id: RunId,
        turn_id: TurnId,
        record_id: RecordId,
        sequence: SessionSequence,
        next_action: tea_protocol::NextTurnAction,
    ) -> Self {
        Self {
            run_id,
            turn_id,
            record_id,
            sequence,
            next_action,
        }
    }

    /// Returns the checkpointed run.
    #[must_use]
    pub const fn run_id(&self) -> RunId {
        self.run_id
    }

    /// Returns the checkpointed turn.
    #[must_use]
    pub const fn turn_id(&self) -> TurnId {
        self.turn_id
    }

    /// Returns the record establishing this checkpoint.
    #[must_use]
    pub const fn record_id(&self) -> RecordId {
        self.record_id
    }

    /// Returns the authoritative sequence.
    #[must_use]
    pub const fn sequence(&self) -> SessionSequence {
        self.sequence
    }

    /// Returns the next action allowed by the checkpoint.
    #[must_use]
    pub const fn next_action(&self) -> tea_protocol::NextTurnAction {
        self.next_action
    }
}

/// Latest durable compaction summary and its source provenance.
#[derive(Debug, Clone, PartialEq)]
pub struct SessionCompaction {
    summary: CanonicalMessage,
    compacted_through_record_id: RecordId,
}

impl SessionCompaction {
    /// Returns the canonical summary message used for future model context.
    #[must_use]
    pub const fn summary(&self) -> &CanonicalMessage {
        &self.summary
    }

    /// Returns the last source record replaced in model context.
    #[must_use]
    pub const fn compacted_through_record_id(&self) -> RecordId {
        self.compacted_through_record_id
    }
}

/// Summary of one durable branch.
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(clippy::struct_field_names)] // Explicit `_id` fields retain strong identifier semantics.
pub struct BranchSummary {
    branch_id: BranchId,
    source_branch_id: Option<BranchId>,
    from_record_id: RecordId,
    leaf_record_id: RecordId,
}

impl BranchSummary {
    pub(crate) const fn new(
        branch_id: BranchId,
        source_branch_id: Option<BranchId>,
        from_record_id: RecordId,
        leaf_record_id: RecordId,
    ) -> Self {
        Self {
            branch_id,
            source_branch_id,
            from_record_id,
            leaf_record_id,
        }
    }

    pub(crate) fn set_leaf(&mut self, record_id: RecordId) {
        self.leaf_record_id = record_id;
    }

    /// Returns branch identity.
    #[must_use]
    pub const fn branch_id(&self) -> BranchId {
        self.branch_id
    }

    /// Returns source branch for a fork, or `None` for the root branch.
    #[must_use]
    pub const fn source_branch_id(&self) -> Option<BranchId> {
        self.source_branch_id
    }

    /// Returns the durable source position used to create this branch.
    #[must_use]
    pub const fn from_record_id(&self) -> RecordId {
        self.from_record_id
    }

    /// Returns the current durable leaf record.
    #[must_use]
    pub const fn leaf_record_id(&self) -> RecordId {
        self.leaf_record_id
    }
}

/// Deterministic session projection derived only from the append-only record log.
#[derive(Debug, Clone, PartialEq)]
pub struct MaterializedSessionState {
    pub(crate) session_id: SessionId,
    pub(crate) tail_sequence: SessionSequence,
    pub(crate) tail_record_id: RecordId,
    pub(crate) metadata: ProtocolMetadata,
    pub(crate) configuration: SessionConfiguration,
    pub(crate) messages: Vec<CanonicalMessage>,
    pub(crate) pending_approvals: BTreeMap<ApprovalId, PendingApproval>,
    pub(crate) tool_calls: BTreeMap<ToolCallId, ToolCallState>,
    pub(crate) active_branch_id: Option<BranchId>,
    pub(crate) branches: BTreeMap<BranchId, BranchSummary>,
    pub(crate) run_recovery: BTreeMap<RunId, RunRecoveryState>,
    pub(crate) latest_checkpoint: Option<TurnCheckpoint>,
    pub(crate) latest_compaction: Option<SessionCompaction>,
}

impl MaterializedSessionState {
    /// Returns session identity.
    #[must_use]
    pub const fn session_id(&self) -> SessionId {
        self.session_id
    }

    /// Returns the current authoritative tail sequence.
    #[must_use]
    pub const fn tail_sequence(&self) -> SessionSequence {
        self.tail_sequence
    }

    /// Returns the current tail record identity.
    #[must_use]
    pub const fn tail_record_id(&self) -> RecordId {
        self.tail_record_id
    }

    /// Returns bounded creation metadata.
    #[must_use]
    pub const fn metadata(&self) -> &ProtocolMetadata {
        &self.metadata
    }

    /// Returns active configuration.
    #[must_use]
    pub const fn configuration(&self) -> &SessionConfiguration {
        &self.configuration
    }

    /// Returns the active durable transcript.
    #[must_use]
    pub fn messages(&self) -> &[CanonicalMessage] {
        &self.messages
    }

    /// Returns pending approvals in stable ID order.
    #[must_use]
    pub const fn pending_approvals(&self) -> &BTreeMap<ApprovalId, PendingApproval> {
        &self.pending_approvals
    }

    /// Returns tool-call lifecycle projections in stable ID order.
    #[must_use]
    pub const fn tool_calls(&self) -> &BTreeMap<ToolCallId, ToolCallState> {
        &self.tool_calls
    }

    /// Returns active branch, or `None` for a legacy unbranched Protocol 1.0 log.
    #[must_use]
    pub const fn active_branch_id(&self) -> Option<BranchId> {
        self.active_branch_id
    }

    /// Returns durable branch summaries in stable ID order.
    #[must_use]
    pub const fn branches(&self) -> &BTreeMap<BranchId, BranchSummary> {
        &self.branches
    }

    /// Returns provider-run restart diagnostics.
    #[must_use]
    pub const fn run_recovery(&self) -> &BTreeMap<RunId, RunRecoveryState> {
        &self.run_recovery
    }

    /// Returns the latest durable turn checkpoint.
    #[must_use]
    pub const fn latest_checkpoint(&self) -> Option<&TurnCheckpoint> {
        self.latest_checkpoint.as_ref()
    }

    /// Returns the latest compaction summary without removing original messages.
    #[must_use]
    pub const fn latest_compaction(&self) -> Option<&SessionCompaction> {
        self.latest_compaction.as_ref()
    }
}

pub(crate) fn message_id(message: &CanonicalMessage) -> MessageId {
    match message {
        CanonicalMessage::User { id, .. }
        | CanonicalMessage::Assistant { id, .. }
        | CanonicalMessage::ToolResult { id, .. } => *id,
    }
}

pub(crate) fn declared_tool_calls(
    message: &CanonicalMessage,
) -> impl Iterator<Item = (ToolCallId, &str, &Value)> {
    let content = match message {
        CanonicalMessage::Assistant { content, .. } => Some(content.as_slice()),
        CanonicalMessage::User { .. } | CanonicalMessage::ToolResult { .. } => None,
    };
    content
        .into_iter()
        .flatten()
        .filter_map(|block| match block {
            ContentBlock::ToolCall {
                tool_call_id,
                tool_name,
                arguments,
                ..
            } => Some((*tool_call_id, tool_name.as_str(), arguments)),
            ContentBlock::Text { .. }
            | ContentBlock::Thinking { .. }
            | ContentBlock::Image { .. }
            | ContentBlock::HostedTool { .. }
            | ContentBlock::Citation { .. } => None,
        })
}

pub(crate) fn new_state(
    record_id: RecordId,
    session_id: SessionId,
    sequence: SessionSequence,
    profile_id: ProfileId,
    metadata: ProtocolMetadata,
    root_branch_id: Option<BranchId>,
) -> MaterializedSessionState {
    let branches = root_branch_id.map_or_else(BTreeMap::new, |branch_id| {
        BTreeMap::from([(
            branch_id,
            BranchSummary {
                branch_id,
                source_branch_id: None,
                from_record_id: record_id,
                leaf_record_id: record_id,
            },
        )])
    });
    MaterializedSessionState {
        session_id,
        tail_sequence: sequence,
        tail_record_id: record_id,
        metadata,
        configuration: SessionConfiguration {
            model: None,
            profile_id,
            reasoning_effort: None,
        },
        messages: Vec::new(),
        pending_approvals: BTreeMap::new(),
        tool_calls: BTreeMap::new(),
        active_branch_id: root_branch_id,
        branches,
        run_recovery: BTreeMap::new(),
        latest_checkpoint: None,
        latest_compaction: None,
    }
}

pub(crate) fn new_pending_approval(
    approval_id: ApprovalId,
    tool_call_id: ToolCallId,
    expires_at: ProtocolTimestamp,
    requested_at: ProtocolTimestamp,
) -> PendingApproval {
    PendingApproval {
        approval_id,
        tool_call_id,
        expires_at,
        requested_at,
    }
}

pub(crate) fn new_tool_call(
    tool_call_id: ToolCallId,
    tool_name: String,
    arguments: Value,
) -> ToolCallState {
    ToolCallState {
        tool_call_id,
        tool_name,
        arguments,
        policy_decision: None,
        approval_id: None,
        approval_decision: None,
        execution: ToolExecutionState::NotStarted,
        result_message_id: None,
    }
}

pub(crate) fn set_model(configuration: &mut SessionConfiguration, model: ModelRef) {
    configuration.model = Some(model);
}

pub(crate) fn set_profile(configuration: &mut SessionConfiguration, profile_id: ProfileId) {
    configuration.profile_id = profile_id;
}

pub(crate) fn set_reasoning_effort(
    configuration: &mut SessionConfiguration,
    reasoning_effort: ReasoningEffort,
) {
    configuration.reasoning_effort = Some(reasoning_effort);
}

pub(crate) fn set_policy(tool: &mut ToolCallState, decision: PolicyDecision) {
    tool.policy_decision = Some(decision);
}

pub(crate) fn set_approval(tool: &mut ToolCallState, approval_id: ApprovalId) {
    tool.approval_id = Some(approval_id);
}

pub(crate) fn resolve_approval(tool: &mut ToolCallState, decision: ApprovalDecision) {
    tool.approval_decision = Some(decision);
}

pub(crate) fn start_tool(
    tool: &mut ToolCallState,
    execution_target: ExecutionTarget,
    idempotency: ToolIdempotency,
) {
    tool.execution = ToolExecutionState::Started {
        execution_target,
        idempotency,
    };
}

pub(crate) fn finish_tool(
    tool: &mut ToolCallState,
    is_error: bool,
    content: Vec<ContentBlock>,
    error: Option<ToolFailure>,
    presentation: Option<ToolPresentation>,
) {
    tool.execution = ToolExecutionState::Finished {
        is_error,
        content,
        error,
        presentation,
    };
}

pub(crate) fn set_compaction(
    state: &mut MaterializedSessionState,
    summary: CanonicalMessage,
    compacted_through_record_id: RecordId,
) {
    // Replace the compacted prefix with the summary message so the model-visible
    // transcript begins with the summary followed by later uncompacted records.
    // Original records remain in the durable log for audit and replay.
    state.messages.clear();
    state.messages.push(summary.clone());
    state.latest_compaction = Some(SessionCompaction {
        summary,
        compacted_through_record_id,
    });
}

pub(crate) fn commit_tool_result(tool: &mut ToolCallState, message_id: MessageId) {
    tool.result_message_id = Some(message_id);
}

pub(crate) fn interrupt_tool(tool: &mut ToolCallState, reason: String) {
    if let ToolExecutionState::Started {
        execution_target,
        idempotency,
    } = tool.execution
    {
        tool.execution = ToolExecutionState::Interrupted {
            reason,
            execution_target,
            idempotency,
        };
    }
}