Files
hapi/hub
8f9d6bd7f8 fix(hub): keepalive-only sessions reconcile to an idle lifecycle (#1820) (#1821)
* fix(hub): reconcile keepalive-only sessions to an idle lifecycle

`sessionCache.expireInactive` only expires a session when `activeAt` goes
stale, and `session-alive` refreshes `activeAt` every ~2s for as long as the
CLI socket lives. A session whose agent has done nothing for days therefore
stays `active: true` / `lifecycleState: running` forever — socket pinging was
being read as agent health.

Add the missing second signal. `active` keeps its meaning (the CLI is
reachable); `metadata.lifecycleState` moves `running` -> `idle` once the hub
has seen nothing but keepalives for `HAPI_SESSION_IDLE_TIMEOUT_MS` (default
12h, `0` disables), and back to `running` on the next real progress.

`active` is deliberately not flipped: the socket really is up, and a false
`active: false` would let `resumeSession` spawn a second agent against a live
process and would unlock dedup-merge / delete on it — the #1705 class of bug.

- `sessionCache.recordAgentProgress` tracks a progress clock that keepalives
  can never touch: messages in either direction, queued prompts, background
  tasks. Seeded from `updatedAt` so a hub restart re-derives it from disk.
- `reconcileKeepaliveIdle` runs on the existing 5s inactivity tick. Sessions
  that are thinking, running background tasks or holding a pending permission
  request are never marked; `metadata.idleReconcileExempt` opts out entirely.
- Only real progress clears `idle`, never `thinking` — Cursor ACP emits
  ambient idle/running churn (#1553) that would otherwise flap the lifecycle
  every tick.
- `isLiveLifecycleState` replaces the `=== 'running'` literals so an `idle`
  row does not read as dead to the archive route or the Cursor migrator.
- Web splits keepalive-only sessions into their own session-list bucket.

Refs #1820. Distinct from #842 (`active=false` with a stale `running` badge);
this is the inverse.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

* fix(hub): seed the idle clock from stored messages across hub restart

PR review on #1821: a cold `SessionCache` had no observed progress and fell
back to `updatedAt` alone. Assistant messages deliberately do not move
`updatedAt`, so a session that was streaming output a minute before a hub
restart read as hours idle and got marked on the very next tick — a false
`idle` that persisted until the next progress event.

Seed the clock once from `getNewestMessagePosition`, which is the durable
record of that output. `updatedAt` stays a floor on top of the seed: todos /
teamState / agentState writes bump it without routing through
`recordAgentProgress`.

Regression test recreates a SessionCache over the same store after recent
assistant output and confirms keepalives no longer cause premature idle
classification — and that the same session is still marked once the output
itself goes stale.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

* test: cover the socket wake-up path and the idle session-list bucket

Follow-up review on #1821 reported no findings but flagged two uncovered
paths. Both have bad failure modes, so close them.

- Hub: an assistant message arriving through `registerSessionHandlers` must
  wake an idle session. Assistant messages never bump `updatedAt`, so the
  `onAgentProgress` hook is the only thing that can clear the mark — a
  refactor dropping that wiring would silently false-idle every actively
  working session, and nothing would have caught it.
- Web: extract the inline bucketing from the `runningSessions` useMemo into a
  pure `bucketRunningSessions`, matching how the rest of SessionList's logic
  is exported and tested. Covers idle vs quiet-but-ready separation, no
  double-bucketing, the transition back out of idle on thinking/background
  task/pending request, and the disconnected/pinned skips.

No behaviour change.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

* test: cover the idle lifecycle at both isLiveLifecycleState call sites

Third review round reported no findings but named the two guards this PR
changed as uncovered. Both protect against reading a keepalive-idle row as a
dead one:

- Cursor migrator: an idle-marked row still has a live CLI behind it, so
  preflight must refuse it without `forceArchiveRunning`. A regression here
  would let the migrator transplant the ACP session directory out from under
  a running Cursor runner.
- Archive route: once a keepalive-idle session finally loses its socket it
  must stay archivable, rather than being stranded behind a 409.

Both tests were confirmed non-vacuous by reverting each guard to the
`=== 'running'` literal; each then fails.

No behaviour change.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

* test: stop the restart regression leaking its temp DB on failure

The file-backed store's `rmSync` was a trailing call, so any failing
assertion above it skipped cleanup and left a temp DB behind — one per failed
run. Noticed while verifying the test was non-vacuous, which deliberately
fails it.

Wrap in try/finally. Verified: a forced failure now leaves zero
`/tmp/hapi-1820-*` directories.

Test-only.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>

---------

Co-authored-by: HAPI <noreply@hapi.run>
2026-09-16 08:11:54 +08:00
..
2026-08-25 13:31:59 +08:00

hapi-hub

Telegram bot + HTTP API + realtime updates for hapi hub.

What it does

  • Telegram bot for notifications and the Mini App entrypoint.
  • HTTP API for sessions, messages, permissions, machines, and files.
  • Server-Sent Events stream for live updates in web and native clients.
  • Socket.IO channel for CLI connections.
  • Serves the web app from web/dist or embedded assets in the single binary; network-relay mode uses the separately hosted official web app.
  • Persists state in SQLite via bun:sqlite.

Configuration

See src/configuration.ts for all options.

Required

  • CLI_API_TOKEN - Base shared secret used by CLI, web login and native pairing. Clients append :<namespace> for isolation. Auto-generated on first run if not set.

Optional (Telegram)

  • TELEGRAM_BOT_TOKEN - Token from @BotFather.
  • HAPI_PUBLIC_URL - Public HTTPS URL for Telegram Mini App access. Also used to derive default CORS origins for the web app.

Optional (Voice)

Dictation and voice-assistant provider keys can also be added from Settings → Voice (stored in settings.json under providerCredentials; env vars still win when set at process start).

  • ELEVENLABS_API_KEY - ElevenLabs API key for voice assistant + dictation.
  • ELEVENLABS_AGENT_ID - Custom ElevenLabs agent ID (auto-created if not set).
  • GEMINI_API_KEY / GOOGLE_API_KEY - Gemini Live voice assistant.
  • DASHSCOPE_API_KEY / QWEN_API_KEY - Qwen Realtime voice assistant.
  • VOICE_BACKEND - Default assistant backend (elevenlabs, gemini-live, or qwen-realtime).
  • OPENAI_API_KEY - OpenAI dictation (gpt-transcribe / gpt-live-transcribe).
  • DEEPGRAM_API_KEY - Deepgram dictation (nova-3, standard and realtime).
  • GROQ_API_KEY - Groq dictation (whisper-large-v3).
  • TRANSCRIPTION_BASE_URL and TRANSCRIPTION_MODEL - OpenAI-compatible/local transcription endpoint and model.
  • TRANSCRIPTION_API_KEY - Optional bearer token for the OpenAI-compatible endpoint.

Optional

  • HAPI_LISTEN_HOST - HTTP bind address (default: 127.0.0.1).
  • HAPI_LISTEN_PORT - HTTP port (default: 3006).
  • CORS_ORIGINS - Comma-separated origins, or *.
  • HAPI_HOME - Data directory (default: ~/.hapi).
  • DB_PATH - SQLite database path (default: HAPI_HOME/hapi.db).
  • TELEGRAM_NOTIFICATION - Enable/disable Telegram notifications (default: true).
  • HAPI_RELAY_API - Relay API domain (default: relay.hapi.run).
  • HAPI_RELAY_AUTH - Explicit relay auth key. By default the hub obtains and persists an individually revocable key from the relay. A persisted key rejected with HTTP 403 is discarded and reissued once; an explicitly configured environment key must be updated manually.
  • HAPI_RELAY_FORCE_TCP - Force TCP relay mode (true/1).
  • VAPID_SUBJECT - Contact email/URL for Web Push.
  • HAPI_ANDROID_PUSH - auto (default: direct FCM when credentials are configured, otherwise relay), relay, fcm, or off.
  • FCM_SERVICE_ACCOUNT_PATH - Service-account JSON for private Firebase builds; the app must use the same project. Invalid configured credentials disable Android push instead of switching projects.
  • HAPI_IOS_PUSH - relay (default), apns, or off.
  • HAPI_PUSH_RELAY_URL - Shared Android/iOS push relay (default: https://push.hapi.run; persisted as iosPushRelayUrl). Independent of the --relay network tunnel.
  • HAPI_SESSION_IDLE_TIMEOUT_MS - Keep-alive-idle window in ms (default: 43200000 / 12 h; 0 disables). See "Session liveness" below.

Official native apps register their encryption keys automatically; no push provider setup is needed on a fresh hub. See the native companion push contract.

Running

Binary (single executable):

export TELEGRAM_BOT_TOKEN="..."
export CLI_API_TOKEN="shared-secret"
export HAPI_PUBLIC_URL="https://your-domain.example"

hapi hub

hapi server remains supported as an alias.

For web/native clients + CLI, you can omit TELEGRAM_BOT_TOKEN. To enable Telegram, set TELEGRAM_BOT_TOKEN and HAPI_PUBLIC_URL, start the hub, open /app in the bot chat, and bind the Mini App with CLI_API_TOKEN:<namespace> when prompted.

From source:

bun install
bun run dev:hub

HTTP API

The following is an overview. See the client contract for request/response shapes and error semantics, and src/web/routes/ for all endpoints.

Authentication (src/web/routes/auth.ts, src/web/routes/bind.ts)

  • POST /api/auth - Get JWT token (Telegram initData or CLI_API_TOKEN[:namespace]).
  • POST /api/bind - Bind a Telegram account using initData + CLI_API_TOKEN:<namespace>.

Sessions (src/web/routes/sessions.ts)

  • GET /api/sessions - List all sessions. Each summary includes hasConversationContent, derived from stored conversation messages (not titles or lifecycle events); full session SSE updates carry changes to this flag.
  • GET /api/sessions/:id - Get session details.
  • POST /api/sessions/:id/abort - Abort session.
  • POST /api/sessions/:id/switch - Hand off session control to the web.
  • POST /api/sessions/:id/resume - Resume inactive session.
  • POST /api/sessions/:id/reopen - Reopen a session; follow the returned session ID.
  • POST /api/sessions/:id/clear - Start a fresh conversation when supported.
  • POST /api/sessions/:id/upload - Upload file (base64, max 50MB).
  • POST /api/sessions/:id/upload/delete - Delete uploaded file.
  • POST /api/sessions/:id/archive - Archive active session.
  • PATCH /api/sessions/:id - Rename session.
  • DELETE /api/sessions/:id - Delete inactive session.
  • GET /api/sessions/:id/slash-commands - List slash commands.
  • GET /api/sessions/:id/skills - List skills.
  • POST /api/sessions/:id/permission-mode - Set permission mode.
  • POST /api/sessions/:id/model - Set model preference.
  • POST /api/sessions/:id/effort - Set effort for Claude, Grok, or Pi; other config controls are flavor/capability-gated (see the client contract).
  • GET/POST /api/sessions/:id/scratchlist - Read/create Hub-persisted scratchlist entries; update/delete and attachment routes share this prefix.

Messages (src/web/routes/messages.ts)

  • GET /api/sessions/:id/messages - Get messages (paginated).
  • POST /api/sessions/:id/messages - Send message.
  • POST /api/sessions/:id/messages/queued-state - Reconcile queued, indeterminate, and invoked local IDs.
  • DELETE /api/sessions/:id/messages/:messageId - Cancel a queued message.
  • POST /api/sessions/:id/messages/:messageId/steer - Steer a queued message when the session supports it.
  • POST /api/sessions/:id/messages/:messageId/retry - Explicitly retry indeterminate delivery when safe; never auto-replay.

Permissions (src/web/routes/permissions.ts)

  • POST /api/sessions/:id/permissions/:requestId/approve - Approve permission.
  • POST /api/sessions/:id/permissions/:requestId/deny - Deny permission.

Machines (src/web/routes/machines.ts)

  • GET /api/machines - List online machines.
  • PATCH /api/machines/:id - Set/clear the machine display name.
  • GET /api/machines/:id/agent-availability - List installed/configured Agents.
  • POST /api/machines/:id/spawn - Spawn new session on machine.
  • POST /api/machines/:id/list-directory - Browse runner-scoped directories.
  • POST /api/machines/:id/paths/exists - Check if path exists.
  • POST /api/machines/:id/restart-runner - Request runner restart (requires an available restart path).

Usage (src/web/routes/usage.ts)

  • GET /api/usage/summary - Get cache-aware token usage for the owner namespace (range=7d|30d|all).

Storage (src/web/routes/storage.ts)

  • GET /api/storage/sqlite - SQLite database/WAL/SHM sizes for the owner namespace.

Git/Files (src/web/routes/git.ts)

  • GET /api/sessions/:id/git-status - Git status.
  • GET /api/sessions/:id/git-diff-numstat - Diff summary.
  • GET /api/sessions/:id/git-diff-file - File-specific diff.
  • GET /api/sessions/:id/file - Read file content.
  • GET /api/sessions/:id/files - File search with ripgrep.

Events (src/web/routes/events.ts)

  • GET /api/events - SSE stream for live updates.
  • POST /api/visibility - Report client visibility state.

Voice (src/web/routes/voice.ts)

  • POST /api/voice/token - Get ElevenLabs conversation token.
  • GET /api/voice/backend - Discover configured assistant backends.
  • GET /api/voice/voices - List voices for the selected backend.
  • GET/PUT /api/voice/transcription/credentials - Read masked provider settings or update credentials (owner-only).
  • GET /api/voice/transcription/providers - List configured providers and supported modes.
  • POST /api/voice/transcription - Transcribe a bounded recording.
  • POST /api/voice/transcription/realtime-token - Mint a short-lived OpenAI, ElevenLabs, or Deepgram credential.

Push Notifications (src/web/routes/push.ts)

  • GET /api/push/vapid-public-key - Get VAPID public key.
  • POST /api/push/subscribe - Subscribe to push notifications.
  • DELETE /api/push/subscribe - Unsubscribe.

Native Android/iOS registration uses POST/DELETE /api/devices/register (src/web/routes/devices.ts); see the native push contract.

CLI (src/web/routes/cli.ts)

  • POST /cli/sessions - Create/load session.
  • GET /cli/sessions/:id - Get session by ID.
  • POST /cli/machines - Create/load machine.
  • GET /cli/machines/:id - Get machine by ID.

Socket.IO

See src/socket/handlers/cli/index.ts and src/socket/handlers/terminal.ts for event handlers.

Namespaces: /cli (raw CLI access token) and /terminal (client JWT). Ordinary web/native session updates use SSE, not the CLI Socket.IO namespace.

Client events (CLI to hub)

  • message - Send message to session.
  • update-metadata - Update session metadata.
  • update-state - Update agent state.
  • session-alive - Keep session active.
  • session-ready - Cursor ACP session/load (or newSession) succeeded; hub defers merge/dedup until this arrives on reopen.
  • session-end - Mark session ended.
  • machine-alive - Keep machine online.
  • rpc-register - Register RPC handler.
  • rpc-unregister - Unregister RPC handler.

Terminal events (web to hub, /terminal)

  • terminal:create - Open terminal for session.
  • terminal:write - Send input.
  • terminal:resize - Resize dimensions.
  • terminal:close - Close terminal.
  • agent-terminal:subscribe / agent-terminal:unsubscribe - Attach/detach the wrapped agent's terminal stream.
  • agent-terminal:input / agent-terminal:resize - Interact with that terminal when supported.

Hub events (hub to CLI clients, /cli)

  • update - Broadcast session/message updates.
  • rpc-request - Incoming RPC call.

See src/socket/rpcRegistry.ts for RPC routing.

Telegram Bot

See src/telegram/bot.ts for bot implementation.

Commands

  • /start - Welcome message with Mini App link.
  • /app - Open Mini App.

Features

  • Permission request notifications with approve/deny buttons.
  • Session ready notifications.
  • Deep links to Mini App sessions.

See src/telegram/callbacks.ts for button handlers.

Core Logic

See src/sync/syncEngine.ts for the main session/message manager:

  • In-memory session cache with versioning.
  • Message pagination and retrieval.
  • Permission approval/denial.
  • RPC method routing via Socket.IO.
  • Event publishing to SSE and Telegram.
  • Git operations and file search.
  • Activity tracking and timeouts.

Session liveness

Two separate signals, deliberately (tiann/hapi#1820):

  • active / activeAt - transport. The CLI socket is connected; session-alive refreshes it every ~2 s. sessionCache.expireInactive drops active after 30 s of silence.
  • metadata.lifecycleState - agent health. running -> idle -> running, plus archived.

Keep-alives prove a socket, not an agent. A session can therefore heartbeat for days with no messages, no thinking and no background tasks - active: true forever, which expireInactive can never catch. sessionCache.reconcileKeepaliveIdle (same 5 s tick) closes that gap: after HAPI_SESSION_IDLE_TIMEOUT_MS with no agent progress it moves lifecycleState running -> idle.

  • active is left alone on purpose. The CLI really is reachable, and flipping it would let resumeSession spawn a second agent against a live process, and unlock dedup-merge / delete on it.
  • Progress means a message in either direction, a queued prompt, or a background task - never a keep-alive. Sessions that are thinking, running background tasks or holding a pending permission request are never marked.
  • idle reverts to running on the next progress, within one tick.
  • metadata.idleReconcileExempt: true opts a session out entirely.

Storage

See src/store/index.ts for SQLite persistence:

  • Sessions with metadata and agent state.
  • Messages with pagination support.
  • Machines with runner state.
  • Todo extraction from messages.
  • Users table for Telegram bindings (includes namespace).
  • Scratchlist entries/attachments, usage, work graph, and push registrations.

Message content is stored via src/store/contentCodec.ts: oversized strings inside agent messages (giant tool output) are head+tail truncated at ingest, and payloads ≥256 chars are zstd-compressed (TEXT = plaintext JSON, BLOB = zstd). User prompts are never truncated — queued rows are delivered to the CLI verbatim.

Maintenance scripts (run with the hub stopped before swapping files):

  • scripts/compact-db.ts — retroactively truncate + compress + VACUUM an existing DB into a new file (the source is only opened read-only).
  • scripts/cleanup-sessions.ts — bulk-delete sessions by message count, path glob, or first-message pattern.

Source structure

  • src/web/ - HTTP service and routes.
  • src/socket/ - Socket.IO setup and handlers.
  • src/socket/handlers/cli/ - Modular CLI handlers.
  • src/telegram/ - Telegram bot.
  • src/sync/ - Core session/message logic.
  • src/store/ - SQLite persistence.
  • src/sse/ - Server-Sent Events.
  • src/config/ - Configuration loading and generation.
  • src/notifications/ - Push and Telegram notifications.
  • src/visibility/ - Client visibility tracking.

Security model

Access is controlled by:

  • Telegram initData verification plus bound Telegram users (bound via CLI_API_TOKEN:<namespace>).
  • CLI_API_TOKEN base secret for CLI and browser access (namespace is appended by clients).

Transport security depends on HTTPS in front of the hub.

Build for deployment

From the repo root:

bun run build:hub
bun run build:web

The hub build output is hub/dist/index.js, and the web assets are in web/dist.

Networking notes

  • Telegram Mini Apps require HTTPS and a public URL. If the hub has no public IP, use Cloudflare Tunnel or Tailscale and set HAPI_PUBLIC_URL to the HTTPS endpoint.
  • If the web app is hosted on a different origin, set CORS_ORIGINS (or HAPI_PUBLIC_URL) to include that static host origin.

Standalone web hosting

The web UI can be hosted separately from the hub (for example on GitHub Pages or Cloudflare Pages):

  1. Build and deploy web/dist from the repo root.
  2. Set CORS_ORIGINS (or HAPI_PUBLIC_URL) to the static host origin.
  3. Open the static site, click the Hub button on the login screen, and enter the hapi hub origin.

Leaving the hub override empty preserves the default same-origin behavior when the hub serves the web assets directly.