update: an Intel magpie under Rosetta updates to the Apple silicon build

cdredfox on Discord saw magpie at 150% of a core. Their goroutine dump
shows AstrLink Guard running in Go (guard.geluRow) in a darwin/amd64
build on an 8-core Mac. That is the Intel build under Rosetta: no
CoreML, and Go's kernels are translated, several times slower than
Apple silicon's build. AppAsset and BinaryAsset named runtime.GOARCH,
so every update fetched the Intel build again, with no way off it.

- internal/rosetta: Translated (sysctl.proc_translated), moved out of
  pii so the updater reads the same thing. pii's translated is now that
  var; its tests still replace it.
- update: arch() is arm64 for darwin/amd64 under Rosetta. AppAsset and
  BinaryAsset (the darwin CLI) use it. goos/goarch/translated are vars
  that tests set.
  Checked before switching: Stage downloads the zip, ditto unpacks it,
  sameSigner wants the same team, and Install renames it over the
  bundle. Nothing there reads the architecture, and the CLI's
  InstallBinary is a rename too. An arm64 build runs natively on every
  Mac that runs Rosetta.
- pii: Status.Rosetta, set on AstrLink Guard's Now() only. Privacy
  Filter's pf-serve is already Apple silicon's there (target).
- GUI: Settings > Privacy's local model line, while AstrLink Guard runs
  under Rosetta, says "On, slowly under Rosetta: the next update
  installs magpie for Apple silicon" in place of "On, on the CPU". It
  is the same one sub-line, with no new element. en/zh/zh-TW/ja/de.

Not done, as decided: skipping tool outputs for the model, or capping
its cores.

Run (on mote unless named):
- go test -count=1 ./internal/update ./internal/rosetta ./internal/pii:
  ok. On WSL magpie-desktop: the same three, plus go vet of them, ok.
  On Windows (xiaomi-gpu): -run "Rosetta|Asset|GuardNow" on update and
  pii, ok.
- go vet ./... (darwin) and CGO_ENABLED=0 GOOS=windows go vet ./...: ok.
  A Linux cross vet needs cgo for Wails, and WSL has no gtk4, so Linux
  vet covered the changed packages only. gofmt -l: clean.
- node --test privacy-model, i18n-keys, i18n-en-no-cjk (chromium +
  webkit, 5 languages, 1100/640px): 91 of 92. The new rosetta case
  passed in every engine, language and width. The one red is "a
  download is cancelled" (chromium). It passes alone, 1/1 at origin/main
  8eb219354 and 2/2 with this change. Filed as #5292 (wheelTo/viewOf
  return "" after a reload).
- go test -run "Budget|I18n|Lang" ./internal/gui: ok. Core scripts are
  1,530,017, under 1,533,802; no raise.

Broken by hand:
- arch() always the build's: TestRosettaUpdatesToAppleSilicon red (4
  cases), and TestRosettaStagesAppleSiliconsApp red, with Intel's
  2.0-amd64 app installed.
- No Rosetta in guardModel.Now(): TestGuardNowSaysRosetta red.
- The app.js line off: privacy-model's rosetta case red ("On, on the
  CPU").

Not tried:
- A real Rosetta process. mote has no Rosetta (no oahd); an x86_64 test
  binary built there didn't start. The translated check is the sysctl
  pii has used since the pf-serve fix, but this commit never saw it
  return 1.
- A real update from an Intel release to an arm64 one. The stage test
  swaps ad-hoc signed fake bundles through the real
  Stage/sameSigner/Install.
