issue-flow 0.11.0 → 0.12.0

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 (118) hide show
  1. package/README.md +1 -1
  2. package/dist/{analyze-7B5TORNK.js → analyze-OEET7KGE.js} +17 -11
  3. package/dist/analyze-OEET7KGE.js.map +1 -0
  4. package/dist/{apply-WUJHDD7A.js → apply-ZIA3C52S.js} +6 -4
  5. package/dist/{apply-WUJHDD7A.js.map → apply-ZIA3C52S.js.map} +1 -1
  6. package/dist/{chunk-6N7LZZ5U.js → chunk-3QHK4YQA.js} +204 -107
  7. package/dist/chunk-3QHK4YQA.js.map +1 -0
  8. package/dist/chunk-42PJFMAR.js +605 -0
  9. package/dist/chunk-42PJFMAR.js.map +1 -0
  10. package/dist/{chunk-PL6CNMVS.js → chunk-4JSOYE4G.js} +105 -19
  11. package/dist/chunk-4JSOYE4G.js.map +1 -0
  12. package/dist/chunk-56KS3TFQ.js +703 -0
  13. package/dist/chunk-56KS3TFQ.js.map +1 -0
  14. package/dist/{chunk-AAY6KM7N.js → chunk-73ATPIYX.js} +160 -154
  15. package/dist/chunk-73ATPIYX.js.map +1 -0
  16. package/dist/{chunk-MKD2D7WE.js → chunk-A4SLHOW7.js} +70 -13
  17. package/dist/chunk-A4SLHOW7.js.map +1 -0
  18. package/dist/chunk-BAUPQXPE.js +173 -0
  19. package/dist/chunk-BAUPQXPE.js.map +1 -0
  20. package/dist/{chunk-HC2MT7BX.js → chunk-BYZDPR7N.js} +14 -581
  21. package/dist/chunk-BYZDPR7N.js.map +1 -0
  22. package/dist/chunk-CD5ULRVC.js +615 -0
  23. package/dist/chunk-CD5ULRVC.js.map +1 -0
  24. package/dist/chunk-DIDUVL6O.js +1365 -0
  25. package/dist/chunk-DIDUVL6O.js.map +1 -0
  26. package/dist/{chunk-47VIN33V.js → chunk-EZATO3DW.js} +7 -5
  27. package/dist/{chunk-47VIN33V.js.map → chunk-EZATO3DW.js.map} +1 -1
  28. package/dist/{chunk-JW7WLSZ6.js → chunk-FADTCQGM.js} +48 -12
  29. package/dist/chunk-FADTCQGM.js.map +1 -0
  30. package/dist/{chunk-GANSDJV7.js → chunk-FJP3UQGD.js} +15 -13
  31. package/dist/chunk-FJP3UQGD.js.map +1 -0
  32. package/dist/chunk-KMMASOA6.js +134 -0
  33. package/dist/chunk-KMMASOA6.js.map +1 -0
  34. package/dist/{chunk-HQYXVIH6.js → chunk-LAFNLLQP.js} +3 -3
  35. package/dist/chunk-LWRSMTTZ.js +18 -0
  36. package/dist/chunk-LWRSMTTZ.js.map +1 -0
  37. package/dist/{chunk-MSPUB635.js → chunk-M5UCXTIT.js} +15 -12
  38. package/dist/chunk-M5UCXTIT.js.map +1 -0
  39. package/dist/chunk-M66SJHQQ.js +206 -0
  40. package/dist/chunk-M66SJHQQ.js.map +1 -0
  41. package/dist/{chunk-OBLVLIUS.js → chunk-MJULIKQU.js} +42 -6
  42. package/dist/chunk-MJULIKQU.js.map +1 -0
  43. package/dist/{chunk-RKTEV6EG.js → chunk-NS6R37II.js} +4 -4
  44. package/dist/{run-H75MD5BQ.js → chunk-NTMMF5UD.js} +515 -544
  45. package/dist/chunk-NTMMF5UD.js.map +1 -0
  46. package/dist/{chunk-UBIQXCVE.js → chunk-QCPGHBSZ.js} +16 -13
  47. package/dist/chunk-QCPGHBSZ.js.map +1 -0
  48. package/dist/{chunk-LVKQ2AKT.js → chunk-TSOLWK7Y.js} +8 -6
  49. package/dist/chunk-TSOLWK7Y.js.map +1 -0
  50. package/dist/{chunk-VCKX262Z.js → chunk-XQUSIEKA.js} +13 -10
  51. package/dist/chunk-XQUSIEKA.js.map +1 -0
  52. package/dist/chunk-Y5ZDJ4PJ.js +76 -0
  53. package/dist/chunk-Y5ZDJ4PJ.js.map +1 -0
  54. package/dist/cli.js +156 -26
  55. package/dist/cli.js.map +1 -1
  56. package/dist/{config-4THHOXNN.js → config-PRJGWW7J.js} +15 -3
  57. package/dist/execute-XBNVL753.js +18 -0
  58. package/dist/{generate-6RA2MFJB.js → generate-EGB4KV2B.js} +18 -11
  59. package/dist/generate-EGB4KV2B.js.map +1 -0
  60. package/dist/init-AJHCXJBP.js +10 -0
  61. package/dist/operations-BYT626KQ.js +365 -0
  62. package/dist/operations-BYT626KQ.js.map +1 -0
  63. package/dist/plan-QOZ3RIO6.js +21 -0
  64. package/dist/{policy-XQW5NUIE.js → policy-6S4GFC76.js} +6 -4
  65. package/dist/{policy-XQW5NUIE.js.map → policy-6S4GFC76.js.map} +1 -1
  66. package/dist/pr-EBBHD2ZX.js +26 -0
  67. package/dist/pr-review-VVMW5PO4.js +20 -0
  68. package/dist/prd-JQSZF7PR.js +21 -0
  69. package/dist/resume-PPCNQVVV.js +221 -0
  70. package/dist/resume-PPCNQVVV.js.map +1 -0
  71. package/dist/review-3F6MARWH.js +19 -0
  72. package/dist/run-RXE5GDQO.js +36 -0
  73. package/dist/run-RXE5GDQO.js.map +1 -0
  74. package/dist/state-manager-2EDVDBAR.js +34 -0
  75. package/dist/state-manager-2EDVDBAR.js.map +1 -0
  76. package/dist/{web-ANQQTTRD.js → web-EF5WIIGG.js} +13 -8
  77. package/dist/{web-ANQQTTRD.js.map → web-EF5WIIGG.js.map} +1 -1
  78. package/package.json +1 -1
  79. package/prompts/pr.md +11 -4
  80. package/web/public/app.js +12 -0
  81. package/dist/analyze-7B5TORNK.js.map +0 -1
  82. package/dist/chunk-3BOHTH4V.js +0 -58
  83. package/dist/chunk-3BOHTH4V.js.map +0 -1
  84. package/dist/chunk-6N7LZZ5U.js.map +0 -1
  85. package/dist/chunk-AAY6KM7N.js.map +0 -1
  86. package/dist/chunk-GANSDJV7.js.map +0 -1
  87. package/dist/chunk-HC2MT7BX.js.map +0 -1
  88. package/dist/chunk-HDV3YDTG.js +0 -52
  89. package/dist/chunk-HDV3YDTG.js.map +0 -1
  90. package/dist/chunk-JW7WLSZ6.js.map +0 -1
  91. package/dist/chunk-LVKQ2AKT.js.map +0 -1
  92. package/dist/chunk-MKD2D7WE.js.map +0 -1
  93. package/dist/chunk-MSPUB635.js.map +0 -1
  94. package/dist/chunk-OBLVLIUS.js.map +0 -1
  95. package/dist/chunk-PL6CNMVS.js.map +0 -1
  96. package/dist/chunk-UBIQXCVE.js.map +0 -1
  97. package/dist/chunk-VCKX262Z.js.map +0 -1
  98. package/dist/chunk-VQ5VN7DF.js +0 -1018
  99. package/dist/chunk-VQ5VN7DF.js.map +0 -1
  100. package/dist/execute-5OEKLM5X.js +0 -15
  101. package/dist/generate-6RA2MFJB.js.map +0 -1
  102. package/dist/init-PRMOTQ7N.js +0 -9
  103. package/dist/plan-KENR6MJ3.js +0 -17
  104. package/dist/pr-KDGKG4BP.js +0 -21
  105. package/dist/pr-review-CAMAB7KO.js +0 -17
  106. package/dist/prd-V3MWDIHK.js +0 -17
  107. package/dist/review-7LN47SAN.js +0 -15
  108. package/dist/run-H75MD5BQ.js.map +0 -1
  109. /package/dist/{chunk-HQYXVIH6.js.map → chunk-LAFNLLQP.js.map} +0 -0
  110. /package/dist/{chunk-RKTEV6EG.js.map → chunk-NS6R37II.js.map} +0 -0
  111. /package/dist/{config-4THHOXNN.js.map → config-PRJGWW7J.js.map} +0 -0
  112. /package/dist/{execute-5OEKLM5X.js.map → execute-XBNVL753.js.map} +0 -0
  113. /package/dist/{init-PRMOTQ7N.js.map → init-AJHCXJBP.js.map} +0 -0
  114. /package/dist/{plan-KENR6MJ3.js.map → plan-QOZ3RIO6.js.map} +0 -0
  115. /package/dist/{pr-KDGKG4BP.js.map → pr-EBBHD2ZX.js.map} +0 -0
  116. /package/dist/{pr-review-CAMAB7KO.js.map → pr-review-VVMW5PO4.js.map} +0 -0
  117. /package/dist/{prd-V3MWDIHK.js.map → prd-JQSZF7PR.js.map} +0 -0
  118. /package/dist/{review-7LN47SAN.js.map → review-3F6MARWH.js.map} +0 -0
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ runLockSchema
4
+ } from "./chunk-DIDUVL6O.js";
5
+
6
+ // src/storage/lock.ts
7
+ import { mkdir, readFile, rm, writeFile } from "fs/promises";
8
+ import { hostname } from "os";
9
+ import { dirname } from "path";
10
+ var RUN_LOCK_HEARTBEAT_MS = 1e4;
11
+ var RUN_LOCK_STALE_INTERVALS = 3;
12
+ var CLAIM_RETRIES = 3;
13
+ function isProcessAlive(pid) {
14
+ try {
15
+ process.kill(pid, 0);
16
+ return true;
17
+ } catch (err) {
18
+ return err.code === "EPERM";
19
+ }
20
+ }
21
+ async function readRunLock(lockFile) {
22
+ let raw;
23
+ try {
24
+ raw = await readFile(lockFile, "utf-8");
25
+ } catch {
26
+ return null;
27
+ }
28
+ try {
29
+ const result = runLockSchema.safeParse(JSON.parse(raw));
30
+ return result.success ? result.data : null;
31
+ } catch {
32
+ return null;
33
+ }
34
+ }
35
+ async function removeRunLock(lockFile) {
36
+ try {
37
+ await rm(lockFile, { force: true });
38
+ } catch {
39
+ }
40
+ }
41
+ function isRunLockStale(lock, options = {}) {
42
+ const heartbeatMs = options.heartbeatMs ?? RUN_LOCK_HEARTBEAT_MS;
43
+ const staleIntervals = options.staleIntervals ?? RUN_LOCK_STALE_INTERVALS;
44
+ const now = (options.clock ?? Date.now)();
45
+ const host = options.host ?? hostname();
46
+ const beat = Date.parse(lock.lastHeartbeatAt);
47
+ const silent = Number.isNaN(beat) || now - beat > heartbeatMs * staleIntervals;
48
+ if (lock.host !== host) return silent;
49
+ return silent || !isProcessAlive(lock.pid);
50
+ }
51
+ function isSelf(lock, pid, host) {
52
+ return lock.pid === pid && lock.host === host;
53
+ }
54
+ function describeRunLockOwner(lock) {
55
+ return `pid ${lock.pid} on ${lock.host} (issue ${lock.target}, started ${lock.startedAt}, last heartbeat ${lock.lastHeartbeatAt})`;
56
+ }
57
+ async function touchRunLock(lockFile, lock, at) {
58
+ const next = { ...lock, lastHeartbeatAt: at };
59
+ try {
60
+ await writeFile(lockFile, `${JSON.stringify(next, null, 2)}
61
+ `, "utf-8");
62
+ } catch {
63
+ }
64
+ return next;
65
+ }
66
+ async function claim(lockFile, lock) {
67
+ try {
68
+ await mkdir(dirname(lockFile), { recursive: true });
69
+ await writeFile(lockFile, `${JSON.stringify(lock, null, 2)}
70
+ `, {
71
+ encoding: "utf-8",
72
+ flag: "wx"
73
+ });
74
+ return true;
75
+ } catch (err) {
76
+ if (err.code === "EEXIST") return false;
77
+ throw err;
78
+ }
79
+ }
80
+ async function acquireRunLock(lockFile, options) {
81
+ const pid = options.pid ?? process.pid;
82
+ const host = options.host ?? hostname();
83
+ const heartbeatMs = options.heartbeatMs ?? RUN_LOCK_HEARTBEAT_MS;
84
+ const clock = options.clock ?? Date.now;
85
+ const staleness = {
86
+ heartbeatMs,
87
+ ...options.staleIntervals === void 0 ? {} : { staleIntervals: options.staleIntervals },
88
+ clock,
89
+ pid,
90
+ host
91
+ };
92
+ let reclaimedFrom = null;
93
+ for (let attempt = 0; attempt <= CLAIM_RETRIES; attempt++) {
94
+ const at = new Date(clock()).toISOString();
95
+ const mine = {
96
+ pid,
97
+ host,
98
+ target: options.target,
99
+ startedAt: at,
100
+ lastHeartbeatAt: at
101
+ };
102
+ if (await claim(lockFile, mine)) {
103
+ return { ok: true, handle: startHandle(lockFile, mine, reclaimedFrom, options) };
104
+ }
105
+ const existing = await readRunLock(lockFile);
106
+ if (existing === null) {
107
+ await removeRunLock(lockFile);
108
+ continue;
109
+ }
110
+ if (isSelf(existing, pid, host)) {
111
+ return { ok: true, handle: nestedHandle(existing) };
112
+ }
113
+ if (!isRunLockStale(existing, staleness)) {
114
+ return { ok: false, owner: existing };
115
+ }
116
+ reclaimedFrom = existing;
117
+ try {
118
+ options.onTakeover?.(existing);
119
+ } catch {
120
+ }
121
+ await removeRunLock(lockFile);
122
+ }
123
+ const owner = await readRunLock(lockFile);
124
+ if (owner !== null) return { ok: false, owner };
125
+ return {
126
+ ok: false,
127
+ owner: {
128
+ pid: 0,
129
+ host,
130
+ target: options.target,
131
+ startedAt: new Date(clock()).toISOString(),
132
+ lastHeartbeatAt: new Date(clock()).toISOString()
133
+ }
134
+ };
135
+ }
136
+ function startHandle(lockFile, lock, reclaimedFrom, options) {
137
+ const heartbeatMs = options.heartbeatMs ?? RUN_LOCK_HEARTBEAT_MS;
138
+ const clock = options.clock ?? Date.now;
139
+ let current = lock;
140
+ let timer = null;
141
+ if (options.heartbeat !== false) {
142
+ timer = setInterval(() => {
143
+ void touchRunLock(lockFile, current, new Date(clock()).toISOString()).then((next) => {
144
+ current = next;
145
+ });
146
+ }, heartbeatMs);
147
+ timer.unref();
148
+ }
149
+ return {
150
+ lock,
151
+ reclaimedFrom,
152
+ release: async () => {
153
+ if (timer !== null) {
154
+ clearInterval(timer);
155
+ timer = null;
156
+ }
157
+ await removeRunLock(lockFile);
158
+ }
159
+ };
160
+ }
161
+ function nestedHandle(lock) {
162
+ return { lock, reclaimedFrom: null, release: async () => {
163
+ } };
164
+ }
165
+
166
+ export {
167
+ isProcessAlive,
168
+ readRunLock,
169
+ isRunLockStale,
170
+ describeRunLockOwner,
171
+ acquireRunLock
172
+ };
173
+ //# sourceMappingURL=chunk-BAUPQXPE.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/storage/lock.ts"],"sourcesContent":["import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';\nimport { hostname } from 'node:os';\nimport { dirname } from 'node:path';\nimport { type RunLock, runLockSchema } from './schemas.js';\n\n/**\n * Run ownership: who is executing in this project right now.\n *\n * Generalised from `web/lock.ts`, which has guarded the monitoring server the\n * same way since it existed — atomic `wx` create, `process.kill(pid, 0)` for\n * liveness, and a read that degrades to \"no lock\" on anything malformed. What\n * this module adds is the piece a *long* run needs and a server does not: a\n * **heartbeat**. A web server proves it is alive by answering a health probe;\n * a pipeline has no port to probe, so it says so in the file instead.\n *\n * The two signals answer different questions and both are needed:\n *\n * - the **pid** catches the machine that rebooted or the process that was\n * `kill -9`ed — the lock outlives its owner, and nothing else would notice;\n * - the **heartbeat** catches the owner that is technically alive but no longer\n * running (a pid reused by an unrelated process is the same case), and it is\n * the only signal that survives being read from another host.\n *\n * Nothing here is destructive by inference: a lock is only taken over when it\n * is stale by both measures the policy states, and taking one over is reported\n * to the caller (`reclaimedFrom`) so the run can be recorded as interrupted\n * rather than silently restarted.\n */\n\n/** The lock is rewritten this often while a run holds it. */\nexport const RUN_LOCK_HEARTBEAT_MS = 10_000;\n\n/**\n * How many missed heartbeats make a lock stale.\n *\n * Three, not one: a machine under load, a suspended laptop or a slow\n * filesystem can miss a beat without the run being dead, and stealing a lock\n * from a live owner is the failure this module exists to prevent.\n */\nexport const RUN_LOCK_STALE_INTERVALS = 3;\n\n/** How many times a claim is retried after clearing a stale lock. */\nconst CLAIM_RETRIES = 3;\n\nexport interface RunLockHandle {\n readonly lock: RunLock;\n /**\n * The dead owner this lock was taken from, when it was taken from one.\n *\n * `null` on a clean acquisition. Non-null is the fact a caller records as an\n * *interrupted* run: something was executing here and never finished.\n */\n readonly reclaimedFrom: RunLock | null;\n /** Stop the heartbeat and remove the lock. Never rejects. */\n release(): Promise<void>;\n}\n\nexport type AcquireRunLockResult =\n | { ok: true; handle: RunLockHandle }\n | { ok: false; owner: RunLock };\n\nexport interface AcquireRunLockOptions {\n /** Issue (or queue) identifier this run works on. */\n target: string;\n /** Rewrite interval. Also the unit `staleIntervals` counts. */\n heartbeatMs?: number;\n staleIntervals?: number;\n /** Injectable identity, so a test can pretend to be another process. */\n pid?: number;\n host?: string;\n /** Injectable clock, so staleness is testable without waiting. */\n clock?: () => number;\n /** Called once, when a stale lock is taken over. Never throws through. */\n onTakeover?: (previous: RunLock) => void;\n /**\n * Start the rewriting timer. Default true; a test that drives the clock by\n * hand turns it off and calls `touchRunLock` itself.\n */\n heartbeat?: boolean;\n}\n\n/**\n * `process.kill(pid, 0)` throws `ESRCH` when the pid is gone and `EPERM` when\n * it is alive but owned by another user — **only `ESRCH` means \"not alive\"**.\n * Reading `EPERM` as death is how a lock gets stolen from a healthy run\n * started by another user.\n */\nexport function isProcessAlive(pid: number): boolean {\n try {\n process.kill(pid, 0);\n return true;\n } catch (err) {\n return (err as NodeJS.ErrnoException).code === 'EPERM';\n }\n}\n\n/**\n * Read a lock, degrading anything unreadable to \"no lock\".\n *\n * A partial write, a file from an incompatible release, a truncated JSON: all\n * of them mean the same thing operationally, and none of them justifies\n * failing a run. Same behaviour as `readWebLock` and `loadGlobalConfig`.\n */\nexport async function readRunLock(lockFile: string): Promise<RunLock | null> {\n let raw: string;\n try {\n raw = await readFile(lockFile, 'utf-8');\n } catch {\n return null;\n }\n try {\n const result = runLockSchema.safeParse(JSON.parse(raw));\n return result.success ? result.data : null;\n } catch {\n return null;\n }\n}\n\n/** Remove a lock file. Best effort; never rejects. */\nexport async function removeRunLock(lockFile: string): Promise<void> {\n try {\n await rm(lockFile, { force: true });\n } catch {\n /* best-effort cleanup */\n }\n}\n\nexport interface StalenessOptions {\n heartbeatMs?: number;\n staleIntervals?: number;\n clock?: () => number;\n /** Treat this pid as our own, so a re-entrant claim is not \"stale\". */\n pid?: number;\n host?: string;\n}\n\n/**\n * Whether `lock` may be taken over.\n *\n * A lock written on another host cannot be judged by pid — `process.kill` there\n * says nothing about a process here — so on a foreign host only the heartbeat\n * decides. That is the case a shared filesystem creates, and reading it wrong\n * is how two machines end up on the same branch.\n */\nexport function isRunLockStale(lock: RunLock, options: StalenessOptions = {}): boolean {\n const heartbeatMs = options.heartbeatMs ?? RUN_LOCK_HEARTBEAT_MS;\n const staleIntervals = options.staleIntervals ?? RUN_LOCK_STALE_INTERVALS;\n const now = (options.clock ?? Date.now)();\n const host = options.host ?? hostname();\n\n const beat = Date.parse(lock.lastHeartbeatAt);\n const silent = Number.isNaN(beat) || now - beat > heartbeatMs * staleIntervals;\n\n if (lock.host !== host) return silent;\n return silent || !isProcessAlive(lock.pid);\n}\n\n/** Whether this very process already owns `lock`. */\nfunction isSelf(lock: RunLock, pid: number, host: string): boolean {\n return lock.pid === pid && lock.host === host;\n}\n\n/**\n * One line naming the owner, for the message a refused invocation prints.\n *\n * The pid alone is useless to a person; the pid *plus the host plus when it\n * last said anything* is what lets them decide between waiting and killing it.\n */\nexport function describeRunLockOwner(lock: RunLock): string {\n return `pid ${lock.pid} on ${lock.host} (issue ${lock.target}, started ${lock.startedAt}, last heartbeat ${lock.lastHeartbeatAt})`;\n}\n\n/** Rewrite the lock with a fresh heartbeat. Never rejects. */\nexport async function touchRunLock(lockFile: string, lock: RunLock, at: string): Promise<RunLock> {\n const next: RunLock = { ...lock, lastHeartbeatAt: at };\n try {\n await writeFile(lockFile, `${JSON.stringify(next, null, 2)}\\n`, 'utf-8');\n } catch {\n // A heartbeat that cannot be written is not worth failing a run over: the\n // worst case is that another invocation eventually judges us stale, which\n // is the same outcome as this process having died.\n }\n return next;\n}\n\n/**\n * Atomic exclusive create. `wx` fails with `EEXIST` when someone else won the\n * race, which is what lets two concurrent invocations agree on one winner\n * without a lock for the lock.\n */\nasync function claim(lockFile: string, lock: RunLock): Promise<boolean> {\n try {\n await mkdir(dirname(lockFile), { recursive: true });\n await writeFile(lockFile, `${JSON.stringify(lock, null, 2)}\\n`, {\n encoding: 'utf-8',\n flag: 'wx',\n });\n return true;\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'EEXIST') return false;\n throw err;\n }\n}\n\n/**\n * Take ownership of the run in this project, or report who already has it.\n *\n * Three outcomes, and only three:\n *\n * 1. **free** — the lock is claimed and a heartbeat starts;\n * 2. **held by a dead owner** — the stale lock is cleared, claimed, and the\n * previous owner comes back on `reclaimedFrom` so the caller can record the\n * interrupted run;\n * 3. **held by a live owner** — `{ ok: false, owner }`, and the caller refuses\n * with a message naming it. Nothing is deleted, nothing is retried into\n * submission.\n *\n * A lock this very process already holds is outcome 1 with a no-op release:\n * re-entering is not a conflict, and a nested acquisition must not remove the\n * file the outer one still owns.\n */\nexport async function acquireRunLock(\n lockFile: string,\n options: AcquireRunLockOptions,\n): Promise<AcquireRunLockResult> {\n const pid = options.pid ?? process.pid;\n const host = options.host ?? hostname();\n const heartbeatMs = options.heartbeatMs ?? RUN_LOCK_HEARTBEAT_MS;\n const clock = options.clock ?? Date.now;\n const staleness: StalenessOptions = {\n heartbeatMs,\n ...(options.staleIntervals === undefined ? {} : { staleIntervals: options.staleIntervals }),\n clock,\n pid,\n host,\n };\n\n let reclaimedFrom: RunLock | null = null;\n\n for (let attempt = 0; attempt <= CLAIM_RETRIES; attempt++) {\n const at = new Date(clock()).toISOString();\n const mine: RunLock = {\n pid,\n host,\n target: options.target,\n startedAt: at,\n lastHeartbeatAt: at,\n };\n\n if (await claim(lockFile, mine)) {\n return { ok: true, handle: startHandle(lockFile, mine, reclaimedFrom, options) };\n }\n\n const existing = await readRunLock(lockFile);\n\n if (existing === null) {\n // Unreadable: a partial write, or a file from a shape we do not know.\n // Treated as absent, exactly as `readRunLock` documents.\n await removeRunLock(lockFile);\n continue;\n }\n\n if (isSelf(existing, pid, host)) {\n return { ok: true, handle: nestedHandle(existing) };\n }\n\n if (!isRunLockStale(existing, staleness)) {\n return { ok: false, owner: existing };\n }\n\n reclaimedFrom = existing;\n try {\n options.onTakeover?.(existing);\n } catch {\n // A caller's bookkeeping must not decide whether the run starts.\n }\n await removeRunLock(lockFile);\n }\n\n // Every attempt lost the race and every lock read was stale or unreadable:\n // something else is fighting for this file. Refusing is the safe answer.\n const owner = await readRunLock(lockFile);\n if (owner !== null) return { ok: false, owner };\n return {\n ok: false,\n owner: {\n pid: 0,\n host,\n target: options.target,\n startedAt: new Date(clock()).toISOString(),\n lastHeartbeatAt: new Date(clock()).toISOString(),\n },\n };\n}\n\nfunction startHandle(\n lockFile: string,\n lock: RunLock,\n reclaimedFrom: RunLock | null,\n options: AcquireRunLockOptions,\n): RunLockHandle {\n const heartbeatMs = options.heartbeatMs ?? RUN_LOCK_HEARTBEAT_MS;\n const clock = options.clock ?? Date.now;\n let current = lock;\n let timer: NodeJS.Timeout | null = null;\n\n if (options.heartbeat !== false) {\n timer = setInterval(() => {\n void touchRunLock(lockFile, current, new Date(clock()).toISOString()).then((next) => {\n current = next;\n });\n }, heartbeatMs);\n // The heartbeat must never be the reason a process stays alive.\n timer.unref();\n }\n\n return {\n lock,\n reclaimedFrom,\n release: async () => {\n if (timer !== null) {\n clearInterval(timer);\n timer = null;\n }\n await removeRunLock(lockFile);\n },\n };\n}\n\n/** A re-entrant acquisition: same owner, no timer of its own, no removal. */\nfunction nestedHandle(lock: RunLock): RunLockHandle {\n return { lock, reclaimedFrom: null, release: async () => {} };\n}\n"],"mappings":";;;;;;AAAA,SAAS,OAAO,UAAU,IAAI,iBAAiB;AAC/C,SAAS,gBAAgB;AACzB,SAAS,eAAe;AA4BjB,IAAM,wBAAwB;AAS9B,IAAM,2BAA2B;AAGxC,IAAM,gBAAgB;AA6Cf,SAAS,eAAe,KAAsB;AACnD,MAAI;AACF,YAAQ,KAAK,KAAK,CAAC;AACnB,WAAO;AAAA,EACT,SAAS,KAAK;AACZ,WAAQ,IAA8B,SAAS;AAAA,EACjD;AACF;AASA,eAAsB,YAAY,UAA2C;AAC3E,MAAI;AACJ,MAAI;AACF,UAAM,MAAM,SAAS,UAAU,OAAO;AAAA,EACxC,QAAQ;AACN,WAAO;AAAA,EACT;AACA,MAAI;AACF,UAAM,SAAS,cAAc,UAAU,KAAK,MAAM,GAAG,CAAC;AACtD,WAAO,OAAO,UAAU,OAAO,OAAO;AAAA,EACxC,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAGA,eAAsB,cAAc,UAAiC;AACnE,MAAI;AACF,UAAM,GAAG,UAAU,EAAE,OAAO,KAAK,CAAC;AAAA,EACpC,QAAQ;AAAA,EAER;AACF;AAmBO,SAAS,eAAe,MAAe,UAA4B,CAAC,GAAY;AACrF,QAAM,cAAc,QAAQ,eAAe;AAC3C,QAAM,iBAAiB,QAAQ,kBAAkB;AACjD,QAAM,OAAO,QAAQ,SAAS,KAAK,KAAK;AACxC,QAAM,OAAO,QAAQ,QAAQ,SAAS;AAEtC,QAAM,OAAO,KAAK,MAAM,KAAK,eA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