eidetic-engine 0.15.1

Durable, local-first, explainable memory for coding agents.
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
//! Global (user-level) memory tier — separate-store design (bd-2vq2z.13).
//!
//! ee memories are workspace-scoped, but a user's durable conventions
//! ("always run `cargo fmt --check` before release", "prefer X over Y",
//! "never use approach Z") should follow them into every repo. This module is
//! the decided core of the cross-workspace / user-global memory tier.
//!
//! # Decision (owner, 2026-06-17, bd-2vq2z.13)
//!
//! The tier is a **separate local store** at `<user-data-root>/global/` (its
//! own `ee.db` + index directory) with full schema/migration parity to a
//! workspace store, surfaced through the existing trust-lane scope machinery
//! ([`crate::models::query::MemoryScope::Global`]). It is local-first: the
//! store lives on the same machine; there is no cloud.
//!
//! This deliberately **supersedes** ADR 0069's rejection of a separate global
//! DB for the user-global tier (ADR 0069 chose a `scope` column inside one
//! store; bd-1bfwa.2, its storage engine, is blocked). See
//! `docs/adr/0083-user-global-memory-store.md`. The existing scope/quota
//! machinery is **reused, not broken**: `MemoryScope::Global` selects the
//! global store, and the house-rules quota
//! ([`crate::core::house_rules`]) bounds the fan-in budget so global rules
//! never crowd out project context.
//!
//! # What lives here
//!
//! This module is the pure, deterministic policy core — testable without a
//! database:
//! - path resolution for the separate global store
//!   ([`GlobalStorePaths`]);
//! - the include / opt-out decision matrix
//!   ([`resolve_global_inclusion`]) — config, per-invocation flag, and
//!   per-workspace participation, each "off" reason distinct so posture is
//!   explainable and never silently dropped;
//! - **surfaced (never silent) cross-lane conflict resolution**
//!   ([`surface_lane_conflicts`]) — the non-negotiable from the bead: a
//!   workspace override is allowed but recorded for audit, and contradictions
//!   keep both sides visible with a conflict marker (assembly must never
//!   resolve a cross-lane contradiction by rank);
//! - budget-bounded fan-in ([`bounded_global_fan_in`]);
//! - the `ee.global_memory.v1` store-metadata block
//!   ([`GlobalMemoryStoreMetadata`]).
//!
//! The CLI / `remember` / `search` / `pack` / `backup` wiring adapts real
//! records to these cores in the follow-on leaves; keeping the policy here
//! makes it unit-testable in isolation.

use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

use serde::Serialize;
use serde_json::{Value, json};

use crate::core::house_rules::{
    HouseRulesQuotaInput, house_rules_quota, select_within_house_rules_quota,
};
use crate::db::{CreateWorkspaceInput, DbConnection, StoredMemory};

pub use crate::models::GLOBAL_MEMORY_SCHEMA_V1;

/// Degraded code emitted when the global tier is not contributing/consuming
/// (disabled store-wide, the workspace is not participating, or no store exists
/// yet). Mirrors ADR 0069's vocabulary so the two designs report posture with a
/// single code.
pub const GLOBAL_MEMORY_DISABLED_CODE: &str = "global_lane_disabled";

/// Marker substring in the read-path error raised when the user-global store
/// is present but needs a schema migration.
///
/// `core::search` matches on this to report the optional lane as skipped
/// (`global_lane_migration_required`, info) instead of as a failure to verify
/// the scope of the workspace's own results. The error is a plain `String`, so
/// the constant is the only thing keeping producer and consumer from drifting
/// apart silently.
pub const GLOBAL_STORE_NEEDS_MIGRATION_MARKER: &str = "needs migration";

/// The provenance lane label attached to every memory surfaced from the global
/// tier, so an agent always knows a memory came from the user-global store and
/// not the current workspace.
pub const GLOBAL_PROVENANCE_LANE: &str = "global";

/// Directory name of the separate global store under the user data root.
pub const GLOBAL_STORE_DIRNAME: &str = "global";

/// Default ee user-data suffix under `$HOME` when `XDG_DATA_HOME` is absent.
pub const DEFAULT_USER_DATA_SUFFIX: &str = ".local/share/ee";

/// Default fan-in budget for the global tier, in basis points of the pack
/// budget (1500 bp = 15%). A small bounded slice so global rules never crowd
/// out project context; configurable via `[memory] global_fan_in_basis_points`.
pub const DEFAULT_GLOBAL_FAN_IN_BASIS_POINTS: u32 = 1_500;

/// Resolved on-disk locations for the separate global store.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GlobalStorePaths {
    /// The global-store root directory (e.g. `~/.local/share/ee/global`).
    pub root: PathBuf,
    /// The global store database (`<root>/ee.db`).
    pub database_path: PathBuf,
    /// The derived global search-index directory (`<root>/indexes`).
    pub index_dir: PathBuf,
}

impl GlobalStorePaths {
    /// Derive the global-store paths from the **user data root** — the
    /// directory the workspace store database lives in by default. With the
    /// built-in layout (`~/.local/share/ee/ee.db`) the global store is
    /// `~/.local/share/ee/global/{ee.db,indexes}`.
    ///
    /// The global store must follow the *user*, not a project-local workspace
    /// DB, so callers pass the resolved user data dir here — never a
    /// project-local `.ee/` path.
    #[must_use]
    pub fn from_data_root(user_data_root: &Path) -> Self {
        Self::from_root(&user_data_root.join(GLOBAL_STORE_DIRNAME))
    }

