Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

44.7 KB
Raw Blame
import { DatabaseManager } from './DatabaseManager.js';
import { logger } from '../../utils/logger.js';
import { redactForLog } from '../../utils/redaction.js';
import type { ActiveSession, PendingMessage, PendingMessageWithId, ObservationData } from '../worker-types.js';
import { SessionMessageBuffer } from './SessionMessageBuffer.js';
import { getSdkProcessForSession, ensureSdkProcessExit, getParkedSlotWaiterCount } from '../../supervisor/process-registry.js';
import { getSupervisor } from '../../supervisor/index.js';
import { telemetryBuffer } from '../telemetry/buffer.js';
import { deliverSessionWrapup, type TelegramWrapupFormatter } from '../integrations/TelegramWrapupNotifier.js';
import { MAX_LLM_TIMEOUT_MS, resolveLlmTimeoutMs } from './retry.js';
import { canCmemGatewayServe } from '../../shared/cmem-gateway.js';
import { planUnattendedGatewayResume, unattendedGatewayResumesSpent } from './session/response-pacer.js';
import { CAPTURE_DISABLED_ABORT_REASON, PROVIDER_SWITCH_BOUNDARY_ABORT_REASON } from './session/abort-reason.js';
import { isSimpleTierMessage } from './session/tier-routing.js';
import { claudeStandInStillServes } from '../../shared/claude-fallback-readiness.js';
import { SettingsDefaultsManager } from '../../shared/SettingsDefaultsManager.js';
import { isObserverCaptureEnabled } from '../../shared/observer-capture.js';
import { USER_SETTINGS_PATH } from '../../shared/paths.js';

export const SESSION_END_WRAPUP_GRACE_MS = 5_000;

export interface TransportResumeClock {
  setTimeout(callback: () => void, delayMs: number): ReturnType<typeof setTimeout>;
  clearTimeout(timer: ReturnType<typeof setTimeout>): void;
}

const defaultTransportResumeClock: TransportResumeClock = {
  setTimeout: (callback, delayMs) => setTimeout(callback, delayMs),
  clearTimeout: timer => clearTimeout(timer),
};

// The nominal interval stops at the largest supported request timeout.
// Jitter remains after that cap so sessions that failed together do not
// synchronize their probes during a shared provider outage.
export const MAX_TRANSPORT_RESUME_BASE_DELAY_MS = MAX_LLM_TIMEOUT_MS;

export function transportResumeDelayMs(consecutivePauses: number): number {
  const nominalMs = Math.min(
    resolveLlmTimeoutMs() * Math.pow(2, Math.max(0, consecutivePauses - 1)),
    MAX_TRANSPORT_RESUME_BASE_DELAY_MS,
  );
  return Math.floor(nominalMs + Math.random() * nominalMs / 4);
}

type TransportResumeState = { pauses: number; timer?: ReturnType<typeof setTimeout> };
type ReadyTransportResume = {
  sessionDbId: number;
  session: ActiveSession;
  state: TransportResumeState;
};

export class SessionManager {
  private dbManager: DatabaseManager;
  private sessions: Map<number, ActiveSession> = new Map();
  private onPendingMutate?: () => void;
  private telegramWrapupFormatter: TelegramWrapupFormatter | null = null;
  private readonly buffer = new SessionMessageBuffer(() => this.onPendingMutate?.());
  private generatorStarter: ((sessionDbId: number, source: string) => void | Promise<void>) | null = null;
  private transportResumes = new Map<number, TransportResumeState>();
  private transportResumeReady: ReadyTransportResume[] = [];
  private activeTransportResume: ReadyTransportResume | null = null;
  private deletingSessions = new Set<number>();
  private transportResumeDispositionEpochs = new WeakMap<ActiveSession, number>();
  /** Sessions that already used their one summarize rescue (#3419). */
  private readonly summarizeRescues = new Set<number>();
  /** Telegram wrap-ups started by deliverSessionWrapupInBackground and not yet settled. */
  private sessionWrapupsInFlight = 0;

  constructor(
    dbManager: DatabaseManager,
    private readonly transportResumeClock: TransportResumeClock = defaultTransportResumeClock,
  ) {
    this.dbManager = dbManager;
  }

  setOnPendingMutate(cb: () => void): void {
    this.onPendingMutate = cb;
  }

  setGeneratorStarter(starter: (sessionDbId: number, source: string) => void | Promise<void>): void {
    this.generatorStarter = starter;
  }

  /**
   * Park the last-sent batch when its PaidSendBudget is spent: it is never
   * resent, by any path, and stays visible in the buffer (getParkedMessages)
   * until the session ends, while newer work keeps flowing. Returns the ids
   * parked; none when the budget has a send left or the batch was stored.
   */
  parkBatchOnSpentPaidSendBudget(session: ActiveSession): number[] {
    const budget = session.paidSendBudget;
    if (!budget || budget.hasRemainingPaidSend()) return [];
    session.paidSendBudget = undefined;
    const parkedMessageIds = this.buffer.park(session.sessionDbId, [...budget.batchMessageIds], budget.clientAttemptId);
    if (parkedMessageIds.length > 0) {
      logger.error('SESSION', 'Batch parked: its paid-send budget is spent, so it is not resent', {
        sessionId: session.sessionDbId,
        parkedMessageIds,
        paidSendsSpent: budget.spentPaidSends,
        maxPaidSends: budget.maxPaidSends,
        clientAttemptId: budget.clientAttemptId,
        pendingCount: this.buffer.getPendingCount(session.sessionDbId),
      });
    }
    return parkedMessageIds;
  }

