Add Epiphany MVP coordinator

This commit is contained in:
Meta Vens 2026-04-28 18:43:46 +01:00
commit b7f4a03ff8
16 changed files with 1773 additions and 31 deletions

View file

@ -15105,6 +15105,57 @@
],
"type": "string"
},
"ThreadEpiphanyCoordinatorAction": {
"enum": [
"prepareCheckpoint",
"compactRehydrateReorient",
"launchReorientWorker",
"waitForReorientWorker",
"reviewReorientResult",
"regatherManually",
"launchModeling",
"reviewModelingResult",
"launchVerification",
"reviewVerificationResult",
"continueImplementation"
],
"type": "string"
},
"ThreadEpiphanyCoordinatorSignals": {
"properties": {
"crrcAction": {
"$ref": "#/definitions/v2/ThreadEpiphanyCrrcAction"
},
"modelingResultStatus": {
"$ref": "#/definitions/v2/ThreadEpiphanyRoleResultStatus"
},
"pressureLevel": {
"$ref": "#/definitions/v2/ThreadEpiphanyPressureLevel"
},
"reorientAction": {
"$ref": "#/definitions/v2/ThreadEpiphanyReorientAction"
},
"reorientResultStatus": {
"$ref": "#/definitions/v2/ThreadEpiphanyReorientResultStatus"
},
"shouldPrepareCompaction": {
"type": "boolean"
},
"verificationResultStatus": {
"$ref": "#/definitions/v2/ThreadEpiphanyRoleResultStatus"
}
},
"required": [
"crrcAction",
"modelingResultStatus",
"pressureLevel",
"reorientAction",
"reorientResultStatus",
"shouldPrepareCompaction",
"verificationResultStatus"
],
"type": "object"
},
"ThreadEpiphanyCrrcAction": {
"enum": [
"continue",
@ -16260,6 +16311,7 @@
"context",
"jobs",
"roles",
"coordinator",
"roleLaunch",
"roleResult",
"jobLaunch",

View file

@ -12992,6 +12992,57 @@
],
"type": "string"
},
"ThreadEpiphanyCoordinatorAction": {
"enum": [
"prepareCheckpoint",
"compactRehydrateReorient",
"launchReorientWorker",
"waitForReorientWorker",
"reviewReorientResult",
"regatherManually",
"launchModeling",
"reviewModelingResult",
"launchVerification",
"reviewVerificationResult",
"continueImplementation"
],
"type": "string"
},
"ThreadEpiphanyCoordinatorSignals": {
"properties": {
"crrcAction": {
"$ref": "#/definitions/ThreadEpiphanyCrrcAction"
},
"modelingResultStatus": {
"$ref": "#/definitions/ThreadEpiphanyRoleResultStatus"
},
"pressureLevel": {
"$ref": "#/definitions/ThreadEpiphanyPressureLevel"
},
"reorientAction": {
"$ref": "#/definitions/ThreadEpiphanyReorientAction"
},
"reorientResultStatus": {
"$ref": "#/definitions/ThreadEpiphanyReorientResultStatus"
},
"shouldPrepareCompaction": {
"type": "boolean"
},
"verificationResultStatus": {
"$ref": "#/definitions/ThreadEpiphanyRoleResultStatus"
}
},
"required": [
"crrcAction",
"modelingResultStatus",
"pressureLevel",
"reorientAction",
"reorientResultStatus",
"shouldPrepareCompaction",
"verificationResultStatus"
],
"type": "object"
},
"ThreadEpiphanyCrrcAction": {
"enum": [
"continue",
@ -14147,6 +14198,7 @@
"context",
"jobs",
"roles",
"coordinator",
"roleLaunch",
"roleResult",
"jobLaunch",

View file

@ -0,0 +1,5 @@
// GENERATED CODE! DO NOT MODIFY BY HAND!
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type ThreadEpiphanyCoordinatorAction = "prepareCheckpoint" | "compactRehydrateReorient" | "launchReorientWorker" | "waitForReorientWorker" | "reviewReorientResult" | "regatherManually" | "launchModeling" | "reviewModelingResult" | "launchVerification" | "reviewVerificationResult" | "continueImplementation";

View file

@ -0,0 +1,10 @@
// GENERATED CODE! DO NOT MODIFY BY HAND!
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ThreadEpiphanyCrrcAction } from "./ThreadEpiphanyCrrcAction";
import type { ThreadEpiphanyPressureLevel } from "./ThreadEpiphanyPressureLevel";
import type { ThreadEpiphanyReorientAction } from "./ThreadEpiphanyReorientAction";
import type { ThreadEpiphanyReorientResultStatus } from "./ThreadEpiphanyReorientResultStatus";
import type { ThreadEpiphanyRoleResultStatus } from "./ThreadEpiphanyRoleResultStatus";
export type ThreadEpiphanyCoordinatorSignals = { pressureLevel: ThreadEpiphanyPressureLevel, shouldPrepareCompaction: boolean, reorientAction: ThreadEpiphanyReorientAction, crrcAction: ThreadEpiphanyCrrcAction, modelingResultStatus: ThreadEpiphanyRoleResultStatus, verificationResultStatus: ThreadEpiphanyRoleResultStatus, reorientResultStatus: ThreadEpiphanyReorientResultStatus, };

View file

@ -2,4 +2,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type ThreadEpiphanySceneAction = "index" | "retrieve" | "distill" | "context" | "jobs" | "roles" | "roleLaunch" | "roleResult" | "jobLaunch" | "jobInterrupt" | "freshness" | "pressure" | "reorient" | "crrc" | "reorientLaunch" | "reorientResult" | "reorientAccept" | "propose" | "promote" | "update";
export type ThreadEpiphanySceneAction = "index" | "retrieve" | "distill" | "context" | "jobs" | "roles" | "coordinator" | "roleLaunch" | "roleResult" | "jobLaunch" | "jobInterrupt" | "freshness" | "pressure" | "reorient" | "crrc" | "reorientLaunch" | "reorientResult" | "reorientAccept" | "propose" | "promote" | "update";

View file

@ -321,6 +321,8 @@ export type { ThreadEpiphanyContext } from "./ThreadEpiphanyContext";
export type { ThreadEpiphanyContextMissing } from "./ThreadEpiphanyContextMissing";
export type { ThreadEpiphanyContextSource } from "./ThreadEpiphanyContextSource";
export type { ThreadEpiphanyContextStateStatus } from "./ThreadEpiphanyContextStateStatus";
export type { ThreadEpiphanyCoordinatorAction } from "./ThreadEpiphanyCoordinatorAction";
export type { ThreadEpiphanyCoordinatorSignals } from "./ThreadEpiphanyCoordinatorSignals";
export type { ThreadEpiphanyCrrcAction } from "./ThreadEpiphanyCrrcAction";
export type { ThreadEpiphanyCrrcRecommendation } from "./ThreadEpiphanyCrrcRecommendation";
export type { ThreadEpiphanyFreshnessSource } from "./ThreadEpiphanyFreshnessSource";

View file

@ -351,6 +351,11 @@ client_request_definitions! {
params: v2::ThreadEpiphanyRolesParams,
response: v2::ThreadEpiphanyRolesResponse,
},
#[experimental("thread/epiphany/coordinator")]
ThreadEpiphanyCoordinator => "thread/epiphany/coordinator" {
params: v2::ThreadEpiphanyCoordinatorParams,
response: v2::ThreadEpiphanyCoordinatorResponse,
},
#[experimental("thread/epiphany/roleLaunch")]
ThreadEpiphanyRoleLaunch => "thread/epiphany/roleLaunch" {
params: v2::ThreadEpiphanyRoleLaunchParams,
@ -1742,6 +1747,7 @@ mod tests {
v2::ThreadEpiphanySceneAction::Context,
v2::ThreadEpiphanySceneAction::Jobs,
v2::ThreadEpiphanySceneAction::Roles,
v2::ThreadEpiphanySceneAction::Coordinator,
v2::ThreadEpiphanySceneAction::RoleLaunch,
v2::ThreadEpiphanySceneAction::RoleResult,
v2::ThreadEpiphanySceneAction::JobLaunch,
@ -1846,6 +1852,7 @@ mod tests {
"context",
"jobs",
"roles",
"coordinator",
"roleLaunch",
"roleResult",
"jobLaunch",
@ -2012,6 +2019,89 @@ mod tests {
Ok(())
}
#[test]
fn serialize_thread_epiphany_coordinator_response() -> Result<()> {
let response = ClientResponse::ThreadEpiphanyCoordinator {
request_id: RequestId::Integer(8),
response: v2::ThreadEpiphanyCoordinatorResponse {
thread_id: "thr_123".to_string(),
source: v2::ThreadEpiphanyRolesSource::Live,
state_status: v2::ThreadEpiphanyReorientStateStatus::Ready,
state_revision: Some(4),
action: v2::ThreadEpiphanyCoordinatorAction::LaunchModeling,
target_role: Some(v2::ThreadEpiphanyRoleId::Modeling),
recommended_scene_action: Some(v2::ThreadEpiphanySceneAction::RoleLaunch),
requires_review: false,
can_auto_run: true,
reason: "The modeling/checkpoint lane is ready.".to_string(),
source_signals: v2::ThreadEpiphanyCoordinatorSignals {
pressure_level: v2::ThreadEpiphanyPressureLevel::Low,
should_prepare_compaction: false,
reorient_action: v2::ThreadEpiphanyReorientAction::Resume,
crrc_action: v2::ThreadEpiphanyCrrcAction::Continue,
modeling_result_status: v2::ThreadEpiphanyRoleResultStatus::MissingBinding,
verification_result_status: v2::ThreadEpiphanyRoleResultStatus::MissingBinding,
reorient_result_status: v2::ThreadEpiphanyReorientResultStatus::MissingBinding,
},
roles: vec![v2::ThreadEpiphanyRoleLane {
id: v2::ThreadEpiphanyRoleId::Modeling,
title: "Modeling / Checkpoint".to_string(),
owner_role: "epiphany-modeler".to_string(),
status: v2::ThreadEpiphanyRoleStatus::Ready,
note: "Checkpoint ready.".to_string(),
jobs: Vec::new(),
authority_scopes: vec!["thread/epiphany/roleLaunch".to_string()],
recommended_action: Some(v2::ThreadEpiphanySceneAction::RoleLaunch),
}],
note: "Coordinator is read-only.".to_string(),
},
};
assert_eq!(response.id(), &RequestId::Integer(8));
assert_eq!(response.method(), "thread/epiphany/coordinator");
assert_eq!(
json!({
"method": "thread/epiphany/coordinator",
"id": 8,
"response": {
"threadId": "thr_123",
"source": "live",
"stateStatus": "ready",
"stateRevision": 4,
"action": "launchModeling",
"targetRole": "modeling",
"recommendedSceneAction": "roleLaunch",
"requiresReview": false,
"canAutoRun": true,
"reason": "The modeling/checkpoint lane is ready.",
"sourceSignals": {
"pressureLevel": "low",
"shouldPrepareCompaction": false,
"reorientAction": "resume",
"crrcAction": "continue",
"modelingResultStatus": "missingBinding",
"verificationResultStatus": "missingBinding",
"reorientResultStatus": "missingBinding"
},
"roles": [
{
"id": "modeling",
"title": "Modeling / Checkpoint",
"ownerRole": "epiphany-modeler",
"status": "ready",
"note": "Checkpoint ready.",
"authorityScopes": ["thread/epiphany/roleLaunch"],
"recommendedAction": "roleLaunch"
}
],
"note": "Coordinator is read-only."
}
}),
serde_json::to_value(&response)?,
);
Ok(())
}
#[test]
fn serialize_thread_epiphany_role_launch_response() -> Result<()> {
let response = ClientResponse::ThreadEpiphanyRoleLaunch {
@ -4211,6 +4301,18 @@ mod tests {
assert_eq!(reason, Some("thread/epiphany/roles"));
}
#[test]
fn thread_epiphany_coordinator_is_marked_experimental() {
let request = ClientRequest::ThreadEpiphanyCoordinator {
request_id: RequestId::Integer(1),
params: v2::ThreadEpiphanyCoordinatorParams {
thread_id: "thr_123".to_string(),
},
};
let reason = crate::experimental_api::ExperimentalApi::experimental_reason(&request);
assert_eq!(reason, Some("thread/epiphany/coordinator"));
}
#[test]
fn thread_epiphany_role_launch_is_marked_experimental() {
let request = ClientRequest::ThreadEpiphanyRoleLaunch {

View file

@ -3998,6 +3998,7 @@ pub enum ThreadEpiphanySceneAction {
Context,
Jobs,
Roles,
Coordinator,
RoleLaunch,
RoleResult,
JobLaunch,
@ -4303,6 +4304,68 @@ pub enum ThreadEpiphanyRoleResultStatus {
Cancelled,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ThreadEpiphanyCoordinatorParams {
pub thread_id: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ThreadEpiphanyCoordinatorResponse {
pub thread_id: String,
pub source: ThreadEpiphanyRolesSource,
pub state_status: ThreadEpiphanyReorientStateStatus,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional = nullable)]
pub state_revision: Option<u64>,
pub action: ThreadEpiphanyCoordinatorAction,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional = nullable)]
pub target_role: Option<ThreadEpiphanyRoleId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional = nullable)]
pub recommended_scene_action: Option<ThreadEpiphanySceneAction>,
pub requires_review: bool,
pub can_auto_run: bool,
pub reason: String,
pub source_signals: ThreadEpiphanyCoordinatorSignals,
pub roles: Vec<ThreadEpiphanyRoleLane>,
pub note: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub enum ThreadEpiphanyCoordinatorAction {
PrepareCheckpoint,
CompactRehydrateReorient,
LaunchReorientWorker,
WaitForReorientWorker,
ReviewReorientResult,
RegatherManually,
LaunchModeling,
ReviewModelingResult,
LaunchVerification,
ReviewVerificationResult,
ContinueImplementation,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ThreadEpiphanyCoordinatorSignals {
pub pressure_level: ThreadEpiphanyPressureLevel,
pub should_prepare_compaction: bool,
pub reorient_action: ThreadEpiphanyReorientAction,
pub crrc_action: ThreadEpiphanyCrrcAction,
pub modeling_result_status: ThreadEpiphanyRoleResultStatus,
pub verification_result_status: ThreadEpiphanyRoleResultStatus,
pub reorient_result_status: ThreadEpiphanyReorientResultStatus,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]

View file

@ -154,6 +154,10 @@ use codex_app_server_protocol::ThreadEpiphanyContextParams;
use codex_app_server_protocol::ThreadEpiphanyContextResponse;
use codex_app_server_protocol::ThreadEpiphanyContextSource;
use codex_app_server_protocol::ThreadEpiphanyContextStateStatus;
use codex_app_server_protocol::ThreadEpiphanyCoordinatorAction;
use codex_app_server_protocol::ThreadEpiphanyCoordinatorParams;
use codex_app_server_protocol::ThreadEpiphanyCoordinatorResponse;
use codex_app_server_protocol::ThreadEpiphanyCoordinatorSignals;
use codex_app_server_protocol::ThreadEpiphanyCrrcAction;
use codex_app_server_protocol::ThreadEpiphanyCrrcParams;
use codex_app_server_protocol::ThreadEpiphanyCrrcRecommendation;
@ -1102,6 +1106,10 @@ impl CodexMessageProcessor {
self.thread_epiphany_roles(to_connection_request_id(request_id), params)
.await;
}
ClientRequest::ThreadEpiphanyCoordinator { request_id, params } => {
self.thread_epiphany_coordinator(to_connection_request_id(request_id), params)
.await;
}
ClientRequest::ThreadEpiphanyRoleLaunch { request_id, params } => {
self.thread_epiphany_role_launch(to_connection_request_id(request_id), params)
.await;
@ -4468,6 +4476,216 @@ impl CodexMessageProcessor {
self.outgoing.send_response(request_id, response).await;
}
async fn thread_epiphany_coordinator(
&self,
request_id: ConnectionRequestId,
params: ThreadEpiphanyCoordinatorParams,
) {
let ThreadEpiphanyCoordinatorParams { thread_id } = params;
let thread_uuid = match ThreadId::from_string(&thread_id) {
Ok(id) => id,
Err(err) => {
self.send_invalid_request_error(request_id, format!("invalid thread id: {err}"))
.await;
return;
}
};
let loaded_thread = self.thread_manager.get_thread(thread_uuid).await.ok();
let loaded = loaded_thread.is_some();
let thread = match self.read_thread_view(thread_uuid, false).await {
Ok(thread) => thread,
Err(ThreadReadViewError::InvalidRequest(message)) => {
self.send_invalid_request_error(request_id, message).await;
return;
}
Err(ThreadReadViewError::Internal(message)) => {
self.send_internal_error(request_id, message).await;
return;
}
};
let retrieval_override = if let Some(loaded_thread) = loaded_thread.as_ref() {
Some(loaded_thread.epiphany_retrieval_state().await)
} else {
None
};
let watcher_snapshot = if let Some(loaded_thread) = loaded_thread.as_ref() {
let config_snapshot = loaded_thread.config_snapshot().await;
self.epiphany_invalidation_manager
.ensure_thread_watch(&thread_id, &config_snapshot.cwd)
.await;
Some(
self.epiphany_invalidation_manager
.snapshot(&thread_id)
.await,
)
} else {
None
};
let (source, token_usage_info) = if let Some(loaded_thread) = loaded_thread.as_ref() {
(
ThreadEpiphanyRolesSource::Live,
loaded_thread.token_usage_info().await,
)
} else {
let token_usage_info = match thread.path.as_deref() {
Some(path) => latest_token_usage_info_from_rollout_path(path).await,
None => None,
};
(ThreadEpiphanyRolesSource::Stored, token_usage_info)
};
let (state_revision, retrieval, graph, watcher) = map_epiphany_freshness(
thread.epiphany_state.as_ref(),
retrieval_override.as_ref(),
watcher_snapshot.as_ref(),
);
let pressure = map_epiphany_pressure(token_usage_info.as_ref());
let (state_status, decision) = map_epiphany_reorient(
thread.epiphany_state.as_ref(),
&pressure,
&retrieval,
&graph,
&watcher,
);
let mut state_db_ctx = if let Some(loaded_thread) = loaded_thread.as_ref() {
loaded_thread.epiphany_state_runtime().await
} else {
None
};
if state_db_ctx.is_none() {
state_db_ctx = open_state_db_for_direct_thread_lookup(&self.config).await;
}
let job_resolution = load_epiphany_job_launcher_snapshots(
state_db_ctx.as_ref(),
thread.epiphany_state.as_ref(),
)
.await;
let jobs = map_epiphany_jobs(
thread.epiphany_state.as_ref(),
retrieval_override.as_ref(),
&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(
thread.epiphany_state.as_ref(),
state_db_ctx.as_ref(),
EPIPHANY_REORIENT_LAUNCH_BINDING_ID,
)
.await;
let checkpoint_present = thread
.epiphany_state
.as_ref()
.and_then(|state| state.investigation_checkpoint.as_ref())
.is_some();
let recommendation = map_epiphany_crrc_recommendation(
loaded,
state_status,
&pressure,
&decision,
reorient_result_status,
checkpoint_present,
reorient_finding.is_some(),
reorient_finding.as_ref().is_some_and(|finding| {
thread
.epiphany_state
.as_ref()
.is_some_and(|state| epiphany_reorient_finding_already_accepted(state, finding))
}),
);
let roles = map_epiphany_roles(
thread.epiphany_state.as_ref(),
&jobs,
&decision,
&pressure,
&recommendation,
reorient_result_status,
reorient_job.as_ref(),
);
let (modeling_result_status, _, _) = if let Some(state) = thread.epiphany_state.as_ref() {
load_epiphany_role_result_snapshot(
state,
state_db_ctx.as_ref(),
ThreadEpiphanyRoleId::Modeling,
EPIPHANY_MODELING_ROLE_BINDING_ID,
)
.await
} else {
(
ThreadEpiphanyRoleResultStatus::MissingState,
None,
"No authoritative Epiphany state exists for this thread.".to_string(),
)
};
let (verification_result_status, _, _) = if let Some(state) = thread.epiphany_state.as_ref()
{
load_epiphany_role_result_snapshot(
state,
state_db_ctx.as_ref(),
ThreadEpiphanyRoleId::Verification,
EPIPHANY_VERIFICATION_ROLE_BINDING_ID,
)
.await
} else {
(
ThreadEpiphanyRoleResultStatus::MissingState,
None,
"No authoritative Epiphany state exists for this thread.".to_string(),
)
};
let source_signals = ThreadEpiphanyCoordinatorSignals {
pressure_level: pressure.level,
should_prepare_compaction: pressure.should_prepare_compaction,
reorient_action: decision.action,
crrc_action: recommendation.action,
modeling_result_status,
verification_result_status,
reorient_result_status,
};
let coordinator = map_epiphany_coordinator(
state_status,
checkpoint_present,
&pressure,
&recommendation,
&roles,
&source_signals,
);
let note = format!(
"Coordinator is read-only. It recommends {:?} from CRRC {:?}, pressure {:?}, modeling {:?}, verification {:?}, and reorient {:?}.",
coordinator.action,
recommendation.action,
pressure.level,
modeling_result_status,
verification_result_status,
reorient_result_status,
);
let response = ThreadEpiphanyCoordinatorResponse {
thread_id,
source,
state_status,
state_revision,
action: coordinator.action,
target_role: coordinator.target_role,
recommended_scene_action: coordinator.recommended_scene_action,
requires_review: coordinator.requires_review,
can_auto_run: coordinator.can_auto_run,
reason: coordinator.reason,
source_signals,
roles,
note,
};
self.outgoing.send_response(request_id, response).await;
}
async fn thread_epiphany_role_launch(
&self,
request_id: ConnectionRequestId,
@ -12488,6 +12706,7 @@ fn epiphany_scene_available_actions(
ThreadEpiphanySceneAction::Context,
ThreadEpiphanySceneAction::Jobs,
ThreadEpiphanySceneAction::Roles,
ThreadEpiphanySceneAction::Coordinator,
ThreadEpiphanySceneAction::RoleLaunch,
ThreadEpiphanySceneAction::RoleResult,
ThreadEpiphanySceneAction::JobLaunch,
@ -13282,6 +13501,221 @@ fn map_epiphany_crrc_recommendation(
)
}
struct EpiphanyCoordinatorDecision {
action: ThreadEpiphanyCoordinatorAction,
target_role: Option<ThreadEpiphanyRoleId>,
recommended_scene_action: Option<ThreadEpiphanySceneAction>,
requires_review: bool,
can_auto_run: bool,
reason: String,
}
fn map_epiphany_coordinator(
state_status: ThreadEpiphanyReorientStateStatus,
checkpoint_present: bool,
pressure: &ThreadEpiphanyPressure,
recommendation: &ThreadEpiphanyCrrcRecommendation,
roles: &[ThreadEpiphanyRoleLane],
signals: &ThreadEpiphanyCoordinatorSignals,
) -> EpiphanyCoordinatorDecision {
let build = |action,
target_role,
recommended_scene_action,
requires_review,
can_auto_run,
reason: &str| EpiphanyCoordinatorDecision {
action,
target_role,
recommended_scene_action,
requires_review,
can_auto_run,
reason: reason.to_string(),
};
if state_status == ThreadEpiphanyReorientStateStatus::Missing || !checkpoint_present {
return build(
ThreadEpiphanyCoordinatorAction::PrepareCheckpoint,
Some(ThreadEpiphanyRoleId::Modeling),
Some(ThreadEpiphanySceneAction::Update),
false,
false,
"Authoritative state or investigation checkpoint is missing; prepare a checkpoint before coordination can continue.",
);
}
match recommendation.action {
ThreadEpiphanyCrrcAction::WaitForReorientWorker => {
return build(
ThreadEpiphanyCoordinatorAction::WaitForReorientWorker,
Some(ThreadEpiphanyRoleId::Reorientation),
Some(ThreadEpiphanySceneAction::ReorientResult),
false,
false,
"A CRRC reorient worker is already in flight; wait for the bounded result.",
);
}
ThreadEpiphanyCrrcAction::AcceptReorientResult
| ThreadEpiphanyCrrcAction::ReviewReorientResult => {
return build(
ThreadEpiphanyCoordinatorAction::ReviewReorientResult,
Some(ThreadEpiphanyRoleId::Reorientation),
recommendation.recommended_scene_action,
true,
false,
"A CRRC reorientation finding needs human review before continuation.",
);
}
ThreadEpiphanyCrrcAction::RegatherManually => {
return build(
ThreadEpiphanyCoordinatorAction::RegatherManually,
Some(ThreadEpiphanyRoleId::Reorientation),
recommendation.recommended_scene_action,
true,
false,
"CRRC cannot safely continue automatically; manual regathering is required.",
);
}
ThreadEpiphanyCrrcAction::PrepareCheckpoint
| ThreadEpiphanyCrrcAction::LaunchReorientWorker
| ThreadEpiphanyCrrcAction::Continue => {}
}
if pressure.should_prepare_compaction {
return build(
ThreadEpiphanyCoordinatorAction::CompactRehydrateReorient,
Some(ThreadEpiphanyRoleId::Reorientation),
Some(ThreadEpiphanySceneAction::Reorient),
false,
true,
"Context pressure crossed the preparation threshold; compact, rehydrate, and reorient before more implementation work.",
);
}
if recommendation.action == ThreadEpiphanyCrrcAction::LaunchReorientWorker {
return build(
ThreadEpiphanyCoordinatorAction::LaunchReorientWorker,
Some(ThreadEpiphanyRoleId::Reorientation),
Some(ThreadEpiphanySceneAction::ReorientLaunch),
false,
true,
"CRRC says continuity needs a bounded reorient worker before safe continuation.",
);
}
if signals.verification_result_status == ThreadEpiphanyRoleResultStatus::Completed {
return build(
ThreadEpiphanyCoordinatorAction::ReviewVerificationResult,
Some(ThreadEpiphanyRoleId::Verification),
Some(ThreadEpiphanySceneAction::RoleResult),
true,
false,
"A verification/review finding is complete and must be reviewed before continuation.",
);
}
if signals.modeling_result_status == ThreadEpiphanyRoleResultStatus::Completed {
return build(
ThreadEpiphanyCoordinatorAction::LaunchVerification,
Some(ThreadEpiphanyRoleId::Verification),
Some(ThreadEpiphanySceneAction::RoleLaunch),
false,
true,
"Modeling/checkpoint guidance is available; run verification before implementation continues.",
);
}
if role_status(roles, ThreadEpiphanyRoleId::Modeling).is_some_and(|status| {
matches!(
status,
ThreadEpiphanyRoleStatus::Ready | ThreadEpiphanyRoleStatus::Needed
)
}) && matches!(
signals.modeling_result_status,
ThreadEpiphanyRoleResultStatus::MissingBinding
| ThreadEpiphanyRoleResultStatus::BackendMissing
| ThreadEpiphanyRoleResultStatus::Cancelled
| ThreadEpiphanyRoleResultStatus::Failed
) {
return build(
ThreadEpiphanyCoordinatorAction::LaunchModeling,
Some(ThreadEpiphanyRoleId::Modeling),
Some(ThreadEpiphanySceneAction::RoleLaunch),
false,
true,
"The modeling/checkpoint lane is ready and no current modeling finding is available.",
);
}
if matches!(
signals.modeling_result_status,
ThreadEpiphanyRoleResultStatus::Pending | ThreadEpiphanyRoleResultStatus::Running
) {
return build(
ThreadEpiphanyCoordinatorAction::ReviewModelingResult,
Some(ThreadEpiphanyRoleId::Modeling),
Some(ThreadEpiphanySceneAction::RoleResult),
false,
false,
"A modeling/checkpoint specialist is already running; wait for its result.",
);
}
if role_status(roles, ThreadEpiphanyRoleId::Verification).is_some_and(|status| {
matches!(
status,
ThreadEpiphanyRoleStatus::Ready | ThreadEpiphanyRoleStatus::Needed
)
}) && matches!(
signals.verification_result_status,
ThreadEpiphanyRoleResultStatus::MissingBinding
| ThreadEpiphanyRoleResultStatus::BackendMissing
| ThreadEpiphanyRoleResultStatus::Cancelled
| ThreadEpiphanyRoleResultStatus::Failed
) {
return build(
ThreadEpiphanyCoordinatorAction::LaunchVerification,
Some(ThreadEpiphanyRoleId::Verification),
Some(ThreadEpiphanySceneAction::RoleLaunch),
false,
true,
"The verification/review lane is ready and no current verification finding is available.",
);
}
if matches!(
signals.verification_result_status,
ThreadEpiphanyRoleResultStatus::Pending | ThreadEpiphanyRoleResultStatus::Running
) {
return build(
ThreadEpiphanyCoordinatorAction::ReviewVerificationResult,
Some(ThreadEpiphanyRoleId::Verification),
Some(ThreadEpiphanySceneAction::RoleResult),
false,
false,
"A verification/review specialist is already running; wait for its result.",
);
}
build(
ThreadEpiphanyCoordinatorAction::ContinueImplementation,
Some(ThreadEpiphanyRoleId::Implementation),
None,
false,
false,
"CRRC is clear and no specialist lane is currently blocking implementation.",
)
}
fn role_status(
roles: &[ThreadEpiphanyRoleLane],
role_id: ThreadEpiphanyRoleId,
) -> Option<ThreadEpiphanyRoleStatus> {
roles
.iter()
.find(|role| role.id == role_id)
.map(|role| role.status)
}
fn epiphany_reorient_finding_already_accepted(
state: &EpiphanyThreadState,
finding: &ThreadEpiphanyReorientFinding,
@ -17736,6 +18170,288 @@ mod tests {
assert_eq!(roles[3].status, ThreadEpiphanyRoleStatus::Ready);
}
fn base_coordinator_roles() -> Vec<ThreadEpiphanyRoleLane> {
vec![
coordinator_role(
ThreadEpiphanyRoleId::Implementation,
ThreadEpiphanyRoleStatus::Ready,
),
coordinator_role(
ThreadEpiphanyRoleId::Modeling,
ThreadEpiphanyRoleStatus::Ready,
),
coordinator_role(
ThreadEpiphanyRoleId::Verification,
ThreadEpiphanyRoleStatus::Ready,
),
coordinator_role(
ThreadEpiphanyRoleId::Reorientation,
ThreadEpiphanyRoleStatus::Ready,
),
]
}
fn coordinator_role(
id: ThreadEpiphanyRoleId,
status: ThreadEpiphanyRoleStatus,
) -> ThreadEpiphanyRoleLane {
ThreadEpiphanyRoleLane {
id,
title: format!("{id:?}"),
owner_role: format!("{id:?}"),
status,
note: "test lane".to_string(),
jobs: Vec::new(),
authority_scopes: Vec::new(),
recommended_action: None,
}
}
fn coordinator_signals(
crrc_action: ThreadEpiphanyCrrcAction,
reorient_result_status: ThreadEpiphanyReorientResultStatus,
modeling_result_status: ThreadEpiphanyRoleResultStatus,
verification_result_status: ThreadEpiphanyRoleResultStatus,
) -> ThreadEpiphanyCoordinatorSignals {
ThreadEpiphanyCoordinatorSignals {
pressure_level: ThreadEpiphanyPressureLevel::Low,
should_prepare_compaction: false,
reorient_action: ThreadEpiphanyReorientAction::Resume,
crrc_action,
modeling_result_status,
verification_result_status,
reorient_result_status,
}
}
#[test]
fn map_epiphany_coordinator_prepares_missing_checkpoint() {
let pressure = map_epiphany_pressure(None);
let recommendation = ThreadEpiphanyCrrcRecommendation {
action: ThreadEpiphanyCrrcAction::PrepareCheckpoint,
recommended_scene_action: Some(ThreadEpiphanySceneAction::Update),
reason: "checkpoint missing".to_string(),
};
let roles = base_coordinator_roles();
let signals = coordinator_signals(
ThreadEpiphanyCrrcAction::PrepareCheckpoint,
ThreadEpiphanyReorientResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::MissingBinding,
);
let decision = map_epiphany_coordinator(
ThreadEpiphanyReorientStateStatus::Ready,
false,
&pressure,
&recommendation,
&roles,
&signals,
);
assert_eq!(
decision.action,
ThreadEpiphanyCoordinatorAction::PrepareCheckpoint
);
assert!(!decision.can_auto_run);
}
#[test]
fn map_epiphany_coordinator_compacts_at_pressure_threshold() {
let pressure = map_epiphany_pressure(Some(&CoreTokenUsageInfo {
total_token_usage: codex_protocol::protocol::TokenUsage {
total_tokens: 92,
..Default::default()
},
last_token_usage: codex_protocol::protocol::TokenUsage::default(),
model_context_window: Some(200),
model_auto_compact_token_limit: Some(100),
}));
let recommendation = ThreadEpiphanyCrrcRecommendation {
action: ThreadEpiphanyCrrcAction::Continue,
recommended_scene_action: Some(ThreadEpiphanySceneAction::Reorient),
reason: "continue".to_string(),
};
let roles = base_coordinator_roles();
let signals = coordinator_signals(
ThreadEpiphanyCrrcAction::Continue,
ThreadEpiphanyReorientResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::MissingBinding,
);
let decision = map_epiphany_coordinator(
ThreadEpiphanyReorientStateStatus::Ready,
true,
&pressure,
&recommendation,
&roles,
&signals,
);
assert_eq!(
decision.action,
ThreadEpiphanyCoordinatorAction::CompactRehydrateReorient
);
assert!(decision.can_auto_run);
}
#[test]
fn map_epiphany_coordinator_launches_reorient_worker_from_crrc() {
let pressure = map_epiphany_pressure(None);
let recommendation = ThreadEpiphanyCrrcRecommendation {
action: ThreadEpiphanyCrrcAction::LaunchReorientWorker,
recommended_scene_action: Some(ThreadEpiphanySceneAction::ReorientLaunch),
reason: "regather".to_string(),
};
let roles = base_coordinator_roles();
let signals = coordinator_signals(
ThreadEpiphanyCrrcAction::LaunchReorientWorker,
ThreadEpiphanyReorientResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::MissingBinding,
);
let decision = map_epiphany_coordinator(
ThreadEpiphanyReorientStateStatus::Ready,
true,
&pressure,
&recommendation,
&roles,
&signals,
);
assert_eq!(
decision.action,
ThreadEpiphanyCoordinatorAction::LaunchReorientWorker
);
assert_eq!(
decision.target_role,
Some(ThreadEpiphanyRoleId::Reorientation)
);
assert!(decision.can_auto_run);
}
#[test]
fn map_epiphany_coordinator_reviews_reorient_result() {
let pressure = map_epiphany_pressure(None);
let recommendation = ThreadEpiphanyCrrcRecommendation {
action: ThreadEpiphanyCrrcAction::AcceptReorientResult,
recommended_scene_action: Some(ThreadEpiphanySceneAction::ReorientAccept),
reason: "review".to_string(),
};
let roles = base_coordinator_roles();
let signals = coordinator_signals(
ThreadEpiphanyCrrcAction::AcceptReorientResult,
ThreadEpiphanyReorientResultStatus::Completed,
ThreadEpiphanyRoleResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::MissingBinding,
);
let decision = map_epiphany_coordinator(
ThreadEpiphanyReorientStateStatus::Ready,
true,
&pressure,
&recommendation,
&roles,
&signals,
);
assert_eq!(
decision.action,
ThreadEpiphanyCoordinatorAction::ReviewReorientResult
);
assert!(decision.requires_review);
}
#[test]
fn map_epiphany_coordinator_runs_modeling_then_verification_then_continue() {
let pressure = map_epiphany_pressure(None);
let recommendation = ThreadEpiphanyCrrcRecommendation {
action: ThreadEpiphanyCrrcAction::Continue,
recommended_scene_action: Some(ThreadEpiphanySceneAction::Reorient),
reason: "continue".to_string(),
};
let roles = base_coordinator_roles();
let missing = coordinator_signals(
ThreadEpiphanyCrrcAction::Continue,
ThreadEpiphanyReorientResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::MissingBinding,
);
let launch_modeling = map_epiphany_coordinator(
ThreadEpiphanyReorientStateStatus::Ready,
true,
&pressure,
&recommendation,
&roles,
&missing,
);
assert_eq!(
launch_modeling.action,
ThreadEpiphanyCoordinatorAction::LaunchModeling
);
let modeling_done = coordinator_signals(
ThreadEpiphanyCrrcAction::Continue,
ThreadEpiphanyReorientResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::Completed,
ThreadEpiphanyRoleResultStatus::MissingBinding,
);
let launch_verification = map_epiphany_coordinator(
ThreadEpiphanyReorientStateStatus::Ready,
true,
&pressure,
&recommendation,
&roles,
&modeling_done,
);
assert_eq!(
launch_verification.action,
ThreadEpiphanyCoordinatorAction::LaunchVerification
);
let verification_done = coordinator_signals(
ThreadEpiphanyCrrcAction::Continue,
ThreadEpiphanyReorientResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::Completed,
ThreadEpiphanyRoleResultStatus::Completed,
);
let review_verification = map_epiphany_coordinator(
ThreadEpiphanyReorientStateStatus::Ready,
true,
&pressure,
&recommendation,
&roles,
&verification_done,
);
assert_eq!(
review_verification.action,
ThreadEpiphanyCoordinatorAction::ReviewVerificationResult
);
assert!(review_verification.requires_review);
let reviewed = coordinator_signals(
ThreadEpiphanyCrrcAction::Continue,
ThreadEpiphanyReorientResultStatus::MissingBinding,
ThreadEpiphanyRoleResultStatus::BackendMissing,
ThreadEpiphanyRoleResultStatus::BackendMissing,
);
let continue_implementation = map_epiphany_coordinator(
ThreadEpiphanyReorientStateStatus::Ready,
true,
&pressure,
&recommendation,
&[],
&reviewed,
);
assert_eq!(
continue_implementation.action,
ThreadEpiphanyCoordinatorAction::ContinueImplementation
);
}
#[test]
fn build_epiphany_role_launch_request_uses_fixed_mvp_templates() {
let state = codex_protocol::protocol::EpiphanyThreadState {