Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 无续接点回退只认当前原生会话的回复、文件回滚失败不吞、关闭会话后可继续回退
Dasha35611de86a2
diff --git a/apps/desktop/src/main/__tests__/rewind.test.ts b/apps/desktop/src/main/__tests__/rewind.test.tsindex 503fe87978..5ff926e57e 100644--- a/apps/desktop/src/main/__tests__/rewind.test.ts+++ b/apps/desktop/src/main/__tests__/rewind.test.ts@@ -178,7 +178,7 @@ const txMock = vi.fn((name: string, args: unknown) => {txCalls.push({ name, args });return Promise.resolve({});});-const queryOneMock = vi.fn(async () => ({ cleared_at: null as number | null }));+const queryOneMock = vi.fn(async (_sql?: string, _params?: unknown[]) => ({ cleared_at: null as number | null, created_at: null as number | null }));vi.mock('../localDb/client/current', () => ({getDbClient: () => ({ drizzle: fakeDb, tx: txMock, queryOne: queryOneMock }),@@ -197,7 +197,7 @@ beforeEach(async () => {txCalls.length = 0;txMock.mockClear();queryOneMock.mockReset();- queryOneMock.mockResolvedValue({ cleared_at: null });+ queryOneMock.mockResolvedValue({ cleared_at: null, created_at: null });setLastAssistantTranscriptUuidMock.mockClear();previewRewindFilesMock.mockReset();commitRewindFilesMock.mockReset();@@ -926,6 +926,40 @@ describe('commitRewindAtMessage', () => {expect(setLastAssistantTranscriptUuidMock).toHaveBeenCalledWith('sess-1', undefined);});+ it('ignores assistant replies from a native session abandoned by a context rebuild', async () => {+ // 重建后的第一条消息:重建前的 assistant 仍可见,但它的 uuid 不在当前原生会话里。+ selectQueue.push([makeUserMessageRow({+ agentMeta: JSON.stringify({ uuid: 'sdk-msg-uuid-target', sdkSessionId: 'sdk-uuid-old' }),+ })]);+ selectQueue.push([]); // agent_switch 边界守卫:无边界+ selectQueue.push([makeAssistantMessageRow({ createdAt: 2500 })]); // 重建前的 assistant+ selectQueue.push([makeSessionRow({ sdkSessionId: null })]);+ queryOneMock.mockImplementation(async (sql?: string) => ({+ cleared_at: null,+ created_at: sql?.includes('context_rebuild') ? 2800 : null,+ }));++ await commitRewindAtMessage('sess-1', 'client-id');++ expect(commitRewindFilesMock).toHaveBeenCalledWith('sdk-msg-uuid-target', '', { rewindsToNativeThreadStart: true });+ expect(closeSessionMock).toHaveBeenCalledWith('sess-1');+ });++ it('keeps rewinding after the session was closed by a previous restart (no live session, no native id)', async () => {+ fakeSession = undefined;+ selectQueue.push([{ agentKind: 'cc', sdkSessionId: null }]); // 脱离会话判定+ selectQueue.push([makeUserMessageRow()]);+ selectQueue.push([]); // agent_switch 边界守卫:无边界+ selectQueue.push([]); // prior assistants+ selectQueue.push([makeSessionRow({ sdkSessionId: null })]);++ await commitRewindAtMessage('sess-1', 'client-id');++ expect(commitRewindFilesMock).not.toHaveBeenCalled();+ const txCall = txCalls.find((c) => c.name === 'rewind.commit');+ expect(txCall?.args).toMatchObject({ nativeContextReset: expect.any(Object) });+ });+it('first message: restarts the native session with an empty handoff', async () => {selectQueue.push([makeUserMessageRow({agentMeta: JSON.stringify({ uuid: 'sdk-msg-uuid-target', sdkSessionId: 'sdk-uuid-old' }),@@ -1151,7 +1185,7 @@ describe('commitRewindAtMessage', () => {enqueueCodexClearGeneration();selectQueue.push([]); // target 之前没有任何行selectQueue.push([]); // 时间线上也没有属于当前线程的 user 行- queryOneMock.mockResolvedValueOnce({ cleared_at: 4000 });+ queryOneMock.mockResolvedValueOnce({ cleared_at: 4000, created_at: null });await expect(commitRewindAtMessage('sess-1', 'client-id')).rejects.toMatchObject({code: 'REWIND_UNSUPPORTED_HISTORY',diff --git a/apps/desktop/src/main/maker-orchestration/rewind.ts b/apps/desktop/src/main/maker-orchestration/rewind.tsindex 9a70833e53..996b6d01b8 100644--- a/apps/desktop/src/main/maker-orchestration/rewind.ts+++ b/apps/desktop/src/main/maker-orchestration/rewind.ts@@ -182,6 +182,11 @@ interface RewindContext {* Pi 同一套),对话只在那台截断。本机 Claude Code 任务不设。*/savepointRewind?: true;+ /**+ * Claude 任务当前没有活动会话也没有原生会话 id(上一次无续接点的回退已关闭会话,下次发送+ * 才会开新会话)。此时没有文件检查点可用,只裁剪对话。+ */+ detached?: true;}/** target 及其之后仍可见的 user 消息(按时间线);不含 target 时说明它已不在当前时间线。 */@@ -222,19 +227,30 @@ async function loadRewindContext(// 1. session live 校验 + running 守卫 (改走 maker.getSession, 重构前是 agentManager.getLiveSession)const maker = getMaker();const makerSession = maker.getSession(sessionId);+ let detached = false;if (!makerSession) {// session 不在内存中:app 刚启动 / 这条 session 还没在新链路激活过。- throw rewindError(- 'NO_LIVE_QUERY',- '会话未激活——请先发送任意一条消息(即使是 "ok"),让 SDK 起来后再 rewind',- );+ // 例外:Claude 任务上一次回退没有续接点,已关闭会话并清掉原生会话 id——没有原生+ // 会话可回滚,继续回退只需要裁剪对话,不必先发一条无关消息把会话叫起来。+ const [row] = await db+ .select({ agentKind: sessions.agentKind, sdkSessionId: sessions.sdkSessionId })+ .from(sessions)+ .where(eq(sessions.id, sessionId))+ .limit(1);+ if (row?.agentKind !== 'cc' || activeSdkSessionId(row.sdkSessionId ?? undefined)) {+ throw rewindError(+ 'NO_LIVE_QUERY',+ '会话未激活——请先发送任意一条消息(即使是 "ok"),让 SDK 起来后再 rewind',+ );+ }+ detached = true;}- if (makerSession.isTurnRunning()) {+ if (makerSession?.isTurnRunning()) {throw rewindError('SESSION_RUNNING', '会话进行中,无法回滚');}- const agentKind = makerSession.agentKind === 'codex'+ const agentKind = makerSession?.agentKind === 'codex'? 'codex'- : makerSession.agentKind === 'pi'+ : makerSession?.agentKind === 'pi'? 'pi': 'claude-code';@@ -333,11 +349,12 @@ async function loadRewindContext(// 锚点会让 resumeSessionAt 指向一条逻辑上已不存在的 assistant,CLI 找不到。const targetMeta = parseClaudeAgentMeta(target.agentMeta);const currentSessionMeta = await maker.getSessionMeta(sessionId);- const sdkSessionId =- activeSdkSessionId(makerSession.sdkSessionId) ??- activeSdkSessionId(currentSessionMeta?.sdkSessionId) ??- targetMeta.sdkSessionId;- const anchorIndex = sdkSessionId+ const sdkSessionId = detached+ ? undefined+ : activeSdkSessionId(makerSession?.sdkSessionId) ??+ activeSdkSessionId(currentSessionMeta?.sdkSessionId) ??+ targetMeta.sdkSessionId;+ const anchorIndex = sdkSessionId && makerSession? await loadClaudeTranscriptAnchorIndex({sdkSessionId,workingDir: makerSession.workDir,@@ -346,17 +363,28 @@ async function loadRewindContext(const assistantAnchor = resolveClaudeRewindAssistantEntry(targetMeta.uuid, anchorIndex);let assistantUuid = assistantAnchor?.uuid;- const visibleAssistants = await db- .select()- .from(messages)- .where(- and(- eq(messages.sessionId, sessionId),- eq(messages.role, 'assistant'),- isNull(messages.rewindAt),- ),- )- .orderBy(asc(messages.createdAt));+ // 只有当前原生会话里的回复才能当续接点:上下文重建(溢出、删除消息、上次无续接点的回退)+ // 与 /clear 都会放弃旧原生会话,而旧回复仍留在可见历史里。context_rebuild 行的+ // rewind_at 恒非空,所以这里不能带可见性过滤。+ const latestRebuild = await getDbClient().queryOne<{ created_at: number | null }>(+ "SELECT MAX(created_at) AS created_at FROM messages WHERE session_id = ? AND role = 'context_rebuild'",+ [sessionId],+ );+ const clearedAt = await rereadSessionClearedAt(sessionId);+ const nativeSessionStartedAt = Math.max(latestRebuild?.created_at ?? 0, clearedAt ?? 0);+ const visibleAssistants = (+ await db+ .select()+ .from(messages)+ .where(+ and(+ eq(messages.sessionId, sessionId),+ eq(messages.role, 'assistant'),+ isNull(messages.rewindAt),+ ),+ )+ .orderBy(asc(messages.createdAt))+ ).filter((row) => row.createdAt > nativeSessionStartedAt);let preserveMessageUuid = findDbPreserveMessageUuidForClaudeAnchor(visibleAssistants, assistantAnchor);if (!assistantUuid && targetMeta.transcriptParentUuid) {for (let i = visibleAssistants.length - 1; i >= 0; i--) {@@ -405,10 +433,11 @@ async function loadRewindContext(targetMessageId: target.id,targetClientId: target.clientId,agentKind,- assistantUuid,+ assistantUuid: detached ? undefined : assistantUuid,userUuid,preserveMessageUuid: preserveMessageUuid ?? assistantUuid,...savepointTimeline,+ ...(detached ? { detached: true as const } : {}),};}@@ -429,6 +458,10 @@ export async function previewRewindAtMessage(const makerSession = getMaker().getSession(sessionId);if (!makerSession) {+ if (ctx.detached) {+ // 没有原生会话也就没有文件检查点:只裁剪对话。+ return { canRewind: true, conversationOnly: true, filesChanged: [], insertions: 0, deletions: 0 };+ }throw rewindError('NO_LIVE_QUERY', '会话未激活');}if (ctx.agentKind === 'codex' || ctx.agentKind === 'pi' || ctx.savepointRewind) {@@ -802,18 +835,23 @@ export async function commitRewindAtMessage(// makerSession 必有 (loadRewindContext 已 guard); 这里只为类型收紧。const makerSession = getMaker().getSession(sessionId);- if (!makerSession) {+ if (!makerSession && !ctx.detached) {throw rewindError('NO_LIVE_QUERY', '会话未激活');}// ── 段 1: SDK 副作用全部委托给 maker-core ──// userUuid 缺失 (老消息) 时跳 SDK 文件回滚, 直接进 DB 段; ctx.assistantUuid 设进// pendingRewindTo, 下次 send 仍走三件套 (forkSession=true CLI 端兜底回滚)。- let rewindResult: Awaited<ReturnType<typeof makerSession.commitRewindFiles>> | undefined;+ let rewindResult: Awaited<ReturnType<NonNullable<typeof makerSession>['commitRewindFiles']>> | undefined;let nativeForkAnchorSessionMap: Array<[string, string]> | undefined;let nativeContextReset: RewindCommitArgs['nativeContextReset'];let expectedClearedAt: number | null = null;- if (ctx.agentKind === 'codex' || ctx.agentKind === 'pi') {+ if (!makerSession) {+ // 脱离会话(见 RewindContext.detached):没有原生会话和文件检查点,只裁剪对话并留下交接。+ expectedClearedAt = await rereadSessionClearedAt(sessionId);+ await assertClearGenerationUnchanged(sessionId, expectedClearedAt, ctx.targetCreatedAt);+ nativeContextReset = await buildNativeContextReset(sessionId, ctx.targetClientId);+ } else if (ctx.agentKind === 'codex' || ctx.agentKind === 'pi') {// thread/rollback 不可用(分页线程 #4421、0.156.0 起已移除 #4994):把 target 之前的// 原生 turn 边界(持久化锚点、事件时间戳或线程第一轮标记)一并交给 maker-core,// 由它改走 thread/fork 或换空线程。diff --git a/packages/maker-core/src/agents/claude-code/__tests__/rewind-runtime-settings.test.ts b/packages/maker-core/src/agents/claude-code/__tests__/rewind-runtime-settings.test.tsindex 287a01c3df..e82e4ab7aa 100644--- a/packages/maker-core/src/agents/claude-code/__tests__/rewind-runtime-settings.test.ts+++ b/packages/maker-core/src/agents/claude-code/__tests__/rewind-runtime-settings.test.ts@@ -660,6 +660,18 @@ describe('ClaudeCodeAgent runtime settings during rewind window', () => {await handle.close();});+ it('surfaces a file restore failure instead of reporting a files-only rewind as done', async () => {+ const { handle, firstQuery } = await startRewindableSession();+ firstQuery.rewindFiles.mockRejectedValueOnce(new Error('EACCES: cannot restore'));++ await expect(+ handle.commitRewindFiles?.('user-uuid-1', '', { rewindsToNativeThreadStart: true }),+ ).rejects.toThrow('EACCES');+ expect(firstQuery.close).not.toHaveBeenCalled();++ await handle.close();+ });+it('uses the session provider route when the same model id has different windows', async () => {const configDir = await makeTempDir();process.env.CLAUDE_CONFIG_DIR = configDir;diff --git a/packages/maker-core/src/agents/claude-code/index.ts b/packages/maker-core/src/agents/claude-code/index.tsindex 8257b1dfe8..dd770c56a1 100644--- a/packages/maker-core/src/agents/claude-code/index.ts+++ b/packages/maker-core/src/agents/claude-code/index.ts@@ -7908,6 +7908,14 @@ export class ClaudeCodeAgent extends BaseAgent {await q.rewindFiles(userUuid, { dryRun: false });log.debug('commitRewindFiles: SDK rewindFiles ok');} catch (err) {+ if (rewindOpts?.rewindsToNativeThreadStart) {+ // 只回滚文件的路径没有下一次 send 的 forkSession 兜底重试,吞掉错误会让+ // 宿主继续关闭会话、软删消息,用户看到回退成功但文件没恢复。+ log.warn('commitRewindFiles: rewindFiles failed on files-only rewind', {+ error: err instanceof Error ? err.message : String(err),+ });+ throw err;+ }log.warn('commitRewindFiles: rewindFiles failed, continuing (forkSession on next send will retry)', {error: err instanceof Error ? err.message : String(err),});