Skip to content

Commit 265976c

Browse files
committed
fix(cursor): settle declined steers and replay native delivery
1 parent 5ee4963 commit 265976c

9 files changed

Lines changed: 162 additions & 43 deletions

‎apps/server/scripts/record-cursor-agent-sdk-replay-fixture.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const RECORDINGS = {
5555
message_steering: {
5656
prompts: [MESSAGE_STEERING_INITIAL_PROMPT, MESSAGE_STEERING_STEER_PROMPT],
5757
output: "../src/orchestration-v2/testkit/fixtures/message_steering/cursor_transcript.ndjson",
58-
interruptAfterRunStartPromptIndex: 0,
58+
steerAfterRunStartPromptIndex: 0,
5959
},
6060
provider_thread_resume: {
6161
prompts: [PROVIDER_THREAD_RESUME_FIRST_PROMPT, PROVIDER_THREAD_RESUME_SECOND_PROMPT],
@@ -183,8 +183,8 @@ const recordScenario = Effect.fn("recordScenario")(function* (
183183
...("interruptAfterToolStart" in recording
184184
? { interruptAfterToolStart: recording.interruptAfterToolStart }
185185
: {}),
186-
...("interruptAfterRunStartPromptIndex" in recording
187-
? { interruptAfterRunStartPromptIndex: recording.interruptAfterRunStartPromptIndex }
186+
...("steerAfterRunStartPromptIndex" in recording
187+
? { steerAfterRunStartPromptIndex: recording.steerAfterRunStartPromptIndex }
188188
: {}),
189189
...("restartBeforePromptIndex" in recording
190190
? { restartBeforePromptIndex: recording.restartBeforePromptIndex }

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

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as HostProcess from "@t3tools/shared/HostProcess";
2-
import type { InteractionUpdate, RunResult } from "@cursor/sdk";
2+
import type { InteractionUpdate, RunResult, SteerAckOutcome } from "@cursor/sdk";
33
import * as NodeServices from "@effect/platform-node/NodeServices";
44
import {
55
ProviderReplayEntry,
@@ -213,6 +213,7 @@ function replayRunnerError(
213213
export function makeCursorAgentSdkReplayRunner(
214214
transcript: CursorAgentSdkReplayTranscript,
215215
): CursorAgentSdk.CursorAgentSdkRunner["Service"] {
216+
const steerAcks = new Map<string, Deferred.Deferred<SteerAckOutcome>>();
216217
let cursor = 0;
217218
let failure: CursorAgentSdkReplayError | null = null;
218219
let cursorAdvanced = makeSignal();
@@ -306,16 +307,19 @@ export function makeCursorAgentSdkReplayRunner(
306307
);
307308
}
308309
if (entry.type === "expect_outbound") {
309-
const expectedCancel = {
310-
type: "run.cancel",
311-
runId,
312-
};
313-
if (!sameFrame(entry.frame, expectedCancel)) {
310+
const type =
311+
typeof entry.frame === "object" && entry.frame !== null
312+
? Reflect.get(entry.frame, "type")
313+
: undefined;
314+
if (
315+
(type !== "run.cancel" && type !== "run.steer") ||
316+
Reflect.get(entry.frame as object, "runId") !== runId
317+
) {
314318
return yield* recordFailure(
315319
new CursorReplayFrameMismatchError({
316320
scenario: transcript.scenario,
317321
cursor,
318-
expected: expectedCancel,
322+
expected: { type: "run.cancel | run.steer", runId },
319323
actual: entry.frame,
320324
}),
321325
);
@@ -378,6 +382,23 @@ export function makeCursorAgentSdkReplayRunner(
378382
yield* Effect.yieldNow;
379383
continue;
380384
}
385+
if (frame.type === "run.steered") {
386+
const ack = steerAcks.get(frame.runId);
387+
if (ack === undefined || frame.runId !== runId) {
388+
return yield* recordFailure(
389+
new CursorReplayFrameMismatchError({
390+
scenario: transcript.scenario,
391+
cursor,
392+
expected: { runId },
393+
actual: frame,
394+
}),
395+
);
396+
}
397+
advance();
398+
steerAcks.delete(runId);
399+
yield* Deferred.succeed(ack, frame.outcome);
400+
continue;
401+
}
381402
if (frame.type === "run.completed") {
382403
if (frame.result.id !== runId) {
383404
return yield* recordFailure(
@@ -433,6 +454,16 @@ export function makeCursorAgentSdkReplayRunner(
433454
replayRunnerError(transcript, cause, "replay.run.wait"),
434455
),
435456
),
457+
steer: Effect.fn("CursorReplay.steer")(function* (text) {
458+
const ack = yield* Deferred.make<SteerAckOutcome>();
459+
steerAcks.set(started.runId, ack);
460+
yield* Effect.try({
461+
try: () =>
462+
assertOutbound({ type: "run.steer", runId: started.runId, text }),
463+
catch: (cause) => replayRunnerError(transcript, cause, "replay.run.steer"),
464+
});
465+
return yield* Deferred.await(ack);
466+
}),
436467
cancel: Effect.try({
437468
try: () =>
438469
assertOutbound({
@@ -663,6 +694,7 @@ interface CursorReplayRecordingInput {
663694
readonly apiKey?: string;
664695
readonly interruptAfterToolStart?: boolean;
665696
readonly interruptAfterRunStartPromptIndex?: number;
697+
readonly steerAfterRunStartPromptIndex?: number;
666698
readonly restartBeforePromptIndex?: number;
667699
}
668700

@@ -809,6 +841,11 @@ export const recordCursorAgentSdkReplayTranscript = Effect.fn(
809841
prompt: string,
810842
index: number,
811843
) {
844+
if (
845+
input.steerAfterRunStartPromptIndex !== undefined &&
846+
index === input.steerAfterRunStartPromptIndex + 1
847+
)
848+
return;
812849
const interruptAfterRunStart = input.interruptAfterRunStartPromptIndex === index;
813850
const firstUpdate = yield* Deferred.make<void>();
814851
const toolStarted = yield* Deferred.make<void>();
@@ -831,6 +868,15 @@ export const recordCursorAgentSdkReplayTranscript = Effect.fn(
831868
});
832869
sent = true;
833870
const waiting = yield* run.wait.pipe(Effect.forkChild({ startImmediately: true }));
871+
if (input.steerAfterRunStartPromptIndex === index) {
872+
yield* awaitSignal(firstUpdate, "Cursor SDK activity before steering");
873+
const steerText = input.prompts[index + 1];
874+
if (run.steer === undefined || steerText === undefined)
875+
return yield* invalid("Steering requires a following prompt and Run.steer.");
876+
const outcome = yield* run.steer(steerText);
877+
if (outcome !== "complete_delivered")
878+
return yield* invalid("The running turn did not accept the steer.");
879+
}
834880
if (interruptAfterRunStart) {
835881
yield* awaitSignal(firstUpdate, "Cursor SDK run activity before interrupt");
836882
yield* run.cancel;

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6083,8 +6083,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
60836083
projection.thread.archivedAt !== null ||
60846084
projection.thread.deletedAt !== null ||
60856085
(yield* stopReachedRun(command, command.threadId, source.id))
6086-
)
6086+
) {
6087+
yield* emit(
6088+
events,
6089+
command,
6090+
)({
6091+
type: "thread.metadata-updated",
6092+
threadId: command.threadId,
6093+
occurredAt: yield* DateTime.now,
6094+
payload: projection.thread,
6095+
});
60876096
return;
6097+
}
60886098
}
60896099
if (command.manualContinuationOfRunId !== undefined) {
60906100
const source = projection.runs.find((run) => run.id === command.manualContinuationOfRunId);

‎apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as Queue from "effect/Queue";
2525
import * as Stream from "effect/Stream";
2626
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
2727
import * as EffectWorker from "./EffectWorker.ts";
28+
import * as EffectOutbox from "./EffectOutbox.ts";
2829
import * as EventSink from "./EventSink.ts";
2930
import * as Orchestrator from "./Orchestrator.ts";
3031
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
@@ -1208,6 +1209,13 @@ it.effect.each(["complete", "stop-before-decline", "stop-after-decline"] as cons
12081209
yield* worker.drain();
12091210
const pending = yield* orchestrator.getThreadProjection(threadId);
12101211
const queued = pending.runs.find((run) => run.id !== first.runId);
1212+
const effects = yield* (yield* EffectOutbox.EffectOutboxV2).listByCommandId(
1213+
CommandId.make("steer"),
1214+
);
1215+
assert.deepEqual(
1216+
effects.map((effect) => [effect.request.type, effect.status]),
1217+
[["provider-turn.steer", "succeeded"]],
1218+
);
12111219
assert.equal(started.length, 1);
12121220
if (timing === "stop-before-decline") {
12131221
assert.isUndefined(queued);

‎apps/server/src/orchestration-v2/testkit/CursorReplayFixtures.integration.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ describe("Cursor replay fixtures", () => {
2121
runReplayFixture({
2222
fixtureName: "message_steering",
2323
buildInput: messageRestartInput,
24-
driver: cursorSteeringProvider,
24+
driver: {
25+
...cursorSteeringProvider,
26+
transcriptFile: new URL(
27+
"./fixtures/message_steering/cursor_restart_transcript.ndjson",
28+
import.meta.url,
29+
),
30+
},
2531
harness: CursorOrchestratorReplayHarness,
2632
}),
2733
);

‎apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export function layerWithRegistry<Error>(
286286
} = {},
287287
): Layer.Layer<
288288
| Orchestrator.OrchestratorV2
289+
| EffectOutbox.EffectOutboxV2
289290
| EffectWorker.OrchestrationEffectWorkerV2
290291
| EventSink.EventSinkV2
291292
| ProviderSessionManager.ProviderSessionManagerV2
@@ -322,12 +323,13 @@ export function layerWithRegistry<Error>(
322323
),
323324
),
324325
).pipe(Layer.orDie);
326+
const layerEffectOutboxProvided = EffectOutbox.layer.pipe(Layer.provide(layerDatabase));
325327
const layerStores = Layer.mergeAll(
326328
EventStore.layer,
327329
ProjectionStore.layer,
328330
ProjectStore.layer,
329331
CommandReceiptStore.layer,
330-
EffectOutbox.layer,
332+
layerEffectOutboxProvided,
331333
TurnItemPositionStore.layer,
332334
).pipe(Layer.provide(layerDatabase));
333335
const layerEventSinkProvided = EventSink.layerFromStores.pipe(
@@ -536,6 +538,7 @@ export function layerWithRegistry<Error>(
536538
Layer.provide(Layer.merge(layerStores, layerEffectExecutorProvided)),
537539
);
538540
const layerReplayRuntime = Layer.mergeAll(
541+
layerEffectOutboxProvided,
539542
layerOrchestratorProvided,
540543
layerProviderSessionManagerProvided,
541544
layerEffectWorkerProvided,

‎apps/server/src/orchestration-v2/testkit/fixtures/message_steering/cursor_output.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,28 @@ export function assertCursorMessageSteeringOutput(
3434
assertAssistantTextIncludes(projection, "steering fixture observed");
3535

3636
assert.lengthOf(projection.runs, 1, "steering must preserve the app run");
37+
const nativeSteer = transcript.entries.some(
38+
(entry) =>
39+
entry.type === "expect_outbound" &&
40+
typeof entry.frame === "object" &&
41+
entry.frame !== null &&
42+
Reflect.get(entry.frame, "type") === "run.steer",
43+
);
3744
assert.deepEqual(
3845
projection.attempts.map((attempt) => [attempt.reason, attempt.status]),
39-
[
40-
["initial", "superseded"],
41-
["steering_restart", "completed"],
42-
],
46+
nativeSteer
47+
? [["initial", "completed"]]
48+
: [
49+
["initial", "superseded"],
50+
["steering_restart", "completed"],
51+
],
4352
);
4453
assert.deepEqual(
4554
projection.providerTurns.map((turn) => turn.status),
46-
["interrupted", "completed"],
55+
nativeSteer ? ["completed"] : ["interrupted", "completed"],
4756
);
48-
assert.equal(projection.runs[0]?.activeAttemptId, projection.attempts[1]?.id);
49-
assert.equal(projection.runs[0]?.rootNodeId, projection.attempts[1]?.rootNodeId);
57+
assert.equal(projection.runs[0]?.activeAttemptId, projection.attempts.at(-1)?.id);
58+
assert.equal(projection.runs[0]?.rootNodeId, projection.attempts.at(-1)?.rootNodeId);
5059
assert.isFalse(
5160
projection.turnItems.some(
5261
(item) => item.type === "run_interrupt_request" || item.type === "run_interrupt_result",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{"type":"transcript_start","provider":"cursor","protocol":"cursor-agent-sdk.local","version":"1","scenario":"message_steering","metadata":{"generatedBy":"recordCursorAgentSdkReplayTranscript","nativeAgentId":"agent-575fd75e-3fd7-412e-9b74-36f3a5aa5371"}}
2+
{"type":"expect_outbound","label":"agent.open","frame":{"type":"agent.open","operation":"create","options":{"model":{"id":"composer-2.5"},"hasName":true,"mode":"agent","local":{"hasCwd":true,"autoReview":false,"settingSources":["project","user","team","mdm","plugins"],"sandboxEnabled":false,"enableAgentRetries":true}}}}
3+
{"type":"emit_inbound","label":"agent.opened","frame":{"type":"agent.opened","agentId":"agent-575fd75e-3fd7-412e-9b74-36f3a5aa5371"}}
4+
{"type":"expect_outbound","label":"run.start:1","frame":{"type":"run.start","message":"Respond with exactly: steering fixture initial response","options":{"model":{"id":"composer-2.5"},"mode":"agent"}}}
5+
{"type":"emit_inbound","label":"run.started:1","frame":{"type":"run.started","runId":"run-046488f0-f621-4d59-976c-c4c935ad1282","agentId":"agent-575fd75e-3fd7-412e-9b74-36f3a5aa5371"}}
6+
{"type":"emit_inbound","label":"thinking-delta","frame":{"type":"interaction.update","runId":"run-046488f0-f621-4d59-976c-c4c935ad1282","update":{"type":"thinking-delta","text":"The user requested the"}}}
7+
{"type":"expect_outbound","label":"run.cancel:1","frame":{"type":"run.cancel","runId":"run-046488f0-f621-4d59-976c-c4c935ad1282"}}
8+
{"type":"emit_inbound","label":"run.completed:1","frame":{"type":"run.completed","result":{"id":"run-046488f0-f621-4d59-976c-c4c935ad1282","requestId":"d3b97820-1cf3-448d-a4b1-5fa7edc21460","status":"cancelled","model":{"id":"composer-2.5"},"durationMs":3177}}}
9+
{"type":"expect_outbound","label":"run.start:2","frame":{"type":"run.start","message":"Your previous turn was interrupted and is missing from your history. Some of its work may already be done. Its request was:\n\nRespond with exactly: steering fixture initial response\n\nThe new message, which may continue or change that request:\n\nActually, respond with exactly: steering fixture observed","options":{"model":{"id":"composer-2.5"},"mode":"agent"}}}
10+
{"type":"emit_inbound","label":"run.started:2","frame":{"type":"run.started","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","agentId":"agent-575fd75e-3fd7-412e-9b74-36f3a5aa5371"}}
11+
{"type":"emit_inbound","label":"thinking-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"thinking-delta","text":"The response will be"}}}
12+
{"type":"emit_inbound","label":"token-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"token-delta","tokens":5}}}
13+
{"type":"emit_inbound","label":"thinking-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"thinking-delta","text":" exactly: \"steering "}}}
14+
{"type":"emit_inbound","label":"token-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"token-delta","tokens":5}}}
15+
{"type":"emit_inbound","label":"thinking-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"thinking-delta","text":"fixture observed\"."}}}
16+
{"type":"emit_inbound","label":"token-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"token-delta","tokens":4}}}
17+
{"type":"emit_inbound","label":"thinking-completed","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"thinking-completed","thinkingDurationMs":2}}}
18+
{"type":"emit_inbound","label":"text-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"text-delta","text":"ste"}}}
19+
{"type":"emit_inbound","label":"token-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"token-delta","tokens":1}}}
20+
{"type":"emit_inbound","label":"text-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"text-delta","text":"ering fixture observed"}}}
21+
{"type":"emit_inbound","label":"token-delta","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"token-delta","tokens":5}}}
22+
{"type":"emit_inbound","label":"step-completed","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"step-completed","stepId":2,"stepDurationMs":789}}}
23+
{"type":"emit_inbound","label":"turn-ended","frame":{"type":"interaction.update","runId":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","update":{"type":"turn-ended","usage":{"inputTokens":11626,"outputTokens":38,"cacheReadTokens":11584,"cacheWriteTokens":0}}}}
24+
{"type":"emit_inbound","label":"run.completed:2","frame":{"type":"run.completed","result":{"id":"run-e3b66d77-b2d4-4791-be2b-1a35c8e997d1","requestId":"ed3603d4-2d59-409d-a131-da4a48002bed","status":"finished","result":"steering fixture observed","model":{"id":"composer-2.5"},"durationMs":1746,"usage":{"inputTokens":11626,"outputTokens":38,"cacheReadTokens":11584,"cacheWriteTokens":0,"totalTokens":23248}}}}
25+
{"type":"expect_outbound","label":"agent.close","frame":{"type":"agent.close","agentId":"agent-575fd75e-3fd7-412e-9b74-36f3a5aa5371"}}

0 commit comments

Comments
 (0)