Skip to content

Add an opt-in Lite mode (session lifetime, model settings, soft memory target) - #984

Closed
HoloNova wants to merge 4 commits into
agegr:mainfrom
HoloNova:pr/lite-mode
Closed

HoloNova wants to merge 4 commits into
agegr:mainfrom
HoloNova:pr/lite-mode

Conversation

@HoloNova

@HoloNova HoloNova commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Closed — superseded by a six-PR split. This branch was a first
implementation of Lite mode and served as the port reference; the design was
then reworked (Lite is the instance's setting rather than a device-local
one, the extension-release and catalogue questions got their own fixes), and
every part of it now lives in a reviewable PR below. Nothing here is lost:
the branch is kept at HoloNova/pi-web:prototype/lite-mode-cd0e8f5 (note the
fork name) and the closed diff stays readable.

This PR contained Now in
SSE backpressure for a stalled client (lib/agent-event-stream.ts) #997
Delegated work counted as busy (lib/delegated-work.ts, scan truncation ⇒ busy) #998
Server-wide Lite configuration (pi-web-settings.json, GET/PUT /api/lite, settings UI) #999
Session lifetime: per-page presence, release on conversation switch / page close / idle window, page identity, LRU timestamp kept apart from timer maintenance #1000
Lite model/provider reads without loading extensions (cache keyed by catalogue; writes still resolve against the full one) #1002
Listing models registered by extensions, as a Lite-mode switch (new) #1002
Soft memory target, cgroup-preferred measurement, reclaim of the oldest idle session, settings control and chat notice, docs/adr/0006 #1004
Releasing the extensions a full-catalogue read loads (new — this was a pre-existing leak, not a Lite feature) #1003

Deliberately not carried over, after review:

  • the device-local layer (lite-mode-preference, device-preference,
    lite-idle-minutes, useLiteIdleMinutes, the localStorage switch, the
    cross-tab storage sync): the mode is the instance's, so one settings file
    serves every device;
  • the x-pi-web-lite request header (lib/lite-request.ts): no client could
    be allowed to ask for the extension-loading catalogue, so the server decides
    from its own configuration;
  • the pi-subagents private run registry in delegated-work.ts (status.json
    scanning, PID liveness, deadlines, terminal-state table): there is no public
    format to depend on, and the built-in exact state plus artifact-directory
    recency cover it;
  • the "armed" deferred reclaim (idleReclaimRequested /
    attemptIdleReclaim() / the 30 s retry): it turned "a session is busy" into
    "a session owes a close", which is where the race came from — the idle timer
    now treats delegated work exactly like a running turn;
  • reclaiming on GET /api/memory: a status read is read-only now, and the pass
    is POST /api/memory/reclaim, one session per pass.

What

Adds an opt-in Lite mode (Settings → General → Session lifetime) and fixes a
way sessions could be closed out from under a running child. Normal mode is
unchanged: nothing here runs unless the user turns Lite mode on.

  • Session lifetime. A page holds a per-tab presence for the session it shows
    and releases it on a conversation switch, on page close, or after the idle
    window (device-local, 1–60 minutes, default 5) without real interaction —
    typing, clicking, touching, scrolling. Heartbeats, polling and SSE do not
    count. Hiding the page (tab switch, minimized window, another app) keeps the
    session warm. Presence is per (session, tab), so one tab cannot close a session
    another tab or device is still viewing, and a running task is never
    interrupted.
  • Model settings without loading extensions. Lite requests to the model and
    provider routes use ModelRuntime.create() — built-in providers plus
    models.json — instead of loading every configured extension to answer a
    provider list. enabledModels writes still compute from the full catalogue, so
    a Lite save cannot prune extension-provided entries, and a session pinned to an
    extension model is flagged, never silently switched.
  • Soft memory target. Configurable (256–16384 MiB, default 1800), measured
    read-only from the service's own cgroup, and acted on by reclaiming the oldest
    idle, unviewed sessions when usage is near (≥ 90 %) or over it. Soft by
    construction: no process is killed and systemd is untouched. The target exists
    only in Lite mode — a normal-mode GET /api/memory reports { active: false }
    and PUT is refused — so normal mode has no ceiling.
  • Delegated work counts as busy. isRunning() only described a wrapper's own
    turn, so an async subagent run that outlived its parent turn looked idle and
    could be closed, killing in-process children with it. isBusy() now covers
    both, decided from two read-only signals: recent writes under
    <session file without .jsonl>/ (where every child run writes its own session,
    whichever plugin spawned it) and the pi-subagents run registry when readable.
    Unreadable state counts as busy, and a deferred close is retried every 30 s.
    /api/agent/running uses the same predicate, so anything that waits for a
    quiet service — a nightly restart guard, for instance — no longer skips a busy
    host by luck.

