Skip to content

The agent that grows with you

fix(desktop): profile-only secondaries count their live turn too

kshitijk4poor4f766f8fa5f0
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
index 62eeff9c574..60846e343eb 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -908,7 +908,8 @@ export function useGatewayBoot({
// primary thread or a just-created session's owner hold is bound to
// (#93892).
foregroundScopes: foregroundSessionScopes,
- liveScopes: liveSessionScopes,
+ // Defined further down the effect body; read at call time, never during boot.
+ liveScopes: () => liveWorkScopes(),
onLocalProfileRetired: forgetProfileOnlyRuntimeOwners,
onActiveConnectionChanged: publish,
// Keep $activeGatewayProfile in lockstep with the registry's OWN record
@@ -1157,20 +1158,28 @@ export function useGatewayBoot({
// and its backend is free to idle-reap. The active profile is always spared.
// Do not key this off `entry.retained` — that flag only skips dispose-after-
// RPC; idle prune is what reclaims hover-warmed sockets after you leave.
- const recomputeKeptGateways = () => {
+ // Scopes with a running or needs-input session: registry-scoped
+ // (connectionId, profile) keys plus the bare profile of every live local
+ // session. Two sources can expose the same profile name (every source has
+ // a 'default'), so bare profile names can't represent a non-local
+ // source's liveness without keeping the wrong gateway alive. Feeds the
+ // pruner's keep-set and the wake probe's in-flight-work signal.
+ const liveWorkScopes = (): Set<string> => {
const live = new Set([...$workingSessionIds.get(), ...$attentionSessionIds.get()])
- // Registry-scoped (connectionId, profile) scopes with live work. Two
- // sources can expose the same profile name (every source has a
- // 'default'), so bare profile names can't represent a non-local
- // source's liveness without keeping the wrong gateway alive.
- const keep = new Set([...liveSessionScopes(), ...foregroundSessionScopes()])
+ const scopes = liveSessionScopes()
for (const session of $sessions.get()) {
if (live.has(session.id)) {
- keep.add(normalizeProfileKey(session.profile))
+ scopes.add(normalizeProfileKey(session.profile))
}
}
+ return scopes
+ }
+
+ const recomputeKeptGateways = () => {
+ const keep = new Set([...liveWorkScopes(), ...foregroundSessionScopes()])
+
for (const scope of openTileGatewayScopes()) {
keep.add(scope)
}
diff --git a/apps/desktop/src/lib/gateway-liveness-policy.ts b/apps/desktop/src/lib/gateway-liveness-policy.ts
index b5653d521f9..3f219a433b3 100644
--- a/apps/desktop/src/lib/gateway-liveness-policy.ts
+++ b/apps/desktop/src/lib/gateway-liveness-policy.ts
@@ -34,10 +34,10 @@
* (mirroring gateway-liveness usage in use-gateway-boot).
*/
-/** Consecutive unanswered probes tolerated while work is in flight. */
/** Ping budget for a liveness probe (primary and secondary sockets share it). */
export const LIVENESS_PROBE_TIMEOUT_MS = 5_000
+/** Consecutive unanswered probes tolerated while work is in flight. */
export const LIVENESS_PROBE_FAILURE_STREAK = 2
/** How long after a deferred probe we try again (bounded, coalesced). */
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 77c01ca2456..d318c3df3a7 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -99,9 +99,11 @@ interface RegistryConfig {
*/
foregroundScopes?: () => ReadonlySet<string>
/**
- * Scopes with a running or needs-input session runtime. The wake-path
- * liveness probe counts these as in-flight work: prompt.submit returns
- * before the turn ends, so `activeRequests` is 0 during most of a turn.
+ * Scopes with a running or needs-input session runtime, in the same key
+ * language as `foregroundScopes` (composite registry keys, bare profiles
+ * for local/legacy entries). The wake-path liveness probe counts these as
+ * in-flight work: prompt.submit returns before the turn ends, so
+ * `activeRequests` is 0 during most of a turn.
*/
liveScopes?: () => ReadonlySet<string>
}
@@ -1980,7 +1982,9 @@ function probeSecondaryLiveness(entry: Secondary): void {
// Counted RPCs alone under-report in-flight work: prompt.submit returns
// before the turn ends, so a foreground turn mid tool call shows
// activeRequests 0. The registry's live-scope hook supplies the turn.
- const turnInFlight = g.config?.liveScopes?.().has(entry.scope) ? 1 : 0
+ const liveScopes = g.config?.liveScopes?.()
+ const turnInFlight =
+ liveScopes && (liveScopes.has(entry.scope) || (!entry.connectionId && liveScopes.has(entry.profile))) ? 1 : 0
const decision = decideLivenessForceClose({
workingSessionCount: entry.activeRequests + turnInFlight,