From 262d9b87d1ead761e2857f3bc170d9b8c32b5417 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 13 May 2026 01:24:58 +0100 Subject: [PATCH] Extract Epiphany pressure projection --- notes/fresh-workspace-handoff.md | 7 +- notes/json-contamination-ledger.md | 2 +- state/ledgers.msgpack | Bin 72827 -> 73127 bytes state/map.yaml | 4 +- .../app-server/src/codex_message_processor.rs | 81 +---------------- .../app-server/src/epiphany_pressure.rs | 83 ++++++++++++++++++ vendor/codex/codex-rs/app-server/src/lib.rs | 1 + 7 files changed, 95 insertions(+), 83 deletions(-) create mode 100644 vendor/codex/codex-rs/app-server/src/epiphany_pressure.rs diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 76430b69..6e1a9f2b 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -425,14 +425,17 @@ about 19,377 lines. This is progress, not absolution: handlers, roles/coordinator mappers, route orchestration, accept policy plumbing, and tests still keep too much Epiphany inside Codex. The eighth cut moved retrieve projection into `vendor/codex/codex-rs/app-server/src/epiphany_retrieve.rs`, -taking the processor to about 19,307 lines. +taking the processor to about 19,307 lines. The ninth cut moved pressure and +pre-compaction checkpoint projection into +`vendor/codex/codex-rs/app-server/src/epiphany_pressure.rs`, taking the +processor to about 19,232 lines. Also: MCP itself is allowed to be JSON. The target is not "replace MCP JSON"; the target is an Epiphany-owned boundary that speaks typed Epiphany intent/result/receipt documents internally and normal MCP JSON-RPC externally. Continue with the actual whale-carcass cut: extract Epiphany roles/coordinator -projection mappers, route handlers, and remaining policy-shaped code out of +projection mappers as one owned block, then route handlers and remaining policy-shaped code out of `codex_message_processor.rs` into Epiphany-owned app-server modules, moving logic further into `epiphany-core` whenever it owns real policy. Success is a visibly smaller `codex_message_processor.rs`, not merely nicer payload names. Do diff --git a/notes/json-contamination-ledger.md b/notes/json-contamination-ledger.md index d35500d1..7eb3643e 100644 --- a/notes/json-contamination-ledger.md +++ b/notes/json-contamination-ledger.md @@ -62,6 +62,6 @@ Rule: JSON is allowed at external schema/wire boundaries. Inside Epiphany, live 3. Done: replace role `statePatch` JSON with typed map/planning/graph patch documents. `EpiphanyRoleFindingInterpretation` now carries `EpiphanyRoleStatePatchDocument`; app-server maps it to the legacy protocol patch without JSON round-tripping. 4. Done: replace runtime job result JSON projections. Runtime-spine results now project through typed role/reorient interpreters instead of `runtime_job_result_to_role_json` and `runtime_job_result_to_reorient_json`. 5. Done: replace app-server launch protocol `input_json` / `output_schema_json` with typed launch documents and `output_contract_id`. -6. Continue the whale-carcass cut: the first cuts moved Epiphany launch doctrine and protocol-to-core launch document mapping into `epiphany_launch.rs`, role/reorient result projection into `epiphany_results.rs`, scene projection into `epiphany_scene.rs`, freshness/reorientation mapping into `epiphany_reorient.rs`, context/planning/graph-query projection into `epiphany_context.rs`, jobs projection into `epiphany_jobs.rs`, and retrieve projection into `epiphany_retrieve.rs`, reducing `codex_message_processor.rs` from about 21,263 to 19,307 lines. Next move roles/coordinator projection mappers, route handlers, and policy-shaped code out of the processor; success means the processor keeps shrinking and ownership gets clearer, not merely better typed. +6. Continue the whale-carcass cut: the first cuts moved Epiphany launch doctrine and protocol-to-core launch document mapping into `epiphany_launch.rs`, role/reorient result projection into `epiphany_results.rs`, scene projection into `epiphany_scene.rs`, freshness/reorientation mapping into `epiphany_reorient.rs`, context/planning/graph-query projection into `epiphany_context.rs`, jobs projection into `epiphany_jobs.rs`, retrieve projection into `epiphany_retrieve.rs`, and pressure/pre-compaction checkpoint projection into `epiphany_pressure.rs`, reducing `codex_message_processor.rs` from about 21,263 to 19,232 lines. Next move roles/coordinator projection mappers, route handlers, and policy-shaped code out of the processor; success means the processor keeps shrinking and ownership gets clearer, not merely better typed. 7. Seal MCP payloads behind an Epiphany boundary that speaks typed Epiphany documents internally and MCP JSON externally. 8. Audit heartbeat cognition values and either type them or explicitly expire them. diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index 9a2852e28aced7628ec0b9f317b6e3508492af7d..61ff38e093d854bb46dc805ffb45a01f570690ac 100644 GIT binary patch delta 212 zcmeypgJt<{mI=yCvbh^or!g|_Y2L%QeGen!N~y{ErMz5*#vuj6e*TlA++7pORXkP+XE&RF+thnV+YSTv}qKkegqYnxarplv-R|T9m4gmk{2efS5$1RFx^0yQ8GNWAhRGNF|RUSp(M4qq*$RKvA7s$f`+M)zNwL>HPER1 wWT0XBMGATO) -> ThreadEpiphanyPressure { - map_core_epiphany_pressure(derive_pressure_view(info)) -} - -fn map_core_epiphany_pressure(pressure: EpiphanyPressure) -> ThreadEpiphanyPressure { - ThreadEpiphanyPressure { - status: match pressure.status { - CoreEpiphanyPressureStatus::Unknown => ThreadEpiphanyPressureStatus::Unknown, - CoreEpiphanyPressureStatus::Ready => ThreadEpiphanyPressureStatus::Ready, - }, - level: match pressure.level { - CoreEpiphanyPressureLevel::Unknown => ThreadEpiphanyPressureLevel::Unknown, - CoreEpiphanyPressureLevel::Low => ThreadEpiphanyPressureLevel::Low, - CoreEpiphanyPressureLevel::Elevated => ThreadEpiphanyPressureLevel::Elevated, - CoreEpiphanyPressureLevel::High => ThreadEpiphanyPressureLevel::High, - CoreEpiphanyPressureLevel::Critical => ThreadEpiphanyPressureLevel::Critical, - }, - basis: match pressure.basis { - CoreEpiphanyPressureBasis::Unknown => ThreadEpiphanyPressureBasis::Unknown, - CoreEpiphanyPressureBasis::AutoCompactLimit => { - ThreadEpiphanyPressureBasis::AutoCompactLimit - } - CoreEpiphanyPressureBasis::ModelContextWindow => { - ThreadEpiphanyPressureBasis::ModelContextWindow - } - }, - used_tokens: pressure.used_tokens, - model_context_window: pressure.model_context_window, - model_auto_compact_token_limit: pressure.model_auto_compact_token_limit, - remaining_tokens: pressure.remaining_tokens, - ratio_per_mille: pressure.ratio_per_mille, - should_prepare_compaction: pressure.should_prepare_compaction, - note: pressure.note, - } -} - -fn should_run_epiphany_pre_compaction_checkpoint_intervention( - pressure: &ThreadEpiphanyPressure, -) -> bool { - pressure.status == ThreadEpiphanyPressureStatus::Ready && pressure.should_prepare_compaction -} - -fn render_epiphany_pre_compaction_checkpoint_intervention( - pressure: &ThreadEpiphanyPressure, -) -> String { - let usage = match ( - pressure.used_tokens, - pressure.remaining_tokens, - pressure.ratio_per_mille, - ) { - (Some(used), Some(remaining), Some(ratio)) => format!( - "{used} tokens used, {remaining} remaining, {}.{}% of the selected limit", - ratio / 10, - ratio % 10 - ), - (Some(used), _, _) => format!("{used} tokens used"), - _ => "token usage known only as a pressure threshold crossing".to_string(), - }; - let template = epiphany_agent_prompt_with_memory( - &epiphany_specialist_prompt_config() - .crrc - .pre_compaction_checkpoint_intervention, - ); - template - .trim() - .replace("{pressure_level}", pressure_level_label(pressure.level)) - .replace("{usage}", &usage) -} - pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary( thread_id: ThreadId, thread: Arc, diff --git a/vendor/codex/codex-rs/app-server/src/epiphany_pressure.rs b/vendor/codex/codex-rs/app-server/src/epiphany_pressure.rs new file mode 100644 index 00000000..700431c9 --- /dev/null +++ b/vendor/codex/codex-rs/app-server/src/epiphany_pressure.rs @@ -0,0 +1,83 @@ +use codex_app_server_protocol::ThreadEpiphanyPressure; +use codex_app_server_protocol::ThreadEpiphanyPressureBasis; +use codex_app_server_protocol::ThreadEpiphanyPressureLevel; +use codex_app_server_protocol::ThreadEpiphanyPressureStatus; +use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo; +use epiphany_core::EpiphanyPressure; +use epiphany_core::EpiphanyPressureBasis as CoreEpiphanyPressureBasis; +use epiphany_core::EpiphanyPressureLevel as CoreEpiphanyPressureLevel; +use epiphany_core::EpiphanyPressureStatus as CoreEpiphanyPressureStatus; +use epiphany_core::derive_pressure_view; + +use crate::epiphany_launch::epiphany_agent_prompt_with_memory; +use crate::epiphany_launch::epiphany_specialist_prompt_config; +use crate::epiphany_launch::pressure_level_label; + +pub(super) fn map_epiphany_pressure(info: Option<&CoreTokenUsageInfo>) -> ThreadEpiphanyPressure { + map_core_epiphany_pressure(derive_pressure_view(info)) +} + +fn map_core_epiphany_pressure(pressure: EpiphanyPressure) -> ThreadEpiphanyPressure { + ThreadEpiphanyPressure { + status: match pressure.status { + CoreEpiphanyPressureStatus::Unknown => ThreadEpiphanyPressureStatus::Unknown, + CoreEpiphanyPressureStatus::Ready => ThreadEpiphanyPressureStatus::Ready, + }, + level: match pressure.level { + CoreEpiphanyPressureLevel::Unknown => ThreadEpiphanyPressureLevel::Unknown, + CoreEpiphanyPressureLevel::Low => ThreadEpiphanyPressureLevel::Low, + CoreEpiphanyPressureLevel::Elevated => ThreadEpiphanyPressureLevel::Elevated, + CoreEpiphanyPressureLevel::High => ThreadEpiphanyPressureLevel::High, + CoreEpiphanyPressureLevel::Critical => ThreadEpiphanyPressureLevel::Critical, + }, + basis: match pressure.basis { + CoreEpiphanyPressureBasis::Unknown => ThreadEpiphanyPressureBasis::Unknown, + CoreEpiphanyPressureBasis::AutoCompactLimit => { + ThreadEpiphanyPressureBasis::AutoCompactLimit + } + CoreEpiphanyPressureBasis::ModelContextWindow => { + ThreadEpiphanyPressureBasis::ModelContextWindow + } + }, + used_tokens: pressure.used_tokens, + model_context_window: pressure.model_context_window, + model_auto_compact_token_limit: pressure.model_auto_compact_token_limit, + remaining_tokens: pressure.remaining_tokens, + ratio_per_mille: pressure.ratio_per_mille, + should_prepare_compaction: pressure.should_prepare_compaction, + note: pressure.note, + } +} + +pub(super) fn should_run_epiphany_pre_compaction_checkpoint_intervention( + pressure: &ThreadEpiphanyPressure, +) -> bool { + pressure.status == ThreadEpiphanyPressureStatus::Ready && pressure.should_prepare_compaction +} + +pub(super) fn render_epiphany_pre_compaction_checkpoint_intervention( + pressure: &ThreadEpiphanyPressure, +) -> String { + let usage = match ( + pressure.used_tokens, + pressure.remaining_tokens, + pressure.ratio_per_mille, + ) { + (Some(used), Some(remaining), Some(ratio)) => format!( + "{used} tokens used, {remaining} remaining, {}.{}% of the selected limit", + ratio / 10, + ratio % 10 + ), + (Some(used), _, _) => format!("{used} tokens used"), + _ => "token usage known only as a pressure threshold crossing".to_string(), + }; + let template = epiphany_agent_prompt_with_memory( + &epiphany_specialist_prompt_config() + .crrc + .pre_compaction_checkpoint_intervention, + ); + template + .trim() + .replace("{pressure_level}", pressure_level_label(pressure.level)) + .replace("{usage}", &usage) +} diff --git a/vendor/codex/codex-rs/app-server/src/lib.rs b/vendor/codex/codex-rs/app-server/src/lib.rs index 3f509062..bc4b3b8a 100644 --- a/vendor/codex/codex-rs/app-server/src/lib.rs +++ b/vendor/codex/codex-rs/app-server/src/lib.rs @@ -79,6 +79,7 @@ mod epiphany_context; mod epiphany_invalidation; mod epiphany_jobs; mod epiphany_launch; +mod epiphany_pressure; mod epiphany_reorient; mod epiphany_results; mod epiphany_retrieve;