astrocode-workflow 0.3.2 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astrocode-workflow",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,36 @@
1
+ // src/astro/workflow-runner.ts
2
+ import { acquireRepoLock } from "../state/repo-lock";
3
+ import { workflowRepoLock } from "../state/workflow-repo-lock";
4
+
5
+ /**
6
+ * This is the only place you should hold the repo lock.
7
+ * Everything that mutates the repo (tool calls, steps) runs inside this scope.
8
+ *
9
+ * Replace the internals with your actual astro/opencode driver loop.
10
+ */
11
+ export async function runAstroWorkflow(opts: {
12
+ lockPath: string;
13
+ repoRoot: string;
14
+ sessionId: string;
15
+ owner?: string;
16
+
17
+ // Hook in your existing workflow engine
18
+ proceedOneStep: () => Promise<{ done: boolean }>;
19
+ }): Promise<void> {
20
+ await workflowRepoLock(
21
+ { acquireRepoLock },
22
+ {
23
+ lockPath: opts.lockPath,
24
+ repoRoot: opts.repoRoot,
25
+ sessionId: opts.sessionId,
26
+ owner: opts.owner,
27
+ fn: async () => {
28
+ // ✅ Lock is held ONCE for the entire run. Tool calls can "rattle through".
29
+ while (true) {
30
+ const { done } = await opts.proceedOneStep();
31
+ if (done) return;
32
+ }
33
+ },
34
+ }
35
+ );
36
+ }
@@ -1,13 +1,37 @@
1
1
  // src/state/repo-lock.ts
2
2
  import fs from "node:fs";
3
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;
4
16
 
5
17
  type LockFile = {
18
+ v: number;
19
+
6
20
  pid: number;
7
21
  created_at: string;
8
22
  updated_at: string;
9
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).
10
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
+
11
35
  owner?: string; // optional human-readable owner
12
36
  };
13
37
 
@@ -19,117 +43,509 @@ function sleep(ms: number) {
19
43
  return new Promise((r) => setTimeout(r, ms));
20
44
  }
21
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
+ */
22
51
  function isPidAlive(pid: number): boolean {
23
52
  try {
24
- // Signal 0 checks existence without killing.
25
53
  (process as any).kill(pid, 0);
26
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);
27
79
  } catch {
28
- return false;
80
+ // ignore
29
81
  }
30
82
  }
31
83
 
84
+ /**
85
+ * Reads & validates lock file defensively.
86
+ * Returns null on any parse/validation failure.
87
+ */
32
88
  function readLock(lockPath: string): LockFile | null {
33
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
+
34
94
  const raw = fs.readFileSync(lockPath, "utf8");
35
95
  const parsed = JSON.parse(raw) as LockFile;
36
- if (!parsed || typeof parsed.pid !== "number") return null;
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
+
37
110
  return parsed;
38
111
  } catch {
39
112
  return null;
40
113
  }
41
114
  }
42
115
 
