Open History to see the last commit
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 DateTime from "effect/DateTime";
11import * as Effect from "effect/Effect";
12
13import {
14 makeProviderFailure,
15 makeProviderFailureTurnItem,
16 MAX_PROVIDER_FAILURE_CODE_LENGTH,
17 MAX_PROVIDER_FAILURE_MESSAGE_LENGTH,
18} from "./ProviderFailure.ts";
19import { IdAllocatorV2, layer as idAllocatorLayer } from "./IdAllocator.ts";
20
21it("redacts credentials and URL secrets from provider failures", () => {
22 const failure = makeProviderFailure({
23 message:
24 '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',
25 code: "provider_rejected",
26 class: "provider_error",
27 });
28
29 assert.equal(failure.class, "provider_error");
30 assert.equal(failure.code, "provider_rejected");
31 assert.include(failure.message, "[REDACTED]");
32 assert.include(failure.message, "https://example.test/path");
33 assert.notInclude(failure.message, "bearer-secret");
34 assert.notInclude(failure.message, "user:pass");
35 assert.notInclude(failure.message, "url-secret");
36 assert.notInclude(failure.message, "json-secret");
37 assert.notInclude(failure.message, "key-secret");
38 assert.notInclude(failure.message, "sk-abcdefghijklmnop");
39});
40
41it("replaces unsafe control characters without stripping whitespace", () => {
42 const failure = makeProviderFailure({ message: "before\u0000\u0007\t\nafter\u007f" });
43
44 assert.equal(failure.message, "before \t\nafter");
45});
46
47it("bounds provider-controlled failure strings", () => {
48 const failure = makeProviderFailure({
49 message: "m".repeat(MAX_PROVIDER_FAILURE_MESSAGE_LENGTH + 500),
50 code: "c".repeat(MAX_PROVIDER_FAILURE_CODE_LENGTH + 50),
51 });
52
53 assert.equal(failure.message.length, MAX_PROVIDER_FAILURE_MESSAGE_LENGTH);
54 assert.equal(failure.code?.length, MAX_PROVIDER_FAILURE_CODE_LENGTH);
55 assert.match(failure.message, /…$/u);
56 assert.match(failure.code ?? "", /…$/u);
57});
58
59it("does not split a surrogate pair at the truncation boundary", () => {
60 const failure = makeProviderFailure({
61 message: `${"a".repeat(MAX_PROVIDER_FAILURE_MESSAGE_LENGTH - 2)}🚀tail`,
62 });
63
64 assert.equal(failure.message.length, MAX_PROVIDER_FAILURE_MESSAGE_LENGTH - 1);
65 assert.equal(failure.message.at(-1), "…");
66 assert.notMatch(failure.message.slice(0, -1), /[\uD800-\uDBFF]$/u);
67});
68
69it("does not serialize arbitrary provider causes", () => {
70 const failure = makeProviderFailure({
71 cause: {
72 payload: { authorization: "Bearer nested-secret" },
73 stack: "private provider stack",
74 },
75 class: "transport_error",
76 });
77
78 assert.deepEqual(failure, {
79 class: "transport_error",
80 message: "Provider turn failed.",
81 code: null,
82 retryable: null,
83 });
84});
85
86it.effect("keys terminal failure items by provider turn across retries and fallback paths", () =>
87 Effect.gen(function* () {
88 const idAllocator = yield* IdAllocatorV2;
89 const driver = ProviderDriverKind.make("codex");
90 const runId = RunId.make("run:provider-failure-id");
91 const base = {
92 idAllocator,
93 driver,
94 threadId: ThreadId.make("thread:provider-failure-id"),
95 runId,
96 nodeId: NodeId.make("node:provider-failure-id"),
97 providerThreadId: ProviderThreadId.make("provider-thread:provider-failure-id"),
98 itemOrdinal: 101,
99 failure: makeProviderFailure({ message: "Provider failed" }),
100 occurredAt: DateTime.makeUnsafe("2026-06-22T12:00:00.000Z"),
101 } as const;
102 const firstTurnId = ProviderTurnId.make("provider-turn:provider-failure-id:first");
103 const secondTurnId = ProviderTurnId.make("provider-turn:provider-failure-id:second");
104
105 const firstAttempt = makeProviderFailureTurnItem({
106 ...base,
107 providerTurnId: firstTurnId,
108 });
109 const retriedAttempt = makeProviderFailureTurnItem({
110 ...base,
111 providerTurnId: secondTurnId,
112 });
113 const ingestorFallback = makeProviderFailureTurnItem({
114 ...base,
115 runId: null,
116 nodeId: null,
117 providerTurnId: firstTurnId,
118 });
119
120 assert.notEqual(firstAttempt.id, retriedAttempt.id);
121 assert.equal(firstAttempt.id, ingestorFallback.id);
122 assert.equal(firstAttempt.ordinal, 101);
123 }).pipe(Effect.provide(idAllocatorLayer)),
124);