From b95291f8b0af26ba109053c7825c51f1a4c39c66 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 09:15:29 -0600 Subject: [PATCH] feat(mcpx): model.provider=plugin backend as a broker sampler binding (#1925) --- pkgs/mcpx/internal/daemon/consumer.go | 21 +++++++++--- pkgs/mcpx/internal/model/model.go | 5 +-- pkgs/mcpx/internal/model/model_test.go | 42 ++++++++++++++++++++++- pkgs/mcpx/internal/model/plugin.go | 47 ++++++++++++++++++++++++++ pkgs/mcpx/internal/model/resolve.go | 29 +++++++++++----- pkgs/mcpx/internal/settings/model.go | 7 ++-- 6 files changed, 133 insertions(+), 18 deletions(-) create mode 100644 pkgs/mcpx/internal/model/plugin.go diff --git a/pkgs/mcpx/internal/daemon/consumer.go b/pkgs/mcpx/internal/daemon/consumer.go index 7704d80712..ecb510babb 100644 --- a/pkgs/mcpx/internal/daemon/consumer.go +++ b/pkgs/mcpx/internal/daemon/consumer.go @@ -1180,6 +1180,7 @@ func (s *Server) modelLayer(ctx context.Context, session string) (model.Provider DefaultModel: pol.ModelDefault, Timeout: pol.ModelTimeout, Sampler: s.brokerSampler(session), + PluginSampler: s.pluginSampler(session), }) } @@ -1292,6 +1293,18 @@ func (p modelRepairProvider) Complete(ctx context.Context, req scripting.Complet // unavailable provider, not a hang: the broker's expiry answer lands as // an error here and the caller falls back deterministically. func (s *Server) brokerSampler(session string) model.SampleFunc { + return s.brokerSamplerFor(session, "sampling") +} + +// pluginSampler is the plugin backend's binding over the same broker +// (#1925): identical plumbing, but the question and the answer carry +// the plugin provider's name, so a plugin answerer can tell its +// questions from sampling's and callers see which backend answered. +func (s *Server) pluginSampler(session string) model.SampleFunc { + return s.brokerSamplerFor(session, "plugin") +} + +func (s *Server) brokerSamplerFor(session string, provider string) model.SampleFunc { if s.reg == nil || s.reg.broker == nil { return nil } @@ -1311,7 +1324,7 @@ func (s *Server) brokerSampler(session string) model.SampleFunc { question, err := broker.OpenRequest(elicit.SampleAsk(elicit.Ask{ Server: "mcpx", Session: session, - Message: "mcpx asks for a model completion (model.provider=sampling)", + Message: "mcpx asks for a model completion (model.provider=" + provider + ")", TTL: ttl, }, params)) if err != nil { @@ -1340,13 +1353,13 @@ func (s *Server) brokerSampler(session string) model.SampleFunc { s.reg.publish(events.Event{Kind: events.SampleAnswered, Server: question.Server, Data: mustJSON(elicitAnsweredSummary(question, ans))}) if ans.Action != elicit.Accept { - return model.Response{}, fmt.Errorf("model: sampling %s: %w", ans.Action, model.ErrUnavailable) + return model.Response{}, fmt.Errorf("model: %s %s: %w", provider, ans.Action, model.ErrUnavailable) } text, answered := parseSampleResult(ans.Content) if strings.TrimSpace(text) == "" { - return model.Response{}, fmt.Errorf("model: sampling answered with no text: %w", model.ErrUnavailable) + return model.Response{}, fmt.Errorf("model: %s answered with no text: %w", provider, model.ErrUnavailable) } - return model.Response{Text: text, Model: answered, Provider: "sampling"}, nil + return model.Response{Text: text, Model: answered, Provider: provider}, nil } } diff --git a/pkgs/mcpx/internal/model/model.go b/pkgs/mcpx/internal/model/model.go index 1633e56642..49d2435626 100644 --- a/pkgs/mcpx/internal/model/model.go +++ b/pkgs/mcpx/internal/model/model.go @@ -8,8 +8,9 @@ // Backends in this slice: an OpenAI-compatible configured provider (any // base URL, so OpenAI, LiteLLM, Ollama and LM Studio all work through it) // and sampling through the broker (the pre-existing path, kept as a -// backend rather than replaced). The opencode-plugin backend is a later -// slice; it depends on #47. +// backend rather than replaced), plus the plugin backend: a second +// sampler binding over the same broker, labelled for the plugin +// answerer (#1925). // // The scope guard from #1925 binds every caller: no mcpx operation may // call a model on a path where a deterministic answer exists, and on a diff --git a/pkgs/mcpx/internal/model/model_test.go b/pkgs/mcpx/internal/model/model_test.go index 545a2ef37c..34a16cd865 100644 --- a/pkgs/mcpx/internal/model/model_test.go +++ b/pkgs/mcpx/internal/model/model_test.go @@ -180,9 +180,49 @@ func TestResolveAuto(t *testing.T) { } } +func TestResolvePluginWithInstalledPath(t *testing.T) { + // #1925 (task 13246): with the plugin sampler binding installed, + // model.provider=plugin resolves to the plugin backend and + // generates through that path. + sampler := func(_ context.Context, req Request) (Response, error) { + return Response{Text: "from the plugin answerer", Model: req.Model}, nil + } + p, err := Resolve(Config{Provider: "plugin", PluginSampler: sampler}) + if err != nil { + t.Fatalf("Resolve(plugin with path): %v", err) + } + if p == nil || p.Name() != "plugin" { + t.Fatalf("Resolve(plugin with path) = %v, want the plugin provider", p) + } + resp, err := p.Generate(context.Background(), Request{Model: "some-model"}) + if err != nil { + t.Fatalf("plugin Generate: %v", err) + } + if resp.Text != "from the plugin answerer" || resp.Provider != "plugin" { + t.Errorf("plugin Generate = %+v, want the sampler's text stamped provider=plugin", resp) + } + // The bare provider with no function installed is unavailable, + // never a panic. + if _, err := NewPluginProvider(nil).Generate(context.Background(), Request{}); !errors.Is(err, ErrUnavailable) { + t.Errorf("nil plugin provider: err = %v, want ErrUnavailable", err) + } + // A profile chain can also name the plugin backend. + p, err = Resolve(Config{ + ProfilesJSON: `{"p":{"models":["plugin/answerer"]}}`, + PluginSampler: sampler, + }) + if err != nil { + t.Fatalf("Resolve(profile naming plugin): %v", err) + } + resp, err = p.Generate(context.Background(), Request{Model: "p"}) + if err != nil || resp.Text != "from the plugin answerer" { + t.Errorf("profile plugin Generate = %+v, %v; want the plugin answerer's text", resp, err) + } +} + func TestResolvePluginAndSamplingWithoutPath(t *testing.T) { if _, err := Resolve(Config{Provider: "plugin"}); !errors.Is(err, ErrUnavailable) { - t.Errorf("plugin: err = %v, want ErrUnavailable (backend is a later slice, #47)", err) + t.Errorf("plugin without a plugin sampler: err = %v, want ErrUnavailable (no plugin answerer path installed)", err) } if _, err := Resolve(Config{Provider: "sampling"}); !errors.Is(err, ErrUnavailable) { t.Errorf("sampling without a sampler: err = %v, want ErrUnavailable", err) diff --git a/pkgs/mcpx/internal/model/plugin.go b/pkgs/mcpx/internal/model/plugin.go new file mode 100644 index 0000000000..280e12a52c --- /dev/null +++ b/pkgs/mcpx/internal/model/plugin.go @@ -0,0 +1,47 @@ +package model + +import ( + "context" + "fmt" +) + +// PluginProvider is the opencode-plugin backend: a Provider whose +// generations are answered by the plugin answerer through the same +// elicitation broker the sampling backend uses (#1925). Triage on +// #1925 (task 13246) verified the broker already carries +// daemon-initiated model requests to an answerer -- brokerSampler +// opens an elicit.SampleAsk question, publishes sample.opened before +// awaiting, and maps a non-accept answer to ErrUnavailable -- so the +// plugin backend is a different sampler binding (its questions are +// labelled model.provider=plugin and its answers are stamped with +// this provider's name), not a new transport. The daemon installs +// the binding as Config.PluginSampler; the v2 plugin work it was +// once said to depend on ("#47") was a stale reference -- #47 in +// this repo is an unrelated Dependabot PR, and the plugin-port docs +// it meant landed via #1855. +type PluginProvider struct { + fn SampleFunc +} + +// NewPluginProvider wraps fn, the daemon's plugin sampler binding. A +// nil fn is a provider that is never available, which is the honest +// state of a daemon with no plugin answerer path installed. +func NewPluginProvider(fn SampleFunc) *PluginProvider { + return &PluginProvider{fn: fn} +} + +func (p *PluginProvider) Name() string { return "plugin" } + +func (p *PluginProvider) Generate(ctx context.Context, req Request) (Response, error) { + if p.fn == nil { + return Response{}, fmt.Errorf("model: plugin: %w (no plugin answerer path installed)", ErrUnavailable) + } + resp, err := p.fn(ctx, req) + if err != nil { + return Response{}, err + } + if resp.Provider == "" { + resp.Provider = p.Name() + } + return resp, nil +} diff --git a/pkgs/mcpx/internal/model/resolve.go b/pkgs/mcpx/internal/model/resolve.go index d8b2fdc8da..e227992a58 100644 --- a/pkgs/mcpx/internal/model/resolve.go +++ b/pkgs/mcpx/internal/model/resolve.go @@ -31,6 +31,11 @@ type Config struct { Timeout time.Duration // Sampler is the broker sampling path, when the daemon has one. Sampler SampleFunc + // PluginSampler is the plugin sampler binding over the same + // broker, when the daemon has one: the path model.provider=plugin + // resolves through. Nil means no plugin answerer path is + // installed, and plugin resolves to a precise ErrUnavailable. + PluginSampler SampleFunc // LookupEnv reads the environment; nil means os.Getenv. A seam for // tests, which must not depend on the machine's real variables. LookupEnv func(string) string @@ -74,8 +79,8 @@ var Providers = []string{"auto", "sampling", "plugin", "openai", "anthropic", "g // none (the daemon default), so nothing calls a model and the caller // stays on its deterministic path -- no behaviour change for a user who // configures nothing. An error means a provider was named but cannot be -// built (the plugin backend is not built yet; sampling was asked for -// with no sampling path installed); callers treat it as unavailable and +// built (sampling or plugin was asked for with no answerer path +// installed); callers treat it as unavailable and // fall back the same way. func Resolve(cfg Config) (Provider, error) { lookup := cfg.LookupEnv @@ -106,11 +111,16 @@ func Resolve(cfg Config) (Provider, error) { } return NewSamplingProvider(cfg.Sampler), nil case "plugin": - // The opencode-plugin backend answers through the v2 plugin's - // ctx.generate.text and depends on #47; it is a later slice of - // #1925, named in the enum now so configurations written for it - // fail with this sentence rather than a bare "unknown provider". - return nil, fmt.Errorf("model: provider plugin: %w (the opencode-plugin backend is not built yet; it depends on #47)", ErrUnavailable) + // The plugin backend is a sampler binding over the elicitation + // broker (#1925, task 13246): the daemon installs it as + // PluginSampler when it can carry questions to a plugin + // answerer. Without that path the honest answer is a precise + // unavailable, not the stale "#47" dependency this case used + // to cite -- #47 is an unrelated closed Dependabot PR. + if cfg.PluginSampler == nil { + return nil, fmt.Errorf("model: provider plugin: %w (no plugin answerer path installed)", ErrUnavailable) + } + return NewPluginProvider(cfg.PluginSampler), nil case "openai", "anthropic", "google": name := strings.ToLower(strings.TrimSpace(cfg.Provider)) return configured(cfg, name, lookup), nil @@ -238,7 +248,10 @@ func (r *profileRouter) provider(name string) (Provider, error) { return NewSamplingProvider(r.cfg.Sampler), nil } if name == "plugin" { - return nil, fmt.Errorf("model: provider plugin: %w", ErrUnavailable) + if r.cfg.PluginSampler == nil { + return nil, fmt.Errorf("model: provider plugin: %w (no plugin answerer path installed)", ErrUnavailable) + } + return NewPluginProvider(r.cfg.PluginSampler), nil } var settings providerSettings configured := false diff --git a/pkgs/mcpx/internal/settings/model.go b/pkgs/mcpx/internal/settings/model.go index 5c369f440f..c444327ebf 100644 --- a/pkgs/mcpx/internal/settings/model.go +++ b/pkgs/mcpx/internal/settings/model.go @@ -34,9 +34,10 @@ func modelSettings() []Setting { "declare it). openai, anthropic and google are configured " + "providers reached through one OpenAI-compatible client, so " + "model.baseURL can point them at a gateway, LiteLLM, Ollama or " + - "LM Studio instead. plugin is the opencode-plugin backend, a " + - "later slice that depends on #47; naming it resolves to " + - "unavailable until it lands. auto prefers a live sampling " + + "LM Studio instead. plugin is the plugin backend: a sampler " + + "binding over the same broker, labelled for the plugin " + + "answerer; naming it resolves to unavailable when the daemon " + + "has no plugin answerer path installed. auto prefers a live sampling " + "path, then a configured provider, then fails cleanly.", }, {