Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import {
3 NodeId,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 ProviderSetupError,
7 ProviderThreadId,
8 ProviderTurnId,
9 RunId,
10 ThreadId,
11} from "@t3tools/contracts";
12import * as Cause from "effect/Cause";
13import * as DateTime from "effect/DateTime";
14import * as Effect from "effect/Effect";
15
16import {
17 makeProviderFailure,
18 makeProviderFailureTurnItem,
19 MAX_PROVIDER_FAILURE_MESSAGE_LENGTH,
20 ContextHandoffBudgetError,
21 ContextHandoffBackgroundWorkError,
22} from "./failure.ts";
23import * as IdAllocator from "./IdAllocator.ts";
24import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
25
26it("redacts credentials and URL secrets from provider failures", () => {
27 const failure = makeProviderFailure({
28 message:
29 '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',
30 code: "provider_rejected",
31 class: "provider_error",
32 });
33
34 assert.equal(failure.class, "provider_error");
35 assert.equal(failure.code, "provider_rejected");
36 assert.include(failure.message, "[REDACTED]");
37 assert.include(failure.message, "https://example.test/path");
38 assert.notInclude(failure.message, "bearer-secret");
39 assert.notInclude(failure.message, "user:pass");
40 assert.notInclude(failure.message, "url-secret");
41 assert.notInclude(failure.message, "json-secret");
42 assert.notInclude(failure.message, "key-secret");
43 assert.notInclude(failure.message, "sk-abcdefghijklmnop");
44});
45
46it("redacts credentials whose key names end in a credential word", () => {
47 const failure = makeProviderFailure({
48 code: '{"refresh_token":"code value"}',
49 message:
50 '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',
51 });
52
53 assert.equal(failure.code, '{"refresh_token":"[REDACTED]"}');
54 assert.equal(
55 failure.message,
56 'oauth refresh failed: client_secret=[REDACTED] refresh_token=[REDACTED] {"client_secret":"[REDACTED]"} clientSecret: [REDACTED] X-Auth-Token: [REDACTED] max_tokens=4096',
57 );
58});
59
60it("does not split a surrogate pair at the truncation boundary", () => {
61 const failure = makeProviderFailure({
62 message: `${"a".repeat(MAX_PROVIDER_FAILURE_MESSAGE_LENGTH - 2)}🚀tail`,
63 });
64
65 assert.equal(failure.message.length, MAX_PROVIDER_FAILURE_MESSAGE_LENGTH - 1);
66 assert.equal(failure.message.at(-1), "…");
67 assert.notMatch(failure.message.slice(0, -1), /[\uD800-\uDBFF]$/u);
68});
69
70it("does not expose arbitrary cause messages and preserves explicit messages", () => {
71 const cause = new Error("Adapter failed", {
72 cause: new Error("Session expired. Sign in again."),
73 });
74 for (const value of [
75 cause,
76 Cause.fail(cause),
77 "Session expired. Sign in again.",
78 { message: "Session expired. Sign in again." },
79 ]) {
80 assert.equal(makeProviderFailure({ cause: value }).message, "Provider turn failed.");
81 assert.equal(
82 makeProviderFailure({ cause: value, message: "Provider connection closed." }).message,
83 "Provider connection closed.",
84 );
85 }
86});
87
88it("preserves actionable T3 errors wrapped by turn startup", () => {
89 for (const error of [
90 new ContextHandoffBudgetError(),
91 new ContextHandoffBackgroundWorkError(),
92 new ProviderSetupError({
93 instanceId: ProviderInstanceId.make("codex"),
94 operation: "runtime",
95 detail: "Codex 0.144.3 does not work with this T3 Code release.",
96 }),
97 ]) {
98 const cause = new ProviderAdapter.ProviderAdapterTurnStartError({
99 driver: ProviderDriverKind.make("codex"),
100 threadId: ThreadId.make("thread:handoff-error"),
101 providerThreadId: ProviderThreadId.make("provider-thread:handoff-error"),
102 runId: RunId.make("run:handoff-error"),
103 cause: error,
104 });
105 assert.equal(makeProviderFailure({ cause: Cause.fail(cause) }).message, error.message);
106 }
107});
108
109it("keeps unrecognized reasons and provider text out of category messages", () => {
110 for (const cause of [
111 "Private launch command",
112 { _tag: "ProviderAdapterProtocolError", detail: "Pi model 'private-config' is invalid" },
113 { _tag: "ProviderAdapterProtocolError", reason: "private-config" },
114 { _tag: "ProviderAdapterProtocolError", reason: "constructor" },
115 { _tag: "constructor", cause: "private-config" },
116 ]) {
117 assert.equal(
118 makeProviderFailure({ cause: { _tag: "ProviderAdapterTurnStartError", cause } }).message,
119 "The provider could not start this turn. Retry the turn; if it keeps failing, check the provider setup and server logs.",
120 );
121 }
122 assert.equal(
123 makeProviderFailure({
124 cause: { _tag: "ProviderAdapterProtocolError", reason: "previous_turn_active" },
125 }).message,
126 "Provider turn failed.",
127 );
128});
129
130it("uses the innermost category without carrying an outer reason into it", () => {
131 assert.equal(
132 makeProviderFailure({
133 cause: {
134 _tag: "ProviderAdapterTurnStartError",
135 cause: {
136 _tag: "ProviderAdapterProtocolError",
137 reason: "previous_turn_active",
138 cause: Cause.fail({ _tag: "ProviderAdapterOpenSessionError" }),
139 },
140 },
141 }).message,
142 "The provider session could not be opened. Check that the provider is installed and signed in, then retry the turn.",
143 );
144});
145
146it("does not expose defect text nested inside a known error category", () => {
147 const failure = makeProviderFailure({
148 cause: {
149 _tag: "ProviderAdapterEventStreamError",
150 cause: new Error(
151 `Session rejected: Bearer nested-secret https://user:pass@example.test/path?token=secret ${"x".repeat(5000)}`,
152 ),
153 },
154 });
155 assert.include(failure.message, "provider event stream closed unexpectedly");
156 assert.notInclude(failure.message, "Session rejected:");
157 assert.notInclude(failure.message, "nested-secret");
158 assert.notInclude(failure.message, "user:pass");
159 assert.notInclude(failure.message, "token=secret");
160 assert.isBelow(failure.message.length, MAX_PROVIDER_FAILURE_MESSAGE_LENGTH);
161});
162
163it("handles cyclic causes and throwing accessors", () => {
164 const cyclic: { message: string; cause?: unknown } = {
165 message: "Provider disconnected. Retry the turn.",
166 };
167 cyclic.cause = cyclic;
168 assert.equal(makeProviderFailure({ cause: cyclic }).message, "Provider turn failed.");
169 assert.equal(
170 makeProviderFailure({
171 cause: {
172 get message() {
173 throw new Error("unreadable");
174 },
175 get cause() {
176 throw new Error("unreadable");
177 },
178 },
179 }).message,
180 "Provider turn failed.",
181 );
182});
183
184it("does not serialize arbitrary provider causes", () => {
185 const failure = makeProviderFailure({
186 cause: {
187 payload: { authorization: "Bearer nested-secret" },
188 stack: "private provider stack",
189 },
190 class: "transport_error",
191 });
192
193 assert.deepEqual(failure, {
194 class: "transport_error",
195 message: "Provider turn failed.",
196 code: null,
197 retryable: null,
198 });
199});
200
201it.effect("keys terminal failure items by provider turn across retries and fallback paths", () =>
202 Effect.gen(function* () {
203 const idAllocator = yield* IdAllocator.IdAllocatorV2;
204 const driver = ProviderDriverKind.make("codex");
205 const runId = RunId.make("run:provider-failure-id");
206 const base = {
207 idAllocator,
208 driver,
209 threadId: ThreadId.make("thread:provider-failure-id"),
210 runId,
211 nodeId: NodeId.make("node:provider-failure-id"),
212 providerThreadId: ProviderThreadId.make("provider-thread:provider-failure-id"),
213 itemOrdinal: 101,
214 failure: makeProviderFailure({ message: "Provider failed" }),
215 occurredAt: DateTime.makeUnsafe("2026-06-22T12:00:00.000Z"),
216 } as const;
217 const firstTurnId = ProviderTurnId.make("provider-turn:provider-failure-id:first");
218 const secondTurnId = ProviderTurnId.make("provider-turn:provider-failure-id:second");
219
220 const firstAttempt = makeProviderFailureTurnItem({
221 ...base,
222 providerTurnId: firstTurnId,
223 });
224 const retriedAttempt = makeProviderFailureTurnItem({
225 ...base,
226 providerTurnId: secondTurnId,
227 });
228 const ingestorFallback = makeProviderFailureTurnItem({
229 ...base,
230 runId: null,
231 nodeId: null,
232 providerTurnId: firstTurnId,
233 });
234
235 assert.notEqual(firstAttempt.id, retriedAttempt.id);
236 assert.equal(firstAttempt.id, ingestorFallback.id);
237 assert.equal(firstAttempt.ordinal, 101);
238 }).pipe(Effect.provide(IdAllocator.layer)),
239);