ultron-session-file 1.0.2

Ultron's Pi session JSONL format: entries, migrations, tree, model context, discovery
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//! `SessionManager`: an append-only session tree persisted as JSONL.

use std::fmt;
use std::fs::{self, OpenOptions};
use std::io::{self, Write};
use std::path::Path;

use indexmap::IndexMap;
use serde_json::{Value, json};

use crate::context::{
    self, Leaf, SessionContext, SessionProjection, entry_type, id_key, is_message_with_role, object, str_key,
};
use crate::env::Env;
use crate::file::{
    self, CURRENT_SESSION_VERSION, HeaderScanError, generate_id, header_cwd, load_entries_from_file,
    migrate_session_entries, read_session_header,
};
use crate::js;
use crate::paths::{normalize_path, parent, resolve_path};
use jsrt::path::join;

/// Errors from session operations. `Invalid` messages are the TypeScript error messages.
#[derive(Debug)]
pub enum Error {
    Invalid(String),
    Io(io::Error),
}

impl Error {
    /// The Node error code an I/O failure maps to (`EEXIST`, `ENOENT`, ...).
    pub fn code(&self) -> Option<&'static str> {
        let Error::Io(e) = self else { return None };
        Some(match e.kind() {
            io::ErrorKind::AlreadyExists => "EEXIST",
            io::ErrorKind::NotFound => "ENOENT",
            io::ErrorKind::PermissionDenied => "EACCES",
            io::ErrorKind::IsADirectory => "EISDIR",
            io::ErrorKind::NotADirectory => "ENOTDIR",
            _ => "EIO",
        })
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Invalid(message) => f.write_str(message),
            Error::Io(e) => write!(f, "{}: {e}", self.code().unwrap_or("EIO")),
        }
    }
}

impl std::error::Error for Error {}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::Io(e)
    }
}

fn invalid<T>(message: impl Into<String>) -> Result<T, Error> {
    Err(Error::Invalid(message.into()))
}

#[derive(Clone, Debug, Default)]
pub struct NewSessionOptions {
    pub id: Option<String>,
    pub parent_session: Option<String>,
}

/// `assertValidSessionId`: alphanumerics with interior `.`, `_`, `-`.
fn assert_valid_session_id(id: &str) -> Result<(), Error> {
    let bytes = id.as_bytes();
    let edge_ok = |b: Option<&u8>| b.is_some_and(u8::is_ascii_alphanumeric);
    let valid = edge_ok(bytes.first())
        && edge_ok(bytes.last())
        && bytes
            .iter()
            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'));
    if valid {
        Ok(())
    } else {
        invalid(
            "Session id must be non-empty, contain only alphanumeric characters, '-', '_', and '.', and start and end with an alphanumeric character",
        )
    }
}

/// A node of [`SessionTree`]; `children` index into [`SessionTree::nodes`].
#[derive(Clone, Debug, PartialEq)]
pub struct TreeNode<'a> {
    pub entry: &'a Value,
    pub children: Vec<usize>,
    pub label: Option<&'a Value>,
    pub label_timestamp: Option<&'a Value>,
}

/// `getTree()` as an arena (deep sessions stay iterative): roots are entries with a `null`,
/// self-referencing or missing parent; children are ordered by timestamp, oldest first.
#[derive(Clone, Debug, PartialEq)]
pub struct SessionTree<'a> {
    pub nodes: Vec<TreeNode<'a>>,
    pub roots: Vec<usize>,
}

#[derive(Clone, Debug)]
struct LabelState {
    label: Value,
    timestamp: Value,
}

/// `SessionManager`: entries form a tree through `id`/`parentId`; the leaf is the current position.
/// Appends add a child of the leaf; branching moves the leaf. Persisted sessions are written only once
/// the first assistant message exists, then appended line by line.
pub struct SessionManager {
    env: Box<dyn Env>,
    session_id: String,
    session_file: Option<String>,
    session_dir: String,
    cwd: String,
    persist: bool,
    flushed: bool,
    file_entries: Vec<Value>,
    by_id: IndexMap<String, usize>,
    labels: IndexMap<String, LabelState>,
    leaf_id: Option<Value>,
}

impl fmt::Debug for SessionManager {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SessionManager")
            .field("session_id", &self.session_id)
            .field("session_file", &self.session_file)
            .field("leaf_id", &self.leaf_id)
            .finish_non_exhaustive()
    }
}

