sailfork / t3code-github / packages/ client-runtime/ src/ connection/ outdatedHostUpdate.ts

import {
  ORCHESTRATION_PROTOCOL_VERSION,
  type EnvironmentId,
  type ExecutionEnvironmentDescriptor,
  type ServerSelfUpdateInput,
  type ServerSelfUpdateResult,
  WS_METHODS,
} from "@t3tools/contracts";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";
import * as Schedule from "effect/Schedule";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import * as SubscriptionRef from "effect/SubscriptionRef";
import * as HttpClient from "effect/http/HttpClient";
import * as RpcClient from "effect/rpc/RpcClient";
import * as RpcSerialization from "effect/rpc/RpcSerialization";
import * as Socket from "effect/socket/Socket";

import { fetchRemoteEnvironmentDescriptor } from "../environment/descriptor.ts";
import { makeWsRpcProtocolClient, PING_TIMEOUT } from "../rpc/protocol.ts";
import { isLegacyUpdateHandoffLoss, resolveServerUpdateProgressResult } from "../state/server.ts";
import * as RelayEnvironmentDiscovery from "../relay/discovery.ts";
import * as ConnectionResolver from "./resolver.ts";
import * as EnvironmentRegistry from "./registry.ts";
import {
  connectionRoutes,
  hasRelayRoute,
  routeEntry,
  routeHttpBaseUrl,
  usableRoutes,
} from "./routes.ts";

// A v1 host restarting into v2 runs migrations before its descriptor answers again.
const OUTDATED_HOST_RESTART_TIMEOUT = Duration.minutes(4);
const SOCKET_OPEN_TIMEOUT = "15 seconds";

export class OutdatedHostUpdateError extends Schema.TaggedError<OutdatedHostUpdateError>()(
  "OutdatedHostUpdateError",
  {
    environmentId: Schema.String,
    message: Schema.String,
  },
) {}

export type OutdatedHostUpdateStage = "downloading" | "installing" | "resuming";

const reachOutdatedHost = Effect.fn("clientRuntime.connection.reachOutdatedHost")(function* (
  environmentId: EnvironmentId,
) {
  const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
  const resolver = yield* ConnectionResolver.ConnectionResolver;
  const entry = (yield* SubscriptionRef.get(registry.entries)).get(environmentId);
  if (entry === undefined) {
    return yield* new EnvironmentRegistry.EnvironmentNotRegisteredError({ environmentId });
  }
  const { prepared, descriptor } = yield* Effect.firstSuccessOf(
    usableRoutes(entry).map((route) => resolver.prepareForUpdate(routeEntry(entry, route))),
  );
  return { entry, prepared, descriptor };
});

// The normal session cannot read config from a host behind the protocol gate.
export const describeOutdatedHost = (environmentId: EnvironmentId) =>
  reachOutdatedHost(environmentId).pipe(Effect.map(({ descriptor }) => descriptor));

/**
 * The normal session refuses to open against such a host, so this opens a bare
 * socket and calls only the self-update RPCs, whose wire shape has not changed
 * across protocol versions.
 */
