@oxyhq/contracts 0.28.0 → 0.29.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.
@@ -38,6 +38,11 @@ import { inferenceProviderSlugSchema, requestIdSchema } from './identifiers.js';
38
38
  * PLATFORM's own credential fails every identical retry until an operator
39
39
  * rotates a key, so classifying it as `provider_error` would send every client
40
40
  * into a retry loop against a request that cannot succeed.
41
+ *
42
+ * `provider_billing_refused` is in that group for the same reason and was found
43
+ * the same way — an upstream declining to bill OXY (Anthropic answers 402) has
44
+ * to be distinguishable from the customer's own balance running out, or the
45
+ * error tells them to go and top up an account that is not the one at fault.
41
46
  */
42
47
  export const INFERENCE_ERROR_CODES = [
43
48
  'invalid_request',
@@ -65,6 +70,7 @@ export const INFERENCE_ERROR_CODES = [
65
70
  'provider_timeout',
66
71
  'provider_overloaded',
67
72
  'provider_credential_invalid',
73
+ 'provider_billing_refused',
68
74
  'service_unavailable',
69
75
  'internal_error',
70
76
  ];
@@ -84,6 +90,12 @@ export const inferenceErrorCodeSchema = z.enum(INFERENCE_ERROR_CODES);
84
90
  * one because only the first names an action the customer can take. Both are
85
91
  * non-retryable for the same reason: a credential an upstream has refused keeps
86
92
  * being refused until somebody replaces it.
93
+ *
94
+ * `quota_exceeded` and `provider_billing_refused` divide along the same line:
95
+ * both are money, but one is the CUSTOMER's ceiling and the other is Oxy's
96
+ * account with an upstream. Reporting the second as the first is retryability-
97
+ * correct and diagnostically wrong, which is the worst combination — it reads
98
+ * as actionable and the action does nothing.
87
99
  */
88
100
  export const NON_RETRYABLE_INFERENCE_ERROR_CODES = [
89
101
  'invalid_request',
@@ -106,27 +118,114 @@ export const NON_RETRYABLE_INFERENCE_ERROR_CODES = [
106
118
  'upstream_content_filtered',
107
119
  'cancelled',
108
120
  'provider_credential_invalid',
121
+ 'provider_billing_refused',
109
122
  ];
110
123
  const NON_RETRYABLE_CODE_SET = new Set(NON_RETRYABLE_INFERENCE_ERROR_CODES);
124
+ /* -------------------------------------------------------------------------- */
125
+ /* Credential-shaped text */
126
+ /* -------------------------------------------------------------------------- */
127
+ /**
128
+ * A run of characters long enough and opaque enough to BE a credential.
129
+ *
130
+ * The alphabet every bearer token, API key and base64/base64url secret is
131
+ * written in. The LENGTH floors below are what keep this from being an entropy
132
+ * heuristic: nothing here fires on a short word, so `authorization: none` and
133
+ * `api_key=***` read as what they are.
134
+ */
135
+ const OPAQUE_ALPHABET = '[A-Za-z0-9][A-Za-z0-9._~+/=-]';
111
136
  /**
112
- * Text that looks like it carries a credential.
137
+ * Words a producer substitutes FOR a credential.
113
138
  *
114
- * A deliberately narrow set of literal markers the shapes upstream providers
115
- * actually echo rather than an entropy heuristic, which would reject
116
- * legitimate error text (a request id, a base64 image fragment) and teach
117
- * producers to strip messages until they pass.
139
+ * Excluded at the value position so a message whose secret has already been
140
+ * replaced is accepted. That acceptance is deliberate and is half the fix for
141
+ * issue #1027: the previous pattern refused `Authorization: [redacted]` a
142
+ * correctly redacted string which is precisely what pushed a producer into
143
+ * redacting the MARKER instead, and a marker-redacted string carries the secret
144
+ * and passes.
118
145
  */
119
- const CREDENTIAL_LIKE_TEXT = /(?:bearer\s+[a-z0-9._~+/=-]{8,}|authorization\s*[:=]|api[_-]?key\s*[:=]|\bsk-[a-z0-9_-]{8,}|\bsk_(?:live|test)_[a-z0-9]{8,})/i;
146
+ const PLACEHOLDER_WORDS = 'redacted|removed|hidden|masked|scrubbed|elided|omitted|filtered|sanitized|sanitised|none|null|undefined|empty';
147
+ /** A value position whose contents are a placeholder rather than a secret. */
148
+ const NOT_A_PLACEHOLDER = `(?!(?:${PLACEHOLDER_WORDS})\\b)`;
120
149
  /**
121
- * Free text that is safe to hand a customer: bounded, and refused outright if a
122
- * credential marker appears in it. Applied to BOTH the Oxy message and the
150
+ * Header and parameter names that carry a credential, as any provider spells
151
+ * them.
152
+ *
153
+ * The prefix group is the whole point of the rewrite: `authorization` and
154
+ * `api_key` were matched literally, so `x-api-key`, `anthropic-api-key`,
155
+ * `x-goog-api-key` and `proxy-authorization` — the spellings an upstream
156
+ * actually echoes — went unrecognised.
157
+ */
158
+ const CREDENTIAL_NAME = '(?:[a-z0-9]{1,20}[-_]){0,3}(?:api[-_]?(?:key|token|secret)|authorization|auth[-_]?(?:token|key)?|access[-_]?token|id[-_]?token|refresh[-_]?token|bearer[-_]?token|secret[-_]?key|private[-_]?key|client[-_]?secret|session[-_]?(?:id|key|token)|passwords?|passwd|cookie|credentials?|tokens?|secrets?)';
159
+ /** An auth scheme sitting between the marker and the value. */
160
+ const AUTH_SCHEME = '(?:(?:bearer|basic|token|apikey|digest)\\s+)?';
161
+ /**
162
+ * The four ways a credential is recognisable in free text.
163
+ *
164
+ * Each is checked independently, so removing one signal does not clear the
165
+ * string — which is the failure #1027 reported. All four are load-bearing:
166
+ * `inference.errors.test.ts` has a case that only one of them catches, and
167
+ * deleting any one entry turns a test red.
168
+ */
169
+ const CREDENTIAL_PATTERNS = [
170
+ // 1. A credential-bearing name ASSIGNED a value that is long enough to be a
171
+ // credential. The value is anchored to the separator so a placeholder at
172
+ // that position ends the match rather than being skipped over.
173
+ new RegExp(`(?:^|[^a-z0-9])${CREDENTIAL_NAME}["']?\\s*[:=]\\s*["']?${AUTH_SCHEME}${NOT_A_PLACEHOLDER}${OPAQUE_ALPHABET}{7,}`, 'i'),
174
+ // 2. A bearer token with no marker in front of it, which is how an upstream
175
+ // quotes the header value alone.
176
+ new RegExp(`\\bbearer\\s+${NOT_A_PLACEHOLDER}${OPAQUE_ALPHABET}{7,}`, 'i'),
177
+ // 3. Token grammars that ARE credentials wherever they appear, marker or not.
178
+ // This is the layer that survives a producer stripping the marker, and it
179
+ // is a closed list of issued shapes rather than an entropy score, so a
180
+ // request id or a base64 image fragment is unaffected.
181
+ //
182
+ // Case-SENSITIVE on purpose: `AKIA`, `AIza` and `gh[pousr]_` are issued in
183
+ // exactly that case, and matching them case-insensitively would start
184
+ // firing on ordinary words.
185
+ /\b(?:sk-[A-Za-z0-9_-]{8,}|[sprk]k_(?:live|test)_[A-Za-z0-9]{8,}|AKIA[0-9A-Z]{12,}|ASIA[0-9A-Z]{12,}|AIza[0-9A-Za-z_-]{20,}|gh[pousr]_[A-Za-z0-9]{16,}|github_pat_[A-Za-z0-9_]{20,}|xox[abeprs]-[A-Za-z0-9-]{10,}|glpat-[A-Za-z0-9_-]{16,}|npm_[A-Za-z0-9]{20,}|eyJ[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{4,})/,
186
+ // 4. A redaction placeholder standing NEXT TO a surviving opaque value — the
187
+ // exact residue of the span redaction in #1027 (`{x-[redacted] <key>}`).
188
+ // A correct redaction puts the placeholder WHERE the value was, so the two
189
+ // never appear side by side; a marker-span redaction leaves them adjacent.
190
+ // Both signals are required, which is what keeps an ordinary redacted
191
+ // message from being refused.
192
+ new RegExp(`(?:[[<({]\\s*(?:${PLACEHOLDER_WORDS})[^\\])}>]{0,16}[\\])}>]|\\*{3,})[^A-Za-z0-9]{0,4}${OPAQUE_ALPHABET}{11,}`, 'i'),
193
+ ];
194
+ /**
195
+ * Free text that is safe to hand a customer: bounded, and refused if it still
196
+ * looks like it carries a credential. Applied to BOTH the Oxy message and the
123
197
  * upstream one — a leak is no less a leak for having been written by a provider.
198
+ *
199
+ * ## This is a last-resort REFUSAL, not protection
200
+ *
201
+ * A pattern over the OUTPUT cannot be the control that keeps a credential out of
202
+ * an error, and a producer that treats it as one has the hole #1027 reported.
203
+ * The only reliable control is redacting the KNOWN SECRET VALUE at the point
204
+ * where the producer still holds the bytes it sent — which is an adapter's job
205
+ * and is available to nobody else. This refinement exists to catch what that
206
+ * control missed, and nothing here is a licence to skip it.
207
+ *
208
+ * Two rules follow, and they are the whole reason this text is longer than the
209
+ * pattern it describes:
210
+ *
211
+ * - **Never redact by replacing the span this pattern matched.** The span is
212
+ * the MARKER; the secret is what follows it. OxyHQ/Relay#3 measured the
213
+ * result: `{x-api-key: <key>}` is refused, `{x-[redacted] <key>}` was
214
+ * accepted, and both carry the key. Redaction made the leak worse by
215
+ * converting "this string is dangerous" into "this string is fine".
216
+ * - **This package deliberately ships no redaction helper.** One keyed on these
217
+ * patterns would rebuild the same defect one layer up, and one that took the
218
+ * secret as an argument would only restate what the producer already has.
219
+ *
220
+ * What it still cannot see, stated so nobody relies on it: a credential with no
221
+ * marker, no issued-token prefix and no placeholder beside it is bytes that look
222
+ * like a request id, and refusing those means refusing request ids.
124
223
  */
125
224
  export const safeErrorTextSchema = z
126
225
  .string()
127
226
  .min(1)
128
227
  .max(2000)
129
- .refine((value) => !CREDENTIAL_LIKE_TEXT.test(value), 'error text must not contain credential-shaped material');
228
+ .refine((value) => !CREDENTIAL_PATTERNS.some((pattern) => pattern.test(value)), 'error text must not contain credential-shaped material');
130
229
  /**
131
230
  * A coarse classification of an upstream failure (ADR 0010's `upstreamCategory`).
132
231
  *
@@ -174,12 +174,22 @@ export const inferenceStreamErrorEventSchema = z.object({
174
174
  /** Carries its own `schemaVersion`: the same body is returned non-streaming. */
175
175
  error: inferenceErrorSchema,
176
176
  });
177
- /** Why generation stopped. */
177
+ /**
178
+ * Why generation stopped.
179
+ *
180
+ * `refusal` and `content_filter` are separate members because they are separate
181
+ * events: the MODEL declining to answer is a property of the answer, while a
182
+ * filter is an upstream system removing one. The delta channels already carry
183
+ * that distinction (`channel: 'refusal'` beside the filter's own error code),
184
+ * so collapsing it here would have made the terminal event less specific than
185
+ * the stream that produced it.
186
+ */
178
187
  export const inferenceFinishReasonSchema = z.enum([
179
188
  'stop',
180
189
  'length',
181
190
  'tool_calls',
182
191
  'content_filter',
192
+ 'refusal',
183
193
  'cancelled',
184
194
  ]);
185
195
  /**
@@ -71,12 +71,19 @@
71
71
  *
72
72
  * MAJOR is bumped when any individual shape's `schemaVersion` increments (at
73
73
  * least one message is now read differently by the two sides); MINOR when a
74
- * shape or an optional field is added; PATCH for documentation-only changes
75
- * that leave every parsed byte identical.
74
+ * shape or an optional field is added, when a CLOSED ENUM gains a member, or
75
+ * when a refinement changes which bytes parse; PATCH for documentation-only
76
+ * changes that leave every parsed byte identical.
77
+ *
78
+ * The last two are MINOR rather than PATCH because both produce the same
79
+ * failure: a producer on the newer set emits something the older set refuses,
80
+ * with no `schemaVersion` difference to explain it. A new enum member and a
81
+ * loosened refinement are exactly what the handshake exists to surface — a
82
+ * skew the per-message version cannot express.
76
83
  *
77
84
  * This constant is deliberately NOT embedded in the request envelope. Pinning a
78
85
  * request to the version of the whole set would make an unrelated additive
79
86
  * change to, say, the catalogue reject every in-flight inference request; the
80
87
  * per-shape `schemaVersion` is what a message is validated against.
81
88
  */
82
- export const INFERENCE_CONTRACT_VERSION = '1.0.0';
89
+ export const INFERENCE_CONTRACT_VERSION = '1.1.0';