From 07becb1ca7ac5ade107a23c9da4954930e808202 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 14 May 2026 08:49:03 +0100 Subject: [PATCH] Move coordinator automation decision to bridge --- epiphany-codex-bridge/src/coordinator.rs | 128 ++++++++++++++++++ notes/fresh-workspace-handoff.md | 8 ++ state/ledgers.msgpack | Bin 115933 -> 116199 bytes state/map.yaml | 13 +- .../epiphany_automation.rs | 97 ++----------- 5 files changed, 157 insertions(+), 89 deletions(-) diff --git a/epiphany-codex-bridge/src/coordinator.rs b/epiphany-codex-bridge/src/coordinator.rs index 2a070459..9f673e69 100644 --- a/epiphany-codex-bridge/src/coordinator.rs +++ b/epiphany-codex-bridge/src/coordinator.rs @@ -19,8 +19,11 @@ use codex_app_server_protocol::ThreadEpiphanyRoleStatus; use codex_app_server_protocol::ThreadEpiphanyRolesSource; use codex_app_server_protocol::ThreadEpiphanySceneAction; use codex_app_server_protocol::ThreadEpiphanyViewCoordinator; +use codex_core::EpiphanyJobLaunchRequest; use codex_protocol::protocol::EpiphanyAcceptanceReceipt; +use codex_protocol::protocol::EpiphanyRetrievalState; use codex_protocol::protocol::EpiphanyThreadState; +use codex_protocol::protocol::TokenUsageInfo; use epiphany_core::EpiphanyCoordinatorAction as CoreEpiphanyCoordinatorAction; use epiphany_core::EpiphanyCoordinatorAutomationAction as CoreEpiphanyCoordinatorAutomationAction; use epiphany_core::EpiphanyCoordinatorCrrcRecommendation; @@ -55,11 +58,18 @@ use epiphany_core::select_coordinator_automation_action; use crate::launch::EPIPHANY_IMAGINATION_OWNER_ROLE; use crate::launch::EPIPHANY_IMAGINATION_ROLE_BINDING_ID; use crate::launch::EPIPHANY_MODELING_ROLE_BINDING_ID; +use crate::launch::EPIPHANY_REORIENT_LAUNCH_BINDING_ID; use crate::launch::EPIPHANY_REORIENT_OWNER_ROLE; use crate::launch::EPIPHANY_VERIFICATION_ROLE_BINDING_ID; +use crate::launch::build_epiphany_reorient_launch_request; use crate::launch::epiphany_role_label; use crate::launch::render_epiphany_coordinator_note; use crate::mutation::epiphany_modeling_finding_has_reviewable_state_patch; +use crate::pressure::map_epiphany_pressure; +use crate::reorient::EpiphanyFreshnessWatcherSnapshot; +use crate::reorient::map_epiphany_freshness; +use crate::reorient::map_epiphany_reorient; +use crate::runtime_results::load_epiphany_reorient_result_snapshot; use crate::runtime_results::load_epiphany_role_result_snapshot; use std::collections::HashSet; @@ -375,6 +385,124 @@ pub fn select_epiphany_coordinator_automation_action( )) } +pub struct EpiphanyCoordinatorAutomationInput<'a> { + pub thread_id: &'a str, + pub state: &'a EpiphanyThreadState, + pub retrieval_override: &'a EpiphanyRetrievalState, + pub watcher_snapshot: EpiphanyFreshnessWatcherSnapshot<'a>, + pub token_usage_info: Option<&'a TokenUsageInfo>, + pub runtime_store_path: &'a Path, + pub force_checkpoint_compaction: bool, +} + +pub struct EpiphanyCoordinatorAutomationVerdict { + pub action: EpiphanyCoordinatorAutomationAction, + pub launch_request: Option, +} + +pub async fn select_epiphany_coordinator_automation( + input: EpiphanyCoordinatorAutomationInput<'_>, +) -> EpiphanyCoordinatorAutomationVerdict { + let (_state_revision, retrieval, graph, watcher) = map_epiphany_freshness( + Some(input.state), + Some(input.retrieval_override), + Some(input.watcher_snapshot), + ); + let pressure = map_epiphany_pressure(input.token_usage_info); + let (state_status, reorient_decision) = + map_epiphany_reorient(Some(input.state), &pressure, &retrieval, &graph, &watcher); + if state_status != ThreadEpiphanyReorientStateStatus::Ready { + return EpiphanyCoordinatorAutomationVerdict { + action: EpiphanyCoordinatorAutomationAction::None, + launch_request: None, + }; + } + + let jobs = crate::jobs::map_epiphany_jobs(Some(input.state), Some(input.retrieval_override)); + let reorient_job = jobs + .iter() + .find(|job| job.id == EPIPHANY_REORIENT_LAUNCH_BINDING_ID) + .cloned(); + let (reorient_result_status, reorient_finding, _) = load_epiphany_reorient_result_snapshot( + Some(input.state), + Some(input.runtime_store_path), + EPIPHANY_REORIENT_LAUNCH_BINDING_ID, + ) + .await; + let reorient_finding_accepted = reorient_finding + .as_ref() + .is_some_and(|finding| epiphany_reorient_finding_already_accepted(input.state, finding)); + let crrc_recommendation = map_epiphany_crrc_recommendation( + true, + state_status, + &pressure, + &reorient_decision, + reorient_result_status, + input.state.investigation_checkpoint.as_ref().is_some(), + reorient_finding.is_some(), + reorient_finding_accepted, + ); + let roles = map_epiphany_roles( + Some(input.state), + &jobs, + &reorient_decision, + &pressure, + &crrc_recommendation, + reorient_result_status, + reorient_job.as_ref(), + ); + let coordinator = derive_epiphany_coordinator_status( + Some(input.state), + Some(input.runtime_store_path), + state_status, + &pressure, + &crrc_recommendation, + roles, + Some(&reorient_decision), + reorient_result_status, + reorient_finding.as_ref(), + input.state.investigation_checkpoint.as_ref().is_some(), + ) + .await; + + let action = select_epiphany_coordinator_automation_action( + &coordinator.decision, + input.force_checkpoint_compaction, + ); + let launch_request = match action { + EpiphanyCoordinatorAutomationAction::LaunchReorientWorker => input + .state + .investigation_checkpoint + .as_ref() + .map(|checkpoint| { + build_epiphany_reorient_launch_request( + input.thread_id, + Some(input.state.revision), + None, + input.state, + checkpoint, + &reorient_decision, + ) + }), + EpiphanyCoordinatorAutomationAction::None + | EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient => None, + }; + let action = if matches!( + action, + EpiphanyCoordinatorAutomationAction::LaunchReorientWorker + ) && launch_request.is_none() + { + EpiphanyCoordinatorAutomationAction::None + } else { + action + }; + + EpiphanyCoordinatorAutomationVerdict { + action, + launch_request, + } +} + pub fn map_epiphany_coordinator( state_status: ThreadEpiphanyReorientStateStatus, checkpoint_present: bool, diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 636688b0..2957099f 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -787,6 +787,14 @@ persisted token-usage snapshot and maps it back to the rebuilt turn owner. `token_usage_replay.rs` is now just the host shell: read rollout items, choose whether to replay, and send a connection-scoped JSON-RPC notification. +Safe-boundary coordinator automation decision composition has followed. +`epiphany-codex-bridge::coordinator::select_epiphany_coordinator_automation` +now owns the freshness/pressure/reorient/jobs/CRRC/roles/coordinator +composition and returns either no action, compact, or a typed reorient launch +request. `epiphany_automation.rs` still gathers live host facts and executes +the host side effects: submit `Op::Compact`, call `epiphany_launch_job`, and +emit JSON-RPC state-update notifications. + The MCP config path is now product-quarantined. `Config::to_mcp_config` keeps user-declared MCP servers but no longer accepts a `PluginsManager`, folds plugin-provided MCP servers into the runtime, enables Codex apps, enables skill diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index b12826e77d1cc8f1b6fad72bcc8acac0f9038140..a0e991c0eeb03f46f221d7839a245b7155384b1a 100644 GIT binary patch delta 202 zcmccH$^N{XeS$L6(X|^@r!g`bH}7HGzK4;K`|$M5hZu!fOf0O7CqJB_&3)WAzbrLH zp*S%ub^2ZvMn!>?)a1mtU@so>-ERT2!o%kzZV*kd~NSQml}em!gnbk(yjul3ENl1}Lgul$u|ZnVMIk wo0C|Ymz<$coSBlUkeZg33eu6Ems6?Nu*Jy0$V}J3RM*fX#K7DN` or `` prompt compatibility, memory usage classification, snapshot normalization, or cleanup paths. Remaining product-shaped skill residue is also cut from memory templates, app-server docs/tests/warnings, TUI tooltips/placeholders/warnings/comments, app-server v1 docs, agent-tool schema text, stale `UserInput::Skill` test arms, and obsolete feature flags/schema entries. App-instruction prompt compatibility is cut too: no `APPS_INSTRUCTIONS` protocol constants, no `` snapshot canonicalization, and MCP fixture tests now use neutral `demo_mcp` / `calendar_mcp` names instead of `codex_apps` / `/api/codex/apps` branding. The TUI app connector product path is also deleted: no `/apps` slash command, app-link view, connector AppEvent variants, app connector cache/popup/refresh loop, app mention insertion, `app://` mention codec support, stale app-popup tests, or TUI browser-opening shim remains. The `app/list` route and `app/list/updated` notification are gone from app-server protocol, generated schemas, app-server dispatch, test helpers, and docs; `AppInfo`/app-list metadata schema structs are deleted; the orphaned `codex-connectors` crate and `core::connectors` shim are deleted from the workspace; Codex app analytics no longer tracks `codex_app_mentioned` / `codex_app_used` facts, events, reducer arms, client APIs, or dedupe cache state; `[apps.*]`, `Feature::Apps`, `include_apps_instructions`, app config/read schemas, app managed requirements, app prompt tests, and app file-MCP tests are gone; `Feature::Plugins`, `Feature::RemotePlugin`, `plugin://` structured mentions, plugin instruction tags, and stale plugin popup snapshots are gone too; `[plugins]` / `[marketplaces]`, `PluginConfig`, `MarketplaceConfig`, marketplace edit helpers, stale plugin/external-agent README route claims, and generated schema entries are removed from config; and the bundled `codex-skills` sample-skill crate plus embedded imagegen/openai-docs/plugin-creator/skill-creator/skill-installer payloads are deleted from the workspace. The vendored `vendor/codex/sdk` subtree is deleted as non-spine app-server client/release scaffolding, including stale generated Python SDK route types for removed product surfaces; the root pnpm workspace no longer lists `sdk/typescript`. Live MCP tool approval is named `McpToolApproval`, not `AppToolApproval`, and remains under MCP server config; live structured user-input mentions are MCP-shaped only. `codex_message_processor.rs` is about 9,482 lines; root `message_processor.rs` is about 1,292 lines after the external-agent/config-product cuts; `epiphany_read_routes.rs` is about 579 lines after the full view and read-response extractions; `epiphany_mutation_routes.rs` is about 694 lines after route-load/notification helper cuts and product-surface starvation; the old vendored `epiphany_state_helpers.rs` rollout-state loader is deleted and its remaining function lives in `epiphany-codex-bridge::state`; and `codex_thread.rs` is about 1,425 lines after state-update law moved native. `epiphany-openai-adapter` defines CultCache `DatabaseEntry` request/event/receipt/auth-status documents with no Codex app-server or generic JSON dependency. `epiphany-openai-codex-spine` owns the native OpenAI model transport path: it exposes `default_codex_home` and `auth_manager`, projects `epiphany-openai-auth-spine` `AuthManager` / `CodexAuth` into typed `EpiphanyOpenAiAdapterStatus`, builds a local serializable OpenAI Responses request body instead of Codex protocol request/item/config types, opens HTTP Responses streams through `codex-client`, parses Responses SSE frames into typed stream events and receipts, and no longer directly or transitively depends on `codex-login`, `codex-app-server-protocol`, `codex-protocol`, `codex-api`, `codex-model-provider`, or `codex-model-provider-info`. Native auth currently covers env/file/keyring/auto credentials plus ChatGPT token refresh; external bearer command auth and agent identity remain explicit compatibility gaps. `epiphany-openai-runtime` no longer depends directly on `codex-login`; it writes OpenAI status/request/stream-event/receipt records plus runtime sessions/jobs/results/events into `epiphany-core::runtime_spine_cache`, can emit the assistant text artifact for legacy result consumers, is now the default executor for startup-only repo birth specialists, and now has `run-worker` to load a durable `EpiphanyRuntimeWorkerLaunchRequest`, call the typed OpenAI runtime through the spine, persist typed `EpiphanyRuntimeRoleWorkerResult` / `EpiphanyRuntimeReorientWorkerResult` documents, and complete the original heartbeat/specialist runtime job without Codex worker execution; `codex exec` survives only as explicit fallback in repo-birth startup. `epiphany-mvp-coordinator` now invokes `epiphany-openai-runtime run-worker` for launched runtime job ids and no longer creates a second coordinator-owned shadow runtime job/result as a compensator. `epiphany-core::plan_runtime_spine_heartbeat_launch` validates launch contracts/conflicts and projects durable job bindings/runtime links; `epiphany-core::open_runtime_spine_heartbeat_job` owns the initialize-runtime/ensure-session/create-job sequence and now persists `EpiphanyRuntimeWorkerLaunchRequest` CultCache records keyed by runtime job id, with indexed kind/contract fields plus MessagePack-encoded typed launch documents instead of discarding work intent after queue receipt creation; `roleResult` and `reorientResult` now require typed worker result documents; generic job summaries are lifecycle receipts, not reviewable findings; `epiphany-core::EpiphanyStateUpdate`, `epiphany_state_update_validation_errors`, and `apply_epiphany_state_update` own state-update document shape and mutation law while `codex-core` calls them for compatibility. Vendored `CodexThread::epiphany_launch_job` is now mostly revision check, persistence validation, launch request handoff, and rollout/session writeback for the compatibility route. Vendored `codex-core` no longer has `epiphany_distillation`, `epiphany_promotion`, `epiphany_proposal`, or `epiphany_retrieval` husk modules; it re-exports native surfaces from `epiphany-core` directly, and now owns only the real Codex rollout adapter because it is a host seam. `epiphany-runtime-spine` advertises OpenAI adapter status/model-request/stream-event/receipt CultNet schemas and mutation contracts, so the native caller now has a real storage route instead of only JSON-RPC. MCP itself is correctly JSON at its protocol edge; the target is typed Epiphany intent/result/receipt wrappers around MCP, not replacing MCP JSON. The current OpenAI request path no longer pulls `codex-login` or `codex-api`; the next OpenAI spine decision is whether external bearer or agent-identity auth deserves native implementation or remains sealed legacy Codex compatibility. latest_cut: > - Token-usage rollout projection moved out of the vendored app-server helper - and into `epiphany-codex-bridge::token_usage`. App-server still owns host - lifecycle: reading rollout files, replaying connection-scoped JSON-RPC - token usage notifications, and preserving response ordering. The bridge now - owns the pure projection from Codex rollout items to latest token-usage - pressure/turn-owner facts used by Epiphany views. + Safe-boundary coordinator automation decision composition moved from + vendored `epiphany_automation.rs` into + `epiphany-codex-bridge::coordinator::select_epiphany_coordinator_automation`. + App-server still gathers host facts and executes host actions: submit + compact, launch the reorient job, and send JSON-RPC notifications. The + bridge now owns the freshness/pressure/reorient/jobs/CRRC/roles/coordinator + composition that decides which automation action is allowed. next_action: Continue starving coordinator/runtime execution and operator surfaces off app-server JSON-RPC and out of `codex_message_processor.rs`; next suspicion is physical extraction of Epiphany route/helper logic from vendored app-server without inventing compensator traits. diff --git a/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_automation.rs b/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_automation.rs index 7b38997e..85881fea 100644 --- a/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_automation.rs +++ b/vendor/codex/codex-rs/app-server/src/codex_message_processor/epiphany_automation.rs @@ -1,7 +1,6 @@ use std::sync::Arc; use codex_app_server_protocol::ServerNotification; -use codex_app_server_protocol::ThreadEpiphanyReorientStateStatus; use codex_app_server_protocol::ThreadEpiphanyStateUpdatedNotification; use codex_app_server_protocol::ThreadEpiphanyStateUpdatedSource; use codex_core::CodexThread; @@ -11,23 +10,14 @@ use codex_protocol::protocol::Op; use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo; use codex_protocol::user_input::UserInput as CoreInputItem; use epiphany_codex_bridge::coordinator::EpiphanyCoordinatorAutomationAction; -use epiphany_codex_bridge::coordinator::derive_epiphany_coordinator_status; -use epiphany_codex_bridge::coordinator::epiphany_reorient_finding_already_accepted; -use epiphany_codex_bridge::coordinator::map_epiphany_crrc_recommendation; -use epiphany_codex_bridge::coordinator::map_epiphany_roles; -use epiphany_codex_bridge::coordinator::select_epiphany_coordinator_automation_action; +use epiphany_codex_bridge::coordinator::EpiphanyCoordinatorAutomationInput; +use epiphany_codex_bridge::coordinator::select_epiphany_coordinator_automation; use epiphany_codex_bridge::invalidation::EpiphanyInvalidationManager; use epiphany_codex_bridge::invalidation::epiphany_freshness_watcher_snapshot; -use epiphany_codex_bridge::jobs::map_epiphany_jobs; -use epiphany_codex_bridge::launch::EPIPHANY_REORIENT_LAUNCH_BINDING_ID; -use epiphany_codex_bridge::launch::build_epiphany_reorient_launch_request; use epiphany_codex_bridge::mutation::epiphany_job_launch_changed_fields; use epiphany_codex_bridge::pressure::map_epiphany_pressure; use epiphany_codex_bridge::pressure::render_epiphany_pre_compaction_checkpoint_intervention; use epiphany_codex_bridge::pressure::should_run_epiphany_pre_compaction_checkpoint_intervention; -use epiphany_codex_bridge::reorient::map_epiphany_freshness; -use epiphany_codex_bridge::reorient::map_epiphany_reorient; -use epiphany_codex_bridge::runtime_results::load_epiphany_reorient_result_snapshot; use epiphany_codex_bridge::state::client_visible_live_thread_epiphany_state; use tokio::sync::Mutex; use tracing::warn; @@ -58,69 +48,18 @@ pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary( let token_usage_info = thread.token_usage_info().await; let runtime_store_path = thread.epiphany_runtime_spine_store_path().await; - let (_state_revision, retrieval, graph, watcher) = map_epiphany_freshness( - Some(&state), - Some(&retrieval_override), - Some(epiphany_freshness_watcher_snapshot(&watcher_snapshot)), - ); - let pressure = map_epiphany_pressure(token_usage_info.as_ref()); - let (state_status, reorient_decision) = - map_epiphany_reorient(Some(&state), &pressure, &retrieval, &graph, &watcher); - if state_status != ThreadEpiphanyReorientStateStatus::Ready { - return; - } - - let jobs = map_epiphany_jobs(Some(&state), Some(&retrieval_override)); - let reorient_job = jobs - .iter() - .find(|job| job.id == EPIPHANY_REORIENT_LAUNCH_BINDING_ID) - .cloned(); - let (reorient_result_status, reorient_finding, _) = load_epiphany_reorient_result_snapshot( - Some(&state), - Some(runtime_store_path.as_path()), - EPIPHANY_REORIENT_LAUNCH_BINDING_ID, - ) - .await; - let reorient_finding_accepted = reorient_finding - .as_ref() - .is_some_and(|finding| epiphany_reorient_finding_already_accepted(&state, finding)); - let crrc_recommendation = map_epiphany_crrc_recommendation( - true, - state_status, - &pressure, - &reorient_decision, - reorient_result_status, - state.investigation_checkpoint.as_ref().is_some(), - reorient_finding.is_some(), - reorient_finding_accepted, - ); - let roles = map_epiphany_roles( - Some(&state), - &jobs, - &reorient_decision, - &pressure, - &crrc_recommendation, - reorient_result_status, - reorient_job.as_ref(), - ); - let coordinator = derive_epiphany_coordinator_status( - Some(&state), - Some(runtime_store_path.as_path()), - state_status, - &pressure, - &crrc_recommendation, - roles, - Some(&reorient_decision), - reorient_result_status, - reorient_finding.as_ref(), - state.investigation_checkpoint.as_ref().is_some(), - ) - .await; - - match select_epiphany_coordinator_automation_action( - &coordinator.decision, + let verdict = select_epiphany_coordinator_automation(EpiphanyCoordinatorAutomationInput { + thread_id: &thread_id_text, + state: &state, + retrieval_override: &retrieval_override, + watcher_snapshot: epiphany_freshness_watcher_snapshot(&watcher_snapshot), + token_usage_info: token_usage_info.as_ref(), + runtime_store_path: runtime_store_path.as_path(), force_checkpoint_compaction, - ) { + }) + .await; + + match verdict.action { EpiphanyCoordinatorAutomationAction::None => {} EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient => { if let Err(err) = thread.submit(Op::Compact).await { @@ -130,17 +69,9 @@ pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary( } } EpiphanyCoordinatorAutomationAction::LaunchReorientWorker => { - let Some(checkpoint) = state.investigation_checkpoint.as_ref() else { + let Some(launch_request) = verdict.launch_request else { return; }; - let launch_request = build_epiphany_reorient_launch_request( - &thread_id_text, - Some(state.revision), - None, - &state, - checkpoint, - &reorient_decision, - ); let launched = match thread.epiphany_launch_job(launch_request).await { Ok(launched) => launched, Err(err) => {