Skip to content

feat: snapshot diffing AUTO-40#154

Merged
ahmedAmien merged 9 commits into
mainfrom
feat/snapshot-diffing
Jul 6, 2026
Merged

feat: snapshot diffing AUTO-40#154
ahmedAmien merged 9 commits into
mainfrom
feat/snapshot-diffing

Conversation

@andyMrtnzP

@andyMrtnzP andyMrtnzP commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds snapshot diffing for save tokens, allowing agents to request a full snapshot if needed

Related issues

Closes AUTO-40

Summary by CodeRabbit

  • New Features
    • Snapshot output is now more compact by showing diffs (new/changed/removed) after the first snapshot.
    • Added an optional full parameter to request a complete snapshot when prior context isn’t available.
  • Bug Fixes
    • Improved snapshot tracking so unchanged elements stay stable and diffs remain accurate even when element identifiers shift.
    • Enhanced snapshot selection during tab switching, hydration, and peeking scenarios to prevent incorrect diff context.
  • Tests
    • Added coverage for diffed snapshot formatting and identity/positional change detection.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds diff-based snapshot rendering with cached prior snapshot state, a full snapshot override, updated prompt/schema guidance, and tests for diff output and positional matching.

Changes

Snapshot Diff Feature

Layer / File(s) Summary
Session state fields for snapshot caching
src/@types/types.d.ts
ActiveSession gains optional lastElements and lastSnapshotElements fields for prior snapshot caching.
Snapshot diff helper functions
src/lib/agent-format.ts
Adds identity, diff assembly, change formatting, and positional diff helpers, and refactors frame label construction through a shared helper.
Schema and prompt updates for full snapshot option
src/tools/schemas.ts, src/skills/system-prompt.ts
Adds optional params.full validation for snapshot commands and updates the system prompt to describe diff snapshots and full snapshot requests.
Agent tool snapshot rendering integration
src/tools/agent.ts
Imports diff helpers and replaces snapshot rendering with diff-aware candidate selection, session cache updates, and full-snapshot gating.
Diff function test coverage
test/lib/agent-format.diff.spec.ts
Adds tests for omitted unchanged elements, identity collisions, frame labels, state changes, no-change output, positional diffs, and framework-id selector churn.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant browserless_agent
  participant agent-format.ts
  participant agentSession

  browserless_agent->>agent-format.ts: formatSnapshotDiff(snapshot, lastElements)
  browserless_agent->>agent-format.ts: formatSnapshotDiffPositional(lastSnapshotElements, snapshot)
  agent-format.ts-->>browserless_agent: candidate snapshot texts
  browserless_agent->>browserless_agent: choose shortest result or full snapshot
  browserless_agent->>agentSession: store lastElements, lastSnapshotElements
Loading

Possibly related PRs

Suggested reviewers: ashwinsingh2007

Poem

