Skip to content

Context

Context is a value that says who is acting, for which client, as part of which request. You pass it explicitly to use cases, lock keys and handlers. The framework never stores it in a static, a thread-local or a context.Context, so there is no ambient context to forget about.

public record Context(
ClientId client,
@Nullable PrincipalId principal,
@Nullable PrincipalKind principalKind,
@Nullable ApplicationCode application,
CorrelationId correlationId,
@Nullable String causationId,
@Nullable String executionId,
Set<String> permissions,
@Nullable Actor actor,
@Nullable String subjectName) { ... }
Field Required Meaning
client always the tenant the work is for, holding the platform’s client code (see below). Every aggregate carries one, and a use case refuses to write another client’s aggregate
principal no the user or service account acting; the audit row and event metadata record it
principal kind no; only with a principal USER (a person), SERVICE (a service account) or FIELD_WORKER (a driver, picker or yard worker who signed in with a PIN, see Field workers)
application no the application code
correlation id always follows a request into logs, events and jobs; generated when absent
causation id no the event or message that caused this work
execution id no the job or run this work belongs to
permissions no (empty) what the principal may do, in the platform’s form (ingestor:ingestion:manage)
actor no who is signed in as the principal: the operator behind an impersonated field request
subject name no; only with a principal the principal’s display name: a field worker’s, or a user’s when the token carries one. For showing and recording; it identifies nobody and grants nothing

Each is a validated wrapper, so a mix-up is a compile error rather than a bug.

Type Rule
client id [A-Za-z0-9._-]{1,100}. It never contains :, so it can sit in composite keys (lock keys, cache keys) without two clients colliding
principal id non-blank
application code [a-z][a-z0-9-]*
correlation id non-blank; generated as a random UUID

In Go they are fw.ClientID, fw.PrincipalID, fw.ApplicationCode, fw.CorrelationID, fw.CausationID, fw.ExecutionID and fw.Permission, each built by Parse... for input (returns an error) or Must... for literals (the fwvet analyser checks the literal at lint time). In Java they are the records ClientId, PrincipalId, ApplicationCode and CorrelationId.

A client id is a string wrapper with a name that suggests the platform’s internal client id. It is not. The platform has two names for a client: the generated id (clt_0HZ...) and the client code (also called its identifier: a short slug such as acme, matching [a-z0-9][a-z0-9-]*). The framework uses the code everywhere:

  • the dispatch receiver puts the code from X-FlowCatalyst-Client: {id}:{code} into the handler’s context;
  • the outbox writes the value as the payload’s clientCode (and in the row’s client_id column), which is how the platform learns whose event it is. When the context’s client is the platform’s (_platform), the payloads carry no clientCode: _platform names no client the platform could resolve, so the event and the audit row are stored platform-scoped.

Three things get called “client id” in the framework and the platform. Which one you hold decides where it may go:

What Looks like Where the framework uses it
The client’s code (identifier) acme: [a-z0-9][a-z0-9-]*, never contains _ the Context’s client; the outbox’s clientCode and client_id column; client-scoped lock keys; the part after : in the clients token entry and in X-FlowCatalyst-Client
The client’s platform id clt_0HZ... (at most 17 characters) the part before : in clients and in X-FlowCatalyst-Client; the session’s home client; the dispatch-jobs batch’s clientId (set from an explicit platform client id, never derived from the Context)
An OAuth client id (credentials) whatever the platform issued for the application the platform client authenticates the application’s service account with it; it names an application, not a tenant

Rules of thumb. A field of your own API or table that says which tenant a record belongs to should be named for what it holds (clientCode, or clientId only if it is the clt_ id) and should be the same kind as the Context’s client, so a filter and a Context compare equal. If a page has to show or accept the other kind, translate at the edge: the principal’s clients entry pairs the two (clt_1:acme).

So when you build a Context yourself, for example in a request resolver, put the client code in the client id. Putting a clt_... id there writes events the platform cannot attribute to the client you meant. Nothing in the type can tell the two apart, because both are valid strings.

Context context = Context.of(new ClientId("acme")).withPrincipal(new PrincipalId("usr_1"));

