agix-telemetry 0.4.5

Agix — telemetry, usage stats, LLM call recording, perf
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Lossless run-length packing for streamed delta chunks.
//!
//! Providers emit token-sized deltas, so a recording that stores one line per
//! chunk spends most of its bytes on JSON envelopes rather than content. This
//! module folds a run of consecutive same-kind deltas belonging to one call
//! into a single row and expands rows back into the exact original chunks.
//!
//! Two properties the encoder is built around:
//!
//! - **Members are never joined.** Token boundaries are evidence — where the
//!   model paused, which span came back from cache. Keeping each fragment a
//!   separate array element is also what makes packing correct without tracking
//!   content-block indices: even a run that spans a block boundary expands to
//!   the identical chunk sequence, because there is no per-chunk index to
//!   restore.
//! - **Anything unrecognized passes through verbatim.** An event the encoder
//!   cannot classify costs compression, never data.

use base::interface::model::ModelEvent;

use super::format::{ChunkRecord, Record, TextRun, ToolArgsRun};

/// Below this a row's envelope rivals the lines it replaces. A format constant,
/// not a tunable: both layouts expand identically, so changing it never
/// invalidates a stored recording.
const MIN_RUN: usize = 3;

/// Runs split here instead of growing without bound. The packer holds pending
/// members in memory until a run ends, so an uncapped run would mean a whole
/// content block buffered — and lost if the process died mid-stream, which is
/// exactly when the recording matters most.
const MAX_RUN: usize = 64;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RunKind {
    Text,
    Thinking,
    ToolArgs,
}

fn classify(chunk: &ChunkRecord) -> Option<RunKind> {
    match &chunk.chunk {
        ModelEvent::TextDelta { .. } => Some(RunKind::Text),
        ModelEvent::ThinkingDelta { .. } => Some(RunKind::Thinking),
        ModelEvent::ToolArgsDelta { .. } => Some(RunKind::ToolArgs),
        _ => None,
    }
}

fn tool_args_id(chunk: &ChunkRecord) -> Option<&str> {
    match &chunk.chunk {
        ModelEvent::ToolArgsDelta { id, .. } => Some(id),
        _ => None,
    }
}

fn payload(chunk: &ChunkRecord) -> String {
    match &chunk.chunk {
        ModelEvent::TextDelta { text } | ModelEvent::ThinkingDelta { text } => text.clone(),
        ModelEvent::ToolArgsDelta { partial_json, .. } => partial_json.clone(),
        _ => String::new(),
    }
}

/// Whether `next` extends a run ending at `prev`.
///
/// Adjacency is arrival order into a packer, which only ever sees one call's
/// stream — deliberately *not* `seq` adjacency. `seq` is a line number handed
/// out by the writer, and when a parent and its sub-agent record into the same
/// file their line numbers interleave; requiring them to be consecutive meant
/// every run broke after one member and packing silently stopped working in
/// exactly the case it was needed for. Line numbers are assigned to a run when
/// it is written ([`stamp_seqs`]), so a row's block stays contiguous regardless.
fn continues(prev: &ChunkRecord, next: &ChunkRecord, kind: RunKind) -> bool {
    if next.call != prev.call {
        return false;
    }
    if kind == RunKind::ToolArgs && tool_args_id(prev) != tool_args_id(next) {
        return false;
    }
    true
}

/// How many chunks a record stands for — its share of the line numbering.
pub fn seq_width(record: &Record) -> usize {
    match record {
        Record::TextChunks(run) | Record::ThinkingChunks(run) => run.texts.len(),
        Record::ToolArgsChunks(run) => run.args.len(),
        _ => 1,
    }
}

/// Assign `record` a block of line numbers starting at `from`.
///
/// Called when the record is about to be appended rather than when its chunks
/// arrived, so a packed row always occupies a contiguous block and `seq0 + k`
/// keeps naming the k-th member even with another stream writing in between.
pub fn stamp_seqs(record: &mut Record, from: u64) {
    match record {
        Record::Chunk(c) => c.seq = from,
        Record::TextChunks(run) | Record::ThinkingChunks(run) => run.seq0 = from,
        Record::ToolArgsChunks(run) => run.seq0 = from,
        _ => {}
    }
}