A little rabbit hops through diffs so neat,
With plus and tilde marching in beat.
A full snapshot waits if context is thin,
While the smallest text gets tucked right in.
🐰 Thump, thump — the changes shine!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description includes a summary and related issue, but it omits Changes, Test plan, and Checklist sections from the template. Add the missing Changes, Test plan, and Checklist sections, including verification steps or N/A entries where appropriate.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and matches the main change: snapshot diffing.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/snapshot-diffing

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lib/agent-format.ts`:
- Around line 291-309: The changed-element delta in agent-format.ts is missing
the actual field-level reason for updates detected by elementSignature, so a `~`
line can appear without showing what changed. Update the change rendering in the
delta builder that uses elementSignature, formatElement, and changeLines to
include the previous vs new values for the relevant fields (such as placeholder,
href, ariaLabel, type, and frameId) when an element is marked as changed. Keep
the existing add/remove behavior, but make the `changed` output explicitly
surface the difference so the formatted diff matches the detection logic.
- Around line 241-246: The current indexByIdentity helper uses Map construction,
which overwrites duplicate semantic keys and loses snapshot elements with the
same identity. Update indexByIdentity (and the related comparison logic around
the duplicate handling in agent-format.ts) to preserve all elements for a given
elementKey instead of last-wins, so repeated buttons/links are retained and can
be compared correctly. Use the existing elementKey and
SnapshotResult/SnapshotElement symbols to locate the affected code and adjust
any consumers that assume a single SnapshotElement per key.
- Around line 255-280: The diff snapshot output built by assembleDiff is missing
the frame legend and deep-ref guidance that full snapshots include, so agents
can see [frame#N] markers without the mapping. Update assembleDiff to carry over
the same frame-context metadata used in the full snapshot path, including the
frame legend and deep-ref guidance, and append it alongside the existing counts
and detected challenge lines.

In `@src/tools/agent.ts`:
- Around line 715-742: The snapshot diff logic in agentSession is using shared
caches across tab/target contexts, so peeks like snapshot { targetId } or tab
switches can diff against the wrong prior snapshot and then overwrite it. Update
the cache usage around formatSnapshotDiff/formatSnapshotDiffPositional so
lastElements and lastSnapshotElements are scoped by target/active target (or
bypass diffing and cache updates when the target context changes), and keep the
existing full snapshot fallback in agent.ts.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6c31376f-57e5-42b8-8095-c0d9176678df

📥 Commits

Reviewing files that changed from the base of the PR and between ac855c1 and ab50ff9.

📒 Files selected for processing (6)
  • src/@types/types.d.ts
  • src/lib/agent-format.ts
  • src/skills/system-prompt.ts
  • src/tools/agent.ts
  • src/tools/schemas.ts
  • test/lib/agent-format.diff.spec.ts

Comment thread src/lib/agent-format.ts Outdated
Comment thread src/lib/agent-format.ts
Comment thread src/lib/agent-format.ts Outdated
Comment thread src/tools/agent.ts Outdated

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (1)
test/lib/agent-format.diff.spec.ts (1)

57-63: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Test only checks the summary counts, not the actual removal line.

This test verifies the "1 removed" count but doesn't assert the rendered removal line itself (e.g. that the correct selector shows up in a - ref=... (removed) line). Since the whole point of this test is that removal isn't hidden by key collision, asserting the actual diff line content would make the test more robust against regressions where counts are right but the wrong element gets flagged.

Suggested strengthening
   const out = formatSnapshotDiff(snap([el(dup)]), prev);
   expect(out).to.match(/0 new, 0 changed, 1 removed/);
+  expect(out).to.match(/- ref=.*\(removed\)/);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/lib/agent-format.diff.spec.ts` around lines 57 - 63, The current test
for formatSnapshotDiff only asserts the summary count and can miss regressions
where the removed element is not rendered correctly. Strengthen the spec in
agent-format.diff.spec.ts around the duplicate-key case by also asserting the
actual removal line emitted by formatSnapshotDiff, using the same
dup/snap/indexByIdentity setup and checking for the expected “- ref=...
(removed)” content tied to the removed element.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@test/lib/agent-format.diff.spec.ts`:
- Around line 57-63: The current test for formatSnapshotDiff only asserts the
summary count and can miss regressions where the removed element is not rendered
correctly. Strengthen the spec in agent-format.diff.spec.ts around the
duplicate-key case by also asserting the actual removal line emitted by
formatSnapshotDiff, using the same dup/snap/indexByIdentity setup and checking
for the expected “- ref=... (removed)” content tied to the removed element.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d6b8a5b3-eeb9-4f46-912e-33aea87d32c8

📥 Commits

Reviewing files that changed from the base of the PR and between ab50ff9 and de8024c.

📒 Files selected for processing (2)
  • src/lib/agent-format.ts
  • test/lib/agent-format.diff.spec.ts

@andyMrtnzP andyMrtnzP changed the title feat: snapshot diffing feat: snapshot diffing AUTO-40 Jul 6, 2026
@ahmedAmien ahmedAmien merged commit 9bca439 into main Jul 6, 2026
6 checks passed
@ahmedAmien ahmedAmien deleted the feat/snapshot-diffing branch July 6, 2026 20:28
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.

2 participants