Files
magpie/internal/agent/mark.go
T
yetone 0c4e3455ee agents: a line magpie writes into a TOML, YAML or env config has a note above it saying so
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.
2026-10-29 17:42:35 +08:00

57 lines
2.0 KiB
Go

package agent
import (
"net/url"
"strings"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
// MarkWritten has every TOML, YAML and dotenv line magpie writes that
// points an agent at it carry a comment saying so and how to undo it
// (lineNote). A Codex left with openai_base_url on a gateway that no
// longer ran, after magpie's folders were deleted by hand, failed every
// turn with nothing in its config naming magpie (Jevik_AI on X). Set by
// main; tests of an agent's exact bytes run without it.
func MarkWritten() { edit.Mark = lineNote }
const undo = "Disconnect in magpie or `magpie uninstall` undoes it"
// lineNote is the note a config line gets, "" for one that isn't
// magpie's: a URL on the gateway's port, and Codex's own keys that name
// magpie.
func lineNote(table, key, value string) string {
switch {
case table == "" && key == "model_provider" && value == magpieID:
return "Codex on magpie's local gateway; " + undo + ", or delete this line and the model line"
case table == "" && key == "model_catalog_json" && strings.Contains(value, "magpie"):
return "magpie's model list for Codex; " + undo + ", or delete this line"
case !onGateway(value):
return ""
case table == "model_providers."+magpieID:
return "for Codex threads started on magpie; unused while model_provider doesn't name magpie"
case table == "":
return "points at magpie's local gateway; " + undo + ", or delete this line"
}
return "points at magpie's local gateway; " + undo + ", or remove this entry and pick another model"
}
// onGateway says whether v is a URL on the gateway's port: on loopback, or
// on the address a WSL distro reaches the host by.
func onGateway(v string) bool {
if !strings.HasPrefix(v, "http://") && !strings.HasPrefix(v, "https://") {
return false
}
u, err := url.Parse(v)
if err != nil || u.Port() == "" {
return false
}
for _, base := range []string{gateway.URL(), gateway.SavedURL()} {
if b, err := url.Parse(base); err == nil && b.Port() == u.Port() {
return true
}
}
return false
}