Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(claude): atomic lock acquisition (wx flag) + stale-reclaim test
Shrish Dwivedi35d26baf235d
diff --git a/src/claude/state.ts b/src/claude/state.tsindex ab09cc9d..e20b9933 100644--- a/src/claude/state.ts+++ b/src/claude/state.ts@@ -1,5 +1,5 @@import { readFileSync, writeFileSync, mkdirSync, existsSync, renameSync, rmSync, statSync } from 'node:fs';-import { join } from 'node:path';+import { join, dirname } from 'node:path';export interface Stats {nodeCount: number; edgeCount: number; languages: string[];@@ -32,7 +32,7 @@ function readJson<T>(p: string): T | null {try { return JSON.parse(readFileSync(p, 'utf8')) as T; } catch { return null; }}function writeJsonAtomic(p: string, value: unknown): void {- mkdirSync(join(p, '..'), { recursive: true });+ mkdirSync(dirname(p), { recursive: true });const tmp = `${p}.${process.pid}.tmp`;writeFileSync(tmp, JSON.stringify(value, null, 2));renameSync(tmp, p);@@ -54,9 +54,19 @@ export function writeSession(d: string, id: string, s: SessionState): void {export function acquireLock(d: string): boolean {const p = lockPath(d);- if (existsSync(p) && Date.now() - statSync(p).mtimeMs < LOCK_STALE_MS) return false;mkdirSync(cacheDir(d), { recursive: true });- writeFileSync(p, JSON.stringify({ pid: process.pid, at: new Date().toISOString() }));- return true;+ const payload = JSON.stringify({ pid: process.pid, at: new Date().toISOString() });+ try {+ writeFileSync(p, payload, { flag: 'wx' }); // atomic exclusive create+ return true;+ } catch (e: any) {+ if (e?.code !== 'EEXIST') throw e;+ let stale: boolean;+ try { stale = Date.now() - statSync(p).mtimeMs >= LOCK_STALE_MS; } catch { stale = true; }+ if (!stale) return false;+ try { rmSync(p); } catch { /* another process reclaimed it */ }+ try { writeFileSync(p, payload, { flag: 'wx' }); return true; }+ catch (e2: any) { if (e2?.code === 'EEXIST') return false; throw e2; }+ }}export function releaseLock(d: string): void { try { rmSync(lockPath(d)); } catch { /* already gone */ } }diff --git a/test/claude-state.test.ts b/test/claude-state.test.tsindex 474fb5b4..397ff1f0 100644--- a/test/claude-state.test.ts+++ b/test/claude-state.test.ts@@ -1,11 +1,11 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { mkdtempSync, existsSync } from 'node:fs';+import { mkdtempSync, existsSync, utimesSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import {emptyStats, readStats, writeStats, patchStats,- readSession, writeSession, acquireLock, releaseLock, cacheDir,+ readSession, writeSession, acquireLock, releaseLock, cacheDir, LOCK_STALE_MS,} from '../src/claude/state.js';function fresh(): string { return mkdtempSync(join(tmpdir(), 'graft-state-')); }@@ -39,3 +39,12 @@ test('lock is exclusive then releasable', () => {releaseLock(d);assert.equal(acquireLock(d), true, 'reacquire after release');});++test('acquireLock reclaims a stale lock', () => {+ const d = fresh();+ assert.equal(acquireLock(d), true);+ const p = join(cacheDir(d), '.sync.lock');+ const old = (Date.now() - LOCK_STALE_MS - 1000) / 1000;+ utimesSync(p, old, old);+ assert.equal(acquireLock(d), true, 'stale lock reclaimed');+});