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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
//! "Self-model" (SelfModel) tools — the MVP probe: reading, reflecting on, and
//! minimally updating the agent's representation of itself, its goals, and the
//! interlocutor. Data is per-profile, in SQLite (like notes) — mutator tools write
//! **directly** through `ctx.storage` (not via `ChatEffect`). Isolation by
//! `ctx.profile_id`. See [docs/history/self-model-mvp.md](../../../docs/history/self-model-mvp.md).

use anyhow::Result;
use chrono::Utc;

use crate::entities::profile::ToolId;
use crate::entities::self_model::{
    GoalMatch, GoalStatus, NarrativeSegment, SelfModel, SelfModelParams,
};
use crate::shared::api::EmbedRole;
use crate::shared::i18n::Locale;

use super::{Tool, ToolContext, ToolOutcome, notes};

pub const GET_SELF_MODEL_ID: &str = "get_self_model";
pub const REFLECT_ID: &str = "reflect";
pub const UPDATE_SELF_MODEL_ID: &str = "update_self_model";
pub const UPDATE_USER_MODEL_ID: &str = "update_user_model";
pub const ADD_INSIGHT_ID: &str = "add_insight";

/// All ids of the "self-model" tool group (for detecting edits within a turn).
/// `consolidate_narrative` was removed — observations moved into notes (Tier 1
/// "narrative as notes"), consolidated by note tools (note_revise/note_supersede/
/// note_merge). See docs/history/narrative-as-notes.md.
pub const ALL_IDS: &[&str] = &[
    GET_SELF_MODEL_ID,
    REFLECT_ID,
    UPDATE_SELF_MODEL_ID,
    UPDATE_USER_MODEL_ID,
    ADD_INSIGHT_ID,
];

/// Is the tool part of the "self-model" group (for the `SelfModelChanged` signal
/// after a turn where the model edited something through its tools).
pub fn is_self_model_tool(name: &str) -> bool {
    ALL_IDS.contains(&name)
}

/// Canonical "self-model" maintenance rules in the agent-scaffold language (`loc`)
/// — key `selfmodel.policy_core`. **The single source** of the wording, from which
/// the maintenance protocol ([`maintenance_protocol`]) and the auto-reflection
/// system message (`orchestrator::reflection`) are assembled. The interactive
/// `reflect` rubric — deliberately not from here (a different genre). Localization
/// — axis A, see docs/history/i18n.md.
pub fn policy_core(loc: &Locale) -> &str {
    loc.t("selfmodel.policy_core")
}

/// A persona-neutral "model maintenance protocol" — [`policy_core`] framed as "you
/// manage this model yourself". Mixed into the turn's system prompt on top of any
/// profile persona (see `orchestrator::generation::inject_self_model`), making use
/// of SelfModel tools predictable regardless of persona.
pub fn maintenance_protocol(loc: &Locale) -> String {
    loc.tf(
        "selfmodel.maintenance_wrapper",
        &[("core", policy_core(loc))],
    )
}

/// Loads the profile's model from storage (or an empty one, if it hasn't been
/// created yet). Read from the DB, not from the `ctx.self_model` snapshot, to see
/// edits made by other SelfModel tools within the same turn.
fn load(ctx: &ToolContext) -> Result<SelfModel> {
    Ok(ctx
        .storage
        .db()
        .self_model_get(ctx.profile_id)?
        .unwrap_or_else(|| SelfModel::new(ctx.profile_id)))
}

/// The profile's fresh observations (self-notes) as narrative segments — for a
/// full read (`render_full`). Observations moved into notes (`@self`), so the
/// "self-model" render gets them as a parameter. Newest first, up to
/// `max_narrative`.
fn recent_segments(ctx: &ToolContext) -> Vec<NarrativeSegment> {
    notes::self_notes_recent(
        &ctx.storage,
        ctx.profile_id,
        ctx.self_model_params.max_narrative,
    )
    .into_iter()
    .map(|n| NarrativeSegment {
        id: n.id,
        text: n.content,
        created_at: n.created_at,
    })
    .collect()
}

/// Full "self-model" read for tools (`get_self_model`/`reflect`/echo):
/// `render_full` + a "Related observations" block (the graph over observations,
/// Tier 2) + a "Source citations" block (an observation citing a RAG source,
/// Tier 3, Path 3). Observations, their links, and citations — from notes.
fn render_self_read(ctx: &ToolContext, m: &SelfModel) -> String {
    let recent = recent_segments(ctx);
    let ids: Vec<uuid::Uuid> = recent.iter().map(|s| s.id).collect();
    let mut out = m.render_full(Utc::now(), &recent, ctx.loc);
    if let Some(block) = notes::self_related_block(ctx, &ids) {
        out.push_str(&block);
    }
    if let Some(block) = notes::cited_sources_block(ctx, &ids) {
        out.push_str(&block);
    }
    // Soft gate on description size (stage 2): if the summary has grown — a hint
    // to shrink it. Visible in get_self_model/reflect and auto-reflection (which
    // starts with get_self_model). See docs/summary-as-snapshot.md.
    if let Some(hint) = m.summary_fill_hint(ctx.self_model_params.summary_target_chars, ctx.loc) {
        out.push_str("\n\n");
        out.push_str(&hint);
    }
    out
}

/// Extracts a string array by key (empty if missing/not an array).
fn str_array(args: &serde_json::Value, key: &str) -> Vec<String> {
    args.get(key)
        .and_then(|v| v.as_array())
        .map(|a| {
            a.iter()
                .filter_map(|x| x.as_str().map(|s| s.trim().to_string()))
                .filter(|s| !s.is_empty())
                .collect()
        })
        .unwrap_or_default()
}

/// Cosine-similarity threshold above which an added trait counts as **related** to
/// an existing one (the `add_traits` gate). Calibrated on live bge-m3 (Step C): it
/// compresses short traits into a narrow band, and real paraphrases score
/// ~0.73-0.83 ("values brevity" ↔ "appreciates concise answers" = 0.77,
/// "skeptical" ↔ "critical" = 0.73), while unrelated ones score lower (coffee↔hiking
/// = 0.69, programmer↔cooking = 0.58). The 0.72 threshold separates them.
/// **Important:** bge-m3 groups by *dimension/topic*, not by direction of meaning,
/// so antonyms also fall into the band ("values brevity" ↔ "values long
/// explanations" = 0.71) — but that's a feature of the gate: a related trait is
/// worth surfacing so the model can decide **duplicate (merge) or contradiction
/// (record as an observation)**. See docs/history/narrative-as-notes.md (Tier 2,
/// Step C).
///
/// A position in the **reference (bge-m3) scale**, not an absolute cosine: the
/// numbers above describe *that* model's distribution, and another model's can be
/// far narrower — on `multilingual-e5-large-instruct` the unrelated mean is 0.79,
/// i.e. above this constant, so used raw the gate would fire on everything
/// (research §6). It is therefore read through
/// [`crate::shared::embed_calibration::SimilarityScale`], which maps the same
/// intent to ~0.91 there (research §8.2).
const TRAIT_SIMILARITY: f32 = 0.72;

