fix: harden /browse lifecycle and ref safety#5
Open
morluto wants to merge 9 commits intogarrytan:mainfrom
Open
fix: harden /browse lifecycle and ref safety#5morluto wants to merge 9 commits intogarrytan:mainfrom
morluto wants to merge 9 commits intogarrytan:mainfrom
Conversation
Clean up browse daemon lifecycle, freeze refs per tab, and add regression coverage for restart, cookie, network, and snapshot edge cases.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Wait for the requested daemon pid to exit instead of treating raw state-file existence as part of shutdown completion.
…ness-patch # Conflicts: # CHANGELOG.md # browse/src/cli.ts
|
@dependabot close |
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.
What This PR Does
/browsedaemon lifecycle sostopandrestartcomplete deterministically, startup is serialized, recreated state files do not wedge shutdown, and concurrent shells do not race a replacement daemonElementHandles so refs do not leak across tabs or drift after SPA rerendersnth()bookkeeping for-dand-cfillandselect, quoted accessible names, and same-URL network attributionContext
The original
/browseimplementation had a few correctness problems that were acceptable in a single-shell happy path but broke down under real agent usage:stopandrestartcould report success while the daemon was still in an ambiguous shutdown stateThe common theme is that wrong-target behavior is worse than an ordinary failure because it looks successful while mutating the wrong tab, element, or daemon state.
Implementation Overview
1. Snapshot refs and DOM targeting
BrowserManagernow stores snapshot refs per tab as frozenElementHandles instead of one globalLocatormaprun snapshotguidancesnapshotnow resolves a concrete handle before storing a ref, so refs only exist for resolvable nodes-d,-c, or-istill advance same-role/name occurrence tracking when they affect laternth()disambiguation2. Daemon lifecycle and startup serialization
stopandrestartreturn their HTTP response first, then schedule shutdown on the next event-loop turnstoporrestartcomplete3. Command and capture correctness
cookieaccepts an explicit origin so it works before first navigation instead of failing onabout:blankfillandselectnow accept explicit empty-string valuesRequestidentity instead of reverse-scanning by URL, so same-URL requests keep the right status/size/duration pairingReviewer Guide
Suggested review order:
browse/src/cli.ts,browse/src/server.ts,browse/src/meta-commands.tsLifecycle, startup locking, stop/restart semantics, state ownership
browse/src/browser-manager.ts,browse/src/snapshot.tsRef model migration, stale-ref handling, request-identity tracking
browse/src/write-commands.ts,browse/src/read-commands.tsRef consumers and command-semantic fixes
browse/test/commands.test.ts,browse/test/snapshot.test.tsRegression coverage for each failure mode fixed here
Risk / Compatibility
/browseref semantics: stale refs now fail instead of silently drifting to a different elementstopandrestartwait for a real shutdown boundary rather than treating socket drop as successVerification
bun test browse/test/commands.test.ts browse/test/snapshot.test.ts./setupsanity check on the merged branch