Documentation
¶
Overview ¶
Package store owns Codex Switch's shared domain types and one SQLite database.
It persists routing facts and the responses the relay completed: no credentials, allowance snapshots, rates, provider abstraction, migrations, retries, or response content. It imports no other Codex Switch package; the usage package keeps its own tables in the same database. SQLite is provided by modernc.org/sqlite so the proxy stays a single CGO-free binary.
Index ¶
- func StoredAccountKey(table string) string
- func StreamCompletedResponses(ctx context.Context, query ResponseQuery, afterRowID, throughRowID int64, ...) error
- type AccountAllowanceWindowIdentity
- type AccountIdentity
- type AccountKey
- type CompletedResponseID
- type CompletedResponseRecord
- type ModelRequestCounts
- type ModelRequestOutcome
- type ProviderAccountID
- type ResponseQuery
- type StoreDatabase
- func (database *StoreDatabase) ClearAccountExhaustionBefore(accountKey AccountKey, observedAt time.Time) (bool, error)
- func (database *StoreDatabase) CloseStoreDatabase() error
- func (database *StoreDatabase) CompareAndSwapTaskAccountPin(taskID TaskID, servedAccountKey AccountKey, nextAccountKey AccountKey, ...) (bool, error)
- func (database *StoreDatabase) FindAccountForAllowanceWindow(durationSeconds int64, resetsAt, eventAt time.Time) (ProviderAccountID, bool, error)
- func (database *StoreDatabase) HasReadOnlyWeeklyAllowanceReading(accountKey AccountKey) bool
- func (database *StoreDatabase) NextAccountNumber() (int, error)
- func (database *StoreDatabase) PinTaskAccountIfAbsent(candidate TaskAccountPin) (TaskAccountPin, error)
- func (database *StoreDatabase) ReadAccountIdentities() ([]AccountIdentity, error)
- func (database *StoreDatabase) ReadAccountReportedReset(accountKey AccountKey) (time.Time, bool, error)
- func (database *StoreDatabase) ReadModelRequestCounts() (time.Time, []ModelRequestCounts, error)
- func (database *StoreDatabase) ReadPromptCacheParent(taskID TaskID, prefixes []string) (TaskID, error)
- func (database *StoreDatabase) ReadTaskAccountPin(taskID TaskID) (TaskAccountPin, bool, error)
- func (database *StoreDatabase) ReadWeeklyAllowanceBurn(accountKeys []AccountKey, now time.Time) (*float64, error)
- func (database *StoreDatabase) RecordAccountAllowanceWindowIdentity(identity AccountAllowanceWindowIdentity) error
- func (database *StoreDatabase) RecordAccountReportedReset(accountKey AccountKey, resetsAt, observedAt time.Time) error
- func (database *StoreDatabase) RecordCompletedResponse(record CompletedResponseRecord) error
- func (database *StoreDatabase) RecordModelRequestOutcome(account AccountKey, admittedAt time.Time, outcome ModelRequestOutcome) error
- func (database *StoreDatabase) RecordWeeklyAllowanceReading(reading WeeklyAllowanceReading) error
- func (database *StoreDatabase) RememberAccountIdentity(identity AccountIdentity) error
- func (database *StoreDatabase) RememberPromptCachePrefix(taskID TaskID, prefix string) error
- func (database *StoreDatabase) RemoveAccountRoutingState(accountKey AccountKey) (int64, error)
- func (database *StoreDatabase) SQLDatabase() *sql.DB
- func (database *StoreDatabase) StartModelRequestObservation(now time.Time) error
- func (database *StoreDatabase) SyncAccountKeys() (int64, error)
- type TaskAccountPin
- type TaskID
- type WeeklyAllowanceReading
- type WorkKind
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StoredAccountKey ¶ added in v0.1.10
StoredAccountKey is the SQL for the account key of a row of table: the key recorded for the row's folder. The folder decides, because builds before account keys know an account only by its folder and may still write: while an update switches proxies, or after deploy.sh back. Such a build moves a task's pin by changing its folder and leaves the row's key as it was. The row's own key counts only where its folder has no recorded key, as in a row copied from another Mac; a row with neither keeps its folder name, which matches no signed-in account.
func StreamCompletedResponses ¶ added in v0.1.1
func StreamCompletedResponses(ctx context.Context, query ResponseQuery, afterRowID, throughRowID int64, start, end time.Time, visit func(int64, CompletedResponseRecord) error) error
StreamCompletedResponses reads each newly appended row without retaining historical responses. Bounds come from the caller's read transaction.
Types ¶
type AccountAllowanceWindowIdentity ¶
type AccountAllowanceWindowIdentity struct {
ProviderAccountID ProviderAccountID
DurationSeconds int64
ResetsAt time.Time
ObservedAt time.Time
UsedPercent float64
}
AccountAllowanceWindowIdentity records an explicit provider reset from an authenticated allowance read after the account has used that window.
type AccountIdentity ¶
type AccountIdentity struct {
AccountFolder string
AccountKey AccountKey
ProviderAccountID string
Email string
}
AccountIdentity records which folder in the data directory holds which account. It outlives the account's removal from the pool, so records written with only the folder name still find their account key.
type AccountKey ¶ added in v0.1.10
type AccountKey string
AccountKey identifies a subscription account by its provider and email, such as "chatgpt:you@example.com", so every Mac records an account under the same key.
type CompletedResponseID ¶
type CompletedResponseID string
CompletedResponseID is the provider identity used to record one completion exactly once.
type CompletedResponseRecord ¶
type CompletedResponseRecord struct {
CompletedResponseID CompletedResponseID
TaskID TaskID
AccountKey AccountKey
ModelSlug string
HarnessMode string
InputTokens int64
CachedInputTokens int64
CacheWriteInputTokens *int64
CostNanoUSD *int64
OutputTokens int64
ReasoningTokens int64
CompletedAt time.Time
}
CompletedResponseRecord excludes response content because accounting needs token facts only.
type ModelRequestCounts ¶
type ModelRequestCounts struct {
AccountKey AccountKey
Requests int64
Successful int64
Failed int64
Interrupted int64
}
type ModelRequestOutcome ¶
type ModelRequestOutcome int
const ( ModelRequestStarted ModelRequestOutcome = iota ModelRequestSuccessful ModelRequestFailed ModelRequestInterrupted )
type ProviderAccountID ¶
type ProviderAccountID string
type ResponseQuery ¶ added in v0.1.1
ResponseQuery permits callers to include responses in their own read snapshot.
type StoreDatabase ¶
type StoreDatabase struct {
// contains filtered or unexported fields
}
StoreDatabase is shared by old and new proxy generations during a live drain.
func OpenStoreDatabase ¶
func OpenStoreDatabase(databasePath string) (*StoreDatabase, error)
OpenStoreDatabase enables WAL before exposing the database to concurrent proxy generations.
func (*StoreDatabase) ClearAccountExhaustionBefore ¶
func (database *StoreDatabase) ClearAccountExhaustionBefore(accountKey AccountKey, observedAt time.Time) (bool, error)
ClearAccountExhaustionBefore lets a successful later provider read restore capacity. It reports whether it cleared a recorded exhaustion.
func (*StoreDatabase) CloseStoreDatabase ¶
func (database *StoreDatabase) CloseStoreDatabase() error
CloseStoreDatabase releases only this process's connection and never affects another generation.
func (*StoreDatabase) CompareAndSwapTaskAccountPin ¶
func (database *StoreDatabase) CompareAndSwapTaskAccountPin(taskID TaskID, servedAccountKey AccountKey, nextAccountKey AccountKey, movedAt time.Time) (bool, error)
CompareAndSwapTaskAccountPin makes a concurrent exhaustion report a no-op after another process moves the pin.
func (*StoreDatabase) FindAccountForAllowanceWindow ¶
func (database *StoreDatabase) FindAccountForAllowanceWindow(durationSeconds int64, resetsAt, eventAt time.Time) (ProviderAccountID, bool, error)
FindAccountForAllowanceWindow identifies only events within a window that has exactly one authenticated owner. Colliding resets do not establish ownership.
func (*StoreDatabase) HasReadOnlyWeeklyAllowanceReading ¶ added in v0.1.15
func (database *StoreDatabase) HasReadOnlyWeeklyAllowanceReading(accountKey AccountKey) bool
HasReadOnlyWeeklyAllowanceReading reports whether this database has ever observed a weekly reading for the given folderless read-only account.
func (*StoreDatabase) NextAccountNumber ¶
func (database *StoreDatabase) NextAccountNumber() (int, error)
NextAccountNumber is called under the pool mutation lock, after remembering existing accounts.
func (*StoreDatabase) PinTaskAccountIfAbsent ¶
func (database *StoreDatabase) PinTaskAccountIfAbsent(candidate TaskAccountPin) (TaskAccountPin, error)
PinTaskAccountIfAbsent returns the winner when two proxy generations first see the same task.
func (*StoreDatabase) ReadAccountIdentities ¶
func (database *StoreDatabase) ReadAccountIdentities() ([]AccountIdentity, error)
func (*StoreDatabase) ReadAccountReportedReset ¶
func (database *StoreDatabase) ReadAccountReportedReset(accountKey AccountKey) (time.Time, bool, error)
ReadAccountReportedReset adjusts provider snapshots with later exhaustion evidence.
func (*StoreDatabase) ReadModelRequestCounts ¶
func (database *StoreDatabase) ReadModelRequestCounts() (time.Time, []ModelRequestCounts, error)
func (*StoreDatabase) ReadPromptCacheParent ¶
func (database *StoreDatabase) ReadPromptCacheParent(taskID TaskID, prefixes []string) (TaskID, error)
ReadPromptCacheParent looks up a matching reusable prefix only for an unpinned session. The index expires with Anthropic's longest cache lifetime.
func (*StoreDatabase) ReadTaskAccountPin ¶
func (database *StoreDatabase) ReadTaskAccountPin(taskID TaskID) (TaskAccountPin, bool, error)
ReadTaskAccountPin reads current state on every turn so repairs take effect without restart.
func (*StoreDatabase) ReadWeeklyAllowanceBurn ¶ added in v0.1.1
func (database *StoreDatabase) ReadWeeklyAllowanceBurn(accountKeys []AccountKey, now time.Time) (*float64, error)
ReadWeeklyAllowanceBurn counts increases at their observation time. The first reading of an account is a baseline; the first reading after a reset is usage. Until a reading is added or ages out, only the observed coverage changes.
func (*StoreDatabase) RecordAccountAllowanceWindowIdentity ¶
func (database *StoreDatabase) RecordAccountAllowanceWindowIdentity(identity AccountAllowanceWindowIdentity) error
func (*StoreDatabase) RecordAccountReportedReset ¶
func (database *StoreDatabase) RecordAccountReportedReset(accountKey AccountKey, resetsAt, observedAt time.Time) error
RecordAccountReportedReset preserves account-wide provider exhaustion independently of any task pin.
func (*StoreDatabase) RecordCompletedResponse ¶
func (database *StoreDatabase) RecordCompletedResponse(record CompletedResponseRecord) error
RecordCompletedResponse preserves directly observed inference usage once per response.
func (*StoreDatabase) RecordModelRequestOutcome ¶
func (database *StoreDatabase) RecordModelRequestOutcome(account AccountKey, admittedAt time.Time, outcome ModelRequestOutcome) error
func (*StoreDatabase) RecordWeeklyAllowanceReading ¶ added in v0.1.1
func (database *StoreDatabase) RecordWeeklyAllowanceReading(reading WeeklyAllowanceReading) error
func (*StoreDatabase) RememberAccountIdentity ¶
func (database *StoreDatabase) RememberAccountIdentity(identity AccountIdentity) error
func (*StoreDatabase) RememberPromptCachePrefix ¶
func (database *StoreDatabase) RememberPromptCachePrefix(taskID TaskID, prefix string) error
func (*StoreDatabase) RemoveAccountRoutingState ¶
func (database *StoreDatabase) RemoveAccountRoutingState(accountKey AccountKey) (int64, error)
RemoveAccountRoutingState retires routing state while preserving account identity and completed usage.
func (*StoreDatabase) SQLDatabase ¶
func (database *StoreDatabase) SQLDatabase() *sql.DB
SQLDatabase shares this process's one connection with a module that owns its own tables, so its writes queue behind the same connection as routing writes.
func (*StoreDatabase) StartModelRequestObservation ¶
func (database *StoreDatabase) StartModelRequestObservation(now time.Time) error
func (*StoreDatabase) SyncAccountKeys ¶ added in v0.1.10
func (database *StoreDatabase) SyncAccountKeys() (int64, error)
SyncAccountKeys sets the account key on each row to the key recorded for its folder, so the column agrees with StoredAccountKey for anyone who reads the table directly or combines it with another Mac's. A build before account keys writes rows without a key, and moves pins without changing theirs. It returns how many rows it changed.
type TaskAccountPin ¶
type TaskAccountPin struct {
TaskID TaskID
AccountKey AccountKey
PinnedAt time.Time
}
TaskAccountPin is durable so draining proxy generations agree on the next account.
type TaskID ¶
type TaskID string
TaskID makes the affinity key impossible to swap with another string identifier.