mobius 0.16.20

A small, modular Rust framework for building coding 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
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
use std::io::Cursor;
use std::sync::Arc;

use image::metadata::Orientation;
use image::{AnimationDecoder, ImageDecoder, ImageFormat, ImageReader};

use super::{SessionFileOrigin, SessionFileStore};
use crate::protocol::{ImageDetail, ImageReference, ImageRendition, SessionFileReference};
use crate::{Error, Result};

const MAX_PIXELS: u64 = 40_000_000;
const MAX_DECODE_BYTES: u64 = 256 * 1024 * 1024;

crate::embedded_config! {
    copy;
    /// Model presentation dimensions, independent of encoded request admission.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct ImagePresentation {
        /// Longest permitted image side.
        pub max_dimension: u32,
        /// Maximum number of 32 by 32 pixel patches.
        pub max_patches: u32,
    }
    defaults = include_str!("images.toml");
}

impl ImagePresentation {
    /// Whether these pixels fit without resizing.
    #[must_use]
    pub fn fits(self, width: u32, height: u32) -> bool {
        width > 0
            && height > 0
            && width.max(height) <= self.max_dimension
            && u64::from(width.div_ceil(32)) * u64::from(height.div_ceil(32))
                <= u64::from(self.max_patches)
    }

    /// Fits both limits, preserving aspect ratio to pixel rounding without enlargement.
    #[must_use]
    pub fn dimensions(self, width: u32, height: u32) -> (u32, u32) {
        if self.fits(width, height) {
            return (width, height);
        }
        // Integer search avoids rounding a patch grid back over its budget.
        let longest = width.max(height).max(1);
        let scaled = |side: u32| {
            (
                u32::try_from((u64::from(width) * u64::from(side) / u64::from(longest)).max(1))
                    .unwrap_or(width.max(1)),
                u32::try_from((u64::from(height) * u64::from(side) / u64::from(longest)).max(1))
                    .unwrap_or(height.max(1)),
            )
        };
        let (mut low, mut high) = (1, longest.min(self.max_dimension).max(1));
        while low < high {
            let mid = low + (high - low).div_ceil(2);
            let (w, h) = scaled(mid);
            if self.fits(w, h) {
                low = mid;
            } else {
                high = mid - 1;
            }
        }
        scaled(low)
    }
}

impl SessionFileStore {
    /// Sets model presentation policy for this store.
    /// # Errors
    /// Rejects zero or decoder-exceeding dimensions and patch budgets.
    pub fn image_presentation(mut self, policy: ImagePresentation) -> Result<Self> {
        if policy.max_dimension == 0 || policy.max_dimension > 16_384 || policy.max_patches == 0 {
            return Err(Error::Config("invalid image presentation limits".into()));
        }
        self.image_policy = policy;
        Ok(self)
    }

    /// Resolves or creates model pixels without changing the original observation.
    /// # Errors
    /// Returns authorization, decoding, or storage errors.
    pub async fn prepare_image(
        &self,
        session_id: &str,
        image: &ImageReference,
    ) -> Result<Option<ImageRendition>> {
        let record = self.resolve_image(session_id, image).await?;
        let policy = self.image_policy;
        let key = format!(
            "{}-{}-{}-v4",
            record.content_hash, policy.max_dimension, policy.max_patches
        );
        let (source_width, source_height) = oriented_dimensions(
            image.width,
            image.height,
            record.image_orientation.unwrap_or(1),
        );
        let (width, height) = policy.dimensions(source_width, source_height);
        if record.image_rendition_key.as_deref() == Some(key.as_str()) {
            return Ok(Some(ImageRendition {
                file: record.file,
                width,
                height,
            }));
        }
        if let Some(rendition) = &image.rendition
            && let Some(rendition) = self
                .resolve_rendition(session_id, rendition, &key, [width, height])
                .await?
        {
            return Ok(Some(rendition));
        }
        // ponytail: legacy references scan session records under one preparation lock; add a bounded
        // in-memory key-to-reference cache if repeated legacy replay scans become a bottleneck.
        let _preparation = self.renditions.lock().await;
        // Session-file records are the cache: authorization, restart recovery and cleanup share one owner.
        if let Some(record) = super::list_completed(&self.session_dir(session_id), &self.blob_dir())
            .await?
            .into_iter()
            .find(|record| {
                record.origin == SessionFileOrigin::Observation
                    && record.image_rendition_key.as_deref() == Some(key.as_str())
            })
        {
            if model_dimensions(&record) != Some((width, height)) {
                return Err(Error::Tool(
                    "invalid cached image rendition dimensions".into(),
                ));
            }
            return Ok(Some(ImageRendition {
                file: record.file,
                width,
                height,
            }));
        }
        let permit = Arc::clone(&self.image_work)
            .acquire_owned()
            .await
            .map_err(|_| Error::Tool("image preparation unavailable".into()))?;
        let bytes = self.read_file(session_id, &image.file).await?;
        let prepared = tokio::task::spawn_blocking(move || {
            let _permit = permit;
            prepare_pixels(&bytes, width, height)
        })
        .await
        .map_err(|error| Error::Tool(format!("image preparation failed: {error}")))??;
        let Some((bytes, media_type)) = prepared else {
            self.remember_image_preparation(
                session_id,
                &image.file,
                [image.width, image.height],
                record.image_orientation.unwrap_or(1),
                Some(key),
            )
            .await?;
            return Ok(Some(ImageRendition {
                file: record.file,
                width,
                height,
            }));
        };
        let size = u64::try_from(bytes.len())
            .map_err(|_| Error::Tool("image rendition size overflow".into()))?;
        let mut pending = self
            .begin(
                session_id,
                "model-rendition".into(),
                size,
                media_type.into(),
                SessionFileOrigin::Observation,
                false,
            )
            .await?;
        pending.record.image_rendition_key = Some(key);
        pending.record.image_dimensions = Some([width, height]);
        pending.record.image_orientation = Some(1);
        let file = pending.complete_bytes(&bytes).await?;
        Ok(Some(ImageRendition {
            file,
            width,
            height,
        }))
    }

