| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "math" |
| 7 | "sync/atomic" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/config" |
| 11 | "reasonix/internal/extension/providerext" |
| 12 | "reasonix/internal/provider" |
| 13 | ) |
| 14 | |
| 15 | // Only a threshold is shared, never the mutable config snapshot. Foreground |
| 16 | // executor/planner reads are live; child agents freeze a value at creation. |
| 17 | type liveRuntimeSettings struct{ compactRatioBits atomic.Uint64 } |
| 18 | |
| 19 | func newLiveRuntimeSettings(ratio float64) *liveRuntimeSettings { |
| 20 | s := &liveRuntimeSettings{} |
| 21 | s.apply(ratio) |
| 22 | return s |
| 23 | } |
| 24 | |
| 25 | func (s *liveRuntimeSettings) compactRatio() float64 { |
| 26 | return math.Float64frombits(s.compactRatioBits.Load()) |
| 27 | } |
| 28 | func (s *liveRuntimeSettings) apply(ratio float64) { s.compactRatioBits.Store(math.Float64bits(ratio)) } |
| 29 | |
| 30 | type skillOptionsFactory func(context.Context, int, *provider.Pricing, int, int) agent.Options |
| 31 | |
| 32 | func (s *liveRuntimeSettings) skillOptions(factory skillOptionsFactory) skillOptionsFactory { |
| 33 | return func(ctx context.Context, steps int, price *provider.Pricing, window, depth int) agent.Options { |
| 34 | opts := factory(ctx, steps, price, window, depth) |
| 35 | opts.CompactRatio = s.compactRatio() |
| 36 | return opts |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Explicit caller selections can change runtime identity without changing cfg. |
| 41 | // Copy optional effort values rather than retaining mutable caller pointers. |
| 42 | type runtimeSelection struct { |
| 43 | model, effort string |
| 44 | hasEffort bool |
| 45 | } |
| 46 | |
| 47 | func runtimeSelectionFrom(opts Options) runtimeSelection { |
| 48 | s := runtimeSelection{model: opts.Model} |
| 49 | if opts.EffortOverride != nil { |
| 50 | s.effort, s.hasEffort = *opts.EffortOverride, true |
| 51 | } |
| 52 | return s |
| 53 | } |
| 54 | |
| 55 | func validateLiveCompactRatio(ratio float64) error { |
| 56 | if math.IsNaN(ratio) || math.IsInf(ratio, 0) { |
| 57 | return fmt.Errorf("compact ratio must be finite") |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // Compare canonical model identities: a frontend may make the already-selected |
| 63 | // default explicit while saving an unrelated setting. |
| 64 | func runtimeSelectionForConfig(previous *BuildResult, cfg *config.Config, opts Options) (runtimeSelection, error) { |
| 65 | selection := runtimeSelectionFrom(opts) |
| 66 | name := opts.Model |
| 67 | if name == "" { |
| 68 | if resolved, _, ok := cfg.ResolveNewSessionChatModel(); ok { |
| 69 | name = resolved |
| 70 | } |
| 71 | } |
| 72 | resolver := opts.ProviderResolver |
| 73 | if resolver == nil && providerext.PluginRefOwner(name) != "" { |
| 74 | resolver = previous.ProviderResolver |
| 75 | } |
| 76 | _, ref, err := resolveModelEntry(resolver, cfg, name) |
| 77 | selection.model = ref |
| 78 | return selection, err |
| 79 | } |
| 80 |