/// The related-traits gate (Step C): for EVERY actually-added trait, looks for the
/// closest one among the PRIOR ones (existing before this edit) above the
/// [`TRAIT_SIMILARITY`] threshold. Returns pairs (added, close existing one),
/// newest first. Embeds new + prior traits in one request; traits have no stored
/// vectors (a flat `Vec<String>`), so we compute on the fly. Empty when the
/// embedder is unavailable or the vector count doesn't match — **graceful
/// degradation**, a direct mirror of the `add_insight`/`note_save` gates. See
/// docs/history/narrative-as-notes.md (Tier 2, Step C).
async fn near_duplicate_traits(
    ctx: &ToolContext,
    added: &[String],
    existing_before: &[String],
) -> Vec<(String, String)> {
    if added.is_empty() || existing_before.is_empty() {
        return Vec::new();
    }
    // One request: added ones first, then prior ones — to split by the boundary.
    let texts: Vec<String> = added.iter().chain(existing_before).cloned().collect();
    // Passage on both sides: the comparison is symmetric (new traits against
    // prior ones) and feeds TRAIT_SIMILARITY, which is calibrated on
    // passage-role text. A mismatched role here would cost up to 17% of a
    // compressed model's usable range (research §2.3).
    let Ok(vecs) = ctx.embedder.embed(texts, EmbedRole::Passage).await else {
        return Vec::new();
    };
    if vecs.len() != added.len() + existing_before.len() {
        return Vec::new();
    }
    let (added_vecs, existing_vecs) = vecs.split_at(added.len());
    // The constant is a position in the reference (bge-m3) scale, so it has to be
    // read in the active model's range (identity until the model actually
    // changes). Once, not inside the nested loop.
    let threshold = ctx.storage.db().similarity_scale().map(TRAIT_SIMILARITY);
    let mut out = Vec::new();
    for (i, a) in added.iter().enumerate() {
        // Closest prior trait above the threshold (one per added trait — no noise).
        let mut best: Option<(f32, usize)> = None;
        for (j, _) in existing_before.iter().enumerate() {
            let s = notes::cosine(&added_vecs[i], &existing_vecs[j]);
            if s >= threshold && best.map(|(bs, _)| s > bs).unwrap_or(true) {
                best = Some((s, j));
            }
        }
        if let Some((_, j)) = best {
            out.push((a.clone(), existing_before[j].clone()));
        }
    }
    out
}

/// `get_self_model` — current state of the self-model (read).
pub struct GetSelfModel;

#[async_trait::async_trait]
impl Tool for GetSelfModel {
    fn id(&self) -> ToolId {
        GET_SELF_MODEL_ID.into()
    }
    /// Reads the profile's self-model; a read (docs/research/concurrent-tools.md
    /// §2.3). Its writers are not marked.
    fn concurrent(&self) -> bool {
        true
    }
    fn group(&self) -> crate::features::tools::meta::ToolGroup {
        crate::features::tools::meta::ToolGroup::SelfModel
    }
    fn ui_label(&self) -> &'static str {
        "show self-model"
    }
    fn enabled_by_default(&self) -> bool {
        false
    }
    fn description(&self, loc: &crate::shared::i18n::Locale) -> String {
        loc.t("tool.get_self_model.desc").into()
    }
    fn parameters(&self, _loc: &crate::shared::i18n::Locale) -> serde_json::Value {
        serde_json::json!({ "type": "object", "properties": {} })
    }
    async fn invoke(&self, ctx: &ToolContext, _args: serde_json::Value) -> Result<ToolOutcome> {
        let m = load(ctx)?;
        Ok(ToolOutcome::text(render_self_read(ctx, &m)))
    }
}

/// `reflect` — returns the current model and a rubric for reflection. Writes
/// nothing: this is the reflection "entry point", after which the model calls
/// `update_self_model`/`update_user_model` on its own if there's something to record.
pub struct Reflect;

#[async_trait::async_trait]
impl Tool for Reflect {
    fn id(&self) -> ToolId {
        REFLECT_ID.into()
    }
    fn group(&self) -> crate::features::tools::meta::ToolGroup {
        crate::features::tools::meta::ToolGroup::SelfModel
    }
    fn ui_label(&self) -> &'static str {
        "self-reflection"
    }
    fn enabled_by_default(&self) -> bool {
        false
    }
    fn description(&self, loc: &crate::shared::i18n::Locale) -> String {
        loc.t("tool.reflect.desc").into()
    }
    fn parameters(&self, _loc: &crate::shared::i18n::Locale) -> serde_json::Value {
        serde_json::json!({ "type": "object", "properties": {} })
    }
    async fn invoke(&self, ctx: &ToolContext, _args: serde_json::Value) -> Result<ToolOutcome> {
        let m = load(ctx)?;
        // Observation overview for consolidation (similar pairs / contradicts /
        // without links) — concrete data under the rubric below. Empty if there
        // are fewer than 2 observations.
        let mut overview =
            notes::build_self_consolidation_overview(&ctx.storage, ctx.profile_id, ctx.loc)
                .unwrap_or_default();
        // A2: semantic overlap between self-description (summary) paragraphs and
        // observations — embedding paragraphs on the fly (summary has no stored
        // vectors). See docs/history/self-model-consolidation.md §A2.
        if let Some(section) = notes::summary_observation_overlaps(
            &ctx.storage,
            ctx.embedder.as_ref(),
            ctx.profile_id,
            ctx.loc,
        )
        .await
        {
            if overview.is_empty() {
                overview = section;
            } else {
                overview.push_str("\n\n");
                overview.push_str(&section);
            }
        }
        let overview = if overview.is_empty() {
            String::new()
        } else {
            format!("\n\n{overview}")
        };
        let out = format!(
            "{}\n{}{overview}\n\n{}",
            ctx.loc.t("tool.reflect.rubric.header"),
            render_self_read(ctx, &m),
            ctx.loc.t("tool.reflect.rubric.questions"),
        );
        Ok(ToolOutcome::text(out))
    }
}

/// `add_insight` — adds a short observation/insight to the narrative (including
/// noticed contradictions — in prose, no separate type). Writes directly to the DB.
pub struct AddInsight;

#[async_trait::async_trait]
impl Tool for AddInsight {
    fn id(&self) -> ToolId {
        ADD_INSIGHT_ID.into()
    }
    fn group(&self) -> crate::features::tools::meta::ToolGroup {
        crate::features::tools::meta::ToolGroup::SelfModel
    }
    fn ui_label(&self) -> &'static str {
        "add observation"
    }
    fn enabled_by_default(&self) -> bool {
        false
    }
    fn description(&self, loc: &crate::shared::i18n::Locale) -> String {
        loc.t("tool.add_insight.desc").into()
    }
    fn parameters(&self, loc: &crate::shared::i18n::Locale) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "text": {"type": "string", "description": loc.t("tool.add_insight.param.text")}
            },
            "required": ["text"]
        })
    }
    async fn invoke(&self, ctx: &ToolContext, args: serde_json::Value) -> Result<ToolOutcome> {
        let text = args
            .get("text")
            .and_then(|v| v.as_str())
            .unwrap_or_default()
            .trim()
            .to_string();
        if text.is_empty() {
            anyhow::bail!(ctx.loc.t("tool.add_insight.err.text_empty"));
        }
        // An observation is a self-note (@self): gets an embedding, semantic
        // search, a graph, and consolidation on par with regular notes, but is
        // hidden from user-facing recall. See docs/history/narrative-as-notes.md.
        let id =
            notes::create_note(ctx, text.clone(), vec![notes::SELF_NOTE_TAG.to_string()]).await?;
        let mut msg = ctx.loc.tf(
            "tool.add_insight.result.recorded",
            &[("id", &id.to_string())],
        );
        // The gate (Tier 1's core hypothesis): similar existing observations — to
        // rewrite a near-duplicate via note_revise/note_supersede rather than
        // spawning a copy.
        let similar = notes::self_note_similar(ctx, &text, id).await;
        if !similar.is_empty() {
            msg.push('\n');
            msg.push_str(ctx.loc.t("tool.add_insight.gate.similar"));
            for n in similar {
                msg.push_str(&format!("\n- (id={}) {}", n.id, n.content));
            }
        }
        Ok(ToolOutcome::text(msg).wrote())
    }
}

/// `update_self_model` — edits the self-description and/or goals.
pub struct UpdateSelfModel;

#[async_trait::async_trait]
impl Tool for UpdateSelfModel {
    fn id(&self) -> ToolId {
        UPDATE_SELF_MODEL_ID.into()
    }
    fn group(&self) -> crate::features::tools::meta::ToolGroup {
        crate::features::tools::meta::ToolGroup::SelfModel
    }
    fn ui_label(&self) -> &'static str {
        "update self-model"
    }
    fn enabled_by_default(&self) -> bool {
        false
    }
    fn description(&self, loc: &crate::shared::i18n::Locale) -> String {
        loc.t("tool.update_self_model.desc").into()
    }
    fn parameters(&self, loc: &crate::shared::i18n::Locale) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "summary": {"type": "string", "description": loc.t("tool.update_self_model.param.summary")},
                "add_goals": {"type": "array", "items": {"type": "string"}, "description": loc.t("tool.update_self_model.param.add_goals")},
                "complete_goals": {"type": "array", "items": {"type": "string"}, "description": loc.t("tool.update_self_model.param.complete_goals")},
                "abandon_goals": {"type": "array", "items": {"type": "string"}, "description": loc.t("tool.update_self_model.param.abandon_goals")}
            }
        })
    }
    async fn invoke(&self, ctx: &ToolContext, args: serde_json::Value) -> Result<ToolOutcome> {
        // Unresolved goal handles and scars from folded closed goals are collected
        // inside the atomic edit (captured by `&mut`); writing the model happens
        // under one mutex acquisition, while scars → self-notes happen afterward
        // (the closure has no access to storage/async).
        let mut deltas = SelfModelEditDeltas::default();
        let params = ctx.self_model_params;
        let loc = ctx.loc; // &'static — copy so the closure doesn't borrow ctx
        let (model, changed) = ctx.storage.db().self_model_update(ctx.profile_id, |m| {
            apply_self_model_edit(m, &args, params, loc, &mut deltas)
        })?;

        // Scars from folded closed goals → self-notes (observations). Best-effort.
        for scar in &deltas.scars {
            let _ =
                notes::create_note(ctx, scar.clone(), vec![notes::SELF_NOTE_TAG.to_string()]).await;
        }

        if !changed {
            let mut msg = ctx.loc.t("selfmodel.result.nothing").to_string();
            if !deltas.unresolved.is_empty() {
                msg.push('\n');
                msg.push_str(&ctx.loc.tf(
                    "tool.update_self_model.goals_not_found",
                    &[("list", &deltas.unresolved.join(", "))],
                ));
            }
            return Ok(ToolOutcome::text(msg));
        }
        Ok(ToolOutcome::text(update_self_model_echo(ctx.loc, &model, params, &deltas)).wrote())
    }
}

/// Side data of `update_self_model`'s atomic edit, collected inside the
/// `self_model_update` closure (captured by `&mut`) and consumed afterward.
#[derive(Default)]
struct SelfModelEditDeltas {
    /// Unresolved goal handles (misses and, localized, ambiguities).
    unresolved: Vec<String>,
    /// Scars from folded closed goals — written as self-notes after the edit.
    scars: Vec<String>,
    /// Whether the self-description changed — for the size line in the echo
    /// (the size gate, stage 2).
    summary_changed: bool,
    /// Edit deltas for a compact echo (stage 4): what was actually added/closed
    /// — instead of a full render_full (that stays with get_self_model). Goals
    /// are named by #id — the same handle used later to close them.
    added_goals: Vec<(String, uuid::Uuid)>,
    completed: Vec<uuid::Uuid>,
    abandoned: Vec<uuid::Uuid>,
}

/// The body of `update_self_model`'s atomic edit. Runs INSIDE the
/// `self_model_update` closure — one mutex acquisition, no storage/async access
/// here (the atomicity boundary is load-bearing). Returns whether the model
/// changed.
fn apply_self_model_edit(
    m: &mut SelfModel,
    args: &serde_json::Value,
    params: SelfModelParams,
    loc: &Locale,
    d: &mut SelfModelEditDeltas,
) -> bool {
    let mut changed = false;
    if let Some(s) = args.get("summary").and_then(|v| v.as_str()) {
        let s = s.trim().to_string();
        if m.summary != s {
            m.summary = s;
            changed = true;
            d.summary_changed = true;
        }
    }
    for g in str_array(args, "add_goals") {
        let before = m.goals.len();
        m.add_goal(g);
        // add_goal ignores empty ones — record only what was actually added.
        if m.goals.len() != before {
            let goal = m.goals.last().expect("just added");
            d.added_goals.push((goal.description.clone(), goal.id));
            changed = true;
        }
    }
    // Goals are closed by #id/a full id — resolve the handle among the
    // model's goals.
    changed |= close_goals(
        m,
        args,
        "complete_goals",
        GoalStatus::Completed,
        loc,
        &mut d.completed,
        &mut d.unresolved,
    );
    changed |= close_goals(
        m,
        args,
        "abandon_goals",
        GoalStatus::Abandoned,
        loc,
        &mut d.abandoned,
        &mut d.unresolved,
    );
    // Folding old closed goals: fold returns scars (texts) — we'll
    // write them as self-notes after the atomic edit (a cap on closed
    // goals: the structure doesn't grow, the "biography" is preserved
    // as an observation).
    d.scars = m.fold_closed_goals(params.max_closed_goals, loc);
    if !d.scars.is_empty() {
        changed = true;
    }
    changed
}

/// Closes goals from the `key` string array by #id/full-id handle (see
/// `SelfModel::match_goal`): closed ids go into `closed`, misses/ambiguities
/// into `unresolved`. Returns whether anything actually changed.
fn close_goals(
    m: &mut SelfModel,
    args: &serde_json::Value,
    key: &str,
    status: GoalStatus,
    loc: &Locale,
    closed: &mut Vec<uuid::Uuid>,
    unresolved: &mut Vec<String>,
) -> bool {
    let mut changed = false;
    for h in str_array(args, key) {
        match m.match_goal(&h) {
            GoalMatch::One(id) => {
                if m.set_goal_status(id, status) {
                    closed.push(id);
                    changed = true;
                }
            }
            GoalMatch::None => unresolved.push(h),
            GoalMatch::Ambiguous => {
                unresolved.push(loc.tf("tool.update_self_model.ambiguous", &[("h", &h)]))
            }
        }
    }
    changed
}

