astrocode-workflow 0.3.1 → 0.3.3

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 (136) hide show
  1. package/dist/index.js +6 -0
  2. package/dist/shared/metrics.d.ts +66 -0
  3. package/dist/shared/metrics.js +112 -0
  4. package/dist/src/agents/commands.d.ts +9 -0
  5. package/dist/src/agents/commands.js +121 -0
  6. package/dist/src/agents/prompts.d.ts +3 -0
  7. package/dist/src/agents/prompts.js +232 -0
  8. package/dist/src/agents/registry.d.ts +6 -0
  9. package/dist/src/agents/registry.js +242 -0
  10. package/dist/src/agents/types.d.ts +14 -0
  11. package/dist/src/agents/types.js +8 -0
  12. package/dist/src/config/config-handler.d.ts +4 -0
  13. package/dist/src/config/config-handler.js +46 -0
  14. package/dist/src/config/defaults.d.ts +3 -0
  15. package/dist/src/config/defaults.js +3 -0
  16. package/dist/src/config/loader.d.ts +11 -0
  17. package/dist/src/config/loader.js +82 -0
  18. package/dist/src/config/schema.d.ts +194 -0
  19. package/dist/src/config/schema.js +223 -0
  20. package/dist/src/hooks/continuation-enforcer.d.ts +34 -0
  21. package/dist/src/hooks/continuation-enforcer.js +190 -0
  22. package/dist/src/hooks/inject-provider.d.ts +22 -0
  23. package/dist/src/hooks/inject-provider.js +120 -0
  24. package/dist/src/hooks/tool-output-truncator.d.ts +25 -0
  25. package/dist/src/hooks/tool-output-truncator.js +57 -0
  26. package/dist/src/index.d.ts +3 -0
  27. package/dist/src/index.js +308 -0
  28. package/dist/src/shared/deep-merge.d.ts +8 -0
  29. package/dist/src/shared/deep-merge.js +25 -0
  30. package/dist/src/shared/hash.d.ts +1 -0
  31. package/dist/src/shared/hash.js +4 -0
  32. package/dist/src/shared/log.d.ts +7 -0
  33. package/dist/src/shared/log.js +24 -0
  34. package/dist/src/shared/metrics.d.ts +66 -0
  35. package/dist/src/shared/metrics.js +112 -0
  36. package/dist/src/shared/model-tuning.d.ts +9 -0
  37. package/dist/src/shared/model-tuning.js +28 -0
  38. package/dist/src/shared/paths.d.ts +19 -0
  39. package/dist/src/shared/paths.js +64 -0
  40. package/dist/src/shared/text.d.ts +4 -0
  41. package/dist/src/shared/text.js +19 -0
  42. package/dist/src/shared/time.d.ts +1 -0
  43. package/dist/src/shared/time.js +3 -0
  44. package/dist/src/state/adapters/index.d.ts +41 -0
  45. package/dist/src/state/adapters/index.js +115 -0
  46. package/dist/src/state/db.d.ts +16 -0
  47. package/dist/src/state/db.js +225 -0
  48. package/dist/src/state/ids.d.ts +8 -0
  49. package/dist/src/state/ids.js +25 -0
  50. package/dist/src/state/repo-lock.d.ts +3 -0
  51. package/dist/src/state/repo-lock.js +29 -0
  52. package/dist/src/state/schema.d.ts +2 -0
  53. package/dist/src/state/schema.js +251 -0
  54. package/dist/src/state/types.d.ts +71 -0
  55. package/dist/src/state/types.js +1 -0
  56. package/dist/src/tools/artifacts.d.ts +18 -0
  57. package/dist/src/tools/artifacts.js +71 -0
  58. package/dist/src/tools/health.d.ts +8 -0
  59. package/dist/src/tools/health.js +119 -0
  60. package/dist/src/tools/index.d.ts +20 -0
  61. package/dist/src/tools/index.js +94 -0
  62. package/dist/src/tools/init.d.ts +17 -0
  63. package/dist/src/tools/init.js +96 -0
  64. package/dist/src/tools/injects.d.ts +53 -0
  65. package/dist/src/tools/injects.js +325 -0
  66. package/dist/src/tools/metrics.d.ts +7 -0
  67. package/dist/src/tools/metrics.js +61 -0
  68. package/dist/src/tools/repair.d.ts +8 -0
  69. package/dist/src/tools/repair.js +25 -0
  70. package/dist/src/tools/reset.d.ts +8 -0
  71. package/dist/src/tools/reset.js +92 -0
  72. package/dist/src/tools/run.d.ts +13 -0
  73. package/dist/src/tools/run.js +54 -0
  74. package/dist/src/tools/spec.d.ts +12 -0
  75. package/dist/src/tools/spec.js +44 -0
  76. package/dist/src/tools/stage.d.ts +23 -0
  77. package/dist/src/tools/stage.js +371 -0
  78. package/dist/src/tools/status.d.ts +8 -0
  79. package/dist/src/tools/status.js +125 -0
  80. package/dist/src/tools/story.d.ts +23 -0
  81. package/dist/src/tools/story.js +85 -0
  82. package/dist/src/tools/workflow.d.ts +13 -0
  83. package/dist/src/tools/workflow.js +355 -0
  84. package/dist/src/ui/inject.d.ts +12 -0
  85. package/dist/src/ui/inject.js +107 -0
  86. package/dist/src/ui/toasts.d.ts +13 -0
  87. package/dist/src/ui/toasts.js +39 -0
  88. package/dist/src/workflow/artifacts.d.ts +24 -0
  89. package/dist/src/workflow/artifacts.js +45 -0
  90. package/dist/src/workflow/baton.d.ts +72 -0
  91. package/dist/src/workflow/baton.js +166 -0
  92. package/dist/src/workflow/context.d.ts +20 -0
  93. package/dist/src/workflow/context.js +113 -0
  94. package/dist/src/workflow/directives.d.ts +39 -0
  95. package/dist/src/workflow/directives.js +137 -0
  96. package/dist/src/workflow/repair.d.ts +8 -0
  97. package/dist/src/workflow/repair.js +99 -0
  98. package/dist/src/workflow/state-machine.d.ts +86 -0
  99. package/dist/src/workflow/state-machine.js +216 -0
  100. package/dist/src/workflow/story-helpers.d.ts +9 -0
  101. package/dist/src/workflow/story-helpers.js +13 -0
  102. package/dist/state/db.d.ts +1 -0
  103. package/dist/state/db.js +9 -0
  104. package/dist/state/repo-lock.d.ts +3 -0
  105. package/dist/state/repo-lock.js +29 -0
  106. package/dist/test/integration/db-transactions.test.d.ts +1 -0
  107. package/dist/test/integration/db-transactions.test.js +126 -0
  108. package/dist/test/integration/injection-metrics.test.d.ts +1 -0
  109. package/dist/test/integration/injection-metrics.test.js +129 -0
  110. package/dist/tools/health.d.ts +8 -0
  111. package/dist/tools/health.js +119 -0
  112. package/dist/tools/index.js +9 -0
  113. package/dist/tools/metrics.d.ts +7 -0
  114. package/dist/tools/metrics.js +61 -0
  115. package/dist/tools/reset.d.ts +8 -0
  116. package/dist/tools/reset.js +92 -0
  117. package/dist/tools/workflow.js +178 -168
  118. package/dist/ui/inject.js +21 -9
  119. package/package.json +6 -3
  120. package/src/astro/workflow-runner.ts +36 -0
  121. package/src/index.ts +8 -0
  122. package/src/shared/metrics.ts +148 -0
  123. package/src/state/db.ts +10 -1
  124. package/src/state/repo-lock.ts +574 -0
  125. package/src/state/workflow-repo-lock.ts +74 -0
  126. package/src/tools/health.ts +128 -0
  127. package/src/tools/index.ts +12 -3
  128. package/src/tools/init.ts +7 -6
  129. package/src/tools/metrics.ts +71 -0
  130. package/src/tools/repair.ts +1 -0
  131. package/src/tools/reset.ts +100 -0
  132. package/src/tools/stage.ts +1 -0
  133. package/src/tools/status.ts +2 -1
  134. package/src/tools/story.ts +1 -0
  135. package/src/tools/workflow.ts +18 -1
  136. package/src/ui/inject.ts +21 -9
