t3code icon indicating copy to clipboard operation
t3code copied to clipboard

fix(server): keep provider sessions alive during background agent work

Open ChamaruAmasara opened this issue 4 months ago • 3 comments

Closes #4198

What Changed

  • Add a targeted touchLastSeen to the provider session runtime repository and session directory: a lightweight update that bumps only last_seen_at, and only for non-stopped rows (so a reaped session is never resurrected).
  • In ProviderService, refresh a session's last_seen_at from runtime activity: processRuntimeEvent now calls touchLastSeen when 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); a ProviderService test 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 touches lastSeenAt and that a burst is throttled (verified it fails without the fix).
  • vp test run apps/server/src/provider/Layers/ProviderSessionDirectory.test.ts — touchLastSeen bumps a live binding; leaves a stopped binding untouched.
  • All affected provider test files pass (83 tests).
  • End-to-end: with a live server and a running background workflow, last_seen_at kept advancing from task.progress activity 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 touchLastSeen through persistence (UPDATE last_seen_at only, skipping stopped rows), ProviderSessionDirectory, and wiring in ProviderService.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 leaving last_seen_at stale.

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_at during background agent activity

  • Adds touchLastSeen(threadId) to ProviderSessionDirectory and its underlying repository, issuing a targeted SQL UPDATE on last_seen_at that skips rows with status = 'stopped'.
  • ProviderService calls touchLastSeen on each ProviderRuntimeEvent, throttled to at most one successful write per thread per 60s (configurable via runtimeActivityTouchThrottleMs).
  • Failed writes are silently ignored and do not arm the throttle, so the next event will retry immediately.

Macroscope summarized faca14e.

ChamaruAmasara avatar Jul 20 '26 19:07 ChamaruAmasara

[!IMPORTANT]

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a03e75b1-3f47-42c2-bb66-0eae85e1edc0

You can disable this status message by setting the reviews.review_status to false in 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 20 '26 19:07 coderabbitai[bot]

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.

macroscopeapp[bot] avatar Jul 20 '26 19:07 macroscopeapp[bot]

commenting for exposure. would be great if this could be prioritized

NoorChasib avatar Jul 27 '26 04:07 NoorChasib

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.

juliusmarminge avatar Jul 30 '26 15:07 juliusmarminge