fn append_line(path: &str, entries: &[&Value]) -> io::Result<()> {
    let mut text = String::new();
    for entry in entries {
        text.push_str(&jsrt::json::stringify(entry));
        text.push('\n');
    }
    OpenOptions::new()
        .append(true)
        .create(true)
        .open(path)?
        .write_all(text.as_bytes())
}

fn write_all(path: &str, entries: &[Value], create_new: bool) -> io::Result<()> {
    let mut text = String::new();
    for entry in entries {
        text.push_str(&jsrt::json::stringify(entry));
        text.push('\n');
    }
    let mut options = OpenOptions::new();
    options.write(true);
    if create_new {
        options.create_new(true);
    } else {
        options.create(true).truncate(true);
    }
    options.open(path)?.write_all(text.as_bytes())
}

fn file_timestamp(iso: &str) -> String {
    iso.replace([':', '.'], "-")
}

impl SessionManager {
    fn construct(
        env: Box<dyn Env>,
        cwd: &str,
        session_dir: &str,
        session_file: Option<&str>,
        persist: bool,
        options: Option<NewSessionOptions>,
        preloaded: Option<Vec<Value>>,
    ) -> Result<Self, Error> {
        let (session_dir, cwd) = (normalize_path(&*env, session_dir), resolve_path(&*env, cwd));
        let mut manager = SessionManager {
            env,
            session_id: String::new(),
            session_file: None,
            session_dir,
            cwd,
            persist,
            flushed: false,
            file_entries: Vec::new(),
            by_id: IndexMap::new(),
            labels: IndexMap::new(),
            leaf_id: None,
        };
        if persist && !manager.session_dir.is_empty() && !Path::new(&manager.session_dir).exists() {
            fs::create_dir_all(&manager.session_dir)?;
        }
        match (session_file, preloaded) {
            (Some(file), preloaded) => manager.set_session_file_with(file, preloaded)?,
            (None, Some(entries)) if !entries.is_empty() => manager.load_entries(entries, options)?,
            _ => {
                manager.new_session(options)?;
            }
        }
        Ok(manager)
    }

    /// `create`: a new persisted session in `session_dir` (default: the cwd's dir under the agent dir).
    pub fn create(
        env: Box<dyn Env>,
        cwd: &str,
        session_dir: Option<&str>,
        options: Option<NewSessionOptions>,
    ) -> Result<Self, Error> {
        let dir = match session_dir.filter(|d| !d.is_empty()) {
            Some(dir) => normalize_path(&*env, dir),
            None => file::default_session_dir(&*env, cwd, &env.agent_dir())?,
        };
        Self::construct(env, cwd, &dir, None, true, options, None)
    }

    /// `open`: a specific session file. The cwd comes from `cwd_override`, else the header, else
    /// the process cwd; `session_dir` defaults to the file's directory.
    pub fn open(
        env: Box<dyn Env>,
        path: &str,
        session_dir: Option<&str>,
        cwd_override: Option<&str>,
    ) -> Result<Self, Error> {
        let resolved = resolve_path(&*env, path);
        let mut header = None;
        let mut preloaded = None;
        if cwd_override.is_none() && Path::new(&resolved).exists() {
            match read_session_header(&resolved) {
                Ok(found) => header = found,
                Err(HeaderScanError::Io(e)) => return Err(e.into()),
                // The bounded scan only speeds up discovery; a full load stays authoritative.
                Err(HeaderScanError::ScanLimit) => {
                    let entries = load_entries_from_file(&*env, &resolved)?;
                    header = entries.first().filter(|e| entry_type(e) == Some("session")).cloned();
                    preloaded = Some(entries);
                }
            }
        }
        let process_cwd = env.cwd();
        let cwd = cwd_override
            .or_else(|| header.as_ref().and_then(header_cwd))
            .unwrap_or(&process_cwd)
            .to_string();
        let dir = session_dir
            .filter(|d| !d.is_empty())
            .map_or_else(|| parent(&*env, &resolved), |dir| normalize_path(&*env, dir));
        Self::construct(env, &cwd, &dir, Some(&resolved), true, None, preloaded)
    }

    /// `inMemory`: a session that is never written, optionally adopting stored entries.
    pub fn in_memory(
        env: Box<dyn Env>,
        cwd: &str,
        options: Option<NewSessionOptions>,
        entries: Option<Vec<Value>>,
    ) -> Result<Self, Error> {
        Self::construct(env, cwd, "", None, false, options, entries)
    }

