Skip to content

feat(lite): release an idle session when no page is viewing it - #1000

Closed
HoloNova wants to merge 3 commits into
agegr:mainfrom
HoloNova:pr/lite-lifecycle
Closed

HoloNova wants to merge 3 commits into
agegr:mainfrom
HoloNova:pr/lite-lifecycle

Conversation

@HoloNova

Copy link
Copy Markdown
Contributor

What

A page kept its session warm for as long as it was open. The SSE lease is
renewed every 30 s, so pi-web's idle shutdown only ever closed a wrapper after
ten minutes without a viewer — switching conversations, closing a tab, or
walking away from a phone left a whole session (and its extension host) alive
for minutes, and there was no way for the server to know a page had stopped
looking at a session.

This adds a page-scoped presence signal and a prompt reclaim that uses it. It
is the session-lifetime half of the Lite mode work.

How

  • Presence is per page. POST /api/agent/[id]/presence takes
    { clientId, action: acquire | renew | release } and keeps one presence lease
    per page, so one page releasing a session never closes a wrapper another page
    or device is still using. The route is refused while the instance runs in
    normal mode (409) and goes through the same lib/request-security.ts check
    every other state-changing route uses, because a release can close a session.

  • The page id lives in the page's JavaScript runtime (lib/client-identity.ts)
    — not in sessionStorage, which some browsers copy into a new window or a
    window opened from an opener. A copied id would let one page release another
    page's leases. A reload therefore gets a new id, and the server's 90 s lease
    TTL covers a page that vanished without releasing. The same id scopes the SSE
    lease (/events?client=) and its renewal (/lease?client=), so a page can
    neither release nor keep alive another page's connection.

  • What counts as activity. A mounted page holds presence while the last real
    interaction — pointerdown, keydown, touchstart, wheel — is within the
    instance's configured idleMinutes (from GET /api/lite, feat(lite): make Lite mode a server-wide setting #999). Heartbeats,
    SSE traffic, polling and lease renewals are deliberately not activity, and
    neither is visibility: minimizing or switching tabs keeps a recently used
    session warm; the deadline alone releases it.

  • Reclaim is conservative. When the last holder releases (switch, page hide,
    idle window, or lease expiry), the server closes the wrapper only if nothing
    is running, no delegated child is still working, and no other lease or
    presence holds it. A running turn or an in-flight subagent is never
    interrupted: the reclaim does nothing and the idle timer closes the session
    once that work settles. A command arriving for a wrapper that is already
    closing is not sent into it — the routes and startRpcSession treat a closing
    wrapper as absent and build a replacement.

  • Idle machinery split (lib/rpc-manager.ts): markActivity() records real
    activity and moves the LRU timestamp, armIdleTimer() only (re)starts the
    window. Previously a re-arm also moved the timestamp, so "last activity" could
    mean "last time a timer was reset".

Behaviour change

Normal mode is unchanged: presence is refused server-side and no client ever
sends a mode flag (x-pi-web-lite is not introduced — the mode is the server's
own configuration). What changes is Lite mode: an idle unviewed session is now
reclaimed on release instead of after the idle window, and the session's lifetime
is governed by the instance's idleMinutes rather than the connection lease.

Stacking

This branch is stacked: it is built on #999 (server-wide Lite configuration)
and includes #998 (delegated work counts as busy), because the reclaim path
must never close a session that still has a running child. Until those two land
the diff below contains their files as well; once they are merged this is
rebased onto main and the diff becomes this PR only. Opened as a draft for that
reason — it will be force-pushed.

Verified

node --experimental-strip-types --test over the whole suite: 1402 tests, 0
failures
, tsc --noEmit and eslint . clean. New coverage: the page identity
(one per page runtime, never persisted, no storage access), the presence
controller (Strict Mode remount, immediate release on page hide), the pure
hold/deadline rules (hidden-but-recent holds; heartbeat/poll/renewal do not),
session-liveness presence scoping and the expiry fallback, the presence route
(per-page acquire/renew/release, 400s, 409 in normal mode, 403 for an untrusted
host or cross-site request), and the reclaim rules (idle unviewed closes; another
viewer, a connection lease, a running turn or a writing delegated child all
hold it open).

isRunning() only described a wrapper's own turn, so a session whose async
subagent was still working looked idle: the idle shutdown could close it and
kill an in-process child with it. /api/agent/running had the same hole, so
anything that waits for a quiet service — a nightly restart guard, for
instance — could skip a busy host.

isBusy() now means "own turn or delegated work". Delegated work is read from
two read-only signals: a built-in subagent run whose parent is this session,
and recent writes under <session file without .jsonl>/ — the directory every
child run writes its own session into, whichever plugin spawned it. An
incomplete scan counts as busy.

The idle timer treats delegated work exactly as it treats a running turn: the
window starts over rather than the session being closed the moment a child
exits, so a result on its way back to the parent always lands.
Lite mode is not a browser preference. What it changes — which model catalogue
gets loaded, how long an idle session is kept warm, how much memory the service
aims to stay under — are resources the server shares, so a per-device switch
meant two browsers on one Pi-Web disagreed about the same process.

Lite mode now lives in one Pi-Web-owned file, ~/.pi/agent/pi-web-settings.json
({version, lite: {enabled, idleMinutes, memoryTargetMiB}}), read and written by
the server and shared by every client. GET /api/lite reports the configuration
with the bounds each value is validated against; PUT changes it as a partial
update through the same request-security check every other mutating route uses.
General settings gains the Session lifetime section that edits it, and a client
that reads the shared value no longer keeps a copy of its own in localStorage.

The stored values are policy, so a missing, damaged or hand-edited file falls
back to the defaults instead of failing a read, and a file that cannot be parsed
is never overwritten. Nothing changes how the service behaves yet: the switch
and the numbers are stored and reported, and the session-lifetime, model and
memory work that acts on them arrives in the follow-ups.
A page kept its session warm for as long as it was open: the SSE lease was
renewed every 30 s, so the idle timer only closed a wrapper after ten minutes
without a viewer — switching conversations, closing a tab, or walking away from
a phone left a session process (and its extensions) alive for minutes.

Lite mode now gives the server a page-scoped presence signal. A mounted page
holds presence while the last real interaction is within the instance's
configured idle window — pointer, keyboard, touch or wheel, never a heartbeat,
a poll, or a lease renewal — and releases it on a conversation switch, a page
close, or that window. When the last holder releases, the server reclaims the
wrapper at once if nothing is running and no other tab or device still holds
it; a running turn or a delegated child is never interrupted, and the idle
timer closes the session once such work settles.

The page id this rests on lives in the page's JavaScript runtime only: a
reload, a new window, or a window opened from an opener therefore gets its own
identity, and the server's 90 s lease TTL covers a page that vanished without
releasing. The same id scopes the SSE lease and its renewal, so one page can
never release — or keep alive — another page's connection.

The idle machinery is split while we are here: markActivity() records real
activity and moves the LRU timestamp, armIdleTimer() only (re)starts the
window, so "last activity" always means the last real one.

Depends on the delegated-work busy signal and on the server-wide Lite
configuration; presence is refused while the instance runs in normal mode, so
normal-mode session lifetime is unchanged.
@agegr

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks @HoloNova. Splitting #984 into reviewable pieces and writing down what was deliberately left out made this much easier to evaluate. After reading the series, though, I'm closing the Lite mode feature PRs (#999, #1000, #1002, #1004):

Focused fixes like #997 and #1003 are very welcome. Thanks again for the depth of the work here.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants