Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 63 additions & 11 deletions apps/desktop/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions apps/desktop/src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ pub async fn refresh_runtime_status(
project_state_result(&app, state.refresh_runtime_status().await)
}

#[tauri::command]
pub async fn observe_runtime_status(
app: AppHandle,
state: State<'_, AppState>,
) -> Result<DesktopStateSnapshot, DesktopError> {
project_state_result(&app, state.observe_runtime_status().await)
}

#[tauri::command]
pub async fn observe_chatgpt_activity(
app: AppHandle,
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ fn desktop_builder() -> tauri::Builder<tauri::Wry> {
commands::get_launch_at_login,
commands::set_launch_at_login,
commands::refresh_runtime_status,
commands::observe_runtime_status,
commands::observe_chatgpt_activity,
commands::resume_saved_runtime,
commands::resume_saved_connections,
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src-tauri/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod projectless_tests;
#[cfg(test)]
mod reconfiguration_tests;
mod runner_capability_grant;
mod runtime_observation;
mod runtime_shell;
#[cfg(test)]
mod shutdown_tests;
Expand Down Expand Up @@ -104,6 +105,7 @@ pub struct AppState {
activity: ActivityLog,
operations: OperationController,
refresh_lock: tokio::sync::Mutex<()>,
runtime_observation: runtime_observation::RuntimeObservationCache,
runner_settings_reader: Arc<Mutex<()>>,
shutdown_signal: CancellationSignal,
shutdown_started: AtomicBool,
Expand Down
56 changes: 18 additions & 38 deletions apps/desktop/src-tauri/src/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl AppState {
connections,
operations: OperationController::new(activity.clone()),
refresh_lock: tokio::sync::Mutex::new(()),
runtime_observation: runtime_observation::RuntimeObservationCache::default(),
runner_settings_reader: Arc::new(Mutex::new(())),
activity,
shutdown_signal: CancellationSignal::new(),
Expand Down Expand Up @@ -153,35 +154,11 @@ impl AppState {
})
}

pub async fn refresh_runtime_status(&self) -> DesktopResult<DesktopStateSnapshot> {
if self.shutdown_signal.is_cancelled() {
return Err(cancelled_error());
}
if let Some(current) = self.operations.observe()? {
return Err(crate::operation::busy_error(&current));
}
if self.get_state().configuration_issue.is_some() {
return Err(DesktopError::new(
"configuration_migration_failed",
"Configuration could not be migrated",
"Open Diagnostics or explicitly restore the previous known-good configuration.",
));
}

let before_generation = self.get_state().state_generation;
let _refresh_guard = self.refresh_lock.lock().await;
if self.shutdown_signal.is_cancelled() {
return Err(cancelled_error());
}
if let Some(current) = self.operations.observe()? {
return Err(crate::operation::busy_error(&current));
}
// Coalesce overlapping background refreshes: if another refresh already
// published a fresher snapshot while we waited, return it directly.
if self.get_state().state_generation != before_generation {
return Ok(self.get_state());
}

async fn refresh_runtime_status_uncached(
&self,
before_generation: u64,
) -> DesktopResult<runtime_observation::RefreshOutcome> {
use runtime_observation::RefreshOutcome;
let cancellation =
CancellationContext::new(CancellationSignal::new(), self.shutdown_signal.clone());
let start_op_generation = self.operations.generation();
Expand Down Expand Up @@ -233,20 +210,20 @@ impl AppState {
RefreshPlan::QuickShare | RefreshPlan::Unconfigured => {
let mut slot = self.core.lock().await;
let Some(core) = slot.as_mut() else {
return Ok(self.get_state());
return Ok(RefreshOutcome::superseded(self.get_state()));
};
if self.operations.current().is_some()
|| self.operations.generation() != start_op_generation
{
return Ok(self.get_state());
return Ok(RefreshOutcome::superseded(self.get_state()));
}
core.refresh_runtime_status(&cancellation).await?;
if operation_completion::reconcile_runtime_errors_after_observation(
&mut core.snapshot,
) {
core.publish_snapshot();
}
Ok(self.get_state())
Ok(RefreshOutcome::applied(self.get_state(), None))
}
RefreshPlan::Persistent { environment_id } => {
let store = environment::store()?;
Expand All @@ -260,26 +237,27 @@ impl AppState {
Some(self.activity.latest_sequence()),
)
})?;
let heartbeat = runtime_observation::RuntimeHeartbeat::from(&observed.observation);
cancellation.check()?;
let mut slot = self.core.lock().await;
let Some(core) = slot.as_mut() else {
return Ok(self.get_state());
return Ok(RefreshOutcome::superseded(self.get_state()));
};
if self.operations.current().is_some()
|| self.operations.generation() != start_op_generation
|| observed.environment.environment_id != environment_id
|| core.config.persistent_environment.as_deref()
!= Some(environment_id.as_str())
{
return Ok(self.get_state());
return Ok(RefreshOutcome::superseded(self.get_state()));
}
core.project_environment_result(&store, observed).await?;
if operation_completion::reconcile_runtime_errors_after_observation(
&mut core.snapshot,
) {
core.publish_snapshot();
}
Ok(self.get_state())
Ok(RefreshOutcome::applied(self.get_state(), Some(heartbeat)))
}
RefreshPlan::NonPersistent {
mut adapter,
Expand Down Expand Up @@ -365,14 +343,14 @@ impl AppState {

let mut slot = self.core.lock().await;
let Some(core) = slot.as_mut() else {
return Ok(self.get_state());
return Ok(RefreshOutcome::superseded(self.get_state()));
};
if self.operations.current().is_some()
|| self.operations.generation() != start_op_generation
|| core.config.persistent_environment.is_some()
|| runner_identity_from_config(&core.config).as_ref() != Some(&identity)
{
return Ok(self.get_state());
return Ok(RefreshOutcome::superseded(self.get_state()));
}
if let Some(binaries) = resolved_binaries {
core.snapshot.binaries = Some(binaries.info());
Expand All @@ -395,7 +373,7 @@ impl AppState {
&mut core.snapshot,
);
core.publish_snapshot();
Ok(self.get_state())
Ok(RefreshOutcome::applied(self.get_state(), None))
}
}
}
Expand Down Expand Up @@ -709,6 +687,7 @@ impl AppState {
));
}
let mut operation = self.operations.admit(kind, cancellable)?;
self.runtime_observation.invalidate();
let cancellation = operation.cancellation_context(self.shutdown_signal.clone());
// Both waits precede dispatch. Cancelling either wait cannot lose the
// core, leave a queued write, or skip cleanup of an external effect.
Expand Down Expand Up @@ -774,6 +753,7 @@ impl AppState {
core.publish_snapshot();
}
*self.core.lock().await = Some(core);
self.runtime_observation.invalidate();
operation.finish(&result);
match result {
Ok(_) => Ok(self.get_state()),
Expand Down
Loading
Loading