Add native coordinator interruption

This commit is contained in:
Meta Vens 2026-07-12 00:46:53 +01:00
commit a6965012a6
8 changed files with 236 additions and 9 deletions

View file

@ -195,7 +195,10 @@ impl Args {
fn run_status(args: &Args) -> Result<Value> {
if args.interrupt_binding.is_some() {
return run_codex_interrupt(args);
return match args.source {
StatusSource::Native => run_native_interrupt(args),
StatusSource::Codex => run_codex_interrupt(args),
};
}
match args.source {
StatusSource::Native => run_native_status(args),
@ -203,6 +206,46 @@ fn run_status(args: &Args) -> Result<Value> {
}
}
fn run_native_interrupt(args: &Args) -> Result<Value> {
let thread_id = args
.thread_id
.as_ref()
.ok_or_else(|| anyhow!("--interrupt-binding requires --thread-id"))?;
let binding_id = args
.interrupt_binding
.as_ref()
.ok_or_else(|| anyhow!("--interrupt-binding requires a binding id"))?;
let store = absolute_path(&args.thread_state_store)?;
let runtime_store = absolute_path(&args.runtime_store)?;
if store != runtime_store {
return Err(anyhow!(
"native interruption requires one unified thread/runtime store"
));
}
let service = epiphany_core::EpiphanyCoordinatorService::new(&store, &runtime_store);
let state = service
.state()?
.ok_or_else(|| anyhow!("cannot interrupt without native coordinator state"))?;
let result = service.interrupt_job(
thread_id,
&state,
epiphany_core::EpiphanyJobInterruptRequest {
expected_revision: Some(state.revision),
binding_id: binding_id.clone(),
reason: args.interrupt_reason.clone(),
},
)?;
Ok(json!({
"source": "native",
"threadId": thread_id,
"bindingId": result.binding_id,
"revision": result.epiphany_state.revision,
"cancelRequested": result.cancel_requested,
"interruptedThreadIds": result.interrupted_thread_ids,
"epiphanyState": result.epiphany_state,
}))
}
fn run_codex_interrupt(args: &Args) -> Result<Value> {
if args.source != StatusSource::Codex {
return Err(anyhow!(
@ -1865,6 +1908,61 @@ fn home_dir() -> PathBuf {
.unwrap_or_else(|| PathBuf::from("."))
}
#[cfg(test)]
mod native_interrupt_tests {
use super::*;
use epiphany_state_model::{EpiphanyJobBinding, EpiphanyJobKind};
#[test]
fn default_status_interrupt_uses_native_coordinator_state() -> Result<()> {
let temp = tempfile::tempdir()?;
let store = temp.path().join("status-interrupt.cc");
let service = epiphany_core::EpiphanyCoordinatorService::new(&store, &store);
service.apply_state_update(
"thread-1",
epiphany_core::EpiphanyStateUpdate {
expected_revision: Some(0),
job_bindings: Some(vec![EpiphanyJobBinding {
id: "modeling-worker".to_string(),
kind: EpiphanyJobKind::Specialist,
scope: "model".to_string(),
owner_role: "modeling".to_string(),
authority_scope: Some("model".to_string()),
linked_subgoal_ids: Vec::new(),
linked_graph_node_ids: Vec::new(),
blocking_reason: None,
}]),
..Default::default()
},
None,
)?;
let args = Args {
source: StatusSource::Native,
app_server: PathBuf::new(),
codex_home: PathBuf::new(),
thread_id: Some("thread-1".to_string()),
cwd: temp.path().to_path_buf(),
thread_state_store: store.clone(),
runtime_store: store,
ephemeral: true,
json: true,
result: None,
transcript: PathBuf::new(),
stderr: PathBuf::new(),
interrupt_binding: Some("modeling-worker".to_string()),
interrupt_reason: Some("native status proof".to_string()),
};
let result = run_status(&args)?;
assert_eq!(result["source"], "native");
assert_eq!(result["cancelRequested"], false);
assert_eq!(
result["epiphanyState"]["job_bindings"][0]["blocking_reason"],
"native status proof"
);
Ok(())
}
}
pub fn absolute_path(path: &Path) -> Result<PathBuf> {
if path.is_absolute() {
Ok(path.to_path_buf())

View file

@ -0,0 +1,108 @@
use crate::*;
use anyhow::{Result, anyhow};
use epiphany_state_model::EpiphanyThreadState;
use std::path::Path;
pub fn interrupt_coordinator_job(
store: &Path,
thread_id: &str,
state: &EpiphanyThreadState,
request: EpiphanyJobInterruptRequest,
) -> Result<EpiphanyJobInterruptResult> {
if request.binding_id.trim().is_empty() {
return Err(anyhow!(
"epiphany job interrupt binding_id must be non-empty"
));
}
if let Some(expected) = request.expected_revision
&& expected != state.revision
{
return Err(anyhow!(
"epiphany state revision mismatch: expected {expected}, found {}",
state.revision
));
}
let binding_index = state
.job_bindings
.iter()
.position(|binding| binding.id == request.binding_id)
.ok_or_else(|| {
anyhow!(
"epiphany job binding {:?} was not found",
request.binding_id
)
})?;
let reason = request
.reason
.as_deref()
.map(str::trim)
.filter(|reason| !reason.is_empty())
.unwrap_or(
"No active heartbeat turn is currently bound; launch explicitly to resume specialist work.",
);
let applied = apply_coordinator_state_update(
store,
thread_id,
EpiphanyStateUpdate {
expected_revision: request.expected_revision,
job_bindings: Some(clear_epiphany_job_binding_backend(
state.job_bindings.clone(),
binding_index,
reason,
)),
..Default::default()
},
None,
)?;
Ok(EpiphanyJobInterruptResult {
epiphany_state: applied.state,
binding_id: request.binding_id,
cancel_requested: false,
interrupted_thread_ids: Vec::new(),
})
}
#[cfg(test)]
mod tests {
use super::*;
use epiphany_state_model::EpiphanyJobBinding;
#[test]
fn native_interrupt_updates_the_canonical_binding_once() -> Result<()> {
let temp = tempfile::tempdir()?;
let store = temp.path().join("interrupt.cc");
let state = EpiphanyThreadState {
job_bindings: vec![EpiphanyJobBinding {
id: "modeling-worker".to_string(),
kind: epiphany_state_model::EpiphanyJobKind::Specialist,
scope: "model".to_string(),
owner_role: "modeling".to_string(),
authority_scope: Some("model".to_string()),
linked_subgoal_ids: Vec::new(),
linked_graph_node_ids: Vec::new(),
blocking_reason: None,
}],
..Default::default()
};
let result = interrupt_coordinator_job(
&store,
"thread-1",
&state,
EpiphanyJobInterruptRequest {
expected_revision: Some(0),
binding_id: "modeling-worker".to_string(),
reason: Some("operator interrupted".to_string()),
},
)?;
assert_eq!(result.epiphany_state.revision, 1);
assert_eq!(
result.epiphany_state.job_bindings[0]
.blocking_reason
.as_deref(),
Some("operator interrupted")
);
assert!(!result.cancel_requested);
assert_eq!(read_coordinator_state(&store)?, Some(result.epiphany_state));
Ok(())
}
}

View file

@ -1,6 +1,8 @@
use crate::EpiphanyCoordinatorReorientResultSnapshot;
use crate::EpiphanyCoordinatorRoleResultSnapshot;
use crate::EpiphanyCoordinatorStateApplied;
use crate::EpiphanyJobInterruptRequest;
use crate::EpiphanyJobInterruptResult;
use crate::EpiphanyJobLaunchRequest;
use crate::EpiphanyJobLaunchResult;
use crate::EpiphanyNativeReorientAcceptance;
@ -11,6 +13,7 @@ use crate::accept_coordinator_reorient_finding;
use crate::accept_coordinator_role_finding;
use crate::apply_coordinator_state_update;
use crate::commit_coordinator_job_launch;
use crate::interrupt_coordinator_job;
use crate::plan_coordinator_job_launch;
use crate::read_coordinator_state;
use crate::read_reorient_result_snapshot;
@ -161,6 +164,15 @@ impl EpiphanyCoordinatorService {
update_investigation_checkpoint,
)
}
pub fn interrupt_job(
&self,
thread_id: &str,
state: &EpiphanyThreadState,
request: EpiphanyJobInterruptRequest,
) -> Result<EpiphanyJobInterruptResult> {
interrupt_coordinator_job(&self.thread_state_store, thread_id, state, request)
}
}
#[cfg(test)]
@ -170,7 +182,7 @@ mod tests {
let source = include_str!("coordinator_service.rs");
let production = source.split("#[cfg(test)]").next().unwrap_or(source);
assert!(
production.lines().count() < 175,
production.lines().count() < 190,
"coordinator facade regrew into a host brain"
);
for forbidden in [

View file

@ -3,6 +3,7 @@ mod agent_memory;
mod agent_utterance_state;
mod continuity_gateway;
mod coordinator_acceptance;
mod coordinator_interrupt;
mod coordinator_launch;
mod coordinator_launch_context;
mod coordinator_results;
@ -146,6 +147,7 @@ pub use coordinator_acceptance::reorient_acceptance_claimed_effects;
pub use coordinator_acceptance::role_acceptance_claimed_effects;
pub use coordinator_acceptance::role_acceptance_enforceable_receipts;
pub use coordinator_acceptance::state_update_from_role_patch;
pub use coordinator_interrupt::interrupt_coordinator_job;
pub use coordinator_launch::EpiphanyCoordinatorJobLaunchPlan;
pub use coordinator_launch::commit_coordinator_job_launch;
pub use coordinator_launch::plan_coordinator_job_launch;