Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

failure.test.ts

215 lines7.5 KB

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