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 on10bef5fe0): - 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 on6a81a3c62(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).
152 lines
4.4 KiB
Go
152 lines
4.4 KiB
Go
package agent
|
||
|
||
// Every agent's telemetry off, from Settings › Privacy (OipsAnthony on X:
|
||
// "支持统一关闭所有 Agent 的遥测功能,这个统一管理的地方优先级最高").
|
||
// settings.AgentTelemetryOff is above each agent's own Telemetry row:
|
||
//
|
||
// - On, every agent here with a switch (Agent.Telemetry) is turned off
|
||
// through that switch, by its rules: only keys the file lacks are
|
||
// written, and a value of the user's own that keeps telemetry on is
|
||
// never written over (the agent stays on, and says why). An agent
|
||
// found later is turned off as it is seen: on each state the window
|
||
// reads, and by the gateway's loop (KeepTelemetryAllOff).
|
||
// - An agent this switch turned off is marked in the stash
|
||
// (Telemetry.all). One that was off already, by its own row or the
|
||
// user's config, isn't: that is its own choice.
|
||
// - Off, each marked agent is turned back on, which takes out only what
|
||
// magpie wrote; the rest stay as their own rows had them.
|
||
// - While it is on, an agent's own row is held (ErrTelemetryAllOff).
|
||
//
|
||
// A settings.json that can't be read is unknown: nothing is written.
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"sync"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/settings"
|
||
)
|
||
|
||
// telemetryAllMu keeps the switch for every agent and an agent's own row
|
||
// from writing one file at once.
|
||
var telemetryAllMu sync.Mutex
|
||
|
||
// ErrTelemetryAllOff: an agent's own row while Settings › Privacy turns
|
||
// every agent's telemetry off.
|
||
var ErrTelemetryAllOff = errors.New("Settings › Privacy turns off every agent's telemetry; turn that off first")
|
||
|
||
// TelemetryAllOff says whether Settings › Privacy's switch is on, read
|
||
// without a copy (each poll asks).
|
||
func TelemetryAllOff() bool {
|
||
s, err := settings.SharedChecked()
|
||
return err == nil && s.AgentTelemetryOff
|
||
}
|
||
|
||
// KeepTelemetryOff turns a's telemetry off when the switch for every agent
|
||
// is on and it isn't off yet, marking it as the switch's. It says why it
|
||
// couldn't (a value of the user's own, a file that doesn't read).
|
||
func KeepTelemetryOff(a *Agent) error {
|
||
if a.Telemetry == nil {
|
||
return nil
|
||
}
|
||
telemetryAllMu.Lock()
|
||
defer telemetryAllMu.Unlock()
|
||
s, err := settings.SharedChecked()
|
||
if err != nil || !s.AgentTelemetryOff {
|
||
return err
|
||
}
|
||
return keepTelemetryOff(a.Telemetry)
|
||
}
|
||
|
||
func keepTelemetryOff(t *Telemetry) error {
|
||
st, err := t.Get()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if st.Off {
|
||
return nil
|
||
}
|
||
if err := t.Set(true); err != nil {
|
||
return err
|
||
}
|
||
stash(map[string]string{t.all: "1"})
|
||
return nil
|
||
}
|
||
|
||
// SetTelemetryAll saves Settings › Privacy's switch and carries it out:
|
||
// on, every agent found here is turned off; off, each the switch turned
|
||
// off is turned back on. The setting is saved whatever an agent says; the
|
||
// error names each agent that couldn't follow, and why.
|
||
func SetTelemetryAll(off bool) error {
|
||
telemetryAllMu.Lock()
|
||
defer telemetryAllMu.Unlock()
|
||
s, err := settings.LoadChecked()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if s.AgentTelemetryOff != off {
|
||
s.AgentTelemetryOff = off
|
||
if err := settings.Save(s); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
var errs []error
|
||
if off {
|
||
for _, a := range Detected() {
|
||
if a.Telemetry == nil {
|
||
continue
|
||
}
|
||
if err := keepTelemetryOff(a.Telemetry); err != nil {
|
||
errs = append(errs, fmt.Errorf("%s: %w", a.Name, err))
|
||
}
|
||
}
|
||
return errors.Join(errs...)
|
||
}
|
||
// every agent, found here or not: one taken away since keeps no key of
|
||
// magpie's behind
|
||
for _, a := range All() {
|
||
t := a.Telemetry
|
||
if t == nil || stashShared()[t.all] == "" {
|
||
continue
|
||
}
|
||
err := t.Set(false)
|
||
if err != nil {
|
||
// on as far as magpie goes: what keeps it off is the user's
|
||
if st, gerr := t.Get(); gerr == nil && !hasOurs(st) {
|
||
err = nil
|
||
}
|
||
}
|
||
if err != nil {
|
||
errs = append(errs, fmt.Errorf("%s: %w", a.Name, err))
|
||
continue
|
||
}
|
||
forget(t.all)
|
||
}
|
||
return errors.Join(errs...)
|
||
}
|
||
|
||
// hasOurs: a state off by keys of magpie's, where Own are the user's
|
||
func hasOurs(st TelemetryState) bool { return st.Off && len(st.Own) < len(st.Keys) }
|
||
|
||
// KeepTelemetryAllOff turns off, while the switch for every agent is on,
|
||
// the telemetry of an agent found since: with no window open to read the
|
||
// state, the gateway's own look.
|
||
func KeepTelemetryAllOff(ctx context.Context) {
|
||
tick := time.NewTicker(time.Minute)
|
||
defer tick.Stop()
|
||
for {
|
||
if TelemetryAllOff() {
|
||
for _, a := range Detected() {
|
||
_ = KeepTelemetryOff(a)
|
||
}
|
||
}
|
||
select {
|
||
case <-ctx.Done():
|
||
return
|
||
case <-tick.C:
|
||
}
|
||
}
|
||
}
|