Repository navigation
Expand file tree
/
Copy pathhost_control.go
More file actions
623 lines (578 loc) · 21.5 KB
/
Copy pathhost_control.go
File metadata and controls
623 lines (578 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package engine
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/GrayCodeAI/relay/catalog"
"github.com/GrayCodeAI/relay/catalog/registry"
"github.com/GrayCodeAI/relay/catalog/xiaomi"
"github.com/GrayCodeAI/relay/catalog/zai"
"github.com/GrayCodeAI/relay/config"
"github.com/GrayCodeAI/relay/credentials"
"github.com/GrayCodeAI/relay/runtime"
"github.com/GrayCodeAI/relay/setup"
)
// SecretStoreName is safe presentation metadata for host UIs.
func SecretStoreName() string { return credentials.PlatformSecretStoreName() }
// SetSecretStoreServiceName overrides the OS secret-store service name (default
// "relay"). Hosts call this once at startup so existing credentials filed under
// their product name (e.g. "decoder") stay readable.
func SetSecretStoreServiceName(name string) { credentials.SetServiceName(name) }
// -- Test-fixture re‑exports -------------------------------------------------
// These let host test code inject credential fixtures through the engine
// boundary instead of importing relay/credentials directly.
// SetDefaultStore replaces the process-wide credential store (for tests).
var SetDefaultStore = credentials.SetDefaultStore
// DefaultStore returns the process-wide credential store (for tests).
var DefaultStore = credentials.DefaultStore
// MapStore is the in-memory credential store for tests (alias).
type MapStore = credentials.MapStore
// AccountForEnv returns the keychain account name for an env var.
func AccountForEnv(envVar string) string { return credentials.AccountForEnv(envVar) }
// HasSecret reports whether a secret exists for an env var (for tests).
func HasSecret(ctx context.Context, envKey string) bool { return credentials.HasSecret(ctx, envKey) }
// CredentialStorageReport is safe to print and never includes secrets.
type CredentialStorageReport struct {
PlatformStore string `json:"platform_store"`
Writable bool `json:"writable"`
Detail string `json:"detail,omitempty"`
Formatted string `json:"formatted,omitempty"`
}
func CredentialStorage(ctx context.Context) CredentialStorageReport {
ctx = nonNilContext(ctx)
report := credentials.StorageReportFor(ctx)
return CredentialStorageReport{
PlatformStore: report.PlatformStore, Writable: report.KeychainWritable, Detail: report.KeychainDetail,
Formatted: credentials.FormatStorageReport(report),
}
}
func (e *Engine) credentialStorage(ctx context.Context) CredentialStorageReport {
if e.defaultSecretStore {
return CredentialStorage(ctx)
}
return CredentialStorageReport{
PlatformStore: "host-injected", Writable: false,
Detail: "host-injected credential store configured (writability not probed)",
Formatted: "Credential storage:\n platform store: host-injected\n writability: not probed",
}
}
func (e *Engine) StatePaths() StatePaths {
return StatePaths{Catalog: e.catalogPath, ProviderConfig: e.providerConfigPath}
}
func MigrateEnvFileCredentials(ctx context.Context) (int, error) {
return credentials.MigrateEnvFileCredentials(nonNilContext(ctx))
}
// HasCredentialEnv reports presence without exposing the credential value.
func (e *Engine) HasCredentialEnv(ctx context.Context, envVar string) bool {
return e.hasCredential(nonNilContext(ctx), []string{envVar})
}
// SaveCredentialEnv validates and stores a legacy env-addressed credential.
// New callers should prefer SaveCredential(providerID, secret).
func (e *Engine) SaveCredentialEnv(ctx context.Context, envVar, secret string) error {
envVar, secret = strings.TrimSpace(envVar), strings.TrimSpace(secret)
if envVar == "" || secret == "" {
return invalid("save_credential_env", "relay engine: credential env and value are required")
}
if err := config.ValidateCredentialSecret(envVar, secret); err != nil {
return &Error{Code: ErrorInvalidRequest, Operation: "save_credential_env", Message: err.Error(), Cause: err}
}
if err := e.secretStore.Set(nonNilContext(ctx), credentials.AccountForEnv(envVar), secret); err != nil {
return &Error{Code: ErrorInternal, Operation: "save_credential_env", Message: err.Error(), Cause: err}
}
return nil
}
func (e *Engine) CredentialEnvKeys(providerID string) []string {
if gateway, ok := e.customGateway(providerID); ok {
if gateway.CredentialEnv == "" {
return nil
}
return []string{gateway.CredentialEnv}
}
spec, ok := registry.SpecByProviderID(NormalizeProviderID(providerID))
if !ok {
return nil
}
seen := map[string]bool{}
var out []string
for _, envVar := range append(append([]string{spec.CredentialEnv}, spec.CredentialEnvFallbacks...), spec.CredentialAliases...) {
envVar = strings.TrimSpace(envVar)
if envVar != "" && !seen[envVar] {
seen[envVar] = true
out = append(out, envVar)
}
}
return out
}
// GatewayRegion returns normalized region presentation for regional gateways.
func (e *Engine) GatewayRegion(providerID string) (label string, required bool) {
cfg := config.LoadProviderConfig(e.providerConfigPath)
return gatewayRegionFromConfig(providerID, cfg)
}
func gatewayRegionFromConfig(providerID string, cfg *config.ProviderConfig) (label string, required bool) {
providerID = NormalizeProviderID(providerID)
// Check registry: any provider with RegionOptions is regional.
spec, hasSpec := registry.SpecByProviderID(providerID)
if !hasSpec || len(spec.RegionOptions) == 0 {
return "", false
}
// Provider is regional. Delegate to provider-specific config readers.
switch providerID {
case runtime.GatewayXiaomiTokenPlan:
if cfg == nil {
return "", true
}
region, err := xiaomi.NormalizeRegion(cfg.XiaomiMimoTokenPlanRegion)
return string(region), err != nil
case "zai_coding":
if cfg == nil {
return "", true
}
region, err := zai.NormalizeRegion(cfg.ZAICodingRegion)
return string(region), err != nil
case "zai_payg":
if cfg == nil {
return "", true
}
region, err := zai.NormalizeRegion(cfg.ZAIRegion)
return string(region), err != nil
default:
// Future regional providers: default to required with no label when
// no config reader exists yet.
return "", true
}
}
// SetGatewayRegion persists regional gateway configuration at the Engine path.
func (e *Engine) SetGatewayRegion(ctx context.Context, providerID, value string) error {
ctx = nonNilContext(ctx)
providerID = NormalizeProviderID(providerID)
unlock := lockProviderStatePath(e.providerConfigPath)
defer unlock()
cfg, err := e.loadProviderConfigStrict()
if err != nil {
return &Error{Code: ErrorInternal, Operation: "set_gateway_region", Provider: providerID, Message: err.Error(), Cause: err}
}
switch providerID {
case runtime.GatewayXiaomiTokenPlan:
region, err := xiaomi.NormalizeRegion(value)
if err != nil {
return err
}
cfg.XiaomiMimoTokenPlanRegion = string(region)
if base, err := config.ResolveXiaomiOpenAIBase(providerID, cfg); err == nil {
cfg.XiaomiMimoTokenPlanBaseURL = base
}
case "zai_payg", "zai_coding":
region, err := zai.NormalizeRegion(value)
if err != nil {
return err
}
if providerID == "zai_coding" {
cfg.ZAICodingRegion = string(region)
if base, err := zai.ResolveOpenAIBase(zai.PlanCoding, region, cfg.ZAICodingBaseURL); err == nil {
cfg.ZAICodingBaseURL = base
}
} else {
cfg.ZAIRegion = string(region)
if base, err := zai.ResolveOpenAIBase(zai.PlanGeneral, region, cfg.ZAIBaseURL); err == nil {
cfg.ZAIBaseURL = base
}
}
default:
return invalid("set_gateway_region", "relay engine: gateway does not support regions")
}
if err := e.saveProviderConfig(ctx, cfg); err != nil {
return err
}
return nil
}
// ApplyGatewayEnvironment derives process-local regional base URLs.
//
// Deprecated: invocation-scoped hosts must use Engine state directly and must
// not mutate process environment. This remains only for legacy callers.
func (e *Engine) ApplyGatewayEnvironment(_ context.Context, providerID string) {
cfg := config.LoadProviderConfig(e.providerConfigPath)
if cfg == nil {
return
}
switch NormalizeProviderID(providerID) {
case runtime.GatewayXiaomiTokenPlan:
if cfg.XiaomiMimoTokenPlanRegion != "" {
_ = os.Setenv(config.EnvXiaomiTokenPlanRegion, cfg.XiaomiMimoTokenPlanRegion)
}
if base, err := config.ResolveXiaomiOpenAIBase(runtime.GatewayXiaomiTokenPlan, cfg); err == nil && base != "" {
_ = os.Setenv(config.EnvXiaomiTokenPlanBaseURL, base)
}
case "zai_payg":
if cfg.ZAIRegion != "" {
_ = os.Setenv("ZAI_REGION", cfg.ZAIRegion)
}
if region, err := zai.NormalizeRegion(cfg.ZAIRegion); err == nil {
if base, err := zai.ResolveOpenAIBase(zai.PlanGeneral, region, cfg.ZAIBaseURL); err == nil && base != "" {
_ = os.Setenv("ZAI_BASE_URL", base)
}
}
case "zai_coding":
if cfg.ZAICodingRegion != "" {
_ = os.Setenv("ZAI_CODING_REGION", cfg.ZAICodingRegion)
}
if region, err := zai.NormalizeRegion(cfg.ZAICodingRegion); err == nil {
if base, err := zai.ResolveOpenAIBase(zai.PlanCoding, region, cfg.ZAICodingBaseURL); err == nil && base != "" {
_ = os.Setenv("ZAI_CODING_BASE_URL", base)
}
}
}
}
func (e *Engine) CatalogHealth(ctx context.Context) CatalogHealth {
health := CatalogHealth{Path: e.catalogPath}
exists, modified, size, err := catalog.CacheInfo(e.catalogPath)
health.Exists, health.ModifiedAt, health.Size = exists, modified, size
if err != nil {
health.Error = err.Error()
return health
}
if !exists {
return health
}
compiled, err := catalog.LoadCatalog(nonNilContext(ctx), catalog.LoadCatalogOptions{CachePath: e.catalogPath, RequireCache: true})
if err != nil {
health.Error = err.Error()
return health
}
health.Models = len(compiled.ModelsByID)
health.Deployments = len(compiled.DeploymentsByID)
health.Offerings = len(compiled.OfferingsByID)
if compiled.Catalog != nil {
health.StaleAfter = compiled.Catalog.StaleAfter
health.Stale = !compiled.Catalog.StaleAfter.IsZero() && time.Now().UTC().After(compiled.Catalog.StaleAfter)
if compiled.Catalog.Provenance != nil {
health.Source = compiled.Catalog.Provenance.Source
}
}
return health
}
func (e *Engine) CanonicalModel(ctx context.Context, modelID string) string {
if _, ok := e.customGatewayForModel(modelID); ok {
return strings.TrimSpace(modelID)
}
compiled, err := e.policyCatalog(ctx)
if err == nil {
if canonical := catalog.ResolveModel(compiled, modelID); canonical != "" {
return canonical
}
}
return strings.TrimSpace(modelID)
}
func (e *Engine) GatewayForModel(ctx context.Context, modelID string) string {
if gateway, ok := e.customGatewayForModel(modelID); ok {
return gateway.ID
}
compiled, _ := e.policyCatalog(ctx)
return NormalizeProviderID(catalog.GatewayForModel(compiled, modelID))
}
func (e *Engine) DeploymentRoutingEnabled(override *bool) bool {
if override != nil {
return *override
}
cfg, err := e.loadProviderConfigStrict()
return err == nil && setup.DeploymentRoutingFromState(cfg)
}
func (e *Engine) DeploymentStatus(ctx context.Context, activeModel string) (string, error) {
// Compatibility formatter remains Relay-owned while setup internals migrate
// to injected paths. Host code receives presentation text only.
report, err := setup.DeploymentStatusFromPaths(nonNilContext(ctx), activeModel, e.providerConfigPath, e.catalogPath)
if err != nil {
return "", err
}
return setup.FormatStatus(report), nil
}
func (e *Engine) DeploymentSummary(ctx context.Context, activeModel string) (DeploymentSummary, error) {
report, err := setup.DeploymentStatusFromPaths(nonNilContext(ctx), activeModel, e.providerConfigPath, e.catalogPath)
if err != nil {
return DeploymentSummary{}, err
}
return DeploymentSummary{
RoutingSource: report.RoutingSource, RoutingStages: report.RoutingStages,
Formatted: setup.FormatStatus(report),
}, nil
}
func (e *Engine) RoutingPreview(ctx context.Context, modelID string) (string, error) {
return setup.RoutingPreviewFromPaths(nonNilContext(ctx), modelID, e.providerConfigPath, e.catalogPath)
}
// FormatSetupError returns a safe provider setup hint.
func FormatSetupError(providerID string, err error) string {
if err == nil {
return ""
}
return runtime.FormatSetupError(providerID, err).Error()
}
// CredentialGuidance returns provider-specific, non-secret setup guidance.
func CredentialGuidance(providerID, secret string) string {
switch NormalizeProviderID(providerID) {
case runtime.GatewayXiaomiTokenPlan:
return xiaomi.KeyMismatchHint(xiaomi.BillingTokenPlan, secret)
case xiaomi.ProviderPayAsYouGo:
return xiaomi.KeyMismatchHint(xiaomi.BillingPayAsYouGo, secret)
default:
return ""
}
}
func (e *Engine) DefaultProviderFilter(ctx context.Context) string {
selection := e.EffectiveSelection(ctx, SelectionOptions{})
return selection.Provider
}
func (e *Engine) Preflight(ctx context.Context) PreflightReport {
return e.PreflightWithOptions(ctx, PreflightOptions{})
}
func (e *Engine) PreflightWithOptions(ctx context.Context, opts PreflightOptions) PreflightReport {
ctx = nonNilContext(ctx)
var checks []PreflightCheck
cfg, stateErr := e.loadProviderConfigStrict()
if stateErr != nil {
checks = append(checks, PreflightCheck{Name: "provider_state", Status: CheckFail, Detail: stateErr.Error()})
cfg = &config.ProviderConfig{}
} else {
checks = append(checks, PreflightCheck{Name: "provider_state", Status: CheckOK, Detail: e.providerConfigPath})
}
providerID := NormalizeProviderID(config.ActiveProvider(cfg))
activeModel := strings.TrimSpace(config.ActiveModel(cfg))
customGateway, custom := e.customGateway(providerID)
var compiled *catalog.CompiledCatalog
if custom {
checks = append(checks, PreflightCheck{Name: "catalog", Status: CheckOK, Detail: "not required for selected custom gateway"})
} else {
health := e.CatalogHealth(ctx)
if health.Error != "" || health.Models == 0 {
checks = append(checks, PreflightCheck{Name: "catalog", Status: CheckFail, Detail: fmt.Sprintf("catalog unavailable at %s", health.Path)})
} else {
var err error
compiled, err = catalog.LoadCatalog(ctx, catalog.LoadCatalogOptions{CachePath: e.catalogPath, RequireCache: true})
if err != nil {
checks = append(checks, PreflightCheck{Name: "catalog", Status: CheckFail, Detail: err.Error()})
} else {
checks = append(checks, PreflightCheck{Name: "catalog", Status: CheckOK, Detail: fmt.Sprintf("%d models cached at %s", health.Models, health.Path)})
}
}
}
storage := e.credentialStorage(ctx)
status := CheckWarn
if storage.Writable {
status = CheckOK
}
checks = append(checks, PreflightCheck{Name: "credentials_store", Status: status, Detail: storage.Detail})
credentialReady := false
credentialDetail := "no active provider selected"
if custom {
credentialReady = customGateway.CredentialEnv == "" || e.hasCredential(ctx, []string{customGateway.CredentialEnv})
credentialDetail = providerID + " credential is not configured"
if credentialReady {
credentialDetail = providerID + " credential is configured"
}
} else if providerID != "" && compiled != nil && stateErr == nil {
if spec, ok := registry.SpecByProviderID(providerID); ok {
env := e.discoveryCredentialsFromConfig(ctx, compiled, cfg).Env()
deployments := buildDeployments(compiled, cfg.Deployments, env)
if deployment, ok := deployments[spec.DeploymentID]; ok {
_, credentialReady = setup.ProviderForDeploymentFromState(spec.DeploymentID, deployment, cfg)
}
credentialDetail = providerID + " deployment is not fully configured"
if credentialReady {
credentialDetail = providerID + " deployment is configured"
}
} else {
credentialDetail = "unknown active provider " + providerID
}
}
credentialStatus := CheckFail
if credentialReady {
credentialStatus = CheckOK
}
checks = append(checks, PreflightCheck{Name: "credentials", Status: credentialStatus, Detail: credentialDetail})
modelDetail := activeModel
switch {
case activeModel == "":
checks = append(checks, PreflightCheck{Name: "model", Status: CheckFail, Detail: "no model selected"})
case custom:
checks = append(checks, PreflightCheck{Name: "model", Status: CheckOK, Detail: activeModel})
case compiled != nil && providerModelAvailable(compiled, providerID, activeModel):
checks = append(checks, PreflightCheck{Name: "model", Status: CheckOK, Detail: activeModel})
default:
checks = append(checks, PreflightCheck{Name: "model", Status: CheckFail, Detail: modelDetail + " is not available through " + providerID})
}
localReady := true
for _, check := range checks {
if check.Status == CheckFail {
localReady = false
}
}
liveVerified := false
switch {
case !opts.VerifyLive:
checks = append(checks, PreflightCheck{Name: "provider_live", Status: CheckWarn, Detail: "not checked (local preflight only)"})
case !localReady:
checks = append(checks, PreflightCheck{Name: "provider_live", Status: CheckWarn, Detail: "skipped until local setup is complete"})
default:
var err error
if custom {
err = e.probeCustomGateway(ctx, customGateway, "")
} else {
var liveModels []Model
liveModels, err = e.ListLiveModels(ctx, providerID)
if err == nil && !modelListContains(liveModels, activeModel) {
err = &Error{
Code: ErrorModelUnavailable, Operation: "preflight", Provider: providerID, Model: activeModel,
Message: fmt.Sprintf("relay engine: selected model %q is not present in %s live model list", activeModel, providerID),
}
}
}
if err != nil {
checks = append(checks, PreflightCheck{Name: "provider_live", Status: CheckFail, Detail: fmt.Sprintf("%s verification failed: %v", providerID, err)})
} else {
liveVerified = true
checks = append(checks, PreflightCheck{Name: "provider_live", Status: CheckOK, Detail: providerID + " connectivity and authentication verified"})
}
}
ready := localReady
if opts.VerifyLive && !liveVerified {
ready = false
}
return PreflightReport{Ready: ready, LiveVerified: liveVerified, Checks: checks}
}
func modelListContains(models []Model, modelID string) bool {
modelID = strings.TrimSpace(modelID)
for _, model := range models {
if strings.TrimSpace(model.ID) == modelID || strings.TrimSpace(model.CanonicalID) == modelID {
return true
}
}
return false
}
func providerModelAvailable(compiled *catalog.CompiledCatalog, providerID, modelID string) bool {
if compiled == nil || providerID == "" || strings.TrimSpace(modelID) == "" {
return false
}
if _, ok := catalog.CanonicalModelForProviderNative(compiled, providerID, modelID); ok {
return true
}
canonical, ok := compiled.CanonicalModelForAliasOrID(modelID)
if !ok {
canonical = strings.TrimSpace(modelID)
}
spec, ok := registry.SpecByProviderID(providerID)
if !ok {
return false
}
for _, offering := range compiled.OfferingsByDeployment[spec.DeploymentID] {
if offering.CanonicalModelID == canonical || offering.NativeModelID == modelID {
return true
}
}
return false
}
func FormatPreflight(report PreflightReport) string {
var b strings.Builder
switch {
case report.Ready && report.LiveVerified:
b.WriteString("Preflight: ready to chat (live verified)\n")
case report.Ready:
b.WriteString("Preflight: locally ready to chat\n")
default:
b.WriteString("Preflight: setup incomplete\n")
}
for _, check := range report.Checks {
icon := "+"
switch check.Status {
case CheckWarn:
icon = "!"
case CheckFail:
icon = "x"
}
fmt.Fprintf(&b, " %s %s: %s\n", icon, check.Name, check.Detail)
}
return strings.TrimRight(b.String(), "\n")
}
// ProviderStateSecurityStatus inspects provider.json without returning values.
func (e *Engine) ProviderStateSecurityStatus() ProviderStateSecurity {
status := ProviderStateSecurity{Path: e.providerConfigPath}
cfg, err := config.LoadProviderConfigWithError(e.providerConfigPath)
if err != nil {
status.Error = err.Error()
return status
}
if cfg == nil {
return status
}
if config.ProviderConfigContainsSecrets(*cfg) {
status.HasSecrets = true
status.Detail = "provider state has credential fields on disk"
return status
}
return status
}
// MigrateProviderSecrets imports credential fields persisted in provider
// state into the Engine's secret store, then atomically strips them from
// provider.json.
func (e *Engine) MigrateProviderSecrets() error {
return e.MigrateProviderSecretsContext(context.Background())
}
func (e *Engine) MigrateProviderSecretsContext(ctx context.Context) error {
ctx = nonNilContext(ctx)
path := e.providerConfigPath
unlock := lockProviderStatePath(path)
defer unlock()
marker := path + ".secrets-migrated"
cfgState, err := config.LoadProviderConfigWithError(path)
if err != nil {
return err
}
if cfgState == nil {
return nil
}
cfg := *cfgState
writes, err := e.importProviderConfigSecrets(ctx, cfg)
if err != nil {
return &Error{Code: ErrorInternal, Operation: "migrate_provider_secrets", Message: "relay engine: could not import provider credentials", Cause: err}
}
sanitized := config.SanitizeProviderConfigForDisk(cfg)
if err := writeProviderConfigAtomic(path, &sanitized); err != nil {
return errors.Join(err, e.rollbackImportedCredentials(ctx, writes))
}
if err := writeBytesAtomic(marker, []byte("ok\n")); err != nil {
return err
}
return nil
}
func writeProviderConfigAtomic(path string, cfg *config.ProviderConfig) (err error) {
return config.SaveProviderConfig(cfg, path)
}
func writeBytesAtomic(path string, data []byte) (err error) {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
tmp, err := os.CreateTemp(dir, ".provider-*.tmp")
if err != nil {
return err
}
tmpPath := tmp.Name()
defer func() {
_ = tmp.Close()
_ = os.Remove(tmpPath)
}()
if err := tmp.Chmod(0o600); err != nil {
return err
}
if _, err := tmp.Write(data); err != nil {
return err
}
if err := tmp.Sync(); err != nil {
return err
}
if err := tmp.Close(); err != nil {
return err
}
return os.Rename(tmpPath, path)
}