Skip to content

SnapshotAgent: Add normalizeBody and normalizeQuery#5121

Merged
mcollina merged 1 commit into
nodejs:mainfrom
GeoffreyBooth:snapshot-agent-normalize-body-and-query-params
May 5, 2026
Merged

SnapshotAgent: Add normalizeBody and normalizeQuery#5121
mcollina merged 1 commit into
nodejs:mainfrom
GeoffreyBooth:snapshot-agent-normalize-body-and-query-params

Conversation

@GeoffreyBooth
Copy link
Copy Markdown
Member

@GeoffreyBooth GeoffreyBooth commented Apr 26, 2026

Adds two new options to SnapshotAgent and SnapshotRecorder for partial request matching:

  • normalizeBody(body) — normalizes the request body before hashing, e.g. to strip volatile fields like timestamps from JSON payloads
  • normalizeQuery(params) — normalizes query parameters (as URLSearchParams) before hashing, e.g. to strip cache-busting params

Both options pair with the existing matchBody/matchQuery boolean toggles and run at both record and playback time so hashes stay consistent across both sides.

This relates to…

SnapshotAgent; #4114

Rationale

When recording snapshots, sometimes we don’t want to match on the body or query params exactly; there might be volatile parts of the body or query params to exclude such as timestamps or cache-busters. In my case I want to match on the system prompt of an LLM call, ignoring the rest of the body.

Changes

Adds normalizeBody and normalizeQuery options to SnapshotAgent, for users to define functions to simplify the recorded request body or query params before saving; and to run again when computing the hash for matching.

Features

Adds normalizeBody and normalizeQuery options to SnapshotAgent.

Bug Fixes

N/A

Breaking Changes and Deprecations

N/A

Status

I tested this branch against my project and it works as intended.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.27%. Comparing base (a07d945) to head (45a60e7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5121      +/-   ##
==========================================
- Coverage   93.28%   93.27%   -0.01%     
==========================================
  Files         110      110              
  Lines       36313    36350      +37     
==========================================
+ Hits        33873    33906      +33     
- Misses       2440     2444       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GeoffreyBooth GeoffreyBooth force-pushed the snapshot-agent-normalize-body-and-query-params branch from d047f55 to c1e7ba2 Compare May 4, 2026 00:34
@GeoffreyBooth GeoffreyBooth marked this pull request as ready for review May 4, 2026 00:36
@trivikr trivikr requested a review from mcollina May 4, 2026 01:26
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Can you add a test for the types changes?

Adds two new options to SnapshotAgent and SnapshotRecorder for partial
request matching:

- `normalizeBody(body)` — normalizes the request body before hashing,
  e.g. to strip volatile fields like timestamps from JSON payloads
- `normalizeQuery(params)` — normalizes query parameters (as
  URLSearchParams) before hashing, e.g. to strip cache-busting params

Both options pair with the existing `matchBody`/`matchQuery` boolean
toggles and run at both record and playback time so hashes stay
consistent across both sides.
@GeoffreyBooth GeoffreyBooth force-pushed the snapshot-agent-normalize-body-and-query-params branch from c1e7ba2 to 45a60e7 Compare May 4, 2026 18:30
@GeoffreyBooth
Copy link
Copy Markdown
Member Author

Can you add a test for the types changes?

Done

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit ae8df16 into nodejs:main May 5, 2026
34 of 35 checks passed
@GeoffreyBooth GeoffreyBooth deleted the snapshot-agent-normalize-body-and-query-params branch May 5, 2026 15:53
@github-actions github-actions Bot mentioned this pull request May 14, 2026
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.

3 participants