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
//! The `--lens` seam (SPEC.md §Lenses).
//!
//! # What a lens is, and what it is not
//!
//! A lens is a **transform over records**, not a format. It never reads a file,
//! never owns a [`crate::source::Source`], and never decides where a row goes
//! on the screen: given one parsed record it says *what that record is* — who
//! spoke, when, what happened, and whether the record is conversation or
//! mechanics. The record seam ([`crate::source::record`]) does the rest: it
//! turns runs of mechanics into one foldable group row and leaves the
//! conversation alone.
//!
//! That split is the whole point. A second dialect — opencode, OpenAI Codex,
//! ATIF — is a new module here and one entry in [`LENSES`]; nothing about
//! folding, row arithmetic, the levels a record has, search or yanking is
//! written twice. [`part`] is the vocabulary the second half of that answer is
//! given in: what a record's parts *are*, said without saying anything about
//! rows.
//!
//! # The contract
//!
//! * [`Lens::read`] returns `None` for a record it does not recognise, and that
//! record renders as the generic JSON tree. **A lens adds interpretation; it
//! must never lose data** (SPEC.md §Lenses: "an unrecognised record falls
//! back to the generic rendering rather than being hidden"). Even a record it
//! *does* recognise keeps every byte: the summary is a headline, and opening
//! the row still shows the whole record as a tree.
//! * Records are read **in file order, once each**, which is what lets a lens
//! carry a little state across them — the agent dialect matches a tool result
//! back to the call that made it. `read` therefore takes `&mut self`. A lens
//! whose state is wrong (a record read out of order, a truncated log) must
//! still return a usable summary rather than nothing.
//! * Nothing here allocates per *document*: a summary is built from one record
//! and thrown away with it — and a [`Body`] keeps that true for the message
//! text as well, by holding a bounded head and *where the rest is* rather
//! than a copy of it.
//! * [`Lens::detail`] is the exception that proves it: the parts of a record —
//! its tool calls, with their arguments and what came back — are read **when
//! the reader opens that record** and are never stored. A `Summary` is per
//! record and forever; a `Vec<Part>` is per keystroke and momentary.
pub use tokens;
pub use Part;
use crateValue;
use crateStyle;
use cratetheme;
/// Whether a record is conversation or mechanics.
///
/// This is the one decision that changes the *shape* of the document: a run of
/// consecutive [`Class::Step`] records collapses into a single summary row that
/// opens, while a [`Class::Message`] is always on screen.
/// Who a row is attributed to. Colour only — the text is [`Summary::actor`],
/// so a dialect can say `subagent` and still be painted as an assistant.
/// One step of the way back to a message inside its own record: a key, or an
/// index into an array. Static keys because a dialect names them in its own
/// source (`message`, `content`, `text`) — nothing is allocated to remember
/// where a message lives.
/// Bytes of a message the seam keeps in memory, per record.
///
/// The ceiling on what a summary costs: a 40 KB message contributes 1 KB and a
/// path, not 40 KB. Enough to lay a clipped body out at any width up to about
/// 190 columns, and past that the clip shows fewer rows and still says what it
/// is not showing — which is the half that may not slip.
pub const BODY_KEEP: usize = 1024;
/// The message text under a summary row (SPEC.md §Lenses).
///
/// # Why this is a head and a path, not the text
///
/// A summary is kept for **every** classified record, and this module's
/// contract is that nothing here allocates per document. Holding each message
/// in full would make a long log's summaries as big as the log. So a `Body`
/// keeps:
///
/// * `head` — the first [`BODY_KEEP`] bytes, which is what a clipped row paints
/// and what the row *arithmetic* measures, so a resize re-lays every body
/// without reading a file;
/// * `bytes` and `lines` — what the whole message is, so a clipped body can say
/// what it is not showing, and so its height at a width can be *derived*
/// rather than measured;
/// * `at` — where the text sits inside the record, so painting reads the whole
/// of it back out of the record that is being painted anyway.
///
/// The dialect supplies text and a path and makes no layout decision: how many
/// rows this becomes is [`crate::source::record::body`]'s answer, and it
/// depends on the width.
/// The first `bytes` of `text`, cut on a character boundary: a message may be
/// any UTF-8 at all, and cutting mid-character would panic.
/// One record, as a lens reads it.
///
/// Everything on it is display text the source paints; no styling and no
/// layout decisions, so the same summary reads the same way in the pager, in a
/// dump and in a test.
/// Where a dialect's records live.
///
/// A lens is a transform over records, and this is the one thing it says about
/// where they come from — because that decides which *file* the flag can be
/// pointed at, and nothing else about the dialect does. It is a declaration,
/// not a reader: `src/open/lens.rs` turns it into a format, and
/// `src/source/record/` reaches the records the same way whichever it is.
/// One dialect of record file.
/// How a lens is built. A function pointer rather than a value because a lens
/// carries state across records, so every open needs its own.
type Make = fn ;
/// Every lens there is. **Adding a dialect is one module and one line here**
/// (docs/lenses.md says what the module has to provide).
const LENSES: & = &;
/// The lens called `name`, or `None`.
/// Where the lens called `name` keeps its records. The routing asks before the
/// file is opened, because the answer decides which formats `--lens <name>`
/// will accept.
/// Is `name` a lens? The CLI asks before the file is opened.
/// The names, for an error message.
/// `--lens list`: the available lenses, one per line. The description comes
/// from the lens itself, so a dialect says what it is in one place.
///
/// The gutter is *after* the padding rather than inside it: `usage-atif` is
/// exactly ten columns, and a name that filled the field would otherwise run
/// straight into its own description.
// -- helpers every dialect wants ------------------------------------------------
/// Columns a one-line excerpt of what someone said may occupy.
pub const EXCERPT: usize = 160;
/// Columns a tool's argument may occupy on a summary row.
pub const ARG: usize = 56;
/// The first line of `text`, whitespace collapsed, cut to `cols` columns.
///
/// A summary row stands for a record that may be forty kilobytes; this is what
/// makes it one line. The record itself is untouched — opening the row shows
/// all of it.
/// `HH:MM` of an ISO-8601 timestamp such as `2026-08-05T21:28:58.659Z`.
///
/// No timezone database and no dependencies, so this is the *recorded* time,
/// which in every agent log seen so far is UTC. Showing `21:28` rather than the
/// 24-character original is the whole point: a reader scanning a log wants the
/// clock, and the date is the same for almost every row in a session.
/// `None` when the string is not shaped like a timestamp, which is how a
/// dialect avoids showing an arbitrary substring of some other field.
/// Works on bytes rather than on `&str` slices on purpose: the text comes
/// straight out of the log, so byte 16 is not known to be a character boundary
/// and `&ts[14..16]` would panic on `2026-08-05T21:€z`. Every byte this accepts
/// is an ASCII digit, so the answer is built from those digits directly.
/// The `timestamp` field of a record as a clock time.
/// A record's `type`, when it has one that is a string.