astrocode-workflow 0.4.1 → 0.4.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.
- package/dist/src/astro/workflow-runner.d.ts +1 -5
- package/dist/src/astro/workflow-runner.js +6 -17
- package/dist/src/index.js +0 -6
- package/dist/src/tools/health.js +0 -31
- package/dist/src/tools/index.js +0 -3
- package/dist/src/tools/repair.js +4 -37
- package/dist/src/tools/workflow.js +178 -192
- package/package.json +1 -1
- package/src/astro/workflow-runner.ts +5 -25
- package/src/index.ts +0 -7
- package/src/tools/health.ts +0 -29
- package/src/tools/index.ts +2 -5
- package/src/tools/repair.ts +4 -38
- package/src/tools/workflow.ts +1 -17
- package/src/state/repo-lock.ts +0 -706
- package/src/state/workflow-repo-lock.ts +0 -111
- package/src/tools/lock.ts +0 -75
package/src/state/repo-lock.ts
DELETED
|
@@ -1,706 +0,0 @@
|
|
|
1
|
-
// src/state/repo-lock.ts
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import crypto from "node:crypto";
|
|
5
|
-
|
|
6
|
-
const LOCK_VERSION = 2;
|
|
7
|
-
|
|
8
|
-
// Process-stable identifier for this Node process instance.
|
|
9
|
-
const PROCESS_INSTANCE_ID = crypto.randomUUID();
|
|
10
|
-
|
|
11
|
-
// Hard guardrails against garbage/corruption.
|
|
12
|
-
const MAX_LOCK_BYTES = 64 * 1024; // 64KB; lock file should be tiny.
|
|
13
|
-
|
|
14
|
-
// How many times we’ll attempt "atomic-ish replace" before giving up.
|
|
15
|
-
const ATOMIC_REPLACE_RETRIES = 3;
|
|
16
|
-
|
|
17
|
-
type LockFile = {
|
|
18
|
-
v: number;
|
|
19
|
-
|
|
20
|
-
pid: number;
|
|
21
|
-
created_at: string;
|
|
22
|
-
updated_at: string;
|
|
23
|
-
repo_root: string;
|
|
24
|
-
|
|
25
|
-
// Identifies the running process instance (process-stable).
|
|
26
|
-
instance_id: string;
|
|
27
|
-
|
|
28
|
-
// Logical session owner (propagated by opencode).
|
|
29
|
-
session_id?: string;
|
|
30
|
-
|
|
31
|
-
// Fencing token: changes every successful acquire.
|
|
32
|
-
// Prevents ABA release deleting someone else’s lock.
|
|
33
|
-
lease_id: string;
|
|
34
|
-
|
|
35
|
-
owner?: string; // optional human-readable owner
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
function nowISO(): string {
|
|
39
|
-
return new Date().toISOString();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function sleep(ms: number) {
|
|
43
|
-
return new Promise((r) => setTimeout(r, ms));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* PID existence check:
|
|
48
|
-
* - EPERM => process exists but we can't signal it (treat as alive)
|
|
49
|
-
* - ESRCH => process does not exist (dead)
|
|
50
|
-
*/
|
|
51
|
-
function isPidAlive(pid: number): boolean {
|
|
52
|
-
try {
|
|
53
|
-
(process as any).kill(pid, 0);
|
|
54
|
-
return true;
|
|
55
|
-
} catch (err: any) {
|
|
56
|
-
const code = err?.code;
|
|
57
|
-
if (code === "EPERM") return true;
|
|
58
|
-
if (code === "ESRCH") return false;
|
|
59
|
-
// Unknown: conservative = don't evict.
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function parseISOToMs(iso: string): number | null {
|
|
65
|
-
const t = Date.parse(iso);
|
|
66
|
-
if (Number.isNaN(t)) return null;
|
|
67
|
-
return t;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function isStaleByAge(existing: LockFile, staleMs: number): boolean {
|
|
71
|
-
const updatedMs = parseISOToMs(existing.updated_at);
|
|
72
|
-
if (updatedMs === null) return true;
|
|
73
|
-
return Date.now() - updatedMs > staleMs;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function safeUnlink(p: string) {
|
|
77
|
-
try {
|
|
78
|
-
fs.unlinkSync(p);
|
|
79
|
-
} catch {
|
|
80
|
-
// ignore
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Reads & validates lock file defensively.
|
|
86
|
-
* Supports both v2 JSON format and legacy PID-only format for compatibility.
|
|
87
|
-
* Returns null on any parse/validation failure.
|
|
88
|
-
*/
|
|
89
|
-
function readLock(lockPath: string): LockFile | null {
|
|
90
|
-
try {
|
|
91
|
-
const st = fs.statSync(lockPath);
|
|
92
|
-
if (!st.isFile()) return null;
|
|
93
|
-
if (st.size <= 0 || st.size > MAX_LOCK_BYTES) return null;
|
|
94
|
-
|
|
95
|
-
const raw = fs.readFileSync(lockPath, "utf8").trim();
|
|
96
|
-
|
|
97
|
-
// Try v2 JSON first
|
|
98
|
-
try {
|
|
99
|
-
const parsed = JSON.parse(raw) as LockFile;
|
|
100
|
-
if (parsed && typeof parsed === "object" && parsed.v === LOCK_VERSION) {
|
|
101
|
-
if (typeof parsed.pid !== "number") return null;
|
|
102
|
-
if (typeof parsed.created_at !== "string") return null;
|
|
103
|
-
if (typeof parsed.updated_at !== "string") return null;
|
|
104
|
-
if (typeof parsed.repo_root !== "string") return null;
|
|
105
|
-
if (typeof parsed.instance_id !== "string") return null;
|
|
106
|
-
if (typeof parsed.lease_id !== "string") return null;
|
|
107
|
-
|
|
108
|
-
if (parsed.session_id !== undefined && typeof parsed.session_id !== "string") return null;
|
|
109
|
-
if (parsed.owner !== undefined && typeof parsed.owner !== "string") return null;
|
|
110
|
-
|
|
111
|
-
return parsed;
|
|
112
|
-
}
|
|
113
|
-
} catch {
|
|
114
|
-
// Not JSON, try legacy format
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Legacy format: just PID as number string
|
|
118
|
-
const legacyPid = parseInt(raw, 10);
|
|
119
|
-
if (Number.isNaN(legacyPid) || legacyPid <= 0) return null;
|
|
120
|
-
|
|
121
|
-
// Convert legacy to v2 format
|
|
122
|
-
const now = nowISO();
|
|
123
|
-
const leaseId = crypto.randomUUID();
|
|
124
|
-
return {
|
|
125
|
-
v: LOCK_VERSION,
|
|
126
|
-
pid: legacyPid,
|
|
127
|
-
created_at: now, // Approximate
|
|
128
|
-
updated_at: now,
|
|
129
|
-
repo_root: "", // Unknown, will be filled by caller
|
|
130
|
-
instance_id: PROCESS_INSTANCE_ID, // Assume same instance
|
|
131
|
-
session_id: undefined,
|
|
132
|
-
lease_id: leaseId,
|
|
133
|
-
owner: "legacy-lock",
|
|
134
|
-
};
|
|
135
|
-
} catch {
|
|
136
|
-
return null;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Best-effort directory fsync:
|
|
142
|
-
* Helps durability on crash for some filesystems (mostly POSIX).
|
|
143
|
-
* On platforms where opening a directory fails, we ignore.
|
|
144
|
-
*/
|
|
145
|
-
function fsyncDirBestEffort(dirPath: string) {
|
|
146
|
-
try {
|
|
147
|
-
const fd = fs.openSync(dirPath, "r");
|
|
148
|
-
try {
|
|
149
|
-
fs.fsyncSync(fd);
|
|
150
|
-
} finally {
|
|
151
|
-
fs.closeSync(fd);
|
|
152
|
-
}
|
|
153
|
-
} catch {
|
|
154
|
-
// ignore (not portable)
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* "Atomic-ish" replace:
|
|
160
|
-
* - Write temp file
|
|
161
|
-
* - Try rename over target (POSIX generally atomic)
|
|
162
|
-
* - Windows can fail if target exists/locked; fallback to unlink+rename (not atomic, but best-effort)
|
|
163
|
-
* - Best-effort directory fsync after rename
|
|
164
|
-
*/
|
|
165
|
-
function writeLockAtomicish(lockPath: string, lock: LockFile) {
|
|
166
|
-
const dir = path.dirname(lockPath);
|
|
167
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
168
|
-
|
|
169
|
-
const tmp = `${lockPath}.${(process as any).pid}.${Date.now()}.${crypto.randomUUID()}.tmp`;
|
|
170
|
-
const body = JSON.stringify(lock); // compact JSON to reduce IO
|
|
171
|
-
|
|
172
|
-
fs.writeFileSync(tmp, body, "utf8");
|
|
173
|
-
|
|
174
|
-
let lastErr: any = null;
|
|
175
|
-
for (let i = 0; i < ATOMIC_REPLACE_RETRIES; i++) {
|
|
176
|
-
try {
|
|
177
|
-
fs.renameSync(tmp, lockPath);
|
|
178
|
-
fsyncDirBestEffort(dir);
|
|
179
|
-
return;
|
|
180
|
-
} catch (err: any) {
|
|
181
|
-
lastErr = err;
|
|
182
|
-
const code = err?.code;
|
|
183
|
-
|
|
184
|
-
// Common Windows-ish cases where rename over existing fails.
|
|
185
|
-
if (code === "EEXIST" || code === "EPERM" || code === "ENOTEMPTY") {
|
|
186
|
-
safeUnlink(lockPath);
|
|
187
|
-
continue;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// If tmp vanished somehow, stop.
|
|
191
|
-
if (code === "ENOENT") break;
|
|
192
|
-
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
safeUnlink(tmp);
|
|
198
|
-
if (lastErr) throw lastErr;
|
|
199
|
-
throw new Error(`Failed to replace lock file: ${lockPath}`);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Atomic "create if not exists" using exclusive open.
|
|
204
|
-
*/
|
|
205
|
-
function tryCreateExclusiveFile(filePath: string, contentsUtf8: string): boolean {
|
|
206
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
207
|
-
|
|
208
|
-
try {
|
|
209
|
-
const fd = fs.openSync(filePath, "wx");
|
|
210
|
-
try {
|
|
211
|
-
fs.writeFileSync(fd, contentsUtf8, "utf8");
|
|
212
|
-
fs.fsyncSync(fd);
|
|
213
|
-
} finally {
|
|
214
|
-
fs.closeSync(fd);
|
|
215
|
-
}
|
|
216
|
-
fsyncDirBestEffort(path.dirname(filePath));
|
|
217
|
-
return true;
|
|
218
|
-
} catch (err: any) {
|
|
219
|
-
if (err?.code === "EEXIST") return false;
|
|
220
|
-
throw err;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function tryCreateRepoLockExclusive(lockPath: string, lock: LockFile): boolean {
|
|
225
|
-
return tryCreateExclusiveFile(lockPath, JSON.stringify(lock));
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* In-process lock cache:
|
|
230
|
-
* Prevents repeated acquire/release cycles during tool-call storms.
|
|
231
|
-
*/
|
|
232
|
-
type CachedHandle = {
|
|
233
|
-
key: string;
|
|
234
|
-
lockPath: string;
|
|
235
|
-
sessionId?: string;
|
|
236
|
-
leaseId: string;
|
|
237
|
-
refCount: number;
|
|
238
|
-
heartbeatStop: () => void;
|
|
239
|
-
releaseOnce: () => void;
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
const ACTIVE_LOCKS = new Map<string, CachedHandle>();
|
|
243
|
-
|
|
244
|
-
function cacheKey(lockPath: string, sessionId?: string): string {
|
|
245
|
-
return `${lockPath}::${sessionId ?? ""}`;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Heartbeat loop:
|
|
250
|
-
* - setTimeout (not setInterval) to avoid backlog drift under load
|
|
251
|
-
* - Minimizes writes by enforcing minWriteMs
|
|
252
|
-
* - ABA-safe: only refreshes if lock matches our lease_id and process identity
|
|
253
|
-
* - Avoids unnecessary writes if lock already has a recent updated_at
|
|
254
|
-
*/
|
|
255
|
-
function startHeartbeat(opts: {
|
|
256
|
-
lockPath: string;
|
|
257
|
-
repoRoot: string;
|
|
258
|
-
sessionId?: string;
|
|
259
|
-
owner?: string;
|
|
260
|
-
leaseId: string;
|
|
261
|
-
heartbeatMs: number;
|
|
262
|
-
minWriteMs: number;
|
|
263
|
-
}): () => void {
|
|
264
|
-
let stopped = false;
|
|
265
|
-
let lastWriteAt = 0;
|
|
266
|
-
let timer: NodeJS.Timeout | null = null;
|
|
267
|
-
|
|
268
|
-
const tick = () => {
|
|
269
|
-
if (stopped) return;
|
|
270
|
-
|
|
271
|
-
const now = Date.now();
|
|
272
|
-
const shouldAttempt = now - lastWriteAt >= opts.minWriteMs;
|
|
273
|
-
|
|
274
|
-
if (shouldAttempt) {
|
|
275
|
-
try {
|
|
276
|
-
const existing = readLock(opts.lockPath);
|
|
277
|
-
|
|
278
|
-
if (
|
|
279
|
-
existing &&
|
|
280
|
-
existing.lease_id === opts.leaseId &&
|
|
281
|
-
existing.pid === (process as any).pid &&
|
|
282
|
-
existing.instance_id === PROCESS_INSTANCE_ID
|
|
283
|
-
) {
|
|
284
|
-
const updatedMs = parseISOToMs(existing.updated_at);
|
|
285
|
-
const isFresh = updatedMs !== null && now - updatedMs < opts.minWriteMs;
|
|
286
|
-
|
|
287
|
-
if (!isFresh) {
|
|
288
|
-
writeLockAtomicish(opts.lockPath, {
|
|
289
|
-
...existing,
|
|
290
|
-
updated_at: nowISO(),
|
|
291
|
-
repo_root: opts.repoRoot,
|
|
292
|
-
session_id: opts.sessionId ?? existing.session_id,
|
|
293
|
-
owner: opts.owner ?? existing.owner,
|
|
294
|
-
});
|
|
295
|
-
lastWriteAt = now;
|
|
296
|
-
} else {
|
|
297
|
-
lastWriteAt = now;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
} catch (err) {
|
|
301
|
-
// Heartbeat write failed - don't propagate, just reschedule
|
|
302
|
-
// Lock will become stale if heartbeat continues failing
|
|
303
|
-
// eslint-disable-next-line no-console
|
|
304
|
-
console.warn("[Astrocode] Heartbeat write error:", err);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
timer = setTimeout(tick, opts.heartbeatMs);
|
|
309
|
-
(timer as any).unref?.();
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
tick();
|
|
313
|
-
|
|
314
|
-
return () => {
|
|
315
|
-
stopped = true;
|
|
316
|
-
if (timer) clearTimeout(timer);
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Shutdown cleanup:
|
|
322
|
-
* Best-effort release on normal termination signals.
|
|
323
|
-
*/
|
|
324
|
-
let EXIT_HOOK_INSTALLED = false;
|
|
325
|
-
function installExitHookOnce() {
|
|
326
|
-
if (EXIT_HOOK_INSTALLED) return;
|
|
327
|
-
EXIT_HOOK_INSTALLED = true;
|
|
328
|
-
|
|
329
|
-
const cleanup = () => {
|
|
330
|
-
for (const [key, h] of ACTIVE_LOCKS.entries()) {
|
|
331
|
-
try {
|
|
332
|
-
ACTIVE_LOCKS.delete(key);
|
|
333
|
-
h.heartbeatStop();
|
|
334
|
-
h.releaseOnce();
|
|
335
|
-
} catch {
|
|
336
|
-
// ignore
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
};
|
|
340
|
-
|
|
341
|
-
(process as any).once("exit", cleanup);
|
|
342
|
-
(process as any).once("SIGINT", () => {
|
|
343
|
-
cleanup();
|
|
344
|
-
(process as any).exit(130);
|
|
345
|
-
});
|
|
346
|
-
(process as any).once("SIGTERM", () => {
|
|
347
|
-
cleanup();
|
|
348
|
-
(process as any).exit(143);
|
|
349
|
-
});
|
|
350
|
-
(process as any).once("uncaughtException", (err: any) => {
|
|
351
|
-
// eslint-disable-next-line no-console
|
|
352
|
-
console.error("[Astrocode] Uncaught Exception, cleaning up locks:", err);
|
|
353
|
-
cleanup();
|
|
354
|
-
(process as any).exit(1);
|
|
355
|
-
});
|
|
356
|
-
(process as any).once("unhandledRejection", (reason: any) => {
|
|
357
|
-
// eslint-disable-next-line no-console
|
|
358
|
-
console.error("[Astrocode] Unhandled Rejection, cleaning up locks:", reason);
|
|
359
|
-
cleanup();
|
|
360
|
-
(process as any).exit(1);
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* Acquire a repo-scoped lock with:
|
|
366
|
-
* - ✅ process-local caching + refcount (efficient repeated tool calls)
|
|
367
|
-
* - ✅ heartbeat lease + stale recovery
|
|
368
|
-
* - ✅ atomic create (`wx`) + portable replace fallback
|
|
369
|
-
* - ✅ dead PID eviction + stale eviction
|
|
370
|
-
* - ✅ no live takeover (even same session) to avoid concurrency stomps
|
|
371
|
-
* - ✅ ABA-safe release via lease_id fencing
|
|
372
|
-
* - ✅ exponential backoff + jitter to reduce FS churn
|
|
373
|
-
*/
|
|
374
|
-
export async function acquireRepoLock(opts: {
|
|
375
|
-
lockPath: string;
|
|
376
|
-
repoRoot: string;
|
|
377
|
-
sessionId?: string;
|
|
378
|
-
owner?: string;
|
|
379
|
-
|
|
380
|
-
retryMs?: number; // default 8000
|
|
381
|
-
pollMs?: number; // default 20
|
|
382
|
-
pollMaxMs?: number; // default 250
|
|
383
|
-
staleMs?: number; // default 2 minutes
|
|
384
|
-
heartbeatMs?: number; // default 200
|
|
385
|
-
minWriteMs?: number; // default 800
|
|
386
|
-
}): Promise<{ release: () => void }> {
|
|
387
|
-
installExitHookOnce();
|
|
388
|
-
|
|
389
|
-
const { lockPath, repoRoot, sessionId, owner } = opts;
|
|
390
|
-
|
|
391
|
-
const retryMs = opts.retryMs ?? 8000;
|
|
392
|
-
const pollBaseMs = opts.pollMs ?? 20;
|
|
393
|
-
const pollMaxMs = opts.pollMaxMs ?? 250;
|
|
394
|
-
|
|
395
|
-
const heartbeatMs = opts.heartbeatMs ?? 200;
|
|
396
|
-
const minWriteMs = opts.minWriteMs ?? 800;
|
|
397
|
-
|
|
398
|
-
// Ensure stale is comfortably above minWriteMs to prevent false-stale under load.
|
|
399
|
-
const staleMs = Math.max(opts.staleMs ?? 2 * 60 * 1000, minWriteMs * 8);
|
|
400
|
-
|
|
401
|
-
// ✅ Fast path: reuse cached handle in the same process/session.
|
|
402
|
-
const key = cacheKey(lockPath, sessionId);
|
|
403
|
-
const cached = ACTIVE_LOCKS.get(key);
|
|
404
|
-
if (cached) {
|
|
405
|
-
cached.refCount += 1;
|
|
406
|
-
return {
|
|
407
|
-
release: () => {
|
|
408
|
-
cached.refCount -= 1;
|
|
409
|
-
if (cached.refCount <= 0) {
|
|
410
|
-
ACTIVE_LOCKS.delete(key);
|
|
411
|
-
cached.heartbeatStop();
|
|
412
|
-
cached.releaseOnce();
|
|
413
|
-
}
|
|
414
|
-
},
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
const myPid = ((process as any).pid as number);
|
|
419
|
-
const startedAt = Date.now();
|
|
420
|
-
let pollMs = pollBaseMs;
|
|
421
|
-
|
|
422
|
-
while (true) {
|
|
423
|
-
const existing = readLock(lockPath);
|
|
424
|
-
|
|
425
|
-
// No lock (or unreadable/invalid) -> try create.
|
|
426
|
-
if (!existing) {
|
|
427
|
-
const now = nowISO();
|
|
428
|
-
const leaseId = crypto.randomUUID();
|
|
429
|
-
|
|
430
|
-
const candidate: LockFile = {
|
|
431
|
-
v: LOCK_VERSION,
|
|
432
|
-
pid: myPid,
|
|
433
|
-
created_at: now,
|
|
434
|
-
updated_at: now,
|
|
435
|
-
repo_root: repoRoot,
|
|
436
|
-
instance_id: PROCESS_INSTANCE_ID,
|
|
437
|
-
session_id: sessionId,
|
|
438
|
-
lease_id: leaseId,
|
|
439
|
-
owner,
|
|
440
|
-
};
|
|
441
|
-
|
|
442
|
-
const created = tryCreateRepoLockExclusive(lockPath, candidate);
|
|
443
|
-
if (created) {
|
|
444
|
-
const heartbeatStop = startHeartbeat({
|
|
445
|
-
lockPath,
|
|
446
|
-
repoRoot,
|
|
447
|
-
sessionId,
|
|
448
|
-
owner,
|
|
449
|
-
leaseId,
|
|
450
|
-
heartbeatMs,
|
|
451
|
-
minWriteMs,
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
const releaseOnce = () => {
|
|
455
|
-
const cur = readLock(lockPath);
|
|
456
|
-
if (!cur) return;
|
|
457
|
-
|
|
458
|
-
// ABA-safe
|
|
459
|
-
if (cur.lease_id !== leaseId) return;
|
|
460
|
-
|
|
461
|
-
// Strict identity: only exact process instance can delete.
|
|
462
|
-
if (cur.pid !== myPid) return;
|
|
463
|
-
if (cur.instance_id !== PROCESS_INSTANCE_ID) return;
|
|
464
|
-
|
|
465
|
-
safeUnlink(lockPath);
|
|
466
|
-
fsyncDirBestEffort(path.dirname(lockPath));
|
|
467
|
-
};
|
|
468
|
-
|
|
469
|
-
const handle: CachedHandle = {
|
|
470
|
-
key,
|
|
471
|
-
lockPath,
|
|
472
|
-
sessionId,
|
|
473
|
-
leaseId,
|
|
474
|
-
refCount: 1,
|
|
475
|
-
heartbeatStop,
|
|
476
|
-
releaseOnce,
|
|
477
|
-
};
|
|
478
|
-
ACTIVE_LOCKS.set(key, handle);
|
|
479
|
-
|
|
480
|
-
return {
|
|
481
|
-
release: () => {
|
|
482
|
-
const h = ACTIVE_LOCKS.get(key);
|
|
483
|
-
if (!h) return;
|
|
484
|
-
h.refCount -= 1;
|
|
485
|
-
if (h.refCount <= 0) {
|
|
486
|
-
ACTIVE_LOCKS.delete(key);
|
|
487
|
-
h.heartbeatStop();
|
|
488
|
-
h.releaseOnce();
|
|
489
|
-
}
|
|
490
|
-
},
|
|
491
|
-
};
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
// Race lost; reset backoff and loop.
|
|
495
|
-
pollMs = pollBaseMs;
|
|
496
|
-
continue;
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
// Re-entrant by SAME PROCESS IDENTITY (pid+instance), or legacy lock with same PID.
|
|
500
|
-
if (existing.pid === myPid && (existing.instance_id === PROCESS_INSTANCE_ID || existing.owner === "legacy-lock")) {
|
|
501
|
-
const leaseId = crypto.randomUUID();
|
|
502
|
-
|
|
503
|
-
writeLockAtomicish(lockPath, {
|
|
504
|
-
...existing,
|
|
505
|
-
v: LOCK_VERSION,
|
|
506
|
-
updated_at: nowISO(),
|
|
507
|
-
repo_root: repoRoot,
|
|
508
|
-
instance_id: PROCESS_INSTANCE_ID, // Upgrade legacy
|
|
509
|
-
session_id: sessionId ?? existing.session_id,
|
|
510
|
-
owner: owner ?? existing.owner,
|
|
511
|
-
lease_id: leaseId,
|
|
512
|
-
});
|
|
513
|
-
|
|
514
|
-
const heartbeatStop = startHeartbeat({
|
|
515
|
-
lockPath,
|
|
516
|
-
repoRoot,
|
|
517
|
-
sessionId: sessionId ?? existing.session_id,
|
|
518
|
-
owner: owner ?? existing.owner,
|
|
519
|
-
leaseId,
|
|
520
|
-
heartbeatMs,
|
|
521
|
-
minWriteMs,
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
const releaseOnce = () => {
|
|
525
|
-
const cur = readLock(lockPath);
|
|
526
|
-
if (!cur) return;
|
|
527
|
-
if (cur.lease_id !== leaseId) return;
|
|
528
|
-
if (cur.pid !== myPid) return;
|
|
529
|
-
if (cur.instance_id !== PROCESS_INSTANCE_ID) return;
|
|
530
|
-
safeUnlink(lockPath);
|
|
531
|
-
fsyncDirBestEffort(path.dirname(lockPath));
|
|
532
|
-
};
|
|
533
|
-
|
|
534
|
-
const handle: CachedHandle = {
|
|
535
|
-
key,
|
|
536
|
-
lockPath,
|
|
537
|
-
sessionId,
|
|
538
|
-
leaseId,
|
|
539
|
-
refCount: 1,
|
|
540
|
-
heartbeatStop,
|
|
541
|
-
releaseOnce,
|
|
542
|
-
};
|
|
543
|
-
ACTIVE_LOCKS.set(key, handle);
|
|
544
|
-
|
|
545
|
-
return {
|
|
546
|
-
release: () => {
|
|
547
|
-
const h = ACTIVE_LOCKS.get(key);
|
|
548
|
-
if (!h) return;
|
|
549
|
-
h.refCount -= 1;
|
|
550
|
-
if (h.refCount <= 0) {
|
|
551
|
-
ACTIVE_LOCKS.delete(key);
|
|
552
|
-
h.heartbeatStop();
|
|
553
|
-
h.releaseOnce();
|
|
554
|
-
}
|
|
555
|
-
},
|
|
556
|
-
};
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
// 🚫 No live takeover (even same session).
|
|
560
|
-
// We only evict dead/stale locks.
|
|
561
|
-
|
|
562
|
-
const pidAlive = isPidAlive(existing.pid);
|
|
563
|
-
const staleByAge = isStaleByAge(existing, staleMs);
|
|
564
|
-
|
|
565
|
-
if (!pidAlive || staleByAge) {
|
|
566
|
-
safeUnlink(lockPath);
|
|
567
|
-
fsyncDirBestEffort(path.dirname(lockPath));
|
|
568
|
-
pollMs = pollBaseMs;
|
|
569
|
-
continue;
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
// Alive and not us -> bounded wait with exponential backoff + jitter.
|
|
573
|
-
if (Date.now() - startedAt > retryMs) {
|
|
574
|
-
const ownerBits = [
|
|
575
|
-
`pid=${existing.pid}`,
|
|
576
|
-
existing.session_id ? `session=${existing.session_id}` : null,
|
|
577
|
-
existing.owner ? `owner=${existing.owner}` : null,
|
|
578
|
-
`updated_at=${existing.updated_at}`,
|
|
579
|
-
sessionId && existing.session_id === sessionId ? `(same-session waiting)` : null,
|
|
580
|
-
]
|
|
581
|
-
.filter(Boolean)
|
|
582
|
-
.join(" ");
|
|
583
|
-
|
|
584
|
-
throw new Error(
|
|
585
|
-
`Astrocode lock is already held (${lockPath}). ${ownerBits}. ` +
|
|
586
|
-
`Close other opencode processes or wait.`
|
|
587
|
-
);
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
const jitter = Math.floor(Math.random() * Math.min(12, pollMs));
|
|
591
|
-
await sleep(pollMs + jitter);
|
|
592
|
-
pollMs = Math.min(pollMaxMs, Math.floor(pollMs * 1.35));
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
/**
|
|
597
|
-
* Helper wrapper: always releases lock.
|
|
598
|
-
*/
|
|
599
|
-
export async function withRepoLock<T>(opts: {
|
|
600
|
-
lockPath: string;
|
|
601
|
-
repoRoot: string;
|
|
602
|
-
sessionId?: string;
|
|
603
|
-
owner?: string;
|
|
604
|
-
fn: () => Promise<T>;
|
|
605
|
-
}): Promise<T> {
|
|
606
|
-
const handle = await acquireRepoLock({
|
|
607
|
-
lockPath: opts.lockPath,
|
|
608
|
-
repoRoot: opts.repoRoot,
|
|
609
|
-
sessionId: opts.sessionId,
|
|
610
|
-
owner: opts.owner,
|
|
611
|
-
});
|
|
612
|
-
|
|
613
|
-
try {
|
|
614
|
-
return await opts.fn();
|
|
615
|
-
} finally {
|
|
616
|
-
handle.release();
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
/**
|
|
621
|
-
* Lock diagnostics and status information.
|
|
622
|
-
*/
|
|
623
|
-
export type LockStatus = {
|
|
624
|
-
exists: boolean;
|
|
625
|
-
path: string;
|
|
626
|
-
pid?: number;
|
|
627
|
-
pidAlive?: boolean;
|
|
628
|
-
instanceId?: string;
|
|
629
|
-
sessionId?: string;
|
|
630
|
-
owner?: string;
|
|
631
|
-
leaseId?: string;
|
|
632
|
-
createdAt?: string;
|
|
633
|
-
updatedAt?: string;
|
|
634
|
-
ageMs?: number;
|
|
635
|
-
isStale?: boolean;
|
|
636
|
-
repoRoot?: string;
|
|
637
|
-
version?: number;
|
|
638
|
-
};
|
|
639
|
-
|
|
640
|
-
/**
|
|
641
|
-
* Get lock file status and diagnostics.
|
|
642
|
-
* Returns detailed information about the current lock state.
|
|
643
|
-
*/
|
|
644
|
-
export function getLockStatus(lockPath: string, staleMs: number = 30_000): LockStatus {
|
|
645
|
-
const existing = readLock(lockPath);
|
|
646
|
-
|
|
647
|
-
if (!existing) {
|
|
648
|
-
return {
|
|
649
|
-
exists: false,
|
|
650
|
-
path: lockPath,
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
const updatedMs = parseISOToMs(existing.updated_at);
|
|
655
|
-
const ageMs = updatedMs !== null ? Date.now() - updatedMs : undefined;
|
|
656
|
-
const pidAlive = isPidAlive(existing.pid);
|
|
657
|
-
const isStale = isStaleByAge(existing, staleMs);
|
|
658
|
-
|
|
659
|
-
return {
|
|
660
|
-
exists: true,
|
|
661
|
-
path: lockPath,
|
|
662
|
-
pid: existing.pid,
|
|
663
|
-
pidAlive,
|
|
664
|
-
instanceId: existing.instance_id,
|
|
665
|
-
sessionId: existing.session_id,
|
|
666
|
-
owner: existing.owner,
|
|
667
|
-
leaseId: existing.lease_id,
|
|
668
|
-
createdAt: existing.created_at,
|
|
669
|
-
updatedAt: existing.updated_at,
|
|
670
|
-
ageMs,
|
|
671
|
-
isStale,
|
|
672
|
-
repoRoot: existing.repo_root,
|
|
673
|
-
version: existing.v,
|
|
674
|
-
};
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
/**
|
|
678
|
-
* Attempt to remove a lock file if it's safe to do so.
|
|
679
|
-
* Only removes locks with dead PIDs or stale timestamps.
|
|
680
|
-
* Returns true if lock was removed, false if lock is still held.
|
|
681
|
-
*/
|
|
682
|
-
export function tryRemoveStaleLock(lockPath: string, staleMs: number = 30_000): { removed: boolean; reason: string } {
|
|
683
|
-
const existing = readLock(lockPath);
|
|
684
|
-
|
|
685
|
-
if (!existing) {
|
|
686
|
-
return { removed: false, reason: "No lock file found" };
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
const pidAlive = isPidAlive(existing.pid);
|
|
690
|
-
const isStale = isStaleByAge(existing, staleMs);
|
|
691
|
-
|
|
692
|
-
if (!pidAlive) {
|
|
693
|
-
safeUnlink(lockPath);
|
|
694
|
-
fsyncDirBestEffort(path.dirname(lockPath));
|
|
695
|
-
return { removed: true, reason: `Dead PID ${existing.pid}` };
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
if (isStale) {
|
|
699
|
-
safeUnlink(lockPath);
|
|
700
|
-
fsyncDirBestEffort(path.dirname(lockPath));
|
|
701
|
-
const ageSeconds = Math.floor((Date.now() - (parseISOToMs(existing.updated_at) ?? 0)) / 1000);
|
|
702
|
-
return { removed: true, reason: `Stale lock (${ageSeconds}s old, threshold ${staleMs / 1000}s)` };
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
return { removed: false, reason: `Lock is active (PID ${existing.pid} alive, age ${Math.floor((Date.now() - (parseISOToMs(existing.updated_at) ?? 0)) / 1000)}s)` };
|
|
706
|
-
}
|