@in-the-loop-labs/pair-review 5.4.0 → 5.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ // Copyright 2026 Tim Perkins (tjwp) | SPDX-License-Identifier: Apache-2.0
2
+ 'use strict';
3
+
4
+ /**
5
+ * Process liveness helpers.
6
+ *
7
+ * Used to decide whether a pair-review process recorded as the holder of a
8
+ * shared resource (e.g. a worktree_pool row) is still running. Every
9
+ * uncertain answer errs toward "alive": wrongly treating a live holder as
10
+ * dead lets this process reclaim a worktree another process is using, which
11
+ * is far worse than leaving a dead holder's worktree parked until the next
12
+ * startup.
13
+ */
14
+
15
+ const os = require('os');
16
+
17
+ const defaults = {
18
+ kill: (pid, signal) => process.kill(pid, signal),
19
+ now: () => Date.now(),
20
+ uptime: () => os.uptime(),
21
+ };
22
+
23
+ /**
24
+ * Return whether a process with the given PID currently exists.
25
+ *
26
+ * Uses signal 0, which performs the existence/permission check without
27
+ * delivering a signal.
28
+ *
29
+ * @param {number} pid - Process ID to probe
30
+ * @param {Object} [_deps] - Dependency overrides for testing ({ kill })
31
+ * @returns {boolean} false for an invalid PID or ESRCH; true otherwise
32
+ * (success, EPERM — exists but owned by another user — and any other error,
33
+ * so an unexpected failure never causes a live holder to be treated as dead)
34
+ */
35
+ function isProcessAlive(pid, _deps = {}) {
36
+ const deps = { ...defaults, ..._deps };
37
+ if (pid == null || !Number.isInteger(pid) || pid <= 0) {
38
+ return false;
39
+ }
40
+ try {
41
+ deps.kill(pid, 0);
42
+ return true;
43
+ } catch (err) {
44
+ if (err && err.code === 'ESRCH') return false;
45
+ // EPERM: the process exists but we may not signal it.
46
+ // Anything else: unknown — fail safe and treat the holder as alive.
47
+ return true;
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Approximate wall-clock time (ms since epoch) at which the OS booted.
53
+ * A holder claim stamped before this moment cannot belong to a process that
54
+ * is running now, regardless of what PID it recorded.
55
+ *
56
+ * @param {Object} [_deps] - Dependency overrides for testing ({ now, uptime })
57
+ * @returns {number}
58
+ */
59
+ function getBootTimeMs(_deps = {}) {
60
+ const deps = { ...defaults, ..._deps };
61
+ return deps.now() - deps.uptime() * 1000;
62
+ }
63
+
64
+ module.exports = { isProcessAlive, getBootTimeMs };