fix(gateway): isolate model selections by org
This commit is contained in:
9 files changed
+123
-119
No files matched your search
@@ -19,7 +19,7 @@ import (
|
||||
type chatModelManager struct {
|
||||
mu sync.RWMutex
|
||||
aliases map[string]string
|
||||
active string
|
||||
active map[org.OrgID]string
|
||||
details map[string]gateway.ModelDetails
|
||||
providerNames map[string]string
|
||||
// orgAliases answers the aliases the principal ctx carries may use. Nil
|
||||
@@ -28,7 +28,7 @@ type chatModelManager struct {
|
||||
}
|
||||
|
||||
func newChatModelManager(aliases map[string]string) *chatModelManager {
|
||||
m := &chatModelManager{details: make(map[string]gateway.ModelDetails), providerNames: make(map[string]string)}
|
||||
m := &chatModelManager{active: make(map[org.OrgID]string), details: make(map[string]gateway.ModelDetails), providerNames: make(map[string]string)}
|
||||
m.SetConfigured(aliases)
|
||||
return m
|
||||
}
|
||||
@@ -67,8 +67,9 @@ func (m *chatModelManager) SetModelCatalog(snapshot modelcatalog.Snapshot) {
|
||||
|
||||
// keepActiveLocked retains an alias or an available model across live updates.
|
||||
func (m *chatModelManager) keepActiveLocked() {
|
||||
if !slices.Contains(config.ChatAliases(m.aliases), m.active) && !slices.Contains(chatModelRefs(m.aliases, m.details), m.active) {
|
||||
m.active = config.PurposeChat.DefaultAlias()
|
||||
selection := m.selectionLocked(org.DefaultOrgID)
|
||||
if !slices.Contains(config.ChatAliases(m.aliases), selection) && !slices.Contains(chatModelRefs(m.aliases, m.details), selection) {
|
||||
m.active[org.DefaultOrgID] = config.PurposeChat.DefaultAlias()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,31 +86,21 @@ func (m *chatModelManager) ModelDetails(ref string) (gateway.ModelDetails, bool)
|
||||
return details, ok
|
||||
}
|
||||
|
||||
// Models returns the configured and catalog model references chat may select.
|
||||
func (m *chatModelManager) Models() []string {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return chatModelRefs(m.aliases, m.details)
|
||||
func (m *chatModelManager) selectionLocked(id org.OrgID) string {
|
||||
if selection := m.active[id]; selection != "" {
|
||||
return selection
|
||||
}
|
||||
return config.PurposeChat.DefaultAlias()
|
||||
}
|
||||
|
||||
func (m *chatModelManager) ActiveAlias() string {
|
||||
func (m *chatModelManager) ActiveAlias(ctx context.Context) string {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if strings.Contains(m.active, "/") {
|
||||
selection := m.selectionLocked(org.OrgFromContext(ctx))
|
||||
if strings.Contains(selection, "/") {
|
||||
return ""
|
||||
}
|
||||
return m.active
|
||||
}
|
||||
|
||||
// ActiveModel returns the active alias's model ref, or "" when it is unset.
|
||||
func (m *chatModelManager) ActiveModel() string {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if strings.Contains(m.active, "/") {
|
||||
return m.active
|
||||
}
|
||||
ref, _ := config.ResolveModel(m.aliases, m.active, config.PurposeChat)
|
||||
return ref
|
||||
return selection
|
||||
}
|
||||
|
||||
func (m *chatModelManager) SetActiveModel(ctx context.Context, selection string) error {
|
||||
@@ -120,12 +111,16 @@ func (m *chatModelManager) SetActiveModel(ctx context.Context, selection string)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !slices.Contains(config.ChatAliases(aliases), selection) && !slices.Contains(m.modelRefsFor(ctx, aliases), selection) {
|
||||
return fmt.Errorf("model or alias %q is not available", selection)
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.active = selection
|
||||
id := org.OrgFromContext(ctx)
|
||||
if id == org.DefaultOrgID || m.orgAliases == nil {
|
||||
aliases = m.aliases
|
||||
}
|
||||
if !slices.Contains(config.ChatAliases(aliases), selection) && !slices.Contains(m.refsLocked(id, aliases), selection) {
|
||||
return fmt.Errorf("model or alias %q is not available", selection)
|
||||
}
|
||||
m.active[id] = selection
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -146,7 +141,13 @@ func (m *chatModelManager) ModelsFor(ctx context.Context) ([]string, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m.modelRefsFor(ctx, aliases), nil
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
id := org.OrgFromContext(ctx)
|
||||
if id == org.DefaultOrgID || m.orgAliases == nil {
|
||||
aliases = m.aliases
|
||||
}
|
||||
return m.refsLocked(id, aliases), nil
|
||||
}
|
||||
|
||||
// ModelFor resolves the current selection in the caller's org. A direct
|
||||
@@ -157,10 +158,14 @@ func (m *chatModelManager) ModelFor(ctx context.Context) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
m.mu.RLock()
|
||||
selection := m.active
|
||||
m.mu.RUnlock()
|
||||
defer m.mu.RUnlock()
|
||||
id := org.OrgFromContext(ctx)
|
||||
if id == org.DefaultOrgID || m.orgAliases == nil {
|
||||
aliases = m.aliases
|
||||
}
|
||||
selection := m.selectionLocked(id)
|
||||
if strings.Contains(selection, "/") {
|
||||
if !slices.Contains(m.modelRefsFor(ctx, aliases), selection) {
|
||||
if !slices.Contains(m.refsLocked(id, aliases), selection) {
|
||||
return "", fmt.Errorf("model %q is not available in this org", selection)
|
||||
}
|
||||
return selection, nil
|
||||
@@ -168,14 +173,12 @@ func (m *chatModelManager) ModelFor(ctx context.Context) (string, error) {
|
||||
return config.ResolveModel(aliases, selection, config.PurposeChat)
|
||||
}
|
||||
|
||||
// modelRefsFor keeps tenant choices within their assigned aliases; the
|
||||
// system org can also choose from its credential-filtered provider catalog.
|
||||
func (m *chatModelManager) modelRefsFor(ctx context.Context, aliases map[string]string) []string {
|
||||
if org.OrgFromContext(ctx) != org.DefaultOrgID {
|
||||
// refsLocked keeps tenant choices within their assigned aliases; the system
|
||||
// org can also select from its credential-filtered provider catalog.
|
||||
func (m *chatModelManager) refsLocked(id org.OrgID, aliases map[string]string) []string {
|
||||
if id != org.DefaultOrgID {
|
||||
return chatModelRefs(aliases, nil)
|
||||
}
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return chatModelRefs(aliases, m.details)
|
||||
}
|
||||
|
||||
|
||||
@@ -77,25 +77,43 @@ func TestChatModelSelection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatModelPickerAfterOrgChange(t *testing.T) {
|
||||
manager := newChatModelManager(map[string]string{"default": "openai/old"})
|
||||
manager.SetModelCatalog(modelcatalog.Snapshot{Providers: []modelcatalog.Provider{{ID: "openai", Models: []modelcatalog.Model{{ID: "new"}}}}})
|
||||
manager.orgAliases = func(context.Context) (map[string]string, error) {
|
||||
return map[string]string{"default": "anthropic/sonnet"}, nil
|
||||
}
|
||||
if err := manager.SetActiveModel(t.Context(), "openai/new"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := org.WithOrg(t.Context(), "tenant")
|
||||
if _, err := manager.ModelFor(ctx); err == nil {
|
||||
t.Fatal("a tenant could execute the system org's model")
|
||||
}
|
||||
adapter := &gateway.LocalChatAdapter{Router: gateway.NewRouter(nil, nil, "telegram"), Sessions: modelSelectionSessions{}, Models: manager}
|
||||
snapshot, err := adapter.Snapshot(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if snapshot.ActiveModel != "" || !slices.Contains(snapshot.ModelsByProvider["anthropic"], "anthropic/sonnet") {
|
||||
t.Fatalf("picker cannot recover from revoked model: %+v", snapshot)
|
||||
func TestChatModelOrgIsolation(t *testing.T) {
|
||||
for _, selection := range []string{"fast", "tenant/fast"} {
|
||||
t.Run(selection, func(t *testing.T) {
|
||||
m := newChatModelManager(map[string]string{"default": "openai/main"})
|
||||
tenantAliases := map[string]string{"default": "tenant/main", "fast": "tenant/fast"}
|
||||
m.orgAliases = func(ctx context.Context) (map[string]string, error) {
|
||||
if org.OrgFromContext(ctx) == "tenant" {
|
||||
return tenantAliases, nil
|
||||
}
|
||||
return map[string]string{"default": "other/main"}, nil
|
||||
}
|
||||
tenant := org.WithOrg(t.Context(), "tenant")
|
||||
if err := m.SetActiveModel(tenant, selection); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
check := func(systemModel string) {
|
||||
t.Helper()
|
||||
for id, want := range map[org.OrgID]string{org.DefaultOrgID: systemModel, "tenant": "tenant/fast", "other": "other/main"} {
|
||||
got, err := m.ModelFor(org.WithOrg(t.Context(), id))
|
||||
if err != nil || got != want {
|
||||
t.Errorf("org %s model = %q, %v; want %q", id, got, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
check("openai/main")
|
||||
m.SetConfigured(map[string]string{"default": "openai/updated"})
|
||||
m.SetModelCatalog(modelcatalog.Snapshot{})
|
||||
check("openai/updated")
|
||||
delete(tenantAliases, "fast")
|
||||
if _, err := m.ModelFor(tenant); err == nil {
|
||||
t.Fatal("revoked tenant choice still executes")
|
||||
}
|
||||
adapter := &gateway.LocalChatAdapter{Router: gateway.NewRouter(nil, nil, "telegram"), Sessions: modelSelectionSessions{}, Models: m}
|
||||
snapshot, err := adapter.Snapshot(tenant)
|
||||
if err != nil || snapshot.ActiveModel != "" || !slices.Contains(snapshot.ModelsByProvider["tenant"], "tenant/main") {
|
||||
t.Fatalf("picker cannot recover: %+v, %v", snapshot, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -367,7 +367,11 @@ func (g *chatTitleGenerator) GenerateTitle(ctx context.Context, sessionID, first
|
||||
if llm == nil {
|
||||
return "", fmt.Errorf("no model runtime is configured")
|
||||
}
|
||||
text, err := sendChatTurn(ctx, llm, g.chatModels.ActiveModel(),
|
||||
model, err := g.chatModels.ModelFor(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
text, err := sendChatTurn(ctx, llm, model,
|
||||
core.GenerateOptions{Messages: messages, MaxSteps: 1}, nil, nil, g.usage, nil)
|
||||
if err != nil {
|
||||
if g.log != nil {
|
||||
|
||||
@@ -24,6 +24,11 @@ import (
|
||||
|
||||
func (b *server) setupCurators(ctx context.Context) {
|
||||
log := b.log.With("component", "curator")
|
||||
model, err := b.chatModels.ModelFor(ctx)
|
||||
if err != nil {
|
||||
log.Warn("curators disabled: chat model unavailable", "err", err)
|
||||
return
|
||||
}
|
||||
skillsRoot := b.cfg.SkillsDir
|
||||
if skillsRoot == "" {
|
||||
skillsRoot = b.cfg.WorkDir
|
||||
@@ -36,7 +41,7 @@ func (b *server) setupCurators(ctx context.Context) {
|
||||
Skills: skillcurator.NewStore(skillsRoot),
|
||||
Conversations: sessioncurator.NewAdapter(b.chatSessionStore, b.cfg.BotUser),
|
||||
LLM: curatorLLMRunner{llm: b.chatLLM, outcomes: b.providerOutcomes, usage: b.usage},
|
||||
Model: b.chatModels.ActiveModel(),
|
||||
Model: model,
|
||||
})
|
||||
|
||||
if err := b.curatorRegistry.Register(skillcurator.New(skillcurator.DefaultInterval)); err != nil {
|
||||
|
||||
@@ -27,9 +27,9 @@ func (b *server) mcpSamplingHandler() mcp.SamplingHandler {
|
||||
if b.chatModels == nil {
|
||||
return mcp.SamplingResult{}, errors.New("mcp sampling: no chat model is configured")
|
||||
}
|
||||
model := b.chatModels.ActiveModel()
|
||||
if model == "" {
|
||||
return mcp.SamplingResult{}, errors.New("mcp sampling: no chat model is configured")
|
||||
model, err := b.chatModels.ModelFor(ctx)
|
||||
if err != nil {
|
||||
return mcp.SamplingResult{}, fmt.Errorf("mcp sampling: %w", err)
|
||||
}
|
||||
messages, err := modelloop.SamplingMessages(req)
|
||||
if err != nil {
|
||||
|
||||
@@ -28,8 +28,10 @@ func (u *usageSink) record(ctx context.Context, source usage.Source, ref string,
|
||||
return
|
||||
}
|
||||
alias := ""
|
||||
if u.models != nil && u.models.ActiveModel() == ref {
|
||||
alias = u.models.ActiveAlias()
|
||||
if u.models != nil {
|
||||
if model, err := u.models.ModelFor(ctx); err == nil && model == ref {
|
||||
alias = u.models.ActiveAlias(ctx)
|
||||
}
|
||||
}
|
||||
record := usage.Record{
|
||||
Source: source, Alias: alias,
|
||||
|
||||
@@ -46,17 +46,12 @@ func (a *LocalChatAdapter) Snapshot(ctx context.Context) (ChatSnapshot, error) {
|
||||
}
|
||||
s := ChatSnapshot{Sessions: sessions, Models: []string{}, Providers: []string{}, Personas: []string{}, ModelsByProvider: map[string][]string{}, ActivePersonas: map[string]string{}, CancellationAvailable: a.Turns != nil, PersonasAvailable: a.Personas != nil, VoiceAvailable: a.VoiceAvailable, Version: a.Router.Version}
|
||||
if a.Models != nil {
|
||||
s.Models = slices.Clone(a.Models.Models())
|
||||
if manager, ok := a.Models.(OrgModelManager); ok {
|
||||
if s.Models, err = manager.ModelsFor(ctx); err != nil {
|
||||
return ChatSnapshot{}, err
|
||||
}
|
||||
}
|
||||
s.ActiveModel = a.Models.ActiveModel()
|
||||
if manager, ok := a.Models.(OrgModelManager); ok {
|
||||
// Keep the picker available when a previous selection was revoked.
|
||||
s.ActiveModel, _ = manager.ModelFor(ctx)
|
||||
s.Models, err = a.Models.ModelsFor(ctx)
|
||||
if err != nil {
|
||||
return ChatSnapshot{}, err
|
||||
}
|
||||
// Keep the picker available when a previous selection was revoked.
|
||||
s.ActiveModel, _ = a.Models.ModelFor(ctx)
|
||||
|
||||
}
|
||||
s.ActiveProvider, _, _ = strings.Cut(s.ActiveModel, "/")
|
||||
|
||||
+19
-39
@@ -55,22 +55,10 @@ type LLMStreamResponder func(ctx context.Context, in Inbound, stream TurnStream)
|
||||
// runtime provider catalog. When nil on a Router, /model returns a
|
||||
// "not configured" message.
|
||||
type ModelManager interface {
|
||||
// Models returns the model references chat may switch between.
|
||||
Models() []string
|
||||
// ActiveAlias returns the selected alias, or empty for a direct model choice.
|
||||
ActiveAlias() string
|
||||
// ActiveModel returns the selected "provider/model" reference.
|
||||
ActiveModel() string
|
||||
// SetActiveModel selects an available model reference or a named alias.
|
||||
SetActiveModel(ctx context.Context, alias string) error
|
||||
}
|
||||
|
||||
// OrgModelManager is the optional org-aware extension: the choices and the
|
||||
// active model for the org of the principal ctx carries.
|
||||
type OrgModelManager interface {
|
||||
ModelManager
|
||||
ModelsFor(ctx context.Context) ([]string, error)
|
||||
ActiveAlias(ctx context.Context) string
|
||||
ModelFor(ctx context.Context) (string, error)
|
||||
SetActiveModel(ctx context.Context, selection string) error
|
||||
}
|
||||
|
||||
// ModelDetails is the catalog metadata rendered after an interactive model
|
||||
@@ -349,7 +337,7 @@ func (r *Router) dispatchLocal(ctx context.Context, msg messaging.Message, platf
|
||||
reply, err := r.handleStart()
|
||||
return reply, true, err
|
||||
case "/whoami":
|
||||
reply, err := r.handleWhoami()
|
||||
reply, err := r.handleWhoami(ctx)
|
||||
return reply, true, err
|
||||
case "/profile":
|
||||
reply, err := r.handleProfile(ctx)
|
||||
@@ -487,7 +475,7 @@ func (r *Router) handleModel(ctx context.Context, arg string) (string, error) {
|
||||
return "Model switching is not configured.", nil
|
||||
}
|
||||
if arg == "" {
|
||||
models, err := r.chatModels(ctx)
|
||||
models, err := r.Models.ModelsFor(ctx)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Cannot list models: %v", err), nil
|
||||
}
|
||||
@@ -496,7 +484,7 @@ func (r *Router) handleModel(ctx context.Context, arg string) (string, error) {
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("Usage: /model <alias|provider/model>\nAvailable models:\n")
|
||||
active := r.Models.ActiveModel()
|
||||
active, _ := r.Models.ModelFor(ctx)
|
||||
for _, m := range models {
|
||||
if m == active {
|
||||
fmt.Fprintf(&b, " %s (active)\n", m)
|
||||
@@ -512,14 +500,6 @@ func (r *Router) handleModel(ctx context.Context, arg string) (string, error) {
|
||||
return fmt.Sprintf("Active model set to %s.", arg), nil
|
||||
}
|
||||
|
||||
// chatModels lists the model references the caller's org may switch between.
|
||||
func (r *Router) chatModels(ctx context.Context) ([]string, error) {
|
||||
if manager, ok := r.Models.(OrgModelManager); ok {
|
||||
return manager.ModelsFor(ctx)
|
||||
}
|
||||
return r.Models.Models(), nil
|
||||
}
|
||||
|
||||
// restAfter returns the text after the command token, stripping the
|
||||
// optional @gateway mention. E.g. for "/spawn Fix the bug", returns
|
||||
// "Fix the bug".
|
||||
@@ -637,7 +617,7 @@ func (r *Router) handleStatus(ctx context.Context) (string, error) {
|
||||
if r.Health != nil {
|
||||
report = r.Health.Health()
|
||||
}
|
||||
return formatStatus(counts, report, r.Models, time.Now()), nil
|
||||
return formatStatus(ctx, counts, report, r.Models, time.Now()), nil
|
||||
}
|
||||
|
||||
// handleTasks answers /tasks: the live view of this identity's work, with
|
||||
@@ -813,11 +793,15 @@ func formatProviderName(provider string) string {
|
||||
return trimmed
|
||||
}
|
||||
|
||||
func extractRuntimeInfo(models ModelManager) (provider, model string) {
|
||||
func extractRuntimeInfo(ctx context.Context, models ModelManager) (provider, model string) {
|
||||
if models == nil {
|
||||
return "", ""
|
||||
}
|
||||
model = strings.TrimSpace(models.ActiveModel())
|
||||
model, err := models.ModelFor(ctx)
|
||||
if err != nil {
|
||||
return "", "unavailable: " + err.Error()
|
||||
}
|
||||
model = strings.TrimSpace(model)
|
||||
if model != "" {
|
||||
if p, _, ok := strings.Cut(model, "/"); ok {
|
||||
provider = p
|
||||
@@ -851,7 +835,7 @@ func formatRuntimeSection(b *strings.Builder, provider, model string) {
|
||||
|
||||
// formatStatus renders the queue summary, the health sections the process
|
||||
// can report, and the runtime block.
|
||||
func formatStatus(counts map[string]int, report HealthReport, models ModelManager, now time.Time) string {
|
||||
func formatStatus(ctx context.Context, counts map[string]int, report HealthReport, models ModelManager, now time.Time) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("📊 Archie status\n\n")
|
||||
b.WriteString(formatQueueDepth(counts))
|
||||
@@ -861,7 +845,7 @@ func formatStatus(counts map[string]int, report HealthReport, models ModelManage
|
||||
b.WriteString(reportLineBreak)
|
||||
}
|
||||
|
||||
provider, model := extractRuntimeInfo(models)
|
||||
provider, model := extractRuntimeInfo(ctx, models)
|
||||
formatRuntimeSection(&b, provider, model)
|
||||
|
||||
return b.String()
|
||||
@@ -892,14 +876,14 @@ func formatQueueDepth(counts map[string]int) string {
|
||||
return fmt.Sprintf("Queue: %d in flight (%s)", inFlight, strings.Join(parts, ", "))
|
||||
}
|
||||
|
||||
func (r *Router) handleWhoami() (string, error) {
|
||||
func (r *Router) handleWhoami(ctx context.Context) (string, error) {
|
||||
if r.Identity == "" {
|
||||
return "I'm Archie, running without a configured identity.", nil
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "I'm %s.", r.Identity)
|
||||
if r.Models != nil {
|
||||
if active := r.Models.ActiveModel(); active != "" {
|
||||
if active, _ := r.Models.ModelFor(ctx); active != "" {
|
||||
fmt.Fprintf(&b, " Running %s.", active)
|
||||
}
|
||||
}
|
||||
@@ -914,13 +898,9 @@ func (r *Router) handleProfile(ctx context.Context) (string, error) {
|
||||
}
|
||||
fmt.Fprintf(&b, "Identity: %s\n", identity)
|
||||
if r.Models != nil {
|
||||
model := r.Models.ActiveModel()
|
||||
if manager, ok := r.Models.(OrgModelManager); ok {
|
||||
var err error
|
||||
model, err = manager.ModelFor(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
model, err := r.Models.ModelFor(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
fmt.Fprintf(&b, "Model: %s\n", model)
|
||||
}
|
||||
|
||||
@@ -309,12 +309,9 @@ func (r *TurnRunner) prepareTurn(ctx context.Context, sessionID string, in Inbou
|
||||
// The question tool is per-turn: it exists only when this turn's channel
|
||||
// adapter can carry a clarify/picker interaction (see WithInteractive).
|
||||
extraTools = append(extraTools, InteractiveTools(ctx)...)
|
||||
modelName := r.Models.ActiveModel()
|
||||
if manager, ok := r.Models.(OrgModelManager); ok {
|
||||
var err error
|
||||
if modelName, err = manager.ModelFor(ctx); err != nil {
|
||||
return preparedTurn{}, err
|
||||
}
|
||||
modelName, err := r.Models.ModelFor(ctx)
|
||||
if err != nil {
|
||||
return preparedTurn{}, err
|
||||
}
|
||||
modelDetails := ModelDetails{}
|
||||
if detailed, ok := r.Models.(DetailedModelManager); ok {
|
||||
|
||||
Reference in new issue
Block a user