Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

10.6 KB
Raw Blame
import * as NodeCrypto from "node:crypto";

import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import { HostProcessExecutablePath, HostProcessPlatform } from "@akeru/shared/hostProcess";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Path from "effect/Path";
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";

import * as ServerConfig from "../config.ts";
import * as ProcessRunner from "../processRunner.ts";
import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
import * as ServerSelfUpdate from "./selfUpdate.ts";

const ARCHIVE_SHA256 = "a".repeat(64);

const releaseKeys = NodeCrypto.generateKeyPairSync("ed25519");

const releaseManifest = new TextEncoder().encode(
  `${ARCHIVE_SHA256}  Akeru-Remote-1.1.0-win32-x64.zip\n`,
);

const releaseSignature = NodeCrypto.sign(null, releaseManifest, releaseKeys.privateKey);

/** Serves the signed stub release that the Windows update verifies. */
const releaseClient = HttpClient.make((request) =>
  Effect.succeed(
    HttpClientResponse.fromWeb(
      request,
      new Response(request.url.endsWith(".sig") ? releaseSignature : releaseManifest),
    ),
  ),
);

interface HarnessOptions {
  readonly mode?: "web" | "desktop";
  readonly managed?: boolean;
  readonly preflight?: "ready" | "blocked";
  readonly platform?: NodeJS.Platform;
  readonly archiveFailure?: boolean;
  readonly requestUpdate?: ServiceLauncherClient.ServiceLauncherClient["Service"]["requestUpdate"];
}

const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
  options: HarnessOptions = {},
) {
  const fs = yield* FileSystem.FileSystem;
  const path = yield* Path.Path;
  const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-self-update-test-" });
  const order: string[] = [];
  const preflightCommands: string[] = [];

  const runner = ProcessRunner.ProcessRunner.of({
    run: (input) =>
      Effect.gen(function* () {
        if (input.command === "sh" || input.command === "powershell.exe") {
          order.push("archive");
          expect(input.args).toContain("v1.1.0");
          expect(input.args).toContain(input.command === "sh" ? "--prepare-only" : "-PrepareOnly");

          if (input.command === "powershell.exe") {
            expect(input.args[input.args.indexOf("-ExpectedSha256") + 1]).toBe(ARCHIVE_SHA256);
          }

          const installRoot = input.env?.AKERU_INSTALL_ROOT;

          if (installRoot === undefined) return yield* Effect.die("missing archive install root");

          const entry = path.join(
            installRoot,
            "versions",
            "1.1.0",
            "node_modules",
            "akeru-bot",
            "dist",
            "bin.mjs",
          );

          yield* fs.makeDirectory(path.dirname(entry), { recursive: true }).pipe(Effect.orDie);
          yield* fs.writeFileString(entry, "verified archive bytes\n").pipe(Effect.orDie);

          if (options.platform === "win32") {
            const node = path.join(installRoot, "versions", "1.1.0", "node", "node.exe");
            yield* fs.makeDirectory(path.dirname(node), { recursive: true }).pipe(Effect.orDie);
            yield* fs.writeFileString(node, "bundled node\n").pipe(Effect.orDie);
          }

          return {
            stdout: "",
            stderr: "",
            code: ChildProcessSpawner.ExitCode(options.archiveFailure ? 1 : 0),
            timedOut: false,
            stdoutTruncated: false,
            stderrTruncated: false,
            stdoutInvalidUtf8: false,
            stderrInvalidUtf8: false,
          };
        }

        if (input.command === "npm") {
          order.push("install");
          const prefix = input.args[input.args.indexOf("--prefix") + 1];

          if (prefix === undefined) return yield* Effect.die("missing npm prefix");
          const entry = path.join(prefix, "node_modules", "akeru-bot", "dist", "bin.mjs");
          yield* fs.makeDirectory(path.dirname(entry), { recursive: true }).pipe(Effect.orDie);
          yield* fs.writeFileString(entry, "export {};\n").pipe(Effect.orDie);

          return {
            stdout: "",
            stderr: "",
            code: ChildProcessSpawner.ExitCode(0),
            timedOut: false,
            stdoutTruncated: false,
            stderrTruncated: false,
            stdoutInvalidUtf8: false,
            stderrInvalidUtf8: false,
          };
        }

        order.push("preflight");
        preflightCommands.push(input.command);

        const result =
          options.preflight === "blocked"
            ? { status: "blocked", version: "1.1.0", reason: "local update required" }
            : {
                status: "ready",
                version: "1.1.0",
                launcherProtocol: SERVICE_LAUNCHER_PROTOCOL,
              };

        return {
          stdout: JSON.stringify(result),
          stderr: "",
          code: ChildProcessSpawner.ExitCode(0),
          timedOut: false,
          stdoutTruncated: false,
          stderrTruncated: false,
          stdoutInvalidUtf8: false,
          stderrInvalidUtf8: false,
        };
      }),
  });

  const launcher = ServiceLauncherClient.ServiceLauncherClient.of({
    managed: options.managed ?? true,
    requestUpdate:
      options.requestUpdate ??
      (() =>
        Effect.sync(() => {
          order.push("accept");

          return "launcher-id";
        })),
    prepareTrial: Effect.sync((): undefined => undefined),
  });

  const config = yield* ServerConfig.ServerConfig.pipe(
    Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
  );

  const selfUpdate = yield* ServerSelfUpdate.make({
    manifestKey: releaseKeys.publicKey.export({ type: "spki", format: "pem" }).toString(),
  }).pipe(
    Effect.provideService(HttpClient.HttpClient, releaseClient),
    Effect.provideService(ProcessRunner.ProcessRunner, runner),
    Effect.provideService(ServiceLauncherClient.ServiceLauncherClient, launcher),
    Effect.provideService(HostProcessExecutablePath, "/usr/bin/node"),
    Effect.provideService(HostProcessPlatform, options.platform ?? "linux"),
    Effect.provide(ServerConfig.layer({ ...config, mode: options.mode ?? "web" })),
  );

  return { selfUpdate, order, preflightCommands, baseDir };
});

it.layer(NodeServices.layer)("server self update", (it) => {
  for (const platform of ["linux", "win32"] as const) {
    it.effect(`publishes verified archive bytes without npm on ${platform}`, () =>
      Effect.gen(function* () {
        const fs = yield* FileSystem.FileSystem;
        const path = yield* Path.Path;
        const { selfUpdate, order, preflightCommands, baseDir } = yield* makeHarness({ platform });
        const versionDir = path.join(baseDir, "runtime", "versions", "1.1.0");
        const entry = path.join(versionDir, "node_modules", "akeru-bot", "dist", "bin.mjs");
        yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
        yield* fs.writeFileString(entry, "unverified npm bytes\n");
        yield* fs.writeFileString(path.join(versionDir, ".install-complete"), "1.1.0\n");
        yield* selfUpdate.update({ targetVersion: "1.1.0", source: "remote-archive" });
        expect(order).toEqual(["archive", "preflight", "accept"]);
        expect(yield* fs.readFileString(entry)).toBe("verified archive bytes\n");
        expect(yield* fs.exists(path.join(versionDir, ".archive-verified"))).toBe(true);
        // Windows archives preflight on their own Node, the one the launcher will run them on.
        expect(preflightCommands).toHaveLength(1);

        if (platform === "win32") {
          expect(preflightCommands[0]).toMatch(
            /[/\\]runtime[/\\]versions[/\\].+[/\\]node[/\\]node\.exe$/,
          );
        } else {
          expect(preflightCommands[0]).toBe("/usr/bin/node");
        }
      }),
    );
  }

  it.effect("does not activate an archive rejected by the installer", () =>
    Effect.gen(function* () {
      const { selfUpdate, order } = yield* makeHarness({ archiveFailure: true });

      const error = yield* selfUpdate
        .update({ targetVersion: "1.1.0", source: "remote-archive" })
        .pipe(Effect.flip);

      expect(error.reason).toContain("Could not prepare");
      expect(order).toEqual(["archive"]);
    }),
  );
  it.effect("stages and preflights before asking the launcher for an update ID", () =>
    Effect.gen(function* () {
      const { selfUpdate, order } = yield* makeHarness();
      expect(yield* selfUpdate.update({ targetVersion: "1.1.0" })).toEqual({
        targetVersion: "1.1.0",
        method: "boot-service",
        updateId: "launcher-id",
      });
      expect(order).toEqual(["install", "preflight", "accept"]);
    }),
  );

  it.effect("rejects invalid versions and desktop-managed servers before staging", () =>
    Effect.gen(function* () {
      const web = yield* makeHarness();
      expect(
        (yield* web.selfUpdate.update({ targetVersion: "latest" }).pipe(Effect.flip)).reason,
      ).toBe("'latest' is not an exact akeru-bot version.");
      const desktop = yield* makeHarness({ mode: "desktop" });
      expect(
        (yield* desktop.selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason,
      ).toContain("desktop app");
      expect([...web.order, ...desktop.order]).toEqual([]);
    }),
  );

  it.effect("preserves the preflight refusal reason", () =>
    Effect.gen(function* () {
      const { selfUpdate } = yield* makeHarness({ preflight: "blocked" });
      expect((yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason).toBe(
        "local update required",
      );
    }),
  );

  it.effect("allows only one update at a time", () =>
    Effect.gen(function* () {
      const requested = yield* Deferred.make<void>();
      const accepted = yield* Deferred.make<string>();

      const { selfUpdate } = yield* makeHarness({
        requestUpdate: () =>
          Deferred.succeed(requested, undefined).pipe(Effect.andThen(Deferred.await(accepted))),
      });

      const first = yield* Effect.forkChild(selfUpdate.update({ targetVersion: "1.1.0" }), {
        startImmediately: true,
      });

      yield* Deferred.await(requested);
      expect((yield* selfUpdate.update({ targetVersion: "1.1.1" }).pipe(Effect.flip)).reason).toBe(
        "A server update is already in progress.",
      );
      yield* Deferred.succeed(accepted, "launcher-id");
      expect((yield* Fiber.join(first)).updateId).toBe("launcher-id");
    }),
  );
});