Skip to content

Commit ba5362c

Browse files
Huijirocursoragent
andcommitted
feat(cursor): compact threads with native SDK summarize
Replace the /compress rewrite path with agent.summarize, project summary lifecycle into compaction turn items, and clear instruction-delivery state after compact so the next turn reinjects prompts. Co-authored-by: Cursor <[email protected]>
1 parent cf220e6 commit ba5362c

6 files changed

Lines changed: 602 additions & 15 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.testkit.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ export function makeCursorAgentSdkReplayRunner(
288288

289289
const waitForRun = <Error>(
290290
runId: string,
291-
sendInput: CursorAgentSdk.CursorAgentSdkSendInput<Error>,
291+
runInput: {
292+
readonly onDelta?: (update: InteractionUpdate) => Effect.Effect<void, Error>;
293+
},
292294
): Effect.Effect<RunResult, CursorAgentSdkReplayError> =>
293295
Effect.gen(function* () {
294296
while (true) {
@@ -364,7 +366,7 @@ export function makeCursorAgentSdkReplayRunner(
364366
);
365367
}
366368
advance();
367-
yield* (sendInput.onDelta?.(frame.update) ?? Effect.void).pipe(
369+
yield* (runInput.onDelta?.(frame.update) ?? Effect.void).pipe(
368370
Effect.mapError((cause) =>
369371
recordFailure(
370372
new CursorReplayRuntimeError({
@@ -446,6 +448,34 @@ export function makeCursorAgentSdkReplayRunner(
446448
},
447449
catch: (cause) => replayRunnerError(transcript, cause, "replay.session.send"),
448450
}),
451+
summarize: (summarizeInput) =>
452+
Effect.try({
453+
try: () => {
454+
assertOutbound({
455+
type: "run.summarize",
456+
});
457+
const started = consumeInbound("run.started");
458+
const run: CursorAgentSdk.CursorAgentSdkRun = {
459+
runId: started.runId,
460+
agentId: started.agentId,
461+
wait: waitForRun(started.runId, summarizeInput ?? {}).pipe(
462+
Effect.mapError((cause) =>
463+
replayRunnerError(transcript, cause, "replay.run.wait"),
464+
),
465+
),
466+
cancel: Effect.try({
467+
try: () =>
468+
assertOutbound({
469+
type: "run.cancel",
470+
runId: started.runId,
471+
}),
472+
catch: (cause) => replayRunnerError(transcript, cause, "replay.run.cancel"),
473+
}),
474+
};
475+
return run;
476+
},
477+
catch: (cause) => replayRunnerError(transcript, cause, "replay.session.summarize"),
478+
}),
449479
listMessages: Effect.try({
450480
try: () => {
451481
assertOutbound({

‎docs/user/cursor.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ The adapter supports:
5050
- thread-scoped T3 Code MCP tools alongside ambient Cursor MCP/plugins from disk;
5151
- image attachments;
5252
- interruption, queued app messages, and orchestrator-owned interrupt/restart steering;
53-
- provider conversation snapshots through `Agent.messages.list()`;
53+
- native conversation compaction;
54+
- provider conversation snapshots;
5455
- Cursor `task` subagents, projected as read-only child app threads with their tool activity and
5556
final result.
5657

‎packages/provider-cursor/src/server/CursorAgentSdk.ts‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ export interface CursorAgentSdkSendInput<Error> {
9191
readonly onDelta?: (update: InteractionUpdate) => Effect.Effect<void, Error>;
9292
}
9393

94+
export interface CursorAgentSdkSummarizeInput<Error> {
95+
readonly onDelta?: (update: InteractionUpdate) => Effect.Effect<void, Error>;
96+
}
97+
9498
export interface CursorAgentSdkRun {
9599
readonly runId: string;
96100
readonly agentId: string;
@@ -103,6 +107,9 @@ export interface CursorAgentSdkSession {
103107
readonly send: <Error>(
104108
input: CursorAgentSdkSendInput<Error>,
105109
) => Effect.Effect<CursorAgentSdkRun, CursorAgentSdkRunnerError>;
110+
readonly summarize: <Error>(
111+
input?: CursorAgentSdkSummarizeInput<Error>,
112+
) => Effect.Effect<CursorAgentSdkRun, CursorAgentSdkRunnerError>;
106113
readonly listMessages: Effect.Effect<ReadonlyArray<AgentMessage>, CursorAgentSdkRunnerError>;
107114
readonly close: Effect.Effect<void, CursorAgentSdkRunnerError>;
108115
}
@@ -176,6 +183,13 @@ export type CursorAgentSdkProtocolLogEvent =
176183
readonly options: CursorAgentSdkLoggedSendOptions;
177184
};
178185
}
186+
| {
187+
readonly direction: "outgoing";
188+
readonly stage: "decoded";
189+
readonly payload: {
190+
readonly type: "run.summarize";
191+
};
192+
}
179193
| {
180194
readonly direction: "incoming";
181195
readonly stage: "decoded";
@@ -318,6 +332,25 @@ export function loggedCursorSendOptions(
318332
};
319333
}
320334

335+
function summaryMessageToInteractionUpdate(message: {
336+
readonly type: string;
337+
readonly status?: string;
338+
readonly fallback?: boolean;
339+
}): InteractionUpdate | undefined {
340+
if (message.type !== "summary") return undefined;
341+
if (message.status === "running") {
342+
return { type: "summary-started" };
343+
}
344+
if (message.status === "completed" || message.status === "error") {
345+
return {
346+
type: "summary-completed",
347+
...(message.status === "error" ? { failed: true } : {}),
348+
...(message.fallback === true ? { fallback: true } : {}),
349+
};
350+
}
351+
return undefined;
352+
}
353+
321354
function makeCursorAgentSdkProtocolLogger(input: {
322355
readonly nativeEventLogger: ProviderEventLoggers.EventNdjsonLogger | undefined;
323356
readonly threadId: ThreadId;
@@ -419,6 +452,106 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
419452
};
420453
let abandonedRunRecoveryAvailable = input.operation === "resume";
421454

455+
const wrapRun = Effect.fnUntraced(function* <Error>(input: {
456+
readonly run: Awaited<ReturnType<typeof agent.send>>;
457+
readonly method: string;
458+
readonly onDelta?: (update: InteractionUpdate) => Effect.Effect<void, Error>;
459+
readonly pumpStream?: boolean;
460+
}) {
461+
const context = yield* Effect.context();
462+
let callbackFailure: { readonly cause: unknown } | undefined;
463+
let callbackChain = Promise.resolve();
464+
const dispatchUpdate = (update: InteractionUpdate): Promise<void> => {
465+
callbackChain = callbackChain
466+
.then(() => {
467+
if (callbackFailure !== undefined) {
468+
return;
469+
}
470+
return Effect.runPromiseWith(context)(
471+
log({
472+
direction: "incoming",
473+
stage: "decoded",
474+
payload: {
475+
type: "interaction.update",
476+
runId: input.run.id,
477+
update,
478+
},
479+
}).pipe(Effect.andThen(input.onDelta?.(update) ?? Effect.void)),
480+
);
481+
})
482+
.catch((cause) => {
483+
callbackFailure ??= { cause };
484+
});
485+
return callbackChain;
486+
};
487+
488+
// Keep the stream pump off callbackChain — awaiting dispatchUpdate from inside a
489+
// callbackChain task deadlocks when the next link waits for that same task to finish.
490+
const pumpDone =
491+
input.pumpStream === true
492+
? (async () => {
493+
for await (const message of input.run.stream()) {
494+
const update = summaryMessageToInteractionUpdate(message);
495+
if (update !== undefined) {
496+
await dispatchUpdate(update);
497+
}
498+
}
499+
})().catch((cause) => {
500+
callbackFailure ??= { cause };
501+
})
502+
: Promise.resolve();
503+
504+
return {
505+
runId: input.run.id,
506+
agentId: input.run.agentId,
507+
wait: Effect.tryPromise({
508+
try: async () => {
509+
const result = await input.run.wait();
510+
await Promise.all([callbackChain, pumpDone]);
511+
if (callbackFailure !== undefined) {
512+
throw callbackFailure.cause;
513+
}
514+
return result;
515+
},
516+
catch: (cause) => runnerError(cause, `${input.method}.wait`),
517+
}).pipe(
518+
Effect.tap((result) =>
519+
log({
520+
direction: "incoming",
521+
stage: "decoded",
522+
payload: {
523+
type: "run.completed",
524+
result,
525+
},
526+
}),
527+
),
528+
),
529+
cancel: log({
530+
direction: "outgoing",
531+
stage: "decoded",
532+
payload: {
533+
type: "run.cancel",
534+
runId: input.run.id,
535+
},
536+
}).pipe(
537+
Effect.andThen(
538+
Effect.tryPromise({
539+
try: async () => {
540+
try {
541+
await input.run.cancel();
542+
} catch (cause) {
543+
if (!isCursorCancellationError(cause)) {
544+
throw cause;
545+
}
546+
}
547+
},
548+
catch: (cause) => runnerError(cause, `${input.method}.cancel`),
549+
}),
550+
),
551+
),
552+
} satisfies CursorAgentSdkRun;
553+
});
554+
422555
return {
423556
agentId: agent.agentId,
424557
send: Effect.fn("CursorAgentSdkSession.send")(function* (sendInput) {
@@ -575,6 +708,32 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
575708
),
576709
} satisfies CursorAgentSdkRun;
577710
}),
711+
summarize: Effect.fn("CursorAgentSdkSession.summarize")(function* (summarizeInput) {
712+
yield* log({
713+
direction: "outgoing",
714+
stage: "decoded",
715+
payload: { type: "run.summarize" },
716+
});
717+
const run = yield* Effect.tryPromise({
718+
try: () => agent.summarize(),
719+
catch: (cause) => runnerError(cause, "run.summarize"),
720+
});
721+
yield* log({
722+
direction: "incoming",
723+
stage: "decoded",
724+
payload: {
725+
type: "run.started",
726+
runId: run.id,
727+
agentId: run.agentId,
728+
},
729+
});
730+
return yield* wrapRun({
731+
run,
732+
method: "run.summarize",
733+
...(summarizeInput?.onDelta === undefined ? {} : { onDelta: summarizeInput.onDelta }),
734+
pumpStream: true,
735+
});
736+
}),
578737
listMessages: log({
579738
direction: "outgoing",
580739
stage: "decoded",

0 commit comments

Comments
 (0)