package thin import ( "context" "encoding/json" "errors" "time" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/connect" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/wire" ) // errNoReading: the app-server answered, but with no rate limits Equipo // can read. var errNoReading = errors.New("no reading") // CodexReading is the production reading probe (connectors spec rev 3.2 // ยง6, the controller's ruling #7): a Codex subscription's headroom is // refreshed with the app-server's account/rateLimits/read, run as the // hourly refresh runs (the source home under its lock, the login // probe's environment, no namespace and no egress relay, never a thread // or a turn). Claude OAuth usage is read separately by the status check. func CodexReading(c runner.Config, read func(context.Context, provider.Binding) (provider.Identity, json.RawMessage, error), wait time.Duration) func(context.Context, runner.AccountBinding) (*connect.Limit, error) { if read == nil { options := provider.LoginOptions{ClaudePath: c.ClaudePath, CodexPath: c.CodexPath} read = func(ctx context.Context, b provider.Binding) (provider.Identity, json.RawMessage, error) { return provider.ReadCodexLimits(ctx, b, options) } } return func(ctx context.Context, b runner.AccountBinding) (*connect.Limit, error) { if b.Provider != wire.ProviderCodex || b.Kind == wire.KindToken || b.Kind == wire.KindAPIKey { return nil, nil } direct, err := c.DirectBinding(b) if err != nil { return nil, err } var raw json.RawMessage lease, err := provider.OpenRuntimeCopy(ctx, direct, func(ctx context.Context, pb provider.Binding) (provider.Identity, error) { id, limits, err := read(ctx, pb) raw = limits return id, err }, wait) if lease != nil { lease.Close() } if err != nil { return nil, err } l, _, ok := connect.CodexRateLimits(raw) if !ok { return nil, errNoReading } return &l, nil } }