Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): refresh provider session lastSeenAt on turn and task activity #5524

Closedkraptor23(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/5524/headopened View on GitHub

What

Refreshes provider_session_runtime.last_seen_at when the adapter emits turn or background-task lifecycle events, so the inactivity reaper measures idleness from the last completed work instead of the last explicit sendTurn.

Addresses the staleness half of #4198.

Why

last_seen_at is only written by startSession / recoverSessionForThread / sendTurn / stopSession. Turns delivered without a sendTurn call (queued follow-ups, plan-sourced turns) and background tasks never refresh it. Observed on 0.0.32-nightly.20260805.1006: one thread was reaped 6 times in ~12 hours, three of those 105 s - 4 m 53 s after a turn had actually completed — the reaper was measuring idleness from a last_seen_at several turns stale, exactly what the reporters in #4198 back-computed from their logs.

How

  • ProviderSessionDirectory.touch(threadId) updates only last_seen_at for an existing binding via a new repository touchByThreadId (UPDATE ... WHERE thread_id). It never creates rows, and preserves every other column.
  • ProviderService.processRuntimeEvent touches the binding on turn.started/completed/aborted and task.started/progress/completed, after event fan-out.
  • Touches are throttled to one per thread per 60 s (in-memory map, cleared on stopSession) because this runs on the event drain fiber and task.progress can fire many times per second in multi-agent sessions; reaper thresholds are minutes, so minute-level granularity is sufficient. The gate timestamp is set before the write so a failing database is not retried per event.
  • Touch failures log provider.session.activity-touch-failed and never disturb fan-out.

This deliberately does not add a busy-guard for in-flight background tasks (discussed in #4198) — that is a separate lifecycle change; this PR only fixes the idle clock.

Tests

  • ProviderService.test.ts: every activity event type refreshes the binding's lastSeenAt; content.delta does not; a second boundary inside the throttle window does not write again.
  • ProviderSessionDirectory.test.ts: touch refreshes lastSeenAt and leaves all other fields unchanged; touch on an unknown thread creates no row.
  • vp test run on the five touched test files (97 tests) and tsgo --noEmit pass.

Prior art and non-goals

This addresses the liveness-staleness half of #4198. Back-computing the reaper's logged idleDurationMs against turn timestamps shows every incident reported there is an event-emitting case this PR covers: ordinary provider-started turns that never refreshed last_seen_at (NoorChasib's table — turns at 06:08 and 06:17 with last_seen_at stuck at 05:49), and background tasks killed seconds after their foreground turn settled (Mooned8, Zeus-Deus — task.started/turn.completed both now touch the binding).

Earlier attempts in this area (#4199, #3856, #4994) were closed for orchestration-V2 roadmap reasons rather than on the mechanism, and #2829 is still open — this PR is deliberately the smallest reviewable slice of that idea: one new directory method, one call site on the existing event drain, throttled, with no lifecycle behavior changes.

Non-goals, intentionally out of scope here:

  • a busy-guard exempting sessions with outstanding-but-quiet background tasks (the remaining half of #4198) — planned as a small follow-up building on the existing backgroundLiveness thread-shell state;
  • post-reap result re-delivery / respawn-on-completion (#4265);
  • reaper convergence for wedged threads (#4723, #4584) — only sensible after this PR makes lastSeenAt trustworthy.

Written by Claude Fable 5 via Claude Code.


Note

Medium Risk Changes when sessions are considered idle for reaping (core provider lifecycle), but scope is narrow—targeted DB updates with throttling and non-blocking failure handling.

Overview Provider session lastSeenAt now advances when adapters emit turn or background-task lifecycle events, not only on startSession, sendTurn, or stopSession. That aligns the inactivity reaper with last completed work (queued turns, plan-driven work, long-running tasks) instead of the last explicit user send.

A touch path updates only last_seen_at via repository touchByThreadId (UPDATE … WHERE thread_id); it never inserts rows and leaves other binding fields unchanged. ProviderService.processRuntimeEvent calls it after fan-out for SESSION_ACTIVITY_EVENT_TYPES (turn.*, task.*), with a 60s per-thread throttle and throttle state cleared on stopSession. Streaming events like content.delta do not touch; touch failures are logged and do not affect event delivery.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 0fadabd37e6508ab6685a04cf4a0d103e5dad39b. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Refresh provider session lastSeenAt on turn and task activity events

  • Adds a touch method to ProviderSessionDirectory and ProviderSessionDirectoryShape that updates only last_seen_at for an existing binding, with no-op behavior for unknown threads.
  • Extends ProviderSessionRuntimeRepository with touchByThreadId, which performs a targeted UPDATE on last_seen_at without affecting other fields.
  • In makeProviderService, emitting events from the new SESSION_ACTIVITY_EVENT_TYPES set (turn.started, turn.completed, turn.aborted, task.*) triggers directory.touch, throttled to at most once per 60 seconds per thread. The throttle map entry is cleared on stopSession.
  • Behavioral Change: provider session lastSeenAt now updates during active turns and tasks, where previously it only updated on session creation or explicit upsert.
Macroscope summarized 0fadabd.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      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 Plus

      Run ID: b02ac06f-8f1a-4af5-ae12-8301f9034df2

      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

      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.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. One finding: the new activity-touch warning log serializes the raw cause into the log payload instead of a normalized error tag.

      Posted via Macroscope — Effect Service Conventions
    4. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      This PR changes session lifecycle behavior by updating lastSeenAt timestamps based on turn/task events, affecting how the inactivity reaper determines session idleness. While well-tested and throttled, this runtime behavior change to session management warrants human review.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. Note

      This comment is posted by Julius' dot

      A maintainer scope decision is needed here. Julius closed #4199 in favor of V2, asking for a port after #2829 lands. This PR still changes V1 ProviderService, while narrowing the fix to stale activity timestamps.

      Do maintainers want this interim V1 fix, or should the author port it to V2? Leaving this open pending that decision.

    6. Note

      🤖 GPT-6 Astra responding on behalf of Theo

      We're closing this as part of a cleanup of stale PRs. Thank you for the time and care you put into this contribution.

      A local merge check found conflicts in 7 files against the current target branch. The latest implementation commit is from 2026-08-06, and there has been no contribution update in over a week.

      At this point, regenerating a focused patch against the current intended base would be easier to review and maintain than updating this PR. We appreciate the work and will keep this thread as a reference. If you revisit it, please check the current direction with us and link the fresh PR back here.

Sign in to join this conversation.