    /// Build paths from an explicit global-store root (config override).
    #[must_use]
    pub fn from_root(root: &Path) -> Self {
        Self {
            root: root.to_path_buf(),
            database_path: root.join("ee.db"),
            index_dir: root.join("indexes"),
        }
    }

    /// Resolve the global-store paths, honoring an optional configured root
    /// override; otherwise `<user_data_root>/global`.
    #[must_use]
    pub fn resolve(user_data_root: &Path, configured_root: Option<&Path>) -> Self {
        match configured_root {
            Some(root) => Self::from_root(root),
            None => Self::from_data_root(user_data_root),
        }
    }
}

/// Resolve the ee user-data root from environment values without touching the
/// filesystem. `XDG_DATA_HOME=/tmp/data` maps to `/tmp/data/ee`; otherwise
/// `HOME=/home/a` maps to `/home/a/.local/share/ee`.
#[must_use]
pub fn default_user_data_root_from_values(
    xdg_data_home: Option<&OsStr>,
    home: Option<&OsStr>,
) -> Option<PathBuf> {
    xdg_data_home
        .filter(|value| !value.is_empty())
        .map(|root| PathBuf::from(root).join("ee"))
        .or_else(|| {
            home.filter(|value| !value.is_empty())
                .map(|root| PathBuf::from(root).join(DEFAULT_USER_DATA_SUFFIX))
        })
}

/// Resolve the current process' default ee user-data root.
///
/// This intentionally uses XDG/HOME rather than a workspace-local `.ee` path:
/// the global tier follows the user across repositories.
pub fn default_user_data_root_from_env() -> Result<PathBuf, String> {
    default_user_data_root_from_values(
        std::env::var_os("XDG_DATA_HOME").as_deref(),
        std::env::var_os("HOME").as_deref(),
    )
    .ok_or_else(|| "global memory store requires XDG_DATA_HOME or HOME".to_owned())
}

/// Resolve the current process' default separate global-store paths.
pub fn default_global_store_paths_from_env() -> Result<GlobalStorePaths, String> {
    default_user_data_root_from_env().map(|root| GlobalStorePaths::from_data_root(&root))
}

/// Why the global tier is or is not included in a retrieval. Each "off" reason
/// is distinct so the posture is explainable instead of a silent no-op.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum GlobalInclusionReason {
    /// The global tier is active and will be consulted.
    Included,
    /// `[memory] include_global = false` disabled it store-wide.
    DisabledByConfig,
    /// A per-invocation `--no-global` flag disabled it.
    DisabledByFlag,
    /// The workspace opted out of participation (`participate = false`).
    NotParticipating,
    /// No global store exists on disk yet.
    StoreAbsent,
}

impl GlobalInclusionReason {
    /// Stable machine token for the reason.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Included => "included",
            Self::DisabledByConfig => "disabled_by_config",
            Self::DisabledByFlag => "disabled_by_flag",
            Self::NotParticipating => "not_participating",
            Self::StoreAbsent => "store_absent",
        }
    }

    /// The degraded code to surface when this reason means the tier is off, or
    /// `None` when the tier is included. All "off" reasons share the
    /// [`GLOBAL_MEMORY_DISABLED_CODE`] posture code.
    #[must_use]
    pub const fn degraded_code(self) -> Option<&'static str> {
        match self {
            Self::Included => None,
            _ => Some(GLOBAL_MEMORY_DISABLED_CODE),
        }
    }
}

/// The resolved include/opt-out decision for one retrieval.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GlobalInclusionDecision {
    /// Whether the global tier participates in this retrieval.
    pub included: bool,
    /// The deciding reason (the first failing gate, or `Included`).
    pub reason: GlobalInclusionReason,
}

/// Inputs to the include/opt-out decision.
#[derive(Clone, Copy, Debug)]
pub struct GlobalInclusionInput {
    /// Whether the global store exists on disk.
    pub store_present: bool,
    /// Whether this workspace participates in the global tier
    /// (`participate = true`). The hard privacy boundary from ADR 0069 §5.
    pub participating: bool,
    /// Whether `[memory] include_global` is enabled store-wide.
    pub config_enabled: bool,
    /// Whether the per-invocation `--no-global` flag was set.
    pub no_global_flag: bool,
}

/// Resolve whether the global tier is included.
///
/// Precedence of "off" gates, evaluated in order so the surfaced reason is
/// stable: store presence → participation → config → per-invocation flag. A
/// missing store, a non-participating workspace, a store-wide config opt-out,
/// and a per-call `--no-global` are each reported distinctly; none silently
/// collapses into another.
#[must_use]
pub fn resolve_global_inclusion(input: &GlobalInclusionInput) -> GlobalInclusionDecision {
    let reason = if !input.store_present {
        GlobalInclusionReason::StoreAbsent
    } else if !input.participating {
        GlobalInclusionReason::NotParticipating
    } else if !input.config_enabled {
        GlobalInclusionReason::DisabledByConfig
    } else if input.no_global_flag {
        GlobalInclusionReason::DisabledByFlag
    } else {
        GlobalInclusionReason::Included
    };
    GlobalInclusionDecision {
        included: matches!(reason, GlobalInclusionReason::Included),
        reason,
    }
}

/// Which store a retrieval candidate came from.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum MemoryLane {
    /// The current workspace store.
    Workspace,
    /// Team-synced inbound or teammate-attributed rows.
    Team,
    /// The user-global store.
    Global,
}

impl MemoryLane {
    /// Stable machine token for the lane.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Workspace => "workspace",
            Self::Team => "team",
            Self::Global => "global",
        }
    }
}

/// ADR 0086 TC-D16 / bd-1bfwa: lower rank is more specific.
/// Local workspace beats team beats global on overlap.
#[must_use]
pub const fn lane_specificity_rank(lane: MemoryLane) -> u8 {
    match lane {
        MemoryLane::Workspace => 0,
        MemoryLane::Team => 1,
        MemoryLane::Global => 2,
    }
}

/// A minimal view of a retrieval candidate for cross-lane conflict analysis.
///
/// `conflict_key` is a normalized subject (e.g. a rule subject, a shared tag,
/// or a normalized title) the wiring layer derives; `content_hash`
/// distinguishes identical content from divergent content under the same
/// subject. Holding only ids/keys/hashes here keeps the policy decoupled from
/// the full memory record.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LaneCandidate {
    /// The memory id.
    pub id: String,
    /// Which store the candidate came from.
    pub lane: MemoryLane,
    /// Normalized subject used to detect overlap/contradiction across lanes.
    pub conflict_key: String,
    /// Content fingerprint, used to tell corroboration from contradiction.
    pub content_hash: String,
}

/// How a workspace/global pair relate on a shared conflict key.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum LaneConflictKind {
    /// Same subject AND same content: repetition is a trust signal. The
    /// workspace row wins; the global row is annotated as corroboration.
    Corroboration,
    /// Same subject, **divergent** content: a genuine contradiction. Both sides
    /// are surfaced with a conflict marker; assembly must NOT resolve it by
    /// rank — hiding exactly the disagreement the user must see.
    Contradiction,
}

impl LaneConflictKind {
    /// Stable machine token for the conflict kind.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Corroboration => "corroboration",
            Self::Contradiction => "contradiction",
        }
    }
}

/// A surfaced cross-lane relationship between a workspace row and a global row.
///
/// Provenance is always explicit and nothing is silently dropped or merged: for
/// a contradiction both rows stay visible and neither auto-wins; for
/// corroboration the workspace row takes precedence but that decision is
/// recorded (`workspace_overrides`) for audit rather than applied invisibly.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LaneConflict {
    /// The shared subject the two rows collide on.
    pub conflict_key: String,
    /// Corroboration vs contradiction.
    pub kind: LaneConflictKind,
    /// The workspace-store memory id.
    pub workspace_id: String,
    /// The global-store memory id.
    pub global_id: String,
    /// Whether both rows remain visible. Always `true` — the non-negotiable.
    pub both_surfaced: bool,
    /// Whether the workspace row takes recorded precedence. `true` only for
    /// corroboration; `false` for a contradiction (which routes to review).
    pub workspace_overrides: bool,
}

impl LaneConflict {
    /// Render as a redaction-safe JSON marker (ids/keys/kind only).
    #[must_use]
    pub fn data_json(&self) -> Value {
        json!({
            "conflictKey": self.conflict_key,
            "kind": self.kind.as_str(),
            "workspaceId": self.workspace_id,
            "globalId": self.global_id,
            "bothSurfaced": self.both_surfaced,
            "workspaceOverrides": self.workspace_overrides,
        })
    }
}

/// Surface cross-lane conflicts between workspace and global candidates.
///
/// Deterministic and insertion-order independent: candidates are grouped by
/// `conflict_key` (via a [`BTreeMap`]); within a shared key, every
/// workspace×global pair is classified. Same content hash → corroboration
/// (workspace wins, recorded; global kept as corroboration); divergent content
/// → contradiction (both surfaced, no silent resolution). Output is sorted by
/// `(conflict_key, workspace_id, global_id)` for byte-stable JSON.
#[must_use]
pub fn surface_lane_conflicts(candidates: &[LaneCandidate]) -> Vec<LaneConflict> {
    let mut by_key: BTreeMap<&str, (Vec<&LaneCandidate>, Vec<&LaneCandidate>)> = BTreeMap::new();
    for candidate in candidates {
        let entry = by_key.entry(candidate.conflict_key.as_str()).or_default();
        match candidate.lane {
            MemoryLane::Workspace => entry.0.push(candidate),
            MemoryLane::Global => entry.1.push(candidate),
            MemoryLane::Team => {}
        }
    }

    let mut out = Vec::new();
    for (key, (workspace_rows, global_rows)) in by_key {
        for workspace in &workspace_rows {
            for global in &global_rows {
                let kind = if workspace.content_hash == global.content_hash {
                    LaneConflictKind::Corroboration
                } else {
                    LaneConflictKind::Contradiction
                };
                out.push(LaneConflict {
                    conflict_key: key.to_owned(),
                    kind,
                    workspace_id: workspace.id.clone(),
                    global_id: global.id.clone(),
                    both_surfaced: true,
                    workspace_overrides: matches!(kind, LaneConflictKind::Corroboration),
                });
            }
        }
    }

    out.sort_by(|a, b| {
        a.conflict_key
            .cmp(&b.conflict_key)
            .then_with(|| a.workspace_id.cmp(&b.workspace_id))
            .then_with(|| a.global_id.cmp(&b.global_id))
    });
    out
}

