Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: send only partials of updated config data (#7553)

Dylan Madisetti790df0a7b125
diff --git a/frontend/src/components/app-config/__tests__/get-dirty-values.test.ts b/frontend/src/components/app-config/__tests__/get-dirty-values.test.ts
new file mode 100644
index 0000000000..ff37f06c8d
--- /dev/null
+++ b/frontend/src/components/app-config/__tests__/get-dirty-values.test.ts
@@ -0,0 +1,74 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { describe, expect, test } from "vitest";
+import { getDirtyValues } from "../user-config-form";
+
+describe("getDirtyValues", () => {
+ test("extracts only dirty fields", () => {
+ const values = {
+ display: { theme: "dark", fontSize: 14 },
+ runtime: { dotenv: [".env.dev"], auto_reload: "lazy" },
+ };
+ const dirtyFields = {
+ display: { theme: true },
+ };
+
+ const result = getDirtyValues(values, dirtyFields);
+
+ expect(result).toEqual({ display: { theme: "dark" } });
+ expect(result).not.toHaveProperty("runtime");
+ });
+
+ test("returns empty object when nothing is dirty", () => {
+ const values = { display: { theme: "dark" } };
+ const dirtyFields = {};
+
+ const result = getDirtyValues(values, dirtyFields);
+
+ expect(result).toEqual({});
+ });
+
+ test("handles nested dirty fields", () => {
+ const values = {
+ runtime: { auto_reload: "lazy", dotenv: [".env"] },
+ display: { theme: "dark" },
+ };
+ const dirtyFields = {
+ runtime: { auto_reload: true },
+ };
+
+ const result = getDirtyValues(values, dirtyFields);
+
+ expect(result).toEqual({ runtime: { auto_reload: "lazy" } });
+ expect(result.runtime).not.toHaveProperty("dotenv");
+ });
+
+ test("handles multiple dirty fields at same level", () => {
+ const values = {
+ display: { theme: "dark", fontSize: 16, width: "full" },
+ };
+ const dirtyFields = {
+ display: { theme: true, fontSize: true },
+ };
+
+ const result = getDirtyValues(values, dirtyFields);
+
+ expect(result).toEqual({ display: { theme: "dark", fontSize: 16 } });
+ expect(result.display).not.toHaveProperty("width");
+ });
+
+ test("preserves unmodified runtime settings like dotenv", () => {
+ const values = {
+ display: { theme: "dark" },
+ runtime: { dotenv: [".env.local"], pythonpath: ["/custom/path"] },
+ };
+ const dirtyFields = {
+ display: { theme: true },
+ };
+
+ const result = getDirtyValues(values, dirtyFields);
+
+ expect(result).toEqual({ display: { theme: "dark" } });
+ expect(result).not.toHaveProperty("runtime");
+ });
+});
diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsx
index 04a7801d47..ea734f879d 100644
--- a/frontend/src/components/app-config/user-config-form.tsx
+++ b/frontend/src/components/app-config/user-config-form.tsx
@@ -14,6 +14,7 @@ import {
} from "lucide-react";
import React, { useId, useRef } from "react";
import { useLocale } from "react-aria";
+import type { FieldValues } from "react-hook-form";
import { useForm } from "react-hook-form";
import type z from "zod";
import { Button } from "@/components/ui/button";
@@ -59,6 +60,33 @@ import { DataForm } from "./data-form";
import { IsOverridden } from "./is-overridden";
import { OptionalFeatures } from "./optional-features";
+/**
+ * Extract only the values that have been modified (dirty) from form state.
+ * This prevents sending unchanged fields that could overwrite backend values.
+ */
+export function getDirtyValues<T extends FieldValues>(
+ values: T,
+ dirtyFields: Partial<Record<keyof T, unknown>>,
+): Partial<T> {
+ const result: Partial<T> = {};
+ for (const key of Object.keys(dirtyFields) as Array<keyof T>) {
+ const dirty = dirtyFields[key];
+ if (dirty === true) {
+ result[key] = values[key];
+ } else if (typeof dirty === "object" && dirty !== null) {
+ // Nested object - recurse
+ const nested = getDirtyValues(
+ values[key] as FieldValues,
+ dirty as Partial<Record<string, unknown>>,
+ );
+ if (Object.keys(nested).length > 0) {
+ result[key] = nested as T[keyof T];
+ }
+ }
+ }
+ return result;
+}
+
const categories = [
{
id: "editor",
@@ -146,8 +174,15 @@ export const UserConfigForm: React.FC = () => {
});
const onSubmitNotDebounced = async (values: UserConfig) => {
- await saveUserConfig({ config: values }).then(() => {
- setConfig(values);
+ // Only send values that were actually changed to avoid
+ // overwriting backend values the form doesn't manage
+ const dirtyValues = getDirtyValues(values, form.formState.dirtyFields);
+ if (Object.keys(dirtyValues).length === 0) {
+ return; // Nothing changed
+ }
+ await saveUserConfig({ config: dirtyValues }).then(() => {
+ // Update local state with form values
+ setConfig({ ...config, ...values });
});
};
const onSubmit = useDebouncedCallback(onSubmitNotDebounced, FORM_DEBOUNCE);
diff --git a/frontend/src/components/editor/chrome/wrapper/footer-items/runtime-settings.tsx b/frontend/src/components/editor/chrome/wrapper/footer-items/runtime-settings.tsx
index 58c48c9c56..bdd9f591bd 100644
--- a/frontend/src/components/editor/chrome/wrapper/footer-items/runtime-settings.tsx
+++ b/frontend/src/components/editor/chrome/wrapper/footer-items/runtime-settings.tsx
@@ -19,7 +19,6 @@ import {
} from "@/components/ui/dropdown-menu";
import { Switch } from "@/components/ui/switch";
import { useResolvedMarimoConfig, useUserConfig } from "@/core/config/config";
-import type { UserConfig } from "@/core/config/config-schema";
import { useRequestClient } from "@/core/network/requests";
import { isWasm } from "@/core/wasm/utils";
import { cn } from "@/utils/cn";
@@ -53,43 +52,44 @@ export const RuntimeSettings: React.FC<RuntimeSettingsProps> = ({
}, []);
const handleStartupToggle = async (checked: boolean) => {
- const newConfig = {
- ...config,
- runtime: {
- ...config.runtime,
- auto_instantiate: checked,
- },
- };
- await saveUserConfig({ config: newConfig }).then(() =>
- setUserConfig(newConfig),
+ // Send only the changed portion to avoid overwriting other config values
+ await saveUserConfig({
+ config: { runtime: { auto_instantiate: checked } },
+ }).then(() =>
+ // Update local state with merged config
+ setUserConfig({
+ ...userConfig,
+ runtime: { ...userConfig.runtime, auto_instantiate: checked },
+ }),
);
};
const handleCellChangeToggle = async (checked: boolean) => {
- const newUserConfig: UserConfig = {
- ...userConfig,
- runtime: {
- ...userConfig.runtime,
- on_cell_change: checked ? "autorun" : "lazy",
- },
- };
- await saveUserConfig({ config: newUserConfig }).then(() =>
- setUserConfig(newUserConfig),
+ const onCellChange = checked ? "autorun" : "lazy";
+ // Send only the changed portion to avoid overwriting other config values
+ await saveUserConfig({
+ config: { runtime: { on_cell_change: onCellChange } },
+ }).then(() =>
+ // Update local state with merged config
+ setUserConfig({
+ ...userConfig,
+ runtime: { ...userConfig.runtime, on_cell_change: onCellChange },
+ }),
);
};
const handleModuleReloadChange = async (
option: "off" | "lazy" | "autorun",
) => {
- const newUserConfig: UserConfig = {
- ...userConfig,
- runtime: {
- ...userConfig.runtime,
- auto_reload: option,
- },
- };
- await saveUserConfig({ config: newUserConfig }).then(() =>
- setUserConfig(newUserConfig),
+ // Send only the changed portion to avoid overwriting other config values
+ await saveUserConfig({
+ config: { runtime: { auto_reload: option } },
+ }).then(() =>
+ // Update local state with merged config
+ setUserConfig({
+ ...userConfig,
+ runtime: { ...userConfig.runtime, auto_reload: option },
+ }),
);
};
diff --git a/frontend/src/core/codemirror/compat/__tests__/jupyter.test.ts b/frontend/src/core/codemirror/compat/__tests__/jupyter.test.ts
index afc5b93d55..482996daec 100644
--- a/frontend/src/core/codemirror/compat/__tests__/jupyter.test.ts
+++ b/frontend/src/core/codemirror/compat/__tests__/jupyter.test.ts
@@ -91,10 +91,9 @@ describe("jupyterHelpExtension", () => {
selection: { anchor: "%autoreload 2".length },
});
+ // Verify only the partial config is sent, not the full config
expect(mockRequestClient.saveUserConfig).toHaveBeenCalledWith({
- config: expect.objectContaining({
- runtime: { auto_reload: "autorun" },
- }),
+ config: { runtime: { auto_reload: "autorun" } },
});
// Test lazy mode
@@ -103,10 +102,9 @@ describe("jupyterHelpExtension", () => {
selection: { anchor: "%autoreload 1".length },
});
+ // Verify only the partial config is sent
expect(mockRequestClient.saveUserConfig).toHaveBeenCalledWith({
- config: expect.objectContaining({
- runtime: { auto_reload: "lazy" },
- }),
+ config: { runtime: { auto_reload: "lazy" } },
});
// Test off mode
@@ -115,10 +113,9 @@ describe("jupyterHelpExtension", () => {
selection: { anchor: "%autoreload 0".length },
});
+ // Verify only the partial config is sent
expect(mockRequestClient.saveUserConfig).toHaveBeenCalledWith({
- config: expect.objectContaining({
- runtime: { auto_reload: "off" },
- }),
+ config: { runtime: { auto_reload: "off" } },
});
});
diff --git a/frontend/src/core/codemirror/compat/jupyter.tsx b/frontend/src/core/codemirror/compat/jupyter.tsx
index 6c398d7aeb..8f8695e8be 100644
--- a/frontend/src/core/codemirror/compat/jupyter.tsx
+++ b/frontend/src/core/codemirror/compat/jupyter.tsx
@@ -5,7 +5,6 @@ import { focusPackagesInput } from "@/components/editor/chrome/panels/packages-u
import { chromeAtom } from "@/components/editor/chrome/state";
import { Kbd } from "@/components/ui/kbd";
import { userConfigAtom } from "@/core/config/config";
-import type { UserConfig } from "@/core/config/config-schema";
import { getRequestClient } from "@/core/network/requests";
import { store } from "@/core/state/jotai";
import { toast } from "../../../components/ui/use-toast";
@@ -48,15 +47,14 @@ export function jupyterHelpExtension(): Extension {
const handleUpdateModuleReload = async (mode: "lazy" | "autorun" | "off") => {
const config = store.get(userConfigAtom);
const { saveUserConfig } = getRequestClient();
- const newConfig: UserConfig = {
- ...config,
- runtime: {
- ...config.runtime,
- auto_reload: mode,
- },
- };
- await saveUserConfig({ config: newConfig }).then(() =>
- store.set(userConfigAtom, newConfig),
+ // Send only the changed portion to avoid overwriting other config values
+ await saveUserConfig({ config: { runtime: { auto_reload: mode } } }).then(
+ () =>
+ // Update local state with merged config
+ store.set(userConfigAtom, {
+ ...config,
+ runtime: { ...config.runtime, auto_reload: mode },
+ }),
);
};
diff --git a/frontend/src/core/config/feature-flag.tsx b/frontend/src/core/config/feature-flag.tsx
index 5c80f27fcc..5ee5ba1204 100644
--- a/frontend/src/core/config/feature-flag.tsx
+++ b/frontend/src/core/config/feature-flag.tsx
@@ -37,10 +37,10 @@ export function getFeatureFlag<T extends keyof ExperimentalFeatures>(
}
function setFeatureFlag(feature: keyof ExperimentalFeatures, value: boolean) {
- const userConfig = getResolvedMarimoConfig();
- userConfig.experimental = userConfig.experimental ?? {};
- userConfig.experimental[feature] = value;
- getRequestClient().saveUserConfig({ config: userConfig });
+ // Send only the changed portion to avoid overwriting other config values
+ getRequestClient().saveUserConfig({
+ config: { experimental: { [feature]: value } },
+ });
}
export const FeatureFlagged: React.FC<{