Jevik_AI on X deleted magpie's folders by hand. Codex's config.toml kept openai_base_url = "http://127.0.0.1:3425/backend-api/codex", with nothing in the file naming magpie, and every Codex turn failed with a 502. Each line magpie writes that points at the gateway, or at magpie's model list, now gets a comment line of its own just above it: # magpie: Codex on magpie's local gateway; Disconnect in magpie or # `magpie uninstall` undoes it, or delete this line and the model line model_provider = "magpie" - Only a line the write adds or changes, or one that had a note, gets one. A line of the user's own that magpie leaves alone gets none. - The note is never on the value's line. Grok App's providers.rs reads the rest of a line as the value, and an end-of-line comment broke TestGrokApp in a first try. - Every write takes the notes out and puts back the ones still needed, so a note goes with its line and Disconnect leaves none. - No note in JSON. A YAML note that would change what the file reads as isn't added. - Wired in run (agent.MarkWritten); tests leave edit.Mark nil so their exact bytes stay. Run at this commit's tree (worktree, sandbox HOME, private GOCACHE): - gofmt -l clean; go vet darwin, GOOS=windows, GOOS=linux -tags nogui ok. - go test -tags nogui -run Mark ./internal/edit ./internal/agent: ok. - ./internal/agent and ./internal/edit whole with edit.Mark forced on for every test (a snapshot with lineNote set in agent's TestMain): edit ok; agent fails only TestDshRouteKeepsTheAPIPickedInDsh and TestDshSearchThroughGateway, which compare exact bytes and see the note line. With Mark unset (as shipped) they pass. - Real Codex: built magpie from this tree, `magpie codex fakeai/fake-1` in a sandbox HOME. config.toml got the three notes above model_provider, model_catalog_json and openai_base_url; `codex mcp list` and `codex features list` (codex-cli 0.162.1) read it, rc 0, and Python's tomllib reads the same values. `magpie codex default` took them out. - Broken by hand: a note on every line, not only changed ones -> TestMarkTOML and TestMarkOnlyWhatMagpieWrites red. - The notes' line filter is withoutNotes, unexported: an exported Unmarked put a word the routing plugin translates into a Go file, and i18n-keys' "a plugin's words are its own" went red on it. It passes now. Full suite: run with the uninstall commit that follows, recorded there.
95 lines
2.5 KiB
Go
95 lines
2.5 KiB
Go
package edit
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// dotenv files (KEY=value per line) as Gemini CLI reads from ~/.gemini/.env.
|
|
|
|
var envLine = regexp.MustCompile(`^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$`)
|
|
|
|
// GetEnvFile reads one key from a dotenv file.
|
|
func GetEnvFile(path, key string) (string, bool) {
|
|
raw, err := readHeld(path)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
// A key set twice takes its last value: dotenv readers, Gemini CLI's among
|
|
// them, let a later line override an earlier one.
|
|
lines := splitLines(string(raw))
|
|
for i := len(lines) - 1; i >= 0; i-- {
|
|
if m := envLine.FindStringSubmatch(lines[i]); m != nil && m[1] == key {
|
|
if v, ok := unquote(m[2]); ok {
|
|
return v, true
|
|
}
|
|
v := m[2]
|
|
if i := strings.IndexByte(v, '#'); i >= 0 {
|
|
v = strings.TrimSpace(v[:i])
|
|
}
|
|
return v, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// SetEnvFile sets keys in a dotenv file, replacing lines in place and
|
|
// appending new ones. Other lines are kept byte for byte.
|
|
func SetEnvFile(path string, kvs ...KV) error {
|
|
raw, err := Read(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
lines := splitLines(string(raw))
|
|
for _, kv := range kvs {
|
|
v := toString(kv.Value)
|
|
if strings.ContainsAny(v, " #\"'$\r\n") {
|
|
// Gemini CLI's dotenv keeps backslashes literal inside single quotes.
|
|
if !strings.ContainsAny(v, "'\r\n") {
|
|
v = "'" + v + "'"
|
|
} else {
|
|
v = `"` + strings.ReplaceAll(strings.ReplaceAll(v, `\`, `\\`), `"`, `\"`) + `"`
|
|
}
|
|
}
|
|
lines = setLine(lines, kv.Path, kv.Path+"="+v, nil, func(l string) (string, bool) {
|
|
if m := envLine.FindStringSubmatch(l); m != nil {
|
|
return m[1], true
|
|
}
|
|
return "", false
|
|
})
|
|
}
|
|
lines = markEnv(lines, string(raw))
|
|
return WriteAtomic(path, []byte(joinLinesLike(lines, string(raw))))
|
|
}
|
|
|
|
// DelEnvFile removes keys from a dotenv file, and the file when they were
|
|
// all it held (Gemini CLI's .env, made by magpie for its key).
|
|
func DelEnvFile(path string, keys ...string) error {
|
|
raw, err := Read(path)
|
|
if err != nil || len(raw) == 0 {
|
|
return err
|
|
}
|
|
drop := map[string]bool{}
|
|
for _, k := range keys {
|
|
drop[k] = true
|
|
}
|
|
var out []string
|
|
changed := false
|
|
for _, l := range splitLines(string(raw)) {
|
|
if m := envLine.FindStringSubmatch(l); m != nil && drop[m[1]] {
|
|
changed = true
|
|
continue
|
|
}
|
|
out = append(out, l)
|
|
}
|
|
if !changed {
|
|
return nil
|
|
}
|
|
// a note magpie put above a key goes with it
|
|
out = markEnv(out, string(raw))
|
|
if strings.TrimSpace(strings.Join(withoutNotes(out), "")) == "" {
|
|
return Remove(path)
|
|
}
|
|
return WriteAtomic(path, []byte(joinLines(out)))
|
|
}
|