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

This commit is contained in:
Simone authored and GitHub committed 2026-09-03 09:35:25 -07:00
1 parent 57626eb6ea
commit cfddb4201d
6 files changed
+119 -69

No files matched your search

@@ -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([]);
@@ -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 [];