feat(core): add prePlanMode tracking to restore previous approval mode

When entering plan mode, Config now saves the previous approval mode
(e.g. AUTO_EDIT, YOLO) so it can be restored when exiting. Previously,
/plan execute and ExitPlanModeTool both hardcoded a return to DEFAULT,
losing the user's prior mode.

Changes:
- Config: add prePlanMode field, getPrePlanMode(), auto-track in
  setApprovalMode()
- planCommand: /plan execute restores prePlanMode instead of DEFAULT
- ExitPlanModeTool: ProceedOnce restores prePlanMode instead of DEFAULT
- Tests: 4 new Config tests, 1 new planCommand test, 1 new
  ExitPlanModeTool test

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
wenshao 2026-04-06 17:39:13 +08:00
parent 09ccc1b896
commit c597847bc3
6 changed files with 105 additions and 2 deletions

View file

@ -18,6 +18,7 @@ describe('planCommand', () => {
services: {
config: {
getApprovalMode: vi.fn().mockReturnValue(ApprovalMode.DEFAULT),
getPrePlanMode: vi.fn().mockReturnValue(ApprovalMode.DEFAULT),
setApprovalMode: vi.fn(),
} as unknown as import('@qwen-code/qwen-code-core').Config,
},
@ -116,6 +117,30 @@ describe('planCommand', () => {
});
});
it('should restore pre-plan mode when executing from plan mode', async () => {
if (!planCommand.action) {
throw new Error('The plan command must have an action.');
}
(mockContext.services.config?.getApprovalMode as Mock).mockReturnValue(
ApprovalMode.PLAN,
);
(mockContext.services.config?.getPrePlanMode as Mock).mockReturnValue(
ApprovalMode.AUTO_EDIT,
);
const result = await planCommand.action(mockContext, 'execute');
expect(mockContext.services.config?.setApprovalMode).toHaveBeenCalledWith(
ApprovalMode.AUTO_EDIT,
);
expect(result).toEqual({
type: 'message',
messageType: 'info',
content: 'Exited plan mode. The agent will now execute the plan.',
});
});
it('should return error when execute is used but not in plan mode', async () => {
if (!planCommand.action) {
throw new Error('The plan command must have an action.');

View file

@ -45,7 +45,7 @@ export const planCommand: SlashCommand = {
};
}
try {
config.setApprovalMode(ApprovalMode.DEFAULT);
config.setApprovalMode(config.getPrePlanMode());
} catch (e) {
return {
type: 'message',