每个 turn 由多个 step 组成,step 内共享一次不可漂移的上下文快照
run_turn 在采样前处理压缩、skills/plugins、session/input hooks 和待处理输入;随后为一个 step 捕获 StepContext,并用同一视图构造上下文、工具与模型请求。
一轮任务可以问模型很多次,但每一次“想一想并行动”的小步都先拍一张现场快照,避免工具清单和提示词在同一步里前后不一致。
配置热更新只能在安全边界生效,换来 prompt cache 和工具调用的确定性。
153 pub(crate) async fn run_turn(
154 sess: Arc<Session>,
155 turn_context: Arc<TurnContext>,
156 turn_extension_data: Arc<codex_extension_api::ExtensionData>,
157 input: Vec<TurnInput>,
158 prewarmed_client_session: Option<ModelClientSession>,
159 cancellation_token: CancellationToken,
160 ) -> CodexResult<Option<String>> {
161 let mut client_session =
162 prewarmed_client_session.unwrap_or_else(|| sess.services.model_client.new_session());
163 // TODO(ccunningham): Pre-turn compaction runs before context updates and the
164 // new user message are recorded. Estimate pending incoming items (context
165 // diffs/full reinjection + user input) and trigger compaction preemptively
166 // when they would push the thread over the compaction threshold.
167 if let Err(err) = run_pre_sampling_compact(
168 &sess,
169 &turn_context,
170 &mut client_session,
171 &cancellation_token,
172 )
173 .await
174 {
175 if matches!(err.details(), CodexErrorDetails::TurnAborted) {
176 run_hooks_and_record_inputs(&sess, &turn_context, &input).await;
… 88 lines omitted; exact range 153–274 …
265 break;
266 }
267
268 let window_id = sess.current_window_id().await;
269 super::rollout_budget::maybe_record_reminder(
270 sess.as_ref(),
271 turn_context.as_ref(),
272 &window_id,
273 )
274 .await;
查看全部 2 处证据
-
实现
codex-rs/core/src/session/turn.rs:153–274turn 初始化、压缩、hooks 与 step context。 -
实现
codex-rs/core/src/session/turn.rs:275–455step 循环、待处理输入、压缩和停止控制。