rhema-transcript 0.4.2

The bounded view over an unbounded session: the request view, the slot algebra, and the zoom tool
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
//! The extractive line text: what the view says about messages it does
//! not carry whole.
//!
//! Everything here is mechanical, heads and counts read off the
//! messages themselves. No model call belongs to the transcript in v1
//! (spec §7.4): the lines cost no latency and cannot be wrong about
//! what happened, only short. A later compactor replaces exactly the
//! text generators below; the addresses stay.

use rhema_core::{Content, Message, Role};

use crate::tree::Range;

/// Byte cap on one quoted head inside a line.
pub const HEAD_BYTES: usize = 160;

/// Byte cap on one structural line covering a range.
pub const RANGE_HEAD_BYTES: usize = 300;

/// Byte cap on one user message carried whole in a digest. A user's
/// instruction is the one thing the block must not paraphrase, so it
/// stands as written up to this size. Past it the head stands and an
/// explicit marker states the message's full length, so a cut is
/// visible and worth a `zoom`.
pub const USER_BYTES: usize = 8 * 1024;

/// The bytes one image costs the budget. A provider tokenizes an image
/// by its pixels, not by its base64 length: a 228 KB screenshot is
/// about two thousand tokens, near the top of the range. Charging the
/// encoded length would count that screenshot as 57 KB of text, and an
/// image-heavy turn would flush the block over bytes compaction can
/// never remove. One image is charged as one small message.
pub const IMAGE_BYTES: usize = 1024;

/// The bytes a message costs the budget: text, tool-call arguments,
/// and tool-result content, summed, with each image at [`IMAGE_BYTES`]
/// instead of its encoded length. This is the unit the view's budget
/// spends.
pub fn message_bytes(message: &Message) -> usize {
    message
        .content
        .iter()
        .map(|content| match content {
            Content::Text(text) => text.len(),
            Content::ToolCall(call) => call.name.len() + call.args.to_string().len(),
            Content::ToolResult(result) => result.content.len() + result.images.len() * IMAGE_BYTES,
            Content::Image(_) => IMAGE_BYTES,
        })
        .sum()
}

/// The tokens one image stands for: a vision price, not a text
/// length. A flat figure keeps an image-heavy turn from reading as
/// millions of tokens; the footer's context gauge prices an image the
/// same way.
pub const IMAGE_TOKENS: u64 = 1_000;

/// The tokens a run of text stands for: about four bytes per token.
/// No tokenizer runs this side of the wire, so every token figure the
/// Usage page shows is this estimate, and the page marks it as one.
pub fn text_tokens(bytes: usize) -> u64 {
    bytes as u64 / 4
}

/// The estimated tokens a message stands for: its text at about four
/// bytes per token, each image at [`IMAGE_TOKENS`]. Summed part by
/// part, so the split can price each category with the same rounding.
pub fn message_tokens(message: &Message) -> u64 {
    message
        .content
        .iter()
        .map(|content| match content {
            Content::Text(text) => text_tokens(text.len()),
            Content::ToolCall(call) => text_tokens(call.name.len() + call.args.to_string().len()),
            Content::ToolResult(result) => {
                text_tokens(result.content.len()) + result.images.len() as u64 * IMAGE_TOKENS
            }
            Content::Image(_) => IMAGE_TOKENS,
        })
        .sum()
}

/// The images one message holds: tool-result images and bare image
/// parts. The count the view's image cap spends, and nothing else:
/// an image's bytes and tokens are the flat charges above.
pub fn message_images(message: &Message) -> usize {
    message
        .content
        .iter()
        .map(|content| match content {
            Content::ToolResult(result) => result.images.len(),
            Content::Image(_) => 1,
            _ => 0,
        })
        .sum()
}

/// `text` on one line, at most `cap` bytes, cut on a char boundary
/// with an ellipsis when longer.
pub fn head(text: &str, cap: usize) -> String {
    let flat = one_line(text);
    if flat.len() <= cap {
        return flat;
    }
    let mut end = cap;
    while end > 0 && !flat.is_char_boundary(end) {
        end -= 1;
    }
    format!("{}\u{2026}", &flat[..end])
}

