| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "crypto/hmac" |
| 5 | "crypto/rand" |
| 6 | "crypto/sha256" |
| 7 | "encoding/hex" |
| 8 | "encoding/json" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | "reasonix/internal/control" |
| 12 | "reasonix/internal/netclient" |
| 13 | ) |
| 14 | |
| 15 | // Config can contain proxy passwords, headers, and service tokens. Keep its |
| 16 | // fingerprint process-local and keyed rather than exposing a password oracle. |
| 17 | var runtimeConfigKey = func() []byte { |
| 18 | key := make([]byte, 32) |
| 19 | if _, err := rand.Read(key); err != nil { |
| 20 | panic(err) |
| 21 | } |
| 22 | return key |
| 23 | }() |
| 24 | |
| 25 | // The extension graph omits host configuration. Its no-op/narrow plans may |
| 26 | // reuse a controller only when the effective configuration still matches the |
| 27 | // one that built it, including project overrides and caller-owned snapshots. |
| 28 | func reusableRuntimeConfiguration(previous *BuildResult, opts Options) (*config.Config, bool, error) { |
| 29 | if previous.configFingerprint == "" || previous.liveSettings == nil { |
| 30 | return nil, false, nil |
| 31 | } |
| 32 | root := ResolveWorkspaceRoot(opts.WorkspaceRoot) |
| 33 | cfg, resolved, err := resolveBuildSelection(root, opts) |
| 34 | if err != nil { |
| 35 | return nil, true, err |
| 36 | } |
| 37 | selection, selectionErr := runtimeSelectionForConfig(previous, cfg, resolved) |
| 38 | if selectionErr != nil || previous.selection != selection { |
| 39 | return nil, false, nil |
| 40 | } |
| 41 | fingerprint := runtimeConfigFingerprint(cfg, root) |
| 42 | return cfg, fingerprint != "" && fingerprint == previous.configFingerprint, nil |
| 43 | } |
| 44 | |
| 45 | func withRuntimeConfiguration(res *BuildResult, cfg *config.Config, live *liveRuntimeSettings, opts Options) *BuildResult { |
| 46 | res.configFingerprint = runtimeConfigFingerprint(cfg, res.Controller.WorkspaceRoot()) |
| 47 | res.liveSettings, res.selection = live, runtimeSelectionFrom(opts) |
| 48 | res.selection.model = res.Controller.ModelRef() |
| 49 | return res |
| 50 | } |
| 51 | |
| 52 | // Only assembly dependencies participate: frontend/transport preferences do not |
| 53 | // own this controller, and compaction thresholds have their own live setting. |
| 54 | type runtimeConfigDependencies struct { |
| 55 | DefaultModel, ModelAccess, ResponseLanguage, DisplayCurrency string |
| 56 | SystemPrompt string |
| 57 | Agent config.AgentConfig |
| 58 | Providers []runtimeProviderMetadata |
| 59 | Tools config.ToolsConfig |
| 60 | Checkpoints config.CheckpointsConfig |
| 61 | Permissions config.PermissionsConfig |
| 62 | Sandbox config.SandboxConfig |
| 63 | Proxy netclient.ProxySpec |
| 64 | Environment config.EnvironmentConfig |
| 65 | Plugins []config.PluginEntry |
| 66 | Skills config.SkillsConfig |
| 67 | LSP config.LSPConfig |
| 68 | Browser config.BrowserConfig |
| 69 | Secrets config.SecretsConfig |
| 70 | CredentialEnvNames []string |
| 71 | PluginOwners map[string]string |
| 72 | SkillOwners, AgentOwners map[string][]string |
| 73 | } |
| 74 | |
| 75 | type runtimeProviderMetadata struct { |
| 76 | BalanceURL, APIKeyEnv, CredentialProxyURL string |
| 77 | Authentication control.AuthenticationState |
| 78 | } |
| 79 | |
| 80 | func runtimeConfigFingerprint(cfg *config.Config, roots ...string) string { |
| 81 | if cfg == nil { |
| 82 | return "" |
| 83 | } |
| 84 | root := "." |
| 85 | if len(roots) > 0 { |
| 86 | root = roots[0] |
| 87 | } |
| 88 | prompt, err := cfg.ResolveSystemPromptForRoot(root) |
| 89 | if err != nil { |
| 90 | return "" // Let full assembly report the ordinary prompt resolution error. |
| 91 | } |
| 92 | modelAccess := cfg.ModelRuntimeFingerprint("") |
| 93 | if modelAccess == "" { |
| 94 | return "" |
| 95 | } |
| 96 | deps := projectRuntimeConfig(cfg, root) |
| 97 | deps.SystemPrompt, deps.ModelAccess = prompt, modelAccess |
| 98 | data, err := json.Marshal(deps) |
| 99 | if err != nil { |
| 100 | return "" // An unrepresentable snapshot must never authorize reuse. |
| 101 | } |
| 102 | mac := hmac.New(sha256.New, runtimeConfigKey) |
| 103 | _, _ = mac.Write(data) |
| 104 | return hex.EncodeToString(mac.Sum(nil)) |
| 105 | } |
| 106 | |
| 107 | func projectRuntimeConfig(cfg *config.Config, root string) runtimeConfigDependencies { |
| 108 | // ModelRuntimeFingerprint owns provider credentials, access, role models and |
| 109 | // effort/concurrency settings. Keep only the remaining captured agent options. |
| 110 | a := cfg.Agent |
| 111 | deps := runtimeConfigDependencies{ |
| 112 | DefaultModel: cfg.DefaultModel, ResponseLanguage: cfg.ResponseLanguage(), |
| 113 | DisplayCurrency: cfg.ExplicitDisplayCurrency(), |
| 114 | Agent: config.AgentConfig{ |
| 115 | Temperature: a.Temperature, GuardianTemperature: a.GuardianTemperature, |
| 116 | TaskCostBudget: a.TaskCostBudget, TaskTimeBudgetMinutes: a.TaskTimeBudgetMinutes, |
| 117 | GoalTokenBudget: a.GoalTokenBudget, OutputStyle: a.OutputStyle, |
| 118 | ReasoningLanguage: cfg.ReasoningLanguage(), ColdResumePrune: a.ColdResumePrune, |
| 119 | Keep: a.Keep, RecentKeep: a.RecentKeep, PlanModeReadOnlyCommands: a.PlanModeReadOnlyCommands, |
| 120 | SoftCompactRatio: a.SoftCompactRatio, ToolResultSnipRatio: a.ToolResultSnipRatio, |
| 121 | CompactForceRatio: a.CompactForceRatio, ContextEditing: a.ContextEditing, |
| 122 | }, |
| 123 | Tools: cfg.Tools, Checkpoints: cfg.Checkpoints, Permissions: cfg.Permissions, |
| 124 | Sandbox: config.SandboxConfig{ |
| 125 | WorkspaceRoot: cfg.WriteRootsForRoot(root)[0], AllowWrite: cfg.AllowWriteRoots(), |
| 126 | ForbidRead: cfg.ForbidReadRootsForRoot(root), Bash: cfg.BashMode(), Network: cfg.Sandbox.Network, |
| 127 | }, |
| 128 | Proxy: cfg.NetworkProxySpec(), Environment: cfg.Environment, Plugins: cfg.Plugins, |
| 129 | Skills: config.SkillsConfig{ |
| 130 | Paths: cfg.SkillCustomPaths(), ExcludedPaths: cfg.SkillExcludedPaths(), |
| 131 | DisabledSkills: cfg.DisabledSkillNames(), MaxDepth: cfg.SkillMaxDepth(), |
| 132 | DisableImplicitInvocation: !cfg.ImplicitSkillInvocationEnabled(), |
| 133 | }, |
| 134 | LSP: cfg.LSP, Browser: cfg.Browser, Secrets: cfg.Secrets, |
| 135 | CredentialEnvNames: cfg.CredentialEnvNames(), PluginOwners: pluginPackageOwners(cfg), |
| 136 | SkillOwners: cfg.PluginPackageSkillOwners(), AgentOwners: cfg.PluginPackageAgentOwners(), |
| 137 | } |
| 138 | // These are deliberately outside the model digest, but boot captures them for |
| 139 | // balance lookups, missing-credential diagnostics and subprocess protection. |
| 140 | for i := range cfg.Providers { |
| 141 | p := &cfg.Providers[i] |
| 142 | deps.Providers = append(deps.Providers, runtimeProviderMetadata{ |
| 143 | BalanceURL: p.BalanceURL, APIKeyEnv: p.APIKeyEnv, CredentialProxyURL: p.CredentialProxyURL(), |
| 144 | Authentication: authenticationStateForModelEntry(p, ""), |
| 145 | }) |
| 146 | } |
| 147 | return deps |
| 148 | } |
| 149 |