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
//! P4e (COMPOSABLE-HARNESS-DESIGN.md §1.6/§3.1 `core.session.export_format`,
//! catalog:283 "transcript export for humans"): a READ-ONLY rendering of a
//! [`crate::Session`]'s conversation into text a human reads directly
//! (terminal/file/clipboard) or opens in a browser — CC's `/export`+`/copy`,
//! CX's Ctrl+O copy-last. This is core, not gated by the `session.share`
//! module (§1.6: "export-to-human is universal while *share links* … are
//! the OC+PI-only part `session.share` actually narrows to").
//!
//! Deliberately distinct from [`crate::reduce::export_session`], which
//! translates a session losslessly BETWEEN harness wire formats (priority-1
//! "translate" — machine-to-machine, round-trippable, JSONL). This module
//! goes the other direction: session (any harness, already loaded) to a
//! human-readable rendering (JSONL in, prose/markup out — deliberately NOT
//! round-trippable, and never claims to be). Per §1.13, the session DATA
//! itself stays typed/lossless in the sidecar; a render is a projection a
//! human reads, never a channel anything is reconstructed from — so
//! [`render_transcript`] takes `&Session` (never mutates it) and returns an
//! owned `String`.
use crate::message::{ChatMessage, Role};
use crate::session::Session;
/// `core.session.export_format` (§3.1): which rendering
/// [`render_transcript`] produces.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum HumanExportFormat {
/// Plain text, one paragraph per message, role-labeled headers. The
/// default.
#[default]
Text,
/// A minimal, dependency-free (no template engine) standalone HTML
/// document — safe to open directly in a browser.
Html,
}
impl HumanExportFormat {
/// Parse the `"text"` / `"html"` config strings (§3.1
/// `core.session.export_format`). Unrecognized input is `None` — the
/// caller decides the fail-safe fallback (mirrors
/// `SteeringMode::parse`'s contract).
pub fn parse(s: &str) -> Option<HumanExportFormat> {
match s {
"text" => Some(HumanExportFormat::Text),
"html" => Some(HumanExportFormat::Html),
_ => None,
}
}
}
/// Render `session`'s conversation (`session.messages`, in order) for a
/// human, in `format`. Pure/read-only: `session` is untouched, and calling
/// this twice on the same session is idempotent (same output both times).
/// System messages are included — they're part of the honest record of
/// what happened (e.g. compaction markers, `context_injections` blocks).
pub fn render_transcript(session: &Session, format: HumanExportFormat) -> String {
render_messages(
&session.messages,
session.meta.session_id.as_deref(),
session.meta.model.as_deref(),
format,
)
}
/// The lower-level entry point [`render_transcript`] delegates to: render a
/// bare `messages` slice (no [`Session`] wrapper required) — for a caller
/// (e.g. the CLI's `sessions export`) that already has the parsed
/// [`ChatMessage`]s and a session name/model but not a full [`Session`]
/// (which is `#[non_exhaustive]` and cannot be constructed outside this
/// crate). Same read-only/idempotent contract as [`render_transcript`].
pub fn render_messages(
messages: &[ChatMessage],
session_id: Option<&str>,
model: Option<&str>,
format: HumanExportFormat,
) -> String {
match format {
HumanExportFormat::Text => render_text(messages, session_id, model),
HumanExportFormat::Html => render_html(messages, session_id, model),
}
}
fn role_label(role: Role) -> &'static str {
match role {
Role::System => "System",
Role::User => "User",
Role::Assistant => "Assistant",
Role::Tool => "Tool",
}
}
/// The body text a single message contributes to a render: its plain
/// `content` if set, else a placeholder describing any tool calls / an
/// empty turn — every message contributes SOME visible line, so a reader
/// never sees a silently-skipped turn.
fn message_body(msg: &ChatMessage) -> String {
let mut parts = Vec::new();
if let Some(content) = &msg.content {
if !content.is_empty() {
parts.push(content.clone());
}
}
if let Some(calls) = &msg.tool_calls {
for call in calls {
parts.push(format!(
"[tool call: {}({})]",
call.function.name, call.function.arguments
));
}
}
if parts.is_empty() {
parts.push("(empty)".to_string());
}
parts.join("\n")
}
fn render_text(messages: &[ChatMessage], session_id: Option<&str>, model: Option<&str>) -> String {
let mut out = String::new();
if let Some(id) = session_id {
out.push_str(&format!("Session: {id}\n"));
}
if let Some(model) = model {
out.push_str(&format!("Model: {model}\n"));
}
if !out.is_empty() {
out.push('\n');
}
for (i, msg) in messages.iter().enumerate() {
if i > 0 {
out.push('\n');
}
out.push_str(&format!("## {}\n", role_label(msg.role)));
out.push_str(&message_body(msg));
out.push('\n');
}
out
}
fn html_escape(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
}
fn render_html(messages: &[ChatMessage], session_id: Option<&str>, model: Option<&str>) -> String {
let mut out = String::new();
out.push_str("<!doctype html>\n<html><head><meta charset=\"utf-8\">\n");
out.push_str("<title>supercode session export</title>\n");
out.push_str(
"<style>body{font-family:monospace;max-width:60rem;margin:2rem auto;padding:0 1rem}\
.msg{border-left:3px solid #ccc;margin:1rem 0;padding:0 1rem;white-space:pre-wrap}\
.role{font-weight:bold}</style>\n",
);
out.push_str("</head><body>\n");
if let Some(id) = session_id {
out.push_str(&format!("<p>Session: {}</p>\n", html_escape(id)));
}
if let Some(model) = model {
out.push_str(&format!("<p>Model: {}</p>\n", html_escape(model)));
}
for msg in messages {
out.push_str("<div class=\"msg\"><div class=\"role\">");
out.push_str(role_label(msg.role));
out.push_str("</div><div class=\"body\">");
out.push_str(&html_escape(&message_body(msg)));
out.push_str("</div></div>\n");
}
out.push_str("</body></html>\n");
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::message::{FunctionCall, ToolCall};
use crate::session::{Session, SessionSource};
fn sample_session() -> Session {
let mut session = Session::from_native_messages(vec![
ChatMessage::system("you are helpful".to_string()),
ChatMessage::user("list files".to_string()),
ChatMessage {
role: Role::Assistant,
content: None,
content_parts: None,
tool_calls: Some(vec![ToolCall {
id: "call-1".to_string(),
kind: "function".to_string(),
function: FunctionCall {
name: "bash".to_string(),
arguments: r#"{"command":"ls"}"#.to_string(),
},
}]),
tool_call_id: None,
name: None,
metadata: Default::default(),
},
ChatMessage::tool_result("call-1", "bash", "a.txt\nb.txt".to_string()),
]);
session.meta.source = SessionSource::ClaudeCode;
session.meta.session_id = Some("sess-1".to_string());
session.meta.model = Some("anthropic/claude-opus-4-8".to_string());
session
}
/// Default-unchanged: `HumanExportFormat::default()` is `Text` (the
/// annotated §3.1 schema's illustrative `export_format = "text"`
/// example value).
#[test]
fn default_export_format_is_text() {
assert_eq!(HumanExportFormat::default(), HumanExportFormat::Text);
}
#[test]
fn parse_round_trips_known_strings() {
assert_eq!(
HumanExportFormat::parse("text"),
Some(HumanExportFormat::Text)
);
assert_eq!(
HumanExportFormat::parse("html"),
Some(HumanExportFormat::Html)
);
assert_eq!(HumanExportFormat::parse("xml"), None);
}
/// Happy path: every message contributes a visible section, including
/// the tool-call/tool-result pair, and the render never panics/loses a
/// turn silently.
#[test]
fn text_render_includes_every_message() {
let session = sample_session();
let text = render_transcript(&session, HumanExportFormat::Text);
assert!(text.contains("Session: sess-1"));
assert!(text.contains("Model: anthropic/claude-opus-4-8"));
assert!(text.contains("## System"));
assert!(text.contains("you are helpful"));
assert!(text.contains("## User"));
assert!(text.contains("list files"));
assert!(text.contains("## Assistant"));
assert!(text.contains("[tool call: bash({\"command\":\"ls\"})]"));
assert!(text.contains("## Tool"));
assert!(text.contains("a.txt\nb.txt"));
}
/// Boundary: an empty session (no messages) renders without panicking
/// and without fabricating content.
#[test]
fn text_render_handles_empty_session() {
let mut session = sample_session();
session.messages.clear();
let text = render_transcript(&session, HumanExportFormat::Text);
assert!(text.contains("Session: sess-1"));
assert!(!text.contains("##"));
}
/// HTML render escapes hostile content instead of injecting it — a
/// transcript containing `<script>` must not become live markup in the
/// rendered document.
#[test]
fn html_render_escapes_message_content() {
let mut session = sample_session();
session
.messages
.push(ChatMessage::user("<script>alert(1)</script>".to_string()));
let html = render_transcript(&session, HumanExportFormat::Html);
assert!(!html.contains("<script>alert(1)</script>"));
assert!(html.contains("<script>alert(1)</script>"));
assert!(html.contains("<!doctype html>"));
}
/// Read-only guarantee (§1.6 "a RENDER … never mutates the session"):
/// rendering twice is idempotent and the session's own fields are
/// untouched (checked via a full clone-and-compare of the messages,
/// since `Session` has no derived `PartialEq`).
#[test]
fn render_is_read_only_and_idempotent() {
let session = sample_session();
let before_len = session.messages.len();
let first = render_transcript(&session, HumanExportFormat::Text);
let second = render_transcript(&session, HumanExportFormat::Text);
assert_eq!(first, second);
assert_eq!(session.messages.len(), before_len);
}
/// `render_messages` is what a caller without a full `Session` (e.g.
/// the CLI, which only has a parsed `Vec<ChatMessage>` plus a name) can
/// call directly — proves it agrees with `render_transcript` on the
/// same underlying data.
#[test]
fn render_messages_agrees_with_render_transcript() {
let session = sample_session();
let via_session = render_transcript(&session, HumanExportFormat::Text);
let via_messages = render_messages(
&session.messages,
session.meta.session_id.as_deref(),
session.meta.model.as_deref(),
HumanExportFormat::Text,
);
assert_eq!(via_session, via_messages);
}
}