diff --git a/scripts/measure-rpc-rtt.mjs b/scripts/measure-rpc-rtt.mjs index 565759bc5e5..f806668a1c5 100644 --- a/scripts/measure-rpc-rtt.mjs +++ b/scripts/measure-rpc-rtt.mjs @@ -1,5 +1,6 @@ import { spawn } from "node:child_process"; import { randomUUID } from "node:crypto"; +import { existsSync } from "node:fs"; import fs from "node:fs/promises"; import { createRequire } from "node:module"; import net from "node:net"; @@ -156,6 +157,14 @@ async function defaultOpen(filePath, flags) { return await fs.open(filePath, flags); } +function resolveOpenClawLaunchArgs(repoRoot, sourceEntryExists = existsSync) { + const sourceEntry = path.join(repoRoot, "src", "entry.ts"); + if (sourceEntryExists(sourceEntry)) { + return ["--import", "tsx", sourceEntry]; + } + return [path.join(repoRoot, "openclaw.mjs")]; +} + export function signalGatewayProcess(child, signal, killProcess = defaultKillProcess) { if (process.platform !== "win32" && typeof child.pid === "number") { try { @@ -272,6 +281,7 @@ export async function startGateway({ openImpl = defaultOpen, port, repoRoot, + sourceEntryExists = existsSync, spawnImpl = spawn, stderrPath, stdoutPath, @@ -290,11 +300,12 @@ export async function startGateway({ } let child; + const launcherArgs = resolveOpenClawLaunchArgs(repoRoot, sourceEntryExists); try { child = spawnImpl( - "pnpm", + process.execPath, [ - "openclaw", + ...launcherArgs, "gateway", "run", "--port", @@ -353,6 +364,25 @@ export async function cleanupTempRoot(tempRoot, { rmImpl = fs.rm } = {}) { } } +async function copyLogIfPresent(source, target) { + try { + await fs.copyFile(source, target); + } catch (error) { + if (error && typeof error === "object" && error.code === "ENOENT") { + return; + } + throw error; + } +} + +async function copyGatewayLogs({ outputDir, stderrPath, stdoutPath }) { + await fs.mkdir(outputDir, { recursive: true }); + await Promise.all([ + copyLogIfPresent(stdoutPath, path.join(outputDir, "gateway.stdout.log")), + copyLogIfPresent(stderrPath, path.join(outputDir, "gateway.stderr.log")), + ]); +} + function quantile(sorted, q) { return sorted[Math.min(sorted.length - 1, Math.max(0, Math.ceil(sorted.length * q) - 1))]; } @@ -642,6 +672,14 @@ async function main() { } finally { removeGatewayParentCleanup(); } + try { + await copyGatewayLogs({ outputDir, stderrPath, stdoutPath }); + } catch (error) { + const message = formatErrorMessage(error); + details = details + ? `${details}\nwarning: failed to copy gateway logs: ${message}` + : `warning: failed to copy gateway logs: ${message}`; + } try { await cleanupTempRoot(tempRoot); } catch (error) { diff --git a/test/scripts/measure-rpc-rtt.test.ts b/test/scripts/measure-rpc-rtt.test.ts index 44f216df755..b5977ceba4b 100644 --- a/test/scripts/measure-rpc-rtt.test.ts +++ b/test/scripts/measure-rpc-rtt.test.ts @@ -29,6 +29,7 @@ describe("scripts/measure-rpc-rtt.mjs", () => { openImpl, port: 23456, repoRoot: "/repo", + sourceEntryExists: () => true, spawnImpl, stderrPath: "/tmp/stderr.log", stdoutPath: "/tmp/stdout.log", @@ -40,9 +41,11 @@ describe("scripts/measure-rpc-rtt.mjs", () => { expect(openImpl).toHaveBeenNthCalledWith(1, "/tmp/stdout.log", "w"); expect(openImpl).toHaveBeenNthCalledWith(2, "/tmp/stderr.log", "w"); expect(spawnImpl).toHaveBeenCalledWith( - "pnpm", + process.execPath, [ - "openclaw", + "--import", + "tsx", + "/repo/src/entry.ts", "gateway", "run", "--port",