Files
openbot/scripts/host-manager.ts
T
Aleksander DygońandClaude Opus 5.5 d1ce0ee68f Migrate service runtimes to Effect 4.0.0 (#1283)
* refactor: migrate service runtimes to Effect 4

* refactor: make Effect the canonical service runtime

* fix: preserve typed Effect failures and align CI test boundaries

* test: await Effect test boundaries and lower lint baseline

* fix: handle early provider exit and retain remote send errors

* Simplify Effect composition and failure handling

* Fix review findings in hosted sites, updates and WebRTC requests

- Release a hosted-site idempotency claim on any failure, defects included.
- Keep update downloads running after stop(), as before the migration.
- Clean up a pending WebRTC request when the send is interrupted.
- Compose the account switch in one Effect, without nested runPromise.
- Remove single-effect generators in the WebRTC client transport.
- Put the atomic-write JSDoc on writeFileAtomically; reuse writeJsonFileAtomically.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Run Effects at Promise boundaries through one runCauseEffect

Replace the inline Effect.runPromise(x.pipe(Effect.mapError((error) => error.cause))) calls and
the per-domain runners (runBrowserEffect, runMcpEffect, runArchiveEffect, runRemoteWorkflow,
runDesktopEffect, runProviderClientEffect, runTestEffect) with runCauseEffect in
src/backend/effect-boundary.ts. Behavior is unchanged: expected failures reject with their cause,
and defects and interruption reject as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Mark concurrent test starts as not awaited

runCauseEffect returns a Promise, so Biome now finds the operations that
these tests start and do not wait for. Mark each with void. The start
stays concurrent, and a rejection still fails the run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Send CentralAuth requests through the network-block detector

The merge of main wrapped the options fetch, but CentralAuth sends its
requests through the Effect transport. Build the transport from the
wrapped fetch, and run the new test through Effect.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Run each account API entry point as one Effect

Route handlers, the browser API, the Worker pre-routing and the cron
now run one Effect each. Request body readers, the SMTP socket close
and the remaining R2 and D1 calls are Effects. The skill icon read is
now part of its Effect; before, the Effect returned a Promise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Run the remaining service runtime in Effect

Main: the automation server, Team API server start and stop, duplicate
requests, Dynamic Island, the Computer Use highlight, trace flushes,
update background work, sibling scans, upload slots, server mode, window
bounds, attachment archives and uploads, and remote transport callbacks
are Effects forked into their owner's scope.

Backend: usage limit hooks and the channel requeue are Effects.

Team client: request frames, Signal notices and connection failures run
in the peer's work scope, and recovery loops inside its run.

Site router: one Effect per request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Run the picture-in-picture and permission help windows in Effect

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Remove the unused analytics runner

The trace file forks its flush into its own scope now, so nothing calls
runAnalytics.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep cron jobs, session ends and host starts complete after a failure

- The cron waits for every job before it reports the first failure, so a
  failed delivery does not stop retention or a site cleanup midway.
- A connection failure ends the logical session in an uninterruptible
  fork, so dispose does not stop it.
- A dependency that throws while the Team API listener starts fails the
  start, so the host stops the listener.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 15:06:54 +02:00

94 lines
3.8 KiB
TypeScript

import { chown, lstat, mkdir } from "node:fs/promises";
import { join } from "node:path";
import { runCauseEffect } from "../src/backend/effect-boundary";
import { HostManager } from "../src/main/host-manager";
import {
HOST_MANAGER_DIRECTORY,
HOST_POLL_MS,
hostConfigSchema,
readHostConfig,
writeProtocolJson,
} from "../src/main/host-update-files";
import { relaunchManagedTenant } from "../src/main/host-update-relaunch";
import {
ensureHostDirectory,
macHostOperations,
openSharedApplication,
verifyHostPath,
verifySharedAppParent,
} from "./host-manager-macos";
async function main(): Promise<void> {
if (process.argv[2] === "--help") {
process.stdout.write("OpenBot Host Manager: run through launchd; administrator setup uses openbot-host.\n");
return;
}
if (process.argv[2] === "--runtime-check") {
// Exercise JavaScriptCore's hot loop in the hardened standalone executable without host access.
let sum = 0;
for (let index = 0; index < 1_000_000; index++) sum += index;
if (sum !== 499_999_500_000) throw new Error("Runtime check failed.");
process.stdout.write("Standalone runtime passed.\n");
return;
}
if (process.platform !== "darwin" || process.arch !== "arm64")
throw new Error("Host Manager requires an Apple Silicon Mac.");
const uid = process.getuid?.();
if (process.argv[2] === "--relaunch" && process.argv.length === 3 && uid !== undefined && uid >= 501) {
await runCauseEffect(relaunchManagedTenant(uid, { ...macHostOperations(), open: openSharedApplication }));
return;
}
if (uid !== 0) throw new Error("Host maintenance requires root.");
const directory = HOST_MANAGER_DIRECTORY;
if (process.argv[2] === "--setup") {
const tenants = process.argv.slice(3).map((uid) => (/^\d+$/.test(uid) ? Number(uid) : NaN));
const config = hostConfigSchema.parse({ managed: true, tenants });
await verifySharedAppParent();
await macHostOperations().installedVersion();
await ensureHostDirectory("/Library/Application Support/OpenBot", 0o755);
await ensureHostDirectory(directory, 0o755);
if (await runCauseEffect(readHostConfig()))
throw new Error(
"Host configuration already exists. Stop the daemon before an administrator changes registration.",
);
await ensureHostDirectory(join(directory, "tenants"), 0o755);
for (const uid of config.tenants) {
const path = join(directory, "tenants", String(uid));
// Never repair or follow an existing tenant-controlled path as root.
await mkdir(path, { mode: 0o700 });
await chown(path, uid, 0);
const info = await lstat(path);
if (info.uid !== uid || !info.isDirectory()) throw new Error("Tenant directory setup failed.");
}
await runCauseEffect(
writeProtocolJson(join(directory, "state.json"), {
phase: "idle",
cycle: "",
version: null,
updatedAt: Date.now(),
error: null,
}),
);
await runCauseEffect(writeProtocolJson(join(directory, "config.json"), config));
return;
}
// launchd owns daemon lifetime and serialization. Do not allow a second interactive coordinator.
if (process.ppid !== 1 || process.argv.length !== 2)
throw new Error("Start Host Manager through its system LaunchDaemon.");
await verifyHostPath(directory);
const manager = new HostManager(directory, macHostOperations());
const run = (): void => {
void runCauseEffect(manager.tick()).catch(() => {
process.stderr.write("Host Manager control write failed. Check root-owned host paths.\n");
process.exit(1);
});
};
run();
setInterval(run, HOST_POLL_MS);
}
void main().catch(() => {
process.stderr.write("Host Manager failed. Check host configuration, ownership and LaunchDaemon installation.\n");
process.exitCode = 1;
});