pi-web icon indicating copy to clipboard operation
pi-web copied to clipboard

feat: make session idle timeout configurable via env var

Open fallleave001 opened this issue 3 months ago • 2 comments

Problem

Session idle timeout is hardcoded to 10 minutes (lib/rpc-manager.ts, 10 * 60 * 1000) with no way to configure it. This breaks use cases where sessions need to stay alive without active interaction:

  • Cross-session coordination (e.g. pi-intercom): a session that is merely listening for messages from other sessions is shut down after 10 min of inactivity, disconnecting it from the coordination broker.
  • Long-running extensions: sessions with background tasks that don't produce agent events get killed.

Proposed Solution

Support a PI_WEB_IDLE_TIMEOUT_MS environment variable as the global default idle timeout:

# Disable idle shutdown entirely
PI_WEB_IDLE_TIMEOUT_MS=0 pi-web

# Set 30-minute timeout
PI_WEB_IDLE_TIMEOUT_MS=1800000 pi-web

Behavior:

  • PI_WEB_IDLE_TIMEOUT_MS not set ??current 10-minute default (no breaking change)
  • 0 ??never kill idle sessions
  • Positive number ??use as timeout in milliseconds
  • Invalid/negative ??fall back to 10 minutes with a console warning

Implementation (~10 lines):

const DEFAULT_IDLE_TIMEOUT_MS = (() => {
  const raw = process.env.PI_WEB_IDLE_TIMEOUT_MS;
  if (raw !== undefined && raw.trim() !== "") {
    const parsed = Number(raw);
    if (Number.isFinite(parsed) && parsed >= 0) return parsed;
    console.warn(`[pi-web] invalid PI_WEB_IDLE_TIMEOUT_MS "${raw}", falling back to 10 minutes`);
  }
  return 10 * 60 * 1000;
})();

Then replace the hardcoded 10 * 60 * 1000 in resetIdleTimer() with DEFAULT_IDLE_TIMEOUT_MS.

Why Env Var (Not UI/Per-Session)

  • Zero UI complexity, zero new API surface
  • Standard configuration pattern (follows existing PI_WEB_* prefix convention)
  • No race conditions (no session file header writes)
  • Users who need this are typically headless/server deployments where env vars are natural

Additional Thought

If there's interest, a per-session override could be added later via a custom idleTimeoutMs field in the session file header (the SDK already preserves unknown header fields). But that can be a separate discussion/PR.

Context

pi-web-upstream fork with local modifications; happy to upstream this if there's interest.

fallleave001 avatar Aug 27 '26 15:08 fallleave001

Adding a concrete data point for the "long-running extensions" case in the problem statement, since I hit it from a different angle.

isRunning() cannot see work owned by an extension, so a session that is only waiting on background subagents looks idle and gets shut down at the 10-minute mark:

isRunning() {
  return this._alive && (
    this.pendingPromptCount > 0 ||
    this.inner.isStreaming ||
    this.inner.isCompacting ||
    this.inner.isBashRunning
  );
}

None of the four terms covers "an extension has in-flight work". Concretely, with @tintinweb/pi-subagents, spawning with run_in_background: true returns immediately, the parent turn ends, pendingPromptCount drops to 0 — and from that moment the host is a shutdown candidate even though the child is still running.

This is structural rather than a tuning problem. The extension has no way to register activity: grepping @tintinweb/[email protected] for __piSessions, registerSession, piSubagentRuns and pi-web gives 0 hits in all cases. The two layers are fully decoupled, so no value of PI_WEB_IDLE_TIMEOUT_MS lets an extension say "I'm busy" — it can only trade one fixed guess for another.

When the shutdown does happen, the failure is silent in a way worth noting: the extension keeps agent records in memory only (agents = new Map(), no writeFileSync in agent-manager.js) and its completion notification is a setTimeout (NUDGE_HOLD_MS = 200) whose send errors are swallowed. So the host session dies, the extension's JS context goes with it, and the parent conversation — which is persisted — is left waiting for a notification whose sender no longer exists. I filed that half upstream at tintinweb/pi-subagents#272.

Two notes on scope, so this doesn't overstate the case:

  1. I could not show that the idle timer caused the failures I was originally investigating. Duration buckets for 23 non-completed workflow runs: <2min 14, 2-8min 3, 8-9.5min 0, 9.5-10.5min 1, 10.5-12min 1, >12min 6. No clustering at ~10 min, so those were mostly something else. This is a real structural hazard, not the explanation for that batch.
  2. The terminal TUI does not have this path. @earendil-works/pi-coding-agent has exactly one idleTimer hit and it is OpenAI Codex WebSocket pooling (scheduleSessionWebSocketExpiry), unrelated to session lifetime. This shutdown behavior is pi-web-specific.

So PI_WEB_IDLE_TIMEOUT_MS as proposed would help me immediately (I'd set it to 0), and I'd support it landing as-is. But if a follow-up is on the table, the more complete fix is letting extensions keep a session alive — e.g. exposing a lease/keepalive an extension can hold while it has in-flight work, so isRunning() reflects extension activity instead of each side guessing. That would also cover the pi-intercom listening case in the original post without requiring users to disable idle shutdown globally.

Environment: @agegr/[email protected], @tintinweb/[email protected], Node v25.9.0, Windows. Code references are against the compiled .next/server output of the published 0.8.11, since that's what I inspected.

Naoki326 avatar Aug 29 '26 01:08 Naoki326

Implemented by #665, with complementary extension-liveness support in #705. PI_WEB_IDLE_TIMEOUT_MS now supports unset/blank default, 0 to disable eviction, positive millisecond values, and warning plus fallback for invalid or negative values. Closing as completed.

agegr avatar Sep 05 '26 16:09 agegr