fix: preserve cached session restore by default

This commit is contained in:
coso
2026-05-24 11:46:13 +08:00
parent 5ce1228bf6
commit f5e23ef164
4 changed files with 56 additions and 19 deletions
@@ -3991,12 +3991,12 @@ export function AgentChatWorkspace({
const resolveInitialSessionSwitch = useCallback(
(topicId: string) => {
const topic = topicById.get(topicId);
const shouldResume = shouldResumeTaskSession(topic);
const shouldForceRefresh = topic?.statusReason === "workspace_error";
return {
allowDetachedSession: true,
forceRefresh: true,
...(shouldResumeTaskSession(topic)
? { resumeSessionStartHooks: true }
: {}),
...(shouldForceRefresh ? { forceRefresh: true } : {}),
...(shouldResume ? { resumeSessionStartHooks: true } : {}),
};
},
[topicById],
+39 -1
View File
@@ -1410,7 +1410,7 @@ afterEach(() => {
});
describe("AgentChatPage 任务中心初始会话标签", () => {
it("路由初始会话恢复应强制刷新,避免旧缓存串到目标会话", async () => {
it("路由普通历史会话恢复不应强制刷新,应复用本地缓存主链", async () => {
const state: Record<string, unknown> = createMockAgentChatUnifiedState({
sessionId: null,
topics: [
@@ -1442,7 +1442,45 @@ describe("AgentChatPage 任务中心初始会话标签", () => {
expect(switchTopic).toHaveBeenCalledWith("topic-target", {
allowDetachedSession: true,
});
});
it("路由恢复工作区错误会话时仍应强制刷新并恢复启动钩子", async () => {
const state: Record<string, unknown> = createMockAgentChatUnifiedState({
sessionId: null,
topics: [
{
id: "topic-workspace-error",
title: "工作区错误会话",
updatedAt: new Date(FIXED_TOPIC_UPDATED_AT),
workspaceId: "workspace-test",
status: "failed",
statusReason: "workspace_error",
},
],
});
const switchTopic = vi.fn(async (topicId: string) => {
state.sessionId = topicId;
});
state.switchTopic = switchTopic;
installMockAgentChatUnifiedState(state);
const { container } = mountPage({
agentEntry: "claw",
initialSessionId: "topic-workspace-error",
projectId: "workspace-test",
});
await waitForElement(
container,
'[data-testid="workspace-shell-scene"]',
80,
);
await flushEffects();
expect(switchTopic).toHaveBeenCalledWith("topic-workspace-error", {
allowDetachedSession: true,
forceRefresh: true,
resumeSessionStartHooks: true,
});
});
@@ -95,9 +95,7 @@ describe("useWorkspaceInitialSessionNavigation", () => {
await flushEffects();
expect(switchTopic).toHaveBeenCalledWith("session-42", {
forceRefresh: true,
});
expect(switchTopic).toHaveBeenCalledWith("session-42", undefined);
});
it("当前已经在目标会话时不应重复切换", async () => {
@@ -130,12 +128,8 @@ describe("useWorkspaceInitialSessionNavigation", () => {
});
await flushEffects();
expect(switchTopic).toHaveBeenNthCalledWith(1, "session-42", {
forceRefresh: true,
});
expect(switchTopic).toHaveBeenNthCalledWith(2, "session-99", {
forceRefresh: true,
});
expect(switchTopic).toHaveBeenNthCalledWith(1, "session-42", undefined);
expect(switchTopic).toHaveBeenNthCalledWith(2, "session-99", undefined);
});
it("应透传解析后的初始会话切换选项", async () => {
@@ -210,8 +204,6 @@ describe("useWorkspaceInitialSessionNavigation", () => {
await flushEffects();
expect(switchTopic).toHaveBeenCalledTimes(1);
expect(switchTopic).toHaveBeenCalledWith("session-dedupe", {
forceRefresh: true,
});
expect(switchTopic).toHaveBeenCalledWith("session-dedupe", undefined);
});
});
@@ -123,6 +123,7 @@ export function useWorkspaceInitialSessionNavigation({
);
logAgentDebug("AgentChatPage", "initialSessionNavigation.start", {
currentSessionId: normalizedCurrentSessionId,
forceRefresh: resolvedSwitchOptions?.forceRefresh === true,
initialSessionId: normalizedInitialSessionId,
resumeSessionStartHooks:
resolvedSwitchOptions?.resumeSessionStartHooks === true,
@@ -131,7 +132,9 @@ export function useWorkspaceInitialSessionNavigation({
});
const switchOptions: InitialSessionSwitchOptions = {
forceRefresh: resolvedSwitchOptions?.forceRefresh ?? true,
...(resolvedSwitchOptions?.forceRefresh === true
? { forceRefresh: true }
: {}),
...(resolvedSwitchOptions?.resumeSessionStartHooks === true
? { resumeSessionStartHooks: true }
: {}),
@@ -139,8 +142,12 @@ export function useWorkspaceInitialSessionNavigation({
? { allowDetachedSession: true }
: {}),
};
const hasSwitchOptions = Object.keys(switchOptions).length > 0;
void switchTopic(normalizedInitialSessionId, switchOptions).catch(
void switchTopic(
normalizedInitialSessionId,
hasSwitchOptions ? switchOptions : undefined,
).catch(
(error) => {
appliedInitialSessionIdRef.current = null;
recentInitialSessionNavigationStarts.delete(normalizedInitialSessionId);