The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 14:45 UTC
fix(web): use optimistic updates for settings across environments (#28945)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
oliver committed
Nov 14, 2026 at 07:43 UTC
ee4ef06469c8e7551aa3c97b554991c9ab996a8f
4 files changed
+169
-15
apps/web/src/components/settings/SettingsScopeContext.tsx
+12
-2
@@ -7,7 +7,12 @@ import { createContext, type ReactNode, useContext } from "react";
7
import { useEnvironments, usePrimaryEnvironmentId } from "../../state/environments";
8
import { getProjectFileQueryAtom, optimisticFileAtom } from "../files/projectFilesQueryState";
9
import { useSettingsProjectGroups } from "./useSettingsProjectGroups";
10
-import { resolveScopedSettingsTargets, selectScopedSettingsEnvironments } from "./scopedSettings";
10
+import {
11
+ applyPendingScopedWrites,
12
+ pendingScopedWritesAtom,
13
+ resolveScopedSettingsTargets,
14
+ selectScopedSettingsEnvironments,
15
+} from "./scopedSettings";
16
import { resolveSettingsScope, type SettingsScopeSearch } from "./settingsScope";
17
import { selectSingleEnvironmentScope } from "./settingsScopeAxis";
18
@@ -69,12 +74,17 @@ function useResolvedSettingsScope(rawSearch: SettingsScopeSearch, singleEnvironm
74
: rawSearch;
75
const scope = resolveSettingsScope(search, groups, availableEnvironments);
76
const projectFiles = useMemberProjectFiles(scope);
77
+ const pendingWrites = useAtomValue(pendingScopedWritesAtom);
78
const selected = selectScopedSettingsEnvironments(
79
scope,
80
availableEnvironments,
81
primaryEnvironmentId,
82
);
77
- const targets = resolveScopedSettingsTargets(scope, selected.connectedEnvironments, projectFiles);
83
+ const targets = resolveScopedSettingsTargets(
84
+ scope,
85
+ applyPendingScopedWrites(selected.connectedEnvironments, pendingWrites),
86
+ projectFiles,
87
+ );
88
const target =
89
targets.find((candidate) => candidate.environmentId === selected.environment?.environmentId) ??
90
targets[0] ??
apps/web/src/components/settings/scopedSettings.test.ts
+56
@@ -14,6 +14,7 @@ import { resolveWorktreeCleanup } from "@t3tools/shared/projectSettings";
14
15
import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
16
import {
17
+ applyPendingScopedWrites,
18
createScopedSettingsWriter,
19
persistScopedSettingsPatch,
20
planProjectOverridesClear,
@@ -24,6 +25,8 @@ import {
25
scopedSettingsAreMixed,
26
scopedSettingsSource,
27
selectScopedSettingsEnvironments,
28
+ settlePendingScopedWrites,
29
+ type PendingScopedWrite,
30
} from "./scopedSettings";
31
import { resolveSettingsScope } from "./settingsScope";
32
@@ -207,6 +210,20 @@ describe("scoped settings writes", () => {
210
const legacy = { settings: { ...DEFAULT_SERVER_SETTINGS, compactIdleThreads: false } };
211
expect(scopedIdleCompaction([fixed, modes])).toEqual({ mixed: false, neverAvailable: true });
212
expect(scopedIdleCompaction([fixed, legacy])).toEqual({ mixed: false, neverAvailable: false });
213
+ const legacyEnvironment = environment("Legacy", { settings: legacy.settings });
214
+ const projected = applyPendingScopedWrites(
215
+ [legacyEnvironment],
216
+ [
217
+ {
218
+ save: {},
219
+ environmentId: legacyEnvironment.environmentId,
220
+ patch: { defaultRuntimeMode: "auto" },
221
+ },
222
+ ],
223
+ );
224
+ expect(scopedIdleCompaction(resolveScopedSettingsTargets(all, projected)).neverAvailable).toBe(
225
+ false,
226
+ );
227
expect(
228
scopedIdleCompaction([fixed, { settings: { ...fixed.settings, idleCompaction: "off" } }])
229
.mixed,
@@ -704,6 +721,45 @@ describe("scoped settings mixed values", () => {
721
const targets = resolveScopedSettingsTargets(all, [first, second]);
722
expect(scopedSettingsAreMixed(targets, ["sourceControlWritingStyle"])).toBe(false);
723
});
724
+
725
+ it("agrees while a save is in flight and follows the environment once it reports back", () => {
726
+ const patch = { defaultRuntimeMode: "auto" as const };
727
+ const saved = environment("Laptop", { settings: patch });
728
+ let waiting = environment("Server");
729
+ const save = {};
730
+ let writes: readonly PendingScopedWrite[] = [saved, waiting].map(({ environmentId }) => ({
731
+ save,
732
+ environmentId,
733
+ patch,
734
+ }));
735
+ const mixed = () =>
736
+ scopedSettingsAreMixed(
737
+ resolveScopedSettingsTargets(all, applyPendingScopedWrites([saved, waiting], writes)),
738
+ ["defaultRuntimeMode"],
739
+ );
740
+ const settle = (environmentId: EnvironmentId, result?: ServerSettings) => {
741
+ writes = settlePendingScopedWrites(
742
+ writes,
743
+ { save, environmentId },
744
+ (id) => [saved, waiting].find((env) => env.environmentId === id)?.serverConfig?.settings,
745
+ result,
746
+ );
747
+ };
748
+ expect(mixed()).toBe(false);
749
+
750
+ // Laptop already pushed its result; Server answers before its push arrives.
751
+ settle(saved.environmentId, saved.serverConfig!.settings);
752
+ settle(waiting.environmentId, applyServerSettingsPatch(waiting.serverConfig!.settings, patch));
753
+ expect(writes.map(({ environmentId }) => environmentId)).toEqual([waiting.environmentId]);
754
+ expect(mixed()).toBe(false);
755
+
756
+ waiting = environment("Server", { settings: { defaultRuntimeMode: "approval-required" } });
757
+ expect(mixed()).toBe(true);
758
+
759
+ writes = [{ save, environmentId: waiting.environmentId, patch }];
760
+ settle(waiting.environmentId);
761
+ expect(writes).toEqual([]);
762
+ });
763
});
764
765
describe("project overrides at environment scope", () => {
apps/web/src/components/settings/scopedSettings.ts
+65
@@ -21,8 +21,10 @@ import {
21
resolveWorktreeCleanup,
22
type ProjectSettingSource,
23
} from "@t3tools/shared/projectSettings";
24
+import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
25
import * as Equal from "effect/Equal";
26
import * as Cause from "effect/Cause";
27
+import { Atom } from "effect/reactivity";
28
29
import type { ResolvedSettingsScope } from "./settingsScope";
30
@@ -55,6 +57,69 @@ export function isConnectedWithConfig(
57
return environment.connection.phase === "connected" && environment.serverConfig !== null;
58
}
59
60
+/**
61
+ * A server save shown before its environment pushes the new settings back. It applies while in
62
+ * flight, and after success only until that environment's settings change from `settledOver`.
63
+ */
64
+export interface PendingScopedWrite {
65
+ readonly save: object;
66
+ readonly environmentId: EnvironmentId;
67
+ readonly patch: ServerSettingsPatch;
68
+ readonly settledOver?: ServerSettings;
69
+}
70
+
71
+export const pendingScopedWritesAtom = Atom.make<readonly PendingScopedWrite[]>([]).pipe(
72
+ Atom.keepAlive,
73
+ Atom.withLabel("web-settings:pending-scoped-writes"),
74
+);
75
+
76
+const pendingWriteApplies = (write: PendingScopedWrite, settings: ServerSettings | undefined) =>
77
+ settings !== undefined && (write.settledOver === undefined || write.settledOver === settings);
78
+
79
+/** Settles one environment's write: a failure or an already-pushed result drops it. */
80
+export function settlePendingScopedWrites(
81
+ writes: readonly PendingScopedWrite[],
82
+ target: Pick<PendingScopedWrite, "save" | "environmentId">,
83
+ readSettings: (environmentId: EnvironmentId) => ServerSettings | undefined,
84
+ saved?: ServerSettings,
85
+): readonly PendingScopedWrite[] {
86
+ return writes.flatMap((write) => {
87
+ const current = readSettings(write.environmentId);
88
+ if (write.save !== target.save || write.environmentId !== target.environmentId) {
89
+ return pendingWriteApplies(write, current) ? [write] : [];
90
+ }
91
+ if (saved === undefined || current === undefined || Equal.equals(current, saved)) return [];
92
+ return [{ ...write, settledOver: current }];
93
+ });
94
+}
95
+
96
+export function applyPendingScopedWrites<T extends ScopedSettingsEnvironment>(
97
+ environments: readonly T[],
98
+ writes: readonly PendingScopedWrite[],
99
+): readonly T[] {
100
+ if (writes.length === 0) return environments;
101
+ return environments.map((environment) => {
102
+ const config = environment.serverConfig;
103
+ if (config === null) return environment;
104
+ const settings = writes.reduce(
105
+ (current, write) =>
106
+ write.environmentId === environment.environmentId &&
107
+ pendingWriteApplies(write, config.settings)
108
+ ? applyServerSettingsPatch(current, write.patch)
109
+ : current,
110
+ config.settings,
111
+ );
112
+ if (settings === config.settings) return environment;
113
+ // Legacy servers read `compactIdleThreads`, so project the mode back onto it.
114
+ const { idleCompaction: _mode, ...legacy } = settings;
115
+ const projected =
116
+ config.settings.idleCompaction === undefined
117
+ ? { ...legacy, ...idleCompactionPatch(config.settings, globalIdleCompaction(settings)) }
118
+ : settings;
119
+ return { ...environment, serverConfig: { ...config, settings: projected } };
120
+ });
121
+}
122
+
123
/** The representative supplies display values, never the set of write targets. */
124
export function selectScopedSettingsEnvironments<T extends ScopedSettingsEnvironment>(
125
scope: ResolvedSettingsScope,
apps/web/src/components/settings/useScopedSettings.ts
+36
-13
@@ -9,12 +9,14 @@ import * as Cause from "effect/Cause";
9
import { AsyncResult } from "effect/reactivity";
10
import {
11
DEFAULT_SERVER_SETTINGS,
12
+ type EnvironmentId,
13
type ProjectScopedServerSettingKey,
14
type ServerSettings,
15
type UnifiedSettings,
16
} from "@t3tools/contracts";
17
18
import { persistClientSettingsPatch, useMergedSettings } from "../../hooks/useSettings";
19
+import { appAtomRegistry } from "../../rpc/atomRegistry";
20
import { readEnvironment } from "../../state/environments";
21
import { serverEnvironment } from "../../state/server";
22
import { useAtomCommand } from "../../state/use-atom-command";
@@ -22,18 +24,20 @@ import { toastManager } from "../ui/toast";
24
import { useOptionalSettingsScope, useSettingsScope } from "./SettingsScopeContext";
25
import {
26
createScopedSettingsWriter,
27
+ pendingScopedWritesAtom,
28
planProjectOverridesClear,
29
planScopedSettingsClear,
30
planScopedSettingsPatch,
31
scopedSettingsAreMixed,
32
scopedSettingsSource,
33
+ settlePendingScopedWrites,
34
type ProjectOverrideEntry,
35
type ScopedSettingsPatch,
36
} from "./scopedSettings";
37
34
-const writeScopedSettings = createScopedSettingsWriter(
35
- (environmentId) => readEnvironment(environmentId)?.serverConfig?.settings ?? null,
36
-);
38
+const readSettings = (environmentId: EnvironmentId) =>
39
+ readEnvironment(environmentId)?.serverConfig?.settings ?? null;
40
+const writeScopedSettings = createScopedSettingsWriter(readSettings);
41
42
export function useScopedSettings<T = UnifiedSettings>(
43
selector?: (settings: UnifiedSettings) => T,
@@ -69,6 +73,20 @@ function useRunScopedPlan() {
73
onFailed?.();
74
return;
75
}
76
+ const save = {};
77
+ const settle = (environmentId: EnvironmentId, saved?: ServerSettings) =>
78
+ appAtomRegistry.update(pendingScopedWritesAtom, (writes) =>
79
+ settlePendingScopedWrites(
80
+ writes,
81
+ { save, environmentId },
82
+ (id) => readSettings(id) ?? undefined,
83
+ saved,
84
+ ),
85
+ );
86
+ appAtomRegistry.update(pendingScopedWritesAtom, (writes) => [
87
+ ...writes,
88
+ ...plan.serverWrites.map(({ environmentId, patch }) => ({ save, environmentId, patch })),
89
+ ]);
90
void writeScopedSettings(
91
environments,
92
planner,
@@ -76,16 +94,21 @@ function useRunScopedPlan() {
94
const missing = requiredScopesForServerSettingsPatch(request.input.patch).find(
95
(scope) => !readEnvironmentScope(request.environmentId, scope),
96
);
79
- if (missing)
80
- return AsyncResult.failure(
81
- Cause.fail(
82
- new EnvironmentAuthorizationError({
83
- requiredScope: missing,
84
- message: "This connection lacks permission to change these settings.",
85
- }),
86
- ),
87
- );
88
- return persistServer(request);
97
+ const result = missing
98
+ ? AsyncResult.failure(
99
+ Cause.fail(
100
+ new EnvironmentAuthorizationError({
101
+ requiredScope: missing,
102
+ message: "This connection lacks permission to change these settings.",
103
+ }),
104
+ ),
105
+ )
106
+ : await persistServer(request).catch((error: unknown) => {
107
+ settle(request.environmentId);
108
+ throw error;
109
+ });
110
+ settle(request.environmentId, result._tag === "Success" ? result.value : undefined);
111
+ return result;
112
},
113
persistClientSettingsPatch,
114
).then(({ failedEnvironments, savedEnvironments, savedEnvironmentCount }) => {