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
// SPDX-License-Identifier: AGPL-3.0-only
//! Health / liveness.
//!
//! Mode-aware: one-shot uses the exit code; the long-lived daemon modes
//! (loop / reactive / schedule) expose **supervisor-heartbeat liveness**.
//! The key rule: *a live PID is not a live agent* — liveness
//! tracks whether the **supervisor reactor loop** is making progress, not
//! whether the process exists. **Idle is healthy** (the reactor wakes on every
//! `recv_timeout` expiry and [`tick`]s, so a daemon idling for hours stays
//! live), and a **stuck *subagent* must NOT fail the daemon's liveness**: the
//! reactor is the thing that *detects and kills* a wedged child (the
//! three-detector model in `supervisor::liveness` plus the kill ladder), so
//! while it does so it is by definition still ticking — failing pod liveness
//! there would destroy a whole healthy tree for one wedged leaf.
//!
//! Concretely: every reactor hot loop (the daemon driver, the per-run reactor,
//! the interval sleep) calls [`tick`], bumping a process-global timestamp. The
//! per-child stuck-detector (`supervisor::liveness`) is a **separate** clock
//! that never touches this one — it drives the kill ladder, not pod liveness.
//! A background writer thread renders `{alive, supervisor_tick_age_ms, …}` to
//! the `--health-file` once a second; a K8s `exec` probe reads it. If the
//! reactor itself wedges, ticks stop, the age grows past the threshold, and
//! `alive` flips to false — even though the writer keeps writing — so k8s
//! restarts the pod.
//!
//! Default surface = exit code + `--health-file`. The opt-in `/healthz`+`/readyz`
//! HTTP surface (feature `metrics`, `obs::serve`) reuses this same heartbeat
//! liveness over the hand-rolled HTTP server.
use craterfc3339_millis;
use json;
use ;
use ;
use JoinHandle;
use ;
/// The liveness staleness window (ms): a supervisor tick older than this reads
/// unhealthy on `/healthz` and in the `--health-file` (`alive:false`), so k8s
/// SIGKILLs the pod. The single source of truth for the window —
/// `obs::serve`'s `/healthz` and the `--health-file` writer both read it, and the
/// reactor-thread MANAGEMENT timeout below is sized strictly under it.
pub const LIVENESS_STALE_AFTER_MS: u64 = 5_000;
/// The SHORT per-request timeout (ms) for reactor-thread MANAGEMENT calls — the
/// hot-reload re-handshake (`list_tools`), the claim lease calls (renew / ack /
/// release / the coordination re-validation), the claim release a drain performs,
/// and the reactor's notify-then-read (`read_resource`). It is deliberately FAR
/// under the liveness window ([`LIVENESS_STALE_AFTER_MS`]): a slow-but-alive
/// coordination / resource server blocks the single reactor thread for at most
/// this long, so a management call can never starve the heartbeat past the
/// staleness window and get a healthy daemon SIGKILLed, nor overrun the drain
/// deadline while shutdown waits on one unresponsive server. The data path
/// (subagent tool calls, `resources/read` on the agentloop) keeps the default
/// ~60s — only the reactor MANAGEMENT path is bounded.
pub const MANAGEMENT_TIMEOUT_MS: u64 = 2_000;
/// The reactor-thread management-call timeout as a [`Duration`].
pub const
// The management timeout MUST stay strictly under the liveness window, or a single
// management call could itself age the heartbeat past the staleness threshold —
// the exact starvation the short bound exists to prevent. Pin the
// invariant at compile time so a later edit to either constant can't silently
// break it (a generous 2x margin leaves room for a couple of back-to-back calls
// between ticks; the per-block `health::tick()` insurance covers longer runs).
const _: = assert!;
/// Last time a supervisor loop proved progress (ms since epoch). Bumped from
/// every hot loop; read by the writer thread. Cheap (one relaxed store).
static LAST_TICK_MS: AtomicU64 = new;
/// Record supervisor progress. Call from each supervisor hot loop.
/// Age of the last tick (ms). Large ⇒ the supervisor reactor loop is wedged.
/// This is the sole input to the `/healthz` liveness verdict; no per-subagent
/// state feeds it.
/// Test-only seam: stamp the heartbeat at `ms_ago` in the past, so a test can
/// exercise the *wedged-reactor* path (`tick_age_ms() ≈ ms_ago`) deterministically
/// without sleeping. Saturates at the epoch. Not compiled into release builds.
pub
/// Test-only serialization for the process-global heartbeat. Any test that
/// *mutates* `LAST_TICK_MS` (via [`tick`] or [`set_tick_age_for_test`]) and then
/// asserts on the age must hold this lock, so a concurrent test's `tick()` can't
/// race a wedged-path assertion. Mirrors `obs::log`'s `STDERR_LOCK` pattern.
pub static HEARTBEAT_TEST_LOCK: Mutex = new;
/// Spawn the health-file writer thread for a daemon. It writes once a second
/// until the process exits or a drain is requested (after which it writes a
/// final `draining` record and stops). `stale_after` is the liveness window.
/// Write `bytes` to `path` atomically (temp + rename) so a probe never reads a
/// torn file.