  /** Resume preserved transport work without requiring another hook from the IDE. */
  scheduleTransportResume(sessionDbId: number): void {
    const session = this.sessions.get(sessionDbId);
    if (!session || this.deletingSessions.has(sessionDbId)) return;
    // A resume resends the batch; one whose budget is spent is parked instead,
    // and only work behind it, if any, is resumed.
    this.parkBatchOnSpentPaidSendBudget(session);
    if (this.buffer.getPendingCount(sessionDbId) === 0) return;
    if (!this.generatorStarter) {
      logger.error('SESSION', 'Cannot schedule transport resume: generator starter is not attached', { sessionId: sessionDbId });
      return;
    }

    const prior = this.transportResumes.get(sessionDbId);
    if (prior?.timer) this.transportResumeClock.clearTimeout(prior.timer);
    const pauses = (prior?.pauses ?? 0) + 1;
    // Each unattended resume on the cmem gateway spends plan tokens; this one
    // shares its budget with rate-limit and fallback resumes. Spent, the pause
    // keeps its backoff position without a timer, and the next hook-driven
    // start still drains the buffer.
    if (!planUnattendedGatewayResume(session, 'transport-resume', canCmemGatewayServe(session.platformSource)).resume) {
      this.transportResumes.set(sessionDbId, { pauses });
      return;
    }
    const delayMs = transportResumeDelayMs(pauses);
    const state: TransportResumeState = { pauses };
    const timer = this.transportResumeClock.setTimeout(() => {
      if (this.transportResumes.get(sessionDbId) !== state || state.timer !== timer) return;
      state.timer = undefined;
      const ready = { sessionDbId, session, state };
      if (!this.canResumeTransport(ready)) {
        this.transportResumes.delete(sessionDbId);
        return;
      }
      this.transportResumeReady.push(ready);
      this.pumpTransportResumes();
    }, delayMs);
    timer.unref?.();
    state.timer = timer;
    this.transportResumes.set(sessionDbId, state);
    logger.warn('SESSION', 'Transport pause: scheduled buffered-work resume', { sessionId: sessionDbId, pauses, delayMs });
  }

  private canResumeTransport(ready: ReadyTransportResume): boolean {
    return this.sessions.get(ready.sessionDbId) === ready.session
      && !this.deletingSessions.has(ready.sessionDbId)
      && this.transportResumes.get(ready.sessionDbId) === ready.state
      && this.buffer.getPendingCount(ready.sessionDbId) > 0;
  }

  /** Start one unattended probe at a time; ready sessions retain FIFO order. */
  private pumpTransportResumes(): void {
    if (this.activeTransportResume) return;
    while (this.transportResumeReady.length > 0) {
      const ready = this.transportResumeReady.shift()!;
      if (!this.canResumeTransport(ready)) continue;
      // A hook already restarted this session. Do not occupy the automatic
      // probe slot, but watch its completion. An explicit pause decision
      // clears the state, so it cannot rearm after the generator settles.
      if (ready.session.generatorPromise) {
        this.rearmAfterRunningGenerator(ready, ready.session.generatorPromise);
        continue;
      }
      this.activeTransportResume = ready;
      void Promise.resolve()
        .then(() => {
          if (!this.canResumeTransport(ready) || this.activeTransportResume !== ready) return;
          return this.generatorStarter?.(ready.sessionDbId, 'transport-resume');
        })
        .catch(error => {
          logger.error('SESSION', 'Failed to resume observer after transport pause', { sessionId: ready.sessionDbId },
            error instanceof Error ? error : new Error(String(error)));
        })
        .then(() => {
          if (this.activeTransportResume !== ready) return;
          const generator = ready.session.generatorPromise;
          if (!generator) {
            // Quota and overflow admission can decline without a generator exit.
            this.finishTransportResume(ready);
            return;
          }
          void generator.then(
            () => this.finishTransportResume(ready),
            () => this.finishTransportResume(ready),
          );
        });
      return;
    }
  }

  private rearmAfterRunningGenerator(ready: ReadyTransportResume, running: Promise<void>): void {
    const onSettled = () => {
      if (!this.canResumeTransport(ready) || ready.state.timer) return;
      const current = ready.session.generatorPromise;
      if (current && current !== running) {
        this.rearmAfterRunningGenerator(ready, current);
        return;
      }
      // A failed finalizer can leave its already-settled promise in the
      // session. It no longer owns the generator slot, so clear that stale
      // reference before scheduling the next attempt.
      if (current === running) ready.session.generatorPromise = null;
      try {
        this.scheduleTransportResume(ready.sessionDbId);
      } catch (error) {
        logger.error('SESSION', 'Failed to rearm transport resume after hook generator', { sessionId: ready.sessionDbId },
          error instanceof Error ? error : new Error(String(error)));
      }
    };
    void running.then(onSettled, onSettled);
  }

  private finishTransportResume(ready: ReadyTransportResume): void {
    if (this.activeTransportResume !== ready) return;
    this.activeTransportResume = null;
    // A declined start has no exit to schedule its next attempt. A completed
    // generator may have scheduled a new state; the identity check excludes it.
    try {
      if (this.canResumeTransport(ready) && !ready.state.timer
        && !ready.session.generatorPromise) {
        this.scheduleTransportResume(ready.sessionDbId);
      }
    } catch (error) {
      logger.error('SESSION', 'Failed to schedule another transport resume', { sessionId: ready.sessionDbId },
        error instanceof Error ? error : new Error(String(error)));
    } finally {
      this.pumpTransportResumes();
    }
  }

  clearTransportResume(sessionDbId: number): void {
    const session = this.sessions.get(sessionDbId);
    if (session) {
      this.transportResumeDispositionEpochs.set(session,
        (this.transportResumeDispositionEpochs.get(session) ?? 0) + 1);
    }
    const state = this.transportResumes.get(sessionDbId);
    if (state?.timer) this.transportResumeClock.clearTimeout(state.timer);
    this.transportResumes.delete(sessionDbId);
    if (this.activeTransportResume?.sessionDbId === sessionDbId) {
      this.activeTransportResume = null;
      this.pumpTransportResumes();
    }
  }

  setTelegramWrapupFormatter(formatter: TelegramWrapupFormatter): void {
    this.telegramWrapupFormatter = formatter;
  }

  initializeSession(
    sessionDbId: number,
    currentUserPrompt?: string,
    promptNumber?: number,
    currentProject?: string,
  ): ActiveSession {
    const suppliedProject = currentProject && currentProject !== 'unknown' ? currentProject : undefined;

    logger.debug('SESSION', 'initializeSession called', {
      sessionDbId,
      promptNumber,
      has_currentUserPrompt: !!currentUserPrompt
    });

    let session = this.sessions.get(sessionDbId);
    if (session) {
      logger.debug('SESSION', 'Returning cached session', {
        sessionDbId,
        contentSessionId: session.contentSessionId,
        lastPromptNumber: session.lastPromptNumber
      });

      const dbSession = this.dbManager.getSessionById(sessionDbId);
      if (dbSession.project && dbSession.project !== session.project && !suppliedProject) {
        logger.debug('SESSION', 'Updating project from database', {
          sessionDbId,
          oldProject: session.project,
          newProject: dbSession.project
        });
        session.project = dbSession.project;
      }
      if (suppliedProject) {
        session.project = suppliedProject;
      }
      if (dbSession.platform_source && dbSession.platform_source !== session.platformSource) {
        session.platformSource = dbSession.platform_source;
      }
      if (dbSession.observed_model && dbSession.observed_model !== session.observedModel) {
        session.observedModel = dbSession.observed_model;
      }
      if (dbSession.observed_billing && dbSession.observed_billing !== session.observedBilling) {
        session.observedBilling = dbSession.observed_billing;
      }

      if (currentUserPrompt) {
        logger.debug('SESSION', 'Updating userPrompt for continuation', {
          sessionDbId,
          promptNumber,
          oldPrompt: session.userPrompt?.substring(0, 80) ?? '',
          newPrompt: currentUserPrompt.substring(0, 80)
        });
        session.userPrompt = currentUserPrompt;
        session.lastPromptNumber = promptNumber || session.lastPromptNumber;
      } else {
        logger.debug('SESSION', 'No currentUserPrompt provided for existing session', {
          sessionDbId,
          promptNumber,
          usingCachedPrompt: session.userPrompt?.substring(0, 80) ?? ''
        });
      }
      return session;
    }

    const dbSession = this.dbManager.getSessionById(sessionDbId);

    logger.debug('SESSION', 'Fetched session from database', {
      sessionDbId,
      content_session_id: dbSession.content_session_id,
      memory_session_id: dbSession.memory_session_id
    });

    if (dbSession.memory_session_id) {
      logger.warn('SESSION', `Discarding stale memory_session_id from previous worker instance (Issue #817)`, {
        sessionDbId,
        staleMemorySessionId: dbSession.memory_session_id,
        reason: 'SDK context lost on worker restart - will capture new ID'
      });
    }

    const latestPromptText = currentUserPrompt
      ? null
      : this.dbManager.getSessionStore().getLatestPromptTextFromUserPrompts(
          dbSession.content_session_id,
          sessionDbId,
        );
    const userPrompt = currentUserPrompt || latestPromptText || dbSession.user_prompt;

    if (!currentUserPrompt) {
      logger.debug('SESSION', latestPromptText
        ? 'No currentUserPrompt provided for new session, using latest user_prompts'
        : 'No currentUserPrompt provided for new session, using database', {
        sessionDbId,
        promptNumber,
        latestPrompt: latestPromptText?.substring(0, 80) ?? '',
        dbPrompt: dbSession.user_prompt?.substring(0, 80) ?? ''
      });
    } else {
      logger.debug('SESSION', 'Initializing session with fresh userPrompt', {
        sessionDbId,
        promptNumber,
        userPrompt: currentUserPrompt.substring(0, 80)
      });
    }

    session = {
      sessionDbId,
      contentSessionId: dbSession.content_session_id,
      memorySessionId: null,  // Always start fresh - SDK will capture new ID
      project: suppliedProject || dbSession.project,
      platformSource: dbSession.platform_source,
      observedModel: dbSession.observed_model ?? undefined,
      observedBilling: dbSession.observed_billing ?? undefined,
      userPrompt,
      abortController: new AbortController(),
      generatorPromise: null,
      lastPromptNumber: promptNumber || this.dbManager.getSessionStore().getPromptNumberFromUserPrompts(dbSession.content_session_id, sessionDbId),
      startTime: Date.now(),
      cumulativeInputTokens: 0,
      cumulativeOutputTokens: 0,
      earliestPendingTimestamp: null,
      claimedMessageIds: [],
      conversationHistory: [],  // Initialize empty - will be populated by agents
      currentProvider: null,  // Will be set when generator starts
      consecutiveRestarts: 0,
      consecutiveInvalidOutputs: 0,
      consecutiveContextOverflows: 0,
      lastGeneratorActivity: Date.now(),  // Initialize for stale detection (Issue #1099)
      pendingAgentId: null,   // Subagent identity carried from the most recent claimed message
      pendingAgentType: null,
      pausedReason: null
    };

    logger.debug('SESSION', 'Creating new session object (memorySessionId cleared to prevent stale resume)', {
      sessionDbId,
      contentSessionId: dbSession.content_session_id,
      dbMemorySessionId: dbSession.memory_session_id || '(none in DB)',
      memorySessionId: '(cleared - will capture fresh from SDK)',
      lastPromptNumber: promptNumber || this.dbManager.getSessionStore().getPromptNumberFromUserPrompts(dbSession.content_session_id, sessionDbId)
    });

    this.sessions.set(sessionDbId, session);

    logger.info('SESSION', 'Session initialized', {
      sessionId: sessionDbId,
      project: session.project,
      contentSessionId: session.contentSessionId,
      queueDepth: 0,
      hasGenerator: false
    });

    return session;
  }

