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.

refactor(runtime): keep program lifecycle separate from capacity

kashorin507cd5685e56
diff --git a/packages/cli/test/programs-command.test.ts b/packages/cli/test/programs-command.test.ts
index 4fb54b29..1fd1fcb0 100644
--- a/packages/cli/test/programs-command.test.ts
+++ b/packages/cli/test/programs-command.test.ts
@@ -82,7 +82,7 @@ test("program status may report down without failing the observation", async ()
setExitCode(code) { exitCode = code; },
}, distribution([], [{
id: "speech-evidence.local",
- instances: ["speech.primary"],
+ endpoint: "speech.primary",
state: { state: "down", detail: "not running" },
}]));
assert.equal(exitCode, undefined);
@@ -104,7 +104,7 @@ test("program startup reports actions, not no-op checks", async () => {
options.onProgress?.({ id: "example", phase: "starting" });
options.onProgress?.({ id: "example", phase: "waiting" });
options.onProgress?.({ id: "example", phase: "ready" });
- return { dataRoot: "/tmp", programs: [{ id: "example", instances: [], state: { state: "ready" } }] };
+ return { dataRoot: "/tmp", programs: [{ id: "example", endpoint: "example", state: { state: "ready" } }] };
},
},
}),
@@ -149,7 +149,7 @@ test("runtime status keeps scheduling phases out of the default view", async ()
worker: { status: async () => ({ state: "running", profile: path, logPath: "/tmp/worker.log" }) },
programs: { report: async () => ({
dataRoot: "/tmp",
- programs: [{ id: "renderer", instances: [], state: { state: "ready" } }],
+ programs: [{ id: "renderer", endpoint: "renderer", state: { state: "ready" } }],
}) },
}),
openControl: async () => ({
diff --git a/packages/provider-hyperframes-local/src/activation.ts b/packages/provider-hyperframes-local/src/activation.ts
index e3ab881e..585ca994 100644
--- a/packages/provider-hyperframes-local/src/activation.ts
+++ b/packages/provider-hyperframes-local/src/activation.ts
@@ -11,6 +11,7 @@ import {
} from "@hypit/runtime-host-node";
import { createLocalHyperframesProvider } from "./provider.js";
+import { defaultHyperframesCliPath } from "./provider.js";
import type { HyperframesBrowserGpu, HyperframesQuality, HyperframesWorkers } from "./provider.js";
import { localHyperframesBrowserProgram } from "./program.js";
@@ -42,9 +43,9 @@ const localHyperframesRuntimeAdapter = createRuntimeEndpointAdapterFacet({
const configuredNode = runtimeConfigString(config.nodePath, "HyperFrames nodePath");
const configuredCli = runtimeConfigString(config.hyperframesCliPath, "HyperFrames hyperframesCliPath");
const configuredFfprobe = runtimeConfigString(config.ffprobePath, "HyperFrames ffprobePath");
- const nodePath = configuredNode === undefined ? undefined : resolveRuntimeExecutable(context.dataRoot, configuredNode);
- const hyperframesCliPath = configuredCli === undefined ? undefined : resolveRuntimeExecutable(context.dataRoot, configuredCli);
- const ffprobePath = configuredFfprobe === undefined ? undefined : resolveRuntimeExecutable(context.dataRoot, configuredFfprobe);
+ const nodePath = resolveRuntimeExecutable(context.dataRoot, configuredNode ?? process.execPath);
+ const hyperframesCliPath = resolveRuntimeExecutable(context.dataRoot, configuredCli ?? defaultHyperframesCliPath());
+ const ffprobePath = resolveRuntimeExecutable(context.dataRoot, configuredFfprobe ?? "ffprobe");
const defaultConcurrency = runtimeConfigPositiveInteger(config.defaultConcurrency, "HyperFrames defaultConcurrency");
const processTimeoutMs = runtimeConfigPositiveInteger(config.processTimeoutMs, "HyperFrames processTimeoutMs");
const maxProcessOutputBytes = runtimeConfigPositiveInteger(config.maxProcessOutputBytes, "HyperFrames maxProcessOutputBytes");
@@ -53,9 +54,9 @@ const localHyperframesRuntimeAdapter = createRuntimeEndpointAdapterFacet({
endpoint: createLocalHyperframesProvider({
instance: context.instance,
pool: context.pool,
- ...(nodePath === undefined ? {} : { nodePath }),
- ...(hyperframesCliPath === undefined ? {} : { hyperframesCliPath }),
- ...(ffprobePath === undefined ? {} : { ffprobePath }),
+ nodePath,
+ hyperframesCliPath,
+ ffprobePath,
...(workers === undefined ? {} : { workers: workers as HyperframesWorkers }),
...(quality === undefined ? {} : { quality: quality as HyperframesQuality }),
...(browserGpu === undefined ? {} : { browserGpu: browserGpu as HyperframesBrowserGpu }),
@@ -64,7 +65,12 @@ const localHyperframesRuntimeAdapter = createRuntimeEndpointAdapterFacet({
...(maxProcessOutputBytes === undefined ? {} : { maxProcessOutputBytes }),
...(maxRenderedBytes === undefined ? {} : { maxRenderedBytes }),
}),
- program: localHyperframesBrowserProgram(context),
+ program: localHyperframesBrowserProgram({
+ id: context.instance,
+ nodePath,
+ hyperframesCliPath,
+ ffprobePath,
+ }),
diagnose: async () => [
...await diagnoseRuntimeExecutable({
root: context.dataRoot,
diff --git a/packages/provider-hyperframes-local/src/program.ts b/packages/provider-hyperframes-local/src/program.ts
index a27a8e07..b2304cd9 100644
--- a/packages/provider-hyperframes-local/src/program.ts
+++ b/packages/provider-hyperframes-local/src/program.ts
@@ -1,11 +1,7 @@
import { execFile } from "node:child_process";
import { probeMediaToolchain } from "@hypit/media-execution";
-import { runtimeConfigObject, runtimeConfigString } from "@hypit/runtime-kit";
-import type { RuntimeAdapterFactoryContext, ManagedProgram, ManagedProgramState } from "@hypit/runtime-kit";
-import { resolveRuntimeExecutable } from "@hypit/runtime-host-node";
-
-import { defaultHyperframesCliPath } from "./provider.js";
+import type { ManagedProgram, ManagedProgramState } from "@hypit/runtime-kit";
function run(executable: string, args: readonly string[]): Promise<{ ok: boolean; output: string }> {
return new Promise((resolve) => {
@@ -23,17 +19,15 @@ function run(executable: string, args: readonly string[]): Promise<{ ok: boolean
* when this Provider is selected.
*/
export function localHyperframesBrowserProgram(
- context: RuntimeAdapterFactoryContext,
+ input: {
+ readonly id: string;
+ readonly nodePath: string;
+ readonly hyperframesCliPath: string;
+ readonly ffprobePath: string;
+ },
): ManagedProgram {
- const config = runtimeConfigObject(context.config, "local HyperFrames");
- const configuredNode = runtimeConfigString(config.nodePath, "HyperFrames nodePath");
- const configuredCli = runtimeConfigString(config.hyperframesCliPath, "HyperFrames hyperframesCliPath");
- const configuredFfprobe = runtimeConfigString(config.ffprobePath, "HyperFrames ffprobePath");
- const node = resolveRuntimeExecutable(context.dataRoot, configuredNode ?? process.execPath);
- const cli = resolveRuntimeExecutable(context.dataRoot, configuredCli ?? defaultHyperframesCliPath());
- const ffprobe = resolveRuntimeExecutable(context.dataRoot, configuredFfprobe ?? "ffprobe");
const probeBrowser = async (): Promise<ManagedProgramState> => {
- const located = await run(node, [cli, "browser", "path"]);
+ const located = await run(input.nodePath, [input.hyperframesCliPath, "browser", "path"]);
if (!located.ok) return { state: "down", detail: `HyperFrames browser is unavailable: ${located.output}` };
const path = located.output.trim();
if (path.length === 0 || path.includes("\n") || path.includes("\r")) {
@@ -46,15 +40,15 @@ export function localHyperframesBrowserProgram(
return { state: "ready" };
};
return {
- id: "hyperframes-browser",
+ id: input.id,
installation: {
probe: probeBrowser,
- commands: [{ command: node, args: [cli, "browser", "ensure"] }],
+ commands: [{ command: input.nodePath, args: [input.hyperframesCliPath, "browser", "ensure"] }],
},
async probe(): Promise<ManagedProgramState> {
const browser = await probeBrowser();
if (browser.state !== "ready") return browser;
- const media = await probeMediaToolchain({ ffprobePath: ffprobe });
+ const media = await probeMediaToolchain({ ffprobePath: input.ffprobePath });
return media.state === "ready"
? { state: "ready" }
: { state: media.state, detail: media.detail };
diff --git a/packages/provider-hyperframes-local/test/provider.test.ts b/packages/provider-hyperframes-local/test/provider.test.ts
index e8517654..f7871787 100644
--- a/packages/provider-hyperframes-local/test/provider.test.ts
+++ b/packages/provider-hyperframes-local/test/provider.test.ts
@@ -109,9 +109,12 @@ test("local HyperFrames Provider exposes one exact visual capability and two sep
test("the selected HyperFrames Provider owns one idempotent browser installation", () => {
const program = localHyperframesBrowserProgram({
- hostStateRoot: "/host", dataRoot: "/project", instance: "hyperframes", config: {},
+ id: "hyperframes",
+ nodePath: process.execPath,
+ hyperframesCliPath: "/hyperframes-cli.js",
+ ffprobePath: "ffprobe",
});
- assert.equal(program.id, "hyperframes-browser");
+ assert.equal(program.id, "hyperframes");
assert.equal(program.start, undefined);
assert.deepEqual(program.installation?.commands[0]?.args.slice(-2), ["browser", "ensure"]);
});
diff --git a/packages/provider-image-opencv-local/src/activation.ts b/packages/provider-image-opencv-local/src/activation.ts
index 2129742f..4e5c7250 100644
--- a/packages/provider-image-opencv-local/src/activation.ts
+++ b/packages/provider-image-opencv-local/src/activation.ts
@@ -21,8 +21,13 @@ const localOpenCvRuntimeAdapter = createRuntimeEndpointAdapterFacet({
runtimeConfigExact(config, [
"pythonExecutable", "defaultConcurrency", "processTimeoutMs", "maxInputBytes", "maxOutputBytes",
], "local OpenCV image");
- runtimeConfigString(config.pythonExecutable, "OpenCV pythonExecutable");
- const deployment = resolveLocalOpenCvDeployment(context);
+ const configuredPython = runtimeConfigString(config.pythonExecutable, "OpenCV pythonExecutable");
+ const deployment = resolveLocalOpenCvDeployment({
+ hostStateRoot: context.hostStateRoot,
+ dataRoot: context.dataRoot,
+ instance: context.instance,
+ ...(configuredPython === undefined ? {} : { pythonExecutable: configuredPython }),
+ });
const defaultConcurrency = runtimeConfigPositiveInteger(config.defaultConcurrency, "OpenCV defaultConcurrency");
const processTimeoutMs = runtimeConfigPositiveInteger(config.processTimeoutMs, "OpenCV processTimeoutMs");
const maxInputBytes = runtimeConfigPositiveInteger(config.maxInputBytes, "OpenCV maxInputBytes");
@@ -37,7 +42,7 @@ const localOpenCvRuntimeAdapter = createRuntimeEndpointAdapterFacet({
...(maxInputBytes === undefined ? {} : { maxInputBytes }),
...(maxOutputBytes === undefined ? {} : { maxOutputBytes }),
}),
- program: localOpenCvProgram(context),
+ program: localOpenCvProgram(context.instance, deployment),
diagnose: () => diagnoseRuntimeExecutable({
root: context.dataRoot,
configured: deployment.pythonExecutable,
diff --git a/packages/provider-image-opencv-local/src/deployment.ts b/packages/provider-image-opencv-local/src/deployment.ts
index 3ef7e687..e21fa6b9 100644
--- a/packages/provider-image-opencv-local/src/deployment.ts
+++ b/packages/provider-image-opencv-local/src/deployment.ts
@@ -2,8 +2,7 @@ import { existsSync } from "node:fs";
import { join } from "node:path";
import { resolveNodePackageResource } from "@hypit/package-loader-node";
-import { runtimeConfigObject, runtimeConfigString } from "@hypit/runtime-kit";
-import type { RuntimeAdapterFactoryContext, ManagedProgramCommand } from "@hypit/runtime-kit";
+import type { ManagedProgramCommand } from "@hypit/runtime-kit";
import {
pythonEnvironmentExecutable,
resolveRuntimeExecutable,
@@ -32,13 +31,16 @@ export type LocalOpenCvDeployment = {
* project is the complete managed deployment.
*/
export function resolveLocalOpenCvDeployment(
- context: RuntimeAdapterFactoryContext,
+ input: {
+ readonly hostStateRoot: string;
+ readonly dataRoot: string;
+ readonly instance: string;
+ readonly pythonExecutable?: string;
+ },
): LocalOpenCvDeployment {
- const config = runtimeConfigObject(context.config, "local OpenCV image");
- const configured = runtimeConfigString(config.pythonExecutable, "OpenCV pythonExecutable");
- if (configured !== undefined) {
+ if (input.pythonExecutable !== undefined) {
return {
- pythonExecutable: resolveRuntimeExecutable(context.dataRoot, configured),
+ pythonExecutable: resolveRuntimeExecutable(input.dataRoot, input.pythonExecutable),
ownership: "external",
};
}
@@ -47,7 +49,11 @@ export function resolveLocalOpenCvDeployment(
"local OpenCV has no bundled managed runtime; configure pythonExecutable explicitly",
);
}
- const stateRoot = join(context.hostStateRoot, "programs", "image-opencv");
+ const stateRoot = join(
+ input.hostStateRoot,
+ "programs",
+ `image-opencv-${encodeURIComponent(input.instance)}`,
+ );
const environment = join(stateRoot, ".venv");
return {
pythonExecutable: pythonEnvironmentExecutable(environment),
diff --git a/packages/provider-image-opencv-local/src/program.ts b/packages/provider-image-opencv-local/src/program.ts
index d6b8e7d2..ccae54a1 100644
--- a/packages/provider-image-opencv-local/src/program.ts
+++ b/packages/provider-image-opencv-local/src/program.ts
@@ -1,8 +1,8 @@
import { execFile } from "node:child_process";
-import type { RuntimeAdapterFactoryContext, ManagedProgram, ManagedProgramState } from "@hypit/runtime-kit";
+import type { ManagedProgram, ManagedProgramState } from "@hypit/runtime-kit";
-import { resolveLocalOpenCvDeployment } from "./deployment.js";
+import type { LocalOpenCvDeployment } from "./deployment.js";
/** Kept equal to `services/image-opencv/pyproject.toml` by a test in this package. */
const REQUIRED_MAJOR = { cv2: 4, numpy: 2 } as const;
@@ -27,11 +27,10 @@ function run(executable: string, args: readonly string[]): Promise<{ ok: boolean
* the APIs this Provider calls, fails in the middle of a Build with a
* subprocess error. Probing says so at `doctor` time instead.
*/
-export function localOpenCvProgram(context: RuntimeAdapterFactoryContext): ManagedProgram {
- const deployment = resolveLocalOpenCvDeployment(context);
+export function localOpenCvProgram(id: string, deployment: LocalOpenCvDeployment): ManagedProgram {
const python = deployment.pythonExecutable;
return {
- id: "image-opencv",
+ id,
...(deployment.stateRoot === undefined ? {} : { stateRoot: deployment.stateRoot }),
...(deployment.installCommands === undefined ? {} : {
installation: {
diff --git a/packages/provider-image-opencv-local/test/program.test.ts b/packages/provider-image-opencv-local/test/program.test.ts
index aa305e40..a8fde247 100644
--- a/packages/provider-image-opencv-local/test/program.test.ts
+++ b/packages/provider-image-opencv-local/test/program.test.ts
@@ -6,15 +6,18 @@ import { isAbsolute, join } from "node:path";
import test from "node:test";
import { localOpenCvProgram } from "../src/program.js";
+import { resolveLocalOpenCvDeployment } from "../src/deployment.js";
-const context = (config: Record<string, unknown> = {}) => ({
+const context = (pythonExecutable?: string) => ({
hostStateRoot: join(tmpdir(), "hypit-opencv-host"),
- dataRoot: process.cwd(), instance: "opencv.test", config: config as never,
+ dataRoot: process.cwd(), instance: "opencv.test",
+ ...(pythonExecutable === undefined ? {} : { pythonExecutable }),
});
test("OpenCV declares how to install its interpreter and nothing to keep running", () => {
- const program = localOpenCvProgram(context());
- assert.equal(program.id, "image-opencv");
+ const selected = context();
+ const program = localOpenCvProgram(selected.instance, resolveLocalOpenCvDeployment(selected));
+ assert.equal(program.id, "opencv.test");
assert.equal(program.start, undefined, "OpenCV runs per Need; there is no daemon to start");
// Absolute: a Runtime root is wherever the Profile lives, not where the
// pinned uv project lives.
@@ -23,12 +26,13 @@ test("OpenCV declares how to install its interpreter and nothing to keep running
const project = program.installation?.commands[0]?.args.at(-2) ?? "";
assert.ok(isAbsolute(project), `${project} must be absolute`);
assert.ok(existsSync(join(project, "pyproject.toml")), `${project} must be the pinned uv project`);
- assert.equal(program.stateRoot, join(context().hostStateRoot, "programs", "image-opencv"));
+ assert.equal(program.stateRoot, join(context().hostStateRoot, "programs", "image-opencv-opencv.test"));
});
test("an interpreter without cv2 is reported here, not mid-Build", async () => {
// `false` exits non-zero without printing a version report.
- const state = await localOpenCvProgram(context({ pythonExecutable: "/usr/bin/false" })).probe();
+ const selected = context("/usr/bin/false");
+ const state = await localOpenCvProgram(selected.instance, resolveLocalOpenCvDeployment(selected)).probe();
assert.equal(state.state, "down");
assert.match(state.state === "down" ? state.detail : "", /cannot import cv2 and numpy/u);
});
@@ -45,7 +49,8 @@ test("an interpreter carrying another OpenCV major is a mismatch, not a failure"
const fake = join(directory, "python");
await writeFile(fake, "#!/bin/sh\necho '{\"cv2\": \"3.4.18\", \"numpy\": \"2.1.0\"}'\n", { mode: 0o755 });
try {
- const state = await localOpenCvProgram(context({ pythonExecutable: fake })).probe();
+ const selected = context(fake);
+ const state = await localOpenCvProgram(selected.instance, resolveLocalOpenCvDeployment(selected)).probe();
assert.equal(state.state, "mismatch");
assert.match(state.state === "mismatch" ? state.detail : "", /cv2 is 3\.4\.18, expected 4\.x/u);
} finally {
diff --git a/packages/provider-image-opencv-local/test/provider.test.ts b/packages/provider-image-opencv-local/test/provider.test.ts
index 4a6a25ab..f74eb174 100644
--- a/packages/provider-image-opencv-local/test/provider.test.ts
+++ b/packages/provider-image-opencv-local/test/provider.test.ts
@@ -67,28 +67,28 @@ test("the OpenCV package is one replaceable Endpoint with no second queue", asyn
});
test("managed and external OpenCV deployments never mix their interpreters", () => {
- const managedContext = { hostStateRoot: "/host", dataRoot: "/project", instance: "opencv", config: {} } as const;
+ const managedContext = { hostStateRoot: "/host", dataRoot: "/project", instance: "opencv" } as const;
const managed = resolveLocalOpenCvDeployment(managedContext);
assert.equal(managed.ownership, "managed");
// The deployment already branches on the platform for the venv layout, so the tail it produces
// is joined with the platform's separator too. Spell the separator as either one.
assert.match(managed.pythonExecutable,
- /host[\\/]programs[\\/]image-opencv[\\/]\.venv[\\/](?:bin[\\/]python|Scripts[\\/]python\.exe)$/u);
+ /host[\\/]programs[\\/]image-opencv-opencv[\\/]\.venv[\\/](?:bin[\\/]python|Scripts[\\/]python\.exe)$/u);
assert.deepEqual(managed.installCommands?.[0]?.args.slice(-1), ["--frozen"]);
- const managedProgram = localOpenCvProgram(managedContext);
+ const managedProgram = localOpenCvProgram(managedContext.instance, managed);
assert.deepEqual(managedProgram.installation?.commands, managed.installCommands);
const externalContext = {
hostStateRoot: "/host",
dataRoot: "/project",
instance: "opencv",
- config: { pythonExecutable: "./tools/python" },
+ pythonExecutable: "./tools/python",
} as const;
const external = resolveLocalOpenCvDeployment(externalContext);
assert.equal(external.ownership, "external");
assert.equal(external.pythonExecutable, resolve(join("/project", "tools", "python")));
assert.equal(external.installCommands, undefined);
- assert.equal(localOpenCvProgram(externalContext).installation, undefined);
+ assert.equal(localOpenCvProgram(externalContext.instance, external).installation, undefined);
});
const liveEnabled = process.env.HYPIT_OPENCV_TESTS === "1";
diff --git a/packages/provider-media-local/src/activation.ts b/packages/provider-media-local/src/activation.ts
index 12e0dc47..45e540c1 100644
--- a/packages/provider-media-local/src/activation.ts
+++ b/packages/provider-media-local/src/activation.ts
@@ -23,8 +23,8 @@ const localMediaRuntimeAdapter = createRuntimeEndpointAdapterFacet({
], "local media");
const configuredFfmpeg = runtimeConfigString(config.ffmpegPath, "media ffmpegPath");
const configuredFfprobe = runtimeConfigString(config.ffprobePath, "media ffprobePath");
- const ffmpegPath = configuredFfmpeg === undefined ? undefined : resolveRuntimeExecutable(context.dataRoot, configuredFfmpeg);
- const ffprobePath = configuredFfprobe === undefined ? undefined : resolveRuntimeExecutable(context.dataRoot, configuredFfprobe);
+ const ffmpegPath = resolveRuntimeExecutable(context.dataRoot, configuredFfmpeg ?? "ffmpeg");
+ const ffprobePath = resolveRuntimeExecutable(context.dataRoot, configuredFfprobe ?? "ffprobe");
const defaultConcurrency = runtimeConfigPositiveInteger(config.defaultConcurrency, "media defaultConcurrency");
const processTimeoutMs = runtimeConfigPositiveInteger(config.processTimeoutMs, "media processTimeoutMs");
const maxProbeOutputBytes = runtimeConfigPositiveInteger(config.maxProbeOutputBytes, "media maxProbeOutputBytes");
@@ -32,13 +32,13 @@ const localMediaRuntimeAdapter = createRuntimeEndpointAdapterFacet({
endpoint: createLocalMediaProvider({
instance: context.instance,
pool: context.pool,
- ...(ffmpegPath === undefined ? {} : { ffmpegPath }),
- ...(ffprobePath === undefined ? {} : { ffprobePath }),
+ ffmpegPath,
+ ffprobePath,
...(defaultConcurrency === undefined ? {} : { defaultConcurrency }),
...(processTimeoutMs === undefined ? {} : { processTimeoutMs }),
...(maxProbeOutputBytes === undefined ? {} : { maxProbeOutputBytes }),
}),
- program: localMediaToolchainProgram(context),
+ program: localMediaToolchainProgram({ id: context.instance, ffmpegPath, ffprobePath }),
diagnose: async () => [
...await diagnoseRuntimeExecutable({
root: context.dataRoot,
diff --git a/packages/provider-media-local/src/program.ts b/packages/provider-media-local/src/program.ts
index 3d3fc734..1c2fb6ff 100644
--- a/packages/provider-media-local/src/program.ts
+++ b/packages/provider-media-local/src/program.ts
@@ -1,23 +1,16 @@
import { probeMediaToolchain } from "@hypit/media-execution";
-import { runtimeConfigObject, runtimeConfigString } from "@hypit/runtime-kit";
-import type { RuntimeAdapterFactoryContext, ManagedProgram } from "@hypit/runtime-kit";
-import { resolveRuntimeExecutable } from "@hypit/runtime-host-node";
+import type { ManagedProgram } from "@hypit/runtime-kit";
/** A system/custom FFmpeg deployment is external: programs up probes it but never mutates a package manager. */
-export function localMediaToolchainProgram(context: RuntimeAdapterFactoryContext): ManagedProgram {
- const config = runtimeConfigObject(context.config, "local media");
- const ffmpegPath = resolveRuntimeExecutable(
- context.dataRoot,
- runtimeConfigString(config.ffmpegPath, "media ffmpegPath") ?? "ffmpeg",
- );
- const ffprobePath = resolveRuntimeExecutable(
- context.dataRoot,
- runtimeConfigString(config.ffprobePath, "media ffprobePath") ?? "ffprobe",
- );
+export function localMediaToolchainProgram(input: {
+ readonly id: string;
+ readonly ffmpegPath: string;
+ readonly ffprobePath: string;
+}): ManagedProgram {
return {
- id: "media-ffmpeg-toolchain",
+ id: input.id,
async probe() {
- const state = await probeMediaToolchain({ ffmpegPath, ffprobePath });
+ const state = await probeMediaToolchain({ ffmpegPath: input.ffmpegPath, ffprobePath: input.ffprobePath });
return state.state === "ready"
? { state: "ready" as const }
: { state: state.state, detail: state.detail };
diff --git a/packages/provider-media-local/test/provider.test.ts b/packages/provider-media-local/test/provider.test.ts
index c2ea3c35..0f1bf96b 100644
--- a/packages/provider-media-local/test/provider.test.ts
+++ b/packages/provider-media-local/test/provider.test.ts
@@ -32,9 +32,11 @@ const hasMediaBinaries = spawnSync("ffmpeg", ["-version"], { stdio: "ignore", wi
test("the local media Provider declares its external toolchain without owning a second daemon", async () => {
const program = localMediaToolchainProgram({
- hostStateRoot: "/host", dataRoot: "/project", instance: "media", config: {},
+ id: "media",
+ ffmpegPath: "ffmpeg",
+ ffprobePath: "ffprobe",
});
- assert.equal(program.id, "media-ffmpeg-toolchain");
+ assert.equal(program.id, "media");
assert.equal(program.installation, undefined);
assert.equal(program.start, undefined);
const state = await program.probe();
diff --git a/packages/provider-whisperx-local/src/activation.ts b/packages/provider-whisperx-local/src/activation.ts
index 019e7790..bf527c81 100644
--- a/packages/provider-whisperx-local/src/activation.ts
+++ b/packages/provider-whisperx-local/src/activation.ts
@@ -6,7 +6,7 @@ import {
runtimeConfigString,
} from "@hypit/runtime-kit";
-import { createLocalWhisperXProvider } from "./provider.js";
+import { createLocalWhisperXProvider, localWhisperXDefaults } from "./provider.js";
import { localWhisperXProgram } from "./program.js";
const localWhisperXRuntimeAdapter = createRuntimeEndpointAdapterFacet({
@@ -37,30 +37,50 @@ const localWhisperXRuntimeAdapter = createRuntimeEndpointAdapterFacet({
const defaultConcurrency = runtimeConfigPositiveInteger(config.defaultConcurrency, "WhisperX defaultConcurrency");
const requestTimeoutMs = runtimeConfigPositiveInteger(config.requestTimeoutMs, "WhisperX requestTimeoutMs");
const maxResponseBytes = runtimeConfigPositiveInteger(config.maxResponseBytes, "WhisperX maxResponseBytes");
- for (const key of ["serviceCommand"] as const) {
- const value = config[key];
- if (value !== undefined
- && (!Array.isArray(value) || value.length === 0
- || value.some((item) => typeof item !== "string" || item.length === 0))) {
- throw new Error(`WhisperX ${key} must be a non-empty array of non-empty strings`);
- }
+ const serviceCommandValue = config.serviceCommand;
+ if (serviceCommandValue !== undefined
+ && (!Array.isArray(serviceCommandValue) || serviceCommandValue.length === 0
+ || serviceCommandValue.some((item) => typeof item !== "string" || item.length === 0))) {
+ throw new Error("WhisperX serviceCommand must be a non-empty array of non-empty strings");
}
+ const selectedBaseUrl = baseUrl ?? localWhisperXDefaults.baseUrl;
+ const selectedModel = expectedModel ?? localWhisperXDefaults.expectedModel;
+ const selectedDevice = expectedDevice ?? localWhisperXDefaults.expectedDevice;
+ const selectedCompute = expectedCompute ?? (selectedDevice === "cpu" ? "int8" : "float16");
+ const selectedBatchSize = expectedBatchSize ?? localWhisperXDefaults.expectedBatchSize;
+ const selectedServiceVersion = expectedServiceVersion ?? localWhisperXDefaults.expectedServiceVersion;
+ const selectedWhisperXVersion = expectedWhisperXVersion ?? localWhisperXDefaults.expectedWhisperXVersion;
+ const serviceCommand = serviceCommandValue === undefined ? undefined : {
+ command: serviceCommandValue[0] as string,
+ args: (serviceCommandValue as string[]).slice(1),
+ };
return {
endpoint: createLocalWhisperXProvider({
instance: context.instance,
pool: context.pool,
- ...(baseUrl === undefined ? {} : { baseUrl }),
- ...(expectedModel === undefined ? {} : { expectedModel }),
- ...(expectedDevice === undefined ? {} : { expectedDevice }),
- ...(expectedCompute === undefined ? {} : { expectedCompute }),
- ...(expectedBatchSize === undefined ? {} : { expectedBatchSize }),
- ...(expectedServiceVersion === undefined ? {} : { expectedServiceVersion }),
- ...(expectedWhisperXVersion === undefined ? {} : { expectedWhisperXVersion }),
+ baseUrl: selectedBaseUrl,
+ expectedModel: selectedModel,
+ expectedDevice: selectedDevice,
+ expectedCompute: selectedCompute,
+ expectedBatchSize: selectedBatchSize,
+ expectedServiceVersion: selectedServiceVersion,
+ expectedWhisperXVersion: selectedWhisperXVersion,
...(defaultConcurrency === undefined ? {} : { defaultConcurrency }),
...(requestTimeoutMs === undefined ? {} : { requestTimeoutMs }),
...(maxResponseBytes === undefined ? {} : { maxResponseBytes }),
}),
- program: localWhisperXProgram(context),
+ program: localWhisperXProgram({
+ id: context.instance,
+ hostStateRoot: context.hostStateRoot,
+ baseUrl: selectedBaseUrl,
+ expectedModel: selectedModel,
+ expectedDevice: selectedDevice,
+ expectedCompute: selectedCompute,
+ expectedBatchSize: selectedBatchSize,
+ expectedServiceVersion: selectedServiceVersion,
+ expectedWhisperXVersion: selectedWhisperXVersion,
+ ...(serviceCommand === undefined ? {} : { serviceCommand }),
+ }),
};
},
});
diff --git a/packages/provider-whisperx-local/src/program.ts b/packages/provider-whisperx-local/src/program.ts
index 1f9342a8..24637d20 100644
--- a/packages/provider-whisperx-local/src/program.ts
+++ b/packages/provider-whisperx-local/src/program.ts
@@ -3,12 +3,10 @@ import { existsSync } from "node:fs";
import { join } from "node:path";
import { resolveNodePackageResource } from "@hypit/package-loader-node";
-import { runtimeConfigObject, runtimeConfigString } from "@hypit/runtime-kit";
import type {
ManagedProgram,
ManagedProgramCommand,
ManagedProgramState,
- RuntimeAdapterFactoryContext,
} from "@hypit/runtime-kit";
import { pythonEnvironmentCommand } from "@hypit/runtime-host-node";
@@ -27,14 +25,6 @@ export const localWhisperXManagedProject = join(
"..",
);
-function configuredCommand(value: unknown, key: string) {
- if (value === undefined) return undefined;
- if (!Array.isArray(value) || value.length === 0 || value.some((item) => typeof item !== "string")) {
- throw new Error(`WhisperX ${key} must be a non-empty array of strings`);
- }
- return { command: value[0] as string, args: (value as string[]).slice(1) };
-}
-
function run(command: ManagedProgramCommand): Promise<{ readonly ok: boolean; readonly output: string }> {
return new Promise((resolve) => {
execFile(command.command, [...command.args], {
@@ -51,28 +41,41 @@ function run(command: ManagedProgramCommand): Promise<{ readonly ok: boolean; re
});
}
-export function localWhisperXProgram(context: RuntimeAdapterFactoryContext): ManagedProgram {
- const config = runtimeConfigObject(context.config, "local WhisperX");
- const baseUrl = (runtimeConfigString(config.baseUrl, "WhisperX baseUrl") ?? "http://127.0.0.1:8765")
- .replace(/\/+$/u, "");
+export type LocalWhisperXProgramOptions = {
+ readonly id: string;
+ readonly hostStateRoot: string;
+ readonly baseUrl: string;
+ readonly expectedModel: string;
+ readonly expectedDevice: string;
+ readonly expectedCompute: string;
+ readonly expectedBatchSize: number;
+ readonly expectedServiceVersion: string;
+ readonly expectedWhisperXVersion: string;
+ readonly serviceCommand?: ManagedProgramCommand;
+};
+
+export function localWhisperXProgram(options: LocalWhisperXProgramOptions): ManagedProgram {
+ const baseUrl = options.baseUrl.replace(/\/+$/u, "");
const serviceUrl = new URL(baseUrl);
- const expectedDevice = runtimeConfigString(config.expectedDevice, "WhisperX expectedDevice") ?? "cpu";
const expected = {
protocol: "hypit.whisperx-service@1",
- serviceVersion: runtimeConfigString(config.expectedServiceVersion, "WhisperX expectedServiceVersion") ?? "0.1.0",
- whisperxVersion: runtimeConfigString(config.expectedWhisperXVersion, "WhisperX expectedWhisperXVersion") ?? "3.8.6",
- model: runtimeConfigString(config.expectedModel, "WhisperX expectedModel") ?? "small",
- device: expectedDevice,
- compute: runtimeConfigString(config.expectedCompute, "WhisperX expectedCompute")
- ?? (expectedDevice === "cpu" ? "int8" : "float16"),
- batchSize: typeof config.expectedBatchSize === "number" ? config.expectedBatchSize : 8,
+ serviceVersion: options.expectedServiceVersion,
+ whisperxVersion: options.expectedWhisperXVersion,
+ model: options.expectedModel,
+ device: options.expectedDevice,
+ compute: options.expectedCompute,
+ batchSize: options.expectedBatchSize,
};
- const customStart = configuredCommand(config.serviceCommand, "serviceCommand");
+ const customStart = options.serviceCommand;
const managed = customStart === undefined;
if (managed && !existsSync(localWhisperXManagedProject)) {
throw new Error("local WhisperX has no packaged managed runtime; configure serviceCommand explicitly");
}
- const stateRoot = join(context.hostStateRoot, "programs", "whisperx");
+ const stateRoot = join(
+ options.hostStateRoot,
+ "programs",
+ `whisperx-${encodeURIComponent(options.id)}-${encodeURIComponent(serviceUrl.host)}`,
+ );
const environment = join(stateRoot, ".venv");
const nltkData = join(stateRoot, "nltk_data");
const serviceEnvironment = {
@@ -116,7 +119,7 @@ export function localWhisperXProgram(context: RuntimeAdapterFactoryContext): Man
: { state: "mismatch", detail: differs.join("; ") };
};
return {
- id: "whisperx",
+ id: options.id,
...(managed ? {
stateRoot,
installation: {
diff --git a/packages/provider-whisperx-local/src/provider.ts b/packages/provider-whisperx-local/src/provider.ts
index c5b32f24..b5857bc6 100644
--- a/packages/provider-whisperx-local/src/provider.ts
+++ b/packages/provider-whisperx-local/src/provider.ts
@@ -19,6 +19,14 @@ export const localWhisperXProviderModuleRef = {
name: "@hypit/provider-whisperx-local",
version: "1",
} as const;
+export const localWhisperXDefaults = {
+ baseUrl: "http://127.0.0.1:8765",
+ expectedModel: "small",
+ expectedDevice: "cpu",
+ expectedBatchSize: 8,
+ expectedServiceVersion: "0.1.0",
+ expectedWhisperXVersion: "3.8.6",
+} as const;
export type CreateLocalWhisperXProviderOptions = {
readonly instance?: string;
readonly pool?: string;
@@ -93,16 +101,16 @@ function result(value: CanonicalValue): EndpointFulfillment {
}
export function createLocalWhisperXProvider(config: CreateLocalWhisperXProviderOptions) {
- const baseUrl = new URL(config.baseUrl ?? "http://127.0.0.1:8765");
+ const baseUrl = new URL(config.baseUrl ?? localWhisperXDefaults.baseUrl);
assert(baseUrl.protocol === "http:" && ["127.0.0.1", "localhost", "::1", "[::1]"].includes(baseUrl.hostname),
"local WhisperX Provider requires a loopback HTTP service");
const normalizedBaseUrl = baseUrl.href.replace(/\/+$/u, "");
- const expectedModel = config.expectedModel ?? "small";
- const expectedDevice = config.expectedDevice ?? "cpu";
+ const expectedModel = config.expectedModel ?? localWhisperXDefaults.expectedModel;
+ const expectedDevice = config.expectedDevice ?? localWhisperXDefaults.expectedDevice;
const expectedCompute = config.expectedCompute ?? (expectedDevice === "cpu" ? "int8" : "float16");
- const expectedBatchSize = positiveInteger(config.expectedBatchSize ?? 8, "expectedBatchSize");
- const expectedServiceVersion = config.expectedServiceVersion ?? "0.1.0";
- const expectedWhisperXVersion = config.expectedWhisperXVersion ?? "3.8.6";
+ const expectedBatchSize = positiveInteger(config.expectedBatchSize ?? localWhisperXDefaults.expectedBatchSize, "expectedBatchSize");
+ const expectedServiceVersion = config.expectedServiceVersion ?? localWhisperXDefaults.expectedServiceVersion;
+ const expectedWhisperXVersion = config.expectedWhisperXVersion ?? localWhisperXDefaults.expectedWhisperXVersion;
assert(expectedModel.trim().length > 0, "expectedModel is empty");
assert(expectedDevice.trim().length > 0, "expectedDevice is empty");
assert(expectedCompute.trim().length > 0, "expectedCompute is empty");
diff --git a/packages/provider-whisperx-local/test/program.test.ts b/packages/provider-whisperx-local/test/program.test.ts
index c8992ea3..6036ee9e 100644
--- a/packages/provider-whisperx-local/test/program.test.ts
+++ b/packages/provider-whisperx-local/test/program.test.ts
@@ -5,26 +5,38 @@ import { tmpdir } from "node:os";
import test from "node:test";
import { localWhisperXProgram } from "../src/program.js";
+import type { LocalWhisperXProgramOptions } from "../src/program.js";
-const context = (config: Record<string, unknown> = {}) => ({
+const options = (overrides: Partial<LocalWhisperXProgramOptions> = {}): LocalWhisperXProgramOptions => ({
+ id: "whisperx.test",
hostStateRoot: join(tmpdir(), "hypit-whisperx-host"),
- dataRoot: tmpdir(), instance: "whisperx.test", config: config as never,
+ baseUrl: "http://127.0.0.1:8765",
+ expectedModel: "small",
+ expectedDevice: "cpu",
+ expectedCompute: "int8",
+ expectedBatchSize: 8,
+ expectedServiceVersion: "0.1.0",
+ expectedWhisperXVersion: "3.8.6",
+ ...overrides,
});
test("the Provider declares how to bring WhisperX up and how to recognise it", () => {
- const program = localWhisperXProgram(context());
- assert.equal(program.id, "whisperx");
+ const program = localWhisperXProgram(options());
+ assert.equal(program.id, "whisperx.test");
assert.match(program.start?.command ?? "", /hypit-whisperx-service(?:\.exe)?$/u);
assert.equal(program.installation?.commands[0]?.command, "uv");
assert.match(program.installation?.commands[1]?.command ?? "", /hypit-whisperx-prepare(?:\.exe)?$/u);
const project = program.installation?.commands[0]?.args.at(-3) ?? "";
assert.ok(isAbsolute(project), `${project} must be absolute`);
assert.ok(existsSync(join(project, "pyproject.toml")), `${project} must be the pinned uv project`);
- assert.equal(program.stateRoot, join(context().hostStateRoot, "programs", "whisperx"));
+ assert.equal(program.stateRoot,
+ join(options().hostStateRoot, "programs", "whisperx-whisperx.test-127.0.0.1%3A8765"));
});
test("a deployment that installs WhisperX elsewhere overrides the command", () => {
- const program = localWhisperXProgram(context({ serviceCommand: ["conda", "run", "whisperx-serve"] }));
+ const program = localWhisperXProgram(options({
+ serviceCommand: { command: "conda", args: ["run", "whisperx-serve"] },
+ }));
assert.deepEqual(program.start, { command: "conda", args: ["run", "whisperx-serve"] });
assert.equal(program.installation, undefined);
assert.equal(program.stateRoot, undefined);
@@ -41,7 +53,7 @@ test("a program answering with another identity is reported, never used", async
device: "cpu",
}), { status: 200 })) as typeof fetch;
try {
- const state = await localWhisperXProgram(context()).probe();
+ const state = await localWhisperXProgram(options()).probe();
assert.equal(state.state, "mismatch");
assert.match(state.state === "mismatch" ? state.detail : "", /model is large-v3, expected small/u);
} finally {
@@ -53,7 +65,7 @@ test("nothing answering is down, not a mismatch", async () => {
const original = globalThis.fetch;
globalThis.fetch = (async () => { throw new Error("ECONNREFUSED"); }) as typeof fetch;
try {
- const state = await localWhisperXProgram(context({ baseUrl: "http://127.0.0.1:9" })).probe();
+ const state = await localWhisperXProgram(options({ baseUrl: "http://127.0.0.1:9" })).probe();
assert.equal(state.state, "down");
} finally {
globalThis.fetch = original;
diff --git a/packages/runtime-host-node/src/index.ts b/packages/runtime-host-node/src/index.ts
index 84a2f398..c8da8857 100644
--- a/packages/runtime-host-node/src/index.ts
+++ b/packages/runtime-host-node/src/index.ts
@@ -163,7 +163,7 @@ export type ManagedProgramProgress = {
export type ManagedProgramReport = {
readonly id: string;
- readonly instances: readonly string[];
+ readonly endpoint: string;
readonly action?: "already-running" | "installed" | "started" | "stopped" | "not-ours" | "nothing-to-stop" | "unchanged";
readonly state:
| { readonly state: "ready" }
diff --git a/packages/runtime-local/src/programs.ts b/packages/runtime-local/src/programs.ts
index 54f8956f..62ae9ca2 100644
--- a/packages/runtime-local/src/programs.ts
+++ b/packages/runtime-local/src/programs.ts
@@ -21,8 +21,8 @@ export type ManagedProgramAction =
export type ManagedProgramReport = {
readonly id: string;
- /** Every Endpoint this program serves, so an operator sees what a restart affects. */
- readonly instances: readonly string[];
+ /** The configured Endpoint that declared this external program. */
+ readonly endpoint: string;
/** What this call did. Absent when it was only asked to look. */
readonly action?: ManagedProgramAction;
readonly state: ManagedProgramState;
@@ -46,15 +46,19 @@ export type ManagedProgramOptions = LoadRuntimeConfigOptions & {
readonly capabilities?: readonly CapabilityRef[];
};
-/** Two Endpoints may drive the same program; it is brought up once. */
-function distinct(programs: readonly { instance: string; program: ManagedProgram }[]) {
- const unique = new Map<string, { program: ManagedProgram; instances: string[] }>();
+function independentPrograms(programs: readonly { instance: string; program: ManagedProgram }[]) {
+ const owners = new Map<string, string>();
for (const item of programs) {
- const found = unique.get(item.program.id);
- if (found === undefined) unique.set(item.program.id, { program: item.program, instances: [item.instance] });
- else found.instances.push(item.instance);
+ const found = owners.get(item.program.id);
+ if (found !== undefined) {
+ throw new Error(
+ `Endpoints ${found} and ${item.instance} declare the same Managed Program ${item.program.id}; `
+ + "each Endpoint must declare its own lifecycle identity",
+ );
+ }
+ owners.set(item.program.id, item.instance);
}
- return [...unique.values()];
+ return programs;
}
function directory(root: string, program: ManagedProgram): string {
@@ -227,11 +231,11 @@ async function waitForReady(program: ManagedProgram, pid: number, maxWaitMs: num
async function bringUp(
root: string,
program: ManagedProgram,
- instances: readonly string[],
+ endpoint: string,
maxWaitMs: number,
onProgress?: (event: ManagedProgramProgress) => void,
): Promise<ManagedProgramReport> {
- const base = { id: program.id, instances };
+ const base = { id: program.id, endpoint };
onProgress?.({ id: program.id, phase: "checking" });
const initial = await program.probe();
if (initial.state === "ready") {
@@ -332,8 +336,8 @@ export async function bringManagedProgramsUp(
// A fresh Runtime has no data directory yet. External commands may use it as
// their working directory, so create it before the first install/start.
await mkdir(dataRoot, { recursive: true });
- const reports = await Promise.all(distinct(programs).map(async ({ program, instances }) =>
- await bringUp(dataRoot, program, instances, options.maxWaitMs ?? 300_000, options.onProgress)));
+ const reports = await Promise.all(independentPrograms(programs).map(async ({ instance, program }) =>
+ await bringUp(dataRoot, program, instance, options.maxWaitMs ?? 300_000, options.onProgress)));
return { dataRoot, programs: reports };
}
@@ -343,8 +347,8 @@ export async function takeManagedProgramsDown(
options: ManagedProgramOptions = {},
): Promise<{ readonly dataRoot: string; readonly programs: readonly ManagedProgramReport[] }> {
const { dataRoot, programs } = await declaredManagedPrograms(path, options);
- const reports = await Promise.all(distinct(programs).map(async ({ program, instances }): Promise<ManagedProgramReport> => {
- const base = { id: program.id, instances };
+ const reports = await Promise.all(independentPrograms(programs).map(async ({ instance, program }): Promise<ManagedProgramReport> => {
+ const base = { id: program.id, endpoint: instance };
const pid = await readPid(dataRoot, program);
if (pid === undefined || !processAlive(pid)) {
if (pid !== undefined) await rm(join(directory(dataRoot, program), "process.pid"), { force: true });
@@ -408,7 +412,7 @@ export async function reportManagedPrograms(
options: ManagedProgramOptions = {},
): Promise<{ readonly dataRoot: string; readonly programs: readonly ManagedProgramReport[] }> {
const { dataRoot, programs } = await declaredManagedPrograms(path, options);
- const reports = await Promise.all(distinct(programs).map(async ({ program, instances }): Promise<ManagedProgramReport> => {
+ const reports = await Promise.all(independentPrograms(programs).map(async ({ instance, program }): Promise<ManagedProgramReport> => {
const state = await program.probe();
const pid = await readPid(dataRoot, program);
const logPath = join(directory(dataRoot, program), "program.log");
@@ -421,7 +425,7 @@ export async function reportManagedPrograms(
}
return {
id: program.id,
- instances,
+ endpoint: instance,
state,
...(pid !== undefined && processAlive(pid) ? { pid } : {}),
...(hasLog ? { logPath } : {}),
diff --git a/packages/runtime-local/test/programs.test.ts b/packages/runtime-local/test/programs.test.ts
index 9db9f55c..bf656df2 100644
--- a/packages/runtime-local/test/programs.test.ts
+++ b/packages/runtime-local/test/programs.test.ts
@@ -51,7 +51,6 @@ async function project(program: (root: string) => ManagedProgram) {
credentials: {},
endpoints: {
one: { use: "example.program", pool: "example.local", config: {} },
- two: { use: "example.program", pool: "example.local", config: {} },
},
}));
const registry = new RuntimeAdapterRegistry();
@@ -126,7 +125,7 @@ function fileBackedProgram(marker: string): ManagedProgram {
};
}
-test("up starts the program once for every Endpoint that drives it, and down stops it", async () => {
+test("up starts one Endpoint's program and down stops it", async () => {
const marker = join(await mkdtemp(join(tmpdir(), "hypit-marker-")), "ready");
const { root, path, options } = await project(() => fileBackedProgram(marker));
const progress: string[] = [];
@@ -136,8 +135,8 @@ test("up starts the program once for every Endpoint that drives it, and down sto
maxWaitMs: 20_000,
onProgress: (event) => progress.push(`${event.id}:${event.phase}`),
});
- assert.equal(started.programs.length, 1, "one program, not one per Endpoint");
- assert.deepEqual(started.programs[0]!.instances, ["one", "two"]);
+ assert.equal(started.programs.length, 1);
+ assert.equal(started.programs[0]!.endpoint, "one");
assert.equal(started.programs[0]!.action, "started");
assert.deepEqual(started.programs[0]!.state, { state: "ready" });
assert.deepEqual(progress, ["example:checking", "example:starting", "example:waiting", "example:ready"]);
@@ -164,7 +163,7 @@ test("up starts the program once for every Endpoint that drives it, and down sto
await rm(root, { recursive: true, force: true });
});
-test("a program answering with another identity is never joined by a second copy", async () => {
+test("a program answering with another identity is never started beside it", async () => {
const { path, options } = await project(() => ({
id: "example",
start: nodeProgram("process.exit(1);"),