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。

fix(desktop): make cross-process lock legacy takeover fail closed

Yuewei-LLae5cfcb40a66
diff --git a/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts b/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts
index 81b30234a2..05ca7a4c15 100644
--- a/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts
@@ -147,7 +147,7 @@ describe('接管陈旧锁', () => {
).resolves.toEqual({ held: false, reason: 'busy' });
});
- it('reclaims a legacy lock when the recorded pid was reused after file creation', async () => {
+ it('reclaims a lock when the recorded process identity no longer matches the live pid', async () => {
const lock = path.join(dir, 'lock');
await fsp.writeFile(lock, '{}', 'utf8');
await new Promise((resolve) => setTimeout(resolve, 2_100));
@@ -161,7 +161,22 @@ describe('接管陈旧锁', () => {
child.once('error', reject);
});
if (!child.pid) throw new Error('child pid missing');
- await fsp.writeFile(lock, JSON.stringify({ pid: child.pid, startedAt: 1 }), 'utf8');
+ // The lock records a process identity that cannot match the live pid's
+ // current identity ('start-ms:0' parses to null → never equal). This is
+ // the only sound signal for a PID-reuse takeover: a live pid alone is
+ // treated as the active holder (fail closed), because a still-running
+ // drain/cleanup owner must not be squeezed out.
+ await fsp.writeFile(
+ lock,
+ JSON.stringify({
+ pid: child.pid,
+ startedAt: 1,
+ processStartIdentity: 'start-ms:0',
+ nonce: '00000000-0000-4000-8000-000000000001',
+ state: 'held',
+ }),
+ 'utf8',
+ );
const old = new Date(Date.now() - 60_000);
await fsp.utimes(lock, old, old);
diff --git a/apps/desktop/src/main/device-link/crossProcessLock.ts b/apps/desktop/src/main/device-link/crossProcessLock.ts
index 157c5ecbe4..a95fdd560f 100644
--- a/apps/desktop/src/main/device-link/crossProcessLock.ts
+++ b/apps/desktop/src/main/device-link/crossProcessLock.ts
@@ -244,9 +244,14 @@ async function inspectStaleLock(
? { 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
- : null;
+ : (Number.isFinite(stat.mtimeMs) && stat.mtimeMs > 0 ? stat.mtimeMs : null);
return (await isRecordOwnerActive(record, createdAtMs))
? null
: { kind: 'record', record };
@@ -503,7 +508,7 @@ async function findActiveReclaimGate(dirPath: string): Promise<string | null> {
const fresh = Date.now() - stat.mtimeMs <= LOCK_STALE_MS;
const createdAtMs = Number.isFinite(stat.birthtimeMs) && stat.birthtimeMs > 0
? stat.birthtimeMs
- : null;
+ : (Number.isFinite(stat.mtimeMs) && stat.mtimeMs > 0 ? stat.mtimeMs : null);
if (fresh || await isRecordOwnerActive(record, createdAtMs)) {
active.push({ filePath, record });
} else {
@@ -678,12 +683,22 @@ async function isRecordOwnerActive(
}
const currentIdentity = await getProcessIdentity(record.pid);
if (!currentIdentity) return true;
+ // 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.
+ if (record.pid === process.pid) return true;
if (record.processStartIdentity) {
return processIdentitiesMatch(record.processStartIdentity, currentIdentity);
}
- return currentIdentity.startedAtMs === null
- || lockCreatedAtMs === null
- || currentIdentity.startedAtMs <= lockCreatedAtMs + LEGACY_PID_REUSE_TOLERANCE_MS;
+ // 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> {
diff --git a/docs/dev-rules/plugin-approval-main-merge-2026-08-10.md b/docs/dev-rules/plugin-approval-main-merge-2026-08-10.md
index 863a616b7a..4e623beeb4 100644
--- a/docs/dev-rules/plugin-approval-main-merge-2026-08-10.md
+++ b/docs/dev-rules/plugin-approval-main-merge-2026-08-10.md
@@ -102,6 +102,44 @@ main's changes without weakening fail-closed behavior.
--config.engine-strict=false` links the workspace without touching the
lockfile. The lockfile in the merge matches `origin/main` byte-for-byte.
+## Post-merge fixes (2026-08-10 round 2)
+
+This round fixed two Linux CI failures plus one Codex review P1:
+
+- **crossProcessLock legacy PID-reuse takeover was platform-dependent and
+ unsound.** `isRecordOwnerActive` decided a legacy record's liveness from the
+ lock file's `birthtimeMs`; on filesystems where birthtime is unavailable or 0
+ the same legacy takeover decided differently from platforms with a real
+ birthtime, so `reclaims a legacy lock when the recorded pid was reused`
+ failed on Linux, and a mtime-based fallback made the "live owner must not be
+ taken over" guard timing-dependent on the parent's age. A PID-reuse takeover
+ cannot be proven soundly from a legacy record (a live pid could be the
+ original still-running holder mid-drain with a stale mtime). Fix:
+ `isRecordOwnerActive` now treats a live non-self pid (kill(pid,0) succeeds)
+ as the active holder unconditionally (fail closed, timing-independent); a
+ PID-reuse takeover is only accepted when the lock's recorded
+ `processStartIdentity` demonstrably does not match the current process. The
+ PID-reuse test was updated to record a mismatching identity (`start-ms:0`)
+ instead of relying on a sentinel startedAt + stale mtime. This is
+ cross-platform stable and never squeezes out a live drain/cleanup owner.
+- **Silent default upgrade minted a fresh receipt for non-approved installs.**
+ `applyDefaultUpgrades` passed `expectedInstalledApproval` +
+ `allowPermissionExpansion` for `legacy-unapproved`/`invalid` org defaults,
+ letting `installDetail` mint a new approved receipt without user reapproval
+ (Codex P1). Fix: skip the silent upgrade when `approval.state !== 'approved'`
+ and leave the plugin in `update-available` so it goes through the
+ reapproval/recovery flow. Added a regression test covering both
+ `legacy-unapproved` and `invalid`.
+- **`readBoundedFile.test.ts` carried two merge-introduced assertions that
+ contradicted the implementation.** Two tests asserted every `readBoundedFile*`
+ open passes `O_NONBLOCK`, but the no-follow variants only enable it via the
+ `nonBlocking` option (and the synchronous variant has no such option); only
+ `readBoundedFileFollowLinks` always opens non-blocking. These assertions do
+ not exist in `origin/main`'s test file and were introduced by the merge.
+ Fixed the assertions to match the implementation: no-follow defaults to
+ blocking, `{ nonBlocking: true }` opts in, follow-links always non-blocking.
+ No production code changed.
+
## Post-merge gate
After all conflicts are resolved: inspect the complete diff, verify no unmerged