Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

4.0 KB
Raw Blame
// Before "Sign in with Boat" replaces, or stops being, the Boat credential in use: the same checks a changed API key
// gets in Settings (server/routes/config.ts). The cloud computers this installation remembers must be reachable with
// what comes next, so none is left running on an account the app can no longer reach.
import * as boat from "./boat.ts";
import { boatCreateRecoverySnapshot, retireDeletedBoatCreate } from "./boat-create-idempotency.ts";
import { boatDeletionSnapshot } from "./boat-delete-journal.ts";
import { boatAccountResourceChangeError } from "./cloud-backend.ts";
import type { AppConfig } from "./config.ts";

type Identity = { boxId: string; name: string };

/** `current` and `next` each name one credential (`{ box: { token } }`); null is none. A refusal in words, or null. */
export async function boatAccountChangeRefusal(
  current: AppConfig | null,
  next: AppConfig | null,
  owners: boat.ManagedBoatOwner[],
): Promise<string | null> {
  if (boatDeletionSnapshot().length > 0) {
    if (!next) return "finish or retry pending cloud computer deletion before removing the Boat account";
    try {
      await boat.verifyBoatDeletionCredential(next);
    } catch (error) {
      return error instanceof Error ? error.message : "a cloud computer being deleted could not be verified";
    }
  }
  const deleting = new Set(boatDeletionSnapshot().map((entry) => entry.boxId));
  const recoveries = boatCreateRecoverySnapshot();

  let kept: Map<string, Identity> | null = null;
  if (current) {
    const inventory = await boat.listManagedBoats(current, owners);
    if (inventory.available) {
      kept = new Map(inventory.instances.map((instance) => [instance.boxId, { boxId: instance.boxId, name: instance.name }]));
      for (const recovery of recoveries) {
        if (!recovery.boxId || deleting.has(recovery.boxId)) continue;
        const inspected = await boat.inspectBoatIdentity(current, recovery.boxId);
        if (!inspected.available) return `${inspected.problem ?? "a remembered cloud computer could not be verified"}. Keep the current Boat account and retry`;
        // A receipt for a computer that is gone is retired while the current credential still proves it.
        if (!inspected.identity) retireDeletedBoatCreate(recovery.boxId);
        else kept.set(inspected.identity.boxId, inspected.identity);
      }
    } else if (!(inventory.credentialRejected === true && next)) {
      // A credential Boat no longer accepts cannot vouch for anything; one that could not be asked must be asked again.
      return `${inventory.problem ?? "cloud computer inventory is unavailable"}. Keep the current Boat account and retry`;
    }
  }

  if (!next) {
    if (kept) return boatAccountResourceChangeError([...kept.values()], null);
    return boatCreateRecoverySnapshot().length > 0 ? "restore the Boat account that owns the remembered cloud computers before clearing it" : null;
  }

  const replacement = await boat.listManagedBoats(next, owners, { adoptLegacy: false });
  if (!replacement.available) return replacement.problem ?? "Could not verify the Boat account. Nothing was changed.";
  const reached = new Map(replacement.instances.map((instance) => [instance.boxId, { boxId: instance.boxId, name: instance.name }]));
  for (const recovery of boatCreateRecoverySnapshot()) {
    if (!recovery.boxId) return "finish reconciling pending cloud computer creation before changing the Boat account";
    if (deleting.has(recovery.boxId)) continue;
    const inspected = await boat.inspectBoatIdentity(next, recovery.boxId);
    if (!inspected.available) return `${inspected.problem ?? "a remembered cloud computer could not be verified"}. Retry with the Boat account that created it`;
    if (!inspected.identity || !(await boat.boatNameMatchesBot(recovery.botId, inspected.identity.name))) {
      return "that Boat account cannot access the remembered cloud computers from this installation";
    }
    reached.set(inspected.identity.boxId, inspected.identity);
  }
  return kept ? boatAccountResourceChangeError([...kept.values()], [...reached.values()]) : null;
}