@devries-cameron20 / t3code Mirror · read-only / commits / 9a067b2dfa
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 09:15 UTC

perf(server): skip unchanged device host refreshes (#19961)

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

Julius Marminge committed Oct 15, 2026 at 15:59 UTC 9a067b2dfa4a07d06c4589b746e50f9f398e35ef
2 files changed +120 -2
apps/server/src/device/DeviceService.test.ts
+115
@@ -6,17 +6,28 @@ import {
6 LOCAL_DEVICE_HOST_ID,
7 ThreadId,
8 type DeviceServiceState,
9 + type ServerSettings as Settings,
10 + type SshDeviceHostConfig,
11 } from "@t3tools/contracts";
12 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
13 +import * as NodeServices from "@effect/platform-node/NodeServices";
14 +import * as NetService from "@t3tools/shared/Net";
15 import * as Effect from "effect/Effect";
16 import * as Exit from "effect/Exit";
17 import * as Deferred from "effect/Deferred";
18 import * as Fiber from "effect/Fiber";
19 +import * as FileSystem from "effect/FileSystem";
20 +import * as Layer from "effect/Layer";
21 import * as PubSub from "effect/PubSub";
22 +import * as Queue from "effect/Queue";
23 import * as Ref from "effect/Ref";
24 import * as Schema from "effect/Schema";
25 +import * as Sink from "effect/Sink";
26 import * as Stream from "effect/Stream";
27 +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
28 import { HttpClient, HttpClientResponse } from "effect/http";
29 +import * as ServerConfig from "../config.ts";
30 +import * as ProcessRunner from "../processRunner.ts";
31 import * as ServerSettings from "../serverSettings.ts";
32 import * as DeviceHost from "./DeviceHost.ts";
33 import { NodeRuntimeUnavailableError } from "@t3tools/shared/nodeRuntime";
@@ -773,3 +784,107 @@ it.effect("failed manual installation leaves lifecycle state unchanged and can b
784 expect(agentStarts).toEqual([]);
785 }).pipe(Effect.scoped),
786 );
787 +
788 +it.effect("reconciles SSH host settings without publishing no-op changes", () =>
789 + Effect.gen(function* () {
790 + const fs = yield* FileSystem.FileSystem;
791 + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-reconcile-" });
792 + const settings = yield* ServerSettings.ServerSettingsService;
793 + // Each change is acknowledged after reconciliation consumed it, so no-ops are observable.
794 + const changes = yield* Queue.unbounded<{ value: Settings; done: Deferred.Deferred<void> }>();
795 + const removedFiles: string[] = [];
796 + const localHost: DeviceHost.DeviceHost["Service"] = {
797 + id: LOCAL_DEVICE_HOST_ID,
798 + summary: Effect.succeed({
799 + id: LOCAL_DEVICE_HOST_ID,
800 + label: "Local",
801 + kind: "local",
802 + platforms: [],
803 + hubInstalled: false,
804 + agentDeviceInstalled: false,
805 + }),
806 + platformAvailability: (platform) => Effect.succeed({ platform, available: false }),
807 + current: Effect.succeed(null),
808 + ensureReady: () => Effect.die("Unexpected device launch"),
809 + ensureAgentReady: () => Effect.die("Unexpected agent launch"),
810 + stop: Effect.void,
811 + stopAgent: Effect.void,
812 + };
813 + const service = yield* DeviceService.make.pipe(
814 + Effect.provideService(DeviceHost.DeviceHost, localHost),
815 + Effect.provideService(ServerSettings.ServerSettingsService, {
816 + ...settings,
817 + subscribeChanges: Effect.succeed(
818 + Stream.fromQueue(changes).pipe(
819 + Stream.flatMap(({ value, done }) =>
820 + Stream.make(value).pipe(
821 + Stream.concat(
822 + Stream.fromEffect(Deferred.succeed(done, undefined)).pipe(Stream.drain),
823 + ),
824 + ),
825 + ),
826 + ),
827 + ),
828 + }),
829 + Effect.provideService(
830 + FileSystem.FileSystem,
831 + FileSystem.makeNoop({
832 + remove: (file) => Effect.sync(() => void removedFiles.push(file)),
833 + }),
834 + ),
835 + Effect.provideService(ProcessRunner.ProcessRunner, {
836 + run: () => Effect.die("Unexpected helper launch"),
837 + }),
838 + Effect.provideService(
839 + HttpClient.HttpClient,
840 + HttpClient.make(() => Effect.die("Unexpected HTTP request")),
841 + ),
842 + // `ssh -G` resolves the target to a non-local address.
843 + Effect.provideService(
844 + ChildProcessSpawner.ChildProcessSpawner,
845 + ChildProcessSpawner.make(() =>
846 + Effect.succeed(
847 + ChildProcessSpawner.makeHandle({
848 + pid: ChildProcessSpawner.ProcessId(123),
849 + stdout: Stream.make(new TextEncoder().encode("hostname 192.0.2.1\nport 22\n")),
850 + stderr: Stream.empty,
851 + all: Stream.empty,
852 + exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
853 + isRunning: Effect.succeed(false),
854 + kill: () => Effect.void,
855 + stdin: Sink.drain,
856 + getInputFd: () => Sink.drain,
857 + getOutputFd: () => Stream.empty,
858 + unref: Effect.succeed(Effect.void),
859 + }),
860 + ),
861 + ),
862 + ),
863 + Effect.provide(Layer.mergeAll(ServerConfig.layerTest(baseDir, baseDir), NetService.layer)),
864 + );
865 + const update = Effect.fnUntraced(function* (deviceHosts: ReadonlyArray<SshDeviceHostConfig>) {
866 + const value = yield* settings.updateSettings({ deviceHosts: [...deviceHosts] });
867 + const done = yield* Deferred.make<void>();
868 + yield* Queue.offer(changes, { value, done });
869 + yield* Deferred.await(done);
870 + return yield* service.state;
871 + });
872 +
873 + const initial = yield* service.state;
874 + expect(yield* update([])).toEqual(initial);
875 +
876 + const host: SshDeviceHostConfig = { id: "remote", label: "Remote", target: "remote-fixture" };
877 + const added = yield* update([host]);
878 + expect(added.hosts.map((entry) => entry.id)).toEqual([LOCAL_DEVICE_HOST_ID, host.id]);
879 + expect(yield* update([{ ...host }])).toEqual(added);
880 +
881 + yield* service.setHostStatus(host.id, { status: "failed", detail: "stale" });
882 + const removed = yield* update([]);
883 + expect(removed.hosts.map((entry) => entry.id)).toEqual([LOCAL_DEVICE_HOST_ID]);
884 + expect(removed.hostStatuses[host.id]).toBeUndefined();
885 + expect(removedFiles).toHaveLength(1);
886 + }).pipe(
887 + Effect.scoped,
888 + Effect.provide(Layer.mergeAll(ServerSettings.layerTest(), NodeServices.layer)),
889 + ),
890 +);
apps/server/src/device/DeviceService.ts
+5 -2
@@ -1089,7 +1089,7 @@ export const make = Effect.gen(function* () {
1089 configured.delete(id);
1090 removed.push({ id, scope: previous.scope });
1091 }
1092 - yield* service.refreshHosts;
1092 + if (removed.length > 0) yield* service.refreshHosts;
1093 return removed;
1094 }),
1095 );
@@ -1105,8 +1105,10 @@ export const make = Effect.gen(function* () {
1105 );
1106 yield* service.withLifecycleLock(
1107 Effect.gen(function* () {
1108 + let added = false;
1109 for (const host of next) {
1110 if (configured.has(host.id)) continue;
1111 + added = true;
1112 const hostScope = yield* Scope.fork(scope);
1113 const instance = yield* SshDeviceHost.make(
1114 host,
@@ -1130,7 +1132,8 @@ export const make = Effect.gen(function* () {
1132 hosts.set(host.id, instance);
1133 configured.set(host.id, { config: host, scope: hostScope });
1134 }
1133 - yield* service.refreshHosts;
1135 + // Refresh again after removed scopes closed so a late status write from an old host cannot linger.
1136 + if (added || removed.length > 0) yield* service.refreshHosts;
1137 }),
1138 );
1139 });