agent-file-tools 0.55.0

Agent File Tools — tree-sitter powered code analysis for AI agents
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
#![cfg(unix)]

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicI64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;

use aft::github_read::{
    download_github_image_attachments, render_document, sqlite_cache_store, DownloadedGithubImage,
    GithubDocument, GithubDocumentKind, GithubFetchRequest, GithubFetcher, GithubImageDownloader,
    GithubReadClock, GithubReadCompletion, GithubReadEngine, GithubReadError, GithubReadFreshness,
    GithubReadSelector, GithubReadStart, GithubResourceKind, MAX_GITHUB_IMAGE_ATTACHMENTS,
    MAX_GITHUB_IMAGE_ATTACHMENT_BYTES,
};
use parking_lot::Mutex;
use url::Url;

fn enabled_gh_read() -> aft::config::GhReadConfig {
    aft::config::GhReadConfig { enabled: true }
}

const FIXTURE_DIRECTORY: &str = "tests/fixtures/github_read_cache_attachment";
const RESOURCE: &str = "issue://owner/repo/7";
const PNG_SIGNATURE: &[u8] = &[137, 80, 78, 71, 13, 10, 26, 10];

fn fixture_path(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join(FIXTURE_DIRECTORY)
        .join(name)
}

fn bug_report_fixture() -> GithubDocument {
    serde_json::from_str(
        &fs::read_to_string(fixture_path("bug_report.json"))
            .expect("read GitHub bug-report fixture"),
    )
    .expect("parse GitHub bug-report fixture")
}

struct FixtureClock(AtomicI64);

impl FixtureClock {
    fn new(now_ms: i64) -> Self {
        Self(AtomicI64::new(now_ms))
    }
}

impl GithubReadClock for FixtureClock {
    fn now_ms(&self) -> i64 {
        self.0.load(Ordering::SeqCst)
    }
}

struct ProbeFetcher {
    calls: AtomicUsize,
}

impl ProbeFetcher {
    fn ordinary() -> Self {
        Self {
            calls: AtomicUsize::new(0),
        }
    }

    fn calls(&self) -> usize {
        self.calls.load(Ordering::SeqCst)
    }
}

impl GithubFetcher for ProbeFetcher {
    fn fetch(&self, request: &GithubFetchRequest) -> Result<GithubDocument, GithubReadError> {
        let call = self.calls.fetch_add(1, Ordering::SeqCst) + 1;
        let repository = request.resource.repository.clone().unwrap_or_else(|| {
            let context = request
                .working_directory
                .file_name()
                .and_then(|name| name.to_str())
                .unwrap_or("unresolved");
            format!("owner/{context}")
        });
        let kind = match request.resource.kind {
            GithubResourceKind::Issue => GithubDocumentKind::Issue,
            GithubResourceKind::PullRequest => GithubDocumentKind::PullRequest,
        };
        Ok(GithubDocument {
            repository,
            kind,
            number: request.resource.number,
            title: format!("network revision {call}"),
            state: "OPEN".to_string(),
            body: format!("network body revision {call}"),
            ..GithubDocument::default()
        })
    }
}

struct FailingAfterFirstFetcher(AtomicUsize);

impl GithubFetcher for FailingAfterFirstFetcher {
    fn fetch(&self, request: &GithubFetchRequest) -> Result<GithubDocument, GithubReadError> {
        let call = self.0.fetch_add(1, Ordering::SeqCst) + 1;
        if call > 1 {
            return Err(GithubReadError::FetchFailed(
                "fixture live fetch failed".to_string(),
            ));
        }
        Ok(GithubDocument {
            repository: request
                .resource
                .repository
                .clone()
                .unwrap_or_else(|| "owner/repo".to_string()),
            kind: GithubDocumentKind::Issue,
            number: request.resource.number,
            title: "fallback seed".to_string(),
            state: "OPEN".to_string(),
            body: "cached fallback body".to_string(),
            ..GithubDocument::default()
        })
    }
}

struct StaticFixtureFetcher {
    document: GithubDocument,
    calls: AtomicUsize,
}

impl StaticFixtureFetcher {
    fn new(document: GithubDocument) -> Self {
        Self {
            document,
            calls: AtomicUsize::new(0),
        }
    }

    fn calls(&self) -> usize {
        self.calls.load(Ordering::SeqCst)
    }
}

