Commit Graph
5 Commits
Author SHA1 Message Date
yetone 81798f7793 browser: a recording is an MP4, encoded in magpie (GIF on format: gif)
record_stop now makes an H.264 MP4 by default, from the same frames as
the GIF (a second a frame, the last held three, the first frame's
size). record_stop takes format: "mp4" (default) or "gif"; any other
value is refused and the recording goes on, so the agent can ask again.

internal/h264 is a small encoder with no cgo and no ffmpeg:
Constrained Baseline, CAVLC, deblocking off. The first picture is IDR.
After that a macroblock the same as before is P_Skip, and a changed one
is Intra16x16 (V/H/DC by SAD). Colour is BT.709 limited range, and an
odd size is cropped. The MP4 puts its moov before mdat, so a player can
start before it has the whole file. QP 23.

The image MCP's file route serves through http.ServeContent, so a
player's Range request gets 206. Safari plays an MP4 only when it is
served that way. extOf gives video/mp4 the extension .mp4.

Run (on mote unless named):
- internal/h264 TestDecodersMakeTheSamePicture. ffmpeg n7.1 (local;
  mote has none) decodes 7 cases to the encoder's own reconstruction,
  byte for byte (diff 0): 330x190 q24, 800x522 q20, 64x48 q10,
  48x32 q40, 96x64 q0 (escape codes), 96x64 q51, 18x18 q23.
  TestEncodeKeepsThePicture: PSNR 42-44 dB at q24.
- internal/gateway: TestBrowserMP4, TestBrowserGIF*,
  TestBrowserRecordStopWithNothing, TestBrowserToolListIsSmall
  (4585 bytes vs 12138), TestImageMCP*.
- TestBrowserRecordsAVideo, in a real Chrome for Testing (chromium-1234)
  with the extension: the MP4 has video/mp4 and a .mp4 link, and a Range
  request gets 206. Chrome opens the link and plays it: 800 wide, 5 s,
  a frame read back from a canvas. format gif decodes to 3 frames, and
  format webm is refused.
- All of internal/gateway's Browser and ImageMCP tests on origin/main
  3fc6ee411, green. The 3-frame recording was MP4 24 KB, GIF 26 KB.
- go vet for darwin, linux and windows; gofmt -l clean.

Broken by hand:
- w.Write in place of ServeContent: TestBrowserRecordsAVideo is red
  ("a range: 200").
- avcC's NAL length size changed from 4 to 2: Chrome's playback check is
  red (this change also stops the script waiting more than 10 s).
- Before the fix in this change, a non-symmetric Hadamard gave PSNR
  ~20 dB and an ffmpeg mismatch. Both tests were red.

Not tried: Safari or Firefox playback, a Windows or Linux run of the
real-browser test, or a recording of hundreds of frames for time and
size.
2026-11-09 06:03:39 +08:00
yetone 8f69ca29c4 browser: Magpie Browser acts like a person and stays out of the user's tabs
The owner, 11-05: Magpie Browser interrupts the user's own browsing, and
driving Discord with it gets the account flagged by Discord's bot check,
which never happened under Claude's Chrome extension.

What a site could see, and what changed (extension 0.1.6):
- CDP's Runtime domain was on in every tab, which bot checks look for.
  It is now off until browser_console is asked for (on localhost, 127.x,
  [::1], *.localhost and *.test it is on from the start, as before).
  The first console call says it reads the console from then on.
- Every read and helper (the clickable scan's getComputedStyle and
  querySelectorAll on each element, focus, readyState, innerText,
  scroll position) ran in the page's own world, where a page's patched
  functions see each call. They now run in an isolated world of the
  extension's (Page.createIsolatedWorld, DOM.resolveNode in it).
  browser_evaluate stays in the page's world, since the agent's script
  is for the page's JavaScript.
- A click jumped to an element's exact middle. The mouse now moves along
  a curved, eased path to a spot drawn around the middle, then presses
  and lets go at uneven timings. Hover and drag go the same way.
- Text went in with Input.insertText, with no key events. It is now
  typed a key at a time with US key codes, Shift held for capitals and
  symbols, at a typist's uneven rhythm (35s at most for a whole text).
  Chinese, Japanese and Korean come through an IME composition, emoji
  through insertText, and a newline as Shift+Enter so a chat box doesn't
  send early. A field's old text is cleared with Backspace only when it
  has some, and submit waits a moment before Enter.
- A scroll was one wheel event. It is several notches now.
- Actions follow each other at a person's pace (350 to 1600ms apart),
  and the debugger stays attached for 5 minutes idle, not 2, so Chrome's
  bar doesn't come and go between calls.

The user's tabs:
- A call naming one of the user's own tabs (one outside every Magpie
  group) now opens that page in a background tab of the agent's own, in
  its group, and the agent is told so; naming it again goes on in that
  copy. This is how Claude's extension keeps to its own group.
- tab_close refuses a tab of the user's.
- A tab an action opens always goes in the agent's group, and a page
  that opens a window of its own gives focus back to the window the user
  was in.
- The tool's instructions said calls go to the tab the user is looking
  at. They now say the agent works in tabs of its own, that naming the
  user's tab copies it, that steps take a person's time, and how the
  console starts.

Run at this head (rebased on fe61503e6), on mote with Chromium 1248
(Chrome for Testing, --headless=new):
- gofmt -l clean; go vet for darwin, linux and windows on
  internal/gateway and internal/browserext
- go test ./internal/browserext: ok
- go test -run TestBrowser ./internal/gateway, with
  MAGPIE_BROWSER_CHROME: 21 of 21 pass, the six real-browser tests among
  them
- New TestBrowserActsLikeAPerson, on shop.example (mapped to the test's
  site, so not a developer's host). It checks that the page never sees
  its getComputedStyle, querySelectorAll, elementFromPoint or focus
  called. It checks that a click is trusted, off the middle, after 4 or
  more mousemoves. It checks a keydown per character at uneven gaps,
  Shift for capitals, and Chinese through compositionend, both in a
  textarea and in a contenteditable chat box. In that chat box a newline
  doesn't send and submit does. It also checks that a 600px scroll is 3
  or more wheel events, that the console is off until asked for, and
  that a named user tab is copied into the group and left as it was.

Broken by hand, each red in that test:
- Runtime always on: console says nothing about starting
- a click at the exact middle with one mousemove: "landed on the middle
  (-0.5, -0.375)" and "moved 1 times"
- insertText for typing: 0 keydowns, even gaps, no Shift, no composition
- the clickable scan in the page's world: the page saw 27 calls
- naming the user's tab acting in it: "doesn't say is the user's own tab"

Not tried:
- The real Discord or a real account. Whether Discord's check now passes
  is not something a test here shows. The reasons it is likely to are the
  differences above, and that they match what Claude's extension does
  (Runtime only when asked, reads outside the page's world, a move before
  each click).
- Headful (MAGPIE_BROWSER_HEADFUL=1) on mote. TestBrowserLeavesTheUsersTabAlone
  fails at random steps there on origin/main fe61503e6 too, even right
  after a plain navigate, because the window counts as hidden on mote's
  idle display (#5513).
- Edge, Windows and Linux Chrome.

The user reloads Magpie Browser in chrome://extensions to get 0.1.6.
2026-11-06 18:41:38 +08:00
yetone 8e27369bd6 browser: record_start and record_stop make a GIF of what an agent did
Asked for in chat ("Magpie Browser MCP 也要支持录屏哦"), like
claude-in-chrome's gif_creator. The browser tool gets two actions:

- record_start: from now on, after each action that can change the page
  (not tabs, snapshot, text, console, screenshot, tab_close), the gateway
  takes a picture of the session's window, 800 wide, with the action and
  what it was done to written in a strip under it (the url, ref, key or
  drag; never what was typed).
- record_stop: the frames as an animated GIF, a second a frame and the
  last three, each frame after the first only the rectangle that changed.
  It is kept an hour behind the image server's tokened files link and
  named in FilesMeta, so `magpie tool` saves it; the text says to curl it.

The gateway does it all through browser_screenshot, so every extension
version records and nobody has to reload theirs. At most 300 frames;
a recording nobody stops goes after 30 minutes. A picture an action's
screenshot: true asks for is the same one the frame is made from.

Run on mote (macOS, Chrome for Testing 1234 headless):
- GOOS=darwin/linux/windows go vet ./internal/gateway, gofmt -l: clean.
- MAGPIE_BROWSER_CHROME=... go test ./internal/gateway -count=1
  -timeout 45m: ok (794s).
- New: TestBrowserRecordsAGIF (real Chromium: navigate, type, snapshot,
  navigate → 3 frames, GIF fetched from the link, 800 wide),
  TestBrowserGIFFramesHoldWhatChanged, TestBrowserRecSayKeepsTypedTextOut,
  TestBrowserRecordStopWithNothing.
Broken by hand: with browserRecWorth always false, TestBrowserRecordsAGIF
is red ("no frames").
Not tried: a real headful Chrome, Edge or Arc; Windows and Linux browsers.
2026-11-04 00:10:50 +08:00
yetone 51fa4b7eb0 browser: agents work in tabs of their own, behind the user's
The owner: Magpie Browser "老是会代替掉用户正在浏览的网页". A call that
named no tab, from a session with none, took the tab the user had in
front and navigated it away under them. Now:

- such a call opens a new tab of the session's (about:blank), behind the
  shown one, in its Magpie tab group, in the window used last, or an
  unfocused window of its own when none is open (open() in background.js);
- browser_tabs new / tab_new open behind too; front: true shows the tab,
  for when the user asked to see it (background is still taken, and is
  now the default);
- a screenshot of a tab behind shows it for the picture, then gives the
  window back the tab the user had in front;
- a link with target=_blank clicked in a tab behind: Chrome shows the new
  tab and names the user's shown tab as its opener. It is still followed,
  grouped in the session's group, and the user's tab is put back in front.
- the tool descriptions say so; the user's tabs are acted in only when a
  call names one.

Extension 0.1.4. When an older one connects, the gateway writes this
magpie's over its folders (browserext.Refresh; only folders already
written, never a newer one's: browserext.Older), so the browser picks it
up when it starts again or on the extension's reload in
chrome://extensions. Before, only opening Library's Browser page did. The
dir helpers moved from gui to browserext for it.

Tried and dropped: telling the extension to chrome.runtime.reload()
itself. In Chrome for Testing 1234 on mote an unpacked extension that
calls it never comes back (headless and headful, --load-extension and
CDP Extensions.loadUnpacked, with an onInstalled listener), which would
leave the user with none until a restart.

Run on mote at 1e1068c86 + this:
- MAGPIE_BROWSER_CHROME=Chrome for Testing 1234, go test -run Browser -v
  ./internal/gateway: ok, 16 passed, including the new
  TestBrowserLeavesTheUsersTabAlone (navigate, link to a new tab, tab new,
  screenshot, tab_new: the user's about:blank tab stays + and unchanged,
  the agent's in [Magpie]) and TestBrowserOlderExtensionsFolderWritten.
- go test ./internal/browserext (TestOlder, TestRefreshWritesOnlyWrittenFolders): ok.
- go test ./internal/gui: one red, TestMainWindowCorners/macOS_26_SDK,
  filed as #3949 (macOS 27 on mote), not touched here.
- gofmt -l; GOOS=darwin/linux/windows go vet of the three packages: clean.

Broken by hand: TestBrowserLeavesTheUsersTabAlone with origin/main's
background.js is red ("navigated the user's tab"). The link case was
found by TestBrowserToolsInARealBrowser going red on the first version.

Not tried: the user's own Chrome (headful Chrome with real sign-ins),
Edge and Arc; Windows and Linux browsers.
2026-11-03 23:48:36 +08:00
yetone 7e201ce20d browser: one tool, browser, whose action says what to do
magpie-browser listed 17 tools (11,483 bytes of schema in every request);
it now lists one, browser, whose action picks what to do (4,087 bytes).
The old tool names are still accepted on tools/call, and text the
extension writes about browser_X now says "the X action".

Any action takes screenshot: true and returns the page after it, as a
1024px-wide JPEG (q70), so an agent sees the page without a second call.

Run on mote at this head: go test -tags nogui -run Browser
./internal/gateway/ with a real Chromium: ok, including
TestBrowserToolActionsInARealBrowser. TestBrowserToolCallsTheExtensionsTool
went red with the name rewrite taken out by hand and with the downscale
taken out (2880x1800 stays 2880x1800). gofmt clean; go vet -tags nogui for
darwin, linux and windows ok. Not tried: Claude Code and Codex using the
one tool against a live gateway.
2026-11-02 06:40:03 +08:00