Skip to content
TypeScript

fix(mobile): skip unsupported shared settings targets (#9381)

Simonecfddb4201df8
diff --git a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx
index 58a2779840c..41c2076ac7b 100644
--- a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx
+++ b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx
@@ -46,6 +46,7 @@ import {
import {
findSharedSettingsMismatches,
pickSharedServerSettings,
+ supportsSharedSettingsSync,
} from "@t3tools/client-runtime/state/shared-settings";
import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled";
import {
@@ -553,9 +554,9 @@ const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterD
/**
* Auto-settlement is a user preference that every server has to hold. Mobile
- * has no primary environment, so the first connected environment that
- * supports it is the reference value. Edits fan out to every connected
- * environment, and a mismatch row lets the user push the reference out.
+ * has no primary environment, so the first eligible sync target provides the
+ * reference value. Edits fan out to every eligible target, and a mismatch row
+ * lets the user push the reference out.
*/
function AutoSettleSettingsRows() {
const { environments } = useEnvironments();
@@ -564,12 +565,8 @@ function AutoSettleSettingsRows() {
reportFailure: true,
});
- const connected = environments.filter(
- (environment) =>
- environment.connection.phase === "connected" &&
- environment.serverConfig?.environment.capabilities.threadAutoSettlement === true,
- );
- const reference = connected[0] ?? null;
+ const syncTargets = environments.filter(supportsSharedSettingsSync);
+ const reference = syncTargets[0] ?? null;
const referenceSettings = reference?.serverConfig?.settings ?? null;
const [daysDraft, setDaysDraft] = useState<string | null>(null);
@@ -579,7 +576,7 @@ function AutoSettleSettingsRows() {
}
const writeToAll = (patch: ServerSettingsPatch) => {
- for (const environment of connected) {
+ for (const environment of syncTargets) {
void updateSettings({ environmentId: environment.environmentId, input: { patch } });
}
};
@@ -590,7 +587,7 @@ function AutoSettleSettingsRows() {
environments: environments.map((environment) => ({
environmentId: environment.environmentId,
label: environment.label,
- connected: environment.connection.phase === "connected",
+ syncEligible: supportsSharedSettingsSync(environment),
settings: environment.serverConfig?.settings ?? null,
})),
});
@@ -600,7 +597,7 @@ function AutoSettleSettingsRows() {
const draft = (daysDraft ?? "").trim();
setDaysDraft(null);
// Whole-string check so "3.5" and "3days" are rejected instead of
- // silently becoming 3 on every connected environment.
+ // silently becoming 3 on every eligible sync target.
const parsed = /^\d+$/.test(draft) ? Number(draft) : Number.NaN;
if (
Number.isInteger(parsed) &&
diff --git a/apps/web/src/hooks/useSettings.ts b/apps/web/src/hooks/useSettings.ts
index 6eb571f70aa..70e16ff33d1 100644
--- a/apps/web/src/hooks/useSettings.ts
+++ b/apps/web/src/hooks/useSettings.ts
@@ -29,6 +29,7 @@ import {
findSharedSettingsMismatches,
pickSharedServerSettings,
splitSharedServerPatch,
+ supportsSharedSettingsSync,
} from "@t3tools/client-runtime/state/shared-settings";
import { ensureLocalApi } from "~/localApi";
import {
@@ -42,11 +43,7 @@ import * as Struct from "effect/Struct";
import { toastManager } from "~/components/ui/toast";
import { isHostedStaticApp } from "~/hostedPairing";
import { primaryServerSettingsAtom, serverEnvironment } from "~/state/server";
-import {
- type EnvironmentPresentation,
- useEnvironments,
- usePrimaryEnvironment,
-} from "~/state/environments";
+import { useEnvironments, usePrimaryEnvironment } from "~/state/environments";
import { useAtomCommand } from "~/state/use-atom-command";
import { useTheme } from "./useTheme";
@@ -332,26 +329,14 @@ export function usePrimarySettingsAvailable(): boolean {
return primaryEnvironment !== null || !isHostedStaticApp();
}
-/**
- * Whether an environment can hold every shared key right now. Gated on the
- * auto-settlement capability because it is the newest of the shared keys: a
- * server that has it has all of them. Older servers drop unknown keys on
- * write, so a mismatch against them could never clear, and their decoded
- * defaults must not be treated as real values.
- */
-function supportsSharedSettings(environment: EnvironmentPresentation): boolean {
- return (
- environment.connection.phase === "connected" &&
- environment.serverConfig?.environment.capabilities.threadAutoSettlement === true
- );
-}
-
/** Environments that can receive a shared settings write right now. */
-function useConnectedEnvironmentIds(): ReadonlyArray<EnvironmentId> {
+function useSharedSettingsSyncTargetIds(): ReadonlyArray<EnvironmentId> {
const { environments } = useEnvironments();
return useMemo(
() =>
- environments.filter(supportsSharedSettings).map((environment) => environment.environmentId),
+ environments
+ .filter(supportsSharedSettingsSync)
+ .map((environment) => environment.environmentId),
[environments],
);
}
@@ -361,16 +346,16 @@ function useConnectedEnvironmentIds(): ReadonlyArray<EnvironmentId> {
*
* Server keys are optimistically patched in atom-backed server state, then
* persisted via RPC. Shared server keys (see `SHARED_SERVER_SETTING_KEYS`)
- * are written to every connected environment, not only the target, so a user
- * preference does not silently drift between machines. Client keys go through
- * client persistence.
+ * are written to every eligible sync target, not only the selected target, so
+ * a user preference does not silently drift between machines. Client keys go
+ * through client persistence.
*/
function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
const persistServerSettings = useAtomCommand(
serverEnvironment.updateSettings,
"server settings update",
);
- const connectedEnvironmentIds = useConnectedEnvironmentIds();
+ const sharedSettingsSyncTargetIds = useSharedSettingsSyncTargetIds();
const updateSettings = useCallback(
(patch: UnifiedSettingsPatch) => {
const { serverPatch, clientPatch } = splitPatch(patch);
@@ -395,7 +380,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
}
}
if (Object.keys(sharedPatch).length > 0) {
- const targets = new Set(connectedEnvironmentIds);
+ const targets = new Set(sharedSettingsSyncTargetIds);
if (environmentId) {
targets.add(environmentId);
}
@@ -417,14 +402,14 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
});
}
},
- [connectedEnvironmentIds, environmentId, persistServerSettings],
+ [environmentId, persistServerSettings, sharedSettingsSyncTargetIds],
);
return updateSettings;
}
/**
- * Connected environments whose shared settings differ from the primary's,
+ * Shared-settings sync targets whose values differ from the primary's,
* plus an action that writes the primary's values to all of them. Drift
* happens when an environment was offline during an edit or was changed by
* an older client.
@@ -437,7 +422,7 @@ export function useSharedSettingsSync() {
// must never push defaults over real values. Same for a primary too old to
// hold the shared keys: its decoded defaults are not a source of truth.
const primarySettings =
- primaryEnvironment !== null && supportsSharedSettings(primaryEnvironment)
+ primaryEnvironment !== null && supportsSharedSettingsSync(primaryEnvironment)
? (primaryEnvironment.serverConfig?.settings ?? null)
: null;
const { environments } = useEnvironments();
@@ -454,7 +439,7 @@ export function useSharedSettingsSync() {
environments: environments.map((environment) => ({
environmentId: environment.environmentId,
label: environment.label,
- connected: supportsSharedSettings(environment),
+ syncEligible: supportsSharedSettingsSync(environment),
settings: environment.serverConfig?.settings ?? null,
})),
}),
diff --git a/docs/internals/overview.md b/docs/internals/overview.md
index 37f8c60ac11..7ad971ae745 100644
--- a/docs/internals/overview.md
+++ b/docs/internals/overview.md
@@ -90,9 +90,10 @@ A turn is complete when its session leaves `running` status, projected by
does not define turn end.
Thread settlement is server-owned. Each server's own settings control PR and inactivity
-settlement. Those keys are user preferences, so clients write them to every connected environment
-(`SHARED_SERVER_SETTING_KEYS` in `packages/client-runtime/src/state/sharedSettings.ts`) and warn
-when a connected environment drifts.
+settlement. Those keys are user preferences, so clients write them to every shared-settings sync
+target (`SHARED_SERVER_SETTING_KEYS` in `packages/client-runtime/src/state/sharedSettings.ts`) and
+warn when another target drifts. A target must have an active connection and advertise the
+`threadAutoSettlement` capability, which signals that the server can hold every shared key.
[`ThreadSettlementReactor`][settlement] checks threads at startup, when those settings change, and
once per minute, including when no client is connected. It dispatches the guarded internal
`thread.auto-settle` command, which uses the existing settlement event lifecycle. Automatic
diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md
index 3bbdc6888fd..204678eb1d2 100644
--- a/docs/user/thread-sidebar.md
+++ b/docs/user/thread-sidebar.md
@@ -22,11 +22,13 @@ available, the inactivity rule still applies. A manual un-settle also keeps the
sorts by the moment you settled it. A thread that settled on its own sorts by its last message or
turn, not by when the server noticed it was inactive.
-Change these rules in **Settings > General**. The change is written to every environment you are
-connected to at that moment. An environment that is offline keeps its old value. When a connected
-environment holds a different value, **Settings > General** shows a warning that names it. Choose
-**Apply to all** to write your current values to every connected environment. The same applies to
-the new-thread workspace mode and the source control writing style.
+Change these rules in **Settings > General**. The change is written to every connected environment
+whose server supports shared settings. An environment that is offline or needs a server update
+keeps its old value and does not appear in mismatch warnings. When a connected environment whose
+server supports shared settings holds a different value, **Settings > General** shows a warning
+that names it. Choose **Apply to all** to write your current values to the environments named in
+the warning. The same applies to the new-thread workspace mode and the source control writing
+style.
A settings change affects future settlement and does not reopen a settled thread. Settings saved
by older clients on one device no longer control this behavior.
diff --git a/packages/client-runtime/src/state/sharedSettings.test.ts b/packages/client-runtime/src/state/sharedSettings.test.ts
index dbdf651180d..8c46a9f3357 100644
--- a/packages/client-runtime/src/state/sharedSettings.test.ts
+++ b/packages/client-runtime/src/state/sharedSettings.test.ts
@@ -5,12 +5,36 @@ import {
findSharedSettingsMismatches,
pickSharedServerSettings,
splitSharedServerPatch,
+ supportsSharedSettingsSync,
} from "./sharedSettings.ts";
const primaryId = EnvironmentId.make("env-primary");
const laptopId = EnvironmentId.make("env-laptop");
const boxId = EnvironmentId.make("env-box");
+describe("supportsSharedSettingsSync", () => {
+ it("accepts only connected servers that advertise the shared-settings capability", () => {
+ expect(
+ supportsSharedSettingsSync({
+ connection: { phase: "connected" },
+ serverConfig: { environment: { capabilities: { threadAutoSettlement: true } } },
+ }),
+ ).toBe(true);
+ expect(
+ supportsSharedSettingsSync({
+ connection: { phase: "connected" },
+ serverConfig: { environment: { capabilities: {} } },
+ }),
+ ).toBe(false);
+ expect(
+ supportsSharedSettingsSync({
+ connection: { phase: "reconnecting" },
+ serverConfig: { environment: { capabilities: { threadAutoSettlement: true } } },
+ }),
+ ).toBe(false);
+ });
+});
+
describe("splitSharedServerPatch", () => {
it("routes preference keys to the shared patch and machine keys to the local patch", () => {
const { sharedPatch, localPatch } = splitSharedServerPatch({
@@ -38,17 +62,27 @@ describe("pickSharedServerSettings", () => {
describe("findSharedSettingsMismatches", () => {
const primarySettings = { ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: 7 };
- it("lists connected environments whose shared settings differ", () => {
+ it("lists sync-eligible environments whose shared settings differ", () => {
const mismatches = findSharedSettingsMismatches({
primaryEnvironmentId: primaryId,
primarySettings,
environments: [
- { environmentId: primaryId, label: "Desktop", connected: true, settings: primarySettings },
- { environmentId: laptopId, label: "Laptop", connected: true, settings: primarySettings },
+ {
+ environmentId: primaryId,
+ label: "Desktop",
+ syncEligible: true,
+ settings: primarySettings,
+ },
+ {
+ environmentId: laptopId,
+ label: "Laptop",
+ syncEligible: true,
+ settings: primarySettings,
+ },
{
environmentId: boxId,
label: "Remote Box",
- connected: true,
+ syncEligible: true,
settings: DEFAULT_SERVER_SETTINGS,
},
],
@@ -64,7 +98,7 @@ describe("findSharedSettingsMismatches", () => {
{
environmentId: boxId,
label: "Remote Box",
- connected: true,
+ syncEligible: true,
settings: { ...primarySettings, enableAgentBrowserAccess: false },
},
],
@@ -74,7 +108,12 @@ describe("findSharedSettingsMismatches", () => {
it("reports nothing until the primary environment's settings are loaded", () => {
const environments = [
- { environmentId: boxId, label: "Remote Box", connected: true, settings: primarySettings },
+ {
+ environmentId: boxId,
+ label: "Remote Box",
+ syncEligible: true,
+ settings: primarySettings,
+ },
];
expect(
findSharedSettingsMismatches({ primaryEnvironmentId: null, primarySettings, environments }),
@@ -88,7 +127,7 @@ describe("findSharedSettingsMismatches", () => {
).toEqual([]);
});
- it("skips offline environments and environments without a loaded config", () => {
+ it("skips ineligible environments and environments without a loaded config", () => {
const mismatches = findSharedSettingsMismatches({
primaryEnvironmentId: primaryId,
primarySettings,
@@ -96,10 +135,10 @@ describe("findSharedSettingsMismatches", () => {
{
environmentId: laptopId,
label: "Laptop",
- connected: false,
+ syncEligible: false,
settings: DEFAULT_SERVER_SETTINGS,
},
- { environmentId: boxId, label: "Remote Box", connected: true, settings: null },
+ { environmentId: boxId, label: "Remote Box", syncEligible: true, settings: null },
],
});
expect(mismatches).toEqual([]);
diff --git a/packages/client-runtime/src/state/sharedSettings.ts b/packages/client-runtime/src/state/sharedSettings.ts
index 35fa4adb46b..f3236ef2035 100644
--- a/packages/client-runtime/src/state/sharedSettings.ts
+++ b/packages/client-runtime/src/state/sharedSettings.ts
@@ -4,14 +4,21 @@
* Every server keeps its own `settings.json`, but some keys are user
* preferences that only live on the server because the server has to act on
* them (auto-settlement runs with no client attached). A user does not want
- * those to differ per machine. Clients write these keys to every connected
- * environment, and warn when a connected environment still holds a different
- * value so the user can push their current value out.
+ * those to differ per machine. Clients write these keys to every shared-settings
+ * sync target, and warn when another target still holds a different value so
+ * the user can push their current value out.
*/
-import type { EnvironmentId, ServerSettings, ServerSettingsPatch } from "@t3tools/contracts";
+import type {
+ EnvironmentId,
+ ExecutionEnvironmentCapabilities,
+ ServerSettings,
+ ServerSettingsPatch,
+} from "@t3tools/contracts";
import * as Equal from "effect/Equal";
import * as Struct from "effect/Struct";
+import type { EnvironmentConnectionPhase } from "../connection/presentation.ts";
+
/** Server keys that hold a user preference rather than machine config. */
export const SHARED_SERVER_SETTING_KEYS = [
"sidebarAutoSettleAfterDays",
@@ -50,18 +57,37 @@ export function pickSharedServerSettings(settings: ServerSettings): ServerSettin
return Struct.pick(settings, SHARED_SERVER_SETTING_KEYS);
}
+/**
+ * Whether an environment can participate in shared-settings sync right now.
+ * Auto-settlement is the newest feature backed by a shared key, so a server
+ * advertising `threadAutoSettlement` can hold every shared key.
+ */
+export function supportsSharedSettingsSync(environment: {
+ readonly connection: { readonly phase: EnvironmentConnectionPhase };
+ readonly serverConfig: {
+ readonly environment: {
+ readonly capabilities: Pick<ExecutionEnvironmentCapabilities, "threadAutoSettlement">;
+ };
+ } | null;
+}): boolean {
+ return (
+ environment.connection.phase === "connected" &&
+ environment.serverConfig?.environment.capabilities.threadAutoSettlement === true
+ );
+}
+
export interface SharedSettingsEnvironment {
readonly environmentId: EnvironmentId;
readonly label: string;
- readonly connected: boolean;
+ readonly syncEligible: boolean;
readonly settings: ServerSettings | null;
}
/**
- * Connected environments whose shared settings differ from the primary
- * environment's. Offline environments are skipped: nothing can be read from
- * or written to them, and the warning would never clear. With no primary
- * settings loaded there is nothing to compare against, so nothing is
+ * Shared-settings sync targets whose values differ from the primary
+ * environment's. Other environments are skipped: nothing can be read from or
+ * written to them, or their server cannot hold every shared key. With no
+ * primary settings loaded there is nothing to compare against, so nothing is
* reported. Callers must pass the real loaded settings, never a default
* fallback, or "apply to all" would push defaults over real values.
*/
@@ -77,7 +103,7 @@ export function findSharedSettingsMismatches(input: {
return input.environments.flatMap((environment) => {
if (
environment.environmentId === input.primaryEnvironmentId ||
- !environment.connected ||
+ !environment.syncEligible ||
environment.settings === null
) {
return [];