Remove legacy Epiphany state from Codex protocol
This commit is contained in:
parent
d6ebed2c01
commit
febef826c0
20 changed files with 147 additions and 828 deletions
|
|
@ -1,16 +1,12 @@
|
|||
use std::path::Path;
|
||||
|
||||
use super::legacy_epiphany_rollout::latest_legacy_epiphany_state;
|
||||
pub(super) use super::legacy_epiphany_rollout::load_epiphany_state_from_rollout_path;
|
||||
use codex_core::CodexThread;
|
||||
use codex_core::RolloutRecorder;
|
||||
use epiphany_state_model::EpiphanyThreadState;
|
||||
use codex_protocol::protocol::InitialHistory;
|
||||
use codex_protocol::protocol::TokenUsageInfo;
|
||||
use epiphany_codex_bridge::mutation_service::EpiphanyMutationHost;
|
||||
use epiphany_codex_bridge::mutation_service::load_authoritative_accepted_state;
|
||||
use epiphany_codex_bridge::pressure::EpiphanyTokenUsageSnapshot;
|
||||
use epiphany_codex_bridge::state::client_visible_epiphany_state_for_paths;
|
||||
use epiphany_codex_bridge::state::thread_state_mirror_id_from_rollout_path;
|
||||
use epiphany_state_model::EpiphanyThreadState;
|
||||
|
||||
pub(super) struct EpiphanyCodexThreadHost<'a> {
|
||||
thread: &'a CodexThread,
|
||||
|
|
@ -55,24 +51,6 @@ impl EpiphanyMutationHost for EpiphanyCodexThreadHost<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
pub(super) async fn load_epiphany_state_from_rollout_path(
|
||||
rollout_path: &Path,
|
||||
) -> std::result::Result<Option<EpiphanyThreadState>, String> {
|
||||
let items = match RolloutRecorder::get_rollout_history(rollout_path)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
format!(
|
||||
"failed to load rollout `{}` for Epiphany state: {err}",
|
||||
rollout_path.display()
|
||||
)
|
||||
})? {
|
||||
InitialHistory::New | InitialHistory::Cleared => Vec::new(),
|
||||
InitialHistory::Forked(items) => items,
|
||||
InitialHistory::Resumed(resumed) => resumed.history,
|
||||
};
|
||||
latest_legacy_epiphany_state(&items)
|
||||
}
|
||||
|
||||
pub(super) async fn live_thread_epiphany_state(
|
||||
thread: &CodexThread,
|
||||
) -> Option<EpiphanyThreadState> {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,16 @@
|
|||
use std::path::Path;
|
||||
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use epiphany_state_model::EpiphanyThreadState;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::RolloutItem;
|
||||
use codex_protocol::protocol::RolloutLine;
|
||||
use epiphany_state_model::EpiphanyStateItem;
|
||||
use epiphany_state_model::EpiphanyThreadState;
|
||||
|
||||
enum LegacyReplayItem {
|
||||
Codex(RolloutItem),
|
||||
Epiphany(EpiphanyStateItem),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct LegacySegment {
|
||||
|
|
@ -28,8 +37,8 @@ fn is_out_of_band(segment: &LegacySegment) -> bool {
|
|||
segment.turn_id.is_none() && !segment.user_turn && segment.state.is_some()
|
||||
}
|
||||
|
||||
pub(super) fn latest_legacy_epiphany_state(
|
||||
items: &[RolloutItem],
|
||||
fn latest_legacy_epiphany_state(
|
||||
items: &[LegacyReplayItem],
|
||||
) -> Result<Option<EpiphanyThreadState>, String> {
|
||||
let mut state = None;
|
||||
let mut rollbacks = 0usize;
|
||||
|
|
@ -46,15 +55,15 @@ pub(super) fn latest_legacy_epiphany_state(
|
|||
}
|
||||
|
||||
match item {
|
||||
RolloutItem::EventMsg(EventMsg::ThreadRolledBack(event)) => {
|
||||
LegacyReplayItem::Codex(RolloutItem::EventMsg(EventMsg::ThreadRolledBack(event))) => {
|
||||
rollbacks = rollbacks
|
||||
.saturating_add(usize::try_from(event.num_turns).unwrap_or(usize::MAX));
|
||||
}
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(event)) => {
|
||||
LegacyReplayItem::Codex(RolloutItem::EventMsg(EventMsg::TurnComplete(event))) => {
|
||||
let segment = segment.get_or_insert_with(LegacySegment::default);
|
||||
segment.turn_id.get_or_insert_with(|| event.turn_id.clone());
|
||||
}
|
||||
RolloutItem::EventMsg(EventMsg::TurnAborted(event)) => {
|
||||
LegacyReplayItem::Codex(RolloutItem::EventMsg(EventMsg::TurnAborted(event))) => {
|
||||
if let Some(turn_id) = &event.turn_id {
|
||||
segment
|
||||
.get_or_insert_with(LegacySegment::default)
|
||||
|
|
@ -62,17 +71,16 @@ pub(super) fn latest_legacy_epiphany_state(
|
|||
.get_or_insert_with(|| turn_id.clone());
|
||||
}
|
||||
}
|
||||
RolloutItem::EventMsg(EventMsg::UserMessage(_)) => {
|
||||
LegacyReplayItem::Codex(RolloutItem::EventMsg(EventMsg::UserMessage(_))) => {
|
||||
segment.get_or_insert_with(LegacySegment::default).user_turn = true;
|
||||
}
|
||||
RolloutItem::ResponseItem(ResponseItem::Message { role, .. }) if role == "user" => {
|
||||
LegacyReplayItem::Codex(RolloutItem::ResponseItem(ResponseItem::Message {
|
||||
role,
|
||||
..
|
||||
})) if role == "user" => {
|
||||
segment.get_or_insert_with(LegacySegment::default).user_turn = true;
|
||||
}
|
||||
RolloutItem::LegacyEpiphanyState(payload) => {
|
||||
let item: epiphany_state_model::EpiphanyStateItem =
|
||||
serde_json::from_value(payload.clone()).map_err(|error| {
|
||||
format!("invalid legacy Epiphany rollout payload: {error}")
|
||||
})?;
|
||||
LegacyReplayItem::Epiphany(item) => {
|
||||
let segment = segment.get_or_insert_with(LegacySegment::default);
|
||||
if segment.turn_id.is_none() {
|
||||
segment.turn_id = item.turn_id.clone();
|
||||
|
|
@ -83,7 +91,7 @@ pub(super) fn latest_legacy_epiphany_state(
|
|||
segment.state = Some(item.state.clone());
|
||||
}
|
||||
}
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(event)) => {
|
||||
LegacyReplayItem::Codex(RolloutItem::EventMsg(EventMsg::TurnStarted(event))) => {
|
||||
if segment.as_ref().is_some_and(|segment| {
|
||||
compatible(segment.turn_id.as_deref(), Some(event.turn_id.as_str()))
|
||||
}) && let Some(segment) = segment.take()
|
||||
|
|
@ -91,11 +99,7 @@ pub(super) fn latest_legacy_epiphany_state(
|
|||
finish(segment, &mut state, &mut rollbacks);
|
||||
}
|
||||
}
|
||||
RolloutItem::ResponseItem(_)
|
||||
| RolloutItem::EventMsg(_)
|
||||
| RolloutItem::Compacted(_)
|
||||
| RolloutItem::TurnContext(_)
|
||||
| RolloutItem::SessionMeta(_) => {}
|
||||
LegacyReplayItem::Codex(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -105,15 +109,49 @@ pub(super) fn latest_legacy_epiphany_state(
|
|||
Ok(state)
|
||||
}
|
||||
|
||||
pub(super) async fn load_epiphany_state_from_rollout_path(
|
||||
path: &Path,
|
||||
) -> Result<Option<EpiphanyThreadState>, String> {
|
||||
let text = tokio::fs::read_to_string(path)
|
||||
.await
|
||||
.map_err(|error| format!("failed to read rollout `{}`: {error}", path.display()))?;
|
||||
let mut items = Vec::new();
|
||||
for line in text.lines().filter(|line| !line.trim().is_empty()) {
|
||||
if let Some(item) = parse_replay_line(line)? {
|
||||
items.push(item);
|
||||
}
|
||||
}
|
||||
latest_legacy_epiphany_state(&items)
|
||||
}
|
||||
|
||||
fn parse_replay_line(line: &str) -> Result<Option<LegacyReplayItem>, String> {
|
||||
let Ok(value) = serde_json::from_str::<serde_json::Value>(line) else {
|
||||
return Ok(None);
|
||||
};
|
||||
if value.get("type").and_then(serde_json::Value::as_str) == Some("epiphany_state") {
|
||||
let payload = value
|
||||
.get("payload")
|
||||
.cloned()
|
||||
.ok_or_else(|| "legacy Epiphany rollout item is missing payload".to_string())?;
|
||||
let item = serde_json::from_value(payload)
|
||||
.map_err(|error| format!("invalid legacy Epiphany rollout payload: {error}"))?;
|
||||
Ok(Some(LegacyReplayItem::Epiphany(item)))
|
||||
} else {
|
||||
Ok(serde_json::from_value::<RolloutLine>(value)
|
||||
.ok()
|
||||
.map(|line| LegacyReplayItem::Codex(line.item)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use epiphany_state_model::EpiphanyStateItem;
|
||||
use codex_protocol::protocol::ThreadRolledBackEvent;
|
||||
use codex_protocol::protocol::TurnCompleteEvent;
|
||||
use codex_protocol::protocol::TurnStartedEvent;
|
||||
use codex_protocol::protocol::UserMessageEvent;
|
||||
use epiphany_state_model::EpiphanyStateItem;
|
||||
|
||||
fn state(id: &str) -> EpiphanyThreadState {
|
||||
EpiphanyThreadState {
|
||||
|
|
@ -123,34 +161,37 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
fn turn(id: &str, state: EpiphanyThreadState) -> Vec<RolloutItem> {
|
||||
fn turn(id: &str, state: EpiphanyThreadState) -> Vec<LegacyReplayItem> {
|
||||
vec![
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
|
||||
turn_id: id.to_string(),
|
||||
started_at: None,
|
||||
model_context_window: None,
|
||||
collaboration_mode_kind: ModeKind::Default,
|
||||
})),
|
||||
RolloutItem::EventMsg(EventMsg::UserMessage(UserMessageEvent {
|
||||
message: id.to_string(),
|
||||
images: None,
|
||||
local_images: Vec::new(),
|
||||
text_elements: Vec::new(),
|
||||
})),
|
||||
RolloutItem::LegacyEpiphanyState(
|
||||
serde_json::to_value(EpiphanyStateItem {
|
||||
turn_id: Some(id.to_string()),
|
||||
state,
|
||||
})
|
||||
.expect("serialize legacy Epiphany payload"),
|
||||
),
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
turn_id: id.to_string(),
|
||||
last_agent_message: None,
|
||||
completed_at: None,
|
||||
duration_ms: None,
|
||||
time_to_first_token_ms: None,
|
||||
})),
|
||||
LegacyReplayItem::Codex(RolloutItem::EventMsg(EventMsg::TurnStarted(
|
||||
TurnStartedEvent {
|
||||
turn_id: id.to_string(),
|
||||
started_at: None,
|
||||
model_context_window: None,
|
||||
collaboration_mode_kind: ModeKind::Default,
|
||||
},
|
||||
))),
|
||||
LegacyReplayItem::Codex(RolloutItem::EventMsg(EventMsg::UserMessage(
|
||||
UserMessageEvent {
|
||||
message: id.to_string(),
|
||||
images: None,
|
||||
local_images: Vec::new(),
|
||||
text_elements: Vec::new(),
|
||||
},
|
||||
))),
|
||||
LegacyReplayItem::Epiphany(EpiphanyStateItem {
|
||||
turn_id: Some(id.to_string()),
|
||||
state,
|
||||
}),
|
||||
LegacyReplayItem::Codex(RolloutItem::EventMsg(EventMsg::TurnComplete(
|
||||
TurnCompleteEvent {
|
||||
turn_id: id.to_string(),
|
||||
last_agent_message: None,
|
||||
completed_at: None,
|
||||
duration_ms: None,
|
||||
time_to_first_token_ms: None,
|
||||
},
|
||||
))),
|
||||
]
|
||||
}
|
||||
|
||||
|
|
@ -168,18 +209,43 @@ mod tests {
|
|||
let first = state("one");
|
||||
let mut items = turn("one", first.clone());
|
||||
items.extend(turn("two", state("two")));
|
||||
items.push(RolloutItem::EventMsg(EventMsg::ThreadRolledBack(
|
||||
ThreadRolledBackEvent { num_turns: 1 },
|
||||
items.push(LegacyReplayItem::Codex(RolloutItem::EventMsg(
|
||||
EventMsg::ThreadRolledBack(ThreadRolledBackEvent { num_turns: 1 }),
|
||||
)));
|
||||
assert_eq!(latest_legacy_epiphany_state(&items), Ok(Some(first)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_malformed_legacy_state_payload() {
|
||||
let items = vec![RolloutItem::LegacyEpiphanyState(serde_json::json!({
|
||||
"turn_id": "not-the-legacy-shape"
|
||||
}))];
|
||||
let line = serde_json::json!({
|
||||
"timestamp": "2026-07-12T00:00:00Z",
|
||||
"type": "epiphany_state",
|
||||
"payload": { "turn_id": "not-the-legacy-shape" }
|
||||
});
|
||||
assert!(parse_replay_line(&line.to_string()).is_err());
|
||||
}
|
||||
|
||||
assert!(latest_legacy_epiphany_state(&items).is_err());
|
||||
#[test]
|
||||
fn recognizes_only_the_historical_raw_line_tag() {
|
||||
let expected = state("raw-line");
|
||||
let line = serde_json::json!({
|
||||
"timestamp": "2026-07-12T00:00:00Z",
|
||||
"type": "epiphany_state",
|
||||
"payload": {
|
||||
"turn_id": "raw-line",
|
||||
"state": expected
|
||||
}
|
||||
});
|
||||
let Some(LegacyReplayItem::Epiphany(item)) =
|
||||
parse_replay_line(&line.to_string()).expect("parse historical line")
|
||||
else {
|
||||
panic!("expected quarantined Epiphany migration item");
|
||||
};
|
||||
assert_eq!(item.turn_id.as_deref(), Some("raw-line"));
|
||||
assert_eq!(item.state, state("raw-line"));
|
||||
|
||||
assert!(parse_replay_line(r#"{"type":"future_unknown","payload":{}}"#)
|
||||
.expect("unknown line should remain nonfatal to migration scan")
|
||||
.is_none());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue