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/release): ship responsive npm integrations (#1458)

liuyongcheng000033f204406cc6
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index d5f0112b5..d6b6ab09d 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -813,6 +813,11 @@ jobs:
node-version: ">=24.16.0 <25"
registry-url: ${{ env.NPM_REGISTRY_URL }}
+ - name: Verify npm publishing authentication
+ env:
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
+ run: npm whoami --registry "${NPM_REGISTRY_URL}"
+
# No artifact download required: the publish steps below pack and
# publish directly from the checked-out source tree (sdk/typescript
# and plugins/openclaw) via `npm pack` + `npm publish`. The
@@ -828,12 +833,15 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
version="${{ needs.detect-version.outputs.npm_version }}"
+ if npm view "${NPM_SDK_PACKAGE}@${version}" version --registry "${NPM_REGISTRY_URL}" >/dev/null 2>&1; then
+ echo "${NPM_SDK_PACKAGE}@${version} is already published; continuing."
+ exit 0
+ fi
cd sdk/typescript
npm ci
npm run build
npm version "$version" --no-git-tag-version --allow-same-version
npm publish --access public
- continue-on-error: true
- name: Publish ${{ env.NPM_OPENCLAW_PACKAGE }} to npmjs.org
id: npm-openclaw-publish
@@ -841,6 +849,10 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
version="${{ needs.detect-version.outputs.npm_version }}"
+ if npm view "${NPM_OPENCLAW_PACKAGE}@${version}" version --registry "${NPM_REGISTRY_URL}" >/dev/null 2>&1; then
+ echo "${NPM_OPENCLAW_PACKAGE}@${version} is already published; continuing."
+ exit 0
+ fi
cd plugins/openclaw
npm ci
npm run build
@@ -854,7 +866,6 @@ jobs:
EOF
node prepare-dist.mjs
npm publish --access public
- continue-on-error: true
- name: Publish ${{ env.NPM_OPENCODE_PACKAGE }} to npmjs.org
id: npm-opencode-publish
@@ -862,6 +873,10 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
version="${{ needs.detect-version.outputs.npm_version }}"
+ if npm view "${NPM_OPENCODE_PACKAGE}@${version}" version --registry "${NPM_REGISTRY_URL}" >/dev/null 2>&1; then
+ echo "${NPM_OPENCODE_PACKAGE}@${version} is already published; continuing."
+ exit 0
+ fi
cd plugins/opencode
npm ci
npm run build
@@ -874,12 +889,6 @@ jobs:
fs.writeFileSync("package.json", `${JSON.stringify(pkg, null, 2)}\n`);
EOF
npm publish --access public
- continue-on-error: true
-
- - name: npm publish notice
- if: steps.npm-sdk-publish.outcome == 'failure' || steps.npm-openclaw-publish.outcome == 'failure' || steps.npm-opencode-publish.outcome == 'failure'
- run: |
- echo "::notice::One or more npm publishes failed. Set NPM_SKIP=true in repo Variables to skip npm publishes if tokens are not configured."
publish-github-packages:
needs: [detect-version, build]
diff --git a/plugins/openclaw/README.md b/plugins/openclaw/README.md
index 0994c72f6..7de45bdf2 100644
--- a/plugins/openclaw/README.md
+++ b/plugins/openclaw/README.md
@@ -207,7 +207,8 @@ Compression is lossless via CCR (Compress-Cache-Retrieve): originals are stored
| `pythonPath` | auto-detected | Optional Python executable override for Python fallback launcher. |
| `autoStart` | `false` | Opt-in auto-start for a local `headroom proxy` if not already running (local URLs only; ignored for remote proxies). Keep `false` when systemd owns the proxy. |
| `startupTimeoutMs` | `20000` | Time to wait for auto-started proxy to become healthy |
-| `requestTimeoutMs` | `30000` | Maximum milliseconds to wait for a single `compress()` call. If the proxy hangs or is slow, the call is cancelled after this deadline and the original uncompressed messages are used as a fallback. |
+| `requestTimeoutMs` | `2000` | Maximum milliseconds to wait for a single `compress()` call. The SDK aborts the request at this deadline and the original messages are used as a fallback. |
+| `minContextChars` | `800` | Skip proxy compression below this approximate 200-token context floor. Large single tool results still qualify; set to `0` to disable the gate. |
| `circuitBreakerThreshold` | `3` | Number of consecutive `assemble()` errors before the circuit breaker opens and all requests bypass the proxy. Prevents cascading failures when the proxy is unhealthy. |
| `circuitBreakerCooldownMs` | `60000` | How long (ms) the circuit breaker stays open after the threshold is reached. After the cool-down the breaker resets automatically and the next request re-probes the proxy via `/health`. |
| `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. |
diff --git a/plugins/openclaw/openclaw.plugin.json b/plugins/openclaw/openclaw.plugin.json
index a60bac685..09f3d7bb6 100644
--- a/plugins/openclaw/openclaw.plugin.json
+++ b/plugins/openclaw/openclaw.plugin.json
@@ -26,6 +26,10 @@
"label": "Compression Request Timeout",
"help": "Maximum milliseconds to wait for one compression request before returning the original messages."
},
+ "minContextChars": {
+ "label": "Minimum Context Size",
+ "help": "Skip the proxy for smaller contexts that cannot produce meaningful savings. Set to 0 to disable."
+ },
"circuitBreakerThreshold": {
"label": "Circuit Breaker Threshold",
"help": "Consecutive compression failures before temporarily bypassing Headroom."
@@ -88,7 +92,12 @@
"requestTimeoutMs": {
"type": "integer",
"minimum": 1,
- "default": 30000
+ "default": 2000
+ },
+ "minContextChars": {
+ "type": "integer",
+ "minimum": 0,
+ "default": 800
},
"circuitBreakerThreshold": {
"type": "integer",
diff --git a/plugins/openclaw/src/engine.ts b/plugins/openclaw/src/engine.ts
index 1d7f9304d..0a260e844 100644
--- a/plugins/openclaw/src/engine.ts
+++ b/plugins/openclaw/src/engine.ts
@@ -17,17 +17,6 @@ import {
type OpenClawCompactResult,
} from "./openclaw-compaction.js";
-/** Race a promise against a timeout and always release the timer. */
-function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
- let timerId: ReturnType<typeof setTimeout> | undefined;
- const timer = new Promise<never>((_, reject) => {
- timerId = setTimeout(() => reject(new Error(`headroom compress() timed out after ${ms}ms`)), ms);
- });
- return Promise.race([promise, timer]).finally(() => {
- if (timerId !== undefined) clearTimeout(timerId);
- });
-}
-
// Must stay static — OpenClaw prepends it to the cached system prompt.
const HEADROOM_COMPRESSION_NOTICE =
"[Headroom is compressing tool outputs in this session. Use headroom_retrieve if you need the original, uncompressed content.]";
@@ -35,6 +24,7 @@ const HEADROOM_COMPRESSION_NOTICE =
export interface HeadroomEngineConfig extends ProxyManagerConfig {
enabled?: boolean;
requestTimeoutMs?: number;
+ minContextChars?: number;
circuitBreakerThreshold?: number;
circuitBreakerCooldownMs?: number;
/** Where to durably record committed turn-advancement keys (see
@@ -160,17 +150,26 @@ export class HeadroomContextEngine {
try {
// Convert AgentMessage → OpenAI format
const openaiMessages = agentToOpenAIIndexed(params.messages);
+ const minContextChars = this.config.minContextChars ?? 800;
+ if (minContextChars > 0 && countContextChars(openaiMessages) < minContextChars) {
+ this.logger.debug("[headroom] Context below compression threshold — using original messages");
+ return {
+ messages: normalizeAgentMessages(params.messages),
+ estimatedTokens: 0,
+ systemPromptAddition: this.sessionCompressionNotice(params.sessionId),
+ };
+ }
// Compress via proxy — pass tokenBudget so RollingWindow enforces it
- const result = await withTimeout(
- compress(openaiMessages, {
- model: params.model ?? "claude-sonnet-4-5",
- baseUrl: this.proxyUrl,
- fallback: true,
- tokenBudget: params.tokenBudget,
- } as any),
- this.config.requestTimeoutMs ?? 30_000,
- );
+ const result = await compress(openaiMessages, {
+ model: params.model ?? "claude-sonnet-4-5",
+ baseUrl: this.proxyUrl,
+ fallback: true,
+ tokenBudget: params.tokenBudget,
+ // The SDK applies this to AbortSignal.timeout(), so a timed-out call is
+ // cancelled instead of continuing in the background after fail-open.
+ timeout: this.config.requestTimeoutMs ?? 2_000,
+ } as any);
const shouldAnnounceCompression =
(this.hasAnnouncedCompression(params.sessionId) || result.tokensSaved > 100) &&
@@ -400,3 +399,14 @@ export class HeadroomContextEngine {
}
}
}
+
+function countContextChars(messages: ReturnType<typeof agentToOpenAIIndexed>): number {
+ let total = 0;
+ for (const message of messages) {
+ total += message.content?.length ?? 0;
+ for (const toolCall of message.tool_calls ?? []) {
+ total += toolCall.function?.arguments?.length ?? 0;
+ }
+ }
+ return total;
+}
diff --git a/plugins/openclaw/test/engine.test.ts b/plugins/openclaw/test/engine.test.ts
index 33f14f3a3..386a1740f 100644
--- a/plugins/openclaw/test/engine.test.ts
+++ b/plugins/openclaw/test/engine.test.ts
@@ -277,40 +277,77 @@ describe("HeadroomContextEngine proxy startup helpers", () => {
expect(mocked.start).toHaveBeenCalledTimes(1);
});
- it("clears the request timeout after successful compression", async () => {
- vi.useFakeTimers();
- try {
- vi.mocked(compress).mockResolvedValue({
- compressed: false,
- messages: [{ role: "user", content: "hello" }],
- tokensBefore: 5,
- tokensAfter: 5,
- tokensSaved: 0,
- });
+ it("passes the configured request timeout to the aborting SDK client", async () => {
+ vi.mocked(compress).mockResolvedValue({
+ compressed: false,
+ messages: [{ role: "user", content: "hello" }],
+ tokensBefore: 5,
+ tokensAfter: 5,
+ tokensSaved: 0,
+ });
- const engine = new HeadroomContextEngine({ requestTimeoutMs: 30_000 });
- (engine as { proxyUrl: string | null }).proxyUrl = "http://127.0.0.1:8787";
+ const engine = new HeadroomContextEngine({ requestTimeoutMs: 1_234, minContextChars: 0 });
+ (engine as { proxyUrl: string | null }).proxyUrl = "http://127.0.0.1:8787";
- await expect(
- engine.assemble({
- sessionId: "session-1",
- messages: [{ role: "user", content: "hello" }],
- }),
- ).resolves.toEqual({
- messages: [{ role: "user", content: "hello" }],
- estimatedTokens: 5,
- });
+ await engine.assemble({
+ sessionId: "session-1",
+ messages: [{ role: "user", content: "hello" }],
+ });
- expect(vi.getTimerCount()).toBe(0);
- } finally {
- vi.useRealTimers();
- }
+ expect(compress).toHaveBeenCalledWith(
+ expect.any(Array),
+ expect.objectContaining({ timeout: 1_234 }),
+ );
+ });
+
+ it("skips proxy compression for short contexts with no meaningful savings", async () => {
+ const messages = [{ role: "user", content: "hello" }];
+ const engine = new HeadroomContextEngine();
+ (engine as { proxyUrl: string | null }).proxyUrl = "http://127.0.0.1:8787";
+
+ await expect(engine.assemble({ sessionId: "session-1", messages })).resolves.toEqual({
+ messages,
+ estimatedTokens: 0,
+ });
+ expect(compress).not.toHaveBeenCalled();
+ });
+
+ it("counts a single large tool result as eligible context", async () => {
+ const messages = [{ role: "toolResult", toolCallId: "call-1", content: "x".repeat(800) }];
+ vi.mocked(compress).mockResolvedValue({
+ compressed: false,
+ messages: [{ role: "tool", tool_call_id: "call-1", content: "x".repeat(800) }],
+ tokensBefore: 200,
+ tokensAfter: 200,
+ tokensSaved: 0,
+ });
+ const engine = new HeadroomContextEngine();
+ (engine as { proxyUrl: string | null }).proxyUrl = "http://127.0.0.1:8787";
+
+ await engine.assemble({ sessionId: "session-1", messages });
+ expect(compress).toHaveBeenCalledTimes(1);
+ });
+
+ it("allows the short-context gate to be disabled", async () => {
+ vi.mocked(compress).mockResolvedValue({
+ compressed: false,
+ messages: [{ role: "user", content: "hello" }],
+ tokensBefore: 2,
+ tokensAfter: 2,
+ tokensSaved: 0,
+ });
+ const engine = new HeadroomContextEngine({ minContextChars: 0 });
+ (engine as { proxyUrl: string | null }).proxyUrl = "http://127.0.0.1:8787";
+
+ await engine.assemble({ sessionId: "session-1", messages: [{ role: "user", content: "hello" }] });
+ expect(compress).toHaveBeenCalledTimes(1);
});
it("opens the circuit after consecutive compression failures", async () => {
vi.mocked(compress).mockRejectedValue(new Error("proxy stalled"));
const messages = [{ role: "user", content: "hello" }];
const engine = new HeadroomContextEngine({
+ minContextChars: 0,
circuitBreakerThreshold: 2,
circuitBreakerCooldownMs: 60_000,
});
@@ -451,7 +488,7 @@ describe("HeadroomContextEngine assemble() compression notice", () => {
}
function readyEngine(config?: HeadroomEngineConfig) {
- const engine = new HeadroomContextEngine(config);
+ const engine = new HeadroomContextEngine({ minContextChars: 0, ...config });
(engine as unknown as { proxyUrl: string | null }).proxyUrl = "http://127.0.0.1:8787";
return engine;
}
@@ -491,6 +528,18 @@ describe("HeadroomContextEngine assemble() compression notice", () => {
expect(second.systemPromptAddition).toBe(HEADROOM_COMPRESSION_NOTICE);
});
+ it("keeps an earned notice on a later context below the compression threshold", async () => {
+ vi.mocked(compress).mockResolvedValueOnce(mockCompressResult({ tokensSaved: 150 }));
+ const engine = readyEngine({ minContextChars: 800 });
+ const first = await engine.assemble({
+ sessionId: "s1", messages: [{ role: "user", content: "x".repeat(1000) }],
+ });
+ const second = await engine.assemble({ sessionId: "s1", messages });
+ expect(first.systemPromptAddition).toBe(HEADROOM_COMPRESSION_NOTICE);
+ expect(second.systemPromptAddition).toBe(first.systemPromptAddition);
+ expect(compress).toHaveBeenCalledTimes(1);
+ });
+
it("never returns a notice when announceCompression is false", async () => {
vi.mocked(compress).mockResolvedValueOnce(mockCompressResult({ tokensSaved: 500 }));
@@ -571,7 +620,9 @@ describe("HeadroomContextEngine assemble() compression notice", () => {
const firstNotice = turn1.systemPromptAddition;
expect(firstNotice).toBe(HEADROOM_COMPRESSION_NOTICE);
- vi.mocked(compress).mockImplementationOnce(() => new Promise(() => {}));
+ vi.mocked(compress).mockImplementationOnce(() => new Promise((_, reject) => {
+ setTimeout(() => reject(new Error("SDK request timed out")), requestTimeoutMs);
+ }));
const turn2Promise = engine.assemble({ sessionId: "s1", messages });
await vi.advanceTimersByTimeAsync(requestTimeoutMs);
const turn2 = await turn2Promise;
@@ -619,7 +670,9 @@ describe("HeadroomContextEngine assemble() compression notice", () => {
const turn1 = await engine.assemble({ sessionId: "s1", messages });
expect(turn1.systemPromptAddition).toBeUndefined();
- vi.mocked(compress).mockImplementationOnce(() => new Promise(() => {}));
+ vi.mocked(compress).mockImplementationOnce(() => new Promise((_, reject) => {
+ setTimeout(() => reject(new Error("SDK request timed out")), requestTimeoutMs);
+ }));
const turn2Promise = engine.assemble({ sessionId: "s1", messages });
await vi.advanceTimersByTimeAsync(requestTimeoutMs);
const turn2 = await turn2Promise;
diff --git a/tests/test_release_workflows.py b/tests/test_release_workflows.py
index 1f5410ffb..8e0881e77 100644
--- a/tests/test_release_workflows.py
+++ b/tests/test_release_workflows.py
@@ -1052,7 +1052,7 @@ def test_publish_npm_regenerates_openclaw_dist_metadata_after_version_and_depend
content = (ROOT / ".github" / "workflows" / "release.yml").read_text(encoding="utf-8")
start = content.index("name: Publish ${{ env.NPM_OPENCLAW_PACKAGE }} to npmjs.org")
- end = content.index("continue-on-error: true", start)
+ end = content.index("name: Publish ${{ env.NPM_OPENCODE_PACKAGE }}", start)
block = content[start:end]
version = block.index('npm version "$version"')
@@ -1068,7 +1068,7 @@ def test_publish_npm_rewrites_opencode_dependency_after_version_and_before_publi
content = (ROOT / ".github" / "workflows" / "release.yml").read_text(encoding="utf-8")
start = content.index("name: Publish ${{ env.NPM_OPENCODE_PACKAGE }} to npmjs.org")
- end = content.index("continue-on-error: true", start)
+ end = content.index("\n publish-github-packages:", start)
block = content[start:end]
version = block.index('npm version "$version"')
@@ -1078,6 +1078,39 @@ def test_publish_npm_rewrites_opencode_dependency_after_version_and_before_publi
assert version < dependency < publish
+def test_publish_npm_authenticates_before_any_publish_and_fails_closed() -> None:
+ """An opted-in npm release must not silently succeed with stale packages."""
+ content = (ROOT / ".github" / "workflows" / "release.yml").read_text(encoding="utf-8")
+ start = content.index("\n publish-npm:")
+ end = content.index("\n publish-github-packages:", start)
+ block = content[start:end]
+
+ auth = block.index("name: Verify npm publishing authentication")
+ first_publish = block.index("npm publish --access public")
+ assert auth < first_publish
+ assert 'npm whoami --registry "${NPM_REGISTRY_URL}"' in block
+ assert "NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}" in block
+ assert "continue-on-error: true" not in block
+ assert "npm publish notice" not in block
+
+
+def test_publish_npm_is_rerunnable_after_a_partial_release() -> None:
+ """Immutable versions already published by an earlier attempt must be skipped."""
+ content = (ROOT / ".github" / "workflows" / "release.yml").read_text(encoding="utf-8")
+ start = content.index("\n publish-npm:")
+ end = content.index("\n publish-github-packages:", start)
+ block = content[start:end]
+
+ for package_var in ("NPM_SDK_PACKAGE", "NPM_OPENCLAW_PACKAGE", "NPM_OPENCODE_PACKAGE"):
+ lookup = (
+ f'npm view "${{{package_var}}}@${{version}}" version --registry "${{NPM_REGISTRY_URL}}"'
+ )
+ assert lookup in block
+ lookup_index = block.index(lookup)
+ publish_index = block.index("npm publish --access public", lookup_index)
+ assert lookup_index < publish_index
+
+
def test_sdist_license_is_packaged_and_verified_before_upload() -> None:
"""STRUCTURAL INVARIANT: the sdist tarball must physically contain
every license file PEP 639 declares in PKG-INFO, and the release