package runner import ( "context" "crypto/sha256" "encoding/hex" "encoding/json" "os" "path/filepath" "slices" "sort" "amadan.net/oficina/equipo/internal/runner/provider" runnersetup "amadan.net/oficina/equipo/internal/runner/setup" "amadan.net/oficina/equipo/internal/runner/workspace" ) type AccountBinding = runnersetup.AccountBinding func (c Config) securityPaths(bs []AccountBinding) workspace.SecurityPaths { home, _ := os.UserHomeDir() homes := []string{filepath.Join(home, ".claude"), filepath.Join(home, ".codex")} for _, b := range bs { if b.ProviderHome != "" { homes = append(homes, b.ProviderHome) } else { homes = append(homes, b.CredentialHome) } } return workspace.SecurityPaths{RunnerState: c.StateDir, RunnerConfig: c.AccountBindingsPath, Journal: filepath.Join(c.StateDir, "journal.db"), Bearer: filepath.Join(c.StateDir, "credentials.json"), Cleanup: filepath.Join(c.StateDir, "credentials.json"), ForgeCredential: c.AmadanCredentialPath, LegacyHome: filepath.Join(home, ".paseo"), SSH: filepath.Join(home, ".ssh"), ProviderHomes: homes, PreviewCredential: c.PreviewCredentialPath} } func bindingPolicy(c Config, bs []AccountBinding, b AccountBinding) (provider.ReadPolicy, error) { s := c.securityPaths(bs) paths := append(s.Credentials(), s.ProviderHomes...) paths = append(paths, filepath.Join(c.StateDir, "private")) var denied []string for _, p := range paths { real, err := workspace.CanonicalSecurityPath(p, provider.ErrPolicy) if err != nil { return provider.ReadPolicy{}, err } denied = append(denied, p, real) } sort.Strings(denied) denied = slices.Compact(denied) for _, root := range []string{c.WorkspaceRoot, c.ScratchRoot} { for _, p := range denied { if containsPath(p, root) { return provider.ReadPolicy{}, provider.ErrPolicy } } } b.Model = "" b.Label = "" raw, _ := json.Marshal(struct { Binding AccountBinding Mode string Denied []string }{b, "default", denied}) sum := sha256.Sum256(raw) return provider.ReadPolicy{DeniedPaths: denied, ConfigHash: hex.EncodeToString(sum[:])}, nil } // DirectAccount preserves the configured person's LocalID and model order. // It contains no credentials and does not grant dispatch authority. type DirectAccount struct { Binding AccountBinding Identity provider.Identity Available bool Failure error } func loadDirectBindings(path string) ([]AccountBinding, error) { var bs []AccountBinding if e := privateJSON(path, &bs); e != nil { if os.IsNotExist(e) { return nil, nil } return nil, e } if len(bs) > 256 { return nil, provider.ErrIdentity } seen := map[string]bool{} accounts := map[string]AccountBinding{} homes := map[string]AccountBinding{} homeIDs := map[string]AccountBinding{} accountIDs := map[string]AccountBinding{} rules := provider.DefaultHomeRules() for _, b := range bs { key := b.LocalID + "/" + b.Model if b.ID == "" || b.LocalID == "" || b.Model == "" || b.AccountID == "" || b.ExpectedIdentity.AccountID != b.AccountID || b.ExpectedIdentity.HomeID == "" || b.ExpectedIdentity.ProviderVersion == "" || (provider.SubscriptionFamily(b.Provider) == "" || b.Family != provider.SubscriptionFamily(b.Provider)) || !(b.Kind == "subscription" || b.Kind == "api" || b.Kind == "token" && b.Provider == "claude") || rules.RefusesPath(b.ProviderHome) || seen[key] || b.CredentialHome != "" && b.CredentialHome != b.ProviderHome { return nil, provider.ErrIdentity } if prior, ok := accounts[b.LocalID]; ok && (prior.ID != b.ID || prior.ProviderHome != b.ProviderHome || prior.AccountID != b.AccountID || prior.PersonID != b.PersonID || prior.ExpectedIdentity != b.ExpectedIdentity) { return nil, provider.ErrIdentity } for _, prior := range []AccountBinding{homes[b.ProviderHome], homeIDs[b.ExpectedIdentity.HomeID], accountIDs[b.Provider+"/"+b.AccountID]} { if prior.LocalID != "" && (prior.LocalID != b.LocalID || prior.PersonID != b.PersonID) { return nil, provider.ErrIdentity } } homes[b.ProviderHome] = b homeIDs[b.ExpectedIdentity.HomeID] = b accountIDs[b.Provider+"/"+b.AccountID] = b seen[key] = true accounts[b.LocalID] = b } return bs, nil } // DiscoverDirect uses only configured dedicated homes. Adapters supply // a no-prompt account-status probe. The dispatch path must open a fresh lease. func DiscoverDirect(ctx context.Context, c Config, probe func(context.Context, provider.Binding) (provider.Identity, error)) ([]DirectAccount, error) { return discoverDirect(ctx, c, func(ctx context.Context, b provider.Binding) (provider.Identity, error) { l, e := provider.OpenAccountRefreshing(ctx, b, probe) if e != nil { return provider.Identity{}, e } defer l.Close() return l.Identity(), nil }) } func discoverDirect(ctx context.Context, c Config, check func(context.Context, provider.Binding) (provider.Identity, error)) ([]DirectAccount, error) { bs, e := loadDirectBindings(c.AccountBindingsPath) if e != nil { return nil, e } reports := make([]DirectAccount, 0, len(bs)) for _, b := range bs { if e = ctx.Err(); e != nil { return nil, e } binding, e := c.DirectBinding(b) if e != nil { return nil, e } id, e := check(ctx, binding) reports = append(reports, DirectAccount{Binding: b, Identity: id, Available: e == nil && id == b.ExpectedIdentity, Failure: e}) } return reports, nil }