Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(agent): refresh branch after CLI checkout (#17090)

Arvin Xu6118cde456af
diff --git a/src/store/tool/slices/builtin/executors/__tests__/worktreeDetection.test.ts b/src/store/tool/slices/builtin/executors/__tests__/worktreeDetection.test.ts
index 642f0be5c20..f7a45000373 100644
--- a/src/store/tool/slices/builtin/executors/__tests__/worktreeDetection.test.ts
+++ b/src/store/tool/slices/builtin/executors/__tests__/worktreeDetection.test.ts
@@ -19,6 +19,12 @@ const gitServiceMocks = vi.hoisted(() => ({
listGitWorktrees: vi.fn(),
}));
+const swrMocks = vi.hoisted(() => ({
+ mutate: vi.fn(),
+}));
+
+vi.mock('@/libs/swr', () => swrMocks);
+
vi.mock('@/store/chat/store', () => ({
getChatStoreState: () => chatMocks,
}));
@@ -246,6 +252,44 @@ describe('recordWorktreeAdd', () => {
});
describe('recordGitCommandEffects', () => {
+ it('refreshes the local branch cache when the run is bound to this device', async () => {
+ chatMocks.topics = {
+ t1: {
+ metadata: {
+ boundDeviceId: 'device-1',
+ workingDirectoryConfig: {
+ git: { branch: 'canary' },
+ path: '/repo',
+ repoType: 'github',
+ },
+ },
+ },
+ };
+
+ await recordGitCommandEffects({ command: 'git switch fix/topic', topicId: 't1' });
+
+ expect(swrMocks.mutate).toHaveBeenCalledWith(['device:gitBranch', 'local', '/repo']);
+ });
+
+ it('refreshes the remote-device branch cache after a branch switch', async () => {
+ chatMocks.topics = {
+ t1: {
+ metadata: {
+ boundDeviceId: 'remote-device',
+ workingDirectoryConfig: {
+ git: { branch: 'canary' },
+ path: '/repo',
+ repoType: 'github',
+ },
+ },
+ },
+ };
+
+ await recordGitCommandEffects({ command: 'git switch fix/topic', topicId: 't1' });
+
+ expect(swrMocks.mutate).toHaveBeenCalledWith(['device:gitBranch', 'remote-device', '/repo']);
+ });
+
it('updates the topic branch when the agent switches branch with git switch', async () => {
chatMocks.topics = {
t1: {
diff --git a/src/store/tool/slices/builtin/executors/worktreeDetection.ts b/src/store/tool/slices/builtin/executors/worktreeDetection.ts
index 1966db4fdcf..d4f106d2c15 100644
--- a/src/store/tool/slices/builtin/executors/worktreeDetection.ts
+++ b/src/store/tool/slices/builtin/executors/worktreeDetection.ts
@@ -3,6 +3,8 @@ import type { DeviceGitLinkedPullRequest, WorkingDirConfig } from '@lobechat/typ
import { getWorkingDirSourcePath } from '@lobechat/types';
import isEqual from 'fast-deep-equal';
+import { mutate } from '@/libs/swr';
+import { deviceKeys } from '@/libs/swr/keys';
import { gitService } from '@/services/git';
import { topicSelectors } from '@/store/chat/selectors';
import { getChatStoreState } from '@/store/chat/store';
@@ -875,8 +877,21 @@ export const recordGitCommandEffects = async (params: {
nextConfig = applyPullRequestToConfig(nextConfig, source, prCreate);
}
- if (isEqual(currentConfig, nextConfig)) return;
- await state.updateTopicMetadata(topicId, { workingDirectoryConfig: nextConfig });
+ if (!isEqual(currentConfig, nextConfig)) {
+ await state.updateTopicMetadata(topicId, { workingDirectoryConfig: nextConfig });
+ }
+
+ // A checkout performed inside the heterogeneous CLI bypasses ChatInput's
+ // BranchSwitcher, which normally refreshes this cache itself. Revalidate the
+ // shared branch key so the control bar (and its branch-keyed PR lookup) follows
+ // the repository's actual HEAD immediately after the tool call completes.
+ if (branchSwitch) {
+ const boundDeviceId = topic?.metadata?.boundDeviceId;
+ const currentDeviceId = getElectronStoreState().gatewayDeviceInfo?.deviceId;
+ const cacheDeviceId =
+ currentDeviceId && boundDeviceId === currentDeviceId ? 'local' : boundDeviceId;
+ await mutate(deviceKeys.gitBranch(cacheDeviceId ?? 'local', source));
+ }
};
/**