Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
63 changes: 63 additions & 0 deletions packages/cli/src/config/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,69 @@ describe('Settings Loading and Merging', () => {
});
});

it('should preserve legacy auto memory behavior when only memoryManager is explicitly enabled', () => {
(mockFsExistsSync as Mock).mockImplementation((p: fs.PathLike) => {
const normP = path.normalize(p.toString());
return (
normP === path.normalize(USER_SETTINGS_PATH) ||
normP === path.normalize(MOCK_WORKSPACE_SETTINGS_PATH)
);
});

const userSettingsContent = {
experimental: {
memoryManager: true,
},
};

(fs.readFileSync as Mock).mockImplementation(
(p: fs.PathOrFileDescriptor) => {
const normP = path.normalize(p.toString());
if (normP === path.normalize(USER_SETTINGS_PATH)) {
return JSON.stringify(userSettingsContent);
}
return '{}';
},
);

const settings = loadSettings(MOCK_WORKSPACE_DIR);

expect(settings.merged.experimental?.memoryManager).toBe(true);
expect(settings.merged.experimental?.autoMemory).toBe(true);
});

it('should let explicit autoMemory false override legacy memoryManager compatibility', () => {
(mockFsExistsSync as Mock).mockImplementation((p: fs.PathLike) => {
const normP = path.normalize(p.toString());
return (
normP === path.normalize(USER_SETTINGS_PATH) ||
normP === path.normalize(MOCK_WORKSPACE_SETTINGS_PATH)
);
});

const userSettingsContent = {
experimental: {
memoryManager: true,
autoMemory: false,
},
};

(fs.readFileSync as Mock).mockImplementation(
(p: fs.PathOrFileDescriptor) => {
const normP = path.normalize(p.toString());
if (normP === path.normalize(USER_SETTINGS_PATH)) {
return JSON.stringify(userSettingsContent);
}
return '{}';
},
);

const settings = loadSettings(MOCK_WORKSPACE_DIR);

expect(settings.merged.experimental?.memoryManager).toBe(true);
expect(settings.merged.experimental?.autoMemory).toBe(false);
});

it('should merge all settings files with the correct precedence', () => {
// Mock schema to test defaults application
const mockSchema = {
Expand Down
40 changes: 39 additions & 1 deletion packages/cli/src/config/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,28 @@ export function getDefaultsFromSchema(
return defaults as Settings;
}

function hasOwnNestedProperty(
obj: Settings | undefined,
path: string[],
): boolean {
let current: unknown = obj;

for (const key of path) {
if (typeof current !== 'object' || current === null) {
return false;
}

const record = current as Record<string, unknown>;
if (!Object.hasOwn(record, key)) {
return false;
}

current = record[key];
}

return true;
}

export function mergeSettings(
system: Settings,
systemDefaults: Settings,
Expand All @@ -262,14 +284,30 @@ export function mergeSettings(
// 4. Workspace Settings
// 5. System Settings (as overrides)
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
return customDeepMerge(
const merged = customDeepMerge(
getMergeStrategyForPath,
schemaDefaults,
systemDefaults,
user,
safeWorkspace,
system,
) as MergedSettings;

const hasExplicitAutoMemory =
hasOwnNestedProperty(system, ['experimental', 'autoMemory']) ||
hasOwnNestedProperty(systemDefaults, ['experimental', 'autoMemory']) ||
hasOwnNestedProperty(user, ['experimental', 'autoMemory']) ||
hasOwnNestedProperty(safeWorkspace, ['experimental', 'autoMemory']);

if (
!hasExplicitAutoMemory &&
merged.experimental?.memoryManager &&
merged.experimental
) {
merged.experimental.autoMemory = true;
}

return merged;
}

/**
Expand Down