use std::collections::HashSet;
use rho_protocol as wire;
use super::jobs::{JobId, JobRecord, JobState, JobSubject, Moved, TurnEnd};
use crate::app::agent_concurrency::{Budget, QueuePlace};
pub(super) fn session_to_wire(id: rho_sdk::SessionId) -> wire::SessionId {
wire::SessionId::new(id.into_string())
}
pub(super) fn session_from_wire(
id: &wire::SessionId,
) -> Result<rho_sdk::SessionId, rho_sdk::InvalidId> {
rho_sdk::SessionId::from_string(id.as_str())
}
impl From<&JobId> for wire::JobId {
fn from(id: &JobId) -> Self {
Self::new(id.as_str())
}
}
impl From<&JobState> for wire::JobState {
fn from(state: &JobState) -> Self {
match state {
JobState::Idle => Self::Idle,
JobState::Queued => Self::Queued,
JobState::Running => Self::Running,
JobState::WaitingForInput => Self::WaitingForInput,
JobState::Ended(TurnEnd::Completed) => Self::Completed,
JobState::Ended(TurnEnd::Failed(_)) => Self::Failed,
JobState::Ended(TurnEnd::Cancelled) => Self::Cancelled,
JobState::Ended(TurnEnd::TimedOut { .. }) => Self::TimedOut,
JobState::Ended(TurnEnd::Interrupted) => Self::Interrupted,
}
}
}
fn failure(state: &JobState) -> Option<String> {
match state {
JobState::Ended(TurnEnd::Failed(error)) => Some(error.clone()),
JobState::Idle
| JobState::Queued
| JobState::Running
| JobState::WaitingForInput
| JobState::Ended(
TurnEnd::Completed
| TurnEnd::Cancelled
| TurnEnd::TimedOut { .. }
| TurnEnd::Interrupted,
) => None,
}
}
pub(super) fn state_event(moved: &Moved) -> wire::SessionEvent {
wire::SessionEvent::State {
state: (&moved.state).into(),
turn: moved.turn.clone(),
}
}
pub(super) fn job_summary(
record: JobRecord,
queue: Option<QueuePlace>,
undelivered: &HashSet<String>,
live: Option<wire::JobLive>,
worktree_stale: bool,
) -> wire::JobSummary {
let JobRecord {
id,
subject,
state,
workspace,
parent,
grant,
grant_limit,
admission: _,
created_at,
updated_at,
outcome: _,
turn: _,
worktree,
worktree_removed_at: _,
resume_after_restart: _,
keeping: _,
} = record;
let (kind, session, run, workflow_run) = match subject {
JobSubject::Session { session } => (wire::JobKind::Session, Some(session), None, None),
JobSubject::Subagent { session, run } => (wire::JobKind::Subagent, session, run, None),
JobSubject::Workflow { workflow_run } => (
wire::JobKind::Workflow,
None,
None,
Some(workflow_run.to_string()),
),
};
wire::JobSummary {
id: (&id).into(),
kind,
state: (&state).into(),
workspace,
parent: parent.as_ref().map(Into::into),
grant: (!grant.is_root()).then(|| grant.into()),
grant_limit: grant_limit.map(Into::into),
session: session.map(session_to_wire),
run,
result_pending: workflow_run
.as_ref()
.is_some_and(|run| undelivered.contains(run)),
workflow_run,
created_at,
updated_at,
error: failure(&state),
queue: queue.map(Into::into),
live,
worktree,
worktree_stale,
}
}
impl From<Budget> for wire::Budget {
fn from(budget: Budget) -> Self {
match budget {
Budget::AgentConcurrency => Self::AgentConcurrency,
Budget::ClaudeAgentConcurrency => Self::ClaudeAgentConcurrency,
}
}
}
impl From<QueuePlace> for wire::QueuePlace {
fn from(place: QueuePlace) -> Self {
let QueuePlace {
budget,
limit,
position,
} = place;
let count = |value: usize| u32::try_from(value).unwrap_or(u32::MAX);
Self {
budget: budget.into(),
limit: count(limit),
position: count(position),
}
}
}