TypeScript
feat(connections): balance new threads across connected machines (#9895)
maria420fd76f6043
diff --git a/apps/desktop/src/settings/DesktopClientSettings.test.ts b/apps/desktop/src/settings/DesktopClientSettings.test.tsindex 9fbacc832a9..89e1a7fb19e 100644--- a/apps/desktop/src/settings/DesktopClientSettings.test.ts+++ b/apps/desktop/src/settings/DesktopClientSettings.test.ts@@ -58,6 +58,8 @@ const clientSettings: ClientSettings = {sidebarThreadSortOrder: "created_at",sidebarThreadPreviewCount: 6,legacySidebarEnabled: false,+ loadBalancingEnabled: false,+ loadBalancingWeights: { "environment-1": 75, "environment-2": 0 },timestampFormat: "24-hour",wordWrap: true,};diff --git a/apps/server/src/auth/RpcAuthorization.ts b/apps/server/src/auth/RpcAuthorization.tsindex 7bd1ed6c45f..a069322aa8b 100644--- a/apps/server/src/auth/RpcAuthorization.ts+++ b/apps/server/src/auth/RpcAuthorization.ts@@ -53,6 +53,7 @@ export const RPC_REQUIRED_SCOPES = {[WS_METHODS.serverDiscoverSourceControl]: AuthOrchestrationReadScope,[WS_METHODS.serverGetTraceDiagnostics]: AuthOrchestrationReadScope,[WS_METHODS.serverGetProcessDiagnostics]: AuthOrchestrationReadScope,+ [WS_METHODS.serverGetHostResources]: AuthOrchestrationReadScope,[WS_METHODS.serverGetProcessResourceHistory]: AuthOrchestrationReadScope,[WS_METHODS.serverGetResourceTelemetryHistory]: AuthOrchestrationReadScope,[WS_METHODS.serverRetryResourceTelemetry]: AuthOrchestrationOperateScope,diff --git a/apps/server/src/resourceTelemetry/HostResources.ts b/apps/server/src/resourceTelemetry/HostResources.tsnew file mode 100644index 00000000000..032832dd486--- /dev/null+++ b/apps/server/src/resourceTelemetry/HostResources.ts@@ -0,0 +1,93 @@+import * as NodeOS from "node:os";+import type { HostResourcesSnapshot } from "@t3tools/contracts";+import { HostProcessPlatform } from "@t3tools/shared/hostProcess";+import * as Cache from "effect/Cache";+import * as Context from "effect/Context";+import * as DateTime from "effect/DateTime";+import * as Effect from "effect/Effect";+import * as FileSystem from "effect/FileSystem";+import * as Layer from "effect/Layer";+import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";++export class HostResources extends Context.Service<+ HostResources,+ { readonly read: Effect.Effect<HostResourcesSnapshot> }+>()("t3/resourceTelemetry/HostResources") {}++function readCpu() {+ const cpus = NodeOS.cpus();+ const cpu = cpus.reduce(+ (sum, { times }) => ({+ idle: sum.idle + times.idle,+ total: sum.total + times.user + times.nice + times.sys + times.idle + times.irq,+ }),+ { idle: 0, total: 0 },+ );+ return { ...cpu, count: cpus.length };+}++function darwinAvailableMemory(output: string): number | null {+ const pageSize = /page size of (\d+) bytes/.exec(output)?.[1];+ const free = /^Pages free:\s+(\d+)\./m.exec(output)?.[1];+ const inactive = /^Pages inactive:\s+(\d+)\./m.exec(output)?.[1];+ const speculative = /^Pages speculative:\s+(\d+)\./m.exec(output)?.[1];+ if (!pageSize || !free || !inactive || !speculative) return null;+ // vm_stat subtracts speculative pages from its printed "Pages free" count.+ // Adding them here counts each reclaimable page once; purgeable pages overlap.+ const available = (Number(free) + Number(inactive) + Number(speculative)) * Number(pageSize);+ return Number.isSafeInteger(available) && Number(pageSize) > 0 ? available : null;+}++export const make = Effect.fn("makeHostResources")(function* () {+ const fs = yield* FileSystem.FileSystem;+ const platform = yield* HostProcessPlatform;+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;++ const sample = Effect.fn("HostResources.sample")(function* () {+ const previousCpu = readCpu();+ // CPU counters need two readings; idle servers do no polling or process scans.+ yield* Effect.sleep("200 millis");+ const cpu = readCpu();+ const totalDelta = cpu.total - previousCpu.total;+ const idleDelta = cpu.idle - previousCpu.idle;+ const cpuUtilization =+ previousCpu.count === cpu.count && totalDelta > 0 && idleDelta >= 0+ ? Math.min(1, Math.max(0, 1 - idleDelta / totalDelta))+ : null;+ const totalMemoryBytes = NodeOS.totalmem();+ // On Windows libuv returns GlobalMemoryStatusEx.ullAvailPhys, including standby memory.+ let availableMemoryBytes = NodeOS.freemem();+ if (platform === "linux") {+ const meminfo = yield* fs+ .readFileString("/proc/meminfo")+ .pipe(Effect.catch(() => Effect.succeed("")));+ const available = /^MemAvailable:\s+(\d+)\s+kB$/m.exec(meminfo)?.[1];+ if (available) availableMemoryBytes = Number(available) * 1024;+ } else if (platform === "darwin") {+ const output = yield* spawner+ .string(ChildProcess.make("/usr/bin/vm_stat", [], { stdin: "ignore", stderr: "ignore" }))+ .pipe(+ Effect.timeout("1 second"),+ Effect.catch(() => Effect.succeed("")),+ );+ availableMemoryBytes = darwinAvailableMemory(output) ?? availableMemoryBytes;+ }+ return {+ sampledAt: DateTime.toEpochMillis(yield* DateTime.now),+ cpuUtilization,+ cpuCount: cpu.count,+ availableMemoryBytes: Math.min(totalMemoryBytes, Math.max(0, availableMemoryBytes)),+ totalMemoryBytes,+ };+ });++ // One server-lifetime cache deduplicates simultaneous requests from all sockets.+ const cache = yield* Cache.make({+ capacity: 1,+ lookup: (_key: "host") => sample(),+ timeToLive: "5 seconds",+ });+ return HostResources.of({ read: Cache.get(cache, "host") });+});++export const layer = Layer.effect(HostResources, make());diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.tsindex 56889c1e63b..64bfea1b980 100644--- a/apps/server/src/server.test.ts+++ b/apps/server/src/server.test.ts@@ -165,6 +165,7 @@ import * as PairingGrantStore from "./auth/PairingGrantStore.ts";import * as CloudManagedEndpointRuntime from "./cloud/ManagedEndpointRuntime.ts";import * as CloudCliTokenManager from "./cloud/CliTokenManager.ts";import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts";+import * as HostResources from "./resourceTelemetry/HostResources.ts";import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts";import * as TraceDiagnostics from "./diagnostics/TraceDiagnostics.ts";import * as DesktopTelemetryReceiver from "./resourceTelemetry/DesktopTelemetryReceiver.ts";@@ -845,7 +846,8 @@ const buildAppUnderTest = (options?: {}),}),),- Layer.provide(+ Layer.provide([+ HostResources.layer,Layer.mock(ProcessResourceMonitor.ProcessResourceMonitor)({readHistory: (input) =>Effect.succeed({@@ -860,7 +862,7 @@ const buildAppUnderTest = (options?: {error: Option.none(),}),}),- ),+ ]),Layer.provide(Layer.mock(TraceDiagnostics.TraceDiagnostics)({read: () =>@@ -6154,6 +6156,94 @@ it.layer(NodeServices.layer)("server router seam", (it) => {}).pipe(Effect.provide(NodeHttpServer.layerTest)),);+ it.effect("returns cached whole-host resources over websocket", () =>+ Effect.gen(function* () {+ yield* buildAppUnderTest();+ const wsUrl = yield* getWsServerUrl("/ws");+ const [first, second] = yield* Effect.scoped(+ withWsRpcClient(wsUrl, (client) =>+ Effect.all(+ [+ client[WS_METHODS.serverGetHostResources]({}),+ client[WS_METHODS.serverGetHostResources]({}),+ ],+ { concurrency: "unbounded" },+ ),+ ),+ );+ assert.deepEqual(first, second);+ assert.isAtLeast(first.sampledAt, 0);+ assert.isAbove(first.cpuCount, 0);+ assert.isAbove(first.totalMemoryBytes, 0);+ assert.isAtLeast(first.availableMemoryBytes, 0);+ assert.isAtMost(first.availableMemoryBytes, first.totalMemoryBytes);+ if (first.cpuUtilization !== null) {+ assert.isAtLeast(first.cpuUtilization, 0);+ assert.isAtMost(first.cpuUtilization, 1);+ }+ }).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),+ );++ it.effect("counts macOS reclaimable memory once and shares concurrent samples", () =>+ Effect.gen(function* () {+ const commandCalls = yield* Ref.make(0);+ const hostResources = yield* HostResources.make().pipe(+ Effect.provideService(HostProcessPlatform, "darwin"),+ Effect.provide(+ Layer.mock(ChildProcessSpawner.ChildProcessSpawner)({+ string: () =>+ Ref.update(commandCalls, (count) => count + 1).pipe(+ Effect.as(+ "Mach Virtual Memory Statistics: (page size of 16384 bytes)\n" ++ "Pages free: 10.\nPages inactive: 20.\nPages speculative: 5.\n" ++ "Pages purgeable: 999.\n",+ ),+ ),+ }),+ ),+ );+ const [first, second] = yield* Effect.all([hostResources.read, hostResources.read], {+ concurrency: "unbounded",+ });+ assert.equal(first.availableMemoryBytes, 35 * 16384);+ assert.deepEqual(first, second);+ assert.deepEqual(yield* hostResources.read, first);+ assert.equal(yield* Ref.get(commandCalls), 1);+ }).pipe(TestClock.withLive),+ );++ it.effect("retries host sampling immediately after its caller is interrupted", () =>+ Effect.gen(function* () {+ const started = yield* Deferred.make<void>();+ const commandCalls = yield* Ref.make(0);+ const hostResources = yield* HostResources.make().pipe(+ Effect.provideService(HostProcessPlatform, "darwin"),+ Effect.provide(+ Layer.mock(ChildProcessSpawner.ChildProcessSpawner)({+ string: () =>+ Effect.gen(function* () {+ const call = yield* Ref.updateAndGet(commandCalls, (count) => count + 1);+ if (call === 1) {+ yield* Deferred.succeed(started, undefined);+ return yield* Effect.never;+ }+ return (+ "Mach Virtual Memory Statistics: (page size of 4096 bytes)\n" ++ "Pages free: 10.\nPages inactive: 20.\nPages speculative: 5.\n"+ );+ }),+ }),+ ),+ );+ const firstRead = yield* hostResources.read.pipe(Effect.forkChild);+ yield* Deferred.await(started);+ yield* Fiber.interrupt(firstRead);+ const recovered = yield* hostResources.read;+ assert.equal(recovered.availableMemoryBytes, 35 * 4096);+ assert.equal(yield* Ref.get(commandCalls), 2);+ }).pipe(TestClock.withLive),+ );+it.effect("routes websocket resource telemetry through the subscription", () =>Effect.gen(function* () {yield* buildAppUnderTest();diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex 4abe43d8a63..ce39ee64f51 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -115,6 +115,7 @@ import * as ServerSelfUpdate from "./cloud/selfUpdate.ts";import * as DesktopAppUpdate from "./desktopUpdate/DesktopAppUpdate.ts";import * as ServiceLauncherClient from "./cloud/serviceLauncherClient.ts";import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts";+import * as HostResources from "./resourceTelemetry/HostResources.ts";import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts";import * as TraceDiagnostics from "./diagnostics/TraceDiagnostics.ts";import * as DesktopTelemetryReceiver from "./resourceTelemetry/DesktopTelemetryReceiver.ts";@@ -199,6 +200,7 @@ const BackgroundLayerLive = BackgroundPolicy.layer.pipe(const UsageLayerLive = UsageService.layer.pipe(Layer.provide(ServerSettingsLayerLive));const ResourceDiagnosticsLayerLive = Layer.mergeAll(+ HostResources.layer,ResourceTelemetryLayerLive,ProcessDiagnostics.layer.pipe(Layer.provide(ResourceTelemetryLayerLive)),ProcessResourceMonitor.layer.pipe(Layer.provide(ResourceTelemetryLayerLive)),diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.tsindex fa29d5bd984..b4255c64781 100644--- a/apps/server/src/ws.ts+++ b/apps/server/src/ws.ts@@ -135,6 +135,7 @@ import { requiredScopeForRpcMethod } from "./auth/RpcAuthorization.ts";import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts";import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts";import * as ResourceTelemetry from "./resourceTelemetry/ResourceTelemetry.ts";+import * as HostResources from "./resourceTelemetry/HostResources.ts";import * as AnalyticsService from "./telemetry/AnalyticsService.ts";import * as UsageLimitSources from "./usage/UsageLimitSources.ts";import * as UsageService from "./usage/UsageService.ts";@@ -605,6 +606,7 @@ const makeWsRpcLayer = (const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;const sessions = yield* SessionStore.SessionStore;const processDiagnostics = yield* ProcessDiagnostics.ProcessDiagnostics;+ const hostResources = yield* HostResources.HostResources;const processResourceMonitor = yield* ProcessResourceMonitor.ProcessResourceMonitor;const resourceTelemetry = yield* ResourceTelemetry.ResourceTelemetry;const usage = yield* UsageService.UsageService;@@ -2020,6 +2022,10 @@ const makeWsRpcLayer = (observeRpcEffect(WS_METHODS.serverGetProcessDiagnostics, processDiagnostics.read, {"rpc.aggregate": "server",}),+ [WS_METHODS.serverGetHostResources]: (_input) =>+ observeRpcEffect(WS_METHODS.serverGetHostResources, hostResources.read, {+ "rpc.aggregate": "server",+ }),[WS_METHODS.serverGetProcessResourceHistory]: (input) =>observeRpcEffect(WS_METHODS.serverGetProcessResourceHistory,diff --git a/apps/web/src/components/BranchToolbar.tsx b/apps/web/src/components/BranchToolbar.tsxindex 07407bcf21b..7ad86106ae0 100644--- a/apps/web/src/components/BranchToolbar.tsx+++ b/apps/web/src/components/BranchToolbar.tsx@@ -6,6 +6,7 @@ import {FolderGitIcon,FolderIcon,HistoryIcon,+ ScaleIcon,} from "lucide-react";import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";@@ -56,6 +57,8 @@ interface BranchToolbarProps {onActiveThreadBranchOverrideChange?: (branch: string | null) => void;startFromOrigin: boolean;onStartFromOriginChange: (startFromOrigin: boolean) => void;+ autoEnvironmentLabel?: string | undefined;+ onAutoEnvironment?: (() => void) | undefined;envLocked: boolean;onCheckoutPullRequestRequest?: (reference: string) => void;onComposerFocusRequest?: () => void;@@ -66,6 +69,8 @@ interface BranchToolbarProps {}interface MobileRunContextSelectorProps {+ autoEnvironmentLabel?: string | undefined;+ onAutoEnvironment?: (() => void) | undefined;envLocked: boolean;envModeLocked: boolean;environmentId: EnvironmentId;@@ -81,6 +86,8 @@ interface MobileRunContextSelectorProps {}const MobileRunContextSelector = memo(function MobileRunContextSelector({+ autoEnvironmentLabel,+ onAutoEnvironment,envLocked,envModeLocked,environmentId,@@ -114,10 +121,14 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({// Button's base styles apply `-mx-0.5` to descendant SVGs, which eats 4px// out of whatever gap we set. mx-0! cancels that so gap-0.5 reads as 2px.<span className="inline-flex shrink-0 items-center gap-0.5">- <EnvironmentMachineIcon- kind={activeEnvironment?.machine ?? "server"}- className="size-3 shrink-0 mx-0!"- />+ {autoEnvironmentLabel ? (+ <ScaleIcon className="size-3 shrink-0 mx-0!" aria-hidden="true" />+ ) : (+ <EnvironmentMachineIcon+ kind={activeEnvironment?.machine ?? "server"}+ className="size-3 shrink-0 mx-0!"+ />+ )}<WorkspaceIcon className="size-3 shrink-0 mx-0!" /></span>) : (@@ -134,7 +145,8 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({data-composer-label-motionclassName="block w-full min-w-0 max-w-[240px] origin-left truncate transition-[opacity,transform] duration-180 ease-[cubic-bezier(0.32,0.72,0,1)] group-data-[compact]/composer-context:[transform:translateX(-0.25rem)_scaleX(0.95)] group-data-[compact]/composer-context:opacity-0 motion-reduce:transform-none motion-reduce:transition-opacity">- {showEnvironmentIndicator ? (activeEnvironment?.label ?? "Run on") : workspaceLabel}+ {autoEnvironmentLabel ??+ (showEnvironmentIndicator ? (activeEnvironment?.label ?? "Run on") : workspaceLabel)}</span></span></>@@ -167,9 +179,29 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({<MenuGroup><MenuGroupLabel>Run on</MenuGroupLabel><MenuRadioGroup- value={environmentId}- onValueChange={(value) => onEnvironmentChange(value as EnvironmentId)}+ value={autoEnvironmentLabel ? "auto" : environmentId}+ onValueChange={(value) =>+ value === "auto"+ ? onAutoEnvironment?.()+ : onEnvironmentChange(value as EnvironmentId)+ }>+ {onAutoEnvironment && (+ <MenuRadioItem+ value="auto"+ disabled={envLocked}+ onClick={() => {+ if (autoEnvironmentLabel) onAutoEnvironment?.();+ }}+ >+ <span className="flex min-w-0 items-center gap-1.5">+ <ScaleIcon className="size-3" aria-hidden="true" />+ <span className="min-w-0 truncate">+ {autoEnvironmentLabel ?? "Auto balance"}+ </span>+ </span>+ </MenuRadioItem>+ )}{availableEnvironments.map((env) => (<MenuRadioItemkey={env.environmentId}@@ -421,6 +453,8 @@ export const BranchToolbar = memo(function BranchToolbar({onActiveThreadBranchOverrideChange,startFromOrigin,onStartFromOriginChange,+ autoEnvironmentLabel,+ onAutoEnvironment,envLocked,onCheckoutPullRequestRequest,onComposerFocusRequest,@@ -518,6 +552,8 @@ export const BranchToolbar = memo(function BranchToolbar({{showGitControls ? (<div className="contents @3xl/composer-surface:hidden"><MobileRunContextSelector+ autoEnvironmentLabel={autoEnvironmentLabel}+ onAutoEnvironment={onAutoEnvironment}envLocked={envLocked}envModeLocked={envModeLocked}environmentId={environmentId}@@ -544,6 +580,8 @@ export const BranchToolbar = memo(function BranchToolbar({{showEnvironmentIndicator && availableEnvironments && (<><BranchToolbarEnvironmentSelector+ autoEnvironmentLabel={autoEnvironmentLabel}+ onAutoEnvironment={onAutoEnvironment}envLocked={envLocked}environmentId={environmentId}availableEnvironments={availableEnvironments}diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsxindex f6bdd64756d..c3c59129ca3 100644--- a/apps/web/src/components/BranchToolbarBranchSelector.tsx+++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx@@ -155,7 +155,7 @@ export function BranchToolbarBranchSelector({// Thread branch mutation (colocated — only this component calls it)// ---------------------------------------------------------------------------const setThreadBranch = useCallback(- (branch: string | null, worktreePath: string | null) => {+ (branch: string | null, worktreePath: string | null, automatic = false) => {if (!activeThreadId || !activeProject) return;if (serverSession && worktreePath !== activeWorktreePath) {void stopThreadSession({@@ -186,6 +186,7 @@ export function BranchToolbarBranchSelector({branch,worktreePath,envMode: nextDraftEnvMode,+ environmentSelection: automatic ? (draftThread?.environmentSelection ?? "auto") : "manual",projectRef: scopeProjectRef(environmentId, activeProject.id),});},@@ -201,6 +202,7 @@ export function BranchToolbarBranchSelector({threadRef,environmentId,effectiveEnvMode,+ draftThread?.environmentSelection,stopThreadSession,updateThreadMetadata,],@@ -507,7 +509,7 @@ export function BranchToolbarBranchSelector({) {return;}- setThreadBranch(worktreeBaseBranchCandidate, null);+ setThreadBranch(worktreeBaseBranchCandidate, null, true);}, [activeThreadBranch,activeWorktreePath,diff --git a/apps/web/src/components/BranchToolbarEnvironmentSelector.tsx b/apps/web/src/components/BranchToolbarEnvironmentSelector.tsxindex 431805f2174..863cc7312fe 100644--- a/apps/web/src/components/BranchToolbarEnvironmentSelector.tsx+++ b/apps/web/src/components/BranchToolbarEnvironmentSelector.tsx@@ -1,4 +1,5 @@import type { EnvironmentId } from "@t3tools/contracts";+import { ScaleIcon } from "lucide-react";import { memo, useMemo } from "react";import type { EnvironmentOption } from "./BranchToolbar.logic";@@ -15,6 +16,8 @@ import {} from "./ui/select";interface BranchToolbarEnvironmentSelectorProps {+ autoEnvironmentLabel?: string | undefined;+ onAutoEnvironment?: (() => void) | undefined;envLocked: boolean;environmentId: EnvironmentId;availableEnvironments: readonly EnvironmentOption[];@@ -24,6 +27,8 @@ interface BranchToolbarEnvironmentSelectorProps {}export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector({+ autoEnvironmentLabel,+ onAutoEnvironment,envLocked,environmentId,availableEnvironments,@@ -34,12 +39,16 @@ export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvir}, [availableEnvironments, environmentId]);const environmentItems = useMemo(- () =>- availableEnvironments.map((env) => ({+ () => [+ ...(onAutoEnvironment+ ? [{ value: "auto", label: autoEnvironmentLabel ?? "Auto balance" }]+ : []),+ ...availableEnvironments.map((env) => ({value: env.environmentId,label: env.label,})),- [availableEnvironments],+ ],+ [availableEnvironments, autoEnvironmentLabel, onAutoEnvironment],);// The static label carries the xs control's height (h-7 sm:h-6) as well as@@ -75,8 +84,10 @@ export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvirreturn (<Selectmodal={false}- value={environmentId}- onValueChange={(value) => onEnvironmentChange(value as EnvironmentId)}+ value={autoEnvironmentLabel ? "auto" : environmentId}+ onValueChange={(value) =>+ value === "auto" ? onAutoEnvironment?.() : onEnvironmentChange(value as EnvironmentId)+ }items={environmentItems}><SelectTrigger@@ -86,10 +97,14 @@ export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnviraria-label="Run on"data-composer-context-control>- <EnvironmentMachineIcon- kind={activeEnvironment?.machine ?? "server"}- className="size-3 shrink-0"- />+ {autoEnvironmentLabel ? (+ <ScaleIcon className="size-3 shrink-0" aria-hidden="true" />+ ) : (+ <EnvironmentMachineIcon+ kind={activeEnvironment?.machine ?? "server"}+ className="size-3 shrink-0"+ />+ )}<spandata-composer-labelclassName="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"@@ -105,6 +120,19 @@ export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvir<SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}><SelectGroup><SelectGroupLabel>Run on</SelectGroupLabel>+ {onAutoEnvironment && (+ <SelectItem+ value="auto"+ onClick={() => {+ if (autoEnvironmentLabel) onAutoEnvironment?.();+ }}+ >+ <span className="inline-flex items-center gap-1.5">+ <ScaleIcon className="size-3" aria-hidden="true" />+ {autoEnvironmentLabel ?? "Auto balance"}+ </span>+ </SelectItem>+ )}{availableEnvironments.map((env) => (<SelectItem key={env.environmentId} value={env.environmentId}><span className="inline-flex items-center gap-1.5">diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex d9d66df9cd5..c0c79c93bec 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -1,3 +1,4 @@+import { useLoadBalancedEnvironment } from "../hooks/useLoadBalancedEnvironment";import type { UsageLimitSourceSnapshots } from "@t3tools/contracts";import {collectProviderUsageLimits,@@ -3278,6 +3279,112 @@ export default function ChatView(props: ChatViewProps) {(activeThread.session !== null && activeThread.session.status !== "stopped")),);+ const loadBalancingSettings = useClientSettings();+ const automaticEnvironment = Boolean(+ clientSettingsHydrated &&+ draftId &&+ !envLocked &&+ hasMultipleEnvironments &&+ loadBalancingSettings.loadBalancingEnabled &&+ draftThread?.environmentSelection !== "manual" &&+ (!composerHasAttachments || Boolean(draftThread?.loadBalancedEnvironmentId)) &&+ (!draftThread?.branch || draftThread.environmentSelection === "auto") &&+ !draftThread?.worktreePath,+ );+ const needsLoadBalancing = automaticEnvironment && !draftThread?.loadBalancedEnvironmentId;+ const loadBalancingCandidates = useMemo(+ () =>+ needsLoadBalancing+ ? logicalProjectEnvironments+ .filter((candidate) => {+ const environment = environmentById.get(candidate.environmentId);+ return (+ environment?.connection.phase === "connected" &&+ (loadBalancingSettings.loadBalancingWeights[candidate.environmentId] ?? 50) > 0 &&+ environment.serverConfig?.providers.some(+ (provider) =>+ (activeProviderInstanceId === null ||+ provider.instanceId === activeProviderInstanceId) &&+ provider.driver === selectedProvider &&+ provider.enabled &&+ provider.installed &&+ provider.status !== "error" &&+ provider.auth.status !== "unauthenticated" &&+ provider.availability !== "unavailable",+ )+ );+ })+ .map((candidate) => candidate.environmentId)+ : [],+ [+ needsLoadBalancing,+ logicalProjectEnvironments,+ environmentById,+ loadBalancingSettings.loadBalancingWeights,+ activeProviderInstanceId,+ selectedProvider,+ ],+ );+ const loadBalancing = useLoadBalancedEnvironment(+ loadBalancingCandidates,+ loadBalancingSettings.loadBalancingWeights,+ );+ useEffect(() => {+ if (!needsLoadBalancing || loadBalancing.pending || !draftId || sendInFlightRef.current) return;+ const target = logicalProjectEnvironments.find(+ (environment) => environment.environmentId === loadBalancing.environmentId,+ );+ if (!target) return;+ setDraftThreadContext(draftId, {+ projectRef: scopeProjectRef(target.environmentId, target.projectId),+ environmentSelection: "auto",+ loadBalancedEnvironmentId: target.environmentId,+ });+ }, [+ needsLoadBalancing,+ loadBalancing.pending,+ loadBalancing.environmentId,+ draftId,+ logicalProjectEnvironments,+ setDraftThreadContext,+ ]);+ const onAutoEnvironment = useCallback(() => {+ if (envLocked || !draftId) return;+ if (composerHasAttachments) {+ toastManager.add({+ type: "warning",+ id: "load-balancing-attachments",+ title: "Keep attachments on this machine",+ description:+ "Remove attachments before choosing automatic routing, then attach them on the selected machine.",+ });+ return;+ }+ loadBalancing.refresh(+ logicalProjectEnvironments.map((environment) => environment.environmentId),+ );+ setDraftThreadContext(draftId, {+ environmentSelection: "auto",+ loadBalancedEnvironmentId: null,+ branch: null,+ worktreePath: null,+ });+ }, [+ envLocked,+ draftId,+ setDraftThreadContext,+ loadBalancing.refresh,+ logicalProjectEnvironments,+ composerHasAttachments,+ ]);+ const autoEnvironmentLabel = automaticEnvironment+ ? draftThread?.loadBalancedEnvironmentId+ ? "Auto balance"+ : loadBalancing.pending+ ? "Checking machines…"+ : "Auto balance unavailable"+ : undefined;+// Handle environment change for draft threads. When the user picks a// different environment we update the draft context to point at the physical// project in that environment while keeping the same logical project.@@ -3290,6 +3397,8 @@ export default function ChatView(props: ChatViewProps) {if (!target) return;setDraftThreadContext(draftId, {projectRef: scopeProjectRef(target.environmentId, target.projectId),+ environmentSelection: "manual",+ loadBalancedEnvironmentId: null,});},[draftId, envLocked, logicalProjectEnvironments, setDraftThreadContext],@@ -6249,6 +6358,7 @@ export default function ChatView(props: ChatViewProps) {!activeThread ||isSendBusy ||isConnecting ||+ !clientSettingsHydrated ||threadDetailLoading ||sendInFlightRef.current ||feedbackUploadsInFlightRef.current.has(routeThreadKey)@@ -6256,6 +6366,18 @@ export default function ChatView(props: ChatViewProps) {notifyDirectAnnotationAttached();return;}+ if (needsLoadBalancing) {+ toastManager.add({+ type: "warning",+ title: loadBalancing.pending+ ? "Checking machine resources"+ : "Choose a machine to continue",+ description: loadBalancing.pending+ ? "Resource checks are still running. You can choose a machine in the composer."+ : "No eligible machine has available resources. Choose a machine in the composer to override.",+ });+ return;+ }if (activeEnvironmentUnavailable) {const toastSlot = environmentUnavailableSendToastSlotRef.current;environmentUnavailableSendToastSlotRef.current =@@ -8231,6 +8353,15 @@ export default function ChatView(props: ChatViewProps) {? { onCheckoutPullRequestRequest: openPullRequestDialog }: {})}{...(hasMultipleEnvironments ? { onEnvironmentChange } : {})}+ autoEnvironmentLabel={autoEnvironmentLabel}+ onAutoEnvironment={+ draftId &&+ !envLocked &&+ hasMultipleEnvironments &&+ loadBalancingSettings.loadBalancingEnabled+ ? onAutoEnvironment+ : undefined+ }availableEnvironments={logicalProjectEnvironments}composerControlsHostRef={setRestingComposerControlsHost}contextStripVisible={showComposerContextStrip}diff --git a/apps/web/src/components/GitActionsControl.tsx b/apps/web/src/components/GitActionsControl.tsxindex b7fc811d512..b0e74159da8 100644--- a/apps/web/src/components/GitActionsControl.tsx+++ b/apps/web/src/components/GitActionsControl.tsx@@ -1042,7 +1042,7 @@ export default function GitActionsControl({}, []);const persistThreadBranchSync = useCallback(- (branch: string | null) => {+ (branch: string | null, manualSelection = false) => {if (!activeThreadRef) {return;}@@ -1070,6 +1070,10 @@ export default function GitActionsControl({setDraftThreadContext(draftId ?? activeThreadRef, {branch,worktreePath: activeDraftThread.worktreePath,+ environmentSelection: manualSelection+ ? "manual"+ : (activeDraftThread.environmentSelection ??+ (activeDraftThread.branch ? "manual" : "auto")),});},[@@ -1089,7 +1093,7 @@ export default function GitActionsControl({return;}- persistThreadBranchSync(branchUpdate.branch);+ persistThreadBranchSync(branchUpdate.branch, true);},[persistThreadBranchSync],);diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsxindex da46e067d47..b5de1e12480 100644--- a/apps/web/src/components/settings/ConnectionsSettings.tsx+++ b/apps/web/src/components/settings/ConnectionsSettings.tsx@@ -60,6 +60,7 @@ import {} from "./settingsLayout";import { searchableSetting } from "./settingsSearch";import { EnvironmentIconPicker } from "./EnvironmentIconPicker";+import { LoadBalancingSettings } from "./LoadBalancingSettings";import { Input } from "../ui/input";import { CommandShortcut } from "../ui/command";import {@@ -3592,6 +3593,7 @@ export function ConnectionsSettings() {savedEnvironments={savedEnvironments}/></SettingsSection>+ <LoadBalancingSettings environments={environments} /></SettingsPageContainer>);}diff --git a/apps/web/src/components/settings/LoadBalancingSettings.tsx b/apps/web/src/components/settings/LoadBalancingSettings.tsxnew file mode 100644index 00000000000..514b146c3ba--- /dev/null+++ b/apps/web/src/components/settings/LoadBalancingSettings.tsx@@ -0,0 +1,94 @@+import { connectionStatusText } from "@t3tools/client-runtime/connection";++import {+ useClientSettings,+ useClientSettingsHydrated,+ useUpdateClientSettings,+} from "~/hooks/useSettings";+import type { EnvironmentPresentation } from "~/state/environments";+import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";+import { Switch } from "../ui/switch";+import { SettingsRow, SettingsSection } from "./settingsLayout";+import { searchableSetting } from "./settingsSearch";++const preferences = [+ { value: 100, label: "Prefer" },+ { value: 50, label: "Normal" },+ { value: 25, label: "Less often" },+ { value: 0, label: "Manual only" },+];++export function LoadBalancingSettings({+ environments,+}: {+ environments: ReadonlyArray<EnvironmentPresentation>;+}) {+ const settings = useClientSettings();+ const settingsHydrated = useClientSettingsHydrated();+ const updateSettings = useUpdateClientSettings();++ return (+ <SettingsSection+ {...searchableSetting("load-balancing")}+ description="Choose how often each machine is used. Prefer gives a machine more work when it has capacity; Less often gives it less. Manual only excludes it from automatic selection. Preferences are saved for this client."+ >+ <SettingsRow+ title="Automatically balance load"+ description="Automatically choose a connected machine for new threads in shared projects. You can choose a machine in the composer."+ control={+ <Switch+ aria-label="Automatically balance load"+ checked={settings.loadBalancingEnabled}+ disabled={!settingsHydrated}+ onCheckedChange={(loadBalancingEnabled) => updateSettings({ loadBalancingEnabled })}+ />+ }+ />+ {environments.map((environment) => {+ const weight = settings.loadBalancingWeights[environment.environmentId] ?? 50;+ // Keep saved slider weights until the user chooses a different preference.+ const preference = weight === 0 ? 0 : weight < 50 ? 25 : weight === 50 ? 50 : 100;++ return (+ <SettingsRow+ key={environment.environmentId}+ title={environment.label}+ description={connectionStatusText(environment.connection)}+ control={+ <Select+ items={preferences}+ value={preference}+ disabled={!settingsHydrated || !settings.loadBalancingEnabled}+ onValueChange={(value) => {+ if (value !== null) {+ updateSettings({+ loadBalancingWeights: {+ ...settings.loadBalancingWeights,+ [environment.environmentId]: value,+ },+ });+ }+ }}+ >+ <SelectTrigger+ size="sm"+ className="w-full sm:w-40"+ aria-label={`${environment.label} load preference`}+ >+ <SelectValue />+ </SelectTrigger>+ <SelectPopup align="end" alignItemWithTrigger={false}>+ {preferences.map(({ value, label }) => (+ <SelectItem key={value} value={value}>+ {label}+ </SelectItem>+ ))}+ </SelectPopup>+ </Select>+ }+ />+ );+ })}+ </SettingsSection>+ );+}diff --git a/apps/web/src/components/settings/settingsSearch.ts b/apps/web/src/components/settings/settingsSearch.tsindex f715f6ca4e6..dd4e357eb9d 100644--- a/apps/web/src/components/settings/settingsSearch.ts+++ b/apps/web/src/components/settings/settingsSearch.ts@@ -514,6 +514,14 @@ export const SETTINGS_SEARCH_ITEMS = [to: "/settings/connections",searchTerms: ["add pair backend host code ssh config agent tunnel saved t3 connect"],},+ {+ id: "load-balancing",+ title: "Load balancing",+ to: "/settings/connections",+ searchTerms: [+ "automatic machine environment resources cpu memory capacity preference weight shared projects",+ ],+ },{id: "archive",title: "Archived threads",diff --git a/apps/web/src/composerDraftStore.test.ts b/apps/web/src/composerDraftStore.test.tsindex 53d07aab21d..4a682b97910 100644--- a/apps/web/src/composerDraftStore.test.ts+++ b/apps/web/src/composerDraftStore.test.ts@@ -1623,6 +1623,63 @@ describe("composerDraftStore project draft thread mapping", () => {expect(file && composerFileNeedsReattach(file)).toBe(true);});+ it("rechecks balancing when an empty draft is remapped to another project member", () => {+ const store = useComposerDraftStore.getState();+ store.setProjectDraftThreadId(projectRef, draftId, {+ threadId,+ environmentSelection: "auto",+ loadBalancedEnvironmentId: TEST_ENVIRONMENT_ID,+ });+ store.setProjectDraftThreadId(remoteProjectRef, draftId, { threadId });+ expect(store.getDraftThread(draftId)).toMatchObject({+ environmentSelection: "auto",+ loadBalancedEnvironmentId: null,+ });+ store.setDraftThreadContext(draftId, { loadBalancedEnvironmentId: OTHER_TEST_ENVIRONMENT_ID });+ store.setDraftThreadContext(draftId, { projectRef });+ expect(store.getDraftThread(draftId)).toMatchObject({+ environmentSelection: "auto",+ loadBalancedEnvironmentId: null,+ });+ });++ it("does not opt a legacy branch choice into balancing when runtime mode changes", () => {+ const store = useComposerDraftStore.getState();+ store.setProjectDraftThreadId(projectRef, draftId, { threadId, branch: "feature/pinned" });+ store.setDraftThreadContext(draftId, { runtimeMode: "full-access" });+ expect(store.getDraftThread(draftId)?.environmentSelection).toBeUndefined();+ expect(store.getDraftThread(draftId)?.branch).toBe("feature/pinned");+ });++ it("pins manual workspace choices and can return to automatic routing without losing the prompt", () => {+ const store = useComposerDraftStore.getState();+ store.setProjectDraftThreadId(projectRef, draftId, { threadId });+ store.setPrompt(draftId, "keep this prompt");+ store.setDraftThreadContext(draftId, {+ projectRef: remoteProjectRef,+ environmentSelection: "auto",+ loadBalancedEnvironmentId: OTHER_TEST_ENVIRONMENT_ID,+ });+ expect(store.getDraftThread(draftId)).toMatchObject({+ environmentId: OTHER_TEST_ENVIRONMENT_ID,+ environmentSelection: "auto",+ loadBalancedEnvironmentId: OTHER_TEST_ENVIRONMENT_ID,+ });+ store.setDraftThreadContext(draftId, { branch: "feature/pinned" });+ expect(store.getDraftThread(draftId)?.environmentSelection).toBe("manual");+ store.setDraftThreadContext(draftId, {+ branch: null,+ environmentSelection: "auto",+ loadBalancedEnvironmentId: null,+ });+ expect(store.getDraftThread(draftId)).toMatchObject({+ branch: null,+ environmentSelection: "auto",+ loadBalancedEnvironmentId: null,+ });+ expect(store.getComposerDraft(draftId)?.prompt).toBe("keep this prompt");+ });+it("clears branch and worktree but keeps env mode when changing a draft thread project ref", () => {const store = useComposerDraftStore.getState();store.setProjectDraftThreadId(projectRef, draftId, {diff --git a/apps/web/src/composerDraftStore.ts b/apps/web/src/composerDraftStore.tsindex 9dbc7b9b5c2..5bdd823b4d8 100644--- a/apps/web/src/composerDraftStore.ts+++ b/apps/web/src/composerDraftStore.ts@@ -315,6 +315,8 @@ const PersistedDraftThreadState = Schema.Struct({environmentId: Schema.String,projectId: ProjectId,logicalProjectKey: Schema.optionalKey(Schema.String),+ environmentSelection: Schema.optionalKey(Schema.Literals(["auto", "manual"])),+ loadBalancedEnvironmentId: Schema.optionalKey(Schema.NullOr(Schema.String)),createdAt: Schema.String,runtimeMode: RuntimeMode,interactionMode: ProviderInteractionMode,@@ -427,6 +429,8 @@ export interface DraftSessionState {environmentId: EnvironmentId;projectId: ProjectId;logicalProjectKey: string;+ environmentSelection?: "auto" | "manual";+ loadBalancedEnvironmentId?: EnvironmentId | null;createdAt: string;runtimeMode: RuntimeMode;interactionMode: ProviderInteractionMode;@@ -503,6 +507,8 @@ interface ComposerDraftStoreState {startFromOrigin?: boolean;runtimeMode?: RuntimeMode;interactionMode?: ProviderInteractionMode;+ environmentSelection?: "auto" | "manual";+ loadBalancedEnvironmentId?: EnvironmentId | null;},) => void;/** Creates or updates the draft session tracked for a concrete project ref. */@@ -518,6 +524,8 @@ interface ComposerDraftStoreState {startFromOrigin?: boolean;runtimeMode?: RuntimeMode;interactionMode?: ProviderInteractionMode;+ environmentSelection?: "auto" | "manual";+ loadBalancedEnvironmentId?: EnvironmentId | null;},) => void;/** Updates mutable draft-session metadata without touching composer content. */@@ -532,6 +540,8 @@ interface ComposerDraftStoreState {startFromOrigin?: boolean;runtimeMode?: RuntimeMode;interactionMode?: ProviderInteractionMode;+ environmentSelection?: "auto" | "manual";+ loadBalancedEnvironmentId?: EnvironmentId | null;},) => void;clearProjectDraftThreadId: (projectRef: ScopedProjectRef) => void;@@ -1534,6 +1544,8 @@ function createDraftThreadState(startFromOrigin?: boolean;runtimeMode?: RuntimeMode;interactionMode?: ProviderInteractionMode;+ environmentSelection?: "auto" | "manual";+ loadBalancedEnvironmentId?: EnvironmentId | null;},): DraftThreadState {// A project change (including switching environments within a logical@@ -1560,11 +1572,23 @@ function createDraftThreadState(options?.startFromOrigin === undefined? (existingThread?.startFromOrigin ?? false): options.startFromOrigin;+ const environmentSelection =+ options?.environmentSelection ?? existingThread?.environmentSelection;return {threadId,environmentId: projectRef.environmentId,projectId: projectRef.projectId,logicalProjectKey,+ ...(environmentSelection ? { environmentSelection } : {}),+ ...(options?.loadBalancedEnvironmentId !== undefined+ ? { loadBalancedEnvironmentId: options.loadBalancedEnvironmentId }+ : existingThread?.loadBalancedEnvironmentId !== undefined+ ? {+ loadBalancedEnvironmentId: projectChanged+ ? null+ : existingThread.loadBalancedEnvironmentId,+ }+ : {}),createdAt: options?.createdAt ?? existingThread?.createdAt ?? new Date().toISOString(),runtimeMode: options?.runtimeMode ?? existingThread?.runtimeMode ?? DEFAULT_RUNTIME_MODE,interactionMode:@@ -1599,6 +1623,8 @@ function draftThreadsEqual(left: DraftThreadState | undefined, right: DraftThrealeft.environmentId === right.environmentId &&left.projectId === right.projectId &&left.logicalProjectKey === right.logicalProjectKey &&+ left.environmentSelection === right.environmentSelection &&+ left.loadBalancedEnvironmentId === right.loadBalancedEnvironmentId &&left.createdAt === right.createdAt &&left.runtimeMode === right.runtimeMode &&left.interactionMode === right.interactionMode &&@@ -1754,6 +1780,16 @@ function normalizePersistedDraftThreads(worktreePath: normalizedWorktreePath,envMode: normalizeDraftThreadEnvMode(candidateDraftThread.envMode, normalizedWorktreePath),startFromOrigin,+ ...(candidateDraftThread.environmentSelection === "manual" ||+ candidateDraftThread.environmentSelection === "auto"+ ? { environmentSelection: candidateDraftThread.environmentSelection }+ : {}),+ ...(typeof candidateDraftThread.loadBalancedEnvironmentId === "string" &&+ candidateDraftThread.loadBalancedEnvironmentId.length > 0+ ? { loadBalancedEnvironmentId: candidateDraftThread.loadBalancedEnvironmentId }+ : candidateDraftThread.loadBalancedEnvironmentId === null+ ? { loadBalancedEnvironmentId: null }+ : {}),promotedTo,};}@@ -2453,6 +2489,15 @@ function toHydratedDraftThreadState(worktreePath: persistedDraftThread.worktreePath,envMode: persistedDraftThread.envMode,startFromOrigin: persistedDraftThread.startFromOrigin,+ ...(persistedDraftThread.environmentSelection+ ? { environmentSelection: persistedDraftThread.environmentSelection }+ : {}),+ ...(persistedDraftThread.loadBalancedEnvironmentId !== undefined+ ? {+ loadBalancedEnvironmentId:+ persistedDraftThread.loadBalancedEnvironmentId as EnvironmentId | null,+ }+ : {}),promotedTo: persistedDraftThread.promotedTo? scopeThreadRef(persistedDraftThread.promotedTo.environmentId as EnvironmentId,@@ -2707,11 +2752,23 @@ const composerDraftStore = create<ComposerDraftStoreState>()(options.startFromOrigin === undefined? existing.startFromOrigin: options.startFromOrigin;+ const environmentSelection =+ options.environmentSelection ??+ (options.branch != null || options.worktreePath != null+ ? "manual"+ : existing.environmentSelection);const nextDraftThread: DraftThreadState = {threadId: existing.threadId,environmentId: nextProjectRef.environmentId,projectId: nextProjectRef.projectId,logicalProjectKey: existing.logicalProjectKey,+ ...(environmentSelection ? { environmentSelection } : {}),+ loadBalancedEnvironmentId:+ options.loadBalancedEnvironmentId === undefined+ ? projectChanged+ ? null+ : (existing.loadBalancedEnvironmentId ?? null)+ : options.loadBalancedEnvironmentId,createdAt:options.createdAt === undefined? existing.createdAt@@ -2729,6 +2786,8 @@ const composerDraftStore = create<ComposerDraftStoreState>()(nextDraftThread.environmentId === existing.environmentId &&nextDraftThread.projectId === existing.projectId &&nextDraftThread.logicalProjectKey === existing.logicalProjectKey &&+ nextDraftThread.environmentSelection === existing.environmentSelection &&+ nextDraftThread.loadBalancedEnvironmentId === existing.loadBalancedEnvironmentId &&nextDraftThread.createdAt === existing.createdAt &&nextDraftThread.runtimeMode === existing.runtimeMode &&nextDraftThread.interactionMode === existing.interactionMode &&diff --git a/apps/web/src/hooks/useLoadBalancedEnvironment.ts b/apps/web/src/hooks/useLoadBalancedEnvironment.tsnew file mode 100644index 00000000000..5d3ddfb8b1e--- /dev/null+++ b/apps/web/src/hooks/useLoadBalancedEnvironment.ts@@ -0,0 +1,50 @@+import { RegistryContext, useAtomValue } from "@effect/atom-react";+import { chooseLoadBalancedEnvironment } from "@t3tools/client-runtime/load-balancing";+import type { EnvironmentId } from "@t3tools/contracts";+import { Atom } from "effect/unstable/reactivity";+import { useCallback, useContext, useMemo } from "react";++import { serverEnvironment } from "../state/server";++/** Only mounted for unresolved automatic drafts, so idle clients do not poll hosts. */+export function useLoadBalancedEnvironment(+ environmentIds: readonly EnvironmentId[],+ weights: Readonly<Record<string, number>>,+) {+ const registry = useContext(RegistryContext);+ const refresh = useCallback(+ (ids: readonly EnvironmentId[]) => {+ for (const environmentId of ids) {+ registry.refresh(serverEnvironment.hostResources({ environmentId, input: {} }));+ }+ },+ [registry],+ );+ const resourcesAtom = useMemo(+ () =>+ Atom.make((get) =>+ environmentIds.map((environmentId) => {+ const result = get(serverEnvironment.hostResources({ environmentId, input: {} }));+ return {+ environmentId,+ resources: result._tag === "Success" ? result.value : null,+ receivedAt: result._tag === "Success" ? result.timestamp : 0,+ pending: result._tag === "Initial" || result.waiting,+ };+ }),+ ),+ [environmentIds],+ );+ const resources = useAtomValue(resourcesAtom);+ return {+ refresh,+ pending: resources.some((resource) => resource.pending),+ environmentId: chooseLoadBalancedEnvironment(+ resources.map((resource) => ({+ ...resource,+ weight: weights[resource.environmentId] ?? 50,+ })),+ Date.now(),+ ) as EnvironmentId | null,+ };+}diff --git a/docs/user/remote-access.md b/docs/user/remote-access.mdindex ec2724b4b04..b10123ab622 100644--- a/docs/user/remote-access.md+++ b/docs/user/remote-access.md@@ -61,6 +61,23 @@ created in Settings can only be copied from the client that created them whileits Connections page stays open. If you leave or reload that page, createanother link to share.+### Balance new threads across machines++Auto balance is off by default. On web and desktop, enable it in+**Settings → Connections → Load balancing** to automatically choose a machine for+new threads in projects grouped across connected environments.+Each machine starts at **Normal**. Choose **Prefer** to favor it when it has CPU and+memory available, **Less often** to reduce its share, or **Manual only** to exclude+it from automatic selection. These are preferences, not fixed traffic percentages.+Preferences are saved separately in each client.++The composer checks eligible machines when choosing a draft's environment, then keeps+that choice stable. Choose **Auto balance** again to check current resources, or choose+a specific machine to override it. Choosing a branch or worktree also keeps the draft+on that machine. Existing threads stay where they started. If resource checks are+unavailable or all eligible machines are full, choose a machine manually to continue.+Mobile keeps its manual environment selection.+### Tailscale HTTPSJoin both devices to the same tailnet. In the desktop app, enable **Tailscalediff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.jsonindex 7409a194a2f..775a4a898f8 100644--- a/packages/client-runtime/package.json+++ b/packages/client-runtime/package.json@@ -3,6 +3,10 @@"private": true,"type": "module","exports": {+ "./load-balancing": {+ "types": "./src/load-balancing.ts",+ "default": "./src/load-balancing.ts"+ },"./project-favicon-cache": {"types": "./src/projectFaviconCache.ts","default": "./src/projectFaviconCache.ts"diff --git a/packages/client-runtime/src/load-balancing.ts b/packages/client-runtime/src/load-balancing.tsnew file mode 100644index 00000000000..0b938c6c090--- /dev/null+++ b/packages/client-runtime/src/load-balancing.ts@@ -0,0 +1,40 @@+import type { HostResourcesSnapshot } from "@t3tools/contracts";++/** Callers supply only connected machines hosting the project and selected provider. */+export function chooseLoadBalancedEnvironment(+ candidates: ReadonlyArray<{+ environmentId: string;+ resources: HostResourcesSnapshot | null;+ /** Client receipt time avoids comparing clocks on different machines. */+ receivedAt?: number;+ weight: number;+ }>,+ now: number,+): string | null {+ let selected: string | null = null;+ let bestScore = 0;+ for (const { environmentId, resources, receivedAt, weight } of candidates) {+ const sampledAt = receivedAt ?? resources?.sampledAt ?? 0;+ if (+ !resources ||+ !Number.isFinite(weight) ||+ weight <= 0 ||+ now - sampledAt > 15_000 ||+ sampledAt > now + 5_000 ||+ resources.cpuUtilization === null ||+ resources.cpuUtilization >= 0.95 ||+ resources.totalMemoryBytes <= 0 ||+ resources.cpuCount <= 0+ ) {+ continue;+ }+ const memoryAvailable = resources.availableMemoryBytes / resources.totalMemoryBytes;+ if (memoryAvailable <= 0.05) continue;+ const score = weight * resources.cpuCount * (1 - resources.cpuUtilization) * memoryAvailable;+ if (score > bestScore) {+ selected = environmentId;+ bestScore = score;+ }+ }+ return selected;+}diff --git a/packages/client-runtime/src/state/projectGrouping.test.ts b/packages/client-runtime/src/state/projectGrouping.test.tsindex 94d213b257b..4884c3b99bb 100644--- a/packages/client-runtime/src/state/projectGrouping.test.ts+++ b/packages/client-runtime/src/state/projectGrouping.test.ts@@ -2,6 +2,7 @@ import { EnvironmentId, ProjectId } from "@t3tools/contracts";import { describe, expect, it } from "vite-plus/test";import type { EnvironmentProject } from "./models.ts";+import { chooseLoadBalancedEnvironment } from "../load-balancing.ts";import {buildProjectGroups,derivePhysicalProjectKey,@@ -9,6 +10,70 @@ import {} from "./projectGrouping.ts";const environmentId = EnvironmentId.make("environment");++describe("load balancing shared project machines", () => {+ const now = 100_000;+ const resources = {+ sampledAt: now,+ cpuUtilization: 0.2,+ cpuCount: 8,+ availableMemoryBytes: 8_000,+ totalMemoryBytes: 16_000,+ };++ it("compares three machines using free capacity and preference", () => {+ const candidates = [+ { environmentId: "busy", resources: { ...resources, cpuUtilization: 0.9 }, weight: 1 },+ { environmentId: "idle", resources, weight: 1 },+ { environmentId: "preferred", resources: { ...resources, cpuCount: 4 }, weight: 3 },+ ];+ expect(chooseLoadBalancedEnvironment(candidates, now)).toBe("preferred");+ expect(chooseLoadBalancedEnvironment(candidates.slice(0, 2), now)).toBe("idle");+ });++ it("rejects stale, unknown, excluded and saturated machines", () => {+ expect(+ chooseLoadBalancedEnvironment(+ [+ {+ environmentId: "stale",+ resources: { ...resources, sampledAt: now - 15_001 },+ weight: 1,+ },+ { environmentId: "unknown", resources: null, weight: 1 },+ {+ environmentId: "no-cpu-sample",+ resources: { ...resources, cpuUtilization: null },+ weight: 1,+ },+ { environmentId: "excluded", resources, weight: 0 },+ {+ environmentId: "cpu-full",+ resources: { ...resources, cpuUtilization: 0.95 },+ weight: 1,+ },+ {+ environmentId: "memory-full",+ resources: { ...resources, availableMemoryBytes: 100 },+ weight: 1,+ },+ ],+ now,+ ),+ ).toBeNull();+ });++ it("uses client receipt time when host clocks differ", () => {+ const candidate = {+ environmentId: "different-clock",+ resources: { ...resources, sampledAt: now + 60_000 },+ receivedAt: now,+ weight: 1,+ };+ expect(chooseLoadBalancedEnvironment([candidate], now)).toBe("different-clock");+ expect(chooseLoadBalancedEnvironment([candidate], now + 15_001)).toBeNull();+ });+});const repositoryIdentity = {canonicalKey: "github.com/t3tools/t3code",locator: {diff --git a/packages/client-runtime/src/state/server.ts b/packages/client-runtime/src/state/server.tsindex 5df29a8629d..911ee1bd85c 100644--- a/packages/client-runtime/src/state/server.ts+++ b/packages/client-runtime/src/state/server.ts@@ -26,6 +26,7 @@ import { AsyncResult, Atom } from "effect/unstable/reactivity";import {createAtomCommandScheduler,createEnvironmentRpcCommand,+ createEnvironmentQueryAtomFamily,createEnvironmentRpcQueryAtomFamily,createEnvironmentRpcSubscriptionAtomFamily,createRuntimeCommand,@@ -1018,6 +1019,12 @@ export function createServerEnvironmentAtoms<R, E>(label: "environment-data:server:process-diagnostics",tag: WS_METHODS.serverGetProcessDiagnostics,}),+ hostResources: createEnvironmentQueryAtomFamily(runtime, {+ label: "environment-data:server:host-resources",+ staleTimeMs: 5_000,+ execute: (input: EnvironmentRpcInput<typeof WS_METHODS.serverGetHostResources>) =>+ request(WS_METHODS.serverGetHostResources, input).pipe(Effect.timeout("5 seconds")),+ }),processResourceHistory: createEnvironmentRpcQueryAtomFamily(runtime, {label: "environment-data:server:process-resource-history",tag: WS_METHODS.serverGetProcessResourceHistory,diff --git a/packages/contracts/src/resourceTelemetry.ts b/packages/contracts/src/resourceTelemetry.tsindex 3ec1e4de3ef..87b52993a56 100644--- a/packages/contracts/src/resourceTelemetry.ts+++ b/packages/contracts/src/resourceTelemetry.ts@@ -6,6 +6,16 @@ import { DesktopUpdateStateSchema } from "./ipc.ts";export const RESOURCE_MONITOR_PROTOCOL_VERSION = 2 as const;+/** Whole-host capacity, independent of T3's process diagnostics. */+export const HostResourcesSnapshot = Schema.Struct({+ sampledAt: NonNegativeInt,+ cpuUtilization: Schema.NullOr(Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }))),+ cpuCount: NonNegativeInt,+ availableMemoryBytes: NonNegativeInt,+ totalMemoryBytes: NonNegativeInt,+});+export type HostResourcesSnapshot = typeof HostResourcesSnapshot.Type;+export const ResourceTelemetryIoSemantics = Schema.Literals(["storage","logical",diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.tsindex 12c653f70cc..fb077193c20 100644--- a/packages/contracts/src/rpc.ts+++ b/packages/contracts/src/rpc.ts@@ -210,6 +210,7 @@ import {ServerUpsertKeybindingResult,} from "./server.ts";import {+ HostResourcesSnapshot,ResourceTelemetryHistory,ResourceTelemetryHistoryInput,ResourceTelemetryRetryResult,@@ -323,6 +324,7 @@ export const WS_METHODS = {serverDiscoverSourceControl: "server.discoverSourceControl",serverGetTraceDiagnostics: "server.getTraceDiagnostics",serverGetProcessDiagnostics: "server.getProcessDiagnostics",+ serverGetHostResources: "server.getHostResources",serverGetProcessResourceHistory: "server.getProcessResourceHistory",serverGetResourceTelemetryHistory: "server.getResourceTelemetryHistory",serverRetryResourceTelemetry: "server.retryResourceTelemetry",@@ -538,6 +540,12 @@ const WsServerGetProcessDiagnosticsRpc = Rpc.make(WS_METHODS.serverGetProcessDiaerror: EnvironmentAuthorizationError,});+const WsServerGetHostResourcesRpc = Rpc.make(WS_METHODS.serverGetHostResources, {+ payload: Schema.Struct({}),+ success: HostResourcesSnapshot,+ error: EnvironmentAuthorizationError,+});+const WsServerGetProcessResourceHistoryRpc = Rpc.make(WS_METHODS.serverGetProcessResourceHistory, {payload: ServerProcessResourceHistoryInput,success: ServerProcessResourceHistoryResult,@@ -1197,6 +1205,7 @@ export const WsRpcGroup = RpcGroup.make(WsServerDiscoverSourceControlRpc,WsServerGetTraceDiagnosticsRpc,WsServerGetProcessDiagnosticsRpc,+ WsServerGetHostResourcesRpc,WsServerGetProcessResourceHistoryRpc,WsServerGetResourceTelemetryHistoryRpc,WsServerRetryResourceTelemetryRpc,diff --git a/packages/contracts/src/settings.test.ts b/packages/contracts/src/settings.test.tsindex 1673f4ad159..7c497bacfa8 100644--- a/packages/contracts/src/settings.test.ts+++ b/packages/contracts/src/settings.test.ts@@ -135,6 +135,21 @@ describe("ClaudeSettings auto-compaction", () => {});});+describe("ClientSettings load balancing", () => {+ it("requires opt-in when settings are new or omit load balancing", () => {+ expect(decodeClientSettings({}).loadBalancingEnabled).toBe(false);+ expect(decodeClientSettings({ loadBalancingWeights: {} }).loadBalancingEnabled).toBe(false);+ });++ it.each([true, false])("preserves a saved choice of %s", (loadBalancingEnabled) => {+ const settings = decodeClientSettings({ loadBalancingEnabled });+ expect(encodeClientSettings(settings).loadBalancingEnabled).toBe(loadBalancingEnabled);+ expect(decodeClientSettingsPatch({ loadBalancingEnabled }).loadBalancingEnabled).toBe(+ loadBalancingEnabled,+ );+ });+});+describe("ClientSettings word wrap", () => {it("defaults word wrap on", () => {expect(decodeClientSettings({}).wordWrap).toBe(true);diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.tsindex 983e17b5437..1ddf66cde63 100644--- a/packages/contracts/src/settings.ts+++ b/packages/contracts/src/settings.ts@@ -204,7 +204,14 @@ export const BrowserLinkTarget = Schema.Literals(["system", "app"]);export type BrowserLinkTarget = typeof BrowserLinkTarget.Type;export const DEFAULT_BROWSER_LINK_TARGET: BrowserLinkTarget = "system";+export const LoadBalancingWeights = Schema.Record(+ TrimmedNonEmptyString,+ Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 100 })),+);+export const ClientSettingsSchema = Schema.Struct({+ loadBalancingEnabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),+ loadBalancingWeights: LoadBalancingWeights.pipe(Schema.withDecodingDefault(Effect.succeed({}))),appearanceContrast: AppearanceContrast.pipe(Schema.withDecodingDefault(Effect.succeed(DEFAULT_APPEARANCE_CONTRAST)),),@@ -1210,6 +1217,8 @@ export const ServerSettingsPatch = Schema.Struct({export type ServerSettingsPatch = typeof ServerSettingsPatch.Type;export const ClientSettingsPatch = Schema.Struct({+ loadBalancingEnabled: Schema.optionalKey(Schema.Boolean),+ loadBalancingWeights: Schema.optionalKey(LoadBalancingWeights),appearanceContrast: Schema.optionalKey(AppearanceContrast),panelAnimationDurationMs: Schema.optionalKey(PanelAnimationDurationMs),browserDefaultViewport: Schema.optionalKey(PreviewViewportSetting),