import * as path from "path" import { tracePersistenceEvent } from "../core/task-persistence/persistenceTrace" import * as lockfile from "proper-lockfile" /** * Shared staleness window for per-file advisory locks. This module owns the * single advisory lock protocol used by `safeWriteJson` and by callers that * must serialize with it, such as task-history deletion. */ export const LOCK_STALE_MS = 31_000 const RECOVERY_RETRY_MS = 1000 const RECOVERY_GRACE_MS = 4000 export interface FileLockOptions { /** Recovery may wait out a crashed writer's staleness window without stealing a live lock. */ waitForStaleLock?: boolean /** Startup scans defer locked files instead of waiting under the store mutex. */ immediate?: boolean } /** * Acquire the advisory lock for one file path using the exact protocol * `safeWriteJson` uses, so operations that hold this lock serialize with * every `safeWriteJson` write to the same path. Callers must release the * returned function exactly once and must not acquire the same lock again * while holding it. */ export async function acquireFileLock(filePath: string, options?: FileLockOptions): Promise<() => Promise> { const absoluteFilePath = path.resolve(filePath) if (options?.waitForStaleLock) tracePersistenceEvent("json-lock-wait", { filePath: absoluteFilePath }) try { return await lockfile.lock(absoluteFilePath, { stale: LOCK_STALE_MS, update: 10000, // Update mtime every 10 seconds to prevent staleness if operation is long realpath: false, // the file may not exist yet, which is acceptable retries: options?.immediate ? 0 : options?.waitForStaleLock ? { retries: Math.ceil((LOCK_STALE_MS + RECOVERY_GRACE_MS) / RECOVERY_RETRY_MS), factor: 1, minTimeout: RECOVERY_RETRY_MS, maxTimeout: RECOVERY_RETRY_MS, } : { // Configuration for retrying lock acquisition retries: 5, // Number of retries after the initial attempt factor: 2, // Exponential backoff factor (e.g., 100ms, 200ms, 400ms, ...) minTimeout: 100, // Minimum time to wait before the first retry (in ms) maxTimeout: 1000, // Maximum time to wait for any single retry (in ms) }, onCompromised: (err) => { console.error(`Lock at ${absoluteFilePath} was compromised:`, err) throw err }, }) } catch (lockError) { console.error(`Failed to acquire lock for ${absoluteFilePath}:`, lockError) throw lockError } } /** * Run one operation while holding the advisory lock for a file path. * Callers must not acquire this lock again from inside `operation`, and * they must keep the documented lock order when combining this helper * with other locks to prevent deadlock. */ export async function withFileLock( filePath: string, operation: (absoluteFilePath: string) => Promise, options?: FileLockOptions, ): Promise { const absoluteFilePath = path.resolve(filePath) const releaseLock = await acquireFileLock(absoluteFilePath, options) let result: T try { result = await operation(absoluteFilePath) } catch (operationError) { // The operation error is the primary failure. Release without // reporting a secondary release error over it. try { await releaseLock() } catch (releaseError) { console.error(`Failed to release lock for ${absoluteFilePath}:`, releaseError) } throw operationError } try { await releaseLock() } catch (releaseError) { // The operation already succeeded, so a release failure is only // logged, matching how `safeWriteJson` handles release failures. console.error(`Failed to release lock for ${absoluteFilePath}:`, releaseError) } return result }