  getSession(sessionDbId: number): ActiveSession | undefined {
    return this.sessions.get(sessionDbId);
  }

  isSessionDeleting(sessionDbId: number): boolean {
    return this.deletingSessions.has(sessionDbId);
  }

  private deliverSessionWrapupInBackground(sessionDbId: number): void {
    const formatSummary = this.telegramWrapupFormatter;
    if (!formatSummary) {
      logger.warn('TELEGRAM', 'Telegram session wrap-up formatter is unavailable', {
        sessionId: sessionDbId,
      });
      return;
    }

    // Read before the count goes up: it throws while the database is not open,
    // and a count that never comes down would read as busy forever.
    const sessionStore = this.dbManager.getSessionStore();
    this.sessionWrapupsInFlight++;
    void deliverSessionWrapup({
      sessionStore,
      sessionDbId,
      formatSummary,
    }).catch((error: unknown) => {
      logger.warn('TELEGRAM', 'Failed to deliver Telegram session wrap-up from SessionManager', {
        sessionId: sessionDbId,
      }, error instanceof Error ? error : new Error(String(error)));
    }).finally(() => {
      this.sessionWrapupsInFlight--;
    });
  }

  /**
   * Telegram session wrap-ups being written and sent now: each one runs after
   * its session has left the session map (an LLM call, then a Telegram post),
   * and nothing asks for it again if the worker stops first. The auto-updater
   * counts them as busy (worker-service.ts runningBackgroundJobNames).
   */
  getSessionWrapupsInFlight(): number {
    return this.sessionWrapupsInFlight;
  }

  private takeRequestedSessionWrapup(session: ActiveSession): boolean {
    if (session.telegramWrapupTimer != null) {
      clearTimeout(session.telegramWrapupTimer);
      session.telegramWrapupTimer = null;
    }

    return session.telegramWrapupRequestedAt != null;
  }

  /** SessionEnd is the sole producer of this marker; Stop only queues a summary. */
  async requestSessionWrapup(sessionDbId: number): Promise<void> {
    const session = this.getSession(sessionDbId);
    if (!session) {
      this.deliverSessionWrapupInBackground(sessionDbId);
      return;
    }

    session.telegramWrapupRequestedAt = Date.now();
    if (session.telegramWrapupTimer != null) {
      clearTimeout(session.telegramWrapupTimer);
    }
    session.telegramWrapupTimer = setTimeout(() => {
      session.telegramWrapupTimer = null;
      this.deliverSessionWrapupInBackground(sessionDbId);
    }, SESSION_END_WRAPUP_GRACE_MS);
    session.telegramWrapupTimer.unref?.();
  }

  /** Called after a summary write; the SessionEnd marker preserves Stop-only silence. */
  deliverRequestedSessionWrapup(sessionDbId: number): void {
    const session = this.getSession(sessionDbId);
    if (session?.telegramWrapupRequestedAt == null) {
      return;
    }
    this.deliverSessionWrapupInBackground(sessionDbId);
  }

  /**
   * Synchronous on purpose: when this returns the message is in the buffer,
   * so ingest can record the hook-spool hand-off before any await (and before
   * the generator kick) — see HookSpool.drain.
   */
  queueObservation(sessionDbId: number, data: ObservationData, enqueuedAtEpochMs?: number): number {
    let session = this.sessions.get(sessionDbId);
    if (!session) {
      session = this.initializeSession(sessionDbId);
    }

    const message: PendingMessage = {
      type: 'observation',
      tool_name: data.tool_name,
      tool_input: data.tool_input,
      tool_response: data.tool_response,
      prompt_number: data.prompt_number,
      cwd: data.cwd,
      agentId: data.agentId,
      agentType: data.agentType,
      toolUseId: data.toolUseId,
      // Bounded side references only; tool_input/tool_response stay text and
      // the durable identity lives in media_events, never this RAM buffer ID.
      ...(data.mediaRefs ? { mediaRefs: data.mediaRefs, mediaEventKey: data.mediaEventKey } : {}),
      ...(data.mediaFailures ? { mediaFailures: data.mediaFailures } : {}),
    };

    const messageId = this.buffer.enqueue(sessionDbId, message, enqueuedAtEpochMs);
    const queueDepth = this.buffer.getPendingCount(sessionDbId);
    const toolSummary = redactForLog(logger.formatTool(data.tool_name, data.tool_input));
    if (messageId === 0) {
      logger.debug('QUEUE', `SKIPPED_DUPLICATE_OR_LIMIT | sessionDbId=${sessionDbId} | type=observation | tool=${toolSummary} | toolUseId=${data.toolUseId ?? 'null'} | depth=${queueDepth}`, {
        sessionId: sessionDbId
      });
    } else {
      logger.info('QUEUE', `ENQUEUED | sessionDbId=${sessionDbId} | messageId=${messageId} | type=observation | tool=${toolSummary} | depth=${queueDepth}`, {
        sessionId: sessionDbId
      });
    }
    return messageId;
  }

  /** Synchronous on purpose — see queueObservation. */
  queueSummarize(
    sessionDbId: number,
    lastAssistantMessage?: string,
    promptNumber?: number,
    enqueuedAtEpochMs?: number,
    cwd?: string,
  ): number {
    let session = this.sessions.get(sessionDbId);
    if (!session) {
      session = this.initializeSession(sessionDbId);
    }

    const message: PendingMessage = {
      type: 'summarize',
      prompt_number: promptNumber ?? session.lastPromptNumber,
      last_assistant_message: lastAssistantMessage,
      // Carry the checkout like an observation does: the generator resolves
      // the host/project model from the message's cwd, and a summary-only turn
      // has no observation to inherit one from.
      ...(cwd ? { cwd } : {})
    };

    const messageId = this.buffer.enqueue(sessionDbId, message, enqueuedAtEpochMs);
    const queueDepth = this.buffer.getPendingCount(sessionDbId);
    if (messageId === 0) {
      logger.debug('QUEUE', `SKIPPED_DUPLICATE_OR_LIMIT | sessionDbId=${sessionDbId} | type=summarize | depth=${queueDepth}`, {
        sessionId: sessionDbId
      });
    } else {
      logger.info('QUEUE', `ENQUEUED | sessionDbId=${sessionDbId} | messageId=${messageId} | type=summarize | depth=${queueDepth}`, {
        sessionId: sessionDbId
      });
    }
    return messageId;
  }

  async clearPendingForSession(sessionDbId: number): Promise<number> {
    const session = this.sessions.get(sessionDbId);
    if (session) {
      // Every caller is a capture-off guard at a safe send boundary. Carry
      // that decision through provider exit: normal finalization would
      // dispose the receipts we are keeping below and admit old events again.
      session.abortReason = CAPTURE_DISABLED_ABORT_REASON;
      session.abortController.abort();
      session.claimedMessageIds = [];
      session.earliestPendingTimestamp = null;
    }
    return this.buffer.discardKeepingDedup(sessionDbId);
  }

  /** Discard a session's buffered work without permitting old tool IDs to re-enter. */
  discardBufferedWork(sessionDbId: number): number {
    const session = this.sessions.get(sessionDbId);
    if (session) {
      session.claimedMessageIds = [];
      session.earliestPendingTimestamp = null;
    }
    return this.buffer.discardKeepingDedup(sessionDbId);
  }

  async resetProcessingToPending(sessionDbId: number): Promise<number> {
    const session = this.sessions.get(sessionDbId);
    if (session) {
      // Released claims are unsent again and may expire before a new claim, so
      // their timestamp must not outlive them; the next claim recomputes it.
      session.claimedMessageIds = [];
      session.earliestPendingTimestamp = null;
    }
    return this.buffer.resetClaimed(sessionDbId);
  }

  async confirmClaimedMessages(sessionDbId: number): Promise<number> {
    const session = this.sessions.get(sessionDbId);
    const claimedIds = session?.claimedMessageIds ?? [];
    let confirmed = 0;
    for (const messageId of claimedIds) {
      confirmed += this.buffer.confirm(messageId);
    }
    if (session) {
      session.claimedMessageIds = [];
      session.earliestPendingTimestamp = null;
    }
    if (confirmed > 0) {
      const state = this.transportResumes.get(sessionDbId);
      if (state?.timer) this.transportResumeClock.clearTimeout(state.timer);
      if (state) {
        state.timer = undefined;
        state.pauses = 0;
      }
      // The generator can remain alive for its idle window after draining
      // queued work. Free the shared probe slot as soon as it is done sending.
      if (this.buffer.getPendingCount(sessionDbId) === 0) this.clearTransportResume(sessionDbId);
    }
    return confirmed;
  }

  getClaimedMessages(sessionDbId: number): PendingMessageWithId[] {
    const session = this.sessions.get(sessionDbId);
    const claimedIds = session?.claimedMessageIds ?? [];
    return this.buffer.getMessagesByIds(sessionDbId, claimedIds);
  }

