fix(sdk): replace a service worker left over from a different extension build

MI
5h ago
+1445-41

why

Chrome keeps an extension's service worker in the profile under its ID, which an unpacked extension derives from its path. When a profile that ran one build loads another from the same path, the old worker keeps running next to the new content script, even across a manifest version bump. The runtime marker cannot tell them apart.

what changed

  • The extension build stamps an identity into the service worker (__stagehand_build_id) and writes it to build-id.txt.
  • After attaching, the TypeScript and Python SDKs compare the two. If the SDK loaded the extension, or finds it installed from its own directory, it loads it again from that directory, which replaces the worker and keeps the extension's own storage. Otherwise it warns and continues. A missing file, an unstamped identity or a failed read is not compared.
  • If that load fails, or does not clear the mismatch, the SDK warns and continues with the running worker.
  • Builds on #3501 (merged), the content-script injection change, since a repair reloads the extension while pages are open.
  • The Go SDK is out of scope.

test plan

  • Unit tests in both SDKs, an extension build test, and a parity test pinning the file name, global name and placeholder. The Python repair is covered by unit tests only.
  • A real-browser test loads build A, then B with a bumped manifest version, at one path and profile: B's connect finds A's worker, loads the extension again and ends on B, with no uninstall.
  • Real Chrome, both changes: reconnect after the directory changed, then navigation, snapshots, typing and clicks on open pages, after reload and in new tabs, 10 runs each.

Summary by cubic

Replaces a service worker a browser profile kept from a different extension build, and injects the content script into pages already open when the extension loads.

  • The extension build stamps an identity into the worker and writes build-id.txt. The TypeScript and Python SDKs compare the running worker with the file after attaching. If the SDK loaded the extension, or finds it installed from its own directory, it loads it again from that directory, which swaps in the new worker and keeps the extension's own storage; otherwise it warns and continues. The installed directory is matched by filesystem identity, falling back to normalized-path comparison when it cannot be read on the machine the SDK runs on, such as a browser on another host. A missing, empty, or unstamped identity is not compared, and a failed or timed-out read keeps the connection going.
  • On worker start, the service worker injects the content script into every frame of open http(s) and file tabs. The readiness check now also requires chrome.runtime.id to detect a stale copy. The Go SDK is out of scope.