Skip to content

docs: add design spec for background sessions - #15

Merged
oonid merged 2 commits into
mainfrom
docs/background-sessions-design
Oct 17, 2026
Merged

oonid merged 2 commits into
mainfrom
docs/background-sessions-design

Conversation

@oonid

@oonid oonid commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Design for keeping terminal sessions running after leaving their screen, mainly so the Hermes gateway can run 24/7. Docs only; the implementation plan (docs/plans/background-sessions.md) and the implementation follow in their own PR. Replaces the cancelled #13, now in the neutral layout from #14.

Spec: docs/design/background-sessions.md. android-apk-architecture.md (Follow-up Items, "Background sessions") points to it.

Baseline before this work: tag pre-background-sessions (main 86d9e93).

Decisions in the spec:

  • Ownership: an app-wide SessionManager holds one session per container (PTY, emulator with the screen contents, reader thread that keeps draining output). A thin foreground service (specialUse; dataSync is capped at 6 hours a day on Android 15) only keeps the process alive and owns the notification. Chosen over a bound service owning the sessions: same reliability, no started-and-bound service, session logic testable on the JVM.
  • Back: keyboard, then selection, then a prompt: End session / Keep running / Cancel.
  • Lifecycle: Home, switching apps and swipe-away keep sessions; only End or the program exiting ends one; a session that ends with no screen attached posts a "session ended (exit N)" notification; process death ends everything and nothing is restored.
  • Deep sleep: a "Keep CPU awake" toggle in the notification (partial wake lock) keeps the CPU running with the screen off; off by default and not remembered. Doze ignores wake locks and suspends network for apps not exempt from battery optimization (a foreground service does not change that), so the 24/7 gateway also needs the exemption, which a banner in the app offers.
  • Emulator thread and screens: each session's emulator runs on its own HandlerThread (termlib builds a snapshot on its looper after every output burst); a session is shown by at most one screen.
  • Testing: JVM unit tests for SessionManager through fake seams, the existing suites, adb lifecycle checks (forced Doze with and without the exemption), and the gateway answering from the host for 30 minutes in forced deep sleep with the exemption granted.

The second commit corrects deep sleep after review (checked against Android's Doze and Android 15 behavior-change docs) and adds the emulator thread and one-screen rule.

Out of scope: several sessions per container, start at boot, restoring sessions after process death.

oonid added 2 commits October 17, 2026 23:26
Terminal sessions are owned by TerminalActivity, so leaving the screen
ends them and the Hermes gateway cannot run in the background. The spec
moves sessions into an app-wide SessionManager (one per container) with
a thin specialUse foreground service that keeps the process alive and
owns the notification, an End session / Keep running / Cancel prompt on
Back, a "Keep CPU awake" wake-lock toggle off by default, a "session
ended" notification for sessions that end while away, and the error
handling and tests. Baseline before this work: tag
pre-background-sessions.
Android's Doze ignores wake locks and suspends network access for apps
that are not exempt from battery optimization, and a foreground service
does not change that (it only keeps the app out of App Standby). The
spec implied the "Keep CPU awake" toggle alone keeps a session alive in
deep sleep. It now separates screen-off CPU sleep (the wake lock) from
Doze (the exemption), says the 24/7 gateway needs both, and the device
checks expect the counter to pause in forced Doze without the
exemption.

Also from the review: each session's emulator runs on its own
HandlerThread, since termlib builds a snapshot on its looper after
every burst of output (verified early, main looper as fallback;
scrollback is capped at 1000 lines); and a session is shown by at most
one screen, so opening it again moves it to the new screen.
@oonid
oonid merged commit c0f9298 into main Oct 17, 2026
1 check passed
@oonid
oonid deleted the docs/background-sessions-design branch October 17, 2026 16:42
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