byob-tools 0.3.1

Agent tools: filesystem, web fetch, charts, python (L2)
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
//! Artifacts: versioned files the model makes beside the conversation (see
//! [`byob_protocol::artifact`]). `create_artifact`, `update_artifact` and `read_artifact`.
//!
//! What an artifact is and what is done with it per type (content checks, prompt guidance,
//! how it is served) comes from the [`ArtifactTypes`] registry the tools are given; nothing
//! here knows a particular type.
//!
//! Layout, in the chat's workspace:
//!
//! ```text
//! .byob/artifacts/<id>/artifact.json   the index: chat, type, title, file name, versions
//! .byob/artifacts/<id>/v1.html         one file per version, never overwritten
//! ```
//!
//! Charts and images other tools make (`charts/…chart.json`, a PNG from `run_python`) are
//! *adopted*: they get an index whose single version points at the file where it is.
//!
//! Access: [`Access::Read`], like `make_chart`: these tools only ever create new files
//! inside `.byob/artifacts/` (exclusively, never through links) and rewrite their own index.
//! A version a per-turn undo removed simply drops out of the list.

use std::fs::OpenOptions;
use std::io::{ErrorKind, Read as _, Write as _};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

use byob_protocol::{Artifact, ArtifactKind, ArtifactRef, ArtifactVersion, MediaKind, MediaRef, artifact::valid_id};

use crate::artifact_types::{ArtifactType, ArtifactTypes};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use crate::sandbox;
use crate::{Access, FileChange, Tool, ToolContext, ToolOutput, ToolSpec, parse_args};

/// Workspace folder artifacts live in (inside byob's reserved `.byob`).
pub const ARTIFACT_DIR: &str = ".byob/artifacts";
const META_FILE: &str = "artifact.json";
/// Largest artifact content the model may write (per version).
pub const MAX_CONTENT_BYTES: usize = 1024 * 1024;
/// Largest version file read back (adopted images may be bigger than what tools write).
pub const MAX_FILE_BYTES: u64 = 32 * 1024 * 1024;
/// Find/replace edits in one `update_artifact` call.
pub const MAX_EDITS: usize = 50;
/// Versions one artifact may have.
pub const MAX_VERSIONS: usize = 500;
const MAX_TITLE_CHARS: usize = 80;
const MAX_NOTE_CHARS: usize = 200;
const MAX_META_BYTES: u64 = 512 * 1024;
/// Artifact folders looked at when listing a workspace.
const MAX_LISTED: usize = 2000;
/// Charts/images adopted from one tool call.
const MAX_ADOPTED: usize = 4;
/// Text returned by `read_artifact`.
const MAX_READ_CHARS: usize = 200_000;

/// The index file: an artifact and the chat it belongs to.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Meta {
    pub chat: String,
    #[serde(flatten)]
    pub artifact: Artifact,
}

/// Milliseconds since the epoch, strictly increasing within the process (so artifacts made
/// in the same millisecond still list in order).
fn now_ms() -> i64 {
    static LAST: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(0);
    let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX));
    let prev = LAST.try_update(Ordering::AcqRel, Ordering::Acquire, |last| Some(now.max(last.saturating_add(1)))).unwrap_or(now);
    now.max(prev.saturating_add(1))
}

/// `.byob/artifacts`, created if needed; refuses links anywhere on the way.
fn root_dir(workspace: &Path) -> Result<(PathBuf, PathBuf), String> {
    let r = sandbox::resolve(workspace, ARTIFACT_DIR)?;
    for dir in [r.root.join(sandbox::RESERVED_DIR), r.abs.clone()] {
        match std::fs::symlink_metadata(&dir) {
            Ok(m) if m.file_type().is_symlink() || !m.is_dir() => return Err(format!("{} is not a plain folder; can't keep artifacts there", dir.display())),
            Ok(_) => {}
            Err(e) if e.kind() == ErrorKind::NotFound => std::fs::create_dir(&dir)
                .or_else(|e| if e.kind() == ErrorKind::AlreadyExists { Ok(()) } else { Err(e) })
                .map_err(|e| format!("can't create {}: {e}", dir.display()))?,
            Err(e) => return Err(format!("can't access {}: {e}", dir.display())),
        }
    }
    Ok((r.root, r.abs))
}

/// The folder of artifact `id` (which must exist and be a plain folder).
fn artifact_dir(workspace: &Path, id: &str) -> Result<(PathBuf, PathBuf), String> {
    if !valid_id(id) {
        return Err(format!("there is no artifact {id:?}"));
    }
    let r = sandbox::resolve(workspace, &format!("{ARTIFACT_DIR}/{id}"))?;
    match std::fs::symlink_metadata(&r.abs) {
        Ok(m) if m.is_dir() && !m.file_type().is_symlink() => Ok((r.root, r.abs)),
        _ => Err(format!("there is no artifact {id:?}")),
    }
}

fn read_capped(path: &Path, max: u64) -> Result<Vec<u8>, String> {
    let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
    let meta = std::fs::metadata(path).map_err(|e| format!("{name}: {e}"))?;
    if !meta.is_file() {
        return Err(format!("{name} is not a file"));
    }
    if meta.len() > max {
        return Err(format!("{name} is too big ({} bytes)", meta.len()));
    }
    let mut out = Vec::new();
    std::fs::File::open(path).and_then(|f| f.take(max).read_to_end(&mut out)).map_err(|e| format!("{name}: {e}"))?;
    Ok(out)
}

/// The file behind a version path, if it is still there (inside the workspace).
fn version_file(workspace: &Path, path: &str) -> Option<PathBuf> {
    let r = sandbox::resolve_follow(workspace, path).ok()?;
    std::fs::metadata(&r.abs).ok().filter(std::fs::Metadata::is_file).map(|_| r.abs)
}

/// Read an artifact's index. Versions whose file is gone (e.g. undone) are left out; an
/// artifact with none left is gone.
pub fn load(workspace: &Path, id: &str) -> Result<Meta, String> {
    let (_, dir) = artifact_dir(workspace, id)?;
    let bytes = read_capped(&dir.join(META_FILE), MAX_META_BYTES).map_err(|_| format!("there is no artifact {id:?}"))?;
    let mut meta: Meta = serde_json::from_slice(&bytes).map_err(|e| format!("artifact {id:?} has a broken index: {e}"))?;
    if meta.artifact.id != id {
        return Err(format!("artifact {id:?} has a broken index"));
    }
    meta.artifact.versions.retain(|v| version_file(workspace, &v.path).is_some());
    // Indices written before content types (or with a broken one): what its kind stands for.
    meta.artifact.content_type = meta.artifact.content_type().to_owned();
    if meta.artifact.versions.is_empty() {
        return Err(format!("artifact {id:?} no longer exists"));
    }
    Ok(meta)
}

/// Like [`load`], but only an artifact of `chat`.
pub fn load_in_chat(workspace: &Path, chat: &str, id: &str) -> Result<Meta, String> {
    let meta = load(workspace, id)?;
    if meta.chat != chat {
        return Err(format!("there is no artifact {id:?} in this chat"));
    }
    Ok(meta)
}

/// Write the index (via a temporary file, so a crash never leaves half of one).
fn save(dir: &Path, meta: &Meta) -> Result<(), String> {
    let json = serde_json::to_vec_pretty(meta).map_err(|e| e.to_string())?;
    let tmp = dir.join(format!("{META_FILE}.tmp"));
    let _ = std::fs::remove_file(&tmp);
    let mut f = OpenOptions::new().write(true).create_new(true).open(&tmp).map_err(|e| format!("can't save the artifact index: {e}"))?;
    f.write_all(&json).map_err(|e| format!("can't save the artifact index: {e}"))?;
    drop(f);
    std::fs::rename(&tmp, dir.join(META_FILE)).map_err(|e| format!("can't save the artifact index: {e}"))
}

/// Every artifact of `chat` in `workspace`, oldest first.
pub fn list(workspace: &Path, chat: &str) -> Vec<Artifact> {
    let Ok(r) = sandbox::resolve(workspace, ARTIFACT_DIR) else { return Vec::new() };
    let Ok(entries) = std::fs::read_dir(&r.abs) else { return Vec::new() };
    let mut out: Vec<Artifact> = entries
        .take(MAX_LISTED)
        .filter_map(Result::ok)
        .filter_map(|e| e.file_name().to_str().map(str::to_owned))
        .filter(|id| valid_id(id))
        .filter_map(|id| load(workspace, &id).ok())
        .filter(|m| m.chat == chat)
        .map(|m| m.artifact)
        .collect();
    out.sort_by_key(|a| (a.versions.first().map_or(0, |v| v.created_at), a.id.clone()));
    out
}

/// Delete every artifact of `chat` in `workspace` for good (when the chat is deleted).
/// Returns how many were removed; ones whose index can't be read are left alone.
pub fn remove_chat(workspace: &Path, chat: &str) -> usize {
    let Ok(r) = sandbox::resolve(workspace, ARTIFACT_DIR) else { return 0 };
    let Ok(entries) = std::fs::read_dir(&r.abs) else { return 0 };
    let mut removed = 0;
    for id in entries.filter_map(Result::ok).filter_map(|e| e.file_name().to_str().map(str::to_owned)) {
        let Ok((_, dir)) = artifact_dir(workspace, &id) else { continue };
        let Ok(bytes) = read_capped(&dir.join(META_FILE), MAX_META_BYTES) else { continue };
        let Ok(meta) = serde_json::from_slice::<Meta>(&bytes) else { continue };
        if meta.chat != chat {
            continue;
        }
        match std::fs::remove_dir_all(&dir) {
            Ok(()) => removed += 1,
            Err(e) => log::warn!("couldn't delete artifact {id} of chat {chat}: {e}"),
        }
    }
    removed
}

/// Bytes of version `version` of artifact `id`, which must belong to `chat`.
pub fn read_version(workspace: &Path, chat: &str, id: &str, version: u32, max: u64) -> Result<(Artifact, Vec<u8>), String> {
    let meta = load_in_chat(workspace, chat, id)?;
    let v = meta.artifact.version(version).ok_or_else(|| format!("artifact {id:?} has no version {version}"))?;
    let file = version_file(workspace, &v.path).ok_or_else(|| format!("version {version} of {id:?} is gone"))?;
    let bytes = read_capped(&file, max)?;
    Ok((meta.artifact, bytes))
}

/// `[.../].byob/artifacts/<id>/v<n>.<ext>` → the version it is.
pub fn parse_version_path(path: &str) -> Option<ArtifactRef> {
    let norm = path.replace('\\', "/");
    let rest = norm.rsplit_once(&format!("{ARTIFACT_DIR}/")).map(|(_, r)| r)?;
    let (id, file) = rest.split_once('/')?;
    let n: u32 = file.strip_prefix('v')?.split_once('.')?.0.parse().ok()?;
    (valid_id(id) && n > 0 && !file.contains('/')).then(|| ArtifactRef { id: id.to_owned(), version: n })
}

/// A new artifact id from a title: `loan-calculator-3f2a`.
fn new_id(title: &str) -> String {
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let mut slug = String::new();
    for c in title.chars().flat_map(char::to_lowercase) {
        if c.is_ascii_alphanumeric() {
            slug.push(c);
        } else if !slug.ends_with('-') && !slug.is_empty() {
            slug.push('-');
        }
        if slug.len() >= 40 {
            break;
        }
    }
    let slug = slug.trim_matches('-');
    let slug = if slug.is_empty() { "artifact" } else { slug };
    let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| u64::from(d.subsec_nanos()));
    let n = COUNTER.fetch_add(1, Ordering::Relaxed);
    let mix = nanos ^ n.wrapping_mul(0x9e37_79b9) ^ u64::from(std::process::id()).rotate_left(16);
    format!("{slug}-{:04x}", mix & 0xffff)
}

/// Create a fresh, empty folder for a new artifact. Returns `(id, folder)`.
fn new_artifact_dir(workspace: &Path, title: &str) -> Result<(String, PathBuf), String> {
    let (_, root) = root_dir(workspace)?;
    for _ in 0..16 {
        let id = new_id(title);
        let dir = root.join(&id);
        match std::fs::create_dir(&dir) {
            Ok(()) => return Ok((id, dir)),
            Err(e) if e.kind() == ErrorKind::AlreadyExists => {}
            Err(e) => return Err(format!("can't create the artifact: {e}")),
        }
    }
    Err("can't find a free artifact id; try again".into())
}

fn rel(root: &Path, path: &Path) -> String {
    path.strip_prefix(root).unwrap_or(path).to_string_lossy().replace('\\', "/")
}

fn clip(s: &str, max: usize) -> String {
    let clean: String = s.chars().map(|c| if c.is_control() { ' ' } else { c }).collect();
    let clean = clean.trim();
    let mut out: String = clean.chars().take(max).collect();
    if clean.chars().count() > max {
        out.push('…');
    }
    out
}

/// A model-given file name made safe, and the extension its versions are saved with.
fn clean_filename(types: &ArtifactTypes, raw: &str, title: &str) -> Result<(String, String), String> {
    let last = raw.trim().rsplit(['/', '\\']).next().unwrap_or_default();
    let name: String = last.chars().filter(|c| c.is_alphanumeric() || matches!(c, '.' | '-' | '_' | ' ')).take(100).collect();
    let name = name.trim().trim_start_matches('.').to_owned();
    let lower = name.to_ascii_lowercase();
    let ext = if let Some(ext) = types.compound_extension(&lower) {
        ext
    } else {
        match lower.rsplit_once('.') {
            Some((stem, ext)) if !stem.is_empty() && (1..=10).contains(&ext.len()) && ext.bytes().all(|b| b.is_ascii_alphanumeric()) => ext.to_owned(),
            _ => {
                return Err(format!("filename {raw:?} {}", types.extension_hint()));
            }
        }
    };
    let name = if name.is_empty() { format!("{}.{ext}", new_id(title)) } else { name };
    Ok((name, ext))
}

/// Write a version file exclusively (never through a link, never over another file).
fn write_version(dir: &Path, n: u32, ext: &str, content: &str) -> Result<PathBuf, String> {
    write_version_bytes(dir, n, ext, content.as_bytes())
}

fn write_version_bytes(dir: &Path, n: u32, ext: &str, bytes: &[u8]) -> Result<PathBuf, String> {
    let path = dir.join(format!("v{n}.{ext}"));
    let mut f = OpenOptions::new().write(true).create_new(true).open(&path).map_err(|e| format!("can't write version {n}: {e}"))?;
    f.write_all(bytes).map_err(|e| format!("can't write version {n}: {e}"))?;
    Ok(path)
}

/// Check content for type `ty` (size, then the type's own check). Returns the text to save
/// and notes for the model.
fn check_content(ty: &ArtifactType, content: &str) -> Result<(String, String), String> {
    if content.len() > MAX_CONTENT_BYTES {
        return Err(format!("the content is too big ({} bytes; the limit is {MAX_CONTENT_BYTES})", content.len()));
    }
    if content.trim().is_empty() {
        return Err("the content is empty".into());
    }
    ty.hooks.check(content)
}

fn chat_of<'a>(ctx: &ToolContext<'a>) -> Result<&'a str, String> {
    ctx.chat().filter(|c| !c.is_empty()).ok_or_else(|| "artifacts are only available in a chat".to_owned())
}

/// Adopt the charts and images a tool call made as artifacts of `chat` (one version each,
/// pointing at the file where it is). Returns refs for the step.
fn adopt(workspace: &Path, chat: &str, media: &[MediaRef]) -> Vec<ArtifactRef> {
    let mut out = Vec::new();
    for m in media.iter().filter(|m| m.kind != MediaKind::File).take(MAX_ADOPTED) {
        let kind = if m.kind == MediaKind::Chart { ArtifactKind::Chart } else { ArtifactKind::Image };
        let name = m.name().to_owned();
        let content_type = kind.content_type(&name).to_owned();
        let title = name.strip_suffix(".chart.json").or_else(|| name.rsplit_once('.').map(|(s, _)| s)).unwrap_or(&name).to_owned();
        let size = version_file(workspace, &m.path).and_then(|p| std::fs::metadata(p).ok()).map_or(m.size, |md| md.len());
        let result = new_artifact_dir(workspace, &title).and_then(|(id, dir)| {
            let version = ArtifactVersion { n: 1, path: m.path.clone(), created_at: now_ms(), note: String::new(), size };
            let artifact = Artifact {
                id: id.clone(),
                kind,
                content_type: content_type.clone(),
                title: clip(&title, MAX_TITLE_CHARS),
                filename: name.clone(),
                versions: vec![version],
            };
            save(&dir, &Meta { chat: chat.to_owned(), artifact }).map(|()| id)
        });
        match result {
            Ok(id) => out.push(ArtifactRef { id, version: 1 }),
            Err(e) => log::warn!("adopting {} as an artifact: {e}", m.path),
        }
    }
    out
}

/// The artifact versions a finished tool call made: versions written by the artifact tools
/// (by path), else the charts and images it made, adopted.
pub fn for_call(workspace: &Path, chat: &str, changes: &[FileChange], media: &[MediaRef]) -> Vec<ArtifactRef> {
    let made: Vec<ArtifactRef> = changes
        .iter()
        .filter_map(|c| match c {
            FileChange::Created(p) => parse_version_path(&p.to_string_lossy()),
            _ => None,
        })
        .collect();
    if !made.is_empty() {
        return made;
    }
    adopt(workspace, chat, media)
}

/// A version to save from bytes someone else made (a plugin backend's tool or view).
#[derive(Debug, Clone, Default)]
pub struct SaveBytes<'a> {
    pub chat: &'a str,
    /// An artifact of `chat`; `None` makes a new one (then `title` and `filename` are needed).
    pub id: Option<&'a str>,
    pub title: Option<&'a str>,
    /// A new file name for an existing artifact renames it (and its type follows the name,
    /// e.g. a `.csv` table that becomes an `.xlsx` workbook).
    pub filename: Option<&'a str>,
    pub bytes: &'a [u8],
    pub note: &'a str,
    /// Overwrite version `n` instead of adding one, if it is still the latest (a draft the
    /// user keeps editing). Ignored otherwise.
    pub replace: Option<u32>,
}

/// What [`save_bytes`] did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Saved {
    pub artifact: Artifact,
    pub version: u32,
    /// The version's file.
    pub file: PathBuf,
    /// A new artifact.
    pub created: bool,
    /// An existing version was overwritten (`SaveBytes::replace`).
    pub replaced: bool,
}

/// Write `s.bytes` as a new version (or over a draft, see [`SaveBytes::replace`]) of an
/// artifact, or as a new artifact. Same layout and rules as the artifact tools: files only
/// inside `.byob/artifacts`, never through links, version numbers never reused.
pub fn save_bytes(types: &ArtifactTypes, workspace: &Path, s: &SaveBytes<'_>) -> Result<Saved, String> {
    if s.chat.is_empty() {
        return Err("artifacts are only available in a chat".into());
    }
    if s.bytes.len() as u64 > MAX_FILE_BYTES {
        return Err(format!("the file is too big ({} bytes; at most {MAX_FILE_BYTES})", s.bytes.len()));
    }
    if s.bytes.is_empty() {
        return Err("the file is empty".into());
    }
    let note = clip(s.note, MAX_NOTE_CHARS);
    let root = sandbox::root(workspace)?;
    let Some(id) = s.id.map(str::trim).filter(|i| !i.is_empty()) else {
        let title = clip(s.title.unwrap_or_default(), MAX_TITLE_CHARS);
        if title.is_empty() {
            return Err("a new artifact needs a title".into());
        }
        let (filename, ext) = clean_filename(types, s.filename.unwrap_or_default(), &title)?;
        let ty = types.for_filename(&filename);
        let (id, dir) = new_artifact_dir(workspace, &title)?;
        let result = (|| {
            let file = write_version_bytes(&dir, 1, &ext, s.bytes)?;
            let version = ArtifactVersion { n: 1, path: rel(&root, &file), created_at: now_ms(), note: note.clone(), size: s.bytes.len() as u64 };
            let artifact =
                Artifact { id: id.clone(), kind: ty.kind, content_type: ty.content_type.clone(), title: title.clone(), filename, versions: vec![version] };
            save(&dir, &Meta { chat: s.chat.to_owned(), artifact: artifact.clone() })?;
            Ok::<_, String>(Saved { artifact, version: 1, file, created: true, replaced: false })
        })();
        if result.is_err() {
            let _ = std::fs::remove_dir_all(&dir);
        }
        return result;
    };
    let mut meta = load_in_chat(workspace, s.chat, id)?;
    let id = meta.artifact.id.clone();
    let (root, dir) = artifact_dir(workspace, &id)?;
    // Every version, also those whose file is gone (numbers are never reused).
    let all = serde_json::from_slice::<Meta>(&read_capped(&dir.join(META_FILE), MAX_META_BYTES)?).map_err(|e| e.to_string())?;
    if let Some(name) = s.filename.map(str::trim).filter(|n| !n.is_empty() && *n != meta.artifact.filename) {
        let (filename, _) = clean_filename(types, name, &meta.artifact.title)?;
        let ty = types.for_filename(&filename);
        meta.artifact.filename = filename;
        meta.artifact.content_type = ty.content_type.clone();
        meta.artifact.kind = ty.kind;
    }
    if let Some(t) = s.title.map(|t| clip(t, MAX_TITLE_CHARS)).filter(|t| !t.is_empty()) {
        meta.artifact.title = t;
    }
    let lower = meta.artifact.filename.to_ascii_lowercase();
    let ext = types.compound_extension(&lower).unwrap_or_else(|| lower.rsplit_once('.').map_or("bin".to_owned(), |(_, e)| e.to_owned()));
    let latest = meta.artifact.latest().map(|v| v.n);
    let top = all.artifact.versions.iter().map(|v| v.n).max().unwrap_or(0);
    meta.artifact.versions = all.artifact.versions;
    if let Some(n) = s.replace.filter(|n| Some(*n) == latest && *n == top)
        && let Some(v) = meta.artifact.versions.iter_mut().find(|v| v.n == n)
        && v.path.ends_with(&format!("/v{n}.{ext}"))
    {
        let file = dir.join(format!("v{n}.{ext}"));
        let tmp = dir.join(format!("v{n}.{ext}.tmp"));
        let _ = std::fs::remove_file(&tmp);
        let mut f = OpenOptions::new().write(true).create_new(true).open(&tmp).map_err(|e| format!("can't write version {n}: {e}"))?;
        f.write_all(s.bytes).map_err(|e| format!("can't write version {n}: {e}"))?;
        drop(f);
        std::fs::rename(&tmp, &file).map_err(|e| format!("can't write version {n}: {e}"))?;
        v.size = s.bytes.len() as u64;
        v.created_at = now_ms();
        if !note.is_empty() {
            v.note = note;
        }
        save(&dir, &meta)?;
        return Ok(Saved { artifact: meta.artifact, version: n, file, created: false, replaced: true });
    }
    if meta.artifact.versions.len() >= MAX_VERSIONS {
        return Err(format!("{id} has too many versions; create a new artifact"));
    }
    let n = top.saturating_add(1);
    let file = write_version_bytes(&dir, n, &ext, s.bytes)?;
    meta.artifact.versions.push(ArtifactVersion { n, path: rel(&root, &file), created_at: now_ms(), note, size: s.bytes.len() as u64 });
    if let Err(e) = save(&dir, &meta) {
        let _ = std::fs::remove_file(&file);
        return Err(e);
    }
    meta.artifact.versions.retain(|v| version_file(workspace, &v.path).is_some());
    Ok(Saved { artifact: meta.artifact, version: n, file, created: false, replaced: false })
}

/// Lines for the system prompt naming the chat's artifacts, so later turns can update them.
pub fn prompt_list(types: &ArtifactTypes, artifacts: &[Artifact]) -> String {
    if artifacts.is_empty() {
        return String::new();
    }
    let mut out = String::from("Artifacts in this chat (update them by id):");
    for a in artifacts.iter().rev().take(30) {
        let v = a.latest().map_or(1, |v| v.n);
        out.push_str(&format!("\n- {}: \"{}\" ({}, v{v}, {})", a.id, a.title, types.of(a).label, a.filename));
    }
    out
}

// ---------------------------------------------------------------------------------------
// Tools

/// `create_artifact`, for the types in its registry.
#[derive(Clone, Default)]
pub struct CreateArtifact {
    pub types: ArtifactTypes,
}

/// `update_artifact`.
#[derive(Clone, Default)]
pub struct UpdateArtifact {
    pub types: ArtifactTypes,
}

/// `read_artifact`.
#[derive(Clone, Default)]
pub struct ReadArtifact {
    pub types: ArtifactTypes,
}

#[derive(Deserialize)]
struct CreateArgs {
    title: String,
    #[serde(default, alias = "name", alias = "file")]
    filename: String,
    content: String,
    #[serde(default)]
    note: String,
}

#[derive(Deserialize)]
struct Edit {
    find: String,
    #[serde(default, alias = "replacement", alias = "replace_with")]
    replace: String,
}

#[derive(Deserialize)]
struct UpdateArgs {
    id: String,
    #[serde(default)]
    content: Option<String>,
    #[serde(default)]
    edits: Option<Vec<Edit>>,
    #[serde(default)]
    note: String,
}

#[derive(Deserialize)]
struct ReadArgs {
    id: String,
    #[serde(default)]
    version: Option<u32>,
}

fn arg_str<'a>(args: &'a Value, key: &str) -> &'a str {
    args.get(key).and_then(Value::as_str).unwrap_or_default()
}

impl Tool for CreateArtifact {
    fn spec(&self) -> ToolSpec {
        ToolSpec {
            name: "create_artifact".into(),
            description: self.types.tool_description(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "title": {"type": "string", "description": "Short title, e.g. \"Loan calculator\""},
                    "filename": {"type": "string", "description": "File name with extension, e.g. loan-calculator.html, q3-summary.md, prices.csv"},
                    "content": {"type": "string", "description": "The whole file"},
                    "note": {"type": "string", "description": "Optional: what this first version is"}
                },
                "required": ["title", "filename", "content"]
            }),
        }
    }

    fn title(&self, args: &Value) -> String {
        format!("Made {}", clip(arg_str(args, "title"), 60))
    }

    fn pending_title(&self, args: &Value) -> String {
        format!("Make {}", clip(arg_str(args, "title"), 60))
    }

    fn access(&self, _args: &Value) -> Access {
        Access::Read
    }

    fn run(&self, args: &Value, ctx: &ToolContext<'_>) -> ToolOutput {
        match create(&self.types, args, ctx) {
            Ok(out) => out,
            Err(e) => ToolOutput::err(e),
        }
    }
}

fn create(types: &ArtifactTypes, args: &Value, ctx: &ToolContext<'_>) -> Result<ToolOutput, String> {
    let chat = chat_of(ctx)?;
    let a: CreateArgs = parse_args(args)?;
    let title = clip(&a.title, MAX_TITLE_CHARS);
    if title.is_empty() {
        return Err("give the artifact a title".into());
    }
    let (filename, ext) = clean_filename(types, &a.filename, &title)?;
    let ty = types.for_filename(&filename);
    let (content, warning) = check_content(&ty, &a.content)?;
    let (id, dir) = new_artifact_dir(ctx.workspace, &title)?;
    let result = (|| {
        let file = write_version(&dir, 1, &ext, &content)?;
        let root = sandbox::root(ctx.workspace)?;
        let version = ArtifactVersion { n: 1, path: rel(&root, &file), created_at: now_ms(), note: clip(&a.note, MAX_NOTE_CHARS), size: content.len() as u64 };
        let artifact = Artifact {
            id: id.clone(),
            kind: ty.kind,
            content_type: ty.content_type.clone(),
            title: title.clone(),
            filename: filename.clone(),
            versions: vec![version],
        };
        save(&dir, &Meta { chat: chat.to_owned(), artifact })?;
        Ok::<_, String>(file)
    })();
    let file = match result {
        Ok(f) => f,
        Err(e) => {
            let _ = std::fs::remove_dir_all(&dir);
            return Err(e);
        }
    };
    let text = format!(
        "Created the {} artifact \"{title}\" (id {id}, version 1, {filename}). It shows beside the chat. To change it later, call update_artifact with id {id}.{warning}",
        ty.label
    );
    Ok(ToolOutput { text, failed: false, changes: vec![FileChange::Created(file)] })
}

impl Tool for UpdateArtifact {
    fn spec(&self) -> ToolSpec {
        ToolSpec {
            name: "update_artifact".into(),
            description: "Save a new version of an artifact. Prefer `edits`: find/replace pairs applied in order, where each `find` is exact text that occurs exactly once \
                in the current version (include enough surrounding text to make it unique). Send `content` (the whole file) only for a rewrite. \
                Use read_artifact first if you don't have the current text."
                .into(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "id": {"type": "string", "description": "The artifact's id"},
                    "edits": {"type": "array", "items": {"type": "object", "properties": {"find": {"type": "string"}, "replace": {"type": "string"}}, "required": ["find", "replace"]}},
                    "content": {"type": "string", "description": "Or: the whole new file"},
                    "note": {"type": "string", "description": "What changed, in a few words"}
                },
                "required": ["id"]
            }),
        }
    }

    fn title(&self, args: &Value) -> String {
        format!("Updated {}", clip(arg_str(args, "id"), 60))
    }

    fn pending_title(&self, args: &Value) -> String {
        format!("Update {}", clip(arg_str(args, "id"), 60))
    }

    fn access(&self, _args: &Value) -> Access {
        Access::Read
    }

    fn run(&self, args: &Value, ctx: &ToolContext<'_>) -> ToolOutput {
        match update(&self.types, args, ctx) {
            Ok(out) => out,
            Err(e) => ToolOutput::err(e),
        }
    }
}

/// Apply find/replace edits in order. Each `find` must occur exactly once in the text as
/// it is at that point.
pub fn apply_edits(text: &str, edits: &[(String, String)]) -> Result<String, String> {
    if edits.is_empty() {
        return Err("edits is empty; give at least one {find, replace}".into());
    }
    if edits.len() > MAX_EDITS {
        return Err(format!("too many edits ({}; at most {MAX_EDITS}); send the whole file as content instead", edits.len()));
    }
    let mut out = text.to_owned();
    for (i, (find, replace)) in edits.iter().enumerate() {
        let k = i.saturating_add(1);
        if find.is_empty() {
            return Err(format!("edit {k}: `find` is empty"));
        }
        let count = out.matches(find.as_str()).count();
        match count {
            1 => out = out.replacen(find.as_str(), replace, 1),
            0 => {
                return Err(format!(
                    "edit {k}: this `find` text is not in the current version: {:?}. It must match exactly (whitespace too); call read_artifact to see the current text. No edits were applied.",
                    clip(find, 120)
                ));
            }
            n => {
                return Err(format!(
                    "edit {k}: this `find` text occurs {n} times: {:?}. Include more surrounding text so it occurs exactly once. No edits were applied.",
                    clip(find, 120)
                ));
            }
        }
        if out.len() > MAX_CONTENT_BYTES {
            return Err(format!("the edits make the file too big (over {MAX_CONTENT_BYTES} bytes)"));
        }
    }
    Ok(out)
}

fn update(types: &ArtifactTypes, args: &Value, ctx: &ToolContext<'_>) -> Result<ToolOutput, String> {
    let chat = chat_of(ctx)?;
    let a: UpdateArgs = parse_args(args)?;
    let mut meta = load_in_chat(ctx.workspace, chat, a.id.trim())?;
    let id = meta.artifact.id.clone();
    let ty = types.of(&meta.artifact);
    if !ty.text {
        return Err(format!("{id} is not a text file ({}); it can't be edited as text. Make a new one instead", ty.label));
    }
    if meta.artifact.versions.len() >= MAX_VERSIONS {
        return Err(format!("{id} has too many versions; create a new artifact"));
    }
    let latest = meta.artifact.latest().cloned().ok_or_else(|| format!("artifact {id:?} has no versions"))?;
    let new_text = match (a.content, a.edits) {
        (Some(_), Some(_)) => return Err("give either content or edits, not both".into()),
        (None, None) => return Err("give edits (find/replace pairs) or the whole new content".into()),
        (Some(content), None) => content,
        (None, Some(edits)) => {
            let file = version_file(ctx.workspace, &latest.path).ok_or_else(|| format!("the latest version of {id} is gone"))?;
            let bytes = read_capped(&file, MAX_FILE_BYTES)?;
            let current = String::from_utf8(bytes).map_err(|_| format!("{id} is not a text file"))?;
            let pairs: Vec<(String, String)> = edits.into_iter().map(|e| (e.find, e.replace)).collect();
            let out = apply_edits(&current, &pairs)?;
            if out == current {
                return Err("the edits change nothing".into());
            }
            out
        }
    };
    let (content, warning) = check_content(&ty, &new_text)?;
    let (root, dir) = artifact_dir(ctx.workspace, &id)?;
    // Version numbers are never reused, even if a version was undone.
    let ext = latest.path.rsplit_once("/v").and_then(|(_, f)| f.split_once('.')).map(|(_, e)| e.to_owned()).unwrap_or_else(|| {
        let lower = meta.artifact.filename.to_ascii_lowercase();
        types.compound_extension(&lower).unwrap_or_else(|| lower.rsplit_once('.').map_or("txt".to_owned(), |(_, e)| e.to_owned()))
    });
    let all = serde_json::from_slice::<Meta>(&read_capped(&dir.join(META_FILE), MAX_META_BYTES)?).map_err(|e| e.to_string())?;
    let n = all.artifact.versions.iter().map(|v| v.n).max().unwrap_or(0).saturating_add(1);
    let file = write_version(&dir, n, &ext, &content)?;
    let note = clip(&a.note, MAX_NOTE_CHARS);
    meta.artifact.versions = all.artifact.versions;
    meta.artifact.versions.push(ArtifactVersion { n, path: rel(&root, &file), created_at: now_ms(), note: note.clone(), size: content.len() as u64 });
    if let Err(e) = save(&dir, &meta) {
        let _ = std::fs::remove_file(&file);
        return Err(e);
    }
    let text =
        format!("Saved version {n} of \"{}\" (id {id}){}.{warning}", meta.artifact.title, if note.is_empty() { String::new() } else { format!(": {note}") });
    Ok(ToolOutput { text, failed: false, changes: vec![FileChange::Created(file)] })
}

impl Tool for ReadArtifact {
    fn spec(&self) -> ToolSpec {
        ToolSpec {
            name: "read_artifact".into(),
            description: "Read the current text of an artifact (or of an older version), e.g. before changing it with update_artifact.".into(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "id": {"type": "string"},
                    "version": {"type": "integer", "description": "Optional; default the latest"}
                },
                "required": ["id"]
            }),
        }
    }

    fn title(&self, args: &Value) -> String {
        format!("Read {}", clip(arg_str(args, "id"), 60))
    }

    fn pending_title(&self, args: &Value) -> String {
        self.title(args)
    }

    fn access(&self, _args: &Value) -> Access {
        Access::Read
    }

    fn run(&self, args: &Value, ctx: &ToolContext<'_>) -> ToolOutput {
        let result = (|| {
            let chat = chat_of(ctx)?;
            let a: ReadArgs = parse_args(args)?;
            let meta = load_in_chat(ctx.workspace, chat, a.id.trim())?;
            let v = match a.version {
                Some(n) => meta.artifact.version(n).cloned().ok_or_else(|| format!("{} has no version {n}", meta.artifact.id))?,
                None => meta.artifact.latest().cloned().ok_or("no versions")?,
            };
            let ty = self.types.of(&meta.artifact);
            if !ty.text {
                return Err(format!("{} is not a text file ({}); it can't be read as text", meta.artifact.id, ty.label));
            }
            let (_, bytes) = read_version(ctx.workspace, chat, &meta.artifact.id, v.n, MAX_FILE_BYTES)?;
            let text = String::from_utf8_lossy(&bytes);
            let shown: String = text.chars().take(MAX_READ_CHARS).collect();
            let more = if shown.len() < text.len() { "\n… [cut off]" } else { "" };
            Ok(format!(
                "{} \"{}\", version {} of {} ({}):\n{shown}{more}",
                meta.artifact.id,
                meta.artifact.title,
                v.n,
                meta.artifact.versions.len(),
                meta.artifact.filename
            ))
        })();
        match result {
            Ok(t) => ToolOutput::ok(t),
            Err(e) => ToolOutput::err(e),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::AtomicBool;

    fn run_in(tool: &dyn Tool, ws: &Path, chat: &str, args: Value) -> ToolOutput {
        let cancel = AtomicBool::new(false);
        tool.run(&args, &ToolContext::new(ws, &cancel).with_chat(chat))
    }

    fn id_of(out: &ToolOutput) -> String {
        let p = match &out.changes[..] {
            [FileChange::Created(p)] => p.to_string_lossy().into_owned(),
            other => panic!("{other:?} / {}", out.text),
        };
        parse_version_path(&p).unwrap().id
    }

    const HTML: &str = "<!doctype html><button style=\"background:red\">Go</button><button>Stop</button>";

    #[test]
    fn create_update_read_and_versions() {
        let d = tempfile::tempdir().unwrap();
        let ws = d.path();
        let out = run_in(&CreateArtifact::default(), ws, "c1", json!({"title": "Loan calculator", "filename": "loan.html", "content": HTML}));
        assert!(!out.failed, "{}", out.text);
        let id = id_of(&out);
        assert!(id.starts_with("loan-calculator-"), "{id}");
        assert!(out.text.contains(&id));
        let a = &list(ws, "c1")[0];
        assert_eq!((a.kind, a.filename.as_str(), a.versions.len()), (ArtifactKind::Html, "loan.html", 1));
        assert_eq!(a.versions[0].path, format!(".byob/artifacts/{id}/v1.html"));
        assert!(list(ws, "other").is_empty(), "artifacts are per chat");

        let out = run_in(
            &UpdateArtifact::default(),
            ws,
            "c1",
            json!({"id": id, "edits": [{"find": "background:red", "replace": "background:blue"}], "note": "blue"}),
        );
        assert!(!out.failed, "{}", out.text);
        assert_eq!(parse_version_path(&id_of_path(&out)).unwrap().version, 2);
        let (a, v2) = read_version(ws, "c1", &id, 2, MAX_FILE_BYTES).unwrap();
        assert_eq!(String::from_utf8(v2).unwrap(), HTML.replace("red", "blue"));
        assert_eq!(a.latest().unwrap().note, "blue");
        // v1 is untouched
        assert_eq!(read_version(ws, "c1", &id, 1, MAX_FILE_BYTES).unwrap().1, HTML.as_bytes());

        let read = run_in(&ReadArtifact::default(), ws, "c1", json!({"id": id}));
        assert!(read.text.contains("version 2") && read.text.contains("background:blue"), "{}", read.text);
        let other_chat = run_in(&ReadArtifact::default(), ws, "c2", json!({"id": id}));
        assert!(other_chat.failed);
        assert!(run_in(&UpdateArtifact::default(), ws, "c2", json!({"id": id, "content": "x"})).failed, "can't touch another chat's artifact");
        assert!(read_version(ws, "c2", &id, 1, MAX_FILE_BYTES).is_err());

        // a whole-content rewrite
        let out = run_in(&UpdateArtifact::default(), ws, "c1", json!({"id": id, "content": "<p>new</p>"}));
        assert!(!out.failed, "{}", out.text);
        assert_eq!(load(ws, &id).unwrap().artifact.latest().unwrap().n, 3);
        // an undone version (file removed) drops out; numbers are never reused
        std::fs::remove_file(ws.join(format!(".byob/artifacts/{id}/v3.html"))).unwrap();
        assert_eq!(load(ws, &id).unwrap().artifact.latest().unwrap().n, 2);
        let out = run_in(&UpdateArtifact::default(), ws, "c1", json!({"id": id, "content": "<p>again</p>"}));
        assert!(!out.failed, "{}", out.text);
        assert_eq!(load(ws, &id).unwrap().artifact.versions.iter().map(|v| v.n).collect::<Vec<_>>(), vec![1, 2, 4]);
    }

    /// Deleting a chat deletes its artifacts for good (they were left orphaned before).
    #[test]
    fn removing_a_chat_removes_only_its_artifacts() {
        let d = tempfile::tempdir().unwrap();
        let ws = d.path();
        for (chat, title) in [("c1", "One"), ("c1", "Two"), ("c2", "Other")] {
            assert!(!run_in(&CreateArtifact::default(), ws, chat, json!({"title": title, "filename": "a.md", "content": "# hi"})).failed);
        }
        assert_eq!(remove_chat(ws, "c1"), 2);
        assert!(list(ws, "c1").is_empty());
        assert_eq!(list(ws, "c2").len(), 1);
        assert_eq!(std::fs::read_dir(ws.join(ARTIFACT_DIR)).unwrap().count(), 1, "nothing left behind on disk");
        assert_eq!(remove_chat(ws, "c1"), 0);
        assert_eq!(remove_chat(&ws.join("missing"), "c2"), 0);
    }

    fn id_of_path(out: &ToolOutput) -> String {
        match &out.changes[..] {
            [FileChange::Created(p)] => p.to_string_lossy().into_owned(),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn edits_must_match_exactly_once() {
        let t = "a b a c";
        assert_eq!(apply_edits(t, &[("b".into(), "B".into()), ("c".into(), "C".into())]).unwrap(), "a B a C");
        let e = apply_edits(t, &[("a".into(), "x".into())]).unwrap_err();
        assert!(e.contains("occurs 2 times") && e.contains("edit 1"), "{e}");
        let e = apply_edits(t, &[("b".into(), "B".into()), ("zz".into(), "x".into())]).unwrap_err();
        assert!(e.contains("edit 2") && e.contains("not in the current version") && e.contains("read_artifact"), "{e}");
        // edits see the text as earlier edits left it
        assert_eq!(apply_edits(t, &[("b".into(), "q".into()), ("q a".into(), "r".into())]).unwrap(), "a r c");
        assert!(apply_edits(t, &[]).is_err());
        assert!(apply_edits(t, &[(String::new(), "x".into())]).is_err());
        let many: Vec<(String, String)> = (0..=MAX_EDITS).map(|i| (format!("{i}"), String::new())).collect();
        assert!(apply_edits(t, &many).unwrap_err().contains("too many"));
        let big = "x".repeat(MAX_CONTENT_BYTES);
        assert!(apply_edits("ab", &[("a".into(), big)]).unwrap_err().contains("too big"));
    }

    #[test]
    fn failed_edits_write_nothing() {
        let d = tempfile::tempdir().unwrap();
        let ws = d.path();
        let id = id_of(&run_in(&CreateArtifact::default(), ws, "c", json!({"title": "T", "filename": "t.html", "content": HTML})));
        let out = run_in(&UpdateArtifact::default(), ws, "c", json!({"id": id, "edits": [{"find": "button", "replace": "x"}]}));
        assert!(out.failed && out.text.contains("occurs 4 times"), "{}", out.text);
        assert!(out.changes.is_empty());
        assert_eq!(load(ws, &id).unwrap().artifact.versions.len(), 1);
        let both = run_in(&UpdateArtifact::default(), ws, "c", json!({"id": id, "content": "x", "edits": [{"find": "Go", "replace": "x"}]}));
        assert!(both.failed && both.text.contains("not both"));
        assert!(run_in(&UpdateArtifact::default(), ws, "c", json!({"id": id})).failed);
        assert!(run_in(&UpdateArtifact::default(), ws, "c", json!({"id": "../../etc", "content": "x"})).failed);
        let same = run_in(&UpdateArtifact::default(), ws, "c", json!({"id": id, "edits": [{"find": "Go", "replace": "Go"}]}));
        assert!(same.failed && same.text.contains("change nothing"));
    }

    #[test]
    fn create_validates_names_kinds_and_sizes() {
        let d = tempfile::tempdir().unwrap();
        let ws = d.path();
        let c = |args: Value| run_in(&CreateArtifact::default(), ws, "c", args);
        assert!(c(json!({"title": "T", "filename": "noext", "content": "x"})).text.contains("extension"));
        assert!(c(json!({"title": "T", "filename": "p.png", "content": "x"})).text.contains("images"));
        assert!(c(json!({"title": "", "filename": "a.md", "content": "x"})).failed);
        assert!(c(json!({"title": "T", "filename": "a.md", "content": "  "})).failed);
        let big = "x".repeat(MAX_CONTENT_BYTES + 1);
        assert!(c(json!({"title": "T", "filename": "a.md", "content": big})).text.contains("too big"));
        assert!(c(json!({"title": "T", "filename": "a.chart.json", "content": "{nope"})).failed);
        // nothing half-made is left behind
        assert!(list(ws, "c").is_empty());
        let root = ws.join(ARTIFACT_DIR);
        assert_eq!(std::fs::read_dir(&root).map(|r| r.count()).unwrap_or(0), 0);

        let md = c(json!({"title": "Q3 summary", "filename": "../../q3.md", "content": "# Q3\n"}));
        assert!(!md.failed, "{}", md.text);
        let chart = c(json!({"title": "Sales", "filename": "sales.chart.json",
            "content": r#"{"type":"bar","x":"m","series":["v"],"data":[{"m":"a","v":1}]}"#}));
        assert!(!chart.failed, "{}", chart.text);
        let csv = c(json!({"title": "Prices", "filename": "prices.csv", "content": "a,b\n1,2\n"}));
        let net = c(json!({"title": "Net", "filename": "n.html", "content": "<script src=\"https://cdn.example/x.js\"></script>"}));
        assert!(net.text.contains("blocked"), "{}", net.text);
        let kinds: Vec<(ArtifactKind, String)> = list(ws, "c").into_iter().map(|a| (a.kind, a.filename)).collect();
        assert_eq!(
            kinds,
            vec![
                (ArtifactKind::Markdown, "q3.md".into()),
                (ArtifactKind::Chart, "sales.chart.json".into()),
                (ArtifactKind::Text, "prices.csv".into()),
                (ArtifactKind::Html, "n.html".into())
            ]
        );
        assert!(!csv.failed);
        assert!(Path::new(&id_of_path(&chart)).ends_with("v1.chart.json"));
        // no chat, no artifacts
        let cancel = AtomicBool::new(false);
        assert!(CreateArtifact::default().run(&json!({"title": "T", "filename": "a.md", "content": "x"}), &ToolContext::new(ws, &cancel)).failed);
    }

    /// A type registered from outside: its file names, check and label are used, its
    /// content type is kept (13.0 clients see text), and edits go through its check too.
    #[test]
    fn a_registered_type_is_used_end_to_end() {
        struct Rows;
        impl crate::ArtifactHooks for Rows {
            fn check(&self, content: &str) -> Result<(String, String), String> {
                if content.lines().all(|l| l.split(',').count() == 2) {
                    Ok((content.to_owned(), " Two columns.".into()))
                } else {
                    Err("every row needs two cells".into())
                }
            }
        }
        let types = ArtifactTypes::builtin();
        types.register(ArtifactType::new("text/csv", "table", ArtifactKind::Text).suffixes(&[".csv"]).hooks(std::sync::Arc::new(Rows)));
        let d = tempfile::tempdir().unwrap();
        let ws = d.path();
        let create = CreateArtifact { types: types.clone() };
        assert!(run_in(&create, ws, "c", json!({"title": "P", "filename": "p.csv", "content": "a,b,c"})).failed);
        let out = run_in(&create, ws, "c", json!({"title": "P", "filename": "p.csv", "content": "a,b\n1,2"}));
        assert!(out.text.contains("Created the table artifact") && out.text.ends_with("Two columns."), "{}", out.text);
        let a = &list(ws, "c")[0];
        assert_eq!((a.kind, a.content_type.as_str()), (ArtifactKind::Text, "text/csv"));
        let update = UpdateArtifact { types: types.clone() };
        assert!(run_in(&update, ws, "c", json!({"id": a.id, "edits": [{"find": "1,2", "replace": "1,2,3"}]})).failed);
        assert!(!run_in(&update, ws, "c", json!({"id": a.id, "edits": [{"find": "1,2", "replace": "3,4"}]})).failed);
        assert!(prompt_list(&types, &list(ws, "c")).contains("(table, v2, p.csv)"));
        // Without the registration (say the plugin is gone) it is plain text again.
        assert!(prompt_list(&ArtifactTypes::builtin(), &list(ws, "c")).contains("(file, v2, p.csv)"));
    }

    #[cfg(unix)]
    #[test]
    fn never_writes_through_a_linked_artifact_folder() {
        let outside = tempfile::tempdir().unwrap();
        let d = tempfile::tempdir().unwrap();
        std::fs::create_dir(d.path().join(".byob")).unwrap();
        std::os::unix::fs::symlink(outside.path(), d.path().join(ARTIFACT_DIR)).unwrap();
        let out = run_in(&CreateArtifact::default(), d.path(), "c", json!({"title": "T", "filename": "a.md", "content": "x"}));
        assert!(out.failed, "{}", out.text);
        assert_eq!(std::fs::read_dir(outside.path()).unwrap().count(), 0);
    }

    #[test]
    fn charts_and_images_are_adopted() {
        let d = tempfile::tempdir().unwrap();
        let ws = d.path();
        std::fs::create_dir(ws.join("charts")).unwrap();
        std::fs::write(ws.join("charts/Sales.chart.json"), "{}").unwrap();
        std::fs::write(ws.join("plot.png"), "png").unwrap();
        let media = vec![MediaRef::new("charts/Sales.chart.json", 2), MediaRef::new("plot.png", 3), MediaRef::new("notes.txt", 1)];
        let refs = for_call(ws, "c", &[], &media);
        assert_eq!(refs.len(), 2);
        let list = list(ws, "c");
        assert_eq!(list.iter().map(|a| (a.kind, a.title.as_str())).collect::<Vec<_>>(), vec![(ArtifactKind::Chart, "Sales"), (ArtifactKind::Image, "plot")]);
        assert_eq!(list[1].versions[0].path, "plot.png");
        // an artifact tool's own version is recognised, not adopted again
        let p = ws.join(".byob/artifacts/x-1/v3.md");
        assert_eq!(for_call(ws, "c", &[FileChange::Created(p)], &media), vec![ArtifactRef { id: "x-1".into(), version: 3 }]);
    }

    #[test]
    fn version_paths() {
        assert_eq!(parse_version_path(".byob/artifacts/a-1/v12.chart.json"), Some(ArtifactRef { id: "a-1".into(), version: 12 }));
        assert_eq!(parse_version_path("/w/.byob/artifacts/a/v1.html"), Some(ArtifactRef { id: "a".into(), version: 1 }));
        for bad in [".byob/artifacts/a/artifact.json", ".byob/artifacts/A/v1.md", ".byob/artifacts/a/v0.md", "charts/x.chart.json", ".byob/artifacts/a/b/v1.md"]
        {
            assert_eq!(parse_version_path(bad), None, "{bad}");
        }
    }

    #[test]
    fn prompt_lists_latest_versions() {
        let d = tempfile::tempdir().unwrap();
        let id = id_of(&run_in(&CreateArtifact::default(), d.path(), "c", json!({"title": "Calc", "filename": "calc.html", "content": HTML})));
        let p = prompt_list(&ArtifactTypes::builtin(), &list(d.path(), "c"));
        assert!(p.contains(&format!("{id}: \"Calc\" (app, v1, calc.html)")), "{p}");
        assert_eq!(prompt_list(&ArtifactTypes::builtin(), &[]), "");
    }

    #[test]
    fn bytes_from_a_plugin_become_versions_and_drafts_replace_one() {
        let d = tempfile::tempdir().unwrap();
        let types = ArtifactTypes::builtin();
        types.register(ArtifactType::new("application/x-sheet", "spreadsheet", ArtifactKind::Text).suffixes(&[".xlsx"]).binary());
        types.register(ArtifactType::new("text/csv", "table", ArtifactKind::Text).suffixes(&[".csv"]));
        let save = |s: SaveBytes<'_>| save_bytes(&types, d.path(), &s);
        // New: needs a title.
        assert!(save(SaveBytes { chat: "c", bytes: b"a,b\n1,2\n", ..Default::default() }).unwrap_err().contains("title"));
        let a = save(SaveBytes { chat: "c", title: Some("Sales"), filename: Some("sales.csv"), bytes: b"a,b\n1,2\n", ..Default::default() }).unwrap();
        assert!(a.created && a.version == 1 && a.artifact.content_type == "text/csv", "{a:?}");
        let id = a.artifact.id.clone();
        assert_eq!(parse_version_path(&a.file.to_string_lossy()), Some(ArtifactRef { id: id.clone(), version: 1 }));
        // A new file name re-types it; binary bytes are kept as they are.
        let bin = [0u8, 159, 146, 150, 1, 2];
        let b = save(SaveBytes { chat: "c", id: Some(&id), filename: Some("sales.xlsx"), bytes: &bin, note: "imported", ..Default::default() }).unwrap();
        assert_eq!((b.version, b.created, b.replaced), (2, false, false));
        assert_eq!((b.artifact.filename.as_str(), b.artifact.content_type.as_str()), ("sales.xlsx", "application/x-sheet"));
        assert!(b.file.ends_with("v2.xlsx"));
        assert_eq!(std::fs::read(&b.file).unwrap(), bin);
        // A draft over the latest replaces it; over an older one it is a new version.
        let c = save(SaveBytes { chat: "c", id: Some(&id), bytes: b"draft", replace: Some(2), ..Default::default() }).unwrap();
        assert_eq!((c.version, c.replaced), (2, true));
        assert_eq!(std::fs::read(&c.file).unwrap(), b"draft");
        assert_eq!(load(d.path(), &id).unwrap().artifact.version(2).unwrap().size, 5);
        let e = save(SaveBytes { chat: "c", id: Some(&id), bytes: b"again", replace: Some(1), ..Default::default() }).unwrap();
        assert_eq!((e.version, e.replaced), (3, false));
        assert_eq!(e.artifact.versions.iter().map(|v| v.n).collect::<Vec<_>>(), [1, 2, 3]);
        // Another chat's artifact, or nothing to save: refused.
        assert!(save(SaveBytes { chat: "other", id: Some(&id), bytes: b"x", ..Default::default() }).is_err());
        assert!(save(SaveBytes { chat: "c", id: Some(&id), bytes: b"", ..Default::default() }).is_err());
        assert!(save(SaveBytes { chat: "c", id: Some("nope"), bytes: b"x", ..Default::default() }).is_err());
    }
}