Skip to content

Cloud-native Phase 4: cross-process single-writer enforcement (session leasing) #60

Closed

Description

@JAORMX

Summary

Cross-process single-writer enforcement for sessions, via leasing. This is Phase 4 of the cloud-native arc (docs/design/CLOUD-NATIVE.md, decision (c) in List 3) and the last unbuilt piece. Phases 1-3 made the harness process disposable (snapshot fidelity, awaiting-approval evict/rehydrate, durable event log). The remaining gap is multi-replica safety: today "one process owns a session at a time" is a deployment constraint (session affinity), not something the code enforces.

Why this matters

Every place a writer exists assumes single-writer, and none of it guards across processes:

  • jsonlstore Save serializes through an in-process mutex and appends open-write-close. No atomic rename, no file lock, no cross-process guard; Load takes the last line. Two processes appending one session file interleave at the mercy of OS append atomicity.
  • The driver protocol is last-write-wins by contract: SaveRequest{session_id, snapshot}, no version, no CAS token, no lease.
  • Run-exclusion is in-process only: Service.runs stops two concurrent runs of one session within a process; loadAndReopen consults nothing cross-process. Two replicas can each load, reopen, run, and persist over each other.
  • The GC liveness predicate (Service.IsLive) sees only this process's runs (already recorded as a deferred gap in DRIVERS.md).

The sharp, concrete failure: this is the cross-process twin of the Phase 2 concurrent-approve bug. In Phase 2 the review panel caught two /approve requests both rehydrating an awaiting session and both executing the pending tool; we fixed that in-process with a per-session keyed mutex (resumeMu) plus Service.runs. Two replicas both rehydrating the same awaiting session and both applying the verdict is the identical double-execution across the process boundary, and only a lease can prevent it. Phase 3 raised the stakes: there are now two append-only durable artifacts per session (the snapshot and the event log), and concurrent writers corrupt the ordering of both.

Proposed design

A lease as an optional capability on the SessionStore seam, discovered by type assertion exactly like PrunableStore already is (so a backend that does not support leasing is simply never leased, and the local path stays byte-identical).

Rough shape:

type SessionLease interface {
    Acquire(ctx, id SessionID, owner string) (Lease, error) // fails if held by a live owner
    Renew(ctx, Lease) error
    Release(ctx, Lease) error
}
  • Enforced at the run-entry funnel (loadAndReopen): acquire before running; refuse to run if the session is owned elsewhere.
  • Crash recovery is TTL expiry: a dead owner's lease lapses and another replica can acquire. This is the Chubby / Kubernetes-Lease / ZooKeeper-ephemeral model.
  • Ties directly to the rehydration work: when a process dies, its lease expires, another replica acquires and rehydrates the session, and the cross-process double-execution is prevented because the second replica cannot acquire until the first releases or its lease expires.
  • Local jsonlstore: a flock-based single-host lease (flock is already in the tree for the memory store) or a documented no-op for single-process deployments.
  • Remote driver: the backend's atomic primitive (Postgres advisory lock, Redis SETNX+TTL, etcd lease).

Scope

Sessions first. The Phase 0 resource inventory (CLOUD-NATIVE.md List 1) confirmed sessions are the first and, for now, only resource that needs a lease, and the GC-liveness gap rides the same mechanism for free. This generalizes later to the resource-lease abstraction raised in PR #54 (subsystem #6: named/leased/scope-bound resources, with K8s owner-refs + finalizers + Lease, and Chubby/ZooKeeper ephemerals as prior art), but it does not have to start there.

Open questions

  • Lease granularity: per-session vs per-run.
  • TTL and renew interval relative to the rehydrate window and run length.
  • Lease-loss mid-run: an in-flight run that loses its lease must stop writing (fence the Save path), not just fail to renew.
  • Whether the GC liveness predicate should consult the lease rather than process-local run state.
  • Local impl: a real flock single-host lease, or a no-op plus documentation that single-writer is the operator's job until a lease-capable backend is configured.
  • Interaction with the HTTP-only rehydrate path from Phase 2 (gRPC Converse has no rehydrate path yet).

Acceptance criteria

  • A SessionLease (or extended SessionStore) optional capability, type-asserted like PrunableStore.
  • loadAndReopen acquires the lease and refuses to run a session owned elsewhere.
  • A conformance suite covering acquire / renew / release / expiry, run against the reference adapter and the gRPC driver over bufconn (the conformance-as-contract discipline from DRIVERS.md).
  • A cross-process test: two Builds over one store, the second cannot acquire while the first holds, can after release or TTL expiry.
  • The Phase 2 double-execution scenario, reproduced across two processes, is prevented by the lease.
  • Composition wiring (a flag), byte-identical when the backend does not support leasing.

Relationship

Non-goals

  • The generalized resource-lease abstraction for every resource type (start with sessions; generalize only when a second resource needs it).
  • Routing-layer session affinity (that is a deployment concern; this issue is the code-enforced backstop).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions