Skip to content

Commit 96164b5

Browse files
committed
fix: improve socket error handling
1 parent a1f5cfc commit 96164b5

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- Telegram: stop typing after tool results. Thanks @AbhisekBasu1 for PR #322.
2525
- Messages: stop defaulting ack reactions to 👀 when identity emoji is missing.
2626
- Auto-reply: require slash for control commands to avoid false triggers in normal text.
27+
- Auto-reply: flag error payloads and improve Bun socket error messaging. Thanks @emanuelst for PR #331.
2728
- Commands: unify native + text chat commands behind `commands.*` config (Discord/Slack/Telegram). Thanks @thewilloftheshadow for PR #275.
2829
- Auto-reply: treat steer during compaction as a follow-up, queued until compaction completes.
2930
- Auth: lock auth profile refreshes to avoid multi-instance OAuth logouts; keep credentials on refresh failure.

src/auto-reply/reply/agent-runner.heartbeat-typing.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,26 @@ describe("runReplyAgent typing (heartbeat)", () => {
209209
expect(payloads[0]?.text).toContain("count 1");
210210
expect(sessionStore.main.compactionCount).toBe(1);
211211
});
212+
213+
it("rewrites Bun socket errors into friendly text", async () => {
214+
runEmbeddedPiAgentMock.mockImplementationOnce(async () => ({
215+
payloads: [
216+
{
217+
text: "TypeError: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()",
218+
isError: true,
219+
},
220+
],
221+
meta: {},
222+
}));
223+
224+
const { run } = createMinimalRun();
225+
const res = await run();
226+
const payloads = Array.isArray(res) ? res : res ? [res] : [];
227+
expect(payloads.length).toBe(1);
228+
expect(payloads[0]?.text).toContain("LLM connection failed");
229+
expect(payloads[0]?.text).toContain(
230+
"socket connection was closed unexpectedly",
231+
);
232+
expect(payloads[0]?.text).toContain("```");
233+
});
212234
});

src/auto-reply/reply/agent-runner.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ import { extractReplyToTag } from "./reply-tags.js";
3131
import { incrementCompactionCount } from "./session-updates.js";
3232
import type { TypingController } from "./typing.js";
3333

34+
const BUN_FETCH_SOCKET_ERROR_RE = /socket connection was closed unexpectedly/i;
35+
36+
const isBunFetchSocketError = (message?: string) =>
37+
Boolean(message && BUN_FETCH_SOCKET_ERROR_RE.test(message));
38+
39+
const formatBunFetchSocketError = (message: string) => {
40+
const trimmed = message.trim();
41+
return [
42+
"⚠️ LLM connection failed. This could be due to server issues, network problems, or context length exceeded (e.g., with local LLMs like LM Studio). Original error:",
43+
"```",
44+
trimmed || "Unknown error",
45+
"```",
46+
].join("\n");
47+
};
48+
3449
export async function runReplyAgent(params: {
3550
commandBody: string;
3651
followupRun: FollowupRun;
@@ -403,19 +418,8 @@ export async function runReplyAgent(params: {
403418
: payloadArray.flatMap((payload) => {
404419
let text = payload.text;
405420

406-
if (payload.isError) {
407-
// Handle Bun fetch socket connection error that may indicate a context length issue
408-
// Error source: https://github.com/oven-sh/bun/blob/main/src/bun.js/webcore/fetch/FetchTasklet.zig
409-
const isBunFetchSocketError =
410-
text ===
411-
"The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()";
412-
413-
if (isBunFetchSocketError) {
414-
text = `⚠️ LLM connection failed. This could be due to server issues, network problems, or context length exceeded (e.g., with local LLMs like LM Studio). Original error:
415-
\`\`\`
416-
${text || "Unknown error"}
417-
\`\`\``;
418-
}
421+
if (payload.isError && text && isBunFetchSocketError(text)) {
422+
text = formatBunFetchSocketError(text);
419423
}
420424

421425
if (!text || !text.includes("HEARTBEAT_OK"))

src/auto-reply/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export type ReplyPayload = {
1414
mediaUrl?: string;
1515
mediaUrls?: string[];
1616
replyToId?: string;
17+
isError?: boolean;
1718
};

0 commit comments

Comments
 (0)