fix(cache): align frozen prefixes with replayable tool shapes

This commit is contained in:
Alicia
2026-10-25 04:04:16 +08:00
parent a618561542
commit 2333a5ccf7
5 changed files with 128 additions and 3 deletions
+4
View File
@@ -93,6 +93,10 @@ RUN npm install -g --no-fund --no-audit npm@11.16.0
COPY e2e/wrap/tools/package.json e2e/wrap/tools/package-lock.json /opt/wrap-tools/
RUN npm ci --prefix /opt/wrap-tools --no-fund --no-audit
# Exercise the installed dependency fixes in both existing wrap-E2E CI callers.
COPY e2e/wrap/tools/dependency-regression.test.cjs /opt/wrap-tools/
RUN npm test --prefix /opt/wrap-tools
WORKDIR /workspace
# Bring in the prebuilt manylinux wheel from stage 1.
+8 -1
View File
@@ -10,17 +10,24 @@ PATH. `npm ci` does not support global installs, so diagnostics must also use
`npm list --prefix /opt/wrap-tools`.
After editing a CLI version in `package.json`, regenerate the lockfile using
Node 22 and npm 11.16.0 on Linux:
Node 24.22.1 or later within 24.x and npm 11.16.0 on Linux:
```sh
cd e2e/wrap/tools
npm install --package-lock-only --no-fund --no-audit
npm ci --no-fund --no-audit
npm test
cd ../../..
docker build -f e2e/wrap/Dockerfile -t headroom-wrap-e2e .
docker run --rm headroom-wrap-e2e
```
`npm test` runs the Ajv URI and Express body-parser regression checks against
the installed locked dependencies. The Dockerfile runs the same command from
`/opt/wrap-tools` after `npm ci`, so failures stop the image build in both the
existing Wrap E2E workflow and CI's Docker-native wrap E2E job. Their path
filters include `e2e/**`, covering dependency and regression-test updates.
To intentionally refresh transitive versions without changing the pinned CLI
versions, use `npm update --package-lock-only --no-fund --no-audit` in this
directory instead of `npm install --package-lock-only`, then run the same
+3
View File
@@ -3,6 +3,9 @@
"version": "1.0.0",
"private": true,
"description": "Locked CLI dependencies for the Docker wrap E2E harness",
"scripts": {
"test": "node --test --test-concurrency=1 --test-reporter=tap dependency-regression.test.cjs"
},
"dependencies": {
"@openai/codex": "0.162.1",
"openclaw": "2026.10.6"
+7 -2
View File
@@ -325,8 +325,9 @@ class CompressionCache:
A message is stable if it is a plain user/assistant/system message,
an assistant message with tool_use blocks, or a tool_result whose
content hash is already in the cache. The first unstable tool_result
(cache miss) stops the count.
content hash is already in the cache and whose shape can be replayed.
The first unstable tool_result (cache miss or unsupported shape) stops
the count. A joined text hash alone does not prove shape-safe replay.
The trailing message is *always* excluded from the frozen prefix
(cap of ``len(messages) - 1``). The trailing message represents
@@ -344,6 +345,10 @@ class CompressionCache:
count = 0
for msg in messages:
if _is_tool_result_message(msg):
# A joined-text cache/stable hash cannot prove replay for
# shapes that apply_cached deliberately leaves untouched.
if not _can_swap_tool_result_content(msg):
break
content = _extract_tool_result_content(msg)
if content is not None:
h = self.content_hash(content)
+106
View File
@@ -0,0 +1,106 @@
"""Freeze eligibility must match the bytes the cache can actually replay."""
from __future__ import annotations
import copy
import unittest
from headroom.cache.compression_cache import CompressionCache
from headroom.proxy.session_engine import (
FREEZE_POLICY_CONFIRMED_CLAMP,
FREEZE_POLICY_REPLAYABLE,
prepare_turn,
)
class TestCacheReplayShapes(unittest.TestCase):
def setUp(self) -> None:
self.cache = CompressionCache()
self.cache.store_compressed(self.cache.content_hash("A\nB"), "short", 1)
self.blocks = [{"type": "text", "text": "A"}, {"type": "text", "text": "B"}]
self.messages = [
{"role": "tool", "tool_call_id": "c1", "content": self.blocks},
{"role": "user", "content": "next"},
]
def test_multiblock_cache_hit_remains_mutable(self) -> None:
before = copy.deepcopy(self.messages)
prep = prepare_turn(self.cache, self.messages, policy=FREEZE_POLICY_REPLAYABLE)
self.assertEqual(prep.frozen_message_count, 0)
self.assertEqual(prep.pipeline_input, before)
self.assertEqual(self.messages, before)
self.assertEqual(self.cache.get_stats()["hits"], 0)
self.assertNotIn(self.cache.content_hash("A\nB"), self.cache._stable_hashes)
def test_stable_joined_hash_does_not_prove_shape_replay(self) -> None:
self.cache.mark_stable(self.cache.content_hash("A\nB"))
self.assertEqual(self.cache.compute_frozen_count(self.messages), 0)
def test_prefix_stops_before_unsupported_shape(self) -> None:
messages = [{"role": "user", "content": "earlier"}, *self.messages]
self.assertEqual(self.cache.compute_frozen_count(messages), 1)
def test_confirmed_policy_is_still_clamped_by_replayability(self) -> None:
prep = prepare_turn(
self.cache,
self.messages,
policy=FREEZE_POLICY_CONFIRMED_CLAMP,
tracker_frozen=1,
)
self.assertEqual(prep.frozen_message_count, 0)
def test_explicit_caller_pin_still_wins(self) -> None:
prep = prepare_turn(
self.cache, self.messages, policy=FREEZE_POLICY_REPLAYABLE, explicit_frozen=1
)
self.assertEqual(prep.frozen_message_count, 1)
self.assertEqual(prep.pipeline_input, self.messages)
def test_supported_string_and_single_block_replay(self) -> None:
for content in ["A\nB", [{"type": "text", "text": "A\nB", "custom": [1]}]]:
with self.subTest(content=content):
messages = [{"role": "tool", "content": content}, self.messages[-1]]
prep = prepare_turn(self.cache, messages, policy=FREEZE_POLICY_REPLAYABLE)
self.assertEqual(prep.frozen_message_count, 1)
expected = (
"short"
if isinstance(content, str)
else [{"type": "text", "text": "short", "custom": [1]}]
)
self.assertEqual(prep.pipeline_input[0]["content"], expected)
def test_anthropic_replay_contract_is_unchanged(self) -> None:
content = [{"type": "tool_result", "tool_use_id": "c1", "content": "A\nB"}]
prep = prepare_turn(
self.cache,
[{"role": "user", "content": content}, self.messages[-1]],
policy=FREEZE_POLICY_REPLAYABLE,
)
self.assertEqual(prep.frozen_message_count, 1)
self.assertEqual(prep.pipeline_input[0]["content"][0]["content"], "short")
def test_malformed_and_media_shapes_remain_mutable(self) -> None:
for content in [
None,
{},
[],
[{"type": "text", "text": 7}],
[
{"type": "image_url", "image_url": {"url": "synthetic"}},
{"type": "text", "text": "A\nB"},
],
]:
with self.subTest(content=content):
messages = [{"role": "tool", "content": content}, self.messages[-1]]
self.assertEqual(self.cache.compute_frozen_count(messages), 0)
def test_supported_stable_hash_and_trailing_live_zone_are_unchanged(self) -> None:
cache = CompressionCache()
cache.mark_stable(cache.content_hash("stable"))
messages = [{"role": "tool", "content": "stable"}, self.messages[-1]]
self.assertEqual(cache.compute_frozen_count(messages), 1)
self.assertEqual(cache.compute_frozen_count(messages[:1]), 0)
if __name__ == "__main__":
unittest.main()