Repository navigation
refactor(agent): preserve Stagehand client leases - #20451
Conversation
Constraint: Preserve cached clients until session cleanup completes. Rejected: Closing on eviction | active sessions can still use the client. Confidence: high Scope-risk: narrow Directive: Keep cache leases around every Stagehand operation. Tested: component tests, race tests, build, Compose, concurrent API smoke. Not-tested: GPU runtime.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (1)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review. 📝 WalkthroughWalkthroughThe Stagehand runtime serializes client acquisition and eviction. Task and extraction operations use client leases. Retired clients close after their active leases are released. ChangesStagehand client lease lifecycle
Priority: ⬇️ Low Estimated code review effort: 3 (Moderate) | ~25 minutes Change: Bug fix Sequence Diagram(s)sequenceDiagram
participant RunTask
participant stagehandRuntime
participant StagehandClient
RunTask->>stagehandRuntime: leaseClient
stagehandRuntime-->>RunTask: client and release function
RunTask->>StagehandClient: run task
stagehandRuntime->>stagehandRuntime: retire cached entry on eviction
RunTask->>stagehandRuntime: release lease
RunTask->>StagehandClient: close after final lease
Suggested reviewers: Merge Risk: ⚪ Minimal · up to No actionable merge-blocking issue is established; the change is mergeable after normal checks. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change protects active browser operations from premature cleanup without adding an observed operation or permission. Active clients can now survive eviction and shutdown, so resource limits and shutdown integration remain important. End-to-end access controls were not established by this review. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. A rabbit checks the client cache, Comment |
| r.cache.Range(func(k, v any) bool { | ||
| e := v.(*stagehandClientEntry) | ||
| if e.lastUsedAt.Load() >= cutoff { | ||
| return true // still fresh |
| removed = append(removed, e) | ||
| } | ||
| } | ||
| return true |
There was a problem hiding this comment.
Is r.mu unlocked here?
Tracking lock is hard. Suggest add a helper function which lock at the beginning, then defer unlock immediately.
Stagehand clients could be closed by TTL or LRU eviction while an active session still used them. This adds leases so eviction and runtime shutdown defer closing until every operation releases the client.