Skip to content

Make lease requests durable across restart #72

Closed

Description

@thymikee

Part of #70. See ADR 0021.

Purpose

Store each lease request before Simlock queues or provisions work.

A client must recover the same result after a disconnect or process restart. A retry must not create a second lease.

Simlock must keep this state in the core registry. A frontend must not own a second request registry.

Scope

Today a lease request exists only as a promise in memory. Nothing on disk names it, so a client that loses its connection cannot ask what happened, and a daemon restart drops every queued request without telling anyone. The HTTP frontend works around this with its own in-memory registry (LeaseRequestTracker), which socket clients cannot see and which dies with the process.

After this task the registry stores a lease request the way it already stores devices and leases. Repeating a request returns its stored result instead of an error. A restart leaves every request in a state a client can read. The HTTP tracker keeps its streaming and long-poll plumbing and stops keeping its own copy of the truth.

Required behavior

  • Store the request before queue admission and before any driver work.
  • Key a request on (requesterId, idempotencyKey).
  • Accept idempotencyKey as optional. Store a request that carries none; it simply cannot be replayed.
  • Return the stored result when a client repeats a request under the same key.
  • Attach a repeat of a request that is still open to the existing wait. Do not start a second one.
  • Return IDEMPOTENCY_CONFLICT when the same key arrives with a different device spec.
  • Store ownerId on the record. Refuse a replay from a different principal with FORBIDDEN.
  • Treat a capacity, disk, validation, or provisioning failure as terminal for that request.
  • Never re-evaluate a terminal result under the same key, whatever changed on the host since.
  • Admit a new key as a new request once the prior one is terminal.
  • Keep REQUESTER_ALREADY_LEASED as the answer when the prior request is still open under a different key.
  • Treat disconnect, request timeout, and abort as the end of that caller's wait only. None of them writes a result.
  • Write a cancellation only through an explicit cancel.
  • Settle every open request as terminal failed at startup. Say that the daemon restarted.
  • Keep a terminal record for a configured window, then prune it.
  • Cap the number of stored records.
  • Serve the same record to every frontend on the daemon.

Recovery is a repeat of the request, not a new operation. A client that lost its response sends the same request again and gets the same answer.

Technical spec

Modules touched

  • src/core/domain.ts — LeaseRequestRecord, its states, and the one predicate that answers whether a record is terminal.
  • src/core/registry.ts — the third record type beside devices and leases: create, settle, read by key, prune, and their #commit/#restore halves. A state file written before this task loads without a request list rather than failing to start.
  • src/core/lease-acquisition-coordinator.ts — write the record inside decisions.run before admission; replay a stored result; attach a repeat to a live waiter; settle the record wherever the request settles today.
  • src/core/startup-converger.ts — settle every non-terminal record as failed before admission opens.
  • src/core/config.ts — lease.requestRetentionMs and lease.maxRequestRecords, their defaults and validators.
  • src/contract/schemas.ts — the config block and the request record's own schema.
  • src/contract/operations.ts — idempotencyKey on lease.request.
  • src/contract/errors.ts — IDEMPOTENCY_CONFLICT.
  • src/daemon/dispatcher.ts — thread the key through lease.request.
  • src/http/tracker.ts — delete #requests, #idempotency, #leaseRequestId and their timers; read core state; keep SSE and long-poll.
  • src/simlock-client/client.ts, src/simlock-client/types.ts — the new input field.
  • docs/CONFIGURATION.md, docs/HTTP-API.md, docs/EVENTS.md, docs/internal/EVENTS.md, docs/internal/KNOWN-PITFALLS.md.

The record must be written inside the same serialized section that checks uniqueness. Two concurrent requests under one key both pass the check otherwise. This puts a state.json write inside SerializedDecision.run, against that class's own note about long I/O. Accept it: leasing a device costs seconds to minutes, and a file write does not register next to booting a simulator. Do not move it out without a measurement.

idempotencyKey is caller-supplied and so is requesterId, so the pair is entirely under the caller's control. The record's ownerId is what authorizes a replay — the same guard lease.cancel already applies through pendingRequestOwner, for the same reason.

Contract and event changes

  • lease.request gains an optional idempotencyKey. Additive; every existing caller keeps working.
  • IDEMPOTENCY_CONFLICT — new error code, kind: "domain", HTTP 409.
  • lease.rejected — reason gains daemon-restarted, emitted once per request settled by startup recovery. This widens a published vocabulary: record it in both EVENTS.md cuts, and say there that a consumer must tolerate a reason it does not know.
  • lease.requested — payload gains the request id, so an observer can correlate an event with a stored record.
  • No new operation. Replaying lease.request is how a client reads its request back.

