fix(server): keep provider sessions alive during background agent work #4199
ClosedChamaruAmasara(GitHub profile, opens in a new tab)wants to merge 5 commits into
main fromrefs/pull/4199/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/provider/Layers/ProviderService.ts line 315OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -282,2 +295,21 @@282295 });283296297+ // Keep a live session's inactivity clock fresh from runtime activity. This298+ // matters for work that runs *after* the foreground turn settles — a299+ // background dynamic workflow / subagents keep emitting runtime events (e.g.300+ // `task.progress`) while the adapter session has already gone `ready` with no301+ // active turn. Without this, the session reaper sees a stale `lastSeenAt` and302+ // tears the session down mid-workflow. Throttled per thread so a burst of303+ // events is at most one lightweight `last_seen_at` write per window.304+ const refreshLastSeenForActivity = (event: ProviderRuntimeEvent): Effect.Effect<void> =>305+ Effect.gen(function* () {306+ const threadId = event.threadId;307+ if (threadId === undefined) return;308+ const now = yield* Clock.currentTimeMillis;309+ const previous = lastSeenTouchByThread.get(threadId);310+ if (previous !== undefined && now - previous < lastSeenTouchMs) return;311+ lastSeenTouchByThread.set(threadId, now);312+ // Best-effort: a failed touch must never break event processing. The row313+ // may be absent/stopped (touch is a no-op) or the write may transiently314+ // fail; the next event refreshes it.315+ yield* directory.touchLastSeen(threadId).pipe(Effect.catchCause(() => Effect.void));apps/server/src/provider/Layers/ProviderService.ts line 337Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -293,4 +327,6 @@293327 increment(providerRuntimeEventsTotal, {294328 provider: canonicalEvent.provider,295329 eventType: canonicalEvent.type,296- }).pipe(Effect.andThen(publishRuntimeEvent(canonicalEvent))),330+ }).pipe(331+ Effect.andThen(refreshLastSeenForActivity(canonicalEvent)),332+ Effect.andThen(publishRuntimeEvent(canonicalEvent)),
- @@ -213,3 +219,10 @@213219 const registry = yield* ProviderAdapterRegistry.ProviderAdapterRegistry;214220 const directory = yield* ProviderSessionDirectory.ProviderSessionDirectory;215221 const runtimeEventPubSub = yield* PubSub.unbounded<ProviderRuntimeEvent>();222+ // Throttle `lastSeenAt` refreshes keyed by thread. A running dynamic workflow223+ // emits many runtime events per second (`task.progress`, token-usage updates,224+ // …); we only need to bump the inactivity clock often enough that the session225+ // reaper never mistakes an active background session for an idle one. One226+ // write per thread per window is plenty and keeps the DB churn negligible.227+ const lastSeenTouchMs = options?.runtimeActivityTouchThrottleMs ?? 60_000;228+ const lastSeenTouchByThread = new Map<ThreadId, number>();
juliusmarminge closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
a03e75b1-3f47-42c2-bb66-0eae85e1edc0You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share