magi-code 0.96.1

Repository-aware CLI coding agent for terminal work
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
use super::{CodecReport, FileAggregate, MAX_JSONL_LINES, u64_from_usize};
use crate::{
    context::count_supported_provider_conversation_item_tokens,
    providers::{OPENAI_CODEX_PROVIDER, ProviderConversationItem},
    sessions::SessionEvent,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;

const MAX_CANDIDATE_BYTES: usize = 1024 * 1024;
const MAX_TEMPLATE_LINE_BYTES: usize = 4 * 1024;
const MIN_TEMPLATE_LITERAL_BYTES: usize = 4;
const LINE_RLE_INSTRUCTION: &str =
    "Concatenate items in order; emit raw once; repeat text count times.";
const TEMPLATE_INSTRUCTION: &str =
    "Concatenate items in order; emit raw once; emit prefix, each value, and suffix.";

#[derive(Debug)]
struct ExtractedToolResult {
    call_id: String,
    content: String,
    legacy: bool,
}

#[derive(Debug)]
struct CandidateBuild {
    output: Option<String>,
    limit_hit: bool,
}

#[derive(Debug)]
struct CandidateEvaluation {
    validation_passed: bool,
    tokens: Option<u64>,
    limit_hit: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LineEndingClass {
    Lf,
    CrLf,
    NoNewline,
}

impl LineEndingClass {
    const fn as_str(self) -> &'static str {
        match self {
            Self::Lf => "lf",
            Self::CrLf => "crlf",
            Self::NoNewline => "none",
        }
    }

    fn from_str(value: &str) -> Option<Self> {
        match value {
            "lf" => Some(Self::Lf),
            "crlf" => Some(Self::CrLf),
            "none" => Some(Self::NoNewline),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct LineRleEnvelope {
    marker: String,
    instruction: String,
    items: Vec<LineRleItem>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", deny_unknown_fields)]
enum LineRleItem {
    #[serde(rename = "raw")]
    Raw { text: String },
    #[serde(rename = "repeat")]
    Repeat { text: String, count: u32 },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct TemplateEnvelope {
    marker: String,
    instruction: String,
    items: Vec<TemplateItem>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", deny_unknown_fields)]
enum TemplateItem {
    #[serde(rename = "raw")]
    Raw { text: String },
    #[serde(rename = "template")]
    Template {
        prefix: String,
        suffix: String,
        ending: String,
        values: Vec<String>,
    },
}

#[derive(Debug, Clone)]
struct TemplatePattern {
    prefix: String,
    suffix: String,
    ending: LineEndingClass,
}

pub(super) fn process_jsonl_line(line: &[u8], model: &str, aggregate: &mut FileAggregate) {
    let line = line.strip_suffix(b"\n").unwrap_or(line);
    let line = line.strip_suffix(b"\r").unwrap_or(line);
    let event = match parse_session_event(line) {
        Some(event) => event,
        None => {
            aggregate.jsonl.malformed = aggregate.jsonl.malformed.saturating_add(1);
            return;
        }
    };
    aggregate.jsonl.valid_events = aggregate.jsonl.valid_events.saturating_add(1);
    if event.event_type != "tool_result" {
        return;
    }
    aggregate.tool_results.found = aggregate.tool_results.found.saturating_add(1);
    let Some(extracted) = extract_tool_result(&event) else {
        aggregate.tool_results.invalid = aggregate.tool_results.invalid.saturating_add(1);
        return;
    };
    if extracted.legacy {
        aggregate.tool_results.legacy = aggregate.tool_results.legacy.saturating_add(1);
    }
    if extracted.content.is_empty() {
        aggregate.tool_results.empty = aggregate.tool_results.empty.saturating_add(1);
    }
    measure_tool_result(extracted, model, aggregate);
}

/// Parses each JSONL line with the canonical persisted session-event type.
/// Serde accepts additive top-level fields, rejects duplicate fields, and
/// preserves the same required-field behavior as session replay.
fn parse_session_event(line: &[u8]) -> Option<SessionEvent> {
    serde_json::from_slice::<SessionEvent>(line).ok()
}

fn extract_tool_result(event: &SessionEvent) -> Option<ExtractedToolResult> {
    if event.event_type != "tool_result" {
        return None;
    }
    let payload = event.payload.as_object()?;
    let result = payload.get("result")?.as_object()?;
    let call_id = result
        .get("call_id")
        .or_else(|| payload.get("call_id"))
        .and_then(Value::as_str)?;
    if call_id.is_empty() {
        return None;
    }
    let tool_name = result
        .get("tool_name")
        .or_else(|| payload.get("tool_name"))
        .and_then(Value::as_str)?;
    if tool_name.is_empty() {
        return None;
    }
    let _success = result.get("success").and_then(Value::as_bool)?;
    let (content, legacy) = match (result.get("content"), result.get("output")) {
        (Some(content), None) => (content.as_str()?.to_string(), false),
        (None, Some(output)) => (output.as_str()?.to_string(), true),
        _ => return None,
    };
    Some(ExtractedToolResult {
        call_id: call_id.to_string(),
        content,
        legacy,
    })
}

fn measure_tool_result(extracted: ExtractedToolResult, model: &str, aggregate: &mut FileAggregate) {
    if extracted.content.len() > MAX_CANDIDATE_BYTES {
        aggregate.tool_results.skipped_limit =
            aggregate.tool_results.skipped_limit.saturating_add(1);
        aggregate.limit_hit = true;
        return;
    }
    let baseline_item = codex_responses_tool_result_item(&extracted.call_id, &extracted.content);
    let Some(baseline_tokens) = count_item_tokens(model, &baseline_item) else {
        // The model was validated before discovery. If a future tokenizer
        // lookup still fails, fail closed without misreporting a result-cap
        // skip.
        return;
    };
    aggregate.tool_results.measured = aggregate.tool_results.measured.saturating_add(1);
    aggregate.baseline.bytes = aggregate
        .baseline
        .bytes
        .saturating_add(u64_from_usize(extracted.content.len()));
    aggregate.baseline.tokens = aggregate.baseline.tokens.saturating_add(baseline_tokens);

    let json = evaluate_candidate(
        build_json_minification(&extracted.content),
        &baseline_item,
        model,
    );
    let rle = evaluate_candidate(build_line_rle(&extracted.content), &baseline_item, model);
    let template = evaluate_candidate(
        build_template_folding(&extracted.content),
        &baseline_item,
        model,
    );
    aggregate.limit_hit |= json.limit_hit || rle.limit_hit || template.limit_hit;
    record_codec_evaluation(
        &mut aggregate.codecs.json_minification,
        &json,
        baseline_tokens,
    );
    record_codec_evaluation(&mut aggregate.codecs.line_rle, &rle, baseline_tokens);
    record_codec_evaluation(
        &mut aggregate.codecs.template_folding,
        &template,
        baseline_tokens,
    );

    let candidates = [json.tokens, rle.tokens, template.tokens];
    let selected = select_unique_best(&candidates, baseline_tokens);
    let selected_tokens = selected
        .and_then(|index| candidates[index])
        .unwrap_or(baseline_tokens);
    aggregate.portfolio.selected_tokens = aggregate
        .portfolio
        .selected_tokens
        .saturating_add(selected_tokens);
    let saved = baseline_tokens.saturating_sub(selected_tokens);
    aggregate.portfolio.saved_tokens = aggregate.portfolio.saved_tokens.saturating_add(saved);
    if let Some(index) = selected {
        aggregate.portfolio.outputs_transformed =
            aggregate.portfolio.outputs_transformed.saturating_add(1);
        let codec = match index {
            0 => &mut aggregate.codecs.json_minification,
            1 => &mut aggregate.codecs.line_rle,
            _ => &mut aggregate.codecs.template_folding,
        };
        codec.selected = codec.selected.saturating_add(1);
        codec.saved_tokens_when_selected = codec.saved_tokens_when_selected.saturating_add(saved);
    }
}

/// Counts the Codex Responses-style local BPE projection: type, call_id,
/// output, and the standard item framing. It is not a provider billing count.
fn codex_responses_tool_result_item(call_id: &str, output: &str) -> ProviderConversationItem {
    ProviderConversationItem::ResponseItem(serde_json::json!({
        "type": "function_call_output",
        "call_id": call_id,
        "output": output,
    }))
}

fn count_item_tokens(model: &str, item: &ProviderConversationItem) -> Option<u64> {
    count_supported_provider_conversation_item_tokens(OPENAI_CODEX_PROVIDER, model, item)
        .map(u64_from_usize)
}
fn evaluate_candidate(
    candidate: CandidateBuild,
    baseline_item: &ProviderConversationItem,
    model: &str,
) -> CandidateEvaluation {
    let Some(output) = candidate.output else {
        return CandidateEvaluation {
            validation_passed: false,
            tokens: None,
            limit_hit: candidate.limit_hit,
        };
    };
    let candidate_item = item_with_output(baseline_item, &output);
    let tokens = count_item_tokens(model, &candidate_item);
    CandidateEvaluation {
        validation_passed: tokens.is_some(),
        tokens,
        limit_hit: candidate.limit_hit,
    }
}

fn item_with_output(item: &ProviderConversationItem, output: &str) -> ProviderConversationItem {
    let ProviderConversationItem::ResponseItem(value) = item else {
        return item.clone();
    };
    let mut value = value.clone();
    let Some(object) = value.as_object_mut() else {
        return item.clone();
    };
    object.insert("output".to_string(), Value::String(output.to_string()));
    ProviderConversationItem::ResponseItem(value)
}

fn record_codec_evaluation(
    codec: &mut CodecReport,
    evaluation: &CandidateEvaluation,
    baseline_tokens: u64,
) {
    codec.attempted = codec.attempted.saturating_add(1);
    if !evaluation.validation_passed {
        return;
    }
    codec.applicable_validation_passed = codec.applicable_validation_passed.saturating_add(1);
    codec.baseline_tokens_when_applicable = codec
        .baseline_tokens_when_applicable
        .saturating_add(baseline_tokens);
    let Some(tokens) = evaluation.tokens else {
        return;
    };
    codec.candidate_tokens_when_applicable = codec
        .candidate_tokens_when_applicable
        .saturating_add(tokens);
    if tokens < baseline_tokens {
        codec.strict_improvements = codec.strict_improvements.saturating_add(1);
        codec.saved_tokens_when_strict_improvement = codec
            .saved_tokens_when_strict_improvement
            .saturating_add(baseline_tokens.saturating_sub(tokens));
    }
}

fn select_unique_best(candidate_tokens: &[Option<u64>], baseline: u64) -> Option<usize> {
    let mut best: Option<(usize, u64)> = None;
    let mut tied = false;
    for (index, candidate) in candidate_tokens.iter().enumerate() {
        let Some(tokens) = candidate else {
            continue;
        };
        if *tokens >= baseline {
            continue;
        }
        match best {
            None => {
                best = Some((index, *tokens));
                tied = false;
            }
            Some((_, best_tokens)) if *tokens < best_tokens => {
                best = Some((index, *tokens));
                tied = false;
            }
            Some((_, best_tokens)) if *tokens == best_tokens => {
                tied = true;
            }
            Some(_) => {}
        }
    }
    if tied {
        None
    } else {
        best.map(|(index, _)| index)
    }
}

fn build_json_minification(input: &str) -> CandidateBuild {
    if input.len() > MAX_CANDIDATE_BYTES {
        return CandidateBuild {
            output: None,
            limit_hit: true,
        };
    }
    let Some(()) = validate_complete_json(input) else {
        return CandidateBuild {
            output: None,
            limit_hit: false,
        };
    };
    let mut output = Vec::with_capacity(input.len());
    let mut in_string = false;
    let mut escaped = false;
    for byte in input.bytes() {
        if in_string {
            output.push(byte);
            if escaped {
                escaped = false;
            } else if byte == b'\\' {
                escaped = true;
            } else if byte == b'"' {
                in_string = false;
            }
            continue;
        }
        if byte == b'"' {
            in_string = true;
            output.push(byte);
        } else if !matches!(byte, b' ' | b'\t' | b'\n' | b'\r') {
            output.push(byte);
        }
    }
    let Ok(output) = String::from_utf8(output) else {
        return CandidateBuild {
            output: None,
            limit_hit: false,
        };
    };
    if validate_complete_json(&output).is_none() {
        return CandidateBuild {
            output: None,
            limit_hit: false,
        };
    }
    CandidateBuild {
        output: Some(output),
        limit_hit: false,
    }
}

fn validate_complete_json(input: &str) -> Option<()> {
    use serde::Deserialize;
    use serde::de::IgnoredAny;
    let mut deserializer = serde_json::Deserializer::from_str(input);
    IgnoredAny::deserialize(&mut deserializer).ok()?;
    deserializer.end().ok()?;
    Some(())
}

fn build_line_rle(input: &str) -> CandidateBuild {
    if input.len() > MAX_CANDIDATE_BYTES {
        return CandidateBuild {
            output: None,
            limit_hit: true,
        };
    }
    let chunks = split_lf_chunks(input);
    if chunks.len() > MAX_JSONL_LINES {
        return CandidateBuild {
            output: None,
            limit_hit: true,
        };
    }
    let mut items = Vec::new();
    let mut raw = String::new();
    let mut index = 0;
    let mut has_run = false;
    while index < chunks.len() {
        let mut end = index.saturating_add(1);
        while end < chunks.len() && chunks[end] == chunks[index] {
            end = end.saturating_add(1);
        }
        let run_length = end.saturating_sub(index);
        if run_length >= 2 {
            flush_rle_raw(&mut items, &mut raw);
            let Some(count) = u32::try_from(run_length).ok() else {
                return CandidateBuild {
                    output: None,
                    limit_hit: true,
                };
            };
            items.push(LineRleItem::Repeat {
                text: chunks[index].to_string(),
                count,
            });
            has_run = true;
        } else {
            raw.push_str(chunks[index]);
        }
        index = end;
    }
    flush_rle_raw(&mut items, &mut raw);
    if !has_run {
        return CandidateBuild {
            output: None,
            limit_hit: false,
        };
    }
    let envelope = LineRleEnvelope {
        marker: "magi-line-rle-v0".to_string(),
        instruction: LINE_RLE_INSTRUCTION.to_string(),
        items,
    };
    let Ok(encoded) = serde_json::to_string(&envelope) else {
        return CandidateBuild {
            output: None,
            limit_hit: true,
        };
    };
    if encoded.len() > MAX_CANDIDATE_BYTES || decode_line_rle(&encoded).as_deref() != Some(input) {
        return CandidateBuild {
            output: None,
            limit_hit: encoded.len() > MAX_CANDIDATE_BYTES,
        };
    }
    CandidateBuild {
        output: Some(encoded),
        limit_hit: false,
    }
}

fn flush_rle_raw(items: &mut Vec<LineRleItem>, raw: &mut String) {
    if raw.is_empty() {
        return;
    }
    items.push(LineRleItem::Raw {
        text: std::mem::take(raw),
    });
}

fn split_lf_chunks(input: &str) -> Vec<&str> {
    let mut chunks = Vec::new();
    let mut start = 0;
    for (index, byte) in input.bytes().enumerate() {
        if byte == b'\n' {
            chunks.push(&input[start..index.saturating_add(1)]);
            start = index.saturating_add(1);
        }
    }
    if start < input.len() {
        chunks.push(&input[start..]);
    }
    chunks
}

fn decode_line_rle(encoded: &str) -> Option<String> {
    if encoded.len() > MAX_CANDIDATE_BYTES {
        return None;
    }
    let value = serde_json::from_str::<Value>(encoded).ok()?;
    validate_exact_object_keys(value.as_object()?, &["marker", "instruction", "items"])?;
    let items = value.get("items")?.as_array()?;
    if items.is_empty()
        || items
            .iter()
            .any(|item| validate_line_rle_item(item).is_none())
    {
        return None;
    }
    let envelope = serde_json::from_value::<LineRleEnvelope>(value).ok()?;
    if envelope.marker != "magi-line-rle-v0" || envelope.instruction != LINE_RLE_INSTRUCTION {
        return None;
    }
    let canonical = serde_json::to_string(&envelope).ok()?;
    if canonical != encoded {
        return None;
    }
    let mut output = String::new();
    let mut line_count = 0usize;
    let mut previous_was_raw = false;
    for item in envelope.items {
        match item {
            LineRleItem::Raw { text } => {
                if text.is_empty() || previous_was_raw {
                    return None;
                }
                line_count = line_count.checked_add(split_lf_chunks(&text).len())?;
                if line_count > MAX_JSONL_LINES {
                    return None;
                }
                push_bounded(&mut output, &text, MAX_CANDIDATE_BYTES)?;
                previous_was_raw = true;
            }
            LineRleItem::Repeat { text, count } => {
                if text.is_empty()
                    || !text.ends_with('\n')
                    || split_lf_chunks(&text).len() != 1
                    || count < 2
                {
                    return None;
                }
                let repeat_count = usize::try_from(count).ok()?;
                line_count = line_count.checked_add(repeat_count)?;
                if line_count > MAX_JSONL_LINES {
                    return None;
                }
                for _ in 0..repeat_count {
                    push_bounded(&mut output, &text, MAX_CANDIDATE_BYTES)?;
                }
                previous_was_raw = false;
            }
        }
    }
    Some(output)
}

fn build_template_folding(input: &str) -> CandidateBuild {
    if input.len() > MAX_CANDIDATE_BYTES {
        return CandidateBuild {
            output: None,
            limit_hit: true,
        };
    }
    let chunks = split_lf_chunks(input);
    if chunks.len() > MAX_JSONL_LINES
        || chunks
            .iter()
            .any(|chunk| chunk.len() > MAX_TEMPLATE_LINE_BYTES)
    {
        return CandidateBuild {
            output: None,
            limit_hit: true,
        };
    }
    let mut items = Vec::new();
    let mut raw = String::new();
    let mut index = 0;
    let mut has_template = false;
    while index < chunks.len() {
        if index.saturating_add(1) < chunks.len()
            && let Some((pattern, first, second)) =
                derive_template_pattern(chunks[index], chunks[index.saturating_add(1)])
        {
            let mut values = vec![first, second];
            let mut end = index.saturating_add(2);
            while end < chunks.len() {
                let Some(value) = template_value(&pattern, chunks[end]) else {
                    break;
                };
                values.push(value);
                end = end.saturating_add(1);
            }
            if values.len() >= 2 {
                flush_template_raw(&mut items, &mut raw);
                items.push(TemplateItem::Template {
                    prefix: pattern.prefix,
                    suffix: pattern.suffix,
                    ending: pattern.ending.as_str().to_string(),
                    values,
                });
                has_template = true;
                index = end;
                continue;
            }
        }
        raw.push_str(chunks[index]);
        index = index.saturating_add(1);
    }
    flush_template_raw(&mut items, &mut raw);
    if !has_template {
        return CandidateBuild {
            output: None,
            limit_hit: false,
        };
    }
    let envelope = TemplateEnvelope {
        marker: "magi-line-template-v0".to_string(),
        instruction: TEMPLATE_INSTRUCTION.to_string(),
        items,
    };
    let Ok(encoded) = serde_json::to_string(&envelope) else {
        return CandidateBuild {
            output: None,
            limit_hit: true,
        };
    };
    if encoded.len() > MAX_CANDIDATE_BYTES || decode_template(&encoded).as_deref() != Some(input) {
        return CandidateBuild {
            output: None,
            limit_hit: encoded.len() > MAX_CANDIDATE_BYTES,
        };
    }
    CandidateBuild {
        output: Some(encoded),
        limit_hit: false,
    }
}

fn flush_template_raw(items: &mut Vec<TemplateItem>, raw: &mut String) {
    if raw.is_empty() {
        return;
    }
    items.push(TemplateItem::Raw {
        text: std::mem::take(raw),
    });
}

fn derive_template_pattern(first: &str, second: &str) -> Option<(TemplatePattern, String, String)> {
    if first == second
        || first.len() > MAX_TEMPLATE_LINE_BYTES
        || second.len() > MAX_TEMPLATE_LINE_BYTES
        || ending_class(first) != ending_class(second)
    {
        return None;
    }
    let ending = ending_class(first);
    let min_len = first.len().min(second.len());
    let mut prefix = first
        .bytes()
        .zip(second.bytes())
        .take_while(|(left, right)| left == right)
        .count();
    while prefix > 0 && (!first.is_char_boundary(prefix) || !second.is_char_boundary(prefix)) {
        prefix = prefix.saturating_sub(1);
    }
    let mut suffix = 0usize;
    while suffix < min_len
        && first.as_bytes()[first.len().saturating_sub(1).saturating_sub(suffix)]
            == second.as_bytes()[second.len().saturating_sub(1).saturating_sub(suffix)]
    {
        suffix = suffix.saturating_add(1);
    }
    let max_suffix = min_len.saturating_sub(prefix);
    suffix = suffix.min(max_suffix);
    while suffix > 0
        && (!first.is_char_boundary(first.len().saturating_sub(suffix))
            || !second.is_char_boundary(second.len().saturating_sub(suffix)))
    {
        suffix = suffix.saturating_sub(1);
    }
    let first_end = first.len().saturating_sub(suffix);
    let second_end = second.len().saturating_sub(suffix);
    if prefix > first_end || prefix > second_end {
        return None;
    }
    let first_value = &first[prefix..first_end];
    let second_value = &second[prefix..second_end];
    if first_value == second_value
        || prefix.saturating_add(suffix) < MIN_TEMPLATE_LITERAL_BYTES
        || prefix.saturating_add(suffix).saturating_mul(2) < first.len().max(second.len())
    {
        return None;
    }
    Some((
        TemplatePattern {
            prefix: first[..prefix].to_string(),
            suffix: first[first_end..].to_string(),
            ending,
        },
        first_value.to_string(),
        second_value.to_string(),
    ))
}

fn template_value(pattern: &TemplatePattern, line: &str) -> Option<String> {
    if line.len() > MAX_TEMPLATE_LINE_BYTES
        || ending_class(line) != pattern.ending
        || !line.starts_with(&pattern.prefix)
        || !line.ends_with(&pattern.suffix)
    {
        return None;
    }
    let start = pattern.prefix.len();
    let end = line.len().checked_sub(pattern.suffix.len())?;
    if start > end || !line.is_char_boundary(start) || !line.is_char_boundary(end) {
        return None;
    }
    Some(line[start..end].to_string())
}

fn ending_class(line: &str) -> LineEndingClass {
    if line.ends_with("\r\n") {
        LineEndingClass::CrLf
    } else if line.ends_with('\n') {
        LineEndingClass::Lf
    } else {
        LineEndingClass::NoNewline
    }
}

fn is_single_line_chunk(line: &str, ending: LineEndingClass) -> bool {
    match ending {
        LineEndingClass::Lf | LineEndingClass::CrLf => {
            line.ends_with('\n') && split_lf_chunks(line).len() == 1
        }
        LineEndingClass::NoNewline => !line.contains('\n'),
    }
}

fn decode_template(encoded: &str) -> Option<String> {
    if encoded.len() > MAX_CANDIDATE_BYTES {
        return None;
    }
    let value = serde_json::from_str::<Value>(encoded).ok()?;
    validate_exact_object_keys(value.as_object()?, &["marker", "instruction", "items"])?;
    let items = value.get("items")?.as_array()?;
    if items.is_empty()
        || items
            .iter()
            .any(|item| validate_template_item(item).is_none())
    {
        return None;
    }
    let envelope = serde_json::from_value::<TemplateEnvelope>(value).ok()?;
    if envelope.marker != "magi-line-template-v0" || envelope.instruction != TEMPLATE_INSTRUCTION {
        return None;
    }
    let canonical = serde_json::to_string(&envelope).ok()?;
    if canonical != encoded {
        return None;
    }
    let mut output = String::new();
    let mut line_count = 0usize;
    let mut previous_was_raw = false;
    for item in envelope.items {
        match item {
            TemplateItem::Raw { text } => {
                if text.is_empty() || previous_was_raw {
                    return None;
                }
                line_count = line_count.checked_add(split_lf_chunks(&text).len())?;
                if line_count > MAX_JSONL_LINES {
                    return None;
                }
                push_bounded(&mut output, &text, MAX_CANDIDATE_BYTES)?;
                previous_was_raw = true;
            }
            TemplateItem::Template {
                prefix,
                suffix,
                ending,
                values,
            } => {
                let ending = LineEndingClass::from_str(&ending)?;
                if values.len() < 2
                    || prefix.len().saturating_add(suffix.len()) < MIN_TEMPLATE_LITERAL_BYTES
                {
                    return None;
                }
                if !values.windows(2).any(|pair| pair[0] != pair[1]) {
                    return None;
                }
                for value in values {
                    let line = format!("{prefix}{value}{suffix}");
                    if line.len() > MAX_TEMPLATE_LINE_BYTES
                        || ending_class(&line) != ending
                        || !is_single_line_chunk(&line, ending)
                        || !line.starts_with(&prefix)
                        || !line.ends_with(&suffix)
                    {
                        return None;
                    }
                    line_count = line_count.checked_add(1)?;
                    if line_count > MAX_JSONL_LINES {
                        return None;
                    }
                    push_bounded(&mut output, &line, MAX_CANDIDATE_BYTES)?;
                }
                previous_was_raw = false;
            }
        }
    }
    Some(output)
}

fn validate_line_rle_item(value: &Value) -> Option<()> {
    let object = value.as_object()?;
    match object.get("kind")?.as_str()? {
        "raw" => validate_exact_object_keys(object, &["kind", "text"]),
        "repeat" => validate_exact_object_keys(object, &["kind", "text", "count"]),
        _ => None,
    }
}

fn validate_template_item(value: &Value) -> Option<()> {
    let object = value.as_object()?;
    match object.get("kind")?.as_str()? {
        "raw" => validate_exact_object_keys(object, &["kind", "text"]),
        "template" => {
            validate_exact_object_keys(object, &["kind", "prefix", "suffix", "ending", "values"])
        }
        _ => None,
    }
}

fn validate_exact_object_keys(
    object: &serde_json::Map<String, Value>,
    keys: &[&str],
) -> Option<()> {
    if object.len() != keys.len() || keys.iter().any(|key| !object.contains_key(*key)) {
        return None;
    }
    Some(())
}

fn push_bounded(output: &mut String, value: &str, max_bytes: usize) -> Option<()> {
    let next_len = output.len().checked_add(value.len())?;
    if next_len > max_bytes {
        return None;
    }
    output.push_str(value);
    Some(())
}