From 86ba6de5a9985c3cc40fa397b284c4976b9c22d1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 28 Apr 2026 19:03:25 +0100 Subject: [PATCH] Add Epiphany CRRC safe-boundary automation --- notes/epiphany-fork-implementation-plan.md | 13 +- notes/fresh-workspace-handoff.md | 18 +- state/evidence.jsonl | 1 + state/map.yaml | 14 +- .../app-server/src/bespoke_event_handling.rs | 18 ++ .../app-server/src/codex_message_processor.rs | 203 ++++++++++++++++++ 6 files changed, 248 insertions(+), 19 deletions(-) diff --git a/notes/epiphany-fork-implementation-plan.md b/notes/epiphany-fork-implementation-plan.md index 15f7b83c..f31d53cb 100644 --- a/notes/epiphany-fork-implementation-plan.md +++ b/notes/epiphany-fork-implementation-plan.md @@ -76,6 +76,7 @@ The landed machine now has: - explicit Phase 6 reorient-worker finding acceptance through `thread/epiphany/reorientAccept` - read-only Phase 6 CRRC coordinator recommendation through `thread/epiphany/crrc` - read-only Phase 6 fixed-lane MVP coordinator recommendation through `thread/epiphany/coordinator`, composing roles, pressure, reorient, CRRC, role results, and reorient result without mutation +- limited native Phase 6 CRRC automation at turn-complete safe boundaries, restricted to coordinator-approved compact and fixed reorient-worker launch actions - read-only Phase 6 role ownership through `thread/epiphany/roles`, projecting implementation, modeling/checkpoint, verification/review, and reorientation lanes from typed state plus jobs/CRRC/result signals - explicit Phase 6 role launch/read-back through `thread/epiphany/roleLaunch` and read-only `thread/epiphany/roleResult`, limited to fixed modeling/checkpoint and verification/review templates over the existing job-control seam - first Phase 6 dogfood operator view through `tools/epiphany_mvp_status.py` @@ -111,6 +112,7 @@ These boundaries are more important than the individual method names: - `thread/epiphany/reorient` is a bounded policy verdict, not an automatic runtime coordinator, scheduler, compactor, or hidden state writer. - `thread/epiphany/crrc` is a read-only coordinator recommendation over existing signals, not a scheduler, launch button, acceptance gate, compactor, or hidden state writer. - `thread/epiphany/coordinator` is a read-only fixed-lane MVP policy projection over existing signals, not a scheduler, launcher, acceptance gate, compactor, or hidden state writer. +- Native CRRC automation may act only at safe turn-complete boundaries and only for coordinator-approved `compactRehydrateReorient` and `launchReorientWorker` actions. It must not auto-launch modeling or verification, auto-accept semantic findings, promote evidence, edit implementation code, or silently continue after unresolved drift. - `thread/epiphany/roles` is a read-only role ownership projection, not a specialist scheduler, marketplace, launcher, acceptance gate, or second job backend. - `thread/epiphany/roleLaunch` is a bounded authority surface for fixed modeling/checkpoint and verification/review templates, not a broad scheduler or specialist marketplace. - `thread/epiphany/roleResult` is a read-only result projection, not a promotion gate, state writer, scheduler, or hidden continuation trigger. @@ -209,9 +211,10 @@ workspace, report through `report_agent_job_result`, and return a completed `checkpoint-ready` finding through `roleResult`. The coordinator runner then proved the sequence-locked MVP policy across cold start, clean checkpoint, modeling, verification, CRRC drift/reorient, and high-pressure compact/dry-run -paths. The next MVP question is human operator testing, not another imagined -organ: use the landed coordinator/status/artifact loop, then fix only concrete -blockers. +paths. Native CRRC automation then wired the proved policy into turn-complete +safe boundaries for compact and fixed reorient-worker launch only. The next MVP +question is human operator testing, not another imagined organ: use the landed +coordinator/status/artifact loop, then fix only concrete blockers. ## Phase 6 Direction @@ -221,7 +224,7 @@ Useful candidates: 1. Put the fixed-lane coordinator MVP in front of a human operator through status/artifact review, then fix concrete usability blockers. 2. Keep accepted worker findings review-gated; do not convert acceptance into automatic promotion of arbitrary worker output. -3. After the coordinator artifacts prove the policy, add only limited safe-boundary CRRC automation: compact/resume/reorient or launch a bounded reorient-worker when the coordinator says it is safe, without silently accepting semantic findings. +3. Dogfood the landed safe-boundary CRRC automation on real work and fix only concrete blockers in compact/reorient-worker launch behavior. Do not spend Phase 6 polishing Phase 5 out of anxiety. The Phase 5 smoke harness is a regression guardrail, not a ritual drum circle for summoning more tiny @@ -236,7 +239,7 @@ These remain later work: - richer evidence and graph-shard inspection beyond the landed targeted context read - richer role ergonomics after the fixed single-user coordinator proves useful - mutation gates that warn or block broad writes when map freshness is stale -- broader CRRC runtime coordination beyond the narrow safe-boundary actions proved by the MVP coordinator +- broader CRRC runtime coordination beyond the landed narrow safe-boundary compact and fixed reorient-worker launch actions - GUI workflows for graph, evidence, job, invariant, and frontier steering Do not start these from vibes. Each one needs a source-grounded slice plan and a diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index e316b681..bd522659 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -31,6 +31,7 @@ Do not trust this file for the exact live HEAD. Always check git. - Phase 6 has read-only `thread/epiphany/scene`, `thread/epiphany/jobs`, `thread/epiphany/roles`, `thread/epiphany/freshness`, `thread/epiphany/context`, `thread/epiphany/pressure`, `thread/epiphany/reorient`, `thread/epiphany/crrc`, `thread/epiphany/coordinator`, `thread/epiphany/reorientResult`, and `thread/epiphany/roleResult`; durable `jobBindings` now act as a thin Epiphany-owned launcher seam with launcher id, authority scope, and backend kind/job id, `thread/epiphany/jobLaunch` / `thread/epiphany/jobInterrupt` / `thread/epiphany/roleLaunch` now provide explicit bounded authority over that seam, jobs can overlay it onto live runtime `agent_jobs` progress for loaded threads, and `thread/epiphany/jobsUpdated` now translates live `agent_job_progress:{json}` background events into changed launcher-bound notifications without polling or scheduling. Freshness carries watcher-backed invalidation inputs, roles project implementation/modeling/verification/reorientation ownership from existing signals without becoming a scheduler, `roleLaunch` can launch only fixed modeling/checkpoint or verification/review specialists, `roleResult` reads those outputs back as reviewable findings without mutation, reorient turns checkpoint plus freshness/pressure/watcher signals into a read-only resume-versus-regather verdict, `thread/epiphany/reorientLaunch` is the explicit runtime consumer over that verdict, `thread/epiphany/reorientResult` reads the launched worker output back as a reviewable finding without mutation or promotion, `thread/epiphany/reorientAccept` explicitly banks completed findings into accepted observation/evidence plus optional scratch/checkpoint state, `thread/epiphany/crrc` recommends the next explicit CRRC action without launching, accepting, compacting, scheduling, or mutating, and `thread/epiphany/coordinator` composes those signals into a fixed-lane MVP action recommendation without becoming a writer. - `tools/epiphany_mvp_status.py` is the first dogfood operator view. It starts or reads a thread through app-server and prints scene, pressure, reorient, jobs, roles, modeling/verification role result read-backs, reorient result, and CRRC recommendation as text or JSON. - `tools/epiphany_mvp_coordinator.py` is the first auditable fixed-lane coordinator runner. It starts or reads a thread through app-server, follows the harness-native coordinator action, can auto-launch modeling, verification, or reorient-worker jobs, keeps semantic findings review-gated by default, and writes summary, steps, rendered snapshots, transcript, stderr, and final next-action artifacts under `.epiphany-dogfood/coordinator` or a caller-provided artifact directory. +- Native CRRC automation is now landed only at turn-complete safe boundaries. It may submit `Op::Compact` for coordinator-approved `compactRehydrateReorient`, and it may launch the fixed `reorient-worker` for coordinator-approved `launchReorientWorker`. It does not auto-launch modeling/verification, accept findings, promote evidence, edit implementation code, or keep going after reviewable semantic output. - `tools/epiphany_mvp_dogfood.py` is the first auditable dogfood runner. It drives a bounded state/role/CRRC/reorientation loop and writes local artifacts under `.epiphany-dogfood/mvp-loop`, including raw app-server transcript, rendered snapshots, final status, vanilla-reference prompt/output, and comparison notes. - `tools/epiphany_mvp_live_specialist.py` is the first auditable live-specialist runner. It launches a real role specialist through `thread/epiphany/roleLaunch`, lets the spawned worker report through `report_agent_job_result`, reads it back through `thread/epiphany/roleResult`, and writes local artifacts under `.epiphany-dogfood/live-specialist`. - Internal `agent_job:` workers now get the reporting tool independent of the user-facing CSV spawn feature, and ephemeral worker sessions can initialize the sqlite state runtime on demand so specialist reports land in the shared backend. @@ -86,6 +87,7 @@ The current spine: - read-only CRRC reorientation policy through `thread/epiphany/reorient` - read-only CRRC next-action recommendation through `thread/epiphany/crrc` - read-only fixed-lane MVP action recommendation through `thread/epiphany/coordinator` +- limited turn-complete CRRC automation for coordinator-approved compact and fixed reorient-worker launch actions - durable investigation checkpoint packet through typed state, prompt, scene, and context - live scene app-server smoke through `tools/epiphany_phase6_scene_smoke.py` - live jobs app-server smoke through `tools/epiphany_phase6_jobs_smoke.py` @@ -291,13 +293,13 @@ The fixed-lane coordinator MVP is now testable. Put it in front of a human operator through `tools/epiphany_mvp_coordinator.py`, `tools/epiphany_mvp_status.py`, `.epiphany-dogfood/coordinator`, `.epiphany-dogfood/mvp-loop`, and `.epiphany-dogfood/live-specialist`. Fix -concrete blockers in that operator loop. The next likely organ, after real -operator artifacts justify it, is limited safe-boundary CRRC execution that can -compact/resume/reorient or launch a bounded reorient-worker when the read-only -coordinator says it is safe. CRRC is not a specialist-agent persona; the -reorient-worker it may launch is the specialist. Do not turn the coordinator -into a broad hidden dispatcher, arbitrary marketplace, alternate job backend, or -GUI-as-source-of-truth. +concrete blockers in that operator loop. Limited safe-boundary CRRC execution is +already wired for compact and fixed reorient-worker launch actions, so the next +unknown is how it behaves under real operator pressure, not which speculative +organ to invent. CRRC is not a specialist-agent persona; the reorient-worker it +may launch is the specialist. Do not turn the coordinator into a broad hidden +dispatcher, arbitrary marketplace, alternate job backend, automatic semantic +acceptance path, or GUI-as-source-of-truth. Live `thread/epiphany/scene`, `thread/epiphany/jobs`, `thread/epiphany/roles`, `thread/epiphany/freshness`, `thread/epiphany/context`, @@ -312,7 +314,7 @@ are now guardrails, not the next organs. - automatic observation promotion - broad specialist-agent scheduling beyond the fixed single-user role lanes - GUI-as-source-of-truth -- automatic runtime CRRC execution acting on the landed recommendation/verdict and launch/read-back path +- broad runtime CRRC execution beyond the landed safe-boundary compact and fixed reorient-worker launch actions - Epiphany-owned long-running job execution beyond the current runtime `agent_jobs` seam - broad event stream beyond the landed state update notification diff --git a/state/evidence.jsonl b/state/evidence.jsonl index b70a0685..8143ac58 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -60,3 +60,4 @@ {"ts": "2026-04-28T16:52:18+00:00", "type": "implementation", "status": "ok", "note": "Clarified MVP direction: marketplace and alternate job backend are not active concerns; the scheduler-shaped need is a narrow auditable fixed-lane role coordinator over modeling, implementation, verification, and CRRC handoffs, not a broad ambient dispatcher."} {"ts": "2026-04-28T16:56:56+00:00", "type": "design", "status": "ok", "note": "Clarified CRRC architecture: CRRC is harness workflow automation that watches context pressure and continuity, persists/checkpoints, triggers compact-rehydrate-reorient behavior, and may launch a bounded reorient-worker specialist; CRRC itself is not a specialist-agent persona."} {"ts": "2026-04-28T18:33:53+01:00", "type": "implementation", "status": "ok", "note": "Landed the fixed-lane Epiphany MVP coordinator: read-only thread/epiphany/coordinator composes roles, pressure, reorient, CRRC, role results, and reorient result into review-gated action recommendations, and tools/epiphany_mvp_coordinator.py drives the same policy through app-server APIs with auditable artifacts. Coordinator smoke covered cold prepareCheckpoint, clean continueImplementation, modeling review, verification review, source-drift reorient review, high-pressure compact/dry-run, and complete artifact bundles."} +{"ts": "2026-04-28T18:56:47+01:00", "type": "implementation", "status": "ok", "note": "Landed limited native CRRC automation at turn-complete safe boundaries. It reuses the read-only coordinator policy and only auto-runs compactRehydrateReorient via Op::Compact or launchReorientWorker via the fixed reorient-worker job binding; modeling, verification, semantic acceptance, evidence promotion, implementation edits, and hidden continuation remain review-gated or manual. Focused coordinator automation assertions, job-progress event handling, coordinator smoke, status smoke, role smoke, and reorient-launch smoke passed."} diff --git a/state/map.yaml b/state/map.yaml index d52162c6..1fb2fae8 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -12,8 +12,8 @@ objective: explicit typed models of the codebase, active goal, evidence, and change machinery they are working on. current: > - Prove the Phase 6 MVP through the landed fixed-lane coordinator and - auditable operator artifacts before adding limited CRRC automation. + Dogfood the landed Phase 6 MVP coordinator and limited CRRC safe-boundary + automation on real local work before adding larger organs. constraints: - No frontier-scale training budget. @@ -54,8 +54,8 @@ data_flow: active_subgoals: - Treat Phase 5 update/distill/propose/promote/read/notification as complete enough unless a concrete regression appears. - Preserve read-only boundaries for retrieve, distill, propose, scene, jobs, roles, roleResult, freshness, context, pressure, reorient, crrc, coordinator, and reorientResult. - - Extend Phase 6 coordination only where it makes role dependencies explicit and auditable without becoming a broad ambient dispatcher. - - Put the dogfooded MVP loop in front of a human operator before adding larger organs. + - Dogfood the landed Phase 6 coordinator and limited safe-boundary CRRC automation before adding larger organs. + - Preserve review gates for semantic findings; automatic CRRC may compact or launch the fixed reorient-worker, but not accept or promote. - Keep persistent project state ruthlessly distilled so rehydration strengthens the next turn instead of bloating it. accepted_design: @@ -85,6 +85,7 @@ accepted_design: - A narrow auditable coordinator is likely central to the MVP because implementation, modeling/checkpoint, verification/review, and CRRC automation depend on each other's signals. CRRC itself is harness workflow automation, not a specialist-agent role: it watches pressure/continuity, ensures persistence, triggers compaction/rehydration/reorientation when needed, and may launch a bounded reorient-worker specialist when semantic regathering is required. - `thread/epiphany/coordinator` is the first harness-native fixed-lane coordinator surface: it composes roles, pressure, reorient, CRRC, roleResult, and reorientResult into a read-only action recommendation with target role, source signals, review requirements, automation eligibility, reason, and current lane summaries without mutating state or launching work. - `tools/epiphany_mvp_coordinator.py` is the first auditable coordinator runner: it drives the fixed single-user lanes through app-server APIs, may auto-launch modeling, verification, or reorient-worker jobs, keeps semantic findings review-gated by default, and writes coordinator summaries, JSONL steps, rendered snapshots, transcript, stderr, and final next-action artifacts. + - Limited native CRRC automation is now wired only at turn-complete safe boundaries: it can submit a compact operation when the read-only coordinator recommends `compactRehydrateReorient`, and it can launch the fixed `reorient-worker` when the coordinator recommends `launchReorientWorker`. It does not auto-launch modeling or verification, auto-accept findings, promote evidence, edit implementation code, or continue after reviewable semantic results. - `tools/epiphany_mvp_status.py` is the first dogfood operator view: it starts or reads a thread through app-server and renders scene, pressure, reorient, jobs, role result read-backs, reorient result, CRRC recommendation, and MVP role lanes as text or JSON without becoming a state store. - `thread/epiphany/roles` is the first harness-native role ownership surface: it projects implementation, modeling/checkpoint, verification/review, and reorientation lanes from authoritative typed state, bound jobs, pressure/reorient/CRRC, and worker result status without launching, scheduling, accepting, compacting, or mutating state. - `thread/epiphany/roleLaunch` and `thread/epiphany/roleResult` are the first explicit role-scoped specialist templates: modeling/checkpoint and verification/review can be launched through fixed bindings over the existing job-control seam and read back as reviewable findings, while implementation stays owned by the main coding agent and reorientation stays on the CRRC-specific launch/result path. @@ -121,6 +122,7 @@ landed_baseline: - Phase 6 role ownership: read-only `thread/epiphany/roles` over implementation, modeling/checkpoint, verification/review, and reorientation lanes. - Phase 6 role specialists: explicit `thread/epiphany/roleLaunch` and read-only `thread/epiphany/roleResult` templates for modeling/checkpoint and verification/review. - Phase 6 coordinator: read-only `thread/epiphany/coordinator` plus `tools/epiphany_mvp_coordinator.py` and smoke coverage for the fixed-lane MVP loop. + - Phase 6 safe-boundary CRRC automation: turn-complete automation limited to coordinator-approved compact and fixed reorient-worker launch actions. guardrails: - Use `tools/epiphany_phase5_smoke.py` before modifying update, distill, propose, promote, read-projection, or notification behavior. - Use `tools/epiphany_phase6_scene_smoke.py` before modifying scene projection behavior. @@ -156,5 +158,5 @@ rejected_paths: reason: persistent state that accumulates every local truth becomes memory cruft and weakens rehydration current_status: - summary: Phase 1 durable state, Phase 2 prompt integration, Phase 3 typed read, Phase 4 retrieval/indexing, Phase 5 update/distill/propose/promote/read/notification, and Phase 6 read-only scene, jobs, roles, freshness, targeted context, pressure, reorient, CRRC, coordinator, reorient launch/result/accept, explicit modeling/checkpoint and verification/review role launch/result templates, MVP status, dogfood runner, coordinator runner, and live specialist runner are landed. The coordinator endpoint and runner now make fixed-lane modeling, verification, implementation continuation, and CRRC handoffs explicit through review-gated artifacts over the existing app-server/job seam. Implementation remains main-agent work; CRRC remains workflow automation, not a specialist persona. Persistent state remains distilled; evidence is a belief ledger, not an activity feed. - next_action: Dogfood the fixed-lane coordinator on real local work through `tools/epiphany_mvp_coordinator.py` and its artifact bundle. Fix concrete operator-loop blockers only; defer limited automatic CRRC safe-boundary execution until the coordinator artifacts prove the policy, and do not build a broad ambient dispatcher, GUI-as-source-of-truth, arbitrary specialist marketplace, or alternate job backend for the MVP. + summary: Phase 1 durable state, Phase 2 prompt integration, Phase 3 typed read, Phase 4 retrieval/indexing, Phase 5 update/distill/propose/promote/read/notification, and Phase 6 read-only scene, jobs, roles, freshness, targeted context, pressure, reorient, CRRC, coordinator, reorient launch/result/accept, explicit modeling/checkpoint and verification/review role launch/result templates, MVP status, dogfood runner, coordinator runner, live specialist runner, and limited safe-boundary CRRC automation are landed. The coordinator endpoint and runner make fixed-lane modeling, verification, implementation continuation, and CRRC handoffs explicit through review-gated artifacts; native automation only compacts or launches the fixed reorient-worker at turn-complete boundaries when the coordinator says it can auto-run. Implementation remains main-agent work; CRRC remains workflow automation, not a specialist persona. Persistent state remains distilled; evidence is a belief ledger, not an activity feed. + next_action: Dogfood the fixed-lane coordinator and native CRRC safe-boundary automation on real local work through `tools/epiphany_mvp_coordinator.py`, `tools/epiphany_mvp_status.py`, and their artifact bundles. Fix concrete operator-loop blockers only; do not build a broad ambient dispatcher, GUI-as-source-of-truth, arbitrary specialist marketplace, alternate job backend, or automatic semantic acceptance for the MVP. diff --git a/vendor/codex/codex-rs/app-server/src/bespoke_event_handling.rs b/vendor/codex/codex-rs/app-server/src/bespoke_event_handling.rs index eea391b8..16c1c6ea 100644 --- a/vendor/codex/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/vendor/codex/codex-rs/app-server/src/bespoke_event_handling.rs @@ -1,8 +1,10 @@ use crate::codex_message_processor::ApiVersion; +use crate::codex_message_processor::maybe_run_epiphany_coordinator_automation_for_turn_boundary; 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_agent_job_progress; +use crate::epiphany_invalidation::EpiphanyInvalidationManager; use crate::error_code::INTERNAL_ERROR_CODE; use crate::error_code::INVALID_REQUEST_ERROR_CODE; use crate::outgoing_message::ClientRequestResult; @@ -187,6 +189,7 @@ pub(crate) async fn apply_bespoke_event_handling( outgoing: ThreadScopedOutgoingMessageSender, thread_state: Arc>, thread_watch_manager: ThreadWatchManager, + epiphany_invalidation_manager: EpiphanyInvalidationManager, api_version: ApiVersion, fallback_model_provider: String, codex_home: &Path, @@ -244,6 +247,15 @@ pub(crate) async fn apply_bespoke_event_handling( &thread_state, ) .await; + if !turn_failed { + maybe_run_epiphany_coordinator_automation_for_turn_boundary( + conversation_id, + conversation, + epiphany_invalidation_manager, + &outgoing, + ) + .await; + } } EventMsg::SkillsUpdateAvailable => { if let ApiVersion::V2 = api_version { @@ -3289,6 +3301,7 @@ mod tests { outgoing: ThreadScopedOutgoingMessageSender, thread_state: Arc>, thread_watch_manager: ThreadWatchManager, + epiphany_invalidation_manager: EpiphanyInvalidationManager, analytics_events_client: AnalyticsEventsClient, codex_home: PathBuf, } @@ -3308,6 +3321,7 @@ mod tests { self.outgoing.clone(), self.thread_state.clone(), self.thread_watch_manager.clone(), + self.epiphany_invalidation_manager.clone(), ApiVersion::V2, "test-provider".to_string(), &self.codex_home, @@ -3629,6 +3643,7 @@ mod tests { outgoing: outgoing.clone(), thread_state: thread_state.clone(), thread_watch_manager: thread_watch_manager.clone(), + epiphany_invalidation_manager: EpiphanyInvalidationManager::new(), analytics_events_client: AnalyticsEventsClient::new( AuthManager::from_auth_for_testing( CodexAuth::create_dummy_chatgpt_auth_for_testing(), @@ -4672,6 +4687,7 @@ mod tests { let thread_state = new_thread_state(); let thread_watch_manager = ThreadWatchManager::new(); + let epiphany_invalidation_manager = EpiphanyInvalidationManager::new(); let (tx, mut rx) = mpsc::channel(CHANNEL_CAPACITY); let outgoing = Arc::new(OutgoingMessageSender::new(tx)); let outgoing = ThreadScopedOutgoingMessageSender::new( @@ -4706,6 +4722,7 @@ mod tests { outgoing.clone(), thread_state.clone(), thread_watch_manager.clone(), + epiphany_invalidation_manager.clone(), ApiVersion::V2, "test-provider".to_string(), codex_home.path(), @@ -4762,6 +4779,7 @@ mod tests { outgoing, thread_state, thread_watch_manager, + epiphany_invalidation_manager, ApiVersion::V2, "test-provider".to_string(), codex_home.path(), diff --git a/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs b/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs index 91779227..dbd0cdcf 100644 --- a/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs +++ b/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs @@ -10632,6 +10632,7 @@ impl CodexMessageProcessor { thread_outgoing, thread_state.clone(), thread_watch_manager.clone(), + epiphany_invalidation_manager.clone(), api_version, fallback_model_provider.clone(), codex_home.as_path(), @@ -13510,6 +13511,40 @@ struct EpiphanyCoordinatorDecision { reason: String, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum EpiphanyCoordinatorAutomationAction { + None, + CompactRehydrateReorient, + LaunchReorientWorker, +} + +fn map_epiphany_coordinator_automation_action( + decision: &EpiphanyCoordinatorDecision, +) -> EpiphanyCoordinatorAutomationAction { + if !decision.can_auto_run { + return EpiphanyCoordinatorAutomationAction::None; + } + match decision.action { + ThreadEpiphanyCoordinatorAction::CompactRehydrateReorient => { + EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient + } + ThreadEpiphanyCoordinatorAction::LaunchReorientWorker => { + EpiphanyCoordinatorAutomationAction::LaunchReorientWorker + } + ThreadEpiphanyCoordinatorAction::PrepareCheckpoint + | ThreadEpiphanyCoordinatorAction::WaitForReorientWorker + | ThreadEpiphanyCoordinatorAction::ReviewReorientResult + | ThreadEpiphanyCoordinatorAction::RegatherManually + | ThreadEpiphanyCoordinatorAction::LaunchModeling + | ThreadEpiphanyCoordinatorAction::ReviewModelingResult + | ThreadEpiphanyCoordinatorAction::LaunchVerification + | ThreadEpiphanyCoordinatorAction::ReviewVerificationResult + | ThreadEpiphanyCoordinatorAction::ContinueImplementation => { + EpiphanyCoordinatorAutomationAction::None + } + } +} + fn map_epiphany_coordinator( state_status: ThreadEpiphanyReorientStateStatus, checkpoint_present: bool, @@ -15191,6 +15226,154 @@ pub(crate) async fn thread_epiphany_jobs_updated_notification_for_agent_job_prog }) } +pub(crate) async fn maybe_run_epiphany_coordinator_automation_for_turn_boundary( + thread_id: ThreadId, + thread: Arc, + epiphany_invalidation_manager: EpiphanyInvalidationManager, + outgoing: &ThreadScopedOutgoingMessageSender, +) { + let thread_id_text = thread_id.to_string(); + let Some(state) = thread.epiphany_state().await else { + return; + }; + + let retrieval_override = thread.epiphany_retrieval_state().await; + let config_snapshot = thread.config_snapshot().await; + epiphany_invalidation_manager + .ensure_thread_watch(&thread_id_text, &config_snapshot.cwd) + .await; + let watcher_snapshot = epiphany_invalidation_manager + .snapshot(&thread_id_text) + .await; + let token_usage_info = thread.token_usage_info().await; + let state_db_ctx = thread.epiphany_state_runtime().await; + + let (_state_revision, retrieval, graph, watcher) = map_epiphany_freshness( + Some(&state), + Some(&retrieval_override), + Some(&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 job_resolution = + load_epiphany_job_launcher_snapshots(state_db_ctx.as_ref(), Some(&state)).await; + let jobs = map_epiphany_jobs(Some(&state), Some(&retrieval_override), &job_resolution); + 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), + state_db_ctx.as_ref(), + EPIPHANY_REORIENT_LAUNCH_BINDING_ID, + ) + .await; + 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 + .as_ref() + .is_some_and(|finding| epiphany_reorient_finding_already_accepted(&state, finding)), + ); + let roles = map_epiphany_roles( + Some(&state), + &jobs, + &reorient_decision, + &pressure, + &crrc_recommendation, + reorient_result_status, + reorient_job.as_ref(), + ); + let (modeling_result_status, _, _) = load_epiphany_role_result_snapshot( + &state, + state_db_ctx.as_ref(), + ThreadEpiphanyRoleId::Modeling, + EPIPHANY_MODELING_ROLE_BINDING_ID, + ) + .await; + let (verification_result_status, _, _) = load_epiphany_role_result_snapshot( + &state, + state_db_ctx.as_ref(), + ThreadEpiphanyRoleId::Verification, + EPIPHANY_VERIFICATION_ROLE_BINDING_ID, + ) + .await; + let source_signals = ThreadEpiphanyCoordinatorSignals { + pressure_level: pressure.level, + should_prepare_compaction: pressure.should_prepare_compaction, + reorient_action: reorient_decision.action, + crrc_action: crrc_recommendation.action, + modeling_result_status, + verification_result_status, + reorient_result_status, + }; + let coordinator = map_epiphany_coordinator( + state_status, + state.investigation_checkpoint.as_ref().is_some(), + &pressure, + &crrc_recommendation, + &roles, + &source_signals, + ); + + match map_epiphany_coordinator_automation_action(&coordinator) { + EpiphanyCoordinatorAutomationAction::None => {} + EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient => { + if let Err(err) = thread.submit(Op::Compact).await { + warn!( + "failed to run Epiphany coordinator automatic compaction for {thread_id}: {err}" + ); + } + } + EpiphanyCoordinatorAutomationAction::LaunchReorientWorker => { + let Some(checkpoint) = state.investigation_checkpoint.as_ref() 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) => { + warn!( + "failed to launch Epiphany coordinator reorientation worker for {thread_id}: {err}" + ); + return; + } + }; + let epiphany_state = + client_visible_live_thread_epiphany_state(thread.as_ref(), launched.epiphany_state) + .await; + outgoing + .send_server_notification(ServerNotification::ThreadEpiphanyStateUpdated( + ThreadEpiphanyStateUpdatedNotification { + thread_id: thread_id_text, + source: ThreadEpiphanyStateUpdatedSource::JobLaunch, + revision: epiphany_state.revision, + changed_fields: vec![ThreadEpiphanyStateUpdatedField::JobBindings], + epiphany_state, + }, + )) + .await; + } + } +} + async fn load_epiphany_job_launcher_snapshots( state_db_ctx: Option<&StateDbHandle>, state: Option<&EpiphanyThreadState>, @@ -18294,6 +18477,10 @@ mod tests { ThreadEpiphanyCoordinatorAction::CompactRehydrateReorient ); assert!(decision.can_auto_run); + assert_eq!( + map_epiphany_coordinator_automation_action(&decision), + EpiphanyCoordinatorAutomationAction::CompactRehydrateReorient + ); } #[test] @@ -18330,6 +18517,10 @@ mod tests { Some(ThreadEpiphanyRoleId::Reorientation) ); assert!(decision.can_auto_run); + assert_eq!( + map_epiphany_coordinator_automation_action(&decision), + EpiphanyCoordinatorAutomationAction::LaunchReorientWorker + ); } #[test] @@ -18362,6 +18553,10 @@ mod tests { ThreadEpiphanyCoordinatorAction::ReviewReorientResult ); assert!(decision.requires_review); + assert_eq!( + map_epiphany_coordinator_automation_action(&decision), + EpiphanyCoordinatorAutomationAction::None + ); } #[test] @@ -18392,6 +18587,10 @@ mod tests { launch_modeling.action, ThreadEpiphanyCoordinatorAction::LaunchModeling ); + assert_eq!( + map_epiphany_coordinator_automation_action(&launch_modeling), + EpiphanyCoordinatorAutomationAction::None + ); let modeling_done = coordinator_signals( ThreadEpiphanyCrrcAction::Continue, @@ -18411,6 +18610,10 @@ mod tests { launch_verification.action, ThreadEpiphanyCoordinatorAction::LaunchVerification ); + assert_eq!( + map_epiphany_coordinator_automation_action(&launch_verification), + EpiphanyCoordinatorAutomationAction::None + ); let verification_done = coordinator_signals( ThreadEpiphanyCrrcAction::Continue,