browser-state-recovery
Skill10 starsChanged 2 months agoApache-2.0View on GitHub
Recover from stale element references, navigation races and bridge reconnects — condition-based waiting, undo-based state restore, markAllStale session triage and snapshot comparisons to detect drift.
In this repository16 other files it gives its agents.
- browser-forensicsSkill
- browser-interactionSkill
- dom-inspectionSkill
- dom-mutationSkill
- dom-targetingSkill
- extension-troubleshootingSkill
- human-like-interactionSkill
- mcp-troubleshootingSkill
- page-annotationSkill
- project-captureSkill
- project-reconstructionSkill
- regression-testingSkill
- selector-recoverySkill
- tab-managementSkill
- viewport-testingSkill
- visual-dom-comparisonSkill
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 15
After any bridge outage/reconnect (extension reload, service-worker suspension, bridge restart).
- Very specific: line 26
Stale element references: run recover_selector {selector, snapshot} with the stored snapshot …
- Very specific: line 27
State restore via mutations: if YOUR changes are the drift, undo_dom_mutation {} walks the immutable inverse records (repeat per step, or …
- Very specific: line 60
Keep evidence: pull get_action_timeline and get_operation_trace for the failing operations before any restart — the in-memory timeline does …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Skill: Browser State Recovery
PURPOSE
Re-establish a trustworthy working state after the page moved under you: elements went stale, a navigation raced your command, or the bridge dropped and reconnected. The platform model makes staleness explicit (tabs OPEN/CLOSED/STALE) and gives you the tools to wait, restore and detect drift — never to guess.
WHEN TO USE
- TARGET_STALE / TARGET_NOT_FOUND errors mid-workflow that used to work.
- A tool succeeded but the page looks wrong (navigation or re-render happened between resolution and action).
- After any bridge outage/reconnect (extension reload, service-worker suspension, bridge restart).
- Long sessions where you suspect background app behavior changed the DOM.
- Resuming an interrupted session: detect what changed while you were gone before acting on cached assumptions.
PREREQUISITES
- MCP server running; the page (or its successor) still reachable via extension or Node simulation.
- Ideally: stored TARGET snapshots (from generate_element_target / region annotations) and captured page states from before the trouble.
WORKFLOW
- Classify the failure first.
get_browser_session {}— tab statuses tell the story: OPEN (fine), CLOSED (gone for good), STALE (reference suspect after a reconnect; the session model marks all open tabs STALE on bridge loss — markAllStale — and a re-registered tab flips back to OPEN with refreshed lastSeenAt). - Navigation races: re-establish readiness with
wait_for_condition—kind: "readiness_state"(defaultcomplete),kind: "url_contains"(did navigation land where expected?),kind: "dom_stable"(two consecutive matching DOM observations), orkind: "selector_present"/"selector_visible"for the element you actually need. Then retry the original command once. If a selector suddenly works again,recover_selectorwill reportstrategy: "original-selector", confidence: 1.0— the earlier failure was a transient navigation/render race. - Stale element references: run
recover_selector {selector, snapshot}with the stored snapshot (tag/text/classes/stableAttributes/fingerprintHash/parentSelector). Safe recovery (≥0.62 confidence, ≥0.15 margin) yields aresolvedSelector; refusals come back with alternatives and diagnostics — pick manually or re-target viasearch_dom+generate_element_target. - State restore via mutations: if YOUR changes are the drift,
undo_dom_mutation {}walks the immutable inverse records (repeat per step, ormutate_dom_transaction {mode: "rollback", reason}for a scoped set). Checkget_mutation_history {limit}for undo/redo depth first. - Drift detection:
capture_page_state {}now vs. your last pre-failure snapshot, thencompare_page_states {snapshotIdA, snapshotIdB}— field-level diffs (url, title, domHash, domLength, interactiveCount, viewport, pendingMutations) plusdomDelta. A changeddomHashwith identical length means re-rendered equivalent; a changedurlmeans navigation. - Tabs:
list_tabs {}to see the real current set; re-focus_tabyour page if the active tab moved; treat STALE identities as untrusted until re-registered. - Resume the workflow from the last verified state — re-resolve targets, re-capture state, then continue.
TOOLS
wait_for_condition, wait_for_dom_stable, recover_selector, diagnose_selector_failure, generate_element_target, undo_dom_mutation, mutate_dom_transaction, get_mutation_history, capture_page_state, compare_page_states, get_browser_session, list_tabs, focus_tab, get_action_timeline
EXAMPLES
tools/call get_browser_session {}
tools/call wait_for_condition { "kind": "readiness_state", "state": "complete", "timeoutMs": 10000 }
tools/call recover_selector { "selector": "#old-submit", "snapshot": { "tag": "button", "text": "Sign in", "stableAttributes": { "data-testid": "login-submit" } } }
tools/call undo_dom_mutation {}
tools/call compare_page_states { "snapshotIdA": "snap_a1", "snapshotIdB": "snap_b2" }FAILURE MODES
- TARGET_STALE — element removed since resolution; classic after SPA re-renders. Recover via snapshot, don't blind-retry the click.
- markAllStale aftermath — after a bridge reconnect all tabs show STALE; acting through them is unreliable until the tab re-registers (any page activity) or you re-list and re-focus.
- Wait condition never satisfied (
satisfied: false) — the expected state genuinely isn't coming (navigation failed, selector is wrong); diagnose instead of re-waiting (max timeout 30000). - Undo "nothing to undo" — the mutations were made in a different document context (engines are per-document) or predate the current session; fall back to
reload_tab {bypassCache: true}. - Snapshot drift with no explanation — compare only covers the digest fields; drill into
get_live_dom_subtreeordiff_dom(on a recorded session) for structural detail.
VALIDATION
- Post-recovery:
get_browser_sessionshows your target tab OPEN and active;inspect_live_pagereturns the expected url/title. - Recaptured
capture_page_statematches the pre-failure snapshot (or the diff is exactly the change you intended). get_mutation_historyundo depth decreased by exactly the number of undos you ran — anything else means an app re-render interleaved.- Re-resolved targets carry fresh
resolvedFrom(selector/xpath, not coordinates) and healthy confidence.
RECOVERY
- Total loss of orientation:
reload_tab {bypassCache: true}for a pristine page, re-run your targeting fromsearch_dom, and treat prior element references as dead. - Bridge-level suspicion: run the mcp-troubleshooting health check before blaming page state — simulated fallback responses (
simulated: true) mean you are not looking at the real page at all. - Keep evidence: pull
get_action_timelineandget_operation_tracefor the failing operations before any restart — the in-memory timeline does not survive a server restart.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

