Skip to content
Merged
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
8 changes: 6 additions & 2 deletions apps/kimi-code/src/cli/v2/run-v2-print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ async function runNativeTurn(
// final message.
writer.flushAssistant();
if (result.type === 'completed') {
const skipTurnId = turn.id;
if (skipTurnId === undefined) {
throw new Error('Prompt turn ended before it started');
}
const configService = app.accessor.get(IConfigService);
const taskConfig = resolveAgentTaskConfig(configService);
const goalService = agent.accessor.get(IAgentGoalService);
Expand All @@ -602,7 +606,7 @@ async function runNativeTurn(
countPending: () => countPendingBackgroundTasks(session),
drain: () => drainBackgroundTasks(session, taskConfig?.printWaitCeilingS),
turnEndings,
skipTurnId: turn.id,
skipTurnId,
warn: (message) => stderr.write(`Warning: ${message}\n`),
now: () => Date.now(),
goalActive: () => goalService.getGoal().goal?.status === 'active',
Expand Down Expand Up @@ -1012,7 +1016,7 @@ async function quiesceSessionAgents(
for (;;) {
await Promise.allSettled(promptServices.map((service) => service.drain()));
for (const loop of loops) {
for (const turnId of loop.status().pendingTurnIds) loop.cancel(turnId);
for (const queueId of loop.status().pendingPromptIds) loop.cancelQueued(queueId);
loop.cancel();
}
await Promise.allSettled(loops.map((loop) => loop.settled()));
Expand Down
4 changes: 2 additions & 2 deletions apps/kimi-code/test/cli/v2-run-print.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function makeFakeHarness() {
[
IAgentLoopService,
{
status: vi.fn(() => ({ state: 'idle', pendingTurnIds: [] })),
status: vi.fn(() => ({ state: 'idle', pendingPromptIds: [] })),
cancel: vi.fn(() => false),
settled: vi.fn(async () => {}),
tryAcquireQuiescence: vi.fn(() => ({ dispose: vi.fn() })),
Expand Down Expand Up @@ -757,7 +757,7 @@ describe('runV2Print', () => {
settled: ReturnType<typeof vi.fn>;
tryAcquireQuiescence: ReturnType<typeof vi.fn>;
};
loop.status.mockReturnValue({ state: 'running', pendingTurnIds: [] });
loop.status.mockReturnValue({ state: 'running', pendingPromptIds: [] });
loop.cancel.mockImplementation(() => {
if (!order.includes('cancel')) order.push('cancel');
return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/agent-core-v2/docs/en/event-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Naming conventions for states, events, actions, guards, and invoked actors in ag
Classify an event by **what the receiver does with it**, not by whether it carries a payload.

1. **Command — imperative verb**. Asks the receiver to do something. Examples: `input.submit`, `input.steer`, `input.abort`, `input.remind`, `tool.abort`, `turn.abort`, `turn.drain`, `turn.notify`, `turn.spawn_tools`, `context.reset`.
2. **Fact — past participle**. Reports that something already happened; usually drives transitions or parent-level bookkeeping. Examples: `llm.sent`, `llm.done`, `llm.failed.syntax`, `llm.failed.remote`, `llm.retrying`, `llm.recovering`, `tool.done`, `tool.failed`, `tool.aborted`, `tool.detached`, `turn.reminders_consumed`, `todo.used`. Emitted events are facts by definition: `turn.started`, `turn.done`, `turn.failed`, `turn.aborted`, `turn.aborting`, `agent.created`, `agent.forked`, `agent.switched`, `agent.stopped`, `agent.failed`, `usage.updated`.
2. **Fact — past participle**. Reports that something already happened; usually drives transitions or parent-level bookkeeping. Examples: `llm.sent`, `llm.done`, `llm.failed.syntax`, `llm.failed.remote`, `llm.retrying`, `llm.recovering`, `tool.done`, `tool.failed`, `tool.aborted`, `tool.detached`, `turn.reminders_consumed`, `todo.used`. Emitted events are facts by definition: `turn.started`, `step.started`, `turn.done`, `turn.failed`, `turn.aborted`, `turn.aborting`, `agent.created`, `agent.forked`, `agent.switched`, `agent.stopped`, `agent.failed`, `usage.updated`.
3. **Data stream — noun (the data's own name)**. Delivers one piece of streaming data; the receiver accumulates or forwards it. Grouped under a `streaming` sub-namespace: `llm.streaming.part`, `llm.streaming.headers`, `llm.streaming.usage`, `llm.streaming.finish`, `llm.streaming.message_id`; also `tool.update`, `usage.record`.

Boundary example: `llm.streaming.finish` carries completion metadata that feeds the accumulator (data stream, noun), while `llm.done` is the payload-free stream terminator that drives the transition (fact, past participle).
Expand Down
4 changes: 1 addition & 3 deletions packages/agent-core-v2/docs/state-manifest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// references become '(circular)', and class instances collapse to a '(ClassName)'
// marker — the wire shape of an entry is the JSON projection of the type here.
//
// Index (App: 0 keys · Workspace: 6 keys · Session: 9 keys · Agent: 81 keys)
// Index (App: 0 keys · Workspace: 6 keys · Session: 9 keys · Agent: 80 keys)
// App
// Workspace
// workspaceDirs.ephemeralDirs src/workspace/workspaceDirs/workspaceDirsService.ts
Expand Down Expand Up @@ -76,7 +76,6 @@
// llmRequester.turnConfigs src/agent/llmRequester/llmRequesterService.ts
// loop.disposing src/agent/loop/loopService.ts
// loop.lastRequestTraceId src/agent/loop/loopService.ts
// loop.nextReservedTurnId src/agent/loop/loopService.ts
// mcp.discovery src/agent/mcp/mcpDiscoveryOps.ts
// mcp.discoveryWritesReady src/agent/mcp/mcpService.ts
// mcp.mcpToolsByServer src/agent/mcp/mcpService.ts
Expand Down Expand Up @@ -1313,7 +1312,6 @@ export interface AgentStateSnapshot {
// src/agent/loop/loopService.ts
'loop.disposing': boolean;
'loop.lastRequestTraceId': string | undefined;
'loop.nextReservedTurnId': number | undefined;
// src/agent/loop/turnOps.ts
// replayable · durable — folds: ContextAppendLoopEvent, TurnPrompt, TurnSteer, ContextUndo, ContextApplyCompaction, ContextClear, TurnCancel, TurnEnded
'turn': /* TurnModelState — packages/agent-core-v2/src/agent/loop/turnOps.ts */ {
Expand Down
1 change: 1 addition & 0 deletions packages/agent-core-v2/docs/wire-manifest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ interface TurnPromptPayload {
/** PromptOrigin */
origin: 'user' | 'skill_activation' | 'plugin_command' | 'injection' | 'shell_command' | 'compaction_summary' | 'system_trigger' | 'task' | 'cron_job' | 'cron_missed' | 'hook_result' | 'retry';
promptId?: string;
turnId?: number;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/agent-core-v2/docs/zh/event-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ agent-core-v2 各 XState 状态机中状态、事件、action、guard、被 invo
按**接收方拿事件做什么**分类,而不是按是否携带 payload。

1. **命令 —— 动词原形**。要求接收方做事。例:`input.submit`、`input.steer`、`input.abort`、`input.remind`、`tool.abort`、`turn.abort`、`turn.drain`、`turn.notify`、`turn.spawn_tools`、`context.reset`。
2. **事实 —— 过去分词**。报告某事已发生,通常驱动转移或父级记账。例:`llm.sent`、`llm.done`、`llm.failed.syntax`、`llm.failed.remote`、`llm.retrying`、`llm.recovering`、`tool.done`、`tool.failed`、`tool.aborted`、`tool.detached`、`turn.reminders_consumed`、`todo.used`。emitted 事件天然是事实:`turn.started`、`turn.done`、`turn.failed`、`turn.aborted`、`turn.aborting`、`agent.created`、`agent.forked`、`agent.switched`、`agent.stopped`、`agent.failed`、`usage.updated`。
2. **事实 —— 过去分词**。报告某事已发生,通常驱动转移或父级记账。例:`llm.sent`、`llm.done`、`llm.failed.syntax`、`llm.failed.remote`、`llm.retrying`、`llm.recovering`、`tool.done`、`tool.failed`、`tool.aborted`、`tool.detached`、`turn.reminders_consumed`、`todo.used`。emitted 事件天然是事实:`turn.started`、`step.started`、`turn.done`、`turn.failed`、`turn.aborted`、`turn.aborting`、`agent.created`、`agent.forked`、`agent.switched`、`agent.stopped`、`agent.failed`、`usage.updated`。
3. **数据流 —— 名词(即数据名)**。把一份流式数据送达,接收方累积或转发。归入 `streaming` 子命名空间:`llm.streaming.part`、`llm.streaming.headers`、`llm.streaming.usage`、`llm.streaming.finish`、`llm.streaming.message_id`;另有 `tool.update`、`usage.record`。

判别示例:`llm.streaming.finish` 携带完成元数据喂给累加器(数据流,名词),而 `llm.done` 是无 payload 的流终止哨兵、驱动转移(事实,过去分词)。
Expand Down
6 changes: 4 additions & 2 deletions packages/agent-core-v2/src/agent/loop/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export type LoopRunResult =
export type TurnResult = LoopRunResult;

export interface Turn {
readonly id: number;
readonly id?: number;
readonly state?: 'queued' | 'running' | 'completed' | 'failed' | 'cancelled';
readonly signal: AbortSignal;
readonly ready: Promise<void>;
Expand All @@ -94,7 +94,7 @@ export interface Turn {
export interface AgentLoopStatus {
readonly state: 'idle' | 'running';
readonly activeTurnId?: number;
readonly pendingTurnIds: readonly number[];
readonly pendingPromptIds: readonly string[];
readonly hasPendingRequests: boolean;
readonly activeTraceId?: string;
}
Expand Down Expand Up @@ -130,6 +130,8 @@ export interface IAgentLoopService {

cancel(turnId?: number, reason?: unknown): boolean;

cancelQueued(queueId: string, reason?: unknown): boolean;

cancelFromUser(turnId?: number): void;

status(): AgentLoopStatus;
Expand Down
Loading
Loading