Skip to content

Service Context

service_context

Immutable context carried through every application-service call.

ServiceContext dataclass

Immutable context carried through every application-service call.

Parameters:

Name Type Description Default
correlation_id UUID

A unique identifier that traces the entire
request/response lifecycle. Auto-generated when omitted.

uuid4()
user_id str | None

The identifier of the authenticated user, if any.

None
permissions list[str]

The permissions held by the current user.

list[str]()
metadata dict[str, Any]

Arbitrary key-value pairs for cross-cutting concerns
(tracing, feature flags, tenant id, etc.).

dict[str, Any]()
Source code in src/forging_blocks/foundation/context/service_context.py
@auto_freeze
@dataclass(frozen=True)
class ServiceContext:
    """Immutable context carried through every application-service call.

    Args:
        correlation_id: A unique identifier that traces the entire
            request/response lifecycle.  Auto-generated when omitted.
        user_id: The identifier of the authenticated user, if any.
        permissions: The permissions held by the current user.
        metadata: Arbitrary key-value pairs for cross-cutting concerns
            (tracing, feature flags, tenant id, etc.).
    """

    correlation_id: uuid.UUID = field(default_factory=uuid.uuid4)
    user_id: str | None = None
    permissions: list[str] = field(default_factory=list[str])
    metadata: dict[str, Any] = field(default_factory=dict[str, Any])