    async fn resolve_image(
        &self,
        session_id: &str,
        image: &ImageReference,
    ) -> Result<super::StoredSessionFile> {
        let (mut record, _) = self.resolve(session_id, &image.file.id).await?;
        if record.file != image.file {
            return Err(Error::Tool("image reference metadata mismatch".into()));
        }
        let dimensions = match (record.image_dimensions, record.image_orientation) {
            (Some(dimensions), Some(_)) => dimensions,
            _ => {
                let bytes = self.read_file(session_id, &image.file).await?;
                let (_, media_type, width, height, orientation) =
                    validated_image(bytes, &self.image_work).await?;
                if media_type != image.file.media_type {
                    return Err(Error::Tool("image reference media type mismatch".into()));
                }
                self.remember_image_preparation(
                    session_id,
                    &image.file,
                    [width, height],
                    orientation,
                    None,
                )
                .await?;
                record.image_dimensions = Some([width, height]);
                record.image_orientation = Some(orientation);
                [width, height]
            }
        };
        if dimensions != [image.width, image.height] {
            return Err(Error::Tool("image reference dimensions mismatch".into()));
        }
        Ok(record)
    }

    async fn resolve_rendition(
        &self,
        session_id: &str,
        rendition: &ImageRendition,
        key: &str,
        [width, height]: [u32; 2],
    ) -> Result<Option<ImageRendition>> {
        if self.file_is_missing(session_id, &rendition.file.id).await? {
            return Ok(None);
        }
        let (record, _) = self.resolve(session_id, &rendition.file.id).await?;
        if record.file != rendition.file {
            return Err(Error::Tool("invalid image rendition".into()));
        }
        if record.image_rendition_key.as_deref() != Some(key) {
            return Ok(None);
        }
        if (rendition.width, rendition.height) != (width, height)
            || model_dimensions(&record) != Some((width, height))
        {
            return Err(Error::Tool("invalid image rendition dimensions".into()));
        }
        Ok(Some(ImageRendition {
            file: record.file,
            width,
            height,
        }))
    }

    async fn remember_image_preparation(
        &self,
        session_id: &str,
        file: &SessionFileReference,
        dimensions: [u32; 2],
        orientation: u8,
        key: Option<String>,
    ) -> Result<()> {
        let _commit = self.commits.lock().await;
        let (mut record, _) = self.resolve(session_id, &file.id).await?;
        if record.file != *file
            || record
                .image_dimensions
                .is_some_and(|stored| stored != dimensions)
            || record
                .image_orientation
                .is_some_and(|stored| stored != orientation)
        {
            return Err(Error::Tool("image reference metadata mismatch".into()));
        }
        record.image_dimensions = Some(dimensions);
        record.image_orientation = Some(orientation);
        if let Some(key) = key {
            record.image_rendition_key = Some(key);
        }
        super::storage::replace_metadata(&self.session_dir(session_id).join(&file.id), &record)
            .await
    }

    /// Reads and validates an owned image without creating another stored reference.
    /// # Errors
    ///
    /// Returns an error if the file is unauthorized, malformed, or not an image.
    pub async fn read_image(
        &self,
        session_id: &str,
        file: &SessionFileReference,
    ) -> Result<Vec<u8>> {
        let bytes = self.read_file(session_id, file).await?;
        let (bytes, media_type, _, _, _) = validated_image(bytes, &self.image_work).await?;
        if file.media_type != media_type {
            return Err(Error::Tool(
                "stored file media type does not match the image".into(),
            ));
        }
        Ok(bytes)
    }

    /// Reopens an owned image and retains its bytes independently of an upload.
    /// # Errors
    ///
    /// Returns an error if validation or an operation required by this function fails.
    pub async fn inspect_image(
        &self,
        session_id: &str,
        file: &SessionFileReference,
        detail: ImageDetail,
    ) -> Result<ImageReference> {
        let bytes = self.read_file(session_id, file).await?;
        let (_, media_type, width, height, orientation) =
            validated_image(bytes, &self.image_work).await?;
        if file.media_type != media_type {
            return Err(Error::Tool(
                "stored file media type does not match the image".into(),
            ));
        }
        let file = self
            .retain_reference(session_id, session_id, file, SessionFileOrigin::Observation)
            .await?;
        self.remember_image_preparation(session_id, &file, [width, height], orientation, None)
            .await?;
        let mut image = ImageReference {
            file,
            width,
            height,
            detail,
            rendition: None,
        };
        image.rendition = self.prepare_image(session_id, &image).await?;
        Ok(image)
    }

