headroom/plugins/openclaw/test/plugin-runtime-routing.test.ts
felixboenkost-droid 6d116b15f1
Harden OpenClaw plugin proxy routing (#1074)
## Description

Hardens the bundled OpenClaw plugin so configured proxy routing is
fail-closed and `autoStart` is opt-in.

Closes: N/A

This follow-up is intentionally separate from the ContentRouter cache
fix because it changes plugin/gateway behavior rather than core
compression routing.

The plugin should not mutate upstream provider routing unless a
configured proxy URL is reachable and looks like Headroom. It should
also avoid unhandled startup promise rejections when proxy startup is
fire-and-forget.

Why this shape:

- `autoStart: false` by default matches deployments where Headroom is
supervised externally, for example by systemd. The plugin should not
silently start or assume ownership of a proxy unless the operator opted
in.
- Provider routing is fail-closed: a configured URL must first respond
like Headroom, not merely expose a generic liveness endpoint. This
prevents accidentally routing model traffic through the wrong local
service.
- `/readyz` is treated as liveness, not identity. Identity comes from
Headroom-shaped stats endpoints (`/v1/retrieve/stats` or `/stats`)
because those are harder for unrelated services to satisfy by accident.
- Startup remains asynchronous, but errors are captured and exposed
instead of becoming unhandled promise rejections.
- This is a separate PR because the core cache fix is about compression
correctness, while this patch is about integration safety around
OpenClaw gateway routing.

## Type of Change

- [x] Bug fix (non-breaking change fixes issue)
- [ ] New feature (non-breaking change adds functionality)
- [ ] Breaking change (fix or feature would cause existing functionality
change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- Make proxy `autoStart` opt-in (`default: false`).
- Probe configured `proxyUrl` before applying provider routing.
- Treat `/readyz` as liveness only; require Headroom-shaped
`/v1/retrieve/stats` or `/stats` for identity.
- Observe fire-and-forget startup promise rejection and expose startup
error for callers.
- Isolate proxy-ready listener failures.
- Keep provider routing deferred when no active/probed Headroom proxy
exists.
- Register retrieve tool with explicit `headroom_retrieve` name.
- Extend plugin/unit tests for configured proxy failures, generic
non-Headroom endpoints, path collisions, and routing behavior.

Changed files:

- `plugins/openclaw/README.md`
- `plugins/openclaw/openclaw.plugin.json`
- `plugins/openclaw/src/engine.ts`
- `plugins/openclaw/src/plugin/index.ts`
- `plugins/openclaw/src/proxy-manager.ts`
- `plugins/openclaw/test/engine.test.ts`
- `plugins/openclaw/test/gateway-config.test.ts`
- `plugins/openclaw/test/plugin-runtime-routing.test.ts`
- `plugins/openclaw/test/proxy-manager.test.ts`

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added new functionality
- [x] Manual testing performed

### Test Output

```text
$ npm test

Test Files  6 passed (6)
Tests  74 passed (74)

$ npm run typecheck
tsc --noEmit

$ npm run build
tsup && node prepare-dist.mjs
Build success
```

## Real Behavior Proof

- Environment: local OpenClaw plugin package in the Headroom repo.
- Exact command / steps:
  - Run plugin test suite.
  - Run TypeScript typecheck.
  - Run plugin build.
- Observed result:
  - Tests passed: `74/74`.
  - Typecheck passed.
  - Build passed.
- Not tested:
- Full OpenClaw Gateway integration as part of this standalone PR prep.

## Review Readiness

- [x] I performed self-review
- [x] This PR ready for human review

## Checklist

- [x] My code follows project's style guidelines
- [x] I performed self-review my code
- [ ] I commented my code, particularly in hard-to-understand areas
- [x] I made corresponding changes documentation
- [x] My changes generate no new warnings
- [x] I added tests prove fix is effective or feature works
- [x] New and existing unit tests pass locally my changes
- [ ] I updated CHANGELOG.md if applicable

## Screenshots (if applicable)

N/A.

## Additional Notes

Checklist items left unchecked intentionally:

- No CHANGELOG update included.
- No extra comments were needed beyond existing code structure.

Co-authored-by: Björn-Christian Bönkost <bjoern@v2202603344248440850.hotsrv.de>
2026-06-22 22:52:59 -05:00

385 lines
12 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
const mocked = vi.hoisted(() => ({
ensureProxyUrl: vi.fn(async () => "http://127.0.0.1:8787"),
ensureProxyStarted: vi.fn(),
getProxyUrl: vi.fn(() => null as string | null),
createHeadroomRetrieveTool: vi.fn(({ proxyUrl }: { proxyUrl: string }) => ({ proxyUrl })),
}));
const proxyReadyListeners: Array<(proxyUrl: string) => void | Promise<void>> = [];
vi.mock("../src/engine.js", () => ({
HeadroomContextEngine: class {
ensureProxyUrl = mocked.ensureProxyUrl;
ensureProxyStarted = mocked.ensureProxyStarted;
getProxyUrl = mocked.getProxyUrl;
onProxyReady(listener: (proxyUrl: string) => void | Promise<void>) {
proxyReadyListeners.push(listener);
return () => {};
}
},
}));
vi.mock("../src/tools/headroom-retrieve.js", () => ({
createHeadroomRetrieveTool: mocked.createHeadroomRetrieveTool,
}));
import headroomPlugin from "../src/plugin/index.js";
afterEach(() => {
vi.restoreAllMocks();
mocked.ensureProxyUrl.mockClear();
mocked.ensureProxyStarted.mockClear();
mocked.getProxyUrl.mockReset();
mocked.getProxyUrl.mockReturnValue(null);
mocked.createHeadroomRetrieveTool.mockClear();
proxyReadyListeners.length = 0;
});
describe("headroomPlugin runtime routing", () => {
function stubConfiguredProxyProbe(response: "headroom" | "non-headroom" | "down") {
if (response === "down") {
vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new Error("ECONNREFUSED")));
return;
}
vi.stubGlobal(
"fetch",
vi.fn((url: string) => {
if (url.endsWith("/readyz")) {
return Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve("") });
}
if (url.endsWith("/v1/retrieve/stats")) {
return Promise.resolve({
ok: response === "headroom",
status: response === "headroom" ? 200 : 404,
text: () => Promise.resolve(""),
});
}
if (url.endsWith("/stats")) {
return Promise.resolve({
ok: response === "headroom",
status: response === "headroom" ? 200 : 200,
text: () =>
Promise.resolve(response === "headroom" ? JSON.stringify({ proxy_inbound: { total: 1 } }) : "{}"),
});
}
return Promise.resolve({ ok: false, status: 404, text: () => Promise.resolve("") });
}),
);
}
it("routes configured providers in memory once the proxy becomes available", async () => {
const gatewayHandlers = new Map<string, () => Promise<void>>();
const writeConfigFile = vi.fn();
const loadConfig = vi.fn(() => ({
models: {
providers: {
anthropic: {
api: "anthropic-messages",
},
},
},
}));
const api: any = {
config: {
plugins: {
entries: {
headroom: {
config: {
gatewayProviderIds: ["codex", "claude", "copilot", "gemini", "openrouter"],
},
},
},
},
models: {
providers: {
anthropic: {
api: "anthropic-messages",
baseUrl: "https://api.anthropic.com",
},
"github-copilot": {
baseUrl: "https://api.githubcopilot.com/v1",
},
google: {
baseUrl: "https://generativelanguage.googleapis.com/v1beta",
},
openrouter: {
baseUrl: "https://openrouter.ai/api/v1",
},
},
},
},
logger: {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
},
registerContextEngine: vi.fn(),
registerTool: vi.fn(),
on: vi.fn((event: string, handler: () => Promise<void>) => {
gatewayHandlers.set(event, handler);
}),
runtime: {
config: {
loadConfig,
writeConfigFile,
},
},
};
headroomPlugin(api);
await Promise.resolve();
// With no active or configured proxy URL, initial routing defers without
// auto-starting the proxy or mutating providers.
expect(mocked.ensureProxyUrl).not.toHaveBeenCalled();
expect(mocked.ensureProxyStarted).not.toHaveBeenCalled();
expect(writeConfigFile).not.toHaveBeenCalled();
expect(loadConfig).not.toHaveBeenCalled();
expect(api.config.models.providers["openai-codex"]).toBeUndefined();
await proxyReadyListeners[0]?.("http://127.0.0.1:8787");
expect(api.config.models.providers["openai-codex"]).toEqual({
baseUrl: "http://127.0.0.1:8787/backend-api",
models: [],
});
expect(api.config.models.providers.anthropic).toEqual({
api: "anthropic-messages",
baseUrl: "http://127.0.0.1:8787",
models: [],
});
expect(api.config.models.providers["github-copilot"]).toEqual({
baseUrl: "http://127.0.0.1:8787/v1",
models: [],
});
expect(api.config.models.providers.google).toEqual({
baseUrl: "http://127.0.0.1:8787/v1beta",
models: [],
});
expect(api.config.models.providers.openrouter).toEqual({
baseUrl: "http://127.0.0.1:8787/api/v1",
models: [],
});
const gatewayStart = gatewayHandlers.get("gateway_start");
expect(gatewayStart).toBeTypeOf("function");
// getProxyUrl now reports the active proxy so gateway_start re-routes in
// memory without ever auto-starting or awaiting the proxy.
mocked.getProxyUrl.mockReturnValue("http://127.0.0.1:8787");
await gatewayStart?.();
expect(mocked.ensureProxyStarted).not.toHaveBeenCalled();
expect(mocked.ensureProxyUrl).not.toHaveBeenCalled();
expect(writeConfigFile).not.toHaveBeenCalled();
expect(loadConfig).not.toHaveBeenCalled();
});
it("does not auto-start on gateway_start when no proxy URL is available", async () => {
const gatewayHandlers = new Map<string, () => Promise<void>>();
const api: any = {
config: {
plugins: {
entries: {
headroom: {
config: { gatewayProviderIds: ["claude"] },
},
},
},
models: {
providers: {
anthropic: { api: "anthropic-messages", baseUrl: "https://api.anthropic.com" },
},
},
},
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
registerContextEngine: vi.fn(),
registerTool: vi.fn(),
on: vi.fn((event: string, handler: () => Promise<void>) => {
gatewayHandlers.set(event, handler);
}),
};
headroomPlugin(api);
await Promise.resolve();
await gatewayHandlers.get("gateway_start")?.();
expect(mocked.ensureProxyStarted).not.toHaveBeenCalled();
expect(mocked.ensureProxyUrl).not.toHaveBeenCalled();
expect(api.config.models.providers.anthropic).toEqual({
api: "anthropic-messages",
baseUrl: "https://api.anthropic.com",
});
});
it("routes configured proxyUrl only after it probes as Headroom", async () => {
const gatewayHandlers = new Map<string, () => Promise<void>>();
stubConfiguredProxyProbe("headroom");
const api: any = {
config: {
plugins: {
entries: {
headroom: {
config: {
proxyUrl: "http://127.0.0.1:8787",
gatewayProviderIds: ["claude"],
},
},
},
},
models: {
providers: {
anthropic: { api: "anthropic-messages", baseUrl: "https://api.anthropic.com" },
},
},
},
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
registerContextEngine: vi.fn(),
registerTool: vi.fn(),
on: vi.fn((event: string, handler: () => Promise<void>) => {
gatewayHandlers.set(event, handler);
}),
};
headroomPlugin(api);
await gatewayHandlers.get("gateway_start")?.();
// Configured proxyUrl is probe-gated before provider mutation.
expect(mocked.ensureProxyStarted).not.toHaveBeenCalled();
expect(mocked.ensureProxyUrl).not.toHaveBeenCalled();
expect(api.config.models.providers.anthropic).toEqual({
api: "anthropic-messages",
baseUrl: "http://127.0.0.1:8787",
models: [],
});
});
it("does not route configured proxyUrl when the proxy is unavailable", async () => {
const gatewayHandlers = new Map<string, () => Promise<void>>();
stubConfiguredProxyProbe("down");
const api: any = {
config: {
plugins: {
entries: {
headroom: {
config: {
proxyUrl: "http://127.0.0.1:8787",
gatewayProviderIds: ["claude"],
},
},
},
},
models: {
providers: {
anthropic: { api: "anthropic-messages", baseUrl: "https://api.anthropic.com" },
},
},
},
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
registerContextEngine: vi.fn(),
registerTool: vi.fn(),
on: vi.fn((event: string, handler: () => Promise<void>) => {
gatewayHandlers.set(event, handler);
}),
};
headroomPlugin(api);
await Promise.resolve();
await Promise.resolve();
await gatewayHandlers.get("gateway_start")?.();
expect(mocked.ensureProxyStarted).not.toHaveBeenCalled();
expect(mocked.ensureProxyUrl).not.toHaveBeenCalled();
expect(api.config.models.providers.anthropic).toEqual({
api: "anthropic-messages",
baseUrl: "https://api.anthropic.com",
});
expect(api.logger.warn).toHaveBeenCalledWith(
expect.stringContaining("Skipping upstream gateway routing"),
);
});
it("does not route configured proxyUrl when only generic liveness endpoints respond", async () => {
const gatewayHandlers = new Map<string, () => Promise<void>>();
stubConfiguredProxyProbe("non-headroom");
const api: any = {
config: {
plugins: {
entries: {
headroom: {
config: {
proxyUrl: "http://127.0.0.1:8787",
gatewayProviderIds: ["claude"],
},
},
},
},
models: {
providers: {
anthropic: { api: "anthropic-messages", baseUrl: "https://api.anthropic.com" },
},
},
},
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
registerContextEngine: vi.fn(),
registerTool: vi.fn(),
on: vi.fn((event: string, handler: () => Promise<void>) => {
gatewayHandlers.set(event, handler);
}),
};
headroomPlugin(api);
await gatewayHandlers.get("gateway_start")?.();
expect(api.config.models.providers.anthropic).toEqual({
api: "anthropic-messages",
baseUrl: "https://api.anthropic.com",
});
expect(api.logger.warn).toHaveBeenCalledWith(
expect.stringContaining("configured proxyUrl is not a ready Headroom proxy"),
);
});
it("documents that the retrieve tool can be created from configured proxyUrl before routing is validated", () => {
stubConfiguredProxyProbe("down");
const api: any = {
config: {
plugins: {
entries: {
headroom: {
config: {
proxyUrl: "http://127.0.0.1:8787",
gatewayProviderIds: ["codex"],
},
},
},
},
models: {
providers: {},
},
},
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
registerContextEngine: vi.fn(),
registerTool: vi.fn(),
on: vi.fn(),
};
headroomPlugin(api);
const [toolFactory] = api.registerTool.mock.calls[0];
const tool = toolFactory({});
expect(tool).toEqual({ proxyUrl: "http://127.0.0.1:8787" });
expect(mocked.createHeadroomRetrieveTool).toHaveBeenCalledWith({
proxyUrl: "http://127.0.0.1:8787",
});
});
});