theway-core 0.1.21

theway core — stateful agent runtime + harness (Agent loop, skills, prompt templates, sessions, compaction) on top of theway-llm-provider.
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
//! Session entry types + `Session` facade. 1:1 port of
//! `packages/agent/src/harness/session/session.ts` plus the entry types from `harness/types.ts`.
//!
//! Append-only entry-tree model: every entry is one of [`SessionTreeEntry`]'s tagged
//! variants. The `Session` struct wraps a [`SessionStorage`] trait object and adds typed
//! `append_*` helpers plus `build_context` (parent-chain replay).

use std::sync::Arc;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::types::AgentMessage;

use super::super::types::{SessionError, SessionErrorCode};
use theway_contract::dag::PersistedRun;

pub use theway_contract::session::{JsonlSessionMetadata, SessionImportOrigin, SessionMetadata};

/// Custom entry type used to persist a session's subagent graph state.
pub const SESSION_GRAPH_STATE_CUSTOM_TYPE: &str = "session_graph_state";

// ──────────────────────────────────────────────────────────────────────────────────────────
// Entry types
// ──────────────────────────────────────────────────────────────────────────────────────────

/// One entry in a session's append-only tree. Tagged by `type`.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SessionTreeEntry {
    Message {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        message: AgentMessage,
    },
    ThinkingLevelChange {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        #[serde(rename = "thinkingLevel")]
        thinking_level: String,
    },
    ModelChange {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        provider: String,
        #[serde(rename = "modelId")]
        model_id: String,
    },
    Compaction {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        summary: String,
        #[serde(rename = "firstKeptEntryId")]
        first_kept_entry_id: String,
        #[serde(rename = "tokensBefore")]
        tokens_before: u64,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        details: Option<Value>,
        #[serde(default, rename = "fromHook", skip_serializing_if = "Option::is_none")]
        from_hook: Option<bool>,
    },
    BranchSummary {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        #[serde(rename = "fromId")]
        from_id: String,
        summary: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        details: Option<Value>,
        #[serde(default, rename = "fromHook", skip_serializing_if = "Option::is_none")]
        from_hook: Option<bool>,
    },
    Custom {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        #[serde(rename = "customType")]
        custom_type: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        data: Option<Value>,
    },
    CustomMessage {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        #[serde(rename = "customType")]
        custom_type: String,
        content: Value,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        details: Option<Value>,
        display: bool,
    },
    Label {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        #[serde(rename = "targetId")]
        target_id: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        label: Option<String>,
    },
    SessionInfo {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
    },
    Leaf {
        id: String,
        #[serde(rename = "parentId")]
        parent_id: Option<String>,
        timestamp: String,
        #[serde(rename = "targetId")]
        target_id: Option<String>,
    },
}

impl SessionTreeEntry {
    pub fn id(&self) -> &str {
        match self {
            Self::Message { id, .. }
            | Self::ThinkingLevelChange { id, .. }
            | Self::ModelChange { id, .. }
            | Self::Compaction { id, .. }
            | Self::BranchSummary { id, .. }
            | Self::Custom { id, .. }
            | Self::CustomMessage { id, .. }
            | Self::Label { id, .. }
            | Self::SessionInfo { id, .. }
            | Self::Leaf { id, .. } => id,
        }
    }

    pub fn parent_id(&self) -> Option<&str> {
        match self {
            Self::Message { parent_id, .. }
            | Self::ThinkingLevelChange { parent_id, .. }
            | Self::ModelChange { parent_id, .. }
            | Self::Compaction { parent_id, .. }
            | Self::BranchSummary { parent_id, .. }
            | Self::Custom { parent_id, .. }
            | Self::CustomMessage { parent_id, .. }
            | Self::Label { parent_id, .. }
            | Self::SessionInfo { parent_id, .. }
            | Self::Leaf { parent_id, .. } => parent_id.as_deref(),
        }
    }

    pub fn type_str(&self) -> &'static str {
        match self {
            Self::Message { .. } => "message",
            Self::ThinkingLevelChange { .. } => "thinking_level_change",
            Self::ModelChange { .. } => "model_change",
            Self::Compaction { .. } => "compaction",
            Self::BranchSummary { .. } => "branch_summary",
            Self::Custom { .. } => "custom",
            Self::CustomMessage { .. } => "custom_message",
            Self::Label { .. } => "label",
            Self::SessionInfo { .. } => "session_info",
            Self::Leaf { .. } => "leaf",
        }
    }
}