    /// Publishes owned stored bytes without copying or re-encoding them.
    /// # Errors
    ///
    /// Returns an error if validation or an operation required by this function fails.
    pub async fn publish_reference(
        &self,
        session_id: &str,
        file: &SessionFileReference,
    ) -> Result<SessionFileReference> {
        self.retain_reference(session_id, session_id, file, SessionFileOrigin::Artifact)
            .await
    }

    /// Grants a trusted fork access to an exact parent file, preserving its ID.
    /// # Errors
    ///
    /// Returns an error if validation or an operation required by this function fails.
    pub async fn grant_file(
        &self,
        parent: &str,
        child: &str,
        file: &SessionFileReference,
    ) -> Result<SessionFileReference> {
        self.retain_reference(parent, child, file, SessionFileOrigin::Observation)
            .await
    }

    /// Grants an authenticated chat's exact upload to one trusted agent execution.
    /// # Errors
    ///
    /// Returns an error if validation or an operation required by this function fails.
    pub async fn grant_upload(
        &self,
        source: &str,
        target: &str,
        file: &SessionFileReference,
    ) -> Result<SessionFileReference> {
        self.verify_upload(source, file).await?;
        self.retain_reference(source, target, file, SessionFileOrigin::Upload)
            .await
    }

    /// Publishes an execution's exact artifact in its owning chat without copying bytes.
    /// # Errors
    ///
    /// Returns an error if validation or an operation required by this function fails.
    pub async fn share_artifact(
        &self,
        source: &str,
        target: &str,
        file: &SessionFileReference,
    ) -> Result<SessionFileReference> {
        self.retain_reference(source, target, file, SessionFileOrigin::Artifact)
            .await
    }

    async fn retain_reference(
        &self,
        source: &str,
        target: &str,
        file: &SessionFileReference,
        origin: SessionFileOrigin,
    ) -> Result<SessionFileReference> {
        super::validate_session_id(target)?;
        self.ensure_initialized().await?;
        let _commit = self.commits.lock().await;
        let (mut record, _) = self.resolve(source, &file.id).await?;
        if record.file != *file {
            return Err(Error::Tool("stored reference metadata mismatch".into()));
        }
        if source == target && record.origin == origin {
            return Ok(record.file);
        }
        if source == target {
            record.file.id = uuid::Uuid::new_v4().to_string();
        }
        let session_dir = self.session_dir(target);
        super::ensure_private_dir(&session_dir).await?;
        let directory = session_dir.join(&record.file.id);
        if tokio::fs::symlink_metadata(&directory).await.is_ok() {
            let (existing, _) = self.resolve(target, &record.file.id).await?;
            if existing.file == record.file && existing.content_hash == record.content_hash {
                return Ok(existing.file);
            }
            return Err(Error::Tool(
                "fork file ID conflicts with an existing file".into(),
            ));
        }
        let completed = super::list_completed(&session_dir, &self.blob_dir()).await?;
        let _reservation = self.reserve_session(target, file.size, &completed)?;
        record.origin = origin;
        let staging = session_dir.join(format!(".{}-partial", record.file.id));
        super::create_private_dir(&staging).await?;
        super::save_metadata(&staging, &record).await?;
        tokio::fs::rename(&staging, directory).await?;
        Ok(record.file)
    }

    /// Validates and records an immutable image without publishing an artifact.
    /// # Errors
    ///
    /// Returns an error if validation or an operation required by this function fails.
    pub async fn ingest_image(
        &self,
        session_id: &str,
        name: String,
        bytes: Vec<u8>,
        detail: ImageDetail,
    ) -> Result<ImageReference> {
        self.ingest_observation(session_id, name, bytes, detail, false)
            .await
    }

    /// Records a coordinate-sensitive capture only if its pixels already fit presentation limits.
    /// # Errors
    /// Rejects oversized captures before any resizing or storage.
    pub async fn ingest_screenshot(
        &self,
        session_id: &str,
        name: String,
        bytes: Vec<u8>,
        detail: ImageDetail,
    ) -> Result<ImageReference> {
        self.ingest_observation(session_id, name, bytes, detail, true)
            .await
    }

