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。

Merge upstream/main into feat/issue-397-ios-simulator-native-framebuffer

JiangJiahao08edd9efa779
diff --cc apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts
index c527231f48,f8f45ddaa7..1f5f3a6f0d
--- a/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts
@@@ -2742,5 -2748,49 +2748,49 @@@ describe('turnRunner send outcome polic
expect(mocks.generateAndPersistFbotTitle).not.toHaveBeenCalled();
});
+
+ it('受保护群的首条消息不拿去起名 —— 标题也是长期记录', async () => {
+ // 标题会一直挂在侧边栏上。把「禁止保存内容」的正文摘要留在那里, 与把它
+ // 写进 transcript 是同一条边界被绕过, 而且主 turn 最终没被 provider 接受
+ // 时照样会留下。
+ const prefixedRunner = makePrefixedRunner();
+ setupSession(async () => ({ accepted: false, reason: 'cancelled-before-dispatch' }));
+ try {
+ await prefixedRunner.runAgentTurn({
+ botContextId: 'cli_test_bot',
+ userId: 'ou_user',
+ userMessageId: 'msg-user',
+ text: '帮我修个 bug',
+ attachments: [],
+ protectedContent: true,
+ });
+ await flushMicrotasks();
+ } finally {
+ await prefixedRunner.disposeAllSessions();
+ }
+
+ expect(mocks.generateAndPersistFbotTitle).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('受保护群的触发消息不进会话存档', () => {
+ // 群历史池已在渠道侧(emitGroupWindow)拦下, 但那只是第一条路径 —— 会话存档
+ // 同样会把正文与附件长期留下, 不挡就是绕过保护边界的旁路。
+ it('protectedContent 的消息不落库, 但照常派发给 agent', async () => {
+ const h = setupSession(async () => ({ accepted: true }));
+ mocks.persistUserMessage.mockClear();
+ await runDefaultTurn(vi.fn(), { protectedContent: true });
+ expect(mocks.persistUserMessage).not.toHaveBeenCalled();
+ // 不留存不等于不响应: provider 照常收到了这一轮(session 已建立并接受派发)。
+ expect(h).toBeTruthy();
+ expect(mocks.beginTurnChangeSetAtDispatch).not.toHaveBeenCalled();
+ });
-
++
+ it('未受保护的消息照常落库(既有行为不变)', async () => {
+ setupSession(async () => ({ accepted: true }));
+ mocks.persistUserMessage.mockClear();
+ await runDefaultTurn();
+ expect(mocks.persistUserMessage).toHaveBeenCalled();
+ });
});
});
diff --cc apps/desktop/src/main/im/shared/sessionRepo.ts
index ff8cec2e1f,beee825002..1aaa36e048
--- a/apps/desktop/src/main/im/shared/sessionRepo.ts
+++ b/apps/desktop/src/main/im/shared/sessionRepo.ts
@@@ -119,37 -232,47 +233,61 @@@ export function createImSessionRepo
async findActiveSession(botContextId, userId, scopeKey) {
const id = ns.sessionIdFor(botContextId, userId, scopeKey);
const db = getDbClient().drizzle;
- const rows = await db.select().from(sessions).where(eq(sessions.id, id)).limit(1);
- const row = rows[0];
- if (!row) return null;
- const workspaceKind = readWorkspaceKind(
- row.workingDir,
- row.workspaceKind ?? null,
- botContextId,
- );
- if (row.status !== 'active') {
- // 复活由用户 IM 消息触发,一并 bump userSendAt:广播 created 后 renderer
- // 立即重拉,而稍后 turnRunner 的 touchUserSent 不再广播 patched,不在这里
- // 写的话 sidebar 会按旧活跃时间排序/分组,直到下次整页刷新。
- const now = Date.now();
- await db
- .update(sessions)
- .set({
- status: 'active',
- userSendAt: now,
- updatedAt: now,
- // 渠道声明了归属分组时顺手校正老行, 但不碰用户 `/project` 切出去的行
- ...correctedWorkspaceKind(botContextId),
- })
- .where(eq(sessions.id, id));
- log.info(`revived soft-deleted ${ns.source} session id=${row.id} (was ${row.status})`);
+ const result = await withSessionRouteLock(id, async () => {
+ const rows = await db.select().from(sessions).where(eq(sessions.id, id)).limit(1);
+ const row = rows[0];
- if (!row) return { row: null, revivedFrom: null };
- if (row.status === 'active') return { row, revivedFrom: null };
-
- // 复活由用户 IM 消息触发,一并 bump userSendAt:广播 created 后 renderer
- // 立即重拉,而稍后 turnRunner 的 touchUserSent 不再广播 patched,不在这里
- // 写的话 sidebar 会按旧活跃时间排序/分组,直到下次整页刷新。
- const now = Date.now();
- await db
- .update(sessions)
- .set({
- status: 'active',
- userSendAt: now,
- updatedAt: now,
- // 渠道声明了归属分组时顺手校正(老行可能还是默认 'project')
- ...(ns.workspaceKind ? { workspaceKind: ns.workspaceKind } : {}),
- })
- .where(eq(sessions.id, id));
- return { row, revivedFrom: row.status };
++ if (!row) return null;
++ const workspaceKind = readWorkspaceKind(
++ row.workingDir,
++ row.workspaceKind ?? null,
++ botContextId,
++ );
++ let revivedFrom: string | null = null;
++ let workspaceKindCorrected = false;
++ if (row.status !== 'active') {
++ // 复活由用户 IM 消息触发,一并 bump userSendAt:广播 created 后 renderer
++ // 立即重拉,而稍后 turnRunner 的 touchUserSent 不再广播 patched,不在这里
++ // 写的话 sidebar 会按旧活跃时间排序/分组,直到下次整页刷新。
++ const now = Date.now();
++ await db
++ .update(sessions)
++ .set({
++ status: 'active',
++ userSendAt: now,
++ updatedAt: now,
++ // 渠道声明了归属分组时顺手校正老行, 但不碰用户 `/project` 切出去的行
++ ...correctedWorkspaceKind(botContextId),
++ })
++ .where(eq(sessions.id, id));
++ revivedFrom = row.status;
++ } else if (workspaceKind !== null && workspaceKind !== row.workspaceKind) {
++ // 存量脏行**就地回写**, 不等下一次归档。sidebar 的分组直接投影 DB 那一列
++ // (localDb/mapper.ts), 只在返回值上现算的话, 会话会一直待在错的分组里 ——
++ // 而它现在是 active, 可能永远等不到那次归档。
++ //
++ // 判据仍走 correctedWorkspaceKind 的 CASE(SQL 里现算), 不把 JS 算出来的值
++ // 写回去: 读和写之间行可能已被 `/project` 改过。
++ await db
++ .update(sessions)
++ .set({ ...correctedWorkspaceKind(botContextId), updatedAt: Date.now() })
++ .where(eq(sessions.id, id));
++ workspaceKindCorrected = true;
++ }
++ return { row, workspaceKind, revivedFrom, workspaceKindCorrected };
+ });
- const row = result.row;
- if (!row) return null;
++ if (!result) return null;
++ const { row, workspaceKind } = result;
+ if (result.revivedFrom) {
+ log.info(
+ `revived soft-deleted ${ns.source} session id=${row.id} (was ${result.revivedFrom})`,
+ );
// 软删行已从 sidebar 消失,patched 增量对不存在的行无效;
// created 触发 renderer 重拉列表,让会话重新出现。
broadcastSessionCreated(row.id);
- } else if (workspaceKind !== null && workspaceKind !== row.workspaceKind) {
- // 存量脏行**就地回写**, 不等下一次归档。sidebar 的分组直接投影 DB 那一列
- // (localDb/mapper.ts), 只在返回值上现算的话, 会话会一直待在错的分组里 ——
- // 而它现在是 active, 可能永远等不到那次归档。
- //
- // 判据仍走 correctedWorkspaceKind 的 CASE(SQL 里现算), 不把 JS 算出来的值
- // 写回去: 读和写之间行可能已被 `/project` 改过。
- await db
- .update(sessions)
- .set({ ...correctedWorkspaceKind(botContextId), updatedAt: Date.now() })
- .where(eq(sessions.id, id));
++ } else if (result.workspaceKindCorrected) {
+ log.info(`corrected ${ns.source} session workspaceKind id=${row.id} -> ${workspaceKind}`);
+ // 行会跨分组移动, patched 增量覆盖不了归组变化 —— 与 switchSessionWorkingDir 同理。
+ broadcastSessionCreated(row.id);
}
return {
id: row.id,
@@@ -191,50 -336,50 +351,52 @@@
const db = getDbClient().drizzle;
const row = prepared ?? (await this.prepareNewSession(botContextId, userId, scopeKey));
const now = Date.now();
- await db
- .insert(sessions)
- .values({
- id: row.id,
- title: ns.defaultTitle(userId),
- ...(ns.workspaceKind ? { workspaceKind: ns.workspaceKind } : {}),
- workingDir: row.workingDir,
- model: row.model,
- effort: row.effort,
- permissionMode: row.permissionMode,
- fastMode: row.fastMode,
- status: 'active',
- agentKind: toDbAgentKind(row.agentKind),
- providerId: row.providerId,
- source: ns.source,
- ...ns.extraInsertColumns(botContextId, userId),
- createdAt: now,
- updatedAt: now,
- // IM 会话由用户消息触发创建,插入时即设 userSendAt,
- // 避免广播后 renderer 重拉到 userSendAt=null 的行被误判为草稿。
- userSendAt: now,
- })
- .onConflictDoUpdate({
- target: sessions.id,
- set: {
+ const persisted = await withSessionRouteLock(row.id, async () => {
+ await db
+ .insert(sessions)
+ .values({
+ id: row.id,
+ title: ns.defaultTitle(userId),
+ ...(ns.workspaceKind ? { workspaceKind: ns.workspaceKind } : {}),
+ workingDir: row.workingDir,
+ model: row.model,
+ effort: row.effort,
+ permissionMode: row.permissionMode,
+ fastMode: row.fastMode,
status: 'active',
+ agentKind: toDbAgentKind(row.agentKind),
+ providerId: row.providerId,
source: ns.source,
- // 冲突分支撞的是残留行 —— 同 findActiveSession, 用户 `/project`
- // 切出去的归属不能被渠道默认值刷掉。
- ...correctedWorkspaceKind(botContextId),
...ns.extraInsertColumns(botContextId, userId),
+ createdAt: now,
updatedAt: now,
+ // IM 会话由用户消息触发创建,插入时即设 userSendAt,
+ // 避免广播后 renderer 重拉到 userSendAt=null 的行被误判为草稿。
userSendAt: now,
- },
- });
- // upsert 可能走冲突分支(残留行的 sdkSessionId / 模型 / 权限被刻意保留),
- // 返回值必须以 DB 持久化结果为准——直接返回 prepared 默认值会让 turn 拿
- // sdkSessionId=null 新开对话,而 DB 里旧上下文仍标记 active,两边失配。
- const persistedRows = await db
- .select()
- .from(sessions)
- .where(eq(sessions.id, row.id))
- .limit(1);
- const persisted = persistedRows[0];
+ })
+ .onConflictDoUpdate({
+ target: sessions.id,
+ set: {
+ status: 'active',
+ source: ns.source,
- ...(ns.workspaceKind ? { workspaceKind: ns.workspaceKind } : {}),
++ // 冲突分支撞的是残留行 —— 同 findActiveSession, 用户 `/project`
++ // 切出去的归属不能被渠道默认值刷掉。
++ ...correctedWorkspaceKind(botContextId),
+ ...ns.extraInsertColumns(botContextId, userId),
+ updatedAt: now,
+ userSendAt: now,
+ },
+ });
+ // upsert 可能走冲突分支(残留行的 sdkSessionId / 模型 / 权限被刻意保留),
+ // 返回值必须以 DB 持久化结果为准——直接返回 prepared 默认值会让 turn 拿
+ // sdkSessionId=null 新开对话,而 DB 里旧上下文仍标记 active,两边失配。
+ const persistedRows = await db
+ .select()
+ .from(sessions)
+ .where(eq(sessions.id, row.id))
+ .limit(1);
+ return persistedRows[0];
+ });
const result: ImSessionRow = persisted
? {
id: persisted.id,