// ──────────────────────────────────────────────────────────────────────────────────────────
// Context + metadata
// ──────────────────────────────────────────────────────────────────────────────────────────

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SessionContext {
    pub messages: Vec<AgentMessage>,
    pub thinking_level: String,
    pub model: Option<SessionContextModel>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SessionContextModel {
    pub provider: String,
    #[serde(rename = "modelId")]
    pub model_id: String,
}

// ──────────────────────────────────────────────────────────────────────────────────────────
// Subagent graph state
// ──────────────────────────────────────────────────────────────────────────────────────────

/// Serializable projection of a tracked subagent job. Kept deliberately small:
/// full transcripts remain addressable through `raw_text_ref` / job ids.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubagentJobSnapshot {
    pub id: String,
    pub agent: String,
    pub source: String,
    pub run_id: Option<String>,
    pub node_id: Option<String>,
    pub session_id: Option<String>,
    pub status: String,
    pub started_at: Option<i64>,
    pub completed_at: Option<i64>,
    pub attempt: u32,
    pub total_attempts: u32,
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub chars: u64,
    pub tools_called: u64,
    pub turn: u32,
    pub error: Option<String>,
    pub output_tail: String,
    pub truncated: bool,
    pub live_preview: Option<String>,
    pub tps: Option<f64>,
    pub cps: Option<f64>,
}

/// Session-scoped subagent graph state, persisted as a `session_graph_state`
/// custom entry (similar to `goal_state`).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SessionGraphState {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dags: Vec<PersistedRun>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub subagents: Vec<SubagentJobSnapshot>,
}

/// Find the newest `session_graph_state` custom entry in root-to-leaf order.
pub fn latest_session_graph_state(entries: &[SessionTreeEntry]) -> Option<SessionGraphState> {
    entries.iter().rev().find_map(|entry| {
        let SessionTreeEntry::Custom {
            custom_type, data, ..
        } = entry
        else {
            return None;
        };
        if custom_type != SESSION_GRAPH_STATE_CUSTOM_TYPE {
            return None;
        }
        serde_json::from_value(data.clone()?).ok()
    })
}

// ──────────────────────────────────────────────────────────────────────────────────────────
// SessionStorage trait
// ──────────────────────────────────────────────────────────────────────────────────────────

#[async_trait]
pub trait SessionStorage: Send + Sync {
    async fn get_metadata_json(&self) -> Result<Value, SessionError>;
    async fn get_leaf_id(&self) -> Result<Option<String>, SessionError>;
    async fn set_leaf_id(&self, id: Option<String>) -> Result<(), SessionError>;
    async fn create_entry_id(&self) -> Result<String, SessionError>;
    async fn append_entry(&self, entry: SessionTreeEntry) -> Result<(), SessionError>;
    async fn append_entries(&self, entries: Vec<SessionTreeEntry>) -> Result<(), SessionError> {
        for entry in entries {
            self.append_entry(entry).await?;
        }
        Ok(())
    }
    async fn get_entry(&self, id: &str) -> Result<Option<SessionTreeEntry>, SessionError>;
    async fn get_entries(&self) -> Result<Vec<SessionTreeEntry>, SessionError>;
    async fn get_path_to_root(
        &self,
        leaf_id: Option<&str>,
    ) -> Result<Vec<SessionTreeEntry>, SessionError>;
    async fn find_entries(&self, entry_type: &str) -> Result<Vec<SessionTreeEntry>, SessionError>;
    async fn get_label(&self, id: &str) -> Result<Option<String>, SessionError>;
}

// ──────────────────────────────────────────────────────────────────────────────────────────
// Replay
// ──────────────────────────────────────────────────────────────────────────────────────────

pub use crate::agent::context::assembly::build_session_context;
pub use crate::agent::context::collapse::{COMPACT_CONTEXT_CUSTOM_TYPE, CompactContext};

// ──────────────────────────────────────────────────────────────────────────────────────────
// Session facade
// ──────────────────────────────────────────────────────────────────────────────────────────

#[derive(Clone)]
pub struct Session {
    storage: Arc<dyn SessionStorage>,
}

impl Session {
    pub fn new(storage: Arc<dyn SessionStorage>) -> Self {
        Self { storage }
    }

    pub fn storage(&self) -> &Arc<dyn SessionStorage> {
        &self.storage
    }

    fn not_found(msg: impl Into<String>) -> SessionError {
        SessionError {
            code: SessionErrorCode::NotFound,
            message: msg.into(),
        }
    }

    fn now_rfc3339() -> String {
        chrono::Utc::now().to_rfc3339()
    }

    pub async fn leaf_id(&self) -> Result<Option<String>, SessionError> {
        self.storage.get_leaf_id().await
    }

    pub async fn get_entry(&self, id: &str) -> Result<Option<SessionTreeEntry>, SessionError> {
        self.storage.get_entry(id).await
    }

    pub async fn entries(&self) -> Result<Vec<SessionTreeEntry>, SessionError> {
        self.storage.get_entries().await
    }

