dedupe resolveCachedCores. · Entire

dedupe resolveCachedCores.

f612abd→main·

toothbrush·1mo ago·2 files·+54 added/-58 removed

Sessions

ba57be5b7bdbView transcript

[?
Audit COR-389 Auth Paths and Token ResolutionClaude Code·Opus 4.8[1m]·1 step](/content/gh/entireio/cli/session/0b211c0d-b6ab-4c61-a033-ffd7a0d2103d#timeline-ba57be5b7bdb/index.html)

Changes

2

61 unmodified lines

// resolveAPICores returns apiHost's trusted issuer URLs, from
// api_discovery.json when fresh, otherwise via a live
// /.well-known/entire-api.json fetch (which is then cached). A stale-but-present
// cache entry is used as a fallback when the live fetch fails, so a brief outage
// doesn't break a command whose trust roots we already knew. Mirrors
// resolveClusterCores exactly — the data-API trusted issuers ARE core URLs, so
// they share the cores cache (different file). Cold failures stay folded under
// /.well-known/entire-api.json fetch (cached, with stale fallback on failure).
// Shares the cache-then-discover logic with resolveClusterCores via
// resolveCachedCores — the data-API trusted issuers ARE core URLs, so they
// reuse the cores cache (different file). Cold failures stay folded under
// ErrDiscoveryUnavailable (from DiscoverAPI) for the caller's static fallback.
func resolveAPICores(ctx context.Context, cacheDir, apiHost string, httpClient *http.Client, debugf DebugFunc) ([]string, error) {
    cache, err := discovery.LoadAPICores(cacheDir)
    if err != nil {
        // A cache read problem must not block resolution — discover live.
        debugf("api-discovery cache load failed: %v; discovering live", err)
        cache = nil
    }

var stale []string
    if cache != nil {
        if urls, fresh, ok := cache.Get(apiHost); ok {
            if fresh {
                debugf("api host %s trusted issuers from cache: %v", apiHost, urls)
                return urls, nil
            }
            stale = urls
            debugf("api host %s trusted-issuers cache expired; re-fetching %s", apiHost, APIPath)
        }
    }

body, err := DiscoverAPI(ctx, apiHost, httpClient, debugf)
    if err != nil {
        if stale != nil {
            debugf("api discovery for %s failed (%v); falling back to stale cached trusted issuers %v", apiHost, err, stale)
            return stale, nil
        }
        return nil, err
    }

if mErr := discovery.ModifyAPICores(cacheDir, func(c discovery.ClusterCoresCache) error {
        c.Set(apiHost, body.TrustedIssuers)
        return nil
    }); mErr != nil {
        // Non-fatal: we resolved the issuers, the next command just re-fetches.
        debugf("api-discovery cache write for %s failed: %v", apiHost, mErr)
    }
    return body.TrustedIssuers, nil
}

Minternal/entireclient/clusterdiscovery/api_discovery.go+12/-40

79 unmodified lines

// resolveClusterCores returns the control-plane core URLs that front
// clusterHost, from cluster_cores.json when fresh, otherwise via a live
// /.well-known fetch (which is then cached). A stale-but-present cache
// entry is used as a fallback when the live fetch fails, so a brief
// cluster outage doesn't break an operation whose cores we already knew.
func resolveClusterCores(ctx context.Context, cacheDir, clusterHost string, httpClient *http.Client, debugf DebugFunc) ([]string, error) {
    cache, err := discovery.LoadClusterCores(cacheDir)

// resolveCachedCores is the shared cache-then-/.well-known resolution behind
    // both resolveClusterCores (git clusters) and resolveAPICores (data APIs):
    // read the host→cores cache, return it when fresh, otherwise discover live and
    // rewrite the cache. A stale-but-present entry is used as a fallback when the
    // live fetch fails, so a brief outage doesn't break a host whose cores we
    // already knew. load/modify select the cache file; discover wraps the
    // host-specific /.well-known fetch (and any host-specific error formatting);
    // label names the resource in debug output ("cluster" / "api host").
    func resolveCachedCores(
        cacheDir, host, label string,
        load func(string) (discovery.ClusterCoresCache, error),
        modify func(string, func(discovery.ClusterCoresCache) error) error,
        discover func() ([]string, error),
        debugf DebugFunc,
    ) ([]string, error) {
        cache, err := load(cacheDir)
        if err != nil {
            // A cache read problem must not block resolution — discover live.
            debugf("cluster-cores cache load failed: %v; discovering live", err)
            debugf("%s cache load failed: %v; discovering live", label, err)
            cache = nil
        }

var stale []string
        if cache != nil {
            if urls, fresh, ok := cache.Get(clusterHost); ok {
                if fresh {
                    debugf("cluster %s cores from cache: %v", clusterHost, urls)
                    debugf("%s %s cores from cache: %v", label, host, urls)
                    return urls, nil
                }
                stale = urls
                debugf("cluster %s cores cache expired; re-fetching /.well-known", clusterHost)
                debugf("%s %s cores cache expired; re-fetching /.well-known", label, host)
            }
        }

body, err := Discover(ctx, clusterHost, httpClient, debugf)
        cores, err := discover()
        if err != nil {
            if stale != nil {
                debugf("discovery for %s failed (%v); falling back to stale cached cores %v", clusterHost, err, stale)
                debugf("%s discovery for %s failed (%v); falling back to stale cached cores %v", label, host, err, stale)
                return stale, nil
            }
            return nil, formatDiscoveryError(clusterHost, err)
        }

if mErr := discovery.ModifyClusterCores(cacheDir, func(c discovery.ClusterCoresCache) error {
            c.Set(clusterHost, body.CoreURLs)
            return nil
        }); mErr != nil {
            // Non-fatal: we resolved the cores, the next call just re-fetches.
            debugf("cluster-cores cache write for %s failed: %v", clusterHost, mErr)
            debugf("%s cache write for %s failed: %v", label, host, mErr)
        }
        return body.CoreURLs, nil
}