fix: ACP loadSession 历史记录恢复失败 — 用 resolveSessionFilePath 替代 getProjectDir 定位 session 文件

- params.cwd 可能与 session 文件实际存储的项目目录不一致(子目录、
  hash 算法差异等),导致 getProjectDir 推算出的路径找不到文件
- 改用 resolveSessionFilePath(sessionId, cwd) 按 sessionId 跨项目
  搜索,先精确匹配再 fallback 全项目扫描
- 切换回已缓存的 session 时也回放历史消息给客户端
- createSession 内部 switchSession 保留 sessionProjectDir 不被覆盖为 null
This commit is contained in:
claude-code-best
2026-06-04 21:57:46 +08:00
parent d8892f19d5
commit 01f26cf42b
2 changed files with 84 additions and 16 deletions

View File

@@ -120,6 +120,15 @@ mockModulePreservingExports('../../../utils/listSessionsImpl.ts', {
listSessionsImpl: mock(async () => []),
})
const mockResolveSessionFilePath = mock(async () => ({
filePath: '/fake/project/dir/session.jsonl',
projectPath: '/tmp',
fileSize: 100,
}))
mockModulePreservingExports('../../../utils/sessionStoragePortable.js', {
resolveSessionFilePath: mockResolveSessionFilePath,
})
const mockGetMainLoopModel = mock(() => 'claude-sonnet-4-6')
mockModulePreservingExports('../../../utils/model/model.ts', {
@@ -1166,7 +1175,7 @@ describe('AcpAgent', () => {
test('newSession calls switchSession with the generated sessionId', async () => {
const agent = new AcpAgent(makeConn())
const res = await agent.newSession({ cwd: '/tmp' } as any)
expect(mockSwitchSession).toHaveBeenCalledWith(res.sessionId)
expect(mockSwitchSession).toHaveBeenCalledWith(res.sessionId, null)
})
test('resumeSession calls switchSession with the requested sessionId', async () => {
@@ -1178,7 +1187,10 @@ describe('AcpAgent', () => {
mcpServers: [],
} as any)
expect(mockSwitchSession).toHaveBeenCalledWith(requestedId)
expect(mockSwitchSession).toHaveBeenCalledWith(
requestedId,
expect.any(String),
)
})
test('loadSession calls switchSession with the requested sessionId', async () => {
@@ -1190,7 +1202,10 @@ describe('AcpAgent', () => {
mcpServers: [],
} as any)
expect(mockSwitchSession).toHaveBeenCalledWith(requestedId)
expect(mockSwitchSession).toHaveBeenCalledWith(
requestedId,
expect.any(String),
)
})
test('resumeSession with existing session still calls switchSession', async () => {
@@ -1205,7 +1220,10 @@ describe('AcpAgent', () => {
mcpServers: [],
} as any)
expect(mockSwitchSession).toHaveBeenCalledWith(sessionId)
expect(mockSwitchSession).toHaveBeenCalledWith(
sessionId,
expect.any(String),
)
})
test('prompt does not trigger additional switchSession for multi-session', async () => {