fn build_row(kind: RunKind, run: &[ChunkRecord]) -> Record {
    let first = &run[0];
    let dt: Vec<i64> = run
        .windows(2)
        .map(|w| w[1].ts as i64 - w[0].ts as i64)
        .collect();
    let members: Vec<String> = run.iter().map(payload).collect();
    match kind {
        RunKind::ToolArgs => Record::ToolArgsChunks(ToolArgsRun {
            seq0: first.seq,
            ts0: first.ts,
            call: first.call,
            id: tool_args_id(first).unwrap_or_default().to_string(),
            args: members,
            dt,
        }),
        RunKind::Text | RunKind::Thinking => {
            let run = TextRun {
                seq0: first.seq,
                ts0: first.ts,
                call: first.call,
                texts: members,
                dt,
            };
            if kind == RunKind::Text {
                Record::TextChunks(run)
            } else {
                Record::ThinkingChunks(run)
            }
        }
    }
}

/// Incremental packer: feed chunks in arrival order, write out what it returns.
///
/// Holding a partial run is what buys the compression, so the packer trades a
/// bounded write delay (at most [`MAX_RUN`] members) for it. [`RunPacker::flush`]
/// must be called at the end of a call's stream — otherwise its tail sits in
/// the buffer and never reaches the file.
#[derive(Debug, Default)]
pub struct RunPacker {
    pending: Vec<ChunkRecord>,
    kind: Option<RunKind>,
}

impl RunPacker {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn push(&mut self, chunk: ChunkRecord) -> Vec<Record> {
        let Some(kind) = classify(&chunk) else {
            let mut out = self.flush();
            out.push(Record::Chunk(chunk));
            return out;
        };

        let extends = self.kind == Some(kind)
            && self
                .pending
                .last()
                .is_some_and(|prev| continues(prev, &chunk, kind));

        if !extends {
            let out = self.flush();
            self.kind = Some(kind);
            self.pending.push(chunk);
            return out;
        }

        self.pending.push(chunk);
        if self.pending.len() >= MAX_RUN {
            return self.flush();
        }
        Vec::new()
    }

    pub fn flush(&mut self) -> Vec<Record> {
        let kind = self.kind.take();
        let run = std::mem::take(&mut self.pending);
        match kind {
            Some(kind) if run.len() >= MIN_RUN => vec![build_row(kind, &run)],
            _ => run.into_iter().map(Record::Chunk).collect(),
        }
    }
}

/// Pack a complete batch. Equivalent to feeding every member to a
/// [`RunPacker`] and flushing.
pub fn pack(chunks: Vec<ChunkRecord>) -> Vec<Record> {
    let mut packer = RunPacker::new();
    let mut out = Vec::new();
    for chunk in chunks {
        out.extend(packer.push(chunk));
    }
    out.extend(packer.flush());
    out
}

