Repository navigation
Conversation
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.
This was referenced Sep 30, 2026
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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]/presencetakes{ clientId, action: acquire | renew | release }and keeps one presence leaseper 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.tscheckevery 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 awindow 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 canneither 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 theinstance's configured
idleMinutes(fromGET /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
startRpcSessiontreat a closingwrapper as absent and build a replacement.
Idle machinery split (
lib/rpc-manager.ts):markActivity()records realactivity and moves the LRU timestamp,
armIdleTimer()only (re)starts thewindow. 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-liteis not introduced — the mode is the server'sown 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
idleMinutesrather 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
mainand the diff becomes this PR only. Opened as a draft for thatreason — it will be force-pushed.
Verified
node --experimental-strip-types --testover the whole suite: 1402 tests, 0failures,
tsc --noEmitandeslint .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).