Skip to content

Commit f47a3fe

Browse files
authored
fix(settings): share restart continuation across environments (#9933)
1 parent c7dc3cb commit f47a3fe

8 files changed

Lines changed: 204 additions & 42 deletions

File tree

‎apps/mobile/src/features/settings/SettingsRouteScreen.tsx‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
type ServerSettingsPatch,
4545
} from "@t3tools/contracts";
4646
import {
47+
filterSharedServerPatch,
4748
findSharedSettingsMismatches,
4849
pickSharedServerSettings,
4950
supportsSharedSettingsSync,
@@ -584,11 +585,13 @@ function AutoSettleSettingsRows() {
584585
const mismatches = findSharedSettingsMismatches({
585586
primaryEnvironmentId: reference.environmentId,
586587
primarySettings: referenceSettings,
588+
primaryCapabilities: reference.serverConfig?.environment.capabilities,
587589
environments: environments.map((environment) => ({
588590
environmentId: environment.environmentId,
589591
label: environment.label,
590592
syncEligible: supportsSharedSettingsSync(environment),
591593
settings: environment.serverConfig?.settings ?? null,
594+
capabilities: environment.serverConfig?.environment.capabilities,
592595
})),
593596
});
594597

@@ -652,11 +655,22 @@ function AutoSettleSettingsRows() {
652655
<Pressable
653656
accessibilityRole="button"
654657
onPress={() => {
655-
const patch = pickSharedServerSettings(referenceSettings);
658+
const patch = pickSharedServerSettings(
659+
referenceSettings,
660+
reference.serverConfig?.environment.capabilities,
661+
);
656662
for (const mismatch of mismatches) {
663+
const target = environments.find(
664+
(candidate) => candidate.environmentId === mismatch.environmentId,
665+
);
657666
void updateSettings({
658667
environmentId: mismatch.environmentId,
659-
input: { patch },
668+
input: {
669+
patch: filterSharedServerPatch(
670+
patch,
671+
target?.serverConfig?.environment.capabilities,
672+
),
673+
},
660674
});
661675
}
662676
}}

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ export const make = Effect.gen(function* () {
219219
pullRequests: true,
220220
threadSettlement: true,
221221
threadAutoSettlement: true,
222+
threadRestartContinuation: true,
222223
threadSnooze: true,
223224
environmentThemes: true,
224225
usageLimitSources: true,

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ export function GeneralSettingsPanel() {
24392439
<SettingsRow
24402440
{...searchableSetting("continue-threads-after-server-update")}
24412441
serverScoped
2442-
description="Automatically resume interrupted threads when this environment starts again after an update, crash, or machine restart."
2442+
description="Automatically resume interrupted threads after an update, crash, or machine restart. Applies to this environment and all connected environments that support it. Update older servers first."
24432443
resetAction={
24442444
settings.continueThreadsAfterServerUpdate !==
24452445
DEFAULT_UNIFIED_SETTINGS.continueThreadsAfterServerUpdate ? (

‎apps/web/src/hooks/useSettings.ts‎

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "@t3tools/contracts/settings";
2727
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
2828
import {
29+
filterSharedServerPatch,
2930
findSharedSettingsMismatches,
3031
pickSharedServerSettings,
3132
splitSharedServerPatch,
@@ -368,18 +369,6 @@ export function usePrimarySettingsAvailable(): boolean {
368369
return primaryEnvironment !== null || !isHostedStaticApp();
369370
}
370371

371-
/** Environments that can receive a shared settings write right now. */
372-
function useSharedSettingsSyncTargetIds(): ReadonlyArray<EnvironmentId> {
373-
const { environments } = useEnvironments();
374-
return useMemo(
375-
() =>
376-
environments
377-
.filter(supportsSharedSettingsSync)
378-
.map((environment) => environment.environmentId),
379-
[environments],
380-
);
381-
}
382-
383372
/**
384373
* Returns an updater that routes each key to the correct backing store.
385374
*
@@ -394,19 +383,19 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
394383
serverEnvironment.updateSettings,
395384
"server settings update",
396385
);
397-
const sharedSettingsSyncTargetIds = useSharedSettingsSyncTargetIds();
386+
const { environments } = useEnvironments();
398387
const updateSettings = useCallback(
399388
(patch: UnifiedSettingsPatch) => {
400389
const { serverPatch, clientPatch } = splitPatch(patch);
401390

402391
if (Object.keys(serverPatch).length > 0) {
403392
const { sharedPatch, localPatch } = splitSharedServerPatch(serverPatch);
404393
// Dropping the write silently leaves the control looking saved.
405-
const warnUnsaved = () =>
394+
const warnUnsaved = (description = PRIMARY_SETTINGS_UNAVAILABLE_MESSAGE) =>
406395
toastManager.add({
407396
type: "warning",
408397
title: "Setting not saved",
409-
description: PRIMARY_SETTINGS_UNAVAILABLE_MESSAGE,
398+
description,
410399
});
411400
if (Object.keys(localPatch).length > 0) {
412401
if (environmentId) {
@@ -419,26 +408,38 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
419408
}
420409
}
421410
if (Object.keys(sharedPatch).length > 0) {
422-
const targets = new Set(sharedSettingsSyncTargetIds);
411+
const targets = new Set(
412+
environments.filter(supportsSharedSettingsSync).map((target) => target.environmentId),
413+
);
423414
if (environmentId) {
424415
targets.add(environmentId);
425416
}
426-
if (targets.size === 0) {
427-
warnUnsaved();
428-
}
417+
let wroteToTarget = false;
429418
for (const targetId of targets) {
419+
const target = environments.find((candidate) => candidate.environmentId === targetId);
420+
const targetPatch = filterSharedServerPatch(
421+
sharedPatch,
422+
target?.serverConfig?.environment.capabilities,
423+
);
424+
if (Object.keys(targetPatch).length === 0) continue;
425+
wroteToTarget = true;
430426
void persistServerSettings({
431427
environmentId: targetId,
432-
input: { patch: sharedPatch },
428+
input: { patch: targetPatch },
433429
});
434430
}
431+
if (!wroteToTarget) {
432+
warnUnsaved(
433+
targets.size > 0 ? "Update older servers to save this setting." : undefined,
434+
);
435+
}
435436
}
436437
}
437438
if (Object.keys(clientPatch).length > 0) {
438439
persistClientSettingsPatch(clientPatch);
439440
}
440441
},
441-
[environmentId, persistServerSettings, sharedSettingsSyncTargetIds],
442+
[environmentId, environments, persistServerSettings],
442443
);
443444

444445
return updateSettings;
@@ -453,6 +454,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
453454
export function useSharedSettingsSync() {
454455
const primaryEnvironment = usePrimaryEnvironment();
455456
const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null;
457+
const primaryCapabilities = primaryEnvironment?.serverConfig?.environment.capabilities;
456458
// Read the loaded config, not `primaryServerSettingsAtom`: that atom falls
457459
// back to defaults while the primary is disconnected, and "apply to all"
458460
// must never push defaults over real values. Same for a primary too old to
@@ -472,28 +474,35 @@ export function useSharedSettingsSync() {
472474
findSharedSettingsMismatches({
473475
primaryEnvironmentId,
474476
primarySettings,
477+
primaryCapabilities,
475478
environments: environments.map((environment) => ({
476479
environmentId: environment.environmentId,
477480
label: environment.label,
478481
syncEligible: supportsSharedSettingsSync(environment),
479482
settings: environment.serverConfig?.settings ?? null,
483+
capabilities: environment.serverConfig?.environment.capabilities,
480484
})),
481485
}),
482-
[environments, primaryEnvironmentId, primarySettings],
486+
[environments, primaryEnvironmentId, primarySettings, primaryCapabilities],
483487
);
484488

485489
const applyToAll = useCallback(() => {
486490
if (primarySettings === null) {
487491
return;
488492
}
489-
const patch = pickSharedServerSettings(primarySettings);
493+
const patch = pickSharedServerSettings(primarySettings, primaryCapabilities);
490494
for (const mismatch of mismatches) {
495+
const target = environments.find(
496+
(candidate) => candidate.environmentId === mismatch.environmentId,
497+
);
491498
void persistServerSettings({
492499
environmentId: mismatch.environmentId,
493-
input: { patch },
500+
input: {
501+
patch: filterSharedServerPatch(patch, target?.serverConfig?.environment.capabilities),
502+
},
494503
});
495504
}
496-
}, [mismatches, persistServerSettings, primarySettings]);
505+
}, [environments, mismatches, persistServerSettings, primarySettings, primaryCapabilities]);
497506

498507
return { mismatches, applyToAll };
499508
}

‎docs/user/updating.md‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ Server updates restart the connection and can interrupt active agents and
1111
terminal commands. Saved threads, settings, and project files remain.
1212

1313
**Settings → General → Continue threads after restarts** is off by default.
14-
Enable it for each environment to resume supported active threads after an
15-
update, crash, or machine restart. T3 Code must start again on that machine;
14+
Enable it to resume supported active threads after an update, crash, or machine
15+
restart. Changes are saved to connected environments that support this setting;
16+
update older servers first. If a supported environment was offline or has a
17+
different value, use **Apply to all** in Settings after it connects.
18+
T3 Code must start again on that machine;
1619
the setting does not enable automatic startup. Terminal commands may still be
1720
interrupted, and threads without saved provider resume state need a new message.
18-
If you previously enabled continuation for updates, enable this environment
19-
setting once to allow recovery without a connected client.
21+
If you previously enabled continuation for updates, enable this setting once
22+
to allow recovery without a connected client.
2023

2124
## Update a connected server
2225

‎packages/client-runtime/src/state/sharedSettings.test.ts‎

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DEFAULT_SERVER_SETTINGS, EnvironmentId } from "@t3tools/contracts";
22
import { describe, expect, it } from "@effect/vitest";
33

44
import {
5+
filterSharedServerPatch,
56
findSharedSettingsMismatches,
67
pickSharedServerSettings,
78
splitSharedServerPatch,
@@ -11,6 +12,7 @@ import {
1112
const primaryId = EnvironmentId.make("env-primary");
1213
const laptopId = EnvironmentId.make("env-laptop");
1314
const boxId = EnvironmentId.make("env-box");
15+
const restartCapabilities = { threadRestartContinuation: true };
1416

1517
describe("supportsSharedSettingsSync", () => {
1618
it("accepts only connected servers that advertise the shared-settings capability", () => {
@@ -40,16 +42,24 @@ describe("splitSharedServerPatch", () => {
4042
const { sharedPatch, localPatch } = splitSharedServerPatch({
4143
sidebarAutoSettleAfterDays: 7,
4244
sidebarAutoSettleOnMerge: false,
45+
continueThreadsAfterServerUpdate: true,
4346
enableAgentBrowserAccess: false,
4447
});
45-
expect(sharedPatch).toEqual({ sidebarAutoSettleAfterDays: 7, sidebarAutoSettleOnMerge: false });
48+
expect(sharedPatch).toEqual({
49+
sidebarAutoSettleAfterDays: 7,
50+
sidebarAutoSettleOnMerge: false,
51+
continueThreadsAfterServerUpdate: true,
52+
});
4653
expect(localPatch).toEqual({ enableAgentBrowserAccess: false });
4754
});
4855
});
4956

5057
describe("pickSharedServerSettings", () => {
5158
it("returns only the shared keys", () => {
52-
expect(Object.keys(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS)).sort()).toEqual([
59+
expect(
60+
Object.keys(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS, restartCapabilities)).sort(),
61+
).toEqual([
62+
"continueThreadsAfterServerUpdate",
5363
"defaultThreadEnvMode",
5464
"newWorktreesStartFromOrigin",
5565
"sidebarAutoSettleAfterDays",
@@ -59,9 +69,106 @@ describe("pickSharedServerSettings", () => {
5969
});
6070
});
6171

72+
describe("filterSharedServerPatch", () => {
73+
it.each([true, false])("preserves supported restart preference %s", (enabled) => {
74+
const patch = { continueThreadsAfterServerUpdate: enabled, sidebarAutoSettleAfterDays: 7 };
75+
expect(filterSharedServerPatch(patch, restartCapabilities)).toEqual(patch);
76+
});
77+
78+
it.each([undefined, {}, { threadRestartContinuation: false }])(
79+
"omits only the unsupported restart preference with capabilities %j",
80+
(capabilities) => {
81+
expect(
82+
filterSharedServerPatch(
83+
{ continueThreadsAfterServerUpdate: true, sidebarAutoSettleAfterDays: 7 },
84+
capabilities,
85+
),
86+
).toEqual({ sidebarAutoSettleAfterDays: 7 });
87+
expect(pickSharedServerSettings(DEFAULT_SERVER_SETTINGS, capabilities)).not.toHaveProperty(
88+
"continueThreadsAfterServerUpdate",
89+
);
90+
},
91+
);
92+
});
93+
6294
describe("findSharedSettingsMismatches", () => {
6395
const primarySettings = { ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: 7 };
6496

97+
it.each([true, false])(
98+
"detects remote restart continuation drift when the preference is %s",
99+
(enabled) => {
100+
const settings = { ...primarySettings, continueThreadsAfterServerUpdate: enabled };
101+
const remoteSettings = { ...settings, continueThreadsAfterServerUpdate: !enabled };
102+
const environment = {
103+
environmentId: boxId,
104+
label: "Remote Box",
105+
syncEligible: true,
106+
settings: remoteSettings,
107+
capabilities: restartCapabilities,
108+
};
109+
expect(
110+
findSharedSettingsMismatches({
111+
primaryEnvironmentId: primaryId,
112+
primarySettings: settings,
113+
primaryCapabilities: restartCapabilities,
114+
environments: [environment],
115+
}),
116+
).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
117+
expect(
118+
findSharedSettingsMismatches({
119+
primaryEnvironmentId: primaryId,
120+
primarySettings: settings,
121+
primaryCapabilities: restartCapabilities,
122+
environments: [
123+
{
124+
...environment,
125+
settings: Object.assign(
126+
{},
127+
remoteSettings,
128+
pickSharedServerSettings(settings, restartCapabilities),
129+
),
130+
},
131+
],
132+
}),
133+
).toEqual([]);
134+
},
135+
);
136+
137+
it.each([
138+
[undefined, restartCapabilities],
139+
[restartCapabilities, undefined],
140+
[undefined, undefined],
141+
])(
142+
"ignores restart drift unless both servers support it (%j, %j)",
143+
(primaryCapabilities, capabilities) => {
144+
const environment = {
145+
environmentId: boxId,
146+
label: "Remote Box",
147+
syncEligible: true,
148+
capabilities,
149+
settings: { ...primarySettings, continueThreadsAfterServerUpdate: true },
150+
};
151+
const input = {
152+
primaryEnvironmentId: primaryId,
153+
primarySettings,
154+
primaryCapabilities,
155+
environments: [environment],
156+
};
157+
expect(findSharedSettingsMismatches(input)).toEqual([]);
158+
expect(
159+
findSharedSettingsMismatches({
160+
...input,
161+
environments: [
162+
{
163+
...environment,
164+
settings: { ...environment.settings, sidebarAutoSettleAfterDays: 14 },
165+
},
166+
],
167+
}),
168+
).toEqual([{ environmentId: boxId, label: "Remote Box" }]);
169+
},
170+
);
171+
65172
it("lists sync-eligible environments whose shared settings differ", () => {
66173
const mismatches = findSharedSettingsMismatches({
67174
primaryEnvironmentId: primaryId,

0 commit comments

Comments
 (0)