Playwright Agent Etiquette
Version 1.0 | Browser coordination rules for multi-agent environments
The Golden Rules
- Reserve before you touch - Never use Playwright MCP without a reservation
- Release when you're done - Don't hoard contexts
- Heartbeat during long tasks - Let others know you're alive
- Check before complex operations - Verify you still have access
- Share sparingly - Only when truly needed
Before Using Playwright
EVERY agent MUST:
1. Call `check <your-agent-id>` to see if you have access
2. If no access, call `reserve <your-agent-id> <purpose>`
3. If reservation fails, WAIT or find alternative approach
4. Only then use Playwright MCP tools
Reservation Protocol
// Pseudo-code every agent should follow
async function beforePlaywrightWork(agentId: string, purpose: string) {
// Step 1: Check existing access
const access = await coordinator.check(agentId);
if (access.canUse) {
return access.contextId; // Good to go
}
// Step 2: Try to reserve
const reservation = await coordinator.reserve(agentId, purpose);
if (reservation.success) {
return reservation.contextId;
}
// Step 3: Handle waiting
if (reservation.waitPosition) {
console.log(`Queue position: ${reservation.waitPosition}`);
console.log(`Estimated wait: ${reservation.estimatedWaitMs}ms`);
// DECISION POINT: Wait or use alternative?
// - If task is urgent: wait and retry
// - If task can be deferred: skip browser work
// - If task has alternative: use that instead
}
throw new Error('Could not acquire browser context');
}
Allowed vs Forbidden Actions
YOU MAY
- Reserve a context for legitimate browser work
- Hold a context for up to 5 minutes of active use
- Send heartbeats during long operations
- Share your context with ONE other agent for collaboration
- Release and re-reserve if your task changes significantly
YOU MUST NOT
- Use Playwright without checking/reserving first
- Hold a context while waiting for user input
- Hold a context while doing non-browser work
- Reserve multiple contexts simultaneously
- Ignore the queue when contexts are unavailable
- Share contexts for parallel independent work (use separate reservations)
Task Duration Guidelines
| Task Type | Max Hold Time | Heartbeat Frequency |
|---|---|---|
| Quick check (screenshot, status) | 30 seconds | Not needed |
| Form interaction | 2 minutes | Every 30 seconds |
| Multi-step flow | 5 minutes | Every 30 seconds |
| Complex testing | 5 minutes, then re-reserve | Every 30 seconds |
If your task exceeds 5 minutes:
- Break it into phases
- Release between phases
- Re-reserve for each phase
- This allows other agents to get access
Handling Contention
When reserve returns waitPosition:
Option A: Wait (for critical browser work)
while (!reserved) {
wait(30 seconds)
retry reserve
}
Option B: Defer (for non-critical work)
log("Browser busy, will retry later")
continue with non-browser tasks
schedule browser work for later
Option C: Alternative (when possible)
// Instead of browser screenshot
use API to fetch data
use cached data
ask user to check manually
Sharing Protocol
Sharing is for collaboration, not parallelism.
Valid Sharing Scenarios
- Agent A logs in, Agent B needs to continue that session
- Agent A fills a form, Agent B needs to verify the result
- Debugging requires two agents looking at same state
Invalid Sharing Scenarios
- Two agents doing independent work "to save time"
- Sharing to avoid waiting in queue
- Sharing without time limit
How to Share
Owner agent:
# Share for 2 minutes (120000ms)
npx ts-node cli.ts share agent-owner agent-helper 120000
Helper agent:
# Check access (will see shared context)
npx ts-node cli.ts check agent-helper
# Returns: { canUse: true, contextId: "context_3", isShared: true }
The owner is responsible for releasing when ALL work is done.
Error Recovery
Agent Crashes Mid-Work
- Contexts auto-cleanup after 10 minutes of no heartbeat
- Stale locks auto-release after 30 seconds
- Waiting agents get notified when contexts free up
Lock Timeout
If you see "Could not acquire lock - try again":
1. Wait 1 second
2. Retry the command
3. If persistent (>5 retries), check for zombie processes
Manual Cleanup
If an agent dies without releasing:
# Force release all contexts for that agent
npx ts-node cli.ts cleanup dead-agent-id
Quick Reference
# Before browser work
npx ts-node cli.ts check my-agent-id
# Reserve a context
npx ts-node cli.ts reserve my-agent-id "purpose description"
# Keep alive during long work
npx ts-node cli.ts heartbeat my-agent-id
# When done
npx ts-node cli.ts release my-agent-id
# Check pool status
npx ts-node cli.ts status
# Emergency cleanup
npx ts-node cli.ts cleanup stuck-agent-id
Integration with Claude Agents
For Claude Code agents, add to your workflow:
## Browser Work Protocol
Before any Playwright MCP calls:
1. Reserve: `coordinator reserve <session-id> <purpose>`
2. Verify: Check the result for success
3. Work: Do your browser tasks
4. Heartbeat: If >30s, send heartbeats
5. Release: Always release when done
If reservation fails with waitPosition:
- Inform user: "Browser pool busy, position X in queue"
- Decide: Wait, defer, or use alternative
FAQ
Q: What if I forget to release? A: Auto-cleanup runs every minute. After 10 minutes of no activity, your context is reclaimed. But don't rely on this - it delays other agents.
Q: Can I reserve "just in case"? A: No. Only reserve when you're about to do browser work. Reserve-and-hold wastes shared resources.
Q: What's my agent ID? A: Use your session ID, task ID, or any unique identifier that persists for your work session.
Q: How do I know if another agent is using my target page? A: You don't - browser contexts are isolated. Each agent gets separate browser state. Sharing creates explicit collaboration, not implicit state sharing.