Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/steer-abort-transfer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Fix loss of Ctrl+S steered messages when the current turn is interrupted.
19 changes: 16 additions & 3 deletions packages/agent-core-v2/src/agent/loop/loopService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,11 @@ export class AgentLoopService extends Disposable implements IAgentLoopService {
return step;
}

private cancelStep(job: TurnJob, step: MutableStep, request: StepRequest, reason?: unknown): boolean {
private cancelStep(job: TurnJob, step: MutableStep, request: StepRequest, reason?: unknown, abortRequest = true): boolean {
if (step.state === 'completed' || step.state === 'failed' || step.state === 'cancelled') return false;
const cancellation = reason ?? userCancellationReason();
step.state = 'cancelled';
request.abort();
if (abortRequest) request.abort();
step.controller?.abort(cancellation);
step.resultControl?.resolve({ type: 'cancelled', reason: cancellation });
return true;
Expand Down Expand Up @@ -599,8 +599,21 @@ export class AgentLoopService extends Disposable implements IAgentLoopService {
const job = this.activeTurnJob?.turn === turn ? this.activeTurnJob : undefined;
if (job === undefined) return;
const reason = result?.type === 'cancelled' ? result.reason : abortError('Turn ended');
const transferred = new Map<string, StepRequest>();
for (const request of job.queue.drain()) {
if (request.state === 'pending' && !request.turnScoped) {
this.standaloneStepQueue.enqueue(request, 'tail');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Attach carryovers to an already queued next turn

When another newTurn request is already in pendingTurns, this moves the unmaterialized steer only to standaloneStepQueue; pumpTurns() then starts the pre-existing turn, whose queue was populated earlier in createPendingTurn(), without moving these standalone requests into it. If that queued turn is the last one, the Ctrl+S message remains pending indefinitely instead of reaching the next turn, and may be injected into an unrelated later prompt. Transfer these requests into the next pending job when one exists, or merge standalone requests when starting every turn.

Useful? React with 👍 / 👎.

transferred.set(request.id, request);
Comment on lines +603 to +606

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Abort carryovers when the loop is disposing

If dispose() is called while an active turn has an unmaterialized non-turn-scoped request, it aborts the turn and drains standaloneStepQueue before the asynchronous turn cleanup reaches this code. This cleanup then transfers the request into the already-drained queue even though disposing prevents any future turn, leaving the request permanently pending and making the disposed loop continue to report pending work. On the disposal path, abort the request rather than transferring it.

Useful? React with 👍 / 👎.

}
}
for (const step of job.steps.values()) {
if (step.state === 'queued' || step.state === 'running') step.cancel(reason);
if (step.state !== 'queued' && step.state !== 'running') continue;
const request = transferred.get(step.id);
if (request === undefined) {
step.cancel(reason);
} else {
this.cancelStep(job, step, request, reason, false);
}
}
this.activeTurnJob = undefined;
this.maybeSettle();
Expand Down
133 changes: 132 additions & 1 deletion packages/agent-core-v2/test/agent/loop/loop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
TurnStepStarted,
} from '#/agent/loop/turnEvents';
import { TurnEnded } from '#/agent/loop/turnOps';
import { RetryStepRequest } from '#/agent/prompt/promptStepRequests';
import { RetryStepRequest, SteerStepRequest } from '#/agent/prompt/promptStepRequests';
import type { ExecutableTool } from '#/tool/toolContract';
import { IAgentToolRegistryService } from '#/agent/toolRegistry/toolRegistry';
import { IEventBus } from '#/app/event/eventBus';
Expand All @@ -34,6 +34,7 @@ import {
type TestAgentOptions,
} from '../../harness';
import { recordingTelemetry, type TelemetryRecord } from '../../app/telemetry/stubs';
import { createReminderStub } from '../../features/reminder/stubs';

type GenerateFn = NonNullable<TestAgentOptions['generate']>;

Expand Down Expand Up @@ -661,6 +662,130 @@ describe('Agent loop', () => {
expect(ctx.llmCalls).toHaveLength(3);
});

it('transfers an unmaterialized non-turn-scoped request into the next turn on cancel', async () => {
let started!: () => void;
const stepEntered = new Promise<void>((resolve) => { started = resolve; });
let release!: () => void;
const canFinish = new Promise<void>((resolve) => { release = resolve; });
const hook = loop.hooks.onWillBeginStep.register('test-non-turn-scoped-transfer', async (_hookCtx, next) => {
started();
await canFinish;
await next();
});

const steerCalls: unknown[] = [];
const steer = new SteerStepRequest(
{ role: 'user', content: [{ type: 'text', text: 'steered' }], toolCalls: [], origin: { kind: 'user' } },
[],
createReminderStub(),
(materialized) => { steerCalls.push(materialized); },
() => {},
);

ctx.mockNextResponse({ type: 'text', text: 'first answer' });
const first = (await loop.enqueue(nextTurnMessage('first')).assigned).turn;
await stepEntered;

const assignment = await loop.enqueue(steer).assigned;
expect(assignment.turn.id).toBe(first.id);

first.cancel(userCancellationReason());
release();
await expect(first.result).resolves.toMatchObject({ type: 'cancelled' });
hook.dispose();

expect(steer.state).toBe('pending');
expect(steerCalls).toHaveLength(0);
await expect(assignment.step.result).resolves.toMatchObject({ type: 'cancelled' });
expect(loop.hasPendingRequests()).toBe(true);

ctx.mockNextResponse({ type: 'text', text: 'second answer' });
const second = (await loop.enqueue(nextTurnMessage('second')).assigned).turn;
await expect(second.result).resolves.toMatchObject({ type: 'completed' });

expect(steerCalls).toHaveLength(1);
expect(steer.state).toBe('materialized');
const texts = llmUserTexts(ctx.llmCalls.at(-1)).join('\n');
expect(texts).toContain('second');
expect(texts).toContain('steered');
expect(texts.indexOf('steered')).toBeGreaterThan(texts.indexOf('second'));
});

it('still aborts turn-scoped queued requests when the turn is cancelled', async () => {
let started!: () => void;
const stepEntered = new Promise<void>((resolve) => { started = resolve; });
let release!: () => void;
const canFinish = new Promise<void>((resolve) => { release = resolve; });
const hook = loop.hooks.onWillBeginStep.register('test-turn-scoped-abort', async (_hookCtx, next) => {
started();
await canFinish;
await next();
});

const scoped = new MessageStepRequest(
{ role: 'user', content: [{ type: 'text', text: 'scoped' }], toolCalls: [], origin: { kind: 'user' } },
{ mergeable: true, admission: 'activeTurnOnly' },
);

ctx.mockNextResponse({ type: 'text', text: 'first answer' });
const first = (await loop.enqueue(nextTurnMessage('first')).assigned).turn;
await stepEntered;

const assignment = await loop.enqueue(scoped).assigned;
expect(assignment.turn.id).toBe(first.id);

first.cancel(userCancellationReason());
release();
await expect(first.result).resolves.toMatchObject({ type: 'cancelled' });
hook.dispose();

expect(scoped.state).toBe('aborted');
await expect(assignment.step.result).resolves.toMatchObject({ type: 'cancelled' });
expect(loop.hasPendingRequests()).toBe(false);
});

it('transfers an unmaterialized non-turn-scoped request when the turn fails', async () => {
let started!: () => void;
const stepEntered = new Promise<void>((resolve) => { started = resolve; });
let fail!: () => void;
const canFail = new Promise<void>((resolve) => { fail = resolve; });
const hook = loop.hooks.onWillBeginStep.register('test-non-turn-scoped-transfer-failure', async () => {
started();
await canFail;
throw new Error('before step failed');
});

const steerCalls: unknown[] = [];
const steer = new SteerStepRequest(
{ role: 'user', content: [{ type: 'text', text: 'steered' }], toolCalls: [], origin: { kind: 'user' } },
[],
createReminderStub(),
(materialized) => { steerCalls.push(materialized); },
() => {},
);

const first = (await loop.enqueue(nextTurnMessage('first')).assigned).turn;
await stepEntered;

const assignment = await loop.enqueue(steer).assigned;
expect(assignment.turn.id).toBe(first.id);

fail();
await expect(first.result).resolves.toMatchObject({ type: 'failed' });
hook.dispose();

expect(steer.state).toBe('pending');
expect(steerCalls).toHaveLength(0);

ctx.mockNextResponse({ type: 'text', text: 'second answer' });
const second = (await loop.enqueue(nextTurnMessage('second')).assigned).turn;
await expect(second.result).resolves.toMatchObject({ type: 'completed' });

expect(steerCalls).toHaveLength(1);
expect(steer.state).toBe('materialized');
expect(llmUserTexts(ctx.llmCalls.at(-1)).join('\n')).toContain('steered');
});

it('refuses a quiescence lease while a turn is active without cancelling it', async () => {
let started!: () => void;
const activeStarted = new Promise<void>((resolve) => {
Expand Down Expand Up @@ -1714,6 +1839,12 @@ function nextTurnMessage(text: string): MessageStepRequest {
);
}

function llmUserTexts(call: TestAgentContext['llmCalls'][number] | undefined): string[] {
return (call?.history ?? [])
.filter((message) => message.role === 'user')
.flatMap((message) => message.content.filter((part) => part.type === 'text').map((part) => part.text));
}

function createTimingRequester(): IAgentLLMRequesterService {
const timing: ModelRequestTiming = {
firstTokenLatencyMs: 100,
Expand Down
Loading