Add Epiphany jobsUpdated notification

This commit is contained in:
Meta Vens 2026-04-26 15:02:45 +01:00
commit 48d6820d7d
22 changed files with 923 additions and 23 deletions

View file

@ -2,6 +2,7 @@ use crate::codex_message_processor::ApiVersion;
use crate::codex_message_processor::read_rollout_items_from_rollout;
use crate::codex_message_processor::read_summary_from_rollout;
use crate::codex_message_processor::summary_to_thread;
use crate::codex_message_processor::thread_epiphany_jobs_updated_notification_for_runtime_job;
use crate::error_code::INTERNAL_ERROR_CODE;
use crate::error_code::INVALID_REQUEST_ERROR_CODE;
use crate::outgoing_message::ClientRequestResult;
@ -77,6 +78,7 @@ use codex_app_server_protocol::ServerNotification;
use codex_app_server_protocol::ServerRequestPayload;
use codex_app_server_protocol::SkillsChangedNotification;
use codex_app_server_protocol::TerminalInteractionNotification;
use codex_app_server_protocol::ThreadEpiphanyJobsUpdatedNotification;
use codex_app_server_protocol::ThreadItem;
use codex_app_server_protocol::ThreadNameUpdatedNotification;
use codex_app_server_protocol::ThreadRealtimeClosedNotification;
@ -145,6 +147,7 @@ use codex_protocol::request_user_input::RequestUserInputResponse as CoreRequestU
use codex_sandboxing::policy_transforms::intersect_permission_profiles;
use codex_shell_command::parse_command::shlex_join;
use codex_utils_absolute_path::AbsolutePathBuf;
use serde::Deserialize;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
@ -167,6 +170,13 @@ struct CommandExecutionCompletionItem {
command_actions: Vec<V2ParsedCommand>,
}
const AGENT_JOB_PROGRESS_PREFIX: &str = "agent_job_progress:";
#[derive(Debug, Deserialize)]
struct AgentJobProgressBackgroundEvent {
job_id: String,
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn apply_bespoke_event_handling(
event: Event,
@ -1974,6 +1984,18 @@ pub(crate) async fn apply_bespoke_event_handling(
)
.await;
}
EventMsg::BackgroundEvent(background_event) => {
if let ApiVersion::V2 = api_version {
maybe_emit_thread_epiphany_jobs_updated(
conversation_id,
&conversation,
background_event.message.as_str(),
&outgoing,
&thread_state,
)
.await;
}
}
EventMsg::ShutdownComplete => {
thread_watch_manager
.note_thread_shutdown(&conversation_id.to_string())
@ -1984,6 +2006,55 @@ pub(crate) async fn apply_bespoke_event_handling(
}
}
async fn maybe_emit_thread_epiphany_jobs_updated(
conversation_id: ThreadId,
conversation: &Arc<CodexThread>,
message: &str,
outgoing: &ThreadScopedOutgoingMessageSender,
thread_state: &Arc<Mutex<ThreadState>>,
) {
let Some(payload_json) = message.strip_prefix(AGENT_JOB_PROGRESS_PREFIX) else {
return;
};
let progress = match serde_json::from_str::<AgentJobProgressBackgroundEvent>(payload_json) {
Ok(progress) => progress,
Err(err) => {
warn!(
thread_id = %conversation_id,
"failed to parse agent job progress background event: {err}"
);
return;
}
};
let Some(notification) = thread_epiphany_jobs_updated_notification_for_runtime_job(
conversation_id,
conversation.as_ref(),
progress.job_id.as_str(),
)
.await
else {
return;
};
if !should_emit_thread_epiphany_jobs_updated(thread_state, &notification).await {
return;
}
outgoing
.send_server_notification(ServerNotification::ThreadEpiphanyJobsUpdated(notification))
.await;
}
async fn should_emit_thread_epiphany_jobs_updated(
thread_state: &Arc<Mutex<ThreadState>>,
notification: &ThreadEpiphanyJobsUpdatedNotification,
) -> bool {
let mut thread_state = thread_state.lock().await;
thread_state.record_epiphany_jobs_update(notification.jobs.as_slice())
}
async fn handle_turn_diff(
conversation_id: ThreadId,
event_turn_id: &str,
@ -4493,6 +4564,206 @@ mod tests {
Ok(())
}
#[tokio::test]
async fn background_agent_job_progress_emits_epiphany_jobs_updated_once() -> Result<()> {
let codex_home = TempDir::new()?;
let config = load_default_config_for_test(&codex_home).await;
let thread_manager = Arc::new(
codex_core::test_support::thread_manager_with_models_provider_and_home(
CodexAuth::create_dummy_chatgpt_auth_for_testing(),
config.model_provider.clone(),
config.codex_home.to_path_buf(),
Arc::new(codex_exec_server::EnvironmentManager::default_for_tests()),
),
);
let codex_core::NewThread {
thread_id: conversation_id,
thread: conversation,
..
} = thread_manager.start_thread(config).await?;
let state_db = conversation.state_db().expect("state db should exist");
state_db
.create_agent_job(
&codex_state::AgentJobCreateParams {
id: "job-specialist".to_string(),
name: "epiphany-specialist".to_string(),
instruction: "Run the runtime-bound specialist smoke.".to_string(),
auto_export: false,
max_runtime_seconds: Some(60),
output_schema_json: None,
input_headers: vec!["row".to_string()],
input_csv_path: "input.csv".to_string(),
output_csv_path: "output.csv".to_string(),
},
&[
codex_state::AgentJobItemCreateParams {
item_id: "item-completed".to_string(),
row_index: 0,
source_id: Some("row-0".to_string()),
row_json: json!({"row": 0}),
},
codex_state::AgentJobItemCreateParams {
item_id: "item-running".to_string(),
row_index: 1,
source_id: Some("row-1".to_string()),
row_json: json!({"row": 1}),
},
codex_state::AgentJobItemCreateParams {
item_id: "item-pending".to_string(),
row_index: 2,
source_id: Some("row-2".to_string()),
row_json: json!({"row": 2}),
},
],
)
.await?;
state_db.mark_agent_job_running("job-specialist").await?;
assert!(
state_db
.mark_agent_job_item_running_with_thread(
"job-specialist",
"item-completed",
"worker-thread-completed",
)
.await?
);
assert!(
state_db
.report_agent_job_item_result(
"job-specialist",
"item-completed",
"worker-thread-completed",
&json!({"accepted": true}),
)
.await?
);
assert!(
state_db
.mark_agent_job_item_running_with_thread(
"job-specialist",
"item-running",
"worker-thread-running",
)
.await?
);
conversation
.epiphany_update_state(codex_core::EpiphanyStateUpdate {
expected_revision: Some(0),
job_bindings: Some(vec![codex_protocol::protocol::EpiphanyJobBinding {
id: "specialist-work".to_string(),
kind: codex_protocol::protocol::EpiphanyJobKind::Specialist,
scope: "runtime-bound specialist work".to_string(),
owner_role: "epiphany-harness".to_string(),
runtime_agent_job_id: Some("job-specialist".to_string()),
linked_subgoal_ids: vec!["phase6-jobs".to_string()],
linked_graph_node_ids: vec!["job-surface".to_string()],
progress_note: Some("Bound to a real runtime specialist job.".to_string()),
blocking_reason: None,
}]),
..Default::default()
})
.await?;
let thread_state = new_thread_state();
let thread_watch_manager = ThreadWatchManager::new();
let (tx, mut rx) = mpsc::channel(CHANNEL_CAPACITY);
let outgoing = Arc::new(OutgoingMessageSender::new(tx));
let outgoing = ThreadScopedOutgoingMessageSender::new(
outgoing,
vec![ConnectionId(1)],
conversation_id,
);
let message = format!(
"{AGENT_JOB_PROGRESS_PREFIX}{}",
json!({
"job_id": "job-specialist",
"total_items": 3,
"pending_items": 1,
"running_items": 1,
"completed_items": 1,
"failed_items": 0,
"eta_seconds": 42,
})
);
apply_bespoke_event_handling(
Event {
id: "turn-epiphany-jobs".to_string(),
msg: EventMsg::BackgroundEvent(codex_protocol::protocol::BackgroundEventEvent {
message: message.clone(),
}),
},
conversation_id,
conversation.clone(),
thread_manager.clone(),
None,
outgoing.clone(),
thread_state.clone(),
thread_watch_manager.clone(),
ApiVersion::V2,
"test-provider".to_string(),
codex_home.path(),
)
.await;
let first = recv_broadcast_message(&mut rx).await?;
match first {
OutgoingMessage::AppServerNotification(
ServerNotification::ThreadEpiphanyJobsUpdated(payload),
) => {
assert_eq!(payload.thread_id, conversation_id.to_string());
assert_eq!(
payload.source,
codex_app_server_protocol::ThreadEpiphanyJobsUpdatedSource::RuntimeProgress
);
assert_eq!(payload.state_revision, Some(1));
assert_eq!(payload.jobs.len(), 1);
let job = &payload.jobs[0];
assert_eq!(job.id, "specialist-work");
assert_eq!(
job.status,
codex_app_server_protocol::ThreadEpiphanyJobStatus::Running
);
assert_eq!(job.runtime_agent_job_id.as_deref(), Some("job-specialist"));
assert_eq!(job.items_processed, Some(1));
assert_eq!(job.items_total, Some(3));
assert_eq!(
job.active_thread_ids,
vec!["worker-thread-running".to_string()]
);
}
other => bail!("unexpected notification: {other:?}"),
}
apply_bespoke_event_handling(
Event {
id: "turn-epiphany-jobs".to_string(),
msg: EventMsg::BackgroundEvent(codex_protocol::protocol::BackgroundEventEvent {
message,
}),
},
conversation_id,
conversation,
thread_manager,
None,
outgoing,
thread_state,
thread_watch_manager,
ApiVersion::V2,
"test-provider".to_string(),
codex_home.path(),
)
.await;
assert!(
rx.try_recv().is_err(),
"duplicate background progress should not emit unchanged jobsUpdated payloads"
);
Ok(())
}
#[tokio::test]
async fn test_construct_mcp_tool_call_begin_notification_with_args() {
let begin_event = McpToolCallBeginEvent {

View file

@ -172,6 +172,8 @@ use codex_app_server_protocol::ThreadEpiphanyJobStatus;
use codex_app_server_protocol::ThreadEpiphanyJobsParams;
use codex_app_server_protocol::ThreadEpiphanyJobsResponse;
use codex_app_server_protocol::ThreadEpiphanyJobsSource;
use codex_app_server_protocol::ThreadEpiphanyJobsUpdatedNotification;
use codex_app_server_protocol::ThreadEpiphanyJobsUpdatedSource;
use codex_app_server_protocol::ThreadEpiphanyPressure;
use codex_app_server_protocol::ThreadEpiphanyPressureBasis;
use codex_app_server_protocol::ThreadEpiphanyPressureLevel;
@ -11934,6 +11936,46 @@ struct EpiphanyAgentJobSnapshot {
active_thread_ids: Vec<String>,
}
pub(crate) async fn thread_epiphany_jobs_updated_notification_for_runtime_job(
thread_id: ThreadId,
thread: &CodexThread,
runtime_agent_job_id: &str,
) -> Option<ThreadEpiphanyJobsUpdatedNotification> {
let state = thread.epiphany_state().await?;
let binding_ids = state
.job_bindings
.iter()
.filter(|binding| binding.runtime_agent_job_id.as_deref() == Some(runtime_agent_job_id))
.map(|binding| binding.id.clone())
.collect::<HashSet<_>>();
if binding_ids.is_empty() {
return None;
}
let state_db_ctx = thread.state_db()?;
let retrieval_override = if state.retrieval.is_none() && binding_ids.contains("retrieval-index")
{
Some(thread.epiphany_retrieval_state().await)
} else {
None
};
let job_resolution = load_epiphany_agent_job_snapshots(Some(&state_db_ctx), Some(&state)).await;
let jobs = map_epiphany_jobs(Some(&state), retrieval_override.as_ref(), &job_resolution)
.into_iter()
.filter(|job| binding_ids.contains(job.id.as_str()))
.collect::<Vec<_>>();
if jobs.is_empty() {
return None;
}
Some(ThreadEpiphanyJobsUpdatedNotification {
thread_id: thread_id.to_string(),
source: ThreadEpiphanyJobsUpdatedSource::RuntimeProgress,
state_revision: Some(state.revision),
jobs,
})
}
async fn load_epiphany_agent_job_snapshots(
state_db_ctx: Option<&StateDbHandle>,
state: Option<&EpiphanyThreadState>,

View file

@ -1,6 +1,7 @@
use crate::outgoing_message::ConnectionId;
use crate::outgoing_message::ConnectionRequestId;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ThreadEpiphanyJob;
use codex_app_server_protocol::ThreadHistoryBuilder;
use codex_app_server_protocol::Turn;
use codex_app_server_protocol::TurnError;
@ -64,6 +65,7 @@ pub(crate) struct ThreadState {
pub(crate) listener_generation: u64,
listener_command_tx: Option<mpsc::UnboundedSender<ThreadListenerCommand>>,
current_turn_history: ThreadHistoryBuilder,
last_epiphany_jobs_by_job_id: HashMap<String, ThreadEpiphanyJob>,
listener_thread: Option<Weak<CodexThread>>,
}
@ -96,6 +98,7 @@ impl ThreadState {
}
self.listener_command_tx = None;
self.current_turn_history.reset();
self.last_epiphany_jobs_by_job_id.clear();
self.listener_thread = None;
}
@ -124,6 +127,18 @@ impl ThreadState {
self.current_turn_history.reset();
}
}
pub(crate) fn record_epiphany_jobs_update(&mut self, jobs: &[ThreadEpiphanyJob]) -> bool {
let mut changed = false;
for job in jobs {
if self.last_epiphany_jobs_by_job_id.get(job.id.as_str()) != Some(job) {
changed = true;
self.last_epiphany_jobs_by_job_id
.insert(job.id.clone(), job.clone());
}
}
changed
}
}
pub(crate) async fn resolve_server_request_on_thread_listener(