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(companion): let a cloud take-over coexist with a same-named local companion

nana66d8529fe92e
diff --git a/apps/desktop/src/main/companion-cloud/host.ts b/apps/desktop/src/main/companion-cloud/host.ts
index 58ee90dfba..6b53c1465a 100644
--- a/apps/desktop/src/main/companion-cloud/host.ts
+++ b/apps/desktop/src/main/companion-cloud/host.ts
@@ -205,7 +205,7 @@ function createMaterializer(input: {
fence.assertCurrent();
await createBotProfile({ id, name: name.slice(0, 200), description, identitySource, userContextSource,
...('portable' in restored ? { avatar: restored.portable } : { avatarImageBase64: restored.imageBase64 }) },
- { templateId });
+ { templateId, allowSameName: true });
},
updateProfile: async ({ id, name, description, identitySource, userContextSource, avatar, templateId }, fence) => {
const restored = await cloudAvatar(avatar, fence.signal);
@@ -214,7 +214,7 @@ function createMaterializer(input: {
...(description !== undefined ? { description } : {}),
...(identitySource !== undefined ? { identitySource } : {}),
...(userContextSource !== undefined ? { userContextSource } : {}),
- ...('portable' in restored ? { avatar: restored.portable } : {}) }, undefined, undefined, { templateId, replacesUnfinishedMeeting: true });
+ ...('portable' in restored ? { avatar: restored.portable } : {}) }, undefined, undefined, { templateId, replacesUnfinishedMeeting: true, allowSameName: true });
if ('imageBase64' in restored) {
const image = decodeBotAvatarImage(restored.imageBase64);
fence.assertCurrent();
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
index d498a51abf..170ca2d054 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
@@ -988,6 +988,55 @@ describe('Bot canonical Session lifecycle', () => {
});
});
+ // cloud-companions.md §3.1 (2026-10-26): a companion taken over from another computer may share its
+ // name with a different companion already here; the two stay independent.
+ describe('cloud take-over next to a same-named local companion', () => {
+ const names = () => h.sqlite!.prepare("SELECT id FROM bot_profiles WHERE display_name = 'Release Bot' ORDER BY id").pluck().all();
+
+ it('creates the copy without the name check', async () => {
+ const { createBotProfile } = await import('../bots');
+ const created = await createBotProfile({ id: 'import_other', name: 'release bot' }, { allowSameName: true });
+ expect(created.id).toBe('import_other');
+ await createBotProfile({ id: 'import_third', name: 'Release Bot' }, { allowSameName: true });
+ expect(names()).toEqual(['bot-1', 'import_third']);
+ });
+
+ it('still refuses a same-named ordinary creation', async () => {
+ const { createBotProfile } = await import('../bots');
+ await expect(createBotProfile({ id: 'import_other', name: 'Release Bot' })).rejects.toMatchObject({ code: 'ALREADY_EXISTS' });
+ await expect(createBotProfile({ id: 'import_other', name: 'Release Bot' }, { templateId: undefined })).rejects.toMatchObject({ code: 'ALREADY_EXISTS' });
+ expect(names()).toEqual(['bot-1']);
+ });
+
+ it('does not let a renderer or model body switch the name check off', async () => {
+ const { createBotProfile } = await import('../bots');
+ for (const flag of ['allowSameName', 'allowDuplicateName']) {
+ await expect(invoke('local-db:bots:create', { id: 'import_other', name: 'Release Bot', [flag]: true }))
+ .rejects.toMatchObject({ code: 'ALREADY_EXISTS' });
+ await expect(createBotProfile({ id: 'import_other', name: 'Release Bot', [flag]: true })).rejects.toMatchObject({ code: 'ALREADY_EXISTS' });
+ }
+ expect(names()).toEqual(['bot-1']);
+ });
+
+ it('keeps the default Cindy unique for a take-over', async () => {
+ const { createBotProfile } = await import('../bots');
+ await invoke('local-db:bots:create', { id: 'local-cindy', name: 'Cindy', templateId: 'cindy' });
+ await expect(createBotProfile({ id: 'import_cindy', name: 'Cindy 2' }, { templateId: 'cindy', allowSameName: true }))
+ .rejects.toMatchObject({ code: 'ALREADY_EXISTS' });
+ expect(h.sqlite!.prepare("SELECT COUNT(*) FROM bot_profiles WHERE id = 'import_cindy'").pluck().get()).toBe(0);
+ });
+
+ it('takes over an existing copy whose cloud name now matches another local companion', async () => {
+ await invoke('local-db:bots:create', { id: 'import_copy', name: 'Copy' });
+ await expect(updateStoredBotProfile({ id: 'import_copy', name: 'Release Bot' })).rejects.toMatchObject({ code: 'ALREADY_EXISTS' });
+ await expect(invoke('local-db:bots:update', { id: 'import_copy', name: 'Release Bot', allowSameName: true }))
+ .rejects.toMatchObject({ code: 'ALREADY_EXISTS' });
+ const updated = await updateStoredBotProfile({ id: 'import_copy', name: 'Release Bot' }, undefined, undefined, { allowSameName: true });
+ expect(updated.name).toBe('Release Bot');
+ expect(names()).toEqual(['bot-1', 'import_copy']);
+ });
+ });
+
it('allows profile edits while an existing companion retries only its portrait', async () => {
await invoke('local-db:bots:create-canonical-session', {
botId: 'bot-1', expectedCanonicalSessionId: null, expectedProfileVersion: 1,
diff --git a/apps/desktop/src/main/localDb/ipc/bots.ts b/apps/desktop/src/main/localDb/ipc/bots.ts
index dde303e182..3adf0502db 100644
--- a/apps/desktop/src/main/localDb/ipc/bots.ts
+++ b/apps/desktop/src/main/localDb/ipc/bots.ts
@@ -1765,10 +1765,11 @@ export async function setBotProfileAvatar(botId: string, image: { buffer: Buffer
/**
* Trusted, main-only inputs of a companion restored from the cloud (cloud-companions.md §3.1):
- * the preset it was made from, and whether the cloud profile replaces a copy whose first meeting
- * never finished. Never reachable from a renderer or model body.
+ * the preset it was made from, whether the cloud profile replaces a copy whose first meeting
+ * never finished, and whether its name may equal another companion's here (the display-name check is
+ * skipped; the default Cindy stays unique). Never reachable from a renderer or model body.
*/
-export interface RestoredBotOrigin { templateId?: unknown; replacesUnfinishedMeeting?: true }
+export interface RestoredBotOrigin { templateId?: unknown; replacesUnfinishedMeeting?: true; allowSameName?: true }
/** Main-owned creation path shared by the renderer and Bot runtime tools. */
export async function createBotProfile(raw: unknown, restored?: RestoredBotOrigin) {
@@ -1885,6 +1886,7 @@ export async function createBotProfile(raw: unknown, restored?: RestoredBotOrigi
avatarColor,
identitySource,
capabilitiesJson: safeJson(persistedCapabilities),
+ ...(restored?.allowSameName === true ? { allowDuplicateName: true } : {}),
now,
});
} catch (error) {
@@ -2113,6 +2115,7 @@ export async function updateBotProfile(raw: unknown, expectedVersion?: number,
id,
...(modelSelectionChanged ? { clearScheduledModelBaseline: true } : {}),
...(changesPermissions ? { canonicalPermissionMode: botSessionPermissionMode(normalizedNextConfig) } : {}),
+ ...(restored?.allowSameName === true ? { allowDuplicateName: true } : {}),
...(patch.displayName !== undefined ? { displayName: patch.displayName } : {}),
...(patch.description !== undefined ? { description: patch.description } : {}),
...(patch.avatar !== undefined ? { avatar: patch.avatar } : {}),
diff --git a/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts b/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts
index 11a8cd4d6a..1c492d5430 100644
--- a/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts
+++ b/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts
@@ -320,16 +320,24 @@ function projectAliasesReplaceIdentity(
})();
}
+/** Main → worker internal flag: absent keeps the original behavior. */
+function expectOptionalFlag(value: unknown, name: string): boolean {
+ if (value === undefined) return false;
+ if (typeof value !== 'boolean') throw invalidArgs(`${name} must be a boolean`);
+ return value;
+}
+
function botsCreateProfile(db: Database.Database, args: unknown): void {
const p = asRecord(args, 'bots.createProfile args');
const id = expectString(p.id, 'id');
const now = expectNumber(p.now, 'now');
+ const allowDuplicateName = expectOptionalFlag(p.allowDuplicateName, 'allowDuplicateName');
db.transaction(() => {
const config = JSON.parse(expectString(p.capabilitiesJson, 'capabilitiesJson'));
const rows = db.prepare(`SELECT p.id, p.display_name AS name, v.capabilities_json AS config, v.identity_source AS identity
FROM bot_profiles p JOIN bot_profile_versions v ON v.bot_id = p.id AND v.version = p.current_version
WHERE p.status != 'archived'`).all() as Array<{ id: string; name: string; config: string; identity: string }>;
- if (rows.some(row => normalizeBotName(row.name) === normalizeBotName(expectString(p.displayName, 'displayName'))) ||
+ if ((!allowDuplicateName && rows.some(row => normalizeBotName(row.name) === normalizeBotName(expectString(p.displayName, 'displayName')))) ||
(config.templateId === 'cindy' && rows.some(row => JSON.parse(row.config).templateId === 'cindy' || inferBotTemplatePresetId(row.identity) === 'cindy'))) {
throw Object.assign(new Error('A matching companion already exists'), { code: 'ALREADY_EXISTS' });
}
@@ -365,6 +373,7 @@ function botsUpdateProfile(db: Database.Database, args: unknown): { currentVersi
const id = expectString(p.id, 'id');
const expectedVersion = expectNumber(p.expectedCurrentVersion, 'expectedCurrentVersion');
const now = expectNumber(p.now, 'now');
+ const allowDuplicateName = expectOptionalFlag(p.allowDuplicateName, 'allowDuplicateName');
const avatarRef = p.botAvatarRef === undefined
? null
: asRecord(p.botAvatarRef, 'botAvatarRef');
@@ -390,7 +399,7 @@ function botsUpdateProfile(db: Database.Database, args: unknown): { currentVersi
if (p.expectedAvatar !== undefined && current.avatar !== expectString(p.expectedAvatar, 'expectedAvatar')) {
throw Object.assign(new Error('Teammate avatar changed during update'), { code: 'PRECONDITION_FAILED' });
}
- if (p.displayName !== undefined && normalizeBotName(expectString(p.displayName, 'displayName')) !== normalizeBotName(current.displayName)) {
+ if (!allowDuplicateName && p.displayName !== undefined && normalizeBotName(expectString(p.displayName, 'displayName')) !== normalizeBotName(current.displayName)) {
const name = normalizeBotName(expectString(p.displayName, 'displayName'));
const others = db.prepare("SELECT display_name AS name FROM bot_profiles WHERE id != ? AND status != 'archived'").all(id) as Array<{ name: string }>;
if (others.some(row => normalizeBotName(row.name) === name)) throw Object.assign(new Error('A companion with this name already exists'), { code: 'ALREADY_EXISTS' });
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botGroupChatService.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botGroupChatService.test.ts
index 1fcad2604e..f670cebc64 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botGroupChatService.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botGroupChatService.test.ts
@@ -628,6 +628,8 @@ describe('group mention and prompt helpers', () => {
const twins = [{ botId: 'x1', name: '小满' }, { botId: 'x2', name: '小满' }];
expect(resolveGroupMentions('@小满 在吗', { all: false, botIds: ['x2'] }, twins))
.toEqual({ all: false, botIds: ['x2'] });
+ // Typed text alone cannot tell same-named members apart: all of them are addressed, never just the first.
+ expect(resolveGroupMentions('@小满 在吗', null, twins)).toEqual({ all: false, botIds: ['x1', 'x2'] });
expect(resolveGroupMentions('@小满帮我查一下', null, members)).toEqual({ all: false, botIds: ['xiaoman'] });
expect(resolveGroupMentions('@所有人看这里', null, members).all).toBe(true);
expect(resolveGroupMentions('@Everyone look', null, members).all).toBe(true);
diff --git a/apps/desktop/src/main/maker-ipc/botGroupChatService.ts b/apps/desktop/src/main/maker-ipc/botGroupChatService.ts
index 8de48a83e3..82a6d6f068 100644
--- a/apps/desktop/src/main/maker-ipc/botGroupChatService.ts
+++ b/apps/desktop/src/main/maker-ipc/botGroupChatService.ts
@@ -471,7 +471,10 @@ export function resolveGroupMentions(
});
if (!match) continue;
if (!match.botId) all = true;
- else if (!ordered.includes(match.botId)) ordered.push(match.botId);
+ // Same-named members are indistinguishable in text: address them all, as the composer's parser does.
+ else for (const twin of labels) {
+ if (twin.botId && twin.label.toLocaleLowerCase() === match.label.toLocaleLowerCase() && !ordered.includes(twin.botId)) ordered.push(twin.botId);
+ }
}
return { all, botIds: structured.length > 0 ? structured : ordered };
}
diff --git a/docs/product-rules/cindy-bots-runtime.md b/docs/product-rules/cindy-bots-runtime.md
index 9748180a32..6de6eee5fe 100644
--- a/docs/product-rules/cindy-bots-runtime.md
+++ b/docs/product-rules/cindy-bots-runtime.md
@@ -1129,6 +1129,7 @@ Codex 切模型重建时,线程索引里的路径不等于文件已经落盘
- Dash / LiZi 等旧角色模板已移除,只有默认 Cindy 保留内置身份。已有伙伴按持久 Profile 运行;
保留历史头像地址与模板标识的读取,未完成邀请使用已保存身份,不重建旧模板或补装角色 Skill。
- 新建与改名不能产生同名伙伴(NFKC、去首尾空格、不区分大小写)。头像可复用,姓名必须可区分。
+ 唯一例外是从其他电脑接管(换到这台电脑):接管不受此检查拦截,允许与本机另一个伙伴同名并存,见 [cloud-companions.md](cloud-companions.md)(2026-10-26)。
- 创建与修改共用头像图库:Cindy 固定第一格,原 16 个头像保持相对顺序,上传在末格;
桌面六列、窄窗口四列。每次打开创建表单从全部 17 个头像随机选一个,表单存续期间不重抽,
用户选择优先;修改已有伙伴不随机、不覆盖当前头像。选 Cindy 头像或取名 Cindy 不附加内置身份,
diff --git a/docs/product-rules/cloud-companions.md b/docs/product-rules/cloud-companions.md
index ff7d606dcf..7cea2a96a8 100644
--- a/docs/product-rules/cloud-companions.md
+++ b/docs/product-rules/cloud-companions.md
@@ -206,6 +206,7 @@
不会把已删除的内容带回云端。资料上传只给本机曾上传或曾接管写入过的键写墓碑,服务器上仅存在的键(含本机未能写入的
记忆)从不被墓碑。云端既没有值也没有墓碑、只存在于这台电脑的记忆 / 技能(例如第二台电脑 Cindy 自己积累的)接管后保留,
并作为新内容上传、合入伙伴(Dash 2026-10-13 确认:合并,不按云端删除)。
+- 接管允许与本机另一个伙伴同名并存(2026-10-26 用户裁决:不要因为同名阻塞接管,不自动改名,两者各自独立、不合并记忆与对话):本机已有不同 ID、同名的伙伴时,接管新建副本与接管已有副本时云端改名撞名,都不再被「已有同名伙伴」拦住,换过来的伙伴照常出现,本机原有伙伴不动。放行只来自接管这条受信任路径:`createBotProfile` / `updateBotProfile` 的 `RestoredBotOrigin.allowSameName` 让 main 向数据库 worker 的 `bots.createProfile` / `bots.updateProfile` 传内部参数 `allowDuplicateName`(缺省与非布尔值=原行为),渲染层、IPC 请求体与模型工具带任何字段都绕不过;普通新建、伙伴市场加入、改名的同名拦截不变,默认 Cindy 仍只能有一个(接管一个 Cindy 模板伙伴而本机已有 Cindy 时仍是 `ALREADY_EXISTS`,行为未改)。同名并存的影响面:按 ID 寻址的路径(委派、`send_to_agent`、IM 绑定、云端同步、群成员授权)不受影响;侧栏伙伴列表用既有的 `botRosterLabel`(「名字 · 序号」)区分,其它选择器(群 @ 候选、添加到群、搜索、手机端)目前只靠头像区分,尚未加序号(待产品定夺);群里只靠文字 `@名字` 且没有结构化点名时,宿主把同名成员全部点名(与输入框解析一致),不再只取第一个。
- 接管已有的本机副本时,副本上没走完的「见面」(新电脑首次启动播种默认伙伴并开始见面,随后因伙伴在另一台电脑运行而被降级,见面停在中途)随云端资料一并结束:接管写资料时由主进程把这条见面记录清掉(`updateBotProfile` 的 `RestoredBotOrigin.replacesUnfinishedMeeting`,只有接管这条受信任路径传入,渲染层与模型请求体不能带;已经走完的见面记录保持不动),不再拦住资料写入和建主任务,也不会在接管后重新生成欢迎消息、头像或人设(正在跑或启动时恢复的见面在下一次保存时以 `INVITATION_REPLACED` 结束,且降级副本拒绝一切发送)。见面已经排进主任务队列、被降级拒绝而留下的打招呼请求(含启动时从崩溃快照恢复的暂停队列里的那条),同样在资料写入之后由 main 侧清掉(`discardInvitationWelcome` → `queuedInvitationWelcome.ts`,只认「仅文本 + 界面触发前缀」的这种队列项,主人自己排的消息和其它队列项保留;读不到队列时只记一条警告日志(带任务 ID、不含正文)并继续接管,不因此让接管失败)。否则接管成功后主人发的第一条消息会把它放行,冒出一条欢迎和问候。普通编辑资料时「伙伴正在准备见面」的保护不变。
- 技能按 `SKILL.md` 原文(含 Cindy 不认识的原生 frontmatter)上传与恢复,停用状态一并恢复(整个技能目录在启用 / 停用之间移动),换电脑后技能语义不变。
- 接管后对话镜像从本机最后一条自有消息(不含导入的云端历史)之后开始,本机接管前的旧对话不上传到房间。