Files
headroom/plugins/openclaw/openclaw.plugin.json
T
f8851878a4 fix(plugins/openclaw): keep compression system-prompt notice cache-stable (#3811)
## Description

`HeadroomContextEngine.assemble()` in the OpenClaw plugin returns a
`systemPromptAddition` string that OpenClaw prepends to the system
prompt every turn. It interpolated the per-turn `tokensSaved` count, so
the system prompt — the start of the provider's cached prefix — changed
on every compressing turn, defeating prompt caching. Reported cost: a
turn saving 267 tokens caused a ~93K-token cache miss on Bedrock/Claude
Opus 5.5, roughly 4,000x the tokens the compression saved.

Refs #3809

## Type of Change

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

## Changes Made

- `plugins/openclaw/src/engine.ts`: replaced the interpolated
`${result.tokensSaved}` notice with a fixed constant
(`HEADROOM_COMPRESSION_NOTICE`), so any turn that includes it is
byte-identical to any other. Added a `hasAnnouncedCompression` instance
flag so the notice, once shown (tokensSaved > 100), stays present and
unchanged on later turns even when a given turn has nothing new to
compress
- `plugins/openclaw/src/engine.ts`: added `announceCompression`
(boolean, default `true`) to `HeadroomEngineConfig` to disable the
notice entirely.
- `plugins/openclaw/openclaw.plugin.json`: added `announceCompression`
to `configSchema.properties` (required — schema has
`additionalProperties: false`) and a matching `uiHints` entry.
- `plugins/openclaw/README.md`: documented the new option in the
configuration table.
- `plugins/openclaw/test/engine.test.ts`: 5 new tests, static notice
with no interpolated count, byte-identical notices across differing
`tokensSaved`, notice staying present on a turn with nothing to compress
(the flap-prevention regression case), `announceCompression: false`
opt-out, and the existing below-threshold no-op case.

## Testing

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

### Test Output

```text
$ npx vitest run test/engine.test.ts
 RUN  v4.1.10 /Users/sarah_ling/Documents/GitHub/headroom/plugins/openclaw

 Test Files  1 passed (1)
      Tests  27 passed (27)
   Start at  18:17:43
   Duration  527ms

$ npx vitest run
 Test Files  1 failed | 6 passed (7)
      Tests  3 failed | 104 passed (107)

# The 3 failures are in test/advancement-key-store.test.ts, unrelated to this change
```

## Real Behavior Proof

- Environment: local vitest run against `plugins/openclaw` (mocked
`compress()`/proxy — no live OpenClaw host or real Bedrock/Anthropic
request).
- Exact command / steps: added a temporary test that mocks a 6-turn
session mirroring the reported scenario (tokensSaved: 0, 267, 0, 9000,
0, 50) and recorded `assemble()`'s `systemPromptAddition` per turn.
- Observed result: notice absent on turn 1, appears static and identical
on turns 2–6 (including turns 3/5 with nothing to compress, and turn 6's
below-threshold 50-token save) — 1 total transition across 6 turns,
versus the old behavior's distinct string on every compressing turn.
- Not tested: no live OpenClaw + real provider run to directly observe
the Bedrock cache-read/cache-write fields improve, as the original
issue's report did. The fix is verified at the unit level (the exact
value the plugin hands to OpenClaw), not against a real provider cache.

## Runtime Rollout Safety

- Rollout-managed feature(s): None
- Minimum rollout channel: N/A
- Stable/default behavior changed: yes, the notice text changes (no
longer reports a live token count) and stays present for the rest of the
session instead of tracking that turn's compression state. Default
(`announceCompression` unset) still shows the notice, matching prior
default-on behavior.
- Kill switch / disable path: set `announceCompression: false` in the
plugin config to fully disable the notice.
- Unsafe override required: None
- Qualification impact: None
- Rollback path: Revert this PR

## Review Readiness

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

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/3811"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
2026-10-06 08:37:07 -05:00

134 lines
4.2 KiB
JSON

{
"id": "headroom",
"kind": "context-engine",
"uiHints": {
"proxyUrl": {
"label": "Proxy URL",
"help": "Optional. URL Headroom proxy (example: http://127.0.0.1:8787 or https://headroom.example.com). Configured URLs probe-gated before provider routing. Auto-start opt-in only works local addresses."
},
"proxyPort": {
"label": "Proxy Port",
"help": "Default port used for auto-detect/auto-start when proxyUrl is not set (default: 8787)."
},
"pythonPath": {
"label": "Python Path",
"help": "Optional explicit python executable for python fallback launcher (for example: python, python3, py, or full path)."
},
"retryMaxAttempts": {
"label": "Retry Max Attempts",
"help": "Optional maximum number of upstream retry attempts for connection/read/5xx failures when the plugin auto-starts a local Headroom proxy. Lower values fail faster for interactive chat."
},
"connectTimeoutSeconds": {
"label": "Connect Timeout Seconds",
"help": "Optional upstream connection timeout for the auto-started local Headroom proxy. Lower values surface network failures sooner."
},
"requestTimeoutMs": {
"label": "Compression Request Timeout",
"help": "Maximum milliseconds to wait for one compression request before returning the original messages."
},
"circuitBreakerThreshold": {
"label": "Circuit Breaker Threshold",
"help": "Consecutive compression failures before temporarily bypassing Headroom."
},
"circuitBreakerCooldownMs": {
"label": "Circuit Breaker Cooldown",
"help": "Milliseconds to bypass compression after the circuit breaker opens."
},
"routeCodexViaProxy": {
"label": "Route OpenAI Codex Via 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 in memory. Friendly aliases codex, claude, copilot, and gemini are also accepted. When set, this overrides the default openai-codex-only routing."
},
"announceCompression": {
"label": "Announce Compression In System Prompt",
"help": "When enabled, a static, cache-stable notice is added to the system prompt the first time a session compresses more than 100 tokens. Disable to guarantee the plugin never modifies the system prompt."
}
},
"configSchema": {
"type": "object",
"additionalProperties": false,
"properties": {
"enabled": {
"type": "boolean"
},
"proxyUrl": {
"type": "string",
"pattern": "^https?:\\/\\/.+(:\\d+)?\\/?$"
},
"proxyPort": {
"type": "integer",
"minimum": 1,
"maximum": 65535,
"default": 8787
},
"pythonPath": {
"type": "string"
},
"autoStart": {
"type": "boolean",
"default": false
},
"startupTimeoutMs": {
"type": "integer",
"minimum": 1000,
"maximum": 120000,
"default": 20000
},
"retryMaxAttempts": {
"type": "integer",
"minimum": 1
},
"connectTimeoutSeconds": {
"type": "integer",
"minimum": 1
},
"requestTimeoutMs": {
"type": "integer",
"minimum": 1,
"default": 30000
},
"circuitBreakerThreshold": {
"type": "integer",
"minimum": 1,
"default": 3
},
"circuitBreakerCooldownMs": {
"type": "integer",
"minimum": 1,
"default": 60000
},
"routeCodexViaProxy": {
"type": "boolean",
"default": true
},
"gatewayProviderIds": {
"type": "array",
"items": {
"type": "string"
},
"default": []
},
"announceCompression": {
"type": "boolean",
"default": true
}
}
},
"capabilities": {
"network": {
"allow": [
"http://*:*",
"https://*:*"
]
}
},
"contracts": {
"tools": [
"headroom_retrieve"
]
}
}