    /// `forkFrom`: copies another session's entries into a new session for `target_cwd`.
    pub fn fork_from(
        mut env: Box<dyn Env>,
        source_path: &str,
        target_cwd: &str,
        session_dir: Option<&str>,
        options: Option<NewSessionOptions>,
    ) -> Result<Self, Error> {
        let source = resolve_path(&*env, source_path);
        let target = resolve_path(&*env, target_cwd);
        let entries = load_entries_from_file(&*env, &source)?;
        if entries.is_empty() {
            return invalid(format!(
                "Cannot fork: source session file is empty or invalid: {source}"
            ));
        }
        if !entries.iter().any(|e| entry_type(e) == Some("session")) {
            return invalid(format!("Cannot fork: source session has no header: {source}"));
        }
        let dir = match session_dir.filter(|d| !d.is_empty()) {
            Some(dir) => normalize_path(&*env, dir),
            None => file::default_session_dir(&*env, &target, &env.agent_dir())?,
        };
        fs::create_dir_all(&dir)?;
        let options = options.unwrap_or_default();
        if let Some(id) = &options.id {
            assert_valid_session_id(id)?;
        }
        let id = match options.id {
            Some(id) => id,
            None => env.session_id(),
        };
        let timestamp = jsrt::date::iso_string(env.now_ms());
        let path = join(&[&dir, &format!("{}_{id}.jsonl", file_timestamp(&timestamp))]);
        let header = json!({
            "type": "session", "version": CURRENT_SESSION_VERSION, "id": id,
            "timestamp": timestamp, "cwd": target, "parentSession": source,
        });
        write_all(&path, &[header], true)?;
        let body: Vec<&Value> = entries.iter().filter(|e| entry_type(e) != Some("session")).collect();
        append_line(&path, &body)?;
        Self::construct(env, &target, &dir, Some(&path), true, None, None)
    }

    fn set_session_file_with(&mut self, session_file: &str, preloaded: Option<Vec<Value>>) -> Result<(), Error> {
        let path = resolve_path(&*self.env, session_file);
        self.session_file = Some(path.clone());
        if !Path::new(&path).exists() {
            self.new_session(None)?;
            self.session_file = Some(path);
            return Ok(());
        }
        let entries = match preloaded {
            Some(entries) => entries,
            None => load_entries_from_file(&*self.env, &path)?,
        };
        if entries.is_empty() {
            // An empty file gets a header; any other non-session content is left untouched.
            if fs::metadata(&path)?.len() > 0 {
                return invalid(format!("Session file is not a valid ultron session: {path}"));
            }
            self.new_session(None)?;
            self.session_file = Some(path);
            self.rewrite_file()?;
            self.flushed = true;
            return Ok(());
        }
        self.load_entries(entries, None)?;
        self.flushed = true;
        Ok(())
    }

    /// `newSession`: a fresh header and no entries. Returns the new session file path (persisted only).
    pub fn new_session(&mut self, options: Option<NewSessionOptions>) -> Result<Option<String>, Error> {
        let options = options.unwrap_or_default();
        if let Some(id) = &options.id {
            assert_valid_session_id(id)?;
        }
        self.session_id = match options.id {
            Some(id) => id,
            None => self.env.session_id(),
        };
        let timestamp = jsrt::date::iso_string(self.env.now_ms());
        let header = object(vec![
            ("type", Some("session".into())),
            ("version", Some(CURRENT_SESSION_VERSION.into())),
            ("id", Some(self.session_id.clone().into())),
            ("timestamp", Some(timestamp.clone().into())),
            ("cwd", Some(self.cwd.clone().into())),
            ("parentSession", options.parent_session.map(Value::from)),
        ]);
        self.file_entries = vec![header];
        self.by_id.clear();
        self.labels.clear();
        self.leaf_id = None;
        self.flushed = false;
        if self.persist {
            let name = format!("{}_{}.jsonl", file_timestamp(&timestamp), self.session_id);
            self.session_file = Some(join(&[&self.session_dir, &name]));
        }
        Ok(self.session_file.clone())
    }

    fn load_entries(&mut self, mut entries: Vec<Value>, options: Option<NewSessionOptions>) -> Result<(), Error> {
        match entries.iter().find(|e| entry_type(e) == Some("session")) {
            Some(header) => {
                self.session_id = match header.get("id") {
                    Some(Value::String(id)) => id.clone(),
                    other => js::join_string(other),
                };
                let migrated = migrate_session_entries(&mut entries, self.env.as_mut());
                self.file_entries = entries;
                if migrated {
                    self.rewrite_file()?;
                }
            }
            None => {
                self.new_session(options)?;
                self.file_entries.extend(entries);
            }
        }
        self.build_index();
        Ok(())
    }

