harnery 0.45.0 → 0.45.2

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.
Files changed (235) hide show
  1. package/dist/commander.js +0 -1
  2. package/dist/commands/files.d.ts +2 -2
  3. package/dist/commands/files.d.ts.map +1 -1
  4. package/dist/commands/files.js +4 -7
  5. package/dist/commands/servers.d.ts.map +1 -1
  6. package/dist/commands/servers.js +17 -11
  7. package/dist/commands/tunnel.d.ts +0 -15
  8. package/dist/commands/tunnel.d.ts.map +1 -1
  9. package/dist/commands/tunnel.js +153 -453
  10. package/dist/commands/workflow.d.ts.map +1 -1
  11. package/dist/commands/workflow.js +6 -45
  12. package/dist/core/agents/coord-client.d.ts.map +1 -1
  13. package/dist/core/agents/coord-client.js +0 -18
  14. package/dist/core/agents/live-authority-v3.d.ts +0 -1
  15. package/dist/core/agents/live-authority-v3.d.ts.map +1 -1
  16. package/dist/core/agents/live-authority-v3.js +0 -11
  17. package/dist/core/agents/live-lifecycle-v3.d.ts +2 -4
  18. package/dist/core/agents/live-lifecycle-v3.d.ts.map +1 -1
  19. package/dist/core/agents/live-lifecycle-v3.js +7 -28
  20. package/dist/core/agents/state/heartbeat-writer.d.ts.map +1 -1
  21. package/dist/core/agents/state/heartbeat-writer.js +14 -25
  22. package/dist/core/agents/state/live-coordination-writer.d.ts.map +1 -1
  23. package/dist/core/agents/state/live-coordination-writer.js +0 -5
  24. package/dist/core/agents/state/pidmap.d.ts.map +1 -1
  25. package/dist/core/agents/state/pidmap.js +2 -7
  26. package/dist/core/agents/state/proc-start.d.ts +20 -31
  27. package/dist/core/agents/state/proc-start.d.ts.map +1 -1
  28. package/dist/core/agents/state/proc-start.js +37 -70
  29. package/dist/core/agents/state/service-process.d.ts +61 -0
  30. package/dist/core/agents/state/service-process.d.ts.map +1 -0
  31. package/dist/core/agents/state/service-process.js +143 -0
  32. package/dist/core/agents/state/stale-sweep.d.ts.map +1 -1
  33. package/dist/core/agents/state/stale-sweep.js +70 -180
  34. package/dist/core/artifacts/activity.d.ts +4 -1
  35. package/dist/core/artifacts/activity.d.ts.map +1 -1
  36. package/dist/core/artifacts/activity.js +8 -2
  37. package/dist/core/artifacts/delivery-card.d.ts.map +1 -1
  38. package/dist/core/artifacts/delivery-card.js +1 -2
  39. package/dist/core/artifacts/index.d.ts.map +1 -1
  40. package/dist/core/artifacts/index.js +38 -12
  41. package/dist/core/context/index.d.ts +6 -4
  42. package/dist/core/context/index.d.ts.map +1 -1
  43. package/dist/core/context/index.js +194 -256
  44. package/dist/core/context/observation.d.ts +9 -9
  45. package/dist/core/context/observation.d.ts.map +1 -1
  46. package/dist/core/context/observation.js +37 -6
  47. package/dist/core/events/v3/authority-outbox.d.ts.map +1 -1
  48. package/dist/core/events/v3/authority-outbox.js +0 -29
  49. package/dist/core/events/v3/producers/coordination-recorder.d.ts +0 -1
  50. package/dist/core/events/v3/producers/coordination-recorder.d.ts.map +1 -1
  51. package/dist/core/events/v3/producers/coordination-recorder.js +0 -7
  52. package/dist/core/events/v3/producers/recorder.d.ts.map +1 -1
  53. package/dist/core/events/v3/producers/recorder.js +64 -77
  54. package/dist/core/events/v3/runtime-build.d.ts +14 -0
  55. package/dist/core/events/v3/runtime-build.d.ts.map +1 -1
  56. package/dist/core/events/v3/runtime-build.js +44 -0
  57. package/dist/core/governor/index.d.ts +2 -1
  58. package/dist/core/governor/index.d.ts.map +1 -1
  59. package/dist/core/governor/index.js +2 -1
  60. package/dist/core/governor/service-read.d.ts +3 -10
  61. package/dist/core/governor/service-read.d.ts.map +1 -1
  62. package/dist/core/governor/service-read.js +10 -94
  63. package/dist/core/governor/service.d.ts +3 -7
  64. package/dist/core/governor/service.d.ts.map +1 -1
  65. package/dist/core/governor/service.js +17 -182
  66. package/dist/core/hooks/cli.js +78 -48
  67. package/dist/core/hooks/resolve/owner.js +2 -2
  68. package/dist/core/resources/sampler.js +2 -2
  69. package/dist/core/resources/service-status.d.ts +2 -14
  70. package/dist/core/resources/service-status.d.ts.map +1 -1
  71. package/dist/core/resources/service-status.js +3 -23
  72. package/dist/core/resources/service.d.ts +3 -7
  73. package/dist/core/resources/service.d.ts.map +1 -1
  74. package/dist/core/resources/service.js +12 -101
  75. package/dist/core/resources/status.d.ts.map +1 -1
  76. package/dist/core/resources/status.js +3 -2
  77. package/dist/core/semantic/service.d.ts.map +1 -1
  78. package/dist/core/semantic/service.js +9 -2
  79. package/dist/core/servers/index.d.ts +11 -10
  80. package/dist/core/servers/index.d.ts.map +1 -1
  81. package/dist/core/servers/index.js +82 -192
  82. package/dist/core/servers/tunnels.d.ts +3 -5
  83. package/dist/core/servers/tunnels.d.ts.map +1 -1
  84. package/dist/core/servers/tunnels.js +2 -45
  85. package/dist/core/storage/durable-history.js +56 -22
  86. package/dist/core/storage/segments.d.ts.map +1 -1
  87. package/dist/core/storage/segments.js +73 -17
  88. package/dist/core/supervisor/service.d.ts +4 -7
  89. package/dist/core/supervisor/service.d.ts.map +1 -1
  90. package/dist/core/supervisor/service.js +23 -122
  91. package/dist/core/supervisor/services.js +2 -2
  92. package/dist/core/supervisor/status.d.ts +3 -17
  93. package/dist/core/supervisor/status.d.ts.map +1 -1
  94. package/dist/core/supervisor/status.js +7 -70
  95. package/dist/core/work/state.js +7 -7
  96. package/dist/core/workflow/engine.d.ts.map +1 -1
  97. package/dist/core/workflow/engine.js +23 -66
  98. package/dist/core/workflow/index.d.ts +3 -4
  99. package/dist/core/workflow/index.d.ts.map +1 -1
  100. package/dist/core/workflow/index.js +2 -2
  101. package/dist/core/workflow/live-session-v3.d.ts +0 -6
  102. package/dist/core/workflow/live-session-v3.d.ts.map +1 -1
  103. package/dist/core/workflow/live-session-v3.js +5 -126
  104. package/dist/core/workflow/proof.d.ts +4 -4
  105. package/dist/core/workflow/proof.d.ts.map +1 -1
  106. package/dist/core/workflow/proof.js +34 -86
  107. package/dist/core/workflow/resume-lock.d.ts +17 -0
  108. package/dist/core/workflow/resume-lock.d.ts.map +1 -0
  109. package/dist/core/workflow/resume-lock.js +158 -0
  110. package/dist/core/workflow/run-state.d.ts +0 -21
  111. package/dist/core/workflow/run-state.d.ts.map +1 -1
  112. package/dist/core/workflow/run-state.js +3 -174
  113. package/dist/core/workflow/spawn-claude.d.ts.map +1 -1
  114. package/dist/core/workflow/spawn-claude.js +0 -19
  115. package/dist/core/workflow/spawn-codex.d.ts.map +1 -1
  116. package/dist/core/workflow/spawn-codex.js +0 -13
  117. package/dist/core/workflow/spawn-cursor.d.ts.map +1 -1
  118. package/dist/core/workflow/spawn-cursor.js +0 -19
  119. package/dist/core/workflow/spawn-opencode.d.ts.map +1 -1
  120. package/dist/core/workflow/spawn-opencode.js +0 -14
  121. package/dist/core/workflow/types.d.ts +11 -54
  122. package/dist/core/workflow/types.d.ts.map +1 -1
  123. package/dist/core/workflow/workspaces/leases.d.ts +4 -0
  124. package/dist/core/workflow/workspaces/leases.d.ts.map +1 -1
  125. package/dist/core/workflow/workspaces/leases.js +1 -1
  126. package/dist/core/workflow/workspaces/local-git.js +91 -210
  127. package/dist/core/workflow/workspaces/ownership.d.ts +0 -7
  128. package/dist/core/workflow/workspaces/ownership.d.ts.map +1 -1
  129. package/dist/core/workflow/workspaces/ownership.js +3 -130
  130. package/dist/core/workflow/workspaces/reclaim.d.ts +4 -7
  131. package/dist/core/workflow/workspaces/reclaim.d.ts.map +1 -1
  132. package/dist/core/workflow/workspaces/reclaim.js +0 -4
  133. package/dist/core/workflow/workspaces/recovery.d.ts +1 -1
  134. package/dist/core/workflow/workspaces/recovery.d.ts.map +1 -1
  135. package/dist/core/workflow/workspaces/recovery.js +1 -1
  136. package/dist/core/workflow/workspaces/state.d.ts.map +1 -1
  137. package/dist/core/workflow/workspaces/state.js +1 -4
  138. package/dist/lib/identities/assume.d.ts.map +1 -1
  139. package/dist/lib/identities/assume.js +0 -4
  140. package/dist/lib/tunnel/state.d.ts +6 -13
  141. package/dist/lib/tunnel/state.d.ts.map +1 -1
  142. package/dist/lib/tunnel/state.js +12 -58
  143. package/package.json +1 -6
  144. package/src/commander.ts +0 -10
  145. package/src/commands/files.ts +5 -13
  146. package/src/commands/servers.ts +23 -12
  147. package/src/commands/tunnel.ts +166 -533
  148. package/src/commands/workflow.ts +8 -58
  149. package/src/core/agents/coord-client.ts +0 -20
  150. package/src/core/agents/live-authority-v3.ts +0 -17
  151. package/src/core/agents/live-lifecycle-v3.ts +14 -52
  152. package/src/core/agents/state/heartbeat-writer.ts +12 -29
  153. package/src/core/agents/state/live-coordination-writer.ts +0 -29
  154. package/src/core/agents/state/pidmap.ts +2 -15
  155. package/src/core/agents/state/proc-start.ts +41 -69
  156. package/src/core/agents/state/service-process.ts +217 -0
  157. package/src/core/agents/state/stale-sweep.ts +82 -247
  158. package/src/core/artifacts/activity.ts +9 -2
  159. package/src/core/artifacts/delivery-card.ts +1 -7
  160. package/src/core/artifacts/index.ts +45 -12
  161. package/src/core/context/index.ts +207 -272
  162. package/src/core/context/observation.ts +40 -9
  163. package/src/core/events/v3/authority-outbox.ts +0 -56
  164. package/src/core/events/v3/producers/coordination-recorder.ts +0 -15
  165. package/src/core/events/v3/producers/recorder.ts +68 -94
  166. package/src/core/events/v3/runtime-build.ts +62 -0
  167. package/src/core/governor/index.ts +1 -2
  168. package/src/core/governor/service-read.ts +15 -114
  169. package/src/core/governor/service.ts +36 -220
  170. package/src/core/hooks/cli.ts +85 -47
  171. package/src/core/hooks/resolve/owner.ts +2 -2
  172. package/src/core/resources/sampler.ts +3 -2
  173. package/src/core/resources/service-status.ts +6 -35
  174. package/src/core/resources/service.ts +23 -133
  175. package/src/core/resources/status.ts +3 -2
  176. package/src/core/semantic/service.ts +8 -2
  177. package/src/core/servers/index.ts +86 -228
  178. package/src/core/servers/tunnels.ts +3 -55
  179. package/src/core/storage/durable-history.ts +52 -20
  180. package/src/core/storage/segments.ts +79 -17
  181. package/src/core/supervisor/service.ts +33 -155
  182. package/src/core/supervisor/services.ts +2 -2
  183. package/src/core/supervisor/status.ts +10 -85
  184. package/src/core/work/state.ts +7 -7
  185. package/src/core/workflow/engine.ts +22 -74
  186. package/src/core/workflow/index.ts +2 -13
  187. package/src/core/workflow/live-session-v3.ts +5 -146
  188. package/src/core/workflow/proof.ts +40 -104
  189. package/src/core/workflow/resume-lock.ts +179 -0
  190. package/src/core/workflow/run-state.ts +3 -207
  191. package/src/core/workflow/spawn-claude.ts +0 -18
  192. package/src/core/workflow/spawn-codex.ts +0 -13
  193. package/src/core/workflow/spawn-cursor.ts +0 -18
  194. package/src/core/workflow/spawn-opencode.ts +0 -14
  195. package/src/core/workflow/types.ts +11 -41
  196. package/src/core/workflow/workspaces/leases.ts +1 -1
  197. package/src/core/workflow/workspaces/local-git.ts +120 -239
  198. package/src/core/workflow/workspaces/ownership.ts +3 -158
  199. package/src/core/workflow/workspaces/reclaim.ts +3 -9
  200. package/src/core/workflow/workspaces/recovery.ts +1 -1
  201. package/src/core/workflow/workspaces/state.ts +1 -5
  202. package/src/lib/identities/assume.ts +0 -4
  203. package/src/lib/tunnel/state.ts +15 -70
  204. package/dist/commands/turn-rollback.d.ts +0 -5
  205. package/dist/commands/turn-rollback.d.ts.map +0 -1
  206. package/dist/commands/turn-rollback.js +0 -76
  207. package/dist/core/agents/state/process-handle.d.ts +0 -26
  208. package/dist/core/agents/state/process-handle.d.ts.map +0 -1
  209. package/dist/core/agents/state/process-handle.js +0 -121
  210. package/dist/core/agents/state/unchanged-file.d.ts +0 -44
  211. package/dist/core/agents/state/unchanged-file.d.ts.map +0 -1
  212. package/dist/core/agents/state/unchanged-file.js +0 -390
  213. package/dist/core/events/v3/generation-custody.d.ts +0 -5
  214. package/dist/core/events/v3/generation-custody.d.ts.map +0 -1
  215. package/dist/core/events/v3/generation-custody.js +0 -50
  216. package/dist/core/turn-rollback/content.d.ts +0 -53
  217. package/dist/core/turn-rollback/content.d.ts.map +0 -1
  218. package/dist/core/turn-rollback/content.js +0 -396
  219. package/dist/core/turn-rollback/index.d.ts +0 -72
  220. package/dist/core/turn-rollback/index.d.ts.map +0 -1
  221. package/dist/core/turn-rollback/index.js +0 -443
  222. package/dist/core/workflow/failure-evidence.d.ts +0 -16
  223. package/dist/core/workflow/failure-evidence.d.ts.map +0 -1
  224. package/dist/core/workflow/failure-evidence.js +0 -114
  225. package/dist/core/workflow/recovery-audit.d.ts +0 -30
  226. package/dist/core/workflow/recovery-audit.d.ts.map +0 -1
  227. package/dist/core/workflow/recovery-audit.js +0 -116
  228. package/src/commands/turn-rollback.ts +0 -111
  229. package/src/core/agents/state/process-handle.ts +0 -165
  230. package/src/core/agents/state/unchanged-file.ts +0 -456
  231. package/src/core/events/v3/generation-custody.ts +0 -59
  232. package/src/core/turn-rollback/content.ts +0 -499
  233. package/src/core/turn-rollback/index.ts +0 -580
  234. package/src/core/workflow/failure-evidence.ts +0 -141
  235. package/src/core/workflow/recovery-audit.ts +0 -181
@@ -33,7 +33,6 @@
33
33
  import { spawnSync } from "node:child_process";
34
34
  import { existsSync, readFileSync } from "node:fs";
35
35
  import { coordEnv } from "../../../lib/env.js";
36
- import { signalProcessWithHandle, windowsProcessStartToken } from "./process-handle.js";
37
36
  /**
38
37
  * The `ps` probe is text formatted by `strftime("%c", localtime(...))`, so its
39
38
  * output moves with `TZ` and `LC_TIME`. A hook running under `LC_ALL=C` (git
@@ -55,8 +54,6 @@ export function startTokenProbe() {
55
54
  const override = coordEnv("PID_PROBE");
56
55
  if (override === "procfs" || override === "ps")
57
56
  return override;
58
- if (process.platform === "win32")
59
- return "windows";
60
57
  if (cachedProbe === undefined) {
61
58
  cachedProbe = existsSync("/proc/self/stat") ? "procfs" : "ps";
62
59
  }
@@ -98,7 +95,7 @@ export function resetStartTokenCaches() {
98
95
  * than by splitting the whole line, because comm is the executable name in
99
96
  * parentheses and may itself contain spaces and parens. The boot segment is
100
97
  * dropped rather than faked when the kernel will not name its boot, which
101
- * yields the pre-boot-id shape that `tokensAgree` still accepts.
98
+ * yields the pre-boot-id shape that `verifyProcessGeneration` compares by ticks.
102
99
  */
103
100
  export function procfsStartToken(pid) {
104
101
  let stat;
@@ -152,93 +149,63 @@ function defaultPsRun(pid) {
152
149
  export function processStartToken(pid) {
153
150
  if (!Number.isFinite(pid) || pid <= 0)
154
151
  return null;
155
- const probe = startTokenProbe();
156
- return probe === "windows"
157
- ? windowsProcessStartToken(pid)
158
- : probe === "procfs"
159
- ? procfsStartToken(pid)
160
- : psStartToken(pid);
152
+ return startTokenProbe() === "procfs" ? procfsStartToken(pid) : psStartToken(pid);
161
153
  }
162
- /** A missing or unreadable token never establishes process identity. */
154
+ /**
155
+ * Does `pid` still name the run that wrote `recordedToken`?
156
+ *
157
+ * - `"same"`: both tokens present and equal.
158
+ * - `"different"`: the pid was recycled. A liveness check cannot see this,
159
+ * because a recycled pid is alive.
160
+ * - `"unknown"`: a token is missing, malformed, or unreadable. Never treat
161
+ * this as proof of identity.
162
+ *
163
+ * A Linux token written without a boot segment cannot identify a run across
164
+ * reboots, so it is compared on its tick count: a different count proves a
165
+ * different run, and an equal count stays unknown.
166
+ */
163
167
  export function verifyProcessGeneration(pid, recordedToken, readToken = processStartToken) {
164
168
  if (!Number.isInteger(pid) ||
165
169
  pid <= 0 ||
166
170
  typeof recordedToken !== "string" ||
167
171
  !recordedToken.trim())
168
172
  return "unknown";
169
- // A bootless Linux token cannot identify a process across machine boots.
170
- if (/^l\d+$/.test(recordedToken))
171
- return "unknown";
173
+ let currentToken;
172
174
  try {
173
- const currentToken = readToken(pid);
174
- if (typeof currentToken !== "string" || !currentToken.trim())
175
- return "unknown";
176
- if (/^l\d+$/.test(currentToken))
177
- return "unknown";
178
- return currentToken === recordedToken ? "same" : "different";
175
+ currentToken = readToken(pid);
179
176
  }
180
177
  catch {
181
178
  return "unknown";
182
179
  }
180
+ if (typeof currentToken !== "string" || !currentToken.trim())
181
+ return "unknown";
182
+ const bootless = (token) => /^l\d+$/.test(token);
183
+ if (recordedToken[0] === "l" &&
184
+ currentToken[0] === "l" &&
185
+ (bootless(recordedToken) || bootless(currentToken)))
186
+ return ticksOf(recordedToken) === ticksOf(currentToken) ? "unknown" : "different";
187
+ return currentToken === recordedToken ? "same" : "different";
188
+ }
189
+ function ticksOf(token) {
190
+ const dot = token.indexOf(".");
191
+ return dot === -1 ? token.slice(1) : token.slice(dot + 1);
183
192
  }
184
193
  /**
185
- * Observations may refuse a signal, but only the retained native process handle
186
- * can authorize it. No numeric-PID effect follows the preliminary token read.
194
+ * Recheck the process generation immediately before sending the signal.
195
+ *
196
+ * The process can still exit and its PID can be reused between the token read
197
+ * and the signal. Eliminating that race requires a generation-bound OS handle.
187
198
  */
188
199
  export function signalIfSameGeneration(pid, recordedToken, signal, dependencies = {}) {
200
+ const kill = dependencies.kill ?? process.kill;
189
201
  const verdict = verifyProcessGeneration(pid, recordedToken, dependencies.readToken);
190
202
  if (verdict !== "same")
191
203
  return { sent: false, verdict };
192
204
  try {
193
- return (dependencies.signalProcess ?? signalProcessWithHandle)(pid, recordedToken, signal);
205
+ kill(pid, signal);
206
+ return { sent: true, verdict };
194
207
  }
195
208
  catch {
196
- return { sent: false, verdict: "unknown" };
209
+ return { sent: false, verdict };
197
210
  }
198
211
  }
199
- /**
200
- * Do two tokens describe the same run?
201
- *
202
- * Equality, plus one concession: a row written before tokens carried a boot
203
- * segment recorded the tick count alone, so it is compared on the part it
204
- * actually recorded. Refusing that would call every pre-upgrade row a recycled
205
- * pid and prune a working machine's live rows the first time the new code ran.
206
- * Such a row loses its reboot protection until the next write rewrites it,
207
- * which is a strictly better position than the one it came from.
208
- */
209
- export function tokensAgree(recorded, current) {
210
- if (recorded === current)
211
- return true;
212
- // Only the Linux form has a boot segment to disagree about, and only when
213
- // exactly one side carries one is this a shape difference rather than two
214
- // genuinely different start instants.
215
- if (recorded[0] !== "l" || current[0] !== "l")
216
- return false;
217
- if (recorded.includes(".") === current.includes("."))
218
- return false;
219
- return ticksOf(recorded) === ticksOf(current);
220
- }
221
- function ticksOf(token) {
222
- const dot = token.indexOf(".");
223
- return dot === -1 ? token.slice(1) : token.slice(dot + 1);
224
- }
225
- /**
226
- * Does `pid` still name the process a row recorded?
227
- *
228
- * Three answers, and the difference between the last two matters:
229
- *
230
- * - `"match"` — recorded token, current token, same run. The row is trustworthy.
231
- * - `"mismatch"` — both present and different. The pid was recycled. This is
232
- * the case a liveness check cannot see, because a recycled pid is alive.
233
- * - `"unverified"` — no recorded token (a row written before tokens existed) or
234
- * no current one (an unsupported platform). Callers treat this the way they
235
- * treated every row previously: trust it if the pid is alive.
236
- */
237
- export function checkPidToken(pid, recorded) {
238
- if (!recorded)
239
- return "unverified";
240
- const current = processStartToken(pid);
241
- if (!current)
242
- return "unverified";
243
- return tokensAgree(recorded, current) ? "match" : "mismatch";
244
- }
@@ -0,0 +1,61 @@
1
+ import { type ProcessGenerationSignalDependencies, type ProcessGenerationVerdict } from "./proc-start.js";
2
+ export interface ServiceProcessDependencies extends ProcessGenerationSignalDependencies {
3
+ /** Throws ESRCH, or returns false, when the process is gone. */
4
+ probePid?: (pid: number) => unknown;
5
+ }
6
+ export type ServiceLiveness = "running" | "stopped" | "unknown";
7
+ export interface ServiceProcessOwner {
8
+ pid: number;
9
+ host: string;
10
+ start_token?: string;
11
+ state?: string;
12
+ }
13
+ /**
14
+ * A stale heartbeat does not prove that a process has exited. Another host, a
15
+ * probe error other than ESRCH, or an unverifiable token answers unknown. A
16
+ * recorded `stopped` state counts only while the generation still matches.
17
+ */
18
+ export declare function observeServiceProcess(owner: ServiceProcessOwner, dependencies?: ServiceProcessDependencies): {
19
+ generation: ProcessGenerationVerdict;
20
+ liveness: ServiceLiveness;
21
+ };
22
+ /** Refuse a start while another owner runs or cannot be identified. */
23
+ export declare function serviceStartRefusal(label: string, stopCommand: string, pid: number | undefined, liveness: ServiceLiveness): Error;
24
+ export interface ServiceLease {
25
+ pid: number;
26
+ start_token?: string;
27
+ host: string;
28
+ nonce: string;
29
+ created_at?: string;
30
+ }
31
+ export declare function readServiceLease(path: string): ServiceLease | undefined;
32
+ /** An absent lease is stopped; an unreadable one cannot be identified. */
33
+ export declare function serviceLeaseOwner(path: string): {
34
+ lease?: ServiceLease;
35
+ liveness: ServiceLiveness;
36
+ };
37
+ /**
38
+ * Take the singleton lease with an exclusive create. A lease whose owner is
39
+ * proven stopped is replaced; any other owner refuses the start.
40
+ */
41
+ export declare function acquireServiceLease(path: string, refuse: (pid: number | undefined, liveness: ServiceLiveness) => Error, fields?: Record<string, unknown>): {
42
+ nonce: string;
43
+ release: () => void;
44
+ };
45
+ export interface ServiceStopFields {
46
+ signal_sent: boolean;
47
+ signal_verdict: ProcessGenerationVerdict;
48
+ }
49
+ /**
50
+ * Write the durable stop request, then SIGTERM the owner `status` observed as
51
+ * running on this host, after a fresh generation check. The request stays for
52
+ * the daemon either way; `read` reports the state afterward.
53
+ */
54
+ export declare function requestServiceStop<S extends {
55
+ liveness: ServiceLiveness;
56
+ generation: ProcessGenerationVerdict;
57
+ record?: ServiceProcessOwner;
58
+ }>(status: S, read: () => S, stopPath: string, dependencies?: ServiceProcessDependencies, options?: {
59
+ signal?: boolean;
60
+ }): S & ServiceStopFields;
61
+ //# sourceMappingURL=service-process.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service-process.d.ts","sourceRoot":"","sources":["../../../../src/core/agents/state/service-process.ts"],"names":[],"mappings":"AAmBA,OAAO,EACL,KAAK,mCAAmC,EACxC,KAAK,wBAAwB,EAI9B,MAAM,iBAAiB,CAAC;AAEzB,MAAM,WAAW,0BAA2B,SAAQ,mCAAmC;IACrF,gEAAgE;IAChE,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;CACrC;AAED,MAAM,MAAM,eAAe,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;AAEhE,MAAM,WAAW,mBAAmB;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,mBAAmB,EAC1B,YAAY,GAAE,0BAA+B,GAC5C;IAAE,UAAU,EAAE,wBAAwB,CAAC;IAAC,QAAQ,EAAE,eAAe,CAAA;CAAE,CAuBrE;AAED,uEAAuE;AACvE,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,MAAM,GAAG,SAAS,EACvB,QAAQ,EAAE,eAAe,GACxB,KAAK,CAMP;AAED,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAmBvE;AAED,0EAA0E;AAC1E,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG;IAC/C,KAAK,CAAC,EAAE,YAAY,CAAC;IACrB,QAAQ,EAAE,eAAe,CAAC;CAC3B,CAIA;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,EAAE,QAAQ,EAAE,eAAe,KAAK,KAAK,EACrE,MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACnC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,IAAI,CAAA;CAAE,CA0CxC;AAED,MAAM,WAAW,iBAAiB;IAChC,WAAW,EAAE,OAAO,CAAC;IACrB,cAAc,EAAE,wBAAwB,CAAC;CAC1C;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,CAAC,SAAS;IACR,QAAQ,EAAE,eAAe,CAAC;IAC1B,UAAU,EAAE,wBAAwB,CAAC;IACrC,MAAM,CAAC,EAAE,mBAAmB,CAAC;CAC9B,EAED,MAAM,EAAE,CAAC,EACT,IAAI,EAAE,MAAM,CAAC,EACb,QAAQ,EAAE,MAAM,EAChB,YAAY,GAAE,0BAA+B,EAC7C,OAAO,GAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAA;CAAO,GACjC,CAAC,GAAG,iBAAiB,CAevB"}
@@ -0,0 +1,143 @@
1
+ /**
2
+ * One owner check for every singleton background service (governor, local
3
+ * supervisor, legacy resource observer): is the recorded process still the
4
+ * run that wrote the record, and how is a stop requested?
5
+ */
6
+ import { randomUUID } from "node:crypto";
7
+ import { closeSync, existsSync, mkdirSync, openSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
8
+ import { hostname } from "node:os";
9
+ import { dirname } from "node:path";
10
+ import { writePrivateJsonAtomic } from "../../storage/atomic-json.js";
11
+ import { stateDirMode, stateFileMode } from "../../storage/modes.js";
12
+ import { processStartToken, signalIfSameGeneration, verifyProcessGeneration, } from "./proc-start.js";
13
+ /**
14
+ * A stale heartbeat does not prove that a process has exited. Another host, a
15
+ * probe error other than ESRCH, or an unverifiable token answers unknown. A
16
+ * recorded `stopped` state counts only while the generation still matches.
17
+ */
18
+ export function observeServiceProcess(owner, dependencies = {}) {
19
+ if (owner.host !== hostname())
20
+ return { generation: "unknown", liveness: "unknown" };
21
+ try {
22
+ if ((dependencies.probePid ?? ((pid) => process.kill(pid, 0)))(owner.pid) === false)
23
+ return { generation: "unknown", liveness: "stopped" };
24
+ }
25
+ catch (error) {
26
+ return {
27
+ generation: "unknown",
28
+ liveness: error.code === "ESRCH" ? "stopped" : "unknown",
29
+ };
30
+ }
31
+ const generation = verifyProcessGeneration(owner.pid, owner.start_token, dependencies.readToken);
32
+ return {
33
+ generation,
34
+ liveness: generation === "same"
35
+ ? owner.state === "stopped"
36
+ ? "stopped"
37
+ : "running"
38
+ : generation === "different"
39
+ ? "stopped"
40
+ : "unknown",
41
+ };
42
+ }
43
+ /** Refuse a start while another owner runs or cannot be identified. */
44
+ export function serviceStartRefusal(label, stopCommand, pid, liveness) {
45
+ return new Error(liveness === "running"
46
+ ? `${label} is already running under pid ${pid}`
47
+ : `${label} process generation is unknown${pid ? ` for pid ${pid}` : ""}; run \`${stopCommand}\` to write a durable stop request before starting it again`);
48
+ }
49
+ export function readServiceLease(path) {
50
+ try {
51
+ const value = JSON.parse(readFileSync(path, "utf8"));
52
+ if (!Number.isSafeInteger(value.pid) ||
53
+ (value.pid ?? 0) <= 0 ||
54
+ typeof value.host !== "string" ||
55
+ !value.host ||
56
+ typeof value.nonce !== "string" ||
57
+ !value.nonce)
58
+ return undefined;
59
+ return {
60
+ ...value,
61
+ start_token: typeof value.start_token === "string" ? value.start_token : undefined,
62
+ };
63
+ }
64
+ catch {
65
+ return undefined;
66
+ }
67
+ }
68
+ /** An absent lease is stopped; an unreadable one cannot be identified. */
69
+ export function serviceLeaseOwner(path) {
70
+ if (!existsSync(path))
71
+ return { liveness: "stopped" };
72
+ const lease = readServiceLease(path);
73
+ return { lease, liveness: lease ? observeServiceProcess(lease).liveness : "unknown" };
74
+ }
75
+ /**
76
+ * Take the singleton lease with an exclusive create. A lease whose owner is
77
+ * proven stopped is replaced; any other owner refuses the start.
78
+ */
79
+ export function acquireServiceLease(path, refuse, fields = {}) {
80
+ mkdirSync(dirname(path), { recursive: true, mode: stateDirMode() });
81
+ for (let attempt = 0; attempt < 2; attempt++) {
82
+ const startToken = processStartToken(process.pid);
83
+ const lease = {
84
+ ...fields,
85
+ pid: process.pid,
86
+ ...(startToken ? { start_token: startToken } : {}),
87
+ host: hostname(),
88
+ nonce: randomUUID(),
89
+ created_at: new Date().toISOString(),
90
+ };
91
+ try {
92
+ const fd = openSync(path, "wx", stateFileMode());
93
+ try {
94
+ writeFileSync(fd, `${JSON.stringify(lease)}\n`, "utf8");
95
+ }
96
+ finally {
97
+ closeSync(fd);
98
+ }
99
+ return {
100
+ nonce: lease.nonce,
101
+ release: () => {
102
+ if (readServiceLease(path)?.nonce !== lease.nonce)
103
+ return;
104
+ try {
105
+ unlinkSync(path);
106
+ }
107
+ catch {
108
+ // A missing lease is already released.
109
+ }
110
+ },
111
+ };
112
+ }
113
+ catch (error) {
114
+ if (error.code !== "EEXIST")
115
+ throw error;
116
+ const owner = serviceLeaseOwner(path);
117
+ if (owner.liveness !== "stopped")
118
+ throw refuse(owner.lease?.pid, owner.liveness);
119
+ try {
120
+ unlinkSync(path);
121
+ }
122
+ catch {
123
+ // A concurrent starter may have replaced or removed the stale lease.
124
+ }
125
+ }
126
+ }
127
+ throw new Error(`could not acquire the singleton lease at ${path}`);
128
+ }
129
+ /**
130
+ * Write the durable stop request, then SIGTERM the owner `status` observed as
131
+ * running on this host, after a fresh generation check. The request stays for
132
+ * the daemon either way; `read` reports the state afterward.
133
+ */
134
+ export function requestServiceStop(status, read, stopPath, dependencies = {}, options = {}) {
135
+ writePrivateJsonAtomic(stopPath, {
136
+ requested_at: new Date().toISOString(),
137
+ requested_by_pid: process.pid,
138
+ });
139
+ const signal = options.signal !== false && status.liveness === "running" && status.record?.host === hostname()
140
+ ? signalIfSameGeneration(status.record.pid, status.record.start_token, "SIGTERM", dependencies)
141
+ : { sent: false, verdict: status.generation };
142
+ return { ...read(), signal_sent: signal.sent, signal_verdict: signal.verdict };
143
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"stale-sweep.d.ts","sourceRoot":"","sources":["../../../../src/core/agents/state/stale-sweep.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAyIH,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG;IAC7C,iBAAiB,EAAE,MAAM,EAAE,CAAC;IAC5B,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;CAC/B,CAuPA"}
1
+ {"version":3,"file":"stale-sweep.d.ts","sourceRoot":"","sources":["../../../../src/core/agents/state/stale-sweep.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAkEH,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG;IAC7C,iBAAiB,EAAE,MAAM,EAAE,CAAC;IAC5B,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;CAC/B,CAyJA"}