/// A surfaced relationship between two different lanes on a shared subject.
///
/// Workspace × global pairs still also project through [`LaneConflict`]. This
/// type is the three-lane surface cited by ADR 0086 TC-D16: more-specific
/// context wins on overlap; contradiction keeps both sides and records that
/// neither auto-wins.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PrecedenceConflict {
    /// The shared subject the two rows collide on.
    pub conflict_key: String,
    /// Corroboration vs contradiction.
    pub kind: LaneConflictKind,
    /// More-specific lane in the TC-D16 chain.
    pub more_specific_lane: MemoryLane,
    /// Memory id on the more-specific lane.
    pub more_specific_id: String,
    /// Less-specific lane in the TC-D16 chain.
    pub less_specific_lane: MemoryLane,
    /// Memory id on the less-specific lane.
    pub less_specific_id: String,
    /// Whether both rows remain visible. Always `true`.
    pub both_surfaced: bool,
    /// Whether the more-specific row takes recorded precedence. `true` only
    /// for corroboration; `false` for contradiction.
    pub more_specific_overrides: bool,
}

impl PrecedenceConflict {
    /// Render as a redaction-safe JSON marker (ids/keys/kind/lanes only).
    #[must_use]
    pub fn data_json(&self) -> Value {
        json!({
            "conflictKey": self.conflict_key,
            "kind": self.kind.as_str(),
            "moreSpecificLane": self.more_specific_lane.as_str(),
            "moreSpecificId": self.more_specific_id,
            "lessSpecificLane": self.less_specific_lane.as_str(),
            "lessSpecificId": self.less_specific_id,
            "bothSurfaced": self.both_surfaced,
            "moreSpecificOverrides": self.more_specific_overrides,
        })
    }
}

/// Surface cross-lane conflicts across workspace, team, and global.
///
/// Deterministic and insertion-order independent. Every pair of distinct
/// lanes that share a `conflict_key` is classified. Same content hash →
/// corroboration (more-specific wins, recorded); divergent content →
/// contradiction (both surfaced, no silent resolution). Output is sorted by
/// `(conflict_key, more_specific_id, less_specific_id, more_specific_lane,
/// less_specific_lane)`.
#[must_use]
pub fn surface_precedence_conflicts(candidates: &[LaneCandidate]) -> Vec<PrecedenceConflict> {
    let mut by_key: BTreeMap<&str, BTreeMap<MemoryLane, Vec<&LaneCandidate>>> = BTreeMap::new();
    for candidate in candidates {
        by_key
            .entry(candidate.conflict_key.as_str())
            .or_default()
            .entry(candidate.lane)
            .or_default()
            .push(candidate);
    }

    let lanes = [MemoryLane::Workspace, MemoryLane::Team, MemoryLane::Global];
    let mut out = Vec::new();
    for (key, by_lane) in by_key {
        for (left_index, left_lane) in lanes.iter().enumerate() {
            for right_lane in lanes.iter().skip(left_index.saturating_add(1)) {
                let Some(left_rows) = by_lane.get(left_lane) else {
                    continue;
                };
                let Some(right_rows) = by_lane.get(right_lane) else {
                    continue;
                };
                for left in left_rows {
                    for right in right_rows {
                        // Iteration order is workspace, team, global, so
                        // `left_lane` is always more specific.
                        let (more, less) = (*left, *right);
                        let kind = if more.content_hash == less.content_hash {
                            LaneConflictKind::Corroboration
                        } else {
                            LaneConflictKind::Contradiction
                        };
                        out.push(PrecedenceConflict {
                            conflict_key: key.to_owned(),
                            kind,
                            more_specific_lane: more.lane,
                            more_specific_id: more.id.clone(),
                            less_specific_lane: less.lane,
                            less_specific_id: less.id.clone(),
                            both_surfaced: true,
                            more_specific_overrides: matches!(
                                kind,
                                LaneConflictKind::Corroboration
                            ),
                        });
                    }
                }
            }
        }
    }

    out.sort_by(|left, right| {
        left.conflict_key
            .cmp(&right.conflict_key)
            .then_with(|| left.more_specific_id.cmp(&right.more_specific_id))
            .then_with(|| left.less_specific_id.cmp(&right.less_specific_id))
            .then_with(|| {
                left.more_specific_lane
                    .as_str()
                    .cmp(right.more_specific_lane.as_str())
            })
            .then_with(|| {
                left.less_specific_lane
                    .as_str()
                    .cmp(right.less_specific_lane.as_str())
            })
    });
    out
}

/// The result of bounding the global tier's fan-in to a share of the budget.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GlobalFanIn {
    /// The hard token cap for global candidates this pack.
    pub cap_tokens: u64,
    /// `false` when the workspace opted out (cap is then zero).
    pub enabled: bool,
    /// Indices (into the input cost slice) of the selected global candidates,
    /// in caller-supplied priority order.
    pub selected: Vec<usize>,
}

/// Select global candidates whose cumulative token cost stays within a bounded
/// share of the pack budget.
///
/// Reuses the house-rules quota ([`crate::core::house_rules`]) so the global
/// tier obeys the same "never crowd out project context" guarantee. A
/// per-workspace opt-out yields a zero cap and an empty selection.
#[must_use]
pub fn bounded_global_fan_in(
    global_item_token_costs: &[u64],
    total_budget_tokens: u64,
    quota_basis_points: u32,
    opted_out: bool,
) -> GlobalFanIn {
    let quota = house_rules_quota(&HouseRulesQuotaInput {
        total_budget_tokens,
        quota_basis_points,
        workspace_opted_out: opted_out,
    });
    let selected = if quota.enabled {
        select_within_house_rules_quota(global_item_token_costs, quota.cap_tokens)
    } else {
        Vec::new()
    };
    GlobalFanIn {
        cap_tokens: quota.cap_tokens,
        enabled: quota.enabled,
        selected,
    }
}

