MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / turnWorker

Method turnWorker

packages/agent-core/src/agent/turn/index.ts:340–381  ·  view source on GitHub ↗

* The body of the single in-flight `activeTurn`. Routes to the goal driver * (sequential continuation turns) when a goal is active, otherwise runs exactly * one turn. Clears `activeTurn` when the whole run finishes (identified by the * launch signal, so a superseding turn is never clobbered

(
    firstTurnId: number,
    input: readonly ContentPart[],
    origin: PromptOrigin,
    signal: AbortSignal,
  )

Source from the content-addressed store, hash-verified

338 * launch signal, so a superseding turn is never clobbered).
339 */
340 private async turnWorker(
341 firstTurnId: number,
342 input: readonly ContentPart[],
343 origin: PromptOrigin,
344 signal: AbortSignal,
345 ): Promise<TurnEndResult> {
346 const ownsActiveTurn = (): boolean =>
347 this.activeTurn !== null &&
348 this.activeTurn !== 'resuming' &&
349 this.activeTurn.controller.signal === signal;
350 try {
351 const initialGoalStatus = this.agent.goal.getGoal().goal?.status;
352 if (initialGoalStatus === 'active') {
353 return await this.driveGoal(firstTurnId, input, origin, signal);
354 }
355 const end = await this.runOneTurn(firstTurnId, input, origin, signal, true);
356 // A goal can become active during an ordinary turn: the model creates one
357 // with CreateGoal, or resumes a paused/blocked goal via UpdateGoal. Either
358 // way, hand the now-active goal to the driver so it is actually pursued,
359 // instead of stopping after the turn that merely started it. (The
360 // already-active case took the early return above.)
361 const goalBecameActive = this.agent.goal.getGoal().goal?.status === 'active';
362 if (
363 goalBecameActive &&
364 end.event.reason !== 'cancelled' &&
365 end.event.reason !== 'failed' &&
366 end.event.reason !== 'filtered'
367 ) {
368 return await this.driveGoal(
369 this.allocateTurnId(),
370 [{ type: 'text', text: GOAL_CONTINUATION_PROMPT }],
371 GOAL_CONTINUATION_ORIGIN,
372 signal,
373 );
374 }
375 return end;
376 } finally {
377 if (ownsActiveTurn()) {
378 this.activeTurn = null;
379 }
380 }
381 }
382
383 /**
384 * Drives an active goal as a sequence of ordinary turns — the autonomous

Callers 1

launchMethod · 0.95

Calls 4

driveGoalMethod · 0.95
runOneTurnMethod · 0.95
allocateTurnIdMethod · 0.95
getGoalMethod · 0.45

Tested by

no test coverage detected