fix(accountsvc): guard nil agent registry on startup

This commit is contained in:
Haitao Pan 2026-03-13 11:18:51 +08:00
parent ea3d6aec06
commit d59a8a0415
3 changed files with 34 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import (
"math/big"
"net/http"
"net/url"
"reflect"
"strings"
"sync"
"time"
@ -220,10 +221,26 @@ func WithOAuthFrontendURL(url string) Option {
// WithAgentRegistry configures the handler with the provided agent registry.
func WithAgentRegistry(registry agentRegistry) Option {
return func(h *handler) {
if isNilAgentRegistry(registry) {
return
}
h.agentRegistry = registry
}
}
func isNilAgentRegistry(registry agentRegistry) bool {
if registry == nil {
return true
}
value := reflect.ValueOf(registry)
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
return value.IsNil()
default:
return false
}
}
// WithGormDB configures the handler with the provided GORM database for admin settings.
func WithGormDB(db *gorm.DB) Option {
return func(h *handler) {

View File

@ -113,6 +113,17 @@ func extractVerificationCodeFromMessage(t *testing.T, msg capturedEmail) string
return ""
}
func TestWithAgentRegistry_IgnoresTypedNil(t *testing.T) {
var registry *agentserver.Registry
h := &handler{}
WithAgentRegistry(registry)(h)
if h.agentRegistry != nil {
t.Fatalf("expected nil agent registry, got %T", h.agentRegistry)
}
}
func decodeResponse(t *testing.T, rr *httptest.ResponseRecorder) apiResponse {
t.Helper()
var resp apiResponse

View File

@ -841,12 +841,14 @@ func runServer(ctx context.Context, cfg *config.Config, logger *slog.Logger) err
options = append(options, api.WithGormDB(gormDB))
// Pre-load sandbox bindings from database into the registry
if agentRegistry != nil {
var sandboxBindings []model.SandboxBinding
if err := gormDB.Find(&sandboxBindings).Error; err == nil {
for _, b := range sandboxBindings {
agentRegistry.SetSandboxAgent(b.AgentID, true)
}
}
}
api.RegisterRoutes(r, options...)