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(sharing): close Bot shared tasks on lifecycle deletion

DavidShen77e03700c81e
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
index 44c9333934..5051c275df 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
@@ -34,6 +34,7 @@ const h = vi.hoisted(() => ({
})),
closeSession: vi.fn(async (_sessionId: string) => undefined),
closeSharedTask: vi.fn(async (_sessionId: string, _database: unknown): Promise<void> => undefined),
+ commitBotProfileDeletion: vi.fn(async (): Promise<{ status: 'archived' | 'deleted'; sessionIds: string[] }> => ({ status: 'archived', sessionIds: [] })),
tapWindowBroadcast: vi.fn(),
windows: [] as Array<{
trusted?: boolean;
@@ -105,6 +106,9 @@ vi.mock('../../client/current', () => ({
getDbClient: () => h.client,
getCurrentDbClientUserId: () => 'test-user',
}));
+vi.mock('../../botProfileDeletionStore.js', () => ({
+ commitBotProfileDeletion: h.commitBotProfileDeletion,
+}));
vi.mock('../../dialogueWorkspace', () => ({ ensureDialogueWorkspaceDir: vi.fn() }));
vi.mock('../../../git-context/prRefsStore', () => ({
recomputePrRefsForSession: vi.fn(async () => undefined),
@@ -157,6 +161,7 @@ vi.mock('../../../cindy-brain/index.js', () => ({
import {
broadcastSessionPatched,
+ deleteBotProfileAndDetachSessionsInDb,
patchSessionMetaInDb,
persistSessionFields,
registerSessionIpc,
@@ -307,6 +312,7 @@ async function invokeCreate(body: Record<string, unknown>): Promise<unknown> {
beforeEach(() => {
vi.clearAllMocks();
h.relocate.mockImplementation(async () => ({ persistedSdkSessionId: null }));
+ h.commitBotProfileDeletion.mockResolvedValue({ status: 'archived', sessionIds: [] });
h.closeSession.mockClear();
h.routeLock.mockImplementation(async (_sessionId, task) => task());
h.upsertRecentWorkdir.mockImplementation(async () => true);
@@ -897,6 +903,18 @@ describe('local-db:sessions:update handler wiring', () => {
expect(h.closeSharedTask).toHaveBeenCalledWith('codex-local', h.client);
});
+ it.each([true, false])('closes shared tasks when Bot deletion transitions sessions to terminal status (keep=%s)', async (keepTaskHistory) => {
+ h.commitBotProfileDeletion.mockResolvedValue({
+ status: keepTaskHistory ? 'archived' : 'deleted',
+ sessionIds: ['bot-local', 'bot-local'],
+ });
+
+ await deleteBotProfileAndDetachSessionsInDb('bot-1', ['bot-local'], keepTaskHistory);
+ await vi.dynamicImportSettled();
+
+ expect(h.closeSharedTask).toHaveBeenCalledExactlyOnceWith('bot-local', h.client);
+ });
+
// 竞态收敛(review on #3225):写入与查询不在同一串行区间,归档写入后、查询前
// 另一窗口可能已把行推进到 deleted。广播必须携带持久化后的 updated.status,
// 否则副窗/控制端镜像会被请求值回滚成旧 UI 状态(已删除任务复活)。
diff --git a/apps/desktop/src/main/localDb/ipc/sessions.ts b/apps/desktop/src/main/localDb/ipc/sessions.ts
index 701f1274a0..daa2da432f 100644
--- a/apps/desktop/src/main/localDb/ipc/sessions.ts
+++ b/apps/desktop/src/main/localDb/ipc/sessions.ts
@@ -2370,7 +2370,8 @@ export async function deleteBotProfileAndDetachSessionsInDb(
): Promise<void> {
const ids = [...new Set(sessionIds)];
const ownerScope = captureOwnerScope();
- const db = getDbClient().drizzle;
+ const dbClient = getDbClient();
+ const db = dbClient.drizzle;
const commitDeletion = () =>
commitBotProfileDeletion({
botId,
@@ -2382,6 +2383,15 @@ export async function deleteBotProfileAndDetachSessionsInDb(
const status = committed.status;
const committedSessionIds = [...new Set(committed.sessionIds)];
+ // Bot profile deletion commits terminal task status through a dedicated
+ // transaction, so it bypasses the ordinary session patch/status writers.
+ // Close the corresponding shared task only after that durable transition;
+ // closeSharedTaskForTask journals network failures and never revives the task.
+ if (committedSessionIds.length > 0) {
+ const { closeSharedTaskForTask } = await import('../../device-link/sharedTaskRuntime.js');
+ for (const id of committedSessionIds) await closeSharedTaskForTask(id, dbClient);
+ }
+
for (const id of committedSessionIds) {
notifyAgentIslandSessionPatch(id, { status });
broadcastSessionPatched(id, { status, source: 'desktop' }, ownerScope);