Skip to content

Make the current client the source of truth for the client id - #21

Merged
loevgaard merged 4 commits into
2.xfrom
client-resolution
Nov 9, 2026
Merged

loevgaard merged 4 commits into
2.xfrom
client-resolution

Conversation

@loevgaard

@loevgaard loevgaard commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Summary

The current client is now the source of truth for the client id. The client cookie always holds the id of the client that ClientContextInterface returns, decorators included. See ADR 0001.

 ClientContextInterface → setono_client.client_context.default
-  [app decorators, priority 0: outside the memo]
-  CachedClientContext                memo cleared by kernel.reset
-    CookieBasedClientContext         decorates DefaultClientContext
-      CachedCookieProvider           WeakMap per Request
-        RequestBasedCookieProvider   parses + validates the cookie
-      DefaultClientContext           new client
+  CachedClientContext                WeakMap per Request; decorates the interface at -1024, so it wraps app decorators;
+                                     a kernel.terminate subscriber, so it keeps the request for terminate listeners
+    [app decorators, on the interface or on setono_client.client_context.default]
+      CookieBasedClientContext       (.cookie_based) cookie → client, else new client; no request → anonymous client
+        ClientCookieJar::read()      @internal, owns name/format/validation/expiry

 StoreCookieSubscriber (kernel.response)
   PreStoreCookieEvent veto
-  cookie = cookieProvider.getCookie() ?? new Cookie(context.getClient().id)   // cookie wins
-  write cookie (bump last seen)
+  cookieJar.write(request, response, context.getClient())                    // current client wins
+    same id as the request's cookie → keep first seen, bump last seen
+    other id                        → fresh timestamps
+    not valid (empty, > 255 bytes)  → LogicException
  • StoreCookieSubscriber, StoreMetadataSubscriber and ClientValueResolver inject the interface alias, so they see app decorators and the cache.
  • kernel.terminate listeners still get the request's current client. Symfony pops the request before that event, so CachedClientContext keeps it, via a WeakReference, between its own terminate listeners at priority 2048 and -2048. Because of that tag, decorating setono_client.client_context.cached itself fails to compile, and the docs say to decorate the interface or .default instead.
  • The PR also adds GLOSSARY.md, docs/adr/0001-… and UPGRADE-2.0.md, updates the README and CLAUDE.md (including the service id convention), and drops symfony/service-contracts.

Evidence

  • Before (1.x): I ran DecoratedClientContextTest against 1.x, with a decorator that names the client by a query parameter:
    ✘ decorator chooses the current client         cookie id '01a19ca1-…' ≠ 'decorated-client-id'
    ✘ cookie rewritten for the current client      cookie id 'cookie-client-id' ≠ 'decorated-client-id'
    ✘ metadata of a per-call client is stored      no row stored
    ✘ invalid current client id is refused         no exception
    
    After: all of them pass, for decorators of the interface and of setono_client.client_context.default. Moving the cache inside the decorator (priority 1024) makes the metadata test fail again.
  • KernelTerminateTest: a terminate listener gets the current client, including when neither the route nor the vetoed cookie subscriber used it (StoreMetadataSubscriber always does). If CachedClientContext ignores terminate, both tests fail with a random anonymous id.
  • AnonymousClientTest: outside a request, two calls give two anonymous clients.
  • Full suite on PHP 8.4: 121 tests OK, PHPStan max clean, ECS clean, Infection MSI 100% (107/107), composer validate --strict and the dependency analyser clean. The CI matrix (8.1/8.2, lowest) wasn't run locally.

Merge Danger

Door: two-way

The PR targets the unreleased 2.x line, so a revert undoes it. The BC check is skipped for PRs into 2.x.

Blast Radius: consumers

When 2.0.0 ships, three groups of apps are affected:

  • Apps that use CookieProviderInterface or DefaultClientContext, or that inject setono_client.client_context.default, need changes.
  • Apps whose decorators return a different client id will see the client cookie rewritten.
  • Code outside a request that relied on repeated calls returning the same anonymous client gets a new one on each call.

UPGRADE-2.0.md covers each case.

The client cookie now always holds the client id of the current client,
i.e. what ClientContextInterface returns, decorators included. Before, a
request with a cookie kept the cookie's id and the cookie writer never
asked the client context.

ClientContext recognizes the client by the cookie of the main request and
replaces CookieBasedClientContext and DefaultClientContext.
CachedClientContext decorates the interface with priority -1024, so it
wraps the application's decorators, and keeps the clients in a WeakMap
keyed by the request, so it needs no kernel.reset. An internal
ClientCookieJar owns reading and writing the cookie and replaces the
cookie providers and CookieProviderInterface. Without a request, every
call gets an anonymous client.

Adds GLOSSARY.md, ADR 0001 and UPGRADE-2.0.md.
…indings

Symfony removes the request from the request stack before it dispatches
kernel.terminate, so terminate listeners got an anonymous client. The
internal MainRequestProvider also returns the request being terminated,
and both client contexts use it.

Renames ClientContext back to CookieBasedClientContext, registered as
setono_client.client_context.cookie_based and aliased by
setono_client.client_context.default. Moves the cookie jar to
setono_client.cookie.client_cookie_jar, and clarifies the service id
convention in CLAUDE.md.

Adds functional tests for a decorator of the service id and for
kernel.terminate. Uses the glossary's terms (valid, current client,
anonymous client) and says bytes where the code counts bytes. Fixes the
UPGRADE snippet for reading the cookie, and notes that
setono_client.client_context.default no longer includes the cache.
Only the cache needs the request while kernel.terminate runs, as its key:
StoreMetadataSubscriber asks for the current client on
kernel.finish_request of every main request, so terminate listeners get it
from the cache. CachedClientContext now subscribes to kernel.terminate
itself, and CookieBasedClientContext reads the RequestStack again.

Removes MainRequestProvider. Because Symfony moves a decorated service's
tags to its decorator, decorating setono_client.client_context.cached
itself now fails to compile; the docs say to decorate the interface or
setono_client.client_context.default.
…decoration assertion

In v4.3.1, assertContainerBuilderServiceDecoration() passes the service definition to the constraint instead of the container, so it always throws. The lowest CI jobs installed v4.3.1. The DI test 5.x requires symfony-config-test ^5.0, so its ^4.3 alternative was dead too.
@loevgaard
loevgaard merged commit 78452de into 2.x Nov 9, 2026
19 checks passed
@loevgaard
loevgaard deleted the client-resolution branch November 9, 2026 13:40
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