impl GithubFetcher for StaticFixtureFetcher {
    fn fetch(&self, _request: &GithubFetchRequest) -> Result<GithubDocument, GithubReadError> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        Ok(self.document.clone())
    }
}

struct ScriptedDownloader {
    calls: Mutex<Vec<(String, usize)>>,
    outcomes: BTreeMap<String, Result<Option<DownloadedGithubImage>, String>>,
}

impl ScriptedDownloader {
    fn new(outcomes: BTreeMap<String, Result<Option<DownloadedGithubImage>, String>>) -> Self {
        Self {
            calls: Mutex::new(Vec::new()),
            outcomes,
        }
    }

    fn calls(&self) -> Vec<(String, usize)> {
        self.calls.lock().clone()
    }
}

impl GithubImageDownloader for ScriptedDownloader {
    fn download(
        &self,
        url: &Url,
        maximum_bytes: usize,
    ) -> Result<Option<DownloadedGithubImage>, String> {
        let source = url.to_string();
        self.calls.lock().push((source.clone(), maximum_bytes));
        self.outcomes.get(&source).cloned().unwrap_or(Ok(None))
    }
}

fn successful_download(url: &str, mime: &str, bytes: Vec<u8>) -> DownloadedGithubImage {
    DownloadedGithubImage {
        final_url: Url::parse(url).expect("parse fixture image URL"),
        mime: mime.to_string(),
        bytes,
    }
}

fn png_bytes() -> Vec<u8> {
    PNG_SIGNATURE.to_vec()
}

fn oversized_png() -> Vec<u8> {
    let mut bytes = vec![0; MAX_GITHUB_IMAGE_ATTACHMENT_BYTES + 1];
    bytes[..PNG_SIGNATURE.len()].copy_from_slice(PNG_SIGNATURE);
    bytes
}

fn complete_read(start: GithubReadStart) -> GithubReadCompletion {
    match start {
        GithubReadStart::Immediate(completion) => completion,
        GithubReadStart::Deferred(deferred) => {
            for _ in 0..1_000 {
                if let Some(completion) = deferred.try_complete() {
                    return completion.expect("fixture GitHub read succeeds");
                }
                std::thread::sleep(Duration::from_millis(1));
            }
            panic!("fixture GitHub read did not complete");
        }
    }
}

#[test]
fn sequential_reads_always_fetch_live_and_refresh_the_fallback_copy() {
    let storage = tempfile::tempdir().expect("create cache storage");
    let fetcher = Arc::new(ProbeFetcher::ordinary());
    let engine = GithubReadEngine::new(
        sqlite_cache_store(storage.path().join("aft.db")),
        fetcher.clone(),
        Arc::new(ScriptedDownloader::new(BTreeMap::new())),
        Arc::new(FixtureClock::new(1_000)),
    );

    let first = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                RESOURCE,
                "/fixture/cache",
                "principal:alice",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("start first live read"),
    );
    let second = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                RESOURCE,
                "/fixture/cache",
                "principal:alice",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("start second live read"),
    );

    assert_eq!(first.freshness, GithubReadFreshness::Fetched);
    assert_eq!(second.freshness, GithubReadFreshness::Fetched);
    assert!(second.content.contains("network revision 2"));
    assert_eq!(
        fetcher.calls(),
        2,
        "two sequential reads must issue two live fetches"
    );
}

#[test]
fn live_fetches_keep_authentication_and_unresolved_short_contexts_isolated() {
    let storage = tempfile::tempdir().expect("create cache storage");
    let fetcher = Arc::new(ProbeFetcher::ordinary());
    let engine = GithubReadEngine::new(
        sqlite_cache_store(storage.path().join("aft.db")),
        fetcher.clone(),
        Arc::new(ScriptedDownloader::new(BTreeMap::new())),
        Arc::new(FixtureClock::new(1_000)),
    );

    let alice = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                RESOURCE,
                "/fixture/authentication",
                "principal:alice",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("start Alice read"),
    );
    let bob = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                RESOURCE,
                "/fixture/authentication",
                "principal:bob",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("start Bob read"),
    );
    assert_eq!(fetcher.calls(), 2);
    assert_ne!(alice.content, bob.content);

    let first_short = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                "issue://7",
                "/fixture/worktree-a",
                "principal:alice",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("resolve first short resource"),
    );
    let second_short = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                "issue://7",
                "/fixture/worktree-b",
                "principal:alice",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("resolve second short resource"),
    );
    assert_eq!(fetcher.calls(), 4);
    assert!(first_short.content.contains("Repository: owner/worktree-a"));
    assert!(second_short
        .content
        .contains("Repository: owner/worktree-b"));

    for (resource, working_directory, identity) in [
        (RESOURCE, "/fixture/authentication", "principal:alice"),
        (RESOURCE, "/fixture/authentication", "principal:bob"),
        ("issue://7", "/fixture/worktree-a", "principal:alice"),
        ("issue://7", "/fixture/worktree-b", "principal:alice"),
    ] {
        complete_read(
            engine
                .start_resource(
                    &enabled_gh_read(),
                    resource,
                    working_directory,
                    identity,
                    None,
                    GithubReadSelector::WholeDocument,
                )
                .expect("repeat live read"),
        );
    }
    assert_eq!(
        fetcher.calls(),
        8,
        "cached fallback copies must not suppress live reads"
    );
}

