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
1 change: 1 addition & 0 deletions CLAUDE.md
13 changes: 7 additions & 6 deletions apps/desktop/scripts/check-file-sizes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ const ALLOWLIST = {
// Cloud REST helpers (auth + conversations + agents). sendConversationMessage removed;
// further split to minos-cloud helpers planned (freeze above current LOC).
"shared/lib/minos-cloud.ts": 1100,
// Mock fixtures + Conversation types; extract mock-conversations.ts planned.
// Mock fixtures + presentation maps; domain types live in shared/domain.
"shared/lib/mock-data.ts": 850,
// Outbox posts + per-lane worker share one module; split to im-outbox-worker.ts next.
"shared/lib/im-cloud-sync.ts": 950,
// Cloud IM bridge (lifecycle + mark-read + timeline merge). Split mark-read /
// lifecycle helpers next; cap raised after Hub→Cloud rename (im-hub-bridge @784→~826).
"shared/lib/im-cloud-bridge.ts": 850,
// Durable outbox machine + account ownership; split persist backend next.
"shared/lib/im-outbox.ts": 850,
// Outbox posts + per-lane worker (store ownership). Split worker lanes next.
"store/im/im-cloud-sync.ts": 1050,
// Cloud IM bridge (lifecycle + mark-read + timeline merge) under store/im.
"store/im/im-cloud-bridge.ts": 850,
};

/**
Expand Down
15 changes: 14 additions & 1 deletion apps/desktop/src-tauri/src/commands/host_link.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Tauri commands for same-account Host Link (D02).

use crate::app_state::AppState;
use crate::daemon::{HostApplyLinkTokenDto, HostPrepareLinkDto, HostSignLinkProofDto};
use crate::daemon::{
HostApplyLinkTokenDto, HostClearCredentialDto, HostPrepareLinkDto, HostSignLinkProofDto,
};
use tauri::State;

#[tauri::command]
Expand Down Expand Up @@ -39,3 +41,14 @@ pub async fn daemon_host_apply_link_token(
.await
.map_err(|e| e.to_string())
}

#[tauri::command]
pub async fn daemon_host_clear_credential(
state: State<'_, AppState>,
) -> Result<HostClearCredentialDto, String> {
state
.daemon
.host_clear_credential()
.await
.map_err(|e| e.to_string())
}
85 changes: 75 additions & 10 deletions apps/desktop/src-tauri/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ use minos_protocol::local_rpc::{
use minos_protocol::{
AppendConversationMessageParams, ApprovalDecisionRequest, CreateConversationParams,
CreateProjectRequest, HostApplyLinkTokenParams, HostApplyLinkTokenResponse,
HostPrepareLinkResponse, HostSignLinkProofParams, HostSignLinkProofResponse, ListClisResponse,
ListConversationAgentSessionsParams, ListConversationMessagesParams, ListConversationsParams,
ListProjectsResponse, LocalConversationMessage, LocalConversationSummary, LocalReactionGroup,
ProjectSummary, ReadSessionParams, RemoveConversationAgentParams, SendUserMessageRequest,
SessionState, SessionSummary, StartAgentInConversationRequest, StartAgentResponse,
HostClearCredentialResponse, HostPrepareLinkResponse, HostSignLinkProofParams,
HostSignLinkProofResponse, ListClisResponse, ListConversationAgentSessionsParams,
ListConversationMessagesParams, ListConversationsParams, ListProjectsResponse,
LocalConversationMessage, LocalConversationSummary, LocalReactionGroup, ProjectSummary,
ReadSessionParams, RemoveConversationAgentParams, SendUserMessageRequest, SessionState,
SessionSummary, StartAgentInConversationRequest, StartAgentResponse,
ToggleConversationMessageReactionParams, ToggleConversationMessageReactionResponse,
UpdateConversationParams,
};
Expand Down Expand Up @@ -403,6 +404,12 @@ pub struct HostApplyLinkTokenDto {
pub linked: bool,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct HostClearCredentialDto {
pub cleared: bool,
}

pub struct DaemonBridge {
inner: Mutex<BridgeInner>,
/// Serialize connect/bootstrap so React StrictMode double-mount cannot
Expand All @@ -421,6 +428,9 @@ struct BridgeInner {
source: String,
/// Keep alive for the app lifetime when we started local RPC ourselves.
managed: Option<Arc<DaemonHandle>>,
/// True while stop is in progress — new connect must wait/refuse so we
/// never start a second daemon against a still-tearing-down handle.
teardown_in_progress: bool,
/// Generation of the pumps currently running for this client.
pumps_running_for: u64,
}
Expand All @@ -434,6 +444,7 @@ impl DaemonBridge {
last_error: None,
source: "none".into(),
managed: None,
teardown_in_progress: false,
pumps_running_for: 0,
}),
connect_lock: Mutex::new(()),
Expand All @@ -460,6 +471,25 @@ impl DaemonBridge {
let _gate = self.connect_lock.lock().await;
let explicit = override_url.is_some();

// Refuse concurrent start while managed stop still owns the handle.
{
let guard = self.inner.lock().await;
if guard.teardown_in_progress {
return ConnectionDto {
connected: false,
endpoint: None,
error: Some("managed daemon is shutting down".into()),
source: "error".into(),
managed: guard.managed.is_some(),
cloud_online: false,
has_host_token: minos_daemon::device_secret_store::read()
.ok()
.flatten()
.is_some(),
};
}
}

// Already healthy? Reuse (and ensure push pumps are armed).
{
let guard = self.inner.lock().await;
Expand Down Expand Up @@ -691,13 +721,19 @@ impl DaemonBridge {
/// After success the bridge owns neither a client nor a managed handle;
/// a later [`Self::connect`] starts a fresh managed daemon (used by
/// `restore_after_failed_update`).
///
/// The managed handle stays owned until `stop` reaches a terminal state so
/// an outer timeout cannot drop the only reference mid-teardown. Concurrent
/// `connect` is refused while `teardown_in_progress`.
pub async fn shutdown_managed(&self) -> Result<(), String> {
// Drop the WS client first so subscription pumps exit promptly.
// Keep `managed` until stop finishes — clone Arc, clear only after.
let managed = {
let mut guard = self.inner.lock().await;
guard.client = None;
guard.endpoint = None;
guard.managed.take()
guard.teardown_in_progress = true;
guard.managed.clone()
};
// Invalidate discovery so a concurrent connect does not attach to a
// port that is about to close (or already closed).
Expand All @@ -706,21 +742,38 @@ impl DaemonBridge {
self.pump_generation.fetch_add(1, Ordering::SeqCst);

if let Some(handle) = managed {
match handle.stop().await {
Ok(()) => {
// Detach stop onto a join handle so an outer timeout cancels only
// our await, not the stop future itself (providers still terminate).
let stop_task = tokio::spawn(async move { handle.stop().await });
let result = match stop_task.await {
Ok(Ok(())) => {
info!(target: "minos_desktop", "managed daemon stopped");
Ok(())
}
Err(e) => {
Ok(Err(e)) => {
warn!(
target: "minos_desktop",
error = %e,
"managed daemon stop failed"
);
Err(e.to_string())
}
}
Err(join_err) => {
warn!(
target: "minos_desktop",
error = %join_err,
"managed daemon stop task panicked"
);
Err(join_err.to_string())
}
};
let mut guard = self.inner.lock().await;
guard.managed = None;
guard.teardown_in_progress = false;
result
} else {
let mut guard = self.inner.lock().await;
guard.teardown_in_progress = false;
Ok(())
}
}
Expand Down Expand Up @@ -1277,6 +1330,18 @@ impl DaemonBridge {
})
}

/// Drop local host credential so the next account cannot inherit `/ws/host`.
pub async fn host_clear_credential(&self) -> Result<HostClearCredentialDto> {
let client = self.client().await?;
let response: HostClearCredentialResponse = client
.request("minos_local_host_clear_credential", ArrayParams::new())
.await
.context("minos_local_host_clear_credential")?;
Ok(HostClearCredentialDto {
cleared: response.cleared,
})
}

/// Reattach a suspended/persisted session. When `auto_continue` is true and
/// the store has `needs_continue`, injects a one-shot CONTINUE prompt.
pub async fn resume_session(&self, session_id: String, auto_continue: bool) -> Result<()> {
Expand Down
Loading
Loading