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): 重启后的中断任务提示纳入伙伴任务

Dash04b77fbbf3c7
diff --git a/apps/desktop/src/main/localDb/__tests__/sessionActiveTurn.test.ts b/apps/desktop/src/main/localDb/__tests__/sessionActiveTurn.test.ts
index dba7920fa8..670da13d96 100644
--- a/apps/desktop/src/main/localDb/__tests__/sessionActiveTurn.test.ts
+++ b/apps/desktop/src/main/localDb/__tests__/sessionActiveTurn.test.ts
@@ -78,6 +78,17 @@ describe('sessionActiveTurn', () => {
CREATE INDEX idx_messages_rewind_at ON messages (rewind_at);
CREATE INDEX idx_messages_active_error_tail ON messages (session_id, created_at)
WHERE role = 'error' AND rewind_at IS NULL;
+
+ CREATE TABLE bot_session_links (
+ id TEXT PRIMARY KEY,
+ bot_id TEXT NOT NULL,
+ session_id TEXT NOT NULL,
+ profile_version INTEGER NOT NULL DEFAULT 1,
+ role TEXT NOT NULL,
+ route_key TEXT,
+ created_at INTEGER NOT NULL,
+ archived_at INTEGER
+ );
`);
const db = drizzle(dbHandle, { schema });
const client: DbClient = {
@@ -550,6 +561,32 @@ describe('sessionActiveTurn', () => {
]);
});
+ it('offers interrupted companion tasks for resume but keeps hidden lanes and red dots out', async () => {
+ const { listInterruptedResumeCandidateRows } = await import('../interruptedResumeCandidates.js');
+ const { listInterruptedPendingSessionIds } = await import('../sessionActiveTurn.js');
+ const client = createTestDbClient();
+ const startedAt = Date.now() - 10_000;
+ await seedSession(client, 's-desktop', { startedAt });
+ for (const role of ['canonical', 'history', 'delegation', 'group', 'channel']) {
+ await seedSession(client, `bot-${role}`, { startedAt, source: 'bot' });
+ await client.exec(
+ 'INSERT INTO bot_session_links (id, bot_id, session_id, role, created_at) VALUES (?, ?, ?, ?, ?)',
+ [`link-${role}`, 'bot-1', `bot-${role}`, role, startedAt],
+ );
+ }
+ // 没有伙伴关联的 bot 来源会话无处打开,不提示。
+ await seedSession(client, 'bot-unlinked', { startedAt, source: 'bot' });
+
+ expect((await listInterruptedResumeCandidateRows()).map((r) => r.sessionId).sort()).toEqual([
+ 'bot-canonical',
+ 'bot-delegation',
+ 'bot-history',
+ 's-desktop',
+ ]);
+ // 侧栏红点不展示伙伴任务,口径不变。
+ expect(await listInterruptedPendingSessionIds()).toEqual(['s-desktop']);
+ });
+
it('excludes plugin background tasks from both red-dot legs', async () => {
const { listInterruptedPendingSessionIds, listErrorTailPendingSessionIds } = await import('../sessionActiveTurn.js');
const client = createTestDbClient();
diff --git a/apps/desktop/src/main/localDb/interruptedResumeCandidates.ts b/apps/desktop/src/main/localDb/interruptedResumeCandidates.ts
index 7bfec5c31c..07ce797c3a 100644
--- a/apps/desktop/src/main/localDb/interruptedResumeCandidates.ts
+++ b/apps/desktop/src/main/localDb/interruptedResumeCandidates.ts
@@ -2,6 +2,7 @@
* 启动「继续上次中断的任务」弹窗的数据源。
*
* 中断判定完全复用 listInterruptedPendingRows(startedAt > endedAt 且早于本进程启动),
+ * 并额外纳入伙伴的主对话、历史与委派任务(它们不在普通任务列表,但同样会被中断),
* 这里只为每个中断会话补两项展示素材:
* - interruptedAt:中断那一轮最后一条落库消息的时间(崩溃时刻无从得知,最后一次
* 落库是最接近的可观察值);该轮没有任何落库消息时回退到 turn 开始时间;
@@ -47,7 +48,7 @@ function parseToolUse(raw: string | null): InterruptedTurnToolUse | null {
export async function listInterruptedResumeCandidateRows(): Promise<
InterruptedResumeCandidateRow[]
> {
- const pending = await listInterruptedPendingRows();
+ const pending = await listInterruptedPendingRows({ includeBotTasks: true });
if (pending.length === 0) return [];
const db = getDbClient().drizzle;
return Promise.all(
diff --git a/apps/desktop/src/main/localDb/sessionActiveTurn.ts b/apps/desktop/src/main/localDb/sessionActiveTurn.ts
index ad5b3f17ae..7e6e5e6ed4 100644
--- a/apps/desktop/src/main/localDb/sessionActiveTurn.ts
+++ b/apps/desktop/src/main/localDb/sessionActiveTurn.ts
@@ -49,7 +49,7 @@
* turn 主流程。
*/
-import { and, desc, eq, gt, inArray, isNull, lt, sql } from 'drizzle-orm';
+import { and, desc, eq, gt, inArray, isNull, lt, or, sql } from 'drizzle-orm';
import { getDbClient } from './client/current';
import { listCindyMakePendingFailureSessionIds } from './cindyMakeAttention';
@@ -57,7 +57,7 @@ import {
getSessionInterruptionBootAt,
setSessionInterruptionBootAtForTests,
} from './sessionInterruptionBoot';
-import { messages, sessions } from './schema';
+import { botSessionLinks, messages, sessions } from './schema';
import { createLogger } from '../logger';
import { DESKTOP_TASK_LIST_SESSION_SOURCES } from '../../shared/sessionSource.js';
import { boundedSummary } from '../maker-ipc/recoveryCoordinator.js';
@@ -375,19 +375,32 @@ export function _setBootAtMsForTests(ms: number): void {
* 启动首拉消费它(见下方两条腿消费周期的说明);但 startedAt < bootAt 的守卫让它
* 在任何时刻调用都不会把运行中的会话算进来,批量处置因此也可以安全复用。
*/
-export async function listInterruptedPendingRows(): Promise<
- { sessionId: string; startedAt: number }[]
-> {
+export async function listInterruptedPendingRows(
+ options: { includeBotTasks?: boolean } = {},
+): Promise<{ sessionId: string; startedAt: number }[]> {
const db = getDbClient().drizzle;
+ // 只统计桌面 sidebar 可见来源(含 feishu/slack 等 IM 渠道)——
+ // 不可见来源的红点无处展示也无法清除。
+ const listedSource = inArray(sessions.source, DESKTOP_TASK_LIST_SESSION_SOURCES);
const rows = await db
.select({ id: sessions.id, startedAt: sessions.activeTurnStartedAt })
.from(sessions)
.where(
and(
eq(sessions.status, 'active'),
- // 只统计桌面 sidebar 可见来源(含 feishu/slack 等 IM 渠道)——
- // 不可见来源的红点无处展示也无法清除。
- inArray(sessions.source, DESKTOP_TASK_LIST_SESSION_SOURCES),
+ options.includeBotTasks
+ ? or(
+ listedSource,
+ // 伙伴的主对话、历史与委派任务有自己的页面,可以继续;群专线与 IM 专线
+ // 是隐藏专线,由群聊 / 渠道收口,不单独提示。
+ and(
+ eq(sessions.source, 'bot'),
+ sql`EXISTS (SELECT 1 FROM ${botSessionLinks}
+ WHERE ${botSessionLinks.sessionId} = ${sessions.id}
+ AND ${botSessionLinks.role} IN ('canonical', 'history', 'delegation'))`,
+ ),
+ )
+ : listedSource,
// 插件后台隐藏任务不在侧栏展示,红点无处展示也无法清除。
isNull(sessions.hiddenAt),
gt(sessions.activeTurnStartedAt, sql`COALESCE(${sessions.lastTurnEndedAt}, 0)`),
diff --git a/apps/desktop/src/renderer/features/cc-agent/InterruptedTasksResumeDialog.tsx b/apps/desktop/src/renderer/features/cc-agent/InterruptedTasksResumeDialog.tsx
index 1611db0e0e..a9c800d373 100644
--- a/apps/desktop/src/renderer/features/cc-agent/InterruptedTasksResumeDialog.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/InterruptedTasksResumeDialog.tsx
@@ -47,6 +47,22 @@ interface InterruptedTask {
session: Session;
interruptedAt: number;
activity: string | null;
+ /** 伙伴的任务不在普通任务列表,来源标签显示伙伴名字,而不是它的工作目录。 */
+ botName: string | null;
+}
+
+/** 伙伴任务 id → 伙伴名字;读取失败时退回普通来源标签,不挡住弹窗。 */
+async function loadBotNamesBySessionId(): Promise<Map<string, string>> {
+ const names = new Map<string, string>();
+ const bots: unknown = await window.electronAPI.localDb.bots.list().catch(() => []);
+ if (!Array.isArray(bots)) return names;
+ for (const bot of bots as { name?: unknown; sessions?: unknown }[]) {
+ if (typeof bot?.name !== 'string' || !Array.isArray(bot.sessions)) continue;
+ for (const row of bot.sessions as { id?: unknown }[]) {
+ if (typeof row?.id === 'string') names.set(row.id, bot.name);
+ }
+ }
+ return names;
}
async function listStillInterruptedIds(): Promise<Set<string>> {
@@ -64,10 +80,22 @@ async function loadInterruptedTasks(): Promise<InterruptedTask[]> {
batches.push(sessionsApi.getMany(ids.slice(i, i + SESSION_READ_BATCH_LIMIT)));
}
const byId = new Map((await Promise.all(batches)).flat().map((s) => [s.id, s]));
+ const botNames = [...byId.values()].some((s) => s.source === 'bot')
+ ? await loadBotNamesBySessionId()
+ : new Map<string, string>();
return candidates
.flatMap((c) => {
const session = byId.get(c.sessionId);
- return session ? [{ session, interruptedAt: c.interruptedAt, activity: c.activity }] : [];
+ return session
+ ? [
+ {
+ session,
+ interruptedAt: c.interruptedAt,
+ activity: c.activity,
+ botName: botNames.get(session.id) ?? null,
+ },
+ ]
+ : [];
})
.sort((a, b) => b.interruptedAt - a.interruptedAt);
}
@@ -207,12 +235,12 @@ export function InterruptedTasksResumeDialog({ suspended = false }: { suspended?
</span>
</div>
<ul className="border-y border-[var(--border-default)] py-1">
- {tasks.map(({ session, interruptedAt, activity }) => {
+ {tasks.map(({ session, interruptedAt, activity, botName }) => {
const checked = !deselected.has(session.id);
const title = getSessionDisplayTitle(session, t('ccAgent.common.unnamedSession'), t);
const relative = formatSidebarTime(new Date(interruptedAt).toISOString(), t);
const meta = [
- sourceLabels.get(session.id),
+ botName ?? sourceLabels.get(session.id),
activity,
Date.now() - interruptedAt < 60_000
? t('chat.interruptedResumeDialog.interruptedJustNow')