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
//! Shared lock utilities for single-instance enforcement.
//!
//! [`try_flock`] is a non-blocking exclusive lock on the WHOLE file, over std's [`File::try_lock`]:
//! `flock(LOCK_EX|LOCK_NB)` on Unix, `LockFileEx` over the whole addressable range from offset 0
//! on Windows. Std owning both the byte range and the platform's contention mapping is deliberate
//! — a whole-file Windows range cannot overlap whatever another handle holds.
//!
//! [`lock_file_path`] gives the standard `mahbot.lock` path. The probes at the bottom answer "does
//! an instance hold this storage location?" for the read-only CLI paths: they report a tri-state,
//! because "the probe could not tell" must never be read as "no instance holds the location".
//!
//! # Release
//!
//! The lock is released by an explicit [`File::unlock`], or at the latest when the last handle to
//! the file is closed: on `Drop`, on `process::exit` (destructors do not run, but the O.S. closes
//! all handles), and on abrupt termination. There is no stale-lock scenario. The explicit unlock
//! is what [`crate::self_update`] uses before spawning the replacement instance: Windows documents
//! the O.S. unlock on close as taking an unspecified amount of time.
use ;
use io;
use ;
/// Path to the standard `mahbot.lock` under the given storage root.
pub
/// Try to acquire an exclusive whole-file lock non-blockingly: `Ok(true)` acquired, `Ok(false)`
/// already held, `Err` a non-retryable OS error.
pub
/// What the instance lock says about a storage location.
pub
/// Refusal wording for the read-only CLI paths (`mahbot debug`, `bench-openrouter`) when the probe
/// reported [`Unknown`](InstanceLockState::Unknown): whether an instance holds the storage location
/// could not be established, so the probe refuses. Returned as a clause without terminal
/// punctuation — each consumer appends its own continuation.
pub
/// Probe the instance lock once for the given storage location: open it read+write and
/// [`try_flock`] it once.
///
/// A `NotFound` open error is [`InstanceLockState::Free`] (the lock file is created before the
/// stores are opened), any other open error [`Unknown`]; `Ok(true)` is `Free` — the probe releases
/// what it took — `Ok(false)` is `Held`, and `Err` is `Unknown`.
///
/// A held lock means an instance is running: the caller must NOT open the live stores directly and
/// must route read-only queries through the debug IPC endpoint. [`InstanceLockState::Unknown`] is
/// deliberately never read as "free".
/// Re-check count/interval for [`instance_lock_state_settled`].
const LOCK_SETTLE_RECHECKS: usize = 3;
const LOCK_SETTLE_INTERVAL: Duration = from_millis;
/// Conservative instance-up probe for read-only CLI fallbacks (`mahbot debug`, `bench-openrouter`):
/// re-probes the instance lock over a short window.
///
/// [`Held`](InstanceLockState::Held) wins outright and is returned as soon as it is seen, which
/// keeps the caller from direct-opening a live store during the self-update handoff.
///
/// [`Free`](InstanceLockState::Free) is returned only after the window consistently shows the lock
/// free. An [`Unknown`](InstanceLockState::Unknown) seen in the window is remembered and returned
/// if no `Held` was ever seen: a probe that could not tell must never be downgraded to `Free`. The
/// extra ~300 ms is a CLI diagnostic tool, not a hot path.
pub