This commit is contained in:
yetone
2026-11-05 08:25:53 +08:00
parent 681a472cab
commit 6abacd46c3
13 changed files with 207 additions and 34 deletions
+2
View File
@@ -21905,6 +21905,8 @@ function pmSub(m) {
if (m.gpu) return t("On, on the GPU ({device})", { device: m.device });
// AstrLink Guard on a Mac: CoreML, on the device its plan picked
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");
if (m.gpuError && m.model === "astrlink-guard") return t("On, on the CPU: CoreML failed ({error})", { error: m.gpuError });
return m.gpuError ? t("On, on the CPU: the GPU failed ({error})", { error: m.gpuError }) : t("On, on the CPU");
}
+4
View File
@@ -1424,6 +1424,7 @@ const I18N = {
"On, on the GPU ({device})": "已开启,运行在 GPU({device})",
"On, on the CPU: the GPU failed ({error})": "已开启,运行在 CPU:GPU 出错({error})",
"On, on the CPU": "已开启,运行在 CPU",
"On, slowly under Rosetta: the next update installs magpie for Apple silicon": "已开启,但在 Rosetta 下运行较慢:下次更新会装上 Apple 芯片版 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",
@@ -6030,6 +6031,7 @@ const I18N = {
"On, on the GPU ({device})": "已開啟,執行於 GPU({device})",
"On, on the CPU: the GPU failed ({error})": "已開啟,執行於 CPU:GPU 出錯({error})",
"On, on the CPU": "已開啟,執行於 CPU",
"On, slowly under Rosetta: the next update installs magpie for Apple silicon": "已開啟,但在 Rosetta 下執行較慢:下次更新會裝上 Apple 晶片版 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",
@@ -10605,6 +10607,7 @@ const I18N = {
"On, on the GPU ({device})": "オン、GPU で実行中({device})",
"On, on the CPU: the GPU failed ({error})": "オン、CPU で実行中:GPU が失敗しました({error})",
"On, on the CPU": "オン、CPU で実行中",
"On, slowly under Rosetta: the next update installs magpie for Apple silicon": "オン、Rosetta で動作中のため低速です。次のアップデートで Apple シリコン版の 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 で実行中",
@@ -15211,6 +15214,7 @@ const I18N = {
"On, on the GPU ({device})": "An, auf der GPU ({device})",
"On, on the CPU: the GPU failed ({error})": "An, auf der CPU: die GPU ist ausgefallen ({error})",
"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, 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",
+9 -6
View File
@@ -99,17 +99,20 @@ 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
// with CoreML's error after a failed read, the CPU where there's no CoreML,
// and a slow line with its fix under Rosetta
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" },
zh: { ne: "已开启,通过 CoreML 运行在神经网络引擎", gpu: "已开启,通过 CoreML 运行在 GPU", ml: "已开启,通过 CoreML 运行", failed: "已开启,运行在 CPU:CoreML 出错(prediction failed)", cpu: "已开启,运行在 CPU" },
"zh-TW": { ne: "已開啟,透過 CoreML 執行於神經網路引擎", gpu: "已開啟,透過 CoreML 執行於 GPU", ml: "已開啟,透過 CoreML 執行", failed: "已開啟,執行於 CPU:CoreML 出錯(prediction failed)", cpu: "已開啟,執行於 CPU" },
ja: { ne: "オン、CoreML により Neural Engine で実行中", gpu: "オン、CoreML により GPU で実行中", ml: "オン、CoreML で実行中", failed: "オン、CPU で実行中:CoreML が失敗しました(prediction failed)", cpu: "オン、CPU で実行中" },
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" },
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" },
};
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 },
};
// what the row shows: its words and its New mark, whether they rest out of
+1 -1
View File
@@ -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()}
s := Status{Model: AstrLinkGuard, Supported: true, Installed: guardInstalled(), Size: guardSize(), Rosetta: translated()}
gdl.fill(&s)
gsrv.mu.Lock()
if gsrv.m != nil {
+20
View File
@@ -402,3 +402,23 @@ func TestGuardPanicFailsOnlyItsRequest(t *testing.T) {
t.Fatalf("Now = %+v after one request's panic", st)
}
}
// Now says when AstrLink Guard runs under Rosetta, for Settings → Privacy
// to say why it is slow (cdredfox on Discord: 150% of a core in the Intel
// build on Apple silicon); Privacy Filter's pf-serve is Apple silicon's
// there (target), so it says nothing.
func TestGuardNowSaysRosetta(t *testing.T) {
guardSandbox(t)
was := translated
t.Cleanup(func() { translated = was })
for _, tr := range []bool{false, true} {
translated = func() bool { return tr }
if st := Of(AstrLinkGuard).Now(); st.Rosetta != tr {
t.Errorf("translated=%v: AstrLink Guard's Rosetta = %v", tr, st.Rosetta)
}
}
translated = func() bool { return true }
if st := Of(PrivacyFilter).Now(); st.Rosetta {
t.Error("Privacy Filter says Rosetta, though its pf-serve is Apple silicon's")
}
}
+4
View File
@@ -161,6 +161,10 @@ type Status struct {
// (or for the one it is on)
Loading bool `json:"loading,omitempty"`
Waiting int `json:"waiting,omitempty"`
// Rosetta: AstrLink Guard runs in an Intel magpie under Rosetta,
// in Go and translated, several times slower than in Apple silicon's
// build, which has CoreML too
Rosetta bool `json:"rosetta,omitempty"`
}
// Now is the model's status.
+7
View File
@@ -0,0 +1,7 @@
package pii
import "github.com/yetone/magpie/internal/rosetta"
// translated says magpie runs under Rosetta: an Intel build on Apple
// silicon. Tests replace it.
var translated = rosetta.Translated
-14
View File
@@ -1,14 +0,0 @@
package pii
import (
"sync"
"golang.org/x/sys/unix"
)
// translated says magpie runs under Rosetta: an Intel build on Apple
// silicon.
var translated = sync.OnceValue(func() bool {
v, err := unix.SysctlUint32("sysctl.proc_translated")
return err == nil && v == 1
})
-6
View File
@@ -1,6 +0,0 @@
//go:build !darwin
package pii
// translated says magpie runs under Rosetta, which only macOS has.
var translated = func() bool { return false }
+19
View File
@@ -0,0 +1,19 @@
// Package rosetta says whether magpie runs under Rosetta: an Intel build
// on Apple silicon. Such a magpie works, but runs its own heavy code (the
// local masking model) several times slower than Apple silicon's build,
// with no CoreML, and fetches Intel builds of what it downloads unless it
// asks this.
package rosetta
import (
"sync"
"golang.org/x/sys/unix"
)
// Translated says this process runs under Rosetta. A failed read (an
// Intel Mac, where the sysctl doesn't exist) is native.
var Translated = sync.OnceValue(func() bool {
v, err := unix.SysctlUint32("sysctl.proc_translated")
return err == nil && v == 1
})
+8
View File
@@ -0,0 +1,8 @@
//go:build !darwin
// Package rosetta says whether magpie runs under Rosetta, which only
// macOS has.
package rosetta
// Translated says this process runs under Rosetta: never off macOS.
func Translated() bool { return false }
+104
View File
@@ -0,0 +1,104 @@
package update
import (
"context"
"crypto/sha256"
"encoding/hex"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
// onSystem runs the test as a magpie built for sys/arch, translated or
// 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 })
goos, goarch, translated = sys, arch, func() bool { return tr }
}
// An Intel magpie under Rosetta updates to Apple silicon's build, the app
// and the terminal build alike; anywhere else an update is the build's
// own architecture.
func TestRosettaUpdatesToAppleSilicon(t *testing.T) {
was := GUI
t.Cleanup(func() { GUI = was })
for _, c := range []struct {
os, arch string
tr, gui bool
app, bin string
}{
{"darwin", "amd64", true, true, "magpie-darwin-arm64.zip", "magpie-cli-darwin-arm64"},
{"darwin", "amd64", true, false, "magpie-darwin-arm64.zip", "magpie-cli-darwin-arm64"},
{"darwin", "amd64", false, true, "magpie-darwin-amd64.zip", "magpie-cli-darwin-amd64"},
{"darwin", "arm64", false, true, "magpie-darwin-arm64.zip", "magpie-cli-darwin-arm64"},
// only macOS has Rosetta: a translated answer elsewhere changes nothing
{"windows", "amd64", true, true, "magpie-darwin-amd64.zip", "magpie-windows-amd64.exe"},
{"linux", "amd64", true, false, "magpie-darwin-amd64.zip", "magpie-cli-linux-amd64"},
{"linux", "arm64", false, true, "magpie-darwin-arm64.zip", "magpie-linux-arm64"},
} {
onSystem(t, c.os, c.arch, c.tr)
GUI = c.gui
if got := AppAsset(); got != c.app {
t.Errorf("%s/%s translated=%v: AppAsset = %s, want %s", c.os, c.arch, c.tr, got, c.app)
}
if got := BinaryAsset(); got != c.bin {
t.Errorf("%s/%s translated=%v gui=%v: BinaryAsset = %s, want %s", c.os, c.arch, c.tr, c.gui, got, c.bin)
}
}
}
// Under Rosetta the update downloads Apple silicon's zip, not Intel's,
// and swaps it in for the Intel app as any update is: signed by the same
// team, renamed over the bundle.
func TestRosettaStagesAppleSiliconsApp(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("ditto and codesign")
}
stubUnregister(t)
onSystem(t, "darwin", "amd64", true)
root := t.TempDir()
zips := map[string][]byte{}
assets := map[string]Asset{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(zips[strings.TrimPrefix(r.URL.Path, "/")])
}))
defer srv.Close()
for _, a := range []string{"amd64", "arm64"} {
src := filepath.Join(root, "src-"+a)
fakeApp(t, filepath.Join(src, "magpie.app"), "2.0-"+a)
name := "magpie-darwin-" + a + ".zip"
zip := filepath.Join(root, name)
cmd := exec.Command("ditto", "-c", "-k", "--keepParent", "magpie.app", zip)
cmd.Dir = src
if b, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("ditto: %v: %s", err, b)
}
body, err := os.ReadFile(zip)
if err != nil {
t.Fatal(err)
}
sum := sha256.Sum256(body)
zips[name] = body
assets[name] = Asset{URL: srv.URL + "/" + name, SHA256: hex.EncodeToString(sum[:])}
}
bundle := filepath.Join(root, "Applications", "magpie.app")
fakeApp(t, bundle, "1.0-amd64")
staged, err := Stage(context.Background(), &Release{Version: "2.0", Assets: assets}, bundle)
if err != nil {
t.Fatal(err)
}
if err := Install(staged, bundle); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(filepath.Join(bundle, "Contents", "Info.plist"))
if !strings.Contains(string(b), "<string>2.0-arm64</string>") {
t.Errorf("Apple silicon's app isn't in place: %s", b)
}
}
+29 -7
View File
@@ -35,6 +35,7 @@ import (
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/netproxy"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/rosetta"
"github.com/yetone/magpie/internal/source"
)
@@ -199,14 +200,35 @@ func parse(v string) *semver {
// one.
var GUI bool
// AppAsset and BinaryAsset name the files this machine would install.
func AppAsset() string { return "magpie-darwin-" + runtime.GOARCH + ".zip" }
func BinaryAsset() string {
name := "magpie-cli-" + runtime.GOOS + "-" + runtime.GOARCH
if GUI && runtime.GOOS != "darwin" {
name = "magpie-" + runtime.GOOS + "-" + runtime.GOARCH
// goos and goarch are the system this magpie was built for, and
// translated says it runs under Rosetta; tests set them.
var (
goos, goarch = runtime.GOOS, runtime.GOARCH
translated = rosetta.Translated
)
// arch is the architecture an update installs: this build's, but Apple
// silicon's for an Intel magpie under Rosetta. Kept on the Intel build,
// such a Mac ran the local masking model in Go under translation, with no
// CoreML, at 150% of a core (cdredfox on Discord), and every update
// 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.
func arch() string {
if goos == "darwin" && goarch == "amd64" && translated() {
return "arm64"
}
if runtime.GOOS == "windows" {
return goarch
}
// AppAsset and BinaryAsset name the files this machine would install.
func AppAsset() string { return "magpie-darwin-" + arch() + ".zip" }
func BinaryAsset() string {
name := "magpie-cli-" + goos + "-" + arch()
if GUI && goos != "darwin" {
name = "magpie-" + goos + "-" + arch()
}
if goos == "windows" {
name += ".exe"
}
return name