#[test]
fn successful_same_resource_invalidation_removes_the_disclosed_fallback_copy() {
    let storage = tempfile::tempdir().expect("create cache storage");
    let fetcher = Arc::new(FailingAfterFirstFetcher(AtomicUsize::new(0)));
    let engine = GithubReadEngine::new(
        sqlite_cache_store(storage.path().join("aft.db")),
        fetcher.clone(),
        Arc::new(ScriptedDownloader::new(BTreeMap::new())),
        Arc::new(FixtureClock::new(1_234)),
    );

    complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                RESOURCE,
                "/fixture/mutation",
                "principal:alice",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("seed fallback copy"),
    );

    // A failed mutation has no success-only invalidation call, so its fallback
    // must remain available and visibly disclosed when the next live fetch fails.
    let fallback = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                RESOURCE,
                "/fixture/mutation",
                "principal:alice",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("attempt live read after failed mutation"),
    );
    assert_eq!(fallback.freshness, GithubReadFreshness::CachedFallback);
    assert!(fallback.content.starts_with(
        "[cached copy from 1970-01-01T00:00:01.234Z; live fetch failed: fixture live fetch failed]\n"
    ));

    assert_eq!(
        engine
            .invalidate(GithubResourceKind::Issue, "owner/repo", 8, None)
            .expect("invalidate unrelated mutation resource"),
        0
    );
    assert!(complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                RESOURCE,
                "/fixture/mutation",
                "principal:alice",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("unrelated mutation leaves fallback copy"),
    )
    .content
    .starts_with("[cached copy from"));

    assert_eq!(
        engine
            .invalidate(GithubResourceKind::Issue, "owner/repo", 7, None)
            .expect("apply successful same-resource mutation invalidation"),
        1
    );
    let error = match engine
        .start_resource(
            &enabled_gh_read(),
            RESOURCE,
            "/fixture/mutation",
            "principal:alice",
            None,
            GithubReadSelector::WholeDocument,
        )
        .expect("attempt read after matching invalidation")
    {
        GithubReadStart::Deferred(deferred) => {
            let result = (0..1_000)
                .find_map(|_| {
                    let result = deferred.try_complete();
                    if result.is_none() {
                        std::thread::sleep(Duration::from_millis(1));
                    }
                    result
                })
                .expect("GitHub read did not complete");
            result.expect_err("invalidated fallback must not hide fetch failure")
        }
        GithubReadStart::Immediate(_) => panic!("every read must attempt a live fetch"),
    };
    assert_eq!(
        error,
        GithubReadError::FetchFailed("fixture live fetch failed".to_string())
    );
    assert_eq!(fetcher.0.load(Ordering::SeqCst), 4);
}

