This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathFileLockManager.ts
More file actions
348 lines (292 loc) · 8.47 KB
/
Copy pathFileLockManager.ts
File metadata and controls
348 lines (292 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import path from "path"
/**
* Information about a file lock held by a task.
*/
export interface FileLockInfo {
/** Absolute normalized path of the locked file */
filePath: string
/** ID of the task holding the lock */
taskId: string
/** Timestamp (ms) when the lock was acquired */
acquiredAt: number
}
/**
* Result returned when a lock acquisition attempt fails.
*/
export interface LockConflict {
/** The file that is already locked */
filePath: string
/** The task that currently holds the lock */
holdingTaskId: string
/** How long (ms) the lock has been held */
heldForMs: number
}
/**
* Events emitted by the FileLockManager.
*/
export type FileLockEvent =
| { type: "lock-acquired"; filePath: string; taskId: string }
| { type: "lock-released"; filePath: string; taskId: string }
| { type: "lock-expired"; filePath: string; taskId: string }
| { type: "all-locks-released"; taskId: string; count: number }
export type FileLockEventListener = (event: FileLockEvent) => void
export interface FileLockManagerOptions {
/**
* Maximum duration (ms) a lock can be held before it is forcibly released.
* Default: 120_000 (2 minutes).
*/
lockTimeoutMs?: number
}
const DEFAULT_LOCK_TIMEOUT_MS = 120_000
/**
* Advisory file-level lock manager for coordinating writes across concurrent tasks.
*
* Locks are "advisory" -- they do not use OS-level file locks. Instead, the
* tool execution layer checks the lock manager before allowing write operations.
* This keeps the system portable and testable.
*
* All file paths are normalized to absolute paths using `path.resolve` before
* being used as map keys, ensuring consistent lookup regardless of how the
* path is specified (relative, absolute, trailing slashes, etc.).
*/
export class FileLockManager {
/**
* Map from normalized absolute file path to lock info.
*/
private locks = new Map<string, FileLockInfo>()
/**
* Reverse index: taskId -> set of normalized file paths locked by that task.
*/
private taskLocks = new Map<string, Set<string>>()
/**
* Event listeners.
*/
private listeners: FileLockEventListener[] = []
/**
* Maximum lock hold duration in milliseconds.
*/
private readonly lockTimeoutMs: number
constructor(options?: FileLockManagerOptions) {
this.lockTimeoutMs = options?.lockTimeoutMs ?? DEFAULT_LOCK_TIMEOUT_MS
}
/**
* Attempt to acquire a write lock on a file for a specific task.
*
* If the file is already locked by the same task, refreshes the timestamp
* and returns true (re-entrant). If locked by a different task, checks
* for expiration first -- if the existing lock has expired it is forcibly
* released before granting the new lock.
*
* @returns `true` if the lock was acquired, `false` if another task holds it.
*/
acquireLock(filePath: string, taskId: string): boolean {
const normalized = this.normalizePath(filePath)
const existing = this.locks.get(normalized)
if (existing) {
// Re-entrant: same task already holds the lock -- refresh timestamp
if (existing.taskId === taskId) {
existing.acquiredAt = Date.now()
return true
}
// Check if the existing lock has expired
if (this.isLockExpired(existing)) {
this.forceReleaseLock(normalized, existing.taskId)
} else {
return false
}
}
// Acquire the lock
const lockInfo: FileLockInfo = {
filePath: normalized,
taskId,
acquiredAt: Date.now(),
}
this.locks.set(normalized, lockInfo)
let taskSet = this.taskLocks.get(taskId)
if (!taskSet) {
taskSet = new Set()
this.taskLocks.set(taskId, taskSet)
}
taskSet.add(normalized)
this.emit({ type: "lock-acquired", filePath: normalized, taskId })
return true
}
/**
* Release a lock held by a specific task.
* No-op if the task does not hold the lock.
*/
releaseLock(filePath: string, taskId: string): void {
const normalized = this.normalizePath(filePath)
const existing = this.locks.get(normalized)
if (!existing || existing.taskId !== taskId) {
return
}
this.locks.delete(normalized)
const taskSet = this.taskLocks.get(taskId)
if (taskSet) {
taskSet.delete(normalized)
if (taskSet.size === 0) {
this.taskLocks.delete(taskId)
}
}
this.emit({ type: "lock-released", filePath: normalized, taskId })
}
/**
* Release all locks held by a specific task.
* Called when a task completes, is cancelled, or errors out.
*/
releaseAllLocks(taskId: string): void {
const taskSet = this.taskLocks.get(taskId)
if (!taskSet || taskSet.size === 0) {
this.taskLocks.delete(taskId)
return
}
const count = taskSet.size
for (const normalized of taskSet) {
this.locks.delete(normalized)
}
this.taskLocks.delete(taskId)
this.emit({ type: "all-locks-released", taskId, count })
}
/**
* Check which task (if any) holds the lock on a file.
* Checks for expiration -- if the lock is expired, it is released and
* `undefined` is returned.
*
* @returns The taskId of the lock holder, or `undefined` if unlocked.
*/
getLockHolder(filePath: string): string | undefined {
const normalized = this.normalizePath(filePath)
const existing = this.locks.get(normalized)
if (!existing) {
return undefined
}
if (this.isLockExpired(existing)) {
this.forceReleaseLock(normalized, existing.taskId)
return undefined
}
return existing.taskId
}
/**
* Get detailed lock info for a file, or undefined if not locked.
* Checks for expiration.
*/
getLockInfo(filePath: string): FileLockInfo | undefined {
const normalized = this.normalizePath(filePath)
const existing = this.locks.get(normalized)
if (!existing) {
return undefined
}
if (this.isLockExpired(existing)) {
this.forceReleaseLock(normalized, existing.taskId)
return undefined
}
return { ...existing }
}
/**
* Get the conflict details when a lock acquisition would fail.
* Returns undefined if the file is not locked by another task.
*/
getLockConflict(filePath: string, taskId: string): LockConflict | undefined {
const normalized = this.normalizePath(filePath)
const existing = this.locks.get(normalized)
if (!existing || existing.taskId === taskId) {
return undefined
}
if (this.isLockExpired(existing)) {
this.forceReleaseLock(normalized, existing.taskId)
return undefined
}
return {
filePath: normalized,
holdingTaskId: existing.taskId,
heldForMs: Date.now() - existing.acquiredAt,
}
}
/**
* List all files currently locked by a specific task.
*/
getLockedFiles(taskId: string): string[] {
const taskSet = this.taskLocks.get(taskId)
if (!taskSet) {
return []
}
return Array.from(taskSet)
}
/**
* Get all currently held locks. Primarily for debugging/UI display.
* Expired locks are cleaned up during this call.
*/
getAllLocks(): FileLockInfo[] {
const result: FileLockInfo[] = []
const expired: Array<{ normalized: string; taskId: string }> = []
for (const [normalized, info] of this.locks) {
if (this.isLockExpired(info)) {
expired.push({ normalized, taskId: info.taskId })
} else {
result.push({ ...info })
}
}
// Clean up expired locks
for (const { normalized, taskId } of expired) {
this.forceReleaseLock(normalized, taskId)
}
return result
}
/**
* Get the total number of active locks.
*/
get lockCount(): number {
return this.locks.size
}
/**
* Register an event listener.
*/
onEvent(listener: FileLockEventListener): void {
this.listeners.push(listener)
}
/**
* Remove an event listener.
*/
offEvent(listener: FileLockEventListener): void {
const idx = this.listeners.indexOf(listener)
if (idx !== -1) {
this.listeners.splice(idx, 1)
}
}
/**
* Clear all locks and listeners. Primarily for testing.
*/
dispose(): void {
this.locks.clear()
this.taskLocks.clear()
this.listeners = []
}
// --- Private helpers ---
private normalizePath(filePath: string): string {
return path.resolve(filePath)
}
private isLockExpired(info: FileLockInfo): boolean {
return Date.now() - info.acquiredAt > this.lockTimeoutMs
}
private forceReleaseLock(normalized: string, taskId: string): void {
this.locks.delete(normalized)
const taskSet = this.taskLocks.get(taskId)
if (taskSet) {
taskSet.delete(normalized)
if (taskSet.size === 0) {
this.taskLocks.delete(taskId)
}
}
this.emit({ type: "lock-expired", filePath: normalized, taskId })
}
private emit(event: FileLockEvent): void {
for (const listener of this.listeners) {
try {
listener(event)
} catch {
// Swallow listener errors to avoid breaking lock operations
}
}
}
}