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): 绑定文件读不出来时不认领,Fast 判定改用校准后的生效来源

Dash5872d30f0569
diff --git a/apps/desktop/src/main/maker-host/__tests__/nativeProviderAuthBinding.test.ts b/apps/desktop/src/main/maker-host/__tests__/nativeProviderAuthBinding.test.ts
index e34a46df21..0e639a5a4a 100644
--- a/apps/desktop/src/main/maker-host/__tests__/nativeProviderAuthBinding.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/nativeProviderAuthBinding.test.ts
@@ -180,6 +180,31 @@ describe('claimDetectedNativeProviderAuth', () => {
expect(claimDetectedNativeProviderAuth('anthropic', () => true)).toBe(true);
});
+ it('绑定文件读不出来时不认领,也不覆盖它', () => {
+ // 「归属信息丢失」不等于「没人绑过」。把损坏当空,等于在最不该下判断的时刻把共享
+ // keychain 里的凭证判给当前账号,随后的写入还会把原有归属彻底盖掉(PR #548 review)。
+ fs.mkdirSync(userDataDir, { recursive: true });
+ fs.writeFileSync(bindingFile, '{ this is not json');
+
+ expect(claimDetectedNativeProviderAuth('anthropic', () => true)).toBe(false);
+ expect(isNativeProviderAuthBound('anthropic')).toBe(false);
+ expect(fs.readFileSync(bindingFile, 'utf8')).toBe('{ this is not json');
+
+ // 一次性 legacy 迁移同样不推进 —— 它还会顺手消费掉 legacyClaimOwner 名额。
+ migrateLegacyNativeProviderAuthBindings('owner-a', { anthropic: true });
+ expect(fs.readFileSync(bindingFile, 'utf8')).toBe('{ this is not json');
+
+ // JSON 合法但根不是对象(数组 / 标量)同样按不可读处理。
+ fs.writeFileSync(bindingFile, '["owner-a"]');
+ expect(claimDetectedNativeProviderAuth('anthropic', () => true)).toBe(false);
+ });
+
+ it('文件确实不存在 = 合法首次状态,照常认领', () => {
+ // 与「读失败」必须分开:ENOENT 是全新安装的正常形态,挡掉它等于把自动继承整条废掉。
+ expect(fs.existsSync(bindingFile)).toBe(false);
+ expect(claimDetectedNativeProviderAuth('anthropic', () => true)).toBe(true);
+ });
+
it('treats corrupted falsy slot values as claimed-by-unknown and fails closed', () => {
// 键存在但值为假(损坏 / 异常写入):按「归属不明」拒绝,绝不重认领。
fs.mkdirSync(userDataDir, { recursive: true });
diff --git a/apps/desktop/src/main/maker-host/nativeProviderAuthBinding.ts b/apps/desktop/src/main/maker-host/nativeProviderAuthBinding.ts
index f61145a3c2..74c235f775 100644
--- a/apps/desktop/src/main/maker-host/nativeProviderAuthBinding.ts
+++ b/apps/desktop/src/main/maker-host/nativeProviderAuthBinding.ts
@@ -26,15 +26,38 @@ function bindingPath(): string {
return path.join(app.getPath('userData'), 'native-provider-auth.json');
}
-function readBindings(): BindingFile {
+/**
+ * 读绑定文件,**区分「确实还没有这个文件」与「有但读不出来」**。
+ *
+ * 只读判定(isNativeProviderAuthBound)两者都当空处理即可——空 = 未绑定 = fail-closed。
+ * 但认领路径不行:把损坏 / 不可读一律当成「名额空着」,等于在归属信息丢失的那一刻
+ * 把共享 keychain 里的凭证判给当前账号,而且随后的 writeBindings 会把损坏文件连同
+ * 里面原有的归属一起覆盖掉,永久失去恢复依据(PR #548 review)。
+ */
+function readBindingsOrFail(): { ok: true; bindings: BindingFile } | { ok: false } {
+ let raw: string;
+ try {
+ raw = fs.readFileSync(bindingPath(), 'utf8');
+ } catch (err) {
+ // 文件不存在 = 合法的首次状态(还没有任何人绑定过);其它读失败(EACCES / EIO 等)
+ // 说明归属不明,不能当成空。
+ if ((err as NodeJS.ErrnoException | null)?.code === 'ENOENT') return { ok: true, bindings: {} };
+ return { ok: false };
+ }
try {
- const value = JSON.parse(fs.readFileSync(bindingPath(), 'utf8')) as unknown;
- return value && typeof value === 'object' ? (value as BindingFile) : {};
+ const value = JSON.parse(raw) as unknown;
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return { ok: false };
+ return { ok: true, bindings: value as BindingFile };
} catch {
- return {};
+ return { ok: false };
}
}
+function readBindings(): BindingFile {
+ const read = readBindingsOrFail();
+ return read.ok ? read.bindings : {};
+}
+
function writeBindings(value: BindingFile): void {
const file = bindingPath();
fs.mkdirSync(path.dirname(file), { recursive: true });
@@ -96,7 +119,11 @@ export function migrateLegacyNativeProviderAuthBindings(
ownerId: string,
available: Partial<Record<NativeProviderId, boolean>>,
): void {
- const bindings = readBindings();
+ // 同 claimDetectedNativeProviderAuth:一次性迁移也是写路径,归属读不出来就不能推进
+ // (还会把 legacyClaimOwner 名额一起消费掉,损失不可逆)。
+ const read = readBindingsOrFail();
+ if (!read.ok) return;
+ const bindings = read.bindings;
if (bindings.legacyClaimOwner) return;
const next: BindingFile = { ...bindings, legacyClaimOwner: ownerId };
@@ -139,7 +166,11 @@ export function claimDetectedNativeProviderAuth(
// Callers reached from an async settle (Codex reconcile) additionally pin an
// owner+generation snapshot; this guard is the floor every caller gets.
if (isAppSessionBoundaryPending()) return false;
- const bindings = readBindings();
+ // 归属文件读不出来 = 归属不明,一律不认领:这条路径是**写**路径,把损坏当空会把共享
+ // keychain 里可能属于别人的凭证判给当前账号,并覆盖掉原有归属(PR #548 review)。
+ const read = readBindingsOrFail();
+ if (!read.ok) return false;
+ const bindings = read.bindings;
// Key-presence, not truthiness: a corrupted/empty-string slot must count as
// "claimed by unknown" and fail closed, never as re-claimable (matches
// unbindNativeProviderAuth's `in` pattern).
diff --git a/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx b/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx
index 22e6f64be6..6ca2697fb6 100644
--- a/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx
@@ -865,11 +865,16 @@ export function NewMakerDraftRoute() {
// Fast 可用判定:本地 + device-link 统一走 resolveFastSupported(共享 per-provider 逻辑,
// 控制端不另写远程判断;旧被控端回退拍平 caps 见 helper)。device-link 的来源/模型取被控端镜像
- // (dlSel live > deviceLinkInitial seed),本地取 chatPrefs —— 即"被控端会话实际会路由到的来源"。
+ // (dlSel live > deviceLinkInitial seed),本地取**校准后的生效来源** —— 即会话实际会路由
+ // 到的那个来源。
const supportsFastMode = useMemo(() => {
+ // 本地不能用 chatPrefs.providerId:它常是 null,而 fast 是 per-provider 能力。默认来源
+ // 被隐藏 / SSH 排除、模型由另一个来源提供时,这里会去查那个被排除的来源 —— 要么藏掉本
+ // 该有的 Fast 开关,要么把 Fast 开在不支持它的来源上。effort / fast 记忆 / 写回都已按
+ // effectiveSourceId 推导,只剩这处没跟上(PR #548 review)。
const providerId = isDeviceLinkDraft
? (dlSel?.providerId ?? deviceLinkInitial?.providerId ?? null)
- : (chatPrefs.providerId ?? null);
+ : effectiveSourceId;
// 本地取**校准后**的模型:fast 能力必须按最终提交的那个模型判定。
const modelId = isDeviceLinkDraft
? (dlSel?.model ?? deviceLinkInitial?.model ?? chatPrefs.model)
@@ -887,7 +892,7 @@ export function NewMakerDraftRoute() {
isDeviceLinkDraft,
dlSel,
deviceLinkInitial,
- chatPrefs.providerId,
+ effectiveSourceId,
chatPrefs.model,
calibratedDraftModel,
effectiveDeviceLinkDeviceId,