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 replace a copy whose first meeting never finished

nanae4ef28941b5b
diff --git a/apps/desktop/src/main/companion-cloud/host.ts b/apps/desktop/src/main/companion-cloud/host.ts
index 433099423c..f638f47066 100644
--- a/apps/desktop/src/main/companion-cloud/host.ts
+++ b/apps/desktop/src/main/companion-cloud/host.ts
@@ -211,7 +211,7 @@ function createMaterializer(input: {
...(description !== undefined ? { description } : {}),
...(identitySource !== undefined ? { identitySource } : {}),
...(userContextSource !== undefined ? { userContextSource } : {}),
- ...('portable' in restored ? { avatar: restored.portable } : {}) }, undefined, undefined, { templateId });
+ ...('portable' in restored ? { avatar: restored.portable } : {}) }, undefined, undefined, { templateId, replacesUnfinishedMeeting: 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 136bf10800..df77efac94 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts
@@ -929,6 +929,42 @@ describe('Bot canonical Session lifecycle', () => {
.toEqual({ name: 'Release Bot', current_version: 1 });
});
+ // #7095: the default companion seeded on a fresh computer is demoted mid-meeting; the take-over then
+ // writes the cloud profile onto that copy.
+ describe('cloud take-over of a copy whose first meeting never finished', () => {
+ const stored = () => JSON.parse(h.sqlite!.prepare(
+ 'SELECT v.capabilities_json AS json FROM bot_profile_versions v JOIN bot_profiles p ON p.id = v.bot_id AND p.current_version = v.version WHERE p.id = ?',
+ ).pluck().get('bot-1') as string) as Record<string, unknown>;
+ const unfinished = (stage: string) => h.sqlite!.prepare('UPDATE bot_profile_versions SET capabilities_json = ? WHERE bot_id = ?')
+ .run(JSON.stringify({ invitation: { id: 'invite-1', stage } }), 'bot-1');
+
+ it.each(['profile', 'skills', 'avatar', 'welcome', 'failed'])('replaces the profile and ends the %s meeting', async (stage) => {
+ unfinished(stage);
+ const updated = await updateStoredBotProfile({ id: 'bot-1', name: 'Cindy', enabled: true }, undefined, undefined,
+ { replacesUnfinishedMeeting: true });
+ expect(updated.name).toBe('Cindy');
+ expect(stored().invitation).toBeUndefined();
+ // The take-over goes on to the main task, which a pending meeting would refuse.
+ await expect(invoke('local-db:bots:create-canonical-session', {
+ botId: 'bot-1', expectedCanonicalSessionId: null, expectedProfileVersion: updated.currentVersion,
+ })).resolves.toMatchObject({ created: true });
+ });
+
+ it('keeps a finished meeting record', async () => {
+ unfinished('ready');
+ await updateStoredBotProfile({ id: 'bot-1', name: 'Cindy' }, undefined, undefined, { replacesUnfinishedMeeting: true });
+ expect(stored().invitation).toMatchObject({ id: 'invite-1', stage: 'ready' });
+ });
+
+ it('does not let a renderer or model body switch the protection off', async () => {
+ unfinished('skills');
+ await expect(invoke('local-db:bots:update', { id: 'bot-1', name: 'Cindy', replacesUnfinishedMeeting: true }))
+ .rejects.toMatchObject({ code: 'PRECONDITION_FAILED' });
+ await expect(updateStoredBotProfile({ id: 'bot-1', name: 'Cindy' })).rejects.toMatchObject({ code: 'PRECONDITION_FAILED' });
+ expect(stored().invitation).toMatchObject({ stage: 'skills' });
+ });
+ });
+
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 aced347eff..9f67bd9943 100644
--- a/apps/desktop/src/main/localDb/ipc/bots.ts
+++ b/apps/desktop/src/main/localDb/ipc/bots.ts
@@ -1759,9 +1759,10 @@ 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. Never reachable from a renderer or model body.
+ * 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.
*/
-export interface RestoredBotOrigin { templateId?: unknown }
+export interface RestoredBotOrigin { templateId?: unknown; replacesUnfinishedMeeting?: true }
/** Main-owned creation path shared by the renderer and Bot runtime tools. */
export async function createBotProfile(raw: unknown, restored?: RestoredBotOrigin) {
@@ -2018,7 +2019,9 @@ export async function updateBotProfile(raw: unknown, expectedVersion?: number,
const previous = parseJson(version?.capabilitiesJson ?? '{}');
const preparation = botInvitationProgress(previous.invitation);
const retryingPortrait = preparation?.stage === 'avatar' && current.canonicalSessionId;
- if (preparation && preparation.stage !== 'ready' && !retryingPortrait && (body.name !== undefined || body.description !== undefined || body.identitySource !== undefined)) {
+ // A take-over replaces this copy with the cloud profile: its own meeting no longer means anything.
+ const droppingMeeting = restored?.replacesUnfinishedMeeting === true && !!preparation && preparation.stage !== 'ready';
+ if (preparation && preparation.stage !== 'ready' && !retryingPortrait && !droppingMeeting && (body.name !== undefined || body.description !== undefined || body.identitySource !== undefined)) {
throwIpcError('PRECONDITION_FAILED', '伙伴正在准备见面,请完成准备后再编辑资料');
}
const nextConfig = mergeBotProfileCapabilities({
@@ -2039,8 +2042,9 @@ export async function updateBotProfile(raw: unknown, expectedVersion?: number,
: inferBotTemplatePresetId(version?.identitySource ?? '');
if (retainedTemplate) nextConfig.templateId = retainedTemplate;
else delete nextConfig.templateId;
- // Keep preparation checkpoints outside caller-editable capabilities.
- if (previous.invitation) nextConfig.invitation = previous.invitation;
+ // Keep preparation checkpoints outside caller-editable capabilities. Dropping the record also
+ // stops a running or recovered invitation (it ends as INVITATION_REPLACED) from greeting again.
+ if (previous.invitation && !droppingMeeting) nextConfig.invitation = previous.invitation;
else delete nextConfig.invitation;
if (Object.prototype.hasOwnProperty.call(body, 'userContextSource')) {
nextConfig.userContextSource = readText(body.userContextSource, 'userContextSource', 12000);
diff --git a/docs/product-rules/cloud-companions.md b/docs/product-rules/cloud-companions.md
index 7aad2a485f..cc6a64ab2e 100644
--- a/docs/product-rules/cloud-companions.md
+++ b/docs/product-rules/cloud-companions.md
@@ -206,6 +206,7 @@
不会把已删除的内容带回云端。资料上传只给本机曾上传或曾接管写入过的键写墓碑,服务器上仅存在的键(含本机未能写入的
记忆)从不被墓碑。云端既没有值也没有墓碑、只存在于这台电脑的记忆 / 技能(例如第二台电脑 Cindy 自己积累的)接管后保留,
并作为新内容上传、合入伙伴(Dash 2026-10-13 确认:合并,不按云端删除)。
+- 接管已有的本机副本时,副本上没走完的「见面」(新电脑首次启动播种默认伙伴并开始见面,随后因伙伴在另一台电脑运行而被降级,见面停在中途)随云端资料一并结束:接管写资料时由主进程把这条见面记录清掉(`updateBotProfile` 的 `RestoredBotOrigin.replacesUnfinishedMeeting`,只有接管这条受信任路径传入,渲染层与模型请求体不能带;已经走完的见面记录保持不动),不再拦住资料写入和建主任务,也不会在接管后重新生成欢迎消息、头像或人设(正在跑或启动时恢复的见面在下一次保存时以 `INVITATION_REPLACED` 结束,且降级副本拒绝一切发送)。普通编辑资料时「伙伴正在准备见面」的保护不变。
- 技能按 `SKILL.md` 原文(含 Cindy 不认识的原生 frontmatter)上传与恢复,停用状态一并恢复(整个技能目录在启用 / 停用之间移动),换电脑后技能语义不变。
- 接管后对话镜像从本机最后一条自有消息(不含导入的云端历史)之后开始,本机接管前的旧对话不上传到房间。
- 资料上传遇到版本冲突时先确认本机仍是主力电脑(否则停止并重新读取),再按服务器当前值最多重写一次;读本机资料(SOUL / USER / 自定义提示词)、