trusty-common 0.59.2

Shared utilities and provider-agnostic streaming chat (ChatProvider, OllamaProvider, OpenRouter, tool-use) for trusty-* projects
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
//! Tests for `PalaceRegistry::rename_palace` (#9544).
//!
//! Why: the rename moves a palace directory and rewrites the alias map, so
//! every refusal must leave both untouched and every partial run must either
//! roll back or be finishable by a re-run.
//! What: one test per refusal class, the happy path, `replace_empty`, both
//! resume points, the rolled-back move, and the reversed rename.
//! Test: this file.

use super::*;
use crate::memory_core::palace::{Drawer, Palace};
use crate::memory_core::store::chat_sessions::ChatSessionStore;
use std::collections::BTreeMap;
use tempfile::tempdir;

/// A short wait so `Busy` refusals answer quickly.
fn opts() -> RenameOptions<'static> {
    RenameOptions {
        busy_wait: Duration::from_millis(200),
        ..RenameOptions::default()
    }
}

/// Create palace `id` under `root` (its handle is dropped, so it is idle).
fn create(reg: &PalaceRegistry, root: &Path, id: &str) -> Arc<crate::memory_core::PalaceHandle> {
    let palace = Palace {
        id: PalaceId::new(id),
        name: id.to_string(),
        description: None,
        created_at: chrono::Utc::now(),
        data_dir: root.join(id),
    };
    reg.create_palace(root, palace).expect("create palace")
}

fn json_id(root: &Path, dir: &str) -> (String, String) {
    let p = PalaceStore::load_palace(&root.join(dir)).expect("load palace.json");
    (p.id.0, p.name)
}

fn aliases(root: &Path) -> BTreeMap<String, String> {
    PalaceAliasStore::load_aliases(root).expect("load aliases")
}

/// Why: the happy path — directory moved, id and name rewritten, the old id
/// and every alias of it reach the renamed palace.
/// Test: itself.
#[test]
fn rename_palace_moves_dir_and_rewrites_palace_json() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    drop(create(&reg, root, "old-p"));
    PalaceAliasStore::register_alias(root, "older", "old-p").unwrap();

    let out = reg
        .rename_palace(root, "old-p", "new-p", &opts())
        .expect("rename");
    assert_eq!((out.old.as_str(), out.new.as_str()), ("old-p", "new-p"));
    assert!(!out.resumed && out.name_rewritten && out.trashed_target.is_none());
    assert!(!root.join("old-p").exists());
    assert_eq!(json_id(root, "new-p"), ("new-p".into(), "new-p".into()));
    let map = aliases(root);
    assert_eq!(map.get("old-p").map(String::as_str), Some("new-p"));
    assert_eq!(map.get("older").map(String::as_str), Some("new-p"));
    for id in ["old-p", "older", "new-p"] {
        let h = reg.open_palace(root, &PalaceId::new(id)).expect("open");
        assert_eq!(h.id.as_str(), "new-p", "{id} reaches the renamed palace");
    }
}

/// Why: a target with drawers must never be replaced, even with
/// `replace_empty`, and the refusal changes nothing.
/// Test: itself.
#[test]
fn rename_palace_refuses_a_nonempty_target() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    drop(create(&reg, root, "old-p"));
    let target = create(&reg, root, "new-p");
    target
        .kg
        .upsert_drawer_sync(&Drawer::new(uuid::Uuid::new_v4(), "kept"))
        .unwrap();
    drop(target);

    let replace = RenameOptions {
        replace_empty: true,
        ..opts()
    };
    let err = reg
        .rename_palace(root, "old-p", "new-p", &replace)
        .unwrap_err();
    assert!(
        matches!(
            &err,
            PalaceRenameError::TargetNotEmpty {
                reason: PalaceNotEmpty::Drawers(1),
                ..
            }
        ),
        "{err:?}"
    );
    assert!(err.is_conflict());
    assert_eq!(json_id(root, "old-p").0, "old-p");
    assert_eq!(json_id(root, "new-p").0, "new-p");
    assert!(aliases(root).is_empty());
}

/// Why (#9544): chat history alone makes a target non-empty.
/// Test: itself.
#[test]
fn rename_palace_refuses_a_target_with_chat_sessions() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    drop(create(&reg, root, "old-p"));
    drop(create(&reg, root, "new-p"));
    let chat = ChatSessionStore::open(&root.join("new-p").join("chat_sessions.redb")).unwrap();
    chat.create_session(None).unwrap();
    drop(chat);

    let replace = RenameOptions {
        replace_empty: true,
        ..opts()
    };
    let err = reg
        .rename_palace(root, "old-p", "new-p", &replace)
        .unwrap_err();
    assert!(
        matches!(
            &err,
            PalaceRenameError::TargetNotEmpty {
                reason: PalaceNotEmpty::ChatSessions(1),
                ..
            }
        ),
        "{err:?}"
    );
    assert!(root.join("old-p").join("palace.json").exists());
}

