continuity: own runtime session closure
This commit is contained in:
parent
f257e07686
commit
230f71f48a
7 changed files with 262 additions and 5 deletions
|
|
@ -5,12 +5,15 @@ use chrono::SecondsFormat;
|
|||
use epiphany_core::EpiphanyRuntimeEvent;
|
||||
use epiphany_core::EpiphanyRuntimeJob;
|
||||
use epiphany_core::EpiphanyRuntimeJobResult;
|
||||
use epiphany_core::EpiphanyRuntimeSession;
|
||||
use epiphany_core::RuntimeSpineEventOptions;
|
||||
use epiphany_core::RuntimeSpineInitOptions;
|
||||
use epiphany_core::RuntimeSpineJobOptions;
|
||||
use epiphany_core::RuntimeSpineJobResultOptions;
|
||||
use epiphany_core::RuntimeSpineSessionClosureOptions;
|
||||
use epiphany_core::RuntimeSpineSessionOptions;
|
||||
use epiphany_core::append_runtime_event;
|
||||
use epiphany_core::close_runtime_session;
|
||||
use epiphany_core::complete_runtime_job;
|
||||
use epiphany_core::create_runtime_job;
|
||||
use epiphany_core::create_runtime_session;
|
||||
|
|
@ -108,6 +111,21 @@ fn main() -> Result<()> {
|
|||
);
|
||||
}
|
||||
}
|
||||
Command::ListSessions => {
|
||||
let mut cache = runtime_spine_cache(&args.store)?;
|
||||
cache.pull_all_backing_stores()?;
|
||||
let mut sessions = cache.get_all::<EpiphanyRuntimeSession>()?;
|
||||
sessions.sort_by(|left, right| left.created_at.cmp(&right.created_at));
|
||||
println!("runtime sessions");
|
||||
for session in sessions {
|
||||
println!(
|
||||
"{}\t{:?}\t{}",
|
||||
session.session_id,
|
||||
session.status,
|
||||
session.objective.replace(['\r', '\n', '\t'], " ")
|
||||
);
|
||||
}
|
||||
}
|
||||
Command::ListModelRequests => {
|
||||
let mut cache = runtime_spine_cache(&args.store)?;
|
||||
cache.pull_all_backing_stores()?;
|
||||
|
|
@ -183,6 +201,18 @@ fn main() -> Result<()> {
|
|||
println!("session: {}", session.session_id);
|
||||
println!("objective: {}", session.objective);
|
||||
}
|
||||
Command::CloseSession { session_id, summary } => {
|
||||
let session = close_runtime_session(
|
||||
&args.store,
|
||||
RuntimeSpineSessionClosureOptions {
|
||||
session_id,
|
||||
completed_at: now(),
|
||||
summary,
|
||||
},
|
||||
)?;
|
||||
println!("runtime session completed");
|
||||
println!("session: {}", session.session_id);
|
||||
}
|
||||
Command::RecordEvent {
|
||||
event_id,
|
||||
event_type,
|
||||
|
|
@ -290,6 +320,7 @@ enum Command {
|
|||
},
|
||||
Status,
|
||||
ListJobs,
|
||||
ListSessions,
|
||||
ListModelRequests,
|
||||
ListEvents,
|
||||
OpenSession {
|
||||
|
|
@ -297,6 +328,10 @@ enum Command {
|
|||
objective: String,
|
||||
coordinator_note: String,
|
||||
},
|
||||
CloseSession {
|
||||
session_id: String,
|
||||
summary: String,
|
||||
},
|
||||
RecordEvent {
|
||||
event_id: String,
|
||||
event_type: String,
|
||||
|
|
@ -373,6 +408,7 @@ fn parse_command(mut args: Vec<String>) -> Result<Command> {
|
|||
}
|
||||
"status" => Ok(Command::Status),
|
||||
"list-jobs" => Ok(Command::ListJobs),
|
||||
"list-sessions" => Ok(Command::ListSessions),
|
||||
"list-model-requests" => Ok(Command::ListModelRequests),
|
||||
"list-events" => Ok(Command::ListEvents),
|
||||
"open-session" => {
|
||||
|
|
@ -492,6 +528,28 @@ fn parse_command(mut args: Vec<String>) -> Result<Command> {
|
|||
artifact_refs,
|
||||
})
|
||||
}
|
||||
"close-session" => {
|
||||
let mut session_id = String::new();
|
||||
let mut summary = String::new();
|
||||
parse_options(args, |name, value| match name {
|
||||
"--session-id" => {
|
||||
session_id = value;
|
||||
Ok(())
|
||||
}
|
||||
"--summary" => {
|
||||
summary = value;
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(anyhow!("unknown close-session argument: {name}")),
|
||||
})?;
|
||||
if session_id.trim().is_empty() {
|
||||
return Err(anyhow!("close-session requires --session-id"));
|
||||
}
|
||||
if summary.trim().is_empty() {
|
||||
return Err(anyhow!("close-session requires --summary"));
|
||||
}
|
||||
Ok(Command::CloseSession { session_id, summary })
|
||||
}
|
||||
"complete-job" => {
|
||||
let mut result_id = format!("result-{}", Uuid::new_v4());
|
||||
let mut job_id = String::new();
|
||||
|
|
@ -620,7 +678,7 @@ fn now() -> String {
|
|||
}
|
||||
|
||||
fn usage() -> &'static str {
|
||||
"usage: epiphany-runtime-spine [--store path] <init|status|list-jobs|list-model-requests|list-events|open-session|open-job|complete-job|record-event|hello-frame|schema-catalog>"
|
||||
"usage: epiphany-runtime-spine [--store path] <init|status|list-jobs|list-sessions|list-model-requests|list-events|open-session|close-session|open-job|complete-job|record-event|hello-frame|schema-catalog>"
|
||||
}
|
||||
|
||||
fn model_input_item_chars(item: &EpiphanyModelInputItem) -> usize {
|
||||
|
|
|
|||
|
|
@ -1006,6 +1006,7 @@ pub use runtime_spine::RuntimeSpineHeartbeatLaunchPlanOptions;
|
|||
pub use runtime_spine::RuntimeSpineInitOptions;
|
||||
pub use runtime_spine::RuntimeSpineJobOptions;
|
||||
pub use runtime_spine::RuntimeSpineJobResultOptions;
|
||||
pub use runtime_spine::RuntimeSpineSessionClosureOptions;
|
||||
pub use runtime_spine::RuntimeSpineSessionOptions;
|
||||
pub use runtime_spine::amend_repo_frontier_execution;
|
||||
pub use runtime_spine::append_runtime_event;
|
||||
|
|
@ -1013,6 +1014,7 @@ pub use runtime_spine::bind_runtime_repository_domain;
|
|||
pub use runtime_spine::bind_runtime_to_agent_memory_swarm;
|
||||
pub use runtime_spine::canonical_repo_frontier_plan_candidate_id;
|
||||
pub use runtime_spine::clear_epiphany_job_binding_backend;
|
||||
pub use runtime_spine::close_runtime_session;
|
||||
pub use runtime_spine::commit_operator_repo_frontier_plan_review;
|
||||
pub use runtime_spine::commit_repo_frontier_modeling_request;
|
||||
pub use runtime_spine::commit_repo_frontier_plan_decision;
|
||||
|
|
|
|||
|
|
@ -758,6 +758,13 @@ pub struct RuntimeSpineSessionOptions {
|
|||
pub coordinator_note: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct RuntimeSpineSessionClosureOptions {
|
||||
pub session_id: String,
|
||||
pub completed_at: String,
|
||||
pub summary: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct RuntimeSpineEventOptions {
|
||||
pub event_id: String,
|
||||
|
|
@ -1231,6 +1238,75 @@ pub fn ensure_runtime_session(
|
|||
Ok(session)
|
||||
}
|
||||
|
||||
pub fn close_runtime_session(
|
||||
store_path: impl AsRef<Path>,
|
||||
options: RuntimeSpineSessionClosureOptions,
|
||||
) -> Result<EpiphanyRuntimeSession> {
|
||||
validate_non_empty(&options.session_id, "session id")?;
|
||||
validate_non_empty(&options.completed_at, "session completion time")?;
|
||||
validate_non_empty(&options.summary, "session completion summary")?;
|
||||
let mut cache = runtime_spine_cache(store_path.as_ref())?;
|
||||
cache.pull_all_backing_stores()?;
|
||||
require_identity(&cache)?;
|
||||
let mut session = cache
|
||||
.get::<EpiphanyRuntimeSession>(&options.session_id)?
|
||||
.ok_or_else(|| anyhow!("runtime session {:?} does not exist", options.session_id))?;
|
||||
if session.status == EpiphanyRuntimeSessionStatus::Completed {
|
||||
return Ok(session);
|
||||
}
|
||||
if session.status == EpiphanyRuntimeSessionStatus::Archived {
|
||||
return Err(anyhow!(
|
||||
"runtime session {:?} is archived and cannot be completed",
|
||||
options.session_id
|
||||
));
|
||||
}
|
||||
let open_job_ids = cache
|
||||
.get_all::<EpiphanyRuntimeJob>()?
|
||||
.into_iter()
|
||||
.filter(|job| {
|
||||
job.session_id == options.session_id
|
||||
&& matches!(
|
||||
job.status,
|
||||
EpiphanyRuntimeJobStatus::Queued
|
||||
| EpiphanyRuntimeJobStatus::Running
|
||||
| EpiphanyRuntimeJobStatus::WaitingForReview
|
||||
)
|
||||
})
|
||||
.map(|job| job.job_id)
|
||||
.collect::<Vec<_>>();
|
||||
if !open_job_ids.is_empty() {
|
||||
return Err(anyhow!(
|
||||
"runtime session {:?} has open jobs: {}",
|
||||
options.session_id,
|
||||
open_job_ids.join(", ")
|
||||
));
|
||||
}
|
||||
let event_id = format!("event-session-completed-{}", options.session_id);
|
||||
if cache.get::<EpiphanyRuntimeEvent>(&event_id)?.is_some() {
|
||||
return Err(anyhow!(
|
||||
"runtime session {:?} has a completion event but is not completed",
|
||||
options.session_id
|
||||
));
|
||||
}
|
||||
session.status = EpiphanyRuntimeSessionStatus::Completed;
|
||||
session.updated_at = options.completed_at.clone();
|
||||
session.coordinator_note = options.summary.clone();
|
||||
let event = EpiphanyRuntimeEvent {
|
||||
schema_version: RUNTIME_SPINE_SCHEMA_VERSION.to_string(),
|
||||
event_id,
|
||||
occurred_at: options.completed_at,
|
||||
event_type: "session.completed".to_string(),
|
||||
source: "continuity".to_string(),
|
||||
session_id: Some(options.session_id),
|
||||
job_id: None,
|
||||
summary: options.summary,
|
||||
metadata: BTreeMap::new(),
|
||||
};
|
||||
cache.put(&session.session_id, &session)?;
|
||||
cache.put(&event.event_id, &event)?;
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
pub fn create_runtime_job(
|
||||
store_path: impl AsRef<Path>,
|
||||
options: RuntimeSpineJobOptions,
|
||||
|
|
@ -13671,6 +13747,87 @@ pub(crate) mod tests {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_session_closure_requires_terminal_jobs_and_replays_exactly() -> Result<()> {
|
||||
let temp = tempdir()?;
|
||||
let store = temp.path().join("runtime.msgpack");
|
||||
initialize_runtime_spine(
|
||||
&store,
|
||||
RuntimeSpineInitOptions {
|
||||
runtime_id: "epiphany-test".to_string(),
|
||||
display_name: "Epiphany Test".to_string(),
|
||||
created_at: "2026-08-08T20:00:00Z".to_string(),
|
||||
},
|
||||
)?;
|
||||
create_runtime_session(
|
||||
&store,
|
||||
RuntimeSpineSessionOptions {
|
||||
session_id: "session-close".to_string(),
|
||||
objective: "Prove bounded closure.".to_string(),
|
||||
created_at: "2026-08-08T20:00:01Z".to_string(),
|
||||
coordinator_note: "test".to_string(),
|
||||
},
|
||||
)?;
|
||||
create_runtime_job(
|
||||
&store,
|
||||
RuntimeSpineJobOptions {
|
||||
job_id: "job-close".to_string(),
|
||||
session_id: "session-close".to_string(),
|
||||
role: "continuity".to_string(),
|
||||
created_at: "2026-08-08T20:00:02Z".to_string(),
|
||||
summary: "Close me first.".to_string(),
|
||||
artifact_refs: Vec::new(),
|
||||
},
|
||||
)?;
|
||||
let closure = RuntimeSpineSessionClosureOptions {
|
||||
session_id: "session-close".to_string(),
|
||||
completed_at: "2026-08-08T20:00:04Z".to_string(),
|
||||
summary: "All session-local jobs are terminal.".to_string(),
|
||||
};
|
||||
assert!(close_runtime_session(&store, closure.clone()).is_err());
|
||||
complete_runtime_job(
|
||||
&store,
|
||||
RuntimeSpineJobResultOptions {
|
||||
result_id: "result-close".to_string(),
|
||||
job_id: "job-close".to_string(),
|
||||
completed_at: "2026-08-08T20:00:03Z".to_string(),
|
||||
verdict: "pass".to_string(),
|
||||
summary: "Closed.".to_string(),
|
||||
next_safe_move: "Complete the session.".to_string(),
|
||||
evidence_refs: Vec::new(),
|
||||
artifact_refs: Vec::new(),
|
||||
},
|
||||
)?;
|
||||
|
||||
let completed = close_runtime_session(&store, closure.clone())?;
|
||||
assert_eq!(completed.status, EpiphanyRuntimeSessionStatus::Completed);
|
||||
assert_eq!(close_runtime_session(&store, closure)?, completed);
|
||||
assert!(
|
||||
create_runtime_job(
|
||||
&store,
|
||||
RuntimeSpineJobOptions {
|
||||
job_id: "job-after-close".to_string(),
|
||||
session_id: "session-close".to_string(),
|
||||
role: "continuity".to_string(),
|
||||
created_at: "2026-08-08T20:00:05Z".to_string(),
|
||||
summary: "Must be refused.".to_string(),
|
||||
artifact_refs: Vec::new(),
|
||||
},
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
let mut cache = runtime_spine_cache(&store)?;
|
||||
cache.pull_all_backing_stores()?;
|
||||
let events = cache
|
||||
.get_all::<EpiphanyRuntimeEvent>()?
|
||||
.into_iter()
|
||||
.filter(|event| event.event_type == "session.completed")
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(events.len(), 1);
|
||||
assert_eq!(runtime_spine_status(&store)?.active_sessions, 0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_spine_opens_and_completes_native_jobs() -> Result<()> {
|
||||
let temp = tempdir()?;
|
||||
|
|
|
|||
Reference in a new issue