    fn build_index(&mut self) {
        self.by_id.clear();
        self.labels.clear();
        self.leaf_id = None;
        for (i, entry) in self.file_entries.iter().enumerate() {
            if entry_type(entry) == Some("session") {
                continue;
            }
            self.by_id.insert(id_key(entry.get("id")), i);
            self.leaf_id = entry.get("id").cloned();
            if entry_type(entry) == Some("label") {
                Self::apply_label(&mut self.labels, entry);
            }
        }
    }

    fn apply_label(labels: &mut IndexMap<String, LabelState>, entry: &Value) {
        let key = id_key(entry.get("targetId"));
        if jsrt::value::truthy(entry.get("label")) {
            labels.insert(
                key,
                LabelState {
                    label: entry["label"].clone(),
                    timestamp: entry.get("timestamp").cloned().unwrap_or(Value::Null),
                },
            );
        } else {
            labels.shift_remove(&key);
        }
    }

    fn rewrite_file(&self) -> io::Result<()> {
        match (&self.session_file, self.persist) {
            (Some(path), true) => write_all(path, &self.file_entries, false),
            _ => Ok(()),
        }
    }

    fn has_assistant(&self) -> bool {
        self.file_entries.iter().any(|e| is_message_with_role(e, "assistant"))
    }

    /// `_persist`: nothing reaches disk until an assistant message exists; then the whole session is
    /// written once (failing if the file already exists) and later entries are appended.
    fn persist_entry(&mut self, index: usize) -> io::Result<()> {
        let Some(path) = self.session_file.clone().filter(|_| self.persist) else {
            return Ok(());
        };
        if !self.has_assistant() {
            if self.flushed {
                append_line(&path, &[&self.file_entries[index]])?;
            }
            return Ok(());
        }
        if self.flushed {
            append_line(&path, &[&self.file_entries[index]])
        } else {
            write_all(&path, &self.file_entries, true)?;
            self.flushed = true;
            Ok(())
        }
    }

    fn append_entry(&mut self, entry: Value) -> Result<(), Error> {
        let index = self.file_entries.len();
        self.by_id.insert(id_key(entry.get("id")), index);
        self.leaf_id = entry.get("id").cloned();
        self.file_entries.push(entry);
        Ok(self.persist_entry(index)?)
    }

    fn new_id(&mut self) -> String {
        let by_id = &self.by_id;
        generate_id(self.env.as_mut(), |id| by_id.contains_key(&str_key(id)))
    }

    fn now_iso(&mut self) -> String {
        jsrt::date::iso_string(self.env.now_ms())
    }

    /// The common `type, id, parentId, timestamp` head followed by `fields`.
    fn entry_with(&mut self, kind: &str, fields: Vec<(&str, Option<Value>)>) -> Value {
        let mut pairs = vec![
            ("type", Some(Value::from(kind))),
            ("id", Some(self.new_id().into())),
            ("parentId", Some(self.leaf_id.clone().unwrap_or(Value::Null))),
            ("timestamp", Some(self.now_iso().into())),
        ];
        pairs.extend(fields);
        object(pairs)
    }

    fn append(&mut self, entry: Value) -> Result<String, Error> {
        let id = entry["id"].as_str().unwrap_or_default().to_string();
        self.append_entry(entry)?;
        Ok(id)
    }

    /// `appendMessage`: a message entry as child of the leaf. Returns its id.
    pub fn append_message(&mut self, message: Value) -> Result<String, Error> {
        let entry = self.entry_with("message", vec![("message", Some(message))]);
        self.append(entry)
    }

    /// `getSessionName`: the latest `session_info` name; empty clears it.
    pub fn session_name(&self) -> Option<String> {
        let entry = self
            .entries()
            .into_iter()
            .rev()
            .find(|e| entry_type(e) == Some("session_info"))?;
        entry
            .get("name")
            .and_then(Value::as_str)
            .map(jsrt::string::trim)
            .filter(|n| !n.is_empty())
            .map(str::to_string)
    }

    /// `branch`: moves the leaf to an earlier entry; the next append starts a new branch there.
    pub fn branch(&mut self, branch_from_id: &str) -> Result<(), Error> {
        let Some(entry) = self.entry(branch_from_id) else {
            return invalid(format!("Entry {branch_from_id} not found"));
        };
        self.leaf_id = entry.get("id").cloned();
        Ok(())
    }