#[derive(Debug, thiserror::Error, PartialEq)]
pub enum PackError {
    #[error("{tag} row: dt has {dt} gaps for {members} members (expected {expected})")]
    DtArity {
        tag: &'static str,
        dt: usize,
        members: usize,
        expected: usize,
    },
    #[error("{tag} row: empty member list")]
    Empty { tag: &'static str },
    #[error("{tag} row: member timestamp left the representable range")]
    TimeRange { tag: &'static str },
}

fn expand_members(
    tag: &'static str,
    members: &[String],
    dt: &[i64],
    seq0: u64,
    ts0: u64,
    call: u64,
    make: impl Fn(&str) -> ModelEvent,
) -> Result<Vec<ChunkRecord>, PackError> {
    if members.is_empty() {
        return Err(PackError::Empty { tag });
    }
    if dt.len() != members.len() - 1 {
        return Err(PackError::DtArity {
            tag,
            dt: dt.len(),
            members: members.len(),
            expected: members.len() - 1,
        });
    }
    let mut out = Vec::with_capacity(members.len());
    let mut ts = ts0 as i64;
    for (i, member) in members.iter().enumerate() {
        if i > 0 {
            ts += dt[i - 1];
        }
        if ts < 0 {
            return Err(PackError::TimeRange { tag });
        }
        out.push(ChunkRecord {
            seq: seq0 + i as u64,
            ts: ts as u64,
            call,
            chunk: make(member),
        });
    }
    Ok(out)
}

pub fn expand_text_run(run: &TextRun, thinking: bool) -> Result<Vec<ChunkRecord>, PackError> {
    let tag = if thinking {
        "thinking_chunks"
    } else {
        "text_chunks"
    };
    expand_members(tag, &run.texts, &run.dt, run.seq0, run.ts0, run.call, |t| {
        if thinking {
            ModelEvent::ThinkingDelta { text: t.into() }
        } else {
            ModelEvent::TextDelta { text: t.into() }
        }
    })
}

pub fn expand_tool_args_run(run: &ToolArgsRun) -> Result<Vec<ChunkRecord>, PackError> {
    expand_members(
        "tool_args_chunks",
        &run.args,
        &run.dt,
        run.seq0,
        run.ts0,
        run.call,
        |a| ModelEvent::ToolArgsDelta {
            id: run.id.clone(),
            partial_json: a.into(),
        },
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use base::interface::model::Usage;

    fn text(seq: u64, ts: u64, s: &str) -> ChunkRecord {
        ChunkRecord {
            seq,
            ts,
            call: 0,
            chunk: ModelEvent::TextDelta { text: s.into() },
        }
    }

    fn args(seq: u64, ts: u64, id: &str, s: &str) -> ChunkRecord {
        ChunkRecord {
            seq,
            ts,
            call: 0,
            chunk: ModelEvent::ToolArgsDelta {
                id: id.into(),
                partial_json: s.into(),
            },
        }
    }

    fn expand_all(records: Vec<Record>) -> Vec<ChunkRecord> {
        let mut out = Vec::new();
        for record in records {
            match record {
                Record::Chunk(c) => out.push(c),
                Record::TextChunks(r) => out.extend(expand_text_run(&r, false).unwrap()),
                Record::ThinkingChunks(r) => out.extend(expand_text_run(&r, true).unwrap()),
                Record::ToolArgsChunks(r) => out.extend(expand_tool_args_run(&r).unwrap()),
                other => panic!("unexpected record: {other:?}"),
            }
        }
        out
    }

    fn assert_round_trip(chunks: Vec<ChunkRecord>) {
        let expected = format!("{chunks:?}");
        let packed = pack(chunks);
        assert_eq!(format!("{:?}", expand_all(packed)), expected);
    }

    #[test]
    fn a_run_at_the_minimum_packs_into_one_row() {
        let chunks = vec![text(0, 100, "a"), text(1, 112, "b"), text(2, 120, "c")];
        let packed = pack(chunks.clone());
        assert_eq!(packed.len(), 1);
        assert!(matches!(packed[0], Record::TextChunks(_)));
        assert_round_trip(chunks);
    }

    #[test]
    fn a_run_below_the_minimum_stays_one_line_each() {
        let chunks = vec![text(0, 100, "a"), text(1, 112, "b")];
        let packed = pack(chunks.clone());
        assert_eq!(packed.len(), 2);
        assert!(packed.iter().all(|r| matches!(r, Record::Chunk(_))));
        assert_round_trip(chunks);
    }

    #[test]
    fn token_boundaries_survive_packing() {
        let chunks = vec![
            text(0, 100, "Hel"),
            text(1, 112, "lo"),
            text(2, 120, " wor"),
            text(3, 135, "ld"),
        ];
        let packed = pack(chunks);
        let Record::TextChunks(run) = &packed[0] else {
            panic!("expected a packed run")
        };
        assert_eq!(run.texts, vec!["Hel", "lo", " wor", "ld"]);
        assert_eq!(run.dt, vec![12, 8, 15]);
    }

    #[test]
    fn a_non_delta_event_breaks_the_run() {
        let chunks = vec![
            text(0, 100, "a"),
            text(1, 110, "b"),
            text(2, 120, "c"),
            ChunkRecord {
                seq: 3,
                ts: 130,
                call: 0,
                chunk: ModelEvent::ContentBlockStop { index: 0 },
            },
            text(4, 140, "d"),
            text(5, 150, "e"),
            text(6, 160, "f"),
        ];
        let packed = pack(chunks.clone());
        assert_eq!(packed.len(), 3);
        assert_round_trip(chunks);
    }

    /// A gap in line numbers means another stream wrote in between, not that
    /// this call's chunks stopped being consecutive. Breaking the run there is
    /// what made packing collapse whenever a parent and a sub-agent shared a
    /// recording — the case packing matters most for.
    #[test]
    fn a_gap_in_line_numbers_does_not_break_the_run() {
        let chunks = vec![text(0, 100, "a"), text(1, 110, "b"), text(9, 120, "c")];
        let packed = pack(chunks);
        assert_eq!(packed.len(), 1, "got {packed:?}");
    }

    /// The row's line numbers are assigned when it is written, so `seq0 + k`
    /// names the k-th member no matter what the members arrived as.
    #[test]
    fn stamping_renumbers_a_row_into_one_contiguous_block() {
        let mut packed = pack(vec![
            text(0, 100, "a"),
            text(1, 110, "b"),
            text(9, 120, "c"),
        ]);
        assert_eq!(seq_width(&packed[0]), 3);
        stamp_seqs(&mut packed[0], 40);
        let Record::TextChunks(run) = &packed[0] else {
            panic!("expected a packed run, got {packed:?}")
        };
        let expanded = expand_text_run(run, false).unwrap();
        assert_eq!(
            expanded.iter().map(|c| c.seq).collect::<Vec<_>>(),
            vec![40, 41, 42]
        );
    }

    #[test]
    fn a_different_call_breaks_the_run() {
        let mut chunks = vec![text(0, 100, "a"), text(1, 110, "b"), text(2, 120, "c")];
        chunks[2].call = 1;
        assert_round_trip(chunks);
    }

    #[test]
    fn tool_args_with_a_different_id_never_share_a_run() {
        let chunks = vec![
            args(0, 100, "t1", "{\"a\":"),
            args(1, 110, "t1", "1}"),
            args(2, 120, "t2", "{\"b\":"),
            args(3, 130, "t2", "2}"),
        ];
        let packed = pack(chunks.clone());
        assert!(packed
            .iter()
            .all(|r| matches!(r, Record::Chunk(_) | Record::ToolArgsChunks(_))));
        assert_round_trip(chunks);
    }

    #[test]
    fn tool_args_run_round_trips_with_its_id() {
        let chunks = vec![
            args(0, 100, "t1", "{\"path\":"),
            args(1, 110, "t1", "\"/tmp"),
            args(2, 120, "t1", "/x\"}"),
        ];
        let packed = pack(chunks.clone());
        assert_eq!(packed.len(), 1);
        assert_round_trip(chunks);
    }

    #[test]
    fn a_run_longer_than_the_cap_splits_and_still_round_trips() {
        let chunks: Vec<_> = (0..MAX_RUN as u64 * 2 + 5)
            .map(|i| text(i, 100 + i * 3, &format!("t{i}")))
            .collect();
        let packed = pack(chunks.clone());
        assert!(packed.len() > 1);
        assert_round_trip(chunks);
    }

    #[test]
    fn a_backwards_clock_step_round_trips() {
        let chunks = vec![text(0, 200, "a"), text(1, 180, "b"), text(2, 210, "c")];
        let packed = pack(chunks.clone());
        let Record::TextChunks(run) = &packed[0] else {
            panic!("expected a packed run")
        };
        assert_eq!(run.dt, vec![-20, 30]);
        assert_round_trip(chunks);
    }

    #[test]
    fn mixed_event_kinds_round_trip_in_order() {
        let chunks = vec![
            ChunkRecord {
                seq: 0,
                ts: 100,
                call: 0,
                chunk: ModelEvent::ThinkingDelta { text: "hm".into() },
            },
            ChunkRecord {
                seq: 1,
                ts: 110,
                call: 0,
                chunk: ModelEvent::ThinkingDelta { text: "mm".into() },
            },
            ChunkRecord {
                seq: 2,
                ts: 120,
                call: 0,
                chunk: ModelEvent::ThinkingDelta { text: "!".into() },
            },
            ChunkRecord {
                seq: 3,
                ts: 130,
                call: 0,
                chunk: ModelEvent::ThinkingSignature {
                    signature: "s".into(),
                },
            },
            text(4, 140, "a"),
            text(5, 150, "b"),
            text(6, 160, "c"),
            ChunkRecord {
                seq: 7,
                ts: 170,
                call: 0,
                chunk: ModelEvent::EndTurn {
                    stop_reason: "end_turn".into(),
                    usage: Usage {
                        input_tokens: 1,
                        output_tokens: 2,
                        ..Default::default()
                    },
                },
            },
        ];
        assert_round_trip(chunks);
    }

    #[test]
    fn a_malformed_row_fails_loudly_instead_of_dropping_the_run() {
        let run = TextRun {
            seq0: 0,
            ts0: 100,
            call: 0,
            texts: vec!["a".into(), "b".into(), "c".into()],
            dt: vec![10],
        };
        assert_eq!(
            expand_text_run(&run, false).unwrap_err(),
            PackError::DtArity {
                tag: "text_chunks",
                dt: 1,
                members: 3,
                expected: 2,
            }
        );
    }

    #[test]
    fn an_empty_row_fails_loudly() {
        let run = TextRun {
            seq0: 0,
            ts0: 100,
            call: 0,
            texts: vec![],
            dt: vec![],
        };
        assert_eq!(
            expand_text_run(&run, false).unwrap_err(),
            PackError::Empty { tag: "text_chunks" }
        );
    }
}