Files
magpie/internal/agent/telemetry.go
T
yetone 720200092e agent: turn off an agent's own telemetry from its settings (OipsAnthony on X)
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.
2026-11-10 17:28:15 +08:00

329 lines
10 KiB
Go

package agent
// An agent's own telemetry, off (OipsAnthony on X: "agent 管理页面支持给每个
// agent 关闭遥测功能"). An agent with a documented switch for the usage data
// it sends its vendor has it in its settings on the Agents page, which
// writes that switch into the agent's own config. Only agents whose key was
// read in their source or binary:
//
// - Codex: [analytics] enabled = false in config.toml. "When false,
// disables OpenAI analytics across Codex product surfaces"; the default
// statsig metrics exporter follows it (AnalyticsConfigToml and
// OtelConfigToml in codex-rs/config/src/types.rs, rust-v0.167.0).
// feedback.enabled is left alone: it takes /feedback away, which only
// sends when the user asks.
// - Claude Code: env DISABLE_TELEMETRY and DISABLE_ERROR_REPORTING in
// settings.json. 2.1.316 takes any value of DISABLE_TELEMETRY as its
// "no-telemetry" level, from any settings tier, and any value of
// DISABLE_ERROR_REPORTING keeps its error reports in.
// CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC would also stop its updates,
// which is no telemetry.
// - Gemini CLI and Qwen Code: privacy.usageStatisticsEnabled = false in
// settings.json (settingsSchema.ts of each, default true).
//
// None is written unless the user turns it off here. Off writes each key
// the file doesn't have and remembers it in the stash; a key the user has
// already written is theirs: one that already turns telemetry off is left
// as it is, and one that keeps it on is never written over (off is
// refused, naming it). On takes out only what magpie wrote, as magpie
// wrote it. Connecting or disconnecting the agent leaves it alone: it is
// about the agent's vendor, not about magpie.
//
// Only an agent on this computer has it: a WSL distro's file isn't read on
// each look, which would start a stopped distro (#3896).
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"strings"
"github.com/tidwall/jsonc"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/filememo"
)
// TelemetryState is an agent's telemetry as its config has it.
type TelemetryState struct {
// Off: every key of the switch turns telemetry off
Off bool `json:"off"`
// Own: keys the user wrote that keep it off, which on leaves in
Own []string `json:"own,omitempty"`
// Keys: what the switch writes, as the file spells it
Keys []string `json:"keys"`
// File: the config it writes
File string `json:"file"`
// Error: why the config couldn't be read, when it couldn't (the
// state is then unknown)
Error string `json:"error,omitempty"`
}
// Telemetry is an agent's switch for its own telemetry.
type Telemetry struct {
Get func() (TelemetryState, error)
Set func(off bool) error
}
// telemetryKey is one key of a switch: name as the file spells it (a
// dotted path in JSON; "table.key" in TOML), value what magpie writes, off
// whether a value the file has keeps telemetry off.
type telemetryKey struct {
name string
value any
off func(v any) bool
}
// telemetryFile reads and writes one key of the agent's config. get says
// whether the key is there, and fails on a file that doesn't parse (which
// is unknown, never "not there").
type telemetryFile struct {
path string
get func(k telemetryKey) (v any, has bool, err error)
set func(k telemetryKey) error
del func(k telemetryKey) error
}
func isFalse(v any) bool { return v == false }
// anyValue: an environment variable that is set, to anything but empty
func anyValue(v any) bool {
switch x := v.(type) {
case string:
return x != ""
case nil:
return false
}
return true
}
// telemetrySwitch is the switch over keys in f; stashKey is where what
// magpie wrote is kept.
func telemetrySwitch(at place, f telemetryFile, stashKey string, keys ...telemetryKey) *Telemetry {
if at.spell != nil || at.cold {
return nil
}
names := make([]string, len(keys))
for i, k := range keys {
names[i] = k.name
}
wrote := func() map[string]bool {
out := map[string]bool{}
for _, n := range strings.Split(stashShared()[stashKey], ",") {
if n != "" {
out[n] = true
}
}
return out
}
keep := func(m map[string]bool) {
var out []string
for _, n := range names {
if m[n] {
out = append(out, n)
}
}
stash(map[string]string{stashKey: strings.Join(out, ",")})
}
ours := func(k telemetryKey, v any) bool {
a, _ := json.Marshal(v)
b, _ := json.Marshal(k.value)
return string(a) == string(b)
}
get := func() (TelemetryState, error) {
st := TelemetryState{Off: true, Keys: names, File: at.native(f.path)}
mine := wrote()
for _, k := range keys {
v, has, err := f.get(k)
if err != nil {
return TelemetryState{Keys: names, File: st.File}, err
}
if !has || !k.off(v) {
st.Off = false
continue
}
if !(mine[k.name] && ours(k, v)) {
st.Own = append(st.Own, k.name)
}
}
return st, nil
}
set := func(off bool) error {
mine := wrote()
if off {
// every key read first: one the user set on stops it before
// anything is written
var missing []telemetryKey
for _, k := range keys {
v, has, err := f.get(k)
if err != nil {
return err
}
switch {
case !has:
missing = append(missing, k)
case !k.off(v):
b, _ := json.Marshal(v)
return fmt.Errorf("%s sets %s to %s; magpie leaves a value of yours as it is (change it there)", at.native(f.path), k.name, b)
case !ours(k, v):
delete(mine, k.name) // the user's, as it is now
}
}
for _, k := range missing {
if err := f.set(k); err != nil {
keep(mine)
return err
}
mine[k.name] = true
}
keep(mine)
return nil
}
for _, k := range keys {
if !mine[k.name] {
continue
}
v, has, err := f.get(k)
if err != nil {
return err
}
// changed since: the user's now
if has && ours(k, v) {
if err := f.del(k); err != nil {
keep(mine)
return err
}
}
delete(mine, k.name)
}
keep(mine)
return nil
}
// a change that fails part way puts the file back as it was
write := func(off bool) error {
if err := edit.Atomically(func() error { return set(off) }, f.path); err != nil || off {
return err
}
st, err := get()
if err != nil {
return err
}
if st.Off {
return fmt.Errorf("%s turns it off itself (%s); magpie leaves a value of yours as it is (take it out there)", at.native(f.path), strings.Join(st.Own, ", "))
}
return nil
}
return &Telemetry{Get: get, Set: write}
}
// jsonTelemetryFile is a JSON or JSONC config's keys, by dotted path.
func jsonTelemetryFile(path string) telemetryFile {
return telemetryFile{
path: path,
get: func(k telemetryKey) (any, bool, error) {
// read again only once the file changes: the Agents page asks
// on each poll
doc, err := filememo.ReadNow("telemetry json", path, func(raw []byte) (any, error) {
b := jsonc.ToJSON(edit.TrimBOM(raw))
if len(strings.TrimSpace(string(b))) == 0 {
return nil, nil
}
var doc any
err := json.Unmarshal(b, &doc)
return doc, err
})
if errors.Is(err, fs.ErrNotExist) {
return nil, false, nil
}
if err != nil {
return nil, false, fmt.Errorf("%s: %w", path, err)
}
for _, part := range strings.Split(k.name, ".") {
m, ok := doc.(map[string]any)
if !ok {
return nil, false, nil
}
if doc, ok = m[part]; !ok {
return nil, false, nil
}
}
return doc, true, nil
},
set: func(k telemetryKey) error { return edit.SetJSON(path, edit.KV{Path: k.name, Value: k.value}) },
del: func(k telemetryKey) error { return edit.DelJSON(path, k.name) },
}
}
// codexTelemetry is Codex's: [analytics] enabled = false in config.toml.
func codexTelemetry(at place, path string) *Telemetry {
f := telemetryFile{
path: path,
get: func(k telemetryKey) (any, bool, error) {
doc, err := codexTopOf(path)
if err != nil {
return nil, false, fmt.Errorf("%s: %w", path, err)
}
table, key, _ := strings.Cut(k.name, ".")
var m map[string]any
switch table {
case "analytics":
if doc.Analytics == nil {
return nil, false, nil
}
var ok bool
if m, ok = doc.Analytics.(map[string]any); !ok {
return nil, false, fmt.Errorf("%s: analytics is not a table", path)
}
}
v, has := m[key]
return v, has, nil
},
set: func(k telemetryKey) error {
table, key, _ := strings.Cut(k.name, ".")
// written inline or as dotted keys, the table is the user's
// spelling, where a [analytics] of magpie's would define it
// twice
if doc, err := codexTopOf(path); err != nil {
return fmt.Errorf("%s: %w", path, err)
} else if t, err := edit.GetTOMLTable(path, table); err != nil {
return err
} else if t == nil && doc.Analytics != nil {
return fmt.Errorf("%s has %s written in a way magpie doesn't add to; set %s there", at.native(path), table, k.name)
}
return edit.SetTOMLKey(path, table, key, k.value)
},
del: func(k telemetryKey) error {
table, key, _ := strings.Cut(k.name, ".")
return edit.DelTOMLKey(path, table, key)
},
}
return telemetrySwitch(at, f, at.key("codex.telemetry"), telemetryKey{name: "analytics.enabled", value: false, off: isFalse})
}
// claudeTelemetry is Claude Code's: its telemetry and error reports off in
// settings.json's env.
func claudeTelemetry(at place, path string) *Telemetry {
return telemetrySwitch(at, jsonTelemetryFile(path), at.key("claude.telemetry"),
telemetryKey{name: "env.DISABLE_TELEMETRY", value: "1", off: anyValue},
telemetryKey{name: "env.DISABLE_ERROR_REPORTING", value: "1", off: anyValue})
}
// usageStatsTelemetry is Gemini CLI's and Qwen Code's: their usage
// statistics off in settings.json.
func usageStatsTelemetry(at place, path, id string) *Telemetry {
return telemetrySwitch(at, jsonTelemetryFile(path), at.key(id+".telemetry"),
telemetryKey{name: "privacy.usageStatisticsEnabled", value: false, off: isFalse})
}
// ErrNoTelemetry: the agent has no telemetry switch magpie knows.
var ErrNoTelemetry = errors.New("magpie knows no switch for this agent's telemetry")
// SetTelemetry turns a's own telemetry off, or back to what it was.
func (a *Agent) SetTelemetry(off bool) error {
if a.Telemetry == nil {
return ErrNoTelemetry
}
return a.Telemetry.Set(off)
}