-
Notifications
You must be signed in to change notification settings - Fork 1k
cdp debug logging [STG-593] #916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
uses placeholder session and message IDs and leads to duplicate entries. TODO stderr watching
|
invoked with DEBUG=sh:api added to default example script
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Summary
This PR introduces a comprehensive CDP (Chrome DevTools Protocol) debug logging system to help developers distinguish between CDP calls made by Stagehand versus Playwright directly. The implementation adds a new debug namespace sh:protocol
that can be used alongside Playwright's existing debug logs.
The core functionality works by:
- Automatic protocol enabling: When
sh:protocol
is specified in DEBUG, it automatically enables Playwright'spw:protocol
logging - stderr interception: The system intercepts
process.stderr.write
to rewrite Playwright's protocol logs, changingpw:protocol
tosh:protocol
for calls that originated from Stagehand code - Call tracking: A
markStagehandCDPCall()
function tracks pending CDP calls with method names and session IDs to correlate with actual protocol messages - Integration points: The debug markers are strategically placed throughout the codebase in
StagehandPage.ts
,StagehandContext.ts
,a11y/utils.ts
, and the mainindex.ts
Additionally, the PR includes:
- A new
createStagehandApiLogger()
utility that formats Stagehand logs to match Playwright's debug output format withsh:api
namespace - Example files demonstrating the debug functionality
- Formatting improvements applied by Prettier across several test files
- Addition of
*.log
to.gitignore
This enhancement allows developers to use environment variables like DEBUG=pw:api,pw:browser*,pw:protocol*,sh:protocol
to get unified debug output where they can clearly see which CDP operations are initiated by Stagehand's high-level abstractions versus Playwright's internal operations.
Confidence score: 3/5
• This PR introduces complex global state mutations and stderr interception that could cause unexpected behavior in production environments
• The implementation has several potential race conditions in call tracking logic and lacks robust error handling around the stderr interception
• Files needing more attention: lib/debug.ts
(global process modifications), lib/StagehandPage.ts
(integration points), lib/index.ts
(CDP call integration)
9 files reviewed, 1 comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Summary
This review covers only the changes made since the last review (commit e4f1e10), not the entire PR. The latest change simplifies the example script by removing the explicit API logging configuration. The developer removed the createStagehandApiLogger()
import and its usage from the Stagehand initialization in examples/example.ts
, leaving only the spread StagehandConfig
. This change aligns with the PR's goal of consolidating debug logging into a unified system that uses environment variables (DEBUG=sh:protocol
) to automatically intercept and mark Stagehand-originated CDP calls within Playwright's existing debug infrastructure.
This modification improves the developer experience by eliminating the need to manually configure API logging in examples while maintaining the sophisticated debugging capabilities through the new debug system that can distinguish between Playwright and Stagehand protocol calls.
Confidence score: 5/5
- This PR is safe to merge with minimal risk
- Score reflects simple cleanup changes that remove outdated manual logging configuration in favor of the new automated debug system
- No files require special attention
1 file reviewed, no comments
to generate CDP message logs with this, set the DEBUG env var:
to use the logger function that formats stagehand method logs into the same format (labeled
you can generate a more readable version of the logs with this one liner:
|
why
make the relationship between stagehand and playwright's APIs easier to debug
what changed
added a debug logger type
sh:protocol
that can be invoked along with playwright debug logs with an envvar e.g.DEBUG=pw:api,pw:browser*,sh:protocol