/// Why: renaming an alias would move nothing it names; refuse and point at
/// the real palace.
/// Test: itself.
#[test]
fn rename_palace_refuses_an_alias_source() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    drop(create(&reg, root, "target-p"));
    PalaceAliasStore::register_alias(root, "alias-p", "target-p").unwrap();

    let err = reg
        .rename_palace(root, "alias-p", "new-p", &opts())
        .unwrap_err();
    assert!(
        matches!(&err, PalaceRenameError::SourceIsAlias { target, .. } if target == "target-p"),
        "{err:?}"
    );
    assert_eq!(json_id(root, "target-p").0, "target-p");
    assert!(!root.join("new-p").exists());
}

/// Why: a missing source is `NotFound`, distinct from a conflict.
/// Test: itself.
#[test]
fn rename_palace_refuses_a_missing_source() {
    let tmp = tempdir().unwrap();
    let reg = PalaceRegistry::new();
    let err = reg
        .rename_palace(tmp.path(), "ghost", "new-p", &opts())
        .unwrap_err();
    assert!(
        matches!(&err, PalaceRenameError::NotFound(id) if id == "ghost"),
        "{err:?}"
    );
    assert!(!err.is_conflict());
    let bad = reg
        .rename_palace(tmp.path(), "ghost", "Bad_Id", &opts())
        .unwrap_err();
    assert!(
        matches!(bad, PalaceRenameError::InvalidTarget { .. }),
        "{bad:?}"
    );
}

/// Why: an empty target is refused without `replace_empty`, and with it is
/// moved to `<root>/.trash/<new>-replaced-<UTC>/`, never deleted.
/// Test: itself.
#[test]
fn rename_palace_replace_empty_trashes_the_target() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    drop(create(&reg, root, "old-p"));
    drop(create(&reg, root, "new-p"));

    let err = reg
        .rename_palace(root, "old-p", "new-p", &opts())
        .unwrap_err();
    assert!(
        matches!(err, PalaceRenameError::TargetExists { .. }),
        "{err:?}"
    );
    assert!(aliases(root).is_empty(), "a refusal writes no alias");

    let replace = RenameOptions {
        replace_empty: true,
        ..opts()
    };
    let out = reg
        .rename_palace(root, "old-p", "new-p", &replace)
        .expect("rename");
    let trash = out.trashed_target.expect("target trashed");
    assert_eq!(trash.parent(), Some(root.join(".trash").as_path()));
    let name = trash.file_name().unwrap().to_string_lossy().into_owned();
    assert!(
        name.starts_with("new-p-replaced-") && !name.contains("reclaim"),
        "{name}"
    );
    assert_eq!(
        PalaceStore::load_palace(&trash).unwrap().id.as_str(),
        "new-p",
        "the replaced palace sits in the trash intact"
    );
    assert_eq!(json_id(root, "new-p").0, "new-p");
    assert!(!root.join("old-p").exists());
}

/// Why: a handle another caller holds must not be popped; the rename waits
/// `busy_wait`, then refuses with nothing changed.
/// Test: itself.
#[test]
fn rename_palace_refuses_a_referenced_handle_and_leaves_state_unchanged() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    let held = create(&reg, root, "old-p");

    let err = reg
        .rename_palace(root, "old-p", "new-p", &opts())
        .unwrap_err();
    assert!(
        matches!(&err, PalaceRenameError::Busy { palace, .. } if palace == "old-p"),
        "{err:?}"
    );
    assert!(
        reg.peek(&PalaceId::new("old-p")).is_some(),
        "the held handle stays cached"
    );
    assert_eq!(json_id(root, "old-p").0, "old-p");
    assert!(!root.join("new-p").exists());
    assert!(aliases(root).is_empty());
    drop(held);
}

/// Why: a crash after the alias write and before the move must be finishable
/// by re-running the same rename.
/// Test: itself.
#[test]
fn rename_palace_resumes_after_alias_written_but_dir_unmoved() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    drop(create(&reg, root, "old-p"));
    PalaceAliasStore::rename_target(root, "old-p", "new-p").unwrap();

    let out = reg
        .rename_palace(root, "old-p", "new-p", &opts())
        .expect("resume");
    assert!(out.resumed);
    assert!(!root.join("old-p").exists());
    assert_eq!(json_id(root, "new-p"), ("new-p".into(), "new-p".into()));
}

/// Why: a crash after the move and before the `palace.json` rewrite must be
/// finishable by re-running the same rename. #9544: in that state an open of
/// `new-p` caches its handle under the json id `old-p`; the resume must drop
/// it, or `old-p` serves a stale handle and `new-p` opens a second Writer.
/// Test: itself.
#[test]
fn rename_palace_resumes_after_dir_moved_but_id_unwritten() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new().with_writer_intent();
    drop(create(&reg, root, "old-p"));
    PalaceAliasStore::rename_target(root, "old-p", "new-p").unwrap();
    // Close the create-time handle (it points at the pre-move path).
    reg.remove(&PalaceId::new("old-p"));
    std::fs::rename(root.join("old-p"), root.join("new-p")).unwrap();
    assert_eq!(json_id(root, "new-p").0, "old-p");
    drop(
        reg.open_palace(root, &PalaceId::new("new-p"))
            .expect("open"),
    );
    assert!(
        reg.peek(&PalaceId::new("old-p")).is_some(),
        "cached under old"
    );

    let out = reg
        .rename_palace(root, "old-p", "new-p", &opts())
        .expect("resume");
    assert!(out.resumed && out.name_rewritten);
    assert_eq!(json_id(root, "new-p"), ("new-p".into(), "new-p".into()));
    assert!(
        reg.peek(&PalaceId::new("old-p")).is_none(),
        "stale handle gone"
    );
    let h = reg
        .open_palace(root, &PalaceId::new("new-p"))
        .expect("reopen");
    assert_eq!(h.id.as_str(), "new-p");
    assert!(!h.is_read_only(), "one Writer");
    assert_eq!(reg.len(), 1, "exactly one cached handle");
}

/// Why (#9544): `save_palace` refuses a newer-format palace, so a rename that
/// moved one first would stick half-done. The refusal must change nothing.
/// Test: itself.
#[test]
fn rename_palace_refuses_a_newer_format_source_and_changes_nothing() {
    use crate::memory_core::store::palace_format::PALACE_FORMAT_SUPPORTED;
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    drop(create(&reg, root, "old-p"));
    reg.remove(&PalaceId::new("old-p"));
    let json = root.join("old-p").join("palace.json");
    let mut v: serde_json::Value = serde_json::from_slice(&std::fs::read(&json).unwrap()).unwrap();
    v["format_version"] = serde_json::json!(PALACE_FORMAT_SUPPORTED + 1);
    std::fs::write(&json, serde_json::to_vec_pretty(&v).unwrap()).unwrap();
    let before = std::fs::read(&json).unwrap();

    let err = reg
        .rename_palace(root, "old-p", "new-p", &opts())
        .unwrap_err();
    assert!(matches!(err, PalaceRenameError::Io { .. }), "{err:?}");
    assert!(format!("{err:#}").contains("FormatTooNew"), "{err}");
    assert!(aliases(root).is_empty(), "no alias written");
    assert!(!root.join("new-p").exists(), "dir not moved");
    assert_eq!(
        std::fs::read(&json).unwrap(),
        before,
        "palace.json unchanged"
    );
}

/// Why: a failed move must restore the replaced target and exactly the alias
/// keys the rename wrote.
/// Test: itself.
#[test]
fn rename_palace_failed_move_rolls_back_alias_keys() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    drop(create(&reg, root, "old-p"));
    drop(create(&reg, root, "new-p"));
    PalaceAliasStore::register_alias(root, "older", "old-p").unwrap();
    PalaceAliasStore::register_alias(root, "new-p", "elsewhere").unwrap();
    let before = aliases(root);
    let old_dir = root.join("old-p");
    let failing = |from: &Path, to: &Path| {
        if from == old_dir {
            Err(std::io::Error::other("injected move failure"))
        } else {
            std::fs::rename(from, to)
        }
    };
    let replace = RenameOptions {
        replace_empty: true,
        ..opts()
    };

    let err = reg
        .rename_palace_with(root, "old-p", "new-p", &replace, &failing)
        .unwrap_err();
    assert!(matches!(err, PalaceRenameError::Io { .. }), "{err:?}");
    assert!(err.to_string().contains("rolled back"), "{err}");
    assert_eq!(aliases(root), before, "every touched key restored");
    assert_eq!(json_id(root, "old-p").0, "old-p");
    assert_eq!(
        json_id(root, "new-p").0,
        "new-p",
        "the trashed target is restored"
    );
}

/// Why: renaming back (`new` is an alias of `old`) is allowed, and both names
/// resolve to one palace, so the rename must take that palace's open-lock
/// once — taking it twice waits on itself until `Busy`.
/// Test: itself.
#[test]
fn rename_palace_allows_the_reverse_rename() {
    let tmp = tempdir().unwrap();
    let root = tmp.path();
    let reg = PalaceRegistry::new();
    drop(create(&reg, root, "a-p"));
    reg.rename_palace(root, "a-p", "b-p", &opts())
        .expect("first rename");

    let out = reg
        .rename_palace(root, "b-p", "a-p", &opts())
        .expect("reverse rename");
    assert_eq!(out.new.as_str(), "a-p");
    assert_eq!(json_id(root, "a-p"), ("a-p".into(), "a-p".into()));
    assert!(!root.join("b-p").exists());
    let map = aliases(root);
    assert_eq!(map.get("b-p").map(String::as_str), Some("a-p"));
    assert_eq!(map.get("a-p"), None, "no alias is keyed by the live id");
}