Verified

node --experimental-strip-types --test for the new suites (Lite lifecycle,
memory status store, reclaim planner, delegated-work detection) plus the
existing Lite/model/settings/i18n suites; tsc --noEmit and eslint clean.
Also deployed and exercised on a 2 vCPU / 3.8 GB VPS host: three Lite provider
reads no longer spawn extension hosts (6 → 6 helper processes, versus +1 for a
single non-Lite read), and an idle session releases about five minutes after the
last interaction, closing the wrapper and reopening it in under a second on
return.

Not done on purpose

  • The OAuth login route still loads extensions: its browser EventSource cannot
    carry the Lite header, and login is a one-off interaction.
  • A Lite enabledModels write still loads extensions once, for the
    full-catalogue guard above.
  • evictIfDiskAhead (the explicit "reload session from disk" action) is
    untouched: it is user-initiated, not an automatic reclaim.
  • Nothing here changes pi's own or any plugin's configuration.

ops and others added 4 commits September 28, 2026 16:20
A client that stops reading let its event queue grow without limit: one 24 MB
command with an unread stream buffered 22.7 MB on the server.

Cap the queued bytes per client (PI_WEB_SSE_BACKLOG_LIMIT_BYTES, default 4 MB).
Past the cap, drop events the client can rebuild from session state, and close
the stream for backlog that cannot be rebuilt so the client reconnects and
re-snapshots instead of holding memory.
Per-device switch (Settings -> General -> Session lifetime) with three effects:

- Session lifetime. A page holds a per-tab presence for the session it shows and
  releases it on a conversation switch, on page close, or after an idle window
  without real interaction. Hiding the page keeps the session warm, another tab
  or device viewing it blocks the close, and a running task is never interrupted.
- Model settings. Lite requests read the built-in catalogue plus models.json
  through ModelRuntime.create() instead of loading every configured extension.
  enabledModels writes still use the full catalogue, and a session pinned to an
  extension model is flagged, never switched.
- Soft memory target. Configurable, measured read-only from the cgroup, acted on
  by reclaiming the oldest idle, unviewed sessions when usage is near or over it.
  Nothing is killed and systemd is untouched. The target exists only in Lite
  mode; normal mode reports { active: false } and keeps its current behaviour.
The settings control read the memory status once at mount while the chat notice
polled, so the two drifted. Both now read one shared store with a single
in-flight-deduped refresh and one 10 s poll loop; saving a target broadcasts to
every subscriber before the reconcile request returns.

The idle window becomes a device-local option (1-60 minutes, default 5, synced
across tabs of the same browser), the settings copy is shortened, the memory
target is hidden and inert while Lite mode is off, and its default moves to
1800 MiB — a starting point for a fresh install, not a tuned value.
isRunning() only described a wrapper's own turn, so a session whose async
subagent was still working looked idle: reclaim, the pressure pass and the
10-minute idle shutdown could close it and kill an in-process child with it.
/api/agent/running had the same hole.

isBusy() now means "own turn or delegated work", decided from two read-only
signals: recent writes under <session file without .jsonl>/ (where any child run
writes its own session) and the pi-subagents run registry when readable.
Unreadable state counts as busy, and a deferred close is retried every 30 s.
@HoloNova

Copy link
Copy Markdown
Contributor Author

Closed as superseded by #997, #998, #999, #1000, #1002, #1003 and #1004 — the description now maps each part of this branch to the PR that carries it. Thanks for the read; the branch stays available at the fork for reference.

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.

1 participant