Context.of(client) generates a correlation id and leaves the rest empty. The with... methods return a copy: withPrincipal, withApplication, withCorrelationId, withCausationId, withExecutionId, withPermissions, withActor, withSubjectName. findPrincipal(), findPrincipalKind(), findApplication(), findActor() and findSubjectName() return Optionals. withPrincipal clears the kind, the actor and the subject name, which described the previous principal.

Work that is not a platform request but acts as a service, such as an inbound provider webhook your application verified itself, builds its context with the service constructor: the client, that principal with kind SERVICE, a generated correlation id. A use case that only a service may run says so in its authorisation step.

Context c = Context.ofService(new ClientId("acme"), new PrincipalId("svc:courier-webhook"));
c.principalKind(); // SERVICE

A use case that only a service may run says so with Authorize.SERVICE.

The client _platform is the client of work that acts for the platform rather than one tenant: a scheduled job’s context, a platform-scoped dispatch job’s, a request whose principal names no client. No real client code contains _. A dispatch job planned in this context names no client. In Java the constant is ClientId.PLATFORM (also Receivers.PLATFORM_CLIENT); in Go fw.PlatformClient.

A kind is never present without a principal. FIELD_WORKER is set only by the request resolver from a field access token, so a switch over the kind must name it (no default that treats it as USER). The framework sets the kind wherever it knows it: the auth module’s resolver from the session or the bearer token’s type, and the receivers always SERVICE, the application’s own service account. Check it in authorize when an operation is for people only or for the service only. It is not written to the outbox.

hasPermission(required) is true when a held permission matches: the same number of :-separated segments, and each held segment is * or equal to the required one. That is the platform’s rule, so a permission the platform grants (billing:*:view) means the same here.

Context c = Context.of(new ClientId("acme")).withPermissions(Set.of("billing:*:view"));
c.hasPermission("billing:invoice:view"); // true
c.hasPermission("billing:invoice:edit"); // false
c.hasPermission("billing:invoice:line:view"); // false: a wildcard is one segment, never more

A wildcard in the required permission grants nothing, and a blank one never matches. Use it in a use case’s authorize step, which sees only the Context and the command; a rule on the stored record is the loaded-authorisation step (Write path).

In a test or a command, build the context as above. In an HTTP request the context comes from a context resolver your application provides. The real one belongs to the auth module (Sessions and login): it fills the principal, the client (an identifier, as above) and the permissions from the session cookie or a platform bearer token, and answers 401 UNAUTHENTICATED when it cannot. After the resolver returns, the framework replaces the context’s correlation id with the request’s X-Correlation-Id (or the one it generated), so logs, events and the audit row agree.

A placeholder for tests that treats every request as one fixed user:

app.provide(
ContextResolver.class,
lookup -> request -> Context.of(new ClientId("demo")).withPrincipal(new PrincipalId("usr_demo")));

A scheduled job’s context is the platform’s (_platform) and a background loop has none, but the work goes through use cases, which are scoped to one client. forClient copies a context for another client and keeps the principal, its kind, the application, the correlation, causation and execution ids and the permissions. A fan-out helper runs a task for each client with that client’s context; one client’s failure is recorded for that client and the others still run, and it stops starting clients when the thread is interrupted (Java) or the context is done (Go).

// SlaSweep: the clients come from one deliberate, unscoped query that returns client codes only
List<ClientId> clients = TicketQueries.overdueClients(database, clock.instant());
Map<ClientId, PerClient.Outcome<int[]>> outcomes = PerClient.each(ctx, clients, this::sweepClient);
// sweepClient(Context forClient): scoped queries, then useCases.run(flagSlaBreach, forClient, command)

context.forClient(clientId) is the copy; PerClient.each(base, clients, task) returns an ordered map of outcomes.

forClient does not check that the principal may act for the client; the use case’s authorize does, as for any caller. The platform does not fire a job once per client: the application does the fan-out, so where the clients come from is yours (never from the platform’s client ids; build a client id from the code).

  • Use cases take a Context on every run. The client scopes every read and write; the principal and ids go into event metadata and the audit row. See Write path.
  • Locks can be client-scoped. See Locks.
  • Outbox rows use the client id value as the payload’s clientCode, except in the _platform context, where the payload has no clientCode.

Specification: Core.