Repository navigation
feat(session): add /branch to fork the current conversation #3539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e43def1
feat(session): add /branch to fork the current conversation
qqqys da1c2dd
Merge branch 'QwenLM:main' into main
qqqys 5467afa
fix(session): drop stale `commandType` field from branchCommand
qqqys e526c5d
fix(session): roll core back to parent when /branch post-fork init th…
qqqys 8ac4af2
fix(session): close /branch transactional swap holes flagged in review
qqqys e1b7c42
Merge branch 'main' of https://github.com/qqqys/qwen-code into feat/b…
qqqys 1ad9ace
Merge branch 'main' of https://github.com/qqqys/qwen-code into feat/b…
qqqys e03596d
perf(session): fold /branch (Branch N) collision lookup into one scan
qqqys 82cdf67
Merge branch 'main' of https://github.com/qqqys/qwen-code into feat/b…
qqqys 36693a3
test(cli): tighten mocks and drop dead assertion in slashCommandProce…
qqqys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen Code | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { branchCommand } from './branchCommand.js'; | ||
| import type { CommandContext } from './types.js'; | ||
|
|
||
| function makeCtx( | ||
| overrides: { | ||
| isIdle?: boolean; | ||
| sessionExists?: boolean; | ||
| noConfig?: boolean; | ||
| } = {}, | ||
| ): CommandContext { | ||
| const sessionService = { | ||
| sessionExists: vi.fn().mockResolvedValue(overrides.sessionExists ?? true), | ||
| }; | ||
| const config = overrides.noConfig | ||
| ? null | ||
| : ({ | ||
| getSessionId: () => '11111111-1111-1111-1111-111111111111', | ||
| getSessionService: () => sessionService, | ||
| } as unknown as NonNullable<CommandContext['services']['config']>); | ||
| return { | ||
| services: { config, settings: {} as never, git: undefined, logger: null }, | ||
| ui: { | ||
| isIdleRef: { current: overrides.isIdle ?? true }, | ||
| } as unknown as CommandContext['ui'], | ||
| session: { stats: {} as never, sessionShellAllowlist: new Set() }, | ||
| } as unknown as CommandContext; | ||
| } | ||
|
|
||
| describe('branchCommand', () => { | ||
| it('rejects when config is unavailable', async () => { | ||
| const result = await branchCommand.action!(makeCtx({ noConfig: true }), ''); | ||
| expect(result).toMatchObject({ type: 'message', messageType: 'error' }); | ||
| }); | ||
|
|
||
| it('rejects when no conversation exists to branch from', async () => { | ||
| const result = await branchCommand.action!( | ||
| makeCtx({ sessionExists: false }), | ||
| '', | ||
| ); | ||
| expect(result).toMatchObject({ type: 'message', messageType: 'error' }); | ||
| expect((result as { content: string }).content).toMatch( | ||
| /No conversation to branch/, | ||
| ); | ||
| }); | ||
|
|
||
| it('rejects while streaming or awaiting a tool confirmation', async () => { | ||
| const result = await branchCommand.action!(makeCtx({ isIdle: false }), ''); | ||
| expect(result).toMatchObject({ type: 'message', messageType: 'error' }); | ||
| expect((result as { content: string }).content).toMatch(/in progress/); | ||
| }); | ||
|
|
||
| it('returns dialog action with no name when args are empty', async () => { | ||
| const result = await branchCommand.action!(makeCtx(), ' '); | ||
| expect(result).toEqual({ type: 'dialog', dialog: 'branch' }); | ||
| }); | ||
|
|
||
| it('returns dialog action with trimmed name when args are provided', async () => { | ||
| const result = await branchCommand.action!(makeCtx(), ' my-branch '); | ||
| expect(result).toEqual({ | ||
| type: 'dialog', | ||
| dialog: 'branch', | ||
| name: 'my-branch', | ||
| }); | ||
| }); | ||
|
|
||
| it('exposes /fork as an alias', () => { | ||
| expect(branchCommand.altNames).toContain('fork'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen Code | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import type { SlashCommand, SlashCommandActionReturn } from './types.js'; | ||
| import { CommandKind } from './types.js'; | ||
| import { t } from '../../i18n/index.js'; | ||
|
|
||
| export const branchCommand: SlashCommand = { | ||
| name: 'branch', | ||
| altNames: ['fork'], | ||
| kind: CommandKind.BUILT_IN, | ||
| get description() { | ||
| return t('Fork the current conversation into a new session'); | ||
| }, | ||
| action: async (context, args): Promise<SlashCommandActionReturn> => { | ||
| const { config } = context.services; | ||
| if (!config) { | ||
| return { | ||
| type: 'message', | ||
| messageType: 'error', | ||
| content: t('Config is not available.'), | ||
| }; | ||
| } | ||
|
|
||
| // Guard: streaming or awaiting tool confirmation — forking mid-flight | ||
| // would tear the new session's parent chain. | ||
| if (context.ui.isIdleRef?.current === false) { | ||
| return { | ||
| type: 'message', | ||
| messageType: 'error', | ||
| content: t( | ||
| 'Cannot branch while a response or tool call is in progress. Wait for it to finish or resolve the pending tool call.', | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| // Guard: nothing to fork from. | ||
| const sessionService = config.getSessionService(); | ||
| const currentId = config.getSessionId(); | ||
| const hasRecords = await sessionService.sessionExists(currentId); | ||
| if (!hasRecords) { | ||
| return { | ||
| type: 'message', | ||
| messageType: 'error', | ||
| content: t('No conversation to branch.'), | ||
| }; | ||
| } | ||
|
|
||
| const name = args.trim().replace(/[\r\n]+/g, ' '); | ||
| return ( | ||
| name | ||
| ? { type: 'dialog', dialog: 'branch', name } | ||
| : { type: 'dialog', dialog: 'branch' } | ||
| ) as SlashCommandActionReturn; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1187,4 +1187,53 @@ describe('useSlashCommandProcessor', () => { | |||||||||
| ).toBeNull(); | ||||||||||
| }); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| describe('SLASH_COMMANDS_SKIP_RECORDING', () => { | ||||||||||
| // Why these live in the skip set: the fork itself is the side effect | ||||||||||
| // (new JSONL file with full parent history), so also writing a | ||||||||||
| // `/branch <name>` slash-command record into the parent session would | ||||||||||
| // bleed into the fork's tail as a trailing user input — user-visible | ||||||||||
| // noise with no semantic value. Same rationale for /new, /resume, | ||||||||||
| // /delete, /clear: session-level commands whose outcome is the new | ||||||||||
| // session state, not a conversation turn. | ||||||||||
| it('does not record /branch via the chat recorder', async () => { | ||||||||||
| const branchCmd = createTestCommand({ | ||||||||||
| name: 'branch', | ||||||||||
| action: vi.fn().mockResolvedValue({ type: 'dialog', dialog: 'branch' }), | ||||||||||
| }); | ||||||||||
| const result = setupProcessorHook([branchCmd]); | ||||||||||
| await waitFor(() => expect(result.current.slashCommands).toHaveLength(1)); | ||||||||||
|
|
||||||||||
| const recorder = mockConfig.getChatRecordingService() as { | ||||||||||
| recordSlashCommand: ReturnType<typeof vi.fn>; | ||||||||||
| }; | ||||||||||
| recorder.recordSlashCommand.mockClear(); | ||||||||||
|
|
||||||||||
| await act(async () => { | ||||||||||
| await result.current.handleSlashCommand('/branch my-branch'); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| expect(recorder.recordSlashCommand).not.toHaveBeenCalled(); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| it('still records unrelated commands via the chat recorder (control)', async () => { | ||||||||||
| const testCmd = createTestCommand({ | ||||||||||
| name: 'regular', | ||||||||||
| action: vi.fn().mockResolvedValue(undefined), | ||||||||||
| }); | ||||||||||
| const result = setupProcessorHook([testCmd]); | ||||||||||
| await waitFor(() => expect(result.current.slashCommands).toHaveLength(1)); | ||||||||||
|
|
||||||||||
| const recorder = mockConfig.getChatRecordingService() as { | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Critical] 第二个
Suggested change
— deepseek-v4-pro via Qwen Code /review |
||||||||||
| recordSlashCommand: ReturnType<typeof vi.fn>; | ||||||||||
| }; | ||||||||||
| recorder.recordSlashCommand.mockClear(); | ||||||||||
|
|
||||||||||
| await act(async () => { | ||||||||||
| await result.current.handleSlashCommand('/regular'); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| expect(recorder.recordSlashCommand).toHaveBeenCalled(); | ||||||||||
| }); | ||||||||||
| }); | ||||||||||
| }); | ||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Critical]
ChatRecordingService | undefined强制转换为{ recordSlashCommand: Mock<Procedure>; }— 类型没有充分重叠(TS2352)。需要先通过unknown进行中间转换。— deepseek-v4-pro via Qwen Code /review