Rules in play

  • architecture.md 8 — the daemon owns this state. A frontend renders it and stores nothing.
  • architecture.md 10 — one request per requester is already enforced in LeaseAcquisitionCoordinator. Replay reads that rule, it does not restate it.
  • architecture.md 12 — startup settlement exists so a request never survives in a state nobody drives.
  • safety.md — requesterId and idempotencyKey are claims. ownerId is what the daemon proved.
  • events.md 6, 8 — the lease.rejected widening and the lease.requested payload change are documented in this change.
  • testing.md — every test title below is a claim its body must prove.

Tests

  • A repeated request under the same key returns the first result and grants no second lease.
  • A repeat of an open request attaches to the existing wait instead of starting a second one.
  • A request is on disk before the wait queue has seen it.
  • A daemon restart settles an open request as failed and names the restart.
  • A terminal capacity failure stays terminal under the same key after capacity frees up.
  • A new key after a terminal result starts a fresh request.
  • The same key with a different device spec is IDEMPOTENCY_CONFLICT.
  • A replay from a different principal is FORBIDDEN.
  • Two concurrent requests under one key produce one record.
  • A disconnect ends the caller's wait and writes no result.
  • A request carrying no idempotency key is still stored and still settled by restart recovery.
  • A terminal record is pruned once the retention window passes.
  • The record cap evicts the oldest terminal record and never an open one.
  • A state file written before this task loads.
  • GET /v1/lease-requests/{id} answers from the core record after a restart.

Done when

  • LeaseRequestTracker holds no request state of its own.
  • A socket client that reconnects after a disconnect recovers its result by repeating the request.
  • docs/HTTP-API.md's lifecycle section no longer documents the 404 → re-request → 409 → GET loop, which only existed because the tracker died on restart.
  • docs/internal/KNOWN-PITFALLS.md's "The HTTP tracker and notice buffer are the last frontend-held state" entry is split: the tracker half is resolved, the notice buffer half stands on its own.
  • docs/internal/KNOWN-PITFALLS.md gains an entry for the gateway gap: a fleet request is durable only once it is routed to a worker, because a gateway persists nothing but its drained-worker list (ADR 0005, Decision 3). Extending durability to the gateway needs its own ADR.
  • Both EVENTS.md cuts carry the lease.rejected and lease.requested changes.

Out of scope

Depends on

Approval

  • Approved for delivery

Written by an agent.

Activity

  1. changed the title [-]Make detached lease requests durable, recoverable, and externally fenceable[/-] [+]Make lease requests durable across restart[/+] on Sep 1, 2026
  2. thymikee commented on Sep 2, 2026

    @thymikee
    MemberAuthor

    Update this contract before implementation: split requester attempt ordering from managed-identity lifetime.

    generation currently carries two meanings: a requester's attempt ordinal (supersession, REQUESTER_BUSY, expectedGeneration) and a managed identity's lifetime (ManagedIdentityStatus, activation, removal acknowledgement). These values must be represented separately. A reusable Android identity survives several requesters, and a #76 warm identity can be created and removed without ever having a requester, so an identity-scoped field has no requester to be scoped to.

    Required split:

    requestGeneration: number;       // scoped to requesterId, as today
    identityIncarnationId: string;   // scoped to deviceId, one per created identity

    Use the #74 provisioning intent id as identityIncarnationId. Mint it before the driver creates the device, store it on the committed DeviceRecord, and preserve it across Android reclaim and reuse. iOS fresh identity (#75) mints a new one per lease.

    Type changes:

    • LeaseRequestStatus: rename generation to requestGeneration; add identityIncarnationId?, present from the moment an identity is reserved. Host derives its execution-binding fence from (requesterId, identityIncarnationId, requestGeneration), so all three must be readable from the status.
    • SupersedeLeaseRequest.expectedGeneration becomes expectedRequestGeneration.
    • ManagedIdentityStatus becomes { deviceId, identityIncarnationId, state }.
    • getManagedIdentityStatus({ deviceId, identityIncarnationId }): identity-scoped.
    • acknowledgeManagedIdentityRemoval({ deviceId, identityIncarnationId }): identity-scoped, no requester fields.
    • confirmLeaseActivation({ requesterId, requestGeneration, identityIncarnationId }): request-scoped, since activation binds one request to one identity.

    The terminal-outcome and supersession rules are unchanged.

    Dependencies: apply the same contract to #74's lease-request origin and to the activation and removal proofs in #76 and #77.

  3. added
    task:draft
    and removed on Sep 10, 2026
  4. V3RON commented on Sep 10, 2026

    @V3RON
    Contributor

    Spec updated: scope cut to durable lease requests, Technical spec / Done when / Out of scope / Depends on / Approval added, supersession and the external-fence and managed-identity work moved to Out of scope.

    Written by an agent.

  5. added
    task:ready
    and removed
    task:draft
    on Oct 1, 2026
  6. github-actions commented on Oct 1, 2026

    @github-actions
    Contributor

    Now task:ready: approved, technical spec present, #71 closed.

  7. self-assigned this
    on Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

task:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions