The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import { |
| 3 | NodeId, |
| 4 | ProviderDriverKind, |
| 5 | ProviderInstanceId, |
| 6 | ProviderSessionId, |
| 7 | ProviderSetupError, |
| 8 | ProviderThreadId, |
| 9 | ProviderTurnId, |
| 10 | RunId, |
| 11 | ThreadId, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import * as Cause from "effect/Cause"; |
| 14 | import * as DateTime from "effect/DateTime"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | |
| 17 | import { |
| 18 | knownProviderFailureMessage, |
| 19 | makeProviderFailure, |
| 20 | makeProviderFailureTurnItem, |
| 21 | MAX_PROVIDER_FAILURE_MESSAGE_LENGTH, |
| 22 | ContextHandoffBudgetError, |
| 23 | ContextHandoffBackgroundWorkError, |
| 24 | } from "./failure.ts"; |
| 25 | import * as IdAllocator from "./IdAllocator.ts"; |
| 26 | import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; |
| 27 | |
| 28 | it("redacts credentials and URL secrets from provider failures", () => { |
| 29 | const failure = makeProviderFailure({ |
| 30 | message: |
| 31 | 'request failed: Authorization: Bearer bearer-secret https://user:pass@example.test/path?access_token=url-secret#fragment {"token":"json-secret"} api_key=key-secret sk-abcdefghijklmnop', |
| 32 | code: "provider_rejected", |
| 33 | class: "provider_error", |
| 34 | }); |
| 35 | |
| 36 | assert.equal(failure.class, "provider_error"); |
| 37 | assert.equal(failure.code, "provider_rejected"); |
| 38 | assert.include(failure.message, "[REDACTED]"); |
| 39 | assert.include(failure.message, "https://example.test/path"); |
| 40 | assert.notInclude(failure.message, "bearer-secret"); |
| 41 | assert.notInclude(failure.message, "user:pass"); |
| 42 | assert.notInclude(failure.message, "url-secret"); |
| 43 | assert.notInclude(failure.message, "json-secret"); |
| 44 | assert.notInclude(failure.message, "key-secret"); |
| 45 | assert.notInclude(failure.message, "sk-abcdefghijklmnop"); |
| 46 | }); |
| 47 | |
| 48 | it("redacts credentials whose key names end in a credential word", () => { |
| 49 | const failure = makeProviderFailure({ |
| 50 | code: '{"refresh_token":"code value"}', |
| 51 | message: |
| 52 | 'oauth refresh failed: client_secret=client-value refresh_token=refresh-value {"client_secret":"json-value"} clientSecret: camel-value X-Auth-Token: header-value max_tokens=4096', |
| 53 | }); |
| 54 | |
| 55 | assert.equal(failure.code, '{"refresh_token":"[REDACTED]"}'); |
| 56 | assert.equal( |
| 57 | failure.message, |
| 58 | 'oauth refresh failed: client_secret=[REDACTED] refresh_token=[REDACTED] {"client_secret":"[REDACTED]"} clientSecret: [REDACTED] X-Auth-Token: [REDACTED] max_tokens=4096', |
| 59 | ); |
| 60 | }); |
| 61 | |
| 62 | it("reports an oversized provider request as not retryable instead of the gateway's error page", () => { |
| 63 | for (const message of [ |
| 64 | "413 <html>\n<head><title>413 Request Entity Too Large</title></head>\n<body>\n<center><h1>413 Request Entity Too Large</h1></center>\n<hr><center>nginx</center>\n</body>\n</html>", |
| 65 | "unexpected status 413 Payload Too Large: request body exceeds the limit", |
| 66 | 'API Error: 413 {"type":"error","error":{"type":"request_too_large"}}', |
| 67 | ]) { |
| 68 | const failure = makeProviderFailure({ message, class: "provider_error", retryable: true }); |
| 69 | |
| 70 | assert.equal(failure.class, "provider_error"); |
| 71 | assert.equal(failure.retryable, false); |
| 72 | assert.equal( |
| 73 | failure.message, |
| 74 | "This thread's request is too large for the provider (HTTP 413), so retrying fails the same way. Start a fresh session with /clear, or continue in a new thread.", |
| 75 | ); |
| 76 | } |
| 77 | for (const message of [ |
| 78 | "Request too large for gpt-4o on tokens per min (TPM): Limit 30000, Requested 41300.", |
| 79 | "Rate limited after 413 requests.", |
| 80 | "Tool result content too large to return.", |
| 81 | ]) { |
| 82 | const failure = makeProviderFailure({ message, retryable: true }); |
| 83 | assert.equal(failure.message, message); |
| 84 | assert.equal(failure.retryable, true); |
| 85 | } |
| 86 | }); |
| 87 | |
| 88 | it("does not split a surrogate pair at the truncation boundary", () => { |
| 89 | const failure = makeProviderFailure({ |
| 90 | message: `${"a".repeat(MAX_PROVIDER_FAILURE_MESSAGE_LENGTH - 2)}🚀tail`, |
| 91 | }); |
| 92 | |
| 93 | assert.equal(failure.message.length, MAX_PROVIDER_FAILURE_MESSAGE_LENGTH - 1); |
| 94 | assert.equal(failure.message.at(-1), "…"); |
| 95 | assert.notMatch(failure.message.slice(0, -1), /[\uD800-\uDBFF]$/u); |
| 96 | }); |
| 97 | |
| 98 | it("does not expose arbitrary cause messages and preserves explicit messages", () => { |
| 99 | const cause = new Error("Adapter failed", { |
| 100 | cause: new Error("Session expired. Sign in again."), |
| 101 | }); |
| 102 | for (const value of [ |
| 103 | cause, |
| 104 | Cause.fail(cause), |
| 105 | "Session expired. Sign in again.", |
| 106 | { message: "Session expired. Sign in again." }, |
| 107 | ]) { |
| 108 | assert.equal(makeProviderFailure({ cause: value }).message, "Provider turn failed."); |
| 109 | assert.equal( |
| 110 | makeProviderFailure({ cause: value, message: "Provider connection closed." }).message, |
| 111 | "Provider connection closed.", |
| 112 | ); |
| 113 | } |
| 114 | }); |
| 115 | |
| 116 | it("preserves actionable T3 errors wrapped by turn startup", () => { |
| 117 | for (const error of [ |
| 118 | new ContextHandoffBudgetError(), |
| 119 | new ContextHandoffBackgroundWorkError(), |
| 120 | new ProviderSetupError({ |
| 121 | instanceId: ProviderInstanceId.make("codex"), |
| 122 | operation: "runtime", |
| 123 | detail: "Codex 0.144.3 does not work with this T3 Code release.", |
| 124 | }), |
| 125 | ]) { |
| 126 | const cause = new ProviderAdapter.ProviderAdapterTurnStartError({ |
| 127 | driver: ProviderDriverKind.make("codex"), |
| 128 | threadId: ThreadId.make("thread:handoff-error"), |
| 129 | providerThreadId: ProviderThreadId.make("provider-thread:handoff-error"), |
| 130 | runId: RunId.make("run:handoff-error"), |
| 131 | cause: error, |
| 132 | }); |
| 133 | assert.equal(makeProviderFailure({ cause: Cause.fail(cause) }).message, error.message); |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | it("names how the provider process exited and the stage it failed in, without its output", () => { |
| 138 | for (const [exit, status] of [ |
| 139 | [{ signal: "SIGILL" }, " after signal SIGILL"], |
| 140 | [{ code: 134 }, " with code 134"], |
| 141 | [{}, ""], |
| 142 | [{ signal: "SIGILL Bearer signal-secret" }, ""], |
| 143 | ] as const) { |
| 144 | const sessionOpenFailure = { |
| 145 | _tag: "ProviderSessionOpenError", |
| 146 | cause: new ProviderAdapter.ProviderAdapterOpenSessionError({ |
| 147 | driver: ProviderDriverKind.make("antigravity"), |
| 148 | providerSessionId: ProviderSessionId.make("provider-session:private-id"), |
| 149 | cause: { |
| 150 | _tag: "AcpProcessExitedError", |
| 151 | ...exit, |
| 152 | pid: 4242, |
| 153 | stderr: "*** SIGILL *** token=stderr-secret", |
| 154 | cause: new Error("Process interrupted with private-detail"), |
| 155 | }, |
| 156 | }), |
| 157 | }; |
| 158 | const expected = `The provider session could not be opened: the provider process exited${status}. Check the server logs for the provider's output, then retry the turn.`; |
| 159 | assert.equal(knownProviderFailureMessage(sessionOpenFailure), expected); |
| 160 | assert.equal(makeProviderFailure({ cause: Cause.fail(sessionOpenFailure) }).message, expected); |
| 161 | } |
| 162 | assert.equal( |
| 163 | makeProviderFailure({ cause: { _tag: "AcpProcessExitedError", code: 1 } }).message, |
| 164 | "The provider process exited with code 1. Check the server logs for the provider's output, then retry the turn.", |
| 165 | ); |
| 166 | const denied = makeProviderFailure({ |
| 167 | cause: { |
| 168 | _tag: "CodexAppServerProcessExitedError", |
| 169 | code: 1, |
| 170 | stderr: "Error: Operation not permitted (os error 1) at /Users/private/Desktop", |
| 171 | }, |
| 172 | }).message; |
| 173 | assert.include(denied, "The provider process exited with code 1. It was denied access"); |
| 174 | assert.include(denied, "Files and Folders"); |
| 175 | assert.notInclude(denied, "/Users/private"); |
| 176 | }); |
| 177 | |
| 178 | it("tells the user to sign in only when an ACP agent answers auth_required", () => { |
| 179 | const providerSessionId = ProviderSessionId.make("provider-session:private-id"); |
| 180 | const sessionOpenFailure = (code: number) => ({ |
| 181 | _tag: "ProviderSessionOpenError", |
| 182 | cause: new ProviderAdapter.ProviderAdapterOpenSessionError({ |
| 183 | driver: ProviderDriverKind.make("acpRegistry"), |
| 184 | providerSessionId, |
| 185 | cause: Cause.fail({ _tag: "AcpRequestError", code, errorMessage: "Private agent text" }), |
| 186 | }), |
| 187 | }); |
| 188 | |
| 189 | assert.equal( |
| 190 | makeProviderFailure({ cause: Cause.fail(sessionOpenFailure(-32000)) }).message, |
| 191 | "The provider needs you to sign in. Sign in from its provider settings or with its own CLI or app, then retry the turn.", |
| 192 | ); |
| 193 | assert.isUndefined(knownProviderFailureMessage(sessionOpenFailure(-32603))); |
| 194 | }); |
| 195 | |
| 196 | it("keeps unrecognized reasons and provider text out of category messages", () => { |
| 197 | for (const cause of [ |
| 198 | "Private launch command", |
| 199 | { _tag: "ProviderAdapterProtocolError", detail: "Pi model 'private-config' is invalid" }, |
| 200 | { _tag: "ProviderAdapterProtocolError", reason: "private-config" }, |
| 201 | { _tag: "ProviderAdapterProtocolError", reason: "constructor" }, |
| 202 | { _tag: "constructor", cause: "private-config" }, |
| 203 | { |
| 204 | _tag: "CodexAppServerRequestError", |
| 205 | method: "thread/goal/set", |
| 206 | message: "thread not found: private-thread", |
| 207 | }, |
| 208 | { |
| 209 | _tag: "CodexAppServerRequestError", |
| 210 | method: "turn/start", |
| 211 | message: "goal objective must be at most 4000 characters", |
| 212 | }, |
| 213 | ]) { |
| 214 | assert.equal( |
| 215 | makeProviderFailure({ cause: { _tag: "ProviderAdapterTurnStartError", cause } }).message, |
| 216 | "The provider could not start this turn. Retry the turn; if it keeps failing, check the provider setup and server logs.", |
| 217 | ); |
| 218 | } |
| 219 | assert.equal( |
| 220 | makeProviderFailure({ |
| 221 | cause: { _tag: "ProviderAdapterProtocolError", reason: "previous_turn_active" }, |
| 222 | }).message, |
| 223 | "Provider turn failed.", |
| 224 | ); |
| 225 | }); |
| 226 | |
| 227 | it("names a managed Codex policy that refuses the thread's access mode", () => { |
| 228 | const message = makeProviderFailure({ |
| 229 | cause: { |
| 230 | _tag: "ProviderAdapterTurnStartError", |
| 231 | cause: { |
| 232 | _tag: "CodexAppServerRequestError", |
| 233 | method: "turn/start", |
| 234 | code: -32600, |
| 235 | message: |
| 236 | "invalid thread settings override: invalid value for `sandbox_mode`: `DangerFullAccess` is not in the allowed set [ReadOnly, WorkspaceWrite] (set by /etc/codex/requirements.toml)", |
| 237 | }, |
| 238 | }, |
| 239 | }).message; |
| 240 | assert.notInclude(message, "Retry the turn"); |
| 241 | assert.match(message, /requirements|policy/iu); |
| 242 | }); |
| 243 | |
| 244 | it("uses the innermost category without carrying an outer reason into it", () => { |
| 245 | assert.equal( |
| 246 | makeProviderFailure({ |
| 247 | cause: { |
| 248 | _tag: "ProviderAdapterTurnStartError", |
| 249 | cause: { |
| 250 | _tag: "ProviderAdapterProtocolError", |
| 251 | reason: "previous_turn_active", |
| 252 | cause: Cause.fail({ _tag: "ProviderAdapterOpenSessionError" }), |
| 253 | }, |
| 254 | }, |
| 255 | }).message, |
| 256 | "The provider session could not be opened. Check that the provider is installed and signed in, then retry the turn.", |
| 257 | ); |
| 258 | }); |
| 259 | |
| 260 | it("explains a native thread another client is writing to", () => { |
| 261 | const inUse = new ProviderAdapter.ProviderAdapterThreadInUseError({ |
| 262 | driver: ProviderDriverKind.make("codex"), |
| 263 | providerThreadId: ProviderThreadId.make("provider-thread:in-use"), |
| 264 | detail: "This conversation is open in another Codex client.", |
| 265 | cause: new Error("thread provider-thread:in-use already has an active writer"), |
| 266 | }); |
| 267 | const cause = new ProviderAdapter.ProviderAdapterTurnStartError({ |
| 268 | driver: ProviderDriverKind.make("codex"), |
| 269 | threadId: ThreadId.make("thread:in-use"), |
| 270 | providerThreadId: ProviderThreadId.make("provider-thread:in-use"), |
| 271 | runId: RunId.make("run:in-use"), |
| 272 | cause: inUse, |
| 273 | }); |
| 274 | assert.equal(makeProviderFailure({ cause: Cause.fail(cause) }).message, inUse.message); |
| 275 | }); |
| 276 | |
| 277 | it("does not expose defect text nested inside a known error category", () => { |
| 278 | const failure = makeProviderFailure({ |
| 279 | cause: { |
| 280 | _tag: "ProviderAdapterEventStreamError", |
| 281 | cause: new Error( |
| 282 | `Session rejected: Bearer nested-secret https://user:pass@example.test/path?token=secret ${"x".repeat(5000)}`, |
| 283 | ), |
| 284 | }, |
| 285 | }); |
| 286 | assert.include(failure.message, "provider event stream closed unexpectedly"); |
| 287 | assert.notInclude(failure.message, "Session rejected:"); |
| 288 | assert.notInclude(failure.message, "nested-secret"); |
| 289 | assert.notInclude(failure.message, "user:pass"); |
| 290 | assert.notInclude(failure.message, "token=secret"); |
| 291 | assert.isBelow(failure.message.length, MAX_PROVIDER_FAILURE_MESSAGE_LENGTH); |
| 292 | }); |
| 293 | |
| 294 | it("handles cyclic causes and throwing accessors", () => { |
| 295 | const cyclic: { message: string; cause?: unknown } = { |
| 296 | message: "Provider disconnected. Retry the turn.", |
| 297 | }; |
| 298 | cyclic.cause = cyclic; |
| 299 | assert.equal(makeProviderFailure({ cause: cyclic }).message, "Provider turn failed."); |
| 300 | assert.equal( |
| 301 | makeProviderFailure({ |
| 302 | cause: { |
| 303 | get message() { |
| 304 | throw new Error("unreadable"); |
| 305 | }, |
| 306 | get cause() { |
| 307 | throw new Error("unreadable"); |
| 308 | }, |
| 309 | }, |
| 310 | }).message, |
| 311 | "Provider turn failed.", |
| 312 | ); |
| 313 | }); |
| 314 | |
| 315 | it("does not serialize arbitrary provider causes", () => { |
| 316 | const failure = makeProviderFailure({ |
| 317 | cause: { |
| 318 | payload: { authorization: "Bearer nested-secret" }, |
| 319 | stack: "private provider stack", |
| 320 | }, |
| 321 | class: "transport_error", |
| 322 | }); |
| 323 | |
| 324 | assert.deepEqual(failure, { |
| 325 | class: "transport_error", |
| 326 | message: "Provider turn failed.", |
| 327 | code: null, |
| 328 | retryable: null, |
| 329 | }); |
| 330 | }); |
| 331 | |
| 332 | it.effect("keys terminal failure items by provider turn across retries and fallback paths", () => |
| 333 | Effect.gen(function* () { |
| 334 | const idAllocator = yield* IdAllocator.IdAllocatorV2; |
| 335 | const driver = ProviderDriverKind.make("codex"); |
| 336 | const runId = RunId.make("run:provider-failure-id"); |
| 337 | const base = { |
| 338 | idAllocator, |
| 339 | driver, |
| 340 | threadId: ThreadId.make("thread:provider-failure-id"), |
| 341 | runId, |
| 342 | nodeId: NodeId.make("node:provider-failure-id"), |
| 343 | providerThreadId: ProviderThreadId.make("provider-thread:provider-failure-id"), |
| 344 | itemOrdinal: 101, |
| 345 | failure: makeProviderFailure({ message: "Provider failed" }), |
| 346 | occurredAt: DateTime.makeUnsafe("2026-06-22T12:00:00.000Z"), |
| 347 | } as const; |
| 348 | const firstTurnId = ProviderTurnId.make("provider-turn:provider-failure-id:first"); |
| 349 | const secondTurnId = ProviderTurnId.make("provider-turn:provider-failure-id:second"); |
| 350 | |
| 351 | const firstAttempt = makeProviderFailureTurnItem({ |
| 352 | ...base, |
| 353 | providerTurnId: firstTurnId, |
| 354 | }); |
| 355 | const retriedAttempt = makeProviderFailureTurnItem({ |
| 356 | ...base, |
| 357 | providerTurnId: secondTurnId, |
| 358 | }); |
| 359 | const ingestorFallback = makeProviderFailureTurnItem({ |
| 360 | ...base, |
| 361 | runId: null, |
| 362 | nodeId: null, |
| 363 | providerTurnId: firstTurnId, |
| 364 | }); |
| 365 | |
| 366 | assert.notEqual(firstAttempt.id, retriedAttempt.id); |
| 367 | assert.equal(firstAttempt.id, ingestorFallback.id); |
| 368 | assert.equal(firstAttempt.ordinal, 101); |
| 369 | }).pipe(Effect.provide(IdAllocator.layer)), |
| 370 | ); |