agent: a renamed provider's models follow it in StepCode's enabledModels (#1919, ggbdpq)
Renaming a provider moved StepCode's defaultModel to the new id and
appended it to enabledModels, but the list kept magpie/<old>/ entries
(the model it was on, others the user listed, a glob), which name nothing
after the rename. StepCode now has a RenameRefs hook, as omp has
(e6753190): it renames magpie/<old>/ entries in config.toml's
enabledModels, drops the copy the move just appended, and renames the
scopes kept for Disconnect (OrigScope, LastScope, AppendedScope), so a
reset neither puts the old id back nor leaves the appended one. An entry
that only shares a prefix (magpie/deepseekx/...) and the user's own
entries stay; a list naming none of the provider's models is not
rewritten.
ggbdpq's patch from #1919, taken as written.
Run:
- go test -tags nogui -count=1 -run 'TestStepCode' ./internal/agent: ok.
- With only the RenameRefs hook removed, TestStepCodeEnabledModelsFollowRename
fails with the scope the issue quotes; TestStepCodeRenameLeavesUserScope
is a guard and passes on the old code too.
- -race -count=5 on the two new tests: ok.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: ok.
- The full ./internal/agent package timed out at the default 10m on a
machine at load ~120 (many sessions testing at once); the run with
this and the Pi sibling together is in the next commit's body.
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
@@ -73,7 +73,7 @@ something else shows up as drift.
|
||||
- An agent's config is written in the form the installed version reads, taken from what that version declares, not the latest one. VSCodium has no Copilot Chat of its own, and the Marketplace's last one (0.48.1) has no Custom Endpoint provider, only OpenAI Compatible (`customoai`, #1014). magpie reads the provider list in the package.json of the `github.copilot-chat` installed under `~/.vscode-oss/extensions` (`vscodeChatOf`). It writes its group for `customendpoint` when that is declared, for `customoai` when only that is, with each model's window as `maxInputTokens` + `maxOutputTokens` and the token as `x-api-key` (that version drops an Authorization in `requestHeaders`). A missing or unreadable extension keeps `customendpoint`. Sync rewrites a group written for the other provider, and Check reports it, as it does a Copilot Chat declaring neither. That Copilot Chat registers its providers only while signed in to GitHub with a personal Copilot plan, which the Notice says.
|
||||
- Tuned settings are written only on the user's click, never by Sync or Connect. The value there before is stashed once (`tune.<agent>.<knob>[.<model>]` in `stash.json`), so Undo puts it back, or takes the key out (and a `modelSettings` entry left empty). A value the user changed after the click is theirs: Undo then only forgets the stash. A variable that comes before the setting (`CLAUDE_CODE_AUTO_COMPACT_WINDOW`, `FORCE_PROMPT_CACHING_5M`, `CLAUDE_CODE_PROMPT_CACHE_TTL`, `CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL`, in settings.json's `env` or magpie's own) is shown as `locked` and Apply refuses, since the setting would change nothing. Claude Code compacts at the smaller of the window set and the model's own (its `/autocompact` says "capped to 200k by model"), so advice starts from that, and a model that isn't Claude's has no window here (magpie's own compact wiring serves it). Advice changes a value only when the replay saves 3% or more, and of the windows within 1% of the cheapest takes the largest. Tokens are weighed as billed: cache reads 0.1, 5-minute writes 1.25, hour writes 2, output 5 for Claude; reads 0.1, writes 1, output 8 otherwise.
|
||||
- A plugin's agent writes each key in place (JSON, YAML or env), the user's comments and other keys kept. An object or array `connect` returns is taken as its JSON text (`agentplug.keys`), so it is written in the order the plugin gave it, not sorted as a Go map would be: Kilo lists its effort variants in key order (#1890, `TestPluggedKeepsPluginOrder`). What each key held before the first write is stashed under `plugin:<id>:<path>`, typed, and its own model or Disconnect puts every key back and takes out the ones that weren't there; the file is never written whole. A `connect` that throws or takes over a second writes nothing and says why. Its gateway key is `magpie-<id>` (`agentKeyAt`), and usage names its requests by that key or by its `ua` words (`usage.PluginAgents`). Its `agent.library` (instructions, `mcp` with `mcpFormat`, skills) gives it the Library's three as well, written by `internal/library` as any agent's are ([library](library.md)); a `mcpFormat` outside `agentplug.MCPFormats`, or an `mcp` without one, keeps the plugin from loading.
|
||||
- StepCode config root resolves to the parent of an absolute `STEP_CODING_AGENT_DIR` (agent resources directory differs from config root; relative values ignored), or `STEPCODE_CONFIG_DIR` under home when native, defaulting to `~/.stepcode` ([`stepcode.go`](../../internal/agent/stepcode.go)). Project configs override global configs in StepCode, but magpie manages global configuration only (`config.toml` and `models.json`). Detection is strictly directory-only via `agentDir`, avoiding collision with Smallstep's bare `step` command on `PATH`. Wiring performs an atomic two-file edit (`models.json` provider block and `config.toml` top-level defaults/scope) backed by `stash.json`. Custom fields within `providers.magpie` are preserved during sync, and the original provider block is restored on disconnect. Disconnect restores original defaults, preserves user native changes and custom scope edits, and removes newly created files only when empty.
|
||||
- StepCode config root resolves to the parent of an absolute `STEP_CODING_AGENT_DIR` (agent resources directory differs from config root; relative values ignored), or `STEPCODE_CONFIG_DIR` under home when native, defaulting to `~/.stepcode` ([`stepcode.go`](../../internal/agent/stepcode.go)). Project configs override global configs in StepCode, but magpie manages global configuration only (`config.toml` and `models.json`). Detection is strictly directory-only via `agentDir`, avoiding collision with Smallstep's bare `step` command on `PATH`. Wiring performs an atomic two-file edit (`models.json` provider block and `config.toml` top-level defaults/scope) backed by `stash.json`. Custom fields within `providers.magpie` are preserved during sync, and the original provider block is restored on disconnect. A renamed provider's `magpie/<old>/` entries in `enabledModels` follow it, a glob or a thinking level included, and the scope remembered for disconnect follows too; the user's other entries stay. Disconnect restores original defaults, preserves user native changes and custom scope edits, and removes newly created files only when empty.
|
||||
- An agent is detected by its config file, its folder, or its command on magpie's PATH (`Detected`). That PATH is the login shell's plus the folders users' tools go in (`proc.UserPath`: `~/.local/bin`, npm's prefixes, the Node nvm's default alias resolves to (the highest installed when it names none), bun, Homebrew, ...), so a CLI a terminal doesn't find can still be found and listed: an npm install under a Node the terminal no longer uses. `/api/agents/cli` then gives where it is (`path`) and `offPath` when the login shell's PATH (`proc.LoginPath`) lacks its folder, and the Agents row says "Not on your terminal's PATH", its tip naming the file (悠悠哥 on Discord: Codex 0.162.1 listed with no codex on PATH and no `~/.codex`). An unknown login PATH says nothing. A folder another tool writes too doesn't count (`dirShared`): Pi's for omp, `~/.copilot` for the Copilot CLI, which GitHub Copilot's JetBrains and VS Code plugins also write, and Qoder's and Qoder CN's, which Qoder's JetBrains plugin fills. A config file another tool writes first counts only when `pathOwn` says it is the agent's: Qoder's `settings.json` holding `enabledPlugins` alone is the plugin's (`qoderPluginOnly`; #1495, `TestCopilotFolderFromAnIDEPluginIsNotTheCLI`, `TestQoderSettingsFromAnIDEPluginIsNotTheCLI`).
|
||||
- Tests never touch the real machine. `TestMain` uses `testenv`, which points HOME, USERPROFILE, the XDG folders and APPDATA into a temporary folder, clears `agentenv.Vars` and puts failing stand-ins for keychain tools and agent CLIs on PATH. Agent-specific side effects (Aside, the Zed keychain, Codex's usage check) are also stubbed in `TestMain`.
|
||||
|
||||
|
||||
@@ -255,6 +255,98 @@ func stepcodeStartup(cfg stepcodeTOML) string {
|
||||
return first
|
||||
}
|
||||
|
||||
// stepcodeRenameRef is one enabledModels entry with provider from renamed
|
||||
// to to. A glob or a thinking level after the model stays. An entry that
|
||||
// does not name that provider is the user's and is left as it is.
|
||||
func stepcodeRenameRef(s, from, to string) (string, bool) {
|
||||
rest, ok := strings.CutPrefix(s, magpieID+"/"+from+"/")
|
||||
if !ok {
|
||||
return s, false
|
||||
}
|
||||
return magpieID + "/" + to + "/" + rest, true
|
||||
}
|
||||
|
||||
// stepcodeRenameList renames magpie/<from>/ entries. The model just set is
|
||||
// appended before the old entry is renamed onto the same text, so that
|
||||
// second copy is dropped. A list that names nothing of that provider is
|
||||
// returned unchanged, a repeated entry of the user's included.
|
||||
func stepcodeRenameList(pats []string, from, to string) ([]string, bool) {
|
||||
if len(pats) == 0 {
|
||||
return pats, false
|
||||
}
|
||||
out := make([]string, 0, len(pats))
|
||||
seen := map[string]bool{}
|
||||
fromRename := map[string]bool{}
|
||||
renamed := false
|
||||
for _, p := range pats {
|
||||
n, hit := stepcodeRenameRef(p, from, to)
|
||||
renamed = renamed || hit
|
||||
if seen[n] && (hit || fromRename[n]) {
|
||||
continue
|
||||
}
|
||||
if !seen[n] {
|
||||
seen[n] = true
|
||||
fromRename[n] = hit
|
||||
}
|
||||
out = append(out, n)
|
||||
}
|
||||
if !renamed {
|
||||
return pats, false
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
|
||||
// stepcodeRenameRefs moves enabledModels off a renamed provider, in the
|
||||
// file and in the scope disconnect remembers, so a reset does not put the
|
||||
// old id back and still takes out only what magpie appended.
|
||||
func stepcodeRenameRefs(at place, path, from, to string) (bool, error) {
|
||||
cur, err := readStepcodeTOML(path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
moved := false
|
||||
if cur.Scope != nil {
|
||||
next, ok := stepcodeRenameList(*cur.Scope, from, to)
|
||||
if ok {
|
||||
raw, err := stepcodeScopeRaw(next)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := edit.SetTOMLTopPreserving(path, edit.KV{Path: "enabledModels", Value: raw}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
moved = true
|
||||
}
|
||||
}
|
||||
state, err := loadStepcodeState(at)
|
||||
if err != nil {
|
||||
return moved, err
|
||||
}
|
||||
if state == nil {
|
||||
return moved, nil
|
||||
}
|
||||
changed := false
|
||||
if next, ok := stepcodeRenameList(state.OrigScope, from, to); ok {
|
||||
state.OrigScope = next
|
||||
changed = true
|
||||
}
|
||||
if next, ok := stepcodeRenameList(state.LastScope, from, to); ok {
|
||||
state.LastScope = next
|
||||
changed = true
|
||||
}
|
||||
if next, ok := stepcodeRenameList(state.AppendedScope, from, to); ok {
|
||||
state.AppendedScope = next
|
||||
changed = true
|
||||
}
|
||||
if !changed {
|
||||
return moved, nil
|
||||
}
|
||||
if err := stepcodeStoreState(at, state); err != nil {
|
||||
return moved, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func stepcodeIn(at place) *Agent {
|
||||
dir := stepcodeDir(at)
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
@@ -420,6 +512,11 @@ func stepcodeIn(at place) *Agent {
|
||||
}
|
||||
return syncJSON(modelsPath, "providers."+magpieID, block)
|
||||
},
|
||||
// enabledModels names models beyond the one field, as omp's list
|
||||
// does: a provider renamed has to take those entries with it
|
||||
RenameRefs: func(from, to string) (bool, error) {
|
||||
return stepcodeRenameRefs(at, path, from, to)
|
||||
},
|
||||
Fields: []Field{
|
||||
{
|
||||
Key: "model",
|
||||
|
||||
@@ -1031,6 +1031,94 @@ func TestStepCodeRealCLISandbox(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A provider renamed moves StepCode's magpie/<old>/ entries in enabledModels
|
||||
// onto the new id, once: the model it is on, another of that provider's the
|
||||
// user listed, and a glob. The user's own entries stay, a name that only
|
||||
// shares a prefix stays, and Disconnect still puts the user's scope back
|
||||
// (those entries renamed) and takes out the one magpie appended.
|
||||
func TestStepCodeEnabledModelsFollowRename(t *testing.T) {
|
||||
home, _ := museHome(t)
|
||||
t.Setenv("STEP_CODING_AGENT_DIR", "")
|
||||
t.Setenv("STEPCODE_CONFIG_DIR", "")
|
||||
dir := filepath.Join(home, ".stepcode")
|
||||
configPath := filepath.Join(dir, "config.toml")
|
||||
modelsPath := filepath.Join(dir, "models.json")
|
||||
const before = `# mine
|
||||
theme = "dark"
|
||||
defaultProvider = "anthropic"
|
||||
defaultModel = "claude"
|
||||
enabledModels = ["anthropic/*", "custom/fast", "magpie/deepseek/flash", "magpie/deepseek/*", "magpie/deepseekx/flash"]
|
||||
`
|
||||
stepcodeWriteTestFile(t, configPath, before)
|
||||
stepcodeWriteTestFile(t, modelsPath, `{"providers":{"anthropic":{"name":"Anthropic"}}}`)
|
||||
|
||||
a := stepcode(home)
|
||||
if err := a.Apply("model", "magpie/deepseek/pro"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := RenameProvider("deepseek", "ds"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw := stepcodeReadTestFile(t, configPath)
|
||||
cfg, err := readStepcodeTOML(configPath)
|
||||
if err != nil || cfg.Scope == nil {
|
||||
t.Fatalf("scope: %v\n%s", err, raw)
|
||||
}
|
||||
want := []string{"anthropic/*", "custom/fast", "magpie/ds/flash", "magpie/ds/*", "magpie/deepseekx/flash", "magpie/ds/pro"}
|
||||
if !slices.Equal(*cfg.Scope, want) || strings.Contains(raw, "magpie/deepseek/") ||
|
||||
!strings.Contains(raw, "# mine") || !strings.Contains(raw, `theme = "dark"`) ||
|
||||
stepcodeValue(cfg.Provider) != magpieID || stepcodeValue(cfg.Model) != "ds/pro" {
|
||||
t.Fatalf("renamed scope = %v\nprovider %q model %q\n%s", *cfg.Scope, stepcodeValue(cfg.Provider), stepcodeValue(cfg.Model), raw)
|
||||
}
|
||||
|
||||
if err := a.Disconnect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw = stepcodeReadTestFile(t, configPath)
|
||||
cfg, err = readStepcodeTOML(configPath)
|
||||
if err != nil || cfg.Scope == nil {
|
||||
t.Fatalf("restored scope: %v\n%s", err, raw)
|
||||
}
|
||||
restored := []string{"anthropic/*", "custom/fast", "magpie/ds/flash", "magpie/ds/*", "magpie/deepseekx/flash"}
|
||||
if !slices.Equal(*cfg.Scope, restored) || strings.Contains(raw, "magpie/deepseek/") || strings.Contains(raw, "magpie/ds/pro") ||
|
||||
!strings.Contains(raw, "# mine") || !strings.Contains(raw, `theme = "dark"`) ||
|
||||
stepcodeValue(cfg.Provider) != "anthropic" || stepcodeValue(cfg.Model) != "claude" {
|
||||
t.Fatalf("disconnect scope = %v\nprovider %q model %q\n%s", *cfg.Scope, stepcodeValue(cfg.Provider), stepcodeValue(cfg.Model), raw)
|
||||
}
|
||||
}
|
||||
|
||||
// A rename of a provider StepCode's list does not name leaves that list, and
|
||||
// a config with no list, as they were. Nothing of the user's is rewritten.
|
||||
func TestStepCodeRenameLeavesUserScope(t *testing.T) {
|
||||
home, _ := museHome(t)
|
||||
t.Setenv("STEP_CODING_AGENT_DIR", "")
|
||||
t.Setenv("STEPCODE_CONFIG_DIR", "")
|
||||
dir := filepath.Join(home, ".stepcode")
|
||||
configPath := filepath.Join(dir, "config.toml")
|
||||
const withList = `# mine
|
||||
theme = "dark"
|
||||
defaultProvider = "anthropic"
|
||||
defaultModel = "claude"
|
||||
enabledModels = ["anthropic/*", "custom/fast", "custom/fast"]
|
||||
`
|
||||
stepcodeWriteTestFile(t, configPath, withList)
|
||||
if _, err := RenameProvider("deepseek", "ds"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := stepcodeReadTestFile(t, configPath); got != withList {
|
||||
t.Fatalf("user scope rewritten:\n%s", got)
|
||||
}
|
||||
|
||||
const noList = "# mine\ntheme = \"dark\"\ndefaultProvider = \"anthropic\"\ndefaultModel = \"claude\"\n"
|
||||
stepcodeWriteTestFile(t, configPath, noList)
|
||||
if _, err := RenameProvider("ds", "deepseek"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := stepcodeReadTestFile(t, configPath); got != noList {
|
||||
t.Fatalf("list created:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
// stepcodeListsMagpie is whether step --list-models has a row under the
|
||||
// magpie provider.
|
||||
func stepcodeListsMagpie(out string) bool {
|
||||
|
||||
Reference in New Issue
Block a user