plugins: every built-in plugin starts on, a new install's too

The owner (10-17): "所有的内置插件默认都应该是开启的", "尤其对新用户来说……
不然新用户都不知道有哪些功能". Since 3ccbe6bc1 (10-13) a new install was
written plugins.yml with the Essentials preset, which left Sessions, the
Library, routing groups, warm-ups and Observability off for every new user
(origin/main's binary, fresh sandbox HOME: those five "off").

- pluginrt.Settle() writes All when there is no plugins.yml, new install or
  not; settings.RanBefore, which only told the two apart, is gone.
- plugins.yml gets `settled: 1`. A file below it is brought up once on
  start: an Essentials preset becomes All, the user's own rows kept (a
  plugin they turned off stays off). Every write (Set, SetConfig,
  SetPreset) sets it, so `magpie plugin preset essentials` picked
  afterwards stays the user's. An unreadable or unparsable file is left
  alone, as before.
- Docs (plugin-runtime.md) and comments that said a new install starts
  with Essentials.

Run (origin/main 72ed518bc + this):
- gofmt -l internal: clean; go vet pluginrt, cli, gui, settings: clean.
- go test ./internal/pluginrt/ ./internal/settings/: ok. New
  TestSettleBringsUpAnEssentialsInstall; TestSettleAndSet and
  TestBuiltinSwitch now check a new install has all 14 on.
- Broken by hand (Settle writing Essentials, the bring-up skipped):
  TestSettleAndSet, TestSettleBringsUpAnEssentialsInstall and
  TestBuiltinSwitch go red; restored, green.
- Real binary, sandbox HOME: a new install lists all 14 on and writes
  `preset: all, settled: 1`; a plugins.yml of `preset: essentials` with
  otel off comes up `preset: all`, groups/library/sessions/warmups on,
  otel off.
