mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 19:07:41 +00:00
fix(mobile): keep disconnected desktops on Home and judge agents by the client's connect time
This commit is contained in:
@@ -46,15 +46,16 @@ import { useMobileHomeHostConnections } from './use-mobile-home-host-connections
|
||||
|
||||
export type MobileHomeData = ReturnType<typeof useMobileHomeData>
|
||||
|
||||
type HostClientEntry = { hostId: string; client: RpcClient }
|
||||
// A paired desktop with its client, or none while Home holds no connection to it (disconnected, never opened).
|
||||
type HomeHost = { hostId: string; client: RpcClient | null }
|
||||
|
||||
// Home reads one of each per paired desktop.
|
||||
const homeQueryOptions = <TData>(
|
||||
query: HostQuery<typeof NO_HOST_LIST_INPUT, TData>,
|
||||
entry: HostClientEntry,
|
||||
host: HomeHost,
|
||||
enabled: boolean
|
||||
) => ({
|
||||
...hostObserverOptions(query, entry.client, entry.hostId, HOST_LIST_TARGET, NO_HOST_LIST_INPUT),
|
||||
...hostObserverOptions(query, host.client, host.hostId, HOST_LIST_TARGET, NO_HOST_LIST_INPUT),
|
||||
enabled
|
||||
})
|
||||
|
||||
@@ -86,31 +87,43 @@ export function useMobileHomeData() {
|
||||
const [homeFocused, setHomeFocused] = useState(false)
|
||||
const hosts = useMemo(() => selectConnectableHostProfiles(hostCatalog), [hostCatalog])
|
||||
const connections = useMobileHomeHostConnections(hosts, hostCatalog)
|
||||
const { allClients, connectedSince, hostStates } = connections
|
||||
const { allClients, hostLastConnected, hostStates } = connections
|
||||
const allClientsRef = useRef(allClients)
|
||||
// Every paired desktop keeps its last counts and totals on Home, connected or not.
|
||||
const homeHosts = useMemo((): HomeHost[] => {
|
||||
const clients = new Map(allClients.map((entry) => [entry.hostId, entry.client]))
|
||||
return hosts.map((host) => ({ hostId: host.id, client: clients.get(host.id) ?? null }))
|
||||
}, [hosts, allClients])
|
||||
// A read from before the desktop's latest connect, or from an earlier launch, can't vouch for agents or usage.
|
||||
const isLive = useCallback(
|
||||
(hostId: string, readAt: number): boolean => {
|
||||
const connectedAt = hostLastConnected[hostId]
|
||||
return connectedAt != null && readAt >= connectedAt
|
||||
},
|
||||
[hostLastConnected]
|
||||
)
|
||||
|
||||
// A desktop's reads wait for its connection; one still connecting would report the list unavailable.
|
||||
const countsQueries = useMemo(
|
||||
() =>
|
||||
allClients.map((entry) =>
|
||||
homeQueryOptions(homeWorktreeCountsQuery, entry, hostStates[entry.hostId] === 'connected')
|
||||
homeHosts.map((host) =>
|
||||
homeQueryOptions(homeWorktreeCountsQuery, host, hostStates[host.hostId] === 'connected')
|
||||
),
|
||||
[allClients, hostStates]
|
||||
[homeHosts, hostStates]
|
||||
)
|
||||
const combineCounts = useCallback(
|
||||
(results: QueryObserverResult<HomeWorktreeCounts>[]): Record<string, HostWorktreeInfo> => {
|
||||
const byHost: Record<string, HostWorktreeInfo> = {}
|
||||
allClients.forEach((entry, at) => {
|
||||
homeHosts.forEach(({ hostId }, at) => {
|
||||
const result = results[at]
|
||||
const info =
|
||||
result && homeWorktreeInfo(entry.hostId, readOf(result), connectedSince[entry.hostId])
|
||||
const info = result && homeWorktreeInfo(hostId, readOf(result), isLive)
|
||||
if (info) {
|
||||
byHost[entry.hostId] = info
|
||||
byHost[hostId] = info
|
||||
}
|
||||
})
|
||||
return byHost
|
||||
},
|
||||
[allClients, connectedSince]
|
||||
[homeHosts, isLive]
|
||||
)
|
||||
// A hidden Home reads nothing; coming back reads what went stale meanwhile.
|
||||
const worktreeInfo = useQueries({
|
||||
@@ -120,15 +133,15 @@ export function useMobileHomeData() {
|
||||
})
|
||||
const statsQueries = useMemo(
|
||||
() =>
|
||||
allClients.map((entry) =>
|
||||
homeQueryOptions(homeStatsQuery, entry, hostStates[entry.hostId] === 'connected')
|
||||
homeHosts.map((host) =>
|
||||
homeQueryOptions(homeStatsQuery, host, hostStates[host.hostId] === 'connected')
|
||||
),
|
||||
[allClients, hostStates]
|
||||
[homeHosts, hostStates]
|
||||
)
|
||||
const combineStats = useCallback(
|
||||
(results: QueryObserverResult<HomeStatsRow>[]): Record<string, HomeStatsRow> =>
|
||||
Object.fromEntries(allClients.map((entry, at) => [entry.hostId, results[at]?.data])),
|
||||
[allClients]
|
||||
Object.fromEntries(homeHosts.map(({ hostId }, at) => [hostId, results[at]?.data])),
|
||||
[homeHosts]
|
||||
)
|
||||
const statsByHost = useQueries({
|
||||
queries: statsQueries,
|
||||
@@ -136,28 +149,26 @@ export function useMobileHomeData() {
|
||||
subscribed: homeFocused
|
||||
})
|
||||
const accountsQueries = useMemo(
|
||||
() => allClients.map((entry) => homeAccountsOptions(entry.hostId)),
|
||||
[allClients]
|
||||
() => homeHosts.map(({ hostId }) => homeAccountsOptions(hostId)),
|
||||
[homeHosts]
|
||||
)
|
||||
// Usage is live: only a snapshot from this connection shows, never a stored or disconnected one.
|
||||
const combineAccounts = useCallback(
|
||||
(results: QueryObserverResult<AccountsSnapshot | null>[]): Record<string, AccountsSnapshot> => {
|
||||
const byHost: Record<string, AccountsSnapshot> = {}
|
||||
allClients.forEach((entry, at) => {
|
||||
homeHosts.forEach(({ hostId }, at) => {
|
||||
const result = results[at]
|
||||
const since = connectedSince[entry.hostId]
|
||||
if (
|
||||
result?.data &&
|
||||
since !== undefined &&
|
||||
result.dataUpdatedAt >= since &&
|
||||
hostStates[entry.hostId] === 'connected'
|
||||
hostStates[hostId] === 'connected' &&
|
||||
isLive(hostId, result.dataUpdatedAt)
|
||||
) {
|
||||
byHost[entry.hostId] = result.data
|
||||
byHost[hostId] = result.data
|
||||
}
|
||||
})
|
||||
return byHost
|
||||
},
|
||||
[allClients, connectedSince, hostStates]
|
||||
[homeHosts, hostStates, isLive]
|
||||
)
|
||||
const accountsByHost = useQueries({
|
||||
queries: accountsQueries,
|
||||
|
||||
@@ -9,7 +9,6 @@ import { retainHostConnectionInvalidation } from '../query/host-connection-inval
|
||||
import { retainHostEventRouter } from '../query/host-event-router'
|
||||
import { keepNativeHostQueries } from '../query/native-query-persistence'
|
||||
import { usePrimeHosts } from '../transport/connection/host-client-hooks'
|
||||
import { createHostConnectRefetchGate } from '../transport/hosts/host-connect-refetch-gate'
|
||||
import { selectHomeAutoConnectHostIds } from '../transport/hosts/home-host-auto-connect'
|
||||
import type { RpcClient } from '../transport/rpc/rpc-client-contract'
|
||||
import type { ConnectionState } from '../transport/connection/connection-state'
|
||||
@@ -23,9 +22,8 @@ import {
|
||||
|
||||
// Home's per-desktop reads (counts, stats) are queries; this wires what a connection carries besides them.
|
||||
function wireMobileHomeHostSubscriptions(
|
||||
entry: { hostId: string; client: RpcClient; state: ConnectionState },
|
||||
queryClient: QueryClient,
|
||||
onConnected: (hostId: string) => void
|
||||
entry: { hostId: string; client: RpcClient },
|
||||
queryClient: QueryClient
|
||||
): () => void {
|
||||
const { hostId, client } = entry
|
||||
keepNativeHostQueries(queryClient, hostId)
|
||||
@@ -34,12 +32,7 @@ function wireMobileHomeHostSubscriptions(
|
||||
const router = retainHostEventRouter(queryClient, hostId, client)
|
||||
let unsubscribeNotifications: (() => void) | null = null
|
||||
let unsubscribeAccounts: (() => void) | null = null
|
||||
const connectGate = createHostConnectRefetchGate()
|
||||
const wireState = (state: ConnectionState): void => {
|
||||
if (connectGate.observe(state)) {
|
||||
// Only reads after this moment may show agent states or usage as current.
|
||||
onConnected(hostId)
|
||||
}
|
||||
if (state === 'connected') {
|
||||
unsubscribeNotifications ??= subscribeToDesktopNotifications(client, hostId)
|
||||
// One stream per connection: each subscribe makes the desktop check usage, so focus must not resubscribe.
|
||||
@@ -55,7 +48,8 @@ function wireMobileHomeHostSubscriptions(
|
||||
unsubscribeAccounts?.()
|
||||
unsubscribeAccounts = null
|
||||
}
|
||||
wireState(entry.state)
|
||||
// The live state: the client may have connected since the render that listed it.
|
||||
wireState(client.getState())
|
||||
const unsubscribeState = client.onStateChange(wireState)
|
||||
return () => {
|
||||
unsubscribeState()
|
||||
@@ -71,8 +65,6 @@ export function useMobileHomeHostConnections(
|
||||
hostCatalog: HostCatalogEntry[]
|
||||
) {
|
||||
const queryClient = useQueryClient()
|
||||
// When Home last saw each desktop connect; a read older than that was a connection ago.
|
||||
const [connectedSince, setConnectedSince] = useState<Record<string, number>>({})
|
||||
const [hostStates, setHostStates] = useState<Record<string, ConnectionState>>({})
|
||||
const [hostAttempts, setHostAttempts] = useState<Record<string, number>>({})
|
||||
const [hostLastConnected, setHostLastConnected] = useState<Record<string, number | null>>({})
|
||||
@@ -115,9 +107,7 @@ export function useMobileHomeHostConnections(
|
||||
if (!subscriptionsRef.current.has(entry.hostId)) {
|
||||
subscriptionsRef.current.set(entry.hostId, {
|
||||
client: entry.client,
|
||||
cleanup: wireMobileHomeHostSubscriptions(entry, queryClient, (hostId) =>
|
||||
setConnectedSince((previous) => ({ ...previous, [hostId]: Date.now() }))
|
||||
)
|
||||
cleanup: wireMobileHomeHostSubscriptions(entry, queryClient)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -136,7 +126,6 @@ export function useMobileHomeHostConnections(
|
||||
return {
|
||||
allClients,
|
||||
autoConnectHostIds,
|
||||
connectedSince,
|
||||
hostAttempts,
|
||||
hostLastConnected,
|
||||
hostStates
|
||||
|
||||
@@ -23,7 +23,11 @@ export function worktreeCatalogMountAdapters(
|
||||
let disposed = false
|
||||
// Home's card for one desktop, read on the connection it was asked on.
|
||||
const publish = (failed: boolean) => {
|
||||
const card = homeWorktreeInfo(HOST, { data: counts, dataUpdatedAt: Date.now(), failed }, 0)
|
||||
const card = homeWorktreeInfo(
|
||||
HOST,
|
||||
{ data: counts, dataUpdatedAt: Date.now(), failed },
|
||||
() => true
|
||||
)
|
||||
info = card ? { [HOST]: card } : {}
|
||||
const effect = () => context.effect('info', projectObservable(info))
|
||||
if (context.captureObservation) {
|
||||
|
||||
@@ -73,7 +73,7 @@ export type HomeWorktreeRead = {
|
||||
export function homeWorktreeInfo(
|
||||
hostId: string,
|
||||
read: HomeWorktreeRead,
|
||||
liveSince: number | undefined
|
||||
isLive: (hostId: string, readAt: number) => boolean
|
||||
): HostWorktreeInfo | undefined {
|
||||
const { data, failed } = read
|
||||
if (!data) {
|
||||
@@ -82,7 +82,7 @@ export function homeWorktreeInfo(
|
||||
? { hostId, totalWorktrees: 0, activeCount: 0, catalogUnavailable: true }
|
||||
: undefined
|
||||
}
|
||||
const live = liveSince !== undefined && read.dataUpdatedAt >= liveSince
|
||||
const live = isLive(hostId, read.dataUpdatedAt)
|
||||
const agents = data.agents.length > 0 ? data.agents : undefined
|
||||
return {
|
||||
hostId,
|
||||
|
||||
Reference in New Issue
Block a user