diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 4155f2c54..427fca23c 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -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"); } diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 25ab89d4d..95343bc64 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -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", diff --git a/internal/gui/tests/privacy-model.test.cjs b/internal/gui/tests/privacy-model.test.cjs index 038f22713..1a0025b52 100644 --- a/internal/gui/tests/privacy-model.test.cjs +++ b/internal/gui/tests/privacy-model.test.cjs @@ -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 diff --git a/internal/pii/astrlink.go b/internal/pii/astrlink.go index 14c3ed2d9..8c92f20b4 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()} + s := Status{Model: AstrLinkGuard, Supported: true, Installed: guardInstalled(), Size: guardSize(), Rosetta: translated()} 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 171394d58..321ac989a 100644 --- a/internal/pii/astrlink_test.go +++ b/internal/pii/astrlink_test.go @@ -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") + } +} diff --git a/internal/pii/pii.go b/internal/pii/pii.go index 1cfd37063..3359a572a 100644 --- a/internal/pii/pii.go +++ b/internal/pii/pii.go @@ -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. diff --git a/internal/pii/rosetta.go b/internal/pii/rosetta.go new file mode 100644 index 000000000..4172130c5 --- /dev/null +++ b/internal/pii/rosetta.go @@ -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 diff --git a/internal/pii/rosetta_darwin.go b/internal/pii/rosetta_darwin.go deleted file mode 100644 index ae96efa6e..000000000 --- a/internal/pii/rosetta_darwin.go +++ /dev/null @@ -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 -}) diff --git a/internal/pii/rosetta_other.go b/internal/pii/rosetta_other.go deleted file mode 100644 index 9de6b16bd..000000000 --- a/internal/pii/rosetta_other.go +++ /dev/null @@ -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 } diff --git a/internal/rosetta/rosetta_darwin.go b/internal/rosetta/rosetta_darwin.go new file mode 100644 index 000000000..6c3f7103c --- /dev/null +++ b/internal/rosetta/rosetta_darwin.go @@ -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 +}) diff --git a/internal/rosetta/rosetta_other.go b/internal/rosetta/rosetta_other.go new file mode 100644 index 000000000..7dafaaa8c --- /dev/null +++ b/internal/rosetta/rosetta_other.go @@ -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 } diff --git a/internal/update/rosetta_test.go b/internal/update/rosetta_test.go new file mode 100644 index 000000000..b78c03ae7 --- /dev/null +++ b/internal/update/rosetta_test.go @@ -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), "2.0-arm64") { + t.Errorf("Apple silicon's app isn't in place: %s", b) + } +} diff --git a/internal/update/update.go b/internal/update/update.go index 5d6f6f52d..abbc166f3 100644 --- a/internal/update/update.go +++ b/internal/update/update.go @@ -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