(opts: {
lockName: LockName;
lockDir: string;
})
| 258 | | { status: 'incompatible'; reason: 'missing-fields' | 'corrupt'; raw: unknown }; |
| 259 | |
| 260 | export function readProcessLockDetailed(opts: { |
| 261 | lockName: LockName; |
| 262 | lockDir: string; |
| 263 | }): ReadProcessLockResult { |
| 264 | const { lockName, lockDir } = opts; |
| 265 | const lockPath = lockFilePath(lockDir, lockName); |
| 266 | if (!existsSync(lockPath)) return { status: 'absent' }; |
| 267 | |
| 268 | let raw: unknown; |
| 269 | try { |
| 270 | raw = JSON.parse(readFileSync(lockPath, 'utf-8')); |
| 271 | } catch { |
| 272 | return { status: 'incompatible', reason: 'corrupt', raw: undefined }; |
| 273 | } |
| 274 | |
| 275 | if (!raw || typeof raw !== 'object') { |
| 276 | return { status: 'incompatible', reason: 'corrupt', raw }; |
| 277 | } |
| 278 | const r = raw as Partial<ProcessLockMetadata>; |
| 279 | if ( |
| 280 | !isValidLockPid(r.pid) || |
| 281 | typeof r.hostname !== 'string' || |
| 282 | typeof r.port !== 'number' || |
| 283 | typeof r.startedAt !== 'string' || |
| 284 | typeof r.worktreeRoot !== 'string' |
| 285 | ) { |
| 286 | return { status: 'incompatible', reason: 'corrupt', raw }; |
| 287 | } |
| 288 | |
| 289 | const lock: ProcessLockMetadata = { |
| 290 | pid: r.pid, |
| 291 | hostname: r.hostname, |
| 292 | port: r.port, |
| 293 | startedAt: r.startedAt, |
| 294 | worktreeRoot: r.worktreeRoot, |
| 295 | protocolVersion: typeof r.protocolVersion === 'number' ? r.protocolVersion : undefined, |
| 296 | runtimeVersion: typeof r.runtimeVersion === 'string' ? r.runtimeVersion : undefined, |
| 297 | }; |
| 298 | |
| 299 | if (lock.hostname !== hostname()) return { status: 'stale', lock }; |
| 300 | if (!isProcessAlive(lock.pid)) { |
| 301 | try { |
| 302 | unlinkSync(lockPath); |
| 303 | } catch {} |
| 304 | return { status: 'stale', lock }; |
| 305 | } |
| 306 | |
| 307 | if (lock.protocolVersion === undefined || lock.runtimeVersion === undefined) { |
| 308 | return { status: 'incompatible', reason: 'missing-fields', raw }; |
| 309 | } |
| 310 | |
| 311 | return { status: 'live', lock }; |
| 312 | } |
| 313 | |
| 314 | export function releaseProcessLock(opts: { lockName: LockName; lockDir: string }): void { |
| 315 | const { lockName, lockDir } = opts; |
no test coverage detected