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(worktree): preserve markers with ambiguous legacy file identity

Lizicfe6c64d722a
diff --git a/apps/desktop/src/main/__tests__/fixtures/legacySharedRuntimeLease.ts b/apps/desktop/src/main/__tests__/fixtures/legacySharedRuntimeLease.ts
index 27d0d8cfdd..8a5bc16dd3 100644
--- a/apps/desktop/src/main/__tests__/fixtures/legacySharedRuntimeLease.ts
+++ b/apps/desktop/src/main/__tests__/fixtures/legacySharedRuntimeLease.ts
@@ -4,7 +4,7 @@ import path from 'node:path';
import type { WorktreeRuntimeLease } from '../../worktree/runtimeLeases';
import { physicalWorktreeKey } from '../../worktree/resourceLock';
-/** Seed persisted old-client evidence; production no longer creates shared leases or Git locks. */
+/** Seed persisted shared evidence with an exact identity; production no longer creates shared leases or Git locks. */
export async function seedLegacySharedRuntimeLease(
appData: string,
worktree: string,
@@ -14,11 +14,11 @@ export async function seedLegacySharedRuntimeLease(
await fs.mkdir(directory, { recursive: true });
await fs.writeFile(gitLock, JSON.stringify({ kind: 'cindy-runtime-lock', version: 1, nonce: 'legacy-fixture' }), { flag: 'wx' })
.catch((error) => { if (error.code !== 'EEXIST') throw error; });
- const identity = await fs.lstat(gitLock);
+ const identity = await fs.lstat(gitLock, { bigint: true });
const keepSentinel = {
file: gitLock,
content: await fs.readFile(gitLock, 'utf8'),
- identity: `${identity.dev}:${identity.ino}:${identity.birthtimeMs}`,
+ identity: `bigint:${identity.dev}:${identity.ino}:${identity.birthtimeNs}`,
};
const file = path.join(directory, `${process.pid}-${randomUUID().replaceAll('-', '').repeat(2)}.json`);
const physicalPath = await physicalWorktreeKey(worktree);
diff --git a/apps/desktop/src/main/__tests__/worktreeRuntimeSafety.test.ts b/apps/desktop/src/main/__tests__/worktreeRuntimeSafety.test.ts
index 19b1332d21..f20a9e15d2 100644
--- a/apps/desktop/src/main/__tests__/worktreeRuntimeSafety.test.ts
+++ b/apps/desktop/src/main/__tests__/worktreeRuntimeSafety.test.ts
@@ -161,6 +161,45 @@ describe('worktree runtime evidence and physical locks', () => {
expect(await fs.readFile(file, 'utf8')).toBe(change === 'edited' ? 'keep my checkout' : lease.keepSentinel!.content);
});
+ it('distinguishes exact file IDs that collide as Numbers when creation time is inherited', async () => {
+ const lease = await seedLegacyLease();
+ const oldIno = 9_007_199_254_740_992n;
+ const replacementIno = oldIno + 1n;
+ expect(Number(oldIno)).toBe(Number(replacementIno));
+ const lstat = fs.lstat.bind(fs);
+ vi.spyOn(fs, 'lstat').mockImplementation(async (...args) => {
+ const identity = await lstat(...args);
+ if (args[0] === gitLock) Object.assign(identity, { dev: 1n, ino: replacementIno, birthtimeNs: 42_000_000n });
+ return identity;
+ });
+ await releaseWorktreeRuntimeLease({
+ ...lease, keepSentinel: { ...lease.keepSentinel!, identity: `bigint:1:${oldIno}:42000000` },
+ });
+ expect(await hasGitLock()).toBe(true);
+ expect(await fs.readFile(gitLock, 'utf8')).toBe(lease.keepSentinel!.content);
+ });
+
+ it.each([
+ { label: 'matching safe legacy ID', recordedIno: 123, observedIno: 123, preserved: false },
+ { label: 'replaced safe legacy ID', recordedIno: 123, observedIno: 124, preserved: true },
+ { label: 'rounded legacy ID', recordedIno: 2 ** 53, observedIno: 2 ** 53, preserved: true },
+ { label: 'missing legacy ID', recordedIno: 0, observedIno: 0, preserved: true },
+ ])('releases old receipts only with unambiguous ownership: $label', async ({ recordedIno, observedIno, preserved }) => {
+ const lease = await seedLegacyLease();
+ const lstat = fs.lstat.bind(fs);
+ vi.spyOn(fs, 'lstat').mockImplementation(async (...args) => {
+ const identity = await lstat(...args);
+ if (args[0] === gitLock) Object.assign(identity, { dev: 1, ino: observedIno, birthtimeMs: 42 });
+ return identity;
+ });
+ await releaseWorktreeRuntimeLease({
+ ...lease, keepSentinel: { ...lease.keepSentinel!, identity: `1:${recordedIno}:42` },
+ });
+ expect(await hasGitLock()).toBe(preserved);
+ if (preserved) expect(await fs.readFile(gitLock, 'utf8')).toBe(lease.keepSentinel!.content);
+ await expect(fs.stat(lease.file)).rejects.toMatchObject({ code: 'ENOENT' });
+ });
+
it('does not remove legacy protection when another borrower crashes', async () => {
const crashed = await seedLegacyLease();
const active = await seedLegacyLease();
diff --git a/apps/desktop/src/main/worktree/runtimeLeases.ts b/apps/desktop/src/main/worktree/runtimeLeases.ts
index 3bba6045b3..4c50568c2b 100644
--- a/apps/desktop/src/main/worktree/runtimeLeases.ts
+++ b/apps/desktop/src/main/worktree/runtimeLeases.ts
@@ -40,6 +40,7 @@ interface RuntimeKeepSentinel {
/** Older release receipts omit this and refer to the in-tree keep marker. */
file?: string;
content: string;
+ /** Legacy numeric dev:ino:birthtimeMs, or lossless bigint:dev:ino:birthtimeNs. */
identity: string;
}
@@ -85,10 +86,21 @@ async function releaseRuntimeKeepSentinel(lease: WorktreeRuntimeLease): Promise<
if (!marker) return;
const file = marker.file ?? path.join(lease.physicalPath, '.worktree-keep');
try {
- const identity = await fs.lstat(file);
- if (!identity.isFile() || identity.isSymbolicLink()
- || `${identity.dev}:${identity.ino}:${identity.birthtimeMs}` !== marker.identity
- || await fs.readFile(file, 'utf8') !== marker.content) return;
+ if (marker.identity.startsWith('bigint:')) {
+ const identity = await fs.lstat(file, { bigint: true });
+ if (!identity.isFile() || identity.isSymbolicLink() || identity.ino === 0n
+ || `bigint:${identity.dev}:${identity.ino}:${identity.birthtimeNs}` !== marker.identity) return;
+ } else {
+ const identity = await fs.lstat(file);
+ // NTFS file IDs can exceed Number's exact integer range, and replacement
+ // files can inherit the old creation time. A rounded legacy ID cannot
+ // prove ownership: preserve that marker rather than delete a user's lock.
+ if (!identity.isFile() || identity.isSymbolicLink()
+ || !Number.isSafeInteger(identity.ino) || identity.ino <= 0
+ || !Number.isSafeInteger(identity.dev)
+ || `${identity.dev}:${identity.ino}:${identity.birthtimeMs}` !== marker.identity) return;
+ }
+ if (await fs.readFile(file, 'utf8') !== marker.content) return;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return;
throw error;
diff --git a/docs/dev-rules/credentials-and-local-storage.md b/docs/dev-rules/credentials-and-local-storage.md
index c3fcc45534..950012c7b6 100644
--- a/docs/dev-rules/credentials-and-local-storage.md
+++ b/docs/dev-rules/credentials-and-local-storage.md
@@ -192,7 +192,7 @@ Desktop 的运行时 refresh 遇到瞬时失败后,主动调用与定时重试
| 跨 profile 的 Cindy 内置 Skill 副本 | `app.getPath('appData')/Cindy/shared-system-skills`,只保存随应用发布、可由 bundle 重建的官方 Skill;Global、China、dev 与 isolated profile 共用稳定物理路径,更新和共享发现链接必须持有下述互斥锁 |
| 跨 profile 的共享 Skill 文件互斥 | `app.getPath('appData')/Cindy/shared-skill-mutation-locks`,仅存文件锁及未完成操作的 token/名称哈希,保证正式版/dev/isolated 共用;短期锁复用既有崩溃回收,持久屏障必须等对应清理完成后删除,读取损坏只阻止相关名称 |
| 旧跨 profile 的 worktree 借用租约 | `app.getPath('appData')/Cindy/shared-worktree-runtime-leases`,内置模拟器下线后不再创建共享租约;回收器仍读取旧证据并重试已有 `.release` 回执,不能因进程退出就移除保护。普通 Agent 的当前 profile 租约照常创建与释放 |
-| 旧版 worktree 回收器兼容锁 | 不再创建新的跨 profile Git 锁;保留旧回执对 `<commonGitDir>/worktrees/<id>/locked` 及更早 `.worktree-keep` 的清理。仅当最后一个共享借用结束且自建文件身份和内容仍匹配时删除,不覆盖用户锁;失败沿用 `.release` 重试 |
+| 旧版 worktree 回收器兼容锁 | 不再创建新的跨 profile Git 锁;保留旧回执对 `<commonGitDir>/worktrees/<id>/locked` 及更早 `.worktree-keep` 的清理。仅当最后一个共享借用结束且自建文件身份和内容仍匹配时删除,不覆盖用户锁;失败沿用 `.release` 重试。支持精确的 `bigint:dev:ino:birthtimeNs` 身份;旧数字编号为 0 或超过安全整数范围时无法证明归属,保留锁但仍释放已结束的租约,避免 Windows 编号精度丢失误删用户替换文件 |
| 跨 profile 的 worktree 回收日志位置 | `app.getPath('appData')/Cindy/shared-worktree-recycle-journals`,仍按日志目录哈希发布原 profile 日志位置,兼容可能同时运行的旧客户端;新客户端不再读取其他 profile 的日志来借用工程。不复制恢复状态、不代替 owner 执行恢复 |
| 供应商分享:分享者电脑上的受邀者用量 | `userData/remote-agent/provider-share-usage.json`,按(日期、分享、成员、Agent、供应商、模型)聚合 token 与轮次,行内 `byMember` 按组内电脑的键再分(2026-10-11 加;行键与合计不变,旧版本照常累计并原样写回,合计多出各台之和的部分算没区分电脑),原子替换写入,保留 400 天;只含服务端生成的分享与成员 id,不含昵称、对话或凭证。成员删除后保留(管理页不再显示),同一人重新加入时接续 |
| 供应商按使用方的用量 | `userData/owners/<账号>/provider-usage-by-party.json`,按(日期、供应商、使用方、设备 id、组内电脑的键、Agent、模型)聚合 token 与轮次。使用方:本机、同账号另一台电脑、经同账号组所在电脑转来的受邀者任务、本机经另一台电脑的组在别的组内电脑上运行的(只用来报告给那台组所在电脑)、同账号电脑报告来的(每次报告整体替换那台从某天起的行);没有使用方字段的旧行按设备 id 推出。原子替换写入,保留 400 天;只含供应商 id、设备 id、组内电脑的键与模型名,不含对话或凭证。按账号存放(供应商 id 在不同账号下会重复),从开始记录起不补历史 |