Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(openclaw): keep gateway routing runtime-only

JerrettDavise0383675aa85
diff --git a/plugins/openclaw/README.md b/plugins/openclaw/README.md
index 42e0891f6..d44f8b0f0 100644
--- a/plugins/openclaw/README.md
+++ b/plugins/openclaw/README.md
@@ -81,9 +81,9 @@ Default `proxyPort` is `8787`.
### Upstream gateway routing
-By default, the plugin also rewrites the built-in `openai-codex` provider base URL to the active Headroom proxy. That means Codex provider traffic flows through Headroom, so `/stats` can observe real upstream request and cache activity instead of only local context compression.
+By default, the plugin also rewrites the built-in `openai-codex` provider base URL to the active Headroom proxy at runtime. That means Codex provider traffic flows through Headroom, so `/stats` can observe real upstream request and cache activity instead of only local context compression.
-This does not replace Headroom's existing Codex routing rules. The proxy already decides between `api.openai.com` and `chatgpt.com/backend-api/codex/responses` based on ChatGPT auth. The plugin change only points OpenClaw's `openai-codex` provider at the active proxy and preserves the rest of the provider config.
+This does not replace Headroom's existing Codex routing rules. The proxy already decides between `api.openai.com` and `chatgpt.com/backend-api/codex/responses` based on ChatGPT auth. The plugin change only points OpenClaw's provider config at the active proxy in memory and preserves the rest of the provider config.
You can also route additional provider ids such as `anthropic`, `copilot`, or `minimax-portal` through the same proxy:
@@ -102,7 +102,12 @@ You can also route additional provider ids such as `anthropic`, `copilot`, or `m
}
```
-When `gatewayProviderIds` is set, it becomes the exact list the plugin rewrites.
+When `gatewayProviderIds` is set, it becomes the exact list the plugin rewrites in memory for the current gateway process.
+
+The routing is intentionally lightweight and reversible:
+- the plugin does not persist provider `baseUrl` changes back to `openclaw.json`
+- disabling the plugin, clearing `gatewayProviderIds`, or restarting without Headroom restores OpenClaw's normal provider resolution
+- if you want durable provider rewrites, use `headroom wrap openclaw` instead of relying on plugin install side effects
If you need to disable that behavior:
@@ -183,8 +188,8 @@ Compression is lossless via CCR (Compress-Cache-Retrieve): originals are stored
| `pythonPath` | auto-detected | Optional Python executable override for Python fallback launcher. |
| `autoStart` | `true` | Auto-start a local `headroom proxy` if not already running (local URLs only; ignored for remote proxies) |
| `startupTimeoutMs` | `20000` | Time to wait for auto-started proxy to become healthy |
-| `routeCodexViaProxy` | `true` | Rewrite OpenClaw's built-in `openai-codex` provider to use the active Headroom proxy so upstream Codex requests pass through Headroom. |
-| `gatewayProviderIds` | `[]` | Optional explicit list of OpenClaw provider ids to route through the active Headroom proxy. When set, this overrides the default `openai-codex` routing list. |
+| `routeCodexViaProxy` | `true` | Rewrite OpenClaw's built-in `openai-codex` provider to use the active Headroom proxy in memory so upstream Codex requests pass through Headroom. |
+| `gatewayProviderIds` | `[]` | Optional explicit list of OpenClaw provider ids to route through the active Headroom proxy in memory. When set, this overrides the default `openai-codex` routing list. |
## Comparison with lossless-claw
diff --git a/plugins/openclaw/openclaw.plugin.json b/plugins/openclaw/openclaw.plugin.json
index da773de0a..50ddae626 100644
--- a/plugins/openclaw/openclaw.plugin.json
+++ b/plugins/openclaw/openclaw.plugin.json
@@ -16,11 +16,11 @@
},
"routeCodexViaProxy": {
"label": "Route OpenAI Codex Via Headroom",
- "help": "When enabled, OpenClaw will use the active Headroom proxy as the upstream base URL for the built-in openai-codex provider so provider traffic flows through Headroom."
+ "help": "When enabled, OpenClaw will use the active Headroom proxy as the in-memory upstream base URL for the built-in openai-codex provider so provider traffic flows through Headroom."
},
"gatewayProviderIds": {
"label": "Gateway Provider IDs",
- "help": "Optional list of OpenClaw provider ids to route through the active Headroom proxy. When set, this overrides the default openai-codex-only routing."
+ "help": "Optional list of OpenClaw provider ids to route through the active Headroom proxy in memory. When set, this overrides the default openai-codex-only routing."
}
},
"configSchema": {
diff --git a/plugins/openclaw/src/plugin/index.ts b/plugins/openclaw/src/plugin/index.ts
index 8f7d05e36..a8558a3d0 100644
--- a/plugins/openclaw/src/plugin/index.ts
+++ b/plugins/openclaw/src/plugin/index.ts
@@ -17,7 +17,6 @@
import { HeadroomContextEngine } from "../engine.js";
import {
- applyGatewayProviderBaseUrls,
applyGatewayProviderBaseUrlsInPlace,
resolveGatewayProviderIds,
} from "../gateway-config.js";
@@ -49,31 +48,15 @@ export default function headroomPlugin(api: any) {
try {
const activeProxyUrl = await engine.ensureProxyUrl();
- applyGatewayProviderBaseUrlsInPlace(api.config, activeProxyUrl, gatewayProviderIds);
-
- const currentConfig = api.runtime?.config?.loadConfig?.();
- const writeConfigFile = api.runtime?.config?.writeConfigFile;
- if (!currentConfig || typeof writeConfigFile !== "function") {
- logger.info(
- `[headroom] Upstream gateway routing active in memory for ${gatewayProviderIds.join(", ")} via ${activeProxyUrl}`,
- );
- return;
- }
-
- const { changed, config: nextConfig } = applyGatewayProviderBaseUrls(
- currentConfig,
- activeProxyUrl,
- gatewayProviderIds,
- );
+ const changed = applyGatewayProviderBaseUrlsInPlace(api.config, activeProxyUrl, gatewayProviderIds);
if (changed) {
- await writeConfigFile(nextConfig);
logger.info(
- `[headroom] Routed ${gatewayProviderIds.join(", ")} through Headroom proxy at ${activeProxyUrl}`,
+ `[headroom] Routed ${gatewayProviderIds.join(", ")} through Headroom proxy in memory at ${activeProxyUrl}`,
);
} else {
logger.info(
- `[headroom] Upstream gateway already routed for ${gatewayProviderIds.join(", ")} at ${activeProxyUrl}`,
+ `[headroom] Upstream gateway already routed in memory for ${gatewayProviderIds.join(", ")} at ${activeProxyUrl}`,
);
}
} catch (error) {
diff --git a/plugins/openclaw/test/plugin-runtime-routing.test.ts b/plugins/openclaw/test/plugin-runtime-routing.test.ts
new file mode 100644
index 000000000..2e25e9fc0
--- /dev/null
+++ b/plugins/openclaw/test/plugin-runtime-routing.test.ts
@@ -0,0 +1,106 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+const mocked = vi.hoisted(() => ({
+ ensureProxyUrl: vi.fn(async () => "http://127.0.0.1:8787"),
+ getProxyUrl: vi.fn(() => "http://127.0.0.1:8787"),
+ createHeadroomRetrieveTool: vi.fn(({ proxyUrl }: { proxyUrl: string }) => ({ proxyUrl })),
+}));
+
+vi.mock("../src/engine.js", () => ({
+ HeadroomContextEngine: class {
+ ensureProxyUrl = mocked.ensureProxyUrl;
+ getProxyUrl = mocked.getProxyUrl;
+ },
+}));
+
+vi.mock("../src/tools/headroom-retrieve.js", () => ({
+ createHeadroomRetrieveTool: mocked.createHeadroomRetrieveTool,
+}));
+
+import headroomPlugin from "../src/plugin/index.js";
+
+afterEach(() => {
+ mocked.ensureProxyUrl.mockClear();
+ mocked.getProxyUrl.mockClear();
+ mocked.createHeadroomRetrieveTool.mockClear();
+});
+
+describe("headroomPlugin runtime routing", () => {
+ it("routes configured providers in memory without writing config files", 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: ["openai-codex", "anthropic", "github-copilot"],
+ },
+ },
+ },
+ },
+ models: {
+ providers: {
+ anthropic: {
+ api: "anthropic-messages",
+ },
+ },
+ },
+ },
+ 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();
+
+ expect(mocked.ensureProxyUrl).toHaveBeenCalledTimes(1);
+ expect(writeConfigFile).not.toHaveBeenCalled();
+ expect(loadConfig).not.toHaveBeenCalled();
+ expect(api.config.models.providers["openai-codex"]).toEqual({
+ baseUrl: "http://127.0.0.1:8787",
+ 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",
+ models: [],
+ });
+
+ const gatewayStart = gatewayHandlers.get("gateway_start");
+ expect(gatewayStart).toBeTypeOf("function");
+ await gatewayStart?.();
+ expect(writeConfigFile).not.toHaveBeenCalled();
+ expect(loadConfig).not.toHaveBeenCalled();
+ });
+});