Skip to content

Implement an immutable SDK request context with scoped metadata propagation #467

Description

@Lakes41

Difficulty: Expert
Type: Feature
Recommended labels (if available in this repo): backend, type-safety, consistency, expert

Background

As the GuildPass SDK grows, individual operations may need to carry scoped metadata such as request identifiers, caller tags, tracing values, or diagnostic attributes through internal layers.

Passing mutable plain objects through many functions can create hidden coupling and accidental mutation.

This issue introduces a generic immutable request-context primitive without integrating it into transport or client methods.

Problem

There is currently no standard SDK mechanism for safely propagating scoped metadata through nested operations.

A mutable context object can be changed by one internal function and unexpectedly affect unrelated callers or sibling operations.

Expected Outcome

Implement an immutable request context that supports typed metadata lookup, derivation, and controlled propagation.

Suggested Implementation

The design may include APIs conceptually similar to:

const child = context.with("requestId", "req_123");
const value = child.get("requestId");

A stronger typed-key design is preferred over an unrestricted global string dictionary if practical.

The context should:

  • support creation of an empty context;
  • support deriving a new context with additional metadata;
  • leave the parent unchanged;
  • support retrieving values by key;
  • distinguish an absent key from a key storing undefined if undefined is supported;
  • support nested derivation efficiently;
  • prevent callers from mutating internal stored values through exposed collections where practical;
  • optionally support removal or shadowing semantics if clearly documented;
  • provide deterministic conversion to safe diagnostic metadata for primitive values;
  • avoid relying on AsyncLocalStorage or global process state;
  • remain generic and framework-independent.

Acceptance Criteria

  • Empty contexts can be created.
  • Derived contexts inherit parent values.
  • Parent contexts remain unchanged after derivation.
  • Child values can shadow parent values according to documented semantics.
  • Independent branches derived from the same parent do not affect each other.
  • Missing-key behaviour is explicit.
  • Internal maps or collections are not exposed mutably.
  • Deep derivation chains behave correctly.
  • Unit tests cover inheritance, shadowing, sibling isolation, and immutability.
  • Type-level tests or compile-time examples demonstrate safe typed-key usage if implemented.
  • pnpm typecheck passes.
  • pnpm build passes.
  • pnpm test passes.
  • No transport integration, AsyncLocalStorage, or global context management is introduced.

Likely Affected Files/Directories

src/context/ or equivalent focused module
src/types/
tests/

Independence Requirement

This issue must be independently implementable from the current SDK baseline and must not depend on middleware, diagnostics, or transport contributions.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

GrantFox OSSGrantFox Open Source Sponsorship program tagMaybe RewardedIssue may qualify for a reward upon successful completion per campaign rulesThird CampaignOfficial FWC26 campaign issue — eligible for campaign scoring and rewardsbackendBackend services, application logic, persistence integration, and server-side functionalityconsistencyPattern and convention standardization across the codebase for uniformityexpertExpert difficulty tasks requiring deep expertise and architectural decision-makingtype-safetyTypeScript type system improvements and strict type enforcement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions