Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

Merge branch 'main' (c29724bcc) into redo/plugin-approval-receipt-migration

Yuewei-LL951ae13093f2
diff --cc apps/desktop/src/main/__tests__/ownerNamespaceMigration.test.ts
index 28f4990a94,5b2637b229..8e221b0689
--- a/apps/desktop/src/main/__tests__/ownerNamespaceMigration.test.ts
+++ b/apps/desktop/src/main/__tests__/ownerNamespaceMigration.test.ts
@@@ -6,12 -5,13 +6,15 @@@ import { afterEach, beforeEach, describ
import { dataOwnerStorageKey } from '../appSessionState.js';
import {
+ acknowledgeRecoveredLegacyGhosts,
claimLegacyOwnerNamespace,
getLegacyGhostRecoveryStatus,
- hasLegacyOwnerNamespaceClaim,
+ hasExclusiveSharedLegacyUserDataAccess,
+ hasLegacyOwnerNamespaceClaim,
- listLegacyOwnerProjectionRoots,
+ isLegacyOwnerNamespaceClaimOwnedBy,
+ isLegacyOwnerNamespaceClaimedByOtherOwner,
listLegacyGhostTombstoneRoots,
++ listLegacyOwnerProjectionRoots,
recoverLegacyGhostPlugins,
__testing,
} from '../ownerNamespaceMigration.js';
diff --cc apps/desktop/src/main/device-link/crossProcessLock.ts
index d6c90edb7c,2cc2d6b45a..aa931c8183
--- a/apps/desktop/src/main/device-link/crossProcessLock.ts
+++ b/apps/desktop/src/main/device-link/crossProcessLock.ts
@@@ -1,11 -1,25 +1,13 @@@
-/**
- * crossProcessLock —— 同一台机器上多个 Cindy 实例之间的建议性文件锁。
- * ---------------------------------------------------------------------------
- * 为什么需要:dev 实例与打包实例可以共用同一个 userData(见 devStartupStatus.ts),于是
- * 「读 → 改 → 整份写回」「先扫描再删除」这类临界区在两个进程之间会互相覆盖 —— 后落位的
- * 那次赢,而另一边以为自己成功了(review: codex P1,镜像缓存的队列与清理都撞过)。
- *
- * 用 `open(lock, 'wx')` 做锁:同目录、O_EXCL 语义在 Windows / macOS / Linux 都可靠。
- *
- * 陈旧锁的接管**不能只看时间**:临界区可能包含递归删除,正常持锁跑过 10 秒完全可能。
- * 只按 mtime 抢锁会把活着的持有者挤掉,它随后还会在 finally 里删掉别人的锁。所以:
- * - 锁内容写 `{ pid, startedAt }`,持锁期间每 2 秒 touch 一次 mtime(心跳);
- * - 只有「mtime 陈旧」**且**「owner pid 已经不在」才接管 —— 共享 userData 必然同机,
- * `process.kill(pid, 0)` 是可靠的存活判定;读不到锁 / 读不出内容一律保守不接管;
- * - 等不到锁就把 `held=false` 交给调用方**自行降级**(绝不无限等待:登出 / teardown 都会
- * 经过这些临界区,卡住比降级更糟);
- * - 释放时先确认锁里还是自己的 pid,再删。
- */
-
+import crypto from 'node:crypto';
+import { execFile } from 'node:child_process';
+import type { Stats } from 'node:fs';
import fsp from 'node:fs/promises';
++import fsSync from 'node:fs';
+import path from 'node:path';
+import { promisify } from 'node:util';
import { createLogger } from '../logger';
++import { readBoundedFileNoFollow } from '../utils/readBoundedFile.js';
const log = createLogger('device-link:cross-process-lock');
@@@ -218,859 -121,65 +220,873 @@@ export async function withCrossProcessL
}
}
-/** 只有「心跳早已停」且「owner 进程确实不在了」才允许接管。 */
-async function canTakeOverLock(lock: string): Promise<boolean> {
+function sleep(ms: number): Promise<void> {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
+
+async function inspectStaleLock(
+ lockPath: string,
+ allowMalformedTakeover = false,
+ requireCompleteRecord = false,
+): Promise<StaleLockCandidate | null> {
let stat: Awaited<ReturnType<typeof fsp.stat>>;
try {
- stat = await fsp.stat(lock);
- } catch (err) {
- const code = (err as NodeJS.ErrnoException)?.code;
- // 只有"锁文件真的没了"才算可以重新抢;EACCES / EPERM 这类读不到锁的情况必须保守。
- if (code === 'ENOENT' || code === 'ENOTDIR') return true;
- return false;
+ stat = await fsp.stat(lockPath);
+ } catch {
+ return null;
}
- if (Date.now() - stat.mtimeMs <= LOCK_STALE_MS) return false;
- const owner = await readLockOwnerPid(lock);
- if (owner === 'unreadable') return false; // 读不出来 ≠ 持有者已死
- if (owner === null) return true; // 能读但不是本模块写的(陈旧残留)
- if (owner === process.pid) return true; // 本进程上一轮崩在临界区里(心跳早停)
- try {
- process.kill(owner, 0);
- return false; // owner 还活着
- } catch (err) {
- // ESRCH = 进程不在了;EPERM = 进程在但不属于当前用户(保守认为活着)
- return (err as NodeJS.ErrnoException)?.code !== 'EPERM';
+ const record = await readLockRecord(lockPath, requireCompleteRecord);
+ if (record === 'missing' || record === 'unreadable') return null;
+ if (typeof record !== 'string' && record.state === 'released') {
+ return { kind: 'record', record };
}
+ if (Date.now() - stat.mtimeMs <= LOCK_STALE_MS) return null;
+ if (record === 'malformed') {
+ return allowMalformedTakeover
+ ? { kind: 'malformed', identity: malformedLockIdentity(stat) }
+ : null;
+ }
+ // Reference time for "when was the lock created": prefer birthtime, but
+ // fall back to mtime when birthtime is unavailable or 0 (several Linux
+ // filesystems and containers report 0). A null reference previously made the
+ // legacy PID-reuse check fail open (owner assumed active), which broke stale
+ // takeover on those platforms.
+ const createdAtMs = Number.isFinite(stat.birthtimeMs) && stat.birthtimeMs > 0
+ ? stat.birthtimeMs
+ : (Number.isFinite(stat.mtimeMs) && stat.mtimeMs > 0 ? stat.mtimeMs : null);
+ return (await isRecordOwnerActive(record, createdAtMs))
+ ? null
+ : { kind: 'record', record };
}
-/**
- * 锁文件里的 owner pid。三态:number = 正常;null = 能读但不是本模块写的;
- * 'unreadable' = 读不出来(不能据此判定持有者已死)。
- */
-async function readLockOwnerPid(lock: string): Promise<number | null | 'unreadable'> {
- let raw: string;
+async function readLockRecord(
+ lockPath: string,
+ requireCompleteRecord = false,
+): Promise<ReadLockRecord> {
- let raw: string;
++ const lockDir = path.dirname(lockPath);
++ let lockRealDir: string;
try {
- raw = await fsp.readFile(lockPath, 'utf8');
- raw = await fsp.readFile(lock, 'utf8');
- } catch (err) {
- const code = (err as NodeJS.ErrnoException)?.code;
- if (code === 'ENOENT' || code === 'ENOTDIR') return null;
++ lockRealDir = fsSync.realpathSync(lockDir);
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException)?.code;
+ if (code === 'ENOENT' || code === 'ENOTDIR') return 'missing';
return 'unreadable';
}
++ let bytes: Buffer | null;
++ try {
++ bytes = await readBoundedFileNoFollow(lockPath, 4 * 1024, {
++ containWithin: lockRealDir,
++ nonBlocking: true,
++ });
++ } catch (error) {
++ const code = (error as NodeJS.ErrnoException)?.code;
++ if (code === 'ENOENT' || code === 'ENOTDIR') return 'missing';
++ return 'unreadable';
++ }
++ if (bytes === null) return 'unreadable';
++ const raw = bytes.toString('utf8');
try {
const parsed = JSON.parse(raw) as unknown;
- if (parsed && typeof parsed === 'object') {
- const pid = (parsed as { pid?: unknown }).pid;
- if (typeof pid === 'number' && Number.isInteger(pid) && pid > 0) return pid;
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return 'malformed';
+ const value = parsed as Record<string, unknown>;
+ if (
+ typeof value.pid !== 'number' ||
+ !Number.isInteger(value.pid) ||
+ value.pid <= 0 ||
+ typeof value.startedAt !== 'number' ||
+ !Number.isFinite(value.startedAt)
+ ) {
+ return 'malformed';
+ }
+ const noncePresent = Object.prototype.hasOwnProperty.call(value, 'nonce');
+ const identityPresent = Object.prototype.hasOwnProperty.call(value, 'processStartIdentity');
+ const statePresent = Object.prototype.hasOwnProperty.call(value, 'state');
+ if (
+ requireCompleteRecord
+ && (
+ !noncePresent
+ || typeof value.nonce !== 'string'
+ || !isValidNonce(value.nonce)
+ || !identityPresent
+ || typeof value.processStartIdentity !== 'string'
+ || parseRecordedProcessStartMs(value.processStartIdentity) === null
+ || !statePresent
+ || (value.state !== 'held' && value.state !== 'released')
+ )
+ ) {
+ return 'malformed';
+ }
+ if (
+ noncePresent
+ && value.nonce !== null
+ && (typeof value.nonce !== 'string' || value.nonce === '')
+ ) {
+ return 'malformed';
}
+ if (
+ identityPresent
+ && value.processStartIdentity !== null
+ && (
+ typeof value.processStartIdentity !== 'string'
+ || value.processStartIdentity === ''
+ || parseRecordedProcessStartMs(value.processStartIdentity) === null
+ )
+ ) {
+ return 'malformed';
+ }
+ if (statePresent && value.state !== 'held' && value.state !== 'released') {
+ return 'malformed';
+ }
+ return {
+ pid: value.pid,
+ startedAt: value.startedAt,
+ nonce: typeof value.nonce === 'string' && value.nonce !== '' ? value.nonce : null,
+ processStartIdentity:
+ typeof value.processStartIdentity === 'string' && value.processStartIdentity !== ''
+ ? value.processStartIdentity
+ : null,
+ state: value.state === 'released' ? 'released' : 'held',
+ };
} catch {
- // 能读但不是合法 JSON:按陈旧残留处理。
+ return 'malformed';
+ }
+}
+
+async function publishLockRecord(
+ targetPath: string,
+ processStartIdentity: string | null,
+): Promise<PublishedLock | null> {
+ const nonce = crypto.randomUUID();
+ const candidatePath = `${targetPath}.candidate-${process.pid}-${nonce}.tmp`;
+ let handle: Awaited<ReturnType<typeof fsp.open>> | null = null;
+ try {
+ handle = await fsp.open(candidatePath, 'wx');
+ const record: LockRecord = {
+ pid: process.pid,
+ startedAt: Date.now(),
+ nonce,
+ processStartIdentity,
+ state: 'held',
+ };
+ await handle.writeFile(JSON.stringify(record), 'utf8');
+ await handle.sync();
+ try {
+ // A hard link publishes the already-flushed inode atomically and refuses
+ // to replace an existing target on every supported desktop platform.
+ await fsp.link(candidatePath, targetPath);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === 'EEXIST') {
+ await handle.close().catch(() => undefined);
+ handle = null;
+ return null;
+ }
+ throw error;
+ }
+ await fsp.rm(candidatePath, { force: true }).catch(() => undefined);
+ return { handle, nonce, record };
+ } catch (error) {
+ await handle?.close().catch(() => undefined);
+ throw error;
+ } finally {
+ await fsp.rm(candidatePath, { force: true }).catch(() => undefined);
+ }
+}
+
+async function quarantineStaleLock(
+ lockPath: string,
+ expected: StaleLockCandidate,
+ requireCompleteRecord = false,
+): Promise<'taken' | 'reclaiming' | 'changed' | 'retry' | 'failed'> {
+ const gate = await acquireReclaimGate(lockPath, 0);
+ if (!gate) return 'reclaiming';
+ const quarantinePath = `${lockPath}.reclaim-${process.pid}-${crypto.randomUUID()}`;
+ let quarantineMatchesExpected = false;
+ try {
+ const current = await inspectStaleLock(
+ lockPath,
+ expected.kind === 'malformed',
+ requireCompleteRecord,
+ );
+ if (!sameStaleCandidate(current, expected)) return 'changed';
+ try {
+ await fsp.rename(lockPath, quarantinePath);
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ return code === 'ENOENT' || code === 'ENOTDIR' ? 'changed' : 'failed';
+ }
+ if (!(await pathMatchesStaleCandidate(quarantinePath, expected, requireCompleteRecord))) {
+ await restoreMovedPathSafely(quarantinePath, lockPath);
+ return 'retry';
+ }
+ quarantineMatchesExpected = true;
+ return 'taken';
+ } finally {
+ if (quarantineMatchesExpected) await removePathWithRetry(quarantinePath);
+ await releaseReclaimGate(gate);
}
- return null;
}
-/** 释放前确认锁里还是自己的 pid:别人接管过之后不能替他删。 */
-async function releaseOwnLock(lock: string, label: string): Promise<void> {
- const owner = await readLockOwnerPid(lock);
- if (owner === 'unreadable') return; // 读不出来就别乱删别人的锁
- if (owner !== null && owner !== process.pid) {
- log.warn(`${label} lock was taken over; leaving it to its new owner`);
+async function reclaimInProgress(lockPath: string): Promise<boolean> {
+ if (await legacyReclaimInProgress(lockPath)) return true;
+ return (await findActiveReclaimGate(reclaimGateDirPath(lockPath))) !== null;
+}
+
+async function legacyReclaimInProgress(lockPath: string): Promise<boolean> {
+ const gatePath = `${lockPath}.reclaim`;
+ let exists = true;
+ try {
+ await fsp.stat(gatePath);
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ exists = code !== 'ENOENT' && code !== 'ENOTDIR';
+ }
+ if (!exists) return false;
+ const expected = await inspectStaleLock(gatePath, false);
+ if (!expected) return true;
+
+ const quarantinePath = `${gatePath}-stale-${process.pid}-${crypto.randomUUID()}`;
+ try {
+ await fsp.rename(gatePath, quarantinePath);
+ } catch {
+ return true;
+ }
+ let result = true;
+ try {
+ if (!(await pathMatchesStaleCandidate(quarantinePath, expected))) {
+ await restoreMovedPathSafely(quarantinePath, gatePath);
+ result = true;
+ } else {
+ result = false;
+ }
+ } finally {
+ // A mismatched file is never removed here: it may be a live successor.
+ if (
+ await pathExists(quarantinePath)
+ && await pathMatchesStaleCandidate(quarantinePath, expected)
+ ) {
+ await removePathWithRetry(quarantinePath);
+ }
+ }
+ return result;
+}
+
+function reclaimGateDirPath(lockPath: string): string {
+ return `${lockPath}.reclaim.d`;
+}
+
+async function cleanupPendingOwnGates(): Promise<void> {
+ for (const file of [...pendingOwnGateCleanup]) {
+ await removePathWithRetry(file);
+ if (!(await pathExists(file))) {
+ await removePathWithRetry(gateReleaseMarkerPath(file));
+ pendingOwnGateCleanup.delete(file);
+ }
+ }
+}
+
+async function findActiveReclaimGate(dirPath: string): Promise<string | null> {
+ let entries: string[];
+ try {
+ entries = await fsp.readdir(dirPath);
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ return code === 'ENOENT' || code === 'ENOTDIR' ? null : dirPath;
+ }
+
+ const active: Array<{ filePath: string; record: LockRecord }> = [];
+ for (const entry of entries) {
+ if (!entry.startsWith('gate-') || !entry.endsWith('.json')) continue;
+ const filePath = path.join(dirPath, entry);
+ let stat: Stats;
+ try {
+ stat = await fsp.stat(filePath);
+ } catch {
+ continue;
+ }
+ const record = await readLockRecord(filePath, true);
+ if (record === 'missing') continue;
+ if (await hasValidGateReleaseMarker(filePath)) {
+ await removePathWithRetry(filePath);
+ await removePathWithRetry(gateReleaseMarkerPath(filePath));
+ continue;
+ }
+ if (typeof record === 'string') {
+ const fresh = Date.now() - stat.mtimeMs <= LOCK_STALE_MS;
+ if (fresh || await isMalformedGateOwnerActive(filePath, stat)) return filePath;
+ await removePathWithRetry(filePath);
+ if (await pathExists(filePath)) return filePath;
+ continue;
+ }
+ if (record.state === 'released') {
+ await removePathWithRetry(filePath);
+ continue;
+ }
+ const fresh = Date.now() - stat.mtimeMs <= LOCK_STALE_MS;
+ const createdAtMs = Number.isFinite(stat.birthtimeMs) && stat.birthtimeMs > 0
+ ? stat.birthtimeMs
+ : (Number.isFinite(stat.mtimeMs) && stat.mtimeMs > 0 ? stat.mtimeMs : null);
+ if (fresh || await isRecordOwnerActive(record, createdAtMs)) {
+ active.push({ filePath, record });
+ } else {
+ // Gate paths contain a never-reused nonce, so deleting a dead contender
+ // cannot remove a successor that later acquired the same logical gate.
+ await removePathWithRetry(filePath);
+ }
+ }
+ active.sort((left, right) =>
+ left.record.startedAt - right.record.startedAt
+ || (left.record.nonce ?? '').localeCompare(right.record.nonce ?? '')
+ || left.filePath.localeCompare(right.filePath));
+ return active[0]?.filePath ?? null;
+}
+
+async function isMalformedGateOwnerActive(
+ filePath: string,
+ stat: Stats,
+): Promise<boolean> {
+ const match = /^gate-(\d+)-[0-9a-f-]+\.json$/i.exec(path.basename(filePath));
+ if (!match) return true;
+ const pid = Number(match[1]);
+ if (!Number.isInteger(pid) || pid <= 0) return true;
+ const createdAtMs = Number.isFinite(stat.birthtimeMs) && stat.birthtimeMs > 0
+ ? stat.birthtimeMs
+ : stat.mtimeMs;
+ return isRecordOwnerActive(
+ {
+ pid,
+ startedAt: stat.mtimeMs,
+ nonce: null,
+ processStartIdentity: null,
+ state: 'held',
+ },
+ createdAtMs,
+ );
+}
+
+async function acquireReclaimGate(
+ lockPath: string,
+ waitMs = RELEASE_GATE_WAIT_MS,
+): Promise<ReclaimGate | null> {
+ await cleanupPendingOwnGates();
+ const deadline = Date.now() + waitMs;
+ do {
+ if (await legacyReclaimInProgress(lockPath)) {
+ if (Date.now() >= deadline) return null;
+ await sleep(LOCK_RETRY_MS);
+ continue;
+ }
+
+ const dirPath = reclaimGateDirPath(lockPath);
+ try {
+ await fsp.mkdir(dirPath, { recursive: true });
+ } catch {
+ return null;
+ }
+ const filePath = path.join(dirPath, `gate-${process.pid}-${crypto.randomUUID()}.json`);
+ let lock: PublishedLock | null = null;
+ try {
+ lock = await publishLockRecord(
+ filePath,
+ (await getProcessIdentity(process.pid))?.key ?? null,
+ );
+ } catch {
+ return null;
+ }
+ if (!lock) return null;
+
+ const heartbeat = setInterval(() => {
+ const now = new Date();
+ void lock?.handle.utimes(now, now).catch(() => undefined);
+ }, LOCK_HEARTBEAT_MS);
+ heartbeat.unref?.();
+ const gate: ReclaimGate = { dirPath, filePath, lock, heartbeat };
+ for (;;) {
+ const winner = await findActiveReclaimGate(dirPath);
+ if (winner === filePath) return gate;
+ if (winner === null || Date.now() >= deadline) {
+ await releaseReclaimGate(gate);
+ break;
+ }
+ await sleep(LOCK_RETRY_MS);
+ }
+ if (Date.now() >= deadline) return null;
+ } while (true);
+}
+
+function sameRecord(left: LockRecord, right: LockRecord): boolean {
+ return left.pid === right.pid
+ && left.startedAt === right.startedAt
+ && left.nonce === right.nonce
+ && left.processStartIdentity === right.processStartIdentity
+ && left.state === right.state;
+}
+
+function sameLockOwner(left: LockRecord, right: LockRecord): boolean {
+ return left.pid === right.pid
+ && left.startedAt === right.startedAt
+ && left.nonce === right.nonce
+ && left.processStartIdentity === right.processStartIdentity;
+}
+
+function malformedLockIdentity(stat: {
+ dev: number | bigint;
+ ino: number | bigint;
+ size: number | bigint;
+ mtimeMs: number;
+}): MalformedLockIdentity {
+ return {
+ dev: Number(stat.dev),
+ ino: Number(stat.ino),
+ size: Number(stat.size),
+ mtimeMs: stat.mtimeMs,
+ };
+}
+
+function sameMalformedLockIdentity(
+ left: MalformedLockIdentity,
+ right: MalformedLockIdentity,
+): boolean {
+ return left.dev === right.dev
+ && left.ino === right.ino
+ && left.size === right.size
+ && left.mtimeMs === right.mtimeMs;
+}
+
+function sameStaleCandidate(
+ left: StaleLockCandidate | null,
+ right: StaleLockCandidate,
+): boolean {
+ if (!left || left.kind !== right.kind) return false;
+ if (left.kind === 'record' && right.kind === 'record') {
+ return sameRecord(left.record, right.record);
+ }
+ return left.kind === 'malformed'
+ && right.kind === 'malformed'
+ && sameMalformedLockIdentity(left.identity, right.identity);
+}
+
+async function pathMatchesStaleCandidate(
+ file: string,
+ expected: StaleLockCandidate,
+ requireCompleteRecord = false,
+): Promise<boolean> {
+ if (expected.kind === 'record') {
+ const moved = await readLockRecord(file, requireCompleteRecord);
+ return typeof moved !== 'string' && sameRecord(moved, expected.record);
+ }
+ try {
+ const [stat, moved] = await Promise.all([fsp.stat(file), readLockRecord(file)]);
+ return moved === 'malformed'
+ && sameMalformedLockIdentity(malformedLockIdentity(stat), expected.identity);
+ } catch {
+ return false;
+ }
+}
+
+async function isRecordOwnerActive(
+ record: LockRecord,
+ lockCreatedAtMs: number | null,
+): Promise<boolean> {
+ try {
+ process.kill(record.pid, 0);
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException)?.code;
+ if (code === 'ESRCH') return false;
+ if (code === 'EPERM' || code === 'EACCES') return true;
+ // Some Windows runtimes report EINVAL for signal 0. Let the process-start
+ // identity query decide when possible; if it also fails, the caller below
+ // remains fail-closed.
+ }
+ const currentIdentity = await getProcessIdentity(record.pid);
+ if (!currentIdentity) return true;
+ // Exact process identity is the strongest signal and must be checked before
+ // the self-pid shortcut: after a crash + PID reuse the OS may hand our pid to
+ // this process while the lock still records the dead instance's identity, so
+ // a mismatching identity on our own pid is a reused-pid takeover target (and
+ // skipping the comparison would leave strict callers permanently busy).
+ if (record.processStartIdentity) {
+ return processIdentitiesMatch(record.processStartIdentity, currentIdentity);
+ }
+ // No recorded identity: the current process is always the live holder of a
+ // lock it wrote — its start time cannot be compared against the lock's
+ // creation time reliably (a long-lived worker can predate the lock while a
+ // short-lived one starts after it), so a record naming our own pid is never
+ // a PID-reuse takeover target without an identity to disprove it.
+ if (record.pid === process.pid) return true;
+ // Legacy record without a processStartIdentity: kill(pid, 0) succeeded, so
+ // the pid is alive. We cannot distinguish a still-running original holder
+ // from a reused pid, and a live holder must never be squeezed out of its lock
+ // (it may be mid-drain/cleanup with an old mtime). Treat a live non-self pid
+ // as the active holder (fail closed). A PID-reuse takeover is only sound when
+ // the exact process identity was recorded in the lock, so this is the
+ // platform-independent, timing-independent safe answer.
+ return true;
+}
+
+async function getProcessIdentity(pid: number): Promise<ProcessIdentity | null> {
+ if (pid === process.pid) {
+ currentProcessIdentityPromise ??= readProcessIdentity(pid);
+ return currentProcessIdentityPromise;
+ }
+ return readProcessIdentity(pid);
+}
+
+async function readProcessIdentity(
+ pid: number,
+ runCommand: ExecFileRunner = execFileAsync as ExecFileRunner,
+): Promise<ProcessIdentity | null> {
+ if (process.platform === 'win32') {
+ const fromPowershell = await readWindowsIdentityWithPowershell(pid, runCommand);
+ if (fromPowershell) return fromPowershell;
+ const fromWmic = await readWindowsIdentityWithWmic(pid, runCommand);
+ if (fromWmic) return fromWmic;
+ return pid === process.pid ? estimateCurrentProcessIdentity() : null;
+ }
+ try {
+ const { stdout } = await runCommand(
+ 'ps',
+ ['-p', String(pid), '-o', 'lstart='],
+ {
+ encoding: 'utf8',
+ timeout: 5_000,
+ env: { ...process.env, LC_ALL: 'C', TZ: 'UTC0' },
+ },
+ );
+ const value = stdout.trim().replace(/\s+/g, ' ');
+ if (!value) return pid === process.pid ? estimateCurrentProcessIdentity() : null;
+ const parsed = Date.parse(`${value} UTC`);
+ return processIdentityFromStartMs(parsed, value);
+ } catch {
+ return pid === process.pid ? estimateCurrentProcessIdentity() : null;
+ }
+}
+
+async function readWindowsIdentityWithPowershell(
+ pid: number,
+ runCommand: ExecFileRunner,
+): Promise<ProcessIdentity | null> {
+ try {
+ const powershell = `${process.env.SystemRoot ?? 'C:\\Windows'}`
+ + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe';
+ const script =
+ `$p = Get-CimInstance Win32_Process -Filter \"ProcessId = ${pid}\" -ErrorAction Stop; `
+ + `if ($p) { $p.CreationDate.ToUniversalTime().Ticks }`;
+ const { stdout } = await runCommand(
+ powershell,
+ ['-NoProfile', '-NonInteractive', '-WindowStyle', 'Hidden', '-Command', script],
+ { encoding: 'utf8', timeout: 5_000, windowsHide: true },
+ );
+ const ticks = Number(stdout.trim());
+ if (!Number.isFinite(ticks)) return null;
+ return processIdentityFromStartMs(ticks / 10_000 - 62_135_596_800_000);
+ } catch {
+ return null;
+ }
+}
+
+async function readWindowsIdentityWithWmic(
+ pid: number,
+ runCommand: ExecFileRunner,
+): Promise<ProcessIdentity | null> {
+ try {
+ const wmic = `${process.env.SystemRoot ?? 'C:\\Windows'}\\System32\\wbem\\WMIC.exe`;
+ const { stdout } = await runCommand(
+ wmic,
+ ['process', 'where', `ProcessId=${pid}`, 'get', 'CreationDate', '/value'],
+ { encoding: 'utf8', timeout: 5_000, windowsHide: true },
+ );
+ const match = stdout.match(/CreationDate=(\d{14})\.(\d{1,6})([+-]\d{3})/i);
+ if (!match) return null;
+ const stamp = match[1];
+ const micros = match[2].padEnd(6, '0');
+ const offsetMinutes = Number(match[3]);
+ const localMs = Date.UTC(
+ Number(stamp.slice(0, 4)),
+ Number(stamp.slice(4, 6)) - 1,
+ Number(stamp.slice(6, 8)),
+ Number(stamp.slice(8, 10)),
+ Number(stamp.slice(10, 12)),
+ Number(stamp.slice(12, 14)),
+ Number(micros.slice(0, 3)),
+ );
+ return processIdentityFromStartMs(localMs - offsetMinutes * 60_000);
+ } catch {
+ return null;
+ }
+}
+
+function estimateCurrentProcessIdentity(): ProcessIdentity {
+ return processIdentityFromStartMs(Date.now() - process.uptime() * 1_000)!;
+}
+
+function processIdentityFromStartMs(
+ startedAtMs: number,
+ legacyKey?: string,
+): ProcessIdentity | null {
+ if (!Number.isFinite(startedAtMs) || startedAtMs <= 0) return null;
+ const normalized = Math.round(startedAtMs);
+ return { key: legacyKey ?? `start-ms:${normalized}`, startedAtMs: normalized };
+}
+
+function isValidNonce(value: string): boolean {
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
+}
+
+function parseRecordedProcessStartMs(key: string): number | null {
+ const normalized = /^start-ms:(\d+)$/.exec(key);
+ if (normalized) {
+ const value = Number(normalized[1]);
+ return value > 0 && Number.isFinite(value) ? value : null;
+ }
+ if (/^\d{15,}$/.test(key)) {
+ const ticks = Number(key);
+ const value = ticks / 10_000 - 62_135_596_800_000;
+ return Number.isFinite(value) && value > 0 ? value : null;
+ }
+ const parsed = Date.parse(`${key} UTC`);
+ return Number.isFinite(parsed) ? parsed : null;
+}
+
+function processIdentitiesMatch(recordedKey: string, current: ProcessIdentity): boolean {
+ if (recordedKey === current.key) return true;
+ const recordedStartedAtMs = parseRecordedProcessStartMs(recordedKey);
+ return recordedStartedAtMs !== null
+ && current.startedAtMs !== null
+ && Math.abs(recordedStartedAtMs - current.startedAtMs) <= LEGACY_PID_REUSE_TOLERANCE_MS;
+}
+
+async function markReleased(lock: PublishedLock): Promise<void> {
+ const released: LockRecord = { ...lock.record, state: 'released' };
+ const originalText = JSON.stringify(lock.record);
+ const releasedText = JSON.stringify(released);
+ try {
+ await writeAllAt(lock.handle, releasedText);
+ await lock.handle.truncate(Buffer.byteLength(releasedText));
+ await lock.handle.sync();
+ lock.record = released;
+ } catch (error) {
+ // A partial write must never be left as the canonical lock record. Restore
+ // the held record when possible; if that also fails, callers remain
+ // fail-closed and retain the nonce for the next safe recovery attempt.
+ try {
+ await writeAllAt(lock.handle, originalText);
+ await lock.handle.truncate(Buffer.byteLength(originalText));
+ await lock.handle.sync();
+ lock.record = { ...lock.record, state: 'held' };
+ } catch (restoreError) {
+ log.error('lock release record could not be restored after a short write', restoreError);
+ }
+ throw error;
+ }
+}
+
+async function writeAllAt(
+ handle: Awaited<ReturnType<typeof fsp.open>>,
+ text: string,
+): Promise<void> {
+ const bytes = Buffer.from(text, 'utf8');
+ let offset = 0;
+ while (offset < bytes.byteLength) {
+ const result = await handle.write(bytes, offset, bytes.byteLength - offset, offset);
+ if (result.bytesWritten <= 0) throw new Error('lock record write made no progress');
+ offset += result.bytesWritten;
+ }
+}
+
+async function cleanupPublishedLock(
+ lockPath: string,
+ label: string,
+ lock: PublishedLock,
+): Promise<void> {
+ const gate = await acquireReclaimGate(lockPath);
+ if (!gate) {
+ pendingOwnRecordRecovery.set(lockPath, lock.nonce);
+ await markReleased(lock).catch((error) => {
+ log.warn(`${label} lock could not be flushed before release`, error);
+ });
+ await lock.handle.close().catch(() => undefined);
+ log.warn(`${label} lock release gate is busy; leaving a recoverable released record`);
return;
}
- await fsp.rm(lock, { force: true }).catch(() => undefined);
+
+ const releasePath = `${lockPath}.release-${process.pid}-${crypto.randomUUID()}`;
+ try {
+ const current = await readLockRecord(lockPath);
+ if (typeof current === 'string' || !sameLockOwner(current, lock.record)) {
+ await lock.handle.close().catch(() => undefined);
+ log.warn(`${label} lock identity changed before release; preserving current lock`);
+ return;
+ }
+ try {
+ await markReleased(lock);
+ } catch (error) {
+ pendingOwnRecordRecovery.set(lockPath, lock.nonce);
+ log.warn(`${label} lock could not be flushed before release`, error);
+ // Keep the canonical record held until a later nonce-checked recovery.
+ // Moving an uncertain record would allow another process to acquire the
+ // lock without knowing whether the release state reached durable storage.
+ await lock.handle.close().catch(() => undefined);
+ return;
+ }
+ await lock.handle.close().catch(() => undefined);
+ try {
+ await fsp.rename(lockPath, releasePath);
+ } catch {
+ pendingOwnRecordRecovery.set(lockPath, lock.nonce);
+ return;
+ }
+ const moved = await readLockRecord(releasePath);
+ if (typeof moved !== 'string' && moved.nonce === lock.nonce) {
+ await removePathWithRetry(releasePath);
+ } else {
+ log.warn(`${label} lock identity changed during release; preserving the isolated record`);
+ }
+ } finally {
+ await releaseReclaimGate(gate);
+ }
+}
+
+async function releaseReclaimGate(gate: ReclaimGate): Promise<void> {
+ clearInterval(gate.heartbeat);
+ let releasedRecordDurable = false;
+ try {
+ await markReleased(gate.lock);
+ releasedRecordDurable = true;
+ } catch (error) {
+ log.warn('lock reclaim gate could not be flushed before release', error);
+ await publishGateReleaseMarker(gate).catch((markerError) => {
+ log.warn('lock reclaim gate release marker could not be published', markerError);
+ });
+ }
+ await gate.lock.handle.close().catch(() => undefined);
+ await removePathWithRetry(gate.filePath);
+ if (await pathExists(gate.filePath)) {
+ pendingOwnGateCleanup.add(gate.filePath);
+ } else if (!releasedRecordDurable) {
+ await removePathWithRetry(gateReleaseMarkerPath(gate.filePath));
+ }
+ try {
+ await fsp.rmdir(gate.dirPath);
+ } catch {
+ // Another contender may already be publishing in the directory.
+ }
+}
+
+async function publishGateReleaseMarker(gate: ReclaimGate): Promise<void> {
+ const markerPath = gateReleaseMarkerPath(gate.filePath);
+ let handle: Awaited<ReturnType<typeof fsp.open>> | null = null;
+ try {
+ handle = await fsp.open(markerPath, 'wx');
+ await writeAllAt(handle, JSON.stringify({
+ gateFile: path.basename(gate.filePath),
+ nonce: gate.lock.nonce,
+ }));
+ await handle.sync();
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error;
+ if (!(await hasValidGateReleaseMarker(gate.filePath))) throw error;
+ } finally {
+ await handle?.close().catch(() => undefined);
+ }
+}
+
+async function hasValidGateReleaseMarker(gatePath: string): Promise<boolean> {
+ try {
+ const value = JSON.parse(
+ await fsp.readFile(gateReleaseMarkerPath(gatePath), 'utf8'),
+ ) as unknown;
+ return Boolean(
+ value
+ && typeof value === 'object'
+ && !Array.isArray(value)
+ && (value as Record<string, unknown>).gateFile === path.basename(gatePath)
+ && typeof (value as Record<string, unknown>).nonce === 'string'
+ && isValidNonce((value as Record<string, unknown>).nonce as string),
+ );
+ } catch {
+ return false;
+ }
+}
+
+async function recoverPendingOwnRecord(file: string): Promise<void> {
+ const nonce = pendingOwnRecordRecovery.get(file);
+ if (!nonce) return;
+
+ const gate = await acquireReclaimGate(file);
+ if (!gate) return;
+ try {
+ const record = await readLockRecord(file);
+ if (record === 'missing' || (typeof record !== 'string' && record.nonce !== nonce)) {
+ pendingOwnRecordRecovery.delete(file);
+ return;
+ }
+ if (
+ typeof record === 'string'
+ || record.pid !== process.pid
+ || record.nonce !== nonce
+ ) {
+ return;
+ }
+
+ const recoveryPath = `${file}.recover-${process.pid}-${crypto.randomUUID()}`;
+ try {
+ await fsp.rename(file, recoveryPath);
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ if (code === 'ENOENT' || code === 'ENOTDIR') pendingOwnRecordRecovery.delete(file);
+ return;
+ }
+
+ const moved = await readLockRecord(recoveryPath);
+ if (typeof moved !== 'string' && moved.pid === process.pid && moved.nonce === nonce) {
+ pendingOwnRecordRecovery.delete(file);
+ await removePathWithRetry(recoveryPath);
+ return;
+ }
+
+ await restoreMovedPathSafely(recoveryPath, file);
+ } finally {
+ await releaseReclaimGate(gate);
+ }
+}
+
+async function pathExists(file: string): Promise<boolean> {
+ try {
+ await fsp.stat(file);
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+/** Restore a moved record without replacing a successor that won the path. */
+async function restoreMovedPathSafely(from: string, to: string): Promise<void> {
+ try {
+ await fsp.link(from, to);
+ await removePathWithRetry(from);
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException)?.code !== 'EEXIST') return;
+ }
+}
+
+async function removePathWithRetry(file: string): Promise<void> {
+ for (let attempt = 0; attempt < REMOVE_RETRY_ATTEMPTS; attempt += 1) {
+ try {
+ await fsp.rm(file, { force: true });
+ return;
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ if (code !== 'EPERM' && code !== 'EBUSY' && code !== 'EACCES') return;
+ if (attempt + 1 < REMOVE_RETRY_ATTEMPTS) await sleep(LOCK_RETRY_MS * (attempt + 1));
+ }
+ }
}
-export const __testing = { staleMs: LOCK_STALE_MS, heartbeatMs: LOCK_HEARTBEAT_MS };
+export const __testing = {
+ staleMs: LOCK_STALE_MS,
+ heartbeatMs: LOCK_HEARTBEAT_MS,
+ getProcessIdentity,
+ readProcessIdentity,
+ parseRecordedProcessStartMs,
+ processIdentitiesMatch,
+};