buntrail-services 0.3.0

Filesystem, Git, terminal, search and connection services.
Documentation
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
use crate::{
    connections::local::new_id,
    files::{atomic_private, io_error, private_directory},
};
use buntrail_core::{contracts::*, workspaces::*};
use std::{
    fs::{File, OpenOptions},
    io::Read,
    os::unix::{ffi::OsStrExt, fs::OpenOptionsExt},
    path::{Path, PathBuf},
};
// Disk catalogs retain documents and authority, never terminal restoration state.
fn clear_terminal_state(catalog: &mut serde_json::Value) -> bool {
    let mut changed = false;
    if let Some(records) = catalog.get_mut("records").and_then(|v| v.as_array_mut()) {
        for record in records {
            if let Some(record) = record.as_object_mut() {
                for key in ["terminal_layout", "terminal_starts", "terminal_height"] {
                    changed |= record.remove(key).is_some();
                }
                changed |= record.get("terminal_visible") != Some(&serde_json::Value::Bool(false));
                record.insert("terminal_visible".into(), serde_json::Value::Bool(false));
            }
        }
    }
    changed
}
pub struct WorkspaceStore {
    directory: PathBuf,
    lock: std::sync::Mutex<File>,
}
impl WorkspaceStore {
    pub fn new(directory: PathBuf) -> Result<Self, CoreError> {
        private_directory(&directory)?;
        let lock = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .mode(0o600)
            .custom_flags(libc::O_NOFOLLOW)
            .open(directory.join("lock"))
            .map_err(io_error)?;
        Ok(Self {
            directory,
            lock: std::sync::Mutex::new(lock),
        })
    }
    fn guard(&self) -> Result<StoreLock<'_>, CoreError> {
        let guard = self.lock.lock().map_err(io_error)?;
        guard.lock().map_err(io_error)?;
        Ok(StoreLock(guard))
    }
    pub fn load(&self) -> Result<WorkspaceCatalog, CoreError> {
        let _lock = self.guard()?;
        self.read()
    }
    fn read(&self) -> Result<WorkspaceCatalog, CoreError> {
        let path = self.directory.join("workspaces.json");
        let mut file = match OpenOptions::new()
            .read(true)
            .custom_flags(libc::O_NOFOLLOW)
            .open(&path)
        {
            Ok(f) => f,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                let c = WorkspaceCatalog {
                    schema: 1,
                    client: ClientID(new_id()?),
                    records: vec![],
                };
                self.write(&c)?;
                return Ok(c);
            }
            Err(e) => return Err(io_error(e)),
        };
        let mut bytes = vec![];
        Read::by_ref(&mut file)
            .take(4 * 1024 * 1024 + 1)
            .read_to_end(&mut bytes)
            .map_err(io_error)?;
        if bytes.len() > 4 * 1024 * 1024 {
            return Err(io_error("workspace state exceeds limit"));
        }
        let mut disk: serde_json::Value = serde_json::from_slice(&bytes).map_err(io_error)?;
        let migrated = clear_terminal_state(&mut disk);
        let c: WorkspaceCatalog = serde_json::from_value(disk).map_err(io_error)?;
        if c.schema != 1
            || !valid_git_recoveries(&c)
            || !valid_file_recoveries(&c)
            || c.records.len() > 100
            || c.records.iter().any(|r| {
                r.documents.len() > 64
                    || r.terminal_layout.validate().is_err()
                    || !valid_terminal_starts(r)
                    || r.terminal_height
                        .is_some_and(|h| !h.is_finite() || !(40.0..=10000.0).contains(&h))
            })
        {
            return Err(io_error("workspace schema or bounds invalid"));
        }
        if migrated {
            self.write(&c)?;
        }
        Ok(c)
    }
    pub fn save(&self, catalog: &WorkspaceCatalog) -> Result<(), CoreError> {
        let _lock = self.guard()?;
        let current = self.read()?;
        let mut catalog = catalog.clone();
        for old in &current.records {
            let Some(record) = catalog
                .records
                .iter_mut()
                .find(|record| record.id == old.id)
            else {
                if !old.git_recoveries.is_empty() || !old.file_recoveries.is_empty() {
                    return Err(CoreError::new(
                        ErrorCode::Busy,
                        "cannot drop retained Git outcomes in a catalog write",
                    ));
                }
                continue;
            };
            if record.target != old.target || record.lease != old.lease {
                return Err(CoreError::new(
                    ErrorCode::TargetChanged,
                    "retained Git recovery identity changed",
                ));
            }
            record.git_recoveries = old.git_recoveries.clone();
            record.file_recoveries = old.file_recoveries.clone();
            record.git_recovery_generation = old.git_recovery_generation;
        }
        self.write(&catalog)
    }
    fn write(&self, catalog: &WorkspaceCatalog) -> Result<(), CoreError> {
        let mut disk = serde_json::to_value(catalog).map_err(io_error)?;
        clear_terminal_state(&mut disk);
        let bytes = serde_json::to_vec(&disk).map_err(io_error)?;
        if catalog.schema != 1
            || !valid_git_recoveries(catalog)
            || !valid_file_recoveries(catalog)
            || catalog.records.len() > 100
            || catalog.records.iter().any(|r| {
                r.terminal_layout.validate().is_err()
                    || !valid_terminal_starts(r)
                    || r.terminal_height
                        .is_some_and(|h| !h.is_finite() || !(40.0..=10000.0).contains(&h))
            })
            || bytes.len() > 4 * 1024 * 1024
        {
            return Err(io_error("workspace state exceeds limit"));
        }
        atomic_private(&self.directory.join("workspaces.json"), &bytes)
    }
    pub fn open(
        &self,
        root: TargetPath,
        target: TargetIdentity,
    ) -> Result<WorkspaceState, CoreError> {
        let _lock = self.guard()?;
        let raw = crate::files::path(&root)?;
        let verified = std::fs::canonicalize(&raw);
        let mut c = self.read()?;
        let existing = c
            .records
            .iter()
            .position(|r| r.locator.root == root && r.target == target);
        let canonical = match verified {
            Ok(p) if p.is_dir() => Some(p),
            _ => None,
        };
        if canonical.is_none() {
            return existing
                .map(|i| WorkspaceState::Missing(c.records[i].clone()))
                .ok_or_else(|| {
                    CoreError::new(ErrorCode::Io, "workspace folder is missing or inaccessible")
                });
        }
        let root = crate::files::target_path(canonical.as_ref().unwrap());
        let existing = existing.or_else(|| {
            c.records
                .iter()
                .position(|r| r.locator.root == root && r.target == target)
        });
        let record = if let Some(i) = existing {
            let mut r = c.records.remove(i);
            r.locator.root = root;
            r
        } else {
            WorkspaceRecord {
                id: WorkspaceID(new_id()?),
                generation: 0,
                lease: WorkspaceLeaseID(new_id()?),
                display_name: raw
                    .file_name()
                    .unwrap_or(raw.as_os_str())
                    .to_string_lossy()
                    .into_owned(),
                locator: WorkspaceLocator {
                    environment: EnvironmentLocator {
                        route: RouteKind::Local,
                        ssh_profile: None,
                        engine: None,
                        engine_context: None,
                        container_selector: None,
                        effective_user: None,
                    },
                    root,
                },
                target,
                documents: vec![],
                selected: None,
                show_hidden: true,
                show_icons: true,
                sidebar_visible: true,
                terminal_visible: false,
                terminal_height: None,
                terminal_layout: Default::default(),
                terminal_starts: vec![],
                graph_filters: vec![],
                git_recoveries: vec![],
                git_recovery_generation: 0,
                file_recoveries: vec![],
            }
        };
        c.records.insert(0, record.clone());
        self.write(&c)?;
        Ok(WorkspaceState::Ready(record))
    }
    /// Catalog-only insertion. Root resolution and lease registration belong to the selected backend.
    pub fn register_resolved(&self, record: WorkspaceRecord) -> Result<WorkspaceRecord, CoreError> {
        let _lock = self.guard()?;
        let mut catalog = self.read()?;
        if let Some(existing) = catalog.records.iter().find(|r| {
            r.id == record.id || (r.target == record.target && r.locator == record.locator)
        }) {
            if existing.id != record.id
                || existing.lease != record.lease
                || existing.target != record.target
                || existing.locator != record.locator
            {
                return Err(CoreError::new(
                    ErrorCode::TargetChanged,
                    "saved workspace identity differs; register its existing owner explicitly",
                ));
            }
            return Ok(existing.clone());
        }
        catalog.records.insert(0, record.clone());
        self.write(&catalog)?;
        Ok(record)
    }
    pub fn update(&self, mut record: WorkspaceRecord) -> Result<(), CoreError> {
        let _lock = self.guard()?;
        let mut c = self.read()?;
        let old = c
            .records
            .iter_mut()
            .find(|r| r.id == record.id)
            .ok_or_else(|| io_error("workspace no longer saved"))?;
        if old.target != record.target || old.locator != record.locator {
            return Err(CoreError::new(
                ErrorCode::TargetChanged,
                "workspace identity changed",
            ));
        }
        if old.generation < record.generation {
            // Navigation/selection writes cannot replace worker-persisted operation references.
            record.git_recoveries = old.git_recoveries.clone();
            record.file_recoveries = old.file_recoveries.clone();
            record.git_recovery_generation = old.git_recovery_generation;
            *old = record;
        }
        self.write(&c)
    }
    pub fn remove(&self, id: WorkspaceID) -> Result<(), CoreError> {
        let _lock = self.guard()?;
        let mut c = self.read()?;
        if c.records
            .iter()
            .any(|r| r.id == id && (!r.git_recoveries.is_empty() || !r.file_recoveries.is_empty()))
        {
            return Err(CoreError::new(
                ErrorCode::Busy,
                "inspect retained Git outcomes before removing the saved workspace",
            ));
        }
        c.records.retain(|r| r.id != id);
        self.write(&c)
    }
    /// Only this locked path adds references; insertion durably precedes target admission.
    pub fn retain_git_recovery(
        &self,
        workspace: WorkspaceID,
        reference: GitRecoveryReference,
    ) -> Result<(WorkspaceRecord, bool), CoreError> {
        let _lock = self.guard()?;
        let mut catalog = self.read()?;
        if let Some(record) = catalog.records.iter().find(|r| {
            r.git_recoveries
                .iter()
                .any(|old| old.operation == reference.operation)
        }) {
            let old = record
                .git_recoveries
                .iter()
                .find(|r| r.operation == reference.operation)
                .unwrap();
            if old.worktree != reference.worktree
                || old.kind != reference.kind
                || old.request_digest != reference.request_digest
            {
                return Err(CoreError::new(
                    ErrorCode::InvalidArgument,
                    "original Git recovery identity/digest differs",
                ));
            }
            return Ok((record.clone(), false));
        }
        if let Some(old) = catalog
            .records
            .iter()
            .flat_map(|r| &r.git_recoveries)
            .find(|old| {
                old.worktree.target == reference.worktree.target
                    && old.worktree.common_git_directory_identity
                        == reference.worktree.common_git_directory_identity
            })
        {
            return Err(CoreError {code:ErrorCode::Busy,message:"Inspect the retained original Git outcome before another mutation in this repository".into(),context:ErrorContext::Operation(old.operation)});
        }
        let client = catalog.client;
        let record = catalog
            .records
            .iter_mut()
            .find(|r| r.id == workspace)
            .ok_or_else(|| {
                CoreError::new(
                    ErrorCode::Closed,
                    "Git recovery workspace is no longer saved",
                )
            })?;
        if record.target != reference.worktree.target
            || reference.owner.workspace != workspace
            || reference.owner.lease != record.lease
            || reference.owner.client != client
        {
            return Err(CoreError::new(
                ErrorCode::TargetChanged,
                "Git recovery owner/target differs",
            ));
        }
        record.git_recovery_generation = record
            .git_recovery_generation
            .checked_add(1)
            .ok_or_else(|| io_error("Git recovery generation exhausted"))?;
        record.git_recoveries.push(reference);
        let record = record.clone();
        self.write(&catalog)?;
        Ok((record, true))
    }
    /// Called only after an authoritative terminal receipt or proven current unsent cancellation.
    pub fn resolve_git_recovery(
        &self,
        workspace: WorkspaceID,
        reference: &GitRecoveryReference,
    ) -> Result<WorkspaceRecord, CoreError> {
        let _lock = self.guard()?;
        let mut catalog = self.read()?;
        let record = catalog
            .records
            .iter_mut()
            .find(|r| r.id == workspace)
            .ok_or_else(|| {
                CoreError::new(
                    ErrorCode::Closed,
                    "original Git recovery workspace is no longer saved",
                )
            })?;
        if let Some(old) = record
            .git_recoveries
            .iter()
            .find(|old| old.operation == reference.operation)
        {
            if old != reference {
                return Err(CoreError::new(
                    ErrorCode::TargetChanged,
                    "Git recovery reference changed before resolution",
                ));
            }
            record
                .git_recoveries
                .retain(|old| old.operation != reference.operation);
            record.git_recovery_generation = record
                .git_recovery_generation
                .checked_add(1)
                .ok_or_else(|| io_error("Git recovery generation exhausted"))?;
        }
        let record = record.clone();
        self.write(&catalog)?;
        Ok(record)
    }
    /// Only this locked path adds references; insertion durably precedes target admission.
    pub fn retain_file_recovery(
        &self,
        workspace: WorkspaceID,
        reference: FileRecoveryReference,
    ) -> Result<(WorkspaceRecord, bool), CoreError> {
        let _lock = self.guard()?;
        let mut catalog = self.read()?;
        if let Some(record) = catalog.records.iter().find(|r| {
            r.file_recoveries
                .iter()
                .any(|old| old.operation == reference.operation)
        }) {
            let old = record
                .file_recoveries
                .iter()
                .find(|r| r.operation == reference.operation)
                .unwrap();
            if old.file != reference.file
                || old.owner != reference.owner
                || old.destination != reference.destination
                || old.kind != reference.kind
                || old.request_digest != reference.request_digest
            {
                return Err(CoreError::new(
                    ErrorCode::InvalidArgument,
                    "original File recovery identity/digest differs",
                ));
            }
            return Ok((record.clone(), false));
        }
        if let Some(old) = catalog
            .records
            .iter()
            .flat_map(|r| &r.file_recoveries)
            .find(|old| old.file.target == reference.file.target)
        {
            return Err(CoreError {code:ErrorCode::Busy,message:"Inspect the retained original file outcome before another mutation in this repository".into(),context:ErrorContext::Operation(old.operation)});
        }
        let client = catalog.client;
        let record = catalog
            .records
            .iter_mut()
            .find(|r| r.id == workspace)
            .ok_or_else(|| {
                CoreError::new(
                    ErrorCode::Closed,
                    "File recovery workspace is no longer saved",
                )
            })?;
        if record.target != reference.file.target
            || reference.owner.workspace != workspace
            || reference.owner.lease != record.lease
            || reference.owner.client != client
        {
            return Err(CoreError::new(
                ErrorCode::TargetChanged,
                "File recovery owner/target differs",
            ));
        }
        record.file_recoveries.push(reference);
        let record = record.clone();
        self.write(&catalog)?;
        Ok((record, true))
    }
    /// Called only after an authoritative terminal receipt or proven current unsent cancellation.
    pub fn resolve_file_recovery(
        &self,
        workspace: WorkspaceID,
        reference: &FileRecoveryReference,
    ) -> Result<WorkspaceRecord, CoreError> {
        let _lock = self.guard()?;
        let mut catalog = self.read()?;
        let record = catalog
            .records
            .iter_mut()
            .find(|r| r.id == workspace)
            .ok_or_else(|| {
                CoreError::new(
                    ErrorCode::Closed,
                    "original File recovery workspace is no longer saved",
                )
            })?;
        if let Some(old) = record
            .file_recoveries
            .iter()
            .find(|old| old.operation == reference.operation)
        {
            if old != reference {
                return Err(CoreError::new(
                    ErrorCode::TargetChanged,
                    "File recovery reference changed before resolution",
                ));
            }
            record
                .file_recoveries
                .retain(|old| old.operation != reference.operation);
        }
        let record = record.clone();
        self.write(&catalog)?;
        Ok(record)
    }
    pub fn relocate(
        &self,
        id: WorkspaceID,
        root: TargetPath,
        target: &TargetIdentity,
    ) -> Result<WorkspaceRecord, CoreError> {
        let _lock = self.guard()?;
        let canonical = std::fs::canonicalize(crate::files::path(&root)?).map_err(io_error)?;
        if !canonical.is_dir() {
            return Err(io_error("choose a folder"));
        }
        let mut c = self.read()?;
        let r = c
            .records
            .iter_mut()
            .find(|r| r.id == id)
            .ok_or_else(|| io_error("workspace no longer saved"))?;
        if &r.target != target {
            return Err(CoreError::new(
                ErrorCode::TargetChanged,
                "workspace target changed",
            ));
        }
        let previous = crate::files::path(&r.locator.root)?;
        for document in &mut r.documents {
            if let Ok(file) = saved_path(&document.file.path)
                && let Ok(relative) = file.strip_prefix(&previous)
            {
                document.file.path = crate::files::target_path(&canonical.join(relative));
                document.file.resolved_identity = None;
            }
        }
        // Recovery lookup uses the document ID and retains a draft's original target/path.
        r.locator.root = crate::files::target_path(&canonical);
        r.generation = r
            .generation
            .checked_add(1)
            .ok_or_else(|| io_error("workspace generation exhausted"))?;
        let record = r.clone();
        self.write(&c)?;
        Ok(record)
    }
}
// Resolve existing ancestors without assigning identity to a missing saved suffix.
fn saved_path(value: &TargetPath) -> Result<PathBuf, CoreError> {
    let mut candidate = crate::files::path(value)?;
    let mut suffix = vec![];
    loop {
        match std::fs::canonicalize(&candidate) {
            Ok(mut verified) => {
                for part in suffix.into_iter().rev() {
                    verified.push(part);
                }
                return Ok(verified);
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                let name = candidate
                    .file_name()
                    .ok_or_else(|| io_error("saved path has no existing ancestor"))?
                    .to_os_string();
                suffix.push(name);
                if !candidate.pop() {
                    return Err(io_error("saved path has no parent"));
                }
            }
            Err(e) => return Err(io_error(e)),
        }
    }
}
pub fn is_within(root: &TargetPath, file: &TargetPath) -> bool {
    match (root, file) {
        (TargetPath::PosixBytes(r), TargetPath::PosixBytes(f)) => {
            Path::new(std::ffi::OsStr::from_bytes(f))
                .starts_with(Path::new(std::ffi::OsStr::from_bytes(r)))
        }
        _ => false,
    }
}

