Skip to main content

supercode_interchange/session/
mod.rs

1//! Natively load — and continue — real Claude Code and Codex sessions.
2//!
3//! Both tools persist their conversations as JSONL on disk:
4//!
5//! - **Claude Code**: `~/.claude/projects/<encoded-cwd>/<session-id>.jsonl`,
6//!   one line per event in the Anthropic message format, linked by
7//!   `uuid`/`parentUuid`.
8//! - **Codex**: `~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`, where each line
9//!   is a `{timestamp, type, payload}` envelope and the `response_item` lines
10//!   form the canonical conversation.
11//!
12//! [`Session::load`] auto-detects the format and normalizes either one into a
13//! provider-neutral [`Vec<ChatMessage>`] that can be handed straight back to a
14//! model (via OpenRouter or any OpenAI-compatible endpoint) to continue.
15//!
16//! Provider-internal artifacts that don't replay across vendors — Anthropic
17//! `thinking` blocks, Codex `reasoning` items — are dropped during
18//! normalization.
19//!
20//! # Where this sits in supercode's priorities
21//!
22//! This module is the home of **feature 1 (translate between session formats)**
23//! and half of **feature 2 (emulate-to-continue)** — the load/emit surface for
24//! each harness ([`SessionFormat`], `from_*_str` loaders, `to_*_jsonl`
25//! emitters). See [`AGENTS.md`](../../../AGENTS.md) for the three ranked
26//! feature-priorities and the glue-tool positioning; the top priority is
27//! **feature 3 (continue losslessly *with massive token reduction*)**, which
28//! this fidelity work exists to make trustworthy. `opencode` + `pi` loaders
29//! are built against the frozen `docs/interop/opencode-pi-spec.md` contract
30//! — OpenCode additionally reads its native SQLite store (`opencode*.db`,
31//! PARITY-3/PARITY-16) via `rusqlite`, reconstructing the same envelope form
32//! [`Session::from_opencode_str`] already parses for the JSON-tree surfaces.
33
34use serde::{Deserialize, Serialize};
35use std::collections::{BTreeMap, HashMap, HashSet};
36use std::io::{BufRead, BufReader, Read, Seek, SeekFrom};
37use std::path::{Path, PathBuf};
38
39use rusqlite::Connection;
40use serde_json::Value;
41
42use crate::{
43    ChatMessage, Fidelity, FunctionCall, InterchangeError as Error, Result, Role, ToolCall,
44};
45
46mod claude_code;
47pub(crate) use claude_code::ClaudeAppendState;
48#[doc(hidden)]
49pub use claude_code::ClaudeReadIndex;
50mod codex;
51mod detect;
52mod gemini;
53mod goose;
54mod grok;
55mod helpers;
56mod hermes;
57mod native;
58mod openclaw;
59mod opencode;
60mod pi;
61
62// The per-harness files below are an internal file layout only: every item
63// keeps its original `crate::session::…` path through these re-exports, whose
64// visibility matches the most-visible item each module holds.
65pub(crate) use claude_code::*;
66use codex::*;
67pub use detect::*;
68use gemini::*;
69use grok::*;
70pub use helpers::*;
71pub use hermes::*;
72use native::*;
73pub use openclaw::*;
74pub use opencode::*;
75pub use pi::*;
76
77/// Which tool produced a session log.
78///
79/// This is **read-provenance**: a fact recovered when a log is loaded (stored
80/// in [`SessionMeta::source`], filled in by auto-detection in
81/// `detect_source`), describing which tool originally wrote the file on
82/// disk. It answers "where did this session come from?" — e.g. for
83/// `inspect`/`convert` display in the CLI.
84///
85/// It is deliberately distinct from [`SessionFormat`], even though the two
86/// enums' variant lists currently coincide: [`SessionFormat`] selects a
87/// serialization codec (what to parse/export *as*), while `SessionSource`
88/// records history (what wrote the file). The pair is intentionally kept
89/// separate rather than merged — a session loaded from one tool's log can
90/// still be exported in the other tool's format, and the two concepts could
91/// diverge further (e.g. a format that is readable but not attributable, or
92/// multiple versioned formats sharing one source).
93#[derive(Debug, Clone, Copy, PartialEq, Eq)]
94pub enum SessionSource {
95    /// A `~/.claude/projects/.../<id>.jsonl` transcript.
96    ClaudeCode,
97    /// A `~/.codex/sessions/.../rollout-*.jsonl` file.
98    Codex,
99    /// An OpenCode session — multi-file JSON tree(s) or SQLite `opencode*.db`
100    /// (`docs/interop/opencode-pi-spec.md` §1.2). Detection and loading are
101    /// wave B; this variant exists now so `SessionSource`/`SessionFormat` stay
102    /// 1:1 per the frozen interop spec (§0).
103    OpenCode,
104    /// A `~/.pi/agent/sessions/--<enc-cwd>--/<iso>_<sessionId>.jsonl`
105    /// transcript (`docs/interop/opencode-pi-spec.md` §1.1) — line-oriented
106    /// JSONL like Claude Code/Codex, so it shares their byte-lossless native
107    /// round-trip property.
108    Pi,
109    /// A Grok session transcript stored as
110    /// `~/.grok/sessions/<percent-encoded-cwd>/<session-id>/chat_history.jsonl`.
111    Grok,
112    /// A Gemini CLI transcript stored under
113    /// `~/.gemini/tmp/<project>/chats/session-*.jsonl`.
114    Gemini,
115    /// A Goose session exported through `_goose/unstable/session/export`, or
116    /// reconstructed from Goose's `sessions/sessions.db` native store.
117    Goose,
118    /// An OpenClaw agent session (`~/.openclaw/agents/<agentId>/sessions/
119    /// <uuid>.jsonl`, openclaw >= 2026.7): pi session-format v3 with
120    /// openclaw dialect divergences — `type:"leaf"` navigation-control
121    /// entries that REDIRECT the active leaf (pi's last-entry anchor rule is
122    /// wrong for them), `appendMode:"side"` entries that never anchor, and
123    /// vendor-namespaced `__openclaw` message metadata. READ-ONLY provenance
124    /// (UNI-16): there is deliberately no `SessionFormat::OpenClaw` — the
125    /// write tier is a permanently skipped direct-DB/store path; loaded
126    /// sessions translate OUT through the other formats.
127    OpenClaw,
128    /// A Hermes Agent session read from its single SQLite store
129    /// (`~/.hermes/state.db`, `SCHEMA_VERSION = 22` at the 0.19.0 pin).
130    /// READ-ONLY provenance (UNI-15): no `SessionFormat::Hermes` exists —
131    /// writing into a live, shared, WAL, single-writer store stays gated by
132    /// UNI-22 (not fired; the schema churned 19->22 in one release) — loaded
133    /// sessions translate OUT through the other formats.
134    Hermes,
135    /// P5-3 safety-hardening fix (Fable-5 review, LOW "translation-fidelity
136    /// cosmetic"): a session that was never imported from ANY foreign
137    /// tool's log at all — authored directly by supercode's own agent loop,
138    /// with no foreign-tool prefix (`Session.raw` starts empty). Currently
139    /// only `crate::agent::Agent`'s `persist_subagent_transcript` (P5-3,
140    /// natively-spawned `spawn_subagent` children) uses this — before this
141    /// variant existed, that call site built its blank `Session` via
142    /// `Session::from_claude_code_str("")` purely as an "empty parser to
143    /// get a blank skeleton" trick, which left `meta.source ==
144    /// SessionSource::ClaudeCode` even though nothing Claude-Code-shaped
145    /// was ever involved, mislabeling a native supercode spawn as an
146    /// imported CC session on disk (and in any `inspect`/`convert` reading
147    /// it back). Never produced by auto-detection (`detect_source`) or any
148    /// `from_<tool>_str` loader — only by code that explicitly constructs
149    /// a `SessionMeta` with this source, so no existing imported-session
150    /// path can ever observe this variant appearing where it didn't before.
151    Native,
152}
153
154/// An on-disk session format supercode can both read and write.
155///
156/// Like an image editor that opens and exports several file formats, supercode
157/// keeps one canonical in-memory model ([`Session`]) and converts to/from each
158/// supported format on the edges.
159///
160/// This is a **write-target** / codec selector: a caller's request, passed to
161/// [`Session::load_str`], [`Session::to_jsonl`], and [`Session::save`],
162/// choosing which on-disk dialect to parse or emit. It answers "what format
163/// should I read/write?" — as opposed to [`SessionSource`], which records the
164/// provenance fact of what actually produced a loaded file. The two enums are
165/// intentionally kept separate (provenance fact vs. serialization choice) and
166/// should not be unified, even though their variants currently match
167/// one-to-one.
168#[derive(Debug, Clone, Copy, PartialEq, Eq)]
169pub enum SessionFormat {
170    /// Claude Code transcript JSONL.
171    ClaudeCode,
172    /// Codex rollout JSONL.
173    Codex,
174    /// OpenCode export-document / envelope JSONL (wave B; see
175    /// [`SessionSource::OpenCode`]).
176    OpenCode,
177    /// Pi session JSONL (see [`SessionSource::Pi`]).
178    Pi,
179    /// Grok `chat_history.jsonl` transcript.
180    Grok,
181    /// Gemini CLI session JSONL.
182    Gemini,
183    /// Goose native session-export JSON.
184    Goose,
185}
186
187impl SessionFormat {
188    /// The [`SessionSource`] a file of this format reports.
189    ///
190    /// This is the deliberate one-way bridge between the two concepts: a file
191    /// saved in this format will, when reloaded, report this provenance (see
192    /// `crates/harness/tests/session_saving.rs`), making the relationship
193    /// discoverable from the method itself.
194    pub fn source(self) -> SessionSource {
195        match self {
196            SessionFormat::ClaudeCode => SessionSource::ClaudeCode,
197            SessionFormat::Codex => SessionSource::Codex,
198            SessionFormat::OpenCode => SessionSource::OpenCode,
199            SessionFormat::Pi => SessionSource::Pi,
200            SessionFormat::Grok => SessionSource::Grok,
201            SessionFormat::Gemini => SessionSource::Gemini,
202            SessionFormat::Goose => SessionSource::Goose,
203        }
204    }
205}
206
207/// Metadata recovered from a session log.
208pub use crate::ontology::surface::{
209    CrossSurface, Recurrence, SurfaceKey, Trigger, WorkspaceKind, WorkspaceRef,
210};
211
212/// ORCH-6: the ORCH-3 conversation nouns as one additive wire block, carried
213/// by `harness.v1.sessions.discover` / `sessions.load` rows and by
214/// [`crate::catalog::SessionDescriptor`]. Every field is optional so an older
215/// client sees exactly the shape it already knows.
216#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
217pub struct OrchestrationNouns {
218    /// Why the session exists.
219    #[serde(default, skip_serializing_if = "Option::is_none")]
220    pub trigger: Option<Trigger>,
221    /// Where the conversation is reached.
222    #[serde(default, skip_serializing_if = "Option::is_none")]
223    pub surface: Option<SurfaceKey>,
224    /// Routed config home (Hermes profile / OpenClaw agent).
225    #[serde(default, skip_serializing_if = "Option::is_none")]
226    pub profile: Option<String>,
227    /// The job a recurring session belongs to.
228    #[serde(default, skip_serializing_if = "Option::is_none")]
229    pub recurrence: Option<Recurrence>,
230    /// Moved-to-another-surface state.
231    #[serde(default, skip_serializing_if = "Option::is_none")]
232    pub cross_surface: Option<CrossSurface>,
233    /// Typed workspace (the D2 precedence result).
234    #[serde(default, skip_serializing_if = "Option::is_none")]
235    pub workspace: Option<WorkspaceRef>,
236}
237
238impl OrchestrationNouns {
239    /// Read the nouns off a loaded session's metadata. `trigger` and
240    /// `workspace` always resolve — through [`SessionMeta::trigger_or_default`]
241    /// and [`SessionMeta::workspace`], never through a second derivation.
242    pub fn from_meta(meta: &SessionMeta) -> Self {
243        Self {
244            trigger: Some(meta.trigger_or_default()),
245            surface: meta.surface.clone(),
246            profile: meta.profile.clone(),
247            recurrence: meta.recurrence.clone(),
248            cross_surface: meta.cross_surface.clone(),
249            workspace: Some(meta.workspace_ref()),
250        }
251    }
252}
253
254#[derive(Debug, Clone)]
255#[non_exhaustive]
256pub struct SessionMeta {
257    /// The tool that wrote the log.
258    pub source: SessionSource,
259    /// The session/rollout id.
260    pub session_id: Option<String>,
261    /// Native recorded session end, in RFC 3339 form. Absence is unknown;
262    /// it must not be replaced by a transcript quiet-time estimate.
263    pub ended_at: Option<String>,
264    /// Source-native reason accompanying the recorded session end.
265    pub end_reason: Option<String>,
266    /// The model the session was running.
267    pub model: Option<String>,
268    /// The working directory the session ran in.
269    pub cwd: Option<PathBuf>,
270    /// The system / base-instructions prompt, when the log records it.
271    pub system_prompt: Option<String>,
272    /// Verbatim source-format header records (the Codex `session_meta` /
273    /// `turn_context` lines), preserved so re-export can replay the exact header
274    /// the original tool expects rather than guessing its required fields.
275    pub codex_headers: Vec<Value>,
276    /// Exact source lines for Codex execution/provenance records that affect
277    /// continuation semantics but must not be replayed as active events after
278    /// a foreign-format hop. Each entry records its original physical-line
279    /// index, discriminant, and verbatim JSONL text. Foreign writers carry the
280    /// list in a namespaced extension; a later Codex export restores headers
281    /// from it while keeping compaction/rollback/review records non-operative,
282    /// avoiding a second rollback or compaction of the already-normalized view.
283    pub codex_provenance: Vec<Value>,
284    /// PARITY-23: generalized source-native residue records for NON-codex
285    /// sources — `{record_index, kind, raw}` entries captured at load time
286    /// (or restored from a portable v2 envelope) so cross-format hops can
287    /// return them exactly. Codex keeps its original dedicated store above.
288    pub native_residue: Vec<Value>,
289    /// The source format `native_residue` belongs to (e.g. `claude_code`).
290    pub native_residue_source: Option<String>,
291    /// The OpenCode analogue of [`Self::codex_headers`]
292    /// (`docs/interop/opencode-pi-spec.md` §1.2/§2.1): the verbatim
293    /// `SessionInfo` record (always element 0, or `Value::Null` if somehow
294    /// absent), plus any captured `session_diff`/`todo` side-records — each
295    /// wrapped as `{"key": [...], "value": ...}`, mirroring the envelope
296    /// shape `raw` uses, so a consumer can tell which storage key a header
297    /// record belongs to. These replay only via the direct-write fallback
298    /// (`Session::to_opencode_direct_write`); `opencode import` has no
299    /// ingestion path for `session_diff`/`todo` (S5).
300    pub opencode_headers: Vec<Value>,
301    /// Goose's native session-export object with `conversation` removed.
302    /// Goose stores sessions in SQLite but defines this JSON object as its
303    /// official import/export boundary. Keeping the shell lets an unchanged
304    /// direct round-trip remain byte exact while appended turns are spliced
305    /// into a stock-importable artifact without guessing native metadata.
306    pub goose_header: Option<Value>,
307    /// For a Claude Code subagent session: its `agentId` (the `agent-<id>` file
308    /// stem). `None` for top-level sessions.
309    pub agent_id: Option<String>,
310    /// For a subagent session: the `tool_use_id` of the parent `Task` call that
311    /// spawned it, recovered from the parent transcript's tool result. Best
312    /// effort — `None` if the link could not be established.
313    pub parent_tool_use_id: Option<String>,
314    /// Cross-file lineage keys for multi-file/multi-agent sessions (Codex
315    /// `parent_thread_id`, `forked_from_id`, `thread_source`, and the
316    /// `source.subagent.thread_spawn` fields `agent_role` / `agent_nickname` /
317    /// `depth`). Empty for a plain top-level session. Used by
318    /// [`Session::reconstruct_tree`] to nest children under their parents.
319    pub lineage: std::collections::BTreeMap<String, String>,
320    /// ORCH-3: why the session exists, when the source says.
321    pub trigger: Option<Trigger>,
322    /// ORCH-3: the conversation's surface identity, when it has one.
323    pub surface: Option<SurfaceKey>,
324    /// ORCH-3: routed config home (Hermes profile / OpenClaw agent / Codex profile).
325    pub profile: Option<String>,
326    /// ORCH-3: the job a recurring session belongs to.
327    pub recurrence: Option<Recurrence>,
328    /// ORCH-3: moved-to-another-surface state.
329    pub cross_surface: Option<CrossSurface>,
330}
331
332impl SessionMeta {
333    pub(crate) fn new(source: SessionSource) -> Self {
334        SessionMeta {
335            source,
336            session_id: None,
337            ended_at: None,
338            end_reason: None,
339            model: None,
340            cwd: None,
341            system_prompt: None,
342            codex_headers: Vec::new(),
343            codex_provenance: Vec::new(),
344            native_residue: Vec::new(),
345            native_residue_source: None,
346            opencode_headers: Vec::new(),
347            goose_header: None,
348            agent_id: None,
349            parent_tool_use_id: None,
350            lineage: std::collections::BTreeMap::new(),
351            trigger: None,
352            surface: None,
353            profile: None,
354            recurrence: None,
355            cross_surface: None,
356        }
357    }
358
359    /// The trigger, defaulting from what the loaders already know: a spawned
360    /// child (`agent_id` / a delegate lineage) is `Parent`; otherwise `Human`.
361    pub fn trigger_or_default(&self) -> Trigger {
362        if let Some(t) = self.trigger {
363            return t;
364        }
365        let delegate = self
366            .lineage
367            .get("hermes_lineage_kind")
368            .map(|k| k == "delegate")
369            .unwrap_or(false);
370        if self.agent_id.is_some() || self.parent_tool_use_id.is_some() || delegate {
371            Trigger::Parent
372        } else {
373            Trigger::Human
374        }
375    }
376
377    /// UNI-9 workspace with the D2 precedence: `repo` when a cwd exists, else
378    /// `channel` when the surface is a channel, else `none`. Derived, never stored.
379    pub fn workspace(&self) -> (WorkspaceKind, Option<String>) {
380        if let Some(cwd) = &self.cwd {
381            return (
382                WorkspaceKind::Repo,
383                Some(cwd.to_string_lossy().into_owned()),
384            );
385        }
386        if let Some(surface) = self.surface.as_ref().filter(|s| s.is_channel()) {
387            let label = match (&surface.platform, &surface.chat_id) {
388                (Some(p), Some(c)) => format!("{p}:{c}"),
389                (Some(p), None) => p.clone(),
390                _ => String::new(),
391            };
392            return (WorkspaceKind::Channel, Some(label));
393        }
394        (WorkspaceKind::None, None)
395    }
396
397    /// [`Self::workspace`] as the wire value. Naming only — the precedence
398    /// stays in `workspace()`.
399    pub fn workspace_ref(&self) -> WorkspaceRef {
400        let (kind, value) = self.workspace();
401        WorkspaceRef { kind, value }
402    }
403}
404
405/// A normalized, replayable conversation loaded from a tool's session log.
406#[derive(Debug, Clone)]
407pub struct Session {
408    /// Recovered metadata.
409    pub meta: SessionMeta,
410    /// The conversation, normalized to the OpenAI chat-completions shape.
411    pub messages: Vec<ChatMessage>,
412    /// Subagent (Task) sub-conversations. Claude Code stores these as separate
413    /// `<session>/subagents/agent-*.jsonl` files; loading a session by path now
414    /// discovers and attaches them here (each is a full [`Session`] whose
415    /// `meta.agent_id` / `meta.parent_tool_use_id` link it back to its spawn).
416    pub subagents: Vec<Session>,
417    /// Every original JSONL line of the source log, STRICT-VERBATIM (IX-1):
418    /// captured via `split_lines_verbatim`, not the blank-skipping/trimming
419    /// `non_empty_lines` parse view, so a blank line, a CRLF (`\r\n`)
420    /// terminator, or trailing whitespace on a line all survive bit-for-bit
421    /// rather than being dropped/normalized away. Normalization into
422    /// `messages` is still lossy by design (it targets the OpenAI replay
423    /// shape), but these raw lines retain *everything* — including records
424    /// with no canonical representation (e.g. Claude `file-history-snapshot`)
425    /// — so a round-trip through the supercode-native format
426    /// ([`Session::to_native_jsonl`]) is byte-lossless for the line-oriented
427    /// formats (Claude Code/Codex/Pi), for ANY input (see
428    /// [`Self::raw_trailing_newline`] for the one piece of information a line
429    /// list alone can't carry).
430    pub raw: Vec<String>,
431    /// Whether the source text `raw` was captured from ended with a trailing
432    /// `\n`. `raw`'s line list alone can't distinguish a source ending with a
433    /// trailing newline from one that doesn't (both split into the same
434    /// lines) — this flag carries that fact out-of-band so
435    /// [`Self::to_native_jsonl`]/[`Self::from_native_str`] can reproduce the
436    /// original source bytes exactly, including the presence/absence of a
437    /// final newline. `true` for a `Session` whose `raw` isn't captured
438    /// verbatim from real source text (e.g. OpenCode's re-synthesized
439    /// export-document `raw`, or a `Session` assembled programmatically) —
440    /// matching the historical always-terminated-by-newline behavior for
441    /// those cases.
442    pub raw_trailing_newline: bool,
443    /// How many of `messages` (and, symmetrically, of `raw` — see below) came
444    /// from parsing the imported log, as opposed to being appended after
445    /// import. Set once, at the end of [`Self::from_claude_code_str`] /
446    /// [`Self::from_codex_str`], to `messages.len()` at that moment — i.e.
447    /// before [`Self::from_native_str`]'s subsequent loop reattaches any
448    /// appended [`crate::sidecar::NativeTurn`] records onto `messages`/`raw`.
449    /// That loop pushes exactly one `raw` line and one message per appended
450    /// turn, so the two lists grow in lockstep from here on: the raw-prefix
451    /// boundary A12's [`Self::to_jsonl_spliced`] needs is always recoverable
452    /// as `raw.len() - (messages.len() - imported_message_count)`, without a
453    /// second counter. `None` only when a `Session` is constructed some other
454    /// way than through those two loaders — splicing then has no boundary to
455    /// honor and treats every message as imported (equivalent to
456    /// `Some(messages.len())`).
457    /// A bounded display-history projection uses this field for the total
458    /// number of normalized messages observed before its in-memory window was
459    /// applied. Such a semantic view is never a continuation source, and all
460    /// splice callers clamp the value to `messages.len()`.
461    pub imported_message_count: Option<usize>,
462    /// Whether `raw` was captured strict-verbatim from real source text
463    /// (`true`) or re-synthesized by this crate (`false`) — the fact
464    /// [`Self::raw_verbatim`]'s callers need to know before claiming a
465    /// same-format `convert` is byte-identical (PARITY-AUDIT.md P006/P007).
466    /// `true` for every line-oriented loader (`from_claude_code_str`,
467    /// `from_codex_str`, `from_pi_str`) and OpenCode's own ENVELOPE read
468    /// surface (`from_opencode_str`'s per-line loop) — each of those splits
469    /// `raw` directly out of the source text via `split_lines_verbatim`, so
470    /// replaying it reproduces the original bytes exactly. `false` for
471    /// OpenCode's EXPORT-DOCUMENT read surface
472    /// (`Session::from_opencode_export_doc`): a pretty-printed
473    /// `{info, messages:[...]}` document has no per-line envelope structure
474    /// of its own, so `raw` there is one envelope line RE-SYNTHESIZED per
475    /// record — faithful in value, but not the original document's bytes.
476    /// A `Session` assembled programmatically (not through a `from_*_str`
477    /// loader) also defaults to `false` — no real source text was captured
478    /// at all.
479    pub raw_is_verbatim: bool,
480    /// PARITY-15: how many non-empty lines of the source text FAILED to
481    /// deserialize at all (a genuinely malformed/truncated JSON line — not
482    /// a well-formed-but-unmodeled record type, which is a normal,
483    /// intentional "skip", tracked separately by `crate::audit`). Every
484    /// line-oriented loader tolerates a stray corrupt line rather than
485    /// hard-failing the whole load (a single bad line must not make an
486    /// otherwise-healthy multi-thousand-line session unloadable) — but that
487    /// tolerance used to be completely invisible: `Session::load` returned
488    /// `Ok` either way, with no signal that anything was skipped. This
489    /// count is what lets a caller (the CLI, `inspect`/`convert`) surface
490    /// that loss loudly instead of silently. `0` for a cleanly-parsed file,
491    /// and for a `Session` assembled programmatically.
492    pub parse_error_lines: usize,
493    /// Named degradations a [`Fidelity::Semantic`] load accepted instead of
494    /// failing — the same "say exactly what was given up" residue list
495    /// `harness.v1.sessions.export` already reports for artifacts.
496    ///
497    /// ALWAYS empty for a lossless load: every stricter fidelity refuses a
498    /// transcript it cannot reconstruct exactly, which is what keeps
499    /// continuation/transfer/export guarantees intact. A non-empty list means
500    /// this session is a read-only VIEW ([`Session::load_with_fidelity`] with
501    /// [`Fidelity::Semantic`]) and must not be used as a continuation source.
502    pub load_residue: Vec<String>,
503}
504
505impl Session {
506    /// The fidelity this reconstruction actually achieved.
507    ///
508    /// Same rule the export path applies to an artifact: named residue means
509    /// [`Fidelity::Semantic`]; otherwise a verbatim source capture is
510    /// [`Fidelity::ByteLossless`] and a re-synthesized one is
511    /// [`Fidelity::ValueLossless`]. A subagent's residue counts as this
512    /// session's: the whole reconstruction is only as faithful as its least
513    /// faithful part, and each child still reports its own residue where it
514    /// was measured.
515    pub fn load_fidelity(&self) -> Fidelity {
516        let own = if !self.load_residue.is_empty() {
517            Fidelity::Semantic
518        } else if self.raw_is_verbatim {
519            Fidelity::ByteLossless
520        } else {
521            Fidelity::ValueLossless
522        };
523        if own != Fidelity::Semantic
524            && self
525                .subagents
526                .iter()
527                .any(|subagent| subagent.load_fidelity() == Fidelity::Semantic)
528        {
529            return Fidelity::Semantic;
530        }
531        own
532    }
533
534    /// Assemble a session from supercode's own flat store transcript (one
535    /// [`ChatMessage`] per JSONL line). These files are the native working
536    /// format written by Supercode's native session store, not a foreign
537    /// harness log, so routing them through format auto-detection would
538    /// misclassify them as an empty Claude Code session.
539    pub fn from_native_messages(messages: Vec<ChatMessage>) -> Session {
540        Session {
541            meta: SessionMeta::new(SessionSource::Native),
542            messages,
543            subagents: Vec::new(),
544            raw: Vec::new(),
545            raw_trailing_newline: true,
546            imported_message_count: None,
547            raw_is_verbatim: false,
548            parse_error_lines: 0,
549            load_residue: Vec::new(),
550        }
551    }
552
553    /// Load a session, auto-detecting whether it's a Claude Code or Codex log
554    /// — or, when `path` looks like a SQLite database, a real OpenCode
555    /// `opencode*.db` store (PARITY-3/PARITY-16): that check runs BEFORE any
556    /// UTF-8 text read, so a binary `.db` file is routed to
557    /// [`Self::from_opencode_sqlite`] instead of failing on a raw "stream
558    /// did not contain valid UTF-8" error (the confirmed footgun these items
559    /// close — see [`looks_like_sqlite`] and the UTF-8 diagnostic reader).
560    ///
561    /// A DIRECTORY is also accepted directly: `path` is probed with
562    /// [`detect_opencode_storage_surface`] BEFORE the SQLite/UTF-8 file
563    /// checks below (both of which assume a file and would otherwise surface
564    /// a cryptic "Is a directory" `io::Error` — the confirmed footgun this
565    /// closes). This lets `inspect`/`convert`/`resume` accept an OpenCode
566    /// DATA-ROOT directly (e.g. `~/.local/share/opencode`), matching what
567    /// `audit --format opencode` already does. A resolved `Sqlite` surface
568    /// loads exactly like pointing `load` at that `opencode*.db` file
569    /// directly (most-recently-updated top-level session). The legacy
570    /// `JsonTreeA`/`JsonTreeB` surfaces are classifier-only (see
571    /// [`OpenCodeStorageSurface`]) — there's no direct-JSON-tree loader, so
572    /// that case returns a clear error naming the `.db` file / `audit` as the
573    /// way in, rather than silently doing nothing or crashing.
574    pub fn load(path: impl AsRef<Path>) -> Result<Session> {
575        Self::load_with_fidelity(path, Fidelity::ByteLossless)
576    }
577
578    /// Load a session at a declared [`Fidelity`].
579    ///
580    /// [`Fidelity::Semantic`] is the READ-ONLY VIEW mode: a transcript whose
581    /// record graph cannot be reconstructed exactly (the everyday case for a
582    /// Claude Code session that has been compacted or resumed across files,
583    /// where a live record's `parentUuid` names a record that was pruned)
584    /// still loads, stitched best-effort in transcript order, and names what
585    /// it gave up in [`Session::load_residue`]. Every stricter level keeps
586    /// the historical behavior — refuse loudly — because a continuation,
587    /// transfer or export built on a guessed graph is exactly the loss
588    /// supercode exists to prevent. Callers that go on to RESUME a session
589    /// must therefore use [`Session::load`].
590    pub fn load_with_fidelity(path: impl AsRef<Path>, fidelity: Fidelity) -> Result<Session> {
591        Self::load_with_fidelity_and_subagents(path, fidelity, true)
592    }
593
594    /// Load only the selected session's own transcript at a declared fidelity.
595    ///
596    /// This is the read-only frontend path: Claude Code can place hundreds of
597    /// child transcripts beside a parent, but a chat viewport displaying the
598    /// parent must not eagerly parse and transport that entire child tree.
599    /// Translation, continuation, export, and the ordinary [`Self::load`]
600    /// path keep attaching every subagent unchanged.
601    #[doc(hidden)]
602    pub fn load_parent_with_fidelity(
603        path: impl AsRef<Path>,
604        fidelity: Fidelity,
605    ) -> Result<Session> {
606        Self::load_with_fidelity_and_subagents(path, fidelity, false)
607    }
608
609    /// Load a bounded, parent-only transcript for human display.
610    ///
611    /// Unlike the continuation loader, Codex compaction records do not erase
612    /// earlier visible assistant turns here: the native rollout still holds
613    /// those records, and a scrollback view should show what the human saw,
614    /// not only the compacted context the next model call will receive.
615    #[doc(hidden)]
616    pub fn load_display_view(
617        path: impl AsRef<Path>,
618        fidelity: Fidelity,
619        message_limit: usize,
620    ) -> Result<Session> {
621        let path = path.as_ref();
622        if path.is_dir() || looks_like_sqlite(path) {
623            let mut session = Self::load_parent_with_fidelity(path, fidelity)?;
624            truncate_session_messages(&mut session, message_limit);
625            return Ok(session);
626        }
627        let mut read_limit = message_limit.max(1);
628        let mut previous_window_len = 0usize;
629        let (mut session, omitted_prefix) = loop {
630            let (source, text, omitted_prefix) = read_display_jsonl(path, read_limit)?;
631            let mut candidate = match source {
632                Some(SessionSource::Codex) => Self::from_codex_display_str(&text, message_limit)?,
633                Some(SessionSource::Gemini) => {
634                    let mut session = Self::from_gemini_str(&text)?;
635                    session.raw_is_verbatim = false;
636                    session.load_residue.push(
637                        "display history is a bounded native-record projection, not a complete Gemini artifact"
638                            .to_string(),
639                    );
640                    session
641                }
642                Some(SessionSource::Pi) => Self::from_pi_str(&text)?,
643                Some(SessionSource::Grok) => {
644                    let mut session = Self::from_grok_str(&text)?;
645                    session.capture_grok_path_metadata(path);
646                    session
647                }
648                Some(SessionSource::OpenCode) => Self::from_opencode_str(&text)?,
649                _ => Self::from_claude_code_str_with_fidelity(&text, fidelity)?,
650            };
651            let observed_messages = candidate
652                .imported_message_count
653                .unwrap_or(candidate.messages.len())
654                .max(candidate.messages.len());
655            let human_turns = candidate
656                .messages
657                .iter()
658                .filter(|message| message.role == Role::User)
659                .count();
660            let window_len = text.len();
661            let sufficient =
662                !omitted_prefix || (observed_messages > message_limit.max(1) && human_turns >= 2);
663            // 16 KiB/message with a 64 MiB ceiling means 4096 is the first
664            // read limit that cannot grow the native byte window further.
665            // Smaller repeated lengths can be the intentional 4 MiB floor;
666            // keep doubling through that plateau instead of declaring a
667            // false pagination end.
668            let byte_window_exhausted = window_len <= previous_window_len && read_limit >= 4096;
669            if sufficient || byte_window_exhausted {
670                if omitted_prefix {
671                    // The prefix is known to contain more native history even
672                    // when this bounded window cannot cheaply normalize its
673                    // exact size. Never turn that into a false end-of-history.
674                    candidate.imported_message_count =
675                        Some(observed_messages.max(message_limit.max(1).saturating_add(1)));
676                }
677                break (candidate, omitted_prefix);
678            }
679            previous_window_len = window_len;
680            read_limit = read_limit.saturating_mul(2);
681        };
682        if omitted_prefix {
683            session.load_residue.push(
684                "older native records remain outside this bounded display window".to_string(),
685            );
686        }
687        truncate_session_messages(&mut session, message_limit);
688        Ok(session)
689    }
690
691    fn load_with_fidelity_and_subagents(
692        path: impl AsRef<Path>,
693        fidelity: Fidelity,
694        include_subagents: bool,
695    ) -> Result<Session> {
696        let path = path.as_ref();
697        if path.is_dir() {
698            return match detect_opencode_storage_surface(path) {
699                Some((OpenCodeStorageSurface::Sqlite, db_path)) => {
700                    Self::from_opencode_sqlite(&db_path, None)
701                }
702                Some((
703                    OpenCodeStorageSurface::JsonTreeA | OpenCodeStorageSurface::JsonTreeB,
704                    _,
705                )) => Err(crate::Error::Other(format!(
706                    "{} is an OpenCode data root using a legacy JSON storage tree, which \
707                         supercode does not load directly — point `inspect`/`convert`/`resume` \
708                         at the store's `opencode*.db` SQLite file if this install has one, or \
709                         use `audit --format opencode {}` instead",
710                    path.display(),
711                    path.display()
712                ))),
713                None => Err(crate::Error::Other(format!(
714                    "{} is a directory, but no session file or OpenCode store was found in it \
715                     (expected an `opencode*.db` SQLite file, or an OpenCode legacy JSON storage \
716                     tree)",
717                    path.display()
718                ))),
719            };
720        }
721        if looks_like_sqlite(path) {
722            // Two SQLite-backed stores exist: OpenCode's (schema_meta-free
723            // key/value envelope db) and Hermes's `state.db` (UNI-15). The
724            // fingerprint check is cheap and read-only.
725            if let Ok(conn) = Connection::open_with_flags(
726                path,
727                rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY
728                    | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX,
729            ) {
730                if hermes_sqlite_fingerprint(&conn) {
731                    drop(conn);
732                    return Self::from_hermes_sqlite(path, None);
733                }
734            }
735            return Self::from_opencode_sqlite(path, None);
736        }
737        let text = read_utf8_or_diagnose(path)?;
738        match detect_source(&text) {
739            Some(SessionSource::Codex) => Self::from_codex_str(&text),
740            Some(SessionSource::Pi) => Self::from_pi_str(&text),
741            Some(SessionSource::OpenClaw) => {
742                let mut session = Self::from_openclaw_str(&text)?;
743                if session.meta.profile.is_none() {
744                    session.meta.profile = openclaw_agent_id_from_path(path);
745                }
746                Ok(session)
747            }
748            Some(SessionSource::Grok) => {
749                let mut session = Self::from_grok_str(&text)?;
750                session.capture_grok_path_metadata(path);
751                Ok(session)
752            }
753            Some(SessionSource::Gemini) => Self::from_gemini_str(&text),
754            Some(SessionSource::Goose) => Self::from_goose_str(&text),
755            // IX-3: a detected OpenCode session must route to its own
756            // loader, not the Claude Code fallback below
757            // (`docs/interop/build-followups.md`).
758            Some(SessionSource::OpenCode) => Self::from_opencode_str(&text),
759            _ => {
760                let mut session = Self::from_claude_code_str_with_fidelity(&text, fidelity)?;
761                if include_subagents {
762                    session.attach_claude_subagents(path, &text, fidelity)?;
763                }
764                Ok(session)
765            }
766        }
767    }
768
769    /// Reconstruct multi-file subagent trees from a flat set of loaded sessions.
770    ///
771    /// Codex stores subagents as separate rollout files linked to their parent
772    /// by `lineage["parent_thread_id"]` (→ the parent's `session_id`). Given a
773    /// collection of sessions, this nests each child into its parent's
774    /// [`Session::subagents`] and returns only the roots. Children whose parent
775    /// isn't in the set are returned as roots themselves (best effort).
776    pub fn reconstruct_tree(sessions: Vec<Session>) -> Vec<Session> {
777        use std::collections::HashMap;
778        // Index each session's position by its session_id.
779        let mut idx: HashMap<String, usize> = HashMap::new();
780        for (i, s) in sessions.iter().enumerate() {
781            if let Some(id) = &s.meta.session_id {
782                idx.insert(id.clone(), i);
783            }
784        }
785        // Determine each session's parent (by index), if present in the set.
786        let parent_of: Vec<Option<usize>> = sessions
787            .iter()
788            .map(|s| {
789                s.meta
790                    .lineage
791                    .get("parent_thread_id")
792                    .and_then(|p| idx.get(p).copied())
793            })
794            .collect();
795
796        // Move children into parents, deepest-first so chains nest correctly.
797        let mut slots: Vec<Option<Session>> = sessions.into_iter().map(Some).collect();
798        let mut order: Vec<usize> = (0..slots.len()).collect();
799        order.sort_by_key(|&i| std::cmp::Reverse(depth_of(i, &parent_of)));
800        for i in order {
801            if let Some(p) = parent_of[i] {
802                if p != i {
803                    if let Some(child) = slots[i].take() {
804                        if let Some(parent) = slots[p].as_mut() {
805                            parent.subagents.push(child);
806                        } else {
807                            slots[i] = Some(child); // parent already moved; keep as root
808                        }
809                    }
810                }
811            }
812        }
813        slots.into_iter().flatten().collect()
814    }
815
816    /// Parse a session of a known format from an in-memory JSONL string.
817    pub fn load_str(jsonl: &str, format: SessionFormat) -> Result<Session> {
818        match format {
819            SessionFormat::ClaudeCode => Self::from_claude_code_str(jsonl),
820            SessionFormat::Codex => Self::from_codex_str(jsonl),
821            SessionFormat::Pi => Self::from_pi_str(jsonl),
822            SessionFormat::OpenCode => Self::from_opencode_str(jsonl),
823            SessionFormat::Grok => Self::from_grok_str(jsonl),
824            SessionFormat::Gemini => Self::from_gemini_str(jsonl),
825            SessionFormat::Goose => Self::from_goose_str(jsonl),
826        }
827    }
828
829    /// Serialize this session to JSONL in the given format.
830    ///
831    /// The conversation is synthesized from the canonical messages, so this
832    /// works for sessions loaded from *either* tool as well as ones supercode
833    /// built itself. Converting between formats (e.g. Codex → Claude Code) is an
834    /// "export": format-specific framing that has no slot in the target may be
835    /// dropped, but the user/assistant/tool conversation is preserved.
836    pub fn to_jsonl(&self, format: SessionFormat) -> Result<String> {
837        match format {
838            SessionFormat::ClaudeCode => Ok(self.to_claude_code_jsonl()),
839            SessionFormat::Codex => Ok(self.to_codex_jsonl()),
840            SessionFormat::Pi => Ok(self.to_pi_jsonl()),
841            SessionFormat::OpenCode => self.to_opencode_jsonl(),
842            SessionFormat::Grok => Ok(self.to_grok_jsonl()),
843            SessionFormat::Gemini => Ok(self.to_gemini_jsonl()),
844            SessionFormat::Goose => Ok(self.to_goose_json()),
845        }
846    }
847
848    /// Export back to `format`, replaying the imported `raw` prefix
849    /// **verbatim** — original uuids/ids, real timestamps, and
850    /// loader-skipped records (e.g. Claude Code `file-history-snapshot`) that
851    /// [`Self::to_jsonl`]'s full synthesis discards or fakes — when `format`
852    /// is the session's own origin (`format.source() == self.meta.source`,
853    /// see [`SessionFormat::source`]) and there is a `raw` prefix to replay.
854    /// Only messages appended *after* import (tracked by
855    /// [`Self::imported_message_count`]) are synthesized, chained onto the
856    /// last original record found in the raw prefix.
857    ///
858    /// `session_id` of `Some(new)` rewrites the session id on every emitted
859    /// line, raw and synthesized alike (`sessionId` for Claude Code,
860    /// `session_meta.payload.id` for Codex); `None` leaves ids as recorded.
861    ///
862    /// Cross-format export (no verbatim prefix exists in the target dialect,
863    /// by definition) and a session with no `raw` lines both fall back
864    /// unchanged to [`Self::to_jsonl`] — full synthesis, same output as
865    /// today. A12 (SPEC.md §6): this turns "export back to origin" from
866    /// *semantic* to *near-byte* fidelity for the dominant hop-back case;
867    /// cross-format stays at the documented semantic tier.
868    pub fn to_jsonl_spliced(
869        &self,
870        format: SessionFormat,
871        session_id: Option<&str>,
872    ) -> Result<String> {
873        if self.parse_error_lines > 0
874            || self
875                .subagents
876                .iter()
877                .any(|subagent| subagent.parse_error_lines > 0)
878        {
879            return Err(Error::InvalidSession(
880                "refusing spliced export because the loaded session contains parse loss"
881                    .to_string(),
882            ));
883        }
884        if self.raw.is_empty() || format.source() != self.meta.source {
885            if let Some(session_id) = session_id {
886                let mut rewritten = self.clone();
887                rewritten.meta.session_id = Some(session_id.to_string());
888                return rewritten.to_jsonl(format);
889            }
890            return self.to_jsonl(format);
891        }
892        match format {
893            SessionFormat::ClaudeCode => Ok(self.to_claude_code_jsonl_spliced(session_id)),
894            SessionFormat::Codex => Ok(self.to_codex_jsonl_spliced(session_id)),
895            SessionFormat::Pi => self.to_pi_jsonl_spliced(session_id),
896            SessionFormat::OpenCode => self.to_opencode_jsonl_spliced(session_id),
897            SessionFormat::Grok => Ok(self.to_grok_jsonl_spliced()),
898            SessionFormat::Gemini => Ok(self.to_gemini_jsonl_spliced(session_id)),
899            SessionFormat::Goose => Ok(self.to_goose_json_spliced(session_id)),
900        }
901    }
902
903    /// Write this session to `path` in the given format.
904    pub fn save(&self, path: impl AsRef<Path>, format: SessionFormat) -> Result<()> {
905        std::fs::write(path.as_ref(), self.to_jsonl(format)?)?;
906        Ok(())
907    }
908
909    /// Reconstruct the exact source bytes this `Session` was loaded from,
910    /// out of [`Self::raw`] + [`Self::raw_trailing_newline`] (the exact
911    /// inverse of the strict-verbatim capture those two fields record — see
912    /// `join_lines_verbatim`).
913    ///
914    /// For a genuinely line-oriented source (Claude Code, Codex, Pi, and an
915    /// OpenCode *envelope*-form JSONL), `raw` is captured verbatim from the
916    /// original text, so this reproduces the original file byte-for-byte —
917    /// the P008/P009 diagonal-convert fix (`convert <file> --to
918    /// <same-format>` is byte-identical to `<file>`) is built on exactly
919    /// this. The one documented exception is an OpenCode **export-document**
920    /// source (a single pretty-printed JSON value, not JSONL): `raw` there
921    /// is RE-SYNTHESIZED as one envelope line per record (see
922    /// `from_opencode_export_doc`'s contract), so this returns a
923    /// verbatim reproduction of THAT captured representation rather than the
924    /// original pretty-printed document — a known, narrow residue, not a
925    /// silent loss (the same records are all still present).
926    pub fn raw_verbatim(&self) -> String {
927        join_lines_verbatim(&self.raw, self.raw_trailing_newline)
928    }
929
930    /// P5-5 (design §2 module 21 `session.tree`, §2.1 D-6 "session.tree →
931    /// core.session(tree-addressable transcript)"): materialize this
932    /// session's linear [`Self::messages`] into a native in-place
933    /// [`crate::session_tree::SessionTree`] — the bridge a caller uses the
934    /// FIRST time it wants to run a tree operation (rewind/branch/label)
935    /// against an otherwise-linear [`Session`]. `created_at_ms` stamps every
936    /// synthesized node (see
937    /// [`crate::session_tree::SessionTree::from_linear`]'s doc comment for
938    /// why a single timestamp is used: the source linear messages carry no
939    /// per-turn timestamp of their own here).
940    ///
941    /// This does not mutate `self` or persist anything — see
942    /// the composition layer's session-store tree writer for persistence, and
943    /// [`Self::apply_session_tree`] for the inverse bridge.
944    pub fn to_session_tree(&self, created_at_ms: i64) -> crate::session_tree::SessionTree {
945        crate::session_tree::SessionTree::from_linear(&self.messages, created_at_ms)
946    }
947
948    /// P5-5: the inverse of [`Self::to_session_tree`] — overwrite
949    /// [`Self::messages`] with `tree`'s ACTIVE branch's linear projection
950    /// (C7's "tree-with-linear-projection": this is exactly what keeps every
951    /// existing linear consumer — the agent loop, exporters — working
952    /// unchanged after a tree operation runs). Nothing else on `self`
953    /// (`meta`, `raw`, ...) is touched.
954    ///
955    /// **Fail-closed.** Propagates [`crate::session_tree::SessionTree::linear_projection`]'s
956    /// `Err` rather than applying anything — a structurally-corrupt tree
957    /// (a cycle, a dangling leaf, an active branch pointing at nothing) must
958    /// error, not silently overwrite [`Self::messages`] with an empty `Vec`.
959    /// `self` is left untouched on `Err` (the assignment only happens after
960    /// the projection has already succeeded).
961    pub fn apply_session_tree(&mut self, tree: &crate::session_tree::SessionTree) -> Result<()> {
962        self.messages = tree.linear_projection()?;
963        Ok(())
964    }
965}
966
967/// Read `path` as UTF-8 text, translating a non-UTF-8 failure into a clear,
968/// format-aware diagnostic (PARITY-16) instead of the raw "stream did not
969/// contain valid UTF-8" `io::Error` — named path, and what supercode DOES
970/// accept there. Binary SQLite input never reaches this function: callers
971/// check [`looks_like_sqlite`] first and route to
972/// [`Session::from_opencode_sqlite`] instead.
973pub(super) fn read_utf8_or_diagnose(path: &Path) -> Result<String> {
974    let bytes = std::fs::read(path)?;
975    String::from_utf8(bytes).map_err(|_| {
976        crate::Error::Other(format!(
977            "{} is not valid UTF-8 text, and is not a recognized OpenCode SQLite store \
978             (no `SQLite format 3` header) — supercode reads Claude Code / Codex / Pi / \
979             OpenCode session logs as UTF-8 JSONL, or an OpenCode `opencode*.db` SQLite file",
980            path.display()
981        ))
982    })
983}
984
985/// Read only the portion of a JSONL transcript a bounded scrollback can use.
986///
987/// The first record carries durable session metadata (especially for Codex),
988/// while the trailing window carries the messages the viewport will render.
989/// Full lossless loaders intentionally continue to read every byte.
990pub(super) fn read_display_jsonl(
991    path: &Path,
992    message_limit: usize,
993) -> Result<(Option<SessionSource>, String, bool)> {
994    const MIN_TAIL_BYTES: u64 = 4 * 1024 * 1024;
995    const MAX_TAIL_BYTES: u64 = 64 * 1024 * 1024;
996    const BYTES_PER_MESSAGE: u64 = 16 * 1024;
997
998    let mut first = String::new();
999    BufReader::new(std::fs::File::open(path)?).read_line(&mut first)?;
1000    let source = detect_source(&first);
1001    if !matches!(
1002        source,
1003        Some(SessionSource::ClaudeCode | SessionSource::Codex | SessionSource::Gemini)
1004    ) {
1005        let text = read_utf8_or_diagnose(path)?;
1006        return Ok((detect_source(&text), text, false));
1007    }
1008
1009    let mut file = std::fs::File::open(path)?;
1010    let file_len = file.metadata()?.len();
1011    let requested = (message_limit.max(1) as u64)
1012        .saturating_mul(BYTES_PER_MESSAGE)
1013        .clamp(MIN_TAIL_BYTES, MAX_TAIL_BYTES);
1014    if file_len <= requested {
1015        let text = read_utf8_or_diagnose(path)?;
1016        return Ok((source, text, false));
1017    }
1018
1019    let start = file_len - requested;
1020    file.seek(SeekFrom::Start(start))?;
1021    let mut bytes = Vec::with_capacity(requested as usize);
1022    file.read_to_end(&mut bytes)?;
1023    // The window normally starts in the middle of a JSON record. Discard that
1024    // partial prefix so every line passed to the existing parsers is valid.
1025    if let Some(newline) = bytes.iter().position(|byte| *byte == b'\n') {
1026        bytes.drain(..=newline);
1027    }
1028    let mut tail = String::from_utf8(bytes).map_err(|_| {
1029        crate::Error::Other(format!(
1030            "{} contains non-UTF-8 data in its display window",
1031            path.display()
1032        ))
1033    })?;
1034    if start > 0 {
1035        // Always recover the human boundary immediately before the byte
1036        // window, even when the window already contains newer prompts. A
1037        // long run of large tool records can otherwise make the numeric tail
1038        // begin in one old turn while its only retained users belong to much
1039        // newer turns. The display projector then (correctly) hides the
1040        // orphaned activity, making pagination appear inert.
1041        //
1042        // Search backward independently of the render window and retain only
1043        // two complete human JSONL records. The search grows geometrically but
1044        // never reads more than the same 64 MiB hard ceiling as the display
1045        // window, and none of the intervening tool bytes are normalized or
1046        // sent over RPC.
1047        let max_search_bytes = start.min(MAX_TAIL_BYTES);
1048        let mut search_bytes = requested.min(max_search_bytes);
1049        let anchors = loop {
1050            let search_start = start - search_bytes;
1051            file.seek(SeekFrom::Start(search_start))?;
1052            let mut search = Vec::with_capacity(search_bytes as usize);
1053            (&mut file).take(search_bytes).read_to_end(&mut search)?;
1054            if search_start > 0 {
1055                if let Some(newline) = search.iter().position(|byte| *byte == b'\n') {
1056                    search.drain(..=newline);
1057                } else {
1058                    search.clear();
1059                }
1060            }
1061            // `start` normally cuts the record whose remainder the tail
1062            // reader discarded. Exclude its incomplete prefix here too.
1063            if let Some(newline) = search.iter().rposition(|byte| *byte == b'\n') {
1064                search.truncate(newline + 1);
1065            } else {
1066                search.clear();
1067            }
1068            let anchors = std::str::from_utf8(&search)
1069                .ok()
1070                .map(|search| {
1071                    let mut found = search
1072                        .lines()
1073                        .rev()
1074                        .filter(|line| native_display_human_line(line, source))
1075                        .take(2)
1076                        .map(str::to_string)
1077                        .collect::<Vec<_>>();
1078                    found.reverse();
1079                    found
1080                })
1081                .unwrap_or_default();
1082            if anchors.len() >= 2 || search_start == 0 || search_bytes == max_search_bytes {
1083                break anchors;
1084            }
1085            search_bytes = search_bytes.saturating_mul(2).min(max_search_bytes);
1086        };
1087        if !anchors.is_empty() {
1088            tail = format!("{}\n{tail}", anchors.join("\n"));
1089        }
1090    }
1091    let text = if matches!(source, Some(SessionSource::Codex | SessionSource::Gemini)) {
1092        format!("{first}{tail}")
1093    } else {
1094        tail
1095    };
1096    Ok((source, text, true))
1097}
1098
1099#[cfg(test)]
1100mod orchestration_noun_tests {
1101    use super::*;
1102
1103    #[test]
1104    fn hermes_key_parses_profile_and_surface() {
1105        let (s, p) = parse_hermes_session_key("agent:coder:telegram:group:-100777:55:u9").unwrap();
1106        assert_eq!(p.as_deref(), Some("coder"));
1107        assert_eq!(s.platform.as_deref(), Some("telegram"));
1108        assert_eq!(s.kind.as_deref(), Some("group"));
1109        assert_eq!(s.chat_id.as_deref(), Some("-100777"));
1110        assert_eq!(s.thread_id.as_deref(), Some("55"));
1111        assert_eq!(s.participant_id.as_deref(), Some("u9"));
1112        let (_, p) = parse_hermes_session_key("agent:main:telegram:dm:1").unwrap();
1113        assert!(p.is_none());
1114        assert!(parse_hermes_session_key("cron:abc").is_none());
1115    }
1116
1117    #[test]
1118    fn openclaw_keys_parse_every_documented_shape() {
1119        let (a, s, t, r) =
1120            parse_openclaw_session_key("agent:design:slack:channel:C1:thread:T2").unwrap();
1121        assert_eq!(a.as_deref(), Some("design"));
1122        assert_eq!(s.platform.as_deref(), Some("slack"));
1123        assert_eq!(s.chat_id.as_deref(), Some("C1"));
1124        assert_eq!(s.thread_id.as_deref(), Some("T2"));
1125        assert_eq!(t, Trigger::Channel);
1126        assert!(r.is_none());
1127        let (a, s, t, _) = parse_openclaw_session_key("agent:main:main").unwrap();
1128        assert_eq!(a.as_deref(), Some("main"));
1129        assert_eq!(s.kind.as_deref(), Some("main"));
1130        assert_eq!(t, Trigger::Unknown);
1131        let (_, _, t, r) = parse_openclaw_session_key("cron:job-7").unwrap();
1132        assert_eq!(t, Trigger::Cron);
1133        assert_eq!(r.unwrap().job_id, "job-7");
1134        assert_eq!(
1135            parse_openclaw_session_key("hook:gmail:m1").unwrap().2,
1136            Trigger::Webhook
1137        );
1138        assert_eq!(
1139            parse_openclaw_session_key("acp-bridge:u").unwrap().2,
1140            Trigger::Api
1141        );
1142        assert!(parse_openclaw_session_key("garbage").is_none());
1143    }
1144
1145    #[test]
1146    fn hermes_source_and_cron_ids_classify() {
1147        assert_eq!(hermes_trigger_for_source("telegram"), Trigger::Channel);
1148        assert_eq!(hermes_trigger_for_source("cli"), Trigger::Human);
1149        assert_eq!(hermes_trigger_for_source("acp"), Trigger::Human);
1150        assert_eq!(hermes_trigger_for_source("api_server"), Trigger::Api);
1151        assert_eq!(hermes_trigger_for_source("cron"), Trigger::Cron);
1152        assert_eq!(hermes_trigger_for_source(""), Trigger::Unknown);
1153        assert_eq!(
1154            hermes_cron_job_id("cron_job42_20260902_120000").as_deref(),
1155            Some("job42")
1156        );
1157        assert_eq!(
1158            hermes_cron_job_id("cron_a_b_20260902_120000").as_deref(),
1159            Some("a_b")
1160        );
1161        assert!(hermes_cron_job_id("cron_job42_2026_1200").is_none());
1162        assert!(hermes_cron_job_id("adf8a015").is_none());
1163    }
1164
1165    #[test]
1166    fn workspace_precedence_repo_over_channel_over_none() {
1167        let mut meta = SessionMeta::new(SessionSource::Hermes);
1168        assert_eq!(meta.workspace().0, WorkspaceKind::None);
1169        meta.surface = Some(SurfaceKey {
1170            platform: Some("telegram".into()),
1171            chat_id: Some("1".into()),
1172            ..Default::default()
1173        });
1174        assert_eq!(
1175            meta.workspace(),
1176            (WorkspaceKind::Channel, Some("telegram:1".into()))
1177        );
1178        meta.cwd = Some(PathBuf::from("/w"));
1179        assert_eq!(meta.workspace().0, WorkspaceKind::Repo);
1180        assert_eq!(meta.trigger_or_default(), Trigger::Human);
1181        meta.agent_id = Some("a".into());
1182        assert_eq!(meta.trigger_or_default(), Trigger::Parent);
1183    }
1184
1185    #[test]
1186    fn openclaw_agent_id_comes_from_the_agents_directory() {
1187        let p = std::path::Path::new("/home/u/.openclaw/agents/design/sessions/x.jsonl");
1188        assert_eq!(openclaw_agent_id_from_path(p).as_deref(), Some("design"));
1189        assert!(openclaw_agent_id_from_path(std::path::Path::new("/tmp/x.jsonl")).is_none());
1190    }
1191}
1192
1193#[cfg(test)]
1194mod tests {
1195    use super::*;
1196
1197    #[test]
1198    fn a_new_user_message_slides_the_display_tail_without_becoming_its_only_anchor() {
1199        let mut messages = vec![
1200            ChatMessage::user("original prompt"),
1201            ChatMessage::assistant("one"),
1202            ChatMessage::assistant("two"),
1203            ChatMessage::assistant("three"),
1204            ChatMessage::assistant("four"),
1205            ChatMessage::assistant("five"),
1206            ChatMessage::user("new prompt"),
1207        ];
1208
1209        truncate_messages_with_anchor(&mut messages, 4, Vec::new());
1210
1211        assert_eq!(messages.len(), 4);
1212        assert_eq!(messages[0].content.as_deref(), Some("original prompt"));
1213        assert_eq!(messages[3].content.as_deref(), Some("new prompt"));
1214    }
1215
1216    #[test]
1217    fn a_tool_heavy_current_turn_keeps_the_previous_and_current_user_anchors() {
1218        let mut messages = vec![
1219            ChatMessage::user("previous prompt"),
1220            ChatMessage::assistant("previous answer"),
1221            ChatMessage::user("current prompt"),
1222            ChatMessage::assistant("tool one"),
1223            ChatMessage::assistant("tool two"),
1224            ChatMessage::assistant("tool three"),
1225            ChatMessage::assistant("tool four"),
1226            ChatMessage::assistant("tool five"),
1227        ];
1228
1229        truncate_messages_with_anchor(&mut messages, 4, Vec::new());
1230
1231        assert_eq!(messages.len(), 4);
1232        assert_eq!(messages[0].content.as_deref(), Some("previous prompt"));
1233        assert_eq!(messages[1].content.as_deref(), Some("current prompt"));
1234        assert_eq!(messages[3].content.as_deref(), Some("tool five"));
1235    }
1236
1237    #[test]
1238    fn a_preceding_user_anchor_is_restored_when_dedup_shrinks_below_the_limit() {
1239        let mut messages = vec![
1240            ChatMessage::user("current prompt"),
1241            ChatMessage::assistant("tool one"),
1242            ChatMessage::assistant("tool two"),
1243        ];
1244
1245        truncate_messages_with_anchor(&mut messages, 4, vec![ChatMessage::user("previous prompt")]);
1246
1247        assert_eq!(messages.len(), 4);
1248        assert_eq!(messages[0].content.as_deref(), Some("previous prompt"));
1249        assert_eq!(messages[1].content.as_deref(), Some("current prompt"));
1250    }
1251
1252    #[test]
1253    fn a_tool_heavy_turn_restores_two_users_that_both_left_the_retention_buffer() {
1254        let mut messages = vec![
1255            ChatMessage::assistant("tool one"),
1256            ChatMessage::assistant("tool two"),
1257            ChatMessage::assistant("tool three"),
1258            ChatMessage::assistant("tool four"),
1259        ];
1260
1261        truncate_messages_with_anchor(
1262            &mut messages,
1263            4,
1264            vec![
1265                ChatMessage::user("previous prompt"),
1266                ChatMessage::user("current prompt"),
1267            ],
1268        );
1269
1270        assert_eq!(messages.len(), 4);
1271        assert_eq!(messages[0].content.as_deref(), Some("previous prompt"));
1272        assert_eq!(messages[1].content.as_deref(), Some("current prompt"));
1273        assert_eq!(messages[3].content.as_deref(), Some("tool four"));
1274    }
1275
1276    #[test]
1277    fn a_loaded_boundary_anchor_survives_several_newer_user_turns() {
1278        let mut messages = vec![
1279            ChatMessage::assistant("older tool one"),
1280            ChatMessage::assistant("older tool two"),
1281            ChatMessage::user("recent prompt one"),
1282            ChatMessage::assistant("recent answer one"),
1283            ChatMessage::user("recent prompt two"),
1284            ChatMessage::assistant("recent answer two"),
1285            ChatMessage::user("current prompt"),
1286            ChatMessage::assistant("current tool"),
1287        ];
1288
1289        truncate_messages_with_anchor(
1290            &mut messages,
1291            6,
1292            vec![ChatMessage::user("loaded earlier boundary")],
1293        );
1294
1295        assert_eq!(messages.len(), 6);
1296        assert_eq!(
1297            messages[0].content.as_deref(),
1298            Some("loaded earlier boundary"),
1299            "newer user prompts must not replace the prompt that owns the retained activity",
1300        );
1301        assert_eq!(messages[4].content.as_deref(), Some("current prompt"));
1302        assert_eq!(messages[5].content.as_deref(), Some("current tool"));
1303    }
1304
1305    #[test]
1306    fn a_bounded_byte_window_recovers_preceding_users_even_when_its_tail_has_users() {
1307        let nonce = std::time::SystemTime::now()
1308            .duration_since(std::time::UNIX_EPOCH)
1309            .unwrap()
1310            .as_nanos();
1311        let path = std::env::temp_dir().join(format!(
1312            "supercode-display-boundary-{}-{nonce}.jsonl",
1313            std::process::id()
1314        ));
1315        let user = |text: &str| {
1316            format!(
1317                r#"{{"type":"response_item","payload":{{"type":"message","role":"user","content":[{{"type":"input_text","text":"{text}"}}]}}}}"#,
1318            )
1319        };
1320        let lines = [
1321            r#"{"type":"session_meta","payload":{"id":"session-1"}}"#.to_string(),
1322            user("preceding boundary"),
1323            format!(
1324                r#"{{"type":"event_msg","payload":{{"type":"token_count","noise":"{}"}}}}"#,
1325                "x".repeat(5 * 1024 * 1024)
1326            ),
1327            user("newer prompt one"),
1328            user("newer prompt two"),
1329        ];
1330        std::fs::write(&path, format!("{}\n", lines.join("\n"))).unwrap();
1331
1332        let (_, text, omitted_prefix) = read_display_jsonl(&path, 120).unwrap();
1333        std::fs::remove_file(&path).unwrap();
1334
1335        assert!(omitted_prefix);
1336        assert!(text.contains("preceding boundary"));
1337        assert!(text.contains("newer prompt one"));
1338        assert!(text.contains("newer prompt two"));
1339    }
1340}