  async deleteSession(sessionDbId: number): Promise<void> {
    const session = this.sessions.get(sessionDbId);
    if (!session || this.deletingSessions.has(sessionDbId)) {
      return;
    }
    const priorTransportPauses = this.transportResumes.get(sessionDbId)?.pauses;
    const priorAbortCategory = (session.abortReason ?? '').split(':')[0];
    let dispositionEpoch: number | undefined;
    // Fence delayed transport callbacks before the async teardown can yield.
    this.deletingSessions.add(sessionDbId);
    try {
      this.clearTransportResume(sessionDbId);
      dispositionEpoch = this.transportResumeDispositionEpochs.get(session);

      // Phase 2: emit this session's single observer_turn_rollup at session end,
      // while the session still exists. flushSession removes the bucket, so the
      // matching call in removeSessionImmediate (or a re-entry here) is a safe
      // no-op. Never throws — telemetry is fire-and-forget.
      telemetryBuffer.flushSession(sessionDbId, 'session_end');

      const sessionDuration = Date.now() - session.startTime;

      if (session.respawnTimer) {
        clearTimeout(session.respawnTimer);
        session.respawnTimer = undefined;
      }

      if (session.stallResumeTimer) {
        clearTimeout(session.stallResumeTimer);
        session.stallResumeTimer = undefined;
      }

      if (session.scheduledResumeTimer) {
        clearTimeout(session.scheduledResumeTimer);
        session.scheduledResumeTimer = undefined;
      }

      session.abortReason = 'shutdown';
      session.abortController.abort();

      if (session.generatorPromise) {
        const generatorDone = session.generatorPromise.catch(() => {
          logger.debug('SYSTEM', 'Generator already failed, cleaning up', { sessionId: session.sessionDbId });
        });
        const timeoutDone = new Promise<void>(resolve => {
          AbortSignal.timeout(30_000).addEventListener('abort', () => resolve(), { once: true });
        });
        await Promise.race([generatorDone, timeoutDone]).then(() => {}, () => {
          logger.warn('SESSION', 'Generator did not exit within 30s after abort, forcing cleanup (#1099)', { sessionDbId });
        });
      }

      const tracked = getSdkProcessForSession(sessionDbId);
      if (tracked && tracked.process.exitCode === null) {
        logger.debug('SESSION', `Waiting for subprocess PID ${tracked.pid} (pgid ${tracked.pgid}) to exit`, {
          sessionId: sessionDbId,
          pid: tracked.pid,
          pgid: tracked.pgid
        });
        await ensureSdkProcessExit(tracked, 5000);
      }

      try {
        await getSupervisor().getRegistry().reapSession(sessionDbId);
      } catch (error) {
        if (error instanceof Error) {
          logger.warn('SESSION', 'Supervisor reapSession failed (non-blocking)', {
            sessionId: sessionDbId
          }, error);
        } else {
          logger.warn('SESSION', 'Supervisor reapSession failed (non-blocking) with non-Error', {
            sessionId: sessionDbId
          }, new Error(String(error)));
        }
      }

      if (this.takeRequestedSessionWrapup(session)) {
        this.deliverSessionWrapupInBackground(sessionDbId);
      }
      this.clearTransportResume(sessionDbId);
      this.logParkedMessagesEndingWithSession(sessionDbId);
      this.disposeSessionBuffer(sessionDbId);
      this.summarizeRescues.delete(sessionDbId);
      this.sessions.delete(sessionDbId);
      logger.info('SESSION', 'Session deleted', {
        sessionId: sessionDbId,
        duration: `${(sessionDuration / 1000).toFixed(1)}s`,
        project: session.project
      });
    } finally {
      // A teardown failure leaves the session and its buffered work alive.
      // Release the deletion fence and restore only a pre-existing transport
      // pause; quota/auth pauses must continue to wait for their own trigger.
      const stillCurrent = this.sessions.get(sessionDbId) === session;
      const currentAbortCategory = (session.abortReason ?? '').split(':')[0];
      if (stillCurrent && session.abortReason === 'shutdown') session.abortReason = null;
      this.deletingSessions.delete(sessionDbId);
      if (stillCurrent && priorTransportPauses !== undefined
        && dispositionEpoch !== undefined
        && this.transportResumeDispositionEpochs.get(session) === dispositionEpoch
        && !['quota', 'auth', 'overflow', 'provider_switch'].includes(priorAbortCategory)
        && !['quota', 'auth', 'overflow', 'provider_switch'].includes(currentAbortCategory)
        && !this.transportResumes.has(sessionDbId) && this.buffer.getPendingCount(sessionDbId) > 0) {
        // scheduleTransportResume increments pauses; retain the original
        // backoff position rather than treating teardown as a new failure.
        this.transportResumes.set(sessionDbId, { pauses: Math.max(0, priorTransportPauses - 1) });
        try {
          this.scheduleTransportResume(sessionDbId);
        } catch (error) {
          this.transportResumes.delete(sessionDbId);
          logger.error('SESSION', 'Failed to restore transport resume after teardown error', { sessionId: sessionDbId },
            error instanceof Error ? error : new Error(String(error)));
        }
      }
    }
  }

  removeSessionImmediate(sessionDbId: number): void {
    const session = this.sessions.get(sessionDbId);
    if (!session) return;

    // Phase 2: same session-end rollup as deleteSession. Whichever teardown
    // path runs first flushes; flushSession removes the bucket so the second is
    // a no-op (guards against the deleteSession/removeSessionImmediate pair).
    telemetryBuffer.flushSession(sessionDbId, 'session_end');

    if (session.respawnTimer) {
      clearTimeout(session.respawnTimer);
      session.respawnTimer = undefined;
    }

    if (session.stallResumeTimer) {
      clearTimeout(session.stallResumeTimer);
      session.stallResumeTimer = undefined;
    }

    if (session.scheduledResumeTimer) {
      clearTimeout(session.scheduledResumeTimer);
      session.scheduledResumeTimer = undefined;
    }

    if (this.takeRequestedSessionWrapup(session)) {
      this.deliverSessionWrapupInBackground(sessionDbId);
    }

    this.clearTransportResume(sessionDbId);
    this.logParkedMessagesEndingWithSession(sessionDbId);
    this.disposeSessionBuffer(sessionDbId);
    this.summarizeRescues.delete(sessionDbId);
    this.sessions.delete(sessionDbId);
    logger.info('SESSION', 'Session removed from active sessions', {
      sessionId: sessionDbId,
      project: session.project
    });
  }

  /** Parked batches end with their session like the rest of the RAM buffer; say so rather than drop them silently. */
  private logParkedMessagesEndingWithSession(sessionDbId: number): void {
    const parkedMessages = this.buffer.getParkedMessages(sessionDbId);
    if (parkedMessages.length === 0) return;
    logger.warn('SESSION', 'Session ended with parked batches; they were never resent', {
      sessionId: sessionDbId,
      parkedMessageIds: parkedMessages.map(parked => parked.messageId),
      clientAttemptIds: [...new Set(parkedMessages.map(parked => parked.clientAttemptId))],
    });
  }