    async fn ingest_observation(
        &self,
        session_id: &str,
        name: String,
        bytes: Vec<u8>,
        detail: ImageDetail,
        coordinates: bool,
    ) -> Result<ImageReference> {
        let (bytes, media_type, width, height, orientation) =
            validated_image(bytes, &self.image_work).await?;
        if coordinates && !self.image_policy.fits(width, height) {
            return Err(Error::Tool("computer capture exceeds presentation dimensions; reduce the viewport or capture size to preserve click coordinates".into()));
        }
        let size = u64::try_from(bytes.len())
            .map_err(|_| Error::Tool("image file size overflow".into()))?;
        let mut pending = self
            .begin(
                session_id,
                name,
                size,
                media_type.into(),
                SessionFileOrigin::Observation,
                false,
            )
            .await?;
        pending.record.image_dimensions = Some([width, height]);
        pending.record.image_orientation = Some(orientation);
        let mut image = ImageReference {
            file: pending.complete_bytes(&bytes).await?,
            width,
            height,
            detail,
            rendition: None,
        };
        image.rendition = self.prepare_image(session_id, &image).await?;
        Ok(image)
    }

    /// Validates and publishes one image directly as an agent artifact.
    /// # Errors
    ///
    /// Returns an error if the image is malformed, mislabeled, or cannot be stored.
    pub async fn publish_image(
        &self,
        session_id: &str,
        name: String,
        declared_media_type: &str,
        bytes: Vec<u8>,
    ) -> Result<SessionFileReference> {
        let (bytes, media_type, _, _, _) = validated_image(bytes, &self.image_work).await?;
        if declared_media_type != media_type {
            return Err(Error::Tool(
                "generated image media type does not match its bytes".into(),
            ));
        }
        self.publish_bytes(
            session_id,
            name,
            media_type.into(),
            &bytes,
            SessionFileOrigin::Artifact,
            true,
        )
        .await
    }

    /// Reads an exact file reference authorized by the owning session.
    /// # Errors
    ///
    /// Returns an error if the resource cannot be read, decoded, or validated.
    pub async fn read_file(
        &self,
        session_id: &str,
        file: &SessionFileReference,
    ) -> Result<Vec<u8>> {
        let (record, path) = self.resolve(session_id, &file.id).await?;
        if record.file != *file {
            return Err(Error::Tool(
                "session file metadata does not match the stored file".into(),
            ));
        }
        Ok(tokio::fs::read(path).await?)
    }

    /// Resolves an opaque file ID inside the authorized session.
    /// # Errors
    ///
    /// Returns an error if validation or an operation required by this function fails.
    pub async fn file_reference(
        &self,
        session_id: &str,
        file_id: &str,
    ) -> Result<SessionFileReference> {
        Ok(self.resolve(session_id, file_id).await?.0.file)
    }
}

fn prepare_pixels(
    bytes: &[u8],
    width: u32,
    height: u32,
) -> Result<Option<(Vec<u8>, &'static str)>> {
    let (source_type, mut decoded, orientation) = decode_image(bytes)?;
    let opaque = opaque_pixels(&decoded);
    decoded.apply_orientation(orientation);
    let resized = (decoded.width(), decoded.height()) != (width, height);
    if !opaque && source_type == "image/png" && !resized && orientation == Orientation::NoTransforms
    {
        return Ok(None);
    }
    let prepared = if resized {
        decoded.resize_exact(width, height, image::imageops::FilterType::Triangle)
    } else {
        decoded
    };
    let (format, media_type) = if opaque {
        (ImageFormat::Jpeg, "image/jpeg")
    } else {
        (ImageFormat::Png, "image/png")
    };
    let encoded = encode_pixels(&prepared, format)?;
    Ok((encoded.as_slice() != bytes).then_some((encoded, media_type)))
}

fn encode_pixels(image: &image::DynamicImage, format: ImageFormat) -> Result<Vec<u8>> {
    let mut output = Cursor::new(Vec::new());
    if format == ImageFormat::Jpeg {
        image::codecs::jpeg::JpegEncoder::new_with_quality(&mut output, 85)
            .encode_image(image)
            .map_err(image_error)?;
    } else {
        image.write_to(&mut output, format).map_err(image_error)?;
    }
    Ok(output.into_inner())
}

fn opaque_pixels(image: &image::DynamicImage) -> bool {
    // Inspect native alpha precision; converting 16-bit alpha to 8-bit can hide transparency.
    match image {
        image::DynamicImage::ImageLumaA8(pixels) => {
            pixels.pixels().all(|pixel| pixel.0[1] == u8::MAX)
        }
        image::DynamicImage::ImageRgba8(pixels) => {
            pixels.pixels().all(|pixel| pixel.0[3] == u8::MAX)
        }
        image::DynamicImage::ImageLumaA16(pixels) => {
            pixels.pixels().all(|pixel| pixel.0[1] == u16::MAX)
        }
        image::DynamicImage::ImageRgba16(pixels) => {
            pixels.pixels().all(|pixel| pixel.0[3] == u16::MAX)
        }
        _ => !image.color().has_alpha(),
    }
}

async fn validated_image(
    bytes: Vec<u8>,
    work: &Arc<tokio::sync::Semaphore>,
) -> Result<(Vec<u8>, &'static str, u32, u32, u8)> {
    let permit = Arc::clone(work)
        .acquire_owned()
        .await
        .map_err(|_| Error::Tool("image decoder unavailable".into()))?;
    tokio::task::spawn_blocking(move || {
        let _permit = permit;
        let (media_type, width, height, orientation) = validate_image(&bytes)?;
        Ok::<_, Error>((bytes, media_type, width, height, orientation))
    })
    .await
    .map_err(|error| Error::Tool(format!("image decoder failed: {error}")))?
}

