fix(server): keep provider sessions alive during background agent work
Closes #4198
What Changed
- Add a targeted
touchLastSeento the provider session runtime repository and session directory: a lightweight update that bumps onlylast_seen_at, and only for non-stoppedrows (so a reaped session is never resurrected). - In
ProviderService, refresh a session'slast_seen_atfrom runtime activity:processRuntimeEventnow callstouchLastSeenwhen the adapter emits runtime events, throttled per thread (default 60s) so an event burst is at most one lightweight write per window. The touch is best-effort — a failed write never breaks event processing. - Tests: persistence/directory tests for
touchLastSeen(bumps a live row; leaves a stopped row untouched); aProviderServicetest proving a runtime event triggers a touch and that a rapid burst is throttled.
Why
ProviderSessionReaper stops any provider session after 30 min of last_seen_at inactivity. Background agent work — dynamic workflows and subagents — keeps running after the foreground turn settles, but at that point the adapter session has gone ready with no active turn, and nothing refreshes last_seen_at. So a long-running background workflow with no foreground turn is torn down mid-run, and the in-flight background orchestration is lost.
Runtime events are the reliable signal for background activity: while a workflow runs, the adapter keeps emitting events (e.g. task.progress from subagents) even with no active foreground turn. Refreshing last_seen_at from these events keeps the session warm exactly when the reaper would otherwise reap it. Throttling keeps DB churn negligible (one write per thread per window instead of one per event).
Validation
vp test run apps/server/src/provider/Layers/ProviderService.test.ts— includes a new test that a runtime event toucheslastSeenAtand that a burst is throttled (verified it fails without the fix).vp test run apps/server/src/provider/Layers/ProviderSessionDirectory.test.ts—touchLastSeenbumps a live binding; leaves astoppedbinding untouched.- All affected provider test files pass (83 tests).
- End-to-end: with a live server and a running background workflow,
last_seen_atkept advancing fromtask.progressactivity and the reaper did not reap the session past its idle threshold. - Typecheck: 0 errors. Lint: clean.
UI Changes
None.
Checklist
- [x] This PR is small and focused
- [x] I explained what changed and why
- [x] No UI changes
[!NOTE] Medium Risk Touches session lifecycle and reaper inputs (
last_seen_at) on every runtime event path; behavior is best-effort and scoped to non-stopped rows, with tests for throttle and failure semantics.Overview Background runtime events now refresh session liveness so the idle reaper does not stop sessions that are still doing work after the foreground turn finishes (e.g. dynamic workflows emitting
task.progress).Adds
touchLastSeenthrough persistence (UPDATE last_seen_atonly, skippingstoppedrows),ProviderSessionDirectory, and wiring inProviderService.processRuntimeEvent: best-effort, per-thread throttled (default 60s, overridable in tests). The throttle arms only after a successful write, so a failed touch can retry on the next event instead of leavinglast_seen_atstale.Tests cover directory behavior (live vs stopped), throttling, and retry-on-failure; adapter test doubles stub
touchLastSeen.Reviewed by Cursor Bugbot for commit faca14eaff410ae563d06e1ff64ab7fbb7643cb1. Bugbot is set up for automated code reviews on this repo. Configure here.
[!NOTE]
Keep provider sessions alive by touching
last_seen_atduring background agent activity
- Adds
touchLastSeen(threadId)toProviderSessionDirectoryand its underlying repository, issuing a targeted SQLUPDATEonlast_seen_atthat skips rows withstatus = 'stopped'.ProviderServicecallstouchLastSeenon eachProviderRuntimeEvent, throttled to at most one successful write per thread per 60s (configurable viaruntimeActivityTouchThrottleMs).- Failed writes are silently ignored and do not arm the throttle, so the next event will retry immediately.
Macroscope summarized faca14e.
[!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:
- [ ] 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
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.
Comment @coderabbitai help to get the list of available commands.
Approvability
Verdict: Needs human review
1 blocking correctness issue found. This PR introduces new runtime behavior for session lifecycle management (keeping sessions alive during background work). There is an unresolved review comment about a potential memory leak in the throttle map that should be addressed before merge.
You can customize Macroscope's approvability policy. Learn more.
commenting for exposure. would be great if this could be prioritized
Closing in favor of #2829 (orchestration V2).
#2829 deletes the V1 orchestration layer this PR builds on — apps/server/src/orchestration/**, provider/Layers/*Adapter.ts and provider/Services/** are removed and replaced by apps/server/src/orchestration-v2/**, with the IPC surface renamed to ORCHESTRATION_V2_WS_METHODS. The files this PR touches either no longer exist or are rewritten, so it can't be rebased — it would need reimplementing against the V2 adapters.
This is not a judgement on the change itself. Several of these are real gaps we still want fixed; the base just moved out from under them.
Once #2829 merges, please rebase onto main, port the change to the V2 equivalent, and reopen (or open a fresh PR). Ping me and I'll prioritise the review.