An agent with a documented telemetry switch has a Telemetry row in its
Settings on the Agents page (opened row and settings sheet). Off writes
that switch into the agent's own config the way magpie writes it already:
- Codex: [analytics] enabled = false in config.toml (feedback left alone)
- Claude Code: env DISABLE_TELEMETRY and DISABLE_ERROR_REPORTING = "1"
- Gemini CLI, Qwen Code: privacy.usageStatisticsEnabled = false
Nothing is written until the user turns it off. Off writes only keys the
file lacks and records them in the stash; a key the user set on refuses
Off and names it; one they set off stays, and the row says so. On takes
out only the keys magpie wrote that still hold magpie's value. An
unreadable config is unknown: the row says so and can't be pressed.
Agent.Telemetry is not a Field, so profiles never carry it and Unwire
leaves it. Only agents on this computer (no WSL distro, #3896).
POST /api/agents/telemetry/{id}; strings in zh, zh-TW, ja, de.
The row is paid for in the core budget with sixteen Agents-page comments
cut in app.js (core 1,532,840 of 1,533,802 after the rebase), no raise.
Run (mote, at this head on origin/main 1ad9d157f; vet, the three packages and agent-telemetry, i18n-keys, agent-details-ready, click-scroll again there, 98 of 98):
- go test -run Telemetry ./internal/agent, then go test -count=1
./internal/agent ./internal/gui ./internal/cli: ok
- go vet ./..., GOOS=linux -tags nogui, GOOS=windows: ok; gofmt clean
- GUI, chromium and webkit: agent-telemetry (34 pass: every language at
1100 and 640px, no scroll on click, no select, the user's own off,
a refusal, the sheet), and 104 agent-*, i18n-*, click-scroll,
settings-search, panel-agent and agent-settings files: reds only in
page-width-steady and mobile-web (red the same at origin/main
5a347ca8c, #5013, #4541) and agent-compact once under concurrency 6
(passes alone, 8 of 8; #5607; its fixture has no telemetry)
- internal/cli failed once in the first run with its name lost to a
tail of mine; it passed on a rerun at this head and at origin/main
Broken by hand: skipping the user-on check, dropping the "still ours"
check before a delete, and removing the WSL gate each turned
TestCodexTelemetryLeavesTheUsers, TestClaudeTelemetryChangedSince,
TestUsageStatsTelemetrySwitch and TestTelemetryOnlyHere red. The tests
don't compile on the old code (no Agent.Telemetry).
Not tried: real Codex, Claude Code, Gemini CLI or Qwen Code reading the
written keys (the keys come from their source); Crush (left for later),
OpenCode (no switch); WSL agents; the whole GUI suite (the agent-*,
i18n and agent-settings files only); the full go suite runs after the tag.
323 lines
9.9 KiB
Go
323 lines
9.9 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
// telemetryHome is a sandbox HOME with file (under it) holding body; "" is
|
|
// no file.
|
|
func telemetryHome(t *testing.T, file, body string) (home, path string) {
|
|
t.Helper()
|
|
home = t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("XDG_DATA_HOME", filepath.Join(home, ".local", "share"))
|
|
t.Setenv("CODEX_HOME", "")
|
|
t.Setenv("CLAUDE_CONFIG_DIR", "")
|
|
t.Setenv("QWEN_HOME", "")
|
|
path = filepath.Join(home, file)
|
|
if body != "" {
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return home, path
|
|
}
|
|
|
|
// jsonAt is the value at a dotted path of the JSON file at path, and
|
|
// whether it is there.
|
|
func jsonAt(t *testing.T, path, key string) (any, bool) {
|
|
t.Helper()
|
|
var doc any
|
|
if err := json.Unmarshal([]byte(readFile(path)), &doc); err != nil {
|
|
t.Fatalf("%v:\n%s", err, readFile(path))
|
|
}
|
|
for _, part := range strings.Split(key, ".") {
|
|
m, ok := doc.(map[string]any)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
if doc, ok = m[part]; !ok {
|
|
return nil, false
|
|
}
|
|
}
|
|
return doc, true
|
|
}
|
|
|
|
func telemetryOf(t *testing.T, a *Agent) TelemetryState {
|
|
t.Helper()
|
|
if a.Telemetry == nil {
|
|
t.Fatalf("%s has no telemetry switch", a.ID)
|
|
}
|
|
st, err := a.Telemetry.Get()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return st
|
|
}
|
|
|
|
// OipsAnthony on X: Codex's telemetry off from its page. Off writes
|
|
// [analytics] enabled = false beside what is there; on takes out only that,
|
|
// and the file is as it was.
|
|
func TestCodexTelemetrySwitch(t *testing.T) {
|
|
const before = `model = "gpt-5.5"
|
|
|
|
[features]
|
|
multi_agent = true
|
|
`
|
|
home, path := telemetryHome(t, ".codex/config.toml", before)
|
|
a := codex(home)
|
|
if st := telemetryOf(t, a); st.Off || len(st.Own) != 0 || !reflect.DeepEqual(st.Keys, []string{"analytics.enabled"}) {
|
|
t.Fatalf("before: %+v", st)
|
|
}
|
|
if err := a.SetTelemetry(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := readFile(path)
|
|
if !strings.HasPrefix(got, before) || !strings.Contains(got, "[analytics]\nenabled = false\n") {
|
|
t.Fatalf("off:\n%s", got)
|
|
}
|
|
if st := telemetryOf(t, codex(home)); !st.Off || len(st.Own) != 0 {
|
|
t.Fatalf("after off: %+v", st)
|
|
}
|
|
// off again writes nothing new
|
|
if err := a.SetTelemetry(true); err != nil || readFile(path) != got {
|
|
t.Fatalf("off again: %v\n%s", err, readFile(path))
|
|
}
|
|
if err := codex(home).SetTelemetry(false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if readFile(path) != before {
|
|
t.Fatalf("on:\n%s", readFile(path))
|
|
}
|
|
if st := telemetryOf(t, a); st.Off {
|
|
t.Fatalf("after on: %+v", st)
|
|
}
|
|
}
|
|
|
|
// A Codex with no config.toml yet gets one with the table alone, and on
|
|
// leaves no table behind.
|
|
func TestCodexTelemetryNoConfig(t *testing.T) {
|
|
home, path := telemetryHome(t, ".codex/config.toml", "")
|
|
a := codex(home)
|
|
if err := a.SetTelemetry(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := readFile(path); strings.TrimSpace(got) != "[analytics]\nenabled = false" {
|
|
t.Fatalf("off:\n%s", got)
|
|
}
|
|
if err := a.SetTelemetry(false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := readFile(path); strings.Contains(got, "analytics") {
|
|
t.Fatalf("on:\n%s", got)
|
|
}
|
|
}
|
|
|
|
// The user's own analytics setting is theirs: on is never written over,
|
|
// their own off is left in on "on", and a table written inline isn't
|
|
// added to. Nothing in the file moves.
|
|
func TestCodexTelemetryLeavesTheUsers(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name, body string
|
|
off bool // the file keeps it off already
|
|
}{
|
|
{"on", "[analytics]\nenabled = true # mine\n", false},
|
|
{"inline", "analytics = { }\n", false},
|
|
{"dotted on", "analytics.enabled = true\n", false},
|
|
{"their off", "[analytics]\nenabled = false # mine\n", true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
home, path := telemetryHome(t, ".codex/config.toml", c.body)
|
|
a := codex(home)
|
|
st := telemetryOf(t, a)
|
|
if st.Off != c.off {
|
|
t.Fatalf("state: %+v", st)
|
|
}
|
|
if c.off {
|
|
if !reflect.DeepEqual(st.Own, []string{"analytics.enabled"}) {
|
|
t.Fatalf("own: %+v", st)
|
|
}
|
|
if err := a.SetTelemetry(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// on: theirs stays, and says why it is still off
|
|
if err := a.SetTelemetry(false); err == nil || !strings.Contains(err.Error(), "analytics.enabled") {
|
|
t.Fatalf("on over the user's off: %v", err)
|
|
}
|
|
} else if err := a.SetTelemetry(true); err == nil {
|
|
t.Fatal("off was written over the user's own value")
|
|
}
|
|
if readFile(path) != c.body {
|
|
t.Fatalf("the user's file changed:\n%s", readFile(path))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A config that doesn't parse is unknown: no state is made up for it and
|
|
// nothing is written into it.
|
|
func TestTelemetryUnreadableConfig(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name, file, body string
|
|
of func(string) *Agent
|
|
}{
|
|
{"codex", ".codex/config.toml", "[analytics\nenabled = ", codex},
|
|
{"claude", ".claude/settings.json", `{"env": {`, claude},
|
|
{"gemini", ".gemini/settings.json", `{"privacy": [`, gemini},
|
|
{"qwen", ".qwen/settings.json", `{"privacy": `, qwen},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
home, path := telemetryHome(t, c.file, c.body)
|
|
a := c.of(home)
|
|
if a.Telemetry == nil {
|
|
t.Fatal("no telemetry switch")
|
|
}
|
|
if _, err := a.Telemetry.Get(); err == nil {
|
|
t.Fatal("an unreadable config read as a state")
|
|
}
|
|
if err := a.SetTelemetry(true); err == nil {
|
|
t.Fatal("off written into an unreadable config")
|
|
}
|
|
if readFile(path) != c.body {
|
|
t.Fatalf("written:\n%s", readFile(path))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Claude Code's telemetry and error reports go off in settings.json's env
|
|
// beside the user's variables; on takes out only magpie's, and a variable
|
|
// the user set keeps its value.
|
|
func TestClaudeTelemetrySwitch(t *testing.T) {
|
|
home, path := telemetryHome(t, ".claude/settings.json", `{
|
|
"model": "opus",
|
|
"env": {"FOO": "1", "DISABLE_TELEMETRY": "true"}
|
|
}
|
|
`)
|
|
a := claude(home)
|
|
st := telemetryOf(t, a)
|
|
if st.Off || !reflect.DeepEqual(st.Keys, []string{"env.DISABLE_TELEMETRY", "env.DISABLE_ERROR_REPORTING"}) {
|
|
t.Fatalf("before: %+v", st)
|
|
}
|
|
if err := a.SetTelemetry(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for k, want := range map[string]any{"env.DISABLE_TELEMETRY": "true", "env.DISABLE_ERROR_REPORTING": "1", "env.FOO": "1", "model": "opus"} {
|
|
if v, _ := jsonAt(t, path, k); v != want {
|
|
t.Fatalf("off: %s = %v:\n%s", k, v, readFile(path))
|
|
}
|
|
}
|
|
if st := telemetryOf(t, a); !st.Off || !reflect.DeepEqual(st.Own, []string{"env.DISABLE_TELEMETRY"}) {
|
|
t.Fatalf("after off: %+v", st)
|
|
}
|
|
// on: magpie's goes; the user's DISABLE_TELEMETRY stays, and with
|
|
// error reports back on, telemetry reads as not all off (no error)
|
|
if err := a.SetTelemetry(false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := jsonAt(t, path, "env.DISABLE_ERROR_REPORTING"); ok {
|
|
t.Fatalf("on kept magpie's:\n%s", readFile(path))
|
|
}
|
|
if v, _ := jsonAt(t, path, "env.DISABLE_TELEMETRY"); v != "true" {
|
|
t.Fatalf("on took the user's:\n%s", readFile(path))
|
|
}
|
|
}
|
|
|
|
// A value magpie wrote that the user changed since is theirs: on leaves it.
|
|
func TestClaudeTelemetryChangedSince(t *testing.T) {
|
|
home, path := telemetryHome(t, ".claude/settings.json", `{"env": {}}`)
|
|
a := claude(home)
|
|
if err := a.SetTelemetry(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := edit.SetJSON(path, edit.KV{Path: "env.DISABLE_TELEMETRY", Value: "yes"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.SetTelemetry(false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v, _ := jsonAt(t, path, "env.DISABLE_TELEMETRY"); v != "yes" {
|
|
t.Fatalf("on took the user's changed value:\n%s", readFile(path))
|
|
}
|
|
if _, ok := jsonAt(t, path, "env.DISABLE_ERROR_REPORTING"); ok {
|
|
t.Fatalf("on kept magpie's:\n%s", readFile(path))
|
|
}
|
|
}
|
|
|
|
// Gemini CLI's and Qwen Code's usage statistics go off in settings.json's
|
|
// privacy, beside what is there; their own true is never written over.
|
|
func TestUsageStatsTelemetrySwitch(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name, file string
|
|
of func(string) *Agent
|
|
}{
|
|
{"gemini", ".gemini/settings.json", gemini},
|
|
{"qwen", ".qwen/settings.json", qwen},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
home, path := telemetryHome(t, c.file, `{"privacy": {"other": 1}, "ui": {"theme": "x"}}`)
|
|
a := c.of(home)
|
|
if st := telemetryOf(t, a); st.Off {
|
|
t.Fatalf("before: %+v", st)
|
|
}
|
|
if err := a.SetTelemetry(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v, ok := jsonAt(t, path, "privacy.usageStatisticsEnabled"); !ok || v != false {
|
|
t.Fatalf("off:\n%s", readFile(path))
|
|
}
|
|
if st := telemetryOf(t, a); !st.Off || len(st.Own) != 0 {
|
|
t.Fatalf("after off: %+v", st)
|
|
}
|
|
if err := a.SetTelemetry(false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := jsonAt(t, path, "privacy.usageStatisticsEnabled"); ok {
|
|
t.Fatalf("on:\n%s", readFile(path))
|
|
}
|
|
if v, _ := jsonAt(t, path, "privacy.other"); v != float64(1) {
|
|
t.Fatalf("on moved the user's:\n%s", readFile(path))
|
|
}
|
|
if v, _ := jsonAt(t, path, "ui.theme"); v != "x" {
|
|
t.Fatalf("on moved the user's:\n%s", readFile(path))
|
|
}
|
|
|
|
const theirs = `{"privacy": {"usageStatisticsEnabled": true}}`
|
|
if err := os.WriteFile(path, []byte(theirs), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.SetTelemetry(true); err == nil || readFile(path) != theirs {
|
|
t.Fatalf("off over the user's true: %v\n%s", err, readFile(path))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// An agent in a WSL distro has no switch: its file isn't read on each
|
|
// look (#3896).
|
|
func TestTelemetryOnlyHere(t *testing.T) {
|
|
home, _ := telemetryHome(t, ".codex/config.toml", "")
|
|
at := place{home: home, spell: func(p string) string { return p }}
|
|
for _, a := range []*Agent{codexIn(at), claudeIn(at), geminiIn(at), qwenIn(at)} {
|
|
if a.Telemetry != nil {
|
|
t.Errorf("%s in a distro has a telemetry switch", a.ID)
|
|
}
|
|
}
|
|
if codex(home).Telemetry == nil {
|
|
t.Error("this machine's Codex has none")
|
|
}
|
|
}
|