/// Retains referenced files before a trusted fork; text-only forks need no file store.
/// # Errors
///
/// Returns an error if validation or an operation required by this function fails.
pub async fn grant_context(
    files: Option<&SessionFileStore>,
    parent: &str,
    child: &str,
    input: &[serde_json::Value],
) -> Result<()> {
    for part in input
        .iter()
        .filter_map(crate::protocol::content_parts)
        .flatten()
    {
        let kind = part.get("type").and_then(serde_json::Value::as_str);
        if !matches!(kind, Some("input_image" | "file")) {
            continue;
        }
        let files = files
            .ok_or_else(|| Error::Config("forking media requires session file storage".into()))?;
        if kind == Some("input_image") {
            let image = part.get("image").ok_or_else(|| {
                Error::Config("forked observation requires an image reference".into())
            })?;
            let image: ImageReference = serde::Deserialize::deserialize(image)?;
            files.grant_file(parent, child, &image.file).await?;
            if let Some(rendition) = files.prepare_image(parent, &image).await? {
                files.grant_file(parent, child, &rendition.file).await?;
            }
        } else {
            let file = part.get("file").ok_or_else(|| {
                Error::Config("forked observation requires a file reference".into())
            })?;
            files
                .grant_file(parent, child, &serde::Deserialize::deserialize(file)?)
                .await?;
        }
    }
    Ok(())
}

fn validate_image(bytes: &[u8]) -> Result<(&'static str, u32, u32, u8)> {
    let (media_type, decoded, orientation) = decode_image(bytes)?;
    Ok((
        media_type,
        decoded.width(),
        decoded.height(),
        orientation.to_exif(),
    ))
}

fn oriented_dimensions(width: u32, height: u32, orientation: u8) -> (u32, u32) {
    if (5..=8).contains(&orientation) {
        (height, width)
    } else {
        (width, height)
    }
}

fn model_dimensions(record: &super::StoredSessionFile) -> Option<(u32, u32)> {
    record.image_dimensions.map(|[width, height]| {
        oriented_dimensions(width, height, record.image_orientation.unwrap_or(1))
    })
}

fn decode_limits() -> image::Limits {
    let mut limits = image::Limits::default();
    limits.max_image_width = Some(16_384);
    limits.max_image_height = Some(16_384);
    limits.max_alloc = Some(MAX_DECODE_BYTES);
    limits
}

pub(super) fn validate_dimensions(width: u32, height: u32) -> Result<()> {
    if width == 0
        || height == 0
        || width.max(height) > 16_384
        || u64::from(width) * u64::from(height) > MAX_PIXELS
    {
        return Err(Error::Tool("image exceeds the decoded pixel limit".into()));
    }
    Ok(())
}

fn decode_image(bytes: &[u8]) -> Result<(&'static str, image::DynamicImage, Orientation)> {
    let format = image::guess_format(bytes).map_err(image_error)?;
    let media_type = match format {
        ImageFormat::Png => "image/png",
        ImageFormat::Jpeg => "image/jpeg",
        ImageFormat::WebP => "image/webp",
        ImageFormat::Gif => "image/gif",
        _ => return Err(Error::Tool("image must be PNG, JPEG, WebP, or GIF".into())),
    };
    let mut reader = ImageReader::with_format(Cursor::new(bytes), format);
    reader.limits(decode_limits());
    let mut decoder = reader.into_decoder().map_err(image_error)?;
    let (width, height) = decoder.dimensions();
    validate_dimensions(width, height)?;
    decoder.set_limits(decode_limits()).map_err(image_error)?;
    let orientation = decoder.orientation().map_err(image_error)?;
    let decoded = image::DynamicImage::from_decoder(decoder).map_err(image_error)?;
    match format {
        ImageFormat::Png => {
            if image::codecs::png::PngDecoder::new(Cursor::new(bytes))
                .map_err(image_error)?
                .is_apng()
                .map_err(image_error)?
            {
                return Err(Error::Tool("animated PNG images are not supported".into()));
            }
        }
        ImageFormat::Gif => {
            let mut decoder =
                image::codecs::gif::GifDecoder::new(Cursor::new(bytes)).map_err(image_error)?;
            decoder.set_limits(decode_limits()).map_err(image_error)?;
            let mut frames = decoder.into_frames();
            frames.next().transpose().map_err(image_error)?;
            if frames.next().transpose().map_err(image_error)?.is_some() {
                return Err(Error::Tool("animated GIF images are not supported".into()));
            }
        }
        ImageFormat::WebP => {
            let decoder =
                image::codecs::webp::WebPDecoder::new(Cursor::new(bytes)).map_err(image_error)?;
            if decoder.has_animation() {
                return Err(Error::Tool("animated WebP images are not supported".into()));
            }
        }
        _ => {}
    }
    Ok((media_type, decoded, orientation))
}

fn image_error(error: image::ImageError) -> Error {
    Error::Tool(format!("invalid or unsupported image: {error}"))
}

