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
//! The one place a redb page-cache ceiling is chosen for palace-scoped stores
//! (#7106).
//!
//! Why: `redb::Builder::default()` sets `cache_size` to 1 GiB, and redb treats
//! that as a ceiling it will grow into rather than a reservation. Every
//! memory-core store opened a `Database` without touching it, so a daemon
//! holding the default 64 resident palaces — each with `kg.redb`,
//! `index.usearch.redb` and `chat_sessions.redb` — carried a 192 GiB
//! theoretical page-cache ceiling. That is what turned a 233 MB daemon into an
//! 11–23 GB one under ordinary browsing: heap the allocator had handed to redb
//! page caches and never returned. trusty-search hit the same thing in #329 and
//! answered it with an explicit 64 MB ceiling; this module is that answer for
//! memory-core, in one place so the number can be tuned once.
//!
//! What: [`DEFAULT_PALACE_REDB_CACHE_MB`] (64 MB, sized off the largest
//! `kg.redb` observed on the reporter's host), the
//! [`PALACE_REDB_CACHE_MB_ENV`] override, and [`palace_db_builder`] /
//! [`create_palace_db`] — the builder and open call every palace-scoped redb
//! open in this workspace must route through.
//!
//! Fail-open check: an unparsable override must NOT silently leave redb on its
//! 1 GiB default, because that is the exact failure this module exists to
//! prevent and it would be invisible. [`parse_cache_mb`] returns the bounded
//! default plus the warning text naming the rejected value, and
//! [`palace_redb_cache_bytes`] logs it.
//!
//! Test: `parse_cache_mb_warns_and_keeps_the_bounded_default_on_garbage`,
//! `parse_cache_mb_accepts_a_valid_override`,
//! `builder_uses_the_bounded_ceiling`.
//!
//! [`DEFAULT_PALACE_REDB_CACHE_MB`]: crate::redb_cache::DEFAULT_PALACE_REDB_CACHE_MB
//! [`PALACE_REDB_CACHE_MB_ENV`]: crate::redb_cache::PALACE_REDB_CACHE_MB_ENV
//! [`palace_db_builder`]: crate::redb_cache::palace_db_builder
//! [`create_palace_db`]: crate::redb_cache::create_palace_db
//! [`parse_cache_mb`]: crate::redb_cache::parse_cache_mb
//! [`palace_redb_cache_bytes`]: crate::redb_cache::palace_redb_cache_bytes
use ;
use Path;
/// Page-cache ceiling, in megabytes, applied to every palace-scoped redb file.
///
/// Why (#7106): redb's own default is 1 GiB per `Database`. Multiplied by three
/// files per palace and 64 resident palaces that ceiling is 192 GiB, and the
/// allocator grows into it. 64 MB comfortably exceeds the largest `kg.redb` on
/// the reporting host (the trusty-tools palace, 64 MB on disk), so a hot palace
/// still caches its whole graph, and it matches the ceiling trusty-search chose
/// for the same reason in #329.
/// What: 64, multiplied by 1 MiB in [`palace_redb_cache_bytes`].
/// Test: `builder_uses_the_bounded_ceiling`.
pub const DEFAULT_PALACE_REDB_CACHE_MB: usize = 64;
/// Environment variable overriding [`DEFAULT_PALACE_REDB_CACHE_MB`].
///
/// Why: an operator on a 128 GB host with one enormous palace may want a larger
/// ceiling, and an operator on the documented 16 GB minimum (#6802) may want a
/// smaller one. Tuning it must not need a rebuild.
/// What: read as decimal megabytes. Unset, empty, non-numeric, or `0` all fall
/// back to the bounded default — never to redb's 1 GiB.
/// Test: `parse_cache_mb_accepts_a_valid_override`,
/// `parse_cache_mb_warns_and_keeps_the_bounded_default_on_garbage`.
pub const PALACE_REDB_CACHE_MB_ENV: &str = "TRUSTY_MEMORY_REDB_CACHE_MB";
/// Decide the ceiling (in MB) from a raw override string, with the warning the
/// caller must log when the value was rejected.
///
/// Why (#7106, Fail-Open Check): the failure mode this module guards is
/// invisible — a daemon that silently falls back would keep redb's 1 GiB
/// default and look exactly like a daemon that was tuned. Separating the
/// decision from the logging makes "a rejected value produces a warning naming
/// it, and the bounded default" a pure, deterministic assertion rather than a
/// log-capture test.
/// What: `None`, empty, non-numeric, and `0` all yield
/// [`DEFAULT_PALACE_REDB_CACHE_MB`]. A rejected non-empty value also yields the
/// warning text, which names the variable and the offending value verbatim.
/// A valid positive value is returned with no warning.
/// Test: `parse_cache_mb_accepts_a_valid_override`,
/// `parse_cache_mb_warns_and_keeps_the_bounded_default_on_garbage`.
/// The page-cache ceiling in bytes for a palace-scoped redb file.
///
/// Why: every open needs the same number, and the env read plus the fail-open
/// warning belong in one place rather than at each call site.
/// What: [`parse_cache_mb`] over [`PALACE_REDB_CACHE_MB_ENV`], logged at `warn`
/// when the value was rejected, multiplied by 1 MiB.
/// Test: `builder_uses_the_bounded_ceiling`.
/// A [`redb::Builder`] carrying the palace page-cache ceiling.
///
/// Why (#7106): this is the single tuning point the module header promises.
/// A palace-scoped `Database::create` / `Database::open` that bypasses it
/// silently reinstates redb's 1 GiB ceiling for that file.
/// What: `Database::builder().set_cache_size(palace_redb_cache_bytes())`.
/// Test: `builder_uses_the_bounded_ceiling`.
/// Create or open a palace-scoped redb database with the bounded ceiling.
///
/// Why: `Database::create(path)` is the call every memory-core store used, and
/// it is the one that takes redb's 1 GiB default. This is its drop-in
/// replacement, with the same signature and the same errors, so routing a call
/// site through it is a one-line change that cannot alter recovery behaviour.
/// What: [`palace_db_builder`] then `.create(path)`.
/// Test: `create_palace_db_round_trips`, and
/// `every_memory_core_redb_open_is_bounded` (which fails if any palace-scoped
/// open goes back to a bare `Database::create`).
/// Open a palace-scoped redb database read-only with the bounded ceiling.
///
/// Why: `ReadOnlyDatabase::open(path)` takes redb's 1 GiB default just as
/// `Database::create` does, and the read-only measurement path opens the same
/// palace files. A ceiling that applied only to the writable path would leave a
/// whole class of opens unbounded.
/// What: [`palace_db_builder`] then `.open_read_only(path)`.
/// Test: `every_memory_core_redb_open_is_bounded` pins that no palace-scoped
/// open bypasses this or [`create_palace_db`].