#[test]
fn vision_capability_preserves_bug_report_text_and_only_downloads_for_explicit_true() {
    let storage = tempfile::tempdir().expect("create cache storage");
    let first = "https://user-images.githubusercontent.com/1234/first.png";
    let second = "https://github.com/user-attachments/files/73/second.png";
    let outcomes = BTreeMap::from([
        (
            first.to_string(),
            Ok(Some(successful_download(first, "image/png", png_bytes()))),
        ),
        (
            second.to_string(),
            Ok(Some(successful_download(second, "image/png", png_bytes()))),
        ),
    ]);
    let fetcher = Arc::new(StaticFixtureFetcher::new(bug_report_fixture()));
    let downloader = Arc::new(ScriptedDownloader::new(outcomes));
    let engine = GithubReadEngine::new(
        sqlite_cache_store(storage.path().join("aft.db")),
        fetcher.clone(),
        downloader.clone(),
        Arc::new(FixtureClock::new(1_000)),
    );

    let missing_capability = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                "issue://cortexkit/aft/73",
                "/fixture/vision",
                "principal:vision",
                None,
                GithubReadSelector::WholeDocument,
            )
            .expect("read fixture without capability"),
    );
    assert!(missing_capability.content.contains(first));
    assert!(missing_capability.content.contains(second));
    assert!(missing_capability.attachments.is_empty());
    assert!(downloader.calls().is_empty());

    let false_capability = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                "issue://cortexkit/aft/73",
                "/fixture/vision",
                "principal:vision",
                Some(false),
                GithubReadSelector::WholeDocument,
            )
            .expect("read fixture with false capability"),
    );
    assert_eq!(false_capability.content, missing_capability.content);
    assert!(false_capability.attachments.is_empty());
    assert!(downloader.calls().is_empty());

    let explicit_vision = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                "issue://cortexkit/aft/73",
                "/fixture/vision",
                "principal:vision",
                Some(true),
                GithubReadSelector::WholeDocument,
            )
            .expect("read fixture with explicit vision capability"),
    );
    assert_eq!(explicit_vision.content, missing_capability.content);
    assert_eq!(
        fetcher.calls(),
        3,
        "each capability mode must fetch live before rendering attachments"
    );
    assert_eq!(
        explicit_vision
            .attachments
            .iter()
            .map(|attachment| attachment.source_url.as_str())
            .collect::<Vec<_>>(),
        [first, second],
        "eligible attachments must follow document order across both allowed host forms"
    );
    assert_eq!(
        downloader.calls(),
        vec![
            (first.to_string(), MAX_GITHUB_IMAGE_ATTACHMENT_BYTES),
            (
                second.to_string(),
                MAX_GITHUB_IMAGE_ATTACHMENT_BYTES - PNG_SIGNATURE.len(),
            ),
        ]
    );
}

#[test]
fn invalid_or_failed_attachment_candidates_leave_the_text_read_complete_without_partial_data() {
    let storage = tempfile::tempdir().expect("create cache storage");
    let unsupported = "https://github.com/user-attachments/files/73/unsupported.svg";
    let redirected = "https://user-images.githubusercontent.com/1234/redirected.png";
    let failed = "https://github.com/user-attachments/files/73/failure.png";
    let oversized = "https://user-images.githubusercontent.com/1234/oversized.png";
    let malformed = "https://github.com:99999/user-attachments/malformed.png";
    let ineligible_host = "https://example.test/bug.png";
    let ineligible_github_path = "https://github.com/cortexkit/aft/blob/main/bug.png";
    let document = GithubDocument {
        repository: "owner/repo".to_string(),
        kind: GithubDocumentKind::Issue,
        number: 7,
        title: "attachment failure fixture".to_string(),
        state: "OPEN".to_string(),
        body: [
            unsupported,
            redirected,
            failed,
            oversized,
            malformed,
            ineligible_host,
            ineligible_github_path,
        ]
        .join("\n"),
        ..GithubDocument::default()
    };
    let outcomes = BTreeMap::from([
        (
            unsupported.to_string(),
            Ok(Some(successful_download(
                unsupported,
                "image/svg+xml",
                png_bytes(),
            ))),
        ),
        (
            redirected.to_string(),
            Ok(Some(successful_download(
                "https://example.test/redirected.png",
                "image/png",
                png_bytes(),
            ))),
        ),
        (
            failed.to_string(),
            Err("fixture image host failed".to_string()),
        ),
        (
            oversized.to_string(),
            Ok(Some(successful_download(
                oversized,
                "image/png",
                oversized_png(),
            ))),
        ),
    ]);
    let downloader = Arc::new(ScriptedDownloader::new(outcomes));
    let engine = GithubReadEngine::new(
        sqlite_cache_store(storage.path().join("aft.db")),
        Arc::new(StaticFixtureFetcher::new(document)),
        downloader.clone(),
        Arc::new(FixtureClock::new(1_000)),
    );

    let completion = complete_read(
        engine
            .start_resource(
                &enabled_gh_read(),
                RESOURCE,
                "/fixture/attachment-failures",
                "principal:vision",
                Some(true),
                GithubReadSelector::WholeDocument,
            )
            .expect("start attachment failure fixture read"),
    );
    assert!(completion.attachments.is_empty());
    for url in [
        unsupported,
        redirected,
        failed,
        oversized,
        malformed,
        ineligible_host,
        ineligible_github_path,
    ] {
        assert!(completion.content.contains(url), "text keeps {url}");
    }
    assert_eq!(
        downloader.calls(),
        vec![
            (unsupported.to_string(), MAX_GITHUB_IMAGE_ATTACHMENT_BYTES),
            (redirected.to_string(), MAX_GITHUB_IMAGE_ATTACHMENT_BYTES),
            (failed.to_string(), MAX_GITHUB_IMAGE_ATTACHMENT_BYTES),
            (oversized.to_string(), MAX_GITHUB_IMAGE_ATTACHMENT_BYTES),
        ],
        "malformed and ineligible URLs must never reach the image network seam"
    );
}

