From ca87ae9c596dc1e080c4e282c833f893994c054a Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 17 Nov 2026 04:28:57 +0800 Subject: [PATCH] update: an x64 magpie emulated on Windows on Arm updates to the arm64 build (EV_John_Liu on X) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit EV_John_Liu on X: "能不能出个windows on arm的版本啊…看到magpie是转译的". magpie ships magpie-windows-arm64.exe and magpie-cli-windows-arm64.exe, and the site's Windows button offers it on Arm, but an x64 install on an Arm64 computer fetched the x64 build on every update and stayed emulated. update's arch() swapped only an Intel Mac under Rosetta. - internal/winarm: Emulated() asks IsWow64Process2 (x/sys/windows) once; an amd64 build whose nativeMachine is IMAGE_FILE_MACHINE_ARM64 runs emulated. A failed call (no IsWow64Process2 before Windows 10 1709) means not emulated. false off Windows. - update: arch() gives arm64 for windows/amd64 when emulated, so BinaryAsset picks magpie-windows-arm64.exe (GUI) and magpie-cli-windows-arm64.exe (CLI). The swap is the x64 one: canStart starts the downloaded exe (it runs natively there), the running exe is moved aside, the arm64 one renamed in under the same name, and magpie.com is rewritten from the new app's own launcher. - pii (sibling found on the way): pf-serve has no windows-arm64 build. After the switch an arm64 magpie looked for a windows-arm64 pf-serve, found none, and left Privacy Filter / AstrLink Guard "not downloaded" while on. target() now maps windows-arm64 to the x64 pf-serve Windows 11 runs emulated; candidates() runs it on the CPU (the x64 pf-serve-vulkan has no x64 Vulkan driver there), then pf-serve-base for an emulation without AVX2. The guard's status says emulated (Status.Emulated), and its row reads "On, slowly under x64 emulation: the next update installs magpie for Arm" in all five languages, like the Rosetta line. app.js's and the core's script budgets grow by its 66 bytes. - gui feedback: the diagnostics block's Arch line says "amd64 (emulated on arm64)" / "amd64 (under Rosetta on arm64)". Checked, not changed: - site/public/index.html: the Windows main button already turns to /download/windows-arm64 when userAgentData's architecture is "arm". Run in Playwright Chromium and WebKit with a Windows Edge UA and injected userAgentData: arm -> windows-arm64; x86 and no userAgentData -> windows-amd64. - telemetry (stats "arch") sends the build's GOARCH; left so, since what is sent and the privacy text stay as they are. After the update it is arm64 by itself. - rtk: an arm64 magpie no longer upgrades a standalone x64 rtk.exe (rtkArch == "amd64" only), filed as #6896. - claudecode (win32-arm64) and Bun (windows-aarch64) downloads already have arm64 builds. Run (worktree at origin/main 17da22414): - mote: go test -count=1 ./internal/winarm ./internal/update ./internal/pii ok; ./internal/gui -run Feedback ok. - mote: go vet ./... ok; GOOS=linux go vet -tags nogui ./... ok; GOOS=windows go vet ./... ok; GOOS=windows GOARCH=arm64 go vet of winarm, update, pii, gui ok; darwin, windows and linux builds ok; gofmt -l clean. - mote: node --test privacy-model.test.cjs (Chromium and WebKit, five languages, two widths) ok; i18n-keys' "a plugin's words are its own" is red at this base on claude_image_budget_test.go's ", with an image", already #6877. - xiaomi-gpu (x64 Windows 11, real IsWow64Process2 saying not emulated): win-run go test ./internal/winarm, ./internal/update -run 'WindowsOnArm|Rosetta|Asset|InstallBinary', ./internal/pii -run 'ArmWindows|GuardNow|Base', ./internal/gui -run Feedback ok. - magpie-desktop (WSL2): winarm, update, pii ok; gui -tags nogui -run Feedback ok. - mote: go test -count=1 -tags nogui -timeout 45m ./... : every package ok but internal/gui's TestCoreScriptBudget (app.js 66 bytes over: the new pmSub line). The budgets are raised by those 66 with why (pmSub draws every Privacy row, no lazy page to move it to); internal/gui then ok in full. - mote: gui-de, gui-ja, gui-zh-tw, i18n-en-no-cjk and the three other privacy-*.test.cjs, in Chromium and WebKit: 28 of 28 each. Not the whole GUI suite: the change is one string in one row, no API, selector, CSS or layout. Broken by hand (each red, then restored): - arch() without the windows clause: TestWindowsOnArmUpdatesToArm64 (BinaryAsset magpie-windows-amd64.exe) and TestWindowsOnArmInstallsArm64Exe (magpie.exe still "2.0 for x64"). - pii target() without the mapping: TestArmWindowsRunsX64Serve (target windows-arm64, Supported false); candidates() without the Arm branch: its subtests start pf-serve-vulkan. - Status without Emulated: TestGuardNowSaysEmulated. - archNote without the emulated case: TestFeedbackArchSaysTranslated. - app.js without the emulated line: privacy-model's "AstrLink Guard says what runs it" red in every language. Not tried: real Windows on ARM (no such machine); detection is injected in the tests, and the real call was only seen saying "not emulated" on x64 Windows. --- internal/gui/assets/app.js | 2 + internal/gui/assets/i18n.js | 4 + internal/gui/core_budget_test.go | 4 +- internal/gui/feedback.go | 24 +++++- internal/gui/feedback_test.go | 22 ++++++ internal/gui/tests/privacy-model.test.cjs | 15 ++-- internal/pii/arch.go | 16 ++++ internal/pii/arch_test.go | 50 ++++++++++++ internal/pii/astrlink.go | 2 +- internal/pii/astrlink_test.go | 20 +++++ internal/pii/main_test.go | 5 ++ internal/pii/pii.go | 3 + internal/pii/rosetta.go | 9 ++- internal/pii/serve.go | 11 +++ internal/update/rosetta_test.go | 5 +- internal/update/update.go | 15 +++- internal/update/winarm_test.go | 93 +++++++++++++++++++++++ internal/winarm/winarm.go | 17 +++++ internal/winarm/winarm_other.go | 7 ++ internal/winarm/winarm_test.go | 36 +++++++++ internal/winarm/winarm_windows.go | 18 +++++ 21 files changed, 362 insertions(+), 16 deletions(-) create mode 100644 internal/update/winarm_test.go create mode 100644 internal/winarm/winarm.go create mode 100644 internal/winarm/winarm_other.go create mode 100644 internal/winarm/winarm_test.go create mode 100644 internal/winarm/winarm_windows.go diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index e3613eaa7..6fbdc9ae8 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -22180,6 +22180,8 @@ function pmSub(m) { if (m.via) return { "Neural Engine": t("On, with CoreML on the Neural Engine"), GPU: t("On, with CoreML on the GPU"), CPU: t("On, with CoreML on the CPU") }[m.device] || t("On, with CoreML"); // the Intel build on Apple silicon: Go, translated, several times slower if (m.rosetta) return t("On, slowly under Rosetta: the next update installs magpie for Apple silicon"); + // the x64 build on Windows on Arm, emulated + if (m.emulated) return t("On, slowly under x64 emulation: the next update installs magpie for Arm"); if (m.gpuError && m.model === "astrlink-guard") return t("On, on the CPU: CoreML failed ({error})", { error: m.gpuError }); // Privacy Filter lost the GPU: to its driver resetting it (device // lost), or to a fault; the GPU is tried again at gpuAgain diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index bcac0f0fe..02abb928f 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -1733,6 +1733,7 @@ const I18N = { "On, on the CPU until {time}: the graphics driver reset the GPU (device lost)": "已开启,{time} 前运行在 CPU:显卡驱动重置了 GPU(设备丢失)", "On, on the CPU": "已开启,运行在 CPU", "On, slowly under Rosetta: the next update installs magpie for Apple silicon": "已开启,但在 Rosetta 下运行较慢:下次更新会装上 Apple 芯片版 magpie", + "On, slowly under x64 emulation: the next update installs magpie for Arm": "已开启,但在 x64 转译下运行较慢:下次更新会装上 ARM 版 magpie", "On, with CoreML on the Neural Engine": "已开启,通过 CoreML 运行在神经网络引擎", "On, with CoreML on the GPU": "已开启,通过 CoreML 运行在 GPU", "On, with CoreML on the CPU": "已开启,通过 CoreML 运行在 CPU", @@ -7093,6 +7094,7 @@ const I18N = { "On, on the CPU until {time}: the graphics driver reset the GPU (device lost)": "已開啟,{time} 前執行於 CPU:顯示卡驅動程式重設了 GPU(裝置遺失)", "On, on the CPU": "已開啟,執行於 CPU", "On, slowly under Rosetta: the next update installs magpie for Apple silicon": "已開啟,但在 Rosetta 下執行較慢:下次更新會裝上 Apple 晶片版 magpie", + "On, slowly under x64 emulation: the next update installs magpie for Arm": "已開啟,但在 x64 模擬下執行較慢:下次更新會裝上 ARM 版 magpie", "On, with CoreML on the Neural Engine": "已開啟,透過 CoreML 執行於神經網路引擎", "On, with CoreML on the GPU": "已開啟,透過 CoreML 執行於 GPU", "On, with CoreML on the CPU": "已開啟,透過 CoreML 執行於 CPU", @@ -12421,6 +12423,7 @@ const I18N = { "On, on the CPU until {time}: the graphics driver reset the GPU (device lost)": "オン、{time} まで CPU で実行中:グラフィックスドライバーが GPU をリセットしました(デバイスロスト)", "On, on the CPU": "オン、CPU で実行中", "On, slowly under Rosetta: the next update installs magpie for Apple silicon": "オン、Rosetta で動作中のため低速です。次のアップデートで Apple シリコン版の magpie が入ります", + "On, slowly under x64 emulation: the next update installs magpie for Arm": "オン、x64 エミュレーションで動作中のため低速です。次のアップデートで ARM 版の magpie が入ります", "On, with CoreML on the Neural Engine": "オン、CoreML により Neural Engine で実行中", "On, with CoreML on the GPU": "オン、CoreML により GPU で実行中", "On, with CoreML on the CPU": "オン、CoreML により CPU で実行中", @@ -17780,6 +17783,7 @@ const I18N = { "On, on the CPU until {time}: the graphics driver reset the GPU (device lost)": "An, bis {time} auf der CPU: der Grafiktreiber hat die GPU zurückgesetzt (Gerät verloren)", "On, on the CPU": "An, auf der CPU", "On, slowly under Rosetta: the next update installs magpie for Apple silicon": "An, unter Rosetta langsam: das nächste Update installiert magpie für Apple Silicon", + "On, slowly under x64 emulation: the next update installs magpie for Arm": "An, unter x64-Emulation langsam: das nächste Update installiert magpie für Arm", "On, with CoreML on the Neural Engine": "An, mit CoreML auf der Neural Engine", "On, with CoreML on the GPU": "An, mit CoreML auf der GPU", "On, with CoreML on the CPU": "An, mit CoreML auf der CPU", diff --git a/internal/gui/core_budget_test.go b/internal/gui/core_budget_test.go index 49cd2dacb..d4e3fa412 100644 --- a/internal/gui/core_budget_test.go +++ b/internal/gui/core_budget_test.go @@ -17,8 +17,8 @@ import ( // never what pays for a change (#5918); the history of each number is in // `git log -L` of this file. const ( - appScriptBudget = 1_087_381 // app.js: 1,080,614 at 8016205e3, with the 5,653 of room it had; +123 for a sidebar list folded from its page's row (hkwok06 on X), its chevron and fold in navicons.js, imported with the first list; +57 for each account of a provider's Usage card in a card of its own (normalcoder_ on X), past the 24 of room left; +17 for an allowance card's wallet line under its rings (apple_test_src on X), the one filter shared with its text line; +338 for the Daily update channel (fishmagic80 on X): its segment, its line under Update channel and the version row's two Up to date on Daily words, packed to the shortest form (one t() per choice); Settings › About and renderUpdate draw them on every window, so there is no page to move them to; +35 for Baidu Qianfan's Token Plan sign-in (yuushabu_buyinn on X, #5860): its field in qianfanplan.js, imported when such a provider's editor opens, drawn by renderStepPlan now told whose sign-in it brings, and what is left is the import and two quotaError lines, past the room there was; quotaError reads every card's error, so there is no page to move it to; +27 for Windows' title bar (258a14 on X): the caption buttons, their clicks, the bar's double click and what keeps them over a dialog in wincap.js, imported in Windows' window alone, the header's room for them in app.css, and what is left is the one line that imports it, past the 29 of room there was; that window's frame is drawn on every start, so there is no page to move it to; +107 for the pages in the reader's own order (chet030714 on X): the order, the sidebar and tab bar put in it, and the menu's drag and keys in navorder.js and navpages.js, imported once an order is kept or Settings draws, and what is left is the one line in navHides that imports it; navHides runs on every start, so there is no page to move it to; +275 for the grants Alibaba's Token Plan asks of a RAM user's AccessKey in the reader's language (MetalHqa on X): one string (ALI_RAM) shown under the editor's AccessKey ID and on the card that names them, less the 48 of the wrong "a RAM user that may use Model Studio is enough" it replaces; the editor and quotaError draw them on every window, so there is no page to move them to; +135 for a model test its vendor queued shown queued and not green (sufeng1894 on X): queuedSaid, one string shared by the chip's title, Test models' and Test this model's status, and the chip's amber dot; renderModels draws the provider editor's chips, so there is no page to move it to - coreScriptBudget = 1_271_471 // every core script at 8016205e3, the old measure 682 over its cap there (#5918); +367 for the model test on a chosen account (tmr11235), no page left to move it to; +151 for the provider menu's "Only on this device" item and its four translations; +458 for Settings › About's Report a problem row and its translations; +113 for withScheme, the one rule every typed address goes through (eachg0 on X), cut to the shortest form that keeps it whole; +10 for the kept whole's tooltip (Musikrv77852 on X), the past-cycle line reused for it, no page left to move it to; +1,112 for Codex's Open button and its Open Codex with menu (Yink88564391 on X), packed to the shortest form, no page left to move them to; +168 for Discover's long lists drawn in part (the owner, 11-13), after the rest of its hook moved into plugingrid.js, imported when Plugins first draws; +296 for the cap toggle and Smart sentences that say what they do and the Sign in on another computer row (Wing8Aqua on X), packed to the shortest form, no page left to move them to; +333 for In turn's figure in a provider's editor and its words naming the message that goes round (Wing8Aqua on X), the figure itself in rotfig.js, imported when In turn is first shown, the hook packed to the shortest form; +236 for a lone capped Codex account's row saying its turns go to the fallbacks when Codex is connected (not_4O4 on X), the two helpers inlined, no page left to move it to; +717 for omp's advisor (Awad_Yoo on X, #6408): its three rows moved into ompadvisor.js, imported when an omp's settings show, and what is left is the import, the role's " · off" and the Settings line's name, packed to the shortest form; +468 for a plugin's own file import (auth.import) in the import sheet, in the plugin's words (marchen_cc on X), packed to one statement; importSay is read as the Providers rows draw, so it has no lazy page to go to, and nothing else was moved out; +180 for Claude Magpie's own Claude Code folder (xMopf7RF on X, #6459): its Advanced fold in claudemagpie.js, imported when it first shows, and what is left is the import and the fold's place in the Separate app line, 216 bytes packed to the shortest form, less the 36 of room there was; +81 for the Use Claude Magpie button on Claude Desktop's row (xMopf7RF on X), drawn by claudemagpie.js too, what is left the one line that places it (#2710); +225 for the Hermes row's Other profiles lines (Norethcy on X), which 1d60b165c took out while each profile had a row and c96f33d65 left with no place at all, packed to the shortest form; the dead hermes# Default note went with it, and the Agents rows draw it, so there is no page to move it to; +397 for a key's own price rate (tnt12231 on X): its lines, check and placeholder in keyrates.js, imported when a provider with two or more keys opens, and what is left is the import, the draft's copy, Advanced opening for it, the save's one line and proxyLines drawing another kind of line; +316 for an image or video model's chip saying what it makes from its right-click (iceloon on X): the chat chip's Makes… now shared with it, and the media chip's Copy model ID left out, packed to the shortest form; the provider's editor draws it, so there is no page to move it to; +67 for the Claude Code · Claude Magpie row saying it is on magpie through Claude Magpie (xMopf7RF on X), one line in connectLine with the shortest words that say it; the Agents rows draw it, so there is no page to move it to; +12 for AIGoCode's logo in pngIcons (MindfulReturn on X), the list every drawn icon is looked up in; +491 for each cost in the tray panel's and Requests' chart and ranking in its own provider's currency (onkashiko on X): ledProv and the provider passed through, packed to the shortest form; drawLedColumns and drawLedRank are shared by the panel, so there is no page to move it to; +326 for the Qwen AI platform's Token Plan sign-in (imthe99galaxy on X): its field, sign-in and wait in qianwenplan.js, imported when such a provider's editor opens, and what is left is the import and one quotaError line for its two refusals, packed to the shortest form; quotaError reads every card's error, so there is no page to move it to; +72 for Copy all keys (emilochen on X): its row in keyrates.js, imported when a provider with two or more keys opens through keyRateField, now told which of its parts to draw, and what is left is the one line that places it; +83 for the page saying it has gone from view, so what its Sessions and Usage pages keep goes in 30 s and not 7 minutes (P0 memory): folded into the visibilitychange listener every window has, packed to the shortest form, so there is no page to move it to; +270 for Providers' Select, to remove several at once (eiyuuzaki2333 on X): its rows, bar, removal and keys in pvpick.js, imported when Select is first clicked, and what is left is pvSel, the button's click and two lines in renderProviders, which draws the list, so there is no page to move it to; +81 for each account of a provider's Usage card in a card of its own (normalcoder_ on X): one line that makes the box, its brief look left to CSS, and renderQuotas, which draws every card, so there is no page to move it to; +17 for Kimi Code's booster balance under an allowance card's rings in the tray panel (apple_test_src on X): the card's text line and the wallet line share one filter, packed to the shortest form; panelQuotaCard draws every card, so there is no page to move it to; +51 for what a failed plugin install left in plugins/, listed under Installed with a Remove (CHENGJIAHEGOXIS on X): its section in plugininstalled.js as pmLeftovers, imported when Plugins first draws, and what is left is the one call after drawInstalled; draw is what draws the page, so there is no page to move it to; +123 for a sidebar list folded from its page's row (hkwok06 on X): the chevron, its click and the fold kept per page in navicons.js, imported with the first list, and what is left is the folded list read beside the hidden pages' (navList, which navMine now shares) and the two rows' clicks asking navFold first; navSubs draws every list, so there is no page to move it to; +338 for the Daily update channel (fishmagic80 on X): its segment, its line under Update channel and the version row's two Up to date on Daily words, packed to the shortest form (one t() per choice); Settings › About and renderUpdate draw them on every window, so there is no page to move them to; +35 for Baidu Qianfan's Token Plan sign-in (yuushabu_buyinn on X, #5860): its field in qianfanplan.js, imported when such a provider's editor opens, drawn by renderStepPlan now told whose sign-in it brings, and what is left is the import and two quotaError lines, past the room there was; quotaError reads every card's error, so there is no page to move it to; +27 for Windows' title bar (258a14 on X): the caption buttons, their clicks, the bar's double click and what keeps them over a dialog in wincap.js, imported in Windows' window alone, the header's room for them in app.css, and what is left is the one line that imports it, past the 29 of room there was; that window's frame is drawn on every start, so there is no page to move it to; +107 for the pages in the reader's own order (chet030714 on X): the order, the sidebar and tab bar put in it, and the menu's drag and keys in navorder.js and navpages.js, imported once an order is kept or Settings draws, and what is left is the one line in navHides that imports it; navHides runs on every start, so there is no page to move it to; +275 for the grants Alibaba's Token Plan asks of a RAM user's AccessKey in the reader's language (MetalHqa on X): one string (ALI_RAM) shown under the editor's AccessKey ID and on the card that names them, less the 48 of the wrong "a RAM user that may use Model Studio is enough" it replaces; the editor and quotaError draw them on every window, so there is no page to move them to; +135 for a model test its vendor queued shown queued and not green (sufeng1894 on X): queuedSaid, one string shared by the chip's title, Test models' and Test this model's status, and the chip's amber dot; renderModels draws the provider editor's chips, so there is no page to move it to + appScriptBudget = 1_087_447 // app.js: 1,080,614 at 8016205e3, with the 5,653 of room it had; +123 for a sidebar list folded from its page's row (hkwok06 on X), its chevron and fold in navicons.js, imported with the first list; +57 for each account of a provider's Usage card in a card of its own (normalcoder_ on X), past the 24 of room left; +17 for an allowance card's wallet line under its rings (apple_test_src on X), the one filter shared with its text line; +338 for the Daily update channel (fishmagic80 on X): its segment, its line under Update channel and the version row's two Up to date on Daily words, packed to the shortest form (one t() per choice); Settings › About and renderUpdate draw them on every window, so there is no page to move them to; +35 for Baidu Qianfan's Token Plan sign-in (yuushabu_buyinn on X, #5860): its field in qianfanplan.js, imported when such a provider's editor opens, drawn by renderStepPlan now told whose sign-in it brings, and what is left is the import and two quotaError lines, past the room there was; quotaError reads every card's error, so there is no page to move it to; +27 for Windows' title bar (258a14 on X): the caption buttons, their clicks, the bar's double click and what keeps them over a dialog in wincap.js, imported in Windows' window alone, the header's room for them in app.css, and what is left is the one line that imports it, past the 29 of room there was; that window's frame is drawn on every start, so there is no page to move it to; +107 for the pages in the reader's own order (chet030714 on X): the order, the sidebar and tab bar put in it, and the menu's drag and keys in navorder.js and navpages.js, imported once an order is kept or Settings draws, and what is left is the one line in navHides that imports it; navHides runs on every start, so there is no page to move it to; +275 for the grants Alibaba's Token Plan asks of a RAM user's AccessKey in the reader's language (MetalHqa on X): one string (ALI_RAM) shown under the editor's AccessKey ID and on the card that names them, less the 48 of the wrong "a RAM user that may use Model Studio is enough" it replaces; the editor and quotaError draw them on every window, so there is no page to move them to; +135 for a model test its vendor queued shown queued and not green (sufeng1894 on X): queuedSaid, one string shared by the chip's title, Test models' and Test this model's status, and the chip's amber dot; renderModels draws the provider editor's chips, so there is no page to move it to; +66 for AstrLink Guard's row saying an x64 magpie runs it under Windows on Arm's emulation until the next update installs the Arm build (EV_John_Liu on X), the Rosetta line's sibling, one line in pmSub; the Privacy rows draw it, so there is no page to move it to + coreScriptBudget = 1_271_537 // every core script at 8016205e3, the old measure 682 over its cap there (#5918); +367 for the model test on a chosen account (tmr11235), no page left to move it to; +151 for the provider menu's "Only on this device" item and its four translations; +458 for Settings › About's Report a problem row and its translations; +113 for withScheme, the one rule every typed address goes through (eachg0 on X), cut to the shortest form that keeps it whole; +10 for the kept whole's tooltip (Musikrv77852 on X), the past-cycle line reused for it, no page left to move it to; +1,112 for Codex's Open button and its Open Codex with menu (Yink88564391 on X), packed to the shortest form, no page left to move them to; +168 for Discover's long lists drawn in part (the owner, 11-13), after the rest of its hook moved into plugingrid.js, imported when Plugins first draws; +296 for the cap toggle and Smart sentences that say what they do and the Sign in on another computer row (Wing8Aqua on X), packed to the shortest form, no page left to move them to; +333 for In turn's figure in a provider's editor and its words naming the message that goes round (Wing8Aqua on X), the figure itself in rotfig.js, imported when In turn is first shown, the hook packed to the shortest form; +236 for a lone capped Codex account's row saying its turns go to the fallbacks when Codex is connected (not_4O4 on X), the two helpers inlined, no page left to move it to; +717 for omp's advisor (Awad_Yoo on X, #6408): its three rows moved into ompadvisor.js, imported when an omp's settings show, and what is left is the import, the role's " · off" and the Settings line's name, packed to the shortest form; +468 for a plugin's own file import (auth.import) in the import sheet, in the plugin's words (marchen_cc on X), packed to one statement; importSay is read as the Providers rows draw, so it has no lazy page to go to, and nothing else was moved out; +180 for Claude Magpie's own Claude Code folder (xMopf7RF on X, #6459): its Advanced fold in claudemagpie.js, imported when it first shows, and what is left is the import and the fold's place in the Separate app line, 216 bytes packed to the shortest form, less the 36 of room there was; +81 for the Use Claude Magpie button on Claude Desktop's row (xMopf7RF on X), drawn by claudemagpie.js too, what is left the one line that places it (#2710); +225 for the Hermes row's Other profiles lines (Norethcy on X), which 1d60b165c took out while each profile had a row and c96f33d65 left with no place at all, packed to the shortest form; the dead hermes# Default note went with it, and the Agents rows draw it, so there is no page to move it to; +397 for a key's own price rate (tnt12231 on X): its lines, check and placeholder in keyrates.js, imported when a provider with two or more keys opens, and what is left is the import, the draft's copy, Advanced opening for it, the save's one line and proxyLines drawing another kind of line; +316 for an image or video model's chip saying what it makes from its right-click (iceloon on X): the chat chip's Makes… now shared with it, and the media chip's Copy model ID left out, packed to the shortest form; the provider's editor draws it, so there is no page to move it to; +67 for the Claude Code · Claude Magpie row saying it is on magpie through Claude Magpie (xMopf7RF on X), one line in connectLine with the shortest words that say it; the Agents rows draw it, so there is no page to move it to; +12 for AIGoCode's logo in pngIcons (MindfulReturn on X), the list every drawn icon is looked up in; +491 for each cost in the tray panel's and Requests' chart and ranking in its own provider's currency (onkashiko on X): ledProv and the provider passed through, packed to the shortest form; drawLedColumns and drawLedRank are shared by the panel, so there is no page to move it to; +326 for the Qwen AI platform's Token Plan sign-in (imthe99galaxy on X): its field, sign-in and wait in qianwenplan.js, imported when such a provider's editor opens, and what is left is the import and one quotaError line for its two refusals, packed to the shortest form; quotaError reads every card's error, so there is no page to move it to; +72 for Copy all keys (emilochen on X): its row in keyrates.js, imported when a provider with two or more keys opens through keyRateField, now told which of its parts to draw, and what is left is the one line that places it; +83 for the page saying it has gone from view, so what its Sessions and Usage pages keep goes in 30 s and not 7 minutes (P0 memory): folded into the visibilitychange listener every window has, packed to the shortest form, so there is no page to move it to; +270 for Providers' Select, to remove several at once (eiyuuzaki2333 on X): its rows, bar, removal and keys in pvpick.js, imported when Select is first clicked, and what is left is pvSel, the button's click and two lines in renderProviders, which draws the list, so there is no page to move it to; +81 for each account of a provider's Usage card in a card of its own (normalcoder_ on X): one line that makes the box, its brief look left to CSS, and renderQuotas, which draws every card, so there is no page to move it to; +17 for Kimi Code's booster balance under an allowance card's rings in the tray panel (apple_test_src on X): the card's text line and the wallet line share one filter, packed to the shortest form; panelQuotaCard draws every card, so there is no page to move it to; +51 for what a failed plugin install left in plugins/, listed under Installed with a Remove (CHENGJIAHEGOXIS on X): its section in plugininstalled.js as pmLeftovers, imported when Plugins first draws, and what is left is the one call after drawInstalled; draw is what draws the page, so there is no page to move it to; +123 for a sidebar list folded from its page's row (hkwok06 on X): the chevron, its click and the fold kept per page in navicons.js, imported with the first list, and what is left is the folded list read beside the hidden pages' (navList, which navMine now shares) and the two rows' clicks asking navFold first; navSubs draws every list, so there is no page to move it to; +338 for the Daily update channel (fishmagic80 on X): its segment, its line under Update channel and the version row's two Up to date on Daily words, packed to the shortest form (one t() per choice); Settings › About and renderUpdate draw them on every window, so there is no page to move them to; +35 for Baidu Qianfan's Token Plan sign-in (yuushabu_buyinn on X, #5860): its field in qianfanplan.js, imported when such a provider's editor opens, drawn by renderStepPlan now told whose sign-in it brings, and what is left is the import and two quotaError lines, past the room there was; quotaError reads every card's error, so there is no page to move it to; +27 for Windows' title bar (258a14 on X): the caption buttons, their clicks, the bar's double click and what keeps them over a dialog in wincap.js, imported in Windows' window alone, the header's room for them in app.css, and what is left is the one line that imports it, past the 29 of room there was; that window's frame is drawn on every start, so there is no page to move it to; +107 for the pages in the reader's own order (chet030714 on X): the order, the sidebar and tab bar put in it, and the menu's drag and keys in navorder.js and navpages.js, imported once an order is kept or Settings draws, and what is left is the one line in navHides that imports it; navHides runs on every start, so there is no page to move it to; +275 for the grants Alibaba's Token Plan asks of a RAM user's AccessKey in the reader's language (MetalHqa on X): one string (ALI_RAM) shown under the editor's AccessKey ID and on the card that names them, less the 48 of the wrong "a RAM user that may use Model Studio is enough" it replaces; the editor and quotaError draw them on every window, so there is no page to move them to; +135 for a model test its vendor queued shown queued and not green (sufeng1894 on X): queuedSaid, one string shared by the chip's title, Test models' and Test this model's status, and the chip's amber dot; renderModels draws the provider editor's chips, so there is no page to move it to; +66 for AstrLink Guard's row saying an x64 magpie runs it under Windows on Arm's emulation until the next update installs the Arm build (EV_John_Liu on X), the Rosetta line's sibling, one line in pmSub; the Privacy rows draw it, so there is no page to move it to ) // codeBytes is how many bytes of b are code: its blank lines, its lines diff --git a/internal/gui/feedback.go b/internal/gui/feedback.go index d114664de..bb2de8cd5 100644 --- a/internal/gui/feedback.go +++ b/internal/gui/feedback.go @@ -10,6 +10,8 @@ import ( "github.com/yetone/magpie/internal/lazyre" "github.com/yetone/magpie/internal/proc" + "github.com/yetone/magpie/internal/rosetta" + "github.com/yetone/magpie/internal/winarm" ) // FeedbackURL is the post on X where users reply with feedback; Settings › @@ -50,6 +52,26 @@ func osVersion() string { return "Linux" } +// underRosetta and emulated say this build runs translated: an Intel one +// on Apple silicon, an x64 one on Windows on Arm. Tests set them. +var ( + underRosetta = rosetta.Translated + emulated = winarm.Emulated +) + +// archNote is the architecture this build is for, and the computer's +// where it runs translated there, which the next update ends (update's +// arch): a report from such a computer says so, not just "amd64". +func archNote() string { + switch { + case underRosetta(): + return runtime.GOARCH + " (under Rosetta on arm64)" + case emulated(): + return runtime.GOARCH + " (emulated on arm64)" + } + return runtime.GOARCH +} + // diagnostics is the block a report starts with. It holds the version, the // system, the architecture and where magpie is open, and never an account, // a key, a path or a user name: the user pastes it into a public reply. @@ -60,7 +82,7 @@ func diagnostics(surface string) string { return strings.Join([]string{ "magpie " + Version, "OS: " + osVersion(), - "Arch: " + runtime.GOARCH, + "Arch: " + archNote(), "Opened in: " + surface, }, "\n") } diff --git a/internal/gui/feedback_test.go b/internal/gui/feedback_test.go index 8249fcc37..4a9763198 100644 --- a/internal/gui/feedback_test.go +++ b/internal/gui/feedback_test.go @@ -47,3 +47,25 @@ func TestFeedbackBlockHoldsNoPrivateFacts(t *testing.T) { t.Error("an unknown surface is not the app") } } + +// A build that runs translated says so beside its architecture: an Intel +// magpie under Rosetta, an x64 one under Windows on Arm's emulation +// (@EV_John_Liu on X), each of which the next update replaces with the +// arm64 build. A native one says its architecture alone. +func TestFeedbackArchSaysTranslated(t *testing.T) { + wasR, wasE := underRosetta, emulated + t.Cleanup(func() { underRosetta, emulated = wasR, wasE }) + for _, c := range []struct { + ros, em bool + want string + }{ + {false, false, "Arch: " + runtime.GOARCH + "\n"}, + {true, false, "Arch: " + runtime.GOARCH + " (under Rosetta on arm64)\n"}, + {false, true, "Arch: " + runtime.GOARCH + " (emulated on arm64)\n"}, + } { + underRosetta, emulated = func() bool { return c.ros }, func() bool { return c.em } + if got := diagnostics("app"); !strings.Contains(got, c.want) { + t.Errorf("rosetta=%v emulated=%v: block lacks %q:\n%s", c.ros, c.em, c.want, got) + } + } +} diff --git a/internal/gui/tests/privacy-model.test.cjs b/internal/gui/tests/privacy-model.test.cjs index 71de8bbe9..1ee98c304 100644 --- a/internal/gui/tests/privacy-model.test.cjs +++ b/internal/gui/tests/privacy-model.test.cjs @@ -100,19 +100,22 @@ const scoreWords = { // where AstrLink Guard runs (yyhhyyyyyy on Discord: it said "on the CPU" // while CoreML ran it): CoreML on the device its plan picked, the CPU // with CoreML's error after a failed read, the CPU where there's no CoreML, -// and a slow line with its fix under Rosetta +// and a slow line with its fix under Rosetta and under Windows on Arm's +// x64 emulation const runWords = { - en: { ne: "On, with CoreML on the Neural Engine", gpu: "On, with CoreML on the GPU", ml: "On, with CoreML", failed: "On, on the CPU: CoreML failed (prediction failed)", cpu: "On, on the CPU", rosetta: "On, slowly under Rosetta: the next update installs magpie for Apple silicon" }, - zh: { ne: "已开启,通过 CoreML 运行在神经网络引擎", gpu: "已开启,通过 CoreML 运行在 GPU", ml: "已开启,通过 CoreML 运行", failed: "已开启,运行在 CPU:CoreML 出错(prediction failed)", cpu: "已开启,运行在 CPU", rosetta: "已开启,但在 Rosetta 下运行较慢:下次更新会装上 Apple 芯片版 magpie" }, - "zh-TW": { ne: "已開啟,透過 CoreML 執行於神經網路引擎", gpu: "已開啟,透過 CoreML 執行於 GPU", ml: "已開啟,透過 CoreML 執行", failed: "已開啟,執行於 CPU:CoreML 出錯(prediction failed)", cpu: "已開啟,執行於 CPU", rosetta: "已開啟,但在 Rosetta 下執行較慢:下次更新會裝上 Apple 晶片版 magpie" }, - ja: { ne: "オン、CoreML により Neural Engine で実行中", gpu: "オン、CoreML により GPU で実行中", ml: "オン、CoreML で実行中", failed: "オン、CPU で実行中:CoreML が失敗しました(prediction failed)", cpu: "オン、CPU で実行中", rosetta: "オン、Rosetta で動作中のため低速です。次のアップデートで Apple シリコン版の magpie が入ります" }, - de: { ne: "An, mit CoreML auf der Neural Engine", gpu: "An, mit CoreML auf der GPU", ml: "An, mit CoreML", failed: "An, auf der CPU: CoreML ist ausgefallen (prediction failed)", cpu: "An, auf der CPU", rosetta: "An, unter Rosetta langsam: das nächste Update installiert magpie für Apple Silicon" }, + en: { ne: "On, with CoreML on the Neural Engine", gpu: "On, with CoreML on the GPU", ml: "On, with CoreML", failed: "On, on the CPU: CoreML failed (prediction failed)", cpu: "On, on the CPU", rosetta: "On, slowly under Rosetta: the next update installs magpie for Apple silicon", emulated: "On, slowly under x64 emulation: the next update installs magpie for Arm" }, + zh: { ne: "已开启,通过 CoreML 运行在神经网络引擎", gpu: "已开启,通过 CoreML 运行在 GPU", ml: "已开启,通过 CoreML 运行", failed: "已开启,运行在 CPU:CoreML 出错(prediction failed)", cpu: "已开启,运行在 CPU", rosetta: "已开启,但在 Rosetta 下运行较慢:下次更新会装上 Apple 芯片版 magpie", emulated: "已开启,但在 x64 转译下运行较慢:下次更新会装上 ARM 版 magpie" }, + "zh-TW": { ne: "已開啟,透過 CoreML 執行於神經網路引擎", gpu: "已開啟,透過 CoreML 執行於 GPU", ml: "已開啟,透過 CoreML 執行", failed: "已開啟,執行於 CPU:CoreML 出錯(prediction failed)", cpu: "已開啟,執行於 CPU", rosetta: "已開啟,但在 Rosetta 下執行較慢:下次更新會裝上 Apple 晶片版 magpie", emulated: "已開啟,但在 x64 模擬下執行較慢:下次更新會裝上 ARM 版 magpie" }, + ja: { ne: "オン、CoreML により Neural Engine で実行中", gpu: "オン、CoreML により GPU で実行中", ml: "オン、CoreML で実行中", failed: "オン、CPU で実行中:CoreML が失敗しました(prediction failed)", cpu: "オン、CPU で実行中", rosetta: "オン、Rosetta で動作中のため低速です。次のアップデートで Apple シリコン版の magpie が入ります", emulated: "オン、x64 エミュレーションで動作中のため低速です。次のアップデートで ARM 版の magpie が入ります" }, + de: { ne: "An, mit CoreML auf der Neural Engine", gpu: "An, mit CoreML auf der GPU", ml: "An, mit CoreML", failed: "An, auf der CPU: CoreML ist ausgefallen (prediction failed)", cpu: "An, auf der CPU", rosetta: "An, unter Rosetta langsam: das nächste Update installiert magpie für Apple Silicon", emulated: "An, unter x64-Emulation langsam: das nächste Update installiert magpie für Arm" }, }; const runs = { ne: { via: "CoreML", device: "Neural Engine" }, gpu: { via: "CoreML", device: "GPU" }, ml: { via: "CoreML" }, failed: { device: "CPU", gpuError: "prediction failed" }, cpu: { device: "CPU" }, // the Intel build on Apple silicon (cdredfox on Discord: 150% of a core) rosetta: { device: "CPU", rosetta: true }, + // the x64 build on Windows on Arm (@EV_John_Liu on X) + emulated: { device: "CPU", emulated: true }, }; // Privacy Filter moved to the CPU after its GPU was lost (plugins#853, diff --git a/internal/pii/arch.go b/internal/pii/arch.go index 32b6dbf1d..195cdeeb9 100644 --- a/internal/pii/arch.go +++ b/internal/pii/arch.go @@ -14,13 +14,29 @@ var osArch = runtime.GOOS + "-" + runtime.GOARCH // Rosetta before macOS 15 has no AVX, so the Intel pf-serve ended with // SIGILL on its first instruction of it, on Metal and on the CPU alike // (echo_yip on X); Apple silicon's runs there natively, on Metal. +// +// On Windows on Arm it is the other way round: pf-serve has no arm64 +// build, and Windows 11 runs the x64 one emulated, as it ran the whole of +// an x64 magpie there. The arm64 magpie an x64 one updates to (update's +// arch) finds the x64 pf-serve already on disk and goes on with it; one +// that found no pf-serve for windows-arm64 left the model "not +// downloaded" while it was on in the settings. func target() string { if osArch == "darwin-amd64" && translated() { return "darwin-arm64" } + if osArch == "windows-arm64" { + return "windows-amd64" + } return osArch } +// onArmWindows says pf-serve, the x64 one, runs emulated on Windows on +// Arm, whose emulation may lack AVX2 (before Windows 11 24H2): fullX86 +// can't ask an x64 CPU there, so pf-serve is tried and then +// pf-serve-base. +func onArmWindows() bool { return osArch == "windows-arm64" } + // fullX86 says the CPU has what pf-serve is built for on x86 (build.sh's // x86): AVX, AVX2, FMA and BMI2, with F16C, which every CPU with AVX2 // has. Without them pf-serve ends with SIGILL (a Celeron, a Pentium, a diff --git a/internal/pii/arch_test.go b/internal/pii/arch_test.go index 56269f01c..3b0630dc3 100644 --- a/internal/pii/arch_test.go +++ b/internal/pii/arch_test.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "reflect" + "strings" "testing" ) @@ -146,3 +147,52 @@ func TestRosettaKeepsIntelsWhereNoneIsFetched(t *testing.T) { t.Fatalf("the Intel pf-serve was deleted: %v", err) } } + +// onArm runs the test as a magpie for windows-arm64: built for it, or the +// x64 one updated to it on Windows on Arm (update's arch). +func onArm(t *testing.T) { + t.Helper() + was := osArch + t.Cleanup(func() { osArch = was }) + osArch = "windows-arm64" +} + +// pf-serve has no arm64 build: Windows on Arm runs the x64 one, emulated, +// as it ran the x64 magpie that updated to arm64 there. Before, an arm64 +// magpie had no Privacy Filter, and the x64 one's update left the model +// on in the settings and "not downloaded". +func TestArmWindowsRunsX64Serve(t *testing.T) { + onArm(t) + if target() != "windows-amd64" || !Supported() { + t.Fatalf("target = %s, Supported = %v on windows-arm64", target(), Supported()) + } + if !strings.HasSuffix(serveDir(), "-windows-amd64") { + t.Fatalf("serveDir = %s, not the x64 pf-serve's", serveDir()) + } + // on the CPU: the x64 pf-serve-vulkan has no x64 Vulkan driver there + t.Run("on the CPU", func(t *testing.T) { + log := fakeInstall(t, "ok", true) + noFullX86(t, true) + if !Installed() { + t.Fatal("the x64 pf-serve on disk isn't installed") + } + if _, err := Find([]string{"hi Alice"}); err != nil { + t.Fatal(err) + } + if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve CPU"}) { + t.Fatalf("starts = %q", starts) + } + }) + // an emulation without AVX2 ends pf-serve at once: pf-serve-base then + t.Run("no AVX2", func(t *testing.T) { + log := fakeInstall(t, "noavx2", true) + withBase(t) + noFullX86(t, true) + if _, err := Find([]string{"hi Alice"}); err != nil { + t.Fatal(err) + } + if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve CPU", "start pf-serve-base CPU"}) { + t.Fatalf("starts = %q", starts) + } + }) +} diff --git a/internal/pii/astrlink.go b/internal/pii/astrlink.go index 8c92f20b4..d89793bbe 100644 --- a/internal/pii/astrlink.go +++ b/internal/pii/astrlink.go @@ -344,7 +344,7 @@ func (guardModel) Find(ctx context.Context, texts []string) ([][]redact.Found, e } func (guardModel) Now() Status { - s := Status{Model: AstrLinkGuard, Supported: true, Installed: guardInstalled(), Size: guardSize(), Rosetta: translated()} + s := Status{Model: AstrLinkGuard, Supported: true, Installed: guardInstalled(), Size: guardSize(), Rosetta: translated(), Emulated: emulated()} gdl.fill(&s) gsrv.mu.Lock() if gsrv.m != nil { diff --git a/internal/pii/astrlink_test.go b/internal/pii/astrlink_test.go index 321ac989a..ef89031fd 100644 --- a/internal/pii/astrlink_test.go +++ b/internal/pii/astrlink_test.go @@ -422,3 +422,23 @@ func TestGuardNowSaysRosetta(t *testing.T) { t.Error("Privacy Filter says Rosetta, though its pf-serve is Apple silicon's") } } + +// Now says when AstrLink Guard runs under Windows on Arm's x64 emulation, +// as under Rosetta: the next update installs the arm64 magpie, where it +// runs natively. Privacy Filter's pf-serve is x64 on either build, so it +// says nothing. +func TestGuardNowSaysEmulated(t *testing.T) { + guardSandbox(t) + was := emulated + t.Cleanup(func() { emulated = was }) + for _, em := range []bool{false, true} { + emulated = func() bool { return em } + if st := Of(AstrLinkGuard).Now(); st.Emulated != em || st.Rosetta { + t.Errorf("emulated=%v: AstrLink Guard's Emulated = %v, Rosetta = %v", em, st.Emulated, st.Rosetta) + } + } + emulated = func() bool { return true } + if st := Of(PrivacyFilter).Now(); st.Emulated { + t.Error("Privacy Filter says emulated, though the update leaves its pf-serve x64") + } +} diff --git a/internal/pii/main_test.go b/internal/pii/main_test.go index 6ea203f93..d4400237d 100644 --- a/internal/pii/main_test.go +++ b/internal/pii/main_test.go @@ -44,6 +44,8 @@ const ( // badmodel says it couldn't read the model's file, by the name // it was given // novulkan as pf-serve-vulkan, says Vulkan is missing; else ok +// noavx2 as pf-serve, ends at once as on a CPU (or an emulation) +// without AVX2, SIGILL; else ok // gpuloadhang on the GPU, never says the model is loaded; else ok // asserts fails a ggml assert at its first request // gpuhang on the GPU, never answers a request; else ok @@ -94,6 +96,9 @@ func fakeServe(mode string) { json.NewEncoder(os.Stdout).Encode(map[string]string{"error": "failed to read GGUF: " + model}) os.Exit(1) } + if mode == "noavx2" && name == "pf-serve" { + os.Exit(132) + } if mode == "noload" || mode == "novulkan" && name == "pf-serve-vulkan" { json.NewEncoder(os.Stdout).Encode(map[string]string{"error": "can't load " + mode}) os.Exit(1) diff --git a/internal/pii/pii.go b/internal/pii/pii.go index 509840a4a..6d8df460d 100644 --- a/internal/pii/pii.go +++ b/internal/pii/pii.go @@ -176,6 +176,9 @@ type Status struct { // in Go and translated, several times slower than in Apple silicon's // build, which has CoreML too Rosetta bool `json:"rosetta,omitempty"` + // Emulated: AstrLink Guard runs in an x64 magpie under Windows on + // Arm's emulation, translated as under Rosetta + Emulated bool `json:"emulated,omitempty"` } // Now is the model's status. diff --git a/internal/pii/rosetta.go b/internal/pii/rosetta.go index 4172130c5..58b89880b 100644 --- a/internal/pii/rosetta.go +++ b/internal/pii/rosetta.go @@ -1,7 +1,14 @@ package pii -import "github.com/yetone/magpie/internal/rosetta" +import ( + "github.com/yetone/magpie/internal/rosetta" + "github.com/yetone/magpie/internal/winarm" +) // translated says magpie runs under Rosetta: an Intel build on Apple // silicon. Tests replace it. var translated = rosetta.Translated + +// emulated says magpie runs under Windows on Arm's x64 emulation: an x64 +// build on an Arm64 computer. Tests replace it. +var emulated = winarm.Emulated diff --git a/internal/pii/serve.go b/internal/pii/serve.go index adfbb5674..80b215357 100644 --- a/internal/pii/serve.go +++ b/internal/pii/serve.go @@ -175,7 +175,18 @@ type candidate struct { // A CPU without what pf-serve is built for runs pf-serve-base, on the // CPU alone: pf-serve-vulkan is built like pf-serve, and would end the // same way. +// +// On Windows on Arm pf-serve runs emulated (onArmWindows): on the CPU, +// since the x64 pf-serve-vulkan has no x64 Vulkan driver to load there, +// pf-serve and then pf-serve-base, for an emulation without AVX2. func candidates(cpu bool) []candidate { + if onArmWindows() { + out := []candidate{{exe("pf-serve"), true}} + if _, err := os.Stat(exe("pf-serve-base")); err == nil { + out = append(out, candidate{exe("pf-serve-base"), true}) + } + return out + } if !fullX86() { if _, err := os.Stat(exe("pf-serve-base")); err == nil { return []candidate{{exe("pf-serve-base"), true}} diff --git a/internal/update/rosetta_test.go b/internal/update/rosetta_test.go index b78c03ae7..4abc10e76 100644 --- a/internal/update/rosetta_test.go +++ b/internal/update/rosetta_test.go @@ -18,9 +18,10 @@ import ( // not. func onSystem(t *testing.T, sys, arch string, tr bool) { t.Helper() - wasOS, wasArch, wasTr := goos, goarch, translated - t.Cleanup(func() { goos, goarch, translated = wasOS, wasArch, wasTr }) + wasOS, wasArch, wasTr, wasEm := goos, goarch, translated, emulated + t.Cleanup(func() { goos, goarch, translated, emulated = wasOS, wasArch, wasTr, wasEm }) goos, goarch, translated = sys, arch, func() bool { return tr } + emulated = func() bool { return false } } // An Intel magpie under Rosetta updates to Apple silicon's build, the app diff --git a/internal/update/update.go b/internal/update/update.go index 6de718969..cc9d08d0b 100644 --- a/internal/update/update.go +++ b/internal/update/update.go @@ -38,6 +38,7 @@ import ( "github.com/yetone/magpie/internal/rosetta" "github.com/yetone/magpie/internal/settings" "github.com/yetone/magpie/internal/source" + "github.com/yetone/magpie/internal/winarm" ) // Site is magpie's home; its /api/latest is the update feed. @@ -304,11 +305,13 @@ func parse(v string) *semver { // one. var GUI bool -// goos and goarch are the system this magpie was built for, and -// translated says it runs under Rosetta; tests set them. +// goos and goarch are the system this magpie was built for, translated +// says it runs under Rosetta, and emulated under Windows on Arm's x64 +// emulation; tests set them. var ( goos, goarch = runtime.GOOS, runtime.GOARCH translated = rosetta.Translated + emulated = winarm.Emulated ) // arch is the architecture an update installs: this build's, but Apple @@ -318,8 +321,14 @@ var ( // fetched the Intel build again. Installing Apple silicon's is the same // swap: the zip's magpie.app is renamed over the bundle, signed by the // same team (sameSigner), and the bare binary is renamed over itself. +// +// The same on Windows: an x64 magpie on an Arm64 computer, emulated, +// updates to the arm64 exe (@EV_John_Liu on X, "magpie是转译的"). The swap +// is the x64 one: canStart starts the download, which runs natively there, +// the running exe is moved aside and the arm64 one renamed in under its +// name, and magpie.com is written again from the new app's own launcher. func arch() string { - if goos == "darwin" && goarch == "amd64" && translated() { + if goarch == "amd64" && (goos == "darwin" && translated() || goos == "windows" && emulated()) { return "arm64" } return goarch diff --git a/internal/update/winarm_test.go b/internal/update/winarm_test.go new file mode 100644 index 000000000..7671667a5 --- /dev/null +++ b/internal/update/winarm_test.go @@ -0,0 +1,93 @@ +package update + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" +) + +// onWindows runs the test as a Windows magpie built for arch, under Windows +// on Arm's x64 emulation or not. +func onWindows(t *testing.T, arch string, em bool) { + t.Helper() + onSystem(t, "windows", arch, false) + emulated = func() bool { return em } +} + +// An x64 magpie emulated on Windows on Arm updates to the arm64 exe, the +// app and the terminal build alike (@EV_John_Liu on X); an x64 Windows +// computer, and an arm64 build, update to their own. Emulation is +// Windows' alone: a Mac or Linux magpie asked about it is unchanged. +func TestWindowsOnArmUpdatesToArm64(t *testing.T) { + was := GUI + t.Cleanup(func() { GUI = was }) + for _, c := range []struct { + os, arch string + em, gui bool + bin string + }{ + {"windows", "amd64", true, true, "magpie-windows-arm64.exe"}, + {"windows", "amd64", true, false, "magpie-cli-windows-arm64.exe"}, + {"windows", "amd64", false, true, "magpie-windows-amd64.exe"}, + {"windows", "amd64", false, false, "magpie-cli-windows-amd64.exe"}, + {"windows", "arm64", false, true, "magpie-windows-arm64.exe"}, + {"darwin", "amd64", true, false, "magpie-cli-darwin-amd64"}, + {"linux", "amd64", true, true, "magpie-linux-amd64"}, + } { + onSystem(t, c.os, c.arch, false) + emulated = func() bool { return c.em } + GUI = c.gui + if got := BinaryAsset(); got != c.bin { + t.Errorf("%s/%s emulated=%v gui=%v: BinaryAsset = %s, want %s", c.os, c.arch, c.em, c.gui, got, c.bin) + } + } +} + +// Emulated, the update downloads the arm64 exe, not the x64 one, and swaps +// it in for the running x64 exe as any update is: under the same name, the +// old one moved aside. +func TestWindowsOnArmInstallsArm64Exe(t *testing.T) { + was := GUI + t.Cleanup(func() { GUI = was }) + GUI = true + onWindows(t, "amd64", true) + wasStart := canStart + t.Cleanup(func() { canStart = wasStart }) + canStart = func(string) error { return nil } + + bodies := map[string]string{ + "magpie-windows-amd64.exe": "2.0 for x64", + "magpie-windows-arm64.exe": "2.0 for arm64", + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(bodies[strings.TrimPrefix(r.URL.Path, "/")])) + })) + defer srv.Close() + assets := map[string]Asset{} + for name, body := range bodies { + sum := sha256.Sum256([]byte(body)) + assets[name] = Asset{URL: srv.URL + "/" + name, Size: int64(len(body)), SHA256: hex.EncodeToString(sum[:])} + } + rel := &Release{Version: "2.0", Assets: assets} + + exe := filepath.Join(t.TempDir(), "magpie.exe") + if err := os.WriteFile(exe, []byte("1.0 for x64"), 0o755); err != nil { + t.Fatal(err) + } + staged, err := stageBinaryFor(context.Background(), rel.Assets[BinaryAsset()], exe) + if err != nil { + t.Fatal(err) + } + if _, err := InstallBinaryAside(staged, exe); err != nil { + t.Fatal(err) + } + if got, _ := os.ReadFile(exe); string(got) != "2.0 for arm64" { + t.Fatalf("magpie.exe is %q, want the arm64 build", got) + } +} diff --git a/internal/winarm/winarm.go b/internal/winarm/winarm.go new file mode 100644 index 000000000..9eb51cb75 --- /dev/null +++ b/internal/winarm/winarm.go @@ -0,0 +1,17 @@ +// Package winarm says whether magpie runs under Windows on Arm's x64 +// emulation: an x64 build on an Arm64 computer. Such a magpie works, but +// its own heavy code (the local masking model) runs translated, and every +// update fetched the x64 build again (@EV_John_Liu on X: "magpie是转译的"). +// It is Rosetta's counterpart (internal/rosetta), kept apart because what +// magpie tells the user differs. +package winarm + +import "debug/pe" + +// emulated says a process of goarch runs emulated on a computer whose +// native machine is native, as IsWow64Process2 said, or failed to (err). +// A failed call (Windows before 10 1511, which has no IsWow64Process2) is +// not translated, and so is any build but x64: an x86 magpie isn't built. +func emulated(goarch string, native uint16, err error) bool { + return err == nil && goarch == "amd64" && native == pe.IMAGE_FILE_MACHINE_ARM64 +} diff --git a/internal/winarm/winarm_other.go b/internal/winarm/winarm_other.go new file mode 100644 index 000000000..c4334473d --- /dev/null +++ b/internal/winarm/winarm_other.go @@ -0,0 +1,7 @@ +//go:build !windows + +package winarm + +// Emulated says this process runs under Windows on Arm's x64 emulation: +// never off Windows. +func Emulated() bool { return false } diff --git a/internal/winarm/winarm_test.go b/internal/winarm/winarm_test.go new file mode 100644 index 000000000..1b39c8d30 --- /dev/null +++ b/internal/winarm/winarm_test.go @@ -0,0 +1,36 @@ +package winarm + +import ( + "debug/pe" + "errors" + "testing" +) + +// Only an x64 build whose computer is Arm64 is emulated; a failed +// IsWow64Process2 is native. +func TestEmulated(t *testing.T) { + for _, c := range []struct { + arch string + native uint16 + err error + want bool + }{ + {"amd64", pe.IMAGE_FILE_MACHINE_ARM64, nil, true}, + {"amd64", pe.IMAGE_FILE_MACHINE_AMD64, nil, false}, + {"amd64", pe.IMAGE_FILE_MACHINE_ARM64, errors.New("procedure not found"), false}, + {"arm64", pe.IMAGE_FILE_MACHINE_ARM64, nil, false}, + {"386", pe.IMAGE_FILE_MACHINE_ARM64, nil, false}, + } { + if got := emulated(c.arch, c.native, c.err); got != c.want { + t.Errorf("emulated(%s, %#x, %v) = %v, want %v", c.arch, c.native, c.err, got, c.want) + } + } +} + +// Off Windows, and on an x64 Windows computer, this process isn't +// emulated: the CI and test machines are one or the other. +func TestNotEmulatedHere(t *testing.T) { + if Emulated() { + t.Fatal("Emulated() on a test machine that isn't Windows on Arm") + } +} diff --git a/internal/winarm/winarm_windows.go b/internal/winarm/winarm_windows.go new file mode 100644 index 000000000..26128c76d --- /dev/null +++ b/internal/winarm/winarm_windows.go @@ -0,0 +1,18 @@ +package winarm + +import ( + "runtime" + "sync" + + "golang.org/x/sys/windows" +) + +// Emulated says this process is an x64 magpie under Windows on Arm's +// emulation. IsWow64Process2 answers an x64 process there with +// processMachine IMAGE_FILE_MACHINE_UNKNOWN (x64 emulation isn't WOW64) +// and nativeMachine IMAGE_FILE_MACHINE_ARM64. +var Emulated = sync.OnceValue(func() bool { + var proc, native uint16 + err := windows.IsWow64Process2(windows.CurrentProcess(), &proc, &native) + return emulated(runtime.GOARCH, native, err) +})