Skip to content

feat: per-call-context Identity, Causation, and CorrelationId managers#13

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/setup-causedby-causation-correlationid
Draft

feat: per-call-context Identity, Causation, and CorrelationId managers#13
Copilot wants to merge 2 commits into
mainfrom
copilot/setup-causedby-causation-correlationid

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 11, 2026

Mirrors the .NET client's auditing/identity infrastructure for the TypeScript client. Each of the three concerns uses Node.js AsyncLocalStorage for per-async-context scoping, with module-level singletons and exported interfaces for consumer override.

New modules

Source/Identity/

  • Identity — value type with subject, name, userName, onBehalfOf; statics: system, notSet, unknown
  • IIdentityProvider / IdentityProvidergetCurrent(), setCurrentIdentity(), clearCurrentIdentity(); defaults to Identity.system
  • Exports singleton identityProvider

Source/Auditing/

  • CausationType — named type with statics root, unknown, appendEvent, appendManyEvents
  • Causationoccurred, type, properties; factory Causation.unknown()
  • ICausationManager / CausationManagergetCurrentChain() returns the full per-context chain (seeded with root); add() appends; defineRoot() sets the process-level root
  • Exports singleton causationManager

Source/Correlation/

  • CorrelationId — value type; CorrelationId.create() generates a new GUID-backed id
  • ICorrelationIdAccessor (read) / ICorrelationIdSetter (write) — interface segregation matching .NET pattern
  • CorrelationIdManager — implements both; current auto-generates if none set in context
  • Exports singleton correlationIdManager

EventSequence wiring

append and appendMany now pull identity, causation chain, and correlation ID from the singletons instead of hard-coded [System] / empty values:

// Before (all hard-coded)
CausedBy: { Subject: '5d032c92-...', Name: '[System]', ... }
Causation: [{ Type: 'TypeScriptClient.Append', ... }]

// After (context-aware)
identityProvider.setCurrentIdentity(new Identity(sub, name, userName));
causationManager.add(CausationType.appendEvent, { eventType: id });

// EventSequence picks up whatever is in the current async context
const identity = identityProvider.getCurrent();
const causationChain = causationManager.getCurrentChain();
const correlationId = correlationIdManager.current;

All three singletons plus their interfaces and value types are re-exported from the package root.

Copilot AI linked an issue May 11, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Implement CausedBy, Causation, and CorrelationId setup feat: per-call-context Identity, Causation, and CorrelationId managers May 11, 2026
Copilot AI requested a review from einari May 11, 2026 11:52
Copilot finished work on behalf of einari May 11, 2026 11:52
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.

Identity, causation, correlation

2 participants