fix(cursor): log close attempts before execution

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-08-14 11:23:28 +02:00
1 parent 29f716986b
commit 5f7840b92f
2 files changed
+117 -12

No files matched your search

@@ -13,6 +13,8 @@ import {
} from "./CursorAgentSdk.ts";
const cursorSdkMock = vi.hoisted(() => {
const closeExecutionOrder: Array<string> = [];
let agentCloseFailure: unknown;
const runWait = vi.fn(async () => ({
id: "run-cursor-agent-sdk-test",
requestId: "request-cursor-agent-sdk-test",
@@ -22,7 +24,12 @@ const cursorSdkMock = vi.hoisted(() => {
durationMs: 1,
}));
const runCancel = vi.fn(async () => {});
const agentClose = vi.fn(() => {});
const agentClose = vi.fn(() => {
closeExecutionOrder.push("sdk:agent.close");
if (agentCloseFailure !== undefined) {
throw agentCloseFailure;
}
});
const send = vi.fn(
async (
_message: unknown,
@@ -50,10 +57,37 @@ const cursorSdkMock = vi.hoisted(() => {
return {
agentClose,
closeExecutionOrder,
create,
runCancel,
runWait,
send,
setAgentCloseFailure: (failure: unknown) => {
agentCloseFailure = failure;
},
};
});
vi.mock("../../provider/Layers/EventNdjsonLogger.ts", async () => {
const Effect = await import("effect/Effect");
return {
makeEventNdjsonLogger: vi.fn(() =>
Effect.succeed({
filePath: "cursor-agent-sdk-test.log",
write: (event: unknown) =>
Effect.sync(() => {
if (
typeof event === "object" &&
event !== null &&
Reflect.get(Reflect.get(event, "event"), "payload")?.type === "agent.close"
) {
cursorSdkMock.closeExecutionOrder.push("protocol:agent.close");
}
}),
close: () => Effect.void,
}),
),
};
});
@@ -77,6 +111,77 @@ const testLayer = cursorAgentSdkRunnerLiveLayer.pipe(
);
describe("CursorAgentSdkRunner", () => {
it.effect("logs agent.close before invoking the Cursor SDK", () =>
Effect.gen(function* () {
cursorSdkMock.agentClose.mockClear();
cursorSdkMock.closeExecutionOrder.length = 0;
cursorSdkMock.setAgentCloseFailure(undefined);
const runner = yield* CursorAgentSdkRunner;
const session = yield* runner.open({
operation: "create",
options: {
model: { id: "default" },
mode: "agent",
local: {
cwd: process.cwd(),
},
},
threadId: ThreadId.make("thread-cursor-agent-sdk-close-order-test"),
providerSessionId: ProviderSessionId.make(
"provider-session-cursor-agent-sdk-close-order-test",
),
});
cursorSdkMock.closeExecutionOrder.length = 0;
yield* session.close;
assert.deepStrictEqual(cursorSdkMock.closeExecutionOrder, [
"protocol:agent.close",
"sdk:agent.close",
]);
assert.equal(cursorSdkMock.agentClose.mock.calls.length, 1);
}).pipe(Effect.provide(testLayer)),
);
it.effect("logs agent.close even when the Cursor SDK close fails", () =>
Effect.gen(function* () {
cursorSdkMock.agentClose.mockClear();
cursorSdkMock.closeExecutionOrder.length = 0;
const closeFailure = new Error("agent close failed");
cursorSdkMock.setAgentCloseFailure(closeFailure);
const runner = yield* CursorAgentSdkRunner;
const session = yield* runner.open({
operation: "create",
options: {
model: { id: "default" },
mode: "agent",
local: {
cwd: process.cwd(),
},
},
threadId: ThreadId.make("thread-cursor-agent-sdk-close-failure-test"),
providerSessionId: ProviderSessionId.make(
"provider-session-cursor-agent-sdk-close-failure-test",
),
});
cursorSdkMock.closeExecutionOrder.length = 0;
const error = yield* Effect.flip(session.close);
assert.instanceOf(error, CursorAgentSdkRunnerError);
assert.equal(error.method, "agent.close");
assert.strictEqual(error.cause, closeFailure);
assert.deepStrictEqual(cursorSdkMock.closeExecutionOrder, [
"protocol:agent.close",
"sdk:agent.close",
]);
assert.equal(cursorSdkMock.agentClose.mock.calls.length, 1);
cursorSdkMock.setAgentCloseFailure(undefined);
}).pipe(Effect.provide(testLayer)),
);
it.effect("surfaces interaction callback failures through run.wait", () =>
Effect.gen(function* () {
cursorSdkMock.create.mockClear();
@@ -505,18 +505,18 @@ export const cursorAgentSdkRunnerLiveLayer: Layer.Layer<CursorAgentSdkRunner, ne
}),
),
),
close: Effect.try({
try: () => agent.close(),
catch: (cause) => runnerError(cause, "agent.close"),
close: log({
direction: "outgoing",
stage: "decoded",
payload: {
type: "agent.close",
agentId: agent.agentId,
},
}).pipe(
Effect.tap(() =>
log({
direction: "outgoing",
stage: "decoded",
payload: {
type: "agent.close",
agentId: agent.agentId,
},
Effect.andThen(
Effect.try({
try: () => agent.close(),
catch: (cause) => runnerError(cause, "agent.close"),
}),
),
),