- go test -count=1 -timeout 45m ./...: every package ok but internal/cli
  (TestPluginLoginTakesTheLapseOff, passes 5/5 alone here and on
  72ed518bc; filed #2748) and internal/gui (TestMainWindowCorners, #1936).
- GUI: builtins.test.cjs 4/4 (chromium + webkit; a first try under load
  ~445 had chromium's page.goto time out at 5s, #2542),
  routing-groups-off.test.cjs pass. Only comments changed in those two.
This commit is contained in:
yetone
2026-10-17 22:54:38 +08:00
parent 1e643ba7e1
commit 5a2414acdd
12 changed files with 113 additions and 109 deletions
+9 -7
View File
@@ -26,7 +26,7 @@ the plugin host) are a different thing; see
| Manifest | `ID`, `API` (a caret range of the host API, `"^1"`; `pluginrt.API` is `"1.0"`), `Requires` and `Provides` (services), `Essential`, `Name`/`Sub` (English; the window translates them), `One`/`Many` (what is in use, of 1 or `{n}`), `InUse`, `Apply`, `Parts` (`{Slot, Key}`: the window's parts it is), `Defaults` (its config), `Settings` (a struct of its settings, made into `Defaults` and a schema at `Register`) and `Client` (its files in the window, under `internal/gui/assets/plugins/<id>/`: scripts, its `i18n.js` and a `.css`). | [`pluginrt.go`](../../internal/pluginrt/pluginrt.go) |
| Lifecycle | `Runtime.Reconcile(on, config)`. Off first: every plugin no longer wanted, and every one requiring a service one of those provided, is disposed newest first. Then on: each wanted plugin whose requirements are provided (by `Core`, `settings`, `providers`, `gateway`, `window`, or an active plugin) is applied, again and again until no more can be. States: `off`, `pending` (waiting for a service, logged), `active`, `failed` (Apply returned an error or panicked, or the API range doesn't match; what it registered before is undone), `disposed`. A failed plugin never stops the others. | [`pluginrt.go`](../../internal/pluginrt/pluginrt.go) |
| Ctx | What `Apply` gets: `OnDispose(f)`, `Contribute(slot, key)`, `Provide(name, v)` (withdrawn when it goes), `Service(name)`, and its composed `Config`. Disposers run in reverse, each one's panic logged and the rest still run. | [`pluginrt.go`](../../internal/pluginrt/pluginrt.go) |
| plugins.yml | `appdir.Config()/plugins.yml`: `preset` (`essentials` or `all`) and `plugins`, rows of `{id, enabled, config}` holding only what the user set. | [`config.go`](../../internal/pluginrt/config.go) |
| plugins.yml | `appdir.Config()/plugins.yml`: `preset` (`essentials` or `all`; `all` unless the user picks Essentials), `settled` (the defaults the file was last brought up to) and `plugins`, rows of `{id, enabled, config}` holding only what the user set. | [`config.go`](../../internal/pluginrt/config.go) |
| Settings schema | `SchemaOf(Settings)` makes a JSON Schema object of the struct, in its fields' order (`x-order`): each exported field with a json name is a setting, of `bool`, an int, `float64` or `string`, with tags `title` (English; the window translates it), `description`, `enum` (comma separated) and `min`/`max`. Its values are the defaults. A field a form can't draw, or a tag that doesn't parse, panics at `Register`. `Schema.Validate` takes a value only as the schema allows it (a JSON float64 or a YAML int for an integer); `SetConfig(id, config)` writes the user's row through it, and `Compose` leaves out a value edited into plugins.yml by hand that it doesn't allow, so the default stands. | [`schema.go`](../../internal/pluginrt/schema.go), [`config.go`](../../internal/pluginrt/config.go) |
| Built-in plugins | `groups`, `library`, `otel`, `sessions`, `warmups` (P0), `gateway` (P1), the Gateway page, which is Essential and in use while a gateway key is there (`access.List`), and `routing` (P2), the Routing page, Essential and in use while the user has a routing group, since the page is where groups are made and changed. `rotation` (P3), the routings besides Smart, is Essential and in use while a provider or group is routed by one of them. `middleware` (P3), the JS gateway middleware, is Essential and in use while plugins.json has a middleware on (`middleware.On`). `redact` (P3), privacy masking, is Essential and in use while Settings → Privacy asks for any masking (`masking.Wanted`). `search` (P3), magpie's web search, is Essential and in use while a search API is set up or a searcher is picked. `budget` (P3), key limits, is Essential and in use while a gateway key has a limit. `usage` (P2), the Usage page, is Essential too, with no `InUse`: what it shows the gateway counts anyway, and the tray panel's Usage tab still shows the allowances with it off. The Gateway page's one setting, `callsPage` (10, 20, 50 or 100; 20), is how many recent calls the page shows at a time. The other five aren't Essential. Each one's `InUse` counts what the user made with it: routing groups not Auto and not hidden, library items given to agents, a collector sent to, gateway conversations recorded, warm-ups and check-ins on. | [`plugins/`](../../plugins) |
@@ -195,7 +195,7 @@ the core. The five other routings are the built-in plugin `rotation`'s.
The user's own routing groups are the `groups` plugin's on this point. The
groups magpie finds (a model two providers serve, `auto-<model>`) stay the
core's, as Smart does: they are how a model several providers serve is one
model with failover, which a new install has with the Essentials preset,
model with failover, which a user has with the Essentials preset,
where `groups` is off. `planGroup` and the walk over a group's members are
the core's either way: a strategy only orders a pool, and a group off is
not offered at all.
@@ -282,10 +282,12 @@ remembering its layer (`Source`):
## Runtime paths
- **Start** (`internal/cli/main.go`, after `settings.Migrate`):
`pluginrt.Settle(!settings.RanBefore())` writes a first plugins.yml when
there is none: `essentials` on a new install, `all` on a magpie that ran
here before (one of `settings.ranBefore`'s files is there; one that can't
be looked at counts as there), so an existing user keeps everything. Then
`pluginrt.Settle()` writes a first plugins.yml, `all`, when there is
none, on a new install too: a new user sees everything magpie does and
turns off what they don't want. A file below `settled: 1` is brought up
once: the `essentials` a new install was written from 10-13 to 10-17
becomes `all`, the user's own rows kept. Every write sets `settled`, so
an Essentials the user picks afterwards stays theirs. Then
`pluginrt.Refresh` makes the process's runtime and reconciles it. An error
is printed and magpie starts.
- **Every read**: `Refresh` loads plugins.yml again, composes it and
@@ -334,7 +336,7 @@ remembering its layer (`Source`):
- `go test ./internal/pluginrt/` (`TestReconcile`, `TestCompatible`,
`TestPluginsImportOnlyCore`, `TestCompose`, `TestSettleAndSet`,
`TestBrokenFileIsKept`), `go test ./internal/settings/ -run TestRanBefore`.
`TestSettleBringsUpAnEssentialsInstall`, `TestBrokenFileIsKept`).
- `go test ./internal/pluginrt/ -run 'TestSchemaOf|TestValidate|TestSetConfig'`.
- `go test ./internal/gui/ -run 'TestBuiltin|TestBootSaysBuiltinsOff'`
(`TestBuiltinConfig`, `TestBuiltinClientsLoaded`).
+2 -4
View File
@@ -245,10 +245,8 @@ func run(args []string) error {
update.AwaitPredecessor()
makeDirs()
settings.Migrate()
// a new install starts with the Essentials preset of built-in plugins
// (plugins.yml), one that ran before with all of them, told by no file
// of a magpie run before: so before anything below writes one
if _, err := pluginrt.Settle(!settings.RanBefore()); err != nil {
// every built-in plugin starts on (plugins.yml), on a new install too
if _, err := pluginrt.Settle(); err != nil {
fmt.Fprintf(os.Stderr, "magpie: plugins.yml: %v\n", err)
}
if _, err := pluginrt.Refresh(); err != nil {
@@ -3493,7 +3493,7 @@ function routingPage(part) {
const gNames = el("div", "rt-gnames");
// the groups go with the routing groups built-in plugin; the pools stay
for (const n of [gHead, gFound, gNames, gList]) n.dataset.feature = "groups";
// and off (a new install's Essentials leaves it off), where they were
// and off (the Essentials preset leaves it off), where they were
// the page says so and turns it on (bigbenniu on X: past the requests
// there was nothing, and no word of where New group had gone). The
// groups magpie finds route all the same.
+2 -2
View File
@@ -17,8 +17,8 @@ import (
// active.
//
// One in use stays on: its switch can't be turned off, and its row says
// what uses it. A new install starts with the Essentials preset; one that
// ran before has them all on until the user says otherwise.
// what uses it. Every install, a new one too, has them all on until the
// user says otherwise.
// TestBuiltinsTranslated asks each string of every plugin of every
// language.
+16 -7
View File
@@ -34,7 +34,16 @@ func postBuiltin(h http.Handler, body string) *httptest.ResponseRecorder {
// one is refused.
func TestBuiltinSwitch(t *testing.T) {
h := builtinHome(t)
if _, err := pluginrt.Settle(true); err != nil {
if _, err := pluginrt.Settle(); err != nil {
t.Fatal(err)
}
// a new install has every one on, so a new user sees all magpie does
for _, b := range builtinsState() {
if !b.On || b.State != pluginrt.Active {
t.Fatalf("a new install: %+v", b)
}
}
if err := pluginrt.SetPreset(pluginrt.PresetEssentials); err != nil {
t.Fatal(err)
}
if rec := postBuiltin(h, `{"id":"library","on":true}`); rec.Code != http.StatusOK {
@@ -102,8 +111,8 @@ func TestBuiltinBrokenFile(t *testing.T) {
}
}
// boot.js says what is off before the page paints: everything for a new
// install, nothing for one that ran before.
// boot.js says what is off before the page paints: nothing for an
// install as it starts, what Essentials leaves off once the user picks it.
func TestBootSaysBuiltinsOff(t *testing.T) {
h := builtinHome(t)
boot := func() string {
@@ -111,11 +120,11 @@ func TestBootSaysBuiltinsOff(t *testing.T) {
h.ServeHTTP(rec, httptest.NewRequest("GET", "/boot.js", nil))
return rec.Body.String()
}
if _, err := pluginrt.Settle(false); err != nil {
if _, err := pluginrt.Settle(); err != nil {
t.Fatal(err)
}
if b := boot(); !strings.Contains(b, `"featuresOff":[]`) {
t.Fatalf("an existing user: %s", b)
t.Fatalf("an install as it starts: %s", b)
}
if err := pluginrt.SetPreset(pluginrt.PresetEssentials); err != nil {
t.Fatal(err)
@@ -124,7 +133,7 @@ func TestBootSaysBuiltinsOff(t *testing.T) {
t.Fatal(err)
}
if b := boot(); !strings.Contains(b, `"featuresOff":["groups","otel","sessions","warmups"]`) {
t.Fatalf("a new install: %s", b)
t.Fatalf("Essentials picked: %s", b)
}
}
@@ -133,7 +142,7 @@ func TestBootSaysBuiltinsOff(t *testing.T) {
// config, not both.
func TestBuiltinConfig(t *testing.T) {
h := builtinHome(t)
if _, err := pluginrt.Settle(false); err != nil {
if _, err := pluginrt.Settle(); err != nil {
t.Fatal(err)
}
gw := func() builtinJSON {
+1 -1
View File
@@ -34,7 +34,7 @@ const PLUGINS = [
{ id: "usage", name: "Usage", sub: "Your allowances, what was spent and on what, each request, and what filled the context window" },
{ id: "warmups", name: "Warm-up and check-in", sub: "Start a subscription's usage window at a time you pick, and claim vendors' daily check-in credits", one: "1 warm-up or check-in on", many: "{n} warm-ups and check-ins on" },
];
// a new install's (the Essentials preset): all off but the Gateway, Routing
// the Essentials preset, picked by the user: all off but the Gateway, Routing
// and Usage pages, the routing strategies, the gateway middleware, privacy
// masking, web search, key limits and plugin tools, which are essential,
// and two routing groups the user made
@@ -1,5 +1,5 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// bigbenniu on X: a new install's Essentials leaves routing groups off, and
// bigbenniu on X: the Essentials preset leaves routing groups off, and
// the Routing page then had nothing past its requests: no New group, no
// groups, and no word of why. With them off, where they were the page says
// so and has a button that turns them on (plugins/builtin {id:"groups"}),
+31 -13
View File
@@ -34,8 +34,10 @@ import (
// say. `magpie plugin dump` shows the result and where each value came
// from.
//
// A new install is written the Essentials preset, a magpie that ran here
// before All (Settle), so an existing user keeps everything they had.
// Every install starts with All (Settle): a new user sees everything
// magpie can do, and turns off what they don't want ("所有的内置插件默认
// 都应该是开启的", the owner, 10-17). Essentials is only ever the user's
// own pick, `magpie plugin preset essentials`.
// Presets a plugins.yml can name.
const (
@@ -45,10 +47,20 @@ const (
// File is plugins.yml as written.
type File struct {
Preset string `yaml:"preset,omitempty"`
Plugins []Row `yaml:"plugins,omitempty"`
Preset string `yaml:"preset,omitempty"`
// Settled is the defaults the file was last settled to (settled):
// below it, Settle brings the file up to them once. Every write the
// user makes sets it, so a preset they pick afterwards is theirs.
Settled int `yaml:"settled,omitempty"`
Plugins []Row `yaml:"plugins,omitempty"`
}
// settled is the defaults plugins.yml is settled to now. 1: All, where
// from 10-13 a new install had been written Essentials, which left
// Sessions, the Library, routing groups, warm-ups and Observability off
// for every new user, who then never knew they were there.
const settled = 1
// Row is the user's own say about one plugin: only what they set.
type Row struct {
ID string `yaml:"id"`
@@ -175,22 +187,27 @@ func merge(under, over any) any {
return out
}
// Settle writes a first plugins.yml when there is none: the Essentials
// preset on a new install, All on a magpie that ran here before. It says
// whether it wrote one. A plugins.yml there, or one that can't be looked
// at, is left as it is.
func Settle(newInstall bool) (bool, error) {
// Settle writes a first plugins.yml, All, when there is none, and brings
// one settled to older defaults up to now's: an Essentials a new install
// was written before settled 1 becomes All, the user's own rows kept
// (what they turned off stays off). It says whether it wrote. A
// plugins.yml that can't be read, or doesn't parse, is left as it is.
func Settle() (bool, error) {
inProcess.Lock()
defer inProcess.Unlock()
fileLock.Lock()
defer fileLock.Unlock()
if _, err := os.Lstat(Path()); !errors.Is(err, os.ErrNotExist) {
if _, err := os.Lstat(Path()); errors.Is(err, os.ErrNotExist) {
return true, write(File{Preset: PresetAll, Settled: settled})
}
f, there, err := Load()
if err != nil || !there || f.Settled >= settled {
return false, nil
}
f := File{Preset: PresetAll}
if newInstall {
f.Preset = PresetEssentials
if f.Preset == PresetEssentials {
f.Preset = PresetAll
}
f.Settled = settled
return true, write(f)
}
@@ -259,6 +276,7 @@ func update(change func(*File)) error {
return err
}
change(&f)
f.Settled = settled
return write(f)
}
+48 -12
View File
@@ -60,20 +60,28 @@ func TestCompose(t *testing.T) {
}
}
// A new install is written Essentials, one that ran before All; the file
// there is never written over by Settle; Set keeps the other rows and the
// preset, and the file it replaces is kept beside it.
// Every install is written All; the file there is never written over by
// Settle once settled; Set keeps the other rows and the preset, and the
// file it replaces is kept beside it.
func TestSettleAndSet(t *testing.T) {
home(t)
if wrote, err := Settle(true); err != nil || !wrote {
if wrote, err := Settle(); err != nil || !wrote {
t.Fatalf("settle: %v %v", wrote, err)
}
if f, there, err := Load(); err != nil || !there || f.Preset != PresetEssentials {
if f, there, err := Load(); err != nil || !there || f.Preset != PresetAll || f.Settled != settled {
t.Fatalf("after settle: %+v %v %v", f, there, err)
}
if wrote, _ := Settle(false); wrote {
for _, c := range Compose(three, File{Preset: PresetAll}) {
if !c.Enabled {
t.Fatalf("a new install: %s off", c.ID)
}
}
if wrote, _ := Settle(); wrote {
t.Fatal("settled over a plugins.yml there")
}
if err := SetPreset(PresetEssentials); err != nil {
t.Fatal(err)
}
if err := Set("extra", true); err != nil {
t.Fatal(err)
}
@@ -90,12 +98,40 @@ func TestSettleAndSet(t *testing.T) {
if _, err := os.Stat(Path() + ".bak"); err != nil {
t.Fatalf("no .bak: %v", err)
}
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if wrote, _ := Settle(false); !wrote {
t.Fatal("not settled")
// Essentials the user picked is theirs: the next start keeps it
if wrote, _ := Settle(); wrote {
t.Fatal("settled over the user's own Essentials")
}
if f, _, _ := Load(); f.Preset != PresetAll {
t.Fatalf("an existing user: %+v", f)
if f, _, _ := Load(); f.Preset != PresetEssentials {
t.Fatalf("the user's Essentials: %+v", f)
}
}
// The Essentials a new install was written before settled 1 becomes
// All on the next start, and what the user turned off stays off.
func TestSettleBringsUpAnEssentialsInstall(t *testing.T) {
home(t)
if err := os.MkdirAll(filepath.Dir(Path()), 0o700); err != nil {
t.Fatal(err)
}
old := "preset: essentials\nplugins:\n - id: extra\n enabled: false\n"
if err := os.WriteFile(Path(), []byte(old), 0o600); err != nil {
t.Fatal(err)
}
if wrote, err := Settle(); err != nil || !wrote {
t.Fatalf("settle: %v %v", wrote, err)
}
f, _, err := Load()
if err != nil || f.Preset != PresetAll || f.Settled != settled {
t.Fatalf("after settle: %+v %v", f, err)
}
for _, c := range Compose(three, f) {
if c.Enabled != (c.ID != "extra") {
t.Fatalf("%s: %+v", c.ID, c)
}
}
if b, _ := os.ReadFile(Path() + ".bak"); string(b) != old {
t.Fatalf("the old file not kept: %q", b)
}
}
@@ -116,7 +152,7 @@ func TestBrokenFileIsKept(t *testing.T) {
if err := Set("x", true); err == nil {
t.Fatal("a broken file written")
}
if wrote, _ := Settle(true); wrote {
if wrote, _ := Settle(); wrote {
t.Fatal("a broken file settled over")
}
if b, _ := os.ReadFile(Path()); string(b) != string(broken) {
+2 -2
View File
@@ -38,8 +38,8 @@ type Manifest struct {
// Requires are services it can't run without; Provides those it gives
// others ("router.strategy/groups")
Requires, Provides []string
// Essential plugins are on in the Essentials preset a new install
// starts with; every built-in is on for a magpie that ran before
// Essential plugins are on in the Essentials preset, when the user
// picks it; every built-in is on otherwise, a new install's too
Essential bool
// Name and Sub say what it is, in English; the window translates them
Name, Sub string
-29
View File
@@ -1,29 +0,0 @@
package settings
import (
"os"
"path/filepath"
)
// ranBefore are files a magpie (or the dial before it, copied over by
// Migrate) leaves in Dir once it has run: its settings, a provider, an
// account, the version it last ran as, the library, a profile, a plugin,
// an agent's stashed config, a request served, the id stats send.
var ranBefore = []string{
"settings.json", "providers.json", "logins.json", "last-version",
"library.json", "profiles.json", "plugins.json", "stash.json",
"usage.jsonl", "migrations.json", "install-id",
}
// RanBefore is whether a magpie has run here before: one of ranBefore's
// files is in Dir. It is asked at start, after Migrate and before anything
// writes into Dir. A file that can't be looked at counts as there:
// unknown is never new.
func RanBefore() bool {
for _, name := range ranBefore {
if _, err := os.Lstat(filepath.Join(Dir(), name)); !os.IsNotExist(err) {
return true
}
}
return false
}
-30
View File
@@ -1,30 +0,0 @@
package settings
import (
"os"
"path/filepath"
"testing"
)
// An empty folder is a new install; any one of ranBefore's files says a
// magpie ran here before.
func TestRanBefore(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if RanBefore() {
t.Fatal("an empty folder ran before")
}
for _, name := range ranBefore {
t.Run(name, func(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := os.MkdirAll(Dir(), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(Dir(), name), []byte("{}"), 0o600); err != nil {
t.Fatal(err)
}
if !RanBefore() {
t.Fatalf("%s there, and not ran before", name)
}
})
}
}