@namzu/sdk 58.1.1 → 59.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/dist/account/binding.d.ts +20 -0
- package/dist/account/binding.d.ts.map +1 -0
- package/dist/account/binding.js +27 -0
- package/dist/account/binding.js.map +1 -0
- package/dist/account/index.d.ts +9 -0
- package/dist/account/index.d.ts.map +1 -0
- package/dist/account/index.js +5 -0
- package/dist/account/index.js.map +1 -0
- package/dist/account/lock.d.ts +33 -0
- package/dist/account/lock.d.ts.map +1 -0
- package/dist/account/lock.js +170 -0
- package/dist/account/lock.js.map +1 -0
- package/dist/account/registry.d.ts +21 -0
- package/dist/account/registry.d.ts.map +1 -0
- package/dist/account/registry.js +340 -0
- package/dist/account/registry.js.map +1 -0
- package/dist/account/types.d.ts +124 -0
- package/dist/account/types.d.ts.map +1 -0
- package/dist/account/types.js +35 -0
- package/dist/account/types.js.map +1 -0
- package/dist/pals/communication/ingress-schema.d.ts +38 -38
- package/dist/peers/record.d.ts +98 -0
- package/dist/peers/record.d.ts.map +1 -1
- package/dist/peers/record.js +17 -0
- package/dist/peers/record.js.map +1 -1
- package/dist/peers/registry.d.ts.map +1 -1
- package/dist/peers/registry.js +2 -2
- package/dist/peers/registry.js.map +1 -1
- package/dist/provider/errors.d.ts.map +1 -1
- package/dist/provider/errors.js +25 -2
- package/dist/provider/errors.js.map +1 -1
- package/dist/provider/token-budget.d.ts.map +1 -1
- package/dist/provider/token-budget.js +9 -1
- package/dist/provider/token-budget.js.map +1 -1
- package/dist/public-runtime.d.ts +1 -0
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +1 -0
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-types.d.ts +2 -0
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/query/executor.d.ts +7 -2
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +10 -2
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/types/errors/catalog.d.ts.map +1 -1
- package/dist/types/errors/catalog.js +6 -0
- package/dist/types/errors/catalog.js.map +1 -1
- package/dist/types/provider/errors.d.ts +7 -0
- package/dist/types/provider/errors.d.ts.map +1 -1
- package/dist/types/provider/errors.js +27 -3
- package/dist/types/provider/errors.js.map +1 -1
- package/dist/types/session/records.d.ts +215 -1
- package/dist/types/session/records.d.ts.map +1 -1
- package/dist/types/session/records.js +13 -0
- package/dist/types/session/records.js.map +1 -1
- package/package.json +1 -1
- package/src/account/binding.ts +43 -0
- package/src/account/index.ts +32 -0
- package/src/account/lock.ts +184 -0
- package/src/account/registry.ts +413 -0
- package/src/account/types.ts +137 -0
- package/src/peers/record.ts +18 -0
- package/src/peers/registry.ts +2 -2
- package/src/provider/errors.ts +27 -1
- package/src/provider/token-budget.ts +9 -3
- package/src/public-runtime.ts +17 -0
- package/src/public-types.ts +15 -0
- package/src/runtime/query/executor.ts +10 -2
- package/src/types/errors/catalog.ts +6 -0
- package/src/types/provider/errors.ts +40 -3
- package/src/types/session/records.ts +16 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accounts: more than one credential for one provider, chosen per session.
|
|
3
|
+
*
|
|
4
|
+
* An account is a provider plus a label. Today's single sign-in for each
|
|
5
|
+
* provider is the account labelled `default`; it is read exactly as before and
|
|
6
|
+
* is never stored here. Further accounts are one of two kinds: a private
|
|
7
|
+
* Codex home directory that holds its own `auth.json`, or a labelled API key.
|
|
8
|
+
*
|
|
9
|
+
* @experimental
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** `<provider>:<label>`. The label is `[a-z0-9-]{1,32}`; `default` is reserved for today's sign-in. */
|
|
13
|
+
export type AccountId = string
|
|
14
|
+
|
|
15
|
+
/** The label of the account that wraps a provider's existing sign-in. */
|
|
16
|
+
export const DEFAULT_ACCOUNT_LABEL = 'default'
|
|
17
|
+
|
|
18
|
+
/** Where an account's credential comes from. */
|
|
19
|
+
export type AccountSource =
|
|
20
|
+
/** The provider's existing sign-in, read the way it was before accounts existed. Synthesised, never stored. */
|
|
21
|
+
| { readonly kind: 'existing-default' }
|
|
22
|
+
/** A private Codex home: `dir/auth.json` was written by `codex login` run with `CODEX_HOME=dir`. */
|
|
23
|
+
| { readonly kind: 'codex-home'; readonly dir: string }
|
|
24
|
+
/** A labelled API key. `keyRef` names a slot in the registry's secret map; the key itself is never in a log. */
|
|
25
|
+
| { readonly kind: 'api-key'; readonly keyRef: string }
|
|
26
|
+
|
|
27
|
+
export interface Account {
|
|
28
|
+
readonly id: AccountId
|
|
29
|
+
readonly provider: string
|
|
30
|
+
readonly label: string
|
|
31
|
+
readonly source: AccountSource
|
|
32
|
+
readonly createdAt: string
|
|
33
|
+
/** A short human tag such as the tail of the provider's account id. Never a token. */
|
|
34
|
+
readonly display?: string
|
|
35
|
+
/** API-key accounts only: the endpoint this key belongs to. A different endpoint is a different account. */
|
|
36
|
+
readonly baseUrl?: string
|
|
37
|
+
/** API-key accounts only: the model a session on this account starts with. */
|
|
38
|
+
readonly defaultModel?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What `add` accepts. The id, the creation time and the key slot are assigned by the registry. */
|
|
42
|
+
export type NewAccount =
|
|
43
|
+
| {
|
|
44
|
+
readonly provider: string
|
|
45
|
+
readonly label: string
|
|
46
|
+
readonly kind: 'api-key'
|
|
47
|
+
readonly apiKey: string
|
|
48
|
+
readonly display?: string
|
|
49
|
+
/** An http(s) endpoint without embedded credentials. */
|
|
50
|
+
readonly baseUrl?: string
|
|
51
|
+
readonly defaultModel?: string
|
|
52
|
+
}
|
|
53
|
+
| {
|
|
54
|
+
readonly provider: 'codex'
|
|
55
|
+
readonly label: string
|
|
56
|
+
readonly kind: 'codex-home'
|
|
57
|
+
/** Defaults to `<home>/accounts/codex/<label>`; must stay inside `<home>/accounts`. */
|
|
58
|
+
readonly dir?: string
|
|
59
|
+
readonly display?: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A credential ready to put on a request. Never logged. */
|
|
63
|
+
export interface ResolvedAccountCredential {
|
|
64
|
+
readonly kind: 'api-key' | 'oauth'
|
|
65
|
+
readonly accessToken: string
|
|
66
|
+
/** Set for an API-key account that names its own endpoint. */
|
|
67
|
+
readonly baseUrl?: string
|
|
68
|
+
/** Set for an API-key account that names a starting model. */
|
|
69
|
+
readonly defaultModel?: string
|
|
70
|
+
/** The provider-side account identity some drivers send as a header. */
|
|
71
|
+
readonly accountId?: string
|
|
72
|
+
/** Epoch milliseconds, when the credential expires. */
|
|
73
|
+
readonly expiresAt?: number
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The credential behind an account is gone or unusable; the caller should offer another account. */
|
|
77
|
+
export class CredentialWithdrawnError extends Error {
|
|
78
|
+
override readonly name = 'CredentialWithdrawnError'
|
|
79
|
+
constructor(
|
|
80
|
+
readonly accountId: AccountId,
|
|
81
|
+
message: string,
|
|
82
|
+
) {
|
|
83
|
+
super(message)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A registry mutation was refused: bad label, duplicate, unknown account, unreadable store. */
|
|
88
|
+
export class AccountRegistryError extends Error {
|
|
89
|
+
override readonly name = 'AccountRegistryError'
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Another owner holds the lock for a store or an account's credential file. */
|
|
93
|
+
export class AccountLockError extends Error {
|
|
94
|
+
override readonly name = 'AccountLockError'
|
|
95
|
+
constructor(
|
|
96
|
+
readonly lockPath: string,
|
|
97
|
+
message: string,
|
|
98
|
+
) {
|
|
99
|
+
super(message)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Source-specific readers. The registry owns the bookkeeping; the code that
|
|
105
|
+
* knows how a provider's sign-in is discovered or refreshed lives with the
|
|
106
|
+
* provider and is handed in here.
|
|
107
|
+
*/
|
|
108
|
+
export interface AccountCredentialReaders {
|
|
109
|
+
/** Read the provider's existing sign-in. Throws {@link CredentialWithdrawnError} when there is none. */
|
|
110
|
+
readonly existingDefault?: (
|
|
111
|
+
provider: string,
|
|
112
|
+
signal: AbortSignal,
|
|
113
|
+
) => Promise<ResolvedAccountCredential>
|
|
114
|
+
/**
|
|
115
|
+
* Read, and refresh when due, the credential in a private Codex home. An
|
|
116
|
+
* implementation holds `withAccountRefreshLock` across the read-refresh-write
|
|
117
|
+
* so two processes never spend one refresh token twice.
|
|
118
|
+
*/
|
|
119
|
+
readonly codexHome?: (
|
|
120
|
+
account: Account & { readonly source: { readonly kind: 'codex-home'; readonly dir: string } },
|
|
121
|
+
signal: AbortSignal,
|
|
122
|
+
) => Promise<ResolvedAccountCredential>
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface AccountRegistry {
|
|
126
|
+
/** Stored accounts plus the synthesised `default` of each known provider. Defaults first, then creation order. */
|
|
127
|
+
list(provider?: string): Account[]
|
|
128
|
+
get(id: AccountId): Account | undefined
|
|
129
|
+
add(account: NewAccount): Account
|
|
130
|
+
/** Change an account's label; its id changes with it. Sessions bound to the old id fall back to `default`. */
|
|
131
|
+
rename(id: AccountId, label: string): Account
|
|
132
|
+
/** Remove a stored account and its secret or private Codex directory. The `default` account cannot be removed. */
|
|
133
|
+
remove(id: AccountId): void
|
|
134
|
+
resolve(id: AccountId, signal: AbortSignal): Promise<ResolvedAccountCredential>
|
|
135
|
+
/** The next account of `provider` not in `excluding`, for the "continue with another account" picker. */
|
|
136
|
+
next(provider: string, excluding: readonly AccountId[]): Account | undefined
|
|
137
|
+
}
|
package/src/peers/record.ts
CHANGED
|
@@ -43,9 +43,27 @@ export const PeerRecordSchema = z
|
|
|
43
43
|
token: z.string().min(1),
|
|
44
44
|
protocol: z.literal(PEER_PROTOCOL_VERSION),
|
|
45
45
|
cliVersion: z.string(),
|
|
46
|
+
/** The slot a spaces host gave this terminal at launch; how the host finds its own sessions. */
|
|
47
|
+
hostSlot: z.string().min(1).max(128).optional(),
|
|
48
|
+
/** The conversation this terminal is on, once it has one. */
|
|
49
|
+
conversationId: z.string().min(1).optional(),
|
|
50
|
+
/** The label of the non-default account the conversation is bound to; absent for the default. */
|
|
51
|
+
account: z.string().min(1).optional(),
|
|
52
|
+
/** True while the bound account is out of allowance and the terminal is waiting for a choice. */
|
|
53
|
+
limited: z.boolean().optional(),
|
|
54
|
+
/** When the allowance is expected back (epoch ms); absent when the provider did not say. */
|
|
55
|
+
limitedUntil: z.number().int().positive().optional(),
|
|
46
56
|
})
|
|
47
57
|
.strict()
|
|
48
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The reader's view of a record: the same fields, but a key this build does
|
|
61
|
+
* not know is dropped instead of failing the whole record. A record written by
|
|
62
|
+
* a newer build (extra optional fields) stays visible to an older reader.
|
|
63
|
+
* Writers keep using the strict `PeerRecordSchema`.
|
|
64
|
+
*/
|
|
65
|
+
export const PeerRecordReaderSchema = PeerRecordSchema.strip()
|
|
66
|
+
|
|
49
67
|
export type PeerRecord = z.infer<typeof PeerRecordSchema>
|
|
50
68
|
|
|
51
69
|
const PEER_REF_HEX_LENGTH = 6
|
package/src/peers/registry.ts
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
import { basename, dirname, join } from 'node:path'
|
|
21
21
|
import { parsePeerAddress } from './address.js'
|
|
22
22
|
import { pingPeer } from './client.js'
|
|
23
|
-
import { type PeerRecord, PeerRecordSchema } from './record.js'
|
|
23
|
+
import { type PeerRecord, PeerRecordReaderSchema, PeerRecordSchema } from './record.js'
|
|
24
24
|
|
|
25
25
|
const RECORD_FILE_MODE = 0o600
|
|
26
26
|
|
|
@@ -75,7 +75,7 @@ function parseRecordText(text: string): PeerRecord | undefined {
|
|
|
75
75
|
} catch {
|
|
76
76
|
return undefined
|
|
77
77
|
}
|
|
78
|
-
const result =
|
|
78
|
+
const result = PeerRecordReaderSchema.safeParse(parsed)
|
|
79
79
|
return result.success ? result.data : undefined
|
|
80
80
|
}
|
|
81
81
|
|
package/src/provider/errors.ts
CHANGED
|
@@ -33,6 +33,7 @@ import { LOG_SECRET_PATTERNS } from '../constants/secret-patterns.js'
|
|
|
33
33
|
import type { ProviderErrorKind, ProviderRequestErrorInit } from '../types/provider/error.js'
|
|
34
34
|
import {
|
|
35
35
|
type TransportCause,
|
|
36
|
+
isUsageLimitSignal,
|
|
36
37
|
readCertificateFailure,
|
|
37
38
|
readTransportCause,
|
|
38
39
|
} from '../types/provider/errors.js'
|
|
@@ -130,6 +131,31 @@ function providerCode(value: unknown): string | undefined {
|
|
|
130
131
|
return safeProviderCode(record.code ?? nested)
|
|
131
132
|
}
|
|
132
133
|
|
|
134
|
+
/**
|
|
135
|
+
* A vendor can name a spent allowance in `type` instead of `code`
|
|
136
|
+
* (`{"error":{"type":"usage_limit_reached"}}`, `{"error":{"type":"billing_error"}}`).
|
|
137
|
+
* Only those identifiers are lifted to a provider code; any other `type` is a
|
|
138
|
+
* category and stays where it was.
|
|
139
|
+
*/
|
|
140
|
+
function usageLimitTypeOf(error: unknown): string | undefined {
|
|
141
|
+
if (typeof error !== 'object' || error === null) return undefined
|
|
142
|
+
const outer = error as { type?: unknown; error?: unknown }
|
|
143
|
+
const inner =
|
|
144
|
+
typeof outer.error === 'object' && outer.error !== null
|
|
145
|
+
? (outer.error as { type?: unknown; error?: unknown })
|
|
146
|
+
: undefined
|
|
147
|
+
const innermost =
|
|
148
|
+
typeof inner?.error === 'object' && inner.error !== null
|
|
149
|
+
? (inner.error as { type?: unknown })
|
|
150
|
+
: undefined
|
|
151
|
+
for (const candidate of [outer.type, inner?.type, innermost?.type]) {
|
|
152
|
+
if (typeof candidate === 'string' && isUsageLimitSignal(candidate, undefined)) {
|
|
153
|
+
return safeProviderCode(candidate)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return undefined
|
|
157
|
+
}
|
|
158
|
+
|
|
133
159
|
/**
|
|
134
160
|
* Credential shapes to scrub before a provider's words are kept.
|
|
135
161
|
*
|
|
@@ -524,7 +550,7 @@ export function providerVendorError(input: {
|
|
|
524
550
|
}): ProviderRequestError {
|
|
525
551
|
const { error } = input
|
|
526
552
|
const status = vendorErrorStatus(error)
|
|
527
|
-
const exactProviderCode = providerCode(error)
|
|
553
|
+
const exactProviderCode = providerCode(error) ?? usageLimitTypeOf(error)
|
|
528
554
|
const name = error instanceof Error ? error.name : ''
|
|
529
555
|
const message = error instanceof Error ? error.message : ''
|
|
530
556
|
|
|
@@ -29,9 +29,14 @@ function rejectedBeforeGeneration(error: unknown): boolean {
|
|
|
29
29
|
[400, 401, 403, 404, 413, 422, 429].includes(classified.status))
|
|
30
30
|
return (
|
|
31
31
|
explicitRejection &&
|
|
32
|
-
[
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
[
|
|
33
|
+
'auth',
|
|
34
|
+
'rate_limit',
|
|
35
|
+
'usage_limit',
|
|
36
|
+
'context_length_exceeded',
|
|
37
|
+
'invalid_request',
|
|
38
|
+
'not_found',
|
|
39
|
+
].includes(classified.code)
|
|
35
40
|
)
|
|
36
41
|
}
|
|
37
42
|
|
|
@@ -96,6 +101,7 @@ export function withTokenBudget(provider: LLMProvider, budget: SessionTokenBudge
|
|
|
96
101
|
![
|
|
97
102
|
'auth',
|
|
98
103
|
'rate_limit',
|
|
104
|
+
'usage_limit',
|
|
99
105
|
'context_length_exceeded',
|
|
100
106
|
'invalid_request',
|
|
101
107
|
'not_found',
|
package/src/public-runtime.ts
CHANGED
|
@@ -1869,6 +1869,23 @@ export {
|
|
|
1869
1869
|
udsPeerAddress,
|
|
1870
1870
|
writePeerRecord,
|
|
1871
1871
|
} from './peers/index.js'
|
|
1872
|
+
export {
|
|
1873
|
+
ACCOUNTS_FILE_NAME,
|
|
1874
|
+
ACCOUNTS_FILE_VERSION,
|
|
1875
|
+
AccountLockError,
|
|
1876
|
+
AccountRegistryError,
|
|
1877
|
+
CredentialWithdrawnError,
|
|
1878
|
+
DEFAULT_ACCOUNT_LABEL,
|
|
1879
|
+
accountBoundPayload,
|
|
1880
|
+
accountId,
|
|
1881
|
+
codexRefreshLockPath,
|
|
1882
|
+
createAccountRegistry,
|
|
1883
|
+
parseAccountId,
|
|
1884
|
+
resolveBoundAccount,
|
|
1885
|
+
withAccountLock,
|
|
1886
|
+
withAccountLockSync,
|
|
1887
|
+
withAccountRefreshLock,
|
|
1888
|
+
} from './account/index.js'
|
|
1872
1889
|
export { DiskPalStore, PalConflictError } from './pals/store.js'
|
|
1873
1890
|
export { PalRuntime, PalUnavailableError } from './pals/runtime.js'
|
|
1874
1891
|
export { buildPalSystemPrompt, palConversationGreeting } from './pals/prompt.js'
|
package/src/public-types.ts
CHANGED
|
@@ -800,6 +800,21 @@ export type {
|
|
|
800
800
|
ResolvePeerRuntimeDirOptions,
|
|
801
801
|
} from './peers/dir.js'
|
|
802
802
|
export type { PeerRecord } from './peers/record.js'
|
|
803
|
+
export type { AccountBoundRecord } from './types/session/records.js'
|
|
804
|
+
export type {
|
|
805
|
+
Account,
|
|
806
|
+
AccountBoundReason,
|
|
807
|
+
AccountCredentialReaders,
|
|
808
|
+
AccountId,
|
|
809
|
+
AccountLockOptions,
|
|
810
|
+
AccountLockSyncOptions,
|
|
811
|
+
AccountRegistry,
|
|
812
|
+
AccountRegistryOptions,
|
|
813
|
+
AccountSource,
|
|
814
|
+
BoundAccount,
|
|
815
|
+
NewAccount,
|
|
816
|
+
ResolvedAccountCredential,
|
|
817
|
+
} from './account/index.js'
|
|
803
818
|
export type {
|
|
804
819
|
ListLivePeersOptions,
|
|
805
820
|
PeerLivenessOptions,
|
|
@@ -13,6 +13,7 @@ import { buildProbeContext } from '../../probe/context.js'
|
|
|
13
13
|
import { ProbeVetoError } from '../../probe/errors.js'
|
|
14
14
|
import { probe as defaultProbeRegistry } from '../../probe/registry.js'
|
|
15
15
|
import type { ProbeEnforcement } from '../../probe/registry.js'
|
|
16
|
+
import { ToolResultHalted } from '../../registry/tool/screen.js'
|
|
16
17
|
import type { ActivityStore } from '../../store/activity/memory.js'
|
|
17
18
|
import {
|
|
18
19
|
effectiveBashWorkingDirectory,
|
|
@@ -3131,11 +3132,16 @@ export class ToolExecutor {
|
|
|
3131
3132
|
}
|
|
3132
3133
|
|
|
3133
3134
|
/**
|
|
3134
|
-
* One execution attempt, with a throw materialized as an error result
|
|
3135
|
+
* One execution attempt, with a throw materialized as an error result —
|
|
3136
|
+
* except `ToolResultHalted`, which this deliberately lets through.
|
|
3135
3137
|
*
|
|
3136
3138
|
* an unhandled throw from `tools.execute(...)` used to
|
|
3137
3139
|
* propagate up to `result.ts` as `turn_failed` without emitting a
|
|
3138
|
-
* terminal `tool_completed`, leaving UI cards stuck in `executing`.
|
|
3140
|
+
* terminal `tool_completed`, leaving UI cards stuck in `executing`. That
|
|
3141
|
+
* is still right for a tool that merely broke. It is wrong for a screen
|
|
3142
|
+
* that said `halt`: `screen.ts` throws `ToolResultHalted` instead of
|
|
3143
|
+
* returning it so a catch this generic cannot flatten it into a failed
|
|
3144
|
+
* `ToolResult` the model reads and works around.
|
|
3139
3145
|
*
|
|
3140
3146
|
* The return is the full `ToolResult`, not a narrowed literal: the
|
|
3141
3147
|
* narrow version silently DROPPED `content`, so a tool returning an
|
|
@@ -3151,6 +3157,8 @@ export class ToolExecutor {
|
|
|
3151
3157
|
try {
|
|
3152
3158
|
return await this.executeWithDeadline(toolName, input, toolContext, prepared)
|
|
3153
3159
|
} catch (err) {
|
|
3160
|
+
if (err instanceof ToolResultHalted) throw err
|
|
3161
|
+
|
|
3154
3162
|
const message = toErrorMessage(err)
|
|
3155
3163
|
this.log.warn('Tool execution threw', {
|
|
3156
3164
|
[NAMZU.TURN_ID]: this.config.turnId,
|
|
@@ -164,6 +164,12 @@ export const DEFAULT_ERROR_RULES: readonly ErrorCatalogRule[] = [
|
|
|
164
164
|
message: 'The provider rejected the credentials for this turn.',
|
|
165
165
|
hint: 'Check that the API key is set, is not expired, and belongs to the account the model is billed to. Retrying will not help until it changes.',
|
|
166
166
|
},
|
|
167
|
+
{
|
|
168
|
+
id: 'provider.usage_limit',
|
|
169
|
+
when: (facts) => facts.code === 'usage_limit',
|
|
170
|
+
message: 'The account has used up its allowance until the limit resets.',
|
|
171
|
+
hint: 'Waiting inside this turn cannot help. Continue on another account or provider, or resume after the reset time the provider reported.',
|
|
172
|
+
},
|
|
167
173
|
{
|
|
168
174
|
id: 'provider.rate_limit',
|
|
169
175
|
when: (facts) => facts.code === 'rate_limit' || facts.status === 429,
|
|
@@ -15,6 +15,12 @@
|
|
|
15
15
|
export type ProviderErrorCode =
|
|
16
16
|
/** 429. Back off; honour `retryAfterMs` when the server sent one. */
|
|
17
17
|
| 'rate_limit'
|
|
18
|
+
/**
|
|
19
|
+
* The account's usage allowance is spent until a later reset, as opposed to
|
|
20
|
+
* a momentary burst limit. Waiting inside this turn cannot help, so it is
|
|
21
|
+
* not retried; a host may offer another account instead.
|
|
22
|
+
*/
|
|
23
|
+
| 'usage_limit'
|
|
18
24
|
/** 529 / 503 — the model is up but saturated. Back off. */
|
|
19
25
|
| 'overloaded'
|
|
20
26
|
/** Any other 5xx. Back off. */
|
|
@@ -487,7 +493,10 @@ const STRUCTURAL_CODES: Readonly<Record<string, ProviderErrorCode>> = {
|
|
|
487
493
|
string_above_max_length: 'context_length_exceeded',
|
|
488
494
|
rate_limit_exceeded: 'rate_limit',
|
|
489
495
|
rate_limit_error: 'rate_limit',
|
|
490
|
-
insufficient_quota: '
|
|
496
|
+
insufficient_quota: 'usage_limit',
|
|
497
|
+
usage_limit_reached: 'usage_limit',
|
|
498
|
+
usage_limit_exceeded: 'usage_limit',
|
|
499
|
+
billing_error: 'usage_limit',
|
|
491
500
|
overloaded_error: 'overloaded',
|
|
492
501
|
content_filter: 'content_filter',
|
|
493
502
|
content_policy_violation: 'content_filter',
|
|
@@ -497,6 +506,24 @@ const STRUCTURAL_CODES: Readonly<Record<string, ProviderErrorCode>> = {
|
|
|
497
506
|
model_not_found: 'not_found',
|
|
498
507
|
}
|
|
499
508
|
|
|
509
|
+
/**
|
|
510
|
+
* Is this exact provider code, or the provider's own complaint, a spent
|
|
511
|
+
* allowance? The codes are the vendors' identifiers (`insufficient_quota`,
|
|
512
|
+
* `usage_limit_reached`, `usage_limit_exceeded`, `billing_error`); the
|
|
513
|
+
* sentences are the two vendors that word it only in prose: an exhausted
|
|
514
|
+
* prepaid balance and an exhausted plan allowance.
|
|
515
|
+
*/
|
|
516
|
+
const USAGE_LIMIT_DETAIL: readonly RegExp[] = [
|
|
517
|
+
/credit balance is too low/i,
|
|
518
|
+
/exceeded your current quota/i,
|
|
519
|
+
/usage limit (?:has been )?(?:reached|exceeded)/i,
|
|
520
|
+
]
|
|
521
|
+
|
|
522
|
+
export function isUsageLimitSignal(code: string | undefined, detail: string | undefined): boolean {
|
|
523
|
+
if (code !== undefined && STRUCTURAL_CODES[code.toLowerCase()] === 'usage_limit') return true
|
|
524
|
+
return detail !== undefined && USAGE_LIMIT_DETAIL.some((re) => re.test(detail))
|
|
525
|
+
}
|
|
526
|
+
|
|
500
527
|
function codeFromStructure(err: unknown): ProviderErrorCode | undefined {
|
|
501
528
|
for (const link of causeChain(err)) {
|
|
502
529
|
for (const field of [link.code, link.type, (link.error as { type?: unknown })?.type]) {
|
|
@@ -556,6 +583,8 @@ function classifyFromProviderRequestError(
|
|
|
556
583
|
const candidate = err as Error & {
|
|
557
584
|
kind?: unknown
|
|
558
585
|
providerId?: unknown
|
|
586
|
+
providerCode?: unknown
|
|
587
|
+
detail?: unknown
|
|
559
588
|
status?: unknown
|
|
560
589
|
retryAfterMs?: unknown
|
|
561
590
|
shouldRetry?: unknown
|
|
@@ -563,15 +592,23 @@ function classifyFromProviderRequestError(
|
|
|
563
592
|
if (typeof candidate.kind !== 'string') return undefined
|
|
564
593
|
const mapped = KIND_TO_CODE[candidate.kind]
|
|
565
594
|
if (!mapped) return undefined
|
|
595
|
+
// A throttle or an invalid request that says the allowance is spent is not
|
|
596
|
+
// a burst limit: resending the same request cannot succeed.
|
|
597
|
+
const spent =
|
|
598
|
+
(candidate.kind === 'throttle' || candidate.kind === 'bad_request') &&
|
|
599
|
+
isUsageLimitSignal(
|
|
600
|
+
typeof candidate.providerCode === 'string' ? candidate.providerCode : undefined,
|
|
601
|
+
typeof candidate.detail === 'string' ? candidate.detail : undefined,
|
|
602
|
+
)
|
|
566
603
|
|
|
567
604
|
const retryAfterMs =
|
|
568
605
|
typeof candidate.retryAfterMs === 'number' ? candidate.retryAfterMs : readRetryAfterMs(err, now)
|
|
569
606
|
|
|
570
607
|
return new ProviderError({
|
|
571
|
-
code: mapped.code,
|
|
608
|
+
code: spent ? 'usage_limit' : mapped.code,
|
|
572
609
|
message: err.message,
|
|
573
610
|
// The provider saying "do not resend" beats the kind's own verdict.
|
|
574
|
-
retryable: candidate.shouldRetry === false ? false : mapped.retryable,
|
|
611
|
+
retryable: spent || candidate.shouldRetry === false ? false : mapped.retryable,
|
|
575
612
|
cause: err,
|
|
576
613
|
...(typeof candidate.providerId === 'string'
|
|
577
614
|
? { providerId: candidate.providerId }
|
|
@@ -576,6 +576,19 @@ export const BudgetBoundRecordSchema = inTurn('budget_bound', {
|
|
|
576
576
|
accountId: text.min(1),
|
|
577
577
|
})
|
|
578
578
|
|
|
579
|
+
/**
|
|
580
|
+
* The account a session's provider calls run under. Written when a session is
|
|
581
|
+
* bound to an account other than the provider's `default`, and again on each
|
|
582
|
+
* switch; never inside an open turn's work, only between turns. Replay reads
|
|
583
|
+
* the last one. A build that predates this record refuses a log holding it, so
|
|
584
|
+
* a session that never leaves the default account never writes one.
|
|
585
|
+
*/
|
|
586
|
+
export const AccountBoundRecordSchema = recordSchema('account_bound', {
|
|
587
|
+
accountId: text.min(1),
|
|
588
|
+
provider: text.min(1),
|
|
589
|
+
reason: z.enum(['start', 'user_switch', 'limit_switch']),
|
|
590
|
+
})
|
|
591
|
+
|
|
579
592
|
export const LogRepairedRecordSchema = recordSchema('log_repaired', {
|
|
580
593
|
truncatedBytes: positive,
|
|
581
594
|
lastGoodSeq: positive,
|
|
@@ -612,6 +625,7 @@ const RECORD_ONLY_SCHEMAS = {
|
|
|
612
625
|
child_session_ended: ChildSessionEndedRecordSchema,
|
|
613
626
|
audit: AuditRecordSchema,
|
|
614
627
|
budget_bound: BudgetBoundRecordSchema,
|
|
628
|
+
account_bound: AccountBoundRecordSchema,
|
|
615
629
|
log_repaired: LogRepairedRecordSchema,
|
|
616
630
|
} as const
|
|
617
631
|
|
|
@@ -982,6 +996,7 @@ export type CompactionRecord = z.infer<typeof CompactionRecordSchema>
|
|
|
982
996
|
export type ChildSessionEndedRecord = z.infer<typeof ChildSessionEndedRecordSchema>
|
|
983
997
|
export type AuditRecord = z.infer<typeof AuditRecordSchema>
|
|
984
998
|
export type BudgetBoundRecord = z.infer<typeof BudgetBoundRecordSchema>
|
|
999
|
+
export type AccountBoundRecord = z.infer<typeof AccountBoundRecordSchema>
|
|
985
1000
|
export type LogRepairedRecord = z.infer<typeof LogRepairedRecordSchema>
|
|
986
1001
|
|
|
987
1002
|
export type SessionRecordOnly =
|
|
@@ -998,6 +1013,7 @@ export type SessionRecordOnly =
|
|
|
998
1013
|
| ChildSessionEndedRecord
|
|
999
1014
|
| AuditRecord
|
|
1000
1015
|
| BudgetBoundRecord
|
|
1016
|
+
| AccountBoundRecord
|
|
1001
1017
|
| LogRepairedRecord
|
|
1002
1018
|
|
|
1003
1019
|
/** One line of a session log. */
|