From 399098329b0b2085ac0b2869669ca6b4f5d8bf09 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 6 Oct 2026 00:57:20 +1100 Subject: [PATCH] fix(modelloop): mark reasoning models from the carried model limits, in the gateway and the agent worker --- internal/agentexec/modelloop/runtime.go | 29 +++++++++++-------- internal/app/agentworker/task_execution.go | 10 +++---- internal/app/archiegateway/chat_runtime.go | 2 +- internal/app/archiegateway/gateway_runtime.go | 2 +- internal/infrastructure/modelcatalog/cache.go | 3 -- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/internal/agentexec/modelloop/runtime.go b/internal/agentexec/modelloop/runtime.go index 7b2831fae..b27ec2575 100644 --- a/internal/agentexec/modelloop/runtime.go +++ b/internal/agentexec/modelloop/runtime.go @@ -1,26 +1,26 @@ package modelloop import ( - "os" + "strings" "github.com/samcharles93/ai-sdk/runtime" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/agentrun" ) // NewRuntime builds a provider runtime for interactive chat or a worker-local -// workflow stage. Nil means no providers were configured. catalogPath is the -// models.dev snapshot the service already cached; it supplies each model's -// reasoning flag so requests use the parameters the model accepts. Empty or -// unreadable leaves the runtime without model metadata. -func NewRuntime(providers map[string]agentrun.Provider, catalogPath string) *runtime.Runtime { +// workflow stage. Nil means no providers were configured. limits is keyed +// "provider/model"; its Reasoning flag tells the runtime which models need +// max_completion_tokens instead of max_tokens. +func NewRuntime(providers map[string]agentrun.Provider, limits map[string]config.ModelLimits) *runtime.Runtime { if len(providers) == 0 { return nil } runtime.RegisterBuiltinClasses() catalog := make(map[string]runtime.ProviderConfig, len(providers)) for name, provider := range providers { - cfg := runtime.ProviderConfig{ID: name, Class: provider.Class, BaseURL: provider.BaseURL} + cfg := runtime.ProviderConfig{ID: name, Class: provider.Class, BaseURL: provider.BaseURL, Models: reasoningModels(name, limits)} if provider.APIKeyEnv == "" { cfg.Auth = runtime.AuthConfig{Type: runtime.AuthTypeNone} } else { @@ -28,10 +28,15 @@ func NewRuntime(providers map[string]agentrun.Provider, catalogPath string) *run } catalog[name] = cfg } - cfg := runtime.Config{Providers: catalog} - models := runtime.NewCatalog(runtime.CatalogOptions{}) - if raw, err := os.ReadFile(catalogPath); err == nil && models.LoadFromJSON(raw) == nil { - return runtime.NewRuntimeWithCatalog(cfg, models) + return runtime.NewRuntime(runtime.Config{Providers: catalog}) +} + +func reasoningModels(provider string, limits map[string]config.ModelLimits) []runtime.ModelConfig { + var models []runtime.ModelConfig + for ref, l := range limits { + if id, ok := strings.CutPrefix(ref, provider+"/"); ok && l.Reasoning { + models = append(models, runtime.ModelConfig{ID: id, Reasoning: true}) + } } - return runtime.NewRuntime(cfg) + return models } diff --git a/internal/app/agentworker/task_execution.go b/internal/app/agentworker/task_execution.go index 0a59e3778..b2b782b49 100644 --- a/internal/app/agentworker/task_execution.go +++ b/internal/app/agentworker/task_execution.go @@ -154,10 +154,10 @@ type taskDependencies struct { guardrails *tools.GuardrailEngine } -type runnerFactory func(map[string]agentrun.Provider, *slog.Logger) agentrun.Runner +type runnerFactory func(map[string]agentrun.Provider, map[string]config.ModelLimits, *slog.Logger) agentrun.Runner -func newTaskRunner(providers map[string]agentrun.Provider, log *slog.Logger) agentrun.Runner { - return modelloop.NewLoopRunner(modelloop.NewRuntime(providers, ""), log) +func newTaskRunner(providers map[string]agentrun.Provider, limits map[string]config.ModelLimits, log *slog.Logger) agentrun.Runner { + return modelloop.NewLoopRunner(modelloop.NewRuntime(providers, limits), log) } // applyToolLimits sets the task's tool policy and allowlist on a @@ -186,7 +186,7 @@ func stageRunners(req taskrun.Request, mcpSet *mcpProviderSet, newRunner runnerF if mcpSet != nil && mcpSet.registry != nil { agent = modelloop.NewLoopRunner(llm, log, mcpSet.registry) } else { - agent = newRunner(req.Providers, log) + agent = newRunner(req.Providers, req.Cfg.ModelLimits, log) } if agent == nil { return nil, fmt.Errorf("no agent runner configured for task %d", req.Task.ID) @@ -231,7 +231,7 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen // the task's own model rather than a second provider client. var llm *runtime.Runtime if len(req.MCPServers) > 0 { - llm = modelloop.NewRuntime(req.Providers, "") + llm = modelloop.NewRuntime(req.Providers, req.Cfg.ModelLimits) } mcpSet, mcpErr := startMCPProviders(ctx, req.MCPServers, taskSamplingHandler(llm, req.Cfg), log) if mcpSet != nil { diff --git a/internal/app/archiegateway/chat_runtime.go b/internal/app/archiegateway/chat_runtime.go index 07e1decd2..01e0ee5b2 100644 --- a/internal/app/archiegateway/chat_runtime.go +++ b/internal/app/archiegateway/chat_runtime.go @@ -92,7 +92,7 @@ func (b *server) rebuildChatModelRuntime(cfg config.Config) { if b.chatModels == nil { return // this process serves no chat turns } - b.setLLM(modelloop.NewRuntime(executionProviders(cfg), b.catalog.Path())) + b.setLLM(modelloop.NewRuntime(executionProviders(cfg), cfg.ModelLimits)) b.chatModels.SetConfigured(cfg.Models) catalog, models := b.catalogState() b.chatModels.SetModelCatalog(catalog, models) diff --git a/internal/app/archiegateway/gateway_runtime.go b/internal/app/archiegateway/gateway_runtime.go index b5a3afd86..944530f76 100644 --- a/internal/app/archiegateway/gateway_runtime.go +++ b/internal/app/archiegateway/gateway_runtime.go @@ -27,7 +27,7 @@ func (b *server) setupChatRuntime(ctx context.Context, cfg config.Config, actor // Created before the router so the LLMResponder can be wired in for // non-command message processing. providers := executionProviders(cfg) - b.setLLM(modelloop.NewRuntime(providers, b.catalog.Path())) + b.setLLM(modelloop.NewRuntime(providers, cfg.ModelLimits)) // Transcription is a model-role capability, like the chat models it sits // beside: it is built here, on the model-owning side, from this process's // own [models]/[providers]. The Messaging Service carries a voice note's diff --git a/internal/infrastructure/modelcatalog/cache.go b/internal/infrastructure/modelcatalog/cache.go index d0c0659dc..7b1b2c9de 100644 --- a/internal/infrastructure/modelcatalog/cache.go +++ b/internal/infrastructure/modelcatalog/cache.go @@ -29,9 +29,6 @@ func NewCatalog(cfgPath string) *Catalog { return &Catalog{cachePath: filepath.Join(filepath.Dir(cfgPath), "models.json")} } -// Path is the cached catalog file. -func (c *Catalog) Path() string { return c.cachePath } - // State returns the loaded snapshot and its model references. func (c *Catalog) State() (Snapshot, []string) { c.mu.RLock()