Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(settings): save only edited keys when hiding the raw JSON editor (#8212)

dodoshell02b05e7a22d3
diff --git a/changelog.d/+raw-config-hide-stale-save.bugfix.md b/changelog.d/+raw-config-hide-stale-save.bugfix.md
new file mode 100644
index 0000000000..b4e22211d1
--- /dev/null
+++ b/changelog.d/+raw-config-hide-stale-save.bugfix.md
@@ -0,0 +1 @@
+Hiding the Settings page's JSON editor no longer resends unedited values, so it no longer undoes a field change whose save was still in progress. Fixes #8157.
diff --git a/src/local_deep_research/web/static/js/components/settings.js b/src/local_deep_research/web/static/js/components/settings.js
index b3964f307e..fb4be91f10 100644
--- a/src/local_deep_research/web/static/js/components/settings.js
+++ b/src/local_deep_research/web/static/js/components/settings.js
@@ -202,6 +202,8 @@
let rawConfigToggle;
let rawConfigSection;
let rawConfigEditor;
+ // Values the raw editor text was built with, by full setting key
+ const rawConfigBaseline = new Map();
const originalSettings = {};
let allSettings = [];
let activeTab = 'all';
@@ -4238,26 +4240,33 @@
if (rawConfigSection && rawConfigEditor) {
const isVisible = rawConfigSection.style.display !== 'none';
- // If hiding the editor, try to apply changes
- if (isVisible) {
+ // The editor text can be older than a field save still in flight,
+ // so resend only edited keys.
+ if (isVisible && rawConfigEditor.getAttribute('data-modified') === 'true') {
try {
// Parse the JSON to validate it
const rawConfig = JSON.parse(rawConfigEditor.value);
// Process and flatten the JSON
- const flattenedConfig = {};
+ const changedConfig = {};
Object.keys(rawConfig).forEach(namespace => {
const section = rawConfig[namespace];
Object.keys(section).forEach(key => {
const fullKey = `${namespace}.${key}`;
- flattenedConfig[fullKey] = section[key];
+ if (isRawConfigValueChanged(fullKey, section[key])) {
+ changedConfig[fullKey] = section[key];
+ }
});
});
- // Save the changes to apply them to UI
- submitSettingsData(flattenedConfig, null);
+ if (Object.keys(changedConfig).length > 0) {
+ // Save the changes to apply them to UI
+ submitSettingsData(changedConfig, null);
+ } else {
+ rawConfigEditor.removeAttribute('data-modified');
+ }
} catch (e) {
// Show error and prevent hiding the editor
showAlert('Invalid JSON in editor: ' + e.message, 'error');
@@ -4285,6 +4294,17 @@
}
}
+ /**
+ * Whether a raw editor value differs from the one the editor was built with
+ * @param {string} fullKey - The full setting key
+ * @param {*} value - The value parsed from the editor
+ * @returns {boolean}
+ */
+ function isRawConfigValueChanged(fullKey, value) {
+ return !rawConfigBaseline.has(fullKey) ||
+ JSON.stringify(rawConfigBaseline.get(fullKey)) !== JSON.stringify(value);
+ }
+
/**
* Prepare the raw JSON editor with all settings
*/
@@ -4341,6 +4361,7 @@
// unrelated field auto-save refresh its own value without
// destroying the raw editor's unsaved work.
let preservedDirtyValue = false;
+ const preservedDirtyKeys = new Set();
Object.keys(existingConfig).forEach(prefix => {
if (!settingsObj[prefix]) {
settingsObj[prefix] = {};
@@ -4357,12 +4378,28 @@
) {
settingsObj[prefix][key] = existingConfig[prefix][key];
if (preserveDirtyValues && !updatedKeySet.has(fullKey)) {
- preservedDirtyValue = true;
+ preservedDirtyKeys.add(fullKey);
+ if (isRawConfigValueChanged(fullKey, existingConfig[prefix][key])) {
+ preservedDirtyValue = true;
+ }
}
}
});
});
+ // A preserved dirty value keeps the baseline it was edited from
+ if (!preserveDirtyValues) {
+ rawConfigBaseline.clear();
+ }
+ Object.keys(settingsObj).forEach(prefix => {
+ Object.keys(settingsObj[prefix]).forEach(key => {
+ const fullKey = `${prefix}.${key}`;
+ if (!preservedDirtyKeys.has(fullKey)) {
+ rawConfigBaseline.set(fullKey, settingsObj[prefix][key]);
+ }
+ });
+ });
+
// Format as pretty JSON
rawConfigEditor.value = JSON.stringify(settingsObj, null, 2);
if (!preservedDirtyValue) {
diff --git a/tests/js/components/settings-submit-runtime.test.js b/tests/js/components/settings-submit-runtime.test.js
index 05b080fffe..d4a774c6ee 100644
--- a/tests/js/components/settings-submit-runtime.test.js
+++ b/tests/js/components/settings-submit-runtime.test.js
@@ -68,6 +68,12 @@ function submitSettingsForm() {
));
}
+function editRawConfig(config) {
+ const rawEditor = document.getElementById('raw_config_editor');
+ rawEditor.value = JSON.stringify(config);
+ rawEditor.dispatchEvent(new Event('input', { bubbles: true }));
+}
+
beforeAll(async () => {
document.head.insertAdjacentHTML(
'beforeend',
@@ -276,7 +282,7 @@ it('does not block an unrelated setting behind an in-flight write', async () =>
.mockImplementationOnce(() => reportSave.promise);
vi.stubGlobal('fetch', fetchMock);
- document.getElementById('raw_config_editor').value = JSON.stringify({
+ editRawConfig({
app: { theme: 'parallel-theme' },
});
document.getElementById('toggle-raw-config').click();
@@ -385,6 +391,89 @@ it('preserves a dirty known raw key while an unrelated save settles', async () =
expect(window.ui.showMessage).toHaveBeenCalledOnce();
});
+const rawEditorSettings = {
+ 'app.theme': { value: 'server-theme', name: 'Theme' },
+ 'report.options': { value: { citations: true }, name: 'Report options' },
+ 'app.enabled': { value: false, name: 'Enabled' },
+ 'search.options': { value: {}, name: 'Search options' },
+ 'app.extra': { value: 7, name: 'Extra' },
+ 'llm.provider': { value: 'openai', name: 'Provider' },
+};
+
+it('saves nothing when the raw editor is hidden without an edit (#8157)', async () => {
+ const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
+ status: 'success',
+ settings: rawEditorSettings,
+ }));
+ vi.stubGlobal('fetch', fetchMock);
+
+ // Build the raw editor from a real settings response.
+ submitSettingsForm();
+ await flushPromises();
+ expect(fetchMock).toHaveBeenCalledOnce();
+
+ document.getElementById('toggle-raw-config').click();
+ await flushPromises();
+
+ expect(document.getElementById('raw-config').style.display).toBe('none');
+ expect(fetchMock).toHaveBeenCalledOnce();
+});
+
+it('sends only the edited raw key while a field save is pending (#8157)', async () => {
+ const reportSave = deferred();
+ const rawSave = deferred();
+ const fetchMock = vi.fn()
+ .mockResolvedValueOnce(jsonResponse({
+ status: 'success',
+ settings: rawEditorSettings,
+ }))
+ .mockImplementationOnce(() => reportSave.promise)
+ .mockImplementationOnce(() => rawSave.promise);
+ vi.stubGlobal('fetch', fetchMock);
+
+ submitSettingsForm();
+ await flushPromises();
+ document.getElementById('raw-config').style.display = 'none';
+ document.getElementById('app-theme').removeAttribute('name');
+ document.getElementById('app-enabled').removeAttribute('name');
+ document.getElementById('search-options_original').removeAttribute('name');
+ document.getElementById('report-options').value = '{"citations":false}';
+ submitSettingsForm();
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+
+ // The editor opens while the report save is pending, so its text still
+ // holds the old report.options value.
+ document.getElementById('toggle-raw-config').click();
+ const rawEditor = document.getElementById('raw_config_editor');
+ const config = JSON.parse(rawEditor.value);
+ expect(config.report.options).toEqual({ citations: true });
+ config.app.theme = 'raw-theme';
+ editRawConfig(config);
+ document.getElementById('toggle-raw-config').click();
+
+ expect(fetchMock).toHaveBeenCalledTimes(3);
+ expect(JSON.parse(fetchMock.mock.calls[2][1].body)).toEqual({
+ 'app.theme': 'raw-theme',
+ });
+
+ reportSave.resolve(jsonResponse({
+ status: 'success',
+ settings: {
+ 'report.options': { value: { citations: false }, name: 'Report options' },
+ },
+ }));
+ rawSave.resolve(jsonResponse({
+ status: 'success',
+ settings: {
+ 'app.theme': { value: 'raw-theme', name: 'Theme' },
+ },
+ }));
+ await flushPromises();
+
+ expect(fetchMock).toHaveBeenCalledTimes(3);
+ expect(rawEditor.hasAttribute('data-modified')).toBe(false);
+});
+
it('keeps a full-form save authoritative over an older raw-editor save', async () => {
const olderRawSave = deferred();
const newerFormSave = deferred();
@@ -393,7 +482,7 @@ it('keeps a full-form save authoritative over an older raw-editor save', async (
.mockImplementationOnce(() => newerFormSave.promise);
vi.stubGlobal('fetch', fetchMock);
- document.getElementById('raw_config_editor').value = JSON.stringify({
+ editRawConfig({
app: { theme: 'raw-older' },
});
document.getElementById('toggle-raw-config').click();
@@ -439,7 +528,7 @@ it('retains the confirmed value when a later queued save fails', async () => {
.mockImplementationOnce(() => rejectedRequest.promise);
vi.stubGlobal('fetch', fetchMock);
- document.getElementById('raw_config_editor').value = JSON.stringify({
+ editRawConfig({
app: { theme: 'confirmed-theme' },
});
document.getElementById('toggle-raw-config').click();
@@ -477,7 +566,7 @@ it('merges keys still owned by an older bulk save without replacing a newer over
.mockImplementationOnce(() => newerSingleSave.promise);
vi.stubGlobal('fetch', fetchMock);
- document.getElementById('raw_config_editor').value = JSON.stringify({
+ editRawConfig({
app: {
theme: 'bulk-older',
extra: 'bulk-only',
@@ -485,7 +574,7 @@ it('merges keys still owned by an older bulk save without replacing a newer over
});
document.getElementById('toggle-raw-config').click();
document.getElementById('toggle-raw-config').click();
- document.getElementById('raw_config_editor').value = JSON.stringify({
+ editRawConfig({
app: { theme: 'single-newer' },
});
document.getElementById('toggle-raw-config').click();
@@ -731,7 +820,7 @@ it('clears a field error on a save path that never pre-cleared', async () => {
.toBe('Choose a supported theme');
expect(field.input.classList).toContain('ldr-settings-error');
- document.getElementById('raw_config_editor').value = JSON.stringify({
+ editRawConfig({
app: { theme: 'dark' },
});
document.getElementById('toggle-raw-config').click();