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.
…ssion
Lite mode could already close one session when its page let go of it; what was
missing was a number to judge the service's own footprint against. This adds
that target and the one path that acts on it.
- `lite.memoryTargetMiB` is the target (already stored with the rest of the
mode; 256-16384 MiB, 1800 default). It is soft: the unit's systemd
MemoryHigh/MemoryMax limits stay the real guardrail and nothing is ever
killed because of this value.
- `lib/service-memory.ts` measures the footprint read-only, preferring the
cgroup (`/proc/self/cgroup` -> `memory.current`, with the pi-web.service
paths and `PI_WEB_MEMORY_CGROUP_PATH` as fallbacks) and reporting a labelled
approximation when only Node RSS (+ direct children) is readable.
- `GET /api/memory` reports the target, the reading, the state (near from 90%,
over past it) and how many idle sessions a pass could close. It closes
nothing: a status read must not change how the service behaves.
- `POST /api/memory/reclaim` runs one pass and is the only thing that closes a
session. It closes at most the oldest idle one (`RECLAIM_PASS_LIMIT`), never a
running task or a session whose delegated child is still working, and never a
session another tab or device is viewing. When nothing qualifies it reports
the counts and the service stays over target — no escalation.
- The settings control and the chat composer show the reading and the state from
one shared 10 s poll (paused while the tab is hidden), and the settings control
can ask for a pass.
- Normal mode is untouched: the target is inert there, `GET /api/memory` reports
`{ active: false }` and the reclaim endpoint refuses with 409, so nothing can
close a normal-mode session.
The ordering and the "never touch a running or viewed session" rule live in
`lib/lite-memory-reclaim.ts`, which is pure and tested on its own; the pass
reuses the same `reclaimIdleRpcSession()` presence release uses. See
docs/adr/0006-lite-memory-target.md.
The target is the instance's, so the policy that acts on it belongs to the server too. A monitor started once per process now measures the service every 15 seconds: a quiet reading stops at the comparison, and at or above 90% of the target it runs one reclaim pass, which still closes at most the oldest eligible session and never escalates. The timer is unref'd and the monitor is a globalThis singleton, so a second module graph starts nothing twice. The status read goes back to being pure observation: it no longer collects reclaim candidates (and no longer walks sessions or delegated trees on every poll) and no longer reports an idle-session count. The UI is display-only, and the browser-driven path is gone: POST /api/memory/reclaim and its settings button are removed, because a policy the operator configured should not need a page open to run. Verified: 1437 tests pass, tsc --noEmit and eslint clean, the production build bundles, and a shadow instance logs 'memory policy monitor started, every 15s' with /api/memory reporting the new shape and the removed endpoint answering 404.
The e2e job failed on 48cbd8f in an assertion about restoring a reading offset after paginating older history (a 5px geometry tolerance), while the same tree passes locally: all ten e2e scenarios pass, including the reading-offset one. Nothing in this commit changes the tree; it only asks CI for another run.
The policy acts at the near threshold (90% of the target), not at the target, so comments and test names that said 'below the target / at or above the target' described a different policy than the code runs. Say near threshold everywhere, including the ADR's 'a single over-target reading' and 'stays over target'. No behavior change: MEMORY_NEAR_TARGET_RATIO, shouldReclaimForMemoryState, the tick, the interval and the result strings are untouched. The monitor tests now name the boundary and pin it from both sides: 1619 MiB against a 1800 MiB target is the last reading that only measures, 1620 MiB (exactly 90%) is the first that acts.
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
Lite mode could already close a session when the page viewing it let go. This
adds the number to judge the service's own footprint against, and the single
path that acts on it.
lite.memoryTargetMiB— the target, stored with the rest of the mode(256–16384 MiB, default 1800). It is soft: the unit's systemd
MemoryHigh/MemoryMaxlimits stay the real guardrail, and nothing is everkilled because of this value.
lib/service-memory.ts— read-only measurement, preferring the cgroup(
/proc/self/cgroup→memory.current, with thepi-web.servicepaths andPI_WEB_MEMORY_CGROUP_PATHas fallbacks). Without a readable cgroup it fallsback to Node RSS + direct children and labels the value as an approximation.
GET /api/memory— reports the target, the reading and the state (nearfrom 90% of the target,
overpast it). Observation only, and cheap: itreads the configuration, reads the cgroup and compares the two; it never walks
sessions, never closes one, and has no session counts to maintain.
lib/lite-memory-monitor.ts— the policy that acts, owned by the server.Started once per process from the Node instrumentation hook, it ticks every
15 s: below the 90% near threshold it only measures; at or above that threshold
it runs one pass. So the target is acted on whether or not a page is open, and
nothing a browser does can close a session.
state; the chat composer warns on
near/over. Both read one shared 10 spoll, paused while the tab is hidden. Display only: there is no "reclaim now"
button and no endpoint to ask for a pass, because a policy the operator
configured should not need a page open to run.
The pass
lib/lite-memory-reclaim.tsis pure and decides what may be closed: orderedoldest-first by last real activity (not by timer maintenance), skipping any
session that is running, has delegated work in flight, or is held by another tab
or device (presence lease or SSE stream).
One session per pass (
RECLAIM_PASS_LIMIT). The monitor re-measures beforethe next pass, so a single pressure reading never drops a page's whole working
set. When nothing qualifies the service stays under memory pressure until the
next tick — there is no escalation and no process kill. The pass reuses
reclaimIdleRpcSession(), the same path presence release uses, and the monitorlogs the passes that actually close something.
Normal mode
Untouched. The target belongs to Lite mode:
GET /api/memoryreports{ active: false }and the monitor's tick returns before it measures anything,so no reading is taken and nothing can close a normal-mode session. The stored
number survives so the operator's value is kept.
Stacking
Stacked on #998, #999 and #1000: the pass needs the delegated-work
busy signal (#998), the mode/target configuration (#999) and the presence/liveness
bookkeeping (#1000). Until those land, the diff below contains their content as
well; this is rebased onto
mainafterwards, and opened as a draft for thatreason.
Verified
Whole suite: 1437 tests, 0 failures;
tsc --noEmitandeslint .clean;the production build bundles. A shadow instance of the built app logs
memory policy monitor started, every 15s, answers/api/memorywith theobservation-only shape, and 404s the removed endpoint.
New coverage: the cgroup discovery and the RSS fallback (path candidates, the
/proc/self/cgroupparse, the children sum, the env override, a non-numericvalue), the pressure boundaries, the reclaim ordering and its skips (running,
viewed, delegated — including that a larger limit still goes oldest-first and
that a failed shutdown is not reported as reclaimed), the shared status store
(one poll loop, concurrent reads sharing a request, polling paused while
hidden), the policy tick (Lite off measures nothing; a quiet reading only
measures and never reaches a session walk; near/over runs exactly one pass;
nothing eligible does not escalate; the next tick stops once the reading is
back; a second start adds no second interval; the timer is unref'd; a failing
pass is logged and retried), and the public surface (the status route has no
POST, no session walk and no counts; the removed endpoint stays removed).