codexproxy

package
v0.29.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 28 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codexproxy is ccdad's loopback reverse proxy for codex.

This file is the one piece of it the daemon's Codex lane needs before the proxy itself exists: the rate-limit record the two share.

The stable port: resolved once per start, and recorded as what was bound.

This comment describes the FILE and not the package. `internal/codexproxy` already has a package comment -- Part 3 landed `limitbook.go` here first and it carries one -- and a second one in this file would be a second package doc for the same package.

Index

Constants

View Source
const (
	// HealthPath is the one unauthenticated route. A launcher proves the
	// LISTENER rather than the daemon process with it, because holding the
	// daemon singleton is not evidence that a proxy is up.
	HealthPath = "/ccdad/health"
	// ResponsesPath is the only route codex uses, and the only one that
	// forwards anything.
	ResponsesPath = "/responses"
	// DefaultUpstream is where a forwarded request goes.
	DefaultUpstream = "https://chatgpt.com/backend-api/codex/responses"
	// DefaultMaxBody is used when Config.MaxBodyBytes is omitted.
	DefaultMaxBody = config.DefaultCodexMaxBodyMiB << 20
	// MaxUnauthenticated caps how many bearers may be checked at once. The
	// check is a stat and a try-lock, so this is not a throughput limit; it is
	// what stops an unauthenticated flood from turning into filesystem work.
	MaxUnauthenticated = 16
)

Variables

This section is empty.

Functions

func PortPath added in v0.13.0

func PortPath(root string) string

PortPath is where the live port is recorded.

func RecordPort added in v0.13.0

func RecordPort(root string, port int) error

RecordPort writes the port the daemon actually bound.

It refuses anything outside 1-65535, and 0 above all: 0 is what a caller passes to ASK for a port, and a store that remembered it would ask the kernel for a fresh random port on every restart -- which strands every codex session started before the restart on a port nothing answers.

func ResolvePort added in v0.13.0

func ResolvePort(root string, configured int) (int, string, error)

ResolvePort decides which port to try, and says where the answer came from.

The source is not decoration: a bind failure on a port the USER configured is a refusal the daemon must fail on, and a bind failure on a port ccdad chose for itself is a fallback.

Types

type Config added in v0.13.0

type Config struct {
	// Root is the ccdad store home.
	Root string
	// Port is the port to bind, from ResolvePort.
	Port int
	// PortSource is where Port came from: "config", "recorded" or "derived". A
	// bind failure is fatal for "config" and a fallback otherwise.
	PortSource string
	// Version is what the health route reports.
	Version string
	// MaxBodyBytes caps request buffering. Zero uses DefaultMaxBody.
	MaxBodyBytes int64
	// Upstream is the URL every forwarded request goes to. Empty means
	// DefaultUpstream.
	Upstream string
	// Client makes the upstream request. Nil means this package's own.
	Client *http.Client
	// Refresher is the daemon's ONE Codex refresher. Nil means a 401 is passed
	// through rather than repaired.
	Refresher *codexauth.Refresher
	// RefreshFunc overrides Refresher. It exists so a test can describe an
	// outcome without a store and a token endpoint, and production leaves it
	// nil -- there must be exactly one refresher per daemon, and this is not a
	// second place to build one.
	RefreshFunc func(ctx context.Context, uuid, triggeredBy string) (codexauth.Outcome, error)
	// Accounts reads the account rows.
	Accounts func() ([]store.Account, error)
	// Credentials reads one account's credential blob.
	Credentials func(uuid string) (cclink.Blob, error)
	// RankedEligible is the lane's last ranking, best first. Nil or empty means
	// the proxy derives an order from the store itself, which is what a daemon
	// whose lane has not run yet has.
	RankedEligible func() []string
	// CrossAccountReplay allows a thread that already has responses from one
	// account to be replayed onto another.
	CrossAccountReplay bool
	// Book is the rate-limit bookkeeping shared with the lane. Nil means the
	// server keeps its own.
	Book *LimitBook
	// Lookup validates a launch bearer. Nil means codexlaunch.Lookup.
	Lookup func(root, bearer string) (codexlaunch.Record, codexlaunch.LookupResult, error)
	// Harvest is handed a usage reading taken off a real inference response.
	// Nil means the readings are dropped.
	Harvest func(uuid string, snap *usage.Snapshot)
	// Now is the clock. Nil means time.Now.
	Now func() time.Time
	// Log records what the proxy decided. It never receives a bearer, an
	// upstream Authorization, a body or the turn metadata header.
	Log func(format string, a ...any)
}

