cursor-delegate-mcp 1.12.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +7 -2
- package/src/acp-client.js +45 -12
- package/src/acp-enums.js +15 -0
- package/src/delegate.js +128 -341
- package/src/doctor.js +34 -3
- package/src/errors.js +20 -0
- package/src/jsonrpc.js +38 -9
- package/src/proc.js +2 -2
- package/src/request-router.js +16 -11
- package/src/server.js +108 -113
- package/src/session-supervisor.js +1 -5
- package/src/spawn.js +5 -0
- package/src/turn-state.js +193 -0
package/src/spawn.js
CHANGED
|
@@ -9,6 +9,11 @@ export function splitArgs(raw) {
|
|
|
9
9
|
return args;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
// shell:true on win32 is what lets a .cmd/.ps1 launcher resolve at all, and it means the
|
|
13
|
+
// command and args are parsed by cmd.exe. Safe only because both come from the environment,
|
|
14
|
+
// which is already as trusted as this process: anything that can set them can run code here
|
|
15
|
+
// anyway. Routing host- or caller-supplied values into either variable would turn this into a
|
|
16
|
+
// command-injection path, so don't.
|
|
12
17
|
export function resolveAcpSpawn() {
|
|
13
18
|
const command = process.env.ACP_AGENT_COMMAND || "cursor-agent";
|
|
14
19
|
const args = process.env.ACP_AGENT_ARGS
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// Everything that describes the turn in progress, in one object because it has to be wiped as
|
|
2
|
+
// one: session/load replays the previous turn as ordinary update frames, so anything left
|
|
3
|
+
// behind leaks into the next result. This used to be a dozen loose variables cleared by a
|
|
4
|
+
// hand-written block before each prompt, where forgetting a line was a silent stale-data bug
|
|
5
|
+
// on resume — and the list grew every time a field was added. Fields are declared in exactly
|
|
6
|
+
// one place now, freshFields(), which is also what reset() applies.
|
|
7
|
+
|
|
8
|
+
const MAX_OUTPUT = 10 * 1024 * 1024;
|
|
9
|
+
const TRUNCATION_MARKER = "\n\n[output truncated at 10MB]";
|
|
10
|
+
|
|
11
|
+
// Cutting a UTF-16 string at an arbitrary index can split a surrogate pair and leave a lone
|
|
12
|
+
// half-character — ill-formed Unicode that strict JSON consumers reject or mangle. Step back
|
|
13
|
+
// one unit when the boundary would land inside a pair.
|
|
14
|
+
function cutAtCodePoint(s, n) {
|
|
15
|
+
if (n >= s.length) return s;
|
|
16
|
+
const cu = s.charCodeAt(n - 1);
|
|
17
|
+
return s.slice(0, cu >= 0xd800 && cu <= 0xdbff ? n - 1 : n);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const isTerminalToolStatus = (status) =>
|
|
21
|
+
status === "completed" || status === "failed" || status === "cancelled";
|
|
22
|
+
|
|
23
|
+
// Each stream reports its newest complete sentence, at most one per throttle window.
|
|
24
|
+
// Capital-letter boundary: cursor-agent thoughts arrive as sentences with no separator.
|
|
25
|
+
const SENTENCE_END = /[.!?](?=\s|[A-Z])|\n/;
|
|
26
|
+
const MARKDOWN_LINE = /^(?:[|#>`~*_=+-]|\d+[.)]\s)/;
|
|
27
|
+
/**
|
|
28
|
+
* @param {string} prefix
|
|
29
|
+
* @param {((message: string) => void) | undefined} onProgress
|
|
30
|
+
* @param {number} throttleMs
|
|
31
|
+
*/
|
|
32
|
+
function progressStream(prefix, onProgress, throttleMs) {
|
|
33
|
+
let buf = "", pending = null, lastEmit = 0;
|
|
34
|
+
const flush = (force) => {
|
|
35
|
+
if (pending === null || (!force && Date.now() - lastEmit < throttleMs)) return;
|
|
36
|
+
lastEmit = Date.now();
|
|
37
|
+
try { onProgress?.(prefix + pending); } catch {}
|
|
38
|
+
pending = null;
|
|
39
|
+
};
|
|
40
|
+
const take = (s) => {
|
|
41
|
+
const line = s.replace(/\s+/g, " ").trim().slice(0, 200);
|
|
42
|
+
if (line.length > 3 && !MARKDOWN_LINE.test(line)) pending = line;
|
|
43
|
+
flush(false);
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
push(text) {
|
|
47
|
+
buf += text;
|
|
48
|
+
for (let m; (m = SENTENCE_END.exec(buf)); buf = buf.slice(m.index + 1)) {
|
|
49
|
+
take(buf.slice(0, m.index + 1));
|
|
50
|
+
}
|
|
51
|
+
if (buf.length > 300) { take(buf); buf = ""; }
|
|
52
|
+
},
|
|
53
|
+
end() { take(buf); buf = ""; flush(true); },
|
|
54
|
+
reset() { buf = ""; pending = null; },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// The single declaration of what a turn accumulates. Anything added here is reset for free.
|
|
59
|
+
function freshFields() {
|
|
60
|
+
return {
|
|
61
|
+
resultChunks: [],
|
|
62
|
+
resultLength: 0,
|
|
63
|
+
truncated: false,
|
|
64
|
+
// Text superseded by a later tool call is normally a preamble ("Inspecting the
|
|
65
|
+
// implementation.") and returning it would be inventing a summary. But the rule cannot
|
|
66
|
+
// tell a preamble from the whole answer: an agent that replies and then runs one more
|
|
67
|
+
// command has its entire reply discarded, and the caller gets "" with stopReason end_turn
|
|
68
|
+
// and no error. So keep the last discarded segment and hand it back only when nothing
|
|
69
|
+
// survived — labelled, never blended with a real final message.
|
|
70
|
+
discardedResult: "",
|
|
71
|
+
sawToolCall: false,
|
|
72
|
+
collectingPostToolResult: false,
|
|
73
|
+
activeToolCalls: new Set(),
|
|
74
|
+
planEntries: [],
|
|
75
|
+
planOverview: undefined,
|
|
76
|
+
planDetail: undefined,
|
|
77
|
+
// merge:false replaces the list, merge:true upserts by id. Entries always arrive complete,
|
|
78
|
+
// so a keyed set is enough — no field-level merging.
|
|
79
|
+
todos: new Map(),
|
|
80
|
+
sawTodoFrame: false,
|
|
81
|
+
touched: new Set(),
|
|
82
|
+
lastToolLabel: null,
|
|
83
|
+
sessionTitle: undefined,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @param {{ onProgress?: (message: string) => void, progressThrottleMs?: number }} [opts]
|
|
89
|
+
*/
|
|
90
|
+
export function makeTurnState({ onProgress, progressThrottleMs = 2000 } = {}) {
|
|
91
|
+
const state = {
|
|
92
|
+
...freshFields(),
|
|
93
|
+
thoughts: progressStream("thinking: ", onProgress, progressThrottleMs),
|
|
94
|
+
messages: progressStream("Cursor: ", onProgress, progressThrottleMs),
|
|
95
|
+
|
|
96
|
+
reset() {
|
|
97
|
+
Object.assign(state, freshFields());
|
|
98
|
+
state.thoughts.reset();
|
|
99
|
+
state.messages.reset();
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
// The result text as it stands, truncation marker included.
|
|
103
|
+
text() {
|
|
104
|
+
const joined = state.resultChunks.join("");
|
|
105
|
+
return state.truncated ? joined + TRUNCATION_MARKER : joined;
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
resetResult() {
|
|
109
|
+
if (state.resultLength > 0) state.discardedResult = state.resultChunks.join("");
|
|
110
|
+
state.resultChunks.length = 0;
|
|
111
|
+
state.resultLength = 0;
|
|
112
|
+
state.truncated = false;
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
appendResult(text) {
|
|
116
|
+
if (state.truncated) return;
|
|
117
|
+
const remaining = MAX_OUTPUT - state.resultLength;
|
|
118
|
+
if (text.length <= remaining) {
|
|
119
|
+
state.resultChunks.push(text);
|
|
120
|
+
state.resultLength += text.length;
|
|
121
|
+
} else if (remaining > 0) {
|
|
122
|
+
const cut = cutAtCodePoint(text, remaining);
|
|
123
|
+
state.resultChunks.push(cut);
|
|
124
|
+
state.resultLength += cut.length;
|
|
125
|
+
state.truncated = true;
|
|
126
|
+
} else {
|
|
127
|
+
state.truncated = true;
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
startTool(toolCallId, status) {
|
|
132
|
+
state.sawToolCall = true;
|
|
133
|
+
state.collectingPostToolResult = false;
|
|
134
|
+
state.resetResult();
|
|
135
|
+
if (toolCallId != null && !isTerminalToolStatus(status)) state.activeToolCalls.add(toolCallId);
|
|
136
|
+
if (isTerminalToolStatus(status) && state.activeToolCalls.size === 0) state.collectingPostToolResult = true;
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
updateToolStatus(toolCallId, status) {
|
|
140
|
+
if (!status) return;
|
|
141
|
+
if (!state.sawToolCall) {
|
|
142
|
+
state.sawToolCall = true;
|
|
143
|
+
state.resetResult();
|
|
144
|
+
}
|
|
145
|
+
if (isTerminalToolStatus(status)) {
|
|
146
|
+
if (toolCallId != null) state.activeToolCalls.delete(toolCallId);
|
|
147
|
+
if (state.activeToolCalls.size === 0 && !state.collectingPostToolResult) {
|
|
148
|
+
// Discard any message text emitted while tools were still running.
|
|
149
|
+
// A duplicate or late terminal update must not wipe an already-collected final message.
|
|
150
|
+
state.resetResult();
|
|
151
|
+
state.collectingPostToolResult = true;
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
state.collectingPostToolResult = false;
|
|
155
|
+
state.resetResult();
|
|
156
|
+
if (toolCallId != null) state.activeToolCalls.add(toolCallId);
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
// True while a message chunk still belongs in the result: before any tool ran, or after
|
|
161
|
+
// the last one finished.
|
|
162
|
+
collectingResult() {
|
|
163
|
+
return !state.sawToolCall || (state.collectingPostToolResult && state.activeToolCalls.size === 0);
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
todoLabel() {
|
|
167
|
+
const entries = [...state.todos.values()].filter((t) => typeof t?.content === "string");
|
|
168
|
+
if (!entries.length) return null;
|
|
169
|
+
const i = entries.findIndex((t) => t.status === "in_progress");
|
|
170
|
+
if (i !== -1) return `todo ${i + 1}/${entries.length}: ${entries[i].content}`;
|
|
171
|
+
const done = entries.filter((t) => t.status === "completed").length;
|
|
172
|
+
return `todos ${done}/${entries.length} complete`;
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
recordTodos({ todos: incoming, merge }) {
|
|
176
|
+
if (!Array.isArray(incoming)) return;
|
|
177
|
+
state.sawTodoFrame = true;
|
|
178
|
+
if (merge === false) state.todos = new Map();
|
|
179
|
+
for (const t of incoming) {
|
|
180
|
+
if (t?.id === undefined || t?.id === null) continue;
|
|
181
|
+
state.todos.set(String(t.id), t);
|
|
182
|
+
}
|
|
183
|
+
const label = state.todoLabel();
|
|
184
|
+
if (label) { try { onProgress?.(label.slice(0, 200)); } catch {} }
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
recordCreatePlan(body) {
|
|
188
|
+
if (body?.overview !== undefined) state.planOverview = body.overview;
|
|
189
|
+
if (body?.plan !== undefined) state.planDetail = body.plan;
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
return state;
|
|
193
|
+
}
|