Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

fix(runtime): include worker stderr in logs and startup failures

kashorinb116caa63d35
diff --git a/packages/runtime-local/README.md b/packages/runtime-local/README.md
index 18f1badc..28708754 100644
--- a/packages/runtime-local/README.md
+++ b/packages/runtime-local/README.md
@@ -108,7 +108,12 @@ The lifecycle commands have deliberately narrow meanings:
- `hypit runtime init` writes and selects the Distribution's starter Profile; it performs no setup or
network access and never overwrites an existing Profile.
-- `hypit runtime up|down` manages local package preparation, declared local Programs and the Worker.
+- `hypit runtime up` prepares selected local dependencies, starts declared local Programs and starts
+ the Worker. `hypit runtime down` stops the Worker; `hypit programs down` stops the Programs.
Remote services have no lifecycle for Hypit to start or stop.
+- `hypit runtime logs` reads the Worker's output, including separately redirected standard errors
+ on Windows. The files are `worker.log` and `worker.err.log` under the Runtime data directory's
+ `worker/`. Separate error output is labeled `[stderr]`; the files do not establish an interleaved
+ event order. A failure before readiness also includes the recorded error in its startup message.
- `hypit doctor` is the active, read-only check. Endpoint-owned diagnostics may authenticate and read a
remote capability catalog; normal preflight never does.
diff --git a/packages/runtime-local/src/process-logs.ts b/packages/runtime-local/src/process-logs.ts
new file mode 100644
index 00000000..9c05dd2b
--- /dev/null
+++ b/packages/runtime-local/src/process-logs.ts
@@ -0,0 +1,33 @@
+import { open } from "node:fs/promises";
+
+/** PowerShell redirects a process's two streams to separate files. */
+export function processErrorLogPath(logPath: string): string {
+ return `${logPath.replace(/\.log$/u, "")}.err.log`;
+}
+
+async function readTail(path: string, maxBytes: number): Promise<string> {
+ let file;
+ try {
+ file = await open(path, "r");
+ const metadata = await file.stat();
+ const length = Math.min(metadata.size, maxBytes);
+ const buffer = Buffer.alloc(length);
+ const { bytesRead } = await file.read(buffer, 0, length, metadata.size - length);
+ return buffer.subarray(0, bytesRead).toString("utf8");
+ } catch (error) {
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") return "";
+ throw error;
+ } finally {
+ await file?.close();
+ }
+}
+
+/** Preserve stream identity; separate files do not establish an interleaved event order. */
+export async function readProcessLogs(logPath: string, maxBytes: number): Promise<string> {
+ const [output, errors] = await Promise.all([
+ readTail(logPath, maxBytes),
+ readTail(processErrorLogPath(logPath), maxBytes),
+ ]);
+ if (errors.length === 0) return output;
+ return `${output}${output.length > 0 && !output.endsWith("\n") ? "\n" : ""}[stderr]\n${errors}`;
+}
diff --git a/packages/runtime-local/src/programs.ts b/packages/runtime-local/src/programs.ts
index 11ba093f..49999a34 100644
--- a/packages/runtime-local/src/programs.ts
+++ b/packages/runtime-local/src/programs.ts
@@ -9,6 +9,7 @@ import type { ManagedProgram, ManagedProgramCommand, ManagedProgramState } from
import { declaredManagedPrograms } from "./config.js";
import type { LoadRuntimeConfigOptions } from "./config.js";
import { processAlive, stopProcessTree } from "./process-control.js";
+import { processErrorLogPath } from "./process-logs.js";
export type ManagedProgramAction =
| "already-running"
@@ -176,7 +177,7 @@ export async function startWithOwnConsole(
root: string,
logPath: string,
): Promise<{ readonly pid?: number; readonly detail?: string }> {
- const errorPath = `${logPath.replace(/\.log$/u, "")}.err.log`;
+ const errorPath = processErrorLogPath(logPath);
const environment = Object.entries(start.env ?? {})
.map(([name, value]) => `$env:${name} = ${powershellLiteral(String(value))}`).join("\n");
const argumentList = start.args.length === 0
diff --git a/packages/runtime-local/src/worker-process.ts b/packages/runtime-local/src/worker-process.ts
index 94ccaf66..de7de136 100644
--- a/packages/runtime-local/src/worker-process.ts
+++ b/packages/runtime-local/src/worker-process.ts
@@ -6,6 +6,7 @@ import { dirname, join, resolve } from "node:path";
import { canonicalStringify } from "@hypit/protocol";
import { processAlive, stopProcessTree } from "./process-control.js";
+import { readProcessLogs } from "./process-logs.js";
import { startWithOwnConsole } from "./programs.js";
export type RuntimeWorkerLaunch = {
@@ -160,12 +161,7 @@ async function waitForReady(
while (Date.now() <= deadline) {
const current = await record(profile, dataRoot);
if (current === undefined || !processAlive(current.pid)) {
- let log = "";
- try {
- log = await readFile(location.log, "utf8");
- } catch (error) {
- if (!nodeError(error, "ENOENT")) throw error;
- }
+ const log = await readProcessLogs(location.log, LOG_TAIL_BYTES);
throw new Error(`Runtime Worker exited before becoming ready${log.length === 0 ? "" : `: ${log.trim().split("\n").at(-1)}`}`);
}
if (current.owner !== owner) {
@@ -338,20 +334,7 @@ export async function stopRuntimeProcess(profile: string, dataRoot: string, time
export async function runtimeProcessLogs(dataRoot: string): Promise<{ readonly path: string; readonly text: string }> {
const path = paths(dataRoot).log;
- let file;
- try {
- file = await open(path, "r");
- const metadata = await file.stat();
- const length = Math.min(metadata.size, LOG_TAIL_BYTES);
- const buffer = Buffer.alloc(length);
- const { bytesRead } = await file.read(buffer, 0, length, metadata.size - length);
- return { path, text: buffer.subarray(0, bytesRead).toString("utf8") };
- } catch (error) {
- if (error instanceof Error && "code" in error && error.code === "ENOENT") return { path, text: "" };
- throw error;
- } finally {
- await file?.close();
- }
+ return { path, text: await readProcessLogs(path, LOG_TAIL_BYTES) };
}
export async function markRuntimeProcessReady(path: string, owner: string): Promise<void> {
diff --git a/packages/runtime-local/test/worker-process.test.ts b/packages/runtime-local/test/worker-process.test.ts
index 44d3542e..39c4485d 100644
--- a/packages/runtime-local/test/worker-process.test.ts
+++ b/packages/runtime-local/test/worker-process.test.ts
@@ -89,3 +89,34 @@ test("one live detached Runtime Worker survives repeated starts and stale startu
await rm(root, { recursive: true, force: true });
}
});
+
+test("Runtime logs include separately redirected errors even without standard output", async () => {
+ const root = await mkdtemp(join(tmpdir(), "hypit-runtime-logs-"));
+ const worker = join(root, "worker");
+ try {
+ await mkdir(worker);
+ await writeFile(join(worker, "worker.err.log"), "worker could not load the Profile\n");
+ assert.equal((await runtimeProcessLogs(root)).text, "[stderr]\nworker could not load the Profile\n");
+ await writeFile(join(worker, "worker.log"), "worker starting\n");
+ assert.equal((await runtimeProcessLogs(root)).text,
+ "worker starting\n[stderr]\nworker could not load the Profile\n");
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+});
+
+test("a Worker that fails before readiness reports its error stream", async () => {
+ const root = await mkdtemp(join(tmpdir(), "hypit-runtime-start-error-"));
+ const profile = join(root, "runtime.json");
+ const dataRoot = join(root, "runtime");
+ await writeFile(profile, JSON.stringify({ format: "hypit.runtime-local@1" }));
+ try {
+ await assert.rejects(ensureRuntimeProcess(profile, dataRoot, {
+ command: process.execPath,
+ args: ["-e", 'process.stderr.write("worker configuration could not load\\n"); process.exitCode = 1;'],
+ }, 5_000), /worker configuration could not load/u);
+ } finally {
+ await stopRuntimeProcess(profile, dataRoot, 1_000).catch(() => undefined);
+ await rm(root, { recursive: true, force: true });
+ }
+});