/// Delta echo (stage 4): only what changed, without a full render_full (a
/// full read — get_self_model's job). Saves tokens and doesn't "anchor" the
/// model on the essay genre.
fn update_self_model_echo(
    loc: &Locale,
    model: &SelfModel,
    params: SelfModelParams,
    d: &SelfModelEditDeltas,
) -> String {
    use crate::entities::self_model::short_id;
    let mut msg = loc.t("tool.update_self_model.result.updated").to_string();
    // Feedback about description size (stage 2): always on a summary edit, so
    // the model sees growth even before exceeding the target. See
    // docs/summary-as-snapshot.md.
    if d.summary_changed {
        msg.push('\n');
        msg.push_str(&loc.tf(
            "tool.update_self_model.size",
            &[
                ("n", &model.summary.chars().count().to_string()),
                ("target", &params.summary_target_chars.to_string()),
            ],
        ));
    }
    if !d.added_goals.is_empty() {
        let list: Vec<String> = d
            .added_goals
            .iter()
            .map(|(desc, id)| format!("#{} {desc}", short_id(id)))
            .collect();
        msg.push('\n');
        msg.push_str(&loc.tf(
            "tool.update_self_model.added_goals",
            &[("list", &list.join("; "))],
        ));
    }
    if !d.completed.is_empty() {
        let ids: Vec<String> = d
            .completed
            .iter()
            .map(|id| format!("#{}", short_id(id)))
            .collect();
        msg.push('\n');
        msg.push_str(&loc.tf(
            "tool.update_self_model.completed",
            &[("list", &ids.join(", "))],
        ));
    }
    if !d.abandoned.is_empty() {
        let ids: Vec<String> = d
            .abandoned
            .iter()
            .map(|id| format!("#{}", short_id(id)))
            .collect();
        msg.push('\n');
        msg.push_str(&loc.tf(
            "tool.update_self_model.abandoned",
            &[("list", &ids.join(", "))],
        ));
    }
    if !d.scars.is_empty() {
        msg.push('\n');
        msg.push_str(&loc.tf(
            "tool.update_self_model.folded",
            &[("n", &d.scars.len().to_string())],
        ));
    }
    if !d.unresolved.is_empty() {
        msg.push('\n');
        msg.push_str(&loc.tf(
            "tool.update_self_model.goals_not_found_paren",
            &[("list", &d.unresolved.join(", "))],
        ));
    }
    msg
}

/// `update_user_model` — edits the representation of the interlocutor.
pub struct UpdateUserModel;

#[async_trait::async_trait]
impl Tool for UpdateUserModel {
    fn id(&self) -> ToolId {
        UPDATE_USER_MODEL_ID.into()
    }
    fn group(&self) -> crate::features::tools::meta::ToolGroup {
        crate::features::tools::meta::ToolGroup::SelfModel
    }
    fn ui_label(&self) -> &'static str {
        "update user model"
    }
    fn enabled_by_default(&self) -> bool {
        false
    }
    fn description(&self, loc: &crate::shared::i18n::Locale) -> String {
        loc.t("tool.update_user_model.desc").into()
    }
    fn parameters(&self, loc: &crate::shared::i18n::Locale) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "add_traits": {"type": "array", "items": {"type": "string"}, "description": loc.t("tool.update_user_model.param.add_traits")},
                "remove_traits": {"type": "array", "items": {"type": "string"}, "description": loc.t("tool.update_user_model.param.remove_traits")},
                "add_interests": {"type": "array", "items": {"type": "string"}, "description": loc.t("tool.update_user_model.param.add_interests")},
                "remove_interests": {"type": "array", "items": {"type": "string"}, "description": loc.t("tool.update_user_model.param.remove_interests")},
                "relationship_dynamic": {"type": "string", "description": loc.t("tool.update_user_model.param.relationship_dynamic")},
                "note": {"type": "string", "description": loc.t("tool.update_user_model.param.note")}
            }
        })
    }
    async fn invoke(&self, ctx: &ToolContext, args: serde_json::Value) -> Result<ToolOutcome> {
        // Removal/note-presence flags are collected inside the atomic edit
        // (captured by `&mut`); the write happens under one mutex acquisition
        // (protection against a race).
        //
        // The near-duplicate-traits gate (Step C): needs a snapshot of the traits
        // BEFORE adding — collected inside the atomic edit (captured by `&mut`),
        // the embedding happens after.
        let requested_traits = str_array(&args, "add_traits");
        let mut deltas = UserModelEditDeltas::default();
        let (model, changed) = ctx.storage.db().self_model_update(ctx.profile_id, |m| {
            apply_user_model_edit(m, &args, &requested_traits, &mut deltas)
        })?;

        // Scar → self-note (observation). Best-effort. `note` by itself doesn't
        // count as a model change (it's a separate note), but makes the call
        // meaningful.
        if let Some(scar) = &deltas.note_scar {
            let _ =
                notes::create_note(ctx, scar.clone(), vec![notes::SELF_NOTE_TAG.to_string()]).await;
        }

        if !changed && !deltas.has_note {
            return Ok(ToolOutcome::text(ctx.loc.t("selfmodel.result.nothing")));
        }
        // Actually-added traits (new after dedup, no within-batch repeats) —
        // compare them against the prior ones via the near-duplicate gate
        // (embedding only if there's something to compare; softly empty if the
        // embedder is unavailable).
        let added_traits = newly_added_traits(&requested_traits, &deltas.existing_before_traits);
        let dup_pairs =
            near_duplicate_traits(ctx, &added_traits, &deltas.existing_before_traits).await;

        Ok(ToolOutcome::text(update_user_model_echo(ctx.loc, &model, &deltas, &dup_pairs)).wrote())
    }
}

/// Side data of `update_user_model`'s atomic edit, collected inside the
/// `self_model_update` closure (captured by `&mut`) and consumed afterward.
#[derive(Default)]
struct UserModelEditDeltas {
    removed_traits: bool,
    removed_interests: bool,
    replaced_dynamic: bool,
    has_note: bool,
    /// The revision scar (`note`) is collected inside the closure, but written
    /// **after** — as a self-note (observation), not into the model blob
    /// (async/storage are outside the closure).
    note_scar: Option<String>,
    /// A snapshot of the traits BEFORE adding — for the near-duplicate gate.
    existing_before_traits: Vec<String>,
}

/// The body of `update_user_model`'s atomic edit. Runs INSIDE the
/// `self_model_update` closure — one mutex acquisition, no storage/async access
/// here (the atomicity boundary is load-bearing). Returns whether the model
/// changed.
fn apply_user_model_edit(
    m: &mut SelfModel,
    args: &serde_json::Value,
    requested_traits: &[String],
    d: &mut UserModelEditDeltas,
) -> bool {
    let mut changed = false;

    // Lists — merge (add/remove with dedup), not replacement: an edit
    // doesn't zero out the accumulated representation (a common
    // "overwritten by mood" bug).
    d.existing_before_traits = m.user_model.perceived_traits.clone();
    changed |= m.user_model.add_traits(requested_traits.to_vec());
    d.removed_traits = m
        .user_model
        .remove_traits(&str_array(args, "remove_traits"));
    changed |= d.removed_traits;
    changed |= m.user_model.add_interests(str_array(args, "add_interests"));
    d.removed_interests = m
        .user_model
        .remove_interests(&str_array(args, "remove_interests"));
    changed |= d.removed_interests;
    if let Some(s) = args.get("relationship_dynamic").and_then(|v| v.as_str()) {
        let s = s.trim().to_string();
        if m.user_model.relationship_dynamic != s {
            // Replacing a NON-EMPTY dynamic — a substantial revision
            // (unlike initial population); ask to leave a trace (a scar),
            // like traits.
            d.replaced_dynamic = !m.user_model.relationship_dynamic.trim().is_empty();
            m.user_model.relationship_dynamic = s;
            changed = true;
        }
    }
    // Revision scar: save `note` (what changed and why) — it goes out as
    // an "about self" observation note, so a change of opinion about the
    // interlocutor leaves a trace (traits are flat — the "biography" of
    // their changes lives in observations).
    d.note_scar = args
        .get("note")
        .and_then(|v| v.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string);
    d.has_note = d.note_scar.is_some();
    changed
}