#[cfg(test)]
mod presentation_tests {
    use super::*;
    use image::ImageEncoder as _;

    #[test]
    fn fitting_preserves_aspect_ratio_and_both_budgets_without_enlarging() {
        let policy = ImagePresentation::default();
        assert_eq!(policy.dimensions(4032, 3024), (1835, 1376));
        for (width, height) in [
            (1, 1),
            (1365, 768),
            (1170, 2532),
            (4032, 3024),
            (16384, 1),
            (1800, 1800),
        ] {
            let (w, h) = policy.dimensions(width, height);
            assert!(policy.fits(w, h));
            assert!(w <= width && h <= height);
            assert!(
                (f64::from(w) / f64::from(width) - f64::from(h) / f64::from(height)).abs()
                    <= 1.0 / f64::from(width.min(height))
            );
            if policy.fits(width, height) {
                assert_eq!((w, h), (width, height));
            }
        }
    }

    #[test]
    fn png_transparency_survives_jpeg_selection_at_native_alpha_precision() {
        for alpha in [u16::MAX - 1, u16::MAX / 2, 0] {
            let pixels = image::ImageBuffer::from_pixel(
                32,
                32,
                image::Rgba([10000_u16, 20000, 30000, alpha]),
            );
            let mut png = Cursor::new(Vec::new());
            image::DynamicImage::ImageRgba16(pixels)
                .write_to(&mut png, ImageFormat::Png)
                .expect("PNG");
            let original = png.into_inner();
            assert!(
                prepare_pixels(&original, 32, 32)
                    .expect("prepare")
                    .is_none()
            );
            let (resized, media_type) = prepare_pixels(&original, 16, 16)
                .expect("resize transparent pixels")
                .expect("PNG rendition");
            assert_eq!(media_type, "image/png");
            assert_eq!(
                image::load_from_memory(&resized)
                    .expect("decode PNG")
                    .to_rgba16()
                    .get_pixel(0, 0)
                    .0[3],
                alpha
            );
        }
        let pixels = image::DynamicImage::new_rgb8(32, 32);
        let png = encode_pixels(&pixels, ImageFormat::Png).expect("small PNG");
        let (prepared, media_type) = prepare_pixels(&png, 32, 32)
            .expect("prepare opaque PNG")
            .expect("JPEG rendition even for small images");
        assert_eq!(media_type, "image/jpeg");
        assert_eq!(
            image::guess_format(&prepared).expect("format"),
            ImageFormat::Jpeg
        );
    }

    #[tokio::test]
    async fn unchanged_pixels_cache_on_the_original_without_another_quota_charge() {
        for (format, pixels) in [
            (ImageFormat::Png, image::DynamicImage::new_rgba8(32, 32)),
            (ImageFormat::Jpeg, image::DynamicImage::new_rgb8(32, 32)),
        ] {
            let state = tempfile::tempdir().expect("state");
            let store = SessionFileStore::new(state.path(), None);
            let bytes = encode_pixels(&pixels, format).expect("image encoding");
            let original_size = u64::try_from(bytes.len()).expect("size");
            let mut image = store
                .ingest_image("session", "image".into(), bytes, ImageDetail::High)
                .await
                .expect("image");
            assert_eq!(
                image.rendition.as_ref().expect("cached no-op").file,
                image.file
            );
            let reopened = SessionFileStore::new(state.path(), None);
            let rendition = image.rendition.take();
            assert_eq!(
                reopened
                    .prepare_image("session", &image)
                    .await
                    .expect("cache"),
                rendition
            );
            image.rendition = rendition;
            assert_eq!(
                reopened
                    .list_files("session", &[SessionFileOrigin::Observation])
                    .await
                    .expect("files")
                    .len(),
                1
            );
            assert_eq!(reopened.stored_bytes().await.expect("bytes"), original_size);
            let input = [
                serde_json::json!({"role":"user", "content":[{"type":"input_image", "image":image}]}),
            ];
            grant_context(Some(&store), "session", "child", &input)
                .await
                .expect("fork");
            assert_eq!(
                store
                    .list_files("child", &[SessionFileOrigin::Observation])
                    .await
                    .expect("child files")
                    .len(),
                1
            );
            store
                .delete_session("session")
                .await
                .expect("delete parent");
            assert_eq!(
                store.stored_bytes().await.expect("child bytes"),
                original_size
            );
            store.delete_session("child").await.expect("delete child");
            assert_eq!(store.stored_bytes().await.expect("collected"), 0);
        }
    }

