Open History to see the last commit
1const maxLineLength = 4096;
2// Cursor also uses RetriableError for agent-loop failures; preserve those diagnostics.
3const transportError =
4 /^Error: (?:RetriableError: (?!\[internal\]).+|ConnectError: \[(?:unavailable|aborted|deadline_exceeded)\].*)$/;
5const serverError = "Something went wrong communicating with the server. Please try again.";
6
7interface ReplyState {
8 disqualified: boolean;
9 failure: string | undefined;
10}
11
12function consumeLine(state: ReplyState, line: string) {
13 if (state.disqualified) return;
14 const text = line.trimEnd();
15 if (transportError.test(text) || text === serverError) {
16 state.failure = text;
17 } else if (text.trim() !== "" && !(state.failure && /^\s+at\s/.test(text))) {
18 // An explanation or code sample can quote the same diagnostic. Only
19 // classify an assistant item consisting entirely of a transport dump.
20 state.disqualified = true;
21 state.failure = undefined;
22 }
23}
24
25export class CursorTransportFailure {
26 private state: ReplyState = { disqualified: false, failure: undefined };
27 private line = "";
28
29 push(text: string) {
30 for (const [index, part] of text.split("\n").entries()) {
31 if (this.state.disqualified) return;
32 if (index > 0) {
33 consumeLine(this.state, this.line);
34 this.line = "";
35 }
36 if (this.line.length + part.length > maxLineLength) {
37 this.state.disqualified = true;
38 this.state.failure = undefined;
39 this.line = "";
40 return;
41 }
42 this.line += part;
43 }
44 }
45
46 get failure() {
47 const state = { ...this.state };
48 consumeLine(state, this.line);
49 return state.failure;
50 }
51}