  private disposeSessionBuffer(sessionDbId: number): void {
    const pendingCount = this.buffer.getPendingCount(sessionDbId);
    if (pendingCount > 0) {
      logger.warn('SESSION', 'Discarding pending session messages', {
        sessionId: sessionDbId,
        pendingCount,
      });
    }
    this.buffer.dispose(sessionDbId);
  }

  async shutdownAll(): Promise<void> {
    const sessionIds = Array.from(this.sessions.keys());
    const pendingCountAtShutdownStart = sessionIds.reduce((total, id) => total + this.buffer.getPendingCount(id), 0);
    if (pendingCountAtShutdownStart > 0) {
      logger.warn('SESSION', 'Worker shutdown starting with pending session messages', {
        pendingCountAtShutdownStart,
      });
    }
    await Promise.all(sessionIds.map(id => this.deleteSession(id)));
  }

  getActiveSessionCount(): number {
    return this.sessions.size;
  }

  /**
   * True when any in-memory session saw message/generator activity at or
   * after the cutoff — the idle-exit monitor's session signal.
   *
   * Deliberately NOT a session count: a session that merely EXISTS is not
   * activity. A standing memory seat registered at boot (Grok Bot awareness
   * registers one), or a session idling between prompts, would hold
   * `sessions.size` above zero for the life of the worker and make the
   * worker permanently un-idleable. lastGeneratorActivity is stamped at
   * session creation and refreshed as the generator drains messages, so this
   * answers "did any session do work recently?" instead of "are any sessions
   * registered?". Queued-but-unprocessed work is a separate signal
   * (getTotalQueueDepth).
   *
   * A running generator is activity however old its last message: it may be
   * waiting on an observer reply with nothing left in the buffer (the
   * bare-prompt init turn). It cannot pin the worker awake for long, since a
   * generator with no messages ends after IDLE_TIMEOUT_MS (3 min).
   */
  hasSessionActivitySince(cutoffMs: number): boolean {
    for (const session of this.sessions.values()) {
      if (session.generatorPromise || session.lastGeneratorActivity >= cutoffMs) return true;
    }
    return false;
  }

  /**
   * Snapshot paused in-memory work without loading sessions or changing the buffer.
   * Quota/auth failures have no pending work. The automatic sweep resumes
   * only short-lived transitions that time heals, each paced or
   * capped where it is armed. A short rate-limit pause may be retried:
   * the breaker it armed paces it. The sweep leaves a session to a resume it
   * scheduled for itself, too: a rate limit must not be retried before its
   * Retry-After. With memory on the cmem gateway, a session whose unattended
   * resumes are spent is left for its next hook as well: the sweep is one more
   * unattended retry, and letting it through would make that budget meaningless.
   *
   * It leaves out pauses that a retry on a timer cannot heal. Each one waits
   * for the next captured event or an operator retry (`POST /api/processing`)
   * until the per-reason resume scheduler gives it a bounded retry:
   *  - a setup failure (`setup_required`), until the Claude CLI or the data
   *    directory is repaired. The start gate rechecks it on the next event;
   *  - an observer that spent its overflow recycles (`overflowPausedUntilMs`):
   *    a message that fits no generation aborts again on every retry, which
   *    the sweep repeated every cooldown, forever.
   */
  getResumableSessionIds(includeOperatorOnly: boolean = false): number[] {
    const automaticallyRetryable = new Set([null, undefined, 'rate_limit', 'overflow', 'provider_switch', 'response_stall']);
    return Array.from(this.sessions.values())
      .filter(session => !session.generatorPromise
        && this.buffer.getPendingCount(session.sessionDbId) > 0
        && (includeOperatorOnly || !(session.pausedReason === 'response_stall' && session.stallResumeTimer !== undefined))
        && (includeOperatorOnly || session.overflowPausedUntilMs === undefined)
        && (includeOperatorOnly || session.scheduledResumeTimer === undefined)
        && (includeOperatorOnly || !(unattendedGatewayResumesSpent(session) && canCmemGatewayServe(session.platformSource)))
        && (includeOperatorOnly || automaticallyRetryable.has(session.pausedReason)))
      .map(session => session.sessionDbId);
  }

  /**
   * Sessions the response-stall pacer gave up on (#5405): its last exit spent
   * the bounded stall resumes (stallResumesExhaustedAt, cleared by any
   * generator start), no generator is running, no resume of any kind is
   * scheduled, and buffered messages remain. The stall counter alone is not
   * enough: it resets only on an answered turn, so a session restarted after
   * exhaustion that later paused with its own retry timer would still look
   * abandoned. The stall exit discards the buffer when it stamps the mark, so
   * a session listed here holds work enqueued afterwards that no generator
   * start took up. Reporting is all this does: nothing here resumes or
   * discards them.
   */
  getAbandonedStallSessions(): Array<{ sessionDbId: number; pendingCount: number }> {
    return Array.from(this.sessions.values())
      .filter(session => session.stallResumesExhaustedAt !== undefined
        && !session.generatorPromise
        && session.stallResumeTimer === undefined
        && session.scheduledResumeTimer === undefined
        && this.buffer.getPendingCount(session.sessionDbId) > 0)
      .map(session => ({ sessionDbId: session.sessionDbId, pendingCount: this.buffer.getPendingCount(session.sessionDbId) }));
  }

  getTotalQueueDepth(): number {
    return this.buffer.getTotalDepth();
  }

  async getTotalActiveWork(): Promise<number> {
    return this.getTotalQueueDepth();
  }

  /**
   * Live inputs discarded by expiry over this worker's lifetime (#6391).
   *
   * Expired inputs get no replay, so this counter is the only signal
   * that the observer lost work; the status endpoint reports it next to the
   * queue depth so a busy queue can be told apart from a healthy one.
   */
  getExpiredObserverInputCount(): number {
    return this.buffer.getExpiredInputCount();
  }

