Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 70 additions & 114 deletions crates/orbit-api/src/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn spawn(state: AppState) -> Vec<JoinHandle<()>> {
loop {
ticker.tick().await;
let tick = Tick::start(&output_state, "outputs");
crate::handlers::outputs::deliver_pending(&output_state).await;
crate::output_delivery::deliver_pending(&output_state).await;
tick.done(true);
}
}));
Expand Down Expand Up @@ -90,7 +90,7 @@ pub fn spawn(state: AppState) -> Vec<JoinHandle<()>> {
loop {
ticker.tick().await;
let tick = Tick::start(&launch_state, "launches");
let ok = crate::handlers::worker_launches::refresh_active_launches(&launch_state).await;
let ok = crate::launches::refresh_active_launches(&launch_state).await;
tick.done(ok);
}
}));
Expand All @@ -111,7 +111,7 @@ pub fn spawn(state: AppState) -> Vec<JoinHandle<()>> {
Ok(runs) => {
after = runs.last().map(|run| run.id);
for run in runs {
match crate::handlers::teams::advance(&team_state, run).await {
match crate::team_runs::advance(&team_state, run).await {
Ok(_) => team_state.metrics.team_run_advanced(),
Err(error) => {
ok = false;
Expand All @@ -127,7 +127,7 @@ pub fn spawn(state: AppState) -> Vec<JoinHandle<()>> {
}
}
}
if let Err(error) = crate::handlers::teams::reconcile_team_outcomes(&team_state).await {
if let Err(error) = crate::team_runs::reconcile_team_outcomes(&team_state).await {
ok = false;
tracing::warn!(%error, "team outcome reconciliation failed");
}
Expand Down Expand Up @@ -518,15 +518,20 @@ async fn fire_trigger_inner(state: &AppState, trigger: Trigger) -> anyhow::Resul
// error and counts towards the trigger's consecutive-failure auto-disable. Harness
// unavailability and the hourly quota skip this tick instead of failing the trigger.
let identity_user = trigger.owner_user_id.or(agent.owner_user_id);
let identity = crate::preflight::background_access(
let verdict = crate::preflight::evaluate_background(&crate::preflight::BackgroundPreflight {
state,
trigger.org_id,
&[trigger.owner_user_id, agent.owner_user_id],
)
org_id: trigger.org_id,
owners: &[trigger.owner_user_id, agent.owner_user_id],
agent: &agent,
spec: &spec,
harness: harness_kind,
extra_connection_ids: &[],
team: None,
})
.await;
let access = match identity {
Ok(access) => access,
Err(error) => {
match verdict {
crate::preflight::BackgroundVerdict::Allowed => {}
crate::preflight::BackgroundVerdict::Denied(error) => {
crate::preflight::audit_denied_run(
state,
trigger.org_id,
Expand All @@ -539,42 +544,20 @@ async fn fire_trigger_inner(state: &AppState, trigger: Trigger) -> anyhow::Resul
.await;
anyhow::bail!("scheduled run refused: {}", error.message);
}
};
if let Some(access) = &access {
let preflight = crate::preflight::Preflight {
state,
org_id: trigger.org_id,
access,
agent: &agent,
spec: &spec,
harness: harness_kind,
extra_connection_ids: &[],
};
if let Err(error) = preflight.authorize_access().await {
crate::preflight::audit_denied_run(
state,
trigger.org_id,
"trigger.fire_denied",
"trigger",
trigger.id,
&error,
identity_user,
)
.await;
anyhow::bail!("scheduled run refused: {}", error.message);
crate::preflight::BackgroundVerdict::Delayed(
crate::preflight::DelayReason::HarnessUnavailable,
error,
) => {
tracing::warn!(trigger_id = %trigger.id, error = %error.message, "scheduled trigger skipped: harness unavailable");
return Ok(());
}
crate::preflight::BackgroundVerdict::Delayed(
crate::preflight::DelayReason::HourlyQuota,
error,
) => {
tracing::info!(trigger_id = %trigger.id, error = %error.message, "scheduled trigger skipped: hourly quota reached");
return Ok(());
}
}
if let Err(error) =
crate::preflight::require_harness_available(state, trigger.org_id, harness_kind).await
{
tracing::warn!(trigger_id = %trigger.id, error = %error.message, "scheduled trigger skipped: harness unavailable");
return Ok(());
}
if let Err(error) =
crate::preflight::require_hourly_quota(state, trigger.org_id, agent.id, &spec).await
{
tracing::info!(trigger_id = %trigger.id, error = %error.message, "scheduled trigger skipped: hourly quota reached");
return Ok(());
}
let idempotency_key = format!("trigger:{}:{}", trigger.id, fired_at.timestamp_millis());
let execution = match state
Expand Down Expand Up @@ -881,9 +864,7 @@ pub(crate) async fn report_ended_execution(
let worker_terminated =
code.is_some_and(|code| orbit_contracts::WorkerTerminationReason::parse(code).is_some());
if worker_terminated || matches!(code, Some("harness_unresponsive")) {
if let Err(error) =
crate::handlers::teams::fail_team_run_for_execution(state, execution).await
{
if let Err(error) = crate::team_runs::fail_team_run_for_execution(state, execution).await {
tracing::warn!(%error, execution_id = %execution.id, "failed to fail team run for ended execution");
}
}
Expand Down Expand Up @@ -945,19 +926,14 @@ async fn reconcile_worker_tags(state: &AppState) -> anyhow::Result<()> {
}
}
let old_image = launch.image.clone();
match crate::handlers::worker_launches::stop_launch_internal(state, &auth, launch).await
{
match crate::launches::stop_launch_internal(state, &auth, launch).await {
Ok(_) => {}
Err(error) => {
tracing::warn!(profile = %profile.name, %error, "could not stop old worker");
continue;
}
}
match crate::handlers::worker_launches::launch_profile_internal(
state, &auth, profile.id, None,
)
.await
{
match crate::launches::launch_profile_internal(state, &auth, profile.id, None).await {
Ok(new_launch) => {
tracing::info!(
profile = %profile.name,
Expand Down Expand Up @@ -1200,68 +1176,48 @@ async fn advance_runtime(
// Shared execution preflight (#140) for the runtime creator (falling back to the
// agent owner). Lost access stops the runtime with a recorded error; an unavailable
// harness or exhausted hourly quota delays the next tick.
let access = crate::preflight::background_access(
let verdict = crate::preflight::evaluate_background(&crate::preflight::BackgroundPreflight {
state,
runtime.org_id,
&[runtime.created_by, agent.owner_user_id],
)
org_id: runtime.org_id,
owners: &[runtime.created_by, agent.owner_user_id],
agent: &agent,
spec: &spec,
harness: harness_kind,
extra_connection_ids: &[],
team: None,
})
.await;
let denied = match &access {
Err(error) => Some(error.clone()),
Ok(Some(access)) => crate::preflight::Preflight {
state,
org_id: runtime.org_id,
access,
agent: &agent,
spec: &spec,
harness: harness_kind,
extra_connection_ids: &[],
}
.authorize_access()
.await
.err(),
Ok(None) => None,
};
if let Some(error) = denied {
crate::preflight::audit_denied_run(
state,
runtime.org_id,
"runtime.run_denied",
"agent",
agent.id,
&error,
runtime.created_by.or(agent.owner_user_id),
)
.await;
let _ = state
.store
.halt_agent_runtime(runtime.id, &format!("run refused: {}", error.message))
.await;
return Ok(());
}
let delayed = match crate::preflight::require_harness_available(
state,
runtime.org_id,
harness_kind,
)
.await
{
Ok(()) => {
crate::preflight::require_hourly_quota(state, runtime.org_id, agent.id, &spec).await
}
Err(error) => Err(error),
};
if let Err(error) = delayed {
let _ = state
.store
.advance_agent_runtime(
runtime.id,
Some(&format!("run delayed: {}", error.message)),
chrono::Utc::now()
+ chrono::Duration::seconds(runtime.interval_seconds.max(30) as i64),
match verdict {
crate::preflight::BackgroundVerdict::Allowed => {}
crate::preflight::BackgroundVerdict::Denied(error) => {
crate::preflight::audit_denied_run(
state,
runtime.org_id,
"runtime.run_denied",
"agent",
agent.id,
&error,
runtime.created_by.or(agent.owner_user_id),
)
.await;
return Ok(());
let _ = state
.store
.halt_agent_runtime(runtime.id, &format!("run refused: {}", error.message))
.await;
return Ok(());
}
crate::preflight::BackgroundVerdict::Delayed(_, error) => {
let _ = state
.store
.advance_agent_runtime(
runtime.id,
Some(&format!("run delayed: {}", error.message)),
chrono::Utc::now()
+ chrono::Duration::seconds(runtime.interval_seconds.max(30) as i64),
)
.await;
return Ok(());
}
}
let input = serde_json::json!({
"task": runtime.task,
Expand Down
6 changes: 3 additions & 3 deletions crates/orbit-api/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use orbit_security::AuthContext;
use orbit_store::team_queue::TeamQueueItem;
use orbit_store::AgentTeam;

use crate::handlers::teams::parse_definition;
use crate::state::AppState;
use crate::team_runs::parse_definition;
use crate::work_sources::{self, WorkSourceDefinition};

pub async fn dispatch_tick(state: &AppState) -> anyhow::Result<()> {
Expand Down Expand Up @@ -311,8 +311,8 @@ async fn start_queue_item(
auth: &AuthContext,
team: &AgentTeam,
item: &TeamQueueItem,
) -> Result<crate::handlers::teams::TeamRunDetail, crate::error::ApiError> {
crate::handlers::team_queue::start_queue_item(state, auth, team, item).await
) -> Result<crate::team_runs::TeamRunDetail, crate::error::ApiError> {
crate::team_runs::start_queue_item(state, auth, team, item).await
}

struct WorkSourceConnection {
Expand Down
2 changes: 1 addition & 1 deletion crates/orbit-api/src/handlers/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ pub(crate) async fn build_document(
}
PortableKind::AgentTeam => {
let team = store.get_agent_team(org_id, id).await?;
let definition = super::teams::parse_definition(&team)?;
let definition = crate::team_runs::parse_definition(&team)?;
let worker_profile = match definition.worker_profile_id {
Some(profile_id) => names
.worker_profile(profile_id)
Expand Down
3 changes: 2 additions & 1 deletion crates/orbit-api/src/handlers/import_portable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ use crate::dto::{
use crate::error::ApiError;
use crate::extract::ApiJson;
use crate::handlers::skills::{CreateSkillRequest, UpdateSkillRequest};
use crate::handlers::teams::{CreateTeamRequest, TeamModelOverride, TeamStepInput};
use crate::handlers::teams::{CreateTeamRequest, TeamStepInput};
use crate::handlers::tool_policies::ToolPolicyRequest;
use crate::handlers::worker_profiles::CreateWorkerProfileRequest;
use crate::state::AppState;
use crate::team_runs::TeamModelOverride;
use crate::work_sources::{RepositorySpec, WorkSourceDefinition};

#[derive(Debug, Clone, Deserialize, utoipa::IntoParams)]
Expand Down
Loading