Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(companions): retain retirement intent when actor lookup is unavailable
Zhao Dengfeng72202d914ede
diff --git a/apps/desktop/src/main/companion-cloud/__tests__/control.test.ts b/apps/desktop/src/main/companion-cloud/__tests__/control.test.tsindex 1749e905bf..5365d46fdd 100644--- a/apps/desktop/src/main/companion-cloud/__tests__/control.test.ts+++ b/apps/desktop/src/main/companion-cloud/__tests__/control.test.ts@@ -21,7 +21,7 @@ function setup(options: { home: string | null; handler?: ApiHandler; devices?: DinstallationId?: string; probe?: (deviceId: string) => Promise<boolean>; values?: Array<{ key: string; value: unknown }>;readValues?: () => Promise<Array<{ key: string; value: unknown }>>; syncProfile?: (botId: string) => Promise<void>;turnActive?: () => boolean; homeLink?: CompanionCloudHost['homeLink']; stopBotTurns?: CompanionCloudHost['stopBotTurns'];- canonicalSession?: CompanionCloudHost['canonicalSession']; paused?: (botId: string) => boolean }) {+ canonicalSession?: CompanionCloudHost['canonicalSession']; paused?: (botId: string) => boolean; savedState?: string }) {const actors: CompanionRuntimeActor[] = [{ id: 'actor-1', kind: 'bot', externalId: 'bot_mia', name: 'Mia', avatar: null, homeDeviceId: options.home }];const { api, calls } = fakeApi(options.handler ?? ((route, method) => {// The home has uploaded the profile snapshot a take-over builds from.@@ -33,7 +33,7 @@ function setup(options: { home: string | null; handler?: ApiHandler; devices?: Dthrow new Error(`unexpected ${method} ${route}`);}));const local = fakeLocal();- const { state } = memoryState();+ const { state, files } = memoryState(options.savedState);const refresh = vi.fn(async () => undefined);const publish = vi.fn();const onShadowChanged = vi.fn();@@ -53,7 +53,7 @@ function setup(options: { home: string | null; handler?: ApiHandler; devices?: Dbots: () => actors.filter(a => a.homeDeviceId === 'desk' && !state.isDemoted(a.externalId)).map(a => ({ botId: a.externalId, actorId: a.id, name: a.name })),actors: () => actors, refresh, publish, onShadowChanged, ...(options.syncProfile ? { syncProfile: options.syncProfile } : {}),...(options.paused ? { paused: options.paused } : {}) });- return { runtime, actors, calls, local, state, refresh, publish, onShadowChanged };+ return { runtime, actors, calls, local, state, files, refresh, publish, onShadowChanged };}describe('take over', () => {@@ -425,6 +425,50 @@ describe('transcript', () => {});describe('retire', () => {+ // An explicit local deletion must survive an unavailable actor registry. Persist the+ // known actor identity; a later registry must still prove home + installation ownership.+ it('persists retirement without an actor registry and completes it after restart', async () => {+ const t = setup({ home: 'desk' });+ t.state.update('bot_mia', 'actor-1', entry => { entry.roomId = ROOM; });+ t.actors.length = 0;+ await t.runtime.retire('bot_mia');+ const savedState = t.files.get('state.json');+ expect(JSON.parse(savedState!).bots.bot_mia.retirePending).toBe(true);+ expect(t.calls.some(call => call.method === 'PATCH')).toBe(false);+ t.runtime.dispose();++ const restarted = setup({ home: 'desk', savedState });+ restarted.runtime.reconcile([]);+ await vi.waitFor(() => expect(restarted.actors[0]!.disabled).toBe(true));+ expect(restarted.state.bot('bot_mia')?.retirePending).toBeUndefined();+ expect(restarted.local.host.createProfile).not.toHaveBeenCalled();+ });++ it.each(['laptop', 'replacement', 'disabled'])('does not apply a deferred deletion to %s', async target => {+ const t = setup({ home: 'desk' });+ t.state.update('bot_mia', 'actor-1', entry => { entry.retirePending = true; });+ if (target === 'laptop') t.actors[0]!.homeDeviceId = 'laptop';+ else if (target === 'replacement') t.actors[0]!.id = 'actor-replacement';+ else t.actors[0]!.disabled = true;+ // Keep a local row to avoid exercising automatic materialization in this retirement test.+ t.runtime.reconcile([{ id: 'bot_mia', status: 'active' }]);+ await Promise.resolve();+ expect(t.calls.some(call => call.method === 'PATCH' && (call.data as { disabled?: boolean }).disabled)).toBe(false);+ expect(t.actors[0]!.disabled === true).toBe(target === 'disabled');+ expect(t.state.bot('bot_mia')?.retirePending).toBeUndefined();+ });++ it('does not queue an unknown or demoted companion when the registry is unavailable', async () => {+ const t = setup({ home: 'desk' });+ t.state.update('bot_mia', 'actor-1', entry => { entry.demoted = true; });+ t.actors.length = 0;+ await t.runtime.retire('bot_mia');+ await t.runtime.retire('unknown');+ expect(t.state.bot('bot_mia')?.retirePending).toBeUndefined();+ expect(t.state.bot('unknown')).toBeNull();+ expect(t.calls.some(call => call.method === 'PATCH')).toBe(false);+ });+it('disables the cloud companion when it is deleted on its main computer', async () => {const t = setup({ home: 'desk' });await t.runtime.retire('bot_mia');diff --git a/apps/desktop/src/main/companion-cloud/runtime.ts b/apps/desktop/src/main/companion-cloud/runtime.tsindex cdbc1bc10b..01613e4cb1 100644--- a/apps/desktop/src/main/companion-cloud/runtime.ts+++ b/apps/desktop/src/main/companion-cloud/runtime.ts@@ -605,8 +605,12 @@ export function startCompanionCloud(input: {if (actor.kind !== 'bot') continue;const botId = actor.externalId;const local = localProfiles.find(profile => profile.id === botId);- const entry = state.bot(botId);+ let entry = state.bot(botId);+ // A local id may now refer to a different cloud actor. In particular, an old+ // retirement must never disable that replacement (state.update uses the same boundary).+ if (entry && entry.actorId !== actor.id) entry = state.update(botId, actor.id, () => undefined);if (actor.disabled) {+ if (entry?.retirePending) state.update(botId, actor.id, next => { delete next.retirePending; });// Deleted on its main computer (perhaps while this one was offline): a copy here is no// longer the companion, so it is hidden and stopped like any other old copy.if (local && local.status !== 'deleting' && !entry?.demoted) {@@ -837,9 +841,18 @@ export function startCompanionCloud(input: {async retire(botId) {const self = input.selfDeviceId;- const actor = actorOf(botId);// A demoted copy is not the companion: deleting it never touches the cloud.- if (!self || !live() || !actor || actor.homeDeviceId !== self || runtime.isShadowed(botId)) return;+ if (!self || !live() || runtime.isShadowed(botId)) return;+ const actor = actorOf(botId);+ if (!actor) {+ // The local deletion has already committed. Keep its intent for a previously+ // known actor even when the registry cannot resolve it yet; reconcile checks the+ // actor identity, current home and installation before sending any cloud mutation.+ const entry = state.bot(botId);+ if (entry) state.update(botId, entry.actorId, next => { next.retirePending = true; });+ return;+ }+ if (actor.homeDeviceId !== self) return;state.update(botId, actor.id, entry => { entry.retirePending = true; });await retireOwned(actor);},diff --git a/docs/product-rules/cloud-companions.md b/docs/product-rules/cloud-companions.mdindex 33ab5004c3..58248cb8c8 100644--- a/docs/product-rules/cloud-companions.md+++ b/docs/product-rules/cloud-companions.md@@ -201,6 +201,10 @@ Desktop `runtime.transcript()` 经注入的 Chat History 读取,首次按现安装认领了伙伴时同样只删本机;本安装尚未确认认领(检查未完成,或伙伴刚被交到这台电脑)时,先按自动接管的方式认领,抢到才停用,被另一个安装抢先则只删本机。其他电脑(包括删除时离线、之后才上线的)看到云端伙伴已停用时,把本机副本转为降级:隐藏、只读、不运行。+- 删除回调已进入云端运行时、但内存名册尚不能解析伙伴时,若同账号/端点的状态文件已有该伙伴的 actor ID,+ 先保存 `retirePending`,名册恢复后再核对主力电脑与安装归属并停用。没有已知 actor ID 或副本已降级时不猜测身份。+ 待停用记录只属于当时的 actor ID;同一本机伙伴 ID 后来对应另一个 actor 时丢弃旧同步状态,不能停用替代身份。+ 服务器已标记停用的 actor 清除待停用记录。此路径不按「本机资料不存在」批量推断历史伙伴已删除。- 接管时导入房间里的全部对话为本机历史(读到房间第一条为止,读不完整则这次接管失败、稍后重试;合并 Cindy 同样如此;云端对话查看最多读最近约 5 万条),附件一起下载。服务器不回传导入来源 ID,本机按「角色 + 消息时间(毫秒)」识别这台电脑当初导入上去的消息;已插入的云端历史(`cloud-history:<消息>`)和房间里直接发给伙伴、已投递进本机主任务的消息(`chat-direct:<执行>:<消息>`)按房间消息 ID 识别,都不重复插入(接管中途失败重试时,同一时间的拆分片段不会被误跳过);之后像「重启伙伴」一样从历史重建上下文,伙伴还有一轮在跑时等它结束再重建,期间副本保持降级。- 导入的伙伴回复(`cloud-history:` 行)在本机和别的伙伴回复一样存为纯文本。早期版本把它存成文本块数组,聊天里会显示成原始 JSON;这些存量行不迁移数据库,读取时(`messageToCamel`)把全是文本块的数组还原成文本,所有读者(聊天、手机、搜索)看到的一致。