Repository navigation
Description
Problem
A held lease's deadline is set once, at grant, and nothing ever extends it:
src/core/lease-lifecycle.ts:53—ttlDeadline: clock.now() + this.#ttlFor(input.mode)src/core/config.ts—lease.heldTtlBackstopMsdefaults to 1 hoursrc/core/lease-lifecycle.ts:77—renew()throws for held leases- README (MCP section) — MCP deliberately exposes no lease renewal
src/core/lease-expiry-scheduler.ts:28— expiry fires purely on the clock,
with no liveness check on the holding connection
At T+60min the lease is released with reason expired
(src/core/lease-release-coordinator.ts:65), the device is reclaimed — i.e.
erased (iOS simctl erase, Android snapshot restore) — and becomes
available to other requesters, while McpSession.#ownedLease still believes it
owns the lease. PR #25's lease-lost push does tell the agent after the fact,
but the device is already gone and wiped.
This breaks the primary use case: an agent installs an app on a leased device,
then works on a feature for hours while continuously testing against it.
Why not simply raise the backstop
The backstop's actual job is "detect a holder that stopped being alive", not
"cap how long work may take". A larger constant only trades device yanked
mid-work for crashed agent squats a device all day — which is the problem
Pitlane exists to solve.
Proposed change
Make the TTL a sliding window driven by an application-level heartbeat,
negotiated as a capability so it can be enabled per frontend.
Pitlane has no users yet, so breaking changes are in scope: bump
DAEMON_PROTOCOL_VERSION to 2 and do not write compatibility shims for
older clients or for state files carrying the old event shape.
Handshake
hello gains an optional client capability flag
(src/daemon-client/connector.ts, DaemonServer.#handleHello in
src/daemon/server.ts):
{ "clientVersion": "1.0.0", "protocolVersion": 2,
"capabilities": { "heartbeat": true } }Unknown capability keys are ignored; a missing capabilities object means no
capabilities. Store the parsed flag on Connection.
Ping/pong wire format
The existing protocol has no server→client request channel: push frames carry
no id (src/daemon-protocol/index.ts), and the client→server direction is
request-only. So the pong needs an explicit return path:
- Server pushes
{ push: "lease.heartbeat", payload: { nonce } }to every
connection that holds at least one lease and declared the capability,
everylease.heartbeatIntervalMs. IpcDaemonConnection(src/daemon-client/connection.ts) answers with an
ordinary request frame{ type: "lease.heartbeat", payload: { nonce } }.
Handling it there — not in MCP — means any frontend inherits it by declaring
the capability.DaemonServerhandles that request like any other command, slides the
deadline of every lease inconnection.heldLeaseIds, and acks with the slid
deadlines (see "Keepinglease_statustruthful" below).
A lease.heartbeat request from a connection that never declared the
capability is a BAD_REQUEST.
Sliding the deadline
Do not call expiryScheduler.replace(...) directly from the daemon: that
slides the timer while leaving the persisted ttlDeadline stale, so
restoreExpiryTimers() (src/core/lease-expiry-scheduler.ts:47) would expire a
healthy lease immediately after a daemon restart. LeaseLifecycle.renew cannot
be reused either — it throws HeldLeaseRenewalError for held leases.
Add LeaseLifecycle.heartbeat(leaseId) instead, which goes through
registry.renewLease(leaseId, now + heldTtlBackstopMs), emits lease.renewed,
and then expiryScheduler.replace(renewed) — keeping the registry the source of
truth. One registry write per lease per interval is negligible.
Pure sliding window — no missed-pong fail-fast
Liveness is expressed entirely by not sliding. A holder that stops ponging
simply reaches its existing backstop deadline and expires exactly as today, so
there is no missed-pong counter, no heartbeatMissTolerance config, and no new
expiry reason to distinguish. Connection close continues to release held leases
as it does now, and PR #25's lease-lost push continues to notify the holder.
Observability
Two different questions, two different answers — do both:
- History ("was this holder alive at 14:20?") —
heartbeat()emits
lease.renewedper beat. At a 5-minute interval this is 12 events/hour/lease
against the 1000-sloteventBuffer.capacityring, which is affordable; at 30s
it would wrap the buffer in under 3 hours and evict the device-lifecycle
events that make the log worth reading. This is why the interval default is
5 minutes, not 30 seconds. - Current state ("is it alive right now?") — surface a derived
lastHeartbeatAtinpitlane statusandpitlane list --leases. It needs
no new field onLeaseRecord: sinceheartbeat()writes through the
registry,lastHeartbeatAt === ttlDeadline - heldTtlBackstopMs. Unlike the
event log this survives daemon restart and buffer wrap.
Keeping lease_status truthful (interacts with merged PR #25)
McpSession caches expiresAtMs once at grant (src/mcp/session.ts:279) and
lease_status returns it from that cache without a daemon round-trip
(src/mcp/session.ts:439) — deliberate in PR #25, but permanently stale
under a sliding TTL. An agent polling lease_status after a context compaction
would see an expiry in the past and conclude it had lost a device it still
holds.
Fix inside this change: the daemon's ack to the lease.heartbeat request
returns the slid deadlines (e.g. { leases: [{ leaseId, ttlDeadline }] });
IpcDaemonConnection surfaces them; McpSession refreshes
#ownedLease.expiresAtMs. lease_status stays a local read, which was the
point of the PR #25 design.
Clock injection
DaemonServerOptions (src/daemon/server.ts:46) has no Clock. Add one and
thread it through the three construction sites (src/daemon/main.ts:98,
src/daemon/server.test.ts:497, src/cli/index.test.ts:715) so the heartbeat
interval is driven by the injected clock and testable with FakeClock.
Scope: MCP declares it, the CLI does not (yet)
Deliberate, and the reason is a trap worth stating explicitly. A sliding TTL
makes lease lifetime equal "the holder process is alive":
| Frontend | Holder dies with the agent? | Sliding TTL safe? |
|---|---|---|
| MCP | Yes — the client spawns pitlane mcp over stdio; client death closes stdin → EOF → session.close() (src/mcp/main.ts) |
Safe |
| CLI held mode | No — backgrounded and reparented; see docs/known-pitfalls.md |
Would convert a bounded 1h leak into an unbounded one |
So the heartbeat does not subsume the orphaned-holder problem — a reparented
holder happily pongs forever. The CLI declares the capability only after the
parent-death watch lands. Until then, CLI held mode uses manual renewal.
Acceptance criteria
- An MCP-held lease survives well past
heldTtlBackstopMswhile the MCP
process is alive (deterministic test with the fake clock and scripted IPC). - Killing the MCP process expires the lease within one backstop window.
- A connection that does not declare the capability behaves exactly as
today, and is never pinged. - A heartbeat slides the deadline in the registry, so a daemon restart
mid-lease restores the slid deadline rather than the grant-time one. - After a heartbeat,
lease_statusreports the slidexpires_at_mswithout
a daemon round-trip (regression guard for PR Add lease_status MCP tool and lease-lost notification #25's local-read design). -
pitlane status/pitlane list --leasesshow a derived
lastHeartbeatAtfor held leases. -
DAEMON_PROTOCOL_VERSIONis2; clients announcing1are rejected by
the existing mismatch path. -
lease.heartbeatIntervalMsdefaults to 5 minutes, is validated in
loadConfig(positive integer, and<= lease.heldTtlBackstopMs / 4), and
is documented in the README config table. -
docs/EVENTS.mdreflects the newlease.expiredsemantics ("backstop
fired without a heartbeat") and thatlease.renewedis now also emitted
per heartbeat, per events rule 8. -
docs/ARCHITECTURE.md"Leases" section describes the sliding TTL and why
it is capability-gated. -
pnpm checkgreen.
Activity