mirror of
https://github.com/nightly-labs/openbot.git
synced 2026-12-03 13:36:27 +08:00
* Remove code comments and add the no-comments rule Remove the comments from first-party code and configuration files: TS/JS, JSX, CSS, SQL migrations, Swift, Kotlin, Gradle, shell, Docker, systemd, Nix, YAML, TOML, JSONC and GritQL. Keep only what a tool reads: license headers, shebangs, compiler and lint directives, JSDoc types in plain .js files, generated file headers, GritQL scope lines, the install-server.sh help text and the openbot-boat-pre-stop marker. Vendored code, fixtures and released schemas keep their comments. Add the rule to AGENTS.md, and change the docs that asked for comments. Add two i18n keys that only comments named to RELEASED_SOURCE_KEYS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep comments that are data in strings The benchmark task files in opencode-free-models-bench.ts tell the model what each function must do. The sample file and HTML page in the two stories are display content. Put their comments back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep released migrations and mobile native inputs unchanged check:migrations compares each released migration with v0.73.0 byte for byte, so restore src/backend/migrations. A change to a native build input of apps/mobile makes a new runtime version, so restore those files and the extra fingerprint sources. List these files and the comment markers that a tool reads in AGENTS.md. Remove the last requests for comments in src/renderer/AGENTS.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
5.3 KiB
TypeScript
135 lines
5.3 KiB
TypeScript
import { type ChildProcess, spawn } from "node:child_process";
|
|
import { createWriteStream } from "node:fs";
|
|
import { mkdir } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import type { Logger } from "@openbot/logging";
|
|
import type { Page } from "playwright-core";
|
|
import { withoutElectronRuntimeFlags } from "../electron-spawn-env";
|
|
import { isSameWorktree, readDevStackRecords } from "./stack-registry";
|
|
|
|
const API_READY_TIMEOUT_MS = 180_000;
|
|
const API_STOP_TIMEOUT_MS = 20_000;
|
|
export const BENCH_ACCOUNT_EMAIL = "openbot-bench@example.com";
|
|
|
|
export interface BenchAuthApi {
|
|
url: string;
|
|
stop: () => Promise<void>;
|
|
}
|
|
|
|
const wait = (milliseconds: number) => new Promise<void>((resolveWait) => setTimeout(resolveWait, milliseconds));
|
|
|
|
async function isLive(port: number): Promise<boolean> {
|
|
try {
|
|
const response = await fetch(`http://127.0.0.1:${port}/health/live`, { signal: AbortSignal.timeout(1_000) });
|
|
return response.ok;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function worktreeApiPort(projectRoot: string, supervisorPid?: number): number | null {
|
|
for (const record of readDevStackRecords()) {
|
|
if (!isSameWorktree(record, projectRoot) || !record.services.includes("api")) continue;
|
|
if (supervisorPid !== undefined && record.supervisorPid !== supervisorPid) continue;
|
|
const port = record.ports.find((entry) => entry.name === "api")?.port;
|
|
if (port !== undefined) return port;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function stopStarted(child: ChildProcess): Promise<void> {
|
|
if (child.exitCode !== null || child.signalCode !== null) return Promise.resolve();
|
|
return new Promise((resolveStop) => {
|
|
const timer = setTimeout(() => {
|
|
if (child.pid !== undefined) {
|
|
try {
|
|
process.kill(-child.pid, "SIGKILL");
|
|
} catch {}
|
|
}
|
|
resolveStop();
|
|
}, API_STOP_TIMEOUT_MS);
|
|
child.once("exit", () => {
|
|
clearTimeout(timer);
|
|
resolveStop();
|
|
});
|
|
child.kill("SIGTERM");
|
|
});
|
|
}
|
|
|
|
export async function ensureBenchAuthApi(
|
|
projectRoot: string,
|
|
logDirectory: string,
|
|
logger: Logger,
|
|
): Promise<BenchAuthApi> {
|
|
const running = worktreeApiPort(projectRoot);
|
|
if (running !== null && (await isLive(running))) {
|
|
logger.info(`using this worktree's Auth API on :${running}`);
|
|
return { url: `http://127.0.0.1:${running}`, stop: async () => undefined };
|
|
}
|
|
await mkdir(logDirectory, { recursive: true });
|
|
const logPath = join(logDirectory, "auth-api.log");
|
|
const log = createWriteStream(logPath, { flags: "a", mode: 0o600 });
|
|
logger.info(`starting the Auth API (log: ${logPath})`);
|
|
const child = spawn("bun", ["scripts/dev-services.ts", "api"], {
|
|
cwd: projectRoot,
|
|
detached: true,
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
env: withoutElectronRuntimeFlags(process.env),
|
|
});
|
|
child.stdout?.pipe(log);
|
|
child.stderr?.pipe(log);
|
|
const supervisorPid = child.pid;
|
|
if (supervisorPid === undefined) throw new Error("The Auth API supervisor did not start.");
|
|
const deadline = Date.now() + API_READY_TIMEOUT_MS;
|
|
for (;;) {
|
|
if (child.exitCode !== null) throw new Error(`The Auth API stopped during startup. See ${logPath}.`);
|
|
const port = worktreeApiPort(projectRoot, supervisorPid);
|
|
if (port !== null && (await isLive(port))) {
|
|
logger.info(`Auth API ready on :${port}`);
|
|
return { url: `http://127.0.0.1:${port}`, stop: () => stopStarted(child) };
|
|
}
|
|
if (Date.now() > deadline) {
|
|
await stopStarted(child);
|
|
throw new Error(`The Auth API did not become ready in ${API_READY_TIMEOUT_MS} ms. See ${logPath}.`);
|
|
}
|
|
await wait(500);
|
|
}
|
|
}
|
|
|
|
interface SignInResult {
|
|
status: string;
|
|
issue: string | null;
|
|
signedInNow: boolean;
|
|
}
|
|
|
|
const SIGN_IN_EXPRESSION = `(async () => {
|
|
const auth = window.openbot.auth;
|
|
const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
let state = await auth.getState();
|
|
for (let tries = 0; tries < 240 && (state.status === "loading" || state.status === "signing_in"); tries += 1) {
|
|
await pause(250);
|
|
state = await auth.getState();
|
|
}
|
|
if (state.status === "error") state = await auth.retry();
|
|
if (state.status === "signed_in") return { status: "signed_in", issue: null, signedInNow: false };
|
|
let challenge = await auth.requestEmailCode(${JSON.stringify(BENCH_ACCOUNT_EMAIL)});
|
|
const retryAfter = challenge.status === "error" ? challenge.issue.retryAfterSeconds : undefined;
|
|
if (retryAfter !== undefined && retryAfter > 0 && retryAfter <= 60) {
|
|
await pause(retryAfter * 1000);
|
|
challenge = await auth.requestEmailCode(${JSON.stringify(BENCH_ACCOUNT_EMAIL)});
|
|
}
|
|
if (challenge.status !== "code_sent" || !challenge.developmentCode) {
|
|
return { status: challenge.status, issue: challenge.issue?.message ?? "no development code", signedInNow: false };
|
|
}
|
|
const verified = await auth.verifyEmailCode({ challengeId: challenge.challengeId, code: challenge.developmentCode });
|
|
return { status: verified.status, issue: verified.issue?.message ?? null, signedInNow: true };
|
|
})()`;
|
|
|
|
export async function signInBenchAccount(page: Page, logger: Logger): Promise<void> {
|
|
const result = await page.evaluate<SignInResult>(SIGN_IN_EXPRESSION);
|
|
if (result.status !== "signed_in") {
|
|
throw new Error(`The bench account could not sign in (${result.status}): ${result.issue ?? "no detail"}.`);
|
|
}
|
|
if (result.signedInNow) logger.info(`signed in as ${BENCH_ACCOUNT_EMAIL}`);
|
|
}
|