43
- function writeLock(lockPath: string, lock: LockFile) {
44
- fs.mkdirSync(path.dirname(lockPath), { recursive: true });
45
- fs.writeFileSync(lockPath, JSON.stringify(lock, null, 2));
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}`);
46
176
  }
47
177
 
48
- function safeUnlink(lockPath: string) {
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
+
49
184
  try {
50
- fs.unlinkSync(lockPath);
51
- } catch {
52
- // ignore
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;
53
197
  }
54
198
  }
55
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
+
56
321
  /**
57
322
  * Acquire a repo-scoped lock with:
58
- * - Re-entrant behavior for SAME PID (your own process can call tools repeatedly)
59
- * - Stale lock eviction for dead PIDs
60
- * - Best-effort contention retry
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
61
330
  */
62
331
  export async function acquireRepoLock(opts: {
63
332
  lockPath: string;
64
333
  repoRoot: string;
65
334
  sessionId?: string;
66
335
  owner?: string;
67
- retryMs?: number; // default 2000
68
- pollMs?: number; // default 100
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
69
343
  }): Promise<{ release: () => void }> {
344
+ installExitHookOnce();
345
+
70
346
  const { lockPath, repoRoot, sessionId, owner } = opts;
71
- const retryMs = opts.retryMs ?? 2000;
72
- const pollMs = opts.pollMs ?? 100;
73
347
 
74
- const myPid = (process as any).pid;
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);
75
376
  const startedAt = Date.now();
377
+ let pollMs = pollBaseMs;
76
378
 
77
379
  while (true) {
78
380
  const existing = readLock(lockPath);
79
381
 
80
- // No lock -> take it.
382
+ // No lock (or unreadable/invalid) -> try create.
81
383
  if (!existing) {
82
384
  const now = nowISO();
83
- writeLock(lockPath, {
385
+ const leaseId = crypto.randomUUID();
386
+
387
+ const candidate: LockFile = {
388
+ v: LOCK_VERSION,
84
389
  pid: myPid,
85
390
  created_at: now,
86
391
  updated_at: now,
87
392
  repo_root: repoRoot,
393
+ instance_id: PROCESS_INSTANCE_ID,
88
394
  session_id: sessionId,
395
+ lease_id: leaseId,
89
396
  owner,
90
- });
397
+ };
91
398
 
92
- // Verify we actually own it (race safety)
93
- const verify = readLock(lockPath);
94
- if (verify && verify.pid === myPid) {
95
- return {
96
- release: () => {
97
- const cur = readLock(lockPath);
98
- // Only the owner PID removes the lock.
99
- if (cur && cur.pid === myPid) safeUnlink(lockPath);
100
- },
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));
101
424
  };
102
- }
103
425
 
104
- // Race lost; retry.
105
- } else {
106
- // Re-entrant: SAME PID owns lock -> refresh timestamp and proceed.
107
- if (existing.pid === myPid) {
108
- const now = nowISO();
109
- writeLock(lockPath, { ...existing, updated_at: now, session_id: sessionId ?? existing.session_id, owner: owner ?? existing.owner });
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
+
110
437
  return {
111
438
  release: () => {
112
- const cur = readLock(lockPath);
113
- if (cur && cur.pid === myPid) safeUnlink(lockPath);
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
+ }
114
447
  },
115
448
  };
116
449
  }
117
450
 
118
- // Another PID: if dead -> evict stale lock
119
- if (!isPidAlive(existing.pid)) {
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;
120
486
  safeUnlink(lockPath);
121
- // loop back and acquire
122
- } else {
123
- // Alive and not us -> wait bounded
124
- if (Date.now() - startedAt > retryMs) {
125
- throw new Error(
126
- `Astrocode lock is already held (${lockPath}). pid=${existing.pid} (alive). ` +
127
- `Close other opencode processes or wait.`
128
- );
129
- }
130
- await sleep(pollMs);
131
- }
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
+ };
132
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));
133
549
  }
134
550
  }
135
551
 
@@ -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
+ }
package/src/tools/init.ts CHANGED
@@ -7,7 +7,7 @@ import { ensureSchema, openSqlite, configurePragmas } from "../state/db";
7
7
  import { getAstroPaths, ensureAstroDirs } from "../shared/paths";
8
8
  import { nowISO } from "../shared/time";
9
9
  import { sha256Hex } from "../shared/hash";
10
- import { withRepoLock } from "../state/repo-lock";
10
+
11
11
 
12
12
  type RuntimeState = {
13
13
  db: SqliteDb | null;
@@ -30,15 +30,6 @@ export function createAstroInitTool(opts: { ctx: any; config: AstrocodeConfig; r
30
30
  },
31
31
  execute: async ({ ensure_spec, spec_placeholder }) => {
32
32
  const repoRoot = ctx.directory as string;
33
- const lockPath = path.join(repoRoot, ".astro", "astro.lock");
34
- const sessionId = (ctx as any).sessionID as string | undefined;
35
-
36
- return withRepoLock({
37
- lockPath,
38
- repoRoot,
39
- sessionId,
40
- owner: "astro_init",
41
- fn: async () => {
42
33
  const paths = getAstroPaths(repoRoot, config.db.path);
43
34
  ensureAstroDirs(paths);
44
35
 
@@ -116,16 +107,14 @@ export function createAstroInitTool(opts: { ctx: any; config: AstrocodeConfig; r
116
107
  ? `Next: run /astro-status. (DB recovered in-process.)`
117
108
  : `Next: restart the agent/runtime if Astrocode is still in Limited Mode, then run /astro-status.`,
118
109
  ].join("\n");
119
- } finally {
120
- // Only close if this tool opened it AND we did not publish it for ongoing use.
121
- if (!hadDbAlready && !publishedToRuntime && db && typeof db.close === "function") {
122
- try {
123
- db.close();
124
- } catch {}
125
- }
126
- }
127
- },
128
- });
110
+ } finally {
111
+ // Only close if this tool opened it AND we did not publish it for ongoing use.
112
+ if (!hadDbAlready && !publishedToRuntime && db && typeof db.close === "function") {
113
+ try {
114
+ db.close();
115
+ } catch {}
116
+ }
117
+ }
129
118
  },
130
119
  });
131
120
  }
@@ -1,12 +1,11 @@
1
1
  import { tool, type ToolDefinition } from "@opencode-ai/plugin/tool";
2
- import path from "node:path";
3
2
  import type { AstrocodeConfig } from "../config/schema";
4
3
  import type { SqliteDb } from "../state/db";
5
4
  import { withTx } from "../state/db";
6
5
  import { repairState, formatRepairReport } from "../workflow/repair";
7
6
  import { putArtifact } from "../workflow/artifacts";
8
7
  import { nowISO } from "../shared/time";
9
- import { withRepoLock } from "../state/repo-lock";
8
+
10
9
 
11
10
  export function createAstroRepairTool(opts: { ctx: any; config: AstrocodeConfig; db: SqliteDb }): ToolDefinition {
12
11
  const { ctx, config, db } = opts;
@@ -18,27 +17,16 @@ export function createAstroRepairTool(opts: { ctx: any; config: AstrocodeConfig;
18
17
  },
19
18
  execute: async ({ write_report_artifact }) => {
20
19
  const repoRoot = ctx.directory as string;
21
- const lockPath = path.join(repoRoot, ".astro", "astro.lock");
22
- const sessionId = (ctx as any).sessionID as string | undefined;
23
-
24
- return withRepoLock({
25
- lockPath,
26
- repoRoot,
27
- sessionId,
28
- owner: "astro_repair",
29
- fn: async () => {
30
- const report = withTx(db, () => repairState(db, config));
31
- const md = formatRepairReport(report);
20
+ const report = withTx(db, () => repairState(db, config));
21
+ const md = formatRepairReport(report);
32
22
 
33
- if (write_report_artifact) {
34
- const rel = `.astro/repair/repair_${nowISO().replace(/[:.]/g, "-")}.md`;
35
- const a = putArtifact({ repoRoot, db, run_id: null, stage_key: null, type: "log", rel_path: rel, content: md, meta: { kind: "repair" } });
36
- return md + `\n\nReport saved: ${rel} (artifact=${a.artifact_id})`;
37
- }
23
+ if (write_report_artifact) {
24
+ const rel = `.astro/repair/repair_${nowISO().replace(/[:.]/g, "-")}.md`;
25
+ const a = putArtifact({ repoRoot, db, run_id: null, stage_key: null, type: "log", rel_path: rel, content: md, meta: { kind: "repair" } });
26
+ return md + `\n\nReport saved: ${rel} (artifact=${a.artifact_id})`;
27
+ }
38
28
 
39
- return md;
40
- },
41
- });
29
+ return md;
42
30
  },
43
31
  });
44
32
  }
@@ -13,7 +13,7 @@ import { getAstroPaths, ensureAstroDirs, toPosix } from "../shared/paths";
13
13
  import { failRun, getActiveRun, getStageRuns, startStage, completeRun } from "../workflow/state-machine";
14
14
  import { newEventId, newId } from "../state/ids";
15
15
  import { insertStory } from "../workflow/story-helpers";
16
- import { withRepoLock } from "../state/repo-lock";
16
+
17
17
 
18
18
  function nextStageKey(pipeline: StageKey[], current: StageKey): StageKey | null {
19
19
  const i = pipeline.indexOf(current);
@@ -129,15 +129,6 @@ export function createAstroStageCompleteTool(opts: { ctx: any; config: Astrocode
129
129
  },
130
130
  execute: async ({ run_id, stage_key, output_text, allow_new_stories, relation_reason }) => {
131
131
  const repoRoot = ctx.directory as string;
132
- const lockPath = path.join(repoRoot, ".astro", "astro.lock");
133
- const sessionId = (ctx as any).sessionID as string | undefined;
134
-
135
- return withRepoLock({
136
- lockPath,
137
- repoRoot,
138
- sessionId,
139
- owner: "astro_stage_complete",
140
- fn: async () => {
141
132
  const paths = getAstroPaths(repoRoot, config.db.path);
142
133
  ensureAstroDirs(paths);
143
134
 
@@ -401,8 +392,6 @@ Ensure JSON has required fields (stage_key, status) and valid syntax.`;
401
392
  lines.push(context);
402
393
 
403
394
  return lines.join("\n").trim();
404
- },
405
- });
406
395
  },
407
396
  });
408
397
  }
@@ -1,9 +1,8 @@
1
1
  import { tool, type ToolDefinition } from "@opencode-ai/plugin/tool";
2
- import path from "node:path";
3
2
  import type { AstrocodeConfig } from "../config/schema";
4
3
  import type { SqliteDb } from "../state/db";
5
4
  import { decideNextAction, getActiveRun, getStageRuns, getStory } from "../workflow/state-machine";
6
- import { withRepoLock } from "../state/repo-lock";
5
+
7
6
 
8
7
  function statusIcon(status: string): string {
9
8
  switch (status) {
@@ -57,17 +56,7 @@ export function createAstroStatusTool(opts: { ctx: any; config: AstrocodeConfig;
57
56
  ].join("\n");
58
57
  }
59
58
 
60
- const repoRoot = ctx.directory as string;
61
- const lockPath = path.join(repoRoot, ".astro", "astro.lock");
62
- const sessionId = (ctx as any).sessionID as string | undefined;
63
-
64
- return withRepoLock({
65
- lockPath,
66
- repoRoot,
67
- sessionId,
68
- owner: "astro_status",
69
- fn: async () => {
70
- try {
59
+ try {
71
60
  const active = getActiveRun(db);
72
61
 
73
62
  const lines: string[] = [];
@@ -141,9 +130,7 @@ export function createAstroStatusTool(opts: { ctx: any; config: AstrocodeConfig;
141
130
  `⛔ Database error.`,
142
131
  `Error: ${msg}`,
143
132
  ].join("\n");
144
- }
145
- },
146
- });
133
+ }
147
134
  },
148
135
  });
149
136
  }
@@ -1,5 +1,4 @@
1
1
  import { tool, type ToolDefinition } from "@opencode-ai/plugin/tool";
2
- import path from "node:path";
3
2
  import type { AstrocodeConfig } from "../config/schema";
4
3
  import type { SqliteDb } from "../state/db";
5
4
  import { withTx } from "../state/db";
@@ -7,11 +6,11 @@ import { nowISO } from "../shared/time";
7
6
  import type { StoryState } from "../state/types";
8
7
 
9
8
  import { insertStory } from "../workflow/story-helpers";
10
- import { withRepoLock } from "../state/repo-lock";
9
+
11
10
 
12
11
 
13
12
  export function createAstroStoryQueueTool(opts: { ctx: any; config: AstrocodeConfig; db: SqliteDb }): ToolDefinition {
14
- const { ctx, db } = opts;
13
+ const { db } = opts;
15
14
 
16
15
  return tool({
17
16
  description: "Create a queued story (ticket) in Astrocode. Returns story_key.",
@@ -22,30 +21,18 @@ export function createAstroStoryQueueTool(opts: { ctx: any; config: AstrocodeCon
22
21
  priority: tool.schema.number().int().default(0),
23
22
  },
24
23
  execute: async ({ title, body_md, epic_key, priority }) => {
25
- const repoRoot = ctx.directory as string;
26
- const lockPath = path.join(repoRoot, ".astro", "astro.lock");
27
- const sessionId = (ctx as any).sessionID as string | undefined;
28
-
29
- return withRepoLock({
30
- lockPath,
31
- repoRoot,
32
- sessionId,
33
- owner: "astro_story_queue",
34
- fn: async () => {
35
- const story_key = withTx(db, () => {
36
- const key = insertStory(db, { title, body_md, epic_key: epic_key ?? null, priority: priority ?? 0, state: 'queued' });
37
- return key;
38
- });
39
-
40
- return `✅ Queued story ${story_key}: ${title}`;
41
- },
24
+ const story_key = withTx(db, () => {
25
+ const key = insertStory(db, { title, body_md, epic_key: epic_key ?? null, priority: priority ?? 0, state: 'queued' });
26
+ return key;
42
27
  });
28
+
29
+ return `✅ Queued story ${story_key}: ${title}`;
43
30
  },
44
31
  });
45
32
  }
46
33
 
47
34
  export function createAstroStoryApproveTool(opts: { ctx: any; config: AstrocodeConfig; db: SqliteDb }): ToolDefinition {
48
- const { ctx, db } = opts;
35
+ const { db } = opts;
49
36
 
50
37
  return tool({
51
38
  description: "Approve a story so it becomes eligible to run.",
@@ -53,26 +40,14 @@ export function createAstroStoryApproveTool(opts: { ctx: any; config: AstrocodeC
53
40
  story_key: tool.schema.string().min(1),
54
41
  },
55
42
  execute: async ({ story_key }) => {
56
- const repoRoot = ctx.directory as string;
57
- const lockPath = path.join(repoRoot, ".astro", "astro.lock");
58
- const sessionId = (ctx as any).sessionID as string | undefined;
59
-
60
- return withRepoLock({
61
- lockPath,
62
- repoRoot,
63
- sessionId,
64
- owner: "astro_story_approve",
65
- fn: async () => {
66
- const now = nowISO();
67
- const row = db.prepare("SELECT story_key, state, title FROM stories WHERE story_key=?").get(story_key) as any;
68
- if (!row) throw new Error(`Story not found: ${story_key}`);
69
-
70
- if (row.state === "approved") return `ℹ️ Story ${story_key} already approved.`;
71
-
72
- db.prepare("UPDATE stories SET state='approved', approved_at=?, updated_at=? WHERE story_key=?").run(now, now, story_key);
73
- return `✅ Approved story ${story_key}: ${row.title}`;
74
- },
75
- });
43
+ const now = nowISO();
44
+ const row = db.prepare("SELECT story_key, state, title FROM stories WHERE story_key=?").get(story_key) as any;
45
+ if (!row) throw new Error(`Story not found: ${story_key}`);
46
+
47
+ if (row.state === "approved") return `ℹ️ Story ${story_key} already approved.`;
48
+
49
+ db.prepare("UPDATE stories SET state='approved', approved_at=?, updated_at=? WHERE story_key=?").run(now, now, story_key);
50
+ return `✅ Approved story ${story_key}: ${row.title}`;
76
51
  },
77
52
  });
78
53
  }
@@ -20,11 +20,12 @@ import { buildStageDirective, directiveHash } from "../workflow/directives";
20
20
  import { injectChatPrompt } from "../ui/inject";
21
21
  import { nowISO } from "../shared/time";
22
22
  import { newEventId } from "../state/ids";
23
- import { withRepoLock } from "../state/repo-lock";
23
+
24
24
  import { debug } from "../shared/log";
25
25
  import { createToastManager } from "../ui/toasts";
26
26
  import type { AgentConfig } from "@opencode-ai/sdk";
27
27
  import { acquireRepoLock } from "../state/repo-lock";
28
+ import { workflowRepoLock } from "../state/workflow-repo-lock";
28
29
 
29
30
  // Agent name mapping for case-sensitive resolution
30
31
  export const STAGE_TO_AGENT_MAP: Record<string, string> = {
@@ -191,12 +192,14 @@ export function createAstroWorkflowProceedTool(opts: { ctx: any; config: Astroco
191
192
  const lockPath = path.join(repoRoot, ".astro", "astro.lock");
192
193
  const sessionId = (ctx as any).sessionID as string | undefined;
193
194
 
194
- return withRepoLock({
195
- lockPath,
196
- repoRoot,
197
- sessionId,
198
- owner: "astro_workflow_proceed",
199
- fn: async () => {
195
+ return workflowRepoLock(
196
+ { acquireRepoLock },
197
+ {
198
+ lockPath,
199
+ repoRoot,
200
+ sessionId,
201
+ owner: "astro_workflow_proceed",
202
+ fn: async () => {
200
203
  const steps = Math.min(max_steps, config.workflow.loop_max_steps_hard_cap);
201
204
 
202
205
  const actions: string[] = [];
@@ -420,8 +423,8 @@ export function createAstroWorkflowProceedTool(opts: { ctx: any; config: Astroco
420
423
  }
421
424
 
422
425
  return lines.join("\n").trim();
423
- },
424
- });
426
+ },
427
+ });
425
428
  },
426
429
  });
427
430
  }