|
1 | | -//! Read and validate applied executor policies for restore. |
| 1 | +//! Import application-confirmed outcomes for original local calls without replay. |
| 2 | +
|
| 3 | +use std::collections::{HashMap, HashSet}; |
2 | 4 |
|
3 | 5 | use anyhow::{Context, Result, ensure}; |
4 | 6 | use codex_history::RolloutItem; |
| 7 | +use codex_protocol::models::{ |
| 8 | + ContentItemKind, FunctionCallOutputBody, FunctionCallOutputPayload, |
| 9 | + InternalChatMessageMetadataPassthrough, ResponseItem, |
| 10 | +}; |
5 | 11 | use codex_protocol::protocol::sdk_executor_policy_receipt; |
| 12 | +use serde::Deserialize; |
6 | 13 | use serde_json::Value; |
7 | 14 |
|
8 | 15 | use crate::engine::ThreadState; |
9 | 16 |
|
| 17 | +const APPLIED: &str = "sdk_reconciliation.applied"; |
| 18 | +const NOT_APPLIED: &str = "sdk_reconciliation.not_applied"; |
| 19 | + |
| 20 | +#[derive(Clone, Copy, Deserialize)] |
| 21 | +#[serde(rename_all = "snake_case")] |
| 22 | +enum Outcome { |
| 23 | + Applied, |
| 24 | + NotApplied, |
| 25 | +} |
| 26 | + |
| 27 | +impl Outcome { |
| 28 | + fn tag(self) -> &'static str { |
| 29 | + match self { |
| 30 | + Self::Applied => APPLIED, |
| 31 | + Self::NotApplied => NOT_APPLIED, |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Deserialize)] |
| 37 | +#[serde(deny_unknown_fields)] |
| 38 | +struct ReconciledCall { |
| 39 | + thread_id: String, |
| 40 | + turn_id: String, |
| 41 | + call_id: String, |
| 42 | + outcome: Outcome, |
| 43 | + result: String, |
| 44 | +} |
| 45 | + |
| 46 | +fn output(item: &ResponseItem) -> Option<(&str, &FunctionCallOutputPayload)> { |
| 47 | + match item { |
| 48 | + ResponseItem::FunctionCallOutput { |
| 49 | + call_id: Some(call_id), |
| 50 | + output, |
| 51 | + .. |
| 52 | + } |
| 53 | + | ResponseItem::CustomToolCallOutput { |
| 54 | + call_id, output, .. |
| 55 | + } => Some((call_id, output)), |
| 56 | + _ => None, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +fn tag(item: &ResponseItem) -> Option<&str> { |
| 61 | + let metadata = match item { |
| 62 | + ResponseItem::FunctionCallOutput { |
| 63 | + internal_chat_message_metadata_passthrough, |
| 64 | + .. |
| 65 | + } |
| 66 | + | ResponseItem::CustomToolCallOutput { |
| 67 | + internal_chat_message_metadata_passthrough, |
| 68 | + .. |
| 69 | + } => internal_chat_message_metadata_passthrough.as_ref()?, |
| 70 | + _ => return None, |
| 71 | + }; |
| 72 | + let tags = metadata.content_item_kinds.as_deref()?; |
| 73 | + match tags { |
| 74 | + [ContentItemKind(tag)] if tag == APPLIED || tag == NOT_APPLIED => Some(tag), |
| 75 | + _ => None, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +fn matches_receipt(item: &ResponseItem, receipt: &ReconciledCall) -> bool { |
| 80 | + item.turn_id() == Some(receipt.turn_id.as_str()) |
| 81 | + && tag(item) == Some(receipt.outcome.tag()) |
| 82 | + && output(item).is_some_and(|(id, payload)| { |
| 83 | + id == receipt.call_id |
| 84 | + && matches!(&payload.body, FunctionCallOutputBody::Text(text) if text == &receipt.result) |
| 85 | + }) |
| 86 | +} |
| 87 | + |
10 | 88 | pub(crate) async fn executor_policies(state: &ThreadState) -> Result<Vec<Value>> { |
11 | 89 | let manager = state.engine.manager.environment_manager(); |
12 | 90 | let local_network = if state |
@@ -35,17 +113,19 @@ pub(crate) async fn executor_policies(state: &ThreadState) -> Result<Vec<Value>> |
35 | 113 | .collect() |
36 | 114 | } |
37 | 115 |
|
38 | | -pub(crate) async fn validate_executor_policy(state: &ThreadState) -> Result<()> { |
| 116 | +pub(crate) async fn reconcile(state: &ThreadState, encoded: &str) -> Result<()> { |
| 117 | + let receipts: Vec<ReconciledCall> = serde_json::from_str(encoded) |
| 118 | + .map_err(|_| anyhow::anyhow!("Invalid local reconciliation receipt"))?; |
39 | 119 | let tools = state |
40 | 120 | .thread |
41 | 121 | .thread_extension_data() |
42 | 122 | .get::<crate::python_tools::PythonTools>() |
43 | | - .context("Local resume requires an SDK-owned Session")?; |
| 123 | + .context("Reconciliation requires an SDK-owned Session")?; |
44 | 124 | let store = state |
45 | 125 | .engine |
46 | 126 | .local_checklist_store |
47 | 127 | .as_ref() |
48 | | - .context("Local resume requires a local storage writer")?; |
| 128 | + .context("Reconciliation requires a local storage writer")?; |
49 | 129 | let _mutation = state.mutations.lock().await; |
50 | 130 | tools.check_idle(&state.thread).await?; |
51 | 131 | let history = store.load_owned_history(state.id).await?; |
@@ -76,5 +156,115 @@ pub(crate) async fn validate_executor_policy(state: &ThreadState) -> Result<()> |
76 | 156 | "Local resume requires the recorded executor write and network policy" |
77 | 157 | ); |
78 | 158 | } |
| 159 | + let guard = state |
| 160 | + .thread |
| 161 | + .thread_extension_data() |
| 162 | + .get::<crate::recovery_guard::Guard>() |
| 163 | + .context("Reconciliation requires the SDK replay guard")?; |
| 164 | + |
| 165 | + let mut seen = HashMap::new(); |
| 166 | + let mut outputs = HashMap::new(); |
| 167 | + let mut protected = HashSet::new(); |
| 168 | + let mut context_turn = None; |
| 169 | + for record in &history { |
| 170 | + match record { |
| 171 | + RolloutItem::TurnContext(context) => { |
| 172 | + context_turn = context.turn_id.as_deref().or(context_turn); |
| 173 | + } |
| 174 | + RolloutItem::ResponseItem(envelope) => { |
| 175 | + let item = &envelope.item; |
| 176 | + let Some(turn) = item.turn_id().or(context_turn) else { |
| 177 | + continue; |
| 178 | + }; |
| 179 | + match item { |
| 180 | + ResponseItem::FunctionCall { call_id, .. } |
| 181 | + | ResponseItem::CustomToolCall { call_id, .. } => { |
| 182 | + let entry = seen.entry((turn, call_id.as_str())).or_insert(Some(item)); |
| 183 | + if *entry != Some(item) { |
| 184 | + *entry = None; |
| 185 | + } |
| 186 | + } |
| 187 | + _ => { |
| 188 | + if let Some((call_id, _)) = output(item) { |
| 189 | + let entry = outputs.entry((turn, call_id)).or_insert(Some(item)); |
| 190 | + if *entry != Some(item) { |
| 191 | + *entry = None; |
| 192 | + } |
| 193 | + if tag(item).is_some() { |
| 194 | + protected.insert(call_id.to_owned()); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + _ => {} |
| 201 | + } |
| 202 | + } |
| 203 | + let mut requested = HashSet::new(); |
| 204 | + let mut append = Vec::new(); |
| 205 | + for receipt in &receipts { |
| 206 | + let key = (receipt.turn_id.as_str(), receipt.call_id.as_str()); |
| 207 | + ensure!( |
| 208 | + receipt.thread_id == thread_id && requested.insert(key), |
| 209 | + "Reconciliation receipt is duplicate or belongs to a different thread" |
| 210 | + ); |
| 211 | + let call = seen |
| 212 | + .get(&key) |
| 213 | + .copied() |
| 214 | + .flatten() |
| 215 | + .context("Reconciliation does not identify one retained invocation")?; |
| 216 | + if let Some(existing) = outputs.get(&key) { |
| 217 | + ensure!( |
| 218 | + existing.is_some_and(|existing| matches_receipt(existing, receipt)), |
| 219 | + "Reconciliation conflicts with a retained result" |
| 220 | + ); |
| 221 | + continue; |
| 222 | + } |
| 223 | + let output = FunctionCallOutputPayload { |
| 224 | + body: FunctionCallOutputBody::Text(receipt.result.clone()), |
| 225 | + success: Some(matches!(receipt.outcome, Outcome::Applied)), |
| 226 | + }; |
| 227 | + let metadata = Some(InternalChatMessageMetadataPassthrough { |
| 228 | + turn_id: Some(receipt.turn_id.clone()), |
| 229 | + content_item_kinds: Some(vec![ContentItemKind(receipt.outcome.tag().to_owned())]), |
| 230 | + ..Default::default() |
| 231 | + }); |
| 232 | + append.push(match call { |
| 233 | + ResponseItem::FunctionCall { |
| 234 | + name, namespace, .. |
| 235 | + } => ResponseItem::FunctionCallOutput { |
| 236 | + id: None, |
| 237 | + call_id: Some(receipt.call_id.clone()), |
| 238 | + name: Some(name.clone()), |
| 239 | + namespace: namespace.clone(), |
| 240 | + output, |
| 241 | + internal_chat_message_metadata_passthrough: metadata, |
| 242 | + }, |
| 243 | + ResponseItem::CustomToolCall { name, .. } => ResponseItem::CustomToolCallOutput { |
| 244 | + id: None, |
| 245 | + call_id: receipt.call_id.clone(), |
| 246 | + name: Some(name.clone()), |
| 247 | + output, |
| 248 | + internal_chat_message_metadata_passthrough: metadata, |
| 249 | + }, |
| 250 | + _ => unreachable!("the call index retains only supported invocations"), |
| 251 | + }); |
| 252 | + protected.insert(receipt.call_id.clone()); |
| 253 | + } |
| 254 | + if !append.is_empty() { |
| 255 | + state.thread.inject_response_items(append).await?; |
| 256 | + state.thread.flush_rollout().await?; |
| 257 | + let durable = store.load_owned_history(state.id).await?; |
| 258 | + for receipt in &receipts { |
| 259 | + ensure!( |
| 260 | + durable.iter().any(|record| matches!( |
| 261 | + record, |
| 262 | + RolloutItem::ResponseItem(envelope) if matches_receipt(&envelope.item, receipt) |
| 263 | + )), |
| 264 | + "Reconciliation result was not confirmed in native storage" |
| 265 | + ); |
| 266 | + } |
| 267 | + } |
| 268 | + guard.set_reconciled(protected); |
79 | 269 | Ok(()) |
80 | 270 | } |
0 commit comments