#[test]
fn attachment_count_and_aggregate_byte_budgets_stop_before_exposing_partial_images() {
    let count_urls = (0..=MAX_GITHUB_IMAGE_ATTACHMENTS)
        .map(|index| format!("https://user-images.githubusercontent.com/1234/count-{index}.png"))
        .collect::<Vec<_>>();
    let count_outcomes = count_urls
        .iter()
        .map(|url| {
            (
                url.clone(),
                Ok(Some(successful_download(url, "image/png", png_bytes()))),
            )
        })
        .collect();
    let count_downloader = ScriptedDownloader::new(count_outcomes);
    let count_attachments =
        download_github_image_attachments(&count_urls.join("\n"), &count_downloader);
    assert_eq!(count_attachments.len(), MAX_GITHUB_IMAGE_ATTACHMENTS);
    assert_eq!(
        count_attachments
            .iter()
            .map(|attachment| attachment.source_url.as_str())
            .collect::<Vec<_>>(),
        count_urls[..MAX_GITHUB_IMAGE_ATTACHMENTS]
            .iter()
            .map(String::as_str)
            .collect::<Vec<_>>()
    );
    assert_eq!(count_downloader.calls().len(), MAX_GITHUB_IMAGE_ATTACHMENTS);

    let first = "https://github.com/user-attachments/files/73/almost-full.png";
    let second = "https://user-images.githubusercontent.com/1234/remainder.png";
    let third = "https://github.com/user-attachments/files/73/never-requested.png";
    let mut almost_full_png = vec![0; MAX_GITHUB_IMAGE_ATTACHMENT_BYTES - PNG_SIGNATURE.len()];
    almost_full_png[..PNG_SIGNATURE.len()].copy_from_slice(PNG_SIGNATURE);
    let aggregate_outcomes = BTreeMap::from([
        (
            first.to_string(),
            Ok(Some(successful_download(
                first,
                "image/png",
                almost_full_png,
            ))),
        ),
        (
            second.to_string(),
            Ok(Some(successful_download(second, "image/png", png_bytes()))),
        ),
        (
            third.to_string(),
            Ok(Some(successful_download(third, "image/png", png_bytes()))),
        ),
    ]);
    let aggregate_downloader = ScriptedDownloader::new(aggregate_outcomes);
    let aggregate_attachments = download_github_image_attachments(
        &[first, second, third].join("\n"),
        &aggregate_downloader,
    );
    assert_eq!(aggregate_attachments.len(), 2);
    assert_eq!(
        aggregate_attachments
            .iter()
            .map(|attachment| attachment.bytes.len())
            .sum::<usize>(),
        MAX_GITHUB_IMAGE_ATTACHMENT_BYTES
    );
    assert_eq!(
        aggregate_downloader.calls(),
        vec![
            (first.to_string(), MAX_GITHUB_IMAGE_ATTACHMENT_BYTES),
            (second.to_string(), PNG_SIGNATURE.len()),
        ],
        "an exhausted aggregate budget must skip later downloads instead of exposing partial data"
    );

    let rendered = render_document(&GithubDocument {
        repository: "owner/repo".to_string(),
        kind: GithubDocumentKind::Issue,
        number: 7,
        title: "bounded attachment fixture".to_string(),
        state: "OPEN".to_string(),
        body: [first, second, third].join("\n"),
        ..GithubDocument::default()
    });
    assert!(
        rendered.contains(third),
        "attachment bounds never remove text URLs"
    );
}