pi-gateway serves as a drop-in replacement for the pi executable for
third-party pi UIs, backed by a background session service that outlives any
single client connection.
Three goals drive the design:
pireplacement over ssh. A third-party UI — in particular the Emacspilishfrontend connecting over TRAMP/ssh — invokes this instead ofpi --mode rpcon the remote host. If the ssh/TRAMP connection drops, the client process is killed, but the session keeps running on the server; reconnecting re-attaches to the same session instead of losing it.- One server, many integrations. The same server also serves other integrations — for example a Slack agent bot — that are not part of this repository. Those integrations talk to the gateway protocol.
- Multiple clients per session. The user can switch between pilish, Slack, or other integrations and continue the same work in the same session.
The server is intended to run as a background service (e.g. a systemd user
unit) on the machine that owns the sessions, and to accept local connections
from clients. Clients are the processes third-party UIs spawn in place of pi.
Status. The design is settled and M1–M3 are implemented: the
pi-gatewayddaemon and thepi-gatewaybridge, with token auth and capability roles, port discovery, session attach/rebinding by path or name, the session catalog, reload and fork/clone policies, turn-author UI routing, the daemon-owned queue, replay/durable resume, coalescing, hibernation, orphan reaping, a systemd user unit, the read-only debug listener (/status/catalog/metrics), and structured logging. Scope, the decision log and what is deliberately deferred live indocs/development.md.
pi cannot be driven by multiple clients on the same session, and a pi
process dies with its client. Evidence from the installed
@earendil-works/pi-coding-agent:
- RPC mode is a single stdin/stdout pipe.
pi --mode rpcreads one JSONL command stream from stdin and writes one JSONL event/response stream to stdout (dist/modes/rpc/rpc-mode.js,dist/modes/rpc/jsonl.js). There is nolisten()/ server / socket anywhere indist/. One process == one client. - The agent loop is single-streaming.
promptwhile streaming withoutstreamingBehavioris rejected;session.isStreamingis a single flag. A second client cannot run a concurrent turn (seedocs/rpc.md). - The session file has no multi-writer safety.
SessionManagerkeepsfileEntries/byId/leafIdin memory, appends withappendFileSync, rewrites the whole file on branch/migration (_rewriteFiletruncates), and usesopenSync(path, "wx")(O_CREAT|O_EXCL) on first flush (dist/core/session-manager.js). Twopiprocesses on one session file diverge and clobber each other. - No late-join/replay/fan-out. Events are written once to the one stdout; there is no event log, cursor, or subscription mechanism.
pi does support queued participation within one client
(steer / follow_up / streamingBehavior), but that is queueing, not
multi-client parallelism. So: true parallel turns on one session are not
possible without changing pi. What is possible — and what this design
provides — is concurrent participation with a single ordered view, by
keeping exactly one Pi SDK AgentSession as the session's sole writer and
multiplexing many clients around it.
Prior art that solves a related part of the problem: marcfargas/pi-server
and tryingET/pi-server (detach/reconnect, one client at a time). This project
targets the broader goal above: many concurrent clients, a session that
survives client death, and non-pi integrations.
| File | Contents |
|---|---|
docs/design.md |
Full architecture, invariants, arbitration, replay, failure modes |
docs/protocol.md |
Client-facing wire protocol (pi-compatible + thin gateway envelope) |
docs/development.md |
Decision log, implementation status, repository layout, building and testing |
test/e2e/README.md |
The Emacs + pilish local-provider end-to-end suites |
typescript/bin/pi-gatewayd.ts |
TypeScript daemon: owns Pi SDK sessions and serves the gateway protocol on loopback TCP |
typescript/bin/pi-gateway.ts |
TypeScript bridge: the executable UIs spawn in place of pi |
gwclient |
retained Go protocol client library, with examples/chat as a runnable example |
third-party UI (pilish over ssh) ─┐
Slack bot / other integration ────┼── loopback TCP ──► pi-gatewayd (daemon)
another pilish tab ───────────────┘ (token auth) │
├─ SessionActor: one per session, sole pi writer
├─ Hub: ordered event log (seq) + replay ring
├─ PromptQueue: daemon-owned per-client FIFO
└─ UIBroker: extension_ui_request routing
▼
Pi SDK AgentSession for session X
(survives client disconnects)
- Clients speak pi's RPC protocol verbatim; a thin
gw_*envelope adds multi-client features for non-pi integrations. - One Pi SDK
AgentSessionper gateway session ⇒ Pi's session invariants are preserved, and the session file has exactly one writer. - A session outlives its clients: the daemon keeps pi running across ssh drops, and clients re-attach.
- All commands are linearized by the actor; all events get a global
seq. - Late joiners / reconnects replay from a
seqcursor, or resync via a snapshot. - Concurrent prompts are queued by the daemon (per-client tagged FIFO, one
turn at a time); any client may prompt and
steerinterjects immediately.
M1 (session core), M2 (multi-client sharing) and M3 (operations) are in:
pi-gatewayd + pi-gateway, one pi process per session, attach by path or
name, the daemon-owned prompt queue, replay/durable resume, token roles and
capabilities, the read-only debug listener, structured logging, metrics, and a
systemd user unit. M4 (session groups across daemons, WebSocket transport) is
optional and not started. The per-area table and the reasoning belong to
docs/development.md.
# Requires Node.js >=22.19.0 and `pi` available on PATH.
npm install --global ./typescript
The daemon owns the pi sessions. On first start it creates its token under
~/.config/pi-gateway/ (mode 0600) together with the port file clients
discover it through; --state-dir moves that directory.
pi-gatewayd # text logs at info level
pi-gatewayd --log-format json --log-level debug # structured, verbose
install -Dm644 packaging/pi-gatewayd.service ~/.config/systemd/user/pi-gatewayd.service
systemctl --user daemon-reload && systemctl --user enable --now pi-gatewayd
journalctl --user -u pi-gatewayd -f
The unit is Type=simple with Restart=on-failure and ExecReload sending
SIGHUP (which re-reads tokens.json without restarting).
A UI spawns the bridge instead of pi. --mode rpc (which pilish appends) is
accepted and consumed, and the accepted pi parameters are forwarded to the
daemon.
pi-gateway --mode rpc --approve # what a UI runs
pi-gateway --version # the Pi version, answered by the daemon
Point pilish-executable (local) at the pi-gateway binary, or the layer's
pilish/remote-executables path (remote). Do not shadow the real pi:
configure the UI's executable setting instead.
A second loopback listener serves read-only operational data with no token.
curl -s 127.0.0.1:7332/status # version, uptime, sessions, clients, queue
curl -s 127.0.0.1:7332/catalog # same rows as gw_list_sessions
curl -s 127.0.0.1:7332/metrics # Prometheus text
It exposes session paths, names, and titles to any local process, so keep it on
loopback and treat it as read-only. --no-debug disables it.
# Mint a token for one integration, then reload in place.
./pi-gatewayd --provision-token --token-name slack --token-role operator
systemctl --user reload pi-gatewayd # or: kill -HUP <pid>
The daemon-generated token always grants everything. Restricted tokens live in
~/.config/pi-gateway/tokens.json (mode 0600) and name a preset role
(admin, operator, observer) or an explicit capability list. An observer
may read (list sessions, get_*, export) and receive events, but cannot
prompt, steer/abort, switch_session, or run bash; shared-state
mutations need control. docs/protocol.md §10 maps every
command.
Pick the role for what the integration does, not for what it reads:
operatorattaches to existing sessions, prompts, steers, aborts, and answers dialogs, but cannot create sessions, name them, or change the model.admin(the daemon-generated token's level) is needed forgw_new_session,gw_stop_sessionandgw_delete_session, so a bot that creates, releases or destroys its own sessions, names them (set_session_name), or reconfigures them (set_model,compact, …) needsadminor an explicit--token-capslist containingadmin/control.
The gateway protocol is also the integration API: non-pi clients (a Slack bot,
a dashboard) connect to the daemon directly instead of going through the
bridge. docs/protocol.md is the reference; the shapes an
integration needs first:
gw_list_sessions -> {"sessions":[{path,name,title,cwd,live,spawn,clients,...}]}
gw_new_session -> {"path","name","sessionId"} (the requester is rebound)
gw_reload_session-> restart pi for a session (control capability;
piArgs replaces its recorded spawn configuration)
A chat integration with a standing instruction of its own should put it in the
prompt the first time it sends a message in a session, wrapped in
integration-specific markers, for example
<slack-specific-instructions>…</slack-specific-instructions>. The agent then
knows what the markers mean for the rest of the session, and the text is
visible to every client looking at the transcript. There is deliberately no
gateway command that writes into the context behind the participants' backs:
pi has no client-reachable primitive for it, and a speculative command that
can only answer not_supported is worse than none. A session the bot creates
can instead be installed at spawn with
gw_new_session{piArgs:["--append-system-prompt", …]}, which is durable
across hibernation and daemon restarts (see §4.3 of the protocol).
A Go integration can import the client library instead of implementing the
connection loop itself. It is deliberately outside internal/, so another
module can depend on it:
import "github.com/tigersoldier/pi-gateway/gwclient"
c, err := gwclient.Dial(ctx, gwclient.Config{Name: "slack", Kind: "bot"})
if err != nil {
return err
}
defer c.Close()
sess, err := c.NewSession(ctx, gwclient.NewSessionRequest{Cwd: "/work/repo"})
if err != nil {
return err
}
if _, err := c.Prompt(ctx, "summarise the open issues"); err != nil {
return err
}
for ev := range c.Events() {
if ev.Type == "message_end" {
// render the assistant message into the chat
}
}
gwclient covers discovery and the handshake, id-correlated Do/Send, the
event stream, and session/catalog/prompt/interject/dialog helpers. For a
long-lived bot it also provides:
- Reconnect and resume —
Cursor/LastSeq/LeafIDreport what the client has consumed;Reconnectdials again with that cursor and the daemon answers with a replay window or agw_snapshotwhen the cursor is unreplayable (Welcome.ResyncRequired). Persist the cursor to survive a process restart. - Callback delivery —
Config.OnEventhands every frame to a function on the read goroutine, so a bot does not need a drain goroutine and cannot overflow the event buffer mid-turn. - Typed events and dialogs —
Event.Turn/Queue/Snapshot/SessionStateand friends, plusEvent.UIRequestwithBlockingUIMethod, so extension dialogs (tool approval,confirm/select/input/editor) can be routed without hand-rolled JSON. - Images —
Prompt,SteerandFollowUpacceptprotocol.ImageContent(base64 + MIME type) for screenshot/attachment messages. - The rest of pi's surface — typed wrappers for models, thinking levels,
steering/follow-up modes, compaction, auto-retry,
bash(with streamedBashUpdatecallbacks), entries/tree/forks/stats/export, andset_session_name. - Releasing and destroying sessions —
StopSessiondisposes the SDK session and keeps the file (a later attach restores it),DeleteSessiondisposes the SDK session and removes the file (irreversible). Both refuse a running turn unlessforceis set; a delete also notifies every attached client withgw_session_state{state:"deleted"}and unbinds them without closing their connections, andgwclientclearsSession()so the next command cannot silently land in a new session.
A bot that creates its own sessions needs a token with admin (see
Restricted tokens); one that only attaches to sessions it
finds in the catalog can use operator.
The TypeScript runtime requires Node.js >=22.19.0 and resolves the pi
executable from PATH; the daemon has no --pi override. A runnable Go
client example remains available at examples/chat:
go run ./examples/chat gives streaming rendering, !queue/!steer/!abort,
and /name for commands, prompt templates and skills.
| Flag | Default | Meaning |
|---|---|---|
--listen <host:port> |
127.0.0.1:7331 |
loopback address for the session listener |
--port <n> |
— | port shorthand; overrides --listen |
--state-dir <dir> |
~/.config/pi-gateway |
where token, tokens.json, port, debug-port and the per-session spawn/ records live |
--token-file <path> |
<state>/token |
token file (mode 0600) |
--tokens-file <path> |
<state>/tokens.json |
restricted-token file (mode 0600) |
--idle-timeout <dur> |
15m |
keep a session warm this long after the last client detaches |
--short-grace <dur> |
10s |
grace for a session that never received a message and has no clients |
--session-dir <dir> |
— | extra session directory to scan for the catalog (repeatable) |
--delta-flush <dur> |
50ms |
coalescing window for streaming deltas, per client |
--log-level <level> |
info |
debug, info, warn/warning, error (case-insensitive) |
--log-format <fmt> |
text |
text or json |
--debug-addr <host:port> |
127.0.0.1:7332 |
read-only, unauthenticated debug listener |
--debug-port <n> |
— | port shorthand; overrides --debug-addr |
--no-debug |
off | disable the debug listener |
--provision-token |
— | mint a restricted token and print it (needs --token-name and --token-role or --token-caps) |
--token-name <name> |
— | name for --provision-token |
--token-role <role> |
— | admin, operator or observer |
--token-caps <csv> |
— | explicit capability list instead of a role |
--version |
— | print the daemon's own version |
SIGINT/SIGTERM dispose all managed Pi SDK sessions; SIGHUP re-reads
tokens.json without restarting. Both listeners refuse
non-loopback addresses. The state directory also follows PI_GATEWAY_CONFIG_DIR
for the daemon (and XDG_CONFIG_HOME when that is unset).
| Flag | Meaning |
|---|---|
--server <host:port> |
daemon address (default: the port file, then 127.0.0.1:7331) |
--port <n> |
daemon port on 127.0.0.1 |
--token-file <path> |
token file (default <state>/token) |
--mode rpc |
accepted and consumed; any other mode is an error |
--version |
print the managed pi version (answered by the daemon) |
--help, -h |
client-local usage |
It also accepts the pi options listed in docs/protocol.md §4.3 and forwards
them to the daemon; anything else is refused before connecting. Environment:
PI_GATEWAY_CLIENT_NAME (default pi-gateway), PI_GATEWAY_CLIENT_KIND
(default pilish), PI_GATEWAY_CONFIG_DIR. Exit codes: 0 success, 2 usage
error (bad flag, unsupported pi option), 3 daemon unreachable or handshake
refused.
The tree, and what each package owns, is in
docs/development.md.