@devries-cameron20 / t3code-1 / commits / f7a23f0789
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): report rejected shared settings writes (#9356)

Co-authored-by: Claude Code <noreply@anthropic.com>

Julius Marminge committed Oct 19, 2026 at 11:07 UTC f7a23f07894df20973fdcf5ad00e286378b77ebe
4 files changed +285 -53
apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.test.tsx
+233 -21
@@ -1,5 +1,7 @@
1 // @vitest-environment jsdom
2 import { DEFAULT_SERVER_SETTINGS, type ServerSettingsPatch } from "@t3tools/contracts";
3 +import * as Cause from "effect/Cause";
4 +import { AsyncResult } from "effect/reactivity";
5 import type { ReactNode } from "react";
6 import { act } from "react";
7 import { createRoot, type Root } from "react-dom/client";
@@ -10,15 +12,45 @@ const { Children, mocks } = vi.hoisted(() => ({
12 mocks: {
13 branchNaming: { onChange: (_patch: ServerSettingsPatch) => {} },
14 updateSettings: vi.fn(),
15 + alert: vi.fn(),
16 + targets: [] as ReturnType<typeof environment>[],
17 + projectSelected: false,
18 },
19 }));
20
16 -vi.mock("react-native", () => ({ View: Children }));
21 +vi.mock("react-native", () => ({
22 + View: Children,
23 + Alert: { alert: mocks.alert },
24 + Pressable: ({
25 + children,
26 + disabled,
27 + onPress,
28 + accessibilityLabel,
29 + }: {
30 + children?: ReactNode;
31 + disabled?: boolean;
32 + onPress: () => void;
33 + accessibilityLabel?: string;
34 + }) => (
35 + <button disabled={disabled} onClick={onPress} aria-label={accessibilityLabel}>
36 + {children}
37 + </button>
38 + ),
39 +}));
40 +vi.mock("@effect/atom-react", () => ({
41 + useAtomValue: () => AsyncResult.success({}),
42 + useAtomSet: () => () => {},
43 +}));
44 +vi.mock("../../state/preferences", () => ({
45 + mobilePreferencesAtom: {},
46 + updateMobilePreferencesAtom: {},
47 +}));
48 +vi.mock("./components/AutoSettleDaysField", () => ({ AutoSettleDaysField: () => null }));
49 vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) }));
50 vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({}) }));
51 vi.mock("../../state/session", () => ({
52 readEnvironmentScope: () => true,
21 - useEnvironmentsWithScope: () => new Set(["local"]),
53 + useEnvironmentsWithScope: () => new Set(mocks.targets.map((target) => target.environmentId)),
54 }));
55 vi.mock("../../state/server", () => ({ serverEnvironment: { updateSettings: {} } }));
56 vi.mock("../../state/use-atom-command", () => ({
@@ -35,11 +67,29 @@ vi.mock("./components/SettingsEnvironmentFilterHeader", () => ({
67 vi.mock("./components/DatabaseStorageNotice", () => ({ DatabaseStorageNotice: () => null }));
68 vi.mock("./components/SettingsSection", () => ({ SettingsSection: Children }));
69 vi.mock("./components/SettingsChoiceRow", () => ({ SettingsChoiceRow: () => null }));
38 -vi.mock("./components/SettingsSwitchRow", () => ({ SettingsSwitchRow: () => null }));
39 -vi.mock("./components/SettingsRow", () => ({ SettingsRow: () => null }));
40 -vi.mock("./components/SettingsProjectOverridesSection", () => ({
41 - SettingsProjectOverridesSection: () => null,
70 +vi.mock("./components/SettingsSwitchRow", () => ({
71 + SettingsSwitchRow: ({
72 + label,
73 + value,
74 + disabled,
75 + onValueChange,
76 + }: {
77 + label: string;
78 + value: boolean | null;
79 + disabled?: boolean;
80 + onValueChange: (value: boolean) => void;
81 + }) => (
82 + <button
83 + role="switch"
84 + aria-checked={value ?? "mixed"}
85 + disabled={disabled}
86 + onClick={() => onValueChange(!value)}
87 + >
88 + {label}
89 + </button>
90 + ),
91 }));
92 +vi.mock("./components/SettingsRow", () => ({ SettingsRow: () => null }));
93 vi.mock("./components/BranchNamingSettings", () => ({
94 BranchNamingSettings: (props: { onChange: (patch: ServerSettingsPatch) => void }) => {
95 mocks.branchNaming.onChange = props.onChange;
@@ -48,28 +98,85 @@ vi.mock("./components/BranchNamingSettings", () => ({
98 }));
99 vi.mock("./settings-environment-filter", () => ({
100 useSettingsEnvironmentFilter: () => ({
51 - selectedTargets: [
101 + selectedTargets: mocks.targets,
102 + projectGroups: [
103 {
53 - environmentId: "local",
54 - serverConfig: {
55 - settings: DEFAULT_SERVER_SETTINGS,
56 - environment: { capabilities: { projectSettingsOverrides: true } },
57 - },
104 + key: "project",
105 + label: "Project",
106 + members: mocks.targets.map((target) => ({
107 + project: { environmentId: target.environmentId, id: "project" },
108 + })),
109 },
110 ],
60 - projectGroups: [],
61 - selectedProjectKey: null,
111 + selectedProjectKey: mocks.projectSelected ? "project" : null,
112 }),
113 }));
114
115 import { SettingsEnvironmentSourceControlRouteScreen } from "./SettingsServerControlsRouteScreen";
116 +import { SettingsThreadsRouteScreen } from "./SettingsThreadsRouteScreen";
117 +
118 +function environment(environmentId: string, label: string) {
119 + return {
120 + environmentId,
121 + label,
122 + connection: { phase: "connected" },
123 + serverConfig: {
124 + settings: {
125 + ...DEFAULT_SERVER_SETTINGS,
126 + defaultAutoPull: false,
127 + sidebarAutoSettleOnMerge: false,
128 + },
129 + environment: {
130 + capabilities: { projectSettingsOverrides: true, threadAutoSettlement: true },
131 + },
132 + },
133 + };
134 +}
135 +
136 +function button(label: string) {
137 + const result = Array.from(container.querySelectorAll("button")).find(
138 + (element) => element.textContent === label || element.getAttribute("aria-label") === label,
139 + );
140 + if (!result) throw new Error(`Missing button: ${label}`);
141 + return result;
142 +}
143 +
144 +function expectFailures() {
145 + const messages = mocks.alert.mock.calls
146 + .map(([title, message]) => `${title}\n${message}`)
147 + .join("\n");
148 + expect.soft(messages).toMatch(/Alpha(?:(?!Beta)[\s\S])*Permission denied/);
149 + expect.soft(messages).toMatch(/Beta(?:(?!Alpha)[\s\S])*Disk full/);
150 + expect.soft(messages).not.toContain("Healthy");
151 +}
152 +
153 +function failTwoEnvironments() {
154 + mocks.targets = [
155 + environment("healthy", "Healthy"),
156 + environment("alpha", "Alpha"),
157 + environment("beta", "Beta"),
158 + ];
159 + mocks.updateSettings.mockImplementation(async ({ environmentId }: { environmentId: string }) =>
160 + environmentId === "healthy"
161 + ? AsyncResult.success(undefined)
162 + : AsyncResult.failure(
163 + Cause.fail(new Error(environmentId === "alpha" ? "Permission denied" : "Disk full")),
164 + ),
165 + );
166 +}
167
168 let root: Root;
169 +let container: HTMLDivElement;
170
171 beforeEach(() => {
172 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
71 - root = createRoot(document.createElement("div"));
173 + container = document.createElement("div");
174 + root = createRoot(container);
175 + mocks.targets = [environment("local", "Local")];
176 + mocks.projectSelected = false;
177 + mocks.alert.mockReset();
178 mocks.updateSettings.mockReset();
179 + mocks.updateSettings.mockResolvedValue(AsyncResult.success(undefined));
180 });
181
182 afterEach(() => {
@@ -78,11 +185,8 @@ afterEach(() => {
185
186 describe("SettingsServerControlsRouteScreen", () => {
187 it("sends a draft saved during a pending write once that write settles", async () => {
81 - let settle = () => {};
82 - mocks.updateSettings.mockImplementationOnce(
83 - () => new Promise<void>((resolve) => (settle = resolve)),
84 - );
85 - mocks.updateSettings.mockResolvedValue(undefined);
188 + const pending = Promise.withResolvers<AsyncResult.Success<void>>();
189 + mocks.updateSettings.mockReturnValueOnce(pending.promise);
190 act(() => root.render(<SettingsEnvironmentSourceControlRouteScreen />));
191
192 act(() => mocks.branchNaming.onChange({ branchNamingMode: "custom" }));
@@ -90,7 +194,7 @@ describe("SettingsServerControlsRouteScreen", () => {
194 act(() => mocks.branchNaming.onChange({ branchNameInstructions: "Use issue IDs." }));
195 expect(mocks.updateSettings).toHaveBeenCalledOnce();
196
93 - await act(async () => settle());
197 + await act(async () => pending.resolve(AsyncResult.success(undefined)));
198
199 expect(mocks.updateSettings).toHaveBeenCalledTimes(2);
200 expect(mocks.updateSettings).toHaveBeenLastCalledWith({
@@ -101,3 +205,111 @@ describe("SettingsServerControlsRouteScreen", () => {
205 });
206 });
207 });
208 +
209 +describe.each([
210 + {
211 + name: "source control",
212 + Screen: SettingsEnvironmentSourceControlRouteScreen,
213 + label: "Automatically pull",
214 + key: "defaultAutoPull",
215 + },
216 + {
217 + name: "thread behavior",
218 + Screen: SettingsThreadsRouteScreen,
219 + label: "Auto-settle merged threads",
220 + key: "sidebarAutoSettleOnMerge",
221 + },
222 +] as const)("$name shared settings", ({ Screen, label, key }) => {
223 + it("identifies each failed environment and reason, then allows a subscription-driven retry", async () => {
224 + failTwoEnvironments();
225 + const pending = Promise.withResolvers<AsyncResult.Success<void>>();
226 + mocks.updateSettings.mockReturnValueOnce(pending.promise);
227 + act(() => root.render(<Screen />));
228 +
229 + act(() => button(label).click());
230 + expect(button(label).disabled).toBe(true);
231 + await act(async () => pending.resolve(AsyncResult.success(undefined)));
232 +
233 + expectFailures();
234 + expect(mocks.updateSettings.mock.calls.map(([input]) => input.environmentId)).toEqual([
235 + "healthy",
236 + "alpha",
237 + "beta",
238 + ]);
239 + expect(button(label).disabled).toBe(false);
240 + expect(button(label).getAttribute("aria-checked")).toBe("false");
241 +
242 + mocks.alert.mockClear();
243 + mocks.updateSettings.mockResolvedValue(AsyncResult.success(undefined));
244 + await act(async () => button(label).click());
245 + expect(mocks.updateSettings).toHaveBeenCalledTimes(6);
246 + expect(mocks.alert).not.toHaveBeenCalled();
247 + expect(button(label).getAttribute("aria-checked")).toBe("false");
248 +
249 + mocks.targets = mocks.targets.map((target) => ({
250 + ...target,
251 + serverConfig: {
252 + ...target.serverConfig,
253 + settings: { ...target.serverConfig.settings, [key]: true },
254 + },
255 + }));
256 + act(() => root.render(<Screen />));
257 + expect(button(label).getAttribute("aria-checked")).toBe("true");
258 + });
259 +
260 + it("reports failed project resets and keeps overrides visible until a successful retry is received", async () => {
261 + failTwoEnvironments();
262 + mocks.projectSelected = true;
263 + mocks.targets = mocks.targets.map((target) => ({
264 + ...target,
265 + serverConfig: {
266 + ...target.serverConfig,
267 + settings: {
268 + ...target.serverConfig.settings,
269 + projectSettingsOverrides: { project: { [key]: true } },
270 + },
271 + },
272 + }));
273 + act(() => root.render(<Screen />));
274 +
275 + await act(async () => button("Use environment defaults").click());
276 +
277 + expectFailures();
278 + expect(button(label).getAttribute("aria-checked")).toBe("true");
279 + expect(button("Use environment defaults").disabled).toBe(false);
280 +
281 + mocks.alert.mockClear();
282 + mocks.updateSettings.mockResolvedValue(AsyncResult.success(undefined));
283 + await act(async () => button("Use environment defaults").click());
284 + expect(mocks.updateSettings).toHaveBeenCalledTimes(6);
285 + expect(mocks.alert).not.toHaveBeenCalled();
286 + expect(button(label).getAttribute("aria-checked")).toBe("true");
287 +
288 + mocks.targets = mocks.targets.map((target) => ({
289 + ...target,
290 + serverConfig: {
291 + ...target.serverConfig,
292 + settings: { ...target.serverConfig.settings, projectSettingsOverrides: {} },
293 + },
294 + }));
295 + act(() => root.render(<Screen />));
296 + expect(button(label).getAttribute("aria-checked")).toBe("false");
297 + expect(container.textContent).not.toContain("Use defaults");
298 + });
299 +
300 + it("does not alert for an interrupted write and releases the controls for retry", async () => {
301 + const pending = Promise.withResolvers<AsyncResult.Failure<void>>();
302 + mocks.updateSettings.mockReturnValueOnce(pending.promise);
303 + act(() => root.render(<Screen />));
304 +
305 + act(() => button(label).click());
306 + expect(button(label).disabled).toBe(true);
307 + await act(async () => pending.resolve(AsyncResult.failure(Cause.interrupt(1))));
308 +
309 + expect(mocks.alert).not.toHaveBeenCalled();
310 + expect(button(label).disabled).toBe(false);
311 + await act(async () => button(label).click());
312 + expect(mocks.updateSettings).toHaveBeenCalledTimes(2);
313 + expect(mocks.alert).not.toHaveBeenCalled();
314 + });
315 +});
apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.tsx
+4 -16
@@ -18,8 +18,6 @@ import { View } from "react-native";
18 import { useSafeAreaInsets } from "react-native-safe-area-context";
19
20 import { RUNTIME_MODE_CHOICES } from "../threads/thread-settings-options";
21 -import { serverEnvironment } from "../../state/server";
22 -import { useAtomCommand } from "../../state/use-atom-command";
21 import { SettingsScreen } from "./components/SettingsScreen";
22 import {
23 AndroidSettingsEnvironmentFilter,
@@ -32,6 +30,7 @@ import { SettingsSection } from "./components/SettingsSection";
30 import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
31 import { SettingsProjectOverridesSection } from "./components/SettingsProjectOverridesSection";
32 import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
33 +import { useSettingsWriter } from "./useSettingsWriter";
34 import {
35 planMobileScopedSettingsClear,
36 planMobileScopedSettingsPatch,
@@ -172,10 +171,7 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
171 const isMixed = (key: keyof ServerSettings) =>
172 reference === null ||
173 displayTargets.some((entry) => entry.settings[key] !== reference.settings[key]);
175 - const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
176 - label: "environment settings update",
177 - reportFailure: true,
178 - });
174 + const writeSettings = useSettingsWriter();
175 const write = (patch: ServerSettingsPatch) => {
176 if (writeInFlight.current) {
177 const queued = queuedPatch.current;
@@ -197,11 +193,7 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
193 writeInFlight.current = true;
194 setPendingTargets(targets);
195 setPendingWrites((count) => count + 1);
200 - void Promise.allSettled(
201 - writes.map((entry) =>
202 - updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
203 - ),
204 - ).finally(() => {
196 + void writeSettings(targets, writes).finally(() => {
197 writeInFlight.current = false;
198 setPendingTargets(null);
199 setPendingWrites((count) => count - 1);
@@ -225,11 +217,7 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
217 writeInFlight.current = true;
218 setPendingTargets(targets);
219 setPendingWrites((count) => count + 1);
228 - void Promise.allSettled(
229 - writes.map((entry) =>
230 - updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
231 - ),
232 - ).finally(() => {
220 + void writeSettings(targets, writes).finally(() => {
221 writeInFlight.current = false;
222 // Resending a draft over these pre-clear targets would undo the clear.
223 queuedPatch.current = null;
apps/mobile/src/features/settings/SettingsThreadsRouteScreen.tsx
+4 -16
@@ -12,8 +12,6 @@ import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
12 import { supportsSharedSettingsSync } from "@t3tools/client-runtime/state/shared-settings";
13 import { AppText as Text } from "../../components/ui/AppText";
14 import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
15 -import { serverEnvironment } from "../../state/server";
16 -import { useAtomCommand } from "../../state/use-atom-command";
15 import { SettingsSection } from "./components/SettingsSection";
16 import { SettingsProjectOverridesSection } from "./components/SettingsProjectOverridesSection";
17 import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
@@ -24,6 +22,7 @@ import {
22 } from "./components/SettingsEnvironmentFilterHeader";
23 import { planAutoSettleSettingsSync, type AutoSettleSettings } from "./autoSettleSettingsSync";
24 import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
25 +import { useSettingsWriter } from "./useSettingsWriter";
26 import {
27 planMobileScopedSettingsClear,
28 planMobileScopedSettingsPatch,
@@ -67,10 +66,7 @@ function AutoSettleSettingsRows() {
66 const [pendingTargets, setPendingTargets] = useState<
67 readonly ScopedMobileSettingsTarget[] | null
68 >(null);
70 - const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
71 - label: "server settings update",
72 - reportFailure: true,
73 - });
69 + const writeSettings = useSettingsWriter();
70
71 const syncEnvironments = selectedTargets.filter(supportsSharedSettingsSync);
72 const syncTargets = resolveMobileSettingsTargets(
@@ -107,11 +103,7 @@ function AutoSettleSettingsRows() {
103 writeInFlight.current = true;
104 setPendingTargets(syncTargets);
105 setPendingWrites((count) => count + 1);
110 - void Promise.allSettled(
111 - writes.map((entry) =>
112 - updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
113 - ),
114 - ).finally(() => {
106 + void writeSettings(syncTargets, writes).finally(() => {
107 writeInFlight.current = false;
108 setPendingTargets(null);
109 setPendingWrites((count) => count - 1);
@@ -161,11 +153,7 @@ function AutoSettleSettingsRows() {
153 writeInFlight.current = true;
154 setPendingTargets(syncTargets);
155 setPendingWrites((count) => count + 1);
164 - void Promise.allSettled(
165 - writes.map((entry) =>
166 - updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
167 - ),
168 - ).finally(() => {
156 + void writeSettings(syncTargets, writes).finally(() => {
157 writeInFlight.current = false;
158 setPendingTargets(null);
159 setPendingWrites((count) => count - 1);
apps/mobile/src/features/settings/useSettingsWriter.ts new
+44
@@ -0,0 +1,44 @@
1 +import type { EnvironmentId, ServerSettingsPatch } from "@t3tools/contracts";
2 +import {
3 + isAtomCommandInterrupted,
4 + squashAtomCommandFailure,
5 +} from "@t3tools/client-runtime/state/runtime";
6 +
7 +import * as Dialog from "../../components/ui/Dialog";
8 +import { serverEnvironment } from "../../state/server";
9 +import { useAtomCommand } from "../../state/use-atom-command";
10 +import type { ScopedMobileSettingsTarget } from "./settings-scoped-server";
11 +
12 +export function useSettingsWriter() {
13 + const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
14 + label: "environment settings update",
15 + reportFailure: true,
16 + });
17 +
18 + return async (
19 + targets: readonly ScopedMobileSettingsTarget[],
20 + writes: readonly {
21 + readonly environmentId: EnvironmentId;
22 + readonly patch: ServerSettingsPatch;
23 + }[],
24 + ) => {
25 + const outcomes = await Promise.all(
26 + writes.map(async (entry) => {
27 + const result = await updateSettings({
28 + environmentId: entry.environmentId,
29 + input: { patch: entry.patch },
30 + });
31 + if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return null;
32 + const label =
33 + targets.find((target) => target.environment.environmentId === entry.environmentId)
34 + ?.environment.label ?? entry.environmentId;
35 + const error = squashAtomCommandFailure(result);
36 + return `${label}: ${error instanceof Error ? error.message : String(error)}`;
37 + }),
38 + );
39 + const failures = outcomes.filter((outcome) => outcome !== null);
40 + if (failures.length > 0) {
41 + Dialog.alert("Could not save settings", failures.join("\n\n"));
42 + }
43 + };
44 +}