Repository navigation
Description
Problem
McpSession caches its daemon connection in #connection
(#connectionForUse, src/mcp/session.ts) and never learns when that
connection dies. The field is cleared only by close() and one abort path;
nothing subscribes to the socket closing. When the daemon restarts or crashes,
the session keeps handing requests to a dead socket forever.
Observed in practice while verifying #12's sliding TTL: restarting the daemon
permanently bricked the running pitlane mcp process. Every subsequent tool
call returned
{"code":"INTERNAL","message":"Pitlane could not complete the request"}while the daemon itself was healthy (daemon status → health: "running")
and the CLI worked normally against it. Recovery required restarting the MCP
client, which for an editor-hosted agent means restarting the whole app.
The CLI is unaffected, but not because it is resilient — it is stateless.
requestOnce (src/cli/index.ts) connects, requests and closes per
invocation, and connectDaemon auto-starts the daemon when the socket is
stale, so a restart is invisible to it.
The information needed to fix this already exists one layer down:
IpcDaemonConnection wires onError / onClose and tracks #closed
(src/daemon-client/connection.ts), but that never reaches the session,
because DaemonConnection (src/daemon-client/protocol.ts) exposes only
request, onPush and close. A dead connection also rejects with a bare
new Error("Daemon connection is closed"), so toMcpErrorResult falls
through to its INTERNAL default — hence the opaque message.
Proposed change
- Add
onClose(listener): () => voidtoDaemonConnectionand implement it in
IpcDaemonConnectionalongside the existingonPush. - In
#connectionForUse, subscribe to it so a closed socket clears
#connectionand#pushUnsubscribe. The next call re-runs#connect(),
which re-auto-starts the daemon exactly as the CLI does. - Reject from a dead connection with a typed
DaemonClientError("DAEMON_CONNECTION_LOST", ...)instead of a bareError,
and have the session drop the cached connection and retry the request once.
This covers the socket dying mid-request, which the close listener alone
does not. - Surface a dead connection as a distinct, typed error rather than the generic
INTERNAL, and clear a stale#ownedLeasewhen the connection that owned it
is gone. (Both moved here from Expire held leases orphaned by a daemon restart #13, which now owns the daemon side only, so
that one issue ownssrc/mcp/session.ts.) - Decide what a reconnect means for a lease the session thought it held —
Expire held leases orphaned by a daemon restart #13 makes that decision, see Notes.
Notes
- Expire held leases orphaned by a daemon restart #13 is now settled and merged (fix(lease): release held leases orphaned by a daemon restart #38), so the dependency this issue used
to carry is discharged. The daemon releases every persistedheldlease at
startup with reasonorphaned, so a reconnecting session's old lease is
definitively gone. This issue therefore only clears#ownedLeaseand fires
onLeaseLost; no mechanism for re-attaching an existing lease to a new
connection is needed. - The useful invariant: connection death implies the held lease is gone.
Verified end to end while testing fix(lease): release held leases orphaned by a daemon restart #38, and true by three independent paths —
a gracefulpitlane daemon stopreleases held leases with reasonclosed
before exiting (DaemonServer.#stop→#releaseHeld); an ordinary
connection close releases that connection'sheldLeaseIdsthe same way; and
an ungraceful death (crash, OOM,kill -9, power loss) leaves the lease
persisted, which the merged startup path then releases asorphaned. There
is no path where the session's connection dies and its held lease survives,
so the reconnect never has to ask. - Whichever is chosen, the reconnect must not silently acquire a new device.
Losing a lease is a fact the agent needs told (Add a lease_status MCP tool and notify the agent when its lease is lost #15 built the channel);
re-leasing is the agent's decision. - Worth deciding whether a reconnect that finds no daemon should surface a
distinct code rather than reusingDAEMON_CONNECTION_LOST. - The heartbeat capability (Keep held leases alive while their holder is alive (heartbeat-negotiated sliding TTL) #12) is negotiated at
hello, so it is
re-declared for free by a freshconnectDaemon— no extra work needed.
Acceptance criteria
- After a daemon restart, the next MCP tool call succeeds against a fresh
connection rather than failing. - A session holding a lease across a daemon restart ends up with
lease_statustelling the truth, instead of reporting a stale cached
lease, and does not claim ownership of a lease it can no longer release. - A connection that dies mid-request surfaces a typed
DAEMON_CONNECTION_LOST, notINTERNAL. - Reconnecting never implicitly acquires a device.
- Tests cover: restart between two calls, death mid-request, and a lease
held across a restart. -
pnpm checkgreen.
Activity
fix(mcp): reconnect the session after a daemon restart (#35)