/// Store-metadata block (`ee.global_memory.v1`) describing the separate global
/// store: where it lives, whether it is enabled/participating, and a content
/// census. Counts/paths/flags only — never memory content (redaction-safe).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GlobalMemoryStoreMetadata {
    /// Absolute path to the global store database.
    pub database_path: String,
    /// Absolute path to the global store's search-index directory.
    pub index_dir: String,
    /// Whether the store exists on disk.
    pub present: bool,
    /// Whether `[memory] include_global` is enabled store-wide.
    pub enabled: bool,
    /// Whether the current workspace participates in the global tier.
    pub participating: bool,
    /// The schema/migration version of the global store (parity with workspace).
    pub schema_version: String,
    /// Number of memories in the global store.
    pub memory_count: u64,
    /// RFC 3339 last-modified timestamp of the store, when known.
    pub last_modified: Option<String>,
}

impl GlobalMemoryStoreMetadata {
    /// Render the redaction-safe `ee.global_memory.v1` metadata block.
    #[must_use]
    pub fn data_json(&self) -> Value {
        json!({
            "schema": GLOBAL_MEMORY_SCHEMA_V1,
            "databasePath": self.database_path,
            "indexDir": self.index_dir,
            "present": self.present,
            "enabled": self.enabled,
            "participating": self.participating,
            "schemaVersion": self.schema_version,
            "memoryCount": self.memory_count,
            "lastModified": self.last_modified,
        })
    }
}

/// Display name for the single workspace row inside the user-global store.
pub const GLOBAL_WORKSPACE_NAME: &str = "ee-global";

/// The store-root path string used as the workspace key for the global store.
///
/// `remember`/search derive the workspace id from this same path
/// through the occupancy-aware workspace bind, so writes and reads
/// agree on a single stable global workspace even when the store root
/// is spelled lexically versus canonically.
#[must_use]
pub fn global_workspace_key(paths: &GlobalStorePaths) -> String {
    paths.root.to_string_lossy().into_owned()
}

/// Deterministic workspace id for the global store, matching the id `remember`
/// derives from the store root so the write path (a `remember` pointed at the
/// global db) and the read path resolve the same workspace.
#[must_use]
pub fn global_workspace_id(paths: &GlobalStorePaths) -> String {
    crate::core::curate::stable_workspace_id(&paths.root)
}

/// Open the separate user-global store at `paths`, creating the directory,
/// database, schema, and the single stable global workspace row if absent.
///
/// This is the real storage engine for the user-global memory tier (ADR 0083,
/// bd-1pq3c): the global store is a standalone `ee.db` with full schema parity
/// to a workspace store, so the engine reuses [`DbConnection`]/`migrate` and a
/// deterministic workspace id derived from the store root. It mirrors the
/// `ee init` create→migrate→workspace flow so a `remember` pointed at the
/// returned database writes into the same workspace this returns.
///
/// Idempotent: re-opening an existing store returns the existing workspace id.
///
/// # Errors
///
/// Returns a message when the directory, database, migration, or workspace row
/// cannot be created.
pub fn open_or_create_global_store(
    paths: &GlobalStorePaths,
) -> Result<(DbConnection, String), String> {
    std::fs::create_dir_all(&paths.root).map_err(|error| {
        format!(
            "failed to create global store directory {}: {error}",
            paths.root.display()
        )
    })?;
    let connection = DbConnection::open_file(&paths.database_path)
        .map_err(|error| format!("failed to open global store database: {error}"))?;
    connection
        .migrate()
        .map_err(|error| format!("failed to migrate global store database: {error}"))?;
    let workspace_id = ensure_global_workspace_row(&connection, paths)?;
    Ok((connection, workspace_id))
}

/// Ensure the single global workspace row exists, returning its id. Resolves an
/// existing row by path first so the id stays stable across calls.
fn ensure_global_workspace_row(
    connection: &DbConnection,
    paths: &GlobalStorePaths,
) -> Result<String, String> {
    let workspace_key = global_workspace_key(paths);
    let requested = global_workspace_id(paths);
    if let Some(existing) = crate::core::workspace::select_existing_workspace_row(
        connection,
        &requested,
        &[paths.root.as_path()],
    )
    .map_err(|error| format!("failed to check global workspace row: {}", error.message()))?
    {
        return Ok(existing.id);
    }
    connection
        .insert_workspace(
            &requested,
            &CreateWorkspaceInput {
                path: workspace_key,
                name: Some(GLOBAL_WORKSPACE_NAME.to_owned()),
            },
        )
        .map_err(|error| format!("failed to create global workspace row: {error}"))?;
    Ok(requested)
}