@@ -0,0 +1,574 @@
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
+ * Returns null on any parse/validation failure.
87
+ */
88
+ function readLock(lockPath: string): LockFile | null {
89
+ try {
90
+ const st = fs.statSync(lockPath);
91
+ if (!st.isFile()) return null;
92
+ if (st.size <= 0 || st.size > MAX_LOCK_BYTES) return null;
93
+
94
+ const raw = fs.readFileSync(lockPath, "utf8");
95
+ const parsed = JSON.parse(raw) as LockFile;
96
+
97
+ if (!parsed) return null;
98
+ if (parsed.v !== LOCK_VERSION) return null;
99
+
100
+ if (typeof parsed.pid !== "number") return null;
101
+ if (typeof parsed.created_at !== "string") return null;
102
+ if (typeof parsed.updated_at !== "string") return null;
103
+ if (typeof parsed.repo_root !== "string") return null;
104
+ if (typeof parsed.instance_id !== "string") return null;
105
+ if (typeof parsed.lease_id !== "string") return null;
106
+
107
+ if (parsed.session_id !== undefined && typeof parsed.session_id !== "string") return null;
108
+ if (parsed.owner !== undefined && typeof parsed.owner !== "string") return null;
109
+
110
+ return parsed;
111
+ } catch {
112
+ return null;
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Best-effort directory fsync:
118
+ * Helps durability on crash for some filesystems (mostly POSIX).
119
+ * On platforms where opening a directory fails, we ignore.
120
+ */
121
+ function fsyncDirBestEffort(dirPath: string) {
122
+ try {
123
+ const fd = fs.openSync(dirPath, "r");
124
+ try {
125
+ fs.fsyncSync(fd);
126
+ } finally {
127
+ fs.closeSync(fd);
128
+ }
129
+ } catch {
130
+ // ignore (not portable)
131
+ }
132
+ }
133
+
134
+ /**
135
+ * "Atomic-ish" replace:
136
+ * - Write temp file
137
+ * - Try rename over target (POSIX generally atomic)
138
+ * - Windows can fail if target exists/locked; fallback to unlink+rename (not atomic, but best-effort)
139
+ * - Best-effort directory fsync after rename
140
+ */
141
+ function writeLockAtomicish(lockPath: string, lock: LockFile) {
142
+ const dir = path.dirname(lockPath);
143
+ fs.mkdirSync(dir, { recursive: true });
144
+
145
+ const tmp = `${lockPath}.${(process as any).pid}.${Date.now()}.${crypto.randomUUID()}.tmp`;
146
+ const body = JSON.stringify(lock); // compact JSON to reduce IO
147
+
148
+ fs.writeFileSync(tmp, body, "utf8");
149
+
150
+ let lastErr: any = null;
151
+ for (let i = 0; i < ATOMIC_REPLACE_RETRIES; i++) {
152
+ try {
153
+ fs.renameSync(tmp, lockPath);
154
+ fsyncDirBestEffort(dir);
155
+ return;
156
+ } catch (err: any) {
157
+ lastErr = err;
158
+ const code = err?.code;
159
+
160
+ // Common Windows-ish cases where rename over existing fails.
161
+ if (code === "EEXIST" || code === "EPERM" || code === "ENOTEMPTY") {
162
+ safeUnlink(lockPath);
163
+ continue;
164
+ }
165
+
166
+ // If tmp vanished somehow, stop.
167
+ if (code === "ENOENT") break;
168
+
169
+ continue;
170
+ }
171
+ }
172
+
173
+ safeUnlink(tmp);
174
+ if (lastErr) throw lastErr;
175
+ throw new Error(`Failed to replace lock file: ${lockPath}`);
176
+ }
177
+
178
+ /**
179
+ * Atomic "create if not exists" using exclusive open.
180
+ */
181
+ function tryCreateExclusiveFile(filePath: string, contentsUtf8: string): boolean {
182
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
183
+
184
+ try {
185
+ const fd = fs.openSync(filePath, "wx");
186
+ try {
187
+ fs.writeFileSync(fd, contentsUtf8, "utf8");
188
+ fs.fsyncSync(fd);
189
+ } finally {
190
+ fs.closeSync(fd);
191
+ }
192
+ fsyncDirBestEffort(path.dirname(filePath));
193
+ return true;
194
+ } catch (err: any) {
195
+ if (err?.code === "EEXIST") return false;
196
+ throw err;
197
+ }
198
+ }
199
+
200
+ function tryCreateRepoLockExclusive(lockPath: string, lock: LockFile): boolean {
201
+ return tryCreateExclusiveFile(lockPath, JSON.stringify(lock));
202
+ }
203
+
204
+ /**
205
+ * In-process lock cache:
206
+ * Prevents repeated acquire/release cycles during tool-call storms.
207
+ */
208
+ type CachedHandle = {
209
+ key: string;
210
+ lockPath: string;
211
+ sessionId?: string;
212
+ leaseId: string;
213
+ refCount: number;
214
+ heartbeatStop: () => void;
215
+ releaseOnce: () => void;
216
+ };
217
+
218
+ const ACTIVE_LOCKS = new Map<string, CachedHandle>();
219
+
220
+ function cacheKey(lockPath: string, sessionId?: string): string {
221
+ return `${lockPath}::${sessionId ?? ""}`;
222
+ }
223
+
224
+ /**
225
+ * Heartbeat loop:
226
+ * - setTimeout (not setInterval) to avoid backlog drift under load
227
+ * - Minimizes writes by enforcing minWriteMs
228
+ * - ABA-safe: only refreshes if lock matches our lease_id and process identity
229
+ * - Avoids unnecessary writes if lock already has a recent updated_at
230
+ */
231
+ function startHeartbeat(opts: {
232
+ lockPath: string;
233
+ repoRoot: string;
234
+ sessionId?: string;
235
+ owner?: string;
236
+ leaseId: string;
237
+ heartbeatMs: number;
238
+ minWriteMs: number;
239
+ }): () => void {
240
+ let stopped = false;
241
+ let lastWriteAt = 0;
242
+ let timer: NodeJS.Timeout | null = null;
243
+
244
+ const tick = () => {
245
+ if (stopped) return;
246
+
247
+ const now = Date.now();
248
+ const shouldAttempt = now - lastWriteAt >= opts.minWriteMs;
249
+
250
+ if (shouldAttempt) {
251
+ const existing = readLock(opts.lockPath);
252
+
253
+ if (
254
+ existing &&
255
+ existing.lease_id === opts.leaseId &&
256
+ existing.pid === (process as any).pid &&
257
+ existing.instance_id === PROCESS_INSTANCE_ID
258
+ ) {
259
+ const updatedMs = parseISOToMs(existing.updated_at);
260
+ const isFresh = updatedMs !== null && now - updatedMs < opts.minWriteMs;
261
+
262
+ if (!isFresh) {
263
+ writeLockAtomicish(opts.lockPath, {
264
+ ...existing,
265
+ updated_at: nowISO(),
266
+ repo_root: opts.repoRoot,
267
+ session_id: opts.sessionId ?? existing.session_id,
268
+ owner: opts.owner ?? existing.owner,
269
+ });
270
+ lastWriteAt = now;
271
+ } else {
272
+ lastWriteAt = now;
273
+ }
274
+ }
275
+ }
276
+
277
+ timer = setTimeout(tick, opts.heartbeatMs);
278
+ (timer as any).unref?.();
279
+ };
280
+
281
+ tick();
282
+
283
+ return () => {
284
+ stopped = true;
285
+ if (timer) clearTimeout(timer);
286
+ };
287
+ }
288
+
289
+ /**
290
+ * Shutdown cleanup:
291
+ * Best-effort release on normal termination signals.
292
+ */
293
+ let EXIT_HOOK_INSTALLED = false;
294
+ function installExitHookOnce() {
295
+ if (EXIT_HOOK_INSTALLED) return;
296
+ EXIT_HOOK_INSTALLED = true;
297
+
298
+ const cleanup = () => {
299
+ for (const [key, h] of ACTIVE_LOCKS.entries()) {
300
+ try {
301
+ ACTIVE_LOCKS.delete(key);
302
+ h.heartbeatStop();
303
+ h.releaseOnce();
304
+ } catch {
305
+ // ignore
306
+ }
307
+ }
308
+ };
309
+
310
+ (process as any).once("exit", cleanup);
311
+ (process as any).once("SIGINT", () => {
312
+ cleanup();
313
+ (process as any).exit(130);
314
+ });
315
+ (process as any).once("SIGTERM", () => {
316
+ cleanup();
317
+ (process as any).exit(143);
318
+ });
319
+ }
320
+
321
+ /**
322
+ * Acquire a repo-scoped lock with:
323
+ * - ✅ process-local caching + refcount (efficient repeated tool calls)
324
+ * - ✅ heartbeat lease + stale recovery
325
+ * - ✅ atomic create (`wx`) + portable replace fallback
326
+ * - ✅ dead PID eviction + stale eviction
327
+ * - ✅ no live takeover (even same session) to avoid concurrency stomps
328
+ * - ✅ ABA-safe release via lease_id fencing
329
+ * - ✅ exponential backoff + jitter to reduce FS churn
330
+ */
331
+ export async function acquireRepoLock(opts: {
332
+ lockPath: string;
333
+ repoRoot: string;
334
+ sessionId?: string;
335
+ owner?: string;
336
+
337
+ retryMs?: number; // default 8000
338
+ pollMs?: number; // default 20
339
+ pollMaxMs?: number; // default 250
340
+ staleMs?: number; // default 2 minutes
341
+ heartbeatMs?: number; // default 200
342
+ minWriteMs?: number; // default 800
343
+ }): Promise<{ release: () => void }> {
344
+ installExitHookOnce();
345
+
346
+ const { lockPath, repoRoot, sessionId, owner } = opts;
347
+
348
+ const retryMs = opts.retryMs ?? 8000;
349
+ const pollBaseMs = opts.pollMs ?? 20;
350
+ const pollMaxMs = opts.pollMaxMs ?? 250;
351
+
352
+ const heartbeatMs = opts.heartbeatMs ?? 200;
353
+ const minWriteMs = opts.minWriteMs ?? 800;
354
+
355
+ // Ensure stale is comfortably above minWriteMs to prevent false-stale under load.
356
+ const staleMs = Math.max(opts.staleMs ?? 2 * 60 * 1000, minWriteMs * 8);
357
+
358
+ // ✅ Fast path: reuse cached handle in the same process/session.
359
+ const key = cacheKey(lockPath, sessionId);
360
+ const cached = ACTIVE_LOCKS.get(key);
361
+ if (cached) {
362
+ cached.refCount += 1;
363
+ return {
364
+ release: () => {
365
+ cached.refCount -= 1;
366
+ if (cached.refCount <= 0) {
367
+ ACTIVE_LOCKS.delete(key);
368
+ cached.heartbeatStop();
369
+ cached.releaseOnce();
370
+ }
371
+ },
372
+ };
373
+ }
374
+
375
+ const myPid = ((process as any).pid as number);
376
+ const startedAt = Date.now();
377
+ let pollMs = pollBaseMs;
378
+
379
+ while (true) {
380
+ const existing = readLock(lockPath);
381
+
382
+ // No lock (or unreadable/invalid) -> try create.
383
+ if (!existing) {
384
+ const now = nowISO();
385
+ const leaseId = crypto.randomUUID();
386
+
387
+ const candidate: LockFile = {
388
+ v: LOCK_VERSION,
389
+ pid: myPid,
390
+ created_at: now,
391
+ updated_at: now,
392
+ repo_root: repoRoot,
393
+ instance_id: PROCESS_INSTANCE_ID,
394
+ session_id: sessionId,
395
+ lease_id: leaseId,
396
+ owner,
397
+ };
398
+
399
+ const created = tryCreateRepoLockExclusive(lockPath, candidate);
400
+ if (created) {
401
+ const heartbeatStop = startHeartbeat({
402
+ lockPath,
403
+ repoRoot,
404
+ sessionId,
405
+ owner,
406
+ leaseId,
407
+ heartbeatMs,
408
+ minWriteMs,
409
+ });
410
+
411
+ const releaseOnce = () => {
412
+ const cur = readLock(lockPath);
413
+ if (!cur) return;
414
+
415
+ // ABA-safe
416
+ if (cur.lease_id !== leaseId) return;
417
+
418
+ // Strict identity: only exact process instance can delete.
419
+ if (cur.pid !== myPid) return;
420
+ if (cur.instance_id !== PROCESS_INSTANCE_ID) return;
421
+
422
+ safeUnlink(lockPath);
423
+ fsyncDirBestEffort(path.dirname(lockPath));
424
+ };
425
+
426
+ const handle: CachedHandle = {
427
+ key,
428
+ lockPath,
429
+ sessionId,
430
+ leaseId,
431
+ refCount: 1,
432
+ heartbeatStop,
433
+ releaseOnce,
434
+ };
435
+ ACTIVE_LOCKS.set(key, handle);
436
+
437
+ return {
438
+ release: () => {
439
+ const h = ACTIVE_LOCKS.get(key);
440
+ if (!h) return;
441
+ h.refCount -= 1;
442
+ if (h.refCount <= 0) {
443
+ ACTIVE_LOCKS.delete(key);
444
+ h.heartbeatStop();
445
+ h.releaseOnce();
446
+ }
447
+ },
448
+ };
449
+ }
450
+
451
+ // Race lost; reset backoff and loop.
452
+ pollMs = pollBaseMs;
453
+ continue;
454
+ }
455
+
456
+ // Re-entrant by SAME PROCESS IDENTITY (pid+instance).
457
+ if (existing.pid === myPid && existing.instance_id === PROCESS_INSTANCE_ID) {
458
+ const leaseId = crypto.randomUUID();
459
+
460
+ writeLockAtomicish(lockPath, {
461
+ ...existing,
462
+ v: LOCK_VERSION,
463
+ updated_at: nowISO(),
464
+ repo_root: repoRoot,
465
+ session_id: sessionId ?? existing.session_id,
466
+ owner: owner ?? existing.owner,
467
+ lease_id: leaseId,
468
+ });
469
+
470
+ const heartbeatStop = startHeartbeat({
471
+ lockPath,
472
+ repoRoot,
473
+ sessionId: sessionId ?? existing.session_id,
474
+ owner: owner ?? existing.owner,
475
+ leaseId,
476
+ heartbeatMs,
477
+ minWriteMs,
478
+ });
479
+
480
+ const releaseOnce = () => {
481
+ const cur = readLock(lockPath);
482
+ if (!cur) return;
483
+ if (cur.lease_id !== leaseId) return;
484
+ if (cur.pid !== myPid) return;
485
+ if (cur.instance_id !== PROCESS_INSTANCE_ID) return;
486
+ safeUnlink(lockPath);
487
+ fsyncDirBestEffort(path.dirname(lockPath));
488
+ };
489
+
490
+ const handle: CachedHandle = {
491
+ key,
492
+ lockPath,
493
+ sessionId,
494
+ leaseId,
495
+ refCount: 1,
496
+ heartbeatStop,
497
+ releaseOnce,
498
+ };
499
+ ACTIVE_LOCKS.set(key, handle);
500
+
501
+ return {
502
+ release: () => {
503
+ const h = ACTIVE_LOCKS.get(key);
504
+ if (!h) return;
505
+ h.refCount -= 1;
506
+ if (h.refCount <= 0) {
507
+ ACTIVE_LOCKS.delete(key);
508
+ h.heartbeatStop();
509
+ h.releaseOnce();
510
+ }
511
+ },
512
+ };
513
+ }
514
+
515
+ // 🚫 No live takeover (even same session).
516
+ // We only evict dead/stale locks.
517
+
518
+ const pidAlive = isPidAlive(existing.pid);
519
+ const staleByAge = isStaleByAge(existing, staleMs);
520
+
521
+ if (!pidAlive || staleByAge) {
522
+ safeUnlink(lockPath);
523
+ fsyncDirBestEffort(path.dirname(lockPath));
524
+ pollMs = pollBaseMs;
525
+ continue;
526
+ }
527
+
528
+ // Alive and not us -> bounded wait with exponential backoff + jitter.
529
+ if (Date.now() - startedAt > retryMs) {
530
+ const ownerBits = [
531
+ `pid=${existing.pid}`,
532
+ existing.session_id ? `session=${existing.session_id}` : null,
533
+ existing.owner ? `owner=${existing.owner}` : null,
534
+ `updated_at=${existing.updated_at}`,
535
+ sessionId && existing.session_id === sessionId ? `(same-session waiting)` : null,
536
+ ]
537
+ .filter(Boolean)
538
+ .join(" ");
539
+
540
+ throw new Error(
541
+ `Astrocode lock is already held (${lockPath}). ${ownerBits}. ` +
542
+ `Close other opencode processes or wait.`
543
+ );
544
+ }
545
+
546
+ const jitter = Math.floor(Math.random() * Math.min(12, pollMs));
547
+ await sleep(pollMs + jitter);
548
+ pollMs = Math.min(pollMaxMs, Math.floor(pollMs * 1.35));
549
+ }
550
+ }
551
+
552
+ /**
553
+ * Helper wrapper: always releases lock.
554
+ */
555
+ export async function withRepoLock<T>(opts: {
556
+ lockPath: string;
557
+ repoRoot: string;
558
+ sessionId?: string;
559
+ owner?: string;
560
+ fn: () => Promise<T>;
561
+ }): Promise<T> {
562
+ const handle = await acquireRepoLock({
563
+ lockPath: opts.lockPath,
564
+ repoRoot: opts.repoRoot,
565
+ sessionId: opts.sessionId,
566
+ owner: opts.owner,
567
+ });
568
+
569
+ try {
570
+ return await opts.fn();
571
+ } finally {
572
+ handle.release();
573
+ }
574
+ }
@@ -0,0 +1,74 @@
1
+ // src/state/workflow-repo-lock.ts
2
+ import type { acquireRepoLock } from "./repo-lock";
3
+
4
+ type RepoLockAcquire = typeof acquireRepoLock;
5
+
6
+ type Held = {
7
+ release: () => void;
8
+ depth: number;
9
+ };
10
+
11
+ const HELD_BY_KEY = new Map<string, Held>();
12
+
13
+ function key(lockPath: string, sessionId?: string) {
14
+ return `${lockPath}::${sessionId ?? ""}`;
15
+ }
16
+
17
+ /**
18
+ * Acquire ONCE per workflow/session in this process.
19
+ * Nested calls reuse the same held lock (no reacquire, no churn).
20
+ */
21
+ export async function workflowRepoLock<T>(
22
+ deps: { acquireRepoLock: RepoLockAcquire },
23
+ opts: {
24
+ lockPath: string;
25
+ repoRoot: string;
26
+ sessionId?: string;
27
+ owner?: string;
28
+ fn: () => Promise<T>;
29
+ }
30
+ ): Promise<T> {
31
+ const k = key(opts.lockPath, opts.sessionId);
32
+ const existing = HELD_BY_KEY.get(k);
33
+
34
+ if (existing) {
35
+ existing.depth += 1;
36
+ try {
37
+ return await opts.fn();
38
+ } finally {
39
+ existing.depth -= 1;
40
+ if (existing.depth <= 0) {
41
+ HELD_BY_KEY.delete(k);
42
+ existing.release();
43
+ }
44
+ }
45
+ }
46
+
47
+ // IMPORTANT: this is tuned for "hold for whole workflow".
48
+ const handle = await deps.acquireRepoLock({
49
+ lockPath: opts.lockPath,
50
+ repoRoot: opts.repoRoot,
51
+ sessionId: opts.sessionId,
52
+ owner: opts.owner,
53
+
54
+ retryMs: 30_000,
55
+ staleMs: 2 * 60_000,
56
+ heartbeatMs: 200,
57
+ minWriteMs: 800,
58
+ pollMs: 20,
59
+ pollMaxMs: 250,
60
+ });
61
+
62
+ const held: Held = { release: handle.release, depth: 1 };
63
+ HELD_BY_KEY.set(k, held);
64
+
65
+ try {
66
+ return await opts.fn();
67
+ } finally {
68
+ held.depth -= 1;
69
+ if (held.depth <= 0) {
70
+ HELD_BY_KEY.delete(k);
71
+ held.release();
72
+ }
73
+ }
74
+ }