    #[tokio::test]
    async fn preparation_rejects_claimed_dimensions_before_reusing_cached_pixels() {
        let state = tempfile::tempdir().expect("state");
        let store = SessionFileStore::new(state.path(), None)
            .image_presentation(ImagePresentation {
                max_dimension: 64,
                max_patches: 2,
            })
            .expect("policy");
        let bytes =
            encode_pixels(&image::DynamicImage::new_rgb8(256, 128), ImageFormat::Png).expect("PNG");
        let mut image = store
            .ingest_image("session", "image.png".into(), bytes, ImageDetail::High)
            .await
            .expect("image");
        image.width = 1;
        assert!(store.prepare_image("session", &image).await.is_err());
        image.width = 256;
        image.rendition.as_mut().expect("rendition").width = 1;
        assert!(store.prepare_image("session", &image).await.is_err());
        image.rendition.as_mut().expect("rendition").width = 64;
        let old_id = image.rendition.as_ref().expect("rendition").file.id.clone();
        store
            .prepare_delete_sessions(
                &["session".into()],
                super::super::SessionFileSelection::Ids(vec![old_id.clone()]),
            )
            .await
            .expect("prepare purge")
            .delete()
            .await
            .expect("purge rendition");
        let regenerated = store
            .prepare_image("session", &image)
            .await
            .expect("rebuild purged rendition")
            .expect("rendition");
        assert_ne!(regenerated.file.id, old_id);
        assert_eq!(
            store
                .prepare_image("session", &image)
                .await
                .expect("reuse regenerated")
                .as_ref(),
            Some(&regenerated)
        );
        let input =
            [serde_json::json!({"role":"user", "content":[{"type":"input_image", "image":image}]})];
        grant_context(Some(&store), "session", "child", &input)
            .await
            .expect("fork repairs the historical rendition pointer");
        assert_eq!(
            store
                .prepare_image("child", &image)
                .await
                .expect("child retains the current rendition")
                .as_ref(),
            Some(&regenerated)
        );
        assert!(
            grant_context(Some(&store), "stranger", "other", &input)
                .await
                .is_err()
        );
        let (record, _) = store
            .resolve("session", &regenerated.file.id)
            .await
            .expect("record");
        std::fs::write(store.blob_path(&record.content_hash), b"tampered").expect("tamper blob");
        image.rendition = Some(regenerated);
        assert!(store.prepare_image("session", &image).await.is_err());
        let (original, _) = store
            .resolve("session", &image.file.id)
            .await
            .expect("original");
        std::fs::write(
            store.blob_path(&original.content_hash),
            b"tampered original",
        )
        .expect("tamper original");
        assert!(
            grant_context(Some(&store), "session", "corrupt", &input)
                .await
                .is_err()
        );
    }

