Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions docs/users/configuration/settings.md

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions packages/cli/src/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3138,3 +3138,41 @@ describe('loadCliConfig runtimeOutputDir', () => {
expect(Storage.getRuntimeBaseDir()).toBe(Storage.getGlobalQwenDir());
});
});

describe('loadCliConfig plansDirectory', () => {
const originalArgv = process.argv;

beforeEach(() => {
process.argv = ['node', 'script.js'];
});

afterEach(() => {
process.argv = originalArgv;
vi.restoreAllMocks();
});

it('should resolve relative plansDirectory against cwd', async () => {
const argv = await parseArguments();
const cwd = path.resolve('workspace', 'my-project');
const settings: Settings = {
plansDirectory: './project-plans',
};

const config = await loadCliConfig(settings, argv, cwd);

expect(config.getPlansDir()).toBe(path.join(cwd, 'project-plans'));
expect(config.getPlanFilePath()).toContain(path.join(cwd, 'project-plans'));
});

it('should reject plansDirectory values outside cwd', async () => {
const argv = await parseArguments();
const cwd = path.resolve('workspace', 'my-project');
const settings: Settings = {
plansDirectory: '../plans',
};

await expect(loadCliConfig(settings, argv, cwd)).rejects.toThrow(
'plansDirectory must resolve within the project root',
);
});
});
1 change: 1 addition & 0 deletions packages/cli/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,7 @@ export async function loadCliConfig(
fileFiltering: settings.context?.fileFiltering,
checkpointing:
argv.checkpointing || settings.general?.checkpointing?.enabled,
plansDirectory: bareMode ? undefined : settings.plansDirectory,
Comment thread
shenyankm marked this conversation as resolved.
Resolved
proxy:
argv.proxy ||
settings.proxy ||
Expand Down
13 changes: 10 additions & 3 deletions packages/cli/src/config/settingsSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ describe('SettingsSchema', () => {
'ide',
'privacy',
'telemetry',
'proxy',
'model',
'context',
'tools',
'mcp',
'security',
'advanced',
'plansDirectory',
];

expectedSettings.forEach((setting) => {
Expand Down Expand Up @@ -129,6 +129,15 @@ describe('SettingsSchema', () => {
expect(getSettingsSchema().proxy.showInDialog).toBe(false);
});

it('should have plansDirectory setting in schema', () => {
expect(getSettingsSchema().plansDirectory).toBeDefined();
expect(getSettingsSchema().plansDirectory.type).toBe('string');
expect(getSettingsSchema().plansDirectory.category).toBe('Advanced');
expect(getSettingsSchema().plansDirectory.default).toBe(undefined);
expect(getSettingsSchema().plansDirectory.requiresRestart).toBe(true);
expect(getSettingsSchema().plansDirectory.showInDialog).toBe(false);
});

it('should have unique categories', () => {
const categories = new Set();

Expand Down Expand Up @@ -251,15 +260,13 @@ describe('SettingsSchema', () => {
includeDirectories: ['/path/to/dir'],
loadFromIncludeDirectories: true,
},
proxy: 'http://localhost:7890',
};

// TypeScript should not complain about these properties
expect(settings.ui?.theme).toBe('dark');
expect(settings.ui?.renderMode).toBe('raw');
expect(settings.context?.includeDirectories).toEqual(['/path/to/dir']);
expect(settings.context?.loadFromIncludeDirectories).toBe(true);
expect(settings.proxy).toBe('http://localhost:7890');
});

it('should have includeDirectories setting in schema', () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/config/settingsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ const SETTINGS_SCHEMA = {
mergeStrategy: MergeStrategy.REPLACE,
},

plansDirectory: {
Comment thread
shenyankm marked this conversation as resolved.
Resolved
type: 'string',
label: 'Plans Directory',
category: 'Advanced',
requiresRestart: true,
default: undefined as string | undefined,
description:
'Custom directory for approved Plan Mode files. Relative paths are resolved from the project root, and the resolved path must stay within the project root. Defaults to ~/.qwen/plans.',
showInDialog: false,
},

// Environment variables fallback
env: {
type: 'object',
Expand Down
40 changes: 38 additions & 2 deletions packages/core/src/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ vi.mock('node:fs', async (importOriginal) => {
realpathSync: vi.fn((path) => path),
mkdirSync: vi.fn(),
writeFileSync: vi.fn(),
renameSync: vi.fn(),
readFileSync: vi.fn(),
};
return {
Expand Down Expand Up @@ -1834,7 +1835,7 @@ describe('setApprovalMode with folder trust', () => {
});

describe('plan file persistence', () => {
it('should save plan to disk', () => {
it('should save plan to disk atomically', () => {
const config = new Config(baseParams);

config.savePlan('# My Plan\n1. Step one\n2. Step two');
Expand All @@ -1843,11 +1844,17 @@ describe('setApprovalMode with folder trust', () => {
expect.stringContaining('plans'),
{ recursive: true },
);
// Writes to temp file first
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('.md'),
expect.stringContaining('.tmp'),
'# My Plan\n1. Step one\n2. Step two',
'utf-8',
);
// Then atomically renames to final path
expect(fs.renameSync).toHaveBeenCalledWith(
expect.stringContaining('.tmp'),
expect.stringContaining('.md'),
);
});

it('should load plan from disk', () => {
Expand Down Expand Up @@ -1891,6 +1898,35 @@ describe('setApprovalMode with folder trust', () => {
expect(filePath).toContain('test-session-123');
expect(filePath).toMatch(/\.md$/);
});

it('should use configured plansDirectory for plan file path', () => {
const config = new Config({
...baseParams,
sessionId: 'test-session-123',
plansDirectory: './project-plans',
});

expect(config.getPlansDir()).toBe(
path.join(path.resolve(baseParams.targetDir), 'project-plans'),
);
expect(config.getPlanFilePath()).toBe(
path.join(
path.resolve(baseParams.targetDir),
'project-plans',
'test-session-123.md',
),
);
});

it('should reject configured plansDirectory outside targetDir', () => {
expect(
() =>
new Config({
...baseParams,
plansDirectory: '../project-plans',
}),
).toThrow('plansDirectory must resolve within the project root');
});
});

describe('registerCoreTools', () => {
Expand Down
19 changes: 17 additions & 2 deletions packages/core/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ export interface ConfigParameters {
enableFuzzySearch?: boolean;
};
checkpointing?: boolean;
/** Directory where approved plan files are stored. Must resolve inside targetDir. */
plansDirectory?: string;
proxy?: string;
cwd: string;
fileDiscoveryService?: FileDiscoveryService;
Expand Down Expand Up @@ -781,6 +783,7 @@ export class Config {
private readonly jsonFile: string | undefined;
private readonly jsonSchema: Record<string, unknown> | undefined;
private readonly inputFile: string | undefined;
private readonly plansDir: string;
private readonly defaultFileEncoding: FileEncodingType | undefined;
private readonly enableManagedAutoMemory: boolean;
private readonly enableManagedAutoDream: boolean;
Expand All @@ -807,6 +810,7 @@ export class Config {
this.fileSystemService = new StandardFileSystemService();
this.sandbox = params.sandbox;
this.targetDir = path.resolve(params.targetDir);
this.plansDir = Storage.getPlansDir(this.targetDir, params.plansDirectory);
Comment thread
shenyankm marked this conversation as resolved.
Resolved
this.explicitIncludeDirectories = Array.from(
new Set(params.includeDirectories ?? []),
);
Expand Down Expand Up @@ -2114,11 +2118,18 @@ export class Config {
this.approvalMode = mode;
}

/**
* Returns the directory where this session's plan file is stored.
*/
getPlansDir(): string {
return this.plansDir;
}

/**
* Returns the file path for this session's plan file.
*/
getPlanFilePath(): string {
return Storage.getPlanFilePath(this.sessionId);
return path.join(this.plansDir, `${this.sessionId}.md`);
}

/**
Expand All @@ -2128,7 +2139,11 @@ export class Config {
const filePath = this.getPlanFilePath();
const dir = path.dirname(filePath);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(filePath, plan, 'utf-8');
// Write to a temp file first, then atomically rename to avoid
// leaving a corrupted file if the process crashes mid-write.
const tmpPath = filePath + '.tmp';
fs.writeFileSync(tmpPath, plan, 'utf-8');
fs.renameSync(tmpPath, filePath);
Comment thread
shenyankm marked this conversation as resolved.
Resolved
}

/**
Expand Down
Loading
Loading