/// Requested traits that are new against the prior list (case-insensitive
/// dedup, no within-batch repeats).
fn newly_added_traits(requested: &[String], existing: &[String]) -> Vec<String> {
    let mut added: Vec<String> = Vec::new();
    for t in requested {
        let lc = t.to_lowercase();
        let known = existing.iter().any(|x| x.to_lowercase() == lc)
            || added.iter().any(|x| x.to_lowercase() == lc);
        if !known {
            added.push(t.clone());
        }
    }
    added
}

/// Delta echo (stage 4): compact final lists of the interlocutor model
/// instead of a full render_full (a full read — get_self_model's job). The
/// lists are short by construction (merge with dedup), so we show them in full.
fn update_user_model_echo(
    loc: &Locale,
    model: &SelfModel,
    d: &UserModelEditDeltas,
    dup_pairs: &[(String, String)],
) -> String {
    let mut msg = loc.t("tool.update_user_model.result.updated").to_string();
    let u = &model.user_model;
    if !u.perceived_traits.is_empty() {
        msg.push('\n');
        msg.push_str(&loc.tf(
            "tool.update_user_model.traits",
            &[("list", &u.perceived_traits.join(", "))],
        ));
    }
    if !u.current_interests.is_empty() {
        msg.push('\n');
        msg.push_str(&loc.tf(
            "tool.update_user_model.interests",
            &[("list", &u.current_interests.join(", "))],
        ));
    }
    if !u.relationship_dynamic.trim().is_empty() {
        msg.push('\n');
        msg.push_str(&loc.tf(
            "tool.update_user_model.relationship",
            &[("dyn", u.relationship_dynamic.trim())],
        ));
    }
    // Confirmation of the revision scar (if `note` was passed) — its text is visible.
    if let Some(scar) = &d.note_scar {
        msg.push('\n');
        msg.push_str(&loc.tf("tool.update_user_model.scar_saved", &[("scar", scar)]));
    }
    // The related-traits gate (Step C): a topically close trait already
    // exists. bge-m3 groups traits by dimension (paraphrases AND antonyms), so
    // we ask the model to DECIDE: is this a duplicate (merge via
    // remove_traits) or a contradiction (record via add_insight) — a mirror
    // of the add_insight gate, but over the flat trait list (integration
    // instead of accumulation).
    if !dup_pairs.is_empty() {
        msg.push('\n');
        msg.push_str(loc.t("tool.update_user_model.gate.related"));
        for (added, existing) in dup_pairs {
            msg.push_str(&format!("\n- «{added}» ≈ «{existing}»"));
        }
    }
    // Removing a trait/interest or replacing a non-empty dynamic — a revision
    // of judgment. No reason recorded → remind to leave a trace as an
    // observation (a scar) rather than changing it silently (a shift in
    // relationship dynamic is the most significant revision of the
    // interlocutor model).
    if (d.removed_traits || d.removed_interests || d.replaced_dynamic) && !d.has_note {
        msg.push('\n');
        msg.push_str(loc.t("tool.update_user_model.nudge_note"));
    }
    msg
}

// `consolidate_narrative` was removed: observations moved into notes (Tier 1),
// consolidated by note tools (note_revise/note_supersede/note_merge) — they're
// stronger (replacement with a "scar" rather than deletion by id). See
// docs/history/narrative-as-notes.md.

#[cfg(test)]
mod tests {
    use super::super::testkit::ctx_with_storage;
    use super::*;
    use uuid::Uuid;

    /// The profile's self-notes (observations) — newest first. Observations moved
    /// into notes (@self), so we check them there, not in the model blob.
    fn self_notes(
        storage: &crate::shared::storage::Storage,
        profile: Uuid,
    ) -> Vec<crate::entities::note::Note> {
        storage
            .db()
            .note_list(profile, None, &[notes::SELF_NOTE_TAG.to_string()], None)
            .unwrap()
    }

    #[test]
    fn self_model_tool_descriptions_are_localized() {
        // Every "self-model" tool returns DIFFERENT text on ru/en (catches a
        // forgotten `_loc`), en — no Cyrillic. §3.5 docs/history/i18n.md.
        use crate::shared::i18n::{Lang, locale};
        let (r, e) = (locale(Lang::Ru), locale(Lang::En));
        let no_cyr = |s: &str| {
            !s.chars()
                .any(|c| ('а'..='я').contains(&c) || ('А'..='Я').contains(&c))
        };
        let pairs: Vec<(String, String)> = vec![
            (GetSelfModel.description(r), GetSelfModel.description(e)),
            (Reflect.description(r), Reflect.description(e)),
            (AddInsight.description(r), AddInsight.description(e)),
            (
                UpdateSelfModel.description(r),
                UpdateSelfModel.description(e),
            ),
            (
                UpdateUserModel.description(r),
                UpdateUserModel.description(e),
            ),
        ];
        for (ru_d, en_d) in pairs {
            assert_ne!(ru_d, en_d, "description not localized: {ru_d}");
            assert!(no_cyr(&en_d), "Cyrillic in en description: {en_d}");
        }
    }

    #[tokio::test]
    async fn add_insight_result_localized_for_all_langs() {
        // Confirmation of recording an observation renders in every built-in language.
        use crate::shared::i18n::{Lang, locale};
        for &lang in Lang::ALL {
            let (_d, _s, mut ctx) = ctx_with_storage(Uuid::new_v4());
            ctx.loc = locale(lang);
            let out = AddInsight
                .invoke(&ctx, serde_json::json!({"text": "hello observation"}))
                .await
                .unwrap();
            // The result starts with the localized "recorded (id=…)" template.
            let prefix = locale(lang)
                .t("tool.add_insight.result.recorded")
                .split("{id}")
                .next()
                .unwrap();
            assert!(out.result.starts_with(prefix), "{lang:?}: {}", out.result);
        }
    }

    #[test]
    fn is_self_model_tool_recognizes_group() {
        assert!(is_self_model_tool(UPDATE_SELF_MODEL_ID));
        assert!(is_self_model_tool(ADD_INSIGHT_ID));
        assert!(is_self_model_tool(GET_SELF_MODEL_ID));
        assert!(!is_self_model_tool("note_save"));
        assert!(!is_self_model_tool("web_search"));
    }