    #[tokio::test]
    async fn exif_rotation_fits_visual_pixels_and_preserves_original_reference_geometry() {
        let state = tempfile::tempdir().expect("state");
        let store = SessionFileStore::new(state.path(), None)
            .image_presentation(ImagePresentation {
                max_dimension: 64,
                max_patches: 2,
            })
            .expect("policy");
        let pixels = image::RgbImage::from_fn(128, 64, |x, _| {
            if x < 64 {
                image::Rgb([255, 0, 0])
            } else {
                image::Rgb([0, 255, 0])
            }
        });
        let mut bytes = Vec::new();
        let mut encoder = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut bytes, 95);
        // Little-endian TIFF containing one SHORT orientation tag: rotate 90 degrees.
        encoder
            .set_exif_metadata(vec![
                0x49, 0x49, 42, 0, 8, 0, 0, 0, 1, 0, 0x12, 0x01, 3, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0,
                0, 0, 0,
            ])
            .expect("EXIF");
        encoder.encode_image(&pixels).expect("JPEG");
        // Existing references have raw encoded geometry and no cached preparation metadata.
        let file = store
            .publish_artifact("session", "rotated.jpg".into(), "image/jpeg".into(), &bytes)
            .await
            .expect("original");
        let image = ImageReference {
            file,
            width: 128,
            height: 64,
            detail: ImageDetail::High,
            rendition: None,
        };
        let rendition = store
            .prepare_image("session", &image)
            .await
            .expect("prepare existing reference")
            .expect("rendition");
        assert_eq!((image.width, image.height), (128, 64));
        assert_eq!((rendition.width, rendition.height), (32, 64));
        assert_eq!(
            store
                .read_file("session", &image.file)
                .await
                .expect("original"),
            bytes
        );
        let prepared = store
            .read_file("session", &rendition.file)
            .await
            .expect("rendition pixels");
        let (_, decoded, orientation) = decode_image(&prepared).expect("decode rendition");
        assert_eq!((decoded.width(), decoded.height()), (32, 64));
        assert_eq!(orientation.to_exif(), 1);
        let pixels = decoded.to_rgb8();
        let top = pixels.get_pixel(16, 8).0;
        let bottom = pixels.get_pixel(16, 56).0;
        assert!(top[0] > 200 && top[1] < 30, "red rotates to the top");
        assert!(
            bottom[1] > 200 && bottom[0] < 30,
            "green rotates to the bottom"
        );
        let record = store
            .resolve("session", &image.file.id)
            .await
            .expect("record")
            .0;
        assert_eq!(record.image_dimensions, Some([128, 64]));
        assert_eq!(record.image_orientation, Some(6));
        assert_eq!(
            store
                .prepare_image("session", &image)
                .await
                .expect("cache")
                .as_ref(),
            Some(&rendition)
        );
    }

    #[tokio::test]
    async fn missing_file_check_distinguishes_purge_from_corruption() {
        let state = tempfile::tempdir().expect("state");
        let store = SessionFileStore::new(state.path(), None);
        let file = store
            .publish_artifact("session", "file.txt".into(), "text/plain".into(), b"file")
            .await
            .expect("file");
        let directory = store.session_dir("session").join(&file.id);
        assert!(
            !store
                .file_is_missing("session", &file.id)
                .await
                .expect("present")
        );
        tokio::fs::remove_file(directory.join(super::super::METADATA_FILE))
            .await
            .expect("remove metadata");
        assert!(
            !store
                .file_is_missing("session", &file.id)
                .await
                .expect("corrupt")
        );
        tokio::fs::remove_dir(&directory)
            .await
            .expect("remove directory");
        assert!(
            store
                .file_is_missing("session", &file.id)
                .await
                .expect("purged")
        );
        tokio::fs::write(&directory, b"not a directory")
            .await
            .expect("non-directory");
        assert!(store.file_is_missing("session", &file.id).await.is_err());
        #[cfg(unix)]
        {
            tokio::fs::remove_file(&directory)
                .await
                .expect("remove file");
            std::os::unix::fs::symlink(state.path(), &directory).expect("symlink");
            assert!(store.file_is_missing("session", &file.id).await.is_err());
        }
    }

    #[tokio::test]
    async fn originals_renditions_cache_forks_and_capture_coordinates_share_storage_rules() {
        let state = tempfile::tempdir().expect("state");
        let policy = ImagePresentation {
            max_dimension: 64,
            max_patches: 2,
        };
        let store = SessionFileStore::new(state.path(), None)
            .image_presentation(policy)
            .expect("policy");
        for format in [ImageFormat::Png, ImageFormat::Jpeg, ImageFormat::WebP] {
            let mut bytes = Cursor::new(Vec::new());
            image::DynamicImage::new_rgb8(256, 128)
                .write_to(&mut bytes, format)
                .expect("image");
            let original = bytes.into_inner();
            let mut observation = store
                .ingest_image(
                    "parent",
                    "original".into(),
                    original.to_vec(),
                    ImageDetail::High,
                )
                .await
                .expect("ingest");
            assert_eq!(
                store
                    .read_file("parent", &observation.file)
                    .await
                    .expect("original"),
                original
            );
            let rendition = observation.rendition.take().expect("prepared");
            assert_eq!((rendition.width, rendition.height), (64, 32));
            let prepared = store
                .read_image("parent", &rendition.file)
                .await
                .expect("prepared bytes");
            assert_eq!(
                image::load_from_memory(&prepared).expect("decode").width(),
                64
            );
            assert_eq!(
                image::guess_format(&prepared).expect("format"),
                ImageFormat::Jpeg
            );
            let reopened = SessionFileStore::new(state.path(), None)
                .image_presentation(policy)
                .expect("policy");
            assert_eq!(
                reopened
                    .prepare_image("parent", &observation)
                    .await
                    .expect("persistent cache")
                    .expect("rendition"),
                rendition
            );
            assert!(store.prepare_image("stranger", &observation).await.is_err());
            observation.rendition = Some(rendition);
            let stricter = SessionFileStore::new(state.path(), None)
                .image_presentation(ImagePresentation {
                    max_dimension: 32,
                    max_patches: 1,
                })
                .expect("stricter policy");
            let different = stricter
                .prepare_image("parent", &observation)
                .await
                .expect("policy cache key")
                .expect("stricter rendition");
            assert_eq!((different.width, different.height), (32, 16));
            assert_ne!(
                different.file.id,
                observation
                    .rendition
                    .as_ref()
                    .expect("initial rendition")
                    .file
                    .id
            );

            let input = [
                serde_json::json!({"role":"user", "content":[{"type":"input_image", "image":observation}]}),
            ];
            grant_context(Some(&store), "parent", "child", &input)
                .await
                .expect("fork");
            assert!(
                store
                    .prepare_image("child", &observation)
                    .await
                    .expect("child authorization")
                    .is_some()
            );
            assert!(
                store
                    .ingest_screenshot("parent", "capture".into(), original, ImageDetail::High)
                    .await
                    .is_err()
            );
        }
        let mut bytes = Cursor::new(Vec::new());
        image::DynamicImage::new_rgb8(16, 16)
            .write_to(&mut bytes, ImageFormat::Png)
            .expect("small image");
        let original = bytes.into_inner();
        let image = store
            .ingest_screenshot(
                "parent",
                "small".into(),
                original.to_vec(),
                ImageDetail::High,
            )
            .await
            .expect("fitting capture");
        let rendition = image.rendition.as_ref().expect("opaque capture rendition");
        assert_eq!(rendition.file.media_type, "image/jpeg");
        assert_eq!((rendition.width, rendition.height), (16, 16));
        assert_eq!(
            store
                .read_file("parent", &image.file)
                .await
                .expect("fitting bytes"),
            original
        );
        store.delete_session("parent").await.expect("delete parent");
        assert!(store.stored_bytes().await.expect("fork charged") > 0);
        store.delete_session("child").await.expect("delete child");
        assert_eq!(store.stored_bytes().await.expect("collected"), 0);
    }
}