/// Read memories persisted in the user-global store (read-only).
///
/// Returns an empty vector when the store does not exist yet (no `remember
/// --global` has run), so callers can include the global tier unconditionally
/// without a pre-existence check. Resolves the workspace by path so it reads
/// exactly what the write path persisted.
///
/// # Errors
///
/// Returns a message when an existing store cannot be opened or queried.
pub fn read_global_store_memories(
    paths: &GlobalStorePaths,
    include_tombstoned: bool,
) -> Result<Vec<StoredMemory>, String> {
    if !paths.database_path.exists() {
        return Ok(Vec::new());
    }
    let connection = DbConnection::open_file_read_only(&paths.database_path)
        .map_err(|error| format!("failed to open global store database read-only: {error}"))?;
    let needs_migration = connection
        .needs_migration()
        .map_err(|error| format!("failed to inspect global store migration state: {error}"))?;
    if needs_migration {
        // Must keep containing `GLOBAL_STORE_NEEDS_MIGRATION_MARKER`; the
        // search read path classifies this case by that substring.
        return Err(
            "global store database needs migration; skipping read-only global tier".to_owned(),
        );
    }
    let requested = global_workspace_id(paths);
    let Some(workspace) = crate::core::workspace::select_existing_workspace_row(
        &connection,
        &requested,
        &[paths.root.as_path()],
    )
    .map_err(|error| {
        format!(
            "failed to resolve global workspace row: {}",
            error.message()
        )
    })?
    else {
        return Ok(Vec::new());
    };
    connection
        .list_memories(&workspace.id, None, include_tombstoned)
        .map_err(|error| format!("failed to list global store memories: {error}"))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn path_safe_tempdir(prefix: &str) -> Result<tempfile::TempDir, String> {
        let system_temp = Path::new("/tmp");
        if system_temp.is_dir() {
            let root =
                std::fs::canonicalize(system_temp).unwrap_or_else(|_| system_temp.to_path_buf());
            return tempfile::Builder::new()
                .prefix(prefix)
                .tempdir_in(root)
                .map_err(|error| error.to_string());
        }
        tempfile::Builder::new()
            .prefix(prefix)
            .tempdir()
            .map_err(|error| error.to_string())
    }

    #[test]
    fn global_store_create_is_idempotent_and_persists_memories() -> Result<(), String> {
        use crate::db::CreateMemoryInput;

        let tempdir = path_safe_tempdir("ee-global-store.")?;
        let paths = GlobalStorePaths::from_root(&tempdir.path().join("global"));

        // Reading before any write returns empty — the store does not exist yet,
        // so callers can include the global tier without a pre-existence check.
        assert!(read_global_store_memories(&paths, false)?.is_empty());

        // First open creates the directory, database, schema, and workspace row.
        let (connection, workspace_id) = open_or_create_global_store(&paths)?;
        assert!(paths.database_path.exists());
        assert_eq!(workspace_id, global_workspace_id(&paths));

        // Write a memory into the same store `remember --global` targets.
        connection
            .insert_memory(
                &crate::testing::mem("global"),
                &CreateMemoryInput {
                    workspace_id: workspace_id.clone(),
                    level: "semantic".to_owned(),
                    kind: "note".to_owned(),
                    content: "always run cargo fmt --check before release".to_owned(),
                    workflow_id: None,
                    confidence: 0.9,
                    utility: 0.0,
                    importance: 0.0,
                    provenance_uri: None,
                    trust_class: "human_explicit".to_owned(),
                    trust_subclass: None,
                    tags: vec!["global".to_owned()],
                    valid_from: None,
                    valid_to: None,
                },
            )
            .map_err(|error| error.to_string())?;
        drop(connection);

        // Re-opening is idempotent: the workspace id stays stable.
        let (again, workspace_id_again) = open_or_create_global_store(&paths)?;
        assert_eq!(workspace_id, workspace_id_again);
        drop(again);

        // The write persists and is read back from a fresh connection.
        let memories = read_global_store_memories(&paths, false)?;
        assert_eq!(memories.len(), 1);
        assert_eq!(
            memories[0].content,
            "always run cargo fmt --check before release"
        );
        assert_eq!(memories[0].workspace_id, workspace_id);
        Ok(())
    }

    #[test]
    fn read_global_store_memories_reports_pending_migration_without_writes() -> Result<(), String> {
        let tempdir = path_safe_tempdir("ee-global-store-stale.")?;
        let paths = GlobalStorePaths::from_root(&tempdir.path().join("global"));
        std::fs::create_dir_all(&paths.root).map_err(|error| error.to_string())?;

        {
            let connection =
                DbConnection::open_file(&paths.database_path).map_err(|error| error.to_string())?;
            assert!(
                !connection
                    .migration_table_exists()
                    .map_err(|error| error.to_string())?,
                "fresh database must not start with a migration table"
            );
        }

        let Err(error) = read_global_store_memories(&paths, false) else {
            return Err("stale global store read must report pending migration".to_owned());
        };
        assert!(
            error.contains(GLOBAL_STORE_NEEDS_MIGRATION_MARKER),
            "the read path's pending-migration error must keep containing \
             `{GLOBAL_STORE_NEEDS_MIGRATION_MARKER}`; core::search classifies \
             the skipped global lane by that substring. Got {error}"
        );

        let connection = DbConnection::open_file_read_only(&paths.database_path)
            .map_err(|error| error.to_string())?;
        assert!(
            connection
                .needs_migration()
                .map_err(|error| error.to_string())?,
            "read-only global store read must leave migration state pending"
        );
        assert!(
            !connection
                .migration_table_exists()
                .map_err(|error| error.to_string())?,
            "read-only global store read must not create migration metadata"
        );
        Ok(())
    }

    fn candidate(id: &str, lane: MemoryLane, key: &str, hash: &str) -> LaneCandidate {
        LaneCandidate {
            id: id.to_owned(),
            lane,
            conflict_key: key.to_owned(),
            content_hash: hash.to_owned(),
        }
    }

    #[test]
    fn paths_derive_under_global_subdir_of_data_root() {
        let paths = GlobalStorePaths::from_data_root(Path::new("/home/agent/.local/share/ee"));
        assert_eq!(
            paths.root,
            PathBuf::from("/home/agent/.local/share/ee/global")
        );
        assert_eq!(
            paths.database_path,
            PathBuf::from("/home/agent/.local/share/ee/global/ee.db")
        );
        assert_eq!(
            paths.index_dir,
            PathBuf::from("/home/agent/.local/share/ee/global/indexes")
        );
    }

    #[test]
    fn resolve_honors_configured_root_override() {
        let from_root =
            GlobalStorePaths::resolve(Path::new("/data"), Some(Path::new("/custom/global")));
        assert_eq!(
            from_root.database_path,
            PathBuf::from("/custom/global/ee.db")
        );

        let from_default = GlobalStorePaths::resolve(Path::new("/data"), None);
        assert_eq!(
            from_default.database_path,
            PathBuf::from("/data/global/ee.db")
        );
    }

    #[test]
    fn inclusion_decision_matrix_is_explainable() {
        // Fully enabled.
        let on = resolve_global_inclusion(&GlobalInclusionInput {
            store_present: true,
            participating: true,
            config_enabled: true,
            no_global_flag: false,
        });
        assert!(on.included);
        assert_eq!(on.reason, GlobalInclusionReason::Included);
        assert_eq!(on.reason.degraded_code(), None);

        // Absent store dominates every other gate.
        let absent = resolve_global_inclusion(&GlobalInclusionInput {
            store_present: false,
            participating: false,
            config_enabled: false,
            no_global_flag: true,
        });
        assert!(!absent.included);
        assert_eq!(absent.reason, GlobalInclusionReason::StoreAbsent);

        // Participation gate (hard privacy boundary) beats config and flag.
        let opted_out = resolve_global_inclusion(&GlobalInclusionInput {
            store_present: true,
            participating: false,
            config_enabled: true,
            no_global_flag: false,
        });
        assert_eq!(opted_out.reason, GlobalInclusionReason::NotParticipating);

        // Store-wide config opt-out.
        let config_off = resolve_global_inclusion(&GlobalInclusionInput {
            store_present: true,
            participating: true,
            config_enabled: false,
            no_global_flag: false,
        });
        assert_eq!(config_off.reason, GlobalInclusionReason::DisabledByConfig);

        // Per-invocation --no-global.
        let flag_off = resolve_global_inclusion(&GlobalInclusionInput {
            store_present: true,
            participating: true,
            config_enabled: true,
            no_global_flag: true,
        });
        assert_eq!(flag_off.reason, GlobalInclusionReason::DisabledByFlag);

        // Every "off" reason reports the shared posture code.
        for reason in [
            GlobalInclusionReason::DisabledByConfig,
            GlobalInclusionReason::DisabledByFlag,
            GlobalInclusionReason::NotParticipating,
            GlobalInclusionReason::StoreAbsent,
        ] {
            assert_eq!(reason.degraded_code(), Some(GLOBAL_MEMORY_DISABLED_CODE));
        }
    }

    #[test]
    fn identical_content_across_lanes_is_corroboration_workspace_wins() {
        let conflicts = surface_lane_conflicts(&[
            candidate("ws1", MemoryLane::Workspace, "fmt-before-release", "h1"),
            candidate("gl1", MemoryLane::Global, "fmt-before-release", "h1"),
        ]);
        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflicts[0].kind, LaneConflictKind::Corroboration);
        assert!(
            conflicts[0].both_surfaced,
            "global row stays as corroboration"
        );
        assert!(
            conflicts[0].workspace_overrides,
            "workspace row wins on identical content"
        );
    }

    #[test]
    fn divergent_content_across_lanes_is_a_surfaced_contradiction() {
        let conflicts = surface_lane_conflicts(&[
            candidate(
                "ws1",
                MemoryLane::Workspace,
                "rebase-policy",
                "rebase-always",
            ),
            candidate("gl1", MemoryLane::Global, "rebase-policy", "rebase-never"),
        ]);
        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflicts[0].kind, LaneConflictKind::Contradiction);
        // The non-negotiable: both surfaced, neither silently wins.
        assert!(conflicts[0].both_surfaced);
        assert!(
            !conflicts[0].workspace_overrides,
            "a contradiction must route to review, not auto-resolve by lane"
        );
    }

    #[test]
    fn unrelated_subjects_do_not_conflict() {
        let conflicts = surface_lane_conflicts(&[
            candidate("ws1", MemoryLane::Workspace, "subject-a", "h1"),
            candidate("gl1", MemoryLane::Global, "subject-b", "h2"),
        ]);
        assert!(conflicts.is_empty());
    }

    #[test]
    fn empty_conflict_input_returns_empty_marker_list() {
        let conflicts = surface_lane_conflicts(&[]);
        assert!(
            conflicts.is_empty(),
            "empty candidate sets must not fabricate global/workspace conflicts"
        );
    }

    #[test]
    fn conflict_surfacing_is_insertion_order_independent() {
        let forward = surface_lane_conflicts(&[
            candidate("ws1", MemoryLane::Workspace, "k", "a"),
            candidate("gl1", MemoryLane::Global, "k", "b"),
            candidate("gl2", MemoryLane::Global, "k", "a"),
        ]);
        let reversed = surface_lane_conflicts(&[
            candidate("gl2", MemoryLane::Global, "k", "a"),
            candidate("gl1", MemoryLane::Global, "k", "b"),
            candidate("ws1", MemoryLane::Workspace, "k", "a"),
        ]);
        assert_eq!(forward, reversed, "conflict output must be deterministic");
        assert_eq!(forward.len(), 2, "one corroboration + one contradiction");
    }

    #[test]
    fn lane_specificity_is_workspace_then_team_then_global() {
        assert!(
            lane_specificity_rank(MemoryLane::Workspace) < lane_specificity_rank(MemoryLane::Team)
        );
        assert!(
            lane_specificity_rank(MemoryLane::Team) < lane_specificity_rank(MemoryLane::Global)
        );
    }

    #[test]
    fn identical_content_across_workspace_and_team_is_local_override() {
        let conflicts = surface_precedence_conflicts(&[
            candidate("ws1", MemoryLane::Workspace, "fmt-before-release", "h1"),
            candidate("tm1", MemoryLane::Team, "fmt-before-release", "h1"),
        ]);
        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflicts[0].kind, LaneConflictKind::Corroboration);
        assert_eq!(conflicts[0].more_specific_lane, MemoryLane::Workspace);
        assert_eq!(conflicts[0].less_specific_lane, MemoryLane::Team);
        assert!(conflicts[0].both_surfaced);
        assert!(
            conflicts[0].more_specific_overrides,
            "local workspace wins on overlap with team"
        );
    }

    #[test]
    fn identical_content_across_team_and_global_is_team_override() {
        let conflicts = surface_precedence_conflicts(&[
            candidate("tm1", MemoryLane::Team, "fmt-before-release", "h1"),
            candidate("gl1", MemoryLane::Global, "fmt-before-release", "h1"),
        ]);
        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflicts[0].more_specific_lane, MemoryLane::Team);
        assert_eq!(conflicts[0].less_specific_lane, MemoryLane::Global);
        assert!(conflicts[0].more_specific_overrides);
    }

    #[test]
    fn workspace_team_contradiction_does_not_auto_resolve() {
        let conflicts = surface_precedence_conflicts(&[
            candidate(
                "ws1",
                MemoryLane::Workspace,
                "rebase-policy",
                "rebase-always",
            ),
            candidate("tm1", MemoryLane::Team, "rebase-policy", "rebase-never"),
        ]);
        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflicts[0].kind, LaneConflictKind::Contradiction);
        assert!(conflicts[0].both_surfaced);
        assert!(
            !conflicts[0].more_specific_overrides,
            "cross-lane contradiction must not silently pick a winner"
        );
    }

    #[test]
    fn three_lane_overlap_is_insertion_order_independent() {
        let forward = surface_precedence_conflicts(&[
            candidate("ws1", MemoryLane::Workspace, "k", "a"),
            candidate("tm1", MemoryLane::Team, "k", "a"),
            candidate("gl1", MemoryLane::Global, "k", "b"),
        ]);
        let reversed = surface_precedence_conflicts(&[
            candidate("gl1", MemoryLane::Global, "k", "b"),
            candidate("tm1", MemoryLane::Team, "k", "a"),
            candidate("ws1", MemoryLane::Workspace, "k", "a"),
        ]);
        assert_eq!(forward, reversed);
        assert_eq!(forward.len(), 3);
        assert!(forward.iter().any(|conflict| conflict.more_specific_lane
            == MemoryLane::Workspace
            && conflict.less_specific_lane == MemoryLane::Team
            && conflict.kind == LaneConflictKind::Corroboration));
        assert!(
            forward
                .iter()
                .any(|conflict| conflict.kind == LaneConflictKind::Contradiction
                    && !conflict.more_specific_overrides)
        );
    }

    #[test]
    fn fan_in_respects_bounded_budget() {
        // 1500 bp of 1000 tokens = 150-token cap.
        let fan = bounded_global_fan_in(
            &[100, 100, 40],
            1_000,
            DEFAULT_GLOBAL_FAN_IN_BASIS_POINTS,
            false,
        );
        assert!(fan.enabled);
        assert_eq!(fan.cap_tokens, 150);
        // First item (100) fits; second (100) overflows; third (40) backfills.
        assert_eq!(fan.selected, vec![0, 2]);
    }

    #[test]
    fn fan_in_opt_out_selects_nothing() {
        let fan =
            bounded_global_fan_in(&[10, 10], 10_000, DEFAULT_GLOBAL_FAN_IN_BASIS_POINTS, true);
        assert!(!fan.enabled);
        assert_eq!(fan.cap_tokens, 0);
        assert!(fan.selected.is_empty());
    }

    #[test]
    fn fan_in_zero_budget_selects_nothing_even_when_enabled() {
        let fan = bounded_global_fan_in(&[1, 2, 3], 0, DEFAULT_GLOBAL_FAN_IN_BASIS_POINTS, false);
        assert!(fan.enabled);
        assert_eq!(fan.cap_tokens, 0);
        assert!(
            fan.selected.is_empty(),
            "zero-token packs cannot admit global memories"
        );
    }

    #[test]
    fn store_metadata_json_is_stable_and_redaction_safe() {
        let meta = GlobalMemoryStoreMetadata {
            database_path: "/home/agent/.local/share/ee/global/ee.db".to_owned(),
            index_dir: "/home/agent/.local/share/ee/global/indexes".to_owned(),
            present: true,
            enabled: true,
            participating: true,
            schema_version: "v77".to_owned(),
            memory_count: 12,
            last_modified: Some("2026-06-17T00:00:00Z".to_owned()),
        };
        let value = meta.data_json();
        assert_eq!(value["schema"], GLOBAL_MEMORY_SCHEMA_V1);
        assert_eq!(value["memoryCount"], 12);
        assert_eq!(value["participating"], true);
        assert_eq!(value["databasePath"], meta.database_path);
        // Content is never present in the metadata block.
        assert!(value.get("content").is_none());
    }

    #[test]
    fn provenance_lane_label_is_global() {
        assert_eq!(GLOBAL_PROVENANCE_LANE, "global");
    }
}