Config is everything the proxy needs and nothing it can resolve itself.

Accounts, Credentials, RankedEligible and Refresher are funcs rather than concrete types for the reason the daemon engine's poller seams are: the daemon hands in the store under its own lock, and a test describes the same behaviour without one.

type LimitBook

type LimitBook struct {
	// contains filtered or unexported fields
}

LimitBook is what ccdad knows about which Codex accounts are currently throttled, shared by the daemon's Codex lane and the proxy handler.

It is in-memory and deliberately not persisted. A 429 from chatgpt.com is a fact about a window that is measured in minutes to hours, and a daemon that restarts has to re-learn it from the endpoint anyway -- whereas a persisted record would outlive the throttle it describes and hold an account out on evidence nobody could re-check.

The zero value is usable, and so is a nil pointer: a nil book answers "no limit" to everything, which is what an attended `ccdad switch` -- a process with no proxy in it -- has to be able to hand in.

func (*LimitBook) LimitedUntil

func (b *LimitBook) LimitedUntil(uuid string, now time.Time) (time.Time, bool)

LimitedUntil is when an account's throttle lifts, and whether one is in force at all right now.

A record whose instant has passed answers no rather than being swept: there is no sweeper, and expiring on read is what keeps a book that nobody prunes from holding an account out forever on one 429.

func (*LimitBook) MarkLimited

func (b *LimitBook) MarkLimited(uuid string, until time.Time)

MarkLimited records a deadline the endpoint stated.

func (*LimitBook) MarkLimitedFor

func (b *LimitBook) MarkLimitedFor(uuid string, until time.Time, resetKnown bool)

MarkLimitedFor records a deadline and whether the endpoint stated it.

The LATER instant always wins. Two writers reach this -- the lane's poll, which learns a limit from /wham/usage, and the proxy, which learns one from a 429 on a real turn -- and the two see different Retry-After values for the same window. Taking the shorter one would send the next request straight back into the throttle.

A nil book records nothing, which is the whole of what a process with no proxy in it needs from this type.

func (*LimitBook) ResetsAt

func (b *LimitBook) ResetsAt(uuid string, now time.Time) (time.Time, bool)

ResetsAt is LimitedUntil restricted to deadlines the endpoint stated, and it is the only one an answer to codex may quote.

type Server added in v0.13.0

type Server struct {
	// contains filtered or unexported fields
}

Server is the bound proxy.

func New added in v0.13.0

func New(cfg Config) (*Server, error)

New binds the listener.

A bind failure on a port the USER configured is a refusal: silently serving on a different one would leave every codex session pointed at a port nothing answers, and codex's symptom for that is an endless reconnect rather than an error. A bind failure on a port ccdad chose for itself falls back, because the alternative is a daemon that will not start.

func (*Server) Close added in v0.13.0

func (s *Server) Close() error

Close releases the listener without serving. Serve does this itself; this is for a caller that bound and then decided not to run.

func (*Server) FellBack added in v0.13.0

func (s *Server) FellBack() bool

FellBack reports that the listener is not on the port that was asked for, so codex sessions started before this daemon must be relaunched.

func (*Server) Handler added in v0.13.0

func (s *Server) Handler() http.Handler

Handler is the routing table, wrapped in the never-500 guard.

func (*Server) Port added in v0.13.0

func (s *Server) Port() int

Port is the port the listener actually took.

func (*Server) Serve added in v0.13.0

func (s *Server) Serve(ctx context.Context) error

Serve runs until ctx is done and drains what is in flight.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL