mindfork 0.11.0

A terminal AI chat written in Rust: local models via llama.cpp or OpenAI, Anthropic, Gemini and Grok in the cloud, with persistent memory, notes, RAG and tools.
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
//! Orchestrator tests — mapping domain messages to the engine format. Part of the
//! [`super`] module (fixtures in mod.rs). See docs/history/refactoring-god-objects.md, stage 3.

use super::*;
use crate::app::orchestrator::request::inject_attachments;
use crate::entities::attachment::{AttachMode, Attachment};
use crate::shared::api::ChatRequest;
use crate::shared::config::{AttachmentSettings, CompactionSettings};

fn ru() -> &'static crate::shared::i18n::Locale {
    crate::shared::i18n::locale(crate::shared::i18n::Lang::Ru)
}

/// "Nothing is indexed" — the default state for the pure injection tests (the
/// semantic index is built in the background and is checked in its own tests).
const NO_INDEX: &[uuid::Uuid] = &[];

fn att(name: &str, text: &str, mode: AttachMode) -> Attachment {
    let bytes = text.len();
    Attachment::new(name, format!("/tmp/{name}"), text.to_string(), bytes, mode)
}

/// A request built with default injection settings — the shape almost every test
/// here wants. The two knobs that tests actually vary get parameters; the rest
/// would only be noise repeated at each call site.
fn request_of(chat: &Chat, compaction: &CompactionSettings, history_tools: bool) -> ChatRequest {
    build_request(
        chat,
        SamplingConfig::default(),
        vec![],
        &PromptContext {
            attachments: &AttachmentSettings::default(),
            compaction,
            indexed: NO_INDEX,
            // The chat-files block has its own tests below; the shared helper builds
            // requests for a turn that stages nothing.
            files: &[],
            python_dirs: ("/w/in", "/w/out"),
            history_tools,
            // The workspace block has its own tests below; this shared helper
            // builds requests for chats with no project, where the list is moot.
            offered_tools: &[],
            loc: ru(),
        },
    )
}

/// The chat-files block names each item's `#N` — the one the user sees — and the name the
/// code will open in `/w/in`, so the model can write the path before it ever sees a result
/// (docs/history/sandbox-file-exchange.md §12 T3, T5). A turn that can stage nothing carries no
/// block at all, and never names a tool it does not have.
#[test]
fn the_chat_files_block_names_handles_and_staged_names() {
    let p = Profile::new("X", "Ты — X.");
    let chat = Chat::from_profile(&p, "c");
    let attached = att("report.pdf", "the extracted text", AttachMode::ByReference);
    let items = crate::features::chat_inputs::items(
        std::slice::from_ref(&attached),
        &[],
        &[],
        std::path::Path::new("/d"),
    );
    let req = build_request(
        &chat,
        SamplingConfig::default(),
        vec![],
        &PromptContext {
            attachments: &AttachmentSettings::default(),
            compaction: &CompactionSettings::default(),
            indexed: NO_INDEX,
            files: &items,
            python_dirs: ("/w/in", "/w/out"),
            history_tools: false,
            offered_tools: &[],
            loc: ru(),
        },
    );
    let system = req.system.unwrap_or_default();
    assert!(system.contains("#1 report.pdf"), "{system}");
    // What the extractor read is staged as text, under the name the block states.
    assert!(system.contains("/w/in/report.pdf.txt"), "{system}");
    assert!(system.contains("python_exec"), "{system}");

    // The same chat on a turn that stages nothing: no block, and no mention of /w/in.
    let bare = request_of(&chat, &CompactionSettings::default(), false)
        .system
        .unwrap_or_default();
    assert!(!bare.contains("/w/in"), "{bare}");

    // The same list in Local mode names the folders that mode actually uses (§14 V2):
    // one block, two spellings, and never the guest's path on the host.
    let local = build_request(
        &chat,
        SamplingConfig::default(),
        vec![],
        &PromptContext {
            attachments: &AttachmentSettings::default(),
            compaction: &CompactionSettings::default(),
            indexed: NO_INDEX,
            files: &items,
            python_dirs: crate::shared::config::PythonMode::Local.dirs(),
            history_tools: false,
            offered_tools: &[],
            loc: ru(),
        },
    )
    .system
    .unwrap_or_default();
    assert!(local.contains("#1 report.pdf"), "{local}");
    assert!(local.contains("in/report.pdf.txt"), "{local}");
    assert!(!local.contains("/w/"), "{local}");
}

#[test]
fn build_request_puts_system_aside_and_maps_roles() {
    let mut p = Profile::new("X", "Ты — X.");
    p.greeting = Some("Здравствуйте!".into());
    let mut chat = Chat::from_profile(&p, "c");
    chat.push_message(Message::assistant("Здравствуйте!"));
    chat.push_message(Message::user("привет"));

    let req = request_of(&chat, &CompactionSettings::default(), true);
    assert_eq!(req.system.as_deref(), Some("Ты — X."));
    assert_eq!(req.messages.len(), 2);
}

#[test]
fn build_request_appends_attached_files_to_system() {
    let p = Profile::new("X", "Ты — X.");
    let mut chat = Chat::from_profile(&p, "c");
    chat.push_message(Message::user("что в файле?"));
    chat.attachments
        .push(att("notes.md", "секретное число 4242", AttachMode::Inline));

    let req = request_of(&chat, &CompactionSettings::default(), true);
    let system = req.system.expect("system with the attachment block");
    // The chat's own system message stays first, the block is appended.
    assert!(system.starts_with("Ты — X."), "{system}");
    assert!(system.contains("notes.md"), "{system}");
    assert!(system.contains("секретное число 4242"), "{system}");
    // Messages are untouched: the file doesn't pollute the conversation.
    assert_eq!(req.messages.len(), 1);
    assert_eq!(req.messages[0].content, "что в файле?");
}

#[test]
fn inject_attachments_is_a_noop_without_attachments() {
    let system = Some("Ты — X.".to_string());
    assert_eq!(
        inject_attachments(
            system.clone(),
            &[],
            &AttachmentSettings::default(),
            NO_INDEX,
            ru()
        ),
        system
    );
    assert_eq!(
        inject_attachments(None, &[], &AttachmentSettings::default(), NO_INDEX, ru()),
        None
    );
}

#[test]
fn inline_carries_full_text_by_reference_only_an_excerpt() {
    // 20 estimated tokens ≈ 80 bytes ≈ 40 Cyrillic characters — enough for the
    // opening phrase, nowhere near the tail.
    let cfg = AttachmentSettings {
        excerpt_tokens: 20,
        ..Default::default()
    };
    let tail = "ХВОСТ-МАРКЕР";
    let long = format!("начало документа, довольно длинное вступление… {tail}");
    let items = vec![
        att("small.txt", "короткий текст", AttachMode::Inline),
        att("big.txt", &long, AttachMode::ByReference),
    ];
    let out = inject_attachments(None, &items, &cfg, NO_INDEX, ru()).expect("a block");

    // Inline — in full.
    assert!(out.contains("короткий текст"), "{out}");
    // By reference — the head is shown, the tail is not.
    assert!(out.contains("начало документа"), "{out}");
    assert!(
        !out.contains(tail),
        "the by-reference tail must stay out of the prompt: {out}"
    );
    // Both are announced by name, and the header marks the content as data.
    assert!(
        out.contains("small.txt") && out.contains("big.txt"),
        "{out}"
    );
    assert!(out.contains("ДАННЫЕ"), "{out}");
}

/// Regression for a defect seen on a live run: the by-reference entry showed an
/// excerpt but never said **how to read the rest**, so the model improvised with
/// the wrong tools (`fs_read` into the sandbox, then `web_search`) and ended up
/// asking the user for the impossible. The entry must name `attachment_read`,
/// state the page range, and say the file is unreachable by other means.
#[test]
fn by_reference_entry_tells_the_model_how_to_read_the_rest() {
    let cfg = AttachmentSettings {
        excerpt_tokens: 5,
        page_tokens: 10,
        ..Default::default()
    };
    let long = "слово ".repeat(200);
    let items = vec![att("big.txt", &long, AttachMode::ByReference)];
    let out = inject_attachments(None, &items, &cfg, NO_INDEX, ru()).unwrap();

    assert!(
        out.contains("attachment_read"),
        "the model must be told which tool reads the rest: {out}"
    );
    let pages = items[0].page_count(cfg.page_tokens);
    assert!(pages > 1, "the fixture must span several pages");
    assert!(
        out.contains(&pages.to_string()),
        "the page range must be stated ({pages} pages): {out}"
    );

    // An inline file needs no such pointer — it is already there in full.
    let inline = vec![att("small.txt", "коротко", AttachMode::Inline)];
    let out = inject_attachments(None, &inline, &cfg, NO_INDEX, ru()).unwrap();
    assert!(!out.contains("attachment_read"), "{out}");
}

/// Search is only offered for a file that actually has an index — promising it
/// over an unindexed file (no embedder configured) would send the model down a
/// dead end. Page reading is offered either way: it is the guaranteed path.
#[test]
fn search_is_offered_only_for_an_indexed_file() {
    let cfg = AttachmentSettings {
        excerpt_tokens: 5,
        page_tokens: 10,
        ..Default::default()
    };
    let items = vec![att(
        "big.txt",
        &"слово ".repeat(200),
        AttachMode::ByReference,
    )];

    let without = inject_attachments(None, &items, &cfg, NO_INDEX, ru()).unwrap();
    assert!(
        !without.contains("attachment_search"),
        "an unindexed file must not advertise search: {without}"
    );
    assert!(without.contains("attachment_read"), "{without}");

    let with = inject_attachments(None, &items, &cfg, &[items[0].id], ru()).unwrap();
    assert!(with.contains("attachment_search"), "{with}");
    assert!(
        with.contains("attachment_read"),
        "the guaranteed path stays advertised: {with}"
    );
}

#[test]
fn fence_widens_so_a_file_cannot_close_its_own_section() {
    // A file quoting the default fence must not be able to end its section and
    // have the rest read as instructions.
    let hostile = "текст >>> и ещё >>> внутри";
    let items = vec![att("evil.md", hostile, AttachMode::Inline)];
    let out =
        inject_attachments(None, &items, &AttachmentSettings::default(), NO_INDEX, ru()).unwrap();
    assert!(
        out.contains(hostile),
        "the content is still delivered: {out}"
    );
    // The fence around it is wider than any run of '>' in the content.
    assert!(
        out.contains(">>>>") && out.contains("<<<<"),
        "the fence must widen past the content's own run: {out}"
    );
}

#[test]
fn block_stands_alone_when_the_chat_has_no_system_message() {
    let items = vec![att("a.txt", "содержимое", AttachMode::Inline)];
    let out =
        inject_attachments(None, &items, &AttachmentSettings::default(), NO_INDEX, ru()).unwrap();
    assert!(out.starts_with('['), "the block leads: {out}");
    assert!(out.contains("содержимое"));
}

#[test]
fn block_is_localized_for_all_langs() {
    let items = vec![att("a.txt", "payload", AttachMode::ByReference)];
    for &lang in crate::shared::i18n::Lang::ALL {
        let loc = crate::shared::i18n::locale(lang);
        let out = inject_attachments(None, &items, &AttachmentSettings::default(), NO_INDEX, loc)
            .unwrap();
        assert!(!out.contains('{') && !out.contains('}'), "{lang:?}: {out}");
        if lang == crate::shared::i18n::Lang::En {
            assert!(
                !out.chars().any(|c| ('\u{0400}'..='\u{04FF}').contains(&c)),
                "Cyrillic leaked into the en block: {out}"
            );
        }
    }
}

// ---------------------------------------------------------------------------
// History compression (spec §6.7): the request carries a summary plus the
// verbatim tail, while `chat.messages` stays whole.
// ---------------------------------------------------------------------------

/// A chat of `n` user/assistant pairs, compacted at message index `upto`.
fn compacted_chat(n: usize, upto: usize, summary: &str) -> Chat {
    let p = Profile::new("X", "Ты — X.");
    let mut chat = Chat::from_profile(&p, "c");
    for i in 0..n {
        chat.push_message(Message::user(format!("вопрос {i}")));
        chat.push_message(Message::assistant(format!("ответ {i}")));
    }
    chat.compaction = Some(crate::entities::chat::Compaction {
        summary: summary.into(),
        upto,
        boundary_id: chat.messages[upto].id,
        compacted_at: chrono::Utc::now(),
        rolls: 1,
    });
    chat
}

#[test]
fn compaction_replaces_the_prefix_with_a_summary_block() {
    let chat = compacted_chat(5, 6, "Ранее: обсудили хранилище, выбрали SQLite.");
    let req = request_of(&chat, &CompactionSettings::default(), true);
    // The persona stays first, the block is appended after it — ordered by
    // volatility so the most stable content keeps its prefix (spec §6.6).
    let system = req.system.expect("system with the summary block");
    assert!(system.starts_with("Ты — X."), "{system}");
    assert!(system.contains("выбрали SQLite"), "{system}");
    // Only the verbatim tail is sent, and the whole history is still on the chat.
    assert_eq!(req.messages.len(), 4);
    assert_eq!(chat.messages.len(), 10);
}

/// The block must name the read-back tools **only when this turn offers them**.
/// They normally travel together (sub-decision S12 gates both on the same
/// `compaction_view`), but a profile can switch the two tools off — and a block
/// that names a tool the model does not have is the dead end the sentence exists
/// to prevent, the fourth instance of that defect class in this codebase.
#[test]
fn the_block_names_the_read_back_tools_only_when_they_are_offered() {
    let chat = compacted_chat(5, 6, "Ранее: выбрали SQLite.");
    let block = |history_tools| {
        request_of(&chat, &CompactionSettings::default(), history_tools)
            .system
            .expect("system with the summary block")
    };

    let with = block(true);
    assert!(
        with.contains("history_search") && with.contains("history_read"),
        "{with}"
    );

    let without = block(false);
    assert!(
        !without.contains("history_search") && !without.contains("history_read"),
        "a tool the model does not have must not be named: {without}"
    );
    // Both wordings still carry the summary and say the block is a record.
    for system in [&with, &without] {
        assert!(system.contains("выбрали SQLite"), "{system}");
        assert!(system.contains("ДАННЫЕ"), "{system}");
    }
}

#[test]
fn the_master_switch_off_makes_compression_inert() {
    // Fork F10: off means the request is byte-for-byte what it was before the
    // feature existed — no block, no truncation — and the stored summary is
    // merely dormant, never discarded.
    let chat = compacted_chat(5, 6, "Ранее: выбрали SQLite.");
    let off = CompactionSettings {
        enabled: false,
        ..Default::default()
    };
    let req = request_of(&chat, &off, true);
    assert_eq!(req.system.as_deref(), Some("Ты — X."));
    assert_eq!(req.messages.len(), 10);
    assert!(
        chat.compaction.is_some(),
        "the summary must survive the flip"
    );
}

#[test]
fn a_vanished_boundary_falls_back_to_the_whole_history() {
    // The boundary is re-found by id. If the message is gone the summary can no
    // longer be placed, so the full history is sent rather than a summary
    // silently covering the wrong span.
    let mut chat = compacted_chat(5, 6, "Ранее: выбрали SQLite.");
    chat.messages.remove(6);
    let req = request_of(&chat, &CompactionSettings::default(), true);
    assert_eq!(req.system.as_deref(), Some("Ты — X."));
    assert_eq!(req.messages.len(), 9);
}

/// The workspace block: present only with a project, naming exactly the tools
/// the turn offers, and absent entirely otherwise — the property that keeps
/// every existing conversation's request unchanged (spec §9.12).
#[test]
fn the_workspace_block_appears_only_with_a_project() {
    use crate::app::orchestrator::request::inject_workspace;
    use crate::entities::workspace::Workspace;
    use crate::features::tools::code;

    let readers: Vec<String> = vec![code::CODE_READ_ID.into(), code::CODE_GREP_ID.into()];
    let ws = Workspace::new("D:/Projects/app");
    let none = inject_workspace(Some("persona".into()), None, &readers, ru());
    assert_eq!(
        none,
        Some("persona".into()),
        "no project must leave the system prompt untouched"
    );

    let with = inject_workspace(Some("persona".into()), Some(&ws), &readers, ru()).unwrap();
    assert!(
        with.starts_with("persona"),
        "the persona stays first: {with}"
    );
    assert!(with.contains("D:/Projects/app"), "{with}");
    assert!(with.contains("app"), "the name is shown too: {with}");
    // It must name the tools that reach the project, or the model improvises
    // with the wrong ones (docs/lessons.md §4)…
    assert!(with.contains("code_read"), "{with}");
    // …and only those. `code_edit` is not in this turn's set, and a block that
    // offers editing to a profile without it costs a wasted round.
    assert!(
        !with.contains("code_edit"),
        "a tool this turn does not have must not be named: {with}"
    );

    // Attached, but the profile has every tool off: the block must say the
    // project is out of reach rather than advertise an absent capability.
    let unreachable = inject_workspace(Some("persona".into()), Some(&ws), &[], ru()).unwrap();
    assert!(unreachable.contains("D:/Projects/app"), "{unreachable}");
    assert!(
        !unreachable.contains("code_read"),
        "a tool this turn does not have must not be named: {unreachable}"
    );
}

/// A command tool's line is quoted into the block verbatim, and a slot with no
/// line contributes nothing.
///
/// Both halves matter: the model is *told* what `code_build` will run, because
/// that text is the only thing it knows about a command it cannot change — and
/// a slot the turn cannot run must not appear, or the model will call a tool
/// that is not in its schema list.
#[test]
fn the_block_quotes_the_command_lines_it_can_run() {
    use crate::app::orchestrator::request::inject_workspace;
    use crate::entities::workspace::{CommandSlot, Workspace};
    use crate::features::tools::code;

    let mut ws = Workspace::new("/home/u/app");
    ws.set_command(CommandSlot::Build, Some("cargo build --offline".into()));
    ws.set_command(CommandSlot::Test, Some("cargo test --offline".into()));

    let offered: Vec<String> = vec![code::CODE_BUILD_ID.into()];
    let block = inject_workspace(None, Some(&ws), &offered, ru()).unwrap();
    assert!(block.contains("cargo build --offline"), "{block}");
    assert!(
        !block.contains("cargo test"),
        "a slot whose tool is not offered must not be described: {block}"
    );
    assert!(
        !block.contains("code_test"),
        "…and neither must its tool: {block}"
    );
}

/// A request for a chat with no project is byte-identical to one built before
/// the feature — the safety property for every stored conversation.
#[test]
fn a_chat_without_a_project_builds_an_unchanged_request() {
    let p = Profile::new("X", "persona");
    let mut chat = Chat::from_profile(&p, "c");
    chat.push_message(Message::user("hi"));
    assert!(chat.workspace.is_none());
    let req = request_of(&chat, &CompactionSettings::default(), false);
    assert!(
        req.system.is_none() || !req.system.as_deref().unwrap().contains("code_read"),
        "no project must add nothing: {:?}",
        req.system
    );
}

/// A task notification (spec §9.3.2, docs/research/background-subagents.md
/// §4.4) is a `System` row for the feed and **user text** on the wire: merged
/// in front of the user message that follows it — one message, the
/// notification first — alone when it is the last user-side row (a turn the
/// app started on it), and alone before an assistant row.
#[test]
fn a_notification_is_user_text_merged_into_the_next_user_message() {
    let p = Profile::new("X", "Ты — X.");
    let run = uuid::Uuid::new_v4();
    let mut chat = Chat::from_profile(&p, "c");
    chat.push_message(Message::user("delegate"));
    chat.push_message(Message::assistant("started"));
    chat.push_message(Message::notification(
        run,
        "[note] the run finished: harsh view",
    ));
    chat.push_message(Message::user("thanks"));

    let req = request_of(&chat, &CompactionSettings::default(), false);
    let roles: Vec<_> = req.messages.iter().map(|m| m.role).collect();
    assert_eq!(
        roles,
        [
            crate::shared::api::contract::ApiRole::User,
            crate::shared::api::contract::ApiRole::Assistant,
            crate::shared::api::contract::ApiRole::User
        ],
        "{:?}",
        req.messages
    );
    assert_eq!(
        req.messages[2].content,
        "[note] the run finished: harsh view

thanks"
    );

    // Alone at the end: the wake turn's request.
    chat.messages.pop();
    let req = request_of(&chat, &CompactionSettings::default(), false);
    assert_eq!(req.messages.len(), 3);
    assert_eq!(
        req.messages[2].role,
        crate::shared::api::contract::ApiRole::User
    );
    assert_eq!(
        req.messages[2].content,
        "[note] the run finished: harsh view"
    );

    // Alone before an assistant row: the reply it woke, replayed.
    chat.push_message(Message::assistant("the review is in"));
    let req = request_of(&chat, &CompactionSettings::default(), false);
    let roles: Vec<_> = req.messages.iter().map(|m| m.role).collect();
    assert_eq!(
        roles,
        [
            crate::shared::api::contract::ApiRole::User,
            crate::shared::api::contract::ApiRole::Assistant,
            crate::shared::api::contract::ApiRole::User,
            crate::shared::api::contract::ApiRole::Assistant
        ]
    );
    // A plain `System` row (a dialogue director's note) still goes nowhere.
    chat.push_message(Message::new(MessageRole::System, "Director: wrap up"));
    let req = request_of(&chat, &CompactionSettings::default(), false);
    assert_eq!(req.messages.len(), 4);
}

/// On an engine that takes no images, the request's copy of each image becomes a marker
/// naming it, after the message's own text (docs/research/history-images-no-vision.md
/// §2.2 — a silent drop made models deny what the picture held). A tool result is treated
/// like a user message, an image-only message becomes the marker alone, an unlabelled
/// image still gets one, and a message without images is left byte-for-byte alone.
#[test]
fn withheld_images_become_markers_that_name_them() {
    use crate::app::orchestrator::request::withhold_images;
    use crate::shared::api::{ApiImage, ApiMessage};

    let loc = crate::shared::i18n::locale(crate::shared::i18n::Lang::En);
    let label = |n: usize, name: &str| {
        Some(loc.tf(
            "prompt.images.label",
            &[("n", &n.to_string()), ("name", name)],
        ))
    };
    let image = |label: Option<String>| ApiImage::new("image/png", "AAAA", label);
    let mut messages = vec![
        ApiMessage::user("what is this?").with_images(vec![
            image(label(1, "figure.png")),
            image(label(2, "chart.png")),
        ]),
        ApiMessage::assistant("A blue field."),
        ApiMessage::tool("c1", "rendered").with_images(vec![image(label(1, "tool-image-1.png"))]),
        ApiMessage::user("").with_images(vec![image(None)]),
        ApiMessage::user("no pictures here"),
    ];

    assert_eq!(withhold_images(&mut messages, loc), 4);

    assert!(messages.iter().all(|m| m.images.is_empty()));
    let marker = |name: &str| loc.tf("prompt.images.withheld", &[("image", name)]);
    assert_eq!(
        messages[0].content,
        format!(
            "what is this?\n\n{}\n{}",
            marker("Image #1 — \"figure.png\""),
            marker("Image #2 — \"chart.png\"")
        )
    );
    assert_eq!(messages[1].content, "A blue field.");
    assert_eq!(
        messages[2].content,
        format!("rendered\n\n{}", marker("Image #1 — \"tool-image-1.png\""))
    );
    assert_eq!(
        messages[3].content,
        marker(loc.t("prompt.images.withheld_unnamed"))
    );
    assert_eq!(messages[4].content, "no pictures here");
    assert_eq!(
        marker("Image #1 — \"figure.png\""),
        "[Image #1 — \"figure.png\" is not included: the current model does not accept images. \
         You have not seen it — do not describe what it shows; say that you cannot see it.]",
        "the wording the measurement ran with"
    );
}