pub fn resolved_parent(path: &TargetPath) -> Result<PathBuf, CoreError> {
    let p = crate::files::path(path)?;
    let parent = p.parent().ok_or_else(|| io_error("missing parent"))?;
    Ok(std::fs::canonicalize(parent)
        .map_err(io_error)?
        .join(p.file_name().ok_or_else(|| io_error("missing filename"))?))
}
pub fn validate_child(root: &TargetPath, file: &TargetPath) -> Result<(), CoreError> {
    let root = std::fs::canonicalize(crate::files::path(root)?).map_err(io_error)?;
    if !resolved_parent(file)?.starts_with(root) {
        return Err(CoreError::new(
            ErrorCode::InvalidArgument,
            "file is outside workspace root",
        ));
    }
    Ok(())
}
pub fn same_path(a: &TargetPath, b: &TargetPath) -> bool {
    a == b
        || resolved_parent(a)
            .ok()
            .zip(resolved_parent(b).ok())
            .is_some_and(|(a, b)| a == b)
}

struct StoreLock<'a>(std::sync::MutexGuard<'a, File>);
impl Drop for StoreLock<'_> {
    fn drop(&mut self) {
        let _ = self.0.unlock();
    }
}

pub fn relative_child(root: &TargetPath, file: &TargetPath) -> Option<PathBuf> {
    let r = resolved_parent(root).ok()?;
    let f = resolved_parent(file).ok()?;
    f.strip_prefix(r).ok().map(Path::to_path_buf)
}
pub fn rebased(destination: &TargetPath, relative: &Path) -> Result<TargetPath, CoreError> {
    if relative.as_os_str().is_empty() {
        Ok(destination.clone())
    } else {
        Ok(crate::files::target_path(
            &crate::files::path(destination)?.join(relative),
        ))
    }
}

fn valid_terminal_starts(record: &WorkspaceRecord) -> bool {
    let sessions = record.terminal_layout.sessions();
    let mut seen = std::collections::HashSet::new();
    record.terminal_starts.len() == sessions.len()
        && record.terminal_starts.len() <= 64
        && record.terminal_starts.iter().all(|start| {
            seen.insert(start.session)
                && sessions.contains(&start.session)
                && [&start.directory, &start.shell]
                    .iter()
                    .all(|path| match path {
                        TargetPath::PosixBytes(bytes) => {
                            bytes.len() <= 65536
                                && bytes.first() == Some(&b'/')
                                && !bytes.contains(&0)
                        }
                        _ => false,
                    })
        })
}
fn valid_git_recoveries(catalog: &WorkspaceCatalog) -> bool {
    let mut seen = std::collections::HashSet::new();
    catalog.records.iter().all(|record|record.git_recoveries.iter().all(|reference| {
        reference.worktree.target==record.target && reference.owner.workspace==record.id
            && reference.owner.lease==record.lease && reference.owner.client==catalog.client
            && !reference.worktree.common_git_directory_identity.is_empty()
            && reference.worktree.common_git_directory_identity.len()<=65536
            && matches!(&reference.worktree.canonical_path,TargetPath::PosixBytes(bytes) if bytes.len()<=65536 && bytes.first()==Some(&b'/') && !bytes.contains(&0))
            && seen.insert(reference.operation)
    }))
}

fn valid_file_recoveries(catalog: &WorkspaceCatalog) -> bool {
    let mut seen = std::collections::HashSet::new();
    let path_valid = |path: &TargetPath| {
        matches!(path, TargetPath::PosixBytes(bytes)
        if bytes.len() <= 65536 && bytes.first() == Some(&b'/') && !bytes.contains(&0))
    };
    catalog.records.iter().all(|record| {
        record.file_recoveries.len() <= 64
            && record.file_recoveries.iter().all(|reference| {
                reference.file.target == record.target
                    && reference.owner.workspace == record.id
                    && reference.owner.lease == record.lease
                    && reference.owner.client == catalog.client
                    && path_valid(&reference.file.path)
                    && reference.destination.as_ref().is_none_or(path_valid)
                    && seen.insert(reference.operation)
            })
    })
}