0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00
Commit graph

71 commits

Author SHA1 Message Date
dane-ai-mastra[bot]
d60ba71be0 chore: version - exit prerelease mode 2026-11-12 21:12:29 +00:00
dane-ai-mastra[bot]
6e8dfcb83f chore: version - exit prerelease mode 2026-11-03 23:45:58 +00:00
dane-ai-mastra[bot]
77076a1012 chore: version - exit prerelease mode 2026-10-30 23:02:25 +00:00
mastra-platform[bot]
422910cb67
fix(browser): stop stale launcher logs and orphaned Chrome from breaking browser relaunch (#28675)
> Opened by @user_01KNA8J74D4RF84RXSDRX26BS9

> Opened by @user_01KNA8J74D4RF84RXSDRX26BS9

## Summary

Fixes #28664

Once the Stagehand browser had run on a profile, every later launch on
that profile could fail with `ECONNREFUSED 127.0.0.1:<port>`. Killing
Chrome didn't fix it. The only fix was deleting the profile, which also
wipes saved logins.

**Primary cause:** chrome-launcher opens `<profile>/chrome-err.log` in
append mode. It finds the DevTools port by taking the *first* `DevTools
listening on ws://…:<port>/` match in the whole file. On a reused
profile, that match is the port from the earliest launch, which is dead
by now. (Diagnosis and real-Chrome repro by @NikAiyer.)

**Secondary cause:** Mastra Code never closed the Chrome it launched. It
didn't on exit, on relaunch, or when `/browser` replaced or disabled it.
A leftover Chrome could keep holding the profile.

**Changes:**

- **`@mastra/core`** (minor, new API):
- Before launching on a local profile, a new `prepareLocalProfile()`
step checks the profile's Chrome lock.
    - A stale lock is removed.
- A live holder throws the new exported `BrowserProfileInUseError`,
which names the profile and the PID.
- A live local PID that hasn't started listening on the socket yet
(Chrome creates the lock first) gets up to 3s before its lock counts as
stale.
- Only after that check passes does it delete `chrome-err.log` and
`chrome-out.log`.
    - The step is skipped on Windows and for remote browsers.
- Close and disconnect only remove the lock when no Chrome is listening,
so a late close can't delete the lock of a browser that took the profile
meanwhile.
- New `MastraBrowser.retire()` closes a browser for good, so later
`ensureReady()` and `launch()` calls throw.
- New `MastraBrowser.killProcesses()` SIGKILLs the browser's process
groups.
- **`@mastra/stagehand`**: thread-scoped browsers also run
`prepareLocalProfile()` before each thread's Chrome starts, when the
installed core has it. The core peer range stays `>=1.80.0-0`; on 1.80.0
Stagehand behaves as before instead of throwing.
- **`mastracode`**:
- It retires the active browser on exit, on relaunch, and when
`/browser` replaces or disables it. If the retire fails or takes longer
than 5s, it kills the browser's processes.
- A generation guard stops a `/browser` call that lost a race from
attaching its browser or saving settings.

## Proof

- Core `browser.test.ts`: 36/36 pass. Each of these fails when its fix
is removed:
  - relaunch reading the second port
  - stale logs cleared before `doLaunch()`
- stale lock and live holder handling, including a SIGKILLed holder that
left its socket behind (`ECONNREFUSED`)
- a live holder that starts listening 300ms after locking is treated as
holding the profile
  - a late close leaving the new holder's lock alone
- retire blocking a relaunch that is already waiting in `ensureReady()`
- `killProcesses()` is tested against a real detached process group.
- Stagehand unit tests: 70/70 pass. The thread-scope profile-prep test
and the "core without `prepareLocalProfile()`" test each fail without
their change.
- `mastracode/tui` `/browser` and lifecycle tests: 56/56 pass. The race,
close-order and kill-on-hang tests each fail when their fix is removed.
- @NikAiyer ran real Chrome on macOS at `0204188b` and `ff7acb14`,
including the real `~/.mastracode/browser-profile-stagehand` profile:
open → close → reopen works and keeps saved cookies; released Mastra
Code fails it with `ECONNREFUSED`.
- `tsc --noEmit` is clean for core, stagehand and mastracode/tui. oxlint
and the peer-dep validator are clean.

## Unverified

- Lock behaviour on Windows and Linux. Real Chrome was only run on
macOS.
- The 3s startup window has not been run against real Chrome.
- Two Stagehand `manual close` e2e cases fail on `hasLockFiles`. They
fail the same way on base.
- A lock from a different hostname is treated as stale.

## Risk

**Low.** Only two launcher log files are deleted, and only when we're
about to launch Chrome on that profile and no live browser holds it.
Lock files are removed only when nothing accepts connections on the
singleton socket. Remote browsers are untouched. Retiring and killing
only affect browsers Mastra Code is replacing or shutting down.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## ELI5
Old Chrome files or a Chrome process that still uses a browser profile
can stop a new browser from starting. This change removes stale files,
reports when a profile is still in use, and closes browsers that Mastra
Code replaces or shuts down.

## What changed
- Before launching with a local profile, core checks for a live Chrome
lock. On non-Windows systems, it removes stale lock files and clears
Chrome launcher logs without deleting the profile. If a browser is using
the profile, it throws `BrowserProfileInUseError` with the profile path
and available process details. Core waits up to three seconds for a live
local lock holder to start listening.
- `MastraBrowser.retire()` closes a browser and prevents it from
launching again. `killProcesses()` force-stops tracked browser processes
where supported. Core uses these APIs during close and retirement.
- Stagehand prepares local profiles before thread-scoped browser
creation. It does not prepare profiles for Browserbase or CDP
connections. The Stagehand peer dependency requires `@mastra/core
>=1.81.0-0`; the code-sdk changeset also requires core 1.81.0 or later.
- Mastra Code closes browsers on shutdown, replacement, and disable.
Generation checks prevent superseded commands from attaching a browser
or saving settings. Replacements wait for earlier closes. If retirement
fails or takes more than five seconds, Mastra Code calls
`killProcesses()`.
- Tests cover profile-lock recovery, profile reuse, browser retirement
and process cleanup, Stagehand thread-scoped preparation, and
overlapping browser commands.

## Validation
The PR reports 36/36 core browser tests, 70/70 Stagehand unit tests, and
56/56 Mastra Code TUI browser and lifecycle tests passing. It also
reports clean TypeScript, oxlint, and peer-dependency checks, plus
real-Chrome testing on macOS. Windows and Linux lock behavior and the
three-second startup window were not verified with real Chrome. Two
Stagehand manual-close end-to-end cases fail on this branch and on the
base branch.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: mastra-platform[bot] <284800079+mastra-platform[bot]@users.noreply.github.com>
2026-10-28 21:20:15 -07:00
dane-ai-mastra[bot]
6a191d46dc chore: version - exit prerelease mode 2026-10-28 13:15:52 +00:00
dane-ai-mastra[bot]
4c22623ea3 chore: version - exit prerelease mode 2026-10-21 11:26:39 +00:00
dane-ai-mastra[bot]
fe516094f5 chore: version - exit prerelease mode 2026-10-15 21:33:45 +00:00
dane-ai-mastra[bot]
f88217d19b chore: version - exit prerelease mode 2026-10-14 05:19:31 +00:00
dane-ai-mastra[bot]
81299c921d chore: version - exit prerelease mode 2026-10-11 16:59:51 +00:00
dane-ai-mastra[bot]
b10b70fd77 chore: version - exit prerelease mode 2026-10-07 08:40:03 +00:00
dane-ai-mastra[bot]
b21e46e19b chore: version - exit prerelease mode 2026-10-01 18:04:32 +00:00
dane-ai-mastra[bot]
8b35a6c1d6 chore: version - exit prerelease mode 2026-10-01 08:22:20 +00:00
dane-ai-mastra[bot]
5306a14346 chore: version - exit prerelease mode 2026-09-29 23:50:38 +00:00
dane-ai-mastra[bot]
6721bffd7e chore: version - exit prerelease mode 2026-09-24 23:17:52 +00:00
dane-ai-mastra[bot]
6d36499758 chore: version - exit prerelease mode 2026-09-24 12:50:16 +00:00
dane-ai-mastra[bot]
a436d6c513 chore: version - exit prerelease mode 2026-09-23 09:15:38 +00:00
dane-ai-mastra[bot]
429d7b9f93 chore: version - exit prerelease mode 2026-09-22 10:43:03 +00:00
dane-ai-mastra[bot]
871bdaa595 chore: version - exit prerelease mode 2026-09-15 09:04:59 +00:00
dane-ai-mastra[bot]
cbd18af524 chore: version - exit prerelease mode 2026-09-10 16:48:00 +00:00
dane-ai-mastra[bot]
4eda447b76 chore: version - exit prerelease mode 2026-09-09 05:42:50 +00:00
dane-ai-mastra[bot]
c19a93b095 chore: version - exit prerelease mode 2026-09-03 04:02:53 +00:00
dane-ai-mastra[bot]
003e75745c chore: version - exit prerelease mode 2026-08-28 21:21:45 +00:00
dane-ai-mastra[bot]
4a41ea6117 chore: version - exit prerelease mode 2026-08-28 13:11:11 +00:00
dane-ai-mastra[bot]
17ad0c69b4 chore: version - exit prerelease mode 2026-08-26 21:34:54 +00:00
dane-ai-mastra[bot]
3cd96a3c5a chore: version - exit prerelease mode 2026-08-26 10:43:02 +00:00
dane-ai-mastra[bot]
ce66f918f0 chore: version - exit prerelease mode 2026-08-21 01:08:55 +00:00
dane-ai-mastra[bot]
c23d44525c chore: version - exit prerelease mode 2026-08-19 03:57:12 +00:00
dane-ai-mastra[bot]
c5702d34a5 chore: version - exit prerelease mode 2026-08-14 00:21:58 +00:00
dane-ai-mastra[bot]
387c6b7f5a chore: version - exit prerelease mode 2026-08-12 04:41:36 +00:00
dane-ai-mastra[bot]
ca935ef6de chore: version - exit prerelease mode 2026-08-05 20:21:53 +00:00
dane-ai-mastra[bot]
b4c8440eef chore: version - exit prerelease mode 2026-08-05 00:05:07 +00:00
dane-ai-mastra[bot]
bccb45d579 chore: version - exit prerelease mode 2026-07-30 01:34:20 +00:00
dane-ai-mastra[bot]
162f3da717 chore: version - exit prerelease mode 2026-07-28 23:00:57 +00:00
dane-ai-mastra[bot]
6c89bcf796 chore: version - exit prerelease mode 2026-07-27 15:35:23 +00:00
Ward Peeters
3a507cbd42
chore: migrate repository formatting to oxfmt (#19807)
Co-authored-by: Mastra Code (openai/gpt-5.5) <noreply@mastra.ai>
2026-07-24 16:18:33 +02:00
dane-ai-mastra[bot]
7a2c3cfb85 chore: version - exit prerelease mode 2026-07-23 09:35:49 +00:00
dane-ai-mastra[bot]
79ee78c5b1 chore: version - exit prerelease mode 2026-07-23 03:17:19 +00:00
dane-ai-mastra[bot]
dfaf448ceb chore: version - exit prerelease mode 2026-07-15 00:15:37 +00:00
dane-ai-mastra[bot]
0223aa21b6 chore: version - exit prerelease mode 2026-07-07 22:40:26 +00:00
dane-ai-mastra[bot]
cf9ef51cc7 chore: version - exit prerelease mode 2026-07-06 17:19:52 +00:00
dane-ai-mastra[bot]
63bac7dc4e chore: version - exit prerelease mode 2026-07-02 22:24:19 +00:00
dane-ai-mastra[bot]
b0c544fc1f chore: version - exit prerelease mode 2026-07-01 00:59:43 +00:00
dane-ai-mastra[bot]
57566498b5 chore: version - exit prerelease mode 2026-06-26 19:51:28 +00:00
dane-ai-mastra[bot]
1556acd04c chore: version - exit prerelease mode 2026-06-24 04:07:33 +00:00
dane-ai-mastra[bot]
7a8a8243bb chore: version - exit prerelease mode 2026-06-19 05:35:11 +00:00
dane-ai-mastra[bot]
17f01b5d9d chore: version - exit prerelease mode 2026-06-18 14:09:51 +00:00
dane-ai-mastra[bot]
90585396ea chore: version - exit prerelease mode 2026-06-17 04:24:24 +00:00
dane-ai-mastra[bot]
331559f184 chore: version - exit prerelease mode 2026-06-12 02:05:27 +00:00
dane-ai-mastra[bot]
a584d8f7c8 chore: version - exit prerelease mode 2026-06-04 14:02:24 +00:00
dane-ai-mastra[bot]
f501edbf79 chore: version - exit prerelease mode 2026-06-03 23:34:12 +00:00