    /// The reference locale (ru) — asserts on Russian substrings pin the ru bundle.
    fn ru() -> &'static Locale {
        crate::shared::i18n::locale(crate::shared::i18n::Lang::Ru)
    }

    #[test]
    fn maintenance_protocol_wraps_policy_core() {
        let p = maintenance_protocol(ru());
        // The "you manage this yourself" wrapper + the whole POLICY_CORE (with the
        // key anti-flattery phrase).
        assert!(p.contains("Ты сам ведёшь эту «модель себя»"));
        assert!(p.contains(policy_core(ru())));
        assert!(p.contains("угодливости"));
    }

    /// Per-language (§3.5): the maintenance protocol wraps `policy_core` of the
    /// same language on every built-in language; the `{core}` placeholder is substituted.
    #[test]
    fn maintenance_protocol_localized_for_all_langs() {
        for &lang in crate::shared::i18n::Lang::ALL {
            let l = crate::shared::i18n::locale(lang);
            let p = maintenance_protocol(l);
            assert!(
                p.contains(policy_core(l)),
                "{lang:?}: policy_core not embedded"
            );
            assert!(
                !p.contains("{core}"),
                "{lang:?}: placeholder not substituted"
            );
        }
    }

    #[test]
    fn policy_core_routes_events_to_insights() {
        // Stage 1 (summary — a snapshot, not a chronicle): the genre boundary runs
        // along the axis "state → summary, event/conclusion → add_insight (even durable)".
        let core = policy_core(ru());
        assert!(core.contains("снимок"));
        assert!(core.contains("СОКРАЩАЙ"));
        assert!(core.contains("ДАЖЕ ЕСЛИ"));
        // The explicit "read it in full before editing" step (protection against
        // editing from a truncated view).
        assert!(core.contains("прочти его целиком через get_self_model"));
    }

    #[test]
    fn policy_core_nudges_interest_aging() {
        // A3-light: a nudge on interest aging via the existing remove_interests.
        let core = policy_core(ru());
        assert!(core.contains("remove_interests"));
        assert!(core.contains("устаревал"));
        assert!(core.contains("давно не"));
    }

    #[test]
    fn update_self_model_description_routes_events_to_insights() {
        // The tool description routes event-like conclusions into add_insight,
        // while summary stays a compact snapshot.
        let d =
            UpdateSelfModel.description(crate::shared::i18n::locale(crate::shared::i18n::Lang::Ru));
        assert!(d.contains("снимок"));
        assert!(d.contains("add_insight"));
    }

    #[tokio::test]
    async fn get_on_empty_reports_empty() {
        let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
        let out = GetSelfModel
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert!(out.result.contains("пуста"));
        assert!(out.effects.is_empty());
    }

    #[tokio::test]
    async fn update_summary_and_goal_persists() {
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);

        let out = UpdateSelfModel
            .invoke(
                &ctx,
                serde_json::json!({"summary": "ценю ясность", "add_goals": ["помочь с проектом"]}),
            )
            .await
            .unwrap();
        assert!(out.result.contains("обновлена"));

        // Recorded in storage under this profile.
        let stored = storage.db().self_model_get(profile).unwrap().unwrap();
        assert_eq!(stored.summary, "ценю ясность");
        assert_eq!(stored.active_goals().count(), 1);

        // get_self_model reflects the record.
        let got = GetSelfModel
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert!(got.result.contains("ценю ясность"));
        assert!(got.result.contains("помочь с проектом"));
    }

    #[tokio::test]
    async fn update_summary_echo_shows_size() {
        // Stage 2: the summary-edit echo always carries a size line (feedback on growth).
        let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
        let out = UpdateSelfModel
            .invoke(&ctx, serde_json::json!({"summary": "ценю ясность"}))
            .await
            .unwrap();
        assert!(out.result.contains("Описание:"));
        assert!(out.result.contains("симв."));
        // An edit with no summary (goal only) — no size line.
        let out = UpdateSelfModel
            .invoke(&ctx, serde_json::json!({"add_goals": ["цель"]}))
            .await
            .unwrap();
        assert!(!out.result.contains("Описание:"));
    }

    #[tokio::test]
    async fn update_self_model_echo_is_delta_not_full() {
        // Stage 4: the edit echo carries deltas (#id of the added goal), but NOT
        // the full summary text (a full read — only get_self_model's job).
        let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
        let out = UpdateSelfModel
            .invoke(
                &ctx,
                serde_json::json!({
                    "summary": "УНИКАЛЬНЫЙ_МАРКЕР_ОПИСАНИЯ",
                    "add_goals": ["новая цель"]
                }),
            )
            .await
            .unwrap();
        assert!(out.result.contains("Добавлены цели:"));
        assert!(out.result.contains("новая цель"));
        assert!(out.result.contains('#'));
        assert!(out.result.contains("Описание:")); // the size line (stage 2)
        // The summary text doesn't make it into the echo (no "anchoring" on the
        // essay genre).
        assert!(!out.result.contains("УНИКАЛЬНЫЙ_МАРКЕР_ОПИСАНИЯ"));
    }

    #[tokio::test]
    async fn update_self_model_echo_shows_closed_goal_id() {
        // Stage 4: closing a goal is reflected in the echo by its #id (the same
        // handle used to close it).
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        UpdateSelfModel
            .invoke(&ctx, serde_json::json!({"add_goals": ["цель"]}))
            .await
            .unwrap();
        let id = storage.db().self_model_get(profile).unwrap().unwrap().goals[0].id;
        let short = id.simple().to_string()[..6].to_string();
        let out = UpdateSelfModel
            .invoke(
                &ctx,
                serde_json::json!({"complete_goals": [format!("#{short}")]}),
            )
            .await
            .unwrap();
        assert!(out.result.contains("Закрыты выполненными:"));
        assert!(out.result.contains(&format!("#{short}")));
    }

    #[tokio::test]
    async fn update_user_model_echo_shows_final_lists() {
        // Stage 4: the echo shows compact final lists, not a full render_full.
        let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
        let out = UpdateUserModel
            .invoke(
                &ctx,
                serde_json::json!({
                    "add_traits": ["скептик"],
                    "add_interests": ["Rust"],
                    "relationship_dynamic": "рабочие"
                }),
            )
            .await
            .unwrap();
        assert!(out.result.contains("Черты теперь: скептик"));
        assert!(out.result.contains("Интересы теперь: Rust"));
        assert!(out.result.contains("Отношения: рабочие"));
    }

    #[tokio::test]
    async fn get_self_model_surfaces_summary_fill_hint_over_target() {
        // Stage 2: a bloated description (over the target) raises a hint in the read.
        use crate::entities::self_model::SelfModelParams;
        use crate::shared::config::SelfModelSettings;
        let profile = Uuid::new_v4();
        let (_d, _s, mut ctx) = ctx_with_storage(profile);
        // The target 5 is sanitized to the 200 floor — take a description longer
        // than 200 characters.
        ctx.self_model_params = SelfModelParams::from_settings(&SelfModelSettings {
            summary_target_chars: 5,
            ..SelfModelSettings::default()
        });
        UpdateSelfModel
            .invoke(&ctx, serde_json::json!({"summary": "я".repeat(250)}))
            .await
            .unwrap();
        let out = GetSelfModel
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert!(out.result.contains("Описание себя разрослось"));
    }

    #[tokio::test]
    async fn complete_goal_by_id() {
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        UpdateSelfModel
            .invoke(&ctx, serde_json::json!({"add_goals": ["старая цель"]}))
            .await
            .unwrap();
        let id = storage.db().self_model_get(profile).unwrap().unwrap().goals[0].id;

        UpdateSelfModel
            .invoke(
                &ctx,
                serde_json::json!({"complete_goals": [id.to_string()]}),
            )
            .await
            .unwrap();
        let stored = storage.db().self_model_get(profile).unwrap().unwrap();
        assert_eq!(stored.active_goals().count(), 0);
    }

    #[tokio::test]
    async fn update_user_model_persists() {
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        UpdateUserModel
            .invoke(
                &ctx,
                serde_json::json!({
                    "add_traits": ["скептичный", "глубокий"],
                    "relationship_dynamic": "рабочие"
                }),
            )
            .await
            .unwrap();
        let stored = storage.db().self_model_get(profile).unwrap().unwrap();
        assert_eq!(stored.user_model.perceived_traits.len(), 2);
        assert_eq!(stored.user_model.relationship_dynamic, "рабочие");
    }

    #[tokio::test]
    async fn update_user_model_merges_not_overwrites() {
        // The key fix: an edit doesn't overwrite the prior data ("overwritten by mood" bug).
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["добрый"]}))
            .await
            .unwrap();
        // A second edit in a different "mood" — adds, doesn't replace.
        UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["прямолинейный"]}))
            .await
            .unwrap();
        let stored = storage.db().self_model_get(profile).unwrap().unwrap();
        assert_eq!(stored.user_model.perceived_traits.len(), 2);
        assert!(
            stored
                .user_model
                .perceived_traits
                .contains(&"добрый".to_string())
        );
        // remove_traits removes precisely.
        UpdateUserModel
            .invoke(&ctx, serde_json::json!({"remove_traits": ["добрый"]}))
            .await
            .unwrap();
        let stored = storage.db().self_model_get(profile).unwrap().unwrap();
        assert_eq!(
            stored.user_model.perceived_traits,
            vec!["прямолинейный".to_string()]
        );
    }

    #[tokio::test]
    async fn complete_goal_by_short_id() {
        // The model references a goal by the short #id from get_self_model.
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        UpdateSelfModel
            .invoke(&ctx, serde_json::json!({"add_goals": ["цель"]}))
            .await
            .unwrap();
        let id = storage.db().self_model_get(profile).unwrap().unwrap().goals[0].id;
        let short = id.simple().to_string()[..6].to_string();

        UpdateSelfModel
            .invoke(
                &ctx,
                serde_json::json!({"complete_goals": [format!("#{short}")]}),
            )
            .await
            .unwrap();
        let stored = storage.db().self_model_get(profile).unwrap().unwrap();
        assert_eq!(stored.active_goals().count(), 0);

        // A nonexistent #id — a clear report, not a panic.
        let out = UpdateSelfModel
            .invoke(&ctx, serde_json::json!({"complete_goals": ["#zzzzzz"]}))
            .await
            .unwrap();
        assert!(out.result.contains("Не найдены цели"));
    }

    #[tokio::test]
    async fn update_with_nothing_is_noop() {
        let (_d, storage, ctx) = ctx_with_storage(Uuid::new_v4());
        let out = UpdateSelfModel
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert!(out.result.contains("Нечего обновлять"));
        // Nothing was recorded.
        assert!(
            storage
                .db()
                .self_model_get(ctx.profile_id)
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn reflect_returns_current_and_rubric() {
        let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
        let out = Reflect.invoke(&ctx, serde_json::json!({})).await.unwrap();
        assert!(out.result.contains("Вопросы для размышления"));
        // Stage 1: the rubric asks about the self-description's growth.
        assert!(out.result.contains("Не разрослось ли описание себя"));
        assert!(out.effects.is_empty());
        // With no observations (< 2) the self-consolidation overview isn't mixed in.
        assert!(!out.result.contains("Обзор наблюдений"));
    }

    #[tokio::test]
    async fn reflect_includes_self_consolidation_overview() {
        // Tier 3: with ≥2 observations, reflect mixes in the self-consolidation
        // overview (concrete similar pairs / links / no-links under the rubric).
        let profile = Uuid::new_v4();
        let (_d, _s, ctx) = ctx_with_storage(profile);
        AddInsight
            .invoke(&ctx, serde_json::json!({"text": "aaaa bbbb"}))
            .await
            .unwrap();
        AddInsight
            .invoke(&ctx, serde_json::json!({"text": "aaab"}))
            .await
            .unwrap();
        let out = Reflect.invoke(&ctx, serde_json::json!({})).await.unwrap();
        assert!(out.result.contains("Обзор наблюдений"));
        assert!(out.result.contains("Похожие пары"));
    }

    #[tokio::test]
    async fn add_insight_creates_self_note_and_shows() {
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        let out = AddInsight
            .invoke(
                &ctx,
                serde_json::json!({"text": "напряжение между краткостью и полнотой"}),
            )
            .await
            .unwrap();
        assert!(out.result.contains("Наблюдение записано"));
        // Recorded as a self-note (@self), not in the model blob (there is none).
        assert_eq!(self_notes(&storage, profile).len(), 1);
        assert!(storage.db().self_model_get(profile).unwrap().is_none());

        // get_self_model shows the observation (assembled from notes).
        let got = GetSelfModel
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert!(got.result.contains("Наблюдения"));
        assert!(got.result.contains("напряжение"));

        // Empty text — an error, nothing created.
        assert!(
            AddInsight
                .invoke(&ctx, serde_json::json!({"text": "  "}))
                .await
                .is_err()
        );
        assert_eq!(self_notes(&storage, profile).len(), 1);
    }

    #[tokio::test]
    async fn add_insight_gate_surfaces_similar_observation() {
        // Tier 1's core hypothesis: a near-duplicate observation surfaces the gate
        // (a similar existing self-note) with a hint to rewrite via note_revise.
        let profile = Uuid::new_v4();
        let (_d, _s, ctx) = ctx_with_storage(profile);
        AddInsight
            .invoke(&ctx, serde_json::json!({"text": "aaaa bbbb"}))
            .await
            .unwrap();
        let out = AddInsight
            .invoke(&ctx, serde_json::json!({"text": "aaab"}))
            .await
            .unwrap();
        assert!(out.result.contains("Похожие наблюдения"));
        assert!(out.result.contains("aaaa bbbb"));
        assert!(out.result.contains("note_revise"));
    }

    #[tokio::test]
    async fn get_self_model_surfaces_linked_observations() {
        // Tier 2: get_self_model shows links between observations (the graph).
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        AddInsight
            .invoke(&ctx, serde_json::json!({"text": "ценю краткость"}))
            .await
            .unwrap();
        AddInsight
            .invoke(
                &ctx,
                serde_json::json!({"text": "иногда бываю многословен"}),
            )
            .await
            .unwrap();
        let ns = self_notes(&storage, profile);
        storage
            .db()
            .note_link_insert(profile, ns[0].id, ns[1].id, "contradicts")
            .unwrap();

        let out = GetSelfModel
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert!(out.result.contains("Связи наблюдений"));
        assert!(out.result.contains("contradicts"));
    }

    #[tokio::test]
    async fn add_trait_gate_surfaces_near_duplicate() {
        // Step C: adding a trait related to an existing one raises the gate
        // (a mirror of the add_insight gate). MockEmbedder(16) — a bag of
        // characters: "aaaa bbbb" ↔ "aaab" are close (cosine ≈ 0.89 > the 0.72 threshold).
        let profile = Uuid::new_v4();
        let (_d, _s, ctx) = ctx_with_storage(profile);
        // The first trait — no prior ones, the gate stays silent.
        let out = UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["aaaa bbbb"]}))
            .await
            .unwrap();
        assert!(!out.result.contains("Родственные черты"));
        // The second trait is close to the first → the gate shows the related trait.
        let out = UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["aaab"]}))
            .await
            .unwrap();
        assert!(out.result.contains("Родственные черты"));
        assert!(out.result.contains("aaab"));
        assert!(out.result.contains("aaaa bbbb"));
        assert!(out.result.contains("remove_traits"));
    }

    #[tokio::test]
    async fn add_trait_gate_follows_the_calibrated_scale() {
        // TRAIT_SIMILARITY is a position in bge-m3's scale, not an absolute
        // cosine. On `multilingual-e5-large-instruct` (range 2.6× narrower,
        // research §8.2) the same intent sits at ~0.908 — so a pair the raw
        // constant calls related no longer is, while a closer one still is.
        // MockEmbedder(16), a bag of characters: "aaaa bbbb" ↔ "aaab" = 0.894,
        // "aaab" ↔ "aaaab" = 0.997.
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        storage
            .db()
            .set_embed_calibration(&crate::shared::embed_calibration::Calibration {
                unrelated: 0.7897,
                paraphrase: 0.9456,
            })
            .unwrap();

        UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["aaaa bbbb"]}))
            .await
            .unwrap();
        // 0.894 clears the raw 0.72 gate (that is what
        // `add_trait_gate_surfaces_near_duplicate` pins, uncalibrated) but not
        // the mapped 0.908 one.
        let out = UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["aaab"]}))
            .await
            .unwrap();
        assert!(!out.result.contains("Родственные черты"), "{}", out.result);

        // 0.997 clears it — the gate moved, it did not switch off.
        let out = UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["aaaab"]}))
            .await
            .unwrap();
        assert!(out.result.contains("Родственные черты"), "{}", out.result);
        assert!(out.result.contains("aaab"), "{}", out.result);
    }

    #[tokio::test]
    async fn add_trait_gate_silent_for_dissimilar() {
        // An unrelated trait doesn't raise the gate (no false positives).
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["aaaa bbbb"]}))
            .await
            .unwrap();
        let out = UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["wwww"]}))
            .await
            .unwrap();
        assert!(!out.result.contains("Родственные черты"));
        // Both traits are kept (the gate blocks nothing — only warns).
        let stored = storage.db().self_model_get(profile).unwrap().unwrap();
        assert_eq!(stored.user_model.perceived_traits.len(), 2);
    }

    #[tokio::test]
    async fn removing_trait_with_note_leaves_scar_as_self_note() {
        // Trait revision with a note leaves a trace — now as a self-note (observation).
        let profile = Uuid::new_v4();
        let (_d, storage, ctx) = ctx_with_storage(profile);
        UpdateUserModel
            .invoke(
                &ctx,
                serde_json::json!({"add_traits": ["компетентный одиночка"]}),
            )
            .await
            .unwrap();
        UpdateUserModel
            .invoke(
                &ctx,
                serde_json::json!({
                    "remove_traits": ["компетентный одиночка"],
                    "add_traits": ["в команде раскрывается при доверии"],
                    "note": "Пересмотрел: раньше видел одиночкой, но в команде при доверии он силён"
                }),
            )
            .await
            .unwrap();
        let stored = storage.db().self_model_get(profile).unwrap().unwrap();
        // The trait is replaced, and the reason is preserved as a self-note (not
        // in the blob, not erased).
        assert_eq!(
            stored.user_model.perceived_traits,
            vec!["в команде раскрывается при доверии".to_string()]
        );
        assert!(stored.narrative.is_empty());
        let n = self_notes(&storage, profile);
        assert_eq!(n.len(), 1);
        assert!(n[0].content.contains("Пересмотрел"));
    }

    #[tokio::test]
    async fn removing_trait_without_note_nudges_for_scar() {
        let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
        UpdateUserModel
            .invoke(&ctx, serde_json::json!({"add_traits": ["скептик"]}))
            .await
            .unwrap();
        let out = UpdateUserModel
            .invoke(&ctx, serde_json::json!({"remove_traits": ["скептик"]}))
            .await
            .unwrap();
        assert!(out.result.contains("без пояснения"));
        assert!(out.result.contains("note"));
    }

    #[tokio::test]
    async fn replacing_nonempty_dynamic_without_note_nudges() {
        let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
        // Initial population of the dynamic — WITHOUT a reminder (not a revision).
        let out = UpdateUserModel
            .invoke(
                &ctx,
                serde_json::json!({"relationship_dynamic": "доверительные"}),
            )
            .await
            .unwrap();
        assert!(!out.result.contains("без пояснения"));
        // Replacing a non-empty dynamic without a note — a reminder about the scar.
        let out = UpdateUserModel
            .invoke(
                &ctx,
                serde_json::json!({"relationship_dynamic": "натянутые"}),
            )
            .await
            .unwrap();
        assert!(out.result.contains("без пояснения"));
        // With a note — no reminder, and the reason goes out as an observation
        // (visible in the echo).
        let out = UpdateUserModel
            .invoke(
                &ctx,
                serde_json::json!({
                    "relationship_dynamic": "снова тёплые",
                    "note": "помирились после спора"
                }),
            )
            .await
            .unwrap();
        assert!(!out.result.contains("без пояснения"));
        assert!(out.result.contains("помирились после спора"));
    }

    #[tokio::test]
    async fn update_self_model_folds_old_closed_goals_into_notes() {
        use crate::entities::self_model::SelfModelParams;
        use crate::shared::config::SelfModelSettings;
        let profile = Uuid::new_v4();
        let (_d, storage, mut ctx) = ctx_with_storage(profile);
        // Keep no more than 2 closed goals.
        ctx.self_model_params = SelfModelParams::from_settings(&SelfModelSettings {
            max_closed_goals: 2,
            ..SelfModelSettings::default()
        });
        UpdateSelfModel
            .invoke(
                &ctx,
                serde_json::json!({"add_goals": ["ц0", "ц1", "ц2", "ц3", "ц4"]}),
            )
            .await
            .unwrap();
        let ids: Vec<String> = storage
            .db()
            .self_model_get(profile)
            .unwrap()
            .unwrap()
            .goals
            .iter()
            .map(|g| g.id.to_string())
            .collect();
        // Close all five — folding will keep the 2 most-recently-closed, 3 → to
        // the archive (now as self-notes, not in the blob).
        UpdateSelfModel
            .invoke(&ctx, serde_json::json!({"complete_goals": ids}))
            .await
            .unwrap();
        let stored = storage.db().self_model_get(profile).unwrap().unwrap();
        assert_eq!(stored.goals.len(), 2); // the closed-goal cap is respected
        assert!(stored.narrative.is_empty());
        // Three folded goals — self-notes "[goal archive]".
        let archived = self_notes(&storage, profile)
            .into_iter()
            .filter(|n| n.content.starts_with("[архив цели]"))
            .count();
        assert_eq!(archived, 3);
    }
    /// The memory writers report a write on their success path and nothing
    /// on a refusal; the reader never does
    /// (docs/research/acted-by-effect.md §3.1).
    #[tokio::test]
    async fn writers_report_a_write_and_the_reader_does_not() {
        let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
        let read = GetSelfModel
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert!(!read.wrote, "a read");
        let added = AddInsight
            .invoke(&ctx, serde_json::json!({"text": "hello observation"}))
            .await
            .unwrap();
        assert!(added.wrote, "an observation was written");
        let changed = UpdateSelfModel
            .invoke(&ctx, serde_json::json!({"summary": "ценю ясность"}))
            .await
            .unwrap();
        assert!(changed.wrote, "the summary was written");
        let nothing = UpdateSelfModel
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert!(!nothing.wrote, "nothing to change: {}", nothing.result);
    }
}