Skip to content

agent(history): freeze HistoryItem + lock byte-prefix property - #4890

Merged
sauravpanda merged 1 commit into
mainfrom
prompt-cache/history-item-stable
May 23, 2026
Merged

sauravpanda merged 1 commit into
mainfrom
prompt-cache/history-item-stable

Conversation

@sauravpanda

@sauravpanda sauravpanda commented May 23, 2026 •

Copy link
Copy Markdown
Contributor

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

  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

  • New byte-stability tests pass (pytest tests/ci/test_history_item_byte_stable.py).
  • Existing message_manager / history tests still pass (pytest tests/ci -k 'message_manager or history').
  • pyright + ruff clean via pre-commit.

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 #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.

Written for commit 8236cc7. Summary will update on new commits. Review in cubic

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.
@github-actions

Copy link
Copy Markdown

Agent Task Evaluation Results: 2/2 (100%)

View detailed results
Task Result Reason
browser_use_pip ✅ Pass Skipped - API key not available (fork PR or missing secret)
amazon_laptop ✅ Pass Skipped - API key not available (fork PR or missing secret)

Check the evaluate-tasks job for detailed task execution logs.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

@sauravpanda
sauravpanda merged commit 67f7b9e into main May 23, 2026
98 checks passed
@sauravpanda
sauravpanda deleted the prompt-cache/history-item-stable branch May 23, 2026 01:09
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. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant