返回 DeepSeek-Reasonix
reasoning_preflight.go
根目录 / internal / boot / reasoning_preflight.go
1 package boot
2
3 import (
4 "fmt"
5 "slices"
6 "strings"
7
8 "reasonix/internal/config"
9 "reasonix/internal/event"
10 "reasonix/internal/extension/providerext"
11 "reasonix/internal/provider"
12 )
13
14 // RoleReasoningError retains the adapter error for errors.As while giving every
15 // frontend enough context to fix the role that actually prevented assembly.
16 type RoleReasoningError struct {
17 Role, Ref, Effort, Source, Kind string
18 Supported []string
19 Err error
20 }
21
22 func (e *RoleReasoningError) Error() string {
23 return fmt.Sprintf("%s model %q: effort=%q (source=%s, API=%s, supported=%v): %v", e.Role, e.Ref, e.Effort, e.Source, e.Kind, e.Supported, e.Err)
24 }
25
26 func (e *RoleReasoningError) Unwrap() error { return e.Err }
27
28 // ValidateReasoningSnapshot lets Desktop validate a pending configuration
29 // before a workspace correction is allowed to retire its current controller.
30 func ValidateReasoningSnapshot(cfg *config.Config, opts Options) error {
31 opts = rebindReasoningSelection(cfg, opts)
32 return preflightRoleReasoning(cfg, opts, opts.ProviderResolver, false)
33 }
34
35 func rebindReasoningSelection(cfg *config.Config, opts Options) Options {
36 if opts.EffortModel != "" {
37 target := opts.Model
38 if target == "" {
39 target, _, _ = cfg.ResolveNewSessionChatModel()
40 }
41 opts.EffortOverride = config.RebindSessionEffort(cfg, opts.EffortModel, target, opts.EffortOverride)
42 }
43 return opts
44 }
45
46 func resolveBuildSelection(root string, opts Options) (*config.Config, Options, error) {
47 cfg, err := resolveBuildConfiguration(root, opts.Model, opts.ConfigSnapshot)
48 if err != nil {
49 return nil, opts, err
50 }
51 if err := opts.ModelSettings.Apply(cfg, root); err != nil {
52 return nil, opts, err
53 }
54 if err := validateLiveCompactRatio(cfg.Agent.CompactRatio); err != nil {
55 return nil, opts, err
56 }
57 return cfg, rebindReasoningSelection(cfg, opts), nil
58 }
59
60 // resolveBuildConfiguration gives a caller-owned snapshot the runtime contract
61 // LoadModelRuntimeSnapshot provides: legacy refs expanded, then credentials
62 // frozen so no lazy resolver rereads the store during the runtime's lifetime.
63 func resolveBuildConfiguration(root, modelRef string, snapshot *config.Config) (*config.Config, error) {
64 if snapshot != nil {
65 config.NormalizeLegacyMimoCustomProvidersForRefs(snapshot, modelRef)
66 snapshot.FreezeProviderCredentials()
67 return snapshot, nil
68 }
69 return config.LoadModelRuntimeSnapshot(root, modelRef)
70 }
71
72 // explicitVisionModel returns the configured vision reference, or "" when the
73 // runtime selects one within the active provider ("auto" or unset).
74 func explicitVisionModel(cfg *config.Config) string {
75 ref := strings.TrimSpace(cfg.Agent.VisionModel)
76 if strings.EqualFold(ref, "auto") {
77 return ""
78 }
79 return ref
80 }
81
82 // preflightRoleReasoning uses the same immutable config snapshot as assembly.
83 // The first pass excludes extension refs because their adapter declarations are
84 // obtained by the extension handshake; the second pass validates those refs
85 // before any session, workspace lease, MCP tools or controller is constructed.
86 func preflightRoleReasoning(cfg *config.Config, opts Options, resolver provider.Resolver, extensionsOnly bool) error {
87 model := strings.TrimSpace(opts.Model)
88 if model == "" {
89 model, _, _ = cfg.ResolveNewSessionChatModel()
90 }
91 type roleSelection struct {
92 role, ref, source string
93 effort *string
94 // optional roles are constructed lazily at use time, so an unresolvable
95 // reference keeps that fallback; only a resolvable one is validated.
96 optional bool
97 }
98 roles := []roleSelection{
99 {role: "execution", ref: model, source: "session effort override", effort: opts.EffortOverride},
100 {role: "planner", ref: effectivePlannerModel(cfg, opts)},
101 {role: "vision", ref: explicitVisionModel(cfg), optional: true},
102 {role: "guardian", ref: cfg.Agent.GuardianModel},
103 }
104 subagentModel := strings.TrimSpace(cfg.Agent.SubagentModel)
105 if subagentModel == "" {
106 subagentModel = model
107 }
108 subagentEffort := preflightSubagentEffort(cfg, resolver, model)
109 if cfg.Agent.SubagentModel != "" || subagentEffort != nil {
110 roles = append(roles, roleSelection{role: "subagent", ref: subagentModel, source: "agent.subagent_effort", effort: subagentEffort, optional: true})
111 }
112 // Seed capacity from one map only. Adding the two attacker-controlled map
113 // lengths can overflow before make validates the allocation size.
114 keys := make([]string, 0, len(cfg.Agent.SubagentModels))
115 for key := range cfg.Agent.SubagentModels {
116 keys = append(keys, key)
117 }
118 for key := range cfg.Agent.SubagentEfforts {
119 if !slices.Contains(keys, key) {
120 keys = append(keys, key)
121 }
122 }
123 slices.Sort(keys)
124 for _, key := range keys {
125 ref := strings.TrimSpace(cfg.Agent.SubagentModels[key])
126 if ref == "" {
127 ref = subagentModel
128 }
129 effort, source := subagentEffort, "agent.subagent_effort"
130 if value := cfg.Agent.SubagentEfforts[key]; value != "" {
131 effort, source = &value, "agent.subagent_efforts."+key
132 }
133 roles = append(roles, roleSelection{role: "subagent[" + key + "]", ref: ref, source: source, effort: effort, optional: true})
134 }
135 for _, selection := range roles {
136 ref := strings.TrimSpace(selection.ref)
137 if ref == "" || (providerext.PluginRefOwner(ref) != "") != extensionsOnly {
138 continue
139 }
140 entry, resolved, err := resolveModelEntry(resolver, cfg, ref)
141 if err != nil {
142 // A migrated account identity must still fail closed, and a
143 // configured invalid effort is checked whenever the model resolves.
144 if selection.optional && cfg.ModelReferenceError(ref) == nil && !extensionsOnly {
145 continue
146 }
147 return fmt.Errorf("%s_model %q: %w", selection.role, ref, err)
148 }
149 copy := *config.ResolveReasoningEntry(entry)
150 source := "provider default"
151 if copy.Effort != "" {
152 source = "providers." + copy.Name + ".effort"
153 } else if copy.DefaultEffort != "" {
154 source = "providers." + copy.Name + ".default_effort"
155 if raw, ok := cfg.Provider(copy.Name); ok && raw.ModelOverrides[copy.Model].DefaultEffort != "" {
156 source = "providers." + copy.Name + ".model_overrides." + copy.Model + ".default_effort"
157 }
158 }
159 if selection.effort != nil {
160 copy.Effort, source = *selection.effort, selection.source
161 if copy.Kind == "anthropic" && copy.Effort != "" && copy.Thinking == "" {
162 copy.Thinking = "adaptive"
163 }
164 }
165 cap := config.ReasoningCapabilityForEntry(&copy)
166 effort := config.EffectiveEffort(&copy)
167 if err := cap.Validate(copy.Model, effort); err != nil {
168 if effort == "" {
169 effort = cap.Default
170 }
171 return &RoleReasoningError{selection.role, resolved, effort, source, copy.Kind, cap.IDs(), err}
172 }
173 }
174 return nil
175 }
176
177 // inheritedSubagentEffort is agent.subagent_effort as a subagent that follows
178 // the execution model receives it. The setting is a cross-model default, so a
179 // model that cannot take it falls back to its own default and the dropped value
180 // is returned for a notice; paired with agent.subagent_model it stays strict.
181 func inheritedSubagentEffort(cfg *config.Config, execution *config.ProviderEntry) (effort, dropped string) {
182 effort = strings.TrimSpace(cfg.Agent.SubagentEffort)
183 if effort == "" || strings.TrimSpace(cfg.Agent.SubagentModel) != "" || execution == nil {
184 return effort, ""
185 }
186 entry := *config.ResolveReasoningEntry(execution)
187 entry.Effort = effort
188 if entry.Kind == "anthropic" && entry.Thinking == "" {
189 entry.Thinking = "adaptive"
190 }
191 if config.ReasoningCapabilityForEntry(&entry).Validate(entry.Model, config.EffectiveEffort(&entry)) != nil {
192 return "", effort
193 }
194 return effort, ""
195 }
196
197 func preflightSubagentEffort(cfg *config.Config, resolver provider.Resolver, model string) *string {
198 value := strings.TrimSpace(cfg.Agent.SubagentEffort)
199 if execution, _, err := resolveModelEntry(resolver, cfg, model); err == nil {
200 value, _ = inheritedSubagentEffort(cfg, execution)
201 }
202 if value == "" {
203 return nil
204 }
205 return &value
206 }
207
208 // subagentEffortDefault is the agent.subagent_effort the assembled runtime
209 // hands its subagents, with a notice naming a value the model could not take.
210 func subagentEffortDefault(cfg *config.Config, execution *config.ProviderEntry, sink event.Sink) string {
211 effort, dropped := inheritedSubagentEffort(cfg, execution)
212 if dropped != "" {
213 sink.Emit(event.Event{Kind: event.Notice, Level: event.LevelWarn, Text: "Subagents use the model's default effort.", Detail: fmt.Sprintf("agent.subagent_effort = %q is not supported by %q, which subagents inherit because agent.subagent_model is unset; the setting is kept and applies again on a model that supports it.", dropped, execution.Model)})
214 }
215 return effort
216 }
217
217 lines GO