Skip to content

fix(session): fence late lifecycle settlements - #955

Merged
benvinegar merged 2 commits into
refactor/session-lifecycle-clockfrom
fix/session-lifecycle-generation-fences
Sep 1, 2026
Merged

benvinegar merged 2 commits into
refactor/session-lifecycle-clockfrom
fix/session-lifecycle-generation-fences

Conversation

@benvinegar

@benvinegar benvinegar commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Summary

  • add exact opaque generation identities across socket callbacks, authentication, reconnect preparation, timers, warnings, and command delivery
  • fence late startup credentials, daemon checks, incumbent probes, launcher probes, and metadata commits after stop or replacement
  • publish startup attempt identity inside the explicit lifecycle union before invoking foreign work
  • retire protocol authority before close settles, including close failures and handshake deadlines
  • preserve foreign bridge work and reservations until settlement while fencing stale response delivery
  • make registration replacement coherent when serialization synchronously rotates generations
  • fence both synchronous and asynchronous injected launcher results/errors while retaining lock release
  • centralize generation-owned handshake disposal and queue-pressure rejection without changing throw/order semantics

Stack

  1. test(session): characterize lifecycle interleavings #949 — lifecycle characterization
  2. fix(session): recover from socket startup failures #950 — synchronous socket-start rollback
  3. refactor(session): model startup lifecycle states #952 — explicit startup lifecycle states
  4. refactor(session): inject a lifecycle clock #953 — plain TypeScript lifecycle clock
  5. This PR: late-settlement and generation fences
  6. Next: real Node/Bun runtime exit fixtures
  7. Then: bounded lifecycle defect reporting

Base branch: refactor/session-lifecycle-clock. Review only the fifth commit for this PR.

Validation

  • focused lifecycle suites — 94 pass
  • full direct unit suite — 2,205 pass, 3 skip during the final fix sequence
  • bun run typecheck
  • changed-file formatting and diff checks
  • bun run lint
  • bun run deps:check
  • bun run test:session-broker-node — 3 pass
  • bun run check:pack
  • five adversarial review/fix rounds covering construction reentrancy, stale reconnect timers, opaque tokens, close failures, parser and serialization reentrancy, startup stop reentrancy, launcher sync/async errors, outbound delivery, reconnect factory failure, and transactional replacement
  • final independent acceptance review approved with no blocker/high/medium findings

Cancellation boundary

This PR fences commits; it does not claim cancellation. Foreign credential/crypto/probe/bridge promises continue until they settle. A detached process already synchronously started by an injected launcher cannot be recalled; stale metadata and cleanup commits are suppressed and launch locks are still released.

Compatibility

  • initial socket construction and explicit public startup failures remain synchronous
  • callbacks receive a frozen opaque generation token; callbacks that ignore the new trailing parameter remain source-compatible
  • socket factories must return a fresh object per generation because property-handler sockets cannot safely distinguish queued events after object reuse
  • no protocol, authentication transcript, stored-state, or delivery replay migration

This PR description was generated by Pi using gpt-5.6-sol

@vercel

vercel Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
hunk-web Ignored Ignored Preview Sep 1, 2026 3:36am UTC

Request Review

@benvinegar
benvinegar force-pushed the fix/session-lifecycle-generation-fences branch from 4f9fd28 to 45120db Compare August 31, 2026 19:56
@benvinegar
benvinegar marked this pull request as ready for review August 31, 2026 23:54
@greptile-apps

greptile-apps Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR adds opaque generation and startup-attempt identities to prevent late asynchronous work from mutating stopped or replaced session lifecycle state.

  • Fences authentication, reconnect preparation, callbacks, timers, command responses, and registration replacement by exact connection generation.
  • Prevents late daemon probes, credentials, launcher results, and metadata writes from committing after startup authority is revoked.
  • Adds extensive regression coverage for reentrancy, stale settlements, close failures, retries, serialization callbacks, and launcher cleanup.
  • Documents the fresh-socket requirement and commit-fencing semantics.

Confidence Score: 5/5

The PR appears safe to merge; no concrete changed-code defect remains after accounting for its documented cancellation and adapter boundaries.

Exact generation and startup-attempt checks consistently guard lifecycle commits, while regression tests cover the relevant synchronous, asynchronous, reentrant, and failure paths.

Important Files Changed

Filename Overview
packages/session-broker/src/connection.ts Introduces exact socket-generation ownership across authentication, close handling, reconnects, registration replacement, timers, queues, and command-result delivery.
src/session/broker/brokerClient.ts Adds startup-attempt and client-connection identities that fence late credentials, daemon checks, callbacks, warnings, and reconnect preparation.
src/session/broker/brokerLauncher.ts Fences synchronous and asynchronous launcher settlements while preserving lock release and suppressing stale metadata or timeout commits.
packages/session-broker/src/connection.test.ts Adds comprehensive lifecycle regression coverage for reentrancy, stale authentication, close failures, generation replacement, and response delivery.
src/session/broker/brokerClient.test.ts Exercises startup and connection fencing under stop, retry, credential, daemon-probe, and socket-construction races.
src/session/broker/brokerLauncher.test.ts Verifies stale launcher values and errors cannot commit while launch locks are still released.

Sequence Diagram

sequenceDiagram
  participant Client as SessionBrokerClient
  participant Launcher as Broker launcher
  participant Connection as Broker connection
  participant Socket as Socket generation
  participant Bridge as App bridge
  Client->>Launcher: Ensure daemon (startup attempt)
  Launcher-->>Client: Health/launch settlement
  Client->>Client: Verify attempt is current
  Client->>Connection: Start connection
  Connection->>Socket: Create fresh socket + generation token
  Socket-->>Connection: Open/authentication callbacks
  Connection->>Connection: Verify generation is current
  Connection->>Socket: Register session
  Socket->>Connection: Command
  Connection->>Bridge: Dispatch retained foreign work
  Bridge-->>Connection: Result settlement
  Connection->>Connection: Verify originating generation is active
  alt Generation remains active
    Connection->>Socket: Send command result
  else Stopped or replaced
    Connection-->>Connection: Suppress stale delivery
  end
Loading

Reviews (1): Last reviewed commit: "refactor(session): consolidate generatio..." | Re-trigger Greptile

@benvinegar
benvinegar force-pushed the fix/session-lifecycle-generation-fences branch from 45120db to fdca603 Compare September 1, 2026 03:33
@benvinegar
benvinegar force-pushed the fix/session-lifecycle-generation-fences branch from fdca603 to 4a1f892 Compare September 1, 2026 03:36
@benvinegar
benvinegar merged commit 9b5d419 into main Sep 1, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant