mindfork 0.10.2

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
//! Orchestrator tests — profiles: create/delete/cascade. Part of the [`super`]
//! module (fixtures in mod.rs). See docs/history/refactoring-god-objects.md, stage 3.

use super::*;

#[tokio::test]
async fn create_profile_appears_in_profile_list() {
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch(None);
    // Bootstrap creates one default profile.
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(p) if p.len() == 1),
    )
    .await
    .unwrap();

    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "  Второй  ".into(),
            system_message: "sys".into(),
        })
        .unwrap();
    let list = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(p) if p.len() == 2),
    )
    .await
    .unwrap();
    if let AppEvent::ProfileList(profiles) = list {
        assert!(profiles.iter().any(|p| p.name == "Второй")); // the name is normalized
    }

    drop(cmd_tx);
    handle.await.unwrap();
}

#[tokio::test]
async fn delete_profile_cascades_to_its_chats() {
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch(None);
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // Create a second profile and learn its id.
    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "Второй".into(),
            system_message: "sys".into(),
        })
        .unwrap();
    let list = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(p) if p.len() == 2),
    )
    .await
    .unwrap();
    let second_id = match list {
        AppEvent::ProfileList(profiles) => profiles.iter().find(|p| p.name == "Второй").unwrap().id,
        _ => unreachable!(),
    };

    // Create a chat from the second profile → two chats in total.
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(second_id),
        })
        .unwrap();
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ChatList(c) if c.len() == 2),
    )
    .await
    .unwrap();

    // Delete the second profile: its chat cascades to hidden → one remains.
    cmd_tx.send(AppCommand::DeleteProfile(second_id)).unwrap();
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(p) if p.len() == 1),
    )
    .await
    .unwrap();
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ChatList(c) if c.len() == 1),
    )
    .await
    .unwrap();

    drop(cmd_tx);
    handle.await.unwrap();
}

/// The settings screen keeps its own copy of the profile list, so creating/deleting a
/// profile must re-emit `Settings` — otherwise the new profile is invisible there (not
/// even selectable) and a deleted one lingers until a restart.
#[tokio::test]
async fn create_and_delete_profile_reemit_settings() {
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch(None);
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "Второй".into(),
            system_message: "sys".into(),
        })
        .unwrap();
    let settings = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::Settings { profiles, .. } if profiles.len() == 2),
    )
    .await
    .unwrap();
    let second_id = match settings {
        AppEvent::Settings { profiles, .. } => {
            profiles.iter().find(|p| p.name == "Второй").unwrap().id
        }
        _ => unreachable!(),
    };

    cmd_tx.send(AppCommand::DeleteProfile(second_id)).unwrap();
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::Settings { profiles, .. } if profiles.len() == 1),
    )
    .await
    .unwrap();

    drop(cmd_tx);
    handle.await.unwrap();
}

/// The legacy per-profile impersonation message becomes a named impersonation profile
/// on startup, and the assistant profile is linked to it (spec §11.8). Idempotent: a
/// second launch on the same data changes nothing.
#[tokio::test]
async fn legacy_impersonation_message_migrates_to_a_profile() {
    let dir = tempfile::tempdir().unwrap();
    let paths = Paths::with_root(dir.path());
    let storage = Storage::open(paths.clone()).unwrap();
    let mut profile = Profile::new("Джойс", "Ты — Джойс.");
    profile.impersonation_system_message = "Ты — Владимир.".into();
    let profile_id = profile.id;
    storage.json().upsert_profile(&profile).unwrap();
    drop(storage);

    // First launch: the migration runs.
    let (cmd_tx, handle) = spawn_orch_at(dir.path());
    drop(cmd_tx);
    handle.await.unwrap();

    let storage = Storage::open(paths.clone()).unwrap();
    let config = storage.json().load_config().unwrap();
    assert_eq!(config.impersonation_profiles.len(), 1);
    let imp = &config.impersonation_profiles[0];
    assert_eq!(imp.system_message, "Ты — Владимир.");
    assert!(
        imp.name.contains("Джойс"),
        "named after the profile: {}",
        imp.name
    );
    let saved = storage.json().load_profiles().unwrap();
    let saved = saved.iter().find(|p| p.id == profile_id).unwrap();
    assert_eq!(saved.impersonation_profile_id, Some(imp.id));
    // The legacy field is kept on disk (nothing reads it for prompts any more).
    assert_eq!(saved.impersonation_system_message, "Ты — Владимир.");
    drop(storage);

    // Second launch: nothing is duplicated.
    let (cmd_tx, handle) = spawn_orch_at(dir.path());
    drop(cmd_tx);
    handle.await.unwrap();
    let storage = Storage::open(paths).unwrap();
    assert_eq!(
        storage
            .json()
            .load_config()
            .unwrap()
            .impersonation_profiles
            .len(),
        1
    );
}

/// Runs the orchestrator on an existing data root (used to check startup migrations
/// across two launches); the events are discarded.
fn spawn_orch_at(
    root: &std::path::Path,
) -> (UnboundedSender<AppCommand>, tokio::task::JoinHandle<()>) {
    let storage = Arc::new(Storage::open(Paths::with_root(root)).unwrap());
    let config = storage.json().load_config().unwrap();
    let (cmd_tx, cmd_rx) = unbounded_channel();
    let (evt_tx, mut evt_rx) = unbounded_channel();
    tokio::spawn(async move { while evt_rx.recv().await.is_some() {} });
    let deps = OrchestratorDeps {
        cmd_rx,
        evt_tx,
        storage,
        config,
        supervisor: Arc::new(MockSupervisor::with_backend(None)),
        default_language: crate::shared::i18n::Lang::default(),
        extra_tools: Vec::new(),
    };
    (cmd_tx, tokio::spawn(run(deps)))
}

#[tokio::test]
async fn cannot_delete_last_profile() {
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch(None);
    let list = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ProfileList(_)))
        .await
        .unwrap();
    let only_id = match list {
        AppEvent::ProfileList(p) => p[0].id,
        _ => unreachable!(),
    };

    cmd_tx.send(AppCommand::DeleteProfile(only_id)).unwrap();
    let err = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::Error(_)))
        .await
        .unwrap();
    assert!(matches!(err, AppEvent::Error(_)));

    drop(cmd_tx);
    handle.await.unwrap();
}

/// Role names shown in the feed are resolved from the **profile** (spec §5.1), so
/// they arrive on chat activation and are re-sent after a profile edit — a rename
/// in settings applies to the already-open chat.
#[test]
fn character_names_come_from_the_profile_and_refresh_on_edit() {
    let (_d, mut orch, mut rx) = bare_orch_rx();
    let mut profile = Profile::new("P", "sys");
    profile.character_names.user = "Гайя".into();
    let pid = profile.id;
    let chat = Chat::from_profile(&profile, "Чат");
    let id = chat.id;
    orch.profiles.push(profile);
    orch.chats.push(chat);

    orch.activate(id);
    let names = take_character_names(&mut rx).expect("names sent on activation");
    assert_eq!(names.user, "Гайя");
    assert_eq!(names.assistant, "", "an unset name stays unset");

    orch.handle_update_profile(
        pid,
        ProfileEdit {
            character_names: Some(crate::entities::profile::CharacterNames {
                user: "Гайя".into(),
                assistant: "Анна".into(),
                system: String::new(),
            }),
            ..Default::default()
        },
    );
    let names = take_character_names(&mut rx).expect("names re-sent after the edit");
    assert_eq!(names.assistant, "Анна");
}

/// The last `CharacterNames` event in the queue (the handlers also emit list/settings
/// events, so we filter rather than take the head).
fn take_character_names(
    rx: &mut UnboundedReceiver<AppEvent>,
) -> Option<crate::entities::profile::CharacterNames> {
    let mut found = None;
    while let Ok(ev) = rx.try_recv() {
        if let AppEvent::CharacterNames(names) = ev {
            found = Some(names);
        }
    }
    found
}

/// Bootstrap clears the legacy seed role names (never displayed before this
/// feature), so the feed/export fall back to the interface language's labels.
#[tokio::test]
async fn bootstrap_clears_legacy_seed_character_names() {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path().to_path_buf();
    let storage = Arc::new(Storage::open(Paths::with_root(&root)).unwrap());

    let mut seeded = Profile::new("Старый", "sys");
    seeded.character_names = crate::entities::profile::CharacterNames {
        user: "Вы".into(),
        assistant: "Assistant".into(),
        system: "Система".into(),
    };
    let mut custom = Profile::new("Свой", "sys");
    custom.character_names.assistant = "Анна".into();
    let (seeded_id, custom_id) = (seeded.id, custom.id);
    storage.json().upsert_profile(&seeded).unwrap();
    storage.json().upsert_profile(&custom).unwrap();

    let (cmd_tx, cmd_rx) = unbounded_channel();
    let (evt_tx, mut evt_rx) = unbounded_channel();
    let handle = tokio::spawn(run(OrchestratorDeps {
        cmd_rx,
        evt_tx,
        storage: storage.clone(),
        config: AppConfig::default(),
        supervisor: Arc::new(MockSupervisor::with_backend(None)),
        default_language: crate::shared::i18n::Lang::default(),
        extra_tools: Vec::new(),
    }));
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    drop(cmd_tx);
    handle.await.unwrap();

    let stored = storage.json().load_profiles().unwrap();
    let seeded = stored.iter().find(|p| p.id == seeded_id).unwrap();
    assert_eq!(
        seeded.character_names,
        crate::entities::profile::CharacterNames::default(),
        "seed names are cleared"
    );
    let custom = stored.iter().find(|p| p.id == custom_id).unwrap();
    assert_eq!(
        custom.character_names.assistant, "Анна",
        "a chosen name stays"
    );
}

/// A fresh install: bootstrap makes one profile and one empty chat, and the
/// scaffold language stays **editable** — the pristine chat binds nothing
/// (spec §10). Switching it re-derives the untouched localized defaults: the
/// profile's name and system message, the chat's title and system-message
/// copy — in memory, in the emitted events, and on disk.
#[tokio::test]
async fn fresh_install_language_is_editable_and_rederives_defaults() {
    let ru = crate::shared::i18n::locale(crate::shared::i18n::Lang::Ru);
    let en = crate::shared::i18n::locale(crate::shared::i18n::Lang::En);
    let (dir, cmd_tx, mut evt_rx, handle) = spawn_orch(None);

    // Bootstrap: one ru profile, one empty chat — and no locked language.
    let list = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(p) if p.len() == 1),
    )
    .await
    .unwrap();
    let profile_id = match list {
        AppEvent::ProfileList(profiles) => {
            assert_eq!(profiles[0].name, ru.t("defaults.profile_name"));
            profiles[0].id
        }
        _ => unreachable!(),
    };
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::Settings { language_locked, .. } if language_locked.is_empty()),
    )
    .await
    .expect("the bootstrap chat is pristine — the language must not be locked");

    // The settings screen sends a full snapshot; the untouched defaults ride
    // along byte-equal to the ru bundle and must follow the language.
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: profile_id,
            edit: Box::new(ProfileEdit {
                name: Some(ru.t("defaults.profile_name").into()),
                system_message: Some(ru.t("defaults.system_message").into()),
                language: Some(crate::shared::i18n::Lang::En),
                ..Default::default()
            }),
        })
        .unwrap();

    // The pristine chat's default title follows — through the same event a
    // manual rename sends, so an open chat's header updates too.
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ChatRenamed { title, .. } if title == en.t("defaults.chat_title")),
    )
    .await
    .expect("the pristine chat is re-derived in the new language");
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(p) if p[0].name == en.t("defaults.profile_name")),
    )
    .await
    .unwrap();
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::Settings { language_locked, .. } if language_locked.is_empty()),
    )
    .await
    .expect("still no data — the language stays editable");

    drop(cmd_tx);
    handle.await.unwrap();

    // What reached disk (the deferred chat save flushes on exit).
    let storage = Storage::open(Paths::with_root(dir.path())).unwrap();
    let profiles = storage.json().load_profiles().unwrap();
    assert_eq!(profiles.len(), 1);
    assert_eq!(profiles[0].language, crate::shared::i18n::Lang::En);
    assert_eq!(profiles[0].name, en.t("defaults.profile_name"));
    assert_eq!(
        profiles[0].default_system_message,
        en.t("defaults.system_message")
    );
    let chats = storage.json().load_chats().unwrap();
    assert_eq!(chats.len(), 1);
    assert_eq!(chats[0].title, en.t("defaults.chat_title"));
    assert_eq!(chats[0].system_message, en.t("defaults.system_message"));
}

/// What locks the scaffold language is conversation content, not the chat's
/// existence: messages (a greeting copy included), deleted-exchange tombstones
/// and a compaction summary each lock; a pristine chat doesn't (spec §10).
#[test]
fn conversation_content_locks_profile_language() {
    let (_d, mut orch, mut rx) = bare_orch_rx();
    let profile = Profile::new("P", "sys");
    let pid = profile.id;
    orch.profiles.push(profile);
    orch.chats
        .push(Chat::from_profile(&orch.profiles[0], "Новый чат"));
    assert!(!orch.profile_has_data(pid), "a pristine chat binds nothing");

    // A greeting-only chat already holds an assistant message — it locks.
    orch.chats[0].push_message(Message::assistant("Здравствуйте!"));
    assert!(orch.profile_has_data(pid));

    // The authoritative gate: the switch is dropped and reported.
    orch.handle_update_profile(
        pid,
        ProfileEdit {
            language: Some(crate::shared::i18n::Lang::En),
            ..Default::default()
        },
    );
    assert_eq!(
        orch.profiles[0].language,
        crate::shared::i18n::Lang::default(),
        "the locked language must not change"
    );
    let mut saw_error = false;
    while let Ok(ev) = rx.try_recv() {
        saw_error |= matches!(ev, AppEvent::Error(_));
    }
    assert!(saw_error, "the rejected switch is reported");

    // Tombstones are restorable conversation content — they lock on their own.
    orch.chats[0].messages.clear();
    assert!(!orch.profile_has_data(pid));
    orch.chats[0].record_deleted(
        vec![Message::user("привет")],
        String::new(),
        crate::entities::chat::DeletedCause::DeleteExchange,
    );
    assert!(orch.profile_has_data(pid));

    // So is a compaction summary.
    orch.chats[0].deleted.clear();
    orch.chats[0].compaction = Some(crate::entities::chat::Compaction {
        summary: "сводка".into(),
        upto: 0,
        boundary_id: Uuid::new_v4(),
        compacted_at: chrono::Utc::now(),
        rolls: 1,
    });
    assert!(orch.profile_has_data(pid));
}

/// The re-derivation respects the user's text: a custom profile name/system
/// message and a manually chosen chat title survive the switch untouched; the
/// pristine chat's system-message copy tracks the profile (as if created now).
#[test]
fn language_switch_keeps_user_edited_texts() {
    let (_d, mut orch) = bare_orch();
    let mut profile = Profile::new("Гея", "Ты — Гея.");
    profile.language = crate::shared::i18n::Lang::Ru;
    let pid = profile.id;
    orch.profiles.push(profile);
    let mut chat = Chat::from_profile(&orch.profiles[0], "Мой чат");
    chat.renamed_manually = true;
    let cid = chat.id;
    orch.chats.push(chat);

    orch.handle_update_profile(
        pid,
        ProfileEdit {
            language: Some(crate::shared::i18n::Lang::En),
            ..Default::default()
        },
    );

    assert_eq!(orch.profiles[0].language, crate::shared::i18n::Lang::En);
    assert_eq!(orch.profiles[0].name, "Гея");
    assert_eq!(orch.profiles[0].default_system_message, "Ты — Гея.");
    let chat = orch.chats.iter().find(|c| c.id == cid).unwrap();
    assert_eq!(
        chat.title, "Мой чат",
        "a manual rename outranks re-derivation"
    );
    assert_eq!(chat.system_message, "Ты — Гея.");
}