  /**
   * Parked inputs evicted to make room for live ones over this worker's
   * lifetime. Parked inputs are never resent, so eviction loses nothing that
   * would have been sent; the counter keeps the eviction visible.
   */
  getEvictedParkedObserverInputCount(): number {
    return this.buffer.getEvictedParkedInputCount();
  }

  async isAnySessionProcessing(): Promise<boolean> {
    return this.getTotalQueueDepth() > 0;
  }

  async *getMessageIterator(sessionDbId: number): AsyncIterableIterator<PendingMessageWithId> {
    let session = this.sessions.get(sessionDbId);
    if (!session) {
      session = this.initializeSession(sessionDbId);
    }

    // Re-yield anything a prior generator pass claimed but did not confirm.
    await this.resetProcessingToPending(sessionDbId);

    for await (const message of this.buffer.drain({
      sessionDbId,
      signal: session.abortController.signal,
      // An idle generator must not hold its slot for the full idle
      // timeout while other sessions are parked waiting for one (#5376).
      shouldReleaseIdleSlot: () => getParkedSlotWaiterCount() > 0,
      onIdleTimeout: () => {
        logger.info('SESSION', 'Triggering abort due to idle timeout to kill subprocess', { sessionDbId });
        session.idleTimedOut = true;
        session.abortReason = 'idle';
        session.abortController.abort();
      }
    })) {
      // Re-check at every message boundary so a settings-file edit made while
      // a generator is already alive takes effect after its current request.
      // Drop buffered payloads but keep delivery identities: enabling capture
      // again permits fresh work, not a replay of these discarded events.
      if (!isObserverCaptureEnabled(SettingsDefaultsManager.loadFromFile(USER_SETTINGS_PATH))) {
        const discarded = await this.clearPendingForSession(sessionDbId);
        logger.info('SESSION', 'Observer capture disabled at message boundary; discarding buffered work', {
          sessionId: sessionDbId,
          discarded,
        });
        return;
      }

      // The previous request is answered, so nothing is in flight: end this
      // generator before it sends on the old provider. The next generator
      // releases this claim when it starts.
      // A Claude quota-fallback run (claudeAuthOverride) hands back the same
      // way once the primary credential's window elapses or its breaker clears:
      // the next admission probes the primary through the single probe claim.
      const standInLapsed = Boolean(session.claudeAuthOverride)
        && !claudeStandInStillServes(session.claudeAuthOverride!);
      // A run tier routing put on the simple model keeps that model for its
      // whole life, so a Bash/Edit/Write that arrives later ends it here and
      // the next generator routes it to CLAUDE_MEM_MODEL (#4963).
      const leavesSimpleTier = this.leavesSimpleTier(session, message);
      if (session.providerSwitchPending || standInLapsed || leavesSimpleTier) {
        logger.info('SESSION', standInLapsed
          ? 'Primary Claude credential may serve again; ending the quota-fallback run at a message boundary'
          : leavesSimpleTier
            ? 'Ending the simple-tier run at a message boundary for a message it should not serve'
            : 'Ending generator at a message boundary to switch providers', {
          sessionDbId,
          currentProvider: session.currentProvider,
          ...(standInLapsed ? { credential: session.claudeAuthOverride } : {}),
          ...(leavesSimpleTier ? { messageType: message.type, toolName: message.tool_name } : {}),
        });
        session.abortReason = PROVIDER_SWITCH_BOUNDARY_ABORT_REASON;
        session.abortController.abort();
        return;
      }
      session.claimedMessageIds.push(message._persistentId);
      if (session.earliestPendingTimestamp === null) {
        session.earliestPendingTimestamp = message._originalTimestamp;
      } else {
        session.earliestPendingTimestamp = Math.min(session.earliestPendingTimestamp, message._originalTimestamp);
      }

      session.lastGeneratorActivity = Date.now();

      yield message;
    }
  }

  /** Only ClaudeProvider reads modelOverride, so only its runs are held to the tier. */
  private leavesSimpleTier(session: ActiveSession, message: PendingMessageWithId): boolean {
    return Boolean(session.simpleTierRun)
      && session.currentProvider === 'claude'
      && !isSimpleTierMessage(message.type, message.tool_name);
  }

  /** Extend the current request without advancing the asynchronous iterator. */
  claimNextObservation(sessionDbId: number, accepts: (message: PendingMessageWithId) => boolean): PendingMessageWithId | null {
    const session = this.sessions.get(sessionDbId);
    if (!session || session.abortController.signal.aborted) return null;
    const message = this.buffer.claimNextMatching(sessionDbId,
      candidate => candidate.type === 'observation'
        && !this.leavesSimpleTier(session, candidate)
        && accepts(candidate));
    if (message) {
      session.claimedMessageIds.push(message._persistentId);
      session.earliestPendingTimestamp = Math.min(session.earliestPendingTimestamp ?? message._originalTimestamp, message._originalTimestamp);
    }
    return message;
  }

  /** Read-only access to the in-RAM buffer for diagnostics. */
  /** Whether a summarize is buffered for this session, claimed or not. */
  hasPendingSummarize(sessionDbId: number): boolean {
    return this.buffer.peekTypes(sessionDbId).some(message => message.message_type === 'summarize');
  }

  /** Take this session's one summarize rescue; false once it has been used. */
  claimSummarizeRescue(sessionDbId: number): boolean {
    if (this.summarizeRescues.has(sessionDbId)) return false;
    this.summarizeRescues.add(sessionDbId);
    return true;
  }

  getMessageBuffer(): SessionMessageBuffer {
    return this.buffer;
  }
}