wire/answer/sessions.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4// Copyright (c) 2026 2youg1 and the sprawling contributors
5
6//! The stretches of one room, newest first: where each began, how, its
7//! runs and its last line (`crates/wire/spec/Answer/Sessions.lean` §8-71).
8
9use kernel::{Address, Origin, Seq, TimeMs};
10use serde::{Deserialize, Serialize};
11
12use crate::command::Carry;
13
14/// How many stretches one answer lists. A bound on the size of an answer
15/// on the wire, not a machine reading: the page that reads it shows a
16/// room's recent stretches, and `earlier` says how many more there are.
17pub const SESSIONS_MAX: usize = 64;
18
19/// One room's stretches, newest first, and how many older ones the answer
20/// leaves out. `room` echoes the question, which the wire carries no id
21/// for.
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
24pub struct SessionsAnswer {
25 pub room: Address,
26 pub sessions: Vec<SessionLine>,
27 pub earlier: u64,
28}
29
30/// One stretch of a room, each field read off a line the Ledger files
31/// under the room's address.
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
34pub struct SessionLine {
35 /// The stretch's first line: its `session_opened`, or the room's first
36 /// `run_started` when dispatching opened the room.
37 pub began: Seq,
38 pub start: SessionStart,
39 /// The runs started in this stretch.
40 pub runs: u64,
41 /// The last line of this stretch filed under the room's address, and
42 /// its time: the room's slice ends there (`crates/storage/Spec.lean` §8-24).
43 pub last: Seq,
44 pub at: TimeMs,
45}
46
47/// How a stretch began.
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
49#[serde(rename_all = "snake_case")]
50#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
51pub enum SessionStart {
52 /// Dispatching into the room opened it; no `session_opened` was written.
53 Dispatched,
54 /// `/new`: what crossed from the stretch before - `Handoff` when the
55 /// previous stretch's handoff travelled - and the run line it branched
56 /// from, when it is a branch.
57 Opened { carry: Carry, from: Option<Origin> },
58}