fix(cursor): settle requests before turn completion
This commit is contained in:
1 parent
7251f1a1f1
commit
a1d871198a
2 files changed
+205
-3
No files matched your search
@@ -60,6 +60,78 @@ exec ${JSON.stringify(mockAgentCommand)} ${mockAgentArgs.map((arg) => JSON.strin
|
||||
return wrapperPath;
|
||||
}
|
||||
|
||||
async function makeTerminalCallbackWrapper(kind: "permission" | "question") {
|
||||
const dir = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "cursor-acp-terminal-"));
|
||||
const wrapperPath = NodePath.join(dir, "fake-agent.mjs");
|
||||
const callback =
|
||||
kind === "permission"
|
||||
? {
|
||||
jsonrpc: "2.0",
|
||||
id: 9001,
|
||||
method: "session/request_permission",
|
||||
params: {
|
||||
sessionId: "terminal-callback-session",
|
||||
toolCall: {
|
||||
toolCallId: "terminal-tool-call",
|
||||
title: "Terminal",
|
||||
kind: "execute",
|
||||
status: "pending",
|
||||
content: [
|
||||
{
|
||||
type: "content",
|
||||
content: { type: "text", text: "run terminal command" },
|
||||
},
|
||||
],
|
||||
},
|
||||
options: [
|
||||
{ optionId: "allow-once", name: "Allow once", kind: "allow_once" },
|
||||
{ optionId: "reject-once", name: "Reject", kind: "reject_once" },
|
||||
],
|
||||
},
|
||||
}
|
||||
: {
|
||||
jsonrpc: "2.0",
|
||||
id: 9002,
|
||||
method: "cursor/ask_question",
|
||||
params: {
|
||||
toolCallId: "terminal-question",
|
||||
title: "Question",
|
||||
questions: [
|
||||
{
|
||||
id: "scope",
|
||||
prompt: "Which scope?",
|
||||
options: [{ id: "workspace", label: "Workspace" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const script = `#!/usr/bin/env node
|
||||
import readline from "node:readline";
|
||||
const callback = ${JSON.stringify(callback)};
|
||||
const kind = ${JSON.stringify(kind)};
|
||||
const send = (message) => process.stdout.write(JSON.stringify(message) + "\\n");
|
||||
readline.createInterface({ input: process.stdin }).on("line", (line) => {
|
||||
const message = JSON.parse(line);
|
||||
if (!("method" in message)) return;
|
||||
if (message.method === "initialize") {
|
||||
send({ jsonrpc: "2.0", id: message.id, result: { protocolVersion: 1, agentCapabilities: {} } });
|
||||
} else if (message.method === "session/new") {
|
||||
send({ jsonrpc: "2.0", id: message.id, result: { sessionId: "terminal-callback-session" } });
|
||||
} else if (message.method === "session/prompt") {
|
||||
const response = { jsonrpc: "2.0", id: message.id, result: { stopReason: "end_turn" } };
|
||||
const messages = kind === "question" ? [response, callback] : [callback];
|
||||
process.stdout.write(messages.map(JSON.stringify).join("\\n") + "\\n");
|
||||
if (kind === "permission") setTimeout(() => send(response), 10);
|
||||
} else if (message.id !== undefined) {
|
||||
send({ jsonrpc: "2.0", id: message.id, result: {} });
|
||||
}
|
||||
});
|
||||
`;
|
||||
await NodeFSP.writeFile(wrapperPath, script, "utf8");
|
||||
await NodeFSP.chmod(wrapperPath, 0o755);
|
||||
return wrapperPath;
|
||||
}
|
||||
|
||||
async function makeProbeWrapper(
|
||||
requestLogPath: string,
|
||||
argvLogPath: string,
|
||||
@@ -1047,6 +1119,105 @@ cursorAdapterTestLayer("CursorAdapterLive", (it) => {
|
||||
yield* adapter.stopSession(threadId);
|
||||
}),
|
||||
);
|
||||
it.effect("resolves a pending permission before completing a normally returned turn", () =>
|
||||
Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
const settings = yield* ServerSettingsService;
|
||||
const threadId = ThreadId.make("cursor-terminal-permission");
|
||||
const events: Array<ProviderRuntimeEvent> = [];
|
||||
const completed = yield* Deferred.make<void>();
|
||||
const wrapperPath = yield* Effect.promise(() => makeTerminalCallbackWrapper("permission"));
|
||||
yield* settings.updateSettings({ providers: { cursor: { binaryPath: wrapperPath } } });
|
||||
|
||||
const eventsFiber = yield* Stream.runForEach(adapter.streamEvents, (event) => {
|
||||
if (String(event.threadId) !== String(threadId)) return Effect.void;
|
||||
events.push(event);
|
||||
return event.type === "turn.completed"
|
||||
? Deferred.succeed(completed, undefined).pipe(Effect.ignore)
|
||||
: Effect.void;
|
||||
}).pipe(Effect.forkChild);
|
||||
|
||||
yield* adapter.startSession({
|
||||
threadId,
|
||||
provider: ProviderDriverKind.make("cursor"),
|
||||
cwd: process.cwd(),
|
||||
runtimeMode: "approval-required",
|
||||
});
|
||||
yield* adapter.sendTurn({
|
||||
threadId,
|
||||
input: "finish without permission response",
|
||||
attachments: [],
|
||||
});
|
||||
yield* Deferred.await(completed);
|
||||
yield* Fiber.interrupt(eventsFiber);
|
||||
|
||||
const relevant = events.filter(
|
||||
(event) =>
|
||||
event.type === "request.opened" ||
|
||||
event.type === "request.resolved" ||
|
||||
event.type === "turn.completed",
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
relevant.map((event) => event.type),
|
||||
["request.opened", "request.resolved", "turn.completed"],
|
||||
);
|
||||
const resolved = relevant[1];
|
||||
assert.equal(resolved?.type, "request.resolved");
|
||||
if (resolved?.type === "request.resolved") assert.equal(resolved.payload.decision, "cancel");
|
||||
yield* adapter.stopSession(threadId);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("resolves a pending question before completing a normally returned turn", () =>
|
||||
Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
const settings = yield* ServerSettingsService;
|
||||
const threadId = ThreadId.make("cursor-terminal-question");
|
||||
const events: Array<ProviderRuntimeEvent> = [];
|
||||
const completed = yield* Deferred.make<void>();
|
||||
const wrapperPath = yield* Effect.promise(() => makeTerminalCallbackWrapper("question"));
|
||||
yield* settings.updateSettings({ providers: { cursor: { binaryPath: wrapperPath } } });
|
||||
|
||||
const eventsFiber = yield* Stream.runForEach(adapter.streamEvents, (event) => {
|
||||
if (String(event.threadId) !== String(threadId)) return Effect.void;
|
||||
events.push(event);
|
||||
return event.type === "turn.completed"
|
||||
? Deferred.succeed(completed, undefined).pipe(Effect.ignore)
|
||||
: Effect.void;
|
||||
}).pipe(Effect.forkChild);
|
||||
|
||||
yield* adapter.startSession({
|
||||
threadId,
|
||||
provider: ProviderDriverKind.make("cursor"),
|
||||
cwd: process.cwd(),
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
yield* adapter.sendTurn({
|
||||
threadId,
|
||||
input: "finish without question response",
|
||||
attachments: [],
|
||||
});
|
||||
yield* Deferred.await(completed);
|
||||
yield* Fiber.interrupt(eventsFiber);
|
||||
|
||||
const relevant = events.filter(
|
||||
(event) =>
|
||||
event.type === "user-input.requested" ||
|
||||
event.type === "user-input.resolved" ||
|
||||
event.type === "turn.completed",
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
relevant.map((event) => event.type),
|
||||
["user-input.requested", "user-input.resolved", "turn.completed"],
|
||||
);
|
||||
const resolved = relevant[1];
|
||||
assert.equal(resolved?.type, "user-input.resolved");
|
||||
if (resolved?.type === "user-input.resolved")
|
||||
assert.deepStrictEqual(resolved.payload.answers, {});
|
||||
yield* adapter.stopSession(threadId);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("stopping a session settles pending approval waits", () =>
|
||||
Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
|
||||
@@ -115,11 +115,13 @@ export interface CursorAdapterLiveOptions {
|
||||
|
||||
interface PendingApproval {
|
||||
readonly decision: Deferred.Deferred<ProviderApprovalDecision>;
|
||||
readonly resolutionOffered: Deferred.Deferred<void>;
|
||||
readonly kind: string | "unknown";
|
||||
}
|
||||
|
||||
interface PendingUserInput {
|
||||
readonly answers: Deferred.Deferred<ProviderUserInputAnswers>;
|
||||
readonly resolutionOffered: Deferred.Deferred<void>;
|
||||
}
|
||||
|
||||
interface CursorSessionContext {
|
||||
@@ -166,6 +168,24 @@ function settlePendingUserInputsAsEmptyAnswers(
|
||||
);
|
||||
}
|
||||
|
||||
function settlePendingCallbacksBeforeTurnCompletion(
|
||||
pendingApprovals: ReadonlyMap<ApprovalRequestId, PendingApproval>,
|
||||
pendingUserInputs: ReadonlyMap<ApprovalRequestId, PendingUserInput>,
|
||||
): Effect.Effect<void> {
|
||||
const approvals = Array.from(pendingApprovals.values());
|
||||
const userInputs = Array.from(pendingUserInputs.values());
|
||||
return Effect.gen(function* () {
|
||||
yield* settlePendingApprovalsAsCancelled(pendingApprovals);
|
||||
yield* settlePendingUserInputsAsEmptyAnswers(pendingUserInputs);
|
||||
yield* Effect.forEach(approvals, (pending) => Deferred.await(pending.resolutionOffered), {
|
||||
discard: true,
|
||||
});
|
||||
yield* Effect.forEach(userInputs, (pending) => Deferred.await(pending.resolutionOffered), {
|
||||
discard: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
@@ -583,7 +603,8 @@ export function makeCursorAdapter(
|
||||
const requestId = ApprovalRequestId.make(yield* randomUUIDv4);
|
||||
const runtimeRequestId = RuntimeRequestId.make(requestId);
|
||||
const answers = yield* Deferred.make<ProviderUserInputAnswers>();
|
||||
pendingUserInputs.set(requestId, { answers });
|
||||
const resolutionOffered = yield* Deferred.make<void>();
|
||||
pendingUserInputs.set(requestId, { answers, resolutionOffered });
|
||||
yield* offerRuntimeEvent({
|
||||
type: "user-input.requested",
|
||||
...(yield* makeEventStamp()),
|
||||
@@ -599,7 +620,6 @@ export function makeCursorAdapter(
|
||||
},
|
||||
});
|
||||
const resolved = yield* Deferred.await(answers);
|
||||
pendingUserInputs.delete(requestId);
|
||||
yield* offerRuntimeEvent({
|
||||
type: "user-input.resolved",
|
||||
...(yield* makeEventStamp()),
|
||||
@@ -609,6 +629,8 @@ export function makeCursorAdapter(
|
||||
requestId: runtimeRequestId,
|
||||
payload: { answers: resolved },
|
||||
});
|
||||
yield* Deferred.succeed(resolutionOffered, undefined).pipe(Effect.ignore);
|
||||
pendingUserInputs.delete(requestId);
|
||||
return { answers: resolved };
|
||||
}),
|
||||
),
|
||||
@@ -687,8 +709,10 @@ export function makeCursorAdapter(
|
||||
const requestId = ApprovalRequestId.make(yield* randomUUIDv4);
|
||||
const runtimeRequestId = RuntimeRequestId.make(requestId);
|
||||
const decision = yield* Deferred.make<ProviderApprovalDecision>();
|
||||
const resolutionOffered = yield* Deferred.make<void>();
|
||||
pendingApprovals.set(requestId, {
|
||||
decision,
|
||||
resolutionOffered,
|
||||
kind: permissionRequest.kind,
|
||||
});
|
||||
yield* offerRuntimeEvent(
|
||||
@@ -710,7 +734,6 @@ export function makeCursorAdapter(
|
||||
}),
|
||||
);
|
||||
const resolved = yield* Deferred.await(decision);
|
||||
pendingApprovals.delete(requestId);
|
||||
yield* offerRuntimeEvent(
|
||||
makeAcpRequestResolvedEvent({
|
||||
stamp: yield* makeEventStamp(),
|
||||
@@ -722,6 +745,8 @@ export function makeCursorAdapter(
|
||||
decision: resolved,
|
||||
}),
|
||||
);
|
||||
yield* Deferred.succeed(resolutionOffered, undefined).pipe(Effect.ignore);
|
||||
pendingApprovals.delete(requestId);
|
||||
return {
|
||||
outcome:
|
||||
resolved === "cancel"
|
||||
@@ -1030,6 +1055,12 @@ export function makeCursorAdapter(
|
||||
// Only the last remaining prompt settles the turn — a steer-
|
||||
// superseded prompt resolving (usually cancelled) while another is
|
||||
// in flight or pending must leave the merged turn running.
|
||||
if (ctx.promptsInFlight === 1) {
|
||||
yield* settlePendingCallbacksBeforeTurnCompletion(
|
||||
ctx.pendingApprovals,
|
||||
ctx.pendingUserInputs,
|
||||
);
|
||||
}
|
||||
if (ctx.promptsInFlight === 1) {
|
||||
yield* offerRuntimeEvent({
|
||||
type: "turn.completed",
|
||||
|
||||
Reference in new issue
Block a user