/// A user message as one digest line: every message, whole, on one
/// line, cut only past [`USER_BYTES`] and then with a marker carrying
/// the length it lost. The marker matters: text that disappears with
/// no sign reads as text that was never there.
fn user_line(text: &str) -> String {
    let flat = one_line(text);
    if flat.len() <= USER_BYTES {
        return flat;
    }
    format!("{} [{} B, cut]", head(&flat, USER_BYTES), flat.len())
}

/// Newlines and tab runs become single spaces: a line is one line.
pub fn one_line(text: &str) -> String {
    let mut flat = String::with_capacity(text.len());
    let mut gap = false;
    for ch in text.chars() {
        if ch == '\n' || ch == '\r' || ch == '\t' {
            gap = true;
        } else {
            if gap && !flat.is_empty() {
                flat.push(' ');
            }
            gap = false;
            flat.push(ch);
        }
    }
    flat
}

/// The role's label in a line: `user`, `assistant`, `system`,
/// `result`.
fn role_label(role: Role) -> &'static str {
    match role {
        Role::User => "user",
        Role::Assistant => "assistant",
        Role::System => "system",
        Role::Tool => "result",
    }
}

/// The aggregate parts of a multi-message range, before addressing.
/// Also the body of the extractive section digest
/// ([`crate::compact::section_digest`]).
pub(crate) fn summarize_range(messages: &[Message], range: Range) -> ((usize, usize), String) {
    let mut user_heads: Vec<String> = Vec::new();
    let mut assistant_heads: Vec<String> = Vec::new();
    let mut calls: Vec<(String, usize)> = Vec::new();
    let mut results = 0usize;
    let mut failures = 0usize;
    let mut images = 0usize;

    // Assistant heads stay a two-line preview: the range's shape is
    // what a future reader needs. The user's words come whole below.
    let push_head = |heads: &mut Vec<String>, text: &str| {
        if heads.len() < 2 {
            heads.push(head(text, 80));
        }
    };
    for message in &messages[..range.n.min(messages.len())] {
        for content in &message.content {
            match content {
                Content::Text(text) => {
                    if message.role == Role::User {
                        user_heads.push(user_line(text));
                    } else if message.role == Role::Assistant {
                        push_head(&mut assistant_heads, text);
                    }
                }
                Content::ToolCall(call) => {
                    if let Some(seen) = calls.iter_mut().find(|(name, _)| *name == call.name) {
                        seen.1 += 1;
                    } else {
                        calls.push((call.name.clone(), 1));
                    }
                }
                Content::ToolResult(result) => {
                    results += 1;
                    failures += usize::from(result.is_error);
                    images += result.images.len();
                }
                // A head quotes text; an image has none. It still
                // counts: a digest that skips the images it covers
                // hides what zoom is there to reopen.
                Content::Image(_) => images += 1,
            }
        }
    }

    let mut parts: Vec<String> = Vec::new();
    parts.extend(user_heads.iter().map(|h| format!("user: {h}")));
    if !calls.is_empty() {
        calls.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
        let named: Vec<String> = calls
            .iter()
            .take(3)
            .map(|(name, count)| format!("{name}\u{d7}{count}"))
            .collect();
        let mut tool_line = if results == 0 {
            // Calls the range opens but its results land elsewhere:
            // say what was called, not that nothing answered.
            format!(
                "{} tool {} ({})",
                calls.iter().map(|(_, n)| n).sum::<usize>(),
                if calls.iter().map(|(_, n)| n).sum::<usize>() == 1 {
                    "call"
                } else {
                    "calls"
                },
                named.join(", ")
            )
        } else {
            format!(
                "{} tool {} ({})",
                results,
                if results == 1 { "result" } else { "results" },
                named.join(", ")
            )
        };
        if calls.len() > 3 {
            tool_line.push_str(", \u{2026}");
        }
        parts.push(tool_line);
    } else if results > 0 {
        parts.push(format!(
            "{} tool {}",
            results,
            if results == 1 { "result" } else { "results" }
        ));
    }
    if failures > 0 {
        parts.push(format!("{failures} failed"));
    }
    if images > 0 {
        parts.push(format!(
            "{images} {}",
            if images == 1 { "image" } else { "images" }
        ));
    }
    parts.extend(assistant_heads.iter().map(|h| format!("assistant: {h}")));
    ((range.id, range.n), parts.join("; "))
}

/// The whole render of one message, as `zoom(id, 1)` returns it: user
/// and assistant text verbatim, tool calls and results spelled out.
/// Uncapped, the zoom pages.
pub fn render_message(message: &Message) -> String {
    let mut out = String::new();
    for content in &message.content {
        match content {
            Content::Text(text) => {
                if !out.is_empty() {
                    out.push('\n');
                }
                out.push_str(text);
            }
            Content::ToolCall(call) => {
                if !out.is_empty() {
                    out.push('\n');
                }
                out.push_str(&format!("[calls {}({})]", call.name, call.args));
            }
            Content::ToolResult(result) => {
                if !out.is_empty() {
                    out.push('\n');
                }
                out.push_str(&format!(
                    "[{}: {}]",
                    if result.is_error { "failed" } else { "ok" },
                    result.content
                ));
                for image in &result.images {
                    out.push_str(&format!("\n[image: {}]", image.media_type.as_str()));
                }
            }
            Content::Image(image) => {
                if !out.is_empty() {
                    out.push('\n');
                }
                out.push_str(&format!("[image: {}]", image.media_type.as_str()));
            }
        }
    }
    if out.is_empty() {
        out.push_str(&format!("({} message, empty)", role_label(message.role)));
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use rhema_core::{ToolCall, ToolResult};

    #[test]
    fn a_results_images_count_and_describe() {
        let message = Message {
            annotations: Default::default(),
            role: Role::Tool,
            content: vec![Content::ToolResult(
                ToolResult::success("c1", "out").with_images(vec![rhema_core::Image::new(
                    rhema_core::ImageMediaType::Png,
                    "cG5n",
                )]),
            )],
        };
        // "out" plus one image at its flat budget charge, not the
        // base64 data's four bytes.
        assert_eq!(message_bytes(&message), 3 + IMAGE_BYTES);
        let rendered = render_message(&message);
        assert!(rendered.contains("[image: image/png]"), "{rendered}");
    }

    #[test]
    fn tokens_are_text_over_four_plus_a_flat_image_price() {
        let message = Message {
            annotations: Default::default(),
            role: Role::Tool,
            content: vec![Content::ToolResult(
                ToolResult::success("c1", "abcdefgh").with_images(vec![
                    rhema_core::Image::new(rhema_core::ImageMediaType::Png, "a".repeat(4_000_000)),
                    rhema_core::Image::new(rhema_core::ImageMediaType::Png, "b".repeat(8)),
                ]),
            )],
        };
        // Eight bytes of text is two tokens; the two images are a
        // flat price each, whatever their base64 length.
        assert_eq!(message_tokens(&message), 2 + 2 * IMAGE_TOKENS);
    }

    /// The cap's unit: tool-result images and bare image parts count,
    /// text does not. The digest names the same figure, so a range's
    /// line says what zoom would bring back.
    #[test]
    fn the_image_count_is_its_own_figure() {
        let user = Message {
            annotations: Default::default(),
            role: Role::User,
            content: vec![
                Content::Text("look".into()),
                Content::Image(rhema_core::Image::new(
                    rhema_core::ImageMediaType::Png,
                    "cG5n",
                )),
            ],
        };
        let result = Message {
            annotations: Default::default(),
            role: Role::Tool,
            content: vec![Content::ToolResult(
                ToolResult::success("c1", "out").with_images(vec![
                    rhema_core::Image::new(rhema_core::ImageMediaType::Png, "YQ"),
                    rhema_core::Image::new(rhema_core::ImageMediaType::Png, "Yg"),
                ]),
            )],
        };
        assert_eq!(message_images(&user), 1);
        assert_eq!(message_images(&result), 2);
        let messages = vec![user, result];
        let ((id, n), text) = summarize_range(&messages, Range { id: 0, n: 2 });
        assert_eq!((id, n), (0, 2));
        assert!(text.contains("3 images"), "{text}");
    }

    #[test]
    fn an_images_encoded_length_does_not_reach_the_budget() {
        let tiny = Message {
            annotations: Default::default(),
            role: Role::Tool,
            content: vec![Content::ToolResult(
                ToolResult::success("c1", "").with_images(vec![rhema_core::Image::new(
                    rhema_core::ImageMediaType::Png,
                    "a".repeat(4_000_000),
                )]),
            )],
        };
        // Four megabytes of base64 is one message's worth of context.
        assert_eq!(message_bytes(&tiny), IMAGE_BYTES);
    }

    #[test]
    fn every_user_text_gets_a_line_whole() {
        let mut messages: Vec<Message> = (0..12)
            .map(|i| Message::text(Role::User, format!("decision {i}: keep this")))
            .collect();
        messages.push(Message::text(Role::Assistant, "done"));
        let (_, text) = summarize_range(&messages, Range { id: 0, n: 13 });
        for i in 0..12 {
            assert!(
                text.contains(&format!("user: decision {i}: keep this")),
                "{text}"
            );
        }
        assert!(!text.contains("cut"), "{text}");
    }

    #[test]
    fn a_user_text_past_the_cap_says_what_it_lost() {
        let long = "x".repeat(USER_BYTES + 500);
        let messages = vec![Message::text(Role::User, long)];
        let (_, text) = summarize_range(&messages, Range { id: 0, n: 1 });
        // The whole head, then the marker naming the bytes that did
        // not fit.
        let head = format!("user: {}", "x".repeat(USER_BYTES));
        assert!(
            text.starts_with(&head),
            "digest starts wrong: {}",
            &text[..64]
        );
        let marker = format!(" [{} B, cut]", USER_BYTES + 500);
        assert!(
            text.ends_with(&marker),
            "digest ends wrong: len {}",
            text.len()
        );
        assert_eq!(text.len(), head.len() + "\u{2026}".len() + marker.len());
    }

    #[test]
    fn heads_cut_on_char_boundaries() {
        // Each 'é' is two bytes; a byte cap of 5 lands mid-char.
        let text = "ééééé";
        let h = head(text, 5);
        // Cap 5 lands mid-char; the cut backs up to the boundary and
        // the ellipsis rides on top: "éé" + "…".
        assert_eq!(h, "\u{e9}\u{e9}\u{2026}");
        assert_eq!(head("short", 10), "short");
    }

    #[test]
    fn one_line_flattens_newlines_but_keeps_inner_spaces() {
        assert_eq!(one_line("a\nb\r\nc\td"), "a b c d");
        assert_eq!(one_line("\n\nlead"), "lead");
    }

    #[test]
    fn bytes_count_text_arguments_and_results() {
        let call = Message {
            annotations: Default::default(),
            role: Role::Assistant,
            content: vec![
                Content::Text("running".into()),
                Content::ToolCall(ToolCall {
                    id: "c1".into(),
                    name: "bash".into(),
                    args: serde_json::json!({ "command": "ls" }),
                    provider_data: serde_json::Value::Null,
                }),
            ],
        };
        // "running" (7) + "bash" (4) + {"command":"ls"} (16)
        assert_eq!(message_bytes(&call), 7 + 4 + 16);
    }

    #[test]
    fn a_single_message_renders_whole_for_zoom() {
        let message = Message {
            annotations: Default::default(),
            role: Role::Assistant,
            content: vec![
                Content::Text("step one".into()),
                Content::ToolCall(ToolCall {
                    id: "c9".into(),
                    name: "edit".into(),
                    args: serde_json::json!({ "path": "a.rs" }),
                    provider_data: serde_json::Value::Null,
                }),
            ],
        };
        let out = render_message(&message);
        assert_eq!(out, "step one\n[calls edit({\"path\":\"a.rs\"})]");
    }
}