export const updateOutdatedHost = Effect.fn("clientRuntime.connection.updateOutdatedHost")(
  function* (
    environmentId: EnvironmentId,
    input: ServerSelfUpdateInput,
    onStage: (stage: OutdatedHostUpdateStage) => Effect.Effect<void>,
  ) {
    const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
    const webSocketConstructor = yield* Socket.WebSocketConstructor;
    const httpClient = yield* HttpClient.HttpClient;
    const { entry, prepared, descriptor } = yield* reachOutdatedHost(environmentId);
    // Discovery still holds the old relay descriptor and would re-block the
    // environment from it, so replace that before clearing the block.
    const switchBackOn = Effect.gen(function* () {
      if (hasRelayRoute(entry)) {
        const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
        yield* discovery.refresh;
      }
      yield* registry.setCompatibility(environmentId, null);
      yield* registry.setEnabled(environmentId, true);
    });

    // A host updated by hand is already compatible, and a bare socket would be
    // rejected with 426, so there is nothing to update.
    if (isCompatibleDescriptor(descriptor)) {
      yield* switchBackOn;
      return { targetVersion: descriptor.serverVersion };
    }

    const capabilities = descriptor.capabilities;
    if (
      capabilities.serverSelfUpdate === undefined ||
      (capabilities.serverSelfUpdate === "desktop-managed" &&
        capabilities.desktopAppUpdate !== true)
    ) {
      return yield* new OutdatedHostUpdateError({
        environmentId,
        message: `Update T3 Code on ${descriptor.label} manually; it cannot update itself.`,
      });
    }

    const result = yield* Effect.scoped(
      Effect.gen(function* () {
        const protocolContext = yield* Layer.build(
          Layer.effect(
            RpcClient.Protocol,
            RpcClient.makeProtocolSocket({
              pingTimeout: PING_TIMEOUT,
              retryTransientErrors: false,
              retryPolicy: Schedule.recurs(0),
            }),
          ).pipe(
            Layer.provide(
              Layer.mergeAll(
                Socket.layerWebSocket(prepared.socketUrl, {
                  openTimeout: SOCKET_OPEN_TIMEOUT,
                }).pipe(
                  Layer.provide(Layer.succeed(Socket.WebSocketConstructor, webSocketConstructor)),
                ),
                RpcSerialization.layerJson,
              ),
            ),
          ),
        );
        const client = yield* makeWsRpcProtocolClient.pipe(Effect.provide(protocolContext));

        const updateResult: ServerSelfUpdateResult =
          capabilities.serverSelfUpdateProgress === true
            ? yield* Effect.gen(function* () {
                const terminal = yield* Ref.make(Option.none<ServerSelfUpdateResult>());
                let receivedProgress = false;
                const streamExit = yield* client[WS_METHODS.serverUpdateServerWithProgress](
                  input,
                ).pipe(
                  Stream.runForEach((event) =>
                    event.type === "complete"
                      ? Ref.set(terminal, Option.some(event.result))
                      : Effect.sync(() => {
                          receivedProgress = true;
                        }).pipe(Effect.andThen(onStage(event.stage))),
                  ),
                  Effect.exit,
                );
                const selfUpdateMethod = capabilities.serverSelfUpdate;
                // Desktop preparation needs its terminal commit token.
                const accepted =
                  receivedProgress &&
                  (selfUpdateMethod === "boot-service" || selfUpdateMethod === "respawn")
                    ? { targetVersion: input.targetVersion, method: selfUpdateMethod }
                    : undefined;
                return yield* resolveServerUpdateProgressResult(
                  input.targetVersion,
                  yield* Ref.get(terminal),
                  streamExit,
                  accepted,
                );
              })
            : yield* client[WS_METHODS.serverUpdateServer](input).pipe(
                // Older servers can drop the socket before acknowledging the restart.
                Effect.catchCauseIf(
                  (cause) =>
                    (capabilities.serverSelfUpdate === "boot-service" ||
                      capabilities.serverSelfUpdate === "respawn") &&
                    isLegacyUpdateHandoffLoss(cause),
                  () =>
                    Effect.succeed({
                      targetVersion: input.targetVersion,
                      method: capabilities.serverSelfUpdate as "boot-service" | "respawn",
                    } satisfies ServerSelfUpdateResult),
                ),
              );

        if (
          updateResult.method === "desktop-app" &&
          updateResult.desktopUpdateToken !== undefined
        ) {
          // The commit relaunches the desktop app, so a dropped socket is success.
          yield* client[WS_METHODS.serverCommitDesktopUpdate]({
            requestId: updateResult.desktopUpdateToken,
          }).pipe(
            Effect.catchCauseIf(
              (cause) => isLegacyUpdateHandoffLoss(cause),
              () => Effect.void,
            ),
          );
        }
        return updateResult;
      }),
    );

    yield* onStage("resuming");
    const pollUrls = [
      prepared.httpBaseUrl,
      ...connectionRoutes(entry).flatMap((route) => routeHttpBaseUrl(route) ?? []),
    ].filter((url, index, all) => all.indexOf(url) === index);
    const resumed = yield* Effect.firstSuccessOf(
      pollUrls.map((httpBaseUrl) =>
        fetchRemoteEnvironmentDescriptor({ httpBaseUrl }).pipe(
          Effect.filterOrFail(
            (descriptor) =>
              descriptor.environmentId === environmentId && isCompatibleDescriptor(descriptor),
            () => "not-ready" as const,
          ),
        ),
      ),
    ).pipe(
      Effect.provideService(HttpClient.HttpClient, httpClient),
      Effect.option,
      Effect.repeat({
        schedule: Schedule.spaced(Duration.seconds(1)),
        until: (current) => Option.exists(current, isCompatibleDescriptor),
      }),
      Effect.timeoutOption(OUTDATED_HOST_RESTART_TIMEOUT),
      Effect.map(Option.flatten),
    );
    if (Option.isNone(resumed)) {
      return yield* new OutdatedHostUpdateError({
        environmentId,
        message: `${descriptor.label} did not come back on a compatible T3 Code version.`,
      });
    }

    yield* switchBackOn;
    return { ...result, targetVersion: resumed.value.serverVersion };
  },
);

function isCompatibleDescriptor(descriptor: ExecutionEnvironmentDescriptor): boolean {
  return (descriptor.orchestrationProtocolVersion ?? 1) === ORCHESTRATION_PROTOCOL_VERSION;
}