Skip to content

Commit 46140b2

Browse files
committed
fix(grok): map text generation failures to safe details
1 parent a42c797 commit 46140b2

3 files changed

Lines changed: 61 additions & 31 deletions

File tree

‎apps/server/src/provider/testFixtures/grok-text-mock-agent.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ for await (const line of NodeReadline.createInterface({ input: process.stdin }))
1616
write({
1717
jsonrpc: "2.0",
1818
id: request.id,
19-
error: { code: -32003, message: process.env.T3_ACP_PROMPT_ERROR_MESSAGE },
19+
error: {
20+
code: Number(process.env.T3_ACP_PROMPT_ERROR_CODE ?? -32003),
21+
message: process.env.T3_ACP_PROMPT_ERROR_MESSAGE,
22+
},
2023
});
2124
continue;
2225
}

‎apps/server/src/textGeneration/GrokTextGeneration.test.ts‎

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,29 +134,42 @@ it.layer(layerGrokTextGenerationTest)("GrokTextGeneration", (it) => {
134134
),
135135
);
136136

137-
it.effect("keeps Grok's error message when the prompt request fails", () =>
138-
withFakeAcpGrok(
139-
{ T3_ACP_PROMPT_ERROR_MESSAGE: "Grok usage limit reached. Try again later." },
140-
(textGeneration) =>
141-
Effect.gen(function* () {
142-
const error = yield* textGeneration
143-
.generateThreadTitle({
144-
cwd: process.cwd(),
145-
message: "the lint job is red",
146-
modelSelection: createModelSelection(
147-
ProviderInstanceId.make("grok"),
148-
"grok-mock-alt",
149-
),
150-
})
151-
.pipe(Effect.flip);
152-
expect(error.detail).toBe(
153-
"Grok ACP request failed: Grok usage limit reached. Try again later.",
154-
);
155-
}),
156-
),
137+
it.effect("maps ACP failures to safe details and preserves their cause", () =>
138+
Effect.gen(function* () {
139+
for (const [code, detail] of [
140+
[-32003, "Grok usage limit reached. Try again later."],
141+
[-32000, "Grok authentication is required."],
142+
[-32603, "Grok ACP request failed."],
143+
] as const) {
144+
yield* withFakeAcpGrok(
145+
{
146+
T3_ACP_PROMPT_ERROR_MESSAGE: "private provider payload " + "x".repeat(4096),
147+
T3_ACP_PROMPT_ERROR_CODE: String(code),
148+
},
149+
(textGeneration) =>
150+
Effect.gen(function* () {
151+
const error = yield* textGeneration
152+
.generateThreadTitle({
153+
cwd: process.cwd(),
154+
message: "the lint job is red",
155+
modelSelection: createModelSelection(
156+
ProviderInstanceId.make("grok"),
157+
"grok-mock-alt",
158+
),
159+
})
160+
.pipe(Effect.flip);
161+
expect(error.detail).toBe(detail);
162+
expect(error.cause).toMatchObject({
163+
code,
164+
errorMessage: "private provider payload " + "x".repeat(4096),
165+
});
166+
}),
167+
);
168+
}
169+
}),
157170
);
158171

159-
it.effect("keeps Grok's error message when switching models fails", () =>
172+
it.effect("explains rejected model settings without copying provider text", () =>
160173
withFakeAcpGrok({}, (textGeneration) =>
161174
Effect.gen(function* () {
162175
const error = yield* textGeneration
@@ -169,9 +182,8 @@ it.layer(layerGrokTextGenerationTest)("GrokTextGeneration", (it) => {
169182
),
170183
})
171184
.pipe(Effect.flip);
172-
expect(error.detail).toBe(
173-
"Failed to set Grok ACP base model for text generation: Unknown Grok model",
174-
);
185+
expect(error.cause).toMatchObject({ code: -32602, errorMessage: "Unknown Grok model" });
186+
expect(error.detail).toBe("Grok rejected the text generation model settings.");
175187
}),
176188
),
177189
);

‎packages/provider-grok/src/server/textGeneration.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,27 @@ import {
1919
resolveGrokAcpBaseModelId,
2020
} from "./acpSupport.ts";
2121

22+
import { xAiRateLimitedErrorCode } from "./xaiAcpExtension.ts";
23+
2224
const GROK_TIMEOUT_MS = 180_000;
2325

2426
const isTextGenerationError = Schema.is(TextGenerationError);
2527
const isAcpRequestError = Schema.is(EffectAcpErrors.AcpRequestError);
2628

29+
function grokAcpFailureDetail(cause: EffectAcpErrors.AcpError, fallback: string) {
30+
if (!isAcpRequestError(cause)) return fallback;
31+
switch (cause.code) {
32+
case xAiRateLimitedErrorCode:
33+
return "Grok usage limit reached. Try again later.";
34+
case -32000:
35+
return "Grok authentication is required.";
36+
case -32602:
37+
return "Grok rejected the text generation model settings.";
38+
default:
39+
return fallback;
40+
}
41+
}
42+
2743
export const makeGrokTextGeneration = Effect.fn("makeGrokTextGeneration")(function* (
2844
grokSettings: GrokSettings,
2945
environment: NodeJS.ProcessEnv = process.env,
@@ -76,9 +92,10 @@ export const makeGrokTextGeneration = Effect.fn("makeGrokTextGeneration")(functi
7692
mapError: (cause) =>
7793
new TextGenerationError({
7894
operation,
79-
detail: isAcpRequestError(cause)
80-
? `Failed to set Grok ACP base model for text generation: ${cause.errorMessage}`
81-
: "Failed to set Grok ACP base model for text generation.",
95+
detail: grokAcpFailureDetail(
96+
cause,
97+
"Failed to set Grok ACP base model for text generation.",
98+
),
8299
cause,
83100
}),
84101
});
@@ -101,9 +118,7 @@ export const makeGrokTextGeneration = Effect.fn("makeGrokTextGeneration")(functi
101118
? cause
102119
: new TextGenerationError({
103120
operation,
104-
detail: isAcpRequestError(cause)
105-
? `Grok ACP request failed: ${cause.errorMessage}`
106-
: "Grok ACP request failed.",
121+
detail: grokAcpFailureDetail(cause, "Grok ACP request failed."),
107122
cause,
108123
}),
109124
),

0 commit comments

Comments
 (0)