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
//! `MachineResource`, `HeartbeatStatus`, `ComponentResource`,
//! `ProcessResource`, and the `Pid` newtype.
//!
//! Contents:
//!
//! - `MachineResource`: `fingerprint`, `name`, `ip`, `hostname`, `platform`,
//! `cores`, `memory`, `disk`, `metadata`, `heartbeat_status`, relationship
//! `license`. ⚠️ `memory` and `disk` are **megabytes**, not bytes — the
//! server's own column comment says so, and these are the values that
//! feed the licence's `machines_memory_count`/`machines_disk_count`
//! totals and the `MEMORY_LIMIT_EXCEEDED`/`DISK_LIMIT_EXCEEDED` checks.
//! - `HeartbeatStatus`: `NOT_STARTED` → `ALIVE` → `DEAD` → `RESURRECTED`.
//! The server's window **is** `policy.heartbeat_duration`, falling back to
//! 600s (10 min) only when that column is null; the write responses carry
//! the fallback, so read the real one with
//! [`crate::Client::effective_heartbeat_window`]. `DEAD` means **only**
//! "the last ping is older than that window" — it is not a tombstone. Under
//! the default policy (`require_heartbeat = false`) nothing is ever culled,
//! so a machine stays `DEAD` indefinitely with its row and its seat still
//! in place. A response can only fail to say `DEAD` when it derives the
//! status from a `last_heartbeat_at` that same request just wrote — ping,
//! reset, create. A verified machine file, an offline proof, `get_machine`,
//! `list_machines` and even `update_machine` (a write that never touches
//! the heartbeat column) all can. Never stop the ping loop on a status; a
//! `404` from the ping is the only terminal signal.
//! - `ComponentResource`: `machine_id`, `fingerprint`, `name`, `metadata`.
//! - `ProcessResource`: `machine_id`, `pid`, `metadata`.
//! - `Pid` newtype: the wire format is a **string, not an integer** —
//! provide `From<u32>`/`From<i32>` that stringify on serialize, so callers
//! holding a native numeric PID don't have to hand-format it. The 30s
//! process heartbeat window exists only in a worker the server never
//! runs: **no process row is ever reaped**, so a leaked process holds its
//! slot against `max_processes` until a client deletes it explicitly with
//! [`crate::Client::delete_process`].
/// The `machines` JSON:API resource: `{ id, type, attributes }`. Field set
/// matches the Tamga API's actual `MachineResource`/`MachineAttributes`
/// serializer — no `relationships` object, same as
/// [`crate::models::license::LicenseResource`].
/// Attributes of a [`MachineResource`], matching the Tamga API's
/// `MachineAttributes` field-for-field.
/// Machine heartbeat state machine: `NotStarted` → `Alive` → `Dead` →
/// `Resurrected`.
///
/// ⚠️ **The window is policy-driven.** Server-side it is
/// `policy.heartbeat_duration`, with 600s (10 min) used only as the fallback
/// when that column is null: `effective_window_secs` prefers the policy
/// value, and the cull job's claim query selects on
/// `COALESCE(p.heartbeat_duration, 600)`. Read it with
/// [`crate::Client::effective_heartbeat_window`], which fetches the licence's
/// policy — a single call at startup, not once per tick.
///
/// It also cannot be recovered from a **write** response. The create,
/// ping-heartbeat and reset-heartbeat queries omit the policy join, so
/// `next_heartbeat_at` on those is `last_heartbeat_at + 600s` whatever the
/// policy says — never derive an interval from a ping.
///
/// The **read** paths do carry it. [`crate::Client::check_out_machine`] and
/// [`crate::Client::generate_offline_proof`] resolve the machine through a
/// policy-joined query, so on a verified machine file (see
/// [`crate::checkout::machine_file::verify_machine_file`]) or a proof
/// response, `next_heartbeat_at - last_heartbeat_at` is the real effective
/// window — the one place this crate can observe it, and only when
/// `last_heartbeat_at` is set. Otherwise learn it out of band, from whoever
/// provisions the policy, and set the interval explicitly. **On a policy with
/// a shorter window, a caller pinging on the 600s assumption pings far too
/// slowly and its machines go `Dead` on schedule.**
///
/// ⚠️ **`Dead` is a staleness report, not a tombstone.** The server computes
/// it purely from `last_heartbeat_at` versus the window and never consults
/// `policy.require_heartbeat` on the way, but the cull job that would
/// actually remove the row bails out unless `require_heartbeat` is set — and
/// that column defaults to `false`. On a default policy nothing is ever
/// culled, so a machine stays `Dead` **forever** while its row, and the seat
/// it holds against the licence, are still there.
///
/// ⚠️ **A ping, reset, create or validate response can never say `Dead`.**
/// Those four are `Dead`-free by construction: `ping-heartbeat` writes
/// `last_heartbeat_at = NOW()` and *then* derives the status from that same
/// timestamp, so its age is ~0 and the answer is always `Alive` or
/// `Resurrected`; `reset-heartbeat` nulls the column and answers
/// `NotStarted`; `POST /machines` never sets it and answers `NotStarted`. The
/// licence `validate` path never constructs
/// [`crate::models::validation::ValidationCode::HeartbeatDead`] either.
///
/// ⚠️ **The rule is not "writes cannot say `Dead`" — `PATCH /machines/{id}`
/// is a write that can.** The durable form is narrower: a response cannot say
/// `Dead` when the server derived the status from a `last_heartbeat_at`
/// *this request just wrote*. `update_machine` writes `name`, `ip`,
/// `hostname`, `platform`, `cores`, `memory`, `disk` and `metadata` and never
/// goes near the heartbeat column, so the timestamp it judges is as old as it
/// was before the call and the verdict is genuine. Stating it as write-vs-read
/// makes `update_machine` look safe to skip, which is how a `Dead` machine
/// stops being noticed on the one route an application calls routinely.
///
/// Checkout is one of the exceptions, and it is a genuine staleness verdict:
/// the server resolves the machine through a read query nobody has just
/// written to, then serializes it into the file.
/// [`crate::checkout::machine_file::verify_machine_file`] returns a
/// [`MachineResource`], so the `heartbeat_status` inside a verified
/// `.machine` file **can be `Dead`** — as can the one on the
/// [`crate::Client::generate_offline_proof`] response, which resolves the
/// same way. [`crate::Client::get_machine`] and
/// [`crate::Client::list_machines`] carry it too, and for the same reason:
/// both resolve through a policy-joined read nobody has just written to. A
/// `Dead` branch against any of those four is live code; against a ping,
/// reset or create response it is unreachable.
///
/// The scheduling rule does not depend on any of that: **never stop the ping
/// loop on a status**, whichever one comes back.
/// [`crate::Client::ping_heartbeat`] is a bare `last_heartbeat_at = NOW()`
/// write with no resurrection check, so it revives a machine that had gone
/// stale — which is why a `Dead` machine is worth pinging even though the
/// ping itself will never label it that way. The only terminal signal from a
/// ping is a `404` ([`crate::TamgaError::NotFound`]): the row is gone. Hang
/// re-activation off that, never off a status.
/// The `components` JSON:API resource: `{ id, type, attributes }`. Field
/// set matches the Tamga API's actual `ComponentResource`/`ComponentAttributes`
/// serializer — no `relationships` object, same pattern as the other
/// resources in this crate.
/// Attributes of a [`ComponentResource`].
/// The `processes` JSON:API resource: `{ id, type, attributes }`. Field set
/// matches the Tamga API's actual `ProcessResource`/`ProcessAttributes`
/// serializer.
/// Attributes of a [`ProcessResource`]. Unlike a [`MachineResource`], there
/// is no `heartbeat_status` field, and there is no server-side state machine
/// behind one either: `last_heartbeat_at` is written on create and on every
/// ping, serialized back out, and **never read by any live code path**. No
/// equivalent of `DEAD`/`RESURRECTED` is ever computed for a process, and no
/// process row is ever deleted by the server — see [`Pid`]'s doc comment.
/// Process ID newtype. The wire format is a JSON **string**, not a number —
/// this type exists so callers holding a native numeric PID don't have to
/// hand-format it; `From<u32>`/`From<i32>` stringify on construction.
///
/// ⚠️ **The server does not currently reap process rows.** A 30-second
/// window and a delete-on-expiry sweep are both written — much shorter than
/// a machine's 600s fallback, with no resurrection grace period and no
/// `KEEP_DEAD` equivalent — but the worker holding them has no call site and
/// the job scheduler wires no process tick, so as shipped nothing runs it:
/// no process is ever marked dead, no `process.heartbeat.dead` event is ever
/// emitted, and no row is ever removed. `last_heartbeat_at` is written and
/// echoed back, never acted on.
///
/// The practical consequence is a leak, not an eviction: a process registered
/// by [`crate::Client::create_process`] increments the licence's
/// `machines_process_count` and holds that slot against the policy's
/// `max_processes` **forever**, however long ago it stopped pinging. Only an
/// explicit delete releases it: [`crate::Client::delete_process`], or
/// [`crate::Client::delete_machine_processes`] for a machine's whole set.
/// Call one of them on shutdown — nothing else will. Keeping a PID stable
/// across restarts also bounds the damage: re-registering the same one is
/// refused with [`crate::TamgaError::PidTaken`] rather than creating a second
/// row, so the original row and its one slot are what stay in use.
///
/// Keep [`crate::Client::ping_process`] on a ~10s timer regardless. The
/// reaper is written and needs only a scheduler entry to go live, so a client
/// that stops pinging is relying on a bug staying unfixed.
String);