Add bounded CDP heartbeat and sanitized disconnect diagnostics - #2894
Open
miguelg719 wants to merge 10 commits into
Open
Add bounded CDP heartbeat and sanitized disconnect diagnostics#2894miguelg719 wants to merge 10 commits into
miguelg719 wants to merge 10 commits into
Conversation
🦋 Changeset detectedLatest commit: 05ca9da The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…solidation-04-cdp-diagnostics
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Architecture diagram
sequenceDiagram
participant App as Application
participant CDP as CDPClient
participant Sock as WebSocket
participant Diag as Diagnostics
participant Env as Environment Config
Note over App,Env: CDP Connection Lifecycle with Heartbeat
App->>CDP: new CDPClient(socket, wssUrl)
CDP->>Env: Read STAGEHAND_CDP_HEARTBEAT_MS
Env-->>CDP: Interval (default 20s)
CDP->>CDP: startHeartbeat()
loop Every heartbeatMs
CDP->>CDP: Check closed/OPEN/outstanding
alt No heartbeat pending
CDP->>CDP: Create AbortController + timeout
CDP->>Sock: sendCommand("Browser.getVersion")
Sock-->>CDP: Response (clears pending)
else Heartbeat pending
CDP->>CDP: Skip (single-flight)
end
Note over CDP: timeout unref'd, min(interval, 10s)
end
Note over App,CDP: Normal Command Flow
App->>CDP: sendCommand(method, params)
CDP->>Sock: Send JSON-RPC message
Sock-->>CDP: Message received
alt Valid message
CDP-->>App: Resolve response
else Invalid payload
CDP->>CDP: finishDrop(error, "error")
end
Note over CDP,Diag: Connection Drop Diagnostics
alt Socket error event
Sock-->>CDP: error event
CDP->>CDP: finishDrop(CDPConnectionClosedError, "error")
opt STAGEHAND_CDP_LOG = "1"
CDP->>Diag: logDrop() - sanitized metadata
Diag->>Diag: Redact URLs, tokens, bearer
Diag-->>CDP: Write stderr + optional file
end
CDP->>Sock: close()
CDP-->>App: onerror(reason) - once
else Socket close event
Sock-->>CDP: close event (code, reason)
CDP->>CDP: finishDrop(CDPConnectionClosedError, "close")
opt STAGEHAND_CDP_LOG = "1"
CDP->>Diag: logDrop() - sanitized
end
CDP-->>App: onclose(reason)
end
Note over CDP: stopHeartbeat() - clear interval<br/>abort pending, clear timers
alt Commands after close
App->>CDP: sendCommand()
CDP-->>App: Reject CDPConnectionClosedError
end
Note over App,CDP: Explicit Shutdown
App->>CDP: close()
CDP->>CDP: stopHeartbeat() + clear callbacks
CDP-->>App: Closed (no further events)
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
…solidation-04-cdp-diagnostics
…solidation-04-cdp-diagnostics
…solidation-04-cdp-diagnostics
…solidation-04-cdp-diagnostics
…solidation-04-cdp-diagnostics
…solidation-04-cdp-diagnostics
akeimach
approved these changes
Sep 9, 2026
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.
Adds configurable single-flight CDP heartbeat with bounded waits and lifecycle cleanup. Sanitized CDP_DROP diagnostics retain transport failure context without logging raw payloads or credentials.
Automatic reconnect is excluded. Error followed by close records the first failure once; heartbeat timers are unrefed and cleared during shutdown.
Nested transport causes are retained in a bounded, sanitized CDP_DROP diagnostic, without changing terminal behavior.
Validation: 49 CDP tests, 44 SDK AST guards and SDK typecheck/format checks; CDP lifecycle/heartbeat/drop-diagnostic unit tests.
Stack position: 4/17. Base:
evals/consolidation-03-capture-recovery. Each PR contains the change relative to its immediate predecessor.Reviewer entry points:
packages/sdk-ts/src/cdpClient.tspackages/sdk-ts/tests/cdpHeartbeat.test.tsReadiness: local validation is described above; GitHub CI and automated review feedback are pending. This PR is open as non-draft so automated reviewers can run.