Repository navigation
agent(history): freeze HistoryItem + lock byte-prefix property - #4890
Merged
Merged
Conversation
For Gemini's implicit cache (and similar provider caches) to actually hit step over step, the rendered transcript for steps 1..N-1 must be byte-identical at step N and at step N+1. The agent already appends HistoryItems immutably in practice, but nothing in the type prevented a future caller from mutating one in place — which would silently kill the cache from that byte onward. Mark HistoryItem frozen so any mutation now fails loud at runtime rather than slowly burning input tokens. Add a regression test set that asserts the cache property directly: - render(items[:N]) must be a strict byte-prefix of render(items[:N+1]) - to_string() is deterministic for identical inputs - the prefix property holds across mixed entry shapes (normal steps, errors, system messages, follow-up tasks) - conditional field inclusion doesn't collapse to ambiguous output Known limitation, not addressed here: max_history_items compaction in MessageManager.agent_history_description rewrites earlier bytes once the cap is exceeded (the omitted-count message changes). That's a larger redesign and deserves its own PR.
Agent Task Evaluation Results: 2/2 (100%)View detailed results
Check the evaluate-tasks job for detailed task execution logs. |
MagMueller
added a commit
that referenced
this pull request
May 23, 2026
Keeps HistoryItem frozen while removing the expanded byte-prefix explanation and byte-stability regression test added in #4890. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Keep `HistoryItem` frozen but trim docs and tests. Removes the byte-prefix caching explanation and drops the byte-stability regression test from #4890. - **Refactors** - Shortened the `HistoryItem` docstring to a single line. - Removed `tests/ci/test_history_item_byte_stable.py` to stop enforcing byte-prefix rendering in unit tests. <sup>Written for commit 51598ef. Summary will update on new commits. <a href="https://cubic.dev/pr/browser-use/browser-use/pull/4902?utm_source=github">Review in cubic</a></sup> <!-- End of auto-generated description by cubic. -->
r266-tech
pushed a commit
to r266-tech/browser-use
that referenced
this pull request
May 26, 2026
…er-use#4890) ## Context Closes part of browser-use#4887 (item browser-use#1 — make the per-step transcript append-only). For Gemini's implicit cache (and similar provider caches) to actually hit step over step, the rendered transcript for steps 1..N-1 must be byte-identical at step N and at step N+1. The agent today appends `HistoryItem`s immutably in practice — every site I checked constructs a new item and `.append()`s it — but nothing in the type itself prevents a future caller from mutating one in place. That kind of mutation would silently kill the cache from that byte onward, and we'd only notice it as a slow erosion of cached-token-ratio. ## Change 1. Mark `HistoryItem` `frozen=True` via pydantic `ConfigDict`, so any mutation fails loud at runtime instead of silently burning input tokens later. 2. Add a regression test set (`tests/ci/test_history_item_byte_stable.py`) that asserts the cache property directly: - `render(items[:N])` is a strict byte-prefix of `render(items[:N+1])` - `to_string()` is deterministic for identical inputs - the prefix property holds across mixed entry shapes (normal steps, errors, system messages, follow-up tasks) - conditional field inclusion in `to_string()` doesn't collapse to ambiguous output (a populated field shouldn't render identically to a missing one) The tests pass against current behavior — they're locking it down, not changing it. ## Known limitation (intentionally not in this PR) `MessageManager.agent_history_description` does have one real cache-buster I noticed during the audit: when `max_history_items` is set and exceeded, the compaction logic at `message_manager/service.py:173-186` rewrites earlier bytes — the `[... N previous steps omitted...]` count changes every step past the cap, so the prefix is not stable past the boundary. Fixing that properly means either (a) capping the omitted-count at a stable value, or (b) restructuring the compaction to omit at a fixed cutoff. Either way it's a bigger design call than this hygiene PR, so I left it for a follow-up. Calling it out in the commit message. ## Test plan - [x] New byte-stability tests pass (`pytest tests/ci/test_history_item_byte_stable.py`). - [x] Existing message_manager / history tests still pass (`pytest tests/ci -k 'message_manager or history'`). - [x] `pyright` + `ruff` clean via pre-commit. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Freeze `HistoryItem` to make agent step transcripts append-only and preserve byte-prefix stability for provider caches (e.g., Gemini). Adds regression tests to lock deterministic rendering and strict prefix growth. Part of browser-use#4887. - **Refactors** - Set `frozen=True` in `pydantic` `ConfigDict` for `HistoryItem`; mutation now raises instead of silently changing rendered bytes. - Added regression tests to enforce: strict byte-prefix growth across steps, deterministic `to_string()`, stability with mixed entry shapes, and disambiguation of optional fields. <sup>Written for commit 8236cc7. Summary will update on new commits. <a href="https://cubic.dev/pr/browser-use/browser-use/pull/4890?utm_source=github">Review in cubic</a></sup> <!-- End of auto-generated description by cubic. -->
r266-tech
pushed a commit
to r266-tech/browser-use
that referenced
this pull request
May 26, 2026
Keeps HistoryItem frozen while removing the expanded byte-prefix explanation and byte-stability regression test added in browser-use#4890. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Keep `HistoryItem` frozen but trim docs and tests. Removes the byte-prefix caching explanation and drops the byte-stability regression test from browser-use#4890. - **Refactors** - Shortened the `HistoryItem` docstring to a single line. - Removed `tests/ci/test_history_item_byte_stable.py` to stop enforcing byte-prefix rendering in unit tests. <sup>Written for commit 51598ef. Summary will update on new commits. <a href="https://cubic.dev/pr/browser-use/browser-use/pull/4902?utm_source=github">Review in cubic</a></sup> <!-- End of auto-generated description by cubic. -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Closes part of #4887 (item #1 — make the per-step transcript append-only).
For Gemini's implicit cache (and similar provider caches) to actually hit step over step, the rendered transcript for steps 1..N-1 must be byte-identical at step N and at step N+1. The agent today appends
HistoryItems immutably in practice — every site I checked constructs a new item and.append()s it — but nothing in the type itself prevents a future caller from mutating one in place. That kind of mutation would silently kill the cache from that byte onward, and we'd only notice it as a slow erosion of cached-token-ratio.Change
HistoryItemfrozen=Truevia pydanticConfigDict, so any mutation fails loud at runtime instead of silently burning input tokens later.tests/ci/test_history_item_byte_stable.py) that asserts the cache property directly:render(items[:N])is a strict byte-prefix ofrender(items[:N+1])to_string()is deterministic for identical inputsto_string()doesn't collapse to ambiguous output (a populated field shouldn't render identically to a missing one)The tests pass against current behavior — they're locking it down, not changing it.
Known limitation (intentionally not in this PR)
MessageManager.agent_history_descriptiondoes have one real cache-buster I noticed during the audit: whenmax_history_itemsis set and exceeded, the compaction logic atmessage_manager/service.py:173-186rewrites earlier bytes — the[... N previous steps omitted...]count changes every step past the cap, so the prefix is not stable past the boundary.Fixing that properly means either (a) capping the omitted-count at a stable value, or (b) restructuring the compaction to omit at a fixed cutoff. Either way it's a bigger design call than this hygiene PR, so I left it for a follow-up. Calling it out in the commit message.
Test plan
pytest tests/ci/test_history_item_byte_stable.py).pytest tests/ci -k 'message_manager or history').pyright+ruffclean via pre-commit.Summary by cubic
Freeze
HistoryItemto make agent step transcripts append-only and preserve byte-prefix stability for provider caches (e.g., Gemini). Adds regression tests to lock deterministic rendering and strict prefix growth. Part of #4887.frozen=TrueinpydanticConfigDictforHistoryItem; mutation now raises instead of silently changing rendered bytes.to_string(), stability with mixed entry shapes, and disambiguation of optional fields.Written for commit 8236cc7. Summary will update on new commits. Review in cubic