fix(api): dynamic import for MITM + fix Turbopack over-bundling warnings (#6366)

Dynamic MITM manager import on the agent-bridge route + Turbopack static-analyzer anchor in the skills generator (#6329). Integrated into release/v3.8.46.
This commit is contained in:
Milan Soni
2026-07-07 02:04:33 +05:30
committed by GitHub
parent 049bad494b
commit 49795c24ef
4 changed files with 161 additions and 23 deletions

View File

@@ -21,6 +21,7 @@
### 🐛 Bug Fixes
- **fix(api):** the agent-bridge server route now resolves the MITM manager via a **dynamic `import("@/mitm/manager.runtime")`** so Turbopack does not statically pull the stub (or over-bundle the manager), and the agent-skills generator anchors its output base path with `path.join(process.cwd(), …)` so Turbopack's static analyzer stops tracing the whole project root ([#6329](https://github.com/diegosouzapw/OmniRoute/issues/6329)). Regression guard: `tests/unit/agent-bridge-server-route-dynamic-import.test.ts`. (thanks @Iammilansoni)
- **fix(api):** internal probes (combo-test, cloud-sync verify) now pick a **management-scoped / allow-all API key** instead of naively grabbing `getApiKeys()[0]` — a restricted `self:usage` first row made the probe fail with "Model X is not allowed for this API key" even when the combo path was healthy (`pickApiKeyForInternalUse` in `src/lib/db/apiKeys.ts`). The API-manager model editor also falls back to `/api/models?all=true` when `/v1/models` is catalog-protected. Regression guard: `tests/unit/pick-internal-api-key-6372.test.ts`. (thanks @jmengit)
- **fix(live-ws):** the Live Dashboard WebSocket server now **rejects on bind failure** (e.g. `EADDRINUSE` when the API bridge already holds the port) instead of letting the error surface as an unhandled `error` event that crash-loops the process — the `error` listener is attached to `wss` (not `server`) and releases the EventBus subscription on a failed start ([#6324](https://github.com/diegosouzapw/OmniRoute/issues/6324)). Regression guard: `tests/unit/live-ws-eaddrinuse-6324.test.ts`. (thanks @vinayakkulkarni)
- **fix(dashboard):** the Home provider-topology widget now trusts the live provider-metrics snapshot — it uses `topology.errorProvider` and live `activeRequests` directly instead of re-deriving state from a stale `lastErrorAt` or applying a frontend timeout filter, so the topology reflects real-time provider health. Regression guard: `tests/unit/home-provider-topology-live-state.test.ts`. (thanks @xz-dev)

View File

@@ -6,7 +6,7 @@
* Body: AgentBridgeServerActionSchema
*/
import { AgentBridgeServerActionSchema } from "@/shared/schemas/agentBridge";
import { startMitm, stopMitm, getMitmStatus, setCachedPassword, getCachedPassword } from "@/mitm/manager";
import { getCachedPassword, setCachedPassword } from "@/mitm/manager";
import { installCertResult, checkCertInstalled } from "@/mitm/cert/install";
import { generateCert } from "@/mitm/cert/generate";
import { resolveMitmDataDir } from "@/mitm/dataDir";
@@ -33,29 +33,34 @@ export async function POST(request: Request): Promise<Response> {
const { action } = parsed.data;
const raw = body as Record<string, unknown>;
const sudoPassword = typeof raw.sudoPassword === "string" ? raw.sudoPassword : (getCachedPassword() ?? "");
const sudoPassword =
typeof raw.sudoPassword === "string" ? raw.sudoPassword : (getCachedPassword() ?? "");
const apiKey = typeof raw.apiKey === "string" ? raw.apiKey : (process.env.ROUTER_API_KEY ?? "");
try {
if (action === "start") {
if (sudoPassword) setCachedPassword(sudoPassword);
const { startMitm } = await import("@/mitm/manager.runtime");
const result = await startMitm(apiKey, sudoPassword);
return Response.json({ ok: true, ...result });
}
if (action === "stop") {
const pwd = sudoPassword || getCachedPassword() || "";
const { stopMitm } = await import("@/mitm/manager.runtime");
const result = await stopMitm(pwd);
return Response.json({ ok: true, ...result });
}
if (action === "restart") {
const pwd = sudoPassword || getCachedPassword() || "";
const { startMitm, stopMitm, getMitmStatus } = await import("@/mitm/manager.runtime");
const status = await getMitmStatus();
if (status.running) {
await stopMitm(pwd);
}
if (sudoPassword) setCachedPassword(sudoPassword);
// stopMitm calls clearCachedPassword() internally, so re-cache after stop
if (sudoPassword || pwd) setCachedPassword(sudoPassword || pwd);
const result = await startMitm(apiKey, sudoPassword || pwd);
return Response.json({ ok: true, ...result });
}

View File

@@ -86,7 +86,7 @@ function buildApiBody(skill: AgentSkill, sources: BuildSources): string {
lines.push("## Authentication\n");
lines.push(
"All requests require a valid Bearer token or session cookie. " +
"Obtain a token via `POST /api/auth/login` or configure `REQUIRE_API_KEY=false` for local development.",
"Obtain a token via `POST /api/auth/login` or configure `REQUIRE_API_KEY=false` for local development."
);
lines.push("");
@@ -108,9 +108,7 @@ function buildApiBody(skill: AgentSkill, sources: BuildSources): string {
// Minimal curl example
const curlMethod = op.method === "GET" ? "" : `-X ${op.method} `;
lines.push("```bash");
lines.push(
`curl ${curlMethod}https://localhost:20128${op.path} \\`,
);
lines.push(`curl ${curlMethod}https://localhost:20128${op.path} \\`);
lines.push(' -H "Authorization: Bearer $OMNIROUTE_TOKEN"');
if (["POST", "PUT", "PATCH"].includes(op.method)) {
lines.push(' -H "Content-Type: application/json" \\');
@@ -124,7 +122,7 @@ function buildApiBody(skill: AgentSkill, sources: BuildSources): string {
lines.push("## Payloads\n");
lines.push(
"See the full OpenAPI specification at `GET /api/openapi/spec` or " +
"`docs/openapi.yaml` for detailed request/response schemas.",
"`docs/openapi.yaml` for detailed request/response schemas."
);
lines.push("");
@@ -133,8 +131,7 @@ function buildApiBody(skill: AgentSkill, sources: BuildSources): string {
function buildCliBody(skill: AgentSkill, sources: BuildSources): string {
const familyMap = sources.cliRegistry.families;
const cmds =
familyMap.get(skill.area as Parameters<typeof familyMap.get>[0]) ?? [];
const cmds = familyMap.get(skill.area as Parameters<typeof familyMap.get>[0]) ?? [];
const lines: string[] = [];
@@ -192,7 +189,7 @@ function buildCliBody(skill: AgentSkill, sources: BuildSources): string {
export function buildSkillMarkdown(
skillId: string,
sources: BuildSources,
existingContent?: string,
existingContent?: string
): { frontmatter: { name: string; description: string }; body: string } {
const skill = getCatalog().find((s) => s.id === skillId);
if (!skill) {
@@ -205,9 +202,7 @@ export function buildSkillMarkdown(
};
const bodyLines =
skill.category === "api"
? buildApiBody(skill, sources)
: buildCliBody(skill, sources);
skill.category === "api" ? buildApiBody(skill, sources) : buildCliBody(skill, sources);
// Re-inject custom block if present in existing content
let customBlock = "";
@@ -218,11 +213,7 @@ export function buildSkillMarkdown(
}
}
const body =
GENERATED_COMMENT +
"\n\n" +
bodyLines +
customBlock;
const body = GENERATED_COMMENT + "\n\n" + bodyLines + customBlock;
return { frontmatter: fm, body };
}
@@ -248,15 +239,15 @@ function assembleFileContent(fm: { name: string; description: string }, body: st
export async function generateAgentSkills(opts: GeneratorOptions): Promise<GeneratorReport> {
const { dryRun = true, prune = false, outputDir = "skills", onlyIds } = opts;
const outputBase = path.resolve(process.cwd(), outputDir);
// Anchor the base path with a literal so Turbopack's static analyzer can resolve
// it without falling back to tracing the entire project root. (#6329)
const outputBase = path.join(process.cwd(), outputDir);
const catalog = getCatalog();
const catalogIds = new Set(catalog.map((s) => s.id));
// Filter to onlyIds if provided
const skillsToProcess = onlyIds
? catalog.filter((s) => onlyIds.includes(s.id))
: catalog;
const skillsToProcess = onlyIds ? catalog.filter((s) => onlyIds.includes(s.id)) : catalog;
// Lazily parse sources (only once per generator run)
let _openapi: ParsedOpenapi | null = null;

View File

@@ -0,0 +1,141 @@
/**
* Unit tests: agent-bridge/server route — dynamic import behavior
*
* Verifies that start/stop/restart actions resolve MITM manager functions
* from @/mitm/manager.runtime (bypassing the Turbopack alias to stub.ts)
* and that restart re-caches the password after stopMitm clears it.
*/
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-ab-dynimp-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
const core = await import("../../src/lib/db/core.ts");
const serverRoute = await import("../../src/app/api/tools/agent-bridge/server/route.ts");
function resetDb() {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(() => resetDb());
test.after(() => {
try {
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
} catch {
/* noop */
}
});
function makeRequest(action: string, body: Record<string, unknown> = {}) {
return new Request("http://localhost/api/tools/agent-bridge/server", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ action, ...body }),
});
}
// ── start action ────────────────────────────────────────────────────────────
test("start: dynamically imports startMitm (not stub)", async () => {
// startMitm will fail because there is no real MITM server to spawn,
// but it should throw a runtime error from the real module, NOT the stub error.
const res = await serverRoute.POST(makeRequest("start", { sudoPassword: "test" }));
const body = (await res.json()) as Record<string, unknown>;
// The real startMitm will fail (no MITM binary), but the error should NOT
// contain the stub error message.
const errMsg =
typeof body.error === "object" && body.error !== null
? ((body.error as Record<string, unknown>).message as string)
: "";
assert.ok(
!errMsg.includes("MITM manager stub reached at runtime"),
`Expected real MITM error, got stub error: ${errMsg}`
);
});
// ── stop action ─────────────────────────────────────────────────────────────
test("stop: dynamically imports stopMitm (not stub)", async () => {
const res = await serverRoute.POST(makeRequest("stop", { sudoPassword: "test" }));
const body = (await res.json()) as Record<string, unknown>;
// stopMitm on a non-running server should succeed (no-op cleanup) or fail
// with a real error — never the stub error.
if (res.status !== 200) {
const errMsg =
typeof body.error === "object" && body.error !== null
? ((body.error as Record<string, unknown>).message as string)
: "";
assert.ok(
!errMsg.includes("MITM manager stub reached at runtime"),
`Expected real error, got stub error: ${errMsg}`
);
}
});
// ── restart action ──────────────────────────────────────────────────────────
test("restart: dynamically imports getMitmStatus (not stub)", async () => {
// The real getMitmStatus returns running:false since no server is started.
// The stub also returns running:false, but we verify the path works end-to-end.
const res = await serverRoute.POST(makeRequest("restart", { sudoPassword: "test" }));
const body = (await res.json()) as Record<string, unknown>;
// Should not contain the stub error
const errMsg =
typeof body.error === "object" && body.error !== null
? ((body.error as Record<string, unknown>).message as string)
: "";
assert.ok(
!errMsg.includes("MITM manager stub reached at runtime"),
`Expected real error, got stub error: ${errMsg}`
);
});
test("restart: re-caches password after stopMitm clears it", async () => {
// This test verifies the fix for the cached-password loss bug.
// We can't easily observe the internal cache, but we can verify
// the restart action completes without a "password required" error
// when sudoPassword is provided.
const res = await serverRoute.POST(makeRequest("restart", { sudoPassword: "my-secret-pwd" }));
// Should not fail with an auth-related error — the password should survive the stop phase
assert.ok(res.status === 200 || res.status === 500, `Unexpected status: ${res.status}`);
if (res.status === 500) {
const body = (await res.json()) as Record<string, unknown>;
const errMsg =
typeof body.error === "object" && body.error !== null
? ((body.error as Record<string, unknown>).message as string)
: "";
// Should NOT be a stub error or password-missing error
assert.ok(
!errMsg.includes("MITM manager stub reached at runtime"),
`Got stub error instead of runtime error: ${errMsg}`
);
}
});
// ── existing validation tests (unchanged behavior) ─────────────────────────
test("invalid action returns 400", async () => {
const res = await serverRoute.POST(makeRequest("bogus"));
assert.equal(res.status, 400);
});
test("malformed JSON returns 400", async () => {
const res = await serverRoute.POST(
new Request("http://localhost/api/tools/agent-bridge/server", {
method: "POST",
headers: { "content-type": "application/json" },
body: "not-json",
})
);
assert.equal(res.status, 400);
});