    /// `resetLeaf`: the next append creates a new root.
    pub fn reset_leaf(&mut self) {
        self.leaf_id = None;
    }

    // ---- reads ----

    pub fn cwd(&self) -> &str {
        &self.cwd
    }

    pub fn session_dir(&self) -> &str {
        &self.session_dir
    }

    pub fn session_id(&self) -> &str {
        &self.session_id
    }

    pub fn session_file(&self) -> Option<&str> {
        self.session_file.as_deref()
    }

    /// The leaf id; `None` before the first entry (or after `reset_leaf`).
    pub fn leaf_id(&self) -> Option<&Value> {
        self.leaf_id.as_ref()
    }

    fn leaf_entry(&self) -> Option<&Value> {
        self.leaf_id
            .as_ref()
            .filter(|id| jsrt::value::truthy(Some(id)))
            .and_then(|id| self.lookup(&id_key(Some(id))))
    }

    fn lookup(&self, key: &str) -> Option<&Value> {
        self.by_id.get(key).map(|i| &self.file_entries[*i])
    }

    pub fn entry(&self, id: &str) -> Option<&Value> {
        self.lookup(&str_key(id))
    }

    /// `getChildren`: direct children in index order.
    pub fn children(&self, parent_id: &str) -> Vec<&Value> {
        self.by_id
            .values()
            .map(|i| &self.file_entries[*i])
            .filter(|e| e.get("parentId").and_then(Value::as_str) == Some(parent_id))
            .collect()
    }

    pub fn label(&self, id: &str) -> Option<&Value> {
        self.labels.get(&str_key(id)).map(|l| &l.label)
    }

    /// `getBranch`: root-first path to `from_id` (default: the leaf).
    pub fn branch_entries(&self, from_id: Option<&str>) -> Vec<&Value> {
        let start = match from_id {
            Some("") => None,
            Some(id) => self.entry(id),
            None => self.leaf_entry(),
        };
        context::walk_to_root(start, self.by_id.len(), |key| self.lookup(key))
    }

    fn leaf(&self) -> Leaf<'_> {
        self.leaf_id.as_ref().map_or(Leaf::Root, Leaf::Id)
    }

    /// `buildContextEntries` from the leaf.
    pub fn build_context_entries(&self) -> Vec<&Value> {
        context::context_entries(&self.entries(), self.leaf())
    }

    /// `buildSessionProjection` from the leaf.
    pub fn build_session_projection(&self) -> SessionProjection<'_> {
        context::session_projection(&self.entries(), self.leaf())
    }

    /// `buildSessionContext` from the leaf.
    pub fn build_session_context(&self) -> SessionContext {
        self.build_session_projection().into()
    }

    pub fn header(&self) -> Option<&Value> {
        self.file_entries.iter().find(|e| entry_type(e) == Some("session"))
    }

    /// `getEntries`: every entry except headers.
    pub fn entries(&self) -> Vec<&Value> {
        self.file_entries
            .iter()
            .filter(|e| entry_type(e) != Some("session"))
            .collect()
    }

    pub fn tree(&self) -> SessionTree<'_> {
        let entries = self.entries();
        let mut node_of: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
        let mut nodes: Vec<TreeNode> = entries
            .iter()
            .enumerate()
            .map(|(i, entry)| {
                let key = id_key(entry.get("id"));
                node_of.insert(key.clone(), i);
                let label = self.labels.get(&key);
                TreeNode {
                    entry,
                    children: Vec::new(),
                    label: label.map(|l| &l.label),
                    label_timestamp: label.map(|l| &l.timestamp),
                }
            })
            .collect();
        let mut roots = Vec::new();
        for entry in &entries {
            let node = node_of[&id_key(entry.get("id"))];
            let parent = entry.get("parentId");
            if parent == Some(&Value::Null) || parent == entry.get("id") {
                roots.push(node);
            } else {
                match node_of.get(&id_key(parent)) {
                    Some(&p) => nodes[p].children.push(node),
                    None => roots.push(node),
                }
            }
        }
        let times: Vec<f64> = nodes
            .iter()
            .map(|n| js::date_value(n.entry.get("timestamp")).map_or(f64::NAN, |t| t as f64))
            .collect();
        for node in &mut nodes {
            js::sort_by(&mut node.children, |a, b| times[*a] - times[*b]);
        }
        SessionTree { nodes, roots }
    }
}