Files
magpie/internal/agent/telemetry_all_test.go
T
yetone 1d41799f2a agent: turn off every agent's telemetry from Settings › Privacy (OipsAnthony on X)
OipsAnthony on X: "magpie 设置 - 隐私,支持统一关闭所有 Agent 的遥测功能,
这个统一管理的地方优先级最高". v0.1.3562 gave Codex, Claude Code, Gemini
CLI and Qwen Code a Telemetry row each; there was no one switch for all.

Settings › Privacy now has "Turn off telemetry for all agents"
(settings.AgentTelemetryOff, POST /api/settings/agent-telemetry,
agent.SetTelemetryAll in internal/agent/telemetry_all.go):

- On: every agent found here with a telemetry switch is turned off through
  its own switch and that switch's rules (only keys the file lacks are
  written; a value of the user's own that keeps telemetry on is never
  written over: the agent stays on and the reply's "refused" names it
  and why). Each agent the switch turned off gets a stash marker,
  <id>.telemetry.all. An agent found later is turned off on the next
  state the window reads and by a one-minute gateway loop
  (KeepTelemetryAllOff, in WhileServing).
- While on, each agent's own Telemetry row shows off, says "Off for all
  agents in Settings › Privacy" and its buttons are disabled; the API
  refuses it too (ErrTelemetryAllOff).
- Off: only the marked agents are turned back on, which takes out only
  magpie's keys. An agent off by its own row or the user's own config
  before stays off.
- An unreadable settings.json is unknown: nothing is written.
- The setting is per computer (KeepOwn) and the generic settings POST
  can't change it (settingsapi.Merge carries it).

app.js pays for its ~650 bytes with ten Privacy-section comments cut
(core 1,533,550, under its 1,533,802 budget, no raise).

Run (mote, first on 10bef5fe0):
- go test -count=1 ./internal/agent ./internal/gui ./internal/cli
  ./internal/settings ./internal/settingsapi: ok, except gui's
  TestSettingsSaveKeepsWhatItDoesNotSend, red as it should be for a new
  field; with AgentTelemetryOff in its fixture it and the budget and
  parity tests pass (-run).
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./...: clean. gofmt -l: clean.
- GUI, chromium and webkit: privacy-*.test.cjs (new
  privacy-agent-telemetry: 5 languages x 1100/640px plus a refused case
  in each engine, no scroll on click, no native select), i18n-*,
  agent-telemetry, settings-search, click-scroll: 218 of 218 pass.
- The whole GUI suite (1,367 files) was started under ~/suite.lock and
  never got it: lockf gave up after 4h with the lock held by other runs.

- Rebased on 6a81a3c62 (this commit's parent), again: go vet
  ./internal/... (darwin) and GOOS=windows vet of agent, gui, cli; the
  Telemetry, budget, settings-keep, CLI parity and route tests; GUI
  privacy-agent-telemetry, agent-telemetry, i18n-keys and
  privacy-usage-stats in both engines: 64 of 64.

Broken by hand:
- no stash marker in keepTelemetryOff: TestTelemetryAllOff red, "Gemini
  CLI not back on".
- SetTelemetry without the ErrTelemetryAllOff check: TestTelemetryAllOff
  red, "Gemini CLI's row turned on under the switch".

Not tried: a real agent's config on this computer (sandbox HOME only);
Windows runs of the new Go tests (they skip there, a #! stand-in isn't a
command on Windows; the switch's code has no OS-specific path).
2026-11-11 02:38:58 +08:00

138 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package agent
import (
"errors"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/testenv"
)
// onPath puts the agents' commands on PATH, which is how they are found
// (Gemini CLI by its binary alone).
func onPath(t *testing.T, names ...string) {
t.Helper()
if runtime.GOOS == "windows" {
t.Skip("a #! stand-in isn't a command on Windows")
}
bin := t.TempDir()
for _, n := range names {
testenv.Program(t, filepath.Join(bin, n), "#!/bin/sh\n")
}
t.Setenv("PATH", bin)
}
// OipsAnthony on X: Settings › Privacy turns off every agent's telemetry,
// over each agent's own row, an agent found later too; off again, each
// agent is back to its own row's choice, and a value of the user's own is
// never written over.
func TestTelemetryAllOff(t *testing.T) {
home, gpath := telemetryHome(t, ".gemini/settings.json", `{"ui": {"theme": "x"}}`)
onPath(t, "gemini", "qwen", "codex")
qpath := filepath.Join(home, ".qwen", "settings.json")
writeAt(t, qpath, `{}`)
// Codex: the user keeps analytics on themselves
const codexTheirs = "[analytics]\nenabled = true\n"
cpath := filepath.Join(home, ".codex", "config.toml")
writeAt(t, cpath, codexTheirs)
// Qwen Code off by its own row before
if err := qwen(home).SetTelemetry(true); err != nil {
t.Fatal(err)
}
err := SetTelemetryAll(true)
if err == nil || !strings.Contains(err.Error(), "Codex") {
t.Fatalf("Codex's own on isn't named: %v", err)
}
if !settings.Load().AgentTelemetryOff {
t.Fatal("the switch wasn't saved")
}
if readFile(cpath) != codexTheirs {
t.Fatalf("the user's Codex value was written over:\n%s", readFile(cpath))
}
if v, ok := jsonAt(t, gpath, "privacy.usageStatisticsEnabled"); !ok || v != false {
t.Fatalf("Gemini CLI not turned off:\n%s", readFile(gpath))
}
// an agent's own row is held while it is on
if err := gemini(home).SetTelemetry(false); !errors.Is(err, ErrTelemetryAllOff) {
t.Fatalf("Gemini CLI's row turned on under the switch: %v", err)
}
if err := qwen(home).SetTelemetry(false); !errors.Is(err, ErrTelemetryAllOff) {
t.Fatalf("Qwen Code's row turned on under the switch: %v", err)
}
// Claude Code found later
cl := filepath.Join(home, ".claude", "settings.json")
writeAt(t, cl, `{"model": "opus"}`)
if err := KeepTelemetryOff(claude(home)); err != nil {
t.Fatal(err)
}
for _, k := range []string{"env.DISABLE_TELEMETRY", "env.DISABLE_ERROR_REPORTING"} {
if v, ok := jsonAt(t, cl, k); !ok || v != "1" {
t.Fatalf("Claude Code found later kept %s:\n%s", k, readFile(cl))
}
}
if err := SetTelemetryAll(false); err != nil {
t.Fatal(err)
}
if settings.Load().AgentTelemetryOff {
t.Fatal("the switch is still on")
}
if _, ok := jsonAt(t, gpath, "privacy.usageStatisticsEnabled"); ok {
t.Fatalf("Gemini CLI not back on:\n%s", readFile(gpath))
}
if v, _ := jsonAt(t, gpath, "ui.theme"); v != "x" {
t.Fatalf("the user's Gemini CLI settings moved:\n%s", readFile(gpath))
}
if _, ok := jsonAt(t, cl, "env.DISABLE_TELEMETRY"); ok {
t.Fatalf("Claude Code not back on:\n%s", readFile(cl))
}
// Qwen Code's own row said off: it stays off
if st := telemetryOf(t, qwen(home)); !st.Off {
t.Fatalf("Qwen Code's own off was undone: %+v", st)
}
if readFile(cpath) != codexTheirs {
t.Fatalf("Codex's file changed:\n%s", readFile(cpath))
}
// and the rows are the user's again
if err := qwen(home).SetTelemetry(false); err != nil {
t.Fatal(err)
}
}
// An agent the user turned off in its own config stays as they wrote it
// when the switch goes off again.
func TestTelemetryAllLeavesTheUsersOff(t *testing.T) {
const theirs = `{"privacy": {"usageStatisticsEnabled": false}}`
_, gpath := telemetryHome(t, ".gemini/settings.json", theirs)
onPath(t, "gemini")
if err := SetTelemetryAll(true); err != nil {
t.Fatal(err)
}
if err := SetTelemetryAll(false); err != nil {
t.Fatal(err)
}
if readFile(gpath) != theirs {
t.Fatalf("the user's own off was changed:\n%s", readFile(gpath))
}
}
// An unreadable settings.json is unknown: nothing is turned off by it.
func TestTelemetryAllUnreadableSettings(t *testing.T) {
home, gpath := telemetryHome(t, ".gemini/settings.json", `{}`)
writeAt(t, settings.Path(), `{"agentTelemetryOff": true`)
if TelemetryAllOff() {
t.Fatal("an unreadable settings.json read as on")
}
if err := KeepTelemetryOff(gemini(home)); err == nil {
t.Fatal("no error for an unreadable settings.json")
}
if readFile(gpath) != `{}` {
t.Fatalf("written under an unreadable settings.json:\n%s", readFile(gpath))
}
}