    pub async fn branch(
        &self,
        from_id: Option<&str>,
    ) -> Result<Vec<SessionTreeEntry>, SessionError> {
        let leaf = match from_id {
            Some(id) => Some(id.to_string()),
            None => self.storage.get_leaf_id().await?,
        };
        self.storage.get_path_to_root(leaf.as_deref()).await
    }

    pub async fn build_context(&self) -> Result<SessionContext, SessionError> {
        let branch = self.branch(None).await?;
        Ok(build_session_context(&branch))
    }

    pub async fn label(&self, id: &str) -> Result<Option<String>, SessionError> {
        self.storage.get_label(id).await
    }

    pub async fn session_name(&self) -> Result<Option<String>, SessionError> {
        let entries = self.storage.find_entries("session_info").await?;
        for entry in entries.into_iter().rev() {
            if let SessionTreeEntry::SessionInfo {
                name: Some(name), ..
            } = entry
            {
                let trimmed = name.trim();
                if !trimmed.is_empty() {
                    return Ok(Some(trimmed.to_string()));
                }
            }
        }
        Ok(None)
    }

    async fn append_typed(&self, entry: SessionTreeEntry) -> Result<String, SessionError> {
        let id = entry.id().to_string();
        self.storage.append_entry(entry).await?;
        Ok(id)
    }

    pub async fn append_message(&self, message: AgentMessage) -> Result<String, SessionError> {
        let id = self.storage.create_entry_id().await?;
        let parent = self.storage.get_leaf_id().await?;
        self.append_typed(SessionTreeEntry::Message {
            id,
            parent_id: parent,
            timestamp: Self::now_rfc3339(),
            message,
        })
        .await
    }

    /// Append a parent-linked message batch through the storage backend's
    /// atomic batch primitive when one is available.
    pub async fn append_messages(
        &self,
        messages: Vec<AgentMessage>,
    ) -> Result<Vec<String>, SessionError> {
        let mut parent = self.storage.get_leaf_id().await?;
        let mut ids = Vec::with_capacity(messages.len());
        let mut entries = Vec::with_capacity(messages.len());
        for message in messages {
            let id = self.storage.create_entry_id().await?;
            entries.push(SessionTreeEntry::Message {
                id: id.clone(),
                parent_id: parent,
                timestamp: Self::now_rfc3339(),
                message,
            });
            parent = Some(id.clone());
            ids.push(id);
        }
        self.storage.append_entries(entries).await?;
        Ok(ids)
    }

    pub async fn append_thinking_level_change(
        &self,
        thinking_level: impl Into<String>,
    ) -> Result<String, SessionError> {
        let id = self.storage.create_entry_id().await?;
        let parent = self.storage.get_leaf_id().await?;
        self.append_typed(SessionTreeEntry::ThinkingLevelChange {
            id,
            parent_id: parent,
            timestamp: Self::now_rfc3339(),
            thinking_level: thinking_level.into(),
        })
        .await
    }

    pub async fn append_model_change(
        &self,
        provider: impl Into<String>,
        model_id: impl Into<String>,
    ) -> Result<String, SessionError> {
        let id = self.storage.create_entry_id().await?;
        let parent = self.storage.get_leaf_id().await?;
        self.append_typed(SessionTreeEntry::ModelChange {
            id,
            parent_id: parent,
            timestamp: Self::now_rfc3339(),
            provider: provider.into(),
            model_id: model_id.into(),
        })
        .await
    }

    pub async fn append_compaction(
        &self,
        summary: impl Into<String>,
        first_kept_entry_id: impl Into<String>,
        tokens_before: u64,
        details: Option<Value>,
        from_hook: bool,
    ) -> Result<String, SessionError> {
        let id = self.storage.create_entry_id().await?;
        let parent = self.storage.get_leaf_id().await?;
        self.append_typed(SessionTreeEntry::Compaction {
            id,
            parent_id: parent,
            timestamp: Self::now_rfc3339(),
            summary: summary.into(),
            first_kept_entry_id: first_kept_entry_id.into(),
            tokens_before,
            details,
            from_hook: if from_hook { Some(true) } else { None },
        })
        .await
    }

    pub async fn append_custom(
        &self,
        custom_type: impl Into<String>,
        data: Option<Value>,
    ) -> Result<String, SessionError> {
        let id = self.storage.create_entry_id().await?;
        let parent = self.storage.get_leaf_id().await?;
        self.append_typed(SessionTreeEntry::Custom {
            id,
            parent_id: parent,
            timestamp: Self::now_rfc3339(),
            custom_type: custom_type.into(),
            data,
        })
        .await
    }

    pub async fn session_id(&self) -> Result<Option<String>, SessionError> {
        let metadata = self.storage.get_metadata_json().await?;
        Ok(metadata
            .get("id")
            .and_then(|value| value.as_str())
            .map(str::to_string))
    }

    /// Append a `session_graph_state` custom entry. The latest entry is the
    /// persisted baseline used by collapse material.
    pub async fn append_session_graph_state(
        &self,
        state: &SessionGraphState,
    ) -> Result<String, SessionError> {
        let mut data = serde_json::to_value(state).map_err(|error| SessionError {
            code: SessionErrorCode::StorageFailure,
            message: format!("serialize session graph state: {error}"),
        })?;
        if let Some(object) = data.as_object_mut() {
            object.insert(
                "updatedAt".to_string(),
                serde_json::Value::String(Self::now_rfc3339()),
            );
        }
        self.append_custom(SESSION_GRAPH_STATE_CUSTOM_TYPE, Some(data))
            .await
    }

    /// Read the newest persisted `session_graph_state` entry.
    pub async fn session_graph_state(&self) -> Result<Option<SessionGraphState>, SessionError> {
        let entries = self.entries().await?;
        Ok(latest_session_graph_state(&entries))
    }

    /// Read the newest non-empty compaction summary (written by `/compact` or
    /// `force_compact`). This is the compact text used by collapse material.
    pub async fn latest_compaction_summary(&self) -> Result<Option<String>, SessionError> {
        let entries = self.entries().await?;
        Ok(entries.iter().rev().find_map(|entry| {
            let SessionTreeEntry::Compaction { summary, .. } = entry else {
                return None;
            };
            (!summary.is_empty()).then(|| summary.clone())
        }))
    }

    /// Read the collapse rolling-summary input for this session.
    ///
    /// Priority: newest non-empty `Compaction` summary first, then newest
    /// non-empty `compact_context.compactText`. The fallback keeps nested
    /// collapses rolling: collapsing a collapse child (which has no new
    /// compaction) still inherits its previous-generation summary.
    pub async fn latest_collapse_summary(&self) -> Result<Option<String>, SessionError> {
        if let Some(summary) = self.latest_compaction_summary().await? {
            return Ok(Some(summary));
        }
        let entries = self.entries().await?;
        Ok(entries.iter().rev().find_map(|entry| {
            crate::agent::context::collapse::compact_context_from_entry(entry)
                .map(|context| context.compact_text)
                .filter(|text| !text.trim().is_empty())
        }))
    }

    /// Read the newest `compact_context` entry written by collapse.
    pub async fn compact_context(&self) -> Result<Option<CompactContext>, SessionError> {
        let entries = self.entries().await?;
        Ok(crate::agent::context::collapse::latest_compact_context(
            &entries,
        ))
    }

    /// Read the `collapseNodeId` metadata written on a collapse child.
    pub async fn collapse_node_id(&self) -> Result<Option<String>, SessionError> {
        let metadata = self.storage.get_metadata_json().await?;
        Ok(metadata
            .get("collapseNodeId")
            .and_then(Value::as_str)
            .map(str::to_string))
    }

    pub async fn append_session_name(
        &self,
        name: impl Into<String>,
    ) -> Result<String, SessionError> {
        let id = self.storage.create_entry_id().await?;
        let parent = self.storage.get_leaf_id().await?;
        let n = name.into().trim().to_string();
        self.append_typed(SessionTreeEntry::SessionInfo {
            id,
            parent_id: parent,
            timestamp: Self::now_rfc3339(),
            name: Some(n),
        })
        .await
    }

    pub async fn move_to(
        &self,
        entry_id: Option<&str>,
        summary: Option<BranchSummaryInput>,
    ) -> Result<Option<String>, SessionError> {
        if let Some(id) = entry_id {
            if self.storage.get_entry(id).await?.is_none() {
                return Err(Self::not_found(format!("Entry {id} not found")));
            }
        }
        self.storage.set_leaf_id(entry_id.map(String::from)).await?;
        let Some(summary) = summary else {
            return Ok(None);
        };
        let id = self.storage.create_entry_id().await?;
        let from_id = entry_id.map(String::from).unwrap_or_else(|| "root".into());
        let entry = SessionTreeEntry::BranchSummary {
            id,
            parent_id: entry_id.map(String::from),
            timestamp: Self::now_rfc3339(),
            from_id,
            summary: summary.summary,
            details: summary.details,
            from_hook: if summary.from_hook { Some(true) } else { None },
        };
        Ok(Some(self.append_typed(entry).await?))
    }
}

#[derive(Clone, Debug, Default)]
pub struct BranchSummaryInput {
    pub summary: String,
    pub details: Option<Value>,
    pub from_hook: bool,
}

#[cfg(test)]
tests_bridge_macro::tests_bridge!("agent/session/session");

#[cfg(test)]
mod session_linecov_tests {
    tests_bridge_macro::tests_bridge!("agent/session/session/linecov");
}